From nobody Wed Dec 17 18:59:04 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8E71FC77B72 for ; Mon, 17 Apr 2023 21:54:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230453AbjDQVyT (ORCPT ); Mon, 17 Apr 2023 17:54:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48594 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230242AbjDQVyP (ORCPT ); Mon, 17 Apr 2023 17:54:15 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CE361213D for ; Mon, 17 Apr 2023 14:54:14 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id v200-20020a252fd1000000b00b8f548a72bbso12042959ybv.9 for ; Mon, 17 Apr 2023 14:54:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1681768454; x=1684360454; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=1ROFbMOV6yOo0pF0esK8s0D/4X+P3zglkAv5TCX1cqs=; b=Pz9Wmj1CrPl1IL4L95VxcDW5MmG+o1YPeNyX6BDBGEP92+EkCITMnKQ7UvdtMPMQrW 5bYV8UujE5AJG6UrgZbcInh+uPT0vISasCoVTa9GdHTMvh7SiOQT0tBNXckQ7dxh9qL6 MDsM1qiT6eXbhT/g07gKJB/FHU4Z2NOE55ctur65xeJEowmDoi4VUxVBK8MeN9wKgmlQ +QtduWbRt7SYN5f4lKm6WYiBh7HlmwIs5OOF8pSxz6DzkhLpLFxOCeKx8e03x9wv8S4K /q0vzu5/DOn9g1FiqjN0UvWzgQZgBvjCwjFiN/YUGv30+JQXuxzHBbyEmpQY4Z9u+myL 5Rvw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681768454; x=1684360454; 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=1ROFbMOV6yOo0pF0esK8s0D/4X+P3zglkAv5TCX1cqs=; b=Amf+0L1vyLZieU1SJv/J/odorXeGVA7lXV7NMithOOj6z41ILZUhY14TxJTMk3IG3A mK0rx2cZD3auIfXiMOHr7++Y3BbrYd0O3/wA60b4M+dT0q1amsmjc4kcl/A/73joPA3X R93+G4ovF9pfLPB6owUFjU1i9jnadwQ0nVNwgAi605biIyo3JzElqLNOpgbHFrrJxOhG GTdMySoO6VvSd2rm8G80TidOIy6f1XZ/1yPiXZo850KFSshRTxMFoB3H04MHCCep3qVO DlcLEE+JgU2YIR5R+YufIW5iaXPId+BfqkYG+eFlJ4x4utw6RsOGOSCxMwmgRlIyUUjZ eCGA== X-Gm-Message-State: AAQBX9fqYqT5WvY0+nQOd174l1rveNl/qGrZLDoD+XxhFuec5Svam2EU 2skWHhSB1rTBO3n0qagBzjFFCAVl99I6k50sjzA= X-Google-Smtp-Source: AKy350b/U+vhtgW/aCNjn1RnkgKz7XdrVLLO1qkpANynkUXFsyTRebUkNcTaVWm1RgD2tS3IBkoN051hd0ctcGynTIc= X-Received: from ndesaulniers-desktop.svl.corp.google.com ([2620:15c:2d1:203:e8f1:df30:2fb2:cf8a]) (user=ndesaulniers job=sendgmr) by 2002:a25:cc05:0:b0:b8b:eea7:525a with SMTP id l5-20020a25cc05000000b00b8beea7525amr8187707ybf.7.1681768454118; Mon, 17 Apr 2023 14:54:14 -0700 (PDT) Date: Mon, 17 Apr 2023 14:54:07 -0700 In-Reply-To: <20230412-no_stackp-v1-0-86d2034a4d06@google.com> Mime-Version: 1.0 References: <20230412-no_stackp-v1-0-86d2034a4d06@google.com> X-Developer-Key: i=ndesaulniers@google.com; a=ed25519; pk=UIrHvErwpgNbhCkRZAYSX0CFd/XFEwqX3D0xqtqjNug= X-Developer-Signature: v=1; a=ed25519-sha256; t=1681768450; l=3068; i=ndesaulniers@google.com; s=20220923; h=from:subject:message-id; bh=Y+mVeYQgxmjVxZ8TAgHiUAti6DIYAGqhexs7SrW7nh8=; b=Xv2k6tZ4Tz1DoWi+BSx+JfcGKmmndDJLVOOT4V3InkpHqs2Txr0BldqG6172TGpaJXU4waGOC7cP UJyi1aBABQQ+iDuRy15lwJ7Wtd0/eqT1u+ZHYnEmGlM+mVeB9tBI X-Mailer: b4 0.12.2 Message-ID: <20230412-no_stackp-v1-1-86d2034a4d06@google.com> Subject: [PATCH 1/2] start_kernel: add no_stack_protector fn attr From: ndesaulniers@google.com To: "Borislav Petkov (AMD)" Cc: Peter Zijlstra , Josh Poimboeuf , x86@kernel.org, Michael Ellerman , Nicholas Piggin , Christophe Leroy , Miguel Ojeda , Nathan Chancellor , Tom Rix , linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev, Nick Desaulniers Content-Type: text/plain; charset="utf-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Back during the discussion of commit a9a3ed1eff36 ("x86: Fix early boot crash on gcc-10, third try") we discussed the need for a function attribute to control the omission of stack protectors on a per-function basis; at the time Clang had support for no_stack_protector but GCC did not. This was fixed in gcc-11. Now that the function attribute is available, let's start using it. Callers of boot_init_stack_canary need to use this function attribute unless they're compiled with -fno-stack-protector, otherwise the canary stored in the stack slot of the caller will differ upon the call to boot_init_stack_canary. This will lead to a call to __stack_chk_fail then panic. Link: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D94722 Link: https://lore.kernel.org/all/20200316130414.GC12561@hirez.programming.= kicks-ass.net/ Tested-by: Nathan Chancellor Acked-by: Michael Ellerman (powerpc) Acked-by: Miguel Ojeda Acked-by: Peter Zijlstra (Intel) Signed-off-by: Nick Desaulniers --- arch/powerpc/kernel/smp.c | 1 + include/linux/compiler_attributes.h | 12 ++++++++++++ init/main.c | 3 ++- 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/arch/powerpc/kernel/smp.c b/arch/powerpc/kernel/smp.c index f62e5e651bcd..48acae0da034 100644 --- a/arch/powerpc/kernel/smp.c +++ b/arch/powerpc/kernel/smp.c @@ -1603,6 +1603,7 @@ static void add_cpu_to_masks(int cpu) } =20 /* Activate a secondary processor. */ +__no_stack_protector void start_secondary(void *unused) { unsigned int cpu =3D raw_smp_processor_id(); diff --git a/include/linux/compiler_attributes.h b/include/linux/compiler_a= ttributes.h index e659cb6fded3..84864767a56a 100644 --- a/include/linux/compiler_attributes.h +++ b/include/linux/compiler_attributes.h @@ -255,6 +255,18 @@ */ #define __noreturn __attribute__((__noreturn__)) =20 +/* + * Optional: only supported since GCC >=3D 11.1, clang >=3D 7.0. + * + * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.ht= ml#index-no_005fstack_005fprotector-function-attribute + * clang: https://clang.llvm.org/docs/AttributeReference.html#no-stack-p= rotector-safebuffers + */ +#if __has_attribute(__no_stack_protector__) +# define __no_stack_protector __attribute__((__no_stack_protector__)) +#else +# define __no_stack_protector +#endif + /* * Optional: not supported by gcc. * diff --git a/init/main.c b/init/main.c index 5d6365510173..1265c8d11052 100644 --- a/init/main.c +++ b/init/main.c @@ -941,7 +941,8 @@ static void __init print_unknown_bootoptions(void) memblock_free(unknown_options, len); } =20 -asmlinkage __visible void __init __no_sanitize_address __noreturn start_ke= rnel(void) +asmlinkage __visible __init __no_sanitize_address __noreturn __no_stack_pr= otector +void start_kernel(void) { char *command_line; char *after_dashes; --=20 2.40.0.634.g4ca3ef3211-goog From nobody Wed Dec 17 18:59:04 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 57E23C77B76 for ; Mon, 17 Apr 2023 21:54:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230471AbjDQVyW (ORCPT ); Mon, 17 Apr 2023 17:54:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48622 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230428AbjDQVyS (ORCPT ); Mon, 17 Apr 2023 17:54:18 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C493E213D for ; Mon, 17 Apr 2023 14:54:16 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-54f8e31155bso146618157b3.11 for ; Mon, 17 Apr 2023 14:54:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1681768456; x=1684360456; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=TqlZpSOgvA1JLW3vzN7VqdCnxfAnggXybW0yzcDiFj0=; b=6Ok0UESUh26svpEt209uFygSyHBhPKwjJP2i5VUax6EplbX8ZR4r9D+qJgxeekR4XK 6Dh99p7UoZMrY3HwK2wWp4r3Bw6dkbzaFd9ThydtwbIN2I3+eDfYB5ErJoEPmfqghLf0 rwtmeBkQ+ySc7U5Zs5VZCi/MQrPDjJ84A1VrKMUWBhaBy1lMeMxtLLmbL0QV1WOG1GQf cyZNgAai2c6reZrcZzBnC0h2wPIRGVQPX0MliftBNYVAYJnVgnjziz8xTWOMY7e73FNW zL8UYzWheabjgDuIuJb6aSMdJR5XSkg3iac6BI0BIPTPysgkzR9ci86tf45CX2FT0U0x mfTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681768456; x=1684360456; 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=TqlZpSOgvA1JLW3vzN7VqdCnxfAnggXybW0yzcDiFj0=; b=dSWci9+aTE7iZ3HbmQ6sfb4IdlVl4Lp50Diti8NMXYd2OqP2CyR+eQ7w7NqQeTaBDk 3QJbsLi78S/K7O53RYh8YgUMCDfd7zMVYr1DzGiCq5up4Jwm9TdOVEV3L+c65P9csuS7 CMwKYc2tKygo/1XsGcyeGN+NcFXfwrSjswPiN/y6Ibrc9HG/2Vc4dNhLqM88Dea8t7ux 4llW0UkWkN2HK21ZXhtC3Vyhg5iOyrUcTnAdc2dH0+h8/X148QfaHSbMdXAPAwxNFBnU n3Vc+9NWWGKTOfyoRCx0UGvB7zef0durlF3/Fp1cEYGRuO8v4c4J6naDISDRVeeNe9+Q tJLA== X-Gm-Message-State: AAQBX9fyX2ps+T/GHABW21Doc3chwb727rUdlc7dBygLfkVvOWhqasMX qHm7lD+k2lWO+07MMU33vJMEG/57Rt1GxRhV5kA= X-Google-Smtp-Source: AKy350ba6ZSdzfrXwGQ5fmWTSwEW508zmjFzpnm3zgJ1VAgvkc67q/5rJe5ITrn8QQW6az8rdP0cUFYN1wNh9cRxrW0= X-Received: from ndesaulniers-desktop.svl.corp.google.com ([2620:15c:2d1:203:e8f1:df30:2fb2:cf8a]) (user=ndesaulniers job=sendgmr) by 2002:a25:df41:0:b0:b8b:eea7:525e with SMTP id w62-20020a25df41000000b00b8beea7525emr10639174ybg.5.1681768455963; Mon, 17 Apr 2023 14:54:15 -0700 (PDT) Date: Mon, 17 Apr 2023 14:54:08 -0700 In-Reply-To: <20230412-no_stackp-v1-0-86d2034a4d06@google.com> Mime-Version: 1.0 References: <20230412-no_stackp-v1-0-86d2034a4d06@google.com> X-Developer-Key: i=ndesaulniers@google.com; a=ed25519; pk=UIrHvErwpgNbhCkRZAYSX0CFd/XFEwqX3D0xqtqjNug= X-Developer-Signature: v=1; a=ed25519-sha256; t=1681768450; l=1232; i=ndesaulniers@google.com; s=20220923; h=from:subject:message-id; bh=on2xOURJ9HOGHwPlBJaraE3eCNVECjunAe4jXLZvvs4=; b=BdjqNu+UYyKvhEjP5f6/uO90m0HacK6S31UoWPhMwomn/HE4zn7R//ZaCa1pvzN25cywrCbexalz 5UZfSBkAD7ZLc+b4TJLKllZ9RD0Vlu6PiiUP8nAw6AhebQv03M0f X-Mailer: b4 0.12.2 Message-ID: <20230412-no_stackp-v1-2-86d2034a4d06@google.com> Subject: [PATCH 2/2] start_kernel: omit prevent_tail_call_optimization for newer toolchains From: ndesaulniers@google.com To: "Borislav Petkov (AMD)" Cc: Peter Zijlstra , Josh Poimboeuf , x86@kernel.org, Michael Ellerman , Nicholas Piggin , Christophe Leroy , Miguel Ojeda , Nathan Chancellor , Tom Rix , linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev, Nick Desaulniers Content-Type: text/plain; charset="utf-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable prevent_tail_call_optimization was added in commit a9a3ed1eff36 ("x86: Fix early boot crash on gcc-10, third try") to work around stack canaries getting inserted into functions that would initialize the stack canary in the first place. Now that we have no_stack_protector function attribute (gcc-11+, clang-7+) and use it on start_kernel, remove the call to prevent_tail_call_optimization such that we may one day remove it outright. Reviewed-by: Nathan Chancellor Acked-by: Peter Zijlstra (Intel) Signed-off-by: Nick Desaulniers --- init/main.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/init/main.c b/init/main.c index 1265c8d11052..c6eef497c8c9 100644 --- a/init/main.c +++ b/init/main.c @@ -1152,7 +1152,13 @@ void start_kernel(void) /* Do the rest non-__init'ed, we're now alive */ arch_call_rest_init(); =20 + /* + * Avoid stack canaries in callers of boot_init_stack_canary for gcc-10 + * and older. + */ +#if !__has_attribute(__no_stack_protector__) prevent_tail_call_optimization(); +#endif } =20 /* Call all constructor functions linked into the kernel. */ --=20 2.40.0.634.g4ca3ef3211-goog