• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1CIL Information
2===============
3
41.  Not all possible alternate statement permutations are shown, however there should be enough variation to work out any other valid formats. There is also an example [`policy.cil`](../test/policy.cil#example-policy) file in the test directory.
5
62.  The MLS components on contexts and user statements must be declared even if the policy does not support MCS/MLS.
7
83.  The CIL compiler will not build a policy unless it also has as a minimum: one [`allow`](cil_access_vector_rules.md#allow) rule, one [`sid`](cil_sid_statements.md#sid), [`sidorder`](cil_sid_statements.md#sidorder) and [`sidcontext`](cil_sid_statements.md#sidcontext) statement.
9
104.  The role `object_r` must be explicitly associated to contexts used for labeling objects. The original **`checkpolicy`**`(8)` and **`checkmodule`**`(8)` compilers did this by default - CIL does not.
11
125.  Be aware that CIL allows [`class`](cil_class_and_permission_statements.md#class) statements to be declared in a namespace, however the policy author needs to note that applications (and the kernel) generally reference a class by its well known class identifier (e.g. `zygote`) however if declared in a namespace (e.g. `(block zygote (class zygote (...)))` or `(block zygote (class class (...)))`) it would be prefixed with that namespace (e.g. `zygote.zygote` or `zygote.class`). Unless the application / kernel code was updated the class would never be resolved, therefore it is recommended that classes are declared in the global namespace.
13
146.  Where possible use [`typeattribute`](cil_type_statements.md#typeattribute)'s when defining source/target [`allow`](cil_access_vector_rules.md#allow) rules instead of multiple [`allow`](cil_access_vector_rules.md#allow) rules with individual [`type`](cil_type_statements.md#type)'s. This will lead to the generation of much smaller kernel policy files.
15
167.  The [](http://github.com/SELinuxProject/cil/wiki) site explains the language however some of the statement definitions are dated.
17
18Declarations
19------------
20
21Declarations may be named or anonymous and have three different forms:
22
231.  Named declarations - These create new objects that introduce a name or identifier, for example:
24
25    `(type process)` - creates a [`type`](cil_type_statements.md#type) with an identifier of `process`.
26
27    `(typeattribute domain)` - creates a [`typeattribute`](cil_type_statements.md#typeattribute) with an identifier of `domain`.
28
29    `(class file (read write))` - creates a [`class`](cil_class_and_permission_statements.md#class) with an identifier of `file` that has `read` and `write` permissions associated to it.
30
31    The list of declaration type statement keywords are:
32
33    block
34    optional
35    common
36    class
37    classmap
38    classmapping
39    sid
40    user
41    role
42    roleattribute
43    type
44    classpermission
45    classpermissionset
46    typeattribute
47    typealias
48    tunable
49    sensitivity
50    sensitivityalias
51    category
52    categoryalias
53    categoryset
54    level
55    levelrange
56    context
57    ipaddr
58    macro
59    policycap
60
612.  Explicit anonymous declarations - These are currently restricted to IP addresses where they can be declared directly in statements by enclosing them within parentheses e.g. `(127.0.0.1)` or `(::1)`. See the [Network Labeling Statements](#network_labeling) section for examples.
62
633.  Anonymous declarations - These have been previously declared and the object already exists, therefore they may be referenced by their name or identifier within statements. For example the following declare all the components required to specify a context:
64
65    ```secil
66        (sensitivity s0)
67        (category c0)
68        (role object_r)
69
70        (block unconfined
71            (user user)
72            (type object)
73        )
74    ```
75
76    now a [`portcon`](cil_network_labeling_statements.md#portcon) statement can be defined that uses these individual components to build a context as follows:
77
78    ```secil
79        (portcon udp 12345 (unconfined.user object_r unconfined.object ((s0) (s0(c0)))))
80    ```
81
82Definitions
83-----------
84
85Statements that build on the objects, for example:
86
87-   `(typeattributeset domain (process))` - Adds the [`type`](cil_type_statements.md#type) '`process`' to the [`typeattribute`](cil_type_statements.md#typeattribute) '`domain`'.
88
89-   `(allow domain process (file (read write))))` - Adds an [`allow`](cil_access_vector_rules.md#allow) rule referencing `domain`, `process` and the `file class`.
90
91Definitions may be repeated many times throughout the policy. Duplicates will resolve to a single definition during compilation.
92
93Symbol Character Set
94--------------------
95
96Symbols (any string not enclosed in double quotes) must only contain alphanumeric `[a-z A-Z] [0-9]` characters plus the following special characters: `\.@=/-_$%@+!|&^:`
97
98However symbols are checked for any specific character set limitations, for example:
99
100-   Names or identifiers must start with an alpa character `[a-z A-Z]`, the remainder may be alphanumeric `[a-z A-Z] [0-9]` characters plus underscore `[_]` or hyphen `[-]`.
101
102-   IP addresses must conform to IPv4 or IPv6 format.
103
104-   Memory, ports, irqs must be numeric `[0-9]`.
105
106String Character Set
107--------------------
108
109Strings are enclosed within double quotes (e.g. `"This is a string"`), and may contain any character except the double quote (").
110
111Comments
112--------
113
114Comments start with a semicolon '`;`' and end when a new line is started.
115
116Namespaces
117----------
118
119CIL supports namespaces via containers such as the [`block`](cil_container_statements.md#block) statement. When a block is resolved to form the parent / child relationship a dot '`.`' is used, for example the following [`allow`](cil_access_vector_rules.md#allow) rule:
120
121```secil
122    (block example_ns
123        (type process)
124        (type object)
125        (class file (open read write getattr))
126
127        (allow process object (file (open read getattr)))
128    )
129```
130
131will resolve to the following kernel policy language statement:
132
133```
134    allow example_ns.process example_ns.object : example_ns.file { open read getattr };
135```
136
137Global Namespace
138----------------
139
140CIL has a global namespace that is always present. Any symbol that is declared outside a container is in the global namespace. To reference a symbol in global namespace, the symbol should be prefixed with a dot '`.`' as shown in the following example:
141
142```secil
143    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
144    ; This example has three namespace 'tmpfs' types declared:
145    ;    1) Global .tmpfs
146    ;    2) file.tmpfs
147    ;    3) other_ns.tmpfs
148    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
149
150    ; This type is the global tmpfs:
151    (type tmpfs)
152
153    (block file
154        ; file namespace tmpfs
155        (type tmpfs)
156        (class file (open read write getattr))
157
158        ; This rule will reference the local namespace for src and tgt:
159        (allow tmpfs tmpfs (file (open)))
160        ; Resulting policy rule:
161        ; allow file.tmpfs file.tmpfs : file.file open;
162
163        ; This rule will reference the local namespace for src and global for tgt:
164        (allow tmpfs .tmpfs (file (read)))
165        ; Resulting policy rule:
166        ; allow file.tmpfs tmpfs : file.file read;
167
168        ; This rule will reference the global namespace for src and tgt:
169        (allow .tmpfs .tmpfs (file (write)))
170        ; Resulting policy rule:
171        ; allow tmpfs tmpfs : file.file write;
172
173        ; This rule will reference the other_ns namespace for src and
174        ; local namespace for tgt:
175        (allow other_ns.tmpfs tmpfs (file (getattr)))
176        ; Resulting policy rule:
177        ; allow other_ns.tmpfs file.tmpfs : file.file getattr;
178    )
179
180    (block other_ns
181        (type tmpfs)
182    )
183```
184
185Should the symbol not be prefixed with a dot, the current namespace would be searched first and then the global namespace (provided there is not a symbol of that name in the current namespace).
186
187Expressions
188-----------
189
190Expressions may occur in the following CIL statements: [`booleanif`](cil_conditional_statements.md#booleanif), [`tunableif`](cil_conditional_statements.md#tunableif), [`classpermissionset`](cil_class_and_permission_statements.md#classpermissionset), [`typeattributeset`](cil_type_statements.md#typeattributeset), [`roleattributeset`](cil_role_statements.md#roleattributeset), [`categoryset`](cil_mls_labeling_statements.md#categoryset), [`constrain`](cil_constraint_statements.md#constrain), [`mlsconstrain`](cil_constraint_statements.md#mlsconstrain), [`validatetrans`](cil_constraint_statements.md#validatetrans), [`mlsvalidatetrans`](cil_constraint_statements.md#mlsvalidatetrans)
191
192CIL expressions use the [prefix](http://www.cs.man.ac.uk/~pjj/cs212/fix.html) or Polish notation and may be nested (note that the kernel policy language uses infix notation). The syntax is as follows, where the parenthesis are part of the syntax:
193
194```
195    expr_set = (name ... | expr ...)
196    expr = (expr_key expr_set ...)
197    expr_key = and | or | xor | not | all | eq | neq | dom | domby | incomp | range
198```
199
200The number of `expr_set`'s in an `expr` is dependent on the statement type (there are four different classes as defined below) that also influence the valid `expr_key` entries (e.g. `dom`, `domby`, `incomp` are only allowed in constraint statements).
201
202| expr_key | classpermissionset roleattributeset typeattributeset | categoryset | booleanif tunableif | constrain mlsconstrain validatetrans mlsvalidatetrans |
203|:----------:|:----------:|:----------:|:----------:|:----------:|
204| **`dom`**    |                        |                      |                      | **X**           |
205| **`domby`**  |                        |                      |                      | **X**           |
206| **`incomp`** |                        |                      |                      | **X**           |
207| **`eq`**     |                        |                      | **X**                | **X**           |
208| **`ne`**     |                        |                      | **X**                | **X**           |
209| **`and`**    | **X**                  | **X**                | **X**                | **X**           |
210| **`or`**     | **X**                  | **X**                | **X**                | **X**           |
211| **`not`**    | **X**                  | **X**                | **X**                | **X**           |
212| **`xor`**    | **X**                  | **X**                | **X**                |                 |
213| **`all`**    | **X**                  | **X**                |                      |                 |
214| **`range`**  |                        | **X**                |                      |                 |
215
2161.  The [`classpermissionset`](cil_class_and_permission_statements.md#classpermissionset), [`roleattributeset`](cil_role_statements.md#roleattributeset) and [`typeattributeset`](cil_type_statements.md#typeattributeset) statements allow `expr_set` to mix names and `expr`s with `expr_key` values of: `and`, `or`, `xor`, `not`, `all` as shown in the examples:
217
218    This example includes all `fs_type type` entries except `file.usermodehelper` and `file.proc_security` in the associated [`typeattribute`](cil_type_statements.md#typeattribute) identifier `all_fs_type_except_usermodehelper_and_proc_security`:
219
220    ```secil
221        (typeattribute all_fs_type_except_usermodehelper_and_proc_security)
222
223        (typeattributeset all_fs_type_except_usermodehelper_and_proc_security
224            (and
225                (and
226                    fs_type
227                    (not file.usermodehelper)
228                )
229                (not file.proc_security)
230            )
231        )
232    ```
233
234    The `cps_1 classpermissionset` identifier includes all permissions except `load_policy` and `setenforce`:
235
236    ```secil
237        (class security (compute_av compute_create compute_member check_context load_policy compute_relabel compute_user setenforce setbool setsecparam setcheckreqprot read_policy))
238
239        (classpermission cps_1)
240
241        (classpermissionset cps_1 (security (not (load_policy setenforce))))
242    ```
243
244    This example includes all permissions in the associated [`classpermissionset`](cil_class_and_permission_statements.md#classpermissionset) identifier `security_all_perms`:
245
246    ```secil
247        (class security (compute_av compute_create compute_member check_context load_policy
248            compute_relabel compute_user setenforce setbool setsecparam setcheckreqprot
249            read_policy)
250        )
251
252        (classpermission security_all_perms)
253
254        (classpermissionset security_all_perms (security (all)))
255    ```
256
2572.  The [`categoryset`](cil_mls_labeling_statements.md#categoryset) statement allows `expr_set` to mix names and `expr_key` values of: `and`, `or`, `not`, `xor`, `all`, `range` as shown in the examples.
258
259    Category expressions are also allowed in [`sensitivitycategory`](cil_mls_labeling_statements.md#sensitivitycategory), [`level`](cil_mls_labeling_statements.md#level), and [`levelrange`](cil_mls_labeling_statements.md#levelrange) statements.
260
2613.  The [`booleanif`](cil_conditional_statements.md#booleanif) and [`tunableif`](cil_conditional_statements.md#tunableif) statements only allow an `expr_set` to have one `name` or `expr` with `expr_key` values of `and`, `or`, `xor`, `not`, `eq`, `neq` as shown in the examples:
262
263    ```secil
264        (booleanif disableAudio
265            (false
266                (allow process device.audio_device (chr_file_set (rw_file_perms)))
267            )
268        )
269
270        (booleanif (and (not disableAudio) (not disableAudioCapture))
271            (true
272                (allow process device.audio_capture_device (chr_file_set (rw_file_perms)))
273            )
274        )
275    ```
276
2774.  The [`constrain`](cil_constraint_statements.md#constrain), [`mlsconstrain`](cil_constraint_statements.md#mlsconstrain), [`validatetrans`](cil_constraint_statements.md#validatetrans) and [`mlsvalidatetrans`](cil_constraint_statements.md#mlsvalidatetrans) statements only allow an `expr_set` to have one `name` or `expr` with `expr_key` values of `and`, `or`, `not`, `all`, `eq`, `neq`, `dom`, `domby`, `incomp`. When `expr_key` is `dom`, `domby` or `incomp`, it must be followed by a string (e.g. `h1`, `l2`) and another string or a set of `name`s. The following examples show CIL constraint statements and their policy language equivalents:
278
279    ```secil
280        ; Process transition:  Require equivalence unless the subject is trusted.
281        (mlsconstrain (process (transition dyntransition))
282            (or (and (eq h1 h2) (eq l1 l2)) (eq t1 mlstrustedsubject)))
283
284        ; The equivalent policy language mlsconstrain statememt is:
285        ;mlsconstrain process { transition dyntransition }
286        ;    ((h1 eq h2 and l1 eq l2) or t1 == mlstrustedsubject);
287
288        ; Process read operations: No read up unless trusted.
289        (mlsconstrain (process (getsched getsession getpgid getcap getattr ptrace share))
290            (or (dom l1 l2) (eq t1 mlstrustedsubject)))
291
292        ; The equivalent policy language mlsconstrain statememt is:
293        ;mlsconstrain process { getsched getsession getpgid getcap getattr ptrace share }
294        ;    (l1 dom l2 or t1 == mlstrustedsubject);
295    ```
296
297Name String
298-----------
299
300Used to define [`macro`](cil_call_macro_statements.md#macro) statement parameter string types:
301
302```secil
303    (call macro1("__kmsg__"))
304
305    (macro macro1 ((string ARG1))
306        (typetransition audit.process device.device chr_file ARG1 device.klog_device)
307    )
308```
309
310Alternatively:
311
312```secil
313    (call macro1("__kmsg__"))
314
315    (macro macro1 ((name ARG1))
316        (typetransition audit.process device.device chr_file ARG1 device.klog_device)
317    )
318```
319
320self
321----
322
323The [`self`](cil_reference_guide.md#self) keyword may be used as the target in AVC rule statements, and means that the target is the same as the source as shown in the following example:.
324
325```secil
326    (allow unconfined.process self (file (read write)))
327```
328