From patchwork Tue Sep 12 14:26:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Klaus Jensen X-Patchwork-Id: 1833068 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=irrelevant.dk header.i=@irrelevant.dk header.a=rsa-sha256 header.s=fm3 header.b=Ccmrng3N; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.a=rsa-sha256 header.s=fm1 header.b=iBl/sDf6; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=patchwork.ozlabs.org) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4RlQt15Rf8z1yhZ for ; Wed, 13 Sep 2023 00:28:09 +1000 (AEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qg4M8-0001Wr-In; Tue, 12 Sep 2023 10:27:08 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qg4M4-0001VH-9F; Tue, 12 Sep 2023 10:27:04 -0400 Received: from wout1-smtp.messagingengine.com ([64.147.123.24]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qg4M1-0006FV-Vm; Tue, 12 Sep 2023 10:27:03 -0400 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.west.internal (Postfix) with ESMTP id 81CFC3200034; Tue, 12 Sep 2023 10:26:59 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Tue, 12 Sep 2023 10:27:00 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=irrelevant.dk; h=cc:cc:content-transfer-encoding:content-type:content-type :date:date:from:from:in-reply-to:in-reply-to:message-id :mime-version:references:reply-to:sender:subject:subject:to:to; s=fm3; t=1694528819; x=1694615219; bh=oiN0GeCq/ks9Q7gseTFkhe2yF Z2NQWahiVOVHZ3K6HE=; b=Ccmrng3NaZWLJtA3QSMafH/inciAZb7VsWra0qEsa mH0i0jj5goRy0WUItU+eGwPe8fO4nuUY+AfukyWY00H4AlDK1CoTeVX8t+dMYDe9 G0UjSFVSNUpvu49TXHpMLs14OItJLSIZzoteozBJo8tBvwLzoamtk8rXopLlq98w e6eVCoNPQnGzxQ+JKpWR1Fj0W7GeXQydblF6LYf0+KGdHlHxY5PbfAFyeNhR0YRa 3hKq0ia6dFg7bv6Lw2ENilje2n47DjJHvbZ1iufwpndD21t/pvGDh0w02GyZfJOA jN4MW1GMyn3WnRxvtjG7oEqCif8sAt52vzGBaUu/YGv9g== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:sender:subject:subject:to:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t= 1694528819; x=1694615219; bh=oiN0GeCq/ks9Q7gseTFkhe2yFZ2NQWahiVO VHZ3K6HE=; b=iBl/sDf6EH/OBg0uHwVsZjJvvC0XKHChc0s7iHolqdf+G9Rzbzk anW+pKHXty40BuG+bUdUyyLrBJpkm5HzYACmPGqzQIrncCqYaDZ7VYxTG9lSj8jC MNqHeEJ9/3Ya3ha7nCZ5T2hci6A5cPUWib22+/9yeb12TF68tL91UtjHbnYTNaM+ AEFaifbZrQApW6BHzUPrItmbw4zCMVetMgaaRIckU4DSZm3ik30gO5YO+s+1Dupy 4C6QBJ8aDdpnHNlNWAfEernisULDaImTmwaNe0xAsCBA7K3JJqc4t88t9BqH96nc klLtlU1iJ2SHRy6KcwDAAI5EmZ4+di6p3RQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedviedrudeiiedgjeeiucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhephffvvefufffkofgjfhggtgfgsehtkeertdertdejnecuhfhrohhmpefmlhgr uhhsucflvghnshgvnhcuoehithhssehirhhrvghlvghvrghnthdrughkqeenucggtffrrg htthgvrhhnpeefvedtueetueduffevgffgtdeftdeuleffhfeigeffkeegfeejfeffteej iefhvdenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpe hithhssehirhhrvghlvghvrghnthdrughk X-ME-Proxy: Feedback-ID: idc91472f:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 12 Sep 2023 10:26:57 -0400 (EDT) From: Klaus Jensen To: Peter Maydell , qemu-devel@nongnu.org Cc: Stefan Hajnoczi , Fam Zheng , qemu-block@nongnu.org, =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Hanna Reitz , Klaus Jensen , Kevin Wolf , Keith Busch , =?utf-8?q?P?= =?utf-8?q?hilippe_Mathieu-Daud=C3=A9?= , Klaus Jensen Subject: [PULL 1/2] hw/nvme: Use #define to avoid variable length array Date: Tue, 12 Sep 2023 16:26:51 +0200 Message-ID: <20230912142649.11002-5-its@irrelevant.dk> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20230912142649.11002-4-its@irrelevant.dk> References: <20230912142649.11002-4-its@irrelevant.dk> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=1587; i=k.jensen@samsung.com; h=from:subject; bh=E7DjdRj14im2VetumRoAJOcha10LzkLK3x/G/8tlZSg=; b=owJ4nAFtAZL+kA0DAAoBTeGvMW1PDekByyZiAGUAdSr6/qYO46V3EqWovW+IbGsg1gu0edN6q 5TRTBw14ybxnYkBMwQAAQoAHRYhBFIoM6p14tzmokdmwE3hrzFtTw3pBQJlAHUqAAoJEE3hrzFt Tw3pmTkIAKDoVQIHzlcjatjeXVTO9cX8C0kb2HrFURVHNSVYqE8UJZyAMVb3nvGBuRGAOP3hj2G vQrdRNIo22vwU1JjhTzdxsmP7iv6QRmn87l5iJyCGsNMYMptWer/fcPRjQ5t840syI1RTcKnVA1 CuVOlx9CiOOWHgBmwBKdv1Kht9PizzaFAPX59OhQFWq0mK7vlMFOOn4V8YeaUikQA91AHfwBUmL vOm7jil2MLJGzyGH6llHWCQh4BfTi8IMIpyiscI3X0lZWfkO16/19sY7Le1a+omrItmzp53zur5 ObGEIXsHHgvtANeLv2+3lOEyL70bAuGBM04Uk3k84FVqq/DT9LOcgKXg X-Developer-Key: i=k.jensen@samsung.com; a=openpgp; fpr=DDCA4D9C9EF931CC3468427263D56FC5E55DA838 Received-SPF: pass client-ip=64.147.123.24; envelope-from=its@irrelevant.dk; helo=wout1-smtp.messagingengine.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: Philippe Mathieu-Daudé In nvme_map_sgl() we create an array segment[] whose size is the 'const int SEG_CHUNK_SIZE'. Since this is C, rather than C++, a "const int foo" is not a true constant, it's merely a variable with a constant value, and so semantically segment[] is a variable-length array. Switch SEG_CHUNK_SIZE to a #define so that we can make the segment[] array truly fixed-size, in the sense that it doesn't trigger the -Wvla warning. The codebase has very few VLAs, and if we can get rid of them all we can make the compiler error on new additions. This is a defensive measure against security bugs where an on-stack dynamic allocation isn't correctly size-checked (e.g. CVE-2021-3527). [PMM: rebased (function has moved file), expand commit message based on discussion from previous version of patch] Signed-off-by: Philippe Mathieu-Daudé Signed-off-by: Peter Maydell Signed-off-by: Klaus Jensen --- hw/nvme/ctrl.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hw/nvme/ctrl.c b/hw/nvme/ctrl.c index 539d27355313..d99a6f5c9a2e 100644 --- a/hw/nvme/ctrl.c +++ b/hw/nvme/ctrl.c @@ -1045,7 +1045,7 @@ static uint16_t nvme_map_sgl(NvmeCtrl *n, NvmeSg *sg, NvmeSglDescriptor sgl, * descriptors and segment chain) than the command transfer size, so it is * not bounded by MDTS. */ - const int SEG_CHUNK_SIZE = 256; +#define SEG_CHUNK_SIZE 256 NvmeSglDescriptor segment[SEG_CHUNK_SIZE], *sgld, *last_sgld; uint64_t nsgld; From patchwork Tue Sep 12 14:26:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Klaus Jensen X-Patchwork-Id: 1833065 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=irrelevant.dk header.i=@irrelevant.dk header.a=rsa-sha256 header.s=fm3 header.b=r+UKaOfF; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.a=rsa-sha256 header.s=fm1 header.b=RynVoW7S; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=patchwork.ozlabs.org) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4RlQsc4B4Cz1yhZ for ; Wed, 13 Sep 2023 00:27:48 +1000 (AEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qg4MF-0001a4-KG; Tue, 12 Sep 2023 10:27:15 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qg4M8-0001XB-MI; Tue, 12 Sep 2023 10:27:08 -0400 Received: from wout1-smtp.messagingengine.com ([64.147.123.24]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qg4M6-0006Fv-Fc; Tue, 12 Sep 2023 10:27:08 -0400 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.west.internal (Postfix) with ESMTP id 22814320098B; Tue, 12 Sep 2023 10:27:03 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Tue, 12 Sep 2023 10:27:04 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=irrelevant.dk; h=cc:cc:content-transfer-encoding:content-type:date:date:from :from:in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to; s=fm3; t=1694528822; x= 1694615222; bh=T5/wSFH5UysJriXBNU9TNxkLsFcDhOioChngTaRpK3Q=; b=r +UKaOfFmRHyyKX4TeQay5uu+FLIM6px5K0cDkbN02JZTpT34mR9sqhnc6afpthV6 cT/rZMXepDEP9LGkVpbwQf8hJzBg41z0Rd7vcyfhXmkN5KX7deJZdlEorygi5yxA Atqi/KEfecdtz47L69uojEIdoyrYSWwM3X+NgF5U7vLOVOcdoNjXDcYCsXiDnCGz 3qPrKPwfRG0hlCI1I92iJZ+4dfvKn61GO7KBcBv1pRABOMaxwBUFjFvSXnsdnuLp VCioXKeDqC64uhRon9QUiJMBXw557Ok/h4TNni0S3aaKyizqFaKa7kBge0gIDH1q lFesa4iPIz83WYXr6+IwQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1694528822; x= 1694615222; bh=T5/wSFH5UysJriXBNU9TNxkLsFcDhOioChngTaRpK3Q=; b=R ynVoW7S6tMus41nELXFqG362gTwDaHvk2T8dc6vKtMzas1NvdgseYAaEdgHTzIYS 1oWSBVYl5btZDiYNu2730sITKv8rfQkf8+Z/JtUEkZfWQO1vLLEsREFuoICBKVcr 9ZmxOeOCgZMVN89VqXvORILzD7y72qcPP7ze+pKcfvlWSA4o+67fX9W78E2K0Qbp jG3Ut3dO1e2oZcbulRL3dvS2+m1yJCrnFErbevfeN1SkCvwNM7COWCHj129OP2Ma OCHJ9aklJp1rHZkDNaBVgTCD4F7Nx2DeLG8Gk3PeUJiVzhxXl3nWZD9vearCQtH4 O2El84+LplqiOd9oHH+9w== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedviedrudeiiedgjeeiucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhephffvvefufffkofgjfhgggfestdekredtredttdenucfhrhhomhepmfhlrghu shculfgvnhhsvghnuceoihhtshesihhrrhgvlhgvvhgrnhhtrdgukheqnecuggftrfgrth htvghrnhepjefgieelgfeiveehkeeuveehheekfeevgeeigfehfefgjeejhefffeegudej udegnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepih htshesihhrrhgvlhgvvhgrnhhtrdgukh X-ME-Proxy: Feedback-ID: idc91472f:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 12 Sep 2023 10:27:00 -0400 (EDT) From: Klaus Jensen To: Peter Maydell , qemu-devel@nongnu.org Cc: Stefan Hajnoczi , Fam Zheng , qemu-block@nongnu.org, =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Hanna Reitz , Klaus Jensen , Kevin Wolf , Keith Busch , Klaus Jensen Subject: [PULL 2/2] hw/nvme: Avoid dynamic stack allocation Date: Tue, 12 Sep 2023 16:26:52 +0200 Message-ID: <20230912142649.11002-6-its@irrelevant.dk> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20230912142649.11002-4-its@irrelevant.dk> References: <20230912142649.11002-4-its@irrelevant.dk> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=1072; i=k.jensen@samsung.com; h=from:subject; bh=k8Y7iQz7gOK9fuxUsAHHCrYcflluV+Qz2N+9aldrpJE=; b=owJ4nAFtAZL+kA0DAAoBTeGvMW1PDekByyZiAGUAdSrjujEk+l6HiX1de5lsZ3HThXQrBQkwU Xa9JxrwkYN8EIkBMwQAAQoAHRYhBFIoM6p14tzmokdmwE3hrzFtTw3pBQJlAHUqAAoJEE3hrzFt Tw3pGFIH/ijZb901m/wUU0yApw6ONIxcyLqh4GM3mnHwFukiBb7iAEyhUVWbwl1hfq6uxB61Vkc giEcT+l5FSxZp8bfMlNeNKwmx0fs+qyx1HNmMuUYC0nyAawyC/QZnwDIQCap5geVCmZYCdNPoU/ SrphZxi4qQ6Oj1/YOmeoGy31vJSRB2DJvXOOiX3roqk+P0M9F7N4A2jyx8Qa8qDxRy/ymGnaoHv yKHtVsW3POY/0X+akDXLVTTJlle/oitBqEqYqPpIpLVj4NGzq23/tJYQHElAZQ9X/FF7SNp+lz0 QKaRrXX1P3BabH7NXJEeorjbC1sf9rmDvCvd3WAw2/UwZPIlql3QV6hD X-Developer-Key: i=k.jensen@samsung.com; a=openpgp; fpr=DDCA4D9C9EF931CC3468427263D56FC5E55DA838 Received-SPF: pass client-ip=64.147.123.24; envelope-from=its@irrelevant.dk; helo=wout1-smtp.messagingengine.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: Peter Maydell Instead of using a variable-length array in nvme_map_prp(), allocate on the stack with a g_autofree pointer. The codebase has very few VLAs, and if we can get rid of them all we can make the compiler error on new additions. This is a defensive measure against security bugs where an on-stack dynamic allocation isn't correctly size-checked (e.g. CVE-2021-3527). Signed-off-by: Peter Maydell Signed-off-by: Klaus Jensen --- hw/nvme/ctrl.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hw/nvme/ctrl.c b/hw/nvme/ctrl.c index d99a6f5c9a2e..90687b168ae1 100644 --- a/hw/nvme/ctrl.c +++ b/hw/nvme/ctrl.c @@ -894,7 +894,7 @@ static uint16_t nvme_map_prp(NvmeCtrl *n, NvmeSg *sg, uint64_t prp1, len -= trans_len; if (len) { if (len > n->page_size) { - uint64_t prp_list[n->max_prp_ents]; + g_autofree uint64_t *prp_list = g_new(uint64_t, n->max_prp_ents); uint32_t nents, prp_trans; int i = 0;