Skip to contentSkip to conversation

libsepol: replace an assert with an error message

PATCH Applied Nicolas Iooss 2 participants 9 messages Nov 15, 2016 – Nov 16, 2016 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 libsepol: replace an assert with an error message

When fuzzing hll/pp inputs, a policy module where the value of scope->decl_ids_len has been modified to zero makes the program abort (when it has been compiled without -DNDEBUG).

Change the behavior to report an error message instead. This eases fuzzing functions like policydb_read().

---
 libsepol/src/policydb.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
libsepol/src/policydb.c +4 -1
index cdb3cde6b5e2..e36d82f0a1e8 100644
@@ -3635,7 +3635,10 @@ static int scope_read(policydb_t * p, int symnum, struct policy_file *fp)
 		goto cleanup;
 	scope->scope = le32_to_cpu(buf[0]);
 	scope->decl_ids_len = le32_to_cpu(buf[1]);
-	assert(scope->decl_ids_len > 0);
+	if (scope->decl_ids_len == 0) {
+		ERR(fp->handle, "invalid scope with no declaration");
+		goto cleanup;
+	}
 	if ((scope->decl_ids =
 	     malloc(scope->decl_ids_len * sizeof(uint32_t))) == NULL) {
 		goto cleanup;
Signature · 1 line
2.10.2
#1 lore raw

PATCH 2/3 libsepol: test for ebitmap_read() negative return value

While fuzzing hll/pp, the fuzzer (AFL) crafted a policy which triggered the following message without making the policy loading fail (the program crashed with a segmentation fault later):

security: ebitmap: map size 192 does not match my size 64 (high bit was 0)

This is because ebitmap_read() returned -EINVAL and this value was handled as a successful return value by scope_index_read() because it was not -1.

---
 libsepol/src/policydb.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
libsepol/src/policydb.c +2 -2
index e36d82f0a1e8..b112fd5465b5 100644
@@ -3447,7 +3447,7 @@ static int scope_index_read(scope_index_t * scope_index,
 	int rc;
 
 	for (i = 0; i < num_scope_syms; i++) {
-		if (ebitmap_read(scope_index->scope + i, fp) == -1) {
+		if (ebitmap_read(scope_index->scope + i, fp) < 0) {
 			return -1;
 		}
 	}
@@ -3465,7 +3465,7 @@ static int scope_index_read(scope_index_t * scope_index,
 		return -1;
 	}
 	for (i = 0; i < scope_index->class_perms_len; i++) {
-		if (ebitmap_read(scope_index->class_perms_map + i, fp) == -1) {
+		if (ebitmap_read(scope_index->class_perms_map + i, fp) < 0) {
 			return -1;
 		}
 	}
Signature · 1 line
2.10.2
#2 lore raw

Re: [PATCH 2/3] libsepol: test for ebitmap_read() negative return value

Quoted 10 lines · Nicolas Iooss

While fuzzing hll/pp, the fuzzer (AFL) crafted a policy which triggered the following message without making the policy loading fail (the program crashed with a segmentation fault later):

security: ebitmap: map size 192 does not match my size 64 (high bit was 0)

This is because ebitmap_read() returned -EINVAL and this value was handled as a successful return value by scope_index_read() because it was not -1.

I'll apply this, but this is a symptom of a wider problem within libsepol with respect to mixed error return values that we should someday fix. It is a historical result of the fact that originally the core checkpolicy/libsepol code was shared in common between the kernel security server and checkpolicy/libsepol, and therefore used kernel return code conventions. We should probably convert it all to set errno and return -1 but that obviously will take time.

Quoted 29 lines
---
 libsepol/src/policydb.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
libsepol/src/policydb.c +2 -2
index e36d82f0a1e8..b112fd5465b5 100644
@@ -3447,7 +3447,7 @@ static int scope_index_read(scope_index_t * scope_index,
 	int rc;
 
 	for (i = 0; i < num_scope_syms; i++) {
-		if (ebitmap_read(scope_index->scope + i, fp) == -1) {
+		if (ebitmap_read(scope_index->scope + i, fp) < 0) {
 			return -1;
 		}
 	}
@@ -3465,7 +3465,7 @@ static int scope_index_read(scope_index_t * scope_index,
 		return -1;
 	}
 	for (i = 0; i < scope_index->class_perms_len; i++) {
-		if (ebitmap_read(scope_index->class_perms_map + i, fp) == -1) {
+		if (ebitmap_read(scope_index->class_perms_map + i, fp) < 0) {
 			return -1;
 		}
 	}
#3 lore raw

PATCH 3/3 libsepol: make parsing symbol table headers more robust

In reply to Nicolas Iooss

When hll/pp loads a policy file which has been modified so that the nprim field of one of its non-empty symbol table was changed to zero, it crashes with a segmentation fault. A quick analysis leads to "p->sym_val_to_name[i] = (char **)alloc(p->symtab[i].nprim, sizeof(char *));" in policydb_index_others(), which is not executed when p->symtab[i].nprim is zero even though there are items in p->symtab[i].table.

Detect such an oddity in the policy file early to exit with a clean error message.

---
 libsepol/src/policydb.c | 8 ++++++++
 1 file changed, 8 insertions(+)
libsepol/src/policydb.c +8 -0
index b112fd5465b5..d1019e42de16 100644
@@ -3510,6 +3510,10 @@ static int avrule_decl_read(policydb_t * p, avrule_decl_t * decl,
 			return -1;
 		nprim = le32_to_cpu(buf[0]);
 		nel = le32_to_cpu(buf[1]);
+		if (nel && !nprim) {
+			ERR(fp->handle, "unexpected items in decl symbol table with no symbol");
+			return -1;
+		}
 		for (j = 0; j < nel; j++) {
 			if (read_f[i] (p, decl->symtab[i].table, fp)) {
 				return -1;
@@ -3881,6 +3885,10 @@ int policydb_read(policydb_t * p, struct policy_file *fp, unsigned verbose)
 			goto bad;
 		nprim = le32_to_cpu(buf[0]);
 		nel = le32_to_cpu(buf[1]);
+		if (nel && !nprim) {
+			ERR(fp->handle, "unexpected items in symbol table with no symbol");
+			goto bad;
+		}
 		for (j = 0; j < nel; j++) {
 			if (read_f[i] (p, p->symtab[i].table, fp))
 				goto bad;
Signature · 1 line
2.10.2
#4 lore raw

Re: [PATCH 3/3] libsepol: make parsing symbol table headers more robust

Quoted 10 lines · Nicolas Iooss

When hll/pp loads a policy file which has been modified so that the nprim field of one of its non-empty symbol table was changed to zero, it crashes with a segmentation fault. A quick analysis leads to "p->sym_val_to_name[i] = (char **)alloc(p->symtab[i].nprim, sizeof(char *));" in policydb_index_others(), which is not executed when p->symtab[i].nprim is zero even though there are items in p->symtab[i].table.

Detect such an oddity in the policy file early to exit with a clean error message.

I'll apply this but I'd like to know where the segmentation fault occurred. The index functions already check whether the value exceeds the nprim value, and therefore shouldn't try to set the _val_to_name[] entry.

Quoted 33 lines
---
 libsepol/src/policydb.c | 8 ++++++++
 1 file changed, 8 insertions(+)
libsepol/src/policydb.c +8 -0
index b112fd5465b5..d1019e42de16 100644
@@ -3510,6 +3510,10 @@ static int avrule_decl_read(policydb_t * p, avrule_decl_t * decl,
 			return -1;
 		nprim = le32_to_cpu(buf[0]);
 		nel = le32_to_cpu(buf[1]);
+		if (nel && !nprim) {
+			ERR(fp->handle, "unexpected items in decl symbol table with no symbol");
+			return -1;
+		}
 		for (j = 0; j < nel; j++) {
 			if (read_f[i] (p, decl->symtab[i].table, fp)) {
 				return -1;
@@ -3881,6 +3885,10 @@ int policydb_read(policydb_t * p, struct policy_file *fp, unsigned verbose)
 			goto bad;
 		nprim = le32_to_cpu(buf[0]);
 		nel = le32_to_cpu(buf[1]);
+		if (nel && !nprim) {
+			ERR(fp->handle, "unexpected items in symbol table with no symbol");
+			goto bad;
+		}
 		for (j = 0; j < nel; j++) {
 			if (read_f[i] (p, p->symtab[i].table, fp))
 				goto bad;
#5 lore raw

Re: [PATCH 3/3] libsepol: make parsing symbol table headers more robust

Quoted 9 lines · Stephen Smalley
Quoted 8 lines · Nicolas Iooss

When hll/pp loads a policy file which has been modified so that the nprim field of one of its non-empty symbol table was changed to zero, it crashes with a segmentation fault. A quick analysis leads to "p->sym_val_to_name[i] = (char **)alloc(p->symtab[i].nprim, sizeof(char *));" in policydb_index_others(), which is not executed when p->symtab[i].nprim is zero even though there are items in p->symtab[i].table.

Quoted 7 lines
Quoted 2 lines

Detect such an oddity in the policy file early to exit with a clean error message.

I'll apply this but I'd like to know where the segmentation fault occurred. The index functions already check whether the value exceeds the nprim value, and therefore shouldn't try to set the _val_to_name[] entry.

Thanks. The segfault occurs in required_scopes_to_cil() in module_to_cil.c. Here is a gdb session.

% gdb-run ./DESTDIR/usr/libexec/selinux/hll/pp afl-out/pp/crashes.2016-11-15-16:49:07/id:000000,sig:11,*

Reading symbols from ./DESTDIR/usr/libexec/selinux/hll/pp...done. Starting program: ./DESTDIR/usr/libexec/selinux/hll/pp afl-out/pp/crashes.2016-11-15-16:49:07/id:000000,sig:11,src:000000,op:flip1,pos:109

Program received signal SIGSEGV, Segmentation fault. required_scopes_to_cil (decl_stack=0x6030b0, block=0x606780, pdb=0x6032e0, indent=0) at module_to_cil.c:3476
3476 key = pdb->sym_val_to_name[sym][i];
=> 0x00007ffff7b6c708 <block_to_cil+1224>: 4a 8b 04 e8 mov
(%rax,%r13,8),%rax
(gdb) bt
#0 required_scopes_to_cil (decl_stack=0x6030b0, block=0x606780, pdb=0x6032e0, indent=0) at module_to_cil.c:3476
#1 block_to_cil (pdb=0x6032e0, block=0x606780, stack=0x6030b0, indent=0) at module_to_cil.c:3622
#2 0x00007ffff7b6e0c6 in blocks_to_cil (pdb=0x6032e0) at module_to_cil.c:3760
#3 sepol_module_policydb_to_cil (fp=fp@entry=0x7ffff7b405e0 <_IO_2_1_stdout_>, pdb=0x6032e0, linked=linked@entry=0) at module_to_cil.c:4047
#4 0x00007ffff7b6e3c6 in sepol_module_package_to_cil (fp=fp@entry=0x7ffff7b405e0
<_IO_2_1_stdout_>, mod_pkg=0x603280) at module_to_cil.c:4076
#5 0x0000000000400e76 in main (argc=<optimized out>, argv=<optimized out>) at pp.c:150
(gdb) list
3471 map = decl->required.scope[sym];
3472 ebitmap_for_each_bit(&map, node, i) {
3473 if (!ebitmap_get_bit(&map, i)) {
3474 continue;
3475 }
3476 key = pdb->sym_val_to_name[sym][i];
3477
3478 scope_datum =
hashtab_search(pdb->scope[sym].table, key);
3479 for (j = 0; j < scope_datum->decl_ids_len;
j++) {
3480 if (scope_datum->decl_ids[j] ==
decl->decl_id) {

Nicolas

#6 lore raw

Re: [PATCH 3/3] libsepol: make parsing symbol table headers more robust

Quoted 58 lines · Nicolas Iooss

On Wed, Nov 16, 2016 at 3:12 PM, Stephen Smalley <[email protected] <mailto:[email protected]>> wrote:

On 11/15/2016 06:07 PM, Nicolas Iooss wrote:
> When hll/pp loads a policy file which has been modified so that the > nprim field of one of its non-empty symbol table was changed to zero, it > crashes with a segmentation fault. A quick analysis leads to > "p->sym_val_to_name[i] = (char **)alloc(p->symtab[i].nprim, sizeof(char > *));" in policydb_index_others(), which is not executed when > p->symtab[i].nprim is zero even though there are items in
> p->symtab[i].table.
>
> Detect such an oddity in the policy file early to exit with a clean > error message.

I'll apply this but I'd like to know where the segmentation fault occurred. The index functions already check whether the value exceeds the nprim value, and therefore shouldn't try to set the _val_to_name[] entry.

Thanks. The segfault occurs in required_scopes_to_cil() in module_to_cil.c. Here is a gdb session.

% gdb-run ./DESTDIR/usr/libexec/selinux/hll/pp afl-out/pp/crashes.2016-11-15-16:49:07/id:000000,sig:11,*

Reading symbols from ./DESTDIR/usr/libexec/selinux/hll/pp...done. Starting program: ./DESTDIR/usr/libexec/selinux/hll/pp afl-out/pp/crashes.2016-11-15-16:49:07/id:000000,sig:11,src:000000,op:flip1,pos:109

Program received signal SIGSEGV, Segmentation fault. required_scopes_to_cil (decl_stack=0x6030b0, block=0x606780, pdb=0x6032e0, indent=0) at module_to_cil.c:3476
3476 key = pdb->sym_val_to_name[sym][i];
=> 0x00007ffff7b6c708 <block_to_cil+1224>: 4a 8b 04 e8 mov
(%rax,%r13,8),%rax
(gdb) bt
#0 required_scopes_to_cil (decl_stack=0x6030b0, block=0x606780, pdb=0x6032e0, indent=0) at module_to_cil.c:3476
#1 block_to_cil (pdb=0x6032e0, block=0x606780, stack=0x6030b0, indent=0) at module_to_cil.c:3622
#2 0x00007ffff7b6e0c6 in blocks_to_cil (pdb=0x6032e0) at module_to_cil.c:3760
#3 sepol_module_policydb_to_cil (fp=fp@entry=0x7ffff7b405e0 <_IO_2_1_stdout_>, pdb=0x6032e0, linked=linked@entry=0) at module_to_cil.c:4047
#4 0x00007ffff7b6e3c6 in sepol_module_package_to_cil (fp=fp@entry=0x7ffff7b405e0 <_IO_2_1_stdout_>, mod_pkg=0x603280) at module_to_cil.c:4076
#5 0x0000000000400e76 in main (argc=<optimized out>, argv=<optimized out>) at pp.c:150
(gdb) list
3471 map = decl->required.scope[sym];
3472 ebitmap_for_each_bit(&map, node, i) {
3473 if (!ebitmap_get_bit(&map, i)) {
3474 continue;
3475 }

Quoted 8 lines

3476 key = pdb->sym_val_to_name[sym][i];
3477
3478 scope_datum =
hashtab_search(pdb->scope[sym].table, key);
3479 for (j = 0; j <
scope_datum->decl_ids_len; j++) {
3480 if (scope_datum->decl_ids[j] ==
decl->decl_id) {

So, don't we need to also check that i < pdb->symtab[sym].nprim before line 3476?

#7 lore raw

Re: [PATCH 3/3] libsepol: make parsing symbol table headers more robust

Quoted 73 lines · Stephen Smalley
Quoted 6 lines · Nicolas Iooss

On Wed, Nov 16, 2016 at 3:12 PM, Stephen Smalley <[email protected] <mailto:[email protected]>> wrote:

On 11/15/2016 06:07 PM, Nicolas Iooss wrote:
> When hll/pp loads a policy file which has been modified so that the > nprim field of one of its non-empty symbol table was changed to

zero, it

Quoted 2 lines
Quoted 2 lines

crashes with a segmentation fault. A quick analysis leads to "p->sym_val_to_name[i] = (char **)alloc(p->symtab[i].nprim,

sizeof(char

Quoted 9 lines
Quoted 6 lines

*));" in policydb_index_others(), which is not executed when p->symtab[i].nprim is zero even though there are items in p->symtab[i].table.

Detect such an oddity in the policy file early to exit with a clean error message.

I'll apply this but I'd like to know where the segmentation fault occurred. The index functions already check whether the value

exceeds

Quoted 1 line

the nprim value, and therefore shouldn't try to set the

_val_to_name[]

Quoted 6 lines

entry.

Thanks. The segfault occurs in required_scopes_to_cil() in module_to_cil.c. Here is a gdb session.

Quoted 6 lines

% gdb-run ./DESTDIR/usr/libexec/selinux/hll/pp afl-out/pp/crashes.2016-11-15-16:49:07/id:000000,sig:11,*

Reading symbols from ./DESTDIR/usr/libexec/selinux/hll/pp...done. Starting program: ./DESTDIR/usr/libexec/selinux/hll/pp afl-out/pp/crashes.2016-11-15-16:49:07/id:000000,sig:11,src:

000000,op:flip1,pos:109

Quoted 28 lines

Program received signal SIGSEGV, Segmentation fault. required_scopes_to_cil (decl_stack=0x6030b0, block=0x606780, pdb=0x6032e0, indent=0) at module_to_cil.c:3476
3476 key = pdb->sym_val_to_name[sym][i];
=> 0x00007ffff7b6c708 <block_to_cil+1224>: 4a 8b 04 e8 mov
(%rax,%r13,8),%rax
(gdb) bt
#0 required_scopes_to_cil (decl_stack=0x6030b0, block=0x606780, pdb=0x6032e0, indent=0) at module_to_cil.c:3476
#1 block_to_cil (pdb=0x6032e0, block=0x606780, stack=0x6030b0, indent=0) at module_to_cil.c:3622
#2 0x00007ffff7b6e0c6 in blocks_to_cil (pdb=0x6032e0) at module_to_cil.c:3760
#3 sepol_module_policydb_to_cil (fp=fp@entry=0x7ffff7b405e0 <_IO_2_1_stdout_>, pdb=0x6032e0, linked=linked@entry=0) at module_to_cil.c:4047
#4 0x00007ffff7b6e3c6 in sepol_module_package_to_cil (fp=fp@entry=0x7ffff7b405e0 <_IO_2_1_stdout_>, mod_pkg=0x603280) at module_to_cil.c:4076
#5 0x0000000000400e76 in main (argc=<optimized out>, argv=<optimized out>) at pp.c:150
(gdb) list
3471 map = decl->required.scope[sym];
3472 ebitmap_for_each_bit(&map, node, i) {
3473 if (!ebitmap_get_bit(&map, i)) {
3474 continue;
3475 }

Quoted 8 lines

3476 key = pdb->sym_val_to_name[sym][i];
3477
3478 scope_datum =
hashtab_search(pdb->scope[sym].table, key);
3479 for (j = 0; j <
scope_datum->decl_ids_len; j++) {
3480 if (scope_datum->decl_ids[j] ==
decl->decl_id) {

Quoted 3 lines

So, don't we need to also check that i < pdb->symtab[sym].nprim before line 3476?

As i is an iterator on decl->required.scope[sym] bitmap, it makes more sense to compare the bitmap high bit value with pdb->symtab[sym].nprim when it is loaded in policydb.c. More precisely, from an API perspective, it makes sense for sepol_module_policydb_to_cil() to expect a well-formed policy database as input. I see this issue as the fact that policydb_read() can load an invalid policy file and still returns a successful return value.

Right now I have no time to investigate/write a patch, so I am sending to the mailing list the files I am using to reproduce the issue (basic.pp is the file I gave to the fuzzer and the other attachment is the invalid policy which made pp crash before the patch I sent yesterday).

Nicolas

#8 lore raw

Re: [PATCH 3/3] libsepol: make parsing symbol table headers more robust

In reply to Nicolas Iooss
Quoted 8 lines · Nicolas Iooss

When hll/pp loads a policy file which has been modified so that the nprim field of one of its non-empty symbol table was changed to zero, it crashes with a segmentation fault. A quick analysis leads to "p->sym_val_to_name[i] = (char **)alloc(p->symtab[i].nprim, sizeof(char *));" in policydb_index_others(), which is not executed when p->symtab[i].nprim is zero even though there are items in p->symtab[i].table.

Quoted 4 lines

Detect such an oddity in the policy file early to exit with a clean error message.

Thanks, applied all three.

Quoted 31 lines
---
 libsepol/src/policydb.c | 8 ++++++++
 1 file changed, 8 insertions(+)
libsepol/src/policydb.c +8 -0
index b112fd5465b5..d1019e42de16 100644
@@ -3510,6 +3510,10 @@ static int avrule_decl_read(policydb_t * p, avrule_decl_t * decl,
 			return -1;
 		nprim = le32_to_cpu(buf[0]);
 		nel = le32_to_cpu(buf[1]);
+		if (nel && !nprim) {
+			ERR(fp->handle, "unexpected items in decl symbol table with no symbol");
+			return -1;
+		}
 		for (j = 0; j < nel; j++) {
 			if (read_f[i] (p, decl->symtab[i].table, fp)) {
 				return -1;
@@ -3881,6 +3885,10 @@ int policydb_read(policydb_t * p, struct policy_file *fp, unsigned verbose)
 			goto bad;
 		nprim = le32_to_cpu(buf[0]);
 		nel = le32_to_cpu(buf[1]);
+		if (nel && !nprim) {
+			ERR(fp->handle, "unexpected items in symbol table with no symbol");
+			goto bad;
+		}
 		for (j = 0; j < nel; j++) {
 			if (read_f[i] (p, p->symtab[i].table, fp))
 				goto bad;