mt_allocate_report_data(), mt_allocate_application() and
mt_allocate_usage() devm_kzalloc() with GFP_KERNEL, but they are
reachable from mt_event()/mt_report() via hid_report_raw_event(),
which can run in (soft)irq context off a USB URB completion (e.g.
dummy_hcd's hrtimer softirq). GFP_KERNEL there may call into
fs_reclaim, which lockdep flags as an inconsistent {SOFTIRQ-ON-W} ->
{IN-SOFTIRQ-W} use of fs_reclaim, alongside a "sleeping function
called from invalid context" splat.
Switch all three to GFP_ATOMIC since none of them are on a path that
needs to sleep.
Reported-by: syzbot+093e05755c2d99caab91@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=093e05755c2d99caab91
Signed-off-by: Nguyen Ngoc Thang <redacted>
---
drivers/hid/hid-multitouch.c | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)
@@ -604,7 +604,8 @@ static struct mt_usages *mt_allocate_usage(struct hid_device *hdev,{structmt_usages*usage;-usage=devm_kzalloc(&hdev->dev,sizeof(*usage),GFP_KERNEL);+/* may run from hid_report_raw_event() in (soft)irq context */+usage=devm_kzalloc(&hdev->dev,sizeof(*usage),GFP_ATOMIC);if(!usage)returnNULL;
@@ -633,8 +634,9 @@ static struct mt_application *mt_allocate_application(struct mt_device *td,unsignedintapplication=report->application;structmt_application*mt_application;+/* may run from hid_report_raw_event() in (soft)irq context */mt_application=devm_kzalloc(&td->hdev->dev,sizeof(*mt_application),-GFP_KERNEL);+GFP_ATOMIC);if(!mt_application)returnNULL;
@@ -692,7 +694,8 @@ static struct mt_report_data *mt_allocate_report_data(struct mt_device *td,structhid_field*field;intr,n;-rdata=devm_kzalloc(&td->hdev->dev,sizeof(*rdata),GFP_KERNEL);+/* may run from hid_report_raw_event() in (soft)irq context */+rdata=devm_kzalloc(&td->hdev->dev,sizeof(*rdata),GFP_ATOMIC);if(!rdata)returnNULL;
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
New issues:
- [Medium] `mt_allocate_usage()` is incorrectly assumed to run in softirq context, leading to an unnecessary downgrade to GFP_ATOMIC.
Pre-existing issues:
- [High] Unprotected iterations of `td->applications` can race with dynamic list additions in softirq context.
--
commit 2b43dba239544b5949bf20a7a3f499c51db9632b
Author: Nguyen Ngoc Thang [off-list ref]
Subject: HID: multitouch: use GFP_ATOMIC for report/application/usage allocations
This commit changes the allocation flags for multitouch report data,
applications, and usages from GFP_KERNEL to GFP_ATOMIC to prevent lockdep
warnings about sleeping in softirq context.
Link: https://syzkaller.appspot.com/bug?extid=093e05755c2d99caab91
@@ -604,7 +604,8 @@ static struct mt_usages *mt_allocate_usage(struct hid_device *hdev,{structmt_usages*usage;-usage=devm_kzalloc(&hdev->dev,sizeof(*usage),GFP_KERNEL);+/* may run from hid_report_raw_event() in (soft)irq context */+usage=devm_kzalloc(&hdev->dev,sizeof(*usage),GFP_ATOMIC);
[Severity: Medium]
Is this downgrade to GFP_ATOMIC necessary?
Looking at the call chain, it appears mt_allocate_usage() is only invoked by
mt_store_field(), which is used exclusively via the MT_STORE_FIELD macro in
mt_touch_input_mapping().
Since mt_touch_input_mapping() is only called during device probe
(hid_hw_start()) and is never invoked from mt_event() or any softirq context,
using GFP_ATOMIC here might unnecessarily increase the risk of allocation
failures during device initialization under memory pressure.
quoted hunk
if (!usage)
return NULL;
@@ -633,8 +634,9 @@ static struct mt_application *mt_allocate_application(struct mt_device *td, unsigned int application = report->application; struct mt_application *mt_application;+ /* may run from hid_report_raw_event() in (soft)irq context */ mt_application = devm_kzalloc(&td->hdev->dev, sizeof(*mt_application),- GFP_KERNEL);+ GFP_ATOMIC);
[Severity: High]
This is a pre-existing issue, but can this allocation and subsequent list
addition race with other list iterations?
When an unknown or unmapped report ID is received, mt_event() can call
mt_allocate_application() from softirq context, which subsequently appends
the new application to td->applications:
drivers/hid/hid-multitouch.c:mt_allocate_application() {
...
list_add_tail(&mt_application->list, &td->applications);
...
}
If this happens concurrently with mt_set_quirks() (running in sysfs process
context) or mt_release_contacts() (running in a timer callback) which both
iterate td->applications without locks:
drivers/hid/hid-multitouch.c:mt_release_contacts() {
...
list_for_each_entry(application, &td->applications, list) {
...
}
Could this concurrent modification and unprotected iteration lead to list
corruption or infinite loops? A malicious or buggy HID device might be able
to trigger this by sending unexpected report IDs.
[ ... ]
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260910153746.16222-1-ngocthang2710.1999@gmail.com?part=1
mt_allocate_report_data() and mt_allocate_application() devm_kzalloc()
with GFP_KERNEL, but they are reachable from mt_event()/mt_report()
via hid_report_raw_event(), which can run in (soft)irq context off a
USB URB completion (e.g. dummy_hcd's hrtimer softirq). GFP_KERNEL
there may call into fs_reclaim, which lockdep flags as an
inconsistent {SOFTIRQ-ON-W} -> {IN-SOFTIRQ-W} use of fs_reclaim,
alongside a "sleeping function called from invalid context" splat.
Switch both to GFP_ATOMIC since neither is on a path that needs to
sleep. mt_allocate_usage() is left at GFP_KERNEL: it is only reached
from mt_touch_input_mapping(), which runs at probe time (hid_hw_start()),
never from mt_event(), so it does not need the downgrade.
Reported-by: syzbot+093e05755c2d99caab91@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=093e05755c2d99caab91
Signed-off-by: Nguyen Ngoc Thang <redacted>
---
v2:
- Keep mt_allocate_usage() at GFP_KERNEL. It is only reached from
mt_touch_input_mapping(), at probe time, never from mt_event(), so
the GFP_ATOMIC downgrade there was unnecessary.
(feedback from sashiko-bot)
---
drivers/hid/hid-multitouch.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
@@ -604,6 +604,7 @@ static struct mt_usages *mt_allocate_usage(struct hid_device *hdev,{structmt_usages*usage;+/* only called from mt_touch_input_mapping(), at probe time */usage=devm_kzalloc(&hdev->dev,sizeof(*usage),GFP_KERNEL);if(!usage)returnNULL;
@@ -633,8 +634,9 @@ static struct mt_application *mt_allocate_application(struct mt_device *td,unsignedintapplication=report->application;structmt_application*mt_application;+/* may run from hid_report_raw_event() in (soft)irq context */mt_application=devm_kzalloc(&td->hdev->dev,sizeof(*mt_application),-GFP_KERNEL);+GFP_ATOMIC);if(!mt_application)returnNULL;
@@ -692,7 +694,8 @@ static struct mt_report_data *mt_allocate_report_data(struct mt_device *td,structhid_field*field;intr,n;-rdata=devm_kzalloc(&td->hdev->dev,sizeof(*rdata),GFP_KERNEL);+/* may run from hid_report_raw_event() in (soft)irq context */+rdata=devm_kzalloc(&td->hdev->dev,sizeof(*rdata),GFP_ATOMIC);if(!rdata)returnNULL;
mt_allocate_report_data() and mt_allocate_application() devm_kzalloc()
with GFP_KERNEL, but they are reachable from mt_event()/mt_report()
via hid_report_raw_event(), which can run in (soft)irq context off a
USB URB completion (e.g. dummy_hcd's hrtimer softirq). GFP_KERNEL
there may call into fs_reclaim, which lockdep flags as an
inconsistent {SOFTIRQ-ON-W} -> {IN-SOFTIRQ-W} use of fs_reclaim,
alongside a "sleeping function called from invalid context" splat.
Switch both to GFP_ATOMIC since neither is on a path that needs to
sleep. mt_allocate_usage() is left at GFP_KERNEL: it is only reached
from mt_touch_input_mapping(), which runs at probe time (hid_hw_start()),
never from mt_event(), so it does not need the downgrade.
Reported-by: syzbot+093e05755c2d99caab91@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=093e05755c2d99caab91
Signed-off-by: Nguyen Ngoc Thang <redacted>
---
v2:
- Keep mt_allocate_usage() at GFP_KERNEL. It is only reached from
mt_touch_input_mapping(), at probe time, never from mt_event(), so
the GFP_ATOMIC downgrade there was unnecessary.
(feedback from sashiko-bot)
---
drivers/hid/hid-multitouch.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
@@ -604,6 +604,7 @@ static struct mt_usages *mt_allocate_usage(struct hid_device *hdev,{structmt_usages*usage;+/* only called from mt_touch_input_mapping(), at probe time */usage=devm_kzalloc(&hdev->dev,sizeof(*usage),GFP_KERNEL);if(!usage)returnNULL;
@@ -633,8 +634,9 @@ static struct mt_application *mt_allocate_application(struct mt_device *td,unsignedintapplication=report->application;structmt_application*mt_application;+/* may run from hid_report_raw_event() in (soft)irq context */mt_application=devm_kzalloc(&td->hdev->dev,sizeof(*mt_application),-GFP_KERNEL);+GFP_ATOMIC);if(!mt_application)returnNULL;
@@ -692,7 +694,8 @@ static struct mt_report_data *mt_allocate_report_data(struct mt_device *td,structhid_field*field;intr,n;-rdata=devm_kzalloc(&td->hdev->dev,sizeof(*rdata),GFP_KERNEL);+/* may run from hid_report_raw_event() in (soft)irq context */+rdata=devm_kzalloc(&td->hdev->dev,sizeof(*rdata),GFP_ATOMIC);if(!rdata)returnNULL;
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but lockless modification of the `td->applications` list can race with concurrent iterations, leading to list corruption.
--
commit b30bba4fe7febcb978a1ecb8c9146a4d278538eb
Author: Nguyen Ngoc Thang [off-list ref]
HID: multitouch: use GFP_ATOMIC for report/application allocations
This commit fixes a lockdep warning by changing devm_kzalloc() calls in
mt_allocate_report_data() and mt_allocate_application() to use GFP_ATOMIC.
This resolves the issue where these functions could be called in softirq
context and improperly attempt to sleep.
Reported-by: syzbot+093e05755c2d99caab91@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=093e05755c2d99caab91
Signed-off-by: Nguyen Ngoc Thang <redacted>
@@ -633,8 +634,9 @@ static struct mt_application *mt_allocate_application(struct mt_device *td, unsigned int application = report->application; struct mt_application *mt_application;+ /* may run from hid_report_raw_event() in (soft)irq context */ mt_application = devm_kzalloc(&td->hdev->dev, sizeof(*mt_application),- GFP_KERNEL);+ GFP_ATOMIC); if (!mt_application) return NULL;
[Severity: High]
This is a pre-existing issue, but does lockless modification of the
td->applications list later in this function race with concurrent iterations?
A malicious USB device could send a touch report to arm the
mt_expired_timeout timer, and subsequently send an unmapped feature or input
report. The unmapped report triggers mt_report() -> mt_allocate_application()
in softirq context, mutating the td->applications list via list_add_tail().
Concurrently, the timer callback mt_release_contacts() iterates this same
list locklessly:
mt_release_contacts()
list_for_each_entry(application, &td->applications, list) {
...
}
Similarly, a local user writing to the quirks sysfs attribute triggers
mt_set_quirks(), which also iterates the list without synchronization.
Could this concurrent modification and iteration corrupt the linked list
pointers and lead to a use-after-free or kernel panic?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260910162331.31578-1-ngocthang2710.1999@gmail.com?part=1
mt_allocate_report_data() and mt_allocate_application() devm_kzalloc()
with GFP_KERNEL, but they are reachable from mt_event()/mt_report()
via hid_report_raw_event(), which can run in (soft)irq context off a
USB URB completion (e.g. dummy_hcd's hrtimer softirq). GFP_KERNEL
there may call into fs_reclaim, which lockdep flags as an
inconsistent {SOFTIRQ-ON-W} -> {IN-SOFTIRQ-W} use of fs_reclaim,
alongside a "sleeping function called from invalid context" splat.
Switch both to GFP_ATOMIC since neither is on a path that needs to
sleep. mt_allocate_usage() is left at GFP_KERNEL: it is only reached
from mt_touch_input_mapping(), which runs at probe time (hid_hw_start()),
never from mt_event(), so it does not need the downgrade.
While auditing that same softirq path, td->applications turned out to
be walked and mutated without any lock: mt_allocate_application() can
list_add_tail() to it from mt_event()/mt_report() in (soft)irq
context, while mt_release_contacts() (timer callback) and
mt_set_quirks() (sysfs write) each list_for_each_entry() over it from
their own, independent contexts. On SMP those can run concurrently
and corrupt the list. Add application_list_lock (spin_lock_irqsave,
since one user is a timer/irq context) around every access to
td->applications, and hold it across mt_find_application()'s whole
search-then-allocate sequence so two callers can never both decide to
create the same application.
Reported-by: syzbot+093e05755c2d99caab91@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=093e05755c2d99caab91
Signed-off-by: Nguyen Ngoc Thang <redacted>
---
v3:
- Add application_list_lock to serialize td->applications against the
concurrent softirq/timer/sysfs accessors flagged by sashiko-bot
(pre-existing race, not introduced by this patch, but fixed here
since it's on the same softirq-safety path).
v2:
- Keep mt_allocate_usage() at GFP_KERNEL. It is only reached from
mt_touch_input_mapping(), at probe time, never from mt_event(), so
the GFP_ATOMIC downgrade there was unnecessary.
(feedback from sashiko-bot)
---
drivers/hid/hid-multitouch.c | 32 ++++++++++++++++++++++++++++++--
1 file changed, 30 insertions(+), 2 deletions(-)
@@ -186,6 +187,11 @@ struct mt_device {boolserial_maybe;/* need to check for serial protocol */structlist_headapplications;+spinlock_tapplication_list_lock;/* protects applications, incl.+*concurrentaddfromsoftirq+*vs.list_for_each_entryfrom+*timer/sysfscontext+*/structlist_headreports;};
@@ -604,6 +613,7 @@ static struct mt_usages *mt_allocate_usage(struct hid_device *hdev,{structmt_usages*usage;+/* only called from mt_touch_input_mapping(), at probe time */usage=devm_kzalloc(&hdev->dev,sizeof(*usage),GFP_KERNEL);if(!usage)returnNULL;
@@ -627,14 +637,16 @@ static struct mt_usages *mt_allocate_usage(struct hid_device *hdev,returnusage;}+/* called with td->application_list_lock held */staticstructmt_application*mt_allocate_application(structmt_device*td,structhid_report*report){unsignedintapplication=report->application;structmt_application*mt_application;+/* may run from hid_report_raw_event() in (soft)irq context */mt_application=devm_kzalloc(&td->hdev->dev,sizeof(*mt_application),-GFP_KERNEL);+GFP_ATOMIC);if(!mt_application)returnNULL;
@@ -692,7 +715,8 @@ static struct mt_report_data *mt_allocate_report_data(struct mt_device *td,structhid_field*field;intr,n;-rdata=devm_kzalloc(&td->hdev->dev,sizeof(*rdata),GFP_KERNEL);+/* may run from hid_report_raw_event() in (soft)irq context */+rdata=devm_kzalloc(&td->hdev->dev,sizeof(*rdata),GFP_ATOMIC);if(!rdata)returnNULL;
mt_allocate_report_data() and mt_allocate_application() devm_kzalloc()
with GFP_KERNEL, but they are reachable from mt_event()/mt_report()
via hid_report_raw_event(), which can run in (soft)irq context off a
USB URB completion (e.g. dummy_hcd's hrtimer softirq). GFP_KERNEL
there may call into fs_reclaim, which lockdep flags as an
inconsistent {SOFTIRQ-ON-W} -> {IN-SOFTIRQ-W} use of fs_reclaim,
alongside a "sleeping function called from invalid context" splat.
Switch both to GFP_ATOMIC since neither is on a path that needs to
sleep. mt_allocate_usage() is left at GFP_KERNEL: it is only reached
from mt_touch_input_mapping(), which runs at probe time (hid_hw_start()),
never from mt_event(), so it does not need the downgrade.
While auditing that same softirq path, td->applications turned out to
be walked and mutated without any lock: mt_allocate_application() can
list_add_tail() to it from mt_event()/mt_report() in (soft)irq
context, while mt_release_contacts() (timer callback) and
mt_set_quirks() (sysfs write) each list_for_each_entry() over it from
their own, independent contexts. On SMP those can run concurrently
and corrupt the list. Add application_list_lock (spin_lock_irqsave,
since one user is a timer/irq context) around every access to
td->applications, and hold it across mt_find_application()'s whole
search-then-allocate sequence so two callers can never both decide to
create the same application.
Reported-by: syzbot+093e05755c2d99caab91@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=093e05755c2d99caab91
Signed-off-by: Nguyen Ngoc Thang <redacted>
---
v3:
- Add application_list_lock to serialize td->applications against the
concurrent softirq/timer/sysfs accessors flagged by sashiko-bot
(pre-existing race, not introduced by this patch, but fixed here
since it's on the same softirq-safety path).
v2:
- Keep mt_allocate_usage() at GFP_KERNEL. It is only reached from
mt_touch_input_mapping(), at probe time, never from mt_event(), so
the GFP_ATOMIC downgrade there was unnecessary.
(feedback from sashiko-bot)
---
drivers/hid/hid-multitouch.c | 32 ++++++++++++++++++++++++++++++--
1 file changed, 30 insertions(+), 2 deletions(-)
@@ -186,6 +187,11 @@ struct mt_device {boolserial_maybe;/* need to check for serial protocol */structlist_headapplications;+spinlock_tapplication_list_lock;/* protects applications, incl.+*concurrentaddfromsoftirq+*vs.list_for_each_entryfrom+*timer/sysfscontext+*/structlist_headreports;};
@@ -604,6 +613,7 @@ static struct mt_usages *mt_allocate_usage(struct hid_device *hdev,{structmt_usages*usage;+/* only called from mt_touch_input_mapping(), at probe time */usage=devm_kzalloc(&hdev->dev,sizeof(*usage),GFP_KERNEL);if(!usage)returnNULL;
@@ -627,14 +637,16 @@ static struct mt_usages *mt_allocate_usage(struct hid_device *hdev,returnusage;}+/* called with td->application_list_lock held */staticstructmt_application*mt_allocate_application(structmt_device*td,structhid_report*report){unsignedintapplication=report->application;structmt_application*mt_application;+/* may run from hid_report_raw_event() in (soft)irq context */mt_application=devm_kzalloc(&td->hdev->dev,sizeof(*mt_application),-GFP_KERNEL);+GFP_ATOMIC);if(!mt_application)returnNULL;
@@ -692,7 +715,8 @@ static struct mt_report_data *mt_allocate_report_data(struct mt_device *td,structhid_field*field;intr,n;-rdata=devm_kzalloc(&td->hdev->dev,sizeof(*rdata),GFP_KERNEL);+/* may run from hid_report_raw_event() in (soft)irq context */+rdata=devm_kzalloc(&td->hdev->dev,sizeof(*rdata),GFP_ATOMIC);if(!rdata)returnNULL;