mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [BUG] kernel: kcov: a possible sleep-in-atomic-context bug in kcov_ioctl()
@ 2018-06-21  3:20 Jia-Ju Bai
  2018-06-21  3:43 ` Al Viro
  0 siblings, 1 reply; 8+ messages in thread
From: Jia-Ju Bai @ 2018-06-21  3:20 UTC (permalink / raw)
  To: akpm, dvyukov, Greg KH, tchibo, aryabinin; +Cc: Linux Kernel Mailing List

The kernel may sleep with holding a spinlock.
The function call path (from bottom to top) in Linux-4.16.7 is:

[FUNC] vfree --> can sleep
kernel/kcov.c, 237: vfree in kcov_put
kernel/kcov.c, 396: kcov_put in kcov_ioctl_locked
kernel/kcov.c, 410: kcov_ioctl_locked in kcov_ioctl
kernel/kcov.c, 409: spin_lock in kcov_ioctl

This bug is found by my static analysis tool (DSAC-2) and checked by my
code review.

I do not know how to correctly fix this bug, so I just report them.


Best wishes,
Jia-Ju Bai

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [BUG] kernel: kcov: a possible sleep-in-atomic-context bug in kcov_ioctl()
  2018-06-21  3:20 [BUG] kernel: kcov: a possible sleep-in-atomic-context bug in kcov_ioctl() Jia-Ju Bai
@ 2018-06-21  3:43 ` Al Viro
  2018-06-21  3:50   ` Jia-Ju Bai
  0 siblings, 1 reply; 8+ messages in thread
From: Al Viro @ 2018-06-21  3:43 UTC (permalink / raw)
  To: Jia-Ju Bai
  Cc: akpm, dvyukov, Greg KH, tchibo, aryabinin, Linux Kernel Mailing List

On Thu, Jun 21, 2018 at 11:20:59AM +0800, Jia-Ju Bai wrote:
> The kernel may sleep with holding a spinlock.
> The function call path (from bottom to top) in Linux-4.16.7 is:
> 
> [FUNC] vfree --> can sleep
> kernel/kcov.c, 237: vfree in kcov_put
> kernel/kcov.c, 396: kcov_put in kcov_ioctl_locked
> kernel/kcov.c, 410: kcov_ioctl_locked in kcov_ioctl
> kernel/kcov.c, 409: spin_lock in kcov_ioctl
> 
> This bug is found by my static analysis tool (DSAC-2) and checked by my
> code review.
> 
> I do not know how to correctly fix this bug, so I just report them.

Assuming it's a bug in the first place, that is.  Note that
	* we never modify task->kcov for task != current
	* task->kcov contributes to refcount
	* opened file contributes to refcount
	* that kcov_put() of yours happens from ->ioctl() and removes
current->kcov reference; it *can't* be the last reference - the one
held by struct file used to call ->ioctl() is also there.

IOW, it's a false positive.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [BUG] kernel: kcov: a possible sleep-in-atomic-context bug in kcov_ioctl()
  2018-06-21  3:43 ` Al Viro
