All of lore.kernel.org
 help / color / mirror / Atom feed
From: Mark Rutland <mark.rutland@arm.com>
To: Will Deacon <will.deacon@arm.com>
Cc: linux-arm-kernel@lists.infradead.org, ard.biesheuvel@linaro.org,
	catalin.marinas@arm.com, james.morse@arm.com, labbott@redhat.com,
	linux-kernel@vger.kernel.org, luto@amacapital.net,
	matt@codeblueprint.co.uk, kernel-hardening@lists.openwall.com,
	keescook@chromium.org
Subject: Re: [PATCH 14/14] arm64: add VMAP_STACK overflow detection
Date: Mon, 14 Aug 2017 18:25:09 +0100	[thread overview]
Message-ID: <20170814172509.GD23428@leverpostej> (raw)
In-Reply-To: <20170814153253.GA22747@arm.com>

On Mon, Aug 14, 2017 at 04:32:53PM +0100, Will Deacon wrote:
> Just some minor comments on this (after taking ages to realise you were
> using tpidr_el0 as a temporary rather than tpidr_el1 and getting totally
> confused!).
> 
> On Mon, Aug 07, 2017 at 07:36:05PM +0100, Mark Rutland wrote:

> > +static inline bool on_overflow_stack(unsigned long sp)
> > +{
> > +	unsigned long low = (unsigned long)this_cpu_ptr(overflow_stack);
> 
> Can you use raw_cpu_ptr here, like you do for the irq stack?

Sure; done.

> > diff --git a/arch/arm64/kernel/entry.S b/arch/arm64/kernel/entry.S
> > index e5aa866..44a27c3 100644
> > --- a/arch/arm64/kernel/entry.S
> > +++ b/arch/arm64/kernel/entry.S
> > @@ -72,6 +72,37 @@
> >  	.macro kernel_ventry	label
> >  	.align 7
> >  	sub	sp, sp, #S_FRAME_SIZE
> > +#ifdef CONFIG_VMAP_STACK
> > +	add	sp, sp, x0			// sp' = sp + x0
> > +	sub	x0, sp, x0			// x0' = sp' - x0 = (sp + x0) - x0 = sp
> > +	tbnz	x0, #THREAD_SHIFT, 0f
> > +	sub	x0, sp, x0			// sp' - x0' = (sp + x0) - sp = x0
> > +	sub	sp, sp, x0			// sp' - x0 = (sp + x0) - x0 = sp
> > +	b	\label
> > +
> > +	/* Stash the original SP value in tpidr_el0 */
> > +0:	msr	tpidr_el0, x0
> 
> The comment here is a bit confusing, since the sp has already been
> decremented for the frame, as mention in a later comment.

True. I've updated the comment to say:

	/*
	 * Stash the SP (minus S_FRAME_SIZE) in tpidr_el0. We can recover the
	 * original SP value later if we need it.
	 */  

[...]

> > +	 * Store the original GPRs to the new stack. The orginial SP (minus
> 
> original

Took me a moment to spot the second instance. Fixed now.

[...]

> > +	/* Time to die */
> > +	bl	handle_bad_stack
> > +	ASM_BUG()
> 
> Why not just a b without the ASM_BUG?

We need the BL to ensure that the LR is valid for unwinding. That's
necessary for the backtrace to identify the exception regs based on the
LR falling into .entry.text.

The ASM_BUG() ensures that the LR value definitely falls in .entry.text,
and makes the backtrace resolve the symbol correctly regardless of
what's next.

I didn't add a comment for the other cases, so I hadn't bothered here.
I'm happy to add those, so long as we're consistent.

Thanks,
Mark.

WARNING: multiple messages have this Message-ID (diff)
From: mark.rutland@arm.com (Mark Rutland)
To: linux-arm-kernel@lists.infradead.org
Subject: [PATCH 14/14] arm64: add VMAP_STACK overflow detection
Date: Mon, 14 Aug 2017 18:25:09 +0100	[thread overview]
Message-ID: <20170814172509.GD23428@leverpostej> (raw)
In-Reply-To: <20170814153253.GA22747@arm.com>

On Mon, Aug 14, 2017 at 04:32:53PM +0100, Will Deacon wrote:
> Just some minor comments on this (after taking ages to realise you were
> using tpidr_el0 as a temporary rather than tpidr_el1 and getting totally
> confused!).
> 
> On Mon, Aug 07, 2017 at 07:36:05PM +0100, Mark Rutland wrote:

> > +static inline bool on_overflow_stack(unsigned long sp)
> > +{
> > +	unsigned long low = (unsigned long)this_cpu_ptr(overflow_stack);
> 
> Can you use raw_cpu_ptr here, like you do for the irq stack?

Sure; done.

> > diff --git a/arch/arm64/kernel/entry.S b/arch/arm64/kernel/entry.S
> > index e5aa866..44a27c3 100644
> > --- a/arch/arm64/kernel/entry.S
> > +++ b/arch/arm64/kernel/entry.S
> > @@ -72,6 +72,37 @@
> >  	.macro kernel_ventry	label
> >  	.align 7
> >  	sub	sp, sp, #S_FRAME_SIZE
> > +#ifdef CONFIG_VMAP_STACK
> > +	add	sp, sp, x0			// sp' = sp + x0
> > +	sub	x0, sp, x0			// x0' = sp' - x0 = (sp + x0) - x0 = sp
> > +	tbnz	x0, #THREAD_SHIFT, 0f
> > +	sub	x0, sp, x0			// sp' - x0' = (sp + x0) - sp = x0
> > +	sub	sp, sp, x0			// sp' - x0 = (sp + x0) - x0 = sp
> > +	b	\label
> > +
> > +	/* Stash the original SP value in tpidr_el0 */
> > +0:	msr	tpidr_el0, x0
> 
> The comment here is a bit confusing, since the sp has already been
> decremented for the frame, as mention in a later comment.

True. I've updated the comment to say:

	/*
	 * Stash the SP (minus S_FRAME_SIZE) in tpidr_el0. We can recover the
	 * original SP value later if we need it.
	 */  

[...]

> > +	 * Store the original GPRs to the new stack. The orginial SP (minus
> 
> original

Took me a moment to spot the second instance. Fixed now.

[...]

> > +	/* Time to die */
> > +	bl	handle_bad_stack
> > +	ASM_BUG()
> 
> Why not just a b without the ASM_BUG?

We need the BL to ensure that the LR is valid for unwinding. That's
necessary for the backtrace to identify the exception regs based on the
LR falling into .entry.text.

The ASM_BUG() ensures that the LR value definitely falls in .entry.text,
and makes the backtrace resolve the symbol correctly regardless of
what's next.

I didn't add a comment for the other cases, so I hadn't bothered here.
I'm happy to add those, so long as we're consistent.

Thanks,
Mark.

WARNING: multiple messages have this Message-ID (diff)
From: Mark Rutland <mark.rutland@arm.com>
To: Will Deacon <will.deacon@arm.com>
Cc: linux-arm-kernel@lists.infradead.org, ard.biesheuvel@linaro.org,
	catalin.marinas@arm.com, james.morse@arm.com, labbott@redhat.com,
	linux-kernel@vger.kernel.org, luto@amacapital.net,
	matt@codeblueprint.co.uk, kernel-hardening@lists.openwall.com,
	keescook@chromium.org
Subject: [kernel-hardening] Re: [PATCH 14/14] arm64: add VMAP_STACK overflow detection
Date: Mon, 14 Aug 2017 18:25:09 +0100	[thread overview]
Message-ID: <20170814172509.GD23428@leverpostej> (raw)
In-Reply-To: <20170814153253.GA22747@arm.com>

On Mon, Aug 14, 2017 at 04:32:53PM +0100, Will Deacon wrote:
> Just some minor comments on this (after taking ages to realise you were
> using tpidr_el0 as a temporary rather than tpidr_el1 and getting totally
> confused!).
> 
> On Mon, Aug 07, 2017 at 07:36:05PM +0100, Mark Rutland wrote:

> > +static inline bool on_overflow_stack(unsigned long sp)
> > +{
> > +	unsigned long low = (unsigned long)this_cpu_ptr(overflow_stack);
> 
> Can you use raw_cpu_ptr here, like you do for the irq stack?

Sure; done.

> > diff --git a/arch/arm64/kernel/entry.S b/arch/arm64/kernel/entry.S
> > index e5aa866..44a27c3 100644
> > --- a/arch/arm64/kernel/entry.S
> > +++ b/arch/arm64/kernel/entry.S
> > @@ -72,6 +72,37 @@
> >  	.macro kernel_ventry	label
> >  	.align 7
> >  	sub	sp, sp, #S_FRAME_SIZE
> > +#ifdef CONFIG_VMAP_STACK
> > +	add	sp, sp, x0			// sp' = sp + x0
> > +	sub	x0, sp, x0			// x0' = sp' - x0 = (sp + x0) - x0 = sp
> > +	tbnz	x0, #THREAD_SHIFT, 0f
> > +	sub	x0, sp, x0			// sp' - x0' = (sp + x0) - sp = x0
> > +	sub	sp, sp, x0			// sp' - x0 = (sp + x0) - x0 = sp
> > +	b	\label
> > +
> > +	/* Stash the original SP value in tpidr_el0 */
> > +0:	msr	tpidr_el0, x0
> 
> The comment here is a bit confusing, since the sp has already been
> decremented for the frame, as mention in a later comment.

True. I've updated the comment to say:

	/*
	 * Stash the SP (minus S_FRAME_SIZE) in tpidr_el0. We can recover the
	 * original SP value later if we need it.
	 */  

[...]

> > +	 * Store the original GPRs to the new stack. The orginial SP (minus
> 
> original

Took me a moment to spot the second instance. Fixed now.

[...]

> > +	/* Time to die */
> > +	bl	handle_bad_stack
> > +	ASM_BUG()
> 
> Why not just a b without the ASM_BUG?

We need the BL to ensure that the LR is valid for unwinding. That's
necessary for the backtrace to identify the exception regs based on the
LR falling into .entry.text.

The ASM_BUG() ensures that the LR value definitely falls in .entry.text,
and makes the backtrace resolve the symbol correctly regardless of
what's next.

I didn't add a comment for the other cases, so I hadn't bothered here.
I'm happy to add those, so long as we're consistent.

Thanks,
Mark.

  reply	other threads:[~2017-08-14 17:26 UTC|newest]

Thread overview: 69+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-08-07 18:35 [PATCH 00/14] arm64: VMAP_STACK support Mark Rutland
2017-08-07 18:35 ` [kernel-hardening] " Mark Rutland
2017-08-07 18:35 ` Mark Rutland
2017-08-07 18:35 ` [PATCH 01/14] arm64: remove __die()'s stack dump Mark Rutland
2017-08-07 18:35   ` [kernel-hardening] " Mark Rutland
2017-08-07 18:35   ` Mark Rutland
2017-08-07 18:35 ` [PATCH 02/14] fork: allow arch-override of VMAP stack alignment Mark Rutland
2017-08-07 18:35   ` [kernel-hardening] " Mark Rutland
2017-08-07 18:35   ` Mark Rutland
2017-08-07 18:35 ` [PATCH 03/14] arm64: kernel: remove {THREAD,IRQ_STACK}_START_SP Mark Rutland
2017-08-07 18:35   ` [kernel-hardening] " Mark Rutland
2017-08-07 18:35   ` Mark Rutland
2017-08-07 18:35 ` [PATCH 04/14] arm64: factor out PAGE_* and CONT_* definitions Mark Rutland
2017-08-07 18:35   ` [kernel-hardening] " Mark Rutland
2017-08-07 18:35   ` Mark Rutland
2017-08-07 18:35 ` [PATCH 05/14] arm64: clean up THREAD_* definitions Mark Rutland
2017-08-07 18:35   ` [kernel-hardening] " Mark Rutland
2017-08-07 18:35   ` Mark Rutland
2017-08-14 11:59   ` Catalin Marinas
2017-08-14 11:59     ` [kernel-hardening] " Catalin Marinas
2017-08-14 11:59     ` Catalin Marinas
2017-08-14 13:10     ` Mark Rutland
2017-08-14 13:10       ` [kernel-hardening] " Mark Rutland
2017-08-14 13:10       ` Mark Rutland
2017-08-07 18:35 ` [PATCH 06/14] arm64: clean up irq stack definitions Mark Rutland
2017-08-07 18:35   ` [kernel-hardening] " Mark Rutland
2017-08-07 18:35   ` Mark Rutland
2017-08-07 18:35 ` [PATCH 07/14] arm64: move SEGMENT_ALIGN to <asm/memory.h> Mark Rutland
2017-08-07 18:35   ` [kernel-hardening] " Mark Rutland
2017-08-07 18:35   ` Mark Rutland
2017-08-07 18:35 ` [PATCH 08/14] efi/arm64: add EFI_KIMG_ALIGN Mark Rutland
2017-08-07 18:35   ` [kernel-hardening] " Mark Rutland
2017-08-07 18:35   ` Mark Rutland
2017-08-07 18:36 ` [PATCH 09/14] arm64: factor out entry stack manipulation Mark Rutland
2017-08-07 18:36   ` [kernel-hardening] " Mark Rutland
2017-08-07 18:36   ` Mark Rutland
2017-08-07 18:36 ` [PATCH 10/14] arm64: assembler: allow adr_this_cpu to use the stack pointer Mark Rutland
2017-08-07 18:36   ` [kernel-hardening] " Mark Rutland
2017-08-07 18:36   ` Mark Rutland
2017-08-14 17:13   ` Catalin Marinas
2017-08-14 17:13     ` [kernel-hardening] " Catalin Marinas
2017-08-14 17:13     ` Catalin Marinas
2017-08-14 17:42     ` Mark Rutland
2017-08-14 17:42       ` [kernel-hardening] " Mark Rutland
2017-08-14 17:42       ` Mark Rutland
2017-08-07 18:36 ` [PATCH 11/14] arm64: use an irq " Mark Rutland
2017-08-07 18:36   ` [kernel-hardening] " Mark Rutland
2017-08-07 18:36   ` Mark Rutland
2017-08-07 18:36 ` [PATCH 12/14] arm64: add basic VMAP_STACK support Mark Rutland
2017-08-07 18:36   ` [kernel-hardening] " Mark Rutland
2017-08-07 18:36   ` Mark Rutland
2017-08-07 18:36 ` [PATCH 13/14] arm64: add on_accessible_stack() Mark Rutland
2017-08-07 18:36   ` [kernel-hardening] " Mark Rutland
2017-08-07 18:36   ` Mark Rutland
2017-08-07 18:36 ` [PATCH 14/14] arm64: add VMAP_STACK overflow detection Mark Rutland
2017-08-07 18:36   ` [kernel-hardening] " Mark Rutland
2017-08-07 18:36   ` Mark Rutland
2017-08-14 15:32   ` Will Deacon
2017-08-14 15:32     ` [kernel-hardening] " Will Deacon
2017-08-14 15:32     ` Will Deacon
2017-08-14 17:25     ` Mark Rutland [this message]
2017-08-14 17:25       ` [kernel-hardening] " Mark Rutland
2017-08-14 17:25       ` Mark Rutland
2017-08-15 11:10   ` Catalin Marinas
2017-08-15 11:10     ` [kernel-hardening] " Catalin Marinas
2017-08-15 11:10     ` Catalin Marinas
2017-08-15 11:19     ` Mark Rutland
2017-08-15 11:19       ` [kernel-hardening] " Mark Rutland
2017-08-15 11:19       ` Mark Rutland

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=20170814172509.GD23428@leverpostej \
    --to=mark.rutland@arm.com \
    --cc=ard.biesheuvel@linaro.org \
    --cc=catalin.marinas@arm.com \
    --cc=james.morse@arm.com \
    --cc=keescook@chromium.org \
    --cc=kernel-hardening@lists.openwall.com \
    --cc=labbott@redhat.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luto@amacapital.net \
    --cc=matt@codeblueprint.co.uk \
    --cc=will.deacon@arm.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.