Fix some issues spotted by static analyzers
cover Fix some issues spotted by static analyzers
Hi,
As you may have noticed, I have been using clang's static analyzer for a few months and submitted fixes for bugs that it found. There are also many minor issues in the code (memory leaks, dead assignments, etc.) which introduce much noise and make it harder to find real issues. For example if a reported "dead variable assignment" is about the return value of a function which would need to be checked and the wrong variable is used in the check, this would be detected by the analyzer but would be in the noise of other minor issues.
Therefore this patchset (and the ones that I would like to send in the following weeks) is about reducing this noise.
I am starting with libsepol/src/module_to_cil.c and while cleaning up the commits I have written in order to use clang's static analyzer, I stumbled upon some other local commits I forgot to send, which fix more important bugs (like a missing call to va_end(), in the last patch). This explains why there are other files which are modified.
Here is the git shortlog:
Nicolas Iooss (7):
libsepol: cil: silence clang analyzer false positive
libsepol: do not leak memory if list_prepend fails
libsepol: remove some dead assignments
libsepol: do not call malloc with 0 byte
libsepol: remove unused variable
checkpolicy: destroy the class datum if it fails to initialize
libsepol: destroy the copied va_list
checkpolicy/module_compiler.c | 1 + libsepol/cil/src/cil_tree.c | 2 +- libsepol/src/kernel_to_common.c | 3 +++ libsepol/src/module_to_cil.c | 21 +++++++++++++-------- 4 files changed, 18 insertions(+), 9 deletions(-)
Cheers,
Nicolas
Signature · 1 line
2.17.0
PATCH 1/7 libsepol: cil: silence clang analyzer false positive
In cil_tree_print_expr(), "rc < 0" is equivalent to "rc != 0" but clang's static analyzer does not know about this. Help it.
--- libsepol/cil/src/cil_tree.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
libsepol/cil/src/cil_tree.c +1 -1
@@ -512,7 +512,7 @@ void cil_tree_print_expr(struct cil_list *datum_expr, struct cil_list *str_expr) }else { rc =cil_expr_to_string (str_expr, &expr_str); } -if (rc <0 ) { +if (rc !=SEPOL_OK ) {cil_log (CIL_INFO ,"ERROR)" );return ; }
Signature · 1 line
2.17.0
PATCH 2/7 libsepol: do not leak memory if list_prepend fails
In reply to Nicolas IoossWhen list_prepend() returns an error, it always means it failed to allocate some memory and does not hold any reference to its argument data. This argument needs to be freed by the caller in order to prevent a memory leak.
While reviewing list_prepend() callers, I spend quite some time understanding why typealiases_gather_map() does not need to strdup(key) or free(key) when calling list_prepend(..., key) even though "key" comes from pdb->p_types.table: because typealias_list_destroy() does not free the inserted items. Add a comment to make this clearer in the code.
--- libsepol/src/module_to_cil.c | 7 +++++++ 1 file changed, 7 insertions(+)
libsepol/src/module_to_cil.c +7 -0
@@ -298,6 +298,8 @@ static int roles_gather_map(char *key, void *data, void *args) role_node->role = role; rc =list_prepend ((struct list *)args, role_node); +if (rc !=0 ) +free (role_node);return rc; }
@@ -344,6 +346,11 @@ static int typealiases_gather_map(char *key, void *data, void *arg) goto exit; } } +/* As typealias_lists[scope_id] does not hold the +* ownership of its items (typealias_list_destroy does +* not free the list items), "key" does not need to be +* strdup'ed before it is inserted in the list. +*/ list_prepend (typealias_lists[scope_id], key); } }
Signature · 1 line
2.17.0
PATCH 3/7 libsepol: remove some dead assignments
In reply to Nicolas Ioossclang's static analyzer warns about dead assignments to local variables. In module_to_cil.c, there are some which are quite straightforward to review. Remove them.
--- libsepol/src/module_to_cil.c | 4 ---- 1 file changed, 4 deletions(-)
libsepol/src/module_to_cil.c +0 -4
@@ -1099,7 +1099,6 @@ static int roletype_role_in_ancestor_to_cil(struct policydb *pdb, struct stack * goto exit; } - curr = role_list->head;for (curr = role_list->head; curr !=NULL ; curr = curr->next) { role_node = curr->data;if (!is_id_in_ancestor_scope (pdb, decl_stack, role_node->role_name,SYM_ROLES )) {
@@ -1291,7 +1290,6 @@ static int cond_expr_to_cil(int indent, struct policydb *pdb, struct cond_expr * rc =-1 ; goto exit; } - num_params =0 ; }else {switch (curr->expr_type) {case COND_NOT : op ="not" ;break ;
@@ -1831,8 +1829,6 @@ static int constraint_expr_to_string(struct policydb *pdb, struct constraint_expfree (names); names =NULL ; } - - num_params =0 ; }else {switch (expr->expr_type) {case CEXPR_NOT : op ="not" ;break ;
Signature · 1 line
2.17.0
PATCH 4/7 libsepol: do not call malloc with 0 byte
In reply to Nicolas Ioossclang's static analyzer reports that ebitmap_to_names() can call malloc(0) when the bitmap is empty. If malloc() returns NULL, this triggers a misleading "Out of memory" error.
Work around this by treating empty bitmaps as appropriate.
--- libsepol/src/module_to_cil.c | 6 ++++++ 1 file changed, 6 insertions(+)
libsepol/src/module_to_cil.c +6 -0
@@ -1009,6 +1009,12 @@ static int ebitmap_to_names(struct ebitmap *map, char **vals_to_names, char ***n } } +if (!num) { + *names =NULL ; + *num_names =0 ; + goto exit; + } + name_arr =malloc (sizeof (*name_arr) * num);if (name_arr ==NULL ) {log_err ("Out of memory" );
Signature · 1 line
2.17.0
PATCH 5/7 libsepol: remove unused variable
In reply to Nicolas Ioosssepol_ppfile_to_module_package() does not use its variable "FILE *f = NULL;" but to fclose() it. This variable has been unneeded since the introduction of function ppfile_to_module_package() in commit 893851c0a146 ("policycoreutils: add a HLL compiler to convert policy packages (.pp) to CIL").
--- libsepol/src/module_to_cil.c | 4 ---- 1 file changed, 4 deletions(-)
libsepol/src/module_to_cil.c +0 -4
@@ -4232,7 +4232,6 @@ exit:int sepol_ppfile_to_module_package (FILE *fp,struct sepol_module_package **mod_pkg) {int rc =-1 ; -FILE *f =NULL ;struct sepol_policy_file *pf =NULL ;struct sepol_module_package *pkg =NULL ;char *data =NULL ;
@@ -4284,9 +4283,6 @@ exit:free (data);sepol_policy_file_free (pf); -if (f !=NULL ) { -fclose (f); - }if (rc !=0 ) {sepol_module_package_free (pkg);
Signature · 1 line
2.17.0
PATCH 6/7 checkpolicy: destroy the class datum if it fails to initialize
In reply to Nicolas Ioossrequire_class() allocate memory for its variable "class_datum_t *datum" and calls symtab_init(&datum->permissions, PERM_SYMTAB_SIZE). If this second call fails, datum is not freed.
Fix this memory leak.
--- checkpolicy/module_compiler.c | 1 + 1 file changed, 1 insertion(+)
checkpolicy/module_compiler.c +1 -0
@@ -802,6 +802,7 @@ int require_class(int pass)if ((datum =calloc (1 ,sizeof (*datum))) ==NULL ||symtab_init (&datum->permissions,PERM_SYMTAB_SIZE )) {yyerror ("Out of memory!" ); +class_datum_destroy (datum);return -1 ; } ret =
Signature · 1 line
2.17.0
PATCH 7/7 libsepol: destroy the copied va_list
In reply to Nicolas Ioossva_copy()'s manpage [1] states:
Each invocation of va_copy() must be matched by a corresponding invocation of va_end() in the same function.
create_str_helper() is using va_copy() without va_end(). Add the missing call.
[1] https://linux.die.net/man/3/va_copy
--- libsepol/src/kernel_to_common.c | 3 +++ 1 file changed, 3 insertions(+)
libsepol/src/kernel_to_common.c +3 -0
@@ -80,10 +80,13 @@ static char *create_str_helper(const char *fmt, int num, va_list vargs) goto exit; } +va_end (vargs2); +return str; exit:free (str); +va_end (vargs2);return NULL ; }
Signature · 1 line
2.17.0
Re: [PATCH 7/7] libsepol: destroy the copied va_list
ack
Quoted 42 lines · Nicolas Iooss
va_copy()'s manpage [1] states:
Each invocation of
va_copy()must be matched by a corresponding invocation ofva_end()in the same function.
create_str_helper()is usingva_copy()withoutva_end(). Add the missing call.[1] https://linux.die.net/man/3/va_copy
Signed-off-by Nicolas Iooss <[email protected]>--- libsepol/src/kernel_to_common.c | 3 +++ 1 file changed, 3 insertions(+)libsepol/src/kernel_to_common.c +3
-1@@ -80,10 +80,13 @@ static char *create_str_helper(const char *fmt, int num, va_list vargs) goto exit; } +va_end (vargs2); +return str; exit:free (str); +va_end (vargs2);return NULL ; } --2.17.0
_______________________________________________
Selinux mailing list
[email protected]
To unsubscribe, send email to [email protected].
To get help, send an email containing "help" to [email protected].
Quoted 30 lines · Nicolas Iooss
Hi,
As you may have noticed, I have been using clang's static analyzer for a few months and submitted fixes for bugs that it found. There are also many minor issues in the code (memory leaks, dead assignments, etc.) which introduce much noise and make it harder to find real issues. For example if a reported "dead variable assignment" is about the return value of a function which would need to be checked and the wrong variable is used in the check, this would be detected by the analyzer but would be in the noise of other minor issues.Therefore this patchset (and the ones that I would like to send in the following weeks) is about reducing this noise.
I am starting with
libsepol/src/module_to_cil.cand while cleaning up the commits I have written in order to use clang's static analyzer, I stumbled upon some other local commits I forgot to send, which fix more important bugs (like a missing call tova_end(), in the last patch). This explains why there are other files which are modified.Here is the git shortlog:
Nicolas Iooss (7):
libsepol: cil: silence clang analyzer false positive
libsepol: do not leak memory iflist_prependfails
libsepol: remove some dead assignments
libsepol: do not call malloc with 0 byte
libsepol: remove unused variable
checkpolicy: destroy the class datum if it fails to initialize
libsepol: destroy the copiedva_list
Quoted 5 lines
checkpolicy/module_compiler.c | 1 + libsepol/cil/src/cil_tree.c | 2 +- libsepol/src/kernel_to_common.c | 3 +++ libsepol/src/module_to_cil.c | 21 +++++++++++++-------- 4 files changed, 18 insertions(+), 9 deletions(-)
These all look good to me. I didnt compile test since your CI has already done that many many times.
For the whole series:
-- Jason
Quoted 13 lines
Cheers,
NicolasSignature · 8 lines
2.17.0 _______________________________________________ Selinux mailing list [email protected] To unsubscribe, send email to [email protected]. To get help, send an email containing "help" to [email protected].
Quoted 37 lines · Jason Zaman
Quoted 30 lines · Nicolas Iooss
Hi,
As you may have noticed, I have been using clang's static analyzer for a few months and submitted fixes for bugs that it found. There are also many minor issues in the code (memory leaks, dead assignments, etc.) which introduce much noise and make it harder to find real issues. For example if a reported "dead variable assignment" is about the return value of a function which would need to be checked and the wrong variable is used in the check, this would be detected by the analyzer but would be in the noise of other minor issues.Therefore this patchset (and the ones that I would like to send in the following weeks) is about reducing this noise.
I am starting with
libsepol/src/module_to_cil.cand while cleaning up the commits I have written in order to use clang's static analyzer, I stumbled upon some other local commits I forgot to send, which fix more important bugs (like a missing call tova_end(), in the last patch). This explains why there are other files which are modified.Here is the git shortlog:
Nicolas Iooss (7):
libsepol: cil: silence clang analyzer false positive
libsepol: do not leak memory iflist_prependfails
libsepol: remove some dead assignments
libsepol: do not call malloc with 0 byte
libsepol: remove unused variable
checkpolicy: destroy the class datum if it fails to initialize
libsepol: destroy the copiedva_listQuoted 5 lines
checkpolicy/module_compiler.c | 1 + libsepol/cil/src/cil_tree.c | 2 +- libsepol/src/kernel_to_common.c | 3 +++ libsepol/src/module_to_cil.c | 21 +++++++++++++-------- 4 files changed, 18 insertions(+), 9 deletions(-)
Quoted 5 lines
These all look good to me. I didnt compile test since your CI has already done that many many times.
For the whole series:
Acked-by Jason Zaman <[email protected]>
Thanks! I applied the commits.
Nicolas