From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.216.43 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796047392; b=GoNj7xBrdIziIquZbC1tNY5RZ3DKqRVw2ZI/YqB0/wCImjhlBElTvs1X6BJsXjnhC2W+ IkTuhsf3Dt58LV7dIxh+5E7Toky40h2nixYyGGpSSlEH+0uS5V0NOszugJ0Fxjq2iJyYI b58ZmvSIl0i7GklGijop6YM3GHw5arjIevfXclMKopRfGtslJuZcXBV88SswI1atCw619 qJM9p2HOkxXKQDHqyNYEHJ1TpefNlsCVwwSPU+EWpPgeQyfpOBXAee6XYyT5GRR1Q5cRK u8DgdF4jbTt4FFeIerAy3tegbJmxDGvuEBQs73/QNcvPgtwQff7P0nnLpRrldLZiqxA== 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=oDGwENjkwKIGknuxiq2Njk9nF9m0PoQ3AP+82cnKuAWKpinelLHahRiDDsWG6rfZywca hMrnYivlvnPzyKQGU8/C940OsFpfDoc6Jx3k5KUoqY0nbr+pmNTuYsZkuD7jL4UvnLaUv Yv9bjX+tMe2h6/VdlrjQgGcdukU/QuUCKS+TZE9ew5mDta3hrQNFpmvXcojoLRYrUKTxV GN7teAxIp0nEeufu5XkLLysnmiwjj506mECJt8dvJFCvqdjmNqRSzCuxgFNF/5lD9MKKg YVH9hhpUAjF08GKc9ToZ6TPT6XGMnl6EKMTpyWTmOEen7fCxM2HXVjDAZdRiOYaMiCQ== 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.43 Received: from mail-pj1-f43.google.com (mail-pj1-f43.google.com [209.85.216.43]) (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 D6FEA606A325 for ; Mon, 30 Nov 2026 14:03:11 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org D6FEA606A325 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-f43.google.com with SMTP id 98e67ed59e1d1-3b402824d07so6719133a91.1 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=cQwS2sByLP1UFRXP3nROKS4gUp6PacNLOcI+O9l4ADl763WCiaOwmSQTq7YXJZOGDH ixUBiTjv/0qpX1IjMQ0qw4g9s1xIySPLCEpQbECXd/vW4dKchV9xLansJWhaZ6n5qI1z zgMg9WV7738RMKe9y8ugzmSP5EBKZ341srFHheclJNAMBr0Wdl2I7IASAQPaMLC3nzPd Lt55Gl93aQ/qb64KIGvLttpuRCZJnz6xbpHlXCPYLg3UJYWGkPxN+8/wOmpdOhejcNo6 /gAoQUsvG6QqSJk91ErVTD6DYqj433JDLWbqLPPyVDmVufd3yjfWuMyPYT9P3V4+KnG5 r7iA== X-Forwarded-Encrypted: i=1; AL+hErHeXErVoK3XDgCda08j4EsHoT+zSUbaWqdXS+tXrbscnLM+vx0OAhDTZbM0hSRhT/4cIloRGFcHfqXZdN8=@vger.kernel.org X-Gm-Message-State: APuU4Z+aQmJQaeGxRWvjiAupA2bcWzDtGKSz/NgidQfXi1gi1viaYlFn jNXt18T2iY2tJpY6np6ZM3dsFnFuaP2SAf5Gi6tOlv/7qRdDmL7MEYz2BiA3uw== X-Gm-Gg: AZFdTZePhtUjnsSM1W+Q64/yvMD69Nl1vRVqQ7f71ZzAd2Lq+ft558dklqgovBEWN1O mW1OIwG/Zd8uFpct1+EQs8sR/ypq9tKAJp+6oy4j6yQiwQ8HNEPcZ77Jlitfr0wHwPuuD6Zd1x0 KzDedkwq0voLQlJkwYs1x0RXaiI0B5cUS+sUx8BKpirDU7LsS1IrNLJtdPTw863qv8//fiPIFMY vh5VCHUiQtXi1rBm6sk3UEkSf021R/4EJL6ZZXWXp7TelIpRFiYCs3qENXOANEUMYsPpiMc9MQT znzxJE+Wj0+tHUNW+0ckBLseH9+AdfEHgWTHvy6VPSiKZtCDb+g0iuS41mc/YWjdLscmD7OqXyR v7BooC2ZejpazDaSj97XnBExIhOS8fPEkS//LahEefqf3YA7GLtwLWxzGkNPcRJhLvs3jtgzlk3 BMjZyZW1b2U9vRay4wTM5lc7+WmBxKx1sScCZXIfbnu2UvXJsNb1Moo7Z0xJnkdDNJ3mTfzEwDF RgY3F8SpbGMbJwdZAfEjCXeNl9pJeIhKHn3/HxrehTkm+wZzajFmU+ppAtiBg0sWXOoJW1vBysH 183jWPgFL9zMHSFRWr1TLQjw7IP1LrKVJ16wt+HnLz1sQZ0UPg== 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: linux-kernel@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