Skip to contentSkip to conversation

libsepol/cil: use __cil_ordered_lists_destroy() to free unordered_classorder_lists

PATCH In review Nicolas Iooss 2 participants 6 messages Feb 19, 2017 – Feb 21, 2017 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/5 libsepol/cil: use __cil_ordered_lists_destroy() to free unordered_classorder_lists

In cil_resolve_ast, unordered_classorder_lists is a list of cil_ordered_list. It needs to be destroyed with __cil_ordered_lists_destroy() to free all associated memory.

This has been tested with the following policy:

    (class CLASS1 ())
    (class CLASS2 ())
    (classorder (unordered CLASS1))
    (classorder (CLASS2))

This memory leak has been found by running clang's Address Sanitizer on a set of policies generated by American Fuzzy Lop.

---
 libsepol/cil/src/cil_resolve_ast.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libsepol/cil/src/cil_resolve_ast.c +1 -1
index 6628dc4d4b30..5e5298d98e61 100644
@@ -3936,8 +3936,8 @@ exit:
 	__cil_ordered_lists_destroy(&extra_args.classorder_lists);
 	__cil_ordered_lists_destroy(&extra_args.catorder_lists);
 	__cil_ordered_lists_destroy(&extra_args.sensitivityorder_lists);
+	__cil_ordered_lists_destroy(&extra_args.unordered_classorder_lists);
 	cil_list_destroy(&extra_args.in_list, CIL_FALSE);
-	cil_list_destroy(&extra_args.unordered_classorder_lists, CIL_FALSE);
 
 	return rc;
 }
Signature · 1 line
2.11.1
#1 lore raw

PATCH 2/5 libsepol/cil: destroy bitmap when __cil_permx_str_to_int() fails

When __cil_permx_to_bitmap() calls __cil_permx_str_to_int() on an invalid number, local variablt "bitmap" is left initialized when the function returns and its memory is leaked.

This memory leak has been found by running clang's Address Sanitizer on a set of policies generated by American Fuzzy Lop.

---
 libsepol/cil/src/cil_post.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libsepol/cil/src/cil_post.c +1 -1
index 687962eae5ee..2de55b562b8f 100644
@@ -806,13 +806,13 @@ static int __cil_permx_to_bitmap(struct cil_symtab_datum *datum, ebitmap_t *bitm
 
 	if (ebitmap_set_bit(bitmap, (unsigned int)val, 1)) {
 		cil_log(CIL_ERR, "Failed to set permissionx bit\n");
-		ebitmap_destroy(bitmap);
 		goto exit;
 	}
 
 	return SEPOL_OK;
 
 exit:
+	ebitmap_destroy(bitmap);
 	return rc;
 }
 
Signature · 1 line
2.11.1
#2 lore raw

Re: [PATCH 2/5] libsepol/cil: destroy bitmap when __cil_permx_str_to_int() fails

Quoted 8 lines · Nicolas Iooss

When __cil_permx_to_bitmap() calls __cil_permx_str_to_int() on an invalid number, local variablt "bitmap" is left initialized when the function returns and its memory is leaked.

This memory leak has been found by running clang's Address Sanitizer on a set of policies generated by American Fuzzy Lop.

For this one I decided to move the ebitmap_init() call to right before the ebitmap_set_bit() call. This seemed to fit the style of the other functions in cil_post.c a bit better.

I applied the other four patches.

Thanks,
Jim

Quoted 24 lines
---
 libsepol/cil/src/cil_post.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libsepol/cil/src/cil_post.c +1 -1
index 687962eae5ee..2de55b562b8f 100644
@@ -806,13 +806,13 @@ static int __cil_permx_to_bitmap(struct cil_symtab_datum *datum, ebitmap_t *bitm
 
 	if (ebitmap_set_bit(bitmap, (unsigned int)val, 1)) {
 		cil_log(CIL_ERR, "Failed to set permissionx bit\n");
-		ebitmap_destroy(bitmap);
 		goto exit;
 	}
 
 	return SEPOL_OK;
 
 exit:
+	ebitmap_destroy(bitmap);
 	return rc;
 }
 
Signature · 2 lines
James Carter <[email protected]>
National Security Agency
#3 lore raw

