自 2025 年 3 月 27 日起,我们建议您使用 android-latest-release
而非 aosp-main
构建 AOSP 并为其做出贡献。如需了解详情,请参阅 AOSP 的变更。
AIDL 模糊测试
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
模糊测试工具通过生成的桩来导入或调用远程服务,从而充当远程服务的客户端:
使用 C++ API:
#include <fuzzbinder/libbinder_ndk_driver.h>
#include <fuzzer/FuzzedDataProvider.h>
#include <android-base/logging.h>
#include <android/binder_interface_utils.h>
using android::fuzzService;
using ndk::SharedRefBase;
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
auto binder = ndk::SharedRefBase::make<MyService>(...);
fuzzService(binder->asBinder().get(), FuzzedDataProvider(data, size));
return 0;
}
使用 Rust API:
#![allow(missing_docs)]
#![no_main]
#[macro_use]
extern crate libfuzzer_sys;
use binder::{self, BinderFeatures, Interface};
use binder_random_parcel_rs::fuzz_service;
fuzz_target!(|data: &[u8]| {
let service = BnTestService::new_binder(MyService, BinderFeatures::default());
fuzz_service(&mut service.as_binder(), data);
});
AIDL 服务模糊测试框架
如上例所示,系统会在模糊测试工具中调用 fuzzService,后者会接受 IBinder (Service) 和 dataProvider 作为输入参数。它会先使用数据提供程序来初始化一个随机 Parcel 对象,然后通过使用输入 Parcel 对远程服务调用事务方法,最后将回复传入回复 Parcel。
构建和运行模糊测试工具
默认情况下,基于覆盖率来构建模糊测试工具。
建议使用以下排错程序来发现内存问题。hwaddress
排错程序仅在 arm
架构上运行:
SANITIZE_HOST=address SANITIZE_TARGET=hwaddress
与 libFuzzer
一起运行时,语料库(即目录)可能会在 Android.bp
文件中指定,并且您可将此目录传递给模糊测试工具。一些模糊测试工具还会在其 Android.bp
文件中指定 dictionary:
,您可以使用 -dict path/to/dict
将其传递给 libFuzzer。如需了解更多选项,请参阅官方 libFuzzer 文档。
如需在设备上运行模糊测试工具,请运行 adb sync data
,然后运行 adb shell data/fuzz/arch/name/name
。如需在主机上运行模糊测试工具,请运行 $ANDROID_HOST_OUT/fuzz/arch/name/name
。
新服务或现有服务的推荐模糊测试工具
构建系统会检查是否每项 AOSP Binder 服务在服务模糊测试工具绑定中都有对应的模糊测试工具条目。模糊测试工具绑定测试会检查 service_contexts
中的每个服务是否都有模糊测试工具。如果找不到新服务对应的模糊测试工具或异常,则表示存在构建错误。
可以通过添加以下代码来编写自动 C++ 服务模糊测试工具(尚不支持 Java 和 Rust 模糊测试工具):
Android.bp
中的 cc_fuzz
条目,它用于定义模糊测试工具模块。cc_default
模块 service_fuzzer_defaults
具有 fuzzService
所需的依赖项。
- 服务专用依赖项应添加为库或源代码。
- 用于构造服务并调用
fuzzService
的主文件
如需详细了解如何使用 cc_fuzz
,请参阅通过 libFuzzer 进行模糊测试文档。如需解决构建错误,请使用新的服务和模糊测试工具名称更新绑定。对于 Java 或 Rust 服务,模糊测试工具列表可以为空。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-03-26。
[null,null,["最后更新时间 (UTC):2025-03-26。"],[],[],null,["# AIDL fuzzing\n\nThe fuzzer behaves as a client for the remote service by importing or invoking\nit through the generated stub:\n\nUsing C++ API: \n\n #include \u003cfuzzbinder/libbinder_ndk_driver.h\u003e\n #include \u003cfuzzer/FuzzedDataProvider.h\u003e\n\n #include \u003candroid-base/logging.h\u003e\n #include \u003candroid/binder_interface_utils.h\u003e\n\n using android::fuzzService;\n using ndk::SharedRefBase;\n\n extern \"C\" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {\n auto binder = ndk::SharedRefBase::make\u003cMyService\u003e(...);\n\n fuzzService(binder-\u003easBinder().get(), FuzzedDataProvider(data, size));\n\n return 0;\n }\n\nUsing Rust API: \n\n #![allow(missing_docs)]\n #![no_main]\n #[macro_use]\n extern crate libfuzzer_sys;\n\n use binder::{self, BinderFeatures, Interface};\n use binder_random_parcel_rs::fuzz_service;\n\n fuzz_target!(|data: &[u8]| {\n let service = BnTestService::new_binder(MyService, BinderFeatures::default());\n fuzz_service(&mut service.as_binder(), data);\n });\n\nFramework to fuzz AIDL services\n-------------------------------\n\nAs shown in the example above,\n[fuzzService](https://cs.android.com/android/platform/superproject/+/android-latest-release:frameworks/native/libs/binder/tests/parcel_fuzzer/libbinder_driver.cpp)\nis called in the fuzzer and takes in an IBinder (Service) and dataProvider as\ninput parameters. It first initializes a random Parcel object using the data\nprovider and call the transact method on the remote service by using the input\nparcel, and finally get the reply into a reply parcel.\n\nBuild and run fuzzers\n---------------------\n\nFuzzers are built with coverage by default.\n\nThe following sanitizers are recommended to discover memory issues.\n`hwaddress` sanitizers only run on `arm` architecture: \n\n SANITIZE_HOST=address SANITIZE_TARGET=hwaddress\n\nWhen running with `libFuzzer`, a corpus, which is a directory, may be specified\nin the `Android.bp` file, and you can pass this directory to the fuzzer. Some\nfuzzers also specify a `dictionary:` in their `Android.bp` file, and you can\npass this to libFuzzer with `-dict `\u003cvar translate=\"no\"\u003epath/to/dict\u003c/var\u003e. For\nmore options, see the\n[official libFuzzer documentation](https://llvm.org/docs/LibFuzzer.html).\n\nTo run fuzzers on device, run `adb sync data` and then\n`adb shell data/fuzz/`\u003cvar translate=\"no\"\u003earch\u003c/var\u003e`/`\u003cvar translate=\"no\"\u003ename\u003c/var\u003e`/`\u003cvar translate=\"no\"\u003ename\u003c/var\u003e.\nTo run fuzzers on host, run\n`$ANDROID_HOST_OUT/`\u003cvar translate=\"no\"\u003efuzz\u003c/var\u003e`/`\u003cvar translate=\"no\"\u003earch\u003c/var\u003e`/`\u003cvar translate=\"no\"\u003ename\u003c/var\u003e`/`\u003cvar translate=\"no\"\u003ename\u003c/var\u003e.\n\nRecommend fuzzers for new or existing services\n----------------------------------------------\n\nThe build system checks whether every AOSP binder service has a fuzzer entry in\n[service fuzzer bindings](https://cs.android.com/android/platform/superproject/+/android-latest-release:system/sepolicy/build/soong/service_fuzzer_bindings.go).\nThe Fuzzer binding test checks that every service in `service_contexts` has a\nfuzzer. If a fuzzer or exception isn't found for a new service, there's a build\nerror.\n\nAn automatic C++ service fuzzer can be written by adding the following (Java and\nRust fuzzers are not yet supported):\n\n- A `cc_fuzz` entry in `Android.bp` to define the fuzzer module. The `cc_default` module `service_fuzzer_defaults` has dependencies required for `fuzzService`.\n- Service-specific dependencies should be added as a library or as sources.\n- A main file that constructs your service and calls `fuzzService`\n\nFor detailed instructions on using `cc_fuzz`, see the\n[Fuzzing with libFuzzer](/docs/security/test/libfuzzer)\ndocumentation. To resolve build error, update bindings with the new service and\nfuzzer names. For Java or Rust services, the fuzzer list can be empty."]]