@ 2018-06-21  3:50   ` Jia-Ju Bai
  0 siblings, 0 replies; 8+ messages in thread
From: Jia-Ju Bai @ 2018-06-21  3:50 UTC (permalink / raw)
  To: Al Viro
  Cc: akpm, dvyukov, Greg KH, tchibo, aryabinin, Linux Kernel Mailing List



On 2018/6/21 11:43, Al Viro wrote:
> On Thu, Jun 21, 2018 at 11:20:59AM +0800, Jia-Ju Bai wrote:
>> The kernel may sleep with holding a spinlock.
>> The function call path (from bottom to top) in Linux-4.16.7 is:
>>
>> [FUNC] vfree --> can sleep
>> kernel/kcov.c, 237: vfree in kcov_put
>> kernel/kcov.c, 396: kcov_put in kcov_ioctl_locked
>> kernel/kcov.c, 410: kcov_ioctl_locked in kcov_ioctl
>> kernel/kcov.c, 409: spin_lock in kcov_ioctl
>>
>> This bug is found by my static analysis tool (DSAC-2) and checked by my
>> code review.
>>
>> I do not know how to correctly fix this bug, so I just report them.
> Assuming it's a bug in the first place, that is.  Note that
> 	* we never modify task->kcov for task != current
> 	* task->kcov contributes to refcount
> 	* opened file contributes to refcount
> 	* that kcov_put() of yours happens from ->ioctl() and removes
> current->kcov reference; it *can't* be the last reference - the one
> held by struct file used to call ->ioctl() is also there.
>
> IOW, it's a false positive.

Okay, thanks for your reply and explanation.
I agree that my report is false.


Best wishes,
Jia-Ju Bai

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [BUG] kernel: kcov: a possible sleep-in-atomic-context bug in kcov_ioctl()
  2019-12-17 13:13     ` Dmitry Vyukov
@ 2019-12-17 13:17       ` Jia-Ju Bai
  0 siblings, 0 replies; 8+ messages in thread
From: Jia-Ju Bai @ 2019-12-17 13:17 UTC (permalink / raw)
  To: Dmitry Vyukov
  Cc: Andrew Morton, Hans Liljestrand, Steven Rostedt, Andrea Parri,
	Anders Roxell, Greg Kroah-Hartman, Reshetova, Elena,
	Andrey Konovalov, LKML



On 2019/12/17 21:13, Dmitry Vyukov wrote:
> On Tue, Dec 17, 2019 at 2:11 PM Jia-Ju Bai <baijiaju1990@gmail.com> wrote:
>> On 2019/12/17 21:02, Dmitry Vyukov wrote:
>>> On Tue, Dec 17, 2019 at 1:56 PM Jia-Ju Bai <baijiaju1990@gmail.com> wrote:
>>>> The kernel may sleep while holding a spinlock.
>>>> The function call path (from bottom to top) in Linux 4.19 is:
>>>>
>>>> kernel/kcov.c, 237:
>>>>        vfree in kcov_put
>>>> kernel/kcov.c, 413:
>>>>        kcov_put in kcov_ioctl_locked
>>>> kernel/kcov.c, 427:
>>>>        kcov_ioctl_locked in kcov_ioctl
>>>> kernel/kcov.c, 426:
>>>>        spin_lock in kcov_ioctl
>>>>
>>>> vfree() can sleep at runtime.
>>>>
>>>> I am not sure how to properly fix this possible bug, so I only report it.
>>>> A possible way is to replace vfree() with kfree(), and replace related
>>>> calls to vmalloc() with kmalloc().
>>>>
>>>> This bug is found by a static analysis tool STCheck written by myself.
>>> Hi Jia-Ju,
>>>
>>> Are you sure kcov_ioctl_locked can really release the descriptor? It
>>> happens in the context of ioctl, which means there is an open
>>> reference for the file descriptor. So ioctl should not do vfree I
>>> would assume.
>> Thanks for the reply :)
>> I am not sure, because I am not familiar with kcov.
>> But looking at the code, if the reference count of kcov is 1, vfree()
>> could be called.
> That kcov_put should never call vfree. We still hold reference
> associated with the file, which will be released in kcov_close.

Okay, thanks for the explanation :)
My static analysis tool does not know this fact, so it reports the false 
bug, sorry...


Best wishes,
Jia-Ju Bai

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [BUG] kernel: kcov: a possible sleep-in-atomic-context bug in kcov_ioctl()
  2019-12-17 13:11   ` Jia-Ju Bai
@ 2019-12-17 13:13     ` Dmitry Vyukov
  2019-12-17 13:17       ` Jia-Ju Bai
  0 siblings, 1 reply; 8+ messages in thread
