16 Star 64 Fork 123

OpenHarmony / security_permission_lite

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
README_zh.md 15.89 KB
一键复制 编辑 原始数据 按行查看 历史
chennian 提交于 2022-09-23 10:51 . readme整改

应用权限管理

简介

OpenHarmony中应用和系统服务均运行在独立的沙箱中,进程空间和程序数据都是相互隔离的,以保护应用数据的安全性;但是运行在独立沙箱中的服务或应用同时需要对外提供一些API以实现所需功能,其他独立沙箱中的应用在跨进程访问这些API时,需要系统提供一种权限管理机制对这些API的访问者进行授权。

  • 应用权限管理提供了权限定义机制,允许系统服务和应用为自己的敏感API定义新的权限,其他应用必须申请此权限才能访问此敏感API;

  • 应用权限管理提供了权限申请机制,允许应用申请权限,这些权限由系统或者其他应用定义,权限申请通过后就能访问这个权限相关的系统或其他应用提供的敏感API;

  • 应用权限管理也为用户提供了一些必须的功能,方便用户查看和管理权限授予情况。

图 1 应用权限管理架构

应用权限管理为用户程序框架子系统提供权限管理功能,并且为上层应用提供权限申请和授权状态查询接口。本次开源的应用权限管理功能适用于轻量系统、小型系统和标准系统。

  • 轻量系统(mini system)面向MCU类处理器例如Arm Cortex-M、RISC-V 32位的设备,硬件资源极其有限,参考内存≥128KiB,可以提供多种轻量级网络协议,轻量级的图形框架,以及丰富的IOT总线读写部件等。可支撑的产品如智能家居领域的连接类模组、传感器设备、穿戴类设备等。
  • 小型系统(small system)面向应用处理器例如Arm Cortex-A的设备,参考内存≥1MiB,可以提供更高的安全能力、标准的图形框架、视频编解码的多媒体能力。可支撑的产品如智能家居领域的IP Camera、电子猫眼、路由器以及智慧出行域的行车记录仪等。

目录

/base/security/permission_lite
├── interfaces                         # 接口层
│   ├── innerkits                      # 内部接口层
│   └── kits                           # 外部接口层
└── services                           # 服务层
    ├── ipc_auth                       # IPC通信鉴权
    ├── js_api                         # JS API封装
    ├── pms                            # 权限管理逻辑和服务端
    ├── pms_base                       # 服务注册
    └── pms_client                     # 权限管理服务的客户端

使用

接口说明

轻量系统、小型系统应用权限管理:当前仅供系统应用和系统服务调用,具体API接口如下:

接口名

描述

int CheckPermission(int uid, const char *permissionName)

检查指定UID的应用进程是否具有访问系统服务API的权限

int CheckSelfPermission(const char *permissionName)

检查调用者是否具有访问系统服务API的权限

int QueryPermission(const char *identifier, PermissionSaved **permissions, int *permNum)

查询应用申请的所有权限,并检查权限是否被授予

int GrantPermission(const char *identifier, const char *permName)

将指定权限授予应用程序

int RevokePermission(const char *identifier, const char *permName)

收回应用程序的指定权限

int GrantRuntimePermission(int uid, const char *permissionName)

应用运行时动态授予指定权限

int RevokeRuntimePermission(int uid, const char *permissionName)

应用运行时动态撤销指定权限

int UpdatePermissionFlags(const char *identifier, const char *permissionName, const int flags)

更新应用程序指定的应用权限的标记

轻量系统、小型系统IPC通信鉴权

接口名

描述

int GetCommunicationStrategy(RegParams params, PolicyTrans **policies, unsigned int *policyNum)

服务注册过程中查询调用接口对应的访问策略,仅供Samgr调用

int IsCommunicationAllowed(AuthParams params)

检查访问主体进程是否有权限调用受访客体进程的接口,仅供Samgr调用

使用说明

轻量系统、小型系统应用权限管理

