nbd_genl_reconnect_sock_cb() replaces nsock->sock and drops the old socket's last file reference while recv_work() can be blocked in recvmsg() on that socket. On AF_UNIX, KASAN reports a slab use-after-free in unix_stream_read_generic() when the worker resumes. Pin the socket for the worker's lifetime and use it for both the reply header and payload. Save the socket generation at worker startup and reject replies for commands sent in another generation. This prevents a buffered old-generation reply from completing a new command if its index and wire handle happen to match. Claim NBD_CMD_INFLIGHT under cmd->lock in nbd_handle_reply(). Otherwise a timeout can requeue and resend the command between reply validation and recv_work() clearing the bit. At worker exit, mark the connection dead only if its generation still matches. Fixes: b7aa3d39385d ("nbd: add a reconfigure netlink command") Co-developed-by: Gwanhyun Lee Signed-off-by: Gwanhyun Lee Signed-off-by: Junyoung Jang Cc: stable@vger.kernel.org --- Disclosure: An in-house LLM-based analysis tool developed by the authors assisted in surfacing this bug candidate and drafting the initial notes. All root-cause analysis, patch code, and QEMU/KVM reproducer results were manually reviewed and independently verified by the authors. Tested with NBD and KASAN enabled on mainline 7.4-rc5 and 6.12.118. In a one-vCPU QEMU guest with a SCHED_FIFO reconnect caller, clean mainline reported this after replacing the AF_UNIX socket: BUG: KASAN: slab-use-after-free in _raw_spin_lock Workqueue: nbd0-recv recv_work unix_stream_read_generic unix_stream_recvmsg __sock_xmit recv_work With the patch applied, the reproducer no longer triggers KASAN or lockdep warnings on either 7.4-rc5 or 6.12.118, and subsequent reads over the reconnected socket complete normally. drivers/block/nbd.c | 44 +++++++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c index b2b27f90a7ed..49efee6907df 100644 --- a/drivers/block/nbd.c +++ b/drivers/block/nbd.c @@ -69,7 +69,9 @@ struct recv_thread_args { struct work_struct work; struct nbd_device *nbd; struct nbd_sock *nsock; + struct socket *sock; int index; + int cookie; }; struct link_dead_args { @@ -879,7 +881,8 @@ static int nbd_read_reply(struct nbd_device *nbd, struct socket *sock, /* NULL returned = something went wrong, inform userspace */ static struct nbd_cmd *nbd_handle_reply(struct nbd_device *nbd, int index, - struct nbd_reply *reply) + int cookie, struct socket *sock, + struct nbd_reply *reply, bool *complete) { int result; struct nbd_cmd *cmd; @@ -916,6 +919,13 @@ static struct nbd_cmd *nbd_handle_reply(struct nbd_device *nbd, int index, ret = -ENOENT; goto out; } + if (cmd->cookie != cookie) { + dev_err(disk_to_dev(nbd->disk), + "Reply for req %p from socket generation %d (expected %d)\n", + req, cookie, cmd->cookie); + ret = -ENOENT; + goto out; + } if (cmd->cmd_cookie != nbd_handle_to_cookie(handle)) { dev_err(disk_to_dev(nbd->disk), "Double reply on req %p, cmd_cookie %u, handle cookie %u\n", req, cmd->cmd_cookie, nbd_handle_to_cookie(handle)); @@ -949,7 +959,7 @@ static struct nbd_cmd *nbd_handle_reply(struct nbd_device *nbd, int index, rq_for_each_segment(bvec, req, iter) { iov_iter_bvec(&to, ITER_DEST, &bvec, 1, bvec.bv_len); - result = sock_xmit(nbd, index, 0, &to, MSG_WAITALL, NULL); + result = __sock_xmit(nbd, sock, 0, &to, MSG_WAITALL, NULL); if (result < 0) { dev_err(disk_to_dev(nbd->disk), "Receive data failed (result %d)\n", result); @@ -972,6 +982,8 @@ static struct nbd_cmd *nbd_handle_reply(struct nbd_device *nbd, int index, } out: trace_nbd_payload_received(req, handle); + if (!ret && likely(!blk_should_fake_timeout(req->q))) + *complete = __test_and_clear_bit(NBD_CMD_INFLIGHT, &cmd->flags); mutex_unlock(&cmd->lock); return ret ? ERR_PTR(ret) : cmd; } @@ -990,8 +1002,9 @@ static void recv_work(struct work_struct *work) while (1) { struct nbd_reply reply; + bool complete = false; - if (nbd_read_reply(nbd, nsock->sock, &reply)) + if (nbd_read_reply(nbd, args->sock, &reply)) break; /* @@ -1006,30 +1019,25 @@ static void recv_work(struct work_struct *work) break; } - cmd = nbd_handle_reply(nbd, args->index, &reply); + cmd = nbd_handle_reply(nbd, args->index, args->cookie, + args->sock, &reply, &complete); if (IS_ERR(cmd)) { percpu_ref_put(&q->q_usage_counter); break; } rq = blk_mq_rq_from_pdu(cmd); - if (likely(!blk_should_fake_timeout(rq->q))) { - bool complete; - - mutex_lock(&cmd->lock); - complete = __test_and_clear_bit(NBD_CMD_INFLIGHT, - &cmd->flags); - mutex_unlock(&cmd->lock); - if (complete) - blk_mq_complete_request(rq); - } + if (complete) + blk_mq_complete_request(rq); percpu_ref_put(&q->q_usage_counter); } mutex_lock(&nsock->tx_lock); - nbd_mark_nsock_dead(nbd, nsock, 1); + if (nsock->cookie == args->cookie) + nbd_mark_nsock_dead(nbd, nsock, 1); mutex_unlock(&nsock->tx_lock); + sockfd_put(args->sock); atomic_dec(&config->recv_threads); wake_up(&config->recv_wq); nbd_config_put(nbd); @@ -1389,6 +1397,9 @@ static int nbd_genl_reconnect_sock_cb(struct nbd_device *nbd, unsigned long arg) args->nbd = nbd; args->nsock = nsock; nsock->cookie++; + args->sock = sock; + get_file(sock->file); + args->cookie = nsock->cookie; mutex_unlock(&nsock->tx_lock); sockfd_put(old); @@ -1576,7 +1587,10 @@ retry: INIT_WORK(&args->work, recv_work); args->nbd = nbd; args->nsock = config->socks[i]; + args->sock = config->socks[i]->sock; + get_file(args->sock->file); args->index = i; + args->cookie = config->socks[i]->cookie; queue_work(nbd->recv_workq, &args->work); } return nbd_set_size(nbd, config->bytesize, nbd_blksize(config)); base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244 -- 2.43.0