From nobody Mon Sep 8 12:54:49 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1756574953; cv=none; d=zohomail.com; s=zohoarc; b=P9Id625InBI2F6VABDrtTGv6Zb9bZfhOR3TQBCY+jNzBkpJW/YhcwJXvsWWneK9HUpDtv0wsUZ4mWe7gKUnbtIVik1/P6YhZwkEAzIRfzkwMzvMBBVJUuJZIo8dd9v4F90h0zgYLoIAXHPOUI0l64VareLZWf6OWSMNvFAMapbc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1756574953; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=HTzcUGGxZsq7kEwv6ih902moRlxo1N6PG0gX7nMiQJ4=; b=XJmYOSoRFmWYlDf5+CrLSB/pQc03PNvaMjSmysf9wdQG6XOYvDg2Q6XonBFWdxLlqMt3mulxQdP5EVmavCEQT6bwZTtpiIgndd6AfLSSdFxMWcFQATeeZLlXPiY23V5YBXHBABqVH7xjEFqvObf8jyPpapCgE6bZF1I1xO8sKTg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1756574953165256.94567049929356; Sat, 30 Aug 2025 10:29:13 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1usOEs-00019B-5W; Sat, 30 Aug 2025 12:15:38 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1usERb-0007YI-TK for qemu-devel@nongnu.org; Sat, 30 Aug 2025 01:48:07 -0400 Received: from mail-pg1-x52e.google.com ([2607:f8b0:4864:20::52e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1usERa-000530-0L for qemu-devel@nongnu.org; Sat, 30 Aug 2025 01:48:07 -0400 Received: by mail-pg1-x52e.google.com with SMTP id 41be03b00d2f7-b475dfb4f42so1927274a12.0 for ; Fri, 29 Aug 2025 22:48:05 -0700 (PDT) Received: from stoup.. (122-150-204-179.dyn.ip.vocus.au. [122.150.204.179]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2490658999fsm40852715ad.112.2025.08.29.22.48.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 Aug 2025 22:48:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1756532884; x=1757137684; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=HTzcUGGxZsq7kEwv6ih902moRlxo1N6PG0gX7nMiQJ4=; b=iw6bbEcBtawB9ZsDqZPUBEBB3zINensW8qp2xmTRIVE5W8kveWSe6fT9XRTfx+BI3i PhofctcjOE9yTs6Urscq1ZQAqsmrsPRGZTMAmQtGuafmH24VclJ+CoSQe41Bnl9OqUzh vY0pOvAOykKm5gJ9B2FGB9X63dZA2zUaCvZfdqufnzX2EQEwh7cFOEd8E5ZDs2/3broK HqvYIdxJ4kfyOudxRurApm878qNmaCppC0N8Xhx1dlsQT9MOrYvTauoNNeMigmhHC0QE qJUcbft2d31dPv4BR1372acLfl/cIIDLC123URljbP0ZoMvOLN3OduseZp/9Z8qNz7Og 6/Zw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756532884; x=1757137684; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=HTzcUGGxZsq7kEwv6ih902moRlxo1N6PG0gX7nMiQJ4=; b=O8dfdI7dViVSA83rKY5lRY0EtFsUBvzCCLJ84HX/5IOiL8Soj1CoVOwyXL8xos5zqQ q93mRwonyMDLMm45eYVIuTp0nQh1lycMfqk/5lk7rWe4BZjUNLndNQ1qfGYTib+y/Fbp hjklBsksVg17uynywrteJi9bNZUtT1QpXK8RQa9LfDQkGgAQY24MXg2joUyCE16BNg4f WpG4VMqmKenDyo3uaCimfmRoAMxTESrn5y0U9NLHF0jkJTw1KNRsVOpumXlQfx/MkJ3y faVvI53U7He1gMvnWIWW4YLXq7vbfP7Y9UVKAfNMkiSXc0uHZIIiBCJTeQVC7M8YciMV a1Bw== X-Gm-Message-State: AOJu0YxlBGGSN1Uk1JIeBFMB934Bq5wNcE5UG4dXe/QTjJh3ktneCs63 PE3BM4QwpzEGRi569NdDkZ6+2kZntEyfeXNL6Rx2Y0OMKNZLr8JXricF1bR/sc0B0p1yb/bM3WB HJ8CKMJ4= X-Gm-Gg: ASbGncsTr+mPZE1XgLhCAIGlu1QUFHIekN4gL/fLwnYitHZ96JbDrenVYKK10dMHFkq k+NPLwLpWKl5h6STVod98FAyJacp50VIlPGPNqCUouvNCW3P/0jkbzJiJs93lF3Oz2ckgXuiDNc AimkW6vsEJDwXtAw7xyKjZ7h+c5uhcx/gB4DhQCZaLdKX8spoLzTprYqaJhEcEVMH9r8UKa07oQ 2yb4g3gVr7IwuzrtoF4w4fOcZHjppWw4LC3xC/k63ddd5i6ujIBRDl6tuFkCFRm9OGz/8ElbdOL F/pXixIEL5LWDbt1HKh2q+rM3pLiAACdYK7zh4GHPWgqKKs/ZuUehix+O1dntMJu6EKFXf9fq8T tCHRavqL4U0CNp4d2Elii/Lhw7BFLpZjF9zB01mDCvt0Uj0ajG04bqz0o0Pm5JEU= X-Google-Smtp-Source: AGHT+IGkP6UopPk67EfOGRW2pZdc2MymT4cPW0rgxrTRD+/UAQXq97E15Kh1qQvF6R7ay2xlypUDrg== X-Received: by 2002:a17:903:b10:b0:248:a18e:200a with SMTP id d9443c01a7336-2494485d6f8mr11763785ad.10.1756532884148; Fri, 29 Aug 2025 22:48:04 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: qemu-arm@nongnu.org, Pierrick Bouvier Subject: [PATCH v4 74/84] linux-user/aarch64: Implement prctls for GCS Date: Sat, 30 Aug 2025 15:41:18 +1000 Message-ID: <20250830054128.448363-75-richard.henderson@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250830054128.448363-1-richard.henderson@linaro.org> References: <20250830054128.448363-1-richard.henderson@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::52e; envelope-from=richard.henderson@linaro.org; helo=mail-pg1-x52e.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1756574955002116600 Content-Type: text/plain; charset="utf-8" This is PR_GET_SHADOW_STACK_STATUS, PR_SET_SHADOW_STACK_STATUS, and PR_LOCK_SHADOW_STACK_STATUS. Reviewed-by: Pierrick Bouvier Signed-off-by: Richard Henderson --- linux-user/aarch64/gcs-internal.h | 38 ++++++++++++ linux-user/aarch64/target_prctl.h | 96 +++++++++++++++++++++++++++++++ linux-user/qemu.h | 5 ++ linux-user/syscall.c | 29 ++++++++++ 4 files changed, 168 insertions(+) create mode 100644 linux-user/aarch64/gcs-internal.h diff --git a/linux-user/aarch64/gcs-internal.h b/linux-user/aarch64/gcs-int= ernal.h new file mode 100644 index 0000000000..e586c7e80e --- /dev/null +++ b/linux-user/aarch64/gcs-internal.h @@ -0,0 +1,38 @@ +/* + * AArch64 gcs functions for linux-user + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ +#ifndef AARCH64_GCS_INTERNAL_H +#define AARCH64_GCS_INTERNAL_H + +#ifndef PR_SHADOW_STACK_ENABLE +# define PR_SHADOW_STACK_ENABLE (1U << 0) +# define PR_SHADOW_STACK_WRITE (1U << 1) +# define PR_SHADOW_STACK_PUSH (1U << 2) +#endif + +static inline uint64_t gcs_get_el0_mode(CPUArchState *env) +{ + uint64_t cr =3D env->cp15.gcscr_el[0]; + abi_ulong flags =3D 0; + + flags |=3D cr & GCSCR_PCRSEL ? PR_SHADOW_STACK_ENABLE : 0; + flags |=3D cr & GCSCR_STREN ? PR_SHADOW_STACK_WRITE : 0; + flags |=3D cr & GCSCR_PUSHMEN ? PR_SHADOW_STACK_PUSH : 0; + + return flags; +} + +static inline void gcs_set_el0_mode(CPUArchState *env, uint64_t flags) +{ + uint64_t cr =3D GCSCRE0_NTR; + + cr |=3D flags & PR_SHADOW_STACK_ENABLE ? GCSCR_RVCHKEN | GCSCR_PCRSEL = : 0; + cr |=3D flags & PR_SHADOW_STACK_WRITE ? GCSCR_STREN : 0; + cr |=3D flags & PR_SHADOW_STACK_PUSH ? GCSCR_PUSHMEN : 0; + + env->cp15.gcscr_el[0] =3D cr; +} + +#endif diff --git a/linux-user/aarch64/target_prctl.h b/linux-user/aarch64/target_= prctl.h index ed75b9e4b5..621be5727f 100644 --- a/linux-user/aarch64/target_prctl.h +++ b/linux-user/aarch64/target_prctl.h @@ -6,8 +6,10 @@ #ifndef AARCH64_TARGET_PRCTL_H #define AARCH64_TARGET_PRCTL_H =20 +#include "qemu/units.h" #include "target/arm/cpu-features.h" #include "mte_user_helper.h" +#include "gcs-internal.h" =20 static abi_long do_prctl_sve_get_vl(CPUArchState *env) { @@ -206,4 +208,98 @@ static abi_long do_prctl_get_tagged_addr_ctrl(CPUArchS= tate *env) } #define do_prctl_get_tagged_addr_ctrl do_prctl_get_tagged_addr_ctrl =20 +static abi_long do_prctl_get_shadow_stack_status(CPUArchState *env, + abi_long arg2) +{ + ARMCPU *cpu =3D env_archcpu(env); + + if (!cpu_isar_feature(aa64_gcs, cpu)) { + return -TARGET_EINVAL; + } + return put_user_ual(gcs_get_el0_mode(env), arg2); +} +#define do_prctl_get_shadow_stack_status do_prctl_get_shadow_stack_status + +static abi_long gcs_alloc(abi_ulong hint, abi_ulong size) +{ + /* + * Without softmmu, we cannot protect GCS memory properly. + * Make do with normal read/write permissions. This at least allows + * emulation of correct programs which don't access the gcs stack + * with normal instructions. + */ + return target_mmap(hint, size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS | + (hint ? MAP_FIXED_NOREPLACE : 0), -1, 0); +} + +static abi_ulong gcs_new_stack(TaskState *ts) +{ + /* Use guest_stack_size as a proxy for RLIMIT_STACK. */ + abi_ulong size =3D MIN(MAX(guest_stack_size / 2, TARGET_PAGE_SIZE), 2 = * GiB); + abi_ulong base =3D gcs_alloc(0, size); + + if (base =3D=3D -1) { + return -1; + } + + ts->gcs_base =3D base; + ts->gcs_size =3D size; + return base + size - 8; +} + +static abi_long do_prctl_set_shadow_stack_status(CPUArchState *env, + abi_long new_mode) +{ + ARMCPU *cpu =3D env_archcpu(env); + TaskState *ts =3D get_task_state(env_cpu(env)); + abi_long cur_mode; + + if (!cpu_isar_feature(aa64_gcs, cpu)) { + return -TARGET_EINVAL; + } + if (new_mode & ~(PR_SHADOW_STACK_ENABLE | + PR_SHADOW_STACK_WRITE | + PR_SHADOW_STACK_PUSH)) { + return -TARGET_EINVAL; + } + + cur_mode =3D gcs_get_el0_mode(env); + if ((new_mode ^ cur_mode) & ts->gcs_el0_locked) { + return -TARGET_EBUSY; + } + + if (new_mode & ~cur_mode & PR_SHADOW_STACK_ENABLE) { + abi_long gcspr; + + if (ts->gcs_base || env->cp15.gcspr_el[0]) { + return -EINVAL; + } + gcspr =3D gcs_new_stack(ts); + if (gcspr =3D=3D -1) { + return -TARGET_ENOMEM; + } + env->cp15.gcspr_el[0] =3D gcspr; + } + + gcs_set_el0_mode(env, new_mode); + arm_rebuild_hflags(env); + return 0; +} +#define do_prctl_set_shadow_stack_status do_prctl_set_shadow_stack_status + +static abi_long do_prctl_lock_shadow_stack_status(CPUArchState *env, + abi_long arg2) +{ + ARMCPU *cpu =3D env_archcpu(env); + TaskState *ts =3D get_task_state(env_cpu(env)); + + if (!cpu_isar_feature(aa64_gcs, cpu)) { + return -EINVAL; + } + ts->gcs_el0_locked |=3D arg2; + return 0; +} +#define do_prctl_lock_shadow_stack_status do_prctl_lock_shadow_stack_status + #endif /* AARCH64_TARGET_PRCTL_H */ diff --git a/linux-user/qemu.h b/linux-user/qemu.h index e4dca0c20f..8a0d240405 100644 --- a/linux-user/qemu.h +++ b/linux-user/qemu.h @@ -121,6 +121,11 @@ struct TaskState { abi_ulong child_tidptr; #ifdef TARGET_M68K abi_ulong tp_value; +#endif +#if defined(TARGET_AARCH64) + vaddr gcs_base; + abi_ulong gcs_size; + abi_ulong gcs_el0_locked; #endif int used; /* non zero if used */ struct image_info *info; diff --git a/linux-user/syscall.c b/linux-user/syscall.c index 91360a072c..2f1e881046 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -6351,6 +6351,11 @@ abi_long do_arch_prctl(CPUX86State *env, int code, a= bi_ulong addr) # define PR_SME_VL_LEN_MASK 0xffff # define PR_SME_VL_INHERIT (1 << 17) #endif +#ifndef PR_GET_SHADOW_STACK_STATUS +# define PR_GET_SHADOW_STACK_STATUS 74 +# define PR_SET_SHADOW_STACK_STATUS 75 +# define PR_LOCK_SHADOW_STACK_STATUS 76 +#endif =20 #include "target_prctl.h" =20 @@ -6397,6 +6402,15 @@ static abi_long do_prctl_inval1(CPUArchState *env, a= bi_long arg2) #ifndef do_prctl_sme_set_vl #define do_prctl_sme_set_vl do_prctl_inval1 #endif +#ifndef do_prctl_get_shadow_stack_status +#define do_prctl_get_shadow_stack_status do_prctl_inval1 +#endif +#ifndef do_prctl_set_shadow_stack_status +#define do_prctl_set_shadow_stack_status do_prctl_inval1 +#endif +#ifndef do_prctl_lock_shadow_stack_status +#define do_prctl_lock_shadow_stack_status do_prctl_inval1 +#endif =20 static abi_long do_prctl(CPUArchState *env, abi_long option, abi_long arg2, abi_long arg3, abi_long arg4, abi_long arg5) @@ -6467,6 +6481,21 @@ static abi_long do_prctl(CPUArchState *env, abi_long= option, abi_long arg2, return -TARGET_EINVAL; } return do_prctl_get_tagged_addr_ctrl(env); + case PR_GET_SHADOW_STACK_STATUS: + if (arg3 || arg4 || arg5) { + return -TARGET_EINVAL; + } + return do_prctl_get_shadow_stack_status(env, arg2); + case PR_SET_SHADOW_STACK_STATUS: + if (arg3 || arg4 || arg5) { + return -TARGET_EINVAL; + } + return do_prctl_set_shadow_stack_status(env, arg2); + case PR_LOCK_SHADOW_STACK_STATUS: + if (arg3 || arg4 || arg5) { + return -TARGET_EINVAL; + } + return do_prctl_lock_shadow_stack_status(env, arg2); =20 case PR_GET_UNALIGN: return do_prctl_get_unalign(env, arg2); --=20 2.43.0