blob: f4c815c8988a613e9f6c04c54ec16b0facba32c8 [file] [log] [blame]
Dylan Katzc247e4a2020-06-10 16:21:39 -07001/*
2 * Copyright 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <fcntl.h>
18
19#include <functional>
Nate Myrene69cada2020-12-10 10:00:36 -080020#include <type_traits>
Dylan Katzc247e4a2020-06-10 16:21:39 -070021
22#include "fuzzer/FuzzedDataProvider.h"
23#include "mediautils/ServiceUtilities.h"
24
25static constexpr int kMaxOperations = 50;
26static constexpr int kMaxStringLen = 256;
27
28const std::vector<std::function<void(FuzzedDataProvider*, android::MediaPackageManager)>>
29 operations = {
30 [](FuzzedDataProvider* data_provider, android::MediaPackageManager pm) -> void {
31 uid_t uid = data_provider->ConsumeIntegral<uid_t>();
32 pm.allowPlaybackCapture(uid);
33 },
34 [](FuzzedDataProvider* data_provider, android::MediaPackageManager pm) -> void {
35 int spaces = data_provider->ConsumeIntegral<int>();
36
37 // Dump everything into /dev/null
38 int fd = open("/dev/null", O_WRONLY);
39 pm.dump(fd, spaces);
40 close(fd);
41 },
42};
43
44extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
45 FuzzedDataProvider data_provider(data, size);
46 uid_t uid = data_provider.ConsumeIntegral<uid_t>();
47 pid_t pid = data_provider.ConsumeIntegral<pid_t>();
Nate Myrene69cada2020-12-10 10:00:36 -080048 audio_source_t source = static_cast<audio_source_t>(data_provider
49 .ConsumeIntegral<std::underlying_type_t<audio_source_t>>());
Dylan Katzc247e4a2020-06-10 16:21:39 -070050
51 // There is not state here, and order is not significant,
52 // so we can simply call all of the target functions
53 android::isServiceUid(uid);
54 android::isAudioServerUid(uid);
55 android::isAudioServerOrSystemServerUid(uid);
56 android::isAudioServerOrMediaServerUid(uid);
57 std::string packageNameStr = data_provider.ConsumeRandomLengthString(kMaxStringLen);
58 android::String16 opPackageName(packageNameStr.c_str());
59 android::recordingAllowed(opPackageName, pid, uid);
Nate Myrene69cada2020-12-10 10:00:36 -080060 android::startRecording(opPackageName, pid, uid, source);
61 android::finishRecording(opPackageName, uid, source);
Dylan Katzc247e4a2020-06-10 16:21:39 -070062 android::captureAudioOutputAllowed(pid, uid);
63 android::captureMediaOutputAllowed(pid, uid);
64 android::captureHotwordAllowed(opPackageName, pid, uid);
65 android::modifyPhoneStateAllowed(uid, pid);
66 android::bypassInterruptionPolicyAllowed(uid, pid);
67 android::settingsAllowed();
68 android::modifyAudioRoutingAllowed();
69 android::modifyDefaultAudioEffectsAllowed();
70 android::dumpAllowed();
71
72 // MediaPackageManager does have state, so we need the fuzzer to decide order
73 android::MediaPackageManager packageManager;
74 size_t ops_run = 0;
75 while (data_provider.remaining_bytes() > 0 && ops_run++ < kMaxOperations) {
76 uint8_t op = data_provider.ConsumeIntegralInRange<uint8_t>(0, operations.size() - 1);
77 operations[op](&data_provider, packageManager);
78 }
79
80 return 0;
81}