Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp1565096imm; Thu, 19 Jul 2018 04:09:01 -0700 (PDT) X-Google-Smtp-Source: AAOMgpfeK3PGu/Zf6ScYOGGFsH9hFIA71XU8HlgpRCW8evTUhkptA/DO+xkmANRIWdbzva2fs06E X-Received: by 2002:a63:524e:: with SMTP id s14-v6mr9690708pgl.35.1531998541684; Thu, 19 Jul 2018 04:09:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1531998541; cv=none; d=google.com; s=arc-20160816; b=BOTfe6jRRShX69QssXqGi2sx/87rHsFW87trgtEeIDvwk/Gm525cAtUTBQubqTXW97 nWDaAbMMcgJx6ED30xEdGaytV5ArMFjnqQX/bs6RoKFU1P6PILC8b6osyeABOwk06+Hb ke7bielgozPWymtMLu2VQxxfXctoJTf5yEwqRVcAY5IiFTzIbeVla+y3DXmemIYSRXt2 G4LneSMeWi/fHV1EBAzMstKNF+b6lQJtuA57xh6DstqvuuLsdLCBhtqQHv6HIzEbDT8W U4TQbw1OwJAtYbp2bhe7g72n0XApVk3DFEsMo45RjK1ik6wnEW6mGQ41nhWHX1negpir uQNw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:arc-authentication-results; bh=27EJ8iA8UZr704vt6vsgGbADKUjdT5p7buQYTg0RGmk=; b=GLjbxjKP+uKN3wlEH4OyyfLpyAbIQseptFB+rRAlNDPo2W40UT0yY+3exJDV3fLmJv mDCpf5dMdlJI+WJgNGSmPlkdrR+roH2iQ/BuUMGQdm6BACRHfb9F1xqw0TpX8F7esPmY k1Pcmnv05Rw+p6BQsXpsk6Z0p3+lQ8YIwZZyD3tEJ63SQU2CjVIHGP8PypimtBIuGkph nvtLryOlsyJ4ufl8D0CPIvR/NkSP/T/JGa62t65Oqrs+6FsmaE9BgOBJPJLmLlVYy5eB OnGdp/ks3P6b+2wbabjiDAZHYYj+lq6FvMMKFl9zsyGcwMV4utjTZ4uEUPdkdMBtpdpB iUpA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j8-v6si5847481pgi.575.2018.07.19.04.08.46; Thu, 19 Jul 2018 04:09:01 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731171AbeGSLum (ORCPT + 99 others); Thu, 19 Jul 2018 07:50:42 -0400 Received: from foss.arm.com ([217.140.101.70]:47854 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726630AbeGSLul (ORCPT ); Thu, 19 Jul 2018 07:50:41 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 3A1E2ED1; Thu, 19 Jul 2018 04:08:02 -0700 (PDT) Received: from lakrids.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.72.51.249]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 6FC6A3F246; Thu, 19 Jul 2018 04:08:00 -0700 (PDT) Date: Thu, 19 Jul 2018 12:07:58 +0100 From: Mark Rutland To: Laura Abbott Cc: Alexander Popov , Kees Cook , Ard Biesheuvel , kernel-hardening@lists.openwall.com, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Will Deacon , Catalin Marinas , james.morse@arm.com Subject: Re: [PATCH 1/2] arm64: Introduce current_stack_type Message-ID: <20180719110757.jh4cnitfrz6pasho@lakrids.cambridge.arm.com> References: <20180718211013.14512-1-labbott@redhat.com> <20180718211013.14512-2-labbott@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20180718211013.14512-2-labbott@redhat.com> User-Agent: NeoMutt/20170113 (1.7.2) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Laura, On Wed, Jul 18, 2018 at 02:10:12PM -0700, Laura Abbott wrote: > > In preparation for enabling the stackleak plugin on arm64, > we need a way to get the bounds of the current stack. > Introduce a new primitive current_stack_type which is similar > to x86's get_stack_info. Utilize that to rework > on_accessible_stack slightly as well. > > Signed-off-by: Laura Abbott > --- > So this did end up looking quite a bit like get_stack_info but I didn't > really see the need to integrate this more than this. I do think > actually enumerating the types makes things a bit cleaner. > --- > arch/arm64/include/asm/sdei.h | 8 ++- > arch/arm64/include/asm/stacktrace.h | 94 ++++++++++++++++++++++++----- > arch/arm64/kernel/ptrace.c | 2 +- > arch/arm64/kernel/sdei.c | 21 ++++++- > 4 files changed, 103 insertions(+), 22 deletions(-) > > diff --git a/arch/arm64/include/asm/sdei.h b/arch/arm64/include/asm/sdei.h > index e073e6886685..34f7b203845b 100644 > --- a/arch/arm64/include/asm/sdei.h > +++ b/arch/arm64/include/asm/sdei.h > @@ -40,15 +40,17 @@ asmlinkage unsigned long __sdei_handler(struct pt_regs *regs, > unsigned long sdei_arch_get_entry_point(int conduit); > #define sdei_arch_get_entry_point(x) sdei_arch_get_entry_point(x) > > -bool _on_sdei_stack(unsigned long sp); > -static inline bool on_sdei_stack(unsigned long sp) > +bool _on_sdei_stack(unsigned long sp, unsigned long *, unsigned long *); > +static inline bool on_sdei_stack(unsigned long sp, > + unsigned long *stack_low, > + unsigned long *stack_high) > { > if (!IS_ENABLED(CONFIG_VMAP_STACK)) > return false; > if (!IS_ENABLED(CONFIG_ARM_SDE_INTERFACE)) > return false; > if (in_nmi()) > - return _on_sdei_stack(sp); > + return _on_sdei_stack(sp, stack_low, stack_high); > > return false; > } > diff --git a/arch/arm64/include/asm/stacktrace.h b/arch/arm64/include/asm/stacktrace.h > index 902f9edacbea..9855a0425e64 100644 > --- a/arch/arm64/include/asm/stacktrace.h > +++ b/arch/arm64/include/asm/stacktrace.h > @@ -39,7 +39,9 @@ extern void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk); > > DECLARE_PER_CPU(unsigned long *, irq_stack_ptr); > > -static inline bool on_irq_stack(unsigned long sp) > +static inline bool on_irq_stack(unsigned long sp, > + unsigned long *stack_low, > + unsigned long *stack_high) > { > unsigned long low = (unsigned long)raw_cpu_read(irq_stack_ptr); > unsigned long high = low + IRQ_STACK_SIZE; > @@ -47,47 +49,109 @@ static inline bool on_irq_stack(unsigned long sp) > if (!low) > return false; > > - return (low <= sp && sp < high); > + if (sp < low || sp >= high) > + return false; > + > + if (stack_low && stack_high) { > + *stack_low = low; > + *stack_high = high; > + } > + > + return true; > } Rather than having to pass separete pointers to low/high, could we please wrap them up as a struct, e.g. struct stack_info { unsigned long low, high; stack_type type; } ... and pass a single pointer to that? e.g. static inline bool on_irq_stack(unsigned long sp, struct stack_info *info) { unsigned long low = (unsigned long)raw_cpu_read(irq_stack_ptr); unsigned long high = low + IRQ_STACK_SIZE; if (!low) return false; if (sp < low || sp >= high) return false; if (info) { info->low = low; info->high = high; info->type = STACK_TYPE_IRQ; } return true; } That simplified the prototypes, and will allow us to distinguish the two SDEI stacks (which we'll need for making stack unwiding robust to cross-stack loops). > -static inline bool on_task_stack(struct task_struct *tsk, unsigned long sp) > +static inline bool on_task_stack(struct task_struct *tsk, unsigned long sp, > + unsigned long *stack_low, > + unsigned long *stack_high) > { > unsigned long low = (unsigned long)task_stack_page(tsk); > unsigned long high = low + THREAD_SIZE; > > - return (low <= sp && sp < high); > + if (sp < low || sp >= high) > + return false; > + > + if (stack_low && stack_high) { > + *stack_low = low; > + *stack_high = high; > + } > + > + return true; > } > > #ifdef CONFIG_VMAP_STACK > DECLARE_PER_CPU(unsigned long [OVERFLOW_STACK_SIZE/sizeof(long)], overflow_stack); > > -static inline bool on_overflow_stack(unsigned long sp) > +static inline bool on_overflow_stack(unsigned long sp, > + unsigned long *stack_low, > + unsigned long *stack_high) > { > unsigned long low = (unsigned long)raw_cpu_ptr(overflow_stack); > unsigned long high = low + OVERFLOW_STACK_SIZE; > > - return (low <= sp && sp < high); > + if (sp < low || sp >= high) > + return false; > + > + if (stack_low && stack_high) { > + *stack_low = low; > + *stack_high = high; > + } > + > + return true; > } > #else > -static inline bool on_overflow_stack(unsigned long sp) { return false; } > +static inline bool on_overflow_stack(unsigned long sp, > + unsigned long *stack_low, > + unsigned long *stack_high) { return false; } > #endif > > +enum stack_type { > + STACK_TYPE_UNKNOWN, > + STACK_TYPE_TASK, > + STACK_TYPE_IRQ, > + STACK_TYPE_OVERFLOW, > + STACK_TYPE_SDEI, > +}; For SDEI we'll need STACK_TYPE_SDEI_NORMAL and STACK_TYPE_SDEI_CRITICAL, at least for stack unwinding. > + > +static inline enum stack_type current_stack_type(struct task_struct *tsk, > + unsigned long sp, > + unsigned long *stack_low, > + unsigned long *stack_high) > +{ > + if (on_task_stack(tsk, sp, stack_low, stack_high)) > + return STACK_TYPE_TASK; > + if (on_irq_stack(sp, stack_low, stack_high)) > + return STACK_TYPE_IRQ; > + if (on_overflow_stack(sp, stack_low, stack_high)) > + return STACK_TYPE_OVERFLOW; > + if (on_sdei_stack(sp, stack_low, stack_high)) > + return STACK_TYPE_SDEI; > + return STACK_TYPE_UNKNOWN; > +} > + > /* > * We can only safely access per-cpu stacks from current in a non-preemptible > * context. > */ > static inline bool on_accessible_stack(struct task_struct *tsk, unsigned long sp) > { > - if (on_task_stack(tsk, sp)) > + enum stack_type type; > + unsigned long low, high; > + > + type = current_stack_type(tsk, sp, &low, &high); > + > + switch (type) { > + case STACK_TYPE_TASK: > return true; > - if (tsk != current || preemptible()) > + case STACK_TYPE_IRQ: > + case STACK_TYPE_OVERFLOW: > + case STACK_TYPE_SDEI: > + if (tsk != current || preemptible()) > + return false; > + else > + return true; > + case STACK_TYPE_UNKNOWN: > return false; > - if (on_irq_stack(sp)) > - return true; > - if (on_overflow_stack(sp)) > - return true; > - if (on_sdei_stack(sp)) > - return true; > + } > > return false; > } With the stacut stack_info, I think we can leave the logic of on_accessible_stack() as-is, modulo a new info parameter that we pass on into each on__stack(), and then we don't neeed a separate current_stack_type() function. > diff --git a/arch/arm64/kernel/ptrace.c b/arch/arm64/kernel/ptrace.c > index 5c338ce5a7fa..a6b3a2be7561 100644 > --- a/arch/arm64/kernel/ptrace.c > +++ b/arch/arm64/kernel/ptrace.c > @@ -132,7 +132,7 @@ static bool regs_within_kernel_stack(struct pt_regs *regs, unsigned long addr) > { > return ((addr & ~(THREAD_SIZE - 1)) == > (kernel_stack_pointer(regs) & ~(THREAD_SIZE - 1))) || > - on_irq_stack(addr); > + on_irq_stack(addr, NULL, NULL); > } > > /** > diff --git a/arch/arm64/kernel/sdei.c b/arch/arm64/kernel/sdei.c > index 6b8d90d5ceae..8e18913a53fd 100644 > --- a/arch/arm64/kernel/sdei.c > +++ b/arch/arm64/kernel/sdei.c > @@ -88,7 +88,9 @@ static int init_sdei_stacks(void) > return err; > } > > -bool _on_sdei_stack(unsigned long sp) > +bool _on_sdei_stack(unsigned long sp, > + unsigned long *stack_low, > + unsigned long *stack_high) > { > unsigned long low, high; > > @@ -98,13 +100,26 @@ bool _on_sdei_stack(unsigned long sp) > low = (unsigned long)raw_cpu_read(sdei_stack_critical_ptr); > high = low + SDEI_STACK_SIZE; > > - if (low <= sp && sp < high) > + if (low <= sp && sp < high) { > + if (stack_low && stack_high) { > + *stack_low = low; > + *stack_high = high; > + } > return true; > + } > > low = (unsigned long)raw_cpu_read(sdei_stack_normal_ptr); > high = low + SDEI_STACK_SIZE; > > - return (low <= sp && sp < high); > + if (low <= sp && sp < high) { > + if (stack_low && stack_high) { > + *stack_low = low; > + *stack_high = high; > + } > + return true; > + } > + > + return false; > } We should probably split this into separate on_sdei_normal_stack() and on_sdei_critical_stack() functions. Then we can do: bool on_sdei__stack(...) { if () return false; if (info) { ; } return true; } bool _on_sdei_stack(unsigned long sp, struct stack_info *info) { if (on_sedi_critical_stack(sp, info)) return true; if (on_sdei_normal_stack(sp, info)) return true; return false; } ... which is a little nicer for legibility. Otherwise, this looks good to me. Thanks, Mark.