Linux Hardening
 help / color / mirror / Atom feed
From: Dan Li <ashimida@linux.alibaba.com>
To: Ard Biesheuvel <ardb@kernel.org>, linux-arm-kernel@lists.infradead.org
Cc: linux-hardening@vger.kernel.org, mark.rutland@arm.com,
	catalin.marinas@arm.com, will@kernel.org
Subject: Re: [RFC PATCH 9/9] arm64: implement dynamic shadow call stack for GCC
Date: Thu, 14 Oct 2021 06:35:03 +0800	[thread overview]
Message-ID: <82104d87-b077-87a0-2393-ab15ac66dcf7@linux.alibaba.com> (raw)
In-Reply-To: <20211013152243.2216899-10-ardb@kernel.org>



On 10/13/21 11:22 PM, Ard Biesheuvel wrote:
> Implement support for the shadow call stack on GCC, and in a dynamic
> manner, by parsing the unwind tables at init time to locate all
> occurrences of PACIASP/AUTIASP, and replacing them with the shadow call
> stack push and pop instructions, respectively.
> 
> This is useful because the overhead of the shadow call stack is
> difficult to justify on hardware that implements pointer authentication
> (PAC), and given that the PAC instructions are executed as NOPs on
> hardware that doesn't, we can just replace them.
> 
> This patch only implements this for the core kernel, but the logic can
> be reused for modules without much trouble.
> 
> Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
> ---
>   Makefile                      |   4 +-
>   arch/Kconfig                  |   4 +-
>   arch/arm64/Kconfig            |   8 +-
>   arch/arm64/kernel/Makefile    |   2 +
>   arch/arm64/kernel/head.S      |   3 +
>   arch/arm64/kernel/patch-scs.c | 223 ++++++++++++++++++++
>   6 files changed, 239 insertions(+), 5 deletions(-)
> 
> diff --git a/Makefile b/Makefile
> index 7cfe4ff36f44..2d94fed93d9d 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -933,8 +933,8 @@ LDFLAGS_vmlinux += --gc-sections
>   endif
>   
>   ifdef CONFIG_SHADOW_CALL_STACK
> -CC_FLAGS_SCS	:= -fsanitize=shadow-call-stack
> -KBUILD_CFLAGS	+= $(CC_FLAGS_SCS)
> +CC_FLAGS_SCS-$(CONFIG_CC_IS_CLANG)	:= -fsanitize=shadow-call-stack
> +KBUILD_CFLAGS				+= $(CC_FLAGS_SCS-y)
>   export CC_FLAGS_SCS
>   endif
>   
> diff --git a/arch/arm64/kernel/patch-scs.c b/arch/arm64/kernel/patch-scs.c
> new file mode 100644
> index 000000000000..878a40060550
> --- /dev/null
> +++ b/arch/arm64/kernel/patch-scs.c
> +static int scs_patch_loc(u64 loc)
> +{
> +	u32 insn = le32_to_cpup((void *)loc);
> +
> +	/*
> +	 * Sometimes, the unwind data appears to be out of sync, and associates
> +	 * the DW_CFA_negate_ra_state directive with the ret instruction
> +	 * following the autiasp, rather than the autiasp itself.
> +	 */
> +	if (insn == 0xd65f03c0) { // ret
> +		loc -= 4;
> +		insn = le32_to_cpup((void *)loc);
> +	}
> +
> +	switch (insn) {
> +	case 0xd503233f: // paciasp
> +		*(u32 *)loc = cpu_to_le32(0xf800865e);
> +		break;
> +	case 0xd50323bf: // autiasp
> +		*(u32 *)loc = cpu_to_le32(0xf85f8e5e);
> +		break;
> +	default:
> +		// ignore
> +		break;
> +	}
> +	return 0;
> +}

Hi Ard,

According to my understanding (may be wrong), here may need to filter out
'-march=armv8.3-a'. When it is specified, gcc will use 'retaa' instead of
'autiasp' as a pac check.

  parent reply	other threads:[~2021-10-13 22:35 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-13 15:22 [RFC PATCH 0/9] arm64: use unwind data on GCC for shadow call stack Ard Biesheuvel
2021-10-13 15:22 ` [RFC PATCH 1/9] arm64: assembler: enable PAC for non-leaf assembler routines Ard Biesheuvel
2021-10-13 15:22 ` [RFC PATCH 2/9] arm64: cache: use ALIAS version of linkage macros for local aliases Ard Biesheuvel
2021-10-13 15:22 ` [RFC PATCH 3/9] arm64: crypto: avoid overlapping linkage definitions for AES-CBC Ard Biesheuvel
2021-10-13 15:22 ` [RFC PATCH 4/9] arm64: aes-neonbs: move frame pop to end of function Ard Biesheuvel
2021-10-13 15:22 ` [RFC PATCH 5/9] arm64: chacha-neon: move frame pop forward Ard Biesheuvel
2021-10-13 15:22 ` [RFC PATCH 6/9] arm64: smccc: create proper stack frames for HVC/SMC calls Ard Biesheuvel
2021-10-13 15:44   ` Mark Brown
2021-10-13 15:22 ` [RFC PATCH 7/9] arm64: assembler: add unwind annotations to frame push/pop macros Ard Biesheuvel
2021-10-13 15:22 ` [RFC PATCH 8/9] arm64: unwind: add asynchronous unwind tables to the kernel proper Ard Biesheuvel
2021-10-13 15:22 ` [RFC PATCH 9/9] arm64: implement dynamic shadow call stack for GCC Ard Biesheuvel
2021-10-13 15:42   ` Mark Brown
2021-10-13 22:35   ` Dan Li [this message]
2021-10-14  9:41     ` Ard Biesheuvel
2021-10-13 17:52 ` [RFC PATCH 0/9] arm64: use unwind data on GCC for shadow call stack Ard Biesheuvel
2021-10-13 18:01 ` Nick Desaulniers

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=82104d87-b077-87a0-2393-ab15ac66dcf7@linux.alibaba.com \
    --to=ashimida@linux.alibaba.com \
    --cc=ardb@kernel.org \
    --cc=catalin.marinas@arm.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=will@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox