From patchwork Mon Dec 4 14:27:09 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jinbum Park X-Patchwork-Id: 10090515 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 0155460327 for ; Mon, 4 Dec 2017 14:27:32 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id ECF8028707 for ; Mon, 4 Dec 2017 14:27:31 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E17E5288B6; Mon, 4 Dec 2017 14:27:31 +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.3 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, 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 BBF9A28707 for ; Mon, 4 Dec 2017 14:27:30 +0000 (UTC) Received: (qmail 29816 invoked by uid 550); 4 Dec 2017 14:27:29 -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 29797 invoked from network); 4 Dec 2017 14:27:27 -0000 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=s0lKnbpcvENTox9p6ppSgLcdzGeIgDC6jOxprWPwMcU=; b=TJ2khB4z6m8AVkKzbwYyTLnPfY9NMKM+13r4NWHusIptLxt+C4HrDMw3OM3+hwxYcQ +FMTNBbOEqthEqZOfKrokShrRwzBC/+Ukv+t3gQYnGbjbPiYBUvIYk/iSf+w2pdQuJwz fZFxIhn08pL2V8uSuwMzObwsL6GzbWtQsRkL68NUA9eYqBxebVgrUoMUJHjzZVMir0QG dwRGL7JV73I9mY/I7rPtCD/ByQoVMrQvUArYx2vqLomDayQLX98CXmi9SLbs91acbbRK QNA6C04TyBylSmpMpiu5W/O0YIvz5u1S0YjPC5lJBOWXE1YvdUIZxTN/4JJVDW1mjPKR JQ0A== 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=s0lKnbpcvENTox9p6ppSgLcdzGeIgDC6jOxprWPwMcU=; b=Sby/P+AxAeENJSeCxvTXGbBpm2ZNFaWiO02SV7Okh13QyWs3Bl5T9dBdRM9QS4+OBU GKtQ96V+37EBuRD+tEOYgPf7MBYtfR/J/1Bx8VGDOVFx2WNk3Z3kwtY4bNicYiZ/lNkA CtPmtzSh2ZahMo/jN9Aq/tlVDSnb1pxpYsBedtcAs7CTzT22jtol7syuOGIezcSlVrSD v71piw7xv1ezelHIQxBqAo+Jzhfx4Vti3MEOX8AOPZZ6gmNLmWpb45dLkyDhTagkC/lh JpgzoYynqXN7eKlnsvdrG6nsoRYyQSASY/THL2F6Gl7CZcbD9f6Nenfr1B6HTTGucixA EtrA== X-Gm-Message-State: AJaThX69P3ccEVumEnBSOgqpakHrXF3WXZAMNK76SvFoSdWLsgtBNVWa PflYW3GGw7M05ZV16dsR+2g= X-Google-Smtp-Source: AGs4zMZRfP5WgD/FP3QQ2PQ2+ymEQMJ53SeVMIh/BAdUOvK3V+/Na12eC8Uk6KUgqO+RP14G/b9KMA== X-Received: by 10.101.69.141 with SMTP id o13mr14392718pgq.125.1512397636174; Mon, 04 Dec 2017 06:27:16 -0800 (PST) Date: Mon, 4 Dec 2017 23:27:09 +0900 From: Jinbum Park To: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, kernel-hardening@lists.openwall.com Cc: afzal.mohd.ma@gmail.com, mark.rutland@arm.com, labbott@redhat.com, linux@armlinux.org.uk, gregkh@linuxfoundation.org, keescook@chromium.org, vladimir.murzin@arm.com, arnd@arndb.de Message-ID: <20171204142709.GA3376@pjb1027-Latitude-E5410> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.21 (2010-09-15) Subject: [kernel-hardening][PATCH v3 3/3] arm: mm: dump: add checking for writable and executable pages X-Virus-Scanned: ClamAV using ClamSMTP Page mappings with full RWX permissions are a security risk. x86, arm64 has an option to walk the page tables and dump any bad pages. (1404d6f13e47 ("arm64: dump: Add checking for writable and exectuable pages")) Add a similar implementation for arm. Signed-off-by: Jinbum Park --- v3: Reuse pg_level, prot_bits to check ro, nx prot. arch/arm/Kconfig.debug | 27 +++++++++++++++++++++++ arch/arm/include/asm/ptdump.h | 8 +++++++ arch/arm/mm/dump.c | 51 +++++++++++++++++++++++++++++++++++++++++++ arch/arm/mm/init.c | 2 ++ 4 files changed, 88 insertions(+) diff --git a/arch/arm/Kconfig.debug b/arch/arm/Kconfig.debug index e7b94db..78a6470 100644 --- a/arch/arm/Kconfig.debug +++ b/arch/arm/Kconfig.debug @@ -20,6 +20,33 @@ config ARM_PTDUMP_DEBUGFS kernel. If in doubt, say "N" +config DEBUG_WX + bool "Warn on W+X mappings at boot" + select ARM_PTDUMP_CORE + ---help--- + Generate a warning if any W+X mappings are found at boot. + + This is useful for discovering cases where the kernel is leaving + W+X mappings after applying NX, as such mappings are a security risk. + + Look for a message in dmesg output like this: + + arm/mm: Checked W+X mappings: passed, no W+X pages found. + + or like this, if the check failed: + + arm/mm: Checked W+X mappings: FAILED, W+X pages found. + + Note that even if the check fails, your kernel is possibly + still fine, as W+X mappings are not a security hole in + themselves, what they do is that they make the exploitation + of other unfixed kernel bugs easier. + + There is no runtime or memory usage effect of this option + once the kernel has booted up - it's a one time check. + + If in doubt, say "Y". + # RMK wants arm kernels compiled with frame pointers or stack unwinding. # If you know what you are doing and are willing to live without stack # traces, you can get a slightly smaller kernel by setting this option to diff --git a/arch/arm/include/asm/ptdump.h b/arch/arm/include/asm/ptdump.h index 3a6c0b7..b6a0162 100644 --- a/arch/arm/include/asm/ptdump.h +++ b/arch/arm/include/asm/ptdump.h @@ -43,6 +43,14 @@ static inline int ptdump_debugfs_register(struct ptdump_info *info, } #endif /* CONFIG_ARM_PTDUMP_DEBUGFS */ +void ptdump_check_wx(void); + #endif /* CONFIG_ARM_PTDUMP_CORE */ +#ifdef CONFIG_DEBUG_WX +#define debug_checkwx() ptdump_check_wx() +#else +#define debug_checkwx() do { } while (0) +#endif + #endif /* __ASM_PTDUMP_H */ diff --git a/arch/arm/mm/dump.c b/arch/arm/mm/dump.c index 43a2bee..3e2e6f0 100644 --- a/arch/arm/mm/dump.c +++ b/arch/arm/mm/dump.c @@ -52,6 +52,8 @@ struct pg_state { unsigned long start_address; unsigned level; u64 current_prot; + bool check_wx; + unsigned long wx_pages; const char *current_domain; }; @@ -194,6 +196,8 @@ struct pg_level { const struct prot_bits *bits; size_t num; u64 mask; + const struct prot_bits *ro_bit; + const struct prot_bits *nx_bit; }; static struct pg_level pg_level[] = { @@ -203,9 +207,17 @@ struct pg_level { }, { /* pmd */ .bits = section_bits, .num = ARRAY_SIZE(section_bits), + #ifdef CONFIG_ARM_LPAE + .ro_bit = section_bits + 1, + #else + .ro_bit = section_bits, + #endif + .nx_bit = section_bits + ARRAY_SIZE(section_bits) - 2, }, { /* pte */ .bits = pte_bits, .num = ARRAY_SIZE(pte_bits), + .ro_bit = pte_bits + 1, + .nx_bit = pte_bits + 2, }, }; @@ -226,6 +238,23 @@ static void dump_prot(struct pg_state *st, const struct prot_bits *bits, size_t } } +static void note_prot_wx(struct pg_state *st, unsigned long addr) +{ + if (!st->check_wx) + return; + if ((st->current_prot & pg_level[st->level].ro_bit->mask) == + pg_level[st->level].ro_bit->val) + return; + if ((st->current_prot & pg_level[st->level].nx_bit->mask) == + pg_level[st->level].nx_bit->val) + return; + + WARN_ONCE(1, "arm/mm: Found insecure W+X mapping at address %p/%pS\n", + (void *)st->start_address, (void *)st->start_address); + + st->wx_pages += (addr - st->start_address) / PAGE_SIZE; +} + static void note_page(struct pg_state *st, unsigned long addr, unsigned int level, u64 val, const char *domain) { @@ -244,6 +273,7 @@ static void note_page(struct pg_state *st, unsigned long addr, unsigned long delta; if (st->current_prot) { + note_prot_wx(st, addr); pt_dump_seq_printf(st->seq, "0x%08lx-0x%08lx ", st->start_address, addr); @@ -367,6 +397,7 @@ void ptdump_walk_pgd(struct seq_file *m, struct ptdump_info *info) struct pg_state st = { .seq = m, .marker = info->markers, + .check_wx = false, }; walk_pgd(&st, info->mm, info->base_addr); @@ -391,6 +422,26 @@ static void ptdump_initialize(void) .base_addr = 0, }; +void ptdump_check_wx(void) +{ + struct pg_state st = { + .seq = NULL, + .marker = (struct addr_marker[]) { + { 0, NULL}, + { -1, NULL}, + }, + .check_wx = true, + }; + + walk_pgd(&st, &init_mm, 0); + note_page(&st, 0, 0, 0, NULL); + if (st.wx_pages) + pr_warn("Checked W+X mappings: FAILED, %lu W+X pages found\n", + st.wx_pages); + else + pr_info("Checked W+X mappings: passed, no W+X pages found\n"); +} + static int ptdump_init(void) { ptdump_initialize(); diff --git a/arch/arm/mm/init.c b/arch/arm/mm/init.c index a1f11a7..bd6f451 100644 --- a/arch/arm/mm/init.c +++ b/arch/arm/mm/init.c @@ -36,6 +36,7 @@ #include #include #include +#include #include #include @@ -738,6 +739,7 @@ static int __mark_rodata_ro(void *unused) void mark_rodata_ro(void) { stop_machine(__mark_rodata_ro, NULL, NULL); + debug_checkwx(); } void set_kernel_text_rw(void)