Skip to content

Commit fd96bc8

Browse files
committed
selinux: more strict bounds check
Validate the types used in bounds checks. Replace the usage of BUG(), to avoid halting the system on malformed polices. Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
1 parent 7c9e044 commit fd96bc8

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

security/selinux/ss/policydb.c

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,15 @@ bool policydb_class_isvalid(const struct policydb *p, u16 class)
10201020
return true;
10211021
}
10221022

1023+
bool policydb_user_isvalid(const struct policydb *p, u32 user)
1024+
{
1025+
if (!user || user > p->p_roles.nprim)
1026+
return false;
1027+
if (!p->sym_val_to_name[SYM_USERS][user - 1])
1028+
return false;
1029+
return true;
1030+
}
1031+
10231032
bool policydb_role_isvalid(const struct policydb *p, u32 role)
10241033
{
10251034
if (!role || role > p->p_roles.nprim)
@@ -1942,6 +1951,12 @@ static int user_bounds_sanity_check(void *key, void *datum, void *datap)
19421951
return -EINVAL;
19431952
}
19441953

1954+
if (!policydb_user_isvalid(p, upper->bounds)) {
1955+
pr_err("SELinux: user %s: invalid boundary id %d\n",
1956+
(char *) key, upper->bounds);
1957+
return -EINVAL;
1958+
}
1959+
19451960
upper = p->user_val_to_struct[upper->bounds - 1];
19461961
ebitmap_for_each_positive_bit(&user->roles, node, bit)
19471962
{
@@ -1979,6 +1994,12 @@ static int role_bounds_sanity_check(void *key, void *datum, void *datap)
19791994
return -EINVAL;
19801995
}
19811996

1997+
if (!policydb_role_isvalid(p, upper->bounds)) {
1998+
pr_err("SELinux: role %s: invalid boundary id %d\n",
1999+
(char *) key, upper->bounds);
2000+
return -EINVAL;
2001+
}
2002+
19822003
upper = p->role_val_to_struct[upper->bounds - 1];
19832004
ebitmap_for_each_positive_bit(&role->types, node, bit)
19842005
{
@@ -2013,9 +2034,13 @@ static int type_bounds_sanity_check(void *key, void *datum, void *datap)
20132034
return -EINVAL;
20142035
}
20152036

2016-
upper = p->type_val_to_struct[upper->bounds - 1];
2017-
BUG_ON(!upper);
2037+
if (!policydb_type_isvalid(p, upper->bounds)) {
2038+
pr_err("SELinux: type %s: invalid boundary id %d\n",
2039+
(char *) key, upper->bounds);
2040+
return -EINVAL;
2041+
}
20182042

2043+
upper = p->type_val_to_struct[upper->bounds - 1];
20192044
if (upper->attribute) {
20202045
pr_err("SELinux: type %s: "
20212046
"bounded by attribute %s\n",

security/selinux/ss/policydb.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ extern bool policydb_context_isvalid(const struct policydb *p, const struct cont
324324
extern bool policydb_class_isvalid(const struct policydb *p, u16 class);
325325
extern bool policydb_type_isvalid(const struct policydb *p, u32 type);
326326
extern bool policydb_role_isvalid(const struct policydb *p, u32 role);
327+
extern bool policydb_user_isvalid(const struct policydb *p, u32 user);
327328
extern bool policydb_boolean_isvalid(const struct policydb *p, u32 boolean);
328329
extern int policydb_read(struct policydb *p, struct policy_file *fp);
329330
extern int policydb_write(struct policydb *p, struct policy_file *fp);

security/selinux/ss/services.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,9 @@ static void context_struct_compute_av(struct policydb *policydb,
711711
* If the given source and target types have boundary
712712
* constraint, lazy checks have to mask any violated
713713
* permission and notice it to userspace via audit.
714+
*
715+
* Infinite recursion is avoided via a depth pre-check in
716+
* type_bounds_sanity_check().
714717
*/
715718
type_attribute_bounds_av(policydb, scontext, tcontext,
716719
tclass, avd);

0 commit comments

Comments
 (0)