xref: /aosp_15_r20/external/selinux/secilc/docs/cil_context_statement.md (revision 2d543d20722ada2425b5bdab9d0d1d29470e7bba)
1*2d543d20SAndroid Build Coastguard WorkerContext Statement
2*2d543d20SAndroid Build Coastguard Worker=================
3*2d543d20SAndroid Build Coastguard Worker
4*2d543d20SAndroid Build Coastguard WorkerContexts are formed using previously declared parameters and may be named or anonymous where:
5*2d543d20SAndroid Build Coastguard Worker
6*2d543d20SAndroid Build Coastguard Worker-   Named - The context is declared with a context identifier that is used as a reference.
7*2d543d20SAndroid Build Coastguard Worker
8*2d543d20SAndroid Build Coastguard Worker-   Anonymous - They are defined within the CIL labeling statement using user, role etc. identifiers.
9*2d543d20SAndroid Build Coastguard Worker
10*2d543d20SAndroid Build Coastguard WorkerEach type is shown in the examples.
11*2d543d20SAndroid Build Coastguard Worker
12*2d543d20SAndroid Build Coastguard Workercontext
13*2d543d20SAndroid Build Coastguard Worker-------
14*2d543d20SAndroid Build Coastguard Worker
15*2d543d20SAndroid Build Coastguard WorkerDeclare an SELinux security context identifier for labeling. The range (or current and clearance levels) MUST be defined whether the policy is MLS/MCS enabled or not.
16*2d543d20SAndroid Build Coastguard Worker
17*2d543d20SAndroid Build Coastguard Worker**Statement definition:**
18*2d543d20SAndroid Build Coastguard Worker
19*2d543d20SAndroid Build Coastguard Worker```secil
20*2d543d20SAndroid Build Coastguard Worker    (context context_id (user_id role_id type_id levelrange_id)))
21*2d543d20SAndroid Build Coastguard Worker```
22*2d543d20SAndroid Build Coastguard Worker
23*2d543d20SAndroid Build Coastguard Worker**Where:**
24*2d543d20SAndroid Build Coastguard Worker
25*2d543d20SAndroid Build Coastguard Worker<table>
26*2d543d20SAndroid Build Coastguard Worker<colgroup>
27*2d543d20SAndroid Build Coastguard Worker<col width="25%" />
28*2d543d20SAndroid Build Coastguard Worker<col width="75%" />
29*2d543d20SAndroid Build Coastguard Worker</colgroup>
30*2d543d20SAndroid Build Coastguard Worker<tbody>
31*2d543d20SAndroid Build Coastguard Worker<tr class="odd">
32*2d543d20SAndroid Build Coastguard Worker<td align="left"><p><code>context</code></p></td>
33*2d543d20SAndroid Build Coastguard Worker<td align="left"><p>The <code>context</code> keyword.</p></td>
34*2d543d20SAndroid Build Coastguard Worker</tr>
35*2d543d20SAndroid Build Coastguard Worker<tr class="even">
36*2d543d20SAndroid Build Coastguard Worker<td align="left"><p><code>context_id</code></p></td>
37*2d543d20SAndroid Build Coastguard Worker<td align="left"><p>The <code>context</code> identifier.</p></td>
38*2d543d20SAndroid Build Coastguard Worker</tr>
39*2d543d20SAndroid Build Coastguard Worker<tr class="odd">
40*2d543d20SAndroid Build Coastguard Worker<td align="left"><p><code>user_id</code></p></td>
41*2d543d20SAndroid Build Coastguard Worker<td align="left"><p>A single previously declared <code>user</code> identifier.</p></td>
42*2d543d20SAndroid Build Coastguard Worker</tr>
43*2d543d20SAndroid Build Coastguard Worker<tr class="even">
44*2d543d20SAndroid Build Coastguard Worker<td align="left"><p><code>role_id</code></p></td>
45*2d543d20SAndroid Build Coastguard Worker<td align="left"><p>A single previously declared <code>role</code> identifier.</p></td>
46*2d543d20SAndroid Build Coastguard Worker</tr>
47*2d543d20SAndroid Build Coastguard Worker<tr class="odd">
48*2d543d20SAndroid Build Coastguard Worker<td align="left"><p><code>type_id</code></p></td>
49*2d543d20SAndroid Build Coastguard Worker<td align="left"><p>A single previously declared <code>type</code> or <code>typealias</code> identifier.</p></td>
50*2d543d20SAndroid Build Coastguard Worker</tr>
51*2d543d20SAndroid Build Coastguard Worker<tr class="even">
52*2d543d20SAndroid Build Coastguard Worker<td align="left"><p><code>levelrange_id</code></p></td>
53*2d543d20SAndroid Build Coastguard Worker<td align="left"><p>A single previously declared <code>levelrange</code> identifier. This entry may also be defined by anonymous or named <code>level</code>, <code>sensitivity</code>, <code>sensitivityalias</code>, <code>category</code>, <code>categoryalias</code> or <code>categoryset</code> as discussed in the <a href="#mls_labeling_statements">Multi-Level Security Labeling Statements</a> section and shown in the examples.</p></td>
54*2d543d20SAndroid Build Coastguard Worker</tr>
55*2d543d20SAndroid Build Coastguard Worker</tbody>
56*2d543d20SAndroid Build Coastguard Worker</table>
57*2d543d20SAndroid Build Coastguard Worker
58*2d543d20SAndroid Build Coastguard Worker**Examples:**
59*2d543d20SAndroid Build Coastguard Worker
60*2d543d20SAndroid Build Coastguard WorkerThis example uses a named context definition:
61*2d543d20SAndroid Build Coastguard Worker
62*2d543d20SAndroid Build Coastguard Worker```secil
63*2d543d20SAndroid Build Coastguard Worker    (context runas_exec_context (u object_r exec low_low))
64*2d543d20SAndroid Build Coastguard Worker
65*2d543d20SAndroid Build Coastguard Worker    (filecon "/system/bin/run-as" file runas_exec_context)
66*2d543d20SAndroid Build Coastguard Worker```
67*2d543d20SAndroid Build Coastguard Worker
68*2d543d20SAndroid Build Coastguard Workerto resolve/build a `file_contexts` entry of (assuming MLS enabled policy):
69*2d543d20SAndroid Build Coastguard Worker
70*2d543d20SAndroid Build Coastguard Worker```
71*2d543d20SAndroid Build Coastguard Worker    /system/bin/run-as  -- u:object_r:runas.exec:s0-s0
72*2d543d20SAndroid Build Coastguard Worker```
73*2d543d20SAndroid Build Coastguard Worker
74*2d543d20SAndroid Build Coastguard WorkerThis example uses an anonymous context where the previously declared `user role type levelrange` identifiers are used to specify two [`portcon`](cil_network_labeling_statements.md#portcon) statements:
75*2d543d20SAndroid Build Coastguard Worker
76*2d543d20SAndroid Build Coastguard Worker```secil
77*2d543d20SAndroid Build Coastguard Worker    (portcon udp 1024 (test.user object_r test.process ((s0) (s1))))
78*2d543d20SAndroid Build Coastguard Worker    (portcon tcp 1024 (test.user object_r test.process (system_low system_high)))
79*2d543d20SAndroid Build Coastguard Worker```
80*2d543d20SAndroid Build Coastguard Worker
81*2d543d20SAndroid Build Coastguard WorkerThis example uses an anonymous context for the first and named context for the second in a [`netifcon`](cil_network_labeling_statements.md#netifcon) statement:
82*2d543d20SAndroid Build Coastguard Worker
83*2d543d20SAndroid Build Coastguard Worker```secil
84*2d543d20SAndroid Build Coastguard Worker    (context netif_context (test.user object_r test.process ((s0 (c0)) (s1 (c0)))))
85*2d543d20SAndroid Build Coastguard Worker
86*2d543d20SAndroid Build Coastguard Worker    (netifcon eth04 (test.user object_r test.process ((s0 (c0)) (s1 (c0)))) netif_context)
87*2d543d20SAndroid Build Coastguard Worker```
88