Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selinux: specialize symtab insert and search functions

This encapsulates symtab a little better and will help with further
refactoring later.

Signed-off-by: Ondrej Mosnacek <omosnace@redhat.com>
Acked-by: Stephen Smalley <stephen.smalley.work@gmail.com>
Signed-off-by: Paul Moore <paul@paul-moore.com>

authored by

Ondrej Mosnacek and committed by
Paul Moore
237389e3 2c3d8dfe

+69 -56
+2 -2
security/selinux/ss/conditional.c
··· 200 200 return 1; 201 201 } 202 202 203 - int cond_read_bool(struct policydb *p, struct hashtab *h, void *fp) 203 + int cond_read_bool(struct policydb *p, struct symtab *s, void *fp) 204 204 { 205 205 char *key = NULL; 206 206 struct cond_bool_datum *booldatum; ··· 235 235 if (rc) 236 236 goto err; 237 237 key[len] = '\0'; 238 - rc = hashtab_insert(h, key, booldatum); 238 + rc = symtab_insert(s, key, booldatum); 239 239 if (rc) 240 240 goto err; 241 241
+1 -1
security/selinux/ss/conditional.h
··· 69 69 70 70 int cond_index_bool(void *key, void *datum, void *datap); 71 71 72 - int cond_read_bool(struct policydb *p, struct hashtab *h, void *fp); 72 + int cond_read_bool(struct policydb *p, struct symtab *s, void *fp); 73 73 int cond_read_list(struct policydb *p, void *fp); 74 74 int cond_write_bool(void *key, void *datum, void *ptr); 75 75 int cond_write_list(struct policydb *p, void *fp);
+11 -10
security/selinux/ss/mls.c
··· 165 165 166 166 if (!l->sens || l->sens > p->p_levels.nprim) 167 167 return 0; 168 - levdatum = hashtab_search(&p->p_levels.table, 169 - sym_name(p, SYM_LEVELS, l->sens - 1)); 168 + levdatum = symtab_search(&p->p_levels, 169 + sym_name(p, SYM_LEVELS, l->sens - 1)); 170 170 if (!levdatum) 171 171 return 0; 172 172 ··· 293 293 *(next_cat++) = '\0'; 294 294 295 295 /* Parse sensitivity. */ 296 - levdatum = hashtab_search(&pol->p_levels.table, sensitivity); 296 + levdatum = symtab_search(&pol->p_levels, sensitivity); 297 297 if (!levdatum) 298 298 return -EINVAL; 299 299 context->range.level[l].sens = levdatum->level->sens; ··· 312 312 *rngptr++ = '\0'; 313 313 } 314 314 315 - catdatum = hashtab_search(&pol->p_cats.table, cur_cat); 315 + catdatum = symtab_search(&pol->p_cats, cur_cat); 316 316 if (!catdatum) 317 317 return -EINVAL; 318 318 ··· 325 325 if (rngptr == NULL) 326 326 continue; 327 327 328 - rngdatum = hashtab_search(&pol->p_cats.table, rngptr); 328 + rngdatum = symtab_search(&pol->p_cats, rngptr); 329 329 if (!rngdatum) 330 330 return -EINVAL; 331 331 ··· 458 458 return 0; 459 459 460 460 for (l = 0; l < 2; l++) { 461 - levdatum = hashtab_search(&newp->p_levels.table, 462 - sym_name(oldp, SYM_LEVELS, 463 - oldc->range.level[l].sens - 1)); 461 + char *name = sym_name(oldp, SYM_LEVELS, 462 + oldc->range.level[l].sens - 1); 463 + 464 + levdatum = symtab_search(&newp->p_levels, name); 464 465 465 466 if (!levdatum) 466 467 return -EINVAL; ··· 471 470 node, i) { 472 471 int rc; 473 472 474 - catdatum = hashtab_search(&newp->p_cats.table, 475 - sym_name(oldp, SYM_CATS, i)); 473 + catdatum = symtab_search(&newp->p_cats, 474 + sym_name(oldp, SYM_CATS, i)); 476 475 if (!catdatum) 477 476 return -EINVAL; 478 477 rc = ebitmap_set_bit(&newc->range.level[l].cat,
+26 -26
security/selinux/ss/policydb.c
··· 400 400 if (!key) 401 401 goto out; 402 402 403 - rc = hashtab_insert(&p->p_roles.table, key, role); 403 + rc = symtab_insert(&p->p_roles, key, role); 404 404 if (rc) 405 405 goto out; 406 406 ··· 1065 1065 return 0; 1066 1066 } 1067 1067 1068 - static int perm_read(struct policydb *p, struct hashtab *h, void *fp) 1068 + static int perm_read(struct policydb *p, struct symtab *s, void *fp) 1069 1069 { 1070 1070 char *key = NULL; 1071 1071 struct perm_datum *perdatum; ··· 1088 1088 if (rc) 1089 1089 goto bad; 1090 1090 1091 - rc = hashtab_insert(h, key, perdatum); 1091 + rc = symtab_insert(s, key, perdatum); 1092 1092 if (rc) 1093 1093 goto bad; 1094 1094 ··· 1098 1098 return rc; 1099 1099 } 1100 1100 1101 - static int common_read(struct policydb *p, struct hashtab *h, void *fp) 1101 + static int common_read(struct policydb *p, struct symtab *s, void *fp) 1102 1102 { 1103 1103 char *key = NULL; 1104 1104 struct common_datum *comdatum; ··· 1128 1128 goto bad; 1129 1129 1130 1130 for (i = 0; i < nel; i++) { 1131 - rc = perm_read(p, &comdatum->permissions.table, fp); 1131 + rc = perm_read(p, &comdatum->permissions, fp); 1132 1132 if (rc) 1133 1133 goto bad; 1134 1134 } 1135 1135 1136 - rc = hashtab_insert(h, key, comdatum); 1136 + rc = symtab_insert(s, key, comdatum); 1137 1137 if (rc) 1138 1138 goto bad; 1139 1139 return 0; ··· 1262 1262 return 0; 1263 1263 } 1264 1264 1265 - static int class_read(struct policydb *p, struct hashtab *h, void *fp) 1265 + static int class_read(struct policydb *p, struct symtab *s, void *fp) 1266 1266 { 1267 1267 char *key = NULL; 1268 1268 struct class_datum *cladatum; ··· 1300 1300 goto bad; 1301 1301 1302 1302 rc = -EINVAL; 1303 - cladatum->comdatum = hashtab_search(&p->p_commons.table, 1304 - cladatum->comkey); 1303 + cladatum->comdatum = symtab_search(&p->p_commons, 1304 + cladatum->comkey); 1305 1305 if (!cladatum->comdatum) { 1306 1306 pr_err("SELinux: unknown common %s\n", 1307 1307 cladatum->comkey); ··· 1309 1309 } 1310 1310 } 1311 1311 for (i = 0; i < nel; i++) { 1312 - rc = perm_read(p, &cladatum->permissions.table, fp); 1312 + rc = perm_read(p, &cladatum->permissions, fp); 1313 1313 if (rc) 1314 1314 goto bad; 1315 1315 } ··· 1347 1347 cladatum->default_type = le32_to_cpu(buf[0]); 1348 1348 } 1349 1349 1350 - rc = hashtab_insert(h, key, cladatum); 1350 + rc = symtab_insert(s, key, cladatum); 1351 1351 if (rc) 1352 1352 goto bad; 1353 1353 ··· 1357 1357 return rc; 1358 1358 } 1359 1359 1360 - static int role_read(struct policydb *p, struct hashtab *h, void *fp) 1360 + static int role_read(struct policydb *p, struct symtab *s, void *fp) 1361 1361 { 1362 1362 char *key = NULL; 1363 1363 struct role_datum *role; ··· 1404 1404 goto bad; 1405 1405 } 1406 1406 1407 - rc = hashtab_insert(h, key, role); 1407 + rc = symtab_insert(s, key, role); 1408 1408 if (rc) 1409 1409 goto bad; 1410 1410 return 0; ··· 1413 1413 return rc; 1414 1414 } 1415 1415 1416 - static int type_read(struct policydb *p, struct hashtab *h, void *fp) 1416 + static int type_read(struct policydb *p, struct symtab *s, void *fp) 1417 1417 { 1418 1418 char *key = NULL; 1419 1419 struct type_datum *typdatum; ··· 1451 1451 if (rc) 1452 1452 goto bad; 1453 1453 1454 - rc = hashtab_insert(h, key, typdatum); 1454 + rc = symtab_insert(s, key, typdatum); 1455 1455 if (rc) 1456 1456 goto bad; 1457 1457 return 0; ··· 1487 1487 return 0; 1488 1488 } 1489 1489 1490 - static int user_read(struct policydb *p, struct hashtab *h, void *fp) 1490 + static int user_read(struct policydb *p, struct symtab *s, void *fp) 1491 1491 { 1492 1492 char *key = NULL; 1493 1493 struct user_datum *usrdatum; ··· 1528 1528 goto bad; 1529 1529 } 1530 1530 1531 - rc = hashtab_insert(h, key, usrdatum); 1531 + rc = symtab_insert(s, key, usrdatum); 1532 1532 if (rc) 1533 1533 goto bad; 1534 1534 return 0; ··· 1537 1537 return rc; 1538 1538 } 1539 1539 1540 - static int sens_read(struct policydb *p, struct hashtab *h, void *fp) 1540 + static int sens_read(struct policydb *p, struct symtab *s, void *fp) 1541 1541 { 1542 1542 char *key = NULL; 1543 1543 struct level_datum *levdatum; ··· 1569 1569 if (rc) 1570 1570 goto bad; 1571 1571 1572 - rc = hashtab_insert(h, key, levdatum); 1572 + rc = symtab_insert(s, key, levdatum); 1573 1573 if (rc) 1574 1574 goto bad; 1575 1575 return 0; ··· 1578 1578 return rc; 1579 1579 } 1580 1580 1581 - static int cat_read(struct policydb *p, struct hashtab *h, void *fp) 1581 + static int cat_read(struct policydb *p, struct symtab *s, void *fp) 1582 1582 { 1583 1583 char *key = NULL; 1584 1584 struct cat_datum *catdatum; ··· 1602 1602 if (rc) 1603 1603 goto bad; 1604 1604 1605 - rc = hashtab_insert(h, key, catdatum); 1605 + rc = symtab_insert(s, key, catdatum); 1606 1606 if (rc) 1607 1607 goto bad; 1608 1608 return 0; ··· 1611 1611 return rc; 1612 1612 } 1613 1613 1614 - static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) = 1614 + static int (*read_f[SYM_NUM]) (struct policydb *p, struct symtab *s, void *fp) = 1615 1615 { 1616 1616 common_read, 1617 1617 class_read, ··· 1751 1751 { 1752 1752 struct class_datum *cladatum; 1753 1753 1754 - cladatum = hashtab_search(&p->p_classes.table, name); 1754 + cladatum = symtab_search(&p->p_classes, name); 1755 1755 if (!cladatum) 1756 1756 return 0; 1757 1757 ··· 1770 1770 cladatum = p->class_val_to_struct[tclass-1]; 1771 1771 comdatum = cladatum->comdatum; 1772 1772 if (comdatum) 1773 - perdatum = hashtab_search(&comdatum->permissions.table, name); 1773 + perdatum = symtab_search(&comdatum->permissions, name); 1774 1774 if (!perdatum) 1775 - perdatum = hashtab_search(&cladatum->permissions.table, name); 1775 + perdatum = symtab_search(&cladatum->permissions, name); 1776 1776 if (!perdatum) 1777 1777 return 0; 1778 1778 ··· 2509 2509 } 2510 2510 2511 2511 for (j = 0; j < nel; j++) { 2512 - rc = read_f[i](p, &p->symtab[i].table, fp); 2512 + rc = read_f[i](p, &p->symtab[i], fp); 2513 2513 if (rc) 2514 2514 goto bad; 2515 2515 }
+17 -17
security/selinux/ss/services.c
··· 1441 1441 1442 1442 *p++ = 0; 1443 1443 1444 - usrdatum = hashtab_search(&pol->p_users.table, scontextp); 1444 + usrdatum = symtab_search(&pol->p_users, scontextp); 1445 1445 if (!usrdatum) 1446 1446 goto out; 1447 1447 ··· 1457 1457 1458 1458 *p++ = 0; 1459 1459 1460 - role = hashtab_search(&pol->p_roles.table, scontextp); 1460 + role = symtab_search(&pol->p_roles, scontextp); 1461 1461 if (!role) 1462 1462 goto out; 1463 1463 ctx->role = role->value; ··· 1469 1469 oldc = *p; 1470 1470 *p++ = 0; 1471 1471 1472 - typdatum = hashtab_search(&pol->p_types.table, scontextp); 1472 + typdatum = symtab_search(&pol->p_types, scontextp); 1473 1473 if (!typdatum || typdatum->attribute) 1474 1474 goto out; 1475 1475 ··· 2024 2024 2025 2025 /* Convert the user. */ 2026 2026 rc = -EINVAL; 2027 - usrdatum = hashtab_search(&args->newp->p_users.table, 2028 - sym_name(args->oldp, 2029 - SYM_USERS, oldc->user - 1)); 2027 + usrdatum = symtab_search(&args->newp->p_users, 2028 + sym_name(args->oldp, 2029 + SYM_USERS, oldc->user - 1)); 2030 2030 if (!usrdatum) 2031 2031 goto bad; 2032 2032 newc->user = usrdatum->value; 2033 2033 2034 2034 /* Convert the role. */ 2035 2035 rc = -EINVAL; 2036 - role = hashtab_search(&args->newp->p_roles.table, 2037 - sym_name(args->oldp, SYM_ROLES, oldc->role - 1)); 2036 + role = symtab_search(&args->newp->p_roles, 2037 + sym_name(args->oldp, SYM_ROLES, oldc->role - 1)); 2038 2038 if (!role) 2039 2039 goto bad; 2040 2040 newc->role = role->value; 2041 2041 2042 2042 /* Convert the type. */ 2043 2043 rc = -EINVAL; 2044 - typdatum = hashtab_search(&args->newp->p_types.table, 2045 - sym_name(args->oldp, 2046 - SYM_TYPES, oldc->type - 1)); 2044 + typdatum = symtab_search(&args->newp->p_types, 2045 + sym_name(args->oldp, 2046 + SYM_TYPES, oldc->type - 1)); 2047 2047 if (!typdatum) 2048 2048 goto bad; 2049 2049 newc->type = typdatum->value; ··· 2623 2623 goto out_unlock; 2624 2624 2625 2625 rc = -EINVAL; 2626 - user = hashtab_search(&policydb->p_users.table, username); 2626 + user = symtab_search(&policydb->p_users, username); 2627 2627 if (!user) 2628 2628 goto out_unlock; 2629 2629 ··· 2975 2975 if (rc) 2976 2976 goto out; 2977 2977 for (i = 0; i < nbools; i++) { 2978 - booldatum = hashtab_search(&policydb->p_bools.table, bnames[i]); 2978 + booldatum = symtab_search(&policydb->p_bools, bnames[i]); 2979 2979 if (booldatum) 2980 2980 booldatum->state = bvalues[i]; 2981 2981 } ··· 3226 3226 read_lock(&state->ss->policy_rwlock); 3227 3227 3228 3228 rc = -EINVAL; 3229 - match = hashtab_search(&policydb->p_classes.table, class); 3229 + match = symtab_search(&policydb->p_classes, class); 3230 3230 if (!match) { 3231 3231 pr_err("SELinux: %s: unrecognized class %s\n", 3232 3232 __func__, class); ··· 3365 3365 case AUDIT_SUBJ_USER: 3366 3366 case AUDIT_OBJ_USER: 3367 3367 rc = -EINVAL; 3368 - userdatum = hashtab_search(&policydb->p_users.table, rulestr); 3368 + userdatum = symtab_search(&policydb->p_users, rulestr); 3369 3369 if (!userdatum) 3370 3370 goto out; 3371 3371 tmprule->au_ctxt.user = userdatum->value; ··· 3373 3373 case AUDIT_SUBJ_ROLE: 3374 3374 case AUDIT_OBJ_ROLE: 3375 3375 rc = -EINVAL; 3376 - roledatum = hashtab_search(&policydb->p_roles.table, rulestr); 3376 + roledatum = symtab_search(&policydb->p_roles, rulestr); 3377 3377 if (!roledatum) 3378 3378 goto out; 3379 3379 tmprule->au_ctxt.role = roledatum->value; ··· 3381 3381 case AUDIT_SUBJ_TYPE: 3382 3382 case AUDIT_OBJ_TYPE: 3383 3383 rc = -EINVAL; 3384 - typedatum = hashtab_search(&policydb->p_types.table, rulestr); 3384 + typedatum = symtab_search(&policydb->p_types, rulestr); 3385 3385 if (!typedatum) 3386 3386 goto out; 3387 3387 tmprule->au_ctxt.type = typedatum->value;
+9
security/selinux/ss/symtab.c
··· 39 39 return hashtab_init(&s->table, symhash, symcmp, size); 40 40 } 41 41 42 + int symtab_insert(struct symtab *s, char *name, void *datum) 43 + { 44 + return hashtab_insert(&s->table, name, datum); 45 + } 46 + 47 + void *symtab_search(struct symtab *s, const char *name) 48 + { 49 + return hashtab_search(&s->table, name); 50 + }
+3
security/selinux/ss/symtab.h
··· 19 19 20 20 int symtab_init(struct symtab *s, unsigned int size); 21 21 22 + int symtab_insert(struct symtab *s, char *name, void *datum); 23 + void *symtab_search(struct symtab *s, const char *name); 24 + 22 25 #endif /* _SS_SYMTAB_H_ */ 23 26 24 27