| 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> | 
|  | 20 |  | 
|  | 21 | #include "fuzzer/FuzzedDataProvider.h" | 
|  | 22 | #include "mediautils/ServiceUtilities.h" | 
|  | 23 |  | 
|  | 24 | static constexpr int kMaxOperations = 50; | 
|  | 25 | static constexpr int kMaxStringLen = 256; | 
|  | 26 |  | 
|  | 27 | const 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 |  | 
|  | 43 | extern "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>(); | 
| Narayan Kamath | bf85d8b | 2020-08-20 17:19:57 +0100 | [diff] [blame] | 47 | bool isHotword = data_provider.ConsumeBool(); | 
| Dylan Katz | c247e4a | 2020-06-10 16:21:39 -0700 | [diff] [blame] | 48 |  | 
|  | 49 | // There is not state here, and order is not significant, | 
|  | 50 | // so we can simply call all of the target functions | 
|  | 51 | android::isServiceUid(uid); | 
|  | 52 | android::isAudioServerUid(uid); | 
|  | 53 | android::isAudioServerOrSystemServerUid(uid); | 
|  | 54 | android::isAudioServerOrMediaServerUid(uid); | 
|  | 55 | std::string packageNameStr = data_provider.ConsumeRandomLengthString(kMaxStringLen); | 
|  | 56 | android::String16 opPackageName(packageNameStr.c_str()); | 
|  | 57 | android::recordingAllowed(opPackageName, pid, uid); | 
| Narayan Kamath | bf85d8b | 2020-08-20 17:19:57 +0100 | [diff] [blame] | 58 | android::startRecording(opPackageName, pid, uid, isHotword); | 
|  | 59 | android::finishRecording(opPackageName, uid, isHotword); | 
| Dylan Katz | c247e4a | 2020-06-10 16:21:39 -0700 | [diff] [blame] | 60 | android::captureAudioOutputAllowed(pid, uid); | 
|  | 61 | android::captureMediaOutputAllowed(pid, uid); | 
|  | 62 | android::captureHotwordAllowed(opPackageName, pid, uid); | 
|  | 63 | android::modifyPhoneStateAllowed(uid, pid); | 
|  | 64 | android::bypassInterruptionPolicyAllowed(uid, pid); | 
|  | 65 | android::settingsAllowed(); | 
|  | 66 | android::modifyAudioRoutingAllowed(); | 
|  | 67 | android::modifyDefaultAudioEffectsAllowed(); | 
|  | 68 | android::dumpAllowed(); | 
|  | 69 |  | 
|  | 70 | // MediaPackageManager does have state, so we need the fuzzer to decide order | 
|  | 71 | android::MediaPackageManager packageManager; | 
|  | 72 | size_t ops_run = 0; | 
|  | 73 | while (data_provider.remaining_bytes() > 0 && ops_run++ < kMaxOperations) { | 
|  | 74 | uint8_t op = data_provider.ConsumeIntegralInRange<uint8_t>(0, operations.size() - 1); | 
|  | 75 | operations[op](&data_provider, packageManager); | 
|  | 76 | } | 
|  | 77 |  | 
|  | 78 | return 0; | 
|  | 79 | } |