From patchwork Mon May 21 21:11:23 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 10416487 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 447CA60365 for ; Mon, 21 May 2018 21:19:56 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3418D28A46 for ; Mon, 21 May 2018 21:19:56 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 287F328A78; Mon, 21 May 2018 21:19:56 +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 39CC428A46 for ; Mon, 21 May 2018 21:19:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 336A16B0006; Mon, 21 May 2018 17:19:54 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 2E5366B0007; Mon, 21 May 2018 17:19:54 -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 187EA6B0008; Mon, 21 May 2018 17:19:54 -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 C80E86B0006 for ; Mon, 21 May 2018 17:19:53 -0400 (EDT) Received: by mail-pf0-f198.google.com with SMTP id q15-v6so9830839pff.17 for ; Mon, 21 May 2018 14:19:53 -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=LG5aD/T2e2pbuaSqVqClgElEFl6P61OhiVJ/9lo8X9U=; b=hoXGmURSD78jU/N8yNc7h3B/PIZh5DF+I7FAtvaFbhbqlIowCUXPZQuChFSviwl7+Y fusMM9kTNJP/fJvAiDz4LfDemoGYUYb8vXajWCT851rBdO5Sp3ac8JPuK3KTySVK8No0 65I3Is0oxFki5N4Hcf9ms7WEr2eZKGhi2gBjtRcsrdXXq4vQxM1VkHYqo2gCC0Fv5nNg U8ei17yNRkOTSJ6hCwpo+YTzaRihHqU5NEOGxzLfxWxW4skzegL7b/MEVwwJZmOmqOIU c1apX2dga9MRHWf0RqfGm0Ky8+eDQvrSsNXQ3T15zK7XSjW0Mje+lIrJS+NhSNatYC/F xTHA== X-Gm-Message-State: ALKqPwfVsQqDENanrNnmvp66jH+P0o22We5cYXlyYneCe0Ntq3htLMNz bxoUuE+c4LhcbBX50r7EqmS0UF2QXdk/vk55eEG3bJp7SurLqv+ewGMHa8xRuf71NFMkbvYlAgL Igi7UX7DP5qeJkjpI+DYIpeqNaJdh7/z3gDwRtp+KaVLjCPvy2xNCGb5jIRSakvI= X-Received: by 2002:a17:902:a5:: with SMTP id a34-v6mr21927163pla.58.1526937593492; Mon, 21 May 2018 14:19:53 -0700 (PDT) X-Google-Smtp-Source: AB8JxZou8GUy8k9wM3ZvVXRd7p5dOIXIv38TSOx8zdcQ0cqsWl+Brqq2ug5Uk48wV9BcvWzJjrx6 X-Received: by 2002:a17:902:a5:: with SMTP id a34-v6mr21927147pla.58.1526937592884; Mon, 21 May 2018 14:19:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526937592; cv=none; d=google.com; s=arc-20160816; b=iIc/qBQrm86W7o/5IMvhJR1dX7tG1e8Yj7aDOt+2c1CijnVxa/DCtawhQj6dmeH4iJ 12GjxD0HZGRn83lz1fsSm+0BnhuZalwGCuTY92k6nZToYBXJ6KJf1Cn43wytkgeA0j+K OVPfO3Jz4wTjspqjNUs+DO35c3gta8dt7v3zMuMF9M25UaEx2seAf0trRptIbOmz6Wa+ Yfe71qKNfTkHgSLZVAXSAq4G8B5emKZ7iGDcuxPbL933ZtH0TTjsjDWzl42UR8Y/Ne7c Sht2pYsie6Fh2q5PSZoLVpL3p4L9borSP8wMrcpHRreStRRiDcYU7FqHo8OzIpgbKwJF AH+Q== 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=LG5aD/T2e2pbuaSqVqClgElEFl6P61OhiVJ/9lo8X9U=; b=J3LArNhSp6oBgX5cw2MrQfRLuCOAsBzKiuZzHbRd1urLJYRS+P3HyPEzGuJI8egKAH v0BBWcjS9TRkBCv4uIVV/yDTfxYG3EvrFPY9S+zDxmK0bFdfJ5ae9XNQA8YgGYV6UuOA KdZc7N+rBnC3BKPttmQB4yFHxgCacjOLTtFA0BORREs3F6scjMUBN2RX4b6St0fZyDFf efBt1fB9blJ2xmRZJKgCGOrQtZEYaniGCVXFgqOxF2LGlyj7fJJm5U3qe9Vr3Nylxxs4 c2CW5qKhbASI0LzY3fzTdd+CBD+9850jfb592AoM3rA75a13Tky6mlgVLilFDEQzDzc2 o17Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=FvC7gM1t; 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 c9-v6si14860333plz.501.2018.05.21.14.19.52 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 21 May 2018 14:19:52 -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=FvC7gM1t; 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 DDF972086B; Mon, 21 May 2018 21:19:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1526937592; bh=Zg4nTwjamT4Z7IEjScB+y+vlfG8knR0xcQPUQIAdDTM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FvC7gM1t9tWsj0+Q9Xnf5ALMmOCLgvOdEVJy/wlTNPKpgStdlLXbxYi/ogUYhCyaa mKO6AilIvdJDkZSQzl4ZF8SO6OiVGTQxLxXglfPzDwdkLn1cRTK6gVno5/N1IeCZd8 IBOk14gF4QsI7gdx13p26TbddgZLdOBRMomWqrx4= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Dave Hansen , Dave Hansen , Linus Torvalds , Michael Ellermen , Peter Zijlstra , Ram Pai , Shuah Khan , Thomas Gleixner , linux-mm@kvack.org, Ingo Molnar , Andrew Morton Subject: [PATCH 4.14 33/95] x86/pkeys: Do not special case protection key 0 Date: Mon, 21 May 2018 23:11:23 +0200 Message-Id: <20180521210454.799243479@linuxfoundation.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180521210447.219380974@linuxfoundation.org> References: <20180521210447.219380974@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.14-stable review patch. If anyone has any objections, please let me know. ------------------ From: Dave Hansen commit 2fa9d1cfaf0e02f8abef0757002bff12dfcfa4e6 upstream. mm_pkey_is_allocated() treats pkey 0 as unallocated. That is inconsistent with the manpages, and also inconsistent with mm->context.pkey_allocation_map. Stop special casing it and only disallow values that are actually bad (< 0). The end-user visible effect of this is that you can now use mprotect_pkey() to set pkey=0. This is a bit nicer than what Ram proposed[1] because it is simpler and removes special-casing for pkey 0. On the other hand, it does allow applications to pkey_free() pkey-0, but that's just a silly thing to do, so we are not going to protect against it. The scenario that could happen is similar to what happens if you free any other pkey that is in use: it might get reallocated later and used to protect some other data. The most likely scenario is that pkey-0 comes back from pkey_alloc(), an access-disable or write-disable bit is set in PKRU for it, and the next stack access will SIGSEGV. It's not horribly different from if you mprotect()'d your stack or heap to be unreadable or unwritable, which is generally very foolish, but also not explicitly prevented by the kernel. 1. http://lkml.kernel.org/r/1522112702-27853-1-git-send-email-linuxram@us.ibm.com Signed-off-by: Dave Hansen Cc: Andrew Morton p 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: 58ab9a088dda ("x86/pkeys: Check against max pkey to avoid overflows") Link: http://lkml.kernel.org/r/20180509171358.47FD785E@viggo.jf.intel.com Signed-off-by: Ingo Molnar Signed-off-by: Greg Kroah-Hartman --- arch/x86/include/asm/mmu_context.h | 2 +- arch/x86/include/asm/pkeys.h | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) --- a/arch/x86/include/asm/mmu_context.h +++ b/arch/x86/include/asm/mmu_context.h @@ -192,7 +192,7 @@ static inline int init_new_context(struc #ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS if (cpu_feature_enabled(X86_FEATURE_OSPKE)) { - /* pkey 0 is the default and always allocated */ + /* pkey 0 is the default and allocated implicitly */ mm->context.pkey_allocation_map = 0x1; /* -1 means unallocated or invalid */ mm->context.execute_only_pkey = -1; --- a/arch/x86/include/asm/pkeys.h +++ b/arch/x86/include/asm/pkeys.h @@ -51,10 +51,10 @@ bool mm_pkey_is_allocated(struct mm_stru { /* * "Allocated" pkeys are those that have been returned - * from pkey_alloc(). pkey 0 is special, and never - * returned from pkey_alloc(). + * from pkey_alloc() or pkey 0 which is allocated + * implicitly when the mm is created. */ - if (pkey <= 0) + if (pkey < 0) return false; if (pkey >= arch_max_pkey()) return false;