2018-09-01 12:18:25

by Jia-Ju Bai

[permalink] [raw]
Subject: [PATCH] hid: hid-core: Fix a sleep-in-atomic-context bug in __hid_request()

The driver may sleep with holding a spinlock.

The function call paths (from bottom to top) in Linux-4.16 are:

[FUNC] hid_alloc_report_buf(GFP_KERNEL)
drivers/hid/hid-core.c, 1435:
hid_alloc_report_buf in __hid_request
./include/linux/hid.h, 1023:
__hid_request in hid_hw_request
drivers/hid/hid-picolcd_core.c, 111:
hid_hw_request in picolcd_send_and_wait
drivers/hid/hid-picolcd_core.c, 100:
_raw_spin_lock_irqsave in picolcd_send_and_wait

[FUNC] hid_alloc_report_buf(GFP_KERNEL)
drivers/hid/hid-core.c, 1435:
hid_alloc_report_buf in __hid_request
./include/linux/hid.h, 1023:
__hid_request in hid_hw_request
drivers/hid/hid-picolcd_core.c, 245:
hid_hw_request in picolcd_reset
drivers/hid/hid-picolcd_core.c, 235:
_raw_spin_lock_irqsave in picolcd_reset

[FUNC] hid_alloc_report_buf(GFP_KERNEL)
drivers/hid/hid-core.c, 1435:
hid_alloc_report_buf in __hid_request
./include/linux/hid.h, 1023:
__hid_request in hid_hw_request
drivers/hid/hid-picolcd_fb.c, 215:
hid_hw_request in picolcd_fb_reset
drivers/hid/hid-picolcd_fb.c, 206:
_raw_spin_lock_irqsave in picolcd_fb_reset

[FUNC] hid_alloc_report_buf(GFP_KERNEL)
drivers/hid/hid-core.c, 1435:
hid_alloc_report_buf in __hid_request
./include/linux/hid.h, 1023:
__hid_request in hid_hw_request
drivers/hid/hid-lg4ff.c, 465:
hid_hw_request in lg4ff_play
drivers/hid/hid-lg4ff.c, 441:
_raw_spin_lock_irqsave in lg4ff_play

To fix this bug, GFP_KERNEL is replaced with GFP_ATOMIC.

This bug is found by my static analysis tool DSAC.

