blob: d23be8c279d2905a76cac751f1419f3d592d597c [file] [log] [blame]
Clay Murphy51dbe2d2013-07-22 12:54:07 -07001page.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,
21Android begins supporting Security-Enhanced Linux (SELinux), a tool for applying
22access control policies. SELinux enhances Android security, and contributions to
23it have been made by a number of companies and organizations; all Android code
24and contributors are publicly available for review on this same site <a
25href="http://source.android.com/">source.android.com</a>. With SELinux, Android
26can better control access to application data and system logs, reduce the
27effects of malicious software, and protect users from potential flaws in mobile
28code. </p>
29
30<p>In this release, Android includes SELinux in permissive mode and a
31corresponding security policy that works by default across the <a
32href="https://android.googlesource.com/">Android Open Source Project</a>. In
33permissive mode, no actions are prevented. Instead, all potential violations are
Clay Murphy15c58c42013-07-23 11:54:42 -070034logged by the kernel to <code>dmesg</code>. This allows Android and Android device
Clay Murphy51dbe2d2013-07-22 12:54:07 -070035manufacturers to gather information about errors so they may refine their
36software and SELinux policies before enforcing them.</p>
37
38<h2 id="background">Background</h2> <p>Used properly, SELinux greatly limits the
39potential damage of compromised machines and accounts. When you adopt SELinux,
40you instill a structure by which software runs at only the minimum privilege
41level. This mitigates the effects of attacks and reduces the likelihood of
42errant processes overwriting or even transmitting data.</p>
43
44<p>SELinux provides a mandatory access control (MAC) umbrella over traditional
45discretionary access control (DAC) environments. For instance, software must
46typically run as the root user account to write to raw block devices. In a
47traditional DAC-based Linux environment, if the root user becomes compromised
48that user can write to every raw block device. However, SELinux can be used to
49label these devices so the user role assigned the root privilege can write to
50only those specified in the associated policy. In this way, root cannot
51overwrite 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
54address them with SELinux.</p>
55
56<h2 id="implementation">Implementation</h2> <p>Android&rsquo;s initial SELinux
57implementation is launching in permissive mode - rather than the non-functional
58disabled mode or the most stringent enforcing mode - to act as a reference and
59facilitate testing and development.</p>
60
61<p>SELinux is launching in permissive mode on Android to enable the first phase
62of policy development, and it is accompanied by everything you need to enable
63SELinux now.</p>
64
65<p>You merely need to integrate the <a
66href="https://android.googlesource.com/kernel/common/">latest Android kernel</a>
67and then incorporate the files found in the ~<a
68href="https://android.googlesource.com/platform/external/sepolicy/">platform/external/sepolicy</a>
69directory:<br>
70<a
71href="https://android.googlesource.com/kernel/common/">https://android.googlesource.com/kernel/common/</a>
72<br>
73<a
74href="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
77cover the upstream Android operating system. Place those files within the
78&lt;root&gt;/device/manufacturer/device-name/sepolicy directory.</p>
79
Clay Murphy15c58c42013-07-23 11:54:42 -070080<p>Then just update your <code>BoardConfig.mk</code> makefile - located in the
Clay Murphy51dbe2d2013-07-22 12:54:07 -070081&lt;device-name&gt; directory containing the sepolicy subdirectory - to
Clay Murphy15c58c42013-07-23 11:54:42 -070082reference the sepolicy subdirectory once created, like so:</p>
Clay Murphy51dbe2d2013-07-22 12:54:07 -070083
Clay Murphy15c58c42013-07-23 11:54:42 -070084<pre>
85BOARD_SEPOLICY_DIRS := \
86 &lt;root&gt;/device/manufacturer/device-name/sepolicy
87
88BOARD_SEPOLICY_UNION := \
89 genfs_contexts \
90 file_contexts \
91 sepolicy.te
92</pre>
93
Clay Murphy51dbe2d2013-07-22 12:54:07 -070094<p>After rebuilding your device, it is enabled with SELinux. You can now either
95customize your SELinux policies to accommodate your own additions to the Android
96operating system as described in the <em>Customization</em> section or verify
97your 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
100base level of functionality and thoroughly analyzed the results, you may add
101your own policy settings to cover your customizations to the Android operating
Clay Murphy15c58c42013-07-23 11:54:42 -0700102system. Of course, these policies must still meet the <a
Clay Murphy51dbe2d2013-07-22 12:54:07 -0700103href="{@docRoot}compatibility/index.html">Android Compatibility
104program</a> requirements and not remove the default SELinux settings.</p>
105
106<p>Manufacturers should not remove existing security settings. Otherwise, they
107risk breaking the Android SELinux implementation and the applications it
108governs. This includes third-party applications that will likely need to be
109improved to be compliant and operational. Applications must require no
110modification to continue functioning on SELinux-enabled devices.</p>
111
112<p>See section 9.7 of the Android 4.3 Compatibility Definition document for
113specific requirements:<br><a
114href="{@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
117upon role. Because the default policy provided by Android is so permissive, OEMs
118have great leeway in strengthening it. Here is how we recommend proceeding:</p>
119
Clay Murphy15c58c42013-07-23 11:54:42 -0700120<ol>
121<li>
122<p>Use the <a
Clay Murphy51dbe2d2013-07-22 12:54:07 -0700123href="https://android.googlesource.com/kernel/common/">latest Android
Clay Murphy15c58c42013-07-23 11:54:42 -0700124kernel</a>.</p> </li>
125<li>
126<p>Adopt the <a
Clay Murphy51dbe2d2013-07-22 12:54:07 -0700127href="http://en.wikipedia.org/wiki/Principle_of_least_privilege">principle of
Clay Murphy15c58c42013-07-23 11:54:42 -0700128least privilege</a>.</p></li>
129<li>
130<p>Address only your own additions to
Clay Murphy51dbe2d2013-07-22 12:54:07 -0700131Android. The default policy works with the <a
132href="https://android.googlesource.com/">Android Open Source Project</a>
Clay Murphy15c58c42013-07-23 11:54:42 -0700133codebase automatically.</p></li>
134<li>
135<p>Compartmentalize software components
136into modules that conduct singular tasks.</p></li>
137<li>
138<p>Create SELinux
139policies that isolate those tasks from unrelated functions.</p></li>
140<li>
Clay Murphy51dbe2d2013-07-22 12:54:07 -0700141<p>Put those policies in *.te files (the extension for SELinux policy source
142files) within the &lt;root&gt;/device/manufacturer/device-name/sepolicy
Clay Murphy15c58c42013-07-23 11:54:42 -0700143directory.</p></li>
144<li>
145<p>Release your SELinux implementation in permissive
146mode first.</p></li>
147<li><p>Analyze results and refine policy settings.</p>
148</li>
149</ol>
Clay Murphy51dbe2d2013-07-22 12:54:07 -0700150
151<p>Once integrated, OEM Android development should include a step to ensure
152SELinux compatibility going forward. In an ideal software development process,
153SELinux policy changes only when the software model changes and not the actual
154implementation.</p>
155
156<p>As device manufacturers begin to customize SELinux, they should first audit
157their additions to Android. If you&rsquo;ve added a component that conducts a
158new function, the manufacturer will need to ensure the component meets the
159security policy applied by Android, as well as any associated policy crafted by
160the OEM, before turning on enforcement.</p>
161
162<p>To prevent unnecessary issues, it is better to be overbroad and
163over-compatible than too restrictive and incompatible, which results in broken
164device functions. Conversely, if a manufacturer&rsquo;s changes will benefit
165others, it should supply the modifications to the default SELinux policy as a <a
166href="{@docRoot}source/submit-patches.html">patch</a>. If the
167patch is applied to the default security policy, the manufacturer will no longer
168need 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
171consider when crafting your own software and associated SELinux policies:</p>
172
Clay Murphy15c58c42013-07-23 11:54:42 -0700173<p><strong>Symlinks</strong> - Because symlinks appear as files, they are often read
174just as that. This can lead to exploits. For instance, some privileged components such
Clay Murphy51dbe2d2013-07-22 12:54:07 -0700175as init change the permissions of certain files, sometimes to be excessively
176open.</p>
177
178<p>Attackers might then replace those files with symlinks to code they control,
Clay Murphy15c58c42013-07-23 11:54:42 -0700179allowing the attacker to overwrite arbitrary files. But if you know your application
180will never traverse a symlink, you can prohibit it from doing so with SELinux.</p>
Clay Murphy51dbe2d2013-07-22 12:54:07 -0700181
Clay Murphy15c58c42013-07-23 11:54:42 -0700182<p><strong>System files</strong> - Consider the class of system files that should only be
183modified by the system server. Still, since <code>netd</code>, <code>init</code>,
184and <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
186potentially the system server itself.</p>
Clay Murphy51dbe2d2013-07-22 12:54:07 -0700187
188<p>With SELinux, you can identify those files as system server data files.
189Therefore, the only domain that has read/write access to them is system server.
Clay Murphy15c58c42013-07-23 11:54:42 -0700190Even if <code>netd</code> became compromised, it could not switch domains to the
191system server domain and access those system files although it runs as root.</p>
Clay Murphy51dbe2d2013-07-22 12:54:07 -0700192
Clay Murphy15c58c42013-07-23 11:54:42 -0700193<p><strong>App data</strong> - Another example is the class of functions that
194must run as root but should not get to access app data. This is incredibly useful as
195wide-ranging assertions can be made, such as certain domains unrelated to application data
Clay Murphy51dbe2d2013-07-22 12:54:07 -0700196being prohibited from accessing the internet.</p>
197
Clay Murphy15c58c42013-07-23 11:54:42 -0700198<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
200can conduct <code>setattr</code>. Anything outside of that could be prohibited from
201these 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
203system_data_files.</p> <h2 id="related-files">Related
Clay Murphy51dbe2d2013-07-22 12:54:07 -0700204Files</h2> <p>This section serves to guide you once you&rsquo;ve decided to
205customize the SELinux policy settings. See the <em>Customization</em> section
206for steps. We recommend device manufacturers start with the default Android
207SELinux policy and make the minimum possible set of changes to address their
208additions to Android. Existing Android SELinux policy files are found in the
209root of the ~<a
210href="https://android.googlesource.com/platform/external/sepolicy/">platform/external/sepolicy</a>
211directory.</p>
212
213<p>Android upgraded its SELinux policy version to allow the SELinux mode to be
214set to permissive on a per-domain basis. For example, if you run all of your
215applications on a single domain, you could set that domain to be permissive and
216then have all other functions and their domains set to enforcement. Domains are
217associated with applications by the key used to sign each application. This
218setting 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>
Clay Murphy15c58c42013-07-23 11:54:42 -0700221<ul>
222<li>
223<p><em>New SELinux policy source (*.te) files</em> - Located in the
Clay Murphy51dbe2d2013-07-22 12:54:07 -0700224&lt;root&gt;/device/manufacturer/device-name/sepolicy directory These files
225define domains and their labels. The new policy files get concatenated with the
226existing policy files during compilation into a single SELinux kernel policy
Clay Murphy15c58c42013-07-23 11:54:42 -0700227file.</p></li>
228<li>
229<p><em>Updated <code>BoardConfig.mk</code> makefile</em> - Located in the
Clay Murphy51dbe2d2013-07-22 12:54:07 -0700230&lt;device-name&gt; directory containing the sepolicy subdirectory. It must be
231updated to reference the sepolicy subdirectory once created if it wasn&rsquo;t
Clay Murphy15c58c42013-07-23 11:54:42 -0700232in initial implementation.</p> </li>
233<li>
234<p><em>Updated <code>file_contexts</code></em> - Located in
Clay Murphy51dbe2d2013-07-22 12:54:07 -0700235the sepolicy subdirectory. It labels files and is managed in the userspace. As
236you create new policies, update this file to reference them. In order to apply
Clay Murphy15c58c42013-07-23 11:54:42 -0700237new <code>file_contexts</code>, you must run <code>restorecon</code> on the file
238to be relabeled.</p>
Clay Murphy51dbe2d2013-07-22 12:54:07 -0700239</li> </ul>
240
Clay Murphy15c58c42013-07-23 11:54:42 -0700241<p>The remaining files in the sepolicy directory are either auto-generated or
Clay Murphy51dbe2d2013-07-22 12:54:07 -0700242should remain static. The policy files come in the form: allow, domain, and
243context, for a set of actions:</p>
244
Clay Murphy15c58c42013-07-23 11:54:42 -0700245<ul>
246<li>
247<p><em>Allow</em> - Gives the role permission to carry out the action described
248in the context within the specified domain.</p> </li>
249<li>
250<p><em>Domain</em> - Domain
Clay Murphy51dbe2d2013-07-22 12:54:07 -0700251represents scope of the rule and gets converted to a security identifier (SID)
Clay Murphy15c58c42013-07-23 11:54:42 -0700252in the kernel.</p> </li>
253<li>
254<p><em>Context</em> - An identifier for the rule, this is converted
Clay Murphy51dbe2d2013-07-22 12:54:07 -0700255to an integer in the kernel.</p> </li> </ul>
256
Clay Murphy15c58c42013-07-23 11:54:42 -0700257<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>
Clay Murphy51dbe2d2013-07-22 12:54:07 -0700259
260<p>This says an application is allowed to read and write files labeled
261app_data_file. During compilation, those overrides are concatenated to the
262existing SELinux settings and into a single security policy. These overrides add
263to the base security policy rather than subtract from existing settings.</p>
264
Clay Murphy15c58c42013-07-23 11:54:42 -0700265<p>Once the new policy files and <code>BoardConfig.mk</code> updates are in place, the new
Clay Murphy51dbe2d2013-07-22 12:54:07 -0700266policy settings get automatically uploaded to the device.</p>
267
268<h2 id="validation">Validation</h2> <p>Android strongly encourages OEMs to test
269their SELinux implementations thoroughly. As manufacturers implement SELinux,
270they should initially release their own policies in permissive mode. If
271possible, 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
Clay Murphy15c58c42013-07-23 11:54:42 -0700274by issuing the command: <code>getenforce</code></p>
Clay Murphy51dbe2d2013-07-22 12:54:07 -0700275
276<p>This will print the SELinux mode: either Disabled, Enforcing, or Permissive.
277If permissive, you are compliant. Enforcing is explicitly not compliant in
278Android 4.3. (Because of its risk, enforcing mode comes with a much heavier
279testing burden.)</p>
280
Clay Murphy15c58c42013-07-23 11:54:42 -0700281<p>Then check for errors. Errors are routed as event logs to <code>dmesg</code>
282and viewable locally on the device. Manufacturers should examine the SELinux output
283to <code>dmesg</code> on these devices and refine settings prior to public release in
284permissive mode.</p>
Clay Murphy51dbe2d2013-07-22 12:54:07 -0700285
286<p>With this output, manufacturers can readily identify when system users or
287components are in violation of SELinux policy. Manufacturers can then repair
288this bad behavior, either by changes to the software, SELinux policy, or
289both.</p>
290
291<p>Specifically, these log messages indicate what roles and processes would fail
292under policy enforcement and why. Android is taking this information, analyzing
293it and refining its default security policy so that it works on a wide range of
294Android devices with little customization. With this policy, OEMs must only
295accommodate their own changes to the Android operating system.</p>
296
297<p>Then run the SELinux-enabled devices through the <a
298href="{@docRoot}compatibility/cts-intro.html">Android
299Compatibility Test Suite</a> (CTS).</p> <p>As said, any new policies must still
300meet the <a href="{@docRoot}compatibility/index.html">Android
301Compatibility program</a> requirements:<br><a
302href="{@docRoot}compatibility/android-4.3-cdd.pdf">http://source.android.com/compatibility/android-4.3-cdd.pdf</a></p>
303
Clay Murphy15c58c42013-07-23 11:54:42 -0700304<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>
Clay Murphy51dbe2d2013-07-22 12:54:07 -0700306
307<p>Finally, if possible, turn on enforcement internally (on devices of
308employees) to raise the visibility of failures. Identify any user issues and
309resolve them. </p> <h2 id="help">Help</h2> Device manufacturers are strongly
310encouraged to work with their Android account managers to analyze SELinux
311results and improve policy settings. Over time, Android intends to support
312common manufacturer additions in its default SELinux policy. For more
313information, contact <a
314href="mailto:security@android.com">security@android.com</a> or Geremy Condra (<a
315href="mailto:gcondra@google.com">gcondra@google.com</a>) directly.