From patchwork Mon Feb 10 20:26:34 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13969340 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 2CD13C0219E for ; Mon, 10 Feb 2025 20:26:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=Q9qkzKsYDwROP0gsq8WnWm/7dIGk720AEL3lnwUPsDE=; b=DXHuOEzOEycyHH RhWmnE1PbTk4knfDpzkPu4V6LMUAndePmdUMtonwNqjfbaA918iSRTBJtenQ0/vQytrFfJJ2E4dDZ tAnKsZiQJDeS7cmISMTrilkDGhDhqh7BxhC51kpO7iYRJtC18fdmWHeh+4vkAKFmlEwlRvVnHPalZ 2UN9UInsS1hUyXzZxUhLQZNgNSSQIRWTlKJ0PBXD+5gLWAd9ytNfhLA3AhyVAJh2nY15w3sx5BZY/ Dr/+ZkSQhGfKW3jhnDET1Zyzi63oChA4qR8MIoIwKKoV7ZURC0dw7dJ394oYTOXEbVoHyTs6l9Iwm 5NWGAjnI+38Zmmn9S6JQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1thaMi-00000001JXj-1gL4; Mon, 10 Feb 2025 20:26:48 +0000 Received: from mail-pl1-x633.google.com ([2607:f8b0:4864:20::633]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1thaMe-00000001JVR-3Hly for linux-riscv@lists.infradead.org; Mon, 10 Feb 2025 20:26:46 +0000 Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-21f7f03d856so35322235ad.1 for ; Mon, 10 Feb 2025 12:26:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1739219204; x=1739824004; darn=lists.infradead.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=SMcP7UjD2QYwWMxlGy7fAHfnV1/oJ7J4NI1X5vP3lD1ck6RqE87WoIjjCpD8rFQ5dY 0uTV/l3OxIJzubGUv68auR0nwB5fq899ltv4ZvIjc+c75FwaMIdhSNEr565YDWD3UQPD fq0o2bj34qMKnrWP4o1xDNqf5r/NwvAQoVxeV9Z2jkdLEB0oN5cRP7T5zGshe2zknkTk YA16JVhuT8olPJrU9p84NXuV0L7op4kiIQR4cByqgxpkfRPCVeF55mn3AOjUYn0IMYPx IW3JhDSnGNMozKusG5I8aLA/jBxKojGL/EM5OF08yTTWfsj8x7lz0rtnTdvlXZ+1iLk0 VQHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739219204; x=1739824004; 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=EHLZ7tE+EtTz03EX29tiCjrxKn1x9qni6jY6iKsakg2/75Gqs0JD9TBrobVsxAAkz4 J+RrLq0ENyq1xMzf9dpC9egHgno173vm/AzBEleISYA3WN4eqeKSN59UlPa6geaBsgot Gwrm8uIrPLCSVDtNV5vDvggOEALR3hgJrCSQJUrOyzbIMB5+Z1a+rPf4S95YyQKAhtCu jTIfyOLkDagGO7bHypU3V6WsWOi4F9Ey5a06hYCuj6x8PQS/plnRT6bXWaXlFSHqeeJw GYO/G+MSlYwdmFYiXDdxiIKiGC2oosfSIU5AIdVTQsqfwRsElVujdedoeg+251SnBgNU mYCQ== X-Forwarded-Encrypted: i=1; AJvYcCVbWGHIXdMsdRoRIFYz+VIZjzQ16ms3MiLbTBwaLsyu/Bjzz/DE38+b3UpAG6cs36PzDWP31jrIUZIicA==@lists.infradead.org X-Gm-Message-State: AOJu0Ywj4APMcFVzplvQfu+Ag+3xoLktShW1jUQjTjiCdeRYBcZts9Q5 v4o83QL71bq4CcualqVrO6N7aR4hOnRrtZubn6EYV3Z8b7xiC6/W9lWNwO/zR1M= X-Gm-Gg: ASbGnctVwgGYuv0MR9ricFs6/KU4s+KCsSliafdOkBgfIxqANkTUhtiPRnwXF4vTKGL P2XPSudBqPde9GymDhvbmjIsykNgLux5lZLrFf79n7yyL5yH5/V6UEh3GluUjLTwpvl64uYaJZa /2334x38vzoMpZCDjlH38Vh1mYRbJ65eKEjObYAb/VfBEZT/hoz+XnfXAFHWIT41Engg16E5cxL Iz2BrNuyYVQ//DYoqhj3KOVU/xHthX9uGuEndm94efP1dvXAQEVxoJsR8Z8uXO50LDhdwfDZovX 7TvF4rhnHMVp0F03/rSKRgeGpg== X-Google-Smtp-Source: AGHT+IFznxG4IUsH1vtGUm0FvXVBvTHvmf0eQhdxQZjLsHpvXLSfCmOiU5USFlGIslppLz4th5AW+A== X-Received: by 2002:a17:902:f114:b0:21f:75c:f7d3 with SMTP id d9443c01a7336-21f4e798104mr179051845ad.38.1739219204274; Mon, 10 Feb 2025 12:26:44 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21f3687c187sm83711555ad.168.2025.02.10.12.26.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Feb 2025 12:26:43 -0800 (PST) From: Deepak Gupta Date: Mon, 10 Feb 2025 12:26:34 -0800 Subject: [PATCH v10 01/27] mm: VM_SHADOW_STACK definition for riscv MIME-Version: 1.0 Message-Id: <20250210-v5_user_cfi_series-v10-1-163dcfa31c60@rivosinc.com> References: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@rivosinc.com> In-Reply-To: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250210_122644_826601_46DA7C78 X-CRM114-Status: GOOD ( 10.03 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org VM_HIGH_ARCH_5 is used for riscv Signed-off-by: Deepak Gupta --- 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 +#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 From patchwork Mon Feb 10 20:26:35 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13969341 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 52A90C021A4 for ; Mon, 10 Feb 2025 20:26:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=jU0T5QkEr3232RN3yDhzbxX5XeNOIdmcHfVs2jzrTyQ=; b=U8YH5ukbUnaXjg UiG6xR1OuCr8XDquZsKgVL6TvzHUjxw1NtMYitlNNiblg1+xYelqJ7reEF91ymyfOgZumSy7ExE+a aWGm2IWIm4YS0mFW5NsShSV7JJe5P13E4cnShmk15e5fEfw57akDm+FN2yK4kCp0bluKMYLPRq8Le UsXbnTp/KT1U4DjjxrbaGjg7GSepelumNnRb04FN1t174LPDwHsbYYnS9OcCMXgxqCoZ7BQh46YUA 5dfp17+vXI0CxK4eO92GhsgkMdCuUCJ6oaW+z8oNhTx9s3ao1eM97iTnbHNt6/dq6YauDX8f4Fc40 MkQmV37TjICL4ExW5GUw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1thaMl-00000001JaM-08ZX; Mon, 10 Feb 2025 20:26:51 +0000 Received: from mail-pl1-x636.google.com ([2607:f8b0:4864:20::636]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1thaMh-00000001JWt-2UYD for linux-riscv@lists.infradead.org; Mon, 10 Feb 2025 20:26:49 +0000 Received: by mail-pl1-x636.google.com with SMTP id d9443c01a7336-21f0444b478so68618965ad.0 for ; Mon, 10 Feb 2025 12:26:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1739219207; x=1739824007; darn=lists.infradead.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=rXRESAb7rd8xxsqthYAtw774SPUzWyy1TU+e5+aQ+fLYULUUaDIQtR5EIxWNM0EjfQ 6ldRFxdyd6ltOf99WHRHXAJb5yr01gpU2CsNZigksxahBiOLWW2FQHCICYYJSd8zy8fd wMcORbaFyYQTnxSDUWaBUKPPpSOzttM4mN8skPKgkcs+ghJaf+/At/1YPv4U4DcR+Dkv 3Gg1gcjffYH+mS516KAUZ67CYZv0yRYYSQ6H0FKfBakO0af1bS3uuI9HNdllDjtPknlL 1PYwswV4yG71BGDpoFrKNdafTODcHO+GVSztCf7+Y6KksqRI7apY8o0PsOiDQAsd5shP wHcg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739219207; x=1739824007; 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=ApdWqk5R67xfwMCrMLyRnGzYN+y8pAaFayTw5R4ehfFBSgIQOOBa42o6i29iOvKdc7 +zvEWAdtrVrXYjikk9YmIhwg0FFNZH2cZRzM1rzZg0YD3QoaclxpU8WQn0K3hLeqVo9K HaLIjZqg80nf5fxAuy4UzzmDAc47TV/ZTnZXvptaxkJszjadGdtv5k4yxl5Q1WTkIZEb Dq3pPwkpCBuT/xYvANZH3FHsE3OE81q7Hhny9+UbgUUUJnGzxrWJvT3NsRI941fAytd2 e/vj8C2dY7fXlLSnw5ptLqMIoaeuoZuTi1L4gfAdfraWQe3fLfPEBJ+V8gu+3dPgX1RC qXyQ== X-Forwarded-Encrypted: i=1; AJvYcCWw2N/YIop8+zQRpBezrO3HVVhyR4HNad4pYiVVyeqVn6mzkXnfLozUp6b1GTUcsg5ysBj25CmYwiV3Ng==@lists.infradead.org X-Gm-Message-State: AOJu0Yw8aTbJaKIJUK0XOjfEccts7q/IbY12Js9XgHigoH6o299X41Pr WEddNFUN+LuwOUMSGU3cJ1qVsnFTFV1Faxv+fEG9J3FnrO3ChLGcMwXj0Ogn1gg= X-Gm-Gg: ASbGncsEqrfEQ+hnGciRvaTg10iupiUBqGgY8Treo5lJ/lQR85G9b//jlfuaAZRbTIk 2X6y4IR2/G9796U4YALxb+b8l4MKcxS9g++E2oHZ0peO/OXG0huXqMlubKABS6tLmRbsvCZY+Uz Iu+vTCJtpRsUD87wHeWo6pFnJfqEWgAsRsCAOwz/TaPSlAaCiD+nVJcBDfeRyBo2HQqglyS6fcH /cx04i2yRpAfpi/64ryr4ksumYbAk3Rp7gzqgQNXHQUt53dehbSwF1KQ2UYlhBqeuPuGAnQkFGE DASNa3MluqQu+y6U1hSe9V2TMA== X-Google-Smtp-Source: AGHT+IGXqRpNguUrMSnJHDFrz3qQhRvvMy95jaGz8XHpdEECN7AcOh1dWxqC1mHx3x1SCoyBHua2rw== X-Received: by 2002:a17:902:db0b:b0:21f:169d:9f39 with SMTP id d9443c01a7336-21f4e779cc3mr280440675ad.46.1739219206908; Mon, 10 Feb 2025 12:26:46 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21f3687c187sm83711555ad.168.2025.02.10.12.26.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Feb 2025 12:26:46 -0800 (PST) From: Deepak Gupta Date: Mon, 10 Feb 2025 12:26:35 -0800 Subject: [PATCH v10 02/27] dt-bindings: riscv: zicfilp and zicfiss in dt-bindings (extensions.yaml) MIME-Version: 1.0 Message-Id: <20250210-v5_user_cfi_series-v10-2-163dcfa31c60@rivosinc.com> References: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@rivosinc.com> In-Reply-To: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250210_122647_646027_B69CC5D7 X-CRM114-Status: UNSURE ( 9.58 ) X-CRM114-Notice: Please train this message. X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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/Documentation/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 ratified in commit 3dd606f ("Create cmobase-v1.0.pdf") of riscv-CMOs. + - 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 From patchwork Mon Feb 10 20:26:36 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13969342 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 37902C021A1 for ; Mon, 10 Feb 2025 20:26:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=pTSA1S2Hfv043WtYxe5yIlGs5CoYWlSZBQAAb1H98Cs=; b=MD+4r0s58YCqq0 34PPJf5YVRNHNiXniGxsIYqMLGtGGto46MJRPDZKFu7ArhfvAVEqi/mUc7p/wWrChIIGpP6jjRscr BuhDGzP1BljQLErC1DlE+1frjrfZcxZ1XgOlGPPiPaRucCNJs9EF6E9q5ceqcJ1fFQ8m/E9Kjs5lR mQo0mo/kvzeagITd+K49OXZps6vdokSGVowPcD7d3hL1uxL2UM3uot5pD1ExtldZr4JiB+90yMfiO 0JIWs1ia3fbv4VuLYByA84nTVAqjyuFm+GEVFhAV6h1cW1XVu4LOzTcvzOA/BScnfxogcoRAHUJIc 9dbq9Jwm4Yq5Z9rcsvfA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1thaMm-00000001JbZ-2jny; Mon, 10 Feb 2025 20:26:52 +0000 Received: from mail-pl1-x632.google.com ([2607:f8b0:4864:20::632]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1thaMk-00000001JZN-11AZ for linux-riscv@lists.infradead.org; Mon, 10 Feb 2025 20:26:51 +0000 Received: by mail-pl1-x632.google.com with SMTP id d9443c01a7336-21f3e2b4eceso90250675ad.2 for ; Mon, 10 Feb 2025 12:26:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1739219209; x=1739824009; darn=lists.infradead.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=e4Q5cQ45XPtn6y+RD8Sdq5HQMM0KMIYwSdtAswCfSLwmNUw8GoCsWaxNzb8NEwGwXz E0ZTQ19d+pNagn/jPur26Pmho6Q6Pip1E3XFEwCZ0opFSiJ7gDljo4kSJgz4hxSQITzK Ho4xukNMRoZERHAWHOG/qs/AskR6Pyg5BwgnUiCGyihrthrPCgYxUaaALJ3j+3/pf5/e YQ+44IBFTpyhE71pok66lL8fcHHk60nTKyOD5nWEDg28ZJExK5Y0pw2NzgZm9i/yjhGZ a4G8s4vSPXh61hyPn8M9M8r7IVuWECrPW2Rvx6YR01m0GPDEgGWEQcsm5ag9R+HUQnFX iT8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739219209; x=1739824009; 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=QYi48kjQyJr689+jSwcGWufzfExGcFnN3dPBDYJOy8Cv28zzxEBe82+utDlrfzMPEw 4svYZk8DfSl8Mf6SAAdJ/NrNX7H+7vQbDf57bbuIjsn3+K5ZLaZByfqtRQybf9ErCWf7 y/Nsv1mWfSPmuTmtlnei1rDnTnUxonguHkGE+8r06St+eS/ri5HvDl1py7+9flGBzQK3 +KRKD/XMbbipmX0OjqVV7yeE08KR8ubTAyqQDX2PDfxmYb8cfwThK5cFIqQImptdXAN+ Z4loHlC2M5b7LJuPF2ToIL2Go9HxGbidJId2bCI1C1v0X6BKFbL3xttDf4c/LZrIVSN6 qJgA== X-Forwarded-Encrypted: i=1; AJvYcCVlJdz2kGj0L4qkAXEVW8KlmdKJq/TT9WcfJgjXpyG9+jS6X44PxQ4DdqbW6purlfoJ54UK4T5bdBoMsQ==@lists.infradead.org X-Gm-Message-State: AOJu0YywPeNVfxKhy8EtnCZepobK8D9JVqtm6mTsboGrkgaY6Xbv40X+ yYSTgJp4RBMWwYdyKwMfIQGRa8Jw/+iJb5abYilbLRyik54WO2J/42+4HX5ur4o= X-Gm-Gg: ASbGncs712gtso85Oeum29czXMpe/zrrK9ucMTkT3K6WLqwrzF2AvTqQbSlXn9VN6dI YKiGTXHef/JfbqeqpCKUDSttq39leERFZ9QV9g2ZBXbHWF7HmoArzaH0NMzrVCLty5pRqxxjPoA 34o4tNs4cT4GPAKD0+MuNRxNjF690EAFvl6fLB62L4h2BU4iHY+vkL2tHmlRGk8VCJwLlhHoUcx ZDo7K+pHDw44GFrtLExa72ke15+jMxx5neRPxYY+YGLj6INaG6cvb7NSz0YAqMmzft9Moo5Fkw4 PNDe7QM0vNACG9aGKKQTYhI8Iw== X-Google-Smtp-Source: AGHT+IEhZ3TYH/tcMpvG04g1Gw5nBcJh1Es1LXU8QZEq1VPJSIGDwObL506BPg7EHhAIxgdRzRFlCQ== X-Received: by 2002:a17:902:c40e:b0:21f:1549:a55f with SMTP id d9443c01a7336-21f4e6c0f49mr269385645ad.19.1739219209495; Mon, 10 Feb 2025 12:26:49 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21f3687c187sm83711555ad.168.2025.02.10.12.26.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Feb 2025 12:26:49 -0800 (PST) From: Deepak Gupta Date: Mon, 10 Feb 2025 12:26:36 -0800 Subject: [PATCH v10 03/27] riscv: zicfiss / zicfilp enumeration MIME-Version: 1.0 Message-Id: <20250210-v5_user_cfi_series-v10-3-163dcfa31c60@rivosinc.com> References: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@rivosinc.com> In-Reply-To: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250210_122650_285208_EFA8C1F6 X-CRM114-Status: GOOD ( 16.29 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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 --- 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/cpufeature.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 @@ -137,4 +138,16 @@ static __always_inline bool riscv_cpu_has_extension_unlikely(int cpu, const unsi return __riscv_isa_extension_available(hart_isa[cpu].isa, ext); } +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_ZICFISS)); +} + +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_ZICFILP)); +} + #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 #define RISCV_ISA_EXT_XLINUXENVCFG 127 diff --git a/arch/riscv/include/asm/processor.h b/arch/riscv/include/asm/processor.h index 5f56eb9d114a..e3aba3336e63 100644 --- a/arch/riscv/include/asm/processor.h +++ b/arch/riscv/include/asm/processor.h @@ -13,6 +13,7 @@ #include #include +#include #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; } +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[] = { RISCV_ISA_EXT_ZBKB, RISCV_ISA_EXT_ZBKC, @@ -333,6 +342,10 @@ const struct riscv_isa_ext_data riscv_isa_ext[] = { __RISCV_ISA_EXT_SUPERSET_VALIDATE(zicboz, RISCV_ISA_EXT_ZICBOZ, riscv_xlinuxenvcfg_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_xlinuxenvcfg_exts, + riscv_cfi_validate), + __RISCV_ISA_EXT_SUPERSET_VALIDATE(zicfiss, RISCV_ISA_EXT_ZICFISS, riscv_xlinuxenvcfg_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), From patchwork Mon Feb 10 20:26:37 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13969343 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id A903BC021A5 for ; Mon, 10 Feb 2025 20:26:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=hRKwbmU5qBvoQWc+V/8xtLYTXQbaV7ZdQ8JJQmB+HDc=; b=dV1ux4fSLusX4W RwsT2MvWpI0Y00mOhW7cxh8RK136ByyOaEy1bf2Wii3ybBlKVE6+5ASpi0H31Qgo3rYWJVo+LolR+ 91AuRR4dMmdcO23qaLIn0MAGxfuVPH2qsX6Z2PuG9NHBhM68P9btmRfrMAGoAhJ7jcn+nwF4yFYnS dzuqh5sweGKojgBPjGHUgHFfEIiW3WV4mZZlXajGdM6G+Lm5yY1gAzJHKL/ZyhEWgNrPjMwmK6AJm R8WrAFdL6U9jSKG6hUfhgHoYMwjDRtqG+xcHlJzcIVh3oe9QsnWb9FoEoQyjd3J1a2Qryoke4jueR 6bYIL5obQL0dUxssYvBA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1thaMp-00000001JfG-1uXO; Mon, 10 Feb 2025 20:26:55 +0000 Received: from mail-pl1-x62e.google.com ([2607:f8b0:4864:20::62e]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1thaMm-00000001JbP-3djJ for linux-riscv@lists.infradead.org; Mon, 10 Feb 2025 20:26:54 +0000 Received: by mail-pl1-x62e.google.com with SMTP id d9443c01a7336-21f55fbb72bso56897055ad.2 for ; Mon, 10 Feb 2025 12:26:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1739219212; x=1739824012; darn=lists.infradead.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=CRSmIkJR9NFqcOpRdPBaKZk3IRGIx7jKSHVG0GBDos8Mgt0j2iEngP5DJzyDYOshOE 1XGa8BrM/x/MzRFEqLbozFsIW+Lrw7UgIdQ6yzDcLI3L9YWxOvdyTgPntglKK4j9HzfF CA2oHE4yQoZe3aSvvgYL59c2aq75yMOItVf83MNSxyJFhgYABT61xEMu+5vYqpsKeCCp ZKC9biQfEZBVnbrRvhA2Xk8wygsiiFAIWq1UwrRZA4kU4CqQxD7/jYKBtBd4a/ORqoeA 9hil92ylhsqR/l/0/d9Gsefrx+6iWk7eP+uJkxum8YmRqczDHCvToE+VIlEOQEqhhrR0 SPNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739219212; x=1739824012; 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=DocP+fDFTzWClJ8msJsACNOcRLZyR9wnRH5zqT0lB3m+IG9Cectzu46Rno2HygOVn5 MacXNLmL7b7AnkftyhFQkMOQRcb637axqwbdqjbXVlC36ZAr+Xt0l96C67hQVHLH1Dxp tYToLoljnW2oCsdkZKppolxUfXjA+b6iEPHTKbePrkoRYOsODBAqNAu7dxHtWlitW7A4 b4u3r6k7ZF8uD+rKc5Pe5oXTQBzG33Bv8gZaVQa7EGRjXh/L32POQgkesqh4gH6xEQwm 7qeOdr41aJuQMu5h1X+8K+tI3upXLq/mn4uL3ttny7KofvUruoWLi7EhYInIgV4R/EIs ck4A== X-Forwarded-Encrypted: i=1; AJvYcCV6uTMiVMpsblaxMHEv2d2QSgi+7uOpTzpbwWkswoJpHDpTn+AeZfL573LJd5uGMF0TTgrGSFJxbfIQXQ==@lists.infradead.org X-Gm-Message-State: AOJu0YxUJZyikkDMvjdDrDhEQE1oBvE7tQWd0m5rbqPYjcJcuCB0KrKs oSD2KT44YsNVPS3SSXu/ew5NlqgtxJYoKKk5gPmkyZQl9M11UBFsoFc8hSg34Oc= X-Gm-Gg: ASbGncu8f4o9XrP3Zkcjdl/kSS/SG2OrYHwHuexEHElHFxrKFf6TFElzAJRi8Vc3ncb c786fG1to9qER7N2hUNeQGFK1KBgJjIGDnPcnID9jWxVQD0SMOuSjYB5MKEPil+ZTarYu2Fu6YC 8dJwXTxsDSlhvZz3lcA9K8zR8XY1OaQbO1rBqRWpzZZC5azoadP5oqZC35epSS7PZwFySA5lxLF MueA0zDo122rqT3L6VdC681+paJjZhfs9NqyUQm2/0OJ84GzImppf+lopGlDbNgGIC5a8VC6hQT wiwT0xZZmYVCXd3K9WC6brsRVw== X-Google-Smtp-Source: AGHT+IGtT2BfpGC+pxbWvm2Qtn55Phvbe3Ks/NAXxmbZaEXtXhWUE3SuxobC1K+zZICcAG0d87INng== X-Received: by 2002:a17:903:32cb:b0:21f:dd1:8359 with SMTP id d9443c01a7336-21f4e6d2642mr247925115ad.29.1739219211966; Mon, 10 Feb 2025 12:26:51 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21f3687c187sm83711555ad.168.2025.02.10.12.26.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Feb 2025 12:26:51 -0800 (PST) From: Deepak Gupta Date: Mon, 10 Feb 2025 12:26:37 -0800 Subject: [PATCH v10 04/27] riscv: zicfiss / zicfilp extension csr and bit definitions MIME-Version: 1.0 Message-Id: <20250210-v5_user_cfi_series-v10-4-163dcfa31c60@rivosinc.com> References: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@rivosinc.com> In-Reply-To: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250210_122652_937294_C50CA6A0 X-CRM114-Status: GOOD ( 10.53 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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 */ +/* 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 /* mseccfg bits */ #define MSECCFG_PMM ENVCFG_PMM From patchwork Mon Feb 10 20:26:38 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13969344 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 87DA8C021A3 for ; Mon, 10 Feb 2025 20:27:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=jPj283BtWr1YV5XtqtJNdszkwiQNQWOiuO8/7l5CUdY=; b=Nr+YyLGnqmJNAo 2ZFdwXr6KcM7WQi1k9BpsvaRovwsaUXfxvRrf7e9/VXEXbWvViQLMWSHBlNxkQVZbusfwHTNKTLjj BV7be6NE0XCU4jBb+sPTeWA5zsMg9ML3r2CFcvP+eo4rtcDTG2iSsHepPfS8w6gBpPHf34eSisTep 6MbQHWwHP2bmioAj5QRx63aZHG5J+M3PcwPWTsIuBYIsXTwF1yma+bB0RZC4awgRQbKc6QAOZqz5r HGt6DAupHJP9W2xOYc9XwHfYbucO6w972QtvgR6R6MN/vBhjPxISm288P49Akeoi22wA2tOCUaks2 A0MKII7CTYzfKqiWPLUg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1thaMr-00000001Ji6-0yXh; Mon, 10 Feb 2025 20:26:57 +0000 Received: from mail-pl1-x62c.google.com ([2607:f8b0:4864:20::62c]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1thaMo-00000001Je5-41gR for linux-riscv@lists.infradead.org; Mon, 10 Feb 2025 20:26:56 +0000 Received: by mail-pl1-x62c.google.com with SMTP id d9443c01a7336-21f55fbb72bso56897745ad.2 for ; Mon, 10 Feb 2025 12:26:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1739219214; x=1739824014; darn=lists.infradead.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=JcAb7yJ6wkzU4OE+95EqjcoAyZ84/jQafYbZans6HYJV2DqC4HTR5veP+Nrq20tHJb 3tCMi7k9G+EFzdc4hUXp02vb/FGb+X41CFh3ZP5KvpZB5jC1Aqns65+InbUuWvhCoV6l bT0dEVX8HOBO00gvYKUv+Y905S5T+5vidRbCyKg/rwFwKAAqaK7i7X6kVxtELNttNlkR kalW3asnifoKiw7amRW9+4yZeH99jMCFpTDz++1SFUWVm1m7bkaX4QD2QBF/MZQjXyAC McusF9BfPeytcxARSOoieOsrFq8xLOg9u5rNs5HiHZ+zuCn4UQCJTbX5dKGUx4ySyFiE 69eQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739219214; x=1739824014; 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=j5RrW+10k8tKWF8KpnpkP0AeMdn/hToeyGiGNNRPcrqodC2Vq93uvaMO8hgus2nQvY sbsCbVRyCv9BAymFE/ZT/YwSsjqh1ygIoM6/NAh/ZnB4L9+7AfzT22LkemMGaXYCT7u/ 9aEGYzvXUhuhj+L3QAIR8iNQES0+0CONe5tKQTO10Q0u8sV5lY5j7Ej7v4DalfXZOfr+ N0plEYGdpsGvqVH0XHeN4cDkMrfCNMhk6YTcwCQXFV574EZ+Ya7F4rs+rNrm0n74bD0C pUOYSZ0KiHIc8wBqgQ3ZIH3ozaZRXb2Pim3wkkbEwBgQreTiE9nlWSf5hE7mS2neQEHE jLkg== X-Forwarded-Encrypted: i=1; AJvYcCVPNVotJePCxksG6cRU6Rv1bZJykjTKUO8RLk+tL/QgjBEXBeq8AFT41siY5LUH46smPzEAyPEgnb2vsA==@lists.infradead.org X-Gm-Message-State: AOJu0Yw/bToiXHn2yvaBAvTuzVhbwNFoH0vqia476HPDmQ51DQnrNk+Y XGrGgIDWW69sugdemB5yI1I1MBUJRDoJcy+iW1gWNtws0nsiTo+kVaUQdBNRL0g= X-Gm-Gg: ASbGncuiLBWuVAhSmEujKjdXGwM+1gV8w+IcOEgqWYtmrnekfvpViOXvsgWf5CjZKqs ZQDGYhkJtelB5h2ufXtNVFWsWUaGs9SlU21oNGSmyvMvGUoQUyv08DYU+VssoVp0ftvp++81XYS V/0zfim7XYxZVWcOHd8TDWDjVjyxFhYXp54kz3tCbprncdcWnpgq7pGagNULy7YV/mOOuSK18et vs5oGeFQ0Kwrjf3HBH0VT3+r6AwHXfJbZoAhE4vcV0HeP+V5BtspqSQ76Z7AWzB9Ff8g/6vxGYE 0UfJDeaws8PPl1DUZ1Cj4F7VFw== X-Google-Smtp-Source: AGHT+IEPSlWV/roU0SyRsD9mZQNHSbQmDLwPbxxPcGI6KoZ78tihK1b8Yp4dYVskjWY846JzmqLryA== X-Received: by 2002:a17:902:d543:b0:21f:f02:4176 with SMTP id d9443c01a7336-21f4e73bcc3mr228449315ad.33.1739219214484; Mon, 10 Feb 2025 12:26:54 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21f3687c187sm83711555ad.168.2025.02.10.12.26.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Feb 2025 12:26:54 -0800 (PST) From: Deepak Gupta Date: Mon, 10 Feb 2025 12:26:38 -0800 Subject: [PATCH v10 05/27] riscv: usercfi state for task and save/restore of CSR_SSP on trap entry/exit MIME-Version: 1.0 Message-Id: <20250210-v5_user_cfi_series-v10-5-163dcfa31c60@rivosinc.com> References: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@rivosinc.com> In-Reply-To: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250210_122655_022882_40CDF5C2 X-CRM114-Status: GOOD ( 21.95 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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 --- 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/processor.h index e3aba3336e63..d851bb5c6da0 100644 --- a/arch/riscv/include/asm/processor.h +++ b/arch/riscv/include/asm/processor.h @@ -14,6 +14,7 @@ #include #include +#include #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/usercfi.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-offsets.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 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_shdw_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) 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 From patchwork Mon Feb 10 20:26:39 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13969345 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C51FAC021A2 for ; Mon, 10 Feb 2025 20:27:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=0BK617Gy1WKKeQRHCfYdFiBRCdcQrpiAUE6oGxEPdqY=; b=2NKuAzl7AOv0V4 IUUMPPSp18j5F3HZk/U3o4e293+Kn2YdZpRPQlztF4W8KDU0UVpj7NzBlmNQceyqgPG9yzeE945vG ISVnsNqLiDFLgyCpbjypKWW+AEE9zjQXrQzPQE67lKEJrXbfR756FXWIUWnpb95SX2pl6KF+mMEDm I/JW9EkslWVtNr1GDwRO9TvqNp3g2igPPM60xQ68tbKsOhIQDLXd6GWUTGzI24Q6CE29MXCIprqdA LctSKnqzPBIqxiuVUICgAr8mTJ55/xOkqpo5gpLX7Fzidq/XDPt3jz0yhX1LWRdiI/rWVcPm0J5if 0XquMn/CTWfeAw0PoBIw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1thaMu-00000001JoX-2P88; Mon, 10 Feb 2025 20:27:00 +0000 Received: from mail-pl1-x629.google.com ([2607:f8b0:4864:20::629]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1thaMr-00000001JiV-2PYO for linux-riscv@lists.infradead.org; Mon, 10 Feb 2025 20:26:58 +0000 Received: by mail-pl1-x629.google.com with SMTP id d9443c01a7336-21f3c119fe6so107239045ad.0 for ; Mon, 10 Feb 2025 12:26:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1739219217; x=1739824017; darn=lists.infradead.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=NNhNzPia1s3EgpDp9iqAG91D0fuQQGbhREA/Wes7LiHnteJPwiIKsVBNblKUYs7fsH Rqi7Wo74B2t9gM2jBNiaZFlF4S9I48oNmI+6cqESXQb7TrZeEM5uQgSxNvCu+zopbnAV kyiKqJ6gKGR6TrLemBrYZmtc2yvKrxu0AeZO5C7OXPIsr/jY9WAeYnzQk5CtXPTUoVPY 04X10xJ9ufHEEPZQ4HqKOWX4lQXm7feHV2iRMzL+OHERiN5xrL2RkrUKdWUXtqHig93x fEQhcSoOyUGZ9hZY9hOeARqHhz+rFsT2Ex8v8gh2LLwMgwIUdbUb58EgdZjXMKXBf/81 SFSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739219217; x=1739824017; 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=AwUW9CediT4BjqheLCy7x5dX3qZk2ScrPdTSG+o5Gm513nUQA5hpKc4Yo7QLzrCoOM tCpVWZdyhWnyGkpYYjtyj9voEY8P/GrWDhS9YINMkIFBDJ6dfNETBYsemTc3rANHx24Q TfenAN/lt/PQtbQHxiYzG+/EnqTyRTC05/zy9HWaLkSFtN2P5S/g/7EP3hpAAiMD8igi 2aTTLVGWTDQUQ4YZhdaPj8HY7FSFtPzkkO82k81JcjeJQCh0BfWL9PserkoMLLzjhfx1 TOFt3UXnE5NcmcW/94BJKPDOb6iHPycEHK7DYFXaVfwucvnSZiGRdgujAkS/+K+F/3uB jsHg== X-Forwarded-Encrypted: i=1; AJvYcCUS84Ehd+hJ3nsMy+To21VoSJB40sG0UpsXkor8JPgrIaVMXzFpYIszJbUvgCq+3ipUUPOBbwuB6EPl8Q==@lists.infradead.org X-Gm-Message-State: AOJu0YwUAGzXEpxu/bIDNWJTlBi0rJaBpkM99bkIFx5s1ioLwFmk+Xgv B6Z1Q8a8KNnnKOqKR9sLsUjjg0YK/G4PbHG2CTVqX4vzFaH/cX0zbP2XJHcvGB0= X-Gm-Gg: ASbGncv7798yPn//gaJQsGz/keWewbPLG+dqNBCp5FJakrmIirKchw0+WR8Pb2NyjhP w8aL/CqfzGrdKM9ijRYxVolYG7zr9j9pnSpoSuFZRAeoPQs5ZzCjZN0EqtsnucGNinlTT1r2F6z anylHgyc5N9IqfkX/tbho7RsrPE9VPNWgGlDDZDqEYTo0vaxnlV3DSa0FNbxaPOroFJbb+MnbmK foPqlaAqnpRO/2Jn57eqTq2x59OHR9mDNmITdmaRInshd6KeOoGwz0U+4BE0ACbDQxjaoWg6zUO 3anYeXSmzJo0k/bD7i9XH8V4pg== X-Google-Smtp-Source: AGHT+IF0tY5SNTXDToYW1J21Hspvgjxz/6jk5/bs6ru5UkSgBD9Y81K/9+zJgqpvYHP1/U94A96iNA== X-Received: by 2002:a17:903:234e:b0:21f:1549:a55a with SMTP id d9443c01a7336-21f4e1c8b5dmr268029605ad.1.1739219216953; Mon, 10 Feb 2025 12:26:56 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21f3687c187sm83711555ad.168.2025.02.10.12.26.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Feb 2025 12:26:56 -0800 (PST) From: Deepak Gupta Date: Mon, 10 Feb 2025 12:26:39 -0800 Subject: [PATCH v10 06/27] riscv/mm : ensure PROT_WRITE leads to VM_READ | VM_WRITE MIME-Version: 1.0 Message-Id: <20250210-v5_user_cfi_series-v10-6-163dcfa31c60@rivosinc.com> References: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@rivosinc.com> In-Reply-To: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250210_122657_732210_5449D040 X-CRM114-Status: GOOD ( 20.68 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org `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 --- 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 = 0; + + /* + * If PROT_WRITE was specified, force it to VM_READ | VM_WRITE. + * Only VM_WRITE means shadow stack. + */ + if (prot & PROT_WRITE) + ret = (VM_READ | VM_WRITE); + return ret; +} + +#define arch_calc_vm_prot_bits(prot, pkey) arch_calc_vm_prot_bits(prot, pkey) + +#endif /* ! __ASM_MMAN_H__ */ diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.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) #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 @@ #include #include +#include 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; + /* + * 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_WRITE | 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 |= 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(PAGE_SIZE); static const pgprot_t protection_map[16] = { [VM_NONE] = PAGE_NONE, [VM_READ] = PAGE_READ, - [VM_WRITE] = PAGE_COPY, + [VM_WRITE] = PAGE_SHADOWSTACK, [VM_WRITE | VM_READ] = PAGE_COPY, [VM_EXEC] = PAGE_EXEC, [VM_EXEC | VM_READ] = PAGE_READ_EXEC, From patchwork Mon Feb 10 20:26:40 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13969346 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id BB7F4C02198 for ; Mon, 10 Feb 2025 20:27:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=ouo6DX3+Zo2X0OcJAXK5MF+nGOnvNHn35A1HrfTTM7o=; b=Yhb83MVYBVBXTV JmkY4IF+BA2HHmTIyncNgYQtltvX4ghTUD2qEMDiOAwx5ALnB95rCHNy7JmnyJoua47w3SMaMald2 LAQUrm8+RFcW8k1TFgWXKGwJDT40Aqi1xsCWSL4g6gWnPckh8zCiUux0u3BXOxEL/ryEVoOTBu2R9 wkSy4eRbbCqdsBONY6qeaMEgDd9KfFmd5UvYSL1e5wWo59OpmqpbiTqdidytbZYQhv2uboHbvxcvH TYs5edY7CHOCiZRC0htRdYUzkZjpPuZCLb3dPGm6qExdeVOzCnU2+YCq9jxPD0rszDjjypXcnTU5p Eyq+Gv5jySTbWN1wG8tw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1thaMw-00000001Jr5-1W00; Mon, 10 Feb 2025 20:27:02 +0000 Received: from mail-pl1-x629.google.com ([2607:f8b0:4864:20::629]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1thaMt-00000001Jmq-3yYF for linux-riscv@lists.infradead.org; Mon, 10 Feb 2025 20:27:01 +0000 Received: by mail-pl1-x629.google.com with SMTP id d9443c01a7336-21f3c119fe6so107239905ad.0 for ; Mon, 10 Feb 2025 12:26:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1739219219; x=1739824019; darn=lists.infradead.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=3B6EnMTtdQI+PCHJqqbCnAMv5N++/8Mz9heC5HqeMA+F26Z+8ow64eDqFTnuLRaq28 ggXK10sfLPgWBiQFFxDN8RUqf6op8Es/ArlV7Mgx4DhgArnUl3GHdK5Jef9vmA+BH4S6 hclXXA2SvYTA2mS/+xcvmvm069q7DYb2Xpl/Pi3YgHVSp3tqWHx+H5apDRKpYnDPEgpA 4/09Y4EsrJl1M2jbm42fFvZXzl3/uGYW+8WxrbrjURkYdgAnoLMCUqfZQrk2FwuAZJsP cWvUIJymiBsPwJ603Sn4By5xjU9wH3UQdInBg+KvlqaomK18da35rCFsayoS3HeiJrwQ CrpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739219219; x=1739824019; 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=JRta7FxU2c3FdlGiZyxSjEuDS5DzYZm9wHzaz4iaeItkD1W0P4y9ysxGzSHZa2Xq1y 2wIykp+FIG7n1nhX2TcY+cWAOh06vCxKRkZEADb3aKqA0hum1AeRyX1fkEV3gU2Qma75 bk/5iYL0q0GBGPjQCqXopwfUa/vkQnkSo8OxDD9ApUxPKJtnkqjAgiUqYhqURxcLAiBd +QjQvoTy7Mx72d6Xda704ffWvubpnL1KEfyiGGeW6engue8o1BltH7YNqAK9vMl9CpHz gvQA5xgMFi/Zvdqey6d/fSU9GMOVFl7goUq1f1uGCrf69Ci+P+eHhLKmTO1NQzI0Hebk 35Wg== X-Forwarded-Encrypted: i=1; AJvYcCWANSdNPGF/P0bTU6CQ8+ORzuzsOOJ/UbRJDchCVSp1G7oLuiM1lxmHFoiNbuXglVC86z0j/ej1WUuOZQ==@lists.infradead.org X-Gm-Message-State: AOJu0YxdW/9zfpy0tOk2rr9Z6/eaGTrd/uSvIG6ahJ/ynLMXw/XvlRq+ J1osPuXGwrdmWCnyB7R+lGdw6DM9FTTLKG1mE7kRwVjQps3iOff26cQCxC56DME= X-Gm-Gg: ASbGncv0+rSQkuyhFzJPVvXymPlr1iLGYqyrkNh630TkvVkWSTyt2z+Cg2RqPB5+udC bWkmCzbP9IZMPOmEpHUHbvHq+eaVhoHmGq/CMZo1iGEp1bxT3iVJ/+0Fulj9CAtOYtSgRHs+qoJ T9Kup8p8gbikdUc+HkKlGUVwzcqlSkDqCtf9mFW5Q5gCpPZufAs8EIHwnrDvAzJcGGbp+rGDkLs XLMzJ6CEkesk+CUEmI54BpOMPiJ9QI/Nud/60NqRElxoqofzfUZUTyX59welpDAzxVC6rrsmFND pNjhYaLtf99LmTXb/gedeIHBBQ== X-Google-Smtp-Source: AGHT+IED7zcqCk6xnlgZyjRhcEK3K99n9HG3VnjktIjXL97/vl92I1hppw2VD80KTsXjIvmfBlGjpA== X-Received: by 2002:a17:903:284:b0:216:7ee9:21ff with SMTP id d9443c01a7336-21f4e763a67mr241716765ad.49.1739219219434; Mon, 10 Feb 2025 12:26:59 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21f3687c187sm83711555ad.168.2025.02.10.12.26.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Feb 2025 12:26:59 -0800 (PST) From: Deepak Gupta Date: Mon, 10 Feb 2025 12:26:40 -0800 Subject: [PATCH v10 07/27] riscv mm: manufacture shadow stack pte MIME-Version: 1.0 Message-Id: <20250210-v5_user_cfi_series-v10-7-163dcfa31c60@rivosinc.com> References: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@rivosinc.com> In-Reply-To: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250210_122700_000288_6934B940 X-CRM114-Status: GOOD ( 10.31 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org This patch implements creating shadow stack pte (on riscv). Creating shadow stack PTE on riscv means that clearing RWX and then setting W=1. Signed-off-by: Deepak Gupta Reviewed-by: Alexandre Ghiti --- 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/pgtable.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); } +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) */ 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))); } +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))); From patchwork Mon Feb 10 20:26:41 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13969347 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 699A9C021A1 for ; Mon, 10 Feb 2025 20:27:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=ar4SyAPKPfkSHaklPGCLUcvtZ4U6CoVqdHZBbcC+pn0=; b=ADp8fF3bQV3q1C 50Wt9B+Cn9iXo/Ty8OrPsyletRHds5b7C2jStUECAG0WM36+tiOw9lz9Kjvwz7GAmtiBWwJ3jF6iI 4J7nSNTGjN22r6uJcw4r+dIeMyEgLtygFaO/EEj8jbxwC9rO6T7M+QIvnj/luYbDbvJ62NfuImMUS RfRi/BfsJbKAJl/ZisCDcD+IRU8b0WLzaIbk4dW2ZK1vPg/WGc9BEiyMheXTkb7d1bR/ppEDWg/3M MDDt0AkNmcOXYTo99/uc8GQNwY02cYhFuPdaQwon7bqkuWnrhV+4Pkin5hraItzRP6/9O0+FMxVdn tyAgaGShRggV+BkssXaA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1thaMz-00000001JwG-1I37; Mon, 10 Feb 2025 20:27:05 +0000 Received: from mail-pl1-x630.google.com ([2607:f8b0:4864:20::630]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1thaMw-00000001JrF-3gwy for linux-riscv@lists.infradead.org; Mon, 10 Feb 2025 20:27:04 +0000 Received: by mail-pl1-x630.google.com with SMTP id d9443c01a7336-21f44e7eae4so77810725ad.2 for ; Mon, 10 Feb 2025 12:27:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1739219222; x=1739824022; darn=lists.infradead.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=XjtboUVWadLR9OgpO4ZDBEduzhqc2i2vU6+m7F0O9jtZQgugInHxqBXm2Zv9F5Q5tE b5z9hHdD3Yh6spFG6d9TiGm6HmAa9pnhqmRDJWhdUAANzHzcCgzIk+cFe+89MbZ4mYqg w+CjUbLuChfk7SXarC57Oxws2ZtUicKbXZjGjQmOyKwcD3JAUud58Yuzac3veDOsjYcA gpxOKJVGW4x5YZmgvKfYDcM18mcB1UFA8Jrn4yKcZWEow8tUTZrzKPMCmfYUSHp33zQ+ ogLiJ8SzkHbeGn0CZmnUGJizLDx/ng4bddczRxa66QbHFqrrDMu1N1PMhn2KYg3SJbN5 OW7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739219222; x=1739824022; 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=pPwJ6ditE/donTIFnfix9TUPGfZgF+n0dguYiCKqzZYTmlISQaf34yaFBEQohxatbq tkDQdMEEkGi0w5LJGLrRhLqInT+YmVfR4ztqJgx4u9nrZjJ9HTG33D07NrYqBRNG/LLh YUu1ZBKVqw+ssX6HBQXR8TvbbeYuGBqO8jJHK14irbl8XhQHFIjNXUkXGVcgn17k7U7U ZnpxZtf0EAbJvz4usVtuPQMFHGg9Thsm+uYTc979DMpZx3HQ7/qZ36LcL2/ZF8kO8aob SD2W2HI/7biqWa43QwivZFoY0r+t89RyOXdHxzW/YligRXlXbUyPwAHd4A4mgc5o/egv wqvg== X-Forwarded-Encrypted: i=1; AJvYcCVHIgs/aiy5QOqZk8KOb+kFdDFDTmrcyDqTMTTId8qPUZLsWxsc7XUzYFS3mt5pN6Zy5pF/Jvm3KfByPg==@lists.infradead.org X-Gm-Message-State: AOJu0Ywq1u01mgFBf86OhKxEwYyNcqX+w8Y7iyOs2QVw1Qur2H+y/ZGP EfQcTbSrKhMjs680IXkRNCB4aMqutqjNBKEb9A0VVAwH7/vWSzV35dsByYPerKg= X-Gm-Gg: ASbGncsMNWuvNhXyD7ngrNreH+ddfHWVTtt4lhgIFpQd2yOTBnI1USsKEqJ4Jfo3Ite 9MyR2C3R47ZhcNyXwD5J9c7ehj1j+FeWWx7M6Rvu2aKANO8UE4Kp+CpIiR+6eRQckBjM9irLfmv PF9ylkHM2zGYiqJBHWCEAzg/hNBYaac/u1WcO7qgnZwH+UrGfVU4iKu9/YM48MJ6b2O6/GKE2uo JwIckUPAfK0sU6sFQVgoRM2BroWvAvG5CSYQHClF2kzv2Wu/zb6RhGNAcLTA1Q+l9BGm2McbdCj yINFRHmIaxGC63zwz84Y5dvh5w== X-Google-Smtp-Source: AGHT+IGkck8QMdE2zF0rHgCttuOewb5gUafKkLZkDOmgM8iUcqpClnt2HEbL9+VvYLlypifzjE6CDA== X-Received: by 2002:a17:902:f687:b0:21f:4479:a34d with SMTP id d9443c01a7336-21f4e6bf3f3mr236952035ad.12.1739219221922; Mon, 10 Feb 2025 12:27:01 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21f3687c187sm83711555ad.168.2025.02.10.12.26.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Feb 2025 12:27:01 -0800 (PST) From: Deepak Gupta Date: Mon, 10 Feb 2025 12:26:41 -0800 Subject: [PATCH v10 08/27] riscv mmu: teach pte_mkwrite to manufacture shadow stack PTEs MIME-Version: 1.0 Message-Id: <20250210-v5_user_cfi_series-v10-8-163dcfa31c60@rivosinc.com> References: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@rivosinc.com> In-Reply-To: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250210_122702_943053_61145508 X-CRM114-Status: GOOD ( 11.22 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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 --- 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/pgtable.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) /* static inline pte_t pte_mkread(pte_t pte) */ +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))); } +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); +} + From patchwork Mon Feb 10 20:26:42 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13969348 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 4746CC021A2 for ; Mon, 10 Feb 2025 20:27:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=Jp1YU5ZYy2X/dbd2FG6aBkHj99RVQsLlPXppCsfvVF8=; b=Y+WbfUR46O7lnP 1tLoAgYH2UNKeTP/UmFLs60nD6wmIwr8hGJaifTg5rcHFOnfPfAjD7oeh6s+V91BCu73TCyQXpKQM ghMK3FORONasKulwxCHCglC2Q7HoFUu31O/Q1vcD1i28tgoLZMv4FAqPK9xN/q/dicsQalSiza9Kv d6Ml7bb4XosJH5JYbFxqoKelGqYBD4CelMtV91nxU9TWQ80AckCeaQ5zduUolb81Cz+oI+KRVEFbc yIAYSnxlDYld7ClVhGIGQd06l9o8g60JHtUzqH16qktqorYZnjKN8Rsfo0dxA+g8u3pCx6AlWvCFx fyc9vTmP3O4TXqaJ+AyA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1thaN3-00000001K1e-0HCq; Mon, 10 Feb 2025 20:27:09 +0000 Received: from mail-pl1-x629.google.com ([2607:f8b0:4864:20::629]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1thaMy-00000001Juy-3xXz for linux-riscv@lists.infradead.org; Mon, 10 Feb 2025 20:27:07 +0000 Received: by mail-pl1-x629.google.com with SMTP id d9443c01a7336-21f92258aa6so39399015ad.3 for ; Mon, 10 Feb 2025 12:27:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1739219224; x=1739824024; darn=lists.infradead.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=Bb8Gi0mUbdXRV82/EKNO9/g1VQHi2IckH8n7u54lzOqiNGgv2b+pFsx31KMvhz1BvX r/sZMckV4ytg18w1rroV3oAIwFM7kr2uth992Qjlxe4a3a5csWqlAkjj1yYVXXQ2y1GD jOoQKQd59uItcxgdU2wvt5LRlCc4wMshWGFOUuc+4Xxyfqfs7QoY7PWEfINCordNozw1 aqgsRd8BOWM5zf6NB/UX98/DsspA+r/lshOBT7RNLWU5cItLIM6Ohg4KEGIMxJzjMZ4r 9SjDYpwAgWTzIxY+353XtizbupxnZhT7CuKd3q5U2HS8vVZwG83tWlguV25SScgH778N ToLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739219224; x=1739824024; 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=HXhXDiUqZM6HtPWltwVbZrQ4Uf/+QCzMJ+Ow0azaQJOz0Xh+A32PXznial0j2JLBHc ZLMhvK4m7PATv6wD4N2hz1h1dXJQzd15vb2JeItQSdyfueYdrsXKeNaU1/Qf3p9iyWQX QIeyV/fPthZNq5BqRkzirrhzl3jwYFS7B1AHy/m85BqAvYfLImLQH7tG9fp1xTMc5xIE ggxl2ltjTOwVHznyFznUZoFYaCzehLtm9lP+fEWYNoKmqmLRUeVby4rAUV6OyWa7MT5v hf7+grSsTv5AqaDGkknK7iFP1oaEHoEhiaUMWI0eg7q9S5WA1tmfU57cUKL6k343DJ92 xBIg== X-Forwarded-Encrypted: i=1; AJvYcCXV0/q4uRGC0AODLe1PASizASFi4DllWVtDm+E28kKExgXVE2QnHWbq+Fhv+s1qN77D32UeeOGLjeJ8CA==@lists.infradead.org X-Gm-Message-State: AOJu0YxebZq0DKHODyn4knqbWDUG5dgZYwX3rz79JtmzpdyhPNenuRci xO0ZYQM8+Q4bIULabqAmhT7mrQ5RpLJFvwaON1EDh2sw0G2QGu0T8R4xEyIjAO4= X-Gm-Gg: ASbGncvlmiZNQcMSul5sAwk9ACVUw01cbbm5cUtw9PrrqKqI/JztQgzo+W5LHkQGQ/w BwSXX4B7y53Cxx1EoaUeukbRi8xoz6mPlGLCS7BseTb0MP/gTWG40nt9qno/T0rht3YfQNBbZY/ m5KlUrkcdCCf2YQyLsHRqK4r3PP3QOzXspU8S42UOIzGmRcJAZ/34Oi5rbE+UH+Ass/51/ZYup9 lVnfsVpobeZbs6W/Sq7PVzYzl6RyZ5rXQebLKjNFAxU+TUoxWJWA7urz9oVCmMD/b0UXwdMG4YP nHsmO+2Mm4zmDdlstLUBpoCQsg== X-Google-Smtp-Source: AGHT+IH51GCkumMcFJPAZ0u0Kjg1gihjqDbfZuYLpqygtJdnaOIjAF7ExdTAshwwxxeYa5aWxibyKg== X-Received: by 2002:a17:903:2282:b0:216:48f4:4f20 with SMTP id d9443c01a7336-21f4e6bf3e8mr230190345ad.16.1739219224426; Mon, 10 Feb 2025 12:27:04 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21f3687c187sm83711555ad.168.2025.02.10.12.27.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Feb 2025 12:27:04 -0800 (PST) From: Deepak Gupta Date: Mon, 10 Feb 2025 12:26:42 -0800 Subject: [PATCH v10 09/27] riscv mmu: write protect and shadow stack MIME-Version: 1.0 Message-Id: <20250210-v5_user_cfi_series-v10-9-163dcfa31c60@rivosinc.com> References: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@rivosinc.com> In-Reply-To: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250210_122704_995551_32683874 X-CRM114-Status: GOOD ( 15.07 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org `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 = 000. This will result in wrong PTE setting which says no perms but V=1 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 --- 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/pgtable.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) 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)); } /* static inline pte_t pte_mkread(pte_t pte) */ @@ -612,7 +612,15 @@ static inline pte_t ptep_get_and_clear(struct mm_struct *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 = READ_ONCE(*ptep); + /* + * ptep_set_wrprotect can be called for shadow stack ranges too. + * shadow stack memory is XWR = 010 and thus clearing _PAGE_WRITE will lead to + * encoding 000b which is wrong encoding with V = 1. This should lead to 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)); } #define __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH From patchwork Mon Feb 10 20:26:43 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13969349 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 329C6C021A5 for ; Mon, 10 Feb 2025 20:27:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=fqRZ4wBEWVT1nosKJndSQ3S2KQmy/MHGsbij1m9f03k=; b=tu32I4uBbTvU+y CzCucfX++/eMXmo8we0aP8EkrBE2lzN5T9XAfZ287X4IzSz/b46YD0t58DzTZQf5AvXQ+60XdaA31 UOgTFKnqvAonDBEPH7Zx/xqYNG5gSNr3sx/8Dihu/Q/nx9rQ+bNi3r3HzKWviMC7ai2oyJmZEDXCI ib9zIGWbQpdf4N1e3U6YQgljEFBPLPFr2at9Nyc1USGSStu/b2P+Buc9eBvBrfhNqqenkF1BNRk2J CrfGjdx9I2exKzWHYXP+XW8Ht/yjssP8UN2Or21ily/YJerq5Ff56Ac073X/FyYRIkVIZHg8FUCA0 Z//A3XJRoNA5Uw0MYX7Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1thaN3-00000001K2o-3bOZ; Mon, 10 Feb 2025 20:27:09 +0000 Received: from mail-pl1-x62d.google.com ([2607:f8b0:4864:20::62d]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1thaN1-00000001JzD-2Bhi for linux-riscv@lists.infradead.org; Mon, 10 Feb 2025 20:27:08 +0000 Received: by mail-pl1-x62d.google.com with SMTP id d9443c01a7336-21f6a47d617so39378275ad.2 for ; Mon, 10 Feb 2025 12:27:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1739219227; x=1739824027; darn=lists.infradead.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=ODdabZWL4Cpwq7AcxGyLA3W5TUfxphqphTuPhzQ9WR2YDr9AK2CFeOFlb1l0ZkHiMx momGwVrs2hUDFiMVzbIp4etGc6eowaFema2tu13nipn9tTsYeb2QR5L7+zRmsMiS6H3w oCCEElPCxb0xirTawm0gyRE4hpckvLM/jITFZrDcdlUX/7qflExGV+B/2lmMSovFNlbl M5eeSL2lQsidx5DNdW/1gmQhGJtVl3BLIe+7dmSbDljH6uwOERyeN/JYXjuteJy6H7GS NAxuDXJKopDDEY8Ve9L2Cmv4Ea1Im/uGwetxP5nMxA/aRKoFtPZeEl2xSjoIfwiRYNHg 5LgA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739219227; x=1739824027; 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=ImKewoMGyjYVPFAjqHz1nd1V5GBYB+LwBMSczrRFFOVyqql6z1RiLCyctjOzJy31ak vv0QHExepotRBC5TrliuF0T2kaSvqCLU8eyDVpbczYySHRusrYyziRrifxl7uGrFAl95 SLVqFD5WLiozAXpj93IuEqrwlACRz9AbffbZ+j1bLzLMNHCHDN8HwZMAyAsgpve4VwvW friJe0jrkRHtWu8C2Dip5+8uh/SqJn1ejZRR9p/whII3WSirbJDlfxawk964R5pblAhR l3v4++pXYjIo4+7I/T4VJOW/N93UUpbnplbz/DNBAi5aDRs1yW/Noh3xNoDpXUKgYjZb IbuQ== X-Forwarded-Encrypted: i=1; AJvYcCXFPiAPeJG7383JyWf7WI1H1mBumfpo/fu0TXDwNrMhgaxDwUuOpN06X/Nl8Qdx8FrKnYa2WJnWkIfk5Q==@lists.infradead.org X-Gm-Message-State: AOJu0Yz2q948e0oFZstfCgM0nvjfFtSIWbTDt1Z4CceydIFOCzRDCH56 7EkVDEb4LySS3vDZN7ZDHcki2bRen2MWi6whhCOT+10G6MhaZ4I4hCi2I6JOcyQ= X-Gm-Gg: ASbGncsPPxrzBIC8DsqJXXHukUDR7RequZnYBpJsepS0VQfmvFYYAxpgrzAcBRbMLUH N+TUnoqHq8CLE6A88YQqfGuZHrv05jEdpalf/Cnb1ITOg+xC8pIOxvDlzIk1Vk32UcE42ZLJMTd Ggp802Wjm69RQZaQWEPRuC+lUFO8zSCnMr8B0AQ3iTLC2zHZY68Bb7bBeNsRW2+CCfCT4d8iDsM YcGJN+w8am6iPKz8GL39/u79BLTERPAnEZIeMqlDzz8PsDxC4B8kXhyzKf4mJx70sL3RCSK4HI7 v3OXbTCnYNsdQ/Ks1toNagRCnA== X-Google-Smtp-Source: AGHT+IFiFOkgExdj3mQCqaaai4pDSiQ+Hqrea6eAygrZlJssTNgeNQJJiOpmwE+Z7PNIOmAr6Vu5+A== X-Received: by 2002:a17:902:ebc3:b0:216:2f9d:32c with SMTP id d9443c01a7336-21f4e7a1ffbmr262362135ad.53.1739219226925; Mon, 10 Feb 2025 12:27:06 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21f3687c187sm83711555ad.168.2025.02.10.12.27.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Feb 2025 12:27:06 -0800 (PST) From: Deepak Gupta Date: Mon, 10 Feb 2025 12:26:43 -0800 Subject: [PATCH v10 10/27] riscv/mm: Implement map_shadow_stack() syscall MIME-Version: 1.0 Message-Id: <20250210-v5_user_cfi_series-v10-10-163dcfa31c60@rivosinc.com> References: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@rivosinc.com> In-Reply-To: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250210_122707_600849_683FAB68 X-CRM114-Status: GOOD ( 24.16 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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 --- 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) += acpi.o obj-$(CONFIG_ACPI_NUMA) += acpi_numa.o obj-$(CONFIG_GENERIC_CPU_VULNERABILITIES) += bugs.o +obj-$(CONFIG_RISCV_USER_CFI) += 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` takes 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 = -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] "=r" (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 = ssp - SHSTK_ENTRY_SIZE; + + if (amo_user_shstk((unsigned long __user *)addr, (unsigned long)ssp) == -1) + return -EFAULT; + + if (token_addr) + *token_addr = addr; + + return 0; +} + +static unsigned long allocate_shadow_stack(unsigned long addr, unsigned long size, + unsigned long token_offset, bool set_tok) +{ + int flags = MAP_ANONYMOUS | MAP_PRIVATE; + struct mm_struct *mm = current->mm; + unsigned long populate, tok_loc = 0; + + if (addr) + flags |= MAP_FIXED_NOREPLACE; + + mmap_write_lock(mm); + addr = 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 = tok_loc; + +out: + return addr; +} + +SYSCALL_DEFINE3(map_shadow_stack, unsigned long, addr, unsigned long, size, unsigned int, flags) +{ + bool set_tok = flags & SHADOW_STACK_SET_TOKEN; + unsigned long aligned_size = 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 programmed in instruction + * itself. This provides static property on register programming and writes 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 architecture (because + * `map_shadow_stack` is arch agnostic syscall), RISC-V will follow expectation 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 = PAGE_ALIGN(size); + if (aligned_size < size) + return -EOVERFLOW; + + return allocate_shadow_stack(addr, aligned_size, size, set_tok); +} From patchwork Mon Feb 10 20:26:44 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13969350 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 35AD9C02198 for ; Mon, 10 Feb 2025 20:27:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=js2Y6rKRWbZN2EBupnuiQxmO53R9yz977a6iMfeU+dg=; b=ICQbcutf4BLI98 Ab01K3mvSqybIxHBrMf74PFx8vaQQjiUg8DDK3U2+MTrkpArFQBCKCczi8KORdbroAmQVwFKpJh3o uOYKfHzR8zuwuBiM1pMTXTuO8ouySIfhwcNkiHarnFm5ypWGqBujWjmHX/3TSXELlitjo2MQ8Liks JNTk84LKzH9cLX1X/VQBtLFvrc5FfASsq0jQhyTMccROPIKHQqK+G0omvGzG5tuRyCddgyoPEnG+N Z6pjP9TYq6dfUiGt9ZHyM956JxWvS3dZyg/l8HMw8ArJXQqEQCBbW99efCcXA8W1iMORZuh29fUpz V+hYQ47x8dfLpgrXzopA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1thaN6-00000001K7d-3J3p; Mon, 10 Feb 2025 20:27:12 +0000 Received: from mail-pl1-f171.google.com ([209.85.214.171]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1thaN4-00000001K3F-2U7p for linux-riscv@lists.infradead.org; Mon, 10 Feb 2025 20:27:11 +0000 Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-21f44e7eae4so77812685ad.2 for ; Mon, 10 Feb 2025 12:27:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1739219229; x=1739824029; darn=lists.infradead.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=vnjpsbk+fyjg4yLK9VMHE59VBnJqMNSMUjtf5e2TfIJUSBd7/cnsp1V+OL3t75/qGW hpTSt7XgYDcGDNGrFYgYphoKy1blJ3mCM2tX5ZrcWqZeDBrHws+4k9LpFPwOth0DaWyY Sf0PIFHjy8Qcv4qFb8jd4veOMMeU0B6MwXR8nbm4zYjW90dgfaIQpW3IibaQxUtuMNqz WKtu3ZHYHo+w/7ErabM7xEBh2+Nqfpo9x7c14+shdKYDbmswG4yy1T63+J0JSZ/vDzzD NIbhW4AKCYj1HVXNnuy/Ii05UE4lC6GhNpx9XKJQKzEL811P5R6U1miordMmMWl4scFU +6eA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739219229; x=1739824029; 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=RUaQ19FmiGgSaYcbFBupz1WToYZlTQDf4tK459ZkZMYWjwUiq7C6To2RTq/fJRXRuS tS4DoptnhL39ilelhA/CpDU8DVxv/r94pHVXpZUDWj9zd82l59JCQd3NZDipl3QOW7nn dQRGHesCaiQI9riWEyoDCGRB8KDoycB2PWJwg81n6p3H88V1aticenlaoFbJunMgBEeq 9rMSSCmf7Ii0WcbxedwFoGuVWB7dLYaBdFtOAwm9gFqF7lUJEKhwlra12Js7XxrAFz3G GxeImnm46Wrc89xGJrcuKq0VbBgm2wzDQp5avEKgoMCjA3ee9gxNikM/1zMEm4an6r6V gYcA== X-Forwarded-Encrypted: i=1; AJvYcCW5PA5Z0nYNI1mRRGGwRiZpus4cZKB4aa0jFXFV4Y3fq6aJVtclGVnjH+N+wOHA7uWKrw2dIAbHRBc7IA==@lists.infradead.org X-Gm-Message-State: AOJu0Yx32CV0HH4/1D5lmj+uuzwStRuavj/7a3fnOC2gVaVZ1aPLtsP3 Q/ZtvI1whwhz1z+QHORwisb3MMUSn+cWvYIY9bQeMnGnDyz71mIcgDtOGMzMMDk= X-Gm-Gg: ASbGncv6bCmb0TS5LrewE0wLUbs0XnySKZ548NloeIf4TSKTQX7xEGM9LTx81q/v5gU 3Rx1KjolmTUCwLKF+NYPfJZQTjDE0A/OKtsucJQB2c80IgJAlbXofPFMlxOdBX2uFEHYqzWKj8q Kp2PvMmNDgwnzQpUvEBtoBoKtF3HQKzB4raSxJpswPCUl5nKzWnoVHbT8gVSjuLG7ohWoyj5xQS cL6DslcCQ+jSnyWcpTuLMZ3UoVkCgT3RAPfv/JMFww93EFNpGB29XSqbnl0EUTq51p5+o1XjW+5 k9s1wMt01VdO/+Wl+QwV1rwkag== X-Google-Smtp-Source: AGHT+IGZnFyPKV59isrengTEGABOlCfahgTa9XjYAn6G0Fd83Q6lRI2on59iQCcOaOdLCTNcJNym3g== X-Received: by 2002:a17:902:f60d:b0:21a:8dec:e59f with SMTP id d9443c01a7336-21f4e7637d1mr213149445ad.39.1739219229424; Mon, 10 Feb 2025 12:27:09 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21f3687c187sm83711555ad.168.2025.02.10.12.27.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Feb 2025 12:27:09 -0800 (PST) From: Deepak Gupta Date: Mon, 10 Feb 2025 12:26:44 -0800 Subject: [PATCH v10 11/27] riscv/shstk: If needed allocate a new shadow stack on clone MIME-Version: 1.0 Message-Id: <20250210-v5_user_cfi_series-v10-11-163dcfa31c60@rivosinc.com> References: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@rivosinc.com> In-Reply-To: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250210_122710_640938_0A5DFAA6 X-CRM114-Status: GOOD ( 29.73 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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 --- 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_struct *mm) } #endif +#define deactivate_mm deactivate_mm +static inline void deactivate_mm(struct task_struct *tsk, + struct mm_struct *mm) +{ + shstk_release(tsk); +} + #include #endif /* _ASM_RISCV_MMU_CONTEXT_H */ diff --git a/arch/riscv/include/asm/usercfi.h b/arch/riscv/include/asm/usercfi.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 +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 */ }; +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, unsigned 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 */ #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 #if defined(CONFIG_STACKPROTECTOR) && !defined(CONFIG_STACKPROTECTOR_PER_TASK) #include @@ -211,6 +212,7 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) unsigned long clone_flags = args->flags; unsigned long usp = args->stack; unsigned long tls = args->tls; + unsigned long ssp = 0; struct pt_regs *childregs = task_pt_regs(p); /* 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 kernel_clone_args *args) p->thread.s[0] = (unsigned long)args->fn; p->thread.s[1] = (unsigned long)args->fn_arg; } else { + /* allocate new shadow stack if needed. In case of CLONE_VM we have to */ + ssp = shstk_alloc_thread_stack(p, args); + if (IS_ERR_VALUE(ssp)) + return PTR_ERR((void *)ssp); + *childregs = *(current_pt_regs()); /* Turn off status.VS */ riscv_v_vstate_off(childregs); if (usp) /* User fork */ childregs->sp = usp; + /* if needed, set new ssp */ + ssp ? set_active_shstk(p, ssp) : 0; if (clone_flags & CLONE_SETTLS) childregs->tp = tls; childregs->a0 = 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 @@ #define SHSTK_ENTRY_SIZE sizeof(void *) +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, unsigned long size) +{ + task->thread_info.user_cfi_state.shdw_stk_base = shstk_addr; + task->thread_info.user_cfi_state.shdw_stk_size = size; +} + +unsigned long get_shstk_base(struct task_struct *task, unsigned long *size) +{ + if (size) + *size = 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 = 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` takes pointer to @@ -142,3 +177,88 @@ SYSCALL_DEFINE3(map_shadow_stack, unsigned long, addr, unsigned long, size, unsi 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 specified by user. We + * thus need a separate shadow stack too. How does separate shadow stack is specified by + * user is still being debated. Once that's settled, remove this part of the 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 = 0 and size = 0, this is special means to track this state + * 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 happening + * using current mm. + */ + size = calc_shstk_size(args->stack_size); + addr = 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 = 0, size = 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 != 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 = get_shstk_base(tsk, &size); + if (!base) + return; + + vm_munmap(base, size); + set_shstk_base(tsk, 0, 0); +} From patchwork Mon Feb 10 20:26:45 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13969366 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 0132EC02198 for ; Mon, 10 Feb 2025 21:41:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=4tWZuXcpoiQvWKJxlIra5S65tkHcJk+ZXShwOiX/68E=; b=QNuX0f4ollNA6A OYuktS//wMZcHsG7EX9zRkbm7CJt5tOsVhadHy0K/FuVMT0ygD0AVToLLhTcWL3tTCuB5HxDDa/8z ENvHnnyOOKiS2OauQ93e3Cv387nuTk1yilUcvLjdc2XzTZpU0tJ372KRKtLxc8W+NU+yQY/bOtvqw V3bYXSVx3z8+dI3JxMxzVLcsgyTdBjrEcy4mdIndWyPnaprz8XmggminBTs/hWG+BFVxZjmLeFEGE XgG9RMBKspyHhe2rkC/9byfZ1vFw8HpSpTiPF56Y0UhKyc/m6FBs12iH9n2dz7S5rX34ux412qNsz I3gvQquuB1zDZU8z8DyA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1thbWK-00000001WkC-05Ej; Mon, 10 Feb 2025 21:40:48 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1thaND-00000001KG5-47Q6 for linux-riscv@bombadil.infradead.org; Mon, 10 Feb 2025 20:27:20 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Cc:To:In-Reply-To:References: Message-Id:Content-Transfer-Encoding:Content-Type:MIME-Version:Subject:Date: From:Sender:Reply-To:Content-ID:Content-Description; bh=Zfw9OtoVzzM7Z5oL2c+Ej/yDm51xyr1WNtqXJN3fx8s=; b=LLWSmqTA87XDE9SiogSFCzdH7D oGZFpmAjDoW6J+28EneNOUXDXFgu6WZ0Ib6mIYrUYoM/d1Gv8WQMCgW1nlk0C3VrEA6erOEAiKoBs 1SpA6qNZ34y1/XvpZN1bE04odjAp8b04HPa/BV4ipSGguAIEpzlTBZ3OIiNjiCIDTDv9fIvPpe7IF 7RUWeuV4kRI0z8gTOx9n/F1itr9uxV0WIPyKpsgAo1ok1VmBZSxTEs5A0alj7kddjAwPCrpPLFyv9 KAkjLhY4+87USigD1/oPUNEQRBL6dPEUF4dhdZ0z2HK845WKJI41SonfoRt2SB0mO6e/L4cAggGDC vGiIEpgg==; Received: from mail-pj1-f43.google.com ([209.85.216.43]) by desiato.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1thaN9-00000000Dcf-0BZJ for linux-riscv@lists.infradead.org; Mon, 10 Feb 2025 20:27:17 +0000 Received: by mail-pj1-f43.google.com with SMTP id 98e67ed59e1d1-2f441791e40so6639074a91.3 for ; Mon, 10 Feb 2025 12:27:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1739219232; x=1739824032; darn=lists.infradead.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=JArWSZ6MR+tr4L3EgI1R1hx7Db1lsZy9hkbI4CFqemczQUNAFVxCsPm4rieqICYXff hQs5mQ/UAuEUl+eYljtBSJN+gb4sfgZH+Dedwb3/Gq2Bw8IcmevI2XpjKo7KvrqY1IFt Z4iOkiCp5OKmg/AeoA3W557vuWL06g1buQ92M4LgGnSTfy3ekWtVoOdkKyHT8WSLGYY9 g41b+f9Q7I0HMvitPyzqj1WIppzl9teNwYUeQBeTrGYErhrKU39SaIOilku6gfv9Z/f3 2Jzdc08WT0vvKRoBHeYX+JhLJYNSOY2ctgNJICYqLn4bEeALy/kJ5VYLefRdTOdHDIhT /v7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739219232; x=1739824032; 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=Ihzk6UfV+L9iL+viwvLTkwMiLFzm+2NDnlocu/GbR5sFg4iCvRwSolp33l6ZgQEtBZ oBi3Az06PeNH0luVF7d7TpSwDJ6xMoEL+dVY6kZTNnzfxEECE/RsIkQUeX9PTG8Lwjru v2xL30GdL9DK8glNKSfL59yQ2IK+pN2K+vW+TpI0Z602TdddFxCbLtBFtb+364DwL9mx /vhHdFYjYAZtAN8KdNe/cd0apxKhRr5yCJYQmN1qmq11YeUlISrZJBnEdbnbxp+e3E+6 MT7+dk3IDmyEKAZt71lkCfoD32XygHzfIt3ukW0HkoHQ4nmQduNCoT+JjimezuAByYuY qX2w== X-Forwarded-Encrypted: i=1; AJvYcCVPPAP9y5BJe4F5ioMfwE2MwxHNZws1Fe1IVAHAh4XzLDIUZWDv84V1CiVVioswPifuHCCPyjqobMSVKw==@lists.infradead.org X-Gm-Message-State: AOJu0YwigJMI8/qk1oOrCaykAVHG1fGJhSBpVmJRPgEdWaYENyc5fETG +KYazFiQrp+s0QeCQZ2kxtSv7pFpxx2BAgHiSPAt71lV/S6xScOJl0eowN4ydSI= X-Gm-Gg: ASbGncuP+KTjbti+XUb0oiiaAT8XTHPpPQ44AzR3flsHQpmTnJrx7KZ8Ax+YmgYgoBt GTqwaj3fdYQrTBBxoVWML0UTlT41gu4Rt7t0QZEHVLeg6U0uBUBc7X5iFHs9y22ZD4wEjSZngir bQ++xKwFaiKS6Ztyn7h91xS4/dpwq1LjVLZ9TgWZ8wXmXjkEyDU7X5vu05g6lkVQbsLUBIkaJhj NJzcBgsXi2Mw2CWRCg9JVOy9RcyfPRGmrwd2PQMCu/Y0zTUJtMMIt2zt0qbNlkh5FJ5YYy8b3og kmWa3fzW4UDTI62h4ig7bD+VHQ== X-Google-Smtp-Source: AGHT+IE5Dg5nkfBxu0gDkqjTzqTfKYquc9Psb1XN5Wmp92oZsUVGzrjZXZcy3FDrBIXQzaUWIDbAIg== X-Received: by 2002:a17:90b:3f4d:b0:2ee:c9d8:d01a with SMTP id 98e67ed59e1d1-2fa24064713mr22932653a91.11.1739219231942; Mon, 10 Feb 2025 12:27:11 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21f3687c187sm83711555ad.168.2025.02.10.12.27.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Feb 2025 12:27:11 -0800 (PST) From: Deepak Gupta Date: Mon, 10 Feb 2025 12:26:45 -0800 Subject: [PATCH v10 12/27] riscv: Implements arch agnostic shadow stack prctls MIME-Version: 1.0 Message-Id: <20250210-v5_user_cfi_series-v10-12-163dcfa31c60@rivosinc.com> References: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@rivosinc.com> In-Reply-To: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250210_202715_607292_6A70501B X-CRM114-Status: GOOD ( 24.24 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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/usercfi.h index 82d28ac98d76..c4dcd256f19a 100644 --- a/arch/riscv/include/asm/usercfi.h +++ b/arch/riscv/include/asm/usercfi.h @@ -7,6 +7,7 @@ #ifndef __ASSEMBLY__ #include +#include 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 long 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) #else @@ -42,6 +50,14 @@ bool is_shstk_enabled(struct task_struct *task); #define is_shstk_enabled(task) false +#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 */ #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 = pc; regs->sp = sp; + /* + * 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 &= ~SR_UXL; 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; } +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, unsigned long size) { task->thread_info.user_cfi_state.shdw_stk_base = 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 = shstk_addr; } +void set_shstk_status(struct task_struct *task, bool enable) +{ + task->thread_info.user_cfi_state.ubcfi_en = enable ? 1 : 0; + + if (enable) + task->thread.envcfg |= ENVCFG_SSE; + else + task->thread.envcfg &= ~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 = 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 __user *status) +{ + unsigned long bcfi_status = 0; + + if (!cpu_supports_shadow_stack()) + return -EINVAL; + + /* this means shadow stack is enabled on the task */ + bcfi_status |= (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 status) +{ + unsigned long size = 0, addr = 0; + bool enable_shstk = 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 = status & PR_SHADOW_STACK_ENABLE; + /* Request is to enable shadow stack and shadow stack is not enabled already */ + 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 = calc_shstk_size(0); + addr = 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 release it + * Although, if CLONE_VFORKed child did this, then in that case we will end 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 enable again + * then in that case, it'll get entirely new shadow stack because following 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 availability of shadow + * stack whenever VFORKed child releases resources via exit or exec but at the same + * time we want VFORKed child to break away and establish new shadow stack 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 != 0) + return -EINVAL; + + set_shstk_lock(task); + + return 0; +} From patchwork Mon Feb 10 20:26:46 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13969364 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 31A28C0219D for ; Mon, 10 Feb 2025 21:41:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=kfy4eS9FV9w6MtMP9gjxFp+GJ73mPOqDk6bl0EcGfo0=; b=2d26bmJEstYXkN c8k1dBObJVQ0I4dS2Qa0Dvs/5I7ql4bxgR2YOXCvjijqcJiiaLtthCxnmTcSbcyFp+zhWfXYRIebl 1DdJTpk4wuDwu2SQROL+GuJDkxp9s3L7j5Ngw1LXtRYXPyCGkiLiWkQun6sSTsYMnTlD5Qkb8N1RO GhdUFgCsn5BE2D4Z2IjLk5y4OOlLU8HHP955D/4OVWKD/s62H1O/hfukZTqCf/tNWHIeyz/fGCSUQ V+/w2xKHCj9qPz7byGyHK4i4bCGVjA1cTVfe9ggqNW1JNSZ6W4Zwzi6t20tSIZFVXEfG/Cn22iRuw E5IcQB7pTyzWG+0U3mRA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1thbWJ-00000001Wjs-1kUm; Mon, 10 Feb 2025 21:40:47 +0000 Received: from mail-pl1-x62b.google.com ([2607:f8b0:4864:20::62b]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1thaN9-00000001KAe-1hrN for linux-riscv@lists.infradead.org; Mon, 10 Feb 2025 20:27:16 +0000 Received: by mail-pl1-x62b.google.com with SMTP id d9443c01a7336-21f92258aa6so39403035ad.3 for ; Mon, 10 Feb 2025 12:27:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1739219234; x=1739824034; darn=lists.infradead.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=YAaebxUEauKQxHTkeq7w1olOutOmV8RNG/5rzLzyqzfYyoHmMHRye5y/W9xX0X7aRd NWrV1/fwqkBcQbIdUymyn3uc0M1lj8TqAXOm1jmkxFIuDZpth7n8GlXWPE3p1vrHLWPQ YMwA0E6aIcanGPvOj/tSwklEXEnRZtjBxSQItv9GNCdmpUmX3Wa6YibtIXbnNrNrygPq tpmiOHvjT234BF/pzpQp28I5buCuR5XR9r//Aoz9qoo9Izq23ThNkb7Jb6VlbQR6cvMi kZ0LclMrtrz3hF12ZhpDAbMX8SNlJ7PQ0TQY6KjnCbTR1Sth9MYLuk4dlgenLEzAVt/Y 0iOA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739219234; x=1739824034; 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=SG4twZ9ELxskfyy2hUzeS8SSvvkAz57ifwOdNWAFDBBGFgTI5p9g9+nI/Xf48u2Tus d0ODkoe9zy4V89ipwomyccQ6EzD3FePvnWw26IlvFGJlJQjUW1Kuo/i5uZDIatkE/47k k7EZ1pX81bs0gD4+GqPp7hP9OaJcbmnvFjD+s8uMK20l2+hhi6Y+N/Pk76u2STKrfGk8 RqSJZdvZBw16fe6+lkuxjklsOHVipfk8jiBXb6UYEBxKyGBOOocac1EF3rIQyenhqbXI kzbA/kuRc0qHE1nDz3vty85PtTaKvIHu2yIVqZ32SRLeShczK768Xis28loiGucWD+TW 05Lw== X-Forwarded-Encrypted: i=1; AJvYcCWTG6DT0BSDQiGX9rDcWY4aeYv6pEIYgZVMiR/jLkJqhiMEGjyfTLxz+gYbMuojKPyjWRx92sq8o9VWiw==@lists.infradead.org X-Gm-Message-State: AOJu0YyN4kMwCODfV0kdnaghUhJvg8Vr8u8jJaHveNWKteWE2Pd08pyM cfNNd+mWS4uix+a/NSOMVAExZ9dJoznQGRO8USdYSYXp5YX8uJyLRejn4Lf4imo= X-Gm-Gg: ASbGncvHotL+MSY3+zd8KqULrR3pGuwwGLnAj9LgKvCAi++5Qkt7HxxcLSs8JJZM3h2 HlwlfqsoaLImSIcu8n1iuGVK/Tj5FGkDE6I6tkWl5roxqSawo2/n9K5BWeU1fs7SBQeZW3LJpbo xnlxQN2Tw1VumOa5Vjmivrs5yG8P8WSkr17FZSb7v5L8azm5z+4v18WavKUbSHH1irPv83n5sh8 nQNg+2Mp5epZjpswiYpPKaP5MBpKAU/cfNoQdnmnU7ckIu5/Egz8hyL6gDLO38wE2PSgOpIPTG4 RnzCCimlY0ETW+aHO+0I5upezA== X-Google-Smtp-Source: AGHT+IF/ji7Q2jsk3glAmrvkgQ8qeyspiS6WaI2wICDLM2NlYpH7cnCIzarKdjCUYb/eofkHRWzaPA== X-Received: by 2002:a17:902:e946:b0:216:69ca:770b with SMTP id d9443c01a7336-21f4e6bc5c3mr267481085ad.12.1739219234469; Mon, 10 Feb 2025 12:27:14 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21f3687c187sm83711555ad.168.2025.02.10.12.27.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Feb 2025 12:27:14 -0800 (PST) From: Deepak Gupta Date: Mon, 10 Feb 2025 12:26:46 -0800 Subject: [PATCH v10 13/27] prctl: arch-agnostic prctl for indirect branch tracking MIME-Version: 1.0 Message-Id: <20250210-v5_user_cfi_series-v10-13-163dcfa31c60@rivosinc.com> References: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@rivosinc.com> In-Reply-To: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250210_122715_475856_26845593 X-CRM114-Status: GOOD ( 24.37 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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/usercfi.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); #define PR_SHADOW_STACK_SUPPORTED_STATUS_MASK (PR_SHADOW_STACK_ENABLE) @@ -58,6 +64,14 @@ void set_shstk_status(struct task_struct *task, bool enable); #define set_shstk_status(task, enable) +#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 */ #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 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); #ifdef CONFIG_64BIT regs->status &= ~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 = 1; } +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 = enable ? 1 : 0; + + if (enable) + task->thread.envcfg |= ENVCFG_LPE; + else + task->thread.envcfg &= ~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 = 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, return 0; } + +int arch_get_indir_br_lp_status(struct task_struct *t, unsigned long __user *status) +{ + unsigned long fcfi_status = 0; + + if (!cpu_supports_indirect_br_lp_instr()) + return -EINVAL; + + /* indirect branch tracking is enabled on the task or not */ + fcfi_status |= (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 status) +{ + bool enable_indir_lp = false; + + if (!cpu_supports_indirect_br_lp_instr()) + return -EINVAL; + + /* indirect branch tracking is locked and further can't be modified by user */ + if (is_indir_lp_locked(t)) + return -EINVAL; + + /* Reject unknown flags */ + if (status & ~PR_INDIR_BR_LP_ENABLE) + return -EINVAL; + + enable_indir_lp = (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 != 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 +int arch_get_indir_br_lp_status(struct task_struct *t, unsigned long __user *status); +int arch_set_indir_br_lp_status(struct task_struct *t, unsigned long status); +int arch_lock_indir_br_lp_status(struct task_struct *t, unsigned long status); + #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 +/* + * 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 will + * 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` instruction. + * 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; } +int __weak arch_get_indir_br_lp_status(struct task_struct *t, unsigned long __user *status) +{ + return -EINVAL; +} + +int __weak arch_set_indir_br_lp_status(struct task_struct *t, unsigned long status) +{ + return -EINVAL; +} + +int __weak arch_lock_indir_br_lp_status(struct task_struct *t, unsigned long status) +{ + return -EINVAL; +} + #define PR_IO_FLUSHER (PF_MEMALLOC_NOIO | PF_LOCAL_THROTTLE) #ifdef CONFIG_ANON_VMA_NAME @@ -2811,6 +2826,21 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3, return -EINVAL; error = arch_lock_shadow_stack_status(me, arg2); break; + case PR_GET_INDIR_BR_LP_STATUS: + if (arg3 || arg4 || arg5) + return -EINVAL; + error = 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 = arch_set_indir_br_lp_status(me, arg2); + break; + case PR_LOCK_INDIR_BR_LP_STATUS: + if (arg3 || arg4 || arg5) + return -EINVAL; + error = arch_lock_indir_br_lp_status(me, arg2); + break; default: trace_task_prctl_unknown(option, arg2, arg3, arg4, arg5); error = -EINVAL; From patchwork Mon Feb 10 20:26:47 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13969355 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C9011C02198 for ; Mon, 10 Feb 2025 21:40:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=vvf5U8P1ZUcySyE1vXvRk52WWtxnDT+sqWXeLjvDYqg=; b=Bv6pTcourjK+vu Nz9YpYblo6//9KzBgJhpcsVTQ/xjhIlFQKGx6JCb33S88otWveMtGaj3s1uQkBmwWoog7z0JNZhHI w+qO4it0wnvz3T5v6KMvMKT7AaB6PefC6wWGpD45qoid0TrL2mYVLP9faonXOu0PR+1TJAy2/Qi7K L/Gwdt+yoFEGYPViPFf2EMUATUZPC5ecxyR2gi7V/Y6Yjmrx/x9qoPtvhqyj9+xjNp/y9RF7Inav2 GufbqPp4UR560ptEtZKIijAIXCvGQF6qDulpe2030HGW88J5Mc7wCns438CGIT70ylUoqqFSJfPcI v71+QMDmf4PTRj4iR/Bg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1thbWK-00000001WkK-2YlN; Mon, 10 Feb 2025 21:40:48 +0000 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1thaNI-00000001KKq-0uCE for linux-riscv@bombadil.infradead.org; Mon, 10 Feb 2025 20:27:24 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Cc:To:In-Reply-To:References:Message-Id :Content-Transfer-Encoding:Content-Type:MIME-Version:Subject:Date:From:Sender :Reply-To:Content-ID:Content-Description; bh=M49HWf8gzr6wWBf1Qtr4VOivQAEEQjfyuWgORGOVTSw=; b=U1odXWMQsnG8J2JPj0NabsWbfK oKZ0SIfJz3Kb8ccMSZ0jiJMeJTWchoDvCTcf6GPKSbiWL3CI/bJWq/mBWWO4m2VybEI+tzzlsNyzp /it1sDX/d6Wfr2JXT45CJr4A0KRxKI5YETuOOdbaE4m8tCazqph2KmBxzVMNpLC8qGuoBBXZL6LXk /09NBWdky/PZtvvExdd8wWRq+Z4n8cb2Ou5huNF/7S+++zRZA5GgcUDq9+YdmtYMJIJrOBt8enxSW FAFqTm9xKXwkynwqjZcumkRmbAHW2eQDuYKjCGu3X82XVJGJtrTj3TgsYvDsUFWESFmLrnDFeOEf7 jxJ859iA==; Received: from mail-pl1-x62e.google.com ([2607:f8b0:4864:20::62e]) by casper.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1thaNE-0000000GihS-37Df for linux-riscv@lists.infradead.org; Mon, 10 Feb 2025 20:27:23 +0000 Received: by mail-pl1-x62e.google.com with SMTP id d9443c01a7336-21f48ebaadfso79066935ad.2 for ; Mon, 10 Feb 2025 12:27:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1739219237; x=1739824037; darn=lists.infradead.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=ZSB+xyG37nBfjq8xOykSwm6uwAPqPiV4eK8DXKMEGdWv7wdHPdOFrAHaAvE2FqgTyj oariIKAeC3xVzOQhWJpDfwAZTbWQ4UFRwG6k9p1U8v70Pzx8syw7iyMQvJfM7jFOAXkj ug7riVZEOICEIXeaN+ZMnYmYuY79VkWZCEpkuvyxsWJ8+DC+3d7ahDMbjAeUi6nq2aoe mW4r3J3jBV6bFIm9TESW529aM1TZfbfRkEvCNpXd+VqrPIA1x1yh2XHFAcIj07QcUaBZ pl8AMuedZzoluKc5B25V92N9+jKvmjR8p39m6xolA5YOadwRqAj/f69vONScMixRXPxe 3PJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739219237; x=1739824037; 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=veBDQuBBvT3yufkOAG0A3z7wTvmCdGKjesUHbJ1x2BzmCe+zI4oq9Otl/xP5XLGEnN 6uYZdxujlb9Ezyo8r5MOihtt4TyIJQ4k0CIlo8dq4BKCy1rIKHpihrq14yWjPEpNU96K 4MadyWT0310bPSlbZO8es1AMktdFjKpip6PW8f4qQfflNdZal/oYyA1hK6z6pLQjSNvB pVvKQxlNDRV70DROfPZ07B6FV72Paq7ZYe+4VPlg8EpDcvHM8fyvcgdDhm2nMsXdb/tT zMfczxeQ9KOVtKr+UXUiJnNvlsbNr2sf6L0IqGiS3CxsIGbozHAY2P1EYpaV0Yc/m5gD YvFw== X-Forwarded-Encrypted: i=1; AJvYcCWg96rSg/7Ie8t0tiQoYED77jz10sBkm1RNKsWmoV8PSoTCm8IO3yCZOEYXJm48brq6bLxxcAjJ4jI8kA==@lists.infradead.org X-Gm-Message-State: AOJu0YxTksUHXlrhIVnVcco0h+A1+Nx4MtTPpoFtR2UMxLZsr6Ur8MYl PWeouegwQI4Hd1KZSKsWGarLU8w5niJd61FbNzrSxap7mWVev6B3Yf3X4q4mfYg= X-Gm-Gg: ASbGncvIdIvT4g8yGDy3jDcUkYWZWe9tLfT5CKgxdE29+0DFZ3T6QTsnUe0FXZKR85g 6RGk6I5++vHOFJubR2txSWiLcJUNwm903A7UpKN26CzKUnGsKsIoQeHqWpeueYRnJnoOB8DNh2L uag11GLvYJ2Yc5RaFPxg8mdlKLWMuHMMEyqUiS42SP246aewtb53C6fUYWyw6x8mXTv8lfCeL9Q RzL6k+Au8rnP1Y8S+NUbn6eRlXcbV52mTIZO4NdTer1howREqvdJjQRy4p88ST3IVkVmXud6V77 puDWI4HvP+245MqfDAPkcjnBqQ== X-Google-Smtp-Source: AGHT+IHdHEBMhep7yU/mVgZUfZFs7fvP4+JZwXsHPWxQJF2WCxVaTQUYKv93Rn4bqO7ShpSAaGLGsg== X-Received: by 2002:a17:903:46c6:b0:216:393b:23d4 with SMTP id d9443c01a7336-21f4e1cb92fmr222614515ad.11.1739219237018; Mon, 10 Feb 2025 12:27:17 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21f3687c187sm83711555ad.168.2025.02.10.12.27.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Feb 2025 12:27:16 -0800 (PST) From: Deepak Gupta Date: Mon, 10 Feb 2025 12:26:47 -0800 Subject: [PATCH v10 14/27] riscv/traps: Introduce software check exception MIME-Version: 1.0 Message-Id: <20250210-v5_user_cfi_series-v10-14-163dcfa31c60@rivosinc.com> References: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@rivosinc.com> In-Reply-To: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250210_202720_791366_424B6F5B X-CRM114-Status: GOOD ( 19.39 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org zicfiss / zicfilp introduces a new exception to priv isa `software check exception` with cause code = 18. This patch implements software check exception. Additionally it implements a cfi violation handler which checks for code in xtval. If xtval=2, 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=3, 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=SEGV_CPERR. SEGV_CPERR was introduced by x86 shadow stack patches. Signed-off-by: Deepak Gupta --- 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/asm/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); 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 +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=16 */ + RISCV_PTR do_trap_unknown /* cause=17 */ + RISCV_PTR do_trap_software_check /* cause=18 is sw check exception */ SYM_DATA_END_LABEL(excp_vect_table, SYM_L_LOCAL, excp_vect_table_end) #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) } +#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 = false; + unsigned long tval = csr_read(CSR_TVAL); + + if ((tval == CFI_TVAL_FCFI_CODE && cpu_supports_indirect_br_lp_instr()) || + (tval == CFI_TVAL_BCFI_CODE && cpu_supports_shadow_stack())) { + do_trap_error(regs, SIGSEGV, SEGV_CPERR, regs->epc, + "Oops - control flow violation"); + ret = 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) { From patchwork Mon Feb 10 20:26:48 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13969351 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 620D4C02198 for ; Mon, 10 Feb 2025 20:27:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=4zqeylQw6K1XmeN+KmrsDIr9DHpPuxliJ1VDOvHF6kw=; b=Xe0CMvBoNlVx+p spCWZFU2hHc1wYG6GVtNzfLEB+qfn362mBcArjQiotEbvXacsi3RL98TdSzpfYnu2dw8TxtKdzi15 BwI9R/hEBTWGBRyjNjntndoDwZgQXhWKsMe8DNWJwbJ9n9p4ZpFlLfHHq0tYP1DlLfdEjQEqddCgf KkN2BSOXkfdujHCYltINx0PMk1jeJVGa1MXaOQLi5JxZiM1pAAuXMtdxgFICAzJhHeNgpWyYnqda5 +KtavupmwM/EMsMYxJX6ACpXlGGczHB3KarYzEgx+YvteZyGoDgHGExVKnlFqUVWE5dFJeLBjPYCU QjwqOnof6iUHTZrZg31A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1thaNL-00000001KOw-0neT; Mon, 10 Feb 2025 20:27:27 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1thaNJ-00000001KMB-0UwL for linux-riscv@bombadil.infradead.org; Mon, 10 Feb 2025 20:27:25 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Cc:To:In-Reply-To:References: Message-Id:Content-Transfer-Encoding:Content-Type:MIME-Version:Subject:Date: From:Sender:Reply-To:Content-ID:Content-Description; bh=N78rd5bu5L3Nb7UCylb6NhXzPkib+ej1wIZfC16ALRg=; b=nLBe0smp5z0fCktB7FwNphjJD2 gZLW7nbgR6hY/MUvCtzQB17aGpoY5yp3+bnuCNzQrwi2MpxmJABrRmjo9C7atp1093J/0pSIgYHnr cJWNOsAvlNPVxGweu3bGGUpPUeuQ+OdpLoMMR7OtpwQODIJcnzEZPtvDDUo5BLrV3/uUn/KTLiI0c TF700l2kFoI1CjfWPnm9iJBfcDKQfjss2sKu6Qemz+G4XQ7QrqOeuYqnIKjbazFzJ5p4Tsn7lM0gB pFc2hmDQvQxB20J+HPcM5FX/wfkCi73zM/6LdkM/ugxhTEs4cR2/EnfQ0XeYm6hpkOGrfurnJ9r52 2wCOTvfw==; Received: from mail-pl1-x633.google.com ([2607:f8b0:4864:20::633]) by desiato.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1thaNG-00000000DgJ-0721 for linux-riscv@lists.infradead.org; Mon, 10 Feb 2025 20:27:24 +0000 Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-21f6d264221so28882625ad.1 for ; Mon, 10 Feb 2025 12:27:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1739219239; x=1739824039; darn=lists.infradead.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=DOtTtJ0o4w2GV75fdbzTJuprmMaorjmm/DYYzkaD3BaZD8ZxC8naSQeVkgKO6MQzbS keD0eFi07+qUrJnhLVuqUoC1ivaCVx3yF6AliPsuHi7I3/S68ZwkHDy9NJbWFITvGIPQ gpIlOweo/lL1wd951RIJdRNHHWn5IXI3FL1fPhPBvsT7rsV+oOaqkaDzc954okC5yMza iE2dHsq5fmeOikdRIAdN2BGfzmUHbK0o5nF1Q5rQmi6oJZX4N8DB589yDgHkG6o5YqjE dOW6ClgT6O8gUju+6MCpFcNFOFNG7cpX0MsBl3QhCdTu5kRSkh/05R0f/U2M+ddxrLfq FAwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739219239; x=1739824039; 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=BIyYYrKxcRqeytBizmKZtY17ltuZ4JPDpW/Avqkg1dZj6Ado+iHfzwwV/QMiTWoCUv j2/jGrOYwuSt/uD6F7MaolcYfde9RobToQcsi6e2zfVInDUk/oR5TStVD978UiW1Cy5u kMQQXgrNRqBawKOmHR+GgsdSo1YRaeYGqx24UT2SWsJeqz5uTX21u7rKxiwhxX2244Dg kpiemXW1hqTL/W2g843KvERg3JDFDS0GxMtkSL9v+ix43/mSS4DzgrBXMCsAnPTwi7BN 6v4wVC8oao5BlfvYZXBB9aUcrL1c/EIMqz8r3jyXtY2EmM1aeJk8kwFUkIgszY9wiNJO dKGA== X-Forwarded-Encrypted: i=1; AJvYcCU+0hKQ6qg7oNNbi1AR+TNSI2IBMqQYB9+9Kkz1O3kYx1BKKTwz0xdIBuHTwRTRxBQFDAoslvReJDI8Qw==@lists.infradead.org X-Gm-Message-State: AOJu0YyyuSGdSJL2n5BNMjZ0D/G1tYy3bYsaRPZFQ1elP9zmNQ0ggssE DNYGa42tnLyEuIWGt7Mzpi0+RT4qbN5tD4A9GYpHbCDHoqME8xuheGvVuJW1CA4= X-Gm-Gg: ASbGncuktSKyzCTHRmtYHJvIl2rVEmLFggcoSpw1B4WUtEcFXfqrin04FXeqdZ+kEX+ NgERV+vkb1pL+NIIofEJe4EuUCnJa3jRljKCUnhzM5o1hb5A6vg5K8gqong99qLDuCXu8xY6NtU xBjoNfzMnPqzh2VREReftqJsLpkDBVrQpi3hOIWYxEHqFDYe5Nv6B3EOPFtU5hDPQsRE+tOyTyA jkRX5f/WV83MVjBQpDREy/eBhtKsQ4P9SK1SAhD4PgyEqUn4YicAOL8iQEpdRBlD+IxPLeTBcP5 uSRLY8vKMYnyq0c33KDiwdlmjA== X-Google-Smtp-Source: AGHT+IEqr2w6ar9z8igWE37AnqdTZQAskGxOfpDyMik3dNCSypk77ZCJWfXC9IN3REVr6mH1hjulIA== X-Received: by 2002:a17:902:c942:b0:215:8809:b3b7 with SMTP id d9443c01a7336-21f4e1cf107mr224484445ad.7.1739219239596; Mon, 10 Feb 2025 12:27:19 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21f3687c187sm83711555ad.168.2025.02.10.12.27.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Feb 2025 12:27:19 -0800 (PST) From: Deepak Gupta Date: Mon, 10 Feb 2025 12:26:48 -0800 Subject: [PATCH v10 15/27] riscv: signal: abstract header saving for setup_sigcontext MIME-Version: 1.0 Message-Id: <20250210-v5_user_cfi_series-v10-15-163dcfa31c60@rivosinc.com> References: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@rivosinc.com> In-Reply-To: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250210_202722_190710_CC75359C X-CRM114-Status: GOOD ( 25.12 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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/vector.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(void) { 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) #endif /* CONFIG_RISCV_ISA_V */ 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 -#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; - hdr = *sc_vec; - /* Place state to the user's signal context space after the hdr */ - state = (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 = (struct __sc_riscv_v_state __user *)sc_vec; /* Point datap right after the end of __sc_riscv_v_state */ datap = state + 1; @@ -97,15 +98,11 @@ static long save_v_state(struct pt_regs *regs, void __user **sc_vec) err |= __put_user((__force void *)datap, &state->v_state.datap); /* Copy the whole vector content to user space datap. */ err |= __copy_to_user(datap, current->thread.vstate.datap, riscv_v_vsize); - /* Copy magic to the user space after saving all vector conetext */ - err |= __put_user(RISCV_V_MAGIC, &hdr->magic); - err |= __put_user(riscv_v_sc_size, &hdr->size); if (unlikely(err)) - return err; + return -EFAULT; - /* Only progress the sv_vec if everything has done successfully */ - *sc_vec += riscv_v_sc_size; - return 0; + /* Only return the size if everything has done successfully */ + return riscv_v_sc_size; } /* @@ -142,10 +139,20 @@ static long __restore_v_state(struct pt_regs *regs, void __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[] = { + { + .magic = RISCV_V_MAGIC, + .save = &save_v_state, + }, +}; + +const size_t nr_arch_exts = ARRAY_SIZE(arch_ext_list); 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 = &frame->uc.uc_mcontext; struct __riscv_ctx_hdr __user *sc_ext_ptr = &sc->sc_extdesc.hdr; - long err; + struct arch_ext_priv *arch_ext; + long err, i, ext_size; /* sc_regs is structured the same as the start of pt_regs */ err = __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 |= save_fp_state(regs, &sc->sc_fpregs); /* Save the vector state. */ - if ((has_vector() || has_xtheadvector()) && riscv_v_vstate_query(regs)) - err |= save_v_state(regs, (void __user **)&sc_ext_ptr); + for (i = 0; i < nr_arch_exts; i++) { + arch_ext = &arch_ext_list[i]; + if (!arch_ext->save) + continue; + + ext_size = arch_ext->save(regs, sc_ext_ptr + 1); + if (ext_size <= 0) { + err |= ext_size; + } else { + err |= __put_user(arch_ext->magic, &sc_ext_ptr->magic); + err |= __put_user(ext_size, &sc_ext_ptr->size); + sc_ext_ptr = (void *)sc_ext_ptr + ext_size; + } + } /* Write zero to fp-reserved space and check it on restore_sigcontext */ err |= __put_user(0, &sc->sc_extdesc.reserved); /* And put END __riscv_ctx_hdr at the end. */ From patchwork Mon Feb 10 20:26:49 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13969363 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 51F15C021A8 for ; Mon, 10 Feb 2025 21:41:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=PVHDhfJzN+BDHx5jRDst+9YI3wRYIBBBDKfIAkjYYOI=; b=j5KcMZkQFYZ7hs j5voaOKrjacTWfWeFjip3bthxvHMfVazXFsHdbjdLw69Icou8m7v4Jgurl2wf2c32iHLbW4EV+vEL l2k5BF+iuab8/onW7uD3jMmuw3HOGOKZOEwFgVpFOd3NcL+/QOVxtB4AjUDeuzb68OumdPodU1DW9 gh/Bw25lGdB4hcgjFZ3vwQezdVJ0O9UohezpaQVebbAHrYcEh2snQrh5grWDbXYyYkqO0zct4rUMe Ezanaek891ws8aHNvf8YlTcBQnJVGgxFmvLQt3wzOjX9bC6/rUzYI3wz9A8W+aIXphJyy2VyQyMHg EwVU10E6BgtGapuKMG8w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1thbWL-00000001Wkb-0v2R; Mon, 10 Feb 2025 21:40:49 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1thaNL-00000001KP7-3wML for linux-riscv@bombadil.infradead.org; Mon, 10 Feb 2025 20:27:28 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Cc:To:In-Reply-To:References: Message-Id:Content-Transfer-Encoding:Content-Type:MIME-Version:Subject:Date: From:Sender:Reply-To:Content-ID:Content-Description; bh=FZizf8oZeAex4CEwHeMnGWZudkjFQvNzoIsL7CfIy1g=; b=fW4xQ1F6jiLqtXkfzyBc4UxvLJ /DnZMFBQe5gcz5Q1nz1hM2o7UloWI5q6CdnXyo6z2V0eFKchQF4wlglj81/4aQeOSehPvHl9MrWVC DaocQV+GPf0cMGMYRY21UCp/8EQoshmw2LfimL0oWLoFoUG8q8PVCHAsiuVf0uVoeZw85yHFb5K0/ pT1YS/LWXR4L31Y2lqPABIj1F4ZTzYABUwuxfaTnOjDPcNn87e7GVE2JM3t/z4R65+fgNAElmy21t +OHp8ZCIy6A6iflR/OQZ5vBN7zTT9nQ23LI1f8hi/xbaYfyNpXK08LX511N9qmlhrysl9LJmScA3Y Bdlv2Eww==; Received: from mail-pj1-x1031.google.com ([2607:f8b0:4864:20::1031]) by desiato.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1thaNI-00000000Di3-3YuV for linux-riscv@lists.infradead.org; Mon, 10 Feb 2025 20:27:26 +0000 Received: by mail-pj1-x1031.google.com with SMTP id 98e67ed59e1d1-2fa8ada6662so1829177a91.1 for ; Mon, 10 Feb 2025 12:27:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1739219242; x=1739824042; darn=lists.infradead.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=yXMogApdGrBTlPT8pSV6vKYZhOU1YeNlJFg/Jvf0dttKkpygrCu+HUQiMo4DY96moz GdxDiUwJVUeOaGZOGePfrOSAxY74WNp/x/Z0Yugh6o5sVVa3Lt1fVhnG04RFzeKKCzL+ lhymyZEvA7Ujf5EZzwkURwxtCqIxPJhok81t9ggR6TUNVjyHNXizhjzx4wMAlk7V6VVl 618Jh5c1rwBFj6QfFfGvf6XD15liv5oe9N8MhSxTvHF29PteSNqMqN47FmOZMnzhUwLJ Vmkx+X+drTDs1TOo4tLOW1RWM4lmqaDMPR5Ca0zfOr7vXFU1fAYn3G3ftN3mQp2RN4gG Vn6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739219242; x=1739824042; 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=vUz3AmVJl1s7xHpmdinq3Cs3JnvG48qNsmz4CkyweM/gxaJ5j6icogatJ7YN1WewLF h6+mx2LJ/LYbDLDUsXUeppo17q6Oz8NTttPYfI7a5Thksn9wAm9KfjK6uVVHSV/ND1kb mCAWtG97nnlPSmj6qjwCLVkRWlMMW9M3OqOGA46l1q+KNVgtQn2gluEZ+JnlV5AP8fE3 Q83xN3pe5eUrAG9q9SfWo67/HB8oK5Ot4agKyDIirGG7aBlgGv4RhtZ2NKcDJ8aEEtaY 5MII7qHXgEkvGkxql5bkUxYO5qvmAj3tsA+t0eOhfQCwrIxH1cmIGnOfbrJruTWOCWy9 GLGA== X-Forwarded-Encrypted: i=1; AJvYcCX/o/1NtiMEFw3rSsLsTR1FcBdkEy/Ri3mJC2MKnG6t0qE6G2nkkA2NRc3GjmyJTRCNvaUO1r/gQFXm4Q==@lists.infradead.org X-Gm-Message-State: AOJu0Yx1jTvOoeuakIlUuhHZgUamm70OoOk6zxvpkMoq8AHRhmwmuL7G 0XsCf6ZaKvfYDpfTBiXyUmUUTfQBZElIFpP9OmJ/A/HKiz43cXsMqNnR5BbVETY= X-Gm-Gg: ASbGncsdqiutyz7JSAnOPnD5ZLWtm5QQvfBzW9rBlzyZhfjbDDB8ts/n6dDLL+F8Umn SbSHY+KvLnWkJdwPCGryinmlCcEAnt8AgahRk8+QWmgeMAn4TSMthrDhEMLeE3jm+HCrnfeYEJl 7pIO+AVAC1Bzx+uqaVu9V95yBwzvxIaHqrBj9MqeFktlOvZxQszABBpxGtaD/CCSo+lig0wf2di F3mMeF6rkzEy3eSZYEsNX6VupirmUfU9tuG+kU5E0uz/W70SSwJ4E5y0lyR8HZIY4qXaP69EVz8 c8a3QMGxWq/lH8XwZCAAyJWJeQ== X-Google-Smtp-Source: AGHT+IFy5C7Irt5WbWHK+S0kb4JRRtbqEM9TFgu6vGtozVHpZLivrLwQvX+c9KIa+SaKH1vXsI4t/Q== X-Received: by 2002:a17:90b:184d:b0:2ea:7cd5:4ad6 with SMTP id 98e67ed59e1d1-2fa245085a3mr21172352a91.32.1739219242068; Mon, 10 Feb 2025 12:27:22 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21f3687c187sm83711555ad.168.2025.02.10.12.27.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Feb 2025 12:27:21 -0800 (PST) From: Deepak Gupta Date: Mon, 10 Feb 2025 12:26:49 -0800 Subject: [PATCH v10 16/27] riscv/signal: save and restore of shadow stack for signal MIME-Version: 1.0 Message-Id: <20250210-v5_user_cfi_series-v10-16-163dcfa31c60@rivosinc.com> References: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@rivosinc.com> In-Reply-To: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250210_202725_042366_9238CA8B X-CRM114-Status: GOOD ( 25.56 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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/usercfi.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 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_ptr); 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); #define set_indir_lp_lock(task) +#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 */ #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) +struct __sc_riscv_cfi_state { + unsigned long ss_ptr; /* shadow stack pointer */ +}; + #endif /* __ASSEMBLY__ */ #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 @@ /* The Magic number for signal context frame header. */ #define RISCV_V_MAGIC 0x53465457 +#define RISCV_ZICFISS_MAGIC 0x9487 #define END_MAGIC 0x0 /* 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 unsigned long signal_minsigstksz __ro_after_init; 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; #define DEBUG_SIG 0 @@ -140,6 +142,62 @@ static long __restore_v_state(struct pt_regs *regs, void __user *sc_vec) return copy_from_user(current->thread.vstate.datap, datap, riscv_v_vsize); } +static long save_cfiss_state(struct pt_regs *regs, void __user *sc_cfi) +{ + struct __sc_riscv_cfi_state __user *state = sc_cfi; + unsigned long ss_ptr = 0; + long err = 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 token. + * 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 of shadow + * stack must check the token before restore. Since writing to shadow stack with + * address of shadow stack itself is not easily allowed. A restore without 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 exception. + */ + err |= save_user_shstk(current, &ss_ptr); + err |= __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_cfi) +{ + struct __sc_riscv_cfi_state __user *state = sc_cfi; + unsigned long ss_ptr = 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 of shadow + * stack must check the token before restore. Since writing to shadow stack with + * address of shadow stack itself is not easily allowed. A restore without 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 exception. + */ + err = __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[] = { .magic = RISCV_V_MAGIC, .save = &save_v_state, }, + { + .magic = RISCV_ZICFISS_MAGIC, + .save = &save_cfiss_state, + }, }; const size_t nr_arch_exts = ARRAY_SIZE(arch_ext_list); @@ -202,6 +264,12 @@ static long restore_sigcontext(struct pt_regs *regs, err = __restore_v_state(regs, sc_ext_ptr); break; + case RISCV_ZICFISS_MAGIC: + if (!is_shstk_enabled(current) || size != riscv_zicfiss_sc_size) + return -EINVAL; + + err = __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 += riscv_v_sc_size; } + + if (is_shstk_enabled(current)) + total_context_size += 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 = (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 = 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 = sizeof(struct __riscv_ctx_hdr) + sizeof(struct __sc_riscv_v_state) + riscv_v_vsize; + + riscv_zicfiss_sc_size = 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 = shstk_addr; } +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 = enable ? 1 : 0; @@ -164,6 +169,57 @@ static int create_rstor_token(unsigned long ssp, unsigned long *token_addr) return 0; } +/* + * Save user shadow stack pointer on shadow stack itself and return pointer to saved location + * returns -EFAULT if operation was unsuccessful + */ +int save_user_shstk(struct task_struct *tsk, unsigned long *saved_shstk_ptr) +{ + unsigned long ss_ptr = 0; + unsigned long token_loc = 0; + int ret = 0; + + if (saved_shstk_ptr == NULL) + return -EINVAL; + + ss_ptr = get_active_shstk(tsk); + ret = create_rstor_token(ss_ptr, &token_loc); + + if (!ret) { + *saved_shstk_ptr = 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 = 0; + + token = amo_user_shstk((unsigned long __user *)shstk_ptr, 0); + + if (token == -1) + return -EFAULT; + + /* invalid token, return EINVAL */ + if ((token - shstk_ptr) != SHSTK_ENTRY_SIZE) { + pr_info_ratelimited( + "%s[%d]: bad restore token in %s: pc=%p sp=%p, token=%p, shstk_ptr=%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 long size, unsigned long token_offset, bool set_tok) { From patchwork Mon Feb 10 20:26:50 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13969352 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 47CA6C0219E for ; Mon, 10 Feb 2025 20:27:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=74LSszYMAoAE1VDF1AyNhPd2u+JFsMbdW+6TQlA6WKw=; b=Rio/rwCXPsJJ6b c/kNaAfKq2EWLRfFCyEWA7i6pt1zivTJ3WMR/8SaKbyZgTVc1KVaRIxRPBdKrl/w8zcQhcx0gq97q ey60BGXLtgPgx9IeBNwFFimVVb8ftdf8lnqlK+YMR4GDNu56RQufsvw7EeOYLaYsDkL8wHhCN0plY 6BYaiT33itykqpzg9LpuYgeh1MmGPPhRAKxF+sScyWf9PLTijGXMfaA49OdDI6Ihvtk3Uq4Mybdhx 8Uw/4VLA3RW64bDy4HXlbkF9+Ce7t0Bw2Ng6NFQcskxrt/8mAnUcyDgE8dhwK8I0+HTUwCOvgjqqx 6ZfeXk/IqwbspqNRbqCg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1thaNO-00000001KTb-40AO; Mon, 10 Feb 2025 20:27:30 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1thaNN-00000001KRg-41la for linux-riscv@bombadil.infradead.org; Mon, 10 Feb 2025 20:27:30 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Cc:To:In-Reply-To:References: Message-Id:Content-Transfer-Encoding:Content-Type:MIME-Version:Subject:Date: From:Sender:Reply-To:Content-ID:Content-Description; bh=hU6IAlGSwKWKw5iWDrFBqBdwaJZ69/+Cs7+/DXu3do8=; b=DKBEPpLac77E4UwgKgC8MwyCsQ r+9K4ELAHsHkqXc5IYn9ChrrrhKDOnneqYtugh2WJ+mhOZu6PXi67zf+L5pbWrq6IN7cUGmsgBm0T UshIKWutkIQj01OHZpOjuzAqHfT0hDex0X+vAD5Piz4EANPLSiXXOQnXnBu0TIgtXPEC6Aqhz4oaQ t26lIc5vE5uwCm2nuFjjplIV+gIqRAVyQ4BukdWumtqJwydGJ8fN7kxzBF2o7RQu5CiGwEX3h2fts GhkHilg4QIaX2iCSlaf6NLQu7PXtDB1lCYAJEhlqV0hxUswqnHF9rBGo38WrIyDoNDNPZgACXH1cB jWFcR+Ww==; Received: from mail-pl1-x634.google.com ([2607:f8b0:4864:20::634]) by desiato.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1thaNK-00000000Djn-4Acp for linux-riscv@lists.infradead.org; Mon, 10 Feb 2025 20:27:28 +0000 Received: by mail-pl1-x634.google.com with SMTP id d9443c01a7336-21f4af4f9ddso54447995ad.1 for ; Mon, 10 Feb 2025 12:27:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1739219244; x=1739824044; darn=lists.infradead.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=Xy2jRUHrglHotL7PU4h5M5BiACY52IisHZTXH1C47X2oTJHTH5svvABT8FaS//hptv z/QFCsUMY6BXCzCbnfH0fbnaQSaBaVebm3Gbj5vXg+l2Jv6RIyJ+PPFRIdcKjIvNDmtW 1ibSToXh8Huw0e1JS/pGpZZhjLfUEMuVWUDnH5uu/aQ/YJ7NfKZHF3P/FoonXQlFffZ+ 6A7+8TLOKwtmznCQGgHf2kWuftAf/lQhOWl+QqLRzfzJNLz7aUHnt9YrqB7OQpf5ZUj2 hshKbyXNd9P+lxdpoTnBqNe3AGhhOOXIsFO9LTuLeTdQXRZBOWrarmU8QhK7da6cBULN E7GA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739219244; x=1739824044; 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=c2ayXlww9tvEOLvXSKQQMX/dt32YytQ3/r9Avgrl6yLegqe1e9sZ1IkcGYuxotN9UQ MFD3A8nAstP0mKQFC65pq6PljutdZCxeuVrHxOprk1/AYECuk1NMokp+dj9+0LOXmjck P26KQ5mPm0p5X2s9dNl9XNTi9kYlrTC8Ev7+niHxi/Ybux/y5cdYe73Eq+v9rNpnRf27 014iJuFnJ8nDkHWoXuyfEoXiTU1GWvf7rtv1TC78x6lCwsWnqRq0dL1x/vx+TiDIa8Vw DzfHeSy8HRBelVdPkJkkYXcF1RGCSvX18m1t5nujrgvMsKgKhdqMeNP5oN6oo+Q+IdRK FWdQ== X-Forwarded-Encrypted: i=1; AJvYcCUAlE5gI6xA1g7SCUtgQ6fnco/6xF8lYJ4Q5NV0Py2N1+NfrmkWtMO1OovZB8Y0S7dC8ZIzdcYPkRT2Yg==@lists.infradead.org X-Gm-Message-State: AOJu0YyH2dJTw0kBy5cGQa1AwcKND5qkJCyvN7JYMTp5aDuhWF7XTCji zqLmxBwez9M0jPE8gKMYZi79K2W4XwcNZDQ8Q41umrEIIExBMcAzvCYTMxeiZQY= X-Gm-Gg: ASbGncsuhevooFoKu8hZD23YcOUOAvr9vNm21kghh95la45c+nWqBLd4INWUstRMHik BCYAcNp97z/SBzQP+YP/OHxRAgpjvAmma9oIioP/17p4o7CJ8GHpE+6nUjeXpoU+1jMy7TAmu3t oxh1D0bo0oT+9UZaT+Getj3ZFUcx0RMUJL3zwPbWtkHskv6X2f70Ixm17uyZBahW88a1Qx5wHi5 w4Glq6iJfKvigrhC0Kx54Zj4WWgyRICiEY5Trjt9WS6KcLJf6+CsZJYVBCDdYJ7B4PkfjW1aBP4 j25/wGAcVYDUMHHc12W1ksAwSg== X-Google-Smtp-Source: AGHT+IEWx6Qroyd+owNLnhKMgXzpvCrfsJ5gLeMKvyo/Cha3hnXDj8amjvFC6tZrBWudZswu2pIc0A== X-Received: by 2002:a17:90b:2317:b0:2ee:94d1:7a9d with SMTP id 98e67ed59e1d1-2fa243eb4e7mr19748747a91.32.1739219244559; Mon, 10 Feb 2025 12:27:24 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21f3687c187sm83711555ad.168.2025.02.10.12.27.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Feb 2025 12:27:24 -0800 (PST) From: Deepak Gupta Date: Mon, 10 Feb 2025 12:26:50 -0800 Subject: [PATCH v10 17/27] riscv/kernel: update __show_regs to print shadow stack register MIME-Version: 1.0 Message-Id: <20250210-v5_user_cfi_series-v10-17-163dcfa31c60@rivosinc.com> References: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@rivosinc.com> In-Reply-To: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250210_202727_164920_1DE50D0D X-CRM114-Status: GOOD ( 11.06 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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)); pr_cont("status: " REG_FMT " badaddr: " REG_FMT " cause: " REG_FMT "\n", regs->status, regs->badaddr, regs->cause); From patchwork Mon Feb 10 20:26:51 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13969353 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C3A41C02198 for ; Mon, 10 Feb 2025 20:27:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=LAf6MslLH1W1Qudsmw0JxdiT7ib3uJj4Ssg/wObdBvc=; b=uVP6XpFuk7pOyA wYGyu5mNE9NmIzt4IwpwyPKacYprcjDfwGaYNI/u4/RoO0xTzuyO9RGcNrBloz59IuCyTDLQHf+d8 94os9T2nffuBixpThBgPbiryekfcEGEmB/MnDdXsOclJ8JyuggzlVObxQFUFmFai2eDBm/9IAkGhx iUxceq+UOYGVNoj2ropWRx4O1ltT5MZa+DQw6ux4eP9S8MDd2gVeqC9n0OxjcOwja5k6WHpNX7sb7 Nm4sjuJQEGot5NkcnQTe7/n0Yhr8Ym8NsT+k34RnIsXXRlJ5hFXdAibCW1G7N9M9qhROrRmVjmstp Z08Cfy6pttXoWVsIVonw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1thaNR-00000001KXn-32Er; Mon, 10 Feb 2025 20:27:33 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1thaNQ-00000001KV9-1shC for linux-riscv@bombadil.infradead.org; Mon, 10 Feb 2025 20:27:32 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Cc:To:In-Reply-To:References: Message-Id:Content-Transfer-Encoding:Content-Type:MIME-Version:Subject:Date: From:Sender:Reply-To:Content-ID:Content-Description; bh=qihiC//CQppmEvCBgeNt37whYo0mSsmsLXBW9eHBYhI=; b=e/4w/q/MsD7KK8jCGduU2srSfV pIc7FYGfgig9gDyvw/l+RRkJR5/AJosaOht4qiTQvtejYjFD5HyXWsqYxJXsFXK5syipsXr+asWtC rdAHoJ63HOsH/HJ5J3JbDvJPkKxmb8jd0CCVOn5t3i1KIYp8rmj4HmfzAv2uzn5ffk8M1zOS4/3XI nRmO7T21vOLLh6DpaRz00xzPo1o0oUOQhheIfk04DW5NCDsRnJklWXShkfLTzz4LRFcHowcIVAxfw 1KScR/TNd64pHz1Aw3Cwg0jtIK+3iDqPoC5zH/GBLC9/ofG1u94nWJsXbsYIHQMlkiX6YvwGbPd3X AcpSqcpA==; Received: from mail-pl1-x636.google.com ([2607:f8b0:4864:20::636]) by desiato.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1thaNN-00000000Dl7-2MN1 for linux-riscv@lists.infradead.org; Mon, 10 Feb 2025 20:27:31 +0000 Received: by mail-pl1-x636.google.com with SMTP id d9443c01a7336-21f49bd087cso63025545ad.0 for ; Mon, 10 Feb 2025 12:27:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1739219247; x=1739824047; darn=lists.infradead.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=pJQtySPkzPbLwjxql4CpWV1LtdJyt/YGIWJnUYyhpiags/atgxPzunFr+dW1JBsqWo NmGWoQyGmNrWyctFRl7EjMK2tycID5n4hL/y7LihQD1xcYNV6vi7/4pNmhVf/M7yNEhH 8pCISeWWcUUG6r7ax58VsHIPxRCN2sJzZOSf7GzjT5dCBJEF0KzovoIlgs0gYVXcgW2g FtoeJXe5OJMvHuhRvMvoD3HZgg4KjqdVDb+yfNC90v4h6H1tGJk5GjAqX+4HIRoPsOW0 8h+LlRepTQGKFo80RwMs1hptVjZkmd4OFCdmmtSg//WS/SHDsNmZuhzCHSlh9LK/m3xd esIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739219247; x=1739824047; 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=B1fFgZBeohVJmA7IHrNGS9alR0embBDNqzy7YnSOWIGkdYCJJGehiKZdDSr0NmRQw7 0/VSaK5xAIWu5IJMk9o73OhxgaRn8MZtcuyk9IMT6uicePo8xZgWAgQUmSNQbkjUMTvL rUqBCw9/t/aYADPZeZ0QiZMJFYaiaNX6DDdYfWvYRyQYLfkCyhz8oy10EMnYWsaIn8lz 25ZjeFgTzjVidtFFBwmyvX0bK7CDZlgArtFceU3c6C3mClBZzRe+HPLGctXPlSSmr/E5 YLkJiZQSq8RiG4obkjLEBQLDgZtPW/ugS+gigkosQgD7iciOq8fGewU+wlADQa2f0qT5 oqKg== X-Forwarded-Encrypted: i=1; AJvYcCX2V4yD0lQLAjJizxIXLl9wUxk/qroXmMyH3qD0o8+ARLml+/pG+VTEsLxcjG4gH4nUDCfBNvh/S3eFUQ==@lists.infradead.org X-Gm-Message-State: AOJu0Yw/Mg4+h4qgGkUBDtInrJmqpeUcvxvkUlmMEAMFp1ES/ORj3yQr fhPweePkpahueksrlV0YCmR4X2FCNN1cVNLHlbv1Ew5r0Ltc/xMjtbujfGTIR0c= X-Gm-Gg: ASbGncu0IYCGM5qYmSyARem3he2aMq2zVW6rd8Q+EuigELLwi4BbPAnVsL0okTeq1c8 h89kilD0i52AGi2Habs6+B0oYC2k+87YdUfCDixN/3rBfFRYRO1bcXvCss7I2/C8or2FluIoQbV YAz+88aD02QzEK0BzDGyNE+EhF7XVC1ddiOBT7Q2tVW5ZD4fbt6oC7UtXBorvx8baTIQqM0K8FV 576ezNfQ6VKk0yVOHowk8xScKg4IJmIQdZqgb8h9f3oA/svUVd46jdRu153w+wNo8y4AwHtULRq U0jWV5HWR+pYxRO7P9jjF/OulQ== X-Google-Smtp-Source: AGHT+IF7EXANdfW+rLsLNvBS7WrISTRoOO6x3oQIFnuoI1GdkKkKLf31wWdGn3loj99yEU6jyJj2UA== X-Received: by 2002:a17:902:e802:b0:21f:4c8b:c514 with SMTP id d9443c01a7336-21f4e76391amr226903755ad.45.1739219247115; Mon, 10 Feb 2025 12:27:27 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21f3687c187sm83711555ad.168.2025.02.10.12.27.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Feb 2025 12:27:26 -0800 (PST) From: Deepak Gupta Date: Mon, 10 Feb 2025 12:26:51 -0800 Subject: [PATCH v10 18/27] riscv/ptrace: riscv cfi status and state via ptrace and in core files MIME-Version: 1.0 Message-Id: <20250210-v5_user_cfi_series-v10-18-163dcfa31c60@rivosinc.com> References: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@rivosinc.com> In-Reply-To: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250210_202729_751031_41332E85 X-CRM114-Status: GOOD ( 21.39 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org Expose a new register type NT_RISCV_USER_CFI for risc-v cfi status and state. Intentionally both landing pad and shadow stack status and state are rolled into cfi state. Creating two different NT_RISCV_USER_XXX would not be useful and wastage of a note type. Enabling or disabling of feature is not allowed via ptrace set interface. However setting `elp` state or setting shadow stack pointer are allowed via ptrace set interface. It is expected `gdb` might have use to fixup `elp` state or `shadow stack` pointer. Signed-off-by: Deepak Gupta --- arch/riscv/include/uapi/asm/ptrace.h | 18 ++++++++ arch/riscv/kernel/ptrace.c | 83 ++++++++++++++++++++++++++++++++++++ include/uapi/linux/elf.h | 1 + 3 files changed, 102 insertions(+) diff --git a/arch/riscv/include/uapi/asm/ptrace.h b/arch/riscv/include/uapi/asm/ptrace.h index 659ea3af5680..e6571fba8a8a 100644 --- a/arch/riscv/include/uapi/asm/ptrace.h +++ b/arch/riscv/include/uapi/asm/ptrace.h @@ -131,6 +131,24 @@ struct __sc_riscv_cfi_state { unsigned long ss_ptr; /* shadow stack pointer */ }; +struct __cfi_status { + /* indirect branch tracking state */ + __u64 lp_en : 1; + __u64 lp_lock : 1; + __u64 elp_state : 1; + + /* shadow stack status */ + __u64 shstk_en : 1; + __u64 shstk_lock : 1; + + __u64 rsvd : sizeof(__u64) - 5; +}; + +struct user_cfi_state { + struct __cfi_status cfi_status; + __u64 shstk_ptr; +}; + #endif /* __ASSEMBLY__ */ #endif /* _UAPI_ASM_RISCV_PTRACE_H */ diff --git a/arch/riscv/kernel/ptrace.c b/arch/riscv/kernel/ptrace.c index ea67e9fb7a58..df8b7c6ab671 100644 --- a/arch/riscv/kernel/ptrace.c +++ b/arch/riscv/kernel/ptrace.c @@ -19,6 +19,7 @@ #include #include #include +#include enum riscv_regset { REGSET_X, @@ -31,6 +32,9 @@ enum riscv_regset { #ifdef CONFIG_RISCV_ISA_SUPM REGSET_TAGGED_ADDR_CTRL, #endif +#ifdef CONFIG_RISCV_USER_CFI + REGSET_CFI, +#endif }; static int riscv_gpr_get(struct task_struct *target, @@ -184,6 +188,75 @@ static int tagged_addr_ctrl_set(struct task_struct *target, } #endif +#ifdef CONFIG_RISCV_USER_CFI +static int riscv_cfi_get(struct task_struct *target, + const struct user_regset *regset, + struct membuf to) +{ + struct user_cfi_state user_cfi; + struct pt_regs *regs; + + regs = task_pt_regs(target); + + user_cfi.cfi_status.lp_en = is_indir_lp_enabled(target); + user_cfi.cfi_status.lp_lock = is_indir_lp_locked(target); + user_cfi.cfi_status.elp_state = (regs->status & SR_ELP); + + user_cfi.cfi_status.shstk_en = is_shstk_enabled(target); + user_cfi.cfi_status.shstk_lock = is_shstk_locked(target); + user_cfi.shstk_ptr = get_active_shstk(target); + + return membuf_write(&to, &user_cfi, sizeof(user_cfi)); +} + +/* + * Does it make sense to allowing enable / disable of cfi via ptrace? + * Not allowing enable / disable / locking control via ptrace for now. + * Setting shadow stack pointer is allowed. GDB might use it to unwind or + * some other fixup. Similarly gdb might want to suppress elp and may want + * to reset elp state. + */ +static int riscv_cfi_set(struct task_struct *target, + const struct user_regset *regset, + unsigned int pos, unsigned int count, + const void *kbuf, const void __user *ubuf) +{ + int ret; + struct user_cfi_state user_cfi; + struct pt_regs *regs; + + regs = task_pt_regs(target); + + ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &user_cfi, 0, -1); + if (ret) + return ret; + + /* + * Not allowing enabling or locking shadow stack or landing pad + * There is no disabling of shadow stack or landing pad via ptrace + * rsvd field should be set to zero so that if those fields are needed in future + */ + if (user_cfi.cfi_status.lp_en || user_cfi.cfi_status.lp_lock || + user_cfi.cfi_status.shstk_en || user_cfi.cfi_status.shstk_lock || + !user_cfi.cfi_status.rsvd) + return -EINVAL; + + /* If lpad is enabled on target and ptrace requests to set / clear elp, do that */ + if (is_indir_lp_enabled(target)) { + if (user_cfi.cfi_status.elp_state) /* set elp state */ + regs->status |= SR_ELP; + else + regs->status &= ~SR_ELP; /* clear elp state */ + } + + /* If shadow stack enabled on target, set new shadow stack pointer */ + if (is_shstk_enabled(target)) + set_active_shstk(target, user_cfi.shstk_ptr); + + return 0; +} +#endif + static const struct user_regset riscv_user_regset[] = { [REGSET_X] = { .core_note_type = NT_PRSTATUS, @@ -224,6 +297,16 @@ static const struct user_regset riscv_user_regset[] = { .set = tagged_addr_ctrl_set, }, #endif +#ifdef CONFIG_RISCV_USER_CFI + [REGSET_CFI] = { + .core_note_type = NT_RISCV_USER_CFI, + .align = sizeof(__u64), + .n = sizeof(struct user_cfi_state) / sizeof(__u64), + .size = sizeof(__u64), + .regset_get = riscv_cfi_get, + .set = riscv_cfi_set, + }, +#endif }; static const struct user_regset_view riscv_user_native_view = { diff --git a/include/uapi/linux/elf.h b/include/uapi/linux/elf.h index 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 registers */ From patchwork Mon Feb 10 20:26:52 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13969365 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 0136DC0219E for ; Mon, 10 Feb 2025 21:41:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=Q1u0VIQ4NIUXAEjzatnUQcz/1UrmCmOCCedYEOwwYYk=; b=GHy6U8ZdT64VEO /piAIuSqn++UcIdGS/xThLKcDCe2im3/i879q4vzT10O7seD1Lkazd+jZcjPYuiBit4KmsuehwX4A U4+4E0eYeIyX8o2zV9KMv/0e9ZHeAbZHitAPioa7XFsP9KznFFn+4TRVbVJ0VX/Rk/7Lf3YGhz/r+ 37WahwVuIiMKfeoJkKV6QpKht+cCVhlcQXkm6jUHZMsCDh7iHFePWHu0foX/7RasToJ4e8oV2sxTK eUeDt7zj1b7da6IPOkzEjcm5pAdol/ETzZdiz4mALsf+MBiP6GT4xbJQjvN/OYs5Tboo6FTQQpAno Y4PQsq4AbzM+HCokmFDQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1thbWL-00000001Wkt-3eyQ; Mon, 10 Feb 2025 21:40:49 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1thaNS-00000001KYJ-2pp4 for linux-riscv@bombadil.infradead.org; Mon, 10 Feb 2025 20:27:34 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Cc:To:In-Reply-To:References: Message-Id:Content-Transfer-Encoding:Content-Type:MIME-Version:Subject:Date: From:Sender:Reply-To:Content-ID:Content-Description; bh=BEoq6zqSeLFMaXOoiuM7yp6ZSaKHVlNR4X3PvApIHRU=; b=U9Iscp55QxwoaGQoYEtYflo5i8 bCLiESkfdZEQqHVFf2d5/6jPiVP3UyWzyjnoJGx6zxRzvbFeA7oo42OoqKnxuU6BGWuSdf8ab4ykq b3gqeGfKT3TFPT+3FdH3xMMQfB8T2K7e3b2tKUMh0R/+cXCNQV05nf1D7CDxUZ4tR4C39dyEFAVuQ IT15ZnBxNSh0JHedxu02pU/zQ4D8kdnA6YwxGbR8Ew/EVUJVJXEgOgU4Hzo3skfz4mojgnrz5t+Sj TPVnn6FXhL1jyMGNMuAW71UhJSLrwo5ZZ/qWPFT47fHB90T4zLfz6eVPx4AHXKBu92RnR/O5/17DD u5rag+SA==; Received: from mail-pl1-x62d.google.com ([2607:f8b0:4864:20::62d]) by desiato.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1thaNP-00000000DmL-3x4r for linux-riscv@lists.infradead.org; Mon, 10 Feb 2025 20:27:33 +0000 Received: by mail-pl1-x62d.google.com with SMTP id d9443c01a7336-21f3e2b4eceso90262195ad.2 for ; Mon, 10 Feb 2025 12:27:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1739219249; x=1739824049; darn=lists.infradead.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=sCyHtiszLJfI83xQxTXu31CX5u+1s5TvtCDS7W9FfFnDC0aK1Uswchd3XBvyHuaj1U Sm57WgQMwjrZj1zCQGGYCNjq6Vj/ebMl5AOvK6gEbfMtn11UirFgBVNsTgJH6pnG9oyr 5DrmdDzBTzlRc8R01rXNUPkVyjLbdR8WQoKS3eorHIVsWr5NxnvtZrksZzc7D2keqE7j uKUevDhMf9Ggvhy8QzTUpuc2/adFMbQhCDU9ylyes+K25+RuLZpVqwoHTXwT9UPzmICj iKR6od+Os1nR6KlEdYAwQKqWbfRgb+gXZYbj7j5oNlUBvnFidJLUbDH4OCZI0DQCb6Jg 0lSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739219249; x=1739824049; 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=bD8I8sw8BarscARWgi71jFOXL2ZQ14CufaAqZQS0ttHBVygJ3ey8NmczkLU8tsPcFz 3RzgEYNkBs7Z6fKCA5D+n/MsGdi+wp+z2Oo9B5r3e1I1IE3AUgMlVWtNkx02dSrNx0ka YLWBEMtPOn1zmtT3FPwaxLOU0R/C+Bx1iTATKNIPIiuu1hpGu2bVOj/yJRfmsd3JYPKn bomt3SHiyLC6EE908EEiLHZ8Gofy5DX2/CngVnUU0UL0qjxjU/MjMniYjAXgKl3MAfft sdbhb5NF2b2qm/HMvaSaWwSpMv3G0MuXdISRn6ZvkjrUrdJ+MxtVxxkxf5uLymCHVzkI iO3w== X-Forwarded-Encrypted: i=1; AJvYcCVE7O7vmTmRk9mnkLln2gQY1+dOR4VJZSH5Wh8+Li2ZNmfprgk+rVpw8nzJ+2sv7KPZMHafPnEwBgMkPQ==@lists.infradead.org X-Gm-Message-State: AOJu0YyxS0f/M9XqOWGW+xgPDnl95V0dGxhlPl2210tXpD29P0EnuLhK Z9r+LWQcktEmaEVlQl/GJWLVc4OCL/1F6ZhKs3VRrREB3xkyq3sh4D3gH1BJxq8= X-Gm-Gg: ASbGncu8OXAGhW7fJXd8bBICY8q5qO/OyGD+AY50WTpj6+DYvIvkGp3gLmmn4Dteu18 me9xSL/st3KHq63HBL0WzQNtMJYxt8mxx5wuj2ioODMz99uZ66PkXtWOyquCXtGe4TCSa3kTECp VA01HjiNWBvDAS0+ZmsJGvVYY9cwVvqn9HmkGVF9qZYHPZrdu0YCgdBMMEftIajeU7D84k9tuvf pZYKzXUA1n/zdZ/AZrW+gGqbgBybY9TvAVdsi4bC0eQOURAjaJWCvyXcMiIb4M+FKey5OXnmFW5 3ZUvCEoS6t+eyfEU2E+jddRnpw== X-Google-Smtp-Source: AGHT+IEt5F3RjsoBA/4cHXJN1Ltv5WX03R8vCEuQgHxCW+5M2vSetK20AA29/O7CLlddL+JvfC+2OA== X-Received: by 2002:a17:902:d484:b0:21f:617a:f1b4 with SMTP id d9443c01a7336-21f617b009dmr211020025ad.45.1739219249583; Mon, 10 Feb 2025 12:27:29 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21f3687c187sm83711555ad.168.2025.02.10.12.27.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Feb 2025 12:27:29 -0800 (PST) From: Deepak Gupta Date: Mon, 10 Feb 2025 12:26:52 -0800 Subject: [PATCH v10 19/27] riscv/hwprobe: zicfilp / zicfiss enumeration in hwprobe MIME-Version: 1.0 Message-Id: <20250210-v5_user_cfi_series-v10-19-163dcfa31c60@rivosinc.com> References: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@rivosinc.com> In-Reply-To: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250210_202732_063882_4059CF6A X-CRM114-Status: GOOD ( 10.04 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org Adding enumeration of zicfilp and zicfiss extensions in hwprobe syscall. Signed-off-by: Deepak Gupta --- 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/uapi/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_hwprobe.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); From patchwork Mon Feb 10 20:26:53 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13969358 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 0FD1CC021A2 for ; Mon, 10 Feb 2025 21:40:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=yMOrut6R4TAN79yO8IxtUtFdOzZL3mTEbv1n5iblEg8=; b=YbBAZJd6pstmb5 2gGSP6VJCJVY3zhmt6Upvo1SmejSR2BzhXFZ5IxJYKwBp0H1gPf0s9ynsyvqpxS8Wf8uWCJ0jlwBp QDJvpKddlkTEvbjoHA2oHpnVQ8NHOvC+4VWy5tCiDBNmPU6CVJdknW18IXPWqdJzbqcE69Nhh1T/v 77j59lP0nJWVb419H4MJ1lfn4oVlEK9WOPNQuC7RW6v9XvlzpUrdy8XigVZ494TBo0AfOpzv3Zyyl hsCsbI7jU/hZO6pUjEOUZWLQ2l1e6FPEihDOXtZkcXXX/+1rUW0/fFq7AARITM4eXE6oOxsCgoSpB P0w0NREpT3ytYT6tlaZg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1thbWN-00000001WmB-0lKG; Mon, 10 Feb 2025 21:40:51 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1thaNU-00000001KaE-0few for linux-riscv@bombadil.infradead.org; Mon, 10 Feb 2025 20:27:36 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Cc:To:In-Reply-To:References: Message-Id:Content-Transfer-Encoding:Content-Type:MIME-Version:Subject:Date: From:Sender:Reply-To:Content-ID:Content-Description; bh=a5uI84SXblQRDID2BHNWhhHsRc7xVUDdoiei4kS9c/0=; b=CvXL9fdfuJjjiIYjYoWFS2ZL84 HHfl43TiJ48MYcj2iqNPPFSwyebQrmg/obgkDXreHUBcmV9o3AJrk1+hxEvv6j+bAeiECeNzDZqGp f+17ZNxFqKdhJ4nJMDD+mk1Rh9DnrapDo1Ff5D5t9sGkRZwrjZRW4BA32OJ9N+4LViXypRBrPTaaX Sqquh83bkAaAzk4M2sz6Ru6MSnZqC7gTrLnu0dbvxyEkDqsXf2skTod4J5RhpUl21qxek6fNkBYq0 37Zuxrh4TfR52/Sz/dJJEqgoNWC31p0TbGXF5Ctpppyef93eU17Vp1rn0Kc3ETA/T+d4l1bEXc/XC KfpYNs1g==; Received: from mail-pl1-x634.google.com ([2607:f8b0:4864:20::634]) by desiato.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1thaNR-00000000Dnf-1Z26 for linux-riscv@lists.infradead.org; Mon, 10 Feb 2025 20:27:35 +0000 Received: by mail-pl1-x634.google.com with SMTP id d9443c01a7336-21f48ebaadfso79070705ad.2 for ; Mon, 10 Feb 2025 12:27:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1739219252; x=1739824052; darn=lists.infradead.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=ufnd/ccd7FJUXgTuwS94eD9ALcRWU2GVnfswn3jVydKsLeqUpco+fIXojIH8gSnj9S I60n2jaimEZZ7ReFtC1o1U568BBeWrziRYYJBd2mwOQ6thHExqO7K+xZMqoyH80OOCMQ HaiIj43AyvMB7GL0fQwE3s5xID+OW6Z4P4t7/kNXbdHOjBqfoMtg2etJgTu46OvM0h8C QTB9On9o8vv0+ot8kNgt2EQcznmVYFzSwmvNRJUg1smfZhQd7vcTMBevFuoO9DmBBo+Y 9z5nusvs/Hd4n4Ay2pnA2IO6NSzW0il7XN8CgPbPgA/ytdqmfpXaeDKJN7GXZFWdDvA7 x7zQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739219252; x=1739824052; 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=Fdr+hyByG4N1q/ZJ6zpVtFIG8xVGj4DAZKCufkNSvuJx2BAKq9f1w4RIEPG77Q42A+ AI1kLipYxIGPZ7MKXAueT2cBPJqYFhawpFr1DglrH4R+VM0YDXXSOvoWg+j1pFvKIHTW h6sN0qigjW0NFnOSeR28aVrU5YPM7NDb/YO/WiticfFGIV17zENRA+uraH0cPYQKvyTM CyGTw2U/OIEotTw/F+YMA5M1HJNQUQNKCU/b9D+/BDrsKEOJo2utQV3bZ3tK/IB5Kbz2 vhdxiOzkBWtzJLBaR/7ULW8qMlWBRBQM5qJHuMFnw+U3Ym+YxibKarn2raVZGnsGlZfM zmOg== X-Forwarded-Encrypted: i=1; AJvYcCUqC4sFrwy3Vg26obekC+/qgcvkW2WAKwGtd8ObuGDW6rWczrvznBboCw32ZQDhR8vlKJEAoMVvVEyeSA==@lists.infradead.org X-Gm-Message-State: AOJu0YwzLx5WVqQNPk6YhE8LT3Sutm4+0oghiemr+eDojEDy8FqpipQv BV3SvZixU+FAaOGfhc8ULl9meXhsT+6xlPIzzRgdYtD+iQCUyPBqRgOuo4pENCw= X-Gm-Gg: ASbGnctaORxIc6cEllWD0XQOnuaKe0EbRhWXagi+QdciZNfSPXOQPWR7zJGq7RfApz7 ZuFwKj3sOvF7BrOUy0eWBu1R5J9LTOVpU/46zfCPMWWx4qIGeBRd74IwSGZO6CqRjd3CLSzuam0 o+qJqnD2D9pqTMUjs3tR/LlseYxZFs1TiywMO1FlpACVX6dpgA6gbYYlmbcWFdq18uwThjSYJgN 3NDBaTJB9D1+DZHjKnoBbGrhu7ozFu0rQgyNz/16qsR7BHhUanI+apCYurc/BzSa/66DTCRtnAA aSutj6DGhbDEDpVUbogRAHrQoQ== X-Google-Smtp-Source: AGHT+IGRSFFgWaceOiIiNm5tv+e7IogqAyyOZF4Jzr3uh0XSSHp0Pwy9ipwzTE00hP4VjDiSKq3vug== X-Received: by 2002:a17:903:32c7:b0:216:39fa:5cb4 with SMTP id d9443c01a7336-21f4e6ce13bmr265561195ad.25.1739219251992; Mon, 10 Feb 2025 12:27:31 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21f3687c187sm83711555ad.168.2025.02.10.12.27.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Feb 2025 12:27:31 -0800 (PST) From: Deepak Gupta Date: Mon, 10 Feb 2025 12:26:53 -0800 Subject: [PATCH v10 20/27] riscv: Add Firmware Feature SBI extensions definitions MIME-Version: 1.0 Message-Id: <20250210-v5_user_cfi_series-v10-20-163dcfa31c60@rivosinc.com> References: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@rivosinc.com> In-Reply-To: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250210_202733_535293_38173B2C X-CRM114-Status: UNSURE ( 9.85 ) X-CRM114-Notice: Please train this message. X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org From: Clément Léger Add necessary SBI definitions to use the FWFT extension. Signed-off-by: Clément Léger --- 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 = 0x4442434E, SBI_EXT_STA = 0x535441, SBI_EXT_NACL = 0x4E41434C, + SBI_EXT_FWFT = 0x46574654, /* Experimentals extensions must lie within this range */ SBI_EXT_EXPERIMENTAL_START = 0x08000000, @@ -401,6 +402,31 @@ enum sbi_ext_nacl_feature { #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 = 0x0, + SBI_FWFT_LANDING_PAD = 0x1, + SBI_FWFT_SHADOW_STACK = 0x2, + SBI_FWFT_DOUBLE_TRAP = 0x3, + SBI_FWFT_PTE_AD_HW_UPDATING = 0x4, + SBI_FWFT_LOCAL_RESERVED_START = 0x5, + SBI_FWFT_LOCAL_RESERVED_END = 0x3fffffff, + SBI_FWFT_LOCAL_PLATFORM_START = 0x40000000, + SBI_FWFT_LOCAL_PLATFORM_END = 0x7fffffff, + + SBI_FWFT_GLOBAL_RESERVED_START = 0x80000000, + SBI_FWFT_GLOBAL_RESERVED_END = 0xbfffffff, + SBI_FWFT_GLOBAL_PLATFORM_START = 0xc0000000, + SBI_FWFT_GLOBAL_PLATFORM_END = 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) /* SBI spec version fields */ #define SBI_SPEC_VERSION_DEFAULT 0x1 From patchwork Mon Feb 10 20:26:54 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13969357 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 0FE55C0219E for ; Mon, 10 Feb 2025 21:40:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=JyKdW/ZXsRTBe1RQ4y0DAX/QqCOyoDEFe+L02jBTanY=; b=vMS3uvSqGfhdON goKMMo21T8KgNdgVmRwY978IjnbFDeTCJn2sBMlVVvo+0LdapU3fMZd54+0Jy4QARnXp3j1xoZ6+B Zf+xg4t3loKe8ZABsa6qRtqKWP6hd9/45fr8RHZnsmXPk38h7q0Vu9mkMN/BDFJzyE8XWkgqoFn0N XPLP6X6W/FSFbuQ16TUowIZPvRHKO78PVNdp2F+glEeidt5UWN5w/AETRlbONkaFm1+88FB4tMMaG pG+GH8XTqYQGSCk8KeuSV3jVb9/E6qk0+V1gOefaMqYJeJR9nIBFoghDJ8DsHmc42nDhq/h8r/r8s 5Xp/XD7COFH4VpF2n9uQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1thbWM-00000001Wll-2A5u; Mon, 10 Feb 2025 21:40:50 +0000 Received: from mail-pl1-x630.google.com ([2607:f8b0:4864:20::630]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1thaNT-00000001KZF-067S for linux-riscv@lists.infradead.org; Mon, 10 Feb 2025 20:27:36 +0000 Received: by mail-pl1-x630.google.com with SMTP id d9443c01a7336-21f49837d36so53937275ad.3 for ; Mon, 10 Feb 2025 12:27:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1739219254; x=1739824054; darn=lists.infradead.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=z2+LwrXuP9C4znd3GaA+RqiaU1LNnMFJ9BmbdoA9UW4Ay7neawKnRadV2Q3IwQqHEf NGGHnU7umUBHZhUOVbjkRVKsU86CGRB6QOPFZHoaFWTOA195+dGeBg5eEKQCdChyDMUW /kEwgOhzy6fHDzL70eRtgRma7J5UnIbZnjqF1S+SJf3Rvo5N5pdsTzTc7oywFNTxEqio 1sfmQItWDvTYh4Ua0EZqjwpp+Vq0TwM7vFjVjMSyFl5WlxgOR61LPPDMQh8044oHIyBG VqoyzQoB0mwTbZTWxXHC2o+hFc5w1ce+P7p0xSeyAbxaqcvuwPTtcTKeT31MYODFR78d JPkw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739219254; x=1739824054; 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=xDAJhcsA1Scl/l9XbeXE71en/RJj51bDfqQX8NygGBoOgkv2EZo4NfR8H306JRXf9f Ya4sVaOmeQzP6/7vhG9l2jck7+TnFXlV3SVr6BhQeps0l744DJNAQ8v6mGRvPgPXlSi3 sz45quPIfsoWRIUPlxcXDdAqxdULd6iXvXwyy9EyEQNGMwUcRaEmm+23RqsPBw/rHhqq 2ScwvPjJeXNc6Pqf+YgmJqcswv04mdj8PyIYo2B5y4CY6YiRPRSdKoeos8TRVGOrSSzp yQ7aZj5zhfCvk5PWW6AH1KKbsvc/xAMbNSWywpPcKOrLD5Ay5U5EXnQjtLc09OJmGCqm Vlqg== X-Forwarded-Encrypted: i=1; AJvYcCW9Bz3QNNqBQeWhRHRnIliPQ44wo+Iv5YT0F4stQ6/Oe+UdaOWPC1+vQyJnD++4Q4rIHcqwXTa9IGTIUg==@lists.infradead.org X-Gm-Message-State: AOJu0YzFqpvHrvyGe9LTx2i5r8MWzIk/195Lp3VMqtUtKj//QRMYFOTj jgE4e3D9xjyl8+rr95x1zZUz97Chbyic64mvQqZrZu+F7yobpK8h9i3HEvtwgvk= X-Gm-Gg: ASbGncv0eSMhwm/AtY7cT2J2zt8Bj1gtsMc2ykEI0RZ0e2l8pkHrZasLAXqm8rbwzN/ mfv2f4OtTccN9j7FOEBUM5cKDTr7C2s4S0ZlbidQhvJsNvIVxOmUqTMvXrHE/kSYGgl5230FcD2 mbfflOOykY3KmN7UPcVqbRdbhfLe35Q5giC0Lo8p7lYMNHRbKowzPS1eKEgrCIN914KLr3TwGbr NgSaOXaFBB8ICk1qotcSa8ViIT5QnipEr2VXlo1zto5Dd8r4OaMnFy4Xio+D8BxAHVVMfyHtLek 6+5QlapYsyt++jzB8L6TK65XRw== X-Google-Smtp-Source: AGHT+IFdaxFBkgdagf7y3zqU2s2u/Xx2zJIy3i8Z2j6IzHAvWKa52jNah2tWLGP7hmgNkKb4ANZukA== X-Received: by 2002:a17:903:230e:b0:21f:7671:a45f with SMTP id d9443c01a7336-21f7671a6c6mr168719375ad.28.1739219254463; Mon, 10 Feb 2025 12:27:34 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21f3687c187sm83711555ad.168.2025.02.10.12.27.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Feb 2025 12:27:34 -0800 (PST) From: Deepak Gupta Date: Mon, 10 Feb 2025 12:26:54 -0800 Subject: [PATCH v10 21/27] riscv: enable kernel access to shadow stack memory via FWFT sbi call MIME-Version: 1.0 Message-Id: <20250210-v5_user_cfi_series-v10-21-163dcfa31c60@rivosinc.com> References: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@rivosinc.com> In-Reply-To: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250210_122735_070597_720B16D3 X-CRM114-Status: GOOD ( 10.63 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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 --- 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-offsets.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 #ifdef CONFIG_KASAN From patchwork Mon Feb 10 20:26:55 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13969356 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 1C0D2C021A4 for ; Mon, 10 Feb 2025 21:40:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=swzmXRjzrZahvaKS38+2rZOE8C+vtm45seRzh2II8UQ=; b=Ey6+vjHI9Wxw4U lXGDUOF+shM7IlbWqyTflmXoHyRoDbyoLpd+eexiS2Tr/73EjjPHz2DsdSqhNhFU6708q8AMi8S94 55T8D5SZSn+X7lan3MN8Wkmf6AQYk4z48Ds8umS9RVeGax7kudINRTF0m/gZRCFmBzQDktIXPM/2O 3BNkFWwuZ31M6xUb3pbKHhh6icdJKefE91FV1+xSNAwecJxmmCAVR5QKyCzHUMmpdH1rzBBbBB8aG F9ZjHQg0Cgv+jGmoC4EEdLDZiJl1qG53vXfIH7W+a+70Y6ccBcu215ak0xmQKPHXpcPCedWbogj9x PcS6xCJhqlPn/h0w+urw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1thbWO-00000001Wn1-1szn; Mon, 10 Feb 2025 21:40:52 +0000 Received: from mail-pl1-x636.google.com ([2607:f8b0:4864:20::636]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1thaNV-00000001Kd6-2BYC for linux-riscv@lists.infradead.org; Mon, 10 Feb 2025 20:27:39 +0000 Received: by mail-pl1-x636.google.com with SMTP id d9443c01a7336-21f7f03d7c0so39216655ad.3 for ; Mon, 10 Feb 2025 12:27:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1739219257; x=1739824057; darn=lists.infradead.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=bjlTo2yAF/YkWtBofZITL93pIQfmGV2Uj2yO+h6zli6ewrh+bI6ITAalJ6/picOGj+ hc8R8u7jfNR9dxPIfdgwWKZTFkbGGVblGEkf/DQ9Zfylx/oG9DLKnui6VXLJuiB8hQVG O0qPqu9K7cEWMa8jbFvd84Si/KMhD5GAJq5C7Fe96MHDllVo6/lxFx/wdYrButtxeGN2 Mvx0mOyba90KfTde7STrV+LDb9Ghj6WiSnZ+DBZK92kjrxevDOCmPHCL/y/0Wz+qsECg apYquUeR9RBkK5j+PPTcpok0T6Vv66G0n8REhSbloK5/cOOgdQ8KJALin+qW7Hgl/E6D ffOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739219257; x=1739824057; 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=rD9VQWb4Z3EBHD0/Lq5J5MEZM6RZc4VQIlBj0ayGbVV8dTpAGo6gnUyR7ZqjV4oo5x nFbnXG8Ux/efnhgw9J0yfJRodY3If+WErUl+Rw821Zic4TubtI4rjQopng1SejmOW222 2z+aLEYy0iMmqHv6jUmyBaye7pww3TBe60K51PpkCN6LsmgrSjXwLa0w9pDtD6yGTrNf HQ9mgbe4/MN5PuPzTHzM6tCvuDvL8s/krrqMvxUaNdjjkCTRexVuPG89HZDVX9M4yQIW qk14OI/+ovzqFCkMGBkqI2VLLZQZiUZK9to54ynW3ObSEfU8DLFk0rl6BIDkIA18naU9 c+7A== X-Forwarded-Encrypted: i=1; AJvYcCXlMzskJHAYRjiLJhTDOg+redrncjDYr1Lo+5+MFS3l8Z1GVdfiNry+Rx0clkA2ET24ZklA0w7Mwktdog==@lists.infradead.org X-Gm-Message-State: AOJu0YxI49qQ9RZF24yNGyd3UDB4s2QvZHMkIBf8O3+PEdKZxgXklQoT fxBaY4BRdMOT2RonWvGY9KvJ3WBjwWOuDTmzSv2iuLzHrBJT3DgZnzneti9UgLY= X-Gm-Gg: ASbGncubcCnewoxXxK8uY7+8bQkK2nu0HOcQWANubfOSGNhJm7PzudBrRVMnv7gTE7u XK6w1YHZjh1PgZs0Vs2LK8I9zUiws/UTvCJtm4dghKtO7ptDbA1ndq4Cntm+lMxdTwqE48140fR IWwVrzS8HBH1N27fzaC9BdmjBsZDvgCb+WDEnc+uXCOaWCyeYSYYXWPzmSyCxj/hY9bG8ePV5CY C4ZZXoJQoFrnW5rQEtc6sQiVe3TMhtJDUU55poXIwIr3AI0gfHimxC0kpQhCUnSCKAe/fR3bwJK 9CpubqYIWH+d31K2sph8Fz7tQg== X-Google-Smtp-Source: AGHT+IF/dAuKVSsF4xfi2H6sG4wlPlN+BXXOHG9nKEFkchqxVssSaBrEBlASEgV9299c+TKy14VoKQ== X-Received: by 2002:a17:902:c94c:b0:216:1543:195e with SMTP id d9443c01a7336-21f4e1c8978mr260581855ad.5.1739219256955; Mon, 10 Feb 2025 12:27:36 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21f3687c187sm83711555ad.168.2025.02.10.12.27.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Feb 2025 12:27:36 -0800 (PST) From: Deepak Gupta Date: Mon, 10 Feb 2025 12:26:55 -0800 Subject: [PATCH v10 22/27] riscv: kernel command line option to opt out of user cfi MIME-Version: 1.0 Message-Id: <20250210-v5_user_cfi_series-v10-22-163dcfa31c60@rivosinc.com> References: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@rivosinc.com> In-Reply-To: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250210_122737_597704_30CED8BF X-CRM114-Status: GOOD ( 13.51 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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 +bool disable_riscv_usercfi; + #define SHSTK_ENTRY_SIZE sizeof(void *) 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 = 0, addr = 0; bool enable_shstk = false; + if (disable_riscv_usercfi) + return 0; + if (!cpu_supports_shadow_stack()) return -EINVAL; @@ -469,6 +474,9 @@ int arch_set_indir_br_lp_status(struct task_struct *t, unsigned long status) { bool enable_indir_lp = false; + if (disable_riscv_usercfi) + return 0; + if (!cpu_supports_indirect_br_lp_instr()) return -EINVAL; @@ -501,3 +509,16 @@ int arch_lock_indir_br_lp_status(struct task_struct *task, return 0; } + +static int __init setup_global_riscv_enable(char *str) +{ + if (strcmp(str, "true") == 0) + disable_riscv_usercfi = true; + + pr_info("Setting riscv usercfi to be %s\n", + (disable_riscv_usercfi ? "disabled" : "enabled")); + + return 1; +} + +__setup("disable_riscv_usercfi=", setup_global_riscv_enable); From patchwork Mon Feb 10 20:26:56 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13969360 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 6B532C021A6 for ; Mon, 10 Feb 2025 21:40:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=PWFWiUKprN4/iE3HWdG/WPJoDPPsydFwNSwXepalu8M=; b=uWYChCX+hvDZY+ D2m3pFe6W7ycNkvQDCrXKOgIb4d0JlEEbe9SaCJlVSdCy7KwqH0UwP0oUEo1qDj7IQ1DnXt5pr8dx SG9Akm2Y6soybNpz4hpTB3LKVxVpSMerEufgtUB4nVJxwy8JemMdHLY0KCltzWG81u8UM4CuxQbUS hUD5jI9QlPPSg6fXIkxizCGv58sYay/gNEF9XtIeSlWxaWhWVir7eF+LauL5i2fCv0lJ+rcdWsdAu oEMbg6QmWu9rZP9ML/KAsmeqpwPCECPjGkQzxKnQ9loGd8u2/E5zj3xZT9a0CBhHIEYkCavRp+eNi ADM8JiE5kJNek9ISsiCg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1thbWP-00000001WnU-0NSM; Mon, 10 Feb 2025 21:40:53 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1thaNc-00000001KlG-2z86 for linux-riscv@bombadil.infradead.org; Mon, 10 Feb 2025 20:27:44 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Cc:To:In-Reply-To:References: Message-Id:Content-Transfer-Encoding:Content-Type:MIME-Version:Subject:Date: From:Sender:Reply-To:Content-ID:Content-Description; bh=r7taaI3D184cw5jK643mXVB9VjzO+GYqeON7OFr07g0=; b=ngToso/QYYr1REYyuCKZD8u/fH uc933fBe+ClGSWuMhAc1lxTygPiGphA91e3yn2S5CahW1Vz+DPaOFnagz6lerpCt7UviM/bcp004a lnD6/jiTimpriJW4i++bCEcCIdROGvO8HhSwcFjr0CXGgs2XaIaiSLSKSE+DQimLiZVsyS6aQfa7P j1MdWQlz3ENar99FccvwTlBkRcRWIcq06GavSbplTspmrOMjtalHuj6L5i0fu3+QPjL8wXDS7lklZ cI48U+VWuoWhPOOp+gzTOrupSFu4FvRWHiIz1Zbuy4L2tZ5aiEIIPHQe/JUJi/MgYxPRRyYjqq/0I AIN30ICA==; Received: from mail-pl1-x634.google.com ([2607:f8b0:4864:20::634]) by desiato.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1thaNY-00000000DrS-3Yyr for linux-riscv@lists.infradead.org; Mon, 10 Feb 2025 20:27:43 +0000 Received: by mail-pl1-x634.google.com with SMTP id d9443c01a7336-21f3e2b4eceso90265345ad.2 for ; Mon, 10 Feb 2025 12:27:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1739219259; x=1739824059; darn=lists.infradead.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=r7taaI3D184cw5jK643mXVB9VjzO+GYqeON7OFr07g0=; b=qVu0KB0811ebdUVcm+r3GoUfhs8JgzciUM6awlFpYVqnJ5fyNDX6pK/yb2S2bEJl0S pS0GbVhx8uqtwc+UkxxQBhSBwTMoJKZ4iBP83ajGzLjtvNZNaa4L9nODBeIhCBV2BJ7x 0eFyDwdksKyIOMe67nzBWrIjVwObzyL8mmW9esV4y6gTRciPtdw5w0BujhQMlJGXI+tl jN3hfeL04GOVrIFpvcdQGgEVrbAsNyidhifELQ8jsDB7Nraqz3EAJzmH4N+2mMdEViKl MtNeILEn10SxJ9SuLpb85+E3LhGwYkKukwu9fnS8vMuWJe+1Ll5BoaKUEjPPDu8frmYL ccdw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739219259; x=1739824059; 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=r7taaI3D184cw5jK643mXVB9VjzO+GYqeON7OFr07g0=; b=IHwbTtIvYHIeQ1VUh0zRwQFA4KasqwJJN299LpmdJRrxGDuoHym9ER0mwIfGc6ZUrt R7kTwnMQB5VfT1T1a/5wj9G/MInhO/2wmwNf85+xKWibiy7b3rEP3xD+aRfwk8RzthSX ph9KDyzEvkKu7A9esVp3h0gSslA/KdwAlzn2q9BFEJ/b9hsjl1tgc4D40ivIIP6w8OPD HATVrWM+UP0uKiSApWabHcxtLKZ1L5qP8ypvHwajPtGnEtBZGxUx04w1Gth1EfPcWc5+ R7qJavoFdIzAdiTKhaV8tg+uGeDlsXPm09g2f0MxYXiDxuw6tdbxkSUpQ59aZBf3KTmh DhTA== X-Forwarded-Encrypted: i=1; AJvYcCVVwDJQ5+T09sKuv8MCD7Qo6HkG3pYpxaiImJyZdgD9K8qk0pw/JPcxNeHQ7R0aQIklpNDP/89r1jTrgw==@lists.infradead.org X-Gm-Message-State: AOJu0YyQ33rd4vAzt03hLULh17kqB07z+XGQtAbnIiY8HTqH8aaSlcNA 7Nv0f0ouy9winu0/ezJ35+svOgz1B5THVTT9Lml+e5SuWJhpNBPhAQ00ZuIo5hE= X-Gm-Gg: ASbGncvoWuERGjic0tdOmXQN79rk0vhilMvDBe5b0eOfLKT8PdaQYxy41mMlvL3Kl9L vZ95BoXi54PbeLlwZ/ogG5FXv/buThuFJEP7yww33UxLuqdbySPRaQXtRyqLrAMuIVcR8H+F5PM 5NmTonUfyFdZvIUm8qskVe8YWFRsjweVVvxNOB1WGilqOTlt0SIla1vY4BT8Ktct0lNaJLghnje XvWQoH8WbLFEgBKjxU0Qt/6oRNIe2VuAV1hlR+a9vXrtXEAo/7oIZYT6Wy8cLp8+aePT0eL5lH0 jB53zSoG4ObhEG2dcLeVeQVFVQ== X-Google-Smtp-Source: AGHT+IG7kVqNapt/vUbJRwHXJK1nO6NkoyLZ2LO+pf9PsrB4j+p9Vq3YwvD6dU3hxfVAOMr3upzlgA== X-Received: by 2002:a17:902:e548:b0:21f:5df6:b259 with SMTP id d9443c01a7336-21f5df6b45bmr209610785ad.21.1739219259424; Mon, 10 Feb 2025 12:27:39 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21f3687c187sm83711555ad.168.2025.02.10.12.27.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Feb 2025 12:27:39 -0800 (PST) From: Deepak Gupta Date: Mon, 10 Feb 2025 12:26:56 -0800 Subject: [PATCH v10 23/27] arch/riscv: compile vdso with landing pad MIME-Version: 1.0 Message-Id: <20250210-v5_user_cfi_series-v10-23-163dcfa31c60@rivosinc.com> References: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@rivosinc.com> In-Reply-To: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250210_202741_037547_44A1380B X-CRM114-Status: GOOD ( 15.85 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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 --- arch/riscv/Makefile | 7 +++++- arch/riscv/include/asm/assembler.h | 44 +++++++++++++++++++++++++++++++++++ arch/riscv/kernel/vdso/Makefile | 8 +++++++ 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, 74 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) := $(riscv-march-y)_zacas # Check if the toolchain supports Zabha riscv-march-$(CONFIG_TOOLCHAIN_HAS_ZABHA) := $(riscv-march-y)_zabha +# Check if the toolchain supports Zihintpause extension +riscv-march-$(CONFIG_TOOLCHAIN_HAS_ZIHINTPAUSE) := $(riscv-march-y)_zihintpause + +KBUILD_BASE_ISA = -march=$(shell echo $(riscv-march-y) | sed -E 's/(rv32ima|rv64ima)fd([^v_]*)v?/\1\2/') +export KBUILD_BASE_ISA # 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 += -march=$(shell echo $(riscv-march-y) | sed -E 's/(rv32ima|rv64ima)fd([^v_]*)v?/\1\2/') +KBUILD_CFLAGS += $(KBUILD_BASE_ISA) KBUILD_AFLAGS += -march=$(riscv-march-y) diff --git a/arch/riscv/include/asm/assembler.h b/arch/riscv/include/asm/assembler.h index 44b1457d3e95..edbe86d0b4f7 100644 --- a/arch/riscv/include/asm/assembler.h +++ b/arch/riscv/include/asm/assembler.h @@ -80,3 +80,47 @@ .endm #endif /* __ASM_ASSEMBLER_H */ + +#if defined(CONFIG_RISCV_USER_CFI) && (__riscv_xlen == 64) +.macro vdso_lpad +lpad 1 +.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 == 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 = GNU_PROPERTY_RISCV_FEATURE_1_DEFAULT + .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 = 0 +.endm +#endif diff --git a/arch/riscv/kernel/vdso/Makefile b/arch/riscv/kernel/vdso/Makefile index 9a1b555e8733..93c134f0afb1 100644 --- a/arch/riscv/kernel/vdso/Makefile +++ b/arch/riscv/kernel/vdso/Makefile @@ -19,6 +19,8 @@ obj-vdso = $(patsubst %, %.o, $(vdso-syms)) note.o ccflags-y := -fno-stack-protector ccflags-y += -DDISABLE_BRANCH_PROFILING ccflags-y += -fno-builtin +ccflags-y += $(KBUILD_BASE_ISA)_zicfilp +asflags-y += $(KBUILD_BASE_ISA)_zicfilp ifneq ($(c-gettimeofday-y),) CFLAGS_vgettimeofday.o += -fPIC -include $(c-gettimeofday-y) @@ -40,6 +42,12 @@ endif CFLAGS_REMOVE_vgettimeofday.o = $(CC_FLAGS_FTRACE) $(CC_FLAGS_SCS) CFLAGS_REMOVE_hwprobe.o = $(CC_FLAGS_FTRACE) $(CC_FLAGS_SCS) +# Disable profiling and instrumentation for VDSO code +GCOV_PROFILE := n +KCOV_INSTRUMENT := n +KASAN_SANITIZE := n +UBSAN_SANITIZE := n + # Force dependency $(obj)/vdso.o: $(obj)/vdso.so 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 @@ #include #include +#include .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/getcpu.S index 9c1bd531907f..5c1ecc4e1465 100644 --- a/arch/riscv/kernel/vdso/getcpu.S +++ b/arch/riscv/kernel/vdso/getcpu.S @@ -5,14 +5,18 @@ #include #include +#include .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 @@ #include #include +#include .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 @@ #include #include +#include .text SYM_FUNC_START(riscv_hwprobe) .cfi_startproc + vdso_lpad li a7, __NR_riscv_hwprobe ecall ret .cfi_endproc SYM_FUNC_END(riscv_hwprobe) + +emit_riscv_feature_1_and From patchwork Mon Feb 10 20:26:57 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13969359 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 82F96C021A3 for ; Mon, 10 Feb 2025 21:40:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=QtbSUQe4nKmAXnCTInEk1WY5vPoUNqfHcvrQI2XShC8=; b=sKTwkSWSt3zOYq Tf4sCtf+LuRT4Y2icHoG/OBAF2yYUbEq/8+Avk2G0xcXmPvWgRRPgCsBgE34O5utjyOwmMLVj+xgX 6uOrEmxlpknDgu2Xru7chO+I4Wn/ec4ZjeNBETCrivK8KUOwyCg2xOKaj6T2eB5CURDKJm5x7tSD7 aZNPGixQT1L+KLDyNtGB/SiP4/7wOhfN8zeVwqOkztC6Yem7cvkcPPvztOxsCRdk/Y7GA4cbcBrMz mJ0V7l1mpgKiETC/5Gp+Cuxtgn9qzt6hacv79alHqNZl8KYr5AbQETTSfG/1/U/QIDkvNbGYReFAW frS08VaDoTjIiCsVCGwA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1thbWP-00000001WoD-3Eyb; Mon, 10 Feb 2025 21:40:53 +0000 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1thaNg-00000001KoQ-13sv for linux-riscv@bombadil.infradead.org; Mon, 10 Feb 2025 20:27:48 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Cc:To:In-Reply-To:References:Message-Id :Content-Transfer-Encoding:Content-Type:MIME-Version:Subject:Date:From:Sender :Reply-To:Content-ID:Content-Description; bh=9LN1OOVFFK55bIQe9Bj7KSCICvDpdODqLxcVYOIQy28=; b=OXlMqZAraBEpJ3RU/E26miRuPt lxDInAlhmKorM3WzYqDLhBLPGHg4fNGz8kVK6QqMHOlc4I/ETEHTJ6+7C7bWXP8pzDNh4Xx28SDJ+ brVXzSWHGgj3iAnRhECHiv8o61eoc76LVdcQKJo2pR28ufxupgyO0TUqKSUpVWhlZFH9d3spPFIuk o4L7DHIHVE3w0rDrhqlzV906vYNPMPujRrcAdEK31UFFyL+oxqUwSBZSuzJ46cF0hoCO3YMiPECN8 7K8N62/Z/YpDFP0LW18kepF/qq7q5J/HT8T5NUyLoJMp/RNIfWXxx+hLDRJ9kS4e4GdAug/Oij+/c YmFMCVFA==; Received: from mail-pl1-x633.google.com ([2607:f8b0:4864:20::633]) by casper.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1thaNc-0000000GioS-3MY7 for linux-riscv@lists.infradead.org; Mon, 10 Feb 2025 20:27:47 +0000 Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-21f40deb941so100127555ad.2 for ; Mon, 10 Feb 2025 12:27:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1739219262; x=1739824062; darn=lists.infradead.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=I5RBVkqOqx5aU8qdT4FRiWvkoF8BxC2kgzXLcHL1pvneLK0Qq6Lw66gltlMZgPLkke 5pXrHUCAw5tfapUHfiY0BTu1ZKDpEIUyXmtOqwE6e9MM/zrYMZARiw+ucbLK0FN/PVVv z0Xz8tTuKZox1VwisLzizxarCT0putMtV6JIW/UQSgHvutjy693SW8S5uVO3wMWhUvOL rJyKSEgkJByRypi8rwHu2RKuJJHurDLOSucjTDbe/5Tl/dFoKmZ5fUSS/Or24AIvxTfN DfUFNkKIZxIE8GF+MDFWqh5zkktpl45AflT4Dh1fwI8pNpnz+jyLIYgtDb4fPYuApjm8 6OKA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739219262; x=1739824062; 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=CrEQTKEMYs0Zls4y1D1EkHE51v8aVJUDTpqQSHYODwqJ58q2Ku03reIbBZMUnfd5UL 53nBQpCuR336BD91RbVkMhdQk/1uCbbay9ZSBeC4g3+h+YyzgC/qcJUZXjy0DmhMYm14 OkWLQzQHwgcTpItjJCrczwhKzc5AVZWqk6aAMI2ccRVIx940TRS3bwtJIuJOHpEb5yOj PcuyYklPqLTNQ3k/l4I4jMC0M2cojLictGVIB7tbyCKn4COgbG6LpCmpdvB9dyZaquib tW+f3ftYbrwvedpkUOqVuj/hBm0yAd+eMH1vh0IMIaDCcaDUdy8sfsLKUfSSViR9/wjo 5RFQ== X-Forwarded-Encrypted: i=1; AJvYcCWDbC+M0q+nrW1ZtXInSOZlDNhfSO5M9lZnOOeZ/QA7tm5rk55/N+z3L03JW2l3kKZlgePI3lg+1FP0zg==@lists.infradead.org X-Gm-Message-State: AOJu0YyZwev/WhY/Hig4LQ+cWk7DDjRzG9165wrmZlCL2ebOblCZ+9C4 jXO5mnfCru0QHRCfTC3VT9lcfWr040KMzOn/swFISJ3jvXtHKlEF/CXrSHTltgY= X-Gm-Gg: ASbGncsqnBmucb1aaq1oau+Du7tDDwHOzUpHaBC5pctZRGfeeb0fS3DNrIMoT6yjmIA gO6r+5FSPeKg+epkNgRiRpGb57KZ52O3Eq/kRffn5soPWTPG2AguPnYBs1wfunPNRzS64ygzwP6 HqRvJ/Uecj5iLwOSKQoaFeli1eh1tL3RCIADrmLptWmWP61f8THV0XjwGqJmGm+3SkyAOeX8NRc AkYsrdNdNXMDTuoloH1/V7HKCxcqG0oafwwOSXbOZIzjTciS1PFvnzhVxY84tRwCeptW7xiWsVh PcuRvIbZdrS7d4mrfT8c7xqswQ== X-Google-Smtp-Source: AGHT+IGvJHthtMMIotmYr/CK0oOoURKWcK3k+qdOAPpSTfBW1H5FZ/zO8d2C9jHOqU/wdcn1U6s6JQ== X-Received: by 2002:a17:903:2311:b0:21f:3e2d:7d36 with SMTP id d9443c01a7336-21f4e6ceb59mr270827895ad.1.1739219261893; Mon, 10 Feb 2025 12:27:41 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21f3687c187sm83711555ad.168.2025.02.10.12.27.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Feb 2025 12:27:41 -0800 (PST) From: Deepak Gupta Date: Mon, 10 Feb 2025 12:26:57 -0800 Subject: [PATCH v10 24/27] riscv: create a config for shadow stack and landing pad instr support MIME-Version: 1.0 Message-Id: <20250210-v5_user_cfi_series-v10-24-163dcfa31c60@rivosinc.com> References: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@rivosinc.com> In-Reply-To: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250210_202744_849533_7EA5D6F6 X-CRM114-Status: GOOD ( 11.91 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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 --- 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/7ffabb61a5569444b5ac9322e22e5471cc5e4a77 depends on LD_IS_LLD && LLD_VERSION < 180000 +config RISCV_USER_CFI + def_bool y + bool "riscv userspace control flow integrity" + depends on 64BIT && $(cc-option,-mabi=lp64 -march=rv64ima_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 From patchwork Mon Feb 10 20:26:58 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13969354 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id CF3C1C0219D for ; Mon, 10 Feb 2025 20:29:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=JGBnYRCFj2pq9AMdyqj4UEl4WGlEgnvBxRgQcjbNKgw=; b=hIzmOw3BxBX1b9 VR0ENBM4yew26rnvEH0+xsGTebCa5AOqTo4EYOzwWe+88sse2CRGT0BowvTaS0K92jN4Ky51ubHMy o700c5UJCc4/9Y6TpgGt17/tAvhYNL1/IsFRmxXk01NBakd4rEM1NWir4IkNehA0FpLMlm4d+Myte ZwbZNOR0wZmRz0v6kLOItmBhZ/1iATdmZSdV6+lft80Yu5jBEph22/wL6k06ft3jQDUN0ROn8SXoi 4HwmBwDeaRGuALe+48UvMHeGpXImFPdtBdpSKlPeYaHvJOqZ2f7FNCjfrTBdQAVNC1ZoQA8kTCtNd ivejhT7xuVFO9GfF5m9Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1thaOu-00000001LPq-3EZr; Mon, 10 Feb 2025 20:29:04 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1thaNk-00000001Ksf-3B6i for linux-riscv@bombadil.infradead.org; Mon, 10 Feb 2025 20:27:52 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Cc:To:In-Reply-To:References: Message-Id:Content-Transfer-Encoding:Content-Type:MIME-Version:Subject:Date: From:Sender:Reply-To:Content-ID:Content-Description; bh=vh4CUvpz/fzJO7m3g4Lrx30VWLQNLgBESiJnDVTGIvg=; b=e1GkXwPBPUMa+FF/PEWkcqhPJt SAc+phvKR2e27DptTeDpa1ana+MVRgZ/6RzYBwdtqRRkepEMruERoJJlCGE3LbQDDotoxZfOhcOP/ hBcqF+4QGUzC9mtfMDOeoZYot3mbm/T27A/TjNjh9jAxwUlJSH8OJw4PIBmGTYXCKBVby51kEkC7p taRA3LwNZRUMkl4yoXY0V/nmYNG+HirRslIgq2rlqztFS9YOvrS8YreUl80sh1IjGHR+59kQfFjda yjD2B0y1pfaC0fhkCXr4a1DWOGRf0QPpxQyykxNp5j+FudUsamTrDDyeFvn+ZngRUWfklTFVBPNWs Bba/Fvvw==; Received: from mail-pl1-x632.google.com ([2607:f8b0:4864:20::632]) by desiato.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1thaNe-00000000DvA-438q for linux-riscv@lists.infradead.org; Mon, 10 Feb 2025 20:27:51 +0000 Received: by mail-pl1-x632.google.com with SMTP id d9443c01a7336-21f55fbb72bso56910445ad.2 for ; Mon, 10 Feb 2025 12:27:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1739219264; x=1739824064; darn=lists.infradead.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=y1E1+wVbW6ve9YQ2fwXzzizK7wcM+XJmD1Usst03IMRcIihDfrskpsrmYmiJsqN1h1 r/66OdHYeqmvHuovhMIm6opRs0HRUjD3A/IN7F2sxc0Ln1woB9GFkD3NQ1s+NXRqOJEq VlWZZdrNuC+ObRRmrbqL5bDyTrE1Mct5Rg3K0M8/A7gkgY5K2uAoYAA7ifPwteS+deug XTXNeyhZpHMTTLOXgXEjVCbIRQoo+0csYMZAUpx0rdsZtx4uwMghz6D1go+MzBMOavHL YsuoacsmbiH7JDWcn39cgzTxyfzvMlkMkLD/o0sFlMViUpf8LZtexLxeQf4belgZvXym Pc/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739219264; x=1739824064; 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=VYyqsCx8WJrBdxaQ18FSuoVf0UC3CUhMVmUlHdkgxHTt7ae9ZoBX92E9x451BHWTxd 5u73RgZhokqtmDyyQsbjbGoopvGMHT6InenEhYslbiokA0lSEkEzQfXe8OPHra7Oubks TUsEHNG8DT4nQs9V2EpHumU3HX2iu/WBNcO7AGkESdMDXPTVouv1uI79Aw1eWeu+ssPY jctqOpmlbycqetoTsY5Ecg4QTURrrdVPA4CWyieaXgQPxbNzzAAscEFJ6LhSFUUkwHNn yCEeP3qsGlUvfwCygw4jLe7+G8YcD5O9lHvUzJ1lGB/WbavV0H4GmV3vYe9FB+8HtypD M2VQ== X-Forwarded-Encrypted: i=1; AJvYcCUtGdOJs0FfB34voNFS9upvqmCqCRl+QSNRG5aAkenNLcNiV92aBFJi08EcqxuyDkPR5shcf+cmBMA+kg==@lists.infradead.org X-Gm-Message-State: AOJu0Yytd0/XJcYJ9gUTrY20AbCPmyabMU1PA6MpjqTvvOKc8tJL3o/u roJdXCAcFveyg5U8fwsIbzzjJGb03bQwd/JYnzBlQLphqI2aXEtqtSd+HjwsrcA= X-Gm-Gg: ASbGnctd429NqSA4VPnwb1RdLFbVDi0zPwUkD9VP4Wk/7lUG0sgUQE3KWZaUpGWqBS9 Hbn5BMoqRicPsnYC/d0eQt436oFTr/RuioAKyCZd4DHawGfYSz6SjywFo5TQ5136bbTbjO7kBHP uMRlp1GNlhe9nHSzFmnTzYcrPy8yetnMiuaLE5ofK8sqeAnIXTmp0Ro4RNA1hIvX2TMGUAf9v/s 0UH78GrveU3eoCWbDcBkPOE9IJ8G4kUsvMXJlXXAQzlfIzeqyD52WaNpu3YRK0pHTalxv3f5F9l QK2ZxExzZANnIKs2GZrZsY+AqQ== X-Google-Smtp-Source: AGHT+IHONYQapq26xKI/Nlqt2sawgQ5ST7q18FCOtyS0Axv9TidoEnrL+FFU0G/ytCjykvg19tyLYQ== X-Received: by 2002:a17:902:d4cc:b0:21f:6a36:7bf3 with SMTP id d9443c01a7336-21f6a369009mr211502955ad.12.1739219264447; Mon, 10 Feb 2025 12:27:44 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21f3687c187sm83711555ad.168.2025.02.10.12.27.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Feb 2025 12:27:44 -0800 (PST) From: Deepak Gupta Date: Mon, 10 Feb 2025 12:26:58 -0800 Subject: [PATCH v10 25/27] riscv: Documentation for landing pad / indirect branch tracking MIME-Version: 1.0 Message-Id: <20250210-v5_user_cfi_series-v10-25-163dcfa31c60@rivosinc.com> References: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@rivosinc.com> In-Reply-To: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250210_202749_388254_A948C83E X-CRM114-Status: GOOD ( 19.41 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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 --- 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 features diff --git a/Documentation/arch/riscv/zicfilp.rst b/Documentation/arch/riscv/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 + +==================================================== +Tracking indirect control transfers on RISC-V Linux +==================================================== + +This document briefly describes the interface provided to userspace by Linux +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 hands of +an adversary and if used creatively can result into variety security issues. + +One of those security issues can be code re-use attacks on program where adversary +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 corruption +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 = x1 or rs1 = x5, i.e. a return from a function and returns are + protected using shadow stack (see zicfiss.rst) + + - rs1 = 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=x7 is exempted from tracking and considered software + guarded jumps. + +``lpad`` instruction is pseudo of ``auipc rd, `` with ``rd=x0`` and +is a HINT nop. ``lpad`` instruction must be aligned on 4 byte boundary and +compares 20 bit immediate withx7. If ``imm_20bit`` == 0, CPU don't perform any +comparision with ``x7``. If ``imm_20bit`` != 0, then ``imm_20bit`` must match +``x7`` else CPU will raise ``software check exception`` (``cause=18``) with +``*tval = 2``. + +Compiler can generate a hash over function signatures and setup them (truncated +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 compiled +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 indirect +branch tracking. prctls are arch agnostic and returns -EINVAL on other arches. + +* 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 loader +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 may +want to run with strict security posture and wouldn't want loading of objects +without ``zicfilp`` support in it and thus would want to disallow disabling of +indirect branch tracking. In that case user space can use this prctl to lock +current settings. + +5. violations related to indirect branch tracking +-------------------------------------------------- + +Pertaining to indirect branch tracking, CPU raises software check exception 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 = 2`` is captured and software check exception is +raised (``cause=18``) + +Linux kernel will treat this as :c:macro:`SIGSEV`` with code = +:c:macro:`SEGV_CPERR` and follow normal course of signal delivery. From patchwork Mon Feb 10 20:26:59 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13969361 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 1195EC021A7 for ; Mon, 10 Feb 2025 21:41:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=9TQ+a8yVy3B/bxqeOdneO2JL5XTeOfgmUQ3FcZuqPaw=; b=IfMbA88IIbJi1R DokKBGVbsEzHo5+6V/B+HHQTHMhEVCp+j3vOOjhZGNAt7PnXJUF3B8IcQy/OjTtAyXbjae/99TFOO RMflXZRE4+5DBLwdbG+plPaye2gUfEMNw9Kh7vtsXfcwRt5arD3kTJ5XpM3WRrOfaO25gVZ1d74Vt LfnJbepnjWGkFWsPk6QGAq/dn9chZwo+I/O/s6MbOYaRkGMEDU696MOI3MepWf+mh2zWdbiXcb71A ckfJjwMKW2qvcOcGLhj2fJxi5rLXxsSLfCteOjQe+qhb2YrGLyxIw93WnhBHEay4EDA9LOSX8vMpd 5TQPkqgtEYPHjSYGj4nA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1thbWQ-00000001WpB-3Fz2; Mon, 10 Feb 2025 21:40:54 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1thaNm-00000001Ktz-1XFD for linux-riscv@bombadil.infradead.org; Mon, 10 Feb 2025 20:27:54 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Cc:To:In-Reply-To:References: Message-Id:Content-Transfer-Encoding:Content-Type:MIME-Version:Subject:Date: From:Sender:Reply-To:Content-ID:Content-Description; bh=tdDNQkSVTKtNTxsV0uSmAnlAiDQNW7ggaTdWphzDp8I=; b=Lp9BRH9AH+pG1WV+EIIeLEqDMs q+5ZPvfxqUqUx9OvB84sVV/ajG2qA8CxETuONpmzPa1vQUKQSNwxXdojHLZPGgvjjJEyLzr+n3nI1 /i3BEkE9rh/UktadyK6O+aZL4s7cdafXOunyPpWPsSedzub16zETog82jrRD1Uln/tUNo+BjI1VFX f4/ZUeYmjVdWszzABmq7AKCygUkef6wUFsLb9Luo9mxPjeSTeORWZHSNZS3RDlRyXtZM5CV3S40bA bW1mx5JVvidTJP6/pLDUoW+7n2qfBJL9FTpA74ofbHmf8Cn1OzBARxaTdwmg3hQDU24Xci5MC1O36 yVQr+T8A==; Received: from mail-pl1-x62c.google.com ([2607:f8b0:4864:20::62c]) by desiato.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1thaNh-00000000DvN-2RQu for linux-riscv@lists.infradead.org; Mon, 10 Feb 2025 20:27:53 +0000 Received: by mail-pl1-x62c.google.com with SMTP id d9443c01a7336-21fa56e1583so18613955ad.3 for ; Mon, 10 Feb 2025 12:27:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1739219267; x=1739824067; darn=lists.infradead.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=kXoB1y+UAa8fWxoTTzq6ZMEpUUYH2hLhYvlZW8+jQkcBAHVO9foEtKdTYoGbZAJgHb JWhOvZlBy6NyR4iZmM9oGgNls0Do9zKmhpaHgympr5Wz66BZa0AgFTvnQ365u+XIDa5i xy6miWkKJkEA5UrMsOLncuT97Bmt1y6TdcOYCCqh6mmp1hkOPpBr9UpvSLtDOwsQoevz rR9QP6AFOlIZhppU9ppdrF1s66yt/GpwNoXO9ihkYzzv2lXlwIOepr3KIVBUsZkkNc9y /GjPbKHEZXpRRh2GaHe+BWGcevodtgss1aySBpg0XiDOEvCKUacLcZHnF04hMSVdmx1A 3zNA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739219267; x=1739824067; 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=BM4InIzalM3o0x5B24k3V7R2peohOyxt+awnk7jokbHt8jyg166suf9kJm2da+p5UE h1Vh2pH/uOcGkIf2n0w+ryLiDCteEj/Jj7km+AajwBtATp2KOC0vypp6eZ6q+PsmuUS1 loZrP/WaIYXL7v4EsRgblqD4MlbUTZC9/e5HH/LM8rRgJfqm4jgFeKgIyPGlN2alfKLV T+dAgP/liWvDtW1Ak3gm1oWizfJNViITpHdWA8vnkBTfP9GAN9nRFTZnSX4rvgBtBOvb uytRPOUCyRcewSwDXJilm765XXHFf+D4BPfUP9y7qmeVVhidDBMtb6X+SSkFQEIfpdn4 GaMQ== X-Forwarded-Encrypted: i=1; AJvYcCWHe7s4xR7XE9BBduviqyr0cdK7HCWyXShfgIBtKWC8wJHpkn/tM83eo6ey2+k1+MMIboJNjfNt8lZyJQ==@lists.infradead.org X-Gm-Message-State: AOJu0YxRRFUK7TFxiNmjZwnDuBaaSCJHA+CNbpOi9mPOEWYoaZDhMLBm P+V5BfaThgF0IZ8n1Mx5Fj6D5yngN/Epd3Lyw2HvnzrPm0MgAFNpyTbx1ahCPwU= X-Gm-Gg: ASbGncso7qQSTdpZ2l+Tb/1bM1wVV9e21U7lx6o+qrA+M76ilAhDR9puYx8whjivEXQ onqN0K7dO0xjy/dq6QzfdOLiTo13bRmMaevEWr97iPPP4VoPIs9mFYVR51hKY2aAQin6NNKN6FD r1I8kdXRg+ausAFnYGU/Y/NzI8azxjbJLS3uHVA9535OTbS5FL7iKFksb+wMUx4AfaKrKVjMGB3 E9VeUFHCyhYFHLg8aWU6mk38ocAZeb6Iw2gzCG6mlhTlxgQgTMX/XLHd6joqcPHry9ygj3r/FtL NT9i5J1ZhDftqncYRJ9RuclbrA== X-Google-Smtp-Source: AGHT+IF77b/hZZKX9o5D9F6SK8FsPyTMmzVDWUBYEE5LDFgE+CZCLSSTSUYC2ZjQiunkNaspDHPycA== X-Received: by 2002:a17:902:f646:b0:21f:522b:690f with SMTP id d9443c01a7336-21f522b6f5dmr266497335ad.46.1739219266961; Mon, 10 Feb 2025 12:27:46 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21f3687c187sm83711555ad.168.2025.02.10.12.27.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Feb 2025 12:27:46 -0800 (PST) From: Deepak Gupta Date: Mon, 10 Feb 2025 12:26:59 -0800 Subject: [PATCH v10 26/27] riscv: Documentation for shadow stack on riscv MIME-Version: 1.0 Message-Id: <20250210-v5_user_cfi_series-v10-26-163dcfa31c60@rivosinc.com> References: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@rivosinc.com> In-Reply-To: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250210_202749_686330_6EFA42BD X-CRM114-Status: GOOD ( 23.77 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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 --- 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 features diff --git a/Documentation/arch/riscv/zicfiss.rst b/Documentation/arch/riscv/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 + +========================================================= +Shadow stack to protect function returns on RISC-V Linux +========================================================= + +This document briefly describes the interface provided to userspace by Linux +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 hands of +an adversary and if used creatively can result into variety security issues. + +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 counter +(PC) in address space. On RISC-V ``zicfiss`` extension provides an alternate +stack termed as shadow stack on which return addresses can be safely placed 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=0, PTE.W=1, PTE.X=0 becomes PTE encoding for shadow stack pages. + +- ``sspush x1/x5`` instruction pushes (stores) ``x1/x5`` to shadow stack. + +- ``sspopchk x1/x5`` instruction pops (loads) from shadow stack and compares + with ``x1/x5`` and if un-equal, CPU raises ``software check exception`` with + ``*tval = 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 have +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 way + 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/store + 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, kernel + can convert shadow stack pages into read-only memory (as it does for regular + 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 compiled +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_STATUS` / +:c:macro:`PR_LOCK_SHADOW_STACK_STATUS` are three prctls added to manage shadow +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 address +space have support for shadow stack. Additionally if there is a +:c:macro:`dlopen` to an object which wasn't compiled with ``zicfiss``, dynamic +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 disabling 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 following +condition: + +- On execution of ``sspopchk x1/x5``, ``x1/x5`` didn't match top of shadow + stack. If mismatch happens then cpu does ``*tval = 3`` and raise software + check exception. + +Linux kernel will treat this as :c:macro:`SIGSEV`` with code = +: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 / switching to +shadow stack is simply writing to csr ``CSR_SSP`` changes active shadow stack. +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 range. +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 various +contexts as part of single thread. Software can be kernel as well when kernel +has to deliver a signal to user task and must save shadow stack pointer. Kernel +can perform similar procedure by saving a token on user shadow stack itself. +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, kernel +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 shadow +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. From patchwork Mon Feb 10 20:27:00 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13969362 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id B63F3C021A5 for ; Mon, 10 Feb 2025 21:41:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=Gj2sMvOMp3Jj4q8lsR+Ze44GE6tPEoF3P9lzbjbgh8c=; b=OUEkitk0BxEP+a piBzqEC5Cs1VDyBN0my0A5T1Cuk7iRHEtMZ+YzNype5cB44Q8VMlMljT0CY3gxSKMaAsCafF64H7C YYvYpYiWK5ATp8F4/ekY7s/1igylOPEvycya4mXDzDn4zTVqKeHHIopy5lyCYX8j+JUpXdQ6gq2rR 3DQUTcr8GzPXnoPZSKsLdOyboPHCojog5WjCni2ozD/6HQS289lpngh2cy0dzFn4UerJwbAuZnGfk zUmDJ6C5AuqcS8m0uNiCJMqSq95jrGl647lcKhmfXmpnayqFBqgqv2Qv3CEmYwr8s9nmUlgrYU7mX WJNxK0DHYkT103F8KSxg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1thbWQ-00000001Woj-29O1; Mon, 10 Feb 2025 21:40:54 +0000 Received: from mail-pl1-x629.google.com ([2607:f8b0:4864:20::629]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1thaNi-00000001Kqr-0fYz for linux-riscv@lists.infradead.org; Mon, 10 Feb 2025 20:27:51 +0000 Received: by mail-pl1-x629.google.com with SMTP id d9443c01a7336-21f49837d36so53940575ad.3 for ; Mon, 10 Feb 2025 12:27:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1739219269; x=1739824069; darn=lists.infradead.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=SsrJQfGea8TwYsK+b3GH1PA8+reyL5uIpdY1QEsIIPPIoQH2GckI1h7cvCKJwAdWwm LLPiLbcB7VpKssnz9zmYijjZIV0CKxy1pCA6UesFT7iPo0Ijo0nj0/X8WIpt0uvtuSgn lE3hWdY6hAPBdZ3Y4NlbWJsRZNOk43q21Jnlh02B3GClYuVheiQJojnzEh40G9Y7VArp Nf45JDs4okttW5kCv6SJWO40OOr5ngi4+HRZLWb5NbHxcVRa8pPLihPkWs4MrhX82FxS W0gcn2bBEljGNB76BzWktGqzdfVoItJ1pv4s4BE3+SWvoIxN9xW4CX8Ja6dA1MIG264+ zoog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739219269; x=1739824069; 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=OkXhScJu+eDvyWyz8jot1EzfNU9hfchw+BWK5QPf+DKU/dQifdFwUPORRWZ1DM4W+W OW7mO90sC3/6OCrCXGZjbaN7a8dczmIRiOSc3VI4pdVx4vZeMi6nv40Nqei3BtjybrRg akfhbrTETf9X6cYYcOfZLl6DubGxZBg3MmR1ZNYovp7tUYCT1IGYMrm2QV7n6/cWw5Ma eA3GxyVjT2oxPOIw091epV/WpZFgZ0eizi9F0cOZeiSWp0eeoFpu4Tu4ougW54VVm9z7 0chdHStkSL8p872AMRvv0S0D0gekXuS/rHxtTzkZwyrN1wngkNqfQ3PhJ/kZqQD7Hd4a TBvQ== X-Forwarded-Encrypted: i=1; AJvYcCWNhe7upkkVQhzFL/QGTdkjtJBuCiy5lvzGo/IEjy6XXvTxsYJw/EbPGJPZzeiGaqDykl8LJWkUJO6FCg==@lists.infradead.org X-Gm-Message-State: AOJu0YxvCYGrCArsk3wA1yjAnek5m8XdaJo3hcLKQxetn8GqYZu9K7UJ 6sjIe4xiM8tCXov6W9GMv30ihzfrf6aRJ6jOEU4TbLQXX52inHXh1DkFGxvlXt0= X-Gm-Gg: ASbGncsZckd0iy4bFY6bNkOt7sI03Vu/yY708cOhM37e0rfT4kPoHUcG7F4xgMAEIyh VjQmeSi0CXZT/Awh3FRSmTvk/09+GcVGKl3K+CNkSQx/M6MatRAwXM60SwQ/bB3ZOScnSbslvpj sklJ6Qv/1mbWlBDfpc7M2tmALQm/I9NNfhtMuhNdopOdp9bWnPLZGdxo5Cqj36/Pld8Pucovyps vgqhqP/fHIyTWvBiWSCZK1+R8o4y5Zn5p1SRNlzFCrOjPow/TBUz1IdelTU0HmohoCigut6hBnJ V2m1Zf02tebxTnSJCCZHce/4wQ== X-Google-Smtp-Source: AGHT+IE2Uj1sWUdpEj0gHWEptcQGW9ooD5IicrlNY1DO+ltEd2VAtju32umUOH5xb6B4TzgsWO/D8A== X-Received: by 2002:a17:902:e88e:b0:21f:55e:ed71 with SMTP id d9443c01a7336-21f4e6a0100mr226698995ad.5.1739219269485; Mon, 10 Feb 2025 12:27:49 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21f3687c187sm83711555ad.168.2025.02.10.12.27.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Feb 2025 12:27:49 -0800 (PST) From: Deepak Gupta Date: Mon, 10 Feb 2025 12:27:00 -0800 Subject: [PATCH v10 27/27] kselftest/riscv: kselftest for user mode cfi MIME-Version: 1.0 Message-Id: <20250210-v5_user_cfi_series-v10-27-163dcfa31c60@rivosinc.com> References: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@rivosinc.com> In-Reply-To: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250210_122750_233766_1D2D5553 X-CRM114-Status: GOOD ( 23.28 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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 = 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 ========== """ 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/selftests/riscv/Makefile index 099b8c1f46f8..5671b4405a12 100644 --- a/tools/testing/selftests/riscv/Makefile +++ b/tools/testing/selftests/riscv/Makefile @@ -5,7 +5,7 @@ ARCH ?= $(shell uname -m 2>/dev/null || echo not) ifneq (,$(filter $(ARCH),riscv)) -RISCV_SUBTARGETS ?= abi hwprobe mm sigreturn vector +RISCV_SUBTARGETS ?= abi hwprobe mm sigreturn vector cfi else RISCV_SUBTARGETS := endif diff --git a/tools/testing/selftests/riscv/cfi/.gitignore b/tools/testing/selftests/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/selftests/riscv/cfi/Makefile new file mode 100644 index 000000000000..b65f7ff38a32 --- /dev/null +++ b/tools/testing/selftests/riscv/cfi/Makefile @@ -0,0 +1,10 @@ +CFLAGS += -I$(top_srcdir)/tools/include + +CFLAGS += -march=rv64gc_zicfilp_zicfiss + +TEST_GEN_PROGS := 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/testing/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") = (num); \ + register long _arg1 __asm__ ("a0") = (long)(arg1); \ + register long _arg2 __asm__ ("a1") = (long)(arg2); \ + register long _arg3 __asm__ ("a2") = (long)(arg3); \ + register long _arg4 __asm__ ("a3") = (long)(arg4); \ + register long _arg5 __asm__ ("a4") = (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") = (num); \ + register long _arg1 __asm__ ("a0") = (long)(arg1); \ + register long _arg2 __asm__ ("a1") = (long)(arg2); \ + register long _arg3 __asm__ ("a2") = (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) \ + : "=r" (__v) : \ + : "memory"); \ + __v; \ +}) + +#define csr_write(csr, val) \ +({ \ + unsigned long __v = (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/testing/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 = (struct ucontext *)uc; + + if (si->si_code == SEGV_CPERR) { + ksft_print_msg("Control flow violation happened somewhere\n"); + ksft_print_msg("PC where violation happened %lx\n", ctx->uc_mcontext.gregs[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 = {}; + + sa.sa_sigaction = sigsegv_handler; + sa.sa_flags = SA_SIGINFO; + if (sigaction(SIGSEGV, &sa, NULL)) { + ksft_print_msg("Registering signal handler for landing pad violation failed\n"); + return false; + } + + return true; +} + +int main(int argc, char *argv[]) +{ + int ret = 0; + unsigned long lpad_status = 0, ss_status = 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 = 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 glibc\n"); + + ret = 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 glibc\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/testing/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 = 0; + + ssp = 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 = 0; + + ssp = 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 = 0, child_status = 0, parent_pid = 0, ret = 0; + unsigned long ss_status = 0; + + ksft_print_msg("Exercising shadow stack fork test\n"); + + ret = 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 = getpid(); + pid = 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 ensure 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 = 0; + + ksft_print_msg("Exercising shadow stack map test\n"); + + shdw_addr = my_syscall3(__NR_map_shadow_stack, NULL, SHADOW_STACK_ALLOC_SIZE, 0); + + if (((long)shdw_addr) <= 0) { + ksft_print_msg("map_shadow_stack failed with error code %d\n", + (int)shdw_addr); + return false; + } + + ret = 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 = NULL; + int ret = 0, pid = 0, child_status = 0; + + ksft_print_msg("Exercising shadow stack protection test (WPT)\n"); + + shdw_addr = my_syscall3(__NR_map_shadow_stack, NULL, SHADOW_STACK_ALLOC_SIZE, 0); + + if (((long)shdw_addr) <= 0) { + ksft_print_msg("map_shadow_stack failed with error code %d\n", + (int)shdw_addr); + return false; + } + + write_addr = (unsigned long *)shdw_addr; + pid = fork(); + + /* no child was created, return false */ + if (pid == -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 = 0xdeadbeef; + } else { + wait(&child_status); + } + + /* test fail, if 0xdeadbeef present on shadow stack address */ + if (*write_addr == 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) != CHILD_EXIT_CODE_SSWRITE) { + ksft_print_msg("Shadow stack WPT failed: child wasn't signaled for write\n"); + return false; + } + + ret = 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 = 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 = 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 != 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 = 0; + unsigned long *write_addr = NULL; + int fd = 0; + bool ret = false; + + ksft_print_msg("Exercising shadow stack gup tests\n"); + shdw_addr = my_syscall3(__NR_map_shadow_stack, NULL, SHADOW_STACK_ALLOC_SIZE, 0); + + if (((long)shdw_addr) <= 0) { + ksft_print_msg("map_shadow_stack failed with error code %d\n", (int)shdw_addr); + return false; + } + + write_addr = (unsigned long *)shdw_addr; + + fd = open("/proc/self/mem", O_RDWR); + if (fd == -1) + return false; + + if (gup_tests(fd, write_addr)) { + ksft_print_msg("gup tests failed\n"); + goto out; + } + + ret = true; +out: + if (shdw_addr && munmap(write_addr, SHADOW_STACK_ALLOC_SIZE)) { + ksft_print_msg("munmap failed with error code %d\n", ret); + ret = false; + } + + return ret; +} + +volatile bool break_loop; + +void sigusr1_handler(int signo) +{ + break_loop = true; +} + +bool sigusr1_signal_test(void) +{ + struct sigaction sa = {}; + + sa.sa_handler = sigusr1_handler; + sa.sa_flags = 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 = 0, child_status = 0, ret = 0; + unsigned long ss_status = 0; + + ksft_print_msg("Exercising shadow stack signal test\n"); + + ret = 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 = fork(); + + if (pid == -1) { + ksft_print_msg("Signal test: fork failed\n"); + goto out; + } + + if (pid == 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) == 11); +} + +int execute_shadow_stack_tests(void) +{ + int ret = 0; + unsigned long test_count = 0; + unsigned long shstk_status = 0; + bool test_pass = false; + + ksft_print_msg("Executing RISC-V shadow stack self tests\n"); + ksft_set_plan(RISCV_SHADOW_STACK_TESTS); + + ret = my_syscall5(__NR_prctl, PR_GET_SHADOW_STACK_STATUS, &shstk_status, 0, 0, 0); + + if (ret != 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 = (*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/testing/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[] = { + { "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