From patchwork Mon May 21 21:11:00 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg Kroah-Hartman X-Patchwork-Id: 10416401 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 1184160365 for ; Mon, 21 May 2018 21:16:17 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0034028A72 for ; Mon, 21 May 2018 21:16:17 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E7CB728A75; Mon, 21 May 2018 21:16: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.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, 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 3344F28A72 for ; Mon, 21 May 2018 21:16:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3FD5C6B0003; Mon, 21 May 2018 17:16:15 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 3AAFC6B0005; Mon, 21 May 2018 17:16:15 -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 29A8E6B0006; Mon, 21 May 2018 17:16:15 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pf0-f198.google.com (mail-pf0-f198.google.com [209.85.192.198]) by kanga.kvack.org (Postfix) with ESMTP id DFC536B0003 for ; Mon, 21 May 2018 17:16:14 -0400 (EDT) Received: by mail-pf0-f198.google.com with SMTP id w7-v6so9934483pfd.9 for ; Mon, 21 May 2018 14:16:14 -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:user-agent:mime-version; bh=HmVeNk31dl2qgj0EV22QjYssl5Bu2iaFTSs1SfRBf6Y=; b=EgSCfCLOJa7q4VdTrcR9K+u6zLvEKlN8/xqv/1jd1FuexNIIvW9dY/eLy6Jr0fcxov zYbyv591G4tLWaLBHk9NmfBVWnBQqz/5L9dhozNJ7C9Mk9oSbOmNsnaDbhZCr2FP1uBJ 8cqIjN7tioa3VgXXn0f1ucNCNUlPXzszFH3XevFoeEqM2qTeAoLlwXKDrBPD4/IhLK++ oG1SMYvZseUvXv7c+TW9pTZFgt6X6uNFBB8Y1tPAgoJ6SZSMg49UDTWqKWdSPbVqTsDN ZFLxm2wQmkPT3rlxoxhc1FC4OS6Wv4qUzI5GKeCcqz5gF8kJKKoZ5eFf5JLXvaJ8MG1M MVIQ== X-Gm-Message-State: ALKqPwfR9MEUASfi9YBKrUUDMWeDlY+9Cqb8ed8O7TYj7nuUpOxpvgZb Urc0uKxELzZ1YIKph5Zkb2NKfshjXfGllEBpJByXgKOaj8JH1x9H9tgSfrbAbwUdc4UUO5GuNi7 4jfuhwoSA54tWQ8BhB5XEfRdrPIF1karW9PzpLZngJ9mijoTC5+zQczPFOG+mIZ4= X-Received: by 2002:a65:668e:: with SMTP id b14-v6mr11687687pgw.172.1526937374576; Mon, 21 May 2018 14:16:14 -0700 (PDT) X-Google-Smtp-Source: AB8JxZqB3uDnM4zbA+PV00e/jCjdXW6OKUQ/fOwL5+8kHVIq0gSGhLg0lxwruUZnTis3FF1hF0kZ X-Received: by 2002:a65:668e:: with SMTP id b14-v6mr11687661pgw.172.1526937373737; Mon, 21 May 2018 14:16:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526937373; cv=none; d=google.com; s=arc-20160816; b=Hi61HgoOlmxJ9F+R0x9E5nfWhVlYOl17DjkbjnaDmLm0x/MkuL4Qo4WeRlgBJ8Y7i/ 3c3CHMmeeUFSoKao8dfR+34fXOHLhPhXq54CGnVW/eNKIsCbbRY8eHrzSrPb66k9l9+d 1acyI/eVet8f1ftEx7U6S4WccXKL4bRQXTo0VTuRlTqfeDIQQPh1xY4pvht7Myx+3n8S n8Tz0D0Vy38GQM9egYVoWpY9qfBo1StqWodjjdYdHt4vxTLYuZXVcKMUZI63iVJaA4Be duLjZbFN/UUzi74nGSMVL3Uz2WcCerZiRBkgzBvBrn34f0qloqcszGCICjk2n0zzMHX4 7+Tw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:user-agent:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=HmVeNk31dl2qgj0EV22QjYssl5Bu2iaFTSs1SfRBf6Y=; b=olgJLgCJSZc4qWKrDfgZ3Y7d99JTdVpaulfIe68wt7jdsSZ+JlansDJ7/Ox0D6GNKd aDWN5I5lcrGA0r/9+qT0WuYHdXjOQDtUPqwhZXuKoVdOB5hNeL4f14YrunTSek9QD8Sv HPNhobz5rHzxybKlloE14EeMrNlkbT9jC4VQv6HTHBZz6n/rCsauCry14OSiGa95jGBQ UAd7GXwZxkl4U25XNDTNOB6T5IZgG7lmnWtb6JTc0ExTKhAbGg1noaa19PR++C7Ov/qk xYdQYIe/ubYrbZg+rXgKTdR2/NncmzsRRbAa0XT4dGzCVvb0KKjRRNz0Y91Z95DrLisA ivHg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=0zo/rFM5; spf=pass (google.com: domain of srs0=nia/=ii=linuxfoundation.org=gregkh@kernel.org designates 198.145.29.99 as permitted sender) smtp.mailfrom=SRS0=nia/=II=linuxfoundation.org=gregkh@kernel.org Received: from mail.kernel.org (mail.kernel.org. [198.145.29.99]) by mx.google.com with ESMTPS id o1-v6si706349pgn.277.2018.05.21.14.16.13 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 21 May 2018 14:16:13 -0700 (PDT) Received-SPF: pass (google.com: domain of srs0=nia/=ii=linuxfoundation.org=gregkh@kernel.org designates 198.145.29.99 as permitted sender) client-ip=198.145.29.99; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=0zo/rFM5; spf=pass (google.com: domain of srs0=nia/=ii=linuxfoundation.org=gregkh@kernel.org designates 198.145.29.99 as permitted sender) smtp.mailfrom=SRS0=nia/=II=linuxfoundation.org=gregkh@kernel.org Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id B56282075C; Mon, 21 May 2018 21:16:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1526937373; bh=IDyZnWEoQhrWIubpahbmblzOdCmd6DkQpypEvhvDleQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=0zo/rFM5JKGjTlb6cHA//H5TmL/v6F4rBC/fOj6jSKspzZnVYqavvAznWXcpT3p9b 8pVMf8Ml82pmI+lTrbAGbCJYQ4633fby8ajhSGPf6iiJg44kc7JmLxe3/Uwxg89cfC ulIbUL1XO786rIok6huepwfR0BDNsjwp7TlDbhQk= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Shakeel Butt , Dave Hansen , Andrew Morton , Dave Hansen , Linus Torvalds , Michael Ellermen , Peter Zijlstra , Ram Pai , Shuah Khan , Thomas Gleixner , linux-mm@kvack.org, Ingo Molnar Subject: [PATCH 4.9 24/87] x86/pkeys: Override pkey when moving away from PROT_EXEC Date: Mon, 21 May 2018 23:11:00 +0200 Message-Id: <20180521210422.554987897@linuxfoundation.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180521210420.222671977@linuxfoundation.org> References: <20180521210420.222671977@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review 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 4.9-stable review patch. If anyone has any objections, please let me know. ------------------ From: Dave Hansen commit 0a0b152083cfc44ec1bb599b57b7aab41327f998 upstream. I got a bug report that the following code (roughly) was causing a SIGSEGV: mprotect(ptr, size, PROT_EXEC); mprotect(ptr, size, PROT_NONE); mprotect(ptr, size, PROT_READ); *ptr = 100; The problem is hit when the mprotect(PROT_EXEC) is implicitly assigned a protection key to the VMA, and made that key ACCESS_DENY|WRITE_DENY. The PROT_NONE mprotect() failed to remove the protection key, and the PROT_NONE-> PROT_READ left the PTE usable, but the pkey still in place and left the memory inaccessible. To fix this, we ensure that we always "override" the pkee at mprotect() if the VMA does not have execute-only permissions, but the VMA has the execute-only pkey. We had a check for PROT_READ/WRITE, but it did not work for PROT_NONE. This entirely removes the PROT_* checks, which ensures that PROT_NONE now works. Reported-by: Shakeel Butt Signed-off-by: Dave Hansen Cc: Andrew Morton Cc: Dave Hansen Cc: Linus Torvalds Cc: Michael Ellermen Cc: Peter Zijlstra Cc: Ram Pai Cc: Shuah Khan Cc: Thomas Gleixner Cc: linux-mm@kvack.org Cc: stable@vger.kernel.org Fixes: 62b5f7d013f ("mm/core, x86/mm/pkeys: Add execute-only protection keys support") Link: http://lkml.kernel.org/r/20180509171351.084C5A71@viggo.jf.intel.com Signed-off-by: Ingo Molnar Signed-off-by: Greg Kroah-Hartman --- arch/x86/include/asm/pkeys.h | 12 +++++++++++- arch/x86/mm/pkeys.c | 21 +++++++++++---------- 2 files changed, 22 insertions(+), 11 deletions(-) --- a/arch/x86/include/asm/pkeys.h +++ b/arch/x86/include/asm/pkeys.h @@ -1,6 +1,8 @@ #ifndef _ASM_X86_PKEYS_H #define _ASM_X86_PKEYS_H +#define ARCH_DEFAULT_PKEY 0 + #define arch_max_pkey() (boot_cpu_has(X86_FEATURE_OSPKE) ? 16 : 1) extern int arch_set_user_pkey_access(struct task_struct *tsk, int pkey, @@ -14,7 +16,7 @@ extern int __execute_only_pkey(struct mm static inline int execute_only_pkey(struct mm_struct *mm) { if (!boot_cpu_has(X86_FEATURE_OSPKE)) - return 0; + return ARCH_DEFAULT_PKEY; return __execute_only_pkey(mm); } @@ -55,6 +57,14 @@ bool mm_pkey_is_allocated(struct mm_stru return false; if (pkey >= arch_max_pkey()) return false; + /* + * The exec-only pkey is set in the allocation map, but + * is not available to any of the user interfaces like + * mprotect_pkey(). + */ + if (pkey == mm->context.execute_only_pkey) + return false; + return mm_pkey_allocation_map(mm) & (1U << pkey); } --- a/arch/x86/mm/pkeys.c +++ b/arch/x86/mm/pkeys.c @@ -95,26 +95,27 @@ int __arch_override_mprotect_pkey(struct */ if (pkey != -1) return pkey; - /* - * Look for a protection-key-drive execute-only mapping - * which is now being given permissions that are not - * execute-only. Move it back to the default pkey. - */ - if (vma_is_pkey_exec_only(vma) && - (prot & (PROT_READ|PROT_WRITE))) { - return 0; - } + /* * The mapping is execute-only. Go try to get the * execute-only protection key. If we fail to do that, * fall through as if we do not have execute-only - * support. + * support in this mm. */ if (prot == PROT_EXEC) { pkey = execute_only_pkey(vma->vm_mm); if (pkey > 0) return pkey; + } else if (vma_is_pkey_exec_only(vma)) { + /* + * Protections are *not* PROT_EXEC, but the mapping + * is using the exec-only pkey. This mapping was + * PROT_EXEC and will no longer be. Move back to + * the default pkey. + */ + return ARCH_DEFAULT_PKEY; } + /* * This is a vanilla, non-pkey mprotect (or we failed to * setup execute-only), inherit the pkey from the VMA we