From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D8C89C433E2 for ; Thu, 21 May 2020 22:42:12 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 8D37D20826 for ; Thu, 21 May 2020 22:42:12 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="n8XMg3aD" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 8D37D20826 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=chromium.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id E7A1C80008; Thu, 21 May 2020 18:42:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E055080007; Thu, 21 May 2020 18:42:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CA31C80008; Thu, 21 May 2020 18:42:11 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0125.hostedemail.com [216.40.44.125]) by kanga.kvack.org (Postfix) with ESMTP id AC1B780007 for ; Thu, 21 May 2020 18:42:11 -0400 (EDT) Received: from smtpin02.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 7485C2498 for ; Thu, 21 May 2020 22:42:11 +0000 (UTC) X-FDA: 76842200862.02.vest58_877c0c709a760 X-HE-Tag: vest58_877c0c709a760 X-Filterd-Recvd-Size: 12462 Received: from mail-pg1-f196.google.com (mail-pg1-f196.google.com [209.85.215.196]) by imf02.hostedemail.com (Postfix) with ESMTP for ; Thu, 21 May 2020 22:42:10 +0000 (UTC) Received: by mail-pg1-f196.google.com with SMTP id t11so4023737pgg.2 for ; Thu, 21 May 2020 15:42:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=jw/xqa3g89q51hDnYa5jOhsO4JnUA2E12TXYJqQM6yo=; b=n8XMg3aD467ovZaJ8YnSYN6LVan287tt4sBIOgRs82WugtI/HdxyJhYnlWCKPHqCgO ill8z/e4VVxGXzzKkIW5NVLJ/1f4x5cJ61kn/qqwkHTnEqz/Vz/IZRFU0R8q/IEQ2Qf7 lId3+6YrFd3uIlEkXBncI8CDs9k/oH/CLKIsc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=jw/xqa3g89q51hDnYa5jOhsO4JnUA2E12TXYJqQM6yo=; b=sN68nB3RUTRRLeBz3svMrOb3/lFLg8tBEVrjYmeq2rBGItqLBDaMpMkK9MpOYwVtR4 z+D0MicrOLGscevQtuGUzmVpPitv7r6uGmVtZGJEliOec3nRf4W0Rtk7wpj4BWlQlEZL 00T9xOs78//QWGAxNOBBaprLUfK7cBPlEDeFVnNdb8BM4BW5UuDgftv12ibdGKgTDouF xFBvv6zLBYhjo2iYniX6Fx5yKCDOjo5YRDI236VQSalyK4N/DCtqgEOAEZT/Qf5YiFxF uz0VVJZTZOgW/Qc5Ze5OgI8liMVaxQwwOeiWCW8Kl5VwZZGLP/eiEAzMBVR1a5gsVNgS Tugg== X-Gm-Message-State: AOAM531fLpEyJsUB5Cq95MC07S/O/HKzw6SZyy7KqGIAZBOhIu0k14zj 5D9R1OdcaETuh+gNM6fhDARn3A== X-Google-Smtp-Source: ABdhPJwXOoPao/Ck/VlSSNAkUg8UNcEFO1e6LSxmA5hGfbn/TjlhNOkzmuwUAXLHuks+nAYhj1nmlg== X-Received: by 2002:a65:4908:: with SMTP id p8mr10684191pgs.214.1590100929772; Thu, 21 May 2020 15:42:09 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id n10sm5395236pfd.192.2020.05.21.15.42.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 May 2020 15:42:08 -0700 (PDT) Date: Thu, 21 May 2020 15:42:07 -0700 From: Kees Cook To: Yu-cheng Yu Cc: 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 , Mike Kravetz , Nadav Amit , Oleg Nesterov , Pavel Machek , Peter Zijlstra , Randy Dunlap , "Ravi V. Shankar" , Vedvyas Shanbhogue , Dave Martin , Weijiang Yang Subject: Re: [PATCH v10 26/26] x86/cet/shstk: Add arch_prctl functions for shadow stack Message-ID: <202005211528.A12B4AD@keescook> References: <20200429220732.31602-1-yu-cheng.yu@intel.com> <20200429220732.31602-27-yu-cheng.yu@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200429220732.31602-27-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: On Wed, Apr 29, 2020 at 03:07:32PM -0700, Yu-cheng Yu wrote: > arch_prctl(ARCH_X86_CET_STATUS, u64 *args) > Get CET feature status. > > The parameter 'args' is a pointer to a user buffer. The kernel returns > the following information: > > *args = shadow stack/IBT status > *(args + 1) = shadow stack base address > *(args + 2) = shadow stack size > > arch_prctl(ARCH_X86_CET_DISABLE, u64 features) > Disable CET features specified in 'features'. Return -EPERM if CET is > locked. > > arch_prctl(ARCH_X86_CET_LOCK) > Lock in CET features. > > arch_prctl(ARCH_X86_CET_ALLOC_SHSTK, u64 *args) > Allocate a new shadow stack. > > The parameter 'args' is a pointer to a user buffer containing the > desired size to allocate. The kernel returns the allocated shadow > stack address in *args. Hi! Just a quick note about getting these designs right -- prctl() (and similar APIs) needs to make sure they're examining all "unknown" flags as zero, or we run the risk of breaking sloppy userspace callers who accidentally set flags and then later the kernel gives meaning to those flags. Notes below... > > Signed-off-by: Yu-cheng Yu > --- > v10: > - Verify CET is enabled before handling arch_prctl. > - Change input parameters from unsigned long to u64, to make it clear they > are 64-bit. > > arch/x86/include/asm/cet.h | 4 ++ > arch/x86/include/uapi/asm/prctl.h | 5 ++ > arch/x86/kernel/Makefile | 2 +- > arch/x86/kernel/cet.c | 29 +++++++++++ > arch/x86/kernel/cet_prctl.c | 87 +++++++++++++++++++++++++++++++ > arch/x86/kernel/process.c | 4 +- > 6 files changed, 128 insertions(+), 3 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 71dc92acd2f2..99e6e741d28c 100644 > --- a/arch/x86/include/asm/cet.h > +++ b/arch/x86/include/asm/cet.h > @@ -14,16 +14,20 @@ struct sc_ext; > struct cet_status { > unsigned long shstk_base; > unsigned long shstk_size; > + unsigned int locked:1; > }; > > #ifdef CONFIG_X86_INTEL_CET > +int prctl_cet(int option, u64 arg2); > int cet_setup_shstk(void); > int cet_setup_thread_shstk(struct task_struct *p); > +int cet_alloc_shstk(unsigned long *arg); > void cet_disable_free_shstk(struct task_struct *p); > int cet_verify_rstor_token(bool ia32, unsigned long ssp, unsigned long *new_ssp); > void cet_restore_signal(struct sc_ext *sc); > int cet_setup_signal(bool ia32, unsigned long rstor, struct sc_ext *sc); > #else > +static inline int prctl_cet(int option, u64 arg2) { return -EINVAL; } > static inline int cet_setup_thread_shstk(struct task_struct *p) { return 0; } > static inline void cet_disable_free_shstk(struct task_struct *p) {} > static inline void cet_restore_signal(struct sc_ext *sc) { return; } > diff --git a/arch/x86/include/uapi/asm/prctl.h b/arch/x86/include/uapi/asm/prctl.h > index 5a6aac9fa41f..d962f0ec9ccf 100644 > --- a/arch/x86/include/uapi/asm/prctl.h > +++ b/arch/x86/include/uapi/asm/prctl.h > @@ -14,4 +14,9 @@ > #define ARCH_MAP_VDSO_32 0x2002 > #define ARCH_MAP_VDSO_64 0x2003 > > +#define ARCH_X86_CET_STATUS 0x3001 > +#define ARCH_X86_CET_DISABLE 0x3002 > +#define ARCH_X86_CET_LOCK 0x3003 > +#define ARCH_X86_CET_ALLOC_SHSTK 0x3004 > + > #endif /* _ASM_X86_PRCTL_H */ > diff --git a/arch/x86/kernel/Makefile b/arch/x86/kernel/Makefile > index e9cc2551573b..0b621e2afbdc 100644 > --- a/arch/x86/kernel/Makefile > +++ b/arch/x86/kernel/Makefile > @@ -144,7 +144,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 > > ### > # 64 bit specific files > diff --git a/arch/x86/kernel/cet.c b/arch/x86/kernel/cet.c > index 121552047b86..c1b9b540c03e 100644 > --- a/arch/x86/kernel/cet.c > +++ b/arch/x86/kernel/cet.c > @@ -145,6 +145,35 @@ static int create_rstor_token(bool ia32, unsigned long ssp, > return 0; > } > > +int cet_alloc_shstk(unsigned long *arg) > +{ > + unsigned long len = *arg; > + unsigned long addr; > + unsigned long token; > + unsigned long ssp; > + > + addr = alloc_shstk(round_up(len, PAGE_SIZE)); > + > + if (IS_ERR((void *)addr)) > + return PTR_ERR((void *)addr); > + > + /* Restore token is 8 bytes and aligned to 8 bytes */ > + ssp = addr + len; > + token = ssp; > + > + if (!in_ia32_syscall()) > + token |= TOKEN_MODE_64; > + ssp -= 8; > + > + if (write_user_shstk_64(ssp, token)) { > + vm_munmap(addr, len); > + return -EINVAL; > + } > + > + *arg = addr; > + return 0; > +} > + > int cet_setup_shstk(void) > { > unsigned long addr, size; > diff --git a/arch/x86/kernel/cet_prctl.c b/arch/x86/kernel/cet_prctl.c > new file mode 100644 > index 000000000000..0139c48f2215 > --- /dev/null > +++ b/arch/x86/kernel/cet_prctl.c > @@ -0,0 +1,87 @@ > +/* SPDX-License-Identifier: GPL-2.0 */ > + > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > + > +/* See Documentation/x86/intel_cet.rst. */ > + > +static int handle_get_status(u64 arg2) > +{ > + struct cet_status *cet = ¤t->thread.cet; > + u64 buf[3] = {0, 0, 0}; > + > + if (cet->shstk_size) { > + buf[0] |= GNU_PROPERTY_X86_FEATURE_1_SHSTK; > + buf[1] = (u64)cet->shstk_base; > + buf[2] = (u64)cet->shstk_size; > + } > + > + return copy_to_user((u64 __user *)arg2, buf, sizeof(buf)); > +} > + > +static int handle_alloc_shstk(u64 arg2) > +{ > + int err = 0; > + unsigned long arg; > + unsigned long addr = 0; > + unsigned long size = 0; > + > + if (get_user(arg, (unsigned long __user *)arg2)) > + return -EFAULT; > + > + size = arg; > + err = cet_alloc_shstk(&arg); > + if (err) > + return err; > + > + addr = arg; > + if (put_user((u64)addr, (u64 __user *)arg2)) { > + vm_munmap(addr, size); > + return -EFAULT; > + } > + > + return 0; > +} > + > +int prctl_cet(int option, u64 arg2) > +{ > + struct cet_status *cet; > + > + if (!IS_ENABLED(CONFIG_X86_INTEL_CET)) > + return -EINVAL; Using -EINVAL here means userspace can't tell the difference between an old kernel and a kernel not built with CONFIG_X86_INTEL_CET. Perhaps -ENOTSUPP? > + > + if (option == ARCH_X86_CET_STATUS) > + return handle_get_status(arg2); > + > + if (!static_cpu_has(X86_FEATURE_SHSTK)) > + return -EINVAL; Similar case: though this is now a kernel that knows how, but a CPU that doesn't. > + > + cet = ¤t->thread.cet; You get this both here and in handle_get_status(). Why not just get it once and pass it into handle_get_status()? (And perhaps rename it to "copy_status_to_user" or so? > + > + switch (option) { > + case ARCH_X86_CET_DISABLE: This must check for unknown flags before doing anything else: if (arg & ~(GNU_PROPERTY_X86_FEATURE_1_SHSTK)) return -EINVAL; > + if (cet->locked) > + return -EPERM; > + if (arg2 & GNU_PROPERTY_X86_FEATURE_1_SHSTK) > + cet_disable_free_shstk(current); > + > + return 0; > + > + case ARCH_X86_CET_LOCK: Same here. > + cet->locked = 1; > + return 0; > + > + case ARCH_X86_CET_ALLOC_SHSTK: > + return handle_alloc_shstk(arg2); > + > + default: > + return -EINVAL; > + } > +} > diff --git a/arch/x86/kernel/process.c b/arch/x86/kernel/process.c > index ef1c2b8086a2..de6773dd6a16 100644 > --- a/arch/x86/kernel/process.c > +++ b/arch/x86/kernel/process.c > @@ -996,7 +996,7 @@ 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); > + default: > + return prctl_cet(option, cpuid_enabled); > } This is weird, but yeah, there's only the cpuid and cet handlers... I think do_arch_prctrl_common() should call the second arg "arg2" and there should be a series of calls: ret = prctl_cpuid(task, option, args); if (ret != -EINVAL) return ret; return prctl_cet(option, args); But that's just a style nit, I guess. And really, why is x86's arch prtcl return EINVAL for unknown options? It should use -ENOSYS for unknown options and -EINVAL for bad arguments, but I guess it's too late for that. :) -- Kees Cook