From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 63C37C46CCD for ; Thu, 21 Dec 2023 16:32:14 +0000 (UTC) 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-Type: Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-Id:Date :Subject:Cc:To:From:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=0Eq2oR3a9qab6om7P8wakiZpwWsAR71Y/xnun3o/NoM=; b=PO47oiRQQJgyvXG8F3SuFjRahX TGTXagtuyECMIAo+aM1ULN69bha6vVQX/DcH2DYP8VUfq4OM/j0ZMfFUnBJRJyf0hU197wXuJB6hb Tctfd4/qJgB9hvybsygvB9PFaF2QO81ty+1IOSGg438iqbg1aQFRevWdRhHQmiGgNq6bVbNvM0K/E phqyuGOBMZfWhHZ6pWq8TQ7C/DViN6NYeoUmwXB9ILu6Adu4fqS/5VIgMObMiP1a9MTWEbd/5p9kI 2EeOdBGShZgDJFbhECP5IhU9fJ87hskKSrPSMNiFvFdmpERD2PrfmwiAuKK879cM71z3m7GSbOOyd bXE0MMrA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1rGLxz-003WSK-1y; Thu, 21 Dec 2023 16:32:11 +0000 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1rGLxw-003WQW-2F for linux-nvme@lists.infradead.org; Thu, 21 Dec 2023 16:32:10 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1703176325; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=0Eq2oR3a9qab6om7P8wakiZpwWsAR71Y/xnun3o/NoM=; b=KdOmKfRLwsOz5qwfQ71BA4LwjMTAzex3BODmS/FeC+0KaaRWbY7bpbs+0Nie9uXv29HO+7 W2f16l6K3AsRknwbdOxVGaZBsQsvbOv0azd3YLtVPpkBc3BSWwjOBPKGC4u5WOLsvSWBsK NcXhrAl5NH2j9NQKEqJCs0WfDSPpLqc= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-359-jV4GWfy-OmWCLkL1FLoCUw-1; Thu, 21 Dec 2023 11:32:00 -0500 X-MC-Unique: jV4GWfy-OmWCLkL1FLoCUw-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id F133F8493F4; Thu, 21 Dec 2023 16:31:59 +0000 (UTC) Received: from kalibr.redhat.com (unknown [10.47.238.49]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1C1722026D66; Thu, 21 Dec 2023 16:31:57 +0000 (UTC) From: Maurizio Lombardi To: sagi@grimberg.me Cc: zahavi.alon@gmail.com, hch@lst.de, chaitanyak@nvidia.com, linux-nvme@lists.infradead.org, kbusch@kernel.org Subject: [PATCH 1/2] nvmet-tcp: Fix a kernel panic when host sends an invalid H2C PDU length Date: Thu, 21 Dec 2023 17:31:53 +0100 Message-Id: <20231221163154.605669-2-mlombard@redhat.com> In-Reply-To: <20231221163154.605669-1-mlombard@redhat.com> References: <20231221163154.605669-1-mlombard@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231221_083208_802449_1D75D00B X-CRM114-Status: GOOD ( 13.46 ) X-BeenThere: linux-nvme@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "Linux-nvme" Errors-To: linux-nvme-bounces+linux-nvme=archiver.kernel.org@lists.infradead.org If the host sends an H2CData command with a DATAL field equal to 0, the kernel crashes in nvmet_tcp_build_pdu_iovec(). Unable to handle kernel NULL pointer dereference at virtual address 0000000000000000 lr : nvmet_tcp_io_work+0x6ac/0x718 [nvmet_tcp] Call trace: process_one_work+0x174/0x3c8 worker_thread+0x2d0/0x3e8 kthread+0x104/0x110 Getting an H2CData Packet with DATAL=0 is unexpected, treat it like a fatal transport error. Also, the PDU length should never exceed the MAXH2CDATA parameter which has been communicated to the host in nvmet_tcp_handle_icreq(). Signed-off-by: Maurizio Lombardi --- drivers/nvme/target/tcp.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/drivers/nvme/target/tcp.c b/drivers/nvme/target/tcp.c index 4cc27856aa8f..44eda6081e99 100644 --- a/drivers/nvme/target/tcp.c +++ b/drivers/nvme/target/tcp.c @@ -24,6 +24,7 @@ #include "nvmet.h" #define NVMET_TCP_DEF_INLINE_DATA_SIZE (4 * PAGE_SIZE) +#define NVMET_TCP_MAXH2CDATA 0x400000 /* 16M arbitrary limit */ static int param_store_val(const char *str, int *val, int min, int max) { @@ -923,7 +924,7 @@ static int nvmet_tcp_handle_icreq(struct nvmet_tcp_queue *queue) icresp->hdr.pdo = 0; icresp->hdr.plen = cpu_to_le32(icresp->hdr.hlen); icresp->pfv = cpu_to_le16(NVME_TCP_PFV_1_0); - icresp->maxdata = cpu_to_le32(0x400000); /* 16M arbitrary limit */ + icresp->maxdata = cpu_to_le32(NVMET_TCP_MAXH2CDATA); icresp->cpda = 0; if (queue->hdr_digest) icresp->digest |= NVME_TCP_HDR_DIGEST_ENABLE; @@ -1002,6 +1003,13 @@ static int nvmet_tcp_handle_h2c_data_pdu(struct nvmet_tcp_queue *queue) } cmd->pdu_len = le32_to_cpu(data->data_length); + if (unlikely(cmd->pdu_len == 0 || + cmd->pdu_len > NVMET_TCP_MAXH2CDATA)) { + pr_err("H2CData PDU len out of range\n"); + /* FIXME: use proper transport errors */ + nvmet_tcp_fatal_error(queue); + return -EPROTO; + } cmd->pdu_recv = 0; nvmet_tcp_build_pdu_iovec(cmd); queue->cmd = cmd; -- 2.39.3