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

Commit 3835b18d authored by David Fu's avatar David Fu Committed by Android (Google) Code Review
Browse files

Merge "Added ndk_crypto_fuzzer"

parents b46a7e65 fac19e5e
Loading
Loading
Loading
Loading
+57 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 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.
 */

cc_defaults {
     name: "libmediandk_fuzzer_defaults",
     shared_libs: [
        "libandroid_runtime_lazy",
        "libbase",
        "libdatasource",
        "libmedia",
        "libmediadrm",
        "libmedia_omx",
        "libmedia_jni_utils",
        "libstagefright",
        "libstagefright_foundation",
        "liblog",
        "libutils",
        "libcutils",
        "libnativewindow",
        "libhidlbase",
        "libgui",
        "libui",
        "libmediandk",
     ],
     static_libs: [
        "libmediandk_utils",
        "libnativehelper_lazy",
     ],
     header_libs: [
         "media_ndk_headers",
     ],
     fuzz_config: {
        cc: [
            "android-media-fuzzing-reports@google.com",
        ],
        componentid: 155276,
    },
}

cc_fuzz {
    name: "ndk_crypto_fuzzer",
    srcs: ["ndk_crypto_fuzzer.cpp"],
    defaults: ["libmediandk_fuzzer_defaults"],
}
+24 −0
Original line number Diff line number Diff line
# Fuzzers for libmediandk

## Table of contents
+ [ndk_crypto_fuzzer](#NdkCrypto)

# <a name="NdkCrypto"></a> Fuzzer for NdkCrypto

NdkCrypto supports the following parameters:
    UniversalIdentifier (parameter name: "uuid")

| Parameter| Valid Values |Configured Value|
|-------------|----------|----- |
| `uuid`| `Array`| Value obtained from FuzzedDataProvider|

#### Steps to run
1. Build the fuzzer
```
  $ mm -j$(nproc) ndk_crypto_fuzzer
```
2. Run on device
```
  $ adb sync data
  $ adb shell /data/fuzz/arm64/ndk_crypto_fuzzer/ndk_crypto_fuzzer
```
+46 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 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 <fuzzer/FuzzedDataProvider.h>
#include <media/NdkMediaCrypto.h>

constexpr size_t kMaxString = 256;
constexpr size_t kMinBytes = 0;
constexpr size_t kMaxBytes = 1000;

extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
    FuzzedDataProvider fdp(data, size);
    AMediaUUID uuid = {};
    int32_t maxLen = fdp.ConsumeIntegralInRange<size_t>(kMinBytes, (size_t)sizeof(AMediaUUID));
    for (size_t idx = 0; idx < maxLen; ++idx) {
        uuid[idx] = fdp.ConsumeIntegral<uint8_t>();
    }
    std::vector<uint8_t> initData =
            fdp.ConsumeBytes<uint8_t>(fdp.ConsumeIntegralInRange<size_t>(kMinBytes, kMaxBytes));
    AMediaCrypto* crypto = AMediaCrypto_new(uuid, initData.data(), initData.size());
    while (fdp.remaining_bytes()) {
        auto invokeNdkCryptoFuzzer = fdp.PickValueInArray<const std::function<void()>>({
                [&]() {
                    AMediaCrypto_requiresSecureDecoderComponent(
                            fdp.ConsumeRandomLengthString(kMaxString).c_str());
                },
                [&]() { AMediaCrypto_isCryptoSchemeSupported(uuid); },
        });
        invokeNdkCryptoFuzzer();
    }
    AMediaCrypto_delete(crypto);
    return 0;
}