android7.1添加开机启动服务被selinux禁用问题

根据项目需求,在init.rc添加一个服务,开机自动启动这个服务:

service eGTouchD /system/bin/eGTouchD
    class main
    user root
    group root
    oneshot

开机后通过ps -Z查看进程,发现/system/bin/eGTouchD这个服务并没有跑起来。

查看kernel的开机log信息,发现有如下提示

init: Service eGTouchD does not have a SELinux domain defined.

说明该服务没有启动起来,是被selinux服务给禁用掉了。

 

为了解决这个问题,需要自己添加一些允许的规则,让改服务可以正常启用:

1.修改seplicy/file_contexts文件,添加以下内容:

/system/bin/eGTouchD     u:object_r:eGTouchD_exec:s0

2.在system/sepolicy下新加eGTouchD.te文件,内容如下:

复制代码
# File types must be defined for file_contexts.
type eGTouchD, domain;
type eGTouchD_exec, exec_type, file_type;
init_daemon_domain(eGTouchD)

allow eGTouchD rootfs:lnk_file { getattr };
allow eGTouchD shell_exec:file { execute read open execute_no_trans getattr };
allow eGTouchD system_data_file:dir { read open write remove_name add_name };
allow eGTouchD toolbox_exec:file { getattr execute read open execute_no_trans };
allow eGTouchD system_data_file:file { getattr open read write create unlink };
allow eGTouchD proc:file { read open getattr };
allow eGTouchD sysfs:dir { read open };
allow eGTouchD sysfs:file { read open getattr };
allow eGTouchD sysfs:lnk_file { getattr };
allow eGTouchD system_file:file { execute_no_trans };
allow eGTouchD device:chr_file { read write open ioctl };
allow eGTouchD uhid_device:chr_file { read write open ioctl };
allow eGTouchD system_data_file:fifo_file { create setattr getattr read write open };
allow eGTouchD input_device:dir { read open };
allow eGTouchD input_device:chr_file { getattr setattr };
allow eGTouchD eGTouchD:capability { dac_override fsetid };
复制代码

说明,以上的内容都是根据开机的log提示的一些缺省的权限一个一个加上去的,例如以下log:

 avc: denied { write } for pid=2047 comm="sh" name="/" dev="dm-1" ino=3 scontext=u:r:sudaemon:s0 tcontext=u:object_r:system_data_file:s0 tclass=dir permissive=1

则需要添加规则语句为:

allow sudaemon system_data_file:dir { write };

 

添加完这些,编译的时候可能会报错,这可能是由于自己写的xxx.te与domain.te等有一些neverallow的规则冲突了,解决办法就是domain.te的规则加入例外:

neverallow { domain -init -ueventd -eGTouchD} device:chr_file { open read write };

neverallow {
domain
-system_server
-system_app
-init
-eGTouchD #wmc
-installd # for relabelfrom and unlink, check for this in explicit neverallow
}system_data_file:file no_w_file_perms;
# do not grant anything greater than r_file_perms and relabelfrom unlink
# to installd
neverallow installd system_data_file:file ~{ r_file_perms relabelfrom unlink };

具体要根据报错来添加。

 

 

这样就可以正常编译通过了,重新烧录系统,开机查看ps -Z,可以看到服务已经正常跑起来了:

 

posted @   M-kobe  阅读(2902)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
点击右上角即可分享
微信分享提示