平台:RK3568
系统:Android 13
1、需求描述
为了实现系统内置的APP在系统中某些用户无法看到的目录下创建文件夹或文件,需要系统创建文件夹,内置的系统APP具有读写权限,以及系统源码也可以读写该目录或文件。
比如:我们需要在系统根目录data或mnt文件夹下创建一个文件夹mcuhot,需要按照如下操作。
1)在init.rc文件中实现创建目录指令
2)在系统代码中实现某些读写逻辑
3)SELlinux限制添加所需的权限
2、功能实现
1)在init.rc文件中实现创建目录指令
在init.rc添加如下指令实现在目录data下创建文件夹mcuhot.
添加:
mkdir /data/mcuhot
chown system system /data/mcuhot
chmod 0777 /data/mcuhot
文件路径:system/core/rootdir/init.rc
on property:sys.boot_completed=1
bootchart stop
# Setup per_boot directory so other .rc could start to use it on boot_completed
exec - system system -- /bin/rm -rf /data/per_boot
mkdir /data/per_boot 0700 system system encryption=Require key=per_boot_ref
mkdir /data/sdady
chown system system /data/sdady
chmod 0777 /data/sdady
# system server cannot write to /proc/sys files,
# and chown/chmod does not work for /proc/sys/ entries.
# So proxy writes through init.
on property:sys.sysctl.extra_free_kbytes=*
exec -- /system/bin/extra_free_kbytes.sh ${sys.sysctl.extra_free_kbytes}
2)在系统代码中实现某些读写逻辑
系统代码实现逻辑功能代码此处忽略。主要介绍编写完成后编译,下载更新系统后,并没有实现预期功能,抓取日志进行分析。
从日志分析来看,创建的mcuhot,系统APP没有打开权限,也就是我们frameworks实现的逻辑代码,没有打开目录权限,从后续的日志分析来看,也没有读取权限,因此我们这里需要简单了解SELinux安全模块。
安全增强型linux(Security-Enhanced Linux)简称 SELinux,它是一个 Linux 内核模块,也是 Linux 的一个安全子系统。SELinux 主要由美国国家安全局开发。2.6 及以上版本的 Linux 内核都已经集成了 SELinux 模块。SELinux 的结构及配置非常复杂,而且有大量概念性的东西,要学精难度较大。很多 Linux 系统管理员嫌麻烦都把 SELinux 关闭了。
关于SELinux我们不过多介绍,网络上有详细介绍和使用。目前只解决如何处理我们的问题。刚才提到我们系统APP没有打开和读取权限,由于SELinux限制了在对根目录下的文件夹及文件的打开,读取,写入等各种操作。
如何添加权限呢?从日志分析可知,mcuhot没有读取被限制,后续日志发现也没有打开权限,日志关键部分如下:
---------------------------------------------------------------------------------------------------------------------------------
auditd : type=1400 audit(0.0:562): avc: denied { read } for comm="DefaultDispatch" name="mcuhot" dev="dm-8" ino=1391 scontext=u:r:system_app:s0 tcontext=u:object_r:system_data_file:s0 tclass=dir permissive=1
... ...
DefaultDispatch: type=1400 audit(0.0:563): avc: denied { open } for path="/data/sdady" dev="dm-8" ino=1391 scontext=u:r:system_app:s0 tcontext=u:object_r:system_data_file:s0 tclass=dir permissive=1
---------------------------------------------------------------------------------------------------------------------------------
SELinux添加权限格式:
allow 源类型 目标类型:访问类型 操作权限
实现代码:
allow system_app system_data_file:dir { read open };
3)SELlinux限制添加所需的权限
在system_app.te添加权限打开和读取权限,各平台芯片system_app.te文件,所处目录各不相同。在文件最后添加如下代码:
allow system_app system_data_file:dir { read open };
路径:device/rockchip/common/sepolicy/vendor/system_app.te
dontaudit system_app mnt_vendor_file:dir search;
#for gpu
allow system_app sysfs_usb:file rw_file_perms;
#for ota
allow system_app selinuxfs:file { read open };
allow system_app mnt_media_rw_file:dir { getattr };
#for hdmi
allow system_app sysfs_hdmi:file rw_file_perms;
allow system_app sysfs_zram:dir r_dir_perms;
allow system_app sysfs_zram:file rw_file_perms;
allow system_app proc_stat:file { read open getattr };
#for samba
allow system_app metadata_file:dir {getattr};
allow system_app block_device:dir {search};
#for devicetest
#allow system_app system_app_data_file:file { execute execute_no_trans };
allow system_app system_data_file:file { read };
allow system_app { sysfs_leds metadata_file }:dir { search };
#TODO removed for Q
#allow system_app vendor_file:file { read open };
allow system_app unlabeled:filesystem { getattr };
allow system_app unlabeled:file { getattr open read write unlink rename };
allow system_app unlabeled:dir { open search read getattr write remove_name add_name };
allow system_app cache_file:lnk_file { read };
allow system_app cache_recovery_file:dir {search create read write open add_name getattr remove_name};
allow system_app cache_recovery_file:file {rw_file_perms create unlink setattr};
#TODO removed for Q
#allow system_app vendor_file:file { getattr };
allow system_app cache_file:dir search;
#set_prop(system_app,exported_system_prop)
set_prop(system_app,powerctl_prop)
allow system_app abc_data_file:file { read getattr };
allow system_app abc_data_file:dir { open getattr read search };
set_prop(system_app, debug_prop)
rw_rockchip_graphic_device(system_app)
allow system_app block_device:dir { search read open getattr };
allow system_app mnt_media_rw_file:dir { search read open getattr };
allow system_app asec_apk_file:dir { search read open getattr };
hal_client_domain(system_app, hal_light)
hal_client_domain(system_app, hal_power)
hal_client_domain(system_app, hal_health)
binder_call(system_app, hal_keymint_default)
binder_call(system_app, hal_usb_impl)
binder_call(system_app, update_engine)
allow system_app proc_pagetypeinfo:file r_file_perms;
allow system_app mnt_sdcard_file:lnk_file r_file_perms;
allow system_app mnt_pass_through_file:dir r_file_perms;
allow system_app system_data_file:dir { read open };