本文为博主原创文章,转载请注明出处:https://i.cnblogs.com/EditPosts.aspx?postid=11185476

CPU:RK3288

系统:Android 5.1

SELinux 主要由美国国家安全局开发。2.6 及以上版本的 Linux 内核都已经集成了 SELinux 模块。

通过虚拟文件系统 proc 来读写 gpio 的方法非常受欢迎,不仅因为其省去了 hal 层、 jni 层的代码,而且可以直接通过 adb shell 来读写 gpio。

在 user 版本,虽然驱动代码中已经将节点权限设置为 777 或者 666,但是在 adb shell 中写 gpio 时会报出权限不足的问题。

最快的解决办法是将系统编译成 userdebug 版本或者 eng 版本,也可以对系统 root。

下面是编译成 user 版本的解决方法,前提是驱动已经OK。

cust_gpios 是驱动中通过 proc_mkdir 创建的目录,relay 是通过 proc_create 创建的节点,对应一个方向为输出 gpio

1、在 adb 中读,没有问题,但是写 gpio 会报出权限不足

shell@rk3288:/proc/cust_gpios $ cat relay
cat relay shell@rk3288:/proc/cust_gpios $ echo > relay
echo > relay
/system/bin/sh: can't create relay: Permission denied

kenel 中打印出的错误信息如下

type=1400 audit(1358758415.820:7): avc: denied { write } for pid=1229 comm="sh" name="relay" dev="proc" ino=4026533065 scontext=u:r:shell:s0 tcontext=u:object_r:proc:s0 tclass=file permissive=0

解释:

write:表示没有 write 权限

shell:shell 中缺少权限,文件名与此相同,xxx.te

proc:proc 文件系统缺少权限

file:file 类型的文件

2、添加 shell 权限,scontext 对应的是 shell ,所以需要在 shell.te 最后面中添加

path:\device\rockchip\common\sepolicy\shell.te

allow shell proc:file write;

3、添加后编译会报错如下,这是因为添加了不允许的规则

libsepol.report_failure: neverallow on line 344 of external/sepolicy/app.te (or line 4313 of policy.conf) violated by allow shell proc:file { write };

需要在 app.te 中的 344 行中不允许的规则中删除添加的权限,用大括号括起来

path:\external\sepolicy\app.te

neverallow {appdomain -shell}
proc:dir_file_class_set write;

此时可以在 adb 来通过 proc 虚拟文件系统正常读写 gpio

4、操作 gpio 最终要由上层 apk 来读写,同样写 gpio 时,kernel 和 logcat 都会报出权限不足,解决方法与上面类似

type=1400 audit(1358758857.090:8): avc: denied { write } for pid=1208 comm="ron.gpiocontorl" name="relay" dev="proc" ino=4026533065 scontext=u:r:untrusted_app:s0 tcontext=u:object_r:proc:s0 tclass=file permissive=0

添加权限

path:device\rockchip\common\sepolicy\untrusted_app.te

allow untrusted_app proc:file write;

添加后编译报错

libsepol.report_failure: neverallow on line 344 of external/sepolicy/app.te (or line 4313 of policy.conf) violated by allow untrusted_app proc:file { write };

删除不允许的权限

path:\external\sepolicy\app.te

neverallow {appdomain -shell -untrusted_app}
proc:dir_file_class_set write;

此时 adb 和 apk 中都能正常读写 gpio。

下面是 RK3288 Android 5.1 添加权限的完整补丁

diff --git a/device/rockchip/common/sepolicy/shell.te b/device/rockchip/common/sepolicy/shell.te
index be7a221..f382240
--- a/device/rockchip/common/sepolicy/shell.te
+++ b/device/rockchip/common/sepolicy/shell.te
@@ -, +, @@ allow shell toolbox_exec:file { read getattr open execute execute_no_trans };
allow shell logcat_exec:file { read getattr open execute execute_no_trans };
allow shell serial_device:chr_file rw_file_perms;
allow shell proc_cpuinfo:file mounton;
+allow shell proc:file write; diff --git a/device/rockchip/common/sepolicy/untrusted_app.te b/device/rockchip/common/sepolicy/untrusted_app.te
index 8c0f740..69cfc0d
--- a/device/rockchip/common/sepolicy/untrusted_app.te
+++ b/device/rockchip/common/sepolicy/untrusted_app.te
@@ -, +, @@ allow untrusted_app kernel:system { module_request };
allow untrusted_app binfmt_misc:dir { search };
allow untrusted_app binfmt_misc:file { read open };
allow untrusted_app video_device:chr_file { read write open ioctl };
+allow untrusted_app proc:file write; diff --git a/external/sepolicy/app.te b/external/sepolicy/app.te
index ca08d74..
--- a/external/sepolicy/app.te
+++ b/external/sepolicy/app.te
@@ -, +, @@ neverallow { appdomain -shell } efs_file:dir_file_class_set read;
# Write to various pseudo file systems.
neverallow { appdomain -bluetooth -nfc }
sysfs:dir_file_class_set write;
-neverallow appdomain
+neverallow {appdomain -shell -untrusted_app}
proc:dir_file_class_set write; # Access to syslog() or /proc/kmsg.

最新文章

  1. jsp上传图片实时显示
  2. php以post方式向接口发送数据
  3. 【转】关于TCP和UDP协议消息保护边界的介绍
  4. URAL 1208 Legendary Teams Contest(DFS)
  5. Sublime Text 2 配置及其使用
  6. PSP0表格
  7. shell初学
  8. 1. Hyper上的CentOS 6.5 网络配置
  9. WebService基础学习(三)—CXF
  10. JS - dateFormat
  11. 微信测试号开发入门配置问题java
  12. MyBatis入门(一)SqlSessionFactory
  13. lua生成UUID
  14. HTML、CSS知识点,面试开发都会需要--No.6 设置背景
  15. C#的delegate简单练习
  16. vCenter机器查找功能不可用的解决
  17. BZOJ.2724.[Violet 6]蒲公英(静态分块)
  18. [No000013A]Windows WMIC命令使用详解(附实例)
  19. Python之路 - 网络编程初识
  20. PYQT实现简单的浏览器功能

热门文章

  1. vue-cli项目开发运行时内存暴涨卡死电脑
  2. Linux 之 文件
  3. linux命令详解
  4. Docker本地镜像上传到阿里云仓库
  5. Hadoop读写mysql
  6. C++——构造和析构函数
  7. springboot整合shiro引用配置文件配置redis信息报空指针异常
  8. 并发编程:Thread和Runable-01
  9. cpu 或 内存 偏高的分析套路
  10. python - scrapy 爬虫框架 ( redis去重 )