Harry Cutts | ccb75e8 | 2023-06-23 14:08:06 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2023 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 <limits> |
| 18 | #include <string> |
| 19 | #include <vector> |
| 20 | |
| 21 | #include <linux/input-event-codes.h> |
| 22 | |
| 23 | #include <FuzzContainer.h> |
Harry Cutts | e36d835 | 2023-07-10 13:28:45 +0000 | [diff] [blame^] | 24 | #include <InputReaderBase.h> |
Harry Cutts | ccb75e8 | 2023-06-23 14:08:06 +0000 | [diff] [blame] | 25 | #include <MapperHelpers.h> |
| 26 | #include <TouchpadInputMapper.h> |
| 27 | |
| 28 | namespace android { |
| 29 | |
| 30 | namespace { |
| 31 | |
| 32 | void setAxisInfo(ThreadSafeFuzzedDataProvider& fdp, FuzzContainer& fuzzer, int axis) { |
| 33 | if (fdp.ConsumeBool()) { |
| 34 | fuzzer.setAbsoluteAxisInfo(axis, |
| 35 | RawAbsoluteAxisInfo{ |
| 36 | .valid = fdp.ConsumeBool(), |
| 37 | .minValue = fdp.ConsumeIntegral<int32_t>(), |
| 38 | .maxValue = fdp.ConsumeIntegral<int32_t>(), |
| 39 | .flat = fdp.ConsumeIntegral<int32_t>(), |
| 40 | .fuzz = fdp.ConsumeIntegral<int32_t>(), |
| 41 | .resolution = fdp.ConsumeIntegral<int32_t>(), |
| 42 | }); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | void setAxisInfos(ThreadSafeFuzzedDataProvider& fdp, FuzzContainer& fuzzer) { |
| 47 | setAxisInfo(fdp, fuzzer, ABS_MT_SLOT); |
| 48 | setAxisInfo(fdp, fuzzer, ABS_MT_POSITION_X); |
| 49 | setAxisInfo(fdp, fuzzer, ABS_MT_POSITION_Y); |
| 50 | setAxisInfo(fdp, fuzzer, ABS_MT_PRESSURE); |
| 51 | setAxisInfo(fdp, fuzzer, ABS_MT_ORIENTATION); |
| 52 | setAxisInfo(fdp, fuzzer, ABS_MT_TOUCH_MAJOR); |
| 53 | setAxisInfo(fdp, fuzzer, ABS_MT_TOUCH_MINOR); |
| 54 | setAxisInfo(fdp, fuzzer, ABS_MT_WIDTH_MAJOR); |
| 55 | setAxisInfo(fdp, fuzzer, ABS_MT_WIDTH_MINOR); |
| 56 | } |
| 57 | |
| 58 | const std::vector<std::string> boolPropertiesToFuzz = { |
| 59 | "gestureProp.Compute_Surface_Area_from_Pressure", |
| 60 | "gestureProp.Drumroll_Suppression_Enable", |
| 61 | "gestureProp.Fling_Buffer_Suppress_Zero_Length_Scrolls", |
| 62 | "gestureProp.Stationary_Wiggle_Filter_Enabled", |
| 63 | }; |
| 64 | const std::vector<std::string> doublePropertiesToFuzz = { |
| 65 | "gestureProp.Fake_Timestamp_Delta", |
| 66 | "gestureProp.Finger_Moving_Energy", |
| 67 | "gestureProp.Finger_Moving_Hysteresis", |
| 68 | "gestureProp.IIR_a1", |
| 69 | "gestureProp.IIR_a2", |
| 70 | "gestureProp.IIR_b0", |
| 71 | "gestureProp.IIR_b1", |
| 72 | "gestureProp.IIR_b2", |
| 73 | "gestureProp.IIR_b3", |
| 74 | "gestureProp.Max_Allowed_Pressure_Change_Per_Sec", |
| 75 | "gestureProp.Max_Hysteresis_Pressure_Per_Sec", |
| 76 | "gestureProp.Max_Stationary_Move_Speed", |
| 77 | "gestureProp.Max_Stationary_Move_Speed_Hysteresis", |
| 78 | "gestureProp.Max_Stationary_Move_Suppress_Distance", |
| 79 | "gestureProp.Multiple_Palm_Width", |
| 80 | "gestureProp.Palm_Edge_Zone_Width", |
| 81 | "gestureProp.Palm_Eval_Timeout", |
| 82 | "gestureProp.Palm_Pressure", |
| 83 | "gestureProp.Palm_Width", |
| 84 | "gestureProp.Pressure_Calibration_Offset", |
| 85 | "gestureProp.Pressure_Calibration_Slope", |
| 86 | "gestureProp.Tap_Exclusion_Border_Width", |
| 87 | "gestureProp.Touchpad_Device_Output_Bias_on_X-Axis", |
| 88 | "gestureProp.Touchpad_Device_Output_Bias_on_Y-Axis", |
| 89 | "gestureProp.Two_Finger_Vertical_Close_Distance_Thresh", |
| 90 | }; |
| 91 | |
| 92 | void setDeviceSpecificConfig(ThreadSafeFuzzedDataProvider& fdp, FuzzContainer& fuzzer) { |
| 93 | // There are a great many gesture properties offered by the Gestures library, all of which could |
| 94 | // potentially be set in Input Device Configuration files. Maintaining a complete list is |
| 95 | // impractical, so instead we only fuzz properties which are used in at least one IDC file, or |
| 96 | // which are likely to be used in future (e.g. ones for controlling palm rejection). |
| 97 | |
| 98 | if (fdp.ConsumeBool()) { |
| 99 | fuzzer.addProperty("gestureProp.Touchpad_Stack_Version", |
| 100 | std::to_string(fdp.ConsumeIntegral<int>())); |
| 101 | } |
| 102 | |
| 103 | for (auto& propertyName : boolPropertiesToFuzz) { |
| 104 | if (fdp.ConsumeBool()) { |
| 105 | fuzzer.addProperty(propertyName, fdp.ConsumeBool() ? "1" : "0"); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | for (auto& propertyName : doublePropertiesToFuzz) { |
| 110 | if (fdp.ConsumeBool()) { |
| 111 | fuzzer.addProperty(propertyName, std::to_string(fdp.ConsumeFloatingPoint<double>())); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | if (fdp.ConsumeBool()) { |
| 116 | fuzzer.addProperty("gestureProp." + fdp.ConsumeRandomLengthString(), |
| 117 | std::to_string(fdp.ConsumeIntegral<int>())); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | void setTouchpadSettings(ThreadSafeFuzzedDataProvider& fdp, InputReaderConfiguration& config) { |
| 122 | config.touchpadPointerSpeed = fdp.ConsumeIntegralInRange(-7, 7); |
| 123 | config.touchpadNaturalScrollingEnabled = fdp.ConsumeBool(); |
| 124 | config.touchpadTapToClickEnabled = fdp.ConsumeBool(); |
| 125 | config.touchpadRightClickZoneEnabled = fdp.ConsumeBool(); |
| 126 | } |
| 127 | |
| 128 | } // namespace |
| 129 | |
| 130 | extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size) { |
| 131 | std::shared_ptr<ThreadSafeFuzzedDataProvider> fdp = |
| 132 | std::make_shared<ThreadSafeFuzzedDataProvider>(data, size); |
| 133 | FuzzContainer fuzzer(fdp); |
| 134 | setAxisInfos(*fdp, fuzzer); |
| 135 | setDeviceSpecificConfig(*fdp, fuzzer); |
| 136 | |
Harry Cutts | e36d835 | 2023-07-10 13:28:45 +0000 | [diff] [blame^] | 137 | InputReaderConfiguration policyConfig; |
Harry Cutts | ccb75e8 | 2023-06-23 14:08:06 +0000 | [diff] [blame] | 138 | // Some settings are fuzzed here, as well as in the main loop, to provide randomized data to the |
| 139 | // TouchpadInputMapper constructor. |
| 140 | setTouchpadSettings(*fdp, policyConfig); |
| 141 | policyConfig.pointerCaptureRequest.enable = fdp->ConsumeBool(); |
| 142 | TouchpadInputMapper& mapper = fuzzer.getMapper<TouchpadInputMapper>(policyConfig); |
| 143 | |
| 144 | // Loop through mapper operations until randomness is exhausted. |
| 145 | while (fdp->remaining_bytes() > 0) { |
| 146 | fdp->PickValueInArray<std::function<void()>>({ |
| 147 | [&]() -> void { |
| 148 | std::string dump; |
| 149 | mapper.dump(dump); |
| 150 | }, |
| 151 | [&]() -> void { |
| 152 | InputDeviceInfo info; |
| 153 | mapper.populateDeviceInfo(info); |
| 154 | }, |
| 155 | [&]() -> void { mapper.getSources(); }, |
| 156 | [&]() -> void { |
| 157 | setTouchpadSettings(*fdp, policyConfig); |
| 158 | policyConfig.pointerCaptureRequest.enable = fdp->ConsumeBool(); |
| 159 | std::list<NotifyArgs> unused = |
| 160 | mapper.reconfigure(fdp->ConsumeIntegral<nsecs_t>(), policyConfig, |
| 161 | InputReaderConfiguration::Change( |
| 162 | fdp->ConsumeIntegral<uint32_t>())); |
| 163 | }, |
| 164 | [&]() -> void { |
| 165 | std::list<NotifyArgs> unused = mapper.reset(fdp->ConsumeIntegral<nsecs_t>()); |
| 166 | }, |
| 167 | [&]() -> void { |
| 168 | RawEvent event = getFuzzedRawEvent(*fdp); |
| 169 | std::list<NotifyArgs> unused = mapper.process(&event); |
| 170 | }, |
| 171 | })(); |
| 172 | } |
| 173 | |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | } // namespace android |