libsepol: fix checkpolicy dontaudit compiler bug
PATCH v2 1/1 libsepol: fix checkpolicy dontaudit compiler bug
The combining logic for dontaudit rules was wrong, causing a dontaudit A B:C *; rule to be clobbered by a dontaudit A B:C p; rule.
--- libsepol/src/expand.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-)
libsepol/src/expand.c +12 -4
@@ -1604,7 +1604,8 @@ static int expand_range_trans(expand_state_t * state,static avtab_ptr_t find_avtab_node (sepol_handle_t * handle,avtab_t * avtab,avtab_key_t * key,cond_av_list_t ** cond, -av_extended_perms_t *xperms) +av_extended_perms_t *xperms, +char *alloced) {avtab_ptr_t node;avtab_datum_t avdatum;
@@ -1658,6 +1659,11 @@ static avtab_ptr_t find_avtab_node(sepol_handle_t * handle, nl->next = *cond; *cond = nl; } +if (alloced) + *alloced =1 ; + }else { +if (alloced) + *alloced =0 ; }return node;
@@ -1750,7 +1756,7 @@ static int expand_terule_helper(sepol_handle_t * handle,return EXPAND_RULE_CONFLICT ; } - node =find_avtab_node (handle, avtab, &avkey, cond,NULL ); + node =find_avtab_node (handle, avtab, &avkey, cond,NULL ,NULL );if (!node)return -1 ;if (enabled) {
@@ -1790,6 +1796,7 @@ static int expand_avrule_helper(sepol_handle_t * handle,class_perm_node_t *cur;uint32_t spec =0 ;unsigned int i; +char alloced;if (specified &AVRULE_ALLOWED ) { spec =AVTAB_ALLOWED ;
@@ -1824,7 +1831,8 @@ static int expand_avrule_helper(sepol_handle_t * handle, avkey.target_class = cur->tclass; avkey.specified = spec; - node =find_avtab_node (handle, avtab, &avkey, cond, extended_perms); + node =find_avtab_node (handle, avtab, &avkey, cond, + extended_perms, &alloced);if (!node)return EXPAND_RULE_ERROR ;if (enabled) {
@@ -1850,7 +1858,7 @@ static int expand_avrule_helper(sepol_handle_t * handle,*/ avdatump->data &= cur->data; }else if (specified &AVRULE_DONTAUDIT ) { -if (avdatump->data) +if (!alloced) avdatump->data &= ~cur->data;else avdatump->data = ~cur->data;
Signature · 1 line
2.7.4
Quoted 87 lines
-----Original Message-----
From: Selinux [mailto:[email protected]] On Behalf Of Stephen Smalley
Sent: Monday, November 14, 2016 9:48 AM
To: [email protected]
Cc: Stephen Smalley <[email protected]>
Subject: [PATCH v2] libsepol: fix checkpolicy dontaudit compiler bugThe combining logic for dontaudit rules was wrong, causing a dontaudit A B:C *; rule to be clobbered by a dontaudit A B:C p; rule.
Reported-by Nick Kralevich <[email protected]>Signed-off-by Stephen Smalley <[email protected]>--- libsepol/src/expand.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-)libsepol/src/expand.c index 004a029..d7adbf8 renamed from libsepol/src/expand.c +0
-0100644
libsepol/src/expand.c +7
-1@@ -1604,7 +1604,8 @@ static int expand_range_trans(expand_state_t * state,static avtab_ptr_t find_avtab_node (sepol_handle_t * handle,avtab_t * avtab,avtab_key_t * key,cond_av_list_t ** cond, -av_extended_perms_t *xperms) +av_extended_perms_t *xperms, +char *alloced) {avtab_ptr_t node;avtab_datum_t avdatum;@@ -1658,6 +1659,11 @@ static avtab_ptr_t find_avtab_node(sepol_handle_t * handle, nl->next = *cond; *cond = nl; } +if (alloced) + *alloced =1 ; + }else { +if (alloced) + *alloced =0 ; }libsepol/src/expand.c +1
-1return node;@@ -1750,7 +1756,7 @@ static int expand_terule_helper(sepol_handle_t * handle, return EXPAND_RULE_CONFLICT; } - node =find_avtab_node (handle, avtab, &avkey, cond,NULL ); + node =find_avtab_node (handle, avtab, &avkey, cond,NULL ,NULL );if (!node)libsepol/src/expand.c +1
-0return -1 ;if (enabled) {@@ -1790,6 +1796,7 @@ static int expand_avrule_helper(sepol_handle_t * handle, class_perm_node_t *cur;uint32_t spec =0 ;unsigned int i; +char alloced;if (specified &AVRULE_ALLOWED ) {libsepol/src/expand.c +2
-1spec =AVTAB_ALLOWED ;@@ -1824,7 +1831,8 @@ static int expand_avrule_helper(sepol_handle_t * handle, avkey.target_class = cur->tclass; avkey.specified = spec; - node =find_avtab_node (handle, avtab, &avkey, cond, extended_perms); + node =find_avtab_node (handle, avtab, &avkey, cond, + extended_perms, &alloced);if (!node)libsepol/src/expand.c +1
-1return EXPAND_RULE_ERROR ;if (enabled) {@@ -1850,7 +1858,7 @@ static int expand_avrule_helper(sepol_handle_t *handle, */ avdatump->data &= cur->data; }else if (specified &AVRULE_DONTAUDIT ) { -if (avdatump->data) +if (!alloced) avdatump->data &= ~cur->data;else
Quoted 1 line
avdatump->data= ~cur->data;
This seems awkward to me. If the insertion created a new empty node why wouldn't !avdump->data be true (note the addition of the not operator)?
Or perhaps a mechanism to actual set the data on allocation, this way the logic is Just &=.
Quoted 8 lines
--
2.7.4_______________________________________________
Selinux mailing list
[email protected]
To unsubscribe, send email to [email protected].
To get help, send an email containing "help" to [email protected].
Quoted 93 lines
-----Original Message-----
From: Selinux [mailto:[email protected]] On Behalf Of Roberts, William C
Sent: Monday, November 14, 2016 10:44 AM
To: Stephen Smalley <[email protected]>; [email protected]
Subject: RE: [PATCH v2] libsepol: fix checkpolicy dontaudit compiler bugQuoted 82 lines
-----Original Message-----
From: Selinux [mailto:[email protected]] On Behalf Of Stephen Smalley
Sent: Monday, November 14, 2016 9:48 AM
To: [email protected]
Cc: Stephen Smalley <[email protected]>
Subject: [PATCH v2] libsepol: fix checkpolicy dontaudit compiler bugThe combining logic for dontaudit rules was wrong, causing a dontaudit A B:C *; rule to be clobbered by a dontaudit A B:C p; rule.
Reported-by Nick Kralevich <[email protected]>Signed-off-by Stephen Smalley <[email protected]>--- libsepol/src/expand.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-)libsepol/src/expand.c index renamed from libsepol/src/expand.c +0
-0004a029..d7adbf8
100644libsepol/src/expand.c +7
-1@@ -1604,7 +1604,8 @@ static int expand_range_trans(expand_state_t * state, static avtab_ptr_t find_avtab_node(sepol_handle_t * handle,avtab_t * avtab,avtab_key_t * key,cond_av_list_t ** cond, -av_extended_perms_t *xperms) +av_extended_perms_t *xperms, +char *alloced) {avtab_ptr_t node;avtab_datum_t avdatum;@@ -1658,6 +1659,11 @@ static avtab_ptr_tfind_avtab_node (sepol_handle_t * handle, nl->next = *cond; *cond = nl; } +if (alloced) + *alloced =1 ; + }else { +if (alloced) + *alloced =0 ; }libsepol/src/expand.c +1
-1return node;@@ -1750,7 +1756,7 @@ static int expand_terule_helper(sepol_handle_t * handle, return EXPAND_RULE_CONFLICT; } - node =find_avtab_node (handle, avtab, &avkey, cond,NULL ); + node =find_avtab_node (handle, avtab, &avkey, cond,NULL ,NULL );if (!node)libsepol/src/expand.c +1
-0return -1 ;if (enabled) {@@ -1790,6 +1796,7 @@ static int expand_avrule_helper(sepol_handle_t * handle, class_perm_node_t *cur;uint32_t spec =0 ;unsigned int i; +char alloced;if (specified &AVRULE_ALLOWED ) {libsepol/src/expand.c +2
-1spec =AVTAB_ALLOWED ;@@ -1824,7 +1831,8 @@ static int expand_avrule_helper(sepol_handle_t * handle, avkey.target_class = cur->tclass; avkey.specified = spec; - node =find_avtab_node (handle, avtab, &avkey, cond, extended_perms); + node =find_avtab_node (handle, avtab, &avkey, cond, + extended_perms, &alloced);if (!node)libsepol/src/expand.c +0
-0return EXPAND_RULE_ERROR ;if (enabled) {@@ -1850,7 +1858,7 @@ static int expand_avrule_helper(sepol_handle_t *handle, */ avdatump->data &= cur->data; }else if (specified &AVRULE_DONTAUDIT ) {
Quoted 8 lines
Quoted 5 lines
libsepol/src/expand.c +1
-1-if (avdatump->data) +if (!alloced) avdatump->data &= ~cur->data;else
avdatump->data= ~cur->data;This seems awkward to me. If the insertion created a new empty node why wouldn't !avdump->data be true (note the addition of the not operator)?
I misstated that a bit, but the !avdump->data was the else case. I am really saying why didn't this work before? In my mind, we don't care if its allocated we care if it's set or not.
Quoted 18 lines
Or perhaps a mechanism to actual set the data on allocation, this way the logic is Just &=.
Quoted 8 lines
--
2.7.4_______________________________________________
Selinux mailing list
[email protected]
To unsubscribe, send email to [email protected].
To get help, send an email containing "help" to [email protected]._______________________________________________
Selinux mailing list
[email protected]
To unsubscribe, send email to [email protected].
To get help, send an email containing "help" to [email protected].
Quoted 102 lines · William C
Quoted 93 lines
-----Original Message-----
From: Selinux [mailto:[email protected]] On Behalf Of Roberts, William C
Sent: Monday, November 14, 2016 10:44 AM
To: Stephen Smalley <[email protected]>; [email protected]
Subject: RE: [PATCH v2] libsepol: fix checkpolicy dontaudit compiler bugQuoted 82 lines
-----Original Message-----
From: Selinux [mailto:[email protected]] On Behalf Of Stephen Smalley
Sent: Monday, November 14, 2016 9:48 AM
To: [email protected]
Cc: Stephen Smalley <[email protected]>
Subject: [PATCH v2] libsepol: fix checkpolicy dontaudit compiler bugThe combining logic for dontaudit rules was wrong, causing a dontaudit A B:C *; rule to be clobbered by a dontaudit A B:C p; rule.
Reported-by Nick Kralevich <[email protected]>Signed-off-by Stephen Smalley <[email protected]>--- libsepol/src/expand.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-)libsepol/src/expand.c index renamed from libsepol/src/expand.c +0
-0004a029..d7adbf8
100644libsepol/src/expand.c +7
-1@@ -1604,7 +1604,8 @@ static int expand_range_trans(expand_state_t * state, static avtab_ptr_t find_avtab_node(sepol_handle_t * handle,avtab_t * avtab,avtab_key_t * key,cond_av_list_t ** cond, -av_extended_perms_t *xperms) +av_extended_perms_t *xperms, +char *alloced) {avtab_ptr_t node;avtab_datum_t avdatum;@@ -1658,6 +1659,11 @@ static avtab_ptr_tfind_avtab_node (sepol_handle_t * handle, nl->next = *cond; *cond = nl; } +if (alloced) + *alloced =1 ; + }else { +if (alloced) + *alloced =0 ; }libsepol/src/expand.c +1
-1return node;@@ -1750,7 +1756,7 @@ static int expand_terule_helper(sepol_handle_t * handle, return EXPAND_RULE_CONFLICT; } - node =find_avtab_node (handle, avtab, &avkey, cond,NULL ); + node =find_avtab_node (handle, avtab, &avkey, cond,NULL ,NULL );if (!node)libsepol/src/expand.c +1
-0return -1 ;if (enabled) {@@ -1790,6 +1796,7 @@ static int expand_avrule_helper(sepol_handle_t * handle, class_perm_node_t *cur;uint32_t spec =0 ;unsigned int i; +char alloced;if (specified &AVRULE_ALLOWED ) {libsepol/src/expand.c +2
-1spec =AVTAB_ALLOWED ;@@ -1824,7 +1831,8 @@ static int expand_avrule_helper(sepol_handle_t * handle, avkey.target_class = cur->tclass; avkey.specified = spec; - node =find_avtab_node (handle, avtab, &avkey, cond, extended_perms); + node =find_avtab_node (handle, avtab, &avkey, cond, + extended_perms, &alloced);if (!node)libsepol/src/expand.c +0
-0return EXPAND_RULE_ERROR ;if (enabled) {@@ -1850,7 +1858,7 @@ static int expand_avrule_helper(sepol_handle_t *handle, */ avdatump->data &= cur->data; }else if (specified &AVRULE_DONTAUDIT ) {Quoted 8 lines
Quoted 5 lines
libsepol/src/expand.c +1
-1-if (avdatump->data) +if (!alloced) avdatump->data &= ~cur->data;else
avdatump->data= ~cur->data;This seems awkward to me. If the insertion created a new empty node why wouldn't !avdump->data be true (note the addition of the not operator)?
Quoted 3 lines
I misstated that a bit, but the !avdump->data was the else case. I am really saying why didn't this work before? In my mind, we don't care if its allocated we care if it's set or not.
The old logic wrongly assumed that !avdatump->data would only be true if this was the first dontaudit rule for the (source type, target type, target class) and the node had just been allocated by find_avtab_node() with a zero avdatump->data value.
However, if you have a dontaudit A B:C *; rule, then the set complement of it will be 0, so we can have !avdatump->data be true in that case too. Thus, if we end up processing:
dontaudit A B:C *;
dontaudit A B:C { p1 p2 ... };we'll end up clobbering avdatump->data with ~{ p1 p2 ... }.
The marlin policy contains:
dontaudit su self:capability *; dontaudit domain self:capability sys_module; and self rules are expanded (the kernel has no notion of self), so we end up with: dontaudit su self:capability *; dontaudit su self:capability sys_module;
We have never encountered this situation before because there are no dontaudit A B:C *; rules in refpolicy; that's a corner case that only shows up in Android's su policy, and only because it is a permissive domain with no explicit allow rules (other than those picked up via macros that set up attributes or transitions).
The fix was to replace the avdatump->data test with an explicit indication that the node was freshly allocated i.e. this is the first such rule. I agree that it could be clearer, but I was going for the simplest, least invasive fix for now, both due to limited time and to ease back-porting.
Quoted 3 lines
Quoted 3 lines
Or perhaps a mechanism to actual set the data on allocation, this way the logic is Just &=.
For bit setting in constant time, one could always clear the bit(s) and or in what you want. I think that logic might be applicable here. I could take a stab at looking at it today, if no one has anything better by tomorrow well just merge yours as is. Does that sound reasonable?
Quoted 153 lines · Stephen Smalley
Quoted 4 lines · William C
Quoted 4 lines
-----Original Message-----
From: Selinux [mailto:[email protected]] On Behalf OfRoberts,
Quoted 98 lines
Quoted 89 lines
William C
Sent: Monday, November 14, 2016 10:44 AM
To: Stephen Smalley <[email protected]>; [email protected]
Subject: RE: [PATCH v2] libsepol: fix checkpolicy dontaudit compiler bugQuoted 82 lines
-----Original Message-----
From: Selinux [mailto:[email protected]] On Behalf Of Stephen Smalley
Sent: Monday, November 14, 2016 9:48 AM
To: [email protected]
Cc: Stephen Smalley <[email protected]>
Subject: [PATCH v2] libsepol: fix checkpolicy dontaudit compiler bugThe combining logic for dontaudit rules was wrong, causing a dontaudit A B:C *; rule to be clobbered by a dontaudit A B:C p; rule.
Reported-by Nick Kralevich <[email protected]>Signed-off-by Stephen Smalley <[email protected]>--- libsepol/src/expand.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-)libsepol/src/expand.c index renamed from libsepol/src/expand.c +0
-0004a029..d7adbf8
100644libsepol/src/expand.c +7
-1@@ -1604,7 +1604,8 @@ static int expand_range_trans(expand_state_t * state, static avtab_ptr_t find_avtab_node(sepol_handle_t * handle,avtab_t * avtab,avtab_key_t * key,cond_av_list_t ** cond, -av_extended_perms_t *xperms) +av_extended_perms_t *xperms, +char *alloced) {avtab_ptr_t node;avtab_datum_t avdatum;@@ -1658,6 +1659,11 @@ static avtab_ptr_tfind_avtab_node (sepol_handle_t * handle, nl->next = *cond; *cond = nl; } +if (alloced) + *alloced =1 ; + }else { +if (alloced) + *alloced =0 ; }return node;libsepol/src/expand.c +1
-1@@ -1750,7 +1756,7 @@ static int expand_terule_helper(sepol_handle_t * handle, return EXPAND_RULE_CONFLICT; } - node =find_avtab_node (handle, avtab, &avkey, cond,NULL ); + node =find_avtab_node (handle, avtab, &avkey, cond,NULL ,NULL );if (!node)return -1; if (enabled) {libsepol/src/expand.c +1
-0@@ -1790,6 +1796,7 @@ static int expand_avrule_helper(sepol_handle_t * handle, class_perm_node_t *cur;uint32_t spec =0 ;unsigned int i; +char alloced;if (specified &AVRULE_ALLOWED ) {spec = AVTAB_ALLOWED;libsepol/src/expand.c +2
-1@@ -1824,7 +1831,8 @@ static int expand_avrule_helper(sepol_handle_t * handle, avkey.target_class = cur->tclass; avkey.specified = spec; - node =find_avtab_node (handle, avtab, &avkey, cond, extended_perms); + node =find_avtab_node (handle, avtab, &avkey, cond, + extended_perms, &alloced);if (!node)return EXPAND_RULE_ERROR; if (enabled) {libsepol/src/expand.c +0
-0@@ -1850,7 +1858,7 @@ static int expand_avrule_helper(sepol_handle_t *handle, */ avdatump->data &= cur->data; }else if (specified &AVRULE_DONTAUDIT ) {Quoted 8 lines
Quoted 5 lines
libsepol/src/expand.c +1
-1-if (avdatump->data) +if (!alloced) avdatump->data &= ~cur->data;else avdatump->data = ~cur->data;This seems awkward to me. If the insertion created a new empty node why wouldn't !avdump->data be true (note the addition of the not operator)?
Quoted 1 line
I misstated that a bit, but the !avdump->data was the else case. I am
really
Quoted 1 line
saying why didn't this work before? In my mind, we don't care if its
allocated
Quoted 1 line
we care if it's set or not.
The old logic wrongly assumed that !avdatump->data would only be true if this was the first dontaudit rule for the (source type, target type, target class) and the node had just been allocated by
find_avtab_node()with a zeroavdatump->datavalue.
However, if you have a dontaudit A B:C *; rule, then the set complement of it will be 0, so we can have !avdatump->data be true in that case too. Thus, if we end up processing:dontaudit A B:C *; dontaudit A B:C { p1 p2 ... };we'll end up clobbering
avdatump->datawith ~{ p1 p2 ... }.The marlin policy contains:
dontaudit su self:capability *; dontaudit domain self:capability sys_module; and self rules are expanded (the kernel has no notion of self), so we end up with: dontaudit su self:capability *; dontaudit su self:capability sys_module;We have never encountered this situation before because there are no dontaudit A B:C *; rules in refpolicy; that's a corner case that only shows up in Android's su policy, and only because it is a permissive domain with no explicit allow rules (other than those picked up via macros that set up attributes or transitions).
The fix was to replace the
avdatump->datatest with an explicit indication that the node was freshly allocated i.e. this is the first such rule. I agree that it could be clearer, but I was going for the simplest, least invasive fix for now, both due to limited time and to ease back-porting.Quoted 2 lines
Quoted 2 lines
Or perhaps a mechanism to actual set the data on allocation, this way
the logic is
Quoted 1 line
Quoted 1 line
Just &=.
_______________________________________________
Selinux mailing list
[email protected]
To unsubscribe, send email to [email protected]. To get help, send an email containing "help" to [email protected].
Perhaps merge Stephen's change now, and address any changes in a followup? I'd like to get it merged into the selinux tree so I can merge the changes into the Android tree.
-- Nick
Quoted 169 lines · William Roberts
For bit setting in constant time, one could always clear the bit(s) and or in what you want. I think that logic might be applicable here. I could take a stab at looking at it today, if no one has anything better by tomorrow well just merge yours as is. Does that sound reasonable?
Quoted 148 lines · Stephen Smalley
Quoted 104 lines · William C
Quoted 95 lines
-----Original Message-----
From: Selinux [mailto:[email protected]] On Behalf Of Roberts,
William C
Sent: Monday, November 14, 2016 10:44 AM
To: Stephen Smalley <[email protected]>; [email protected]
Subject: RE: [PATCH v2] libsepol: fix checkpolicy dontaudit compiler bugQuoted 82 lines
-----Original Message-----
From: Selinux [mailto:[email protected]] On Behalf Of Stephen Smalley
Sent: Monday, November 14, 2016 9:48 AM
To: [email protected]
Cc: Stephen Smalley <[email protected]>
Subject: [PATCH v2] libsepol: fix checkpolicy dontaudit compiler bugThe combining logic for dontaudit rules was wrong, causing a dontaudit A B:C *; rule to be clobbered by a dontaudit A B:C p; rule.
Reported-by Nick Kralevich <[email protected]>Signed-off-by Stephen Smalley <[email protected]>--- libsepol/src/expand.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-)libsepol/src/expand.c index renamed from libsepol/src/expand.c +0
-0004a029..d7adbf8
100644libsepol/src/expand.c +7
-1@@ -1604,7 +1604,8 @@ static int expand_range_trans(expand_state_t * state, static avtab_ptr_t find_avtab_node(sepol_handle_t * handle,avtab_t * avtab,avtab_key_t * key,cond_av_list_t ** cond, -av_extended_perms_t *xperms) +av_extended_perms_t *xperms, +char *alloced) {avtab_ptr_t node;avtab_datum_t avdatum;@@ -1658,6 +1659,11 @@ static avtab_ptr_tfind_avtab_node (sepol_handle_t * handle, nl->next = *cond; *cond = nl; } +if (alloced) + *alloced =1 ; + }else { +if (alloced) + *alloced =0 ; }return node;libsepol/src/expand.c +1
-1@@ -1750,7 +1756,7 @@ static int expand_terule_helper(sepol_handle_t * handle, return EXPAND_RULE_CONFLICT; } - node =find_avtab_node (handle, avtab, &avkey, cond,NULL ); + node =find_avtab_node (handle, avtab, &avkey, cond,NULL ,NULL );if (!node)return -1; if (enabled) {libsepol/src/expand.c +1
-0@@ -1790,6 +1796,7 @@ static int expand_avrule_helper(sepol_handle_t * handle, class_perm_node_t *cur;uint32_t spec =0 ;unsigned int i; +char alloced;if (specified &AVRULE_ALLOWED ) {spec = AVTAB_ALLOWED;libsepol/src/expand.c +2
-1@@ -1824,7 +1831,8 @@ static int expand_avrule_helper(sepol_handle_t * handle, avkey.target_class = cur->tclass; avkey.specified = spec; - node =find_avtab_node (handle, avtab, &avkey, cond, extended_perms); + node =find_avtab_node (handle, avtab, &avkey, cond, + extended_perms, &alloced);if (!node)return EXPAND_RULE_ERROR; if (enabled) {libsepol/src/expand.c +0
-0@@ -1850,7 +1858,7 @@ static int expand_avrule_helper(sepol_handle_t *handle, */ avdatump->data &= cur->data; }else if (specified &AVRULE_DONTAUDIT ) {Quoted 8 lines
Quoted 5 lines
libsepol/src/expand.c +1
-1-if (avdatump->data) +if (!alloced) avdatump->data &= ~cur->data;else avdatump->data = ~cur->data;This seems awkward to me. If the insertion created a new empty node why wouldn't !avdump->data be true (note the addition of the not operator)?
Quoted 5 lines
I misstated that a bit, but the !avdump->data was the else case. I am really
saying why didn't this work before? In my mind, we don't care if its allocated
we care if it's set or not.The old logic wrongly assumed that !avdatump->data would only be true if this was the first dontaudit rule for the (source type, target type, target class) and the node had just been allocated by
find_avtab_node()with a zeroavdatump->datavalue.
However, if you have a dontaudit A B:C *; rule, then the set complement of it will be 0, so we can have !avdatump->data be true in that case too. Thus, if we end up processing:dontaudit A B:C *; dontaudit A B:C { p1 p2 ... };we'll end up clobbering
avdatump->datawith ~{ p1 p2 ... }.The marlin policy contains:
dontaudit su self:capability *; dontaudit domain self:capability sys_module; and self rules are expanded (the kernel has no notion of self), so we end up with: dontaudit su self:capability *; dontaudit su self:capability sys_module;We have never encountered this situation before because there are no dontaudit A B:C *; rules in refpolicy; that's a corner case that only shows up in Android's su policy, and only because it is a permissive domain with no explicit allow rules (other than those picked up via macros that set up attributes or transitions).
The fix was to replace the
avdatump->datatest with an explicit indication that the node was freshly allocated i.e. this is the first such rule. I agree that it could be clearer, but I was going for the simplest, least invasive fix for now, both due to limited time and to ease back-porting.Quoted 4 lines
Quoted 4 lines
Or perhaps a mechanism to actual set the data on allocation, this way the logic is
Just &=.Quoted 6 lines
_______________________________________________
Selinux mailing list
[email protected]
To unsubscribe, send email to [email protected]. To get help, send an email containing "help" to [email protected]._______________________________________________
Selinux mailing list
[email protected]
To unsubscribe, send email to [email protected]. To get help, send an email containing "help" to [email protected].
Signature · 1 line
Nick Kralevich | Android Security | [email protected] | 650.214.4037
Quoted 4 lines · Nick Kralevich
Perhaps merge Stephen's change now, and address any changes in a followup? I'd like to get it merged into the selinux tree so I can merge the changes into the Android tree.
That's fine with me too.
Quoted 6 lines
-- Nick
Quoted 1 line · William Roberts
For bit setting in constant time, one could always clear the bit(s) and
or
Quoted 1 line
Quoted 1 line
in what you want. I think that logic might be applicable here. I could
take
Quoted 20 lines
Quoted 20 lines
a stab at looking at it today, if no one has anything better by tomorrow well just merge yours as is. Does that sound reasonable?
Quoted 15 lines · Stephen Smalley
Quoted 13 lines · William C
Quoted 13 lines
-----Original Message-----
From: Selinux [mailto:[email protected]] On Behalf Of Roberts,
William C
Sent: Monday, November 14, 2016 10:44 AM
To: Stephen Smalley <[email protected]>; [email protected]
Subject: RE: [PATCH v2] libsepol: fix checkpolicy dontaudit compiler bug
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
-----Original Message-----
From: Selinux [mailto:[email protected]] On Behalf Of Stephen Smalley
Sent: Monday, November 14, 2016 9:48 AM
To: [email protected]
Cc: Stephen Smalley <[email protected]>
Subject: [PATCH v2] libsepol: fix checkpolicy dontaudit compiler
bug
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
The combining logic for dontaudit rules was wrong, causing a
dontaudit
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
A B:C *; rule to be clobbered by a dontaudit A B:C p; rule.
Reported-by Nick Kralevich <[email protected]>Signed-off-by Stephen Smalley <[email protected]>--- libsepol/src/expand.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-)
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
libsepol/src/expand.c static int expand_range_trans(expand_state_t
Quoted 6 lines
libsepol/src/expand.c index renamed from libsepol/src/expand.c +0
-0004a029..d7adbf8
100644libsepol/src/expand.c +0
-0@@ -1604,7 +1604,8 @@ static int expand_range_trans(expand_state_t
*
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
libsepol/src/expand.c +7
-1state, static avtab_ptr_t find_avtab_node(sepol_handle_t * handle,avtab_t * avtab,avtab_key_t * key,cond_av_list_t ** cond, -av_extended_perms_t *xperms) +av_extended_perms_t *xperms, +char *alloced) {avtab_ptr_t node;avtab_datum_t avdatum;@@ -1658,6 +1659,11 @@ static avtab_ptr_tfind_avtab_node (sepol_handle_t * handle, nl->next = *cond; *cond = nl; } +if (alloced) + *alloced =1 ; + }else { +if (alloced) + *alloced =0 ; }
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
libsepol/src/expand.c static int
Quoted 2 lines
return node;libsepol/src/expand.c +0
-0@@ -1750,7 +1756,7 @@ static int
expand_terule_helper(sepol_handle_t *
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
libsepol/src/expand.c static int expand_terule_helper(sepol_handle_t * handle,
Quoted 5 lines
libsepol/src/expand.c +0
-1handle, return EXPAND_RULE_CONFLICT; } - node = find_avtab_node(handle, avtab, &avkey, cond,
NULL);
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
libsepol/src/expand.c static int expand_avrule_helper(sepol_handle_t * handle, · line 1834
Quoted 1 line
libsepol/src/expand.c +1
-0+ node = find_avtab_node(handle, avtab, &avkey, cond,
NULL,
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
libsepol/src/expand.c static int
Quoted 5 lines
libsepol/src/expand.c +0
-0NULL);if (!node)return -1; if (enabled) {libsepol/src/expand.c +0
-0@@ -1790,6 +1796,7 @@ static int
expand_avrule_helper(sepol_handle_t *
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
libsepol/src/expand.c static int
Quoted 9 lines
libsepol/src/expand.c +1
-0handle, class_perm_node_t *cur;uint32_t spec =0 ;unsigned int i; +char alloced;if (specified &AVRULE_ALLOWED ) {spec = AVTAB_ALLOWED;libsepol/src/expand.c +0
-0@@ -1824,7 +1831,8 @@ static int
expand_avrule_helper(sepol_handle_t *
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
libsepol/src/expand.c +0
-0handle, avkey.target_class = cur->tclass; avkey.specified = spec;
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
libsepol/src/expand.c static int
Quoted 8 lines
libsepol/src/expand.c +2
-1- node =find_avtab_node (handle, avtab, &avkey, cond, extended_perms); + node =find_avtab_node (handle, avtab, &avkey, cond, + extended_perms, &alloced);if (!node)return EXPAND_RULE_ERROR; if (enabled) {libsepol/src/expand.c +0
-0@@ -1850,7 +1858,7 @@ static int
expand_avrule_helper(sepol_handle_t *
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 9 lines
libsepol/src/expand.c +1
-1handle, */ avdatump->data &= cur->data; }else if (specified &AVRULE_DONTAUDIT ) { -if (avdatump->data) +if (!alloced) avdatump->data &= ~cur->data;else avdatump->data = ~cur->data;This seems awkward to me. If the insertion created a new empty node
why
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
wouldn't !avdump->data be true (note the addition of the not
operator)?
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 6 lines
I misstated that a bit, but the !avdump->data was the else case. I am really
saying why didn't this work before? In my mind, we don't care if its allocated
we care if it's set or not.The old logic wrongly assumed that !avdatump->data would only be true
if
Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
this was the first dontaudit rule for the (source type, target type, target class) and the node had just been allocated by
find_avtab_node()with a zeroavdatump->datavalue.
However, if you have a dontaudit A B:C *; rule, then the set complement of it will be 0, so we can have !avdatump->data be true in that case too. Thus, if we end up processing:dontaudit A B:C *; dontaudit A B:C { p1 p2 ... };we'll end up clobbering
avdatump->datawith ~{ p1 p2 ... }.The marlin policy contains:
dontaudit su self:capability *; dontaudit domain self:capability sys_module; and self rules are expanded (the kernel has no notion of self), so we end up with: dontaudit su self:capability *; dontaudit su self:capability sys_module;We have never encountered this situation before because there are no dontaudit A B:C *; rules in refpolicy; that's a corner case that only shows up in Android's su policy, and only because it is a permissive domain with no explicit allow rules (other than those picked up via macros that set up attributes or transitions).
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
The fix was to replace the
avdatump->datatest with an explicit indication that the node was freshly allocated i.e. this is the first such rule. I agree that it could be clearer, but I was going for the simplest, least invasive fix for now, both due to limited time and to ease back-porting.Quoted 2 lines
Quoted 2 lines
Or perhaps a mechanism to actual set the data on allocation, this
way
Quoted 22 lines
Quoted 11 lines
Quoted 9 lines
Quoted 2 lines
Quoted 2 lines
the logic is
Just &=._______________________________________________
Selinux mailing list
[email protected]
To unsubscribe, send email to [email protected]. To get help, send an email containing "help" to [email protected].Quoted 6 lines
_______________________________________________
Selinux mailing list
[email protected]
To unsubscribe, send email to [email protected]. To get help, send an email containing "help" to [email protected].--
Nick Kralevich | Android Security | [email protected] | 650.214.4037
Quoted 3 lines · Nick Kralevich
Perhaps merge Stephen's change now, and address any changes in a followup? I'd like to get it merged into the selinux tree so I can merge the changes into the Android tree.
Yes, it is already merged.
Quoted 177 lines
-- Nick
Quoted 169 lines · William Roberts
For bit setting in constant time, one could always clear the bit(s) and or in what you want. I think that logic might be applicable here. I could take a stab at looking at it today, if no one has anything better by tomorrow well just merge yours as is. Does that sound reasonable?
Quoted 148 lines · Stephen Smalley
Quoted 104 lines · William C
Quoted 95 lines
-----Original Message-----
From: Selinux [mailto:[email protected]] On Behalf Of Roberts,
William C
Sent: Monday, November 14, 2016 10:44 AM
To: Stephen Smalley <[email protected]>; [email protected]
Subject: RE: [PATCH v2] libsepol: fix checkpolicy dontaudit compiler bugQuoted 82 lines
-----Original Message-----
From: Selinux [mailto:[email protected]] On Behalf Of Stephen Smalley
Sent: Monday, November 14, 2016 9:48 AM
To: [email protected]
Cc: Stephen Smalley <[email protected]>
Subject: [PATCH v2] libsepol: fix checkpolicy dontaudit compiler bugThe combining logic for dontaudit rules was wrong, causing a dontaudit A B:C *; rule to be clobbered by a dontaudit A B:C p; rule.
Reported-by Nick Kralevich <[email protected]>Signed-off-by Stephen Smalley <[email protected]>--- libsepol/src/expand.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-)libsepol/src/expand.c index renamed from libsepol/src/expand.c +0
-0004a029..d7adbf8
100644libsepol/src/expand.c +7
-1@@ -1604,7 +1604,8 @@ static int expand_range_trans(expand_state_t * state, static avtab_ptr_t find_avtab_node(sepol_handle_t * handle,avtab_t * avtab,avtab_key_t * key,cond_av_list_t ** cond, -av_extended_perms_t *xperms) +av_extended_perms_t *xperms, +char *alloced) {avtab_ptr_t node;avtab_datum_t avdatum;@@ -1658,6 +1659,11 @@ static avtab_ptr_tfind_avtab_node (sepol_handle_t * handle, nl->next = *cond; *cond = nl; } +if (alloced) + *alloced =1 ; + }else { +if (alloced) + *alloced =0 ; }return node;libsepol/src/expand.c +1
-1@@ -1750,7 +1756,7 @@ static int expand_terule_helper(sepol_handle_t * handle, return EXPAND_RULE_CONFLICT; } - node =find_avtab_node (handle, avtab, &avkey, cond,NULL ); + node =find_avtab_node (handle, avtab, &avkey, cond,NULL ,NULL );if (!node)return -1; if (enabled) {libsepol/src/expand.c +1
-0@@ -1790,6 +1796,7 @@ static int expand_avrule_helper(sepol_handle_t * handle, class_perm_node_t *cur;uint32_t spec =0 ;unsigned int i; +char alloced;if (specified &AVRULE_ALLOWED ) {spec = AVTAB_ALLOWED;libsepol/src/expand.c +2
-1@@ -1824,7 +1831,8 @@ static int expand_avrule_helper(sepol_handle_t * handle, avkey.target_class = cur->tclass; avkey.specified = spec; - node =find_avtab_node (handle, avtab, &avkey, cond, extended_perms); + node =find_avtab_node (handle, avtab, &avkey, cond, + extended_perms, &alloced);if (!node)return EXPAND_RULE_ERROR; if (enabled) {libsepol/src/expand.c +0
-0@@ -1850,7 +1858,7 @@ static int expand_avrule_helper(sepol_handle_t *handle, */ avdatump->data &= cur->data; }else if (specified &AVRULE_DONTAUDIT ) {Quoted 8 lines
Quoted 5 lines
libsepol/src/expand.c +1
-1-if (avdatump->data) +if (!alloced) avdatump->data &= ~cur->data;else avdatump->data = ~cur->data;This seems awkward to me. If the insertion created a new empty node why wouldn't !avdump->data be true (note the addition of the not operator)?
Quoted 5 lines
I misstated that a bit, but the !avdump->data was the else case. I am really
saying why didn't this work before? In my mind, we don't care if its allocated
we care if it's set or not.The old logic wrongly assumed that !avdatump->data would only be true if this was the first dontaudit rule for the (source type, target type, target class) and the node had just been allocated by
find_avtab_node()with a zeroavdatump->datavalue.
However, if you have a dontaudit A B:C *; rule, then the set complement of it will be 0, so we can have !avdatump->data be true in that case too. Thus, if we end up processing:dontaudit A B:C *; dontaudit A B:C { p1 p2 ... };we'll end up clobbering
avdatump->datawith ~{ p1 p2 ... }.The marlin policy contains:
dontaudit su self:capability *; dontaudit domain self:capability sys_module; and self rules are expanded (the kernel has no notion of self), so we end up with: dontaudit su self:capability *; dontaudit su self:capability sys_module;We have never encountered this situation before because there are no dontaudit A B:C *; rules in refpolicy; that's a corner case that only shows up in Android's su policy, and only because it is a permissive domain with no explicit allow rules (other than those picked up via macros that set up attributes or transitions).
The fix was to replace the
avdatump->datatest with an explicit indication that the node was freshly allocated i.e. this is the first such rule. I agree that it could be clearer, but I was going for the simplest, least invasive fix for now, both due to limited time and to ease back-porting.Quoted 4 lines
Quoted 4 lines
Or perhaps a mechanism to actual set the data on allocation, this way the logic is
Just &=.Quoted 6 lines
_______________________________________________
Selinux mailing list
[email protected]
To unsubscribe, send email to [email protected]. To get help, send an email containing "help" to [email protected]._______________________________________________
Selinux mailing list
[email protected]
To unsubscribe, send email to [email protected]. To get help, send an email containing "help" to [email protected].
Quoted 5 lines · Stephen Smalley
The combining logic for dontaudit rules was wrong, causing a dontaudit A B:C *; rule to be clobbered by a dontaudit A B:C p; rule.
Reported-by Nick Kralevich <[email protected]>
This looks like it fixes my problem. Thanks!
Quoted 70 lines
Signed-off-by Stephen Smalley <[email protected]>--- libsepol/src/expand.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-)libsepol/src/expand.c +12
-5@@ -1604,7 +1604,8 @@ static int expand_range_trans(expand_state_t * state,static avtab_ptr_t find_avtab_node (sepol_handle_t * handle,avtab_t * avtab,avtab_key_t * key,cond_av_list_t ** cond, -av_extended_perms_t *xperms) +av_extended_perms_t *xperms, +char *alloced) {avtab_ptr_t node;avtab_datum_t avdatum;@@ -1658,6 +1659,11 @@ static avtab_ptr_t find_avtab_node(sepol_handle_t * handle, nl->next = *cond; *cond = nl; } +if (alloced) + *alloced =1 ; + }else { +if (alloced) + *alloced =0 ; }return node;@@ -1750,7 +1756,7 @@ static int expand_terule_helper(sepol_handle_t * handle,return EXPAND_RULE_CONFLICT ; } - node =find_avtab_node (handle, avtab, &avkey, cond,NULL ); + node =find_avtab_node (handle, avtab, &avkey, cond,NULL ,NULL );if (!node)return -1 ;if (enabled) {@@ -1790,6 +1796,7 @@ static int expand_avrule_helper(sepol_handle_t * handle,class_perm_node_t *cur;uint32_t spec =0 ;unsigned int i; +char alloced;if (specified &AVRULE_ALLOWED ) { spec =AVTAB_ALLOWED ;@@ -1824,7 +1831,8 @@ static int expand_avrule_helper(sepol_handle_t * handle, avkey.target_class = cur->tclass; avkey.specified = spec; - node =find_avtab_node (handle, avtab, &avkey, cond, extended_perms); + node =find_avtab_node (handle, avtab, &avkey, cond, + extended_perms, &alloced);if (!node)return EXPAND_RULE_ERROR ;if (enabled) {@@ -1850,7 +1858,7 @@ static int expand_avrule_helper(sepol_handle_t * handle,*/ avdatump->data &= cur->data; }else if (specified &AVRULE_DONTAUDIT ) { -if (avdatump->data) +if (!alloced) avdatump->data &= ~cur->data;else avdatump->data = ~cur->data; --2.7.4
Signature · 1 line
Nick Kralevich | Android Security | [email protected] | 650.214.4037
Quoted 15 lines · Stephen Smalley
The combining logic for dontaudit rules was wrong, causing a dontaudit A B:C *; rule to be clobbered by a dontaudit A B:C p; rule.
Reported-by Nick Kralevich <[email protected]>Signed-off-by Stephen Smalley <[email protected]>--- libsepol/src/expand.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-)libsepol/src/expand.c +0
-0
libsepol/src/expand.c static int expand_range_trans(expand_state_t * state, · line 1610
Quoted 8 lines
libsepol/src/expand.c +2
-1@@ -1604,7 +1604,8 @@ static int expand_range_trans(expand_state_t * state,static avtab_ptr_t find_avtab_node (sepol_handle_t * handle,avtab_t * avtab,avtab_key_t * key,cond_av_list_t ** cond, -av_extended_perms_t *xperms) +av_extended_perms_t *xperms, +char *alloced) {avtab_ptr_t node;
For robustness, it would be safer to ensure that alloced was always assigned to. This variable may end up unassigned on certain error conditions. It's not a bug today, since the caller always performs a check on the return value prior to using this variable, but it could be a use of an unassigned variable in a future version of this code.
Also, "bool" would be a better type for alloced, rather than using a "char"....
Quoted 52 lines
libsepol/src/expand.c +10
-4avtab_datum_t avdatum;@@ -1658,6 +1659,11 @@ static avtab_ptr_t find_avtab_node(sepol_handle_t * handle, nl->next = *cond; *cond = nl; } +if (alloced) + *alloced =1 ; + }else { +if (alloced) + *alloced =0 ; }return node;@@ -1750,7 +1756,7 @@ static int expand_terule_helper(sepol_handle_t * handle,return EXPAND_RULE_CONFLICT ; } - node =find_avtab_node (handle, avtab, &avkey, cond,NULL ); + node =find_avtab_node (handle, avtab, &avkey, cond,NULL ,NULL );if (!node)return -1 ;if (enabled) {@@ -1790,6 +1796,7 @@ static int expand_avrule_helper(sepol_handle_t * handle,class_perm_node_t *cur;uint32_t spec =0 ;unsigned int i; +char alloced;if (specified &AVRULE_ALLOWED ) { spec =AVTAB_ALLOWED ;@@ -1824,7 +1831,8 @@ static int expand_avrule_helper(sepol_handle_t * handle, avkey.target_class = cur->tclass; avkey.specified = spec; - node =find_avtab_node (handle, avtab, &avkey, cond, extended_perms); + node =find_avtab_node (handle, avtab, &avkey, cond, + extended_perms, &alloced);if (!node)return EXPAND_RULE_ERROR ;if (enabled) {@@ -1850,7 +1858,7 @@ static int expand_avrule_helper(sepol_handle_t * handle,*/ avdatump->data &= cur->data; }else if (specified &AVRULE_DONTAUDIT ) { -if (avdatump->data) +if (!alloced) avdatump->data &= ~cur->data;else avdatump->data = ~cur->data; --2.7.4
Signature · 1 line
Nick Kralevich | Android Security | [email protected] | 650.214.4037
Quoted 31 lines · Nick Kralevich
Quoted 15 lines · Stephen Smalley
The combining logic for dontaudit rules was wrong, causing a dontaudit A B:C *; rule to be clobbered by a dontaudit A B:C p; rule.
Reported-by Nick Kralevich <[email protected]>Signed-off-by Stephen Smalley <[email protected]>--- libsepol/src/expand.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-)libsepol/src/expand.c +0
-0libsepol/src/expand.c static int expand_range_trans(expand_state_t * state, · line 1610
Quoted 8 lines
libsepol/src/expand.c +2
-1@@ -1604,7 +1604,8 @@ static int expand_range_trans(expand_state_t * state,static avtab_ptr_t find_avtab_node (sepol_handle_t * handle,avtab_t * avtab,avtab_key_t * key,cond_av_list_t ** cond, -av_extended_perms_t *xperms) +av_extended_perms_t *xperms, +char *alloced) {avtab_ptr_t node;For robustness, it would be safer to ensure that alloced was always assigned to. This variable may end up unassigned on certain error conditions. It's not a bug today, since the caller always performs a check on the return value prior to using this variable, but it could be a use of an unassigned variable in a future version of this code.
Quoted 1 line
Also, "bool" would be a better type for alloced, rather than using a "char"....
Originally did that but it broke - requires a separate patch to rename the field named "bool" in include/sepol/policydb/conditional.h and all users. There was no bool type in C when we first wrote the security server code (for Flask).