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 BB743C3DA61 for ; Mon, 29 Jul 2024 14:44:10 +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:Cc:To:From: Subject:Message-ID:References:Mime-Version:In-Reply-To:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=vfulPrfSicc+WNyOjayW5unzLu2GM9o7n+2trJ/3FuM=; b=oXD1DfAqO1v8LiQrnNs7szc1/h pJEYEoHeyza0JbjiYOwdfH19Ntu5zDqdnjvQOwgrMXavA4uz71KoovyAWoLSpOZgsdCBS6IQBuILP HDEXwVJRlKM11xThFeO3+j08J8nYSkS6q+QvnJmwyXnITNZEtPP6ZqQX48utguNl7YFWNKchvSA0K TtdOYDVIbNvCb8xsimkSSWvYcYHKzaYSWEM5GxsWRF54jjhFK8B8sDkLC4OOmYNOZn7XueYurRZ4O glyAlHRYUJPrZTLy23yp4KrdYoOiS09lLQItlmkCwGSj7ze/ke+i7BEZcTPNztR9a+NynxaLPTAeP YwQHgtCw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sYRbV-0000000Bhub-3R2X; Mon, 29 Jul 2024 14:44:02 +0000 Received: from mail-yw1-x114a.google.com ([2607:f8b0:4864:20::114a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sYRHD-0000000BYTZ-1Hln for linux-arm-kernel@lists.infradead.org; Mon, 29 Jul 2024 14:23:04 +0000 Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-66890dbb7b8so65906207b3.0 for ; Mon, 29 Jul 2024 07:23:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722262981; x=1722867781; darn=lists.infradead.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=vfulPrfSicc+WNyOjayW5unzLu2GM9o7n+2trJ/3FuM=; b=KMkFZrluDV2tHhiw23gYMrgTiQtLSBi4LbvKfwtNzAirtOuGWhVUrVYr7BTiSIK0Gd drxrGnD+Z8KDHv6jyDuFazrhe7vpuDlAR/jGVD9u8+LTwFt4ZZdLkIlfs/oYPWTxwD7K 9t63cR2heOw+TdecZRMZThWstluFLgO4UFKGkMvjGn1ae+0PcWpxTApcs7WOO440JQii IzpOD69+6PcWgpXNZkRm5e8rqH/W1ebI49Ny9Cwd1EX5oCUZ4xFQ77QjdHsXhbcDGjic UIYa9yMcp+6Qh84CsppgQ+gkeecaW71mE/AebaOgit/6C6v0R5PR8o+gIEcHuDIineFf 3A0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722262981; x=1722867781; 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=vfulPrfSicc+WNyOjayW5unzLu2GM9o7n+2trJ/3FuM=; b=g1Y/8PbxHHwzk/cYTlQq+CwgE/7RMNWDKGhLZvwTqoNn2daGewOnSPRTg5aL5AlH5i xVnUQDwRishZE62+mEbJMWvwLdFd/bTYlOev+7KjYCKlZXC6ZHFLp7RYE8LzrK6pF+CI fRNpC+5NOiYcaYvsSMZHdNPxV+aSWaO5n5zJ2AfoteXLRqf/ssBSBHdy/lK4KbZ7a3ef VI452FScfpzXJbBszFRGKlsg6suOqfOZg/DcOqk21l7oaM/04QlPx3d84dunTbptoeM/ yf7/6M5Xv+JTjpoJkVOjF23SNHsNK0Ag63rYrtxFuaoieenK9t3QkwTjcKy3LPqJqYs3 K67Q== X-Forwarded-Encrypted: i=1; AJvYcCUU3f4juiMDKOgnpnaPSfuphCvjozDdzWJDinIg0w9mqJQFHfzKWugSwpPrz0rkCkoJmNI6Prc0UVxdfcFdeosirjsIHf4HgGjuarE+F8h/HNCO+zo= X-Gm-Message-State: AOJu0YxM3XSDXtBuxiydwUcc/BLYuFquVoR2cF/dV4h5RXQiWJksqSRA wWGR2guNjxFforlHmem3ovJs5+ZBQ6CCkSPFkyRCyU1aAcguSZMb57Zu1jY8UpMc3uAFmD1Tv18 Ctk6AZ9xKcgMNVA== X-Google-Smtp-Source: AGHT+IG1bISzjta4IGanTLnwN52d5QDMD5IDSQZWzKIOirqZWoAEkezelFYmM4/kNamX3YXHmq9SDd5H7LEll/E= X-Received: from aliceryhl2.c.googlers.com ([fda3:e722:ac3:cc00:68:949d:c0a8:572]) (user=aliceryhl job=sendgmr) by 2002:a81:b049:0:b0:669:f244:2ae5 with SMTP id 00721157ae682-67a0625130amr1706417b3.3.1722262981080; Mon, 29 Jul 2024 07:23:01 -0700 (PDT) Date: Mon, 29 Jul 2024 14:22:50 +0000 In-Reply-To: <20240729-shadow-call-stack-v4-0-2a664b082ea4@google.com> Mime-Version: 1.0 References: <20240729-shadow-call-stack-v4-0-2a664b082ea4@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=3399; i=aliceryhl@google.com; h=from:subject:message-id; bh=RX0RqNRyzzyoW/MQJOHkbfIcM2eu3kixAI50jtAtAag=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBmp6W7wE19VEoXfd9d7Bo4mIIcb77rX+tm389IC TcKknj6Ye+JAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZqeluwAKCRAEWL7uWMY5 RlJJD/4zoQUSAdVgq/uz58fBHA6iQGG/4ockLdhyAueNkNmadzLCyF3t5un1s4DMvdqLDICjJ47 GYg45pLRUiYN0OWG9rrx2S/a+EYStltg9aOvqtLgesvSUUQk2SHs7fpKMn2+61Yo56qFPSl26ju cIeGWunfkKTkqLjZ2Xv4szXNPTRlkGbBRWIXnzKWgRxypvl+cyir6qeDXivzfzrJkt9/gYrvCnR UzFjwXgAyzjZCxA9elABsZJ5N1Xnzmr7lJ7DhZtFqlaYbYPGm7mDTMa0LqM7wJvdkLGZ6kveNMD FAqqcMmk1T2+R04mdloAaRlKgHNJmSBJNtCxwPEnsVDKAJxFVlWJ3yZeIN0vB+h5uSoqWRaL1y6 GywKJabv0aPZxJw6uN+BDNUtJuEFp9+XLwOYGbVwSLyOYTuWQIJCFFSyTcLh65ky69IDa2tWUDX XJtQVnrXJ8wZDPkU3fhRnVM7r/qUmGI34eMdb/wo4oVyvnYELUf8URPR/xkjfgWUXamVrK8ETSH 2cH+V2Q966dMhrfSS3repxrr0I9rlb5A/Cl1EoLJTwSVzkmVOZLSsrZoHnsscBvvHY30W54QgsX E/91A1f6yI4iAfciIB4xrpegIOF7hZKp8xbyCBpewqmR0Np4K+GMNryX14r29XQA/rvwQEqcg5X NNBGNzVRCXiJ+Zg== X-Mailer: b4 0.13-dev-26615 Message-ID: <20240729-shadow-call-stack-v4-2-2a664b082ea4@google.com> Subject: [PATCH v4 2/2] rust: support for shadow call stack sanitizer From: Alice Ryhl To: Catalin Marinas , Will Deacon , Jamie Cunliffe , Sami Tolvanen , Nathan Chancellor , Conor Dooley Cc: Masahiro Yamada , Nicolas Schier , Ard Biesheuvel , Marc Zyngier , Mark Rutland , Mark Brown , Nick Desaulniers , Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg , Valentin Obst , linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, rust-for-linux@vger.kernel.org, Alice Ryhl , Kees Cook Content-Type: text/plain; charset="utf-8" X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240729_072303_386496_4907A204 X-CRM114-Status: GOOD ( 19.45 ) X-BeenThere: linux-arm-kernel@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-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org To use the shadow call stack sanitizer, you must pass special flags: * On arm64, you must pass -ffixed-x18 to your compiler. * On riscv, you must pass --no-relax-gp to your linker. These requirements also apply to Rust code. When using Rust on arm64, you must pass the -Zfixed-x18 flag to rustc, which has the same effect as the -ffixed-x18 flag does for C code. The -Zfixed-x18 flag requires rustc version 1.80.0 or greater. There is no need to pass any flags to rustc on riscv as only the linker requires additional flags on this platform. On older versions of Rust, it is still possible to use shadow call stack by passing -Ctarget-feature=+reserve-x18 instead of -Zfixed-x18. However, this flag emits a warning during the build, so this patch does not add support for using it. Currently, the compiler thinks that the aarch64-unknown-none target doesn't support -Zsanitizer=shadow-call-stack, so the build will fail if you enable shadow call stack in non-dynamic mode. See [1] for the relevant feature request. To avoid this compilation failure, Kconfig is set up to reject such configurations. The `depends on` clause is placed on `config RUST` to avoid a situation where enabling Rust silently turns off the sanitizer. Instead, turning on the sanitizer results in Rust being disabled. We generally do not want changes to CONFIG_RUST to result in any mitigations being changed or turned off. Link: https://github.com/rust-lang/rust/issues/121972 [1] Signed-off-by: Alice Ryhl --- Makefile | 1 + arch/arm64/Makefile | 3 +++ init/Kconfig | 2 +- 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 2b5f9f098b6f..66daca7a9b57 100644 --- a/Makefile +++ b/Makefile @@ -928,6 +928,7 @@ ifdef CONFIG_SHADOW_CALL_STACK ifndef CONFIG_DYNAMIC_SCS CC_FLAGS_SCS := -fsanitize=shadow-call-stack KBUILD_CFLAGS += $(CC_FLAGS_SCS) +KBUILD_RUSTFLAGS += -Zsanitizer=shadow-call-stack endif export CC_FLAGS_SCS endif diff --git a/arch/arm64/Makefile b/arch/arm64/Makefile index f6bc3da1ef11..b058c4803efb 100644 --- a/arch/arm64/Makefile +++ b/arch/arm64/Makefile @@ -57,9 +57,11 @@ KBUILD_AFLAGS += $(call cc-option,-mabi=lp64) ifneq ($(CONFIG_UNWIND_TABLES),y) KBUILD_CFLAGS += -fno-asynchronous-unwind-tables -fno-unwind-tables KBUILD_AFLAGS += -fno-asynchronous-unwind-tables -fno-unwind-tables +KBUILD_RUSTFLAGS += -Cforce-unwind-tables=n else KBUILD_CFLAGS += -fasynchronous-unwind-tables KBUILD_AFLAGS += -fasynchronous-unwind-tables +KBUILD_RUSTFLAGS += -Cforce-unwind-tables=y -Zuse-sync-unwind=n endif ifeq ($(CONFIG_STACKPROTECTOR_PER_TASK),y) @@ -114,6 +116,7 @@ endif ifeq ($(CONFIG_SHADOW_CALL_STACK), y) KBUILD_CFLAGS += -ffixed-x18 +KBUILD_RUSTFLAGS += -Zfixed-x18 endif ifeq ($(CONFIG_CPU_BIG_ENDIAN), y) diff --git a/init/Kconfig b/init/Kconfig index 914edf51b068..103957466cee 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -1909,7 +1909,7 @@ config RUST depends on !MODVERSIONS depends on !GCC_PLUGINS depends on !RANDSTRUCT - depends on !SHADOW_CALL_STACK + depends on !SHADOW_CALL_STACK || RUSTC_VERSION >= 108000 && UNWIND_PATCH_PAC_INTO_SCS depends on !DEBUG_INFO_BTF || PAHOLE_HAS_LANG_EXCLUDE help Enables Rust support in the kernel. -- 2.46.0.rc1.232.g9752f9e123-goog