Android 在 SElinux下 如何获得对一个内核节点的访问权限

转自http://blog.csdn.net/wh_19910525/article/details/45170755

Android 5.0下,因为采取了SEAndroid/SElinux的安全机制,即使拥有root权限,或者对某内核节点设置为777的权限,仍然无法在JNI层访问。

本文将以用户自定义的内核节点 /dev/wf_bt为例,手把手教会读者如何在JNI层获得对该节点的访问权限。

第一步:找到需要访问该内核节点的进程(process),笔者自己这个节点由 system_server进程来访问

第二步:打开文件AndroidL/android/external/sepolicy/ file_contexts.be
仿照这个文件里的写法,为你的节点定义一个你想要的名字:
[python]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /dev/tegra.* u:object_r:video_device:s0  
  2. /dev/tf_driver u:object_r:tee_device:s0  
  3. /dev/tty u:object_r:owntty_device:s0  
  4. /dev/tty[0-9]* u:object_r:tty_device:s0  
  5. # We add here  
  6. /dev/wf_bt              u:object_r:wf_bt_device:s0  
wf_bt_device是自定义,其他左右两边的内容都和上面的范例一致。

第三步:打开文件AndroidL/android/external/sepolicy/ device.te
仿照这个文件里的写法,将刚刚第二步写的wf_bt_device声明为dev_type:
[python]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. # Device types  
  2. type device, dev_type, fs_type;  
  3. type alarm_device, dev_type, mlstrustedobject;  
  4. type adb_device, dev_type;  
  5. type ashmem_device, dev_type, mlstrustedobject;  
  6. type audio_device, dev_type;  
  7. type binder_device, dev_type, mlstrustedobject;  
  8. type block_device, dev_type;  
  9. # We add here  
  10. type wf_bt_device, dev_type;  

第四步:
AndroidL/android/external/sepolicy/目录下 很多.te文件都是以进程名来结尾的,比如有针对surfaceflinger进程的surfaceflinger,有针对vold进程的vold.te,
刚刚从第一步得到,这个节点是由system_server进程来访问,所以,我们找到 system_server.te打开,加入允许这个进程对/dev/wf_bt的读写权限,

[python]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. # Read/Write to /proc/net/xt_qtaguid/ctrl and and /dev/xt_qtaguid.  
  2. allow system_server qtaguid_proc:file rw_file_perms;  
  3. allow system_server qtaguid_device:chr_file rw_file_perms;  
  4.   
  5. # chr_file表示字符设备文件,如果是普通文件用file,目录请用dir  
  6. # rw_file_perms代表读写权限  
  7. allow system_server wf_bt_device:chr_file rw_file_perms;  // 允许system_server进程拥有对wf_bt_device的这个字符设备的读写权限;
这句话的意思是:允许system_server进程拥有对wf_bt_device的这个字符设备的读写权限。
改了这些之后,你就可以make installclean;make -j16编译image来验证权限是否获取成功。

fd =open("/dev/wf_bt",O_RDONLY | O_NOCTTY); 绝对成功!!!!!

=====================================

allow system_server wf_bt_device:chr_file rw_file_perms; //允许system_server进程拥有对wf_bt_device的这个字符设备的读写权限。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值