Clay Murphy | 51dbe2d | 2013-07-22 12:54:07 -0700 | [diff] [blame] | 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 dmesg. 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’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 | <root>/device/manufacturer/device-name/sepolicy directory.</p> |
| 79 | |
| 80 | <p>Then just update your BoardConfig.mk makefile - located in the |
| 81 | <device-name> directory containing the sepolicy subdirectory - to |
| 82 | reference the sepolicy subdirectory once created, like so: <pre> |
| 83 | BOARD_SEPOLICY_DIRS := \ <root>/device/manufacturer/device-name/sepolicy |
| 84 | |
| 85 | BOARD_SEPOLICY_UNION := \ genfs_contexts \ file_contexts \ sepolicy.te </pre> |
| 86 | <p>After rebuilding your device, it is enabled with SELinux. You can now either |
| 87 | customize your SELinux policies to accommodate your own additions to the Android |
| 88 | operating system as described in the <em>Customization</em> section or verify |
| 89 | your existing setup as covered in the <em>Validation</em> section.</p> |
| 90 | |
| 91 | <h2 id="customization">Customization</h2> <p>Once you’ve integrated this |
| 92 | base level of functionality and thoroughly analyzed the results, you may add |
| 93 | your own policy settings to cover your customizations to the Android operating |
| 94 | system.</p> |
| 95 | |
| 96 | <p>Of course, these policies must still meet the <a |
| 97 | href="{@docRoot}compatibility/index.html">Android Compatibility |
| 98 | program</a> requirements and not remove the default SELinux settings.</p> |
| 99 | |
| 100 | <p>Manufacturers should not remove existing security settings. Otherwise, they |
| 101 | risk breaking the Android SELinux implementation and the applications it |
| 102 | governs. This includes third-party applications that will likely need to be |
| 103 | improved to be compliant and operational. Applications must require no |
| 104 | modification to continue functioning on SELinux-enabled devices.</p> |
| 105 | |
| 106 | <p>See section 9.7 of the Android 4.3 Compatibility Definition document for |
| 107 | specific requirements:<br><a |
| 108 | href="{@docRoot}compatibility/android-4.3-cdd.pdf">http://source.android.com/compatibility/android-4.3-cdd.pdf</a></p> |
| 109 | |
| 110 | <p>SELinux uses a whitelist approach, meaning it grants special privileges based |
| 111 | upon role. Because the default policy provided by Android is so permissive, OEMs |
| 112 | have great leeway in strengthening it. Here is how we recommend proceeding:</p> |
| 113 | |
| 114 | <ol> <li> <p>Use the <a |
| 115 | href="https://android.googlesource.com/kernel/common/">latest Android |
| 116 | kernel</a>.</p> </li> <li> <p>Adopt the <a |
| 117 | href="http://en.wikipedia.org/wiki/Principle_of_least_privilege">principle of |
| 118 | least privilege</a>.</p> </li> <li> <p>Address only your own additions to |
| 119 | Android. The default policy works with the <a |
| 120 | href="https://android.googlesource.com/">Android Open Source Project</a> |
| 121 | codebase automatically.</p> </li> <li> <p>Compartmentalize software components |
| 122 | into modules that conduct singular tasks.</p> </li> <li> <p>Create SELinux |
| 123 | policies that isolate those tasks from unrelated functions.</p> </li> <li> |
| 124 | <p>Put those policies in *.te files (the extension for SELinux policy source |
| 125 | files) within the <root>/device/manufacturer/device-name/sepolicy |
| 126 | directory.</p> </li> <li> <p>Release your SELinux implementation in permissive |
| 127 | mode first.</p> </li> <li> <p>Analyze results and refine policy settings.</p> |
| 128 | </li> </ol> |
| 129 | |
| 130 | <p>Once integrated, OEM Android development should include a step to ensure |
| 131 | SELinux compatibility going forward. In an ideal software development process, |
| 132 | SELinux policy changes only when the software model changes and not the actual |
| 133 | implementation.</p> |
| 134 | |
| 135 | <p>As device manufacturers begin to customize SELinux, they should first audit |
| 136 | their additions to Android. If you’ve added a component that conducts a |
| 137 | new function, the manufacturer will need to ensure the component meets the |
| 138 | security policy applied by Android, as well as any associated policy crafted by |
| 139 | the OEM, before turning on enforcement.</p> |
| 140 | |
| 141 | <p>To prevent unnecessary issues, it is better to be overbroad and |
| 142 | over-compatible than too restrictive and incompatible, which results in broken |
| 143 | device functions. Conversely, if a manufacturer’s changes will benefit |
| 144 | others, it should supply the modifications to the default SELinux policy as a <a |
| 145 | href="{@docRoot}source/submit-patches.html">patch</a>. If the |
| 146 | patch is applied to the default security policy, the manufacturer will no longer |
| 147 | need to make this change with each new Android release.</p> |
| 148 | |
| 149 | <h2 id="use-cases">Use Cases</h2> <p>Here are specific examples of exploits to |
| 150 | consider when crafting your own software and associated SELinux policies:</p> |
| 151 | |
| 152 | <p>Symlinks - Because symlinks appear as files, they are often read just as |
| 153 | that. This can lead to exploits. For instance, some privileged components such |
| 154 | as init change the permissions of certain files, sometimes to be excessively |
| 155 | open.</p> |
| 156 | |
| 157 | <p>Attackers might then replace those files with symlinks to code they control, |
| 158 | allowing the attacker to overwrite arbitrary files.</p> |
| 159 | |
| 160 | <p>But if you know your application will never traverse a symlink, you can |
| 161 | prohibit it from doing so with SELinux.</p> |
| 162 | |
| 163 | <p>System files - Consider the class of system files that should only be |
| 164 | modified by the system server. Still, since netd, init, and vold run as root, |
| 165 | they can access those system files. So if netd became compromised, it could |
| 166 | compromise those files and potentially the system server itself.</p> |
| 167 | |
| 168 | <p>With SELinux, you can identify those files as system server data files. |
| 169 | Therefore, the only domain that has read/write access to them is system server. |
| 170 | Even if netd became compromised, it could not switch domains to the system |
| 171 | server domain and access those system files although it runs as root.</p> |
| 172 | |
| 173 | <p>App data - Another example is the class of things that must run as root that |
| 174 | should not get to access app data. This is incredibly useful as wide-ranging |
| 175 | assertions can be made, such as certain domains unrelated to application data |
| 176 | being prohibited from accessing the internet.</p> |
| 177 | |
| 178 | <p>setattr - For commands such as chmod and chown, you could identify the |
| 179 | set of files where the associated domain can conduct setattr. Anything outside |
| 180 | of that could be prohibited from these changes, even by root. So an application |
| 181 | might run chmod and chown against those labeled app_data_files but not |
| 182 | shell_data_files or system_data_files.</p> <h2 id="related-files">Related |
| 183 | Files</h2> <p>This section serves to guide you once you’ve decided to |
| 184 | customize the SELinux policy settings. See the <em>Customization</em> section |
| 185 | for steps. We recommend device manufacturers start with the default Android |
| 186 | SELinux policy and make the minimum possible set of changes to address their |
| 187 | additions to Android. Existing Android SELinux policy files are found in the |
| 188 | root of the ~<a |
| 189 | href="https://android.googlesource.com/platform/external/sepolicy/">platform/external/sepolicy</a> |
| 190 | directory.</p> |
| 191 | |
| 192 | <p>Android upgraded its SELinux policy version to allow the SELinux mode to be |
| 193 | set to permissive on a per-domain basis. For example, if you run all of your |
| 194 | applications on a single domain, you could set that domain to be permissive and |
| 195 | then have all other functions and their domains set to enforcement. Domains are |
| 196 | associated with applications by the key used to sign each application. This |
| 197 | setting is made at the top of each SELinux policy source (*.te) file.</p> |
| 198 | |
| 199 | <p>Here are the files you must create or edit in order to customize SELinux:</p> |
| 200 | <ul> <li> <p>New SELinux policy source (*.te) files - Located in the |
| 201 | <root>/device/manufacturer/device-name/sepolicy directory These files |
| 202 | define domains and their labels. The new policy files get concatenated with the |
| 203 | existing policy files during compilation into a single SELinux kernel policy |
| 204 | file. </p> </li> <li> <p>Updated BoardConfig.mk makefile - Located in the |
| 205 | <device-name> directory containing the sepolicy subdirectory. It must be |
| 206 | updated to reference the sepolicy subdirectory once created if it wasn’t |
| 207 | in initial implementation.</p> </li> <li> <p>Updated file_contexts - Located in |
| 208 | the sepolicy subdirectory. It labels files and is managed in the userspace. As |
| 209 | you create new policies, update this file to reference them. In order to apply |
| 210 | new file_contexts, you must run restorecon on the file to be relabeled.</p> |
| 211 | </li> </ul> |
| 212 | |
| 213 | <p>The remaining files in the /sepolicy directory are either auto-generated or |
| 214 | should remain static. The policy files come in the form: allow, domain, and |
| 215 | context, for a set of actions:</p> |
| 216 | |
| 217 | <ul> <li> <p>Allow - Gives the role permission to carry out the action described |
| 218 | in the context within the specified domain.</p> </li> <li> <p>Domain - Domain |
| 219 | represents scope of the rule and gets converted to a security identifier (SID) |
| 220 | in the kernel.</p> </li> <li> <p>Context - An identifier for the rule, converted |
| 221 | to an integer in the kernel.</p> </li> </ul> |
| 222 | |
| 223 | <p>And so the an example use of this would follow the structure:</p> <p>allow |
| 224 | appdomain app_data_file:file rw_file_perms;</p> |
| 225 | |
| 226 | <p>This says an application is allowed to read and write files labeled |
| 227 | app_data_file. During compilation, those overrides are concatenated to the |
| 228 | existing SELinux settings and into a single security policy. These overrides add |
| 229 | to the base security policy rather than subtract from existing settings.</p> |
| 230 | |
| 231 | <p>Once the new policy files and BoardConfig.mk updates are in place, the new |
| 232 | policy settings get automatically uploaded to the device.</p> |
| 233 | |
| 234 | <h2 id="validation">Validation</h2> <p>Android strongly encourages OEMs to test |
| 235 | their SELinux implementations thoroughly. As manufacturers implement SELinux, |
| 236 | they should initially release their own policies in permissive mode. If |
| 237 | possible, apply the new policy to devices of employees first as a test.</p> |
| 238 | |
| 239 | <p>Once applied, make sure SELinux is running in the correct mode on the device |
| 240 | by issuing the command: getenforce</p> |
| 241 | |
| 242 | <p>This will print the SELinux mode: either Disabled, Enforcing, or Permissive. |
| 243 | If permissive, you are compliant. Enforcing is explicitly not compliant in |
| 244 | Android 4.3. (Because of its risk, enforcing mode comes with a much heavier |
| 245 | testing burden.)</p> |
| 246 | |
| 247 | <p>Then check for errors. Errors are routed as event logs to dmesg and viewable |
| 248 | locally on the device. Manufacturers should examine the SELinux output to dmesg |
| 249 | on these devices and refine settings prior to public release in permissive |
| 250 | mode.</p> |
| 251 | |
| 252 | <p>With this output, manufacturers can readily identify when system users or |
| 253 | components are in violation of SELinux policy. Manufacturers can then repair |
| 254 | this bad behavior, either by changes to the software, SELinux policy, or |
| 255 | both.</p> |
| 256 | |
| 257 | <p>Specifically, these log messages indicate what roles and processes would fail |
| 258 | under policy enforcement and why. Android is taking this information, analyzing |
| 259 | it and refining its default security policy so that it works on a wide range of |
| 260 | Android devices with little customization. With this policy, OEMs must only |
| 261 | accommodate their own changes to the Android operating system.</p> |
| 262 | |
| 263 | <p>Then run the SELinux-enabled devices through the <a |
| 264 | href="{@docRoot}compatibility/cts-intro.html">Android |
| 265 | Compatibility Test Suite</a> (CTS).</p> <p>As said, any new policies must still |
| 266 | meet the <a href="{@docRoot}compatibility/index.html">Android |
| 267 | Compatibility program</a> requirements:<br><a |
| 268 | href="{@docRoot}compatibility/android-4.3-cdd.pdf">http://source.android.com/compatibility/android-4.3-cdd.pdf</a></p> |
| 269 | |
| 270 | <p>If you run the devices through the CTS and find no errors in dmesg, you can |
| 271 | consider your SELinux implementation compatible.</p> |
| 272 | |
| 273 | <p>Finally, if possible, turn on enforcement internally (on devices of |
| 274 | employees) to raise the visibility of failures. Identify any user issues and |
| 275 | resolve them. </p> <h2 id="help">Help</h2> Device manufacturers are strongly |
| 276 | encouraged to work with their Android account managers to analyze SELinux |
| 277 | results and improve policy settings. Over time, Android intends to support |
| 278 | common manufacturer additions in its default SELinux policy. For more |
| 279 | information, contact <a |
| 280 | href="mailto:security@android.com">security@android.com</a> or Geremy Condra (<a |
| 281 | href="mailto:gcondra@google.com">gcondra@google.com</a>) directly. |