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: 10738161 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 6C0E51399 for ; Wed, 19 Dec 2018 21:34:15 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5C0A8286B2 for ; Wed, 19 Dec 2018 21:34:15 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4FAC4286CB; Wed, 19 Dec 2018 21:34:15 +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=-3.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A14AA286B4 for ; Wed, 19 Dec 2018 21:34:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 060AA8E001A; Wed, 19 Dec 2018 16:34:11 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id F00108E0001; Wed, 19 Dec 2018 16:34:10 -0500 (EST) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D38848E001A; Wed, 19 Dec 2018 16:34:10 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-lj1-f199.google.com (mail-lj1-f199.google.com [209.85.208.199]) by kanga.kvack.org (Postfix) with ESMTP id 5BCB68E0001 for ; Wed, 19 Dec 2018 16:34:10 -0500 (EST) Received: by mail-lj1-f199.google.com with SMTP id x18-v6so5938169lji.0 for ; Wed, 19 Dec 2018 13:34:10 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:from:to:cc:subject:date :message-id:in-reply-to:references:reply-to:mime-version :content-transfer-encoding; bh=scWZE0nWnwTd+0FVwhDij4gwRZDyVIgA4bUgU9jm4Qw=; b=HzL2Ct8B47YAxHL8p522HqRCiIIvXNdt4DKNOq9dgMEyhONUCjDmAosJ8G+R3D/225 rS5aABuLXmC1XnFuQA3CUxKw+pePdXRAlmzinS9DBj9/nh7BfSMT+Q+xU+v8jiNj+y6Z lwXvCcP9GbikXbD541mzQq/rEU1KvJG0IegdfSLeyCu6Hzgi1gb7E4L+o93BJYEqxV6p CTncYqn5fCCUZ9vO+1BAHqeZcIlUUe3rPGoEHwYVNwxi+FvmozkQQpITVIR+bOeiWgCI UQkUczL5mxQCEs+ktOEXJS8z/ybC3D43ayYYh8Y6UnIU/Ixmo2cQ63h8Ed2L3YhpIlw4 ESzA== X-Gm-Message-State: AA+aEWY8rbOoXMpD7J6/brzoOOn+bj+ji0RcqRRPxJ/7YatUSOOl2dXO KbdR5hyvECtD6C/13A2+LRk4JeCaJaS9Y4Z65miTRJwLQ+yrnbnIOKRLtAd1SVlkf+DUeew9YCA o1330Wy0o2Vhk1eq8NNEUeXyOSDkIQGcdjYBh+SwUZ2z+aOm6pifKqxLLzhEJTJfbqhZdsGkWgd hHl7YJN4G/X9dSdBDgE5hLe9/TeQYLJYM/wTUxUPKiZ4OuMnSgKqRmjBbf5HSk4Ze4UQHrHiS6c sbv3TuF3CBPJDyFv5/zJwLwEOL5i89SwhPbEwqTqcDLBD5VkDXFn1D1d6nA8V0WC+/ZM/iLIicn /QZHXN4Xmc4Mgs+N46vOp4UY2GkyRTtHkkzRmuHJCnXdkG5OyC5MpcBrzmMoCHKl2rTOg76cui6 7 X-Received: by 2002:a19:ae03:: with SMTP id f3mr14232647lfc.86.1545255249704; Wed, 19 Dec 2018 13:34:09 -0800 (PST) X-Received: by 2002:a19:ae03:: with SMTP id f3mr14232619lfc.86.1545255248608; Wed, 19 Dec 2018 13:34:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1545255248; cv=none; d=google.com; s=arc-20160816; b=agTaDejprHT4xGnKYJhFtZN96sZFTeSc73+gPoASRSfQiTW1jFPMvjh9eX5LCWqTjg hqkiO2fR94rap10x+d36TQil0pzF/Be4Nu5LVby0hV2Okbgh8EgCFd6jpf4CgOiE9Eip pZPTVYtflIiqFj+H8MzjgnrEWV1tgohBg6p1slaYo9fEdTHcCkRHM/hI1Jlz0rIBR6X1 +qZVtqjhhMXeLw6i30rCi0ffooyH2iaFhYl+5XY9k0SMFFb4jgiNAyW02qyBAtD6Iana KrvUF2GGfdxRoywQQPw2jYKnBpcsdFCHUEd/GJh+YCQMFMcRPed0NOzwiGUe+SzMNZC0 ieXg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:dkim-signature; bh=scWZE0nWnwTd+0FVwhDij4gwRZDyVIgA4bUgU9jm4Qw=; b=dqIVmGH5uhUwn58iMAWPY8Vw9kAvmnaPLqoMexmNSfsQhd0gnYgOhk5e/KDkNiB+Tn KH18ulN9g0ddHa6UscEozln5UZQ2PQ5zxDYHLpjR2EnLyDBUcfnfes0W05NTJ1gzzRoM ava2oTN78Tfn+8+PIvyJTBk28qCv67PzfFHbAmXohWzHI+VGamTlaBR0HLnWmtygu91V ruyTDpk7ssDCxq6WXedI0MsEpTPdskeuYEDXOznwBxgfbk2UctH1X4lOFU/1CDd3YL0l IhtvdUvPdU6AtZ+yqsJ5n6Z2oJmZ3PQVwtwHisQLgdQFGz9YUIu9Pr5uhZsZiVnVZKqP 31TQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=ECXYO49y; spf=pass (google.com: domain of igor.stoppa@gmail.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=igor.stoppa@gmail.com; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id b23sor5649412lfa.6.2018.12.19.13.34.08 for (Google Transport Security); Wed, 19 Dec 2018 13:34:08 -0800 (PST) Received-SPF: pass (google.com: domain of igor.stoppa@gmail.com designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=ECXYO49y; spf=pass (google.com: domain of igor.stoppa@gmail.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=igor.stoppa@gmail.com; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com 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-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) Received: from localhost.localdomain (dmhwpt3bffxn8z3-j6k-4.rev.dnainternet.fi. [2001:14bb:51:a4c8:5c24:24d7:ca5f:e7d2]) by smtp.gmail.com with ESMTPSA id v64sm3996867lfa.48.2018.12.19.13.34.06 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 19 Dec 2018 13:34:07 -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> Reply-To: Igor Stoppa MIME-Version: 1.0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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();