From patchwork Thu Aug 1 13:35:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13750541 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7821D184549 for ; Thu, 1 Aug 2024 13:35:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722519347; cv=none; b=r8tjG/dcsg/T9FsVGX+/cL3lpKXS4F/OC5RATSL1LOPy4BCjD5yacYjkZyiSzAoXz86T9xSeRHw/M3L2GGfPUXnj3gQdiDmNH2I0QE+qJeY/Yki50f+LSI1tSDX2eMEBQdDpYXmGoxT21Shn24KAB3bxrojH7cv35E2Tz75f0TM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722519347; c=relaxed/simple; bh=9KJRJCmOWuRfdbZrnq/poLUu8bU4GlJiW3tPxynPLbU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=BSjORSYAMqF2O7aFA+Kr+TiULmZXYlmOReFiNxiI/ohqDR8YkB8TKa4oUmHXjrqMkM0ZfVzXTCiOnGof6mHsS8TrM7HGbBBmmfDOF2ZI7LsVI3Yrsj9xU3sZ7BAsnOKLCz/ViKOephGyruPpLuCrgBYVO22vJx26tMru9hX1SY0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=xd4KyOPw; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="xd4KyOPw" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e03a5534d58so9299443276.1 for ; Thu, 01 Aug 2024 06:35:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722519344; x=1723124144; darn=vger.kernel.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=uUq/S4/wDF6le5lpRBRt1bMVOTfqzg9tYxeqkmN+oyE=; b=xd4KyOPwWqlstBNPiQdTTq5+fnpzpn7AQU9F2lGfs3YoJe1B/7uL6eWSGrUIB3e0Yg PNDOkPu444HZKjxeUKyMKj9tVXqCJzN3PSZU23TiLXpKUqPHBYoWJYG10Xpv3p4TRY5D h+vW54IH+MODV7whzVYl9laKpvmVMmfiyRqJubNZ6qCjzjCOncwQdCneIJpt9MqdUA72 /S8hYktlJ8f3IQ88xui4JGMr8MUxn+coYjwbQUlTDgduF7PEZITHUVxtJ44Dbv87uwkl Izs2jRxH8qo1aZ9XsM9fNpuongk4rMjj72SIYYK9A6jogb077HdBZ+0TLgT/jKERID8V DPEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722519344; x=1723124144; 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=uUq/S4/wDF6le5lpRBRt1bMVOTfqzg9tYxeqkmN+oyE=; b=Tew/fJKUpBXx7rBT1Bh8hrsfI02CERF7pLNCPiAfYGSjYzypcXNDa6RLluBJBvEY2Z oNlhz2LVY1wtCO167+7fQoXrHJaLh8KTEhpMvEcTxGkTpU6suag5fqZ8eZsZRuWD1BgF xearFB8G4sK3OiYmWgxQUIKTvy1wo48uaUtM1ngnEPNsRiGz6po917/O6wgB0JLJIwit tg2zmSed5VmDiElFycPOJRYoh4RdMKdjYL0Z3LFbfjIGQa08xTgKWvz3sQ/H4HD4OWsU R9QF/bSYhd7dQVYOBO7dLmqWKgVT1p09YldtcbUu1ujmPvephAzt1DmK7CPH+Hvq/CH3 3yBQ== X-Forwarded-Encrypted: i=1; AJvYcCUiQ2GlOZ1vR8cCnZkXO5LGye7a+PvnGFEP7yzCQfHIZR0CV9rVpInzRbnOpsQhukNxP8TLoGcIC/F4ZC8O4DW8KVgXo0a5gqt5c9z3 X-Gm-Message-State: AOJu0YwDqIfk9JOj1+WyclkYkjLuyVbKfOZBO1bMvqgpCqFLn1zuNxWH PLM4npkaOC4pWZMnnlot7OJOkjqT7RScdh/s2kaaUVm/JtwMYkCKBC/c6qTjfiRO54qnkD37TBx xKOvrYhH5Lt1Aug== X-Google-Smtp-Source: AGHT+IGOulhKqtomG6+nQDZBOeOqQJ5BWk8oYgiic6zU4MGXtAG1WleW6kXftv8Uix8Iszrj2ormqe1kpJCpJ1k= X-Received: from aliceryhl.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:35bd]) (user=aliceryhl job=sendgmr) by 2002:a05:6902:1004:b0:e05:9a19:cc3c with SMTP id 3f1490d57ef6-e0bde421448mr135276.9.1722519344272; Thu, 01 Aug 2024 06:35:44 -0700 (PDT) Date: Thu, 01 Aug 2024 13:35:17 +0000 In-Reply-To: <20240801-kcfi-v2-0-c93caed3d121@google.com> Precedence: bulk X-Mailing-List: linux-kbuild@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240801-kcfi-v2-0-c93caed3d121@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=3370; i=aliceryhl@google.com; h=from:subject:message-id; bh=9KJRJCmOWuRfdbZrnq/poLUu8bU4GlJiW3tPxynPLbU=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBmq48qREXq217LC2vxsyrziCCz5TQrh0XV2LkeY eYY5oHdVA+JAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZquPKgAKCRAEWL7uWMY5 RvD/D/9hquQktFOsmVioJ1b7Ke3cyQFPFf5cGRdBYu31HVKKRForWtjcnVYHkhzkB/ho3FjnMMb RdBOYJsnOmbn8v4zRhLmsGt4cgXm6MUe4pPjALZIZrCBpqzsjTYp9zVd4GuMY690kIMvVMkPoLx FRJDJ4aAiSfoPKoSJSTXoN7VV+XA+39BT0izA9Butfb6xECntAINrmprDjfY5kHlP4aKJlGuvcR xJ5oMQEFpKGigqxOZfTjsEsygRaGGedn7/ExhieLbROaicbcuNTjgatiYKUTO4hUHduP0Iaax6J uX5aAtqCRvLBZmf8drE9wgpSRhOtPh8VwU3gb2YsfOFqTAhV9QCGwyJE7tI6BRrqwtIndR9lFgQ UG0/UNbpRNT9s3GRz7T9mqeA6eToxyclJNYTcD8x0jGFlKM+zOznZvsbBlBf9ZIN/g5d2TY/7mo ziWrT6B02wlHHyzrR9mYsNzNxUmD7VfduQkGFFEAo7GDpiZV+tKuUnLONtadxUVp+dGC6WpENjg k9YOAU7N8/8Hq9Vu6auu1ITmynKF3npWUSHss8mW5A6BvaXvHebjUU9+bp+0S8UevQtc4z3w/Is r7bhivw/fLMkBoJ6yxcx2xrdyzuACldRc7ddweQCDOlpoWCYw2dGp17Vdkn9Bs882WWyZlW+Q1v gOoqQiRPifA0NaA== X-Mailer: b4 0.13.0 Message-ID: <20240801-kcfi-v2-1-c93caed3d121@google.com> Subject: [PATCH v2 1/2] cfi: add CONFIG_CFI_ICALL_NORMALIZE_INTEGERS From: Alice Ryhl To: Masahiro Yamada , Nathan Chancellor , Nicolas Schier , Sami Tolvanen , Peter Zijlstra , Miguel Ojeda , Kees Cook Cc: Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , " =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= " , Benno Lossin , Andreas Hindborg , Matthew Maurer , Alice Ryhl , linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org Introduce a Kconfig option for enabling the experimental option to normalize integer types. This ensures that integer types of the same size and signedness are considered compatible by the Control Flow Integrity sanitizer. The security impact of this flag is minimal. When Sami Tolvanen looked into it, he found that integer normalization reduced the number of unique type hashes in the kernel by ~1%, which is acceptable. This option exists for compatibility with Rust, as C and Rust do not have the same set of integer types. There are cases where C has two different integer types of the same size and signedness, but Rust only has one integer type of that size and signedness. When Rust calls into C functions using such types in their signature, this results in CFI failures. One example is 'unsigned long long' and 'unsigned long' which are both 64-bit on LP64 targets, so on those targets this flag will give both types the same CFI tag. This flag changes the ABI heavily. It is not applied automatically when CONFIG_RUST is turned on to make sure that the CONFIG_RUST option does not change the ABI of C code. For example, some build may need to make other changes atomically with toggling this flag. Having it be a separate option makes it possible to first turn on normalized integer tags, and then later turn on CONFIG_RUST. Similarly, when turning on CONFIG_RUST in a build, you may need a few attempts where the RUST=y commit gets reverted a few times. It is inconvenient if reverting RUST=y also requires reverting the changes you made to support normalized integer tags. To avoid having this flag impact builds that don't care about this, the next patch in this series will make CONFIG_RUST turn on this option using `select` rather than `depends on`. Signed-off-by: Alice Ryhl --- Makefile | 3 +++ arch/Kconfig | 16 ++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/Makefile b/Makefile index 2b5f9f098b6f..484c6900337e 100644 --- a/Makefile +++ b/Makefile @@ -952,6 +952,9 @@ endif ifdef CONFIG_CFI_CLANG CC_FLAGS_CFI := -fsanitize=kcfi +ifdef CONFIG_CFI_ICALL_NORMALIZE_INTEGERS + CC_FLAGS_CFI += -fsanitize-cfi-icall-experimental-normalize-integers +endif KBUILD_CFLAGS += $(CC_FLAGS_CFI) export CC_FLAGS_CFI endif diff --git a/arch/Kconfig b/arch/Kconfig index 975dd22a2dbd..ee58df8b1080 100644 --- a/arch/Kconfig +++ b/arch/Kconfig @@ -826,6 +826,22 @@ config CFI_CLANG https://clang.llvm.org/docs/ControlFlowIntegrity.html +config CFI_ICALL_NORMALIZE_INTEGERS + bool "Normalize CFI tags for integers" + depends on CFI_CLANG + depends on $(cc-option,-fsanitize=kcfi -fsanitize-cfi-icall-experimental-normalize-integers) + help + This option normalizes the CFI tags for integer types so that all + integer types of the same size and signedness receive the same CFI + tag. + + The option is separate from CONFIG_RUST because it affects the ABI. + When working with build systems that care about the ABI, it is + convenient to be able to turn on this flag first, before Rust is + turned on. + + This option is necessary for using CFI with Rust. If unsure, say N. + config CFI_PERMISSIVE bool "Use CFI in permissive mode" depends on CFI_CLANG From patchwork Thu Aug 1 13:35:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13750542 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5DB4C1A3BDC for ; Thu, 1 Aug 2024 13:35:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722519350; cv=none; b=V0T4fTbmDXUuGM5nJa3HuBP2P90WIP6Z1Yab6+MbOsYqH7vEC4od884mznmzHK/17i+SRH0rLk6p3bSH/CCvZgSYRoG8ZGExP0uOdOlH6DuaKLn1rkdHnFcIdUNzZrR3njUgXnBxSVygzDylaRwl1pciSmZ/U6mhjySkA3jFxl8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722519350; c=relaxed/simple; bh=g6xRP03AEmw2AlXHGBnOhcCWHSV0TgtiDn2YP/jCB3s=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=bzgUDTwusQWvWP5ybXpe0quypDgJurCBDA6B4YUJ4SIttWXUxzv0YvuLi4kDQCUW3eIN9DPluTXBeyuk9pMvP3XTH5JO1gCCT77FAMpzGYUGbL//qyTj7UV+txl2CX5PGC5gKqcCciM3suVpoouAYbzFuuks4SOq9h9eyDKJDVc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=DQa8333d; arc=none smtp.client-ip=209.85.128.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="DQa8333d" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-42808efc688so44138215e9.0 for ; Thu, 01 Aug 2024 06:35:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722519347; x=1723124147; darn=vger.kernel.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=dbWi1FpZZ2yBy3Kz3tZjACI1vgk/mZg9/Nmg6pdDZdI=; b=DQa8333d6FIdfUQA2HAQrSZMDTB+zqlV06R9LUEap7Cise+u9Fpe/3meG6NKiZFyIL 4wEKQDrSoSE73aWb3oJQpEKE4JvDeJejjq7Pat/eLTlX76wHBlqRn4pDqzNlh9P77ES3 uWT8qquif2RG0jcbWOCWOh18V4P7YqxORcPGMnAEXR7vPQ+5rSb3wM3RI1C6MuHvrpeR HRrdFisutG+wTYk+r5uK/Zuq2eVs17itOjhmPnQZ1+iXpXeiq3m9cQmeqFV9lLqVT4Fz kcO41HqpSJBHmk537F2KGvZ438lUfnEZIolcEAiOJ33nG/CBWcEgTK3xWB9gceS1tbyk pdWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722519347; x=1723124147; 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=dbWi1FpZZ2yBy3Kz3tZjACI1vgk/mZg9/Nmg6pdDZdI=; b=Rohi9iB31PG3WXW010pamVDFEZ6gIRwsrRGXTtHXR0IIr4BgNfWCgS1LY+7V79s4Ka WoU6vFPGOE5Rv7na9+rwkmdgzkJTMs6vNch/9s600THq4uMT+J8sNiTGaUTkEiBk5nCr 1+C8UXmn/oFfFkjT1sdNXQacMpn/D8Nt8wUdIsulIPIyvmwsPzrjuUt7vwa6TcO0XtXh 5IJ26SX48wZJ1hkPuW0c/aEyn2PgUxEI1Ukz5HvqW/TOxjW3tNE8q7fTAY5vHvVJuwv1 KrdqU4KqTs6k/VHFfEzNQi5hGJlce3e9VuXkzryuNLYTK/Ga3iSw79DZjDtIj5ADNWNh oEVQ== X-Forwarded-Encrypted: i=1; AJvYcCWU0pwLL1k5ns9KPqY7jBuiRk3+O11MocutSQWS52rUVRWKFmxr7+W3BIlRMhhyalh90DSPm1bMbPqRlSi6Y7AGCL+hs+K51/3adURw X-Gm-Message-State: AOJu0Yw1LGsRdeveiOGplUXGC4i9VJbNPNTeiw6iW3e9R6C6QCEK/vAe iru1ax9h0F2V/dT3UWZcS96Htvpw3L499wiG77Dc1W5zieOkMEk74+gqHWcfnB8R5sRagonSVRp rp1zVCtPG7SbX9Q== X-Google-Smtp-Source: AGHT+IGxf1GtVyZHRl7HaK5uRR1WLEE3Ju2POgiwKW/BWFMsP5oUnpxl0GBV2NRkby6X2k8BnllkS0z/o2InLt4= X-Received: from aliceryhl.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:35bd]) (user=aliceryhl job=sendgmr) by 2002:a05:600c:3d86:b0:428:23c8:1e4f with SMTP id 5b1f17b1804b1-428bda66a81mr102455e9.7.1722519346740; Thu, 01 Aug 2024 06:35:46 -0700 (PDT) Date: Thu, 01 Aug 2024 13:35:18 +0000 In-Reply-To: <20240801-kcfi-v2-0-c93caed3d121@google.com> Precedence: bulk X-Mailing-List: linux-kbuild@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240801-kcfi-v2-0-c93caed3d121@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=4686; i=aliceryhl@google.com; h=from:subject:message-id; bh=+lqmUF30WYAoZ/BdGmYCbU1auClzkGJgWqTZCh5uhMk=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBmq48rQlPXbLqr6/0hL4XByzgKidSQ3xvfC85uK fZrkNxCgxOJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZquPKwAKCRAEWL7uWMY5 RpxBD/9NV1DoY4UTpSVocf3p/FMOxDkDA+CWazPg+TdFq6lZ6rzgjr7YnM4LK+tkJ2uRPyNKVH7 Gn1QjrEi15Z/RuhGKNTRG/Uin/KLOVWA/krnCJzYdujAATyST8rUZIV+vDkP0Ry3n/wLppqVy5c 2YkdEyJ5QG2A91Tz8aN3x2EaQnJciQ/1lPWcEyqeot/6grGuzCAM5F/UKksGK5Apwr0OUnIgSzQ 4Rh2gJ7CcgI4fYd70GYt0xfQ6nPnqs18I6wyx8m3tGiEq2oTDhOnPiF37+JfA3QMxgXtysjlBWz K55bJV7FGlLRJB2LyQdt57u52zh37/Nm0nxNNBOCcI6kKDfy1e8W5fxf1fuUKRM4w99M/rycXR5 4KdwuQDx2y3iBGDaesio6XfIZxzAJbvhr8axmu/zZ/KdDkomGcWiSqOhlQrGJwVYaV6kdKtDX6h QXW1xAtLVi1yFjwAF7A7MIsbxVv5SSEkmU+mmV+DI/avXNH/XD+aGD8/9pLVOe+ecBMso+epdh1 tMAEuC0+e5ECkkZYgj0C8lBHV42tH7Wo4zZVBkNEVZgfkQP92KMVIaVWhqZPcufrDTGOL8xiRj0 aFKxFYs5N7RWGLKn7URVcO9su5Yyg6VrU9zvwg2D74F5s7ukAft6cHYUG/fc+D8YP/SNzpwPe0f Ee/myO/r7R2knUQ== X-Mailer: b4 0.13.0 Message-ID: <20240801-kcfi-v2-2-c93caed3d121@google.com> Subject: [PATCH v2 2/2] rust: cfi: add support for CFI_CLANG with Rust From: Alice Ryhl To: Masahiro Yamada , Nathan Chancellor , Nicolas Schier , Sami Tolvanen , Peter Zijlstra , Miguel Ojeda , Kees Cook Cc: Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , " =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= " , Benno Lossin , Andreas Hindborg , Matthew Maurer , Alice Ryhl , linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org From: Matthew Maurer Make it possible to use the Control Flow Integrity (CFI) sanitizer when Rust is enabled. Enabling CFI with Rust requires that CFI is configured to normalize integer types so that all integer types of the same size and signedness are compatible under CFI. Rust and C use the same LLVM backend for code generation, so Rust KCFI is compatible with the KCFI used in the kernel for C. In the case of FineIBT, CFI also depends on -Zpatchable-function-entry for rewriting the function prolouge, so we set that flag for Rust as well. The flag for FineIBT requires rustc 1.80.0 or later, so include a Kconfig requirement for that. Enabling Rust will select CFI_ICALL_NORMALIZE_INTEGERS because the flag is required to use Rust with CFI. Using select rather than `depends on` avoids the case where Rust is not visible in menuconfig due to CFI_ICALL_NORMALIZE_INTEGERS not being enabled. One disadvantage of select is that RUST must `depends on` all of the things that CFI_ICALL_NORMALIZE_INTEGERS depends on to avoid invalid configurations. Alice has been using KCFI on her phone for several months, so it is reasonably well tested on arm64. Signed-off-by: Matthew Maurer Co-developed-by: Alice Ryhl Signed-off-by: Alice Ryhl --- Makefile | 7 +++++++ arch/x86/Makefile | 4 ++++ init/Kconfig | 4 +++- rust/Makefile | 2 +- scripts/generate_rust_target.rs | 1 + 5 files changed, 16 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 484c6900337e..2dc39a23005d 100644 --- a/Makefile +++ b/Makefile @@ -955,6 +955,13 @@ CC_FLAGS_CFI := -fsanitize=kcfi ifdef CONFIG_CFI_ICALL_NORMALIZE_INTEGERS CC_FLAGS_CFI += -fsanitize-cfi-icall-experimental-normalize-integers endif +ifdef CONFIG_RUST + # Always pass -Zsanitizer-cfi-normalize-integers as CONFIG_RUST selects + # CONFIG_CFI_ICALL_NORMALIZE_INTEGERS. + RUSTC_FLAGS_CFI := -Zsanitizer=kcfi -Zsanitizer-cfi-normalize-integers + KBUILD_RUSTFLAGS += $(RUSTC_FLAGS_CFI) + export RUSTC_FLAGS_CFI +endif KBUILD_CFLAGS += $(CC_FLAGS_CFI) export CC_FLAGS_CFI endif diff --git a/arch/x86/Makefile b/arch/x86/Makefile index 801fd85c3ef6..e9b2ee3c8a71 100644 --- a/arch/x86/Makefile +++ b/arch/x86/Makefile @@ -237,6 +237,10 @@ ifdef CONFIG_CALL_PADDING PADDING_CFLAGS := -fpatchable-function-entry=$(CONFIG_FUNCTION_PADDING_BYTES),$(CONFIG_FUNCTION_PADDING_BYTES) KBUILD_CFLAGS += $(PADDING_CFLAGS) export PADDING_CFLAGS + +PADDING_RUSTFLAGS := -Zpatchable-function-entry=$(CONFIG_FUNCTION_PADDING_BYTES),$(CONFIG_FUNCTION_PADDING_BYTES) +KBUILD_RUSTFLAGS += $(PADDING_RUSTFLAGS) +export PADDING_RUSTFLAGS endif KBUILD_LDFLAGS += -m elf_$(UTS_MACHINE) diff --git a/init/Kconfig b/init/Kconfig index b0238c4b6e79..306af56a22df 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -1905,11 +1905,13 @@ config RUST bool "Rust support" depends on HAVE_RUST depends on RUST_IS_AVAILABLE - depends on !CFI_CLANG depends on !MODVERSIONS depends on !GCC_PLUGINS depends on !RANDSTRUCT depends on !DEBUG_INFO_BTF || PAHOLE_HAS_LANG_EXCLUDE + depends on !CFI_CLANG || RUSTC_VERSION >= 107900 && $(cc-option,-fsanitize=kcfi -fsanitize-cfi-icall-experimental-normalize-integers) + select CFI_ICALL_NORMALIZE_INTEGERS if CFI_CLANG + depends on !FINEIBT || RUSTC_VERSION >= 108000 help Enables Rust support in the kernel. diff --git a/rust/Makefile b/rust/Makefile index f6b9bb946609..a2c9a3e03a23 100644 --- a/rust/Makefile +++ b/rust/Makefile @@ -305,7 +305,7 @@ $(obj)/bindings/bindings_helpers_generated.rs: $(src)/helpers.c FORCE quiet_cmd_exports = EXPORTS $@ cmd_exports = \ $(NM) -p --defined-only $< \ - | awk '/ (T|R|D) / {printf "EXPORT_SYMBOL_RUST_GPL(%s);\n",$$3}' > $@ + | awk '$$2~/(T|R|D)/ && $$3!~/__cfi/ {printf "EXPORT_SYMBOL_RUST_GPL(%s);\n",$$3}' > $@ $(obj)/exports_core_generated.h: $(obj)/core.o FORCE $(call if_changed,exports) diff --git a/scripts/generate_rust_target.rs b/scripts/generate_rust_target.rs index c31657380bf9..9b184099278a 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", ["kcfi"]); ts.push("target-pointer-width", "64"); } else if cfg.has("X86_32") { // This only works on UML, as i386 otherwise needs regparm support in rustc