niobe/base/security/permission
houpengfei 396aa09915 init 2021-09-22 21:52:48 +08:00
..
figures init 2021-09-22 21:52:48 +08:00
frameworks/permission_standard init 2021-09-22 21:52:48 +08:00
interfaces init 2021-09-22 21:52:48 +08:00
services init 2021-09-22 21:52:48 +08:00
.gitattributes init 2021-09-22 21:52:48 +08:00
BUILD.gn init 2021-09-22 21:52:48 +08:00
LICENSE init 2021-09-22 21:52:48 +08:00
OAT.xml init 2021-09-22 21:52:48 +08:00
README.md init 2021-09-22 21:52:48 +08:00
README_zh.md init 2021-09-22 21:52:48 +08:00
ohos.build init 2021-09-22 21:52:48 +08:00

README.md

security_permission

Introduction

In OpenHarmony, apps and system services run in independent sandboxes. Both processes and data are isolated from each other to protect the security of app data. However, services or apps running in the sandboxes provide some APIs to implement specific functionalities. To access these APIs across processes, apps in other sandboxes need the required permissions, which are granted and managed based on a permission management mechanism.

  • App permission management provides a mechanism for defining permissions, allowing system services and apps to define new permissions for their sensitive APIs. To access these APIs, other apps need the required permissions.

  • App permission management also allows apps to request permissions that are defined by the system or other apps. Upon obtaining the permissions, apps can access the sensitive APIs provided by the system or other apps.

  • In addition, app permission management allows users to view and manage the permission granting status.

Figure 1 App permission management architecture

App permission management provides permission management for the application framework subsystem and provides APIs for apps to request permissions and query the permission granting status. Currently, app permission management is available for large and standard systems.

  • Mini system: refers to the system running on the devices whose memory is greater than or equal to 128 KB and that are equipped with MCU processors such as ARM Cortex-M and 32-bit RISC-V. This system provides multiple lightweight network protocols and graphics frameworks, and a wide range of read/write components for the IoT bus. Typical products include connection modules, sensors, and wearables for smart home.
  • Small system: refers to the system running on the devices whose memory is greater than or equal to 1 MB and that are equipped with app processors such as ARM Cortex-A. This system provides higher security capabilities, standard graphics frameworks, and video encoding and decoding capabilities. Typical products include smart home IP cameras, electronic cat eyes, and routers, and event data recorders EDRs for smart travel.
  • Standard system: refers to the system running on the devices whose memory is greater than or equal to 128 MB and that are equipped with app processors such as ARM Cortex-A. This system provides a complete application framework supporting the enhanced interaction, 3D GPU, hardware composer, diverse components, and rich animations. This system applies to high-end refrigerator displays.

Directory Structure

/base/security/permission
├── frameworks                         # Frameworks
│   └── permission_standard            # Permission management framework for the standard system
├── interfaces                         # APIs
│   ├── innerkits                      # Internal APIs
│   │   ├── permission_lite            # Internal permission management APIs for the mini and small systems
│   │   └── permission_standard        # Internal permission management APIs for the standard system
│   └── kits                           # External APIs
│       ├── permission_lite            # External permission management APIs for the mini and small systems
│       └── permission_standard        # External permission management APIs for the standard system
└── services                           # Services
    ├── permission_lite                # Permission management services for the mini and small systems
    └── permission_standard            # Permission management services for the standard system

Constraints

  • Currently, C++ APIs are available only for local permission management in the standard system. Distributed permission management APIs are not provided yet.

Usage

Available APIs

App permission management for a standard system: provides basic permission management and verification capabilities for the application framework subsystem of a standard system and is unavailable for third-party apps. The following table describes the available APIs.

API

Description

int VerifyPermission(const string& bundleName, const string& permissionName, int userId)

Checks whether a specified app has been granted the given permission.

bool CanRequestPermission(const string& bundleName, const string& permissionName, int userId)

Checks whether a specified app can request the given permission through a pop-up window.

int GrantUserGrantedPermission(const string& bundleName, const string& permissionName, int userId)

Grants a specified user_grant permission to the given app.

int GrantSystemGrantedPermission(const string& bundleName, const string& permissionName)

Grants a specified system_grant permission to the given app.

int RevokeUserGrantedPermission(const string& bundleName, const string& permissionName, int userId)

Revokes a specified user_grant permission from the given app.

int RevokeSystemGrantedPermission(const string& bundleName, const string& permissionName)

Revokes a specified system_grant permission from the given app.

int AddUserGrantedReqPermissions(const string& bundleName, const std::vector<string>& permList, int userId)

Adds user_grant permissions requested by a specified app.

int AddSystemGrantedReqPermissions(const string& bundleName, const std::vector<string>& permList)

Adds system_grant permissions requested by a specified app.

int RemoveUserGrantedReqPermissions(const string& bundleName, int userId)

Removes all the user_grant permissions requested by a specified app.

int RemoveSystemGrantedReqPermissions(const string& bundleName)

Removes all the system_grant permissions requested by a specified app.

int AddDefPermissions(const std::vector<PermissionDef>& permList)

Adds the permissions defined by the app.

int GetDefPermission(const string& permissionName, PermissionDef& permissionDefResult)

Obtains the definition of the permission with a specified name.

