From patchwork Thu Oct 11 15:15:11 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yu-cheng Yu X-Patchwork-Id: 10636873 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 2AAC6112B for ; Thu, 11 Oct 2018 15:21:41 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 17B3B2BA19 for ; Thu, 11 Oct 2018 15:21:41 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0B9032BA23; Thu, 11 Oct 2018 15:21:41 +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,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 BDE3A2BA1A for ; Thu, 11 Oct 2018 15:21:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6F18E6B0281; Thu, 11 Oct 2018 11:21:00 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 5AF956B027F; Thu, 11 Oct 2018 11:21:00 -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 42F586B0276; Thu, 11 Oct 2018 11:21:00 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pf1-f199.google.com (mail-pf1-f199.google.com [209.85.210.199]) by kanga.kvack.org (Postfix) with ESMTP id EB1336B0272 for ; Thu, 11 Oct 2018 11:20:59 -0400 (EDT) Received: by mail-pf1-f199.google.com with SMTP id l1-v6so8094644pfb.7 for ; Thu, 11 Oct 2018 08:20:59 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references; bh=k4I+IBksg1YUf85BSJ9dR9AdYeEvkDMb/4N/mPBj2zE=; b=HRlN/KJdQZcGXtKCx2XXppjfMmuG/W1g/R1wqGqFJbHJV2e/sba2QRGt2aKvEZFE/c Jar1eaKWtSTtEpKob4UaI4xzFLYClTQO9gpLOriy2YIbvh1UBrKGxdKiusWifQH1hk1L tfNF/LMBitdU2b7I/nE1KlwAQr3fLCw+WPK5ZRlvu00OFJkRxr1uhPlVjPkVNJQwkPRu Kqxo8xGg66ykR2bzp0OMclZkS15FeYECeKDE8hPLWze7j1OssFQpdJ9RX25XKTMoiCmO KzvxURwzMhaHSEDFRP5W+yzk1WWbhoZe124POWpfsKSJ/eimW3tO8jgFKjhiVqCie78k fj+g== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of yu-cheng.yu@intel.com designates 134.134.136.31 as permitted sender) smtp.mailfrom=yu-cheng.yu@intel.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com X-Gm-Message-State: ABuFfohse09Jy289G0pZxQRGwyFupS3ls3ctGanfTv946E9a7J4+iLYu cc/JcDh5uNCfrQAA3KOp2uFyBMbxSwtp3KSlWVYNoHeFf1VPEKAu0TpUHknOaLHhVtv33s1+oKw piFly45WBzGpUVWzB2s1ou/5AOMy81Puz2f3OxE9a75/1tumtQ0ywDAV1gpVmUjnzSA== X-Received: by 2002:a63:e05:: with SMTP id d5-v6mr1827752pgl.272.1539271259626; Thu, 11 Oct 2018 08:20:59 -0700 (PDT) X-Google-Smtp-Source: ACcGV604hSovoGjLczemV6i0bJNhS5ECDI18z63iVWQ2mGyrygyNtZTETh8CrItMrZtn1giW52VC X-Received: by 2002:a63:e05:: with SMTP id d5-v6mr1827716pgl.272.1539271258859; Thu, 11 Oct 2018 08:20:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1539271258; cv=none; d=google.com; s=arc-20160816; b=ItShhpQ4RkxHTlB5UNGyLLC9b3CCrd/VV1LoxTjx5AYCMoCHmspBlLoivus9J5at4G gSBTvxwoyo075PpM2enO/GBoyzz49zmEad38arKtG5gwsAn02wzl86xliyQvV5R8IOMB iDP1TgAKdbmUIwmsL50gQ+rTdU847ORSmgpXta8+1oSv0qjds7BXho1lnEms9N0cCxK3 SarD9lPu13rIy97EQcHadGtFZJ6h0jrEGVIqynP/Xpmc67F7Ky4NwBAKbYhG61Aa4ZMs jCyVfyMfJTweOqnMG+nFk0WBHp2g5vAF+jHpp2VSMvmwIIhLJIjf/XfUqq3HT5N2KjYv Skzw== 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; bh=k4I+IBksg1YUf85BSJ9dR9AdYeEvkDMb/4N/mPBj2zE=; b=dIkdDtCvwC+USjuz5S/PCH42fIpuHxhJXJ0XOtJH4wXUii7KJdyIAi2M2xA9ZorOhU f2x33BIBQ0iu7fJHqKhQkI6VPYOqkKUm7JuoITOdcdRz6yS/MDLQKevRTWU5xeYB0Uo2 lODVDFiB3PiAz8iO1r4ch2dQbhity9yPVJ48vCth1vPxJB0kMZIzoVWQEuh6Kev5hiGO /QMQCVvNq0Pcx0B+BLI3HKVzsZ7djGcEJap9R0PIjK5OLkdSWJMoglgf7wK+kgNQMVkR 7kFEJ2vXTBXVAHufvjIVZPPjDi5RVfR5Z2s4JgzTJ3JXh0WbzkqaIn0nYU0GO+yeaodO a9lA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of yu-cheng.yu@intel.com designates 134.134.136.31 as permitted sender) smtp.mailfrom=yu-cheng.yu@intel.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from mga06.intel.com (mga06.intel.com. [134.134.136.31]) by mx.google.com with ESMTPS id y62-v6si29748088pfy.139.2018.10.11.08.20.58 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 11 Oct 2018 08:20:58 -0700 (PDT) Received-SPF: pass (google.com: domain of yu-cheng.yu@intel.com designates 134.134.136.31 as permitted sender) client-ip=134.134.136.31; Authentication-Results: mx.google.com; spf=pass (google.com: domain of yu-cheng.yu@intel.com designates 134.134.136.31 as permitted sender) smtp.mailfrom=yu-cheng.yu@intel.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga104.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 11 Oct 2018 08:20:47 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.54,368,1534834800"; d="scan'208";a="78019126" Received: from 2b52.sc.intel.com ([143.183.136.147]) by fmsmga008.fm.intel.com with ESMTP; 11 Oct 2018 08:20:45 -0700 From: Yu-cheng Yu To: 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 , Cyrill Gorcunov , Dave Hansen , Eugene Syromiatnikov , Florian Weimer , "H.J. Lu" , Jann Horn , Jonathan Corbet , Kees Cook , Mike Kravetz , Nadav Amit , Oleg Nesterov , Pavel Machek , Peter Zijlstra , Randy Dunlap , "Ravi V. Shankar" , Vedvyas Shanbhogue Cc: Yu-cheng Yu Subject: [PATCH v5 15/27] x86/mm: Shadow stack page fault error checking Date: Thu, 11 Oct 2018 08:15:11 -0700 Message-Id: <20181011151523.27101-16-yu-cheng.yu@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181011151523.27101-1-yu-cheng.yu@intel.com> References: <20181011151523.27101-1-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: X-Virus-Scanned: ClamAV using ClamSMTP If a page fault is triggered by a shadow stack access (e.g. call/ret) or shadow stack management instructions (e.g. wrussq), then bit[6] of the page fault error code is set. In access_error(), we check if a shadow stack page fault is within a shadow stack memory area. Signed-off-by: Yu-cheng Yu --- 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 5196050ff3d5..58ea2f5722e9 100644 --- a/arch/x86/include/asm/traps.h +++ b/arch/x86/include/asm/traps.h @@ -157,6 +157,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, @@ -165,5 +166,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 47bebfe6efa7..7c3877a982f4 100644 --- a/arch/x86/mm/fault.c +++ b/arch/x86/mm/fault.c @@ -1162,6 +1162,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))) @@ -1300,6 +1311,13 @@ __do_page_fault(struct pt_regs *regs, unsigned long error_code, 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 (error_code & X86_PF_SHSTK) + flags |= FAULT_FLAG_WRITE; if (error_code & X86_PF_WRITE) flags |= FAULT_FLAG_WRITE; if (error_code & X86_PF_INSTR)