提交 dd60e024 编写于 作者: Y Ye Bin 提交者: Yang Yingliang

nbd: Fix use-after-free in pid_show

mainline inclusion
from mainline-v5.16
commit 0c98057b
category: bugfix
bugzilla: 182939
CVE: NA

-----------------------------------------------

I got issue as follows:
[  263.886511] BUG: KASAN: use-after-free in pid_show+0x11f/0x13f
[  263.888359] Read of size 4 at addr ffff8880bf0648c0 by task cat/746
[  263.890479] CPU: 0 PID: 746 Comm: cat Not tainted 4.19.90-dirty #140
[  263.893162] Call Trace:
[  263.893509]  dump_stack+0x108/0x15f
[  263.893999]  print_address_description+0xa5/0x372
[  263.894641]  kasan_report.cold+0x236/0x2a8
[  263.895696]  __asan_report_load4_noabort+0x25/0x30
[  263.896365]  pid_show+0x11f/0x13f
[  263.897422]  dev_attr_show+0x48/0x90
[  263.898361]  sysfs_kf_seq_show+0x24d/0x4b0
[  263.899479]  kernfs_seq_show+0x14e/0x1b0
[  263.900029]  seq_read+0x43f/0x1150
[  263.900499]  kernfs_fop_read+0xc7/0x5a0
[  263.903764]  vfs_read+0x113/0x350
[  263.904231]  ksys_read+0x103/0x270
[  263.905230]  __x64_sys_read+0x77/0xc0
[  263.906284]  do_syscall_64+0x106/0x360
[  263.906797]  entry_SYSCALL_64_after_hwframe+0x44/0xa9

Reproduce this issue as follows:
1. nbd-server 8000 /tmp/disk
2. nbd-client localhost 8000 /dev/nbd1
3. cat /sys/block/nbd1/pid
Then trigger use-after-free in pid_show.

Reason is after do step '2', nbd-client progress is already exit. So
it's task_struct already freed.
To solve this issue, revert part of 6521d39a's modify and remove
useless 'recv_task' member of nbd_device.

Fixes: 6521d39a ("nbd: Remove variable 'pid'")
Signed-off-by: NYe Bin <yebin10@huawei.com>
Reviewed-by: NJosef Bacik <josef@toxicpanda.com>
Link: https://lore.kernel.org/r/20211020073959.2679255-1-yebin10@huawei.comSigned-off-by: NJens Axboe <axboe@kernel.dk>

