From patchwork Wed Jul 19 22:47:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: tip-bot2 for Thomas Gleixner X-Patchwork-Id: 122913 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c923:0:b0:3e4:2afc:c1 with SMTP id j3csp2771490vqt; Wed, 19 Jul 2023 16:27:53 -0700 (PDT) X-Google-Smtp-Source: APBJJlEOM9eP0szbvR2MysZ1zknmJ7yH0ClRjp5f8ey7hAdyvTf+Urbu289Kib3YZsfNZrsMj3R5 X-Received: by 2002:a17:90a:d510:b0:262:e697:f367 with SMTP id t16-20020a17090ad51000b00262e697f367mr4860915pju.15.1689809273216; Wed, 19 Jul 2023 16:27:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689809273; cv=none; d=google.com; s=arc-20160816; b=Iu9oOk5JmkiepmaTrmx/BNx0M8Jkq6lqPThT9smwYjQfbInEL77sZt/hP16YgYXBs9 PluQTzp7q22kGKk8gU+K2RwhBWAsVLiRwz5UTJTrS+0S+buF7ftJAMVIea137ZR+c9n8 UDn8cfo3L6W/hJJfnOycxCNP75SjMI7FIjbGziiBJdI1udIf4Yn0/iRaJmCEPIpmHY9D KHxae5UTOPgRm8iOJ+EgdDzcm7hFFoJOHubL2BMvf3VQHbe7gEbgkUdFZe6stqD4Er4o FBuGFRs6grIz37gJS15poLtDbnsDOdqIUQtBWCW49fK1uqVfmFZPowAJHX6WC1w7dlHL PVOA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:robot-unsubscribe :robot-id:message-id:mime-version:cc:subject:to:reply-to:sender:from :dkim-signature:dkim-signature:date; bh=nsK8SHPWbkeh5fMmHug6tJThMBPtM/s33Dap2BChXo4=; fh=Gre39lXhlvd0HqZD6zlwoEbeK/+QSH4w+yvCIh7xECs=; b=Ya5LGFJkTGuBwSW9B2GUXQkYUmZsBkPCTTP1QGZXKOfqh/MWCd+S6wEgc6aWph8vsq LDWe9dHPKfEX6GQZ763J3OXMrg3w3Ug7m25bs8LTpzMyqsZ3LsAIeyKo4bO8/e2Wm1rL Wd7nj1yk7xRJrjbJDWaDAHaIiXZr7x1+oiZilTBbjShQGBFPz0bwQraRaiffsl2z2C6W tBaPf9whD5/W3K33JHbbZkBku6g6+AxdrZRftvKhgz9RT8kp8jV3bXqf6fn7pXZEBZJD G/6FzbjuzFWxuhw4DDRDjrlUwiXKPMsFiZ/TeklNYS2gsvBGG6sr+60rUXyuD7j6dOqk U7Pw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=tIr3gyve; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id gx4-20020a17090b124400b0026349debbdfsi2285967pjb.136.2023.07.19.16.27.40; Wed, 19 Jul 2023 16:27:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=tIr3gyve; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231289AbjGSWsc (ORCPT + 99 others); Wed, 19 Jul 2023 18:48:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42624 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230451AbjGSWro (ORCPT ); Wed, 19 Jul 2023 18:47:44 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 43F752109; Wed, 19 Jul 2023 15:47:35 -0700 (PDT) Date: Wed, 19 Jul 2023 22:47:32 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1689806853; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=nsK8SHPWbkeh5fMmHug6tJThMBPtM/s33Dap2BChXo4=; b=tIr3gyve9jVQAf8Zjfr5vZo0m1M7nxVyBbq6n8LtDyonWjA9PVXwDwfRukQAdDnT4yPPXG ucDU/ReLRNjLAenZFbuqfhD30Wa48wbU0DTeXRmhMPtrx3eqKFu+868IEsWKL8SwK1kBum H+Rwy/5DLfbW6CTysnmR+YVr8vEm2HJYiVmhEeh7+OwPUjotpB5XC46AzCYkp4Xxd9Lz76 s7gqMd07JVGAB7jqId21YfSi0aTk2ZvhvwATMqW3j9A4fsu75SEKoU4sMCVXK6+w6YnYQJ y3BlZt+SqVuCz5oklkKZaBbWVWZ29+m/o0/NMxta+zv8e99CR6iB/fvBQQx6oA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1689806853; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=nsK8SHPWbkeh5fMmHug6tJThMBPtM/s33Dap2BChXo4=; b=yJy/Qfd7BtnzeZ6R9Q8oSOgngrrY9lmi1RYckiFDDtZT5h3vkgJDDqcv+rC6YAMu0urWyO QGMSfIm9rkMAwfDA== From: "tip-bot2 for Rick Edgecombe" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/shstk] x86: Introduce userspace API for shadow stack Cc: "Kirill A. Shutemov" , Rick Edgecombe , Dave Hansen , "Borislav Petkov (AMD)" , Kees Cook , "Mike Rapoport (IBM)" , Pengfei Xu , John Allen , x86@kernel.org, linux-kernel@vger.kernel.org MIME-Version: 1.0 Message-ID: <168980685283.28540.15524021984284517206.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1771893448168228298 X-GMAIL-MSGID: 1771893448168228298 The following commit has been merged into the x86/shstk branch of tip: Commit-ID: fac85c2cdd5b2c1af04159067f21c7532f0dce36 Gitweb: https://git.kernel.org/tip/fac85c2cdd5b2c1af04159067f21c7532f0dce36 Author: Rick Edgecombe AuthorDate: Mon, 12 Jun 2023 17:10:52 -07:00 Committer: Rick Edgecombe CommitterDate: Tue, 11 Jul 2023 14:12:50 -07:00 x86: Introduce userspace API for shadow stack Add three new arch_prctl() handles: - ARCH_SHSTK_ENABLE/DISABLE enables or disables the specified feature. Returns 0 on success or a negative value on error. - ARCH_SHSTK_LOCK prevents future disabling or enabling of the specified feature. Returns 0 on success or a negative value on error. The features are handled per-thread and inherited over fork(2)/clone(2), but reset on exec(). Co-developed-by: Kirill A. Shutemov Signed-off-by: Kirill A. Shutemov Signed-off-by: Rick Edgecombe Signed-off-by: Dave Hansen Reviewed-by: Borislav Petkov (AMD) Reviewed-by: Kees Cook Acked-by: Mike Rapoport (IBM) Tested-by: Pengfei Xu Tested-by: John Allen Tested-by: Kees Cook Link: https://lore.kernel.org/all/20230613001108.3040476-27-rick.p.edgecombe%40intel.com --- arch/x86/include/asm/processor.h | 6 ++++- arch/x86/include/asm/shstk.h | 21 ++++++++++++++- arch/x86/include/uapi/asm/prctl.h | 6 ++++- arch/x86/kernel/Makefile | 2 +- arch/x86/kernel/process_64.c | 6 ++++- arch/x86/kernel/shstk.c | 44 ++++++++++++++++++++++++++++++- 6 files changed, 85 insertions(+) create mode 100644 arch/x86/include/asm/shstk.h create mode 100644 arch/x86/kernel/shstk.c diff --git a/arch/x86/include/asm/processor.h b/arch/x86/include/asm/processor.h index d46300e..4e35f40 100644 --- a/arch/x86/include/asm/processor.h +++ b/arch/x86/include/asm/processor.h @@ -28,6 +28,7 @@ struct vm86; #include #include #include +#include #include #include @@ -475,6 +476,11 @@ struct thread_struct { */ u32 pkru; +#ifdef CONFIG_X86_USER_SHADOW_STACK + unsigned long features; + unsigned long features_locked; +#endif + /* Floating point and extended processor state */ struct fpu fpu; /* diff --git a/arch/x86/include/asm/shstk.h b/arch/x86/include/asm/shstk.h new file mode 100644 index 0000000..ec75380 --- /dev/null +++ b/arch/x86/include/asm/shstk.h @@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_X86_SHSTK_H +#define _ASM_X86_SHSTK_H + +#ifndef __ASSEMBLY__ +#include + +struct task_struct; + +#ifdef CONFIG_X86_USER_SHADOW_STACK +long shstk_prctl(struct task_struct *task, int option, unsigned long features); +void reset_thread_features(void); +#else +static inline long shstk_prctl(struct task_struct *task, int option, + unsigned long arg2) { return -EINVAL; } +static inline void reset_thread_features(void) {} +#endif /* CONFIG_X86_USER_SHADOW_STACK */ + +#endif /* __ASSEMBLY__ */ + +#endif /* _ASM_X86_SHSTK_H */ diff --git a/arch/x86/include/uapi/asm/prctl.h b/arch/x86/include/uapi/asm/prctl.h index e8d7ebb..1cd44ec 100644 --- a/arch/x86/include/uapi/asm/prctl.h +++ b/arch/x86/include/uapi/asm/prctl.h @@ -23,9 +23,15 @@ #define ARCH_MAP_VDSO_32 0x2002 #define ARCH_MAP_VDSO_64 0x2003 +/* Don't use 0x3001-0x3004 because of old glibcs */ + #define ARCH_GET_UNTAG_MASK 0x4001 #define ARCH_ENABLE_TAGGED_ADDR 0x4002 #define ARCH_GET_MAX_TAG_BITS 0x4003 #define ARCH_FORCE_TAGGED_SVA 0x4004 +#define ARCH_SHSTK_ENABLE 0x5001 +#define ARCH_SHSTK_DISABLE 0x5002 +#define ARCH_SHSTK_LOCK 0x5003 + #endif /* _ASM_X86_PRCTL_H */ diff --git a/arch/x86/kernel/Makefile b/arch/x86/kernel/Makefile index abee056..6b6bf47 100644 --- a/arch/x86/kernel/Makefile +++ b/arch/x86/kernel/Makefile @@ -147,6 +147,8 @@ obj-$(CONFIG_CALL_THUNKS) += callthunks.o obj-$(CONFIG_X86_CET) += cet.o +obj-$(CONFIG_X86_USER_SHADOW_STACK) += shstk.o + ### # 64 bit specific files ifeq ($(CONFIG_X86_64),y) diff --git a/arch/x86/kernel/process_64.c b/arch/x86/kernel/process_64.c index 3d181c1..0f89aa0 100644 --- a/arch/x86/kernel/process_64.c +++ b/arch/x86/kernel/process_64.c @@ -515,6 +515,8 @@ start_thread_common(struct pt_regs *regs, unsigned long new_ip, load_gs_index(__USER_DS); } + reset_thread_features(); + loadsegment(fs, 0); loadsegment(es, _ds); loadsegment(ds, _ds); @@ -894,6 +896,10 @@ long do_arch_prctl_64(struct task_struct *task, int option, unsigned long arg2) else return put_user(LAM_U57_BITS, (unsigned long __user *)arg2); #endif + case ARCH_SHSTK_ENABLE: + case ARCH_SHSTK_DISABLE: + case ARCH_SHSTK_LOCK: + return shstk_prctl(task, option, arg2); default: ret = -EINVAL; break; diff --git a/arch/x86/kernel/shstk.c b/arch/x86/kernel/shstk.c new file mode 100644 index 0000000..41ed655 --- /dev/null +++ b/arch/x86/kernel/shstk.c @@ -0,0 +1,44 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * shstk.c - Intel shadow stack support + * + * Copyright (c) 2021, Intel Corporation. + * Yu-cheng Yu + */ + +#include +#include +#include + +void reset_thread_features(void) +{ + current->thread.features = 0; + current->thread.features_locked = 0; +} + +long shstk_prctl(struct task_struct *task, int option, unsigned long features) +{ + if (option == ARCH_SHSTK_LOCK) { + task->thread.features_locked |= features; + return 0; + } + + /* Don't allow via ptrace */ + if (task != current) + return -EINVAL; + + /* Do not allow to change locked features */ + if (features & task->thread.features_locked) + return -EPERM; + + /* Only support enabling/disabling one feature at a time. */ + if (hweight_long(features) > 1) + return -EINVAL; + + if (option == ARCH_SHSTK_DISABLE) { + return -EINVAL; + } + + /* Handle ARCH_SHSTK_ENABLE */ + return -EINVAL; +}