From patchwork Fri Dec 1 11:34:47 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jinbum Park X-Patchwork-Id: 10086919 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 EB8876037E for ; Fri, 1 Dec 2017 11:42:31 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D0EFA2A497 for ; Fri, 1 Dec 2017 11:42:31 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C59322A49C; Fri, 1 Dec 2017 11:42: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.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 3D9972A497 for ; Fri, 1 Dec 2017 11:42:30 +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=Vzim5/TeWz0F3kb46iO7I+bbCtG21R20V1JIriW+rPg=; b=MgSA7knrCbo+5S rU4tOT9FZI3h3wliTcBRf+WPsMc8mvfblROsWRPi4IytcHxQUJtKQVl/x7pYOLVmbk8Q+c1x7vVh+ JNqsjX8NQT2YH83M176y2JUN49OxFd5nQvUGnFtWBPTziC2al6fkVfIe7usEwnuYg25paklR0iFrL 9QQPDe+7MXL1TayRkGxIM7jnMvIgFyYUZ0DgRuifbAYSKrWDDL895eae+CrCze9cDdaUTEUJRCnkY aXEoWc5oYzlaFKNFRDkQra5aD3LWC4mSAkmJTGj/eRwGhznMGZckRWwPestNJF+ycW8bQg64vLJ9n PCbHnlzQS3IpKj7uKo7g==; 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 1eKji8-0003Pm-Bz; Fri, 01 Dec 2017 11:42:28 +0000 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1eKji6-0003Np-NN for linux-arm-kernel@bombadil.infradead.org; Fri, 01 Dec 2017 11:42:26 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Type:MIME-Version:Message-ID: Subject:Cc:To:From:Date:Sender:Reply-To:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:In-Reply-To:References:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=TC+GJhfGRexAE043VUlfqrPfEZ0NFTJIf2v2lG+SL48=; b=OxMpyWqobT3TZrV6bBvEBCS1zT HoSB0r2Xape5L32hdJvi5sbWkEc5ZCGogIiUvjrLz5Td/F5jlzlov7YYuCdRUqZ2s2LIxREAleEsf ZGRtZLloo7yAcdrB9bVZD48Kth3r3DG/55oB2Iw5Mc2O8mI6Z/h/q7uwdAjvp+HejTwjkggUSyPtZ L/9Mkv0rIGaBAZZ6Ti1XcK4x2NhmKHem/i7kwVhbOxlhn5OPfc3it6djoLABikx7F5pNZSuZAwU1U N1XrKxIyAHRMpyDnqFGQrbZnhlACxhOyxjjDet8EqMVUqh7JMZH3fU+pwuJQJtSr75jh+2gEt0aMn LyaFBkjQ==; Received: from mail-pf0-x241.google.com ([2607:f8b0:400e:c00::241]) by casper.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1eKjbB-00040D-6F for linux-arm-kernel@lists.infradead.org; Fri, 01 Dec 2017 11:35:19 +0000 Received: by mail-pf0-x241.google.com with SMTP id y89so4536913pfk.0 for ; Fri, 01 Dec 2017 03:34:55 -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=TC+GJhfGRexAE043VUlfqrPfEZ0NFTJIf2v2lG+SL48=; b=u9dX4bPa4D8pQY/Oy4Oy7HRQZPnUb+bPgBvLxqm30gYESfoJSeoM15rIl/e+rX06zO tQZw/alvBsyV9xdwA33U7wklH7sP9Owz3xKMldnUmBP/21545eJ+fFzh8xka+w6zoq8/ ZUTtOh9Ua1YKjrm8el51kh+V2edlO/LCcw8owN5Qe/Fk/yMM88IOeMad5i3CsYH9x4ir 2QgxdsyMbdWeIIoLluJzxr1j2vbdym170QHxHxEOTChk3n9ebW2HNRPUBJWVd58dYxUq TFDz/RcZ47RhXxbHaZUXxpfWWo8UG6X0pSmjil1R2QBoGd7gBgX/9Yqm7znqA1UMoXQL BiuA== 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=TC+GJhfGRexAE043VUlfqrPfEZ0NFTJIf2v2lG+SL48=; b=JgclaJ4MstRiX8RGTEBF9wMdQ9YY3QP3LRzlTELpPajZVKTQO0h2eQAlymDAfkctmG o96yza4+NHDYC/3+Fi9nRgz5j2HdQebRF9jPXmLrlo6vKnpAEXztr/LyRUXIAUkMgjZH NCocaUN9PsspyEr34L/GtEN1wPh8jXXfaY5UPOfo+28ZnVMbsk3yNRww2w6/2MtloY48 6AYvYHTDTLfLEE/KMJz2r9VDu9jI8W7ClU6rDtDtcJtyclrYhRiWlcCMZ2p88eIsTtFa JCiKN12g4SACBnJyOG56h7rgbnp4/wS6h7loARnR8+FriVAzse7najsBpTUU6pftT7t6 hn+Q== X-Gm-Message-State: AJaThX7tIdne3pUPTTh0o487jxrZY5/rmCnRgHNQe/oAHx0edz0naLzF EhnC5b+L6CeicMaJHr4gzpDIcg== X-Google-Smtp-Source: AGs4zMYN7DikUBG4mSqrNyPGJD+5/NJEocM8WpqhTR7vLPG+PK5PGpTryZZgjXnrVWBY+L8IlHE1Gg== X-Received: by 10.99.95.22 with SMTP id t22mr5482488pgb.195.1512128094113; Fri, 01 Dec 2017 03:34:54 -0800 (PST) Received: from pjb1027-Latitude-E5410 ([58.227.15.43]) by smtp.gmail.com with ESMTPSA id x6sm10766533pgq.57.2017.12.01.03.34.50 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Fri, 01 Dec 2017 03:34:53 -0800 (PST) Date: Fri, 1 Dec 2017 20:34:47 +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 v2 3/3] arm: mm: dump: add checking for writable and executable pages Message-ID: <20171201113447.GA5234@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-20171201_113517_384314_AB2ECB44 X-CRM114-Status: GOOD ( 23.46 ) 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 --- arch/arm/Kconfig.debug | 27 +++++++++++++++ arch/arm/include/asm/ptdump.h | 8 +++++ arch/arm/mm/dump.c | 78 +++++++++++++++++++++++++++++++++++++++++++ arch/arm/mm/init.c | 2 ++ 4 files changed, 115 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..a0a4b30 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; }; @@ -226,6 +228,60 @@ static void dump_prot(struct pg_state *st, const struct prot_bits *bits, size_t } } +static inline bool is_prot_ro(struct pg_state *st) +{ + if (st->level < 4) { + #ifdef CONFIG_ARM_LPAE + if ((st->current_prot & + (L_PMD_SECT_RDONLY | PMD_SECT_AP2)) == + (L_PMD_SECT_RDONLY | PMD_SECT_AP2)) + return true; + #elif __LINUX_ARM_ARCH__ >= 6 + if ((st->current_prot & + (PMD_SECT_APX | PMD_SECT_AP_READ | PMD_SECT_AP_WRITE)) == + (PMD_SECT_APX | PMD_SECT_AP_WRITE)) + return true; + #else + if ((st->current_prot & + (PMD_SECT_AP_READ | PMD_SECT_AP_WRITE)) == 0) + return true; + #endif + } else { + if ((st->current_prot & L_PTE_RDONLY) == L_PTE_RDONLY) + return true; + } + + return false; +} + +static inline bool is_prot_nx(struct pg_state *st) +{ + if (st->level < 4) { + if ((st->current_prot & PMD_SECT_XN) == PMD_SECT_XN) + return true; + } else { + if ((st->current_prot & L_PTE_XN) == L_PTE_XN) + return true; + } + + return false; +} + +static void note_prot_wx(struct pg_state *st, unsigned long addr) +{ + if (!st->check_wx) + return; + if (is_prot_ro(st)) + return; + if (is_prot_nx(st)) + 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 +300,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 +424,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 +449,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)