libsepol: make capability index an unsigned int
PATCH 1/1 libsepol: make capability index an unsigned int
When sepol_polcap_getname() is called with a negative capnum, it dereferences polcap_names[capnum] which produces a segmentation fault most of the time.
For information, here is a gdb session when hll/pp loads a policy module which has been mutated by American Fuzzy Lop:
Program received signal SIGSEGV, Segmentation fault.
sepol_polcap_getname (capnum=capnum@entry=-4259840) at polcaps.c:34
34 return polcap_names[capnum];
=> 0x00007ffff7a8da07 <sepol_polcap_getname+135>: 48 8b 04 f8 mov
(%rax,%rdi,8),%rax
(gdb) bt
#0 sepol_polcap_getname (capnum=capnum@entry=-4259840) at
polcaps.c:34
#1 0x00007ffff7a7c440 in polcaps_to_cil (pdb=0x6042e0) at
module_to_cil.c:2492
#2 sepol_module_policydb_to_cil (fp=fp@entry=0x7ffff79c75e0
<_IO_2_1_stdout_>, pdb=0x6042e0, linked=linked@entry=0) at
module_to_cil.c:4039
#3 0x00007ffff7a7e695 in sepol_module_package_to_cil
(fp=fp@entry=0x7ffff79c75e0 <_IO_2_1_stdout_>, mod_pkg=0x604280) at
module_to_cil.c:4087
#4 0x0000000000401acc in main (argc=<optimized out>,
argv=<optimized out>) at pp.c:150--- libsepol/include/sepol/policydb/polcaps.h | 2 +- libsepol/src/polcaps.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
libsepol/include/sepol/policydb/polcaps.h +1 -1
@@ -19,7 +19,7 @@ enum {extern int sepol_polcap_getnum (const char *name);/* Convert a capability number to name. */ -extern const char *sepol_polcap_getname (int capnum); +extern const char *sepol_polcap_getname (unsigned int capnum); #ifdef __cplusplus }
libsepol/src/polcaps.c +1 -1
@@ -26,7 +26,7 @@ int sepol_polcap_getnum(const char *name)return -1 ; } -const char *sepol_polcap_getname (int capnum) +const char *sepol_polcap_getname (unsigned int capnum) {if (capnum >POLICYDB_CAPABILITY_MAX )return NULL ;
Signature · 1 line
2.11.0
Quoted 38 lines · Nicolas Iooss
When
sepol_polcap_getname()is called with a negative capnum, it dereferences polcap_names[capnum] which produces a segmentation fault most of the time.For information, here is a gdb session when hll/pp loads a policy module which has been mutated by American Fuzzy Lop:
Program received signal SIGSEGV, Segmentation fault. sepol_polcap_getname (capnum=capnum@entry=-4259840) at polcaps.c:34 34 return polcap_names[capnum]; => 0x00007ffff7a8da07 <sepol_polcap_getname+135>: 48 8b 04 f8 mov (%rax,%rdi,8),%rax (gdb) bt #0 sepol_polcap_getname (capnum=capnum@entry=-4259840) at polcaps.c:34 #1 0x00007ffff7a7c440 in polcaps_to_cil (pdb=0x6042e0) at module_to_cil.c:2492 #2 sepol_module_policydb_to_cil (fp=fp@entry=0x7ffff79c75e0 <_IO_2_1_stdout_>, pdb=0x6042e0, linked=linked@entry=0) at module_to_cil.c:4039 #3 0x00007ffff7a7e695 in sepol_module_package_to_cil (fp=fp@entry=0x7ffff79c75e0 <_IO_2_1_stdout_>, mod_pkg=0x604280) at module_to_cil.c:4087 #4 0x0000000000401acc in main (argc=<optimized out>, argv=<optimized out>) at pp.c:150Signed-off-by Nicolas Iooss <[email protected]>--- libsepol/include/sepol/policydb/polcaps.h | 2 +- libsepol/src/polcaps.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)libsepol/include/sepol/policydb/polcaps.h +0
-0
libsepol/include/sepol/policydb/polcaps.h enum { · line 22
Quoted 5 lines
libsepol/include/sepol/policydb/polcaps.h +1
-1@@ -19,7 +19,7 @@ enum {extern int sepol_polcap_getnum (const char *name);/* Convert a capability number to name. */ -extern const char *sepol_polcap_getname (int capnum); +extern const char *sepol_polcap_getname (unsigned int capnum);
unsigned is definitely a way to fix this, but now I see that sepol_polcap_getnum() wouldn't be able
to use the full range of unsigned as its return of < 0 indicates error.
Perhaps use the enum types in both cases? However, based on compiler flags, IIRC, enums may be treated as signed or unsigned int.
It's really likely never a problem, but just something to
mention.
Quoted 8 lines
libsepol/include/sepol/policydb/polcaps.h +0
-0#ifdef __cplusplus }libsepol/src/polcaps.c +0
-0
libsepol/src/polcaps.c int sepol_polcap_getnum(const char *name) · line 32
Quoted 8 lines
libsepol/src/polcaps.c +1
-1@@ -26,7 +26,7 @@ int sepol_polcap_getnum(const char *name)return -1 ; } -const char *sepol_polcap_getname (int capnum) +const char *sepol_polcap_getname (unsigned int capnum) {if (capnum >POLICYDB_CAPABILITY_MAX )return NULL ;
There is a possibility that this enum and the strings could get out of sync and then we would also want
to bound this value (sizeof(polcap_names)/sizeof(polcap_names[0]))) -1 We would be able to drop the -1 if the NULL on polcap_names can go away...
We could also detect this mismatch at compile time:
typedef int WRONG_POLCAP_SIZE [((sizeof(polcap_names)/sizeof(polcap_names[0]))) -1 == POLICYDB_CAPABILITY_MAX) - 1];
-1 size arrays are illegal in C, so we want the array to be 0 length on correct size, -1 on bad size. That above code snippet might not be 100% but that should be close.
Quoted 8 lines
--
2.11.0_______________________________________________
Selinux mailing list
[email protected]
To unsubscribe, send email to [email protected].
To get help, send an email containing "help" to [email protected].
Signature · 3 lines
Respectfully, William C Roberts
Quoted 51 lines · William Roberts
Quoted 27 lines · Nicolas Iooss
When
sepol_polcap_getname()is called with a negative capnum, it dereferences polcap_names[capnum] which produces a segmentation fault most of the time.For information, here is a gdb session when hll/pp loads a policy module which has been mutated by American Fuzzy Lop:
Program received signal SIGSEGV, Segmentation fault. sepol_polcap_getname (capnum=capnum@entry=-4259840) at polcaps.c:34 34 return polcap_names[capnum]; => 0x00007ffff7a8da07 <sepol_polcap_getname+135>: 48 8b 04 f8 mov (%rax,%rdi,8),%rax (gdb) bt #0 sepol_polcap_getname (capnum=capnum@entry=-4259840) at polcaps.c:34 #1 0x00007ffff7a7c440 in polcaps_to_cil (pdb=0x6042e0) at module_to_cil.c:2492 #2 sepol_module_policydb_to_cil (fp=fp@entry=0x7ffff79c75e0 <_IO_2_1_stdout_>, pdb=0x6042e0, linked=linked@entry=0) at module_to_cil.c:4039 #3 0x00007ffff7a7e695 in sepol_module_package_to_cil (fp=fp@entry=0x7ffff79c75e0 <_IO_2_1_stdout_>, mod_pkg=0x604280) at module_to_cil.c:4087 #4 0x0000000000401acc in main (argc=<optimized out>, argv=<optimized out>) at pp.c:150Review
Quoted 7 lines
Signed-off-by Nicolas Iooss <[email protected]>--- libsepol/include/sepol/policydb/polcaps.h | 2 +- libsepol/src/polcaps.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)(quoted diff) +0
-0
b/libsepol/include/sepol/policydb/polcaps.hlibsepol/include/sepol/policydb/polcaps.h enum { · line 22
Quoted 9 lines
index
c9e40f62048d..623818ab10f5100644libsepol/include/sepol/policydb/polcaps.h +1
-1@@ -19,7 +19,7 @@ enum {extern int sepol_polcap_getnum (const char *name);/* Convert a capability number to name. */ -extern const char *sepol_polcap_getname (int capnum); +extern const char *sepol_polcap_getname (unsigned int capnum);unsigned is definitely a way to fix this, but now I see that
sepol_polcap_getnum()wouldn't be able
to use the full range of unsigned as its return of < 0 indicates error.
Quoted 6 lines
Perhaps use the enum types in both cases? However, based on compiler flags, IIRC, enums may be treated as signed or unsigned int.
It's really likely never a problem, but just something to
mention.
I prefer not to use enum in both sepol_polcap_getname() and sepol_polcap_getnum(). For the first function because it is always called while iterating the policycaps bitmap of a policy so the meaning of the parameter is really "bit position in a bitmap" instead of "the compiler-generated index of the item in the enum" and for the second one because every caller expects the result to be -1 or positive. By the way it looks funny that "git grep -A5 sepol_polcap_getname" shows three callers with three different ways to compare the result (capnum < 0, capnum == -1 or capnum == SEPOL_ERR).
Quoted 22 lines
Quoted 8 lines
libsepol/include/sepol/policydb/polcaps.h +0
-0#ifdef __cplusplus }libsepol/src/polcaps.c +0
-0libsepol/src/polcaps.c int sepol_polcap_getnum(const char *name) · line 32
Quoted 8 lines
libsepol/src/polcaps.c +1
-1@@ -26,7 +26,7 @@ int sepol_polcap_getnum(const char *name)return -1 ; } -const char *sepol_polcap_getname (int capnum) +const char *sepol_polcap_getname (unsigned int capnum) {if (capnum >POLICYDB_CAPABILITY_MAX )return NULL ;There is a possibility that this enum and the strings could get out of sync and then we would also want
to bound this value (sizeof(polcap_names)/sizeof(polcap_names[0]))) -1 We would be able to drop the -1 if the NULL onpolcap_namescan go away...
Quoted 7 lines
We could also detect this mismatch at compile time:
typedef intWRONG_POLCAP_SIZE[((sizeof(polcap_names)/sizeof(polcap_names[0]))) -1 ==POLICYDB_CAPABILITY_MAX) - 1];
-1 size arrays are illegal in C, so we want the array to be 0 length on correct size, -1 on bad size. That above code snippet might not be 100% but that should be close.
I will wait to hear what the project developers think of introducing such a construction as there are several ways to achieve the same result: using a global typedef with a conditionally-sized array,
using gcc's __attribute__((error(message))) construction like the kernel does [1][2], using ((void)sizeof(char[1 - 2 * condition])) like the kernel also does [3], or even using static_assert(expr, message) introduced by C11 standard.
Thanks for your comments,
Nicolas
[1]
https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/include/linux/compiler-gcc.h?h=v4.9#n186
[2]
https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/include/linux/compiler.h?h=v4.9#n496
[3]
https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/include/linux/compiler.h?h=v4.9#n489
Stephen Smalley
AppliedQuoted 31 lines · Nicolas Iooss
When
sepol_polcap_getname()is called with a negative capnum, it dereferences polcap_names[capnum] which produces a segmentation fault most of the time.For information, here is a gdb session when hll/pp loads a policy module
which has been mutated by American Fuzzy Lop:Program received signal SIGSEGV, Segmentation fault.
sepol_polcap_getname(capnum=capnum@entry=-4259840) at polcaps.c:34
34 return polcap_names[capnum];
=>0x00007ffff7a8da07<sepol_polcap_getname+135>: 48 8b 04 f8 mov
(%rax,%rdi,8),%rax(gdb) bt
#0 sepol_polcap_getname (capnum=capnum@entry=-4259840) at polcaps.c:34
#1 0x00007ffff7a7c440 inpolcaps_to_cil(pdb=0x6042e0) at module_to_cil.c:2492
#2 sepol_module_policydb_to_cil (fp=fp@entry=0x7ffff79c75e0 <_IO_2_1_stdout_>, pdb=0x6042e0, linked=linked@entry=0) at module_to_cil.c:4039
#3 0x00007ffff7a7e695 insepol_module_package_to_cil(fp=fp@entry=0x7ffff79c75e0 <_IO_2_1_stdout_>, mod_pkg=0x604280) at
module_to_cil.c:4087
#4 0x0000000000401acc in main (argc=<optimized out>, argv=<optimized out>) at pp.c:150
Quoted 1 line
Signed-off-by Nicolas Iooss <[email protected]>
Thanks, applied.
Quoted 32 lines
---
libsepol/include/sepol/policydb/polcaps.h | 2 +-
libsepol/src/polcaps.c | 2 +-2 files changed, 2 insertions(+), 2 deletions(-)libsepol/include/sepol/policydb/polcaps.h +0
-0@@ -19,7 +19,7 @@ enum { extern int sepol_polcap_getnum(const char *name);
/* Convert a capability number to name. */libsepol/include/sepol/policydb/polcaps.h +1
-1-extern const char *sepol_polcap_getname (int capnum); +extern const char *sepol_polcap_getname (unsigned int capnum);
#ifdef__cplusplus
}libsepol/src/polcaps.c +0
-0@@ -26,7 +26,7 @@ int sepol_polcap_getnum(const char *name) return -1; }libsepol/src/polcaps.c +1
-1-const char *sepol_polcap_getname (int capnum) +const char *sepol_polcap_getname (unsigned int capnum){
if (capnum >POLICYDB_CAPABILITY_MAX)
return NULL;