diff options
author | Namhyung Kim <namhyung@kernel.org> | 2014-06-15 10:02:51 -0400 |
---|---|---|
committer | Paul Moore <pmoore@redhat.com> | 2014-06-18 15:55:58 -0400 |
commit | 4b6f405f72112175a5e044b015c4119b3a5b6f52 (patch) | |
tree | 66869f4749d44bd9718d58d7bede850495ca877f /security | |
parent | 5c7001b84be55917a99c35ce96df7e67b3c40cea (diff) |
selinux: introduce str_read() helper
There're some code duplication for reading a string value during
policydb_read(). Add str_read() helper to fix it.
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Signed-off-by: Paul Moore <pmoore@redhat.com>
Diffstat (limited to 'security')
-rw-r--r-- | security/selinux/ss/policydb.c | 133 |
1 files changed, 37 insertions, 96 deletions
diff --git a/security/selinux/ss/policydb.c b/security/selinux/ss/policydb.c index 56eb65f67cb1..bc2a586f095c 100644 --- a/security/selinux/ss/policydb.c +++ b/security/selinux/ss/policydb.c | |||
@@ -1080,6 +1080,26 @@ out: | |||
1080 | * binary representation file. | 1080 | * binary representation file. |
1081 | */ | 1081 | */ |
1082 | 1082 | ||
1083 | static int str_read(char **strp, gfp_t flags, void *fp, u32 len) | ||
1084 | { | ||
1085 | int rc; | ||
1086 | char *str; | ||
1087 | |||
1088 | str = kmalloc(len + 1, flags); | ||
1089 | if (!str) | ||
1090 | return -ENOMEM; | ||
1091 | |||
1092 | /* it's expected the caller should free the str */ | ||
1093 | *strp = str; | ||
1094 | |||
1095 | rc = next_entry(str, fp, len); | ||
1096 | if (rc) | ||
1097 | return rc; | ||
1098 | |||
1099 | str[len] = '\0'; | ||
1100 | return 0; | ||
1101 | } | ||
1102 | |||
1083 | static int perm_read(struct policydb *p, struct hashtab *h, void *fp) | 1103 | static int perm_read(struct policydb *p, struct hashtab *h, void *fp) |
1084 | { | 1104 | { |
1085 | char *key = NULL; | 1105 | char *key = NULL; |
@@ -1100,15 +1120,9 @@ static int perm_read(struct policydb *p, struct hashtab *h, void *fp) | |||
1100 | len = le32_to_cpu(buf[0]); | 1120 | len = le32_to_cpu(buf[0]); |
1101 | perdatum->value = le32_to_cpu(buf[1]); | 1121 | perdatum->value = le32_to_cpu(buf[1]); |
1102 | 1122 | ||
1103 | rc = -ENOMEM; | 1123 | rc = str_read(&key, GFP_KERNEL, fp, len); |
1104 | key = kmalloc(len + 1, GFP_KERNEL); | ||
1105 | if (!key) | ||
1106 | goto bad; | ||
1107 | |||
1108 | rc = next_entry(key, fp, len); | ||
1109 | if (rc) | 1124 | if (rc) |
1110 | goto bad; | 1125 | goto bad; |
1111 | key[len] = '\0'; | ||
1112 | 1126 | ||
1113 | rc = hashtab_insert(h, key, perdatum); | 1127 | rc = hashtab_insert(h, key, perdatum); |
1114 | if (rc) | 1128 | if (rc) |
@@ -1146,15 +1160,9 @@ static int common_read(struct policydb *p, struct hashtab *h, void *fp) | |||
1146 | comdatum->permissions.nprim = le32_to_cpu(buf[2]); | 1160 | comdatum->permissions.nprim = le32_to_cpu(buf[2]); |
1147 | nel = le32_to_cpu(buf[3]); | 1161 | nel = le32_to_cpu(buf[3]); |
1148 | 1162 | ||
1149 | rc = -ENOMEM; | 1163 | rc = str_read(&key, GFP_KERNEL, fp, len); |
1150 | key = kmalloc(len + 1, GFP_KERNEL); | ||
1151 | if (!key) | ||
1152 | goto bad; | ||
1153 | |||
1154 | rc = next_entry(key, fp, len); | ||
1155 | if (rc) | 1164 | if (rc) |
1156 | goto bad; | 1165 | goto bad; |
1157 | key[len] = '\0'; | ||
1158 | 1166 | ||
1159 | for (i = 0; i < nel; i++) { | 1167 | for (i = 0; i < nel; i++) { |
1160 | rc = perm_read(p, comdatum->permissions.table, fp); | 1168 | rc = perm_read(p, comdatum->permissions.table, fp); |
@@ -1321,25 +1329,14 @@ static int class_read(struct policydb *p, struct hashtab *h, void *fp) | |||
1321 | 1329 | ||
1322 | ncons = le32_to_cpu(buf[5]); | 1330 | ncons = le32_to_cpu(buf[5]); |
1323 | 1331 | ||
1324 | rc = -ENOMEM; | 1332 | rc = str_read(&key, GFP_KERNEL, fp, len); |
1325 | key = kmalloc(len + 1, GFP_KERNEL); | ||
1326 | if (!key) | ||
1327 | goto bad; | ||
1328 | |||
1329 | rc = next_entry(key, fp, len); | ||
1330 | if (rc) | 1333 | if (rc) |
1331 | goto bad; | 1334 | goto bad; |
1332 | key[len] = '\0'; | ||
1333 | 1335 | ||
1334 | if (len2) { | 1336 | if (len2) { |
1335 | rc = -ENOMEM; | 1337 | rc = str_read(&cladatum->comkey, GFP_KERNEL, fp, len2); |
1336 | cladatum->comkey = kmalloc(len2 + 1, GFP_KERNEL); | ||
1337 | if (!cladatum->comkey) | ||
1338 | goto bad; | ||
1339 | rc = next_entry(cladatum->comkey, fp, len2); | ||
1340 | if (rc) | 1338 | if (rc) |
1341 | goto bad; | 1339 | goto bad; |
1342 | cladatum->comkey[len2] = '\0'; | ||
1343 | 1340 | ||
1344 | rc = -EINVAL; | 1341 | rc = -EINVAL; |
1345 | cladatum->comdatum = hashtab_search(p->p_commons.table, cladatum->comkey); | 1342 | cladatum->comdatum = hashtab_search(p->p_commons.table, cladatum->comkey); |
@@ -1422,15 +1419,9 @@ static int role_read(struct policydb *p, struct hashtab *h, void *fp) | |||
1422 | if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) | 1419 | if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) |
1423 | role->bounds = le32_to_cpu(buf[2]); | 1420 | role->bounds = le32_to_cpu(buf[2]); |
1424 | 1421 | ||
1425 | rc = -ENOMEM; | 1422 | rc = str_read(&key, GFP_KERNEL, fp, len); |
1426 | key = kmalloc(len + 1, GFP_KERNEL); | ||
1427 | if (!key) | ||
1428 | goto bad; | ||
1429 | |||
1430 | rc = next_entry(key, fp, len); | ||
1431 | if (rc) | 1423 | if (rc) |
1432 | goto bad; | 1424 | goto bad; |
1433 | key[len] = '\0'; | ||
1434 | 1425 | ||
1435 | rc = ebitmap_read(&role->dominates, fp); | 1426 | rc = ebitmap_read(&role->dominates, fp); |
1436 | if (rc) | 1427 | if (rc) |
@@ -1495,14 +1486,9 @@ static int type_read(struct policydb *p, struct hashtab *h, void *fp) | |||
1495 | typdatum->primary = le32_to_cpu(buf[2]); | 1486 | typdatum->primary = le32_to_cpu(buf[2]); |
1496 | } | 1487 | } |
1497 | 1488 | ||
1498 | rc = -ENOMEM; | 1489 | rc = str_read(&key, GFP_KERNEL, fp, len); |
1499 | key = kmalloc(len + 1, GFP_KERNEL); | ||
1500 | if (!key) | ||
1501 | goto bad; | ||
1502 | rc = next_entry(key, fp, len); | ||
1503 | if (rc) | 1490 | if (rc) |
1504 | goto bad; | 1491 | goto bad; |
1505 | key[len] = '\0'; | ||
1506 | 1492 | ||
1507 | rc = hashtab_insert(h, key, typdatum); | 1493 | rc = hashtab_insert(h, key, typdatum); |
1508 | if (rc) | 1494 | if (rc) |
@@ -1565,14 +1551,9 @@ static int user_read(struct policydb *p, struct hashtab *h, void *fp) | |||
1565 | if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) | 1551 | if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) |
1566 | usrdatum->bounds = le32_to_cpu(buf[2]); | 1552 | usrdatum->bounds = le32_to_cpu(buf[2]); |
1567 | 1553 | ||
1568 | rc = -ENOMEM; | 1554 | rc = str_read(&key, GFP_KERNEL, fp, len); |
1569 | key = kmalloc(len + 1, GFP_KERNEL); | ||
1570 | if (!key) | ||
1571 | goto bad; | ||
1572 | rc = next_entry(key, fp, len); | ||
1573 | if (rc) | 1555 | if (rc) |
1574 | goto bad; | 1556 | goto bad; |
1575 | key[len] = '\0'; | ||
1576 | 1557 | ||
1577 | rc = ebitmap_read(&usrdatum->roles, fp); | 1558 | rc = ebitmap_read(&usrdatum->roles, fp); |
1578 | if (rc) | 1559 | if (rc) |
@@ -1616,14 +1597,9 @@ static int sens_read(struct policydb *p, struct hashtab *h, void *fp) | |||
1616 | len = le32_to_cpu(buf[0]); | 1597 | len = le32_to_cpu(buf[0]); |
1617 | levdatum->isalias = le32_to_cpu(buf[1]); | 1598 | levdatum->isalias = le32_to_cpu(buf[1]); |
1618 | 1599 | ||
1619 | rc = -ENOMEM; | 1600 | rc = str_read(&key, GFP_ATOMIC, fp, len); |
1620 | key = kmalloc(len + 1, GFP_ATOMIC); | ||
1621 | if (!key) | ||
1622 | goto bad; | ||
1623 | rc = next_entry(key, fp, len); | ||
1624 | if (rc) | 1601 | if (rc) |
1625 | goto bad; | 1602 | goto bad; |
1626 | key[len] = '\0'; | ||
1627 | 1603 | ||
1628 | rc = -ENOMEM; | 1604 | rc = -ENOMEM; |
1629 | levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC); | 1605 | levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC); |
@@ -1664,14 +1640,9 @@ static int cat_read(struct policydb *p, struct hashtab *h, void *fp) | |||
1664 | catdatum->value = le32_to_cpu(buf[1]); | 1640 | catdatum->value = le32_to_cpu(buf[1]); |
1665 | catdatum->isalias = le32_to_cpu(buf[2]); | 1641 | catdatum->isalias = le32_to_cpu(buf[2]); |
1666 | 1642 | ||
1667 | rc = -ENOMEM; | 1643 | rc = str_read(&key, GFP_ATOMIC, fp, len); |
1668 | key = kmalloc(len + 1, GFP_ATOMIC); | ||
1669 | if (!key) | ||
1670 | goto bad; | ||
1671 | rc = next_entry(key, fp, len); | ||
1672 | if (rc) | 1644 | if (rc) |
1673 | goto bad; | 1645 | goto bad; |
1674 | key[len] = '\0'; | ||
1675 | 1646 | ||
1676 | rc = hashtab_insert(h, key, catdatum); | 1647 | rc = hashtab_insert(h, key, catdatum); |
1677 | if (rc) | 1648 | if (rc) |
@@ -1968,18 +1939,12 @@ static int filename_trans_read(struct policydb *p, void *fp) | |||
1968 | goto out; | 1939 | goto out; |
1969 | len = le32_to_cpu(buf[0]); | 1940 | len = le32_to_cpu(buf[0]); |
1970 | 1941 | ||
1971 | rc = -ENOMEM; | ||
1972 | name = kmalloc(len + 1, GFP_KERNEL); | ||
1973 | if (!name) | ||
1974 | goto out; | ||
1975 | |||
1976 | ft->name = name; | ||
1977 | |||
1978 | /* path component string */ | 1942 | /* path component string */ |
1979 | rc = next_entry(name, fp, len); | 1943 | rc = str_read(&name, GFP_KERNEL, fp, len); |
1980 | if (rc) | 1944 | if (rc) |
1981 | goto out; | 1945 | goto out; |
1982 | name[len] = 0; | 1946 | |
1947 | ft->name = name; | ||
1983 | 1948 | ||
1984 | rc = next_entry(buf, fp, sizeof(u32) * 4); | 1949 | rc = next_entry(buf, fp, sizeof(u32) * 4); |
1985 | if (rc) | 1950 | if (rc) |
@@ -2045,17 +2010,10 @@ static int genfs_read(struct policydb *p, void *fp) | |||
2045 | if (!newgenfs) | 2010 | if (!newgenfs) |
2046 | goto out; | 2011 | goto out; |
2047 | 2012 | ||
2048 | rc = -ENOMEM; | 2013 | rc = str_read(&newgenfs->fstype, GFP_KERNEL, fp, len); |
2049 | newgenfs->fstype = kmalloc(len + 1, GFP_KERNEL); | ||
2050 | if (!newgenfs->fstype) | ||
2051 | goto out; | ||
2052 | |||
2053 | rc = next_entry(newgenfs->fstype, fp, len); | ||
2054 | if (rc) | 2014 | if (rc) |
2055 | goto out; | 2015 | goto out; |
2056 | 2016 | ||
2057 | newgenfs->fstype[len] = 0; | ||
2058 | |||
2059 | for (genfs_p = NULL, genfs = p->genfs; genfs; | 2017 | for (genfs_p = NULL, genfs = p->genfs; genfs; |
2060 | genfs_p = genfs, genfs = genfs->next) { | 2018 | genfs_p = genfs, genfs = genfs->next) { |
2061 | rc = -EINVAL; | 2019 | rc = -EINVAL; |
@@ -2091,15 +2049,9 @@ static int genfs_read(struct policydb *p, void *fp) | |||
2091 | if (!newc) | 2049 | if (!newc) |
2092 | goto out; | 2050 | goto out; |
2093 | 2051 | ||
2094 | rc = -ENOMEM; | 2052 | rc = str_read(&newc->u.name, GFP_KERNEL, fp, len); |
2095 | newc->u.name = kmalloc(len + 1, GFP_KERNEL); | ||
2096 | if (!newc->u.name) | ||
2097 | goto out; | ||
2098 | |||
2099 | rc = next_entry(newc->u.name, fp, len); | ||
2100 | if (rc) | 2053 | if (rc) |
2101 | goto out; | 2054 | goto out; |
2102 | newc->u.name[len] = 0; | ||
2103 | 2055 | ||
2104 | rc = next_entry(buf, fp, sizeof(u32)); | 2056 | rc = next_entry(buf, fp, sizeof(u32)); |
2105 | if (rc) | 2057 | if (rc) |
@@ -2189,16 +2141,10 @@ static int ocontext_read(struct policydb *p, struct policydb_compat_info *info, | |||
2189 | goto out; | 2141 | goto out; |
2190 | len = le32_to_cpu(buf[0]); | 2142 | len = le32_to_cpu(buf[0]); |
2191 | 2143 | ||
2192 | rc = -ENOMEM; | 2144 | rc = str_read(&c->u.name, GFP_KERNEL, fp, len); |
2193 | c->u.name = kmalloc(len + 1, GFP_KERNEL); | ||
2194 | if (!c->u.name) | ||
2195 | goto out; | ||
2196 | |||
2197 | rc = next_entry(c->u.name, fp, len); | ||
2198 | if (rc) | 2145 | if (rc) |
2199 | goto out; | 2146 | goto out; |
2200 | 2147 | ||
2201 | c->u.name[len] = 0; | ||
2202 | rc = context_read_and_validate(&c->context[0], p, fp); | 2148 | rc = context_read_and_validate(&c->context[0], p, fp); |
2203 | if (rc) | 2149 | if (rc) |
2204 | goto out; | 2150 | goto out; |
@@ -2240,16 +2186,11 @@ static int ocontext_read(struct policydb *p, struct policydb_compat_info *info, | |||
2240 | if (c->v.behavior > SECURITY_FS_USE_MAX) | 2186 | if (c->v.behavior > SECURITY_FS_USE_MAX) |
2241 | goto out; | 2187 | goto out; |
2242 | 2188 | ||
2243 | rc = -ENOMEM; | ||
2244 | len = le32_to_cpu(buf[1]); | 2189 | len = le32_to_cpu(buf[1]); |
2245 | c->u.name = kmalloc(len + 1, GFP_KERNEL); | 2190 | rc = str_read(&c->u.name, GFP_KERNEL, fp, len); |
2246 | if (!c->u.name) | ||
2247 | goto out; | ||
2248 | |||
2249 | rc = next_entry(c->u.name, fp, len); | ||
2250 | if (rc) | 2191 | if (rc) |
2251 | goto out; | 2192 | goto out; |
2252 | c->u.name[len] = 0; | 2193 | |
2253 | rc = context_read_and_validate(&c->context[0], p, fp); | 2194 | rc = context_read_and_validate(&c->context[0], p, fp); |
2254 | if (rc) | 2195 | if (rc) |
2255 | goto out; | 2196 | goto out; |