From patchwork Mon Aug 19 21:35:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Maurer X-Patchwork-Id: 13769022 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 0ADE9C52D7C for ; Mon, 19 Aug 2024 21:35:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 934F76B0088; Mon, 19 Aug 2024 17:35:49 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8E5086B0089; Mon, 19 Aug 2024 17:35:49 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 75FC56B008A; Mon, 19 Aug 2024 17:35:49 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 5709B6B0088 for ; Mon, 19 Aug 2024 17:35:49 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id C8AB641513 for ; Mon, 19 Aug 2024 21:35:48 +0000 (UTC) X-FDA: 82470302376.03.93B5041 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf05.hostedemail.com (Postfix) with ESMTP id EA5F310000D for ; Mon, 19 Aug 2024 21:35:46 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=tYjfzAW3; spf=pass (imf05.hostedemail.com: domain of 3sbrDZgcKCHIccQkhUhWeeWbU.SecbYdkn-ccalQSa.ehW@flex--mmaurer.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3sbrDZgcKCHIccQkhUhWeeWbU.SecbYdkn-ccalQSa.ehW@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=1724103268; 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=aFJ5TYFEs+yR5IdjkZAHXbLg1gl48k9ppZEG1vmVjrs=; b=xLD4S71tmJCJW5VLl9Fr6EULYyJNVqHg0IIkD2ILvs1AZmx1crghM+11Y/+KVhP0S7sXoi 1wFYLlVGalQm6hRjwAeO9RTurgJtlsAfqBCf9BEGMkAG3wnO/H3c2Q33DqKFuO2xQuAD6m cM1mnPVJfCD26/vno1iNRSZMEZlFDaA= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724103268; a=rsa-sha256; cv=none; b=nVItnb/1MCraACcO6KazpicxQZmf8BClNMVzXjS+HbafmwjCyb+9oBA9N/mQHHMBPshIMh 10AZ+yrc7yuAPsPVtjTKXVWadbqq7HgP5cRDQhXXavSOAC7wy6SYz5oRE8OhLlHgs/XP1D Av8ylOauhMf8arEVcx2LtJRHyLu/J28= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=tYjfzAW3; spf=pass (imf05.hostedemail.com: domain of 3sbrDZgcKCHIccQkhUhWeeWbU.SecbYdkn-ccalQSa.ehW@flex--mmaurer.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3sbrDZgcKCHIccQkhUhWeeWbU.SecbYdkn-ccalQSa.ehW@flex--mmaurer.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e1159fb161fso6897805276.1 for ; Mon, 19 Aug 2024 14:35:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1724103346; x=1724708146; 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=aFJ5TYFEs+yR5IdjkZAHXbLg1gl48k9ppZEG1vmVjrs=; b=tYjfzAW39xORD4XsKDiz5V9v4FgCBKVlxQlvY7brR01lmVTtL47dnNDosVmnyeSfRK fNr0Fk9j8bTYYvZuvrkj7ewsI+fm0CvcTUcHYyy5vp+pDT2SzwyjOvJGfIovk25wvq55 JGbEAqcnTOyFUkUQti4wU/19/p7qysFKqM8fKz9DGJ2Iiq6IqvnCLCLafQiZYhV0qo1c UBILc3eXOjAmQ42HFG++B3CgBk0y1UHUiB8tiKyhg4knMiLHQvfQmxI1r0+YqBT9p+yP 5q2JVYE74oJxS6WpmMYc9oAFv01Dpcg/aiUOFLaEr4hDe5PDKNIxJ4TnNxsTeZlujEHg BLKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724103346; x=1724708146; 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=aFJ5TYFEs+yR5IdjkZAHXbLg1gl48k9ppZEG1vmVjrs=; b=w17i2MH50bghyNgcMkJPkFxT/mU5p60jlQeqMPfzVCmmtGRzcEJMuJuqyBnYPagsv1 zZ3bBvbWXA2TOw1cYEf0LON0XP2S7Wb1bl6dWLJLOEjo0MHPXe829ECCIl8Sn/CJ8nbT 51BTTSKXK5OghpK9z4Mj/aEmkDrRdYDoBknkdAFvBdU0gNZrLBfWAWvQKppiLIDMMa95 h42Rdn5TFoliDusadRJ5UqreAOy5bSVW58cX+JDt9FMroiiy6N9j+JwjzmWRA+ZzitBn IVyzuHPy4QoTuWw0moRBahHI2ywIRC76eGYS+hEDzb6EpiBYvAniSwDHLdHOxr/ItU5y OpnQ== X-Forwarded-Encrypted: i=1; AJvYcCWv1b7GzecCTZUh6ZPG5x/GdHAHKQbCs6pq/8KMRzLxCi7Q3cqCYS9OfNNwgDPryC6ZYuEsO5f0ig==@kvack.org X-Gm-Message-State: AOJu0YxLnE4AuXcFOrZLeM9ICckcbcu1O45comDKv1qHF8eMNFkyYpL2 MALIKTNkHw31SCAxgFNJi5bnJXmLOhIHhfpCQv6W+3P95gz+Q91qkZrkn2Sn+eeATh2Imq/tDzj o0KScCA== X-Google-Smtp-Source: AGHT+IG2tKNbtNYlkOTrRuz31J4vbeiBAaW0c0yUYXPPm8A2joe/19hudxJlIbHojlpceaLwmhCZjbn/6/KR X-Received: from anyblade.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1791]) (user=mmaurer job=sendgmr) by 2002:a5b:4cb:0:b0:e11:7a38:8883 with SMTP id 3f1490d57ef6-e1180f71f9dmr21204276.7.1724103345780; Mon, 19 Aug 2024 14:35:45 -0700 (PDT) Date: Mon, 19 Aug 2024 21:35:20 +0000 In-Reply-To: <20240819213534.4080408-1-mmaurer@google.com> Mime-Version: 1.0 References: <20240819213534.4080408-1-mmaurer@google.com> X-Mailer: git-send-email 2.46.0.184.g6999bdac58-goog Message-ID: <20240819213534.4080408-3-mmaurer@google.com> Subject: [PATCH v3 2/4] kbuild: rust: Enable KASAN support From: Matthew Maurer To: dvyukov@google.com, ojeda@kernel.org, andreyknvl@gmail.com, Andrey Ryabinin , Masahiro Yamada , Alex Gaynor , Wedson Almeida Filho , Nathan Chancellor Cc: aliceryhl@google.com, samitolvanen@google.com, kasan-dev@googlegroups.com, linux-mm@kvack.org, glider@google.com, Matthew Maurer , Nicolas Schier , Vincenzo Frascino , 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: pxbyhjmf79uso7wzcbkxxw33jxxp3w68 X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ZR8LKXIvGkNEqEk6qy9Uy246kQ1AnBmv8dRe6vRvq78GGvuSo1QGLeX61j+v+OMCqP2DdzGGbk3h7abmS47i+KkV4fvhLT3EmoXVh8+ek775V2AbsrYHVe30WoW2AjMHrBeNxHg1kskRUA+lNOfLhKCKKQ0rJFZiLcXgqeVvbWjl+EzCUAVJ1JVWo+6dn6W1glibkF/UkQocgpicEKOlv+iFK4YH9ffHc5we4u9PM0DRqPCAPuSqmYLqFZiVXi0IJt/ho6YzyRtlLVPX0Wjjw05bOMLmDXRSD84N68wrx8RQ+5Q9tgFiXdTf6OhR0DLrrqNAK8dN+6BkJggZB/z99xhqDW0vUCEamJRkSJb8CGg8oGE/DR4LSg+1tQ/OopLikIdNwrqSAYUValDMNil8THqwkMvaTHidf9OtPFtgKidNKwSrYYI42qwYfdJNU4I9HBGAtbbsQg/xzKuxyBB0/4H86nBIduCWx/MFa+yjC8UdLdEQL9zNpb6morBf6tTwZYRlxqdnd80Xmweka1OH3LHLT8IYI5y7J6ny/Yg0GCptAvHhsCKA3rBT8Dj0fEAixbdEtFJqrzyaVr53mkXlsgtsnzzz++FY2aR9lnIgEm+7Oj1vQcjOEI7s6P2pHGLjUGrQeocMOdcBw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.003277, 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. Rust hasn't yet enabled software-tagged KWHASAN (only regular HWASAN), so explicitly prevent Rust from being selected when it is enabled. Suggested-by: Miguel Ojeda Signed-off-by: Matthew Maurer --- scripts/Makefile.kasan | 54 +++++++++++++++++++++++---------- scripts/Makefile.lib | 3 ++ scripts/generate_rust_target.rs | 1 + 3 files changed, 42 insertions(+), 16 deletions(-) diff --git a/scripts/Makefile.kasan b/scripts/Makefile.kasan index aab4154af00a..163640fdefa0 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,56 @@ 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 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 + +CFLAGS_KASAN := -fsanitize=kernel-hwaddress +RUSTFLAGS_KASAN := -Zsanitizer=kernel-hwaddress \ + -Zsanitizer-recover=kernel-hwaddress # 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");