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=-6.9 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=unavailable 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 99B33C35E0F for ; Tue, 25 Feb 2020 20:16:09 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 546F721744 for ; Tue, 25 Feb 2020 20:16:09 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="GAF43oao" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 546F721744 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=chromium.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 07E0D6B0005; Tue, 25 Feb 2020 15:16:09 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 02DD26B0007; Tue, 25 Feb 2020 15:16:08 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E377D6B0008; Tue, 25 Feb 2020 15:16:08 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0128.hostedemail.com [216.40.44.128]) by kanga.kvack.org (Postfix) with ESMTP id C3A1D6B0005 for ; Tue, 25 Feb 2020 15:16:08 -0500 (EST) Received: from smtpin29.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 8357D180AD80F for ; Tue, 25 Feb 2020 20:16:08 +0000 (UTC) X-FDA: 76529756016.29.sink41_6fbbd36c13f45 X-HE-Tag: sink41_6fbbd36c13f45 X-Filterd-Recvd-Size: 6452 Received: from mail-pl1-f196.google.com (mail-pl1-f196.google.com [209.85.214.196]) by imf22.hostedemail.com (Postfix) with ESMTP for ; Tue, 25 Feb 2020 20:16:07 +0000 (UTC) Received: by mail-pl1-f196.google.com with SMTP id j7so273204plt.1 for ; Tue, 25 Feb 2020 12:16:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=RVriVMt9B1z7tUDJAr2xJjSmnlOpNUHydkB5FDLVKeU=; b=GAF43oaoRNtdxzQZenAQT+RFt4hEoY74zIx4Ts94IuklRKPnYoVAdw3Z/nLOHu1lRD Fr/45UkbQl5Dwqp9UUOpoe0gg1TKGykbO5z1chVEgJeFvmHIeaUoKvwc3aIJJLq3Irjm GWYJgFFGMRmY4ERQYDWhAUoQc+PURd9UAARns= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=RVriVMt9B1z7tUDJAr2xJjSmnlOpNUHydkB5FDLVKeU=; b=XX3u0JR9e1Rkk6Tl3irlQfyYg/0z0a+MmXYiz3fhnd0jjPJbHLX08fU1q7e3Oy4BDz OHLEh4VsRJyGrS6mMVGC6hEkgdjMgXxSk4BvsZbeC3tfAZVht3P7eCn5YtRqLlkYWYRK uFNDAH1XcPJJoHRS2ieoa2jKzlU/eDoAh/8o+ImrFppjnrldSt8B52QYhT2evO7doSnl VmBdmf5qLOhBLcFuCSOc8u5mMrtpQM5c0rW1B3n1LdQrA/K+sspYLzK6kL7EEo2R0TsC Thl0cL+c1OekAKkle8Rf0REGwt7o1Qck6ArNpQg+Xhu6zqIjNXSwULitDWpQg2wDkpJH ixyA== X-Gm-Message-State: APjAAAXbMheXjOZjKBrD5BR6ZBR/mpV/Nw6rpjNCLnE66TJwIxLe/iPx EGsyjIUuyXayTpT0xQCfc1tbHg== X-Google-Smtp-Source: APXvYqyaptWR9wI2Enrg3ZClqaJ6Jmlx/f50KgN2LoG46+BPg2u5z03Q2p2enk9db8pEcN+7i35xhA== X-Received: by 2002:a17:90a:2486:: with SMTP id i6mr876306pje.9.1582661766805; Tue, 25 Feb 2020 12:16:06 -0800 (PST) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id w25sm17835454pfi.106.2020.02.25.12.16.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Feb 2020 12:16:05 -0800 (PST) Date: Tue, 25 Feb 2020 12:16:04 -0800 From: Kees Cook To: Yu-cheng Yu Cc: x86@kernel.org, "H. Peter Anvin" , Thomas Gleixner , Ingo Molnar , linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-api@vger.kernel.org, Arnd Bergmann , Andy Lutomirski , Balbir Singh , Borislav Petkov , Cyrill Gorcunov , Dave Hansen , Eugene Syromiatnikov , Florian Weimer , "H.J. Lu" , Jann Horn , Jonathan Corbet , Mike Kravetz , Nadav Amit , Oleg Nesterov , Pavel Machek , Peter Zijlstra , Randy Dunlap , "Ravi V. Shankar" , Vedvyas Shanbhogue , Dave Martin , x86-patch-review@intel.com Subject: Re: [RFC PATCH v9 13/27] x86/mm: Shadow Stack page fault error checking Message-ID: <202002251216.EB9BEDD9D0@keescook> References: <20200205181935.3712-1-yu-cheng.yu@intel.com> <20200205181935.3712-14-yu-cheng.yu@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200205181935.3712-14-yu-cheng.yu@intel.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: On Wed, Feb 05, 2020 at 10:19:21AM -0800, Yu-cheng Yu wrote: > If a page fault is triggered by a Shadow Stack (SHSTK) access > (e.g. CALL/RET) or SHSTK management instructions (e.g. WRUSSQ), then bit[6] > of the page fault error code is set. > > In access_error(), verify a SHSTK page fault is within a SHSTK memory area. > It is always an error otherwise. > > For a valid SHSTK access, set FAULT_FLAG_WRITE to effect copy-on-write. > > Signed-off-by: Yu-cheng Yu Reviewed-by: Kees Cook -Kees > --- > arch/x86/include/asm/traps.h | 2 ++ > arch/x86/mm/fault.c | 18 ++++++++++++++++++ > 2 files changed, 20 insertions(+) > > diff --git a/arch/x86/include/asm/traps.h b/arch/x86/include/asm/traps.h > index 7ac26bbd0bef..8023d177fcd8 100644 > --- a/arch/x86/include/asm/traps.h > +++ b/arch/x86/include/asm/traps.h > @@ -169,6 +169,7 @@ enum { > * bit 3 == 1: use of reserved bit detected > * bit 4 == 1: fault was an instruction fetch > * bit 5 == 1: protection keys block access > + * bit 6 == 1: shadow stack access fault > */ > enum x86_pf_error_code { > X86_PF_PROT = 1 << 0, > @@ -177,5 +178,6 @@ enum x86_pf_error_code { > X86_PF_RSVD = 1 << 3, > X86_PF_INSTR = 1 << 4, > X86_PF_PK = 1 << 5, > + X86_PF_SHSTK = 1 << 6, > }; > #endif /* _ASM_X86_TRAPS_H */ > diff --git a/arch/x86/mm/fault.c b/arch/x86/mm/fault.c > index 304d31d8cbbc..9c1243302663 100644 > --- a/arch/x86/mm/fault.c > +++ b/arch/x86/mm/fault.c > @@ -1187,6 +1187,17 @@ access_error(unsigned long error_code, struct vm_area_struct *vma) > (error_code & X86_PF_INSTR), foreign)) > return 1; > > + /* > + * Verify X86_PF_SHSTK is within a Shadow Stack VMA. > + * It is always an error if there is a Shadow Stack > + * fault outside a Shadow Stack VMA. > + */ > + if (error_code & X86_PF_SHSTK) { > + if (!(vma->vm_flags & VM_SHSTK)) > + return 1; > + return 0; > + } > + > if (error_code & X86_PF_WRITE) { > /* write, present and write, not present: */ > if (unlikely(!(vma->vm_flags & VM_WRITE))) > @@ -1344,6 +1355,13 @@ void do_user_addr_fault(struct pt_regs *regs, > > perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address); > > + /* > + * If the fault is caused by a Shadow Stack access, > + * i.e. CALL/RET/SAVEPREVSSP/RSTORSSP, then set > + * FAULT_FLAG_WRITE to effect copy-on-write. > + */ > + if (hw_error_code & X86_PF_SHSTK) > + flags |= FAULT_FLAG_WRITE; > if (hw_error_code & X86_PF_WRITE) > flags |= FAULT_FLAG_WRITE; > if (hw_error_code & X86_PF_INSTR) > -- > 2.21.0 > -- Kees Cook