From patchwork Mon Nov 19 21:48:06 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: 10689559 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 B6A3613BB for ; Mon, 19 Nov 2018 21:55:21 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A746F2A583 for ; Mon, 19 Nov 2018 21:55:21 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9AD9E2A6EE; Mon, 19 Nov 2018 21:55: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 00B2B2A583 for ; Mon, 19 Nov 2018 21:55:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 89F996B1CA4; Mon, 19 Nov 2018 16:54:35 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 829186B1CA6; Mon, 19 Nov 2018 16:54:35 -0500 (EST) 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 606F36B1CA7; Mon, 19 Nov 2018 16:54:35 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pl1-f197.google.com (mail-pl1-f197.google.com [209.85.214.197]) by kanga.kvack.org (Postfix) with ESMTP id 154FB6B1CA4 for ; Mon, 19 Nov 2018 16:54:35 -0500 (EST) Received: by mail-pl1-f197.google.com with SMTP id d11-v6so24533306plo.17 for ; Mon, 19 Nov 2018 13:54:35 -0800 (PST) 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=/HKdGiQvtW5jf7nkNgP3KN8Y7Fo/pfx9me0h5Ldadis=; b=uYD2PiVmdW/+yzWVjipwGuyvMYLUf9S0KytLmX/30+wfh0PxdtLcB1DSWKMpMjOP30 VPsAU5TN6pcswILAlb8xmP51uDo1D3OEuRIVqc14rwqKTml0hC+m/aYQLAONX0+xKxPT Ie1pFxQxeD/m+rrmGv7t0IosVqDkHroAo6Pu0hTasXVgZvQK6iKi3sXFNn142dUH2v5p 1B7xOVmwoOJsOI4N5vnVvjLTXff3lVqb5f7yDM4DxalWcoFDMSkPni2qmNsitUuL33Fg 6RbVJygLNbMqzh2KSeUfBIzB9QrlCLDbn8RoH6HkO6dYzMq9il0hD1sD/UqkpmmaH8jR U2oA== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of yu-cheng.yu@intel.com designates 192.55.52.93 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: AGRZ1gKun7HH3PzsHR3mylt/l/fz3H15UWLCGAy1zErYPTipxsVmdcSr LiXOlQiOQS7zD3XAo/H8N5HRJvPfZkZYTVLVXmgfDiKTnL8/arHv+/YWhI19KlU1C4CDsnu01L3 IZa90nG7tZsNOMsqHL5BhQ0PDoV7KugmzJqUY8JddrQbgbz66eN5Bx9hq73g9LnrYyA== X-Received: by 2002:a62:b15:: with SMTP id t21mr11895390pfi.136.1542664474743; Mon, 19 Nov 2018 13:54:34 -0800 (PST) X-Google-Smtp-Source: AJdET5cUwF5cvFm1gdNpa1hgDndNv94kJUwQNZGqrsPPNFNpzfhtnakhhgRZXrB1XyQjpaNfDoJm X-Received: by 2002:a62:b15:: with SMTP id t21mr11895344pfi.136.1542664473483; Mon, 19 Nov 2018 13:54:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1542664473; cv=none; d=google.com; s=arc-20160816; b=OsmMpoPxuilba3OTVs+5CQN2+sUqUXNkR8YjQu51PVzcAUi4z+p7rSgaT/JSgL5sHD doeg3EqyzQSzMsAfzrWeAM8wB4f96dJV+albuGu2Vfx1fJj3vTKGtMk7MgG7iYU7lA37 b/K5kCgu2mTgL0diOijcuwPWzj4zyMucxmZl00gtMDbjzUuZQ+Q5MaamC+77HzyRfXk5 9Uk5cSIPkZEl3r698tR2fKiyEeWwMfzp2QLQg4+ysYtockESyOZVaJk3ETkojgTI5p+T bX//UKxEeaoHscLZM+VoFGYKwIgTN6QsXUtGLvsbW2K1fb1ETZgT+AEh+5MoB/es/Jjk Gcig== 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=/HKdGiQvtW5jf7nkNgP3KN8Y7Fo/pfx9me0h5Ldadis=; b=MJyJUvH1vLb4SmTGoj7PSD/0r0LorpshC3F62JsG1Vmadu4b4bePV8ICyfHKqtCzYa kCTaLuUKI9UJbSeWcf3YJW69MzEiMfZ3lxZ8osZ/wrphISmkw7qzOZniaB20XywwFEQO NAvEn/pXomPpJP4J1vPc0Tq0axuXENgQPzmmNHgjosjlTX++4jEmP5bK2zCLHwRmnl8+ SRscX9OV3WO27Vg9ZHQbvjQ1FkVB2L/aVpZxL2lkn2w1Zi9c1Od/o1SZ7WSZLFUhUSqO b5pb8DuJjn17azA5nwyUVxmWsxv48zwsWaoDkI3E8yxdHwrjnTPJvlJaVn7M2HKQUSCE qkZQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of yu-cheng.yu@intel.com designates 192.55.52.93 as permitted sender) smtp.mailfrom=yu-cheng.yu@intel.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from mga11.intel.com (mga11.intel.com. [192.55.52.93]) by mx.google.com with ESMTPS id o11si40652866pgd.234.2018.11.19.13.54.33 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 19 Nov 2018 13:54:33 -0800 (PST) Received-SPF: pass (google.com: domain of yu-cheng.yu@intel.com designates 192.55.52.93 as permitted sender) client-ip=192.55.52.93; Authentication-Results: mx.google.com; spf=pass (google.com: domain of yu-cheng.yu@intel.com designates 192.55.52.93 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 orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 19 Nov 2018 13:54:33 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.56,254,1539673200"; d="scan'208";a="93319912" Received: from yyu32-desk1.sc.intel.com ([143.183.136.147]) by orsmga008.jf.intel.com with ESMTP; 19 Nov 2018 13:54:32 -0800 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: [RFC PATCH v6 23/26] x86/cet/shstk: Handle thread shadow stack Date: Mon, 19 Nov 2018 13:48:06 -0800 Message-Id: <20181119214809.6086-24-yu-cheng.yu@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181119214809.6086-1-yu-cheng.yu@intel.com> References: <20181119214809.6086-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. A 64-bit SHSTK has a fixed size of RLIMIT_STACK. A compat-mode thread SHSTK has a fixed size of 1/4 RLIMIT_STACK. This allows more threads to share a 32-bit address space. 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 | 40 ++++++++++++++++++++++++++++++ arch/x86/kernel/process.c | 1 + arch/x86/kernel/process_64.c | 7 ++++++ 5 files changed, 53 insertions(+) diff --git a/arch/x86/include/asm/cet.h b/arch/x86/include/asm/cet.h index 3af544aed800..5957e7257d83 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 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 -EINVAL; } diff --git a/arch/x86/include/asm/mmu_context.h b/arch/x86/include/asm/mmu_context.h index 0ca50611e8ce..57c1f6c42bef 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 44904c90d347..0e3e7a2c6f80 100644 --- a/arch/x86/kernel/cet.c +++ b/arch/x86/kernel/cet.c @@ -145,6 +145,46 @@ 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 = rlimit(RLIMIT_STACK); + + /* + * Compat-mode pthreads share a limited address space. + * If each function call takes an average of four slots + * stack space, we need 1/4 of stack size for shadow stack. + */ + if (in_compat_syscall()) + size /= 4; + + 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 0e0b4288a4b2..3b371a57426e 100644 --- a/arch/x86/kernel/process_64.c +++ b/arch/x86/kernel/process_64.c @@ -456,6 +456,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,