Android : 为系统服务添加 SeLinux 权限 (Android 9.0)

一、SElinux在Android 8.0后的差异:

  从Android 4.4到Android 7.0的SELinux策略构建方式合并了所有sepolicy片段(平台和非平台),然后在根目录生成单一文件,而Android 8.0开始关于selinux架构也类似于HIDL想把系统平台的selinux策略和厂商自己维护的策略剥离开来, 允许合作伙伴单独自己的策略,构建他们的镜像(.img)引导,这样便可以独立于平台更新这些.img,反之亦然(即:在不更新合作伙伴jiang'xaing像的情况下执行平台更新)。

  关于8.0 selinux架构介绍官方文档(SELinux_Treble.pdf): https://pan.baidu.com/s/161_OpZRqx7PvOmcQ4G-CwA

二、修改xxx service示例:

  以下便通过修改xxx系统服务的selinux权限作为例子参考(实际需根据SDK的版本修改对应目录):

1./system/sepolicy/prebuilts/api/26.0/nonplat_sepolicy.ci

(typeattribute xxx_service_26_0)
(roletype object_r xxx_service_26_0)

2./system/sepolicy/prebuilts/api/27.0/nonplat_sepolicy.cil

(typeattribute xxx_service_27_0)
(roletype object_r xxx_service_27_0)

3./system/sepolicy/prebuilts/api/28.0/private/compat/26.0/26.0.cil

(typeattributeset xxx_service_26_0 (xxx_service))

4./system/sepolicy/prebuilts/api/28.0/private/compat/27.0/27.0.cil

(typeattributeset xxx_service_27_0 (xxx_service))

5./system/sepolicy/prebuilts/api/28.0/private/service_contexts

xxx u:object_r:xxx_service:s0

6./system/sepolicy/prebuilts/api/28.0/public/service.te

type xxx_service, system_api_service, system_server_service, service_manager_type;

7./system/sepolicy/private/compat/26.0/26.0.cil

(typeattributeset xxx_service_26_0 (xxx_service))

8./system/sepolicy/private/compat/27.0/27.0.cil

(typeattributeset xxx_service_27_0 (xxx_service))

9./system/sepolicy/private/service_contexts

xxx  u:object_r:xxx_service:s0

10./system/sepolicy/public/service.te

type xxx_service, system_api_service, system_server_service, service_manager_type;

三、使用修改selinux权限的系统服务:

// 1.定义aidl文件:------------------------------------
package com.xxx.aidl;
interface ISecurityServer {
    void startLockAppSevice();

}

//2.实现aidl接口:------------------------------------
package com.xxx.aidl;
public class SecurityServer extends ISecurityServer.Stub{
    public void startLockAppSevice() {

    }

}

//3.提供对外接口类:----------------------------------
package com.xxx.security;
public class SecurityManager {
    private final ISecurityServer mService;
    public SecurityManager(ISecurityServer service) {
        mService = service;
    }
    public void startLockAppSevice(){
        try {
            mService.startLockAppSevice();
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

//4.注册服务:---------------------------------------
SystemServiceRegistry.java 添加 

        registerService("xxx", com.xxx.SecurityManager.class,
             new CachedServiceFetcher<com.xxx.SecurityManager>() {
            @Override
            public com.xxx.SecurityManager createService(ContextImpl ctx) {                
                IBinder b = ServiceManager.getService("xxx");
                return new com.xxx.SecurityManager(com.xxx.aidl.ISecurityServer.Stub.asInterface(b));
            }

        });    

//5. SystemServer.java 将服务添加进ServiceManager -------------
        try {
            // 
            com.xxx.aidl.SecurityServer Security = new com.xxx.aidl.SecurityServer(mContext);
            ServiceManager.addService("xxx", Security);
        } catch (Throwable e) {
            Log.e(TAG, "Failure starting olc_service_security", e);

        }

//6. 服务调用:-------------------------------------------------
SecurityManager securityManager = (SecurityManager)getSystemService("xxx");

 -end-

猜你喜欢

转载自www.cnblogs.com/blogs-of-lxl/p/10017957.html