From patchwork Fri Dec 8 11:00:32 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jinbum Park X-Patchwork-Id: 10102325 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 61049602A0 for ; Fri, 8 Dec 2017 11:01:11 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3394D28B37 for ; Fri, 8 Dec 2017 11:01:11 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2635B28B3C; Fri, 8 Dec 2017 11:01:11 +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=-4.2 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, DKIM_VALID, FREEMAIL_FROM, RCVD_IN_DNSWL_MED autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.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 859AA28B37 for ; Fri, 8 Dec 2017 11:01:10 +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:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-ID:Subject:To:From :Date:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=RH37x5H21jOOiWh4Z808p0PLj5FiYqYFL+v9dpZn9tI=; b=aOkIsJG+drjGBu J7QSJShbdtAkw+lf6/1/mB34YAmASO8ctzxZsqF9jfRvNifoBU4nIpzBbSdjP/E2czzmPXLpjhcLp irTqAayOG8EHIQgTskdoIc+6x5KE+SapXDIpfShBHW2c7VOkJeC4mDyqEk+NWH5sC2DuuvW3SCUDA BK0gBjvpFh30FmaKVyWTIwhiJHIswlX5SC6+MFx8FlDNh9cmw+PU0fvJyWe6D2yIX/OpFJ63YA4qT EJ3upKog83AmCsa5HPbp0eRSw6aWwID6bfgZ+e4G5L1ecKJ9/n1T92HsZImXb/QBfOMjo0dMOvxW+ riguYTtC2mJ3ltgba/1w==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1eNGOt-00055W-LA; Fri, 08 Dec 2017 11:01:03 +0000 Received: from mail-pf0-x243.google.com ([2607:f8b0:400e:c00::243]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1eNGOp-0004sC-RT for linux-arm-kernel@lists.infradead.org; Fri, 08 Dec 2017 11:01:01 +0000 Received: by mail-pf0-x243.google.com with SMTP id u19so7033598pfa.12 for ; Fri, 08 Dec 2017 03:00:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:mime-version:content-disposition :user-agent; bh=OBnrP4MxAZ8dRTcNdGs5tGcWDoU//iyW8HB7QaHYwHU=; b=RdIW9wNngLH/rY28m+P6kY8eIQ+bwo+5wWG9+Rz84hAEVKvd5JS4VFlAbQRWEpP3TY XIxS595o5kqRFVQT+MWehwBoAOgB9IzoJhF/ipZ9Q774pB3hRJQj/+r2Pyqwsrlw4qxm +M6uJvtrGay5U0vSotLlCXNGeZcKk1xgEp2gCz87IRGwxpLjwj3OIetsKCDrDM/YFZsq AZr53BAf0CM4t7VU83HvollaaBZKk8d5SFMHcr/KuNbeaw4XEzaZAxm98pjZpSEXep/W N6cTpcheEszERFULwXPpExSya0VtE7yDB34spIyeWom0pQ1FGUm6oJxjNMq8xPA7b9zD gJ3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition:user-agent; bh=OBnrP4MxAZ8dRTcNdGs5tGcWDoU//iyW8HB7QaHYwHU=; b=FzEmcMuN2UwAJfRISgo9+dFLwQeMEem7Dzg1WGyZjNKqR28NgkdeqE2MTjCOLY2MMd Z0caniL4vhRNKARW1gfE80ugnCn/UNLj57f5maXbsgU+ruC0XAPITkt23EBrcKRWKZZa 4I7fPO1dzyR+qjZK6/bwfj8U+vRDWB3vum2PaRScH8WXGkI8tZGTH+MwhI4/LSRN9FXX 2cU2hUXzAKu1SAXS3MFM7feq6UqASC1Dwy6GCneG1jGFVfkp5COnWhVh7FnSqcTuBIhL /FnpBDyd0jCLzGraJ4HXk4N2vAMG1KAKxJ7prHNhfTtrmHaOjWbcP/v1ElcapBolhrMM S64w== X-Gm-Message-State: AJaThX7fnqlO4i/yiz5LHsGRp5NAgYV0Jd+MPfsVj7KxnJJKXjImGlA2 iVicwwF7tq+iWBSxRovmP3mQYQ== X-Google-Smtp-Source: AGs4zMbxFmQ6nucYUo80HN4K8BoONpqvjziCMtfkxJ24qeQ7xJ0IiXTdth+NQWEdCREnJ8n2SDY7TQ== X-Received: by 10.84.246.137 with SMTP id m9mr29892213pll.130.1512730839581; Fri, 08 Dec 2017 03:00:39 -0800 (PST) Received: from pjb1027-Latitude-E5410 ([58.227.15.43]) by smtp.gmail.com with ESMTPSA id m25sm15139237pfk.37.2017.12.08.03.00.35 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Fri, 08 Dec 2017 03:00:39 -0800 (PST) Date: Fri, 8 Dec 2017 20:00:32 +0900 From: Jinbum Park To: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, kernel-hardening@lists.openwall.com Subject: [kernel-hardening][PATCH v6 1/3] arm: mm: dump: make page table dumping reusable Message-ID: <20171208110032.GA12119@pjb1027-Latitude-E5410> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.21 (2010-09-15) X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20171208_030059_921517_CB114780 X-CRM114-Status: GOOD ( 18.93 ) 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: mark.rutland@arm.com, vladimir.murzin@arm.com, keescook@chromium.org, arnd@arndb.de, gregkh@linuxfoundation.org, linux@armlinux.org.uk, pombredanne@nexb.com, labbott@redhat.com, afzal.mohd.ma@gmail.com 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 This patch refactors the arm page table dumping code, so multiple tables may be registered with the framework. This patch refers below commits of arm64. (4674fdb9f149 ("arm64: mm: dump: make page table dumping reusable")) (4ddb9bf83349 ("arm64: dump: Make ptdump debugfs a separate option")) Reviewed-by: Kees Cook Tested-by: Laura Abbott Reviewed-by: Laura Abbott Signed-off-by: Jinbum Park --- v6: Use SPDX ids in ptdump.h Add Reviewed-by from Kees Cook --- arch/arm/Kconfig.debug | 6 +++- arch/arm/include/asm/ptdump.h | 35 +++++++++++++++++++++++ arch/arm/mm/Makefile | 3 +- arch/arm/mm/dump.c | 65 +++++++++++++++++++------------------------ arch/arm/mm/ptdump_debugfs.c | 34 ++++++++++++++++++++++ 5 files changed, 104 insertions(+), 39 deletions(-) create mode 100644 arch/arm/include/asm/ptdump.h create mode 100644 arch/arm/mm/ptdump_debugfs.c diff --git a/arch/arm/Kconfig.debug b/arch/arm/Kconfig.debug index 17685e1..e7b94db 100644 --- a/arch/arm/Kconfig.debug +++ b/arch/arm/Kconfig.debug @@ -3,10 +3,14 @@ menu "Kernel hacking" source "lib/Kconfig.debug" -config ARM_PTDUMP +config ARM_PTDUMP_CORE + def_bool n + +config ARM_PTDUMP_DEBUGFS bool "Export kernel pagetable layout to userspace via debugfs" depends on DEBUG_KERNEL depends on MMU + select ARM_PTDUMP_CORE select DEBUG_FS ---help--- Say Y here if you want to show the kernel pagetable layout in a diff --git a/arch/arm/include/asm/ptdump.h b/arch/arm/include/asm/ptdump.h new file mode 100644 index 0000000..45d2de3 --- /dev/null +++ b/arch/arm/include/asm/ptdump.h @@ -0,0 +1,35 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (C) 2014 ARM Ltd. */ +#ifndef __ASM_PTDUMP_H +#define __ASM_PTDUMP_H + +#ifdef CONFIG_ARM_PTDUMP_CORE + +#include +#include + +struct addr_marker { + unsigned long start_address; + char *name; +}; + +struct ptdump_info { + struct mm_struct *mm; + const struct addr_marker *markers; + unsigned long base_addr; +}; + +void ptdump_walk_pgd(struct seq_file *s, struct ptdump_info *info); +#ifdef CONFIG_ARM_PTDUMP_DEBUGFS +int ptdump_debugfs_register(struct ptdump_info *info, const char *name); +#else +static inline int ptdump_debugfs_register(struct ptdump_info *info, + const char *name) +{ + return 0; +} +#endif /* CONFIG_ARM_PTDUMP_DEBUGFS */ + +#endif /* CONFIG_ARM_PTDUMP_CORE */ + +#endif /* __ASM_PTDUMP_H */ diff --git a/arch/arm/mm/Makefile b/arch/arm/mm/Makefile index 01bcc33..28be5f4 100644 --- a/arch/arm/mm/Makefile +++ b/arch/arm/mm/Makefile @@ -13,7 +13,8 @@ obj-y += nommu.o obj-$(CONFIG_ARM_MPU) += pmsa-v7.o endif -obj-$(CONFIG_ARM_PTDUMP) += dump.o +obj-$(CONFIG_ARM_PTDUMP_CORE) += dump.o +obj-$(CONFIG_ARM_PTDUMP_DEBUGFS) += ptdump_debugfs.o obj-$(CONFIG_MODULES) += proc-syms.o obj-$(CONFIG_DEBUG_VIRTUAL) += physaddr.o diff --git a/arch/arm/mm/dump.c b/arch/arm/mm/dump.c index fc3b440..8dfe7c3 100644 --- a/arch/arm/mm/dump.c +++ b/arch/arm/mm/dump.c @@ -21,11 +21,7 @@ #include #include #include - -struct addr_marker { - unsigned long start_address; - const char *name; -}; +#include static struct addr_marker address_markers[] = { { MODULES_VADDR, "Modules" }, @@ -335,50 +331,36 @@ static void walk_pud(struct pg_state *st, pgd_t *pgd, unsigned long start) } } -static void walk_pgd(struct seq_file *m) +static void walk_pgd(struct pg_state *st, struct mm_struct *mm, + unsigned long start) { - pgd_t *pgd = swapper_pg_dir; - struct pg_state st; - unsigned long addr; + pgd_t *pgd = pgd_offset(mm, 0UL); unsigned i; - - memset(&st, 0, sizeof(st)); - st.seq = m; - st.marker = address_markers; + unsigned long addr; for (i = 0; i < PTRS_PER_PGD; i++, pgd++) { - addr = i * PGDIR_SIZE; + addr = start + i * PGDIR_SIZE; if (!pgd_none(*pgd)) { - walk_pud(&st, pgd, addr); + walk_pud(st, pgd, addr); } else { - note_page(&st, addr, 1, pgd_val(*pgd), NULL); + note_page(st, addr, 1, pgd_val(*pgd), NULL); } } - - note_page(&st, 0, 0, 0, NULL); } -static int ptdump_show(struct seq_file *m, void *v) +void ptdump_walk_pgd(struct seq_file *m, struct ptdump_info *info) { - walk_pgd(m); - return 0; -} + struct pg_state st = { + .seq = m, + .marker = info->markers, + }; -static int ptdump_open(struct inode *inode, struct file *file) -{ - return single_open(file, ptdump_show, NULL); + walk_pgd(&st, info->mm, info->base_addr); + note_page(&st, 0, 0, 0, NULL); } -static const struct file_operations ptdump_fops = { - .open = ptdump_open, - .read = seq_read, - .llseek = seq_lseek, - .release = single_release, -}; - -static int ptdump_init(void) +static void ptdump_initialize(void) { - struct dentry *pe; unsigned i, j; for (i = 0; i < ARRAY_SIZE(pg_level); i++) @@ -387,9 +369,18 @@ static int ptdump_init(void) pg_level[i].mask |= pg_level[i].bits[j].mask; address_markers[2].start_address = VMALLOC_START; +} - pe = debugfs_create_file("kernel_page_tables", 0400, NULL, NULL, - &ptdump_fops); - return pe ? 0 : -ENOMEM; +static struct ptdump_info kernel_ptdump_info = { + .mm = &init_mm, + .markers = address_markers, + .base_addr = 0, +}; + +static int ptdump_init(void) +{ + ptdump_initialize(); + return ptdump_debugfs_register(&kernel_ptdump_info, + "kernel_page_tables"); } __initcall(ptdump_init); diff --git a/arch/arm/mm/ptdump_debugfs.c b/arch/arm/mm/ptdump_debugfs.c new file mode 100644 index 0000000..be8d87b --- /dev/null +++ b/arch/arm/mm/ptdump_debugfs.c @@ -0,0 +1,34 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include + +#include + +static int ptdump_show(struct seq_file *m, void *v) +{ + struct ptdump_info *info = m->private; + + ptdump_walk_pgd(m, info); + return 0; +} + +static int ptdump_open(struct inode *inode, struct file *file) +{ + return single_open(file, ptdump_show, inode->i_private); +} + +static const struct file_operations ptdump_fops = { + .open = ptdump_open, + .read = seq_read, + .llseek = seq_lseek, + .release = single_release, +}; + +int ptdump_debugfs_register(struct ptdump_info *info, const char *name) +{ + struct dentry *pe; + + pe = debugfs_create_file(name, 0400, NULL, info, &ptdump_fops); + return pe ? 0 : -ENOMEM; + +}