Skip to contentSkip to conversation

libsepol: do not call a NULL function in additive_scopes_to_cil()

PATCH Applied Nicolas Iooss 2 participants 6 messages Nov 22, 2016 – Nov 23, 2016 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/5 libsepol: do not call a NULL function in additive_scopes_to_cil()

When hll/pp operates on an invalid policy module which defines blocks with non-empty decl->symtab[SYM_COMMONS], additive_scopes_to_cil_map() calls func_to_cil[SYM_COMMONS], which is NULL.

In additive_scopes_to_cil(), filter out NULL elements of func_to_cil before calling additive_scopes_to_cil_map().

---
 libsepol/src/module_to_cil.c | 3 +++
 1 file changed, 3 insertions(+)
libsepol/src/module_to_cil.c +3 -0
index b7d3b5eb57eb..ab3ac03d7895 100644
@@ -3530,6 +3530,9 @@ static int additive_scopes_to_cil(int indent, struct policydb *pdb, struct avrul
 	struct avrule_decl *decl = stack_peek(decl_stack);
 
 	for (args.sym_index = 0; args.sym_index < SYM_NUM; args.sym_index++) {
+		if (func_to_cil[args.sym_index] == NULL) {
+			continue;
+		}
 		rc = hashtab_map(decl->symtab[args.sym_index].table, additive_scopes_to_cil_map, &args);
 		if (rc != 0) {
 			goto exit;
Signature · 1 line
2.10.2
#1 lore raw

PATCH 2/5 libsepol: do not crash when a symbol does not exist

When hll/pp reads an invalid policy module where some scopes use required symbols which are not defined, the program crashes with a segmentation fault in required_scopes_to_cil():

    Program received signal SIGSEGV, Segmentation fault.
    required_scopes_to_cil (decl_stack=0x6040b0, block=0x607780,
    pdb=0x6042e0, indent=0) at module_to_cil.c:3479
    3479                for (j = 0; j < scope_datum->decl_ids_len; j++)
    {
    => 0x00007ffff7a7b1a8 <block_to_cil+5224>:  44 8b 58 10 mov
    0x10(%rax),%r11d

    (gdb) bt
    #0  required_scopes_to_cil (decl_stack=0x6040b0, block=0x607780,
    pdb=0x6042e0, indent=0) at module_to_cil.c:3479
    #1  block_to_cil (pdb=pdb@entry=0x6042e0,
    block=block@entry=0x607780, stack=stack@entry=0x6040b0,
    indent=indent@entry=0) at module_to_cil.c:3622
    #2  0x00007ffff7a85a18 in global_block_to_cil (stack=0x6040b0,
    block=0x607780, pdb=0x6042e0) at module_to_cil.c:3738
    #3  blocks_to_cil (pdb=0x6042e0) at module_to_cil.c:3764
    #4  sepol_module_policydb_to_cil (fp=fp@entry=0x7ffff79d05e0
    <_IO_2_1_stdout_>, pdb=0x6042e0, linked=linked@entry=0) at
    module_to_cil.c:4051
    #5  0x00007ffff7a86b55 in sepol_module_package_to_cil
    (fp=fp@entry=0x7ffff79d05e0 <_IO_2_1_stdout_>, mod_pkg=0x604280) at
    module_to_cil.c:4080
    #6  0x0000000000401acc in main (argc=<optimized out>,
    argv=<optimized out>) at pp.c:150

    (gdb) p scope_datum
    $1 = (struct scope_datum *) 0x0

Detect such errors and exit with an error return value.

---
 libsepol/src/module_to_cil.c | 4 ++++
 1 file changed, 4 insertions(+)
