From patchwork Fri Mar 29 04:44:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13610067 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 2D4C9C6FD1F for ; Fri, 29 Mar 2024 04:47:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B9F896B00BD; Fri, 29 Mar 2024 00:47:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B4CC26B00BF; Fri, 29 Mar 2024 00:47:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 929496B00BE; Fri, 29 Mar 2024 00:47:02 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 73DBB6B00BC for ; Fri, 29 Mar 2024 00:47:02 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 44BA9C10A7 for ; Fri, 29 Mar 2024 04:47:02 +0000 (UTC) X-FDA: 81948841884.27.4EB9968 Received: from mail-pf1-f170.google.com (mail-pf1-f170.google.com [209.85.210.170]) by imf04.hostedemail.com (Postfix) with ESMTP id 894024000E for ; Fri, 29 Mar 2024 04:47:00 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=F4XUy8q2; spf=pass (imf04.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.170 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711687620; a=rsa-sha256; cv=none; b=bhNEQNoGzhit1H60yrztNvN7M2ExB3K4EQ6VqQv+H/AiGGOjrNkhL95mkA1QGCl3bvGh+l 6z+v9dpx9nIjmYvGAYg7BpNoSmG8JGbzfMoPzVKjVWz+YblgNO2EnTny7+BAWeaAfQ0lOz jEoBwIcrcMy6ik56/OCkoZVN9sWxg7E= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=F4XUy8q2; spf=pass (imf04.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.170 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=1711687620; 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=UlDO9nZqwHfcx17dlOAhnOW/JNTcCs1MMqQDaWFjG5E2XZrHo66b5gvKqNw86L2/Zonaal kpVD9kAi5vKgdRHqJCGcugjsUWWnspVgyyazaSzFHJlsAWB3RTnNYLnhpVaxTC4z7JCAp9 ecgQdb6jNZEQEl582bVA5cF0W4qPo+I= Received: by mail-pf1-f170.google.com with SMTP id d2e1a72fcca58-6ea9a616cc4so1288271b3a.1 for ; Thu, 28 Mar 2024 21:47:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1711687619; x=1712292419; 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=F4XUy8q2UIoobryLwrpiJE+qZJkLxqAfcL5jrKZYxtV4O8Kteh5Ur03s34M2Auadu1 RfrnjLDtrboRak20/Cm6mIy7cdXBzX/KzPoUzfopwb4N7N6Z6gouL7TrKr5LHmIf7iDR xTKLesU9I+r9LQB8UkQ5W2UMBClnSy7j42eua4HKv/12I9Dc7NURx5HKYpOlS40G83Qq yCVbBZ1WTinsEorSEQoBEr45EPS6f6LABuHsvPrer/AhGP6omagTj0jm455Mjr32H96S tAng+r3HmwmCo3PHqwBSTegj1/rQ6NoFIxDc4uA94FBjccgLlMd3XoU5C9zBB/vcmmHT CFbw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711687619; x=1712292419; 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=MzUMzsW4N3nXNL089z+N17BojrCDAfAsrDXMh2Uc1mKfvqGyAhV6JTFYWWDamv+D/s uAF54PYx2slZEZRCa9Dawm7FchaZKPiTHzjxdTdZXCHLU8Z/ABGk2mZkZrNvqydgR7oo xLo3GbWYpusTfD2EtAksQykQmXjK9Q7bjw6enBa833vgFh8pL9zhf0NF1YyfzSlI+ina 143hMLRsUxAhugZQG5pOWuIESgehf0Aq2BPOu40P2dDWCXlKYTyxETxeGiIfLYBdleYM XokpDI6S0WkoNhIbIN/dgP2fnZCTv4mJmYncRDkK2Pz9MlTVrJZtJWBF5HkDyjQp4mXN WHtw== X-Forwarded-Encrypted: i=1; AJvYcCXX+KbRlfZ7DA/9sx67XBYQ56qdcWAIkzOunRMu4I2bJEHsjLKYPDx43PXjFvB7k3ZO3FBeRRHv06rv3E6Es21Wlys= X-Gm-Message-State: AOJu0Yy/GT1aBUvi+eFAUmPJSleW4MAO1sq6W0tCN+2mRqgF54qfxCAg 2kU9fEam1Vb6FkOzvalRoPYktln9tcBje6OX66Il8cIekPvU/FHro4lzygC/oLc= X-Google-Smtp-Source: AGHT+IF2Qlx2fW0K+tYyoJfMDwHqIC/6lXzl1sxupcwhY7+4lXjfHF7ZdfX+bDYaTDAXzjEckCXang== X-Received: by 2002:a05:6a00:1481:b0:6e6:9c79:87e9 with SMTP id v1-20020a056a00148100b006e69c7987e9mr1470495pfu.34.1711687619345; Thu, 28 Mar 2024 21:46:59 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id i18-20020aa78b52000000b006ea7e972947sm2217120pfd.130.2024.03.28.21.46.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 21:46:59 -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, palmer@sifive.com, conor@kernel.org, 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 Cc: corbet@lwn.net, tech-j-ext@lists.risc-v.org, 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 v2 23/27] riscv/ptrace: riscv cfi status and state via ptrace and in core files Date: Thu, 28 Mar 2024 21:44:55 -0700 Message-Id: <20240329044459.3990638-24-debug@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240329044459.3990638-1-debug@rivosinc.com> References: <20240329044459.3990638-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 894024000E X-Stat-Signature: 3a4izhf3wcbnwacgzjg9kaieawkszycc X-Rspam-User: X-HE-Tag: 1711687620-299052 X-HE-Meta: U2FsdGVkX18cshmmllsoNx6i5mU2smHmh1pYI+LF9fOr9AEOU86cu1eJ0s7XxnO7lgwtk8tEll8wmmnAsYQaSRL/Lqhn5CO83um8Pi7mLJbNFJs76+esTIMLM47MNDx6b2tKoqeMsJPIQDtimxwQCIatyBtf/l7oOfVH4T0GV/WjaRwfCa9mxRb6xEFva/2lVKgGxE1kMpFoGuWOb/xtS/GuwhjphXbvPPV3hxGOqzltARvp2PH0/bdqxl/B6+cj8H/zR977SIvcU6J3rfA50aajnhToPnRO3utLSwjF24mb90qNjdNy+Od0OoKvyOrGt05gmEWcip/d9CshRen9LFnTpIhHFGiPzvTuGFxcZpvhhiE696zYVDrA8uwgf+K4zyivBh+6Uq1wZUKSokVOy5B6egPeufziTlHETsNJ/vTy4A3cUs09OpdGXVSC2mRwr6+zMdabHqnAaThzUgaDtpdmqL2rxCDuIusYDmDtzdBwawib4B/yjzlitGKArWIQbeATcWEAzhD9vH1uxGFRFS4KXLhwPQdlZhRQHK1AaKcvF4o7uKmrCon7PMT6iDRl5zMwRssVWPutQ1lAOLmRJxQgHN7JbDhXAPb/SvPkQhaY7/ZVEZWS3rP4hjRDG8Cv8P2R9PqjZhSOC6Oxvu5bF+mcQgyLEfpJbQ4NjOocQ8myX4/vgGrID9QPSM/R9mHzoKdZ0jK281LQCEJleWgRPKAW//HhgH/qxl+YBySWfi/atH/5e58xa7FKoPnMEE2vAKhXzw+UEm3XhZJNIjOU9NdE99YGGNMCxzhqKjvF3B0iSGQjCZwQ90c86aDrLS3dozPuSK/tAq0rTH+rLSylwl6N8d5gxjbXaE6ldHMO8p7ogMKydeEdzXfdLFdLh31VCUYt2WPdBgxsxoCqK3G33seUkslfLf++y/JABIdkstf3100LopKoXnPN9W8F5p6S8JFRDOI2bE7UP7HCnv7 jZFHJNCb K2OirYQQfXy1BjY/Xqn9yaeaCv4MagTlzCs+oNftoLwAwAJpfRd/i9q+5rY2kjCah3GU+QvGI3ZXyzfdYPRzD1cLAwnFfBDfq/aPucNWZCs7rpI4mm9nTdx1mreUAWA34fY5q+dV4xzBFFX6k5jsfgE0jmvLwOmfixiCzJpMKAewD3WxW8NfDRv93icYiTXsbOS+mSEsgfU3d0NbcJ5EnlMvhFLMtiIKNR4dof6NFL/kRbjK917oGejmWSg== 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 */