Android13 添加init.rc自定义服务,编译的时候在Selinux检测阶段出现 neverallow 编译报错 ,已解决

背景介绍

注:本文适合亲自添加过init.rc自定义服务的人阅读,没有搞过的话,可以先去看看如何添加,再结合本篇文章阅读,可以更好的理解意思。

我使用的Android版本:Android13
  最近在搞init.rc 新增自启动服务的功能,一般添加自启动服务的步骤如下:
1、将服务源代码通过mk / bp 添加编译到Android镜像。
2、init.rc添加自定义服务 定义。
/system/core/rootdir/init.rc
3、为服务制定Selinux规则。
修改目录:device/芯片商/common/sepolicy/vendor
file_contexts

#ftpd
/system/bin/ftpd 		u:object_r:ftpd_exec:s0
#ntpd
/system/bin/ntpd 		u:object_r:ntpd_exec:s0

ftpd.te

type ftpd, domain, coredomain;
type ftpd_exec, exec_type, file_type;
init_daemon_domain(ftpd)

allow ftpd ftpd:tcp_socket {create setopt bind};
allow ftpd ftpd:process {execmem};
allow ftpd ftpd:capability {net_raw};

ntpd.te

type ntpd, domain, coredomain;
type ntpd_exec, exec_type, file_type;
init_daemon_domain(ntpd)

allow ntpd node:udp_socket node_bind;
allow ntpd port:udp_socket name_bind;
allow ntpd self:capability net_bind_service;

走完上面这些过程,编译的时候 就直接报了这个错误:
在这里插入图片描述
关键的报错提取出来:

libsepol.report_failure: neverallow on line 948 of system/sepolicy/public/domain.te violated by allow ntpd ntpd_exec:file { entrypoint };
libsepol.report_failure: neverallow on line 948 of system/sepolicy/public/domain.te violated by allow ftpd ftpd_exec:file { entrypoint };
libsepol.check_assertions: 2 neverallow failures occurred
Error while expanding policy

上述就是整个问题的背景介绍。

解释一下,就是我定义的te文件allow部分的规则和谷歌定义的neverallow的规则发生了冲突,导致编译不过。这个问题我内网 墙网都搜了,根本找不到靠谱的答案,所以最后解决了我觉得非常有必要记录下来。
上面报错说和domain.te 948行冲突了,那么我们直接看看冲突部分:
在这里插入图片描述
红框 框出来的就是冲突部分,我第一眼看到,只想说,我靠这怎么解决,在搜索了无数低质量文章饱受摧残之后,我开始尝试各种解决方法:
注:这里我没有注释domain.te ftpd.te ntpd.te 中每一行的意思,读者可以自己先看看,把意思看懂,实在不会可以评论区问我。
第一种:直接排除相关的 file_type

full_treble_only(`
  # Do not allow coredomain to access entrypoint for files other
  # than system_file_type and postinstall_file
  neverallow coredomain {
    -file_type
    -system_file_type
    -postinstall_file
  }:file entrypoint;

结果失败,会报另外的错误,无法解决的错误。
第二种:将coredomain排除

full_treble_only(`
  # Do not allow coredomain to access entrypoint for files other
  # than system_file_type and postinstall_file
  neverallow { -coredomain } {
    file_type
    -system_file_type
    -postinstall_file
  }:file entrypoint;

结果失败,会报另外的错误,无法解决的错误。
第三种:看了不靠谱博主的博客,选择添加ftpd ntpd

full_treble_only(`
  # Do not allow coredomain to access entrypoint for files other
  # than system_file_type and postinstall_file
  neverallow coredomain {
    -ftpd
    -ntpd
    file_type
    -system_file_type
    -postinstall_file
  }:file entrypoint;

结果直接报unknown type ftpd ntpd ,根本不行,真的低质量博客。
第四种:代码中强行关闭Selinux

system/core/init/selinux.cpp
bool IsEnforcing() {
  if(1>0){
    return false;
   }

}

结果完全没用,会报新错误,解决不了哪种。
第五种:我考虑的是,既然domain.te里面有这么一条neverallow,那么把整个neverallow删除了不就行了?
结果也是会报一些奇奇怪怪的错误。
第六种:我选择将te文件的所属范围减小,删掉cordomain file_type 类型包含。

ftpd.te
type ftpd, domain;
type ftpd_exec, exec_type;
init_daemon_domain(ftpd)

allow ftpd ftpd:tcp_socket {create setopt bind};
allow ftpd ftpd:process {execmem};
allow ftpd ftpd:capability {net_raw};

ntpd.te
type ntpd, domain;
type ntpd_exec, exec_type;
init_daemon_domain(ntpd)

allow ntpd node:udp_socket node_bind;
allow ntpd port:udp_socket name_bind;
allow ntpd self:capability net_bind_service;

结果还是会报其它编译错误。

综上所述,我卡在这里好几天,感觉每一条路都被堵死了。
直到前不久我看到这篇博客:

https://blog.csdn.net/yxw0609131056/article/details/79784490

他的解决方法和我的不太一样,但是看了他的博客让我突然之间灵光一现,既然domain.te里面的规则是不能改也不能删除,我自己缩小allow范围好像也行不通,那么为什么不可以把neverallow中排除掉的类型加到我自己的te文件里面呢?
有了这个想法,让我们回到最初的domain.te 948行,看看:
在这里插入图片描述
这里neverallow coredomain file_type:file entrypoint,我的ftpd ntpd 都属于coredomain类型,所以才会报错,之前试过了删除coredomain file_type 类型包含没用,但是仔细看上面 -system_file_type -postinstall_file两种类型是被排除在外的,于是,把te文件改成这样:
注:neverallow中的-system_file_type表示排除对这种文件类型的neverallow。人话解释就是 system_file_type不受此规则限制。
在这里插入图片描述
结果 顺利编译通过,刷到板子上,功能也运行正常。

总结:Android13 规定好的neverallow规则是改不了的,改了会报很多莫名其妙看不懂的错误,只能在自己的te文件里加上neverallow中的 排除类型,以此通过neverallow的编译规则检测。这种解决方法应该是比较通用的了,只要编译出现neverallow报错,都可以通过这种方式排错。

最后这次排错解决花了一个星期的时间,中间大部分花在了无用的博客上面,如果大家也是搞计算机的,拜托 麻烦写博客的时候动点脑子,不要抄袭,不要照搬,不要胡言乱语,尽量不要有错别字,行文严谨有逻辑一点,让人好读懂一点,还有就是要确保自己写的东西是正确的。希望大家都能把博客写好,给人以便利,给自己以价值。

猜你喜欢

转载自blog.csdn.net/weixin_43522377/article/details/132227267