Android P SElinux权限调试

Android P SElinux权限调试

在Android P上要开发一个开机过程中运行bin程序,在Android O上权限问题还算比较好解决,而在 Android P上面由于谷歌收紧了 Android SElinux控制,增加了许多neverallow规则,导致调试权限十分不便

开发的bin程序由于要开机运行,因此需要通过init.rc去启动这个bin程序severe。而这个bin程序要去控制“data”分区文件和“sys”文件节点状态。

因此该功能涉及到selinux权限有两部分,一部分是sys下的权限,一部分是data下的权限

在userdebug下SElinux 权限调试模式

由于该功能在开机阶段,不能直接用setenforce 0方式去设置SElinux权限为permissive进行debug,可以采用在自己的te文件添加对自己该部分的permissive权限用于调试,以此bin程序举例

type egbin, domain;
type egbin_exec, exec_type, file_type;
permissive egbin;
#注意:该功能只能用在userdebug、eng版本,user版本会导致编译不通过
init_daemo_domain(egbin)

sys下的权限

由于Android P收紧的对 sysfs 的权限,导致直接配置对sysfs的SElinux权限,会触发编译时的neverallow规则,如

……
allow egbin sysfs:file { read getattr open ioctl }
allow egbin sysfs:dir { search }
……

上述规则在Android P之前是可以编译通过的,在Android P上是不可以的。推测原因可能是Android P收紧了对“sys”底下的文件权限,如果直接给应用sysfs权限,会导致应用权限太大,可以在sys下干许多事情,很不安全。因此需要细化sys下的权限,对于上面:

  • 问题需要在file.te和file_contexts下添加对于sys具体目录下的组别
#Path:/device/qcom/sepolicy/vendor/common/file.te 
##file_contexts
#egbin
sys/fs/features(/.*)?     u:object_r:sysfs_egbin:s0
sys/fs/sda(/.*)?        u:object_r:sysf_egbin:s0
#Path:/device/qcom/sepolicy/vendor/common/file_contexts
##file.te
#egbin
type sysfs_egbin, fs_type, sysfs_type
  • 在自己的te文件重新定义规则
……
allow egbin sysfs_egbin:file { read getattr open ioctl }
allow egbin sysfs_egbin:dir { search }
……
  • 注意(一个坑)

按道理这样就已经没有什么问题了,但是在debug的时候还有报sys下权限问题,此时可能不是权限配的有问题,有可能是在file.te 中配置的规则不生效,如在sys/fs/看到的情况如下:

drwxr-xr-x  4 root root u:object_r:sysfs:s0      0 1970-01-29 19:16 .
drwxr-xr-x 11 root root u:object_r:sysfs:s0      0 1970-01-29 19:16 ..
drwxr-xr-x  2 root root u:object_r:sysfs_egbin:s0 0 1970-01-29 19:16 features
drwxr-xr-x  2 root root u:object_r:sysfs:s0      0 2018-10-30 18:16 sda

可以发现features配置成功,而sda10未配置成功,这部分原因是sda是在开机过程中文件系统创建的debug节点之前是不存在的,因此配置不生效。

这个问题需要在init.rc下添加一行命令去让inti从规则中恢复这部分的权限,感兴趣可以看下这个博客:

https://happybevis.github.io/2018/05/02/The-Magic-Selinux-Restore-Rule/

#We restorecon /sys/fs/ in case sysfs_egbin sepolicy effective
restorecon --recursive /sys/fs/

修改后的结果

drwxr-xr-x  4 root root u:object_r:sysfs:s0      0 1970-01-29 19:16 .
drwxr-xr-x 11 root root u:object_r:sysfs:s0      0 1970-01-29 19:16 ..
drwxr-xr-x  2 root root u:object_r:sysfs_egbin:s0 0 1970-01-29 19:16 features
drwxr-xr-x  2 root root u:object_r:sysfs_egbin:s0 0 1970-01-29 19:16 sda

data下的权限

  • egbin配置对system_data_file的相关权限

由于data分区属于system_data_file因此需要给egbin配置对system_data_file的相关权限,如下

drwxrwx--x  43 system system u:object_r:system_data_file:s0  4096 1970-01-29 01:46 data

allow egbin system_data_file:dir{ creat open write ……};
allow egbin system_data_file:file{ getattr open read ioctl};
  • dac_override权限问题

但添加完后在debug时候出现一行权限错误

egbin  : type=1400 audit(0.0:879): avc: denied { dac_override } for capability=1 s:egbin:s0 tclass=capability permissive=1

需要给egbin dac_override权限,但是该权限是Android P的neverallow规则中的,不能被添加。dac_override权限意思是容许进程旁路的所有DAC权限:uid,gid,ACL 等等,即有这个权限可以无视linux的权限中的用户、用户组。谷歌这样做的原因可能是这个dac_override权限能力范围太大,不能给普通程序这个权限,只有少数程序有这个权限

ueventd.te(7):allow ueventd self:capability { chown mknod net_admin setgid fsetid sys_rawio dac_override fowner };
zygote.te(7):allow zygote self:capability { dac_override setgid setuid fowner chown };
netd.te(44):allow netd self:capability { dac_override chown fowner };
runas.te(14):dontaudit runas self:capability dac_override;
vold.te(20):allow vold self:capability { net_admin dac_override mknod sys_admin chown fowner fsetid };
installd.te(6):allow installd self:capability { chown dac_override fowner fsetid setgid setuid };
tee.te(9):allow tee self:capability { dac_override };
  • dac_override权限问题一种解法

出现这种问题可能原因是进程的组与需要访问的文件的组不同,进程没有权限访问改组的文件,需要申请dac_override权限来旁路的所有DAC权限:uid,gid使进程可以访问该文件。以egbin为例,下面为开机时egbin的rc文件,及data的user和group

drwxrwx--x  43 system system u:object_r:system_data_file:s0  4096 1970-01-29 01:46 data

service egbin /system/bin/egbin start
    class core
    user root
    group root
    writepid /dev/stune/top-app/tasks
    seclabel u:r:egbin:s0

可以看到 egbin 执行时候user是root,而data是属于system,因此导致需要申请dac_override才能访问data分区。如果不加dac_override的权限,可以将egbin的组切换为system组即可访问,rc修改如下

service egbin /system/bin/egbin start
    class core
    user root
    group root system
    writepid /dev/stune/top-app/tasks
    seclabel u:r:egbin:s0

SEliunx及rc文件替换

  • SEliunx 文件替换

系统中SElinux文件的存放位置为/system/etc/selinux 和 /vendor/etc/selinux可以在编译出来的版本中只替换手机vendor或system下selinux文件夹(注意是整个文件夹)来达到修改目的

  • rc文件修改

/system/etc/init//vendor/etc/init/可以直接修改相应rc文件,无需下载img,对根目录下的rc文件不能采用该方法

猜你喜欢

转载自blog.csdn.net/u013377887/article/details/108262622