自 2025 年 3 月 27 日起,我们建议您使用 android-latest-release
而非 aosp-main
构建 AOSP 并为其做出贡献。如需了解详情,请参阅 AOSP 的变更。
应用安全性最佳实践
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
本部分包含可确保 Android 设备上应用安全性的建议。
源代码审核
进行源代码审核可以发现大量安全问题,包括本文档中指出的问题。Android 强烈建议采用手动和自动两种方式审核源代码。
- 在进行审核时,应遵循全面的安全指导以确保覆盖率。利用相关的内部或外部标准来确保一致且全面的审核。
- 对所有使用 Android SDK 的应用代码运行 Linter(例如 Android Studio Liner),并更正发现的所有问题。
- 使用可以发现内存管理问题(例如,缓冲区溢出和差一错误)的自动工具分析原生代码。
- Android 构建系统支持多种 LLVM 排错程序,例如可用于对内存相关问题进行运行时分析的 AddressSanitizer 和 UndefinedBehaviorSanitizer。如果与模糊测试配合使用(在 Android 中通过 libFuzzer 来实现),则排错程序可以发现需要进一步调查的异常极端情况。
- 经验丰富的安全评估员应审核风险较高的代码,例如加密、付款处理和 PII 处理。
自动测试
自动测试功能有助于发现大量安全问题,且应定期执行。
- 在整个开发流程中定期运行最新版 CTS,以便尽早发现问题并缩短解决问题所需的时间。在自动构建流程(每天会构建多次)的连续集成期间,Android 会使用 CTS。
- 使接口的安全性测试实现自动化,包括使用格式有误的输入内容进行测试(模糊测试)。Android 构建系统支持使用 libFuzzer 来编写模糊测试。
漏洞扫描
漏洞扫描有助于确保预安装的应用不存在已知的安全漏洞。高级检测可以减少解决这些漏洞以及防止用户和设备面临风险所需的时间和成本。
- 使用行业认可的应用漏洞扫描工具扫描所有预安装的应用,并解决检测到的漏洞。
潜在有害应用
请务必确保设备上的预安装应用不是潜在有害应用 (PHA)。您要为设备上安装的所有应用的行为负责。在设备发布之前,扫描所有预加载的应用,查找是否有漏洞。
如需详细了解 PHA 以及 Google 如何在 Play 商店中防范它们,请参阅 Google Play 保护机制开发者文档。
应用安装和权限
为预安装应用授予过多权限可能会带来安全风险。尽可能减少为预安装应用授予的必要权限数量,并确保这些应用无法获取不必要的权限。AndroidManifest.xml 对应用权限进行了说明。
- 不要向预安装的应用授予不必要的权限。仔细检查具有系统权限的应用,因为它们可能具有非常敏感的权限。
- 确保请求的所有权限都是相关的,并且是使用该特定应用的功能所必需的。
- 确保针对所有使用
INSTALL_PACKAGES
权限的预安装应用提供用户披露功能。
- 确保开发者遵守合同规定,未将任何应用安装为 UID 0。
- 评估在通过开发者网络安装的所有应用清单中声明的权限。
- 确保开发者遵守合同规定,在将应用提供给设备之前,使用 Google Safe Browsing API 扫描自动更新程序和安装程序应用的所有下载网址。
应用签名
应用签名在保障设备安全方面发挥着重要作用,可用于进行权限检查以及软件更新。在选择为应用签名使用的密钥时,请务必考虑应用是仅在一台设备上使用,还是供多台设备共用。
- 确保不使用众所周知的密钥(例如 AOSP 开发者密钥)为应用签名。
- 确保按照与处理敏感密钥方面的业界标准做法一致的方式管理用于为应用签名的密钥,包括使用能够提供有限、可审核访问权限的硬件安全模块 (HSM)。
- 确保不使用平台密钥为应用签名。这样做可以向应用授予平台签名权限,这些权限非常强大,只能由操作系统的一些组件使用。系统应用应使用特许权限。
- 确保不使用不同的密钥为软件包名称相同的应用签名。在针对不同的设备开发应用时,经常会出现这种情况,尤其是在使用平台密钥时。如果应用独立于设备,则在多台设备之间使用相同的密钥。如果应用是特定设备专用的,则按设备和密钥创建独一无二的软件包名称。
隔离应用和进程
如果使用得当,Android 沙盒模型可为应用和进程提供额外的安全性。
隔离 root 进程
Root 进程是最常受到提权攻击的目标,因此减少 Root 进程数量有助于降低提权风险。
- 确保尽可能减少设备上作为 root 代码运行的必要代码的数量。尽可能使用常规 Android 进程而非 root 进程。如果某个进程必须要在设备上作为 root 进程运行,请将该进程记录在 AOSP 功能请求中,以便对其进行公开审核。
- 应尽可能将 root 代码与不可信数据隔离开来,并尽可能通过进程间通信 (IPC) 访问 root 代码。例如,将 root 功能缩减成可通过 Binder 访问的小型服务,并将这个具有签名权限的服务提供给网络流量处理权限很小或没有此类权限的应用。
- Root 进程不得通过网络套接字进行监听。
- root 进程不得包含通用运行时(例如 Java 虚拟机)。
隔离系统应用
一般而言,预安装的应用不应使用共用系统唯一标识符 (UID) 运行。如果某个应用必须使用共用系统 UID 或其他特权服务(例如,电话服务),那么该应用不应导出由用户安装的第三方应用可访问的任何服务、广播接收器或 content provider。
- 确保尽可能减少设备上作为系统代码运行的必要代码的数量。尽可能通过 Android 进程自身的 UID 使用此类进程,而非重复使用系统 UID。
- 应尽可能将系统代码与不可信数据隔离开来,并且系统代码应尽可能仅向其他可信进程提供 IPC。
- 系统进程不得通过网络套接字进行监听。这是 CTS 要求。
隔离进程
Android 应用沙盒要求应用与系统中的其他进程(包括 root 进程和调试程序)隔离开来。除非应用或用户特意启用了调试功能,否则任何应用都不得违反这一要求。
- 确保 root 进程无法访问各个应用数据文件夹内的数据,使用已记录的 Android 调试方法时除外。
- 确保 root 进程无法访问应用内存,使用已记录的 Android 调试方法时除外。
- 确保设备上没有任何会访问其他应用/进程的数据或内存的应用。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-03-26。
[null,null,["最后更新时间 (UTC):2025-03-26。"],[],[],null,["# App security best practices\n\nThis section contains recommendations to ensure the security of apps on\nAndroid devices.\n\nSource code review\n------------------\n\nSource code review can detect a broad range of security issues, including\nthose identified in this document. Android strongly encourages both manual\nand automated source code review.\n\n- Follow comprehensive security guidance when conducting reviews to ensure coverage. Utilize relevant internal or external standards to ensure consistent and complete reviews.\n- Run a linter, such as the [Android Studio linter](https://developer.android.com/studio/write/lint), on all app code using the Android SDK and correct any identified issues.\n- Analyze native code using an automated tool that can detect memory management issues, such as buffer overflows and off-by-one errors.\n- The Android build system supports many of the LLVM sanitizers, such as [AddressSanitizer](/docs/security/test/asan) and [UndefinedBehaviorSanitizer](/docs/security/test/sanitizers#undefinedbehaviorsanitizer), which can be used for runtime analysis of memory-related issues. Combined with fuzzing, supported in Android through [libFuzzer](/docs/security/test/libfuzzer), sanitizers can uncover unusual edge cases requiring further investigation.\n- A knowledgeable security assessor should review higher risk code, such as crypto, payment processing, and PII processing.\n\nAutomated testing\n-----------------\n\nAutomated testing can help detect a broad range of security issues and\nshould be performed regularly.\n\n- Run the latest version of [CTS](/docs/compatibility/cts) regularly throughout the development process to detect problems early and reduce time to correction. Android uses CTS as part of continuous integration in our automated build process, which builds multiple times per day.\n- Automate security testing of interfaces, including testing with malformed inputs (fuzz testing). Android's build system supports [libFuzzer](/docs/security/test/libfuzzer) for writing fuzz tests.\n\nVulnerability scanning\n----------------------\n\nVulnerability scanning can help ensure that pre-installed apps are free of\nknown security vulnerabilities. Advanced detection can reduce the time and\ncost required with addressing these vulnerabilities and preventing risk to\nusers and devices.\n\n- Scan all pre-installed apps using an industry-recognized app vulnerability scanning tool and address detected vulnerabilities.\n\nPotentially Harmful Applications\n--------------------------------\n\nIt is important to ensure that the pre-installed apps on your device aren't\n[Potentially\nHarmful Applications](https://developers.google.com/android/play-protect/phacategories) (PHAs). You are responsible for the behavior of all\napps that are included on your devices. Prior to device launch, scan all\npre-loaded apps for vulnerabilities.\n\nFor more information about PHAs and how Google is combating them in the\nPlay store see the [Google Play Protect\ndeveloper documentation](https://developers.google.com/android/play-protect/).\n\nApp installation and permissions\n--------------------------------\n\nExcessive permissions for pre-installed apps can create a security risk.\nRestrict pre-installed apps to the minimum necessary permissions and ensure they\ndon't have access to unnecessary permissions or privileges. App permissions are\ndescribed in the [AndroidManifest.xml](https://android.googlesource.com/platform/frameworks/base/+/refs/heads/android16-release/core/res/AndroidManifest.xml).\n\n- Don't grant unnecessary permissions or privileges to pre-installed apps. Thoroughly review apps with system privileges as they can have very sensitive permissions.\n- Ensure that all permissions requested are relevant and necessary for the functionality of that specific app.\n- Ensure there is user disclosure for all pre-installed apps that use the `INSTALL_PACKAGES` permission.\n- Ensure that the developer is contractually obligated not to install any apps as UID 0.\n- Evaluate permissions declared in the manifest of all apps to be installed through the developer's network.\n- Ensure that the developer is contractually obligated to scan all download URLs of auto-updater and installer apps with [Google Safe Browsing API](https://developers.google.com/safe-browsing/) before serving apps to the device.\n\nApp signing\n-----------\n\nApp signatures play an important role in device security and are used for\npermissions checks and software updates. When selecting a key to use for\nsigning apps, it is important to consider whether an app is available\nonly on a single device or common across multiple devices.\n\n- Ensure that apps aren't signed with a key that is publicly known, such as the AOSP developer key.\n- Ensure that keys used to sign apps are managed in a manner consistent with industry-standard practices for handling sensitive keys, including an hardware security module (HSM) that provides limited, auditable access.\n- Ensure that apps aren't signed with the platform key. Doing so gives an app access to platform signature permissions, which are very powerful and only intended to be used by components of the operating system. System apps should use privileged permissions.\n- Ensure that apps with the same package name aren't signed with different keys. This often occurs when creating an app for different devices, especially when using the platform key. If the app is device-independent, use the same key across devices. If the app is device-specific, create unique package names per device and key.\n\nIsolate apps and processes\n--------------------------\n\nThe Android [sandboxing model](/docs/security/app-sandbox)\nprovides extra security around apps and processes when used correctly.\n\n### Isolate root processes\n\nRoot processes are the most frequent target of privilege escalation attacks;\nreducing the number of root processes reduces risk of privilege escalation.\n\n- Ensure that devices run the minimum necessary code as root. Where possible, use a regular Android process rather than a root process. If a process must run as root on a device, document the process in an AOSP feature request so it can be publicly reviewed.\n- Where possible, root code should be isolated from untrusted data and accessed through interprocess communication (IPC). For example, reduce root functionality to a small service accessible through Binder and expose the service with signature permission to an app with low or no privileges to handle network traffic.\n- Root processes must not listen on a network socket.\n- Root processes must not include a general-purpose runtime, such as a Java VM.\n\n### Isolate system apps\n\nIn general, pre-installed apps shouldn't run with the shared system unique\nidentifier (UID). If it is necessary for an app to use the shared UID of\nsystem or another privileged service (e.g., phone), the app shouldn't export\nany services, broadcast receivers, or content providers that can be accessed\nby third-party apps installed by users.\n\n- Ensure devices run the minimum necessary code as system. Where possible, use an Android process with its own UID rather than reusing the system UID.\n- Where possible, system code should be isolated from untrusted data and expose IPC only to other trusted processes.\n- System processes must not listen on a network socket. This is a CTS requirement.\n\n### Isolate processes\n\nThe Android Application Sandbox provides apps with an expectation of\nisolation from other processes on the system, including root processes and\ndebuggers. Unless debugging is specifically enabled by the app and the user,\nno app should violate that expectation.\n\n- Ensure root processes don't access data within individual app data folders, unless using a documented Android debugging method.\n- Ensure root processes don't access memory of apps, unless using a documented Android debugging method.\n- Ensure devices don't include any app that accesses data or memory of other apps or processes."]]