From patchwork Wed Dec 19 21:33:27 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Igor Stoppa X-Patchwork-Id: 10738169 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 08B6F14E5 for ; Wed, 19 Dec 2018 21:34:28 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id EF66F286B2 for ; Wed, 19 Dec 2018 21:34:27 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E125B28700; Wed, 19 Dec 2018 21:34:27 +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=-5.0 required=2.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI,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 EF12E286B2 for ; Wed, 19 Dec 2018 21:34:26 +0000 (UTC) Received: (qmail 30193 invoked by uid 550); 19 Dec 2018 21:34:18 -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 30108 invoked from network); 19 Dec 2018 21:34:17 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references:reply-to :mime-version:content-transfer-encoding; bh=zzEjOKOluUUWksw+oXnin6HXgBufUkueAJaYjC1prj4=; b=CWCHozxtXPaF/R34zIBV9kgAuN1gg0PddkrPC5aWleCfLISFSDUnCKYlt1SLfa1Qdf AbBcVlgs7IbX/SocNOSs7TJQFHB47Mb19vHQatLapdRgx2CO4a1kc145mUHuA0wziXHH QzZ8yM8QB4lZEUShcVat6wzXvLInx64kvRel1qGAK2FWOTDIZ18UCt14KqSiQ4ShML9x rAO14BDpxcuPoIQYRNZAzrNtOpMAw7VGp3iOilzQ4tI6o+kS0TZkiyLu0KCkKArZ5UO2 dPjUBiO0nyOIiEfyvkENuq9RLsdpeA4AjYW6bWLilccCTnnGGCpqqA/Quth/AjuruGh3 tVQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:reply-to:mime-version:content-transfer-encoding; bh=zzEjOKOluUUWksw+oXnin6HXgBufUkueAJaYjC1prj4=; b=bvCJXLU2lS4QWXWbHwy7J3rX0NVg5sZKB3Z/K3Tgy05/d+byEePuDCAVMNCvtKrIyV 8iF3Przt3qrLsB3I+wtexSUuEcURH78bEd8BJMfr/0aLIbTY9vzRoJ/9DIEhuEyMi7X0 acZTb6qdAsVncB2bGopNUnyaKNmYe9nuVZbZDL5+ff2hAFFHz579dCMQgXo6suSYjtFS 4cJorpITPzl1E2n+mZ3Mu52KIalfiH5iLMNO6HOvj5uGz3sAhiDaf4dNb5SyYNEp9+jc SKdwNopEdl6zqOhs5RHvjJWUNK7v+m9c8d/FG5kHMOOLiSeYbl/GxGBI8TYnJtZ+wapC MXBQ== X-Gm-Message-State: AA+aEWYaxq32Q3bawkHp4XtFyW1gH+vLriMomr+Yl53wmg5C/ddzELKf LG6MVz5bx7KC7oFaPB//sh0= X-Google-Smtp-Source: AFSGD/WxAkE6ImfOjvKiqC6CvFghm5iWxm8HjnpQw4GJB94Y1GY0BzHPXMXipEe8534UoMRhUUvpBw== X-Received: by 2002:a19:6514:: with SMTP id z20mr6123044lfb.31.1545255246483; Wed, 19 Dec 2018 13:34:06 -0800 (PST) From: Igor Stoppa X-Google-Original-From: Igor Stoppa To: Andy Lutomirski , Matthew Wilcox , Peter Zijlstra , Dave Hansen , Mimi Zohar Cc: igor.stoppa@huawei.com, Nadav Amit , Kees Cook , linux-integrity@vger.kernel.org, kernel-hardening@lists.openwall.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 01/12] x86_64: memset_user() Date: Wed, 19 Dec 2018 23:33:27 +0200 Message-Id: <20181219213338.26619-2-igor.stoppa@huawei.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181219213338.26619-1-igor.stoppa@huawei.com> References: <20181219213338.26619-1-igor.stoppa@huawei.com> MIME-Version: 1.0 X-Virus-Scanned: ClamAV using ClamSMTP Create x86_64 specific version of memset for user space, based on clear_user(). This will be used for implementing wr_memset() in the __wr_after_init scenario, where write-rare variables have an alternate mapping for writing. Signed-off-by: Igor Stoppa CC: Andy Lutomirski CC: Nadav Amit CC: Matthew Wilcox CC: Peter Zijlstra CC: Kees Cook CC: Dave Hansen CC: Mimi Zohar CC: linux-integrity@vger.kernel.org CC: kernel-hardening@lists.openwall.com CC: linux-mm@kvack.org CC: linux-kernel@vger.kernel.org --- arch/x86/include/asm/uaccess_64.h | 6 ++++ arch/x86/lib/usercopy_64.c | 54 +++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/arch/x86/include/asm/uaccess_64.h b/arch/x86/include/asm/uaccess_64.h index a9d637bc301d..f194bfce4866 100644 --- a/arch/x86/include/asm/uaccess_64.h +++ b/arch/x86/include/asm/uaccess_64.h @@ -213,4 +213,10 @@ copy_user_handle_tail(char *to, char *from, unsigned len); unsigned long mcsafe_handle_tail(char *to, char *from, unsigned len); +unsigned long __must_check +memset_user(void __user *mem, int c, unsigned long len); + +unsigned long __must_check +__memset_user(void __user *mem, int c, unsigned long len); + #endif /* _ASM_X86_UACCESS_64_H */ diff --git a/arch/x86/lib/usercopy_64.c b/arch/x86/lib/usercopy_64.c index 1bd837cdc4b1..84f8f8a20b30 100644 --- a/arch/x86/lib/usercopy_64.c +++ b/arch/x86/lib/usercopy_64.c @@ -9,6 +9,60 @@ #include #include +/* + * Memset Userspace + */ + +unsigned long __memset_user(void __user *addr, int c, unsigned long size) +{ + long __d0; + unsigned long pattern = 0; + int i; + + for (i = 0; i < 8; i++) + pattern = (pattern << 8) | (0xFF & c); + might_fault(); + /* no memory constraint: gcc doesn't know about this memory */ + stac(); + asm volatile( + " movq %[val], %%rdx\n" + " testq %[size8],%[size8]\n" + " jz 4f\n" + "0: mov %%rdx,(%[dst])\n" + " addq $8,%[dst]\n" + " decl %%ecx ; jnz 0b\n" + "4: movq %[size1],%%rcx\n" + " testl %%ecx,%%ecx\n" + " jz 2f\n" + "1: movb %%dl,(%[dst])\n" + " incq %[dst]\n" + " decl %%ecx ; jnz 1b\n" + "2:\n" + ".section .fixup,\"ax\"\n" + "3: lea 0(%[size1],%[size8],8),%[size8]\n" + " jmp 2b\n" + ".previous\n" + _ASM_EXTABLE_UA(0b, 3b) + _ASM_EXTABLE_UA(1b, 2b) + : [size8] "=&c"(size), [dst] "=&D" (__d0) + : [size1] "r"(size & 7), "[size8]" (size / 8), "[dst]"(addr), + [val] "ri"(pattern) + : "rdx"); + + clac(); + return size; +} +EXPORT_SYMBOL(__memset_user); + +unsigned long memset_user(void __user *to, int c, unsigned long n) +{ + if (access_ok(VERIFY_WRITE, to, n)) + return __memset_user(to, c, n); + return n; +} +EXPORT_SYMBOL(memset_user); + + /* * Zero Userspace */ From patchwork Wed Dec 19 21:33:28 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Igor Stoppa X-Patchwork-Id: 10738175 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id CAC7D6C5 for ; Wed, 19 Dec 2018 21:34:37 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BCAD0286B2 for ; Wed, 19 Dec 2018 21:34:37 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B102C286B4; Wed, 19 Dec 2018 21:34:37 +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=-5.0 required=2.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI,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 74EDB286B2 for ; Wed, 19 Dec 2018 21:34:36 +0000 (UTC) Received: (qmail 30349 invoked by uid 550); 19 Dec 2018 21:34:20 -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 30271 invoked from network); 19 Dec 2018 21:34:19 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references:reply-to :mime-version:content-transfer-encoding; bh=scWZE0nWnwTd+0FVwhDij4gwRZDyVIgA4bUgU9jm4Qw=; b=ECXYO49yQdsxme7Jgttlgkk1OXF/pQaB16kIMgz5hObs81P2rf53rdtsbq9vuqLTnT jX7gpNLiv7n2ncpWcBw+TV1SPHoghJKVMmyBOzVunlBAW61kLMpZr1w3iqUXJQoeIKEd brnyV5ix2RB7j3HEu34jkYRtexvm/NhrNxTfEAvkekM6qgJOc/YlTSrGbQY/Vl1JRFjx vUOH/DytpLM810A0HmxqVAf2g4zZX/YJL1WbFzWl0Mg2GTodhUeSoi+UJUiQ/AAA4RPC Jbrz4ug0xFFizcuiOeNzDKdpaBPg6nJAXr8izbvHwNWU2BmKHoXdLtFkNMw7Vc0D9izd VsJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:reply-to:mime-version:content-transfer-encoding; bh=scWZE0nWnwTd+0FVwhDij4gwRZDyVIgA4bUgU9jm4Qw=; b=SDlvLy9WrCHvuBc55f6q0+yDWNtgFdK1OszkNaoi6iK40gTKnPOLHyGDHsc67uDcdP QdrJLYJ6uvkN6CSMNAD6BhqBMa6fXnMiIIYE6mIcj3J4/AerFywuCU4eZKo+3AgtmOEd 2Z4/byPOGjWvQB8V0nCFtF3G6pfcm1AAdHrTjX5Ovz1QpNLk2Wy7PAch9wIPVRUSCyDj CuwFbeHHNZ6AOEWwVDEE6m06XHKTvP2zAWC7CQYA1GRfLyOqg+7KfEHI6Oe8taQmYIuf DXW+4COD1hqoqi086RwhJ1z2e/2wceQXYztN6GhjZ94ldtgAgRsBOEGF55clHOIJg5dB zd2Q== X-Gm-Message-State: AA+aEWbbzHv+Nq2CJa0T1peRAiaNDwPwVBWk1VglIi8UNDMnkIO911Vf 0reiBQ6kup6iwHM/e+Cc1cw= X-Google-Smtp-Source: AFSGD/UUnkuvBJtrj64z76vEVRzpp/gNTMhY363jcuYcBn4wBrr23AJH+tdTR+9OboCCOslwcSXVpw== X-Received: by 2002:a19:2906:: with SMTP id p6mr5307891lfp.17.1545255248175; Wed, 19 Dec 2018 13:34:08 -0800 (PST) From: Igor Stoppa X-Google-Original-From: Igor Stoppa To: Andy Lutomirski , Matthew Wilcox , Peter Zijlstra , Dave Hansen , Mimi Zohar Cc: igor.stoppa@huawei.com, Nadav Amit , Kees Cook , linux-integrity@vger.kernel.org, kernel-hardening@lists.openwall.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 02/12] __wr_after_init: linker section and label Date: Wed, 19 Dec 2018 23:33:28 +0200 Message-Id: <20181219213338.26619-3-igor.stoppa@huawei.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181219213338.26619-1-igor.stoppa@huawei.com> References: <20181219213338.26619-1-igor.stoppa@huawei.com> MIME-Version: 1.0 X-Virus-Scanned: ClamAV using ClamSMTP Introduce a section and a label for statically allocated write rare data. The label is named "__wr_after_init". As the name implies, after the init phase is completed, this section will be modifiable only by invoking write rare functions. The section must take up a set of full pages. To activate both section and label, the arch must set CONFIG_ARCH_HAS_PRMEM Signed-off-by: Igor Stoppa CC: Andy Lutomirski CC: Nadav Amit CC: Matthew Wilcox CC: Peter Zijlstra CC: Kees Cook CC: Dave Hansen CC: Mimi Zohar CC: linux-integrity@vger.kernel.org CC: kernel-hardening@lists.openwall.com CC: linux-mm@kvack.org CC: linux-kernel@vger.kernel.org --- arch/Kconfig | 15 +++++++++++++++ include/asm-generic/vmlinux.lds.h | 25 +++++++++++++++++++++++++ include/linux/cache.h | 21 +++++++++++++++++++++ init/main.c | 2 ++ 4 files changed, 63 insertions(+) diff --git a/arch/Kconfig b/arch/Kconfig index e1e540ffa979..8668ffec8098 100644 --- a/arch/Kconfig +++ b/arch/Kconfig @@ -802,6 +802,21 @@ config VMAP_STACK the stack to map directly to the KASAN shadow map using a formula that is incorrect if the stack is in vmalloc space. +config ARCH_HAS_PRMEM + def_bool n + help + architecture specific symbol stating that the architecture provides + a back-end function for the write rare operation. + +config PRMEM + bool "Write protect critical data that doesn't need high write speed." + depends on ARCH_HAS_PRMEM + default y + help + If the architecture supports it, statically allocated data which + has been selected for hardening becomes (mostly) read-only. + The selection happens by labelling the data "__wr_after_init". + config ARCH_OPTIONAL_KERNEL_RWX def_bool n diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h index 3d7a6a9c2370..ddb1fd608490 100644 --- a/include/asm-generic/vmlinux.lds.h +++ b/include/asm-generic/vmlinux.lds.h @@ -311,6 +311,30 @@ KEEP(*(__jump_table)) \ __stop___jump_table = .; +/* + * Allow architectures to handle wr_after_init data on their + * own by defining an empty WR_AFTER_INIT_DATA. + * However, it's important that pages containing WR_RARE data do not + * hold anything else, to avoid both accidentally unprotecting something + * that is supposed to stay read-only all the time and also to protect + * something else that is supposed to be writeable all the time. + */ +#ifndef WR_AFTER_INIT_DATA +#ifdef CONFIG_PRMEM +#define WR_AFTER_INIT_DATA(align) \ + . = ALIGN(PAGE_SIZE); \ + __start_wr_after_init = .; \ + . = ALIGN(align); \ + *(.data..wr_after_init) \ + . = ALIGN(PAGE_SIZE); \ + __end_wr_after_init = .; \ + . = ALIGN(align); +#else +#define WR_AFTER_INIT_DATA(align) \ + . = ALIGN(align); +#endif +#endif + /* * Allow architectures to handle ro_after_init data on their * own by defining an empty RO_AFTER_INIT_DATA. @@ -332,6 +356,7 @@ __start_rodata = .; \ *(.rodata) *(.rodata.*) \ RO_AFTER_INIT_DATA /* Read only after init */ \ + WR_AFTER_INIT_DATA(align) /* wr after init */ \ KEEP(*(__vermagic)) /* Kernel version magic */ \ . = ALIGN(8); \ __start___tracepoints_ptrs = .; \ diff --git a/include/linux/cache.h b/include/linux/cache.h index 750621e41d1c..09bd0b9284b6 100644 --- a/include/linux/cache.h +++ b/include/linux/cache.h @@ -31,6 +31,27 @@ #define __ro_after_init __attribute__((__section__(".data..ro_after_init"))) #endif +/* + * __wr_after_init is used to mark objects that cannot be modified + * directly after init (i.e. after mark_rodata_ro() has been called). + * These objects become effectively read-only, from the perspective of + * performing a direct write, like a variable assignment. + * However, they can be altered through a dedicated function. + * It is intended for those objects which are occasionally modified after + * init, however they are modified so seldomly, that the extra cost from + * the indirect modification is either negligible or worth paying, for the + * sake of the protection gained. + */ +#ifndef __wr_after_init +#ifdef CONFIG_PRMEM +#define __wr_after_init \ + __attribute__((__section__(".data..wr_after_init"))) +#else +#define __wr_after_init +#endif +#endif + + #ifndef ____cacheline_aligned #define ____cacheline_aligned __attribute__((__aligned__(SMP_CACHE_BYTES))) #endif diff --git a/init/main.c b/init/main.c index a461150adfb1..a36f2e54f937 100644 --- a/init/main.c +++ b/init/main.c @@ -498,6 +498,7 @@ void __init __weak thread_stack_cache_init(void) void __init __weak mem_encrypt_init(void) { } void __init __weak poking_init(void) { } +void __init __weak wr_poking_init(void) { } bool initcall_debug; core_param(initcall_debug, initcall_debug, bool, 0644); @@ -734,6 +735,7 @@ asmlinkage __visible void __init start_kernel(void) delayacct_init(); poking_init(); + wr_poking_init(); check_bugs(); acpi_subsystem_init(); From patchwork Wed Dec 19 21:33:29 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Igor Stoppa X-Patchwork-Id: 10738179 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id CD9E414E5 for ; Wed, 19 Dec 2018 21:34:50 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BE57F286B2 for ; Wed, 19 Dec 2018 21:34:50 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B25E9286CB; Wed, 19 Dec 2018 21:34:50 +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=-5.0 required=2.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI,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 0F229286B2 for ; Wed, 19 Dec 2018 21:34:45 +0000 (UTC) Received: (qmail 30534 invoked by uid 550); 19 Dec 2018 21:34:21 -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 30445 invoked from network); 19 Dec 2018 21:34:21 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references:reply-to :mime-version:content-transfer-encoding; bh=a4GARTJCVW7jLG2/Ght7MRyZWVbd6ovvI1Dj2egfwMc=; b=dbfUXW85OwLBh0AWXRb7Rg53FgFGDptikU6zcwkC+hhGhlsvpnutIHcDEd/wtdlKpa gX4MH/CHsn+jl14fjNwSWhH36kcHsblgTScMVGeqR/Lgo1ExI/W0l9l7t+8qgqxb6Lxs 9uLAWe2buKqBOxAkBxX5nLxALwdD06Yl594SWveM3vgTFfO4gUope5PRrGDknnG9XqeE Kmdl/DM/nI/lCQDdMxff3p8gPyWkDpLeVtUYmL0tQyKe0KoyjpvtXCaiaDXkpq7YlZ+9 MRYHuQ5JpqIQdly6QAUrhwsalzo9T7OqVtc0VOq3rEevctvQTMJtK4wivWhqFOt0qfqd UJvA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:reply-to:mime-version:content-transfer-encoding; bh=a4GARTJCVW7jLG2/Ght7MRyZWVbd6ovvI1Dj2egfwMc=; b=kBlAavRDWc52r4FZ5J06W9EQu/+y8Hy3lAljfK4EG6BY0oIsudOMbFte+Dpu0l/vFn LO6nudfuh0H+YeU+vGfOj5ow7dzvXHz4/FEW+8OJfMq+JgMhiOd0Q5xbUrcRegRIBEqT d4IcCznnY6St+DtbDzuPLq7ybBfRJ6qVZYoIPHnckEV6tlwQr0wI1R1YVSak6obPktAs aL2lDzK3lLHmrbLYYukmmYV99jPBYeCSPXcFEJctaAGh4LpxkJT4VXZVL/pEhDDGWZU2 LPLlZpc8HlHE5xGin7OzP8/R8/vD5O5IYm+9EwDCx38c3bv2XwND6iZSu8BEKOBMziv/ C3Pg== X-Gm-Message-State: AA+aEWY3Oh8+j39P0tS2jwBHbwmmb9Kec3VBttmyBLjRDfl6tDvef1Np kbK9F9U5z2d1GDxm9H68Bm4= X-Google-Smtp-Source: AFSGD/VZbcj5I0PidtfgNZ3Lh1artAeHuFU5euFIwEOUPsXo/4L+9qvccqJSpUN7B7uqhIGBhtnvLA== X-Received: by 2002:a19:cc46:: with SMTP id c67mr13252319lfg.145.1545255249984; Wed, 19 Dec 2018 13:34:09 -0800 (PST) From: Igor Stoppa X-Google-Original-From: Igor Stoppa To: Andy Lutomirski , Matthew Wilcox , Peter Zijlstra , Dave Hansen , Mimi Zohar Cc: igor.stoppa@huawei.com, Nadav Amit , Kees Cook , linux-integrity@vger.kernel.org, kernel-hardening@lists.openwall.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 03/12] __wr_after_init: generic header Date: Wed, 19 Dec 2018 23:33:29 +0200 Message-Id: <20181219213338.26619-4-igor.stoppa@huawei.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181219213338.26619-1-igor.stoppa@huawei.com> References: <20181219213338.26619-1-igor.stoppa@huawei.com> MIME-Version: 1.0 X-Virus-Scanned: ClamAV using ClamSMTP The header provides: - the generic part of the write rare functionality for static data - the dummy functionality, in case an arch doesn't support write rare or the functionality is disabled The basic functions are: - wr_memset(): write rare counterpart of memset() - wr_memcpy(): write rare counterpart of memcpy() - wr_assign(): write rare counterpart of the assignment ('=') operator - wr_rcu_assign_pointer(): write rare counterpart of rcu_assign_pointer() Signed-off-by: Igor Stoppa CC: Andy Lutomirski CC: Nadav Amit CC: Matthew Wilcox CC: Peter Zijlstra CC: Kees Cook CC: Dave Hansen CC: Mimi Zohar CC: linux-integrity@vger.kernel.org CC: kernel-hardening@lists.openwall.com CC: linux-mm@kvack.org CC: linux-kernel@vger.kernel.org --- include/linux/prmem.h | 142 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 include/linux/prmem.h diff --git a/include/linux/prmem.h b/include/linux/prmem.h new file mode 100644 index 000000000000..7b8f3a054d97 --- /dev/null +++ b/include/linux/prmem.h @@ -0,0 +1,142 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * prmem.h: Header for memory protection library + * + * (C) Copyright 2018 Huawei Technologies Co. Ltd. + * Author: Igor Stoppa + * + * Support for: + * - statically allocated write rare data + */ + +#ifndef _LINUX_PRMEM_H +#define _LINUX_PRMEM_H + +#include +#include +#include +#include +#include +#include +#include +#include + +/** + * memtst() - test n bytes of the source to match the c value + * @p: beginning of the memory to test + * @c: byte to compare against + * @len: amount of bytes to test + * + * Returns 0 on success, non-zero otherwise. + */ +static inline int memtst(void *p, int c, __kernel_size_t len) +{ + __kernel_size_t i; + + for (i = 0; i < len; i++) { + u8 d = *(i + (u8 *)p) - (u8)c; + + if (unlikely(d)) + return d; + } + return 0; +} + + +#ifndef CONFIG_PRMEM + +static inline void *wr_memset(void *p, int c, __kernel_size_t len) +{ + return memset(p, c, len); +} + +static inline void *wr_memcpy(void *p, const void *q, __kernel_size_t size) +{ + return memcpy(p, q, size); +} + +#define wr_assign(var, val) ((var) = (val)) + +#define wr_rcu_assign_pointer(p, v) \ + rcu_assign_pointer(p, v) + +#else + +/* + * If CONFIG_PRMEM is enabled, the ARCH code must provide an + * implementation for __wr_op() + */ + +enum wr_op_type { + WR_MEMCPY, + WR_MEMSET, + WR_OPS_NUMBER, +}; + +void *__wr_op(unsigned long dst, unsigned long src, __kernel_size_t len, + enum wr_op_type op); + +/** + * wr_memset() - sets n bytes of the destination to the c value + * @p: beginning of the memory to write to + * @c: byte to replicate + * @len: amount of bytes to copy + * + * Returns true on success, false otherwise. + */ +static inline void *wr_memset(void *p, int c, __kernel_size_t len) +{ + return __wr_op((unsigned long)p, (unsigned long)c, len, WR_MEMSET); +} + +/** + * wr_memcpy() - copyes n bytes from source to destination + * @dst: beginning of the memory to write to + * @src: beginning of the memory to read from + * @n_bytes: amount of bytes to copy + * + * Returns pointer to the destination + */ +static inline void *wr_memcpy(void *p, const void *q, __kernel_size_t size) +{ + return __wr_op((unsigned long)p, (unsigned long)q, size, WR_MEMCPY); +} + +/** + * wr_assign() - sets a write-rare variable to a specified value + * @var: the variable to set + * @val: the new value + * + * Returns: the variable + * + * Note: it might be possible to optimize this, to use wr_memset in some + * cases (maybe with NULL?). + */ + +#define wr_assign(var, val) ({ \ + typeof(var) tmp = (typeof(var))val; \ + \ + wr_memcpy(&var, &tmp, sizeof(var)); \ + var; \ +}) + +/** + * wr_rcu_assign_pointer() - initialize a pointer in rcu mode + * @p: the rcu pointer - it MUST be aligned to a machine word + * @v: the new value + * + * Returns the value assigned to the rcu pointer. + * + * It is provided as macro, to match rcu_assign_pointer() + * The rcu_assign_pointer() is implemented as equivalent of: + * + * smp_mb(); + * WRITE_ONCE(); + */ +#define wr_rcu_assign_pointer(p, v) ({ \ + smp_mb(); \ + wr_assign(p, v); \ + p; \ +}) +#endif +#endif From patchwork Wed Dec 19 21:33:30 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Igor Stoppa X-Patchwork-Id: 10738187 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 827CE6C5 for ; Wed, 19 Dec 2018 21:34:58 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 71553286B2 for ; Wed, 19 Dec 2018 21:34:58 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 643D1286B4; Wed, 19 Dec 2018 21:34:58 +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=-5.0 required=2.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI,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 13FE0286B2 for ; Wed, 19 Dec 2018 21:34:56 +0000 (UTC) Received: (qmail 30713 invoked by uid 550); 19 Dec 2018 21:34:23 -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 30626 invoked from network); 19 Dec 2018 21:34:22 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references:reply-to :mime-version:content-transfer-encoding; bh=yHB4k0lvmhU4nXv0IUtjgNRdUGapAYzZQcu+eQuvDdA=; b=kOnaqwp5GBEPCU0a21wgkz/EG5Ts/3mLyDdMp2Mo/NK9HQoGQ8NjvOhrJz+z5RPlwM T3QOLqQzntFpKjHk3dHoLVRsqc2EWGa9YtNeI4cU0zgXLyzfHZhUK8yaDT2ruKWD1LRU KMRBn6v5H2PL0wB2hxA1fqq2DmdHU4ical+SkVHpj5UTMBZHMB+zEleQP1z8EanXyix0 qlv3x63u3GHJt7IKyNKSmuEGyqRdc8eUtV5+ala5HgnVpVM03uQ1OaKjeCJE8KK6dFJ/ oXN2McI5XOKo5TOxG5hc0y2/hAi9i+p/IVDfegOJ2LWqyEzTTUFoZ96howTd3QfExt8H aVPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:reply-to:mime-version:content-transfer-encoding; bh=yHB4k0lvmhU4nXv0IUtjgNRdUGapAYzZQcu+eQuvDdA=; b=QvX3V96s7RtutFvjpdY2OSQClgcA7F1bRDgtfBWuqNm3nhsgQVlOm+6oB0TKIcRx+0 j0IayG69OqHN9JoFqMOOeVGWkEBn2TOq4DUTTLceWlC/zsJv/ZOZFFY+WpASg4yZcUOZ qFUFtWfViJjsAVgxAb4M/sjLQgSL7wPZMSOrPl1wvTo31+4mk6BdXN7J8zbr2PWG57kP ZwP+KhvQ2v7LBajwr9DW4vHchtzF3FR52RkIsgAjpGYwvAa6LUXbYh3HBQMoIixDOWkz yKeLPh03aOFnCE1s5x/B+05OYXHokPi6hO0aDj9jOfwvbZW7FI0HBRG5d1eB4bbcy4cZ FOhg== X-Gm-Message-State: AA+aEWYpS3MjQIF6rwEPMqpeNSeGvB2KlXDzzt+ByOTx1pDzwn3XwUmq pYJguMiAUPcwALVn+kTxfCY= X-Google-Smtp-Source: AFSGD/Uo61rA/JVaTfVFoo4LkxPpQtbW+kf75l1G+fg6IG3iNHxg6ZllSWLW1R02pnii+GSEQCqSOA== X-Received: by 2002:a2e:3012:: with SMTP id w18-v6mr13490729ljw.75.1545255251599; Wed, 19 Dec 2018 13:34:11 -0800 (PST) From: Igor Stoppa X-Google-Original-From: Igor Stoppa To: Andy Lutomirski , Matthew Wilcox , Peter Zijlstra , Dave Hansen , Mimi Zohar Cc: igor.stoppa@huawei.com, Nadav Amit , Kees Cook , linux-integrity@vger.kernel.org, kernel-hardening@lists.openwall.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 04/12] __wr_after_init: x86_64: __wr_op Date: Wed, 19 Dec 2018 23:33:30 +0200 Message-Id: <20181219213338.26619-5-igor.stoppa@huawei.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181219213338.26619-1-igor.stoppa@huawei.com> References: <20181219213338.26619-1-igor.stoppa@huawei.com> MIME-Version: 1.0 X-Virus-Scanned: ClamAV using ClamSMTP Architecture-specific implementation of the core write rare operation. The implementation is based on code from Andy Lutomirski and Nadav Amit for patching the text on x86 [here goes reference to commits, once merged] The modification of write protected data is done through an alternate mapping of the same pages, as writable. This mapping is persistent, but active only for a core that is performing a write rare operation. And only for the duration of said operation. Local interrupts are disabled, while the alternate mapping is active. In theory, it could introduce a non-predictable delay, in a preemptible system, however the amount of data to be altered is likely to be far smaller than a page. Signed-off-by: Igor Stoppa CC: Andy Lutomirski CC: Nadav Amit CC: Matthew Wilcox CC: Peter Zijlstra CC: Kees Cook CC: Dave Hansen CC: Mimi Zohar CC: linux-integrity@vger.kernel.org CC: kernel-hardening@lists.openwall.com CC: linux-mm@kvack.org CC: linux-kernel@vger.kernel.org --- arch/x86/Kconfig | 1 + arch/x86/mm/Makefile | 2 + arch/x86/mm/prmem.c | 120 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 123 insertions(+) create mode 100644 arch/x86/mm/prmem.c diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 8689e794a43c..e5e4fc4fa5c2 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -32,6 +32,7 @@ config X86_64 select SWIOTLB select X86_DEV_DMA_OPS select ARCH_HAS_SYSCALL_WRAPPER + select ARCH_HAS_PRMEM # # Arch settings diff --git a/arch/x86/mm/Makefile b/arch/x86/mm/Makefile index 4b101dd6e52f..66652de1e2c7 100644 --- a/arch/x86/mm/Makefile +++ b/arch/x86/mm/Makefile @@ -53,3 +53,5 @@ obj-$(CONFIG_PAGE_TABLE_ISOLATION) += pti.o obj-$(CONFIG_AMD_MEM_ENCRYPT) += mem_encrypt.o obj-$(CONFIG_AMD_MEM_ENCRYPT) += mem_encrypt_identity.o obj-$(CONFIG_AMD_MEM_ENCRYPT) += mem_encrypt_boot.o + +obj-$(CONFIG_PRMEM) += prmem.o diff --git a/arch/x86/mm/prmem.c b/arch/x86/mm/prmem.c new file mode 100644 index 000000000000..fc367551e736 --- /dev/null +++ b/arch/x86/mm/prmem.c @@ -0,0 +1,120 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * prmem.c: Memory Protection Library + * + * (C) Copyright 2017-2018 Huawei Technologies Co. Ltd. + * Author: Igor Stoppa + */ + +#include +#include +#include +#include +#include +#include +#include + +static __ro_after_init bool wr_ready; +static __ro_after_init struct mm_struct *wr_poking_mm; +static __ro_after_init unsigned long wr_poking_base; + +/* + * The following two variables are statically allocated by the linker + * script at the the boundaries of the memory region (rounded up to + * multiples of PAGE_SIZE) reserved for __wr_after_init. + */ +extern long __start_wr_after_init; +extern long __end_wr_after_init; + +static inline bool is_wr_after_init(unsigned long ptr, __kernel_size_t size) +{ + unsigned long start = (unsigned long)&__start_wr_after_init; + unsigned long end = (unsigned long)&__end_wr_after_init; + unsigned long low = ptr; + unsigned long high = ptr + size; + + return likely(start <= low && low <= high && high <= end); +} + +void *__wr_op(unsigned long dst, unsigned long src, __kernel_size_t len, + enum wr_op_type op) +{ + temporary_mm_state_t prev; + unsigned long offset; + unsigned long wr_poking_addr; + + /* Confirm that the writable mapping exists. */ + if (WARN_ONCE(!wr_ready, "No writable mapping available")) + return (void *)dst; + + if (WARN_ONCE(op >= WR_OPS_NUMBER, "Invalid WR operation.") || + WARN_ONCE(!is_wr_after_init(dst, len), "Invalid WR range.")) + return (void *)dst; + + offset = dst - (unsigned long)&__start_wr_after_init; + wr_poking_addr = wr_poking_base + offset; + local_irq_disable(); + prev = use_temporary_mm(wr_poking_mm); + + if (op == WR_MEMCPY) + copy_to_user((void __user *)wr_poking_addr, (void *)src, len); + else if (op == WR_MEMSET) + memset_user((void __user *)wr_poking_addr, (u8)src, len); + + unuse_temporary_mm(prev); + local_irq_enable(); + return (void *)dst; +} + +#define TB (1UL << 40) + +struct mm_struct *copy_init_mm(void); +void __init wr_poking_init(void) +{ + unsigned long start = (unsigned long)&__start_wr_after_init; + unsigned long end = (unsigned long)&__end_wr_after_init; + unsigned long i; + unsigned long wr_range; + + wr_poking_mm = copy_init_mm(); + if (WARN_ONCE(!wr_poking_mm, "No alternate mapping available.")) + return; + + wr_range = round_up(end - start, PAGE_SIZE); + + /* Randomize the poking address base*/ + wr_poking_base = TASK_UNMAPPED_BASE + + (kaslr_get_random_long("Write Rare Poking") & PAGE_MASK) % + (TASK_SIZE - (TASK_UNMAPPED_BASE + wr_range)); + + /* + * Place 64TB of kernel address space within 128TB of user address + * space, at a random page aligned offset. + */ + wr_poking_base = (((unsigned long)kaslr_get_random_long("WR Poke")) & + PAGE_MASK) % (64 * _BITUL(40)); + + /* Create alternate mapping for the entire wr_after_init range. */ + for (i = start; i < end; i += PAGE_SIZE) { + struct page *page; + spinlock_t *ptl; + pte_t pte; + pte_t *ptep; + unsigned long wr_poking_addr; + + page = virt_to_page(i); + if (WARN_ONCE(!page, "WR memory without physical page")) + return; + wr_poking_addr = i - start + wr_poking_base; + + /* The lock is not needed, but avoids open-coding. */ + ptep = get_locked_pte(wr_poking_mm, wr_poking_addr, &ptl); + if (WARN_ONCE(!ptep, "No pte for writable mapping")) + return; + + pte = mk_pte(page, PAGE_KERNEL); + set_pte_at(wr_poking_mm, wr_poking_addr, ptep, pte); + spin_unlock(ptl); + } + wr_ready = true; +} From patchwork Wed Dec 19 21:33:31 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Igor Stoppa X-Patchwork-Id: 10738199 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id B227214E5 for ; Wed, 19 Dec 2018 21:35:10 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A4847286B2 for ; Wed, 19 Dec 2018 21:35:10 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 98534286B4; Wed, 19 Dec 2018 21:35:10 +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=-5.0 required=2.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI,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 AFECE286B2 for ; Wed, 19 Dec 2018 21:35:09 +0000 (UTC) Received: (qmail 31876 invoked by uid 550); 19 Dec 2018 21:34:26 -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 31789 invoked from network); 19 Dec 2018 21:34:24 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references:reply-to :mime-version:content-transfer-encoding; bh=24igech0ckFrxBXxxwQUZIyc9LBZ6R5ZtTba/TaHc04=; b=PsrHxLPqAAOHeNu5dAOofHFnXp1bLF9bdTdCkJocLpMY34bZfpnS8iWf5Dw2UELMym Gs5mW3HgNBDw5r9JwJTc0Fq3/dkKkbXJS7j9ubeXSvaALWLkByds7T+R2raJoj5RB0Us PON54gmyVQYYU1Mhmt0vzo7LMuv0p5iO1KZA6Qk3Upr5Y64fRgaifZvHyDj0R8B9QWJl BH+63gFqXdkbK13Syi/DwqnOPOJoBavpGBrbHbeAijtUwGiWjznREdZkOCpw0ARGA5FE 7YraB7oodasyYu4ih5gVedVO/3YOfqhU5mGxwIyA1M6Q8keXB3YNZROEWIyRN9g1/NkH hOCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:reply-to:mime-version:content-transfer-encoding; bh=24igech0ckFrxBXxxwQUZIyc9LBZ6R5ZtTba/TaHc04=; b=UeuH+IvQag916NEgvyfIvRzEdGWwJveerf5T494ilTPvrfI+hSR1A9tdSRI3aogQKo mI16Dsg+x8tWPW1RbEBpvXKtX5iklf3JQCgfbG+riefowlwocB60LZnxbl13w+nEMpom A3i2rp0YFPa7t6ZTPU6NacyxkNOzFTVmsrdko09XXRUG0I5ucbdPgu9O/M6OvVSAj6d6 siBiBtwtsp8A3dB1fduyHxu8F6eFOuy6ZBdaXNsD9wZKGIdXTT4BqwXKpNNblmi90CQZ 0coi1ZsoKXgq+t32ZnVA+43sNNViJ7kszSGRXG8SFGISYuR+VCDPlvMU9VI9+xqrNtmG WAXg== X-Gm-Message-State: AA+aEWZpTMyzRC2EVss/vAbNqXgPJrfG+Viv6wbU+l+tkbtYJP8JycZC MZf03o95HKeekvd5DYrRK3k= X-Google-Smtp-Source: AFSGD/XZWonxcWlW1GBfKWAbMcZ0oFNcTXt0omhVGDk3miIbt6GEElW3TXO8yTUpmBSwSEymuJbaRw== X-Received: by 2002:a19:910d:: with SMTP id t13mr12712428lfd.98.1545255253625; Wed, 19 Dec 2018 13:34:13 -0800 (PST) From: Igor Stoppa X-Google-Original-From: Igor Stoppa To: Andy Lutomirski , Matthew Wilcox , Peter Zijlstra , Dave Hansen , Mimi Zohar Cc: igor.stoppa@huawei.com, Nadav Amit , Kees Cook , linux-integrity@vger.kernel.org, kernel-hardening@lists.openwall.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 05/12] __wr_after_init: x86_64: debug writes Date: Wed, 19 Dec 2018 23:33:31 +0200 Message-Id: <20181219213338.26619-6-igor.stoppa@huawei.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181219213338.26619-1-igor.stoppa@huawei.com> References: <20181219213338.26619-1-igor.stoppa@huawei.com> MIME-Version: 1.0 X-Virus-Scanned: ClamAV using ClamSMTP After each write operation, confirm that it was successful, otherwise generate a warning. Signed-off-by: Igor Stoppa CC: Andy Lutomirski CC: Nadav Amit CC: Matthew Wilcox CC: Peter Zijlstra CC: Kees Cook CC: Dave Hansen CC: Mimi Zohar CC: linux-integrity@vger.kernel.org CC: kernel-hardening@lists.openwall.com CC: linux-mm@kvack.org CC: linux-kernel@vger.kernel.org --- arch/x86/mm/prmem.c | 9 ++++++++- mm/Kconfig.debug | 8 ++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/arch/x86/mm/prmem.c b/arch/x86/mm/prmem.c index fc367551e736..9d98525c687a 100644 --- a/arch/x86/mm/prmem.c +++ b/arch/x86/mm/prmem.c @@ -60,7 +60,14 @@ void *__wr_op(unsigned long dst, unsigned long src, __kernel_size_t len, copy_to_user((void __user *)wr_poking_addr, (void *)src, len); else if (op == WR_MEMSET) memset_user((void __user *)wr_poking_addr, (u8)src, len); - +#ifdef CONFIG_DEBUG_PRMEM + if (op == WR_MEMCPY) + VM_WARN_ONCE(memcmp((void *)dst, (void *)src, len), + "Failed wr_memcpy()"); + else if (op == WR_MEMSET) + VM_WARN_ONCE(memtst((void *)dst, (u8)src, len), + "Failed wr_memset()"); +#endif unuse_temporary_mm(prev); local_irq_enable(); return (void *)dst; diff --git a/mm/Kconfig.debug b/mm/Kconfig.debug index 9a7b8b049d04..b10305cfac3c 100644 --- a/mm/Kconfig.debug +++ b/mm/Kconfig.debug @@ -94,3 +94,11 @@ config DEBUG_RODATA_TEST depends on STRICT_KERNEL_RWX ---help--- This option enables a testcase for the setting rodata read-only. + +config DEBUG_PRMEM + bool "Verify each write rare operation." + depends on PRMEM + default n + help + After any write rare operation, compares the data written with the + value provided by the caller. From patchwork Wed Dec 19 21:33:32 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Igor Stoppa X-Patchwork-Id: 10738207 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 3D9236C5 for ; Wed, 19 Dec 2018 21:35:23 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2F7DA286B3 for ; Wed, 19 Dec 2018 21:35:23 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 23B20286B4; Wed, 19 Dec 2018 21:35: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=-5.0 required=2.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI,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 38B41286B2 for ; Wed, 19 Dec 2018 21:35:22 +0000 (UTC) Received: (qmail 32067 invoked by uid 550); 19 Dec 2018 21:34:27 -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 31981 invoked from network); 19 Dec 2018 21:34:26 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references:reply-to :mime-version:content-transfer-encoding; bh=xeUBKKu9pM2Fl/EWnsAPb2mBNLFsikzJ4KNY/hC/k1s=; b=TZIKF4B0i3koI0BW8axIrrGf4qCRSgVc8vuBMQJuNZ1qpAqlmGEW+bOBxwCcDK/JY3 mNOXn/Lytz0LlgdZK6s3JlI7atnuH7qez7sfQofkmzbIV2f2ZxskOsfp9NEhU5NaQTvN tFggyM8mgzKvz7s8hU9qqgBsaUM8BAmUcWFYR5zWO8a3RW+FvppFPc9fZecShjwJLJh/ lBOqekityzb82UV0NrQDkyhgmPQox8nu6C1HKMReNtzcl/z9UGLkONLrC6fFakRwXStK q/7/hXtBDqCHpIXqfHQ2ugAPMEwgUpSvY+la3Uaoflk84Jo7xJduNGkOWP+FAq0r1yMl V6EQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:reply-to:mime-version:content-transfer-encoding; bh=xeUBKKu9pM2Fl/EWnsAPb2mBNLFsikzJ4KNY/hC/k1s=; b=PXvq3NDhE05yKGL9AiKIzGqgYoNIWKPynsvRSYOhkF5Wua8ijwRCr2AzYRS/WYW7+B 34Lok5ZEe8H91dNL8PVEnBLxNCGFoLGmPPWer6sg+3g1xk0GwyT4MQcfxJgSh2IT8mMD a9LUfe7aVw9A0q9eidYjQUPC7+EYzVu0JCB6CgF+tKqwJ9vvVoVdm6ZL+Q4R2zJJowp+ 73HX2+VniANrvyTGIS9mq4haJfTBUAAqmGEYYptafDGk7pLdxE169+rOgJfGvUzaPmLy Tez/q54hksfZduQ9PmYzn+0gDipyDcM0UC6QkbsLeAAXpcLGR6qtjUWuwEjlWY5HvPvI Yvpg== X-Gm-Message-State: AA+aEWbIWRxGr7MPE5TCpeSE/7xawePHtPXXXkcQ4OwgG+qv09uIxKYq 9nmSiPfdYmbGYoDNqnVcmVM= X-Google-Smtp-Source: AFSGD/VdVCH5iML0R9nPvIbiMPCBw0CGtT0CPJuGwuUVLfPeSRuvFLi8jSpphGe6vqzW2o8lG9K3Jw== X-Received: by 2002:a2e:890b:: with SMTP id d11-v6mr4048231lji.113.1545255255391; Wed, 19 Dec 2018 13:34:15 -0800 (PST) From: Igor Stoppa X-Google-Original-From: Igor Stoppa To: Andy Lutomirski , Matthew Wilcox , Peter Zijlstra , Dave Hansen , Mimi Zohar Cc: igor.stoppa@huawei.com, Nadav Amit , Kees Cook , linux-integrity@vger.kernel.org, kernel-hardening@lists.openwall.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 06/12] __wr_after_init: Documentation: self-protection Date: Wed, 19 Dec 2018 23:33:32 +0200 Message-Id: <20181219213338.26619-7-igor.stoppa@huawei.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181219213338.26619-1-igor.stoppa@huawei.com> References: <20181219213338.26619-1-igor.stoppa@huawei.com> MIME-Version: 1.0 X-Virus-Scanned: ClamAV using ClamSMTP Update the self-protection documentation, to mention also the use of the __wr_after_init attribute. Signed-off-by: Igor Stoppa CC: Andy Lutomirski CC: Nadav Amit CC: Matthew Wilcox CC: Peter Zijlstra CC: Kees Cook CC: Dave Hansen CC: Mimi Zohar CC: linux-integrity@vger.kernel.org CC: kernel-hardening@lists.openwall.com CC: linux-mm@kvack.org CC: linux-kernel@vger.kernel.org --- Documentation/security/self-protection.rst | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/Documentation/security/self-protection.rst b/Documentation/security/self-protection.rst index f584fb74b4ff..df2614bc25b9 100644 --- a/Documentation/security/self-protection.rst +++ b/Documentation/security/self-protection.rst @@ -84,12 +84,14 @@ For variables that are initialized once at ``__init`` time, these can be marked with the (new and under development) ``__ro_after_init`` attribute. -What remains are variables that are updated rarely (e.g. GDT). These -will need another infrastructure (similar to the temporary exceptions -made to kernel code mentioned above) that allow them to spend the rest -of their lifetime read-only. (For example, when being updated, only the -CPU thread performing the update would be given uninterruptible write -access to the memory.) +Others, which are statically allocated, but still need to be updated +rarely, can be marked with the ``__wr_after_init`` attribute. + +The update mechanism must avoid exposing the data to rogue alterations +during the update. For example, only the CPU thread performing the update +would be given uninterruptible write access to the memory. + +Currently there is no protection available for data allocated dynamically. Segregation of kernel memory from userspace memory ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ From patchwork Wed Dec 19 21:33:33 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Igor Stoppa X-Patchwork-Id: 10738211 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 69B1F6C5 for ; Wed, 19 Dec 2018 21:35:36 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5B45F286B0 for ; Wed, 19 Dec 2018 21:35:36 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4F25B286B3; Wed, 19 Dec 2018 21:35:36 +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=-5.0 required=2.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI,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 530D4286B0 for ; Wed, 19 Dec 2018 21:35:35 +0000 (UTC) Received: (qmail 32282 invoked by uid 550); 19 Dec 2018 21:34: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 32199 invoked from network); 19 Dec 2018 21:34:28 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references:reply-to :mime-version:content-transfer-encoding; bh=phFz7vOrcS81MlJJt5v1oXi0mDKxGWtwTN4iyYFZ+74=; b=WDs6ySk3E+alRObAy6wDkCWuaVNVmuHxgGD/1dsv9YzDDBu9vjjGYTllVerwNIiKQV pztj6X4r6TPKKDINigAFHDA+MoPMiLGLpN1vkr6o44sTpRmMIM88qMVO05EklU2hqIHl XyrneWxYZWRhJJzHDAvQrleFk6S0QbDdUKm0y6gsEG4SKWX3MtvhKOPdX6gR0x0H9+yq ZtNyRl7v8NUh7DTF2iebL4466gaSC2V48VmytKEVnV7Lafw3Z0t87QsWMQt/SU2T+lRj SHNL1KbcumOc828PDB6DUsY9wnxFMUvyZSF57BED24yikWb6gqsHNmPgjkcetg3IyTbj u+Wg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:reply-to:mime-version:content-transfer-encoding; bh=phFz7vOrcS81MlJJt5v1oXi0mDKxGWtwTN4iyYFZ+74=; b=LpgXdWxiUJBBDUVCmuMRCzyjBnYT0gM07XGa8B2Mri6fGW/C69tmvn/lI1l2Uj5gLs zYwss5l8TQEUmNK+3+2B/us8He2XsTaA6AceEUpS1/8C2bBpIHW03duTr/f/fjKFBN7K nJBoUWfUuK5pvY2mGyUpk/mftba2DcPZvWZTeDs3ygM/kUdGHaBl7/Ak3jvcNKeiRpIl TOKw6rKk0d2EUMeMgjdq73BDYfWE+rbsviCjsaq7VqkVHKMW7gOnZpDikNUsWVTMZ2QL dojxyjGFgC4UGr2Oebjsy+WS1HWYdKMoGOvNGIlAUVTgXKL4bdlNZ+C/2blfmUVy7tLc 8iAA== X-Gm-Message-State: AA+aEWZfCmgV24iRcomsqtyL6ttj5na1c1ui35JZ5Tw7jOfbU2FEQNb5 gyofksb4PsUPiq//UnX5qWc= X-Google-Smtp-Source: AFSGD/Vxltp9cariWUfXVZcfYzagR0U5SpeU/owGfesG8rdFREe2XSYUZ/kYeEPSHTXXU3K1J5TI2g== X-Received: by 2002:a2e:8719:: with SMTP id m25-v6mr14932638lji.121.1545255257431; Wed, 19 Dec 2018 13:34:17 -0800 (PST) From: Igor Stoppa X-Google-Original-From: Igor Stoppa To: Andy Lutomirski , Matthew Wilcox , Peter Zijlstra , Dave Hansen , Mimi Zohar Cc: igor.stoppa@huawei.com, Nadav Amit , Kees Cook , linux-integrity@vger.kernel.org, kernel-hardening@lists.openwall.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 07/12] __wr_after_init: lkdtm test Date: Wed, 19 Dec 2018 23:33:33 +0200 Message-Id: <20181219213338.26619-8-igor.stoppa@huawei.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181219213338.26619-1-igor.stoppa@huawei.com> References: <20181219213338.26619-1-igor.stoppa@huawei.com> MIME-Version: 1.0 X-Virus-Scanned: ClamAV using ClamSMTP Verify that trying to modify a variable with the __wr_after_init attribute will cause a crash. Signed-off-by: Igor Stoppa CC: Andy Lutomirski CC: Nadav Amit CC: Matthew Wilcox CC: Peter Zijlstra CC: Kees Cook CC: Dave Hansen CC: Mimi Zohar CC: linux-integrity@vger.kernel.org CC: kernel-hardening@lists.openwall.com CC: linux-mm@kvack.org CC: linux-kernel@vger.kernel.org --- drivers/misc/lkdtm/core.c | 3 +++ drivers/misc/lkdtm/lkdtm.h | 3 +++ drivers/misc/lkdtm/perms.c | 29 +++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/drivers/misc/lkdtm/core.c b/drivers/misc/lkdtm/core.c index 2837dc77478e..73c34b17c433 100644 --- a/drivers/misc/lkdtm/core.c +++ b/drivers/misc/lkdtm/core.c @@ -155,6 +155,9 @@ static const struct crashtype crashtypes[] = { CRASHTYPE(ACCESS_USERSPACE), CRASHTYPE(WRITE_RO), CRASHTYPE(WRITE_RO_AFTER_INIT), +#ifdef CONFIG_PRMEM + CRASHTYPE(WRITE_WR_AFTER_INIT), +#endif CRASHTYPE(WRITE_KERN), CRASHTYPE(REFCOUNT_INC_OVERFLOW), CRASHTYPE(REFCOUNT_ADD_OVERFLOW), diff --git a/drivers/misc/lkdtm/lkdtm.h b/drivers/misc/lkdtm/lkdtm.h index 3c6fd327e166..abba2f52ffa6 100644 --- a/drivers/misc/lkdtm/lkdtm.h +++ b/drivers/misc/lkdtm/lkdtm.h @@ -38,6 +38,9 @@ void lkdtm_READ_BUDDY_AFTER_FREE(void); void __init lkdtm_perms_init(void); void lkdtm_WRITE_RO(void); void lkdtm_WRITE_RO_AFTER_INIT(void); +#ifdef CONFIG_PRMEM +void lkdtm_WRITE_WR_AFTER_INIT(void); +#endif void lkdtm_WRITE_KERN(void); void lkdtm_EXEC_DATA(void); void lkdtm_EXEC_STACK(void); diff --git a/drivers/misc/lkdtm/perms.c b/drivers/misc/lkdtm/perms.c index 53b85c9d16b8..f681730aa652 100644 --- a/drivers/misc/lkdtm/perms.c +++ b/drivers/misc/lkdtm/perms.c @@ -9,6 +9,7 @@ #include #include #include +#include #include /* Whether or not to fill the target memory area with do_nothing(). */ @@ -27,6 +28,10 @@ static const unsigned long rodata = 0xAA55AA55; /* This is marked __ro_after_init, so it should ultimately be .rodata. */ static unsigned long ro_after_init __ro_after_init = 0x55AA5500; +/* This is marked __wr_after_init, so it should be in .rodata. */ +static +unsigned long wr_after_init __wr_after_init = 0x55AA5500; + /* * This just returns to the caller. It is designed to be copied into * non-executable memory regions. @@ -104,6 +109,28 @@ void lkdtm_WRITE_RO_AFTER_INIT(void) *ptr ^= 0xabcd1234; } +#ifdef CONFIG_PRMEM + +void lkdtm_WRITE_WR_AFTER_INIT(void) +{ + unsigned long *ptr = &wr_after_init; + + /* + * Verify we were written to during init. Since an Oops + * is considered a "success", a failure is to just skip the + * real test. + */ + if ((*ptr & 0xAA) != 0xAA) { + pr_info("%p was NOT written during init!?\n", ptr); + return; + } + + pr_info("attempting bad wr_after_init write at %p\n", ptr); + *ptr ^= 0xabcd1234; +} + +#endif + void lkdtm_WRITE_KERN(void) { size_t size; @@ -200,4 +227,6 @@ void __init lkdtm_perms_init(void) /* Make sure we can write to __ro_after_init values during __init */ ro_after_init |= 0xAA; + /* Make sure we can write to __wr_after_init during __init */ + wr_after_init |= 0xAA; } From patchwork Wed Dec 19 21:33:34 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Igor Stoppa X-Patchwork-Id: 10738213 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 8E5996C5 for ; Wed, 19 Dec 2018 21:35:50 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7EE7C286B0 for ; Wed, 19 Dec 2018 21:35:50 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 72FF7286B3; Wed, 19 Dec 2018 21:35:50 +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=-5.0 required=2.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI,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 4DDCE286B0 for ; Wed, 19 Dec 2018 21:35:49 +0000 (UTC) Received: (qmail 32501 invoked by uid 550); 19 Dec 2018 21:34:31 -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 32418 invoked from network); 19 Dec 2018 21:34:30 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references:reply-to :mime-version:content-transfer-encoding; bh=SjAzFSoIbClc3Yu72aaENnK9IwoMj4eRGCahSXkkXDY=; b=poGcWw/a5dJN1e4IsKUTg9pMbxoGjkckyx8MaSkIr8aVIRng3tIcIrdZ7bIpwBd1aW /Dj6i5fqZOC7b6xeP5q+VcNLiipYZbnUbSanuScQx6ZaQW9f5b2Xc5P/F/qzTZVierGT Rd6CLxydzbm7Mo5/8Hgsx4sm9C32VGSlCBeCSB9A/b18FgE2P5go+U/9Ibz9AFsHQ4YE avQ097RseD5F7PI14K0mNcIC26Xs55WgqRCaGNpums6/2CHHlkW/alMX3lufYGhYovJp I/Fnbo+3KnK1CiTtnCbj4tjLXBQ9xQMrqk97gx2+rBN1d9247ZwYsu/LSWuWqhwEXB+p azjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:reply-to:mime-version:content-transfer-encoding; bh=SjAzFSoIbClc3Yu72aaENnK9IwoMj4eRGCahSXkkXDY=; b=ngpa6G35Tnyn90ov++3HHspgHR0uZsT/sV/Mr5Fudnju7mIqZJz+78DbgCStF9BWnV AS4n94j63lK3BnQU30oPWLU/r0QwCUuU020W+X+mmOc+7XD3acSIkFRfE/Fc65jdT7uP h6JXlQKKdJv0IaEFkgE5McAvcqDaeu6JPQxvlRTuGOU3O0z61fV5VLMNzIoCUFXJAqHm ogO14rqUUhOsfsuDgwBI9U3INtQoK3QIllrhbqr8vWzCqmiCem3fTZj3TLOlYku9w0Jh V6dY8/We0Yfq1IaHC9l0CvYeEqX6mURTW0TTzmmXWPK+GimOpLLrcgALuYAZA6FrayuR HK2Q== X-Gm-Message-State: AA+aEWZbzuVo/B/LeV3Mm+qdyfjne52wHEOkxkNTlPJv57SwyfppG/G3 1q3lP/xZkRmpjbXoNMPENjY= X-Google-Smtp-Source: AFSGD/XS2Kzx9OR3BTlvyctmNLbS6bmvi/yoWDfNmmdJC+n2aSmi3aLbOa8u5dUsvtPjgDv0jk8AaA== X-Received: by 2002:a19:5402:: with SMTP id i2mr13050911lfb.128.1545255259391; Wed, 19 Dec 2018 13:34:19 -0800 (PST) From: Igor Stoppa X-Google-Original-From: Igor Stoppa To: Andy Lutomirski , Matthew Wilcox , Peter Zijlstra , Dave Hansen , Mimi Zohar Cc: igor.stoppa@huawei.com, Nadav Amit , Kees Cook , linux-integrity@vger.kernel.org, kernel-hardening@lists.openwall.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 08/12] rodata_test: refactor tests Date: Wed, 19 Dec 2018 23:33:34 +0200 Message-Id: <20181219213338.26619-9-igor.stoppa@huawei.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181219213338.26619-1-igor.stoppa@huawei.com> References: <20181219213338.26619-1-igor.stoppa@huawei.com> MIME-Version: 1.0 X-Virus-Scanned: ClamAV using ClamSMTP Refactor the test cases, in preparation for using them also for testing __wr_after_init memory, when available. Signed-off-by: Igor Stoppa CC: Andy Lutomirski CC: Nadav Amit CC: Matthew Wilcox CC: Peter Zijlstra CC: Kees Cook CC: Dave Hansen CC: Mimi Zohar CC: linux-integrity@vger.kernel.org CC: kernel-hardening@lists.openwall.com CC: linux-mm@kvack.org CC: linux-kernel@vger.kernel.org --- mm/rodata_test.c | 48 ++++++++++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/mm/rodata_test.c b/mm/rodata_test.c index d908c8769b48..e1349520b436 100644 --- a/mm/rodata_test.c +++ b/mm/rodata_test.c @@ -14,44 +14,52 @@ #include #include -static const int rodata_test_data = 0xC3; +#define INIT_TEST_VAL 0xC3 -void rodata_test(void) +static const int rodata_test_data = INIT_TEST_VAL; + +static bool test_data(char *data_type, const int *data, + unsigned long start, unsigned long end) { - unsigned long start, end; int zero = 0; /* test 1: read the value */ /* If this test fails, some previous testrun has clobbered the state */ - if (!rodata_test_data) { - pr_err("test 1 fails (start data)\n"); - return; + if (*data != INIT_TEST_VAL) { + pr_err("%s: test 1 fails (init data value)\n", data_type); + return false; } /* test 2: write to the variable; this should fault */ - if (!probe_kernel_write((void *)&rodata_test_data, - (void *)&zero, sizeof(zero))) { - pr_err("test data was not read only\n"); - return; + if (!probe_kernel_write((void *)data, (void *)&zero, sizeof(zero))) { + pr_err("%s: test data was not read only\n", data_type); + return false; } /* test 3: check the value hasn't changed */ - if (rodata_test_data == zero) { - pr_err("test data was changed\n"); - return; + if (*data != INIT_TEST_VAL) { + pr_err("%s: test data was changed\n", data_type); + return false; } /* test 4: check if the rodata section is PAGE_SIZE aligned */ - start = (unsigned long)__start_rodata; - end = (unsigned long)__end_rodata; if (start & (PAGE_SIZE - 1)) { - pr_err("start of .rodata is not page size aligned\n"); - return; + pr_err("%s: start of data is not page size aligned\n", + data_type); + return false; } if (end & (PAGE_SIZE - 1)) { - pr_err("end of .rodata is not page size aligned\n"); - return; + pr_err("%s: end of data is not page size aligned\n", + data_type); + return false; } + pr_info("%s tests were successful", data_type); + return true; +} - pr_info("all tests were successful\n"); +void rodata_test(void) +{ + test_data("rodata", &rodata_test_data, + (unsigned long)&__start_rodata, + (unsigned long)&__end_rodata); } From patchwork Wed Dec 19 21:33:35 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Igor Stoppa X-Patchwork-Id: 10738215 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 440656C5 for ; Wed, 19 Dec 2018 21:36:16 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 365D5286A4 for ; Wed, 19 Dec 2018 21:36:16 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 29EA8286B4; Wed, 19 Dec 2018 21:36:16 +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=-2.7 required=2.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI 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 22DE12870C for ; Wed, 19 Dec 2018 21:36:02 +0000 (UTC) Received: (qmail 32696 invoked by uid 550); 19 Dec 2018 21:34:33 -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 32594 invoked from network); 19 Dec 2018 21:34:32 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references:reply-to :mime-version:content-transfer-encoding; bh=wZ8trlwQPC9hiGZKEv5EaYDHKPPRSKQ7DlpYkIOznGo=; b=RU84YQAA9ttAmCj5fwbw1hMbyAy79lr8StEuIYPs9nOgSexZa0zqGJhbGusOBhYRhl xvmpaIiQVWKWaisVJxT3EF8AjHPFUOPsMmvVf0xabzznRPYkn4Ka2csi12L5kF0hjOA2 /YZ4p7KgF6klxJT/Fv9A5bUND4Lp9BrQt1C9Cu+DXoFZTNgZshlgaPpXxwkXJnqUg9BL qrMHZK/pasAsW4YC3x1UFogxA456adKKOJHz1U7syC70Xu0ShrNdgy0ATa0ffanp/ZPa 3YqZPYKxBhicupwqEWngQsDdCleZgY4sTjVKRTlNdvj6EHnv6HhLFbg4pQZAERHbxUTH llTg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:reply-to:mime-version:content-transfer-encoding; bh=wZ8trlwQPC9hiGZKEv5EaYDHKPPRSKQ7DlpYkIOznGo=; b=JALz2aA5GyS6x6pPy4T9Cfoker4HEWKZ8jj4b67/ZHku248HNgP4PgTgZMyuZh0WpO OQfBSmtP/+MhGAJ/QfBeLPAudQNEU5RB9euoM+NmyU6xzPI8nFgg8Znf10MfjzAU8Sgp Wh7MKqjtOZ4HdTlTZSx8024FL5xo+JLXlrJc56n5MFipnemfh+uZsgBisDe7VbarfYdR oLm8dx9hskA4X6sKP8iY/BhTampVNvCViFaqKgu3+8YP1biCrBiq6HC7YudW6eMTkeYa QLEQ76ng+Dnv4MfopMOTfS5wB7G5BtpJsoCaNQLMU39mqH3qrd/PbltZM96vB8CRcHWS kWRg== X-Gm-Message-State: AA+aEWbm096sPwYKHeOh3bKeEBzL75fCyDnzoQo7jIA51eDHubscH/0a U/T08F4i9mJ1sxsfxRYeMlg= X-Google-Smtp-Source: AFSGD/XdC5giLSZrQt/Kv87Ghx67drqER+B1qjvWKaiMHLOdw8yx48gU4/ChW9agrJeuwYmr+pkViQ== X-Received: by 2002:a19:8d01:: with SMTP id p1mr12727200lfd.149.1545255261117; Wed, 19 Dec 2018 13:34:21 -0800 (PST) From: Igor Stoppa X-Google-Original-From: Igor Stoppa To: Andy Lutomirski , Matthew Wilcox , Peter Zijlstra , Dave Hansen , Mimi Zohar Cc: igor.stoppa@huawei.com, Nadav Amit , Kees Cook , linux-integrity@vger.kernel.org, kernel-hardening@lists.openwall.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 09/12] rodata_test: add verification for __wr_after_init Date: Wed, 19 Dec 2018 23:33:35 +0200 Message-Id: <20181219213338.26619-10-igor.stoppa@huawei.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181219213338.26619-1-igor.stoppa@huawei.com> References: <20181219213338.26619-1-igor.stoppa@huawei.com> MIME-Version: 1.0 X-Virus-Scanned: ClamAV using ClamSMTP The write protection of the __wr_after_init data can be verified with the same methodology used for const data. Signed-off-by: Igor Stoppa CC: Andy Lutomirski CC: Nadav Amit CC: Matthew Wilcox CC: Peter Zijlstra CC: Kees Cook CC: Dave Hansen CC: Mimi Zohar CC: linux-integrity@vger.kernel.org CC: kernel-hardening@lists.openwall.com CC: linux-mm@kvack.org CC: linux-kernel@vger.kernel.org --- mm/rodata_test.c | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/mm/rodata_test.c b/mm/rodata_test.c index e1349520b436..a669cf9f5a61 100644 --- a/mm/rodata_test.c +++ b/mm/rodata_test.c @@ -16,8 +16,23 @@ #define INIT_TEST_VAL 0xC3 +/* + * Note: __ro_after_init data is, for every practical effect, equivalent to + * const data, since they are even write protected at the same time; there + * is no need for separate testing. + * __wr_after_init data, otoh, is altered also after the write protection + * takes place and it cannot be exploitable for altering more permanent + * data. + */ + static const int rodata_test_data = INIT_TEST_VAL; +#ifdef CONFIG_PRMEM +static int wr_after_init_test_data __wr_after_init = INIT_TEST_VAL; +extern long __start_wr_after_init; +extern long __end_wr_after_init; +#endif + static bool test_data(char *data_type, const int *data, unsigned long start, unsigned long end) { @@ -59,7 +74,13 @@ static bool test_data(char *data_type, const int *data, void rodata_test(void) { - test_data("rodata", &rodata_test_data, - (unsigned long)&__start_rodata, - (unsigned long)&__end_rodata); + if (!test_data("rodata", &rodata_test_data, + (unsigned long)&__start_rodata, + (unsigned long)&__end_rodata)) + return; +#ifdef CONFIG_PRMEM + test_data("wr after init data", &wr_after_init_test_data, + (unsigned long)&__start_wr_after_init, + (unsigned long)&__end_wr_after_init); +#endif } From patchwork Wed Dec 19 21:33:36 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Igor Stoppa X-Patchwork-Id: 10738217 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 4662214E5 for ; Wed, 19 Dec 2018 21:36:18 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3858B286A4 for ; Wed, 19 Dec 2018 21:36:18 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2AB8E286BF; Wed, 19 Dec 2018 21:36:18 +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=-5.0 required=2.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI,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 D489F286A4 for ; Wed, 19 Dec 2018 21:36:16 +0000 (UTC) Received: (qmail 1082 invoked by uid 550); 19 Dec 2018 21:34:34 -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 32752 invoked from network); 19 Dec 2018 21:34:34 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references:reply-to :mime-version:content-transfer-encoding; bh=E9s6uCpyIf7ZYx5Vk5PQV5IG2eR2Pz8U8igLWYJ/OLE=; b=ISpDosYzp3xoIvuWZPXrM9oTicqJ/P2w8uJIxw6GXmO0zny4GLHIzCf+ZZfRUPnCdj bxR8muhxsVcm9y5zEeV25bmyVIGQdOmzmL6Rwzm7/ESZUOmOR8LXKNO5k1xtce45+4U0 b72RuI1/wQ7jzp9OeW0vBRrdwtSRSZZMFbXRuVqdsp/IWicwV0UHHcK5HEKG6MUeGqVo nZks1JC1F53eomseGmQbqryGiaDVwiTYhf92jzwHWIU9y6H/tjIvQBFTOGl9XX417l5o D/ZrM2vi9xkii/bQQxf98k6U25wTLonbTY4OXi17h4Z4EkOKh7s4VOhuiJyrw6FcF5le AoGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:reply-to:mime-version:content-transfer-encoding; bh=E9s6uCpyIf7ZYx5Vk5PQV5IG2eR2Pz8U8igLWYJ/OLE=; b=h29hhCrxSWC0lVIUGP71JJA0u52HpLENSAjORfSBx1edRJNn8lh/EwPEkdtqKC+WLR OsrnggvpSPs6xQ89NGRvRROYuCzqyTmm/zGhiG2ZJnD1pd62h+Io/n8hb58T6zjXOG84 yiE6uIc30ye8iMpg8HGgEEDGLpJZVJmf77sjKGwl9HgNCM4+cJwmi1AJc4vuIDpWyMcI oQL4w1VVEkDtneXU7alzPKzNZhOyONC97dhwnDYQRxbXh3tNW6IW7e7brfB3tle9YcRQ KwMzcOlCn4BRuLIcXOidAcBAGYpQMzCEAVWmocfqAwBjqUTCvWfl6/mwmeTQ9csjdLzg QT9Q== X-Gm-Message-State: AA+aEWbDW7H6pPg6uuT+Q6wgsarc69jCD+6HSgubstl4GSOl8o+SR5gE N1zIX3LHYpLCfk4eRp7wgMo= X-Google-Smtp-Source: AFSGD/VrNtQizC10rFZW5u/r8TBG0RgDsuW8dogvziYWPYJXjruqtzL0FH3mxtu5WlV/03YzGDFPvQ== X-Received: by 2002:a2e:568d:: with SMTP id k13-v6mr5924180lje.105.1545255262956; Wed, 19 Dec 2018 13:34:22 -0800 (PST) From: Igor Stoppa X-Google-Original-From: Igor Stoppa To: Andy Lutomirski , Matthew Wilcox , Peter Zijlstra , Dave Hansen , Mimi Zohar Cc: igor.stoppa@huawei.com, Nadav Amit , Kees Cook , linux-integrity@vger.kernel.org, kernel-hardening@lists.openwall.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 10/12] __wr_after_init: test write rare functionality Date: Wed, 19 Dec 2018 23:33:36 +0200 Message-Id: <20181219213338.26619-11-igor.stoppa@huawei.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181219213338.26619-1-igor.stoppa@huawei.com> References: <20181219213338.26619-1-igor.stoppa@huawei.com> MIME-Version: 1.0 X-Virus-Scanned: ClamAV using ClamSMTP Set of test cases meant to confirm that the write rare functionality works as expected. It can be optionally compiled as module. Signed-off-by: Igor Stoppa CC: Andy Lutomirski CC: Nadav Amit CC: Matthew Wilcox CC: Peter Zijlstra CC: Kees Cook CC: Dave Hansen CC: Mimi Zohar CC: linux-integrity@vger.kernel.org CC: kernel-hardening@lists.openwall.com CC: linux-mm@kvack.org CC: linux-kernel@vger.kernel.org --- mm/Kconfig.debug | 8 +++ mm/Makefile | 1 + mm/test_write_rare.c | 135 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 144 insertions(+) create mode 100644 mm/test_write_rare.c diff --git a/mm/Kconfig.debug b/mm/Kconfig.debug index b10305cfac3c..ae018e56c4e4 100644 --- a/mm/Kconfig.debug +++ b/mm/Kconfig.debug @@ -102,3 +102,11 @@ config DEBUG_PRMEM help After any write rare operation, compares the data written with the value provided by the caller. + +config DEBUG_PRMEM_TEST + tristate "Run self test for statically allocated protected memory" + depends on PRMEM + default n + help + Tries to verify that the protection for statically allocated memory + works correctly and that the memory is effectively protected. diff --git a/mm/Makefile b/mm/Makefile index d210cc9d6f80..62d719c0ee1e 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -58,6 +58,7 @@ obj-$(CONFIG_SPARSEMEM) += sparse.o obj-$(CONFIG_SPARSEMEM_VMEMMAP) += sparse-vmemmap.o obj-$(CONFIG_SLOB) += slob.o obj-$(CONFIG_MMU_NOTIFIER) += mmu_notifier.o +obj-$(CONFIG_DEBUG_PRMEM_TEST) += test_write_rare.o obj-$(CONFIG_KSM) += ksm.o obj-$(CONFIG_PAGE_POISONING) += page_poison.o obj-$(CONFIG_SLAB) += slab.o diff --git a/mm/test_write_rare.c b/mm/test_write_rare.c new file mode 100644 index 000000000000..30574bc34a20 --- /dev/null +++ b/mm/test_write_rare.c @@ -0,0 +1,135 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* + * test_write_rare.c + * + * (C) Copyright 2018 Huawei Technologies Co. Ltd. + * Author: Igor Stoppa + */ + +#include +#include +#include +#include +#include + +#ifdef pr_fmt +#undef pr_fmt +#endif + +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +extern long __start_wr_after_init; +extern long __end_wr_after_init; + +static __wr_after_init int scalar = '0'; +static __wr_after_init u8 array[PAGE_SIZE * 3] __aligned(PAGE_SIZE); + +/* The section must occupy a non-zero number of whole pages */ +static bool test_alignment(void) +{ + unsigned long pstart = (unsigned long)&__start_wr_after_init; + unsigned long pend = (unsigned long)&__end_wr_after_init; + + if (WARN((pstart & ~PAGE_MASK) || (pend & ~PAGE_MASK) || + (pstart >= pend), "Boundaries test failed.")) + return false; + pr_info("Boundaries test passed."); + return true; +} + +static bool test_pattern(void) +{ + return (memtst(array, '0', PAGE_SIZE / 2) || + memtst(array + PAGE_SIZE / 2, '1', PAGE_SIZE * 3 / 4) || + memtst(array + PAGE_SIZE * 5 / 4, '0', PAGE_SIZE / 2) || + memtst(array + PAGE_SIZE * 7 / 4, '1', PAGE_SIZE * 3 / 4) || + memtst(array + PAGE_SIZE * 5 / 2, '0', PAGE_SIZE / 2)); +} + +static bool test_wr_memset(void) +{ + int new_val = '1'; + + wr_memset(&scalar, new_val, sizeof(scalar)); + if (WARN(memtst(&scalar, new_val, sizeof(scalar)), + "Scalar write rare memset test failed.")) + return false; + + pr_info("Scalar write rare memset test passed."); + + wr_memset(array, '0', PAGE_SIZE * 3); + if (WARN(memtst(array, '0', PAGE_SIZE * 3), + "Array write rare memset test failed.")) + return false; + + wr_memset(array + PAGE_SIZE / 2, '1', PAGE_SIZE * 2); + if (WARN(memtst(array + PAGE_SIZE / 2, '1', PAGE_SIZE * 2), + "Array write rare memset test failed.")) + return false; + + wr_memset(array + PAGE_SIZE * 5 / 4, '0', PAGE_SIZE / 2); + if (WARN(memtst(array + PAGE_SIZE * 5 / 4, '0', PAGE_SIZE / 2), + "Array write rare memset test failed.")) + return false; + + if (WARN(test_pattern(), "Array write rare memset test failed.")) + return false; + + pr_info("Array write rare memset test passed."); + return true; +} + +static u8 array_1[PAGE_SIZE * 2]; +static u8 array_2[PAGE_SIZE * 2]; + +static bool test_wr_memcpy(void) +{ + int new_val = 0x12345678; + + wr_assign(scalar, new_val); + if (WARN(memcmp(&scalar, &new_val, sizeof(scalar)), + "Scalar write rare memcpy test failed.")) + return false; + pr_info("Scalar write rare memcpy test passed."); + + wr_memset(array, '0', PAGE_SIZE * 3); + memset(array_1, '1', PAGE_SIZE * 2); + memset(array_2, '0', PAGE_SIZE * 2); + wr_memcpy(array + PAGE_SIZE / 2, array_1, PAGE_SIZE * 2); + wr_memcpy(array + PAGE_SIZE * 5 / 4, array_2, PAGE_SIZE / 2); + + if (WARN(test_pattern(), "Array write rare memcpy test failed.")) + return false; + + pr_info("Array write rare memcpy test passed."); + return true; +} + +static __wr_after_init int *dst; +static int reference = 0x54; + +static bool test_wr_rcu_assign_pointer(void) +{ + wr_rcu_assign_pointer(dst, &reference); + return dst == &reference; +} + +static int __init test_static_wr_init_module(void) +{ + pr_info("static write_rare test"); + if (WARN(!(test_alignment() && + test_wr_memset() && + test_wr_memcpy() && + test_wr_rcu_assign_pointer()), + "static rare-write test failed")) + return -EFAULT; + pr_info("static write_rare test passed"); + return 0; +} + +module_init(test_static_wr_init_module); + +MODULE_LICENSE("GPL v2"); +MODULE_AUTHOR("Igor Stoppa "); +MODULE_DESCRIPTION("Test module for static write rare."); From patchwork Wed Dec 19 21:33:37 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Igor Stoppa X-Patchwork-Id: 10738219 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 7117B6C5 for ; Wed, 19 Dec 2018 21:36:32 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 629AC286A4 for ; Wed, 19 Dec 2018 21:36:32 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 54999286BF; Wed, 19 Dec 2018 21:36:32 +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=-5.0 required=2.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI,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 C307E286A4 for ; Wed, 19 Dec 2018 21:36:30 +0000 (UTC) Received: (qmail 1257 invoked by uid 550); 19 Dec 2018 21:34:36 -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 1175 invoked from network); 19 Dec 2018 21:34:35 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references:reply-to :mime-version:content-transfer-encoding; bh=nFnYEqhQKjGId61gpPa5/uxgrXQl8+JI/cWK8VDpS5Y=; b=PrHzwn0tF5cZJ8rUcPtjyhBB7fZbNtctJAOvNrx1Oxs6IMttMC5N2zeIsjKNeWRc2R Tcb/3vkEBkE2YnLCKxy9k9DbdgE13xmPle3IC+iMSLMIvuyesjXYTBm8xAwyVIa2AGcH /beeel78v6AhNyt9ytcSLTURukyHaSXKh/ZGctqzdCRkOSX03aZjHTlPJUUD4eH/jCbA x5db8A4YA9MP5Pd45shyIQ4fsqljhgcbJ6WilKnFlCSamAsG00YPl93aOk2b33ZoBRhO 5vMcOFPsCVitBtYv9jQVk1ZaWS2BCUou741tRiulXeuCPZlWtNKjx/KJnY369bIvBg7O zg5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:reply-to:mime-version:content-transfer-encoding; bh=nFnYEqhQKjGId61gpPa5/uxgrXQl8+JI/cWK8VDpS5Y=; b=JvyoF6C3lt10L0wmpUM0H1YzCkaK/DtjUiYB5HV2buVmRuiFBjoanD1JOzxHvtaCdE toDn0Ex4yRAlwsVB1Dhmk0jFOh++k+tQTP4lcZ2iNH3ZSJxxxDitEwlnfwRlFxrEZDRV nkuLXZq8wychmeC7JZLrq+cgdQPOjaj09uMtInu/yU/CIdnXYsKS0JciNT1yYWBvnIyL puW157s3bJ/6+4FOS4HroYK9r8nzsEo6eLs/Yzl6hkYxXsZD6IeV39D8NBWH1lbP2wnO rfM7HnkAwFJxzzPPtxmWCiWxp3f3igu6q5YZKxbtYAJ2NfdyG9kuivg/KC9be0Lgbkq0 QmeA== X-Gm-Message-State: AA+aEWYR20746MYkgRlX52HsqWcMJ7N/HxTSIEVizv68CNNRycdNai5P m1yBHcfQt2gAZ8NwRo4Hmgo= X-Google-Smtp-Source: AFSGD/WDSV/bqitpFVUBqXDWqTMforPVSVFRSbH5BBwX6cUs0/Ablg68IJ90z+EsAlf/wTbs5JI07g== X-Received: by 2002:a19:a149:: with SMTP id k70mr13332526lfe.5.1545255264592; Wed, 19 Dec 2018 13:34:24 -0800 (PST) From: Igor Stoppa X-Google-Original-From: Igor Stoppa To: Andy Lutomirski , Matthew Wilcox , Peter Zijlstra , Dave Hansen , Mimi Zohar Cc: igor.stoppa@huawei.com, Nadav Amit , Kees Cook , linux-integrity@vger.kernel.org, kernel-hardening@lists.openwall.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 11/12] IMA: turn ima_policy_flags into __wr_after_init Date: Wed, 19 Dec 2018 23:33:37 +0200 Message-Id: <20181219213338.26619-12-igor.stoppa@huawei.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181219213338.26619-1-igor.stoppa@huawei.com> References: <20181219213338.26619-1-igor.stoppa@huawei.com> MIME-Version: 1.0 X-Virus-Scanned: ClamAV using ClamSMTP The policy flags could be targeted by an attacker aiming at disabling IMA, so that there would be no trace of a file system modification in the measurement list. Since the flags can be altered at runtime, it is not possible to make them become fully read-only, for example with __ro_after_init. __wr_after_init can still provide some protection, at least against simple memory overwrite attacks Signed-off-by: Igor Stoppa CC: Andy Lutomirski CC: Nadav Amit CC: Matthew Wilcox CC: Peter Zijlstra CC: Kees Cook CC: Dave Hansen CC: Mimi Zohar CC: linux-integrity@vger.kernel.org CC: kernel-hardening@lists.openwall.com CC: linux-mm@kvack.org CC: linux-kernel@vger.kernel.org --- security/integrity/ima/ima.h | 3 ++- security/integrity/ima/ima_init.c | 5 +++-- security/integrity/ima/ima_policy.c | 9 +++++---- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/security/integrity/ima/ima.h b/security/integrity/ima/ima.h index cc12f3449a72..297c25f5122e 100644 --- a/security/integrity/ima/ima.h +++ b/security/integrity/ima/ima.h @@ -24,6 +24,7 @@ #include #include #include +#include #include #include "../integrity.h" @@ -50,7 +51,7 @@ enum tpm_pcrs { TPM_PCR0 = 0, TPM_PCR8 = 8 }; #define IMA_TEMPLATE_IMA_FMT "d|n" /* current content of the policy */ -extern int ima_policy_flag; +extern int ima_policy_flag __wr_after_init; /* set during initialization */ extern int ima_hash_algo; diff --git a/security/integrity/ima/ima_init.c b/security/integrity/ima/ima_init.c index 59d834219cd6..5f4e13e671bf 100644 --- a/security/integrity/ima/ima_init.c +++ b/security/integrity/ima/ima_init.c @@ -21,6 +21,7 @@ #include #include #include +#include #include "ima.h" @@ -98,9 +99,9 @@ void __init ima_load_x509(void) { int unset_flags = ima_policy_flag & IMA_APPRAISE; - ima_policy_flag &= ~unset_flags; + wr_assign(ima_policy_flag, ima_policy_flag & ~unset_flags); integrity_load_x509(INTEGRITY_KEYRING_IMA, CONFIG_IMA_X509_PATH); - ima_policy_flag |= unset_flags; + wr_assign(ima_policy_flag, ima_policy_flag | unset_flags); } #endif diff --git a/security/integrity/ima/ima_policy.c b/security/integrity/ima/ima_policy.c index 7489cb7de6dc..2004de818d92 100644 --- a/security/integrity/ima/ima_policy.c +++ b/security/integrity/ima/ima_policy.c @@ -47,7 +47,7 @@ #define INVALID_PCR(a) (((a) < 0) || \ (a) >= (FIELD_SIZEOF(struct integrity_iint_cache, measured_pcrs) * 8)) -int ima_policy_flag; +int ima_policy_flag __wr_after_init; static int temp_ima_appraise; static int build_ima_appraise __ro_after_init; @@ -452,12 +452,13 @@ void ima_update_policy_flag(void) list_for_each_entry(entry, ima_rules, list) { if (entry->action & IMA_DO_MASK) - ima_policy_flag |= entry->action; + wr_assign(ima_policy_flag, + ima_policy_flag | entry->action); } ima_appraise |= (build_ima_appraise | temp_ima_appraise); if (!ima_appraise) - ima_policy_flag &= ~IMA_APPRAISE; + wr_assign(ima_policy_flag, ima_policy_flag & ~IMA_APPRAISE); } static int ima_appraise_flag(enum ima_hooks func) @@ -574,7 +575,7 @@ void ima_update_policy(void) list_splice_tail_init_rcu(&ima_temp_rules, policy, synchronize_rcu); if (ima_rules != policy) { - ima_policy_flag = 0; + wr_assign(ima_policy_flag, 0); ima_rules = policy; } ima_update_policy_flag(); From patchwork Wed Dec 19 21:33:38 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Igor Stoppa X-Patchwork-Id: 10738221 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 203FF6C5 for ; Wed, 19 Dec 2018 21:36:46 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 113CC286A4 for ; Wed, 19 Dec 2018 21:36:46 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 03A50286B4; Wed, 19 Dec 2018 21:36:46 +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=-5.0 required=2.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI,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 1A197286A4 for ; Wed, 19 Dec 2018 21:36:44 +0000 (UTC) Received: (qmail 1407 invoked by uid 550); 19 Dec 2018 21:34:38 -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 1331 invoked from network); 19 Dec 2018 21:34:37 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references:reply-to :mime-version:content-transfer-encoding; bh=nF+ce+AsD/YO6oFhBzoP5bXERd0j3/JBWm1ePv97n+k=; b=vGTkPcUFo/MMtKaWdBnUN7Wr0yJwxwfs21G27pnVV2k1BB2gRhB1bK7E0cHhYbqIgf stDCnsHWMfyP+yrG6oDmTMF96qa/BaUh7n6LG+3tu8TfXBh5Wu8DfaY+PV6lNfvBRdKh hNQbHgauq8nMJyjwqmvZjnwDRCxA5mLVfzWQ0bVZJe+zeth1/NOc5HzIGqpMFr/Qrlqx aAsQZ/q+cAxCC45Usqy0Xty8HNLpuBvEXpaPgWZc396htU9HqMdTxdiPYyjkaNHn7myJ xkIvhC2HOtkTyU5OhrA1C4U6i8nYCJDolSU8uTM4BXBZOybG0Kkmw2kFOsIZCZuoWSro lNVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:reply-to:mime-version:content-transfer-encoding; bh=nF+ce+AsD/YO6oFhBzoP5bXERd0j3/JBWm1ePv97n+k=; b=tGChq//CBNWEm9F/Nu0bZ13AFlthWzc6rPvYZa6RPCx+b9loI86n1SCeDYB/67WGuU YDm+o+1EL1kdIfHnmAVLiYRkM4vxuvx4KnWt1J4UVADBBKxcK1T7xRNNNPgWGK93ezbF J4rJ7VzoZs4MMog8DYbxJp/lbxyyAQIjV9UKn75xX7QcqgmyhAmKLk39IWiM3uGTHUJ+ /vt40qrMPiRdQ6NfBgZAULj9I29bZxrLsE4L3S+ldqaqaX325lUORQRSlq9toFDUv37D /eDXdu0oS9u/JKTA0/upw34ODLQ287kVfLzs3SCFawNQJ9KkFXV23+uG6Of/IpG5donZ hS6w== X-Gm-Message-State: AA+aEWafiUvuiMXKTWYwtNNd5iCBiKigSp1x4cBas4UBSM/XUp9cK8H8 NlDSuokf2CIFmYqzqcaSGIk= X-Google-Smtp-Source: AFSGD/Wy2ENV+x3psGq5BTNSnjgWTYu45qYohRv5i5VPwG4fDvqlbGM3fM6N3dl/AyW8xTUnzykvyA== X-Received: by 2002:a2e:4601:: with SMTP id t1-v6mr15007443lja.111.1545255266361; Wed, 19 Dec 2018 13:34:26 -0800 (PST) From: Igor Stoppa X-Google-Original-From: Igor Stoppa To: Andy Lutomirski , Matthew Wilcox , Peter Zijlstra , Dave Hansen , Mimi Zohar Cc: igor.stoppa@huawei.com, Nadav Amit , Kees Cook , linux-integrity@vger.kernel.org, kernel-hardening@lists.openwall.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 12/12] x86_64: __clear_user as case of __memset_user Date: Wed, 19 Dec 2018 23:33:38 +0200 Message-Id: <20181219213338.26619-13-igor.stoppa@huawei.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181219213338.26619-1-igor.stoppa@huawei.com> References: <20181219213338.26619-1-igor.stoppa@huawei.com> MIME-Version: 1.0 X-Virus-Scanned: ClamAV using ClamSMTP To avoid code duplication, re-use __memset_user(), when clearing user-space memory. The overhead should be minimal (2 extra register assignments) and outside of the writing loop. Signed-off-by: Igor Stoppa CC: Andy Lutomirski CC: Nadav Amit CC: Matthew Wilcox CC: Peter Zijlstra CC: Kees Cook CC: Dave Hansen CC: Mimi Zohar CC: linux-integrity@vger.kernel.org CC: kernel-hardening@lists.openwall.com CC: linux-mm@kvack.org CC: linux-kernel@vger.kernel.org --- arch/x86/lib/usercopy_64.c | 29 +---------------------------- 1 file changed, 1 insertion(+), 28 deletions(-) diff --git a/arch/x86/lib/usercopy_64.c b/arch/x86/lib/usercopy_64.c index 84f8f8a20b30..ab6aabb62055 100644 --- a/arch/x86/lib/usercopy_64.c +++ b/arch/x86/lib/usercopy_64.c @@ -69,34 +69,7 @@ EXPORT_SYMBOL(memset_user); unsigned long __clear_user(void __user *addr, unsigned long size) { - long __d0; - might_fault(); - /* no memory constraint because it doesn't change any memory gcc knows - about */ - stac(); - asm volatile( - " testq %[size8],%[size8]\n" - " jz 4f\n" - "0: movq $0,(%[dst])\n" - " addq $8,%[dst]\n" - " decl %%ecx ; jnz 0b\n" - "4: movq %[size1],%%rcx\n" - " testl %%ecx,%%ecx\n" - " jz 2f\n" - "1: movb $0,(%[dst])\n" - " incq %[dst]\n" - " decl %%ecx ; jnz 1b\n" - "2:\n" - ".section .fixup,\"ax\"\n" - "3: lea 0(%[size1],%[size8],8),%[size8]\n" - " jmp 2b\n" - ".previous\n" - _ASM_EXTABLE_UA(0b, 3b) - _ASM_EXTABLE_UA(1b, 2b) - : [size8] "=&c"(size), [dst] "=&D" (__d0) - : [size1] "r"(size & 7), "[size8]" (size / 8), "[dst]"(addr)); - clac(); - return size; + return __memset_user(addr, 0, size); } EXPORT_SYMBOL(__clear_user);