From patchwork Fri Dec 21 18:14:13 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Igor Stoppa X-Patchwork-Id: 10740811 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 3C54D13AD for ; Fri, 21 Dec 2018 18:14:54 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2DACC2835B for ; Fri, 21 Dec 2018 18:14:54 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2138728618; Fri, 21 Dec 2018 18:14:54 +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 95CF92835B for ; Fri, 21 Dec 2018 18:14:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0EB638E0005; Fri, 21 Dec 2018 13:14:51 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 071258E0001; Fri, 21 Dec 2018 13:14:51 -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 E83678E0005; Fri, 21 Dec 2018 13:14:50 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-lj1-f197.google.com (mail-lj1-f197.google.com [209.85.208.197]) by kanga.kvack.org (Postfix) with ESMTP id 6C4438E0001 for ; Fri, 21 Dec 2018 13:14:50 -0500 (EST) Received: by mail-lj1-f197.google.com with SMTP id v74-v6so1901138lje.6 for ; Fri, 21 Dec 2018 10:14:50 -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=02CsOTWqtwZzPMlweM9pS6XE1OnLE/PPqdTJz4OZtoU=; b=ubmHfMBSJbzazw0dYZUuB0WQH+PUgQoXQqWerFfCJrjuEgar+iOdKJ2d2n6PPT2FhM RudjSey9DqZkc5gguIdJwzojaM46IoSbkFymMXrk/ZPi/FH/rmpdRDfYrfR94QsuiWIt OQi6cTFrvzi4AmCOt+4pMwtN+VJfGj9c1PcwNzAga6MN5b3IUBJ6qDXopqFgHlqeFPRF KrbHG3/oqo2/aAiJw10nG6ayHIpELsl6qXCa53OJNw+mDdzRSJu6JTdXsf3h4BnTZnMK onkWFeTYYz8c8G50bYCCKWUGBBZOlQs11GybELiLO6v1/9Hbb3hxylu0er5Yb98H/o6v jJkA== X-Gm-Message-State: AJcUukfTku8rTQLpx7yOtJ/Pv6C/penuuNAjGDwrB/jbonPYkhZs2/Eo WgvLNumyP1OWD6aTAzDAEcmTfbG0GHo5/C4p3KZBL+k6bJPwqToTqXj3BknLTCBkacWugva39S0 IgrXOboNIp+iicYqBcw/wDEbjCIXbIFROLeEp6WKnn0HETgVvmjS6opdPZJJrviJd3eC6u38KXb dnrLmclxEzRv72SAOQxQsJ7rN0c4TtU4y8TiC6mBa9NklujopXMc4wPmSt5qr2R2lpOl3fUxMbH e/UZOqCCALIpVwYrdzp7x10LZsohFt9eXaLN9leObyJZQka2mx0wX5mR6MiEm6bsaBMTzMkGOV8 KVECh9IdUrEEOj2ZELgDWxDB2EO+j7sWK3Ni1M/fbfhQzdsO8eXqjCRFYxxUO7jm9Kq7krFdPTp P X-Received: by 2002:a2e:484:: with SMTP id a4-v6mr2258124ljf.27.1545416089775; Fri, 21 Dec 2018 10:14:49 -0800 (PST) X-Received: by 2002:a2e:484:: with SMTP id a4-v6mr2258093ljf.27.1545416088592; Fri, 21 Dec 2018 10:14:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1545416088; cv=none; d=google.com; s=arc-20160816; b=gv8CKIhmNZxaTwinnDZ31ttgbV0h2/w6kLuqqY3zuuWg49ZrkHJCPfPwY7OQJiS72z R61Iqtb7tsvoOEJwNgHJd8THsfcXPUt6RMnZO6WFhlvbFZdFLtX6AFTmV5QS8BxBYL0H aDHAtj9fqEQJ8IVM1OxkUI/nutjO4BP8gyiWE8CJi7wAm5t3TgcOE/GiC0XCyJJ1H86X 1AN0jYVqjr7ovjG6FwWc9LVYX3HYeVAsMN+3Tn7Jz5DEKWSIgC7tubB5Pr1wb6mgMKAm K3D2LfTNif5TnvBPzIHOdpVzFDCyMcaGY2y0ddzQz0SsJDN1TyO945SJ46IcHBqFkgh/ wTCQ== 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=02CsOTWqtwZzPMlweM9pS6XE1OnLE/PPqdTJz4OZtoU=; b=nUt+vNUNFqWCGfrVco8sk/WtI1HlNjKslh9knAzqB/Bwx6cf2IY3aGCrODPb2dMzVT gGF+8W6L1gcDMv6iCAOOrN/S22kR41UYpKYjVQHm59PyVnRqldGs11PeDUeaoYI/clkW A7/MTZQOc76rkfmYOQRRVIBh3vQKq+GroqnczQSjacznrFLgSWbrldx22SboOyyfzMkP FDQPrkfytg7nLs80pzAn6du/ra53d/QgfmOA6ri8Yd/WxAJ/FKD960u7AB+VsSY36Y/O 5O9qgBfomW+CsZNsSWPDntBwvBUs9pIu8+EdYoeavUXGYqHsRW7/whdPUikO289c67eY OP7Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="C/bllgwW"; 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 l20-v6sor16160614lji.21.2018.12.21.10.14.48 for (Google Transport Security); Fri, 21 Dec 2018 10:14:48 -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="C/bllgwW"; 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=02CsOTWqtwZzPMlweM9pS6XE1OnLE/PPqdTJz4OZtoU=; b=C/bllgwWcelpTykNWYi39BnaATo8lpq89ch1ugnwfkYPRnVxve0wGvexjuxUPMzDoo 5Jpl5b0JUZ4ZL/39R1AR2j4X3k6ukiq9RlxTBcEzcGXXAV++v3Za4nKsdMqOIzA9BnLM FYYV6EhXu7gS7jQm20h6QR7xCxqPqLFD8gIyXunzOdhwh2+LhS6sZVyX0ahZHFfkn3Xw sGSbMTE95/rO31tGYeJBOFMCsqpicf6WF5vlojY4Bfno1gSEaX5smNWCKjXFGXXiAutn S0s+m5efoXLUf0pB106ZUBJIrPyvf1cnpu48uQJzohHGLMXY7JXthKhEMa1N9dvbpmSr FT6Q== X-Google-Smtp-Source: ALg8bN6ADTRPD4oTDrtkd5En5+bsIvKrP+5XCjbqDTL4+Lh9yRDACXYcUUsznsEdhYFvH6hg+L2VRA== X-Received: by 2002:a2e:5b1d:: with SMTP id p29-v6mr2252747ljb.176.1545416088161; Fri, 21 Dec 2018 10:14:48 -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 m63-v6sm5444564lje.81.2018.12.21.10.14.46 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 21 Dec 2018 10:14:47 -0800 (PST) From: Igor Stoppa X-Google-Original-From: Igor Stoppa To: Andy Lutomirski , Matthew Wilcox , Peter Zijlstra , Dave Hansen , Mimi Zohar , Thiago Jung Bauermann Cc: igor.stoppa@huawei.com, Nadav Amit , Kees Cook , Ahmed Soliman , 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: Fri, 21 Dec 2018 20:14:13 +0200 Message-Id: <20181221181423.20455-3-igor.stoppa@huawei.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181221181423.20455-1-igor.stoppa@huawei.com> References: <20181221181423.20455-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: Thiago Jung Bauermann CC: Ahmed Soliman 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();