1File Labeling Statements 2======================== 3 4filecon 5------- 6 7Define entries for labeling files. The compiler will produce these entries in a file called **`file_contexts`**`(5)` by default in the `cwd`. The compiler option `[-f|--filecontext <filename>]` may be used to specify a different path or file name. 8 9**Statement definition:** 10 11```secil 12 (filecon "path" file_type context_id) 13``` 14 15**Where:** 16 17<table> 18<colgroup> 19<col width="25%" /> 20<col width="75%" /> 21</colgroup> 22<tbody> 23<tr class="odd"> 24<td align="left"><p><code>filecon</code></p></td> 25<td align="left"><p>The <code>filecon</code> keyword.</p></td> 26</tr> 27<tr class="even"> 28<td align="left"><p><code>path</code></p></td> 29<td align="left"><p>A string representing the file path that may be in the form of a regular expression. The string must be enclosed within double quotes (e.g. <code>"/this/is/a/path(/.*)?"</code>)</p></td> 30</tr> 31<tr class="odd"> 32<td align="left"><p><code>file_type</code></p></td> 33<td align="left"><p>A single keyword representing a file type in the <code>file_contexts</code> file as follows:</p> 34<table> 35<colgroup> 36<col width="44%" /> 37<col width="55%" /> 38</colgroup> 39<tbody> 40<tr class="odd"> 41<td align="left"><p><strong>keyword</strong></p></td> 42<td align="left"><p><strong>file_contexts entry</strong></p></td> 43</tr> 44<tr class="even"> 45<td align="left"><p><code>file</code></p></td> 46<td align="left"><p><code>--</code></p></td> 47</tr> 48<tr class="odd"> 49<td align="left"><p><code>dir</code></p></td> 50<td align="left"><p><code>-d</code></p></td> 51</tr> 52<tr class="even"> 53<td align="left"><p><code>char</code></p></td> 54<td align="left"><p><code>-c</code></p></td> 55</tr> 56<tr class="odd"> 57<td align="left"><p><code>block</code></p></td> 58<td align="left"><p><code>-b</code></p></td> 59</tr> 60<tr class="even"> 61<td align="left"><p><code>socket</code></p></td> 62<td align="left"><p><code>-s</code></p></td> 63</tr> 64<tr class="odd"> 65<td align="left"><p><code>pipe</code></p></td> 66<td align="left"><p><code>-p</code></p></td> 67</tr> 68<tr class="even"> 69<td align="left"><p><code>symlink</code></p></td> 70<td align="left"><p><code>-l</code></p></td> 71</tr> 72<tr class="odd"> 73<td align="left"><p><code>any</code></p></td> 74<td align="left"><p>no entry</p></td> 75</tr> 76</tbody> 77</table></td> 78</tr> 79<tr class="even"> 80<td align="left"><p><code>context_id</code></p></td> 81<td align="left"><p>The security context to be allocated to the file, which may be:</p> 82<ul> 83<li><p>A previously declared <code>context</code> identifier or an anonymous security context (<code>user role type levelrange</code>), the range MUST be defined whether the policy is MLS/MCS enabled or not.</p></li> 84<li><p>An empty context list represented by <code>()</code> can be used to indicate that matching files should not be re-labeled. This will be interpreted as <code><<none>></code> within the <strong><code>file_contexts</code></strong><code>(5)</code> file.</p></li> 85</ul></td> 86</tr> 87</tbody> 88</table> 89 90**Examples:** 91 92These examples use one named, one anonymous and one empty context definition: 93 94```secil 95 (context runas_exec_context (u object_r exec low_low)) 96 97 (filecon "/system/bin/run-as" file runas_exec_context) 98 (filecon "/dev/socket/wpa_wlan[0-9]" any u:object_r:wpa.socket:s0-s0) 99 (filecon "/data/local/mine" dir ()) 100``` 101 102to resolve/build `file_contexts` entries of (assuming MLS enabled policy): 103 104``` 105 /system/bin/run-as -- u:object_r:runas.exec:s0 106 /dev/socket/wpa_wlan[0-9] u:object_r:wpa.socket:s0 107 /data/local/mine -d <<none>> 108``` 109 110fsuse 111----- 112 113Label filesystems that support SELinux security contexts. 114 115**Statement definition:** 116 117```secil 118 (fsuse fstype fsname context_id) 119``` 120 121**Where:** 122 123<table> 124<colgroup> 125<col width="25%" /> 126<col width="75%" /> 127</colgroup> 128<tbody> 129<tr class="odd"> 130<td align="left"><p><code>fsuse</code></p></td> 131<td align="left"><p>The <code>fsuse</code> keyword.</p></td> 132</tr> 133<tr class="even"> 134<td align="left"><p><code>fstype</code></p></td> 135<td align="left"><p>A single keyword representing the type of filesystem as follows:</p> 136<ul> 137<li><p><code>task</code> - For pseudo filesystems supporting task related services such as pipes and sockets.</p></li> 138<li><p><code>trans</code> - For pseudo filesystems such as pseudo terminals and temporary objects.</p></li> 139<li><p><code>xattr</code> - Filesystems supporting the extended attribute <code>security.selinux</code>. The labeling is persistent for filesystems that support extended attributes.</p></li> 140</ul></td> 141</tr> 142<tr class="odd"> 143<td align="left"><p><code>fsname</code></p></td> 144<td align="left"><p>Name of the supported filesystem (e.g. <code>ext4</code> or <code>pipefs</code>).</p></td> 145</tr> 146<tr class="even"> 147<td align="left"><p><code>context_id</code></p></td> 148<td align="left"><p>The security context to be allocated to the network interface.</p> 149<p>A previously declared <code>context</code> identifier or an anonymous security context (<code>user role type levelrange</code>), the range MUST be defined whether the policy is MLS/MCS enabled or not.</p></td> 150</tr> 151</tbody> 152</table> 153 154**Examples:** 155 156The [context](#context) identifiers are declared in the `file` namespace and the [`fsuse`](cil_file_labeling_statements.md#fsuse) statements in the global namespace: 157 158```secil 159 (block file 160 (type labeledfs) 161 (roletype object_r labeledfs) 162 (context labeledfs_context (u object_r labeledfs low_low)) 163 164 (type pipefs) 165 (roletype object_r pipefs) 166 (context pipefs_context (u object_r pipefs low_low)) 167 ... 168 ) 169 170 (fsuse xattr ex4 file.labeledfs_context) 171 (fsuse xattr btrfs file.labeledfs_context) 172 173 (fsuse task pipefs file.pipefs_context) 174 (fsuse task sockfs file.sockfs_context) 175 176 (fsuse trans devpts file.devpts_context) 177 (fsuse trans tmpfs file.tmpfs_context) 178``` 179 180genfscon 181-------- 182 183Used to allocate a security context to filesystems that cannot support any of the [`fsuse`](cil_file_labeling_statements.md#fsuse) file labeling options. Generally a filesystem would have a single default security context assigned by [`genfscon`](cil_file_labeling_statements.md#genfscon) from the root `(/)` that would then be inherited by all files and directories on that filesystem. The exception to this is the `/proc` filesystem, where directories can be labeled with a specific security context (as shown in the examples). 184 185**Statement definition:** 186 187```secil 188 (genfscon fsname path context_id) 189``` 190 191**Where:** 192 193<table> 194<colgroup> 195<col width="25%" /> 196<col width="75%" /> 197</colgroup> 198<tbody> 199<tr class="odd"> 200<td align="left"><p><code>genfscon</code></p></td> 201<td align="left"><p>The <code>genfscon</code> keyword.</p></td> 202</tr> 203<tr class="even"> 204<td align="left"><p><code>fsname</code></p></td> 205<td align="left"><p>Name of the supported filesystem (e.g. <code>rootfs</code> or <code>proc</code>).</p></td> 206</tr> 207<tr class="odd"> 208<td align="left"><p><code>path</code></p></td> 209<td align="left"><p>If <code>fsname</code> is <code>proc</code>, then the partial path (see examples). For all other types this must be ‘<code>/</code>’.</p></td> 210</tr> 211<tr class="even"> 212<td align="left"><p><code>context_id</code></p></td> 213<td align="left"><p>A previously declared <code>context</code> identifier or an anonymous security context (<code>user role type levelrange</code>), the range MUST be defined whether the policy is MLS/MCS enabled or not.</p></td> 214</tr> 215</tbody> 216</table> 217 218**Examples:** 219 220The [context](#context) identifiers are declared in the `file` namespace and the [`genfscon`](cil_file_labeling_statements.md#genfscon) statements are then inserted using the [`in`](cil_container_statements.md#in) container statement: 221 222```secil 223 (file 224 (type rootfs) 225 (roletype object_r rootfs) 226 (context rootfs_context (u object_r rootfs low_low)) 227 228 (type proc) 229 (roletype object_r proc) 230 (context rootfs_context (u object_r proc low_low)) 231 ... 232 ) 233 234 (in file 235 (genfscon rootfs / rootfs_context) 236 ; proc labeling can be further refined (longest matching prefix). 237 (genfscon proc / proc_context) 238 (genfscon proc /net/xt_qtaguid/ctrl qtaguid_proc_context) 239 (genfscon proc /sysrq-trigger sysrq_proc_context) 240 (genfscon selinuxfs / selinuxfs_context) 241 ) 242``` 243