Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp976819imm; Wed, 18 Jul 2018 14:12:21 -0700 (PDT) X-Google-Smtp-Source: AAOMgpe0xnDi/KyIvXThYXS7pIsMziN9eH4pCDmleZ/mqgeorHbs4GVG6ecfcGk99y9wJY+LhVtR X-Received: by 2002:a17:902:8486:: with SMTP id c6-v6mr7262645plo.283.1531948341322; Wed, 18 Jul 2018 14:12:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1531948341; cv=none; d=google.com; s=arc-20160816; b=mQEwUgi7utMocTLSq9Zd4MmUgiT2zOH+rM1WOsLBa81GlRQnO9eynnmZTXsYiOUu5k jIu5lg2XQEDJfKC6//jK4T6aP1EaWBHbvYxdGpMx2bEwP3adwkY9t82ibAg2JsNJRwmQ NOXeLh0Jmhh7YIJPwdbNAGWj2xvTNgKH53X+YcuDnTjyVQ2prBSX1NVCrBANghBvGGF5 9yPPzeSFK3cmKA9p7K7LLg6Oxq7h+/0uokG+yvk9kZQ8Fta/AD8XOCaDYv8NTwRWv4Bt Y3GK+BDEoquISk0FIF6IgspeczgN7JyiROh23jLLcwnUtFnHlWB1Gm0OIrPA6zQgBVKh bD6w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=g/OX3VgJjdgzO7seeCrbMw7ezGcq5eUy3GHwluEcB5c=; b=ZjwwQbP/q+5dEOsAJDgwAeIWeqv0OI93ukVF3ByZgYn8WBF2i0NoUVJSqX8jSPKufe LLIgqMSFHvfX/hP1YVxKtwL8bDbAZfAmcaYMFgFDNVOaNVARBbH2qAz9kHv5etwbIYrj GPPRP2SOdnaXkb4ay2pP37kpiGasrtIZcseMnz1bO1ArrwQthIzvhpdrajpZsQ7gTmhs fV10KD2nCYfhhkffP/NtVssy/9+f7F7j9VgUt3U2jSXQkipmO5EdNd6D/6QrhrsNu2vh GNykskYE5IMMIzdpjW0sjHs9h4P8s7M9fa/3mohpHD/AWRvvEYUkRWjUzGEi7xmzJMkF lgaA== 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v6-v6si3934457plp.60.2018.07.18.14.12.06; Wed, 18 Jul 2018 14:12:21 -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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730499AbeGRVuJ (ORCPT + 99 others); Wed, 18 Jul 2018 17:50:09 -0400 Received: from mail-qt0-f172.google.com ([209.85.216.172]:45598 "EHLO mail-qt0-f172.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726992AbeGRVuI (ORCPT ); Wed, 18 Jul 2018 17:50:08 -0400 Received: by mail-qt0-f172.google.com with SMTP id y5-v6so5380351qti.12 for ; Wed, 18 Jul 2018 14:10:25 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=g/OX3VgJjdgzO7seeCrbMw7ezGcq5eUy3GHwluEcB5c=; b=P2yo6EZ6D33XojjIkesu0jirRnzTGteJRZKLr0ZIuyjmsVsy41fWfuLJOW7E5XKQz8 7lCeKOJEObn9VXp7KyH+1XaNtusl2xpt6Q6GhXVNqL6G5e22fKAkylH7WajsRICKFWi8 +Nvv/pRcKTOI2mAh+RmV1zQK2V34RLNTpC4H+tyRYrvl0IDn++Ui33qFlTkXsm5B3OaD Ck+RFyZQIsty/KlAg+XgBVHloAeXqUuWQukPYIyB/XqcLWudEi0EnYRfM/H/zCizciBO e/Xmno2JBC1Bs0dwMW/xK0XoyftTbZ8msNiVFv+8l6wnmPD7GpvGusQJWgLX8dhKNRuo fIMQ== X-Gm-Message-State: AOUpUlF7Hi4jtyyG1TJ5l856QaTmu4FMRPO0OOE/zyENSEARgJ3mkjAC 6+96fqLnruZU4c2ccZ+9DUTUkQ== X-Received: by 2002:ac8:2cd1:: with SMTP id 17-v6mr7269393qtx.360.1531948225423; Wed, 18 Jul 2018 14:10:25 -0700 (PDT) Received: from labbott-redhat.redhat.com ([2601:602:9802:a8dc:4eb2:6dae:ab32:e5b0]) by smtp.gmail.com with ESMTPSA id k3-v6sm4143463qta.37.2018.07.18.14.10.22 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 18 Jul 2018 14:10:24 -0700 (PDT) From: Laura Abbott To: Alexander Popov , Kees Cook , Mark Rutland , Ard Biesheuvel Cc: Laura Abbott , kernel-hardening@lists.openwall.com, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Will Deacon , Catalin Marinas Subject: [PATCH 1/2] arm64: Introduce current_stack_type Date: Wed, 18 Jul 2018 14:10:12 -0700 Message-Id: <20180718211013.14512-2-labbott@redhat.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180718211013.14512-1-labbott@redhat.com> References: <20180718211013.14512-1-labbott@redhat.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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; } -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, +}; + +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; } 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; } unsigned long sdei_arch_get_entry_point(int conduit) -- 2.17.1