Home | History | Annotate | Download | only in selinux
      1 <html devsite>
      2   <head>
      3     <title>Customizing SELinux</title>
      4     <meta name="project_path" value="/_project.yaml" />
      5     <meta name="book_path" value="/_book.yaml" />
      6   </head>
      7   <body>
      8   <!--
      9       Copyright 2017 The Android Open Source Project
     10 
     11       Licensed under the Apache License, Version 2.0 (the "License");
     12       you may not use this file except in compliance with the License.
     13       You may obtain a copy of the License at
     14 
     15           http://www.apache.org/licenses/LICENSE-2.0
     16 
     17       Unless required by applicable law or agreed to in writing, software
     18       distributed under the License is distributed on an "AS IS" BASIS,
     19       WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     20       See the License for the specific language governing permissions and
     21       limitations under the License.
     22   -->
     23 
     24 
     25 
     26 <p>Once you've integrated this base level of functionality and thoroughly analyzed
     27 the results, you may add your own policy settings to cover your customizations
     28 to the Android operating system. Of course, these policies must still meet the <a href="/compatibility/index.html">Android Compatibility program</a> requirements and not remove the default SELinux settings.</p>
     29 
     30 <p>Manufacturers should not remove existing security settings. Otherwise, they
     31 risk breaking the Android SELinux implementation and the applications it
     32 governs. This includes third-party applications that will likely need to be
     33 improved to be compliant and operational. Applications must require no
     34 modification to continue functioning on SELinux-enabled devices.</p>
     35 
     36 <p>When embarking upon customizing SELinux, manufacturers should remember to:</p>
     37 
     38 <ul>
     39   <li>Write SELinux policy for all new daemons
     40   <li>Use predefined domains whenever appropriate
     41   <li>Assign a domain to any process spawned as an <code>init</code> service
     42   <li>Become familiar with the macros before writing policy
     43   <li>Submit changes to core policy to AOSP
     44 </ul>
     45 
     46 <p>And not to:</p>
     47 
     48 <ul>
     49   <li>Create incompatible policy
     50   <li>Allow end user policy customization
     51   <li>Allow MDM policy customizations
     52   <li>Scare users with policy violations
     53   <li>Add backdoors
     54 </ul>
     55 
     56 <p>See the <em>Kernel Security Features</em> section of the <a href="/compatibility/android-cdd.pdf">Android Compatibility Definition document</a> for specific requirements.</p>
     57 
     58 <p>SELinux uses a whitelist approach, meaning all access must be explicitly
     59 allowed in policy in order to be granted. Since Android's default SELinux
     60 policy already supports the Android Open Source Project, OEMs are not required
     61 to modify SELinux settings in any way. If they do customize SELinux settings,
     62 they should take great care not to break existing applications. Here is how we
     63 recommend proceeding:</p>
     64 
     65 <ol>
     66   <li>Use the <a href="https://android.googlesource.com/kernel/common/">latest Android kernel</a>.
     67   <li>Adopt the <a href="http://en.wikipedia.org/wiki/Principle_of_least_privilege">principle of least privilege</a>.
     68   <li>Address only your own additions to Android. The default policy works with the <a href="https://android.googlesource.com/">Android Open Source Project</a> codebase automatically.
     69   <li>Compartmentalize software components into modules that conduct singular tasks.
     70   <li>Create SELinux policies that isolate those tasks from unrelated functions.
     71   <li>Put those policies in *.te files (the extension for SELinux policy source
     72 files) within the <code>/device/manufacturer/device-name/sepolicy</code> directory and use
     73 <code>BOARD_SEPOLICY</code> variables to include them in your build.
     74   <li>Make new domains permissive initially. This is done by
     75 using a permissive declaration in the domain's .te file.
     76   <li>Analyze results and refine your domain definitions.
     77   <li>Remove the permissive declaration when no further denials appear in userdebug
     78 builds.
     79 </ol>
     80 
     81 <p>Once integrated, OEM Android development should include a step to ensure
     82 SELinux compatibility going forward. In an ideal software development process,
     83 SELinux policy changes only when the software model changes and not the actual
     84 implementation.</p>
     85 
     86 <p>As device manufacturers begin to customize SELinux, they should first audit
     87 their additions to Android. If they've added a component that conducts a new
     88 function, the manufacturers will need to ensure the component meets the
     89 security policy applied by Android, as well as any associated policy crafted by
     90 the OEM, before turning on enforcing mode.</p>
     91 
     92 <p>To prevent unnecessary issues, it is better to be overbroad and over-compatible
     93 than too restrictive and incompatible, which results in broken device
     94 functions. Conversely, if a manufacturer's changes will benefit others, it
     95 should supply the modifications to the default SELinux policy as a <a href="/source/submit-patches.html">patch</a>. If the patch is applied to the default security policy, the manufacturer will no longer need to make this change with each new Android release.</p>
     96 
     97 <h2 id=example_policy_statements>Example policy statements</h2>
     98 
     99 <p>First, note SELinux is based upon the <a href="https://www.gnu.org/software/m4/manual/index.html">M4</a> computer language and therefore supports a variety of macros to save time.</p>
    100 
    101 <p>In the following example, all domains are granted access to read from or write to <code>/dev/null</code> and read from <code>/dev/zero</code>.</p>
    102 
    103 <pre class="devsite-click-to-copy">
    104 # Allow read / write access to /dev/null
    105 allow domain null_device:chr_file { getattr open read ioctl lock append write};
    106 
    107 # Allow read-only access to /dev/zero
    108 allow domain zero_device:chr_file { getattr open read ioctl lock };
    109 </pre>
    110 
    111 
    112 <p>This same statement can be written with SELinux <code>*_file_perms</code> macros (shorthand):</p>
    113 
    114 <pre class="devsite-click-to-copy">
    115 # Allow read / write access to /dev/null
    116 allow domain null_device:chr_file rw_file_perms;
    117 
    118 # Allow read-only access to /dev/zero
    119 allow domain zero_device:chr_file r_file_perms;
    120 </pre>
    121 
    122 <h2 id=example_policy>Example policy</h2>
    123 
    124 <p>Here is a complete example policy for DHCP, which we examine below:</p>
    125 
    126 <pre class="devsite-click-to-copy">
    127 type dhcp, domain;
    128 permissive dhcp;
    129 type dhcp_exec, exec_type, file_type;
    130 type dhcp_data_file, file_type, data_file_type;
    131 
    132 init_daemon_domain(dhcp)
    133 net_domain(dhcp)
    134 
    135 allow dhcp self:capability { setgid setuid net_admin net_raw net_bind_service
    136 };
    137 allow dhcp self:packet_socket create_socket_perms;
    138 allow dhcp self:netlink_route_socket { create_socket_perms nlmsg_write };
    139 allow dhcp shell_exec:file rx_file_perms;
    140 allow dhcp system_file:file rx_file_perms;
    141 # For /proc/sys/net/ipv4/conf/*/promote_secondaries
    142 allow dhcp proc_net:file write;
    143 allow dhcp system_prop:property_service set ;
    144 unix_socket_connect(dhcp, property, init)
    145 
    146 type_transition dhcp system_data_file:{ dir file } dhcp_data_file;
    147 allow dhcp dhcp_data_file:dir create_dir_perms;
    148 allow dhcp dhcp_data_file:file create_file_perms;
    149 
    150 allow dhcp netd:fd use;
    151 allow dhcp netd:fifo_file rw_file_perms;
    152 allow dhcp netd:{ dgram_socket_class_set unix_stream_socket } { read write };
    153 allow dhcp netd:{ netlink_kobject_uevent_socket netlink_route_socket
    154 netlink_nflog_socket } { read write };
    155 </pre>
    156 
    157 <p>Lets dissect the example:</p>
    158 
    159 <p>In the first line, the type declaration, the DHCP daemon inherits from the base
    160 security policy (<code>domain</code>). From the previous statement examples, we know DHCP can read from and write
    161 to <code>/dev/null</code>.</p>
    162 
    163 <p>In the second line, DHCP is identified as a permissive domain.</p>
    164 
    165 <p>In the <code>init_daemon_domain(dhcp)</code> line, the policy states DHCP is spawned from <code>init</code> and is allowed to communicate with it.</p>
    166 
    167 <p>In the <code>net_domain(dhcp)</code> line, the policy allows DHCP to use common network functionality from the <code>net</code> domain such as reading and writing TCP packets, communicating over sockets, and conducting DNS requests.</p>
    168 
    169 <p>In the line <code>allow dhcp proc_net:file write;</code>, the policy states DHCP can write to specific files in <code>/proc</code>. This line demonstrates SELinuxs fine-grained file labeling. It uses the <code>proc_net</code> label to limit write access to only the files under <code>/proc/sys/net</code>.</p>
    170 
    171 <p>The final block of the example starting with <code>allow dhcp netd:fd use;</code> depicts how applications may be allowed to interact with one another. The
    172 policy says DHCP and netd may communicate with one another via file
    173 descriptors, FIFO files, datagram sockets, and UNIX stream sockets. DHCP may
    174 only read to and write from the datagram sockets and UNIX stream sockets and
    175 not create or open them.</p>
    176 
    177 <h2 id=available_controls>Available controls</h2>
    178 
    179 <table>
    180  <tr>
    181     <td>
    182 <p><strong>Class</strong></p>
    183 </td>
    184     <td>
    185 <p><strong>Permission</strong></p>
    186 </td>
    187  </tr>
    188  <tr>
    189     <td>
    190 <p>file</p>
    191 </td>
    192     <td>
    193 <pre>
    194 ioctl read write create getattr setattr lock relabelfrom relabelto append
    195 unlink link rename execute swapon quotaon mounton</pre>
    196 </td>
    197  </tr>
    198  <tr>
    199  <td>
    200 <p>directory</p>
    201 </td>
    202  <td>
    203 <pre>
    204 add_name remove_name reparent search rmdir open audit_access execmod</pre>
    205 </td>
    206  </tr>
    207  <tr>
    208  <td>
    209 <p>socket</p>
    210 </td>
    211  <td>
    212 <pre>
    213 ioctl read write create getattr setattr lock relabelfrom relabelto append bind
    214 connect listen accept getopt setopt shutdown recvfrom sendto recv_msg send_msg
    215 name_bind</pre>
    216 </td>
    217  </tr>
    218  <tr>
    219  <td>
    220 <p>filesystem</p>
    221 </td>
    222  <td>
    223 <pre>
    224 mount remount unmount getattr relabelfrom relabelto transition associate
    225 quotamod quotaget</pre>
    226  </td>
    227  </tr>
    228  <tr>
    229  <td>
    230 <p>process</p>
    231  </td>
    232  <td>
    233 <pre>
    234 fork transition sigchld sigkill sigstop signull signal ptrace getsched setsched
    235 getsession getpgid setpgid getcap setcap share getattr setexec setfscreate
    236 noatsecure siginh setrlimit rlimitinh dyntransition setcurrent execmem
    237 execstack execheap setkeycreate setsockcreate</pre>
    238 </td>
    239  </tr>
    240  <tr>
    241  <td>
    242 <p>security</p>
    243 </td>
    244  <td>
    245 <pre>
    246 compute_av compute_create compute_member check_context load_policy
    247 compute_relabel compute_user setenforce setbool setsecparam setcheckreqprot
    248 read_policy</pre>
    249 </td>
    250  </tr>
    251  <tr>
    252  <td>
    253 <p>capability</p>
    254 </td>
    255  <td>
    256 <pre>
    257 chown dac_override dac_read_search fowner fsetid kill setgid setuid setpcap
    258 linux_immutable net_bind_service net_broadcast net_admin net_raw ipc_lock
    259 ipc_owner sys_module sys_rawio sys_chroot sys_ptrace sys_pacct sys_admin
    260 sys_boot sys_nice sys_resource sys_time sys_tty_config mknod lease audit_write
    261 audit_control setfcap</pre>
    262 </td>
    263  </tr>
    264  <tr>
    265  <td>
    266 <p><strong>MORE</strong></p>
    267 </td>
    268  <td>
    269 <p><strong>AND MORE</strong></p>
    270 </td>
    271  </tr>
    272 </table>
    273 
    274 <h2 id=neverallow>neverallow rules</h2>
    275 
    276 <p>SELinux <code>neverallow</code> rules prohibit behavior that should never occur.
    277 With <a href="/compatibility/index.html">compatibility</a> testing,
    278 SELinux <code>neverallow</code> rules are now enforced across partner devices.</p>
    279 
    280 <p>The following guidelines are intended to help manufacturers avoid errors
    281 related to <code>neverallow</code> rules during customization. The rule numbers
    282 used here correspond to Android 5.1 and are subject to change by release.</p>
    283 
    284 <p>Rule 48: <code>neverallow { domain -debuggerd -vold -dumpstate
    285 -system_server } self:capability sys_ptrace;</code><br>
    286 See the man page for <code>ptrace</code>.  The <code>sys_ptrace</code>
    287 capability grants the ability to <code>ptrace</code> any process, which allows a great deal
    288 of control over other processes and should belong only to designated system
    289 components, outlined in the rule.  The need for this capability often indicates
    290 the presence of something that is not meant for user-facing builds or
    291 functionality that isnt needed. Remove the unnecessary component.</p>
    292 
    293 <p>Rule 76: <code>neverallow { domain -appdomain -dumpstate -shell -system_server -zygote } { file_type -system_file -exec_type }:file execute;</code><br>
    294 This rule is intended to prevent the execution of arbitrary code on the system.
    295 Specifically, it asserts that only code on <code>/system</code> gets executed,
    296 which allows security guarantees thanks to mechanisms such as verified boot.
    297 Often, the best solution when encountering a problem with this
    298 <code>neverallow</code> rule is to move the offending code to the
    299 <code>/system</code> partition.</p>
    300 
    301   </body>
    302 </html>
    303