blob: 449e7de5accb52b4ff2e7cccae064bae298a1d65 [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
Svet Ganov3e5f14f2021-05-13 22:51:08 +000022#include <android/content/AttributionSourceState.h>
Dylan Katzc247e4a2020-06-10 16:21:39 -070023#include "fuzzer/FuzzedDataProvider.h"
24#include "mediautils/ServiceUtilities.h"
25
26static constexpr int kMaxOperations = 50;
27static constexpr int kMaxStringLen = 256;
Akshata Kadam913e7592023-01-03 11:12:38 +053028static constexpr int kMaxSpaces = 1000;
Dylan Katzc247e4a2020-06-10 16:21:39 -070029
Svet Ganov3e5f14f2021-05-13 22:51:08 +000030using android::content::AttributionSourceState;
Philip P. Moltmannbda45752020-07-17 16:41:18 -070031
Dylan Katzc247e4a2020-06-10 16:21:39 -070032const std::vector<std::function<void(FuzzedDataProvider*, android::MediaPackageManager)>>
33 operations = {
34 [](FuzzedDataProvider* data_provider, android::MediaPackageManager pm) -> void {
35 uid_t uid = data_provider->ConsumeIntegral<uid_t>();
36 pm.allowPlaybackCapture(uid);
37 },
38 [](FuzzedDataProvider* data_provider, android::MediaPackageManager pm) -> void {
Akshata Kadam913e7592023-01-03 11:12:38 +053039 /* The large value of spaces was taking time in file write operation.
40 * Limited spaces values in range to avoid timeout.*/
41 int spaces = data_provider->ConsumeIntegralInRange<int>(0, kMaxSpaces);
Dylan Katzc247e4a2020-06-10 16:21:39 -070042
43 // Dump everything into /dev/null
44 int fd = open("/dev/null", O_WRONLY);
45 pm.dump(fd, spaces);
46 close(fd);
47 },
48};
49
50extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
51 FuzzedDataProvider data_provider(data, size);
Philip P. Moltmannbda45752020-07-17 16:41:18 -070052 int32_t uid = data_provider.ConsumeIntegral<int32_t>();
53 int32_t pid = data_provider.ConsumeIntegral<int32_t>();
Nate Myrene69cada2020-12-10 10:00:36 -080054 audio_source_t source = static_cast<audio_source_t>(data_provider
55 .ConsumeIntegral<std::underlying_type_t<audio_source_t>>());
Marvin Raminb03b49f2024-04-04 16:25:31 +020056 uint32_t deviceId = data_provider.ConsumeIntegral<uint32_t>();
Dylan Katzc247e4a2020-06-10 16:21:39 -070057
Philip P. Moltmannbda45752020-07-17 16:41:18 -070058 std::string packageNameStr = data_provider.ConsumeRandomLengthString(kMaxStringLen);
59 std::string msgStr = data_provider.ConsumeRandomLengthString(kMaxStringLen);
60 android::String16 msgStr16(packageNameStr.c_str());
Svet Ganov3e5f14f2021-05-13 22:51:08 +000061 AttributionSourceState attributionSource;
62 attributionSource.packageName = packageNameStr;
63 attributionSource.uid = uid;
64 attributionSource.pid = pid;
65 attributionSource.token = android::sp<android::BBinder>::make();
Philip P. Moltmannbda45752020-07-17 16:41:18 -070066
Dylan Katzc247e4a2020-06-10 16:21:39 -070067 // There is not state here, and order is not significant,
68 // so we can simply call all of the target functions
69 android::isServiceUid(uid);
70 android::isAudioServerUid(uid);
71 android::isAudioServerOrSystemServerUid(uid);
72 android::isAudioServerOrMediaServerUid(uid);
Svet Ganov3e5f14f2021-05-13 22:51:08 +000073 android::recordingAllowed(attributionSource);
Marvin Raminb03b49f2024-04-04 16:25:31 +020074 android::recordingAllowed(attributionSource, deviceId, source);
75 android::startRecording(attributionSource, deviceId, msgStr16, source);
76 android::finishRecording(attributionSource, deviceId, source);
Svet Ganov3e5f14f2021-05-13 22:51:08 +000077 android::captureAudioOutputAllowed(attributionSource);
78 android::captureMediaOutputAllowed(attributionSource);
79 android::captureHotwordAllowed(attributionSource);
80 android::modifyPhoneStateAllowed(attributionSource);
81 android::bypassInterruptionPolicyAllowed(attributionSource);
Dylan Katzc247e4a2020-06-10 16:21:39 -070082 android::settingsAllowed();
83 android::modifyAudioRoutingAllowed();
84 android::modifyDefaultAudioEffectsAllowed();
85 android::dumpAllowed();
86
87 // MediaPackageManager does have state, so we need the fuzzer to decide order
88 android::MediaPackageManager packageManager;
89 size_t ops_run = 0;
90 while (data_provider.remaining_bytes() > 0 && ops_run++ < kMaxOperations) {
91 uint8_t op = data_provider.ConsumeIntegralInRange<uint8_t>(0, operations.size() - 1);
92 operations[op](&data_provider, packageManager);
93 }
94
95 return 0;
96}