From: Dmitry Vyukov @ 2019-12-17 13:13 UTC (permalink / raw)
  To: Jia-Ju Bai
  Cc: Andrew Morton, Hans Liljestrand, Steven Rostedt, Andrea Parri,
	Anders Roxell, Greg Kroah-Hartman, Reshetova, Elena,
	Andrey Konovalov, LKML

On Tue, Dec 17, 2019 at 2:11 PM Jia-Ju Bai <baijiaju1990@gmail.com> wrote:
> On 2019/12/17 21:02, Dmitry Vyukov wrote:
> > On Tue, Dec 17, 2019 at 1:56 PM Jia-Ju Bai <baijiaju1990@gmail.com> wrote:
> >> The kernel may sleep while holding a spinlock.
> >> The function call path (from bottom to top) in Linux 4.19 is:
> >>
> >> kernel/kcov.c, 237:
> >>       vfree in kcov_put
> >> kernel/kcov.c, 413:
> >>       kcov_put in kcov_ioctl_locked
> >> kernel/kcov.c, 427:
> >>       kcov_ioctl_locked in kcov_ioctl
> >> kernel/kcov.c, 426:
> >>       spin_lock in kcov_ioctl
> >>
> >> vfree() can sleep at runtime.
> >>
> >> I am not sure how to properly fix this possible bug, so I only report it.
> >> A possible way is to replace vfree() with kfree(), and replace related
> >> calls to vmalloc() with kmalloc().
> >>
> >> This bug is found by a static analysis tool STCheck written by myself.
> > Hi Jia-Ju,
> >
> > Are you sure kcov_ioctl_locked can really release the descriptor? It
> > happens in the context of ioctl, which means there is an open
> > reference for the file descriptor. So ioctl should not do vfree I
> > would assume.
>
> Thanks for the reply :)
> I am not sure, because I am not familiar with kcov.
> But looking at the code, if the reference count of kcov is 1, vfree()
> could be called.

