[PATCH v2] lib/group_cpus: fix NULL pointer dereference from group_cpus_evenly()

Yu Kuai posted 1 patch 3 months, 3 weeks ago
There is a newer version of this series
lib/group_cpus.c | 3 +++
1 file changed, 3 insertions(+)
[PATCH v2] lib/group_cpus: fix NULL pointer dereference from group_cpus_evenly()
Posted by Yu Kuai 3 months, 3 weeks ago
From: Yu Kuai <yukuai3@huawei.com>

While testing null_blk with configfs, echo 0 > poll_queues will trigger
following panic:

BUG: kernel NULL pointer dereference, address: 0000000000000010
Oops: Oops: 0000 [#1] SMP NOPTI
CPU: 27 UID: 0 PID: 920 Comm: bash Not tainted 6.15.0-02023-gadbdb95c8696-dirty #1238 PREEMPT(undef)
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.16.1-2.fc37 04/01/2014
RIP: 0010:__bitmap_or+0x48/0x70
Call Trace:
 <TASK>
 __group_cpus_evenly+0x822/0x8c0
 group_cpus_evenly+0x2d9/0x490
 blk_mq_map_queues+0x1e/0x110
 null_map_queues+0xc9/0x170 [null_blk]
 blk_mq_update_queue_map+0xdb/0x160
 blk_mq_update_nr_hw_queues+0x22b/0x560
 nullb_update_nr_hw_queues+0x71/0xf0 [null_blk]
 nullb_device_poll_queues_store+0xa4/0x130 [null_blk]
 configfs_write_iter+0x109/0x1d0
 vfs_write+0x26e/0x6f0
 ksys_write+0x79/0x180
 __x64_sys_write+0x1d/0x30
 x64_sys_call+0x45c4/0x45f0
 do_syscall_64+0xa5/0x240
 entry_SYSCALL_64_after_hwframe+0x76/0x7e

Root cause is that numgrps is set to 0, and ZERO_SIZE_PTR is returned
from kcalloc(), then __group_cpus_evenly() will deference the
ZERO_SIZE_PTR.

Fix the problem by checking numgrps first in group_cpus_evenly(), and
return NULL directly if numgrps is zero.

Fixes: 6a6dcae8f486 ("blk-mq: Build default queue map via group_cpus_evenly()")
Signed-off-by: Yu Kuai <yukuai3@huawei.com>
---
Changes in v2:
 - check numgrps directly.

 lib/group_cpus.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/lib/group_cpus.c b/lib/group_cpus.c
index ee272c4cefcc..d68407e3601e 100644
--- a/lib/group_cpus.c
+++ b/lib/group_cpus.c
@@ -352,6 +352,9 @@ struct cpumask *group_cpus_evenly(unsigned int numgrps)
 	int ret = -ENOMEM;
 	struct cpumask *masks = NULL;
 
+	if (numgrps == 0)
+		return NULL;
+
 	if (!zalloc_cpumask_var(&nmsk, GFP_KERNEL))
 		return NULL;
 
-- 
2.39.2
Re: [PATCH v2] lib/group_cpus: fix NULL pointer dereference from group_cpus_evenly()
Posted by John Garry 3 months, 3 weeks ago
On 19/06/2025 14:26, Yu Kuai wrote:
> From: Yu Kuai <yukuai3@huawei.com>
> 
> While testing null_blk with configfs, echo 0 > poll_queues will trigger
> following panic:
> 
> BUG: kernel NULL pointer dereference, address: 0000000000000010
> Oops: Oops: 0000 [#1] SMP NOPTI
> CPU: 27 UID: 0 PID: 920 Comm: bash Not tainted 6.15.0-02023-gadbdb95c8696-dirty #1238 PREEMPT(undef)
> Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.16.1-2.fc37 04/01/2014
> RIP: 0010:__bitmap_or+0x48/0x70
> Call Trace:
>   <TASK>
>   __group_cpus_evenly+0x822/0x8c0
>   group_cpus_evenly+0x2d9/0x490
>   blk_mq_map_queues+0x1e/0x110
>   null_map_queues+0xc9/0x170 [null_blk]
>   blk_mq_update_queue_map+0xdb/0x160
>   blk_mq_update_nr_hw_queues+0x22b/0x560
>   nullb_update_nr_hw_queues+0x71/0xf0 [null_blk]
>   nullb_device_poll_queues_store+0xa4/0x130 [null_blk]
>   configfs_write_iter+0x109/0x1d0
>   vfs_write+0x26e/0x6f0
>   ksys_write+0x79/0x180
>   __x64_sys_write+0x1d/0x30
>   x64_sys_call+0x45c4/0x45f0
>   do_syscall_64+0xa5/0x240
>   entry_SYSCALL_64_after_hwframe+0x76/0x7e
> 
> Root cause is that numgrps is set to 0, and ZERO_SIZE_PTR is returned
> from kcalloc(), then __group_cpus_evenly() will deference the
> ZERO_SIZE_PTR.
> 
> Fix the problem by checking numgrps first in group_cpus_evenly(), and
> return NULL directly if numgrps is zero.
> 
> Fixes: 6a6dcae8f486 ("blk-mq: Build default queue map via group_cpus_evenly()")
> Signed-off-by: Yu Kuai <yukuai3@huawei.com>
> ---
> Changes in v2:
>   - check numgrps directly.
> 
>   lib/group_cpus.c | 3 +++
>   1 file changed, 3 insertions(+)
> 
> diff --git a/lib/group_cpus.c b/lib/group_cpus.c
> index ee272c4cefcc..d68407e3601e 100644
> --- a/lib/group_cpus.c
> +++ b/lib/group_cpus.c
> @@ -352,6 +352,9 @@ struct cpumask *group_cpus_evenly(unsigned int numgrps)
>   	int ret = -ENOMEM;
>   	struct cpumask *masks = NULL;
>   
> +	if (numgrps == 0)
> +		return NULL;
> +
>   	if (!zalloc_cpumask_var(&nmsk, GFP_KERNEL))
>   		return NULL;
>   

There's a non-SMP version of group_cpus_evenly() at the bottom of 
lib/group_cpus.c

It also does a kcalloc(numgrps, ...)

Does that need to be fixed as well?
Re: [PATCH v2] lib/group_cpus: fix NULL pointer dereference from group_cpus_evenly()
Posted by Yu Kuai 3 months, 3 weeks ago
Hi,

在 2025/06/19 22:08, John Garry 写道:
> There's a non-SMP version of group_cpus_evenly() at the bottom of 
> lib/group_cpus.c
> 
> It also does a kcalloc(numgrps, ...)
> 
> Does that need to be fixed as well?

Yes, you're right. Just run the test with SMP disabled, and the problem
can be reporduced as well.

Thanks,
Kuai

Re: [PATCH v2] lib/group_cpus: fix NULL pointer dereference from group_cpus_evenly()
Posted by Jens Axboe 3 months, 3 weeks ago
Reviewed-by: Jens Axboe <axboe@kernel.dk>

-- 
Jens Axboe
Re: [PATCH v2] lib/group_cpus: fix NULL pointer dereference from group_cpus_evenly()
Posted by Ming Lei 3 months, 3 weeks ago
On Thu, Jun 19, 2025 at 9:33 PM Yu Kuai <yukuai1@huaweicloud.com> wrote:
>
> From: Yu Kuai <yukuai3@huawei.com>
>
> While testing null_blk with configfs, echo 0 > poll_queues will trigger
> following panic:
>
> BUG: kernel NULL pointer dereference, address: 0000000000000010
> Oops: Oops: 0000 [#1] SMP NOPTI
> CPU: 27 UID: 0 PID: 920 Comm: bash Not tainted 6.15.0-02023-gadbdb95c8696-dirty #1238 PREEMPT(undef)
> Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.16.1-2.fc37 04/01/2014
> RIP: 0010:__bitmap_or+0x48/0x70
> Call Trace:
>  <TASK>
>  __group_cpus_evenly+0x822/0x8c0
>  group_cpus_evenly+0x2d9/0x490
>  blk_mq_map_queues+0x1e/0x110
>  null_map_queues+0xc9/0x170 [null_blk]
>  blk_mq_update_queue_map+0xdb/0x160
>  blk_mq_update_nr_hw_queues+0x22b/0x560
>  nullb_update_nr_hw_queues+0x71/0xf0 [null_blk]
>  nullb_device_poll_queues_store+0xa4/0x130 [null_blk]
>  configfs_write_iter+0x109/0x1d0
>  vfs_write+0x26e/0x6f0
>  ksys_write+0x79/0x180
>  __x64_sys_write+0x1d/0x30
>  x64_sys_call+0x45c4/0x45f0
>  do_syscall_64+0xa5/0x240
>  entry_SYSCALL_64_after_hwframe+0x76/0x7e
>
> Root cause is that numgrps is set to 0, and ZERO_SIZE_PTR is returned
> from kcalloc(), then __group_cpus_evenly() will deference the
> ZERO_SIZE_PTR.
>
> Fix the problem by checking numgrps first in group_cpus_evenly(), and
> return NULL directly if numgrps is zero.
>
> Fixes: 6a6dcae8f486 ("blk-mq: Build default queue map via group_cpus_evenly()")
> Signed-off-by: Yu Kuai <yukuai3@huawei.com>
> ---
> Changes in v2:
>  - check numgrps directly.
>
>  lib/group_cpus.c | 3 +++
>  1 file changed, 3 insertions(+)
>
> diff --git a/lib/group_cpus.c b/lib/group_cpus.c
> index ee272c4cefcc..d68407e3601e 100644
> --- a/lib/group_cpus.c
> +++ b/lib/group_cpus.c
> @@ -352,6 +352,9 @@ struct cpumask *group_cpus_evenly(unsigned int numgrps)
>         int ret = -ENOMEM;
>         struct cpumask *masks = NULL;
>
> +       if (numgrps == 0)
> +               return NULL;
> +

Reviewed-by: Ming Lei <ming.lei@redhat.com>

Thanks,