Skip to contentSkip to conversation

libsepol: validate default type of transition is not an attribute

PATCH In review Christian Göttsche 2 participants 7 messages Nov 3, 2023 – Nov 7, 2023 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/3 libsepol: validate default type of transition is not an attribute

The default type of a type transition must be a regular type, not an attribute.

---
v2:
fix build error by supplying necessary arguments to validate_simpletype()

---
 libsepol/src/policydb_validate.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)
libsepol/src/policydb_validate.c +10 -6
index 892a0ffd..810c3263 100644
@@ -468,7 +468,7 @@ static int validate_role_datum_wrapper(__attribute__((unused)) hashtab_key_t k,
 	return validate_role_datum(margs->handle, d, margs->flavors);
 }
 
-static int validate_simpletype(uint32_t value, const policydb_t *p, validate_t flavors[])
+static int validate_simpletype(uint32_t value, const policydb_t *p, const validate_t flavors[SYM_NUM])
 {
 	const type_datum_t *type;
 
@@ -1094,7 +1094,9 @@ static int validate_filename_trans(hashtab_key_t k, hashtab_datum_t d, void *arg
 {
 	const filename_trans_key_t *ftk = (filename_trans_key_t *)k;
 	const filename_trans_datum_t *ftd = d;
-	validate_t *flavors = (validate_t *)args;
+	const map_arg_t *margs = args;
+	const validate_t *flavors = margs->flavors;
+	const policydb_t *p = margs->policy;
 
 	if (validate_value(ftk->ttype, &flavors[SYM_TYPES]))
 		goto bad;
@@ -1105,7 +1107,7 @@ static int validate_filename_trans(hashtab_key_t k, hashtab_datum_t d, void *arg
 	for (; ftd; ftd = ftd->next) {
 		if (validate_ebitmap(&ftd->stypes, &flavors[SYM_TYPES]))
 			goto bad;
-		if (validate_value(ftd->otype, &flavors[SYM_TYPES]))
+		if (validate_simpletype(ftd->otype, p, flavors))
 			goto bad;
 	}
 
@@ -1115,9 +1117,11 @@ bad:
 	return -1;
 }
 
-static int validate_filename_trans_hashtab(sepol_handle_t *handle, hashtab_t filename_trans, validate_t flavors[])
+static int validate_filename_trans_hashtab(sepol_handle_t *handle, const policydb_t *p, validate_t flavors[])
 {
-	if (hashtab_map(filename_trans, validate_filename_trans, flavors)) {
+	map_arg_t margs = { flavors, handle, p };
+
+	if (hashtab_map(p->filename_trans, validate_filename_trans, &margs)) {
 		ERR(handle, "Invalid filename trans");
 		return -1;
 	}
@@ -1555,7 +1559,7 @@ int policydb_validate(sepol_handle_t *handle, const policydb_t *p)
 		if (validate_role_allows(handle, p->role_allow, flavors))
 			goto bad;
 		if (p->policyvers >= POLICYDB_VERSION_FILENAME_TRANS)
-			if (validate_filename_trans_hashtab(handle, p->filename_trans, flavors))
+			if (validate_filename_trans_hashtab(handle, p, flavors))
 				goto bad;
 	} else {
 		if (validate_avrule_blocks(handle, p->global, p, flavors))
Signature · 1 line
2.42.0
#1 lore raw

PATCH v2 2/3 libsepol: validate constraint depth

Ensure constraint expressions are complete and do not exceed the supported depth limit.

---
 libsepol/src/policydb_validate.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
libsepol/src/policydb_validate.c +20 -0
index 810c3263..b20ed579 100644
@@ -228,6 +228,7 @@ bad:
 static int validate_constraint_nodes(sepol_handle_t *handle, unsigned int nperms, const constraint_node_t *cons, validate_t flavors[])
 {
 	const constraint_expr_t *cexp;
+	int depth;
 
 	for (; cons; cons = cons->next) {
 		if (nperms == 0 && cons->permissions != 0)
@@ -240,8 +241,14 @@ static int validate_constraint_nodes(sepol_handle_t *handle, unsigned int nperms
 		if (!cons->expr)
 			goto bad;
 
+		depth = -1;
+
 		for (cexp = cons->expr; cexp; cexp = cexp->next) {
 			if (cexp->expr_type == CEXPR_NAMES) {
+				if (depth >= (CEXPR_MAXDEPTH - 1))
+					goto bad;
+				depth++;
+
 				if (cexp->attr & CEXPR_XTARGET && nperms != 0)
 					goto bad;
 				if (!(cexp->attr & CEXPR_TYPE)) {
@@ -282,6 +289,10 @@ static int validate_constraint_nodes(sepol_handle_t *handle, unsigned int nperms
 					goto bad;
 				}
 			} else if (cexp->expr_type == CEXPR_ATTR) {
+				if (depth >= (CEXPR_MAXDEPTH - 1))
+					goto bad;
+				depth++;
+
 				if (!ebitmap_is_empty(&cexp->names))
 					goto bad;
 				if (validate_empty_type_set(cexp->type_names))
@@ -318,8 +329,14 @@ static int validate_constraint_nodes(sepol_handle_t *handle, unsigned int nperms
 			} else {
 				switch (cexp->expr_type) {
 				case CEXPR_NOT:
+					if (depth < 0)
+						goto bad;
+					break;
 				case CEXPR_AND:
 				case CEXPR_OR:
+					if (depth < 1)
+						goto bad;
+					depth--;
 					break;
 				default:
 					goto bad;
@@ -335,6 +352,9 @@ static int validate_constraint_nodes(sepol_handle_t *handle, unsigned int nperms
 					goto bad;
 			}
 		}
+
+		if (depth != 0)
+			goto bad;
 	}
 
 	return 0;
Signature · 1 line
2.42.0
Acked-by on v2 2/3
#2 lore raw

Re: [PATCH v2 2/3] libsepol: validate constraint depth

Quoted 5 lines · Christian Göttsche

Ensure constraint expressions are complete and do not exceed the supported depth limit.

Quoted 70 lines
---
 libsepol/src/policydb_validate.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
libsepol/src/policydb_validate.c +20 -1
index 810c3263..b20ed579 100644
@@ -228,6 +228,7 @@ bad:
 static int validate_constraint_nodes(sepol_handle_t *handle, unsigned int nperms, const constraint_node_t *cons, validate_t flavors[])
 {
        const constraint_expr_t *cexp;
+       int depth;
 
        for (; cons; cons = cons->next) {
                if (nperms == 0 && cons->permissions != 0)
@@ -240,8 +241,14 @@ static int validate_constraint_nodes(sepol_handle_t *handle, unsigned int nperms
                if (!cons->expr)
                        goto bad;
 
+               depth = -1;
+
                for (cexp = cons->expr; cexp; cexp = cexp->next) {
                        if (cexp->expr_type == CEXPR_NAMES) {
+                               if (depth >= (CEXPR_MAXDEPTH - 1))
+                                       goto bad;
+                               depth++;
+
                                if (cexp->attr & CEXPR_XTARGET && nperms != 0)
                                        goto bad;
                                if (!(cexp->attr & CEXPR_TYPE)) {
@@ -282,6 +289,10 @@ static int validate_constraint_nodes(sepol_handle_t *handle, unsigned int nperms
                                        goto bad;
                                }
                        } else if (cexp->expr_type == CEXPR_ATTR) {
+                               if (depth >= (CEXPR_MAXDEPTH - 1))
+                                       goto bad;
+                               depth++;
+
                                if (!ebitmap_is_empty(&cexp->names))
                                        goto bad;
                                if (validate_empty_type_set(cexp->type_names))
@@ -318,8 +329,14 @@ static int validate_constraint_nodes(sepol_handle_t *handle, unsigned int nperms
                        } else {
                                switch (cexp->expr_type) {
                                case CEXPR_NOT:
+                                       if (depth < 0)
+                                               goto bad;
+                                       break;
                                case CEXPR_AND:
                                case CEXPR_OR:
+                                       if (depth < 1)
+                                               goto bad;
+                                       depth--;
                                        break;
                                default:
                                        goto bad;
@@ -335,6 +352,9 @@ static int validate_constraint_nodes(sepol_handle_t *handle, unsigned int nperms
                                        goto bad;
                        }
                }
+
+               if (depth != 0)
+                       goto bad;
        }
 
        return 0;
--

2.42.0

#3 lore raw

PATCH v2 3/3 libsepol: more strict validation

In reply to Christian Göttsche

Ensure the ibendport port is not 0 (similar to the kernel).

More general depth test for boolean expressions.

Ensure the boolean id is not set for logic operators.

---
 libsepol/src/policydb_validate.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
libsepol/src/policydb_validate.c +7 -1
index b20ed579..d04f6bcd 100644
@@ -1002,13 +1002,15 @@ static int validate_cond_expr(sepol_handle_t *handle, const struct cond_expr *ex
 		case COND_BOOL:
 			if (validate_value(expr->boolean, boolean))
 				goto bad;
-			if (depth == (COND_EXPR_MAXDEPTH - 1))
+			if (depth >= (COND_EXPR_MAXDEPTH - 1))
 				goto bad;
 			depth++;
 			break;
 		case COND_NOT:
 			if (depth < 0)
 				goto bad;
+			if (expr->boolean != 0)
+				goto bad;
 			break;
 		case COND_OR:
 		case COND_AND:
@@ -1017,6 +1019,8 @@ static int validate_cond_expr(sepol_handle_t *handle, const struct cond_expr *ex
 		case COND_NEQ:
 			if (depth < 1)
 				goto bad;
+			if (expr->boolean != 0)
+				goto bad;
 			depth--;
 			break;
 		default:
@@ -1203,6 +1207,8 @@ static int validate_ocontexts(sepol_handle_t *handle, const policydb_t *p, valid
 						goto bad;
 					break;
 				case OCON_IBENDPORT:
+					if (octx->u.ibendport.port == 0)
+						goto bad;
 					if (!octx->u.ibendport.dev_name)
 						goto bad;
 					break;
Signature · 1 line
2.42.0
Acked-by on v2 3/3
#4 lore raw

Re: [PATCH v2 3/3] libsepol: more strict validation

Quoted 8 lines · Christian Göttsche

Ensure the ibendport port is not 0 (similar to the kernel).

More general depth test for boolean expressions.

Ensure the boolean id is not set for logic operators.

Quoted 46 lines
---
 libsepol/src/policydb_validate.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
libsepol/src/policydb_validate.c +7 -2
index b20ed579..d04f6bcd 100644
@@ -1002,13 +1002,15 @@ static int validate_cond_expr(sepol_handle_t *handle, const struct cond_expr *ex
                case COND_BOOL:
                        if (validate_value(expr->boolean, boolean))
                                goto bad;
-                       if (depth == (COND_EXPR_MAXDEPTH - 1))
+                       if (depth >= (COND_EXPR_MAXDEPTH - 1))
                                goto bad;
                        depth++;
                        break;
                case COND_NOT:
                        if (depth < 0)
                                goto bad;
+                       if (expr->boolean != 0)
+                               goto bad;
                        break;
                case COND_OR:
                case COND_AND:
@@ -1017,6 +1019,8 @@ static int validate_cond_expr(sepol_handle_t *handle, const struct cond_expr *ex
                case COND_NEQ:
                        if (depth < 1)
                                goto bad;
+                       if (expr->boolean != 0)
+                               goto bad;
                        depth--;
                        break;
                default:
@@ -1203,6 +1207,8 @@ static int validate_ocontexts(sepol_handle_t *handle, const policydb_t *p, valid
                                                goto bad;
                                        break;
                                case OCON_IBENDPORT:
+                                       if (octx->u.ibendport.port == 0)
+                                               goto bad;
                                        if (!octx->u.ibendport.dev_name)
                                                goto bad;
                                        break;
--

2.42.0

Acked-by on v2 1/3
#5 lore raw
In reply to Christian Göttsche
Quoted 5 lines · Christian Göttsche

The default type of a type transition must be a regular type, not an attribute.

Quoted 67 lines

---
v2:
fix build error by supplying necessary arguments to validate_simpletype()

---
 libsepol/src/policydb_validate.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)
libsepol/src/policydb_validate.c +10 -7
index 892a0ffd..810c3263 100644
@@ -468,7 +468,7 @@ static int validate_role_datum_wrapper(__attribute__((unused)) hashtab_key_t k,
        return validate_role_datum(margs->handle, d, margs->flavors);
 }
 
-static int validate_simpletype(uint32_t value, const policydb_t *p, validate_t flavors[])
+static int validate_simpletype(uint32_t value, const policydb_t *p, const validate_t flavors[SYM_NUM])
 {
        const type_datum_t *type;
 
@@ -1094,7 +1094,9 @@ static int validate_filename_trans(hashtab_key_t k, hashtab_datum_t d, void *arg
 {
        const filename_trans_key_t *ftk = (filename_trans_key_t *)k;
        const filename_trans_datum_t *ftd = d;
-       validate_t *flavors = (validate_t *)args;
+       const map_arg_t *margs = args;
+       const validate_t *flavors = margs->flavors;
+       const policydb_t *p = margs->policy;
 
        if (validate_value(ftk->ttype, &flavors[SYM_TYPES]))
                goto bad;
@@ -1105,7 +1107,7 @@ static int validate_filename_trans(hashtab_key_t k, hashtab_datum_t d, void *arg
        for (; ftd; ftd = ftd->next) {
                if (validate_ebitmap(&ftd->stypes, &flavors[SYM_TYPES]))
                        goto bad;
-               if (validate_value(ftd->otype, &flavors[SYM_TYPES]))
+               if (validate_simpletype(ftd->otype, p, flavors))
                        goto bad;
        }
 
@@ -1115,9 +1117,11 @@ bad:
        return -1;
 }
 
-static int validate_filename_trans_hashtab(sepol_handle_t *handle, hashtab_t filename_trans, validate_t flavors[])
+static int validate_filename_trans_hashtab(sepol_handle_t *handle, const policydb_t *p, validate_t flavors[])
 {
-       if (hashtab_map(filename_trans, validate_filename_trans, flavors)) {
+       map_arg_t margs = { flavors, handle, p };
+
+       if (hashtab_map(p->filename_trans, validate_filename_trans, &margs)) {
                ERR(handle, "Invalid filename trans");
                return -1;
        }
@@ -1555,7 +1559,7 @@ int policydb_validate(sepol_handle_t *handle, const policydb_t *p)
                if (validate_role_allows(handle, p->role_allow, flavors))
                        goto bad;
                if (p->policyvers >= POLICYDB_VERSION_FILENAME_TRANS)
-                       if (validate_filename_trans_hashtab(handle, p->filename_trans, flavors))
+                       if (validate_filename_trans_hashtab(handle, p, flavors))
                                goto bad;
        } else {
                if (validate_avrule_blocks(handle, p->global, p, flavors))
--

2.42.0

#6 lore raw
Quoted 11 lines · James Carter
Quoted 5 lines · Christian Göttsche

The default type of a type transition must be a regular type, not an attribute.

These three patches have been merged.
Thanks,
Jim

Quoted 67 lines
Quoted 67 lines

---
v2:
fix build error by supplying necessary arguments to validate_simpletype()

---
 libsepol/src/policydb_validate.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)
libsepol/src/policydb_validate.c +10 -7
index 892a0ffd..810c3263 100644
@@ -468,7 +468,7 @@ static int validate_role_datum_wrapper(__attribute__((unused)) hashtab_key_t k,
        return validate_role_datum(margs->handle, d, margs->flavors);
 }
 
-static int validate_simpletype(uint32_t value, const policydb_t *p, validate_t flavors[])
+static int validate_simpletype(uint32_t value, const policydb_t *p, const validate_t flavors[SYM_NUM])
 {
        const type_datum_t *type;
 
@@ -1094,7 +1094,9 @@ static int validate_filename_trans(hashtab_key_t k, hashtab_datum_t d, void *arg
 {
        const filename_trans_key_t *ftk = (filename_trans_key_t *)k;
        const filename_trans_datum_t *ftd = d;
-       validate_t *flavors = (validate_t *)args;
+       const map_arg_t *margs = args;
+       const validate_t *flavors = margs->flavors;
+       const policydb_t *p = margs->policy;
 
        if (validate_value(ftk->ttype, &flavors[SYM_TYPES]))
                goto bad;
@@ -1105,7 +1107,7 @@ static int validate_filename_trans(hashtab_key_t k, hashtab_datum_t d, void *arg
        for (; ftd; ftd = ftd->next) {
                if (validate_ebitmap(&ftd->stypes, &flavors[SYM_TYPES]))
                        goto bad;
-               if (validate_value(ftd->otype, &flavors[SYM_TYPES]))
+               if (validate_simpletype(ftd->otype, p, flavors))
                        goto bad;
        }
 
@@ -1115,9 +1117,11 @@ bad:
        return -1;
 }
 
-static int validate_filename_trans_hashtab(sepol_handle_t *handle, hashtab_t filename_trans, validate_t flavors[])
+static int validate_filename_trans_hashtab(sepol_handle_t *handle, const policydb_t *p, validate_t flavors[])
 {
-       if (hashtab_map(filename_trans, validate_filename_trans, flavors)) {
+       map_arg_t margs = { flavors, handle, p };
+
+       if (hashtab_map(p->filename_trans, validate_filename_trans, &margs)) {
                ERR(handle, "Invalid filename trans");
                return -1;
        }
@@ -1555,7 +1559,7 @@ int policydb_validate(sepol_handle_t *handle, const policydb_t *p)
                if (validate_role_allows(handle, p->role_allow, flavors))
                        goto bad;
                if (p->policyvers >= POLICYDB_VERSION_FILENAME_TRANS)
-                       if (validate_filename_trans_hashtab(handle, p->filename_trans, flavors))
+                       if (validate_filename_trans_hashtab(handle, p, flavors))
                                goto bad;
        } else {
                if (validate_avrule_blocks(handle, p->global, p, flavors))
--

2.42.0