From patchwork Thu Jun 1 16:00:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 13264201 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 C00F0C7EE23 for ; Thu, 1 Jun 2023 16:00:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version: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:In-Reply-To:References: List-Owner; bh=q+N1LaccOyfy/Uqk+U0h/KwUSLyWjaCacLd866x5YhA=; b=zYKeIhd0kNYJ3q 6Bf0HW/B7lP/N2dRQRp0SLLG5ntmZWzjn85lg3hDvpWjT9F1lKeyy9XpA1Yn9gGWwFxZDVKY1DbCC jP49fw7BUd+m6PAx3zro4/UI8j24Yn8Dd1hQMV5vev0c0m2Zh/DBfP0RT4UE3GEU8cedKPh08lu4v woUIDajSwvjroFTo4UyjEm39BSMEW6XXJIeSKYfb4exntzzgvUdgzqYai3VxdT+FBo88lpAc+kYBW PCELJISpwT9JCZ6v514/hg9/Hz0raKwcKPogkQFUA+hJ21Z2y+981LK9+FzqP0WVkFjBrCLF5/PKV 7vPfjMMsL1LJz3T9yprw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1q4kj9-0048WD-0O; Thu, 01 Jun 2023 16:00:39 +0000 Received: from mail-pg1-x535.google.com ([2607:f8b0:4864:20::535]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1q4kj6-0048TZ-2b for linux-riscv@lists.infradead.org; Thu, 01 Jun 2023 16:00:38 +0000 Received: by mail-pg1-x535.google.com with SMTP id 41be03b00d2f7-53fe2275249so557104a12.2 for ; Thu, 01 Jun 2023 09:00:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1685635233; x=1688227233; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=CSSMVbhKiQKes2FXn07HT2PehTcS3LvEK8iBz94W+GE=; b=G40YuqS1UsFpH+HN2SRB+wSxhkekURBx9NKclSP3kmF3V6Fhww8jiP3cDwI15wLucl SVk/ii9MR6JUzAtMSQ3vdNuC9icNcRIH+N5v2o4AiDjDBIa49c6JdpsvTGQJfLdq/7lp S3EubNPyT2dJIDTD1Pvpj6u6Qf5IlwBF0m/Pg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685635233; x=1688227233; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=CSSMVbhKiQKes2FXn07HT2PehTcS3LvEK8iBz94W+GE=; b=jOqH122v186QZlh4nigUu4H0WAwmbvB0yAwwj4MSBgFswETlWzS63W06DMB5xxTQqY dE3/LGN5LvF7sR3lI1eLZJMUQUW2W4NOuFtP4WjoXzNh7ZzWBpGMk5/M40pInMyQm+KD TDmKa+BoaHoqeR1ojo/LajQRmJnZY5g30IUFibjiZbofjZkq+WjqJhFsCf6ZeGW0h5jS T0EVmgn+CT1fW1TuQvt9JIq66/XODTI66UC4UPhbAeMslTtQDSb0CHkI/KutRvdRd2F2 sRuHUmTcQRxYhlDjLNMyVRiPFykF5+Yq78Izu7R/d1QN/xWeVKLoLtUm0Uz0hpI5dUdA B0IA== X-Gm-Message-State: AC+VfDyld7Vv3Lf11BGESA6cviOjpLkM+WZe34iX4/bMjTLAjnqk2UiK VZNKkb4f9zWWTcpBvCW+61QT9w== X-Google-Smtp-Source: ACHHUZ7Y/j1K0K+OI8RiBenflSTAHsKRmF86JYQCC8TX/WswDP9/djNgLEAJ0bZT13bWWUVoH+F2WQ== X-Received: by 2002:a17:902:8307:b0:1ae:305f:e949 with SMTP id bd7-20020a170902830700b001ae305fe949mr5823287plb.6.1685635232876; Thu, 01 Jun 2023 09:00:32 -0700 (PDT) Received: from www.outflux.net (198-0-35-241-static.hfc.comcastbusiness.net. [198.0.35.241]) by smtp.gmail.com with ESMTPSA id u10-20020a17090341ca00b001ae42480c82sm3662096ple.226.2023.06.01.09.00.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Jun 2023 09:00:32 -0700 (PDT) From: Kees Cook To: Paul Walmsley Cc: Kees Cook , Thorsten Leemhuis , =?utf-8?q?Joan_Bruguera_Mic=C3=B3?= , Palmer Dabbelt , Albert Ou , Masahiro Yamada , Conor Dooley , Nick Desaulniers , Alyssa Ross , Heiko Stuebner , "Gustavo A. R. Silva" , linux-riscv@lists.infradead.org, Andrew Jones , linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: [PATCH v2] riscv/purgatory: Do not use fortified string functions Date: Thu, 1 Jun 2023 09:00:28 -0700 Message-Id: <20230601160025.gonna.868-kees@kernel.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2046; h=from:subject:message-id; bh=D3i2fx38SuZJfTLJnG4ariCLKDkYJ4XMlsdasO2er94=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBkeMCcr4GEaZm3mIC8bLTgzCfv0iCFwT3u6pSWp2/4 oidJwVuJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZHjAnAAKCRCJcvTf3G3AJosmD/ 9VUGMkQRPLvYkOTpl4FX/cepeYlRCO0IrieC8te7msoWdE6Be3W2w+fdYWKAqFdqWAsxpFdq+r1dWa OlSjzEEEhmDAzdQyqllhVMRdKGSYQXUnj2QrDrq+ZHaMpAAQK9xDhDdsqtgpfpUQkfH4HPnJKn7EUJ 9fiG93rORBeoFqTeTjFguaR7qgPET/W/RhadhoeETdqeYKEibyeHTwnrONdW6SLkfmuXn9M4FOCE44 z+z7CZMwLYp5MxSQptwNFeg/XsQXCZlf2ieOvsS3J4xAoOFHGXaSQNorAzm+woUUMP2rdiK6xa08Hl IrCYt5TNpi/0oL0SEUnrEwj+wqCUQ1KPP2AnmfvST8kW/YDCCgEmonSuSNLtPw3LmUcEkc4nLXVPbN wul8nbMNGrFwms1JhbMFkjxGW1xvvqv3uqVahy0MPNOf9a7rABAbfmx5KQE3IjK8OQb75ieWxK5F+s shhk0Qz/JS5E8PM9YKzwIpMFA+I48NuQOSc0zNmxwCvKvJMUspLhUQaqqyosz+IGQw9wLNPLLycEdX PS+r83Gn1ZtHgbteZK2hGOlv4oDVxDE4OYZL/Fl2lHCvjDnXUxGqeqA40rVpidDWZyMkQMP372jPwa WbKuQ7fIa8qQqvkrjU8EzNp/oqETsJZs6fuaXUDAj5uLbRH6uHn2fB2I9BvQ== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230601_090036_864034_359F9B5C X-CRM114-Status: GOOD ( 12.41 ) X-BeenThere: linux-riscv@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-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org With the addition of -fstrict-flex-arrays=3, struct sha256_state's trailing array is no longer ignored by CONFIG_FORTIFY_SOURCE: struct sha256_state { u32 state[SHA256_DIGEST_SIZE / 4]; u64 count; u8 buf[SHA256_BLOCK_SIZE]; }; This means that the memcpy() calls with "buf" as a destination in sha256.c's code will attempt to perform run-time bounds checking, which could lead to calling missing functions, specifically a potential WARN_ONCE, which isn't callable from purgatory. Reported-by: Thorsten Leemhuis Closes: https://lore.kernel.org/lkml/175578ec-9dec-7a9c-8d3a-43f24ff86b92@leemhuis.info/ Bisected-by: "Joan Bruguera Micó" Fixes: df8fc4e934c1 ("kbuild: Enable -fstrict-flex-arrays=3") Cc: Paul Walmsley Cc: Palmer Dabbelt Cc: Albert Ou Cc: Masahiro Yamada Cc: Conor Dooley Cc: Nick Desaulniers Cc: Alyssa Ross Cc: Heiko Stuebner Cc: "Gustavo A. R. Silva" Cc: linux-riscv@lists.infradead.org Signed-off-by: Kees Cook Reviewed-by: Conor Dooley Reviewed-by: Palmer Dabbelt Acked-by: Palmer Dabbelt --- v2: - only limit fortify for sha256 (conor) v1: https://lore.kernel.org/lkml/20230531003404.never.167-kees@kernel.org --- arch/riscv/purgatory/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/riscv/purgatory/Makefile b/arch/riscv/purgatory/Makefile index 5730797a6b40..8c73360c42bb 100644 --- a/arch/riscv/purgatory/Makefile +++ b/arch/riscv/purgatory/Makefile @@ -31,7 +31,7 @@ $(obj)/strncmp.o: $(srctree)/arch/riscv/lib/strncmp.S FORCE $(obj)/sha256.o: $(srctree)/lib/crypto/sha256.c FORCE $(call if_changed_rule,cc_o_c) -CFLAGS_sha256.o := -D__DISABLE_EXPORTS +CFLAGS_sha256.o := -D__DISABLE_EXPORTS -D__NO_FORTIFY CFLAGS_string.o := -D__DISABLE_EXPORTS CFLAGS_ctype.o := -D__DISABLE_EXPORTS