xref: /aosp_15_r20/external/selinux/secilc/docs/cil_access_vector_rules.md (revision 2d543d20722ada2425b5bdab9d0d1d29470e7bba)
1Access Vector Rules
2===================
3
4Rules involving a source type, a target type, and class permissions or extended permissions.
5
6**Rule definition:**
7
8```secil
9    (av_flavor source_id target_id|self|notself|other classpermission_id|permissionx_id)
10```
11
12**Where:**
13
14<table>
15<colgroup>
16<col width="27%" />
17<col width="72%" />
18</colgroup>
19<tbody>
20<tr class="odd">
21<td align="left"><p><code>av_flavor</code></p></td>
22<td align="left"><p>The flavor of access vector rule. Possible flavors are <code>allow</code>, <code>auditallow</code>, <code>dontaudit</code>, <code>neverallow</code>, <code>deny</code>, <code>allowx</code>, <code>auditallowx</code>, <code>dontauditx</code>, and <code>neverallowx</code>.</p></td>
23<tr class="even">
24<td align="left"><p><code>source_id</code></p></td>
25<td align="left"><p>A single previously defined source <code>type</code>, <code>typealias</code> or <code>typeattribute</code> identifier.</p></td>
26</tr>
27<tr class="odd">
28<td align="left"><p><code>target_id</code></p></td>
29<td align="left"><p>A single previously defined target <code>type</code>, <code>typealias</code> or <code>typeattribute</code> identifier.</p>
30<p> Instead it can be one of the special keywords <code>self</code>, <code>notself</code> or <code>other</code>.</p>
31<p>The <code>self</code> keyword may be used to signify that source and target are the same. If the source is an attribute, each type of the source will be paired with itself as the target. The <code>notself</code> keyword may be used to signify that the target is all types except for the types of the source. The <code>other</code> keyword may be used as a short-hand way of writing a rule for each type of the source where it is paired with all of the other types of the source as the target.</p></td>
32</tr>
33<tr class="even">
34<td align="left"><p><code>classpermission_id</code></p></td>
35<td align="left"><p>A single named or anonymous <code>classpermissionset</code> or a single set of <code>classmap</code>/<code>classmapping</code> identifiers. Used for <code>allow</code>, <code>auditallow</code>, <code>dontaudit</code>, <code>neverallow</code> rules.</p></td>
36</tr>
37<tr class="odd">
38<td align="left"><p><code>permissionx_id</code></p></td>
39<td align="left"><p>A single named or anonymous <code>permissionx</code>. Used for <code>allowx</code>, <code>auditallowx</code>, <code>dontauditx</code>, <code>neverallowx</code> rules.</p></td>
40</tr>
41</tbody>
42</table>
43
44allow
45-----
46
47Specifies the access allowed between a source and target type. Note that access may be refined by constraint rules based on the source, target and class ([`validatetrans`](cil_constraint_statements.md#validatetrans) or [`mlsvalidatetrans`](cil_constraint_statements.md#mlsvalidatetrans)) or source, target class and permissions ([`constrain`](cil_constraint_statements.md#constrain) or [`mlsconstrain`](cil_constraint_statements.md#mlsconstrain) statements).
48
49**Rule definition:**
50
51```secil
52    (allow source_id target_id|self|notself|other classpermissionset_id ...)
53```
54
55**Examples:**
56
57These examples show a selection of possible permutations of [`allow`](cil_access_vector_rules.md#allow) rules:
58
59```secil
60    (class binder (impersonate call set_context_mgr transfer receive))
61    (class property_service (set))
62    (class zygote (specifyids specifyrlimits specifycapabilities specifyinvokewith specifyseinfo))
63
64    (classpermission cps_zygote)
65    (classpermissionset cps_zygote (zygote (not (specifyids))))
66
67    (classmap android_classes (set_1 set_2 set_3))
68
69    (classmapping android_classes set_1 (binder (all)))
70    (classmapping android_classes set_1 (property_service (set)))
71    (classmapping android_classes set_1 (zygote (not (specifycapabilities))))
72
73    (classmapping android_classes set_2 (binder (impersonate call set_context_mgr transfer)))
74    (classmapping android_classes set_2 (zygote (specifyids specifyrlimits specifycapabilities specifyinvokewith)))
75
76    (classmapping android_classes set_3 cps_zygote)
77    (classmapping android_classes set_3 (binder (impersonate call set_context_mgr)))
78
79    (block av_rules
80        (type type_1)
81        (type type_2)
82        (type type_3)
83        (type type_4)
84        (type type_5)
85
86        (typeattribute all_types)
87        (typeattributeset all_types (all))
88
89    ; These examples have named and anonymous classpermissionset's and
90    ; classmap/classmapping statements
91        (allow type_1 self (property_service (set)))          ; anonymous
92        (allow type_2 self (zygote (specifyids)))             ; anonymous
93        (allow type_3 self cps_zygote)                        ; named
94        (allow type_4 self (android_classes (set_3)))         ; classmap/classmapping
95        (allow all_types all_types (android_classes (set_2))) ; classmap/classmapping
96
97    ;; This rule will cause the build to fail unless --disable-neverallow
98    ;    (neverallow type_5 all_types (property_service (set)))
99        (allow type_5 type_5 (property_service (set)))
100        (allow type_1 all_types (property_service (set)))
101    )
102```
103
104auditallow
105----------
106
107Audit the access rights defined if there is a valid allow rule. Note: It does NOT allow access, it only audits the event.
108
109**Rule definition:**
110
111```secil
112    (auditallow source_id target_id|self|notself|other classpermissionset_id)
113```
114
115**Example:**
116
117This example will log an audit event whenever the corresponding [`allow`](cil_access_vector_rules.md#allow) rule grants access to the specified permissions:
118
119```secil
120    (allow release_app.process secmark_demo.browser_packet (packet (send recv append bind)))
121
122    (auditallow release_app.process secmark_demo.browser_packet (packet (send recv)))
123```
124
125dontaudit
126---------
127
128Do not audit the access rights defined when access denied. This stops excessive log entries for known events.
129
130Note that these rules can be omitted by the CIL compiler command line parameter `-D` or `--disable-dontaudit` flags.
131
132**Rule definition:**
133
134```secil
135    (dontaudit source_id target_id|self|notself|other classpermissionset_id ...)
136```
137
138**Example:**
139
140This example will not audit the denied access:
141
142```secil
143    (dontaudit zygote.process self (capability (fsetid)))
144```
145
146neverallow
147----------
148
149Never allow access rights defined. This is a compiler enforced action that will stop compilation until the offending rules are modified.
150
151Note that these rules can be over-ridden by the CIL compiler command line parameter `-N` or `--disable-neverallow` flags.
152
153**Rule definition:**
154
155```secil
156    (neverallow source_id target_id|self|notself|other classpermissionset_id ...)
157```
158
159**Example:**
160
161This example will not compile as `type_3` is not allowed to be a source type for the [`allow`](cil_access_vector_rules.md#allow) rule:
162
163```secil
164    (class property_service (set))
165
166    (block av_rules
167        (type type_1)
168        (type type_2)
169        (type type_3)
170        (typeattribute all_types)
171        (typeattributeset all_types ((all)))
172
173        (neverallow type_3 all_types (property_service (set)))
174        ; This rule will fail compilation:
175        (allow type_3 self (property_service (set)))
176    )
177```
178deny
179----------
180
181Remove the access rights defined from any matching allow rules. These rules are processed before [`neverallow`](cil_access_vector_rules.md#neverallow) checking.
182
183**Rule definition:**
184
185```secil
186    (deny source_id target_id|self classpermissionset_id ...)
187```
188
189**Example:**
190
191```secil
192    (class class1 (perm1 perm2))
193
194    (type type1)
195    (type type2)
196    (allow type1 type2 (class1 (perm1))) ; Allow-1
197    (deny type1 type2 (class1 (perm1)))  ; Deny-1
198    ; Allow-1 will be complete removed by Deny-1.
199
200    (type type3)
201    (type type4)
202    (allow type3 type4 (class1 (perm1 perm2))) ; Allow-2
203    (deny type3 type4 (class1 (perm1)))        ; Deny-2
204    ; Allow-2 will be removed and replaced with the following when Deny-2 is evaluated
205    ; (allow type3 type4 (class1 (perm2)))
206
207    (type type5)
208    (type type6)
209    (typeattribute attr1)
210    (typeattributeset attr1 (type5 type6))
211    (allow attr1 attr1 (class1 (perm1))) ; Allow-3
212    (deny type5 type6 (class1 (perm1)))  ; Deny-3
213    ; Allow-3 will be removed and replaced with the following when Deny-3 is evaluated
214    ; (allow type6 attr1 (class1 (perm1)))
215    ; (allow type5 type5 (class1 (perm1)))
216```
217
218allowx
219------
220
221Specifies the access allowed between a source and target type using extended permissions. Unlike the [`allow`](cil_access_vector_rules.md#allow) statement, the statements [`validatetrans`](cil_constraint_statements.md#validatetrans), [`mlsvalidatetrans`](cil_constraint_statements.md#mlsvalidatetrans), [`constrain`](cil_constraint_statements.md#constrain), and [`mlsconstrain`](cil_constraint_statements.md#mlsconstrain) do not limit accesses granted by [`allowx`](cil_access_vector_rules.md#allowx).
222
223Note that for this to work there must *also* be valid equivalent [`allow`](cil_access_vector_rules.md#allow) rules present.
224
225**Rule definition:**
226
227```secil
228    (allowx source_id target_id|self|notself|other permissionx_id)
229```
230
231**Examples:**
232
233These examples show a selection of possible permutations of [`allowx`](cil_access_vector_rules.md#allowx) rules:
234
235```secil
236    (allow type_1 type_2 (tcp_socket (ioctl))) ;; pre-requisite
237    (allowx type_1 type_2 (ioctl tcp_socket (range 0x2000 0x20FF)))
238
239    (permissionx ioctl_nodebug (ioctl udp_socket (not (range 0x4000 0x4010))))
240    (allow type_3 type_4 (udp_socket (ioctl))) ;; pre-requisite
241    (allowx type_3 type_4 ioctl_nodebug)
242```
243
244
245auditallowx
246-----------
247
248Audit the access rights defined if there is a valid [`allowx`](cil_access_vector_rules.md#allowx) rule. It does NOT allow access, it only audits the event.
249
250Note that for this to work there must *also* be valid equivalent [`auditallow`](cil_access_vector_rules.md#auditallow) rules present.
251
252**Rule definition:**
253
254```secil
255    (auditallowx source_id target_id|self|notself|other permissionx_id)
256```
257
258**Examples:**
259
260This example will log an audit event whenever the corresponding [`allowx`](cil_access_vector_rules.md#allowx) rule grants access to the specified extended permissions:
261
262```secil
263    (allowx type_1 type_2 (ioctl tcp_socket (range 0x2000 0x20FF)))
264
265    (auditallow type_1 type_2 (tcp_socket (ioctl))) ;; pre-requisite
266    (auditallowx type_1 type_2 (ioctl tcp_socket (range 0x2005 0x2010)))
267```
268
269dontauditx
270----------
271
272Do not audit the access rights defined when access denied. This stops excessive log entries for known events.
273
274Note that for this to work there must *also* be at least one [`allowx`](cil_access_vector_rules.md#allowx) rule associated with the target type.
275
276Note that these rules can be omitted by the CIL compiler command line parameter `-D` or `--disable-dontaudit` flags.
277
278**Rule definition:**
279
280```secil
281    (dontauditx source_id target_id|self|notself|other permissionx_id)
282```
283
284**Examples:**
285
286This example will not audit the denied access:
287
288```secil
289    (allowx type_1 type_2 (ioctl tcp_socket (0x1))) ;; pre-requisite, just some irrelevant random ioctl
290    (dontauditx type_1 type_2 (ioctl tcp_socket (range 0x3000 0x30FF)))
291```
292
293neverallowx
294----------
295Never allow access rights defined for extended permissions. This is a compiler enforced action that will stop compilation until the offending rules are modified.
296
297Note that these rules can be over-ridden by the CIL compiler command line parameter `-N` or `--disable-neverallow` flags.
298
299**Rule definition:**
300
301```secil
302    (neverallowx source_id target_id|self|notself|other permissionx_id)
303```
304
305**Examples:**
306
307This example will not compile as `type_3` is not allowed to be a source type and ioctl range for the [`allowx`](cil_access_vector_rules.md#allowx) rule:
308
309```secil
310	(class property_service (ioctl))
311	(block av_rules
312		(type type_1)
313		(type type_2)
314		(type type_3)
315		(typeattribute all_types)
316		(typeattributeset all_types ((all)))
317		(neverallowx type_3 all_types (ioctl property_service (range 0x2000 0x20FF)))
318		; This rule will fail compilation:
319		(allowx type_3 self (ioctl property_service (0x20A0)))
320	)
321```
322