Skip to content

Commit 98c598a

Browse files
Li Nanaxboe
authored andcommitted
nbd: pass nbd_sock to nbd_read_reply() instead of index
If a socket is processing ioctl 'NBD_SET_SOCK', config->socks might be krealloc in nbd_add_socket(), and a garbage request is received now, a UAF may occurs. T1 nbd_ioctl __nbd_ioctl nbd_add_socket blk_mq_freeze_queue T2 recv_work nbd_read_reply sock_xmit krealloc config->socks def config->socks Pass nbd_sock to nbd_read_reply(). And introduce a new function sock_xmit_recv(), which differs from sock_xmit only in the way it get socket. ================================================================== BUG: KASAN: use-after-free in sock_xmit+0x525/0x550 Read of size 8 at addr ffff8880188ec428 by task kworker/u12:1/18779 Workqueue: knbd4-recv recv_work Call Trace: __dump_stack dump_stack+0xbe/0xfd print_address_description.constprop.0+0x19/0x170 __kasan_report.cold+0x6c/0x84 kasan_report+0x3a/0x50 sock_xmit+0x525/0x550 nbd_read_reply+0xfe/0x2c0 recv_work+0x1c2/0x750 process_one_work+0x6b6/0xf10 worker_thread+0xdd/0xd80 kthread+0x30a/0x410 ret_from_fork+0x22/0x30 Allocated by task 18784: kasan_save_stack+0x1b/0x40 kasan_set_track set_alloc_info __kasan_kmalloc __kasan_kmalloc.constprop.0+0xf0/0x130 slab_post_alloc_hook slab_alloc_node slab_alloc __kmalloc_track_caller+0x157/0x550 __do_krealloc krealloc+0x37/0xb0 nbd_add_socket +0x2d3/0x880 __nbd_ioctl nbd_ioctl+0x584/0x8e0 __blkdev_driver_ioctl blkdev_ioctl+0x2a0/0x6e0 block_ioctl+0xee/0x130 vfs_ioctl __do_sys_ioctl __se_sys_ioctl+0x138/0x190 do_syscall_64+0x33/0x40 entry_SYSCALL_64_after_hwframe+0x61/0xc6 Freed by task 18784: kasan_save_stack+0x1b/0x40 kasan_set_track+0x1c/0x30 kasan_set_free_info+0x20/0x40 __kasan_slab_free.part.0+0x13f/0x1b0 slab_free_hook slab_free_freelist_hook slab_free kfree+0xcb/0x6c0 krealloc+0x56/0xb0 nbd_add_socket+0x2d3/0x880 __nbd_ioctl nbd_ioctl+0x584/0x8e0 __blkdev_driver_ioctl blkdev_ioctl+0x2a0/0x6e0 block_ioctl+0xee/0x130 vfs_ioctl __do_sys_ioctl __se_sys_ioctl+0x138/0x190 do_syscall_64+0x33/0x40 entry_SYSCALL_64_after_hwframe+0x61/0xc6 Signed-off-by: Li Nan <[email protected]> Reviewed-by: Yu Kuai <[email protected]> Reviewed-by: Ming Lei <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent db46cd1 commit 98c598a

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

drivers/block/nbd.c

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ struct nbd_sock {
6767
struct recv_thread_args {
6868
struct work_struct work;
6969
struct nbd_device *nbd;
70+
struct nbd_sock *nsock;
7071
int index;
7172
};
7273

@@ -505,15 +506,9 @@ static enum blk_eh_timer_return nbd_xmit_timeout(struct request *req)
505506
return BLK_EH_DONE;
506507
}
507508

