From patchwork Thu Jun 7 14:38:03 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: 10452305 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id C0EF960375 for ; Thu, 7 Jun 2018 14:41:55 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B0CDB294DB for ; Thu, 7 Jun 2018 14:41:55 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A45B029516; Thu, 7 Jun 2018 14:41:55 +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 A831D294DB for ; Thu, 7 Jun 2018 14:41:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 32D4E6B028C; Thu, 7 Jun 2018 10:41:35 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 1EFAE6B0288; Thu, 7 Jun 2018 10:41:35 -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 F32416B028B; Thu, 7 Jun 2018 10:41:34 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pf0-f197.google.com (mail-pf0-f197.google.com [209.85.192.197]) by kanga.kvack.org (Postfix) with ESMTP id 9E3826B0288 for ; Thu, 7 Jun 2018 10:41:34 -0400 (EDT) Received: by mail-pf0-f197.google.com with SMTP id j25-v6so3547114pfi.9 for ; Thu, 07 Jun 2018 07:41:34 -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=SgztPB3OIkiEbhY+twjQ7Ys78y94YKnlgo/fmplgdA4=; b=My0k2XoWi/HsOAhDW4lgMLOmkVaJys4BsKSQYs0hKM162lP6zCSq4D4lzd6pVDprQr FI8ZEuD8ckcsQ2C1LzkUlOhqeVZDXpR37K9IGdnrl35YiRnA259c3djF1+DULl6Fzt8p bRsJjc2ojSZvnDzxHOdomaBTaUtCNq7Z6zk0ezwBiECLw56DcpZtjhLAqELmcbMiyOaN d1FXZflZV2C5ozg/vvjasrNyPO8VqIUnaWfBpzSZcdWeWhkTbsane4JSbnXlY6Jquk3K Uy2gpxozVRb7pO9Bn+/obHYlz+KIe7B5OLGADD8exdUWN8gZ6CqUbIc5U3k/RM2pJepk LLcQ== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of yu-cheng.yu@intel.com designates 192.55.52.43 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: APt69E1H2nTzPc0VpklZpX6tj6e6tkm0L54HKTe0ga7AhdHXds1z6cwk DcE9Q0iy7IixceUK4hFwh4TguUBfZTUb0fHEm6rpQVdJTbT5DxGsiCe6o/wwHLGjujJqJ6FFDAM eV3q0FriLqX1Ub0b0PBtc4EttRg8kMM1JiuO7TnbmiHwgEJ7U0+GI+UFLGv21rAVFOg== X-Received: by 2002:a17:902:650a:: with SMTP id b10-v6mr2385918plk.45.1528382494303; Thu, 07 Jun 2018 07:41:34 -0700 (PDT) X-Google-Smtp-Source: ADUXVKJV1vYL2ztCbUU2q8KWCbUZlx98YC8GnQbjfdudS3EY3lRLa/HMCRLbQnbmPZB0PPq66gaj X-Received: by 2002:a17:902:650a:: with SMTP id b10-v6mr2385856plk.45.1528382493216; Thu, 07 Jun 2018 07:41:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1528382493; cv=none; d=google.com; s=arc-20160816; b=o5csGrt2FhbxoNy7zqjSttreoNeoLaPaLzPvS3vDSr2BGZQhGrIKqqTnydm0cHqGbK HYZZ8Hboi2NjKxrolceFtE6di9B0gYLoSnTlvgYb6avajmSquwjhijUu8c9eviYRfIyl Csyl2YSSQfw4SkiZU8/tKZtsDgiXKcsmYg80DAqhC2KB2U5NUK8tloDzavH8t+1HS7+2 hl0/t33UMPWFotUihaFbw/ofYWMJQJ7bj7g5Mvn8WZgwhATyPmqsLabB3LFIR3wOiVki M6pwnTVio5TwCkW82Q0xMITx009wOBbqPiTn1q6WziRWmynENAKzhe1GU2WOwrp+AgI4 lAvw== 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 :arc-authentication-results; bh=SgztPB3OIkiEbhY+twjQ7Ys78y94YKnlgo/fmplgdA4=; b=sxCk8CLk71K9rZ8HMFmZm7Fm7M03macj/kpl3u1EvR4E3Xu3njwZGhILuk/6nADUE9 p9RHw9NJ6GkanqPFGpRRFioUTmSOGsh8YRlQ+N/0BIzq1NCarcnqwzFy1+WbCv20P42F xSCHlgcs2uNhzBXhUU4ihBTRUxvjy6eanSAN/LiLuzXW7jnEM/KEXUhFVsqmsWpU/MpI xXnH2f11utFXwsnbjkBX00RFFrUGEZWk2MJk+rfGrK7jKCUBFS2BtVwLm7NN6J9ZqHMM QX9JlMntHHXRFLWqccVvHAMbu8LH4iEBBCg5HwOH2+kXrjDGAJ7iU3rpbgj+H3vf/vIf 58qQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of yu-cheng.yu@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=yu-cheng.yu@intel.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from mga05.intel.com (mga05.intel.com. [192.55.52.43]) by mx.google.com with ESMTPS id i74-v6si8716254pgc.188.2018.06.07.07.41.32 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 07 Jun 2018 07:41:33 -0700 (PDT) Received-SPF: pass (google.com: domain of yu-cheng.yu@intel.com designates 192.55.52.43 as permitted sender) client-ip=192.55.52.43; Authentication-Results: mx.google.com; spf=pass (google.com: domain of yu-cheng.yu@intel.com designates 192.55.52.43 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 orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga105.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 07 Jun 2018 07:41:32 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.49,486,1520924400"; d="scan'208";a="62632639" Received: from 2b52.sc.intel.com ([143.183.136.51]) by orsmga001.jf.intel.com with ESMTP; 07 Jun 2018 07:41:32 -0700 From: Yu-cheng Yu To: linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, x86@kernel.org, "H. Peter Anvin" , Thomas Gleixner , Ingo Molnar , "H.J. Lu" , Vedvyas Shanbhogue , "Ravi V. Shankar" , Dave Hansen , Andy Lutomirski , Jonathan Corbet , Oleg Nesterov , Arnd Bergmann , Mike Kravetz Cc: Yu-cheng Yu Subject: [PATCH 06/10] x86/cet: Add arch_prctl functions for shadow stack Date: Thu, 7 Jun 2018 07:38:03 -0700 Message-Id: <20180607143807.3611-7-yu-cheng.yu@intel.com> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20180607143807.3611-1-yu-cheng.yu@intel.com> References: <20180607143807.3611-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 following operations are provided. ARCH_CET_STATUS: return the current CET status ARCH_CET_DISABLE: disable CET features ARCH_CET_LOCK: lock out CET features ARCH_CET_EXEC: set CET features for exec() ARCH_CET_ALLOC_SHSTK: allocate a new shadow stack ARCH_CET_PUSH_SHSTK: put a return address on shadow stack ARCH_CET_ALLOC_SHSTK and ARCH_CET_PUSH_SHSTK are intended only for the implementation of GLIBC ucontext related APIs. Signed-off-by: H.J. Lu Signed-off-by: Yu-cheng Yu --- arch/x86/include/asm/cet.h | 7 ++ arch/x86/include/uapi/asm/prctl.h | 15 +++ arch/x86/kernel/Makefile | 2 +- arch/x86/kernel/cet.c | 18 +++- arch/x86/kernel/cet_prctl.c | 203 ++++++++++++++++++++++++++++++++++++++ arch/x86/kernel/elf.c | 24 ++++- arch/x86/kernel/process.c | 7 ++ 7 files changed, 270 insertions(+), 6 deletions(-) create mode 100644 arch/x86/kernel/cet_prctl.c diff --git a/arch/x86/include/asm/cet.h b/arch/x86/include/asm/cet.h index c8fd87e13859..a2a53fe4d5e6 100644 --- a/arch/x86/include/asm/cet.h +++ b/arch/x86/include/asm/cet.h @@ -12,24 +12,31 @@ struct task_struct; struct cet_stat { unsigned long shstk_base; unsigned long shstk_size; + unsigned long exec_shstk_size; unsigned int shstk_enabled:1; + unsigned int locked:1; + unsigned int exec_shstk:2; }; #ifdef CONFIG_X86_INTEL_CET +int prctl_cet(int option, unsigned long arg2); unsigned long cet_get_shstk_ptr(void); int cet_push_shstk(int ia32, unsigned long ssp, unsigned long val); int cet_setup_shstk(void); int cet_setup_thread_shstk(struct task_struct *p); +int cet_alloc_shstk(unsigned long *arg); 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(int ia32, unsigned long addr); #else +static inline int prctl_cet(int option, unsigned long arg2) { return 0; } static inline unsigned long cet_get_shstk_ptr(void) { return 0; } static inline int cet_push_shstk(int ia32, unsigned long ssp, unsigned long val) { return 0; } static inline int cet_setup_shstk(void) { return 0; } static inline int cet_setup_thread_shstk(struct task_struct *p) { return 0; } +static inline int cet_alloc_shstk(unsigned long *arg) { 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 0; } diff --git a/arch/x86/include/uapi/asm/prctl.h b/arch/x86/include/uapi/asm/prctl.h index 5a6aac9fa41f..f9965403b655 100644 --- a/arch/x86/include/uapi/asm/prctl.h +++ b/arch/x86/include/uapi/asm/prctl.h @@ -14,4 +14,19 @@ #define ARCH_MAP_VDSO_32 0x2002 #define ARCH_MAP_VDSO_64 0x2003 +#define ARCH_CET_STATUS 0x3001 +#define ARCH_CET_DISABLE 0x3002 +#define ARCH_CET_LOCK 0x3003 +#define ARCH_CET_EXEC 0x3004 +#define ARCH_CET_ALLOC_SHSTK 0x3005 +#define ARCH_CET_PUSH_SHSTK 0x3006 + +/* + * Settings for ARCH_CET_EXEC + */ +#define CET_EXEC_ELF_PROPERTY 0 +#define CET_EXEC_ALWAYS_OFF 1 +#define CET_EXEC_ALWAYS_ON 2 +#define CET_EXEC_MAX CET_EXEC_ALWAYS_ON + #endif /* _ASM_X86_PRCTL_H */ diff --git a/arch/x86/kernel/Makefile b/arch/x86/kernel/Makefile index cbf983f44b61..80464f925a6a 100644 --- a/arch/x86/kernel/Makefile +++ b/arch/x86/kernel/Makefile @@ -138,7 +138,7 @@ obj-$(CONFIG_UNWINDER_ORC) += unwind_orc.o obj-$(CONFIG_UNWINDER_FRAME_POINTER) += unwind_frame.o obj-$(CONFIG_UNWINDER_GUESS) += unwind_guess.o -obj-$(CONFIG_X86_INTEL_CET) += cet.o +obj-$(CONFIG_X86_INTEL_CET) += cet.o cet_prctl.o obj-$(CONFIG_ARCH_HAS_PROGRAM_PROPERTIES) += elf.o diff --git a/arch/x86/kernel/cet.c b/arch/x86/kernel/cet.c index 156f5d88ffd5..1b7089dcf1ea 100644 --- a/arch/x86/kernel/cet.c +++ b/arch/x86/kernel/cet.c @@ -83,6 +83,19 @@ static unsigned long shstk_mmap(unsigned long addr, unsigned long len) return addr; } +int cet_alloc_shstk(unsigned long *arg) +{ + unsigned long size = *arg; + unsigned long addr; + + addr = shstk_mmap(0, size); + if (addr >= TASK_SIZE) + return -ENOMEM; + + *arg = addr; + return 0; +} + int cet_setup_shstk(void) { unsigned long addr, size; @@ -90,7 +103,10 @@ int cet_setup_shstk(void) if (!cpu_feature_enabled(X86_FEATURE_SHSTK)) return -EOPNOTSUPP; - size = SHSTK_SIZE; + size = current->thread.cet.exec_shstk_size; + if ((size > TASK_SIZE) || (size == 0)) + size = SHSTK_SIZE; + addr = shstk_mmap(0, size); if (addr >= TASK_SIZE) diff --git a/arch/x86/kernel/cet_prctl.c b/arch/x86/kernel/cet_prctl.c new file mode 100644 index 000000000000..326996e2ea80 --- /dev/null +++ b/arch/x86/kernel/cet_prctl.c @@ -0,0 +1,203 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* + * Handler of prctl for CET: + * + * ARCH_CET_STATUS: return the current status + * ARCH_CET_DISABLE: disable features + * ARCH_CET_LOCK: lock out cet features until exec() + * ARCH_CET_EXEC: set default features for exec() + * ARCH_CET_ALLOC_SHSTK: allocate shadow stack + * ARCH_CET_PUSH_SHSTK: put a return address on shadow stack + */ + +static int handle_get_status(unsigned long arg2) +{ + unsigned int features = 0, cet_exec = 0; + unsigned long shstk_size = 0; + + if (current->thread.cet.shstk_enabled) + features |= GNU_PROPERTY_X86_FEATURE_1_SHSTK; + if (current->thread.cet.exec_shstk == CET_EXEC_ALWAYS_ON) + cet_exec |= GNU_PROPERTY_X86_FEATURE_1_SHSTK; + shstk_size = current->thread.cet.exec_shstk_size; + + if (in_compat_syscall()) { + unsigned int buf[3]; + + buf[0] = features; + buf[1] = cet_exec; + buf[2] = (unsigned int)shstk_size; + return copy_to_user((unsigned int __user *)arg2, buf, + sizeof(buf)); + } else { + unsigned long buf[3]; + + buf[0] = (unsigned long)features; + buf[1] = (unsigned long)cet_exec; + buf[2] = shstk_size; + return copy_to_user((unsigned long __user *)arg2, buf, + sizeof(buf)); + } +} + +static int handle_set_exec(unsigned long arg2) +{ + unsigned int features = 0, cet_exec = 0; + unsigned long shstk_size = 0; + int err = 0; + + if (in_compat_syscall()) { + unsigned int buf[3]; + + err = copy_from_user(buf, (unsigned int __user *)arg2, + sizeof(buf)); + if (!err) { + features = buf[0]; + cet_exec = buf[1]; + shstk_size = (unsigned long)buf[2]; + } + } else { + unsigned long buf[3]; + + err = copy_from_user(buf, (unsigned long __user *)arg2, + sizeof(buf)); + if (!err) { + features = (unsigned int)buf[0]; + cet_exec = (unsigned int)buf[1]; + shstk_size = buf[2]; + } + } + + if (err) + return -EFAULT; + if (cet_exec > CET_EXEC_MAX) + return -EINVAL; + if (shstk_size >= TASK_SIZE) + return -EINVAL; + + if (features & GNU_PROPERTY_X86_FEATURE_1_SHSTK) { + if (!cpu_feature_enabled(X86_FEATURE_SHSTK)) + return -EINVAL; + if ((current->thread.cet.exec_shstk == CET_EXEC_ALWAYS_ON) && + (cet_exec != CET_EXEC_ALWAYS_ON)) + return -EPERM; + } + + if (features & GNU_PROPERTY_X86_FEATURE_1_SHSTK) + current->thread.cet.exec_shstk = cet_exec; + + current->thread.cet.exec_shstk_size = shstk_size; + return 0; +} + +static int handle_push_shstk(unsigned long arg2) +{ + unsigned long ssp = 0, ret_addr = 0; + int ia32, err; + + ia32 = in_ia32_syscall(); + + if (ia32) { + unsigned int buf[2]; + + err = copy_from_user(buf, (unsigned int __user *)arg2, + sizeof(buf)); + if (!err) { + ssp = (unsigned long)buf[0]; + ret_addr = (unsigned long)buf[1]; + } + } else { + unsigned long buf[2]; + + err = copy_from_user(buf, (unsigned long __user *)arg2, + sizeof(buf)); + if (!err) { + ssp = buf[0]; + ret_addr = buf[1]; + } + } + if (err) + return -EFAULT; + err = cet_push_shstk(ia32, ssp, ret_addr); + if (err) + return -err; + return 0; +} + +static int handle_alloc_shstk(unsigned long arg2) +{ + int err = 0; + unsigned long shstk_size = 0; + + if (in_ia32_syscall()) { + unsigned int size; + + err = get_user(size, (unsigned int __user *)arg2); + if (!err) + shstk_size = size; + } else { + err = get_user(shstk_size, (unsigned long __user *)arg2); + } + + if (err) + return -EFAULT; + + err = cet_alloc_shstk(&shstk_size); + if (err) + return -err; + + if (in_ia32_syscall()) { + if (put_user(shstk_size, (unsigned int __user *)arg2)) + return -EFAULT; + } else { + if (put_user(shstk_size, (unsigned long __user *)arg2)) + return -EFAULT; + } + return 0; +} + +int prctl_cet(int option, unsigned long arg2) +{ + if (!cpu_feature_enabled(X86_FEATURE_SHSTK)) + return -EINVAL; + + switch (option) { + case ARCH_CET_STATUS: + return handle_get_status(arg2); + + case ARCH_CET_DISABLE: + if (current->thread.cet.locked) + return -EPERM; + if (arg2 & GNU_PROPERTY_X86_FEATURE_1_SHSTK) + cet_disable_free_shstk(current); + + return 0; + + case ARCH_CET_LOCK: + current->thread.cet.locked = 1; + return 0; + + case ARCH_CET_EXEC: + return handle_set_exec(arg2); + + case ARCH_CET_ALLOC_SHSTK: + return handle_alloc_shstk(arg2); + + case ARCH_CET_PUSH_SHSTK: + return handle_push_shstk(arg2); + + default: + return -EINVAL; + } +} diff --git a/arch/x86/kernel/elf.c b/arch/x86/kernel/elf.c index 8e2719d8dc86..de08d41971f6 100644 --- a/arch/x86/kernel/elf.c +++ b/arch/x86/kernel/elf.c @@ -8,7 +8,10 @@ #include #include +#include +#include #include +#include #include #include #include @@ -208,13 +211,26 @@ int arch_setup_features(void *ehdr_p, void *phdr_p, current->thread.cet.shstk_enabled = 0; current->thread.cet.shstk_base = 0; current->thread.cet.shstk_size = 0; + current->thread.cet.locked = 0; if (cpu_feature_enabled(X86_FEATURE_SHSTK)) { - if (shstk) { - err = cet_setup_shstk(); - if (err < 0) - goto out; + int exec = current->thread.cet.exec_shstk; + + if (exec != CET_EXEC_ALWAYS_OFF) { + if (shstk || (exec == CET_EXEC_ALWAYS_ON)) { + err = cet_setup_shstk(); + if (err < 0) + goto out; + } } } + + /* + * Lockout CET features if no interpreter + */ + if (!interp) + current->thread.cet.locked = 1; + + err = 0; out: return err; } diff --git a/arch/x86/kernel/process.c b/arch/x86/kernel/process.c index ae56caee41f9..54ad1863c6d2 100644 --- a/arch/x86/kernel/process.c +++ b/arch/x86/kernel/process.c @@ -794,6 +794,13 @@ long do_arch_prctl_common(struct task_struct *task, int option, return get_cpuid_mode(); case ARCH_SET_CPUID: return set_cpuid_mode(task, cpuid_enabled); + case ARCH_CET_STATUS: + case ARCH_CET_DISABLE: + case ARCH_CET_LOCK: + case ARCH_CET_EXEC: + case ARCH_CET_ALLOC_SHSTK: + case ARCH_CET_PUSH_SHSTK: + return prctl_cet(option, cpuid_enabled); } return -EINVAL;