使用说明:以包管理器的应用权限开发为例进行讲解。开发过程中,首先需要明确涉及的敏感权限,并在config.json中声明该权限,在安装应用程序时,包管理器会调用应用权限管理组件的接口检查该权限是否被授予,若授予,安装流程正常进行,否则安装失败。

  1. 在开发过程中,包管理器明确需要安装应用的权限(ohos.permission.INSTALL_BUNDLE),并声明该权限; FA模型:需要在config.json中声明权限,示例:

    {
      "module": {
          "package": "ohos.demo.kitframework",
          "deviceType": [
              "phone", "tv","tablet", "car","smartWatch","sportsWatch","smartCamera", "smartVision"
          ],
          "reqPermissions": [{
            "name": "ohos.permission.INSTALL_BUNDLE",
            "reason": "install bundle",
            "usedScene": {
              "ability": [
                "KitFramework"
                ],
              "when": "always"
            }
          },
          {
            "name": "ohos.permission.LISTEN_BUNDLE_CHANGE",
            "reason": "install bundle",
            "usedScene": {
              "ability": [
                "KitFramework"
                ],
              "when": "always"
            }
          },
          {
            "name": "ohos.permission.GET_BUNDLE_INFO",
            "reason": "install bundle",
            "usedScene": {
              "ability": [
                "KitFramework"
                ],
              "when": "always"
            }
          }
        ]
      }
    }

    Stage模型:需要在module.json5中声明权限,示例:

    {
      "module": {
        "requestPermissions": [{
          "name": "ohos.permission.INSTALL_BUNDLE",
          "reason": "install bundle",
          "usedScene": {
            "ability": [
              "KitFramework"
            ],
            "when": "always"
          }
        },
        {
          "name": "ohos.permission.LISTEN_BUNDLE_CHANGE",
          "reason": "install bundle",
          "usedScene": {
            "ability": [
              "KitFramework"
            ],
            "when": "always"
          }
        },
        {
          "name": "ohos.permission.GET_BUNDLE_INFO",
          "reason": "install bundle",
          "usedScene": {
            "ability": [
              "KitFramework"
            ],
            "when": "always"
          }
        }]
      }
    }
  2. 当包管理器开发应用安装功能接口时,会调用权限管理相关接口检查自身是否具有安装应用程序的权限,例如:以安装应用的权限名"ohos.permission.INSTALL_BUNDLE"作为入参,调用CheckPermission接口检查包管理器是否具有安装应用的权限,如果有权限,安装流程继续执行,否则返回安装失败;

    constexpr static char PERMISSION_INSTALL_BUNDLE[] = "ohos.permission.INSTALL_BUNDLE";
    
    bool Install(const char *hapPath, const InstallParam *installParam, InstallerCallback installerCallback)
    {
        if ((hapPath == nullptr) || (installerCallback == nullptr) || (installParam == nullptr)) {
            HILOG_ERROR(HILOG_MODULE_APP, "BundleManager install failed due to nullptr parameters");
            return false;
        }
        // 检查ohos.permission.INSTALL_BUNDLE权限是否被授予
        if (CheckPermission(0, static_cast<const char *>(PERMISSION_INSTALL_BUNDLE)) != GRANTED) {
            HILOG_ERROR(HILOG_MODULE_APP, "BundleManager install failed due to permission denied");
            return false;  // 返回安装失败
        }
        // 安装流程
        ...
    }

轻量系统、小型系统IPC通信鉴权

使用说明:以BMS服务通过IPC通信方式对外开放接口为例,讲解如何通过IPC通信鉴权组件配置对应接口的访问策略。这里BMS在Samgr中注册的service为bundlems,为开放的接口注册的Feature为BmsFeature。

  1. 在源码路径下的头文件base/security/permission_lite/services/ipc_auth/include/policy_preset.h中配置相应的访问策略,访问策略主要有三种类型:

    (1)type为RANGE类型:允许某个特定范围UID的进程访问,需要指定uidMin和uidMax;

    (2)type为FIXED类型:允许指定的几个UID的进程访问,需要指定fixedUid,最多配置8个;

    (3)type为BUNDLENAME类型:只允许特定的应用访问,需要指定bundleName(包名);

    FeaturePolicy bmsFeature[] = {
        {
            "BmsFeature",
            {
                {
                    .type=FIXED,    // 允许指定UID的进程访问的方式
                    .fixedUid={2, 3, 8}
                },
                {
                    .type=RANGE,    // 允许特定范围内的UID的进程访问的方式
                    .uidMin=100,
                    .uidMax=__INT_MAX__,
                },
            }
        },
        {
            "BmsInnerFeature",
            {
                {
                    .type=FIXED,     // 允许指定UID的进程访问的方式
                    .fixedUid={2, 3, 8}
                },
                {
                    .type=RANGE,
                    .uidMin=100,
                    .uidMax=999,
                },
            }
        },
    };
  2. 将步骤1中定义的Feature的策略加配到全局策略中,需要配置feature数量;

    static PolicySetting g_presetPolicies[] = {
        {"permissionms", pmsFeature, 1},
        {"abilityms", amsFeature, 2},
        {"bundlems", bmsFeature, 2},  // 步骤1定义的BMS的feature,数量为2
        {"dtbschedsrv", dmsFeature, 1},
        {"samgr", samgrFeature, 1},
        {"appspawn", appspawnFeature, 1},
        {"WMS", wmsFeature, 1},
        {"bundle_daemon", bdsFeature, 1},
    };
  3. 将步骤1中定义的BmsFeature注册到Samgr;

    const char BMS_SERVICE[] = "bundlems";
    const char BMS_FEATURE[] = "BmsFeature";
    static void Init()
    {
        SamgrLite *sm = SAMGR_GetInstance();
        if (sm == nullptr) {
            return;
        }
        // 注册服务到Samgr
        sm->RegisterFeature(BMS_SERVICE, reinterpret_cast<Feature *>(BundleMsFeature::GetInstance()));
        sm->RegisterFeatureApi(BMS_SERVICE, BMS_FEATURE,
            GetBmsFeatureApi(reinterpret_cast<Feature *>(BundleMsFeature::GetInstance())));
        HILOG_DEBUG(HILOG_MODULE_APP, "BundleMS feature start success");
    }
    APP_FEATURE_INIT(Init);

完成以上开发步骤后,开发者在Samgr注册服务时,Samgr会调用IPC通信鉴权组件的GetCommunicationStrategy接口获取服务的访问策略;当其他服务或应用通过IPC方式访问这些服务时,Samgr会调用IPC通信鉴权组件的IsCommunicationAllowed接口检查调用者服务的权限,如果满足访问策略,则可以访问开发者接口,否则拒绝访问。

相关仓

安全子系统 security_permission_lite

1
https://gitee.com/openharmony/security_permission_lite.git
git@gitee.com:openharmony/security_permission_lite.git
openharmony
security_permission_lite
security_permission_lite
master

搜索帮助