Home | History | Annotate | Download | only in security
      1 page.title=Security-Enhanced Linux 
      2 @jd:body
      3 
      4 <!--
      5     Copyright 2010 The Android Open Source Project
      6 
      7     Licensed under the Apache License, Version 2.0 (the "License");
      8     you may not use this file except in compliance with the License.
      9     You may obtain a copy of the License at
     10 
     11         http://www.apache.org/licenses/LICENSE-2.0
     12 
     13     Unless required by applicable law or agreed to in writing, software
     14     distributed under the License is distributed on an "AS IS" BASIS,
     15     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     16     See the License for the specific language governing permissions and
     17     limitations under the License.
     18 -->
     19 
     20 <h2 id="introduction">Introduction</h2> <p>In Android 4.3,
     21 Android begins supporting Security-Enhanced Linux (SELinux), a tool for applying
     22 access control policies. SELinux enhances Android security, and contributions to
     23 it have been made by a number of companies and organizations; all Android code
     24 and contributors are publicly available for review on this same site <a
     25 href="http://source.android.com/">source.android.com</a>. With SELinux, Android
     26 can better control access to application data and system logs, reduce the
     27 effects of malicious software, and protect users from potential flaws in mobile
     28 code. </p>
     29 
     30 <p>In this release, Android includes SELinux in permissive mode and a
     31 corresponding security policy that works by default across the <a
     32 href="https://android.googlesource.com/">Android Open Source Project</a>. In
     33 permissive mode, no actions are prevented. Instead, all potential violations are
     34 logged by the kernel to <code>dmesg</code>. This allows Android and Android device
     35 manufacturers to gather information about errors so they may refine their
     36 software and SELinux policies before enforcing them.</p>
     37 
     38 <h2 id="background">Background</h2> <p>Used properly, SELinux greatly limits the
     39 potential damage of compromised machines and accounts. When you adopt SELinux,
     40 you instill a structure by which software runs at only the minimum privilege
     41 level. This mitigates the effects of attacks and reduces the likelihood of
     42 errant processes overwriting or even transmitting data.</p>
     43 
     44 <p>SELinux provides a mandatory access control (MAC) umbrella over traditional
     45 discretionary access control (DAC) environments. For instance, software must
     46 typically run as the root user account to write to raw block devices. In a
     47 traditional DAC-based Linux environment, if the root user becomes compromised
     48 that user can write to every raw block device. However, SELinux can be used to
     49 label these devices so the user role assigned the root privilege can write to
     50 only those specified in the associated policy. In this way, root cannot
     51 overwrite data and system settings outside of the specific raw block device.</p>
     52 
     53 <p>See the <em>Use Cases</em> section for more examples of threats and ways to
     54 address them with SELinux.</p>
     55 
     56 <h2 id="implementation">Implementation</h2> <p>Android&rsquo;s initial SELinux
     57 implementation is launching in permissive mode - rather than the non-functional
     58 disabled mode or the most stringent enforcing mode - to act as a reference and
     59 facilitate testing and development.</p>
     60 
     61 <p>SELinux is launching in permissive mode on Android to enable the first phase
     62 of policy development, and it is accompanied by everything you need to enable
     63 SELinux now.</p>
     64 
     65 <p>You merely need to integrate the <a
     66 href="https://android.googlesource.com/kernel/common/">latest Android kernel</a>
     67 and then incorporate the files found in the ~<a
     68 href="https://android.googlesource.com/platform/external/sepolicy/">platform/external/sepolicy</a>
     69 directory:<br>
     70 <a
     71 href="https://android.googlesource.com/kernel/common/">https://android.googlesource.com/kernel/common/</a>
     72 <br>
     73 <a
     74 href="https://android.googlesource.com/platform/external/sepolicy/">https://android.googlesource.com/platform/external/sepolicy/</a></p>
     75 
     76 <p>Those files when compiled comprise the SELinux kernel security policy and
     77 cover the upstream Android operating system. Place those files within the
     78 &lt;root&gt;/device/manufacturer/device-name/sepolicy directory.</p>
     79 
     80 <p>Then just update your <code>BoardConfig.mk</code> makefile - located in the
     81 &lt;device-name&gt; directory containing the sepolicy subdirectory - to
     82 reference the sepolicy subdirectory once created, like so:</p>
     83 
     84 <pre>
     85 BOARD_SEPOLICY_DIRS := \
     86         &lt;root&gt;/device/manufacturer/device-name/sepolicy
     87 
     88 BOARD_SEPOLICY_UNION := \
     89         genfs_contexts \ 
     90         file_contexts \ 
     91         sepolicy.te 
     92 </pre>
     93 
     94 <p>After rebuilding your device, it is enabled with SELinux. You can now either
     95 customize your SELinux policies to accommodate your own additions to the Android
     96 operating system as described in the <em>Customization</em> section or verify
     97 your existing setup as covered in the <em>Validation</em> section.</p>
     98 
     99 <h2 id="customization">Customization</h2> <p>Once you&rsquo;ve integrated this
    100 base level of functionality and thoroughly analyzed the results, you may add
    101 your own policy settings to cover your customizations to the Android operating
    102 system. Of course, these policies must still meet the <a
    103 href="{@docRoot}compatibility/index.html">Android Compatibility
    104 program</a> requirements and not remove the default SELinux settings.</p>
    105 
    106 <p>Manufacturers should not remove existing security settings. Otherwise, they
    107 risk breaking the Android SELinux implementation and the applications it
    108 governs. This includes third-party applications that will likely need to be
    109 improved to be compliant and operational. Applications must require no
    110 modification to continue functioning on SELinux-enabled devices.</p>
    111 
    112 <p>See section 9.7 of the Android 4.3 Compatibility Definition document for
    113 specific requirements:<br><a
    114 href="{@docRoot}compatibility/android-4.3-cdd.pdf">http://source.android.com/compatibility/android-4.3-cdd.pdf</a></p>
    115 
    116 <p>SELinux uses a whitelist approach, meaning it grants special privileges based
    117 upon role. Because the default policy provided by Android is so permissive, OEMs
    118 have great leeway in strengthening it. Here is how we recommend proceeding:</p>
    119 
    120 <ol>
    121 <li>
    122 <p>Use the <a
    123 href="https://android.googlesource.com/kernel/common/">latest Android
    124 kernel</a>.</p> </li>
    125 <li>
    126 <p>Adopt the <a
    127 href="http://en.wikipedia.org/wiki/Principle_of_least_privilege">principle of
    128 least privilege</a>.</p></li>
    129 <li>
    130 <p>Address only your own additions to
    131 Android. The default policy works with the <a
    132 href="https://android.googlesource.com/">Android Open Source Project</a>
    133 codebase automatically.</p></li>
    134 <li>
    135 <p>Compartmentalize software components
    136 into modules that conduct singular tasks.</p></li>
    137 <li>
    138 <p>Create SELinux
    139 policies that isolate those tasks from unrelated functions.</p></li>
    140 <li>
    141 <p>Put those policies in *.te files (the extension for SELinux policy source
    142 files) within the &lt;root&gt;/device/manufacturer/device-name/sepolicy
    143 directory.</p></li>
    144 <li>
    145 <p>Release your SELinux implementation in permissive
    146 mode first.</p></li>
    147 <li><p>Analyze results and refine policy settings.</p>
    148 </li>
    149 </ol>
    150 
    151 <p>Once integrated, OEM Android development should include a step to ensure
    152 SELinux compatibility going forward. In an ideal software development process,
    153 SELinux policy changes only when the software model changes and not the actual
    154 implementation.</p>
    155 
    156 <p>As device manufacturers begin to customize SELinux, they should first audit
    157 their additions to Android. If you&rsquo;ve added a component that conducts a
    158 new function, the manufacturer will need to ensure the component meets the
    159 security policy applied by Android, as well as any associated policy crafted by
    160 the OEM, before turning on enforcement.</p>
    161 
    162 <p>To prevent unnecessary issues, it is better to be overbroad and
    163 over-compatible than too restrictive and incompatible, which results in broken
    164 device functions. Conversely, if a manufacturer&rsquo;s changes will benefit
    165 others, it should supply the modifications to the default SELinux policy as a <a
    166 href="{@docRoot}source/submit-patches.html">patch</a>. If the
    167 patch is applied to the default security policy, the manufacturer will no longer
    168 need to make this change with each new Android release.</p>
    169 
    170 <h2 id="use-cases">Use Cases</h2> <p>Here are specific examples of exploits to
    171 consider when crafting your own software and associated SELinux policies:</p>
    172 
    173 <p><strong>Symlinks</strong> - Because symlinks appear as files, they are often read 
    174 just as that. This can lead to exploits. For instance, some privileged components such
    175 as init change the permissions of certain files, sometimes to be excessively
    176 open.</p>
    177 
    178 <p>Attackers might then replace those files with symlinks to code they control,
    179 allowing the attacker to overwrite arbitrary files. But if you know your application 
    180 will never traverse a symlink, you can prohibit it from doing so with SELinux.</p>
    181 
    182 <p><strong>System files</strong> - Consider the class of system files that should only be
    183 modified by the system server. Still, since <code>netd</code>, <code>init</code>, 
    184 and <code>vold</code> run as root, they can access those system files. So if 
    185 <code>netd</code> became compromised, it could compromise those files and 
    186 potentially the system server itself.</p>
    187 
    188 <p>With SELinux, you can identify those files as system server data files.
    189 Therefore, the only domain that has read/write access to them is system server.
    190 Even if <code>netd</code> became compromised, it could not switch domains to the 
    191 system server domain and access those system files although it runs as root.</p>
    192 
    193 <p><strong>App data</strong> - Another example is the class of functions that
    194 must run as root but should not get to access app data. This is incredibly useful as 
    195 wide-ranging assertions can be made, such as certain domains unrelated to application data
    196 being prohibited from accessing the internet.</p>
    197 
    198 <p><strong>setattr</strong> - For commands such as <code>chmod</code> and
    199 <code>chown</code>, you could identify the set of files where the associated domain 
    200 can conduct <code>setattr</code>. Anything outside of that could be prohibited from 
    201 these changes, even by root. So an application might run <code>chmod</code> and
    202 <code>chown</code> against those labeled app_data_files but not shell_data_files or 
    203 system_data_files.</p> <h2 id="related-files">Related
    204 Files</h2> <p>This section serves to guide you once you&rsquo;ve decided to
    205 customize the SELinux policy settings. See the <em>Customization</em> section
    206 for steps. We recommend device manufacturers start with the default Android
    207 SELinux policy and make the minimum possible set of changes to address their
    208 additions to Android. Existing Android SELinux policy files are found in the
    209 root of the ~<a
    210 href="https://android.googlesource.com/platform/external/sepolicy/">platform/external/sepolicy</a>
    211 directory.</p>
    212 
    213 <p>Android upgraded its SELinux policy version to allow the SELinux mode to be
    214 set to permissive on a per-domain basis. For example, if you run all of your
    215 applications on a single domain, you could set that domain to be permissive and
    216 then have all other functions and their domains set to enforcement. Domains are
    217 associated with applications by the key used to sign each application. This
    218 setting is made at the top of each SELinux policy source (*.te) file.</p>
    219 
    220 <p>Here are the files you must create or edit in order to customize SELinux:</p>
    221 <ul> 
    222 <li>
    223 <p><em>New SELinux policy source (*.te) files</em> - Located in the
    224 &lt;root&gt;/device/manufacturer/device-name/sepolicy directory These files
    225 define domains and their labels. The new policy files get concatenated with the
    226 existing policy files during compilation into a single SELinux kernel policy
    227 file.</p></li> 
    228 <li>
    229 <p><em>Updated <code>BoardConfig.mk</code> makefile</em> - Located in the
    230 &lt;device-name&gt; directory containing the sepolicy subdirectory. It must be
    231 updated to reference the sepolicy subdirectory once created if it wasn&rsquo;t
    232 in initial implementation.</p> </li> 
    233 <li>
    234 <p><em>Updated <code>file_contexts</code></em> - Located in
    235 the sepolicy subdirectory. It labels files and is managed in the userspace. As
    236 you create new policies, update this file to reference them. In order to apply
    237 new <code>file_contexts</code>, you must run <code>restorecon</code> on the file
    238 to be relabeled.</p>
    239 </li> </ul>
    240 
    241 <p>The remaining files in the sepolicy directory are either auto-generated or
    242 should remain static. The policy files come in the form: allow, domain, and
    243 context, for a set of actions:</p>
    244 
    245 <ul>
    246 <li>
    247 <p><em>Allow</em> - Gives the role permission to carry out the action described
    248 in the context within the specified domain.</p> </li>
    249 <li>
    250 <p><em>Domain</em> - Domain
    251 represents scope of the rule and gets converted to a security identifier (SID)
    252 in the kernel.</p> </li>
    253 <li>
    254 <p><em>Context</em> - An identifier for the rule, this is converted
    255 to an integer in the kernel.</p> </li> </ul>
    256 
    257 <p>And so the an example use of this would follow the structure:<br>
    258 <code>allow appdomain app_data_file:file rw_file_perms;</code></p>
    259 
    260 <p>This says an application is allowed to read and write files labeled
    261 app_data_file. During compilation, those overrides are concatenated to the
    262 existing SELinux settings and into a single security policy. These overrides add
    263 to the base security policy rather than subtract from existing settings.</p>
    264 
    265 <p>Once the new policy files and <code>BoardConfig.mk</code> updates are in place, the new
    266 policy settings get automatically uploaded to the device.</p>
    267 
    268 <h2 id="validation">Validation</h2> <p>Android strongly encourages OEMs to test
    269 their SELinux implementations thoroughly. As manufacturers implement SELinux,
    270 they should initially release their own policies in permissive mode. If
    271 possible, apply the new policy to devices of employees first as a test.</p>
    272 
    273 <p>Once applied, make sure SELinux is running in the correct mode on the device
    274 by issuing the command: <code>getenforce</code></p>
    275 
    276 <p>This will print the SELinux mode: either Disabled, Enforcing, or Permissive.
    277 If permissive, you are compliant. Enforcing is explicitly not compliant in
    278 Android 4.3. (Because of its risk, enforcing mode comes with a much heavier
    279 testing burden.)</p>
    280 
    281 <p>Then check for errors. Errors are routed as event logs to <code>dmesg</code> 
    282 and viewable locally on the device. Manufacturers should examine the SELinux output 
    283 to <code>dmesg</code> on these devices and refine settings prior to public release in 
    284 permissive mode.</p>
    285 
    286 <p>With this output, manufacturers can readily identify when system users or
    287 components are in violation of SELinux policy. Manufacturers can then repair
    288 this bad behavior, either by changes to the software, SELinux policy, or
    289 both.</p>
    290 
    291 <p>Specifically, these log messages indicate what roles and processes would fail
    292 under policy enforcement and why. Android is taking this information, analyzing
    293 it and refining its default security policy so that it works on a wide range of
    294 Android devices with little customization. With this policy, OEMs must only
    295 accommodate their own changes to the Android operating system.</p>
    296 
    297 <p>Then run the SELinux-enabled devices through the <a
    298 href="{@docRoot}compatibility/cts-intro.html">Android
    299 Compatibility Test Suite</a> (CTS).</p> <p>As said, any new policies must still
    300 meet the <a href="{@docRoot}compatibility/index.html">Android
    301 Compatibility program</a> requirements:<br><a
    302 href="{@docRoot}compatibility/android-4.3-cdd.pdf">http://source.android.com/compatibility/android-4.3-cdd.pdf</a></p>
    303 
    304 <p>If you run the devices through the CTS and find no errors in
    305 <code>dmesg</code>, you can consider your SELinux implementation compatible.</p>
    306 
    307 <p>Finally, if possible, turn on enforcement internally (on devices of
    308 employees) to raise the visibility of failures. Identify any user issues and
    309 resolve them.  </p> <h2 id="help">Help</h2> Device manufacturers are strongly
    310 encouraged to work with their Android account managers to analyze SELinux
    311 results and improve policy settings. Over time, Android intends to support
    312 common manufacturer additions in its default SELinux policy. For more
    313 information, contact <a
    314 href="mailto:security (a] android.com">security (a] android.com</a> or Geremy Condra (<a
    315 href="mailto:gcondra (a] google.com">gcondra (a] google.com</a>) directly.
    316