odd segfault in checkpolicy
I've been trying to track down an odd segfault when checkpolicy is compiled without optimization, but I've been looking at the code for like an hour and a half now and I just can't see the problem. Here's some debugging information:
walters@nexus> make CFLAGS='-g -O0'
[build output elided]
walters@nexus> gdb --args ./checkpolicy -b -d ~/tmp/policy.15
GNU gdb Red Hat Linux (5.3.90-0.20030710.41rh)
Copyright 2003 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-redhat-linux-gnu"...Using host libthread_db library "/lib/tls/libthread_db.so.1".
(gdb) r
Starting program: /src/research/checkpolicy-1.4/checkpolicy -b -d /home/walters/tmp/policy.15 /src/research/checkpolicy-1.4/checkpolicy: loading policy configuration from /home/walters/tmp/policy.15
Program received signal SIGSEGV, Segmentation fault. 0x0804c7ae in policydb_read (p=0x8062a40, fp=0xbffff628) at policydb.c:993
993 buf[i] = le32_to_cpu(buf[i]);
(gdb) bt
#0 0x0804c7ae in policydb_read (p=0x8062a40, fp=0xbffff628) at policydb.c:993 #1 0x0804e86f in security_load_policy (data=0x40012000, len=1150156) at services.c:921 #2 0x08058f55 in main (argc=4, argv=0xbffff9c4) at checkpolicy.c:373
(gdb) p buf
$1 = (__u32 *) 0x40012000
(gdb) p/x buf[0]
$2 = 0xf97cff8c
(gdb) p buf[1]
$3 = 8
This problem disappears if I recompile with the default optimization (just 'make'). I started to suspect a gcc bug...but every time so far that I suspected that I've been wrong :) I can reproduce the problem using the default gcc on FC1 (3.3.2), as well as the default gcc on Debian woody (2.95.4).
Looking at the code, I suspected it was a problem with the "next_entry" inline function - but I don't see a bug there. Note above that buf[0] does match SELINUX_MAGIC.
Does anyone have any ideas? Can anyone else reproduce this?
Quoted 4 lines · Colin Walters
I've been trying to track down an odd segfault when checkpolicy is compiled without optimization, but I've been looking at the code for like an hour and a half now and I just can't see the problem. Here's some debugging information:
<snip>
Quoted 1 line
Does anyone have any ideas? Can anyone else reproduce this?
Yes. Patch below. Since checkpolicy is rewriting the buffer in place (to perform endian conversion), it needs to map the policy file read-write and private, rather than read-only and shared.
Index: checkpolicy/checkpolicy.c
===================================================================
RCS file: /nfshome/pal/CVS/selinux-usr/checkpolicy/checkpolicy.c,v retrieving revision 1.16
checkpolicy.c +1 -1
@@ -502,7 +502,7 @@ file, strerror(errno));exit (1 ); } - map =mmap (NULL , sb.st_size,PROT_READ ,MAP_SHARED , fd,0 ); + map =mmap (NULL , sb.st_size,PROT_READ |PROT_WRITE ,MAP_PRIVATE , fd,0 );if (map ==MAP_FAILED ) {fprintf (stderr,"Can't map '%s': %s\n" , file,strerror (errno));
Signature · 8 lines
Stephen Smalley <[email protected]> National Security Agency -- This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.
Quoted 3 lines · Stephen Smalley
Yes. Patch below. Since checkpolicy is rewriting the buffer in place (to perform endian conversion), it needs to map the policy file read-write and private, rather than read-only and shared.
Ah, that makes sense. On little-endian machines, the statement
buf[i] = le32_to_cpu(buf[i]);
became
buf[i] = buf[i];
so gcc optimized it to nothing with -O2. But on big-endian machines, it should have never worked, and it kind of surprises me no one has apparently run SELinux on a big-endian machine, at least recently.
Quoted 7 lines · Colin Walters
Ah, that makes sense. On little-endian machines, the statement
buf[i] =le32_to_cpu(buf[i]);
became
buf[i] = buf[i];
so gcc optimized it to nothing with -O2. But on big-endian machines, it should have never worked, and it kind of surprises me no one has apparently run SELinux on a big-endian machine, at least recently.
Most people don't try to load binary policies with checkpolicy; they only use it to compile policy.conf, which wouldn't run into this problem.
Signature · 8 lines
Stephen Smalley <[email protected]> National Security Agency -- This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.