From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.3 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,NICE_REPLY_A, SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5691BC433E2 for ; Tue, 15 Sep 2020 00:13:04 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id AD5DF208DB for ; Tue, 15 Sep 2020 00:13:03 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org AD5DF208DB Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=intel.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id F04D96B0080; Mon, 14 Sep 2020 20:13:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E8D426B0087; Mon, 14 Sep 2020 20:13:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D0A078E0001; Mon, 14 Sep 2020 20:13:02 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0002.hostedemail.com [216.40.44.2]) by kanga.kvack.org (Postfix) with ESMTP id B6E8B6B0080 for ; Mon, 14 Sep 2020 20:13:02 -0400 (EDT) Received: from smtpin09.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 6947A181AC9B6 for ; Tue, 15 Sep 2020 00:13:02 +0000 (UTC) X-FDA: 77263370604.09.cats11_0a10edb2710c Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin09.hostedemail.com (Postfix) with ESMTP id 4252D180AD80F for ; Tue, 15 Sep 2020 00:13:02 +0000 (UTC) X-HE-Tag: cats11_0a10edb2710c X-Filterd-Recvd-Size: 6992 Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by imf02.hostedemail.com (Postfix) with ESMTP for ; Tue, 15 Sep 2020 00:13:00 +0000 (UTC) IronPort-SDR: aWSl3pobrWWxTvxhAjVSqVYgU1KZKWGzNUofb17I4sOqq+p3nekpc3sAAf3a5yHz6Gq6F1n3+V Z/+ZkjeCQ4fA== X-IronPort-AV: E=McAfee;i="6000,8403,9744"; a="146868002" X-IronPort-AV: E=Sophos;i="5.76,427,1592895600"; d="scan'208";a="146868002" X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Sep 2020 17:12:58 -0700 IronPort-SDR: ZlB2CRhnI3W3FESQD0mbE2yn6pZqDWFh+jKyQ9sX6ng8+q4N9SENSSceMOyulVv+ueDUQUkZNl dsTW+6FM8NBw== X-IronPort-AV: E=Sophos;i="5.76,427,1592895600"; d="scan'208";a="343303386" Received: from yyu32-mobl1.amr.corp.intel.com (HELO [10.212.244.74]) ([10.212.244.74]) by fmsmga003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Sep 2020 17:12:56 -0700 Subject: Re: [NEEDS-REVIEW] Re: [PATCH v11 25/25] x86/cet/shstk: Add arch_prctl functions for shadow stack To: Dave Hansen , Andy Lutomirski Cc: Dave Martin , "H.J. Lu" , Florian Weimer , X86 ML , "H. Peter Anvin" , Thomas Gleixner , Ingo Molnar , LKML , "open list:DOCUMENTATION" , Linux-MM , linux-arch , Linux API , Arnd Bergmann , Balbir Singh , Borislav Petkov , Cyrill Gorcunov , Dave Hansen , Eugene Syromiatnikov , Jann Horn , Jonathan Corbet , Kees Cook , Mike Kravetz , Nadav Amit , Oleg Nesterov , Pavel Machek , Peter Zijlstra , Randy Dunlap , "Ravi V. Shankar" , Vedvyas Shanbhogue , Weijiang Yang References: <086c73d8-9b06-f074-e315-9964eb666db9@intel.com> <0e9996bc-4c1b-cc99-9616-c721b546f857@intel.com> <4f2dfefc-b55e-bf73-f254-7d95f9c67e5c@intel.com> <20200901102758.GY6642@arm.com> <32005d57-e51a-7c7f-4e86-612c2ff067f3@intel.com> <46dffdfd-92f8-0f05-6164-945f217b0958@intel.com> <6e1e22a5-1b7f-2783-351e-c8ed2d4893b8@intel.com> <5979c58d-a6e3-d14d-df92-72cdeb97298d@intel.com> <08c91835-8486-9da5-a7d1-75e716fc5d36@intel.com> <41aa5e8f-ad88-2934-6d10-6a78fcbe019b@intel.com> From: "Yu, Yu-cheng" Message-ID: Date: Mon, 14 Sep 2020 17:12:54 -0700 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Thunderbird/68.12.0 MIME-Version: 1.0 In-Reply-To: <41aa5e8f-ad88-2934-6d10-6a78fcbe019b@intel.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Rspamd-Queue-Id: 4252D180AD80F X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam03 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: On 9/14/2020 7:50 AM, Dave Hansen wrote: > On 9/11/20 3:59 PM, Yu-cheng Yu wrote: > ... >> Here are the changes if we take the mprotect(PROT_SHSTK) approach. >> Any comments/suggestions? > > I still don't like it. :) > > I'll also be much happier when there's a proper changelog to accompany > this which also spells out the alternatives any why they suck so much. > >> diff --git a/arch/x86/include/uapi/asm/mman.h b/arch/x86/include/uapi/asm/mman.h >> index d4a8d0424bfb..024f006fcfe8 100644 >> --- a/arch/x86/include/uapi/asm/mman.h >> +++ b/arch/x86/include/uapi/asm/mman.h >> @@ -4,6 +4,8 @@ >> >> #define MAP_32BIT 0x40 /* only give out 32bit addresses */ >> >> +#define PROT_SHSTK 0x10 /* shadow stack pages */ >> + >> #ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS >> /* >> * Take the 4 protection key bits out of the vma->vm_flags >> @@ -19,13 +21,35 @@ >> ((vm_flags) & VM_PKEY_BIT2 ? _PAGE_PKEY_BIT2 : 0) | \ >> ((vm_flags) & VM_PKEY_BIT3 ? _PAGE_PKEY_BIT3 : 0)) >> >> -#define arch_calc_vm_prot_bits(prot, key) ( \ >> +#define pkey_vm_prot_bits(prot, key) ( \ >> ((key) & 0x1 ? VM_PKEY_BIT0 : 0) | \ >> ((key) & 0x2 ? VM_PKEY_BIT1 : 0) | \ >> ((key) & 0x4 ? VM_PKEY_BIT2 : 0) | \ >> ((key) & 0x8 ? VM_PKEY_BIT3 : 0)) >> +#else >> +#define pkey_vm_prot_bits(prot, key) >> #endif > > My inner compiler doesn't think this will compile: > > ( | shstk_vm_prot_bits(prot)) > > >> +#define shstk_vm_prot_bits(prot) ( \ >> + (static_cpu_has(X86_FEATURE_SHSTK) && (prot & PROT_SHSTK)) ? \ >> + VM_SHSTK : 0) > > Why do you need to filter PROT_SHSTK twice. Won't the prot passed in > here be filtered by arch_validate_prot()? X86_FEATURE_SHSTK is already checked by arch_validate_prot() and may be skipped. PROT_SHSTK needs to be tested and translated to VM_SHSTK here. > >> +#define arch_calc_vm_prot_bits(prot, key) \ >> + (pkey_vm_prot_bits(prot, key) | shstk_vm_prot_bits(prot)) >> + > > IMNHO, this is eminently more readable if you do: > > #define arch_calc_vm_prot_bits(prot, key) \ > (shstk_vm_prot_bits(prot)) \ > pkey_vm_prot_bits(prot, key)) > > BTW, can these be static inlines? I forget if I had a good reason for > making them #defines. Probably, at compile time, ((key) & 0x1 ? (VM_PKEY_BIT0 : 0) cannot be optimized out, unless there is a #define? Since we are adding the shadow stack stuff, why don't we make it static inline? Also do we want to check if the cpu has PKEY? [...] >> diff --git a/mm/mprotect.c b/mm/mprotect.c >> index a8edbcb3af99..520bd8caa005 100644 >> --- a/mm/mprotect.c >> +++ b/mm/mprotect.c >> @@ -571,6 +571,17 @@ static int do_mprotect_pkey(unsigned long start, size_t >> len, >> goto out; >> } >> } >> + >> + /* >> + * Only anonymous mapping is suitable for shadow stack. >> + */ > > Why? When a file mapping is turned into shadow stack, then the logic is strange. For instance, when a CALL triggers a shadow stack page fault, do we page in from a file? At least this seems to defeat shadow stack.