blob: 3d141b55e3c6b47503a562cdcb75abc6a176900c [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>
20
21#include "fuzzer/FuzzedDataProvider.h"
22#include "mediautils/ServiceUtilities.h"
23
24static constexpr int kMaxOperations = 50;
25static constexpr int kMaxStringLen = 256;
26
27const std::vector<std::function<void(FuzzedDataProvider*, android::MediaPackageManager)>>
28 operations = {
29 [](FuzzedDataProvider* data_provider, android::MediaPackageManager pm) -> void {
30 uid_t uid = data_provider->ConsumeIntegral<uid_t>();
31 pm.allowPlaybackCapture(uid);
32 },
33 [](FuzzedDataProvider* data_provider, android::MediaPackageManager pm) -> void {
34 int spaces = data_provider->ConsumeIntegral<int>();
35
36 // Dump everything into /dev/null
37 int fd = open("/dev/null", O_WRONLY);
38 pm.dump(fd, spaces);
39 close(fd);
40 },
41};
42
43extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
44 FuzzedDataProvider data_provider(data, size);
45 uid_t uid = data_provider.ConsumeIntegral<uid_t>();
46 pid_t pid = data_provider.ConsumeIntegral<pid_t>();
47
48 // There is not state here, and order is not significant,
49 // so we can simply call all of the target functions
50 android::isServiceUid(uid);
51 android::isAudioServerUid(uid);
52 android::isAudioServerOrSystemServerUid(uid);
53 android::isAudioServerOrMediaServerUid(uid);
54 std::string packageNameStr = data_provider.ConsumeRandomLengthString(kMaxStringLen);
55 android::String16 opPackageName(packageNameStr.c_str());
56 android::recordingAllowed(opPackageName, pid, uid);
57 android::startRecording(opPackageName, pid, uid);
58 android::finishRecording(opPackageName, uid);
59 android::captureAudioOutputAllowed(pid, uid);
60 android::captureMediaOutputAllowed(pid, uid);
61 android::captureHotwordAllowed(opPackageName, pid, uid);
62 android::modifyPhoneStateAllowed(uid, pid);
63 android::bypassInterruptionPolicyAllowed(uid, pid);
64 android::settingsAllowed();
65 android::modifyAudioRoutingAllowed();
66 android::modifyDefaultAudioEffectsAllowed();
67 android::dumpAllowed();
68
69 // MediaPackageManager does have state, so we need the fuzzer to decide order
70 android::MediaPackageManager packageManager;
71 size_t ops_run = 0;
72 while (data_provider.remaining_bytes() > 0 && ops_run++ < kMaxOperations) {
73 uint8_t op = data_provider.ConsumeIntegralInRange<uint8_t>(0, operations.size() - 1);
74 operations[op](&data_provider, packageManager);
75 }
76
77 return 0;
78}