Giter Club home page Giter Club logo

fans's Introduction

FANS: Fuzzing Android Native System Services

FANS is a fuzzing tool for fuzzing Android native system services. It contains four components: interface collector, interface model extractor, dependency inferer, and fuzzer engine.

For more details, please refer to our USENIX Security'20 paper.

You could follow the following steps to setup FANS. In the following, we use Pixel 2 XL to illustrate the instructions.

Prepare Host

Please prepare a server with

  • at least 1T disk (preferably SSD) as the following reasons
    • We should separate the AOSP projects with/without ASan enabled.
    • We need to save the logs.
    • etc.
  • many cores as compiling AOSP is time-consuming. The more cores, the better.

We suggest using FANS on Ubuntu. We tested it on Ubuntu 18.04.

Prepare Android Environment

Please refer to AOSP for

  • how to download AOSP source code
  • how to compile AOSP for the target mobile phone with the target version (e.g., Android 9.0.0_r46 for Pixel 2 XL)
  • how to compile AOSP with ASan enabled
  • how to flash devices

Suppose we have

Before building, we'd better modify some options in the following files to make fuzzing more convenient.

/path/to/aosp/build/core/main.mk

  • ro.adb.secure=0, which will disable adb authentication. Otherwise, every time we reflash the phone, we need to click the screen manually to trust the host. Disabling adb authentication will help us reflash the mobile automatically as we will reflash the mobile phone through adb.

  • persist.sys.disable_rescue=1, which will disable rescue party. For more details, please see https://source.android.com/devices/tech/debug/rescue-party. This will improve fuzzing efficiency.

# line 273
## before modifying
ifneq (,$(user_variant))
  # Target is secure in user builds.
  ADDITIONAL_DEFAULT_PROPERTIES += ro.secure=1
  ADDITIONAL_DEFAULT_PROPERTIES += security.perf_harden=1

  ifeq ($(user_variant),user)
    ADDITIONAL_DEFAULT_PROPERTIES += ro.adb.secure=1
  endif
## after modifying
ifneq (,$(user_variant))
  # Target is secure in user builds.
  ADDITIONAL_DEFAULT_PROPERTIES += ro.secure=1
  ADDITIONAL_DEFAULT_PROPERTIES += security.perf_harden=1

  ADDITIONAL_DEFAULT_PROPERTIES += ro.adb.secure=0
  ADDITIONAL_DEFAULT_PROPERTIES += persist.sys.disable_rescue=1

  #ifeq ($(user_variant),user)
  #  ADDITIONAL_DEFAULT_PROPERTIES += ro.adb.secure=1
  #endif

/path/to/aosp/build/make/target/product/core_minimal.mk

  • tombstoned.max_tombstone_count=99999, which will set the maximum number of tombstones to 99999.
# line 170
## before modifying
ifneq (,$(filter userdebug eng,$(TARGET_BUILD_VARIANT)))
PRODUCT_SYSTEM_DEFAULT_PROPERTIES += \
    tombstoned.max_tombstone_count=50
endif
## after modifying
ifneq (,$(filter userdebug eng,$(TARGET_BUILD_VARIANT)))
PRODUCT_SYSTEM_DEFAULT_PROPERTIES += \
    tombstoned.max_tombstone_count=99999
endif

Note, when flashing the image, you should use the correct adb and fastboot version corresponding to the Android version. So please install Android SDK according to the version of the target phone. For instance, we are testing Android 9.0.0_r46, so we install the Android SDK for Android 9.0. After installing the SDK, please create the following symbolic links

sudo ln -s /path/to/sdk/platform-tools/adb /usr/bin/fastboot
sudo ln -s /path/to/sdk/platform-tools/adb /usr/bin/adb

Here are some helpful instructions for flashing a device with ASan enabled.

############################# Flash factory image       #############################
# Before flashing the manually build image, 
# you should flash the mobile phone with the corresponding factory image.
# please refer to the offical website for flashing factory image.

############################# Flash AOSP image without ASan #############################

# we need to compile aosp in a bash environment
bash

cd /path/to/aosp
# prepare environment
source build/envsetup.sh
# select the target version.
# 50 corresponding to the aosp_taimen-userdebug
# you can use lunch to see the allowed choices.
lunch 50

# compile AOSP and save the compile commands
# replace the N_PROCS with the number you want, 
# e.g., make -j15 showcommands 2>&1 >cmd.txt
make -j [N_PROCS] showcommands 2>&1 >cmd.txt

## here, you should run your commands to flash the image.

############################# Flash AOSP image with ASan #############################

cd ..
# copy the entire project to another place.
cp /path/to/aosp /path/to/aosp_asan
cd /path/to/aosp_asan
source build/envsetup.sh
lunch 50

# compile the entire AOSP with ASan enabled
# replace the N_PROCS with the number you want, 
# e.g., SANITIZE_TARGET=address make -j15
SANITIZE_TARGET=address make -j [N_PROCS]

## here, you should run your commands to flash the image with ASan enabled.

Config FANS

