Android 在 SElinux机制下添加内核节点的访问权限方法
SElinux机制说明
Android 5.0及其以上版本,因为采取了SEAndroid/SElinux的安全机制,即使拥有root权限,或者对某内核节点设置为777的权限,仍然无法在JNI层访问。那么就需要添加节点或者APP所需要的权限。
需要什么权限
既然我们要给节点赋予权限,那么前提就是要知道赋予它什么权限:
1. 分解log:
type=1400 audit(171725.225:28): avc: denied { read } for pid=3592 comm=“jw.ComAssistant” name="/" dev=“tmpfs” ino=14757 scontext=u:r:untrusted_app_25:s0:c512,c768 tcontext=u:object_r:device:s0 tclass=dir permissive=0
————————————————
avc: denied { 操作权限 } for pid=3592 comm=“进程名” scontext=u:r:源类型:s0
tcontext=u:r:目标类型:s0 tclass=访问类型 permissive=0
意思是: 在pid为3592的进程名为untrusted_app_25的类型为device的类dir缺少read权限
2. 添加对应的权限:
打开文件AndroidL/android/device/qcom/msm8953/sepolicy/file_contexts 仿照这个文件里的写法,为你的节点定义一个你想要的名字(ttyHSL1是自定义):
/dev/tegra.* u:object_r:video_device:s0
/dev/tf_driver u:object_r:tee_device:s0
/dev/tty u:object_r:owntty_device:s0
/dev/tty[0-9]* u:object_r:tty_device:s0
Ex: /dev/ttyHSL1 u:object_r:device:s0
打开文件AndroidL/android/device/qcom/msm8953/device.te 仿照这个文件里的写法,将刚刚第二步写的ttyHSL1声明为dev_type:
type device, dev_type, fs_type;
type alarm_device, dev_type, mlstrustedobject;
type adb_device, dev_type;
type ashmem_device, dev_type, mlstrustedobject;
type audio_device, dev_type;
type binder_device, dev_type, mlstrustedobject;
type block_device, dev_type;
// We add here
type device, dev_type;
AndroidL/android/device/qcom/msm8953/sepolicy/目录下很多.te文件都是以进程名来结尾的,比如有针对surfaceflinger进程的surfaceflinger,有针对untrusted_app_25进程的untrusted_app_25.te,
刚刚从第一步得到,这个节点是由untrusted_app_25 进程来访问,所以,我们找到untrusted_app_25 .te打开,加入允许这个进程对devices的读写权限
allow untrusted_app_25 qtaguid_proc:file rw_file_perms;
allow untrusted_app_25 qtaguid_device:chr_file rw_file_perms;
// chr_file表示字符设备文件,如果是普通文件用file,目录请用dir
// rw_file_perms代表读写权限
allow <scontext> <tcontext>:<tclass> rw_file_perms
allow untrusted_app_25 device:dir rw_file_perms; // 允许untrusted_app_25 进程拥有对device的这个字符设备的读写权限;
3. 编译验证权限;
make installclean;make -j16编译image来验证权限是否获取成功。
mmm external/sepolicy
make -j24 ramdisk-nodeps
make -j24 bootimage-nodeps
4. 查看权限;
ls -Z 节点
可以用命令来查看selinux的状态 getenforce 这个命令可以查看到selinux的状态,当前可以看到是关闭状态的。 还有一个命令也可以查看出selinux的状态。 sestatus -v 还有一个setenforce 命令可以设置selinux的状态
5.若验证结果依旧没有权限
这种情况下就在type ttyHSL1, dev_type; 中增加 mlstrustedobject。
type device, dev_type,mlstrustedobject;