From patchwork Mon Mar 10 14:52:23 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 14010324 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 23738C282DE for ; Mon, 10 Mar 2025 15:42:31 +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=VNHPHcZx7zY+MX tEiNPbLmC7163+TrEARcpQDaWYV49sTKDmQProISdd8PCmP1GEisLDiR9x2tzzG4npcK8CuP+reKl 2Cv6jd4FqZQ7ISYBzOaYTLJ+PYdgyC4Oc3ab0SbztuO+MJ2cXK2PudUa4exwsDSW8qmxtoiWpx/Um bC+HWakIJRu6dMzvoIPAEVwQz/NizHO7yJqG/Qt0wp9ON5paipQBqnpsi19XL56/EU5GyTjj3HYdp SeK6tafW7k9PhwYCqvHwjIGa+Qt4HVLUjjekXAHferWR/swUWQ9dTlPFo0OfPpBO9x2aoj1KRapf1 4Z1iu13ucowLL3nl1OQQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1trfGq-00000003DDf-43pA; Mon, 10 Mar 2025 15:42:24 +0000 Received: from mail-pl1-x631.google.com ([2607:f8b0:4864:20::631]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1treUd-000000031OO-33rG for linux-riscv@lists.infradead.org; Mon, 10 Mar 2025 14:52:36 +0000 Received: by mail-pl1-x631.google.com with SMTP id d9443c01a7336-223378e2b0dso62364625ad.0 for ; Mon, 10 Mar 2025 07:52:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741618355; x=1742223155; darn=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=bVc4Iok3L1cqWfJRDu/H54+a6HBjz8RfIBxYcGuziCPxVMu2vcI9XXjwYk74FIT8A+ dYQSK0fzdPALX+IQ5bSfqoOX6YxN5zYuARDJLMmYgxE7YDbjsFcyt7H+yb9hT/hfXh2m fLmYay0/aApu24a4xxBWS8auBNIeHceyzkI8GFWwL6iXdDXJkhGtXpifFjRD7S3kk+LU di17FHe71EVFWRo8sOPIHTfmafs0d/t31dVMNK0emLapf9X0JlrmwILBA7/J6NbgHDFX 3ruPFybuTdENeHuF9TTZbM6MbWtuCq2b8Jweehak15BGDab48B+LPjhZMjoXD0OBqsz4 7YJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741618355; x=1742223155; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=9ADFXc53D/cc0VutYfr8tgUjvI5cmEZUhOUtSZhfaHM=; b=vgvsk/fJAlNSWCZPvOICCvTKjFFtJ4n+Q8vFwp+bN2QYif9bRcvn2kfdwfbgohG+uY le1D0JicNrFulaMdr9Xb2Li8gexWG878kBJSk3zH+wFiMOtFnbnEtQofQquoOzkHv5iZ pDYC8DNSnyRxJ8Xxv8Ngy3qgnJoltAEDCSA6HNZM+iAazU6vpXRLtt4zsuWqHxoirthZ pwH2WsKQ/ZAz0jcyOkoTlkWi1pbrDB+mHIM5X2ZLqI7jJYm9XAuKBOcf9+YtYizzeZLf u34Pv7jOnA5NFDXqqmwYeTpHe8ie2hT6e0Z6vlPlinzXBuv4JOOFDZrm37W90nj8nKzX JiCg== X-Forwarded-Encrypted: i=1; AJvYcCXqrQSRHbVD1dEoMSAm692nsTO+bRlzz4Ubj4PWfPTZxcrmOuYBPTgVG0pCfduYRih52WW+ut1xnjnzDA==@lists.infradead.org X-Gm-Message-State: AOJu0Yy8gSswURhi8A5WC0BUeLdx6cZLJxPdUbgZZ2wTJMQqVuHYtkFk plVjun/757IfXssK8IoasIr2mVVJizOl6C4Tw51vF8r8zRieYnu8AQwQ+Tt2ESo= X-Gm-Gg: ASbGncsjgWSi5bbKq1TtHojWu0dF9Iuf1/0TpuO2Ep9XbUYs6EAu87lB4DD20auiKHB 8iRO/E6Sk/I3YA8gOVJ2cgkA2k1yv0m0rn73vFhZ67CbtULXvTc9i+jWqVCL99GPt6sgxGkPXgF G6/wSWRGxGWNa5ah7rHpZqpn9ZWxgcv7xgRNKcOyx46qRGJ71Yezs971WhnoTP1RywNV89irz9V n9PaOFY9cZlTFoxzGzGlgzle5z/T1W4TcFUbJgkErD2a1A413ma05iwTkXW4LjELpmuGco+7MpS IPrVn5FVBX65SS43lkdmGn2Af+9txXsL4+Y6TByYJyk8byeHRfOILWg= X-Google-Smtp-Source: AGHT+IGmALl7tE1vDAuhwzUIZXgg+Q9w22Wqj1GO54eJLmN8OylP52nRYF70/T1mCfLbdEp2H00RTw== X-Received: by 2002:a05:6a21:4606:b0:1f5:7df9:f147 with SMTP id adf61e73a8af0-1f57df9f49dmr7380018637.40.1741618355060; Mon, 10 Mar 2025 07:52:35 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-736d11d4600sm2890275b3a.116.2025.03.10.07.52.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 07:52:34 -0700 (PDT) From: Deepak Gupta Date: Mon, 10 Mar 2025 07:52:23 -0700 Subject: [PATCH v11 01/27] mm: VM_SHADOW_STACK definition for riscv MIME-Version: 1.0 Message-Id: <20250310-v5_user_cfi_series-v11-1-86b36cbfb910@rivosinc.com> References: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> In-Reply-To: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Paul Walmsley , Palmer Dabbelt , Albert Ou , Conor Dooley , Rob Herring , Krzysztof Kozlowski , Arnd Bergmann , Christian Brauner , Peter Zijlstra , Oleg Nesterov , Eric Biederman , Kees Cook , Jonathan Corbet , Shuah Khan , Jann Horn , Conor Dooley Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, alistair.francis@wdc.com, richard.henderson@linaro.org, jim.shu@sifive.com, andybnac@gmail.com, kito.cheng@sifive.com, charlie@rivosinc.com, atishp@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, alexghiti@rivosinc.com, samitolvanen@google.com, broonie@kernel.org, rick.p.edgecombe@intel.com, Deepak Gupta X-Mailer: b4 0.14.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250310_075235_775979_E3FAFBAC X-CRM114-Status: GOOD ( 10.23 ) 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 Mar 10 14:52:24 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 14010334 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 C4F68C282EC for ; Mon, 10 Mar 2025 15:42: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=jU0T5QkEr3232RN3yDhzbxX5XeNOIdmcHfVs2jzrTyQ=; b=xeNqY7cj9CGFiz 7BZTso+v82w52E5yrkFSJb+2Y9SzEHu4pakevZostSpRX90v9uSFHztmyGNv7RRwdT25KhLfiM0Wa i8wusjzcXgyBnRw0R5W7OY2wvHBr179Snam//IrRoyTGNoaRQO5NlFh1SK493twPepWFQUlCrKcEy 0/Vb1NdzCaFDEln+YzqIinobWi4gJ2ofHxk5J7ppcB18xiPGDfwwJ7UfcIuLgGkOu0DDmau9RjFoT QjRX9tjVXuWRoOzw1QjwTkk7jU0bRC/azsxmpqpdsGRkS4sLAodQro/9E45cbUoMBtI9jabvUkHDz AoKK731dhVOgYIn+X33w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1trfGr-00000003DDx-2QL9; Mon, 10 Mar 2025 15:42:25 +0000 Received: from mail-pl1-x62f.google.com ([2607:f8b0:4864:20::62f]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1treUg-000000031Pu-2byj for linux-riscv@lists.infradead.org; Mon, 10 Mar 2025 14:52:39 +0000 Received: by mail-pl1-x62f.google.com with SMTP id d9443c01a7336-22359001f1aso104774875ad.3 for ; Mon, 10 Mar 2025 07:52:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741618358; x=1742223158; darn=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=TAeG+eQwzIREj/gfbrOXihZZTmyOXkY5Jq729zlXc/UWI2XjidfzXFmO6fsqiUPHPK Vx7p381juMeHOXDXRpo7wD924rhcBC6HOoMj8aQw372glV3kutbkFYt/WHS2AfflAO4L QRD9SBKiMkr1taVFSPxflEZk5GY5ORWS4ckA7gngjfZfu7EvPw8EnZZQ8UsQrvmGciRi NI9YvQgm4pCjXNR1I2xm3HXqmtOtQ281jIIFCbAzK2PPUjClkYG8d1hrtRdKt8HYrBGd LLaUtYN5Dec9ow3o70Nq81JgsvtLHQYpTbFczmkTCvTl64NKbsBGKt3uH/vX6jipxi/P OJtQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741618358; x=1742223158; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=kHEXIimjUTXAyC0fOzh8XAkym19Q72QcmDI6hc+cq4k=; b=McrsMZwGbsxd0jbjQzxvoteKiwI8qy2nZGn1G37dEOCMdh35Wk+01FKMQdUOPZSuAc iZJ4xzk/o25EWxzQPzDzqPpr2+vFPR8mMtORJUnyxLuFQQwkJm0z5/GXbzNJyz0dsSRa oRn7CEKnkijlw+23mhiRpS4DMRFp64eDE9XK/uw+siTcnh262JY8RZxyPmL/UN4OplP2 zCZmQuVRWRdzLGpzk6pkD7+7wdAQml14KaxRN0HVWnlSDo2sTObwKYNpsdp/CIHAOAYj Ml1NWtpQjxE0/NFGkPmOvcdHIfesDP6MsAYaGRm1rOkkX2z6NqouDFzUZEW5a4RIHoR5 l2KQ== X-Forwarded-Encrypted: i=1; AJvYcCWkQsSC99ec9G9V4GnIPOotX4VZ9DZhImyjrFIakO0jhIxfYtpCNkRZwevRjM5e2KJTWamcYTlxZKGLog==@lists.infradead.org X-Gm-Message-State: AOJu0YxBSBivRyzbu97LyCM+FcINkmyt2WLsretD0bAqu6uH/oMFun4h XyMz61G5q/fs6o47peNhGXe13dzX2jKSOZD9tdjzIV7KFKQLZhgNTBYxkpu+NlY= X-Gm-Gg: ASbGncvHREYvZFE7VZSm/gKDFmFbs4xdQ0y6ef5ja/gbWsFyrz/MCPTxaEXxCMZ+1Iu HzdQwIsWouxrmC9thYeE6uDCCMW75Pi4rnYNJRArSa2r1cU41ZQ+kDVv8EaCL3fxCLAO9diYMRY 2sYjQw8UaPvTabaRwHT3bYzEoQlc+qHd0ukp0nqoz/oDnfKLYUpi6MB8K/JmncNtOec9V65eVGq SzYPBJ3UGgjRiOvwVn/E/wN03JYY+aKvAHGCYZiXrTOFvw9ScDvy4avsU+PZXScL1ByyNA6PkEp dvdlvI/q1VBIF5mwQQUmFJZZk6BzAX3z5OedbjsARXMwqVdyF5JoK+M= X-Google-Smtp-Source: AGHT+IHtkwbPi3YsMJ6A8ZQyEy1VJS3fRxtZo4fUkREMtAJmz9IifioPXZ2s22L3uz4hyDnWUTIX/g== X-Received: by 2002:a05:6a21:2d08:b0:1ee:d687:c39b with SMTP id adf61e73a8af0-1f544c37a63mr24132640637.7.1741618357489; Mon, 10 Mar 2025 07:52:37 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-736d11d4600sm2890275b3a.116.2025.03.10.07.52.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 07:52:37 -0700 (PDT) From: Deepak Gupta Date: Mon, 10 Mar 2025 07:52:24 -0700 Subject: [PATCH v11 02/27] dt-bindings: riscv: zicfilp and zicfiss in dt-bindings (extensions.yaml) MIME-Version: 1.0 Message-Id: <20250310-v5_user_cfi_series-v11-2-86b36cbfb910@rivosinc.com> References: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> In-Reply-To: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Paul Walmsley , Palmer Dabbelt , Albert Ou , Conor Dooley , Rob Herring , Krzysztof Kozlowski , Arnd Bergmann , Christian Brauner , Peter Zijlstra , Oleg Nesterov , Eric Biederman , Kees Cook , Jonathan Corbet , Shuah Khan , Jann Horn , Conor Dooley Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, alistair.francis@wdc.com, richard.henderson@linaro.org, jim.shu@sifive.com, andybnac@gmail.com, kito.cheng@sifive.com, charlie@rivosinc.com, atishp@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, alexghiti@rivosinc.com, samitolvanen@google.com, broonie@kernel.org, rick.p.edgecombe@intel.com, Deepak Gupta X-Mailer: b4 0.14.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250310_075238_669498_F265B3EA X-CRM114-Status: UNSURE ( 9.59 ) 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 Mar 10 14:52:25 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 14010325 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 A0DFBC28B2E for ; Mon, 10 Mar 2025 15:42:31 +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=PsURrSFlIRoeeN CJjDXba3HfGozbnKi0P4EZ5yrMWkttKWQtsPgpa8pQq09ThOP4JLqQ/m6G7ewDn9NnfwGIAdxYjBH 2OyyoVdwWIc0+5JAsiGNJ90Et/sNwEuKAB2BK+sEhZBHRaAewPV/g2tLt4/+zMrhVKXGP0mLq3q5O EprJHVLS5GSiped8P+l3087IwXU6rKT+5T/62gSprsWIQmqGVNwY5/GThKb9sL27yLDBmLWHbrY2k t0IexLiebGBEfSfLYrvPYKDVl3A6rx4jB4tfj8uqnl+fC16EkFWRC6behD/V1MDyljHOzaPPicR5P L+WezLCFvR5TU9X3aP1Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1trfGs-00000003DF8-0xQi; Mon, 10 Mar 2025 15:42:26 +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 1treUo-000000031UW-45ec for linux-riscv@bombadil.infradead.org; Mon, 10 Mar 2025 14:52:47 +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=dbqILCM4sqAsqnZWR3n0haWRm4rdBukz3/+QEOiXjzw=; b=VIbKnH23qP/e+AXcOM2C5AVrzy BtLUacY6vmSkAsy/dTIQlLx7DF9cmVzETba0s8qCnglL+/slNEXA/C3k2RLp3X/YDCcVBOcffdPm8 FVazuAV12EippDNl7lvVIJgyFDxsGaNNEdC9WDNTyZE5ZV9MFWTCWi5vM8PL49VHnAU3L7uOfF7Bh TnQtEKuYHBPh1AM/6j6Ot44coyWIV8K1s/V2/b5+/380FgY2u9buGit3qxz13OBgVjuGXj9c3GCD3 rpWOzjwCn1UVkziL7MqxeH6yV7H/DbjsknCUQVhy4o4BAlXRCwbWJjQ3XsbluqmJ75vItSGsAuNRW xdcY8pUw==; Received: from mail-pl1-x62a.google.com ([2607:f8b0:4864:20::62a]) by desiato.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1treUl-00000001sEs-1jKJ for linux-riscv@lists.infradead.org; Mon, 10 Mar 2025 14:52:45 +0000 Received: by mail-pl1-x62a.google.com with SMTP id d9443c01a7336-223a7065ff8so3861995ad.0 for ; Mon, 10 Mar 2025 07:52:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741618360; x=1742223160; darn=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=Mwlo1YtuOzVwDu5HBZ4sKeG1NNQdBrpT3VSlsBQ7dDpTA5+hJychJvaoJbapQuoGYQ yPRXNDurVmSdkRJrQSS8cdNjECoDqTXAKm9LChfQMtT+GYP3yfKyNNSXaBf54L3mzG+4 OsywJjoshzyC/yfyZST+EduXDeAtNGLTNyadHeyQLCZzRRxqppRz8VH3Ma2DhIQv9gm7 uzSbNn+D/+zNG6mJbN+9YlFs/gQxVdwkoXj2rBeH3NQb6sxBV3MO5SQ6ieTSNpLEkvCW MeW0pwfjMcElgPxYf3cZhd+aV4tytvm/K4h4CloADNSgO0cf1UaDLKyOW3+gBqhEPo/d xQOA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741618360; x=1742223160; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=dbqILCM4sqAsqnZWR3n0haWRm4rdBukz3/+QEOiXjzw=; b=mhdKMjp0P6IHK5ZHIFmMbsgmEO7aJoCEfyvF1yrD9sA4H94iaxPObxKSTdnjgjBcbp DdyYot8/P3aCJnFd8fOz3Meow2KAd+Ecv8VxOqL2SDqn3yn9rejBprhYCOUb1JAWbjrm eCcBK7OOqlafMDVqbrMTIuR5SwqaNg1Tnwuo2m4Mc+Ca6uBOYcr7TxIlE4DjefGBrw4R mFuoIzb23doTOg+qA51xScAcVT06zfJuYxIH5NGakH3bNU58ehkFUEiA/ZDZTXO3Rsxm czeI7zz9rF2g5ymfFy6hU0YGXROTlpZCga9OxK/NkkdXs1ZzoXi3Z2WfsEiJYTGDdRqG DQqQ== X-Forwarded-Encrypted: i=1; AJvYcCV57JMUR88wDC/tWmA0kX0Hdmb0XJdz+hLJkL7BiwZ7bTc7jOFCPGuL9Lq7R70J7enKCMboRBoCJqNVOg==@lists.infradead.org X-Gm-Message-State: AOJu0YzdkI6Ad/UyKmpLAqLgCMG3Er9plh8JfW7udKXzgWgTlByNXtlY kNKspTfXnSJfWAvI+m6uUUo/HRfXVA0+orkWmBtdiB4GRIar7X1YY8dLvdL+7qo= X-Gm-Gg: ASbGnctPI0FJGqus64Jq1yiie5qCkYbfSF2t4RRGnv25OHxMigP358C7fQjyszTSX1w jxNOJoMb9Fkp16776WvffPfFACEBdln6MvMpf19BqLkr4voHWSfdoWzAKOyoFlQOP2S5P10YQ9J zOhsTPbUnNFWC5aPtkEtYiMTygGaQq6BKnsroSYHVnD4rlm8o3RZehFbEkvxK70vfuYvPV9Wwlv b9xTVMpz81wLJ8IY0wEam+2cepQney1Xp5QMmQHgaFMJhk3uv5EC7Drml6DlydLI31bqVkjYM47 7GgKTrwRm7xK0ulyZe5mZ6hb5su5GbuExCmHdyUB5E/GAleNoM447H0= X-Google-Smtp-Source: AGHT+IGingfz3mCoLXCx4AWjWHsD50K0dzh2NK7v+KMvVV2K5wL6QO4M0vSkH0GvDvftQkvXnd4PuQ== X-Received: by 2002:a05:6a00:b4d:b0:730:8a0a:9f09 with SMTP id d2e1a72fcca58-736aaaaca0fmr22178030b3a.18.1741618359953; Mon, 10 Mar 2025 07:52:39 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-736d11d4600sm2890275b3a.116.2025.03.10.07.52.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 07:52:39 -0700 (PDT) From: Deepak Gupta Date: Mon, 10 Mar 2025 07:52:25 -0700 Subject: [PATCH v11 03/27] riscv: zicfiss / zicfilp enumeration MIME-Version: 1.0 Message-Id: <20250310-v5_user_cfi_series-v11-3-86b36cbfb910@rivosinc.com> References: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> In-Reply-To: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Paul Walmsley , Palmer Dabbelt , Albert Ou , Conor Dooley , Rob Herring , Krzysztof Kozlowski , Arnd Bergmann , Christian Brauner , Peter Zijlstra , Oleg Nesterov , Eric Biederman , Kees Cook , Jonathan Corbet , Shuah Khan , Jann Horn , Conor Dooley Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, alistair.francis@wdc.com, richard.henderson@linaro.org, jim.shu@sifive.com, andybnac@gmail.com, kito.cheng@sifive.com, charlie@rivosinc.com, atishp@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, alexghiti@rivosinc.com, samitolvanen@google.com, broonie@kernel.org, rick.p.edgecombe@intel.com, Deepak Gupta X-Mailer: b4 0.14.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250310_145243_668943_F97905C7 X-CRM114-Status: GOOD ( 16.35 ) 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 Mar 10 14:52:26 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 14010327 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 BDEACC35FF2 for ; Mon, 10 Mar 2025 15:42:33 +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=heKUtBxp5cOymv rV4XFTgyfO3ZhNG/ykzK8SZKs+yVVcO9jLPpIeEGuc8dMZGNXq1fhI+TZdpGtNILcGe86p5u9brny iOb3V+C4BSBfOAGvS+Z2JowiRAJUfASJeKPGFci/2CBkR16CrsTDkBPgnkE47PbWvJ+xbGCWoC7u7 nCHCxqjQgaNTvsGg7pVDaJCtVUPfGHsBjCU1dSRHzk7WnvsbI2yCBC10IVtUccwC+iQrZ/rpPJngV C+KB/ib90EAwFXCFPOJdDEwAydpp2otoDDvg22xgeh7wjTxbYPQED7/2EyaWBtOHiATLn9vX4qb6X RjHn/r4Ot+qvIVhCe4zg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1trfGs-00000003DFw-3y0i; Mon, 10 Mar 2025 15:42:26 +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 1treUp-000000031Uf-2SpQ for linux-riscv@bombadil.infradead.org; Mon, 10 Mar 2025 14:52:47 +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=n3DM8zy+WllPP0TujEXFZ0Ycc0zGrpNRrMRWPkPmGZc=; b=KBfuW6c04koz/zajbUFxA9X7cq t+VWpG38rP/b2W6dHbghYfFgbxNL3IX4rhnMYjEQ2ov5rFtfNgQ5Uu07rJ4Ob7e7CgUirw7kuf7WR SP8Jk09vWwj3FJFhkXZeOEzEs/1bvWMIWn/Q2kTg7TSgNemcDhKh7CIN9siFofTgaFuHbaa1xBAR9 VI3OmLk/tU6uHjYh8+/JHqxI2RJFTYK8I/9/mv4Nbym46QXyb8XkRwaSqVxPJ42Qw6PkSO06ddDF4 5DpgbNBcHrWzqiP7aSXd71ce6xCww286o5A8/g+r31p230u8s5igWkVk6sG3hNmGEKRb6poATQhxn qtbaKy6w==; Received: from mail-pl1-x62b.google.com ([2607:f8b0:4864:20::62b]) by desiato.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1treUm-00000001sEv-3TCw for linux-riscv@lists.infradead.org; Mon, 10 Mar 2025 14:52:46 +0000 Received: by mail-pl1-x62b.google.com with SMTP id d9443c01a7336-22355618fd9so78425085ad.3 for ; Mon, 10 Mar 2025 07:52:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741618362; x=1742223162; darn=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=crjsm9mYsUMR+T8LX/HeNAcv7LogV5yOUemA56Vkez2pdzcmNAlq9h3I369LIMtsqt SvlDIMX3qn0sPcQ4xnsN/QtaA0RZMNGjsWTi7a/3GHPJG+0/bgWueMsNw6tiSY2ybdIO 4nJ+9wUyBmIbyOksXIxsg+h9hddGHpHZxt1MhastQZ9uDRb8y+53mCkxYSUPb/Q4R6mJ aYkTcoNeIGjmJkAoY3Wt3a+Fs0s5D1iy4y4u8VjHOb1C5V6UqUd9Zt/2GBwVRh9z9Jk+ nAUykc/c3jdTcD0ZmIi6ZQJQW0LKCwbeAwJhjDQ6qQHKMNZvVDVHjvCbPbHx3r04Ss8z hHYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741618362; x=1742223162; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=n3DM8zy+WllPP0TujEXFZ0Ycc0zGrpNRrMRWPkPmGZc=; b=ao529opInyIRuET44ARjlTMhFy7mCEDo6dW1Rv3w03Q61ebDz4thfXU9t/ywFn2F1+ vcykqIg4Nzy0E2D1faPmLlSqDKoVAlmvKQw26+u0Yf0fZERXOrKFv4B1sjwyMxUvohVQ MAzsvBuZT1hUyE2SrXzeFjlHFxEaxtarFn0kO6BtiKou0rjVB3dpS8yj70k0puk/Tt8s AUZUookjoYs5C8QGrQuxBoonag7v0x0tntw1Bq/9kF77KtPz3tzCTKxJBUhAU44ll5vu SfHtJvZqel4finrKKqen2mW05ExICDc53BOqg0YAQQfhi+5e4A2iFhPrLtNKYHBSBE9y zVlw== X-Forwarded-Encrypted: i=1; AJvYcCWbFbngmx+GXlizp1JP6VQyFOu22nY82UgS0E+Ypf1j6UIzwGRdsTvOtRgVFAL+kEgolpyxa+rocEaqcQ==@lists.infradead.org X-Gm-Message-State: AOJu0YzqK33LuZQtMX8+rYriAXxWLk4bXY0JsyzuMAK42vTL7J53vIRa JSMpoR8CY5nZqoRNwVNpoFm9pgxCm9IabkF6BeJVLAigBwMkvmrac2YnmO86tVI= X-Gm-Gg: ASbGncvDbz+1v2izHE/KCSS8OPjQSZkKuHqAcZMFX0pjv5YivNrqeRTcLOMzCu3CwoV 3V/yChiuO3iSjh8Df0qBIdf2nKGvXg4ewZluYd94DaYd7TP2f4fKls3DrKe1hycoV5Ph/+FXmqp k3TopJMjqtB8k1E5BgPAXGYCLxW/jhLBjooCP3QxLnbgRNQo1aHgik9P7R2Ys836ZJS36oJiH/y zN9rxDQ7BFIZfCwiqQjLXGJixuTSklU5dJPzc6WfDDFTVIPsffppoHQCdUUKsi6lFzcLnHOwMiR XiUv8B/yIKGi7fcTUQVGARJCn9PoD/Hevnz3B4XGxwQWM1lAcTBuBq0= X-Google-Smtp-Source: AGHT+IHEgbV38+kdB/KuKTC/ngiL6naw+4KBErVaQq0wSwONUo1ce4xmBDTiHE9RxDA/IFBRpdqZjA== X-Received: by 2002:a05:6a21:1fc5:b0:1f5:6878:1a43 with SMTP id adf61e73a8af0-1f58cb239e0mr144294637.14.1741618362407; Mon, 10 Mar 2025 07:52:42 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-736d11d4600sm2890275b3a.116.2025.03.10.07.52.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 07:52:42 -0700 (PDT) From: Deepak Gupta Date: Mon, 10 Mar 2025 07:52:26 -0700 Subject: [PATCH v11 04/27] riscv: zicfiss / zicfilp extension csr and bit definitions MIME-Version: 1.0 Message-Id: <20250310-v5_user_cfi_series-v11-4-86b36cbfb910@rivosinc.com> References: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> In-Reply-To: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Paul Walmsley , Palmer Dabbelt , Albert Ou , Conor Dooley , Rob Herring , Krzysztof Kozlowski , Arnd Bergmann , Christian Brauner , Peter Zijlstra , Oleg Nesterov , Eric Biederman , Kees Cook , Jonathan Corbet , Shuah Khan , Jann Horn , Conor Dooley Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, alistair.francis@wdc.com, richard.henderson@linaro.org, jim.shu@sifive.com, andybnac@gmail.com, kito.cheng@sifive.com, charlie@rivosinc.com, atishp@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, alexghiti@rivosinc.com, samitolvanen@google.com, broonie@kernel.org, rick.p.edgecombe@intel.com, Deepak Gupta X-Mailer: b4 0.14.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250310_145244_974674_AEA6C599 X-CRM114-Status: GOOD ( 10.62 ) 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 Mar 10 14:52:27 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 14010329 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 7F451C282DE for ; Mon, 10 Mar 2025 15:42:34 +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=3EuQfIEDKmn5xF hbh+WGHVKdqcbkCXXFvVNri5yvNmamicZriD6UmDp9DU54AjLxOfxEvGoP4qcZJVY5CqU2gbzmm7M ksi9e1QNz80IC5rVRSRVJm/orI4fYPsnFQPf1u1z2lj5tZ7jURPGUbmXyNOh02LAPMWWbkmthKkdU EBxh8sjjqFiQZHvQUdNDmzoFRjeNQ7pTLTanUIVKwJW1X+ETUsMCuHx9nGiKydtiurteA06OYuZGo ACuEMPMd/7HgKMnQ/Dx6Y3Cc7yYrchESVS749oGpYne05ogjtTLm5L/t8w0vCeHlVhK5iLbMINMAX vt9SvmrD8EiuployItlA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1trfGv-00000003DIM-0kFe; Mon, 10 Mar 2025 15:42:29 +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 1treUt-000000031Y6-1VYv for linux-riscv@bombadil.infradead.org; Mon, 10 Mar 2025 14:52:51 +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=XFoiG9nO9GzRbM78aaWwJK+Q/U0QRSToN08KNluZphY=; b=cnKlZ6rwlxzDa3Sw5dsuJZbai/ 37tpesxgJgoFDa41AUURIh35BKEI/sz16Rpr4A6/Pc8L66z8t2WetOnTdsNCuEZeIWTJd9YrtG9kD U+GP4Vg4ly+ADOmnLg5WKjTbcjr/je+fBOLC1B8aLTPCm8s0hp8GScltBZ2MXNsjXsKfPUQovfx4U aRdzlvyEYJpQm+fU4fHULNhU33yvGnZcATzpH0S5W/XDsjxspdhJDAAlZyoIJNsrjE03Jq/RTMB+d /Mpz+b3EIPShsBj2mQWrphzZAEROg2lfPDM+jZi/vqSdTMEg595eEK/wGNImgHvD78sQxiceUzWGd l1ginz7Q==; Received: from mail-pl1-x629.google.com ([2607:f8b0:4864:20::629]) by desiato.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1treUq-00000001sGs-2kF6 for linux-riscv@lists.infradead.org; Mon, 10 Mar 2025 14:52:50 +0000 Received: by mail-pl1-x629.google.com with SMTP id d9443c01a7336-22409077c06so10321105ad.1 for ; Mon, 10 Mar 2025 07:52:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741618366; x=1742223166; 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=TsLKamSXCR1Jb7oaGy89pyhEiZDW9NJvOXpEH2dibhLBI1K3hrlAccq4fC57lqPeZ0 yD2OGjJi3cjhS13J27E/fvZF+3Tc0OrRy7yfNj6MUvflSIhxOq2B26vyRWZ8uThgF20q eQHlU12ezJm10kZ7SFImyTIWYl2DDbiBhPjH4F41XoSjZJgbVgpOjIabxdQbunmRH393 m2Agnu/qa/c04QIjlnrOkhO/OHBk1bKNiPiMevWGRbK+gsu6uB9X178r1XeEDCQCZCNn Kl3PbtBJogboAzjgWAEn9QITaUTGlKIT2yXpUiwB+poGueahOZVUEiCCg7Ny+cpkU6nc GhUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741618366; x=1742223166; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=XFoiG9nO9GzRbM78aaWwJK+Q/U0QRSToN08KNluZphY=; b=n6ywIrE5s0nYakRng6a8CwBtiImcAnm9GfKu5eJDP8uGrKbWfyFAR+kwo7COuFgD+t +PezAfy0oj6olwudZL4bGvLko734pKbpRVBFSZwVod9DXF03rzZfBiZWN8jvA57UTKUp eWLv61JhjLL7ChhtYwuizqDJwVfWGFaKvuepI87flqkIP3/KMakQRJr0p9J6ouS/TXwd 2rZIudOpFCeISTFSAjsLSM09SF52wLM8peywcyK9EE0IVxsz63RiK6kQsJ4A5mkUS7Gc DIZ4YvogRLmM9JvOdn6bTO1XXjTGck8EZ4Qjp/3SEEFvuXVn4wZVL1utAEhiChv8Ky9d oFKA== X-Forwarded-Encrypted: i=1; AJvYcCVG++y69Yz0M0bU/J6zot5JDQTPtJzLLePELO8PFv79ydX/9aYCPYA1pXE/VvjZueiV3mjKus2q6YGBQQ==@lists.infradead.org X-Gm-Message-State: AOJu0Yz+dJy3ie6w6MpMuMgF+oOLWKD8sqMNAu9q51zM2h5u2UWxhyFD p+emtiOCK3fjJnRs/HFQOpaHR3Q00gklR/nJM+hXV1fGmH6PgM7kM1quDpmJuYg= X-Gm-Gg: ASbGncv7bMi1Zd3BU1rLkSxTiJT+wgYVJC2HaQIz5IHwZsSPFazhwv8RaBUAYVCdBS3 oWbORpBzeGjJTc7Fpburs2b4+OkXv9ZZqIF+9Juh3lCfUIYFt7xgxfeV7iw6KH8ZigyEaqOTwNU CmF4JsLxGs/ughSpZoAfqwyOAYdHSJ+FK0+9yVBvL7nipurVp0W47fEtSsIzvvO/fpvqT65cRAH B3U8lCABIRz3OmvMgJPUzCzsTtT1zMiSykPigYr+N27XyoOIKBVgrJVNjlNlNCbHcKKDIs0FOA8 DCjMFvIbHfMuXIIPtnqT8XEYid0CQjqltF7tS+8O2PW1GeFoS5HhmRw= X-Google-Smtp-Source: AGHT+IFBz3aXVDVmIlmZqzUHJQr+CufocIiZAA2+T986OpEaME/vCYGeegLtAo9744J8E/xHkN/qMg== X-Received: by 2002:a05:6a00:928b:b0:731:737c:3224 with SMTP id d2e1a72fcca58-736aaa1e02emr19336418b3a.10.1741618364845; Mon, 10 Mar 2025 07:52:44 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-736d11d4600sm2890275b3a.116.2025.03.10.07.52.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 07:52:44 -0700 (PDT) From: Deepak Gupta Date: Mon, 10 Mar 2025 07:52:27 -0700 Subject: [PATCH v11 05/27] riscv: usercfi state for task and save/restore of CSR_SSP on trap entry/exit MIME-Version: 1.0 Message-Id: <20250310-v5_user_cfi_series-v11-5-86b36cbfb910@rivosinc.com> References: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> In-Reply-To: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Paul Walmsley , Palmer Dabbelt , Albert Ou , Conor Dooley , Rob Herring , Krzysztof Kozlowski , Arnd Bergmann , Christian Brauner , Peter Zijlstra , Oleg Nesterov , Eric Biederman , Kees Cook , Jonathan Corbet , Shuah Khan , Jann Horn , Conor Dooley Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, alistair.francis@wdc.com, richard.henderson@linaro.org, jim.shu@sifive.com, andybnac@gmail.com, kito.cheng@sifive.com, charlie@rivosinc.com, atishp@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, alexghiti@rivosinc.com, samitolvanen@google.com, broonie@kernel.org, rick.p.edgecombe@intel.com, Deepak Gupta X-Mailer: b4 0.14.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250310_145248_757307_6C03AF48 X-CRM114-Status: GOOD ( 22.52 ) 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 Mar 10 14:52:28 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 14010328 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 71229C35FF1 for ; Mon, 10 Mar 2025 15:42:33 +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=mxfWrgatxkuXaH e1wpeZhZKAkfqfuPWUAHIVlXreJ1oQKfNLDUN43BD8yzp3gRk1t9bNfmVeRnZI5+9+3l3gtDAujwQ k5+0npRNvpVn3y7UX12rSr1TtGvxoJBSVhrN8A12XlA4cbbFoXHrgKrc7TS1OlFiG2B1E0cP/c36j 8tFZhd4HcA67Mo3RdKfQXc8xR17oFNfgSpt05b3giBUUMDuPXm9u1HyeJDAK+UIxcT3lZrlWkdt8m 86oxuVC/sR62+mrGErT7M9yoOLg1uR2jwhxrUuoj+Oj6ej6atZfINZlJQwjC8tnG4dauy5bbZR++X JK+RzputheywLlRlwCiA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1trfGt-00000003DGp-324N; Mon, 10 Mar 2025 15:42:27 +0000 Received: from mail-pl1-x62a.google.com ([2607:f8b0:4864:20::62a]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1treUq-000000031VI-0g7B for linux-riscv@lists.infradead.org; Mon, 10 Mar 2025 14:52:49 +0000 Received: by mail-pl1-x62a.google.com with SMTP id d9443c01a7336-22334203781so95781295ad.0 for ; Mon, 10 Mar 2025 07:52:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741618367; x=1742223167; darn=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=sxSpGViGYe/5liGgu3I5SaKy/x9jnpEQNZZQUjK9PKpa4ipr/nPFS/kEKo1gNPIlYx BU3hMuJbmJXkx5Poy/0xav+F3Cx5PdQg/yJBxJMtXYfH1hBt6RdwDTHaX3pK+NMDbzDx T1YfYnwV/D4XJE3akfsy6kL1r9oS1XBLnNcH5eM0ZFJbAvbGx5JWlnbBRfiUIOkobuRA Cc6eJs0ah4APr7FEZGmStR/ennWG6MLGG4+9uRMZOYZtSpqLhMHXe6NYLbGefCbrngaf HE24T8ucEgzZ6GEujXmWemfWwqimzEzu9C7ba73j+d9WZ4lIYtnZksEfusZLgwABacXb /GlQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741618367; x=1742223167; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=1e5RgGuqZS+AKw5oTSgp47pWFJ0GDjWFc7jT6Xd5TJc=; b=u5Mjh6dZbDydumvSzMBKNlLuLNBdftFvGdCMKc85E+YYTtE6/jLzxGa0pzcpYTMpI6 08r5xLDkK0OEBLG+C93Bv7+lRLH1H2urWIgh4CPTBZUABV55NLafp0p+f+KssInSiKY3 vv0xI4JkjPXyCXqI8piv03z3rXEMz/mxOUex3pksDxGipiOBsE6eqVJWCiBDs1S3LtIb tY7gsLfVASPqppsBYR1H0P1e/C/PXePYl43X+302IEhukG3y8b+Uge+2m9BpuAFCkvqd 3p1BVlrH2pvy3C0F5SEA/VjT0YEDF8yX56QdAEYVWr/Q+mnYtz5kD5zckOb2kuGAhW6H sy3w== X-Forwarded-Encrypted: i=1; AJvYcCUnIYYbYgAE5SxEPaWWbe2tWXACRttSrVP4nc3Aqgl52ViP9+v2rzs5gbKlgVyQaGXDQYbIHMXYU9IahA==@lists.infradead.org X-Gm-Message-State: AOJu0YwwCRfBiXEbI7iVrCsdiAg1lWZz6ZzNwBAQv+PnTICFs+oNNXVg vBn3HprNWMMBKfMIIZfpgy+PVm1ZUTpVEnt+b202BdWMVXRcTiyJX75YPm+u1dI= X-Gm-Gg: ASbGncttIsV2xWYOaDfLIi4Itx53dzjJeQPW9UFlSvoKMj/3Df/ZUYr3PE3WOraaUlv BeFDuxajjzYH2vkvZtwlloAI/ZE5sgvvZLQCbDbMsZEehQo74j3ELHhdoCECEYvm7fdnyjjXAQr 4feaVQCX4xkL/VLztT230zYOx31lmH5wPbqzGSavKcj8YhzusoZ5VdPYzj8sj4MJ+nrOKgBtaYI Vx4hNcj54bdgjZMvMCfDZTolv8/GPUx+GIYD1WC+HPOXpQ9rPPIOeVL4CCdmnWlifYvdpZ91MhZ jQI8z7NVhEZONyxAxbXTZjpqaGhCNt8VDel/lHNli8fa0csDgX/SIYY= X-Google-Smtp-Source: AGHT+IGncZQFemH+Tqb/7Cg0gaGakZGYufPeZUJHTPz51xcsHifa9ayQ0v/V99iUl8be0Qgt8Ur5MQ== X-Received: by 2002:a05:6a00:8506:b0:727:39a4:30cc with SMTP id d2e1a72fcca58-736bbf433d9mr12618993b3a.1.1741618367246; Mon, 10 Mar 2025 07:52:47 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-736d11d4600sm2890275b3a.116.2025.03.10.07.52.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 07:52:46 -0700 (PDT) From: Deepak Gupta Date: Mon, 10 Mar 2025 07:52:28 -0700 Subject: [PATCH v11 06/27] riscv/mm : ensure PROT_WRITE leads to VM_READ | VM_WRITE MIME-Version: 1.0 Message-Id: <20250310-v5_user_cfi_series-v11-6-86b36cbfb910@rivosinc.com> References: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> In-Reply-To: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Paul Walmsley , Palmer Dabbelt , Albert Ou , Conor Dooley , Rob Herring , Krzysztof Kozlowski , Arnd Bergmann , Christian Brauner , Peter Zijlstra , Oleg Nesterov , Eric Biederman , Kees Cook , Jonathan Corbet , Shuah Khan , Jann Horn , Conor Dooley Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, alistair.francis@wdc.com, richard.henderson@linaro.org, jim.shu@sifive.com, andybnac@gmail.com, kito.cheng@sifive.com, charlie@rivosinc.com, atishp@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, alexghiti@rivosinc.com, samitolvanen@google.com, broonie@kernel.org, rick.p.edgecombe@intel.com, Deepak Gupta X-Mailer: b4 0.14.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250310_075248_195244_C505CE9B X-CRM114-Status: GOOD ( 21.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 `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 Mar 10 14:52:29 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 14010326 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 7AC8FC35FF3 for ; Mon, 10 Mar 2025 15:42:33 +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=kLW4Royu2iuADE wiZCwa8u/Lk/d4XvwmVaf/gULg70HQdG5Qij8ClY2RbcoDGkjkgCPqYzl+A9ti0I5S21ERgv9fPL/ IVWZz5dRMN1IyZ51heJmsEw07CjZ1qVPYOK4CWb3ZxB3A6/iF9mbZCBiZHUeG7H92SBqflzclwsdS HVXUCI+7A2PxZy+2B58nt2WcRgmnQR1ZgU2noR42FQ+G2XQfw55Gb7RUiF4E1lb8Rt00RTXBTcMve vToEjjzQeto4+KK/+GxVujAW7taPvB7E6anXAmVzcijq3K7dZDv4q3jSOE46cDj1dTEWsC/WQXmso PXJF+Rk8eqX/MAEjz0xw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1trfGu-00000003DHT-1Vfj; Mon, 10 Mar 2025 15:42:28 +0000 Received: from mail-pl1-x62f.google.com ([2607:f8b0:4864:20::62f]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1treUs-000000031XV-1Br2 for linux-riscv@lists.infradead.org; Mon, 10 Mar 2025 14:52:51 +0000 Received: by mail-pl1-x62f.google.com with SMTP id d9443c01a7336-22409077c06so10323345ad.1 for ; Mon, 10 Mar 2025 07:52:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741618370; x=1742223170; darn=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=lfdsqAjmJq+Ybz92YJxLiu4md3VoSA6Xz0oOeJ1+siKVC1K9zbemmnO7gY48cLAAm0 AWyNllXFKr0xfPFL6pRGthpYOUH0Wt4YXaV36XJQoI9bYUviwC/gAaUZrje0xORnjb/j ZK+oXn+s0nGuViOxLsTE4byV6ybmTVAzp750VlRfbuODwReodAImJ76ZRPouK6uwVMWA uZx2nMuDjSj0xfNz2KdkIi2igBtYj/Ul7AuEwdnpMW+9yH5Hxwr9gXQI6UeRKBRbJHga kVVOay7EPKjJg4/HpKiqwxbE72KraAbXOyV24rUHJr0mEaJ2kRuAVHHPPwS7iDI7vFeS 8D1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741618370; x=1742223170; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Bi056oDr44X6mEA5ptOBWamFE29btTVHHp5F60vYQ4s=; b=TgiKl/jBHdBmxoCAXioBf+SXOjKHBbYiACdBO5nUp+YLwng1j0z3WnWJv2E5CWC4Zb Qc1nc2/+YESbppo+F+Hs9MsoMmV4vuAOn05d+mnc9POt+JrQ8XmApYfpNwTssYSRWL1h 4T6JqDxsVPOzMk1IOnRj1w5UByJCYdJf8Jt6H/E5xxDMBn0+o6ngEydBHjwxfyoIlRrW SumKAlw6NjWmmhYvvFNp9LeL6/WyLx2skTFC91lAWiSGvyCVuDjMXuBNvMFW1SYhuwHe +C4vMsh0ESgq5A7BIrbZXU2O8zS4ZgmUmUCF5IFT01Y5gFXU7Ipb9/TS8xFaZNVxwN7O 21nQ== X-Forwarded-Encrypted: i=1; AJvYcCV4DvfKmubl8CX5RXKw24CZIRU9/RYNjgfkGryObVr0PnnKfTyOzvQHkpwtvPCsKdjU1aF+mklcL8ed8Q==@lists.infradead.org X-Gm-Message-State: AOJu0YwCgq38SRKKth0hUthmxh159+6443CGWt5Bf9jWLHbhIm8ohfrq 9exHYHjVbOcmpUO7k4Tt71PmSk9ffKDO0wTfdbOZrkG3324NySRedsrHiVT4SEI= X-Gm-Gg: ASbGncsu/kjMzqoZyC4ASz/PRhQpJ/VC2VM6VJORfdoqOs/zLJ47dZACxcveTwFQRUw mH80H7Xi2oVxBZlSqBAO18Ou8xm9xDruDXVWcrNcYctdS8ImCwg7NPPhfH57YF9EMF/hBQA6j8w qXCEHbJNi+aniX8JFXawPGnG8qvyX9ckG0q1owABvO7mXgoQIfRNQx+0V4hWUGrOVzGo31VFsCJ 4MwCbbDEI93cMgpzcX4kcopw7klsj40Hhi0RI1aGIkcJtXYPbUTMAmRU3ln4py+CaYyTHwtzERX MyyyfzysFjS2DqFmiau5wc5ov9TrItf/ThUgXBWnuDYHb7LTHiuxU5Q= X-Google-Smtp-Source: AGHT+IHIjPCK5/0EhbZcTRnURyZw0NTj2K+W71g/3+djiMGOR5iYaRFLtirQywnmf6SGr6Tob6ydqg== X-Received: by 2002:a05:6a00:218c:b0:736:562b:9a9c with SMTP id d2e1a72fcca58-736aaadf10emr17282132b3a.18.1741618369717; Mon, 10 Mar 2025 07:52:49 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-736d11d4600sm2890275b3a.116.2025.03.10.07.52.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 07:52:49 -0700 (PDT) From: Deepak Gupta Date: Mon, 10 Mar 2025 07:52:29 -0700 Subject: [PATCH v11 07/27] riscv mm: manufacture shadow stack pte MIME-Version: 1.0 Message-Id: <20250310-v5_user_cfi_series-v11-7-86b36cbfb910@rivosinc.com> References: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> In-Reply-To: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Paul Walmsley , Palmer Dabbelt , Albert Ou , Conor Dooley , Rob Herring , Krzysztof Kozlowski , Arnd Bergmann , Christian Brauner , Peter Zijlstra , Oleg Nesterov , Eric Biederman , Kees Cook , Jonathan Corbet , Shuah Khan , Jann Horn , Conor Dooley Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, alistair.francis@wdc.com, richard.henderson@linaro.org, jim.shu@sifive.com, andybnac@gmail.com, kito.cheng@sifive.com, charlie@rivosinc.com, atishp@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, alexghiti@rivosinc.com, samitolvanen@google.com, broonie@kernel.org, rick.p.edgecombe@intel.com, Deepak Gupta X-Mailer: b4 0.14.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250310_075250_329028_7D6C17BC X-CRM114-Status: GOOD ( 10.42 ) 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 Mar 10 14:52:30 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 14010332 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 1A945C28B2E for ; Mon, 10 Mar 2025 15:42:36 +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=EBvBiWtVmGWSnP unYFgqWR+erpUQB7KisU0P8ZlDhNRckKtpNXu3gjNTdY8PoQHkefVis4i/hOYVej4tKuU4srg+vyl m09hehsbs2QMX0fTDTIUPpW168ekR8ETFobyEzAPVB0KlPgVxY5x97xsBUJ5PlN+LW496F1vepqo3 dRL14YfdqEoVMBTVrlMkORvernIzeWBJ3+Q9yDDhUzL5fjfzTHODVBrDnHQYjugHFvram/oLPqyz7 zl3niBBDC2bqN0ptxsDn0whSiHV0wmThRHHNm0UQeLbSY3ZU9VIwN0b51gupCcyjMMOiA8nXXe39R FrbORJnSZ66PQEmx3Z3Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1trfGw-00000003DK4-2YW9; Mon, 10 Mar 2025 15:42: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 1treV0-000000031cK-2xh7 for linux-riscv@bombadil.infradead.org; Mon, 10 Mar 2025 14:52:58 +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=WPde6NcIQ47rHbegQG8dPMP+orPth983zEAPX8ktkAY=; b=Ku1wrFl09+vCQvUzwTAs5dGRjG Siizgr0QeoEXBxmu9wuFAY7G8bRbWTvLQr29OlhtvxHtJqupm5E/8OSFJyd1tBxKFI6vY787W/+Ex KYemjPwO/UVpsIgUvy9zorAfO6EcDEullibXlucLt50Tv/5H5Kfc9q8QbZUAnTkR/ZcL08BIw3SoI /aLIpttIVjhAOj+8dz6IIQnesO+T9qYXSikRQCPvewxQ8kWDAY3pbKHzZDKHE1L8MsSXIqSto6TQS wPobOyiCzyMhidVbkMiDCMg652W+y15yt4oGIcww0f3V3pOKQCG2NY/fsVdm4dv02aNEsAJEwX/nC f0Teiicg==; Received: from mail-pl1-x635.google.com ([2607:f8b0:4864:20::635]) by desiato.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1treUw-00000001sJd-2IOJ for linux-riscv@lists.infradead.org; Mon, 10 Mar 2025 14:52:57 +0000 Received: by mail-pl1-x635.google.com with SMTP id d9443c01a7336-22398e09e39so78269235ad.3 for ; Mon, 10 Mar 2025 07:52:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741618372; x=1742223172; darn=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=YtYxhyGWlo+plHbsN3iKzhkpMLV0bE0Xeej+rlhoKoeCBDgHSXU9w7caJ8/7mxd9Pc frXDqOCUWt0I34YUU4rCazyi+b9lMeMJ56N6gK1jhHQtsHUwLxrF7GWzzyLIxiOpMljx KmM336VFlX5I1F1/9ZfcgO47v5V6OVmNRzeZ8OFIARdbGdJ32RHCj/rt9UfKwqUgWI8h EQYjEVWSk22YxcaHKWshGUe2rfEOgpCJ3R1gAo/SBrLJpLNv8W6UhRGQI7pQcjfX0MdM LTssE/leMufewOJO2LgtXWX+giuRdSoSWjxzC+VI+PkisTd705S1exOt//drnX1Si1ev /SIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741618372; x=1742223172; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=WPde6NcIQ47rHbegQG8dPMP+orPth983zEAPX8ktkAY=; b=QkJqfnWamIsQmQk/poVq14wnJZ77OPDHQPTFVfYBJ4NM5Ise/1okiAhzJgkPjgiqF3 5Xt9J07Rkl9u7HgknLJ4tHyCOJo76ZLr15CLcjpcvd6QLWOPplx5u0tGhYWnboMG5ImZ Ol47y+iiAKaA7n/XaI0qQHG5Ws+oNkRfLpONvCu8mLKNwdQDYL+8Q6kexGPTo5m+70MK AhP4jJSmlActg7Owdz0kLxvkuHDHnBSDtfB6+f2HjVXlUjgL511W5BnntwzAclQWBgQA nd7ZQ8ggu6jcXVSoH3dqV+XMsgyzm2SzvrYMZnlp4VdVdFTkp8cHRexltW+x+gdyy0/P og4g== X-Forwarded-Encrypted: i=1; AJvYcCX2k0GUs20aVsdNqTzJM1USZ+kAoVVkvkFILRH9lS2Rqdtb68sJ7/4cLM4zmKYqI9xEqaWaJ71+iGPJuw==@lists.infradead.org X-Gm-Message-State: AOJu0Yw3ph2apT4eJ2WRVuFPuHtiupHsGM+iNz2z9NU7fYNJ1cpZ3ve4 YMABfYaGoqzLwM6HUXU7ldhSG4siZx1GSAUzJhJ0lx0J8q8ImhtCBWsEuVTpkY4= X-Gm-Gg: ASbGnctLfVv5J98dy/ZVo8Gm6TBOCjopl1VxGl7SuXWuW9UDrVOcb2N3NhgksG+OVYo 8petXI++PCTdEwhWzJu7xdQkmJfQIF+BJm06Qvx+MYiNCKr19CSUF1Y/tQt98hoGUUuDCZndlX9 9dnlF/iGi7y9O7X83ouC6jjm82Bx0bq9Agi2OGjdnaZHyv58USTS0BxV4z8Tr1c3iaVkpudOiO6 akLAD+TWUQv6WY1bX2dBHAdoL6uLcdbfQJHlBdwRJ0G2azKirIAJPj9Yg7uY3e1kqAfBINvzIYk S3aFTDSmQv2DWAUkHxrhhnJOLMxX6oOd33qJG2e+17XbLJDngbkCCsA= X-Google-Smtp-Source: AGHT+IFqTxU+p9IhkI8RGdvHdyJEHoK+vlH5JWBOUOv0UhGLOT20CpZOV0dtGMVo/7iEBeBe/MTApw== X-Received: by 2002:a05:6a00:928b:b0:736:4d44:8b77 with SMTP id d2e1a72fcca58-736aaa1cf9fmr27006391b3a.8.1741618372105; Mon, 10 Mar 2025 07:52:52 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-736d11d4600sm2890275b3a.116.2025.03.10.07.52.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 07:52:51 -0700 (PDT) From: Deepak Gupta Date: Mon, 10 Mar 2025 07:52:30 -0700 Subject: [PATCH v11 08/27] riscv mmu: teach pte_mkwrite to manufacture shadow stack PTEs MIME-Version: 1.0 Message-Id: <20250310-v5_user_cfi_series-v11-8-86b36cbfb910@rivosinc.com> References: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> In-Reply-To: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Paul Walmsley , Palmer Dabbelt , Albert Ou , Conor Dooley , Rob Herring , Krzysztof Kozlowski , Arnd Bergmann , Christian Brauner , Peter Zijlstra , Oleg Nesterov , Eric Biederman , Kees Cook , Jonathan Corbet , Shuah Khan , Jann Horn , Conor Dooley Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, alistair.francis@wdc.com, richard.henderson@linaro.org, jim.shu@sifive.com, andybnac@gmail.com, kito.cheng@sifive.com, charlie@rivosinc.com, atishp@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, alexghiti@rivosinc.com, samitolvanen@google.com, broonie@kernel.org, rick.p.edgecombe@intel.com, Deepak Gupta X-Mailer: b4 0.14.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250310_145254_606822_285E0ECD X-CRM114-Status: GOOD ( 11.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 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 Mar 10 14:52:31 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 14010331 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 A9F2AC35FF4 for ; Mon, 10 Mar 2025 15:42:34 +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=bPDppx0Q6jf8+U W3l+6R0NGQevJZKanEnF8Nyg96IppblayUEPBjsDKmVC0EEIZhRFSczrk0cAPat3b0WgvCOYI363/ Im8LEXgChWjq0XcNFemx83dSXonABLN5824Ltrv2jGQ+ATxqlyyp8bAqMChwrPaE/DWzT42cADBtP 7x+6UQhSl3IVv4qXBMNKWp08lQELkD6wMqNCjxnqXGUiRCkS3RQYzeXcNaQX4OoDQb1ERfpesos9d /QwmHcLb5afzhS6AcOYPEH0ONlEaLiQgh30NNP/Cb1jg5QCsmZ58Sn8wTVhySSZ9+de9WJENHqV3z 6uKB5K3dUc7NXuqf41MQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1trfGv-00000003DJK-3oIl; Mon, 10 Mar 2025 15:42:29 +0000 Received: from mail-pl1-x631.google.com ([2607:f8b0:4864:20::631]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1treUx-000000031aN-0EYr for linux-riscv@lists.infradead.org; Mon, 10 Mar 2025 14:52:56 +0000 Received: by mail-pl1-x631.google.com with SMTP id d9443c01a7336-223378e2b0dso62374105ad.0 for ; Mon, 10 Mar 2025 07:52:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741618374; x=1742223174; darn=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=MdVFnPsj9YqO+Dg1n4bibvFq2GNtkQbT7p691qNUTPjAFJszpU1MSNFVTSE8OB/MyH FYoSet6eDrssJp5eHkLV9+vqfFiYOZ1xT9LD6/BsEDj+racPGmhOpfJUK5AObbx7Sm5M Tj2JTvvR4kI218D5CSrMlFu6R4LHbBlgB6UCMq6TeLn2pkLiStnZahLvHRQupMW+gN0j NzThc4JjUjms2rcp7gaMgEi4Mbk3/VypH5D3e8mkZbT7fwussJ6LPHNTbtzDARbVx1j1 gq+QWi07l8/mwRoKmCMqX8Z2PldvLhKsOqcL+6tz3Crfjn0hydUwvZEUhgDTbtqlXamB 0nPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741618374; x=1742223174; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=kL+yIoSlkTX0SokBaTv1DsPGjl4jKUPC0i4GBiCWGFY=; b=R4XWX4lFCsfAKJd5zXBBq85ID9r18x3w50ujtJYaRdqcWF9KxuJrlD5hnsXoF1OPSU q4Z6hLpXI6tDk5nj1XFS9ZpTXifTF3TsDa/CwrE38hqEGriq/PceCMymFLCLozIE4Qp0 2Js/LXszt3NvvFoO4Pyz/VZJ33XosuLAhtDgPzZc8inJlPZn93xe3VKEgccDDeG3PGxU WGBsS0FM2zk7Ulpl49bjZNVD68FHnH4sIHe36BzPkgUtwUAUGgFT6536kc3UCLbgw3+v 2h5SBuT8IORurCeELVCyntBIJSzFibYKaO7Z7mTcOteUjlvyL8UFxpoNYNyVrcPhL6Dg uRjg== X-Forwarded-Encrypted: i=1; AJvYcCUUT/0v6YPfrH+BVXw0QY5zjU4s1Crtjx8p19EXeT1F9On1zohnfEkqM20DeuX4d6D0n2Z3M5nc4qCtmQ==@lists.infradead.org X-Gm-Message-State: AOJu0YwSvkpILU/KrO4UWMRW7m+6cEW+GSxoEy7R4ozCJ/dmrUtOL5uS egNFCNbn99j8EpOZ+BiMdRUzYh7U74aMgu4i4d8HWcuSnUgCIVlgH1+c15tPgIs= X-Gm-Gg: ASbGncuNsNixhPvfnjxsEVmFHxbB5dPfIGNIcXdj9ZwGFX/tgKFosmA2JJRikoeXZji FXIytxWO43mWh3bMfoL0F+BCkPIDxf79TNNB3Em6rNcPDH146gShbqfeuz2N2tZYGqezV+Trlmd BQXrLfRFwYQ9KMfcg+tmOF9LxSl6QgG96bMmV2Nu1tjhdgmNpWqzeVfl+ytHcanKh4q+2npSBGz 477+Zhv1slWWiPQ9AY149ZSVISIh2FXSo61tspsGPliZLEnvqzykOPX6Vr49bUn3ed52VSbXnDY KQqtpxr9bw8eHU83ZLJ9maAN12KSUSwyTXEOjV5Fun8GX7JwJPxmOWg= X-Google-Smtp-Source: AGHT+IFKPuh8cOHYjBgEoM7t8cHgv4gW4YpL9WqheJYopfOZX5sbWBiog25DM4bQ+IO4xPTW/pcKfQ== X-Received: by 2002:a05:6a00:14d1:b0:736:755b:8311 with SMTP id d2e1a72fcca58-736aaad1daamr15723454b3a.16.1741618374533; Mon, 10 Mar 2025 07:52:54 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-736d11d4600sm2890275b3a.116.2025.03.10.07.52.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 07:52:54 -0700 (PDT) From: Deepak Gupta Date: Mon, 10 Mar 2025 07:52:31 -0700 Subject: [PATCH v11 09/27] riscv mmu: write protect and shadow stack MIME-Version: 1.0 Message-Id: <20250310-v5_user_cfi_series-v11-9-86b36cbfb910@rivosinc.com> References: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> In-Reply-To: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Paul Walmsley , Palmer Dabbelt , Albert Ou , Conor Dooley , Rob Herring , Krzysztof Kozlowski , Arnd Bergmann , Christian Brauner , Peter Zijlstra , Oleg Nesterov , Eric Biederman , Kees Cook , Jonathan Corbet , Shuah Khan , Jann Horn , Conor Dooley Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, alistair.francis@wdc.com, richard.henderson@linaro.org, jim.shu@sifive.com, andybnac@gmail.com, kito.cheng@sifive.com, charlie@rivosinc.com, atishp@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, alexghiti@rivosinc.com, samitolvanen@google.com, broonie@kernel.org, rick.p.edgecombe@intel.com, Deepak Gupta X-Mailer: b4 0.14.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250310_075255_101888_BF538134 X-CRM114-Status: GOOD ( 15.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 `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 Mar 10 14:52:32 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 14010333 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 C7FBEC282DE for ; Mon, 10 Mar 2025 15:42:37 +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=eFTg1sMw7zgFkw F0jYkaXnXmfN9gvfnuuplgXbHBsJSxHEBPAvc40EbFNvuJdAn7BqsHCJoaQE9e6dJPaLfzl1N/xQX UXn2ef+k/Zz9pKaXK5VRVsaHz8Hlmo6R2694p7G4WbeKebPNjOZCZgRhGKW+PtTjOSJcUNwy/8cUY yV8C8sIElcVsIysyFedaDZk/610D0IewSn3oItiZi7Eg55jI6QTfE+UPLmKczOFpNo+t6SnB5EBEz 6iPhR9bdqncHCklAzMDIkHPwiWAcZylhJOsTBKUpwaGfJALJq7E/s/J/PNoGFPRUAjuoizHyck/TG L3CLxwC1lCs+YPO9wOTw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1trfGx-00000003DLJ-20ep; Mon, 10 Mar 2025 15:42:31 +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 1treUz-000000031bk-3JLV for linux-riscv@lists.infradead.org; Mon, 10 Mar 2025 14:52:59 +0000 Received: by mail-pl1-x62c.google.com with SMTP id d9443c01a7336-22435603572so41597495ad.1 for ; Mon, 10 Mar 2025 07:52:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741618377; x=1742223177; darn=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=LoKPIx1oF137E9XMBFd68bop6do0CtEFaMtzBCsEPXR7UymM5DVPrBYM4H4BiCkJN6 R6Xsuvhc2iGEOYnd/SQsX5rJUmrH/naXV0NqYOzrpUP7Qn2Qm/hSWrs/kvHyTIKB2s/F NIUnzos9RWRaKozVzhpuc4gamt9QEPJPZnKoNcTiA3NOI4PDpdY7PAUDTiJj6E1yyxBo 8CFmQN/yep/ICUG4OzCZUgSb6lv/d6ojjfwnk0OEKHM7i9xPbOF3bQ8TOLeoLJ01u3ey o6izV0j3C1cmmRwZwjSdCwPplRSscwBlQ+sa9+zNWqYsWxE+M02ecahPCRRjrY/wr0da 5T0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741618377; x=1742223177; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=hK7DlmuIRYzIsuvvXHgsbKnv3WN7JKJpEt1D7vAnJaQ=; b=wOAq43xo2vU0oYLDOc6qs4a6CS+JDPeOQgwiiP1K3ViSy39vP6r395hLHDiK2O/Akl dKIXvDk8lnC9bOP9cYDsi22ZeIrPn7IPpAVccU3YlVvlFQiqaFQsqisQVfW/a7TA7LoT 4GwQ4grfPm36p4kY4EXYA70YrarzyMrw8tr2tpgnW025wTHWaPGNIQ1iVHhPItfYXmm9 IHNYVlcZohqDXnrD3eyuaYYIyoSiLrGszTSwcKWREqzhF8rJNfpiIIdFjUZanKYaHN6b bQ9hOlgi4j22K/TOBkMu+HGBFpY/zhZwmjnSLVtLOHuWVPhX2I7y1KRSS6okfraNZJCG o+zA== X-Forwarded-Encrypted: i=1; AJvYcCVhpkwasgF1Zhyhful5e8nQFOiQdtog2PR945WQjreQswV5aB6PjRShFZAIyVJFKqsPn4y3IAhR5qdc5A==@lists.infradead.org X-Gm-Message-State: AOJu0YxsYZtdcRAT1MB8RIDokBrDnESIb9qVjpfwpN/39P0Ts3aYo4pZ Aev6nb1KJfItLyJneii28vhlRU7kESxYUzHSTjZLCWeIYoAItntkokX7p4MYVNY= X-Gm-Gg: ASbGnctSeePsiH+9nrOG8Nr0x8n9bRLhJHZ+ZIpOmifhkGibAZgVxljtZVi+M28OlUV mouFAphhI1gnOKzXamKHQhQ3TvmJHVNdm7nbYlOwuwAt6UYXDMAp0cPX0If2gdfjG5nJvB2G/0X bkVWdtY09HKhpYbMpZc15sVdnYldJEhy5fO+JlsHZsuWmePVIIsnc7MOZjgvS+XkO3ZXMA+PpOY 3Vn38XxhzRN+65HvHGvuW42si6d0B3EDlZ0Cwbi1AMXVmGtH6oyU93Z1JT3UUI2xRnsHZzjAigb RLKClM7GHex8Q8JotxiMvvuakRSDUHu7lZK9RZ0HL4uPifi2roAiz2o= X-Google-Smtp-Source: AGHT+IGqKWX94qO9AqEOI9XVMPr9YtNG73DineLHU1SUa0qmkdl6iHIBj3KU2DOsCD0Dz98oJGoQVg== X-Received: by 2002:a05:6a00:b86:b0:736:50c4:3e0f with SMTP id d2e1a72fcca58-736aaa373b8mr17095202b3a.10.1741618376979; Mon, 10 Mar 2025 07:52:56 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-736d11d4600sm2890275b3a.116.2025.03.10.07.52.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 07:52:56 -0700 (PDT) From: Deepak Gupta Date: Mon, 10 Mar 2025 07:52:32 -0700 Subject: [PATCH v11 10/27] riscv/mm: Implement map_shadow_stack() syscall MIME-Version: 1.0 Message-Id: <20250310-v5_user_cfi_series-v11-10-86b36cbfb910@rivosinc.com> References: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> In-Reply-To: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Paul Walmsley , Palmer Dabbelt , Albert Ou , Conor Dooley , Rob Herring , Krzysztof Kozlowski , Arnd Bergmann , Christian Brauner , Peter Zijlstra , Oleg Nesterov , Eric Biederman , Kees Cook , Jonathan Corbet , Shuah Khan , Jann Horn , Conor Dooley Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, alistair.francis@wdc.com, richard.henderson@linaro.org, jim.shu@sifive.com, andybnac@gmail.com, kito.cheng@sifive.com, charlie@rivosinc.com, atishp@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, alexghiti@rivosinc.com, samitolvanen@google.com, broonie@kernel.org, rick.p.edgecombe@intel.com, Deepak Gupta X-Mailer: b4 0.14.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250310_075257_825661_3F011A35 X-CRM114-Status: GOOD ( 24.18 ) 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 Mar 10 14:52:33 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 14010335 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 5C52AC35FF1 for ; Mon, 10 Mar 2025 15:42: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=js2Y6rKRWbZN2EBupnuiQxmO53R9yz977a6iMfeU+dg=; b=A5WPqqZG6Pn5ys CShwOepNTSr3YSXmZEv2bA78OUkOYu9oZCp4ckT2aXpKB+bMd8Kj/q4jDmKh6OxqVK2oA89nif80z OLLk0BwEI5KkvuLUp2tgX92gZigbUrh/ibjLk3IJBH2Rnv6Y+WqXhcLHAMDGUZ/hSURrpj2TGVMlo /4uhJwIs6yIP/mlspvUu5omwxcI2K58sJdSHjbG9dD5HcsxDLWtNoxpAXvFpgF8jCAo3nrqQWKm3J 9lhHGfahV2Th+I6EoL+qg/61rdp1EpBPnGMC1jlaGee0hRHBNnMkzyR8LlyvluPaZVqe2ntjDlACp OQ8R9iK7mYxAUcsTgaow==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1trfGy-00000003DNj-2yFY; Mon, 10 Mar 2025 15:42:32 +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 1treV5-000000031gD-3b5z for linux-riscv@bombadil.infradead.org; Mon, 10 Mar 2025 14:53:04 +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=42Qh4XiAuZulv75xzl4kByDL6UOmUjop2oq2YsBLVIA=; b=KD7GTEa/7OorxI/o7K+/l4bTTr H+amttTkQwfVQBroy2NRMryki3dYfZMrlEUGK8Jyth9iV5SYd8qoTiISoozhzVJkpZGa4OW/l/azd p3xZ20z65pPT8bvAxFBP2y8TK2rcJqWbPOeQoRaKlYRnlAiw/unZXQYxTdvpJ+y1WbvO7MT6d4yok CFqnukf0f5fumIwjXfOf5qUsKaUETQS1Wq6qKPz4X39SplOxLDHTM0JNyHQmtoiyKyizdfJbwpYZE OF1ti2rGhWnUEN6X0D5WSO6Lb1PglJiEfZEFN6dWo4b+hRTZ1R7+dO8t1md4ahAHX+AB3EWSelcwx Bx69s3ag==; 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 1treV2-00000001sNQ-3u5k for linux-riscv@lists.infradead.org; Mon, 10 Mar 2025 14:53:02 +0000 Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-2235189adaeso72294115ad.0 for ; Mon, 10 Mar 2025 07:53:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741618379; x=1742223179; darn=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=fm9bfYGUWhq6oWuR8w5V7M07MZqd89O0OntA9ZTjabddxn14iOXVRn7q/xVGWQf+72 Gnwq+agDO5nP4GF5RNpghro7TOlwXcHi/gIYpZITxMYQcv+LCm858T0ek4ZPXvHfxIIR b3UYVznNYvrxSp1ftipFkthlPEPRc7eM3Dv7GpKJhr6L/24Nv1wCeN19e2pI1EvgwWed mHCsL8vk5JY4OGxMqsY+q7F2w0KD0c2D2hUOvAMbD3/FWeh4/tVlzmQd57OEG9fWPsDX 1ADv8uxaUeBET7EWOUXGNZPK/BlGj6epZUv5eiQBDIvWvmnaYcxhdg18u3Tz/sz7TJcH 6V4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741618379; x=1742223179; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=42Qh4XiAuZulv75xzl4kByDL6UOmUjop2oq2YsBLVIA=; b=lWSCQS19bEyvJ9m2B8p5l4YeHj/iN7yMk4wX3I0Yo1psQMLWwPfriNactwPTiL7XBv 4QKblICyxc+xSVLPasMrQZk/gIASs1oUayHH+JjYT8RfcRnX2EYQXcWuvlfRedIJxs2q fU93vzmJ+/2aOnO0ydah2vWcHc/j+buc9FMbEIhtlWlwuF8qytEWgiLNcc1Htriak5Be chOoto0lA4C3iF+13/MkqTaClIlmH+ZNR6PYsE3Yh7VC+Vg6n6C/zCiC00wHsPWnJrO3 Lq6BZw/wuIootD/oyWW9ys/lU0aDHfylXSKEzl8FoziP3EKV44TyED/kYoQuKxlvpJDf hWQg== X-Forwarded-Encrypted: i=1; AJvYcCWlb2T2Ag62tZgity5AY91Ru8XRPhHKdMoKPELJAuMV2wYJ3j4qJX/AsseWQUuB2jcRALG6j6Ag5R9RRA==@lists.infradead.org X-Gm-Message-State: AOJu0Ywlxt3UsK+pJpQ0SKjDTuXeQL4rAO3k01iYkXEAqLM2EGEkUU2t edNr8pZyomhdtkIVx8Zuq6SkWUCvW9k4pIwmEcaK3e1R9NOn+WhMbA8+kLP+HjY= X-Gm-Gg: ASbGncvpXSOjgY6HBY52gLEiJUU3sM2FOavyTLjHp61y71gcb23JAbQx45N78kE+VLu f2K+O2kATd8onItf8S8X1jyeu7NSajEwh87oJo4IfQptPV6D2wPy4pKYbeglXu1LygAUl6djmoz N5dcPM4MA+mapVy3kjIy/D/FT5tg5deN0M5jd/AU2wp7LRbditTYVAayVZZSiEMyE5the4DrRhU 3W5hbmEotFgnemZeIwXyhD6X84PgUyJvIMClRfckCzMhu/F4PlOdtLrsCqWe5TfsvzJCVyGTi4K 8pJnoPJpEENb4d9JxPPM0UD9kJ41drdI6g0coLSt3moWfaP222mk71o= X-Google-Smtp-Source: AGHT+IHEXGQJboz6H1D26OUIQwYNlDx0HzkIlJbeLtq0a2EzFTAzmNT4PJCmia/gb9gcmwxF4dQMsA== X-Received: by 2002:a17:902:e74d:b0:223:517a:d5a9 with SMTP id d9443c01a7336-22464503452mr163981475ad.15.1741618379437; Mon, 10 Mar 2025 07:52:59 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-736d11d4600sm2890275b3a.116.2025.03.10.07.52.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 07:52:59 -0700 (PDT) From: Deepak Gupta Date: Mon, 10 Mar 2025 07:52:33 -0700 Subject: [PATCH v11 11/27] riscv/shstk: If needed allocate a new shadow stack on clone MIME-Version: 1.0 Message-Id: <20250310-v5_user_cfi_series-v11-11-86b36cbfb910@rivosinc.com> References: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> In-Reply-To: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Paul Walmsley , Palmer Dabbelt , Albert Ou , Conor Dooley , Rob Herring , Krzysztof Kozlowski , Arnd Bergmann , Christian Brauner , Peter Zijlstra , Oleg Nesterov , Eric Biederman , Kees Cook , Jonathan Corbet , Shuah Khan , Jann Horn , Conor Dooley Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, alistair.francis@wdc.com, richard.henderson@linaro.org, jim.shu@sifive.com, andybnac@gmail.com, kito.cheng@sifive.com, charlie@rivosinc.com, atishp@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, alexghiti@rivosinc.com, samitolvanen@google.com, broonie@kernel.org, rick.p.edgecombe@intel.com, Deepak Gupta X-Mailer: b4 0.14.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250310_145301_212617_AD1D9EE5 X-CRM114-Status: GOOD ( 29.79 ) 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 Mar 10 14:52: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: 14010337 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 93C6CC282DE for ; Mon, 10 Mar 2025 15:42:41 +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=FnJ6JXWCegzNDG a0td9t0VxCv08lEWMXEHVWg1doGFovlEzzCNlazaDa+L+MUTl++mlsVyS7/zG3FglTc+yEyz22gep WhdZCWBvgcy1cFn25MfbW5Lc5Uc/YRLMN2GLX/Wk1KETp57FhcE8afaAqsZemK+Z6SDEHC9pSbDhH 7sMHVqx0Hgx2UX3PMuaDtXSq201Csvh76zyx2RH27xSzg/xs5vDp6RNFoAoGS/B1XexVpvDWiTRcC bfrkk3Y1M3jpAifRSaUWJJseUEpB+5WAwxeb4P4mB0SpN/9fN86YlwrhyFd/bj8vXM3Pr/bafDYi2 I1hXGJIMpegSspdMd7Sw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1trfH1-00000003DTj-1m9Q; Mon, 10 Mar 2025 15:42:35 +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 1treV9-000000031iD-1IAv for linux-riscv@bombadil.infradead.org; Mon, 10 Mar 2025 14:53:07 +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=BWTxuMRGFE7/DgkVta93LZpTxj oKgbMauzwcsYzpAxasumHMBZXIhu06iu7PdW8okckG8y1J50hNCBQloau9FIYpgFdqpvpxhVr1ixu OanWWnY/EAKUe6vMz7SjTzsutB/pjMEYRBoNWOZhFGI9ObYFzyZ2tuecf/I3W9S5lD1BuBZBqdM/L BUgO+iDEaY2/faw3uLY9brIzQaJw2wCHQobUF9PT+bSRGiEqbidHuDOtb4m3zS0ILk08+CraP68ul Mc6knzuuxg15BFlQ8DQa4ryjDKnA9WTLtuYbhxwN/dE0zJe0+Jueqz4L/cg8V6+Q1G8WsW3FxxsTf yOvq6eNQ==; 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 1treV6-00000001sOO-1dyj for linux-riscv@lists.infradead.org; Mon, 10 Mar 2025 14:53:06 +0000 Received: by mail-pl1-x634.google.com with SMTP id d9443c01a7336-2232aead377so13124245ad.0 for ; Mon, 10 Mar 2025 07:53:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741618382; x=1742223182; darn=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=aeHtnn/EeFe8ATv4clzubIUtJgyzqH9QWUITXNQN48CpvHFiYbweIIx8Rvlo/2VGnN 3xHyvoJQAXyDKvcYVVEFwenl4flxBcyJLULu/+5++7kcMucbgInsZjEf0MAQroRjF4A/ M5CT6rKtR0MJVTZts4zDgO7l1qtiernRdjA0TSKbFeU5hWwJCC8FVqV8hEIZ0+tF4LrG Cyk3D5ZWr1aFocrYhpDJURRVn7ecKYfVfS5LCdaXLpL0hQTJvcl/R/9tTiGRAU2/pXiM 321wJA1MjMtIibXvtDRiqU3tI9QiDq/1tFN5t32mmijJMkXQEbYw88uggYJn51T0ElVh FlEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741618382; x=1742223182; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Zfw9OtoVzzM7Z5oL2c+Ej/yDm51xyr1WNtqXJN3fx8s=; b=FL0Zrp5r3BZlRPM5TTvjAQfO4lvcdxG69d+mNVv2sUoGzQX9L6/vfHMYgt2sZEUrRj OJrZ0nofKZzBkiElW8nXiy0hrukW+/Pc1hxaEgVCOH6QvrTGWc9OoPf2Jkp7sdrc9yjw HnnWXM+fYSEclPtzL2/SSJik5dLSt+CCMsk0wxM0ZAal/LGKfjezaT04x9/3MKEcHGhW YqBUaSaZXRuKOzZvOQ6RPI0QiVNu/3wQrJwE8iwKhmoxEjxmd0hb0GOPrQezTrgbD2G/ JMSMd4dJLc6Kj04n3dXvJQE5CHWk7I9cm5kqyltHJtRuwdrwgN32/CjpqFQh4Uog5dB2 seSQ== X-Forwarded-Encrypted: i=1; AJvYcCVYGCNiR0z3iT8OPxuBv5QbewrYJ5d6Eji8Fthl29zRPdCTlq7a9duKdp4yEUmnRbNqjJLs6ZrLXhn2Hw==@lists.infradead.org X-Gm-Message-State: AOJu0Yypr9OeBWaC1iRjARpIE+2nLgo5to4i62pVEEUF2qEqSg6QcZAA nYmR7F/EPeyij3OphCiW6X9A4pkP01NckLjtApzX5iYm0fcmSCpUKv/bq9oGv48= X-Gm-Gg: ASbGncuoJYXTjCSKfoiQPmux5PPvb2RKrHT5EjIwmJbQDYfMBCf5OpydDfjBkqRZjzf dkmiogA3TbX3Tp0il8EF8Il9i02IPmfXmSLpObWpwwlokowoLAp5gayl9RiYfzhrQ79pV3RqXEZ krQasqgEf9nxtILjHr0xvaMImf4p88337NE5ifvIGTuExyUDLD2/kyqnAKqO/4x+Bc1IGQHhe1l ZTI+NgrjBUOTWokE1Zl8DtXnE1VXHcpFrkcQjsRls/j8CcdknWbc1ppZmq9pe0OAZIoErYASSIa 2O05PSH/wXYhTTPHJR3HHJIrUtkKUgMvRAM8W0Sw2R1uNpjg4j2XD7B9hmHdvgP2qQ== X-Google-Smtp-Source: AGHT+IHCKzm+H1WuyN4ioLJbIhvG1jyYj+k1eB31ZxZlrJTUkTC6vhrDB22oVaHxl5JQ/hZbQyLd3A== X-Received: by 2002:a05:6a20:430a:b0:1f0:e893:409f with SMTP id adf61e73a8af0-1f544b180dbmr23325177637.23.1741618381901; Mon, 10 Mar 2025 07:53:01 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-736d11d4600sm2890275b3a.116.2025.03.10.07.52.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 07:53:01 -0700 (PDT) From: Deepak Gupta Date: Mon, 10 Mar 2025 07:52:34 -0700 Subject: [PATCH v11 12/27] riscv: Implements arch agnostic shadow stack prctls MIME-Version: 1.0 Message-Id: <20250310-v5_user_cfi_series-v11-12-86b36cbfb910@rivosinc.com> References: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> In-Reply-To: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Paul Walmsley , Palmer Dabbelt , Albert Ou , Conor Dooley , Rob Herring , Krzysztof Kozlowski , Arnd Bergmann , Christian Brauner , Peter Zijlstra , Oleg Nesterov , Eric Biederman , Kees Cook , Jonathan Corbet , Shuah Khan , Jann Horn , Conor Dooley Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, alistair.francis@wdc.com, richard.henderson@linaro.org, jim.shu@sifive.com, andybnac@gmail.com, kito.cheng@sifive.com, charlie@rivosinc.com, atishp@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, alexghiti@rivosinc.com, samitolvanen@google.com, broonie@kernel.org, rick.p.edgecombe@intel.com, Deepak Gupta X-Mailer: b4 0.14.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250310_145304_585837_3F86EBC9 X-CRM114-Status: GOOD ( 24.35 ) 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 Mar 10 14:52: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: 14010336 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 A3E4AC35FF3 for ; Mon, 10 Mar 2025 15:42:39 +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=eqEWXMAdYJoVlC qjRba04qt4fCJPlXG5mNZUdLluHWTs/HgFo3uuzU4Iy7uCSjn7VXQYHuRJSh+RHp6ACDajV8nD8g3 r2srUuCTOZhHwo/a1KewTEzrx6kvYw/8tQYiELWjOb+JoCye11SeSDes0qLEHJgevzylZZ262KWwC 1mBNLAkkvrT7idHP21EOU2iZCMCnl+0J9b+yDMx757iXW8MZak+fZs4nIvmsOPAegc+hpYPvdQe9a 3HujC/dXGrNT0WOvQrFF6sv+QEdvX3xh0XEJhYOwrSfDSKDRTB5zeeoeBlw/jFzRkweJSTHznKNop uNLXEW7AJaIY5fbUfwLg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1trfGz-00000003DQe-3poG; Mon, 10 Mar 2025 15:42:33 +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 1treV7-000000031h3-17aq for linux-riscv@lists.infradead.org; Mon, 10 Mar 2025 14:53:06 +0000 Received: by mail-pl1-x62b.google.com with SMTP id d9443c01a7336-22334203781so95786775ad.0 for ; Mon, 10 Mar 2025 07:53:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741618384; x=1742223184; darn=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=kzhNRXWTWS8wDx3kOo1d6WkErV08Act7imKAeO33u90hg9T+P1+i2LAfZp8FM2ZMW2 NIuSmSffB4+twHZ+8FFoxRbid3qu/N9BnfWt5WdVBBWT1llshU5cgAlWWAw3ZMUCxr6f 19JJLv6HzitaSZmT4EhIwyN6ZG5W0/jlZTXbbaEHLGvQy43EBvsSnL9gBDQ681wBAE3j IYRDfcjg1y7dKAicEWdyTD89AUE6jo6vhTQneQg+pRb/0LzmeZydkg01efDbAiiSmnNu fr1VsjphQzH12cuP3Ho5dd8aqcaRMpN3SZyP37dhU2Yy8McE733WfLcFqiRlF7ytycBH Q0Xw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741618384; x=1742223184; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=n4wOXS0/20DvZRxruHD7mFnd6lRzln/dV2wTFJ6STYc=; b=Ztyn2EqYMzdkSdXvRhN/ryXmZlhsVldaKx1XVKQ1zrkwEmIor7xUICnitsLGbuYUCe PpKE8zsqAxqLKvqhV4LEIiQBjNFIwOnp8n8dcRkzIarAdxgBR8ko3hv610lFhpWRJQo5 eyqU/RHFcoYIpnwZs8v8JcXpnyDAbB/h7bOg7ITXZLKF7qmimmhb51gMvTD1nz6r0o2C in078uGyW7LNyNeKISVX3cd8BCOD10OvA9XHCaJlc5qDOX4eBQ3Tr/BSEnTf/zuuxy/B pmbC4akMuZDb6frUJ4e1lg2zGQNpeEnwZxyOeQzFWvjHrRfAq58/vpvurJ6jqGo4/BhB tzGw== X-Forwarded-Encrypted: i=1; AJvYcCXzKLaeifXOYpq8av/CH+Oc+yLywyQe597wys79/0fJ6I57K/zQo4/hzrkHJwhk1gkLqKHPBtF6uPyghA==@lists.infradead.org X-Gm-Message-State: AOJu0YwYBKy5I3PiTv78N114caJBQfLEG5DjKq4kzr4PTYe0kVsvJZra iu6ricOZx5jLqj5vRJyDpsu/VWP98SpDXCgnAXEAxvXCdj/wIHBZxy4pqQz1ZLw= X-Gm-Gg: ASbGnctX1g9dCUWQlyRtORixfkhNofdJ8Ak3cCFjZ66cm45tbbi2LHZLo/plhfbYQsJ U1Va0gzWLVMwnA44BMmKnu4hZ4pPG2N1DUtqpeQcTzd9DP9tr34eyad2BZfJjgzXkJvGfNEN6Ff JyKkurGGn9Zi7qRKsAuH+J81BAjiYIXe9SHkACSAex12peVsI/ObWU+ngdIGf6bVNBHgUAfksAM jPDQinRFMyt4vhN/p4Lp67129BGGicpvaxX3voUe6ew+2fK7AcnuAmPZhByo4Lf2v+CFuraKI5h WyzHMsJE2k9/IKUChKYuB3BOSuga3uyBHoRbUmstewqBrBL0Zy/5XVCLkHrWn78NwQ== X-Google-Smtp-Source: AGHT+IFPcCScpuJQuODEjO5ogKF+7rsG+dE6AGDUyxV2l5MZI+A8WNThCUXQtc0yXYjGKQnUtdvO1A== X-Received: by 2002:a05:6a00:13a5:b0:736:9f2e:1357 with SMTP id d2e1a72fcca58-736bc0d54eemr15283735b3a.12.1741618384358; Mon, 10 Mar 2025 07:53:04 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-736d11d4600sm2890275b3a.116.2025.03.10.07.53.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 07:53:04 -0700 (PDT) From: Deepak Gupta Date: Mon, 10 Mar 2025 07:52:35 -0700 Subject: [PATCH v11 13/27] prctl: arch-agnostic prctl for indirect branch tracking MIME-Version: 1.0 Message-Id: <20250310-v5_user_cfi_series-v11-13-86b36cbfb910@rivosinc.com> References: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> In-Reply-To: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Paul Walmsley , Palmer Dabbelt , Albert Ou , Conor Dooley , Rob Herring , Krzysztof Kozlowski , Arnd Bergmann , Christian Brauner , Peter Zijlstra , Oleg Nesterov , Eric Biederman , Kees Cook , Jonathan Corbet , Shuah Khan , Jann Horn , Conor Dooley Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, alistair.francis@wdc.com, richard.henderson@linaro.org, jim.shu@sifive.com, andybnac@gmail.com, kito.cheng@sifive.com, charlie@rivosinc.com, atishp@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, alexghiti@rivosinc.com, samitolvanen@google.com, broonie@kernel.org, rick.p.edgecombe@intel.com, Deepak Gupta X-Mailer: b4 0.14.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250310_075305_307005_9321B660 X-CRM114-Status: GOOD ( 24.50 ) 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 Mar 10 14:52: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: 14010338 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 68F53C282EC for ; Mon, 10 Mar 2025 15:42:42 +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=L66mzjlOwR5Lzz 8GeoxsyR08sEEJlJ09f1pTGc8xRjG1cWYe8onr34+rP23dQ/cS/SUI8UXW/46iUR7zj0HLleW2cgd SNLky3a+ul6QYn1PqyToFnlNmuWq2GfplJsGrbiy/l3irdCvR9F6LnYpbDVq5i+5bJjkoL58QvhzQ clxEYGAOdSyhcqoZcW800EToEUeb++ggxHLDwtgtZyun12OX6hSLlhs/nan3nBX+1u0rYOzzYqiKg gOXCClFPTcPtnVx0xQgAPElGwZUuTj8viRPpZZcqr39yzo2/X/h1IAy14geFmWxuxQkB2m92dDIYh q+MUyhouKUYWnRVc7LrQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1trfH2-00000003DWK-3pgf; Mon, 10 Mar 2025 15:42:36 +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 1treV9-000000031iy-1ok2 for linux-riscv@lists.infradead.org; Mon, 10 Mar 2025 14:53:08 +0000 Received: by mail-pl1-x636.google.com with SMTP id d9443c01a7336-223fd89d036so84669985ad.1 for ; Mon, 10 Mar 2025 07:53:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741618387; x=1742223187; darn=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=w4l/VF3eUyp+VsJy5UArM9IskyIvYHL1aFoVejtrCrInYhjKqQ37D2pws3dbR9jzOU PL4SnGTbQtCsVLl3nOlbROTXtM/a3r1FpkUK/m+9Pe7QD+rHKhGex39EjJK+V3J434gs IKdcRvLtA7f1Y5OYrVx/HBKCQTfSAsZtkZvSHvX4iJ2loFPKFXyFMvn4m5xp9RsEVdBJ yYBj50uQloscuSNxBtLHAN7R1XErX5pXT//3NCC41Yyf3NFXVLVbqjhuWn6vFvbTG0Tt BCGaIZtqgp59yZV6hpmgHQxyddlaclemkUVygH2Ffw9vQ04h+T1BuZLRiN/x1+LCBZii NO1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741618387; x=1742223187; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=M49HWf8gzr6wWBf1Qtr4VOivQAEEQjfyuWgORGOVTSw=; b=uDFMh3CYc9K3MBG+ZoHi7AQNy56/rAUNE18H4KCVp+V1u7xtBZJv8/+lE3xaP+cHy3 ERJWzM/+u9t07RdMtPOZfgjSZN41JUy8PpXwmrIJovEOqB9Ib6lQsvgjYSZW9UuFV3/5 65yNEDU4DY8CpDZY5PZcXSp6ZQSLnu3ymb0TuvJJhYS2eW9Hol6LgpZ2nKA9mHmt0SbL D6bn0KZ1QJ4d5keShP3fG/G8OGiCN7FIFW/TfJHjy7AMvKT4uk1UE/HuUwzQiuoTWxjm uCBvwUidYZaa0lv9+hgYpj3bxGiAYexkuUpfvG+2Aeux2mogAOQ1y/y8J6XwSs2DHsYE uSfA== X-Forwarded-Encrypted: i=1; AJvYcCWmkjU/x8u3kuCu+gevXNjtR+DQ9zhfohJwucicWG+tB8IbfoLIWsn1yu5MvLqax5fKWVi0hVTGEdh6cQ==@lists.infradead.org X-Gm-Message-State: AOJu0YwoCamx/zR3KGe8fPBbrVP35zkkR8LAypbX/mVnCrIpgJCuW3Qn j+VWS2o8WUPJF9wvVZOTMDfSTD7/vnoN2kk8QF7oTXNEfxnK8TIonyNIM63Apgs= X-Gm-Gg: ASbGncu+o3Ho/hdmvuCETkzNkMsfERdjmanJPAYY1JKfPt2fwymnKCUouMlBbUk1wOG woHQmRX5vcPU46+ZnMUgLREaVDcFNXbClREqq+lGW3nRL9vnBWLP313859K64vVCZhiuAcY0TFe sQ1e0rlgqFfCG+lqv5XDvwtwE8hTvEJskAcpF62qwt9iFxjPq3scuzTJrn/GbptiBhVX34s2myR bOcz/Pl0utsLRZaItB8HEDfM1mqqL89KhJ79Vyk5tu7OTCl4IkLXHppDFJaU9TU6pT6l37f5UxR ey87V44ijZbiCh0WPdzbrhaOnE/9cr9b7AgRI4n74XPHsZrfPCXYVAU= X-Google-Smtp-Source: AGHT+IFsZxG1TWVrNQMrqkKpccFn2JJPQ/WSJPQEXgIDTpBCMBrYmD46kiCZqmMB33SVFrCrhoe36g== X-Received: by 2002:a05:6a20:2d2c:b0:1f5:8221:d68c with SMTP id adf61e73a8af0-1f58221fee5mr5284959637.3.1741618386923; Mon, 10 Mar 2025 07:53:06 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-736d11d4600sm2890275b3a.116.2025.03.10.07.53.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 07:53:06 -0700 (PDT) From: Deepak Gupta Date: Mon, 10 Mar 2025 07:52:36 -0700 Subject: [PATCH v11 14/27] riscv/traps: Introduce software check exception MIME-Version: 1.0 Message-Id: <20250310-v5_user_cfi_series-v11-14-86b36cbfb910@rivosinc.com> References: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> In-Reply-To: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Paul Walmsley , Palmer Dabbelt , Albert Ou , Conor Dooley , Rob Herring , Krzysztof Kozlowski , Arnd Bergmann , Christian Brauner , Peter Zijlstra , Oleg Nesterov , Eric Biederman , Kees Cook , Jonathan Corbet , Shuah Khan , Jann Horn , Conor Dooley Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, alistair.francis@wdc.com, richard.henderson@linaro.org, jim.shu@sifive.com, andybnac@gmail.com, kito.cheng@sifive.com, charlie@rivosinc.com, atishp@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, alexghiti@rivosinc.com, samitolvanen@google.com, broonie@kernel.org, rick.p.edgecombe@intel.com, Deepak Gupta X-Mailer: b4 0.14.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250310_075307_508405_53A9A850 X-CRM114-Status: GOOD ( 18.44 ) 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 Mar 10 14:52: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: 14010339 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 3EB5DC282DE for ; Mon, 10 Mar 2025 15:42:45 +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=xpN+ji0ewjvEVu zl1T+fw6KhRJWqaacrObaXhgZ7IUuScFk2UUQ9DHmPxl3R09+f7znvDeohJCdxxrJ2GkjLjDiG4BE RP8X+Dz0iDcqcGRKZUnqXeDUPA0NVc7MATXl4pBe/kpLWtjIdJ9MS284ZDBV1hcbzHd+iEnGCrNhS gzwl+mRLg/+rhTEnPzxQ98FJ6IcXfH7cEabEkXN/JHQGRa+EHMejnU7+/ZYVoVDv3jRRFeFGZZdsu Z+Ma33DdSNSJ1JXM0tp8yaQLkvz7I0xqnikk1t9N2G69IhHBAhr7IMU8nOo9JFJWBjJVjS7Rww2VS tyn123QlnbAx5QXJJSJA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1trfH5-00000003DaJ-0Hp4; Mon, 10 Mar 2025 15:42:39 +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 1treVB-000000031kU-3lBH for linux-riscv@lists.infradead.org; Mon, 10 Mar 2025 14:53:11 +0000 Received: by mail-pl1-x636.google.com with SMTP id d9443c01a7336-224171d6826so63922605ad.3 for ; Mon, 10 Mar 2025 07:53:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741618389; x=1742223189; darn=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=YwTedndY2r9Rv02WqEnyvQtpFj32NdTIvS7Za94B4TnCvm7BrlzfPJWdSwKEaYfFou Sr/HQ79CE2zOC1WQ2zWMRfp92ndnW8hYhsrGWwptKPZWHedD9HiF9xJBIxWUWSt5NNos xrRIPqjOhaM+Wbvi9tpGF9HsSXlxQbAwt00Ab4iTsfnqXYp7M3djKWwpGE5e6nHp5VUW QxAOTpokETyreox0zUFywD9QRJb5aWzdxGSHeIuRpx2xzO9QGtLKuhomqWhFmmeu4RJB mp5g/c3eFZGnz4vM/UUKH+msSQaueCfaCbLAmpX/M654NXuaNXOJavcrfeMcDXxLitpt ioDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741618389; x=1742223189; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=N78rd5bu5L3Nb7UCylb6NhXzPkib+ej1wIZfC16ALRg=; b=v0spBHh5gpkU9NzlAa5R3QUnOIcGza3ivGq33jdBKnvyXo/hkIoo7f9GTgangQ/5L9 Ekwp1AfZiVM6lfIQMvLdorpGMFUbNWrfHywM+djsbDJHkzOiQxeC5kQMvad9U/Syz3C0 6aysnTqZdkFJeEJ+SDytJysAjqHwtm1xoDtbd4dzfGIKM2ir0m+OwlPsRZYIZB6B841V T2pnmovFH/JwDHreVmeQO1z5++zf2Izam9NfR1SqEg4B/FBspQyXfevOSjPgyK9M3I0F YYGy8Y0DB9in9f9hFaVFYlvg4U/GzUBFmxsPC4P/1iwVNx03c3Qng6dyKrqia4eho4Jx jwxA== X-Forwarded-Encrypted: i=1; AJvYcCWFxGXoh/sAv1e/ab9rs4aAKxNy2pkw2608cecfdk7KNyKqiZLfJmfVOLg9KUoPVTTCF9XFo4TLpkSf6Q==@lists.infradead.org X-Gm-Message-State: AOJu0Yy5V9g8D0s/NovxRMY4PhcK4Cz6sNOuewUSDWs+Vjpx61KudZv8 xn8QjmOZPlVus6cjfO8Jz/WepEZRH1F6386zGsFeYa8s76ZXCYsUlnw42MaTE+I= X-Gm-Gg: ASbGncuTtT9Bo7PLDg8663UkLn3IkFOL4EFm8DRNN6lcjhxrJ5ZrG+H31BiiJcCq8dz s90s+wZuT6Hq4+hvS8uqY15Ofdwpx22Fxz/VcLrmIsLfD0UceoN/d4ZMl4RVA7A4E52xsBiorUK zY92QM0BUjeCQR3+zOnFGvBvZQ9Rmlekx/T3lGDbGQi1GmkHxbjLct5vOOtxciFLPHGwKdWcfGk bGiORuP6F3j1nfc772C2oZyHK3oJCgroqs5r0oX+jGExQTriKcZkNg7WXnRrG83IENeFYlW/ShF AW1WV49dh/6bRjlLlTGSwv9yz8E6JXEedV5YuYnaNjBn71NtQfHIAV4HzCHBD8HPOA== X-Google-Smtp-Source: AGHT+IFeD/HgCjO9LcRex1YUUmRruVxizMZhpwj5TvOctT1vNUnUY9bR7/1JUpOvPThhyzvn2X9ItQ== X-Received: by 2002:a05:6a00:1703:b0:736:a6e0:e669 with SMTP id d2e1a72fcca58-736aaad3bb2mr22481649b3a.20.1741618389332; Mon, 10 Mar 2025 07:53:09 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-736d11d4600sm2890275b3a.116.2025.03.10.07.53.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 07:53:08 -0700 (PDT) From: Deepak Gupta Date: Mon, 10 Mar 2025 07:52:37 -0700 Subject: [PATCH v11 15/27] riscv: signal: abstract header saving for setup_sigcontext MIME-Version: 1.0 Message-Id: <20250310-v5_user_cfi_series-v11-15-86b36cbfb910@rivosinc.com> References: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> In-Reply-To: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Paul Walmsley , Palmer Dabbelt , Albert Ou , Conor Dooley , Rob Herring , Krzysztof Kozlowski , Arnd Bergmann , Christian Brauner , Peter Zijlstra , Oleg Nesterov , Eric Biederman , Kees Cook , Jonathan Corbet , Shuah Khan , Jann Horn , Conor Dooley Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, alistair.francis@wdc.com, richard.henderson@linaro.org, jim.shu@sifive.com, andybnac@gmail.com, kito.cheng@sifive.com, charlie@rivosinc.com, atishp@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, alexghiti@rivosinc.com, samitolvanen@google.com, broonie@kernel.org, rick.p.edgecombe@intel.com X-Mailer: b4 0.14.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250310_075309_959683_56784DEC X-CRM114-Status: GOOD ( 25.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 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 Mar 10 14:52: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: 14010340 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 B889EC28B2E for ; Mon, 10 Mar 2025 15:42:46 +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=2q8EbBSdZrcIs3 4OWTxR3hef7pSpP4IxyLkpM7JSPGYf9VJqBB8BaOhwj+zdRoyrOSvZ2eJ+zPadKGFmhaLpVokkTbm XDSQa1nXdTuMIO1sxQLlHA7dJa5SkDuKV++dR3Kb2n9Cd+9pu9Yy0wB/VJY5MswNTmjOnAjhczkmz IJauCaAVYS4uk05sGwcnPm598dKtRNCoJ1SLs7P404Lqs2h7tNqPD+BKHjYZVZTx5seHgmGFn+bZX c6XUcpRP0plGO9K0Z05i7i6x3D9DANMiSUgI/i1/Pls+jZmKb7rlksRTriT2OCGFItj57mpF6MYGu W6+TRktSAWd+h8LoSTTg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1trfH6-00000003Ddb-2Ksv; Mon, 10 Mar 2025 15:42:40 +0000 Received: from mail-pl1-x62f.google.com ([2607:f8b0:4864:20::62f]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1treVE-000000031mO-1lH2 for linux-riscv@lists.infradead.org; Mon, 10 Mar 2025 14:53:13 +0000 Received: by mail-pl1-x62f.google.com with SMTP id d9443c01a7336-223594b3c6dso76384175ad.2 for ; Mon, 10 Mar 2025 07:53:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741618392; x=1742223192; darn=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=kvrPS3eoh7aUn53CrG68afLwAXCArm1XbH1NO0Alai3V2G8+mfOl1qJHK8dxWMuJFv FVCdDNVu2hW7Mi1cqRz/W6Dre2PL3zJ9LsqRagY0Pm/y7SfFftx86+BZzgT4FN8M6x4Q HW3ZrecCDxwBnJxB5UopJ6n74fJmUCWt6xvdwr9E+38BflDhdKQb7g4XHDd2c0qtsZq0 GXZX5aFrgxMoo1BD/spuM/KmYif7EJVh+loyVtiosEcC5jfRxaSYZvip+nOjJa0gUp3i o5/lI3FiWEcehOIkrXrht2CE6sAKSnBUpj8LbWQcmYC+Ly62E8AjMLg515zUk27o4eQi rvYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741618392; x=1742223192; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=FZizf8oZeAex4CEwHeMnGWZudkjFQvNzoIsL7CfIy1g=; b=BLKm+H/DTMhEZeNHtIQoZLJwtw69MnSX2OrbdqfjHAMvSEoiH53dLWtILPONC5dn9u 7E2obXgubcFYFJAPGXm3X+iRVpBgb2PGQsD24oIIjQJ1RoGw7dGH1shxLf8dFh3TdO27 EEJJo9DMNyVxBPtE7jbr4gxSW12ZHbbLgCYepkLeq+TQEgiGNLwvqzf9w/y/Tdhezdbi 6WkNuh3HS/IeFWC6iU5xB2RKkNv4RA1Z1t/lCZ87U0pLeJgLEfllVVwDwlTwAXIUAi+t EGGGcAN+CfSPpkvJuI+z1DV/xpOJad4vwYZyGq/suUmzZ9kNO6EKR7ReGAhZa2isaCWP V4Pg== X-Forwarded-Encrypted: i=1; AJvYcCWJPmguLricLudQErigNCVVA5yXpg4y/dUP3pzvQmKW0GzCF0/G4R1/8wieM+6ZACYM4voJNBlIo4QgNg==@lists.infradead.org X-Gm-Message-State: AOJu0YxE6uAlHO4ypSP1sD+FBdIRnU3vGdh9kJZGJBAheeojbztNPZys kyJl8mCbmjXMzBGlzpnyNUd7lazw1TTMpJZ6dGQ4f1zZ9JTXCLVjChNv91hHnQU= X-Gm-Gg: ASbGnctxsrou+aL1UFQBhHvXsdUP3Wtqs8nLEnK8xovWF4XflGrxGs/AsYLGEKpi7vy ceNU+GvkGDzwoJuGaW3uwgoFIyusPd9e2HxUNyGC/x9jfgsZMb4Vjyd55BKsCBPMr4yrEldZ61i Q8cqZOquSw7AC04prKzh1NOc/lxHz0YjF7ZMsgkbarfi+jZLSXL7jCElNhZjTfiPEDM683VKd7M kurGyEk5m6ivFkUSYukUxmIJzW8Vfex4vUbLr6my4nzpvt3hY65tmO0Wcv+aDyoFSv043wxPHob g4HoAaRIo/vIWYv+oNwTPqXQ68myltTIhVtV6T+oRjSSI0493RA8PDQ= X-Google-Smtp-Source: AGHT+IGtoWSwFQXp42Jt7uQ+iD9QmR3QbXX6mHvaYDmjRmHhCAWQmnMnksRJhwAUzL/h4lDxdU80yg== X-Received: by 2002:a05:6a21:4606:b0:1f5:7df9:f147 with SMTP id adf61e73a8af0-1f57df9f49dmr7383711637.40.1741618391807; Mon, 10 Mar 2025 07:53:11 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-736d11d4600sm2890275b3a.116.2025.03.10.07.53.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 07:53:11 -0700 (PDT) From: Deepak Gupta Date: Mon, 10 Mar 2025 07:52:38 -0700 Subject: [PATCH v11 16/27] riscv/signal: save and restore of shadow stack for signal MIME-Version: 1.0 Message-Id: <20250310-v5_user_cfi_series-v11-16-86b36cbfb910@rivosinc.com> References: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> In-Reply-To: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Paul Walmsley , Palmer Dabbelt , Albert Ou , Conor Dooley , Rob Herring , Krzysztof Kozlowski , Arnd Bergmann , Christian Brauner , Peter Zijlstra , Oleg Nesterov , Eric Biederman , Kees Cook , Jonathan Corbet , Shuah Khan , Jann Horn , Conor Dooley Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, alistair.francis@wdc.com, richard.henderson@linaro.org, jim.shu@sifive.com, andybnac@gmail.com, kito.cheng@sifive.com, charlie@rivosinc.com, atishp@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, alexghiti@rivosinc.com, samitolvanen@google.com, broonie@kernel.org, rick.p.edgecombe@intel.com, Deepak Gupta , Andy Chiu X-Mailer: b4 0.14.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250310_075312_492860_F8FE6ED6 X-CRM114-Status: GOOD ( 25.82 ) 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 Mar 10 14:52: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: 14010341 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 E2F03C35FF1 for ; Mon, 10 Mar 2025 15:42:47 +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=MU4aOqdhWCFlmp Tq+R5jnVmtFp/cfxPjARyKLpGvt30w/TM2fvS//jXouqp7NcXcShjOSq87M9ZkzGFTBhd096pBBJ2 +gwLcLRVweKHK7uRYJFbz7FDH0+OBuhs15eQZ/hMMbmDbFd7mSAhPH2v4eApCWvvvQevB53orNcVu LfBkByBiDtGSZkPR1BhMNfDsZvZvH8SKMYFxEm0rFq7wilSvminazFwkV6NhlLmMEiRCneoE8DObk iQPd/zhbiw5+IVT3Lm7eV0wwhmUiG5valX5eXeESWlSY+cCbYA1WDx81SQqBSZyOWPVNNlbpqS0nW jycljEWGdkjtL7KWz6Cg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1trfH8-00000003DhJ-1CBN; Mon, 10 Mar 2025 15:42:42 +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 1treVG-000000031nv-3WR8 for linux-riscv@lists.infradead.org; Mon, 10 Mar 2025 14:53:15 +0000 Received: by mail-pl1-x62e.google.com with SMTP id d9443c01a7336-223959039f4so85816355ad.3 for ; Mon, 10 Mar 2025 07:53:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741618394; x=1742223194; darn=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=VqBQ/V65oklKFtsTGmv3g39kBPbW45FxSpy4akXDo1XjRLDQvTA36XtDTh/UQZeHv/ pE9H/qAfzw/S2pxarLpizJ/eGP08PplD3QAvXwl4aPuMc6GJyTRckMa9g3sJ/CLf2kHH 3ITqxiX0eg5cBmxzf0chiKvyrEY3jil6W8NZqE/gSES4YW7+T8KYRUPP7IMuNWj/TtYZ rX3BQlEbC5y48tjbrcQ/cRhwBjnqQHnVYgxHwEDfrLHOKeijbI4Y+WWu6Xyjiso+uzyI ec4gN0hjT+tWQ6xue3r6ywPbzAyp7LXFipKph5ThSU9DJb8GIpaEZiEmx99uVgK3t6WU bJgw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741618394; x=1742223194; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=hU6IAlGSwKWKw5iWDrFBqBdwaJZ69/+Cs7+/DXu3do8=; b=AFRcUQTOfhsuISnWc9ap8hjJJvGic6y35eeXJ/JURyqYuAQpv8+8TAumaSmhH+lv+s aMpNczWUJm41CXAcgPGb6RA9PehFnleozwtzSA79Ej/6R6vH+arRMyOn7N4h8pS5ffvg Hiq+aZRfR1X5VCoZn2Vvl72N2+HhJvKnC4uEuVRMq9Su5guJMclPBhvHuNoXeSrTqmzb TMyKWiNB3Qp4nfC1hYis/gBg9QjRgBzjIkesCWUTSMSCbVDFiS4Bn4LmjtoL1uSmIzPc umXYaLHvhDas8N1Puj29Gfp0IBGGZBmC4DPCz79Q84QPcU/0+UeVjZdZkYx0bo+zKTtI baZQ== X-Forwarded-Encrypted: i=1; AJvYcCXMfJ8AuPhcGenql5dsElGIcs1hU/HzSNImk+h2iqBulmI02Pu75/9p2s9X/CB1jqvOxrhmCykRoQ4anQ==@lists.infradead.org X-Gm-Message-State: AOJu0Ywvd2CpzrCqhhLRPtT/Icn9ow5Iv/OVquxsSub9cwtFcY9bRfxP 2uLVJsa3i9FCT08uY0/bBJS+6izcGU8AaVAAcV9xZ+8VcLw6py7XIYgXpguFbHQ= X-Gm-Gg: ASbGnctyipkGQrEKk7V1NLkEBNg0iOOM6R3wSiYcz3TrITb1kEYvvtd4ZhwCHwW+V6i DTc6l/cFHjvN5gWJu/7dtz9EyI9zkpRgf816uQ+ilWHOoftFDmYuZqf/+inBMnnuP2wiR1Sibfr Bo6VbEpjHs9yiD2yOpJ/Z/ctP9iYUM3J31Gsj7kavMOY3OQo0ycKWNSwg87y2Xy12qMI+G1w+Ah QiQXsFVPIo9zfvqHwnNWsN8i0BOez8prOlVr+pzoiK49f/mRIZH317Mx+6lg46iWFrkjtsl8MQ6 4vNlZakBdATgkaojlH0EY5CYxw7gxQ2acvUaD0Y5W43d09so6UTwVTs= X-Google-Smtp-Source: AGHT+IG6aOZPbFLpYB6hUt/uNP0xUDOJ3SJhAN3Hb+FAwmeol3/+ZPXWsjDkyF4qiBV6yJ/cwzujRQ== X-Received: by 2002:a17:902:ec89:b0:224:10a2:cad9 with SMTP id d9443c01a7336-22428bd5852mr262786715ad.41.1741618394280; Mon, 10 Mar 2025 07:53:14 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-736d11d4600sm2890275b3a.116.2025.03.10.07.53.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 07:53:13 -0700 (PDT) From: Deepak Gupta Date: Mon, 10 Mar 2025 07:52:39 -0700 Subject: [PATCH v11 17/27] riscv/kernel: update __show_regs to print shadow stack register MIME-Version: 1.0 Message-Id: <20250310-v5_user_cfi_series-v11-17-86b36cbfb910@rivosinc.com> References: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> In-Reply-To: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Paul Walmsley , Palmer Dabbelt , Albert Ou , Conor Dooley , Rob Herring , Krzysztof Kozlowski , Arnd Bergmann , Christian Brauner , Peter Zijlstra , Oleg Nesterov , Eric Biederman , Kees Cook , Jonathan Corbet , Shuah Khan , Jann Horn , Conor Dooley Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, alistair.francis@wdc.com, richard.henderson@linaro.org, jim.shu@sifive.com, andybnac@gmail.com, kito.cheng@sifive.com, charlie@rivosinc.com, atishp@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, alexghiti@rivosinc.com, samitolvanen@google.com, broonie@kernel.org, rick.p.edgecombe@intel.com, Deepak Gupta X-Mailer: b4 0.14.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250310_075314_874994_52760248 X-CRM114-Status: GOOD ( 11.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 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 Mar 10 14:52: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: 14010342 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 D6F7CC282EC for ; Mon, 10 Mar 2025 15:42:49 +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=tqKWj7O8Q4IxZY bcMIXOa+fmTwzuUYT6h5tlLEG+46TYoj9/I3fZSX/o8xrh88SjWJEl9l6+5Q0GNermteNvOFeTmSX ZDpUO/FjHNV3/0UsTz4oMXcwMVTy8a38ZWz5pMVfaRWEMVRPv6KxKBY8H9pMATDlBBIfVdOfQUSRC oE7+gcJn75Imb56SMxVt/1jgKxmOMOQOZsLTJF6fhxD1Mo40SL1X5U1CtyyYkkKMj8Prh/4J81Y9O u2BvgWlDG33fx7jC2Ncs3pxUxuWyEu3oy5G28qh9820xxr6SiGsh2u0EcmIlTv+JY116CO7YxgcS6 VvA/Pev5+7lTKk5/bwvw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1trfH9-00000003Djp-2S0B; Mon, 10 Mar 2025 15:42:43 +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 1treVJ-000000031p3-1dMX for linux-riscv@lists.infradead.org; Mon, 10 Mar 2025 14:53:18 +0000 Received: by mail-pl1-x632.google.com with SMTP id d9443c01a7336-22401f4d35aso76683765ad.2 for ; Mon, 10 Mar 2025 07:53:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741618397; x=1742223197; darn=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=WHQAtRvuAtfm2jhBn+bKms1mCPMmYWq7l71JbDeCa6PUMSHoqB15+gv3/+FXC7edp8 i1D8o/vi/Rt8kADL5OZEkh33yG2d/mWBQnC4UzllZbmCOKaaaCGeJT76rZ9W5cmzfCid pENLT6yGZ/EtUA0Iflj5bRklFon19F10A3UwwebywzGNTyxwTvesVwwde0ELglma73/N lG6dTd4iBJpTSizULxaYfc6/SonBOxQyy03D4dAQWWbCbQpciSNGlV73aSRUgf1rojZE MY4AZpWllmYpacGg1ivsYHzZmIy4XnlWDMu4y0C/gAPg+rEYwe2ZZ2A9iQQ2MOLJ0jpc nwXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741618397; x=1742223197; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=qihiC//CQppmEvCBgeNt37whYo0mSsmsLXBW9eHBYhI=; b=H/5nqrwATbjwAvBZWE3lwsGZSAx5IgNtpzxCXz0y9To/Kg9BHl3lkVgPu0rvaBEksr 5WSDPITRM213YX+WQqJD48/jYofB5L+9lfF3Gfi94NBD88nnB05IyawPxSSvkiT5gyeV kmTIFO2YntDRpCLJT7FkM10Py4Liz1DQajbwrzhowJnyxxAqpgV6sZ/wuunr+8COUjX4 Fhw2sL+S/wKFDZEaPNw8Sc7l2DZQu5WMrWQuXrp0K9ON4UHfw/Z0vIrNhUMDBa7dkB7h klBHWB02BOeJFpVyUN76b3mYnCSd3iNiJL0NYW+DM/4thJPHUPxewo2jmaBhS1Dr5vGq oiOA== X-Forwarded-Encrypted: i=1; AJvYcCWANt7A7fGjjyX1EA0CFd2bqhlhmrLVKuSGElPN5dI8cTIVB/daBHEUW44igTUE8E24y8R5jlMUHCBZ0w==@lists.infradead.org X-Gm-Message-State: AOJu0YwEou9NwnnYgJbOLhe9E8PBwZrzru7FjMlZt6E7Ie6y/HtnokiG T+Atuhv/eWf5wJf5WW9C2JrgHATnhHajfD3EzVbZI7LJVxMrf+swrDpGqierChg= X-Gm-Gg: ASbGnctvqCimWbEYUvxKisqoKlbIKo4l2pyYKCHSlZJIK8uGC43xhsZm0+Miw7uwPCx jDAtthuB3lrqiHuwKNrrWvKoOmVbIVYprut9U6HXN+oBiu+uQ9jcuEf6m7ctXQT+uP/LwTrNEoT KtHy2dLgwU+MUkonmN99u+6Z6rgIUMqK0naWYBA+Ci6WVFQo9R6ON5TH9s1Bnv3+klgoQNxeDRR tRpzt4184pL7CXP+i+lSuyMDyjtlnTPqLlHg5wpB8ymcKlmApyB+orOvTQxu0adUB994lEirGZd VCzT1fVs9DMhQ1N4PYwIewYjSjrfQaPycTPGaLl6e22ZegVAXEOpLPg= X-Google-Smtp-Source: AGHT+IHLu0RD5d4MddLFDjOWYpbnLGPGC73GF+e3WfGfDfnCowno5o+D7aYjLyREna2LMovQQemWaA== X-Received: by 2002:a05:6a00:c92:b0:736:8c0f:774f with SMTP id d2e1a72fcca58-736eb8a1ccbmr221171b3a.22.1741618396770; Mon, 10 Mar 2025 07:53:16 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-736d11d4600sm2890275b3a.116.2025.03.10.07.53.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 07:53:16 -0700 (PDT) From: Deepak Gupta Date: Mon, 10 Mar 2025 07:52:40 -0700 Subject: [PATCH v11 18/27] riscv/ptrace: riscv cfi status and state via ptrace and in core files MIME-Version: 1.0 Message-Id: <20250310-v5_user_cfi_series-v11-18-86b36cbfb910@rivosinc.com> References: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> In-Reply-To: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Paul Walmsley , Palmer Dabbelt , Albert Ou , Conor Dooley , Rob Herring , Krzysztof Kozlowski , Arnd Bergmann , Christian Brauner , Peter Zijlstra , Oleg Nesterov , Eric Biederman , Kees Cook , Jonathan Corbet , Shuah Khan , Jann Horn , Conor Dooley Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, alistair.francis@wdc.com, richard.henderson@linaro.org, jim.shu@sifive.com, andybnac@gmail.com, kito.cheng@sifive.com, charlie@rivosinc.com, atishp@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, alexghiti@rivosinc.com, samitolvanen@google.com, broonie@kernel.org, rick.p.edgecombe@intel.com, Deepak Gupta X-Mailer: b4 0.14.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250310_075317_438733_56E7A3B5 X-CRM114-Status: GOOD ( 21.55 ) 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 Mar 10 14:52: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: 14010343 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 DC692C28B2E for ; Mon, 10 Mar 2025 15:42:50 +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=Twi0cHhPsuAs1J dAhc3EfSPCLwhL/0CqzMeX6ceVVYiRT3D6O2NxNVDeQ3LHjtyWSL+3AZZRxBhynLoNB7GOj39UIWW +DJ7836Bw+GplZYwp7zI0Mx2WlUW10SDD0xHhdo7gMI1Y7uW/vPkpFmbGFjJLw38FmZ2difLUVF9g HpYTepubtK43ZQrv1JAp17/34tiYupWgX2s/QAZvTexrH2mR/h0giJeuv82xHWumSu7zjd6TIyhW8 Hl8zcg/hIPZ8BfXCbZsH5BOVkD02jMs9Mc7r25Cect78AVTuFskZ1YjxQ0NqOPtKHzANc1mXCoqJd lUo3MdJjmN2UjnqKXHLg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1trfHA-00000003DmC-3mjm; Mon, 10 Mar 2025 15:42:44 +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 1treVL-000000031ql-3Ez1 for linux-riscv@lists.infradead.org; Mon, 10 Mar 2025 14:53:21 +0000 Received: by mail-pl1-x630.google.com with SMTP id d9443c01a7336-22334203781so95792065ad.0 for ; Mon, 10 Mar 2025 07:53:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741618399; x=1742223199; darn=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=iqrBJdukIaYCx039bRcMFJ0Yz+7b2vI6+bH4DPZyAmBZNbPlnNRQ50BJ3bOrXW5pdN OsuqUaLVd6tP48JGiqjM5ymvCc1o/f/3TOptoX6mHplf2oMf43AJC8MMVMJ0q2rp5UP7 puaTEPDBp30PC3Yr9cGIwLL8gTt71OwJzUSwWVthRsbfeKDlVfV4LEmOPuwqQSkRDI7/ 32TAPD9HuhmtM+EOK+PtbXxLGyZYl+ZmVztBMl9I2jGmcuazohtT5RDPsFVQB+L+S4n7 FqIBm7xu027afJ2X4sszIZ5yUaeY0cokufM21kZ9jR1sNIlHcgt/TSfjy1nmVJLL+yVg BlDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741618399; x=1742223199; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=BEoq6zqSeLFMaXOoiuM7yp6ZSaKHVlNR4X3PvApIHRU=; b=F8dhB4Rc/7TpLZIRcmHEbY1ZcFWbc/NKmjC5dnfxAJ6LvggXA45HzqMsVXkGIsKwRk QP0tGAhqsNoRpp6PZNSP6O5Hst21SVGNoFTQrBZ+YjJp+PcBSDc1RTTXoXd5D5C0XQYJ o/NCLDa/egiV9jbVYhvoUNxPGrqGqTtNizied2jIp0p/kA59iHDu474wH+vVhZ4+/zer 0kRphIK6rTTb4eH7Bx/Nuockb1iTl3iTBR4RNy4F/LRC9j6AompitpNAW1rUUwc6uCUp AZGfPW/cXyU0rjbpkAKNBHfeLHq77PG7nO5tcbq2AueNyDXMBtO+qD3AKiV5DukIAl5n hvsA== X-Forwarded-Encrypted: i=1; AJvYcCUbJOGybHuDHqUzAjpS0JAyWL39dfwFQF2to1jj3swz0bsoHrEUNyvcNIDapareyDGyBhWVxdo9qmJ5Pg==@lists.infradead.org X-Gm-Message-State: AOJu0Yz/PN1Qw9QzQDjbfsl3JbpVxb1IP7HBCvWf9lxIwM/eijJnTO5D 5chGlw/gq/PlzmY+kJIJ6w0PxI+/jQIZZp7/8fylL3P/vkgHg9lUUgkMj5pWMzc= X-Gm-Gg: ASbGncu27jiUaDUwhVOzk+vXFKzjnRktBvYnZZuq4ip/Vsj3llzVKpkD/eayCrSrabm NXbyFUv8/kq3YInaLRYfij9hKBU/0a4jQ06RodkcWSWTDmiRg3YbkixQiswLA9Tyta1OCtxAMqb E1+ZoGvIVpqziD3LsGQgjMJBp048tzYDcuTRngksBKNn6SD34++mwtWg5uChSndQDr8ckuoSYgN 1yESWmyNkvBJkRy/HeTt//ssAedQMW8abW+/J4AGuq5jQByfrzpkhPPSIf1albm7qnvt3DOy3Zv e38Aux9srPHxo++pamkzPaFWPznebXfDkcbMIj5gXqW0t+KgdBspdNk= X-Google-Smtp-Source: AGHT+IEx/P9VhGmpM9Qve+8fxlnJz5OWfBkRKBtIDANK/IPRQR8t6D11CbRKG5Xc1EPx3A5RF0LL1A== X-Received: by 2002:a05:6a00:2b4f:b0:736:b923:5323 with SMTP id d2e1a72fcca58-736bc0fb12dmr15850983b3a.10.1741618399230; Mon, 10 Mar 2025 07:53:19 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-736d11d4600sm2890275b3a.116.2025.03.10.07.53.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 07:53:18 -0700 (PDT) From: Deepak Gupta Date: Mon, 10 Mar 2025 07:52:41 -0700 Subject: [PATCH v11 19/27] riscv/hwprobe: zicfilp / zicfiss enumeration in hwprobe MIME-Version: 1.0 Message-Id: <20250310-v5_user_cfi_series-v11-19-86b36cbfb910@rivosinc.com> References: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> In-Reply-To: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Paul Walmsley , Palmer Dabbelt , Albert Ou , Conor Dooley , Rob Herring , Krzysztof Kozlowski , Arnd Bergmann , Christian Brauner , Peter Zijlstra , Oleg Nesterov , Eric Biederman , Kees Cook , Jonathan Corbet , Shuah Khan , Jann Horn , Conor Dooley Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, alistair.francis@wdc.com, richard.henderson@linaro.org, jim.shu@sifive.com, andybnac@gmail.com, kito.cheng@sifive.com, charlie@rivosinc.com, atishp@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, alexghiti@rivosinc.com, samitolvanen@google.com, broonie@kernel.org, rick.p.edgecombe@intel.com, Deepak Gupta X-Mailer: b4 0.14.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250310_075319_806413_9916CE2E X-CRM114-Status: GOOD ( 10.21 ) 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 Mar 10 14:52:42 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 14010344 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 1D54AC35FF1 for ; Mon, 10 Mar 2025 15:42:52 +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=T8rF2b080mxSWh gr6BUP0/UR8s8WzyC4HxwqeD8tab+t/YuqmDqPCoKLfLdKO++yTFxz82bS5mrQNX2VRcTpv0KvbiC QPzi99nYPoENW1Hks93gtprFN5hOObrAfdPu4/oTpuJwghW9C/n0CE3HbGwTJqKtVbc/0AyBDm7cm rfgU+9kwWU1dPNkXwF2+ugNMi1K6zvwEYLYjC7aTTK0y6rjgXAdBB4Vm2Y6O3sbIn+BuU8JoktI5f y8lKgUe3pw5ZsDa7cV3SrohYUtiVGc55HoyCu1Ch1PMP44PUaOHsCVkoDsgBgd+HuUTVAWASuN2kK lCFpFHC4uOwrWDtfjmtQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1trfHD-00000003DqZ-0MVb; Mon, 10 Mar 2025 15:42:47 +0000 Received: from mail-pl1-x62f.google.com ([2607:f8b0:4864:20::62f]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1treVO-000000031s8-0sF6 for linux-riscv@lists.infradead.org; Mon, 10 Mar 2025 14:53:24 +0000 Received: by mail-pl1-x62f.google.com with SMTP id d9443c01a7336-224191d92e4so73633065ad.3 for ; Mon, 10 Mar 2025 07:53:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741618402; x=1742223202; darn=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=DsyrYStd93Bm9fXzqKzktO2N/MRaifOTsv3qWLjFEsYc49xAmM7/F8NBYeq5V6uKIF qJGmZgnnfcH8UtT2+AJtJdQ0zwW9396//oNEbcYsJ+SaQ1j2fPPX7VGD5RsigmlT/so0 KhFmTk1dpsVwJ7R21FYZ+UgfjETJLdlPpJb48+af4R74okMNQLqn1bQduDMMMTMi39UU VFaxj+K5o3rYoQu5CP/o3idvHuiUTcQjiwFOU4wABpGv6vHvRwf/gG8wWb2qlMYiKen1 JcfpXNS6WpQ5WLN6kz5PRkzOvdWSpOX27VfSRPVgNcaURO5B4pz/J+8PDO0ntjl4L91s YzvQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741618402; x=1742223202; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=a5uI84SXblQRDID2BHNWhhHsRc7xVUDdoiei4kS9c/0=; b=NnolckCqL2U8srGZTXiBPZyuV4GtRZxxzd+z9c77Ouytx+i7w+4RmYwnttFUBXoYaY c4rXOxMfXEvLpENu2iMv2B73r8vJqkWgyf0kVAElt6sZTXHlS59y2vVQKBZu5kTKlFiv sVZYmu8UAynt8W2aXqU9yH6EMxAGd/r6k3GEWzIOHiSGXIyanamleIuzP0QGzDFovsaL ZPAQeZw+kxIJxdsgSXGVlbPzfocxhuI9XSKFOeVaXyzWrdt9NlQfS5FSbzjnC7UGHqMz 5Wo1dcFen5KLTEuHnZ2uBC2ESumKBZ/GcS3lCbp51y29G5+mIGbeQKBjiCeI8Ms3J0G4 ZsLA== X-Forwarded-Encrypted: i=1; AJvYcCXfTT8RYUkwxgS77DOYkmgU0Pf9+r2VpiBvka2IOr9WTbP+a9VL09VbFtZRMLpo5WV/yZW5r/GIo/ZAbA==@lists.infradead.org X-Gm-Message-State: AOJu0YwoEFmgF9OmGdJ0AfEJlt2E4TIn9TZbV9RI7qWNTGGhs5K52YEs Se2m7xtMp/weGjhc5R2QqqTvdAURDD32Hmp9iPPtKzLG8e1+69XZC93+dMPMtQ0= X-Gm-Gg: ASbGncsPcW62M7Mc74He8pdPOjGM4Y5Wv00yfpE+pwhdEfha++OBaunXTFKwZ0f/5e7 /M3Dq+2D6bR+BRRx1Spjvvt0qVw7KSILdMyAWpYrFXMOPXN+vLW/lz1hjjsxisLVSE6u6WG/5Pe rfHUkxKkjLBiBOgktRDH9fGiQmlnltcCEkXZwXJHFsxh9z7QiLBCph1CaETtkwGBzRO1SlUX6z7 EARjBR+1XqOrj1hyuGWWdq/t1BaeWU33TiChvQi3PP7udWqCI0+zkl9ZS+oSFV6DLdMpRkNcCfU Nu39WH/QI7mr25cIKtAMDM6T9H/3KgqGHDc16swY4ftFt1ZHkOcSWFA= X-Google-Smtp-Source: AGHT+IFjDwjQ9fbG75ByIkC5Z62BpsOlmfgaoy0NDr7CGAoT4eOeNPAoaYep0Q4HRAItC/SdOQSQyA== X-Received: by 2002:a05:6a00:4b4a:b0:736:54c9:df2c with SMTP id d2e1a72fcca58-736aaab75e8mr18259867b3a.15.1741618401679; Mon, 10 Mar 2025 07:53:21 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-736d11d4600sm2890275b3a.116.2025.03.10.07.53.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 07:53:21 -0700 (PDT) From: Deepak Gupta Date: Mon, 10 Mar 2025 07:52:42 -0700 Subject: [PATCH v11 20/27] riscv: Add Firmware Feature SBI extensions definitions MIME-Version: 1.0 Message-Id: <20250310-v5_user_cfi_series-v11-20-86b36cbfb910@rivosinc.com> References: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> In-Reply-To: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Paul Walmsley , Palmer Dabbelt , Albert Ou , Conor Dooley , Rob Herring , Krzysztof Kozlowski , Arnd Bergmann , Christian Brauner , Peter Zijlstra , Oleg Nesterov , Eric Biederman , Kees Cook , Jonathan Corbet , Shuah Khan , Jann Horn , Conor Dooley Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, alistair.francis@wdc.com, richard.henderson@linaro.org, jim.shu@sifive.com, andybnac@gmail.com, kito.cheng@sifive.com, charlie@rivosinc.com, atishp@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, alexghiti@rivosinc.com, samitolvanen@google.com, broonie@kernel.org, rick.p.edgecombe@intel.com X-Mailer: b4 0.14.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250310_075322_290018_59D329B7 X-CRM114-Status: GOOD ( 10.01 ) 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 Mar 10 14:52: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: 14010468 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 624D3C282DE for ; Mon, 10 Mar 2025 17:50:45 +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=ZbfL3VogSfa6Cg ZF4I5rYUOxTnC51/PPYos3tztvK+/BUbjik1kp9agVhEpDRe19ojk9KaqnDdSk6rRGpk/bZezLnC3 vZ6oNlcCBJ1ORIa4AfxbgjoNfqLElyJMNJFiOl8wJim69slo76Ke2d2Gbc8/iuigfDEae1M167OcZ v++z03RIucHD3WxvpMfMBZ+VN++ZOPACt0cSbqedKd4r0X4GxAV8ceKvUsT38+xu/TNJhxctPuLX2 c2+/oz8T/Jztyu2SkSSAy6bl7U+1EmVxQNPV4d+ppRtqO/4lxvznlGjEW6lpfkLeJa9RN2flQIt2Y C7w5pzC1j9UPZQuaTFmQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1trhGq-00000003Wty-1lT0; Mon, 10 Mar 2025 17:50:32 +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 1treVV-000000031xf-2DQD for linux-riscv@bombadil.infradead.org; Mon, 10 Mar 2025 14:53:29 +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=YHkNmM6XuphgUqeYDdwRc1f9MaOV9mSfy6OUDBv2dEo=; b=eLhqxQb4nYHq5KJewr+1yzInNY +2UbHNwZfQlwVX9bMytff7aNaoLViLMq6HiWbPOiF1eGRGYCdlpbkYV3D3uvQrPkd/ZYlnVeCPOth A9AYwfhVgz/R6jWCD7vKxZfTsM8cr5Pv4y3XzX7TYef4u35VGcjrbM9weP/G/Jxza5W2pCzvHwN82 qvC+vM/S8hF2YpKxXqW7V5svV7TYWf7pF+2S/m8RyMlyLzgx/VyuU3WA9pXKsab4wUeoBIzAapq6T jhK8bjkp/HvqkCK+6EXymUOw8vu+vtajhbJm562iZPUxLf25kyqScwtIcvuZh0932+BzpyvYGvPLY f7e80Wbg==; 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 1treVS-00000001sSd-2GHZ for linux-riscv@lists.infradead.org; Mon, 10 Mar 2025 14:53:28 +0000 Received: by mail-pl1-x632.google.com with SMTP id d9443c01a7336-224171d6826so63930365ad.3 for ; Mon, 10 Mar 2025 07:53:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741618404; x=1742223204; darn=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=cmOC11iBjWuP4YZmtTOy5w0EEgXi90P1XKvTcM4Bf32vr6DP33fxh/YkY0306Rdgg9 WDxg2I6GlFFAFSZNW26pSrb10rJMJXD8oJGp0QzAC/mzGoEipgyqrZske2BK+Q2mnn79 38S6oyYoevO5ONck6i9DgW7xN7HmL0enyNpgOs17OCgDUe7siDeT3ju3pANFRs5rgNRg 2XJsA5E2gDU+q7ptF/rXggy6aiJXmj6QlEsC1qoXfj6Ky4LNbY9yn5TGna16wPa/Ju01 l59bZO9LzR45nUThRRdde8AqRqvqgunm1nN/XcOXEfTMrtEh3kiH5AN5f/HpwEmjIcgo Xxdg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741618404; x=1742223204; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=YHkNmM6XuphgUqeYDdwRc1f9MaOV9mSfy6OUDBv2dEo=; b=Gs3TO8efpJqB/2xcVus4hgRF7mnm0mlMzfeO5CQ4VvBcaVRSxjzIorQn5/wZSIFf46 IXhPj4TvZmCcelsejiH5FzVNCUC1VImxOuCCMCBamPLqury5VseqAf64PmLSyYCLHykd AgbAig5TieZoJ0m3AccybsQwJw9u+0Si+1n9YpSzX7Jiej3L4zcH1ESwCV/RKTXMFnG9 dASly77+SezKE0aeFTF8ybljM+Z+02RZ4RE3Plnw2DJRYWsQu0o5UH4ejvuTKA530pEn kvLdv3C4GBI1bArm3zNWxXNioDEl4p5ivuL0NImiPsK9wS1wsTscO17ytn5iFvk+fcjI e0bw== X-Forwarded-Encrypted: i=1; AJvYcCX1I8JzSqQYUMV3QTLbmOA9I+Nh9f/CiCU5vK7hHboDKxhJYb7yYDvLMc9roJ2TwAyBWGnIt+Um0f4nCA==@lists.infradead.org X-Gm-Message-State: AOJu0YyibKTjwH9nCNDnSnHhOuxU8DMgt4gkyFKYzafRIfs9qOk2qHZY SX2gLeyP899w8SRPeM+ZsUg4AtKkgrnLvj3s9LMkIoczadRvJpYQHlUhhJWksl4= X-Gm-Gg: ASbGncvMfrtiTL5oB0V3rO/gZpbELVBdcrm0Si3sNWvOcoF9cApOh4cEHdX0VDpIeB/ WxL97nZkdGhlNouI9cNGEkS5lQ9qlizOv8a069LZubprYPP0LX4o76Ft7x6IbgRl905y585G4OV AjK8BdLtOQOVBBikvoLq36Qzk3IfDK0jMRr9P0l9dgz4bvDVnJqDuY99qswr/QklZSRqveYzVIZ irI4aU/mMsBJd8XMRfQv7ssAH2bB+Yp0s9+vp844VsXizXxrP5aGETfe2xieKUdmT84dEHY4E6V MBMPKSMLY8t/hW52e6tWPeuNilvMD+R309mN+ZjoS4LQRcf++7Ka/3w= X-Google-Smtp-Source: AGHT+IEufhMFpKiNcPYaN1q7fnoyb0yGUoGedR4PcX/LQ5rFfhM8NE7LJbQGAfjZIn8NhFa9HTR7qA== X-Received: by 2002:a05:6a21:b92:b0:1f3:3f0b:8abe with SMTP id adf61e73a8af0-1f544acd261mr21823779637.9.1741618404090; Mon, 10 Mar 2025 07:53:24 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-736d11d4600sm2890275b3a.116.2025.03.10.07.53.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 07:53:23 -0700 (PDT) From: Deepak Gupta Date: Mon, 10 Mar 2025 07:52:43 -0700 Subject: [PATCH v11 21/27] riscv: enable kernel access to shadow stack memory via FWFT sbi call MIME-Version: 1.0 Message-Id: <20250310-v5_user_cfi_series-v11-21-86b36cbfb910@rivosinc.com> References: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> In-Reply-To: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Paul Walmsley , Palmer Dabbelt , Albert Ou , Conor Dooley , Rob Herring , Krzysztof Kozlowski , Arnd Bergmann , Christian Brauner , Peter Zijlstra , Oleg Nesterov , Eric Biederman , Kees Cook , Jonathan Corbet , Shuah Khan , Jann Horn , Conor Dooley Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, alistair.francis@wdc.com, richard.henderson@linaro.org, jim.shu@sifive.com, andybnac@gmail.com, kito.cheng@sifive.com, charlie@rivosinc.com, atishp@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, alexghiti@rivosinc.com, samitolvanen@google.com, broonie@kernel.org, rick.p.edgecombe@intel.com, Deepak Gupta X-Mailer: b4 0.14.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250310_145326_914206_A7DAD6FF X-CRM114-Status: GOOD ( 10.69 ) 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 Mar 10 14:52: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: 14010469 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 A09AFC28B2E for ; Mon, 10 Mar 2025 17:50:46 +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=sUkhm0csd1wXyG b6WJI1i0El7U0BifQIVm4NNcXb4xsyoAnWKeHj2s8FzGh/KGddJIn3CSlb+WCM7CTmx6xmvOcw+sW Bpewm2wpsuKr59B3AT4/9XjqmkRE08HMKM74mKvfmbor3Fh/u/y+yvC8vKqQCgPpYGPsm8TMIR6Zi Y156AuMxLpVKCbyL3cK+1+KdxuOYLWJmJSmTVGhLdZDbzrcb7QDNM+DH3nI2eIKGfXlSQJRyYaGP0 IVsj573xkkfuTMasi4joiWY+Ju0EmTQJEcGID9I8BjsgUV07cHoTmrpRmSW5VwOaDgteIV59O+3hR wal//HlB2WvuU0M2BY7A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1trhGp-00000003Wtu-3hHL; Mon, 10 Mar 2025 17:50:31 +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 1treVT-000000031vr-0kon for linux-riscv@lists.infradead.org; Mon, 10 Mar 2025 14:53:28 +0000 Received: by mail-pl1-x62d.google.com with SMTP id d9443c01a7336-22548a28d0cso11742405ad.3 for ; Mon, 10 Mar 2025 07:53:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741618406; x=1742223206; darn=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=IQviqiianwC5Nf2LGt+hrOiDUM6ZAOH9XPWgc+euihxNP064/FZhTb0fadCO43CWR5 RbptDONJQLIiIiQt/xn0ECVcNy9JLa9ndwE+O6oTC76wpx5iYuREzoCUkqG918zEk6j9 aI++UdMX3y50etG8jsy1bbkHVE7qFJWrk2V02caRyEv0uQxweWReRW77nuHBKWbS48Q0 hvZB35clieyR/8HTI/jgfWQsdiOMhKJ6MgXRhpHEZh8HzaYfALl8jVboV860WiT+jaLj WyA1b2XnEWbZTdWP2FfGq8nMc9oVr9JiKLfFA0a1cQYe1F3ROMXY+3+3XkxBHh12A9ap cv8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741618406; x=1742223206; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=opxHamP1ejpgUrZttGLg4zwDHWHLF3xPs1/Qb/08WRM=; b=XUM5iULtu0UDkgYOMhdmUEpA0vo6vkgkxDYxwGHEWGaWwL0HndDogxBnx292HOtovF SmZDoMG/JXgoBDJ5gZ90JyKu9yGlep6hP1lZFodg6mdZcLHNYAXXT7Bj7sdfNMTXl1SF HSUfkemSr+IFno/Cry5yd1jBeQbnAWzUaYSAI3td1EZyqXLk5d26is6bEf5DX3HKulnN eW37Fc2bj4Vvgdfpgdw2m6JyfdGlMqKcRFgu7dg/0zU/Wx5N9Sl0Z+PxOz7T0/L3E6Oh OU0X9sEbB68VXgF9QAlCkab7u/k1WT7l0p3pPHlcvNvrKavGmjPam6vfhV2ZKctD+BNg uCDg== X-Forwarded-Encrypted: i=1; AJvYcCW6GimvZJR1XOGJmscGFS99twJyogLDsMUPFdOw8OOZkf1LsMpX4OyuKD/T+jDG6HRhh3uH2kbzz6n2LQ==@lists.infradead.org X-Gm-Message-State: AOJu0Yx3tLIIA6cfe+8Fpe3VBVYeOsRxlOUjPZGlFkx7hzATZHmFXX9E NrUYBcT1VOp6Vom84f9B0O9yYoq/CJthg+GkklbCjnpRoWQeA7Cc9Psjcq1MblA= X-Gm-Gg: ASbGncsGpki67R7oU1F/8pK7MkNmFyCH0Cb8h/S3OB9HxjvdLfN4YOJYlsP5gsd74ZB S7V/GSPrz8cci6SLsoRBiFWnwv+lTMgxXD8FJPGWB9vTjQKK7b9RnphfwbSbfK7vBwcRJvmbhgb Mh5I/Xx1Cp2MBQAMF1ww1i01PKEvDY3KmzXI3Y3hi42/eCa2dBvbs7EUR5HWgKGh9fbFVSDOgW0 xln6uamWARusTDF2PKQamoHbZu88gAkySek29xFuhRsct01Tr42JnePkOdxUFqR8vnnHCQkc7E+ VpY9cYyHLTx9cSnlaE38x/cd2afmfce/xuK5LJ4p4bMCHM5R/i9FbkI= X-Google-Smtp-Source: AGHT+IGghOp+j7sQ0Qt7sq7k8fXJkHLz3hwRxKC8iR81NfpyoYASRowQ1NRmeDSzKjKxRLkZDMtKZA== X-Received: by 2002:a62:fb06:0:b0:736:baa0:2acd with SMTP id d2e1a72fcca58-736baa02af4mr14991022b3a.20.1741618406530; Mon, 10 Mar 2025 07:53:26 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-736d11d4600sm2890275b3a.116.2025.03.10.07.53.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 07:53:26 -0700 (PDT) From: Deepak Gupta Date: Mon, 10 Mar 2025 07:52:44 -0700 Subject: [PATCH v11 22/27] riscv: kernel command line option to opt out of user cfi MIME-Version: 1.0 Message-Id: <20250310-v5_user_cfi_series-v11-22-86b36cbfb910@rivosinc.com> References: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> In-Reply-To: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Paul Walmsley , Palmer Dabbelt , Albert Ou , Conor Dooley , Rob Herring , Krzysztof Kozlowski , Arnd Bergmann , Christian Brauner , Peter Zijlstra , Oleg Nesterov , Eric Biederman , Kees Cook , Jonathan Corbet , Shuah Khan , Jann Horn , Conor Dooley Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, alistair.francis@wdc.com, richard.henderson@linaro.org, jim.shu@sifive.com, andybnac@gmail.com, kito.cheng@sifive.com, charlie@rivosinc.com, atishp@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, alexghiti@rivosinc.com, samitolvanen@google.com, broonie@kernel.org, rick.p.edgecombe@intel.com, Deepak Gupta X-Mailer: b4 0.14.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250310_075327_216402_68843BEB X-CRM114-Status: GOOD ( 13.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 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 Mar 10 14:52: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: 14010345 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 5D36DC282EC for ; Mon, 10 Mar 2025 15:44:16 +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=BdgUCBsENEC5nt9ytk9DQerMwQLDF+bzQpqTBc8jZNg=; b=CzSI98m0+2ifan eRJywhQzNQyAefbW8CqZVBSjzmW9zyfnxJz15GdK9ezB8+kw77r/IG89HcNCbiTmf9/s2zJSunCXz Y3zyPJdOwh6Omvd+WfM1hb9RkTXfbZOL1qMLVKAsKyPtcrzYX7vdjtIdXvvPlGt3eDpmyBwUrIUmc ERhwAn5FsmjRmvEyQh8T59lYcfIusPT/bw2/mFBfFcE70WHVCVrBdKwBEZWtI9krwHq6rLRSx2wje wRVP8L+6ZDMsPdqOWk9vAzAVgHPLxb8tLDbUvyDl9v0pCJv4ZkBe0UxLubGdfbYBfuxddQNJw6aQz RiiZJ/OolN7Vqpq36gSQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1trfIZ-00000003Ewq-0A37; Mon, 10 Mar 2025 15:44:11 +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 1treVV-000000031xy-2VWy for linux-riscv@lists.infradead.org; Mon, 10 Mar 2025 14:53:30 +0000 Received: by mail-pl1-x630.google.com with SMTP id d9443c01a7336-22337bc9ac3so82666315ad.1 for ; Mon, 10 Mar 2025 07:53:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741618409; x=1742223209; darn=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=U5ByeyW+YvFTHyUcGUYHAQSBVf/eKXKcnHA82UZlOkk=; b=KnR77fwpL6rmgShumn3bc+PhtMN9voD/27vqEvixof6rxvbQDsyy8bak7oOlo4RnWe WRu+3ASIwySJU86+A43us/UkVMlTkvVV4zIskjpSTLgmv/VdYhIGGd1LjjDspvMPmmv3 gAjSOUDCxJOOWrqjqFhI/YtQztH3lz8mSQMGroaMZCfXexRqFlG9Az6ECB0gZeU/6Yg7 bQJysG0I0FBjk5HGk9Gfz7nYoTOoDkQtYL2Z+XMx4oM4hONEtVw2BAYlkMtQYX+sODNI TV4jnD0UsFFBCsplFE1TRZjKs1xByavX05f6eGAS9rAs4VX66PelyJjRAqVq/Eo66NZ9 sNpQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741618409; x=1742223209; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=U5ByeyW+YvFTHyUcGUYHAQSBVf/eKXKcnHA82UZlOkk=; b=ZqLUCJPdnmeEMEgnKR8ykQ9OCVBgkZfLpWylAbZEWKM/EKXKQMCnSB20s9hUkww6+h LYowu983ge9/fBgK0euULgMIZNvTT/afOwaZdugSljbSWEcRr1EtQi6OwGOT6NamLMXp 5Jz5D4aTTnhQfbmOY07rNaabLmb+4wlNH1Z043tXrJIQRYzdInBTadvExpgBKZlFDd81 ReNl1e2+jZmxaXUC3O7R7cwGbFpxVvXxarVVWB3MIAoRueNd0SrgHoN2vRRwT0LcqJW8 8AkXMS66MdJ6PGYmQGtk/plzH6IikdnGkZXkPTWEHwOkhBzrVC0WxX26XY6y0fSxe+41 8HkQ== X-Forwarded-Encrypted: i=1; AJvYcCVEJHn8sXrX46XRl+zx2c1tSJT7RwvBglIOq2GLPYY5ufHYO1ye3xU36FOb4/SBhb6AV5Qtqh85NUUNSQ==@lists.infradead.org X-Gm-Message-State: AOJu0YxB1o7IskEWh2t8ZTeK8GEIxR3xgOC1bPVyKfg35znghCLla+41 in+BFM/2Cqb1e8vHFA4+RbbFeKcW6QuqdUZb7FdUUqdJHJ2dUZeN21V8Xc/qW6U= X-Gm-Gg: ASbGncuYc0NNf1SJCrSVJwjIL/z6AnQw+6WapOJ3EkyYR4rktEo8wB47wdsEIBpvT5A t/OFqLVbiMem1LnlMJHg9hMWMYLld8/sqdY5b2yHQKCdLj+hpipgPmNJPecGIhKCrmnXGLKzewd i1RqqWpdlnnyxtAORHsAU0U9P3nkQFSZt5E9rLzAI88oWlHSv55XvfDTcB9s20tAEU8azCN4wD6 oJK9Soz2VIvxW9qxhfg7VHTC9rg7tr5JY3IVWu9LAGZyEhLwUh0krzu4Qd01sI8Vl+rnw4Tp4x+ /pgIsUdMbVXCzIykyAfi5vxCK5ZODdgYBmXaxf5yOz59A6r/fE0qm5E= X-Google-Smtp-Source: AGHT+IGKXHgNMuKPuSz9mbzSVY6kbhzly1ukgPS10UZrRIAfMtlVXU0y2E7jj8WGEeH+e3hYVRFUmw== X-Received: by 2002:a05:6a00:10d5:b0:736:47a5:e268 with SMTP id d2e1a72fcca58-736eb7b363bmr272521b3a.1.1741618408950; Mon, 10 Mar 2025 07:53:28 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-736d11d4600sm2890275b3a.116.2025.03.10.07.53.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 07:53:28 -0700 (PDT) From: Deepak Gupta Date: Mon, 10 Mar 2025 07:52:45 -0700 Subject: [PATCH v11 23/27] arch/riscv: compile vdso with landing pad MIME-Version: 1.0 Message-Id: <20250310-v5_user_cfi_series-v11-23-86b36cbfb910@rivosinc.com> References: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> In-Reply-To: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Paul Walmsley , Palmer Dabbelt , Albert Ou , Conor Dooley , Rob Herring , Krzysztof Kozlowski , Arnd Bergmann , Christian Brauner , Peter Zijlstra , Oleg Nesterov , Eric Biederman , Kees Cook , Jonathan Corbet , Shuah Khan , Jann Horn , Conor Dooley Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, alistair.francis@wdc.com, richard.henderson@linaro.org, jim.shu@sifive.com, andybnac@gmail.com, kito.cheng@sifive.com, charlie@rivosinc.com, atishp@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, alexghiti@rivosinc.com, samitolvanen@google.com, broonie@kernel.org, rick.p.edgecombe@intel.com, Deepak Gupta X-Mailer: b4 0.14.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250310_075329_665862_A3AB07CC X-CRM114-Status: GOOD ( 16.14 ) 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 | 12 ++++++++++ arch/riscv/kernel/vdso/flush_icache.S | 4 ++++ arch/riscv/kernel/vdso/getcpu.S | 4 ++++ arch/riscv/kernel/vdso/rt_sigreturn.S | 4 ++++ arch/riscv/kernel/vdso/sys_hwprobe.S | 4 ++++ 7 files changed, 78 insertions(+), 1 deletion(-) diff --git a/arch/riscv/Makefile b/arch/riscv/Makefile index 13fbc0f94238..ea9468af2cb4 100644 --- a/arch/riscv/Makefile +++ b/arch/riscv/Makefile @@ -87,10 +87,15 @@ riscv-march-$(CONFIG_TOOLCHAIN_HAS_ZACAS) := $(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..a058ea5e9c58 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 0 +.endm +#else +.macro vdso_lpad +.endm +#endif + +/* + * This macro emits a program property note section identifying + * architecture features which require special handling, mainly for + * use in assembly files included in the VDSO. + */ +#define NT_GNU_PROPERTY_TYPE_0 5 +#define GNU_PROPERTY_RISCV_FEATURE_1_AND 0xc0000000 + +#define GNU_PROPERTY_RISCV_FEATURE_1_ZICFILP (1U << 0) +#define GNU_PROPERTY_RISCV_FEATURE_1_ZICFISS (1U << 1) + +#if defined(CONFIG_RISCV_USER_CFI) && (__riscv_xlen == 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..daa10c2b0dd1 100644 --- a/arch/riscv/kernel/vdso/Makefile +++ b/arch/riscv/kernel/vdso/Makefile @@ -13,12 +13,18 @@ vdso-syms += flush_icache vdso-syms += hwprobe vdso-syms += sys_hwprobe +ifdef CONFIG_RISCV_USER_CFI +LPAD_MARCH = _zicfilp +endif + # Files to link into the vdso 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)$(LPAD_MARCH) +asflags-y += $(KBUILD_BASE_ISA)$(LPAD_MARCH) ifneq ($(c-gettimeofday-y),) CFLAGS_vgettimeofday.o += -fPIC -include $(c-gettimeofday-y) @@ -40,6 +46,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 Mar 10 14:52: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: 14010346 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 F281CC282DE for ; Mon, 10 Mar 2025 15:44:16 +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=MkcU3bYs23P+qd w2o8Yr9Zbl/K2XfohMh9n9HnXtw6aZFos2yFtBUBt73WWTg7fbsl0FOakoUnmuWOpGV218A5n2RGy 43rJ6MDlbC7thIlhacsbWPdv5IO6W5NpyEAEWX22htNRHi2z+hNfYXoLrkz9A6P05p2qr09gZ2yyK W05gfs0JjuiPkWVdbe52NbSdbt6r1Q/zNnWMVI7hQTfZYdTos0AL2j6Guq5TBmGWrXkEvUx7tueCT O3WTbw8HTWwxnnqgmu1DDT7vImrjpXJJ4OGel7v3vDW04KkhtGIomzVO5cw1ngo9dKq+LdfBgZYN2 Ss1xZ910OkVtLdCNIZIg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1trfIZ-00000003Exe-3Ika; Mon, 10 Mar 2025 15:44:11 +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 1treVX-000000031zP-3mSC for linux-riscv@lists.infradead.org; Mon, 10 Mar 2025 14:53:33 +0000 Received: by mail-pl1-x62e.google.com with SMTP id d9443c01a7336-2239c066347so74061835ad.2 for ; Mon, 10 Mar 2025 07:53:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741618411; x=1742223211; darn=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=QCyOZ9LW5lFIq+4CKR6YFmrAH5SXIn6i2WvDTYMQWpB0ZoEn4eJtskoaB438jBxXav dEdG9rlZyPbrgF/1PZmIUljpsb4Hrh2H1G9k+eUaEwbq2G0dF0J1veA12oG/kkohe5Ud sPTxY+eNto9RalhNu2rl9J36DL2XFGGV/2hR8r2rnPDe6xL8QUL9JAcRADUnlcMseJ6m rDsmAEw6H4vkgEC4oWUDHverk9p7m1imVaGjTthUs1O0Xex3hcsUA6MO94C3yAHEai6b I8M7G8H69ARvCLbDKX1IontXWFt6TxIPQ5Y/xqrR4zRUaqcEFF3PW4vmGFz7ys6n4CxB WrNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741618411; x=1742223211; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=9LN1OOVFFK55bIQe9Bj7KSCICvDpdODqLxcVYOIQy28=; b=Vs9K1N0m9EsmbtFcki+3Ms1RyIJ9tuqTmQWNvjPr2wkj7d+zuKAeQqVY3E/Xhe/ReG xc0z44yuynp4/XxC4Om5EC1Ay2Y3kSW+9u3kO0GVF7PR5oq76XrMbtzG/QidQFSetAva YPwMts8IetCokJqc0BGZNmhacyAiFLWZGoL5LjKd+RPCogwjNMQ7+pwliblo0OkNI8HQ tupJqVE8eG2I8Goq8y6FBRSoLc9g9aXR+VDPhMurZnBdDplHsGyCnKC40I0DrCjoTELI zaeKNx7AsbqnLLbFnRis88iFtjAi/nboXJcR7wcTx2Ojwrisaftxc54dDdpeBxAj1sQC 9P8A== X-Forwarded-Encrypted: i=1; AJvYcCXMkSNCFWbBMMoSbgf/MpzJmxlpoiFD4qZhGzWoekaCIlaA7sc7pyT+j7mHfHFMZ4D9r/WOYbA9o0sNvg==@lists.infradead.org X-Gm-Message-State: AOJu0YzGe07wTjnvXIK8w+ytRnVu8DQNGqTZShKktsEoskDhSJ1eg6as h94pcuIXhiVYyz3+7MvNTZTvDrOy15d/feIqdvb9AhqaXmBHx0kH2DJ5rGt+H6Hi9TNASEGXW44 w X-Gm-Gg: ASbGncsovnxuKH+5h3K4EBXIk5LE4FP9rI1Z3XH5Kei2DHpry5eeGukzhi8u4LiQ1BB UtGK9JQ7c8qdOiTcZ3T4JBwDrVJfbyWaN9YnFp2D9Tbfe5MaaVKllVDmkXPp8WfXX9PRrw/uF7l e/vSIA6b3N3mdTLaZ2Pa2IilEb1/qrF5EFoQ6SBclL2+S8rWynpb4+gKwpkoyDVBOZF8zxfIfD5 erCe3GKsCdlf7V6YSy6lXIoMxs00ZyvR/KdJ3Z9fZDciSV3+45jKAjFGvPZBPlKxbHU4RpDB9s/ 4bxGOgqRcHw1MEVRlTNKkMalkpcViy334cQ2MHux09kYDdommtT4cOA= X-Google-Smtp-Source: AGHT+IG9tC1WaMP/xC1ABIbEOt50ns/8+sJ1nYsKd4Gr0OatbIpLJiaEKJdeufsJkmLrEp7EYv+TbQ== X-Received: by 2002:a05:6a00:928b:b0:736:5dc6:a14b with SMTP id d2e1a72fcca58-736aaa1ace3mr21470128b3a.13.1741618411393; Mon, 10 Mar 2025 07:53:31 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-736d11d4600sm2890275b3a.116.2025.03.10.07.53.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 07:53:31 -0700 (PDT) From: Deepak Gupta Date: Mon, 10 Mar 2025 07:52:46 -0700 Subject: [PATCH v11 24/27] riscv: create a config for shadow stack and landing pad instr support MIME-Version: 1.0 Message-Id: <20250310-v5_user_cfi_series-v11-24-86b36cbfb910@rivosinc.com> References: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> In-Reply-To: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Paul Walmsley , Palmer Dabbelt , Albert Ou , Conor Dooley , Rob Herring , Krzysztof Kozlowski , Arnd Bergmann , Christian Brauner , Peter Zijlstra , Oleg Nesterov , Eric Biederman , Kees Cook , Jonathan Corbet , Shuah Khan , Jann Horn , Conor Dooley Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, alistair.francis@wdc.com, richard.henderson@linaro.org, jim.shu@sifive.com, andybnac@gmail.com, kito.cheng@sifive.com, charlie@rivosinc.com, atishp@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, alexghiti@rivosinc.com, samitolvanen@google.com, broonie@kernel.org, rick.p.edgecombe@intel.com, Deepak Gupta X-Mailer: b4 0.14.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250310_075331_945655_AACED02A X-CRM114-Status: GOOD ( 11.61 ) 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 Mar 10 14:52: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: 14010347 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 8C765C35FF1 for ; Mon, 10 Mar 2025 15:44:17 +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=kKwZqr3lhattZC dq+JthYdg7cPyXehOjOUL2idTCj6OI8aXNVviQHjgt24saAWv7ARiVqYzfsRXet5AP0Hxi+1goFBY q1Sdd7lkwIRZoTLCYwGDeuN+D68DU2R5cZkCJ0UGkfWrsskqjnGqeQ8ZLmqo4YR+EYh7Edeuu7wTz F9fozMZW6QlZz0498hGTxwt4VL6O04Adepfvx3UuEeOUFfwRwEXoXpa6c+EWzOEtEYor6NRPTwPo0 H16GHEkqM5RvTOb0HzxmxgCvosMcwgHBWU2RKhW6yUYDUFid3qvzF7X5Vb2nUkwNjEuW6qqHIKTEM q0iNV/Ec4cc5iFBJewSg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1trfIa-00000003EyR-2urL; Mon, 10 Mar 2025 15:44:12 +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 1treVb-0000000321J-3Mxv for linux-riscv@lists.infradead.org; Mon, 10 Mar 2025 14:53:37 +0000 Received: by mail-pl1-x62d.google.com with SMTP id d9443c01a7336-22548a28d0cso11750445ad.3 for ; Mon, 10 Mar 2025 07:53:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741618415; x=1742223215; darn=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=xON9mpDuy6qYW9J03rTlIHalR3+P4GhOQD/a0e+tR2uvJAo63BbzoKLDwdbOLxfjuX mQjcCYi04MOuZUGObhO/yN/4xVN1zLvz8rmsp360liRdMX0kX9AVM7wy5L15D4Hnf39s vk2OM+oTNAhABHbgyfs/I3L63vRUy9Wm9kmU5pa02M+P9twLvH8E62jyyGYCkViR7Lq9 3PXbLNeXxwIFuBAybbibob31NoyJQk8iSzWqImvNnDIAS3IWOHXkFtdPIjUGFYRNyyYD Q+O2yL6AdzdY+m8XmdJgYZ9kAjX9oM6S5fj5gssz7WVrzxuGMdzaPygci5VjXFts3bVk opIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741618415; x=1742223215; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=vh4CUvpz/fzJO7m3g4Lrx30VWLQNLgBESiJnDVTGIvg=; b=pg4gJpb4K3MkF2d84VE++Uk6QIJX5laDnaB6dmmhQWSKrcKFY1XW5oKNr+jlNKyOF9 ki8Opwk/+j2jrkVKrKG0jprWLAfvbfh6qGqwPxXsOeEIqgW2LZEm09xhLghyuw5E7Oht 8ZfeHM71+t65enM8YdJAPsAJ9Yq+0g0kMmjhYHmNH7EleeWbPsmE/K4x0qQXBFxK6YSh yK+JI+Al5jPlv5+4D/TqckgmLTQM5FBjggSj4pGfnU2rWviPY56mIuMWPzzzCHPKER5V oYv9vSbW0vzcxT9o53Wl+wWcab8KDB8S/d4t9l4YYPR8q4/PHZZ11KRAmGhlSKWCtClQ qWRA== X-Forwarded-Encrypted: i=1; AJvYcCWdSvELDDwp+HnBETHYlFwNFDVA6JlhOJ4KYV+Wlk9hbY0X0DLsYxb/csWAlA4a38vlfdeNzDJH2RqQkA==@lists.infradead.org X-Gm-Message-State: AOJu0YxCjJYc6loSEzkK8pfAPNbEaUFzSIDP7DhBuRnN7M7GKWf+kWi/ nCAb3Ta17ZB9vBMtaltD5IbAMVG5nRyyZk4ND3zYHlrEiZu1/bCNJ28Chtvrbfo= X-Gm-Gg: ASbGnct9Y8s8CAoqlBZE8lTENRFCK083ghq4afczlcDCdx/Stqw3/c9dhqTXzmOaJxI RDc3G8buwFdiKjN8cM2Jgi5uVrI4TT+cUTbcK6mS6cP+ZtF9gfgc51b9q8nWU2UXt5EY6N5U5cw 2B4Y5OKGHnHSU9/M5rW/edVV9YXNnT8b6oSjMayFlF91QXeqt9oZczPm27twqUSMro4ZUPGYKte +1Sev05Pb8NjEAQD8WfFxYj3LfwI9fuu40jv4oUcMAC8+c0dwKkbu16eDT2izg+7d40sJgONStG nen6wfZx8qoG9SCCnKlQhIOdzEHmvnaJvRWzMWTBSN2MFyrErs4TyBo= X-Google-Smtp-Source: AGHT+IHk6fUGtyziYf/rEaJOxRl/jM9mUkmR597ae136siDXwL+YfMuIUi90ii9WUsyzBw7fk0+kRg== X-Received: by 2002:a05:6a21:158c:b0:1f5:837b:186a with SMTP id adf61e73a8af0-1f5837b19c4mr4882849637.6.1741618413790; Mon, 10 Mar 2025 07:53:33 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-736d11d4600sm2890275b3a.116.2025.03.10.07.53.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 07:53:33 -0700 (PDT) From: Deepak Gupta Date: Mon, 10 Mar 2025 07:52:47 -0700 Subject: [PATCH v11 25/27] riscv: Documentation for landing pad / indirect branch tracking MIME-Version: 1.0 Message-Id: <20250310-v5_user_cfi_series-v11-25-86b36cbfb910@rivosinc.com> References: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> In-Reply-To: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Paul Walmsley , Palmer Dabbelt , Albert Ou , Conor Dooley , Rob Herring , Krzysztof Kozlowski , Arnd Bergmann , Christian Brauner , Peter Zijlstra , Oleg Nesterov , Eric Biederman , Kees Cook , Jonathan Corbet , Shuah Khan , Jann Horn , Conor Dooley Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, alistair.francis@wdc.com, richard.henderson@linaro.org, jim.shu@sifive.com, andybnac@gmail.com, kito.cheng@sifive.com, charlie@rivosinc.com, atishp@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, alexghiti@rivosinc.com, samitolvanen@google.com, broonie@kernel.org, rick.p.edgecombe@intel.com, Deepak Gupta X-Mailer: b4 0.14.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250310_075335_853045_FE7F9E60 X-CRM114-Status: GOOD ( 19.48 ) 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 Mar 10 14:52: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: 14010348 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 B1AEAC282DE for ; Mon, 10 Mar 2025 15:44:19 +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=tXI7Q8fLniazdA wu2b849rN5T82eHulliUzaEoEUT87syKWXpidxQwxoRdfgz2SUuPUBE2kfIaAA1tY8APoxIJEP25F DrAEdwwhuihGd/xDdMdE/4Yr5jwtn5v2PPR2quLDMTzCb2KNmnT3u65A8bT1tz1GKhgbMazBB4lKm AEtRxpwevkWQ/GqGALtcu9OdsT+ON2D56yR/3LPVpUKzp/AmCTptRoHJJ9yVz/cf9WF7qvpfc+NTh Z0dVmYJd07rZRG2Fb9QyaTjQQkNYamUMyKKB0zzXAfXZSH1WymjIC5+0SGAZEh4KPl7+nxrNwbyJv LXnu6ZkoBQh2YJVZVF4Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1trfIc-00000003F0D-0jYI; Mon, 10 Mar 2025 15:44:14 +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 1treVc-0000000322F-3a1e for linux-riscv@lists.infradead.org; Mon, 10 Mar 2025 14:53:38 +0000 Received: by mail-pl1-x630.google.com with SMTP id d9443c01a7336-223378e2b0dso62392405ad.0 for ; Mon, 10 Mar 2025 07:53:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741618416; x=1742223216; darn=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=GN6MwOoh5ILyNUlH62+p79RjLpO+uJFiI+RJSaELyx0y5K1WTNwAK+BbO4KwgHGQpI bVMGzNi/ny7PQDrErZY0U4dOXUhcaHUeOKhw6+5hIKr4T5zFH3U3GPZLuyPrhxXVJGx1 9ETAhoCZlsJxS2w1FLpQ+i9hcIuf0C1M7AplCY31eq+VgudhBGgjIl4Zxnwg8Rfk6j0I cfoTTgymrPgn2pjkpT52XAOPO15l+AjiRaOZn2qS2AaFclch8WdoCKg/SFVDEQx7mzQA zCe8y3kcv+u3HgMlxtJa/xmuFq7ezWSWSZbV1Q0oGjB/1fAsAoCtjfvArexcdYpLFrSr FzsQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741618416; x=1742223216; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=tdDNQkSVTKtNTxsV0uSmAnlAiDQNW7ggaTdWphzDp8I=; b=V1lFD3JatYgK1zAqlZo20VD38nKTM2yrzZO7syUajz4NDRAMG7sYFAfAYgyDVzUa1u VtQFx7cloup5+jgreaPeWwrwZMMNj8DICT4tKsN20+ntIibeiSwIGhbQxXSqzu49deg4 wd+AK4nWFsJfMYcQEHc2uj2WdbnnHNjU05ailpvG2RlLIrWfFv5+RdXQsyQ674F1B/K9 9Ax7Wr+JW5bJb58Cjn/3kNongtm+xyMsH0wVEB65bqco6tt+jnD+RevWQu7NH+XscBnG IifL0dIcDIR8aAZ8Fihsjd10ZqwwyS7NLFTRepBoMacJF+tyNPtP86hNaunRG6DB+mn4 y5zA== X-Forwarded-Encrypted: i=1; AJvYcCX02B5xjfdDLp4WL+lOG9N4AJzxfnbia6w4BRPLQx1oSZ5sHK/zFJ37YLxmSjm8KtEdTN36jzo/HeehRQ==@lists.infradead.org X-Gm-Message-State: AOJu0Yz81mi7HoB/j/eTtnnZ6RiXKOrqdf47EoL10r2/Wk4seL9JKSHM WYL8GACJY+7AWJ7sC68enlQSihnj/IQsB+2vMHJytjZoSb/w1wcGo+A1BqEFfwg= X-Gm-Gg: ASbGncsvr5dHb0xUlrJSJqTZuqqBlHMYDn8VTBuH98fQSmZKSC2+344Usae9/TpC9k2 yKoX1p1kmqPo7YwP1EzPxInOaFvbk1JfubXwMUETRM+ZQ01ouY0dHdfYNsCa+CAxvPBVs8SoATn Z6F1fTcJG6o3xHR7j6+XBsMhddQBQR8k8ptQQab5AZY1YCXi2fYTIG+4/KguznoZuuR6dMxt+M+ bpiKNaRREENT6h8Siib8gZfDSip4c6lF9rwwYlUTlf+wKLdxSv59pzY6V/+OB2lrlywNWcDe5Xo JvH/ZnmvxFB5vbfDucKEQQa9Sb9JkyZha3yQJHJk/odpxBa7E/NOf24= X-Google-Smtp-Source: AGHT+IFaFODVJO35zmAo4CgY/g0ssVNrnGer/dAkzpFZFHLuf8O1kgqUgyNRnEx4GJyvPtN1uJ38Mg== X-Received: by 2002:a05:6a21:4606:b0:1f5:7df9:f147 with SMTP id adf61e73a8af0-1f57df9f49dmr7386102637.40.1741618416216; Mon, 10 Mar 2025 07:53:36 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-736d11d4600sm2890275b3a.116.2025.03.10.07.53.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 07:53:35 -0700 (PDT) From: Deepak Gupta Date: Mon, 10 Mar 2025 07:52:48 -0700 Subject: [PATCH v11 26/27] riscv: Documentation for shadow stack on riscv MIME-Version: 1.0 Message-Id: <20250310-v5_user_cfi_series-v11-26-86b36cbfb910@rivosinc.com> References: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> In-Reply-To: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Paul Walmsley , Palmer Dabbelt , Albert Ou , Conor Dooley , Rob Herring , Krzysztof Kozlowski , Arnd Bergmann , Christian Brauner , Peter Zijlstra , Oleg Nesterov , Eric Biederman , Kees Cook , Jonathan Corbet , Shuah Khan , Jann Horn , Conor Dooley Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, alistair.francis@wdc.com, richard.henderson@linaro.org, jim.shu@sifive.com, andybnac@gmail.com, kito.cheng@sifive.com, charlie@rivosinc.com, atishp@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, alexghiti@rivosinc.com, samitolvanen@google.com, broonie@kernel.org, rick.p.edgecombe@intel.com, Deepak Gupta X-Mailer: b4 0.14.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250310_075336_910454_ED94F1C2 X-CRM114-Status: GOOD ( 23.93 ) 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 Mar 10 14:52: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: 14010467 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 8C8A5C35FF1 for ; Mon, 10 Mar 2025 17:50:39 +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=iwbaqK2RCQ5DdP ehX3wTofduccJlGdlha1I3tr9tnSHnbpEldfQwzHbaxdaz/qKIN+CBrTkIJR6iVPJPXZt9061B5DZ OIc/SrykwEEdG6kiC12q3ejMvexAPLfJYgTpJkUxGYUkRdWRVQEFdgFTJH3xv7IQzMHsbMBPsjfZO y5mT+/PmF0alKIiidRTlus6W5lhzPLhPAJ+t2t6Rapc3woMGupuZV1UgHGB5bm0BBfAGYnbOF5bPf xdoT0xwQVFmf94yAqptNUbrCAj6z+Cox/Fgq8HNIpPzAOt9C235E+8t3bEdrtEuc+a5LslMfVt6Zg amSUTahGtzH15lQz59Vw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1trhGq-00000003Wu0-2pOj; Mon, 10 Mar 2025 17:50:32 +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 1treVf-0000000323o-1xw3 for linux-riscv@lists.infradead.org; Mon, 10 Mar 2025 14:53:41 +0000 Received: by mail-pl1-x62b.google.com with SMTP id d9443c01a7336-223fb0f619dso81637755ad.1 for ; Mon, 10 Mar 2025 07:53:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741618419; x=1742223219; darn=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=z57BjgIL+PUUNwMcRa2p0mLdEmauizezwnnnLiWSgJKlC1G8OUXhPBbM8NT7tOcGyg IOGqtOwPtl5ABGHb4EQ4pw5kYWV1j9I19J2C+4IHlmvaHE1s2HPJP7rXV2o0xMr76fnq rmokUaFcL1/gz88dcJfPB78zmNHcvToaQ9hqlYXLRvzu/fbB8ddetBl1jveizI22s0cn ikq0jlEu2ZNoQcHE28ryPHAQ8D1yiQOwafiC6dNHptzqHSew4jOc2QUwyizBhSW8/UcV deRlaA8ONd0ORa5bIV4m4kItYR1BUWerVmtJ4GvbP9vquZaS1u2m7y9UU4uX9VXV3Qqk o8Hw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741618419; x=1742223219; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ruj/JnwPHtkrEOpCEVLVNX7RrlNYJUu0NWWeo3U6Fsk=; b=cpAlYmij2gLHc9L4ZKuN4hNQ2Z8BFzmwAIVDbbL/bn38OMBVi4zxhFwx3sl9+QNdwx ulOwcq+feeZQoNcHS8U03i9tz94hkr74MzPnolcoYcqg2lYFe48qa1Wz9g9N3K3B4uCf RzgmY3n5Ym/wQKYNgGLrYnm3ljIXpbq91tplXsAZv6XsXF5CG/yIyexJ4t23pPqIBwyV GR5Gqj1s4sl+8rFD2FqmIHs74+ajVxYcp+chEex2R6U1PusLB1wR2MSOTTakqwvCEnz/ gDEnq+I6mPuqpeAqaQQyQRFliS0HPRKWEEfzD8rvmByoQUQQm7/n5XhDMLCzVR6dnsln Pu2A== X-Forwarded-Encrypted: i=1; AJvYcCWyMkFnYiKCeIviOTF/NSCmQ7H+Zp4VX6eVpCKL2Yuf+ndb4UgnueqR7b0iRry5fao0lSx8nmavJ8OYhQ==@lists.infradead.org X-Gm-Message-State: AOJu0YxIcTytR4IpVUXEUwTLZ5hMa72v24ZVcZR9or4JREBYugMpyTBC w9oL/v2oT4n94CxrIykQ2xUpr4RuQxEXmcnAk+c680RPJ0Zkr0ZnirJUyw04FO4= X-Gm-Gg: ASbGncvI4sGUAObUfLFAoaPzN5WYLdRZK00XEwYeA1YCPYtuXt8yu/SePSU+T2dwi8Y pNPl16mVE+YQD1yyvAnT1eHeCfjvrczzBlnt5NLMO2DysCwbu4jzR1saA1J5WGZgx+MZ6O+oyQs +RE47eaaFxwfgdclu1Ze6R7NVDPKdEkCMB4h0g4Weix3VrHxyY8fZ/sohrMBGTj1MI0LdKkYccn TMOQcL5qojSPiS3upVBEW5dmcWAlZP0lkPTT3cz9FO4Lp2KBQfmo1VfhfC2SbejRsiahuRuOQ4L dwUGt23cze9nbCinCOe9ioNg046Nca3hF9ybHrHx4fuNKIksjFL+5Js= X-Google-Smtp-Source: AGHT+IGv7tM2PHL9ys+s6t6SEB0A82+OsRTHqO1WWLNqv0N4xUl6sYQq+4x8MhxgrmcY2aWh78Ovvw== X-Received: by 2002:a05:6a00:1ad2:b0:730:4c55:4fdf with SMTP id d2e1a72fcca58-736eb7ca1f6mr271137b3a.7.1741618418779; Mon, 10 Mar 2025 07:53:38 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-736d11d4600sm2890275b3a.116.2025.03.10.07.53.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 07:53:38 -0700 (PDT) From: Deepak Gupta Date: Mon, 10 Mar 2025 07:52:49 -0700 Subject: [PATCH v11 27/27] kselftest/riscv: kselftest for user mode cfi MIME-Version: 1.0 Message-Id: <20250310-v5_user_cfi_series-v11-27-86b36cbfb910@rivosinc.com> References: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> In-Reply-To: <20250310-v5_user_cfi_series-v11-0-86b36cbfb910@rivosinc.com> To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Paul Walmsley , Palmer Dabbelt , Albert Ou , Conor Dooley , Rob Herring , Krzysztof Kozlowski , Arnd Bergmann , Christian Brauner , Peter Zijlstra , Oleg Nesterov , Eric Biederman , Kees Cook , Jonathan Corbet , Shuah Khan , Jann Horn , Conor Dooley Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, alistair.francis@wdc.com, richard.henderson@linaro.org, jim.shu@sifive.com, andybnac@gmail.com, kito.cheng@sifive.com, charlie@rivosinc.com, atishp@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, alexghiti@rivosinc.com, samitolvanen@google.com, broonie@kernel.org, rick.p.edgecombe@intel.com, Deepak Gupta X-Mailer: b4 0.14.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250310_075339_541590_896AE103 X-CRM114-Status: GOOD ( 23.43 ) 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