Skip to contentSkip to conversation

libsepol: validate default type of transition is not an attribute

PATCH Superseded Christian Göttsche 2 participants 6 messages Nov 1, 2023 – Nov 2, 2023 SELinux security module

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 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.

---
 libsepol/src/policydb_validate.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libsepol/src/policydb_validate.c +1 -1
index 892a0ffd..d214bf09 100644
@@ -1105,7 +1105,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, &flavors[SYM_TYPES]))
 			goto bad;
 	}
 
Signature · 1 line
2.42.0
#1 lore raw

PATCH 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 d214bf09..c6a66fb3 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 v1 2/3
#2 lore raw

Re: [PATCH 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 d214bf09..c6a66fb3 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 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 c6a66fb3..0e4805ef 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:
@@ -1199,6 +1203,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 v1 3/3
#4 lore raw

Re: [PATCH 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 c6a66fb3..0e4805ef 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:
@@ -1199,6 +1203,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

#5 lore raw
In reply to Christian Göttsche
Quoted 14 lines · Christian Göttsche

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

---
 libsepol/src/policydb_validate.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libsepol/src/policydb_validate.c +0 -0
index 892a0ffd..d214bf09 100644

libsepol/src/policydb_validate.c static int validate_filename_trans(hashtab_key_t k, hashtab_datum_t d, void *arg · line 1108

Quoted 5 lines
libsepol/src/policydb_validate.c +1 -1
@@ -1105,7 +1105,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, &flavors[SYM_TYPES]))

You are missing an argument. validate_simpletype() takes three arguments. The 2nd one should be the policydb.
Making the policydb available here is going to take a bit of work.

Jim

Quoted 6 lines
libsepol/src/policydb_validate.c +0 -1
                        goto bad;
        }
 
--

2.42.0