508-
/*
509-
* Send or receive packet. Return a positive value on success and
510-
* negtive value on failue, and never return 0.
511-
*/
512-
static int sock_xmit(struct nbd_device *nbd, int index, int send,
513-
struct iov_iter *iter, int msg_flags, int *sent)
509+
static int __sock_xmit(struct nbd_device *nbd, struct socket *sock, int send,
510+
struct iov_iter *iter, int msg_flags, int *sent)
514511
{
515-
struct nbd_config *config = nbd->config;
516-
struct socket *sock = config->socks[index]->sock;
517512
int result;
518513
struct msghdr msg;
519514
unsigned int noreclaim_flag;
@@ -556,6 +551,19 @@ static int sock_xmit(struct nbd_device *nbd, int index, int send,
556551
return result;
557552
}
558553

554+
/*
555+
* Send or receive packet. Return a positive value on success and
556+
* negtive value on failure, and never return 0.
557+
*/
558+
static int sock_xmit(struct nbd_device *nbd, int index, int send,
559+
struct iov_iter *iter, int msg_flags, int *sent)
560+
{
561+
struct nbd_config *config = nbd->config;
562+
struct socket *sock = config->socks[index]->sock;
563+
564+
return __sock_xmit(nbd, sock, send, iter, msg_flags, sent);
565+
}
566+
559567
/*
560568
* Different settings for sk->sk_sndtimeo can result in different return values
561569
* if there is a signal pending when we enter sendmsg, because reasons?
@@ -712,7 +720,7 @@ static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, int index)
712720
return 0;
713721
}
714722

715-
static int nbd_read_reply(struct nbd_device *nbd, int index,
723+
static int nbd_read_reply(struct nbd_device *nbd, struct socket *sock,
716724
struct nbd_reply *reply)
717725
{
718726
struct kvec iov = {.iov_base = reply, .iov_len = sizeof(*reply)};
@@ -721,7 +729,7 @@ static int nbd_read_reply(struct nbd_device *nbd, int index,
721729

722730
reply->magic = 0;
723731
iov_iter_kvec(&to, ITER_DEST, &iov, 1, sizeof(*reply));
724-
result = sock_xmit(nbd, index, 0, &to, MSG_WAITALL, NULL);
732+
result = __sock_xmit(nbd, sock, 0, &to, MSG_WAITALL, NULL);
725733
if (result < 0) {
726734
if (!nbd_disconnected(nbd->config))
727735
dev_err(disk_to_dev(nbd->disk),
@@ -845,14 +853,14 @@ static void recv_work(struct work_struct *work)
845853
struct nbd_device *nbd = args->nbd;
846854
struct nbd_config *config = nbd->config;
847855
struct request_queue *q = nbd->disk->queue;
848-
struct nbd_sock *nsock;
856+
struct nbd_sock *nsock = args->nsock;
849857
struct nbd_cmd *cmd;
850858
struct request *rq;
851859

852860
while (1) {
853861
struct nbd_reply reply;
854862

855-
if (nbd_read_reply(nbd, args->index, &reply))
863+
if (nbd_read_reply(nbd, nsock->sock, &reply))
856864
break;
857865

858866
/*
@@ -887,7 +895,6 @@ static void recv_work(struct work_struct *work)
887895
percpu_ref_put(&q->q_usage_counter);
888896
}
889897

890-
nsock = config->socks[args->index];
891898
mutex_lock(&nsock->tx_lock);
892899
nbd_mark_nsock_dead(nbd, nsock, 1);
893900
mutex_unlock(&nsock->tx_lock);
@@ -1231,6 +1238,7 @@ static int nbd_reconnect_socket(struct nbd_device *nbd, unsigned long arg)
12311238
INIT_WORK(&args->work, recv_work);
12321239
args->index = i;
12331240
args->nbd = nbd;
1241+
args->nsock = nsock;
12341242
nsock->cookie++;
12351243
mutex_unlock(&nsock->tx_lock);
12361244
sockfd_put(old);
@@ -1413,6 +1421,7 @@ static int nbd_start_device(struct nbd_device *nbd)
14131421
refcount_inc(&nbd->config_refs);
14141422
INIT_WORK(&args->work, recv_work);
14151423
args->nbd = nbd;
1424+
args->nsock = config->socks[i];
14161425
args->index = i;
14171426
queue_work(nbd->recv_workq, &args->work);
14181427
}

0 commit comments

Comments
 (0)