From patchwork Fri Sep 21 15:03:46 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: 10610271 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 4266817EE for ; Fri, 21 Sep 2018 15:10:21 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 336CC2E451 for ; Fri, 21 Sep 2018 15:10:21 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 315EB2E4CB; Fri, 21 Sep 2018 15:10:21 +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 95F1B2E4BE for ; Fri, 21 Sep 2018 15:10:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 25B6D8E001A; Fri, 21 Sep 2018 11:10:18 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 209BB8E0019; Fri, 21 Sep 2018 11:10:18 -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 0F9818E001A; Fri, 21 Sep 2018 11:10:18 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pl1-f199.google.com (mail-pl1-f199.google.com [209.85.214.199]) by kanga.kvack.org (Postfix) with ESMTP id C30C78E0019 for ; Fri, 21 Sep 2018 11:10:17 -0400 (EDT) Received: by mail-pl1-f199.google.com with SMTP id d10-v6so6251119pll.22 for ; Fri, 21 Sep 2018 08:10:17 -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=B3xJgz4x741BDQNBDqqpysPELaB2fR/elxB8Y4YSfZQ=; b=J6eBLMAitrIjA/WiUqFT1sxCQx0AFxQDmP9wDi4S9xO4QZAUyKx4xKjxLUreZyDlk9 ovJcPPbLQJNBo3E6AhPgyMAaouuJCzR+lQghHqgIh19RXJLDu1l7Wsj4XMYnv8s20DlH GlQQGE3mJWqhh/8QhEjWfWl8cf90zUBKT1+a2JhGB0nVyRxs6K8SACeEcJAuZMT39/+y SkS1i9+wCDibwTLonl4VBLo6aINs9+rng23JDeFV5z4iISLBrryHdU34Kj7iq1yZu58z x21L3kCfvRnWPrTSn2x+cxreDYjBRjduJjxFbf3GwpfKS5oQ2NerOis1tWsFD2xJoz/x AmPQ== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of yu-cheng.yu@intel.com designates 192.55.52.136 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: APzg51COD6DjhHXyyDjp6bXQg5J3vSa65wQAmWxAGs4uZXtzGhjTVB3C qDlcA8UExHBgoeWZ92NTPpXZDkKzVTKaLqdYQ8GqENVm9c5An9Fh0t8SJbf2VeSp08goyBoBi8J cslRWGVcJ5Ak9g2ok24d7T3B819HCAr9D2v2HxvyvcuUIguNEsTQp6akIPXNiLVDwSg== X-Received: by 2002:a17:902:1001:: with SMTP id b1-v6mr45006133pla.155.1537542617469; Fri, 21 Sep 2018 08:10:17 -0700 (PDT) X-Google-Smtp-Source: ANB0VdbFad84ofLUg2UILfMdHBMi03kojZb37+ukNDOHM8RKkr3/d6kfNrCYxVQPyj2DNaJb+5ti X-Received: by 2002:a17:902:1001:: with SMTP id b1-v6mr45000555pla.155.1537542532469; Fri, 21 Sep 2018 08:08:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537542532; cv=none; d=google.com; s=arc-20160816; b=Xz7qNkamLvTPBqJ5bHmHX7laAs9RIac64cY2yLltGqNjf2vdrN19vZcZNpWK+FXvBq 4BxOeJX/ViRE9enTYLLgCqEGNMCA8qZGuQDKcLfl4+0nJN0gIbluxn8ExZ6U/zCqTcKw n7HFy2qZQ71rF/QhlDRTtsUkM6g68RqVoNcxqIQCBgoXkmosM/bSPadaVjFRfXB48lYc aVoPIwqEFirmzFCbsB55yznYSlD/3CLz9TJk9iqjLaP9rjEGSSDo80g/Ij1OQ48epD9b Tlage4Zw/jF4RcwK6UEzDwqdMqSd9KJHPEjmWtVEXAiQb1sFkUK5EiYYENatsDSho5Ln C28g== 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=B3xJgz4x741BDQNBDqqpysPELaB2fR/elxB8Y4YSfZQ=; b=0D3sRy/yTy6ves/8smq/yNwCg1qr1awvR7kOD9AZ567YICg9xxOBlneIYx1gDIl0Qh GAgQOw+ytH0JPDB+Smwz+c5TBFbdanw+kQBBsswcTXLSIyVifN8gz7OtmxgN5sVIYogW f8QAJvY5mCyV1kme20SfgxoKx03L0BQdm4RtvO+3dV11ePfw6QaW2KTME33RxVozEKIo A4bh8HgQxuZbiFDLfabWeD9yylqS8uZt0ggV7CJL0yVI/XF2uukw7QNlr8/Zgai+AznW jRmjSMIiDZawm38htWBLgTFyRQGGXe6wKLppJUFgDfSQ6M9LnJywqAAx3xL/pLj2qFHF y0cA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of yu-cheng.yu@intel.com designates 192.55.52.136 as permitted sender) smtp.mailfrom=yu-cheng.yu@intel.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from mga12.intel.com (mga12.intel.com. [192.55.52.136]) by mx.google.com with ESMTPS id r7-v6si26956600pga.77.2018.09.21.08.08.52 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 21 Sep 2018 08:08:52 -0700 (PDT) Received-SPF: pass (google.com: domain of yu-cheng.yu@intel.com designates 192.55.52.136 as permitted sender) client-ip=192.55.52.136; Authentication-Results: mx.google.com; spf=pass (google.com: domain of yu-cheng.yu@intel.com designates 192.55.52.136 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 fmsmga007.fm.intel.com ([10.253.24.52]) by fmsmga106.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 21 Sep 2018 08:08:51 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.54,285,1534834800"; d="scan'208";a="71856598" Received: from 2b52.sc.intel.com ([143.183.136.51]) by fmsmga007.fm.intel.com with ESMTP; 21 Sep 2018 08:08:51 -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 , 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: [RFC PATCH v4 22/27] x86/cet/shstk: Handle thread shadow stack Date: Fri, 21 Sep 2018 08:03:46 -0700 Message-Id: <20180921150351.20898-23-yu-cheng.yu@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180921150351.20898-1-yu-cheng.yu@intel.com> References: <20180921150351.20898-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 The shadow stack for clone/fork is handled as the following: (1) If ((clone_flags & (CLONE_VFORK | CLONE_VM)) == CLONE_VM), the kernel allocates (and frees on thread exit) a new SHSTK for the child. It is possible for the kernel to complete the clone syscall and set the child's SHSTK pointer to NULL and let the child thread allocate a SHSTK for itself. There are two issues in this approach: It is not compatible with existing code that does inline syscall and it cannot handle signals before the child can successfully allocate a SHSTK. (2) For (clone_flags & CLONE_VFORK), the child uses the existing SHSTK. (3) For all other cases, the SHSTK is copied/reused whenever the parent or the child does a call/ret. This patch handles cases (1) & (2). Case (3) is handled in the SHSTK page fault patches. Signed-off-by: Yu-cheng Yu --- arch/x86/include/asm/cet.h | 2 ++ arch/x86/include/asm/mmu_context.h | 3 +++ arch/x86/kernel/cet.c | 34 ++++++++++++++++++++++++++++++ arch/x86/kernel/process.c | 1 + arch/x86/kernel/process_64.c | 7 ++++++ 5 files changed, 47 insertions(+) diff --git a/arch/x86/include/asm/cet.h b/arch/x86/include/asm/cet.h index d9ae3d86cdd7..b7b33e1026bb 100644 --- a/arch/x86/include/asm/cet.h +++ b/arch/x86/include/asm/cet.h @@ -17,12 +17,14 @@ struct cet_status { #ifdef CONFIG_X86_INTEL_CET int cet_setup_shstk(void); +int cet_setup_thread_shstk(struct task_struct *p); void cet_disable_shstk(void); void cet_disable_free_shstk(struct task_struct *p); int cet_restore_signal(unsigned long ssp); int cet_setup_signal(bool ia32, unsigned long rstor, unsigned long *new_ssp); #else static inline int cet_setup_shstk(void) { return 0; } +static inline int cet_setup_thread_shstk(struct task_struct *p) { return 0; } static inline void cet_disable_shstk(void) {} static inline void cet_disable_free_shstk(struct task_struct *p) {} static inline int cet_restore_signal(unsigned long ssp) { return 0; } diff --git a/arch/x86/include/asm/mmu_context.h b/arch/x86/include/asm/mmu_context.h index eeeb9289c764..8da7c999b7ee 100644 --- a/arch/x86/include/asm/mmu_context.h +++ b/arch/x86/include/asm/mmu_context.h @@ -13,6 +13,7 @@ #include #include #include +#include extern atomic64_t last_mm_ctx_id; @@ -223,6 +224,8 @@ do { \ #else #define deactivate_mm(tsk, mm) \ do { \ + if (!tsk->vfork_done) \ + cet_disable_free_shstk(tsk); \ load_gs_index(0); \ loadsegment(fs, 0); \ } while (0) diff --git a/arch/x86/kernel/cet.c b/arch/x86/kernel/cet.c index 5cc4be6e0982..ce0b3b7b1160 100644 --- a/arch/x86/kernel/cet.c +++ b/arch/x86/kernel/cet.c @@ -134,6 +134,40 @@ int cet_setup_shstk(void) return 0; } +int cet_setup_thread_shstk(struct task_struct *tsk) +{ + unsigned long addr, size; + struct cet_user_state *state; + + if (!current->thread.cet.shstk_enabled) + return 0; + + state = get_xsave_addr(&tsk->thread.fpu.state.xsave, + XFEATURE_MASK_SHSTK_USER); + + if (!state) + return -EINVAL; + + size = tsk->thread.cet.shstk_size; + if (size == 0) + size = rlimit(RLIMIT_STACK); + + addr = do_mmap_locked(0, size, PROT_READ, + MAP_ANONYMOUS | MAP_PRIVATE, VM_SHSTK); + + if (addr >= TASK_SIZE_MAX) { + tsk->thread.cet.shstk_base = 0; + tsk->thread.cet.shstk_size = 0; + tsk->thread.cet.shstk_enabled = 0; + return -ENOMEM; + } + + state->user_ssp = (u64)(addr + size - sizeof(u64)); + tsk->thread.cet.shstk_base = addr; + tsk->thread.cet.shstk_size = size; + return 0; +} + void cet_disable_shstk(void) { u64 r; diff --git a/arch/x86/kernel/process.c b/arch/x86/kernel/process.c index 4a776da4c28c..440f012ef925 100644 --- a/arch/x86/kernel/process.c +++ b/arch/x86/kernel/process.c @@ -125,6 +125,7 @@ void exit_thread(struct task_struct *tsk) free_vm86(t); + cet_disable_free_shstk(tsk); fpu__drop(fpu); } diff --git a/arch/x86/kernel/process_64.c b/arch/x86/kernel/process_64.c index ea5ea850348d..9cdbd87bb908 100644 --- a/arch/x86/kernel/process_64.c +++ b/arch/x86/kernel/process_64.c @@ -325,6 +325,13 @@ int copy_thread_tls(unsigned long clone_flags, unsigned long sp, if (sp) childregs->sp = sp; + /* Allocate a new shadow stack for pthread */ + if ((clone_flags & (CLONE_VFORK | CLONE_VM)) == CLONE_VM) { + err = cet_setup_thread_shstk(p); + if (err) + goto out; + } + err = -ENOMEM; if (unlikely(test_tsk_thread_flag(me, TIF_IO_BITMAP))) { p->thread.io_bitmap_ptr = kmemdup(me->thread.io_bitmap_ptr,