libsepol: ebitmap: mark nodes of const ebitmaps const
PATCH 1/9 libsepol: ebitmap: mark nodes of const ebitmaps const
Mark pointers to nodes of const ebitmaps also const. C does not enforce a transitive const-ness, but it clarifies the intent and improves maintainability.
Follow-up of 390ec54d278a
--- libsepol/src/ebitmap.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-)
libsepol/src/ebitmap.c +9 -7
@@ -17,7 +17,8 @@int ebitmap_or (ebitmap_t * dst,const ebitmap_t * e1,const ebitmap_t * e2) { -ebitmap_node_t *n1, *n2, *new, *prev; +const ebitmap_node_t *n1, *n2; +ebitmap_node_t *new, *prev;ebitmap_init (dst);
@@ -154,7 +155,7 @@ int ebitmap_hamming_distance(const ebitmap_t * e1, const ebitmap_t * e2)int ebitmap_cmp (const ebitmap_t * e1,const ebitmap_t * e2) { -ebitmap_node_t *n1, *n2; +const ebitmap_node_t *n1, *n2;if (e1->highbit != e2->highbit)return 0 ;
@@ -175,7 +176,8 @@ int ebitmap_cmp(const ebitmap_t * e1, const ebitmap_t * e2)int ebitmap_cpy (ebitmap_t * dst,const ebitmap_t * src) { -ebitmap_node_t *n, *new, *prev; +const ebitmap_node_t *n; +ebitmap_node_t *new, *prev;ebitmap_init (dst); n = src->node;
@@ -204,7 +206,7 @@ int ebitmap_cpy(ebitmap_t * dst, const ebitmap_t * src)int ebitmap_contains (const ebitmap_t * e1,const ebitmap_t * e2) { -ebitmap_node_t *n1, *n2; +const ebitmap_node_t *n1, *n2;if (e1->highbit < e2->highbit)return 0 ;
@@ -231,8 +233,8 @@ int ebitmap_contains(const ebitmap_t * e1, const ebitmap_t * e2)int ebitmap_match_any (const ebitmap_t *e1,const ebitmap_t *e2) { -ebitmap_node_t *n1 = e1->node; -ebitmap_node_t *n2 = e2->node; +const ebitmap_node_t *n1 = e1->node; +const ebitmap_node_t *n2 = e2->node;while (n1 && n2) {if (n1->startbit < n2->startbit) {
@@ -253,7 +255,7 @@ int ebitmap_match_any(const ebitmap_t *e1, const ebitmap_t *e2)int ebitmap_get_bit (const ebitmap_t * e,unsigned int bit) { -ebitmap_node_t *n; +const ebitmap_node_t *n;if (e->highbit < bit)return 0 ;
Signature · 1 line
2.33.0
PATCH 2/9 libsepol: use correct cast
The function hashtab_insert takes the type hashtab_datum_t (alias void*) as third argument. Do not cast to hashtab_datum_t* alias void**. The casts could be dropped, as explicit casting to void* is unnecessary, but to fit the overall style of this file keep the casts.
expand.c:246:41: error: cast from 'perm_datum_t *' (aka 'struct perm_datum *') to 'hashtab_datum_t *' (aka 'void **') increases required alignment from 4 to 8 [-Werror,-Wcast-align]
ret = hashtab_insert(s->table, new_id, (hashtab_datum_t *) new_perm);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~--- libsepol/src/expand.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
libsepol/src/expand.c +3 -3
@@ -243,7 +243,7 @@ static int perm_copy_callback(hashtab_key_t key, hashtab_datum_t datum, new_perm->s.value = perm->s.value; s->nprim++; - ret =hashtab_insert (s->table, new_id, (hashtab_datum_t *) new_perm); + ret =hashtab_insert (s->table, new_id, (hashtab_datum_t ) new_perm);if (ret) {free (new_id);free (new_perm);
@@ -294,7 +294,7 @@ static int common_copy_callback(hashtab_key_t key, hashtab_datum_t datum, ret =hashtab_insert (state->out->p_commons.table, new_id, - (hashtab_datum_t *) new_common); + (hashtab_datum_t ) new_common);if (ret) {ERR (state->handle,"hashtab overflow" );free (new_common);
@@ -492,7 +492,7 @@ static int class_copy_callback(hashtab_key_t key, hashtab_datum_t datum, ret =hashtab_insert (state->out->p_classes.table, new_id, - (hashtab_datum_t *) new_class); + (hashtab_datum_t ) new_class);if (ret) {ERR (state->handle,"hashtab overflow" );free (new_class);
Signature · 1 line
2.33.0
PATCH 3/9 libsepol: resolve GCC warning about null-dereference
In reply to Christian GöttscheGCC reports a NULL dereference of the return value of stack_peek(). This function explicitly returns NULL in case of 'stack->pos == -1'.
Error out on NULL returned.
module_to_cil.c: In function ‘block_to_cil’:
module_to_cil.c:3357:55: error: potential null pointer dereference [-Werror=null-dereference]
3357 | struct list *alias_list = typealias_lists[decl->decl_id];
| ~~~~^~~~~~~~~
There are more occurrences of unconditionally dereferencing the return value of stack_peek(), but the callers should ensure a valid stack, so just silence this single warning.
--- libsepol/src/module_to_cil.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
libsepol/src/module_to_cil.c +6 -1
@@ -3354,9 +3354,14 @@ static int typealiases_to_cil(int indent, struct policydb *pdb, struct avrule_blchar *type_name;struct list_node *curr;struct avrule_decl *decl =stack_peek (decl_stack); -struct list *alias_list = typealias_lists[decl->decl_id]; +struct list *alias_list;int rc =-1 ; +if (decl ==NULL ) { +return -1 ; + } + + alias_list = typealias_lists[decl->decl_id];if (alias_list ==NULL ) {return 0 ; }
Signature · 1 line
2.33.0
PATCH 4/9 libsepol/cil: silence clang void-pointer-to-enum-cast warning
In reply to Christian GöttscheAdd an intermediate cast to uintptr_t to silence the clang specific warning about casting a void pointer to an enum.
../cil/src/cil_verify.c:1749:28: error: cast to smaller integer type 'enum cil_flavor' from 'void *' [-Werror,-Wvoid-pointer-to-enum-cast]
enum cil_flavor op = (enum cil_flavor)i->data;
^~~~~~~~~~~~~~~~~~~~~~~~Similar to 32f8ed3d6b0b.
--- libsepol/cil/src/cil_verify.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
libsepol/cil/src/cil_verify.c +1 -1
@@ -1746,7 +1746,7 @@ static int __cil_verify_classperms(struct cil_list *classperms, goto exit; } }else { -enum cil_flavor op = (enum cil_flavor )i->data; +enum cil_flavor op = (enum cil_flavor )(uintptr_t )i->data;if (op ==CIL_ALL ) {struct cil_class *mc = cp->class;struct cil_list *perm_list;
Signature · 1 line
2.33.0
PATCH 5/9 checkpolicy: policy_define: cleanup declarations
In reply to Christian GöttscheThe variable curfile is nowhere used.
Static functions do not need to be forward declared if not used before their definition.
The error buffer errormsg can be a simple scoped variable. Also vsnprintf(3) always NUL-terminates the buffer, so the whole length can be passed.
--- checkpolicy/policy_define.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-)
checkpolicy/policy_define.c +2 -8
@@ -67,7 +67,6 @@ extern void yyerror2(const char *fmt, ...);policydb_t *policydbp;queue_t id_queue =0 ;unsigned int pass; -char *curfile =0 ;int mlspol =0 ;extern unsigned long policydb_lineno;
@@ -78,12 +77,6 @@ extern char source_file[PATH_MAX];extern int yywarn (const char *msg);extern int yyerror (const char *msg); -#define ERRORMSG_LEN 255 -static char errormsg[ERRORMSG_LEN +1 ] = {0 }; - -static int id_has_dot (const char *id); -static int parse_security_context (context_struct_t *c); -/* initialize all of the state variables for the scanner/parser */ void init_parser (int pass_number) {
@@ -95,9 +88,10 @@ void init_parser(int pass_number)void yyerror2 (const char *fmt, ...) { +char errormsg[256 ];va_list ap;va_start (ap, fmt); -vsnprintf (errormsg,ERRORMSG_LEN , fmt, ap); +vsnprintf (errormsg,sizeof (errormsg), fmt, ap);yyerror (errormsg);va_end (ap); }
Signature · 1 line
2.33.0
PATCH 6/9 checkpolicy: print reason of fopen failure
In reply to Christian GöttschePrint the reason why opening a source policy file failed, e.g:
checkpolicy: unable to open policy.conf: No such file or directory
--- checkpolicy/parse_util.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
checkpolicy/parse_util.c +1 -1
@@ -36,7 +36,7 @@ int read_source_policy(policydb_t * p, const char *file, const char *progname) { yyin =fopen (file,"r" );if (!yyin) { -fprintf (stderr,"%s: unable to open %s\n" , progname, file); +fprintf (stderr,"%s: unable to open %s: %s\n" , progname, file,strerror (errno));return -1 ; }set_source_file (file);
Signature · 1 line
2.33.0
PATCH 7/9 checkpolicy: update documentation
In reply to Christian GöttscheAdd missing command-line arguments to synopsis and highlight mentions of other tools in man pages.
Add missing space between arguments in help message.
--- checkpolicy/checkmodule.8 | 11 +++++++---- checkpolicy/checkpolicy.8 | 8 +++++--- checkpolicy/checkpolicy.c | 2 +- 3 files changed, 13 insertions(+), 8 deletions(-)
checkpolicy/checkmodule.8 +7 -4
@@ -3,7 +3,7 @@ checkmodule \- SELinux policy module compiler .SH SYNOPSIS .B checkmodule -.I "[\-h] [\-b] [\-C] [\-m] [\-M] [\-U handle_unknown ] [\-V] [\-o output_file] [input_file]" +.I "[\-h] [\-b] [\-c policy_version] [\-C] [\-E] [\-m] [\-M] [\-U handle_unknown] [\-V] [\-o output_file] [input_file]" .SH "DESCRIPTION" This manual page describes the .BR checkmodule
@@ -15,9 +15,12 @@ into a binary representation. It can generate either a base policy module (default) or a non-base policy module (\-m option); typically, you would build a non-base policy module to add to an existing module store that already has a base module provided by the base policy. Use -semodule_package to combine this module with its optional file -contexts to create a policy package, and then use semodule to install -the module package into the module store and load the resulting policy. +.B semodule_package(8) +to combine this module with its optional file +contexts to create a policy package, and then use +.B semodule(8) +to install the module package into the module store and load the resulting +policy. .SH OPTIONS .TP
checkpolicy/checkpolicy.8 +5 -3
@@ -3,7 +3,7 @@ checkpolicy \- SELinux policy compiler .SH SYNOPSIS .B checkpolicy -.I "[\-b[F]] [\-C] [\-d] [\-U handle_unknown (allow,deny,reject)] [\-M] [\-c policyvers] [\-o output_file|\-] [\-S] [\-t target_platform (selinux,xen)] [\-V] [input_file]" +.I "[\-b[F]] [\-C] [\-d] [\-U handle_unknown (allow,deny,reject)] [\-M] [\-c policyvers] [\-o output_file|\-] [\-S] [\-t target_platform (selinux,xen)] [\-O] [\-E] [\-V] [input_file]" .br .SH "DESCRIPTION" This manual page describes the
@@ -13,8 +13,10 @@ command. .B checkpolicy is a program that checks and compiles a SELinux security policy configuration into a binary representation that can be loaded into the kernel. If no -input file name is specified, checkpolicy will attempt to read from -policy.conf or policy, depending on whether the \-b flag is specified. +input file name is specified, +.B checkpolicy +will attempt to read from policy.conf or policy, depending on whether the \-b +flag is specified. .SH OPTIONS .TP
checkpolicy/checkpolicy.c +1 -1
@@ -109,7 +109,7 @@ static __attribute__((__noreturn__)) void usage(const char *progname) {printf ("usage: %s [-b[F]] [-C] [-d] [-U handle_unknown (allow,deny,reject)] [-M] " -"[-c policyvers (%d-%d)] [-o output_file|-] [-S] [-O]" +"[-c policyvers (%d-%d)] [-o output_file|-] [-S] [-O] " "[-t target_platform (selinux,xen)] [-E] [-V] [input_file]\n" , progname,POLICYDB_VERSION_MIN ,POLICYDB_VERSION_MAX );exit (1 );
Signature · 1 line
2.33.0
PATCH 8/9 checkpolicy: drop incorrect cast
In reply to Christian GöttscheThe function require_symbol takes the type hashtab_datum_t (alias void*) as third argument. Do not cast to hashtab_datum_t* alias void**. Since explicit casting to void* is unnecessary, drop the casts.
module_compiler.c:1002:36: warning: cast from 'cond_bool_datum_t *' (aka 'struct cond_bool_datum *') to 'hashtab_datum_t *' (aka 'void **') increases required alignment from 4 to 8 [-Wcast-align]
require_symbol(SYM_BOOLS, id, (hashtab_datum_t *) booldatum,
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
module_compiler.c:1092:40: warning: cast from 'cat_datum_t *' (aka 'struct cat_datum *') to 'hashtab_datum_t *' (aka 'void **') increases required alignment from 4 to 8 [-Wcast-align]
retval = require_symbol(SYM_CATS, id, (hashtab_datum_t *) cat,
^~~~~~~~~~~~~~~~~~~~~~~
--- checkpolicy/module_compiler.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
checkpolicy/module_compiler.c +3 -3
@@ -999,7 +999,7 @@ static int require_bool_tunable(int pass, int is_tunable)if (is_tunable) booldatum->flags |=COND_BOOL_FLAGS_TUNABLE ; retval = -require_symbol (SYM_BOOLS , id, (hashtab_datum_t *) booldatum, +require_symbol (SYM_BOOLS , id, booldatum, &booldatum->s.value, &booldatum->s.value);if (retval !=0 ) {cond_destroy_bool (id, booldatum,NULL );
@@ -1051,7 +1051,7 @@ int require_sens(int pass)return -1 ; }mls_level_init (level->level); - retval =require_symbol (SYM_LEVELS , id, (hashtab_datum_t *) level, + retval =require_symbol (SYM_LEVELS , id, level, &level->level->sens, &level->level->sens);if (retval !=0 ) {free (id);
@@ -1089,7 +1089,7 @@ int require_cat(int pass) }cat_datum_init (cat); - retval =require_symbol (SYM_CATS , id, (hashtab_datum_t *) cat, + retval =require_symbol (SYM_CATS , id, cat, &cat->s.value, &cat->s.value);if (retval !=0 ) {free (id);
Signature · 1 line
2.33.0
PATCH 9/9 checkpolicy: delay down-cast to avoid align warning
In reply to Christian GöttscheDelay the down-cast from hashtab_datum_t, alias void*, to the actual type once its kind has been determined.
module_compiler.c:174:19: warning: cast from 'symtab_datum_t *' (aka 'struct symtab_datum *') to 'level_datum_t *' (aka 'struct level_datum *') increases required alignment from 4 to 8 [-Wcast-align]
*dest_value = ((level_datum_t *)s)->level->sens;
^~~~~~~~~~~~~~~~~~--- checkpolicy/module_compiler.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
checkpolicy/module_compiler.c +2 -2
@@ -165,7 +165,7 @@ static int create_symbol(uint32_t symbol_type, hashtab_key_t key, hashtab_datum_ decl->decl_id, dest_value);if (ret ==1 && dest_value) { -symtab_datum_t *s = +hashtab_datum_t s =hashtab_search (policydbp->symtab[symbol_type].table, key);assert (s !=NULL );
@@ -173,7 +173,7 @@ static int create_symbol(uint32_t symbol_type, hashtab_key_t key, hashtab_datum_if (symbol_type ==SYM_LEVELS ) { *dest_value = ((level_datum_t *)s)->level->sens; }else { - *dest_value = s->value; + *dest_value = ((symtab_datum_t *)s)->value; } }else if (ret ==-2 ) {return -2 ;
Signature · 1 line
2.33.0
Quoted 8 lines · Christian Göttsche
Mark pointers to nodes of const ebitmaps also const. C does not enforce a transitive const-ness, but it clarifies the intent and improves maintainability.
Follow-up of
390ec54d278aSigned-off-by Christian Göttsche <[email protected]>
For all 9 patches:
Quoted 69 lines
--- libsepol/src/ebitmap.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-)libsepol/src/ebitmap.c +9
-8@@ -17,7 +17,8 @@int ebitmap_or (ebitmap_t * dst,const ebitmap_t * e1,const ebitmap_t * e2) { -ebitmap_node_t *n1, *n2, *new, *prev; +const ebitmap_node_t *n1, *n2; +ebitmap_node_t *new, *prev;ebitmap_init (dst);@@ -154,7 +155,7 @@ int ebitmap_hamming_distance(const ebitmap_t * e1, const ebitmap_t * e2)int ebitmap_cmp (const ebitmap_t * e1,const ebitmap_t * e2) { -ebitmap_node_t *n1, *n2; +const ebitmap_node_t *n1, *n2;if (e1->highbit != e2->highbit)return 0 ;@@ -175,7 +176,8 @@ int ebitmap_cmp(const ebitmap_t * e1, const ebitmap_t * e2)int ebitmap_cpy (ebitmap_t * dst,const ebitmap_t * src) { -ebitmap_node_t *n, *new, *prev; +const ebitmap_node_t *n; +ebitmap_node_t *new, *prev;ebitmap_init (dst); n = src->node;@@ -204,7 +206,7 @@ int ebitmap_cpy(ebitmap_t * dst, const ebitmap_t * src)int ebitmap_contains (const ebitmap_t * e1,const ebitmap_t * e2) { -ebitmap_node_t *n1, *n2; +const ebitmap_node_t *n1, *n2;if (e1->highbit < e2->highbit)return 0 ;@@ -231,8 +233,8 @@ int ebitmap_contains(const ebitmap_t * e1, const ebitmap_t * e2)int ebitmap_match_any (const ebitmap_t *e1,const ebitmap_t *e2) { -ebitmap_node_t *n1 = e1->node; -ebitmap_node_t *n2 = e2->node; +const ebitmap_node_t *n1 = e1->node; +const ebitmap_node_t *n2 = e2->node;while (n1 && n2) {if (n1->startbit < n2->startbit) {@@ -253,7 +255,7 @@ int ebitmap_match_any(const ebitmap_t *e1, const ebitmap_t *e2)int ebitmap_get_bit (const ebitmap_t * e,unsigned int bit) { -ebitmap_node_t *n; +const ebitmap_node_t *n;if (e->highbit < bit)return 0 ; --2.33.0
Quoted 8 lines · James Carter
Quoted 5 lines · Christian Göttsche
Mark pointers to nodes of const ebitmaps also const. C does not enforce a transitive const-ness, but it clarifies the intent and improves maintainability.
Quoted 7 lines
Quoted 3 lines
Follow-up of
390ec54d278aSigned-off-by Christian Göttsche <[email protected]>For all 9 patches:
Acked-by James Carter <[email protected]>
All nine patches have been merged.
Thanks,
Jim
Quoted 69 lines
Quoted 69 lines
--- libsepol/src/ebitmap.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-)libsepol/src/ebitmap.c +9
-8@@ -17,7 +17,8 @@int ebitmap_or (ebitmap_t * dst,const ebitmap_t * e1,const ebitmap_t * e2) { -ebitmap_node_t *n1, *n2, *new, *prev; +const ebitmap_node_t *n1, *n2; +ebitmap_node_t *new, *prev;ebitmap_init (dst);@@ -154,7 +155,7 @@ int ebitmap_hamming_distance(const ebitmap_t * e1, const ebitmap_t * e2)int ebitmap_cmp (const ebitmap_t * e1,const ebitmap_t * e2) { -ebitmap_node_t *n1, *n2; +const ebitmap_node_t *n1, *n2;if (e1->highbit != e2->highbit)return 0 ;@@ -175,7 +176,8 @@ int ebitmap_cmp(const ebitmap_t * e1, const ebitmap_t * e2)int ebitmap_cpy (ebitmap_t * dst,const ebitmap_t * src) { -ebitmap_node_t *n, *new, *prev; +const ebitmap_node_t *n; +ebitmap_node_t *new, *prev;ebitmap_init (dst); n = src->node;@@ -204,7 +206,7 @@ int ebitmap_cpy(ebitmap_t * dst, const ebitmap_t * src)int ebitmap_contains (const ebitmap_t * e1,const ebitmap_t * e2) { -ebitmap_node_t *n1, *n2; +const ebitmap_node_t *n1, *n2;if (e1->highbit < e2->highbit)return 0 ;@@ -231,8 +233,8 @@ int ebitmap_contains(const ebitmap_t * e1, const ebitmap_t * e2)int ebitmap_match_any (const ebitmap_t *e1,const ebitmap_t *e2) { -ebitmap_node_t *n1 = e1->node; -ebitmap_node_t *n2 = e2->node; +const ebitmap_node_t *n1 = e1->node; +const ebitmap_node_t *n2 = e2->node;while (n1 && n2) {if (n1->startbit < n2->startbit) {@@ -253,7 +255,7 @@ int ebitmap_match_any(const ebitmap_t *e1, const ebitmap_t *e2)int ebitmap_get_bit (const ebitmap_t * e,unsigned int bit) { -ebitmap_node_t *n; +const ebitmap_node_t *n;if (e->highbit < bit)return 0 ; --2.33.0