From patchwork Wed Jan 18 13:53:10 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jinbum Park X-Patchwork-Id: 9523943 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 B72FF601C3 for ; Wed, 18 Jan 2017 13:53:43 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A8A6528477 for ; Wed, 18 Jan 2017 13:53:43 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9D372285EE; Wed, 18 Jan 2017 13:53:43 +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.1 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED,FREEMAIL_FROM,RCVD_IN_DNSWL_MED,T_DKIM_INVALID autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 2966028477 for ; Wed, 18 Jan 2017 13:53:43 +0000 (UTC) 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 1cTqgI-00086j-20; Wed, 18 Jan 2017 13:53:42 +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 1cTqgD-00081U-Us for linux-arm-kernel@lists.infradead.org; Wed, 18 Jan 2017 13:53:39 +0000 Received: by mail-pf0-x243.google.com with SMTP id e4so1209465pfg.0 for ; Wed, 18 Jan 2017 05:53:17 -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=pcusnLJRjocTGjjjCd4PAHfQG2F9827vy6UM5+gBUMA=; b=PPB5LshrAx9HQM3vTNPCvJ23ol7fNK2R95lKry8EOFM0Zpmuy3r8/dS+3p+n2XBEHi nAOvrKPJHS1uXxGgGZ73q4F5KH8UUa5lcJ1GjeINLSSrLj9Uf9jKA9OtMY48GHX5I5Oe UOWM/FekXcAR/7X3lkR7bHissH46qN4IsTDwaIVUsu8BSDUfqgRmfMyL5DKYHuzV2+nZ wJBuFwg7Zo01MSwTUiy2IM7akdO3YeKOYGP/gSHxvqawzRxllCIL5M8+fl6/1lArybIw Q0hYGpmCIa3wewqW6bBcW0lMSuOjWwgjfa/G2lYBCAHrwnqtQa5u4f1T1rGMqx3SHeYO YgSw== 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=pcusnLJRjocTGjjjCd4PAHfQG2F9827vy6UM5+gBUMA=; b=Nf/kucyNGt/G1Xes43wUTR6LLE9jpit3LECgC27BxiWPn+tRV+IuJJ6q+0dcpVVCOw b/rhtoDtznPGNXadQEfFnXrRKMhFNHsJmxR+L1GJXxbSsi59c36DnpWVOdR/qXP+dty/ Hu+5ECZQ9BeSNVII8tOT8qx0SJKzFJTzzBCPTON3n3gcYlGF6cTUlUnxSvyGxDzlA11d s/fzDzg/XD3ZAq6UikF9Y5gbp72rV6GSLoGEGdQMwGG8R9FDIEitBNbESG3Mir3WeYGY pw9i1Plzw0LxGR0rxp2b8MnqQw5Ow5EfAeTQWfqVNwgkYRj841wK92S5pOwaaFVmFQGT Yn5A== X-Gm-Message-State: AIkVDXLwwUx0WCpqqzzvKodq9t0mFU/OsKnFDq3cI/hAt3IxCeO3gZCyUig1HiVWYNRLlg== X-Received: by 10.84.253.2 with SMTP id z2mr5328923pll.116.1484747596648; Wed, 18 Jan 2017 05:53:16 -0800 (PST) Received: from pjb1027-Latitude-E5410 ([1.241.139.237]) by smtp.gmail.com with ESMTPSA id b83sm1078599pfe.12.2017.01.18.05.53.12 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Wed, 18 Jan 2017 05:53:16 -0800 (PST) Date: Wed, 18 Jan 2017 22:53:10 +0900 From: Jinbum Park To: linux@armlinux.org.uk Subject: [PATCH] ARM: mm: add testcases for RODATA Message-ID: <20170118135310.GA4733@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-20170118_055338_036057_FA115734 X-CRM114-Status: GOOD ( 24.71 ) 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, f.fainelli@gmail.com, keescook@chromium.org, pawel.moll@arm.com, ard.biesheuvel@linaro.org, kernel-janitors@vger.kernel.org, kernel-hardening@lists.openwall.com, will.deacon@arm.com, linux-kernel@vger.kernel.org, paul.gortmaker@windriver.com, vladimir.murzin@arm.com, andy.gross@linaro.org, labbott@redhat.com, arjan@linux.intel.com, mingo@kernel.org, linux-arm-kernel@lists.infradead.org, jonathan.austin@arm.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 adds testcases for the CONFIG_DEBUG_RODATA option. It's similar to x86's testcases. It tests read-only mapped data and page-size aligned rodata section. Signed-off-by: Jinbum Park --- arch/arm/Kconfig.debug | 5 +++ arch/arm/include/asm/cacheflush.h | 10 +++++ arch/arm/mm/Makefile | 1 + arch/arm/mm/init.c | 6 +++ arch/arm/mm/test_rodata.c | 80 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 102 insertions(+) create mode 100644 arch/arm/mm/test_rodata.c diff --git a/arch/arm/Kconfig.debug b/arch/arm/Kconfig.debug index d83f7c3..511e5e1 100644 --- a/arch/arm/Kconfig.debug +++ b/arch/arm/Kconfig.debug @@ -1749,6 +1749,11 @@ config DEBUG_SET_MODULE_RONX against certain classes of kernel exploits. If in doubt, say "N". +config DEBUG_RODATA_TEST + bool "Testcase for the marking rodata read-only" + ---help--- + This option enables a testcase for the setting rodata read-only. + source "drivers/hwtracing/coresight/Kconfig" endmenu diff --git a/arch/arm/include/asm/cacheflush.h b/arch/arm/include/asm/cacheflush.h index bdd283b..741e2e8 100644 --- a/arch/arm/include/asm/cacheflush.h +++ b/arch/arm/include/asm/cacheflush.h @@ -498,6 +498,16 @@ static inline void set_kernel_text_rw(void) { } static inline void set_kernel_text_ro(void) { } #endif +#ifdef CONFIG_DEBUG_RODATA_TEST +extern const int rodata_test_data; +int rodata_test(void); +#else +static inline int rodata_test(void) +{ + return 0; +} +#endif + void flush_uprobe_xol_access(struct page *page, unsigned long uaddr, void *kaddr, unsigned long len); diff --git a/arch/arm/mm/Makefile b/arch/arm/mm/Makefile index b3dea80..dbb76ff 100644 --- a/arch/arm/mm/Makefile +++ b/arch/arm/mm/Makefile @@ -15,6 +15,7 @@ endif obj-$(CONFIG_ARM_PTDUMP) += dump.o obj-$(CONFIG_MODULES) += proc-syms.o obj-$(CONFIG_DEBUG_VIRTUAL) += physaddr.o +obj-$(CONFIG_DEBUG_RODATA_TEST) += test_rodata.o obj-$(CONFIG_ALIGNMENT_TRAP) += alignment.o obj-$(CONFIG_HIGHMEM) += highmem.o diff --git a/arch/arm/mm/init.c b/arch/arm/mm/init.c index 4127f57..3c15f3b 100644 --- a/arch/arm/mm/init.c +++ b/arch/arm/mm/init.c @@ -716,6 +716,7 @@ void fix_kernmem_perms(void) int __mark_rodata_ro(void *unused) { update_sections_early(ro_perms, ARRAY_SIZE(ro_perms)); + rodata_test(); return 0; } @@ -740,6 +741,11 @@ void set_kernel_text_ro(void) static inline void fix_kernmem_perms(void) { } #endif /* CONFIG_DEBUG_RODATA */ +#ifdef CONFIG_DEBUG_RODATA_TEST +const int rodata_test_data = 0xC3; +EXPORT_SYMBOL_GPL(rodata_test_data); +#endif + void free_tcmmem(void) { #ifdef CONFIG_HAVE_TCM diff --git a/arch/arm/mm/test_rodata.c b/arch/arm/mm/test_rodata.c new file mode 100644 index 0000000..133d092 --- /dev/null +++ b/arch/arm/mm/test_rodata.c @@ -0,0 +1,79 @@ +/* + * test_rodata.c: functional test for mark_rodata_ro function + * + * (C) Copyright 2017 Jinbum Park + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; version 2 + * of the License. + */ +#include +#include + +int rodata_test(void) +{ + unsigned long result; + unsigned long start, end; + + /* test 1: read the value */ + /* If this test fails, some previous testrun has clobbered the state */ + + if (!rodata_test_data) { + pr_err("rodata_test: test 1 fails (start data)\n"); + return -ENODEV; + } + + /* test 2: write to the variable; this should fault */ + /* + * If this test fails, we managed to overwrite the data + * + * This is written in assembly to be able to catch the + * exception that is supposed to happen in the correct + * case + */ + + result = 1; + asm volatile( + "0: str %[zero], [%[rodata_test]]\n" + " mov %[rslt], %[zero]\n" + "1:\n" + ".pushsection .text.fixup,\"ax\"\n" + ".align 2\n" + "2:\n" + "b 1b\n" + ".popsection\n" + ".pushsection __ex_table,\"a\"\n" + ".align 3\n" + ".long 0b, 2b\n" + ".popsection\n" + : [rslt] "=r" (result) + : [zero] "r" (0UL), [rodata_test] "r" (&rodata_test_data) + ); + + if (!result) { + pr_err("rodata_test: test data was not read only\n"); + return -ENODEV; + } + + /* test 3: check the value hasn't changed */ + /* If this test fails, we managed to overwrite the data */ + if (!rodata_test_data) { + pr_err("rodata_test: Test 3 fails (end data)\n"); + return -ENODEV; + } + + /* test 4: check if the rodata section is 4Kb aligned */ + start = (unsigned long)__start_rodata; + end = (unsigned long)__end_rodata; + if (start & (PAGE_SIZE - 1)) { + pr_err("rodata_test: .rodata is not 4k aligned\n"); + return -ENODEV; + } + if (end & (PAGE_SIZE - 1)) { + pr_err("rodata_test: .rodata end is not 4k aligned\n"); + return -ENODEV; + } + + return 0; +}