From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CDF9F606A332 for ; Sat, 5 Dec 2026 00:49:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=nseUvAS5; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=Af7X9P/h DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Unsubscribe-Post: List-Owner; bh=2+ySxORGrQBbS9x2GvE0GF5JB2RY+9LjhlCpJWoAJFQ=; b=nseUvAS5MEDXBd g0Ok8O7uhkf9QY8JOwORvr7S0kb2szJY+QrQkteIfH8IWGW7UuT0coCrg/+b8RoTNN4bNViONJLvU xRe4EjHufaXqWwfmn2U8KPw4IVtx3hdtVFPtKCUHywpeeWplaye86dEaY2JChieNG4PnD9mfMspSW ia6ewmU7WM706D7cNcsZkX8B5Z4IYyg2dvEHka9mG0b4JbkQQCl8YBavUgf2FCfSwcpHhkQLDH6+6 CzMvJtmvI2Yf96sP/LM0Twtv9LpIcgr8e46+xxQdzKqkDRlKA1mameT7aJ9yoeU3N9LVEBl06mLjn li/hoL/XAq5YZ+mHAQkg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xZdxZ-00000001orL-1XH7; Sat, 05 Dec 2026 00:49:05 +0000 Received: from mail-pj1-x1036.google.com ([2607:f8b0:4864:20::1036]) by bombadil.infradead.org with esmtps (Exim 4.100.1 #2 (Red Hat Linux)) id 1xZdxW-00000001oqr-1sOB for linux-nvme@lists.infradead.org; Sat, 05 Dec 2026 00:49:04 +0000 Received: by mail-pj1-x1036.google.com with SMTP id 98e67ed59e1d1-3b567ae3c66so792000a91.0 for ; Fri, 04 Dec 2026 16:49:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796431741; x=1797036541; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=2+ySxORGrQBbS9x2GvE0GF5JB2RY+9LjhlCpJWoAJFQ=; b=Af7X9P/hP5tEszaW1JUKUc6R8JTkzM9bxB/QY+qwkNNHZiEHTHQtgjAVdAAGXXIzdB F/PPoMX3pC+i4qLDdHcGuohWUpr49R4aQ+qnIpYcjqxKAjLeH5X9Snnir+JGCEI9UAZp 0uYv0bvkT4qECl+0L2UNZfhYUGaQyYE2LvJGhjFqm7dFsFryN1GQfHNiCZVt7/Injufl 5xsAAQXrkeW7GDSBCccJNWBr4AhjFmLfOy19KSMm61FesIySGOpxMyz1nIfRaRoHwr5t 8GtpGdzqE1GXcwBSzB47WJBXYbnB+lbJrYfAypAimDTGADlpWLhcPoNl+YWrJToJCBDf vLMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796431741; x=1797036541; h=content-transfer-encoding:mime-version:references:in-reply-to :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=2+ySxORGrQBbS9x2GvE0GF5JB2RY+9LjhlCpJWo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T55YvmsazsqrXnsP4wVuyY3R +mrP+K63JJYkmInMa8CC15jYIFZDxKcbUjgDP9dBSbA5jZfuR+/BKteFygynsWckU2l2+DBlW8e u+QHZBdVNmtWtLpI7EDZOs9GPc9lw0gCR3+z7k6HNh4KzU8J5uoCchVTaqcFAcHH/ X-Received: by 2002:a17:90b:554e:b0:3ac:b867:81df with SMTP id 98e67ed59e1d1-3b5e0697a1bmr2999945a91.39.1796431741262; Fri, 04 Dec 2026 16:49:01 -0800 (PST) Received: from localhost.localdomain ([42.118.185.60]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3b5c7d9c004sm11773643a91.17.2026.12.04.16.48.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Dec 2026 16:49:00 -0800 (PST) From: Lee Seong Hyeon To: linux-nvme@lists.infradead.org Cc: hare@kernel.org, hch@lst.de, sagi@grimberg.me, kch@nvidia.com, linux-kernel@vger.kernel.org Subject: [PATCH RESEND 1/2] nvmet-auth: serialize the controller authentication state Date: Sat, 5 Dec 2026 07:48:15 +0700 Message-ID: <20261205004816.2017-2-ihopenre@gmail.com> X-Mailer: git-send-email 2.55.0.windows.3 In-Reply-To: <20261205004816.2017-1-ihopenre@gmail.com> References: <20261205004816.2017-1-ihopenre@gmail.com> X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.9.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20261204_164902_490890_31C1685C X-CRM114-Status: GOOD ( 16.99 ) X-Spam-Score: -2.1 (--) X-BeenThere: linux-nvme@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "Linux-nvme" X-Spam-Status: No, score=-16.1 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 The DH-HMAC-CHAP keys, the hash and the DH group are kept in struct nvmet_ctrl and shared by all queues of a controller, while each queue processes its authentication commands from its own context. sq->auth_lock only serializes the commands of a single queue. An AUTH_Negotiate on the admin queue restarts the authentication and calls nvmet_setup_auth(), which frees ctrl->host_key and ctrl->ctrl_key and installs new ones. A DH-HMAC-CHAP_Reply processed on another queue of the same controller at that time still uses the old key: BUG: KASAN: slab-use-after-free in nvme_auth_transform_key+0x315/0x350 Workqueue: nvmet_tcp_wq nvmet_tcp_io_work Call Trace: nvme_auth_transform_key nvmet_auth_host_hash nvmet_execute_auth_send nvmet_tcp_io_work Freed by task 72: kfree nvmet_setup_auth nvmet_execute_auth_send nvmet_tcp_io_work The host does not have to be authenticated for this: a Connect for an I/O queue is accepted while the admin queue is still unauthenticated, and both queues take authentication commands. It only has to use the NQN of a host which has a DH-HMAC-CHAP key configured. Add a mutex to the controller and hold it for as long as an authentication command is processed. It nests inside sq->auth_lock. Take it around the initial nvmet_setup_auth() as well, since the controller is already on the list of its subsystem by then and an I/O queue can attach to it. The mutex is not placed under CONFIG_NVME_TARGET_AUTH so that nvmet_alloc_ctrl() needs no #ifdef. This was found by code review with the help of an AI coding assistant (Claude), which also drafted this patch. The use-after-free was reproduced under KASAN over nvmet-tcp on v7.4-rc3 and no longer shows with this patch applied. Fixes: db1312dd9548 ("nvmet: implement basic In-Band Authentication") Cc: stable@vger.kernel.org Reviewed-by: Hannes Reinecke Signed-off-by: Lee Seong Hyeon --- drivers/nvme/target/auth.c | 2 ++ drivers/nvme/target/core.c | 3 +++ drivers/nvme/target/fabrics-cmd-auth.c | 6 ++++++ drivers/nvme/target/nvmet.h | 2 ++ 4 files changed, 13 insertions(+) diff --git a/drivers/nvme/target/auth.c b/drivers/nvme/target/auth.c index a4a2a5cfd823..305b5e46f77f 100644 --- a/drivers/nvme/target/auth.c +++ b/drivers/nvme/target/auth.c @@ -152,6 +152,8 @@ u8 nvmet_setup_auth(struct nvmet_ctrl *ctrl, struct nvmet_sq *sq, bool reset) struct nvmet_host *host = NULL; struct nvme_dhchap_key *host_key, *ctrl_key = NULL; + lockdep_assert_held(&ctrl->auth_lock); + down_read(&nvmet_config_sem); if (nvmet_is_disc_subsys(ctrl->subsys)) goto out_unlock; diff --git a/drivers/nvme/target/core.c b/drivers/nvme/target/core.c index e31d65228c11..66d936c8cb12 100644 --- a/drivers/nvme/target/core.c +++ b/drivers/nvme/target/core.c @@ -1654,6 +1654,7 @@ struct nvmet_ctrl *nvmet_alloc_ctrl(struct nvmet_alloc_ctrl_args *args) if (!ctrl) goto out_put_subsystem; mutex_init(&ctrl->lock); + mutex_init(&ctrl->auth_lock); ctrl->port = args->port; ctrl->ops = args->ops; @@ -1740,7 +1741,9 @@ struct nvmet_ctrl *nvmet_alloc_ctrl(struct nvmet_alloc_ctrl_args *args) nvmet_start_keep_alive_timer(ctrl); + mutex_lock(&ctrl->auth_lock); dhchap_status = nvmet_setup_auth(ctrl, args->sq, false); + mutex_unlock(&ctrl->auth_lock); if (dhchap_status) { pr_err("Failed to setup authentication, dhchap status %u\n", dhchap_status); diff --git a/drivers/nvme/target/fabrics-cmd-auth.c b/drivers/nvme/target/fabrics-cmd-auth.c index 34c2e14d0bd9..fe068dd5d450 100644 --- a/drivers/nvme/target/fabrics-cmd-auth.c +++ b/drivers/nvme/target/fabrics-cmd-auth.c @@ -269,6 +269,7 @@ void nvmet_execute_auth_send(struct nvmet_req *req) u8 dhchap_status; mutex_lock(&req->sq->auth_lock); + mutex_lock(&ctrl->auth_lock); if (req->cmd->auth_send.secp != NVME_AUTH_DHCHAP_PROTOCOL_IDENTIFIER) { status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR; @@ -297,6 +298,7 @@ void nvmet_execute_auth_send(struct nvmet_req *req) } if (unlikely(tl != req->transfer_len)) { pr_debug("%s: transfer length mismatch (%u)\n", __func__, tl); + mutex_unlock(&ctrl->auth_lock); mutex_unlock(&req->sq->auth_lock); nvmet_check_transfer_len(req, tl); return; @@ -444,6 +446,7 @@ void nvmet_execute_auth_send(struct nvmet_req *req) nvmet_ctrl_fatal_error(ctrl); complete: + mutex_unlock(&ctrl->auth_lock); mutex_unlock(&req->sq->auth_lock); nvmet_req_complete(req, status); } @@ -564,6 +567,7 @@ void nvmet_execute_auth_receive(struct nvmet_req *req) u16 status = 0; mutex_lock(&req->sq->auth_lock); + mutex_lock(&ctrl->auth_lock); if (req->cmd->auth_receive.secp != NVME_AUTH_DHCHAP_PROTOCOL_IDENTIFIER) { status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR; @@ -592,6 +596,7 @@ void nvmet_execute_auth_receive(struct nvmet_req *req) } if (unlikely(al != req->transfer_len)) { pr_debug("%s: transfer length mismatch (%u)\n", __func__, al); + mutex_unlock(&ctrl->auth_lock); mutex_unlock(&req->sq->auth_lock); nvmet_check_transfer_len(req, al); return; @@ -653,6 +658,7 @@ void nvmet_execute_auth_receive(struct nvmet_req *req) nvmet_auth_sq_free(req->sq); nvmet_ctrl_fatal_error(ctrl); } + mutex_unlock(&ctrl->auth_lock); mutex_unlock(&req->sq->auth_lock); nvmet_req_complete(req, status); } diff --git a/drivers/nvme/target/nvmet.h b/drivers/nvme/target/nvmet.h index e33884a81efc..fbd28c7177ec 100644 --- a/drivers/nvme/target/nvmet.h +++ b/drivers/nvme/target/nvmet.h @@ -308,6 +308,8 @@ struct nvmet_ctrl { struct nvme_error_slot slots[NVMET_ERROR_LOG_SLOTS]; bool pi_support; bool concat; + /* serializes the authentication state of all queues */ + struct mutex auth_lock; #ifdef CONFIG_NVME_TARGET_AUTH struct nvme_dhchap_key *host_key; struct nvme_dhchap_key *ctrl_key; -- 2.53.0