From e54e8d4ebf54513ed478fe5818fa0cd3b722c792 Mon Sep 17 00:00:00 2001 From: Stephen Crane Date: Wed, 14 Apr 2021 20:57:50 +0000 Subject: [PATCH] Trusty: Move tipc fuzzer connection to end of iteration We detect a TA crash by not being able to reconnect to its channel. We were previously connecting to the TA at the beginning of each fuzz iteration, but this results in only detecting a crash on the following iteration. By moving this connection to the end of the fuzz iteration, we can detect a crash corresponding to the correct fuzz iteration and libFuzzer will produce the correct crashing input. Test: /data/fuzz/arm64/trusty_keymaster_fuzzer/trusty_keymaster_fuzzer Bug: 185407818 Change-Id: I6808c72611fcabab5b314218f8b588dd7d944188 --- trusty/fuzz/include/trusty/fuzz/utils.h | 1 + trusty/fuzz/tipc_fuzzer.cpp | 26 +++++++++++-------------- trusty/fuzz/utils.cpp | 4 ++++ 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/trusty/fuzz/include/trusty/fuzz/utils.h b/trusty/fuzz/include/trusty/fuzz/utils.h index bca84e96d..c90641258 100644 --- a/trusty/fuzz/include/trusty/fuzz/utils.h +++ b/trusty/fuzz/include/trusty/fuzz/utils.h @@ -34,6 +34,7 @@ class TrustyApp { android::base::Result Connect(); android::base::Result Read(void* buf, size_t len); android::base::Result Write(const void* buf, size_t len); + void Disconnect(); android::base::Result GetRawFd(); diff --git a/trusty/fuzz/tipc_fuzzer.cpp b/trusty/fuzz/tipc_fuzzer.cpp index 325894470..f265cedb6 100644 --- a/trusty/fuzz/tipc_fuzzer.cpp +++ b/trusty/fuzz/tipc_fuzzer.cpp @@ -41,6 +41,7 @@ using android::trusty::fuzz::TrustyApp; #error "Binary file name must be parameterized using -DTRUSTY_APP_FILENAME." #endif +static TrustyApp kTrustyApp(TIPC_DEV, TRUSTY_APP_PORT); static std::unique_ptr record; extern "C" int LLVMFuzzerInitialize(int* /* argc */, char*** /* argv */) { @@ -52,8 +53,7 @@ extern "C" int LLVMFuzzerInitialize(int* /* argc */, char*** /* argv */) { } /* Make sure lazy-loaded TAs have started and connected to coverage service. */ - TrustyApp ta(TIPC_DEV, TRUSTY_APP_PORT); - auto ret = ta.Connect(); + auto ret = kTrustyApp.Connect(); if (!ret.ok()) { std::cerr << ret.error() << std::endl; exit(-1); @@ -79,22 +79,18 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { ExtraCounters counters(record.get()); counters.Reset(); - TrustyApp ta(TIPC_DEV, TRUSTY_APP_PORT); - auto ret = ta.Connect(); + auto ret = kTrustyApp.Write(data, size); + if (ret.ok()) { + ret = kTrustyApp.Read(&buf, sizeof(buf)); + } + + // Reconnect to ensure that the service is still up + kTrustyApp.Disconnect(); + ret = kTrustyApp.Connect(); if (!ret.ok()) { std::cerr << ret.error() << std::endl; android::trusty::fuzz::Abort(); } - ret = ta.Write(data, size); - if (!ret.ok()) { - return -1; - } - - ret = ta.Read(&buf, sizeof(buf)); - if (!ret.ok()) { - return -1; - } - - return 0; + return ret.ok() ? 0 : -1; } diff --git a/trusty/fuzz/utils.cpp b/trusty/fuzz/utils.cpp index 35263373b..bb096beab 100644 --- a/trusty/fuzz/utils.cpp +++ b/trusty/fuzz/utils.cpp @@ -127,6 +127,10 @@ Result TrustyApp::GetRawFd() { return ta_fd_; } +void TrustyApp::Disconnect() { + ta_fd_.reset(); +} + void Abort() { PrintTrustyLog(); exit(-1);