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,可以看到服务已经正常跑起来了:

原文地址:https://www.cnblogs.com/wmc245376374/p/13435891.html