policydb.h: use AVTAB macros to avoid duplications
PATCH 1/2 policydb.h: use AVTAB macros to avoid duplications
From: William Roberts <[email protected]>
Rather than having multiple copies of the AVTAB and AVRULE defines, consolidate them.
This makes it clear that AVRULE to AVTAB conversion no longer need to occur.
--- libsepol/include/sepol/policydb/policydb.h | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-)
libsepol/include/sepol/policydb/policydb.h +11 -11
@@ -258,20 +258,20 @@ typedef struct av_extended_perms { typedef struct avrule { /* these typedefs are almost exactly the same as those in avtab.h - they are * here because of the need to include neverallow and dontaudit messages */ -#define AVRULE_ALLOWED 0x0001 -#define AVRULE_AUDITALLOW 0x0002 -#define AVRULE_AUDITDENY 0x0004 +#define AVRULE_ALLOWED AVTAB_ALLOWED +#define AVRULE_AUDITALLOW AVTAB_AUDITALLOW +#define AVRULE_AUDITDENY AVTAB_AUDITDENY #define AVRULE_DONTAUDIT 0x0008 -#define AVRULE_NEVERALLOW 0x0080 +#define AVRULE_NEVERALLOW AVTAB_NEVERALLOW #define AVRULE_AV (AVRULE_ALLOWED | AVRULE_AUDITALLOW | AVRULE_AUDITDENY | AVRULE_DONTAUDIT | AVRULE_NEVERALLOW) -#define AVRULE_TRANSITION 0x0010 -#define AVRULE_MEMBER 0x0020 -#define AVRULE_CHANGE 0x0040 +#define AVRULE_TRANSITION AVTAB_TRANSITION +#define AVRULE_MEMBER AVTAB_MEMBER +#define AVRULE_CHANGE AVTAB_CHANGE #define AVRULE_TYPE (AVRULE_TRANSITION | AVRULE_MEMBER | AVRULE_CHANGE) -#define AVRULE_XPERMS_ALLOWED 0x0100 -#define AVRULE_XPERMS_AUDITALLOW 0x0200 -#define AVRULE_XPERMS_DONTAUDIT 0x0400 -#define AVRULE_XPERMS_NEVERALLOW 0x0800 +#define AVRULE_XPERMS_ALLOWED AVTAB_XPERMS_ALLOWED +#define AVRULE_XPERMS_AUDITALLOW AVTAB_XPERMS_AUDITALLOW +#define AVRULE_XPERMS_DONTAUDIT AVTAB_XPERMS_DONTAUDIT +#define AVRULE_XPERMS_NEVERALLOW AVTAB_XPERMS_NEVERALLOW #define AVRULE_XPERMS (AVRULE_XPERMS_ALLOWED | AVRULE_XPERMS_AUDITALLOW | \ AVRULE_XPERMS_DONTAUDIT | AVRULE_XPERMS_NEVERALLOW) uint32_t specified;
Signature · 1 line
2.7.4
PATCH 2/2 expand_avrule_helper: cleanup
From: William Roberts <[email protected]>
General clean up for expand_avrule_helper:
1. Stop converting AVRULE specification to AVTAB specification, they are the same.
2. Clean up if/else logic, collapse with a switch.
3. Move xperms allocation and manipulation to its own helper.
4. Stop checking handle for NULL, handle must never be NULL. Previous code and current code use ERR(), which depends on handle.
5. Only write avkey for values that change.
--- libsepol/src/expand.c | 124 ++++++++++++++++++++++++-------------------------- 1 file changed, 59 insertions(+), 65 deletions(-)
libsepol/src/expand.c +59 -65
@@ -1781,6 +1781,41 @@ static int expand_terule_helper(sepol_handle_t * handle,return EXPAND_RULE_SUCCESS ; } +/* 0 for success -1 indicates failure */ +static int allocate_xperms (sepol_handle_t * handle,avtab_datum_t * avdatump, +av_extended_perms_t * extended_perms) +{ +unsigned int i; + +avtab_extended_perms_t *xperms = avdatump->xperms; +if (!xperms) { + xperms = (avtab_extended_perms_t *) +calloc (1 ,sizeof (avtab_extended_perms_t)); +if (!xperms) { +ERR (handle,"Out of memory!" ); +return -1 ; + } + avdatump->xperms = xperms; + } + +switch (extended_perms->specified) { +case AVRULE_XPERMS_IOCTLFUNCTION : + xperms->specified =AVTAB_XPERMS_IOCTLFUNCTION ; +break ; +case AVRULE_XPERMS_IOCTLDRIVER : + xperms->specified =AVTAB_XPERMS_IOCTLDRIVER ; +break ; +default : +return -1 ; + } + + xperms->driver = extended_perms->driver; +for (i =0 ; i <ARRAY_SIZE (xperms->perms); i++) + xperms->perms[i] |= extended_perms->perms[i]; + +return 0 ; +} + static int expand_avrule_helper(sepol_handle_t * handle, uint32_t specified, cond_av_list_t ** cond,
@@ -1790,44 +1825,20 @@ static int expand_avrule_helper(sepol_handle_t * handle, {avtab_key_t avkey;avtab_datum_t *avdatump; -avtab_extended_perms_t *xperms;avtab_ptr_t node;class_perm_node_t *cur; -uint32_t spec =0 ; -unsigned int i; -if (specified &AVRULE_ALLOWED ) { - spec =AVTAB_ALLOWED ; - }else if (specified &AVRULE_AUDITALLOW ) { - spec =AVTAB_AUDITALLOW ; - }else if (specified &AVRULE_AUDITDENY ) { - spec =AVTAB_AUDITDENY ; - }else if (specified &AVRULE_DONTAUDIT ) { -if (handle && handle->disable_dontaudit) -return EXPAND_RULE_SUCCESS ; - spec =AVTAB_AUDITDENY ; - }else if (specified &AVRULE_NEVERALLOW ) { - spec =AVTAB_NEVERALLOW ; - }else if (specified &AVRULE_XPERMS_ALLOWED ) { - spec =AVTAB_XPERMS_ALLOWED ; - }else if (specified &AVRULE_XPERMS_AUDITALLOW ) { - spec =AVTAB_XPERMS_AUDITALLOW ; - }else if (specified &AVRULE_XPERMS_DONTAUDIT ) { -if (handle && handle->disable_dontaudit) +/* bail early if dontaudit's are disabled and it's a dontaudit rule */ +if ((specified & (AVRULE_DONTAUDIT |AVRULE_XPERMS_DONTAUDIT )) && handle->disable_dontaudit)return EXPAND_RULE_SUCCESS ; - spec =AVTAB_XPERMS_DONTAUDIT ; - }else if (specified &AVRULE_XPERMS_NEVERALLOW ) { - spec =AVTAB_XPERMS_NEVERALLOW ; - }else { -assert (0 );/* unreachable */ - } + + avkey.source_type = stype +1 ; + avkey.target_type = ttype +1 ; + avkey.specified = specified; cur = perms;while (cur) { - avkey.source_type = stype +1 ; - avkey.target_type = ttype +1 ; avkey.target_class = cur->tclass; - avkey.specified = spec; node =find_avtab_node (handle, avtab, &avkey, cond, extended_perms);if (!node)
@@ -1839,13 +1850,15 @@ static int expand_avrule_helper(sepol_handle_t * handle, } avdatump = &node->datum; -if (specified &AVRULE_ALLOWED ) { - avdatump->data |= cur->data; - }else if (specified &AVRULE_AUDITALLOW ) { +switch (specified) { +case AVRULE_ALLOWED : +case AVRULE_AUDITALLOW : +case AVRULE_NEVERALLOW : avdatump->data |= cur->data; - }else if (specified &AVRULE_NEVERALLOW ) { - avdatump->data |= cur->data; - }else if (specified &AVRULE_AUDITDENY ) { +break ; +case AVRULE_DONTAUDIT : + cur->data = ~cur->data; +case AVRULE_AUDITDENY :/* Since a '0' in an auditdeny mask represents * a permission we do NOT want to audit * (dontaudit), we use the '&' operand to
@@ -1854,35 +1867,16 @@ static int expand_avrule_helper(sepol_handle_t * handle,* auditallow cases). */ avdatump->data &= cur->data; - }else if (specified &AVRULE_DONTAUDIT ) { - avdatump->data &= ~cur->data; - }else if (specified &AVRULE_XPERMS ) { - xperms = avdatump->xperms; -if (!xperms) { - xperms = (avtab_extended_perms_t *) -calloc (1 ,sizeof (avtab_extended_perms_t)); -if (!xperms) { -ERR (handle,"Out of memory!" ); -return -1 ; - } - avdatump->xperms = xperms; - } - -switch (extended_perms->specified) { -case AVRULE_XPERMS_IOCTLFUNCTION : - xperms->specified =AVTAB_XPERMS_IOCTLFUNCTION ; -break ; -case AVRULE_XPERMS_IOCTLDRIVER : - xperms->specified =AVTAB_XPERMS_IOCTLDRIVER ; -break ; -default : -return -1 ; - } - - xperms->driver = extended_perms->driver; -for (i =0 ; i <ARRAY_SIZE (xperms->perms); i++) - xperms->perms[i] |= extended_perms->perms[i]; - }else { +break ; +case AVRULE_XPERMS_ALLOWED : +case AVRULE_XPERMS_AUDITALLOW : +case AVRULE_XPERMS_DONTAUDIT : +case AVRULE_XPERMS_NEVERALLOW : +if (allocate_xperms (handle, avdatump, extended_perms)) +return EXPAND_RULE_ERROR ; +break ; +default : +ERR (handle,"Unkown specification: %x\n" , specified);assert (0 );/* should never occur */ }
Signature · 1 line
2.7.4
Re: [PATCH 2/2] expand_avrule_helper: cleanup
sediff reports no delta between policies built on master and these 2 patches.
Quoted 189 lines · On Wed, Nov 16, 2016 at 11:12 AM, <[email protected]> wrote:
From: William Roberts <[email protected]>
General clean up for
expand_avrule_helper:
1. Stop converting AVRULE specification to AVTAB specification, they are the same.
2. Clean up if/else logic, collapse with a switch.
3. Move xperms allocation and manipulation to its own helper.
4. Stop checking handle for NULL, handle must never be NULL. Previous code and current code useERR(), which depends on handle.
5. Only write avkey for values that change.Signed-off-by William Roberts <[email protected]>--- libsepol/src/expand.c | 124 ++++++++++++++++++++++++-------------------------- 1 file changed, 59 insertions(+), 65 deletions(-)libsepol/src/expand.c +59
-66@@ -1781,6 +1781,41 @@ static int expand_terule_helper(sepol_handle_t * handle,return EXPAND_RULE_SUCCESS ; } +/* 0 for success -1 indicates failure */ +static int allocate_xperms (sepol_handle_t * handle,avtab_datum_t * avdatump, +av_extended_perms_t * extended_perms) +{ +unsigned int i; + +avtab_extended_perms_t *xperms = avdatump->xperms; +if (!xperms) { + xperms = (avtab_extended_perms_t *) +calloc (1 ,sizeof (avtab_extended_perms_t)); +if (!xperms) { +ERR (handle,"Out of memory!" ); +return -1 ; + } + avdatump->xperms = xperms; + } + +switch (extended_perms->specified) { +case AVRULE_XPERMS_IOCTLFUNCTION : + xperms->specified =AVTAB_XPERMS_IOCTLFUNCTION ; +break ; +case AVRULE_XPERMS_IOCTLDRIVER : + xperms->specified =AVTAB_XPERMS_IOCTLDRIVER ; +break ; +default : +return -1 ; + } + + xperms->driver = extended_perms->driver; +for (i =0 ; i <ARRAY_SIZE (xperms->perms); i++) + xperms->perms[i] |= extended_perms->perms[i]; + +return 0 ; +} + static int expand_avrule_helper(sepol_handle_t * handle, uint32_t specified, cond_av_list_t ** cond,@@ -1790,44 +1825,20 @@ static int expand_avrule_helper(sepol_handle_t * handle, {avtab_key_t avkey;avtab_datum_t *avdatump; -avtab_extended_perms_t *xperms;avtab_ptr_t node;class_perm_node_t *cur; -uint32_t spec =0 ; -unsigned int i; -if (specified &AVRULE_ALLOWED ) { - spec =AVTAB_ALLOWED ; - }else if (specified &AVRULE_AUDITALLOW ) { - spec =AVTAB_AUDITALLOW ; - }else if (specified &AVRULE_AUDITDENY ) { - spec =AVTAB_AUDITDENY ; - }else if (specified &AVRULE_DONTAUDIT ) { -if (handle && handle->disable_dontaudit) -return EXPAND_RULE_SUCCESS ; - spec =AVTAB_AUDITDENY ; - }else if (specified &AVRULE_NEVERALLOW ) { - spec =AVTAB_NEVERALLOW ; - }else if (specified &AVRULE_XPERMS_ALLOWED ) { - spec =AVTAB_XPERMS_ALLOWED ; - }else if (specified &AVRULE_XPERMS_AUDITALLOW ) { - spec =AVTAB_XPERMS_AUDITALLOW ; - }else if (specified &AVRULE_XPERMS_DONTAUDIT ) { -if (handle && handle->disable_dontaudit) +/* bail early if dontaudit's are disabled and it's a dontaudit rule */ +if ((specified & (AVRULE_DONTAUDIT |AVRULE_XPERMS_DONTAUDIT )) && handle->disable_dontaudit)return EXPAND_RULE_SUCCESS ; - spec =AVTAB_XPERMS_DONTAUDIT ; - }else if (specified &AVRULE_XPERMS_NEVERALLOW ) { - spec =AVTAB_XPERMS_NEVERALLOW ; - }else { -assert (0 );/* unreachable */ - } + + avkey.source_type = stype +1 ; + avkey.target_type = ttype +1 ; + avkey.specified = specified; cur = perms;while (cur) { - avkey.source_type = stype +1 ; - avkey.target_type = ttype +1 ; avkey.target_class = cur->tclass; - avkey.specified = spec; node =find_avtab_node (handle, avtab, &avkey, cond, extended_perms);if (!node)@@ -1839,13 +1850,15 @@ static int expand_avrule_helper(sepol_handle_t * handle, } avdatump = &node->datum; -if (specified &AVRULE_ALLOWED ) { - avdatump->data |= cur->data; - }else if (specified &AVRULE_AUDITALLOW ) { +switch (specified) { +case AVRULE_ALLOWED : +case AVRULE_AUDITALLOW : +case AVRULE_NEVERALLOW : avdatump->data |= cur->data; - }else if (specified &AVRULE_NEVERALLOW ) { - avdatump->data |= cur->data; - }else if (specified &AVRULE_AUDITDENY ) { +break ; +case AVRULE_DONTAUDIT : + cur->data = ~cur->data; +case AVRULE_AUDITDENY :/* Since a '0' in an auditdeny mask represents * a permission we do NOT want to audit * (dontaudit), we use the '&' operand to @@ -1854,35 +1867,16 @@ static int expand_avrule_helper(sepol_handle_t * handle,* auditallow cases). */ avdatump->data &= cur->data; - }else if (specified &AVRULE_DONTAUDIT ) { - avdatump->data &= ~cur->data; - }else if (specified &AVRULE_XPERMS ) { - xperms = avdatump->xperms; -if (!xperms) { - xperms = (avtab_extended_perms_t *) -calloc (1 ,sizeof (avtab_extended_perms_t)); -if (!xperms) { -ERR (handle,"Out of memory!" ); -return -1 ; - } - avdatump->xperms = xperms; - } - -switch (extended_perms->specified) { -case AVRULE_XPERMS_IOCTLFUNCTION : - xperms->specified =AVTAB_XPERMS_IOCTLFUNCTION ; -break ; -case AVRULE_XPERMS_IOCTLDRIVER : - xperms->specified =AVTAB_XPERMS_IOCTLDRIVER ; -break ; -default : -return -1 ; - } - - xperms->driver = extended_perms->driver; -for (i =0 ; i <ARRAY_SIZE (xperms->perms); i++) - xperms->perms[i] |= extended_perms->perms[i]; - }else { +break ; +case AVRULE_XPERMS_ALLOWED : +case AVRULE_XPERMS_AUDITALLOW : +case AVRULE_XPERMS_DONTAUDIT : +case AVRULE_XPERMS_NEVERALLOW : +if (allocate_xperms (handle, avdatump, extended_perms)) +return EXPAND_RULE_ERROR ; +break ; +default : +ERR (handle,"Unkown specification: %x\n" , specified);assert (0 );/* should never occur */ } --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].
Signature · 3 lines
Respectfully, William C Roberts
Re: [PATCH 2/2] expand_avrule_helper: cleanup
Quoted 1 line · William Roberts
sediff reports no delta between policies built on master and these 2 patches.
Not possible. checkpolicy segfaults with these patches. Probably didn't rebuild it after rebuilding libsepol. Anyway, you can just use cmp to compare the policies here; they should be byte-for-byte identical.
Quoted 194 lines · On Wed, Nov 16, 2016 at 11:12 AM, <[email protected]> wrote:
Quoted 189 lines
From: William Roberts <[email protected]>
General clean up for
expand_avrule_helper:
1. Stop converting AVRULE specification to AVTAB specification, they are the same.
2. Clean up if/else logic, collapse with a switch.
3. Move xperms allocation and manipulation to its own helper.
4. Stop checking handle for NULL, handle must never be NULL. Previous code and current code useERR(), which depends on handle.
5. Only write avkey for values that change.Signed-off-by William Roberts <[email protected]>--- libsepol/src/expand.c | 124 ++++++++++++++++++++++++-------------------------- 1 file changed, 59 insertions(+), 65 deletions(-)libsepol/src/expand.c +59
-66@@ -1781,6 +1781,41 @@ static int expand_terule_helper(sepol_handle_t * handle,return EXPAND_RULE_SUCCESS ; } +/* 0 for success -1 indicates failure */ +static int allocate_xperms (sepol_handle_t * handle,avtab_datum_t * avdatump, +av_extended_perms_t * extended_perms) +{ +unsigned int i; + +avtab_extended_perms_t *xperms = avdatump->xperms; +if (!xperms) { + xperms = (avtab_extended_perms_t *) +calloc (1 ,sizeof (avtab_extended_perms_t)); +if (!xperms) { +ERR (handle,"Out of memory!" ); +return -1 ; + } + avdatump->xperms = xperms; + } + +switch (extended_perms->specified) { +case AVRULE_XPERMS_IOCTLFUNCTION : + xperms->specified =AVTAB_XPERMS_IOCTLFUNCTION ; +break ; +case AVRULE_XPERMS_IOCTLDRIVER : + xperms->specified =AVTAB_XPERMS_IOCTLDRIVER ; +break ; +default : +return -1 ; + } + + xperms->driver = extended_perms->driver; +for (i =0 ; i <ARRAY_SIZE (xperms->perms); i++) + xperms->perms[i] |= extended_perms->perms[i]; + +return 0 ; +} + static int expand_avrule_helper(sepol_handle_t * handle, uint32_t specified, cond_av_list_t ** cond,@@ -1790,44 +1825,20 @@ static int expand_avrule_helper(sepol_handle_t * handle, {avtab_key_t avkey;avtab_datum_t *avdatump; -avtab_extended_perms_t *xperms;avtab_ptr_t node;class_perm_node_t *cur; -uint32_t spec =0 ; -unsigned int i; -if (specified &AVRULE_ALLOWED ) { - spec =AVTAB_ALLOWED ; - }else if (specified &AVRULE_AUDITALLOW ) { - spec =AVTAB_AUDITALLOW ; - }else if (specified &AVRULE_AUDITDENY ) { - spec =AVTAB_AUDITDENY ; - }else if (specified &AVRULE_DONTAUDIT ) { -if (handle && handle->disable_dontaudit) -return EXPAND_RULE_SUCCESS ; - spec =AVTAB_AUDITDENY ; - }else if (specified &AVRULE_NEVERALLOW ) { - spec =AVTAB_NEVERALLOW ; - }else if (specified &AVRULE_XPERMS_ALLOWED ) { - spec =AVTAB_XPERMS_ALLOWED ; - }else if (specified &AVRULE_XPERMS_AUDITALLOW ) { - spec =AVTAB_XPERMS_AUDITALLOW ; - }else if (specified &AVRULE_XPERMS_DONTAUDIT ) { -if (handle && handle->disable_dontaudit) +/* bail early if dontaudit's are disabled and it's a dontaudit rule */ +if ((specified & (AVRULE_DONTAUDIT |AVRULE_XPERMS_DONTAUDIT )) && handle->disable_dontaudit)return EXPAND_RULE_SUCCESS ; - spec =AVTAB_XPERMS_DONTAUDIT ; - }else if (specified &AVRULE_XPERMS_NEVERALLOW ) { - spec =AVTAB_XPERMS_NEVERALLOW ; - }else { -assert (0 );/* unreachable */ - } + + avkey.source_type = stype +1 ; + avkey.target_type = ttype +1 ; + avkey.specified = specified; cur = perms;while (cur) { - avkey.source_type = stype +1 ; - avkey.target_type = ttype +1 ; avkey.target_class = cur->tclass; - avkey.specified = spec; node =find_avtab_node (handle, avtab, &avkey, cond, extended_perms);if (!node)@@ -1839,13 +1850,15 @@ static int expand_avrule_helper(sepol_handle_t * handle, } avdatump = &node->datum; -if (specified &AVRULE_ALLOWED ) { - avdatump->data |= cur->data; - }else if (specified &AVRULE_AUDITALLOW ) { +switch (specified) { +case AVRULE_ALLOWED : +case AVRULE_AUDITALLOW : +case AVRULE_NEVERALLOW : avdatump->data |= cur->data; - }else if (specified &AVRULE_NEVERALLOW ) { - avdatump->data |= cur->data; - }else if (specified &AVRULE_AUDITDENY ) { +break ; +case AVRULE_DONTAUDIT : + cur->data = ~cur->data; +case AVRULE_AUDITDENY :/* Since a '0' in an auditdeny mask represents * a permission we do NOT want to audit * (dontaudit), we use the '&' operand to @@ -1854,35 +1867,16 @@ static int expand_avrule_helper(sepol_handle_t * handle,* auditallow cases). */ avdatump->data &= cur->data; - }else if (specified &AVRULE_DONTAUDIT ) { - avdatump->data &= ~cur->data; - }else if (specified &AVRULE_XPERMS ) { - xperms = avdatump->xperms; -if (!xperms) { - xperms = (avtab_extended_perms_t *) -calloc (1 ,sizeof (avtab_extended_perms_t)); -if (!xperms) { -ERR (handle,"Out of memory!" ); -return -1 ; - } - avdatump->xperms = xperms; - } - -switch (extended_perms->specified) { -case AVRULE_XPERMS_IOCTLFUNCTION : - xperms->specified =AVTAB_XPERMS_IOCTLFUNCTION ; -break ; -case AVRULE_XPERMS_IOCTLDRIVER : - xperms->specified =AVTAB_XPERMS_IOCTLDRIVER ; -break ; -default : -return -1 ; - } - - xperms->driver = extended_perms->driver; -for (i =0 ; i <ARRAY_SIZE (xperms->perms); i++) - xperms->perms[i] |= extended_perms->perms[i]; - }else { +break ; +case AVRULE_XPERMS_ALLOWED : +case AVRULE_XPERMS_AUDITALLOW : +case AVRULE_XPERMS_DONTAUDIT : +case AVRULE_XPERMS_NEVERALLOW : +if (allocate_xperms (handle, avdatump, extended_perms)) +return EXPAND_RULE_ERROR ; +break ; +default : +ERR (handle,"Unkown specification: %x\n" , specified);assert (0 );/* should never occur */ } --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].
Re: [PATCH 2/2] expand_avrule_helper: cleanup
Quoted 7 lines · Stephen Smalley
Quoted 1 line · William Roberts
sediff reports no delta between policies built on master and these 2 patches.
Not possible. checkpolicy segfaults with these patches. Probably didn't rebuild it after rebuilding libsepol. Anyway, you can just use cmp to compare the policies here; they should be byte-for-byte identical.
Crazy, I only tested these on Android.
Quoted 196 lines
Quoted 195 lines · On Wed, Nov 16, 2016 at 11:12 AM, <[email protected]> wrote:
Quoted 189 lines
From: William Roberts <[email protected]>
General clean up for
expand_avrule_helper:
1. Stop converting AVRULE specification to AVTAB specification, they are the same.
2. Clean up if/else logic, collapse with a switch.
3. Move xperms allocation and manipulation to its own helper.
4. Stop checking handle for NULL, handle must never be NULL. Previous code and current code useERR(), which depends on handle.
5. Only write avkey for values that change.Signed-off-by William Roberts <[email protected]>--- libsepol/src/expand.c | 124 ++++++++++++++++++++++++-------------------------- 1 file changed, 59 insertions(+), 65 deletions(-)libsepol/src/expand.c +59
-66@@ -1781,6 +1781,41 @@ static int expand_terule_helper(sepol_handle_t * handle,return EXPAND_RULE_SUCCESS ; } +/* 0 for success -1 indicates failure */ +static int allocate_xperms (sepol_handle_t * handle,avtab_datum_t * avdatump, +av_extended_perms_t * extended_perms) +{ +unsigned int i; + +avtab_extended_perms_t *xperms = avdatump->xperms; +if (!xperms) { + xperms = (avtab_extended_perms_t *) +calloc (1 ,sizeof (avtab_extended_perms_t)); +if (!xperms) { +ERR (handle,"Out of memory!" ); +return -1 ; + } + avdatump->xperms = xperms; + } + +switch (extended_perms->specified) { +case AVRULE_XPERMS_IOCTLFUNCTION : + xperms->specified =AVTAB_XPERMS_IOCTLFUNCTION ; +break ; +case AVRULE_XPERMS_IOCTLDRIVER : + xperms->specified =AVTAB_XPERMS_IOCTLDRIVER ; +break ; +default : +return -1 ; + } + + xperms->driver = extended_perms->driver; +for (i =0 ; i <ARRAY_SIZE (xperms->perms); i++) + xperms->perms[i] |= extended_perms->perms[i]; + +return 0 ; +} + static int expand_avrule_helper(sepol_handle_t * handle, uint32_t specified, cond_av_list_t ** cond,@@ -1790,44 +1825,20 @@ static int expand_avrule_helper(sepol_handle_t * handle, {avtab_key_t avkey;avtab_datum_t *avdatump; -avtab_extended_perms_t *xperms;avtab_ptr_t node;class_perm_node_t *cur; -uint32_t spec =0 ; -unsigned int i; -if (specified &AVRULE_ALLOWED ) { - spec =AVTAB_ALLOWED ; - }else if (specified &AVRULE_AUDITALLOW ) { - spec =AVTAB_AUDITALLOW ; - }else if (specified &AVRULE_AUDITDENY ) { - spec =AVTAB_AUDITDENY ; - }else if (specified &AVRULE_DONTAUDIT ) { -if (handle && handle->disable_dontaudit) -return EXPAND_RULE_SUCCESS ; - spec =AVTAB_AUDITDENY ; - }else if (specified &AVRULE_NEVERALLOW ) { - spec =AVTAB_NEVERALLOW ; - }else if (specified &AVRULE_XPERMS_ALLOWED ) { - spec =AVTAB_XPERMS_ALLOWED ; - }else if (specified &AVRULE_XPERMS_AUDITALLOW ) { - spec =AVTAB_XPERMS_AUDITALLOW ; - }else if (specified &AVRULE_XPERMS_DONTAUDIT ) { -if (handle && handle->disable_dontaudit) +/* bail early if dontaudit's are disabled and it's a dontaudit rule */ +if ((specified & (AVRULE_DONTAUDIT |AVRULE_XPERMS_DONTAUDIT )) && handle->disable_dontaudit)return EXPAND_RULE_SUCCESS ; - spec =AVTAB_XPERMS_DONTAUDIT ; - }else if (specified &AVRULE_XPERMS_NEVERALLOW ) { - spec =AVTAB_XPERMS_NEVERALLOW ; - }else { -assert (0 );/* unreachable */ - } + + avkey.source_type = stype +1 ; + avkey.target_type = ttype +1 ; + avkey.specified = specified; cur = perms;while (cur) { - avkey.source_type = stype +1 ; - avkey.target_type = ttype +1 ; avkey.target_class = cur->tclass; - avkey.specified = spec; node =find_avtab_node (handle, avtab, &avkey, cond, extended_perms);if (!node)@@ -1839,13 +1850,15 @@ static int expand_avrule_helper(sepol_handle_t * handle, } avdatump = &node->datum; -if (specified &AVRULE_ALLOWED ) { - avdatump->data |= cur->data; - }else if (specified &AVRULE_AUDITALLOW ) { +switch (specified) { +case AVRULE_ALLOWED : +case AVRULE_AUDITALLOW : +case AVRULE_NEVERALLOW : avdatump->data |= cur->data; - }else if (specified &AVRULE_NEVERALLOW ) { - avdatump->data |= cur->data; - }else if (specified &AVRULE_AUDITDENY ) { +break ; +case AVRULE_DONTAUDIT : + cur->data = ~cur->data; +case AVRULE_AUDITDENY :/* Since a '0' in an auditdeny mask represents * a permission we do NOT want to audit * (dontaudit), we use the '&' operand to @@ -1854,35 +1867,16 @@ static int expand_avrule_helper(sepol_handle_t * handle,* auditallow cases). */ avdatump->data &= cur->data; - }else if (specified &AVRULE_DONTAUDIT ) { - avdatump->data &= ~cur->data; - }else if (specified &AVRULE_XPERMS ) { - xperms = avdatump->xperms; -if (!xperms) { - xperms = (avtab_extended_perms_t *) -calloc (1 ,sizeof (avtab_extended_perms_t)); -if (!xperms) { -ERR (handle,"Out of memory!" ); -return -1 ; - } - avdatump->xperms = xperms; - } - -switch (extended_perms->specified) { -case AVRULE_XPERMS_IOCTLFUNCTION : - xperms->specified =AVTAB_XPERMS_IOCTLFUNCTION ; -break ; -case AVRULE_XPERMS_IOCTLDRIVER : - xperms->specified =AVTAB_XPERMS_IOCTLDRIVER ; -break ; -default : -return -1 ; - } - - xperms->driver = extended_perms->driver; -for (i =0 ; i <ARRAY_SIZE (xperms->perms); i++) - xperms->perms[i] |= extended_perms->perms[i]; - }else { +break ; +case AVRULE_XPERMS_ALLOWED : +case AVRULE_XPERMS_AUDITALLOW : +case AVRULE_XPERMS_DONTAUDIT : +case AVRULE_XPERMS_NEVERALLOW : +if (allocate_xperms (handle, avdatump, extended_perms)) +return EXPAND_RULE_ERROR ; +break ; +default : +ERR (handle,"Unkown specification: %x\n" , specified);assert (0 );/* should never occur */ } --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].
Signature · 3 lines
Respectfully, William C Roberts
Re: [PATCH 2/2] expand_avrule_helper: cleanup
Quoted 10 lines · William Roberts
Quoted 7 lines · Stephen Smalley
Quoted 1 line · William Roberts
sediff reports no delta between policies built on master and these 2 patches.
Not possible. checkpolicy segfaults with these patches. Probably didn't rebuild it after rebuilding libsepol. Anyway, you can just use cmp to compare the policies here; they should be byte-for-byte identical.
Crazy, I only tested these on Android.
Again, not possible. checkpolicy calls expand_module() with NULL handle, and therefore segfaults with this change. You could not have been invoking a checkpolicy built with this change and had it work (also, even aside from that, you would have ended up failing later because you weren't mapping the DONTAUDIT values).
Quoted 200 lines
Quoted 197 lines
Quoted 196 lines · On Wed, Nov 16, 2016 at 11:12 AM, <[email protected]> wrote:
Quoted 189 lines
From: William Roberts <[email protected]>
General clean up for
expand_avrule_helper:
1. Stop converting AVRULE specification to AVTAB specification, they are the same.
2. Clean up if/else logic, collapse with a switch.
3. Move xperms allocation and manipulation to its own helper.
4. Stop checking handle for NULL, handle must never be NULL. Previous code and current code useERR(), which depends on handle.
5. Only write avkey for values that change.Signed-off-by William Roberts <[email protected]>--- libsepol/src/expand.c | 124 ++++++++++++++++++++++++-------------------------- 1 file changed, 59 insertions(+), 65 deletions(-)libsepol/src/expand.c +59
-66@@ -1781,6 +1781,41 @@ static int expand_terule_helper(sepol_handle_t * handle,return EXPAND_RULE_SUCCESS ; } +/* 0 for success -1 indicates failure */ +static int allocate_xperms (sepol_handle_t * handle,avtab_datum_t * avdatump, +av_extended_perms_t * extended_perms) +{ +unsigned int i; + +avtab_extended_perms_t *xperms = avdatump->xperms; +if (!xperms) { + xperms = (avtab_extended_perms_t *) +calloc (1 ,sizeof (avtab_extended_perms_t)); +if (!xperms) { +ERR (handle,"Out of memory!" ); +return -1 ; + } + avdatump->xperms = xperms; + } + +switch (extended_perms->specified) { +case AVRULE_XPERMS_IOCTLFUNCTION : + xperms->specified =AVTAB_XPERMS_IOCTLFUNCTION ; +break ; +case AVRULE_XPERMS_IOCTLDRIVER : + xperms->specified =AVTAB_XPERMS_IOCTLDRIVER ; +break ; +default : +return -1 ; + } + + xperms->driver = extended_perms->driver; +for (i =0 ; i <ARRAY_SIZE (xperms->perms); i++) + xperms->perms[i] |= extended_perms->perms[i]; + +return 0 ; +} + static int expand_avrule_helper(sepol_handle_t * handle, uint32_t specified, cond_av_list_t ** cond,@@ -1790,44 +1825,20 @@ static int expand_avrule_helper(sepol_handle_t * handle, {avtab_key_t avkey;avtab_datum_t *avdatump; -avtab_extended_perms_t *xperms;avtab_ptr_t node;class_perm_node_t *cur; -uint32_t spec =0 ; -unsigned int i; -if (specified &AVRULE_ALLOWED ) { - spec =AVTAB_ALLOWED ; - }else if (specified &AVRULE_AUDITALLOW ) { - spec =AVTAB_AUDITALLOW ; - }else if (specified &AVRULE_AUDITDENY ) { - spec =AVTAB_AUDITDENY ; - }else if (specified &AVRULE_DONTAUDIT ) { -if (handle && handle->disable_dontaudit) -return EXPAND_RULE_SUCCESS ; - spec =AVTAB_AUDITDENY ; - }else if (specified &AVRULE_NEVERALLOW ) { - spec =AVTAB_NEVERALLOW ; - }else if (specified &AVRULE_XPERMS_ALLOWED ) { - spec =AVTAB_XPERMS_ALLOWED ; - }else if (specified &AVRULE_XPERMS_AUDITALLOW ) { - spec =AVTAB_XPERMS_AUDITALLOW ; - }else if (specified &AVRULE_XPERMS_DONTAUDIT ) { -if (handle && handle->disable_dontaudit) +/* bail early if dontaudit's are disabled and it's a dontaudit rule */ +if ((specified & (AVRULE_DONTAUDIT |AVRULE_XPERMS_DONTAUDIT )) && handle->disable_dontaudit)return EXPAND_RULE_SUCCESS ; - spec =AVTAB_XPERMS_DONTAUDIT ; - }else if (specified &AVRULE_XPERMS_NEVERALLOW ) { - spec =AVTAB_XPERMS_NEVERALLOW ; - }else { -assert (0 );/* unreachable */ - } + + avkey.source_type = stype +1 ; + avkey.target_type = ttype +1 ; + avkey.specified = specified; cur = perms;while (cur) { - avkey.source_type = stype +1 ; - avkey.target_type = ttype +1 ; avkey.target_class = cur->tclass; - avkey.specified = spec; node =find_avtab_node (handle, avtab, &avkey, cond, extended_perms);if (!node)@@ -1839,13 +1850,15 @@ static int expand_avrule_helper(sepol_handle_t * handle, } avdatump = &node->datum; -if (specified &AVRULE_ALLOWED ) { - avdatump->data |= cur->data; - }else if (specified &AVRULE_AUDITALLOW ) { +switch (specified) { +case AVRULE_ALLOWED : +case AVRULE_AUDITALLOW : +case AVRULE_NEVERALLOW : avdatump->data |= cur->data; - }else if (specified &AVRULE_NEVERALLOW ) { - avdatump->data |= cur->data; - }else if (specified &AVRULE_AUDITDENY ) { +break ; +case AVRULE_DONTAUDIT : + cur->data = ~cur->data; +case AVRULE_AUDITDENY :/* Since a '0' in an auditdeny mask represents * a permission we do NOT want to audit * (dontaudit), we use the '&' operand to @@ -1854,35 +1867,16 @@ static int expand_avrule_helper(sepol_handle_t * handle,* auditallow cases). */ avdatump->data &= cur->data; - }else if (specified &AVRULE_DONTAUDIT ) { - avdatump->data &= ~cur->data; - }else if (specified &AVRULE_XPERMS ) { - xperms = avdatump->xperms; -if (!xperms) { - xperms = (avtab_extended_perms_t *) -calloc (1 ,sizeof (avtab_extended_perms_t)); -if (!xperms) { -ERR (handle,"Out of memory!" ); -return -1 ; - } - avdatump->xperms = xperms; - } - -switch (extended_perms->specified) { -case AVRULE_XPERMS_IOCTLFUNCTION : - xperms->specified =AVTAB_XPERMS_IOCTLFUNCTION ; -break ; -case AVRULE_XPERMS_IOCTLDRIVER : - xperms->specified =AVTAB_XPERMS_IOCTLDRIVER ; -break ; -default : -return -1 ; - } - - xperms->driver = extended_perms->driver; -for (i =0 ; i <ARRAY_SIZE (xperms->perms); i++) - xperms->perms[i] |= extended_perms->perms[i]; - }else { +break ; +case AVRULE_XPERMS_ALLOWED : +case AVRULE_XPERMS_AUDITALLOW : +case AVRULE_XPERMS_DONTAUDIT : +case AVRULE_XPERMS_NEVERALLOW : +if (allocate_xperms (handle, avdatump, extended_perms)) +return EXPAND_RULE_ERROR ; +break ; +default : +ERR (handle,"Unkown specification: %x\n" , specified);assert (0 );/* should never occur */ } --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].
Re: [PATCH 2/2] expand_avrule_helper: cleanup
Quoted 10 lines · Stephen Smalley
Quoted 9 lines · William Roberts
Quoted 7 lines · Stephen Smalley
Quoted 1 line · William Roberts
sediff reports no delta between policies built on master and these 2 patches.
Not possible. checkpolicy segfaults with these patches. Probably didn't rebuild it after rebuilding libsepol. Anyway, you can just use cmp to compare the policies here; they should be byte-for-byte identical.
Quoted 8 lines
Quoted 1 line
Crazy, I only tested these on Android.
Again, not possible. checkpolicy calls
expand_module()with NULL handle, and therefore segfaults with this change. You could not have been invoking a checkpolicy built with this change and had it work (also, even aside from that, you would have ended up failing later because you weren't mapping the DONTAUDIT values).
Hmm I'm using a test script the always builds everything, it didn't segfault, but
I did get different policy hashes, which is why I ran sediff. Either way it doesn't
matter, the patch was wrong.
v2 (currently unsent):
59b2538cf2789ba8f7496644ff8fef5c bullhead.policy.old 93548cfc4de715432d2118353ed1d56a marlin.policy.old 59b2538cf2789ba8f7496644ff8fef5c bullhead.policy.new 93548cfc4de715432d2118353ed1d56a marlin.policy.new
That looks better.
<snip>
Re: [PATCH 2/2] expand_avrule_helper: cleanup
In reply to [email protected]Quoted 9 lines · On 11/16/2016 02:12 PM, [email protected] wrote:
From: William Roberts <[email protected]>
General clean up for
expand_avrule_helper:
1. Stop converting AVRULE specification to AVTAB specification, they are the same.
2. Clean up if/else logic, collapse with a switch.
3. Move xperms allocation and manipulation to its own helper.
4. Stop checking handle for NULL, handle must never be NULL. Previous code and current code useERR(), which depends on handle.
ERR() -> msg_write() has a fallback to sepol_compat_handle if the handle is NULL. checkpolicy, checkmodule and libsepol/tests/*.c call expand_module() with a NULL handle.
Quoted 31 lines
5. Only write avkey for values that change.
Signed-off-by William Roberts <[email protected]>--- libsepol/src/expand.c | 124 ++++++++++++++++++++++++-------------------------- 1 file changed, 59 insertions(+), 65 deletions(-)libsepol/src/expand.c +16
-0@@ -1781,6 +1781,41 @@ static int expand_terule_helper(sepol_handle_t * handle,return EXPAND_RULE_SUCCESS ; } +/* 0 for success -1 indicates failure */ +static int allocate_xperms (sepol_handle_t * handle,avtab_datum_t * avdatump, +av_extended_perms_t * extended_perms) +{ +unsigned int i; + +avtab_extended_perms_t *xperms = avdatump->xperms; +if (!xperms) { + xperms = (avtab_extended_perms_t *) +calloc (1 ,sizeof (avtab_extended_perms_t)); +if (!xperms) { +ERR (handle,"Out of memory!" ); +return -1 ; + } + avdatump->xperms = xperms; + }
libsepol/src/expand.c static int expand_terule_helper(sepol_handle_t * handle, · line 1809
Quoted 10 lines
libsepol/src/expand.c +10
-0+ +switch (extended_perms->specified) { +case AVRULE_XPERMS_IOCTLFUNCTION : + xperms->specified =AVTAB_XPERMS_IOCTLFUNCTION ; +break ; +case AVRULE_XPERMS_IOCTLDRIVER : + xperms->specified =AVTAB_XPERMS_IOCTLDRIVER ; +break ; +default : +return -1 ;
Not new to your code but should we free(xperms) here if we allocated it earlier?
Quoted 43 lines
libsepol/src/expand.c +11
-21+ } + + xperms->driver = extended_perms->driver; +for (i =0 ; i <ARRAY_SIZE (xperms->perms); i++) + xperms->perms[i] |= extended_perms->perms[i]; + +return 0 ; +} + static int expand_avrule_helper(sepol_handle_t * handle, uint32_t specified, cond_av_list_t ** cond,@@ -1790,44 +1825,20 @@ static int expand_avrule_helper(sepol_handle_t * handle, {avtab_key_t avkey;avtab_datum_t *avdatump; -avtab_extended_perms_t *xperms;avtab_ptr_t node;class_perm_node_t *cur; -uint32_t spec =0 ; -unsigned int i; -if (specified &AVRULE_ALLOWED ) { - spec =AVTAB_ALLOWED ; - }else if (specified &AVRULE_AUDITALLOW ) { - spec =AVTAB_AUDITALLOW ; - }else if (specified &AVRULE_AUDITDENY ) { - spec =AVTAB_AUDITDENY ; - }else if (specified &AVRULE_DONTAUDIT ) { -if (handle && handle->disable_dontaudit) -return EXPAND_RULE_SUCCESS ; - spec =AVTAB_AUDITDENY ; - }else if (specified &AVRULE_NEVERALLOW ) { - spec =AVTAB_NEVERALLOW ; - }else if (specified &AVRULE_XPERMS_ALLOWED ) { - spec =AVTAB_XPERMS_ALLOWED ; - }else if (specified &AVRULE_XPERMS_AUDITALLOW ) { - spec =AVTAB_XPERMS_AUDITALLOW ; - }else if (specified &AVRULE_XPERMS_DONTAUDIT ) { -if (handle && handle->disable_dontaudit) +/* bail early if dontaudit's are disabled and it's a dontaudit rule */ +if ((specified & (AVRULE_DONTAUDIT |AVRULE_XPERMS_DONTAUDIT )) && handle->disable_dontaudit)return EXPAND_RULE_SUCCESS ;
libsepol/src/expand.c static int expand_avrule_helper(sepol_handle_t * handle, · line 1837
Quoted 10 lines
libsepol/src/expand.c +4
-6- spec =AVTAB_XPERMS_DONTAUDIT ; - }else if (specified &AVRULE_XPERMS_NEVERALLOW ) { - spec =AVTAB_XPERMS_NEVERALLOW ; - }else { -assert (0 );/* unreachable */ - } + + avkey.source_type = stype +1 ; + avkey.target_type = ttype +1 ; + avkey.specified = specified;
What happened to mapping AVRULE_DONTAUDIT to AVTAB_AUDITDENY?
Quoted 18 lines
libsepol/src/expand.c +1
-6cur = perms;while (cur) { - avkey.source_type = stype +1 ; - avkey.target_type = ttype +1 ; avkey.target_class = cur->tclass; - avkey.specified = spec; node =find_avtab_node (handle, avtab, &avkey, cond, extended_perms);if (!node)@@ -1839,13 +1850,15 @@ static int expand_avrule_helper(sepol_handle_t * handle, } avdatump = &node->datum; -if (specified &AVRULE_ALLOWED ) { - avdatump->data |= cur->data; - }else if (specified &AVRULE_AUDITALLOW ) { +switch (specified) {
libsepol/src/expand.c static int expand_avrule_helper(sepol_handle_t * handle, · line 1860
Quoted 10 lines
libsepol/src/expand.c +6
-3+case AVRULE_ALLOWED : +case AVRULE_AUDITALLOW : +case AVRULE_NEVERALLOW : avdatump->data |= cur->data; - }else if (specified &AVRULE_NEVERALLOW ) { - avdatump->data |= cur->data; - }else if (specified &AVRULE_AUDITDENY ) { +break ; +case AVRULE_DONTAUDIT : + cur->data = ~cur->data;
Missing break; ?
Quoted 37 lines
libsepol/src/expand.c +1
-29+case AVRULE_AUDITDENY :/* Since a '0' in an auditdeny mask represents * a permission we do NOT want to audit * (dontaudit), we use the '&' operand to @@ -1854,35 +1867,16 @@ static int expand_avrule_helper(sepol_handle_t * handle,* auditallow cases). */ avdatump->data &= cur->data; - }else if (specified &AVRULE_DONTAUDIT ) { - avdatump->data &= ~cur->data; - }else if (specified &AVRULE_XPERMS ) { - xperms = avdatump->xperms; -if (!xperms) { - xperms = (avtab_extended_perms_t *) -calloc (1 ,sizeof (avtab_extended_perms_t)); -if (!xperms) { -ERR (handle,"Out of memory!" ); -return -1 ; - } - avdatump->xperms = xperms; - } - -switch (extended_perms->specified) { -case AVRULE_XPERMS_IOCTLFUNCTION : - xperms->specified =AVTAB_XPERMS_IOCTLFUNCTION ; -break ; -case AVRULE_XPERMS_IOCTLDRIVER : - xperms->specified =AVTAB_XPERMS_IOCTLDRIVER ; -break ; -default : -return -1 ; - } - - xperms->driver = extended_perms->driver; -for (i =0 ; i <ARRAY_SIZE (xperms->perms); i++) - xperms->perms[i] |= extended_perms->perms[i]; - }else {
libsepol/src/expand.c static int expand_avrule_helper(sepol_handle_t * handle, · line 1879
Quoted 10 lines
libsepol/src/expand.c +10
-0+break ; +case AVRULE_XPERMS_ALLOWED : +case AVRULE_XPERMS_AUDITALLOW : +case AVRULE_XPERMS_DONTAUDIT : +case AVRULE_XPERMS_NEVERALLOW : +if (allocate_xperms (handle, avdatump, extended_perms)) +return EXPAND_RULE_ERROR ; +break ; +default : +ERR (handle,"Unkown specification: %x\n" , specified);
Spelling
Quoted 4 lines
libsepol/src/expand.c +0
-0assert (0 );/* should never occur */ }
Re: [PATCH 2/2] expand_avrule_helper: cleanup
Quoted 138 lines · Stephen Smalley
Quoted 9 lines · On 11/16/2016 02:12 PM, [email protected] wrote:
From: William Roberts <[email protected]>
General clean up for
expand_avrule_helper:
1. Stop converting AVRULE specification to AVTAB specification, they are the same.
2. Clean up if/else logic, collapse with a switch.
3. Move xperms allocation and manipulation to its own helper.
4. Stop checking handle for NULL, handle must never be NULL. Previous code and current code useERR(), which depends on handle.
ERR()->msg_write()has a fallback tosepol_compat_handleif the handle is NULL. checkpolicy, checkmodule and libsepol/tests/*.c callexpand_module()with a NULL handle.Quoted 31 lines
5. Only write avkey for values that change.
Signed-off-by William Roberts <[email protected]>--- libsepol/src/expand.c | 124 ++++++++++++++++++++++++-------------------------- 1 file changed, 59 insertions(+), 65 deletions(-)libsepol/src/expand.c +16
-0@@ -1781,6 +1781,41 @@ static int expand_terule_helper(sepol_handle_t * handle,return EXPAND_RULE_SUCCESS ; } +/* 0 for success -1 indicates failure */ +static int allocate_xperms (sepol_handle_t * handle,avtab_datum_t * avdatump, +av_extended_perms_t * extended_perms) +{ +unsigned int i; + +avtab_extended_perms_t *xperms = avdatump->xperms; +if (!xperms) { + xperms = (avtab_extended_perms_t *) +calloc (1 ,sizeof (avtab_extended_perms_t)); +if (!xperms) { +ERR (handle,"Out of memory!" ); +return -1 ; + } + avdatump->xperms = xperms; + }libsepol/src/expand.c static int expand_terule_helper(sepol_handle_t * handle, · line 1809
Quoted 10 lines
libsepol/src/expand.c +10
-0+ +switch (extended_perms->specified) { +case AVRULE_XPERMS_IOCTLFUNCTION : + xperms->specified =AVTAB_XPERMS_IOCTLFUNCTION ; +break ; +case AVRULE_XPERMS_IOCTLDRIVER : + xperms->specified =AVTAB_XPERMS_IOCTLDRIVER ; +break ; +default : +return -1 ;Not new to your code but should we free(xperms) here if we allocated it earlier?
Quoted 43 lines
libsepol/src/expand.c +11
-21+ } + + xperms->driver = extended_perms->driver; +for (i =0 ; i <ARRAY_SIZE (xperms->perms); i++) + xperms->perms[i] |= extended_perms->perms[i]; + +return 0 ; +} + static int expand_avrule_helper(sepol_handle_t * handle, uint32_t specified, cond_av_list_t ** cond,@@ -1790,44 +1825,20 @@ static int expand_avrule_helper(sepol_handle_t * handle, {avtab_key_t avkey;avtab_datum_t *avdatump; -avtab_extended_perms_t *xperms;avtab_ptr_t node;class_perm_node_t *cur; -uint32_t spec =0 ; -unsigned int i; -if (specified &AVRULE_ALLOWED ) { - spec =AVTAB_ALLOWED ; - }else if (specified &AVRULE_AUDITALLOW ) { - spec =AVTAB_AUDITALLOW ; - }else if (specified &AVRULE_AUDITDENY ) { - spec =AVTAB_AUDITDENY ; - }else if (specified &AVRULE_DONTAUDIT ) { -if (handle && handle->disable_dontaudit) -return EXPAND_RULE_SUCCESS ; - spec =AVTAB_AUDITDENY ; - }else if (specified &AVRULE_NEVERALLOW ) { - spec =AVTAB_NEVERALLOW ; - }else if (specified &AVRULE_XPERMS_ALLOWED ) { - spec =AVTAB_XPERMS_ALLOWED ; - }else if (specified &AVRULE_XPERMS_AUDITALLOW ) { - spec =AVTAB_XPERMS_AUDITALLOW ; - }else if (specified &AVRULE_XPERMS_DONTAUDIT ) { -if (handle && handle->disable_dontaudit) +/* bail early if dontaudit's are disabled and it's a dontaudit rule */ +if ((specified & (AVRULE_DONTAUDIT |AVRULE_XPERMS_DONTAUDIT )) && handle->disable_dontaudit)return EXPAND_RULE_SUCCESS ;libsepol/src/expand.c static int expand_avrule_helper(sepol_handle_t * handle, · line 1837
Quoted 10 lines
libsepol/src/expand.c +4
-6- spec =AVTAB_XPERMS_DONTAUDIT ; - }else if (specified &AVRULE_XPERMS_NEVERALLOW ) { - spec =AVTAB_XPERMS_NEVERALLOW ; - }else { -assert (0 );/* unreachable */ - } + + avkey.source_type = stype +1 ; + avkey.target_type = ttype +1 ; + avkey.specified = specified;What happened to mapping
AVRULE_DONTAUDITtoAVTAB_AUDITDENY?Quoted 22 lines
libsepol/src/expand.c +4
-6cur = perms;while (cur) { - avkey.source_type = stype +1 ; - avkey.target_type = ttype +1 ; avkey.target_class = cur->tclass; - avkey.specified = spec; node =find_avtab_node (handle, avtab, &avkey, cond, extended_perms);if (!node)@@ -1839,13 +1850,15 @@ static int expand_avrule_helper(sepol_handle_t * handle, } avdatump = &node->datum; -if (specified &AVRULE_ALLOWED ) { - avdatump->data |= cur->data; - }else if (specified &AVRULE_AUDITALLOW ) { +switch (specified) { +case AVRULE_ALLOWED : +case AVRULE_AUDITALLOW : +case AVRULE_NEVERALLOW : avdatump->data |= cur->data;
Quoted 8 lines
libsepol/src/expand.c static int expand_avrule_helper(sepol_handle_t * handle, · line 1860
Quoted 6 lines
libsepol/src/expand.c +3
-3- }else if (specified &AVRULE_NEVERALLOW ) { - avdatump->data |= cur->data; - }else if (specified &AVRULE_AUDITDENY ) { +break ; +case AVRULE_DONTAUDIT : + cur->data = ~cur->data;Missing break; ?
Also should be &=, not = .
Quoted 56 lines
Quoted 38 lines
libsepol/src/expand.c +1
-29+case AVRULE_AUDITDENY :/* Since a '0' in an auditdeny mask represents * a permission we do NOT want to audit * (dontaudit), we use the '&' operand to @@ -1854,35 +1867,16 @@ static int expand_avrule_helper(sepol_handle_t * handle,* auditallow cases). */ avdatump->data &= cur->data; - }else if (specified &AVRULE_DONTAUDIT ) { - avdatump->data &= ~cur->data; - }else if (specified &AVRULE_XPERMS ) { - xperms = avdatump->xperms; -if (!xperms) { - xperms = (avtab_extended_perms_t *) -calloc (1 ,sizeof (avtab_extended_perms_t)); -if (!xperms) { -ERR (handle,"Out of memory!" ); -return -1 ; - } - avdatump->xperms = xperms; - } - -switch (extended_perms->specified) { -case AVRULE_XPERMS_IOCTLFUNCTION : - xperms->specified =AVTAB_XPERMS_IOCTLFUNCTION ; -break ; -case AVRULE_XPERMS_IOCTLDRIVER : - xperms->specified =AVTAB_XPERMS_IOCTLDRIVER ; -break ; -default : -return -1 ; - } - - xperms->driver = extended_perms->driver; -for (i =0 ; i <ARRAY_SIZE (xperms->perms); i++) - xperms->perms[i] |= extended_perms->perms[i]; - }else {libsepol/src/expand.c static int expand_avrule_helper(sepol_handle_t * handle, · line 1879
Quoted 10 lines
libsepol/src/expand.c +10
-0+break ; +case AVRULE_XPERMS_ALLOWED : +case AVRULE_XPERMS_AUDITALLOW : +case AVRULE_XPERMS_DONTAUDIT : +case AVRULE_XPERMS_NEVERALLOW : +if (allocate_xperms (handle, avdatump, extended_perms)) +return EXPAND_RULE_ERROR ; +break ; +default : +ERR (handle,"Unkown specification: %x\n" , specified);Spelling
Quoted 4 lines
libsepol/src/expand.c +0
-0assert (0 );/* should never occur */ }
Re: [PATCH 2/2] expand_avrule_helper: cleanup
Quoted 148 lines · Stephen Smalley
Quoted 138 lines · Stephen Smalley
Quoted 9 lines · On 11/16/2016 02:12 PM, [email protected] wrote:
From: William Roberts <[email protected]>
General clean up for
expand_avrule_helper:
1. Stop converting AVRULE specification to AVTAB specification, they are the same.
2. Clean up if/else logic, collapse with a switch.
3. Move xperms allocation and manipulation to its own helper.
4. Stop checking handle for NULL, handle must never be NULL. Previous code and current code useERR(), which depends on handle.
ERR()->msg_write()has a fallback tosepol_compat_handleif the handle is NULL. checkpolicy, checkmodule and libsepol/tests/*.c callexpand_module()with a NULL handle.Quoted 31 lines
5. Only write avkey for values that change.
Signed-off-by William Roberts <[email protected]>--- libsepol/src/expand.c | 124 ++++++++++++++++++++++++-------------------------- 1 file changed, 59 insertions(+), 65 deletions(-)libsepol/src/expand.c +16
-0@@ -1781,6 +1781,41 @@ static int expand_terule_helper(sepol_handle_t * handle,return EXPAND_RULE_SUCCESS ; } +/* 0 for success -1 indicates failure */ +static int allocate_xperms (sepol_handle_t * handle,avtab_datum_t * avdatump, +av_extended_perms_t * extended_perms) +{ +unsigned int i; + +avtab_extended_perms_t *xperms = avdatump->xperms; +if (!xperms) { + xperms = (avtab_extended_perms_t *) +calloc (1 ,sizeof (avtab_extended_perms_t)); +if (!xperms) { +ERR (handle,"Out of memory!" ); +return -1 ; + } + avdatump->xperms = xperms; + }libsepol/src/expand.c static int expand_terule_helper(sepol_handle_t * handle, · line 1809
Quoted 10 lines
libsepol/src/expand.c +10
-0+ +switch (extended_perms->specified) { +case AVRULE_XPERMS_IOCTLFUNCTION : + xperms->specified =AVTAB_XPERMS_IOCTLFUNCTION ; +break ; +case AVRULE_XPERMS_IOCTLDRIVER : + xperms->specified =AVTAB_XPERMS_IOCTLDRIVER ; +break ; +default : +return -1 ;Not new to your code but should we free(xperms) here if we allocated it earlier?
Quoted 43 lines
libsepol/src/expand.c +11
-21+ } + + xperms->driver = extended_perms->driver; +for (i =0 ; i <ARRAY_SIZE (xperms->perms); i++) + xperms->perms[i] |= extended_perms->perms[i]; + +return 0 ; +} + static int expand_avrule_helper(sepol_handle_t * handle, uint32_t specified, cond_av_list_t ** cond,@@ -1790,44 +1825,20 @@ static int expand_avrule_helper(sepol_handle_t * handle, {avtab_key_t avkey;avtab_datum_t *avdatump; -avtab_extended_perms_t *xperms;avtab_ptr_t node;class_perm_node_t *cur; -uint32_t spec =0 ; -unsigned int i; -if (specified &AVRULE_ALLOWED ) { - spec =AVTAB_ALLOWED ; - }else if (specified &AVRULE_AUDITALLOW ) { - spec =AVTAB_AUDITALLOW ; - }else if (specified &AVRULE_AUDITDENY ) { - spec =AVTAB_AUDITDENY ; - }else if (specified &AVRULE_DONTAUDIT ) { -if (handle && handle->disable_dontaudit) -return EXPAND_RULE_SUCCESS ; - spec =AVTAB_AUDITDENY ; - }else if (specified &AVRULE_NEVERALLOW ) { - spec =AVTAB_NEVERALLOW ; - }else if (specified &AVRULE_XPERMS_ALLOWED ) { - spec =AVTAB_XPERMS_ALLOWED ; - }else if (specified &AVRULE_XPERMS_AUDITALLOW ) { - spec =AVTAB_XPERMS_AUDITALLOW ; - }else if (specified &AVRULE_XPERMS_DONTAUDIT ) { -if (handle && handle->disable_dontaudit) +/* bail early if dontaudit's are disabled and it's a dontaudit rule */ +if ((specified & (AVRULE_DONTAUDIT |AVRULE_XPERMS_DONTAUDIT )) && handle->disable_dontaudit)return EXPAND_RULE_SUCCESS ;libsepol/src/expand.c static int expand_avrule_helper(sepol_handle_t * handle, · line 1837
Quoted 10 lines
libsepol/src/expand.c +4
-6- spec =AVTAB_XPERMS_DONTAUDIT ; - }else if (specified &AVRULE_XPERMS_NEVERALLOW ) { - spec =AVTAB_XPERMS_NEVERALLOW ; - }else { -assert (0 );/* unreachable */ - } + + avkey.source_type = stype +1 ; + avkey.target_type = ttype +1 ; + avkey.specified = specified;What happened to mapping
AVRULE_DONTAUDITtoAVTAB_AUDITDENY?Quoted 22 lines
libsepol/src/expand.c +4
-6cur = perms;while (cur) { - avkey.source_type = stype +1 ; - avkey.target_type = ttype +1 ; avkey.target_class = cur->tclass; - avkey.specified = spec; node =find_avtab_node (handle, avtab, &avkey, cond, extended_perms);if (!node)@@ -1839,13 +1850,15 @@ static int expand_avrule_helper(sepol_handle_t * handle, } avdatump = &node->datum; -if (specified &AVRULE_ALLOWED ) { - avdatump->data |= cur->data; - }else if (specified &AVRULE_AUDITALLOW ) { +switch (specified) { +case AVRULE_ALLOWED : +case AVRULE_AUDITALLOW : +case AVRULE_NEVERALLOW : avdatump->data |= cur->data;Quoted 8 lines
libsepol/src/expand.c static int expand_avrule_helper(sepol_handle_t * handle, · line 1860
Quoted 6 lines
libsepol/src/expand.c +3
-3- }else if (specified &AVRULE_NEVERALLOW ) { - avdatump->data |= cur->data; - }else if (specified &AVRULE_AUDITDENY ) { +break ; +case AVRULE_DONTAUDIT : + cur->data = ~cur->data;Missing break; ?
Quoted 1 line
Also should be &=, not = .
I wanted to flip it and let it fall through to the common &= routine, which is why
this isn't assigned back to avdatump->data.
Quoted 63 lines
Quoted 43 lines
Quoted 43 lines
libsepol/src/expand.c +5
-29+case AVRULE_AUDITDENY :/* Since a '0' in an auditdeny mask represents * a permission we do NOT want to audit * (dontaudit), we use the '&' operand to @@ -1854,35 +1867,16 @@ static int expand_avrule_helper(sepol_handle_t * handle,* auditallow cases). */ avdatump->data &= cur->data; - }else if (specified &AVRULE_DONTAUDIT ) { - avdatump->data &= ~cur->data; - }else if (specified &AVRULE_XPERMS ) { - xperms = avdatump->xperms; -if (!xperms) { - xperms = (avtab_extended_perms_t *) -calloc (1 ,sizeof (avtab_extended_perms_t)); -if (!xperms) { -ERR (handle,"Out of memory!" ); -return -1 ; - } - avdatump->xperms = xperms; - } - -switch (extended_perms->specified) { -case AVRULE_XPERMS_IOCTLFUNCTION : - xperms->specified =AVTAB_XPERMS_IOCTLFUNCTION ; -break ; -case AVRULE_XPERMS_IOCTLDRIVER : - xperms->specified =AVTAB_XPERMS_IOCTLDRIVER ; -break ; -default : -return -1 ; - } - - xperms->driver = extended_perms->driver; -for (i =0 ; i <ARRAY_SIZE (xperms->perms); i++) - xperms->perms[i] |= extended_perms->perms[i]; - }else { +break ; +case AVRULE_XPERMS_ALLOWED : +case AVRULE_XPERMS_AUDITALLOW : +case AVRULE_XPERMS_DONTAUDIT :Quoted 14 lines
libsepol/src/expand.c static int expand_avrule_helper(sepol_handle_t * handle, · line 1879
Quoted 6 lines
libsepol/src/expand.c +6
-0+case AVRULE_XPERMS_NEVERALLOW : +if (allocate_xperms (handle, avdatump, extended_perms)) +return EXPAND_RULE_ERROR ; +break ; +default : +ERR (handle,"Unkown specification: %x\n" , specified);Spelling
Quoted 4 lines
libsepol/src/expand.c +0
-0assert (0 );/* should never occur */ }_______________________________________________
Selinux mailing list
[email protected]
To unsubscribe, send email to [email protected].
To get help, send an email containing "help" to [email protected].
Signature · 3 lines
Respectfully, William C Roberts
Re: [PATCH 2/2] expand_avrule_helper: cleanup
Quoted 149 lines · William Roberts
Quoted 148 lines · Stephen Smalley
Quoted 146 lines · Stephen Smalley
Quoted 9 lines · On 11/16/2016 02:12 PM, [email protected] wrote:
From: William Roberts <[email protected]>
General clean up for
expand_avrule_helper:
1. Stop converting AVRULE specification to AVTAB specification, they are the same.
2. Clean up if/else logic, collapse with a switch.
3. Move xperms allocation and manipulation to its own helper.
4. Stop checking handle for NULL, handle must never be NULL. Previous code and current code useERR(), which depends on handle.
ERR()->msg_write()has a fallback tosepol_compat_handleif the handle is NULL. checkpolicy, checkmodule and libsepol/tests/*.c callexpand_module()with a NULL handle.Quoted 31 lines
5. Only write avkey for values that change.
Signed-off-by William Roberts <[email protected]>--- libsepol/src/expand.c | 124 ++++++++++++++++++++++++-------------------------- 1 file changed, 59 insertions(+), 65 deletions(-)libsepol/src/expand.c +16
-0@@ -1781,6 +1781,41 @@ static int expand_terule_helper(sepol_handle_t * handle,return EXPAND_RULE_SUCCESS ; } +/* 0 for success -1 indicates failure */ +static int allocate_xperms (sepol_handle_t * handle,avtab_datum_t * avdatump, +av_extended_perms_t * extended_perms) +{ +unsigned int i; + +avtab_extended_perms_t *xperms = avdatump->xperms; +if (!xperms) { + xperms = (avtab_extended_perms_t *) +calloc (1 ,sizeof (avtab_extended_perms_t)); +if (!xperms) { +ERR (handle,"Out of memory!" ); +return -1 ; + } + avdatump->xperms = xperms; + }libsepol/src/expand.c static int expand_terule_helper(sepol_handle_t * handle, · line 1809
Quoted 10 lines
libsepol/src/expand.c +10
-0+ +switch (extended_perms->specified) { +case AVRULE_XPERMS_IOCTLFUNCTION : + xperms->specified =AVTAB_XPERMS_IOCTLFUNCTION ; +break ; +case AVRULE_XPERMS_IOCTLDRIVER : + xperms->specified =AVTAB_XPERMS_IOCTLDRIVER ; +break ; +default : +return -1 ;Not new to your code but should we free(xperms) here if we allocated it earlier?
Quoted 43 lines
libsepol/src/expand.c +11
-21+ } + + xperms->driver = extended_perms->driver; +for (i =0 ; i <ARRAY_SIZE (xperms->perms); i++) + xperms->perms[i] |= extended_perms->perms[i]; + +return 0 ; +} + static int expand_avrule_helper(sepol_handle_t * handle, uint32_t specified, cond_av_list_t ** cond,@@ -1790,44 +1825,20 @@ static int expand_avrule_helper(sepol_handle_t * handle, {avtab_key_t avkey;avtab_datum_t *avdatump; -avtab_extended_perms_t *xperms;avtab_ptr_t node;class_perm_node_t *cur; -uint32_t spec =0 ; -unsigned int i; -if (specified &AVRULE_ALLOWED ) { - spec =AVTAB_ALLOWED ; - }else if (specified &AVRULE_AUDITALLOW ) { - spec =AVTAB_AUDITALLOW ; - }else if (specified &AVRULE_AUDITDENY ) { - spec =AVTAB_AUDITDENY ; - }else if (specified &AVRULE_DONTAUDIT ) { -if (handle && handle->disable_dontaudit) -return EXPAND_RULE_SUCCESS ; - spec =AVTAB_AUDITDENY ; - }else if (specified &AVRULE_NEVERALLOW ) { - spec =AVTAB_NEVERALLOW ; - }else if (specified &AVRULE_XPERMS_ALLOWED ) { - spec =AVTAB_XPERMS_ALLOWED ; - }else if (specified &AVRULE_XPERMS_AUDITALLOW ) { - spec =AVTAB_XPERMS_AUDITALLOW ; - }else if (specified &AVRULE_XPERMS_DONTAUDIT ) { -if (handle && handle->disable_dontaudit) +/* bail early if dontaudit's are disabled and it's a dontaudit rule */ +if ((specified & (AVRULE_DONTAUDIT |AVRULE_XPERMS_DONTAUDIT )) && handle->disable_dontaudit)return EXPAND_RULE_SUCCESS ;libsepol/src/expand.c static int expand_avrule_helper(sepol_handle_t * handle, · line 1837
Quoted 10 lines
libsepol/src/expand.c +4
-6- spec =AVTAB_XPERMS_DONTAUDIT ; - }else if (specified &AVRULE_XPERMS_NEVERALLOW ) { - spec =AVTAB_XPERMS_NEVERALLOW ; - }else { -assert (0 );/* unreachable */ - } + + avkey.source_type = stype +1 ; + avkey.target_type = ttype +1 ; + avkey.specified = specified;What happened to mapping
AVRULE_DONTAUDITtoAVTAB_AUDITDENY?Quoted 18 lines
libsepol/src/expand.c +1
-6cur = perms;while (cur) { - avkey.source_type = stype +1 ; - avkey.target_type = ttype +1 ; avkey.target_class = cur->tclass; - avkey.specified = spec; node =find_avtab_node (handle, avtab, &avkey, cond, extended_perms);if (!node)@@ -1839,13 +1850,15 @@ static int expand_avrule_helper(sepol_handle_t * handle, } avdatump = &node->datum; -if (specified &AVRULE_ALLOWED ) { - avdatump->data |= cur->data; - }else if (specified &AVRULE_AUDITALLOW ) { +switch (specified) {libsepol/src/expand.c static int expand_avrule_helper(sepol_handle_t * handle, · line 1860
Quoted 10 lines
libsepol/src/expand.c +6
-3+case AVRULE_ALLOWED : +case AVRULE_AUDITALLOW : +case AVRULE_NEVERALLOW : avdatump->data |= cur->data; - }else if (specified &AVRULE_NEVERALLOW ) { - avdatump->data |= cur->data; - }else if (specified &AVRULE_AUDITDENY ) { +break ; +case AVRULE_DONTAUDIT : + cur->data = ~cur->data;Missing break; ?
Quoted 5 lines
Quoted 1 line
Also should be &=, not = .
I wanted to flip it and let it fall through to the common &= routine, which is why
this isn't assigned back toavdatump->data.
I'd rather keep them separate.
Quoted 67 lines
Quoted 64 lines
Quoted 44 lines
Quoted 44 lines
libsepol/src/expand.c +5
-29+case AVRULE_AUDITDENY :/* Since a '0' in an auditdeny mask represents * a permission we do NOT want to audit * (dontaudit), we use the '&' operand to @@ -1854,35 +1867,16 @@ static int expand_avrule_helper(sepol_handle_t * handle,* auditallow cases). */ avdatump->data &= cur->data; - }else if (specified &AVRULE_DONTAUDIT ) { - avdatump->data &= ~cur->data; - }else if (specified &AVRULE_XPERMS ) { - xperms = avdatump->xperms; -if (!xperms) { - xperms = (avtab_extended_perms_t *) -calloc (1 ,sizeof (avtab_extended_perms_t)); -if (!xperms) { -ERR (handle,"Out of memory!" ); -return -1 ; - } - avdatump->xperms = xperms; - } - -switch (extended_perms->specified) { -case AVRULE_XPERMS_IOCTLFUNCTION : - xperms->specified =AVTAB_XPERMS_IOCTLFUNCTION ; -break ; -case AVRULE_XPERMS_IOCTLDRIVER : - xperms->specified =AVTAB_XPERMS_IOCTLDRIVER ; -break ; -default : -return -1 ; - } - - xperms->driver = extended_perms->driver; -for (i =0 ; i <ARRAY_SIZE (xperms->perms); i++) - xperms->perms[i] |= extended_perms->perms[i]; - }else { +break ; +case AVRULE_XPERMS_ALLOWED : +case AVRULE_XPERMS_AUDITALLOW : +case AVRULE_XPERMS_DONTAUDIT :Quoted 14 lines
libsepol/src/expand.c static int expand_avrule_helper(sepol_handle_t * handle, · line 1879
Quoted 6 lines
libsepol/src/expand.c +6
-0+case AVRULE_XPERMS_NEVERALLOW : +if (allocate_xperms (handle, avdatump, extended_perms)) +return EXPAND_RULE_ERROR ; +break ; +default : +ERR (handle,"Unkown specification: %x\n" , specified);Spelling
Quoted 4 lines
libsepol/src/expand.c +0
-0assert (0 );/* should never occur */ }_______________________________________________
Selinux mailing list
[email protected]
To unsubscribe, send email to [email protected].
To get help, send an email containing "help" to [email protected].