CVE-2026-59031 AI-generated analysis
platform/frameworks/native, inputflinger InputDispatcher (services/inputflinger/dispatcher/InputDispatcher.cpp)
Replaces the manual erase-while-iterating loop in InputDispatcher::flushSensor with std::erase_if so the iterator is never advanced past end() after erasing the tail element, and restricts the erase to SensorEntry items whose deviceId and sensorType match the arguments.
Root cause
In InputDispatcher::flushSensor, the loop `for (auto it = mInboundQueue.begin(); it != mInboundQueue.end(); it++)` did `it = mInboundQueue.erase(it)` for every entry of type EventEntry::Type::SENSOR and then still ran the loop's `it++`. When the erased sensor entry was the last element of mInboundQueue, erase() returned end() and the increment moved the iterator past end(). The next iteration then dereferenced an invalid iterator (out-of-bounds read / use-after-free, per the commit message). A second logic defect: the loop ignored the deviceId and sensorType parameters and removed every sensor entry in the queue. Because of the double advance, it also skipped the entry right after each erased one.
How the fix works
- services/inputflinger/dispatcher/InputDispatcher.cpp:InputDispatcher::flushSensor: the hand-written loop is removed and replaced by `std::erase_if(mInboundQueue, lambda)`, which runs under std::scoped_lock _l(mLock). erase_if handles element removal internally, so no iterator can be advanced past end().
- The lambda is annotated REQUIRES(mLock) and captures this, deviceId and sensorType. It returns false for any entry whose type is not EventEntry::Type::SENSOR. Otherwise it static_casts the entry to `const SensorEntry&` and returns false unless `sensorEntry.deviceId == deviceId && sensorEntry.sensorType == sensorType`.
- For matching entries the lambda calls releaseInboundEventLocked(entry), the same cleanup as the old code, and returns true so erase_if removes the entry. All non-matching entries are kept, including sensor entries of other types or from other devices.
- Tests (services/inputflinger/tests/InputDispatcher_test.cpp) add FlushSensor_EraseTailElement and FlushSensor_OnlyErasesMatchingDeviceIdAndSensorType. In FlushSensor_EraseTailElement a key event sits at the head and a sensor event at the tail; dispatch is frozen with setInputDispatchMode(true, true) before the events are enqueued, so the flush runs on a populated queue. On Android 15/16/17 the fix also adds test helpers for these tests: FakeInputDispatcherPolicy::SensorEvent, getSensorEvent(), a notifySensorEvent() that actually records events (FakeInputDispatcherPolicy.cpp/.h), and the WithSensorEvent matcher (TestEventMatchers.h).
Completeness
- flushSensor is the only erase-while-iterating site changed. The fix removes the faulty iterator handling there for any position of the sensor entry in mInboundQueue, not only the tail.
- The deviceId/sensorType filter is applied to every SENSOR entry in mInboundQueue. Non-sensor entries are untouched.
- The material shows no other loops over mInboundQueue, so it cannot be judged whether similar patterns exist elsewhere in InputDispatcher.
Behavior changes
- flushSensor now removes only queued sensor events whose SensorEntry::deviceId and SensorEntry::sensorType equal the arguments. Before, it dropped every queued sensor event of every device and type, and could skip the entry after an erased one.
- The return value is unchanged (still true) and locking is unchanged (still under mLock).
- Test-only: on Android 15/16/17, FakeInputDispatcherPolicy::notifySensorEvent now records events; before, it did nothing.
Per major
- Android 14: Same production fix: commit 69809a4c8421 in android-security-14.0.0_r31, linked by change_id. It is the same std::erase_if replacement; the lambda takes std::shared_ptr<EventEntry> (non-const), the entry type the old A14 loop used. The commit touches only InputDispatcher.cpp and InputDispatcher_test.cpp; it does not include the FakeInputDispatcherPolicy/TestEventMatchers helper additions found on 15 and later.
- Android 15: Same fix: commit 04898b6d69ac in android-security-15.0.0_r19, linked by bug_id, using std::shared_ptr<const EventEntry>. It also adds FakeInputDispatcherPolicy::SensorEvent/getSensorEvent, the notifySensorEvent recorder and the WithSensorEvent matcher for the new tests.
- Android 16: Same fix: commit f659e7887862 in android-security-16.0.0_r10, linked by bug_id. The production diff is identical and the test helper additions are the same as on 15.
- Android 17: Same fix: commit e99abcf08d48 in android-security-17.0.0_r3, linked by bug_id. The production diff is identical and the test helper additions are the same, with DeviceId-typed parameters in FakeInputDispatcherPolicy.
Unknowns
- The declared container type of mInboundQueue is not shown in the material; it only shows that the queue supports begin/end/erase and std::erase_if.
- Whether the Android 14 test changes compile as committed: the A14 diff does not add the helpers they use, and the A14 test context shows the older display id constants.
- How flushSensor interacts with an event that has already been taken from mInboundQueue for dispatch is not shown.
- The material does not explain the per-major figure '4/8 lines of the patched region present'; all four branches are reported as resolved.