> ## Documentation Index
> Fetch the complete documentation index at: https://dragonwingdocs.qualcomm.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 定制安全服务

支持对内存和 SEPolicy 进行定制。对于大尺寸的可信应用程序，你可以定制内存区域。

## **定制内存**

内存定制功能仅面向具有授权访问权限的许可用户。如果你拥有访问权限，请参阅 [Qualcomm Linux Security Guide - Addendum](https://docs.qualcomm.com/bundle/resource/topics/80-80021-11A/customize-fru.html)。

## **定制 SEPolicy**

Qualcomm SEPolicy 依赖于上游 SEPolicy。因此，构建和定制 SEPolicy 使用的是上游 SEPolicy 的 make 系统。

对上游 selinux 策略进行定制：

* 在 [SELinuxProject/refpolicy](https://github.com/SELinuxProject/refpolicy/) 上创建 Pull Request。
* 将更改以补丁形式存放在 `meta-qcom-distro/recipes-security/refpolicy/refpolicy-targeted/` 目录中，并在 SRC\_URI `meta-qcom-distro/recipes-security/refpolicy/refpolicy-targeted_git.bbappend` 中包含该补丁
* 在 [qualcomm-linux/meta-qcom-distro](https://github.com/qualcomm-linux/meta-qcom-distro) 上创建 Pull Request
* 将补丁发送到 \[meta-selinux][https://github.com/ni/meta-selinux。请参阅](https://github.com/ni/meta-selinux。请参阅) \[向 meta-selinux 发送补丁的步骤][https://github.com/ni/meta-selinux/blob/nilrt/master/scarthgap/MAINTAINERS。此步骤是可选的，因为在](https://github.com/ni/meta-selinux/blob/nilrt/master/scarthgap/MAINTAINERS。此步骤是可选的，因为在) meta-selinux 升级时，更改会自动从上游 refpolicy 同步。

对 ostree selinux 策略进行定制：

* 在现有 sepolicy 代码基础上创建补丁。
* 将更改以补丁形式存放在 `meta-updater/dynamic-layers/selinux/recipes-security/refpolicy/refpolicy-targeted/` 目录中，并在 SRC\_URI `meta-updater/dynamic-layers/selinux/recipes-security/refpolicy/refpolicy-targeted_git.bbappend` 中包含该补丁
* 在 [uptane/meta-updater](https://github.com/uptane/meta-updater) 上创建 Pull Request

对 qualcomm selinux 策略进行定制：

* 在现有 sepolicy 代码基础上创建补丁。
* 将更改以补丁形式存放在 `meta-qcom/dynamic-layers/selinux/recipes-security/refpolicy/refpolicy-targeted/` 目录中，并在 SRC\_URI `meta-qcom/dynamic-layers/selinux/recipes-security/refpolicy/refpolicy-targeted_git.bbappend` 中包含该补丁
* 在 [qualcomm-linux/meta-qcom](https://github.com/qualcomm-linux/meta-qcom) 上创建 Pull Request

为新服务/模块添加 sepolicy：

* 在现有 selinux 策略基础上创建新的 `<module_name>.fc`、`<module_name>.if`、`<module_name>.te` 文件，并添加所需的 selinux 规则。
* 将新模块上游提交到 [SELinuxProject/refpolicy](https://github.com/SELinuxProject/refpolicy/)。如果服务/模块是私有的，请跳过此步骤。
* 确定服务/模块所在的 yocto 层。在该层内，创建 `dynamic-layers/.../refpolicy-targeted/` 用于以补丁形式保存 sepolicy，并创建 `dynamic-layers/.../refpolicy-targeted_git.bbappend` 配方以包含补丁。

Qualcomm 代码被配置为模块化 SEPolicy 模式，SELinux 类型为 targeted。要修改 SEPolicy 模式和 SELinux 类型，请执行以下操作：

> * 要更改 SELinux 类型和模式，可以编辑 [Selinux 基础文件](https://github.com/qualcomm-linux/meta-qcom-distro/blob/main/conf/distro/include/qcom-distro-selinux.inc)。

### **编译 SEPolicy**

1. 设置构建环境。有关说明，请参阅 [Qualcomm Linux 构建指南](https://dragonwingdocs.qualcomm.com/Key-Documents/Firmware-Guide/build-firmware)。
2. 仅编译 selinux 发行版：
   ```text theme={null}
   kas build meta-qcom/ci/<machine-name>.yml:meta-qcom/ci/qcom-distro-selinux.yml
   ```
3. 编译 catchall 发行版（该发行版包含 selinux 以及其他一些发行版特性）：
   ```text theme={null}
   kas build meta-qcom/ci/<machine-name>.yml:meta-qcom/ci/qcom-distro-catchall.yml
   ```

### **修改和构建**

你也可以进行增量修改和构建。

Ubuntu 上的 audit2allow 和相关研究工具不支持 policy version35。你可以通过安装 policycoreutils-python-utils 来运行 audit2allow。

```text theme={null}
sudo apt install policycoreutils-python-utils
```

然后在此 shell 中运行 audit2allow。

从目标设备的 `/etc/selinux/targeted/policy/policy.35` 拉取 policy version35。该策略也可在构建树中获得：`build/tmp/sysroots-components/<machine_name>/refpolicy-targeted/etc/selinux/targeted/policy/policy.35`

### **捕获拒绝记录**

如果 `denials.txt` 是捕获拒绝记录的文件，请使用以下命令解决这些拒绝：

```text theme={null}
audit2allow -i denails.txt -p policy.35
```

## **配置重放保护内存块**

重放保护内存块（RPMB）配置在设备整个生命周期中是一次性过程，且不可逆。

* 为安全启动设备配置 RPMB：
  > * 在安全启动过程中，RPMB 会自动使用生产密钥进行配置。
  > * 请勿在启用安全启动之前配置 RPMB。
  > * 对于安全启动设备，无需执行任何显式的 RPMB 配置步骤。
* 为非安全启动设备配置 RPMB：
  > * 你可以使用测试密钥配置 RPMB，用于测试和演练某些用例，以观察类似于安全启动设备的功能。示例用例包括：
  >   > * 安全文件系统（SFS）文件版本信息和防回滚保护
  >   > * 可信应用程序防回滚保护
  >
  > <Warning>
  >   一旦使用测试密钥配置了 RPMB，就无法再在该设备上启用安全启动。
  > </Warning>
  >
  > * 要使用测试密钥配置 RPMB，请参阅[验证 RPMB 配置状态](./verify-the-security-configurations-of-qualcomm-linux#verify-rpmb-provisioning-status)

## **后续步骤**

* 有关常见的日志记录和调试技术，请参阅[调试 Qualcomm TEE 和安全设备](./debug-qualcomm-tee-and-secure-devices)。
* 要了解如何开发和运行可信应用程序与客户端应用程序，以及获取示例代码和示例，请参阅[开发可信应用程序和客户端应用程序](./develop-trusted-and-client-applications)。
