Received: by 2002:ab2:b82:0:b0:1f3:401:3cfb with SMTP id 2csp869181lqh; Thu, 28 Mar 2024 21:51:52 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXFIQ1xqEMRXioB3mdVsreWqhFlyAJ0SkWEfiI7EIfWcIYOxJnrwjMfsqDehb/AHW5u0cfOGeGHXPhYxysNWDV7mMR/Y9NIFITZ0WuYow== X-Google-Smtp-Source: AGHT+IGkh+d+HsEamPvMUDzn+h41dOdHZH/BSexcOwRbszKd91hGtP1oFMd5t/Mp3EKPhudBjYNM X-Received: by 2002:a0c:e6c5:0:b0:696:7ed9:9d8b with SMTP id l5-20020a0ce6c5000000b006967ed99d8bmr1137733qvn.2.1711687912758; Thu, 28 Mar 2024 21:51:52 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711687912; cv=pass; d=google.com; s=arc-20160816; b=XNjtLz1q6K3O9er18wXtJhHXRURUzrQefsimt+XD5DPDrexfw5jSmA2hflVNc7IGZC s6+XE2eU+utcSBvHHHxWqUBojA1Sm0ZVy+F2cGG6f0yAppxn9oLO14pQ+DuH8ZPNQo7T uXuyQYBwnC0+sHtS5I+5JpT7gKASbYl+noX68tIyX7oTSISmAj88hDOTXWgMcQoftV89 eHeC3yIqdNSRuMqhHV3WMgpj+e/GKo/dCDGCjHZEO8/jR/GYI1muXRNFgx+EeU8HjWdR bBJy9nU51rIW0j6gUme/njsxPMgpqILpqyhsrr3Pnp6Uf0TqjPxeoEDcENuMBiNmmnMB pFkw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=J4h29gLixoMV8jqguvN/5Pzgz4C0EFoOwStmiEzJSk4=; fh=Uhrch+bDp7qB2S80wDzG+sI4BpBVxsvVOqkzHp8S1c0=; b=caxlCZC8Z2xWyVFMTHAoJmZ1je50J478R5K4b9Y+Gnd87hQ8D+8kgtbXwFDJ2ZEI+4 Ac72WQ3K4vQlN70S8Ippm0L2tgonEDVcO3vuR9y+sGiib/D4MgBzyrtuWCqsBp1/21+b FOfkK4bPSvr/8ijmZCh/cORaGmtZrgEnZjxu2LMEHvT+x85TqdzebwZfyK3RsWBUm3u6 YonvYPaQNTh+Oxkt8TD3R2iL7KKZK+QevJHs6vUbSb8qx1oaGgf7o/cTp6RLGnvZrwiR gtlz2W6mGTjJ8R5l+SSKFXVPWF99XeHTl3/N2zbxfgUGnYz1NwHqB4W5r5ecYYNcRvuS BMvQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=tFVgbcRW; arc=pass (i=1 spf=pass spfdomain=rivosinc.com dkim=pass dkdomain=rivosinc-com.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-124140-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-124140-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id l12-20020a0ce08c000000b006968ed166b9si2931258qvk.468.2024.03.28.21.51.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 21:51:52 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-124140-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=tFVgbcRW; arc=pass (i=1 spf=pass spfdomain=rivosinc.com dkim=pass dkdomain=rivosinc-com.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-124140-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-124140-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 6F1661C2108E for ; Fri, 29 Mar 2024 04:51:52 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2CA9B6A333; Fri, 29 Mar 2024 04:47:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b="tFVgbcRW" Received: from mail-pf1-f180.google.com (mail-pf1-f180.google.com [209.85.210.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2CEB96A01B for ; Fri, 29 Mar 2024 04:46:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.180 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711687622; cv=none; b=q/5L3RmClc2gTBGOogva7GNiRdCMRUoL1h5St0YMNqE2kVL8GoV9cY1xdZQNJE8irvsDZVG5mWmAKsEJuTGRRqJpvYbeBiXV34ILNckghhfWeOT8nloPIPw/GoDCgI66spSTB8tVK1fR9TmSDbHQ3aOpj1twLkoQNQ6x8tfuwpU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711687622; c=relaxed/simple; bh=l/nyvaOgUV9mE95wDYkfkeHkD4SONVQazcZc1dFrpBI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=uVgNzXYCIUOg4+Y7j0EYvUStFvk7EgUFyUzhV6LEDVCvI7ZMetGZqOywUmNslTuvFyDKuq0fYPXybqP98drz1/muzxFZIVx0HkWc7tKpOYGcPqHW4CWIzRzNmqv/Pn8rPLyd0pJLSoU70+jqCPshL2gX41hkgPPDNh/hZtf7NuM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com; spf=pass smtp.mailfrom=rivosinc.com; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b=tFVgbcRW; arc=none smtp.client-ip=209.85.210.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rivosinc.com Received: by mail-pf1-f180.google.com with SMTP id d2e1a72fcca58-6eac64f2205so1472459b3a.2 for ; Thu, 28 Mar 2024 21:46:59 -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=vger.kernel.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=tFVgbcRWSQjYUNPPSPGC+vwied/AUdSVgiyuyYcBdkB0PF9aRdFnnP7742tbYteI4/ UOJswo5nsSh4twpnpW7Kf066njfhx+4oikkpiSx8bcmLAfjG6ZzBZHfmTTNBR8elsoym e/jUVprJSnuKWF9Te35IHagOp3lDOoNLqLVxHFVkAQWA0wYiBsD1nmUudktYhAkJSYM2 xeCT7hmF3hf0UJJUqGrrJ1E3kLDPhs3CgNWbtOxW0SmiQ6hsXVbJfQF3kw/iJPtu8cJp WQsTSFcZO7uuwYHSenT973iFZ0KVBQWhbBdhQHL0ilg0pCM4Yr3JCc/HRXnp1k8hIaP3 OBoQ== 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=lQmUV+cKyRzao3XVUP32RGTx2y4zvBBeq9YzOrynbSvvbupq5N2/Aadtg58+0GM4gm Fq3fu8nPlPWLgsMT5zV8WX/z6Zz2UJqsSeCx7CBhQZ+6HHwLksTDAs7g92IgXYv41PR8 VHZInTJ2B+zprZ1uiTVKyHtMtkOnj3gA+0DvYEpTTEOhDNoS2HzeJRHA17UVaHd1iPjw +8I3CiyftXPnoaM4sGjH6F65xt7Lgk+RqIiJQsQm6s6Hv6QbamUr1uY/y29autR20xfS 8XPdOA3ZwHAvCznx7bs6+ScQL8CZnrE/hevDSq8SxHem4ZS4Hw+JF8vm/SEkU1cD/biO RTXQ== X-Forwarded-Encrypted: i=1; AJvYcCVC1VBUgoToaEgwhv0EzwMYC9crAz4QYT5DA6yzIc40240W1LTO4uZdnl/AuIw1c3qiZXRuiORM/5WmeMHjYyCO8RBvT6EwDRBPBcbv X-Gm-Message-State: AOJu0YxX9DHWc2d0cpfNSgM44BmKF2caINFn6Xea09TsAFxbSb0snE01 MNH7mSEXTO9kP7UXdFBGqNj6Kxke56oHGIJj5gTo0Bkr+aNqLDRVeN6LJevfMdw= 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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 */ -- 2.43.2