From patchwork Wed Jul 11 20:36:39 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Popov X-Patchwork-Id: 10520491 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 DE76D603D7 for ; Wed, 11 Jul 2018 20:38:23 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BE9C9206E2 for ; Wed, 11 Jul 2018 20:38:23 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B1D8728DE3; Wed, 11 Jul 2018 20:38:23 +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 977F0206E2 for ; Wed, 11 Jul 2018 20:38:22 +0000 (UTC) Received: (qmail 5466 invoked by uid 550); 11 Jul 2018 20:37:59 -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 5401 invoked from network); 11 Jul 2018 20:37:58 -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=/3bLHP10IBcKuaF/5y8jju3XChJVN0U61v2beKUwza8=; b=HEvcohhcWbG6ouQ0sTzU/4ATDsnX/zddEruw/6eVU/qznZeyViHjCmwOM2wpEgrnDS gZgMNYPyEmrYIAo4BAwbqw2DxaFDKBoXPqh1L0HIlmYFxgU50n5Fo/8c1JmD/lIyxuBB TYJtwHCf7/UrwsAh3JTPY39AFDX9N+qUXLFHGPdpyKIuzGT9qrSLrFCQmSziKtehFild uaEoC4Q0P+O9Q51P0V1G4iwTCV9G/BjnY4c+3ao5E4ns9WRbuTcSn3xQ8U7gRC7OFfqk /ANqis+jbyJLhOcebXppTBxSk3Iz2H3ytGY3V/PpCN9diX6wje6xaGJblySrUK8Maj4M wPaw== X-Gm-Message-State: AOUpUlEhg7tF0Wsv6ehJoNMeSZfGVs7ArQudjJ3X4G/G9eXA6vJCa45K /vx5li3TOgA6s7QM3j4I4oC+5yFYzc8= X-Google-Smtp-Source: AAOMgpdevRuZxH9H5aflbBYJIxqTojKlVNux1MT9RtBOYz9iXwJvdefHMyZXWICe/Tp0XMRVGti9lw== X-Received: by 2002:a19:53cc:: with SMTP id h73-v6mr93939lfl.110.1531341466927; Wed, 11 Jul 2018 13:37:46 -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 v14 5/6] fs/proc: Show STACKLEAK metrics in the /proc file system Date: Wed, 11 Jul 2018 23:36:39 +0300 Message-Id: <1531341400-12077-6-git-send-email-alex.popov@linux.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1531341400-12077-1-git-send-email-alex.popov@linux.com> References: <1531341400-12077-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 ++++++++++++ fs/proc/base.c | 18 ++++++++++++++++++ include/linux/sched.h | 1 + include/linux/stackleak.h | 3 +++ kernel/stackleak.c | 4 ++++ 5 files changed, 38 insertions(+) diff --git a/arch/Kconfig b/arch/Kconfig index aa9374b..227a6eb 100644 --- a/arch/Kconfig +++ b/arch/Kconfig @@ -590,6 +590,18 @@ config STACKLEAK_TRACK_MIN_SIZE a stack 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_STACKPROTECTOR bool help diff --git a/fs/proc/base.c b/fs/proc/base.c index aaffc0c..66ee5b7 100644 --- a/fs/proc/base.c +++ b/fs/proc/base.c @@ -2893,6 +2893,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->prev_lowest_stack & (THREAD_SIZE - 1)); + unsigned long depth = THREAD_SIZE - + (task->lowest_stack & (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 */ @@ -2994,6 +3009,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/sched.h b/include/linux/sched.h index bf773d1..5c729ed 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1182,6 +1182,7 @@ struct task_struct { #ifdef CONFIG_GCC_PLUGIN_STACKLEAK unsigned long lowest_stack; + unsigned long prev_lowest_stack; #endif /* diff --git a/include/linux/stackleak.h b/include/linux/stackleak.h index d2560159..b62f133 100644 --- a/include/linux/stackleak.h +++ b/include/linux/stackleak.h @@ -17,6 +17,9 @@ static inline void stackleak_task_init(struct task_struct *t) { #ifdef CONFIG_GCC_PLUGIN_STACKLEAK t->lowest_stack = (unsigned long)end_of_stack(t) + sizeof(unsigned long); +# ifdef CONFIG_STACKLEAK_METRICS + t->prev_lowest_stack = t->lowest_stack; +# endif #endif } diff --git a/kernel/stackleak.c b/kernel/stackleak.c index 659dcc9..f5c4111 100644 --- a/kernel/stackleak.c +++ b/kernel/stackleak.c @@ -39,6 +39,10 @@ asmlinkage void stackleak_erase(void) if (kstack_ptr == boundary) kstack_ptr += sizeof(unsigned long); +#ifdef CONFIG_STACKLEAK_METRICS + current->prev_lowest_stack = kstack_ptr; +#endif + /* * Now write the poison value to the kernel stack. Start from * 'kstack_ptr' and move up till the new 'boundary'. We assume that