From patchwork Wed Jul 18 21:10:12 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laura Abbott X-Patchwork-Id: 10533283 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 608C66053F for ; Wed, 18 Jul 2018 21:19:01 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4EDC829A6D for ; Wed, 18 Jul 2018 21:19:01 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4215B29AD7; Wed, 18 Jul 2018 21:19:01 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id BF84E29A6D for ; Wed, 18 Jul 2018 21:19:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=+BrbnmmjiJhw/MgwLf4Uo1EgDaFyZIR6/fOUM9v0V0w=; b=iJDD+tKlTcmi3F7gHJWR6q+jV/ jUn/shVaBd8OgnoxhcsicmmyiPHsW9Mx1g+xI50S9ZMsme3GaOpzGrofO1e/MgKbNwrhm+oJQ05PI Z9Q/jVKuTs3OGTSem/ibJURwtaVEyHgxR6DarX0/Tp1Iua2lGkhehgN4jhPtg0gMLW40FKy3+B9w9 wnElHj7CvymVGLp62Ip25SM+UGQ0YFKwWEGWDzZ1Wqx5tejNtfMU2EfntMa5R4UowZTUDUOKpWV1e VmqKy4qeZ4iJJPcQKdfVk/fw7Xb2XokGHfmlVGK+Acw8kvDV9xWt9xGvLBg6xiwGdTRcv06W+VWjI cS6toudA==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1fftqY-0006jJ-Cy; Wed, 18 Jul 2018 21:18:54 +0000 Received: from mail-qt0-f178.google.com ([209.85.216.178]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1fftqU-0006i8-Uc for linux-arm-kernel@lists.infradead.org; Wed, 18 Jul 2018 21:18:52 +0000 Received: by mail-qt0-f178.google.com with SMTP id q12-v6so5392080qtp.6 for ; Wed, 18 Jul 2018 14:18:40 -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=t1S4kEK/a+m5ZjGsRSR5mI0uIRjMCcZ7tsqvPBqF4BsrJdS2JrBZhmHFimQPFeca/1 J0tfRKUtEJJilf1CFm0SsC4Gn8xVf+lhKUmgFxg6j7+OZix2p7SUYF+dUUDAxf1VY7IQ hqYtDdkiDvqEEOchh7MGoCgRNtzG74u2Z0NgQ3zwZv2X4PYIfPJrojwrCAP9fR3i1YWi pHC+W5JzNQtz6NVicD8hubTNQ0ha80YhGgLD6881JKqB2RrBxZIZU1Whjr44ihQU3Dk3 E7TvxPOZocI1dJfO7dzapzqo4j5DudPwIFm+NYqU4EZHqKBYv9WT2c43xI+zGaSbDcW9 heQw== X-Gm-Message-State: AOUpUlHM7T0txEEavI+zBk+NjP3o27rHyye/czWt18ywig6k1xcQ+W2n UffZcTdlEwE4Ir/U66V5wyt0PQ== X-Google-Smtp-Source: AAOMgpdIv0PZhCXXELQUtUh0J1jgiVgnoxQ/gLwR7BU6QL06DkD8U6X4M2+n+cTqTqDApdoqs9xezg== 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 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> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20180718_141850_986193_6D7DFAC6 X-CRM114-Status: GOOD ( 18.08 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kernel-hardening@lists.openwall.com, Catalin Marinas , Will Deacon , linux-kernel@vger.kernel.org, Laura Abbott , linux-arm-kernel@lists.infradead.org MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP 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)