Skip to contentSkip to conversation

policy_define.c: don't free memory returned from queue_head()

PATCH Applied Nick Kralevich 2 participants 2 messages Jan 13, 2017 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 policy_define.c: don't free memory returned from queue_head()

Unlike queue_remove(), queue_head() does not modify the queue, but rather, returns a pointer to an element within the queue. Freeing the memory associated with a value returned from that function corrupts subsequent users of the queue, who may try to reference this now-deallocated memory.

This causes the following policy generation errors on Android:

FAILED:
out/target/product/bullhead/obj/ETC/plat_sepolicy.cil_intermediates/plat_policy_nvr.cil /bin/bash -c "out/host/linux-x86/bin/checkpolicy -M -C -c 30 -o out/target/product/bullhead/obj/ETC/plat_sepolicy.cil_intermediates/plat_policy_nvr.cil out/target/product/bullhead/obj/ETC/plat_sepolicy.cil_intermediates/plat_policy.conf" system/sepolicy/public/app.te:241:ERROR 'only ioctl extended permissions are supported' at token ';' on line 6784:
#line 241
} };
checkpolicy: error(s) encountered while parsing configuration

because the value of "id" in:

  id = queue_remove(id_queue);
  if (strcmp(id,"ioctl") == 0) {
    ...
  } else {
    yyerror("only ioctl extended permissions are supported");
    ...
  }

is now garbage.

This is a partial revert of the following commit:

c1ba8311 checkpolicy: free id where it was leaked

---
 checkpolicy/policy_define.c | 2 --
 1 file changed, 2 deletions(-)
checkpolicy/policy_define.c +0 -2
index d158ad0..6bfadbe 100644
@@ -2012,7 +2012,6 @@ int define_te_avtab_xperms_helper(int which, avrule_t ** rule)
 		    (class_perm_node_t *) malloc(sizeof(class_perm_node_t));
 		if (!cur_perms) {
 			yyerror("out of memory");
-			free(id);
 			ret = -1;
 			goto out;
 		}
@@ -2048,7 +2047,6 @@ int define_te_avtab_xperms_helper(int which, avrule_t ** rule)
 		}
 	}
 
-	free(id);
 	ebitmap_destroy(&tclasses);
 
 	avrule->perms = perms;
Signature · 1 line
2.11.0.483.g087da7b7c-goog
#1 lore raw
Quoted 36 lines · Nick Kralevich

Unlike queue_remove(), queue_head() does not modify the queue, but rather, returns a pointer to an element within the queue. Freeing the memory associated with a value returned from that function corrupts subsequent users of the queue, who may try to reference this now-deallocated memory.

This causes the following policy generation errors on Android:

  FAILED:
out/target/product/bullhead/obj/ETC/plat_sepolicy.cil_intermediates/p lat_policy_nvr.cil
  /bin/bash -c "out/host/linux-x86/bin/checkpolicy -M -C -c 30 -o out/target/product/bullhead/obj/ETC/plat_sepolicy.cil_intermediates/p lat_policy_nvr.cil
out/target/product/bullhead/obj/ETC/plat_sepolicy.cil_intermediates/p lat_policy.conf"
  system/sepolicy/public/app.te:241:ERROR 'only ioctl extended permissions
  are supported' at token ';' on line 6784:
  #line 241
  } };
  checkpolicy:  error(s) encountered while parsing configuration

because the value of "id" in:

  id = queue_remove(id_queue);
  if (strcmp(id,"ioctl") == 0) {
    ...
  } else {
    yyerror("only ioctl extended permissions are supported");
    ...
  }

Quoted 5 lines

is now garbage.

This is a partial revert of the following commit:

  c1ba8311 checkpolicy: free id where it was leaked

Thanks, applied.

Quoted 30 lines

---
 checkpolicy/policy_define.c | 2 --

 1 file changed, 2 deletions(-)
checkpolicy/policy_define.c +0 -1
index d158ad0..6bfadbe 100644
@@ -2012,7 +2012,6 @@ int define_te_avtab_xperms_helper(int which,
 avrule_t ** rule)
  		    (class_perm_node_t *)
 malloc(sizeof(class_perm_node_t));
  		if (!cur_perms) {
  			yyerror("out of memory");
-			free(id);
  			ret = -1;

  goto out;
  }

checkpolicy/policy_define.c +0 -0
@@ -2048,7 +2047,6 @@ int define_te_avtab_xperms_helper(int which,
 avrule_t ** rule)
  		}
  	}

checkpolicy/policy_define.c +0 -1
-	free(id);

  ebitmap_destroy(&tclasses);

  avrule->perms = perms;