Android 5.x SEAndroid/SElinux内核节点的读写权限
2016-06-17 15:57
627 查看
转自:http://blog.csdn.net/tung214/article/details/44461985
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
/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
# We add here
/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
# Device types
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 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
# Read/Write to /proc/net/xt_qtaguid/ctrl and and /dev/xt_qtaguid.
allow system_server qtaguid_proc:file rw_file_perms;
allow system_server qtaguid_device:chr_file rw_file_perms;
# chr_file表示字符设备文件,如果是普通文件用file,目录请用dir
# rw_file_perms代表读写权限
allow system_server wf_bt_device:chr_file rw_file_perms;
这句话的意思是:允许system_server进程拥有对wf_bt_device的这个字符设备的读写权限。
改了这些之后,你就可以make installclean;make -j16编译image来验证权限是否获取成功。
fd =open("/dev/wf_bt",O_RDONLY | O_NOCTTY); 绝对成功!!!!!
鸣谢:感谢Joly_xie
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
/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
# We add here
/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
# Device types
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 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
# Read/Write to /proc/net/xt_qtaguid/ctrl and and /dev/xt_qtaguid.
allow system_server qtaguid_proc:file rw_file_perms;
allow system_server qtaguid_device:chr_file rw_file_perms;
# chr_file表示字符设备文件,如果是普通文件用file,目录请用dir
# rw_file_perms代表读写权限
allow system_server wf_bt_device:chr_file rw_file_perms;
这句话的意思是:允许system_server进程拥有对wf_bt_device的这个字符设备的读写权限。
改了这些之后,你就可以make installclean;make -j16编译image来验证权限是否获取成功。
fd =open("/dev/wf_bt",O_RDONLY | O_NOCTTY); 绝对成功!!!!!
鸣谢:感谢Joly_xie
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories