From patchwork Wed Dec 6 10:26:22 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jinbum Park X-Patchwork-Id: 10095355 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 2722560567 for ; Wed, 6 Dec 2017 10:27:23 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 135AC29C8E for ; Wed, 6 Dec 2017 10:27:23 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0850C29CA1; Wed, 6 Dec 2017 10:27: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=-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 6ED0B29C8E for ; Wed, 6 Dec 2017 10:27:22 +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=adur356GgT9okfCc+zHNHCOSzsalb3BzrRcBKL9LWDU=; b=sew2267Q8JHx0Z p3pL9JZBTbGlqo5SdVG1x4jhqs1y1SSkZjijwPtlVpO5mcyvT5TVkB//1984VH5feURnOheq75vJO 516bSgp9u20RDk9ysYoG6CB8tMaLKFzTroGxL0LY3B1XLKYg9i1kPiynwuiudkbWP6lpcLRYCx78V J/lJkwyWE2PTUmzLC459hfPRL+94d/hFLzGs0qSDjirAUOIx5ovh/n9flrIvVAU+MQ7HjI0s/bZxB u7xmdDgn/G2Yyn9ZYsVCwAwNq2zM6VOZzJkooTgvwHJZRV03RDRwkWBe1XuiujQPdj3xH7Yyi7evz yZTtPUZ5Gv7ERN9+exdQ==; 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 1eMWvA-0006c8-Pk; Wed, 06 Dec 2017 10:27:20 +0000 Received: from mail-pf0-x244.google.com ([2607:f8b0:400e:c00::244]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1eMWug-0005yY-GD for linux-arm-kernel@lists.infradead.org; Wed, 06 Dec 2017 10:27:11 +0000 Received: by mail-pf0-x244.google.com with SMTP id e3so2061529pfi.10 for ; Wed, 06 Dec 2017 02:26:30 -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=j3iWMxxEHsc8hwbqQ/F28rm26z4uOL0dw0oWf99Wx0k=; b=tvvcd6RMoOTPeiaI7YhSGdZNsjUIgpx6ue81uId+6TLKrv4bZk9HblOkiLznX/MZ/8 w0FoHCDbgaDaZEiNdxc0iMj7Ou7iPkxP/f9uGmIq59108oUTy7VG51NqYT2cD14tKTb1 PuFUkNysrfpkxNAuzDOaUvGcKayQ1V6m9K3YTA/rPG+08wMFiImqh+1454L3cR+f8Q+i gYVdgotp/MRywApSe/TWaaykzX0/kOaI8q5eXc3/SgfFX9XcllEYH0q5kuvSLcViYaaa hGgN5yFxhi+QhbSwluN4E2cJWbN/4t39TJuuy9btle0QcbTcpKc4+mRnnB3DHQFVXMtG iXbA== 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=j3iWMxxEHsc8hwbqQ/F28rm26z4uOL0dw0oWf99Wx0k=; b=e97IfcZRBG7sRyI7YJPt2AmKPY+0fleyT4QHwmEf8+WpkSKzapgbqhU5yGk6sKG93k Rff7XA+4sAptKiCiM+SLu7EFT3NNHkZzIIPnect0e4oksu8Y9XARtJGWIcMdwJifKrXb MIHsVPQJZb6oRu/ymnlZjhco1B8dK2FPEcD48P57gc3u6PBFBNxCs5tVdWV7YDzmb123 I+JvgTOorKZWAPvE9qThdUm/ptxQ3TuOVkkIYjBIpPqZqiSyZzJUb8N8iIfry/ixoM++ rCsOMj6D8v1Wvt3zz575lw5hJo0n51TLbKv1ULgctUETcfgXiHIUE0tPLygHO4oIUwd8 mGoQ== X-Gm-Message-State: AJaThX4PI8ac9K25L22Q4GEGC7e4SAw2jpATCED3VhjwDRoEbBSe41Eu iYYiBnETmIfDsz0cxstWUMkTUA== X-Google-Smtp-Source: AGs4zMY3CVnit0FGairf97RLsEE19ChwiNPaZlGBe5rDPJzQZaev16DkdbKdFzTF+UHCL4vLlm14PQ== X-Received: by 10.99.52.65 with SMTP id b62mr20717726pga.383.1512555989484; Wed, 06 Dec 2017 02:26:29 -0800 (PST) Received: from pjb1027-Latitude-E5410 ([58.227.15.43]) by smtp.gmail.com with ESMTPSA id a67sm3604101pgc.5.2017.12.06.02.26.25 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Wed, 06 Dec 2017 02:26:29 -0800 (PST) Date: Wed, 6 Dec 2017 19:26:22 +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 v4 3/3] arm: mm: dump: add checking for writable and executable pages Message-ID: <20171206102622.GA7069@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-20171206_022650_857141_9D91C932 X-CRM114-Status: GOOD ( 19.94 ) 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, 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 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 --- v4: Add boolean for ro_bit, nx_bit into prot_bits to point ro_bit, nx_bit in pg_level. Remove unnecessary %p in WARN_ONCE(). --- arch/arm/Kconfig.debug | 27 ++++++++++++++++++++ arch/arm/include/asm/ptdump.h | 8 ++++++ arch/arm/mm/dump.c | 58 ++++++++++++++++++++++++++++++++++++++++++- arch/arm/mm/init.c | 2 ++ 4 files changed, 94 insertions(+), 1 deletion(-) 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..084779c 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; }; @@ -60,6 +62,8 @@ struct prot_bits { u64 val; const char *set; const char *clear; + bool ro_bit; + bool nx_bit; }; static const struct prot_bits pte_bits[] = { @@ -73,11 +77,13 @@ struct prot_bits { .val = L_PTE_RDONLY, .set = "ro", .clear = "RW", + .ro_bit = true, }, { .mask = L_PTE_XN, .val = L_PTE_XN, .set = "NX", .clear = "x ", + .nx_bit = true, }, { .mask = L_PTE_SHARED, .val = L_PTE_SHARED, @@ -141,11 +147,13 @@ struct prot_bits { .val = L_PMD_SECT_RDONLY | PMD_SECT_AP2, .set = "ro", .clear = "RW", + .ro_bit = true, #elif __LINUX_ARM_ARCH__ >= 6 { .mask = PMD_SECT_APX | PMD_SECT_AP_READ | PMD_SECT_AP_WRITE, .val = PMD_SECT_APX | PMD_SECT_AP_WRITE, .set = " ro", + .ro_bit = true, }, { .mask = PMD_SECT_APX | PMD_SECT_AP_READ | PMD_SECT_AP_WRITE, .val = PMD_SECT_AP_WRITE, @@ -164,6 +172,7 @@ struct prot_bits { .mask = PMD_SECT_AP_READ | PMD_SECT_AP_WRITE, .val = 0, .set = " ro", + .ro_bit = true, }, { .mask = PMD_SECT_AP_READ | PMD_SECT_AP_WRITE, .val = PMD_SECT_AP_WRITE, @@ -182,6 +191,7 @@ struct prot_bits { .val = PMD_SECT_XN, .set = "NX", .clear = "x ", + .nx_bit = true, }, { .mask = PMD_SECT_S, .val = PMD_SECT_S, @@ -194,6 +204,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[] = { @@ -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 %pS\n", + (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); @@ -379,8 +410,13 @@ static void ptdump_initialize(void) for (i = 0; i < ARRAY_SIZE(pg_level); i++) if (pg_level[i].bits) - for (j = 0; j < pg_level[i].num; j++) + for (j = 0; j < pg_level[i].num; j++) { pg_level[i].mask |= pg_level[i].bits[j].mask; + if (pg_level[i].bits[j].ro_bit) + pg_level[i].ro_bit = &pg_level[i].bits[j]; + if (pg_level[i].bits[j].nx_bit) + pg_level[i].nx_bit = &pg_level[i].bits[j]; + } address_markers[2].start_address = VMALLOC_START; } @@ -391,6 +427,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)