Then we need to create a config file fans.cfg for FANS. You could utilize the template fans.template.cfg to set up your config. In detail, we need to config the following options of FANS.

  • fans_dir, FANS directory.
  • aosp_dir, AOSP directory.
  • aosp_sanitizer_dir, AOSP with ASan enabled directory.
  • aosp_compilation_cmd_file, the location of the AOSP compilation cmd file.
  • lunch_command, the lunch command, e.g., lunch 50 for aosp_taimen-userdebug.
  • aosp_clang_location, the location of clang used to compile AOSP, relative to aosp_dir, e.g., prebuilts/clang/host/linux-x86/clang-4691093/bin/clang++.real for Android 9.0.0_r46.
  • manually_build_clang_location, the location of clang manually built. For details, please refer to pre-process of the interface model extractor.
  • clang_plugin_option, the additional options appended to the compilation cmd to load the clang plugin.
  • service_related_file_collector_workdir, the work dir of the service-related file collector. Keep as default.
  • service_related_filepath_storage_location, store files related to service. Keep as default.
  • misc_parcel_related_function_storage_location, store misc functions that have a parcel parameter, e.g., setSchedPolicy(data). Keep as default.
  • special_parcelable_function_storage_location, store special functions of special parcelable structures. Keep as default.
  • aosp_compilation_cc1_cmd_file, store cc1 cmd. Keep as default.
  • already_preprocessed_files_storage_location, store already preprocessed files. Keep as default.
  • rough_interface_related_data_dir, store the data extracted during the pre-processing. This directory locates in the root dir of aosp. Its name is data.
  • already_parsed_interfaces_storage_location, store already parsed interfaces during the post process. Keep as default.
  • interface_model_extractor_tmp_dir, the tmp dir used by interface model extractor. Keep as default.
  • interface_model_extractor_dir, interface model extractor work dir. Keep as default.
  • interface_dependency_dir, interface dependency dir. Keep as default.

Collect Interface and Related Files

Please see Service Related File Collector.

Extract Interface Model

Please see Interface Model Extractor.

Infer Dependency

Please see Dependency Inferer.

Start Fuzzing

Please see Fuzzer Engine.

Results

workdir contains the following results, including

  • service-related files information, located in workdir/service-related-file.
  • interface model, located in workdir/interface-model-extractor/model.
  • simplified interface dependency, located in workdir/interface-dependency.

For details, you can refer to the workdir.

As for the fuzzing results, you can refer to Fuzzer Manager.

If you find bugs by running FANS, please let us know by sending a PR.

TODO

See TODO.

Disclaimer

I am not sure what will happen to your device when using FANS. So good luck!

Contact

Baozheng Liu ([email protected])

fans's People

Contributors

iromise avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

fans's Issues

clang exec error

