From patchwork Wed Apr 3 23:35:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13616819 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 CAE11CD1288 for ; Wed, 3 Apr 2024 23:42:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DCBB88D0005; Wed, 3 Apr 2024 19:42:45 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CBC778D0001; Wed, 3 Apr 2024 19:42:45 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A455A8D0005; Wed, 3 Apr 2024 19:42:45 -0400 (EDT) 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 7AC478D0001 for ; Wed, 3 Apr 2024 19:42:45 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 49014801E9 for ; Wed, 3 Apr 2024 23:42:45 +0000 (UTC) X-FDA: 81969847890.25.B1EA1B4 Received: from mail-pg1-f182.google.com (mail-pg1-f182.google.com [209.85.215.182]) by imf04.hostedemail.com (Postfix) with ESMTP id 6DE9F40008 for ; Wed, 3 Apr 2024 23:42:43 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=rbGh8mI0; dmarc=none; spf=pass (imf04.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.182 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1712187763; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=J4h29gLixoMV8jqguvN/5Pzgz4C0EFoOwStmiEzJSk4=; b=N6kkFLieBdVgkT/dKS0SQr7BbCzZrJNZK3OukPXx9BWYz7uxeaxxzh4l6jyiMcG0hpI/kM Lj4Tm6+/csugUeZbh1P6xRCyS0DX8+w+3Ra1SbzzdPMrwpe86E+YnEmgbs2G9i/2R5TGOO GBz0IARbUBLUI9tfpS1fJ7V4MArEfk8= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=rbGh8mI0; dmarc=none; spf=pass (imf04.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.182 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712187763; a=rsa-sha256; cv=none; b=3/MIuESqbey1YqDxW2MjGTLUUTXR5E8pIROQLli9ICTvUD+MWGsSq8mib/UXUe0P3XLfjY bcyAklomYXMf7phXFP9x6rewTDovk3xqH7DD5R9aUp2AHp0iWUteh321GurpWrUKPnMtjF SIZ1mc1kBSLjwiCUTwEL4DjH/1ONCiU= Received: by mail-pg1-f182.google.com with SMTP id 41be03b00d2f7-5d8b70b39efso396100a12.0 for ; Wed, 03 Apr 2024 16:42:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1712187762; x=1712792562; darn=kvack.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=J4h29gLixoMV8jqguvN/5Pzgz4C0EFoOwStmiEzJSk4=; b=rbGh8mI0lSIPwCI1gCMbxy0Tie1tjUKh5kxtdqkroUlcYNz6DKEUzXUAlRpPJQlSv5 9gu9bbasK4FAZAhIm7n4Ibi8CkTK9ypk5b4Oh7JXZNktsMBHbhDqpLzRFgjfi3x5TZTo UVwQRaAYa0Ef+75U6ThLGlhkmTRsmqXO3yoTyMPhXKsn4zj4y8sYx12mm7TgLfsCN//3 2d4GhMeFCOcyJG7EXbxrSG9Q3HwA+GKoqEHHr/KEXHzZn+LcuE6qJ4l5Zlgp4PqKno/c R3aIiNWoZu55bQmk+OvJ8SFiSUJOHhOc4uoUwxLrSWVJoR3AbQ3fLsu1n2sqUsb7X6Hn Fxww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712187762; x=1712792562; 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=J4h29gLixoMV8jqguvN/5Pzgz4C0EFoOwStmiEzJSk4=; b=iT4k1tKNEtkN6FAWxUoZbVyLiWnBtTH3MZ+/6uT1zZko4LQwox4wj+esNEamc/SmAo NJwj48YezbbWXMDjj2Exv0AFZLf+1nkBNQ9XRkSSypGg2PJHaq/nR9DG8yb0p3CAPms3 sy8VGdeR2tttNBNJ4HOtMC2b1Y6iHrKgvv7F6pR8vlVySfia0jZCxleqafVb2i3nQyIl v0kpJFZy2AWB0u8dJqLN6wpdEq4cwoQkYMzrw+ikDRdfmi27hq3Tc0ZlDJ9bYKsaz8QD pHak+IYtETJFONspFo5YahMSGicaMrRBeaNre/DowaSyOSja73ZBZy7+kFwVL9NaLxnV 8HQA== X-Forwarded-Encrypted: i=1; AJvYcCVV/uqdOY6ehvR+kyz6MVrefOQlW5ETqC0MlAgD6kHpYymRWmMuiJr2bYrvkk687e3VbFn/stDDWnhEMnBbmHHh/Pw= X-Gm-Message-State: AOJu0Ywbqs1n3TMww/+lzJyaqx8YAG9nbplzZP3rQ8kJUbk5J2Gzm+rq 5gPgdQ15KqZtb2mHDttA/m5tBiDgXX1upLkYtj0SucV9A4WIjH1D8aqS9746BZA= X-Google-Smtp-Source: AGHT+IG6JjvBwkxgqryw6XYXQuT6s6l7JFwHNmCUllxfSiPnkNHrCcfUHOA44/lFJ8XzXC2yFAlGAA== X-Received: by 2002:a05:6a20:6a0b:b0:1a7:ea4:e13a with SMTP id p11-20020a056a206a0b00b001a70ea4e13amr1275128pzk.54.1712187761904; Wed, 03 Apr 2024 16:42:41 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id b18-20020a170902d51200b001deeac592absm13899117plg.180.2024.04.03.16.42.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 16:42:41 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com, samuel.holland@sifive.com, conor@kernel.org Cc: linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org, corbet@lwn.net, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh+dt@kernel.org, krzysztof.kozlowski+dt@linaro.org, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, Liam.Howlett@oracle.com, vbabka@suse.cz, lstoakes@gmail.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, hankuan.chen@sifive.com, greentime.hu@sifive.com, evan@rivosinc.com, xiao.w.wang@intel.com, charlie@rivosinc.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, dbarboza@ventanamicro.com, sameo@rivosinc.com, shikemeng@huaweicloud.com, willy@infradead.org, vincent.chen@sifive.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, gerg@kernel.org, heiko@sntech.de, bhe@redhat.com, jeeheng.sia@starfivetech.com, cyy@cyyself.name, maskray@google.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bgray@linux.ibm.com, mpe@ellerman.id.au, baruch@tkos.co.il, alx@kernel.org, david@redhat.com, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, shr@devkernel.io, deller@gmx.de, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com Subject: [PATCH v3 24/29] riscv/ptrace: riscv cfi status and state via ptrace and in core files Date: Wed, 3 Apr 2024 16:35:12 -0700 Message-ID: <20240403234054.2020347-25-debug@rivosinc.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240403234054.2020347-1-debug@rivosinc.com> References: <20240403234054.2020347-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 6DE9F40008 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 6qtqo3jdjeqj757denpbrikodidhf9f9 X-HE-Tag: 1712187763-515942 X-HE-Meta: U2FsdGVkX18vvG0cSaCJ7weOY4fvXkNw9xPm8JaHJ5JCfLomdlvO0sq8cKE5vZXP42L+DdzsS5+EsTA9ww3aUXOO11E9W04v2VkjshMsTk5nsjtmxql7P9z2fXN0HLxTqt5bW279rjX+YGDISl25KrwLboG5PthLsRWICSjdee2p3lJdXRCH18xx7UZiwea/Sz/Hr5COwHBaRfIkbIQxqiQmOjmu80RQsV1lnSRrAavsMkZwmFOHBZouq6XJD6fIroos9YE2qpXhv5GRF+oup8LmOHMGeGo69rbDAYXVg/mi0qlBSejyJ50ZdAQMJcNU1bIzZAdXltzK7QKzY7Em+Ly2Kpvw38kHxdwHnPzx/mxSSGuSBwvO9o1hlHK36PRuWwfjf7Z61OgYOA/6OFXBj5d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kC3me5aRDfCsUTXvSyNBnuSBMLYjne1ha3zlEa6s= 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 a38268b19c3d..512be06a8661 100644 --- a/arch/riscv/include/uapi/asm/ptrace.h +++ b/arch/riscv/include/uapi/asm/ptrace.h @@ -127,6 +127,24 @@ struct __riscv_v_regset_state { */ #define RISCV_MAX_VLENB (8192) +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 e8515aa9d80b..33d4b32cc6a7 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, @@ -28,6 +29,9 @@ enum riscv_regset { #ifdef CONFIG_RISCV_ISA_V REGSET_V, #endif +#ifdef CONFIG_RISCV_USER_CFI + REGSET_CFI, +#endif }; static int riscv_gpr_get(struct task_struct *target, @@ -152,6 +156,75 @@ static int riscv_vr_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, @@ -182,6 +255,16 @@ static const struct user_regset riscv_user_regset[] = { .set = riscv_vr_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 9417309b7230..f60b2de66b1c 100644 --- a/include/uapi/linux/elf.h +++ b/include/uapi/linux/elf.h @@ -447,6 +447,7 @@ typedef struct elf64_shdr { #define NT_MIPS_MSA 0x802 /* MIPS SIMD registers */ #define NT_RISCV_CSR 0x900 /* RISC-V Control and Status Registers */ #define NT_RISCV_VECTOR 0x901 /* RISC-V vector registers */ +#define NT_RISCV_USER_CFI 0x902 /* 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 */