From patchwork Thu Oct 11 15:15:20 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: 10636893 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 1B411112B for ; Thu, 11 Oct 2018 15:22:22 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 09FF92BA40 for ; Thu, 11 Oct 2018 15:22:22 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id F1B4C2BA3B; Thu, 11 Oct 2018 15:22: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 703892BA36 for ; Thu, 11 Oct 2018 15:22:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D9D906B0282; Thu, 11 Oct 2018 11:21:02 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 9A9A16B028C; Thu, 11 Oct 2018 11:21:02 -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 5B30B6B0290; Thu, 11 Oct 2018 11:21:02 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pf1-f200.google.com (mail-pf1-f200.google.com [209.85.210.200]) by kanga.kvack.org (Postfix) with ESMTP id BA1026B028F for ; Thu, 11 Oct 2018 11:21:01 -0400 (EDT) Received: by mail-pf1-f200.google.com with SMTP id i81-v6so8148113pfj.1 for ; Thu, 11 Oct 2018 08:21:01 -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=w0kT9LujSLvc82kqhfwMcvEMstJT4PvNNrcS249mWtg=; b=Da1udrjXXpOZU1pz8D/Ckh06cz83q/TmJzEfQ2gzoLueyf3XCwsCkkBx4GkQf+eoil MMqKEEfzwY6M8zZgjxEIbNuLAAhUGid3AyTxihits/6aMVVkUNxc6WLzo52JOgGIFYG9 JuGMWjt3krNuxhkKNJUt7y835IoFKgbhES4mC3OEU8AAqh/vyadsyQoCyHRsm2PEmIzJ nvrd8dFzwxd5IB067aXFzUmukTCJfM9h+D2MKs5bfB4f12mQ5w5PpbYeegqHk1Oc+MRD pGmIWN7MlrumkRAM50Erw4j3dFoMARMJyo7TpYDklBrMZUpIsy62nJDydSwdTintTWHC AkQA== 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: ABuFfogXpoNhCxkUTJqofARDj5kypw9BFFMKCZ1NXpuxBblWl4gumQ+b zcstNfdz4FfDUAJRC7cKqmf6q7pS1q3KZKFTGis8bl8QDyhUJzGVE/wU6qT0UWezFnUKFv2AZmS k50FxIInrZzTg3FXijYc6VbPJ5U44kgdbTPil+8Q6XGdHb4KXpHSGyWuBah2fUfhiOg== X-Received: by 2002:a63:f448:: with SMTP id p8-v6mr1827897pgk.124.1539271261402; Thu, 11 Oct 2018 08:21:01 -0700 (PDT) X-Google-Smtp-Source: ACcGV61LonuUCfKu8sv9Ftg5dfInu/h26X8Stz1x4223AXhs0TOHnqo1yJCH/dxFQw+OWaMuHHlB X-Received: by 2002:a63:f448:: with SMTP id p8-v6mr1827853pgk.124.1539271260540; Thu, 11 Oct 2018 08:21:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1539271260; cv=none; d=google.com; s=arc-20160816; b=blfYrN28h+lMTXi18DTlofBkrHuHAnE/OJhn54nNXoEjmXFSTxVSluIJOTKik4Yq3v fJOpiVr2yihcaVot926GlzwLitkwIsLn9YqlBhxYYQeQwwxDPgZfyLLBmWxDKKVNuPR0 U3fSvUamEzFFyehWI93gBIM+IdYEw1eKf5tBgz+TZ7KdPSLbTD+mPM3ybCmsmUpFBNos iqnbXowo3w9WBcVAaZl/1yCiFUB6y/TCYzP9kqu6yygcWvmJpZq8oRHz0On8A5GX5mQF cADyRQlEEw09HelEmZUEcaQ9fs7grGz4ywl/k2pRsfykbBIPRd4IjrKc7vN/m6PE/QH9 SQkA== 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=w0kT9LujSLvc82kqhfwMcvEMstJT4PvNNrcS249mWtg=; b=z9GbW/78BJKEwEOR0t6aTa3IiN8V92rqqiiBlYEtPYpNmXeWl3P+uJmCUhTNThUx0s 3wm0muLUTzibH6wDIH9uTIzmH3e2DPbmjYUNPILvLaWtJc1cHAm3mfPWSx9YFQx6JjDP dGjnEI1yQ0vxDQo/ef2t/4/hrHUyv1vykxVOJS+dbx0NMXSPCSBJodr0vNHU8QHLUJr5 0+rATSjvORfcbZEor+aQpPF/IW0Zw1voKZsD0FGhCpewx5xFxzKYDAa+sd6d3dtVHsoa TFst4oBj5khbncb6YLCEPjkIf565+yenIy2tqXIz4TBGtIGYyiu7R+PY2+beG2J/kfnJ tnqw== 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 bg10-v6si26471290plb.47.2018.10.11.08.21.00 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 11 Oct 2018 08:21:00 -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:48 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.54,368,1534834800"; d="scan'208";a="78019169" Received: from 2b52.sc.intel.com ([143.183.136.147]) by fmsmga008.fm.intel.com with ESMTP; 11 Oct 2018 08:20:47 -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 24/27] x86/cet/shstk: Handle thread shadow stack Date: Thu, 11 Oct 2018 08:15:20 -0700 Message-Id: <20181011151523.27101-25-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 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 3af544aed800..008b4651b5be 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 -EINVAL; } +static inline int cet_setup_thread_shstk(struct task_struct *p) { return -EINVAL; } 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 -EINVAL; } 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 2000e5e87f8f..51b26616ac20 100644 --- a/arch/x86/kernel/cet.c +++ b/arch/x86/kernel/cet.c @@ -145,6 +145,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,