From patchwork Mon Feb 27 22:29:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 62192 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp2689556wrd; Mon, 27 Feb 2023 14:55:42 -0800 (PST) X-Google-Smtp-Source: AK7set/ML09CH2G+B66eXcNrWWgNHJWiGC8dqQEVXTQpktcm/nVHcagu+TTSRtY7A8Gw+bv1iRu0 X-Received: by 2002:a17:906:9f19:b0:8b1:7de9:b38c with SMTP id fy25-20020a1709069f1900b008b17de9b38cmr475650ejc.52.1677538542798; Mon, 27 Feb 2023 14:55:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677538542; cv=none; d=google.com; s=arc-20160816; b=KsXcEBNMT6/8DiWk3uCF0oSsDJwZl3ApLTLTt3eUEQAvaeScsbD7LBSgj73LOtKcxR DpzIJ55IZpt5wWc4Aykqwl7CHLlduob8t1wovyAmlLIo3TegjoBz/P9oBjPAvBzBpvSZ 1tu3strfVdyuJ4TKs07AOzzcpH8jboYb6BJBaMFQv6hS6nRMgATJpt/pDgMFwyfj84C/ rBWPT4hVWDZwTHwu1Z+mSCvqYNY4niDhV9RxCNM8WrGpiX3oyOEqAzTi3kjwgugCJNb8 oRsss7Hr5qoCwrSELIe5v/ssLq9v6Fgt02jcwSJ5BxaKsaOz+1gfJzlkhrnuFtfyaL9W +4+g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from:dkim-signature; bh=s3SKx8uA87FhB8rouLjfRqrxDp5yHfGp17WLKTIWOmg=; b=W8bsHLKKlCvAcOrhVDfX4rMMf3XXHYiveDpvcHov8UCjDYk7YGAXE6Pnl07hMGB5pW cD9YMqjA91G1RSx8mLXH2LcQBr23HLRYptHw2fWlMAWqwELgjjKtSpB7FZ0mTqGoGjpg dW1s14pHcznLfW88l4+gGCeGR1yKIl/DFbnHbn3Y6G+Mc06aNH1vDq2fScl9v/i6hynr TMr78xgiHEEnSwCjzsD/cHaVpQr39SKpXH5zgsAg4XMgPlVmhUhGbGdeyNDK6HPSdWJ/ M3QIwF1bhEz/j7H0R0go+hQ9z4/FwEqIOCz/kaGezz4abrn8LGVxqglSSNSDCGrWoCeT 9SLA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=NxyD5Gvf; 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=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i19-20020a17090685d300b008c86dd47e82si9554024ejy.581.2023.02.27.14.55.17; Mon, 27 Feb 2023 14:55:42 -0800 (PST) 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=@intel.com header.s=Intel header.b=NxyD5Gvf; 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=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231153AbjB0WiC (ORCPT + 99 others); Mon, 27 Feb 2023 17:38:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46718 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230260AbjB0Whb (ORCPT ); Mon, 27 Feb 2023 17:37:31 -0500 Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AA00C244B5; Mon, 27 Feb 2023 14:33:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1677537203; x=1709073203; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=dUBKpkq7gq2yOqJjADRSuNiVlevkRQtTmCsetlkcYh0=; b=NxyD5GvfR3k1Feyz9RMmFr/mRJQ9Nfvb9FOrz/nHA8mGK5xcPUfqWHOI EeGGzjSNOZQ4Cp4Rm/ezYL9a1IyS1lT1Ax37GRDu4UzorQAU0SaPPVxyZ jKSKmFgJS8wjmAtYCsuZqKKnLyxy+lJO969YDG545a6dVW2mStVgvVo2E rVZDx07/G7Futfd0qKzj/2tyG173afj4oHQJVyf+EhnWhwphp8GlsYFSm /fPH25Ah+SFHiLwQ4A665nKn+mcrq/klSRDMwinFedAx6FEk2VwsSutPZ gbR3cdT70ilGkkd8Z8aZY24xSPJ3s9n68484XBxFSQlTSKw/f3fgzMRmM g==; X-IronPort-AV: E=McAfee;i="6500,9779,10634"; a="313658000" X-IronPort-AV: E=Sophos;i="5.98,220,1673942400"; d="scan'208";a="313658000" Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Feb 2023 14:31:38 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10634"; a="848024827" X-IronPort-AV: E=Sophos;i="5.98,220,1673942400"; d="scan'208";a="848024827" Received: from leonqu-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.72.19]) by orsmga005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Feb 2023 14:31:37 -0800 From: Rick Edgecombe 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 , Weijiang Yang , "Kirill A . Shutemov" , John Allen , kcc@google.com, eranian@google.com, rppt@kernel.org, jamorris@linux.microsoft.com, dethoma@microsoft.com, akpm@linux-foundation.org, Andrew.Cooper3@citrix.com, christina.schimpe@intel.com, david@redhat.com, debug@rivosinc.com Cc: rick.p.edgecombe@intel.com Subject: [PATCH v7 41/41] x86/shstk: Add ARCH_SHSTK_STATUS Date: Mon, 27 Feb 2023 14:29:57 -0800 Message-Id: <20230227222957.24501-42-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230227222957.24501-1-rick.p.edgecombe@intel.com> References: <20230227222957.24501-1-rick.p.edgecombe@intel.com> X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_PASS,SPF_NONE 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: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1759026655032173483?= X-GMAIL-MSGID: =?utf-8?q?1759026655032173483?= CRIU and GDB need to get the current shadow stack and WRSS enablement status. This information is already available via /proc/pid/status, but this is inconvenient for CRIU because it involves parsing the text output in an area of the code where this is difficult. Provide a status arch_prctl(), ARCH_SHSTK_STATUS for retrieving the status. Have arg2 be a userspace address, and make the new arch_prctl simply copy the features out to userspace. Tested-by: Pengfei Xu Tested-by: John Allen Tested-by: Kees Cook Acked-by: Mike Rapoport (IBM) Reviewed-by: Kees Cook Suggested-by: Mike Rapoport Signed-off-by: Rick Edgecombe --- v5: - Fix typo in commit log v4: - New patch --- Documentation/x86/shstk.rst | 6 ++++++ arch/x86/include/asm/shstk.h | 2 +- arch/x86/include/uapi/asm/prctl.h | 1 + arch/x86/kernel/process_64.c | 1 + arch/x86/kernel/shstk.c | 8 +++++++- 5 files changed, 16 insertions(+), 2 deletions(-) diff --git a/Documentation/x86/shstk.rst b/Documentation/x86/shstk.rst index e8ed5fc0f7ae..7f4af798794e 100644 --- a/Documentation/x86/shstk.rst +++ b/Documentation/x86/shstk.rst @@ -77,6 +77,11 @@ arch_prctl(ARCH_SHSTK_UNLOCK, unsigned long features) Unlock features. 'features' is a mask of all features to unlock. All bits set are processed, unset bits are ignored. Only works via ptrace. +arch_prctl(ARCH_SHSTK_STATUS, unsigned long addr) + Copy the currently enabled features to the address passed in addr. The + features are described using the bits passed into the others in + 'features'. + The return values are as follows. On success, return 0. On error, errno can be:: @@ -84,6 +89,7 @@ be:: -ENOTSUPP if the feature is not supported by the hardware or kernel. -EINVAL arguments (non existing feature, etc) + -EFAULT if could not copy information back to userspace The feature's bits supported are:: diff --git a/arch/x86/include/asm/shstk.h b/arch/x86/include/asm/shstk.h index acee68d30a07..be9267897211 100644 --- a/arch/x86/include/asm/shstk.h +++ b/arch/x86/include/asm/shstk.h @@ -14,7 +14,7 @@ struct thread_shstk { u64 size; }; -long shstk_prctl(struct task_struct *task, int option, unsigned long features); +long shstk_prctl(struct task_struct *task, int option, unsigned long arg2); void reset_thread_features(void); int shstk_alloc_thread_stack(struct task_struct *p, unsigned long clone_flags, unsigned long stack_size, diff --git a/arch/x86/include/uapi/asm/prctl.h b/arch/x86/include/uapi/asm/prctl.h index 200efbbe5809..1b85bc876c2d 100644 --- a/arch/x86/include/uapi/asm/prctl.h +++ b/arch/x86/include/uapi/asm/prctl.h @@ -26,6 +26,7 @@ #define ARCH_SHSTK_DISABLE 0x5002 #define ARCH_SHSTK_LOCK 0x5003 #define ARCH_SHSTK_UNLOCK 0x5004 +#define ARCH_SHSTK_STATUS 0x5005 /* ARCH_SHSTK_ features bits */ #define ARCH_SHSTK_SHSTK (1ULL << 0) diff --git a/arch/x86/kernel/process_64.c b/arch/x86/kernel/process_64.c index d368854fa9c4..dde43caf196e 100644 --- a/arch/x86/kernel/process_64.c +++ b/arch/x86/kernel/process_64.c @@ -836,6 +836,7 @@ long do_arch_prctl_64(struct task_struct *task, int option, unsigned long arg2) case ARCH_SHSTK_DISABLE: case ARCH_SHSTK_LOCK: case ARCH_SHSTK_UNLOCK: + case ARCH_SHSTK_STATUS: return shstk_prctl(task, option, arg2); default: ret = -EINVAL; diff --git a/arch/x86/kernel/shstk.c b/arch/x86/kernel/shstk.c index 3197ff824809..4069d5bbbe8c 100644 --- a/arch/x86/kernel/shstk.c +++ b/arch/x86/kernel/shstk.c @@ -444,8 +444,14 @@ SYSCALL_DEFINE3(map_shadow_stack, unsigned long, addr, unsigned long, size, unsi return alloc_shstk(addr, aligned_size, size, set_tok); } -long shstk_prctl(struct task_struct *task, int option, unsigned long features) +long shstk_prctl(struct task_struct *task, int option, unsigned long arg2) { + unsigned long features = arg2; + + if (option == ARCH_SHSTK_STATUS) { + return put_user(task->thread.features, (unsigned long __user *)arg2); + } + if (option == ARCH_SHSTK_LOCK) { task->thread.features_locked |= features; return 0;