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 | |||