when I try to exec
build/bin/clang++ -cc1 -triple aarch64-unknown-linux-android10000 -emit-llvm-bc -flto -flto-unit -disable-free -disable-llvm-verifier -discard-value-names -main-file-name IKeystoreCertificateChainCallback.cpp -mrelocation-model pic -pic-level 2 -mthread-model posix -mframe-pointer=non-leaf -relaxed-aliasing -fno-rounding-math -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu generic -target-feature +neon -target-abi aapcs -mllvm -aarch64-fix-cortex-a53-835769=1 -fallow-half-arguments-and-returns -dwarf-column-info -fno-split-dwarf-inlining -debug-info-kind=limited -dwarf-version=4 -debugger-tuning=gdb -v -ffunction-sections -fdata-sections -nostdsysteminc -resource-dir prebuilts/clang/host/linux-x86/clang-r383902b1/lib64/clang/11.0.2 -dependency-file out/soong/.intermediates/system/security/keystore/libkeystore_aidl/android_arm64_armv8-a_shared_cfi/obj/.intermediates/system/security/keystore/libkeystore_aidl/android_arm64_armv8-a_shared_cfi/gen/aidl/system/security/keystore/binder/android/security/keystore/IKeystoreCertificateChainCallback.o.d -MT out/soong/.intermediates/system/security/keystore/libkeystore_aidl/android_arm64_armv8-a_shared_cfi/obj/.intermediates/system/security/keystore/libkeystore_aidl/android_arm64_armv8-a_shared_cfi/gen/aidltics-show-option -fcolor-diagnostics -vectorize-loops -vectorize-slp -fsplit-lto-unit -faddrsig -o out/soong/.intermediates/system/security/keystore/libkeystore_aidl/android_arm64_armv8-a_shared_cfi/obj/.intermediates/system/security/keystore/libkeystore_aidl/android_arm64_armv8-a_shared_cfi/gen/aidl/system/security/keystore/binder/android/security/keystore/IKeystoreCertificateChainCallback.o -x c++ out/soong/.intermediates/system/security/keystore/libkeystore_aidl/android_arm64_armv8-a_shared_cfi/gen/aidl/system/security/keystore/binder/android/security/keystore/IKeystoreCertificateChainCallback.cpp
The output error is :
clang (LLVM option parsing): Unknown command line argument '-aarch64-fix-cortex-a53-835769=1'. Try: 'clang (LLVM option parsing) --help'
clang (LLVM option parsing): Did you mean '--arc-opt-max-ptr-states=1'?
clang (LLVM option parsing) --help
bash: syntax error near unexpected token `LLVM'

And I just use the right aosp clang corresponding to the llvm clang and compile them. Use them to compile the cpp of aosp.

KeyError: ‘interfaceName’

sudo sh postprocess.sh
Traceback (most recent call last):
File "parse_interface.py", line 138, in
parse_interfaces()
File "parse_interface.py", line 117, in parse_interfaces
parse_one_interface(filename)
File "parse_interface.py", line 92, in parse_one_interface
interfaceName =func2svc[funcname]["interfaceName"]
KeyError: 'interfaceName'
ERROR:main:readDecryptHandleFromParcelData.xml
ERROR:main:Unexpected thing meeted when parsing structure.
cp: cannot stat 'manually/flattenable/reply/': No such file or directory
cp: cannot stat 'manually/light_flattenable/data/
': No such file or directory
cp: cannot stat 'manually/light_flattenable/reply/*': No such file or directory

The file doesn't exist: service_related_file.txt

File "gen_all_related_cc1_cmd.py", line 33, in main
service_related_filepath_storage_location).read().strip().split("\n")
IOError: [Errno 2] No such file or directory: u'/fans/workdir/service-related-file/service_related_file.txt'

extract_from_ast error

when execute python extract_from_ast.py, the error happen:

Do you want to remove all of the files and extract again?y/nn Processing file art/runtime/entrypoints/entrypoint_utils.cc b'including device/generic/car/vendorsetup.sh\nincluding device/generic/mini-emulator-arm64/vendorsetup.sh\nincluding device/generic/mini-emulator-armv7-a-neon/vendorsetup.sh\nincluding device/generic/mini-emulator-mips/vendorsetup.sh\nincluding device/generic/mini-emulator-mips64/vendorsetup.sh\nincluding device/generic/mini-emulator-x86/vendorsetup.sh\nincluding device/generic/mini-emulator-x86_64/vendorsetup.sh\nincluding device/generic/uml/vendorsetup.sh\nincluding device/google/bonito/vendorsetup.sh\nincluding device/google/crosshatch/vendorsetup.sh\nincluding device/google/cuttlefish/vendorsetup.sh\nincluding device/google/marlin/vendorsetup.sh\nincluding device/google/muskie/vendorsetup.sh\nincluding device/google/taimen/vendorsetup.sh\nincluding device/linaro/hikey/vendorsetup.sh\nincluding sdk/bash_completion/adb.bash\n\n============================================\nPLATFORM_VERSION_CODENAME=REL\nPLATFORM_VERSION=9\nTARGET_PRODUCT=aosp_taimen\nTARGET_BUILD_VARIANT=userdebug\nTARGET_BUILD_TYPE=release\nTARGET_ARCH=arm64\nTARGET_ARCH_VARIANT=armv8-a\nTARGET_CPU_VARIANT=cortex-a73\nTARGET_2ND_ARCH=arm\nTARGET_2ND_ARCH_VARIANT=armv8-a\nTARGET_2ND_CPU_VARIANT=cortex-a73\nHOST_ARCH=x86_64\nHOST_2ND_ARCH=x86\nHOST_OS=linux\nHOST_OS_EXTRA=Linux-4.15.0-107-generic-x86_64-Ubuntu-18.04.4-LTS\nHOST_CROSS_OS=windows\nHOST_CROSS_ARCH=x86\nHOST_CROSS_2ND_ARCH=x86_64\nHOST_BUILD_TYPE=release\nBUILD_ID=PQ3A.190801.002\nOUT_DIR=out\n============================================\n/bin/bash: line 3: -load: command not found\n' exception meeted.

Android 11 compatibility problems

Hi,

I am trying to use this tool with a recent AOSP release (android-11.0.0_r37), and I'm having trouble getting the BinderIface plugin to compile. LLVM has changed, so I have made the following adjustments to CMakeLists.txt

--- a/interface-model-extractor/pre-process/BinderIface/CMakeLists.txt
+++ b/interface-model-extractor/pre-process/BinderIface/CMakeLists.txt
@@ -9,10 +9,10 @@ if( NOT MSVC ) # MSVC mangles symbols differently, and
   endif()
 endif()
 
-add_llvm_loadable_module(BinderIface BinderIface.cpp PLUGIN_TOOL clang)
+add_llvm_library(BinderIface MODULE BinderIface.cpp PLUGIN_TOOL clang)
 SET(CMAKE_INSTALL_RPATH "")
 if(LLVM_ENABLE_PLUGINS AND (WIN32 OR CYGWIN))
-  target_link_libraries(BinderIface PRIVATE
+  target_link_libraries(BinderIface
     clangAST
     clangBasic
     clangFrontend

Additionally, I had to clone LLVM using these instructions: https://android.googlesource.com/toolchain/llvm_android/+/master/README.md#instructions-to-rebuild-a-particular-toolchain-release
This means LLVM source code is coming from a different repo, and the build system is different. I believe we must do this because the repo withing the FANS docs is outdated, and does not contain the current version used by AOSP 11.

After making these changes, I can build clang, but I cannot get any plugins to build. Can you please provide updated instructions?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.