From nobody Tue Feb 10 09:58:08 2026 Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) (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 E5E5C22CBCC for ; Mon, 10 Mar 2025 14:52:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741618357; cv=none; b=HOAv2E2Xs/EHpKtD/SKYMpyp8VOZyWAoDvHUvUCSlXEdsPV8hOz13mXGcS8z8j2mLR6hHlsu486KJ+K/7CiTndInFCFqSUly+XM4W07LHDlyZA2dXrtP4ZCuxYDQc0lM1CjypXpoAuEmwKhqlX8mfhLA8FQ1CgiaS/WoBA9pxj0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741618357; c=relaxed/simple; bh=fvZEXse498MAei+juRUqd4/R9FeoMw38NPBzNnBXz58=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=XeTAtr9oOZVFOAVZBiDLNRsiJRUHp4O4rCBCMMou3pPzCHXyTY0DU54jdczZ8heZx48yyGX2n6Hhs4hGy2enwmzA50xLjAI9niabZ9tzwXEqP2Bge4Dd0e4jdfz0l3coQ427MN/6uMXzzG4FocoIH6EbGFfX12MnOfA7uoRaHRA= 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=qT8njxvc; arc=none smtp.client-ip=209.85.214.181 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 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="qT8njxvc" Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-2234e4b079cso76854905ad.1 for ; Mon, 10 Mar 2025 07:52:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741618355; x=1742223155; darn=vger.kernel.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=9ADFXc53D/cc0VutYfr8tgUjvI5cmEZUhOUtSZhfaHM=; b=qT8njxvcFWywG74UVYmqzi+wSnsvfdw3Uv7Oqs6yTatlDi7wXNGpKuHM2kvPsw3ijb +l33qtxvqgiLvjXowL9sNa7A42FMbI/UPW3tQkupYq7mbR+MS0JpCMGW/EBBdm7O2NtF 4uUq2Pnn6gb+sykYVaGeSTx/Gjx4NYx8u/47/ZfVKQGhZuPGFVwSPBcIXkS+oanJceUh +2XFVVjZDm5szukXs626lde7nfrfjIkzt3NLhvXhtDNmmxIEzVnz20MUJMYy0S72UyVD XqwxHOE6mJp9sGmrVBLUuLONPoEQser0ZpgrgLfVLYMZjpo+M0aEMrRSLQ1grYcop58a WpLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741618355; x=1742223155; 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=9ADFXc53D/cc0VutYfr8tgUjvI5cmEZUhOUtSZhfaHM=; b=I2HEfD3D7RpvnALgszN6m85tH236PK45DkjOQXUAub8+S0e9lSvmQ6Aa0yP1wUt3N7 ZnWq714uEtkqk/8bmdAFxin0vgmZzMf1zpO3+8z37D/gzEyP3bMPDhCPlsDm3A15lcYf UaaHsJ3a/2Gkh6ttcpnQTFX/AZWMHOLuyFkR6fJofxUQ0CCSyiZy1MjXSn4K3ELL5/S7 ZACFRxpy9NBU663QtplCzm0NXlBbf4/1AirmAY5nWNMQ2KNpsRen30GRzWf9iE2Hz5HT AgkpHF5RC46ub14MEy7mh9sSev6WKhpHTfjciH4SR6Ojim8FXpoSoQX6thnyqNLOntZP SBrA== X-Gm-Message-State: AOJu0YwnMaYoba0/shefjJNLSyxbp47L2P0BCSRV5c1PTRm2yUC7EeTX ftlpS1M9Almc1TecL7lDQa+3uKcYSKn9+hWMR6l9A4QHZ2o59nykmFKBhJURKhw= X-Gm-Gg: ASbGncst+PGppOujf8NhepJ4+H9ToqwRSA/8c6I//gNddhT6bm/yrHMdEN8sqTWqjqW j47j8BB7vq/LE37Oxr8lW5kxS22Lu/2m+bd3jYZzjEIc1kXXR9XgmtWXYK+i9QwVt1KPpDGUHi0 pnEL+cnt9NVJ4Dze3GMTTw3IYwmCGNV2D5Disz1O1ETY8czyKR2wtWZ4MocLCTB6a1+CidSZ1MY DILtc9gFb/WuxuvOesnzzX4v7ijOFi8oV8qtw2Kme6c+315ohZGRwYnPjpEGhPJuAZjUiGatryi XvejAzJv6bLFz9o1rzGeXYUwXTlk2LMMYs9Ahox/XRHYCFRnjiHNQho= X-Google-Smtp-Source: AGHT+IGmALl7tE1vDAuhwzUIZXgg+Q9w22Wqj1GO54eJLmN8OylP52nRYF70/T1mCfLbdEp2H00RTw== X-Received: by 2002:a05:6a21:4606:b0:1f5:7df9:f147 with SMTP id adf61e73a8af0-1f57df9f49dmr7380018637.40.1741618355060; Mon, 10 Mar 2025 07:52:35 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-736d11d4600sm2890275b3a.116.2025.03.10.07.52.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 07:52:34 -0700 (PDT) From: Deepak Gupta Date: Mon, 10 Mar 2025 07:52:23 -0700 Subject: [PATCH v11 01/27] mm: VM_SHADOW_STACK definition for riscv Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250310-v5_user_cfi_series-v11-1-86b36cbfb910@rivosinc.com> References: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> In-Reply-To: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@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 , Jann Horn , Conor Dooley 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 VM_HIGH_ARCH_5 is used for riscv Signed-off-by: Deepak Gupta Reviewed-by: Zong Li --- include/linux/mm.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/include/linux/mm.h b/include/linux/mm.h index 7b1068ddcbb7..1ef231cbc8fe 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -378,6 +378,13 @@ extern unsigned int kobjsize(const void *objp); # define VM_SHADOW_STACK VM_HIGH_ARCH_6 #endif =20 +#if defined(CONFIG_RISCV_USER_CFI) +/* + * Following x86 and picking up the same bitpos. + */ +# define VM_SHADOW_STACK VM_HIGH_ARCH_5 +#endif + #ifndef VM_SHADOW_STACK # define VM_SHADOW_STACK VM_NONE #endif --=20 2.34.1 From nobody Tue Feb 10 09:58:08 2026 Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) (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 C4F7322CBE4 for ; Mon, 10 Mar 2025 14:52:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741618360; cv=none; b=YX1zDyFZ35Axeq3bF6UyvYdEmiOi1W4xV7Y9lI/JtUGUzi2x9TuBe7bXvGGbJ42yi68Hs0ezSGyFOW7ou+HO1qipxpMmrtXD5Iez//xs9SbCxzNyN645pB+xjkFuJWoETOugQgPSlA9g0xMmF8z/ReIGIoFSjLOMNT4d+pAV4Lo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741618360; c=relaxed/simple; bh=DjlSjyKZg5L1RJtFrw9Awsbe+InTSaQHdJt80sHp1pA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=cqIDq1xbaObeidKZLNRmmt7T5pXqP06HdBZqTbL8NmjWTIZRHLUrmanO0P8Sdqg8CJqvW0PaggYchgM8exMYVhA4cLko89IXWQcBic21m8WS2sWQm+qUrLkHGFi6UE2UomOOLBlzV8BB3UZUYsT0znscnkA8PNgqlRcZHLI9JbY= 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=fpB+ZLEG; arc=none smtp.client-ip=209.85.214.179 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 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="fpB+ZLEG" Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-224019ad9edso99084355ad.1 for ; Mon, 10 Mar 2025 07:52:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741618358; x=1742223158; darn=vger.kernel.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=kHEXIimjUTXAyC0fOzh8XAkym19Q72QcmDI6hc+cq4k=; b=fpB+ZLEGGOr4HB1489JPrDqL88yR4qlhADmJ5wM0+L0MyptC5pY5oC6/0Wxm5lFI20 F1wpaZLQpsV5dGmdI0JwDL8/6rhwvenzHT+/6E+ntF/4Hy7/AkPqjjYwxQ80BGfp8Bhx fYWDPd+QRQwPq/LVuWC0p4Os/OVHNnHVk7k1cOHtsDsdIm8OIVUdi0orf+oWwYi48xJS 0mWMd2QFO98Yk6JHt/hmWniv92TPVN51o4KC+0uorSmlU3UB6YJuKH6QBif9RRA/A1cx 8RAAkElNPNwwOG5AhaYIRyNPqC6lh38HLwqu9npFfAHL0cMJnp6gOuZ+NjyzntEe4nTF 4J6w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741618358; x=1742223158; 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=kHEXIimjUTXAyC0fOzh8XAkym19Q72QcmDI6hc+cq4k=; b=cCJRBzj9BCyQZH7+kjbEhrHC3KD4XER9zv3njuQChqjahwgi1I6F5Kb10d8n3Si/cr lDLNH7kdVvPW2mFaVg/Aw0ixuGBQwqHNglfMkpCbsr4uNFPmhyi2jO927o/HcPRsoxeD xxbtukT4gyfAPtxviTl93JDfdoENsg4Ma+AX4u9Gjff6WubOWcD8h+an3wF6XMsAntkN QgJmDkfN4LsQKmt9y5EPPIxOZJU0uQYCwIO4/HQ09c+ayrxa9Kt7xrGOchLi1VhqCNJZ 9AsvjikXYxqq+KN0oz45zVF022/KpWi0vECP5oVvbDWGfyfQsBQPNBrAOGq2FB7Dj21c 1gBg== X-Gm-Message-State: AOJu0YystTpzIqcgClpZHXgUvkBAMsE90qOqLBqXsWmucMeA9UiUNul2 YQHnfxr9vMLTucmdjnwZXaekv4oon1dd+E6wSoCX5r07c6r03IxiIy1EXxdkY/U= X-Gm-Gg: ASbGncuXsLHDRMjfHQ+FXwgfNV+7hPOXRsvpeXJJcyJncpyJUytVA6kBVsZ+kt0q7VP xLN53vSMRMu7VPxzCLbPCqpo3dsABtEY7nqbBaIzf6elw7VB4WHYnQrWvP9Y5yOCmQTi+GwRnls 0p8oOc/+UfBy/po/ncK/PsHiC4U7YzjKz9mVth0w1rTuaMLl78ywUkX7NEyFzxSZ3ZvAI55x9gc Ti1TXimm2vXguxyus/bjvnfzbM0o7KAhEWF4WsyQqxZZ/wt4mCKVSOV7er1CUoVxHFexiKXwYRP wjOw3Ytjn+SkM/fpvRIx//uwpyPo+/H8YleLpMecqUbsDZCOjcH9SkU= X-Google-Smtp-Source: AGHT+IHtkwbPi3YsMJ6A8ZQyEy1VJS3fRxtZo4fUkREMtAJmz9IifioPXZ2s22L3uz4hyDnWUTIX/g== X-Received: by 2002:a05:6a21:2d08:b0:1ee:d687:c39b with SMTP id adf61e73a8af0-1f544c37a63mr24132640637.7.1741618357489; Mon, 10 Mar 2025 07:52:37 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-736d11d4600sm2890275b3a.116.2025.03.10.07.52.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 07:52:37 -0700 (PDT) From: Deepak Gupta Date: Mon, 10 Mar 2025 07:52:24 -0700 Subject: [PATCH v11 02/27] dt-bindings: riscv: zicfilp and zicfiss in dt-bindings (extensions.yaml) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250310-v5_user_cfi_series-v11-2-86b36cbfb910@rivosinc.com> References: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> In-Reply-To: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@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 , Jann Horn , Conor Dooley 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 Make an entry for cfi extensions in extensions.yaml. Signed-off-by: Deepak Gupta Acked-by: Rob Herring (Arm) --- Documentation/devicetree/bindings/riscv/extensions.yaml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Documentation/devicetree/bindings/riscv/extensions.yaml b/Docu= mentation/devicetree/bindings/riscv/extensions.yaml index a63b994e0763..9b9024dbc8d2 100644 --- a/Documentation/devicetree/bindings/riscv/extensions.yaml +++ b/Documentation/devicetree/bindings/riscv/extensions.yaml @@ -426,6 +426,20 @@ properties: The standard Zicboz extension for cache-block zeroing as ratif= ied in commit 3dd606f ("Create cmobase-v1.0.pdf") of riscv-CMOs. =20 + - const: zicfilp + description: | + The standard Zicfilp extension for enforcing forward edge + control-flow integrity as ratified in commit 3f8e450 ("merge + pull request #227 from ved-rivos/0709") of riscv-cfi + github repo. + + - const: zicfiss + description: | + The standard Zicfiss extension for enforcing backward edge + control-flow integrity as ratified in commit 3f8e450 ("merge + pull request #227 from ved-rivos/0709") of riscv-cfi + github repo. + - const: zicntr description: The standard Zicntr extension for base counters and timers, as --=20 2.34.1 From nobody Tue Feb 10 09:58:08 2026 Received: from mail-pl1-f178.google.com (mail-pl1-f178.google.com [209.85.214.178]) (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 E022A22D4C6 for ; Mon, 10 Mar 2025 14:52:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741618362; cv=none; b=k5nOSMJFt2OUQjNgxb12Og+0wHBBrw3/r8qLRaAbBjubIvPC22woGqzUPVQvdT1fajq08qzbe04dKZb6zucYIkGQzzKkM9/7VyEUlc9sA0OhT/jplmgXfKsvCOk8Xw+3qIyESCHlQdui6l0e/NtWbeoqavUYbsY1PO3SwKAOs9Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741618362; c=relaxed/simple; bh=IL2TK1PHTDBgYiy66N142GrBjNXRZOjcIWW8bkBi80A=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=oM4jivLHmcbMTwoI8ms9CGUd8J3F/ZcJ7hSjcXFigbAfIhFtU2JTuPjzKv5nssOamkUZ8IvO+zjLawxR8NOa/d/XvqumnKzbLTYndBithUsf0hQ7k5++M2q4yR/VFGSeiz2UrEr+8Ifn3etDzEv0Ic7JTqsYxmwZUrpjfTRXPmI= 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=kVpphZAe; arc=none smtp.client-ip=209.85.214.178 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 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="kVpphZAe" Received: by mail-pl1-f178.google.com with SMTP id d9443c01a7336-2241053582dso69026015ad.1 for ; Mon, 10 Mar 2025 07:52:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741618360; x=1742223160; darn=vger.kernel.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=dbqILCM4sqAsqnZWR3n0haWRm4rdBukz3/+QEOiXjzw=; b=kVpphZAev7bqIGrkyCgCDSpCrJfFcDqQoamjyhxj3+qQepcJU6KLtXV9YDQ0NG/mMP I1HM+H7/ZdjRDeSBbEi1miuTk44mKc2eX52C6RrufXTAlCBLKuczRafLiDSm9bfJZNC7 +YOHAvAeanrHJrGGP7jbijHCjr7v+O+Wkj+hjY2St813OVac7Fl2TefRJQWmY5koWK+l 57riTjgfO+ugz1AdTP8tQDyeXIvfEQSBeEnCLz1pEJ1xb4HXeSNYfMRtCiXR+yNsKGpB DYhJ4CJiQ5NvamQzZSfMw3QrQoBiQesp0bjubDkrDmfBAgWRBJec/NYuMPmM2p+YGTe1 KM6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741618360; x=1742223160; 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=dbqILCM4sqAsqnZWR3n0haWRm4rdBukz3/+QEOiXjzw=; b=c6KpeS65In1EQxYmuldFPzE6Bgv/yhd53N3ifDQnN0L+cyFgzzAU+U+EDuMIUxvIsJ v+uaDBV48JihMN1N5h7LznJLHUNWND2EIWcA/uXEJRDH1izBV7ynwXINtA9vflfGPdt+ 2z/xbx6d9pb/kKYRl7/sk62n8SQLClvxz6CXZBAM996tOs7TccuFBYraQaWFofji249F fC82a5Jqxxjfev/IQgAHqN8PW7y1g2bR/zhGOYVPwJ6PtrCfaDtdtDhqAm8oMEmsxfHq iPFv/nVztQG1BWXd5yw7ah4I5SQO7XKPXnn0DDr2WNNBDw2EnmhZ/2qMDhQbKnH4N1IH eWlA== X-Gm-Message-State: AOJu0YzHAcZBraYvU+3RB6SXGACm/kDJK4qoyDt68UDgiB3uiYBM0Uu4 YGZIQR+cWExy45SaRXvSP9RerzEB38zHgkvZdTqWz/uU4nEHDIXak1DZlbktAj8= X-Gm-Gg: ASbGnctx0iwgTgAyIUqIEVq3yT8o/2zoos1CvrrnxLpdyCWbyFOtnK8iY2RPa4+0VgF Ho3jhAuBiP8Uvd5X7b3FoR6Zbr1QCv6LE4iD/jo8WzWz1VG6AMbMl5lnC5h8c81bdRP11iGNd6z SEk1lA5zWcvAezADUibNZgADwfB3tWE4He9frlt+TRAcge/3Df4hbi28SVNglePo1PS+1DMRHch yPoCPBSd7NmKb8CFVH0b7t6ynA9hEDO3kOwUKc7hlJz8V/zYxoXc9XcdQnsTM8x/oHu40ODlYCY RUh0qxEvGGgLQOj3RV07vAfPxkmk3XqbXLG8GtvJUTdm0H0thouR1sI= X-Google-Smtp-Source: AGHT+IGingfz3mCoLXCx4AWjWHsD50K0dzh2NK7v+KMvVV2K5wL6QO4M0vSkH0GvDvftQkvXnd4PuQ== X-Received: by 2002:a05:6a00:b4d:b0:730:8a0a:9f09 with SMTP id d2e1a72fcca58-736aaaaca0fmr22178030b3a.18.1741618359953; Mon, 10 Mar 2025 07:52:39 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-736d11d4600sm2890275b3a.116.2025.03.10.07.52.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 07:52:39 -0700 (PDT) From: Deepak Gupta Date: Mon, 10 Mar 2025 07:52:25 -0700 Subject: [PATCH v11 03/27] riscv: zicfiss / zicfilp enumeration Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250310-v5_user_cfi_series-v11-3-86b36cbfb910@rivosinc.com> References: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> In-Reply-To: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@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 , Jann Horn , Conor Dooley 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 This patch adds support for detecting zicfiss and zicfilp. zicfiss and zicfilp stands for unprivleged integer spec extension for shadow stack and branch tracking on indirect branches, respectively. This patch looks for zicfiss and zicfilp in device tree and accordinlgy lights up bit in cpu feature bitmap. Furthermore this patch adds detection utility functions to return whether shadow stack or landing pads are supported by cpu. Signed-off-by: Deepak Gupta Reviewed-by: Zong Li --- arch/riscv/include/asm/cpufeature.h | 13 +++++++++++++ arch/riscv/include/asm/hwcap.h | 2 ++ arch/riscv/include/asm/processor.h | 1 + arch/riscv/kernel/cpufeature.c | 13 +++++++++++++ 4 files changed, 29 insertions(+) diff --git a/arch/riscv/include/asm/cpufeature.h b/arch/riscv/include/asm/c= pufeature.h index 569140d6e639..69007b8100ca 100644 --- a/arch/riscv/include/asm/cpufeature.h +++ b/arch/riscv/include/asm/cpufeature.h @@ -12,6 +12,7 @@ #include #include #include +#include #include #include =20 @@ -137,4 +138,16 @@ static __always_inline bool riscv_cpu_has_extension_un= likely(int cpu, const unsi return __riscv_isa_extension_available(hart_isa[cpu].isa, ext); } =20 +static inline bool cpu_supports_shadow_stack(void) +{ + return (IS_ENABLED(CONFIG_RISCV_USER_CFI) && + riscv_cpu_has_extension_unlikely(smp_processor_id(), RISCV_ISA_EXT_ZICFI= SS)); +} + +static inline bool cpu_supports_indirect_br_lp_instr(void) +{ + return (IS_ENABLED(CONFIG_RISCV_USER_CFI) && + riscv_cpu_has_extension_unlikely(smp_processor_id(), RISCV_ISA_EXT_ZICFI= LP)); +} + #endif diff --git a/arch/riscv/include/asm/hwcap.h b/arch/riscv/include/asm/hwcap.h index 869da082252a..2dc4232bdb3e 100644 --- a/arch/riscv/include/asm/hwcap.h +++ b/arch/riscv/include/asm/hwcap.h @@ -100,6 +100,8 @@ #define RISCV_ISA_EXT_ZICCRSE 91 #define RISCV_ISA_EXT_SVADE 92 #define RISCV_ISA_EXT_SVADU 93 +#define RISCV_ISA_EXT_ZICFILP 94 +#define RISCV_ISA_EXT_ZICFISS 95 =20 #define RISCV_ISA_EXT_XLINUXENVCFG 127 =20 diff --git a/arch/riscv/include/asm/processor.h b/arch/riscv/include/asm/pr= ocessor.h index 5f56eb9d114a..e3aba3336e63 100644 --- a/arch/riscv/include/asm/processor.h +++ b/arch/riscv/include/asm/processor.h @@ -13,6 +13,7 @@ #include =20 #include +#include =20 #define arch_get_mmap_end(addr, len, flags) \ ({ \ diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c index c6ba750536c3..82065cc55822 100644 --- a/arch/riscv/kernel/cpufeature.c +++ b/arch/riscv/kernel/cpufeature.c @@ -150,6 +150,15 @@ static int riscv_ext_svadu_validate(const struct riscv= _isa_ext_data *data, return 0; } =20 +static int riscv_cfi_validate(const struct riscv_isa_ext_data *data, + const unsigned long *isa_bitmap) +{ + if (!IS_ENABLED(CONFIG_RISCV_USER_CFI)) + return -EINVAL; + + return 0; +} + static const unsigned int riscv_zk_bundled_exts[] =3D { RISCV_ISA_EXT_ZBKB, RISCV_ISA_EXT_ZBKC, @@ -333,6 +342,10 @@ const struct riscv_isa_ext_data riscv_isa_ext[] =3D { __RISCV_ISA_EXT_SUPERSET_VALIDATE(zicboz, RISCV_ISA_EXT_ZICBOZ, riscv_xli= nuxenvcfg_exts, riscv_ext_zicboz_validate), __RISCV_ISA_EXT_DATA(ziccrse, RISCV_ISA_EXT_ZICCRSE), + __RISCV_ISA_EXT_SUPERSET_VALIDATE(zicfilp, RISCV_ISA_EXT_ZICFILP, riscv_x= linuxenvcfg_exts, + riscv_cfi_validate), + __RISCV_ISA_EXT_SUPERSET_VALIDATE(zicfiss, RISCV_ISA_EXT_ZICFISS, riscv_x= linuxenvcfg_exts, + riscv_cfi_validate), __RISCV_ISA_EXT_DATA(zicntr, RISCV_ISA_EXT_ZICNTR), __RISCV_ISA_EXT_DATA(zicond, RISCV_ISA_EXT_ZICOND), __RISCV_ISA_EXT_DATA(zicsr, RISCV_ISA_EXT_ZICSR), --=20 2.34.1 From nobody Tue Feb 10 09:58:08 2026 Received: from mail-pl1-f176.google.com (mail-pl1-f176.google.com [209.85.214.176]) (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 090A022D7B3 for ; Mon, 10 Mar 2025 14:52:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741618364; cv=none; b=STWIxeShYE+CcCGtyqTaBYclqGnvIxJdJo7xKy2CNyb1DA8e6NBWp8sQDP75T7hz/RF6wE2CBKI0pdk60tUtP7acNmMceYt3U14NG/BlZLbjYWRUgIA0OJwnq/z23jlIDIl824oJH5XGEjFGbWftv303ZUcMisMYakspejQ4Tpw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741618364; c=relaxed/simple; bh=f+s1qRILk5w2zoTzbSGEFuSk9CM1mNXrrF3BbhojAEc=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=mPZU43x3zdBpHGjzqBfKJUO+SYgvQCrn3JQaiDMQGRhQpitj14J9yl90WeKR4XUbrWObV37/czx0euvj4CvS4WGdiaoe5BzlIML68+UyM+8wqa77G6p1UZh6zArTMopBhEw2jdQjyjtqzSlBCwCtZqzV3yUQm7lrEnG2Q6uVMNg= 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=EydyJ0Bp; arc=none smtp.client-ip=209.85.214.176 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 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="EydyJ0Bp" Received: by mail-pl1-f176.google.com with SMTP id d9443c01a7336-22355618fd9so78425115ad.3 for ; Mon, 10 Mar 2025 07:52:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741618362; x=1742223162; darn=vger.kernel.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=n3DM8zy+WllPP0TujEXFZ0Ycc0zGrpNRrMRWPkPmGZc=; b=EydyJ0BpriCvs2qPsub39795edQPTmKCu8O/B5RPJ2VjhMQbBwGyf6jq0uvq53HTCW WRaxFAFaGjW4k0qSvC2Yjp0AnrYP3DQmtvZo1gcp0sH43DC3wwlYj33e/XhfKTlrrSkj yRxeSqP5c98EfTLk9y7Or+5sq4xzgJebDPwitoM3VRx3tCUYbaxqrEjf85/2OAXR1rQp z+v4zwakubW62pee6309vt/IY07dxbb9dJ0h+ooMzyKB3NlVlHVcf61RTyOsSLkqB38c TbWLbveGzTPnj6b+3VOKwf0UwCG3+uHNrvdXnbzMuyr3B3Y+dn52A5RigiexhFCFhWRE GEWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741618362; x=1742223162; 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=n3DM8zy+WllPP0TujEXFZ0Ycc0zGrpNRrMRWPkPmGZc=; b=pDCoMCWk1GJKW4FRlJPknq19KjwQHWqkmQX4zKykYepWbysKjWsa9IXF1U8cOPQ0+3 fRnN7ILGaRNnFgIRFZnhwte7YBd+CrRz4OQZotHzyDPxRT0Ky9Iplg76Zcx1Mm4liJUT f16Pu8jbIRxAgW/w7VKhT578HbBEHUNMHtj/vkoa3mSulNPGYYjxgrW+WuQ+mXDJ3l81 e+2LH364JAHXpxrpKMvyr4aO1Lmees4hd4jEps01lKhJ6GqBGdV9zxqu9McFXyDBcNkz 4MLGOp3fIfKh6A5guK3b91Vsx4VaqViaWJXKM1ruFdqo21xAjcPgv4sCwQ/XYbCMtFoC 5yrw== X-Gm-Message-State: AOJu0Yw1Rdg08EXdV/R+GTgoTQjDmVwTJriVxK7PmujjsRuN6c3J1KVf qvV4hOxLazBV4i/T5YhepNbI8C+GTY3J+KN1NW4YJd76Tz3siR9ACbzo6+9dEEE= X-Gm-Gg: ASbGncuGde72jGxNOJuhEhqNomasCu8ro2d7Rb3D2oAVWz0qjG+vtrUJjI0wcaIWaQJ E2Fm1L+cN9lIW1gpO4Jtm+uuNiNafknv9++tBJaUfZlBJYZRXso7esD0lfS9HtQPNMWBgGXhHee h6BTyhKDX5LqPS6fYH5AfX/5Z40qGowwXk1FloIofd/TNtEF+wI/ZlcXWx8s9AFtxBv/SwPwQB3 VJIEiyB2F5Fhd2wZMrMNmULe/qOE1MhpOjSQ3APfmEku+CGLz1XedMFrl1HkFUIoDeOeNRVeHMO 4I72IvQ/kaZ/CRjHIHxiXDwDLY7IkEnE5ZerOXUj8a5HJI8C0DXeCpw= X-Google-Smtp-Source: AGHT+IHEgbV38+kdB/KuKTC/ngiL6naw+4KBErVaQq0wSwONUo1ce4xmBDTiHE9RxDA/IFBRpdqZjA== X-Received: by 2002:a05:6a21:1fc5:b0:1f5:6878:1a43 with SMTP id adf61e73a8af0-1f58cb239e0mr144294637.14.1741618362407; Mon, 10 Mar 2025 07:52:42 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-736d11d4600sm2890275b3a.116.2025.03.10.07.52.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 07:52:42 -0700 (PDT) From: Deepak Gupta Date: Mon, 10 Mar 2025 07:52:26 -0700 Subject: [PATCH v11 04/27] riscv: zicfiss / zicfilp extension csr and bit definitions Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250310-v5_user_cfi_series-v11-4-86b36cbfb910@rivosinc.com> References: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> In-Reply-To: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@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 , Jann Horn , Conor Dooley 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 zicfiss and zicfilp extension gets enabled via b3 and b2 in *envcfg CSR. menvcfg controls enabling for S/HS mode. henvcfg control enabling for VS while senvcfg controls enabling for U/VU mode. zicfilp extension extends *status CSR to hold `expected landing pad` bit. A trap or interrupt can occur between an indirect jmp/call and target instr. `expected landing pad` bit from CPU is recorded into xstatus CSR so that when supervisor performs xret, `expected landing pad` state of CPU can be restored. zicfiss adds one new CSR - CSR_SSP: CSR_SSP contains current shadow stack pointer. Signed-off-by: Deepak Gupta Reviewed-by: Charlie Jenkins --- arch/riscv/include/asm/csr.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/arch/riscv/include/asm/csr.h b/arch/riscv/include/asm/csr.h index 6fed42e37705..2f49b9663640 100644 --- a/arch/riscv/include/asm/csr.h +++ b/arch/riscv/include/asm/csr.h @@ -18,6 +18,15 @@ #define SR_MPP _AC(0x00001800, UL) /* Previously Machine */ #define SR_SUM _AC(0x00040000, UL) /* Supervisor User Memory Access */ =20 +/* zicfilp landing pad status bit */ +#define SR_SPELP _AC(0x00800000, UL) +#define SR_MPELP _AC(0x020000000000, UL) +#ifdef CONFIG_RISCV_M_MODE +#define SR_ELP SR_MPELP +#else +#define SR_ELP SR_SPELP +#endif + #define SR_FS _AC(0x00006000, UL) /* Floating-point Status */ #define SR_FS_OFF _AC(0x00000000, UL) #define SR_FS_INITIAL _AC(0x00002000, UL) @@ -212,6 +221,8 @@ #define ENVCFG_PMM_PMLEN_16 (_AC(0x3, ULL) << 32) #define ENVCFG_CBZE (_AC(1, UL) << 7) #define ENVCFG_CBCFE (_AC(1, UL) << 6) +#define ENVCFG_LPE (_AC(1, UL) << 2) +#define ENVCFG_SSE (_AC(1, UL) << 3) #define ENVCFG_CBIE_SHIFT 4 #define ENVCFG_CBIE (_AC(0x3, UL) << ENVCFG_CBIE_SHIFT) #define ENVCFG_CBIE_ILL _AC(0x0, UL) @@ -230,6 +241,11 @@ #define SMSTATEEN0_HSENVCFG (_ULL(1) << SMSTATEEN0_HSENVCFG_SHIFT) #define SMSTATEEN0_SSTATEEN0_SHIFT 63 #define SMSTATEEN0_SSTATEEN0 (_ULL(1) << SMSTATEEN0_SSTATEEN0_SHIFT) +/* + * zicfiss user mode csr + * CSR_SSP holds current shadow stack pointer. + */ +#define CSR_SSP 0x011 =20 /* mseccfg bits */ #define MSECCFG_PMM ENVCFG_PMM --=20 2.34.1 From nobody Tue Feb 10 09:58:08 2026 Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.169]) (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 1832822F384 for ; Mon, 10 Mar 2025 14:52:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741618368; cv=none; b=tmLNDsn8+w/bUtAvjQVOTABF79H7rlsMBulhHZGdVry33oGc5iGDDNAjMJvzwoRVmjANgiyoiVgX0z3RgrQShSTIckYX0zie/MJuyvzVwIpDHWQaPCr8wUcEwf2qp2wvB5QY/k4eVloRw3w5kRQrz9bDwJGo/z1hPsKJr8kQsdA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741618368; c=relaxed/simple; bh=wqi2P2G1FvVMl224SWtklzWEgyidrNAsFMS/1YKFkVQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=T1dcH9m1XA2FwbmPc50wWa9utW9CMkjlhHc2SJg0lr4JY95T23FykVct8bakZed1o1+S7cvnCdr7bqG6gNnwhAKhJ1RrUhtBU73zkgBlGSQ9AS70UFlTlFks/Awcq14lPQqiM4e+hEouk0Lyqq/sLJJLhEkAslvhKI/2f8Q+ICk= 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=z0pyuVN8; arc=none smtp.client-ip=209.85.214.169 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 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="z0pyuVN8" Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-22548a28d0cso11712795ad.3 for ; Mon, 10 Mar 2025 07:52:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741618366; x=1742223166; darn=vger.kernel.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=XFoiG9nO9GzRbM78aaWwJK+Q/U0QRSToN08KNluZphY=; b=z0pyuVN8NmlKrhpMAKg5OpKURybbe/JfJT2SA/+0X/X8mH8EW+lcXARMRWOgSr5RSg Zz7tA8dkcoFnvMce089uE417cvP1EZjGnjoctzTJbg3HmNBWVFFV3dR8gHqc/664VNMb nQHNmGcopPQ7RvGVlazYQI5hoakKoyRyJWJjM3WpRg6SoMwH9qRIyTNmeUkqr8hgTKmo o+eXchaw2NpgHMvbu9f5QpVOjqQg+p0whQMCfY+t5y1kzV2K6du22+Iz1HnP+kX+DGOi 9ALmU9SKj7Ed+pXIk6bCZXgdajYJYr3gmM3ZNuiR65ddUW3dUPwoFiPED6vdmI9NeNV+ u4Rw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741618366; x=1742223166; 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=XFoiG9nO9GzRbM78aaWwJK+Q/U0QRSToN08KNluZphY=; b=wEcM1mgs6C1xCjABBQpVFYN5ZX3UAX6JKlMilfhtK0zoZS3CO3VT1QDLyYNVDpTVXp GbMVMpiPnlxrS0qNqOUVyOAijDnVDBo/PBspc5ku7XRayEph6LU0lPXhuOCBU1Jy5Pwd xAToVa/8IzXkIh9uZOoSlwICdOTm0AmIhA7W7+wkeqs8OXmCKT3ahD4vrjWbJVPA3/1n Er5lDd4MDRFNkRLSfPN3G61f3BEDKO5ha+wtf46beq4Qz/zBVFu0x8ZEIM4lVElklXrw uRfwgrg9zkYt1uLUkaLSzkkOn60IVdP+16F5XHPxIztZDMRG58/NW+FIB1CczrxnhugC tH6Q== X-Gm-Message-State: AOJu0YyfL9zK3lC5WkLHIJQtTvjImnjD7pIEFKcHzlAlXlkSz9eFYiQD 0eur16NQLkbOsNEKn/dqhRZAv9Z83nuE/z+OlA0synhbg8qmhmubDuOeUj3vtkM= X-Gm-Gg: ASbGncs+XlmQQ95FJIicQh4MK4YTIdfezfIAHLRYG1yr5fYIakVGc8Z6RHwSGMNwDu7 7Z617IgdyAwZGZrck9q10LYcq7RWt7iX9sTSVXcAcRtNBdeYW3WgpSv+z23GP6/ghHKH2yzE4P2 1CMzm85tTBqlpCRYB7hp73D9wTnK2EmUBd3Rmj4E7J5+wIYv95pdh+GcoBxfrfy4d0vEDEomV5J NhmekoEInGq8rlBrS3Nlb7DRnN7WsmVS3Y0T1PMHQicB574g/A8XjLBILcgiaLomLDjEpbJJNd1 o+ZJ8ogO5R55e56Z2/k5vDl6834vOLW+Dqxcg4Gu8RkaxjojeLTBD6o= X-Google-Smtp-Source: AGHT+IFBz3aXVDVmIlmZqzUHJQr+CufocIiZAA2+T986OpEaME/vCYGeegLtAo9744J8E/xHkN/qMg== X-Received: by 2002:a05:6a00:928b:b0:731:737c:3224 with SMTP id d2e1a72fcca58-736aaa1e02emr19336418b3a.10.1741618364845; Mon, 10 Mar 2025 07:52:44 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-736d11d4600sm2890275b3a.116.2025.03.10.07.52.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 07:52:44 -0700 (PDT) From: Deepak Gupta Date: Mon, 10 Mar 2025 07:52:27 -0700 Subject: [PATCH v11 05/27] riscv: usercfi state for task and save/restore of CSR_SSP on trap entry/exit Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250310-v5_user_cfi_series-v11-5-86b36cbfb910@rivosinc.com> References: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> In-Reply-To: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@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 , Jann Horn , Conor Dooley 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 Carves out space in arch specific thread struct for cfi status and shadow stack in usermode on riscv. This patch does following - defines a new structure cfi_status with status bit for cfi feature - defines shadow stack pointer, base and size in cfi_status structure - defines offsets to new member fields in thread in asm-offsets.c - Saves and restore shadow stack pointer on trap entry (U --> S) and exit (S --> U) Shadow stack save/restore is gated on feature availiblity and implemented using alternative. CSR can be context switched in `switch_to` as well but soon as kernel shadow stack support gets rolled in, shadow stack pointer will need to be switched at trap entry/exit point (much like `sp`). It can be argued that kernel using shadow stack deployment scenario may not be as prevalant as user mode using this feature. But even if there is some minimal deployment of kernel shadow stack, that means that it needs to be supported. And thus save/restore of shadow stack pointer in entry.S instead of in `switch_to.h`. Signed-off-by: Deepak Gupta Reviewed-by: Charlie Jenkins Reviewed-by: Zong Li --- arch/riscv/include/asm/processor.h | 1 + arch/riscv/include/asm/thread_info.h | 3 +++ arch/riscv/include/asm/usercfi.h | 24 ++++++++++++++++++++++++ arch/riscv/kernel/asm-offsets.c | 4 ++++ arch/riscv/kernel/entry.S | 26 ++++++++++++++++++++++++++ 5 files changed, 58 insertions(+) diff --git a/arch/riscv/include/asm/processor.h b/arch/riscv/include/asm/pr= ocessor.h index e3aba3336e63..d851bb5c6da0 100644 --- a/arch/riscv/include/asm/processor.h +++ b/arch/riscv/include/asm/processor.h @@ -14,6 +14,7 @@ =20 #include #include +#include =20 #define arch_get_mmap_end(addr, len, flags) \ ({ \ diff --git a/arch/riscv/include/asm/thread_info.h b/arch/riscv/include/asm/= thread_info.h index f5916a70879a..a0cfe00c2ca6 100644 --- a/arch/riscv/include/asm/thread_info.h +++ b/arch/riscv/include/asm/thread_info.h @@ -62,6 +62,9 @@ struct thread_info { long user_sp; /* User stack pointer */ int cpu; unsigned long syscall_work; /* SYSCALL_WORK_ flags */ +#ifdef CONFIG_RISCV_USER_CFI + struct cfi_status user_cfi_state; +#endif #ifdef CONFIG_SHADOW_CALL_STACK void *scs_base; void *scs_sp; diff --git a/arch/riscv/include/asm/usercfi.h b/arch/riscv/include/asm/user= cfi.h new file mode 100644 index 000000000000..5f2027c51917 --- /dev/null +++ b/arch/riscv/include/asm/usercfi.h @@ -0,0 +1,24 @@ +/* SPDX-License-Identifier: GPL-2.0 + * Copyright (C) 2024 Rivos, Inc. + * Deepak Gupta + */ +#ifndef _ASM_RISCV_USERCFI_H +#define _ASM_RISCV_USERCFI_H + +#ifndef __ASSEMBLY__ +#include + +#ifdef CONFIG_RISCV_USER_CFI +struct cfi_status { + unsigned long ubcfi_en : 1; /* Enable for backward cfi. */ + unsigned long rsvd : ((sizeof(unsigned long) * 8) - 1); + unsigned long user_shdw_stk; /* Current user shadow stack pointer */ + unsigned long shdw_stk_base; /* Base address of shadow stack */ + unsigned long shdw_stk_size; /* size of shadow stack */ +}; + +#endif /* CONFIG_RISCV_USER_CFI */ + +#endif /* __ASSEMBLY__ */ + +#endif /* _ASM_RISCV_USERCFI_H */ diff --git a/arch/riscv/kernel/asm-offsets.c b/arch/riscv/kernel/asm-offset= s.c index e89455a6a0e5..0c188aaf3925 100644 --- a/arch/riscv/kernel/asm-offsets.c +++ b/arch/riscv/kernel/asm-offsets.c @@ -50,6 +50,10 @@ void asm_offsets(void) #endif =20 OFFSET(TASK_TI_CPU_NUM, task_struct, thread_info.cpu); +#ifdef CONFIG_RISCV_USER_CFI + OFFSET(TASK_TI_CFI_STATUS, task_struct, thread_info.user_cfi_state); + OFFSET(TASK_TI_USER_SSP, task_struct, thread_info.user_cfi_state.user_shd= w_stk); +#endif OFFSET(TASK_THREAD_F0, task_struct, thread.fstate.f[0]); OFFSET(TASK_THREAD_F1, task_struct, thread.fstate.f[1]); OFFSET(TASK_THREAD_F2, task_struct, thread.fstate.f[2]); diff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S index 33a5a9f2a0d4..68c99124ea55 100644 --- a/arch/riscv/kernel/entry.S +++ b/arch/riscv/kernel/entry.S @@ -147,6 +147,20 @@ SYM_CODE_START(handle_exception) =20 REG_L s0, TASK_TI_USER_SP(tp) csrrc s1, CSR_STATUS, t0 + /* + * If previous mode was U, capture shadow stack pointer and save it away + * Zero CSR_SSP at the same time for sanitization. + */ + ALTERNATIVE("nop; nop; nop; nop", + __stringify( \ + andi s2, s1, SR_SPP; \ + bnez s2, skip_ssp_save; \ + csrrw s2, CSR_SSP, x0; \ + REG_S s2, TASK_TI_USER_SSP(tp); \ + skip_ssp_save:), + 0, + RISCV_ISA_EXT_ZICFISS, + CONFIG_RISCV_USER_CFI) csrr s2, CSR_EPC csrr s3, CSR_TVAL csrr s4, CSR_CAUSE @@ -236,6 +250,18 @@ SYM_CODE_START_NOALIGN(ret_from_exception) * structures again. */ csrw CSR_SCRATCH, tp + + /* + * Going back to U mode, restore shadow stack pointer + */ + ALTERNATIVE("nop; nop", + __stringify( \ + REG_L s3, TASK_TI_USER_SSP(tp); \ + csrw CSR_SSP, s3), + 0, + RISCV_ISA_EXT_ZICFISS, + CONFIG_RISCV_USER_CFI) + 1: #ifdef CONFIG_RISCV_ISA_V_PREEMPTIVE move a0, sp --=20 2.34.1 From nobody Tue Feb 10 09:58:08 2026 Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) (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 EC8D722F397 for ; Mon, 10 Mar 2025 14:52:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741618370; cv=none; b=Xpq32oLllGXz/fLA4MWh1Th7AF9OXNZgFOk7G7DZHhWc7UF4qv+L80jLPXBfPUSYTIGu0+Wg19Vjrjd6BZLDc2W2wXXVcreyp+qcHm9AKRLNM94u8RFH5TWF1z29IZLG4su0moV6uua8czMv6Hvb9p80Tr8heico5FJPbRDBIw8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741618370; c=relaxed/simple; bh=toOY3g5UWbhyzFvpokgMbtCDrGwGLK+0MjdImo2Zc6U=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=DabaMDGbQJCuN99T7qvNQeXWWMCVsHFufc9CU5FkGv22LeD6Z0RbCc+MMmwf2rJ/seh5k8WZBKMf7Y/dFRDa4649W46A5i2faQZd8TwGmqz5/5PIf8VDNQD+xrErcXKJxcukoq3H+M52TgRy3KqF8+arEsGgxCqdyQ+IyHX+9U4= 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=rtfYbo3Z; arc=none smtp.client-ip=209.85.214.179 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 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="rtfYbo3Z" Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-22334203781so95781235ad.0 for ; Mon, 10 Mar 2025 07:52:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741618367; x=1742223167; darn=vger.kernel.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=1e5RgGuqZS+AKw5oTSgp47pWFJ0GDjWFc7jT6Xd5TJc=; b=rtfYbo3ZMjS4PdVSBQWDC5p1T/pOIaBfq2YNoEcmO+YC3yhQju2V7Wv5bK3jDzEvg7 +01FCieRoO3iF3j6f2LHLI0vWsHhLx2KvLmAdbP92ODPXlinr7YWaA552N+Q/5+TAPpa FBMoqoXB0nsWfPPuMsTJy5K8a27BladbspWBE0hxSAZM1Qc7l+qhBs74elmn1DjtjIWU XuKNKYd9W/n+9wROB6XefLQn5ZxAN7bJ9tL1P0qFZala1O3/np9QFBOsbkZoE28ZrFZl s5VYRl1O5pGDjY80G7asnJZoWi0/M7tKewGDOElmXBC9mz6H609keBZc2yy/1MoRkCUh 7ViQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741618367; x=1742223167; 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=1e5RgGuqZS+AKw5oTSgp47pWFJ0GDjWFc7jT6Xd5TJc=; b=c+QoEiVvKZI549D2uugaF1Jjb3dUJ6Y54UkYlklDe886W962W5UipQpXHhZfB9mpRp vWF+W1jJgBRYf5J967uvk86JCln+dVGRJHRxPiKNcGekegHg6vfkDWIEZGBMO2ONzviK ALzpqYfiPey1/QXAwRS20A/oWWuKdtysD7ddobhmS8gNAu7q6VeU6AhJOM7VNWsxcNFE h9n3Smorhpvc1STXUqOmXvKETWrdKh5bqvSnAFMKsyEP/P5f3Gkvxttr7K8sgs9zYTJ6 jZa4HxxU/aIdt9H3wgOMode8tz0JShzRxoCS6PkTHPaCfB0wyP87/HJeZKFmS0EUbaYC WBSg== X-Gm-Message-State: AOJu0YwNBrbiuBm9gtYrJRhn47exwaRSdqYjKhyWNRsWZx/iAPx3q+SO e35k2EqfWXPFK4gtAR2s0Fe0IV1vuOkcMN6K9F5vWQPKpVbYDjBYv+oJsPIGhCk= X-Gm-Gg: ASbGncsCuBAjCdbkMW4ttarmK19+bGloWelOgXR4Xsav4o/FAYZ+Il/18drSFNphSLm 2HRhahBuMy8VJT7gsxfgOaubmJH6XT12o3DZ/dXlro+Ikb1jqqMNzNoHL3zb4h1bpxq2VlUDlrb yXj+9Xp6pbtGvAgWVOA26QDWm2Ee3tlLE/8anhlBN/X84Xy2bPjDy+2cYFRI/rBPiO6+8+bsyPo 1FzFcKF4kPbu/SEl0OfvQlEK2tcNyEAzXg74jBHGBpD3ar9zpvmc3j4SZjmzPXqLYRr+9uHDaHc jsagkM2lwbmuCZzLFxTyYqw7yuy75zA+mWv2/kmBwbDZbZ0rtW7Au8M= X-Google-Smtp-Source: AGHT+IGncZQFemH+Tqb/7Cg0gaGakZGYufPeZUJHTPz51xcsHifa9ayQ0v/V99iUl8be0Qgt8Ur5MQ== X-Received: by 2002:a05:6a00:8506:b0:727:39a4:30cc with SMTP id d2e1a72fcca58-736bbf433d9mr12618993b3a.1.1741618367246; Mon, 10 Mar 2025 07:52:47 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-736d11d4600sm2890275b3a.116.2025.03.10.07.52.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 07:52:46 -0700 (PDT) From: Deepak Gupta Date: Mon, 10 Mar 2025 07:52:28 -0700 Subject: [PATCH v11 06/27] riscv/mm : ensure PROT_WRITE leads to VM_READ | VM_WRITE Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250310-v5_user_cfi_series-v11-6-86b36cbfb910@rivosinc.com> References: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> In-Reply-To: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@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 , Jann Horn , Conor Dooley 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 `arch_calc_vm_prot_bits` is implemented on risc-v to return VM_READ | VM_WRITE if PROT_WRITE is specified. Similarly `riscv_sys_mmap` is updated to convert all incoming PROT_WRITE to (PROT_WRITE | PROT_READ). This is to make sure that any existing apps using PROT_WRITE still work. Earlier `protection_map[VM_WRITE]` used to pick read-write PTE encodings. Now `protection_map[VM_WRITE]` will always pick PAGE_SHADOWSTACK PTE encodings for shadow stack. Above changes ensure that existing apps continue to work because underneath kernel will be picking `protection_map[VM_WRITE|VM_READ]` PTE encodings. Signed-off-by: Deepak Gupta Reviewed-by: Zong Li --- arch/riscv/include/asm/mman.h | 25 +++++++++++++++++++++++++ arch/riscv/include/asm/pgtable.h | 1 + arch/riscv/kernel/sys_riscv.c | 10 ++++++++++ arch/riscv/mm/init.c | 2 +- 4 files changed, 37 insertions(+), 1 deletion(-) diff --git a/arch/riscv/include/asm/mman.h b/arch/riscv/include/asm/mman.h new file mode 100644 index 000000000000..392c9c2d2e78 --- /dev/null +++ b/arch/riscv/include/asm/mman.h @@ -0,0 +1,25 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __ASM_MMAN_H__ +#define __ASM_MMAN_H__ + +#include +#include +#include + +static inline unsigned long arch_calc_vm_prot_bits(unsigned long prot, + unsigned long pkey __always_unused) +{ + unsigned long ret =3D 0; + + /* + * If PROT_WRITE was specified, force it to VM_READ | VM_WRITE. + * Only VM_WRITE means shadow stack. + */ + if (prot & PROT_WRITE) + ret =3D (VM_READ | VM_WRITE); + return ret; +} + +#define arch_calc_vm_prot_bits(prot, pkey) arch_calc_vm_prot_bits(prot, pk= ey) + +#endif /* ! __ASM_MMAN_H__ */ diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgta= ble.h index 050fdc49b5ad..8c528cd7347a 100644 --- a/arch/riscv/include/asm/pgtable.h +++ b/arch/riscv/include/asm/pgtable.h @@ -178,6 +178,7 @@ extern struct pt_alloc_ops pt_ops __meminitdata; #define PAGE_READ_EXEC __pgprot(_PAGE_BASE | _PAGE_READ | _PAGE_EXEC) #define PAGE_WRITE_EXEC __pgprot(_PAGE_BASE | _PAGE_READ | \ _PAGE_EXEC | _PAGE_WRITE) +#define PAGE_SHADOWSTACK __pgprot(_PAGE_BASE | _PAGE_WRITE) =20 #define PAGE_COPY PAGE_READ #define PAGE_COPY_EXEC PAGE_READ_EXEC diff --git a/arch/riscv/kernel/sys_riscv.c b/arch/riscv/kernel/sys_riscv.c index d77afe05578f..43a448bf254b 100644 --- a/arch/riscv/kernel/sys_riscv.c +++ b/arch/riscv/kernel/sys_riscv.c @@ -7,6 +7,7 @@ =20 #include #include +#include =20 static long riscv_sys_mmap(unsigned long addr, unsigned long len, unsigned long prot, unsigned long flags, @@ -16,6 +17,15 @@ static long riscv_sys_mmap(unsigned long addr, unsigned = long len, if (unlikely(offset & (~PAGE_MASK >> page_shift_offset))) return -EINVAL; =20 + /* + * If PROT_WRITE is specified then extend that to PROT_READ + * protection_map[VM_WRITE] is now going to select shadow stack encodings. + * So specifying PROT_WRITE actually should select protection_map [VM_WRI= TE | VM_READ] + * If user wants to create shadow stack then they should use `map_shadow_= stack` syscall. + */ + if (unlikely((prot & PROT_WRITE) && !(prot & PROT_READ))) + prot |=3D PROT_READ; + return ksys_mmap_pgoff(addr, len, prot, flags, fd, offset >> (PAGE_SHIFT - page_shift_offset)); } diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c index 15b2eda4c364..9d6661638d0b 100644 --- a/arch/riscv/mm/init.c +++ b/arch/riscv/mm/init.c @@ -342,7 +342,7 @@ pgd_t early_pg_dir[PTRS_PER_PGD] __initdata __aligned(P= AGE_SIZE); static const pgprot_t protection_map[16] =3D { [VM_NONE] =3D PAGE_NONE, [VM_READ] =3D PAGE_READ, - [VM_WRITE] =3D PAGE_COPY, + [VM_WRITE] =3D PAGE_SHADOWSTACK, [VM_WRITE | VM_READ] =3D PAGE_COPY, [VM_EXEC] =3D PAGE_EXEC, [VM_EXEC | VM_READ] =3D PAGE_READ_EXEC, --=20 2.34.1 From nobody Tue Feb 10 09:58:08 2026 Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) (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 539BC22FE0A for ; Mon, 10 Mar 2025 14:52:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741618372; cv=none; b=MdvWGuy+ngmlOG4HrrxZ3SvT7bxfiZ+qbBfFfyHw7JUq5e5bH0SfV3TEleEEwu9DL7sH01bnZ8CsJXpr+arnRptvdpy2SWNABeJhgfEPdRgd3F2mxjHZEFYeh+0AQl37syJD0Io4WhMA5QsUAn3WWNcd55IOPVBXwfRw0H1doNM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741618372; c=relaxed/simple; bh=VGYxjWQw4QYGSfT3AnAkedt5HRuw2tlx5dJCnKUawWM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Pv6xj1MnMSwvnaKArntS2dBkcO7mx2O4Bd3EnGODN9tVSquwaVz3rDelfwU0ZDFSPA0ZVzE9+K/86SQBHF23HymNkhJvIpVzsPcHb73bgxQi/OKudGJngAVoAT+WFwVyUwCVv/WpFYme6F2b/yGJvitA54JvUFJklUTxw6p8QR0= 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=oc1s/WCY; arc=none smtp.client-ip=209.85.214.174 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 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="oc1s/WCY" Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-22548a28d0cso11715525ad.3 for ; Mon, 10 Mar 2025 07:52:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741618370; x=1742223170; darn=vger.kernel.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=Bi056oDr44X6mEA5ptOBWamFE29btTVHHp5F60vYQ4s=; b=oc1s/WCYZRpnVyFozEO8JkLT7a/d9l53T015BjY4bGVFrZrfX7l0wLxzZeeBFrmEaN qLm2gSBxCbttdRqkiIg32bsbrOfwXAdXPhDKlTshunkV2KtrufBxbKTyWdPryJNUwHsU MFPhz4EZVX0KJfZ1ycLZ/SxkIEr7i9u6zrtd1c/V6a74x9lararexA/WTlNp7QZLTX9a c6ciXdtgNvSbH0v9PqKBaMy0KAEUkJXLK+nGpbQBIHtc+/qVZ6pHXlR4elVQArkSngp3 ODr8KoS0BzLiGxIroIlf5qDx9Bdzi8vZiUEbE8FQZf8GRqNMc3iE6iN9dSr+vzCf4tb1 nRlQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741618370; x=1742223170; 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=Bi056oDr44X6mEA5ptOBWamFE29btTVHHp5F60vYQ4s=; b=E0h7u3PRHsb1zuB5RN5o8zGIKwVS5MYE7yu+JvbLloOIKhVmVijnaHs9g6XiYNQXJ4 mC3bG6pCCSwaoLkc6KrJPyjJrIoCrPE7dkVxyXUY4tdNU7vLHJLA5ZZv1RUmkujGtGvb iXWnOQfUriBTzEKloBMfzr/1h/qgh0834jz1BlnGQ44zC536MFlmwC/SbuWOlNCcaLot vxMr+f8J9yFNjTv4bukGTmrFP5aSfKwgyHh1iiLBqwh/6nqsSDqxBI5OuPQlPvGksp1U Q1Am0K+iozK25SMXSXVGsne+wS+qw0XeVdUoCaowsUZAWJdVyumbXWgdfIAkF4X+Msy/ 00LQ== X-Gm-Message-State: AOJu0YzoNRP4+3tEBOpFrVnBpylVsTl8qUHh8fRDkKZVLS1X9JgYfuMP QrlPByWS/lgBTzgM8C2Pnxxqqf+4mHtTEt6o6TPwTa4bzhrPkovgBplRokBQooo= X-Gm-Gg: ASbGncuREpmX5JMASdfjYR1WUyJ3EGadAC4r/HHAW1sfGt5+M7eI+kyrgLJOp5EHIxS gDCvJBWl0PmJ43zJsGUyvCzp3BIk3FdA+sqAFUwtwZvH0gQ9r57b+BlbgvpHud8ZBc720d3yYct nHcsfhWhMoF1WX1Fk3700WktYfxOH2ylVVDdWWJc5O5kF+5pyId6AaZ/h2V+UfWi7ElcVDtPWxd cBrws52n1JSSUS65DCyBFSsan8Bdki/tTi2wP90sF6chG13PdNqBhMTM5RTbJIfZdPZ85YptDKz VtzGHBo+zn0G31PBgYtUE6l3ZAI8yT1FSkGfdIOpIvdfAb0KV8CZqM4= X-Google-Smtp-Source: AGHT+IHIjPCK5/0EhbZcTRnURyZw0NTj2K+W71g/3+djiMGOR5iYaRFLtirQywnmf6SGr6Tob6ydqg== X-Received: by 2002:a05:6a00:218c:b0:736:562b:9a9c with SMTP id d2e1a72fcca58-736aaadf10emr17282132b3a.18.1741618369717; Mon, 10 Mar 2025 07:52:49 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-736d11d4600sm2890275b3a.116.2025.03.10.07.52.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 07:52:49 -0700 (PDT) From: Deepak Gupta Date: Mon, 10 Mar 2025 07:52:29 -0700 Subject: [PATCH v11 07/27] riscv mm: manufacture shadow stack pte Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250310-v5_user_cfi_series-v11-7-86b36cbfb910@rivosinc.com> References: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> In-Reply-To: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@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 , Jann Horn , Conor Dooley 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 This patch implements creating shadow stack pte (on riscv). Creating shadow stack PTE on riscv means that clearing RWX and then setting W=3D1. Signed-off-by: Deepak Gupta Reviewed-by: Alexandre Ghiti Reviewed-by: Zong Li --- arch/riscv/include/asm/pgtable.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgta= ble.h index 8c528cd7347a..ede43185ffdf 100644 --- a/arch/riscv/include/asm/pgtable.h +++ b/arch/riscv/include/asm/pgtable.h @@ -421,6 +421,11 @@ static inline pte_t pte_mkwrite_novma(pte_t pte) return __pte(pte_val(pte) | _PAGE_WRITE); } =20 +static inline pte_t pte_mkwrite_shstk(pte_t pte) +{ + return __pte((pte_val(pte) & ~(_PAGE_LEAF)) | _PAGE_WRITE); +} + /* static inline pte_t pte_mkexec(pte_t pte) */ =20 static inline pte_t pte_mkdirty(pte_t pte) @@ -749,6 +754,11 @@ static inline pmd_t pmd_mkwrite_novma(pmd_t pmd) return pte_pmd(pte_mkwrite_novma(pmd_pte(pmd))); } =20 +static inline pmd_t pmd_mkwrite_shstk(pmd_t pte) +{ + return __pmd((pmd_val(pte) & ~(_PAGE_LEAF)) | _PAGE_WRITE); +} + static inline pmd_t pmd_wrprotect(pmd_t pmd) { return pte_pmd(pte_wrprotect(pmd_pte(pmd))); --=20 2.34.1 From nobody Tue Feb 10 09:58:08 2026 Received: from mail-pl1-f176.google.com (mail-pl1-f176.google.com [209.85.214.176]) (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 E82C022F39B for ; Mon, 10 Mar 2025 14:52:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741618374; cv=none; b=U1iG3wkHmTQ+xjaLgqzK8LXsj2tzpui/wAUN6vWFqYzN6S/Jo66dVRA7QnBEOuieJ/sNFWPSZQmX39ovevNNdJ/GB0lFYDgXV/+Tz5DHzgAAbV/V/DjZtzvuGyQk/Cf7V7n0CWxc3MlcMSE9/q1c1sdalM2tH2b1D9/AhqdNKko= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741618374; c=relaxed/simple; bh=HOQqOSPO+MhBbccjb2UbKqhOT3MYkxspDNQyPTpGYhw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=pYUE/PlhE1JvUIvlriuvixmMJgY5ftyO+cWGxflk6RX3XNi3/BeaEeMarsK6uC1IV29tvfGkkwCAbGku6FR77jREbxPfc3DybJgV+TRTwLQV9ZNAlfEzPKZNK4cVjW5odHuz+ZO3jdcvRgkNg5EPtRvDblPcfNiEjNMXfpICiSM= 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=Hx4U+qGs; arc=none smtp.client-ip=209.85.214.176 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 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="Hx4U+qGs" Received: by mail-pl1-f176.google.com with SMTP id d9443c01a7336-22398e09e39so78269225ad.3 for ; Mon, 10 Mar 2025 07:52:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741618372; x=1742223172; darn=vger.kernel.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=WPde6NcIQ47rHbegQG8dPMP+orPth983zEAPX8ktkAY=; b=Hx4U+qGs9S6hqwXpS5iUSJGfoCj/H/Z2V37VGzS1jtYLjJ9Od9PdFoyeMKbfw8hB+A OylLH/zZLAYT7hAp2IKTnQfwYK/TU5Mg0N93bgKBl24PVn8Agn6YYZnpamFwYsLSXoTw +2d09hyxqQlZVx7gvxJAKZya1LORRFpS0Nl8WN9oQHIpJjGoRBPMpY3Mt1IaamowcCBu DdFkAfLXjr+9dikeulp7CLiDbkjs8yiWh+CK6SQjZoBzpDyHwxbohMqq+cwMK/158zJZ upRS1TC2lCXZd0ZYgl0ApPGe6FbmvTdHLDfhzlcM63pWopY5ykHE4RoJF1DH2YZjPQdj /wiQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741618372; x=1742223172; 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=WPde6NcIQ47rHbegQG8dPMP+orPth983zEAPX8ktkAY=; b=d9wBKi3rvEnfa9CgxRUmHT3Cf2hWyhIcXDEn3ckqOlE0ZRcm8GVbUeUi04tUOOU19o Sac9mUsyRXfujI7KnEsuV3QP2TD92hRqQTkY6ayBx55D/UjK25k7fmxDk88QuD1f+aWG +gx5NHD+etwAg+mZd5ZlRP7/EgFqSkfzB1T7Q+iY0l98czSGYzx14XSudryVKP2KVP85 V2HZHxj15rbI2s5IoVWRQ8fVI2Z5ZWOAVdJdQgjvZ7BI6VzGhQf7IwmR9jGv/pB0XzNS 54e+7pjF10m2F4wq2OArM7dG+1CZlNG1SoUmFIIjoxPcWM8KSMD0OxhUAXBESp8prv3H aA7A== X-Gm-Message-State: AOJu0YzASaBYdRYhh6UdWoIn1f6a196x2RG84qgfRtc4Xmws25uQSrwp 8c1q7J/HYNw8ofSqPL47eELSoJeSnRpxJ7fPROrD09+Jfib747/Cfimg3MBluY0= X-Gm-Gg: ASbGnctxVlLNMofkDw1RtzeVetd4ro1ydu0N0D5jY0DGQuIaqlNeNk3APd3Hl1nldnF EG4i8XoSt3NAFzjsLqY+9sTk/fvLLe3fBtsNAgUEAv5gH+54XS0xKRvlW1uHO6a7JdnJsDJErvy f61tF/ae07WocWBpaQCF0oa/1nDYQDXV0d+1oXhEe/KZXlydVU99ZxSVwL3HkHMAiACeQWgkMpd F4a3/jW3R8DaWDCboMa/NRM0E43JuW5ZslzxhrjSO7VbxbjWuXTj1UDdOUEKWxLjKfzboy8o/QV OLOp2ck1MPFvsv3Qq/h9EEA3XHklDuEL63vFZx0nHlKWCYfZVeyXU7Y= X-Google-Smtp-Source: AGHT+IFqTxU+p9IhkI8RGdvHdyJEHoK+vlH5JWBOUOv0UhGLOT20CpZOV0dtGMVo/7iEBeBe/MTApw== X-Received: by 2002:a05:6a00:928b:b0:736:4d44:8b77 with SMTP id d2e1a72fcca58-736aaa1cf9fmr27006391b3a.8.1741618372105; Mon, 10 Mar 2025 07:52:52 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-736d11d4600sm2890275b3a.116.2025.03.10.07.52.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 07:52:51 -0700 (PDT) From: Deepak Gupta Date: Mon, 10 Mar 2025 07:52:30 -0700 Subject: [PATCH v11 08/27] riscv mmu: teach pte_mkwrite to manufacture shadow stack PTEs Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250310-v5_user_cfi_series-v11-8-86b36cbfb910@rivosinc.com> References: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> In-Reply-To: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@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 , Jann Horn , Conor Dooley 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 pte_mkwrite creates PTEs with WRITE encodings for underlying arch. Underlying arch can have two types of writeable mappings. One that can be written using regular store instructions. Another one that can only be written using specialized store instructions (like shadow stack stores). pte_mkwrite can select write PTE encoding based on VMA range (i.e. VM_SHADOW_STACK) Signed-off-by: Deepak Gupta Reviewed-by: Alexandre Ghiti Reviewed-by: Zong Li --- arch/riscv/include/asm/pgtable.h | 7 +++++++ arch/riscv/mm/pgtable.c | 17 +++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgta= ble.h index ede43185ffdf..ccd2fa34afb8 100644 --- a/arch/riscv/include/asm/pgtable.h +++ b/arch/riscv/include/asm/pgtable.h @@ -416,6 +416,10 @@ static inline pte_t pte_wrprotect(pte_t pte) =20 /* static inline pte_t pte_mkread(pte_t pte) */ =20 +struct vm_area_struct; +pte_t pte_mkwrite(pte_t pte, struct vm_area_struct *vma); +#define pte_mkwrite pte_mkwrite + static inline pte_t pte_mkwrite_novma(pte_t pte) { return __pte(pte_val(pte) | _PAGE_WRITE); @@ -749,6 +753,9 @@ static inline pmd_t pmd_mkyoung(pmd_t pmd) return pte_pmd(pte_mkyoung(pmd_pte(pmd))); } =20 +pmd_t pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma); +#define pmd_mkwrite pmd_mkwrite + static inline pmd_t pmd_mkwrite_novma(pmd_t pmd) { return pte_pmd(pte_mkwrite_novma(pmd_pte(pmd))); diff --git a/arch/riscv/mm/pgtable.c b/arch/riscv/mm/pgtable.c index 4ae67324f992..be5d38546bb3 100644 --- a/arch/riscv/mm/pgtable.c +++ b/arch/riscv/mm/pgtable.c @@ -155,3 +155,20 @@ pmd_t pmdp_collapse_flush(struct vm_area_struct *vma, return pmd; } #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ + +pte_t pte_mkwrite(pte_t pte, struct vm_area_struct *vma) +{ + if (vma->vm_flags & VM_SHADOW_STACK) + return pte_mkwrite_shstk(pte); + + return pte_mkwrite_novma(pte); +} + +pmd_t pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma) +{ + if (vma->vm_flags & VM_SHADOW_STACK) + return pmd_mkwrite_shstk(pmd); + + return pmd_mkwrite_novma(pmd); +} + --=20 2.34.1 From nobody Tue Feb 10 09:58:08 2026 Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) (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 2AB74230BE3 for ; Mon, 10 Mar 2025 14:52:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741618378; cv=none; b=GXbrFYNNEAkdWS7ntG3BA16IKxJdgkYylNXpHz2Wzc1nqx4Nq7WodjACsw3e1arHjkGWhrILBxk7jJiawqcw50AsQQYWdiF3HorAqYRrIZIaf4Eijs0p6rrKsT2SRhqs0XviYLwWIOluB35MYO6k3YtXrfXJBo6W9IYkuALdPIw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741618378; c=relaxed/simple; bh=e2JFB4Jy2B8PEfrSQ43O9FncAUYVFHD+Rwfp/3fLFok=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=DcD/FK0B3JoqoXDbcZI5hnjI2su5V9Xue1GLWoCxNZ5yOYErR4lu7HPJHeeb8Wq3JNGYyeGaTZ+2RjVPc5LlEvsQWdj5wn1jtGk76a0SDhuUz6ERYtaYNZZVNK/NjSYGof3hWK2ik8F3mRIXLYNxh/TtrU/yWpRcZiWKSRBhkXA= 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=LuFdM79y; arc=none smtp.client-ip=209.85.214.174 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 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="LuFdM79y" Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-2239aa5da08so68477295ad.3 for ; Mon, 10 Mar 2025 07:52:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741618374; x=1742223174; darn=vger.kernel.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=kL+yIoSlkTX0SokBaTv1DsPGjl4jKUPC0i4GBiCWGFY=; b=LuFdM79yGrhEpDO04BXfvUzHEh4vd1FandU8wTPhue8Tj2YvZ+fA0BgWi3fsUHBdLT pde4Yi4mV2AqE4ybFzZ5hxBkTRi9F4Z++sm32zMfh0PT+XpX/hBQnjjBeHlufV761eUZ CzLA9vXAQnDIOknNMjHlLeVVMC5iznFYqqfFrxCcaN5aoBuScOAFNr0UQxOt3XYXSO/0 Re+bNbOaohstxNUYVvBZQmTphYgt1W1DqKKY9NwKELxIMI1IC5FlJ5vloTpeOpcLyWv+ d6/D5Vy30OuIix+io7FnNjA3k+70DH4V97wsUJ1NpgkAN5619qLCDM4meygsOLm8kPiW VMbQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741618374; x=1742223174; 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=kL+yIoSlkTX0SokBaTv1DsPGjl4jKUPC0i4GBiCWGFY=; b=fBC7ZEIBRe/MdjW+7aKq+I2bH5Gc/xeje9HIZuNJI7kjoslHMTLr6RTfWL5PjYxmiG BVtgz6+fsYnAlloFjP2rks1++Q8ZoIezlIll/2lN2F03k5XV+MSvuENQDFbzxtmcary4 IDXuWXg9SfV49YxmOhh4WMb+tN8arVnxRtRFvk798iq9y5PxKdRdSAG/H9xIOoDgjdvC +uYy28AOfCkwmQ+a0sVe8HeHxTFagaLBcn2zOxU7jHVIEoYI3yHjQEMRqPrzzznyO06C iayc4/Fc+j6jNmuDHBHnLiWRWexhSp5mLS9Kcph+OD89QS/+ntaAxD06QAL2oO6lStOK u8lQ== X-Gm-Message-State: AOJu0Yy6kdy12qGk7SVHWmHO3aPLJtIEulHvSXXnG2RAFh8Exl5ejK1g nMgJr5uQGQIbyE2PsMXwRGz+Ior9qZNsIu5oFnEjKD1jS1AVwL04/OBk6rDHDTk= X-Gm-Gg: ASbGncu8H+wGmJRc6ExcHpeJGOQ4fPpQmSk7sLAq3/bFkNuzt76G8WX30LBrpIwpmnT LkPB/DlYOblAd1Mw9sQvvmeM775FKihfu0b8Y7+ujhtZAUfS8Umrf3ZwUaYN9+kk30KEv7Chxrz IWMh7bSbbnJB9wlsrdCf1Nfssqvg9wmZuzFc0CFGUKK8eojQevhHRkKigLL27NKJ4vin6owxgBG VZA2JaRAKX3Xlng0g2JXSm2/x0FfHMYIgHwJvBlFsJpxU6LOxVQJPws8UrG30Tfb8n70MEaR6yP TWLXNWUpmWPosqE7l15F0Cf6HARtstL+t9UVyBDwHo2Ue2Yl3kTiOT4= X-Google-Smtp-Source: AGHT+IFKPuh8cOHYjBgEoM7t8cHgv4gW4YpL9WqheJYopfOZX5sbWBiog25DM4bQ+IO4xPTW/pcKfQ== X-Received: by 2002:a05:6a00:14d1:b0:736:755b:8311 with SMTP id d2e1a72fcca58-736aaad1daamr15723454b3a.16.1741618374533; Mon, 10 Mar 2025 07:52:54 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-736d11d4600sm2890275b3a.116.2025.03.10.07.52.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 07:52:54 -0700 (PDT) From: Deepak Gupta Date: Mon, 10 Mar 2025 07:52:31 -0700 Subject: [PATCH v11 09/27] riscv mmu: write protect and shadow stack Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250310-v5_user_cfi_series-v11-9-86b36cbfb910@rivosinc.com> References: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> In-Reply-To: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@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 , Jann Horn , Conor Dooley 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 `fork` implements copy on write (COW) by making pages readonly in child and parent both. ptep_set_wrprotect and pte_wrprotect clears _PAGE_WRITE in PTE. Assumption is that page is readable and on fault copy on write happens. To implement COW on shadow stack pages, clearing up W bit makes them XWR = =3D 000. This will result in wrong PTE setting which says no perms but V=3D1 and PFN field pointing to final page. Instead desired behavior is to turn it into a readable page, take an access (load/store) fault on sspush/sspop (shadow stack) and then perform COW on such pages. This way regular reads would still be allowed and not lead to COW maintaining current behavior of COW on non-shadow stack but writeable memory. On the other hand it doesn't interfere with existing COW for read-write memory. Assumption is always that _PAGE_READ must have been set and thus setting _PAGE_READ is harmless. Signed-off-by: Deepak Gupta Reviewed-by: Alexandre Ghiti Reviewed-by: Zong Li --- arch/riscv/include/asm/pgtable.h | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgta= ble.h index ccd2fa34afb8..54707686f042 100644 --- a/arch/riscv/include/asm/pgtable.h +++ b/arch/riscv/include/asm/pgtable.h @@ -411,7 +411,7 @@ static inline int pte_devmap(pte_t pte) =20 static inline pte_t pte_wrprotect(pte_t pte) { - return __pte(pte_val(pte) & ~(_PAGE_WRITE)); + return __pte((pte_val(pte) & ~(_PAGE_WRITE)) | (_PAGE_READ)); } =20 /* static inline pte_t pte_mkread(pte_t pte) */ @@ -612,7 +612,15 @@ static inline pte_t ptep_get_and_clear(struct mm_struc= t *mm, static inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long address, pte_t *ptep) { - atomic_long_and(~(unsigned long)_PAGE_WRITE, (atomic_long_t *)ptep); + pte_t read_pte =3D READ_ONCE(*ptep); + /* + * ptep_set_wrprotect can be called for shadow stack ranges too. + * shadow stack memory is XWR =3D 010 and thus clearing _PAGE_WRITE will = lead to + * encoding 000b which is wrong encoding with V =3D 1. This should lead t= o page fault + * but we dont want this wrong configuration to be set in page tables. + */ + atomic_long_set((atomic_long_t *)ptep, + ((pte_val(read_pte) & ~(unsigned long)_PAGE_WRITE) | _PAGE_READ)); } =20 #define __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH --=20 2.34.1 From nobody Tue Feb 10 09:58:08 2026 Received: from mail-pl1-f178.google.com (mail-pl1-f178.google.com [209.85.214.178]) (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 B4437231A37 for ; Mon, 10 Mar 2025 14:52:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741618380; cv=none; b=R8uiWiEDZu+0xj3GZlykVGgaMmTerWvqc7fFQJH0M1TREvPOpoiXlAEuO8+vctfBMjpDM/pcMgE9aYlSQiiGoDQhie3Zfzk4BHL4I/BFxMGdPH6iw56/dg2zcVAemJpJLqbbudOc2Ax4u2peVkXX+RvnTcx8LBv+sJjvbFpkR40= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741618380; c=relaxed/simple; bh=n/OJIVv97sm5i0GJSBQDLTL5bF2hSZWy7j+gIWNrU0I=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Dvbq+ly7NWOs6u9ehH9LH+E1Y5T0Iglp7Ky3cIilZ03Tmy6eBtGtj23iKKIom7aAS4cJWDFYbuFmaPOZSkSiMTrC6PpcDoiMLg/96TsQl3bqhVneD9D2psaL7dJ8rxUen+3tSg6GIuJunZqjxykJfvqzV6bXVD0p9BEz7iWQGe4= 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=pyUWQWKH; arc=none smtp.client-ip=209.85.214.178 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 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="pyUWQWKH" Received: by mail-pl1-f178.google.com with SMTP id d9443c01a7336-225477548e1so32565285ad.0 for ; Mon, 10 Mar 2025 07:52:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741618377; x=1742223177; darn=vger.kernel.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=hK7DlmuIRYzIsuvvXHgsbKnv3WN7JKJpEt1D7vAnJaQ=; b=pyUWQWKHtVELlxZsVva9Yh0OFLlmCHRDhPpwGxjr9CeIbmVmSNf2pLWiho+HKm+/V/ mocRtuWtCosANQXa+aQOsZeo+KkgFkilvu5aoFXqsb5bOtimfMr0hLb1unmKCzjdLEp3 4Y+aiStiCkV1ZCabRjvfUKRnYPpyyq76fBcojz1CPcnxXCKmNDmvdRKVhT1NEA2pYfZs L1hGHfYQ1teooBaokcTYPlE0Wc33DyKbDKD6pKJspSg06qziPeqaCohl+sSpxItYVG5I sb1s7NMN17PXj6bUVK04VzAzC/cq/rEOHgE71esd74WdeeBrm/DqIQzlMJitthd56d9U eC3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741618377; x=1742223177; 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=hK7DlmuIRYzIsuvvXHgsbKnv3WN7JKJpEt1D7vAnJaQ=; b=PkcT3zyGLMRTl8Dm+jCIkx9BShilrgwqEUFg5Eqno5aiM7WPl3HtglQEIVM8F8r1KJ icI6yl6v7auoelwx0JFFfEgdSsNJkX6obcHUKwghea2HxzBgsY2DZjXx2oDW9dl6J6Xf KVkqHuqjkZ/LCncSWvexbK65t+lwTmbo0RqAor94T87216AnzrmjoLJTkqIK3Ix+rZuP pFr+JwdD++Mq8wMhsEiBoCqQl8RsUeHu3tiBdQL33v1K/6oRIsxCzPrVk6cwq6+toxd1 7UWJLayDar0E/DB3P21uUX5pGD66KsJjO4QPXGvBzjh/DZS23DbSXUzYdrZJE0rKuN4M qBXQ== X-Gm-Message-State: AOJu0YzciomrBUOIiUp5MFnovAWucvBLuIyabHGYA+b5rlTUdoeaAcUI 33rvsJMOZz5AJZrGDMdJpQXAu1TEYTDCxYk2c+8kQYWbWbYT9y9UYZsUJVc7YMI= X-Gm-Gg: ASbGncs0zQurhim0bI7y4TEr11xVFPsWT3tVZ8wyBTiDOrMlOA7rRlDEITqxVOHBgAd DPmgMh++3Pv7n4qZgCcif0fWj9I11GKCS+M6VGYgKlTguKKe+Tc36ydPqhbYEB6fbu8lNY5mtsj vdDYH2iQdgp0K81Xk3a13sBxW1yZdF3qTOS9DEZGGH+PUL5mPO1KTQZ1CnWs096v2PRW8ebSq8V p52+v2xu/+WVRNG/h1DR5/6dCeJz3hzyKJB5VU9xz+wNn65f3n2EISf+w1fyFwkdrJlW4sPUO3O haznOJz5xFm4e9QiHMZR7jdCM2GGJXgwiRbcTP7bAVOdSvwZsOsAL7Q= X-Google-Smtp-Source: AGHT+IGqKWX94qO9AqEOI9XVMPr9YtNG73DineLHU1SUa0qmkdl6iHIBj3KU2DOsCD0Dz98oJGoQVg== X-Received: by 2002:a05:6a00:b86:b0:736:50c4:3e0f with SMTP id d2e1a72fcca58-736aaa373b8mr17095202b3a.10.1741618376979; Mon, 10 Mar 2025 07:52:56 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-736d11d4600sm2890275b3a.116.2025.03.10.07.52.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 07:52:56 -0700 (PDT) From: Deepak Gupta Date: Mon, 10 Mar 2025 07:52:32 -0700 Subject: [PATCH v11 10/27] riscv/mm: Implement map_shadow_stack() syscall Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250310-v5_user_cfi_series-v11-10-86b36cbfb910@rivosinc.com> References: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> In-Reply-To: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@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 , Jann Horn , Conor Dooley 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 As discussed extensively in the changelog for the addition of this syscall on x86 ("x86/shstk: Introduce map_shadow_stack syscall") the existing mmap() and madvise() syscalls do not map entirely well onto the security requirements for shadow stack memory since they lead to windows where memory is allocated but not yet protected or stacks which are not properly and safely initialised. Instead a new syscall map_shadow_stack() has been defined which allocates and initialises a shadow stack page. This patch implements this syscall for riscv. riscv doesn't require token to be setup by kernel because user mode can do that by itself. However to provide compatibility and portability with other architectues, user mode can specify token set flag. Signed-off-by: Deepak Gupta Reviewed-by: Zong Li --- arch/riscv/kernel/Makefile | 1 + arch/riscv/kernel/usercfi.c | 144 ++++++++++++++++++++++++++++++++++++++++= ++++ 2 files changed, 145 insertions(+) diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile index 8d186bfced45..3a861d320654 100644 --- a/arch/riscv/kernel/Makefile +++ b/arch/riscv/kernel/Makefile @@ -125,3 +125,4 @@ obj-$(CONFIG_ACPI) +=3D acpi.o obj-$(CONFIG_ACPI_NUMA) +=3D acpi_numa.o =20 obj-$(CONFIG_GENERIC_CPU_VULNERABILITIES) +=3D bugs.o +obj-$(CONFIG_RISCV_USER_CFI) +=3D usercfi.o diff --git a/arch/riscv/kernel/usercfi.c b/arch/riscv/kernel/usercfi.c new file mode 100644 index 000000000000..24022809a7b5 --- /dev/null +++ b/arch/riscv/kernel/usercfi.c @@ -0,0 +1,144 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2024 Rivos, Inc. + * Deepak Gupta + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define SHSTK_ENTRY_SIZE sizeof(void *) + +/* + * Writes on shadow stack can either be `sspush` or `ssamoswap`. `sspush` = can happen + * implicitly on current shadow stack pointed to by CSR_SSP. `ssamoswap` t= akes pointer to + * shadow stack. To keep it simple, we plan to use `ssamoswap` to perform = writes on shadow + * stack. + */ +static noinline unsigned long amo_user_shstk(unsigned long *addr, unsigned= long val) +{ + /* + * Never expect -1 on shadow stack. Expect return addresses and zero + */ + unsigned long swap =3D -1; + + __enable_user_access(); + asm goto( + ".option push\n" + ".option arch, +zicfiss\n" + "1: ssamoswap.d %[swap], %[val], %[addr]\n" + _ASM_EXTABLE(1b, %l[fault]) + RISCV_ACQUIRE_BARRIER + ".option pop\n" + : [swap] "=3Dr" (swap), [addr] "+A" (*addr) + : [val] "r" (val) + : "memory" + : fault + ); + __disable_user_access(); + return swap; +fault: + __disable_user_access(); + return -1; +} + +/* + * Create a restore token on the shadow stack. A token is always XLEN wide + * and aligned to XLEN. + */ +static int create_rstor_token(unsigned long ssp, unsigned long *token_addr) +{ + unsigned long addr; + + /* Token must be aligned */ + if (!IS_ALIGNED(ssp, SHSTK_ENTRY_SIZE)) + return -EINVAL; + + /* On RISC-V we're constructing token to be function of address itself */ + addr =3D ssp - SHSTK_ENTRY_SIZE; + + if (amo_user_shstk((unsigned long __user *)addr, (unsigned long)ssp) =3D= =3D -1) + return -EFAULT; + + if (token_addr) + *token_addr =3D addr; + + return 0; +} + +static unsigned long allocate_shadow_stack(unsigned long addr, unsigned lo= ng size, + unsigned long token_offset, bool set_tok) +{ + int flags =3D MAP_ANONYMOUS | MAP_PRIVATE; + struct mm_struct *mm =3D current->mm; + unsigned long populate, tok_loc =3D 0; + + if (addr) + flags |=3D MAP_FIXED_NOREPLACE; + + mmap_write_lock(mm); + addr =3D do_mmap(NULL, addr, size, PROT_READ, flags, + VM_SHADOW_STACK | VM_WRITE, 0, &populate, NULL); + mmap_write_unlock(mm); + + if (!set_tok || IS_ERR_VALUE(addr)) + goto out; + + if (create_rstor_token(addr + token_offset, &tok_loc)) { + vm_munmap(addr, size); + return -EINVAL; + } + + addr =3D tok_loc; + +out: + return addr; +} + +SYSCALL_DEFINE3(map_shadow_stack, unsigned long, addr, unsigned long, size= , unsigned int, flags) +{ + bool set_tok =3D flags & SHADOW_STACK_SET_TOKEN; + unsigned long aligned_size =3D 0; + + if (!cpu_supports_shadow_stack()) + return -EOPNOTSUPP; + + /* Anything other than set token should result in invalid param */ + if (flags & ~SHADOW_STACK_SET_TOKEN) + return -EINVAL; + + /* + * Unlike other architectures, on RISC-V, SSP pointer is held in CSR_SSP = and is available + * CSR in all modes. CSR accesses are performed using 12bit index program= med in instruction + * itself. This provides static property on register programming and writ= es to CSR can't + * be unintentional from programmer's perspective. As long as programmer = has guarded areas + * which perform writes to CSR_SSP properly, shadow stack pivoting is not= possible. Since + * CSR_SSP is writeable by user mode, it itself can setup a shadow stack = token subsequent + * to allocation. Although in order to provide portablity with other arch= itecture (because + * `map_shadow_stack` is arch agnostic syscall), RISC-V will follow expec= tation of a token + * flag in flags and if provided in flags, setup a token at the base. + */ + + /* If there isn't space for a token */ + if (set_tok && size < SHSTK_ENTRY_SIZE) + return -ENOSPC; + + if (addr && (addr & (PAGE_SIZE - 1))) + return -EINVAL; + + aligned_size =3D PAGE_ALIGN(size); + if (aligned_size < size) + return -EOVERFLOW; + + return allocate_shadow_stack(addr, aligned_size, size, set_tok); +} --=20 2.34.1 From nobody Tue Feb 10 09:58:08 2026 Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) (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 61EBC22D78E for ; Mon, 10 Mar 2025 14:52:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741618382; cv=none; b=qwCXey0Y+udx7906LBfG6vg19TperSMLSaIJ6mGxQAt/96OnP73L6v6jCS2aF6ycCxHMitvYN6YB7M1enlIt8TJjjSxneAQLXN8Orl4v65h9LViyc2ZYjn4NbP3vCuC8HY/OJzECeLJEHUc8rtFxIgoKbb7vi2bYUe9VreDy7i8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741618382; c=relaxed/simple; bh=BQistqq2aCZJrgSlVqRoyzl9URmzqjL5e4ZD9aRzNA4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=XbhoFJjcAdCYcsyrjbkKqe+66YmrR3Zs8YbL8IbBHBcW2Xt3CQteloaV4O6a0iEV4yhNFX7GPteUKisZ3beSAJPqC98R71+t4NWuI8GmxIYg5Eq5aE/t1feNUcDi4GcIfyvT+tcecdMWZE1D9F5eWA0m+EKw+HPRcgu/A9Dm09E= 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=XfM/cK4w; arc=none smtp.client-ip=209.85.214.174 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 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="XfM/cK4w" Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-223f4c06e9fso72341505ad.1 for ; Mon, 10 Mar 2025 07:52:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741618379; x=1742223179; darn=vger.kernel.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=42Qh4XiAuZulv75xzl4kByDL6UOmUjop2oq2YsBLVIA=; b=XfM/cK4wKNxb7XmNV63Q5FJE1li3SilnftiR9uMQY6ZnNNCWhU1Ge+l6B9ozZJFPz4 4QhwFkPxEzUdKwcHQP0ec+xvIppVPHBrt3GgeXDEPY7f7px/437fDAC+u1rETnmZ/bwo e2ZEWh6CfCC3EDGbkAvHq0DmhOZ1RxKxKYex5m2gnG9qBrZjWcIlBRbm2nuP5ah9cuqR A72C7JZcBOH7m1E9bVY96Ohrn/IWtUmHAjqYp+INBQfl+ksWG8UAtDDr2CWOCiYBwuRp AIqg6fvKlcgVWWrRZuKz5s0Fn1I4JN2VGkafuzZ6yUyV1eP1lY2D+CzGenuJmS5UGpl1 qwTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741618379; x=1742223179; 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=42Qh4XiAuZulv75xzl4kByDL6UOmUjop2oq2YsBLVIA=; b=eZ+id8a8/P6bWZVE67Rg6qaI1SSONOHbffup2scCiNsN0hlu39G+QwhHM5lA6Xzhfa ofgqLH5lrfBtsGV6lgvEuHc17hTx6iFaCdOToVFPyoHbm38vA0Ag70MsVaOgwcqSLtBP V39gabeuIycEh6uxtSk/5CenKzjjTjxyvvo8m6LS0b97Vl8bGmfSAbIvyZ+lf06iMSNh D5yE4kJDCmCfjv93o9J+bHremqyxrpvkvHuWXJ1amltpbGigX9P+u6KB1hsqkQlcPlQQ A0GPKP7s8gxqb1zYKEhFuM58YJf4Idsvc/eX117SIss/JBmBYF177BZ226V0rcvofip3 EfGQ== X-Gm-Message-State: AOJu0YyK2bpf0k/u9n4+xGPdcYzwEY855ef0XUUIm0Z1lmCJdi7GGp6V Ercvf+diOgpnwPg0708zynaQsT8oKzkHjgtYAHkzVPO2MMYEHz2n0GL+2fzOU2g= X-Gm-Gg: ASbGncsPU07HpIttDwjDGbpO2wu4sW+26cuUz2ZPi0IzWAvHY9HA45RKUpzo71jYhDP z+gLB6O5j7C/strRnMJg4Yv5rpw280e74gqhKjfYSz8zSsVXqpZJhspUjNS/f07LvR/4BxOlAuN 06SppEzZZSqodfaI8uOYjZWIpJTRI+QKUAo4z4ZwP8Nu4e2q6nBNbiOpY3oK0A8XQH9HV8xWWDn PzQWjj3+ZRTSo8eU+atKaWHHDM22Q+G4YJFwBWvDgWHkwGx7spcFj2s0yESOY9VAFqtfOb5YSwO Wq3ExbJHd+nGA+K6GLZhEZs62w5XzxXSO8rd5BOIO68fK3cImG2s8kA= X-Google-Smtp-Source: AGHT+IHEXGQJboz6H1D26OUIQwYNlDx0HzkIlJbeLtq0a2EzFTAzmNT4PJCmia/gb9gcmwxF4dQMsA== X-Received: by 2002:a17:902:e74d:b0:223:517a:d5a9 with SMTP id d9443c01a7336-22464503452mr163981475ad.15.1741618379437; Mon, 10 Mar 2025 07:52:59 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-736d11d4600sm2890275b3a.116.2025.03.10.07.52.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 07:52:59 -0700 (PDT) From: Deepak Gupta Date: Mon, 10 Mar 2025 07:52:33 -0700 Subject: [PATCH v11 11/27] riscv/shstk: If needed allocate a new shadow stack on clone Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250310-v5_user_cfi_series-v11-11-86b36cbfb910@rivosinc.com> References: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> In-Reply-To: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@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 , Jann Horn , Conor Dooley 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 Userspace specifies CLONE_VM to share address space and spawn new thread. `clone` allow userspace to specify a new stack for new thread. However there is no way to specify new shadow stack base address without changing API. This patch allocates a new shadow stack whenever CLONE_VM is given. In case of CLONE_VFORK, parent is suspended until child finishes and thus can child use parent shadow stack. In case of !CLONE_VM, COW kicks in because entire address space is copied from parent to child. `clone3` is extensible and can provide mechanisms using which shadow stack as an input parameter can be provided. This is not settled yet and being extensively discussed on mailing list. Once that's settled, this commit will adapt to that. Signed-off-by: Deepak Gupta Reviewed-by: Zong Li --- arch/riscv/include/asm/mmu_context.h | 7 ++ arch/riscv/include/asm/usercfi.h | 25 ++++++++ arch/riscv/kernel/process.c | 9 +++ arch/riscv/kernel/usercfi.c | 120 +++++++++++++++++++++++++++++++= ++++ 4 files changed, 161 insertions(+) diff --git a/arch/riscv/include/asm/mmu_context.h b/arch/riscv/include/asm/= mmu_context.h index 8c4bc49a3a0f..dbf27a78df6c 100644 --- a/arch/riscv/include/asm/mmu_context.h +++ b/arch/riscv/include/asm/mmu_context.h @@ -48,6 +48,13 @@ static inline unsigned long mm_untag_mask(struct mm_stru= ct *mm) } #endif =20 +#define deactivate_mm deactivate_mm +static inline void deactivate_mm(struct task_struct *tsk, + struct mm_struct *mm) +{ + shstk_release(tsk); +} + #include =20 #endif /* _ASM_RISCV_MMU_CONTEXT_H */ diff --git a/arch/riscv/include/asm/usercfi.h b/arch/riscv/include/asm/user= cfi.h index 5f2027c51917..82d28ac98d76 100644 --- a/arch/riscv/include/asm/usercfi.h +++ b/arch/riscv/include/asm/usercfi.h @@ -8,6 +8,9 @@ #ifndef __ASSEMBLY__ #include =20 +struct task_struct; +struct kernel_clone_args; + #ifdef CONFIG_RISCV_USER_CFI struct cfi_status { unsigned long ubcfi_en : 1; /* Enable for backward cfi. */ @@ -17,6 +20,28 @@ struct cfi_status { unsigned long shdw_stk_size; /* size of shadow stack */ }; =20 +unsigned long shstk_alloc_thread_stack(struct task_struct *tsk, + const struct kernel_clone_args *args); +void shstk_release(struct task_struct *tsk); +void set_shstk_base(struct task_struct *task, unsigned long shstk_addr, un= signed long size); +unsigned long get_shstk_base(struct task_struct *task, unsigned long *size= ); +void set_active_shstk(struct task_struct *task, unsigned long shstk_addr); +bool is_shstk_enabled(struct task_struct *task); + +#else + +#define shstk_alloc_thread_stack(tsk, args) 0 + +#define shstk_release(tsk) + +#define get_shstk_base(task, size) 0UL + +#define set_shstk_base(task, shstk_addr, size) + +#define set_active_shstk(task, shstk_addr) + +#define is_shstk_enabled(task) false + #endif /* CONFIG_RISCV_USER_CFI */ =20 #endif /* __ASSEMBLY__ */ diff --git a/arch/riscv/kernel/process.c b/arch/riscv/kernel/process.c index 7c244de77180..99acb6342a37 100644 --- a/arch/riscv/kernel/process.c +++ b/arch/riscv/kernel/process.c @@ -29,6 +29,7 @@ #include #include #include +#include =20 #if defined(CONFIG_STACKPROTECTOR) && !defined(CONFIG_STACKPROTECTOR_PER_T= ASK) #include @@ -211,6 +212,7 @@ int copy_thread(struct task_struct *p, const struct ker= nel_clone_args *args) unsigned long clone_flags =3D args->flags; unsigned long usp =3D args->stack; unsigned long tls =3D args->tls; + unsigned long ssp =3D 0; struct pt_regs *childregs =3D task_pt_regs(p); =20 /* Ensure all threads in this mm have the same pointer masking mode. */ @@ -229,11 +231,18 @@ int copy_thread(struct task_struct *p, const struct k= ernel_clone_args *args) p->thread.s[0] =3D (unsigned long)args->fn; p->thread.s[1] =3D (unsigned long)args->fn_arg; } else { + /* allocate new shadow stack if needed. In case of CLONE_VM we have to */ + ssp =3D shstk_alloc_thread_stack(p, args); + if (IS_ERR_VALUE(ssp)) + return PTR_ERR((void *)ssp); + *childregs =3D *(current_pt_regs()); /* Turn off status.VS */ riscv_v_vstate_off(childregs); if (usp) /* User fork */ childregs->sp =3D usp; + /* if needed, set new ssp */ + ssp ? set_active_shstk(p, ssp) : 0; if (clone_flags & CLONE_SETTLS) childregs->tp =3D tls; childregs->a0 =3D 0; /* Return value of fork() */ diff --git a/arch/riscv/kernel/usercfi.c b/arch/riscv/kernel/usercfi.c index 24022809a7b5..73cf87dab186 100644 --- a/arch/riscv/kernel/usercfi.c +++ b/arch/riscv/kernel/usercfi.c @@ -19,6 +19,41 @@ =20 #define SHSTK_ENTRY_SIZE sizeof(void *) =20 +bool is_shstk_enabled(struct task_struct *task) +{ + return task->thread_info.user_cfi_state.ubcfi_en ? true : false; +} + +void set_shstk_base(struct task_struct *task, unsigned long shstk_addr, un= signed long size) +{ + task->thread_info.user_cfi_state.shdw_stk_base =3D shstk_addr; + task->thread_info.user_cfi_state.shdw_stk_size =3D size; +} + +unsigned long get_shstk_base(struct task_struct *task, unsigned long *size) +{ + if (size) + *size =3D task->thread_info.user_cfi_state.shdw_stk_size; + return task->thread_info.user_cfi_state.shdw_stk_base; +} + +void set_active_shstk(struct task_struct *task, unsigned long shstk_addr) +{ + task->thread_info.user_cfi_state.user_shdw_stk =3D shstk_addr; +} + +/* + * If size is 0, then to be compatible with regular stack we want it to be= as big as + * regular stack. Else PAGE_ALIGN it and return back + */ +static unsigned long calc_shstk_size(unsigned long size) +{ + if (size) + return PAGE_ALIGN(size); + + return PAGE_ALIGN(min_t(unsigned long long, rlimit(RLIMIT_STACK), SZ_4G)); +} + /* * Writes on shadow stack can either be `sspush` or `ssamoswap`. `sspush` = can happen * implicitly on current shadow stack pointed to by CSR_SSP. `ssamoswap` t= akes pointer to @@ -142,3 +177,88 @@ SYSCALL_DEFINE3(map_shadow_stack, unsigned long, addr,= unsigned long, size, unsi =20 return allocate_shadow_stack(addr, aligned_size, size, set_tok); } + +/* + * This gets called during clone/clone3/fork. And is needed to allocate a = shadow stack for + * cases where CLONE_VM is specified and thus a different stack is specifi= ed by user. We + * thus need a separate shadow stack too. How does separate shadow stack i= s specified by + * user is still being debated. Once that's settled, remove this part of t= he comment. + * This function simply returns 0 if shadow stack are not supported or if = separate shadow + * stack allocation is not needed (like in case of !CLONE_VM) + */ +unsigned long shstk_alloc_thread_stack(struct task_struct *tsk, + const struct kernel_clone_args *args) +{ + unsigned long addr, size; + + /* If shadow stack is not supported, return 0 */ + if (!cpu_supports_shadow_stack()) + return 0; + + /* + * If shadow stack is not enabled on the new thread, skip any + * switch to a new shadow stack. + */ + if (!is_shstk_enabled(tsk)) + return 0; + + /* + * For CLONE_VFORK the child will share the parents shadow stack. + * Set base =3D 0 and size =3D 0, this is special means to track this sta= te + * so the freeing logic run for child knows to leave it alone. + */ + if (args->flags & CLONE_VFORK) { + set_shstk_base(tsk, 0, 0); + return 0; + } + + /* + * For !CLONE_VM the child will use a copy of the parents shadow + * stack. + */ + if (!(args->flags & CLONE_VM)) + return 0; + + /* + * reaching here means, CLONE_VM was specified and thus a separate shadow + * stack is needed for new cloned thread. Note: below allocation is happe= ning + * using current mm. + */ + size =3D calc_shstk_size(args->stack_size); + addr =3D allocate_shadow_stack(0, size, 0, false); + if (IS_ERR_VALUE(addr)) + return addr; + + set_shstk_base(tsk, addr, size); + + return addr + size; +} + +void shstk_release(struct task_struct *tsk) +{ + unsigned long base =3D 0, size =3D 0; + /* If shadow stack is not supported or not enabled, nothing to release */ + if (!cpu_supports_shadow_stack() || !is_shstk_enabled(tsk)) + return; + + /* + * When fork() with CLONE_VM fails, the child (tsk) already has a + * shadow stack allocated, and exit_thread() calls this function to + * free it. In this case the parent (current) and the child share + * the same mm struct. Move forward only when they're same. + */ + if (!tsk->mm || tsk->mm !=3D current->mm) + return; + + /* + * We know shadow stack is enabled but if base is NULL, then + * this task is not managing its own shadow stack (CLONE_VFORK). So + * skip freeing it. + */ + base =3D get_shstk_base(tsk, &size); + if (!base) + return; + + vm_munmap(base, size); + set_shstk_base(tsk, 0, 0); +} --=20 2.34.1 From nobody Tue Feb 10 09:58:08 2026 Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) (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 CED9E233157 for ; Mon, 10 Mar 2025 14:53:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741618384; cv=none; b=nTRUyYoYaslxPWYw3Uy25f6yJL8HGnzoi4LTg2Ni89oXNXk+SThyzikCKEa+yx+FuPsZNh0cWPf892IrvVAd2FUY1MnU0pErjaAtb0ygUGU/+XYhovl8fUqI1pR9kdTbX17F+iwuMFuCdWX79ExYkm+00k+7DkBH5gabeh5XQ/I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741618384; c=relaxed/simple; bh=6tij1ZY5YMYBXzl8yUkJ2kDZaNjjUiJRSlpStARzsww=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=CXqns4xC7KfaHNHuObPygyb48BOSxv3264wAlR1pdol6wklYXe9boaByhFiRQBq7QK32kYhjv+4cP0KplQvSJUP07KEu5kCM33t8jStjYTOybCRKP3zOpdlmNRCfPUFLLAyOg/nEe7qhVBbKAWlNL5O3oGGrPMq6rl4XsBOl7qE= 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=PLfl5b5w; arc=none smtp.client-ip=209.85.214.170 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 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="PLfl5b5w" Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-22548a28d0cso11724695ad.3 for ; Mon, 10 Mar 2025 07:53:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741618382; x=1742223182; darn=vger.kernel.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=Zfw9OtoVzzM7Z5oL2c+Ej/yDm51xyr1WNtqXJN3fx8s=; b=PLfl5b5wMQ8zWz3Jqeq8E3z1CJECjdkTEfwzmIPs7L+do8i6mjJqUmJUvZkxTwEqpx DGltGkP620pMRfEI14TY36ktd2LNKeqDY30oiCZblqYKrfmFv2JVBHJqk91FyUoDfymY RtrAE/uXspy9e3l0rshg5gCeUDRNfzA5Ls75k82CVrVmCpO4VbVuceBwyC3NJceNjOH2 8rdUopp4HgSyKFD4JjAXIRawlccILMo6f5ownl6QAdXBp0YrpTjo1lUpfOIYZBLJjNGW Hjw586MUsb2GAIlqD14PPWX3U43SxDUDupXN4P9i0TO58fwXMXoHyU1U/R0sSSThNK1X LHdw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741618382; x=1742223182; 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=Zfw9OtoVzzM7Z5oL2c+Ej/yDm51xyr1WNtqXJN3fx8s=; b=RFcE7muDP/JQuDbRjZbL6ssH2U36MXWpgzPJZd6C9YlHwpR/pjxsYsC4zgV0nEQeUZ hHGGrIhwV4Cfq1h9un5TmQL/7UdE2yrPGGOwEorf46hQHdLZ+RB7M+MNs79m+NxTj+TK O61eFFr8SZzqJATeRCwkPw62crsOeGbN8eC9RLlF+Qk82FOLVJCeamUMjuyZrkDcanRb g7cFCnhuH1QPU9f1k3nOC7E528/BF5StTa0eheteB0LFxvzkecin+ebdHtCWg0qB8CEE NFHbIV+Q91f0WN4fHWW1j2uQVb+PMxlwxnnpgBeTLxhA26V0NyAWRghL4n+T7Dki8GpF b7kQ== X-Gm-Message-State: AOJu0YzUL/yVlBkuer+eGXv1t86aevxqOQXeOLR2Hbpoqfs+p4OWScFF RSwOo3a3J1+GdDz+rsr5sN1V2Wos3XtkZfoQ9aXpV5+bwVOMMEuMiAYaxUr5N/A= X-Gm-Gg: ASbGncvz9amyApA7iAz36jWHrzZfRAu/dydH4Fyo8xS8xtmu1AzGc6GL9nXtbXuBzF5 ImB9JmvNzCzivIwikteM9omV82Oy5cAtPzlFaHI1iAndLplTIhAsvc8zEpYy3xev+qRnvU+2cWn 06gqR0XTI8Io2arr053ispxmyasncOtigYF1m+WaBvdSMj+qWnK0ZKjmpk/it0zle30Rc2cxB0d DOUDHvO2bDQ/KfWp6OcKyRzBBa7Ezz/Av2aO61b2W4ebe5AMerE2yZvmxxJUhKVFUgq2hUdlL0O QMyX7OL8IYs9ZQPa6Bjd+yPIjrNZ8o43VrWIbs0502wlT5xL0FnGvbWa7S+1M5Je5w== X-Google-Smtp-Source: AGHT+IHCKzm+H1WuyN4ioLJbIhvG1jyYj+k1eB31ZxZlrJTUkTC6vhrDB22oVaHxl5JQ/hZbQyLd3A== X-Received: by 2002:a05:6a20:430a:b0:1f0:e893:409f with SMTP id adf61e73a8af0-1f544b180dbmr23325177637.23.1741618381901; Mon, 10 Mar 2025 07:53:01 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-736d11d4600sm2890275b3a.116.2025.03.10.07.52.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 07:53:01 -0700 (PDT) From: Deepak Gupta Date: Mon, 10 Mar 2025 07:52:34 -0700 Subject: [PATCH v11 12/27] riscv: Implements arch agnostic shadow stack prctls Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250310-v5_user_cfi_series-v11-12-86b36cbfb910@rivosinc.com> References: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> In-Reply-To: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@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 , Jann Horn , Conor Dooley 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 Implement architecture agnostic prctls() interface for setting and getting shadow stack status. prctls implemented are PR_GET_SHADOW_STACK_STATUS, PR_SET_SHADOW_STACK_STATUS and PR_LOCK_SHADOW_STACK_STATUS. As part of PR_SET_SHADOW_STACK_STATUS/PR_GET_SHADOW_STACK_STATUS, only PR_SHADOW_STACK_ENABLE is implemented because RISCV allows each mode to write to their own shadow stack using `sspush` or `ssamoswap`. PR_LOCK_SHADOW_STACK_STATUS locks current configuration of shadow stack enabling. Signed-off-by: Deepak Gupta --- arch/riscv/include/asm/usercfi.h | 18 ++++++- arch/riscv/kernel/process.c | 8 +++ arch/riscv/kernel/usercfi.c | 107 +++++++++++++++++++++++++++++++++++= ++++ 3 files changed, 132 insertions(+), 1 deletion(-) diff --git a/arch/riscv/include/asm/usercfi.h b/arch/riscv/include/asm/user= cfi.h index 82d28ac98d76..c4dcd256f19a 100644 --- a/arch/riscv/include/asm/usercfi.h +++ b/arch/riscv/include/asm/usercfi.h @@ -7,6 +7,7 @@ =20 #ifndef __ASSEMBLY__ #include +#include =20 struct task_struct; struct kernel_clone_args; @@ -14,7 +15,8 @@ struct kernel_clone_args; #ifdef CONFIG_RISCV_USER_CFI struct cfi_status { unsigned long ubcfi_en : 1; /* Enable for backward cfi. */ - unsigned long rsvd : ((sizeof(unsigned long) * 8) - 1); + unsigned long ubcfi_locked : 1; + unsigned long rsvd : ((sizeof(unsigned long) * 8) - 2); unsigned long user_shdw_stk; /* Current user shadow stack pointer */ unsigned long shdw_stk_base; /* Base address of shadow stack */ unsigned long shdw_stk_size; /* size of shadow stack */ @@ -27,6 +29,12 @@ void set_shstk_base(struct task_struct *task, unsigned l= ong shstk_addr, unsigned unsigned long get_shstk_base(struct task_struct *task, unsigned long *size= ); void set_active_shstk(struct task_struct *task, unsigned long shstk_addr); bool is_shstk_enabled(struct task_struct *task); +bool is_shstk_locked(struct task_struct *task); +bool is_shstk_allocated(struct task_struct *task); +void set_shstk_lock(struct task_struct *task); +void set_shstk_status(struct task_struct *task, bool enable); + +#define PR_SHADOW_STACK_SUPPORTED_STATUS_MASK (PR_SHADOW_STACK_ENABLE) =20 #else =20 @@ -42,6 +50,14 @@ bool is_shstk_enabled(struct task_struct *task); =20 #define is_shstk_enabled(task) false =20 +#define is_shstk_locked(task) false + +#define is_shstk_allocated(task) false + +#define set_shstk_lock(task) + +#define set_shstk_status(task, enable) + #endif /* CONFIG_RISCV_USER_CFI */ =20 #endif /* __ASSEMBLY__ */ diff --git a/arch/riscv/kernel/process.c b/arch/riscv/kernel/process.c index 99acb6342a37..cd11667593fe 100644 --- a/arch/riscv/kernel/process.c +++ b/arch/riscv/kernel/process.c @@ -153,6 +153,14 @@ void start_thread(struct pt_regs *regs, unsigned long = pc, regs->epc =3D pc; regs->sp =3D sp; =20 + /* + * clear shadow stack state on exec. + * libc will set it later via prctl. + */ + set_shstk_status(current, false); + set_shstk_base(current, 0, 0); + set_active_shstk(current, 0); + #ifdef CONFIG_64BIT regs->status &=3D ~SR_UXL; =20 diff --git a/arch/riscv/kernel/usercfi.c b/arch/riscv/kernel/usercfi.c index 73cf87dab186..37d6fb8144e7 100644 --- a/arch/riscv/kernel/usercfi.c +++ b/arch/riscv/kernel/usercfi.c @@ -24,6 +24,16 @@ bool is_shstk_enabled(struct task_struct *task) return task->thread_info.user_cfi_state.ubcfi_en ? true : false; } =20 +bool is_shstk_allocated(struct task_struct *task) +{ + return task->thread_info.user_cfi_state.shdw_stk_base ? true : false; +} + +bool is_shstk_locked(struct task_struct *task) +{ + return task->thread_info.user_cfi_state.ubcfi_locked ? true : false; +} + void set_shstk_base(struct task_struct *task, unsigned long shstk_addr, un= signed long size) { task->thread_info.user_cfi_state.shdw_stk_base =3D shstk_addr; @@ -42,6 +52,23 @@ void set_active_shstk(struct task_struct *task, unsigned= long shstk_addr) task->thread_info.user_cfi_state.user_shdw_stk =3D shstk_addr; } =20 +void set_shstk_status(struct task_struct *task, bool enable) +{ + task->thread_info.user_cfi_state.ubcfi_en =3D enable ? 1 : 0; + + if (enable) + task->thread.envcfg |=3D ENVCFG_SSE; + else + task->thread.envcfg &=3D ~ENVCFG_SSE; + + csr_write(CSR_ENVCFG, task->thread.envcfg); +} + +void set_shstk_lock(struct task_struct *task) +{ + task->thread_info.user_cfi_state.ubcfi_locked =3D 1; +} + /* * If size is 0, then to be compatible with regular stack we want it to be= as big as * regular stack. Else PAGE_ALIGN it and return back @@ -262,3 +289,83 @@ void shstk_release(struct task_struct *tsk) vm_munmap(base, size); set_shstk_base(tsk, 0, 0); } + +int arch_get_shadow_stack_status(struct task_struct *t, unsigned long __us= er *status) +{ + unsigned long bcfi_status =3D 0; + + if (!cpu_supports_shadow_stack()) + return -EINVAL; + + /* this means shadow stack is enabled on the task */ + bcfi_status |=3D (is_shstk_enabled(t) ? PR_SHADOW_STACK_ENABLE : 0); + + return copy_to_user(status, &bcfi_status, sizeof(bcfi_status)) ? -EFAULT = : 0; +} + +int arch_set_shadow_stack_status(struct task_struct *t, unsigned long stat= us) +{ + unsigned long size =3D 0, addr =3D 0; + bool enable_shstk =3D false; + + if (!cpu_supports_shadow_stack()) + return -EINVAL; + + /* Reject unknown flags */ + if (status & ~PR_SHADOW_STACK_SUPPORTED_STATUS_MASK) + return -EINVAL; + + /* bcfi status is locked and further can't be modified by user */ + if (is_shstk_locked(t)) + return -EINVAL; + + enable_shstk =3D status & PR_SHADOW_STACK_ENABLE; + /* Request is to enable shadow stack and shadow stack is not enabled alre= ady */ + if (enable_shstk && !is_shstk_enabled(t)) { + /* shadow stack was allocated and enable request again + * no need to support such usecase and return EINVAL. + */ + if (is_shstk_allocated(t)) + return -EINVAL; + + size =3D calc_shstk_size(0); + addr =3D allocate_shadow_stack(0, size, 0, false); + if (IS_ERR_VALUE(addr)) + return -ENOMEM; + set_shstk_base(t, addr, size); + set_active_shstk(t, addr + size); + } + + /* + * If a request to disable shadow stack happens, let's go ahead and relea= se it + * Although, if CLONE_VFORKed child did this, then in that case we will e= nd up + * not releasing the shadow stack (because it might be needed in parent).= Although + * we will disable it for VFORKed child. And if VFORKed child tries to en= able again + * then in that case, it'll get entirely new shadow stack because followi= ng condition + * are true + * - shadow stack was not enabled for vforked child + * - shadow stack base was anyways pointing to 0 + * This shouldn't be a big issue because we want parent to have availabil= ity of shadow + * stack whenever VFORKed child releases resources via exit or exec but a= t the same + * time we want VFORKed child to break away and establish new shadow stac= k if it desires + * + */ + if (!enable_shstk) + shstk_release(t); + + set_shstk_status(t, enable_shstk); + return 0; +} + +int arch_lock_shadow_stack_status(struct task_struct *task, + unsigned long arg) +{ + /* If shtstk not supported or not enabled on task, nothing to lock here */ + if (!cpu_supports_shadow_stack() || + !is_shstk_enabled(task) || arg !=3D 0) + return -EINVAL; + + set_shstk_lock(task); + + return 0; +} --=20 2.34.1 From nobody Tue Feb 10 09:58:08 2026 Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) (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 55DB8233152 for ; Mon, 10 Mar 2025 14:53:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741618387; cv=none; b=iMF5VHP43N2r6TGLhGS/6cRf/I/1ojmmMDYQXvALf0vSD49m64554OJZqxhOZT7Svwf1/qXZm6YjMpOyxqm55ozBZdqAXDBUwgcmr0h7kLcG6/Mhv8u5rj9qSNE7Rktk4t+2d7EQ894j6dBret+Cqg/60m9HgAkA17ymebGLjRo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741618387; c=relaxed/simple; bh=Xs2uqRxouRI9SPyjKajL1/XQBsSIyAcRzD4AuzRwjnA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=h/J7gy4hymz9Dl67FPpBhfkTTnfIuvp+BNQRoHQbDE2Ys7n1+EDkVv7r2SoS3H7d5aOpFhmI9EJPNAS+JLAUzHI6RmjyOXjOQYeGQ/QE9YxV8XeNy/c3WP4fy5ajh4BSgxOzZev1eQBVoRyXg0V6VjojVHvzSQmqeBTUoInUuGQ= 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=QdGRLpF/; arc=none smtp.client-ip=209.85.214.170 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 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="QdGRLpF/" Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-22334203781so95786815ad.0 for ; Mon, 10 Mar 2025 07:53:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741618384; x=1742223184; darn=vger.kernel.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=n4wOXS0/20DvZRxruHD7mFnd6lRzln/dV2wTFJ6STYc=; b=QdGRLpF/3DEKTvDtWhuo96wyDLxKIqVCUwQFz6TO1lu0kdNcy5h1ARebUlvitShxTu To/8usgApBHdIJhBJ8kFBVe/1Z3/sL0Wa2xCVNiAMz42FwfEx8Ldl9PYADR6/Xobzs0S 29s77GiKin4lSSiTVbsnZLu2YVU9g0gKQnfmODW9JpSxCSIEgouPIGjWzvdAVozIw7Yw kKohBJmckUYgqAZRt724L5B36wRWQLYy5zTQNfYwW2YBMMLzJw+0PfgVFHF5iGMvUBhP NPPyqZGrh730dKTi3TBcsa35u3Du0klf8H4XPCrcBDJTa+Jb7FgB2dhAfrxIjGCijHZ9 x4+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741618384; x=1742223184; 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=n4wOXS0/20DvZRxruHD7mFnd6lRzln/dV2wTFJ6STYc=; b=iHHbkJ7sfLmrX0hraGMr5SOkCnCgDvY02VEGy+yIenlOd/On6gPQpR4FS01Z4YhDuf B863MlCP3KiqhnPFo37JTH7g7W64Ah1cwxQsN7K4JrHBO9NEOK/2fmO1dv28rqqZesY4 YQz4m00yx4/6fKFO0TeU4180puElKwCyQXlpxm4kGROKGIVK0hTRDFh9bLFuWI70bvTI AivajRk/OlyIBW6FDd7xy4lx7PipkhrhXdjTTPGPbZ+ak1p8oXL6553OUf/7hI3WEYBF D1RhKcp79cvLI+S1WH3bcViIkAsBNSn52sOAKcv/Kn2O0+o9R5Hgc7rAsGtx3YJhaT2T WTgQ== X-Gm-Message-State: AOJu0Yzh0V0EMezmYBQuUhTjbfHnOt7YZNnDHPmZWZNmjrgKR8Rbamy+ 6OjrmoRLQIcEyStImDMa5dENPP4GlwLXQXRlvxjluH0IL5awlKeMZbwcOj1bnsA= X-Gm-Gg: ASbGncuiziG09BXKDxgNF7vSl2ejnKqsiR53y88WF5E3qWnMJzrNCHzgeYMlurydyf5 zuhv5jl7Xzbg1zRRWwA8B6NxSyHungSPKGuHEnzrsqdTFF/SbSr9UURSw5dFQeRvVHpXp9X8BFL q9qrJzBodnjN+WxaHSPU8Zk2OIGvKSfd2k+j27vAJFyifeoPWM+BFlopLHXUPuLs2g9M5656vN6 xSRlQp7Fy4xLlKXlOP8G7X5LJhqrAdsjdK0txPI0dx2pF9U+TSKNCC+Qo/Hs3xI/iZnnqC2EqZX QM2UH+DAjzYrZEye8sn+WsopmdhK9hkuSagI6hMt+D3Y1/jKyTyuQ4DmCOimRuhL1Q== X-Google-Smtp-Source: AGHT+IFPcCScpuJQuODEjO5ogKF+7rsG+dE6AGDUyxV2l5MZI+A8WNThCUXQtc0yXYjGKQnUtdvO1A== X-Received: by 2002:a05:6a00:13a5:b0:736:9f2e:1357 with SMTP id d2e1a72fcca58-736bc0d54eemr15283735b3a.12.1741618384358; Mon, 10 Mar 2025 07:53:04 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-736d11d4600sm2890275b3a.116.2025.03.10.07.53.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 07:53:04 -0700 (PDT) From: Deepak Gupta Date: Mon, 10 Mar 2025 07:52:35 -0700 Subject: [PATCH v11 13/27] prctl: arch-agnostic prctl for indirect branch tracking Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250310-v5_user_cfi_series-v11-13-86b36cbfb910@rivosinc.com> References: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> In-Reply-To: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@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 , Jann Horn , Conor Dooley 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 Three architectures (x86, aarch64, riscv) have support for indirect branch tracking feature in a very similar fashion. On a very high level, indirect branch tracking is a CPU feature where CPU tracks branches which uses memory operand to perform control transfer in program. As part of this tracking on indirect branches, CPU goes in a state where it expects a landing pad instr on target and if not found then CPU raises some fault (architecture dependent) x86 landing pad instr - `ENDBRANCH` aarch64 landing pad instr - `BTI` riscv landing instr - `lpad` Given that three major arches have support for indirect branch tracking, This patch makes `prctl` for indirect branch tracking arch agnostic. To allow userspace to enable this feature for itself, following prtcls are defined: - PR_GET_INDIR_BR_LP_STATUS: Gets current configured status for indirect branch tracking. - PR_SET_INDIR_BR_LP_STATUS: Sets a configuration for indirect branch tracking. Following status options are allowed - PR_INDIR_BR_LP_ENABLE: Enables indirect branch tracking on user thread. - PR_INDIR_BR_LP_DISABLE; Disables indirect branch tracking on user thread. - PR_LOCK_INDIR_BR_LP_STATUS: Locks configured status for indirect branch tracking for user thread. Signed-off-by: Deepak Gupta Reviewed-by: Mark Brown --- arch/riscv/include/asm/usercfi.h | 16 ++++++++- arch/riscv/kernel/entry.S | 2 +- arch/riscv/kernel/process.c | 5 +++ arch/riscv/kernel/usercfi.c | 76 ++++++++++++++++++++++++++++++++++++= ++++ include/linux/cpu.h | 4 +++ include/uapi/linux/prctl.h | 27 ++++++++++++++ kernel/sys.c | 30 ++++++++++++++++ 7 files changed, 158 insertions(+), 2 deletions(-) diff --git a/arch/riscv/include/asm/usercfi.h b/arch/riscv/include/asm/user= cfi.h index c4dcd256f19a..a8cec7c14d1d 100644 --- a/arch/riscv/include/asm/usercfi.h +++ b/arch/riscv/include/asm/usercfi.h @@ -16,7 +16,9 @@ struct kernel_clone_args; struct cfi_status { unsigned long ubcfi_en : 1; /* Enable for backward cfi. */ unsigned long ubcfi_locked : 1; - unsigned long rsvd : ((sizeof(unsigned long) * 8) - 2); + unsigned long ufcfi_en : 1; /* Enable for forward cfi. Note that ELP goes= in sstatus */ + unsigned long ufcfi_locked : 1; + unsigned long rsvd : ((sizeof(unsigned long) * 8) - 4); unsigned long user_shdw_stk; /* Current user shadow stack pointer */ unsigned long shdw_stk_base; /* Base address of shadow stack */ unsigned long shdw_stk_size; /* size of shadow stack */ @@ -33,6 +35,10 @@ bool is_shstk_locked(struct task_struct *task); bool is_shstk_allocated(struct task_struct *task); void set_shstk_lock(struct task_struct *task); void set_shstk_status(struct task_struct *task, bool enable); +bool is_indir_lp_enabled(struct task_struct *task); +bool is_indir_lp_locked(struct task_struct *task); +void set_indir_lp_status(struct task_struct *task, bool enable); +void set_indir_lp_lock(struct task_struct *task); =20 #define PR_SHADOW_STACK_SUPPORTED_STATUS_MASK (PR_SHADOW_STACK_ENABLE) =20 @@ -58,6 +64,14 @@ void set_shstk_status(struct task_struct *task, bool ena= ble); =20 #define set_shstk_status(task, enable) =20 +#define is_indir_lp_enabled(task) false + +#define is_indir_lp_locked(task) false + +#define set_indir_lp_status(task, enable) + +#define set_indir_lp_lock(task) + #endif /* CONFIG_RISCV_USER_CFI */ =20 #endif /* __ASSEMBLY__ */ diff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S index 68c99124ea55..00494b54ff4a 100644 --- a/arch/riscv/kernel/entry.S +++ b/arch/riscv/kernel/entry.S @@ -143,7 +143,7 @@ SYM_CODE_START(handle_exception) * Disable the FPU/Vector to detect illegal usage of floating point * or vector in kernel space. */ - li t0, SR_SUM | SR_FS_VS + li t0, SR_SUM | SR_FS_VS | SR_ELP =20 REG_L s0, TASK_TI_USER_SP(tp) csrrc s1, CSR_STATUS, t0 diff --git a/arch/riscv/kernel/process.c b/arch/riscv/kernel/process.c index cd11667593fe..4587201dd81d 100644 --- a/arch/riscv/kernel/process.c +++ b/arch/riscv/kernel/process.c @@ -160,6 +160,11 @@ void start_thread(struct pt_regs *regs, unsigned long = pc, set_shstk_status(current, false); set_shstk_base(current, 0, 0); set_active_shstk(current, 0); + /* + * disable indirect branch tracking on exec. + * libc will enable it later via prctl. + */ + set_indir_lp_status(current, false); =20 #ifdef CONFIG_64BIT regs->status &=3D ~SR_UXL; diff --git a/arch/riscv/kernel/usercfi.c b/arch/riscv/kernel/usercfi.c index 37d6fb8144e7..3a66f149a4ef 100644 --- a/arch/riscv/kernel/usercfi.c +++ b/arch/riscv/kernel/usercfi.c @@ -69,6 +69,32 @@ void set_shstk_lock(struct task_struct *task) task->thread_info.user_cfi_state.ubcfi_locked =3D 1; } =20 +bool is_indir_lp_enabled(struct task_struct *task) +{ + return task->thread_info.user_cfi_state.ufcfi_en ? true : false; +} + +bool is_indir_lp_locked(struct task_struct *task) +{ + return task->thread_info.user_cfi_state.ufcfi_locked ? true : false; +} + +void set_indir_lp_status(struct task_struct *task, bool enable) +{ + task->thread_info.user_cfi_state.ufcfi_en =3D enable ? 1 : 0; + + if (enable) + task->thread.envcfg |=3D ENVCFG_LPE; + else + task->thread.envcfg &=3D ~ENVCFG_LPE; + + csr_write(CSR_ENVCFG, task->thread.envcfg); +} + +void set_indir_lp_lock(struct task_struct *task) +{ + task->thread_info.user_cfi_state.ufcfi_locked =3D 1; +} /* * If size is 0, then to be compatible with regular stack we want it to be= as big as * regular stack. Else PAGE_ALIGN it and return back @@ -369,3 +395,53 @@ int arch_lock_shadow_stack_status(struct task_struct *= task, =20 return 0; } + +int arch_get_indir_br_lp_status(struct task_struct *t, unsigned long __use= r *status) +{ + unsigned long fcfi_status =3D 0; + + if (!cpu_supports_indirect_br_lp_instr()) + return -EINVAL; + + /* indirect branch tracking is enabled on the task or not */ + fcfi_status |=3D (is_indir_lp_enabled(t) ? PR_INDIR_BR_LP_ENABLE : 0); + + return copy_to_user(status, &fcfi_status, sizeof(fcfi_status)) ? -EFAULT = : 0; +} + +int arch_set_indir_br_lp_status(struct task_struct *t, unsigned long statu= s) +{ + bool enable_indir_lp =3D false; + + if (!cpu_supports_indirect_br_lp_instr()) + return -EINVAL; + + /* indirect branch tracking is locked and further can't be modified by us= er */ + if (is_indir_lp_locked(t)) + return -EINVAL; + + /* Reject unknown flags */ + if (status & ~PR_INDIR_BR_LP_ENABLE) + return -EINVAL; + + enable_indir_lp =3D (status & PR_INDIR_BR_LP_ENABLE) ? true : false; + set_indir_lp_status(t, enable_indir_lp); + + return 0; +} + +int arch_lock_indir_br_lp_status(struct task_struct *task, + unsigned long arg) +{ + /* + * If indirect branch tracking is not supported or not enabled on task, + * nothing to lock here + */ + if (!cpu_supports_indirect_br_lp_instr() || + !is_indir_lp_enabled(task) || arg !=3D 0) + return -EINVAL; + + set_indir_lp_lock(task); + + return 0; +} diff --git a/include/linux/cpu.h b/include/linux/cpu.h index 6a0a8f1c7c90..fb0c394430c6 100644 --- a/include/linux/cpu.h +++ b/include/linux/cpu.h @@ -204,4 +204,8 @@ static inline bool cpu_mitigations_auto_nosmt(void) } #endif =20 +int arch_get_indir_br_lp_status(struct task_struct *t, unsigned long __use= r *status); +int arch_set_indir_br_lp_status(struct task_struct *t, unsigned long statu= s); +int arch_lock_indir_br_lp_status(struct task_struct *t, unsigned long stat= us); + #endif /* _LINUX_CPU_H_ */ diff --git a/include/uapi/linux/prctl.h b/include/uapi/linux/prctl.h index 5c6080680cb2..6cd90460cbad 100644 --- a/include/uapi/linux/prctl.h +++ b/include/uapi/linux/prctl.h @@ -353,4 +353,31 @@ struct prctl_mm_map { */ #define PR_LOCK_SHADOW_STACK_STATUS 76 =20 +/* + * Get the current indirect branch tracking configuration for the current + * thread, this will be the value configured via PR_SET_INDIR_BR_LP_STATUS. + */ +#define PR_GET_INDIR_BR_LP_STATUS 77 + +/* + * Set the indirect branch tracking configuration. PR_INDIR_BR_LP_ENABLE w= ill + * enable cpu feature for user thread, to track all indirect branches and = ensure + * they land on arch defined landing pad instruction. + * x86 - If enabled, an indirect branch must land on `ENDBRANCH` instructi= on. + * arch64 - If enabled, an indirect branch must land on `BTI` instruction. + * riscv - If enabled, an indirect branch must land on `lpad` instruction. + * PR_INDIR_BR_LP_DISABLE will disable feature for user thread and indirect + * branches will no more be tracked by cpu to land on arch defined landing= pad + * instruction. + */ +#define PR_SET_INDIR_BR_LP_STATUS 78 +# define PR_INDIR_BR_LP_ENABLE (1UL << 0) + +/* + * Prevent further changes to the specified indirect branch tracking + * configuration. All bits may be locked via this call, including + * undefined bits. + */ +#define PR_LOCK_INDIR_BR_LP_STATUS 79 + #endif /* _LINUX_PRCTL_H */ diff --git a/kernel/sys.c b/kernel/sys.c index cb366ff8703a..f347f3518d0b 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -2336,6 +2336,21 @@ int __weak arch_lock_shadow_stack_status(struct task= _struct *t, unsigned long st return -EINVAL; } =20 +int __weak arch_get_indir_br_lp_status(struct task_struct *t, unsigned lon= g __user *status) +{ + return -EINVAL; +} + +int __weak arch_set_indir_br_lp_status(struct task_struct *t, unsigned lon= g status) +{ + return -EINVAL; +} + +int __weak arch_lock_indir_br_lp_status(struct task_struct *t, unsigned lo= ng status) +{ + return -EINVAL; +} + #define PR_IO_FLUSHER (PF_MEMALLOC_NOIO | PF_LOCAL_THROTTLE) =20 #ifdef CONFIG_ANON_VMA_NAME @@ -2811,6 +2826,21 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, a= rg2, unsigned long, arg3, return -EINVAL; error =3D arch_lock_shadow_stack_status(me, arg2); break; + case PR_GET_INDIR_BR_LP_STATUS: + if (arg3 || arg4 || arg5) + return -EINVAL; + error =3D arch_get_indir_br_lp_status(me, (unsigned long __user *)arg2); + break; + case PR_SET_INDIR_BR_LP_STATUS: + if (arg3 || arg4 || arg5) + return -EINVAL; + error =3D arch_set_indir_br_lp_status(me, arg2); + break; + case PR_LOCK_INDIR_BR_LP_STATUS: + if (arg3 || arg4 || arg5) + return -EINVAL; + error =3D arch_lock_indir_br_lp_status(me, arg2); + break; default: trace_task_prctl_unknown(option, arg2, arg3, arg4, arg5); error =3D -EINVAL; --=20 2.34.1 From nobody Tue Feb 10 09:58:08 2026 Received: from mail-pl1-f176.google.com (mail-pl1-f176.google.com [209.85.214.176]) (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 5E02D233D9C for ; Mon, 10 Mar 2025 14:53:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741618389; cv=none; b=V0sxDcwqM7f3ljOfCdsQGeW4PmW2G++R1V1qoHPmE3g98rhAKhWpmAT2FHyQiV+x4g7lOouQ+Q2M6FjCoQxsw7K+DVO/nu8CCd/dwFcuUtEISuH9tviW/idrM8f8uGGPC1xuA/pZzAMdhUcb31NzaUPV4vlAfe5SjxUHKOS5YlI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741618389; c=relaxed/simple; bh=mWDyYETp4wr+sZ2udIiiXn9veUStZJ0b+YrPzAFDVJE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=QRnhk/Dg/JhATj8HRwlWvGXf8gH//UeFsmgDFtbtbXIzgXvUUNtOwKYY9J1o4oLu9JgP7fa2Cl8lVQ+N9YnfFA6H9O/9+1TaSNcJBlphiyvEtz+xQfyMqYM7/3JYY4AuP2qjngb/y4MeLOjiYwzbOAfT0R4nubQYtEu/0X1tz6k= 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=ruC45iPE; arc=none smtp.client-ip=209.85.214.176 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 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="ruC45iPE" Received: by mail-pl1-f176.google.com with SMTP id d9443c01a7336-22349bb8605so78595565ad.0 for ; Mon, 10 Mar 2025 07:53:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741618387; x=1742223187; darn=vger.kernel.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=M49HWf8gzr6wWBf1Qtr4VOivQAEEQjfyuWgORGOVTSw=; b=ruC45iPEggx6iINPgbVg/Jjk6tyrE36Kku7m4ofEcj0qNu+4jUkWMEzYe/sZKYzE0E gIV3hoyTmrg1sl0dKYyXjt73D29j0c3lnirnRbKaDag37wAlT9ORPeqHRQSKYq/UhkxZ pyLVDXddoxiaXA0HJKpWfCGJXWeAEp8d6dJ/c+qSmFbIdDIyqK8XYqtjO5EUt2vjh54x Kf3auTTaVrx54nLF5mPxAMqJs4yjR5A7bkcp2jU1hX3ISWhmQGxRIb0G7Ok9N7Wb8hpd b69AwO0wiyXkxv1eE/FKyYIT9Hhh682WhtD4dDY6B2M/XeKRy81Ye4Nm9od2nlhTaIIk Nd1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741618387; x=1742223187; 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=M49HWf8gzr6wWBf1Qtr4VOivQAEEQjfyuWgORGOVTSw=; b=NM0Xrx7u0d6ND6Cc4O+j+4QpquUEO3jTLwhgAhjijE+AKXWOrU9Iu798Iqf2H2yqQX pCKsmEX/s9+fgHGNo8LxR3r/pfEV7nkbEOsd3f6FYSbn0SoVnotHMb2K8HRe7otBcg4M uFP23tRIak7PG+7DuBE7+sVs/CztwsX4n2c9NOewVXSSfyO0lPGM4Ik3OG99cH9tOkmj HoEAiWWeadxNgQUpkM+1JRpIgm4q68ykOmOgkrIDJRJhuWR9mA08I73c6yyCbcEgegv6 xlGN2Sc/IMMHfwNbxNwx7OWVWAy5qArLccChTFC9VTriyHUNvNRxLMGgWmaAp2b+Mp7V yNHw== X-Gm-Message-State: AOJu0YxkQgIR4VG795DUOssnLTYNZWvi1XXN8U+iASHM1bY3zFXwUlu5 fthlXFHgVCVIdS1RoW7spTqCD/4bW3F0q60+TvsMGu//ggfiKsWwbYIX+uuKfxk= X-Gm-Gg: ASbGncuMYRm9fFU/0qdMKTk7h0qgDD3uqhfbm+brhNEJovxbq9GsVJxCgFCOMHo2uBV V9qu2LxHT+azUR6msvTd85HawpYtJLagL00xjDIlaqt56h/BO78aAmhAv7SlncanUTDqahu28sL gZJVScuhxqcz2YejWZtvMMJjUvCAG/25c2lph83BSR2QLC6P0jf6IVkuKSgx0YaTFdQ5EF2mj+M EeaZg4Ay8De3smIF4iZh2ehNcr1nce5bOPeomYS/82503PHsahJ8egUd3ndPyU2bhdzlljwWe/7 BoUIJDAaY9MgML0rk0HxuDSNqlZZAhxUBeyM2EUCkzbegRNQHl7rw+8= X-Google-Smtp-Source: AGHT+IFsZxG1TWVrNQMrqkKpccFn2JJPQ/WSJPQEXgIDTpBCMBrYmD46kiCZqmMB33SVFrCrhoe36g== X-Received: by 2002:a05:6a20:2d2c:b0:1f5:8221:d68c with SMTP id adf61e73a8af0-1f58221fee5mr5284959637.3.1741618386923; Mon, 10 Mar 2025 07:53:06 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-736d11d4600sm2890275b3a.116.2025.03.10.07.53.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 07:53:06 -0700 (PDT) From: Deepak Gupta Date: Mon, 10 Mar 2025 07:52:36 -0700 Subject: [PATCH v11 14/27] riscv/traps: Introduce software check exception Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250310-v5_user_cfi_series-v11-14-86b36cbfb910@rivosinc.com> References: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> In-Reply-To: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@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 , Jann Horn , Conor Dooley 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 zicfiss / zicfilp introduces a new exception to priv isa `software check exception` with cause code =3D 18. This patch implements software check exception. Additionally it implements a cfi violation handler which checks for code in xtval. If xtval=3D2, it means that sw check exception happened because of an indirect branch not landing on 4 byte aligned PC or not landing on `lpad` instruction or label value embedded in `lpad` not matching label value setup in `x7`. If xtval=3D3, it means that sw check exception happened because of mismatch between link register (x1 or x5) and top of shadow stack (on execution of `sspopchk`). In case of cfi violation, SIGSEGV is raised with code=3DSEGV_CPERR. SEGV_CPERR was introduced by x86 shadow stack patches. Signed-off-by: Deepak Gupta Reviewed-by: Zong Li --- arch/riscv/include/asm/asm-prototypes.h | 1 + arch/riscv/include/asm/entry-common.h | 2 ++ arch/riscv/kernel/entry.S | 3 +++ arch/riscv/kernel/traps.c | 43 +++++++++++++++++++++++++++++= ++++ 4 files changed, 49 insertions(+) diff --git a/arch/riscv/include/asm/asm-prototypes.h b/arch/riscv/include/a= sm/asm-prototypes.h index cd627ec289f1..5a27cefd7805 100644 --- a/arch/riscv/include/asm/asm-prototypes.h +++ b/arch/riscv/include/asm/asm-prototypes.h @@ -51,6 +51,7 @@ DECLARE_DO_ERROR_INFO(do_trap_ecall_u); DECLARE_DO_ERROR_INFO(do_trap_ecall_s); DECLARE_DO_ERROR_INFO(do_trap_ecall_m); DECLARE_DO_ERROR_INFO(do_trap_break); +DECLARE_DO_ERROR_INFO(do_trap_software_check); =20 asmlinkage void handle_bad_stack(struct pt_regs *regs); asmlinkage void do_page_fault(struct pt_regs *regs); diff --git a/arch/riscv/include/asm/entry-common.h b/arch/riscv/include/asm= /entry-common.h index b28ccc6cdeea..34ed149af5d1 100644 --- a/arch/riscv/include/asm/entry-common.h +++ b/arch/riscv/include/asm/entry-common.h @@ -40,4 +40,6 @@ static inline int handle_misaligned_store(struct pt_regs = *regs) } #endif =20 +bool handle_user_cfi_violation(struct pt_regs *regs); + #endif /* _ASM_RISCV_ENTRY_COMMON_H */ diff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S index 00494b54ff4a..9c00cac3f6f2 100644 --- a/arch/riscv/kernel/entry.S +++ b/arch/riscv/kernel/entry.S @@ -472,6 +472,9 @@ SYM_DATA_START_LOCAL(excp_vect_table) RISCV_PTR do_page_fault /* load page fault */ RISCV_PTR do_trap_unknown RISCV_PTR do_page_fault /* store page fault */ + RISCV_PTR do_trap_unknown /* cause=3D16 */ + RISCV_PTR do_trap_unknown /* cause=3D17 */ + RISCV_PTR do_trap_software_check /* cause=3D18 is sw check exception */ SYM_DATA_END_LABEL(excp_vect_table, SYM_L_LOCAL, excp_vect_table_end) =20 #ifndef CONFIG_MMU diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c index 8ff8e8b36524..3f7709f4595a 100644 --- a/arch/riscv/kernel/traps.c +++ b/arch/riscv/kernel/traps.c @@ -354,6 +354,49 @@ void do_trap_ecall_u(struct pt_regs *regs) =20 } =20 +#define CFI_TVAL_FCFI_CODE 2 +#define CFI_TVAL_BCFI_CODE 3 +/* handle cfi violations */ +bool handle_user_cfi_violation(struct pt_regs *regs) +{ + bool ret =3D false; + unsigned long tval =3D csr_read(CSR_TVAL); + + if ((tval =3D=3D CFI_TVAL_FCFI_CODE && cpu_supports_indirect_br_lp_instr(= )) || + (tval =3D=3D CFI_TVAL_BCFI_CODE && cpu_supports_shadow_stack())) { + do_trap_error(regs, SIGSEGV, SEGV_CPERR, regs->epc, + "Oops - control flow violation"); + ret =3D true; + } + + return ret; +} + +/* + * software check exception is defined with risc-v cfi spec. Software check + * exception is raised when:- + * a) An indirect branch doesn't land on 4 byte aligned PC or `lpad` + * instruction or `label` value programmed in `lpad` instr doesn't + * match with value setup in `x7`. reported code in `xtval` is 2. + * b) `sspopchk` instruction finds a mismatch between top of shadow stack = (ssp) + * and x1/x5. reported code in `xtval` is 3. + */ +asmlinkage __visible __trap_section void do_trap_software_check(struct pt_= regs *regs) +{ + if (user_mode(regs)) { + irqentry_enter_from_user_mode(regs); + + /* not a cfi violation, then merge into flow of unknown trap handler */ + if (!handle_user_cfi_violation(regs)) + do_trap_unknown(regs); + + irqentry_exit_to_user_mode(regs); + } else { + /* sw check exception coming from kernel is a bug in kernel */ + die(regs, "Kernel BUG"); + } +} + #ifdef CONFIG_MMU asmlinkage __visible noinstr void do_page_fault(struct pt_regs *regs) { --=20 2.34.1 From nobody Tue Feb 10 09:58:08 2026 Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) (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 46A4E2343C0 for ; Mon, 10 Mar 2025 14:53:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741618392; cv=none; b=on9DmMn85TEQjgLPYJR64Bh5GH3KP+W+bFUnHi3TUMsJP3oa1JE6qp4Wupd/GBedcm+lqRm8P98juHMbaR8VK/echHuFZevQRIuTKkNeRxWfKKw824ZQ5mHcJUyLhGtS3D16D04uPCRWMV/BSEwDSB5VsjOLC9xKmZYfB5GTC8U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741618392; c=relaxed/simple; bh=NNu0KVfl+doCLPAFQT7Oz1ESYLTNqUHW5G9P/tWXfKw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=knCZPqzvsAX2KMA1OB/bAyrqMYEcgXp2xMRn+E/kmS8qwzkV0XZ6/w33EPr3FemReEjMtnZXem6WomgArc82RIH0SZbGUBVJn9ZoP/70WNoTVjvmI4Beb92ypRDN7yPfW5vdYq9Us1OwS1/djGWzH4NFfHz/Wbu8JN7GS7MvhPU= 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=gLOeU6eE; arc=none smtp.client-ip=209.85.214.181 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 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="gLOeU6eE" Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-223a7065ff8so3881825ad.0 for ; Mon, 10 Mar 2025 07:53:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741618389; x=1742223189; darn=vger.kernel.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=N78rd5bu5L3Nb7UCylb6NhXzPkib+ej1wIZfC16ALRg=; b=gLOeU6eE00N32D5NtmbA8LlKl3+5i7r46QdjDe4EJjdLupBy408+rKoH+dMWDq56ih Oq6G8aoqDPK8r+HSd524k2d5g0JsSYDLj3bROjKlDrwgnXor8cJjwVDKqr1qyzEcY4NX IMeNqKRcrv5tt7GRwPh53Ifpt3gpLyafccJ8fxaX3yUL8stvDZKvyHxKUANNwo4y05Qj gvMn9WBifQ0EKT3UgQLPZ0gtRsZq22jB1IvtYlegspK1ZCK1yS63dG3MFDP2CexfsuqM 5JACUXJM14iZI/nnPAzo9I1yWtrduH8wrcCW10WsUFfmN0OpBR/+NygxC96SjYn7vOw0 HMCw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741618389; x=1742223189; 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=N78rd5bu5L3Nb7UCylb6NhXzPkib+ej1wIZfC16ALRg=; b=VEltZKkslA2XF33SAoh/4yj4kW84JUGYhlc4uNawldQDKEPbbjwbCAMCZEDFQG1Q7I UpzLmPZW5VmB0MXT/aP2yc5PyNCHNSC/ta2i4/kpAdWNS9eZiq011cY6oC03/o7lwRO5 bq/bRNRg1rZl83TYKP0kSNzVtvo41GCAVUJKz/yr1mLZU5kmiAwAWABylCQs4IYSW8hM wyz3n8zhPcEgyVmNW/C0XwEw3XoXN4ti4SsfZqR/SJ5EsMLxzBPl7DXuYZIHku9uPfqQ 3YLDjIdFr6uXIl71s1J6XwX0q+wT/SFyGOEAt0u+o2S+USI7MnqCdH5At59fiRCAopqE /n5g== X-Gm-Message-State: AOJu0Yz5kUBK4KCg/5KVERtUL7JeRPTYETZ5m7NTw/rIVjP6fQJXGdpD 2+cTm4N9ExFkr6SmVlZJ9ycTrjxqKRbSeEyqAIaneUwEdoffCkxcoov/InbckdY= X-Gm-Gg: ASbGncvTNxkAKbBtGyBeErz89CNf2WjhILVeiB3wYxDRxsM26CBfQkXpns/5bPdiqcA uF6KiyixQOqbHybHytoOnyO3bBw8eWMhGWzxo5IcQ/H9aPb9wsEfQACekxwEs3RLSUWgtgxtRck vD32vYT5rmnwkB0n1rBFLh9w8zZTPYjN4oiL5k/nsFa12iUvBGyXA45G+ndXxKIco7QclCNphkF CMIMdvmQkahOukcMLtrVp3v4cKvphMULD5xEyI5lLfFOlYxflSbiB/3qKas8RZNYvGougySKjXo yDmGYv/4acDMugicLONETDZ/imxXxnz3ykqZcflSlBgMarSEjz7SGrqJFspXkNpNgA== X-Google-Smtp-Source: AGHT+IFeD/HgCjO9LcRex1YUUmRruVxizMZhpwj5TvOctT1vNUnUY9bR7/1JUpOvPThhyzvn2X9ItQ== X-Received: by 2002:a05:6a00:1703:b0:736:a6e0:e669 with SMTP id d2e1a72fcca58-736aaad3bb2mr22481649b3a.20.1741618389332; Mon, 10 Mar 2025 07:53:09 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-736d11d4600sm2890275b3a.116.2025.03.10.07.53.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 07:53:08 -0700 (PDT) From: Deepak Gupta Date: Mon, 10 Mar 2025 07:52:37 -0700 Subject: [PATCH v11 15/27] riscv: signal: abstract header saving for setup_sigcontext Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250310-v5_user_cfi_series-v11-15-86b36cbfb910@rivosinc.com> References: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> In-Reply-To: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@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 , Jann Horn , Conor Dooley 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 X-Mailer: b4 0.14.0 From: Andy Chiu The function save_v_state() served two purposes. First, it saved extension context into the signal stack. Then, it constructed the extension header if there was no fault. The second part is independent of the extension itself. As a result, we can pull that part out, so future extensions may reuse it. This patch adds arch_ext_list and makes setup_sigcontext() go through all possible extensions' save() callback. The callback returns a positive value indicating the size of the successfully saved extension. Then the kernel proceeds to construct the header for that extension. The kernel skips an extension if it does not exist, or if the saving fails for some reasons. The error code is propagated out on the later case. This patch does not introduce any functional changes. Signed-off-by: Andy Chiu --- arch/riscv/include/asm/vector.h | 3 ++ arch/riscv/kernel/signal.c | 62 +++++++++++++++++++++++++++----------= ---- 2 files changed, 44 insertions(+), 21 deletions(-) diff --git a/arch/riscv/include/asm/vector.h b/arch/riscv/include/asm/vecto= r.h index e8a83f55be2b..05390538ea8a 100644 --- a/arch/riscv/include/asm/vector.h +++ b/arch/riscv/include/asm/vector.h @@ -407,6 +407,9 @@ static inline bool riscv_v_vstate_ctrl_user_allowed(voi= d) { return false; } #define riscv_v_thread_free(tsk) do {} while (0) #define riscv_v_setup_ctx_cache() do {} while (0) #define riscv_v_thread_alloc(tsk) do {} while (0) +#define get_cpu_vector_context() do {} while (0) +#define put_cpu_vector_context() do {} while (0) +#define riscv_v_vstate_set_restore(task, regs) do {} while (0) =20 #endif /* CONFIG_RISCV_ISA_V */ =20 diff --git a/arch/riscv/kernel/signal.c b/arch/riscv/kernel/signal.c index 94e905eea1de..80c70dccf09f 100644 --- a/arch/riscv/kernel/signal.c +++ b/arch/riscv/kernel/signal.c @@ -68,18 +68,19 @@ static long save_fp_state(struct pt_regs *regs, #define restore_fp_state(task, regs) (0) #endif =20 -#ifdef CONFIG_RISCV_ISA_V - -static long save_v_state(struct pt_regs *regs, void __user **sc_vec) +static long save_v_state(struct pt_regs *regs, void __user *sc_vec) { - struct __riscv_ctx_hdr __user *hdr; struct __sc_riscv_v_state __user *state; void __user *datap; long err; =20 - hdr =3D *sc_vec; - /* Place state to the user's signal context space after the hdr */ - state =3D (struct __sc_riscv_v_state __user *)(hdr + 1); + if (!IS_ENABLED(CONFIG_RISCV_ISA_V) || + !((has_vector() || has_xtheadvector()) && + riscv_v_vstate_query(regs))) + return 0; + + /* Place state to the user's signal context spac */ + state =3D (struct __sc_riscv_v_state __user *)sc_vec; /* Point datap right after the end of __sc_riscv_v_state */ datap =3D state + 1; =20 @@ -97,15 +98,11 @@ static long save_v_state(struct pt_regs *regs, void __u= ser **sc_vec) err |=3D __put_user((__force void *)datap, &state->v_state.datap); /* Copy the whole vector content to user space datap. */ err |=3D __copy_to_user(datap, current->thread.vstate.datap, riscv_v_vsiz= e); - /* Copy magic to the user space after saving all vector conetext */ - err |=3D __put_user(RISCV_V_MAGIC, &hdr->magic); - err |=3D __put_user(riscv_v_sc_size, &hdr->size); if (unlikely(err)) - return err; + return -EFAULT; =20 - /* Only progress the sv_vec if everything has done successfully */ - *sc_vec +=3D riscv_v_sc_size; - return 0; + /* Only return the size if everything has done successfully */ + return riscv_v_sc_size; } =20 /* @@ -142,10 +139,20 @@ static long __restore_v_state(struct pt_regs *regs, v= oid __user *sc_vec) */ return copy_from_user(current->thread.vstate.datap, datap, riscv_v_vsize); } -#else -#define save_v_state(task, regs) (0) -#define __restore_v_state(task, regs) (0) -#endif + +struct arch_ext_priv { + __u32 magic; + long (*save)(struct pt_regs *regs, void __user *sc_vec); +}; + +struct arch_ext_priv arch_ext_list[] =3D { + { + .magic =3D RISCV_V_MAGIC, + .save =3D &save_v_state, + }, +}; + +const size_t nr_arch_exts =3D ARRAY_SIZE(arch_ext_list); =20 static long restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc) @@ -276,7 +283,8 @@ static long setup_sigcontext(struct rt_sigframe __user = *frame, { struct sigcontext __user *sc =3D &frame->uc.uc_mcontext; struct __riscv_ctx_hdr __user *sc_ext_ptr =3D &sc->sc_extdesc.hdr; - long err; + struct arch_ext_priv *arch_ext; + long err, i, ext_size; =20 /* sc_regs is structured the same as the start of pt_regs */ err =3D __copy_to_user(&sc->sc_regs, regs, sizeof(sc->sc_regs)); @@ -284,8 +292,20 @@ static long setup_sigcontext(struct rt_sigframe __user= *frame, if (has_fpu()) err |=3D save_fp_state(regs, &sc->sc_fpregs); /* Save the vector state. */ - if ((has_vector() || has_xtheadvector()) && riscv_v_vstate_query(regs)) - err |=3D save_v_state(regs, (void __user **)&sc_ext_ptr); + for (i =3D 0; i < nr_arch_exts; i++) { + arch_ext =3D &arch_ext_list[i]; + if (!arch_ext->save) + continue; + + ext_size =3D arch_ext->save(regs, sc_ext_ptr + 1); + if (ext_size <=3D 0) { + err |=3D ext_size; + } else { + err |=3D __put_user(arch_ext->magic, &sc_ext_ptr->magic); + err |=3D __put_user(ext_size, &sc_ext_ptr->size); + sc_ext_ptr =3D (void *)sc_ext_ptr + ext_size; + } + } /* Write zero to fp-reserved space and check it on restore_sigcontext */ err |=3D __put_user(0, &sc->sc_extdesc.reserved); /* And put END __riscv_ctx_hdr at the end. */ --=20 2.34.1 From nobody Tue Feb 10 09:58:08 2026 Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) (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 A671B235C03 for ; Mon, 10 Mar 2025 14:53:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741618395; cv=none; b=tNAPmjFO5QTpJT2N6YIyZZvN8XoZ1yQNXYtjbBvnqD2MvGWbHDMuTV9wuGoJeZDBqmV9tKwpm5yqpXJxrg2wl9dpPtSVqE05FzlxIXHnUf1kAyNTtzT2VcD4EDN59V1AwUq7RTPnXA9T16gUhIK5mkxRilt1NTwCz136gNgE4Yw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741618395; c=relaxed/simple; bh=wI9o+6ZNrC0pPpw2dP6AWMAMeO7BYuPrKSYAS17pkaw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=DSL6IS/2gci/ktVc/eppL+jboKFcWwacxGUdQJwMSeRfnl/EZP9s5CUQrCHf5lZTwTSfJqh3e3uVAOuZ5HAWexBXJJFMIrRJm+o2UwZgMlkBeCcOr/X5VLUwNwhXyw/ZdU4XEppY1rY4Ao2uarmfNerBm09ISYDwCqCoxy5w96Q= 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=ZuBV8IA3; arc=none smtp.client-ip=209.85.214.172 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 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="ZuBV8IA3" Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-2239aa5da08so68485205ad.3 for ; Mon, 10 Mar 2025 07:53:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741618392; x=1742223192; darn=vger.kernel.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=FZizf8oZeAex4CEwHeMnGWZudkjFQvNzoIsL7CfIy1g=; b=ZuBV8IA32Jr8hTGvCFE0IdZw+QQptGtu+T10SXZ006poHGETlGncEheOGF4ZmhtYaZ P3UIDdBjIHIQtTJ6ahTL9bCYo2Kbdjr8hfOF8cRyrDOsVbcidlTtooTk/W4XC45As3j2 ZRfqdPgxKQkTXVHp6R1ftseF1c77bXMkMw6jSDPeIS2lt8w3R2ixh8d1977M+7EFgPEt YwTCUwEu9BmEOwmNsusRicJl9F6hVBbeXrsUonC4X4qcuBfAevSr1/OZEGwj4UxI0HAY RHSHHxQya+TgXVEAb8Q0WDSjgreVFPTPLCNt3YSzEVieiV+1ZLpE/Qh2A6gwMr5O7HFS pIOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741618392; x=1742223192; 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=FZizf8oZeAex4CEwHeMnGWZudkjFQvNzoIsL7CfIy1g=; b=AOfewGNQ/MPEuuC4kissfRNiRrqqS7usYCIBZT4jzJSxMhIZePZT51XL3UiFfpq1jV RBSW+6E66yIa+ya6u8HL7UKv7FhFb+rmpHyR6XSvpWCmHu/kz9oxDrLbd9jf7Ntj1rPT 5VZLJ5dTbgQ2Fd1NEYsF90A0ttcQfp9j3qC+MyzOSBtUHdl3afVi/ss9zw+IQZrEkg23 nKTn7rRFelstqXupC1u1q45ambpi0F9vEnzKdqV/6w9b0WGx5fr4itYvEOZAtgop06Ry G/ftpuSbwobdJ2wDC+WtBSyHKOcdA9dhtEKQXPN0+my5DLYllZUmNCIuO75CnPQmQ52w ZTkQ== X-Gm-Message-State: AOJu0Yznzw3OXaPtLnkEFVKr9eTQ0YrU6qARLVHuDBZmfmoGYBX03/vR hAepTPqIglcmuj7N3tWyFtd+xJqHb7hYJCWNytWjBakpLc+L6YDFPl0pup9NzQw= X-Gm-Gg: ASbGncvGYbbQ//ORKCO6t74VZcgncbo2EJYld+whUsaBy8HhfzTm79IikaglOa7MB43 mg+n2tzTv9jYs3kbQH068qiAa+/Ut6uvfYdOTFON137MhdFrk42KlrIEYkBBlUetyjb1rVwLSN7 GQUW2H8+RPFNl3DGrjKOleihANZrYUZbEAcYfUkBhyzunTXqLp0+2xc8futCFuRmKSwESzqivdE tw880dd/l48M52kjQ2kkp6odhSJQO7BaoZ8G/ItGbJpm1IE66SJCf82dC6VIJNxqRxdnSzPmdZb DWMjgwAv5gyLlg6H8se5pZNccHJKlDFxlTnpfHu5aULng73OmdIb0YU= X-Google-Smtp-Source: AGHT+IGtoWSwFQXp42Jt7uQ+iD9QmR3QbXX6mHvaYDmjRmHhCAWQmnMnksRJhwAUzL/h4lDxdU80yg== X-Received: by 2002:a05:6a21:4606:b0:1f5:7df9:f147 with SMTP id adf61e73a8af0-1f57df9f49dmr7383711637.40.1741618391807; Mon, 10 Mar 2025 07:53:11 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-736d11d4600sm2890275b3a.116.2025.03.10.07.53.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 07:53:11 -0700 (PDT) From: Deepak Gupta Date: Mon, 10 Mar 2025 07:52:38 -0700 Subject: [PATCH v11 16/27] riscv/signal: save and restore of shadow stack for signal Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250310-v5_user_cfi_series-v11-16-86b36cbfb910@rivosinc.com> References: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> In-Reply-To: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@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 , Jann Horn , Conor Dooley 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 , Andy Chiu X-Mailer: b4 0.14.0 Save shadow stack pointer in sigcontext structure while delivering signal. Restore shadow stack pointer from sigcontext on sigreturn. As part of save operation, kernel uses `ssamoswap` to save snapshot of current shadow stack on shadow stack itself (can be called as a save token). During restore on sigreturn, kernel retrieves token from top of shadow stack and validates it. This allows that user mode can't arbitrary pivot to any shadow stack address without having a token and thus provide strong security assurance between signaly delivery and sigreturn window. Use ABI compatible way of saving/restoring shadow stack pointer into signal stack. This follows what Vector extension, where extra registers are placed in a form of extension header + extension body in the stack. The extension header indicates the size of the extra architectural states plus the size of header itself, and a magic identifier of the extension. Then, the extensions body contains the new architectural states in the form defined by uapi. Signed-off-by: Andy Chiu Signed-off-by: Deepak Gupta --- arch/riscv/include/asm/usercfi.h | 10 ++++ arch/riscv/include/uapi/asm/ptrace.h | 4 ++ arch/riscv/include/uapi/asm/sigcontext.h | 1 + arch/riscv/kernel/signal.c | 80 ++++++++++++++++++++++++++++= ++++ arch/riscv/kernel/usercfi.c | 56 ++++++++++++++++++++++ 5 files changed, 151 insertions(+) diff --git a/arch/riscv/include/asm/usercfi.h b/arch/riscv/include/asm/user= cfi.h index a8cec7c14d1d..361f59edbdef 100644 --- a/arch/riscv/include/asm/usercfi.h +++ b/arch/riscv/include/asm/usercfi.h @@ -8,6 +8,7 @@ #ifndef __ASSEMBLY__ #include #include +#include =20 struct task_struct; struct kernel_clone_args; @@ -35,6 +36,9 @@ bool is_shstk_locked(struct task_struct *task); bool is_shstk_allocated(struct task_struct *task); void set_shstk_lock(struct task_struct *task); void set_shstk_status(struct task_struct *task, bool enable); +unsigned long get_active_shstk(struct task_struct *task); +int restore_user_shstk(struct task_struct *tsk, unsigned long shstk_ptr); +int save_user_shstk(struct task_struct *tsk, unsigned long *saved_shstk_pt= r); bool is_indir_lp_enabled(struct task_struct *task); bool is_indir_lp_locked(struct task_struct *task); void set_indir_lp_status(struct task_struct *task, bool enable); @@ -72,6 +76,12 @@ void set_indir_lp_lock(struct task_struct *task); =20 #define set_indir_lp_lock(task) =20 +#define restore_user_shstk(tsk, shstk_ptr) -EINVAL + +#define save_user_shstk(tsk, saved_shstk_ptr) -EINVAL + +#define get_active_shstk(task) 0UL + #endif /* CONFIG_RISCV_USER_CFI */ =20 #endif /* __ASSEMBLY__ */ diff --git a/arch/riscv/include/uapi/asm/ptrace.h b/arch/riscv/include/uapi= /asm/ptrace.h index a38268b19c3d..659ea3af5680 100644 --- a/arch/riscv/include/uapi/asm/ptrace.h +++ b/arch/riscv/include/uapi/asm/ptrace.h @@ -127,6 +127,10 @@ struct __riscv_v_regset_state { */ #define RISCV_MAX_VLENB (8192) =20 +struct __sc_riscv_cfi_state { + unsigned long ss_ptr; /* shadow stack pointer */ +}; + #endif /* __ASSEMBLY__ */ =20 #endif /* _UAPI_ASM_RISCV_PTRACE_H */ diff --git a/arch/riscv/include/uapi/asm/sigcontext.h b/arch/riscv/include/= uapi/asm/sigcontext.h index cd4f175dc837..f37e4beffe03 100644 --- a/arch/riscv/include/uapi/asm/sigcontext.h +++ b/arch/riscv/include/uapi/asm/sigcontext.h @@ -10,6 +10,7 @@ =20 /* The Magic number for signal context frame header. */ #define RISCV_V_MAGIC 0x53465457 +#define RISCV_ZICFISS_MAGIC 0x9487 #define END_MAGIC 0x0 =20 /* The size of END signal context header. */ diff --git a/arch/riscv/kernel/signal.c b/arch/riscv/kernel/signal.c index 80c70dccf09f..a7472a6fcdca 100644 --- a/arch/riscv/kernel/signal.c +++ b/arch/riscv/kernel/signal.c @@ -22,11 +22,13 @@ #include #include #include +#include =20 unsigned long signal_minsigstksz __ro_after_init; =20 extern u32 __user_rt_sigreturn[2]; static size_t riscv_v_sc_size __ro_after_init; +static size_t riscv_zicfiss_sc_size __ro_after_init; =20 #define DEBUG_SIG 0 =20 @@ -140,6 +142,62 @@ static long __restore_v_state(struct pt_regs *regs, vo= id __user *sc_vec) return copy_from_user(current->thread.vstate.datap, datap, riscv_v_vsize); } =20 +static long save_cfiss_state(struct pt_regs *regs, void __user *sc_cfi) +{ + struct __sc_riscv_cfi_state __user *state =3D sc_cfi; + unsigned long ss_ptr =3D 0; + long err =3D 0; + + if (!IS_ENABLED(CONFIG_RISCV_USER_CFI) || !is_shstk_enabled(current)) + return 0; + + /* + * Save a pointer to shadow stack itself on shadow stack as a form of tok= en. + * A token on shadow gives following properties + * - Safe save and restore for shadow stack switching. Any save of shadow= stack + * must have had saved a token on shadow stack. Similarly any restore o= f shadow + * stack must check the token before restore. Since writing to shadow s= tack with + * address of shadow stack itself is not easily allowed. A restore with= out a save + * is quite difficult for an attacker to perform. + * - A natural break. A token in shadow stack provides a natural break in= shadow stack + * So a single linear range can be bucketed into different shadow stack= segments. Any + * sspopchk will detect the condition and fault to kernel as sw check e= xception. + */ + err |=3D save_user_shstk(current, &ss_ptr); + err |=3D __put_user(ss_ptr, &state->ss_ptr); + if (unlikely(err)) + return -EFAULT; + + return riscv_zicfiss_sc_size; +} + +static long __restore_cfiss_state(struct pt_regs *regs, void __user *sc_cf= i) +{ + struct __sc_riscv_cfi_state __user *state =3D sc_cfi; + unsigned long ss_ptr =3D 0; + long err; + + /* + * Restore shadow stack as a form of token stored on shadow stack itself = as a safe + * way to restore. + * A token on shadow gives following properties + * - Safe save and restore for shadow stack switching. Any save of shadow= stack + * must have had saved a token on shadow stack. Similarly any restore o= f shadow + * stack must check the token before restore. Since writing to shadow s= tack with + * address of shadow stack itself is not easily allowed. A restore with= out a save + * is quite difficult for an attacker to perform. + * - A natural break. A token in shadow stack provides a natural break in= shadow stack + * So a single linear range can be bucketed into different shadow stack= segments. + * sspopchk will detect the condition and fault to kernel as sw check e= xception. + */ + err =3D __copy_from_user(&ss_ptr, &state->ss_ptr, sizeof(unsigned long)); + + if (unlikely(err)) + return err; + + return restore_user_shstk(current, ss_ptr); +} + struct arch_ext_priv { __u32 magic; long (*save)(struct pt_regs *regs, void __user *sc_vec); @@ -150,6 +208,10 @@ struct arch_ext_priv arch_ext_list[] =3D { .magic =3D RISCV_V_MAGIC, .save =3D &save_v_state, }, + { + .magic =3D RISCV_ZICFISS_MAGIC, + .save =3D &save_cfiss_state, + }, }; =20 const size_t nr_arch_exts =3D ARRAY_SIZE(arch_ext_list); @@ -202,6 +264,12 @@ static long restore_sigcontext(struct pt_regs *regs, =20 err =3D __restore_v_state(regs, sc_ext_ptr); break; + case RISCV_ZICFISS_MAGIC: + if (!is_shstk_enabled(current) || size !=3D riscv_zicfiss_sc_size) + return -EINVAL; + + err =3D __restore_cfiss_state(regs, sc_ext_ptr); + break; default: return -EINVAL; } @@ -222,6 +290,10 @@ static size_t get_rt_frame_size(bool cal_all) if (cal_all || riscv_v_vstate_query(task_pt_regs(current))) total_context_size +=3D riscv_v_sc_size; } + + if (is_shstk_enabled(current)) + total_context_size +=3D riscv_zicfiss_sc_size; + /* * Preserved a __riscv_ctx_hdr for END signal context header if an * extension uses __riscv_extra_ext_header @@ -365,6 +437,11 @@ static int setup_rt_frame(struct ksignal *ksig, sigset= _t *set, #ifdef CONFIG_MMU regs->ra =3D (unsigned long)VDSO_SYMBOL( current->mm->context.vdso, rt_sigreturn); + + /* if bcfi is enabled x1 (ra) and x5 (t0) must match. not sure if we need= this? */ + if (is_shstk_enabled(current)) + regs->t0 =3D regs->ra; + #else /* * For the nommu case we don't have a VDSO. Instead we push two @@ -493,6 +570,9 @@ void __init init_rt_signal_env(void) { riscv_v_sc_size =3D sizeof(struct __riscv_ctx_hdr) + sizeof(struct __sc_riscv_v_state) + riscv_v_vsize; + + riscv_zicfiss_sc_size =3D sizeof(struct __riscv_ctx_hdr) + + sizeof(struct __sc_riscv_cfi_state); /* * Determine the stack space required for guaranteed signal delivery. * The signal_minsigstksz will be populated into the AT_MINSIGSTKSZ entry diff --git a/arch/riscv/kernel/usercfi.c b/arch/riscv/kernel/usercfi.c index 3a66f149a4ef..6e561256bfaf 100644 --- a/arch/riscv/kernel/usercfi.c +++ b/arch/riscv/kernel/usercfi.c @@ -52,6 +52,11 @@ void set_active_shstk(struct task_struct *task, unsigned= long shstk_addr) task->thread_info.user_cfi_state.user_shdw_stk =3D shstk_addr; } =20 +unsigned long get_active_shstk(struct task_struct *task) +{ + return task->thread_info.user_cfi_state.user_shdw_stk; +} + void set_shstk_status(struct task_struct *task, bool enable) { task->thread_info.user_cfi_state.ubcfi_en =3D enable ? 1 : 0; @@ -164,6 +169,57 @@ static int create_rstor_token(unsigned long ssp, unsig= ned long *token_addr) return 0; } =20 +/* + * Save user shadow stack pointer on shadow stack itself and return pointe= r to saved location + * returns -EFAULT if operation was unsuccessful + */ +int save_user_shstk(struct task_struct *tsk, unsigned long *saved_shstk_pt= r) +{ + unsigned long ss_ptr =3D 0; + unsigned long token_loc =3D 0; + int ret =3D 0; + + if (saved_shstk_ptr =3D=3D NULL) + return -EINVAL; + + ss_ptr =3D get_active_shstk(tsk); + ret =3D create_rstor_token(ss_ptr, &token_loc); + + if (!ret) { + *saved_shstk_ptr =3D token_loc; + set_active_shstk(tsk, token_loc); + } + + return ret; +} + +/* + * Restores user shadow stack pointer from token on shadow stack for task = `tsk` + * returns -EFAULT if operation was unsuccessful + */ +int restore_user_shstk(struct task_struct *tsk, unsigned long shstk_ptr) +{ + unsigned long token =3D 0; + + token =3D amo_user_shstk((unsigned long __user *)shstk_ptr, 0); + + if (token =3D=3D -1) + return -EFAULT; + + /* invalid token, return EINVAL */ + if ((token - shstk_ptr) !=3D SHSTK_ENTRY_SIZE) { + pr_info_ratelimited( + "%s[%d]: bad restore token in %s: pc=3D%p sp=3D%p, token=3D%p, shstk_pt= r=3D%p\n", + tsk->comm, task_pid_nr(tsk), __func__, (void *)(task_pt_regs(tsk)->epc), + (void *)(task_pt_regs(tsk)->sp), (void *)token, (void *)shstk_ptr); + return -EINVAL; + } + + /* all checks passed, set active shstk and return success */ + set_active_shstk(tsk, token); + return 0; +} + static unsigned long allocate_shadow_stack(unsigned long addr, unsigned lo= ng size, unsigned long token_offset, bool set_tok) { --=20 2.34.1 From nobody Tue Feb 10 09:58:08 2026 Received: from mail-pl1-f178.google.com (mail-pl1-f178.google.com [209.85.214.178]) (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 19AC4236A7B for ; Mon, 10 Mar 2025 14:53:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741618396; cv=none; b=nKVZWfgV/tXMr7AU3MoYy9eYJApnrZP4u5VOp+z0kEEXZrZQzyEW1lsOrLYWrOpQIXbG+9InSZn6SfrQpZ4BSUab7qgp405H3pXpOccQYbQRdmAELa83o6YhbrLvYoB6o586/+5S755eIMRaasyZn67+IaHgpAWTrw/zWiVZL7Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741618396; c=relaxed/simple; bh=UvMauEZ7ujgiT53rhoVNtF8wKatfWBS5dd1E01vsJYA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ejVpFV1y3FjyToC7Sg+6TaYZfoDPrzza9KOOpoHLo6O1NdIzkDuZDUMsyKLx+nHfPZcbl3h2NAHMxFGXrAt2qE/WQYSv/j6HUg2DB8ekoexG2puQ3Gd3/rG9QksQ8EXi9djJYBvIhl2Rm2Qsv4y9NsiHJlkMbeGtVhXXYFH9eXQ= 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=zPXTTQSK; arc=none smtp.client-ip=209.85.214.178 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 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="zPXTTQSK" Received: by mail-pl1-f178.google.com with SMTP id d9443c01a7336-22580c9ee0aso12630285ad.2 for ; Mon, 10 Mar 2025 07:53:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741618394; x=1742223194; darn=vger.kernel.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=hU6IAlGSwKWKw5iWDrFBqBdwaJZ69/+Cs7+/DXu3do8=; b=zPXTTQSKs3/W7hpXrMrHxoM7PXJu/T5lIZU0UskLLk+biL8fBDRNPITf9//V/O1GpU AJD3oPHH3qQlP5OL3dMoul6OWtzKNlN1m5QgSOVk3mMCpxeBh6nR68Wc6O1TMFIIVzxc dJDnrdwCGuChWNwT6mVSgVFssX0UtKee87VE0WtWpJeAHHCyxwhxl/FNW/Z0xEQEO9aU bWreBVHBCsf2t2Rk2Re/3K7fEiBJTGmlpIJt15/pfWNcu3FfL1SYxIaucRVjKfBEmnVw TxvnRmNJM89a6WslO5CZEDDyZ1OLtU1dlyrNR88YQpQC2HyraDmzvUKUY/WiN0tolozE 9IPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741618394; x=1742223194; 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=hU6IAlGSwKWKw5iWDrFBqBdwaJZ69/+Cs7+/DXu3do8=; b=FYPyLa/KU/Suv5E/DWByz4Ce+/32doucbEILivLa34JMpIEckdZso9PLmduTdXaVgi oj8YXW5ZqGuHqV1GPPfOCx57iP2aZAxE0DgcRKmseOPm9EmmJi5RlMQ1yBf8LF/Oz2Oz AjlEDqHVPUXbszNPqP3OgdqFhQmwM4rxf/HMW1uusqFf87dKbShx0hOlzKWTWu9BvhNn ZXxq9MFWq+EJ9ZkOVllA89MnMCwxqsvd7ctXQomEU5e5GoKlspHlWYwrYeQx9dbE7OjX KfZPJMijArLsYZhIK05JlzHxSYylcFhqEaBSO5boWGtkRqyE5zzAMBgJDOO7YmH9G0QB t2DA== X-Gm-Message-State: AOJu0Yzsbu+hLQKyNJkRcO7spfUbAMjyKruvJSajoq/+JrqKAs1sRbE/ VX1Py69uiW7i5hBlYTydA1pJNP5qqeOzKa1Ah6uU1bpgjEmYshrL6Ill5pgDwBc= X-Gm-Gg: ASbGncuRXcxdduf934IGWGZyHD6TDEpraefkj37wWY6gkFKhx8T8hKEjMXdT3sirEZv xkKbtBhHDMCFFus7HOqygyFm1RVtKg+k0v7m8H/6ngW0sSUvNm/mQum1LIr4NgBpQlYnGW59zOF MGkZW9x6REzO9jHbFDGTvhMIXSbD4a75w+laF0uwtntzlo/aqHEdoD6n+DRDs/ByTgEicsB7/44 c6B3HOxzbSvCo06wS7UCQq2Uni4/gxkJAjgIotHqBJYHz0Al+iAanK3F7SPHwAeuz6gWzYijy2J SNbzq0QYDpf7hLxhapZs5KoOwZjezV+KAUrtwJbRQx3v4tNWk1iIZ7U= X-Google-Smtp-Source: AGHT+IG6aOZPbFLpYB6hUt/uNP0xUDOJ3SJhAN3Hb+FAwmeol3/+ZPXWsjDkyF4qiBV6yJ/cwzujRQ== X-Received: by 2002:a17:902:ec89:b0:224:10a2:cad9 with SMTP id d9443c01a7336-22428bd5852mr262786715ad.41.1741618394280; Mon, 10 Mar 2025 07:53:14 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-736d11d4600sm2890275b3a.116.2025.03.10.07.53.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 07:53:13 -0700 (PDT) From: Deepak Gupta Date: Mon, 10 Mar 2025 07:52:39 -0700 Subject: [PATCH v11 17/27] riscv/kernel: update __show_regs to print shadow stack register Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250310-v5_user_cfi_series-v11-17-86b36cbfb910@rivosinc.com> References: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> In-Reply-To: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@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 , Jann Horn , Conor Dooley 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 Updating __show_regs to print captured shadow stack pointer as well. On tasks where shadow stack is disabled, it'll simply print 0. Signed-off-by: Deepak Gupta Reviewed-by: Alexandre Ghiti --- arch/riscv/kernel/process.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arch/riscv/kernel/process.c b/arch/riscv/kernel/process.c index 4587201dd81d..6bb53ce72ed5 100644 --- a/arch/riscv/kernel/process.c +++ b/arch/riscv/kernel/process.c @@ -90,8 +90,8 @@ void __show_regs(struct pt_regs *regs) regs->s8, regs->s9, regs->s10); pr_cont(" s11: " REG_FMT " t3 : " REG_FMT " t4 : " REG_FMT "\n", regs->s11, regs->t3, regs->t4); - pr_cont(" t5 : " REG_FMT " t6 : " REG_FMT "\n", - regs->t5, regs->t6); + pr_cont(" t5 : " REG_FMT " t6 : " REG_FMT " ssp : " REG_FMT "\n", + regs->t5, regs->t6, get_active_shstk(current)); =20 pr_cont("status: " REG_FMT " badaddr: " REG_FMT " cause: " REG_FMT "\n", regs->status, regs->badaddr, regs->cause); --=20 2.34.1 From nobody Tue Feb 10 09:58:08 2026 Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) (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 9C710236A9C for ; Mon, 10 Mar 2025 14:53:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741618399; cv=none; b=KX8omYGVoIU7Vyaq0Qc0dPhrhfCe4V1a3WU3gqYeGzvw2MmGuCF4BOjbDqpZ345LvuL4ZvBfhQfWy1W+KHqJjS628Foc6SgsYU9e4Z7oW6xRZ+oBn30s3Sx7tcWL/Skf9GWLKX1HVLSZ7aHE8Ck6HF9BCm62TRiOUZsoNxq3ys8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741618399; c=relaxed/simple; bh=no//4s1hCd1n+ijW2BrzZfXeDu40oLpUpqqQfLr6u/M=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=hcPa7qAx4P5uL751qCOK6O5+S2PvKF0dvYCIHBAMY67g6Rqtkh1LrQdjiSzXeEdiBTvmB1Bj4jInIEwPvqFr6mBOQ0FoMJ19VQh0q9YLRYMUCbIUPkuezykTY08HIitmth/n1WKXxyhF7k4fKsyfbAJsLp9gfxI2XCjWjbZzgy4= 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=q7Y7ULmP; arc=none smtp.client-ip=209.85.214.174 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 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="q7Y7ULmP" Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-219f8263ae0so81545005ad.0 for ; Mon, 10 Mar 2025 07:53:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741618397; x=1742223197; darn=vger.kernel.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=qihiC//CQppmEvCBgeNt37whYo0mSsmsLXBW9eHBYhI=; b=q7Y7ULmPK32MW3OtlYUCGdsP9cufm0C+WkOCAOrO7VD7tnMku/M7tZF2RS2+bbW3K9 GkoTZvR02Jh/6b87JS70oTo/ItSNFXe+crjkhM249Pl5E7YhnD+8kC/KTo0k5jx/y/cg 2plYyLCz3Pyz9sVY6idSrlB/Njx6XPgg8x9WsExcDSkWPC7s3wOlG7eOuJPVq8FFNNBP 3GZwb1B+fFpG+rkNJSvZ1of202xXfjd2JfWpsRiSbNCLth78bc6VB5vr3AtFoli9u15R yYsyuf8zADv/1hdAlXDnWKprDaQq3fuvJVaQ1/X5v8qAtANBk5tqonfO8shn5HiiTVuK NSjQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741618397; x=1742223197; 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=qihiC//CQppmEvCBgeNt37whYo0mSsmsLXBW9eHBYhI=; b=jxNXyicDjuEOHSeAafQnc/+WStGZ2VoKhVwYedOgI+v1LOMuhQQMzDFZbTJn3JnQPF sPdk8S+CmK4AP7TzdX9EDtzUriBXHPKCNZmy9zC0bZdD00JE6wggTUfGA21/0WD6FCzn 9gZ7PgNuRFWmrM6VBko+kkOi2/g42C4Ejj24Ay7iI0i0F0mJ/C1EnsJAww29muI4aZq9 AfKgcgrs/aTaOOODpkq1uVE1+D/5uSA8yog9E5PzZRqd+Eu2fO0SZ5Qnmqnv7EjhJS/E eLgm2X2/oCwuiYkyBgHOfgJGVotEeqH9CtPk5OkX1CjccCxDSsu/pu8d6hQaThKEAGpE bswQ== X-Gm-Message-State: AOJu0Yzif7aCyp8yXekAp7gIM+oVkh/yKZwEwRziWDhPN2N8ak3pj5HT kCtgm9784xAmqFLE/f2exgGfT/pAyGZLQP5ucRCd+htQo1AeZRpWj9pmkvCxzq8= X-Gm-Gg: ASbGncsnf2QbzOmjWUYOab/E+u2nOf8mcWhfkrA1BH7Efj/kizcOElehlRFLhZTGiSj 7csPwOCLDaRESmL05M1HwoZwzzvFL59/ao8NpDckQqHjsEXIwiyp8uaJLp0bcjfABesMhXdMNX+ SVkq9EhzuZwk2bsGSIgZufkXNuKUPvfr76LPsfT0ejf/Zxlw8gPow+IaBFVW0MQOpBrSPaXC80f RGbMPmgFSYv8obUQz8wzHB/iE5bUv0yRYXajMM/AmRjINaUh+WtNjq12wIwg76L9dLWinm/F4Ac ihVyn0kAPDCNa9JiFsCN0Z/hlluHYwanUO6GAjwt2Hqitp8EdsAxyfo= X-Google-Smtp-Source: AGHT+IHLu0RD5d4MddLFDjOWYpbnLGPGC73GF+e3WfGfDfnCowno5o+D7aYjLyREna2LMovQQemWaA== X-Received: by 2002:a05:6a00:c92:b0:736:8c0f:774f with SMTP id d2e1a72fcca58-736eb8a1ccbmr221171b3a.22.1741618396770; Mon, 10 Mar 2025 07:53:16 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-736d11d4600sm2890275b3a.116.2025.03.10.07.53.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 07:53:16 -0700 (PDT) From: Deepak Gupta Date: Mon, 10 Mar 2025 07:52:40 -0700 Subject: [PATCH v11 18/27] riscv/ptrace: riscv cfi status and state via ptrace and in core files Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250310-v5_user_cfi_series-v11-18-86b36cbfb910@rivosinc.com> References: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> In-Reply-To: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@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 , Jann Horn , Conor Dooley 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 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 */ }; =20 +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__ */ =20 #endif /* _UAPI_ASM_RISCV_PTRACE_H */ diff --git a/arch/riscv/kernel/ptrace.c b/arch/riscv/kernel/ptrace.c index ea67e9fb7a58..df8b7c6ab671 100644 --- a/arch/riscv/kernel/ptrace.c +++ b/arch/riscv/kernel/ptrace.c @@ -19,6 +19,7 @@ #include #include #include +#include =20 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 }; =20 static int riscv_gpr_get(struct task_struct *target, @@ -184,6 +188,75 @@ static int tagged_addr_ctrl_set(struct task_struct *ta= rget, } #endif =20 +#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 =3D task_pt_regs(target); + + user_cfi.cfi_status.lp_en =3D is_indir_lp_enabled(target); + user_cfi.cfi_status.lp_lock =3D is_indir_lp_locked(target); + user_cfi.cfi_status.elp_state =3D (regs->status & SR_ELP); + + user_cfi.cfi_status.shstk_en =3D is_shstk_enabled(target); + user_cfi.cfi_status.shstk_lock =3D is_shstk_locked(target); + user_cfi.shstk_ptr =3D 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 =3D task_pt_regs(target); + + ret =3D 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, d= o that */ + if (is_indir_lp_enabled(target)) { + if (user_cfi.cfi_status.elp_state) /* set elp state */ + regs->status |=3D SR_ELP; + else + regs->status &=3D ~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[] =3D { [REGSET_X] =3D { .core_note_type =3D NT_PRSTATUS, @@ -224,6 +297,16 @@ static const struct user_regset riscv_user_regset[] = =3D { .set =3D tagged_addr_ctrl_set, }, #endif +#ifdef CONFIG_RISCV_USER_CFI + [REGSET_CFI] =3D { + .core_note_type =3D NT_RISCV_USER_CFI, + .align =3D sizeof(__u64), + .n =3D sizeof(struct user_cfi_state) / sizeof(__u64), + .size =3D sizeof(__u64), + .regset_get =3D riscv_cfi_get, + .set =3D riscv_cfi_set, + }, +#endif }; =20 static const struct user_regset_view riscv_user_native_view =3D { diff --git a/include/uapi/linux/elf.h b/include/uapi/linux/elf.h index b44069d29cec..b9daed4ab780 100644 --- a/include/uapi/linux/elf.h +++ b/include/uapi/linux/elf.h @@ -452,6 +452,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 regist= ers */ --=20 2.34.1 From nobody Tue Feb 10 09:58:08 2026 Received: from mail-pl1-f176.google.com (mail-pl1-f176.google.com [209.85.214.176]) (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 E936C238160 for ; Mon, 10 Mar 2025 14:53:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741618401; cv=none; b=ZKsCj/k4vrdSj+nFLDoppBFclNdQlYAnECu6hrg4cRAMB7sn0585twsDpllKjD/LtndDhZgbxUTef0BFfwDoombBs0A9sKJ1UR3n8vdN8nyEMAD5kU0+ZLX2DQ8WhzdIw+wAtjGENj+wwh7x8RRw7PDcOyeVvKdNsOibcH1bpUM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741618401; c=relaxed/simple; bh=NmVWKk7RCdoTm8/kxl5H6XmDwgptfJerdilzp6YHjbI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=PyX6XfjKVjpvrm/p1GIvVQ+Fr60cJv2mlQNmuSCV53Ny2y3NmNtMJLfn34LFTjo8XWn3mB6dO7sIw8VSF9TbS0mNEU15u/uTwXnei5DGQAJSwhkoEcSNHvklDtK7QedUAsn2k+NBKi6erspvRXqEk3pFyM3Za6D7xUGFjczvPAA= 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=001MDNVH; arc=none smtp.client-ip=209.85.214.176 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 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="001MDNVH" Received: by mail-pl1-f176.google.com with SMTP id d9443c01a7336-22185cddbffso95176255ad.1 for ; Mon, 10 Mar 2025 07:53:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741618399; x=1742223199; darn=vger.kernel.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=BEoq6zqSeLFMaXOoiuM7yp6ZSaKHVlNR4X3PvApIHRU=; b=001MDNVHlC1le3S/p3HpJjvvRY1zWlgI7KtAcZRY/0MIZyG2mLNEY95y0IK+V2WarG MJJOCnOK10PlxOPHAIX08BPn2OjLCRoB9QlZZwXVNknopwgUgFClom06HTKqOKFF1g91 yjNwR/9DnNTCex/suq/biq2N1c+CHhS7+kiY/cwHEaIdBNlPYnGde3oprqKifrr8f3qg zTyXQmFw1Odpv1AhL4vVA5Sl61WG3HSX/RAdSZUSqsGogS73lJ5fSIFwDWXgbenw9a7p JrTwrZqZhiyjHptDYnm0hz/vj/99iTc7WB/SLoV5zrvrHZJUXIbICtAYM/zsqWL7Qr3x 3bfw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741618399; x=1742223199; 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=BEoq6zqSeLFMaXOoiuM7yp6ZSaKHVlNR4X3PvApIHRU=; b=Jb7LZfqJW/3Y04WS2LXv3rbLwq7TdTLkpkHrNKY4fmVYqGe1NmhRkmto6dVZJSSOou mWDjk/NZEVxGxkPwmqQWR128UBQhQg6+/d4gBm2HOQB0PFDKn/tV7Ye43G6oNp4Trhbv vHHBcAkbpKV1K2VxJUZ3/mKxwtxC4JytJbd+kmgDCtcYaRZf+Ktgl2WFaISQ7Efk4ULm DOCxG49/GQDoxZLLQO8grjIeagwsPSvhS+P5XcmYfr91PJfrthwvt8IbbqjVLEBxzUKb hgNHqN5vVnlztfj9dVFqY5V8EdtqOa25akugzCIpchGk/tAzEnnjAHErRsvhMdJNWYYm qxqw== X-Gm-Message-State: AOJu0YzR9tCwyQNSUYuB3orzVMgEDbHfdzH3qHKu7vcSnQvE4bcpKo8R tkW31IKI4EFQ5voclCXoJ3dDQK2OlbKhbSx1jsueEISB7EoT6fszuGcjowmwkdM= X-Gm-Gg: ASbGnctnHhfrJhaEv2VwX5AwaPnqiTSc81EeKt79EmSHK1gDnh1EcwjA7mK2YSm2TzT jJJdcAwYI4QCjDRSS//C9QAfzKKCO9EWV6pImyn3WACUX9qOO44EylxlABX6eEBsAGUJn/oRh8L oiwAONJinCvmiMGtNiy1BzBXdj7wNPcOUADefNzdg+/em4nKRKMqsH4L64jirH8XsKJPuZxvVsY h/q9gbJfEE0enEBv7CGCPbvB4xqLo4zZMV7esQlQE0P71K5j1pBbsx+NvIJFMAyifsLhj1NplSc bfpS3vKqVUP9Ayj1KJzQPCVAz6iEw2KobSb5mJLvpOrTyVdicarMEv4= X-Google-Smtp-Source: AGHT+IEx/P9VhGmpM9Qve+8fxlnJz5OWfBkRKBtIDANK/IPRQR8t6D11CbRKG5Xc1EPx3A5RF0LL1A== X-Received: by 2002:a05:6a00:2b4f:b0:736:b923:5323 with SMTP id d2e1a72fcca58-736bc0fb12dmr15850983b3a.10.1741618399230; Mon, 10 Mar 2025 07:53:19 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-736d11d4600sm2890275b3a.116.2025.03.10.07.53.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 07:53:18 -0700 (PDT) From: Deepak Gupta Date: Mon, 10 Mar 2025 07:52:41 -0700 Subject: [PATCH v11 19/27] riscv/hwprobe: zicfilp / zicfiss enumeration in hwprobe Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250310-v5_user_cfi_series-v11-19-86b36cbfb910@rivosinc.com> References: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> In-Reply-To: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@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 , Jann Horn , Conor Dooley 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 Adding enumeration of zicfilp and zicfiss extensions in hwprobe syscall. Signed-off-by: Deepak Gupta Reviewed-by: Zong Li --- arch/riscv/include/uapi/asm/hwprobe.h | 2 ++ arch/riscv/kernel/sys_hwprobe.c | 2 ++ 2 files changed, 4 insertions(+) diff --git a/arch/riscv/include/uapi/asm/hwprobe.h b/arch/riscv/include/uap= i/asm/hwprobe.h index c3c1cc951cb9..c1b537b50158 100644 --- a/arch/riscv/include/uapi/asm/hwprobe.h +++ b/arch/riscv/include/uapi/asm/hwprobe.h @@ -73,6 +73,8 @@ struct riscv_hwprobe { #define RISCV_HWPROBE_EXT_ZCMOP (1ULL << 47) #define RISCV_HWPROBE_EXT_ZAWRS (1ULL << 48) #define RISCV_HWPROBE_EXT_SUPM (1ULL << 49) +#define RISCV_HWPROBE_EXT_ZICFILP (1ULL << 50) +#define RISCV_HWPROBE_EXT_ZICFISS (1ULL << 51) #define RISCV_HWPROBE_KEY_CPUPERF_0 5 #define RISCV_HWPROBE_MISALIGNED_UNKNOWN (0 << 0) #define RISCV_HWPROBE_MISALIGNED_EMULATED (1 << 0) diff --git a/arch/riscv/kernel/sys_hwprobe.c b/arch/riscv/kernel/sys_hwprob= e.c index bcd3b816306c..d802ff707913 100644 --- a/arch/riscv/kernel/sys_hwprobe.c +++ b/arch/riscv/kernel/sys_hwprobe.c @@ -108,6 +108,8 @@ static void hwprobe_isa_ext0(struct riscv_hwprobe *pair, EXT_KEY(ZCB); EXT_KEY(ZCMOP); EXT_KEY(ZICBOZ); + EXT_KEY(ZICFILP); + EXT_KEY(ZICFISS); EXT_KEY(ZICOND); EXT_KEY(ZIHINTNTL); EXT_KEY(ZIHINTPAUSE); --=20 2.34.1 From nobody Tue Feb 10 09:58:08 2026 Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) (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 39F27238D50 for ; Mon, 10 Mar 2025 14:53:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741618404; cv=none; b=flOovY7YR+T0lWyr5E7LLNnydKSJBSxy6lOgXxrIKKYugPSvYmC5h2Db87Gw12/9du+W9hHlBBGeuVR8h+QxJGzzTL4Nd3F66+tDbUt7rUvpMQY2vSIL1PMTyumrZxsGH/gturUBMTrE5Qw80lj9f2HrfjqxPs0ynGCWhPIZu+U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741618404; c=relaxed/simple; bh=lD6rgHPb/ib2WH9dder9ZLKtBlS2SotatDP0DK7YItE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=BxmG7PBgKjuIUoGO+Cr1L5bIoZKi5lqMTG7HZLjTEbvOD97IwqamorpOc04QGYH0h/pBra/omjKFTD2QNcEYruZElye/DAk1o0drhWKYVwjQ4WyaEfzPBmrbDz84ln/p8Gbt1gOrbhaIuEruSArgMDci/xLmlOdg+k/tOsNzq5Q= 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=Ct+JB6P8; arc=none smtp.client-ip=209.85.214.182 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 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="Ct+JB6P8" Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-2239c066347so74054385ad.2 for ; Mon, 10 Mar 2025 07:53:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741618402; x=1742223202; darn=vger.kernel.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=a5uI84SXblQRDID2BHNWhhHsRc7xVUDdoiei4kS9c/0=; b=Ct+JB6P8gBFhU2GL68I9x56icjkLjP62AapuT2jcDeENjphhxaGGU3o+qC8dvKrJ0g 2VfkE9773u7mT4/bPGU8BwIZlfR+Ynatelwu14iYwVM6exIiH6gjp+3B4XjVdJfL4EYs 4qC50m+e0mG7AbBt3fM2AWvnhDyCBIpD8l2BTMQceznIti+vPI3gt2Y8jENfIaqA2a9t YRel1XQSF1IYenBIUAg/xE4OMXKixw6zVwXIxoQBEQQPIM3e5Gk7KDzclPmCj/1bCMaG In0BU9boVSJlZ7ykEefPbMdFA3Qx0QxQ6esFxVSSS91DR0SnEDmlQDd66ojWVQcUwJ2q gKZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741618402; x=1742223202; 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=a5uI84SXblQRDID2BHNWhhHsRc7xVUDdoiei4kS9c/0=; b=pECKeIaGwiIuazO+F8SSPCSSE6lqoGExUo4zyyXG43SaMN9cofY+k0sp30dRv8UrJA KlokD8fSQMWKA0BBSgz6v+YVVaoj9VFr08sXrWNs2q9h13SGaXwOAxW+o/B7DXbDansx XXeaQRKHbG0rhy4dKLTdkylebIIyqfiFG1oeeTScLdcurJkI4OL1zFHZCDQiO+N2IfwC ydPb/XIZRT0TADb/vu4RzQcnZAGIeS9DfOSNqyj6XqdTwCSbWroHBQ5geEXafYy9CkRk k9cT8FqFuj32ZEqL0r5sNz+1qZBuPxvCurbwOomRyrhzQ1aRy17EK2N/BM3qbQOluu23 s81A== X-Gm-Message-State: AOJu0YwZ1UbXWyZDynx+Fy4Kz+3oqHY3QuYL6KgRHtL+qQmU9dH39hez 92kNdbnCaOI3d5AmpZvnQiwVa15plEAEaQ7qxD9Ewct7Yk8slVKa1iZkCCVt6BU= X-Gm-Gg: ASbGncvvfAfMWWdIFYgbZS4n/Du3cNaipbiWHnB90IRpQHde6/tFRIUewdyyEyxaXwK Wjxh67GX5x0X5jibe6jn6KV2Us6bsXvOf2qr//qTDFeU0+68dOXHqM3u5wHBkEITGIgq4Z7veKw hSCUkeKCD4vPBGzkT1w05JmIb3X/yFfHER1GKJlU90UYTP41wSaTgBiqOgFKjRf1T0hDkB9MhF4 VB+JInst3D9Tpe6zeM6ajvFUKJPCMAyxq/ZfYK3sZsTUDGUkO+x8IQXDxfaDZdFq4nDaf5vQQX6 7PeZzlcLEZHkzBQcw90yoj+73bTs59bQuiGKNbhwi7gM7T+aZ9RtXj8= X-Google-Smtp-Source: AGHT+IFjDwjQ9fbG75ByIkC5Z62BpsOlmfgaoy0NDr7CGAoT4eOeNPAoaYep0Q4HRAItC/SdOQSQyA== X-Received: by 2002:a05:6a00:4b4a:b0:736:54c9:df2c with SMTP id d2e1a72fcca58-736aaab75e8mr18259867b3a.15.1741618401679; Mon, 10 Mar 2025 07:53:21 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-736d11d4600sm2890275b3a.116.2025.03.10.07.53.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 07:53:21 -0700 (PDT) From: Deepak Gupta Date: Mon, 10 Mar 2025 07:52:42 -0700 Subject: [PATCH v11 20/27] riscv: Add Firmware Feature SBI extensions definitions Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250310-v5_user_cfi_series-v11-20-86b36cbfb910@rivosinc.com> References: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> In-Reply-To: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@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 , Jann Horn , Conor Dooley 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 X-Mailer: b4 0.14.0 From: Cl=C3=A9ment L=C3=A9ger Add necessary SBI definitions to use the FWFT extension. Signed-off-by: Cl=C3=A9ment L=C3=A9ger Reviewed-by: Zong Li --- arch/riscv/include/asm/sbi.h | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/arch/riscv/include/asm/sbi.h b/arch/riscv/include/asm/sbi.h index 3d250824178b..23bfb254e3f4 100644 --- a/arch/riscv/include/asm/sbi.h +++ b/arch/riscv/include/asm/sbi.h @@ -35,6 +35,7 @@ enum sbi_ext_id { SBI_EXT_DBCN =3D 0x4442434E, SBI_EXT_STA =3D 0x535441, SBI_EXT_NACL =3D 0x4E41434C, + SBI_EXT_FWFT =3D 0x46574654, =20 /* Experimentals extensions must lie within this range */ SBI_EXT_EXPERIMENTAL_START =3D 0x08000000, @@ -401,6 +402,31 @@ enum sbi_ext_nacl_feature { =20 #define SBI_NACL_SHMEM_SRET_X(__i) ((__riscv_xlen / 8) * (__i)) #define SBI_NACL_SHMEM_SRET_X_LAST 31 +/* SBI function IDs for FW feature extension */ +#define SBI_EXT_FWFT_SET 0x0 +#define SBI_EXT_FWFT_GET 0x1 + +enum sbi_fwft_feature_t { + SBI_FWFT_MISALIGNED_EXC_DELEG =3D 0x0, + SBI_FWFT_LANDING_PAD =3D 0x1, + SBI_FWFT_SHADOW_STACK =3D 0x2, + SBI_FWFT_DOUBLE_TRAP =3D 0x3, + SBI_FWFT_PTE_AD_HW_UPDATING =3D 0x4, + SBI_FWFT_LOCAL_RESERVED_START =3D 0x5, + SBI_FWFT_LOCAL_RESERVED_END =3D 0x3fffffff, + SBI_FWFT_LOCAL_PLATFORM_START =3D 0x40000000, + SBI_FWFT_LOCAL_PLATFORM_END =3D 0x7fffffff, + + SBI_FWFT_GLOBAL_RESERVED_START =3D 0x80000000, + SBI_FWFT_GLOBAL_RESERVED_END =3D 0xbfffffff, + SBI_FWFT_GLOBAL_PLATFORM_START =3D 0xc0000000, + SBI_FWFT_GLOBAL_PLATFORM_END =3D 0xffffffff, +}; + +#define SBI_FWFT_GLOBAL_FEATURE_BIT (1 << 31) +#define SBI_FWFT_PLATFORM_FEATURE_BIT (1 << 30) + +#define SBI_FWFT_SET_FLAG_LOCK (1 << 0) =20 /* SBI spec version fields */ #define SBI_SPEC_VERSION_DEFAULT 0x1 --=20 2.34.1 From nobody Tue Feb 10 09:58:08 2026 Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) (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 8F02E23A560 for ; Mon, 10 Mar 2025 14:53:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741618406; cv=none; b=NrO31QlRzBUGAcc+jQoE0O3+lMFRkS30uNyRIWJKrFNwwzbncrRHeP2hgcBCFlqE52HL/6HRqXisjGBGPVOnv++pedIku62uiuuONgEnaKbYFZdY84gfEo8T8HeqiZLSvhHh78+UDf6a3Jqsmnh+06nLUbk9G/I0VSsj4fHnqXY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741618406; c=relaxed/simple; bh=eLCcCnwX3oMrnR0hoKevVmuVrExvCA/E4+w0/d6DmO4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=jxVjpqC2OAq7X7sO2Plzz97FZKGerUPVKqAXUgaa0DmqMMx4rz5yeUvMetUohGyHZaq5YX3z69ckmmUJCZUhffRlsFU8i5wuwNchlZTVp5b49Z4Fd4YBocHCQ06Olp8q8jsVRkcpE18sqkxM1KSaNC4fzXQPqPjeLZ9yak4zwDs= 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=zUHRTAMn; arc=none smtp.client-ip=209.85.214.173 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 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="zUHRTAMn" Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-223a7065ff8so3892115ad.0 for ; Mon, 10 Mar 2025 07:53:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741618404; x=1742223204; darn=vger.kernel.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=YHkNmM6XuphgUqeYDdwRc1f9MaOV9mSfy6OUDBv2dEo=; b=zUHRTAMnMSaHlOldgj+y5jDjAYlmU7B0XUvZ6KgxMKKNrluU6quTfpD/Duk5wM0s2J UfTIYhxGa5RhQK6VYs5rn78ang84tKTfKdmruN71mjbatvIArCfof4qVUOgEvKKYZDOk sgIsgIItjsfP5m/lEdxKFxEmr2lmRac66WGRLhUFcZkm+MlvNwTDTmPVAKfhLdVnWwwm 8MYZ8VbRMGciDWwLP5joLbMMNmaJRsAcJqdvtzQbvF59J7Qg3xfWKSPUzkrvLCaBtrzd dTdyYNOphbJTwNWRVHPpu3kx1sFnWNno9IN21VxZTeCjPv6AnDfG2RUu4TXA8n+N4wKQ QPzg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741618404; x=1742223204; 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=YHkNmM6XuphgUqeYDdwRc1f9MaOV9mSfy6OUDBv2dEo=; b=Lfgkd2VR36wOBjumcyu2N10DbKSqrkSt71jWxTkE0vDl+r8ROw+FUoIme2iilbuXAe obdh2ZGvq0N8eDLGCnuCyEF6D02+Ts2yixv537MYlWZ9GW0/RZhsKuRdTb+gpR63cOES oY8y0kanXzue6yzra0A5eKI9O14hSdDrmY1V5tVsmscznB9knO9p3XtToIgawRoxY3fa o2s7Zm0P4666ngYr42luXyyuLyBWPstZyud0pOcz586kG5GioDmVYyoULd9NeTNd14hG c3xpiGxmN2enl+P6ygJGfA5NfE0pTPztaaPJfiGoKwreJldFXcTZfjTV8XF0ClVASbVL 9lDw== X-Gm-Message-State: AOJu0YwMb1uG61YG4x8iNnivmMWYt6RCqGGTe7K2dIimkPWw0RbFsGJB gubHzdEnCnmXs8ov0JUQRtfVOhuYpXaPrHBMotJMJoclgXmk1H5MVZbX/Xcms9M= X-Gm-Gg: ASbGncv0EK46Q8oXsHi7NU5k4sCXwKAgQ0XCUiXLWFrpKdeRzMHVhAXrY5FMIOGovZa nLAs7X4GkVdJxuLGrj62p13n6Ky6OuzqDUcKs+1uG0BuMH8o7y+UyLxWpbCbmEvChbHUKNCVCmK sZ9ewjnQkzHbnQaXBcQk7jVAmrFyzcQWAJeSDA0aIHlit6Qhdhx8eA0UpIjGrnYvkzZhkMmv852 Lgj8+KG6EUE3cX2CZtsakRyFSNo7VbM8JCgzZBa6WyeqCVWON3YvqmgDMiwX0gV4to22wEJH+S6 gqogNHwji1yzlOioWkqt/W/hhG2sfpbHB3vPnY9VMPOeGlp+6mmkGHI= X-Google-Smtp-Source: AGHT+IEufhMFpKiNcPYaN1q7fnoyb0yGUoGedR4PcX/LQ5rFfhM8NE7LJbQGAfjZIn8NhFa9HTR7qA== X-Received: by 2002:a05:6a21:b92:b0:1f3:3f0b:8abe with SMTP id adf61e73a8af0-1f544acd261mr21823779637.9.1741618404090; Mon, 10 Mar 2025 07:53:24 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-736d11d4600sm2890275b3a.116.2025.03.10.07.53.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 07:53:23 -0700 (PDT) From: Deepak Gupta Date: Mon, 10 Mar 2025 07:52:43 -0700 Subject: [PATCH v11 21/27] riscv: enable kernel access to shadow stack memory via FWFT sbi call Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250310-v5_user_cfi_series-v11-21-86b36cbfb910@rivosinc.com> References: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> In-Reply-To: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@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 , Jann Horn , Conor Dooley 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 Kernel will have to perform shadow stack operations on user shadow stack. Like during signal delivery and sigreturn, shadow stack token must be created and validated respectively. Thus shadow stack access for kernel must be enabled. In future when kernel shadow stacks are enabled for linux kernel, it must be enabled as early as possible for better coverage and prevent imbalance between regular stack and shadow stack. After `relocate_enable_mmu` has been done, this is as early as possible it can enabled. Signed-off-by: Deepak Gupta Reviewed-by: Zong Li --- arch/riscv/kernel/asm-offsets.c | 4 ++++ arch/riscv/kernel/head.S | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/arch/riscv/kernel/asm-offsets.c b/arch/riscv/kernel/asm-offset= s.c index 0c188aaf3925..21f99d5757b6 100644 --- a/arch/riscv/kernel/asm-offsets.c +++ b/arch/riscv/kernel/asm-offsets.c @@ -515,4 +515,8 @@ void asm_offsets(void) DEFINE(FREGS_A6, offsetof(struct __arch_ftrace_regs, a6)); DEFINE(FREGS_A7, offsetof(struct __arch_ftrace_regs, a7)); #endif + DEFINE(SBI_EXT_FWFT, SBI_EXT_FWFT); + DEFINE(SBI_EXT_FWFT_SET, SBI_EXT_FWFT_SET); + DEFINE(SBI_FWFT_SHADOW_STACK, SBI_FWFT_SHADOW_STACK); + DEFINE(SBI_FWFT_SET_FLAG_LOCK, SBI_FWFT_SET_FLAG_LOCK); } diff --git a/arch/riscv/kernel/head.S b/arch/riscv/kernel/head.S index 356d5397b2a2..6244408ca917 100644 --- a/arch/riscv/kernel/head.S +++ b/arch/riscv/kernel/head.S @@ -164,6 +164,12 @@ secondary_start_sbi: call relocate_enable_mmu #endif call .Lsetup_trap_vector + li a7, SBI_EXT_FWFT + li a6, SBI_EXT_FWFT_SET + li a0, SBI_FWFT_SHADOW_STACK + li a1, 1 /* enable supervisor to access shadow stack access */ + li a2, SBI_FWFT_SET_FLAG_LOCK + ecall scs_load_current call smp_callin #endif /* CONFIG_SMP */ @@ -320,6 +326,12 @@ SYM_CODE_START(_start_kernel) la tp, init_task la sp, init_thread_union + THREAD_SIZE addi sp, sp, -PT_SIZE_ON_STACK + li a7, SBI_EXT_FWFT + li a6, SBI_EXT_FWFT_SET + li a0, SBI_FWFT_SHADOW_STACK + li a1, 1 /* enable supervisor to access shadow stack access */ + li a2, SBI_FWFT_SET_FLAG_LOCK + ecall scs_load_current =20 #ifdef CONFIG_KASAN --=20 2.34.1 From nobody Tue Feb 10 09:58:08 2026 Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) (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 1B81D23BD14 for ; Mon, 10 Mar 2025 14:53:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741618409; cv=none; b=fos7bG3oh16tCng9epvZ58nx7eKrQJ0VcUgwRPPG0oZGZRh0OmA0Md56FSksWHIu/pmZWJX/Mm8Dxh/Ca8MJmxpQGd9tmDwpsr4WoNRIYkf1ri1zhWVcPeqKEH8JhZ9Wj23f3HlExh8YRfXDgOVRsuxMlSJEbQD3ZC3GJxdOfFs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741618409; c=relaxed/simple; bh=zWvCMjEUr0fm2YGOtkO5JQMRJ3MfHFejTQpY2YYRpeI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=TqtZ8GacdZlgs5oVGUYnCutQlf5lWny/Faxr58xO+gycVnZj+YdUgFgoQ6Wd/ZDR/tlmvEiw0QeOADvyXkJ4zcJQJdePt7PlUkW5Y20DJs/NdJFZ3LLYv7oP3IOp1y3eEfGdnDEvh6rgG//peOSMtZTRVEktQk/xsgFIhfAcZBQ= 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=02fXvqYm; arc=none smtp.client-ip=209.85.214.173 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 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="02fXvqYm" Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-2254e0b4b79so11056135ad.2 for ; Mon, 10 Mar 2025 07:53:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741618406; x=1742223206; darn=vger.kernel.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=opxHamP1ejpgUrZttGLg4zwDHWHLF3xPs1/Qb/08WRM=; b=02fXvqYmI7IQzrMpZK7gWQgvRJrftC9vOY2QlY/CM8V2HigjhV+w/NhLxRTeEtYott S2q7kSjN72yJa5Ufr7WNsgAxBQaPRxaPtmQ14/lwF1on8E1MOrns3WfNwEZCQNFrYfxl GX52zg5xG6UIPsdCmuXWJna4/t0SmvTqPIp2e0KDF5gTKiAT1Amm+d7ScFnLCsHzXtGI FEyqvUmVtYaQI/Zb7K1ksG7qzmbIOSeCsvQqszo50mYTw1QzcKG9b/fXneMLRQWsQOnq bg32wUkKQoBy8zsP0AAuBeuiOmbpUWRyIMYciP3/LXXmnjAuZ1ZA/9PzQt0oY9ba4Agc 5BPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741618406; x=1742223206; 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=opxHamP1ejpgUrZttGLg4zwDHWHLF3xPs1/Qb/08WRM=; b=GFNOYQZBhkNDKKh7dmxhRTRvDFRl20FVJ51Dfm6x5BhNV17nJHmjeko9EsvcUOkngH gTec56HhRazjZ476zxJDhb891sAO8o5ArYzTc/28SpA9StWH9zCvLTqUQFiDyQ2hqOOY lDlNsNsOdQXJspHFkoJnkkMZUHMoCLgyoBeUeC/ZuQPbrjfljY9MAOqQyfaaeOiQOgF9 LeBqHQpTwhvlB+19Gjw7CE85u/UD/AP5rr1NREsV2AnCqtPnEHHA2IKkjUokU/Jke5NI bw4kP//63PuOhFNG+AdjQXj+Oj1EF6vX5fS1TXPGRSpT5nqoVCzMUwcvUAq3M22JasDN DaYQ== X-Gm-Message-State: AOJu0Yw0kndjLDvO6QP/FAOkFAj4vgyyv5QOZrN0S2189qq7st212r1d 73i/SwPFLk3pmKggBWRqp5/48wWO+n7dXai2MfLVilgI5RxC3tXmq7qNDNyfrr0= X-Gm-Gg: ASbGncuZ4n7nUoU9JDs8bsq5jiUiVHq6ZjrkcMGge1fa1qO/0WEmI6ZRkFjhgeI7iBs jgbJ2LJGLzAWV3K2VQjjjfskWeMyTkiKvTLTKFUTTgAZUoOYd2duemcceobTwY+iSKsDDvZWsye r92QR0Pz8aEyJg64Awoyu433SJBlR7lsNIysTkZv2eGCVqE0PoHzdm/929UnNVz3/mn68IqEYiG kfnxeg7xOfF7TFwBId4/ne9+3wS8nZOTDfP9+10gsF+EO19ekhI/y7NdxhVi2sN0lhZSqigmNcO NdXkgS/v24skYRFvrfKVSRLSM8pbrSKIfA36795h9EEnEP4UpoG0nPo= X-Google-Smtp-Source: AGHT+IGghOp+j7sQ0Qt7sq7k8fXJkHLz3hwRxKC8iR81NfpyoYASRowQ1NRmeDSzKjKxRLkZDMtKZA== X-Received: by 2002:a62:fb06:0:b0:736:baa0:2acd with SMTP id d2e1a72fcca58-736baa02af4mr14991022b3a.20.1741618406530; Mon, 10 Mar 2025 07:53:26 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-736d11d4600sm2890275b3a.116.2025.03.10.07.53.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 07:53:26 -0700 (PDT) From: Deepak Gupta Date: Mon, 10 Mar 2025 07:52:44 -0700 Subject: [PATCH v11 22/27] riscv: kernel command line option to opt out of user cfi Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250310-v5_user_cfi_series-v11-22-86b36cbfb910@rivosinc.com> References: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> In-Reply-To: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@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 , Jann Horn , Conor Dooley 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 This commit adds a kernel command line option using which user cfi can be disabled. Signed-off-by: Deepak Gupta --- arch/riscv/kernel/usercfi.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/arch/riscv/kernel/usercfi.c b/arch/riscv/kernel/usercfi.c index 6e561256bfaf..be08e5bb6e62 100644 --- a/arch/riscv/kernel/usercfi.c +++ b/arch/riscv/kernel/usercfi.c @@ -17,6 +17,8 @@ #include #include =20 +bool disable_riscv_usercfi; + #define SHSTK_ENTRY_SIZE sizeof(void *) =20 bool is_shstk_enabled(struct task_struct *task) @@ -390,6 +392,9 @@ int arch_set_shadow_stack_status(struct task_struct *t,= unsigned long status) unsigned long size =3D 0, addr =3D 0; bool enable_shstk =3D false; =20 + if (disable_riscv_usercfi) + return 0; + if (!cpu_supports_shadow_stack()) return -EINVAL; =20 @@ -469,6 +474,9 @@ int arch_set_indir_br_lp_status(struct task_struct *t, = unsigned long status) { bool enable_indir_lp =3D false; =20 + if (disable_riscv_usercfi) + return 0; + if (!cpu_supports_indirect_br_lp_instr()) return -EINVAL; =20 @@ -501,3 +509,16 @@ int arch_lock_indir_br_lp_status(struct task_struct *t= ask, =20 return 0; } + +static int __init setup_global_riscv_enable(char *str) +{ + if (strcmp(str, "true") =3D=3D 0) + disable_riscv_usercfi =3D true; + + pr_info("Setting riscv usercfi to be %s\n", + (disable_riscv_usercfi ? "disabled" : "enabled")); + + return 1; +} + +__setup("disable_riscv_usercfi=3D", setup_global_riscv_enable); --=20 2.34.1 From nobody Tue Feb 10 09:58:08 2026 Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.175]) (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 DDC5723C8CE for ; Mon, 10 Mar 2025 14:53:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741618412; cv=none; b=AZciqDZRaZ7nCJcy4gO5n0NPRNVQsOmABKJqMwG54PGhoF/kw4sAy9hG+sz+jW9po9H56V6p81ZBNwzdQjhlUlk4gsRVnZFTwWXacdHFRO4pTpTUCRhlAVSbr9VaLGv4D1eVopIoy/sxmSzsNEMu9ay50+xQLEoh2FWKGgZDSls= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741618412; c=relaxed/simple; bh=ZyOnJKWlzXtbmES+BvhM6pQYqEddvDRTqnmzVnKP/PM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=sfpwHwogXlVDXRyfHhZs5x8KpnyGhkLPUVLTuX8jZS3L578oSd14mBhRiu9Q4cBMlTWJ+KZzTHYY26Jb1dEv+jRr+zSsT/LOfbwlEaVtnZBKf0eyrMV04h0PpGai6OVCFmwfrpzs5UvejyB0wDIkr5mzQDel9ZenkPdI5FVDNuE= 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=FlqsXr2t; arc=none smtp.client-ip=209.85.214.175 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 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="FlqsXr2t" Received: by mail-pl1-f175.google.com with SMTP id d9443c01a7336-22401f4d35aso76691105ad.2 for ; Mon, 10 Mar 2025 07:53:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741618409; x=1742223209; darn=vger.kernel.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=U5ByeyW+YvFTHyUcGUYHAQSBVf/eKXKcnHA82UZlOkk=; b=FlqsXr2t3BG149e5/H3c6GJX4gDschb0USesSOT/o/BDd7PlLc870z7XoWl7noRg0P ZEiScFswFIHkSRyT2/VmupApLjTlg9sWbF94sM3J5ll3d+UwspWsUbwvX/RCE8UJHeLO Cdnma9dot/xP1RQlweYhiWTszsEXLMHPEdlHZ4gDAfno9Z4OLhr6E/yvbiEbi9DPwE6i buN4fDvaSHT8blO7D5KMYtmJtJUzrCbEOxb38vTGnbCCcS/ZAG7p4RVXS0/ui7f/k5BW 3PrCSUm7BSPpNPNQ0ZGUJSMDA2yzVDlxwRMyHmHGV088aiup8C1e3nOwGZJPOLuymPft HiiA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741618409; x=1742223209; 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=U5ByeyW+YvFTHyUcGUYHAQSBVf/eKXKcnHA82UZlOkk=; b=M4k2AsBjJdINCiQm9/qcDS0RiPtCTIQlOHOs9PQ3D2JjshEPNa1hfXZV+HW5/DkUuI pEZMzuSz78OuqTmvDYncgSlbjdEpmHyAcI8JgUqMVXljf+t2+aDnfS84TscAnz9GeT7I B2PigIlv/kNUZ9Mrnqm4c2fVlYmrbn1EDmIj5Sp2a9Gm7/Q0U0K6qIz+2XMtE/m8ZhZ3 fxg37a2Ky06/RHL2/yt4HpERlxFG0l0rJf+T8IhfD36wC7lxNrxqX3m9xzWEL7cr6lTJ waFn52prhfYtti1zZbomaXp+sKQs8W283qmO1V2EsZ5m0LiF2u3RxzlOYK/XQxMgwmq7 j0mQ== X-Gm-Message-State: AOJu0YzqLzUOsOhsGYxUqHsZOc5hIdvDOu9WKCLNuAkqSUctIoven0V1 n4dx48ReOlcP13f31kwRsPHHJNjK8O37Y23Ned67VdhQf8b4/ib4LOni27KMvjQ= X-Gm-Gg: ASbGnctpkV3FF9/EZaxXUaGcFH6Y+KVKq63Zh5lByTzXCxO1k8nKCQcm869jL4xoo6i wecQKRMXRlVnu2D/YacjuSu9dUavhlfIsilrCLT8kR15pgFjRSwD99W9BCWD6dqfZTZhR5X22Th kHEtG5CamhEfGI5aAIMVXuAYi1HDr/djgyv9C6yk35XVHI8Oj9rCN8oqh98qs21j7/DIMleowcV NGk8MKctNqXN5iwkc6e1snDjiSBlY7HghYyElq1gmtpw1f64lPHg7Jc3SkQAVD8PHi8WaDVfpCC Yhdao8lwZxWUispqFzZF1oi8YuyHoDUBaXvqDsD+W46tPgF7rwmrzlU= X-Google-Smtp-Source: AGHT+IGKXHgNMuKPuSz9mbzSVY6kbhzly1ukgPS10UZrRIAfMtlVXU0y2E7jj8WGEeH+e3hYVRFUmw== X-Received: by 2002:a05:6a00:10d5:b0:736:47a5:e268 with SMTP id d2e1a72fcca58-736eb7b363bmr272521b3a.1.1741618408950; Mon, 10 Mar 2025 07:53:28 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-736d11d4600sm2890275b3a.116.2025.03.10.07.53.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 07:53:28 -0700 (PDT) From: Deepak Gupta Date: Mon, 10 Mar 2025 07:52:45 -0700 Subject: [PATCH v11 23/27] arch/riscv: compile vdso with landing pad Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250310-v5_user_cfi_series-v11-23-86b36cbfb910@rivosinc.com> References: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> In-Reply-To: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@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 , Jann Horn , Conor Dooley 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 From: Jim Shu user mode tasks compiled with zicfilp may call indirectly into vdso (like hwprobe indirect calls). Add landing pad compile support in vdso. vdso with landing pad in it will be nop for tasks which have not enabled landing pad. This patch allows to run user mode tasks with cfi eanbled and do no harm. Future work can be done on this to do below - labeled landing pad on vdso functions (whenever labeling support shows up in gnu-toolchain) - emit shadow stack instructions only in vdso compiled objects as part of kernel compile. Signed-off-by: Jim Shu Signed-off-by: Deepak Gupta Reviewed-by: Zong Li --- arch/riscv/Makefile | 7 +++++- arch/riscv/include/asm/assembler.h | 44 +++++++++++++++++++++++++++++++= ++++ arch/riscv/kernel/vdso/Makefile | 12 ++++++++++ arch/riscv/kernel/vdso/flush_icache.S | 4 ++++ arch/riscv/kernel/vdso/getcpu.S | 4 ++++ arch/riscv/kernel/vdso/rt_sigreturn.S | 4 ++++ arch/riscv/kernel/vdso/sys_hwprobe.S | 4 ++++ 7 files changed, 78 insertions(+), 1 deletion(-) diff --git a/arch/riscv/Makefile b/arch/riscv/Makefile index 13fbc0f94238..ea9468af2cb4 100644 --- a/arch/riscv/Makefile +++ b/arch/riscv/Makefile @@ -87,10 +87,15 @@ riscv-march-$(CONFIG_TOOLCHAIN_HAS_ZACAS) :=3D $(riscv-= march-y)_zacas =20 # Check if the toolchain supports Zabha riscv-march-$(CONFIG_TOOLCHAIN_HAS_ZABHA) :=3D $(riscv-march-y)_zabha +# Check if the toolchain supports Zihintpause extension +riscv-march-$(CONFIG_TOOLCHAIN_HAS_ZIHINTPAUSE) :=3D $(riscv-march-y)_zihi= ntpause + +KBUILD_BASE_ISA =3D -march=3D$(shell echo $(riscv-march-y) | sed -E 's/(rv= 32ima|rv64ima)fd([^v_]*)v?/\1\2/') +export KBUILD_BASE_ISA =20 # Remove F,D,V from isa string for all. Keep extensions between "fd" and "= v" by # matching non-v and non-multi-letter extensions out with the filter ([^v_= ]*) -KBUILD_CFLAGS +=3D -march=3D$(shell echo $(riscv-march-y) | sed -E 's/(rv3= 2ima|rv64ima)fd([^v_]*)v?/\1\2/') +KBUILD_CFLAGS +=3D $(KBUILD_BASE_ISA) =20 KBUILD_AFLAGS +=3D -march=3D$(riscv-march-y) =20 diff --git a/arch/riscv/include/asm/assembler.h b/arch/riscv/include/asm/as= sembler.h index 44b1457d3e95..a058ea5e9c58 100644 --- a/arch/riscv/include/asm/assembler.h +++ b/arch/riscv/include/asm/assembler.h @@ -80,3 +80,47 @@ .endm =20 #endif /* __ASM_ASSEMBLER_H */ + +#if defined(CONFIG_RISCV_USER_CFI) && (__riscv_xlen =3D=3D 64) +.macro vdso_lpad +lpad 0 +.endm +#else +.macro vdso_lpad +.endm +#endif + +/* + * This macro emits a program property note section identifying + * architecture features which require special handling, mainly for + * use in assembly files included in the VDSO. + */ +#define NT_GNU_PROPERTY_TYPE_0 5 +#define GNU_PROPERTY_RISCV_FEATURE_1_AND 0xc0000000 + +#define GNU_PROPERTY_RISCV_FEATURE_1_ZICFILP (1U << 0) +#define GNU_PROPERTY_RISCV_FEATURE_1_ZICFISS (1U << 1) + +#if defined(CONFIG_RISCV_USER_CFI) && (__riscv_xlen =3D=3D 64) +#define GNU_PROPERTY_RISCV_FEATURE_1_DEFAULT \ + (GNU_PROPERTY_RISCV_FEATURE_1_ZICFILP) +#endif + +#ifdef GNU_PROPERTY_RISCV_FEATURE_1_DEFAULT +.macro emit_riscv_feature_1_and, feat =3D GNU_PROPERTY_RISCV_FEATURE_1_DEF= AULT + .pushsection .note.gnu.property, "a" + .p2align 3 + .word 4 + .word 16 + .word NT_GNU_PROPERTY_TYPE_0 + .asciz "GNU" + .word GNU_PROPERTY_RISCV_FEATURE_1_AND + .word 4 + .word \feat + .word 0 + .popsection +.endm +#else +.macro emit_riscv_feature_1_and, feat =3D 0 +.endm +#endif diff --git a/arch/riscv/kernel/vdso/Makefile b/arch/riscv/kernel/vdso/Makef= ile index 9a1b555e8733..daa10c2b0dd1 100644 --- a/arch/riscv/kernel/vdso/Makefile +++ b/arch/riscv/kernel/vdso/Makefile @@ -13,12 +13,18 @@ vdso-syms +=3D flush_icache vdso-syms +=3D hwprobe vdso-syms +=3D sys_hwprobe =20 +ifdef CONFIG_RISCV_USER_CFI +LPAD_MARCH =3D _zicfilp +endif + # Files to link into the vdso obj-vdso =3D $(patsubst %, %.o, $(vdso-syms)) note.o =20 ccflags-y :=3D -fno-stack-protector ccflags-y +=3D -DDISABLE_BRANCH_PROFILING ccflags-y +=3D -fno-builtin +ccflags-y +=3D $(KBUILD_BASE_ISA)$(LPAD_MARCH) +asflags-y +=3D $(KBUILD_BASE_ISA)$(LPAD_MARCH) =20 ifneq ($(c-gettimeofday-y),) CFLAGS_vgettimeofday.o +=3D -fPIC -include $(c-gettimeofday-y) @@ -40,6 +46,12 @@ endif CFLAGS_REMOVE_vgettimeofday.o =3D $(CC_FLAGS_FTRACE) $(CC_FLAGS_SCS) CFLAGS_REMOVE_hwprobe.o =3D $(CC_FLAGS_FTRACE) $(CC_FLAGS_SCS) =20 +# Disable profiling and instrumentation for VDSO code +GCOV_PROFILE :=3D n +KCOV_INSTRUMENT :=3D n +KASAN_SANITIZE :=3D n +UBSAN_SANITIZE :=3D n + # Force dependency $(obj)/vdso.o: $(obj)/vdso.so =20 diff --git a/arch/riscv/kernel/vdso/flush_icache.S b/arch/riscv/kernel/vdso= /flush_icache.S index 8f884227e8bc..e4c56970905e 100644 --- a/arch/riscv/kernel/vdso/flush_icache.S +++ b/arch/riscv/kernel/vdso/flush_icache.S @@ -5,11 +5,13 @@ =20 #include #include +#include =20 .text /* int __vdso_flush_icache(void *start, void *end, unsigned long flags); */ SYM_FUNC_START(__vdso_flush_icache) .cfi_startproc + vdso_lpad #ifdef CONFIG_SMP li a7, __NR_riscv_flush_icache ecall @@ -20,3 +22,5 @@ SYM_FUNC_START(__vdso_flush_icache) ret .cfi_endproc SYM_FUNC_END(__vdso_flush_icache) + +emit_riscv_feature_1_and diff --git a/arch/riscv/kernel/vdso/getcpu.S b/arch/riscv/kernel/vdso/getcp= u.S index 9c1bd531907f..5c1ecc4e1465 100644 --- a/arch/riscv/kernel/vdso/getcpu.S +++ b/arch/riscv/kernel/vdso/getcpu.S @@ -5,14 +5,18 @@ =20 #include #include +#include =20 .text /* int __vdso_getcpu(unsigned *cpu, unsigned *node, void *unused); */ SYM_FUNC_START(__vdso_getcpu) .cfi_startproc + vdso_lpad /* For now, just do the syscall. */ li a7, __NR_getcpu ecall ret .cfi_endproc SYM_FUNC_END(__vdso_getcpu) + +emit_riscv_feature_1_and diff --git a/arch/riscv/kernel/vdso/rt_sigreturn.S b/arch/riscv/kernel/vdso= /rt_sigreturn.S index 3dc022aa8931..e82987dc3739 100644 --- a/arch/riscv/kernel/vdso/rt_sigreturn.S +++ b/arch/riscv/kernel/vdso/rt_sigreturn.S @@ -5,12 +5,16 @@ =20 #include #include +#include =20 .text SYM_FUNC_START(__vdso_rt_sigreturn) .cfi_startproc .cfi_signal_frame + vdso_lpad li a7, __NR_rt_sigreturn ecall .cfi_endproc SYM_FUNC_END(__vdso_rt_sigreturn) + +emit_riscv_feature_1_and diff --git a/arch/riscv/kernel/vdso/sys_hwprobe.S b/arch/riscv/kernel/vdso/= sys_hwprobe.S index 77e57f830521..f1694451a60c 100644 --- a/arch/riscv/kernel/vdso/sys_hwprobe.S +++ b/arch/riscv/kernel/vdso/sys_hwprobe.S @@ -3,13 +3,17 @@ =20 #include #include +#include =20 .text SYM_FUNC_START(riscv_hwprobe) .cfi_startproc + vdso_lpad li a7, __NR_riscv_hwprobe ecall ret =20 .cfi_endproc SYM_FUNC_END(riscv_hwprobe) + +emit_riscv_feature_1_and --=20 2.34.1 From nobody Tue Feb 10 09:58:08 2026 Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) (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 1429F23E33D for ; Mon, 10 Mar 2025 14:53:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741618414; cv=none; b=DMumDSLd0hp6K8mkcExX95lShgfStI1dy0+8a7PaDeD96nr/5vUi5I68K63+NT7UCXnm1WXwe3kOLQIDQSPZgN+oPH211++Ye0kjETV03Nd7fB1qWqkhpQTr3s8HH+2Qg6BJyMdR5tegiCaZ8iGPhyfQKfIYcCK+eLXg+oVU83c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741618414; c=relaxed/simple; bh=Eua84f2QnuJ0+u1eEfH6+3KRo1p7kHINzNbKNk8bPkI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=YHN44KYPlD9Nk5NgrwVzT2daLvViWHfZ1jg3XgxOSwzgMyRBfwxpKaSCOhBHA4Cgq/XWxFtUs0yZVd70URl+NwqZn8txY4+5wZedZue+EgJigfJ5/iRWItYCh848bQinJeqB54TVK6Yz43Ck/UEJoTbSKkmC6rVZiGpiabWoazE= 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=O8fbsLvd; arc=none smtp.client-ip=209.85.214.170 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 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="O8fbsLvd" Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-224191d92e4so73640505ad.3 for ; Mon, 10 Mar 2025 07:53:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741618411; x=1742223211; darn=vger.kernel.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=9LN1OOVFFK55bIQe9Bj7KSCICvDpdODqLxcVYOIQy28=; b=O8fbsLvdnD8UIpI0UlJYiIL1lmGzpKtFjWwms4QoY9YaH3g/9k1ytjjnEo0uM2m9Yy W2+Y6867Upup1zQj5IAU2dgH03j17eT/1zaY4BVmuJM23jmrrJXgehS3ICvbG7LfdfIZ QP2E02aEVi+J+ESa6fcPFwgqfT1ZxOVBUvlgBG8sImxddE4P/II57EuZQZcClr/HMaRD oSwMqUtUIZGhXodE0rBBd8Kx7q8fN3Z95Lo63cCYt1ivb9PhTmekIeiV6/dPI7mm1Uk7 12q7qlXE1j2tlsZ93G7j8IaJlk3TYZY8BGZDhnhxsag4zLwfdFjZDxV1TEZUwFAQzL9p YXig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741618411; x=1742223211; 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=9LN1OOVFFK55bIQe9Bj7KSCICvDpdODqLxcVYOIQy28=; b=HWQv83MD1QeTXdxS8mub2+vY06qgXKkrHq+XFX2OoES//e48NDZeJ0JDkUVeT/pn4k rYy+OOKfEUiAa+MA7ScuDgEp0GyKPO0BI8thtKT85+WwK6Pa6c8N/3++UdqXUhlrjRSj GRum55edEebpciSoIivBV7/c9WE2kZyo+e7/UwzGbAk4/WeazFJbNYVUlBwNonrdZKsR wrdhMRHqJJLx0XXTpqA7vVs65+334KSVSFxZLunGmwcwh8e0N/XvkB5bmX4ucvvjVdO8 SYiOfEUHxbVFikKTQ/pLxK4nJLBj1abejwYdHBJI0J2hyzAss5foCE9c5ib79OomeKls JNsw== X-Gm-Message-State: AOJu0YwGWvGUotE1HK4/aIfGeGcEKnPbiH/U0DIlIkphwHqwD9fu+Orq 6QVxhlTBSJvsR9r/I5yVud5h89u6eGIZYK/vlqiCa8ZUln/rcGlfcSM0S5EKyEg= X-Gm-Gg: ASbGncusfrdspMI0Bmj0O6QZuM+4pqTYkkPqXsoSr3OBOUmm/Ha9SD7nDPExiM1W2Pj VK7Szi1Fmpm2fmhBoIhml453+EV/3bsjb7Ej+2w2pRnWBEXJ2fUN0mjY5K8v3mDprLNSS8ClKiN hzHxORmrwDCBMfOR6MK8CuTA+L7bcv/7/B++bA/c9Kuj5WBdxvNP4GsjhbsWIRCtwQhChRf1PxI UEKBMUrhM8FES2wFqO5Sth2dZ+mVlQgViq+6qZ/f54cfPypVkxxO8587ibSlQGgDxq+FOeI6t4W sX+Sdh5V3EdEir68oyJMnEpKMn+Lr5tE5KKkftsVE5bi6y3J2xvmwVY= X-Google-Smtp-Source: AGHT+IG9tC1WaMP/xC1ABIbEOt50ns/8+sJ1nYsKd4Gr0OatbIpLJiaEKJdeufsJkmLrEp7EYv+TbQ== X-Received: by 2002:a05:6a00:928b:b0:736:5dc6:a14b with SMTP id d2e1a72fcca58-736aaa1ace3mr21470128b3a.13.1741618411393; Mon, 10 Mar 2025 07:53:31 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-736d11d4600sm2890275b3a.116.2025.03.10.07.53.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 07:53:31 -0700 (PDT) From: Deepak Gupta Date: Mon, 10 Mar 2025 07:52:46 -0700 Subject: [PATCH v11 24/27] riscv: create a config for shadow stack and landing pad instr support Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250310-v5_user_cfi_series-v11-24-86b36cbfb910@rivosinc.com> References: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> In-Reply-To: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@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 , Jann Horn , Conor Dooley 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 This patch creates a config for shadow stack support and landing pad instr support. Shadow stack support and landing instr support can be enabled by selecting `CONFIG_RISCV_USER_CFI`. Selecting `CONFIG_RISCV_USER_CFI` wires up path to enumerate CPU support and if cpu support exists, kernel will support cpu assisted user mode cfi. If CONFIG_RISCV_USER_CFI is selected, select `ARCH_USES_HIGH_VMA_FLAGS`, `ARCH_HAS_USER_SHADOW_STACK` and DYNAMIC_SIGFRAME for riscv. Signed-off-by: Deepak Gupta Reviewed-by: Zong Li --- arch/riscv/Kconfig | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index 7612c52e9b1e..0a2e50f056e8 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -250,6 +250,26 @@ config ARCH_HAS_BROKEN_DWARF5 # https://github.com/llvm/llvm-project/commit/7ffabb61a5569444b5ac9322e22= e5471cc5e4a77 depends on LD_IS_LLD && LLD_VERSION < 180000 =20 +config RISCV_USER_CFI + def_bool y + bool "riscv userspace control flow integrity" + depends on 64BIT && $(cc-option,-mabi=3Dlp64 -march=3Drv64ima_zicfiss) + depends on RISCV_ALTERNATIVE + select ARCH_HAS_USER_SHADOW_STACK + select ARCH_USES_HIGH_VMA_FLAGS + select DYNAMIC_SIGFRAME + help + Provides CPU assisted control flow integrity to userspace tasks. + Control flow integrity is provided by implementing shadow stack for + backward edge and indirect branch tracking for forward edge in program. + Shadow stack protection is a hardware feature that detects function + return address corruption. This helps mitigate ROP attacks. + Indirect branch tracking enforces that all indirect branches must land + on a landing pad instruction else CPU will fault. This mitigates against + JOP / COP attacks. Applications must be enabled to use it, and old user- + space does not get protection "for free". + default y + config ARCH_MMAP_RND_BITS_MIN default 18 if 64BIT default 8 --=20 2.34.1 From nobody Tue Feb 10 09:58:08 2026 Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) (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 143CF23ED58 for ; Mon, 10 Mar 2025 14:53:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741618418; cv=none; b=gjj7duXirpCncWN9g8MzDD15zPXCb7pW8XVuWGRYvm2AedupP/VYdRgjYP3rQxuduEsugrahqqiTGKKRhHARo9qA8PLnTSXwAfFQtdkgSOBH+tyyK4G6t7tfRzUPHq2HV9TcEqiWU/fmjxgrsyUOsR0f2dG+EJR/xS2OyicrD3k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741618418; c=relaxed/simple; bh=1sSn7aDfUjcOB92/7A7Venz1EjXj471Fv9kmdXzLrbs=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=t9VkO5BHGHwrF/XWgNxXadNhkI8gg2OcAvoYYCo7Fuz3jpXwNG19UIcFDUDwCnawd49sUxHkxaMHBX2EmWZwx92vXoAXqhyOQxZS4jfwKyn+YAzlRomdn0InmV9LKHeFt/Nt0oTAEnJyt7nhkfL8P2KmxL1X5imE3WCP4f8Qs1g= 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=NM9Z3YUI; arc=none smtp.client-ip=209.85.214.172 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 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="NM9Z3YUI" Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-22409077c06so10355455ad.1 for ; Mon, 10 Mar 2025 07:53:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741618415; x=1742223215; darn=vger.kernel.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=vh4CUvpz/fzJO7m3g4Lrx30VWLQNLgBESiJnDVTGIvg=; b=NM9Z3YUIqqVT36Sd5ugohAchChJFL6shcTEI8kDE8FboE1uTFXdnDvBwHZ9eI9Eyjj zfRnwq5pfVm9SdLWuddrtoh1TetK+ZCTqZaq2Tbdy/P2dfhk78e/qtD7ZnVVyXd1M+9x M3dRJ/36AOLtlZyVAFN/qY5mSACTJd2oBwrzBDDoLjF9zFopiA724vCWeTfbp6H1T37X aUmxkAwqz4pjJcWRPjpKlrGZ2zHVtbv5WwZqn/vOKargXp5qLvawDaLTBQHUXSukXo2t oKCsfnAovaPwuc4PHYQSCYxT7L+CkJwMgjsyot0NN+AMefrklrLd2p4JjRmjJyRSH+3w r6+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741618415; x=1742223215; 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=vh4CUvpz/fzJO7m3g4Lrx30VWLQNLgBESiJnDVTGIvg=; b=IsFkPvzWoUfgS62NJAqhqeFdwQtZkHr5hmZXJicVqZFQDTC6cKmDbyKW8mCitzOIVo RuPyfCSW/ggazkdX0fYSuuSwOBuwxTm3egJboHqVT6MmSb37NTAE3f6HUb+kJKlrsOvH XmMWNc7xHp0T5Kvt6wcMlV770aL1asiMgiUx2OD9FCaVexNs6ThctePwk7myGggvpnoC thWqis/g263LQFUjIu2zKQQbDaIEuVeLuAeojc3w/KiZHNfH17qoy9D/leA7Nz8VXkDv kFrrjI3NCAmZMWBQZwDVT7ao/Ri1TBwUMyOAUz0CAXXudZB3Y8t+dAI0B+5SHxYHWuGX mGRg== X-Gm-Message-State: AOJu0Yy+ZVZWh3wXucKHYVAuVH06Vft99N743/K7sktI7y096FxCacCr TyrGbws2U4Dq7lqd/KLZK3dRA7Jsijxp1cqEE2qpLcbkk+WaJqYs29HankTLEC4= X-Gm-Gg: ASbGncup/+Jdt+Y6+VTrP3mZCdyZ8v0LaAskEsgil3404kSLUsjzZp9f6FHOoVyDYt/ 3d6VqrwmJirWbZNBNqJXbOqMD7QSTvbOkg6MifVNEnzOKStJ1xndC3I7M3DaPnxxnpqLiAhEkzP j1NUcSU0bWZ5D2qXEWQCx5U65eOK/7was3YJ2dlAdU8DNm714ydgm1Mpx/AieVtv6VkIpDTud/m NmuEv3axGZDpvCK3cpvuFfm+2HJOdrSviixHtjxBhlJmjSfexoYcuVrcWcZGDhzpeHYdD6ah0mD dUKw/0gSvUzx6gcrx/My5TBrXYtDyHvTrAr0JlNcVl/iHpBpLxJ8eCE= X-Google-Smtp-Source: AGHT+IHk6fUGtyziYf/rEaJOxRl/jM9mUkmR597ae136siDXwL+YfMuIUi90ii9WUsyzBw7fk0+kRg== X-Received: by 2002:a05:6a21:158c:b0:1f5:837b:186a with SMTP id adf61e73a8af0-1f5837b19c4mr4882849637.6.1741618413790; Mon, 10 Mar 2025 07:53:33 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-736d11d4600sm2890275b3a.116.2025.03.10.07.53.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 07:53:33 -0700 (PDT) From: Deepak Gupta Date: Mon, 10 Mar 2025 07:52:47 -0700 Subject: [PATCH v11 25/27] riscv: Documentation for landing pad / indirect branch tracking Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250310-v5_user_cfi_series-v11-25-86b36cbfb910@rivosinc.com> References: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> In-Reply-To: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@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 , Jann Horn , Conor Dooley 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 Adding documentation on landing pad aka indirect branch tracking on riscv and kernel interfaces exposed so that user tasks can enable it. Signed-off-by: Deepak Gupta Reviewed-by: Zong Li --- Documentation/arch/riscv/index.rst | 1 + Documentation/arch/riscv/zicfilp.rst | 115 +++++++++++++++++++++++++++++++= ++++ 2 files changed, 116 insertions(+) diff --git a/Documentation/arch/riscv/index.rst b/Documentation/arch/riscv/= index.rst index eecf347ce849..be7237b69682 100644 --- a/Documentation/arch/riscv/index.rst +++ b/Documentation/arch/riscv/index.rst @@ -14,6 +14,7 @@ RISC-V architecture uabi vector cmodx + zicfilp =20 features =20 diff --git a/Documentation/arch/riscv/zicfilp.rst b/Documentation/arch/risc= v/zicfilp.rst new file mode 100644 index 000000000000..a188d78fcde6 --- /dev/null +++ b/Documentation/arch/riscv/zicfilp.rst @@ -0,0 +1,115 @@ +.. SPDX-License-Identifier: GPL-2.0 + +:Author: Deepak Gupta +:Date: 12 January 2024 + +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D +Tracking indirect control transfers on RISC-V Linux +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D + +This document briefly describes the interface provided to userspace by Lin= ux +to enable indirect branch tracking for user mode applications on RISV-V + +1. Feature Overview +-------------------- + +Memory corruption issues usually result in to crashes, however when in han= ds of +an adversary and if used creatively can result into variety security issue= s. + +One of those security issues can be code re-use attacks on program where a= dversary +can use corrupt function pointers and chain them together to perform jump = oriented +programming (JOP) or call oriented programming (COP) and thus compromising= control +flow integrity (CFI) of the program. + +Function pointers live in read-write memory and thus are susceptible to co= rruption +and allows an adversary to reach any program counter (PC) in address space= . On +RISC-V zicfilp extension enforces a restriction on such indirect control +transfers: + +- indirect control transfers must land on a landing pad instruction ``lpad= ``. + There are two exception to this rule: + + - rs1 =3D x1 or rs1 =3D x5, i.e. a return from a function and returns are + protected using shadow stack (see zicfiss.rst) + + - rs1 =3D x7. On RISC-V compiler usually does below to reach function + which is beyond the offset possible J-type instruction:: + + auipc x7, + jalr (x7) + + Such form of indirect control transfer are still immutable and don't rely + on memory and thus rs1=3Dx7 is exempted from tracking and considered s= oftware + guarded jumps. + +``lpad`` instruction is pseudo of ``auipc rd, `` with ``rd=3Dx0= `` and +is a HINT nop. ``lpad`` instruction must be aligned on 4 byte boundary and +compares 20 bit immediate withx7. If ``imm_20bit`` =3D=3D 0, CPU don't per= form any +comparision with ``x7``. If ``imm_20bit`` !=3D 0, then ``imm_20bit`` must = match +``x7`` else CPU will raise ``software check exception`` (``cause=3D18``) w= ith +``*tval =3D 2``. + +Compiler can generate a hash over function signatures and setup them (trun= cated +to 20bit) in x7 at callsites and function prologues can have ``lpad`` with= same +function hash. This further reduces number of program counters a call site= can +reach. + +2. ELF and psABI +----------------- + +Toolchain sets up :c:macro:`GNU_PROPERTY_RISCV_FEATURE_1_FCFI` for property +:c:macro:`GNU_PROPERTY_RISCV_FEATURE_1_AND` in notes section of the object= file. + +3. Linux enabling +------------------ + +User space programs can have multiple shared objects loaded in its address= space +and it's a difficult task to make sure all the dependencies have been comp= iled +with support of indirect branch. Thus it's left to dynamic loader to enable +indirect branch tracking for the program. + +4. prctl() enabling +-------------------- + +:c:macro:`PR_SET_INDIR_BR_LP_STATUS` / :c:macro:`PR_GET_INDIR_BR_LP_STATUS= ` / +:c:macro:`PR_LOCK_INDIR_BR_LP_STATUS` are three prctls added to manage ind= irect +branch tracking. prctls are arch agnostic and returns -EINVAL on other arc= hes. + +* prctl(PR_SET_INDIR_BR_LP_STATUS, unsigned long arg) + +If arg1 is :c:macro:`PR_INDIR_BR_LP_ENABLE` and if CPU supports ``zicfilp`` +then kernel will enabled indirect branch tracking for the task. Dynamic lo= ader +can issue this :c:macro:`prctl` once it has determined that all the objects +loaded in address space support indirect branch tracking. Additionally if = there +is a `dlopen` to an object which wasn't compiled with ``zicfilp``, dynamic +loader can issue this prctl with arg1 set to 0 (i.e. +:c:macro:`PR_INDIR_BR_LP_ENABLE` being clear) + +* prctl(PR_GET_INDIR_BR_LP_STATUS, unsigned long arg) + +Returns current status of indirect branch tracking. If enabled it'll return +:c:macro:`PR_INDIR_BR_LP_ENABLE` + +* prctl(PR_LOCK_INDIR_BR_LP_STATUS, unsigned long arg) + +Locks current status of indirect branch tracking on the task. User space m= ay +want to run with strict security posture and wouldn't want loading of obje= cts +without ``zicfilp`` support in it and thus would want to disallow disablin= g of +indirect branch tracking. In that case user space can use this prctl to lo= ck +current settings. + +5. violations related to indirect branch tracking +-------------------------------------------------- + +Pertaining to indirect branch tracking, CPU raises software check exceptio= n in +following conditions: + +- missing ``lpad`` after indirect call / jmp +- ``lpad`` not on 4 byte boundary +- ``imm_20bit`` embedded in ``lpad`` instruction doesn't match with ``x7`` + +In all 3 cases, ``*tval =3D 2`` is captured and software check exception is +raised (``cause=3D18``) + +Linux kernel will treat this as :c:macro:`SIGSEV`` with code =3D +:c:macro:`SEGV_CPERR` and follow normal course of signal delivery. --=20 2.34.1 From nobody Tue Feb 10 09:58:08 2026 Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.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 CCA8923ED76 for ; Mon, 10 Mar 2025 14:53:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741618419; cv=none; b=FlVV9p9iVkyP8MWvY5TVwXaJRmyk6wUZr9hAY7FsTn4zfo3JRVgjCxhKq5cfafKFwVfRW1wd0xKL6u+cB8uv2U7SbczRfYo5mk3gmIFNKl+20J0ulol9NyG5qj8uYP/YhJVH8T+srI3PUSWBSC95Pxqv/gVplW+5f9Jq2UYEpbw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741618419; c=relaxed/simple; bh=o8EriA1inC7Heg917dIVYKG2AAC4jWIU3SL9yJoIqng=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=C7HSs8I5CywIhK96gsLwrAIT3MUSV3BSgnSU7QjVvoqnui427/I1CZUlb/J7W7/bxOzEWZQuCJRR1ClCeQ3rSZysoQsoFuVcvRdAZQUVrslfd6eTqQpRtYcVjtMIEOl3PlthKgd6JE3U/5Y9Eb9QsfBuaK2rlM0JnTn//pucY1E= 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=vqVfAmUI; arc=none smtp.client-ip=209.85.214.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 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="vqVfAmUI" Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-223594b3c6dso76396875ad.2 for ; Mon, 10 Mar 2025 07:53:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741618416; x=1742223216; darn=vger.kernel.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=tdDNQkSVTKtNTxsV0uSmAnlAiDQNW7ggaTdWphzDp8I=; b=vqVfAmUIeSLsKZpVUcenMx3iA0NsdIqkEXwKjudMTi5Lkcr9lENJgGj4GQf5lvwldD Nc6URQgtTlSfD3D8h113d4HKtz7eS1teZ461xB0UdVzm2b3UCmzL27mrnvOU+wGI790z iYccPjiiw1wFvG6PkBmI2h262oiMoq0bpJd3Pna4K//r8k4moOiFoadA3ph09j/8m0U6 JbgfCM7uENp44E+q3yfrHHPYtcC99iE3JXiiehim6uwj04JGSoC9TDCSLbrh8IpxT3A0 NWjEMCd/sws5mP6u64Wqdp/6SCvgzmE/xpTnuY24lNHXRzKrNaN1wShoQX9MrwbbNmON QYlA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741618416; x=1742223216; 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=tdDNQkSVTKtNTxsV0uSmAnlAiDQNW7ggaTdWphzDp8I=; b=kLB4Mz2Lt78Qg6PrylfsLAG3Cl/Ei0mrPdS19qzxESj0DlzgTMpYazjr85L3J/bgib /S/9RSDNvYJzO+trykuut93Ud914R5jK1Z5U/MEBXhTPc3zWedE/+e4juKMbjSUZzv3J 4TWptjNPyG1FZQkOWB/qnBPo6OFC852QM747+9klacOt1m+bypZWLTiFh1kL/+9CVzCY b2euG3CaLk6xahM41hakkZHsH5P2lMgWXOHfz/YSaxv/tPegjDXM/h9jE25umzzWfs27 aPPE2sMLpzsiXvyDndrbXt2fARa/8Nez+SRU0nO2kNS6GOhtr7tRZsx4NQV/lLqedb8/ UHpg== X-Gm-Message-State: AOJu0YzqfHifx5vSjqDaqTQ3/WKNnUPYnFlIKEX4mw8WNn7xRh/C64B+ 78mHV3DoKXq8XwRurAVT4z+iwP6Any395YAK9crj0y+2ddpO95KCAAt6aNOY2dI= X-Gm-Gg: ASbGncuczsD5Ut0JqnHnNXOhAgUi3GomKPR9XHrwO1WqyPM+1xO1ec2YWMZSVcTqlOG ARXRolgiREU0EL9v/5+Zwsg0AN+FNjIFard54ivS9Yxmvq9xiH39ZGNh0dXtbw9qoazJ7NJFurC xHslSGQrs0Ggu9U0wRndAvpC0seWHZajBzpPTmP0+lCldVp9p1Qff+CRub+9HFhtjWCqLV1Aj+/ 1ZzHbxYmbbLGYf74oCHE4307fQwI0xVIvYGj5IfjWDjRMKGJhYHI7NAFfLnDrVGgovrJSXiGPYv okQXlTf1pM9vdBz/XYgK5lHhrqxQcl2fM4uVIZR3a2BaM0oy0/Q9GZs= X-Google-Smtp-Source: AGHT+IFaFODVJO35zmAo4CgY/g0ssVNrnGer/dAkzpFZFHLuf8O1kgqUgyNRnEx4GJyvPtN1uJ38Mg== X-Received: by 2002:a05:6a21:4606:b0:1f5:7df9:f147 with SMTP id adf61e73a8af0-1f57df9f49dmr7386102637.40.1741618416216; Mon, 10 Mar 2025 07:53:36 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-736d11d4600sm2890275b3a.116.2025.03.10.07.53.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 07:53:35 -0700 (PDT) From: Deepak Gupta Date: Mon, 10 Mar 2025 07:52:48 -0700 Subject: [PATCH v11 26/27] riscv: Documentation for shadow stack on riscv Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250310-v5_user_cfi_series-v11-26-86b36cbfb910@rivosinc.com> References: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> In-Reply-To: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@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 , Jann Horn , Conor Dooley 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 Adding documentation on shadow stack for user mode on riscv and kernel interfaces exposed so that user tasks can enable it. Signed-off-by: Deepak Gupta Reviewed-by: Zong Li --- Documentation/arch/riscv/index.rst | 1 + Documentation/arch/riscv/zicfiss.rst | 176 +++++++++++++++++++++++++++++++= ++++ 2 files changed, 177 insertions(+) diff --git a/Documentation/arch/riscv/index.rst b/Documentation/arch/riscv/= index.rst index be7237b69682..e240eb0ceb70 100644 --- a/Documentation/arch/riscv/index.rst +++ b/Documentation/arch/riscv/index.rst @@ -15,6 +15,7 @@ RISC-V architecture vector cmodx zicfilp + zicfiss =20 features =20 diff --git a/Documentation/arch/riscv/zicfiss.rst b/Documentation/arch/risc= v/zicfiss.rst new file mode 100644 index 000000000000..5ba389f15b3f --- /dev/null +++ b/Documentation/arch/riscv/zicfiss.rst @@ -0,0 +1,176 @@ +.. SPDX-License-Identifier: GPL-2.0 + +:Author: Deepak Gupta +:Date: 12 January 2024 + +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D +Shadow stack to protect function returns on RISC-V Linux +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D + +This document briefly describes the interface provided to userspace by Lin= ux +to enable shadow stack for user mode applications on RISV-V + +1. Feature Overview +-------------------- + +Memory corruption issues usually result in to crashes, however when in han= ds of +an adversary and if used creatively can result into variety security issue= s. + +One of those security issues can be code re-use attacks on program where +adversary can use corrupt return addresses present on stack and chain them +together to perform return oriented programming (ROP) and thus compromising +control flow integrity (CFI) of the program. + +Return addresses live on stack and thus in read-write memory and thus are +susceptible to corruption and allows an adversary to reach any program cou= nter +(PC) in address space. On RISC-V ``zicfiss`` extension provides an alterna= te +stack termed as shadow stack on which return addresses can be safely place= d in +prolog of the function and retrieved in epilog. ``zicfiss`` extension makes +following changes: + +- PTE encodings for shadow stack virtual memory + An earlier reserved encoding in first stage translation i.e. + PTE.R=3D0, PTE.W=3D1, PTE.X=3D0 becomes PTE encoding for shadow stack p= ages. + +- ``sspush x1/x5`` instruction pushes (stores) ``x1/x5`` to shadow stack. + +- ``sspopchk x1/x5`` instruction pops (loads) from shadow stack and compar= es + with ``x1/x5`` and if un-equal, CPU raises ``software check exception`` = with + ``*tval =3D 3`` + +Compiler toolchain makes sure that function prologue have ``sspush x1/x5``= to +save return address on shadow stack in addition to regular stack. Similarly +function epilogs have ``ld x5, offset(x2)`` followed by ``sspopchk x5`` to +ensure that popped value from regular stack matches with popped value from +shadow stack. + +2. Shadow stack protections and linux memory manager +----------------------------------------------------- + +As mentioned earlier, shadow stack get new page table encodings and thus h= ave +some special properties assigned to them and instructions that operate on = them +as below: + +- Regular stores to shadow stack memory raises access store faults. This w= ay + shadow stack memory is protected from stray inadvertant writes. + +- Regular loads to shadow stack memory are allowed. This allows stack trace + utilities or backtrace functions to read true callstack (not tampered). + +- Only shadow stack instructions can generate shadow stack load or shadow = stack + store. + +- Shadow stack load / shadow stack store on read-only memory raises AMO/st= ore + page fault. Thus both ``sspush x1/x5`` and ``sspopchk x1/x5`` will raise= AMO/ + store page fault. This simplies COW handling in kernel During fork, kern= el + can convert shadow stack pages into read-only memory (as it does for reg= ular + read-write memory) and as soon as subsequent ``sspush`` or ``sspopchk`` = in + userspace is encountered, then kernel can perform COW. + +- Shadow stack load / shadow stack store on read-write, read-write-execute + memory raises an access fault. This is a fatal condition because shadow = stack + should never be operating on read-write, read-write-execute memory. + +3. ELF and psABI +----------------- + +Toolchain sets up :c:macro:`GNU_PROPERTY_RISCV_FEATURE_1_BCFI` for property +:c:macro:`GNU_PROPERTY_RISCV_FEATURE_1_AND` in notes section of the object= file. + +4. Linux enabling +------------------ + +User space programs can have multiple shared objects loaded in its address= space +and it's a difficult task to make sure all the dependencies have been comp= iled +with support of shadow stack. Thus it's left to dynamic loader to enable +shadow stack for the program. + +5. prctl() enabling +-------------------- + +:c:macro:`PR_SET_SHADOW_STACK_STATUS` / :c:macro:`PR_GET_SHADOW_STACK_STAT= US` / +:c:macro:`PR_LOCK_SHADOW_STACK_STATUS` are three prctls added to manage sh= adow +stack enabling for tasks. prctls are arch agnostic and returns -EINVAL on = other +arches. + +* prctl(PR_SET_SHADOW_STACK_STATUS, unsigned long arg) + +If arg1 :c:macro:`PR_SHADOW_STACK_ENABLE` and if CPU supports ``zicfiss`` = then +kernel will enable shadow stack for the task. Dynamic loader can issue this +:c:macro:`prctl` once it has determined that all the objects loaded in add= ress +space have support for shadow stack. Additionally if there is a +:c:macro:`dlopen` to an object which wasn't compiled with ``zicfiss``, dyn= amic +loader can issue this prctl with arg1 set to 0 (i.e. +:c:macro:`PR_SHADOW_STACK_ENABLE` being clear) + +* prctl(PR_GET_SHADOW_STACK_STATUS, unsigned long *arg) + +Returns current status of indirect branch tracking. If enabled it'll return +:c:macro:`PR_SHADOW_STACK_ENABLE`. + +* prctl(PR_LOCK_SHADOW_STACK_STATUS, unsigned long arg) + +Locks current status of shadow stack enabling on the task. User space may = want +to run with strict security posture and wouldn't want loading of objects +without ``zicfiss`` support in it and thus would want to disallow disablin= g of +shadow stack on current task. In that case user space can use this prctl to +lock current settings. + +5. violations related to returns with shadow stack enabled +----------------------------------------------------------- + +Pertaining to shadow stack, CPU raises software check exception in followi= ng +condition: + +- On execution of ``sspopchk x1/x5``, ``x1/x5`` didn't match top of shadow + stack. If mismatch happens then cpu does ``*tval =3D 3`` and raise softw= are + check exception. + +Linux kernel will treat this as :c:macro:`SIGSEV`` with code =3D +:c:macro:`SEGV_CPERR` and follow normal course of signal delivery. + +6. Shadow stack tokens +----------------------- +Regular stores on shadow stacks are not allowed and thus can't be tampered +with via arbitrary stray writes due to bugs. Method of pivoting / switchin= g to +shadow stack is simply writing to csr ``CSR_SSP`` changes active shadow st= ack. +This can be problematic because usually value to be written to ``CSR_SSP``= will +be loaded somewhere in writeable memory and thus allows an adversary to +corruption bug in software to pivot to an any address in shadow stack rang= e. +Shadow stack tokens can help mitigate this problem by making sure that: + +- When software is switching away from a shadow stack, shadow stack pointer + should be saved on shadow stack itself and call it ``shadow stack token`` + +- When software is switching to a shadow stack, it should read the + ``shadow stack token`` from shadow stack pointer and verify that + ``shadow stack token`` itself is pointer to shadow stack itself. + +- Once the token verification is done, software can perform the write to + ``CSR_SSP`` to switch shadow stack. + +Here software can be user mode task runtime itself which is managing vario= us +contexts as part of single thread. Software can be kernel as well when ker= nel +has to deliver a signal to user task and must save shadow stack pointer. K= ernel +can perform similar procedure by saving a token on user shadow stack itsel= f. +This way whenever :c:macro:`sigreturn` happens, kernel can read the token = and +verify the token and then switch to shadow stack. Using this mechanism, ke= rnel +helps user task so that any corruption issue in user task is not exploited= by +adversary by arbitrarily using :c:macro:`sigreturn`. Adversary will have to +make sure that there is a ``shadow stack token`` in addition to invoking +:c:macro:`sigreturn` + +7. Signal shadow stack +----------------------- +Following structure has been added to sigcontext for RISC-V:: + + struct __sc_riscv_cfi_state { + unsigned long ss_ptr; + }; + +As part of signal delivery, shadow stack token is saved on current shadow = stack +itself and updated pointer is saved away in :c:macro:`ss_ptr` field in +:c:macro:`__sc_riscv_cfi_state` under :c:macro:`sigcontext`. Existing shad= ow +stack allocation is used for signal delivery. During :c:macro:`sigreturn`, +kernel will obtain :c:macro:`ss_ptr` from :c:macro:`sigcontext` and verify= the +saved token on shadow stack itself and switch shadow stack. --=20 2.34.1 From nobody Tue Feb 10 09:58:08 2026 Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) (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 6FF8F23F387 for ; Mon, 10 Mar 2025 14:53:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741618422; cv=none; b=UpEcimH5KU5jgPREi0QVvOlCYWF07jyVU+ZRXhQEoygsSXMqXl6+0YUd3KHRdNFN9nhP1FLOx9T8E+SNWKgloerbQ2UQcyFy7sZjbiTBCItG5Xe/63PBKRz/GtDLZVZtdwvod7IpKy0aaVmPeC8n/bpBJd30YXlwfiSw7FFcQjg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741618422; c=relaxed/simple; bh=ALqPfs+yE9Sutp4spxrhbCTh8xfaqVKddbYQ02jvGHs=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=jvQv4A3hR99WRHIa4AR4NIYCM5y1tHpJ5xhqusgUf7r24COzhFfjuODYKiZdD+kuNZ2reuzPf2vfgZJAHcpOb9GWjN522ss8vEp8zgEeyqeQDVMtQjB8Yc30og24M8tiZZr1bd2VnLfxpGTStxpKxZInbAw91EsRH/oMR81Vsm0= 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=y9qvhmSa; arc=none smtp.client-ip=209.85.214.173 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 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="y9qvhmSa" Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-22423adf751so60805845ad.2 for ; Mon, 10 Mar 2025 07:53:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741618419; x=1742223219; darn=vger.kernel.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=ruj/JnwPHtkrEOpCEVLVNX7RrlNYJUu0NWWeo3U6Fsk=; b=y9qvhmSacAc+g6wExBnKESRGefew1nuQXU8kY/VQ4bKMy2I8iYJZbB1PkybxKcfO+Q z/JndC07blwQc7iZy9kMM8Q+rcsgl+6ss/cOBVhC/ug7+lGoXGuId7OEOBnEEgIqaq+l +SJhFBFH/2hmkMnwKiPjBMgGOAAL+r6yoQEZJRcFOGB4WRopFJ0LqRNVA8UWQqkYGrKU w0FaS4tx2pkPA8J/X1mGAXUEbg9ERYnC0Hwefj7jdbngBglhXXlwe5W6QBoegY5tKbSN 6/Vrf1RGhelD1RqmDV/CN2xQizF8Xy0wusZwMIf7w9l2a/GmMhA/tnHPTBGhPkdOZTWd PLXQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741618419; x=1742223219; 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=ruj/JnwPHtkrEOpCEVLVNX7RrlNYJUu0NWWeo3U6Fsk=; b=FkIKUR7nl0gpbij1G5+SAef05xPjeLNSL0SjImJ2BRIhzZHfYOPgU+9K6nsMJlTl9M SvZy3Dvdo3oGgntUP9WTOMtCXbczIAoSyuMXDkwdZcWEYhJeTphclqDJ7E86H3adIWMY YipH1Z2jv9JiN1IarHVt+lMNcX/pRLz/xIwk6pWjuZWKgxuQ7vxuORUtjAcUiZaCrwoq xE5CJQWf0gZKTpMBSGMgE7+VRu52p2Zo76kZ3/ihhzYuPL9C//Kps3IvqjFXIc2XaR5R qwsLEQFCB7p1bexVvq0sN5VE5XOOd4IGyDkSCPqAtmvgntfWMUq3LzZY0wcqGHSLxjuV NSGw== X-Gm-Message-State: AOJu0YyfLErmh64k9ba0BMuhW9R70SJYLlMVPqWHIPTbzDriHXmn1yl/ eYHA3Hmpzik7+rXwFXW0VVH8Jl+si8VM1dIGCbVJd80W1aB8FaoBO9c8+3jVgeg= X-Gm-Gg: ASbGncuW98Ii9xHRiCRyyCpCQU2OvEPFEZ8qbKMkWMUKLBauCM2NTuH8UPlkzHWCWJq 0IeYvKA9nHcl8dmjQ75W3r4JOjatXTXN1rdcdh5X05z2Arno2jSdhdfx8gIbHAVoRpvIu56dH/q wtrqQrKuohjBBsZ4LMw3w16GMypF9Vh+0w1KfPVl1fKLJYWnpUbqFkc0bO+Faq3xiAB9o+P1XqF UivdEgXi+vvoYddj6snxslaZ3jJ9d2xKnMWFU639B7z650OpRh7OF31jX28JeGLh+Ex+Pyltw+g pOfQxh7b6g/boMhbhtJFWyG4l3EMyi/LiYzirUG/VCa0MlQSw2jFqoY= X-Google-Smtp-Source: AGHT+IGv7tM2PHL9ys+s6t6SEB0A82+OsRTHqO1WWLNqv0N4xUl6sYQq+4x8MhxgrmcY2aWh78Ovvw== X-Received: by 2002:a05:6a00:1ad2:b0:730:4c55:4fdf with SMTP id d2e1a72fcca58-736eb7ca1f6mr271137b3a.7.1741618418779; Mon, 10 Mar 2025 07:53:38 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-736d11d4600sm2890275b3a.116.2025.03.10.07.53.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 07:53:38 -0700 (PDT) From: Deepak Gupta Date: Mon, 10 Mar 2025 07:52:49 -0700 Subject: [PATCH v11 27/27] kselftest/riscv: kselftest for user mode cfi Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250310-v5_user_cfi_series-v11-27-86b36cbfb910@rivosinc.com> References: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> In-Reply-To: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@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 , Jann Horn , Conor Dooley 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 Adds kselftest for RISC-V control flow integrity implementation for user mode. There is not a lot going on in kernel for enabling landing pad for user mode. cfi selftest are intended to be compiled with zicfilp and zicfiss enabled compiler. Thus kselftest simply checks if landing pad and shadow stack for the binary and process are enabled or not. selftest then register a signal handler for SIGSEGV. Any control flow violation are reported as SIGSEGV with si_code =3D SEGV_CPERR. Test will fail on receiving any SEGV_CPERR. Shadow stack part has more changes in kernel and thus there are separate tests for that - Exercise `map_shadow_stack` syscall - `fork` test to make sure COW works for shadow stack pages - gup tests Kernel uses FOLL_FORCE when access happens to memory via /proc//mem. Not breaking that for shadow stack. - signal test. Make sure signal delivery results in token creation on shadow stack and consumes (and verifies) token on sigreturn - shadow stack protection test. attempts to write using regular store instruction on shadow stack memory must result in access faults Test outut =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D """ TAP version 13 1..5 This is to ensure shadow stack is indeed enabled and working This is to ensure shadow stack is indeed enabled and working ok 1 shstk fork test ok 2 map shadow stack syscall ok 3 shadow stack gup tests ok 4 shadow stack signal tests ok 5 memory protections of shadow stack memory """ Signed-off-by: Deepak Gupta --- tools/testing/selftests/riscv/Makefile | 2 +- tools/testing/selftests/riscv/cfi/.gitignore | 3 + tools/testing/selftests/riscv/cfi/Makefile | 10 + tools/testing/selftests/riscv/cfi/cfi_rv_test.h | 84 +++++ tools/testing/selftests/riscv/cfi/riscv_cfi_test.c | 78 +++++ tools/testing/selftests/riscv/cfi/shadowstack.c | 375 +++++++++++++++++= ++++ tools/testing/selftests/riscv/cfi/shadowstack.h | 37 ++ 7 files changed, 588 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/riscv/Makefile b/tools/testing/selftes= ts/riscv/Makefile index 099b8c1f46f8..5671b4405a12 100644 --- a/tools/testing/selftests/riscv/Makefile +++ b/tools/testing/selftests/riscv/Makefile @@ -5,7 +5,7 @@ ARCH ?=3D $(shell uname -m 2>/dev/null || echo not) =20 ifneq (,$(filter $(ARCH),riscv)) -RISCV_SUBTARGETS ?=3D abi hwprobe mm sigreturn vector +RISCV_SUBTARGETS ?=3D abi hwprobe mm sigreturn vector cfi else RISCV_SUBTARGETS :=3D endif diff --git a/tools/testing/selftests/riscv/cfi/.gitignore b/tools/testing/s= elftests/riscv/cfi/.gitignore new file mode 100644 index 000000000000..82545863bac6 --- /dev/null +++ b/tools/testing/selftests/riscv/cfi/.gitignore @@ -0,0 +1,3 @@ +cfitests +riscv_cfi_test +shadowstack diff --git a/tools/testing/selftests/riscv/cfi/Makefile b/tools/testing/sel= ftests/riscv/cfi/Makefile new file mode 100644 index 000000000000..b65f7ff38a32 --- /dev/null +++ b/tools/testing/selftests/riscv/cfi/Makefile @@ -0,0 +1,10 @@ +CFLAGS +=3D -I$(top_srcdir)/tools/include + +CFLAGS +=3D -march=3Drv64gc_zicfilp_zicfiss + +TEST_GEN_PROGS :=3D cfitests + +include ../../lib.mk + +$(OUTPUT)/cfitests: riscv_cfi_test.c shadowstack.c + $(CC) -o$@ $(CFLAGS) $(LDFLAGS) $^ diff --git a/tools/testing/selftests/riscv/cfi/cfi_rv_test.h b/tools/testin= g/selftests/riscv/cfi/cfi_rv_test.h new file mode 100644 index 000000000000..a9d5d6f8e29c --- /dev/null +++ b/tools/testing/selftests/riscv/cfi/cfi_rv_test.h @@ -0,0 +1,84 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#ifndef SELFTEST_RISCV_CFI_H +#define SELFTEST_RISCV_CFI_H +#include +#include +#include "shadowstack.h" + +#define RISCV_CFI_SELFTEST_COUNT RISCV_SHADOW_STACK_TESTS + +#define CHILD_EXIT_CODE_SSWRITE 10 +#define CHILD_EXIT_CODE_SIG_TEST 11 + +#define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ +({ \ + register long _num __asm__ ("a7") =3D (num); \ + register long _arg1 __asm__ ("a0") =3D (long)(arg1); \ + register long _arg2 __asm__ ("a1") =3D (long)(arg2); \ + register long _arg3 __asm__ ("a2") =3D (long)(arg3); \ + register long _arg4 __asm__ ("a3") =3D (long)(arg4); \ + register long _arg5 __asm__ ("a4") =3D (long)(arg5); \ + \ + __asm__ volatile( \ + "ecall\n" \ + : "+r" \ + (_arg1) \ + : "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \ + "r"(_num) \ + : "memory", "cc" \ + ); \ + _arg1; \ +}) + +#define my_syscall3(num, arg1, arg2, arg3) \ +({ \ + register long _num __asm__ ("a7") =3D (num); \ + register long _arg1 __asm__ ("a0") =3D (long)(arg1); \ + register long _arg2 __asm__ ("a1") =3D (long)(arg2); \ + register long _arg3 __asm__ ("a2") =3D (long)(arg3); \ + \ + __asm__ volatile( \ + "ecall\n" \ + : "+r" (_arg1) \ + : "r"(_arg2), "r"(_arg3), \ + "r"(_num) \ + : "memory", "cc" \ + ); \ + _arg1; \ +}) + +#ifndef __NR_prctl +#define __NR_prctl 167 +#endif + +#ifndef __NR_map_shadow_stack +#define __NR_map_shadow_stack 453 +#endif + +#define CSR_SSP 0x011 + +#ifdef __ASSEMBLY__ +#define __ASM_STR(x) x +#else +#define __ASM_STR(x) #x +#endif + +#define csr_read(csr) \ +({ \ + register unsigned long __v; \ + __asm__ __volatile__ ("csrr %0, " __ASM_STR(csr) \ + : "=3Dr" (__v) : \ + : "memory"); \ + __v; \ +}) + +#define csr_write(csr, val) \ +({ \ + unsigned long __v =3D (unsigned long)(val); \ + __asm__ __volatile__ ("csrw " __ASM_STR(csr) ", %0" \ + : : "rK" (__v) \ + : "memory"); \ +}) + +#endif diff --git a/tools/testing/selftests/riscv/cfi/riscv_cfi_test.c b/tools/tes= ting/selftests/riscv/cfi/riscv_cfi_test.c new file mode 100644 index 000000000000..cf33aa25cc73 --- /dev/null +++ b/tools/testing/selftests/riscv/cfi/riscv_cfi_test.c @@ -0,0 +1,78 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include "../../kselftest.h" +#include +#include +#include +#include "cfi_rv_test.h" + +/* do not optimize cfi related test functions */ +#pragma GCC push_options +#pragma GCC optimize("O0") + +void sigsegv_handler(int signum, siginfo_t *si, void *uc) +{ + struct ucontext *ctx =3D (struct ucontext *)uc; + + if (si->si_code =3D=3D SEGV_CPERR) { + ksft_print_msg("Control flow violation happened somewhere\n"); + ksft_print_msg("PC where violation happened %lx\n", ctx->uc_mcontext.gre= gs[0]); + exit(-1); + } + + /* all other cases are expected to be of shadow stack write case */ + exit(CHILD_EXIT_CODE_SSWRITE); +} + +bool register_signal_handler(void) +{ + struct sigaction sa =3D {}; + + sa.sa_sigaction =3D sigsegv_handler; + sa.sa_flags =3D SA_SIGINFO; + if (sigaction(SIGSEGV, &sa, NULL)) { + ksft_print_msg("Registering signal handler for landing pad violation fai= led\n"); + return false; + } + + return true; +} + +int main(int argc, char *argv[]) +{ + int ret =3D 0; + unsigned long lpad_status =3D 0, ss_status =3D 0; + + ksft_print_header(); + + ksft_print_msg("Starting risc-v tests\n"); + + /* + * Landing pad test. Not a lot of kernel changes to support landing + * pad for user mode except lighting up a bit in senvcfg via a prctl + * Enable landing pad through out the execution of test binary + */ + ret =3D my_syscall5(__NR_prctl, PR_GET_INDIR_BR_LP_STATUS, &lpad_status, = 0, 0, 0); + if (ret) + ksft_exit_fail_msg("Get landing pad status failed with %d\n", ret); + + if (!(lpad_status & PR_INDIR_BR_LP_ENABLE)) + ksft_exit_fail_msg("Landing pad is not enabled, should be enabled via gl= ibc\n"); + + ret =3D my_syscall5(__NR_prctl, PR_GET_SHADOW_STACK_STATUS, &ss_status, 0= , 0, 0); + if (ret) + ksft_exit_fail_msg("Get shadow stack failed with %d\n", ret); + + if (!(ss_status & PR_SHADOW_STACK_ENABLE)) + ksft_exit_fail_msg("Shadow stack is not enabled, should be enabled via g= libc\n"); + + if (!register_signal_handler()) + ksft_exit_fail_msg("Registering signal handler for SIGSEGV failed\n"); + + ksft_print_msg("Landing pad and shadow stack are enabled for binary\n"); + execute_shadow_stack_tests(); + + return 0; +} + +#pragma GCC pop_options diff --git a/tools/testing/selftests/riscv/cfi/shadowstack.c b/tools/testin= g/selftests/riscv/cfi/shadowstack.c new file mode 100644 index 000000000000..a0ef066e98ab --- /dev/null +++ b/tools/testing/selftests/riscv/cfi/shadowstack.c @@ -0,0 +1,375 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include "../../kselftest.h" +#include +#include +#include +#include +#include +#include "shadowstack.h" +#include "cfi_rv_test.h" + +/* do not optimize shadow stack related test functions */ +#pragma GCC push_options +#pragma GCC optimize("O0") + +void zar(void) +{ + unsigned long ssp =3D 0; + + ssp =3D csr_read(CSR_SSP); + ksft_print_msg("Spewing out shadow stack ptr: %lx\n" + " This is to ensure shadow stack is indeed enabled and working\n", + ssp); +} + +void bar(void) +{ + zar(); +} + +void foo(void) +{ + bar(); +} + +void zar_child(void) +{ + unsigned long ssp =3D 0; + + ssp =3D csr_read(CSR_SSP); + ksft_print_msg("Spewing out shadow stack ptr: %lx\n" + " This is to ensure shadow stack is indeed enabled and working\n", + ssp); +} + +void bar_child(void) +{ + zar_child(); +} + +void foo_child(void) +{ + bar_child(); +} + +typedef void (call_func_ptr)(void); +/* + * call couple of functions to test push pop. + */ +int shadow_stack_call_tests(call_func_ptr fn_ptr, bool parent) +{ + ksft_print_msg("dummy calls for sspush and sspopchk in context of %s\n", + parent ? "parent" : "child"); + + (fn_ptr)(); + + return 0; +} + +/* forks a thread, and ensure shadow stacks fork out */ +bool shadow_stack_fork_test(unsigned long test_num, void *ctx) +{ + int pid =3D 0, child_status =3D 0, parent_pid =3D 0, ret =3D 0; + unsigned long ss_status =3D 0; + + ksft_print_msg("Exercising shadow stack fork test\n"); + + ret =3D my_syscall5(__NR_prctl, PR_GET_SHADOW_STACK_STATUS, &ss_status, 0= , 0, 0); + if (ret) { + ksft_exit_skip("Shadow stack get status prctl failed with errorcode %d\n= ", ret); + return false; + } + + if (!(ss_status & PR_SHADOW_STACK_ENABLE)) + ksft_exit_skip("Shadow stack is not enabled, should be enabled via glibc= \n"); + + parent_pid =3D getpid(); + pid =3D fork(); + + if (pid) { + ksft_print_msg("Parent pid %d and child pid %d\n", parent_pid, pid); + shadow_stack_call_tests(&foo, true); + } else { + shadow_stack_call_tests(&foo_child, false); + } + + if (pid) { + ksft_print_msg("Waiting on child to finish\n"); + wait(&child_status); + } else { + /* exit child gracefully */ + exit(0); + } + + if (pid && WIFSIGNALED(child_status)) { + ksft_print_msg("Child faulted, fork test failed\n"); + return false; + } + + return true; +} + +/* exercise `map_shadow_stack`, pivot to it and call some functions to ens= ure it works */ +#define SHADOW_STACK_ALLOC_SIZE 4096 +bool shadow_stack_map_test(unsigned long test_num, void *ctx) +{ + unsigned long shdw_addr; + int ret =3D 0; + + ksft_print_msg("Exercising shadow stack map test\n"); + + shdw_addr =3D my_syscall3(__NR_map_shadow_stack, NULL, SHADOW_STACK_ALLOC= _SIZE, 0); + + if (((long)shdw_addr) <=3D 0) { + ksft_print_msg("map_shadow_stack failed with error code %d\n", + (int)shdw_addr); + return false; + } + + ret =3D munmap((void *)shdw_addr, SHADOW_STACK_ALLOC_SIZE); + + if (ret) { + ksft_print_msg("munmap failed with error code %d\n", ret); + return false; + } + + return true; +} + +/* + * shadow stack protection tests. map a shadow stack and + * validate all memory protections work on it + */ +bool shadow_stack_protection_test(unsigned long test_num, void *ctx) +{ + unsigned long shdw_addr; + unsigned long *write_addr =3D NULL; + int ret =3D 0, pid =3D 0, child_status =3D 0; + + ksft_print_msg("Exercising shadow stack protection test (WPT)\n"); + + shdw_addr =3D my_syscall3(__NR_map_shadow_stack, NULL, SHADOW_STACK_ALLOC= _SIZE, 0); + + if (((long)shdw_addr) <=3D 0) { + ksft_print_msg("map_shadow_stack failed with error code %d\n", + (int)shdw_addr); + return false; + } + + write_addr =3D (unsigned long *)shdw_addr; + pid =3D fork(); + + /* no child was created, return false */ + if (pid =3D=3D -1) + return false; + + /* + * try to perform a store from child on shadow stack memory + * it should result in SIGSEGV + */ + if (!pid) { + /* below write must lead to SIGSEGV */ + *write_addr =3D 0xdeadbeef; + } else { + wait(&child_status); + } + + /* test fail, if 0xdeadbeef present on shadow stack address */ + if (*write_addr =3D=3D 0xdeadbeef) { + ksft_print_msg("Shadow stack WPT failed\n"); + return false; + } + + /* if child reached here, then fail */ + if (!pid) { + ksft_print_msg("Shadow stack WPT failed: child reached unreachable state= \n"); + return false; + } + + /* if child exited via signal handler but not for write on ss */ + if (WIFEXITED(child_status) && + WEXITSTATUS(child_status) !=3D CHILD_EXIT_CODE_SSWRITE) { + ksft_print_msg("Shadow stack WPT failed: child wasn't signaled for write= \n"); + return false; + } + + ret =3D munmap(write_addr, SHADOW_STACK_ALLOC_SIZE); + if (ret) { + ksft_print_msg("Shadow stack WPT failed: munmap failed, error code %d\n", + ret); + return false; + } + + return true; +} + +#define SS_MAGIC_WRITE_VAL 0xbeefdead + +int gup_tests(int mem_fd, unsigned long *shdw_addr) +{ + unsigned long val =3D 0; + + lseek(mem_fd, (unsigned long)shdw_addr, SEEK_SET); + if (read(mem_fd, &val, sizeof(val)) < 0) { + ksft_print_msg("Reading shadow stack mem via gup failed\n"); + return 1; + } + + val =3D SS_MAGIC_WRITE_VAL; + lseek(mem_fd, (unsigned long)shdw_addr, SEEK_SET); + if (write(mem_fd, &val, sizeof(val)) < 0) { + ksft_print_msg("Writing shadow stack mem via gup failed\n"); + return 1; + } + + if (*shdw_addr !=3D SS_MAGIC_WRITE_VAL) { + ksft_print_msg("GUP write to shadow stack memory failed\n"); + return 1; + } + + return 0; +} + +bool shadow_stack_gup_tests(unsigned long test_num, void *ctx) +{ + unsigned long shdw_addr =3D 0; + unsigned long *write_addr =3D NULL; + int fd =3D 0; + bool ret =3D false; + + ksft_print_msg("Exercising shadow stack gup tests\n"); + shdw_addr =3D my_syscall3(__NR_map_shadow_stack, NULL, SHADOW_STACK_ALLOC= _SIZE, 0); + + if (((long)shdw_addr) <=3D 0) { + ksft_print_msg("map_shadow_stack failed with error code %d\n", (int)shdw= _addr); + return false; + } + + write_addr =3D (unsigned long *)shdw_addr; + + fd =3D open("/proc/self/mem", O_RDWR); + if (fd =3D=3D -1) + return false; + + if (gup_tests(fd, write_addr)) { + ksft_print_msg("gup tests failed\n"); + goto out; + } + + ret =3D true; +out: + if (shdw_addr && munmap(write_addr, SHADOW_STACK_ALLOC_SIZE)) { + ksft_print_msg("munmap failed with error code %d\n", ret); + ret =3D false; + } + + return ret; +} + +volatile bool break_loop; + +void sigusr1_handler(int signo) +{ + break_loop =3D true; +} + +bool sigusr1_signal_test(void) +{ + struct sigaction sa =3D {}; + + sa.sa_handler =3D sigusr1_handler; + sa.sa_flags =3D 0; + sigemptyset(&sa.sa_mask); + if (sigaction(SIGUSR1, &sa, NULL)) { + ksft_print_msg("Registering signal handler for SIGUSR1 failed\n"); + return false; + } + + return true; +} + +/* + * shadow stack signal test. shadow stack must be enabled. + * register a signal, fork another thread which is waiting + * on signal. Send a signal from parent to child, verify + * that signal was received by child. If not test fails + */ +bool shadow_stack_signal_test(unsigned long test_num, void *ctx) +{ + int pid =3D 0, child_status =3D 0, ret =3D 0; + unsigned long ss_status =3D 0; + + ksft_print_msg("Exercising shadow stack signal test\n"); + + ret =3D my_syscall5(__NR_prctl, PR_GET_SHADOW_STACK_STATUS, &ss_status, 0= , 0, 0); + if (ret) { + ksft_print_msg("Shadow stack get status prctl failed with errorcode %d\n= ", ret); + return false; + } + + if (!(ss_status & PR_SHADOW_STACK_ENABLE)) + ksft_print_msg("Shadow stack is not enabled, should be enabled via glibc= \n"); + + /* this should be caught by signal handler and do an exit */ + if (!sigusr1_signal_test()) { + ksft_print_msg("Registering sigusr1 handler failed\n"); + exit(-1); + } + + pid =3D fork(); + + if (pid =3D=3D -1) { + ksft_print_msg("Signal test: fork failed\n"); + goto out; + } + + if (pid =3D=3D 0) { + while (!break_loop) + sleep(1); + + exit(11); + /* child shouldn't go beyond here */ + } + + /* send SIGUSR1 to child */ + kill(pid, SIGUSR1); + wait(&child_status); + +out: + + return (WIFEXITED(child_status) && + WEXITSTATUS(child_status) =3D=3D 11); +} + +int execute_shadow_stack_tests(void) +{ + int ret =3D 0; + unsigned long test_count =3D 0; + unsigned long shstk_status =3D 0; + bool test_pass =3D false; + + ksft_print_msg("Executing RISC-V shadow stack self tests\n"); + ksft_set_plan(RISCV_SHADOW_STACK_TESTS); + + ret =3D my_syscall5(__NR_prctl, PR_GET_SHADOW_STACK_STATUS, &shstk_status= , 0, 0, 0); + + if (ret !=3D 0) + ksft_exit_fail_msg("Get shadow stack status failed with %d\n", ret); + + /* + * If we are here that means get shadow stack status succeeded and + * thus shadow stack support is baked in the kernel. + */ + while (test_count < ARRAY_SIZE(shstk_tests)) { + test_pass =3D (*shstk_tests[test_count].t_func)(test_count, NULL); + ksft_test_result(test_pass, shstk_tests[test_count].name); + test_count++; + } + + ksft_finished(); + + return 0; +} + +#pragma GCC pop_options diff --git a/tools/testing/selftests/riscv/cfi/shadowstack.h b/tools/testin= g/selftests/riscv/cfi/shadowstack.h new file mode 100644 index 000000000000..b43e74136a26 --- /dev/null +++ b/tools/testing/selftests/riscv/cfi/shadowstack.h @@ -0,0 +1,37 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#ifndef SELFTEST_SHADOWSTACK_TEST_H +#define SELFTEST_SHADOWSTACK_TEST_H +#include +#include + +/* + * a cfi test returns true for success or false for fail + * takes a number for test number to index into array and void pointer. + */ +typedef bool (*shstk_test_func)(unsigned long test_num, void *); + +struct shadow_stack_tests { + char *name; + shstk_test_func t_func; +}; + +bool shadow_stack_fork_test(unsigned long test_num, void *ctx); +bool shadow_stack_map_test(unsigned long test_num, void *ctx); +bool shadow_stack_protection_test(unsigned long test_num, void *ctx); +bool shadow_stack_gup_tests(unsigned long test_num, void *ctx); +bool shadow_stack_signal_test(unsigned long test_num, void *ctx); + +static struct shadow_stack_tests shstk_tests[] =3D { + { "shstk fork test\n", shadow_stack_fork_test }, + { "map shadow stack syscall\n", shadow_stack_map_test }, + { "shadow stack gup tests\n", shadow_stack_gup_tests }, + { "shadow stack signal tests\n", shadow_stack_signal_test}, + { "memory protections of shadow stack memory\n", shadow_stack_protection_= test } +}; + +#define RISCV_SHADOW_STACK_TESTS ARRAY_SIZE(shstk_tests) + +int execute_shadow_stack_tests(void); + +#endif --=20 2.34.1