Clay Murphy | 882fa88 | 2013-10-18 16:43:40 -0700 | [diff] [blame] | 1 | page.title=Validating Security-Enhanced Linux in Android |
Clay Murphy | 51dbe2d | 2013-07-22 12:54:07 -0700 | [diff] [blame] | 2 | @jd:body |
| 3 | |
| 4 | <!-- |
Stephen Smalley | d330d3e | 2014-03-14 15:28:51 -0400 | [diff] [blame^] | 5 | Copyright 2014 The Android Open Source Project |
Clay Murphy | 51dbe2d | 2013-07-22 12:54:07 -0700 | [diff] [blame] | 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 | --> |
Clay Murphy | 882fa88 | 2013-10-18 16:43:40 -0700 | [diff] [blame] | 19 | <div id="qv-wrapper"> |
| 20 | <div id="qv"> |
| 21 | <h2>In this document</h2> |
| 22 | <ol id="auto-toc"> |
| 23 | </ol> |
| 24 | </div> |
| 25 | </div> |
Clay Murphy | 51dbe2d | 2013-07-22 12:54:07 -0700 | [diff] [blame] | 26 | |
Clay Murphy | 882fa88 | 2013-10-18 16:43:40 -0700 | [diff] [blame] | 27 | <h2 id="introduction">Introduction</h2> |
| 28 | <p> |
| 29 | As part of the Android <a href="{@docRoot}devices/tech/security/index.html">security |
Stephen Smalley | d330d3e | 2014-03-14 15:28:51 -0400 | [diff] [blame^] | 30 | model</a>, Android uses Security-Enhanced Linux (SELinux) to enforce Mandatory |
| 31 | Access Control (MAC) over all processes, even processes running |
| 32 | with root/superuser privileges (a.k.a. Linux capabilities). SELinux enhances |
| 33 | Android security, and contributions to it have been made by a number of |
| 34 | companies and organizations; all Android code and contributors are publicly |
| 35 | available for review on |
| 36 | <a href="https://android.googlesource.com/">android.googlesource.com</a>. With |
| 37 | SELinux, Android can better protect and confine system services, control access |
| 38 | to application data and system logs, reduce the effects of malicious software, |
| 39 | and protect users from potential flaws in code on mobile devices. |
Clay Murphy | 882fa88 | 2013-10-18 16:43:40 -0700 | [diff] [blame] | 40 | </p> |
| 41 | <p> |
| 42 | Android includes SELinux in enforcing mode and a corresponding security policy |
| 43 | that works by default across the <a |
| 44 | href="https://android.googlesource.com/">Android Open Source |
| 45 | Project</a>. In enforcing mode, illegitimate |
| 46 | actions are prevented and all potential violations are logged by the kernel to |
Stephen Smalley | d330d3e | 2014-03-14 15:28:51 -0400 | [diff] [blame^] | 47 | <code>dmesg</code>. Android device manufacturers should gather information about |
| 48 | errors so they may refine their software and SELinux policies before enforcing |
| 49 | them. |
Clay Murphy | 882fa88 | 2013-10-18 16:43:40 -0700 | [diff] [blame] | 50 | </p> |
Clay Murphy | 51dbe2d | 2013-07-22 12:54:07 -0700 | [diff] [blame] | 51 | |
Clay Murphy | 882fa88 | 2013-10-18 16:43:40 -0700 | [diff] [blame] | 52 | <h2 id="background">Background</h2> |
| 53 | <p> |
Stephen Smalley | d330d3e | 2014-03-14 15:28:51 -0400 | [diff] [blame^] | 54 | SELinux can operate in one of two global modes: permissive mode, in |
| 55 | which permission denials are logged but not enforced, and enforcing |
| 56 | mode, in which permission denials are both logged and |
| 57 | enforced. SELinux also supports a per-domain permissive mode in which |
| 58 | specific domains (processes) can be made permissive while placing the |
| 59 | rest of the system in global enforcing mode. A domain is simply a |
| 60 | label identifying a process or set of processes in the security |
| 61 | policy, where all processes labeled with the same domain are treated |
| 62 | identically by the security policy. Per-domain permissive mode enables |
| 63 | incremental application of SELinux to an ever-increasing portion of |
| 64 | the system. Per-domain permissive mode also enables policy |
| 65 | development for new services while keeping the rest of the system |
| 66 | enforcing. |
Clay Murphy | 882fa88 | 2013-10-18 16:43:40 -0700 | [diff] [blame] | 67 | </p> |
Stephen Smalley | d330d3e | 2014-03-14 15:28:51 -0400 | [diff] [blame^] | 68 | |
Clay Murphy | 882fa88 | 2013-10-18 16:43:40 -0700 | [diff] [blame] | 69 | <p> |
Stephen Smalley | d330d3e | 2014-03-14 15:28:51 -0400 | [diff] [blame^] | 70 | In Android 4.3, SELinux was fully permissive. In Android 4.4, SELinux |
| 71 | was made enforcing for the domains for several root processes: |
| 72 | <code>installd</code>, <code>netd</code>, <code>vold</code> and |
| 73 | <code>zygote</code>. <em>All other processes, including other |
| 74 | services and all apps, remain in permissive mode to allow further |
| 75 | evaluation and prevent failures in Android 4.4. Still, an errant |
| 76 | application could trigger an action in a root process that is not |
| 77 | allowed, thereby causing the process or the application to crash.</em> |
Clay Murphy | 882fa88 | 2013-10-18 16:43:40 -0700 | [diff] [blame] | 78 | </p> |
| 79 | <p> |
| 80 | For this reason, device manufacturers should retain the default settings |
Stephen Smalley | d330d3e | 2014-03-14 15:28:51 -0400 | [diff] [blame^] | 81 | provided by Android and limit enforcing mode to system services only until |
Clay Murphy | 882fa88 | 2013-10-18 16:43:40 -0700 | [diff] [blame] | 82 | they've resolved issues reported in dmesg. That said, device manufacturers may |
| 83 | need to augment their SELinux implementation to account for their additions and |
| 84 | other changes to the operating system. See the <em>Customization</em> section for |
| 85 | instructions. |
| 86 | </p> |
Clay Murphy | 51dbe2d | 2013-07-22 12:54:07 -0700 | [diff] [blame] | 87 | |
Clay Murphy | 882fa88 | 2013-10-18 16:43:40 -0700 | [diff] [blame] | 88 | <h2 id="mac">Mandatory access control</h2> |
| 89 | <p> |
| 90 | In conjunction with other Android security measures, Android's access control |
| 91 | policy greatly limits the potential damage of compromised |
| 92 | machines and accounts. Using tools like Android's discretionary and mandatory |
| 93 | access controls gives you a structure to ensure your software runs |
| 94 | only at the minimum privilege level. This mitigates the effects of |
| 95 | attacks and reduces the likelihood of errant processes overwriting or even |
| 96 | transmitting data. |
| 97 | </p> |
| 98 | <p> |
Stephen Smalley | d330d3e | 2014-03-14 15:28:51 -0400 | [diff] [blame^] | 99 | Starting in Android 4.3, SELinux provides a mandatory access control (MAC) |
| 100 | umbrella over traditional discretionary access control (DAC) environments. |
| 101 | For instance, software must typically run as the root user account to write |
| 102 | to raw block devices. In a traditional DAC-based Linux environment, if the root |
| 103 | user becomes compromised that user can write to every raw block device. However, |
| 104 | SELinux can be used to label these devices so the process assigned the root |
| 105 | privilege can write to only those specified in the associated policy. In this |
| 106 | way, the process cannot overwrite data and system settings outside of the |
| 107 | specific raw block device. |
Clay Murphy | 882fa88 | 2013-10-18 16:43:40 -0700 | [diff] [blame] | 108 | </p> |
| 109 | <p> |
Stephen Smalley | d330d3e | 2014-03-14 15:28:51 -0400 | [diff] [blame^] | 110 | See the <em>Use Cases</em> section for more examples of threats and ways to |
| 111 | address them with SELinux. |
Clay Murphy | 882fa88 | 2013-10-18 16:43:40 -0700 | [diff] [blame] | 112 | </p> |
Clay Murphy | 51dbe2d | 2013-07-22 12:54:07 -0700 | [diff] [blame] | 113 | |
Clay Murphy | 882fa88 | 2013-10-18 16:43:40 -0700 | [diff] [blame] | 114 | <h2 id="implementation">Implementation</h2> |
| 115 | <p> |
| 116 | Android's SELinux implementation is in enforcing mode - rather than the |
| 117 | non-functional disabled mode or the notification-only permissive mode - to act |
| 118 | as a reference and facilitate testing and development. Although enforcing mode |
| 119 | is set globally, please remember this can be overridden on a per-domain basis |
| 120 | as is in the case of the application domain. |
| 121 | </p> |
| 122 | <p> |
| 123 | SELinux for Android is accompanied by everything you need to enable SELinux |
| 124 | now. You merely need to integrate the <a |
| 125 | href="https://android.googlesource.com/kernel/common/">latest Android |
| 126 | kernel</a> and then incorporate the files found in the |
Clay Murphy | 51dbe2d | 2013-07-22 12:54:07 -0700 | [diff] [blame] | 127 | <a |
Stephen Smalley | d330d3e | 2014-03-14 15:28:51 -0400 | [diff] [blame^] | 128 | href="https://android.googlesource.com/platform/external/sepolicy/"> |
| 129 | external/sepolicy</a> directory:<br/> |
| 130 | <a |
| 131 | href="https://android.googlesource.com/kernel/common/"> |
| 132 | https://android.googlesource.com/kernel/common/</a> |
Clay Murphy | 882fa88 | 2013-10-18 16:43:40 -0700 | [diff] [blame] | 133 | <br/> |
Clay Murphy | 51dbe2d | 2013-07-22 12:54:07 -0700 | [diff] [blame] | 134 | <a |
Stephen Smalley | d330d3e | 2014-03-14 15:28:51 -0400 | [diff] [blame^] | 135 | href="https://android.googlesource.com/platform/external/sepolicy/"> |
| 136 | https://android.googlesource.com/platform/external/sepolicy/</a> |
Clay Murphy | 882fa88 | 2013-10-18 16:43:40 -0700 | [diff] [blame] | 137 | </p> |
Clay Murphy | 51dbe2d | 2013-07-22 12:54:07 -0700 | [diff] [blame] | 138 | |
Stephen Smalley | d330d3e | 2014-03-14 15:28:51 -0400 | [diff] [blame^] | 139 | <p> |
Clay Murphy | 882fa88 | 2013-10-18 16:43:40 -0700 | [diff] [blame] | 140 | Those files when compiled comprise the SELinux kernel security policy and cover |
Stephen Smalley | d330d3e | 2014-03-14 15:28:51 -0400 | [diff] [blame^] | 141 | the upstream Android operating system. You should not need to modify |
| 142 | the <root>external/sepolicy</root> files directly. Instead, add your own |
| 143 | device-specific policy files within the |
| 144 | <root>/device/manufacturer/device-name/sepolicy directory. |
| 145 | </p> |
| 146 | |
| 147 | <p> |
| 148 | Then just update your <code>BoardConfig.mk</code> makefile - located in the |
| 149 | <device-name> directory containing the sepolicy subdirectory - to reference the |
| 150 | sepolicy subdirectory and any policy file once created, as shown below. The |
| 151 | BOARD_SEPOLICY variables and their meaning is documented in the |
| 152 | external/sepolicy/README file. |
Clay Murphy | 882fa88 | 2013-10-18 16:43:40 -0700 | [diff] [blame] | 153 | </p> |
Clay Murphy | 51dbe2d | 2013-07-22 12:54:07 -0700 | [diff] [blame] | 154 | |
Clay Murphy | 15c58c4 | 2013-07-23 11:54:42 -0700 | [diff] [blame] | 155 | <pre> |
Nick Kralevich | 4d1a350 | 2014-01-22 16:17:10 -0800 | [diff] [blame] | 156 | BOARD_SEPOLICY_DIRS += \ |
Clay Murphy | 15c58c4 | 2013-07-23 11:54:42 -0700 | [diff] [blame] | 157 | <root>/device/manufacturer/device-name/sepolicy |
| 158 | |
Nick Kralevich | 4d1a350 | 2014-01-22 16:17:10 -0800 | [diff] [blame] | 159 | BOARD_SEPOLICY_UNION += \ |
Stephen Smalley | d330d3e | 2014-03-14 15:28:51 -0400 | [diff] [blame^] | 160 | genfs_contexts \ |
| 161 | file_contexts \ |
| 162 | sepolicy.te |
Clay Murphy | 15c58c4 | 2013-07-23 11:54:42 -0700 | [diff] [blame] | 163 | </pre> |
| 164 | |
Clay Murphy | 882fa88 | 2013-10-18 16:43:40 -0700 | [diff] [blame] | 165 | <p> |
| 166 | After rebuilding your device, it is enabled with SELinux. You can now either |
Clay Murphy | 51dbe2d | 2013-07-22 12:54:07 -0700 | [diff] [blame] | 167 | customize your SELinux policies to accommodate your own additions to the Android |
| 168 | operating system as described in the <em>Customization</em> section or verify |
Stephen Smalley | d330d3e | 2014-03-14 15:28:51 -0400 | [diff] [blame^] | 169 | your existing setup as covered in the <em>Validation</em> section. |
Clay Murphy | 882fa88 | 2013-10-18 16:43:40 -0700 | [diff] [blame] | 170 | </p> |
Clay Murphy | 51dbe2d | 2013-07-22 12:54:07 -0700 | [diff] [blame] | 171 | |
Clay Murphy | 882fa88 | 2013-10-18 16:43:40 -0700 | [diff] [blame] | 172 | <h2 id="customization">Customization</h2> |
| 173 | <p> |
| 174 | Once you've integrated this base level of functionality and thoroughly analyzed |
| 175 | the results, you may add your own policy settings to cover your customizations |
| 176 | to the Android operating system. Of course, these policies must still meet the |
| 177 | <a href="http://source.android.com/compatibility/index.html">Android |
| 178 | Compatibility |
| 179 | program</a> requirements and |
| 180 | not remove the default SELinux settings. |
| 181 | </p> |
| 182 | <p> |
| 183 | Manufacturers should not remove existing security settings. Otherwise, they risk |
| 184 | breaking the Android SELinux implementation and the applications it governs. |
| 185 | This includes third-party applications that will likely need to be improved to |
| 186 | be compliant and operational. Applications must require no modification to |
| 187 | continue functioning on SELinux-enabled devices. |
| 188 | </p> |
| 189 | <p> |
| 190 | See the <em>Kernel Security Features</em> section of the Android Compatibility |
| 191 | Definition document for specific requirements:<br/> |
| 192 | <a |
Stephen Smalley | d330d3e | 2014-03-14 15:28:51 -0400 | [diff] [blame^] | 193 | href="http://source.android.com/compatibility/index.html"> |
| 194 | http://source.android.com/compatibility/index.html</a> |
Clay Murphy | 882fa88 | 2013-10-18 16:43:40 -0700 | [diff] [blame] | 195 | </p> |
| 196 | <p> |
Stephen Smalley | d330d3e | 2014-03-14 15:28:51 -0400 | [diff] [blame^] | 197 | SELinux uses a whitelist approach, meaning all access must be explicitly allowed |
| 198 | in policy in order to be granted. Since Android's default SELinux policy already |
| 199 | supports the Android Open Source Project, OEMs are not required to modify |
| 200 | SELinux settings in any way. If they do customize SELinux settings, they should |
| 201 | take great care not to break existing applications. Here is how we recommend |
| 202 | proceeding: |
Clay Murphy | 882fa88 | 2013-10-18 16:43:40 -0700 | [diff] [blame] | 203 | </p> |
Clay Murphy | 51dbe2d | 2013-07-22 12:54:07 -0700 | [diff] [blame] | 204 | |
Clay Murphy | 15c58c4 | 2013-07-23 11:54:42 -0700 | [diff] [blame] | 205 | <ol> |
Clay Murphy | 882fa88 | 2013-10-18 16:43:40 -0700 | [diff] [blame] | 206 | <li>Use the <a href="https://android.googlesource.com/kernel/common/">latest |
| 207 | Android |
| 208 | kernel</a>.</li> |
| 209 | <li>Adopt the <a |
Clay Murphy | 51dbe2d | 2013-07-22 12:54:07 -0700 | [diff] [blame] | 210 | href="http://en.wikipedia.org/wiki/Principle_of_least_privilege">principle of |
Clay Murphy | 882fa88 | 2013-10-18 16:43:40 -0700 | [diff] [blame] | 211 | least |
| 212 | privilege</a>.</li> |
| 213 | <li>Address only your own additions to Android. The default policy works with |
| 214 | the |
| 215 | <a href="https://android.googlesource.com/">Android Open Source Project</a> |
| 216 | codebase |
| 217 | automatically.</li> |
| 218 | <li>Compartmentalize software components into modules that conduct singular |
| 219 | tasks.</li> |
| 220 | <li>Create SELinux policies that isolate those tasks from unrelated |
| 221 | functions.</li> |
| 222 | <li>Put those policies in *.te files (the extension for SELinux policy source |
| 223 | files) within the <root>/device/manufacturer/device-name/sepolicy |
Stephen Smalley | d330d3e | 2014-03-14 15:28:51 -0400 | [diff] [blame^] | 224 | directory and use BOARD_SEPOLICY variables to include them in your build.</li> |
| 225 | <li>Make new domains permissive initially. In Android 4.4 and earlier, this |
| 226 | is done using a permissive declaration. In later versions of Android, |
| 227 | per-domain permissive mode is specified using the permissive_or_unconfined() |
| 228 | macro.</li> |
| 229 | <li>Analyze results and refine your domain definitions.</li> |
| 230 | <li>Remove the permissive declaration when no further denials appear |
| 231 | in userdebug builds.</li> |
Clay Murphy | 15c58c4 | 2013-07-23 11:54:42 -0700 | [diff] [blame] | 232 | </ol> |
Clay Murphy | 51dbe2d | 2013-07-22 12:54:07 -0700 | [diff] [blame] | 233 | |
Clay Murphy | 882fa88 | 2013-10-18 16:43:40 -0700 | [diff] [blame] | 234 | <p> |
| 235 | Once integrated, OEM Android development should include a step to ensure |
| 236 | SELinux |
| 237 | compatibility going forward. In an ideal software development process, SELinux |
| 238 | policy changes only when the software model changes and not the actual |
| 239 | implementation. |
| 240 | </p> |
| 241 | <p> |
| 242 | As device manufacturers begin to customize SELinux, they should first audit |
| 243 | their additions to Android. If they've added a component that conducts a new |
| 244 | function, the manufacturers will need to ensure the component meets the security |
| 245 | policy applied by Android, as well as any associated policy crafted by the OEM, |
Stephen Smalley | d330d3e | 2014-03-14 15:28:51 -0400 | [diff] [blame^] | 246 | before turning on enforcing mode. |
Clay Murphy | 882fa88 | 2013-10-18 16:43:40 -0700 | [diff] [blame] | 247 | </p> |
| 248 | <p> |
| 249 | To prevent unnecessary issues, it is better to be overbroad and over-compatible |
| 250 | than too restrictive and incompatible, which results in broken device functions. |
| 251 | Conversely, if a manufacturer's changes will benefit others, it should supply |
| 252 | the modifications to the default SELinux policy as a |
| 253 | <a href="http://source.android.com/source/submit-patches.html">patch</a>. If the |
| 254 | patch is |
| 255 | applied to the default security policy, the manufacturer will no longer need to |
| 256 | make this change with each new Android release. |
| 257 | </p> |
Clay Murphy | 51dbe2d | 2013-07-22 12:54:07 -0700 | [diff] [blame] | 258 | |
| 259 | <h2 id="use-cases">Use Cases</h2> <p>Here are specific examples of exploits to |
| 260 | consider when crafting your own software and associated SELinux policies:</p> |
| 261 | |
Stephen Smalley | d330d3e | 2014-03-14 15:28:51 -0400 | [diff] [blame^] | 262 | <p><strong>Symlinks</strong> - Because symlinks appear as files, they are often |
| 263 | read just as that. This can lead to exploits. For instance, some privileged |
| 264 | components such as <code>init</code> change the permissions of certain files, |
| 265 | sometimes to be excessively open.</p> |
Clay Murphy | 51dbe2d | 2013-07-22 12:54:07 -0700 | [diff] [blame] | 266 | |
| 267 | <p>Attackers might then replace those files with symlinks to code they control, |
Stephen Smalley | d330d3e | 2014-03-14 15:28:51 -0400 | [diff] [blame^] | 268 | allowing the attacker to overwrite arbitrary files. But if you know your |
| 269 | application will never traverse a symlink, you can prohibit it from doing so |
| 270 | with SELinux.</p> |
Clay Murphy | 51dbe2d | 2013-07-22 12:54:07 -0700 | [diff] [blame] | 271 | |
Stephen Smalley | d330d3e | 2014-03-14 15:28:51 -0400 | [diff] [blame^] | 272 | <p><strong>System files</strong> - Consider the class of system files that |
| 273 | should only be modified by the system server. Still, since <code>netd</code>, |
| 274 | <code>init</code>, and <code>vold</code> run as root, they can access those |
| 275 | system files. So if <code>netd</code> became compromised, it could compromise |
| 276 | those files and potentially the system server itself.</p> |
Clay Murphy | 51dbe2d | 2013-07-22 12:54:07 -0700 | [diff] [blame] | 277 | |
| 278 | <p>With SELinux, you can identify those files as system server data files. |
| 279 | Therefore, the only domain that has read/write access to them is system server. |
Stephen Smalley | d330d3e | 2014-03-14 15:28:51 -0400 | [diff] [blame^] | 280 | Even if <code>netd</code> became compromised, it could not switch domains to the |
Clay Murphy | 15c58c4 | 2013-07-23 11:54:42 -0700 | [diff] [blame] | 281 | system server domain and access those system files although it runs as root.</p> |
Clay Murphy | 51dbe2d | 2013-07-22 12:54:07 -0700 | [diff] [blame] | 282 | |
Clay Murphy | 15c58c4 | 2013-07-23 11:54:42 -0700 | [diff] [blame] | 283 | <p><strong>App data</strong> - Another example is the class of functions that |
Stephen Smalley | d330d3e | 2014-03-14 15:28:51 -0400 | [diff] [blame^] | 284 | must run as root but should not get to access app data. This is incredibly |
| 285 | useful as wide-ranging assertions can be made, such as certain domains |
| 286 | unrelated to application data being prohibited from accessing the internet.</p> |
Clay Murphy | 51dbe2d | 2013-07-22 12:54:07 -0700 | [diff] [blame] | 287 | |
Clay Murphy | 15c58c4 | 2013-07-23 11:54:42 -0700 | [diff] [blame] | 288 | <p><strong>setattr</strong> - For commands such as <code>chmod</code> and |
Stephen Smalley | d330d3e | 2014-03-14 15:28:51 -0400 | [diff] [blame^] | 289 | <code>chown</code>, you could identify the set of files where the associated |
| 290 | domain can conduct <code>setattr</code>. Anything outside of that could be |
| 291 | prohibited from these changes, even by root. So an application might run |
| 292 | <code>chmod</code> and <code>chown</code> against those labeled app_data_files |
| 293 | but not shell_data_files or system_data_files.</p> |
| 294 | |
| 295 | <h2 id="related-files">Related Files</h2> |
| 296 | <p>This section serves to guide you once you’ve decided to |
Clay Murphy | 51dbe2d | 2013-07-22 12:54:07 -0700 | [diff] [blame] | 297 | customize the SELinux policy settings. See the <em>Customization</em> section |
| 298 | for steps. We recommend device manufacturers start with the default Android |
| 299 | SELinux policy and make the minimum possible set of changes to address their |
| 300 | additions to Android. Existing Android SELinux policy files are found in the |
Stephen Smalley | d330d3e | 2014-03-14 15:28:51 -0400 | [diff] [blame^] | 301 | root of the <a |
| 302 | href="https://android.googlesource.com/platform/external/sepolicy/"> |
| 303 | external/sepolicy</a> directory.</p> |
Clay Murphy | 51dbe2d | 2013-07-22 12:54:07 -0700 | [diff] [blame] | 304 | |
| 305 | <p>Android upgraded its SELinux policy version to allow the SELinux mode to be |
| 306 | set to permissive on a per-domain basis. For example, if you run all of your |
Stephen Smalley | d330d3e | 2014-03-14 15:28:51 -0400 | [diff] [blame^] | 307 | applications in a single domain, you could set that domain to be permissive and |
| 308 | then have all other functions and their domains set to enforcing. Domains are |
| 309 | associated with applications by the key used to sign each application. The |
| 310 | mapping of app certificates to domains is specified via the |
| 311 | mac_permissions.xml and seapp_contexts configuration files.</p> |
Clay Murphy | 51dbe2d | 2013-07-22 12:54:07 -0700 | [diff] [blame] | 312 | |
| 313 | <p>Here are the files you must create or edit in order to customize SELinux:</p> |
Stephen Smalley | d330d3e | 2014-03-14 15:28:51 -0400 | [diff] [blame^] | 314 | <ul> |
Clay Murphy | 15c58c4 | 2013-07-23 11:54:42 -0700 | [diff] [blame] | 315 | <li> |
| 316 | <p><em>New SELinux policy source (*.te) files</em> - Located in the |
Clay Murphy | 51dbe2d | 2013-07-22 12:54:07 -0700 | [diff] [blame] | 317 | <root>/device/manufacturer/device-name/sepolicy directory These files |
| 318 | define domains and their labels. The new policy files get concatenated with the |
| 319 | existing policy files during compilation into a single SELinux kernel policy |
Clay Murphy | 882fa88 | 2013-10-18 16:43:40 -0700 | [diff] [blame] | 320 | file.</p> |
| 321 | <p><strong>Important</strong>:Do not alter the app.te file provided by the |
Stephen Smalley | d330d3e | 2014-03-14 15:28:51 -0400 | [diff] [blame^] | 322 | Android Open Source Project. Doing so risks breaking all third-party |
| 323 | applications. |
Clay Murphy | 882fa88 | 2013-10-18 16:43:40 -0700 | [diff] [blame] | 324 | </p> |
| 325 | </li> |
Clay Murphy | 15c58c4 | 2013-07-23 11:54:42 -0700 | [diff] [blame] | 326 | <li> |
| 327 | <p><em>Updated <code>BoardConfig.mk</code> makefile</em> - Located in the |
Clay Murphy | 51dbe2d | 2013-07-22 12:54:07 -0700 | [diff] [blame] | 328 | <device-name> directory containing the sepolicy subdirectory. It must be |
| 329 | updated to reference the sepolicy subdirectory once created if it wasn’t |
Stephen Smalley | d330d3e | 2014-03-14 15:28:51 -0400 | [diff] [blame^] | 330 | in initial implementation.</p> </li> |
Clay Murphy | 15c58c4 | 2013-07-23 11:54:42 -0700 | [diff] [blame] | 331 | <li> |
| 332 | <p><em>Updated <code>file_contexts</code></em> - Located in |
Clay Murphy | 51dbe2d | 2013-07-22 12:54:07 -0700 | [diff] [blame] | 333 | the sepolicy subdirectory. It labels files and is managed in the userspace. As |
| 334 | you create new policies, update this file to reference them. In order to apply |
Clay Murphy | 15c58c4 | 2013-07-23 11:54:42 -0700 | [diff] [blame] | 335 | new <code>file_contexts</code>, you must run <code>restorecon</code> on the file |
| 336 | to be relabeled.</p> |
Clay Murphy | 51dbe2d | 2013-07-22 12:54:07 -0700 | [diff] [blame] | 337 | </li> </ul> |
| 338 | |
Clay Murphy | 15c58c4 | 2013-07-23 11:54:42 -0700 | [diff] [blame] | 339 | <p>The remaining files in the sepolicy directory are either auto-generated or |
Stephen Smalley | d330d3e | 2014-03-14 15:28:51 -0400 | [diff] [blame^] | 340 | should remain static. The policy rules come in the form: allow <em>domains</em> |
| 341 | <em>types</em>:<em>classes</em> <em>permissions</em>;, where:</p> |
Clay Murphy | 15c58c4 | 2013-07-23 11:54:42 -0700 | [diff] [blame] | 342 | <ul> |
| 343 | <li> |
Stephen Smalley | d330d3e | 2014-03-14 15:28:51 -0400 | [diff] [blame^] | 344 | <p><em>Domain</em> - A label for the process or set of processes. |
| 345 | </p></li> |
Clay Murphy | 15c58c4 | 2013-07-23 11:54:42 -0700 | [diff] [blame] | 346 | <li> |
Stephen Smalley | d330d3e | 2014-03-14 15:28:51 -0400 | [diff] [blame^] | 347 | <p><em>Type</em> - A label for the object (e.g. file, socket) or set of objects. |
| 348 | </p></li> |
Clay Murphy | 15c58c4 | 2013-07-23 11:54:42 -0700 | [diff] [blame] | 349 | <li> |
Stephen Smalley | d330d3e | 2014-03-14 15:28:51 -0400 | [diff] [blame^] | 350 | <p><em>Class</em> - The kind of object (e.g. file, socket) being accessed. |
| 351 | </p></li> |
| 352 | <li> |
| 353 | <p><em>Permission</em> - The operation (e.g. read, write) being performed. |
| 354 | </p></li> |
Clay Murphy | 51dbe2d | 2013-07-22 12:54:07 -0700 | [diff] [blame] | 355 | |
Stephen Smalley | d330d3e | 2014-03-14 15:28:51 -0400 | [diff] [blame^] | 356 | <p>And so an example use of this would follow the structure:<br> |
Clay Murphy | 15c58c4 | 2013-07-23 11:54:42 -0700 | [diff] [blame] | 357 | <code>allow appdomain app_data_file:file rw_file_perms;</code></p> |
Clay Murphy | 51dbe2d | 2013-07-22 12:54:07 -0700 | [diff] [blame] | 358 | |
| 359 | <p>This says an application is allowed to read and write files labeled |
Stephen Smalley | d330d3e | 2014-03-14 15:28:51 -0400 | [diff] [blame^] | 360 | app_data_file. Note that this rule relies upon macros defined in the |
| 361 | global_macros file, and other helpful macros can also be found in the |
| 362 | te_macros file. Macros are provided for common groupings of classes, |
| 363 | permissions and rules, and should be used whenever possible to help reduce the |
| 364 | likelihood of failures due to denials on related permissions. During |
| 365 | compilation, those overrides are concatenated to the existing SELinux settings |
| 366 | and into a single security policy. These overrides add to the base security |
| 367 | policy rather than subtract from existing settings.</p> |
Clay Murphy | 51dbe2d | 2013-07-22 12:54:07 -0700 | [diff] [blame] | 368 | |
Stephen Smalley | d330d3e | 2014-03-14 15:28:51 -0400 | [diff] [blame^] | 369 | <p>Once the new policy files and <code>BoardConfig.mk</code> updates are in |
| 370 | place, the new policy settings are automatically built into the final kernel |
| 371 | policy file.</p> |
Clay Murphy | 51dbe2d | 2013-07-22 12:54:07 -0700 | [diff] [blame] | 372 | |
| 373 | <h2 id="validation">Validation</h2> <p>Android strongly encourages OEMs to test |
| 374 | their SELinux implementations thoroughly. As manufacturers implement SELinux, |
| 375 | they should initially release their own policies in permissive mode. If |
Clay Murphy | 882fa88 | 2013-10-18 16:43:40 -0700 | [diff] [blame] | 376 | possible, apply the new policy to a test pool of devices first.</p> |
Clay Murphy | 51dbe2d | 2013-07-22 12:54:07 -0700 | [diff] [blame] | 377 | |
| 378 | <p>Once applied, make sure SELinux is running in the correct mode on the device |
Clay Murphy | 15c58c4 | 2013-07-23 11:54:42 -0700 | [diff] [blame] | 379 | by issuing the command: <code>getenforce</code></p> |
Clay Murphy | 51dbe2d | 2013-07-22 12:54:07 -0700 | [diff] [blame] | 380 | |
Stephen Smalley | d330d3e | 2014-03-14 15:28:51 -0400 | [diff] [blame^] | 381 | <p>This will print the global SELinux mode: either Disabled, Enforcing, or |
| 382 | Permissive. |
Clay Murphy | 882fa88 | 2013-10-18 16:43:40 -0700 | [diff] [blame] | 383 | Please note, this command shows only the global SELinux mode. To determine the |
| 384 | SELinux mode for each domain, you must examine the corresponding files.</p> |
Clay Murphy | 51dbe2d | 2013-07-22 12:54:07 -0700 | [diff] [blame] | 385 | |
Stephen Smalley | d330d3e | 2014-03-14 15:28:51 -0400 | [diff] [blame^] | 386 | <p>Then check for errors. Errors are routed as event logs to <code>dmesg</code> |
| 387 | and viewable locally on the device. Manufacturers should examine the SELinux |
| 388 | output to <code>dmesg</code> on these devices and refine settings prior to |
| 389 | public release in permissive mode and eventual switch to enforcing mode. It is |
| 390 | possible to capture the ongoing denial logs by running |
| 391 | <code>cat /proc/kmsg</code> or to capture denial logs from the previous boot by |
| 392 | running <code>cat /proc/last_kmsg</code>.</p> |
Clay Murphy | 51dbe2d | 2013-07-22 12:54:07 -0700 | [diff] [blame] | 393 | |
| 394 | <p>With this output, manufacturers can readily identify when system users or |
| 395 | components are in violation of SELinux policy. Manufacturers can then repair |
| 396 | this bad behavior, either by changes to the software, SELinux policy, or |
| 397 | both.</p> |
| 398 | |
Stephen Smalley | d330d3e | 2014-03-14 15:28:51 -0400 | [diff] [blame^] | 399 | <p>Specifically, these log messages indicate what processes would fail |
| 400 | under enforcing mode and why. Here is an example:</p> |
Clay Murphy | 882fa88 | 2013-10-18 16:43:40 -0700 | [diff] [blame] | 401 | |
| 402 | <pre> |
| 403 | denied { connectto } for pid=2671 comm="ping" path="/dev/socket/dnsproxyd" |
| 404 | scontext=u:r:shell:s0 tcontext=u:r:netd:s0 tclass=unix_stream_socket |
| 405 | </pre> |
| 406 | |
| 407 | <p>Interpret this output like so:</p> |
| 408 | <ul> |
| 409 | <li>The { connectto } above represents the action being taken. Together with the |
| 410 | tclass at the end (unix_stream_socket) it tells you roughly what was being done |
| 411 | to what. In this case, something was trying to connect to a unix stream |
| 412 | socket.</li> |
| 413 | <li>The scontext (u:r:shell:s0) tells you what context initiated the action. In |
| 414 | this case this is something running as the shell.</li> |
| 415 | <li>The tcontext (u:r:netd:s0) tells you the context of the action’s target. In |
| 416 | this case, that’s a unix_stream_socket owned by netd.</li> |
| 417 | <li>The comm="ping" at the top gives you an additional hint about what was being |
| 418 | run at the time the denial was generated. In this case, it’s a pretty good |
| 419 | hint.</li> |
| 420 | </ul> |
| 421 | |
| 422 | <p>Android is taking this information, analyzing |
Clay Murphy | 51dbe2d | 2013-07-22 12:54:07 -0700 | [diff] [blame] | 423 | it and refining its default security policy so that it works on a wide range of |
| 424 | Android devices with little customization. With this policy, OEMs must only |
| 425 | accommodate their own changes to the Android operating system.</p> |
| 426 | |
| 427 | <p>Then run the SELinux-enabled devices through the <a |
| 428 | href="{@docRoot}compatibility/cts-intro.html">Android |
| 429 | Compatibility Test Suite</a> (CTS).</p> <p>As said, any new policies must still |
| 430 | meet the <a href="{@docRoot}compatibility/index.html">Android |
Clay Murphy | 882fa88 | 2013-10-18 16:43:40 -0700 | [diff] [blame] | 431 | Compatibility program</a> requirements.</p> |
Clay Murphy | 51dbe2d | 2013-07-22 12:54:07 -0700 | [diff] [blame] | 432 | |
Stephen Smalley | d330d3e | 2014-03-14 15:28:51 -0400 | [diff] [blame^] | 433 | <p>Finally, if possible, turn on enforcing mode internally (on devices of |
Clay Murphy | 51dbe2d | 2013-07-22 12:54:07 -0700 | [diff] [blame] | 434 | employees) to raise the visibility of failures. Identify any user issues and |
| 435 | resolve them. </p> <h2 id="help">Help</h2> Device manufacturers are strongly |
| 436 | encouraged to work with their Android account managers to analyze SELinux |
| 437 | results and improve policy settings. Over time, Android intends to support |
| 438 | common manufacturer additions in its default SELinux policy. For more |
| 439 | information, contact <a |
Clay Murphy | 78beecd | 2013-11-05 16:27:26 -0800 | [diff] [blame] | 440 | href="mailto:security@google.com?subject=se-linux">security@android.com</a>. |