From patchwork Mon Nov 11 20:54:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13871358 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 16EFCD3ABF5 for ; Mon, 11 Nov 2024 20:55:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 99A2B6B00CD; Mon, 11 Nov 2024 15:54:55 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8D1726B00CE; Mon, 11 Nov 2024 15:54:55 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6D5A86B00CF; Mon, 11 Nov 2024 15:54:55 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 495026B00CD for ; Mon, 11 Nov 2024 15:54:55 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 04547161756 for ; Mon, 11 Nov 2024 20:54:54 +0000 (UTC) X-FDA: 82775018130.29.37FFD06 Received: from mail-pg1-f181.google.com (mail-pg1-f181.google.com [209.85.215.181]) by imf11.hostedemail.com (Postfix) with ESMTP id 7AD654000D for ; Mon, 11 Nov 2024 20:54:02 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=qfYei0GY; spf=pass (imf11.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.181 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1731358351; h=from:from:sender: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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=GP0I58BLUE60FYcCdRZWgegjor4Rhw7u4n7VUbDEgHI=; b=zqDrRsGaRB1DryG07EGXrYVP/Bc/lrPYDLaThmGjMvepYkotkYPk+o9kyTpWt0/Wy/0W4z q77HRVeAHf8S5y1yceM+1y4yb3VPpLoCSabe2sKG3jlqxsn5W6pCnpQtkI8/qBqpcW91LY Yfv7fPG8useQncCwA4CU41DtM0lXpKA= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731358351; a=rsa-sha256; cv=none; b=knC3qecWVD0D78Riv3RiHlB8+YNf+OJIkb3VIgZMQaoX7Qgx68PJYFkaUUMhAKVb/rgf1C 1XQsKWi8ZnTo0yXYUpyvrtiOrDRWHNjYMA+0yV/aFUfmftSATUbJMa+YCDLW97h0U1szXu ic1Rxj77wxrpEPUnSRGGm8AEx39poF0= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=qfYei0GY; spf=pass (imf11.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.181 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pg1-f181.google.com with SMTP id 41be03b00d2f7-7f3f1849849so3350467a12.1 for ; Mon, 11 Nov 2024 12:54:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1731358492; x=1731963292; darn=kvack.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=GP0I58BLUE60FYcCdRZWgegjor4Rhw7u4n7VUbDEgHI=; b=qfYei0GY1pKogG1Ate61p53FEwexdgsz6kkyOxQi8J/yqqEfXbmtLeXScI8oMzZbkM UUoBPAobrdW4FkokU7SUM23ttNMoPRkl3VlEtfvJUjCPV097aiRZy9HB9hJcNcvvPEnk zvK0yE8MHWuO+OX7u4hxmJ5U54Lkli1EiQ57Gf7ut/6wDqjYip8ksPM4GJWwdl9LxbHl Tgx6H/22jfP8sficC7bUr4ngH6kM5XDCdmrhig1hMQrMj/Z2OLC2OZLlEopwRxF760BP WDxuhWkKl/rZpEKFLA+iHkJkOzLUBkPjwXY7s6Jxhx/ZUMMc+Yl7EZQ75X6nApe8T0yf 4lWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731358492; x=1731963292; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=GP0I58BLUE60FYcCdRZWgegjor4Rhw7u4n7VUbDEgHI=; b=lc0GLTXiva6Yp71R+jisItJ8OY2RvYcS4rX4DBhMwz7HKrHDm3njGInoX5e3s4QV6d s4ireDjoWWANay4kHqBEt5uo7cPSYn89b/hoDA1VOpgzsipt7PQwNrNQ5jK8PdDCYEHI fVwEuwwaXpnR3tJzZj27zFlO5pjkR6AZMiKI+m9BHMkTbwj6QoV8nm+Cqsm8bGNfsL+/ 80eqe+F19690zE2KmmYPvvY8pJ9vvaon8H1tmzphFCW6xJWSHE4ycw5bbhRHro8N8f7O Wb6NGn1Thkhus2kaDRfzscDk75BPA1MjTDfcaHwdUYCyBLBOwiWiW9GYnSGw/x53JABH zREw== X-Forwarded-Encrypted: i=1; AJvYcCV+mB9FFpOAQglUzQwLumMzxftSWZVpqfkxtJMDDDSYExG2O6Q6HInttij+sH8DkM8uNRrARQ/8qw==@kvack.org X-Gm-Message-State: AOJu0YwvqL3CqaPX30zGsLDTM1DNTy1HPHIqonnZfPp0kmpaCVF7DwfE SSnhZzrb7i5N12NRLi7XC7TJZxDQbJlW/VoL+DsmVYLkK0qKaP0f6/e2si71GaY= X-Google-Smtp-Source: AGHT+IEIBfOZ1jmuiPm63k8prxDErgx1l4b0Xg9s/9vxhWpYcoduBzr4Dyzk8y8uGrWaFpzc99rxZg== X-Received: by 2002:a17:90b:4a49:b0:2e2:92dc:6fd4 with SMTP id 98e67ed59e1d1-2e9e4c73b28mr403622a91.23.1731358491799; Mon, 11 Nov 2024 12:54:51 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e9a5fd1534sm9059974a91.42.2024.11.11.12.54.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Nov 2024 12:54:51 -0800 (PST) From: Deepak Gupta Date: Mon, 11 Nov 2024 12:54:06 -0800 Subject: [PATCH v8 21/29] riscv/ptrace: riscv cfi status and state via ptrace and in core files MIME-Version: 1.0 Message-Id: <20241111-v5_user_cfi_series-v8-21-dce14aa30207@rivosinc.com> References: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@rivosinc.com> In-Reply-To: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@rivosinc.com> To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Paul Walmsley , Palmer Dabbelt , Albert Ou , Conor Dooley , Rob Herring , Krzysztof Kozlowski , Arnd Bergmann , Christian Brauner , Peter Zijlstra , Oleg Nesterov , Eric Biederman , Kees Cook , Jonathan Corbet , Shuah Khan Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, alistair.francis@wdc.com, richard.henderson@linaro.org, jim.shu@sifive.com, andybnac@gmail.com, kito.cheng@sifive.com, charlie@rivosinc.com, atishp@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, alexghiti@rivosinc.com, samitolvanen@google.com, broonie@kernel.org, rick.p.edgecombe@intel.com, Deepak Gupta X-Mailer: b4 0.14.0 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 7AD654000D X-Stat-Signature: t43qk5euuwkh7zdbphqcoimdgic4ri6n X-HE-Tag: 1731358442-481269 X-HE-Meta: U2FsdGVkX197mYREVeaJKavS97bmX87JwAt8qWT26llzwVkwU3CcxgEe+uFkvqBlQX4FYH1ZmRAi3x+GFtg+CbR4vGVZLMYahCgRh5U6gfgwEvcOErXsfd5CB3N1GGCp3wlkBJdtlApxhZIYkOhTG5s9Wo2KlGTfmrnsYd5ZoLY1wYDcwvAV3xhyVmiFJ9HFHwFMu1A/INxD0XsLbmcMCjf6z100rElTNgTzrVdtJBcOc2JJwrmYgvTjXQcAaI6FoiJkQF7t27UPY/m0nybQLHbW0AoUe+bzVM8TOe+/gpYP0tulVCfHu2nXrI2cbjLjc1SWPBtMEQcEVJVRniq1GWRsTT35F8rK0BjMvP8Mm9ZFIP/8VQIfBaDi+H7P6i/l0ZZWMNXAxo5ttso8dX8Ww7xicKmQbh6fiDjp5sAXcP9+RokXUEmFO2jniyfLazLQFr5NnT05yrxCnm+VhSMPVe+h11MJJY90g0bm2aqz87ij0jPzNu6LfwTpSAV2/Ck396xa0boJ9m1+veyVRDtgsOOYwa4Y4HawTY6C5/h0qnuQakNdlq1FJz+n0RFpyZMWWYSRyliq0JyPn6ySE1M3ahk3YGGogUtcBWsSrpAZkJjTUg80UflPc0UbFovJnIQis0vODV//K2U/3BczHsAI8+4wAFF9FH9oXnZIiy0rOtEFiwxqTHyLQeMPGJbV4Cl1UBc1g9JIwndaXPTqme9oZgeml9kk/1uvqg7rygrOz0f4qV0ir+V4f9ZI0qFWhSFDJCnmt34hbbe+Lf26R88fy+4dZ5vSETCV1b9jsTBwccDnpd3mWFnWovusDbUA8X7Xi+gmVHkfuq9TXCZ4YW9MnonR28TkgziMOQbKTOdLUo3L7+DCdT6lQbEH3Li4MR2wxKmPrhBqjYiUDzS0iGGBvSY4/J2GYz7NuzwTPoKItYwrRgFYt06wGgrxYtMI1XiuwCu2djA4FrNGAc4dqqg ePkPsf3J l2iO5onOoedU7wOp0ZqA1B+mzvFptYVTuTIoNy3+hS05EwNHUSToCu9+rAXedU9vvdxHAyid0OjRRpnMTn8TDIqmVmzTsN619U5aT0YnmUOo6PDdTIugPF+60MEa3W0FOrnhJmvOR+pk85lzlFUK6ggkmt+WZYfWu4jyFJ26R1yDhV+o6TYL3j+ZyKZIu56q8l7H0kaR3TAWlMJQCrPu4854koJN4s8vDi3bSIcgmp6JNcr7+q8WhPvYU91OFQeVgC7ywjzRYs1itFQYKBB5Q4ZmVoKWSMA9iYfmYked9NphLnyl1q2LuHyEI2OKb4dNYgtp9PxTebxoY1+an9hpsKkUCbkuJENUtjThdxXq8VBGp2QSKpnvQtbMVeWszTiKor4qxLrNGHisEHEfcyXLxI1kWYYYqczpS2I3eEVFa7hpFSFSAHp/8Rfin2LM/4sD0SOryjFgv0764MITvUpILy8pqGA== 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: List-Subscribe: List-Unsubscribe: Expose a new register type NT_RISCV_USER_CFI for risc-v cfi status and state. Intentionally both landing pad and shadow stack status and state are rolled into cfi state. Creating two different NT_RISCV_USER_XXX would not be useful and wastage of a note type. Enabling or disabling of feature is not allowed via ptrace set interface. However setting `elp` state or setting shadow stack pointer are allowed via ptrace set interface. It is expected `gdb` might have use to fixup `elp` state or `shadow stack` pointer. Signed-off-by: Deepak Gupta --- arch/riscv/include/uapi/asm/ptrace.h | 18 ++++++++ arch/riscv/kernel/ptrace.c | 83 ++++++++++++++++++++++++++++++++++++ include/uapi/linux/elf.h | 1 + 3 files changed, 102 insertions(+) diff --git a/arch/riscv/include/uapi/asm/ptrace.h b/arch/riscv/include/uapi/asm/ptrace.h index 659ea3af5680..e6571fba8a8a 100644 --- a/arch/riscv/include/uapi/asm/ptrace.h +++ b/arch/riscv/include/uapi/asm/ptrace.h @@ -131,6 +131,24 @@ struct __sc_riscv_cfi_state { unsigned long ss_ptr; /* shadow stack pointer */ }; +struct __cfi_status { + /* indirect branch tracking state */ + __u64 lp_en : 1; + __u64 lp_lock : 1; + __u64 elp_state : 1; + + /* shadow stack status */ + __u64 shstk_en : 1; + __u64 shstk_lock : 1; + + __u64 rsvd : sizeof(__u64) - 5; +}; + +struct user_cfi_state { + struct __cfi_status cfi_status; + __u64 shstk_ptr; +}; + #endif /* __ASSEMBLY__ */ #endif /* _UAPI_ASM_RISCV_PTRACE_H */ diff --git a/arch/riscv/kernel/ptrace.c b/arch/riscv/kernel/ptrace.c index ea67e9fb7a58..1c546c01715e 100644 --- a/arch/riscv/kernel/ptrace.c +++ b/arch/riscv/kernel/ptrace.c @@ -19,6 +19,7 @@ #include #include #include +#include enum riscv_regset { REGSET_X, @@ -31,6 +32,9 @@ enum riscv_regset { #ifdef CONFIG_RISCV_ISA_SUPM REGSET_TAGGED_ADDR_CTRL, #endif +#ifdef CONFIG_RISCV_USER_CFI + REGSET_CFI, +#endif }; static int riscv_gpr_get(struct task_struct *target, @@ -184,6 +188,75 @@ static int tagged_addr_ctrl_set(struct task_struct *target, } #endif +#ifdef CONFIG_RISCV_USER_CFI +static int riscv_cfi_get(struct task_struct *target, + const struct user_regset *regset, + struct membuf to) +{ + struct user_cfi_state user_cfi; + struct pt_regs *regs; + + regs = task_pt_regs(target); + + user_cfi.cfi_status.lp_en = is_indir_lp_enabled(target); + user_cfi.cfi_status.lp_lock = is_indir_lp_locked(target); + user_cfi.cfi_status.elp_state = (regs->status & SR_ELP); + + user_cfi.cfi_status.shstk_en = is_shstk_enabled(target); + user_cfi.cfi_status.shstk_lock = is_shstk_locked(target); + user_cfi.shstk_ptr = get_active_shstk(target); + + return membuf_write(&to, &user_cfi, sizeof(user_cfi)); +} + +/* + * Does it make sense to allowing enable / disable of cfi via ptrace? + * Not allowing enable / disable / locking control via ptrace for now. + * Setting shadow stack pointer is allowed. GDB might use it to unwind or + * some other fixup. Similarly gdb might want to suppress elp and may want + * to reset elp state. + */ +static int riscv_cfi_set(struct task_struct *target, + const struct user_regset *regset, + unsigned int pos, unsigned int count, + const void *kbuf, const void __user *ubuf) +{ + int ret; + struct user_cfi_state user_cfi; + struct pt_regs *regs; + + regs = task_pt_regs(target); + + ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &user_cfi, 0, -1); + if (ret) + return ret; + + /* + * Not allowing enabling or locking shadow stack or landing pad + * There is no disabling of shadow stack or landing pad via ptrace + * rsvd field should be set to zero so that if those fields are needed in future + */ + if (user_cfi.cfi_status.lp_en || user_cfi.cfi_status.lp_lock || + user_cfi.cfi_status.shstk_en || user_cfi.cfi_status.shstk_lock || + !user_cfi.cfi_status.rsvd) + return -EINVAL; + + /* If lpad is enabled on target and ptrace requests to set / clear elp, do that */ + if (is_indir_lp_enabled(target)) { + if (user_cfi.cfi_status.elp_state) /* set elp state */ + regs->status |= SR_ELP; + else + regs->status &= ~SR_ELP; /* clear elp state */ + } + + /* If shadow stack enabled on target, set new shadow stack pointer */ + if (is_shstk_enabled(target)) + set_active_shstk(target, user_cfi.shstk_ptr); + + return 0; +} +#endif + static const struct user_regset riscv_user_regset[] = { [REGSET_X] = { .core_note_type = NT_PRSTATUS, @@ -224,6 +297,16 @@ static const struct user_regset riscv_user_regset[] = { .set = tagged_addr_ctrl_set, }, #endif +#ifdef CONFIG_RISCV_USER_CFI + [REGSET_CFI] = { + .core_note_type = NT_RISCV_USER_CFI, + .align = sizeof(__u64), + .n = sizeof(struct user_cfi_state) / sizeof(__u64), + .size = sizeof(__u64), + .regset_get = riscv_cfi_get, + .set = riscv_cfi_set, + }, +#endif }; static const struct user_regset_view riscv_user_native_view = { diff --git a/include/uapi/linux/elf.h b/include/uapi/linux/elf.h index a920cf8934dc..83d16bc61709 100644 --- a/include/uapi/linux/elf.h +++ b/include/uapi/linux/elf.h @@ -451,6 +451,7 @@ typedef struct elf64_shdr { #define NT_RISCV_CSR 0x900 /* RISC-V Control and Status Registers */ #define NT_RISCV_VECTOR 0x901 /* RISC-V vector registers */ #define NT_RISCV_TAGGED_ADDR_CTRL 0x902 /* RISC-V tagged address control (prctl()) */ +#define NT_RISCV_USER_CFI 0x903 /* RISC-V shadow stack state */ #define NT_LOONGARCH_CPUCFG 0xa00 /* LoongArch CPU config registers */ #define NT_LOONGARCH_CSR 0xa01 /* LoongArch control and status registers */ #define NT_LOONGARCH_LSX 0xa02 /* LoongArch Loongson SIMD Extension registers */