16cd6a6acSopenharmony_ciContainer Statements
26cd6a6acSopenharmony_ci====================
36cd6a6acSopenharmony_ci
46cd6a6acSopenharmony_ciblock
56cd6a6acSopenharmony_ci-----
66cd6a6acSopenharmony_ci
76cd6a6acSopenharmony_ciStart a new namespace.
86cd6a6acSopenharmony_ci
96cd6a6acSopenharmony_ciNot allowed in [`macro`](cil_call_macro_statements.md#macro) and [`optional`](cil_container_statements.md#optional) blocks.
106cd6a6acSopenharmony_ci
116cd6a6acSopenharmony_ci[`sensitivity`](cil_mls_labeling_statements.md#sensitivity) and [`category`](cil_mls_labeling_statements.md#category) statements are not allowed in [`block`](cil_container_statements.md#block) blocks.
126cd6a6acSopenharmony_ci
136cd6a6acSopenharmony_ciDuplicate declarations of a [`block`](cil_container_statements.md#block) in the same namespace will normally cause an error, but inheriting a block into a namespace (with [`blockinherit`](cil_container_statements.md#blockinherit)) that already has a block with the same name will only result in a warning message and not cause an error. The policy from both blocks will end up in the binary policy. This behavior was used in the past to allow a block to be declared so that an [`in-statement`](cil_container_statements.md#in) could be used on it, but now an [`in-statement`](cil_container_statements.md#in) can be specified to occur after inheritance, so this behavior is not necessary (but is still allowed).
146cd6a6acSopenharmony_ci
156cd6a6acSopenharmony_ci**Statement definition:**
166cd6a6acSopenharmony_ci
176cd6a6acSopenharmony_ci```secil
186cd6a6acSopenharmony_ci    (block block_id
196cd6a6acSopenharmony_ci        cil_statement
206cd6a6acSopenharmony_ci        ...
216cd6a6acSopenharmony_ci    )
226cd6a6acSopenharmony_ci```
236cd6a6acSopenharmony_ci
246cd6a6acSopenharmony_ci**Where:**
256cd6a6acSopenharmony_ci
266cd6a6acSopenharmony_ci<table>
276cd6a6acSopenharmony_ci<colgroup>
286cd6a6acSopenharmony_ci<col width="25%" />
296cd6a6acSopenharmony_ci<col width="75%" />
306cd6a6acSopenharmony_ci</colgroup>
316cd6a6acSopenharmony_ci<tbody>
326cd6a6acSopenharmony_ci<tr class="odd">
336cd6a6acSopenharmony_ci<td align="left"><p><code>block</code></p></td>
346cd6a6acSopenharmony_ci<td align="left"><p>The <code>block</code> keyword.</p></td>
356cd6a6acSopenharmony_ci</tr>
366cd6a6acSopenharmony_ci<tr class="even">
376cd6a6acSopenharmony_ci<td align="left"><p><code>block_id</code></p></td>
386cd6a6acSopenharmony_ci<td align="left"><p>The namespace identifier.</p></td>
396cd6a6acSopenharmony_ci</tr>
406cd6a6acSopenharmony_ci<tr class="odd">
416cd6a6acSopenharmony_ci<td align="left"><p><code>cil_statement</code></p></td>
426cd6a6acSopenharmony_ci<td align="left"><p>Zero or more valid CIL statements.</p></td>
436cd6a6acSopenharmony_ci</tr>
446cd6a6acSopenharmony_ci</tbody>
456cd6a6acSopenharmony_ci</table>
466cd6a6acSopenharmony_ci
476cd6a6acSopenharmony_ci**Example:**
486cd6a6acSopenharmony_ci
496cd6a6acSopenharmony_ciSee the [`blockinherit`](cil_container_statements.md#blockinherit) statement for an example.
506cd6a6acSopenharmony_ci
516cd6a6acSopenharmony_ciblockabstract
526cd6a6acSopenharmony_ci-------------
536cd6a6acSopenharmony_ci
546cd6a6acSopenharmony_ciDeclares the namespace as a 'template' and does not generate code until instantiated by another namespace that has a [`blockinherit`](cil_container_statements.md#blockinherit) statement.
556cd6a6acSopenharmony_ci
566cd6a6acSopenharmony_ciNot allowed in [`macro`](cil_call_macro_statements.md#macro) and [`optional`](cil_container_statements.md#optional) blocks.
576cd6a6acSopenharmony_ci
586cd6a6acSopenharmony_ci**Statement definition:**
596cd6a6acSopenharmony_ci
606cd6a6acSopenharmony_ci```secil
616cd6a6acSopenharmony_ci    (block block_id
626cd6a6acSopenharmony_ci        (blockabstract template_id)
636cd6a6acSopenharmony_ci        cil_statement
646cd6a6acSopenharmony_ci        ...
656cd6a6acSopenharmony_ci    )
666cd6a6acSopenharmony_ci```
676cd6a6acSopenharmony_ci
686cd6a6acSopenharmony_ci**Where:**
696cd6a6acSopenharmony_ci
706cd6a6acSopenharmony_ci<table>
716cd6a6acSopenharmony_ci<colgroup>
726cd6a6acSopenharmony_ci<col width="25%" />
736cd6a6acSopenharmony_ci<col width="75%" />
746cd6a6acSopenharmony_ci</colgroup>
756cd6a6acSopenharmony_ci<tbody>
766cd6a6acSopenharmony_ci<tr class="odd">
776cd6a6acSopenharmony_ci<td align="left"><p><code>block</code></p></td>
786cd6a6acSopenharmony_ci<td align="left"><p>The <code>block</code> keyword.</p></td>
796cd6a6acSopenharmony_ci</tr>
806cd6a6acSopenharmony_ci<tr class="even">
816cd6a6acSopenharmony_ci<td align="left"><p><code>block_id</code></p></td>
826cd6a6acSopenharmony_ci<td align="left"><p>The namespace identifier.</p></td>
836cd6a6acSopenharmony_ci</tr>
846cd6a6acSopenharmony_ci<tr class="odd">
856cd6a6acSopenharmony_ci<td align="left"><p><code>blockabstract</code></p></td>
866cd6a6acSopenharmony_ci<td align="left"><p>The <code>blockabstract</code> keyword.</p></td>
876cd6a6acSopenharmony_ci</tr>
886cd6a6acSopenharmony_ci<tr class="even">
896cd6a6acSopenharmony_ci<td align="left"><p><code>template_id</code></p></td>
906cd6a6acSopenharmony_ci<td align="left"><p>The abstract namespace identifier. This must match the <code>block_id</code> entry.</p></td>
916cd6a6acSopenharmony_ci</tr>
926cd6a6acSopenharmony_ci<tr class="odd">
936cd6a6acSopenharmony_ci<td align="left"><p><code>cil_statement</code></p></td>
946cd6a6acSopenharmony_ci<td align="left"><p>Zero or more valid CIL statements forming the abstract block.</p></td>
956cd6a6acSopenharmony_ci</tr>
966cd6a6acSopenharmony_ci</tbody>
976cd6a6acSopenharmony_ci</table>
986cd6a6acSopenharmony_ci
996cd6a6acSopenharmony_ci**Example:**
1006cd6a6acSopenharmony_ci
1016cd6a6acSopenharmony_ciSee the [`blockinherit`](cil_container_statements.md#blockinherit) statement for an example.
1026cd6a6acSopenharmony_ci
1036cd6a6acSopenharmony_ciblockinherit
1046cd6a6acSopenharmony_ci------------
1056cd6a6acSopenharmony_ci
1066cd6a6acSopenharmony_ciUsed to add common policy rules to the current namespace via a template that has been defined with the [`blockabstract`](cil_container_statements.md#blockabstract) statement. All [`blockinherit`](cil_container_statements.md#blockinherit) statements are resolved first and then the contents of the block are copied. This is so that inherited blocks will not be inherited. For a concrete example, please see the examples section.
1076cd6a6acSopenharmony_ci
1086cd6a6acSopenharmony_ciInherited rules are resolved by searching namespaces in the following order:
1096cd6a6acSopenharmony_ci
1106cd6a6acSopenharmony_ci-  The parent namespaces (if any) where the [`blockinherit`](cil_container_statements.md#blockinherit) rule is located with the exception of the global namespace.
1116cd6a6acSopenharmony_ci
1126cd6a6acSopenharmony_ci-  The parent namespaces of the block being inherited (but not that block's namespace) with the exception of the global namespace.
1136cd6a6acSopenharmony_ci
1146cd6a6acSopenharmony_ci-  The global namespace.
1156cd6a6acSopenharmony_ci
1166cd6a6acSopenharmony_ciNot allowed in [`macro`](cil_call_macro_statements.md#macro) blocks.
1176cd6a6acSopenharmony_ci
1186cd6a6acSopenharmony_ci**Statement definition:**
1196cd6a6acSopenharmony_ci
1206cd6a6acSopenharmony_ci```secil
1216cd6a6acSopenharmony_ci    (block block_id
1226cd6a6acSopenharmony_ci        (blockinherit template_id)
1236cd6a6acSopenharmony_ci        cil_statement
1246cd6a6acSopenharmony_ci        ...
1256cd6a6acSopenharmony_ci    )
1266cd6a6acSopenharmony_ci```
1276cd6a6acSopenharmony_ci
1286cd6a6acSopenharmony_ci**Where:**
1296cd6a6acSopenharmony_ci
1306cd6a6acSopenharmony_ci<table>
1316cd6a6acSopenharmony_ci<colgroup>
1326cd6a6acSopenharmony_ci<col width="25%" />
1336cd6a6acSopenharmony_ci<col width="75%" />
1346cd6a6acSopenharmony_ci</colgroup>
1356cd6a6acSopenharmony_ci<tbody>
1366cd6a6acSopenharmony_ci<tr class="odd">
1376cd6a6acSopenharmony_ci<td align="left"><p><code>block</code></p></td>
1386cd6a6acSopenharmony_ci<td align="left"><p>The <code>block</code> keyword.</p></td>
1396cd6a6acSopenharmony_ci</tr>
1406cd6a6acSopenharmony_ci<tr class="even">
1416cd6a6acSopenharmony_ci<td align="left"><p><code>block_id</code></p></td>
1426cd6a6acSopenharmony_ci<td align="left"><p>The namespace identifier.</p></td>
1436cd6a6acSopenharmony_ci</tr>
1446cd6a6acSopenharmony_ci<tr class="odd">
1456cd6a6acSopenharmony_ci<td align="left"><p><code>blockinherit</code></p></td>
1466cd6a6acSopenharmony_ci<td align="left"><p>The <code>blockinherit</code> keyword.</p></td>
1476cd6a6acSopenharmony_ci</tr>
1486cd6a6acSopenharmony_ci<tr class="even">
1496cd6a6acSopenharmony_ci<td align="left"><p><code>template_id</code></p></td>
1506cd6a6acSopenharmony_ci<td align="left"><p>The inherited namespace identifier.</p></td>
1516cd6a6acSopenharmony_ci</tr>
1526cd6a6acSopenharmony_ci<tr class="odd">
1536cd6a6acSopenharmony_ci<td align="left"><p><code>cil_statement</code></p></td>
1546cd6a6acSopenharmony_ci<td align="left"><p>Zero or more valid CIL statements.</p></td>
1556cd6a6acSopenharmony_ci</tr>
1566cd6a6acSopenharmony_ci</tbody>
1576cd6a6acSopenharmony_ci</table>
1586cd6a6acSopenharmony_ci
1596cd6a6acSopenharmony_ci**Example:**
1606cd6a6acSopenharmony_ci
1616cd6a6acSopenharmony_ciThis example contains a template `client_server` that is instantiated in two blocks (`netserver_app` and `netclient_app`):
1626cd6a6acSopenharmony_ci
1636cd6a6acSopenharmony_ci```secil
1646cd6a6acSopenharmony_ci    ; This is the template block:
1656cd6a6acSopenharmony_ci    (block client_server
1666cd6a6acSopenharmony_ci        (blockabstract client_server)
1676cd6a6acSopenharmony_ci
1686cd6a6acSopenharmony_ci        ; Log file labeling
1696cd6a6acSopenharmony_ci        (type log_file)
1706cd6a6acSopenharmony_ci        (typeattributeset file_type (log_file))
1716cd6a6acSopenharmony_ci        (typeattributeset data_file_type (log_file))
1726cd6a6acSopenharmony_ci        (allow process log_file (dir (write search create setattr add_name)))
1736cd6a6acSopenharmony_ci        (allow process log_file (file (create open append getattr setattr)))
1746cd6a6acSopenharmony_ci        (roletype object_r log_file)
1756cd6a6acSopenharmony_ci        (context log_file_context (u object_r log_file low_low))
1766cd6a6acSopenharmony_ci
1776cd6a6acSopenharmony_ci        ; Process labeling
1786cd6a6acSopenharmony_ci        (type process)
1796cd6a6acSopenharmony_ci        (typeattributeset domain (process))
1806cd6a6acSopenharmony_ci        (call app_domain (process))
1816cd6a6acSopenharmony_ci        (call net_domain (process))
1826cd6a6acSopenharmony_ci    )
1836cd6a6acSopenharmony_ci
1846cd6a6acSopenharmony_ci    ; This is a policy block that will inherit the abstract block above:
1856cd6a6acSopenharmony_ci    (block netclient_app
1866cd6a6acSopenharmony_ci        ; Add common policy rules to namespace:
1876cd6a6acSopenharmony_ci        (blockinherit client_server)
1886cd6a6acSopenharmony_ci        ; Label the log files
1896cd6a6acSopenharmony_ci        (filecon "/data/data/com.se4android.netclient/.*" file log_file_context)
1906cd6a6acSopenharmony_ci    )
1916cd6a6acSopenharmony_ci
1926cd6a6acSopenharmony_ci    ; This is another policy block that will inherit the abstract block above:
1936cd6a6acSopenharmony_ci    (block netserver_app
1946cd6a6acSopenharmony_ci       ; Add common policy rules to namespace:
1956cd6a6acSopenharmony_ci        (blockinherit client_server)
1966cd6a6acSopenharmony_ci
1976cd6a6acSopenharmony_ci        ; Label the log files
1986cd6a6acSopenharmony_ci        (filecon "/data/data/com.se4android.netserver/.*" file log_file_context)
1996cd6a6acSopenharmony_ci    )
2006cd6a6acSopenharmony_ci
2016cd6a6acSopenharmony_ci    ; This is an example of how blockinherits resolve inherits before copying
2026cd6a6acSopenharmony_ci    (block a
2036cd6a6acSopenharmony_ci        (type one))
2046cd6a6acSopenharmony_ci
2056cd6a6acSopenharmony_ci    (block b
2066cd6a6acSopenharmony_ci        ; Notice that block a is declared here as well
2076cd6a6acSopenharmony_ci        (block a
2086cd6a6acSopenharmony_ci            (type two)))
2096cd6a6acSopenharmony_ci
2106cd6a6acSopenharmony_ci    ; This will first copy the contents of block b, which results in type b.a.two being copied.
2116cd6a6acSopenharmony_ci    ; Next, the contents of block a will be copied which will result in type a.one.
2126cd6a6acSopenharmony_ci    (block ab
2136cd6a6acSopenharmony_ci        (blockinherit b)
2146cd6a6acSopenharmony_ci        (blockinherit a))
2156cd6a6acSopenharmony_ci```
2166cd6a6acSopenharmony_ci
2176cd6a6acSopenharmony_cioptional
2186cd6a6acSopenharmony_ci--------
2196cd6a6acSopenharmony_ci
2206cd6a6acSopenharmony_ciDeclare an [`optional`](cil_container_statements.md#optional) namespace. All CIL statements in the optional block must be satisfied before instantiation in the binary policy.
2216cd6a6acSopenharmony_ci
2226cd6a6acSopenharmony_ciNot allowed in [`booleanif`](cil_conditional_statements.md#booleanif) blocks.
2236cd6a6acSopenharmony_ci
2246cd6a6acSopenharmony_ci[`tunable`](cil_conditional_statements.md#tunable), [`in`](cil_container_statements.md#in), [`block`](cil_container_statements.md#block), [`blockabstract`](cil_container_statements.md#blockabstract), and [`macro`](cil_call_macro_statements.md#macro) statements are not allowed in [`optional`](cil_container_statements.md#optional) blocks.
2256cd6a6acSopenharmony_ci
2266cd6a6acSopenharmony_ci**Statement definition:**
2276cd6a6acSopenharmony_ci
2286cd6a6acSopenharmony_ci```secil
2296cd6a6acSopenharmony_ci    (optional optional_id
2306cd6a6acSopenharmony_ci        cil_statement
2316cd6a6acSopenharmony_ci        ...
2326cd6a6acSopenharmony_ci    )
2336cd6a6acSopenharmony_ci```
2346cd6a6acSopenharmony_ci
2356cd6a6acSopenharmony_ci**Where:**
2366cd6a6acSopenharmony_ci
2376cd6a6acSopenharmony_ci<table>
2386cd6a6acSopenharmony_ci<colgroup>
2396cd6a6acSopenharmony_ci<col width="25%" />
2406cd6a6acSopenharmony_ci<col width="75%" />
2416cd6a6acSopenharmony_ci</colgroup>
2426cd6a6acSopenharmony_ci<tbody>
2436cd6a6acSopenharmony_ci<tr class="odd">
2446cd6a6acSopenharmony_ci<td align="left"><p><code>optional</code></p></td>
2456cd6a6acSopenharmony_ci<td align="left"><p>The <code>optional</code> keyword.</p></td>
2466cd6a6acSopenharmony_ci</tr>
2476cd6a6acSopenharmony_ci<tr class="even">
2486cd6a6acSopenharmony_ci<td align="left"><p><code>optional_id</code></p></td>
2496cd6a6acSopenharmony_ci<td align="left"><p>The <code>optional</code> namespace identifier.</p></td>
2506cd6a6acSopenharmony_ci</tr>
2516cd6a6acSopenharmony_ci<tr class="odd">
2526cd6a6acSopenharmony_ci<td align="left"><p><code>cil_statement</code></p></td>
2536cd6a6acSopenharmony_ci<td align="left"><p>Zero or more valid CIL statements.</p></td>
2546cd6a6acSopenharmony_ci</tr>
2556cd6a6acSopenharmony_ci</tbody>
2566cd6a6acSopenharmony_ci</table>
2576cd6a6acSopenharmony_ci
2586cd6a6acSopenharmony_ci**Example:**
2596cd6a6acSopenharmony_ci
2606cd6a6acSopenharmony_ciThis example will instantiate the optional block `ext_gateway.move_file` into policy providing all optional CIL statements can be resolved:
2616cd6a6acSopenharmony_ci
2626cd6a6acSopenharmony_ci```secil
2636cd6a6acSopenharmony_ci    (block ext_gateway
2646cd6a6acSopenharmony_ci        ......
2656cd6a6acSopenharmony_ci        (optional move_file
2666cd6a6acSopenharmony_ci            (typetransition process msg_filter.move_file.in_queue file msg_filter.move_file.in_file)
2676cd6a6acSopenharmony_ci            (allow process msg_filter.move_file.in_queue (dir (read getattr write search add_name)))
2686cd6a6acSopenharmony_ci            (allow process msg_filter.move_file.in_file (file (write create getattr)))
2696cd6a6acSopenharmony_ci            (allow msg_filter.move_file.in_file unconfined.object (filesystem (associate)))
2706cd6a6acSopenharmony_ci            (typetransition msg_filter.int_gateway.process msg_filter.move_file.out_queue file
2716cd6a6acSopenharmony_ci                msg_filter.move_file.out_file)
2726cd6a6acSopenharmony_ci            (allow msg_filter.int_gateway.process msg_filter.move_file.out_queue (dir (read write search)))
2736cd6a6acSopenharmony_ci            (allow msg_filter.int_gateway.process msg_filter.move_file.out_file (file (read getattr unlink)))
2746cd6a6acSopenharmony_ci        ) ; End optional block
2756cd6a6acSopenharmony_ci
2766cd6a6acSopenharmony_ci        .....
2776cd6a6acSopenharmony_ci    ) ; End block
2786cd6a6acSopenharmony_ci```
2796cd6a6acSopenharmony_ci
2806cd6a6acSopenharmony_ciin
2816cd6a6acSopenharmony_ci--
2826cd6a6acSopenharmony_ci
2836cd6a6acSopenharmony_ciAllows the insertion of CIL statements into a named container ([`block`](cil_container_statements.md#block), [`optional`](cil_container_statements.md#optional) or [`macro`](cil_call_macro_statements.md#macro)). This insertion can be specified to occur either before or after block inheritance has been resolved.
2846cd6a6acSopenharmony_ci
2856cd6a6acSopenharmony_ciNot allowed in [`macro`](cil_call_macro_statements.md#macro), [`booleanif`](cil_conditional_statements.md#booleanif), and other [`in`](cil_container_statements.md#in) blocks.
2866cd6a6acSopenharmony_ci
2876cd6a6acSopenharmony_ci[`tunable`](cil_conditional_statements.md#tunable) and [`in`](cil_container_statements.md#in) statements are not allowed in [`in`](cil_container_statements.md#in) blocks.
2886cd6a6acSopenharmony_ci
2896cd6a6acSopenharmony_ci**Statement definition:**
2906cd6a6acSopenharmony_ci
2916cd6a6acSopenharmony_ci```secil
2926cd6a6acSopenharmony_ci    (in [before|after] container_id
2936cd6a6acSopenharmony_ci        cil_statement
2946cd6a6acSopenharmony_ci        ...
2956cd6a6acSopenharmony_ci    )
2966cd6a6acSopenharmony_ci```
2976cd6a6acSopenharmony_ci
2986cd6a6acSopenharmony_ci**Where:**
2996cd6a6acSopenharmony_ci
3006cd6a6acSopenharmony_ci<table>
3016cd6a6acSopenharmony_ci<colgroup>
3026cd6a6acSopenharmony_ci<col width="25%" />
3036cd6a6acSopenharmony_ci<col width="75%" />
3046cd6a6acSopenharmony_ci</colgroup>
3056cd6a6acSopenharmony_ci<tbody>
3066cd6a6acSopenharmony_ci<tr class="odd">
3076cd6a6acSopenharmony_ci<td align="left"><p><code>in</code></p></td>
3086cd6a6acSopenharmony_ci<td align="left"><p>The <code>in</code> keyword.</p></td>
3096cd6a6acSopenharmony_ci</tr>
3106cd6a6acSopenharmony_ci<tr class="even">
3116cd6a6acSopenharmony_ci<td align="left"><p><code>before|after</code></p></td>
3126cd6a6acSopenharmony_ci<td align="left"><p>An optional value that specifies whether to process the [`in`](cil_container_statements.md#in) <code>before</code> or <code>after</code> block inheritance. If no value is specified, then the [`in`](cil_container_statements.md#in) will be processed before block inheritance.</p></td>
3136cd6a6acSopenharmony_ci</tr>
3146cd6a6acSopenharmony_ci<tr class="odd">
3156cd6a6acSopenharmony_ci<td align="left"><p><code>container_id</code></p></td>
3166cd6a6acSopenharmony_ci<td align="left"><p>A valid <code>block</code>, <code>optional</code> or <code>macro</code> namespace identifier.</p></td>
3176cd6a6acSopenharmony_ci</tr>
3186cd6a6acSopenharmony_ci<tr class="even">
3196cd6a6acSopenharmony_ci<td align="left"><p><code>cil_statement</code></p></td>
3206cd6a6acSopenharmony_ci<td align="left"><p>Zero or more valid CIL statements.</p></td>
3216cd6a6acSopenharmony_ci</tr>
3226cd6a6acSopenharmony_ci</tbody>
3236cd6a6acSopenharmony_ci</table>
3246cd6a6acSopenharmony_ci
3256cd6a6acSopenharmony_ci**Example:**
3266cd6a6acSopenharmony_ci
3276cd6a6acSopenharmony_ciThis will add rules to the container named `system_server`:
3286cd6a6acSopenharmony_ci
3296cd6a6acSopenharmony_ci```secil
3306cd6a6acSopenharmony_ci    (in system_server
3316cd6a6acSopenharmony_ci        (dontaudit process secmark_demo.dns_packet (packet (send recv)))
3326cd6a6acSopenharmony_ci        (allow process secmark_demo.dns_packet (packet (send recv)))
3336cd6a6acSopenharmony_ci    )
3346cd6a6acSopenharmony_ci```
335