Skip to content

Commit c55b2b9

Browse files
YuKuai-huaweiaxboe
authored andcommitted
nbd: fix race between nbd_alloc_config() and module removal
When nbd module is being removing, nbd_alloc_config() may be called concurrently by nbd_genl_connect(), although try_module_get() will return false, but nbd_alloc_config() doesn't handle it. The race may lead to the leak of nbd_config and its related resources (e.g, recv_workq) and oops in nbd_read_stat() due to the unload of nbd module as shown below: BUG: kernel NULL pointer dereference, address: 0000000000000040 Oops: 0000 [#1] SMP PTI CPU: 5 PID: 13840 Comm: kworker/u17:33 Not tainted 5.14.0+ #1 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996) Workqueue: knbd16-recv recv_work [nbd] RIP: 0010:nbd_read_stat.cold+0x130/0x1a4 [nbd] Call Trace: recv_work+0x3b/0xb0 [nbd] process_one_work+0x1ed/0x390 worker_thread+0x4a/0x3d0 kthread+0x12a/0x150 ret_from_fork+0x22/0x30 Fixing it by checking the return value of try_module_get() in nbd_alloc_config(). As nbd_alloc_config() may return ERR_PTR(-ENODEV), assign nbd->config only when nbd_alloc_config() succeeds to ensure the value of nbd->config is binary (valid or NULL). Also adding a debug message to check the reference counter of nbd_config during module removal. Signed-off-by: Hou Tao <houtao1@huawei.com> Signed-off-by: Yu Kuai <yukuai3@huawei.com> Reviewed-by: Josef Bacik <josef@toxicpanda.com> Link: https://lore.kernel.org/r/20220521073749.3146892-3-yukuai3@huawei.com Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 06c4da8 commit c55b2b9

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

drivers/block/nbd.c

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,15 +1518,20 @@ static struct nbd_config *nbd_alloc_config(void)
15181518
{
15191519
struct nbd_config *config;
15201520

1521+
if (!try_module_get(THIS_MODULE))
1522+
return ERR_PTR(-ENODEV);
1523+
15211524
config = kzalloc(sizeof(struct nbd_config), GFP_NOFS);
1522-
if (!config)
1523-
return NULL;
1525+
if (!config) {
1526+
module_put(THIS_MODULE);
1527+
return ERR_PTR(-ENOMEM);
1528+
}
1529+
15241530
atomic_set(&config->recv_threads, 0);
15251531
init_waitqueue_head(&config->recv_wq);
15261532
init_waitqueue_head(&config->conn_wait);
15271533
config->blksize_bits = NBD_DEF_BLKSIZE_BITS;
15281534
atomic_set(&config->live_connections, 0);
1529-
try_module_get(THIS_MODULE);
15301535
return config;
15311536
}
15321537

@@ -1553,12 +1558,13 @@ static int nbd_open(struct block_device *bdev, fmode_t mode)
15531558
mutex_unlock(&nbd->config_lock);
15541559
goto out;
15551560
}
1556-
config = nbd->config = nbd_alloc_config();
1557-
if (!config) {
1558-
ret = -ENOMEM;
1561+
config = nbd_alloc_config();
1562+
if (IS_ERR(config)) {
1563+
ret = PTR_ERR(config);
15591564
mutex_unlock(&nbd->config_lock);
15601565
goto out;
15611566
}
1567+
nbd->config = config;
15621568
refcount_set(&nbd->config_refs, 1);
15631569
refcount_inc(&nbd->refs);
15641570
mutex_unlock(&nbd->config_lock);
@@ -1964,13 +1970,14 @@ static int nbd_genl_connect(struct sk_buff *skb, struct genl_info *info)
19641970
nbd_put(nbd);
19651971
return -EINVAL;
19661972
}
1967-
config = nbd->config = nbd_alloc_config();
1968-
if (!nbd->config) {
1973+
config = nbd_alloc_config();
1974+
if (IS_ERR(config)) {
19691975
mutex_unlock(&nbd->config_lock);
19701976
nbd_put(nbd);
19711977
printk(KERN_ERR "nbd: couldn't allocate config\n");
1972-
return -ENOMEM;
1978+
return PTR_ERR(config);
19731979
}
1980+
nbd->config = config;
19741981
refcount_set(&nbd->config_refs, 1);
19751982
set_bit(NBD_RT_BOUND, &config->runtime_flags);
19761983

@@ -2543,6 +2550,9 @@ static void __exit nbd_cleanup(void)
25432550
while (!list_empty(&del_list)) {
25442551
nbd = list_first_entry(&del_list, struct nbd_device, list);
25452552
list_del_init(&nbd->list);
2553+
if (refcount_read(&nbd->config_refs))
2554+
printk(KERN_ERR "nbd: possibly leaking nbd_config (ref %d)\n",
2555+
refcount_read(&nbd->config_refs));
25462556
if (refcount_read(&nbd->refs) != 1)
25472557
printk(KERN_ERR "nbd: possibly leaking a device\n");
25482558
nbd_put(nbd);

0 commit comments

Comments
 (0)