Fuzzing secilc with AFL
cover Fuzzing secilc with AFL
Hello,
Last week I started fuzzing secilc with american fuzzy lop, mainly because I was curious of the result. I started with a small CIL policy, for which AFL quickly found one crashing mutation (fixed by commit c303ca910add ("libsepol/cil: Check for too many permissions in classes and commons")).
Then I unleashed AFL on the files in secilc and it found 19 unique crashing inputs over the week-end. I started digging through these inputs to uncover bugs and this patchset consists in the patches I wrote and quickly tested tonight.
If anyone is interested in running AFL too, I published my setup (along with my custom Makefile and some other patches) on
https://github.com/fishilico/selinux (branch 'master'). I wrote run_afl.sh (in the root of this project) as a wrapper to afl-fuzz to make fuzzing start with "./run_afl.sh secilc".
Cheers,
Nicolas
PS: I am quite busy in the next weeks so I may be quite slow to reply to feedbacks and to send other patches.
Nicolas Iooss (3):
libsepol/cil: make cil_resolve_name() fail for '.' libsepol/cil: fix double-free in cil categories parser libsepol/cil: fix memory leak in __cil_fill_expr()
libsepol/cil/src/cil_build_ast.c | 2 ++ libsepol/cil/src/cil_resolve_ast.c | 8 +++++++- 2 files changed, 9 insertions(+), 1 deletion(-)
Signature · 1 line
2.10.0
PATCH 1/3 libsepol/cil: make cil_resolve_name() fail for '.'
This CIL policy makes secilc crash with a NULL pointer dereference:
(class CLASS (PERM))
(classorder (CLASS))
(sid SID)
(sidorder (SID))
(user USER)
(role ROLE)
(type TYPE)
(category CAT)
(categoryorder (CAT))
(sensitivity SENS)
(sensitivityorder (SENS)) (sensitivitycategory SENS (CAT))
(allow TYPE self (CLASS (PERM))) (roletype ROLE TYPE)
(userrole USER ROLE)
(userlevel USER (SENS))
(userrange USER ((SENS)(SENS (CAT)))) (sidcontext SID (USER ROLE TYPE ((SENS)(SENS))))
(allow . self (CLASS (PERM)))
Using "." in the allow statement makes strtok_r() return NULL in cil_resolve_name() and this result is then used in a call to cil_symtab_get_datum(), which is thus invalid.
Instead of crashing, make secilc fail with an error message.
This bug has been found by fuzzing secilc with american fuzzy lop.
--- libsepol/cil/src/cil_resolve_ast.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
libsepol/cil/src/cil_resolve_ast.c +7 -1
@@ -4027,7 +4027,13 @@ int cil_resolve_name(struct cil_tree_node *ast_node, char *name, enum cil_sym_inchar *current =strtok_r (name_dup,"." , &sp);char *next =strtok_r (NULL ,"." , &sp);symtab_t *symtab =NULL ; - + +if (current ==NULL ) { +/* Only dots */ +cil_tree_log (ast_node,CIL_ERR ,"Invalid name %s" , name); + goto exit; + } + node = ast_node;if (*name =='.' ) {/* Leading '.' */
Signature · 1 line
2.10.0
Re: [PATCH 1/3] libsepol/cil: make cil_resolve_name() fail for '.'
Quoted 41 lines · Nicolas Iooss
This CIL policy makes secilc crash with a NULL pointer dereference:
(class CLASS (PERM))
(classorder (CLASS))
(sid SID)
(sidorder (SID))
(user USER)
(role ROLE)
(type TYPE)
(category CAT)
(categoryorder (CAT))
(sensitivity SENS)
(sensitivityorder (SENS)) (sensitivitycategory SENS (CAT))
(allow TYPE self (CLASS (PERM))) (roletype ROLE TYPE)
(userrole USER ROLE)
(userlevel USER (SENS))
(userrange USER ((SENS)(SENS (CAT)))) (sidcontext SID (USER ROLE TYPE ((SENS)(SENS))))(allow . self (CLASS (PERM)))
Using "." in the allow statement makes
strtok_r()return NULL incil_resolve_name()and this result is then used in a call tocil_symtab_get_datum(), which is thus invalid.Instead of crashing, make secilc fail with an error message.
This bug has been found by fuzzing secilc with american fuzzy lop.
Signed-off-by Nicolas Iooss <[email protected]>--- libsepol/cil/src/cil_resolve_ast.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)libsepol/cil/src/cil_resolve_ast.c +0
-0
libsepol/cil/src/cil_resolve_ast.c int cil_resolve_name(struct cil_tree_node *ast_node, char *name, enum cil_sym_in · line 4033
Quoted 8 lines
libsepol/cil/src/cil_resolve_ast.c +4
-1@@ -4027,7 +4027,13 @@ int cil_resolve_name(struct cil_tree_node *ast_node, char *name, enum cil_sym_inchar *current =strtok_r (name_dup,"." , &sp);char *next =strtok_r (NULL ,"." , &sp);symtab_t *symtab =NULL ; - + +if (current ==NULL ) { +/* Only dots */ +cil_tree_log (ast_node,CIL_ERR ,"Invalid name %s" , name);
I added "free(name_dup);" here when I applied the patch.
Jim
Quoted 7 lines
libsepol/cil/src/cil_resolve_ast.c +3
-0+ goto exit; + } + node = ast_node;if (*name =='.' ) {/* Leading '.' */
Signature · 2 lines
James Carter <[email protected]> National Security Agency
PATCH 2/3 libsepol/cil: fix double-free in cil categories parser
In reply to Nicolas IoossWhen cil_fill_cats() fails to parse an expression and destroys a category set, it fails to reset *cats to NULL. This makes this object be destroyed again in cil_destroy_catset().
This bug can be triggered by the following policy:
(class CLASS (PERM))
(classorder (CLASS))
(sid SID)
(sidorder (SID))
(user USER)
(role ROLE)
(type TYPE)
(category CAT)
(categoryorder (CAT))
(sensitivity SENS)
(sensitivityorder (SENS)) (sensitivitycategory SENS (CAT))
(allow TYPE self (CLASS (PERM))) (roletype ROLE TYPE)
(userrole USER ROLE)
(userlevel USER (SENS))
(userrange USER ((SENS)(SENS (CAT)))) (sidcontext SID (USER ROLE TYPE ((SENS)(SENS))))
(categoryset cats (range unknown))
This bug has been found by fuzzing secilc with american fuzzy lop.
--- libsepol/cil/src/cil_build_ast.c | 1 + 1 file changed, 1 insertion(+)
libsepol/cil/src/cil_build_ast.c +1 -0
@@ -5481,6 +5481,7 @@ int cil_fill_cats(struct cil_tree_node *curr, struct cil_cats **cats) rc =cil_gen_expr (curr,CIL_CAT , &(*cats)->str_expr);if (rc !=SEPOL_OK ) {cil_destroy_cats (*cats); + *cats =NULL ; }return rc;
Signature · 1 line
2.10.0
PATCH 3/3 libsepol/cil: fix memory leak in __cil_fill_expr()
In reply to Nicolas Iooss__cil_fill_expr() initializes 'cil_list *sub_expr' but does not destroy it when __cil_fill_expr_helper() fails. This list is therefore leaked when __cil_fill_expr() returns.
This occurs when secilc compiles the following policy:
(class CLASS (PERM))
(classorder (CLASS))
(sid SID)
(sidorder (SID))
(user USER)
(role ROLE)
(type TYPE)
(category CAT)
(categoryorder (CAT))
(sensitivity SENS)
(sensitivityorder (SENS)) (sensitivitycategory SENS (CAT))
(allow TYPE self (CLASS (PERM))) (roletype ROLE TYPE)
(userrole USER ROLE)
(userlevel USER (SENS))
(userrange USER ((SENS)(SENS (CAT)))) (sidcontext SID (USER ROLE TYPE ((SENS)(SENS))))
(categoryset cats (not (range unknown)))
This bug has been found using gcc address sanitizer.
--- libsepol/cil/src/cil_build_ast.c | 1 + 1 file changed, 1 insertion(+)
libsepol/cil/src/cil_build_ast.c +1 -0
@@ -2562,6 +2562,7 @@ static int __cil_fill_expr(struct cil_tree_node *current, enum cil_flavor flavorcil_list_init (&sub_expr, flavor); rc =__cil_fill_expr_helper (current->cl_head, flavor, sub_expr, depth);if (rc !=SEPOL_OK ) { +cil_list_destroy (&sub_expr,CIL_TRUE ); goto exit; }cil_list_append (expr,CIL_LIST , sub_expr);
Signature · 1 line
2.10.0
Quoted 19 lines · Nicolas Iooss
Hello,
Last week I started fuzzing secilc with american fuzzy lop, mainly because I was curious of the result. I started with a small CIL policy, for which AFL quickly found one crashing mutation (fixed by commit
c303ca910add("libsepol/cil: Check for too many permissions in classes and commons")).Then I unleashed AFL on the files in secilc and it found 19 unique crashing inputs over the week-end. I started digging through these inputs to uncover bugs and this patchset consists in the patches I wrote and quickly tested tonight.
If anyone is interested in running AFL too, I published my setup (along with my custom
Makefileand some other patches) on
https://github.com/fishilico/selinux (branch 'master'). I wroterun_afl.sh(in the root of this project) as a wrapper to afl-fuzz to make fuzzing start with "./run_afl.sh secilc".
Quoted 5 lines
Cheers,
NicolasPS: I am quite busy in the next weeks so I may be quite slow to reply to feedbacks and to send other patches.
Applied these with the one addition that I noted.
Thanks,
Jim
Quoted 10 lines
Nicolas Iooss (3):
libsepol/cil: makecil_resolve_name()fail for '.' libsepol/cil: fix double-free in cil categories parser libsepol/cil: fix memory leak in__cil_fill_expr()libsepol/cil/src/cil_build_ast.c | 2 ++ libsepol/cil/src/cil_resolve_ast.c | 8 +++++++- 2 files changed, 9 insertions(+), 1 deletion(-)
Signature · 2 lines
James Carter <[email protected]> National Security Agency