Fix problems with CIL's handling of anonymous call arguments
cover Fix problems with CIL's handling of anonymous call arguments
For more information on anonymous call arguments, see the third patch.
The first two patches fix a couple of bugs in the handling of anonymous call arguments.
The last patch adds a test policy that can be used to test the handling of anonymouse call arguments.
James Carter (3):
libsepol/cil: Fix anonymous IP address call arguments libsepol/cil: Account for anonymous category sets in an expression secilc/test: Add test for anonymous args
libsepol/cil/src/cil_build_ast.c | 4 -- libsepol/cil/src/cil_resolve_ast.c | 47 +++++++------ secilc/test/anonymous_arg_test.cil | 106 +++++++++++++++++++++++++++++ 3 files changed, 133 insertions(+), 24 deletions(-) create mode 100644 secilc/test/anonymous_arg_test.cil
Signature · 1 line
2.26.3
PATCH 1/3 libsepol/cil: Fix anonymous IP address call arguments
A named IP address (using an ipaddr rule) could be passed as an argument, but trying to pass an actual IP address caused an error.
As an exmample, consider the following portion of a policy.
(macro m4 ((ipaddr ip)(ipaddr nm))
(nodecon ip nm (USER ROLE TYPE ((s0) (s0))))
)
(ipaddr nm1 255.255.255.0)
(ipaddr ip1 1.2.3.4)
(call m4 (ip1 nm1)) ; This works
(call m4 (1.2.3.4 255.255.255.0)) ; This doesn't
Allow actual IP addresses to be passed as a call argument. Now the second call works as well.
--- libsepol/cil/src/cil_build_ast.c | 4 ---- libsepol/cil/src/cil_resolve_ast.c | 23 ++++++++++------------- 2 files changed, 10 insertions(+), 17 deletions(-)
libsepol/cil/src/cil_build_ast.c +0 -4
@@ -5642,10 +5642,6 @@ int cil_fill_ipaddr(struct cil_tree_node *addr_node, struct cil_ipaddr *addr) goto exit; } -if (addr_node->cl_head !=NULL || addr_node->next !=NULL ) { - goto exit; - } -if (strchr (addr_node->data,'.' ) !=NULL ) { addr->family =AF_INET ; }else {
libsepol/cil/src/cil_resolve_ast.c +10 -13
@@ -3024,14 +3024,18 @@ static int cil_build_call_args(struct cil_tree_node *call_node, struct cil_callbreak ; }case CIL_IPADDR : { -if (arg_node->cl_head !=NULL ) { +if (arg_node->data ==NULL ) { +cil_tree_log (call_node,CIL_ERR ,"Invalid macro parameter" ); +cil_destroy_args (arg); + rc =SEPOL_ERR ; + goto exit; + }else if (strchr (arg_node->data,'.' ) ||strchr (arg_node->data,':' )) {struct cil_ipaddr *ipaddr =NULL ;struct cil_tree_node *addr_node =NULL ;cil_ipaddr_init (&ipaddr); - - rc =cil_fill_ipaddr (arg_node->cl_head, ipaddr); + rc =cil_fill_ipaddr (arg_node, ipaddr);if (rc !=SEPOL_OK ) { -cil_log (CIL_ERR ,"Failed to create anonymous ip address, rc: %d\n" , rc); +cil_tree_log (call_node,CIL_ERR ,"Failed to create anonymous ip address" );cil_destroy_ipaddr (ipaddr);cil_destroy_args (arg); goto exit;
@@ -3039,18 +3043,11 @@ static int cil_build_call_args(struct cil_tree_node *call_node, struct cil_callcil_tree_node_init (&addr_node); addr_node->flavor =CIL_IPADDR ; addr_node->data = ipaddr; -cil_list_append (((struct cil_symtab_datum *)ipaddr)->nodes, -CIL_LIST_ITEM , addr_node); - arg->arg = (struct cil_symtab_datum *)ipaddr; - }else if (arg_node->data ==NULL ) { -cil_tree_log (call_node,CIL_ERR ,"Invalid macro parameter" ); -cil_destroy_args (arg); - rc =SEPOL_ERR ; - goto exit; +cil_list_append (DATUM (ipaddr)->nodes,CIL_LIST_ITEM , addr_node); + arg->arg =DATUM (ipaddr); }else { arg->arg_str = arg_node->data; } -break ; }case CIL_CLASS :
Signature · 1 line
2.26.3
PATCH 2/3 libsepol/cil: Account for anonymous category sets in an expression
In reply to James CarterIt is possible for anonymous category sets to be in a category expression if the expression has a macro parameter in it. Unfortunately, anonymous category sets are not looked for when resolving category expressions and a segfault will occur during later processing if there was one.
As an example, consider the following portion of a policy.
(macro m1 ((categoryset cs))
(userlevel USER (s0 (cs)))
)
(call m1 ((c0 c1)))
This policy will cause a segault, because the categoryset datum for the parameter cs is not seen as a categoryset and is treated as a plain category.
When resolving an expression, check whether or not the datum that is found is actually an anonymous category set associated with a macro parameter. If it is, then resolve the category set if it has not already been resolved and treat its categories as a sub expression.
--- libsepol/cil/src/cil_resolve_ast.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-)
libsepol/cil/src/cil_resolve_ast.c +17 -7
@@ -3346,6 +3346,7 @@ int cil_resolve_expr(enum cil_flavor expr_type, struct cil_list *str_expr, strucstruct cil_list_item *curr;struct cil_symtab_datum *res_datum =NULL ;enum cil_sym_index sym_index =CIL_SYM_UNKNOWN ; +struct cil_list *datum_sub_expr;switch (str_expr->flavor) {case CIL_BOOL :
@@ -3379,18 +3380,26 @@ int cil_resolve_expr(enum cil_flavor expr_type, struct cil_list *str_expr, strucif (rc !=SEPOL_OK ) { goto exit; } - -if (sym_index ==CIL_SYM_TYPES && (expr_type ==CIL_CONSTRAIN || expr_type ==CIL_VALIDATETRANS )) { -cil_type_used (res_datum,CIL_ATTR_CONSTRAINT ); +if (sym_index ==CIL_SYM_CATS &&NODE (res_datum)->flavor ==CIL_CATSET ) { +struct cil_catset *catset = (struct cil_catset *)res_datum; +if (!catset->cats->datum_expr) { + rc =cil_resolve_expr (expr_type, catset->cats->str_expr, &catset->cats->datum_expr, parent, extra_args); +if (rc !=SEPOL_OK ) { + goto exit; + } + } +cil_copy_list (catset->cats->datum_expr, &datum_sub_expr); +cil_list_append (*datum_expr,CIL_LIST , datum_sub_expr); + }else { +if (sym_index ==CIL_SYM_TYPES && (expr_type ==CIL_CONSTRAIN || expr_type ==CIL_VALIDATETRANS )) { +cil_type_used (res_datum,CIL_ATTR_CONSTRAINT ); + } +cil_list_append (*datum_expr,CIL_DATUM , res_datum); } - -cil_list_append (*datum_expr,CIL_DATUM , res_datum);break ;case CIL_LIST : { -struct cil_list *datum_sub_expr; rc =cil_resolve_expr (expr_type, curr->data, &datum_sub_expr, parent, extra_args);if (rc !=SEPOL_OK ) { -cil_list_destroy (&datum_sub_expr,CIL_TRUE ); goto exit; }cil_list_append (*datum_expr,CIL_LIST , datum_sub_expr);
@@ -3404,6 +3413,7 @@ int cil_resolve_expr(enum cil_flavor expr_type, struct cil_list *str_expr, strucreturn SEPOL_OK ; exit: +cil_list_destroy (datum_expr,CIL_FALSE );return rc; }
Signature · 1 line
2.26.3
PATCH 3/3 secilc/test: Add test for anonymous args
In reply to James CarterCIL has rules that allow names to be assigned to certain objects like MLS category sets, MLS levels, MLS ranges, IP addresses, and class permission sets. These objects can also be named as parameters for a macro. A call may pass in a name for one of these objects, but it also may pass in one of the actual objects. These objects are referred as anonymous arguments.
Add CIL policy that can be used to test whether or not anonymous arguments are being handled properly in macros. Also test the equivalent named arguments to help determine if the problem is with that argument type or just with an anonymous argument of that type.
The anonymouse arguments that are tested are categoryset, level, levelrange, ipaddr, and classpermission.
--- secilc/test/anonymous_arg_test.cil | 106 +++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 secilc/test/anonymous_arg_test.cil
secilc/test/anonymous_arg_test.cil new file +106 -0
@@ -0,0 +1,106 @@ +;; Test anonymous args + +(mls true) +(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)))) + +(category c0) +(category c1) +(category c2) +(category c3) +(categoryorder (CAT c0 c1 c2 c3)) +(categoryset cs01 (c0 c1)) +(categoryset cs03 (range c0 c3)) + +(sensitivity s0) +(sensitivity s1) +(sensitivity s2) +(sensitivity s3) +(sensitivityorder (SENS s0 s1 s2 s3)) + +(sensitivitycategory s0 (cs01 c2 c3)) +(sensitivitycategory s1 (c0 c1 c2 c3)) +(sensitivitycategory s2 (c0 c1 c2 c3)) +(sensitivitycategory s3 (range c0 c3)) + +(level lvl (s0 (c0))) +(level lvl0 (s0)) +(level lvl3 (s3 (range c0 c3))) + +(levelrange rng ((s0) (s3 (c0 c1 c2 c3)))) + +(user u1) +(user u2) +(user u3) +(user u4) + +(userrole u1 ROLE) +(userrole u2 ROLE) +(userrole u3 ROLE) +(userrole u4 ROLE) + +; Test categoryset +(macro m1 ((user u)(sensitivity s)(categoryset cs)) + (userlevel u (s (cs))) +) +(call m1 (u1 s1 (c0 c1))) +(call m1 (u2 s2 cs01)) + +; Test level +(macro m2 ((user u)(level l)) + (userlevel u l) +) +(call m2 (u3 (s3 (c2)))) +(call m2 (u4 lvl)) + +; Test levelrange +(macro m3 ((user u)(levelrange lr)) + (userrange u lr) +) +(call m3 (u1 ((s0) (s3 (range c0 c3))))) +(call m3 (u2 (lvl0 (s3 (cs03))))) +(call m3 (u3 (lvl0 lvl3))) +(call m3 (u4 rng)) + +; Test ipaddr +(macro m4 ((user u)(ipaddr nm)(ipaddr ip)) + (nodecon ip nm (u ROLE TYPE ((s0) (s0)))) +) +(ipaddr nm1 255.255.255.0) +(ipaddr ip4 1.2.3.4) +(call m4 (u1 nm1 192.25.35.200)) +(call m4 (u2 255.255.255.0 ip4)) + +; Test classpermission +(type t1) +(type t2) +(type t3) + +(classpermission cp1) +(classpermissionset cp1 (CLASS (PERM))) + +(classmap cm1 (cm1p)) +(classmapping cm1 cm1p (CLASS (PERM))) + +(macro m5 ((type t)(classpermission cp)) + (allow t self cp) +) +(call m5 (t1 (CLASS (PERM)))) +(call m5 (t2 cp1)) +(call m5 (t3 (cm1 (cm1p))))
Signature · 1 line
2.26.3
Quoted 14 lines · James Carter
For more information on anonymous call arguments, see the third patch.
The first two patches fix a couple of bugs in the handling of anonymous call arguments.
The last patch adds a test policy that can be used to test the handling of anonymouse call arguments.
James Carter (3):
libsepol/cil: Fix anonymous IP address call arguments libsepol/cil: Account for anonymous category sets in an expression secilc/test: Add test for anonymous args
Quoted 8 lines
libsepol/cil/src/cil_build_ast.c | 4 -- libsepol/cil/src/cil_resolve_ast.c | 47 +++++++------ secilc/test/anonymous_arg_test.cil | 106 +++++++++++++++++++++++++++++ 3 files changed, 133 insertions(+), 24 deletions(-) create mode 100644 secilc/test/anonymous_arg_test.cil--
2.26.3
For these 3 patches:
Thanks!
Nicolas
Quoted 22 lines · Nicolas Iooss
Quoted 20 lines · James Carter
For more information on anonymous call arguments, see the third patch.
The first two patches fix a couple of bugs in the handling of anonymous call arguments.
The last patch adds a test policy that can be used to test the handling of anonymouse call arguments.
James Carter (3):
libsepol/cil: Fix anonymous IP address call arguments libsepol/cil: Account for anonymous category sets in an expression secilc/test: Add test for anonymous argslibsepol/cil/src/cil_build_ast.c | 4 -- libsepol/cil/src/cil_resolve_ast.c | 47 +++++++------ secilc/test/anonymous_arg_test.cil | 106 +++++++++++++++++++++++++++++ 3 files changed, 133 insertions(+), 24 deletions(-) create mode 100644 secilc/test/anonymous_arg_test.cil
Quoted 7 lines
Quoted 2 lines
--
2.26.3For these 3 patches:
Acked-by Nicolas Iooss <[email protected]>
This series has been merged.
Jim
Quoted 3 lines
Thanks!
Nicolas