libsepol - cond_expr mapping and package num_sections bugs
PATCH 1/1 libsepol - cond_expr mapping and package num_sections bugs
This patch fixes a bug where a boolean expression node which was an operation was trying to map the boolean value to the base policy during linking and getting an index from the previous map ( 0 - 1). The solution is to not map the boolean value if the expr_type is not COND_BOOL.
It also fixes a bug where a base module getting written after being read (during linking) would end up with a num_sections of 4 since num_sections was initialized during the read and then incremented during write. The solution is to move the num_sections incrementing to the functions where the sections are actually set, so that it is already correct when entering package_write.
Joshua Brindle
src/link.c +3 -0
@@ -1010,6 +1010,9 @@ static int copy_cond_list(cond_node_t *l goto cleanup;/* go back through and remap the expression */ for (cur_expr = new_node->expr; cur_expr !=NULL ; cur_expr = cur_expr->next) { +/* expression nodes don't have a bool value of 0 - don't map them */ +if (cur_expr->expr_type !=COND_BOOL ) +continue ;assert (module->map[SYM_BOOLS ][cur_expr->bool -1 ] !=0 ); cur_expr->bool = module->map[SYM_BOOLS ][cur_expr->bool -1 ]; }
src/module.c +3 -7
@@ -78,7 +78,7 @@ static int module_package_init(sepol_modif (sepol_policydb_create (&p->policy))return -1 ; - p->num_sections =0 ; + p->num_sections =1 ; p->version =1 ;return 0 ; }
@@ -131,6 +131,7 @@ int sepol_module_package_set_file_contexmemcpy (p->file_contexts, data, len); } p->file_contexts_len = len; + p->num_sections++;return 0 ; }
@@ -536,9 +537,7 @@ int sepol_module_package_write(sepol_modif (policydb_write (&p->policy->p, &polfile))return -1 ; len = polfile.len; -if (polfile.len) - p->num_sections++; -else +if (!polfile.len)return -1 ; }else {
@@ -546,9 +545,6 @@ int sepol_module_package_write(sepol_modreturn -1 ; } -if (p->file_contexts) - p->num_sections++; - buf[0 ] =cpu_to_le32 (SEPOL_MODULE_PACKAGE_MAGIC ); buf[1 ] =cpu_to_le32 (p->version); buf[2 ] =cpu_to_le32 (p->num_sections);
Quoted 11 lines · Joshua Brindle
This patch fixes a bug where a boolean expression node which was an operation was trying to map the boolean value to the base policy during linking and getting an index from the previous map ( 0 - 1). The solution is to not map the boolean value if the
expr_typeis notCOND_BOOL.It also fixes a bug where a base module getting written after being read (during linking) would end up with a
num_sectionsof 4 sincenum_sectionswas initialized during the read and then incremented during write. The solution is to move thenum_sectionsincrementing to the functions where the sections are actually set, so that it is already correct when enteringpackage_write.
src/module.c +2 -1
@@ -78,7 +78,7 @@ static int module_package_init(sepol_modif (sepol_policydb_create (&p->policy))return -1 ; - p->num_sections =0 ; + p->num_sections =1 ; p->version =1 ;return 0 ; }
@@ -131,6 +131,7 @@ int sepol_module_package_set_file_contexmemcpy (p->file_contexts, data, len); } p->file_contexts_len = len; + p->num_sections++;return 0 ; }
This seems prone to similar bugs later. Suppose I call sepol_module_package_set_file_contexts multiple times; each time it frees the old one and replaces it, so I shouldn't be incrementing the number of sections here.
Possibly num_sections shouldn't be part of the state of the struct at all; it should just be a local variable. In the read case, we pull it from the image/file and use it locally (just need to pass it back from read_offsets to the caller). For write, we always start with a count of 1 and increment it if we have a p->file_contexts.
@@ -536,9 +537,7 @@ int sepol_module_package_write(sepol_mod
if (policydb_write(&p->policy->p, &polfile))
return -1;
len = polfile.len;
- if (polfile.len)
- p->num_sections++;
- else
+ if (!polfile.len)
return -1;
} else {
@@ -546,9 +545,6 @@ int sepol_module_package_write(sepol_mod
return -1;
}
- if (p->file_contexts)
- p->num_sections++;
-
buf[0] = cpu_to_le32(SEPOL_MODULE_PACKAGE_MAGIC); buf[1] = cpu_to_le32(p->version);
buf[2] = cpu_to_le32(p->num_sections);
Signature · 8 lines
Stephen Smalley National Security Agency -- This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.
Quoted 10 lines · Stephen Smalley
This seems prone to similar bugs later. Suppose I call
sepol_module_package_set_file_contextsmultiple times; each time it frees the old one and replaces it, so I shouldn't be incrementing the number of sections here.Possibly
num_sectionsshouldn't be part of the state of the struct at all; it should just be a local variable. In the read case, we pull it from the image/file and use it locally (just need to pass it back fromread_offsetsto the caller). For write, we always start with a count of 1 and increment it if we have ap->file_contexts.
How about the patch below instead?
Index: libsepol/include/sepol/policydb/module.h
===================================================================
RCS file: /nfshome/pal/CVS/selinux-usr/libsepol/include/sepol/policydb/module.h,v retrieving revision 1.4
include/sepol/policydb/module.h +0 -1
@@ -33,7 +33,6 @@struct sepol_module_package {sepol_policydb_t *policy;uint32_t version; -uint32_t num_sections;char *file_contexts;size_t file_contexts_len; };
Index: libsepol/src/link.c
===================================================================
RCS file: /nfshome/pal/CVS/selinux-usr/libsepol/src/link.c,v retrieving revision 1.13
src/link.c +3 -0
@@ -1010,6 +1010,9 @@ static int copy_cond_list(cond_node_t *l goto cleanup;/* go back through and remap the expression */ for (cur_expr = new_node->expr; cur_expr !=NULL ; cur_expr = cur_expr->next) { +/* expression nodes don't have a bool value of 0 - don't map them */ +if (cur_expr->expr_type !=COND_BOOL ) +continue ;assert (module->map[SYM_BOOLS ][cur_expr->bool -1 ] !=0 ); cur_expr->bool = module->map[SYM_BOOLS ][cur_expr->bool -1 ]; }
Index: libsepol/src/module.c
===================================================================
RCS file: /nfshome/pal/CVS/selinux-usr/libsepol/src/module.c,v retrieving revision 1.14
src/module.c +25 -25
150 lines not on this page · raw message
Signature · 8 lines
Stephen Smalley National Security Agency -- This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.
Quoted 7 lines · Stephen Smalley
Quoted 5 lines · Stephen Smalley
This seems prone to similar bugs later. Suppose I call
sepol_module_package_set_file_contextsmultiple times; each time it frees the old one and replaces it, so I shouldn't be incrementing the number of sections here.
Quoted 8 lines
Quoted 5 lines
Possibly
num_sectionsshouldn't be part of the state of the struct at all; it should just be a local variable. In the read case, we pull it from the image/file and use it locally (just need to pass it back fromread_offsetsto the caller). For write, we always start with a count of 1 and increment it if we have ap->file_contexts.How about the patch below instead?
ah! this is very close to a patch I was getting ready to send you, looks good to me, should solve the problem.
Btw, also getting ready to send a patch that adds 2 new sections to the package format so we'll see how flexible this really is then.
--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.
Quoted 2 lines · Joshua Brindle
ah! this is very close to a patch I was getting ready to send you, looks good to me, should solve the problem.
Included in libsepol 1.11.10 along with Ivan's changes.
Quoted 3 lines
Btw, also getting ready to send a patch that adds 2 new sections to the package format so we'll see how flexible this really is then.
Signature · 8 lines
Stephen Smalley National Security Agency -- This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.