From nobody Sat Nov 15 10:38:42 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=1754179152; cv=none; d=zohomail.com; s=zohoarc; b=lJ1d2+zL4Fp9d+qyBMyZXNwTlWUBHaCotrO3quTk7XvGQZaLnW8wWCiYj4PAt1K8spQAYtpQpXg2ft/uRUhjuPi9e3HvXqJEG8lMy3aSEyBggv0ELczGDZnBCVaYcJm881cpN8vE5MQ9INBq/FRL0SO3reShJzlN2kt4UefeQ2o= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1754179152; 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=lflJ+SmP9/WjBOPLau+3bavo7afA8IIGWLBMr9HKB4c=; b=V1DzUQ2YCy4KYwA45vBxTCoOVURlClkjeeTRbZ4X7CZSj7YQzhD+pNCukQTm9oR3ReIFqXx8TbkCP+BKLHiZciRIwTs6kVrt4735g5C78NxuZeY/ASoxOByZCJIDfu72moa/0vzKMRgHmP9oK5l4asXXCwNJHbXjWTpxnjgUqIk= 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 175417915210695.68456125018213; Sat, 2 Aug 2025 16:59:12 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uiLtF-0006X8-WC; Sat, 02 Aug 2025 19:43:50 -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 1uiLoS-0005yg-Mo for qemu-devel@nongnu.org; Sat, 02 Aug 2025 19:39:03 -0400 Received: from mail-ot1-x329.google.com ([2607:f8b0:4864:20::329]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uiLoQ-0006Qy-ND for qemu-devel@nongnu.org; Sat, 02 Aug 2025 19:38:52 -0400 Received: by mail-ot1-x329.google.com with SMTP id 46e09a7af769-73e58d51060so736667a34.0 for ; Sat, 02 Aug 2025 16:38:50 -0700 (PDT) Received: from stoup.. ([172.58.111.133]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-741a71dc78asm380409a34.30.2025.08.02.16.38.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 02 Aug 2025 16:38:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1754177929; x=1754782729; 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=lflJ+SmP9/WjBOPLau+3bavo7afA8IIGWLBMr9HKB4c=; b=tHevbo9/aQzb/+shwL+6MtCZE24uMlWonxndulCq0V4/h/cWYvTmuT5oPVw21FJNYm XNRMaOo6SBVKWkuUc/2BVel1eydGKvSnt//o1luqxNtUL4vF2SFIOG1u2yoTBdbYzz4b MfQnI6WWKc50EganVM0jUQ2tbJTfa6G3A6ljoxCTXrcyA005g3+5duZgQJxmB+yJRb1r mHB8rEBHyBrNn2MZLfoseCkXUi3Izg8mZPBPk3qR5I4Dp3iVP658NtQpIQ90SUosjuGF +/CdSdqQlLb+lZAIWRMpcrw4Neg6Rmp8cfLRXUijGVuV2THnf3IBcWO/oXq99cwEOeKA 6KEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1754177929; x=1754782729; 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=lflJ+SmP9/WjBOPLau+3bavo7afA8IIGWLBMr9HKB4c=; b=mUA1PKyiamYG+Bx+6Rj2aHhOJzfp0yM1hrn4Hi1GsFzUmcyWcg6dZghbFGRsJyLACv J2s19yc1+OQjiYMS9Ot/0McTlaAaiuoUgELNdmQ/mk5znTYfE9AUvWb4rkkqSSjhB8ft fjN0+wm9ol7Er5reTOqsAszqBcfV+/aooVvU0s0nPVKSgPGoQb5RP06dTKFFMS5tUlJS p+Ru+Br30OWHln6zWFXxIauFwJ+l++1z6vALNis5wvuXP9RsSYyaNGlwKNEh6/R9bo4z W/D+wB7DqIUGFmr1/nz6mh/tKD4I76d617Y8sLmo38CzSAj8rBhcZ/bR0ehNKHgJodpo zNlg== X-Gm-Message-State: AOJu0YzTviPT5xldJVP11bfaNera2Xo837YezMzYnk/no63pd64T4nEc rTKAcUgBbVxODysBulG1e8bA7edSylofE3JKRTxza7NPXkwaFTOVkRcCcSA6WemT1G506UK2ZAs r/FWS2LE= X-Gm-Gg: ASbGnct5llsWPhb4plji6MIDMz1fRxwL6P2bLcOzVHED0UAABiUFZRRItkSEC788K+h zW3kJG/c/S4U7sug38KIqkQKu9ilcmsjh90UIsxaRoIU62cVv5boWbnKpGXwoFOde6/hy8MDQQC frjyqvgcCN+bIdbxNUS8bnPogsGtnicdVdfweCn6HqwxvM0CViy+SF9L006A9eJFF2MbFRQqIHc Tjdf4F3gFCnE5yY3wiitNOyA0GRa2XjXdBcQxX7nn0IEGZfcja7p9dWiiFayHwddPul0S64iOoL T7NsaCdIumgcVmCJ+JKj9d9VhrkkPZDs9QBaHT2I7ZyMIUUhL4ZBPVEqnKml7Zbkfk3RjNJ73RU k+cyuPQwkAFl6jYV5rzJU+f2HUP2PVGAdckW076c3SXM6CotQE4D04FP2HQu9Cy0= X-Google-Smtp-Source: AGHT+IHUnvNoLk3MlLa8ec3EshU7F7ZOyeck/zWtKVfShr+3m2JlXLKZvKrO+UmRReCToudBPx5sCA== X-Received: by 2002:a05:6830:6308:b0:73e:9cdc:a6d3 with SMTP id 46e09a7af769-7419d24f11emr2553239a34.25.1754177929534; Sat, 02 Aug 2025 16:38:49 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: qemu-arm@nongnu.org, Pierrick Bouvier Subject: [PATCH v2 74/85] linux-user/aarch64: Implement prctls for GCS Date: Sun, 3 Aug 2025 09:29:42 +1000 Message-ID: <20250802232953.413294-75-richard.henderson@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250802232953.413294-1-richard.henderson@linaro.org> References: <20250802232953.413294-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::329; envelope-from=richard.henderson@linaro.org; helo=mail-ot1-x329.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: 1754179153425116600 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 0b19fa43e6..6f0bfa83db 100644 --- a/linux-user/qemu.h +++ b/linux-user/qemu.h @@ -126,6 +126,11 @@ struct TaskState { /* Extra fields for semihosted binaries. */ abi_ulong heap_base; abi_ulong heap_limit; +#endif +#if defined(TARGET_AARCH64) + vaddr gcs_base; + abi_ulong gcs_size; + abi_ulong gcs_el0_locked; #endif abi_ulong stack_base; int used; /* non zero if used */ 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