Skip to contentSkip to conversation

libsepol: propagate calloc() failure

PATCH Applied Nicolas Iooss 2 participants 3 messages May 20, 2017 – May 23, 2017 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 libsepol: propagate calloc() failure

When common_to_cil() or class_to_cil() fail to allocate an array to map a permissions hashtable (for example when permissions.nprim is too big), class_perm_to_array() gets called on a NULL pointer. Fix this.

This issue has been found while fuzzing hll/pp with the American Fuzzy Lop.

---
 libsepol/src/module_to_cil.c | 6 ++++++
 1 file changed, 6 insertions(+)
libsepol/src/module_to_cil.c +6 -0
index 7d8eb204d2fd..e2bc4b2e6bec 100644
@@ -1662,6 +1662,9 @@ static int common_to_cil(char *key, void *data, void *UNUSED(arg))
 
 	arr.count = 0;
 	arr.perms = calloc(common->permissions.nprim, sizeof(*arr.perms));
+	if (arr.perms == NULL) {
+		goto exit;
+	}
 	rc = hashtab_map(common->permissions.table, class_perm_to_array, &arr);
 	if (rc != 0) {
 		goto exit;
@@ -1952,6 +1955,9 @@ static int class_to_cil(int indent, struct policydb *pdb, struct avrule_block *U
 
 	arr.count = 0;
 	arr.perms = calloc(class->permissions.nprim, sizeof(*arr.perms));
+	if (arr.perms == NULL) {
+		goto exit;
+	}
 	rc = hashtab_map(class->permissions.table, class_perm_to_array, &arr);
 	if (rc != 0) {
 		goto exit;
Signature · 1 line
2.12.2
#1 lore raw

PATCH 2/2 libsepol: use the number of elements in calloc first argument

When allocating an array with calloc(), the first argument usually is the number of items and the second one the size of an item. Doing so silences a warning reported by clang's static analyzer:

kernel_to_cil.c:2050:14: warning: Call to 'calloc' has an allocation size of 0 bytes.
cond_data = calloc(sizeof(struct cond_data), num);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

---
 libsepol/src/kernel_to_cil.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libsepol/src/kernel_to_cil.c +1 -1
index 3a1c0be76f45..498e2947f6cb 100644
@@ -2043,7 +2043,7 @@ static int write_cond_nodes_to_cil(FILE *out, struct policydb *pdb)
 		num++;
 	}
 
-	cond_data = calloc(sizeof(struct cond_data), num);
+	cond_data = calloc(num, sizeof(struct cond_data));
 	if (!cond_data) {
 		rc = -1;
 		goto exit;
Signature · 1 line
2.12.2
#2 lore raw

Re: [PATCH 2/2] libsepol: use the number of elements in calloc first argument

Quoted 11 lines · Nicolas Iooss

When allocating an array with calloc(), the first argument usually is the number of items and the second one the size of an item. Doing so silences a warning reported by clang's static analyzer:

    kernel_to_cil.c:2050:14: warning: Call to 'calloc' has an allocation
    size of 0 bytes.
            cond_data = calloc(sizeof(struct cond_data), num); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Thanks, applied both.

Quoted 19 lines

---
 libsepol/src/kernel_to_cil.c | 2 +-

 1 file changed, 1 insertion(+), 1 deletion(-)
libsepol/src/kernel_to_cil.c +0 -0
index 3a1c0be76f45..498e2947f6cb 100644
@@ -2043,7 +2043,7 @@ static int write_cond_nodes_to_cil(FILE *out,
 struct policydb *pdb)
  		num++;
  	}

libsepol/src/kernel_to_cil.c +1 -1
-	cond_data = calloc(sizeof(struct cond_data), num);
+	cond_data = calloc(num, sizeof(struct cond_data));

  if (!cond_data) {
  rc = -1;
  goto exit;