blob: acf9291ec97ba2e869c7b989583d903d92e604fd [file] [log] [blame]
Clay Murphy882fa882013-10-18 16:43:40 -07001page.title=Validating Security-Enhanced Linux in Android
Clay Murphy51dbe2d2013-07-22 12:54:07 -07002@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-->
Clay Murphy882fa882013-10-18 16:43:40 -070019<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 Murphy51dbe2d2013-07-22 12:54:07 -070026
Clay Murphy882fa882013-10-18 16:43:40 -070027<h2 id="introduction">Introduction</h2>
28<p>
29As part of the Android <a href="{@docRoot}devices/tech/security/index.html">security
30model</a>, Android uses Security-Enhanced Linux (SELinux) to apply access
31control policies. SELinux enhances Android security, and contributions to it
32have been made by a number of companies and organizations; all Android code and
33contributors are publicly available for review on
34<a href="https://android.googlesource.com/">android.googlesource.com</a>. With SELinux,
35Android can
36better control access to application data and system logs, reduce the effects of
37malicious software, and protect users from potential flaws in code on mobile
38devices.
39</p>
40<p>
41Android includes SELinux in enforcing mode and a corresponding security policy
42that works by default across the <a
43href="https://android.googlesource.com/">Android Open Source
44Project</a>. In enforcing mode, illegitimate
45actions are prevented and all potential violations are logged by the kernel to
46<code>dmesg</code>. Android device manufacturers should gather information about errors so
47they may refine their software and SELinux policies before enforcing them.
48</p>
Clay Murphy51dbe2d2013-07-22 12:54:07 -070049
Clay Murphy882fa882013-10-18 16:43:40 -070050<h2 id="background">Background</h2>
51<p>
52Please note, Android upgraded its SELinux policy version to allow the SELinux
53mode to be set on a per-domain basis. For example, if you run all of your
54applications on a single domain, you could set that domain to be permissive and
55then have all other functions and their domains set to enforcement. Domains are
56associated with applications by the key used to sign each application. This
57setting is made at the top of each SELinux policy source (*.te) file.
58</p>
59<p>
60Android follows this model of isolating applications to a single domain. With
61this, only the root domain and root-level processes (such as <code>initd</code>,
62<code>installd</code> and
63<code>vold</code>) are now set to enforcing mode. <em>The application domain remains in
64permissive mode to allow further evaluation and prevent failures. Still, an
65errant application could trigger an action in the root domain that is not
66allowed, thereby causing the application to crash.</em>
67</p>
68<p>
69For this reason, device manufacturers should retain the default settings
70provided by Android and limit enforcing mode to the root domain only until
71they've resolved issues reported in dmesg. That said, device manufacturers may
72need to augment their SELinux implementation to account for their additions and
73other changes to the operating system. See the <em>Customization</em> section for
74instructions.
75</p>
Clay Murphy51dbe2d2013-07-22 12:54:07 -070076
Clay Murphy882fa882013-10-18 16:43:40 -070077<h2 id="mac">Mandatory access control</h2>
78<p>
79In conjunction with other Android security measures, Android's access control
80policy greatly limits the potential damage of compromised
81machines and accounts. Using tools like Android's discretionary and mandatory
82access controls gives you a structure to ensure your software runs
83only at the minimum privilege level. This mitigates the effects of
84attacks and reduces the likelihood of errant processes overwriting or even
85transmitting data.
86</p>
87<p>
88Starting in Android 4.3, SELinux provides a mandatory access control (MAC) umbrella over traditional
Clay Murphy51dbe2d2013-07-22 12:54:07 -070089discretionary access control (DAC) environments. For instance, software must
90typically run as the root user account to write to raw block devices. In a
91traditional DAC-based Linux environment, if the root user becomes compromised
92that user can write to every raw block device. However, SELinux can be used to
93label these devices so the user role assigned the root privilege can write to
94only those specified in the associated policy. In this way, root cannot
Clay Murphy882fa882013-10-18 16:43:40 -070095overwrite data and system settings outside of the specific raw block
96device.
97</p>
98<p>
99See the <em>Use Cases</em> section for more examples of threats and ways to address
100them with SELinux.
101</p>
Clay Murphy51dbe2d2013-07-22 12:54:07 -0700102
Clay Murphy882fa882013-10-18 16:43:40 -0700103<h2 id="implementation">Implementation</h2>
104<p>
105Android's SELinux implementation is in enforcing mode - rather than the
106non-functional disabled mode or the notification-only permissive mode - to act
107as a reference and facilitate testing and development. Although enforcing mode
108is set globally, please remember this can be overridden on a per-domain basis
109as is in the case of the application domain.
110</p>
111<p>
112SELinux for Android is accompanied by everything you need to enable SELinux
113now. You merely need to integrate the <a
114href="https://android.googlesource.com/kernel/common/">latest Android
115kernel</a> and then incorporate the files found in the
116~<a
Clay Murphy51dbe2d2013-07-22 12:54:07 -0700117href="https://android.googlesource.com/platform/external/sepolicy/">platform/external/sepolicy</a>
Clay Murphy882fa882013-10-18 16:43:40 -0700118directory (where examples can also be found):<br/>
Clay Murphy51dbe2d2013-07-22 12:54:07 -0700119<a
120href="https://android.googlesource.com/kernel/common/">https://android.googlesource.com/kernel/common/</a>
Clay Murphy882fa882013-10-18 16:43:40 -0700121<br/>
Clay Murphy51dbe2d2013-07-22 12:54:07 -0700122<a
Clay Murphy882fa882013-10-18 16:43:40 -0700123href="https://android.googlesource.com/platform/external/sepolicy/">https://android.googlesource.com/platform/external/sepolicy/</a>
124</p>
Clay Murphy51dbe2d2013-07-22 12:54:07 -0700125
Clay Murphy882fa882013-10-18 16:43:40 -0700126</p>
127 Those files when compiled comprise the SELinux kernel security policy and cover
128the upstream Android operating system. Place those files within the
129<root>/device/manufacturer/device-name/sepolicy directory.<br/>
130Then just update your <code>BoardConfig.mk</code> makefile - located in the <device-name>
131directory containing the sepolicy subdirectory - to reference the sepolicy
132subdirectory once created, like so:
133</p>
Clay Murphy51dbe2d2013-07-22 12:54:07 -0700134
Clay Murphy15c58c42013-07-23 11:54:42 -0700135<pre>
136BOARD_SEPOLICY_DIRS := \
137 &lt;root&gt;/device/manufacturer/device-name/sepolicy
138
139BOARD_SEPOLICY_UNION := \
140 genfs_contexts \
141 file_contexts \
142 sepolicy.te
143</pre>
144
Clay Murphy882fa882013-10-18 16:43:40 -0700145<p>
146After rebuilding your device, it is enabled with SELinux. You can now either
Clay Murphy51dbe2d2013-07-22 12:54:07 -0700147customize your SELinux policies to accommodate your own additions to the Android
148operating system as described in the <em>Customization</em> section or verify
Clay Murphy882fa882013-10-18 16:43:40 -0700149your
150existing setup as covered in the <em>Validation</em> section.
151</p>
Clay Murphy51dbe2d2013-07-22 12:54:07 -0700152
Clay Murphy882fa882013-10-18 16:43:40 -0700153<h2 id="customization">Customization</h2>
154<p>
155Once you've integrated this base level of functionality and thoroughly analyzed
156the results, you may add your own policy settings to cover your customizations
157to the Android operating system. Of course, these policies must still meet the
158<a href="http://source.android.com/compatibility/index.html">Android
159Compatibility
160program</a> requirements and
161not remove the default SELinux settings.
162</p>
163<p>
164Manufacturers should not remove existing security settings. Otherwise, they risk
165breaking the Android SELinux implementation and the applications it governs.
166This includes third-party applications that will likely need to be improved to
167be compliant and operational. Applications must require no modification to
168continue functioning on SELinux-enabled devices.
169</p>
170<p>
171See the <em>Kernel Security Features</em> section of the Android Compatibility
172Definition document for specific requirements:<br/>
173<a
174href="http://source.android.com/compatibility/index.html">http://source.android.com/compatibility/index.html</a>
175</p>
176<p>
177SELinux uses a whitelist approach, meaning it grants special privileges based
178upon role. Since Android's default SELinux policy already supports the Android
179Open Source Project, OEMs are not required to modify SELinux settings in any
180way. If they do customize SELinux settings, they should take great care not to
181break existing applications. Here is how we recommend proceeding:
182</p>
Clay Murphy51dbe2d2013-07-22 12:54:07 -0700183
Clay Murphy15c58c42013-07-23 11:54:42 -0700184<ol>
Clay Murphy882fa882013-10-18 16:43:40 -0700185<li>Use the <a href="https://android.googlesource.com/kernel/common/">latest
186Android
187kernel</a>.</li>
188<li>Adopt the <a
Clay Murphy51dbe2d2013-07-22 12:54:07 -0700189href="http://en.wikipedia.org/wiki/Principle_of_least_privilege">principle of
Clay Murphy882fa882013-10-18 16:43:40 -0700190least
191privilege</a>.</li>
192<li>Address only your own additions to Android. The default policy works with
193the
194<a href="https://android.googlesource.com/">Android Open Source Project</a>
195codebase
196automatically.</li>
197<li>Compartmentalize software components into modules that conduct singular
198tasks.</li>
199<li>Create SELinux policies that isolate those tasks from unrelated
200functions.</li>
201<li>Put those policies in *.te files (the extension for SELinux policy source
202files) within the <root>/device/manufacturer/device-name/sepolicy
203directory.</li>
204<li>Release your SELinux implementation in permissive mode first.</li>
205<li>Analyze results and refine policy settings.</li>
Clay Murphy15c58c42013-07-23 11:54:42 -0700206</ol>
Clay Murphy51dbe2d2013-07-22 12:54:07 -0700207
Clay Murphy882fa882013-10-18 16:43:40 -0700208<p>
209Once integrated, OEM Android development should include a step to ensure
210SELinux
211compatibility going forward. In an ideal software development process, SELinux
212policy changes only when the software model changes and not the actual
213implementation.
214</p>
215<p>
216As device manufacturers begin to customize SELinux, they should first audit
217their additions to Android. If they've added a component that conducts a new
218function, the manufacturers will need to ensure the component meets the security
219policy applied by Android, as well as any associated policy crafted by the OEM,
220before turning on enforcement.
221</p>
222<p>
223To prevent unnecessary issues, it is better to be overbroad and over-compatible
224than too restrictive and incompatible, which results in broken device functions.
225Conversely, if a manufacturer's changes will benefit others, it should supply
226the modifications to the default SELinux policy as a
227<a href="http://source.android.com/source/submit-patches.html">patch</a>. If the
228patch is
229applied to the default security policy, the manufacturer will no longer need to
230make this change with each new Android release.
231</p>
Clay Murphy51dbe2d2013-07-22 12:54:07 -0700232
233<h2 id="use-cases">Use Cases</h2> <p>Here are specific examples of exploits to
234consider when crafting your own software and associated SELinux policies:</p>
235
Clay Murphy15c58c42013-07-23 11:54:42 -0700236<p><strong>Symlinks</strong> - Because symlinks appear as files, they are often read
237just as that. This can lead to exploits. For instance, some privileged components such
Clay Murphy882fa882013-10-18 16:43:40 -0700238as <code>init</code> change the permissions of certain files, sometimes to be excessively
Clay Murphy51dbe2d2013-07-22 12:54:07 -0700239open.</p>
240
241<p>Attackers might then replace those files with symlinks to code they control,
Clay Murphy15c58c42013-07-23 11:54:42 -0700242allowing the attacker to overwrite arbitrary files. But if you know your application
243will never traverse a symlink, you can prohibit it from doing so with SELinux.</p>
Clay Murphy51dbe2d2013-07-22 12:54:07 -0700244
Clay Murphy15c58c42013-07-23 11:54:42 -0700245<p><strong>System files</strong> - Consider the class of system files that should only be
246modified by the system server. Still, since <code>netd</code>, <code>init</code>,
247and <code>vold</code> run as root, they can access those system files. So if
248<code>netd</code> became compromised, it could compromise those files and
249potentially the system server itself.</p>
Clay Murphy51dbe2d2013-07-22 12:54:07 -0700250
251<p>With SELinux, you can identify those files as system server data files.
252Therefore, the only domain that has read/write access to them is system server.
Clay Murphy15c58c42013-07-23 11:54:42 -0700253Even if <code>netd</code> became compromised, it could not switch domains to the
254system server domain and access those system files although it runs as root.</p>
Clay Murphy51dbe2d2013-07-22 12:54:07 -0700255
Clay Murphy15c58c42013-07-23 11:54:42 -0700256<p><strong>App data</strong> - Another example is the class of functions that
257must run as root but should not get to access app data. This is incredibly useful as
258wide-ranging assertions can be made, such as certain domains unrelated to application data
Clay Murphy51dbe2d2013-07-22 12:54:07 -0700259being prohibited from accessing the internet.</p>
260
Clay Murphy15c58c42013-07-23 11:54:42 -0700261<p><strong>setattr</strong> - For commands such as <code>chmod</code> and
262<code>chown</code>, you could identify the set of files where the associated domain
263can conduct <code>setattr</code>. Anything outside of that could be prohibited from
264these changes, even by root. So an application might run <code>chmod</code> and
265<code>chown</code> against those labeled app_data_files but not shell_data_files or
266system_data_files.</p> <h2 id="related-files">Related
Clay Murphy51dbe2d2013-07-22 12:54:07 -0700267Files</h2> <p>This section serves to guide you once you&rsquo;ve decided to
268customize the SELinux policy settings. See the <em>Customization</em> section
269for steps. We recommend device manufacturers start with the default Android
270SELinux policy and make the minimum possible set of changes to address their
271additions to Android. Existing Android SELinux policy files are found in the
272root of the ~<a
273href="https://android.googlesource.com/platform/external/sepolicy/">platform/external/sepolicy</a>
274directory.</p>
275
276<p>Android upgraded its SELinux policy version to allow the SELinux mode to be
277set to permissive on a per-domain basis. For example, if you run all of your
278applications on a single domain, you could set that domain to be permissive and
279then have all other functions and their domains set to enforcement. Domains are
280associated with applications by the key used to sign each application. This
281setting is made at the top of each SELinux policy source (*.te) file.</p>
282
283<p>Here are the files you must create or edit in order to customize SELinux:</p>
Clay Murphy15c58c42013-07-23 11:54:42 -0700284<ul>
285<li>
286<p><em>New SELinux policy source (*.te) files</em> - Located in the
Clay Murphy51dbe2d2013-07-22 12:54:07 -0700287&lt;root&gt;/device/manufacturer/device-name/sepolicy directory These files
288define domains and their labels. The new policy files get concatenated with the
289existing policy files during compilation into a single SELinux kernel policy
Clay Murphy882fa882013-10-18 16:43:40 -0700290file.</p>
291<p><strong>Important</strong>:Do not alter the app.te file provided by the
292Android Open Source Project. Doing so risks breaking all third-party applications.
293</p>
294</li>
Clay Murphy15c58c42013-07-23 11:54:42 -0700295<li>
296<p><em>Updated <code>BoardConfig.mk</code> makefile</em> - Located in the
Clay Murphy51dbe2d2013-07-22 12:54:07 -0700297&lt;device-name&gt; directory containing the sepolicy subdirectory. It must be
298updated to reference the sepolicy subdirectory once created if it wasn&rsquo;t
Clay Murphy15c58c42013-07-23 11:54:42 -0700299in initial implementation.</p> </li>
300<li>
301<p><em>Updated <code>file_contexts</code></em> - Located in
Clay Murphy51dbe2d2013-07-22 12:54:07 -0700302the sepolicy subdirectory. It labels files and is managed in the userspace. As
303you create new policies, update this file to reference them. In order to apply
Clay Murphy15c58c42013-07-23 11:54:42 -0700304new <code>file_contexts</code>, you must run <code>restorecon</code> on the file
305to be relabeled.</p>
Clay Murphy51dbe2d2013-07-22 12:54:07 -0700306</li> </ul>
307
Clay Murphy15c58c42013-07-23 11:54:42 -0700308<p>The remaining files in the sepolicy directory are either auto-generated or
Clay Murphy51dbe2d2013-07-22 12:54:07 -0700309should remain static. The policy files come in the form: allow, domain, and
310context, for a set of actions:</p>
311
Clay Murphy15c58c42013-07-23 11:54:42 -0700312<ul>
313<li>
314<p><em>Allow</em> - Gives the role permission to carry out the action described
315in the context within the specified domain.</p> </li>
316<li>
317<p><em>Domain</em> - Domain
Clay Murphy51dbe2d2013-07-22 12:54:07 -0700318represents scope of the rule and gets converted to a security identifier (SID)
Clay Murphy15c58c42013-07-23 11:54:42 -0700319in the kernel.</p> </li>
320<li>
321<p><em>Context</em> - An identifier for the rule, this is converted
Clay Murphy51dbe2d2013-07-22 12:54:07 -0700322to an integer in the kernel.</p> </li> </ul>
323
Clay Murphy15c58c42013-07-23 11:54:42 -0700324<p>And so the an example use of this would follow the structure:<br>
325<code>allow appdomain app_data_file:file rw_file_perms;</code></p>
Clay Murphy51dbe2d2013-07-22 12:54:07 -0700326
327<p>This says an application is allowed to read and write files labeled
328app_data_file. During compilation, those overrides are concatenated to the
329existing SELinux settings and into a single security policy. These overrides add
330to the base security policy rather than subtract from existing settings.</p>
331
Clay Murphy15c58c42013-07-23 11:54:42 -0700332<p>Once the new policy files and <code>BoardConfig.mk</code> updates are in place, the new
Clay Murphy51dbe2d2013-07-22 12:54:07 -0700333policy settings get automatically uploaded to the device.</p>
334
335<h2 id="validation">Validation</h2> <p>Android strongly encourages OEMs to test
336their SELinux implementations thoroughly. As manufacturers implement SELinux,
337they should initially release their own policies in permissive mode. If
Clay Murphy882fa882013-10-18 16:43:40 -0700338possible, apply the new policy to a test pool of devices first.</p>
Clay Murphy51dbe2d2013-07-22 12:54:07 -0700339
340<p>Once applied, make sure SELinux is running in the correct mode on the device
Clay Murphy15c58c42013-07-23 11:54:42 -0700341by issuing the command: <code>getenforce</code></p>
Clay Murphy51dbe2d2013-07-22 12:54:07 -0700342
Clay Murphy882fa882013-10-18 16:43:40 -0700343<p>This will print the global SELinux mode: either Disabled, Enforcing, or Permissive.
344Please note, this command shows only the global SELinux mode. To determine the
345SELinux mode for each domain, you must examine the corresponding files.</p>
Clay Murphy51dbe2d2013-07-22 12:54:07 -0700346
Clay Murphy15c58c42013-07-23 11:54:42 -0700347<p>Then check for errors. Errors are routed as event logs to <code>dmesg</code>
348and viewable locally on the device. Manufacturers should examine the SELinux output
349to <code>dmesg</code> on these devices and refine settings prior to public release in
Clay Murphy882fa882013-10-18 16:43:40 -0700350permissive mode and eventual switch to enforcing mode.</p>
Clay Murphy51dbe2d2013-07-22 12:54:07 -0700351
352<p>With this output, manufacturers can readily identify when system users or
353components are in violation of SELinux policy. Manufacturers can then repair
354this bad behavior, either by changes to the software, SELinux policy, or
355both.</p>
356
357<p>Specifically, these log messages indicate what roles and processes would fail
Clay Murphy882fa882013-10-18 16:43:40 -0700358under policy enforcement and why. Here is an example:</p>
359
360<pre>
361denied { connectto } for pid=2671 comm="ping" path="/dev/socket/dnsproxyd"
362scontext=u:r:shell:s0 tcontext=u:r:netd:s0 tclass=unix_stream_socket
363</pre>
364
365<p>Interpret this output like so:</p>
366<ul>
367<li>The { connectto } above represents the action being taken. Together with the
368tclass at the end (unix_stream_socket) it tells you roughly what was being done
369to what. In this case, something was trying to connect to a unix stream
370socket.</li>
371<li>The scontext (u:r:shell:s0) tells you what context initiated the action. In
372this case this is something running as the shell.</li>
373<li>The tcontext (u:r:netd:s0) tells you the context of the actions target. In
374this case, thats a unix_stream_socket owned by netd.</li>
375<li>The comm="ping" at the top gives you an additional hint about what was being
376run at the time the denial was generated. In this case, its a pretty good
377hint.</li>
378</ul>
379
380<p>Android is taking this information, analyzing
Clay Murphy51dbe2d2013-07-22 12:54:07 -0700381it and refining its default security policy so that it works on a wide range of
382Android devices with little customization. With this policy, OEMs must only
383accommodate their own changes to the Android operating system.</p>
384
385<p>Then run the SELinux-enabled devices through the <a
386href="{@docRoot}compatibility/cts-intro.html">Android
387Compatibility Test Suite</a> (CTS).</p> <p>As said, any new policies must still
388meet the <a href="{@docRoot}compatibility/index.html">Android
Clay Murphy882fa882013-10-18 16:43:40 -0700389Compatibility program</a> requirements.</p>
Clay Murphy51dbe2d2013-07-22 12:54:07 -0700390
391<p>Finally, if possible, turn on enforcement internally (on devices of
392employees) to raise the visibility of failures. Identify any user issues and
393resolve them. </p> <h2 id="help">Help</h2> Device manufacturers are strongly
394encouraged to work with their Android account managers to analyze SELinux
395results and improve policy settings. Over time, Android intends to support
396common manufacturer additions in its default SELinux policy. For more
397information, contact <a
398href="mailto:security@android.com">security@android.com</a> or Geremy Condra (<a
399href="mailto:gcondra@google.com">gcondra@google.com</a>) directly.