conflicts:
drivers/block/nbd.c
Signed-off-by: NYe Bin <yebin10@huawei.com>
Reviewed-by: NJason Yan <yanaijie@huawei.com>
Signed-off-by: NYang Yingliang <yangyingliang@huawei.com>
上级 a8322e38
...@@ -112,11 +112,11 @@ struct nbd_device { ...@@ -112,11 +112,11 @@ struct nbd_device {
struct workqueue_struct *recv_workq; struct workqueue_struct *recv_workq;
struct list_head list; struct list_head list;
struct task_struct *task_recv;
struct task_struct *task_setup; struct task_struct *task_setup;
struct completion *destroy_complete; struct completion *destroy_complete;
unsigned long flags; unsigned long flags;
pid_t pid; /* pid of nbd-client, if attached */
}; };
#define NBD_CMD_REQUEUED 1 #define NBD_CMD_REQUEUED 1
...@@ -211,7 +211,7 @@ static ssize_t pid_show(struct device *dev, ...@@ -211,7 +211,7 @@ static ssize_t pid_show(struct device *dev,
struct gendisk *disk = dev_to_disk(dev); struct gendisk *disk = dev_to_disk(dev);
struct nbd_device *nbd = (struct nbd_device *)disk->private_data; struct nbd_device *nbd = (struct nbd_device *)disk->private_data;
return sprintf(buf, "%d\n", task_pid_nr(nbd->task_recv)); return sprintf(buf, "%d\n", nbd->pid);
} }
static const struct device_attribute pid_attr = { static const struct device_attribute pid_attr = {
...@@ -329,7 +329,7 @@ static void nbd_size_set(struct nbd_device *nbd, loff_t blocksize, ...@@ -329,7 +329,7 @@ static void nbd_size_set(struct nbd_device *nbd, loff_t blocksize,
struct nbd_config *config = nbd->config; struct nbd_config *config = nbd->config;
config->blksize = blocksize; config->blksize = blocksize;
config->bytesize = blocksize * nr_blocks; config->bytesize = blocksize * nr_blocks;
if (nbd->task_recv != NULL) if (nbd->pid)
nbd_size_update(nbd, false); nbd_size_update(nbd, false);
} }
...@@ -1234,7 +1234,7 @@ static void nbd_config_put(struct nbd_device *nbd) ...@@ -1234,7 +1234,7 @@ static void nbd_config_put(struct nbd_device *nbd)
if (test_and_clear_bit(NBD_RT_HAS_PID_FILE, if (test_and_clear_bit(NBD_RT_HAS_PID_FILE,
&config->runtime_flags)) &config->runtime_flags))
device_remove_file(disk_to_dev(nbd->disk), &pid_attr); device_remove_file(disk_to_dev(nbd->disk), &pid_attr);
nbd->task_recv = NULL; nbd->pid = 0;
nbd_clear_sock(nbd); nbd_clear_sock(nbd);
if (config->num_connections) { if (config->num_connections) {
int i; int i;
...@@ -1269,7 +1269,7 @@ static int nbd_start_device(struct nbd_device *nbd) ...@@ -1269,7 +1269,7 @@ static int nbd_start_device(struct nbd_device *nbd)
int num_connections = config->num_connections; int num_connections = config->num_connections;
int error = 0, i; int error = 0, i;
if (nbd->task_recv) if (nbd->pid)
return -EBUSY; return -EBUSY;
if (!config->socks) if (!config->socks)
return -EINVAL; return -EINVAL;
...@@ -1288,7 +1288,7 @@ static int nbd_start_device(struct nbd_device *nbd) ...@@ -1288,7 +1288,7 @@ static int nbd_start_device(struct nbd_device *nbd)
} }
blk_mq_update_nr_hw_queues(&nbd->tag_set, config->num_connections); blk_mq_update_nr_hw_queues(&nbd->tag_set, config->num_connections);
nbd->task_recv = current; nbd->pid = task_pid_nr(current);
nbd_parse_flags(nbd); nbd_parse_flags(nbd);
...@@ -1556,8 +1556,8 @@ static int nbd_dbg_tasks_show(struct seq_file *s, void *unused) ...@@ -1556,8 +1556,8 @@ static int nbd_dbg_tasks_show(struct seq_file *s, void *unused)
{ {
struct nbd_device *nbd = s->private; struct nbd_device *nbd = s->private;
if (nbd->task_recv) if (nbd->pid)
seq_printf(s, "recv: %d\n", task_pid_nr(nbd->task_recv)); seq_printf(s, "recv: %d\n", nbd->pid);
return 0; return 0;
} }
...@@ -2116,7 +2116,7 @@ static int nbd_genl_reconfigure(struct sk_buff *skb, struct genl_info *info) ...@@ -2116,7 +2116,7 @@ static int nbd_genl_reconfigure(struct sk_buff *skb, struct genl_info *info)
mutex_lock(&nbd->config_lock); mutex_lock(&nbd->config_lock);
config = nbd->config; config = nbd->config;
if (!test_bit(NBD_RT_BOUND, &config->runtime_flags) || if (!test_bit(NBD_RT_BOUND, &config->runtime_flags) ||
!nbd->task_recv) { !nbd->pid) {
dev_err(nbd_to_dev(nbd), dev_err(nbd_to_dev(nbd),
"not configured, cannot reconfigure\n"); "not configured, cannot reconfigure\n");
ret = -EINVAL; ret = -EINVAL;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册