xmikos/setools-android

ERROR: policydb magic number 0x000008 does not match expected magic number 0xf97cff8c or 0xf97cff8d

MaxChinni opened this issue · 5 comments

I receive an error inspecting /sys/fs/selinux/policy:

$ adb root
adbd is already running as root
$ adb push sesearch /data/local/tmp/
3228 KB/s (243004 bytes in 0.073s)
$ adb shell /data/local/tmp/sesearch -A /sys/fs/selinux/policy
ERROR: policydb magic number 0x000008 does not match expected magic number 0xf97cff8c or 0xf97cff8d
ERROR: Unable to open policy /sys/fs/selinux/policy.
ERROR: Success

This is weird because if I copy that file on the /sdcard/ path, I can inspect it:

$ adb shell cp /sys/fs/selinux/policy /sdcard/
$ adb shell /data/local/tmp/sesearch -A -s system_app -t system_data_file -c file -p write /sdcard/policy
Found 1 semantic av rules:
   allow system_app system_data_file : file { ioctl read write create getattr setattr lock append unlink link rename open } ;

The copied file has the same content as the source:

$ adb shell md5sum /sys/fs/selinux/policy /sdcard/policy
e164e15e24e861f56ce827883e11b6b4  /sys/fs/selinux/policy
e164e15e24e861f56ce827883e11b6b4  /sdcard/policy

Setting SELinux to "Permissive" does not change the behavior.

Am I missing something?

I can confirm this, /sys/fs/selinux/policy have to be copied to another location or else sesearch and seinfo could not read it. I don't know the reason, but I suspect that selinuxfs (virtual filesystem mounted on /sys/fs/selinux) doesn't support some file descriptor operations/flags that those utilities use. I will look into it when I have time...

Ok, I have run strace on seinfo and it seems I was right. seinfo uses lseek() function on opened policy file, but selinuxfs seems it doesn't support lseek. It returns -1 ESPIPE (Illegal seek) error:

openat(AT_FDCWD, "/sys/fs/selinux/policy", O_RDONLY|O_LARGEFILE) = 4
fstat64(4, {st_mode=S_IFREG|0444, st_size=130458, ...}) = 0
brk(0xb70dc000)                         = 0xb70dc000
mprotect(0xb6f2d000, 4096, PROT_READ|PROT_WRITE) = 0
mprotect(0xb6f2d000, 4096, PROT_READ)   = 0
read(4, "\214\377|\371\10\0\0\0SE Linux\32\0\0\0\1\0\0\0\10\0\0\0\7\0\0\0"..., 4096) = 4096
close(4)                                = 0
openat(AT_FDCWD, "/sys/fs/selinux/policy", O_RDONLY|O_LARGEFILE) = 4
fstat64(4, {st_mode=S_IFREG|0444, st_size=130458, ...}) = 0
brk(0xb70dd000)                         = 0xb70dd000
mprotect(0xb6f2d000, 4096, PROT_READ|PROT_WRITE) = 0
mprotect(0xb6f2d000, 4096, PROT_READ)   = 0
read(4, "\214\377|\371\10\0\0\0SE Linux\32\0\0\0\1\0\0\0\10\0\0\0\7\0\0\0"..., 4096) = 4096
lseek(4, 0, SEEK_CUR)                   = -1 ESPIPE (Illegal seek)
lseek(4, 0, SEEK_SET)                   = -1 ESPIPE (Illegal seek)
write(2, "ERROR: ", 7ERROR: )                  = 7
write(2, "policydb magic number 0x000008 d"..., 92policydb magic number 0x000008 does not match expected magic number 0xf97cff8c or 0xf97cff8d) = 92

I have found patch fixing selinuxfs.c to support lseek() that should already be in the Linux kernel:
http://permalink.gmane.org/gmane.linux.kernel.commits.head/318251

So it seems Android is running too old kernel version...

According to commit info from Linux kernel tree mirror on GitHub it seems you need at least Linux 3.5 for this to work: torvalds/linux@47a93a5

Linux 3.5 is from 2012, so 3 years old! It's really sad that Android kernel is so outdated :-(

That's really a great analysis, so the problem seems out of this project.
By the way, I'm so close... Hell's bells!

shell@A0001:/ $ uname -r
3.4.67-cyanogenmod-ge9b3515