Skip to content

Commit c3f0124

Browse files
committed
libsepol: Validate conditional expressions
When validating a policydb, validate the conditional expressions including the values of the booleans within them. Found by oss-fuzz (#45523) Signed-off-by: James Carter <[email protected]>
1 parent dfc652f commit c3f0124

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

libsepol/src/policydb_validate.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,9 +881,52 @@ static int validate_bool_id_array(sepol_handle_t *handle, uint32_t bool_ids[], u
881881
return -1;
882882
}
883883

884+
static int validate_cond_expr(sepol_handle_t *handle, struct cond_expr *expr, validate_t *bool)
885+
{
886+
int depth = -1;
887+
888+
for (; expr; expr = expr->next) {
889+
switch(expr->expr_type) {
890+
case COND_BOOL:
891+
if (validate_value(expr->bool, bool))
892+
goto bad;
893+
if (depth == (COND_EXPR_MAXDEPTH - 1))
894+
goto bad;
895+
depth++;
896+
break;
897+
case COND_NOT:
898+
if (depth < 0)
899+
goto bad;
900+
break;
901+
case COND_OR:
902+
case COND_AND:
903+
case COND_XOR:
904+
case COND_EQ:
905+
case COND_NEQ:
906+
if (depth < 1)
907+
goto bad;
908+
depth--;
909+
break;
910+
default:
911+
goto bad;
912+
}
913+
}
914+
915+
if (depth != 0)
916+
goto bad;
917+
918+
return 0;
919+
920+
bad:
921+
ERR(handle, "Invalid cond expression");
922+
return -1;
923+
}
924+
884925
static int validate_cond_list(sepol_handle_t *handle, cond_list_t *cond, validate_t flavors[])
885926
{
886927
for (; cond; cond = cond->next) {
928+
if (validate_cond_expr(handle, cond->expr, &flavors[SYM_BOOLS]))
929+
goto bad;
887930
if (validate_cond_av_list(handle, cond->true_list, flavors))
888931
goto bad;
889932
if (validate_cond_av_list(handle, cond->false_list, flavors))

0 commit comments

Comments
 (0)