From patchwork Tue Aug 20 19:48:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Maurer X-Patchwork-Id: 13770566 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id BE7B5C5320E for ; Tue, 20 Aug 2024 19:49:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4DECA6B0089; Tue, 20 Aug 2024 15:49:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 467FB6B008A; Tue, 20 Aug 2024 15:49:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 246366B008C; Tue, 20 Aug 2024 15:49:28 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 050166B0089 for ; Tue, 20 Aug 2024 15:49:27 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id B311A80520 for ; Tue, 20 Aug 2024 19:49:27 +0000 (UTC) X-FDA: 82473663174.08.2F75932 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf03.hostedemail.com (Postfix) with ESMTP id A32A320029 for ; Tue, 20 Aug 2024 19:49:25 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=So9jI0VN; spf=pass (imf03.hostedemail.com: domain of 3RPPEZgcKCHsllZtqdqfnnfkd.bnlkhmtw-lljuZbj.nqf@flex--mmaurer.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3RPPEZgcKCHsllZtqdqfnnfkd.bnlkhmtw-lljuZbj.nqf@flex--mmaurer.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1724183303; h=from:from:sender: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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=jYOQ4ic50oYuK2FpEcKGnGi9zqTDnUtCD7+2BoOkpco=; b=SJvmagtqUklBA64cDHqL0E9TnY5fmpD9jirlfrQN6z+UlXOW7xrDnq2YyWtTYD5A0uBB70 vGz3BeQEHQ7OMJ8Y5kRlL3/L0MjRNRi6iRmhy8j5z04HMwNYggdjnaugQ01qZhfG55oHwa kau+zix6bArxjaGy+sGJ2TpAf2uiwz0= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=So9jI0VN; spf=pass (imf03.hostedemail.com: domain of 3RPPEZgcKCHsllZtqdqfnnfkd.bnlkhmtw-lljuZbj.nqf@flex--mmaurer.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3RPPEZgcKCHsllZtqdqfnnfkd.bnlkhmtw-lljuZbj.nqf@flex--mmaurer.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724183303; a=rsa-sha256; cv=none; b=clBbp8WFWA2ZSvDDo3BEF+wcKCP3fFlBSebj+rp/KV5KbbHyKwY1EMUyRfzU6pIgIHx1nG rSsVg+jeF7oVD/Ee35RmfJNjkurIIBK13pVNcF0mYHxrcjEFJ+uNPH73DsQZVEkjg8TtMJ 1uEVM+EZ3pSJUsn2+AsKYo2kGbGmYf8= Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6b46d8bc153so55395687b3.2 for ; Tue, 20 Aug 2024 12:49:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1724183365; x=1724788165; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=jYOQ4ic50oYuK2FpEcKGnGi9zqTDnUtCD7+2BoOkpco=; b=So9jI0VNnWg6R+y/aZWp1BVn2iz+jkSz968x9jKCcpPnhZzHacpuEr7ob1n+yxkMXM CSV8QLI0PAjRk8LFBRIl5oq9jsrZFJGeYzmMZQKjMoeUOWQfPvrdKZiWYnX+3PI5FH97 QpFi1gUkKIzYUe20g2qxwbT0oPdZB+Bq6bdd8d1sPeYs6ejbvov2iQpZqLRby5RHWxLA IVY1BGdsndjavpNJjRFyse0Y8189zvSxkmMdIqdg97q9HJA6wtFqyv8YXdiedYjbaek+ Th6Wr0pj1XTP9bNem6tR3FNmIS3r5fi4HbZe3JZO1QLa0wX67IuDhT8Atcv87JacGtIi Z7jg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724183365; x=1724788165; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=jYOQ4ic50oYuK2FpEcKGnGi9zqTDnUtCD7+2BoOkpco=; b=biP0CgDVDYjeyYIefi5hfcz3soA8a3sHiPAY9KAllDQUYg8GF8DE0KfpzNzOLk9CGv loB7cbl00eugUkfKMYs55TAzNNNHG12gdrpbMTg8TbpeM9ckI7yKkdcK3hYmhRyQKxLx /aOG2+h8KV3mM0eNlX8OXHyUng6zrfl1y3vWQLkSEvp/ZGGhdtZHQg+eDFYQGCmA6j7f +4EWy2IXbiLIwlLz4MXOSZBPvEZ+bcqH4cCa4bQ7qrSClgLfM3weh68Y92VKqfXJ7SNH NfqEyD8WQFrfMCkkzo8UAwufF0hbqZu/xvzKHLTRS0SOpNalWvKdbmU/77q4ABxERyGD 8hZg== X-Forwarded-Encrypted: i=1; AJvYcCVch/xfIR+ec+Mw4pjBmywxuGJvdkJKGHZ0/s+orwyA8j2EeYgRCf25BtwPfI565EPWmB1JJeEA7g==@kvack.org X-Gm-Message-State: AOJu0Ywm2bHsgUFt9nGbmPO0BYpf9UOxkF3+jAjUPuTsp07LRC3j3jOX M34Ce6niIqoWOucv6oy9t5to22oxRycvrG9sHpPBb0ein9zMPCRN9i5VfwJgivscQnbHD9GRY5h O0pXd2g== X-Google-Smtp-Source: AGHT+IE7sGIPkJ+edoqGJ+fIEw1QkDsz00XLJirRhxEYq6ET/0uf39KsW1PUXkwOfVWsCP5G1VWLg50n5WDX X-Received: from anyblade.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1791]) (user=mmaurer job=sendgmr) by 2002:a05:690c:340a:b0:6ad:351e:a9d0 with SMTP id 00721157ae682-6c09c5982aamr6117b3.3.1724183364571; Tue, 20 Aug 2024 12:49:24 -0700 (PDT) Date: Tue, 20 Aug 2024 19:48:58 +0000 In-Reply-To: <20240820194910.187826-1-mmaurer@google.com> Mime-Version: 1.0 References: <20240820194910.187826-1-mmaurer@google.com> X-Mailer: git-send-email 2.46.0.184.g6999bdac58-goog Message-ID: <20240820194910.187826-4-mmaurer@google.com> Subject: [PATCH v4 3/4] kbuild: rust: Enable KASAN support From: Matthew Maurer To: andreyknvl@gmail.com, ojeda@kernel.org, Masahiro Yamada , Andrey Ryabinin , Alex Gaynor , Wedson Almeida Filho , Nathan Chancellor Cc: dvyukov@google.com, aliceryhl@google.com, samitolvanen@google.com, kasan-dev@googlegroups.com, linux-mm@kvack.org, glider@google.com, Matthew Maurer , Vincenzo Frascino , Nicolas Schier , Boqun Feng , Gary Guo , " =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= " , Benno Lossin , Andreas Hindborg , Nick Desaulniers , Bill Wendling , Justin Stitt , linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, llvm@lists.linux.dev X-Stat-Signature: bdhc6ydka4ptfuaggycrnisnsyqoomyi X-Rspam-User: X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aY0HP/gKWVDN5pmDNY1WSC7hOpcwSDAt6xgdlzrLmslbVzS/dRbrkx2m4ns2ZzidCIf8U9lmnU+87Yo6fztKhgU3ywtJbBzl9CW84nIWebiK2bAnkCZqp7Ql5l8JRODAUm2xzXTNJKNhA127OGduX067lFbO5PyLyr3Y08Li5moGfbU+9DkIu9z0zDee2HASPuvGr6Cpdbe49ahGUu8uA/rZTeNYwYhWUz9pDpiidSwiF8is09Pbf/U8WYig/vcZUdNMR3Q9eUqa0oiPZSSkrAoKKjIaSKfSjhiBqk178JhhnNgHWRod1qHcT3YdrG06fnzq0cLHdcoMStip7GG9riXWotLe1/8xa0lUdK1Tj613+hvOZ8je3iAN9icIXr8+EgdxnxPW84PhcFnpb+3GkMn5ZIeqOPaskKUs76BORL8XwWjNQnAIG7M9NtFdLw2wYlrErPAZFl5uSZIVg5d951sKEp22QfKBOugcGc2I9W9Hkne0XzOkuTFhRhiFnq8NdvGmtHnFd031lD236GL5VO/nicRkzkmzGdk9MKXk11lu+rZdjHbb9IOr+ohRt9V2Vcn540SIzjfdL7typE99/Q2tTtb54iCNate7WG81D+ftqbNkyCY7fa4JuZrmOczaWnA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.013398, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Rust supports KASAN via LLVM, but prior to this patch, the flags aren't set properly. Suggested-by: Miguel Ojeda Signed-off-by: Matthew Maurer --- scripts/Makefile.kasan | 57 ++++++++++++++++++++++++--------- scripts/Makefile.lib | 3 ++ scripts/generate_rust_target.rs | 1 + 3 files changed, 45 insertions(+), 16 deletions(-) diff --git a/scripts/Makefile.kasan b/scripts/Makefile.kasan index aab4154af00a..97570df40a98 100644 --- a/scripts/Makefile.kasan +++ b/scripts/Makefile.kasan @@ -12,6 +12,11 @@ endif KASAN_SHADOW_OFFSET ?= $(CONFIG_KASAN_SHADOW_OFFSET) cc-param = $(call cc-option, -mllvm -$(1), $(call cc-option, --param $(1))) +rustc-param = $(call rustc-option, -Cllvm-args=-$(1),) + +check-args = $(foreach arg,$(2),$(call $(1),$(arg))) + +kasan_params := ifdef CONFIG_KASAN_STACK stack_enable := 1 @@ -41,39 +46,59 @@ CFLAGS_KASAN := $(call cc-option, -fsanitize=kernel-address \ $(call cc-option, -fsanitize=kernel-address \ -mllvm -asan-mapping-offset=$(KASAN_SHADOW_OFFSET))) -# Now, add other parameters enabled similarly in both GCC and Clang. -# As some of them are not supported by older compilers, use cc-param. -CFLAGS_KASAN += $(call cc-param,asan-instrumentation-with-call-threshold=$(call_threshold)) \ - $(call cc-param,asan-stack=$(stack_enable)) \ - $(call cc-param,asan-instrument-allocas=1) \ - $(call cc-param,asan-globals=1) +# The minimum supported `rustc` version has a minimum supported LLVM +# version late enough that we can assume support for -asan-mapping-offset. +RUSTFLAGS_KASAN := -Zsanitizer=kernel-address \ + -Zsanitizer-recover=kernel-address \ + -Cllvm-args=-asan-mapping-offset=$(KASAN_SHADOW_OFFSET) + +# Now, add other parameters enabled similarly in GCC, Clang, and rustc. +# As some of them are not supported by older compilers, these will be filtered +# through `cc-param` or `rust-param` as applicable. +kasan_params += asan-instrumentation-with-call-threshold=$(call_threshold) \ + asan-stack=$(stack_enable) \ + asan-instrument-allocas=1 \ + asan-globals=1 # Instrument memcpy/memset/memmove calls by using instrumented __asan_mem*() # instead. With compilers that don't support this option, compiler-inserted # memintrinsics won't be checked by KASAN on GENERIC_ENTRY architectures. -CFLAGS_KASAN += $(call cc-param,asan-kernel-mem-intrinsic-prefix=1) +kasan_params += asan-kernel-mem-intrinsic-prefix=1 endif # CONFIG_KASAN_GENERIC ifdef CONFIG_KASAN_SW_TAGS +CFLAGS_KASAN := -fsanitize=kernel-hwaddress + +# This sets flags that will enable KHWASAN once enabled in Rust. These will +# not work today, and is guarded against in dependencies for CONFIG_RUST. +RUSTFLAGS_KASAN := -Zsanitizer=kernel-hwaddress \ + -Zsanitizer-recover=kernel-hwaddress + ifdef CONFIG_KASAN_INLINE - instrumentation_flags := $(call cc-param,hwasan-mapping-offset=$(KASAN_SHADOW_OFFSET)) + kasan_params += hwasan-mapping-offset=$(KASAN_SHADOW_OFFSET) else - instrumentation_flags := $(call cc-param,hwasan-instrument-with-calls=1) + kasan_params += hwasan-instrument-with-calls=1 endif -CFLAGS_KASAN := -fsanitize=kernel-hwaddress \ - $(call cc-param,hwasan-instrument-stack=$(stack_enable)) \ - $(call cc-param,hwasan-use-short-granules=0) \ - $(call cc-param,hwasan-inline-all-checks=0) \ - $(instrumentation_flags) +kasan_params += hwasan-instrument-stack=$(stack_enable) \ + hwasan-use-short-granules=0 \ + hwasan-inline-all-checks=0 # Instrument memcpy/memset/memmove calls by using instrumented __hwasan_mem*(). ifeq ($(call clang-min-version, 150000)$(call gcc-min-version, 130000),y) - CFLAGS_KASAN += $(call cc-param,hwasan-kernel-mem-intrinsic-prefix=1) + kasan_params += hwasan-kernel-mem-intrinsic-prefix=1 endif endif # CONFIG_KASAN_SW_TAGS -export CFLAGS_KASAN CFLAGS_KASAN_NOSANITIZE +# Add all as-supported KASAN LLVM parameters requested by the configuration. +CFLAGS_KASAN += $(call check-args, cc-param, $(kasan_params)) + +ifdef CONFIG_RUST + # Avoid calling `rustc-param` unless Rust is enabled. + RUSTFLAGS_KASAN += $(call check-args, rustc-param, $(kasan_params)) +endif # CONFIG_RUST + +export CFLAGS_KASAN CFLAGS_KASAN_NOSANITIZE RUSTFLAGS_KASAN diff --git a/scripts/Makefile.lib b/scripts/Makefile.lib index 9f06f6aaf7fc..4a58636705e0 100644 --- a/scripts/Makefile.lib +++ b/scripts/Makefile.lib @@ -167,6 +167,9 @@ ifneq ($(CONFIG_KASAN_HW_TAGS),y) _c_flags += $(if $(patsubst n%,, \ $(KASAN_SANITIZE_$(target-stem).o)$(KASAN_SANITIZE)$(is-kernel-object)), \ $(CFLAGS_KASAN), $(CFLAGS_KASAN_NOSANITIZE)) +_rust_flags += $(if $(patsubst n%,, \ + $(KASAN_SANITIZE_$(target-stem).o)$(KASAN_SANITIZE)$(is-kernel-object)), \ + $(RUSTFLAGS_KASAN)) endif endif diff --git a/scripts/generate_rust_target.rs b/scripts/generate_rust_target.rs index ced405d35c5d..c24c2abd67db 100644 --- a/scripts/generate_rust_target.rs +++ b/scripts/generate_rust_target.rs @@ -192,6 +192,7 @@ fn main() { } ts.push("features", features); ts.push("llvm-target", "x86_64-linux-gnu"); + ts.push("supported-sanitizers", ["kernel-address"]); ts.push("target-pointer-width", "64"); } else if cfg.has("LOONGARCH") { panic!("loongarch uses the builtin rustc loongarch64-unknown-none-softfloat target");