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