libsepol/src/module_to_cil.c +4 -0
index ab3ac03d7895..1c2c51c614ab 100644
@@ -3469,6 +3469,10 @@ static int required_scopes_to_cil(int indent, struct policydb *pdb, struct avrul
 			key = pdb->sym_val_to_name[sym][i];
 
 			scope_datum = hashtab_search(pdb->scope[sym].table, key);
+			if (scope_datum == NULL) {
+				rc = -1;
+				goto exit;
+			}
 			for (j = 0; j < scope_datum->decl_ids_len; j++) {
 				if (scope_datum->decl_ids[j] == decl->decl_id) {
 					break;
Signature · 1 line
2.10.2
#2 lore raw

PATCH 3/5 libsepol: do not crash when block->branch_list is NULL

In reply to Nicolas Iooss

When fuzzing hll/pp, the fuzzer created a policy module with a block which has no declaration. With block->branch_list = NULL, typealias_list_create() triggered a NULL pointer dereference when computing max_decl_id.

---
 libsepol/src/module_to_cil.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libsepol/src/module_to_cil.c +1 -1
index 1c2c51c614ab..51a478434805 100644
@@ -374,7 +374,7 @@ static int typealias_list_create(struct policydb *pdb)
 
 	for (block = pdb->global; block != NULL; block = block->next) {
 		decl = block->branch_list;
-		if (decl->decl_id > max_decl_id) {
+		if (decl != NULL && decl->decl_id > max_decl_id) {
 			max_decl_id = decl->decl_id;
 		}
 	}
Signature · 1 line
2.10.2
#3 lore raw

PATCH 4/5 libsepol: make scope_index_destroy() more robust

In reply to Nicolas Iooss

When scope_index_read() fails while attempting to allocate memory for scope_index->class_perms_map, scope_index_destroy() gets called with scope->class_perms_len != 0 and scope->class_perms_map == NULL. This triggers the following segmentation fault (in semodule_package):

    Program received signal SIGSEGV, Segmentation fault.
    ebitmap_destroy (e=0x10) at ebitmap.c:362
    362     n = e->node;
    => 0x00007ffff79ff7f6 <ebitmap_destroy+134>:    48 8b 3f    mov
    (%rdi),%rdi

    (gdb) bt
    #0  ebitmap_destroy (e=0x10) at ebitmap.c:362
    #1  0x00007ffff79e2c37 in scope_index_destroy (scope=0x608860) at
    avrule_block.c:87
    #2  avrule_decl_destroy (x=0x608830) at avrule_block.c:103
    #3  0x00007ffff7aae99c in avrule_block_read (fp=0x605090,
    num_scope_syms=8, block=0x6054e8, p=0x605360) at policydb.c:3598
    #4  policydb_read (p=0x605360, fp=fp@entry=0x605090,
    verbose=verbose@entry=0) at policydb.c:3946
    #5  0x00007ffff7ab4ab4 in sepol_policydb_read (p=<optimized out>,
    pf=pf@entry=0x605090) at policydb_public.c:174
    #6  0x0000000000401d33 in main (argc=<optimized out>,
    argv=0x7fffffffdc88) at semodule_package.c:220

    (gdb) f 1

    (gdb) p *scope
    $1 = {scope = {{node = 0x0, highbit = 0}, {node = 0x0, highbit = 0},
    {node = 0x0, highbit = 0}, {node = 0x0, highbit = 0}, {node = 0x0,
    highbit = 0}, {node = 0x0, highbit = 0}, {node = 0x0, highbit = 0},
    {node = 0x0, highbit = 0}}, class_perms_map = 0x0, class_perms_len =
    4294934272}
---
 libsepol/src/avrule_block.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
libsepol/src/avrule_block.c +4 -2
index 84cfaf8c8201..224e9994110a 100644
@@ -83,8 +83,10 @@ static void scope_index_destroy(scope_index_t * scope)
 	for (i = 0; i < SYM_NUM; i++) {
 		ebitmap_destroy(scope->scope + i);
 	}
-	for (i = 0; i < scope->class_perms_len; i++) {
-		ebitmap_destroy(scope->class_perms_map + i);
+	if (scope->class_perms_map) {
+		for (i = 0; i < scope->class_perms_len; i++) {
+			ebitmap_destroy(scope->class_perms_map + i);
+		}
 	}
 	free(scope->class_perms_map);
 }
Signature · 1 line
2.10.2
#4 lore raw

PATCH 5/5 libsepol: fix unknown magic section number error message

In reply to Nicolas Iooss

When running hll/pp on some invalid policy module, it can output:

libsepol.sepol_module_package_read: unknown magic number at section 1, offset: 251, number: 0x

The last number looks funny and was caused by using "%ux". "u" is not a prefix like "l", "h", "z"... and "%x" already expects an unsigned integer (cf. http://man7.org/linux/man-pages/man3/printf.3.html).

---
 libsepol/src/module.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libsepol/src/module.c +1 -1
index 1c2becea5ef6..219355f30d27 100644
@@ -627,7 +627,7 @@ int sepol_module_package_read(sepol_module_package_t * mod,
 		default:
 			/* unknown section, ignore */
 			ERR(file->handle,
-			    "unknown magic number at section %u, offset: %zx, number: %ux ",
+			    "unknown magic number at section %u, offset: %zx, number: %x ",
 			    i, offsets[i], le32_to_cpu(buf[0]));
 			break;
 		}
Signature · 1 line
2.10.2
#5 lore raw

Re: [PATCH 5/5] libsepol: fix unknown magic section number error message

Quoted 10 lines · Nicolas Iooss

When running hll/pp on some invalid policy module, it can output:

libsepol.sepol_module_package_read: unknown magic number at section 1, offset: 251, number: 0x

The last number looks funny and was caused by using "%ux". "u" is not a prefix like "l", "h", "z"... and "%x" already expects an unsigned integer (cf. http://man7.org/linux/man-pages/man3/printf.3.html).

Thanks, applied all five patches.

Quoted 18 lines
---
 libsepol/src/module.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libsepol/src/module.c +1 -1
index 1c2becea5ef6..219355f30d27 100644
@@ -627,7 +627,7 @@ int sepol_module_package_read(sepol_module_package_t * mod,
 		default:
 			/* unknown section, ignore */
 			ERR(file->handle,
-			    "unknown magic number at section %u, offset: %zx, number: %ux ",
+			    "unknown magic number at section %u, offset: %zx, number: %x ",
 			    i, offsets[i], le32_to_cpu(buf[0]));
 			break;
 		}