App permission management for a mini or small system: The following table lists the available APIs, which can be called only by system apps and services.

API

Description

int CheckPermission(int uid, const char *permissionName)

Checks whether the app with a specified UID has the permission to access system service APIs.

int CheckSelfPermission(const char *permissionName)

Checks whether the caller has the permission to access system service APIs.

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

Queries all permissions requested by the app and checks whether the requested permissions have been granted.

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

Grants a specified permission to the app.

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

Revokes a specified permission from the app.

int GrantRuntimePermission(int uid, const char *permissionName)

Grants a specified runtime permission to the app.

int RevokeRuntimePermission(int uid, const char *permissionName)

Revokes a specified runtime permission from the app.

IPC authentication for a mini or small system

API

Description

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

Obtains the access policies of a service API.

int IsCommunicationAllowed(AuthParams params)

Checks whether a process has the permission to access an API of another process.

Usage Guidelines

App permission management for a standard system

The APIs provided are for internal use and unavailable to developers. During the authentication, you only need to call VerifyPermission.

  1. Determine the app UID and the name of the permission to verify.
  2. Obtain the app bundle name based on the app UID.
  3. Obtain the user ID of the app based on the UID.
  4. Pass the permission name, bundle name, and user ID to VerifyPermission(string permissionName, string bundleName, int userId).
  5. Obtain the verification result.

App permission management for a mini or small system

This section uses the bundle manager as an example to describe the app permission development. Before starting development, you need to declare the required sensitive permissions in the config.json file. During app installation, the BMS calls APIs of the app permission management component to check whether the required permissions have been granted. If yes, the installation proceeds; if not, the installation fails.

  1. Declare the required permission **ohos.permission.INSTALL\_BUNDLE** in the config.json file.

    {
      ...
      "module": {
          "package": "ohos.demo.kitframework",
          "deviceType": [
              "phone", "tv","tablet", "pc","car","smartWatch","sportsWatch","smartCamera", "smartVision"
          ],
          "reqPermissions": [{
            // Declare the ohos.permission.INSTALL_BUNDLE permission required for installing the app.
            "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. The BMS calls the corresponding API of the app permission management component for example, the **CheckPermission** function with **ohos.permission.INSTALL\_BUNDLE** as an input parameter to check whether the BMS has the permission to install the app. If yes, the installation proceeds; if not, the installation fails.

    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;
        }
        // Check whether the ohos.permission.INSTALL_BUNDLE permission has been granted.
        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;  // App installation fails.
        }
        // App installation process
        ...
    }
    

IPC authentication for a mini or small system

This section uses the bundle manager as an example to describe how to configure access policies for APIs provided by the IPC authentication component. In this example, the service registered by BMS with Samgr is bundlems, and the feature registered for open APIs is BmsFeature.

  1. Configure access policies in the base/security/permission/services/permission_lite/ipc_auth/include/policy_preset.h file. Access policies are classified into the following three types:

    1. RANGE: Processes with a specified range of UIDs can access BMS APIs. uidMin and uidMax must be specified.

    2. FIXED: Processes with specified UIDs can access BMS APIs. fixedUid must be specified, and a maximum of eight UIDs are allowed.

    3. BUNDLENAME: An app with a specified bundleName can access BMS APIs.

    FeaturePolicy bmsFeature[] = {
        {
            "BmsFeature",
            {
                {
                    .type=FIXED,    // Processes with specified UIDs can access BMS APIs.
                    .fixedUid={2, 3, 8}
                },
                {
                    .type=RANGE,    // Processes with a specified range of UIDs can access BMS APIs.
                    .uidMin=100,
                    .uidMax=__INT_MAX__,
                },
            }
        },
        {
            "BmsInnerFeature",
            {
                {
                    .type=FIXED,     // Processes with specified UIDs can access BMS APIs.
                    .fixedUid={2, 3, 8}
                },
                {
                    .type=RANGE,
                    .uidMin=100,
                    .uidMax=999,
                },
            }
        },
    };
    
  2. Add the policies configured for the features in Step 1 to the global policy settings. You need to set the number of features.

    static PolicySetting g_presetPolicies[] = {
        {"permissionms", pmsFeature, 1},
        {"abilityms", amsFeature, 2},
        {"bundlems", bmsFeature, 2},  // Add the policies configured for the two features in [Step 1](#li15901515152517) to the global policy settings.
        {"dtbschedsrv", dmsFeature, 1},
        {"samgr", samgrFeature, 1},
        {"appspawn", appspawnFeature, 1},
        {"WMS", wmsFeature, 1},
        {"bundle_daemon", bdsFeature, 1},
    };
    
  3. Register the BmsFeature defined in Step 1 with Samgr.

    const char BMS_SERVICE[] = "bundlems";
    const char BMS_FEATURE[] = "BmsFeature";
    static void Init()
    {
        SamgrLite *sm = SAMGR_GetInstance();
        if (sm == nullptr) {
            return;
        }
        // Register the BmsFeature with 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);
    

When you register a service with Samgr, Samgr calls the GetCommunicationStrategy function of the IPC authentication component to obtain access policies of the service. When other services or apps access this service through IPC, Samgr calls the IsCommunicationAllowed function of the IPC authentication component to check whether the services or apps have the access permission.

Repositories Involved

security security_permission