Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp848065pxv; Thu, 22 Jul 2021 13:55:57 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz8e1QW/ZiFO6jItPJPZsCvp0WdTEIZWAnrrjJJH88gbH4od09LiZ2kSZJk30eD7AByp1HR X-Received: by 2002:a92:ab0a:: with SMTP id v10mr1164832ilh.17.1626987357671; Thu, 22 Jul 2021 13:55:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626987357; cv=none; d=google.com; s=arc-20160816; b=SnDH3E+efo3rcw0vVP/EVyAxH+lVqeEt3IDOnlA9anq+z2o+/GQx4v9IuPFcpa+Zbx N/9SXpZulZdbTpdLx4/0cyNPwS7ka07pRJKE4iIOxXLcPsVMZSyt4YxVolllTbrYrH9q VfG1lEqT3qxWFxFGxAojWI/tZuIEhS5xLrqNp3c7QH0PZfdC65hr9uHJ8aGj2+vk9Rs5 9PgM/x54tZlBqQiyfGm+MpODrW9gTqxO6aJFusvmGNwQrrPtZqCKh+OWmB3Ecukyg6Wy pu3eOh6/AjVB4Ex+daK9NzQoR2pLxcqkNkOJSb4icouIo1HfkCvv6rV6Ej4BJAwT5SmG UCZQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=ypmshHe7DxPvlC1G9jdvK5KqH136v1o2KKm5SpMa86g=; b=ZozR9xxZaVf+jnVEpZk/rUP7gQXHR3t3HYCxA5U7s9HzVzkdeZs2lpNfdBMWILwBhs TR4QBtXDKDTrMCKPNIXPURdBkf5l/AqtpOSgEoawPJ3Fa058Vrkc3nLV+qHKmdS+i6tx c2HPH/aRZtH+3lsrAdWH1d7uO7tte8CDQXWc+THlhNJqEo8FevpvOIBxDF4/FT28SYWk JXwaMCd61AJwt8t2HnRKnGtLRe1ACqG+zP30aL6hss34xWavSdGQzc+MgaGEsKTsWZ5W FhDu2OkeYMM6RYDUsw1mzxrpmdWOqgLqrT3+8ZOgT2invOVTY7XhJAdJE0s3jZkntim0 0nDg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id x90si21117602jah.3.2021.07.22.13.55.46; Thu, 22 Jul 2021 13:55:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232097AbhGVUOD (ORCPT + 99 others); Thu, 22 Jul 2021 16:14:03 -0400 Received: from mga07.intel.com ([134.134.136.100]:54383 "EHLO mga07.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231318AbhGVUM3 (ORCPT ); Thu, 22 Jul 2021 16:12:29 -0400 X-IronPort-AV: E=McAfee;i="6200,9189,10053"; a="275560625" X-IronPort-AV: E=Sophos;i="5.84,262,1620716400"; d="scan'208";a="275560625" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Jul 2021 13:53:03 -0700 X-IronPort-AV: E=Sophos;i="5.84,262,1620716400"; d="scan'208";a="502035505" Received: from yyu32-desk.sc.intel.com ([143.183.136.146]) by fmsmga003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Jul 2021 13:53:03 -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 , Borislav Petkov , 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 , Dave Martin , Weijiang Yang , Pengfei Xu , Haitao Huang , Rick P Edgecombe Cc: Yu-cheng Yu Subject: [PATCH v28 25/32] x86/cet/shstk: Handle thread shadow stack Date: Thu, 22 Jul 2021 13:52:12 -0700 Message-Id: <20210722205219.7934-26-yu-cheng.yu@intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20210722205219.7934-1-yu-cheng.yu@intel.com> References: <20210722205219.7934-1-yu-cheng.yu@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org For clone() with CLONE_VM, except vfork, the child and the parent must have separate shadow stacks. Thus, the kernel allocates, and frees on thread exit a new shadow stack for the child. Use stack_size passed from clone3() syscall for thread shadow stack size. A compat-mode thread shadow stack size is further reduced to 1/4. This allows more threads to run in a 32-bit address space. The earlier version of clone() did not have stack_size passed in. In that case, use RLIMIT_STACK size and cap to 4 GB. Signed-off-by: Yu-cheng Yu --- v28: - Split out copy_thread() argument name changes to a new patch. - Add compatibility for earlier clone(), which does not pass stack_size. - Add comment for get_xsave_addr(), explain the handling of null return value. arch/x86/include/asm/cet.h | 5 +++ arch/x86/include/asm/mmu_context.h | 3 ++ arch/x86/kernel/process.c | 6 +++ arch/x86/kernel/shstk.c | 63 +++++++++++++++++++++++++++++- 4 files changed, 76 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/cet.h b/arch/x86/include/asm/cet.h index 6432baf4de1f..4314a41ab3c9 100644 --- a/arch/x86/include/asm/cet.h +++ b/arch/x86/include/asm/cet.h @@ -17,10 +17,15 @@ struct thread_shstk { #ifdef CONFIG_X86_SHADOW_STACK int shstk_setup(void); +int shstk_alloc_thread_stack(struct task_struct *p, unsigned long clone_flags, + unsigned long stack_size); void shstk_free(struct task_struct *p); void shstk_disable(void); #else static inline int shstk_setup(void) { return 0; } +static inline int shstk_alloc_thread_stack(struct task_struct *p, + unsigned long clone_flags, + unsigned long stack_size) { return 0; } static inline void shstk_free(struct task_struct *p) {} static inline void shstk_disable(void) {} #endif diff --git a/arch/x86/include/asm/mmu_context.h b/arch/x86/include/asm/mmu_context.h index 27516046117a..e1dd083261a5 100644 --- a/arch/x86/include/asm/mmu_context.h +++ b/arch/x86/include/asm/mmu_context.h @@ -12,6 +12,7 @@ #include #include #include +#include extern atomic64_t last_mm_ctx_id; @@ -146,6 +147,8 @@ do { \ #else #define deactivate_mm(tsk, mm) \ do { \ + if (!tsk->vfork_done) \ + shstk_free(tsk); \ load_gs_index(0); \ loadsegment(fs, 0); \ } while (0) diff --git a/arch/x86/kernel/process.c b/arch/x86/kernel/process.c index e6e4d8bc9023..bade6a594d63 100644 --- a/arch/x86/kernel/process.c +++ b/arch/x86/kernel/process.c @@ -43,6 +43,7 @@ #include #include #include +#include #include "process.h" @@ -103,6 +104,7 @@ void exit_thread(struct task_struct *tsk) free_vm86(t); + shstk_free(tsk); fpu__drop(fpu); } @@ -200,6 +202,10 @@ int copy_thread(unsigned long clone_flags, unsigned long sp, if (clone_flags & CLONE_SETTLS) ret = set_new_tls(p, tls); + /* Allocate a new shadow stack for pthread */ + if (!ret) + ret = shstk_alloc_thread_stack(p, clone_flags, stack_size); + if (!ret && unlikely(test_tsk_thread_flag(current, TIF_IO_BITMAP))) io_bitmap_share(p); diff --git a/arch/x86/kernel/shstk.c b/arch/x86/kernel/shstk.c index 5993aa8db338..a3fecd608388 100644 --- a/arch/x86/kernel/shstk.c +++ b/arch/x86/kernel/shstk.c @@ -75,6 +75,61 @@ int shstk_setup(void) return err; } +int shstk_alloc_thread_stack(struct task_struct *tsk, unsigned long clone_flags, + unsigned long stack_size) +{ + struct thread_shstk *shstk = &tsk->thread.shstk; + struct cet_user_state *state; + unsigned long addr; + + /* + * Earlier clone() does not pass stack_size. Use RLIMIT_STACK and + * cap to 4 GB. + */ + if (!stack_size) + stack_size = min_t(unsigned long long, rlimit(RLIMIT_STACK), SZ_4G); + + if (!shstk->size) + return 0; + + /* + * For CLONE_VM, except vfork, the child needs a separate shadow + * stack. + */ + if ((clone_flags & (CLONE_VFORK | CLONE_VM)) != CLONE_VM) + return 0; + + /* + * This is in clone() syscall and fpu__copy() already copies xstates + * from the parent. If get_xsave_addr() returns null, then XFEATURE_ + * CET_USER is still in init state, which certainly is an error. + */ + state = get_xsave_addr(&tsk->thread.fpu.state.xsave, XFEATURE_CET_USER); + if (!state) + return -EINVAL; + + /* + * Compat-mode pthreads share a limited address space. + * If each function call takes an average of four slots + * stack space, allocate 1/4 of stack size for shadow stack. + */ + if (in_compat_syscall()) + stack_size /= 4; + + stack_size = round_up(stack_size, PAGE_SIZE); + addr = alloc_shstk(stack_size); + if (IS_ERR_VALUE(addr)) { + shstk->base = 0; + shstk->size = 0; + return PTR_ERR((void *)addr); + } + + state->user_ssp = (u64)(addr + stack_size); + shstk->base = addr; + shstk->size = stack_size; + return 0; +} + void shstk_free(struct task_struct *tsk) { struct thread_shstk *shstk = &tsk->thread.shstk; @@ -84,7 +139,13 @@ void shstk_free(struct task_struct *tsk) !shstk->base) return; - if (!tsk->mm) + /* + * When fork() with CLONE_VM fails, the child (tsk) already has a + * shadow stack allocated, and exit_thread() calls this function to + * free it. In this case the parent (current) and the child share + * the same mm struct. + */ + if (!tsk->mm || tsk->mm != current->mm) return; while (1) { -- 2.21.0