PATCH 3/5 libsepol/cil: free the first operand if the second one is invalid

In reply to Nicolas Iooss

When __cil_expr_to_bitmap() fails to parse the second operand of an operation with two operands, it returns an error without destroying the bitmap which has been created for the first operand. Fix this memory leak.

This has been tested with the following policy:

(class CLASS (PERM))
(classorder (CLASS))
(sid SID)
(sidorder (SID))
(user USER)
(role ROLE)
(type TYPE)
(category CAT)
(categoryorder (CAT))
(sensitivity SENS)
(sensitivityorder (SENS)) (sensitivitycategory SENS (CAT))
(allow TYPE self (CLASS (PERM))) (roletype ROLE TYPE)
(userrole USER ROLE)
(userlevel USER (SENS))
(userrange USER ((SENS)(SENS (CAT)))) (sidcontext SID (USER ROLE TYPE ((SENS)(SENS))))

(permissionx ioctl_test (ioctl CLASS
(and (range 0x1600 0x19FF) (.ot (range 0x1750 0x175F)))))

This memory leak has been found by running clang's Address Sanitizer on a set of policies generated from secilc/test/policy.cil by American Fuzzy Lop.

---
 libsepol/cil/src/cil_post.c | 1 +
 1 file changed, 1 insertion(+)
libsepol/cil/src/cil_post.c +1 -0
index 2de55b562b8f..48a7bd3daeb8 100644
@@ -1105,6 +1105,7 @@ static int __cil_expr_to_bitmap(struct cil_list *expr, ebitmap_t *out, int max,
 				rc = __cil_expr_to_bitmap_helper(curr->next->next, flavor, &b2, max, db);
 				if (rc != SEPOL_OK) {
 					cil_log(CIL_INFO, "Failed to get second operand bitmap\n");
+					ebitmap_destroy(&b1);
 					goto exit;
 				}
 
Signature · 1 line
2.11.1
#4 lore raw

PATCH 4/5 libsepol/cil: do not leak left-hand side of an invalid constraint

In reply to Nicolas Iooss

__cil_fill_constraint_expr() does not destroy the list associated with the first operand of a two-operand operation when the second operand is invalid.

This memory leak can be reproduced with the following policy:

(constrain (files (read))
(not (or (and (eq t1 exec_t) (%q t2 bin_t)) (eq r1 r2))))

This memory leak has been found by running clang's Address Sanitizer on a set of policies generated from secilc/test/policy.cil by American Fuzzy Lop.

---
 libsepol/cil/src/cil_build_ast.c | 1 +
 1 file changed, 1 insertion(+)
libsepol/cil/src/cil_build_ast.c +1 -0
index e4a0539f64ad..d3d663b06512 100644
@@ -2753,6 +2753,7 @@ static int __cil_fill_constraint_expr(struct cil_tree_node *current, enum cil_fl
 		}
 		rc = __cil_fill_constraint_expr(current->next->next->cl_head, flavor, &rexpr, depth);
 		if (rc != SEPOL_OK) {
+			cil_list_destroy(&lexpr, CIL_TRUE);
 			goto exit;
 		}
 		cil_list_init(expr, flavor);
Signature · 1 line
2.11.1
#5 lore raw

PATCH 5/5 libsepol/cil: free bitmaps in cil_level_equals()

In reply to Nicolas Iooss

cil_level_equals() builds two bitmap and compare them but does not destroy them before returning the result.

This memory leak has been found by running clang's Address Sanitizer on a set of policies generated by American Fuzzy Lop.

---
 libsepol/cil/src/cil.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
libsepol/cil/src/cil.c +3 -1
index 15833c1322e2..7c40ad0c5ed8 100644
@@ -1299,7 +1299,9 @@ static int cil_level_equals(struct cil_level *low, struct cil_level *high)
 		goto exit;
 	}
 
-	return ebitmap_cmp(&elow, &ehigh);
+	rc = ebitmap_cmp(&elow, &ehigh);
+	ebitmap_destroy(&elow);
+	ebitmap_destroy(&ehigh);
 
 exit:
 	return rc;
Signature · 1 line
2.11.1