From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.216.47 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796047392; b=pawzd7YGhhTqcbsMe5cOeC8fo1DdMDwqeGk1FzCVlPbNsLpH7Tr/WH9U+UL2Je2A/m71 jfsEk77Hh4Y1NnA4p8VH9VHsE2OcUfpE4u82VRXImbN8xdCuucxHO5+x17DB4JyAPqQ/J i5aNhO6q6kVK3qG0NR8u8afnJRrVGJOSlIClhQTB0O2wVNcRlFd/TcX7NVhtiBgsLbGcy pwELnDVTJNmU2gfHdpTmulKmAiExHOb3Fua+0J0IhleTNfFeZgf55p2/NUJ211pp4uT7R Rll1TojoN+1CgUE5dy0kqo31g6pSvCUv+ZQUO6ygPneQCPhNXfyQjIJK5zKB/y4o04g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796047392; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=MHFMJdZFFVTIDbgrK0dzlVJkVPe9jEfXpI8/wbJkFzo=; b=gEo5IsSm4auciVARZQOezla2BJxeIwUPTH6nPnwJfsHgrsdr46atn6GhZp1MaWD6y04a Vt+00yHxkwwQt9Y9EoeMdl5ftPj57x0imbrqqAq1YlX6dy1dmHTpEgoOuhk/bMeiPIFZN Nre1y22r4nPQ/KwiMt1J9hLozCI+no/ue+lgIMRBgMcTkfgyLWUpntE//fIH6/lU0lb/X Esl5+5OvUmMSbB85KM2mtEKqxEBA345IqwR3K1hdm5UCnYUaU+eODPsiKEXUbk5iyzvRB IWfVBWaIClhA9jNbh05il/hqIzUdq6M/VXZ+0dmoXj6nIQfauFY7VKV+S4fzswVUjew== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=cJr+x86x; arc=none smtp.remote-ip=209.85.216.47 Received: from mail-pj1-f47.google.com (mail-pj1-f47.google.com [209.85.216.47]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D8C97606A327 for ; Mon, 30 Nov 2026 14:03:11 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org D8C97606A327 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=cJr+x86x Received: by mail-pj1-f47.google.com with SMTP id 98e67ed59e1d1-3b4021b572dso6432532a91.2 for ; Mon, 30 Nov 2026 06:03:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796047391; x=1796652191; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=MHFMJdZFFVTIDbgrK0dzlVJkVPe9jEfXpI8/wbJkFzo=; b=cJr+x86x/wRK9y/V6wSdwSx6Imc4G/KKX8PEBA8m9nPK7vOQZ2tP09xpv0k+8Nw71M s2kBc3COjzSr/q4JSC3JUdSDicuim3Hem+ezlw87boZMtztu8hWyQDNuSyuFUsXAFdut NRXzbsJeRylXJC6bTmpAiz/pg+KDUCwP/5uB/Fgvy6GwVpc9u9XZAZP2f/1dquBPoQuJ hUjq9bzQ7lmPFAfpWigB4G3oojbl+liI9Sgzo+nPoWaxPQwgFR+rEyUjdiw7Jc7N0VMA u/onHFicC0YeUzto7dCsA96QfILB5BufmMNJONoROHgtBsBrEz7kHsvWUR4GqMsh00BM aOLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796047391; x=1796652191; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=MHFMJdZFFVTIDbgrK0dzlVJkVPe9jEfXpI8/wbJkFzo=; b=iACB19zd0Hp0SJ/ZjzpJH4aOnYY87xejy+JuEQye7ucyjzOCpjtH3jD2sJxXH9GnEg CW28CjOAu+0PA+AHQlJtURwUp4kaqHKbVn5Bts8pzEUPLzp/Toh750nvlhTrYhIAlEQo hcYmAQZaZkDzpmv0PavFyM5j2c0Kyd4615STcL70Zgx3v7dYr8PjIQrbwfKlbrspwLIZ DVxJ/mF3vxPuuqBuQIg2/x9YJSKEgZnqIxwWCvecmrWBu8yPeTeBJU6aBeZPXUv65qQr mX8/OmB8UffCzMOziYY9zRDzF361GWCS0QqDDiwvBm9OJ/tzY+vRbmPZyYY5acX9Yjh4 ri1w== X-Forwarded-Encrypted: i=1; AL+hErEpNOhidQ/elsfUnjpnmTHLKq6q0AQKkWMDOY1nFC1KbziyEy1VIGA9nSlu3Z7Md9Lw4j8AfNM=@vger.kernel.org X-Gm-Message-State: APuU4Z/PmVu7o0ZdkngUbNkzMxS7l1gJGlx5yW3nJ7W/HwxRSjCgBm4s SX1sww+qQzTV8IcM6zfgI702P8zVGIOxlfusGyrM/1glgYqo4FYkSN1f X-Gm-Gg: AZFdTZdBU+iFBTom1EohnXZV/6/vPXWTo4zt1K3QgeXUytsNzJAMXUBP8xpgAekPTIk krrm3/N+mTIC8H83yzM9hm/YM6HAbbsgRLGGqtP13pL0dldMbnldk65DVkWL2q9CHuqCEZk3GnR nE9uY3m7Tr+e+m49UI9U9u8AHG5cVt6NZzkiLDuVNHtiBIQ0zy7WhFhWUmCWOuya26n3skEBP2j WmT9YmWyeVs+EOM04MuCxNqpSyCrq35PxEfbrbqQm+CIadUteq+RyY6+DwnHh+3MgbmUKA6VBt0 J0Eog5G159PfB/lDNKL/J1xWTNrBI0W3tMGuxvfq4eRQux2Vb7H4qDvImjvhuNfJ/koFT6QI5XC 8y4S0ZKXa/HFB37Xw9SyNFIMyhSJi5sTMq47wrpIcDOYGKAIL+hjWUmY287HS6zDQdnwVRkXLvL UY4/GR03hBequtdM/PdRqSCRQVojaPRkQRrSBEJHGOLoilbW8ogOCicUMKRsqaNFl5FSVZOz0lP CjhyFkbYRjIyFJcIXc3JT9wpX6t7mqow4qjEmchfe+YLhpCxstd8Jqi+pHC1UsC78hnJqR8E7Gr zeGYy0nWOnNYU8AzlaCbn5idi0tqtv1crc64zjsDSs9Nz5p2gw== X-Received: by 2002:a17:90b:2c87:b0:3b0:9e42:b67c with SMTP id 98e67ed59e1d1-3b41c96ed40mr32697352a91.61.1796047390762; Mon, 30 Nov 2026 06:03:10 -0800 (PST) Received: from localhost.localdomain ([112.170.44.210]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3b44b7c6513sm26230278a91.4.2026.11.30.06.03.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 06:03:09 -0800 (PST) From: Junyoung Jang To: Josef Bacik , Jens Axboe Cc: Junyoung Jang , Gwanhyun Lee , linux-block@vger.kernel.org, nbd@other.debian.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: [PATCH] nbd: pin the socket used by each receive worker Date: Mon, 30 Nov 2026 23:03:02 +0900 Message-ID: <20261130140304.3662756-1-graypanda.inzag@gmail.com> X-Mailer: git-send-email 2.43.0 X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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