Dylan Katz | c247e4a | 2020-06-10 16:21:39 -0700 | [diff] [blame] | 1 | /* |
| 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 Myren | e69cada | 2020-12-10 10:00:36 -0800 | [diff] [blame] | 20 | #include <type_traits> |
Dylan Katz | c247e4a | 2020-06-10 16:21:39 -0700 | [diff] [blame] | 21 | |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 22 | #include <android/media/permission/Identity.h> |
Dylan Katz | c247e4a | 2020-06-10 16:21:39 -0700 | [diff] [blame] | 23 | #include "fuzzer/FuzzedDataProvider.h" |
| 24 | #include "mediautils/ServiceUtilities.h" |
| 25 | |
| 26 | static constexpr int kMaxOperations = 50; |
| 27 | static constexpr int kMaxStringLen = 256; |
| 28 | |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 29 | using android::media::permission::Identity; |
| 30 | |
Dylan Katz | c247e4a | 2020-06-10 16:21:39 -0700 | [diff] [blame] | 31 | const std::vector<std::function<void(FuzzedDataProvider*, android::MediaPackageManager)>> |
| 32 | operations = { |
| 33 | [](FuzzedDataProvider* data_provider, android::MediaPackageManager pm) -> void { |
| 34 | uid_t uid = data_provider->ConsumeIntegral<uid_t>(); |
| 35 | pm.allowPlaybackCapture(uid); |
| 36 | }, |
| 37 | [](FuzzedDataProvider* data_provider, android::MediaPackageManager pm) -> void { |
| 38 | int spaces = data_provider->ConsumeIntegral<int>(); |
| 39 | |
| 40 | // Dump everything into /dev/null |
| 41 | int fd = open("/dev/null", O_WRONLY); |
| 42 | pm.dump(fd, spaces); |
| 43 | close(fd); |
| 44 | }, |
| 45 | }; |
| 46 | |
| 47 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
| 48 | FuzzedDataProvider data_provider(data, size); |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 49 | int32_t uid = data_provider.ConsumeIntegral<int32_t>(); |
| 50 | int32_t pid = data_provider.ConsumeIntegral<int32_t>(); |
Nate Myren | e69cada | 2020-12-10 10:00:36 -0800 | [diff] [blame] | 51 | audio_source_t source = static_cast<audio_source_t>(data_provider |
| 52 | .ConsumeIntegral<std::underlying_type_t<audio_source_t>>()); |
Dylan Katz | c247e4a | 2020-06-10 16:21:39 -0700 | [diff] [blame] | 53 | |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 54 | std::string packageNameStr = data_provider.ConsumeRandomLengthString(kMaxStringLen); |
| 55 | std::string msgStr = data_provider.ConsumeRandomLengthString(kMaxStringLen); |
| 56 | android::String16 msgStr16(packageNameStr.c_str()); |
| 57 | Identity identity; |
| 58 | identity.packageName = packageNameStr; |
| 59 | identity.uid = uid; |
| 60 | identity.pid = pid; |
| 61 | |
Dylan Katz | c247e4a | 2020-06-10 16:21:39 -0700 | [diff] [blame] | 62 | // There is not state here, and order is not significant, |
| 63 | // so we can simply call all of the target functions |
| 64 | android::isServiceUid(uid); |
| 65 | android::isAudioServerUid(uid); |
| 66 | android::isAudioServerOrSystemServerUid(uid); |
| 67 | android::isAudioServerOrMediaServerUid(uid); |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 68 | android::recordingAllowed(identity); |
| 69 | android::startRecording(identity, msgStr16, source); |
| 70 | android::finishRecording(identity, source); |
| 71 | android::captureAudioOutputAllowed(identity); |
| 72 | android::captureMediaOutputAllowed(identity); |
| 73 | android::captureHotwordAllowed(identity); |
| 74 | android::modifyPhoneStateAllowed(identity); |
| 75 | android::bypassInterruptionPolicyAllowed(identity); |
Dylan Katz | c247e4a | 2020-06-10 16:21:39 -0700 | [diff] [blame] | 76 | android::settingsAllowed(); |
| 77 | android::modifyAudioRoutingAllowed(); |
| 78 | android::modifyDefaultAudioEffectsAllowed(); |
| 79 | android::dumpAllowed(); |
| 80 | |
| 81 | // MediaPackageManager does have state, so we need the fuzzer to decide order |
| 82 | android::MediaPackageManager packageManager; |
| 83 | size_t ops_run = 0; |
| 84 | while (data_provider.remaining_bytes() > 0 && ops_run++ < kMaxOperations) { |
| 85 | uint8_t op = data_provider.ConsumeIntegralInRange<uint8_t>(0, operations.size() - 1); |
| 86 | operations[op](&data_provider, packageManager); |
| 87 | } |
| 88 | |
| 89 | return 0; |
| 90 | } |