Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp4831894pxy; Tue, 27 Apr 2021 13:46:45 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxf6kgyuu6iR1OUmnJGxUvr7xsG0XDkSGy6rbd0a7qoDTmQKrRjbqgTwGhlk+gSJhvbHsnp X-Received: by 2002:a05:6a00:16c2:b029:228:964e:8b36 with SMTP id l2-20020a056a0016c2b0290228964e8b36mr25327929pfc.11.1619556405602; Tue, 27 Apr 2021 13:46:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619556405; cv=none; d=google.com; s=arc-20160816; b=anT5I8FARaZD5Jw93NP7Vz5zM1N/rUTXQsApEz2lGr6R7LBvK5xm6yGMNxB289NBZq ENkjpNYB3ng5OzZjnOv58/l+b9O79NmI82dRO/WqZJQie+ZpT7xE30z+IOQtPJ/njjUp LRazyHPQ4hiY49N/jVstz8zGWhQmWoBa9BGufBMaOJw9i+ToydkTcHR7hlYi1qzQue3T YTK0wIsZHey+JnIsiS0m8bG1Gl6bGlJsYVxA22EOpoAGDSbyTAy/KEFIfjjOfr9pXfuz JGqL5RafKlkOcSVVJZnl0mX9kQMwKLAs63sMl0iLgzdyqrgbXcvyVQWTjEUSCFK2GsCa 35qw== 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 :ironport-sdr:ironport-sdr; bh=gvUNH02o2ArH4/aqBYeR6l0ybBENR7ZTr1+KMi6Ykqw=; b=R3tdMSEi+U393pvsaBlh6g/dEISRctjje53WNwuQ3rqfkoutpAXCySdZFuYMmJVgpi sA8pM+bwGMIGFDwKxyO74/ddr0xAm2zfn5wd+L86daI7l+ZOWwQjynO3pbIpfu7pfhJf x0ZrtAICqUeWiGAdCYqzEqx3+bw6EHQBwjTVOb723HdyaLZLe0HUAEWU8kNpOhdz/zIM 19RMSJAUQsrSBKXIrgAuZer12OGc9aPXGrTGS8KiMPBDiXta6eLotIBgdx/sQ865LwRz jw1hmAQFuR7tzy2sAF3zTeKCfO7TLzs4/Sz9gYoLnnZLZRbj0RHgMcnLcHtJKuXgXYkl Cmsw== 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 g21si946289pgl.526.2021.04.27.13.46.32; Tue, 27 Apr 2021 13:46:45 -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 S239389AbhD0Uqf (ORCPT + 99 others); Tue, 27 Apr 2021 16:46:35 -0400 Received: from mga05.intel.com ([192.55.52.43]:31780 "EHLO mga05.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239183AbhD0UqT (ORCPT ); Tue, 27 Apr 2021 16:46:19 -0400 IronPort-SDR: NBeY9gqQIDRS6JTJsY0HSbmj1qprTpdgXPao9j6tJ1DJ6WP3Z87Khmz6X5iF3NGUE9AjTDcqHY ITH6SDt1J+DA== X-IronPort-AV: E=McAfee;i="6200,9189,9967"; a="281922493" X-IronPort-AV: E=Sophos;i="5.82,255,1613462400"; d="scan'208";a="281922493" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Apr 2021 13:44:22 -0700 IronPort-SDR: 75VLjNgq4HBl3JHUeKIoj71GJcm5Gbcd3TtQYTMTQH/p9nAv02K9RPUsZm2A3RWacsigMkHh5P jeDPO8y3iV0g== X-IronPort-AV: E=Sophos;i="5.82,255,1613462400"; d="scan'208";a="465623548" Received: from yyu32-desk.sc.intel.com ([143.183.136.146]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Apr 2021 13:44:21 -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 Cc: Yu-cheng Yu Subject: [PATCH v26 23/30] x86/cet/shstk: Handle thread shadow stack Date: Tue, 27 Apr 2021 13:43:08 -0700 Message-Id: <20210427204315.24153-24-yu-cheng.yu@intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20210427204315.24153-1-yu-cheng.yu@intel.com> References: <20210427204315.24153-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 specified, 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, but cap it to min(RLIMIT_STACK, 4 GB). 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. Signed-off-by: Yu-cheng Yu --- arch/x86/include/asm/cet.h | 5 +++ arch/x86/include/asm/mmu_context.h | 3 ++ arch/x86/kernel/process.c | 15 ++++++-- arch/x86/kernel/shstk.c | 57 +++++++++++++++++++++++++++++- 4 files changed, 76 insertions(+), 4 deletions(-) diff --git a/arch/x86/include/asm/cet.h b/arch/x86/include/asm/cet.h index aa85d599b184..8b83ded577cc 100644 --- a/arch/x86/include/asm/cet.h +++ b/arch/x86/include/asm/cet.h @@ -16,10 +16,15 @@ struct cet_status { #ifdef CONFIG_X86_SHADOW_STACK int shstk_setup(void); +int shstk_setup_thread(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_setup_thread(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..53569114aa01 100644 --- a/arch/x86/include/asm/mmu_context.h +++ b/arch/x86/include/asm/mmu_context.h @@ -11,6 +11,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 9c214d7085a4..fa01e8679d01 100644 --- a/arch/x86/kernel/process.c +++ b/arch/x86/kernel/process.c @@ -43,6 +43,7 @@ #include #include #include +#include #include "process.h" @@ -109,6 +110,7 @@ void exit_thread(struct task_struct *tsk) free_vm86(t); + shstk_free(tsk); fpu__drop(fpu); } @@ -122,8 +124,9 @@ static int set_new_tls(struct task_struct *p, unsigned long tls) return do_set_thread_area_64(p, ARCH_SET_FS, tls); } -int copy_thread(unsigned long clone_flags, unsigned long sp, unsigned long arg, - struct task_struct *p, unsigned long tls) +int copy_thread(unsigned long clone_flags, unsigned long sp, + unsigned long stack_size, struct task_struct *p, + unsigned long tls) { struct inactive_task_frame *frame; struct fork_frame *fork_frame; @@ -163,7 +166,7 @@ int copy_thread(unsigned long clone_flags, unsigned long sp, unsigned long arg, /* Kernel thread ? */ if (unlikely(p->flags & (PF_KTHREAD | PF_IO_WORKER))) { memset(childregs, 0, sizeof(struct pt_regs)); - kthread_frame_init(frame, sp, arg); + kthread_frame_init(frame, sp, stack_size); return 0; } @@ -181,6 +184,12 @@ int copy_thread(unsigned long clone_flags, unsigned long sp, unsigned long arg, if (clone_flags & CLONE_SETTLS) ret = set_new_tls(p, tls); +#ifdef CONFIG_X86_64 + /* Allocate a new shadow stack for pthread */ + if (!ret) + ret = shstk_setup_thread(p, clone_flags, stack_size); +#endif + 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 c815c7507830..d387df84b7f1 100644 --- a/arch/x86/kernel/shstk.c +++ b/arch/x86/kernel/shstk.c @@ -70,6 +70,55 @@ int shstk_setup(void) return 0; } +int shstk_setup_thread(struct task_struct *tsk, unsigned long clone_flags, + unsigned long stack_size) +{ + unsigned long addr, size; + struct cet_user_state *state; + struct cet_status *cet = &tsk->thread.cet; + + if (!cet->shstk_size) + return 0; + + if ((clone_flags & (CLONE_VFORK | CLONE_VM)) != CLONE_VM) + return 0; + + state = get_xsave_addr(&tsk->thread.fpu.state.xsave, + XFEATURE_CET_USER); + + if (!state) + return -EINVAL; + + if (stack_size == 0) + return -EINVAL; + + /* Cap shadow stack size to 4 GB */ + size = min_t(unsigned long long, rlimit(RLIMIT_STACK), SZ_4G); + size = min(size, stack_size); + + /* + * 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()) + size /= 4; + size = round_up(size, PAGE_SIZE); + addr = alloc_shstk(size); + + if (IS_ERR_VALUE(addr)) { + cet->shstk_base = 0; + cet->shstk_size = 0; + return PTR_ERR((void *)addr); + } + + fpu__prepare_write(&tsk->thread.fpu); + state->user_ssp = (u64)(addr + size); + cet->shstk_base = addr; + cet->shstk_size = size; + return 0; +} + void shstk_free(struct task_struct *tsk) { struct cet_status *cet = &tsk->thread.cet; @@ -79,7 +128,13 @@ void shstk_free(struct task_struct *tsk) !cet->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 is + * sharing the same mm struct. + */ + if (!tsk->mm || tsk->mm != current->mm) return; while (1) { -- 2.21.0