kernel: kcov: a possible sleep-in-atomic-context bug in kcov_ioctl()
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_putkernel/kcov.c, 413:
kcov_put in kcov_ioctl_lockedkernel/kcov.c, 427:
kcov_ioctl_locked in kcov_ioctlkernel/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
Quoted 13 lines · Jia-Ju Bai
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 inkcov_putkernel/kcov.c, 413:
kcov_putinkcov_ioctl_lockedkernel/kcov.c, 427:
kcov_ioctl_lockedinkcov_ioctlkernel/kcov.c, 426:
spin_lockinkcov_ioctl
Quoted 7 lines
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()withkfree(), and replace related calls tovmalloc()withkmalloc().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.
Quoted 19 lines · Dmitry Vyukov
Quoted 18 lines · Jia-Ju Bai
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 inkcov_putkernel/kcov.c, 413:
kcov_putinkcov_ioctl_lockedkernel/kcov.c, 427:
kcov_ioctl_lockedinkcov_ioctlkernel/kcov.c, 426:
spin_lockinkcov_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()withkfree(), and replace related calls tovmalloc()withkmalloc().
Quoted 7 lines
Quoted 1 line
This bug is found by a static analysis tool STCheck written by myself.
Hi Jia-Ju,
Are you sure
kcov_ioctl_lockedcan 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
Quoted 28 lines · Jia-Ju Bai
Quoted 19 lines · Dmitry Vyukov
Quoted 18 lines · Jia-Ju Bai
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 inkcov_putkernel/kcov.c, 413:
kcov_putinkcov_ioctl_lockedkernel/kcov.c, 427:
kcov_ioctl_lockedinkcov_ioctlkernel/kcov.c, 426:
spin_lockinkcov_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()withkfree(), and replace related calls tovmalloc()withkmalloc().Quoted 7 lines
Quoted 1 line
This bug is found by a static analysis tool STCheck written by myself.
Hi Jia-Ju,
Are you sure
kcov_ioctl_lockedcan 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.
Quoted 4 lines
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.
Quoted 28 lines · Dmitry Vyukov
Quoted 27 lines · Jia-Ju Bai
Quoted 26 lines · Dmitry Vyukov
Quoted 12 lines · Jia-Ju Bai
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 inkcov_putkernel/kcov.c, 413:
kcov_putinkcov_ioctl_lockedkernel/kcov.c, 427:
kcov_ioctl_lockedinkcov_ioctlkernel/kcov.c, 426:
spin_lockinkcov_ioctlQuoted 7 lines
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()withkfree(), and replace related calls tovmalloc()withkmalloc().This bug is found by a static analysis tool STCheck written by myself.
Hi Jia-Ju,
Are you sure
kcov_ioctl_lockedcan 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.
Quoted 6 lines
Quoted 4 lines
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_putshould never call vfree. We still hold reference associated with the file, which will be released inkcov_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