Skip to contentSkip to conversation

libsepol/cil: silence static analyser's use-after-free warning

PATCH In review Nicolas Iooss 2 participants 3 messages Feb 6, 2019 – Feb 12, 2019 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libsepol/cil: silence static analyser's use-after-free warning

clang's static analyze reports a use-after-free in __cil_expr_to_string(), when __cil_expr_to_string_helper() does not modify its third parameter (variable s1 here) in this loop:

    for (curr = curr->next; curr; curr = curr->next) {
        __cil_expr_to_string_helper(curr, flavor, &s1);
        cil_asprintf(&c2, "%s %s", c1, s1);
        free(c1);
        free(s1);
        c1 = c2;
    }

Silence this warning by making sure s1 is always NULL at the beginning of every iteration of the loop.

---
 libsepol/cil/src/cil_binary.c | 1 +
 1 file changed, 1 insertion(+)
libsepol/cil/src/cil_binary.c +1 -0
index a10c3763bea4..e2eb3ebe8ff3 100644
@@ -2075,6 +2075,7 @@ static void __cil_expr_to_string(struct cil_list *expr, enum cil_flavor flavor,
 		char *c2 = NULL;
 		__cil_expr_to_string_helper(curr, flavor, &c1);
 		for (curr = curr->next; curr; curr = curr->next) {
+			s1 = NULL;
 			__cil_expr_to_string_helper(curr, flavor, &s1);
 			cil_asprintf(&c2, "%s %s", c1, s1);
 			free(c1);
Signature · 1 line
2.20.1
Acked-by on v1 1/1
#1 lore raw
Quoted 12 lines · Nicolas Iooss

clang's static analyze reports a use-after-free in __cil_expr_to_string(), when __cil_expr_to_string_helper() does not modify its third parameter (variable s1 here) in this loop:

     for (curr = curr->next; curr; curr = curr->next) {
         __cil_expr_to_string_helper(curr, flavor, &s1);
         cil_asprintf(&c2, "%s %s", c1, s1);
         free(c1);
         free(s1);
         c1 = c2;
     }
Quoted 4 lines

Silence this warning by making sure s1 is always NULL at the beginning of every iteration of the loop.

Quoted 17 lines
---
  libsepol/cil/src/cil_binary.c | 1 +
  1 file changed, 1 insertion(+)
libsepol/cil/src/cil_binary.c +1 -0
index a10c3763bea4..e2eb3ebe8ff3 100644
@@ -2075,6 +2075,7 @@ static void __cil_expr_to_string(struct cil_list *expr, enum cil_flavor flavor,
  		char *c2 = NULL;
  		__cil_expr_to_string_helper(curr, flavor, &c1);
  		for (curr = curr->next; curr; curr = curr->next) {
+			s1 = NULL;
  			__cil_expr_to_string_helper(curr, flavor, &s1);
  			cil_asprintf(&c2, "%s %s", c1, s1);
  			free(c1);
Signature · 2 lines
James Carter <[email protected]>
National Security Agency
#2 lore raw
Quoted 14 lines · On Mon, Feb 11, 2019 at 8:06 PM jwcart2 <[email protected]> wrote:
Quoted 12 lines · Nicolas Iooss

clang's static analyze reports a use-after-free in __cil_expr_to_string(), when __cil_expr_to_string_helper() does not modify its third parameter (variable s1 here) in this loop:

     for (curr = curr->next; curr; curr = curr->next) {
         __cil_expr_to_string_helper(curr, flavor, &s1);
         cil_asprintf(&c2, "%s %s", c1, s1);
         free(c1);
         free(s1);
         c1 = c2;
     }
Quoted 6 lines
Quoted 4 lines

Silence this warning by making sure s1 is always NULL at the beginning of every iteration of the loop.

Merged.

Nicolas

Quoted 22 lines
Quoted 10 lines
---
  libsepol/cil/src/cil_binary.c | 1 +
  1 file changed, 1 insertion(+)
libsepol/cil/src/cil_binary.c +0 -0
index a10c3763bea4..e2eb3ebe8ff3 100644

libsepol/cil/src/cil_binary.c static void __cil_expr_to_string(struct cil_list *expr, enum cil_flavor flavor, · line 2081

Quoted 7 lines
libsepol/cil/src/cil_binary.c +1 -0
@@ -2075,6 +2075,7 @@ static void __cil_expr_to_string(struct cil_list *expr, enum cil_flavor flavor,
              char *c2 = NULL;
              __cil_expr_to_string_helper(curr, flavor, &c1);
              for (curr = curr->next; curr; curr = curr->next) {
+                     s1 = NULL;
                      __cil_expr_to_string_helper(curr, flavor, &s1);
                      cil_asprintf(&c2, "%s %s", c1, s1);
                      free(c1);

--
James Carter <[email protected]>
National Security Agency