1e41f4b71Sopenharmony_ci# Seccomp Policy Development
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci## Overview
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci### Introduction
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ciSecure computing mode (Seccomp) is a security mechanism provided by the Linux kernel. In the Linux system, a large number of system calls can be opened to user-mode programs without any restrictions. However, not all of these system calls are necessarily needed for user-mode programs. In this case, abuse of system calls can lead to system threats. For example, if a process has a security vulnerability, an attacker can run a shellcode segment to trigger system calls that are not triggered during normal execution, resulting in privilege escalation or private information leakage. To prevent such security risks, Seccomp limits the scope of system calls that can be used by programs, so as to reduce system exposure and improve security.
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci### Operating Mechanism
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci1. Basic mechanism
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ci    Seccomp policies exist in the form of policy files. During compilation and building, a policy file is parsed to generate a source file that contains the BPF instruction policies, and then the source file is compiled into a dynamic policy library. During startup of a user-mode process, Seccomp system calls are invoked to load the BPF instruction policies into the kernel through the dynamic policy library.
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ci2. Basic features
16e41f4b71Sopenharmony_ci    - A child process inherits the Seccomp policies of its parent process.
17e41f4b71Sopenharmony_ci    - After a Seccomp policy is loaded to the kernel during process running, the policy exists in the memory as a singly linked list and cannot be modified.
18e41f4b71Sopenharmony_ci    - Seccomp policies can be set for a process for multiple times. When a process executes a system call, the kernel traverses the policies specified for the nodes in the singly linked list, and then compares the policies to obtain the policy with the highest priority.
19e41f4b71Sopenharmony_ci
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_ci### Constraints
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ci- System restrictions<br>The system used must be a standard system, and the options listed below must be enabled in the kernel. You can find the kernel option configuration file of the product in **//kernel/linux/config/{linux_version}/arch/{target_cpu}/configs/**.
24e41f4b71Sopenharmony_ci    ```shell
25e41f4b71Sopenharmony_ci    CONFIG_HAVE_ARCH_SECCOMP=y
26e41f4b71Sopenharmony_ci    CONFIG_HAVE_ARCH_SECCOMP_FILTER=y
27e41f4b71Sopenharmony_ci    CONFIG_SECCOMP=y
28e41f4b71Sopenharmony_ci    CONFIG_SECCOMP_FILTER=y
29e41f4b71Sopenharmony_ci    ```
30e41f4b71Sopenharmony_ci
31e41f4b71Sopenharmony_ci- Feature restrictions
32e41f4b71Sopenharmony_ci    - The Seccomp policy for non-privileged processes complies with the baseline blocklist mechanism.
33e41f4b71Sopenharmony_ci    - If a process needs to use system calls in the baseline blocklist, the system calls must be declared in the privileged process policy file.
34e41f4b71Sopenharmony_ci    - The same Seccomp policy is enabled for all application processes.
35e41f4b71Sopenharmony_ci    - The same Seccomp policy is enabled for most of system service processes.
36e41f4b71Sopenharmony_ci    - Personalized Seccomp policies can be enabled for the native service processes incubated by the init process.
37e41f4b71Sopenharmony_ci
38e41f4b71Sopenharmony_ci## Enabling Seccomp
39e41f4b71Sopenharmony_ci
40e41f4b71Sopenharmony_ci### When to Use
41e41f4b71Sopenharmony_ci
42e41f4b71Sopenharmony_ciTo meet product security requirements, you can enable Seccomp to limit the scope of system calls that can be invoked by processes. The development procedure below describes how to enable the basic functions and policies of Seccomp. Note that the basic functions must meet the feature restrictions described in [Constraints](#constraints). For details about the basic policy file, see [Policy File Overview](#policy-file-overview).
43e41f4b71Sopenharmony_ci
44e41f4b71Sopenharmony_ci### How to Develop
45e41f4b71Sopenharmony_ci
46e41f4b71Sopenharmony_ci1. Add the following field to **vendor/Product vendor/Product name/config.json**:
47e41f4b71Sopenharmony_ci    ```c
48e41f4b71Sopenharmony_ci    "build_seccomp": true
49e41f4b71Sopenharmony_ci    ```
50e41f4b71Sopenharmony_ci    The following is an example of adding the **build_seccomp** field to the product configuration file:
51e41f4b71Sopenharmony_ci    ```c
52e41f4b71Sopenharmony_ci    {
53e41f4b71Sopenharmony_ci        "product_name": "MyProduct",
54e41f4b71Sopenharmony_ci        "version": "3.0",
55e41f4b71Sopenharmony_ci        "type": "standard",
56e41f4b71Sopenharmony_ci        "target_cpu": "arm",
57e41f4b71Sopenharmony_ci        "ohos_version": "OpenHarmony 1.0",
58e41f4b71Sopenharmony_ci        "device_company": "MyProductVendor",
59e41f4b71Sopenharmony_ci        "board": "MySOC",
60e41f4b71Sopenharmony_ci        "enable_ramdisk": true,
61e41f4b71Sopenharmony_ci        "build_seccomp": true
62e41f4b71Sopenharmony_ci        "subsystems": [
63e41f4b71Sopenharmony_ci        {
64e41f4b71Sopenharmony_ci            "subsystem": "ace",
65e41f4b71Sopenharmony_ci            "components": [
66e41f4b71Sopenharmony_ci            { "component": "ace_engine_lite","features":[""] }
67e41f4b71Sopenharmony_ci            ]
68e41f4b71Sopenharmony_ci        },
69e41f4b71Sopenharmony_ci        ...
70e41f4b71Sopenharmony_ci        ]
71e41f4b71Sopenharmony_ci    }
72e41f4b71Sopenharmony_ci    ```
73e41f4b71Sopenharmony_ci2. Perform a full build on the product code to generate an image.
74e41f4b71Sopenharmony_ci    ```
75e41f4b71Sopenharmony_ci    ./build.sh --product-name *product name* --ccache --build-target make_all --target-cpu *specified CPU*
76e41f4b71Sopenharmony_ci    ```
77e41f4b71Sopenharmony_ci3. Burn the image into the device.
78e41f4b71Sopenharmony_ci
79e41f4b71Sopenharmony_ci### Debugging and Verification
80e41f4b71Sopenharmony_ci
81e41f4b71Sopenharmony_ciCheck whether Seccomp is enabled for application processes and system service processes.
82e41f4b71Sopenharmony_ci1. Run the shell command to obtain the process ID (that is, **target pid**) of the target process.
83e41f4b71Sopenharmony_ci    ```
84e41f4b71Sopenharmony_ci    ps -ef | grep xxx
85e41f4b71Sopenharmony_ci    ```
86e41f4b71Sopenharmony_ci    Information similar to the following is displayed, in which **target pid** is **1686**:
87e41f4b71Sopenharmony_ci    ```
88e41f4b71Sopenharmony_ci    media         1686     1 0 08:16:12 ?     00:00:00 xxx
89e41f4b71Sopenharmony_ci    root          1869  1678 4 10:32:29 pts/0 00:00:00 grep xxx
90e41f4b71Sopenharmony_ci    ```
91e41f4b71Sopenharmony_ci
92e41f4b71Sopenharmony_ci2. Check the process status to determine whether Seccomp is enabled.
93e41f4b71Sopenharmony_ci    ```shell
94e41f4b71Sopenharmony_ci    cat /proc/[target pid]/status | grep Seccomp
95e41f4b71Sopenharmony_ci    ```
96e41f4b71Sopenharmony_ci    Information similar to the following is displayed:
97e41f4b71Sopenharmony_ci    ```
98e41f4b71Sopenharmony_ci    Seccomp:        2
99e41f4b71Sopenharmony_ci    Seccomp_filters:        1
100e41f4b71Sopenharmony_ci    ```
101e41f4b71Sopenharmony_ci    **Table 1** Description of the Seccomp status
102e41f4b71Sopenharmony_ci    |  Parameter |  Description |
103e41f4b71Sopenharmony_ci    |  ---  |  ---  |
104e41f4b71Sopenharmony_ci    |  Seccomp  |  - **0**: disabled.<br>- **1**: enabled with the strict mode. Only the read, write, exit, and sigreturn system calls are allowed.<br>- **2**: enabled with the filter mode. The customized policies can be enabled by loading BPF instruction sets. |
105e41f4b71Sopenharmony_ci    |  Seccomp_filters  |  Number of Seccomp policies set for a process. |
106e41f4b71Sopenharmony_ci
107e41f4b71Sopenharmony_ci## Customizing Seccomp Policies for a Process
108e41f4b71Sopenharmony_ci
109e41f4b71Sopenharmony_ci### When to Use
110e41f4b71Sopenharmony_ci
111e41f4b71Sopenharmony_ciIf the basic Seccomp policy has been enabled for a product, you can customize Seccomp policies for native service processes incubated by the init process to adapt to diversified security requirements. In this case, the Seccomp policies of other native service processes remain unchanged.
112e41f4b71Sopenharmony_ci
113e41f4b71Sopenharmony_ci### How to Develop
114e41f4b71Sopenharmony_ci
115e41f4b71Sopenharmony_ci1. Collect statistics on the system calls required by the 32-bit and 64-bit systems by using the static analysis and Strace statistics methods described in [System Call Statistic Methods](#system-call-statistic-methods). In this way, you will obtain the initial Seccomp policy.
116e41f4b71Sopenharmony_ci2. Write a policy file. For details, see [How to Write a Common Policy File](#how-to-write-a-common-policy-file).
117e41f4b71Sopenharmony_ci3. Write and build the **BUILD.gn** file.
118e41f4b71Sopenharmony_ci
119e41f4b71Sopenharmony_ci    1.  Store the policy file in the code repository of the service subsystem and create a **BUILD.gn** file. For example, create the **seccomp_policy** folder in the service code repository, and create the policy file and **BUILD.gn** file in the folder.
120e41f4b71Sopenharmony_ci            
121e41f4b71Sopenharmony_ci        ```shell
122e41f4b71Sopenharmony_ci        //path/to/code/seccomp_policy
123e41f4b71Sopenharmony_ci        ├── BUILD.gn
124e41f4b71Sopenharmony_ci        └── example.seccomp.policy
125e41f4b71Sopenharmony_ci        ```
126e41f4b71Sopenharmony_ci    2. To parse the policy file and build the policy dynamic library, use the **ohos_prebuilt_seccomp** template to declare the Seccomp policy file path of the process in the **BUILD.gn** file. The **ohos_prebuilt_seccomp** template is defined in the **//base/startup/init/services/modules/seccomp/scripts/seccomp_policy_fixer.gni** file. The following table describes the parameters in the template.
127e41f4b71Sopenharmony_ci    
128e41f4b71Sopenharmony_ci        **Table 2** Parameters in the ohos_prebuilt_seccomp template
129e41f4b71Sopenharmony_ci        |  Parameter |  Description |
130e41f4b71Sopenharmony_ci        |  ---  |  ---  |
131e41f4b71Sopenharmony_ci        |  sources  |  Path of the policy configuration file, mandatory.|
132e41f4b71Sopenharmony_ci        |  filtername  |  Filter name, mandatory. The value must be the same as the value of **Services name** in the [boot configuration file](subsys-boot-init-cfg.md) of the process. Otherwise, the attempt to enable Seccomp will fail. This parameter determines the name of the dynamic policy library generated after the build. For example, if **filtername** is set to **xxx**, the name of the dynamic policy library is **libxxx_filter.z.so**. |
133e41f4b71Sopenharmony_ci        |  process_type  |  Process type, mandatory. If the process is one incubated by the init process, set this parameter to **system**; if the process is an application process, set this parameter to **app**.|
134e41f4b71Sopenharmony_ci        |  part_name  |  Part name, mandatory. |
135e41f4b71Sopenharmony_ci        |  subsystem_name  |  Subsystem name, mandatory. |
136e41f4b71Sopenharmony_ci        |  install_enable  |  Option specifying whether to install the policy file to the image, mandatory. Set the value to **true**. |
137e41f4b71Sopenharmony_ci        |  install_images  |  Installation location in the image, mandatory. |
138e41f4b71Sopenharmony_ci    
139e41f4b71Sopenharmony_ci        Example
140e41f4b71Sopenharmony_ci        ```python
141e41f4b71Sopenharmony_ci        #Import the template file.
142e41f4b71Sopenharmony_ci        import("//base/startup/init/services/modules/seccomp/scripts/seccomp_policy_fixer.gni")
143e41f4b71Sopenharmony_ci
144e41f4b71Sopenharmony_ci        #Use the ohos_prebuilt_seccomp template.
145e41f4b71Sopenharmony_ci        ohos_prebuilt_seccomp("xxxx_seccomp_filter") {
146e41f4b71Sopenharmony_ci            sources = [ "xxxx.seccomp.policy" ]
147e41f4b71Sopenharmony_ci            
148e41f4b71Sopenharmony_ci            filtername = "xxx"
149e41f4b71Sopenharmony_ci
150e41f4b71Sopenharmony_ci            process_type = "system"
151e41f4b71Sopenharmony_ci
152e41f4b71Sopenharmony_ci            part_name = "xx_part_name"
153e41f4b71Sopenharmony_ci            subsystem_name = "x_subsystem_name"
154e41f4b71Sopenharmony_ci
155e41f4b71Sopenharmony_ci            install_enable = true
156e41f4b71Sopenharmony_ci            install_images = [ "xxxxx" ]
157e41f4b71Sopenharmony_ci        }
158e41f4b71Sopenharmony_ci        ```
159e41f4b71Sopenharmony_ci    3. Add the build target of **xxxx_seccomp_filter** to other **BUILD.gn** files.
160e41f4b71Sopenharmony_ci        ```python
161e41f4b71Sopenharmony_ci        if (build_seccomp) {
162e41f4b71Sopenharmony_ci            deps += [ "path:xxxx_seccomp_filter" ]
163e41f4b71Sopenharmony_ci        }
164e41f4b71Sopenharmony_ci        ```
165e41f4b71Sopenharmony_ci4. Build the dynamic policy library **libxxx_filter.z.so**.
166e41f4b71Sopenharmony_ci    ```shell
167e41f4b71Sopenharmony_ci    ./build.sh --product-name *product name* --ccache --build-target xxxx_seccomp_filter --target-cpu *specified CPU*
168e41f4b71Sopenharmony_ci    ```
169e41f4b71Sopenharmony_ci    If an error message that contains the following information is reported, the process needs to use the system calls in the baseline blocklist. In such a case, you need to declare the corresponding system call in **privileged_process.seccomp.policy**. For details, see [How to Write a Privileged Process Policy File](#how-to-write-a-privileged-process-policy-file). After the declaration is done, try again until the build is successful.
170e41f4b71Sopenharmony_ci    ```shell
171e41f4b71Sopenharmony_ci    xx of allow list is in block list
172e41f4b71Sopenharmony_ci    ```
173e41f4b71Sopenharmony_ci5. Use the hdc tool to push the dynamic policy library to the device and restart the device.
174e41f4b71Sopenharmony_ci    ```shell
175e41f4b71Sopenharmony_ci    # Push an appropriate library path based on the installation location in the image. For example, if the image is **system** and the system architecture is 32-bit, the path of the dynamic policy library is **/system/lib/seccomp/**.
176e41f4b71Sopenharmony_ci    hdc shell mount -rw -o remount /
177e41f4b71Sopenharmony_ci    hdc file send /path/to/libxxx_filter.z.so /path/to/lib(or lib64)/seccomp/
178e41f4b71Sopenharmony_ci    hdc shell reboot
179e41f4b71Sopenharmony_ci    ```
180e41f4b71Sopenharmony_ci6. Use the [audit statistics](#audit-statistics) method to check and supplement the Seccomp policies. Repeat steps 4 to 6 until the process can run properly.
181e41f4b71Sopenharmony_ci
182e41f4b71Sopenharmony_ci### Debugging and Verification
183e41f4b71Sopenharmony_ci
184e41f4b71Sopenharmony_ci1. If Seccomp is not enabled for the target process, [check the Seccomp status](#debugging-and-verification) of the target process.
185e41f4b71Sopenharmony_ci2. If the process is terminated and audit log information is present in the kernel logs, the Seccomp policy is enabled but the policy list is incomplete. You can find an example audit log in [Audit Statistics](#audit-statistics).
186e41f4b71Sopenharmony_ci3. If the process is not terminated, comment out the system calls (for example, **setuid**) related to the specified uid in the Seccomp policy file. Rebuild the dynamic policy library, push the library to the image, and restart the process. Then, check whether the process is terminated by Seccomp. If the process is terminated, Seccomp has been enabled.
187e41f4b71Sopenharmony_ci
188e41f4b71Sopenharmony_ci## FAQs
189e41f4b71Sopenharmony_ci
190e41f4b71Sopenharmony_ci### How do I determine whether a process termination is caused by Seccomp?
191e41f4b71Sopenharmony_ci
192e41f4b71Sopenharmony_ci**Symptom**
193e41f4b71Sopenharmony_ci
194e41f4b71Sopenharmony_ciIf a process is terminated under certain conditions, how do I determine whether the issue is caused by Seccomp?
195e41f4b71Sopenharmony_ci
196e41f4b71Sopenharmony_ci**Solution**
197e41f4b71Sopenharmony_ci
198e41f4b71Sopenharmony_ciUse either of the following methods:
199e41f4b71Sopenharmony_ci
200e41f4b71Sopenharmony_ci- Check the crash stack backtrace log of the process. If the signal in the log is **SIGSYS**, the process termination is caused by the Seccomp mechanism. To view the crash stack backtrace content, run the following shell command:
201e41f4b71Sopenharmony_ci    ```shell
202e41f4b71Sopenharmony_ci    cat /data/log/faultlog/faultlogger/crash stack backtrace log
203e41f4b71Sopenharmony_ci    ```
204e41f4b71Sopenharmony_ci    Example output:
205e41f4b71Sopenharmony_ci    ```shell
206e41f4b71Sopenharmony_ci    Generated by HiviewDFX@OpenHarmony
207e41f4b71Sopenharmony_ci    ================================================================
208e41f4b71Sopenharmony_ci    Device info:xxx
209e41f4b71Sopenharmony_ci    Build info:xxx
210e41f4b71Sopenharmony_ci    Module name:xxx
211e41f4b71Sopenharmony_ci    Pid:xxxx
212e41f4b71Sopenharmony_ci    Uid:xxxxx
213e41f4b71Sopenharmony_ci    Reason:Signal:SIGSYS(UNKNOWN)
214e41f4b71Sopenharmony_ci    ...
215e41f4b71Sopenharmony_ci    ```
216e41f4b71Sopenharmony_ci- Check whether the process is still terminated after Seccomp is disabled. If the process runs properly after Seccomp is disabled, the process termination is caused by Seccomp.
217e41f4b71Sopenharmony_ci
218e41f4b71Sopenharmony_ci    Seccomp is enabled by default. When the device operation mode is set to **root**, you can run the shell command to set the corresponding system parameter to disable Seccomp of the entire system.
219e41f4b71Sopenharmony_ci    ```shell
220e41f4b71Sopenharmony_ci    # Set the system parameter to disable Seccomp and restart the process.
221e41f4b71Sopenharmony_ci    param set persist.init.debug.seccomp.enable 0; reboot
222e41f4b71Sopenharmony_ci    # Set the system parameter to enable Seccomp and restart the process.
223e41f4b71Sopenharmony_ci    param set persist.init.debug.seccomp.enable 1; reboot
224e41f4b71Sopenharmony_ci    ```
225e41f4b71Sopenharmony_ci
226e41f4b71Sopenharmony_ci## Reference
227e41f4b71Sopenharmony_ci
228e41f4b71Sopenharmony_ci### Seccomp source code directory
229e41f4b71Sopenharmony_ci```
230e41f4b71Sopenharmony_cibase/startup/init/services/modules/seccomp
231e41f4b71Sopenharmony_ci├── BUILD.gn
232e41f4b71Sopenharmony_ci├── gen_syscall_name_nrs.c
233e41f4b71Sopenharmony_ci├── scripts
234e41f4b71Sopenharmony_ci│   ├── generate_code_from_policy.py        # Policy file parsing script
235e41f4b71Sopenharmony_ci│   ├── seccomp_policy_fixer.gni            # Template definition in the BUILD.gn file for generating the dynamic policy library
236e41f4b71Sopenharmony_ci│   └── tools                               # Scripts for collecting system call statistics
237e41f4b71Sopenharmony_ci├── seccomp_policy                          # Basic policy files
238e41f4b71Sopenharmony_ci│   ├── app.blocklist.seccomp.policy
239e41f4b71Sopenharmony_ci│   ├── app.seccomp.policy
240e41f4b71Sopenharmony_ci│   ├── privileged_process.seccomp.policy
241e41f4b71Sopenharmony_ci│   ├── renderer.seccomp.policy
242e41f4b71Sopenharmony_ci│   ├── spawn.seccomp.policy
243e41f4b71Sopenharmony_ci│   ├── system.blocklist.seccomp.policy
244e41f4b71Sopenharmony_ci│   └── system.seccomp.policy
245e41f4b71Sopenharmony_ci├── seccomp_policy.c                        # Core Seccomp implementation code
246e41f4b71Sopenharmony_ci└── seccomp_policy_static.c                 # Seccomp plug-in code
247e41f4b71Sopenharmony_ci```
248e41f4b71Sopenharmony_ci
249e41f4b71Sopenharmony_ci### Policy File Overview
250e41f4b71Sopenharmony_ci
251e41f4b71Sopenharmony_ci- Location<br>Basic policy files are stored in **//base/startup/init/services/modules/seccomp/seccomp_policy**.
252e41f4b71Sopenharmony_ci- Basic policy files
253e41f4b71Sopenharmony_ci
254e41f4b71Sopenharmony_ci    **Table 3** Description of policy files
255e41f4b71Sopenharmony_ci    |  Policy File |  Description |
256e41f4b71Sopenharmony_ci    |  ---  |  ---  |
257e41f4b71Sopenharmony_ci    |  system.seccomp.policy  | Seccomp policy enabled for most of system service processes.|
258e41f4b71Sopenharmony_ci    |  system.blocklist.seccomp.policy  | System call baseline blocklist for system processes, that is, the list of system calls that cannot be invoked by non-privileged processes.|
259e41f4b71Sopenharmony_ci    |  app.seccomp.policy  | Seccomp policy enabled for all application processes.|
260e41f4b71Sopenharmony_ci    |  app.blocklist.seccomp.policy  | System call baseline blocklist for application processes, that is, the list of system calls that cannot be invoked by application processes.|
261e41f4b71Sopenharmony_ci    |  spawn.seccomp.policy  | Seccomp policy enabled for the appspawn and nwebspawn processes.|
262e41f4b71Sopenharmony_ci    |  renderer.seccomp.policy  | Seccomp policy enabled for the rendering processes incubated by the nwebspawn process.|
263e41f4b71Sopenharmony_ci    |  privileged_process.seccomp.policy  | Privileged process policy file. If certain processes need to use the system calls on the baseline blocklist, you need to declare the corresponding process identifiers and baseline blocklists in this file.|
264e41f4b71Sopenharmony_ci
265e41f4b71Sopenharmony_ci### How to Write a Common Policy File
266e41f4b71Sopenharmony_ci
267e41f4b71Sopenharmony_ci- To declare a configuration item, write **@** followed by the configuration item, for example, **@returnValue**.
268e41f4b71Sopenharmony_ci- Add the content of a configuration item from the next line of this configuration item to the beginning of the next configuration item.
269e41f4b71Sopenharmony_ci- To comment out a line, add a pound sign (#) at the beginning of this line.
270e41f4b71Sopenharmony_ci- Set the system architecture to **arm** or **arm64**. Only these two system architectures are supported currently.
271e41f4b71Sopenharmony_ci- Separate system calls from the system architecture by a semicolon (;). The value **all** indicates that the system calls will be used by all supported system architectures.
272e41f4b71Sopenharmony_ci- Set other parameters as needed. Except **returnValue**, all the other parameters are optional.
273e41f4b71Sopenharmony_ci
274e41f4b71Sopenharmony_ci**Table 4** Configuration items in the policy file
275e41f4b71Sopenharmony_ci
276e41f4b71Sopenharmony_ci|  Item |  Description | Configuration Rule |
277e41f4b71Sopenharmony_ci|  ---  |  ---  |  --  |
278e41f4b71Sopenharmony_ci|  returnValue  |  Return value. |  This parameter is mandatory. Value range:<br>- **LOG**: tolerant mode, in which only audit logs are recorded and the process is not terminated.<br>- **TRAP**: a mode in which the process is terminated and can be passed on to the faultloggerd process.<br>- **KILL_PROCESS**: a mode in which the process is terminated.<br>- **KILL_THREAD**: a mode in which the thread is terminated. |
279e41f4b71Sopenharmony_ci|  headFiles  |  Header file, which is used to declare the macros in the **allowListWithArgs** and **priorityWithArgs** parameters.|  Use **""** and **<>** to include the file name, for example, **<abc.h>** and **"cde.h"**. The default header files are **<linux/filter.h>**, **<stddef.h>**, **<linux/seccomp.h>**, and **<audit.h>**. |
280e41f4b71Sopenharmony_ci|  priority  |  Allowlist of frequently used system calls. |  System calls on the list are treated with a higher priority to improve the performance. |
281e41f4b71Sopenharmony_ci|  priorityWithArgs  |  Allowlist of frequently used system calls with arguments. |  System calls on the list are treated with a higher priority to improve the performance. |
282e41f4b71Sopenharmony_ci|  allowList  |  Allowlist |  List of system calls that can be invoked by a process. |
283e41f4b71Sopenharmony_ci|  allowListWithArgs  |  List of system calls with arguments that can be invoked by a process. |  Separate the system call name and argument by a colon (:). Supported relational operators include **<**, **<=**, **>**, **>=**, **==**, **!=**, and **&**, and supported logical operators include **&&** and \.|\|.<br>Use **arg***n* to specify the SN of the argument in the system. The value of **n** ranges from **0** to **5**. A judgment statement starts with **if** and ends with **else**. Declare the return value after the statement ends, and use a semicolon (;) to separate the judgment statement from the return value.<br>The return value must be in the format of **return xxx**, where the value range of **xxx** is the same as that of **returnValue**. If there are multiple conditions in the judgment statement, separate them with **elif**.|
284e41f4b71Sopenharmony_ci|  blockList  |  Blocklist of system calls. |  Before generating BPF instructions during policy file parsing, the system checks whether the system calls on the allowlist exist in the blocklist. If yes, a parsing error occurs.|
285e41f4b71Sopenharmony_ci|  selfDefineSyscall  |  Customized system call. |  Set the value of this parameter to a number. |
286e41f4b71Sopenharmony_ci
287e41f4b71Sopenharmony_ciExample: example.seccomp.policy
288e41f4b71Sopenharmony_ci
289e41f4b71Sopenharmony_ci```
290e41f4b71Sopenharmony_ci@returnValue
291e41f4b71Sopenharmony_ciTRAP
292e41f4b71Sopenharmony_ci
293e41f4b71Sopenharmony_ci@headFiles
294e41f4b71Sopenharmony_ci"time.h"
295e41f4b71Sopenharmony_ci
296e41f4b71Sopenharmony_ci@priority
297e41f4b71Sopenharmony_ciioctl;all
298e41f4b71Sopenharmony_ci
299e41f4b71Sopenharmony_ci@allowList
300e41f4b71Sopenharmony_ciopenat;all
301e41f4b71Sopenharmony_ciclose;all
302e41f4b71Sopenharmony_cilseek;all
303e41f4b71Sopenharmony_ciread;all
304e41f4b71Sopenharmony_ciwrite;all
305e41f4b71Sopenharmony_cisetresuid;arm64
306e41f4b71Sopenharmony_cisetresgid;arm64
307e41f4b71Sopenharmony_cisetresuid32;arm
308e41f4b71Sopenharmony_cisetresgid32;arm
309e41f4b71Sopenharmony_ci
310e41f4b71Sopenharmony_ci@allowListWithArgs
311e41f4b71Sopenharmony_ciclock_getres:if arg0 >= CLOCK_REALTIME && arg0 <= CLOCK_BOOTTIME; return ALLOW; else return TRAP;all
312e41f4b71Sopenharmony_ci
313e41f4b71Sopenharmony_ci@blockList
314e41f4b71Sopenharmony_ciswapon;all
315e41f4b71Sopenharmony_ci
316e41f4b71Sopenharmony_ci@selfDefineSyscall
317e41f4b71Sopenharmony_ci787
318e41f4b71Sopenharmony_ci```
319e41f4b71Sopenharmony_ci
320e41f4b71Sopenharmony_ci### How to Write a Privileged Process Policy File
321e41f4b71Sopenharmony_ci
322e41f4b71Sopenharmony_ci- To declare a configuration item, write **@** followed by the configuration item, for example, **@allowBlockList**.
323e41f4b71Sopenharmony_ci- Add the content of a configuration item from the next line of this configuration item to the beginning of the next configuration item.
324e41f4b71Sopenharmony_ci- To comment out a line, add a pound sign (#) at the beginning of this line.
325e41f4b71Sopenharmony_ci- Set the system architecture to **arm** or **arm64**. Only these two system architectures are supported currently.
326e41f4b71Sopenharmony_ci- Separate system calls from the system architecture by a semicolon (;). The value **all** indicates that the system calls will be used by all supported system architectures.
327e41f4b71Sopenharmony_ci
328e41f4b71Sopenharmony_ci**Table 5** Configuration items in the privileged process policy file
329e41f4b71Sopenharmony_ci|  Item |  Description | Configuration Rule |
330e41f4b71Sopenharmony_ci|  ---  |  ---  |  --  |
331e41f4b71Sopenharmony_ci|  privilegedProcessName  |  Process name identifier. |  Character string corresponding to **name** in the **services** parameter in the boot file of the native service process.|
332e41f4b71Sopenharmony_ci|  allowBlockList  |  Available baseline blocklist. |  Fill in the system call and the system architecture.|
333e41f4b71Sopenharmony_ci
334e41f4b71Sopenharmony_ciThe following example shows how to enable process1 and process2 to use the swapon system call in the baseline blocklist.
335e41f4b71Sopenharmony_ci```
336e41f4b71Sopenharmony_ci@privilegedProcessName
337e41f4b71Sopenharmony_ciprocess1
338e41f4b71Sopenharmony_ci
339e41f4b71Sopenharmony_ci@allowBlockList
340e41f4b71Sopenharmony_ciswapon;all
341e41f4b71Sopenharmony_ci
342e41f4b71Sopenharmony_ci@privilegedProcessName
343e41f4b71Sopenharmony_ciprocess2
344e41f4b71Sopenharmony_ci
345e41f4b71Sopenharmony_ci@allowBlockList
346e41f4b71Sopenharmony_ciswapon;all
347e41f4b71Sopenharmony_ci```
348e41f4b71Sopenharmony_ci
349e41f4b71Sopenharmony_ci### System Call Statistic Methods
350e41f4b71Sopenharmony_ci
351e41f4b71Sopenharmony_ci**Table 6** Comparison of statistic methods
352e41f4b71Sopenharmony_ci|  Statistic Method |  Description |  Advantage |  Disadvantage |
353e41f4b71Sopenharmony_ci|  ---  |  ---  |  ---  |  ---  |
354e41f4b71Sopenharmony_ci|  <div style="width: 50pt">Static analysis | <div style="width: 300pt">Analyze the ELF disassembly code to obtain the call relationship, collect statistics on the APIs that call the libc library, and then parse the LibC library to obtain the call relationship between the LibC APIs and the system call numbers. In this way, you will obtain the system call numbers used by the ELF file.|  <div style="width: 100pt">Statistics collection is supported for system calls in abnormal branches. |  <div style="width: 100pt">Parsing of call relationship is not supported for pointer functions. |
355e41f4b71Sopenharmony_ci|  Strace statistics | Use Strace to trace service processes when the device is running. During the trace, the invoked system calls are recorded into logs. Collect the logs after the trace is complete, and use a script to parse the logs and generate a Seccomp policy file.|  Easy to use. |  System calls can be completely collected only when all code branches are traversed. |
356e41f4b71Sopenharmony_ci|  Audit statistics | After the Seccomp policy is enabled for a process, Seccomp intercepts invalid system calls and records audit log information containing the system call numbers into kernel logs. Collect the logs after the trace is complete, and use a script to parse the logs and generate a Seccomp policy file.|  This method can be used as a supplement to the preceding methods. |  Logs may be lost.<br>System calls can be completely collected only when all code branches are traversed. |
357e41f4b71Sopenharmony_ci
358e41f4b71Sopenharmony_ci#### Static Analysis
359e41f4b71Sopenharmony_ci
360e41f4b71Sopenharmony_ci1. Prepare the environment.
361e41f4b71Sopenharmony_ci    1. Prepare a Linux environment.
362e41f4b71Sopenharmony_ci    2. Download the cross compilers arm-linux-musleabi and aarch64-linux-musl.
363e41f4b71Sopenharmony_ci        ```shell
364e41f4b71Sopenharmony_ci        wget https://musl.cc/arm-linux-musleabi-cross.tgz
365e41f4b71Sopenharmony_ci        wget https://musl.cc/aarch64-linux-musl-cross.tgz
366e41f4b71Sopenharmony_ci
367e41f4b71Sopenharmony_ci        tar -zxvf arm-linux-musleabi-cross.tgz
368e41f4b71Sopenharmony_ci        tar -zxvf aarch64-linux-musl-cross.tgz
369e41f4b71Sopenharmony_ci
370e41f4b71Sopenharmony_ci        # Add the tool execution path to the environment variable.
371e41f4b71Sopenharmony_ci        export PATH=$PATH:/path/to/arm-linux-musleabi-cross/bin
372e41f4b71Sopenharmony_ci        export PATH=$PATH:/path/to/aarch64-linux-musl-cross/bin
373e41f4b71Sopenharmony_ci        ```
374e41f4b71Sopenharmony_ci
375e41f4b71Sopenharmony_ci    3. Download the OpenHarmony source code. For details, see [Obtaining Source Code](../get-code/sourcecode-acquire.md).
376e41f4b71Sopenharmony_ci
377e41f4b71Sopenharmony_ci2. Compile **seccomp_filter** to obtain the dependency files **libsyscall_to_nr_arm** and **libsyscall_to_nr_arm64**.
378e41f4b71Sopenharmony_ci
379e41f4b71Sopenharmony_ci    **seccomp_filter** is declared in **base/startup/init/services/modules/seccomp/BUILD.gn** and is used to build the basic dynamic policy library of Seccomp. After the compilation is complete, dependency files are generated in **//out/Product name /gen/base/startup/init/services/modules/seccomp/gen_system_filter/**.
380e41f4b71Sopenharmony_ci    ```shell
381e41f4b71Sopenharmony_ci    ./build.sh --product-name *product name* --ccache --build-target seccomp_filter --target-cpu *specified CPU*
382e41f4b71Sopenharmony_ci
383e41f4b71Sopenharmony_ci    # Copy the dependency files to the tool folder for later use.
384e41f4b71Sopenharmony_ci    cp out/*product name* /gen/base/startup/init/services/modules/seccomp/gen_system_filter/libsyscall_to_nr_arm* base/startup/init/services/modules/seccomp/scripts/tools/
385e41f4b71Sopenharmony_ci    ```
386e41f4b71Sopenharmony_ci
387e41f4b71Sopenharmony_ci3. Copy the **generate_code_from_policy.py** script file to the tool folder. This script file is available at **//base/startup/init/services/modules/seccomp/scripts/**.
388e41f4b71Sopenharmony_ci    ```shell
389e41f4b71Sopenharmony_ci    # Go to the root directory of the OpenHarmony source code.
390e41f4b71Sopenharmony_ci    cd /root/to/OpenHarmonyCode;
391e41f4b71Sopenharmony_ci    # Go to the directory where the **generate_code_from_policy.py** script file is located.
392e41f4b71Sopenharmony_ci    cd base/startup/init/services/modules/seccomp/scripts/;
393e41f4b71Sopenharmony_ci    # Copy the **generate_code_from_policy.py** script file.
394e41f4b71Sopenharmony_ci    cp generate_code_from_policy.py tools/;
395e41f4b71Sopenharmony_ci    ```
396e41f4b71Sopenharmony_ci
397e41f4b71Sopenharmony_ci4. Compile ELF files related to the service code. In the 32-bit architecture, the implementation of disassembly code redirection for ELF files is complex. Therefore, ELF files are all compiled into 64-bit ELF files to parse the function call relationship.
398e41f4b71Sopenharmony_ci    ```shell
399e41f4b71Sopenharmony_ci    ./build.sh --product-name *product file* --ccache --target-cpu arm64 --build-target *target file*
400e41f4b71Sopenharmony_ci    ```
401e41f4b71Sopenharmony_ci
402e41f4b71Sopenharmony_ci5. If full build has not been performed before and the dependent dynamic libraries for step 4 are not in the **//out** directory, copy the related dynamic libraries to the **//out** directory. The following code is for reference only. If other dynamic libraries are involved, copy them in a similar way.
403e41f4b71Sopenharmony_ci    ```shell
404e41f4b71Sopenharmony_ci    # Go to the root directory of the source code.
405e41f4b71Sopenharmony_ci    cd /root/to/OpenHarmonyCode
406e41f4b71Sopenharmony_ci    # Create the **aarch64-linux-ohos** folder in **out/*product name*/lib.unstripped/** to store the dependent dynamic libraries.
407e41f4b71Sopenharmony_ci    mkdir out/*product name*/lib.unstripped/aarch64-linux-ohos
408e41f4b71Sopenharmony_ci    # Copy the related dynamic libraries to the //out directory.
409e41f4b71Sopenharmony_ci    cp prebuilts/clang/ohos/${host_platform_dir}/llvm/lib/clang/${clang_version}/lib/aarch64-linux-ohos/*.so out/*product name*/lib.unstripped/aarch64-linux-ohos/
410e41f4b71Sopenharmony_ci    cp prebuilts/clang/ohos/${host_platform_dir}/${clang_version}/llvm/lib/aarch64-linux-ohos/*.so out/*product name*/lib.unstripped/aarch64-linux-ohos/
411e41f4b71Sopenharmony_ci    ```
412e41f4b71Sopenharmony_ci
413e41f4b71Sopenharmony_ci6. Modify the **collect_elf_syscall.py** script file, and change the paths of the objdump and readelf tools to their absolute paths in the Linux environment. This script file is available at **base/startup/init/services/modules/seccomp/scripts/tools/**. The **objdump** and **readelf** tools available at **//prebuilts**.
414e41f4b71Sopenharmony_ci    ```python
415e41f4b71Sopenharmony_ci    #modified the path of objdump and readelf path
416e41f4b71Sopenharmony_ci    def get_obj_dump_path():
417e41f4b71Sopenharmony_ci        obj_dump_path = '/path/to/llvm-objdump'
418e41f4b71Sopenharmony_ci        return obj_dump_path
419e41f4b71Sopenharmony_ci
420e41f4b71Sopenharmony_ci
421e41f4b71Sopenharmony_ci    def get_read_elf_path():
422e41f4b71Sopenharmony_ci        read_elf_path = '/path/to/llvm-readelf'
423e41f4b71Sopenharmony_ci        return read_elf_path
424e41f4b71Sopenharmony_ci    ```
425e41f4b71Sopenharmony_ci
426e41f4b71Sopenharmony_ci7. Use the **collect_elf_syscall.py** script file to parse and generate the corresponding policy file **xxx.seccomp.policy**.
427e41f4b71Sopenharmony_ci
428e41f4b71Sopenharmony_ci    **Table 7** Parameters in the collect_elf_syscall.py script file
429e41f4b71Sopenharmony_ci    |  Parameter |  Description |
430e41f4b71Sopenharmony_ci    |  ---  |  ---  |
431e41f4b71Sopenharmony_ci    |  --src-elf-path  | Folder where the ELF file is located, for example, **~/ohcode/out/rk3568**. Do not end the value with a slash (/).|
432e41f4b71Sopenharmony_ci    |  --elf-name| ELF file name, for example, **libmedia_service.z.so**.|
433e41f4b71Sopenharmony_ci    |  --src-syscall-path  |  **libsyscall_to_nr_arm** or **libsyscall_to_nr_arm64**, which corresponds to the architecture specified by **--target-cpu**. |
434e41f4b71Sopenharmony_ci    |  --target-cpu  |  CPU architecture, that is, the architecture for which system calls are collected. This parameter determines the architecture for libC file parsing. Its value can be **arm** or **arm64**. |
435e41f4b71Sopenharmony_ci    |  --filter-name  | Name of the generated policy file. For example, if the input value is **test**, the generated file name is **test.seccomp.policy**. |
436e41f4b71Sopenharmony_ci    
437e41f4b71Sopenharmony_ci    Use **collect_elf_syscall.py** to parse ELF files.
438e41f4b71Sopenharmony_ci
439e41f4b71Sopenharmony_ci        ```
440e41f4b71Sopenharmony_ci        # The following example considers **rk3568** as the product and **libmedia_service.z.so** as the ELF file.
441e41f4b71Sopenharmony_ci        python3 collect_elf_syscall.py --src-elf-path ~/ohcode/out/rk3568 --elf-name libmedia_service.z.so --src-syscall-path libsyscall_to_nr_arm64 --target-cpu arm64 --filter-name media_service
442e41f4b71Sopenharmony_ci        ```
443e41f4b71Sopenharmony_ci
444e41f4b71Sopenharmony_ci        Example result of xxx.seccomp.policy 
445e41f4b71Sopenharmony_ci        ```
446e41f4b71Sopenharmony_ci        @allowList
447e41f4b71Sopenharmony_ci        getcwd;arm64
448e41f4b71Sopenharmony_ci        eventfd2;arm64
449e41f4b71Sopenharmony_ci        epoll_create1;arm64
450e41f4b71Sopenharmony_ci        epoll_ctl;arm64
451e41f4b71Sopenharmony_ci        dup;arm64
452e41f4b71Sopenharmony_ci        dup3;arm64
453e41f4b71Sopenharmony_ci        fcntl;arm64
454e41f4b71Sopenharmony_ci        ioctl;arm64
455e41f4b71Sopenharmony_ci        ...
456e41f4b71Sopenharmony_ci        ```
457e41f4b71Sopenharmony_ci
458e41f4b71Sopenharmony_ci#### Strace Statistics
459e41f4b71Sopenharmony_ci
460e41f4b71Sopenharmony_ci1. Use the cross compilers arm-linux-musleabi and aarch64-linux-musl to build the Strace tool for the 32-bit and 64-bit architectures, respectively.
461e41f4b71Sopenharmony_ci2. [Trace the service process](#tracing-the-service-process) to obtain the Strace logs.
462e41f4b71Sopenharmony_ci3. [Parse Strace logs](#parsing-strace-logs) by using scripts to obtain the Seccomp policy file.
463e41f4b71Sopenharmony_ci
464e41f4b71Sopenharmony_ci##### Tracing the Service Process
465e41f4b71Sopenharmony_ci
466e41f4b71Sopenharmony_ci1. Modify the embedded code in the init repository. Specifically, add the following content to **//base/startup/init/services/init/init_common_service.c** before executing the **SetSystemseccompPolicy** function to set the Seccomp policy.  If the line starts with a plus sign (+), the line is added; if the line starts with a hyphen (-), the line is deleted. **xxxx** must be the same as the value of **Services name** in the [boot configuration file](subsys-boot-init-cfg.md) of the process.
467e41f4b71Sopenharmony_ci    ```c
468e41f4b71Sopenharmony_ci    --- a/services/init/init_common_service.c
469e41f4b71Sopenharmony_ci    +++ b/services/init/init_common_service.c
470e41f4b71Sopenharmony_ci    @@ -155,7 +155,19 @@ static int SetPerms(const Service *service)
471e41f4b71Sopenharmony_ci        // set seccomp policy before setuid
472e41f4b71Sopenharmony_ci        INIT_ERROR_CHECK(SetSystemseccompPolicy(service) == SERVICE_SUCCESS, return SERVICE_FAILURE,
473e41f4b71Sopenharmony_ci            "set seccomp policy failed for service %s", service->name);
474e41f4b71Sopenharmony_ci    -
475e41f4b71Sopenharmony_ci    +    if (strncmp(service->name, "xxxx", strlen("xxxx")) == 0) {
476e41f4b71Sopenharmony_ci    +        pid_t pid = getpid();
477e41f4b71Sopenharmony_ci    +        pid_t pid_child = fork();
478e41f4b71Sopenharmony_ci    +        if (pid_child == 0) {
479e41f4b71Sopenharmony_ci    +            char pidStr[9] = {0};
480e41f4b71Sopenharmony_ci    +            sprintf_s(pidStr, 6, "%d", pid);
481e41f4b71Sopenharmony_ci    +            if (execl("/system/bin/strace", "/system/bin/strace", "-p", (const char *)pidStr, "-ff", "-o", "/data/strace/xxxx.strace.log", NULL) !=0 ) {
482e41f4b71Sopenharmony_ci    +                INIT_LOGE("strace failed");
483e41f4b71Sopenharmony_ci    +            }
484e41f4b71Sopenharmony_ci    +        }
485e41f4b71Sopenharmony_ci    +        sleep(5);
486e41f4b71Sopenharmony_ci    +    }
487e41f4b71Sopenharmony_ci        if (service->servPerm.uID != 0) {
488e41f4b71Sopenharmony_ci            if (setuid(service->servPerm.uID) != 0) {
489e41f4b71Sopenharmony_ci                INIT_LOGE("setuid of service: %s failed, uid = %d", service->name, service->servPerm.uID);
490e41f4b71Sopenharmony_ci    ```
491e41f4b71Sopenharmony_ci2. Perform a full build, and burn the image.
492e41f4b71Sopenharmony_ci3. Disable SElinux, and push Strace to the device.
493e41f4b71Sopenharmony_ci    ```shell
494e41f4b71Sopenharmony_ci    hdc shell setenforce 0
495e41f4b71Sopenharmony_ci    hdc shell mount -rw -o remount /
496e41f4b71Sopenharmony_ci    hdc file send /path/to/strace /system/bin/
497e41f4b71Sopenharmony_ci    hdc shell chmod a+x /system/bin/strace
498e41f4b71Sopenharmony_ci    ```
499e41f4b71Sopenharmony_ci4. Create a folder for storing Strace logs.
500e41f4b71Sopenharmony_ci    ```shell
501e41f4b71Sopenharmony_ci    hdc shell mkdir -p /data/strace
502e41f4b71Sopenharmony_ci    ```
503e41f4b71Sopenharmony_ci5. Terminate the service process, and restart it. In the following command, **xxx** indicates the service process name.
504e41f4b71Sopenharmony_ci    ```shell
505e41f4b71Sopenharmony_ci    kill -9 $(pidof xxx)
506e41f4b71Sopenharmony_ci    ```
507e41f4b71Sopenharmony_ci6. Perform service operations on the device. Make sure that all code is covered.
508e41f4b71Sopenharmony_ci7. Obtain Strace logs from **/data/strace** on the device, and save them to the directory where the parsing script is located.
509e41f4b71Sopenharmony_ci    ```shell
510e41f4b71Sopenharmony_ci    hdc file recv /data/strace /path/to/base/startup/init/services/modules/seccomp/scripts/tools/
511e41f4b71Sopenharmony_ci    ```
512e41f4b71Sopenharmony_ci
513e41f4b71Sopenharmony_ci##### Parsing Strace Logs
514e41f4b71Sopenharmony_ci
515e41f4b71Sopenharmony_ci1. Copy the dependency files to the Strace log folder for later use. The dependency files are those generated in step 2 in [Static Analysis](#static-analysis).
516e41f4b71Sopenharmony_ci    ```shell
517e41f4b71Sopenharmony_ci    cp out/*product name* /gen/base/startup/init/services/modules/seccomp/gen_system_filter/libsyscall_to_nr_arm* base/startup/init/services/modules/seccomp/scripts/tools/strace/
518e41f4b71Sopenharmony_ci    ```
519e41f4b71Sopenharmony_ci 
520e41f4b71Sopenharmony_ci2. Use the **strace_log_analysis.py** script file to parse and generate the corresponding policy file **xxx.seccomp.policy**.
521e41f4b71Sopenharmony_ci
522e41f4b71Sopenharmony_ci    The script file is available at **//base/startup/init/services/modules/seccomp/scripts/tools/**.
523e41f4b71Sopenharmony_ci    
524e41f4b71Sopenharmony_ci    **Table 8** Parameters in the strace_log_analysis.py script file
525e41f4b71Sopenharmony_ci    |  Parameter |  Description |
526e41f4b71Sopenharmony_ci    |  ---  |  ---  |
527e41f4b71Sopenharmony_ci    |  --src-path  | Folder for storing log files. It must contain **libsyscall_to_nr_arm** and **libsyscall_to_nr_arm64**. The folder name must not end with a slash (/), for example, **./strace**.|
528e41f4b71Sopenharmony_ci    |  --target-cpu  |  CPU architecture, which is the same as that of the traced process. Its value can be **arm** or **arm64**. |
529e41f4b71Sopenharmony_ci    |  --filter-name  | Name of the generated policy file. For example, if the input value is **test**, the generated file name is **test.seccomp.policy**. |
530e41f4b71Sopenharmony_ci
531e41f4b71Sopenharmony_ci    Use the **strace_log_analysis.py** script file to parse Strace logs.
532e41f4b71Sopenharmony_ci    ```shell
533e41f4b71Sopenharmony_ci    cd base/startup/init/services/modules/seccomp/scripts/tools;
534e41f4b71Sopenharmony_ci    python3 strace_log_analysis.py --src-path strace --target-cpu *specified CPU* --filter-name xxx
535e41f4b71Sopenharmony_ci    ```
536e41f4b71Sopenharmony_ci    Example result of xxx.seccomp.policy 
537e41f4b71Sopenharmony_ci    ```
538e41f4b71Sopenharmony_ci    @allowList
539e41f4b71Sopenharmony_ci    getcwd;arm64
540e41f4b71Sopenharmony_ci    eventfd2;arm64
541e41f4b71Sopenharmony_ci    epoll_create1;arm64
542e41f4b71Sopenharmony_ci    epoll_ctl;arm64
543e41f4b71Sopenharmony_ci    dup;arm64
544e41f4b71Sopenharmony_ci    dup3;arm64
545e41f4b71Sopenharmony_ci    fcntl;arm64
546e41f4b71Sopenharmony_ci    ioctl;arm64
547e41f4b71Sopenharmony_ci    ...
548e41f4b71Sopenharmony_ci    ```
549e41f4b71Sopenharmony_ci
550e41f4b71Sopenharmony_ci#### Audit Statistics
551e41f4b71Sopenharmony_ci
552e41f4b71Sopenharmony_ci1. Enable the initial Seccomp policy. For details, see [Customizing Seccomp Policies for a Process](#customizing-seccomp-policies-for-a-process).
553e41f4b71Sopenharmony_ci2. Obtain logs.
554e41f4b71Sopenharmony_ci    1. Create a folder for storing logs.
555e41f4b71Sopenharmony_ci        ```shell
556e41f4b71Sopenharmony_ci        mkdir -p /data/audit
557e41f4b71Sopenharmony_ci        ```
558e41f4b71Sopenharmony_ci    2. Obtain Seccomp-related audit log information from kernel logs. The logs end with **.audit.log**.
559e41f4b71Sopenharmony_ci        ```shell
560e41f4b71Sopenharmony_ci        cat /proc/kmsg | grep type=1326 > /data/audit/media_service.audit.log
561e41f4b71Sopenharmony_ci        ```
562e41f4b71Sopenharmony_ci3. Perform service-related operations and segment fault triggering operations.
563e41f4b71Sopenharmony_ci    1. To trigger a segment fault, add the following code to the service code to call **TriggerSegmentFault** at a certain point to rebuild and burn the image:
564e41f4b71Sopenharmony_ci        ```c
565e41f4b71Sopenharmony_ci        static void TriggerSegmentFault(void)
566e41f4b71Sopenharmony_ci        {
567e41f4b71Sopenharmony_ci            pid_t pid_child = fork();
568e41f4b71Sopenharmony_ci            if (pid_child == 0) {
569e41f4b71Sopenharmony_ci                char *test = (char *)0x1234;
570e41f4b71Sopenharmony_ci                *test = 1;
571e41f4b71Sopenharmony_ci            }
572e41f4b71Sopenharmony_ci        }
573e41f4b71Sopenharmony_ci        ```
574e41f4b71Sopenharmony_ci    2. After the device is started, run the following shell command to temporarily shut down SELinux and terminate the service process. The process then automatically restarts.
575e41f4b71Sopenharmony_ci        ```shell
576e41f4b71Sopenharmony_ci        setenforce 0
577e41f4b71Sopenharmony_ci        ```
578e41f4b71Sopenharmony_ci
579e41f4b71Sopenharmony_ci4. Run the hdc command to obtain audit logs from the **/data/audit** on of the device, and save them to the directory where the parsing script is located.
580e41f4b71Sopenharmony_ci    ```shell
581e41f4b71Sopenharmony_ci    hdc file recv /data/audit /path/to/base/startup/init/services/modules/seccomp/scripts/tools/
582e41f4b71Sopenharmony_ci    ```
583e41f4b71Sopenharmony_ci5. Parse the audit logs.
584e41f4b71Sopenharmony_ci    
585e41f4b71Sopenharmony_ci    Example audit log:
586e41f4b71Sopenharmony_ci    ```shell
587e41f4b71Sopenharmony_ci    <5>[  198.963101] audit: type=1326 audit(1659528178.748:27): auid=4294967295 uid=0 gid=1000 ses=4294967295 subj=u:r:appspawn:s0 pid=2704 comm="config_dialog_s" exe="/system/bin/appspawn" sig=31 arch=40000028 syscall=208 compat=1 ip=0xf7b79400 code=0x80000000
588e41f4b71Sopenharmony_ci    ```
589e41f4b71Sopenharmony_ci    **Table 9** Key parameters in audit logs
590e41f4b71Sopenharmony_ci    |  Parameter |  Description |
591e41f4b71Sopenharmony_ci    |  ---  |  ---  |
592e41f4b71Sopenharmony_ci    |  type  |  Type. The value **1326** indicates that the log is of the Seccomp type. |
593e41f4b71Sopenharmony_ci    |  sig  |  Semaphore. The value **31** indicates **SIGSYS**, which is the signal sent to the process when Seccomp interception occurs. |
594e41f4b71Sopenharmony_ci    |  arch  |  Architecture ID. The value **40000028** indicates **arm**, and the value **c00000b7** indicates **arm64**. |
595e41f4b71Sopenharmony_ci    |  syscall  |  System call ID. |
596e41f4b71Sopenharmony_ci    |  compat  |  The value **1** indicates the compatibility mode, that is, the arm64 kernel uses arm system calls.|
597e41f4b71Sopenharmony_ci
598e41f4b71Sopenharmony_ci
599e41f4b71Sopenharmony_ci    1. Copy the dependency files to the log folder for later use. The dependency files are those generated in step 2 in [Static Analysis](#static-analysis).
600e41f4b71Sopenharmony_ci        ```shell
601e41f4b71Sopenharmony_ci        cp out/*product name* /gen/base/startup/init/services/modules/seccomp/gen_system_filter/libsyscall_to_nr_arm* base/startup/init/services/modules/seccomp/scripts/tools/audit/
602e41f4b71Sopenharmony_ci        ```
603e41f4b71Sopenharmony_ci    2. Run the **audit_log_analysis.py** script to parse logs and generate **xxx.seccomp.policy**. The tool is available at **//base/startup/init/services/modules/seccomp/scripts/tools/**.
604e41f4b71Sopenharmony_ci
605e41f4b71Sopenharmony_ci        **Table 10** Parameters in the audit_log_analysis.py script file
606e41f4b71Sopenharmony_ci        |  Parameter |  Description |
607e41f4b71Sopenharmony_ci        |  ---  |  ---  |
608e41f4b71Sopenharmony_ci        |  --src-path  | Folder for storing log files. It must contain **libsyscall_to_nr_arm** and **libsyscall_to_nr_arm64**. The folder name must not end with a slash (/), for example, **./audit**.|
609e41f4b71Sopenharmony_ci        |  --filter-name  | Name of the generated policy file. For example, if the input value is **test**, the generated file name is **test.seccomp.policy**.|
610e41f4b71Sopenharmony_ci
611e41f4b71Sopenharmony_ci        ```shell
612e41f4b71Sopenharmony_ci        cd base/startup/init/services/modules/seccomp/scripts/tools
613e41f4b71Sopenharmony_ci        python3 audit_log_analysis.py --src-path audit --filter-name xxx
614e41f4b71Sopenharmony_ci        ```
615e41f4b71Sopenharmony_ci
616e41f4b71Sopenharmony_ci### Combining Multiple Policy Files
617e41f4b71Sopenharmony_ci
618e41f4b71Sopenharmony_ciDuring [colltatistics on system calls](#system-call-statistic-methods), multiple policy files may be generated. In these policy files, system calls may be repeated or disordered. To solve these problems, you can combine policy files to sort system calls by arm64/arm and by system call number in ascending order.
619e41f4b71Sopenharmony_ci
620e41f4b71Sopenharmony_ci**Table 11** Parameters in the merge_policy.py script file
621e41f4b71Sopenharmony_ci|  Parameter |  Description |
622e41f4b71Sopenharmony_ci|  ---  |  ---  |
623e41f4b71Sopenharmony_ci|  --src-files  | Files to be processed, including **libsyscall_to_nr_arm** and **libsyscall_to_nr_arm64**.|
624e41f4b71Sopenharmony_ci|  --filter-name  | Name of the generated policy file. For example, if the input value is **test**, the generated file name is **test.seccomp.policy**. |
625e41f4b71Sopenharmony_ci1. Copy the dependency files to the log folder for later use.
626e41f4b71Sopenharmony_ci    ```shell
627e41f4b71Sopenharmony_ci    cp out/*product name* /gen/base/startup/init/services/modules/seccomp/gen_system_filter/libsyscall_to_nr_arm* base/startup/init/services/modules/seccomp/scripts/tools/
628e41f4b71Sopenharmony_ci    ```
629e41f4b71Sopenharmony_ci2. Run the **merge_policy.py** script to merge **policy1.seccomp.policy** and **policy2.seccomp.policy** into **xxxx.seccomp.policy**.
630e41f4b71Sopenharmony_ci    ```shell
631e41f4b71Sopenharmony_ci    python3 merge_policy.py --src-files libsyscall_to_nr_arm --src-files libsyscall_to_nr_arm64 --src-files policy1.seccomp.policy --src-files policy2.seccomp.policy --filter-name xxxx
632e41f4b71Sopenharmony_ci    ```
633