Skip to contentSkip to conversation

libsepol: do not decode out-of-bound rolebounds

PATCH In review Nicolas Iooss 2 participants 2 messages Jan 20, 2021 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 libsepol: do not decode out-of-bound rolebounds

While fuzzing /usr/libexec/hll/pp, a policy module was generated with a role->bounds larger that the number of roles in the policy.

This issue has been found while fuzzing hll/pp with the American Fuzzy Lop.

This patch was suggested by James Carter <[email protected]> in
https://lore.kernel.org/selinux/CAP+JOzQc3yXczhk5JfUrr+6rFe3AXis+yJAukCFbz=aQotriQQ@mail.gmail.com/T/#mdd4bed0972c7c6f339e28270f73e9b7b09bb359f

---
 libsepol/src/policydb.c | 4 ++++
 1 file changed, 4 insertions(+)
libsepol/src/policydb.c +4 -0
index f5809315cc08..08c4cb18efcf 100644
@@ -1030,6 +1030,8 @@ static int role_index(hashtab_key_t key, hashtab_datum_t datum, void *datap)
 		return -EINVAL;
 	if (p->p_role_val_to_name[role->s.value - 1] != NULL)
 		return -EINVAL;
+	if (role->bounds > p->p_roles.nprim)
+		return -EINVAL;
 	p->p_role_val_to_name[role->s.value - 1] = (char *)key;
 	p->role_val_to_struct[role->s.value - 1] = role;
 
@@ -1049,6 +1051,8 @@ static int type_index(hashtab_key_t key, hashtab_datum_t datum, void *datap)
 			return -EINVAL;
 		if (p->p_type_val_to_name[typdatum->s.value - 1] != NULL)
 			return -EINVAL;
+		if (typdatum->bounds > p->p_types.nprim)
+			return -EINVAL;
 		p->p_type_val_to_name[typdatum->s.value - 1] = (char *)key;
 		p->type_val_to_struct[typdatum->s.value - 1] = typdatum;
 	}
Signature · 1 line
2.30.0
#1 lore raw
Quoted 29 lines · Nicolas Iooss

While fuzzing /usr/libexec/hll/pp, a policy module was generated with a role->bounds larger that the number of roles in the policy.

This issue has been found while fuzzing hll/pp with the American Fuzzy Lop.

This patch was suggested by James Carter <[email protected]> in
https://lore.kernel.org/selinux/CAP+JOzQc3yXczhk5JfUrr+6rFe3AXis+yJAukCFbz=aQotriQQ@mail.gmail.com/T/#mdd4bed0972c7c6f339e28270f73e9b7b09bb359f

---
 libsepol/src/policydb.c | 4 ++++
 1 file changed, 4 insertions(+)
libsepol/src/policydb.c +2 -0
index f5809315cc08..08c4cb18efcf 100644
@@ -1030,6 +1030,8 @@ static int role_index(hashtab_key_t key, hashtab_datum_t datum, void *datap)
                return -EINVAL;
        if (p->p_role_val_to_name[role->s.value - 1] != NULL)
                return -EINVAL;
+       if (role->bounds > p->p_roles.nprim)
+               return -EINVAL;
        p->p_role_val_to_name[role->s.value - 1] = (char *)key;
        p->role_val_to_struct[role->s.value - 1] = role;
 

libsepol/src/policydb.c static int type_index(hashtab_key_t key, hashtab_datum_t datum, void *datap) · line 1055

Quoted 5 lines
libsepol/src/policydb.c +2 -0
@@ -1049,6 +1051,8 @@ static int type_index(hashtab_key_t key, hashtab_datum_t datum, void *datap)
                        return -EINVAL;
                if (p->p_type_val_to_name[typdatum->s.value - 1] != NULL)
                        return -EINVAL;
+               if (typdatum->bounds > p->p_types.nprim)
+                       return -EINVAL;

This is very tricky, but for both of these the bounds value cannot be 0 or >= nprim because the value -1 is used as an index.

It has taken me longer than I thought it would, but I am almost ready to send out a patch that should check everything in a policydb_t after it is read in. Maybe by the end of the day, but definitely before the end of the week.

Thanks,
Jim

Quoted 6 lines
libsepol/src/policydb.c +0 -1
                p->p_type_val_to_name[typdatum->s.value - 1] = (char *)key;
                p->type_val_to_struct[typdatum->s.value - 1] = typdatum;
        }
--

2.30.0