1page.title=Implementing Security 2@jd:body 3 4<!-- 5 Copyright 2014 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<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> 26 27<h2 id="introduction">Introduction</h2> 28 29<p>The Android Security Team regularly receive requests for more information about 30how to prevent potential security issues on Android devices. We also 31occasionally perform spot-checks of devices and let OEMs and affected partners 32know of potential issues.</p> 33 34<p>This document provides OEMs and other partners with a number of security best 35practices based upon our own experiences. This is an extension of the 36<a href="http://developer.android.com/guide/practices/security.html">Designing for 37Security</a> documentation we've provided for developers, including best 38practices that are unique to those who are building or installing system-level 39software on devices.</p> 40 41<p>Where possible, the Android Security Team will incorporate tests into the 42<a href="{@docRoot}compatibility/cts-intro.html">Android Compatibility Test 43Suite</a> (CTS) and 44<a href="http://tools.android.com/tips/lint">Android Lint</a> to facilitate adoption of 45these best practices. We encourage partners to contribute tests that can help 46other Android users. A partial list of security-related tests can be found at: 47<code>root/cts/tests/tests/security/src/android/security/cts</code></p> 48 49<h2 id="dev-process">Development process</h2> 50 51<h3 id="sec-review">Source code security review</h3> 52<p> Source code review can detect a broad range of security issues, including those 53identified in this document. Android strongly encourages both manual and 54automated source code review.</p> 55 56<ol> 57<li><a href="http://tools.android.com/tips/lint">Android Lint</a> 58<strong>should</strong> be run on all application code using the Android SDK. 59Issues that are identified should be corrected.</li> 60<li>Native code <strong>should</strong> be analyzed using an automated tool that 61can detect memory management issues such as buffer overflows and off-by-one 62errors.</li> 63</ol> 64 65<h3 id="auto-test">Automated testing</h3> 66<p>Automated testing can detect a broad range of security issues, including many of 67those identified in this document.</p> 68 69<ol> 70<li>CTS is regularly updated with security tests; the most recent version of CTS 71<strong>must</strong> be run to verify compatibility.</li> 72<li>CTS <strong>should</strong> be run regularly throughout the development process to detect 73problems early and reduce time to correction. Android uses CTS as part of 74continuous integration with our automated build process, which builds 75multiple times per day.</li> 76<li>OEMs <strong>should</strong> automate security testing of any interfaces including testing 77with malformed inputs (fuzz testing).</li> 78</ol> 79 80<h3 id="sign-sysimg">Signing system images</h3> 81<p>The signature of the system image is critical for determining the integrity of 82the device. Specifically:</p> 83 84<ol> 85<li>Devices <strong>must not</strong> be signed with a key that is publicly known.</li> 86<li>Keys used to sign devices <strong>should</strong> be managed in a manner consistent with 87industry standard practices for handling sensitive keys, including a hardware 88security module (HSM) that provides limited, auditable access.</li> 89</ol> 90 91<h3 id="sign-apk">Signing applications (APKs)</h3> 92<p>Application signatures play an important role in device security. They are used 93for permissions checks as well as software updates. When selecting a key to use 94for signing applications, it is important to consider whether an application 95will be available only on a single device or common across multiple devices. 96Consider:</p> 97 98<ol> 99<li>Applications <strong>must not</strong> be signed with a key that is publicly known.</li> 100<li>Keys used to sign applications <strong>should</strong> be managed in a manner consistent 101with industry standard practices for handling sensitive keys, including an 102HSM that provides limited, auditable access.</li> 103<li>Applications <strong>should not</strong> be signed with the platform key.</li> 104<li>Applications with the same package name <strong>should not</strong> be signed with 105different keys. This often occurs when creating an application for different 106devices, especially when using the platform key. If the application is 107device-independent, then use the same key across devices. If the application 108is device-specific, create unique package names per device and key.</li> 109</ol> 110 111<h3 id="apps-pub">Apps publishing</h3> 112<p>Google Play provides OEMs with the ability to update applications without 113performing a complete system update. This can expedite response to security 114issues and delivery of new features. This also provides a way to make sure that 115your application has a unique package name.</p> 116 117<ol> 118<li>Apps <strong>should</strong> be uploaded to Google Play to allow automated updates without 119requiring a full OTA. Applications that are uploaded but "unpublished" are 120not directly downloadable by users, but the apps are still updated. Users who 121have ever installed such an app can install it again and again on their other 122devices as well.</li> 123<li>To avoid potential confusion, apps <strong>should</strong> be created with a package name 124clearly associated with your company, such as by using a company trademark.</li> 125<li>Apps published by OEMs <strong>should</strong> be uploaded on the Google Play store in 126order to avoid package name impersonation by third-party users.<br/> 127<br/> 128If an OEM installs an app on a phone without publishing it on the Play store, 129another developer could upload that same app, using the same package name,, 130and change the metadata for the app. When presented to the user, this 131unrelated metadata could create confusion.</li> 132</ol> 133 134<h3 id="incident-response">Incident response</h3> 135<p>External parties must have the ability to contact OEMs about device-specific 136security issues. We strongly recommend the creation of a publicly accessible 137email address for managing security incidents.</p> 138 139<ol> 140<li>Create a <em>security@your-company.com</em> or similar address and publicize 141it.</li> 142<li>If you become aware of a security issue affecting Android OS or Android 143devices from multiple OEMs, you <strong>should</strong> contact the Android 144Security Team with a bug filed through the AOSP bug tracker <a 145href="https://code.google.com/p/android/issues/entry?template=Security%20bug%20report">Security 146bug report</a> template.</p> 147.</li> 148</ol> 149 150<h2 id="prod-implement">Product implementation</h2> 151 152<h3 id="root-processes">Root processes</h3> 153<p>Root processes are the most frequent target of privilege escalation attacks, so 154reducing the number of root processes reduces risk of privilege escalation. CTS 155has been expanded with an informational test that lists root processes.</p> 156 157<ol> 158<li>The devices <strong>should</strong> run the minimum necessary code as root. Where 159possible, use a regular android process rather than a root process. The ICS 160Galaxy Nexus has only six root processes - vold, inetd, zygote, tf_daemon, 161ueventd, and init. Please let the Android team know if capabilities are 162required that are not accessible without root privileges.</li> 163<li>Where possible, root code <strong>should</strong> be isolated from untrusted data and 164accessed via IPC. For example, reduce root functionality to a small Service 165accessible via Binder and expose the Service with signature permission to an 166application with low or no privileges to handle network traffic.</li> 167<li>Root processes <strong>must not</strong> listen on a network socket.</li> 168<li>Root processes <strong>must not</strong> provide a general-purpose runtime for 169applications. (e.g. a Java VM)</li> 170</ol> 171 172<h3 id="sys-apps">System apps</h3> 173<p>In general, apps pre-installed by OEMs should not be running with the shared UID 174of system. Realistically, however, sometimes this is necessary. If an app is 175using the shared UID of system or another privileged service (i.e., phone), it 176should not export any services, broadcast receivers, or content providers that 177can be accessed by third-party apps installed by users.</p> 178 179<ol> 180<li>The devices <strong>should</strong> run the minimum necessary code as system. Where 181possible, use an android process with its own UID rather than reusing the 182system UID.</li> 183<li>Where possible, system code <strong>should</strong> be isolated from untrusted data and 184expose IPC only to other trusted processes.</li> 185<li>System processes <strong>must not</strong> listen on a network socket.</li> 186</ol> 187 188<h3 id="process-isolate">Process isolation</h3> 189<p>The Android Application Sandbox provides applications with an expectation of 190isolation from other processes on the system, including root processes and 191debuggers. Unless debugging is specifically enabled by the application and the 192user, no application should violate that expectation.</p> 193 194<ol> 195<li>Root processes <strong>must not</strong> access data within individual application data 196folders, unless using a documented Android debugging method.</li> 197<li>Root processes <strong>must not</strong> access memory of applications, unless using a 198documented Android debugging method.</li> 199<li>The device <strong>must not</strong> include any application that accesses data or memory 200of other applications or processes.</li> 201</ol> 202 203<h3 id="suid-files">SUID files</h3> 204<p>New setuid programs should not be accessible by untrusted programs. Setuid 205programs have frequently been the location of vulnerabilities that can be used 206to gain root access, and minimizing the availability of the program to untrusted 207applications is a security best practice.</p> 208 209<ol> 210<li>SUID processes <strong>must not</strong> provide a shell or backdoor that can be used to 211circumvent the Android security model.</li> 212<li>SUID programs <strong>must not</strong> be writable by any user.</li> 213<li>SUID programs <strong>should</strong> not be world readable or executable. Create a 214group, limit access to the SUID binary to members of that group, and place any 215applications that should be able to execute the SUID program into that 216group.</li> 217<li>SUID programs are a common source of user "rooting" of devices. To reduce 218this risk, SUID programs <strong>should not</strong> be executable by the shell 219user.</li> 220</ol> 221 222<p>The CTS verifier has been expanded with an informational test that lists SUID 223files. Certain setuid files are not permitted, per CTS tests.</p> 224 225<h3 id="listening-sockets">Listening sockets</h3> 226<p>CTS tests have been expanded to fail when a device is listening on any port, on 227any interface. In the event of a failure, Google will verify that the following 228best practices are being used:</p> 229 230<ol> 231<li>There <strong>should</strong> be no listening ports on the device.</li> 232<li>Listening ports <strong>must</strong> be able to be disabled without an OTA. 233This can be either a server or user-device configuration change.</li> 234<li>Root processes <strong>must not</strong> listen on any port.</li> 235<li>Processes owned by the system UID <strong>must not</strong> listen on any 236port.</li> 237<li>For local IPC using sockets, applications <strong>must</strong> use a UNIX 238Domain Socket with access limited to a group. Create a file descriptor for the 239IPC and make it +RW for a specific UNIX group. Any client applications must be 240within that UNIX group.</li> 241<li>Some devices with multiple processors (e.g. a radio/modem separate from the 242application processor) use network sockets to communicate between processors. 243In those instances, the network socket used for inter-processor communication 244<strong>must</strong> use an isolated network interface to prevent access by 245unauthorized 246applications on the device. One approach is to use iptables to prevent access by 247other applications on the device.</li> 248<li>Daemons that handle listening ports <strong>must</strong> be robust against malformed 249data. Google may conduct fuzz-testing against the port using an unauthorized 250client, and, where possible, authorized client. Any crashes will be filed as 251bugs with an appropriate severity.</li> 252</ol> 253 254<h3 id="logging">Logging</h3> 255<p>Logging of data increases the risk of exposure of that data and reduces system 256performance. Multiple public security incidents have occurred as the result of 257logging of sensitive user data by applications installed by default on Android 258devices.</p> 259 260<ol> 261<li>Applications or system services <strong>should not</strong> log data provided from 262third-party applications that might include sensitive information.</li> 263<li>Applications <strong>must not</strong> log any Personally Identifiable Information (PII) 264as part of normal operation.</li> 265</ol> 266 267<p>CTS has been expanded with a number of tests that check for the presence of 268potentially sensitive information in the system logs.</p> 269 270<h3 id="directories">Directories</h3> 271<p>World-writable directories can introduce security weaknesses. Writable 272directories may enable an application to rename other trusted files, 273substituting their own files or conducting symlink-based attacks. By creating a 274symlink to a file, the attacker may trick a trusted program into performing 275actions it shouldn't.</p> 276 277<p> Writable directories prevent the uninstall of an application from properly 278cleaning up all files associated with an application. Directories created by the 279system or root users <strong>should not</strong> be world writable.</p> 280 281<p>CTS tests help enforce this best practice by testing known directories.</p> 282 283<h3 id="config-files">Configuration files</h3> 284<p>Many drivers and services rely on configuration and data files stored in 285directories like /system/etc and various other directories in /data. If these 286files are processed by a privileged process and are world writable, then it 287could be possible for an app to exploit a vulnerability in the process by 288crafting malicious contents in the world-writable file.</p> 289 290<ol> 291<li>Configuration files used by privileged processes <strong>should not</strong> 292be world readable.</li> 293<li>Configuration files used by privileged processes <strong>must not</strong> be 294world writable.</li> 295</ol> 296 297<h3 id="native-code">Native code libraries</h3> 298<p>Any code used by privileged OEM processes must be in /vendor or /system; these 299filesystems are mounted read-only on boot. Any libraries used by system or other 300highly-privileged apps installed on the phone should also be in these 301filesystems. This can prevent a security vulnerability that could allow an 302attacker to control the code that a privileged process executes.</p> 303 304<ol> 305<li>All native code used by privileged OEM processes <strong>must be</strong> in /vendor or 306/system.</li> 307</ol> 308 309<h3 id="device-drivers">Device drivers</h3> 310<p>Only trusted code should have direct access to drivers. Where possible, the 311preferred architecture is to provide a single-purpose daemon that proxies calls 312to the driver and restrict access to the driver to that daemon.</p> 313 314<p>Driver device nodes <strong>should not</strong> be world readable or 315writable. CTS tests help enforce this best practice by checking for known 316instances of exposed drivers.</p> 317 318<h3 id="adb">ADB</h3> 319<p>ADB <strong>must be</strong> disabled by default and must require the user to turn it on 320before accepting connections.</p> 321 322<h3 id="unlockable-bootloaders">Unlockable bootloaders</h3> 323<p>Unlockable Android devices must securely erase all user data prior to being 324unlocked. The failure to properly delete all data on unlocking may allow a 325physically proximate attacker to gain unauthorized access to confidential 326Android user data. We've seen numerous instances where device manufacturers 327improperly implemented unlocking.</p> 328 329<p>Many Android devices support unlocking. This allows the device owner to modify 330the system partition and/or install a custom operating system. Common use cases 331for this include installing a third-party ROM, and/or doing systems-level 332development on the device.</p> 333 334<p>For example, on Google Nexus devices, an end user can run <code>fastboot oem 335unlock</code> to start the unlocking process. When an end user runs this command, 336the following message is displayed:</p> 337 338<blockquote> 339<strong>Unlock bootloader?</strong> 340 341<p>If you unlock the bootloader, you will be able to install custom operating 342system software on this phone.</p> 343 344<p>A custom OS is not subject to the same testing as the original OS, and can 345cause your phone and installed applications to stop working properly.</p> 346 347<p>To prevent unauthorized access to your personal data, unlocking the 348bootloader will also delete all personal data from your phone (a "factory data 349reset"). 350 351<p>Press the Volume Up/Down buttons to select Yes or No. Then press the Power 352button to continue.</p> 353 354<p><strong>Yes</strong>: Unlock bootloader (may void warranty)</p> 355 356<p><strong>No</strong>: Do not unlock bootloader and restart phone.</p> 357</blockquote> 358 359<p>A device that is unlocked may be subsequently relocked, by issuing the 360<code>fastboot oem lock</code> command. Locking the bootloader provides the same 361protection of user data with the new custom OS as was available with the 362original OEM OS. e.g. user data will be wiped if the device is unlocked again in 363the future.</p> 364 365<p>To prevent the disclosure of user data, a device that supports unlocking needs 366to implement it properly.</p> 367 368<p>A properly implemented unlocking process will have the following properties:</p> 369 370<ol> 371<li>When the unlocking command is confirmed by the user, the device MUST start an 372immediate data wipe. The "unlocked" flag MUST NOT be set until after the 373secure deletion is complete.</li> 374<li>If a secure deletion cannot be completed, the device MUST stay in a locked 375state.</li> 376<li>If supported by the underlying block device, 377<code>ioctl(BLKSECDISCARD)</code> or equivalent SHOULD be used. For eMMC 378devices, this means using a Secure Erase or Secure Trim command. For eMMC 4.5 379and later, this means doing a normal Erase or Trim followed by a Sanitize 380operation.</li> 381<li>If <code>BLKSECDISCARD</code> is NOT supported by the underlying block 382device, <code>ioctl(BLKDISCARD)</code> MUST be used instead. On eMMC devices, 383this is a normal Trim operation.</li> 384<li>If <code>BLKDISCARD</code> is NOT supported, overwriting the block devices 385with all zeros is acceptable.</li> 386<li>An end user MUST have the option to require that user data be wiped prior to 387flashing a partition. For example, on Nexus devices, this is done via the 388<code>fastboot oem lock</code> command.</li> 389<li>A device MAY record, via efuses or similar mechanism, whether a device was 390unlocked and/or relocked.</li> 391</ol> 392 393<p>These requirements ensure that all data is destroyed upon the completion of an 394unlock operation. Failure to implement these protections is considered a 395"moderate" level security vulnerability.</p> 396