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 602. 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. 61 623. 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: 63 64 (sensitivity s0) 65 (category c0) 66 (role object_r) 67 68 (block unconfined 69 (user user) 70 (type object) 71 ) 72 73 now a [`portcon`](cil_network_labeling_statements.md#portcon) statement can be defined that uses these individual components to build a context as follows: 74 75 (portcon udp 12345 (unconfined.user object_r unconfined.object ((s0) (s0(c0))))) 76 77Definitions 78----------- 79 80Statements that build on the objects, for example: 81 82- `(typeattributeset domain (process))` - Adds the [`type`](cil_type_statements.md#type) '`process`' to the [`typeattribute`](cil_type_statements.md#typeattribute) '`domain`'. 83 84- `(allow domain process (file (read write))))` - Adds an [`allow`](cil_access_vector_rules.md#allow) rule referencing `domain`, `process` and the `file class`. 85 86Definitions may be repeated many times throughout the policy. Duplicates will resolve to a single definition during compilation. 87 88Symbol Character Set 89-------------------- 90 91Symbols (any string not enclosed in double quotes) must only contain alphanumeric `[a-z A-Z] [0-9]` characters plus the following special characters: `\.@=/-_$%@+!|&^:` 92 93However symbols are checked for any specific character set limitations, for example: 94 95- 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 `[-]`. 96 97- IP addresses must conform to IPv4 or IPv6 format. 98 99- Memory, ports, irqs must be numeric `[0-9]`. 100 101String Character Set 102-------------------- 103 104Strings are enclosed within double quotes (e.g. `"This is a string"`), and may contain any character except the double quote ("). 105 106Comments 107-------- 108 109Comments start with a semicolon '`;`' and end when a new line is started. 110 111Namespaces 112---------- 113 114CIL 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: 115 116 (block example_ns 117 (type process) 118 (type object) 119 (class file (open read write getattr)) 120 121 (allow process object (file (open read getattr))) 122 ) 123 124will resolve to the following kernel policy language statement: 125 126 allow example_ns.process example_ns.object : example_ns.file { open read getattr }; 127 128Global Namespace 129---------------- 130 131CIL 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: 132 133 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 134 ; This example has three namespace 'tmpfs' types declared: 135 ; 1) Global .tmpfs 136 ; 2) file.tmpfs 137 ; 3) other_ns.tmpfs 138 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 139 140 ; This type is the global tmpfs: 141 (type tmpfs) 142 143 (block file 144 ; file namespace tmpfs 145 (type tmpfs) 146 (class file (open read write getattr)) 147 148 ; This rule will reference the local namespace for src and tgt: 149 (allow tmpfs tmpfs (file (open))) 150 ; Resulting policy rule: 151 ; allow file.tmpfs file.tmpfs : file.file open; 152 153 ; This rule will reference the local namespace for src and global for tgt: 154 (allow tmpfs .tmpfs (file (read))) 155 ; Resulting policy rule: 156 ; allow file.tmpfs tmpfs : file.file read; 157 158 ; This rule will reference the global namespace for src and tgt: 159 (allow .tmpfs .tmpfs (file (write))) 160 ; Resulting policy rule: 161 ; allow tmpfs tmpfs : file.file write; 162 163 ; This rule will reference the other_ns namespace for src and 164 ; local namespace for tgt: 165 (allow other_ns.tmpfs tmpfs (file (getattr))) 166 ; Resulting policy rule: 167 ; allow other_ns.tmpfs file.tmpfs : file.file getattr; 168 ) 169 170 (block other_ns 171 (type tmpfs) 172 ) 173 174Should 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). 175 176Expressions 177----------- 178 179Expressions 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), [`validatetrans`](cil_constraint_statements.md#validatetrans) 180 181CIL 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 postfix or reverse Polish notation). The syntax is as follows, where the parenthesis are part of the syntax: 182 183 expr_set = (name ... | expr ...) 184 expr = (expr_key expr_set ...) 185 expr_key = and | or | xor | not | all | eq | neq | dom | domby | incomp | range 186 187The 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). 188 189| expr_key | classpermissionset roleattributeset typeattributeset | categoryset | booleanif tunableif | constrain mlsconstrain validatetrans mlsvalidatetrans | 190|:----------:|:----------:|:----------:|:----------:|:----------:| 191| **`dom`** | | | | **X** | 192| **`domby`** | | | | **X** | 193| **`incomp`** | | | | **X** | 194| **`eq`** | | | **X** | **X** | 195| **`ne`** | | | **X** | **X** | 196| **`and`** | **X** | **X** | **X** | **X** | 197| **`or`** | **X** | **X** | **X** | **X** | 198| **`not`** | **X** | **X** | **X** | **X** | 199| **`xor`** | **X** | **X** | **X** | | 200| **`all`** | **X** | **X** | | | 201| **`range`** | | **X** | | | 202 2031. 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: 204 205 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`: 206 207 (typeattribute all_fs_type_except_usermodehelper_and_proc_security) 208 209 (typeattributeset all_fs_type_except_usermodehelper_and_proc_security 210 (and 211 (and 212 fs_type 213 (not file.usermodehelper) 214 ) 215 (not file.proc_security) 216 ) 217 ) 218 219 The `cps_1 classpermissionset` identifier includes all permissions except `load_policy` and `setenforce`: 220 221 (class security (compute_av compute_create compute_member check_context load_policy compute_relabel compute_user setenforce setbool setsecparam setcheckreqprot read_policy)) 222 223 (classpermission cps_1) 224 225 (classpermissionset cps_1 (security (not (load_policy setenforce)))) 226 227 This example includes all permissions in the associated [`classpermissionset`](cil_class_and_permission_statements.md#classpermissionset) identifier `security_all_perms`: 228 229 (class security (compute_av compute_create compute_member check_context load_policy 230 compute_relabel compute_user setenforce setbool setsecparam setcheckreqprot 231 read_policy) 232 ) 233 234 (classpermission security_all_perms) 235 236 (classpermissionset security_all_perms (security (all))) 237 2382. 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. 239 240 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. 241 2423. 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: 243 244 (booleanif disableAudio 245 (false 246 (allow process device.audio_device (chr_file_set (rw_file_perms))) 247 ) 248 ) 249 250 (booleanif (and (not disableAudio) (not disableAudioCapture)) 251 (true 252 (allow process device.audio_capture_device (chr_file_set (rw_file_perms))) 253 ) 254 ) 255 2564. 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: 257 258 ; Process transition: Require equivalence unless the subject is trusted. 259 (mlsconstrain (process (transition dyntransition)) 260 (or (and (eq h1 h2) (eq l1 l2)) (eq t1 mlstrustedsubject))) 261 262 ; The equivalent policy language mlsconstrain statememt is: 263 ;mlsconstrain process { transition dyntransition } 264 ; ((h1 eq h2 and l1 eq l2) or t1 == mlstrustedsubject); 265 266 ; Process read operations: No read up unless trusted. 267 (mlsconstrain (process (getsched getsession getpgid getcap getattr ptrace share)) 268 (or (dom l1 l2) (eq t1 mlstrustedsubject))) 269 270 ; The equivalent policy language mlsconstrain statememt is: 271 ;mlsconstrain process { getsched getsession getpgid getcap getattr ptrace share } 272 ; (l1 dom l2 or t1 == mlstrustedsubject); 273 274Name String 275----------- 276 277Used to define [`macro`](cil_call_macro_statements.md#macro) statement parameter string types: 278 279 (call macro1("__kmsg__")) 280 281 (macro macro1 ((string ARG1)) 282 (typetransition audit.process device.device chr_file ARG1 device.klog_device) 283 ) 284 285Alternatively: 286 287 (call macro1("__kmsg__")) 288 289 (macro macro1 ((name ARG1)) 290 (typetransition audit.process device.device chr_file ARG1 device.klog_device) 291 ) 292 293self 294---- 295 296The [`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:. 297 298 (allow unconfined.process self (file (read write))) 299