Skip to content

Commit 01884db

Browse files
Treehugger RobotAndroid (Google) Code Review
authored andcommitted
Merge changes I7c2108a2,I23fa0e50,I20d02933 into main
* changes: inputflinger: Restricted invalid MotionEvent button enum values inputflinger: Restricted invalid InputDeviceClass enum values inputflinger: only use UTF-8 characters in device name and location
2 parents 43a1d49 + f54ab06 commit 01884db

5 files changed

Lines changed: 85 additions & 11 deletions

File tree

include/android/input.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,7 @@ enum {
862862
AMOTION_EVENT_BUTTON_FORWARD = 1 << 4,
863863
AMOTION_EVENT_BUTTON_STYLUS_PRIMARY = 1 << 5,
864864
AMOTION_EVENT_BUTTON_STYLUS_SECONDARY = 1 << 6,
865-
// LINT.ThenChange(/frameworks/native/libs/input/rust/input.rs)
865+
// LINT.ThenChange(/frameworks/native/libs/input/rust/input.rs,/frameworks/native/services/inputflinger/tests/fuzzers/FuzzedInputStream.h)
866866
};
867867

868868
/**

services/inputflinger/reader/include/EventHub.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ std::ostream& operator<<(std::ostream& out, const std::optional<RawAbsoluteAxisI
8888
* If any new classes are added, we need to add them in rust input side too.
8989
*/
9090
enum class InputDeviceClass : uint32_t {
91+
// LINT.IfChange
9192
/* The input device is a keyboard or has buttons. */
9293
KEYBOARD = android::os::IInputConstants::DEVICE_CLASS_KEYBOARD,
9394

@@ -144,6 +145,7 @@ enum class InputDeviceClass : uint32_t {
144145

145146
/* The input device is external (not built-in). */
146147
EXTERNAL = android::os::IInputConstants::DEVICE_CLASS_EXTERNAL,
148+
// LINT.ThenChange(frameworks/native/services/inputflinger/tests/fuzzers/MapperHelpers.h)
147149
};
148150

149151
enum class SysfsClass : uint32_t {

services/inputflinger/tests/fuzzers/FuzzedInputStream.h

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ namespace android {
2121
static constexpr int32_t MAX_RANDOM_POINTERS = 4;
2222
static constexpr int32_t MAX_RANDOM_DEVICES = 4;
2323

24+
// The maximum value that we use for the action button field of NotifyMotionArgs. (We allow multiple
25+
// bits to be set for this since we're just trying to generate a fuzzed event stream that doesn't
26+
// cause crashes when enum values are converted to Rust — we don't necessarily want it to be valid.)
27+
//
28+
// AMOTION_EVENT_BUTTON_STYLUS_SECONDARY should be replaced with whatever AMOTION_EVENT_BUTTON_
29+
// value is highest if the enum is edited.
30+
static constexpr int8_t MAX_ACTION_BUTTON_VALUE = (AMOTION_EVENT_BUTTON_STYLUS_SECONDARY << 1) - 1;
31+
2432
int getFuzzedMotionAction(FuzzedDataProvider& fdp) {
2533
int actionMasked = fdp.PickValueInArray<int>({
2634
AMOTION_EVENT_ACTION_DOWN, AMOTION_EVENT_ACTION_UP, AMOTION_EVENT_ACTION_MOVE,
@@ -185,18 +193,16 @@ NotifyMotionArgs generateFuzzedMotionArgs(IdGenerator& idGenerator, FuzzedDataPr
185193
fdp.ConsumeIntegralInRange<nsecs_t>(currentTime - 5E9, currentTime + 5E9);
186194
const nsecs_t readTime = downTime;
187195
const nsecs_t eventTime = fdp.ConsumeIntegralInRange<nsecs_t>(downTime, downTime + 1E9);
196+
const int32_t actionButton = fdp.ConsumeIntegralInRange<int32_t>(0, MAX_ACTION_BUTTON_VALUE);
188197

189198
const float cursorX = fdp.ConsumeIntegralInRange<int>(-10000, 10000);
190199
const float cursorY = fdp.ConsumeIntegralInRange<int>(-10000, 10000);
191200
return NotifyMotionArgs(idGenerator.nextId(), eventTime, readTime, deviceId, source, displayId,
192-
POLICY_FLAG_PASS_TO_USER, action,
193-
/*actionButton=*/fdp.ConsumeIntegral<int32_t>(),
201+
POLICY_FLAG_PASS_TO_USER, action, actionButton,
194202
getFuzzedFlags(fdp, action), AMETA_NONE, getFuzzedButtonState(fdp),
195203
MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount,
196-
pointerProperties.data(), pointerCoords.data(),
197-
/*xPrecision=*/0,
198-
/*yPrecision=*/0, cursorX, cursorY, downTime,
199-
/*videoFrames=*/{});
204+
pointerProperties.data(), pointerCoords.data(), /*xPrecision=*/0,
205+
/*yPrecision=*/0, cursorX, cursorY, downTime, /*videoFrames=*/{});
200206
}
201207

202208
} // namespace android

services/inputflinger/tests/fuzzers/MapperHelpers.h

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,28 @@ constexpr size_t kValidTypes[] = {EV_SW,
3434
android::EventHubInterface::DEVICE_ADDED,
3535
android::EventHubInterface::DEVICE_REMOVED};
3636

37+
static const android::InputDeviceClass kInputDeviceClasses[] = {
38+
android::InputDeviceClass::KEYBOARD,
39+
android::InputDeviceClass::ALPHAKEY,
40+
android::InputDeviceClass::TOUCH,
41+
android::InputDeviceClass::CURSOR,
42+
android::InputDeviceClass::TOUCH_MT,
43+
android::InputDeviceClass::DPAD,
44+
android::InputDeviceClass::GAMEPAD,
45+
android::InputDeviceClass::SWITCH,
46+
android::InputDeviceClass::JOYSTICK,
47+
android::InputDeviceClass::VIBRATOR,
48+
android::InputDeviceClass::MIC,
49+
android::InputDeviceClass::EXTERNAL_STYLUS,
50+
android::InputDeviceClass::ROTARY_ENCODER,
51+
android::InputDeviceClass::SENSOR,
52+
android::InputDeviceClass::BATTERY,
53+
android::InputDeviceClass::LIGHT,
54+
android::InputDeviceClass::TOUCHPAD,
55+
android::InputDeviceClass::VIRTUAL,
56+
android::InputDeviceClass::EXTERNAL,
57+
};
58+
3759
constexpr size_t kValidCodes[] = {
3860
SYN_REPORT,
3961
ABS_MT_SLOT,
@@ -105,7 +127,13 @@ class FuzzEventHub : public EventHubInterface {
105127
void addProperty(std::string key, std::string value) { mFuzzConfig.addProperty(key, value); }
106128

107129
ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
108-
return ftl::Flags<InputDeviceClass>(mFdp->ConsumeIntegral<uint32_t>());
130+
uint32_t flags = 0;
131+
for (auto inputDeviceClass : kInputDeviceClasses) {
132+
if (mFdp->ConsumeBool()) {
133+
flags |= static_cast<uint32_t>(inputDeviceClass);
134+
}
135+
}
136+
return ftl::Flags<InputDeviceClass>(flags);
109137
}
110138
InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
111139
return mIdentifier;
@@ -367,8 +395,8 @@ class FuzzInputReaderContext : public InputReaderContext {
367395
template <class Fdp>
368396
InputDevice getFuzzedInputDevice(Fdp& fdp, FuzzInputReaderContext* context) {
369397
InputDeviceIdentifier identifier;
370-
identifier.name = fdp.ConsumeRandomLengthString(16);
371-
identifier.location = fdp.ConsumeRandomLengthString(12);
398+
identifier.name = fdp.ConsumeRandomLengthUtf8String(16);
399+
identifier.location = fdp.ConsumeRandomLengthUtf8String(12);
372400
int32_t deviceID = fdp.ConsumeIntegralInRange(0, 5);
373401
int32_t deviceGeneration = fdp.ConsumeIntegralInRange(0, 5);
374402
return InputDevice(context, deviceID, deviceGeneration, identifier);

services/inputflinger/tests/fuzzers/ThreadSafeFuzzedDataProvider.h

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
#include <fuzzer/FuzzedDataProvider.h>
18-
18+
#include <algorithm>
1919
/**
2020
* A thread-safe interface to the FuzzedDataProvider
2121
*/
@@ -60,6 +60,44 @@ class ThreadSafeFuzzedDataProvider : FuzzedDataProvider {
6060
return FuzzedDataProvider::ConsumeRandomLengthString();
6161
}
6262

63+
// Converting the string to a UTF-8 string by setting the prefix bits of each
64+
// byte according to UTF-8 encoding rules.
65+
std::string ConsumeRandomLengthUtf8String(size_t max_length) {
66+
std::scoped_lock _l(mLock);
67+
std::string result = FuzzedDataProvider::ConsumeRandomLengthString(max_length);
68+
size_t remaining_bytes = result.length(), idx = 0;
69+
while (remaining_bytes > 0) {
70+
size_t random_byte_size = FuzzedDataProvider::ConsumeIntegralInRange(1, 4);
71+
size_t byte_size = std::min(random_byte_size, remaining_bytes);
72+
switch (byte_size) {
73+
// Prefix byte: 0xxxxxxx
74+
case 1:
75+
result[idx++] &= 0b01111111;
76+
break;
77+
// Prefix bytes: 110xxxxx 10xxxxxx
78+
case 2:
79+
result[idx++] = (result[idx] & 0b00011111) | 0b11000000;
80+
result[idx++] = (result[idx] & 0b00111111) | 0b10000000;
81+
break;
82+
// Prefix bytes: 1110xxxx 10xxxxxx 10xxxxxx
83+
case 3:
84+
result[idx++] = (result[idx] & 0b00001111) | 0b11100000;
85+
result[idx++] = (result[idx] & 0b00111111) | 0b10000000;
86+
result[idx++] = (result[idx] & 0b00111111) | 0b10000000;
87+
break;
88+
// Prefix bytes: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
89+
case 4:
90+
result[idx++] = (result[idx] & 0b00000111) | 0b11110000;
91+
result[idx++] = (result[idx] & 0b00111111) | 0b10000000;
92+
result[idx++] = (result[idx] & 0b00111111) | 0b10000000;
93+
result[idx++] = (result[idx] & 0b00111111) | 0b10000000;
94+
break;
95+
}
96+
remaining_bytes -= byte_size;
97+
}
98+
return result;
99+
}
100+
63101
std::string ConsumeRemainingBytesAsString() {
64102
std::scoped_lock _l(mLock);
65103
return FuzzedDataProvider::ConsumeRemainingBytesAsString();

0 commit comments

Comments
 (0)