From patchwork Fri Apr 26 23:22:50 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nadav Amit X-Patchwork-Id: 10919953 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 D23E914DB for ; Sat, 27 Apr 2019 06:43:40 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C122128E6B for ; Sat, 27 Apr 2019 06:43:40 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B525328E7B; Sat, 27 Apr 2019 06:43:40 +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=-1.5 required=2.0 tests=BAYES_00,DATE_IN_PAST_06_12, 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 3862D28E6B for ; Sat, 27 Apr 2019 06:43:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 332076B026A; Sat, 27 Apr 2019 02:43:23 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 21EDE6B026B; Sat, 27 Apr 2019 02:43:23 -0400 (EDT) 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 04C7C6B026C; Sat, 27 Apr 2019 02:43:22 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-it1-f200.google.com (mail-it1-f200.google.com [209.85.166.200]) by kanga.kvack.org (Postfix) with ESMTP id D704D6B026A for ; Sat, 27 Apr 2019 02:43:22 -0400 (EDT) Received: by mail-it1-f200.google.com with SMTP id p23so4901523itc.7 for ; Fri, 26 Apr 2019 23:43:22 -0700 (PDT) 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; bh=yNv4uuWY37+QDIo+8eJ5Cs1RQKFygWuhu/mUpTFc+50=; b=r+fuLtQaqCYnkGPj7mv+XlxKjEEp6a9cXsQXvS7FNd2wf59zsQjVovhLQ2W32L2XoD HUo44LRRWFk3B0H4PGkd1jmBnXDVFEo0bairyCpS5+1WwMwcMguXyPx2QQr1B/F81npE 0y5FpBjbulxyMJYhl0fgnXkX0yq9JXbPOg7WP+jiCx8x7Rmq+Z89MJoSDGFqCzadJC/p lWHGXHsKLqomzUNh/kf/hMupiJ0W988gSU6B441uoU9da0e8nkStDwBSPJhRkK6C3ls3 kfuOCzKKZnws/XqHe7d0sGo6MdnnrLxbU0FRIHO8jNsQ5ZXch5Gro5LFaAC73sshtGNH aRTg== X-Gm-Message-State: APjAAAXwGlxWWh9cCAht/FQP9Kvx1rRteoLF9HNFjJ8K6Nn1Sa7fZO2O oDmWkrXy1OdGGDUxRF4XTSyvewy9oXUg6OwRV0tIfOBptni8I4+ipUKkEbjemdPMvA7cGL0LKRJ CFOtUoIXj89LXVR+z8HYMN7IajcEqKy1+4YJs5YPx4BTOeGgTMYl3wBfwv6T5iyjXvg== X-Received: by 2002:a24:f8c7:: with SMTP id a190mr10805151ith.72.1556347402565; Fri, 26 Apr 2019 23:43:22 -0700 (PDT) X-Received: by 2002:a24:f8c7:: with SMTP id a190mr10805121ith.72.1556347401264; Fri, 26 Apr 2019 23:43:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1556347401; cv=none; d=google.com; s=arc-20160816; b=wlN4H8Q48VuEb2TmuE5Mbr0lGmh+bGAJI/OuP9ignJhPRHzEPIMsg2aBIe1MX1LWML RKceN9IcrSLyJROyrLomulNpbOBRgKX7LysQbC9bfwkumVeP1D8LyQvXmFAI3AHK0ddt bbRWb9rYz86zej7GBmS9CbOn99BvnBTpFpkBPQkNYGpVUZi6we7DWxhz8etUTW9S+xAB n9qreGioHp9/j6XcbLWrsQzKeltYuNWBjgkokOF4hvzZMpOC+jsbqVxD+j07HgsKKY10 0W0MUfBd4bHLwfooSv+/+YvOL/r/lw76pXGtMR2rcCURvp8zETSkNKEoWjJXR+0bTpE2 XwYw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=yNv4uuWY37+QDIo+8eJ5Cs1RQKFygWuhu/mUpTFc+50=; b=E2CkCn+DAMmsWvLzV5GP1rwODgXOTt1Muy81wbkCLDjuWlI41g/J65q8zZqWYVv9bI dZCkR049ikc4t4jOFnhmaDkoRFhTnpa/9ZEEUIAomkJNs2uv91KQYomOmF6RJum+3k6P AfB7pky09B9kQcP5oXcV7in1C7RYOxSfbm9dUczGdpwkEADXrDrHxVmOOWVcvRrWKCwZ cQQ1Wle/1W+IR3xiSYVF6TkHt5slW1uenknOSqQW4tcyWRHjVFXcvRmXRPdF18OLigy9 zGfuUcY+qyQHt7CbrnA1Gh89gcNYPm3EiujhMYhDVnD9eAT6ko+FG8O3mU+A+DOb6O4S ivNA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=qfcv0jVy; spf=pass (google.com: domain of nadav.amit@gmail.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=nadav.amit@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 s135sor9184478itb.9.2019.04.26.23.43.21 for (Google Transport Security); Fri, 26 Apr 2019 23:43:21 -0700 (PDT) Received-SPF: pass (google.com: domain of nadav.amit@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=qfcv0jVy; spf=pass (google.com: domain of nadav.amit@gmail.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=nadav.amit@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; bh=yNv4uuWY37+QDIo+8eJ5Cs1RQKFygWuhu/mUpTFc+50=; b=qfcv0jVylRwgUSdFp0yVo6y3SoCAOjol9XbR7W5+lwraaEg2QoXyt+VrAjGp9wzdAn BOm9G9cC4mrsWbrKkWfeWua0CLcUhNFX2UYmr160CAGJNRPhpKunjycWCJnpjaUptQXK FymoezD52jYoCa9vw1d8DrTtNcDpUD3gesH2ZoYEwsgq0bZE6tTukM9zPOscjJPRRiHd 0d0EQKT1ASnMLtYxkgiq4MVCWAUZf/kjKC/u6ibFbPlbEhDhs/jBfSTvPAadE2T4MBPs yoHMRLVUDfJJ0O31Q2uS3OPiszz2lAVHUHfzcDpTk8KxpsBn4aqr0vTCALbdk7a/EPYd 2uWQ== X-Google-Smtp-Source: APXvYqxCgMCnuubydaHXfJIDthz2u46RkKoNrhpM9xg9spe5Zm1Eq7YBRg8tloTlDMLwvkBDrQMPvQ== X-Received: by 2002:a63:7f0b:: with SMTP id a11mr44847434pgd.234.1556347400564; Fri, 26 Apr 2019 23:43:20 -0700 (PDT) Received: from sc2-haas01-esx0118.eng.vmware.com ([66.170.99.1]) by smtp.gmail.com with ESMTPSA id j22sm36460145pfn.129.2019.04.26.23.43.19 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 26 Apr 2019 23:43:20 -0700 (PDT) From: nadav.amit@gmail.com To: Peter Zijlstra , Borislav Petkov , Andy Lutomirski , Ingo Molnar Cc: linux-kernel@vger.kernel.org, x86@kernel.org, hpa@zytor.com, Thomas Gleixner , Nadav Amit , Dave Hansen , linux_dti@icloud.com, linux-integrity@vger.kernel.org, linux-security-module@vger.kernel.org, akpm@linux-foundation.org, kernel-hardening@lists.openwall.com, linux-mm@kvack.org, will.deacon@arm.com, ard.biesheuvel@linaro.org, kristen@linux.intel.com, deneen.t.dock@intel.com, Rick Edgecombe , Nadav Amit Subject: [PATCH v6 11/24] x86/kprobes: Set instruction page as executable Date: Fri, 26 Apr 2019 16:22:50 -0700 Message-Id: <20190426232303.28381-12-nadav.amit@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190426232303.28381-1-nadav.amit@gmail.com> References: <20190426232303.28381-1-nadav.amit@gmail.com> 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 From: Nadav Amit Set the page as executable after allocation. This patch is a preparatory patch for a following patch that makes module allocated pages non-executable. While at it, do some small cleanup of what appears to be unnecessary masking. Acked-by: Masami Hiramatsu Signed-off-by: Nadav Amit Signed-off-by: Rick Edgecombe --- arch/x86/kernel/kprobes/core.c | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/arch/x86/kernel/kprobes/core.c b/arch/x86/kernel/kprobes/core.c index a034cb808e7e..1591852d3ac4 100644 --- a/arch/x86/kernel/kprobes/core.c +++ b/arch/x86/kernel/kprobes/core.c @@ -431,8 +431,20 @@ void *alloc_insn_page(void) void *page; page = module_alloc(PAGE_SIZE); - if (page) - set_memory_ro((unsigned long)page & PAGE_MASK, 1); + if (!page) + return NULL; + + /* + * First make the page read-only, and only then make it executable to + * prevent it from being W+X in between. + */ + set_memory_ro((unsigned long)page, 1); + + /* + * TODO: Once additional kernel code protection mechanisms are set, ensure + * that the page was not maliciously altered and it is still zeroed. + */ + set_memory_x((unsigned long)page, 1); return page; } @@ -440,8 +452,12 @@ void *alloc_insn_page(void) /* Recover page to RW mode before releasing it */ void free_insn_page(void *page) { - set_memory_nx((unsigned long)page & PAGE_MASK, 1); - set_memory_rw((unsigned long)page & PAGE_MASK, 1); + /* + * First make the page non-executable, and only then make it writable to + * prevent it from being W+X in between. + */ + set_memory_nx((unsigned long)page, 1); + set_memory_rw((unsigned long)page, 1); module_memfree(page); }