From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.216.41 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796431742; b=3uNDqVSna3cIlbm6RPsXRbd3LFgaqAeBVgXj+ZdL6uOZhh3bMfASNfJuSUbQeoKP0MkB e2fRCZHJpa3wsBsAFagEfpTvYOX7L5QJt4T39KUBSpeFFy6fIE32mmbfvho9cz5byiD6c ujKUsjiJZs45EUXK/bcNnvK6djRirqN/uTOdDGjF29tCtLADmTmXXAUZVuEdmB3e0wAr4 k0hEh4A6rexhXzP2xFS9kbMosQ7kJBR2fgGYTDEXUO23m1NWBeWEYy6mA+7CJKANqaHL/ 7WSQenj28zYKoJ0TnkdkZyjLKY3k66O5nhXK4CJjk06dM61+lIXyAzuONkktO0XuJtA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796431742; 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:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=2+ySxORGrQBbS9x2GvE0GF5JB2RY+9LjhlCpJWoAJFQ=; b=t5/u+FKKLvoFR+pb988TPmRK/0I7kxyyMTS/1djxtnTfqj+ixRosQL50QT53TwqreSPO wyHEVN+UV9l/PlknWDbCK9CrP9AfgfruCVogk4tb4BQMe+JtTobawb1NQb3HiF0MPW0Fa x1Wvmt3aM2dpXxN4HRktz/J7VEIO+KJYE7a8Qd2nveRAcSJvxyz/gskb8r1TriQxDfUjY iVI8ZpzEHZ92ewwkbxZuqq0SsLy65g27gowEKXDJtxcuMfzsvClGe31+67huUNgYkwV3J trD1Xa6jxFbijiGfgXtauqkBedr8QxWcxaSgInesoZaX7O6EHJxYa9XRgliFnXieNrA== 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=Mf0saOPe; arc=none smtp.remote-ip=209.85.216.41 Received: from mail-pj1-f41.google.com (mail-pj1-f41.google.com [209.85.216.41]) (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 19977606A322 for ; Sat, 5 Dec 2026 00:49:01 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 19977606A322 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=Mf0saOPe Received: by mail-pj1-f41.google.com with SMTP id 98e67ed59e1d1-3b4cd193b50so875519a91.1 for ; Fri, 04 Dec 2026 16:49:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796431741; x=1797036541; darn=vger.kernel.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=Mf0saOPeZMosRc4i0NNlcbfb8g3XmOHSc9B+p694SMoOG/b4yArdxKut3BUdiAP8kP v2XBsyk12KIG1Jmfrobclo3RDS08CTf8gtNO/KFeQNz2VJ31iMLdnOPAXD2p5ULHd/xm sMKm9oCoOYfjL9pDZwRkW++AdUwDoGEKNFHSjYUilUi8/6qT06XI9vof0gPZql9YKryf ICuQaLPKrn/hidnLYmK2bBi67Jw7fPZ8XglC7tAobZdGASpD5Bwi2kZdInBasbh6YlvU oQHBSRgUUV9ZVDx4e5IFvHcnWR61LV/gB93MWwxrUdu2k5GYPx0tvZtDCLwZSJ0Exiuh fmbw== 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+9LjhlCpJWoAJFQ=; b=ujg66JNAqEEaF1mG09ddT+bBv2bSq9TnFxjpjUR4OclGBfgP8XbHDPnk5X1pQcsWGN Q2iZIfbkJWo6+a1zkOitr4AW9oxJNn+48SFKuIdFxjPE1IB0yujcQ8am5HRf5qUpIv+A gOBYIyviwKca2Ofco9JDThep1uw9gAH2jtKYb6IUlsiXnSnkzQgFKdLbgMH1b1nMXRTa kbbKjpgFdQzaGGLq0EBxc4j/pbn3RdHkGay0fu1h05HwjhHipHBCNMBRyYi9h5Ce3ZM0 Co3aplG2+Z/DlFbjZz4V1J2nWSfe3Od4kp0byZ+dIK1oGzlvukGe+L2v460H7CLKcx8H 9QNg== X-Forwarded-Encrypted: i=1; AL+hErE6OUXk6+Lnd9AUAKyLEpEb1EZk6A0WcavNwf7TV22ZJsVv46a5CNOGeFrLl3vjIEEiuISwbqC3V8feojA=@vger.kernel.org X-Gm-Message-State: AB4q+uHHcSWYX6AdanI/rtHmg1d/sAoYuTT+59lkhE3efhQm3nSL+Lmk cJrl6zqvkv/fSaHjWX79GqyE8Qxyq7mKfb2lBuRHW9k3O/h680ASfex4 X-Gm-Gg: AZFdTZdt+uvPwFbgKhE9Lf97GjIQ2w1DSLuT5A1RtzRj6UDTSmqQMsTV7IzA6itPoJ0 iET6FPdKY+zEEWpMaBPe++gcqWN3AhrZecdRjRq6RQ5D0i7YnieW4EJDFxlh/hjk5robDhrrD5N CPRCzSjKyAzwHLw3bqE0PEZItipKTzNMz51A2XjowWug6E6sVFMu0CaAptdaAF3ZaKi49SLBuvg v86FUu4BFwS6/lGQWv9oXmvNxCsIYJAtSUqraKLmRoM6NoIXmibWns8X0g+rNIzzOq3rvb3LQFy lf5MuRE+wMVpDSChEoLCwnv08POX3ntglJBWyV9SmCxbM5ulCaG4oQKcY4fXmJQSJIEqTTqGASp wF1t9A6YiEI+fU+R2PtddvxBaWE0mnUUj1bBno2DISAmcS9Ij/UePffUusHu2WSAVPP3xjNTW6w baeX2apJoQhKDgXSMf9U8ozPlY7N6XTN/dFFIhRxQKuLAi+W9EjqJklQQKuyN/syF7ImBedlI/l uHYq6dYpARX0uSwLCAiFnEu6pIckuo0uu2GiCt0bHnfRqX/oLo3AZYT3ZJZT0i7Ukuw3Wv/ngCk ggW9FMygJG+r9YHbAzbQkzyuq6b9LjB3YjeINPPJ3FwWC0StADgYxI0EflOb46dCVOrAlG1AiIp blepwPAdUK7uqq39t3Xg4HnVGjpSYHHowP1k+h6dWV8q3BmmTQ2rf9+JEDgr1Yw9H 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-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 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