You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Native多AIDL服务SEPolicy配置问题求助

问题描述

我需要添加两个自定义服务,代码如下:

MyService sv1 = MyService::getInstance(mode::mode1);
defaultServiceManager()->addService(String16("Service1"), sv1);
MyService sv2 = MyService::getInstance(mode::mode2);
defaultServiceManager()->addService(String16("Service2"), sv2);

当前的SEPolicy配置文件:

myservice.te

type myservice, domain, coredomain;

service.te

type my_service, system_api_service, service_manager_type;

为注册这两个不同名称的服务,我在service_contexts中添加了以下内容:

Service1 u:object_r:my_service:s0
Service2 u:object_r:my_service:s0

但运行时Logcat返回AVC错误:

E SELinux : avc:  denied  { add } for pid=431 uid=1000 name=Service1 scontext=u:r:myservice:s0 tcontext=u:object_r:default_android_service:s0 tclass=service_manager permissive=1

我判断Service1和Service2未成功注册到servicemanager,因此AVC日志中的tcontext显示为default_android_service。请问如何在SEPolicy中正确注册这两个自定义服务?


解决方案

按以下步骤调整SEPolicy配置即可解决问题:

  1. 确保service_contexts配置生效

    • 检查service_contexts文件路径,通常位于system/sepolicy/public/service_contexts或设备专属的vendor/sepolicy/service_contexts目录下。
    • 确认编译时该文件被正确打包进系统镜像,部分设备需在BoardConfig.mk中指定自定义SEPolicy路径。
  2. 为myservice域添加服务添加权限
    在myservice.te中添加规则,允许你的服务进程所在域向my_service类型服务执行添加操作:

    allow myservice my_service:service_manager add;
    
  3. 验证服务名称匹配度
    SELinux的service_contexts对名称大小写、拼写完全敏感,需确保代码中的Service1、Service2和配置文件中的名称完全一致,无空格或特殊字符差异。

  4. 可选:补充服务查询权限
    如果后续服务需要查询自身或其他服务,可添加以下规则:

    allow myservice my_service:service_manager find;
    

完成上述配置后,重新编译SEPolicy并刷入系统,AVC错误会消失,服务可正常注册。

内容的提问来源于stack exchange,提问作者hieu0nguyen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.26 01:15:36