That kcov_put should never call vfree. We still hold reference
associated with the file, which will be released in kcov_close.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [BUG] kernel: kcov: a possible sleep-in-atomic-context bug in kcov_ioctl()
  2019-12-17 13:02 ` Dmitry Vyukov
@ 2019-12-17 13:11   ` Jia-Ju Bai
  2019-12-17 13:13     ` Dmitry Vyukov
  0 siblings, 1 reply; 8+ messages in thread
From: Jia-Ju Bai @ 2019-12-17 13:11 UTC (permalink / raw)
  To: Dmitry Vyukov
  Cc: Andrew Morton, Hans Liljestrand, Steven Rostedt, Andrea Parri,
	Anders Roxell, Greg Kroah-Hartman, Reshetova, Elena,
	Andrey Konovalov, LKML



On 2019/12/17 21:02, Dmitry Vyukov wrote:
> On Tue, Dec 17, 2019 at 1:56 PM Jia-Ju Bai <baijiaju1990@gmail.com> wrote:
>> The kernel may sleep while holding a spinlock.
>> The function call path (from bottom to top) in Linux 4.19 is:
>>
>> kernel/kcov.c, 237:
>>       vfree in kcov_put
>> kernel/kcov.c, 413:
>>       kcov_put in kcov_ioctl_locked
>> kernel/kcov.c, 427:
>>       kcov_ioctl_locked in kcov_ioctl
>> kernel/kcov.c, 426:
>>       spin_lock in kcov_ioctl
>>
>> vfree() can sleep at runtime.
>>
>> I am not sure how to properly fix this possible bug, so I only report it.
>> A possible way is to replace vfree() with kfree(), and replace related
>> calls to vmalloc() with kmalloc().
>>
>> This bug is found by a static analysis tool STCheck written by myself.
> Hi Jia-Ju,
>
> Are you sure kcov_ioctl_locked can really release the descriptor? It
> happens in the context of ioctl, which means there is an open
> reference for the file descriptor. So ioctl should not do vfree I
> would assume.

Thanks for the reply :)
I am not sure, because I am not familiar with kcov.
But looking at the code, if the reference count of kcov is 1, vfree() 
could be called.


Best wishes,
Jia-Ju Bai

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [BUG] kernel: kcov: a possible sleep-in-atomic-context bug in kcov_ioctl()
  2019-12-17 12:56 Jia-Ju Bai
@ 2019-12-17 13:02 ` Dmitry Vyukov
  2019-12-17 13:11   ` Jia-Ju Bai
  0 siblings, 1 reply; 8+ messages in thread
From: Dmitry Vyukov @ 2019-12-17 13:02 UTC (permalink / raw)
  To: Jia-Ju Bai
  Cc: Andrew Morton, Hans Liljestrand, Steven Rostedt, Andrea Parri,
	Anders Roxell, Greg Kroah-Hartman, Reshetova, Elena,
	Andrey Konovalov, LKML

On Tue, Dec 17, 2019 at 1:56 PM Jia-Ju Bai <baijiaju1990@gmail.com> wrote:
>
> The kernel may sleep while holding a spinlock.
> The function call path (from bottom to top) in Linux 4.19 is:
>
> kernel/kcov.c, 237:
>      vfree in kcov_put
> kernel/kcov.c, 413:
>      kcov_put in kcov_ioctl_locked
> kernel/kcov.c, 427:
>      kcov_ioctl_locked in kcov_ioctl
> kernel/kcov.c, 426:
>      spin_lock in kcov_ioctl
>
> vfree() can sleep at runtime.
>
> I am not sure how to properly fix this possible bug, so I only report it.
> A possible way is to replace vfree() with kfree(), and replace related
> calls to vmalloc() with kmalloc().
>
> This bug is found by a static analysis tool STCheck written by myself.

Hi Jia-Ju,

Are you sure kcov_ioctl_locked can really release the descriptor? It
happens in the context of ioctl, which means there is an open
reference for the file descriptor. So ioctl should not do vfree I
would assume.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [BUG] kernel: kcov: a possible sleep-in-atomic-context bug in kcov_ioctl()
@ 2019-12-17 12:56 Jia-Ju Bai
  2019-12-17 13:02 ` Dmitry Vyukov
  0 siblings, 1 reply; 8+ messages in thread
From: Jia-Ju Bai @ 2019-12-17 12:56 UTC (permalink / raw)
  To: akpm, ishkamiel, rostedt, dvyukov, andrea.parri, anders.roxell,
	Greg Kroah-Hartman, elena.reshetova, andreyknvl
  Cc: linux-kernel

The kernel may sleep while holding a spinlock.
The function call path (from bottom to top) in Linux 4.19 is:

kernel/kcov.c, 237:
     vfree in kcov_put
kernel/kcov.c, 413:
     kcov_put in kcov_ioctl_locked
kernel/kcov.c, 427:
     kcov_ioctl_locked in kcov_ioctl
kernel/kcov.c, 426:
     spin_lock in kcov_ioctl

vfree() can sleep at runtime.

I am not sure how to properly fix this possible bug, so I only report it.
A possible way is to replace vfree() with kfree(), and replace related 
calls to vmalloc() with kmalloc().

This bug is found by a static analysis tool STCheck written by myself.


Best wishes,
Jia-Ju Bai

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2019-12-17 13:17 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-06-21  3:20 [BUG] kernel: kcov: a possible sleep-in-atomic-context bug in kcov_ioctl() Jia-Ju Bai
2018-06-21  3:43 ` Al Viro
2018-06-21  3:50   ` Jia-Ju Bai
2019-12-17 12:56 Jia-Ju Bai
2019-12-17 13:02 ` Dmitry Vyukov
2019-12-17 13:11   ` Jia-Ju Bai
2019-12-17 13:13     ` Dmitry Vyukov
2019-12-17 13:17       ` Jia-Ju Bai

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

Powered by JetHome