Signed-off-by: Jia-Ju Bai <[email protected]>
---
drivers/hid/hid-core.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/hid/hid-core.c b/drivers/hid/hid-core.c
index 3942ee61bd1c..c886af00c8c9 100644
--- a/drivers/hid/hid-core.c
+++ b/drivers/hid/hid-core.c
@@ -1442,7 +1442,7 @@ void __hid_request(struct hid_device *hid, struct hid_report *report,
int ret;
u32 len;

- buf = hid_alloc_report_buf(report, GFP_KERNEL);
+ buf = hid_alloc_report_buf(report, GFP_ATOMIC);
if (!buf)
return;

--
2.17.0



2018-09-05 08:30:53

by Jiri Kosina

[permalink] [raw]
Subject: Re: [PATCH] hid: hid-core: Fix a sleep-in-atomic-context bug in __hid_request()

On Sat, 1 Sep 2018, Jia-Ju Bai wrote:

> The driver may sleep with holding a spinlock.
>
> The function call paths (from bottom to top) in Linux-4.16 are:
>
> [FUNC] hid_alloc_report_buf(GFP_KERNEL)
> drivers/hid/hid-core.c, 1435:
> hid_alloc_report_buf in __hid_request
> ./include/linux/hid.h, 1023:
> __hid_request in hid_hw_request
> drivers/hid/hid-picolcd_core.c, 111:
> hid_hw_request in picolcd_send_and_wait
> drivers/hid/hid-picolcd_core.c, 100:
> _raw_spin_lock_irqsave in picolcd_send_and_wait
>
> [FUNC] hid_alloc_report_buf(GFP_KERNEL)
> drivers/hid/hid-core.c, 1435:
> hid_alloc_report_buf in __hid_request
> ./include/linux/hid.h, 1023:
> __hid_request in hid_hw_request
> drivers/hid/hid-picolcd_core.c, 245:
> hid_hw_request in picolcd_reset
> drivers/hid/hid-picolcd_core.c, 235:
> _raw_spin_lock_irqsave in picolcd_reset
>
> [FUNC] hid_alloc_report_buf(GFP_KERNEL)
> drivers/hid/hid-core.c, 1435:
> hid_alloc_report_buf in __hid_request
> ./include/linux/hid.h, 1023:
> __hid_request in hid_hw_request
> drivers/hid/hid-picolcd_fb.c, 215:
> hid_hw_request in picolcd_fb_reset
> drivers/hid/hid-picolcd_fb.c, 206:
> _raw_spin_lock_irqsave in picolcd_fb_reset
>
> [FUNC] hid_alloc_report_buf(GFP_KERNEL)
> drivers/hid/hid-core.c, 1435:
> hid_alloc_report_buf in __hid_request
> ./include/linux/hid.h, 1023:
> __hid_request in hid_hw_request
> drivers/hid/hid-lg4ff.c, 465:
> hid_hw_request in lg4ff_play
> drivers/hid/hid-lg4ff.c, 441:
> _raw_spin_lock_irqsave in lg4ff_play
>
> To fix this bug, GFP_KERNEL is replaced with GFP_ATOMIC.
>
> This bug is found by my static analysis tool DSAC.

Could you please rewrite the changelog so that it's human readable? The
above is a bit hard to understand, I think something along the lines of
"__hid_request() has to be allocating with GFP_ATOMIC because there are
the following callchains leading to __hid_request() being an atomic
context: ... a->b->c.._hid_request()" etc.

Thanks,

--
Jiri Kosina
SUSE Labs


2018-09-06 02:39:23

by Jia-Ju Bai

[permalink] [raw]
Subject: Re: [PATCH] hid: hid-core: Fix a sleep-in-atomic-context bug in __hid_request()



On 2018/9/5 16:29, Jiri Kosina wrote:
> On Sat, 1 Sep 2018, Jia-Ju Bai wrote:
>
>> The driver may sleep with holding a spinlock.
>>
>> The function call paths (from bottom to top) in Linux-4.16 are:
>>
>> [FUNC] hid_alloc_report_buf(GFP_KERNEL)
>> drivers/hid/hid-core.c, 1435:
>> hid_alloc_report_buf in __hid_request
>> ./include/linux/hid.h, 1023:
>> __hid_request in hid_hw_request
>> drivers/hid/hid-picolcd_core.c, 111:
>> hid_hw_request in picolcd_send_and_wait
>> drivers/hid/hid-picolcd_core.c, 100:
>> _raw_spin_lock_irqsave in picolcd_send_and_wait
>>
>> [FUNC] hid_alloc_report_buf(GFP_KERNEL)
>> drivers/hid/hid-core.c, 1435:
>> hid_alloc_report_buf in __hid_request
>> ./include/linux/hid.h, 1023:
>> __hid_request in hid_hw_request
>> drivers/hid/hid-picolcd_core.c, 245:
>> hid_hw_request in picolcd_reset
>> drivers/hid/hid-picolcd_core.c, 235:
>> _raw_spin_lock_irqsave in picolcd_reset
>>
>> [FUNC] hid_alloc_report_buf(GFP_KERNEL)
>> drivers/hid/hid-core.c, 1435:
>> hid_alloc_report_buf in __hid_request
>> ./include/linux/hid.h, 1023:
>> __hid_request in hid_hw_request
>> drivers/hid/hid-picolcd_fb.c, 215:
>> hid_hw_request in picolcd_fb_reset
>> drivers/hid/hid-picolcd_fb.c, 206:
>> _raw_spin_lock_irqsave in picolcd_fb_reset
>>
>> [FUNC] hid_alloc_report_buf(GFP_KERNEL)
>> drivers/hid/hid-core.c, 1435:
>> hid_alloc_report_buf in __hid_request
>> ./include/linux/hid.h, 1023:
>> __hid_request in hid_hw_request
>> drivers/hid/hid-lg4ff.c, 465:
>> hid_hw_request in lg4ff_play
>> drivers/hid/hid-lg4ff.c, 441:
>> _raw_spin_lock_irqsave in lg4ff_play
>>
>> To fix this bug, GFP_KERNEL is replaced with GFP_ATOMIC.
>>
>> This bug is found by my static analysis tool DSAC.
> Could you please rewrite the changelog so that it's human readable? The
> above is a bit hard to understand, I think something along the lines of
> "__hid_request() has to be allocating with GFP_ATOMIC because there are
> the following callchains leading to __hid_request() being an atomic
> context: ... a->b->c.._hid_request()" etc.
>

Okay, I will send a new patch.


Best wishes,
Jia-Ju Bai