为 Android 8.0 添加开机启动脚本
2018-01-12 10:25
274 查看
为 Android 8.0 添加开机启动脚本
Qidi 2018.01.11 (Markdown & Haroopad)【写在前面】
本人对于
SELinux for Android理解不深,下文中的各文件及安全规则虽都是我所编写,但也是一边查阅文档一边试验得出的。在此强行为文,若有理解错误之处,请各位工程师同仁热情指出。
[b]一、需求描述[/b]
项目上需要在
Android 8.0上实现开机自动调用可执行文件
/system/bin/aaa去处理
/vendor/lib/bbb.so,并将处理后的文件保存为
/tmp/ds/ccc.so。这期间
/system/bin/aaa在处理文件时会去访问
/dev/ddd节点,并且在开机时
/tmp/ds/目录并不存在,需要每次开机时创建。
其中
/system/bin/aaa和
/dev/ddd是自定义添加的可执行文件和设备节点,在原生 Android 8.0 系统中并不存在。
[b]二、实现思路[/b]
1) 编写一个脚本实现调用
/system/bin/aaa对文件
/vendor/lib/bbb.so进行处理。
2) 在
.rc文件中新增创建
/tmp/ds/目录的代码和新增一个 service 用于启动 步骤1 中编写的脚本。
3) 新增
.te文件并编写相应 sepolicy 规则以解决权限问题。
[b]三、实现过程[/b]
首先编写开机启动脚本。因为 Google 启动了 Treble 计划,对 system 分区和 vendor 分区进行严格的分离,所以这样的自定义脚本我们最好不要放在
/system/bin/目录下。因为我工作于芯片公司,即 vendor 厂商,所以我把它放在
/vendor/bin/目录下。同时,从
Android 8.0开始,
SELinux for Android也进行了模块化,为了避免不必要的权限问题,我们使用
/vendor/bin/sh作为脚本解释器。我编写的开机脚本是
prepare_ds.sh,内容很简单,如下:
#!/vendor/bin/sh /system/bin/aaa /vendor/lib/bbb.so /tmp/ds/ccc.so
脚本应该在编译时自动拷贝到
vendor/bin/目录下,所以我们还要在 makefile 中添加拷贝命令。以我这个项目为例,对应的 makefile 文件是
product_mbox.mk,为其添加如下代码:
# aaa Security PRODUCT_COPY_FILES += \ $(LOCAL_PATH)/../../prepare_ds.sh:/vendor/bin/prepare_ds.sh
然后在相应的
.rc文件中添加代码。这是为了在开机时创建
/tmp/ds/目录,以及以 service 的形式运行脚本
prepare_ds.sh。以我这个项目为例,需要在
init.vendorName.board.rc中添加如下语句:
on init mkdir /tmp 0770 root root mkdir /tmp/ds 0770 root root mount tmpfs tmpfs /tmp/ds on boot #aaa security chmod 0666 /dev/ddd chmod 0777 /system/bin/aaa chmod 0666 /vendor/lib/bbb.so start aaa_security chmod 0666 /tmp/ds/ccc.so service aaa_security /vendor/bin/sh /vendor/bin/prepare_ds.sh user root group root disabled oneshot seclabel u:r:aaa_security:s0
这几行命令的意思是在系统 init 阶段创建
/tmp/ds/目录,定义名为 aaa_security 的服务,并在 boot 阶段以 root 身份显式启动且仅启动服务一次。
接着我们还要为上一步骤里定义的
aaa_sercurity服务编写
.te文件,定义其所属的安全域(SELinux domain)。否则设备在开机后无法执行这个 service,并且我们可以在 dmesg 中看到下方这样的警告:
[ 9.910511] init: service aaa_security does not have a SELinux domain defined
在
device/<vendorName>/common/sepolicy/目录下新建
aaa_security.te文件。我们可能并不清楚开机运行这个脚本并执行相应的调用需要哪些权限,所以我们可以先在
.te文件中添加上基本的定义。这些基本的文件内容如下:
######################################## # sepolicy rules for aaa_security ######################################## type aaa_security, domain; type aaa_security_exec, exec_type, vendor_file_type, file_type; permissive aaa_security; init_daemon_domain(aaa_security)
其中
aaa_security这个域对应
.rc文件中定义的服务 aaa_security,
aaa_security_exec对应脚本文件
prepare_ds.sh。语句
permissive aaa_security是暂时添加的,目的是使服务在遇到权限问题时也可以正常执行,但会将所需的权限类型打印出来。
init_daemon_domain是一个宏,用来使 aaa_security 域生效。
因为
/system/bin/aaa在处理文件时要访问
/dev/ddd节点,所以我们也要为这个设备节点编写文件
aaa_device.te。内容如下:
######################################## # sepolicy rules for aaa_device ######################################## type aaa_device, file_type, dev_type;
最后,在
device/<vendorName>/sepolicy/file_contexts里为这些新增的文件指定要使用的安全上下文,我们的工作就接近完成了。新增语句如下:
/vendor/bin/prepare_ds.sh u:object_r:aaa_security_exec:s0 /dev/aaa u:object_r:aaa_device:s0
为了验证我们的修改是否有效,需要编译并烧写 boot.img 和 vendor.img,然后重启设备。
不过最初编写的
aaa_security.te文件中,往往赋予开机脚本的权限会有不足,我们需要通过查看 dmesg 中的打印来添加上相应的权限。比如我们可能会看到下面这样的权限缺失提示:
[ 10.368517] type=1400 audit(1483292256.112:14): avc: denied { execute_no_trans } for pid=2768 comm="prepare_ds.sh" path="/vendor/bin/toybox_vendor" dev="mmcblk0p14" ino=222 scontext=u:r:aaa_security:s0 tcontext=u:object_r:vendor_toolbox_exec:s0 tclass=file permissive=1 [ 10.434460] type=1400 audit(1483292256.112:14): avc: denied { execute_no_trans } for pid=2768 comm="prepare_ds.sh" path="/vendor/bin/toybox_vendor" dev="mmcblk0p14" ino=222 scontext=u:r:aaa_security:s0 tcontext=u:object_r:vendor_toolbox_exec:s0 tclass=file permissive=1 [ 10.662017] type=1400 audit(1483292256.408:16): avc: denied { read write } for pid=2775 comm="aaa" name="aaa" dev="tmpfs" ino=1287 scontext=u:r:aaa_security:s0 tcontext=u:object_r:device:s0 tclass=chr_file permissive=1 [ 10.666135] type=1400 audit(1483292256.408:16): avc: denied { read write } for pid=2775 comm="aaa" name="aaa" dev="tmpfs" ino=1287 scontext=u:r:aaa_security:s0 tcontext=u:object_r:device:s0 tclass=chr_file permissive=1 [ 10.666145] type=1400 audit(1483292256.408:17): avc: denied { open } for pid=2775 comm="aaa" path="/dev/ddd" dev="tmpfs" ino=1287 scontext=u:r:aaa_security:s0 tcontext=u:object_r:aaa_device:s0 tclass=chr_file permissive=1 [ 10.666264] type=1400 audit(1483292256.408:17): avc: denied { open } for pid=2775 comm="aaa" path="/dev/ddd" dev="tmpfs" ino=1287 scontext=u:r:aaa_security:s0 tcontext=u:object_r:aaa_device:s0 tclass=chr_file permissive=1 [ 10.666271] type=1400 audit(1483292256.408:18): avc: denied { ioctl } for pid=2775 comm="aaa" path="/dev/ddd" dev="tmpfs" ino=1287 ioctlcmd=0x10 scontext=u:r:aaa_security:s0 tcontext=u:object_r:aaa_device:s0 tclass=chr_file permissive=1 [ 10.679685] type=1400 audit(1483292256.408:18): avc: denied { ioctl } for pid=2775 comm="aaa" path="/dev/ddd" dev="tmpfs" ino=1287 ioctlcmd=0x10 scontext=u:r:aaa_security:s0 tcontext=u:object_r:aaa_device:s0 tclass=chr_file permissive=1
那么我们就应该在
.te文件中添加上这些权限,然后删除掉
permissive aaa_security语句。以我的项目为例,添加上所需全部权限后,完整的
aaa_security.te文件内容如下:
######################################## # sepolicy rules for aaa_security ######################################## type aaa_security, domain; type aaa_security_exec, exec_type, vendor_file_type, file_type; r_dir_file(aaa_security, rootfs) r_dir_file(aaa_security, system_file) r_dir_file(aaa_security, sysfs_type) r_dir_file(aaa_security, aaa_device) #permissive aaa_security; init_daemon_domain(aaa_security) allow aaa_security shell_exec:file { read getattr }; allow aaa_security vendor_toolbox_exec:file { read open execute execute_no_trans }; allow aaa_security system_data_file:file { getattr }; allow aaa_security system_data_file:dir { getattr }; allow aaa_security self:capability { dac_override sys_admin }; #allow aaa_security rootfs:filesystem { remount }; allow aaa_security rootfs:dir { write add_name create }; allow aaa_security system_file:file { execute_no_trans }; allow aaa_security aaa_device:chr_file { read write open ioctl }; allow aaa_security cache_file:dir { getattr }; allow aaa_security tee_data_file:dir { getattr }; allow aaa_security storage_file:dir { getattr }; allow aaa_security media_rw_data_file:dir { getattr }; allow dolby_security vendor_shell_exec:file { entrypoint }; allow dolby_security tmpfs:dir { write add_name create }; allow dolby_security dolby_security_tmpfs:file { create open };
至此,我们的工作就全部完成了。重新编译并烧写 boot.img 和 vendor.img 后,重启设备,可以看到开机脚本工作正常。
相关文章推荐
- Android 8.0 添加开机启动脚本
- (OK) 为Android添加开机启动脚本
- 为Android添加开机启动脚本
- 为Android添加开机启动脚本
- 为Android添加开机启动脚本
- (OK) Android 添加 开机启动 脚本
- 为Android添加开机启动脚本
- 为Android添加开机启动脚本
- Ubuntu下添加开机启动脚本
- Ubuntu下添加开机启动脚本
- centos7如何添加开机启动服务/脚本
- Ubuntu下添加开机启动脚本
- android 开机自动启动某个linux应用或脚本
- Ubuntu下添加开机启动脚本
- Ubuntu下添加开机启动脚本
- ubuntu 添加开机启动脚本
- linux添加开机启动脚本
- 为Android添加一个开机完成后执行的脚本
- 【centos7】添加开机启动服务/脚本
- Android系统启动时添加运行脚本