Merge tag 'selinux-pr-20190917' of git://git.kernel.org/pub/scm/linux/kernel/git/pcmoore/selinux
Pull selinux updates from Paul Moore: - Add LSM hooks, and SELinux access control hooks, for dnotify, fanotify, and inotify watches. This has been discussed with both the LSM and fs/notify folks and everybody is good with these new hooks. - The LSM stacking changes missed a few calls to current_security() in the SELinux code; we fix those and remove current_security() for good. - Improve our network object labeling cache so that we always return the object's label, even when under memory pressure. Previously we would return an error if we couldn't allocate a new cache entry, now we always return the label even if we can't create a new cache entry for it. - Convert the sidtab atomic_t counter to a normal u32 with READ/WRITE_ONCE() and memory barrier protection. - A few patches to policydb.c to clean things up (remove forward declarations, long lines, bad variable names, etc) * tag 'selinux-pr-20190917' of git://git.kernel.org/pub/scm/linux/kernel/git/pcmoore/selinux: lsm: remove current_security() selinux: fix residual uses of current_security() for the SELinux blob selinux: avoid atomic_t usage in sidtab fanotify, inotify, dnotify, security: add security hook for fs notifications selinux: always return a secid from the network caches if we find one selinux: policydb - rename type_val_to_struct_array selinux: policydb - fix some checkpatch.pl warnings selinux: shuffle around policydb.c to get rid of forward declarations
This commit is contained in:
@@ -176,397 +176,6 @@ static struct policydb_compat_info *policydb_lookup_compat(int version)
|
||||
return info;
|
||||
}
|
||||
|
||||
/*
|
||||
* Initialize the role table.
|
||||
*/
|
||||
static int roles_init(struct policydb *p)
|
||||
{
|
||||
char *key = NULL;
|
||||
int rc;
|
||||
struct role_datum *role;
|
||||
|
||||
role = kzalloc(sizeof(*role), GFP_KERNEL);
|
||||
if (!role)
|
||||
return -ENOMEM;
|
||||
|
||||
rc = -EINVAL;
|
||||
role->value = ++p->p_roles.nprim;
|
||||
if (role->value != OBJECT_R_VAL)
|
||||
goto out;
|
||||
|
||||
rc = -ENOMEM;
|
||||
key = kstrdup(OBJECT_R, GFP_KERNEL);
|
||||
if (!key)
|
||||
goto out;
|
||||
|
||||
rc = hashtab_insert(p->p_roles.table, key, role);
|
||||
if (rc)
|
||||
goto out;
|
||||
|
||||
return 0;
|
||||
out:
|
||||
kfree(key);
|
||||
kfree(role);
|
||||
return rc;
|
||||
}
|
||||
|
||||
static u32 filenametr_hash(struct hashtab *h, const void *k)
|
||||
{
|
||||
const struct filename_trans *ft = k;
|
||||
unsigned long hash;
|
||||
unsigned int byte_num;
|
||||
unsigned char focus;
|
||||
|
||||
hash = ft->stype ^ ft->ttype ^ ft->tclass;
|
||||
|
||||
byte_num = 0;
|
||||
while ((focus = ft->name[byte_num++]))
|
||||
hash = partial_name_hash(focus, hash);
|
||||
return hash & (h->size - 1);
|
||||
}
|
||||
|
||||
static int filenametr_cmp(struct hashtab *h, const void *k1, const void *k2)
|
||||
{
|
||||
const struct filename_trans *ft1 = k1;
|
||||
const struct filename_trans *ft2 = k2;
|
||||
int v;
|
||||
|
||||
v = ft1->stype - ft2->stype;
|
||||
if (v)
|
||||
return v;
|
||||
|
||||
v = ft1->ttype - ft2->ttype;
|
||||
if (v)
|
||||
return v;
|
||||
|
||||
v = ft1->tclass - ft2->tclass;
|
||||
if (v)
|
||||
return v;
|
||||
|
||||
return strcmp(ft1->name, ft2->name);
|
||||
|
||||
}
|
||||
|
||||
static u32 rangetr_hash(struct hashtab *h, const void *k)
|
||||
{
|
||||
const struct range_trans *key = k;
|
||||
return (key->source_type + (key->target_type << 3) +
|
||||
(key->target_class << 5)) & (h->size - 1);
|
||||
}
|
||||
|
||||
static int rangetr_cmp(struct hashtab *h, const void *k1, const void *k2)
|
||||
{
|
||||
const struct range_trans *key1 = k1, *key2 = k2;
|
||||
int v;
|
||||
|
||||
v = key1->source_type - key2->source_type;
|
||||
if (v)
|
||||
return v;
|
||||
|
||||
v = key1->target_type - key2->target_type;
|
||||
if (v)
|
||||
return v;
|
||||
|
||||
v = key1->target_class - key2->target_class;
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap);
|
||||
|
||||
/*
|
||||
* Initialize a policy database structure.
|
||||
*/
|
||||
static int policydb_init(struct policydb *p)
|
||||
{
|
||||
int i, rc;
|
||||
|
||||
memset(p, 0, sizeof(*p));
|
||||
|
||||
for (i = 0; i < SYM_NUM; i++) {
|
||||
rc = symtab_init(&p->symtab[i], symtab_sizes[i]);
|
||||
if (rc)
|
||||
goto out;
|
||||
}
|
||||
|
||||
rc = avtab_init(&p->te_avtab);
|
||||
if (rc)
|
||||
goto out;
|
||||
|
||||
rc = roles_init(p);
|
||||
if (rc)
|
||||
goto out;
|
||||
|
||||
rc = cond_policydb_init(p);
|
||||
if (rc)
|
||||
goto out;
|
||||
|
||||
p->filename_trans = hashtab_create(filenametr_hash, filenametr_cmp, (1 << 10));
|
||||
if (!p->filename_trans) {
|
||||
rc = -ENOMEM;
|
||||
goto out;
|
||||
}
|
||||
|
||||
p->range_tr = hashtab_create(rangetr_hash, rangetr_cmp, 256);
|
||||
if (!p->range_tr) {
|
||||
rc = -ENOMEM;
|
||||
goto out;
|
||||
}
|
||||
|
||||
ebitmap_init(&p->filename_trans_ttypes);
|
||||
ebitmap_init(&p->policycaps);
|
||||
ebitmap_init(&p->permissive_map);
|
||||
|
||||
return 0;
|
||||
out:
|
||||
hashtab_destroy(p->filename_trans);
|
||||
hashtab_destroy(p->range_tr);
|
||||
for (i = 0; i < SYM_NUM; i++) {
|
||||
hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
|
||||
hashtab_destroy(p->symtab[i].table);
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
/*
|
||||
* The following *_index functions are used to
|
||||
* define the val_to_name and val_to_struct arrays
|
||||
* in a policy database structure. The val_to_name
|
||||
* arrays are used when converting security context
|
||||
* structures into string representations. The
|
||||
* val_to_struct arrays are used when the attributes
|
||||
* of a class, role, or user are needed.
|
||||
*/
|
||||
|
||||
static int common_index(void *key, void *datum, void *datap)
|
||||
{
|
||||
struct policydb *p;
|
||||
struct common_datum *comdatum;
|
||||
|
||||
comdatum = datum;
|
||||
p = datap;
|
||||
if (!comdatum->value || comdatum->value > p->p_commons.nprim)
|
||||
return -EINVAL;
|
||||
|
||||
p->sym_val_to_name[SYM_COMMONS][comdatum->value - 1] = key;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int class_index(void *key, void *datum, void *datap)
|
||||
{
|
||||
struct policydb *p;
|
||||
struct class_datum *cladatum;
|
||||
|
||||
cladatum = datum;
|
||||
p = datap;
|
||||
if (!cladatum->value || cladatum->value > p->p_classes.nprim)
|
||||
return -EINVAL;
|
||||
|
||||
p->sym_val_to_name[SYM_CLASSES][cladatum->value - 1] = key;
|
||||
p->class_val_to_struct[cladatum->value - 1] = cladatum;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int role_index(void *key, void *datum, void *datap)
|
||||
{
|
||||
struct policydb *p;
|
||||
struct role_datum *role;
|
||||
|
||||
role = datum;
|
||||
p = datap;
|
||||
if (!role->value
|
||||
|| role->value > p->p_roles.nprim
|
||||
|| role->bounds > p->p_roles.nprim)
|
||||
return -EINVAL;
|
||||
|
||||
p->sym_val_to_name[SYM_ROLES][role->value - 1] = key;
|
||||
p->role_val_to_struct[role->value - 1] = role;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int type_index(void *key, void *datum, void *datap)
|
||||
{
|
||||
struct policydb *p;
|
||||
struct type_datum *typdatum;
|
||||
|
||||
typdatum = datum;
|
||||
p = datap;
|
||||
|
||||
if (typdatum->primary) {
|
||||
if (!typdatum->value
|
||||
|| typdatum->value > p->p_types.nprim
|
||||
|| typdatum->bounds > p->p_types.nprim)
|
||||
return -EINVAL;
|
||||
p->sym_val_to_name[SYM_TYPES][typdatum->value - 1] = key;
|
||||
p->type_val_to_struct_array[typdatum->value - 1] = typdatum;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int user_index(void *key, void *datum, void *datap)
|
||||
{
|
||||
struct policydb *p;
|
||||
struct user_datum *usrdatum;
|
||||
|
||||
usrdatum = datum;
|
||||
p = datap;
|
||||
if (!usrdatum->value
|
||||
|| usrdatum->value > p->p_users.nprim
|
||||
|| usrdatum->bounds > p->p_users.nprim)
|
||||
return -EINVAL;
|
||||
|
||||
p->sym_val_to_name[SYM_USERS][usrdatum->value - 1] = key;
|
||||
p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int sens_index(void *key, void *datum, void *datap)
|
||||
{
|
||||
struct policydb *p;
|
||||
struct level_datum *levdatum;
|
||||
|
||||
levdatum = datum;
|
||||
p = datap;
|
||||
|
||||
if (!levdatum->isalias) {
|
||||
if (!levdatum->level->sens ||
|
||||
levdatum->level->sens > p->p_levels.nprim)
|
||||
return -EINVAL;
|
||||
|
||||
p->sym_val_to_name[SYM_LEVELS][levdatum->level->sens - 1] = key;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int cat_index(void *key, void *datum, void *datap)
|
||||
{
|
||||
struct policydb *p;
|
||||
struct cat_datum *catdatum;
|
||||
|
||||
catdatum = datum;
|
||||
p = datap;
|
||||
|
||||
if (!catdatum->isalias) {
|
||||
if (!catdatum->value || catdatum->value > p->p_cats.nprim)
|
||||
return -EINVAL;
|
||||
|
||||
p->sym_val_to_name[SYM_CATS][catdatum->value - 1] = key;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
|
||||
{
|
||||
common_index,
|
||||
class_index,
|
||||
role_index,
|
||||
type_index,
|
||||
user_index,
|
||||
cond_index_bool,
|
||||
sens_index,
|
||||
cat_index,
|
||||
};
|
||||
|
||||
#ifdef DEBUG_HASHES
|
||||
static void hash_eval(struct hashtab *h, const char *hash_name)
|
||||
{
|
||||
struct hashtab_info info;
|
||||
|
||||
hashtab_stat(h, &info);
|
||||
pr_debug("SELinux: %s: %d entries and %d/%d buckets used, "
|
||||
"longest chain length %d\n", hash_name, h->nel,
|
||||
info.slots_used, h->size, info.max_chain_len);
|
||||
}
|
||||
|
||||
static void symtab_hash_eval(struct symtab *s)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < SYM_NUM; i++)
|
||||
hash_eval(s[i].table, symtab_name[i]);
|
||||
}
|
||||
|
||||
#else
|
||||
static inline void hash_eval(struct hashtab *h, char *hash_name)
|
||||
{
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Define the other val_to_name and val_to_struct arrays
|
||||
* in a policy database structure.
|
||||
*
|
||||
* Caller must clean up on failure.
|
||||
*/
|
||||
static int policydb_index(struct policydb *p)
|
||||
{
|
||||
int i, rc;
|
||||
|
||||
if (p->mls_enabled)
|
||||
pr_debug("SELinux: %d users, %d roles, %d types, %d bools, %d sens, %d cats\n",
|
||||
p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim,
|
||||
p->p_bools.nprim, p->p_levels.nprim, p->p_cats.nprim);
|
||||
else
|
||||
pr_debug("SELinux: %d users, %d roles, %d types, %d bools\n",
|
||||
p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim,
|
||||
p->p_bools.nprim);
|
||||
|
||||
pr_debug("SELinux: %d classes, %d rules\n",
|
||||
p->p_classes.nprim, p->te_avtab.nel);
|
||||
|
||||
#ifdef DEBUG_HASHES
|
||||
avtab_hash_eval(&p->te_avtab, "rules");
|
||||
symtab_hash_eval(p->symtab);
|
||||
#endif
|
||||
|
||||
p->class_val_to_struct = kcalloc(p->p_classes.nprim,
|
||||
sizeof(*p->class_val_to_struct),
|
||||
GFP_KERNEL);
|
||||
if (!p->class_val_to_struct)
|
||||
return -ENOMEM;
|
||||
|
||||
p->role_val_to_struct = kcalloc(p->p_roles.nprim,
|
||||
sizeof(*p->role_val_to_struct),
|
||||
GFP_KERNEL);
|
||||
if (!p->role_val_to_struct)
|
||||
return -ENOMEM;
|
||||
|
||||
p->user_val_to_struct = kcalloc(p->p_users.nprim,
|
||||
sizeof(*p->user_val_to_struct),
|
||||
GFP_KERNEL);
|
||||
if (!p->user_val_to_struct)
|
||||
return -ENOMEM;
|
||||
|
||||
p->type_val_to_struct_array = kvcalloc(p->p_types.nprim,
|
||||
sizeof(*p->type_val_to_struct_array),
|
||||
GFP_KERNEL);
|
||||
if (!p->type_val_to_struct_array)
|
||||
return -ENOMEM;
|
||||
|
||||
rc = cond_init_bool_indexes(p);
|
||||
if (rc)
|
||||
goto out;
|
||||
|
||||
for (i = 0; i < SYM_NUM; i++) {
|
||||
p->sym_val_to_name[i] = kvcalloc(p->symtab[i].nprim,
|
||||
sizeof(char *),
|
||||
GFP_KERNEL);
|
||||
if (!p->sym_val_to_name[i])
|
||||
return -ENOMEM;
|
||||
|
||||
rc = hashtab_map(p->symtab[i].table, index_f[i], p);
|
||||
if (rc)
|
||||
goto out;
|
||||
}
|
||||
rc = 0;
|
||||
out:
|
||||
return rc;
|
||||
}
|
||||
|
||||
/*
|
||||
* The following *_destroy functions are used to
|
||||
* free any memory allocated for each kind of
|
||||
@@ -723,6 +332,7 @@ static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
|
||||
static int filenametr_destroy(void *key, void *datum, void *p)
|
||||
{
|
||||
struct filename_trans *ft = key;
|
||||
|
||||
kfree(ft->name);
|
||||
kfree(key);
|
||||
kfree(datum);
|
||||
@@ -733,6 +343,7 @@ static int filenametr_destroy(void *key, void *datum, void *p)
|
||||
static int range_tr_destroy(void *key, void *datum, void *p)
|
||||
{
|
||||
struct mls_range *rt = datum;
|
||||
|
||||
kfree(key);
|
||||
ebitmap_destroy(&rt->level[0].cat);
|
||||
ebitmap_destroy(&rt->level[1].cat);
|
||||
@@ -754,6 +365,397 @@ static void ocontext_destroy(struct ocontext *c, int i)
|
||||
kfree(c);
|
||||
}
|
||||
|
||||
/*
|
||||
* Initialize the role table.
|
||||
*/
|
||||
static int roles_init(struct policydb *p)
|
||||
{
|
||||
char *key = NULL;
|
||||
int rc;
|
||||
struct role_datum *role;
|
||||
|
||||
role = kzalloc(sizeof(*role), GFP_KERNEL);
|
||||
if (!role)
|
||||
return -ENOMEM;
|
||||
|
||||
rc = -EINVAL;
|
||||
role->value = ++p->p_roles.nprim;
|
||||
if (role->value != OBJECT_R_VAL)
|
||||
goto out;
|
||||
|
||||
rc = -ENOMEM;
|
||||
key = kstrdup(OBJECT_R, GFP_KERNEL);
|
||||
if (!key)
|
||||
goto out;
|
||||
|
||||
rc = hashtab_insert(p->p_roles.table, key, role);
|
||||
if (rc)
|
||||
goto out;
|
||||
|
||||
return 0;
|
||||
out:
|
||||
kfree(key);
|
||||
kfree(role);
|
||||
return rc;
|
||||
}
|
||||
|
||||
static u32 filenametr_hash(struct hashtab *h, const void *k)
|
||||
{
|
||||
const struct filename_trans *ft = k;
|
||||
unsigned long hash;
|
||||
unsigned int byte_num;
|
||||
unsigned char focus;
|
||||
|
||||
hash = ft->stype ^ ft->ttype ^ ft->tclass;
|
||||
|
||||
byte_num = 0;
|
||||
while ((focus = ft->name[byte_num++]))
|
||||
hash = partial_name_hash(focus, hash);
|
||||
return hash & (h->size - 1);
|
||||
}
|
||||
|
||||
static int filenametr_cmp(struct hashtab *h, const void *k1, const void *k2)
|
||||
{
|
||||
const struct filename_trans *ft1 = k1;
|
||||
const struct filename_trans *ft2 = k2;
|
||||
int v;
|
||||
|
||||
v = ft1->stype - ft2->stype;
|
||||
if (v)
|
||||
return v;
|
||||
|
||||
v = ft1->ttype - ft2->ttype;
|
||||
if (v)
|
||||
return v;
|
||||
|
||||
v = ft1->tclass - ft2->tclass;
|
||||
if (v)
|
||||
return v;
|
||||
|
||||
return strcmp(ft1->name, ft2->name);
|
||||
|
||||
}
|
||||
|
||||
static u32 rangetr_hash(struct hashtab *h, const void *k)
|
||||
{
|
||||
const struct range_trans *key = k;
|
||||
|
||||
return (key->source_type + (key->target_type << 3) +
|
||||
(key->target_class << 5)) & (h->size - 1);
|
||||
}
|
||||
|
||||
static int rangetr_cmp(struct hashtab *h, const void *k1, const void *k2)
|
||||
{
|
||||
const struct range_trans *key1 = k1, *key2 = k2;
|
||||
int v;
|
||||
|
||||
v = key1->source_type - key2->source_type;
|
||||
if (v)
|
||||
return v;
|
||||
|
||||
v = key1->target_type - key2->target_type;
|
||||
if (v)
|
||||
return v;
|
||||
|
||||
v = key1->target_class - key2->target_class;
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
/*
|
||||
* Initialize a policy database structure.
|
||||
*/
|
||||
static int policydb_init(struct policydb *p)
|
||||
{
|
||||
int i, rc;
|
||||
|
||||
memset(p, 0, sizeof(*p));
|
||||
|
||||
for (i = 0; i < SYM_NUM; i++) {
|
||||
rc = symtab_init(&p->symtab[i], symtab_sizes[i]);
|
||||
if (rc)
|
||||
goto out;
|
||||
}
|
||||
|
||||
rc = avtab_init(&p->te_avtab);
|
||||
if (rc)
|
||||
goto out;
|
||||
|
||||
rc = roles_init(p);
|
||||
if (rc)
|
||||
goto out;
|
||||
|
||||
rc = cond_policydb_init(p);
|
||||
if (rc)
|
||||
goto out;
|
||||
|
||||
p->filename_trans = hashtab_create(filenametr_hash, filenametr_cmp,
|
||||
(1 << 10));
|
||||
if (!p->filename_trans) {
|
||||
rc = -ENOMEM;
|
||||
goto out;
|
||||
}
|
||||
|
||||
p->range_tr = hashtab_create(rangetr_hash, rangetr_cmp, 256);
|
||||
if (!p->range_tr) {
|
||||
rc = -ENOMEM;
|
||||
goto out;
|
||||
}
|
||||
|
||||
ebitmap_init(&p->filename_trans_ttypes);
|
||||
ebitmap_init(&p->policycaps);
|
||||
ebitmap_init(&p->permissive_map);
|
||||
|
||||
return 0;
|
||||
out:
|
||||
hashtab_destroy(p->filename_trans);
|
||||
hashtab_destroy(p->range_tr);
|
||||
for (i = 0; i < SYM_NUM; i++) {
|
||||
hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
|
||||
hashtab_destroy(p->symtab[i].table);
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
/*
|
||||
* The following *_index functions are used to
|
||||
* define the val_to_name and val_to_struct arrays
|
||||
* in a policy database structure. The val_to_name
|
||||
* arrays are used when converting security context
|
||||
* structures into string representations. The
|
||||
* val_to_struct arrays are used when the attributes
|
||||
* of a class, role, or user are needed.
|
||||
*/
|
||||
|
||||
static int common_index(void *key, void *datum, void *datap)
|
||||
{
|
||||
struct policydb *p;
|
||||
struct common_datum *comdatum;
|
||||
|
||||
comdatum = datum;
|
||||
p = datap;
|
||||
if (!comdatum->value || comdatum->value > p->p_commons.nprim)
|
||||
return -EINVAL;
|
||||
|
||||
p->sym_val_to_name[SYM_COMMONS][comdatum->value - 1] = key;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int class_index(void *key, void *datum, void *datap)
|
||||
{
|
||||
struct policydb *p;
|
||||
struct class_datum *cladatum;
|
||||
|
||||
cladatum = datum;
|
||||
p = datap;
|
||||
if (!cladatum->value || cladatum->value > p->p_classes.nprim)
|
||||
return -EINVAL;
|
||||
|
||||
p->sym_val_to_name[SYM_CLASSES][cladatum->value - 1] = key;
|
||||
p->class_val_to_struct[cladatum->value - 1] = cladatum;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int role_index(void *key, void *datum, void *datap)
|
||||
{
|
||||
struct policydb *p;
|
||||
struct role_datum *role;
|
||||
|
||||
role = datum;
|
||||
p = datap;
|
||||
if (!role->value
|
||||
|| role->value > p->p_roles.nprim
|
||||
|| role->bounds > p->p_roles.nprim)
|
||||
return -EINVAL;
|
||||
|
||||
p->sym_val_to_name[SYM_ROLES][role->value - 1] = key;
|
||||
p->role_val_to_struct[role->value - 1] = role;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int type_index(void *key, void *datum, void *datap)
|
||||
{
|
||||
struct policydb *p;
|
||||
struct type_datum *typdatum;
|
||||
|
||||
typdatum = datum;
|
||||
p = datap;
|
||||
|
||||
if (typdatum->primary) {
|
||||
if (!typdatum->value
|
||||
|| typdatum->value > p->p_types.nprim
|
||||
|| typdatum->bounds > p->p_types.nprim)
|
||||
return -EINVAL;
|
||||
p->sym_val_to_name[SYM_TYPES][typdatum->value - 1] = key;
|
||||
p->type_val_to_struct[typdatum->value - 1] = typdatum;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int user_index(void *key, void *datum, void *datap)
|
||||
{
|
||||
struct policydb *p;
|
||||
struct user_datum *usrdatum;
|
||||
|
||||
usrdatum = datum;
|
||||
p = datap;
|
||||
if (!usrdatum->value
|
||||
|| usrdatum->value > p->p_users.nprim
|
||||
|| usrdatum->bounds > p->p_users.nprim)
|
||||
return -EINVAL;
|
||||
|
||||
p->sym_val_to_name[SYM_USERS][usrdatum->value - 1] = key;
|
||||
p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int sens_index(void *key, void *datum, void *datap)
|
||||
{
|
||||
struct policydb *p;
|
||||
struct level_datum *levdatum;
|
||||
|
||||
levdatum = datum;
|
||||
p = datap;
|
||||
|
||||
if (!levdatum->isalias) {
|
||||
if (!levdatum->level->sens ||
|
||||
levdatum->level->sens > p->p_levels.nprim)
|
||||
return -EINVAL;
|
||||
|
||||
p->sym_val_to_name[SYM_LEVELS][levdatum->level->sens - 1] = key;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int cat_index(void *key, void *datum, void *datap)
|
||||
{
|
||||
struct policydb *p;
|
||||
struct cat_datum *catdatum;
|
||||
|
||||
catdatum = datum;
|
||||
p = datap;
|
||||
|
||||
if (!catdatum->isalias) {
|
||||
if (!catdatum->value || catdatum->value > p->p_cats.nprim)
|
||||
return -EINVAL;
|
||||
|
||||
p->sym_val_to_name[SYM_CATS][catdatum->value - 1] = key;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
|
||||
{
|
||||
common_index,
|
||||
class_index,
|
||||
role_index,
|
||||
type_index,
|
||||
user_index,
|
||||
cond_index_bool,
|
||||
sens_index,
|
||||
cat_index,
|
||||
};
|
||||
|
||||
#ifdef DEBUG_HASHES
|
||||
static void hash_eval(struct hashtab *h, const char *hash_name)
|
||||
{
|
||||
struct hashtab_info info;
|
||||
|
||||
hashtab_stat(h, &info);
|
||||
pr_debug("SELinux: %s: %d entries and %d/%d buckets used, longest chain length %d\n",
|
||||
hash_name, h->nel, info.slots_used, h->size,
|
||||
info.max_chain_len);
|
||||
}
|
||||
|
||||
static void symtab_hash_eval(struct symtab *s)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < SYM_NUM; i++)
|
||||
hash_eval(s[i].table, symtab_name[i]);
|
||||
}
|
||||
|
||||
#else
|
||||
static inline void hash_eval(struct hashtab *h, char *hash_name)
|
||||
{
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Define the other val_to_name and val_to_struct arrays
|
||||
* in a policy database structure.
|
||||
*
|
||||
* Caller must clean up on failure.
|
||||
*/
|
||||
static int policydb_index(struct policydb *p)
|
||||
{
|
||||
int i, rc;
|
||||
|
||||
if (p->mls_enabled)
|
||||
pr_debug("SELinux: %d users, %d roles, %d types, %d bools, %d sens, %d cats\n",
|
||||
p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim,
|
||||
p->p_bools.nprim, p->p_levels.nprim, p->p_cats.nprim);
|
||||
else
|
||||
pr_debug("SELinux: %d users, %d roles, %d types, %d bools\n",
|
||||
p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim,
|
||||
p->p_bools.nprim);
|
||||
|
||||
pr_debug("SELinux: %d classes, %d rules\n",
|
||||
p->p_classes.nprim, p->te_avtab.nel);
|
||||
|
||||
#ifdef DEBUG_HASHES
|
||||
avtab_hash_eval(&p->te_avtab, "rules");
|
||||
symtab_hash_eval(p->symtab);
|
||||
#endif
|
||||
|
||||
p->class_val_to_struct = kcalloc(p->p_classes.nprim,
|
||||
sizeof(*p->class_val_to_struct),
|
||||
GFP_KERNEL);
|
||||
if (!p->class_val_to_struct)
|
||||
return -ENOMEM;
|
||||
|
||||
p->role_val_to_struct = kcalloc(p->p_roles.nprim,
|
||||
sizeof(*p->role_val_to_struct),
|
||||
GFP_KERNEL);
|
||||
if (!p->role_val_to_struct)
|
||||
return -ENOMEM;
|
||||
|
||||
p->user_val_to_struct = kcalloc(p->p_users.nprim,
|
||||
sizeof(*p->user_val_to_struct),
|
||||
GFP_KERNEL);
|
||||
if (!p->user_val_to_struct)
|
||||
return -ENOMEM;
|
||||
|
||||
p->type_val_to_struct = kvcalloc(p->p_types.nprim,
|
||||
sizeof(*p->type_val_to_struct),
|
||||
GFP_KERNEL);
|
||||
if (!p->type_val_to_struct)
|
||||
return -ENOMEM;
|
||||
|
||||
rc = cond_init_bool_indexes(p);
|
||||
if (rc)
|
||||
goto out;
|
||||
|
||||
for (i = 0; i < SYM_NUM; i++) {
|
||||
p->sym_val_to_name[i] = kvcalloc(p->symtab[i].nprim,
|
||||
sizeof(char *),
|
||||
GFP_KERNEL);
|
||||
if (!p->sym_val_to_name[i])
|
||||
return -ENOMEM;
|
||||
|
||||
rc = hashtab_map(p->symtab[i].table, index_f[i], p);
|
||||
if (rc)
|
||||
goto out;
|
||||
}
|
||||
rc = 0;
|
||||
out:
|
||||
return rc;
|
||||
}
|
||||
|
||||
/*
|
||||
* Free any memory allocated by a policy database structure.
|
||||
*/
|
||||
@@ -777,7 +779,7 @@ void policydb_destroy(struct policydb *p)
|
||||
kfree(p->class_val_to_struct);
|
||||
kfree(p->role_val_to_struct);
|
||||
kfree(p->user_val_to_struct);
|
||||
kvfree(p->type_val_to_struct_array);
|
||||
kvfree(p->type_val_to_struct);
|
||||
|
||||
avtab_destroy(&p->te_avtab);
|
||||
|
||||
@@ -1722,7 +1724,7 @@ static int type_bounds_sanity_check(void *key, void *datum, void *datap)
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
upper = p->type_val_to_struct_array[upper->bounds - 1];
|
||||
upper = p->type_val_to_struct[upper->bounds - 1];
|
||||
BUG_ON(!upper);
|
||||
|
||||
if (upper->attribute) {
|
||||
|
@@ -253,7 +253,7 @@ struct policydb {
|
||||
struct class_datum **class_val_to_struct;
|
||||
struct role_datum **role_val_to_struct;
|
||||
struct user_datum **user_val_to_struct;
|
||||
struct type_datum **type_val_to_struct_array;
|
||||
struct type_datum **type_val_to_struct;
|
||||
|
||||
/* type enforcement access vectors and transitions */
|
||||
struct avtab te_avtab;
|
||||
|
@@ -542,13 +542,13 @@ static void type_attribute_bounds_av(struct policydb *policydb,
|
||||
struct type_datum *target;
|
||||
u32 masked = 0;
|
||||
|
||||
source = policydb->type_val_to_struct_array[scontext->type - 1];
|
||||
source = policydb->type_val_to_struct[scontext->type - 1];
|
||||
BUG_ON(!source);
|
||||
|
||||
if (!source->bounds)
|
||||
return;
|
||||
|
||||
target = policydb->type_val_to_struct_array[tcontext->type - 1];
|
||||
target = policydb->type_val_to_struct[tcontext->type - 1];
|
||||
BUG_ON(!target);
|
||||
|
||||
memset(&lo_avd, 0, sizeof(lo_avd));
|
||||
@@ -891,7 +891,7 @@ int security_bounded_transition(struct selinux_state *state,
|
||||
|
||||
index = new_context->type;
|
||||
while (true) {
|
||||
type = policydb->type_val_to_struct_array[index - 1];
|
||||
type = policydb->type_val_to_struct[index - 1];
|
||||
BUG_ON(!type);
|
||||
|
||||
/* not bounded anymore */
|
||||
|
@@ -12,7 +12,7 @@
|
||||
#include <linux/slab.h>
|
||||
#include <linux/sched.h>
|
||||
#include <linux/spinlock.h>
|
||||
#include <linux/atomic.h>
|
||||
#include <asm/barrier.h>
|
||||
#include "flask.h"
|
||||
#include "security.h"
|
||||
#include "sidtab.h"
|
||||
@@ -23,14 +23,14 @@ int sidtab_init(struct sidtab *s)
|
||||
|
||||
memset(s->roots, 0, sizeof(s->roots));
|
||||
|
||||
/* max count is SIDTAB_MAX so valid index is always < SIDTAB_MAX */
|
||||
for (i = 0; i < SIDTAB_RCACHE_SIZE; i++)
|
||||
atomic_set(&s->rcache[i], -1);
|
||||
s->rcache[i] = SIDTAB_MAX;
|
||||
|
||||
for (i = 0; i < SECINITSID_NUM; i++)
|
||||
s->isids[i].set = 0;
|
||||
|
||||
atomic_set(&s->count, 0);
|
||||
|
||||
s->count = 0;
|
||||
s->convert = NULL;
|
||||
|
||||
spin_lock_init(&s->lock);
|
||||
@@ -130,14 +130,12 @@ static struct context *sidtab_do_lookup(struct sidtab *s, u32 index, int alloc)
|
||||
|
||||
static struct context *sidtab_lookup(struct sidtab *s, u32 index)
|
||||
{
|
||||
u32 count = (u32)atomic_read(&s->count);
|
||||
/* read entries only after reading count */
|
||||
u32 count = smp_load_acquire(&s->count);
|
||||
|
||||
if (index >= count)
|
||||
return NULL;
|
||||
|
||||
/* read entries after reading count */
|
||||
smp_rmb();
|
||||
|
||||
return sidtab_do_lookup(s, index, 0);
|
||||
}
|
||||
|
||||
@@ -210,10 +208,10 @@ static int sidtab_find_context(union sidtab_entry_inner entry,
|
||||
static void sidtab_rcache_update(struct sidtab *s, u32 index, u32 pos)
|
||||
{
|
||||
while (pos > 0) {
|
||||
atomic_set(&s->rcache[pos], atomic_read(&s->rcache[pos - 1]));
|
||||
WRITE_ONCE(s->rcache[pos], READ_ONCE(s->rcache[pos - 1]));
|
||||
--pos;
|
||||
}
|
||||
atomic_set(&s->rcache[0], (int)index);
|
||||
WRITE_ONCE(s->rcache[0], index);
|
||||
}
|
||||
|
||||
static void sidtab_rcache_push(struct sidtab *s, u32 index)
|
||||
@@ -227,14 +225,14 @@ static int sidtab_rcache_search(struct sidtab *s, struct context *context,
|
||||
u32 i;
|
||||
|
||||
for (i = 0; i < SIDTAB_RCACHE_SIZE; i++) {
|
||||
int v = atomic_read(&s->rcache[i]);
|
||||
u32 v = READ_ONCE(s->rcache[i]);
|
||||
|
||||
if (v < 0)
|
||||
if (v >= SIDTAB_MAX)
|
||||
continue;
|
||||
|
||||
if (context_cmp(sidtab_do_lookup(s, (u32)v, 0), context)) {
|
||||
sidtab_rcache_update(s, (u32)v, i);
|
||||
*index = (u32)v;
|
||||
if (context_cmp(sidtab_do_lookup(s, v, 0), context)) {
|
||||
sidtab_rcache_update(s, v, i);
|
||||
*index = v;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -245,8 +243,7 @@ static int sidtab_reverse_lookup(struct sidtab *s, struct context *context,
|
||||
u32 *index)
|
||||
{
|
||||
unsigned long flags;
|
||||
u32 count = (u32)atomic_read(&s->count);
|
||||
u32 count_locked, level, pos;
|
||||
u32 count, count_locked, level, pos;
|
||||
struct sidtab_convert_params *convert;
|
||||
struct context *dst, *dst_convert;
|
||||
int rc;
|
||||
@@ -255,11 +252,10 @@ static int sidtab_reverse_lookup(struct sidtab *s, struct context *context,
|
||||
if (rc == 0)
|
||||
return 0;
|
||||
|
||||
/* read entries only after reading count */
|
||||
count = smp_load_acquire(&s->count);
|
||||
level = sidtab_level_from_count(count);
|
||||
|
||||
/* read entries after reading count */
|
||||
smp_rmb();
|
||||
|
||||
pos = 0;
|
||||
rc = sidtab_find_context(s->roots[level], &pos, count, level,
|
||||
context, index);
|
||||
@@ -272,7 +268,7 @@ static int sidtab_reverse_lookup(struct sidtab *s, struct context *context,
|
||||
spin_lock_irqsave(&s->lock, flags);
|
||||
|
||||
convert = s->convert;
|
||||
count_locked = (u32)atomic_read(&s->count);
|
||||
count_locked = s->count;
|
||||
level = sidtab_level_from_count(count_locked);
|
||||
|
||||
/* if count has changed before we acquired the lock, then catch up */
|
||||
@@ -320,7 +316,7 @@ static int sidtab_reverse_lookup(struct sidtab *s, struct context *context,
|
||||
}
|
||||
|
||||
/* at this point we know the insert won't fail */
|
||||
atomic_set(&convert->target->count, count + 1);
|
||||
convert->target->count = count + 1;
|
||||
}
|
||||
|
||||
if (context->len)
|
||||
@@ -331,9 +327,7 @@ static int sidtab_reverse_lookup(struct sidtab *s, struct context *context,
|
||||
*index = count;
|
||||
|
||||
/* write entries before writing new count */
|
||||
smp_wmb();
|
||||
|
||||
atomic_set(&s->count, count + 1);
|
||||
smp_store_release(&s->count, count + 1);
|
||||
|
||||
rc = 0;
|
||||
out_unlock:
|
||||
@@ -423,7 +417,7 @@ int sidtab_convert(struct sidtab *s, struct sidtab_convert_params *params)
|
||||
return -EBUSY;
|
||||
}
|
||||
|
||||
count = (u32)atomic_read(&s->count);
|
||||
count = s->count;
|
||||
level = sidtab_level_from_count(count);
|
||||
|
||||
/* allocate last leaf in the new sidtab (to avoid race with
|
||||
@@ -436,7 +430,7 @@ int sidtab_convert(struct sidtab *s, struct sidtab_convert_params *params)
|
||||
}
|
||||
|
||||
/* set count in case no new entries are added during conversion */
|
||||
atomic_set(¶ms->target->count, count);
|
||||
params->target->count = count;
|
||||
|
||||
/* enable live convert of new entries */
|
||||
s->convert = params;
|
||||
|
@@ -40,8 +40,8 @@ union sidtab_entry_inner {
|
||||
#define SIDTAB_LEAF_ENTRIES \
|
||||
(SIDTAB_NODE_ALLOC_SIZE / sizeof(struct sidtab_entry_leaf))
|
||||
|
||||
#define SIDTAB_MAX_BITS 31 /* limited to INT_MAX due to atomic_t range */
|
||||
#define SIDTAB_MAX (((u32)1 << SIDTAB_MAX_BITS) - 1)
|
||||
#define SIDTAB_MAX_BITS 32
|
||||
#define SIDTAB_MAX U32_MAX
|
||||
/* ensure enough tree levels for SIDTAB_MAX entries */
|
||||
#define SIDTAB_MAX_LEVEL \
|
||||
DIV_ROUND_UP(SIDTAB_MAX_BITS - size_to_shift(SIDTAB_LEAF_ENTRIES), \
|
||||
@@ -69,13 +69,22 @@ struct sidtab_convert_params {
|
||||
#define SIDTAB_RCACHE_SIZE 3
|
||||
|
||||
struct sidtab {
|
||||
/*
|
||||
* lock-free read access only for as many items as a prior read of
|
||||
* 'count'
|
||||
*/
|
||||
union sidtab_entry_inner roots[SIDTAB_MAX_LEVEL + 1];
|
||||
atomic_t count;
|
||||
/*
|
||||
* access atomically via {READ|WRITE}_ONCE(); only increment under
|
||||
* spinlock
|
||||
*/
|
||||
u32 count;
|
||||
/* access only under spinlock */
|
||||
struct sidtab_convert_params *convert;
|
||||
spinlock_t lock;
|
||||
|
||||
/* reverse lookup cache */
|
||||
atomic_t rcache[SIDTAB_RCACHE_SIZE];
|
||||
/* reverse lookup cache - access atomically via {READ|WRITE}_ONCE() */
|
||||
u32 rcache[SIDTAB_RCACHE_SIZE];
|
||||
|
||||
/* index == SID - 1 (no entry for SECSID_NULL) */
|
||||
struct sidtab_isid_entry isids[SECINITSID_NUM];
|
||||
|
Reference in New Issue
Block a user