Skip to contentSkip to conversation

selinux: tighten type and boolean validation

PATCH In review Stephen Smalley 3 participants 3 messages Apr 30 SELinux security module

lore
Share & tools
lore

Stephen Smalley

author maintainer
#0 lore raw

PATCH 1/1 selinux: tighten type and boolean validation

Tighten type and boolean validation during policy load to improve robustness in the face of ill-formed policies.

Some or all of this might be redundant with the patches previously posted in the series linked below:
https://lore.kernel.org/all/[email protected]/

---
 security/selinux/ss/conditional.c |  7 +++++--
 security/selinux/ss/policydb.c    | 23 +++++++++++++++++++----
 2 files changed, 24 insertions(+), 6 deletions(-)
security/selinux/ss/conditional.c +5 -2
index 64f1bbb8caa0..342153ecdcb3 100644
@@ -357,12 +357,15 @@ static int cond_read_av_list(struct policydb *p, struct policy_file *fp,
 
 static int expr_node_isvalid(struct policydb *p, struct cond_expr_node *expr)
 {
-	if (expr->expr_type <= 0 || expr->expr_type > COND_LAST) {
+	if (!expr->expr_type || expr->expr_type > COND_LAST) {
 		pr_err("SELinux: conditional expressions uses unknown operator.\n");
 		return 0;
 	}
 
-	if (expr->boolean > p->p_bools.nprim) {
+	if (expr->expr_type != COND_BOOL)
+		return 1;
+
+	if (!expr->boolean || expr->boolean > p->p_bools.nprim) {
 		pr_err("SELinux: conditional expressions uses unknown bool.\n");
 		return 0;
 	}
security/selinux/ss/policydb.c +19 -4
index 738fd47f33e6..33ed75b92fa4 100644
@@ -603,10 +603,15 @@ static int type_index(void *key, void *datum, void *datap)
 	typdatum = datum;
 	p = datap;
 
+	if (!typdatum->value || typdatum->value > p->p_types.nprim ||
+		typdatum->bounds > p->p_types.nprim) {
+		pr_err("SELinux: %s had value %u bounds %u nprim %u\n",
+			(char *)key, typdatum->value, typdatum->bounds,
+			p->p_types.nprim);
+		return -EINVAL;
+	}
+
 	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;
 	}
@@ -1736,6 +1741,12 @@ static int role_bounds_sanity_check(void *key, void *datum, void *datap)
 		}
 
 		upper = p->role_val_to_struct[upper->bounds - 1];
+		if (!upper) {
+			pr_err("SELinux: role %s: bounds wasn't defined\n",
+			       (char *)key);
+			return -EINVAL;
+		}
+
 		ebitmap_for_each_positive_bit(&role->types, node, bit)
 		{
 			if (ebitmap_get_bit(&upper->types, bit))
@@ -1770,7 +1781,11 @@ static int type_bounds_sanity_check(void *key, void *datum, void *datap)
 		}
 
 		upper = p->type_val_to_struct[upper->bounds - 1];
-		BUG_ON(!upper);
+		if (!upper) {
+			pr_err("SELinux: type %s: bounds wasn't defined\n",
+				(char *)key);
+			return -EINVAL;
+		}
 
 		if (upper->attribute) {
 			pr_err("SELinux: type %s: "
Signature · 1 line
2.53.0
#1 lore raw
Quoted 18 lines · Stephen Smalley

Tighten type and boolean validation during policy load to improve robustness in the face of ill-formed policies.

Some or all of this might be redundant with the patches previously posted in the series linked below:
https://lore.kernel.org/all/[email protected]/

---
 security/selinux/ss/conditional.c |  7 +++++--
 security/selinux/ss/policydb.c    | 23 +++++++++++++++++++----
 2 files changed, 24 insertions(+), 6 deletions(-)
security/selinux/ss/conditional.c +0 -0
index 64f1bbb8caa0..342153ecdcb3 100644

security/selinux/ss/conditional.c static int cond_read_av_list(struct policydb *p, struct policy_file *fp, · line 360

Quoted 5 lines
security/selinux/ss/conditional.c +1 -1
@@ -357,12 +357,15 @@ static int cond_read_av_list(struct policydb *p, struct policy_file *fp,
 
 static int expr_node_isvalid(struct policydb *p, struct cond_expr_node *expr)
 {
-	if (expr->expr_type <= 0 || expr->expr_type > COND_LAST) {
+	if (!expr->expr_type || expr->expr_type > COND_LAST) {

Is this correct? Previous check included the '< 0' case but your new test omits it.

br,
KK

maintainer
#2 lore raw
Quoted 20 lines · Kalevi Kolttonen
Quoted 19 lines · Stephen Smalley

Tighten type and boolean validation during policy load to improve robustness in the face of ill-formed policies.

Some or all of this might be redundant with the patches previously posted in the series linked below:
https://lore.kernel.org/all/[email protected]/

---
 security/selinux/ss/conditional.c |  7 +++++--
 security/selinux/ss/policydb.c    | 23 +++++++++++++++++++----
 2 files changed, 24 insertions(+), 6 deletions(-)
security/selinux/ss/conditional.c +0 -0
index 64f1bbb8caa0..342153ecdcb3 100644
@@ -357,12 +357,15 @@ static int cond_read_av_list(struct policydb *p, struct policy_file *fp,
 
Quoted 7 lines

security/selinux/ss/conditional.c static int cond_read_av_list(struct policydb *p, struct policy_file *fp, · line 360

Quoted 4 lines
security/selinux/ss/conditional.c +1 -1
 static int expr_node_isvalid(struct policydb *p, struct cond_expr_node *expr)
 {
-     if (expr->expr_type <= 0 || expr->expr_type > COND_LAST) {
+     if (!expr->expr_type || expr->expr_type > COND_LAST) {

Is this correct? Previous check included the '< 0' case but your new test omits it.

That shouldn't be an issue because the expr_type field in cond_expr_node is a u32, it will never be negative.

Signature · 1 line
paul-moore.com