From patchwork Wed May 16 16:28:16 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Popov X-Patchwork-Id: 10404355 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 1D2F6601F7 for ; Wed, 16 May 2018 16:29:55 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 096562855C for ; Wed, 16 May 2018 16:29:55 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id EFBCC2855E; Wed, 16 May 2018 16:29:54 +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=-5.2 required=2.0 tests=BAYES_00, MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from mother.openwall.net (mother.openwall.net [195.42.179.200]) by mail.wl.linuxfoundation.org (Postfix) with SMTP id A67292855C for ; Wed, 16 May 2018 16:29:53 +0000 (UTC) Received: (qmail 7731 invoked by uid 550); 16 May 2018 16:29:07 -0000 Mailing-List: contact kernel-hardening-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Delivered-To: mailing list kernel-hardening@lists.openwall.com Received: (qmail 7564 invoked from network); 16 May 2018 16:29:04 -0000 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=Z+F0AcOccYfLI+6u0SmcGszYzmuXqCISkQjylv7khew=; b=Nmu1GmGrGORFB/WM+l7YHJjzZBeKujjKM5cxPNFDSOYN7ApAD/pbNvkdrCtsCn8i6N 8pnVaHs82IFiFtbDjv6H6RbnEoc9jiloKYMDG7e0zmdRGHYO1KCpz/AYK9Mhk2lGCVVs jMVsS6/YuxWLgMu6eP/IdIJxOEaDrDjxdHpqzYoe+H2FThRS5eydkPitN959BOENWu1c nt/8cmuUSVC/p1qFhPv4uOZvaqECvN5sQwJMEXhMn2rUOXpO88fm85fZRD/QgP68vslV /Usa0CTLhS4LH81cl6umIfTdFlzpplqZaUjlenL7Iuh1SqdslUxO1DZStKkET7MXrwWl Ka3g== X-Gm-Message-State: ALKqPwdrW2QmujCBq12VtG+YROBwdUn7Vn2hxTUzXMalW+ff87uaB49N WZczX/HEfRxAU2049+EVcwNrdMofjQg= X-Google-Smtp-Source: AB8JxZoopez9wvNH6ujTrfmgZfDsSj1tWdDFpbNLCXk+h56ClgQNQ1xJVYeRnFHg9/WFNU0pdSfm5A== X-Received: by 2002:a2e:5559:: with SMTP id j86-v6mr1037959ljb.147.1526488133178; Wed, 16 May 2018 09:28:53 -0700 (PDT) From: Alexander Popov To: kernel-hardening@lists.openwall.com, Kees Cook , PaX Team , Brad Spengler , Ingo Molnar , Andy Lutomirski , Tycho Andersen , Laura Abbott , Mark Rutland , Ard Biesheuvel , Borislav Petkov , Richard Sandiford , Thomas Gleixner , "H . Peter Anvin" , Peter Zijlstra , "Dmitry V . Levin" , Emese Revfy , Jonathan Corbet , Andrey Ryabinin , "Kirill A . Shutemov" , Thomas Garnier , Andrew Morton , Alexei Starovoitov , Josef Bacik , Masami Hiramatsu , Nicholas Piggin , Al Viro , "David S . Miller" , Ding Tianhong , David Woodhouse , Josh Poimboeuf , Steven Rostedt , Dominik Brodowski , Juergen Gross , Linus Torvalds , Greg Kroah-Hartman , Dan Williams , Dave Hansen , Mathias Krause , Vikas Shivappa , Kyle Huey , Dmitry Safonov , Will Deacon , Arnd Bergmann , Florian Weimer , Boris Lukashev , Andrey Konovalov , x86@kernel.org, linux-kernel@vger.kernel.org, alex.popov@linux.com Subject: [PATCH v12 5/6] fs/proc: Show STACKLEAK metrics in the /proc file system Date: Wed, 16 May 2018 19:28:16 +0300 Message-Id: <1526488097-20611-6-git-send-email-alex.popov@linux.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1526488097-20611-1-git-send-email-alex.popov@linux.com> References: <1526488097-20611-1-git-send-email-alex.popov@linux.com> X-Virus-Scanned: ClamAV using ClamSMTP Introduce CONFIG_STACKLEAK_METRICS providing STACKLEAK information about tasks via the /proc file system. In particular, /proc//stack_depth shows the maximum kernel stack consumption for the current and previous syscalls. Although this information is not precise, it can be useful for estimating the STACKLEAK performance impact for your workloads. Signed-off-by: Alexander Popov --- arch/Kconfig | 12 ++++++++++++ arch/x86/kernel/process_32.c | 3 +++ arch/x86/kernel/process_64.c | 3 +++ fs/proc/base.c | 18 ++++++++++++++++++ include/linux/stackleak.h | 3 +++ kernel/stackleak.c | 4 ++++ 6 files changed, 43 insertions(+) diff --git a/arch/Kconfig b/arch/Kconfig index e9d3841..4ebe06f 100644 --- a/arch/Kconfig +++ b/arch/Kconfig @@ -572,6 +572,18 @@ config STACKLEAK_TRACK_MIN_SIZE frame size greater than or equal to this parameter. If unsure, leave the default value 100. +config STACKLEAK_METRICS + bool "Show STACKLEAK metrics in the /proc file system" + depends on GCC_PLUGIN_STACKLEAK + depends on PROC_FS + help + If this is set, STACKLEAK metrics for every task are available in + the /proc file system. In particular, /proc//stack_depth + shows the maximum kernel stack consumption for the current and + previous syscalls. Although this information is not precise, it + can be useful for estimating the STACKLEAK performance impact for + your workloads. + config HAVE_CC_STACKPROTECTOR bool help diff --git a/arch/x86/kernel/process_32.c b/arch/x86/kernel/process_32.c index a19ea44..3f5f6b8 100644 --- a/arch/x86/kernel/process_32.c +++ b/arch/x86/kernel/process_32.c @@ -139,6 +139,9 @@ int copy_thread_tls(unsigned long clone_flags, unsigned long sp, #ifdef CONFIG_GCC_PLUGIN_STACKLEAK p->thread.lowest_stack.val = (unsigned long)end_of_stack(p) + sizeof(unsigned long); +# ifdef CONFIG_STACKLEAK_METRICS + p->thread.lowest_stack.prev_val = p->thread.lowest_stack.val; +# endif #endif if (unlikely(p->flags & PF_KTHREAD)) { diff --git a/arch/x86/kernel/process_64.c b/arch/x86/kernel/process_64.c index f7412c6..a2d1af8 100644 --- a/arch/x86/kernel/process_64.c +++ b/arch/x86/kernel/process_64.c @@ -298,6 +298,9 @@ int copy_thread_tls(unsigned long clone_flags, unsigned long sp, #ifdef CONFIG_GCC_PLUGIN_STACKLEAK p->thread.lowest_stack.val = (unsigned long)end_of_stack(p) + sizeof(unsigned long); +# ifdef CONFIG_STACKLEAK_METRICS + p->thread.lowest_stack.prev_val = p->thread.lowest_stack.val; +# endif #endif savesegment(gs, p->thread.gsindex); diff --git a/fs/proc/base.c b/fs/proc/base.c index 1b2ede6..a4d37a2 100644 --- a/fs/proc/base.c +++ b/fs/proc/base.c @@ -2924,6 +2924,21 @@ static int proc_pid_patch_state(struct seq_file *m, struct pid_namespace *ns, } #endif /* CONFIG_LIVEPATCH */ +#ifdef CONFIG_STACKLEAK_METRICS +static int proc_stack_depth(struct seq_file *m, struct pid_namespace *ns, + struct pid *pid, struct task_struct *task) +{ + unsigned long prev_depth = THREAD_SIZE - + (task->thread.lowest_stack.prev_val & (THREAD_SIZE - 1)); + unsigned long depth = THREAD_SIZE - + (task->thread.lowest_stack.val & (THREAD_SIZE - 1)); + + seq_printf(m, "previous stack depth: %lu\nstack depth: %lu\n", + prev_depth, depth); + return 0; +} +#endif /* CONFIG_STACKLEAK_METRICS */ + /* * Thread groups */ @@ -3025,6 +3040,9 @@ static const struct pid_entry tgid_base_stuff[] = { #ifdef CONFIG_LIVEPATCH ONE("patch_state", S_IRUSR, proc_pid_patch_state), #endif +#ifdef CONFIG_STACKLEAK_METRICS + ONE("stack_depth", S_IRUGO, proc_stack_depth), +#endif }; static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx) diff --git a/include/linux/stackleak.h b/include/linux/stackleak.h index 458c73b..73f18be 100644 --- a/include/linux/stackleak.h +++ b/include/linux/stackleak.h @@ -13,6 +13,9 @@ struct lowest_stack { #ifdef CONFIG_GCC_PLUGIN_STACKLEAK unsigned long val; +# ifdef CONFIG_STACKLEAK_METRICS + unsigned long prev_val; +# endif #endif }; diff --git a/kernel/stackleak.c b/kernel/stackleak.c index 27565db..e49d694 100644 --- a/kernel/stackleak.c +++ b/kernel/stackleak.c @@ -50,6 +50,10 @@ asmlinkage void erase_kstack(void) if (p == boundary) p += sizeof(unsigned long); +#ifdef CONFIG_STACKLEAK_METRICS + current->thread.lowest_stack.prev_val = p; +#endif + /* * So let's write the poison value to the kernel stack. * Start from the address in p and move up till the new boundary.