libsepol: In module_to_cil skip empty conditional blocks
PATCH 1/3 libsepol: In module_to_cil skip empty conditional blocks
When converting a module to CIL, skip conditional blocks that do not have either a true block or a false block.
--- libsepol/src/module_to_cil.c | 2 ++ 1 file changed, 2 insertions(+)
libsepol/src/module_to_cil.c +2 -0
@@ -1426,6 +1426,8 @@ static int cond_list_to_cil(int indent, struct policydb *pdb, struct cond_node *struct cond_node *cond;for (cond = cond_list; cond !=NULL ; cond = cond->next) { +if (!cond->avtrue_list && !cond->avfalse_list) +continue ; rc =cond_expr_to_cil (indent, pdb, cond->expr, cond->flags);if (rc !=0 ) {
Signature · 1 line
2.54.0
PATCH 2/3 libsepol: In module_to_cil correctly choose tunableif or booleanif
The flag COND_NODE_FLAGS_TUNABLE in struct cond_node is calculated and used only during module expansion, so it should not be used to determine if a conditional block is a tunableif or a booleanif when converting a module to CIL.
Instead, set a flag when a tunable is found in a conditional expression so the conditional block can be marked later as a tunableif.
--- libsepol/src/module_to_cil.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
libsepol/src/module_to_cil.c +7 -3
@@ -1280,10 +1280,12 @@ exit:return rc; } -static int cond_expr_to_cil (int indent,struct policydb *pdb,struct cond_expr *cond_expr,uint32_t flags) +static int cond_expr_to_cil (int indent,struct policydb *pdb,struct cond_expr *cond_expr) {int rc =0 ;struct cond_expr *curr; +struct cond_bool_datum *dat1; +uint32_t is_tunable =0 ;struct stack *stack =NULL ;int len =0 ;int rlen;
@@ -1303,6 +1305,8 @@ static int cond_expr_to_cil(int indent, struct policydb *pdb, struct cond_expr *for (curr = cond_expr; curr !=NULL ; curr = curr->next) {if (curr->expr_type ==COND_BOOL ) { + dat1 = pdb->bool_val_to_struct[curr->boolean -1 ]; + is_tunable |= dat1->flags; val1 = pdb->p_bool_val_to_name[curr->boolean -1 ];// length of boolean + 2 parens + null terminator len =strlen (val1) +2 +1 ;
@@ -1388,7 +1392,7 @@ static int cond_expr_to_cil(int indent, struct policydb *pdb, struct cond_expr * new_val =NULL ; } -if (flags &COND_NODE_FLAGS_TUNABLE ) { +if (is_tunable) { type ="tunableif" ; }else { type ="booleanif" ;
@@ -1429,7 +1433,7 @@ static int cond_list_to_cil(int indent, struct policydb *pdb, struct cond_node *if (!cond->avtrue_list && !cond->avfalse_list)continue ; - rc =cond_expr_to_cil (indent, pdb, cond->expr, cond->flags); + rc =cond_expr_to_cil (indent, pdb, cond->expr);if (rc !=0 ) { goto exit; }
Signature · 1 line
2.54.0
Re: [PATCH 2/3] libsepol: In module_to_cil correctly choose tunableif or booleanif
Quoted 11 lines · James Carter
The flag
COND_NODE_FLAGS_TUNABLEin structcond_nodeis calculated and used only during module expansion, so it should not be used to determine if a conditional block is a tunableif or a booleanif when converting a module to CIL.Instead, set a flag when a tunable is found in a conditional expression so the conditional block can be marked later as a tunableif.
Signed-off-by James Carter <[email protected]>
Would be good to add a test for this if we don't already have one.
Quoted 53 lines
--- libsepol/src/module_to_cil.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)libsepol/src/module_to_cil.c +7
-4@@ -1280,10 +1280,12 @@ exit:return rc; } -static int cond_expr_to_cil (int indent,struct policydb *pdb,struct cond_expr *cond_expr,uint32_t flags) +static int cond_expr_to_cil (int indent,struct policydb *pdb,struct cond_expr *cond_expr) {int rc =0 ;struct cond_expr *curr; +struct cond_bool_datum *dat1; +uint32_t is_tunable =0 ;struct stack *stack =NULL ;int len =0 ;int rlen;@@ -1303,6 +1305,8 @@ static int cond_expr_to_cil(int indent, struct policydb *pdb, struct cond_expr *for (curr = cond_expr; curr !=NULL ; curr = curr->next) {if (curr->expr_type ==COND_BOOL ) { + dat1 = pdb->bool_val_to_struct[curr->boolean -1 ]; + is_tunable |= dat1->flags; val1 = pdb->p_bool_val_to_name[curr->boolean -1 ];// length of boolean + 2 parens + null terminator len =strlen (val1) +2 +1 ;@@ -1388,7 +1392,7 @@ static int cond_expr_to_cil(int indent, struct policydb *pdb, struct cond_expr * new_val =NULL ; } -if (flags &COND_NODE_FLAGS_TUNABLE ) { +if (is_tunable) { type ="tunableif" ; }else { type ="booleanif" ;@@ -1429,7 +1433,7 @@ static int cond_list_to_cil(int indent, struct policydb *pdb, struct cond_node *if (!cond->avtrue_list && !cond->avfalse_list)continue ; - rc =cond_expr_to_cil (indent, pdb, cond->expr, cond->flags); + rc =cond_expr_to_cil (indent, pdb, cond->expr);if (rc !=0 ) { goto exit; } --2.54.0
PATCH 3/3 libsepol: In module_to_cil use constrain and validatetrans where possible
In reply to James CarterWhen converting a module to CIL for an MLS policy module_to_cil was always using mlsconstrain and mlsvalidatetrans.
Instead only use mlsconstrain and mlsvalidatetrans when the constraint expression contains l1, l2, h1, or h2.
This follows the example of kernel_to_cil.
It also avoids an error that would occur if a user is referred to in a constrain that then becomes an mlsconstrain. "u1 == user_u" is allowed for a constrain, but the error "unknown user user_u" will occur for an mlsconstrain.
--- libsepol/src/module_to_cil.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-)
libsepol/src/module_to_cil.c +19 -5
@@ -1742,7 +1742,7 @@ exit: } -static int constraint_expr_to_string (struct policydb *pdb,struct constraint_expr *exprs,char **expr_string) +static int constraint_expr_to_string (struct policydb *pdb,struct constraint_expr *exprs,char **expr_string,int *use_mls) {int rc =-1 ;struct constraint_expr *expr;
@@ -1762,6 +1762,8 @@ static int constraint_expr_to_string(struct policydb *pdb, struct constraint_expunsigned int num_names =0 ;struct type_set *ts; + *use_mls =0 ; + rc =stack_init (&stack);if (rc !=0 ) { goto exit;
@@ -1803,6 +1805,10 @@ static int constraint_expr_to_string(struct policydb *pdb, struct constraint_exp goto exit; } +if (expr->attr >=CEXPR_L1L2 ) { + *use_mls =1 ; + } +if (expr->expr_type ==CEXPR_ATTR ) {// length of values/attrs + 2 separating spaces + 2 parens + null terminator len =strlen (op) +strlen (attr1) +strlen (attr2) +2 +2 +1 ;
@@ -1979,18 +1985,26 @@ static int constraints_to_cil(int indent, struct policydb *pdb, char *classkey,int rc =-1 ;struct constraint_node *node;char *expr =NULL ; -const char *mls; +int use_mls; +const char *mls ="" ;char *perms; - mls = pdb->mls ?"mls" :"" ; -for (node = constraints; node !=NULL ; node = node->next) { - rc =constraint_expr_to_string (pdb, node->expr, &expr); + rc =constraint_expr_to_string (pdb, node->expr, &expr, &use_mls);if (rc !=0 ) { goto exit; } +if (use_mls) { +if (!pdb->mls) { +ERR (NULL ,"MLS constraint expression in non-mls policy" ); + rc =-1 ; + goto exit; + } + mls ="mls" ; + } +if (is_constraint) { perms =sepol_av_to_string (pdb, class->s.value, node->permissions);if (perms ==NULL ) {
Signature · 1 line
2.54.0
Re: [PATCH 3/3] libsepol: In module_to_cil use constrain and validatetrans where possible
Quoted 7 lines · James Carter
When converting a module to CIL for an MLS policy
module_to_cilwas always using mlsconstrain and mlsvalidatetrans.Instead only use mlsconstrain and mlsvalidatetrans when the constraint expression contains l1, l2, h1, or h2.
Quoted 6 lines
This follows the example of
kernel_to_cil.It also avoids an error that would occur if a user is referred to in a constrain that then becomes an mlsconstrain. "u1 ==
user_u" is allowed for a constrain, but the error "unknown useruser_u" will occur for an mlsconstrain.
Actually, only checkpolicy has this problem, so you would have to convert a base module to CIL and then used secil2conf to convert that to a policy.conf and compile it with checkpolicy to get the error.
But still want to follow the example of kernel_to_cil and kernel_to_conf for consistency.
Jim
Quoted 73 lines
Signed-off-by James Carter <[email protected]>--- libsepol/src/module_to_cil.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-)libsepol/src/module_to_cil.c +19
-6@@ -1742,7 +1742,7 @@ exit: } -static int constraint_expr_to_string (struct policydb *pdb,struct constraint_expr *exprs,char **expr_string) +static int constraint_expr_to_string (struct policydb *pdb,struct constraint_expr *exprs,char **expr_string,int *use_mls) {int rc =-1 ;struct constraint_expr *expr;@@ -1762,6 +1762,8 @@ static int constraint_expr_to_string(struct policydb *pdb, struct constraint_expunsigned int num_names =0 ;struct type_set *ts; + *use_mls =0 ; + rc =stack_init (&stack);if (rc !=0 ) { goto exit;@@ -1803,6 +1805,10 @@ static int constraint_expr_to_string(struct policydb *pdb, struct constraint_exp goto exit; } +if (expr->attr >=CEXPR_L1L2 ) { + *use_mls =1 ; + } +if (expr->expr_type ==CEXPR_ATTR ) {// length of values/attrs + 2 separating spaces + 2 parens + null terminator len =strlen (op) +strlen (attr1) +strlen (attr2) +2 +2 +1 ;@@ -1979,18 +1985,26 @@ static int constraints_to_cil(int indent, struct policydb *pdb, char *classkey,int rc =-1 ;struct constraint_node *node;char *expr =NULL ; -const char *mls; +int use_mls; +const char *mls ="" ;char *perms; - mls = pdb->mls ?"mls" :"" ; -for (node = constraints; node !=NULL ; node = node->next) { - rc =constraint_expr_to_string (pdb, node->expr, &expr); + rc =constraint_expr_to_string (pdb, node->expr, &expr, &use_mls);if (rc !=0 ) { goto exit; } +if (use_mls) { +if (!pdb->mls) { +ERR (NULL ,"MLS constraint expression in non-mls policy" ); + rc =-1 ; + goto exit; + } + mls ="mls" ; + } +if (is_constraint) { perms =sepol_av_to_string (pdb, class->s.value, node->permissions);if (perms ==NULL ) { --2.54.0
Re: [PATCH 3/3] libsepol: In module_to_cil use constrain and validatetrans where possible
In reply to James CarterQuoted 7 lines · James Carter
When converting a module to CIL for an MLS policy
module_to_cilwas always using mlsconstrain and mlsvalidatetrans.Instead only use mlsconstrain and mlsvalidatetrans when the constraint expression contains l1, l2, h1, or h2.
Quoted 6 lines
This follows the example of
kernel_to_cil.It also avoids an error that would occur if a user is referred to in a constrain that then becomes an mlsconstrain. "u1 ==
user_u" is allowed for a constrain, but the error "unknown useruser_u" will occur for an mlsconstrain.
I'm a little confused by the above - why would u1 == user_u be allowed for a constrain but fail for a mlsconstrain -
shouldn't it be consistent between both (i.e. user_u is either defined or it isn't)?
Likewise would be good to have an explicit test added for this.
Otherwise,
Quoted 74 lines
Signed-off-by James Carter <[email protected]>--- libsepol/src/module_to_cil.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-)libsepol/src/module_to_cil.c +19
-6@@ -1742,7 +1742,7 @@ exit: } -static int constraint_expr_to_string (struct policydb *pdb,struct constraint_expr *exprs,char **expr_string) +static int constraint_expr_to_string (struct policydb *pdb,struct constraint_expr *exprs,char **expr_string,int *use_mls) {int rc =-1 ;struct constraint_expr *expr;@@ -1762,6 +1762,8 @@ static int constraint_expr_to_string(struct policydb *pdb, struct constraint_expunsigned int num_names =0 ;struct type_set *ts; + *use_mls =0 ; + rc =stack_init (&stack);if (rc !=0 ) { goto exit;@@ -1803,6 +1805,10 @@ static int constraint_expr_to_string(struct policydb *pdb, struct constraint_exp goto exit; } +if (expr->attr >=CEXPR_L1L2 ) { + *use_mls =1 ; + } +if (expr->expr_type ==CEXPR_ATTR ) {// length of values/attrs + 2 separating spaces + 2 parens + null terminator len =strlen (op) +strlen (attr1) +strlen (attr2) +2 +2 +1 ;@@ -1979,18 +1985,26 @@ static int constraints_to_cil(int indent, struct policydb *pdb, char *classkey,int rc =-1 ;struct constraint_node *node;char *expr =NULL ; -const char *mls; +int use_mls; +const char *mls ="" ;char *perms; - mls = pdb->mls ?"mls" :"" ; -for (node = constraints; node !=NULL ; node = node->next) { - rc =constraint_expr_to_string (pdb, node->expr, &expr); + rc =constraint_expr_to_string (pdb, node->expr, &expr, &use_mls);if (rc !=0 ) { goto exit; } +if (use_mls) { +if (!pdb->mls) { +ERR (NULL ,"MLS constraint expression in non-mls policy" ); + rc =-1 ; + goto exit; + } + mls ="mls" ; + } +if (is_constraint) { perms =sepol_av_to_string (pdb, class->s.value, node->permissions);if (perms ==NULL ) { --2.54.0
Re: [PATCH 3/3] libsepol: In module_to_cil use constrain and validatetrans where possible
Quoted 16 lines · Stephen Smalley
Quoted 7 lines · James Carter
When converting a module to CIL for an MLS policy
module_to_cilwas always using mlsconstrain and mlsvalidatetrans.Instead only use mlsconstrain and mlsvalidatetrans when the constraint expression contains l1, l2, h1, or h2.
Quoted 6 lines
This follows the example of
kernel_to_cil.It also avoids an error that would occur if a user is referred to in a constrain that then becomes an mlsconstrain. "u1 ==
user_u" is allowed for a constrain, but the error "unknown useruser_u" will occur for an mlsconstrain.
Quoted 5 lines
I'm a little confused by the above - why would u1 ==
user_ube allowed for a constrain but fail for a mlsconstrain -
shouldn't it be consistent between both (i.e.user_uis either defined or it isn't)?
For some reason users are not added to the symtab on the first pass, so using a user in an mlsconstrain fails, but the normal constrain works because it is after the user declarations.
Jim
Quoted 79 lines
Likewise would be good to have an explicit test added for this.
Otherwise,
Acked-by Stephen Smalley <[email protected]>Quoted 74 lines
Signed-off-by James Carter <[email protected]>--- libsepol/src/module_to_cil.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-)libsepol/src/module_to_cil.c +19
-6@@ -1742,7 +1742,7 @@ exit: } -static int constraint_expr_to_string (struct policydb *pdb,struct constraint_expr *exprs,char **expr_string) +static int constraint_expr_to_string (struct policydb *pdb,struct constraint_expr *exprs,char **expr_string,int *use_mls) {int rc =-1 ;struct constraint_expr *expr;@@ -1762,6 +1762,8 @@ static int constraint_expr_to_string(struct policydb *pdb, struct constraint_expunsigned int num_names =0 ;struct type_set *ts; + *use_mls =0 ; + rc =stack_init (&stack);if (rc !=0 ) { goto exit;@@ -1803,6 +1805,10 @@ static int constraint_expr_to_string(struct policydb *pdb, struct constraint_exp goto exit; } +if (expr->attr >=CEXPR_L1L2 ) { + *use_mls =1 ; + } +if (expr->expr_type ==CEXPR_ATTR ) {// length of values/attrs + 2 separating spaces + 2 parens + null terminator len =strlen (op) +strlen (attr1) +strlen (attr2) +2 +2 +1 ;@@ -1979,18 +1985,26 @@ static int constraints_to_cil(int indent, struct policydb *pdb, char *classkey,int rc =-1 ;struct constraint_node *node;char *expr =NULL ; -const char *mls; +int use_mls; +const char *mls ="" ;char *perms; - mls = pdb->mls ?"mls" :"" ; -for (node = constraints; node !=NULL ; node = node->next) { - rc =constraint_expr_to_string (pdb, node->expr, &expr); + rc =constraint_expr_to_string (pdb, node->expr, &expr, &use_mls);if (rc !=0 ) { goto exit; } +if (use_mls) { +if (!pdb->mls) { +ERR (NULL ,"MLS constraint expression in non-mls policy" ); + rc =-1 ; + goto exit; + } + mls ="mls" ; + } +if (is_constraint) { perms =sepol_av_to_string (pdb, class->s.value, node->permissions);if (perms ==NULL ) { --2.54.0
Quoted 5 lines · James Carter
When converting a module to CIL, skip conditional blocks that do not have either a true block or a false block.
Signed-off-by James Carter <[email protected]>
Out of curiosity, when is this possible?
Quoted 21 lines
--- libsepol/src/module_to_cil.c | 2 ++ 1 file changed, 2 insertions(+)libsepol/src/module_to_cil.c +2
-1@@ -1426,6 +1426,8 @@ static int cond_list_to_cil(int indent, struct policydb *pdb, struct cond_node *struct cond_node *cond;for (cond = cond_list; cond !=NULL ; cond = cond->next) { +if (!cond->avtrue_list && !cond->avfalse_list) +continue ; rc =cond_expr_to_cil (indent, pdb, cond->expr, cond->flags);if (rc !=0 ) { --2.54.0
Quoted 9 lines · Stephen Smalley
Quoted 5 lines · James Carter
When converting a module to CIL, skip conditional blocks that do not have either a true block or a false block.
Signed-off-by James Carter <[email protected]>Out of curiosity, when is this possible?
Tunables. I don't know why this hasn't come up before.
When I was testing the patch to correctly choose tunableif or booleanif I came across this:
Original in module:
if (tun1b) {
allow tbd self : CLASS1 PERM1;
}
Output in CIL (before patch):
(allow tbd self (CLASS1 (PERM1)))
(tunableif (tun1b)
)
Original in module:
optional {
require {
tunable tun1a;
}
if (tun1a) {
allow tbc self : CLASS1 PERM1; }
}
Output in CIL (before patch):
(optional base_optional_3
(allow tbc self (CLASS1 (PERM1)))
(tunableif (tun1a)
)
)
Jim
Quoted 24 lines
Acked-by Stephen Smalley <[email protected]>Quoted 21 lines
--- libsepol/src/module_to_cil.c | 2 ++ 1 file changed, 2 insertions(+)libsepol/src/module_to_cil.c +2
-1@@ -1426,6 +1426,8 @@ static int cond_list_to_cil(int indent, struct policydb *pdb, struct cond_node *struct cond_node *cond;for (cond = cond_list; cond !=NULL ; cond = cond->next) { +if (!cond->avtrue_list && !cond->avfalse_list) +continue ; rc =cond_expr_to_cil (indent, pdb, cond->expr, cond->flags);if (rc !=0 ) { --2.54.0