Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 49560fa9 authored by Andy Hung's avatar Andy Hung Committed by Gerrit Code Review
Browse files

Merge "Added media_log_fuzzer"

parents 453028a2 ec2a22e4
Loading
Loading
Loading
Loading
+33 −0
Original line number Diff line number Diff line
cc_fuzz {
    name: "media_log_fuzzer",
    static_libs: [
        "libmedialogservice",
    ],
    srcs: [
        "media_log_fuzzer.cpp",
    ],
    header_libs: [
        "libmedia_headers",
    ],
    shared_libs: [
        "libaudioutils",
        "libbinder",
        "liblog",
        "libmediautils",
        "libnblog",
        "libutils",
    ],
    include_dirs: [
        "frameworks/av/services/medialog",
    ],
    cflags: [
        "-Werror",
        "-Wall",
    ],
    fuzz_config: {
        cc: [
            "android-media-fuzzing-reports@google.com",
        ],
        componentid: 155276,
    },
}
+50 −0
Original line number Diff line number Diff line
# Fuzzer for libmedialogservice

## Plugin Design Considerations
The fuzzer plugin for libmedialogservice is designed based on the understanding of the
service and tries to achieve the following:

##### Maximize code coverage
The configuration parameters are not hardcoded, but instead selected based on
incoming data. This ensures more code paths are reached by the fuzzer.

medialogservice supports the following parameters:
1. Writer name (parameter name: `writerNameIdx`)
2. Log size (parameter name: `logSize`)
3. Enable dump before unrgister API (parameter name: `shouldDumpBeforeUnregister`)
5. size of string for log dump (parameter name: `numberOfLines`)

| Parameter| Valid Values| Configured Value|
|------------- |-------------| ----- |
| `writerNameIdx` | 0. `0` 1. `1` | Value obtained from FuzzedDataProvider |
| `logSize` | In the range `256 to 65536` | Value obtained from FuzzedDataProvider |
| `shouldDumpBeforeUnregister` | 0. `0` 1. `1` | Value obtained from FuzzedDataProvider |
| `numberOfLines` | In the range `0 to 65535` | Value obtained from FuzzedDataProvider |

This also ensures that the plugin is always deterministic for any given input.

## Build

This describes steps to build media_log_fuzzer binary.

### Android

#### Steps to build
Build the fuzzer
```
  $ mm -j$(nproc) media_log_fuzzer
```

#### Steps to run
Create a directory CORPUS_DIR and copy some files to that folder
Push this directory to device.

To run on device
```
  $ adb sync data
  $ adb shell /data/fuzz/arm64/media_log_fuzzer/media_log_fuzzer CORPUS_DIR
```

## References:
 * http://llvm.org/docs/LibFuzzer.html
 * https://github.com/google/oss-fuzz
+76 −0
Original line number Diff line number Diff line
/**
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <binder/IMemory.h>
#include <binder/MemoryDealer.h>
#include <private/android_filesystem_config.h>
#include "MediaLogService.h"
#include "fuzzer/FuzzedDataProvider.h"

constexpr const char* kWriterNames[2] = {"FastMixer", "FastCapture"};
constexpr size_t kMinSize = 0x100;
constexpr size_t kMaxSize = 0x10000;
constexpr size_t kLogMemorySize = 400 * 1024;
constexpr size_t kMaxNumLines = USHRT_MAX;

using namespace android;

class MediaLogFuzzer {
   public:
    void init();
    void process(const uint8_t* data, size_t size);

   private:
    sp<MemoryDealer> mMemoryDealer = nullptr;
    sp<MediaLogService> mService = nullptr;
};

void MediaLogFuzzer::init() {
    setuid(AID_MEDIA);
    mService = new MediaLogService();
    mMemoryDealer = new MemoryDealer(kLogMemorySize, "MediaLogFuzzer", MemoryHeapBase::READ_ONLY);
}

void MediaLogFuzzer::process(const uint8_t* data, size_t size) {
    FuzzedDataProvider fuzzedDataProvider(data, size);
    size_t writerNameIdx =
        fuzzedDataProvider.ConsumeIntegralInRange<size_t>(0, std::size(kWriterNames) - 1);
    bool shouldDumpBeforeUnregister = fuzzedDataProvider.ConsumeBool();
    size_t logSize = fuzzedDataProvider.ConsumeIntegralInRange<size_t>(kMinSize, kMaxSize);
    sp<IMemory> logBuffer = mMemoryDealer->allocate(NBLog::Timeline::sharedSize(logSize));
    Vector<String16> args;
    size_t numberOfLines = fuzzedDataProvider.ConsumeIntegralInRange<size_t>(0, kMaxNumLines);
    for (size_t lineIdx = 0; lineIdx < numberOfLines; ++lineIdx) {
        args.add(static_cast<String16>(fuzzedDataProvider.ConsumeRandomLengthString().c_str()));
    }
    const char* fileName = "logDumpFile";
    int fd = memfd_create(fileName, MFD_ALLOW_SEALING);
    fuzzedDataProvider.ConsumeData(logBuffer->unsecurePointer(), logBuffer->size());
    mService->registerWriter(logBuffer, logSize, kWriterNames[writerNameIdx]);
    if (shouldDumpBeforeUnregister) {
        mService->dump(fd, args);
        mService->unregisterWriter(logBuffer);
    } else {
        mService->unregisterWriter(logBuffer);
        mService->dump(fd, args);
    }
}

extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
    MediaLogFuzzer mediaLogFuzzer = MediaLogFuzzer();
    mediaLogFuzzer.init();
    mediaLogFuzzer.process(data, size);
    return 0;
}