From patchwork Fri Mar 14 21:39:20 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 14017526 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 774B8C35FF6 for ; Fri, 14 Mar 2025 21:39: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=Tk97CLjujx8kLXhmHiYf6VqelQvRtdvO2pydQZE/S7A=; b=Qu1wqlrs0EBbp4 vOCJGrJBM+Pb1YIMiGdZLVmk495lQoQREr7CR0eNHoWlxCelUAtuN2eQ4w0F5A1PsBbjqVqeF2RWH DWD+9KdN/VHcacE44PqIwylhKpCa4VX3IhonGrXY046RHRPsTRQidZSkVBAND2I0ExdzidWuIIhzu 01JaJ337aaKVJpbKzEUBhMk6rdVRAWCsQQmgPB6AGiQlbv5igd0BRwzY1jQiPy+qLiXwfwiWsSILK SCzfI8WqBZDyf+ReyCkk/QSzFE2whSS+HdNTvDmsjIsSnWP5fQkKcmTC2Q5iQv0KLBEloV/bFMJAk nUSo4qL8ByGyuBGf/1kw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1ttCkf-0000000FKDB-3kuC; Fri, 14 Mar 2025 21:39:33 +0000 Received: from mail-pl1-x635.google.com ([2607:f8b0:4864:20::635]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1ttCkc-0000000FKAa-31j8 for linux-riscv@lists.infradead.org; Fri, 14 Mar 2025 21:39:31 +0000 Received: by mail-pl1-x635.google.com with SMTP id d9443c01a7336-223fd89d036so53920155ad.1 for ; Fri, 14 Mar 2025 14:39:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741988370; x=1742593170; 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=SDDw4jYk8ElhORayLnctVQ+77ceNByh8dqs8gmZ197w=; b=0PoTYUQwPwYvx7z6lz/bHqD5zBhwtQZ0OjrIE6f9jbrlOcgj9vpR+sS23shAe00Zkx dF1KeAE9yFG7GoHCuHkUB5291RAhPt8UaAHzI43PCceXzzrHUR/TI03/sCLXRx9iKg/r p7HIZ5IN27kbM+ASL3wz6H6NwjAp1WfDZPRxzh+lTj+HYSruLwONWcL4HUrRRhe81PTA RPa8cZAxJxYGw1kNtNxea6hJaSF2hWXwzwu9reJ25eWSQObYb3yCtbJURguWwBucSf4F 594xqNqou6yYmPuk7oF9fyNw09Mm3dk7XlSAVl7gLjML2+7nbv98bWFB6ygYhGl9DMhx di/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741988370; x=1742593170; 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=SDDw4jYk8ElhORayLnctVQ+77ceNByh8dqs8gmZ197w=; b=LHdVQkf7l2I8yOMDvGkdG0BPsL623phHaUJqlDcSKWVcWijSLxiPxWrSip+sdmtZWL sVEttMgDe/vTOaYZ0GBr0jI+38zbntCPAd9FVw+K8g+4ABgeFVZE9akPTmQGWy2yfPjV DU+UElEjjZ2pAkgiPvxwKBwVjzj1ldm1X+Ca+oE26i7nv7BeNLYOuQLVMaM7FfHupqOl NdewM/ez657WYGHrnADd6PoRDq9yHcTSSPdsTdajTZfJwvx5LuzRa6yVSFsDcnIDtOCP OE9afyoFt+dA8GOf1xIvtR/G0pFHWZ7YGsRWgIcy27mHU/rgPmDCwa3zjeEjOQAOb7ne oyxQ== X-Forwarded-Encrypted: i=1; AJvYcCVqFqHc7CxCp+49Uf28Uj5h0qFzDBZok73l6HB7YMg2Hyu9CnfCZGPMZ+OqtAF40bZiyGsAGRt2TQpHFA==@lists.infradead.org X-Gm-Message-State: AOJu0YyRUj8LPAGO/0B7a+FrxefYqIbG5VoGRg/Y6bR0NiUfpjlFMSU3 Y6hTbx+eDxVS0LzQTcXo++wvCWARYktAQxB7B6xD0Rf2uEav8yVkI+056ULsuQoHNUxikJilo1Z T X-Gm-Gg: ASbGncudF0R+F8UdmnYiKJUkP8eHeDP8GPnUI4088spCZX8o9t5hblqx1MHoB6j0gzs JmQO7ERERnUcdyLBLzSXhQXGSMSxHzSSxKy7iqDJRSkuXaQXELOCL81xTFfwomL6/eW1f1VFAuy 9sUt9KTle/s2Xss2OXuVH1xY2ycBTVibO03HzLoBLq7Y5bktzYVlF5iyCZAVgyOIX3OBT++o7s2 CdhDLjx2XxueLJeFr+XQcdRPkFkuaVT0Xxu//9EBnuY4S7iv77BQT4LGAHQ+4JgnJ7rnjwJkyyt AZO4h1JEmfme/Ob1g4OvsEE7k2o0CyQ73F73mVcw/4vuYub/p9FgduVt7ibD83yQAg== X-Google-Smtp-Source: AGHT+IFpg7z+OE/2A1gqmKjw7jqoEU+X8gr8RuneE9Exr7j30nKzCtL2OnQx5eLIT+ktDst/E1FOnA== X-Received: by 2002:a17:902:db04:b0:224:8bd:a482 with SMTP id d9443c01a7336-225e0aa7e06mr46126855ad.25.1741988369862; Fri, 14 Mar 2025 14:39:29 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-225c68a6e09sm33368855ad.55.2025.03.14.14.39.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Mar 2025 14:39:29 -0700 (PDT) From: Deepak Gupta Date: Fri, 14 Mar 2025 14:39:20 -0700 Subject: [PATCH v12 01/28] mm: VM_SHADOW_STACK definition for riscv MIME-Version: 1.0 Message-Id: <20250314-v5_user_cfi_series-v12-1-e51202b53138@rivosinc.com> References: <20250314-v5_user_cfi_series-v12-0-e51202b53138@rivosinc.com> In-Reply-To: <20250314-v5_user_cfi_series-v12-0-e51202b53138@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, Zong Li , Deepak Gupta X-Mailer: b4 0.14.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250314_143930_764006_B25D9CFE X-CRM114-Status: UNSURE ( 9.47 ) 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 VM_HIGH_ARCH_5 is used for riscv Reviewed-by: Zong Li 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 Fri Mar 14 21:39:21 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 14017522 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 E475FC28B2F for ; Fri, 14 Mar 2025 21:39:40 +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=KAQjVkKew3Gk3y 98C0M4b3s1o1l6R23Sp9m1G3AjKoXoMAfzUkwo+N4UxukhiZ3HeGtMRqjpac2FPhfSZzGt1FoS+bk o1zqR4QOXTuMhk5spb0KvzW6JbgTUuk6BtxBKhlNP2Zi8AIr2cl5rLIVy9INn/Ar5QYtc0aIO34vi rp10a/NFLWW6MMK3ANRsV4NXjDVOBJzxK4bCJEKTM7/gL70kq573qDsLSC2AidkFozCuIX/BydQgN 9a8TaRm1vu0PlQFFFHBNAbhrKOiTD9i32QOY+V4NdrJSL2hkIZtKN2yAB/6xTre+NOQVR/BXy7FIS drFxGlv/FujiyHtEPIYA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1ttCkh-0000000FKF0-2NOu; Fri, 14 Mar 2025 21:39:35 +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 1ttCkf-0000000FKCR-0Itn for linux-riscv@lists.infradead.org; Fri, 14 Mar 2025 21:39:34 +0000 Received: by mail-pl1-x636.google.com with SMTP id d9443c01a7336-22113560c57so51668525ad.2 for ; Fri, 14 Mar 2025 14:39:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741988372; x=1742593172; 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=wYPJBMt0khCYk46NZEo7SRJzO67og7Lbrx9ND7uZI0+vUgSCc2g04XT4uC1yWAwsOf 2dpLllwBRW6IzwC8Dax//iDO2YQN8Lmr6Vr4oZ8yVkWlZRPP1rfVV3eAu4rUOUSZaxJW fomuFelKHvThbqwYGDE9yUfZ9QHbiG4TEsAz+mhLX9TEy+ccIY2G46wMmJverh6xCJKn e4tURJLp/tXCGfbUBVIgjzqRIEfjnJtT3nxfc694xEN+yLZuixly2176ch783Po0dR84 XZTop1MY3sUn4mQO6uLz65BuxA6yKNahv6/U5GIorPauqyb2twAJBvzCbVxG5JyhApVK 1dXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741988372; x=1742593172; 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=ApUbr0Hg9WUdovXGHCPyfoy2xTrZfLnTeUY2/7gn+FpsPbk0HprrMvHasfY1LWoXl0 ZVXMGC+mEXp0g4daXCukNfb1fdQ5mTuMe0an4BYtJppWxr/nr6PMw3iCBiMANUFBcscu 4CnwTjKE4h1ZNBluay+gktZwyDN2ERJJYT6GzF7AcsSfE1DyfoElErptB/LhG9OV6ToR vEZ+kjYIC1i4RIv1vCEG+qiPEgHi9qUZG1uG5fZEd1F5KWxuR+5puUgS0KBA8HCa7/fx T1puIBHw5b9h1fMwlCd90jWk69TN2C82C5lly8iSANunZ0HY6A9S3xGqpqHUfAL2VTfa 6BNg== X-Forwarded-Encrypted: i=1; AJvYcCXF1mvBqd/SuNYy9YKTapjje4Q5V9TkhI3MW9/sFXcAyZcYbOIiavTivZ8ulb8L4wm4M46yB4d9DRoZAA==@lists.infradead.org X-Gm-Message-State: AOJu0Yyajb1puYxHZJJrvx2pwcBhZQ3v41LM04mOHX6NPCvON6mgyG9k BJ3u/YkNKh8bKbXGjDiMNMKreAbvkJ810uipJqmQk27shKqqUYcU5rVxQ9kZ1+8= X-Gm-Gg: ASbGncvcbalGjsCyFxCUsQQyHE/SiaqkRQIOPSGym4vew/qGrii2KsYVW2PwXTRrnnI V4d4KuEzxV+Yv6KPlyDyEQxSPltATR3hLil6VKrIkAZ9DmNeJI4mhVfy/OhOzRjfe3hmG+KL+dF bkvzf5hn74asW7QArfSg/Wfhgb2uk2puDDcpaFy+zDb25dkKXjss3KqYcIH7bCQCvTdo4BxSFiE t25Ms/d1dSqUbQoq0VGUFwyf/0iav92e2oppyth8Q3EBN7wffy66QwSQk5CkKruC329j0FcoFXr YPLfIYAxWp7JOnJj6U62ovxhXufyFX11b0O0ppi+XFkuxW+8YK3hIl4= X-Google-Smtp-Source: AGHT+IHD2xqF6ISXqrc66wt93NwJ9QpiMUhs8Y+9GVF8Ev9srJWeDiSZWraNiRIMMMrHSoWJEgWxBw== X-Received: by 2002:a17:902:d50e:b0:215:94eb:adb6 with SMTP id d9443c01a7336-225e0aee9bbmr48831645ad.40.1741988372408; Fri, 14 Mar 2025 14:39:32 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-225c68a6e09sm33368855ad.55.2025.03.14.14.39.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Mar 2025 14:39:32 -0700 (PDT) From: Deepak Gupta Date: Fri, 14 Mar 2025 14:39:21 -0700 Subject: [PATCH v12 02/28] dt-bindings: riscv: zicfilp and zicfiss in dt-bindings (extensions.yaml) MIME-Version: 1.0 Message-Id: <20250314-v5_user_cfi_series-v12-2-e51202b53138@rivosinc.com> References: <20250314-v5_user_cfi_series-v12-0-e51202b53138@rivosinc.com> In-Reply-To: <20250314-v5_user_cfi_series-v12-0-e51202b53138@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-20250314_143933_127833_B3FB7693 X-CRM114-Status: UNSURE ( 9.13 ) 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 Fri Mar 14 21:39:22 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 14017524 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 8F224C28B30 for ; Fri, 14 Mar 2025 21:39:43 +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=36/1fcDcDJuiHBtBoJOxOFQNHucjTo+Sp7h0Af0s4GQ=; b=bUlWA+t+f3+lt9 LPpr971F4m11PR3Fctn349uFrC9Q27siRjJ/CNtRS9GGsNBWJMuUA6OL1M/BxUvp49C2Ml/hB1+AA zLkl/RX9q1R8MdKv0Ue1qCkJplmxD78yN+NI+tAYpqZVWuTG3ZmlLiQsLiIDZZin7EBrr4ty7SKzk bWZokn0LKTjeU64iF8KAIcCClC3PhQZfMinK9jhNdGB3csi5dq+1kQgTapqvemG3xxxA0DM2YmWJC khYM+51MgJZvPH+ZrvZ9kV3lM2HPX82qTFB5hxFnK51/WEAgUR9y7tw6+z3I68fI/YjhqCe6+EyYU eddFtHebmy8cCvroJ9AA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1ttCkk-0000000FKIV-1wsX; Fri, 14 Mar 2025 21:39:38 +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 1ttCkh-0000000FKEy-2fcM for linux-riscv@lists.infradead.org; Fri, 14 Mar 2025 21:39:36 +0000 Received: by mail-pl1-x62f.google.com with SMTP id d9443c01a7336-22435603572so43463495ad.1 for ; Fri, 14 Mar 2025 14:39:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741988375; x=1742593175; 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=2gHC+OQH/HyLVaEB8d5yyptpM2IOJ5Z6rZqVaRKPZQE=; b=NcKMbW2XNURoMV94LUjsV+dsN75rp3ZKIKfw4d8uuRu9piLbzHidPU8bca7tmmb09J NT56/z+Z1+tDHvTD41Orm0CJ5zw/X/wS/EPloBJSnLUfuAug6rzhs3lfLRyER9rnbSGQ Zbw/g9KDZEjS9/HF2XkwKi/q/VE0LkVIu3FCh0uzHIjPi5vHwVqHg9dtYLVV6RnVDi/o zqtoPZS5ICVwvnswCIm+fht9TfK4HV/KADlhdzvQT22olMv7V/ZT9v3vTGM8WZAoS7VK XWlo5ACiaR6hujNSTrXyWFD5RePsM13txNO4w/U2XWQZU6g7AOcXcIXmgkI05oRwlpw4 dZ2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741988375; x=1742593175; 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=2gHC+OQH/HyLVaEB8d5yyptpM2IOJ5Z6rZqVaRKPZQE=; b=dqVtFj1xmhLVhA8w+7A6UrVKynZ0EyNoMS9LLz9rFlYQgrJtxHF6sV+5O1sOMKdocI EwHoJCsrzoIDhVJiQQgo/GWmAUkFTO0Ce5J5KhLHHFfCs+ghrD8LcGqV7CQg6wIxKyB3 L99WtB2FxgiyIQ4crsnikS8TVxtc/QmKs/gCK8ASNpopREYOVBWR5Xq9oRtjb+++iA9j +IdpWurYmfTtQ7//8Zw06Fb5vvF0Y1a4ZZqSfKV0mJNtfisTj3HAwqe49HRpjvIf14hN 6XyKzgQNFxtnGCe+C7Y/RH20V/+ze6Nx9SvSt/touRUG2A5K3hl4j5ankVT0lXV3Cz1e fwcQ== X-Forwarded-Encrypted: i=1; AJvYcCXC+o5bkH/MgtpdeZU/SJgXw8jbZQILWqpTzf+dej4DHRbvPRZXxrTwfNUuwMx/8f2sUYFWDyL/5bCreQ==@lists.infradead.org X-Gm-Message-State: AOJu0Yyt4pmnCJLzMQ4Gg2yUP2pqx3lug53UccvuZk1ngnQhEkCJCXJd jxOa95dsng6j/LhAJQveID7Ppz0kAG/dNGeJrRNDTPuzuMMfZF0V7CTvLiuBIEU= X-Gm-Gg: ASbGncsNDP/OFIC3YeX256bnnzhRRb2t4KUYwglR5sGEl082bqoI7Swx9Cg9naM5fnD 5du6zsBG7kxlVkiOD3U8xBvhVfH32rMqhlpFCZ+o2v9SkPcJegKslaJCQy8p9ibgQI/h9nBhIod XNXUYpKpgtqbfD/c0ruYPkP4BzQx1AN94VyuW0+C5DbjbS5CZbmXOcT/xj4IBH1L3BYivtyW8BR VgaXPat7gTGV1qMVaVPFdnC6prWlzDtbUrcE0LJYWX4Aq9lmumhVUK4GzDVsPhHR3tDKxodm9HJ 8hGDc6YO5YPess//uqapGkRE/MuiR7ud5aikHGGtIjP9bcdx+0BVBHU= X-Google-Smtp-Source: AGHT+IHvHW2W/QvmAtTQpnpsWWRaM1HH0j3AoAcqaAS/SF4n712ckDXuPjUXwXP+UZCTKkiG9rvh0w== X-Received: by 2002:a17:902:f54c:b0:224:1935:fb7e with SMTP id d9443c01a7336-225e0a75439mr57801355ad.24.1741988374981; Fri, 14 Mar 2025 14:39:34 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-225c68a6e09sm33368855ad.55.2025.03.14.14.39.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Mar 2025 14:39:34 -0700 (PDT) From: Deepak Gupta Date: Fri, 14 Mar 2025 14:39:22 -0700 Subject: [PATCH v12 03/28] riscv: zicfiss / zicfilp enumeration MIME-Version: 1.0 Message-Id: <20250314-v5_user_cfi_series-v12-3-e51202b53138@rivosinc.com> References: <20250314-v5_user_cfi_series-v12-0-e51202b53138@rivosinc.com> In-Reply-To: <20250314-v5_user_cfi_series-v12-0-e51202b53138@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, Zong Li , Deepak Gupta X-Mailer: b4 0.14.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250314_143935_710467_17508028 X-CRM114-Status: GOOD ( 15.84 ) 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. Reviewed-by: Zong Li 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 Fri Mar 14 21:39: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: 14017525 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 BED8DC282EC for ; Fri, 14 Mar 2025 21:39: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=hRKwbmU5qBvoQWc+V/8xtLYTXQbaV7ZdQ8JJQmB+HDc=; b=mHoAGAzHjiutfC 2r8VZc4bbGVlypPjdgrhL6sSqoAV29bdqdpGeNt7azfE4Ll1F+3882BoL8TT86DOZTxfUsC5Qs5zt JBK1Z6g1GzpX1KGJror7Xr4WpJ2lyATtqL82HFlhVTgg5v6PpNgs+vPd8Eqycr/1A2pm/W8TuLrrB wYYFCJlcXSYlfY0c6sZN6ApZKfHYo0EXQLC+XOeJOKS7eAnwUEadrjClZft9CXVWF/2BdKXkMraP/ b9s1dRY6vFjcHmBjYC8j1mSFA03OLBjQHWnybMgJqDWEDMGR2XoX4Kd37OBnH+goEZDaZJuGHNFIn Tvcy+dlNYupTzInGtT5Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1ttCkm-0000000FKKh-1MgC; Fri, 14 Mar 2025 21:39:40 +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 1ttCkk-0000000FKI2-0qKn for linux-riscv@lists.infradead.org; Fri, 14 Mar 2025 21:39:39 +0000 Received: by mail-pl1-x62c.google.com with SMTP id d9443c01a7336-223594b3c6dso52482595ad.2 for ; Fri, 14 Mar 2025 14:39:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741988378; x=1742593178; 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=BFIG+6dd78ospSgFkUDFMq6V6JOv2iYpPDSeRIIiq+ZzxCalVSKeRiTuND6mJqJniJ coXyunS3N/IuxZPHfD0P86HdH6PgH9+QIKesjzGyW06G97uGGYyIY7yeDJQGcSU8N01T JWM0OVZxUWRypnTTeQA/ZeyTz3xFNLcSJ6K26WRMNSzIrGJjB549fBaIxom7tmREJ3qY 1akZB6Y78QMwsZRoma5EaeqFQ6OQwKNIChsJfkKbujgc5c6F+AkKJ2+9I0uiQW9vkaEW mNu8hqD72mgg2JISB/3UTkAXci5qJak+ogHseLbgkPSO223krwSc8dflAc0wvw4Cu09L u6cg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741988378; x=1742593178; 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=U4O1Gy5y/Awt2mfJt0A20sAJ1F0TuzO5ZC1fK6Wb4yoeMlxA6Xu5ijTWZgOA/snqbJ 8zANPtq45kOTzfk+xqwtO3BDiLdrbozxJt+dDlFrNoQdpUvB80wl7ubw6b8LuzCgOdtd TgKgKNb0KTQiHm6zy+kgGx5/W9UmwUWCxuTskZyV1cqskSXWTdgRl8jj/prE6PK/W+qM IzvYtXNtjvo09uwacKyvwXxXo6LnyWGoks5uEl3Cooo1bnumvOlQ/qeAbRo5paRnN0xX jaYdFnI0nTbeds3bgbYWDhmWses8eDz/eKjix8ZsH6xufltrzj1iwk8LLXCuUAWGhYLY T+qg== X-Forwarded-Encrypted: i=1; AJvYcCVUcvp+Kg3P54tYb+hUf7fgNSf7r1ufIglTW3UX3v+jQ1k5VP4bTUB5W2ERdydm1VKA2yvyZ+57ToEcvw==@lists.infradead.org X-Gm-Message-State: AOJu0YzcPBAKDaU7LlHOQMicFXJJAZxN/KU3tMvyVKhZUh8N+WcSxOQH AAnjzPuyQfKop43eu3SllasSO4iODuhbzlaP3T7OYL0aAITLU4mbJub4zwHfHxw= X-Gm-Gg: ASbGncs6CywNGbdUcvqeUTnMJDNsk4SCN+LbBu/TpZStrRTcxw7Li63a1TIbI5OBm5O WzN/V2fRZfeAzK5oRon23wALODzmSEkUoaP25q0Lrx1x3QaTnPsxCOzfyq+tGAp3Pvn4VS7p51M 76hOhkD2Txd2gZa2WNdWENsrcrOxBSnhk3pULNsKSDHiG/8HtBvSXK0dVLlkyu+LdmDQFA6Vsoy 07IhUEG8zQDFmlImMjFfepgBSp04foaG0WJXL151FC64ePBPFQURTwNg2bHW/KAqFe7lByvkyBC +YZNUZQf8mvQzzzulfknBVoOTnheyHtDNXIFTvs3SdUs1JkI9hj4g/mlK4cle1KusQ== X-Google-Smtp-Source: AGHT+IEd6y00tJpY5AwKZ0G453WB85qrfmfVkSTQW7JFb/Aht1BlD2RcJLOAn300bIU+BiCy9clyEg== X-Received: by 2002:a17:902:cec5:b0:224:abb:92c with SMTP id d9443c01a7336-225e0b19692mr43817135ad.50.1741988377710; Fri, 14 Mar 2025 14:39:37 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-225c68a6e09sm33368855ad.55.2025.03.14.14.39.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Mar 2025 14:39:37 -0700 (PDT) From: Deepak Gupta Date: Fri, 14 Mar 2025 14:39:23 -0700 Subject: [PATCH v12 04/28] riscv: zicfiss / zicfilp extension csr and bit definitions MIME-Version: 1.0 Message-Id: <20250314-v5_user_cfi_series-v12-4-e51202b53138@rivosinc.com> References: <20250314-v5_user_cfi_series-v12-0-e51202b53138@rivosinc.com> In-Reply-To: <20250314-v5_user_cfi_series-v12-0-e51202b53138@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-20250314_143938_240541_CD5865EB 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 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 Fri Mar 14 21:39: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: 14017527 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 7E704C35FF7 for ; Fri, 14 Mar 2025 21:39:48 +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=+2btVyOVg+lM7n5u/oMJdGFPVyT6LgpPgPMcdDpdyLo=; b=HrdE8OqE20tLT4 UFAMjHVmFfcVP7Xs8OVG1ZylrJ421KS0upHWLLWtRE2rzEo3CQK2/FgYTddoEuFARim6GLIamJIYE XyXEv5YDcmIaxLaKc+r5bng6ThFpSB0m2zJ9VTtwMcW96JMgCe3ZA9cMzA7BeG3y2GivG5Ce4oh0h kUDC5DBZYUOOj425j/x4uiBxAfHNMXFO9EvXbgT0u5UUURmRhBR3lPx+PTV3AZpxdX3Q9kMXnkP5j PdnYv2F31/PlRkt8C74uk5clN3Xkcc3PDkNXTyXu/9XtRWHFu/jE2TYSyZLPN9DaQbzNxVEddvkSy IvF6oXgHzMVQV2Hx8k0A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1ttCkq-0000000FKOs-06KR; Fri, 14 Mar 2025 21:39:44 +0000 Received: from mail-pj1-x102c.google.com ([2607:f8b0:4864:20::102c]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1ttCkn-0000000FKLW-0quI for linux-riscv@lists.infradead.org; Fri, 14 Mar 2025 21:39:42 +0000 Received: by mail-pj1-x102c.google.com with SMTP id 98e67ed59e1d1-2ff797f8f1bso251352a91.3 for ; Fri, 14 Mar 2025 14:39:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741988380; x=1742593180; 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=EvIiEqJjsqgbsYMUY6NKV1JWZOMquSSP9DBah5grL80=; b=nmyuwvsy21GuZxDABUFNcEFEjFclgXGC5TlZdeZmfwv7yWZnMag6ymWgqWkCpxaKw1 koxRXS7Sv6iZ3v71bpmJtjmJzMJgC/lVbLZZ8NU2GQli3qnqw2CNx39pPJ9omzBkja6t S7rvwV6x4uES7mkM5kaFLx1WfvBARaqlvma3CWQnvjTcIM3dyDpn4YeyMakxY9b3yfTq 5nKrvVqrLOJ8lpmAwkmHeY/iRRf3+F+tKZodUxvkquM3HsTaeGSgX0xUGfyos7IEoUt8 FBijqb/2zzCevfMfBH6TSvvyUDAo7NG2fORtbrRSyyr/raZisH9AW3n+t36MZrwKjRSb f0rA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741988380; x=1742593180; 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=EvIiEqJjsqgbsYMUY6NKV1JWZOMquSSP9DBah5grL80=; b=cCp8IN5aqiv8TWC5hb/8KXcJbZ16Ok3fHW6EZxYg/qqYLSsm88tP28sSXAldQJmSPW orjeFSHR2nzQnsitUvl3kgGt3ojUmLLd+XSfLmyOW60p76Z1jGDipeWLrgoj6QIjfBjH T1T83/3iazGHYnMr1Ur84dCRgnMoSWB1U1mtH6yPtYo9FU8//HDPK6EYALnBEWHjrXoB plh/vna+Mjql67D0GuOWVURqpIQEljWUaeixG/4tNHIcRwe1TPU3CUEw5VzwzPoGn2V+ KdyxX863+VyIQqLEyqaz3C2/ML6bUTlCNCqZJeY9Mbnf02oi6EOS5Sy25IPv2ECl/959 WdZQ== X-Forwarded-Encrypted: i=1; AJvYcCVYAXD9MbfeO6W+wPP5c9DA+LOJaYN9ul6LOvXAaJgFkaBB1cm105Irlskjvwb389GuQkshWnmLx6QkZQ==@lists.infradead.org X-Gm-Message-State: AOJu0Yw5hehEDifklGwBijIDTQFd80UGTV59+0tX1xgLNBKkdtJtZDg2 i1huiWnDZbzEin3a7Yj+uEtlTOLdWd4FgXDGhGM2yZu5pZoRAGZVxTTJs/ZlAso= X-Gm-Gg: ASbGncsSIlok0miBxG7dg2kfQoiKRgwZ6ZE32NwOsECdwrT9fAjzgHMqmTFuR9JEMwf uCXJbxCPn702L04uwEV0ObIrZ8Ia0tRgk84i7GMblbMODAP4V05Fbug9sEfnww3Qs4eteFwkIoN mZWPfVZnpBKSAry9XzJIwdSqrxwp971/ugQ0kXdd9ZM/HflkUfpw2Dr243QSJ8f0KJPgZmSGJww NjRFNQghpInG2JiqzgWj/hPFDczvabb7dl4UzmUZXNToqOcoPivROtqpWGqp9VgPi8SnKUjEcL9 IdtEGl69V3xOR+hRxtUIs5PMi5rEvJy0jgh3QmJOTqEcIqzXcVX5nQU= X-Google-Smtp-Source: AGHT+IF8ug4c9Y+jOxof384y9deWCg2zD3mmcGlgW83PZeg3Mt1WDeZf/8sTLDXIp12wuvsfq/2KLQ== X-Received: by 2002:a17:90b:51c7:b0:2f4:49d8:e6f6 with SMTP id 98e67ed59e1d1-30151cab271mr4923714a91.3.1741988380285; Fri, 14 Mar 2025 14:39:40 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-225c68a6e09sm33368855ad.55.2025.03.14.14.39.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Mar 2025 14:39:39 -0700 (PDT) From: Deepak Gupta Date: Fri, 14 Mar 2025 14:39:24 -0700 Subject: [PATCH v12 05/28] riscv: usercfi state for task and save/restore of CSR_SSP on trap entry/exit MIME-Version: 1.0 Message-Id: <20250314-v5_user_cfi_series-v12-5-e51202b53138@rivosinc.com> References: <20250314-v5_user_cfi_series-v12-0-e51202b53138@rivosinc.com> In-Reply-To: <20250314-v5_user_cfi_series-v12-0-e51202b53138@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, Zong Li , Deepak Gupta X-Mailer: b4 0.14.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250314_143941_252792_9E34D7CD X-CRM114-Status: GOOD ( 21.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 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`. Reviewed-by: Charlie Jenkins Reviewed-by: Zong Li Signed-off-by: Deepak Gupta --- 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 Fri Mar 14 21:39: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: 14017528 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 E525DC28B2F for ; Fri, 14 Mar 2025 21:39: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=WcqZsyHa5iZ2ZW6WMiFUBOGzTQsmkqNgxcgl2yBiZFg=; b=pqMtZSMc74u4zS 7JI9EKWoYMYPG57hat46tkSX632ysZe6YUBkmGnqCZ7YAc77+yV5pAiYH35vIYjrvm8W5NFa6DaJ7 fClF3eCeNAitweWLadveKrfuJCYAPuyCnKNlg6B7PMwegMHg2d40av3E5y1Lcebf1YnqKr+WtBoL6 rCTxOGlZT447vx9gg7E5V4lfyjceKauDxBM2uhE77IYRQNunHoiX9djCe8J5xKYeA8xgQrgi2Xhwv ZvuHxou+lUR4tH2uCfbrT48ZDMgRhIdLiYEsFeIMzzoiUfWQ9e/HdsbDBY9ftpCvNUDXjD3V6Cvy0 nukFyP2/FoQ61Hl3YcjQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1ttCkr-0000000FKQL-30af; Fri, 14 Mar 2025 21:39:45 +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 1ttCkp-0000000FKNx-1g4O for linux-riscv@lists.infradead.org; Fri, 14 Mar 2025 21:39:44 +0000 Received: by mail-pl1-x636.google.com with SMTP id d9443c01a7336-224171d6826so63843005ad.3 for ; Fri, 14 Mar 2025 14:39:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741988383; x=1742593183; 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=gQxyFEGdxJnYf6tF0dDNzkrR4Tdl8J2Aoq3Zv3gQ3iw=; b=Tn0aGr26oKQy5abDxKsb4qYz3o8hfTFb/B5QBjHz+1acbo/2AJA5r/orDgWxxu20pr t5siPwA6+DYHqa1F6L1UlGCZW76q7+5quxgq9oTiRKuM2e3mV5jaiCmebwEuXqEfIGJi cMpiVeEKx1FamJ9Clz47i9YhBWL0sPTLJWPqSPRNG2cQ68v4Nb0yPXrbSTOsXLa3hhNO oCPHlCJNaih0Z5E/0tucxwRAbbXXxI62udLovSHC//6pZSBDeOuNZDbL9O1F95VfeySs GbemYRu4GJ/lb6Dj9Z3BCdKEX1bAZWTpxaeKV660VpGG4fjXygHlJ1mCDDJoczgjcKAJ XDLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741988383; x=1742593183; 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=gQxyFEGdxJnYf6tF0dDNzkrR4Tdl8J2Aoq3Zv3gQ3iw=; b=jNSTN6k+fy+uzkXrC1NgG/lOXgLS05pMGT+s1qUF9dQb/wDhp/x55nIqI6VTt1xCCW KUaUZXttE3pTCj8GkJlPXqTaWF23sgakGhg0Dc7xCfn3+NGkVJpNJN1VNc1h80se9ZJB JrGd+Dp6TQJq39mm7s7gUOG29D1k8L76XRlTyJfo51pX6IEo2uN6bP8frRS97uTLKmaH l2kkCIBs+B2yJWJdh0RVr3fOXo0Y02/ou8JYfwgdiFu+wTfr/mNfVYqUKpoFvocopSJS 46vBuAd8gSEhgGh/MVHtPR3sQw8HVyrtKrh+vpxfejEs0YcbnPY7I+dj03k6P/yZQ/dE czwQ== X-Forwarded-Encrypted: i=1; AJvYcCXSkXI5NlTmhhzM2Zmu7PtMPw62E0mYZB6vkNjdCcOj6RFovhLVKyLCzOb+9DCQdEmf/a0kxdpAEkkNrQ==@lists.infradead.org X-Gm-Message-State: AOJu0Yw960fjKaIqcyjpxYTMdBwy+WMGV71bLc/kIWntr/d5qOU1uSK8 Z/X7G2gAH/oXUIp6/lb8ZQrbOjsnwZQzsNd88vmSlBFHFEx7GD4WN6HyU7zF5pM= X-Gm-Gg: ASbGncsuGjY1QM1aHnLnc+bJIGHhHkbhMW4F0XmZ2z1Ur+pcjKWLAGQcv2ABbsC0wbl qao2yBVgqQe2KTGgHElCYgcsMk5wnPb4PP4+60mheNR72kukFzQwAsqzbKIIQht17/0rzPKGcm3 e+KkZe9nUwCM6EwMot3RrSDV+ogpru3TzVd9vUJFHsTj6Rb0gGDGJrZfMyU41W+t9klYLDUUWCC LrU0GM4E7YS2STGQAfaRkVTCu3KQIwEO+ptauVTQDpTqOe/KEUtgLnfGj+3WrEC9Bs405XJoqCc iWAY9uCNqVnwDZvOdzu/yAZSRerWdS5whhJgPL8Wpd56+scKOzOJ5mQ= X-Google-Smtp-Source: AGHT+IG1XKNpuQB9eoNKiKGV8OaMyZRapBWvcssowC/o6htzWDndePZH0CVmNsc5LDFT4JSdbgFB4Q== X-Received: by 2002:a17:902:f693:b0:21d:3bd7:afdd with SMTP id d9443c01a7336-225e0868050mr56743905ad.0.1741988382861; Fri, 14 Mar 2025 14:39:42 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-225c68a6e09sm33368855ad.55.2025.03.14.14.39.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Mar 2025 14:39:42 -0700 (PDT) From: Deepak Gupta Date: Fri, 14 Mar 2025 14:39:25 -0700 Subject: [PATCH v12 06/28] riscv/mm : ensure PROT_WRITE leads to VM_READ | VM_WRITE MIME-Version: 1.0 Message-Id: <20250314-v5_user_cfi_series-v12-6-e51202b53138@rivosinc.com> References: <20250314-v5_user_cfi_series-v12-0-e51202b53138@rivosinc.com> In-Reply-To: <20250314-v5_user_cfi_series-v12-0-e51202b53138@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, Zong Li , Deepak Gupta X-Mailer: b4 0.14.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250314_143943_451054_2AF10365 X-CRM114-Status: GOOD ( 20.25 ) 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. Reviewed-by: Zong Li 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 Fri Mar 14 21:39: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: 14017529 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 801E9C28B2F for ; Fri, 14 Mar 2025 21:39:55 +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=8C5jrW7ytHSa8cSJV01cxe24UoCCmkfBBL1E0WgPoEQ=; b=UC3YM7dasau9a4 blI9UpD7qIXjWZjDPCXKyuZAp0/QUs/vf/3MfA1pX1kI1WoBLZyB9zUjC8kDe8keoFbjUPaB2jHXc mwFVgERtuyhInQgFclPbwRh3ajaQp7ycJNeRW1D0tZnGThuI3yWkCmIA+exJr4cvEZH8ThoVkmKGv cA628ONUlsmir86cmouUkZ6G+njzp/QTzZAKnwpg9Kqw7S4kr7Y+cveudLmlNMGv57FGeYQWlrIQS Xx7aR4KsFxC1f4p/HqFQ1nhzyA3DFHVri9li+EFqzZUE11bGvR4KTPqnHRUwinGyadsbjrCTHt33s nS7fFoGNuPCL6KthNzRQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1ttCkv-0000000FKWN-27Wl; Fri, 14 Mar 2025 21:39:49 +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 1ttCks-0000000FKQg-02S5 for linux-riscv@lists.infradead.org; Fri, 14 Mar 2025 21:39:47 +0000 Received: by mail-pl1-x62c.google.com with SMTP id d9443c01a7336-2254e0b4b79so65807385ad.2 for ; Fri, 14 Mar 2025 14:39:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741988385; x=1742593185; 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=J0cdClzbcf4jwWk6NNoAER7Uol3lcFHrvU6ecxcolCg=; b=VoAvEEXgD8JhYzOIiGZws2SZ428MPdVIoHByqs2zsg9gMoBgByvaY7fFWs2UB+q2pt 8Rt8at1b6JsbfZZNifEHgAdUBa6QXDixKc6pC+285itI2zadALEHHobmpQWj6Hu2wX77 7m/+4BQKWKiu1HKYpW7GVbAigLv3ngsgwn2MKU34G4saQDE3tkB225TDLX7gLB8jWkkR TbeCdP707oVGmgBhV7zNetMaN+1ZO4Sd6HQbSUs4XqjeiFS33TMbMhQewFNo3pj1+R20 EGgnXUVOd89CQtgd7L2QVaXIdAhtkEiMSWczAgUGmD344oH86EG98BFazVhn/3Wz9Sm4 oAgQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741988385; x=1742593185; 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=J0cdClzbcf4jwWk6NNoAER7Uol3lcFHrvU6ecxcolCg=; b=wGSZmD8a4S9sR7oWSozh2xiN9dJ+f4s0il7JwnYGQLrODMu6vBPWHcEBzGWpx1HYKh NJTKSX0TGxZPoiuT9ZRG9iUG4sun5DPhdEMzRCtdzY8QnM2T7fny7I3aVb2dzyio4DD1 Pm3DRmR/DZVAbdWg22CNc/wXSQ6BSxN5NCxq/4LJE4oPHib4kzpXVMsYTMvYtkzbK9gJ LGclacSZ12dj2IIX+LXN0BtVI+zXi660p8Q3Ni+rqYbK59NWeavxs3d1ti5vlEyh/7Am J75RlZ2wVC8xaIPiNb8l/pULky/TS0bVPZOHdW8PVWZ3sFBA09z+qwGGY9U4hf22tZNw xhdA== X-Forwarded-Encrypted: i=1; AJvYcCVCCnLw97M6B4+7WmW+7SQ0WK9Dzg0uaJb0EGLP4pX+7Wmxqb+3IdnFAO/7vKhgnHmwTLPah8s6EjhHSA==@lists.infradead.org X-Gm-Message-State: AOJu0YxSLUy9as9ZnBPSICJfcU31s51nZV64aI47asYdFalACqEpY5B7 96cKEXsRPBYUG4H0iB6oqlnUZgT6X9SByqOvxbbjyk9VVG2xI52izVLix2Q9Xzw= X-Gm-Gg: ASbGnctA3u5xakXVvJomvwXGodBvSK6wzZQpGsIFUps3OWRwDSTMbUH+vCOfeA+Tgku +4qr+Zvgl3mQj11MQqtoVoB9zlvfM0QKHyVWfrcC0g7md0RPqdU03TDy1An3oaANw4aPYn5EvEo L8bVY+n7WP7WU5I1y7Kzm3Xsb5L61znceUDKBbm9tTAVr0QANmr+X2ExfeivmfWHwEpGNSLalcd KnIELMBDR9djQ8FXvm382O7n5OwxvM+03xVV5Cg/lErW+C3h1HvIvexCs0FacqIG/YNi6DXvbrb jCHoWmRwr+Kit3M38QlG6xY5Y6gsulLR1SzrPXuQIDV+ANSE8YDKoqLOIbwD7CJqJw== X-Google-Smtp-Source: AGHT+IFZaFTcFkxKWE6fnM6U3z3NBo+SecOR6nfScdkNYaVzLCXCW0qb6at3qhL4+W3vIc9ENh5Ndg== X-Received: by 2002:a17:902:dad1:b0:21b:b3c9:38ff with SMTP id d9443c01a7336-225e0aee9ffmr52390865ad.37.1741988385430; Fri, 14 Mar 2025 14:39:45 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-225c68a6e09sm33368855ad.55.2025.03.14.14.39.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Mar 2025 14:39:45 -0700 (PDT) From: Deepak Gupta Date: Fri, 14 Mar 2025 14:39:26 -0700 Subject: [PATCH v12 07/28] riscv mm: manufacture shadow stack pte MIME-Version: 1.0 Message-Id: <20250314-v5_user_cfi_series-v12-7-e51202b53138@rivosinc.com> References: <20250314-v5_user_cfi_series-v12-0-e51202b53138@rivosinc.com> In-Reply-To: <20250314-v5_user_cfi_series-v12-0-e51202b53138@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, Zong Li , Deepak Gupta X-Mailer: b4 0.14.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250314_143946_060255_B1222716 X-CRM114-Status: UNSURE ( 9.96 ) 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 This patch implements creating shadow stack pte (on riscv). Creating shadow stack PTE on riscv means that clearing RWX and then setting W=1. Reviewed-by: Alexandre Ghiti Reviewed-by: Zong Li Signed-off-by: Deepak Gupta --- 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 Fri Mar 14 21:39: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: 14017530 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 1D2DAC35FF6 for ; Fri, 14 Mar 2025 21:39:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=qF8Ez9J+N2t5x9xaUU8QM/in0L541Dn4ajy99vIxdkM=; b=W03QSH4SKnvKxc HuAwhgaDvHvXIpe8Z2rLAY+LQvNDAo8xizHajgJkWZlCp7SP4Q9+m09e0WaCS1t2ep9v7fGfbo6pz b/KMbdZOW8g0+49aW2OyGWCtxKE3+hKbHiiJoMVhZ/4jrJq9acCIDw1mN9xfSRWfVlUH/BgDT3wK+ wJCc5buap0KfdLjeNni/xuMCJdsvrnZWA+yHLCiGfek4EkVGl7mNUc2mj36LitK6tMNAvp/1qcEbV WUoYIPJjBNjA78wsrRWm/ztgqVL341fmUBHuJKj+7otSZtPtC/8aDxLSF/CoQKKpZgtruz+KIsq14 M5FecFmE0Y6D01W5208Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1ttCkx-0000000FKZp-2l2A; Fri, 14 Mar 2025 21:39:51 +0000 Received: from mail-pl1-x633.google.com ([2607:f8b0:4864:20::633]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1ttCku-0000000FKUh-2ZOa for linux-riscv@lists.infradead.org; Fri, 14 Mar 2025 21:39:49 +0000 Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-22423adf751so41790895ad.2 for ; Fri, 14 Mar 2025 14:39:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741988388; x=1742593188; 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=kAIAgpv3AXe4grYgMLwnUee/eXYDz+xNUbGxtfEmLwk=; b=I0K3VStabjr8M5CTYm8bXx73Rs74fUoLx4npt9i0XXNu9C4sFjr15f+0Hdw0DiUeer XkhfWb8f2yZxJ00Kj66I7mkVp4zjhYFcJDjbKoB06W30nYl69cwn7u2loIsrGPuynCZS WcTDdLAG0DNKk8KZSEuMbmWLYI2jA4RVGSpx/yWSexediBhSU2Sv/shqmd0Q4Wo75KjS kx6160PIstAhSb12JWDIU+zNjkBjaTcDe0GCWT7pz3DquuvZRlt3gPo8A+MQaUvHVl6Z w+L+2T3oIVhI0Edl8TpxOhz0+CdPu/rsp2XWTueq18rQiVoUfYWzilgmQ+pPChJIOtLo RZow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741988388; x=1742593188; 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=kAIAgpv3AXe4grYgMLwnUee/eXYDz+xNUbGxtfEmLwk=; b=d83xe/ju5oDczYd2+I9Jr8sDp/Bxozf5s2uVD3roW2y/K8TNJ7qbDi2I/pjXr6qSbc JFcmMczaqub2nmfg8NGQhV5tDba+2g/vS+uf3RiHsK5gU3E8vsX+6GQgsEKGgDDj1ZPk Q47hL23C8jxRer1q/3qybInW4QRTkSQ+oiBdMauLe2qFNjB+ZwX5KCXP6qWQ5/J4fqYG TWuW78bNBIzBuT5XISDH+JZqC5jTb+iHht41mXcYMuBGXVFWX2huv4VjFZbYS5hmpIjO XDelvUVMIqgmVcY4Zy9bLb3nqmGEvyhDT2sPpn+Si7r/O5v6VAMHrgzWNdzxKbPcLwII Dcyw== X-Forwarded-Encrypted: i=1; AJvYcCV8aHWyqbZJgtpfxFMvW4sGM/FrEiNRICyula2Bn4q2GJebovioYVERtTQ9ayfr0HDCyrVpMXzEWzW4fw==@lists.infradead.org X-Gm-Message-State: AOJu0Ywsgx4EMkXQNUq8wk1UzdXwqI5dHhIY0bITh2+1iydHX/4qp1km qh8gsqo9hVf8fcg93YY5yXduLwPwLfYofrabk/vJy2UJhhDzSADSLoL0mk6dugE= X-Gm-Gg: ASbGncvoO7rCwRDK4A8QOE9PXyRDOnqJQr/snrnk7yYnc/Ej+t9adOuc+vxVd8xQepq DKCpKKm2kE0fCJf54GQL4b9i8fKCtVTY7WkmS5mBzUIe1sng3v9WyPIp9405LRpITSjY2yK1rs2 fozpN/mrAD2vqIAcGoBuIP0lMsWbFVpKySfTsVItJwN5agfQE9bXRc/FhTRctNhAvM81GrB1T+C WUl7yeB1pjyokDAYtEO3mbyhcbVqgRM4HXFu7eO+4+mTxhG7zrz5sf9gk9ioejgSyjZ9MnoONqv YdPVKBB/Hkl6/26vKN8/UXZN4zUhMiExwZG+ZtPOYZh/+8ioapaoTpo= X-Google-Smtp-Source: AGHT+IHx7WQpWru2fKimtMpSmVFwsTrooQDFYBOULcY3q47kcw+TEVwpgQle1i3yqVDeVf/+DpJMig== X-Received: by 2002:a17:902:d2ca:b0:224:fa0:36d2 with SMTP id d9443c01a7336-225e0a82dcamr59199255ad.26.1741988388068; Fri, 14 Mar 2025 14:39:48 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-225c68a6e09sm33368855ad.55.2025.03.14.14.39.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Mar 2025 14:39:47 -0700 (PDT) From: Deepak Gupta Date: Fri, 14 Mar 2025 14:39:27 -0700 Subject: [PATCH v12 08/28] riscv mmu: teach pte_mkwrite to manufacture shadow stack PTEs MIME-Version: 1.0 Message-Id: <20250314-v5_user_cfi_series-v12-8-e51202b53138@rivosinc.com> References: <20250314-v5_user_cfi_series-v12-0-e51202b53138@rivosinc.com> In-Reply-To: <20250314-v5_user_cfi_series-v12-0-e51202b53138@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, Zong Li , Deepak Gupta X-Mailer: b4 0.14.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250314_143948_665205_1C295F10 X-CRM114-Status: GOOD ( 10.87 ) 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) Reviewed-by: Alexandre Ghiti Reviewed-by: Zong Li Signed-off-by: Deepak Gupta --- 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 Fri Mar 14 21:39: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: 14017531 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 B3552C35FF7 for ; Fri, 14 Mar 2025 21:39:57 +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=dUXZMf4BtZbjr9FT5TS1DOfkjhkP94+WA27GwSQLEx8=; b=doG91/yIR50Fn5 SwH9Z4kX5jhcHA1whGS3+9ugnwisroJCJ6TzhULxhZKoRwKXWoUjvKs/7x5/qPRK+HHbFhgW95yPz AirUNIVOXc6w8dgTaEZp1JzZuefFT8mz2+7rVml9tJZMQRp8omy/h41TdOlrBzlRdIbSWN33ZPIRx 0qd6uqA/RwHuehFi45eqh+APAP1xjtmfglfuyyWQyfUTfXIaXRbWam2qV+n0ELabUsS48l3WWt8dH FRZMi3k9QqyXGjR3kfB09Ku/pDnUFDu0WVI5ecOILTmLx9FZxXTrg3TJZko+S1U8s2nWQGGXvMQQO Y7r39BPtMXRoU35RNHdA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1ttCkz-0000000FKcm-2PT4; Fri, 14 Mar 2025 21:39:53 +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 1ttCkx-0000000FKYM-0XAT for linux-riscv@lists.infradead.org; Fri, 14 Mar 2025 21:39:52 +0000 Received: by mail-pl1-x62c.google.com with SMTP id d9443c01a7336-2254e0b4b79so65808375ad.2 for ; Fri, 14 Mar 2025 14:39:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741988390; x=1742593190; 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=0XcHZdxwrXOxfjvym0f4ldwiX/cDoifADRl7Uwkj6t8=; b=mDvZ3PRSL4JDrra8Hu8oubmTBAjh+3iu+32sSbtVm0EhOQ6pBjtiRIfoixBBkobDoV fpd1EW8wEZ9vTfUfC/yn5rmNBWGToavLMhngJoVi++puF0XfHAnsfZ/G7akR7ggMBvI5 70jC6D/dlPKgj7JBlzi02/JrVpS1RsNlXs3IWSv6KS4V0ermqcoWHTZGVYb27wFP219z yduZGr8k/FO8vZRdeu0qPOGb+0fsnGPZ1X8eWMje6blnLISQOCVqkn89fREEJyE5Qlzy pUTanrJYBwImTv5AVq0T6IbhEkSHJdlkQhdxJgmpILW7RbEr7w/hPOc89a//xfXg2iJA UJog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741988390; x=1742593190; 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=0XcHZdxwrXOxfjvym0f4ldwiX/cDoifADRl7Uwkj6t8=; b=a5AlODRsOgDcQEQa0+r99oTengp2n2a00D7hQ6toNPtTuaV1eH4Qn5Jrz4o8WcZeuN pv7q8/ppz8315Gd/FUxelcrETgQ+mjJz2jk20Dv0NiQ/p07TkisERHY0tiR0hUsC0uFo o/ICI0sEeaooWsBnz3RceqjQAW9iTItf+d/KczPfzW4kxc3KAfduPW02pTnefTrKegYs Lxw+qnbKJfPDVImzLKNZjYgfrcvsexfAwDYiOCSq74Bt/cpQRLqzK1DzzP4A9xyzHDNi z8XENQs0rIdBzsb5cT7MN9w1RMylLNhN/udD1sNkKRQFUnF9aqO6CfSHAv1yH1W9N5Sm ImTA== X-Forwarded-Encrypted: i=1; AJvYcCUySPB8vQ7RqETfeS7/znmtUOBmTiAKpFSsha+RJQMnG2e5rV4NUgYWY/OfbZhvLp3+jmok1dR6s3mmMA==@lists.infradead.org X-Gm-Message-State: AOJu0YxSpa3HAjHvL3BW2GSfG0KBsi9gtaItUVlrrFrHWVpY0b2MDFxk Urmq3qtPXlPbbQmeF7jdQfTjPeIk/YmNp3PDyM1CcCzIBMmzzhF4TsmFjgV7rQA= X-Gm-Gg: ASbGncs53IsVwCUcDyPnD3E520TygIKXrUwK0/3e8Tx7716VKMtZUGsULHzun3+X3HE cOITK9QlsCdoLtiS2+kPxNJ5D2BWeC/Gkyc5NmfKoQGvUQil2ch0TbdhCvxXMqmBXIh8gsav+Qe zkR9l1Tm2+U9/HWKHY4lifvsUOUijmoX3JSBjUZyF0lVaAyW2A2dv9dKhAJ55SULMwi5LX9hhTA Gat6q/yb4E7s3vP6lC1GXFKezlAw8WWOA4k9Q2tVfj8qvPym3NZreYHn/gvQhE//qMebyffdkBh pexcM3tcdmSFOaRAePLmQ7qIFl+31tBoJEpKc25yTEES3geTv2XUsaM= X-Google-Smtp-Source: AGHT+IFpAfWeI9aPllc1CwqYl/66ArnIXE3uKAZ+LvLNawWnKsDtpPp8xhAGnGFewoMM9kaYyptgHA== X-Received: by 2002:a17:903:2b0f:b0:220:e655:d77 with SMTP id d9443c01a7336-225e0aee8bemr51023205ad.36.1741988390643; Fri, 14 Mar 2025 14:39:50 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-225c68a6e09sm33368855ad.55.2025.03.14.14.39.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Mar 2025 14:39:50 -0700 (PDT) From: Deepak Gupta Date: Fri, 14 Mar 2025 14:39:28 -0700 Subject: [PATCH v12 09/28] riscv mmu: write protect and shadow stack MIME-Version: 1.0 Message-Id: <20250314-v5_user_cfi_series-v12-9-e51202b53138@rivosinc.com> References: <20250314-v5_user_cfi_series-v12-0-e51202b53138@rivosinc.com> In-Reply-To: <20250314-v5_user_cfi_series-v12-0-e51202b53138@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, Zong Li , Deepak Gupta X-Mailer: b4 0.14.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250314_143951_183063_59D55829 X-CRM114-Status: GOOD ( 14.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 `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. Reviewed-by: Alexandre Ghiti Reviewed-by: Zong Li Signed-off-by: Deepak Gupta --- 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 Fri Mar 14 21:39: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: 14017532 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 B2986C28B2F for ; Fri, 14 Mar 2025 21:40:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=H9r+YYVIli4/2rilwD1/jxSPWc7nEx3PKEQ4GYO2NDY=; b=x8iHTSFSQZ9CH3 Hz5EdQ6QyHzEGl7RBYTGseqdcJSQcHuuj/xj0UyeSUVcyasWFsUfdFXBZYiqWNfrF82JM9RolP8yL y31AcWBeNVeHNkKmBSAOfM9KL9EuudOyNNIp0gR78xw1Eox71RdoI9XSwc3TBTvvT0EsjjL/bK7WN brn9Z+2HqPuMozVsJxv34kuucPma1T0ySoP4K2IplHI02qnoItCGTY0/utEH/W8fgH+Bvo0iXRE3+ 5T6/KmHKzNUHwWsUsevmVbtqvKHvoCZ3WFsHQccSBUgvpFgdMbQ8FrwVOAeYBPuUqqe2vtx3AgF61 jNIrPKs2orXC3ozYJFSA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1ttCl2-0000000FKh7-2NTv; Fri, 14 Mar 2025 21:39:56 +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 1ttCl0-0000000FKd6-0cLi for linux-riscv@lists.infradead.org; Fri, 14 Mar 2025 21:39:55 +0000 Received: by mail-pl1-x631.google.com with SMTP id d9443c01a7336-2255003f4c6so46174475ad.0 for ; Fri, 14 Mar 2025 14:39:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741988393; x=1742593193; 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=ZL2v/gS+0+DF34n+ZrnHt0WiY6OgvVWONOJtZHYCX1s=; b=BgyI6cLm8v+QyNHXD9lyOlaF+dCwNeWGe5wuP2GZ57TptXXKI3hG5mGmaky2uqwisD Y6q+5f+N9SJ910HvO8msDSAn0DzZ73XwyZQEGJ5yTgI3adNDvk1uw/vNByOALtgZNuxC rV3MkeSaSWsGv/7+usVQoFcgqOvzQ7sFPwl9qFjwryTrNL6L/qZxE9W6S+s/RC0mcIP0 iuBJSigzpXiTwKm3/D96f8RwqIBwsafjKfGgKZFbmnCImCm/j/ILutqIEMmYHfDXQ6pY bKRR9Je8qwU7iPAc8bP67HrhzgUrVV35i4PnLuhTV99ClAJla7p5xbpzCrsMeCjr5L98 3+sQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741988393; x=1742593193; 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=ZL2v/gS+0+DF34n+ZrnHt0WiY6OgvVWONOJtZHYCX1s=; b=tIMOT0Ihx5H02b8bICDJJ97VlNM6mUlk0Ue4RlcHL/8Yzare+l393ADKMZlZkKwMUS EHL8LTkwzv8MY6StTyXcjxapaBpQoJx/hCo0mFwUXnG3AsvYpNLk6nY/OwBdgvs5wFtv kc64shR4lEm2tmSo4b1Ex8as0NNezB4p/VIcnFwAISMSVL/tgqfDLAYKUnjCKn9JEo2J 4hQqjhrX/9SXX+VxqGGK1Eol9nl59O4VU5caOVVpywmgQW8Gpf2L2eTgt0jvH6XB4CSz wrRvcOGdbhbox3uMjdCgvh5uqk2x5e+yZZL/qZSaEz8NCDwVdEuHdkUKYGk9zxmbL30W b1IQ== X-Forwarded-Encrypted: i=1; AJvYcCUWgOMtV1DOfBcpNCBYOJatVYqHO+m7b+IoUuN5gbo1pT1Nb1vHCX+SlVFMWNuc3acyVhDugmjkCd4lOA==@lists.infradead.org X-Gm-Message-State: AOJu0Yz7mmQAvW70kxdeUjFScxAf9W+LAcXCWsa4DEQbWkNNLTNIYzhr WDbHdD4LF3/Kp283rQFDlu781CLX+5pJcIg1DMKhG/UJKYOD/JK0ueoUBQXSCRk= X-Gm-Gg: ASbGnctX7uiEfbaLZJ/2FX1OOdYNjd7r6ouvZk55yiUcT0tIbgLWCUF+lv5Drr+rZJU fBrh6absgs0HcZ0O32xGF0riTIvi4hSQG8oDl0bHK61NANotEUuPMpx9kjfVUNMy1xBUNcgqOeO e1IQE6Kb4SxlzCC9v2pPh8yLJznP+sLw+EElrIX9pMSGP8f3why294h82MTY/RQB57e+v5MPFTW or+XvM71v9aYlSxBO4x9NtnctE6rhgAkVFnBQTN8IJNUqcFTymVzXaxg6IUI4SgfylInErtq3j+ MJTQzYwUGNd/XlJrxkiMJpzFNdeANXUzC8ZX9dbmWBp+jsqUfgkZEEQ= X-Google-Smtp-Source: AGHT+IExiBLi5afxFVIy1qmyPdBKTHq7Fhk/9edJe1Uyl1tqXfGvVFk/2MK6lH2iHYAUQvjSyaVdrQ== X-Received: by 2002:a17:902:d4cb:b0:223:402b:cce2 with SMTP id d9443c01a7336-225e0aeefcdmr42109185ad.33.1741988393228; Fri, 14 Mar 2025 14:39:53 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-225c68a6e09sm33368855ad.55.2025.03.14.14.39.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Mar 2025 14:39:52 -0700 (PDT) From: Deepak Gupta Date: Fri, 14 Mar 2025 14:39:29 -0700 Subject: [PATCH v12 10/28] riscv/mm: Implement map_shadow_stack() syscall MIME-Version: 1.0 Message-Id: <20250314-v5_user_cfi_series-v12-10-e51202b53138@rivosinc.com> References: <20250314-v5_user_cfi_series-v12-0-e51202b53138@rivosinc.com> In-Reply-To: <20250314-v5_user_cfi_series-v12-0-e51202b53138@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, Zong Li , Deepak Gupta X-Mailer: b4 0.14.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250314_143954_196635_DC790437 X-CRM114-Status: GOOD ( 23.60 ) 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. Reviewed-by: Zong Li 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 Fri Mar 14 21:39: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: 14017533 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 71DA1C282EC for ; Fri, 14 Mar 2025 21:40:05 +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=rnk9eBzpAtMl7OROmHxWGKa/HxDg3TJFjPeTCJJR4ZY=; b=aQzXSoC81pT6eS Yzl8VId6236uzk8o9LTOOEBMIwK73wJ+dDAvVw+Xa3hYoMwvfYNhIbZ9kEKrm6+8EkggL3pYsDSQZ ttoMTwkR4y9eTzH/3CrchxTvEaixNB58i6TocbtPURTQrysCi9LCVHMRQ9M4pIiaz+XYOL6JXC3qK mjJbm/s79/tIHeRK/NQdWBlUYEvx90WHttlY5oNuLY89QhCLbIVNzZbQck26GHM8smk1lURRbQFJJ NiMgZsl6VwJ8wyst0HcqrpoE++J0ehnX3E2iuEICZFfQPYUx4ZvCGizTBAiPcrq12kMWRgTpBpi7D RhKVXrW8fV0Tid0NTbTg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1ttCl6-0000000FKoA-2a06; Fri, 14 Mar 2025 21:40:00 +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 1ttCl2-0000000FKgz-3SeH for linux-riscv@lists.infradead.org; Fri, 14 Mar 2025 21:39:58 +0000 Received: by mail-pl1-x630.google.com with SMTP id d9443c01a7336-224171d6826so63846455ad.3 for ; Fri, 14 Mar 2025 14:39:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741988396; x=1742593196; 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=DxdMdi2cubLlt4TIGXXJrBSyWEjwvqF79MEiREZoHyk=; b=dwFNAltqseFj/Tys2l5iTKbdWkHc+8F23QSxS7jLCmvjGjfDrp3uEQgAh70tpP35QC mh7z5HD8DXsD6bMjjBpy2nBhwGE6IXEI0d7f0Cng1EcLtKwWQQbbORd8Fyodsk3OmWfs Mfnl9UBxfr2RKLi3eJvpZDTlo40AaRQlPcQWEuteCEHFtaWtuOhU9Uy4MYVTI0gKX0DZ LWYZvAphN0PW1qaKjcpQ4LBknzZ1p6ySPInpWLrWUD+ctIF4I5LaJ7E6qjQPjpswwNG7 FUkkbsEty8A3mHUeBJFBffMiMNR0kb3OcWgUO6SE43/hQGh0kOlogpfSPWyPCxBTSvl4 0F3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741988396; x=1742593196; 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=DxdMdi2cubLlt4TIGXXJrBSyWEjwvqF79MEiREZoHyk=; b=AvvHeT4CS3uTuOAd5mhgLPuBOZiq0I+tMvz+qlzzcp7Wa3NmdM+M8SYI3So1IkGj5K 9ADxE2twrGCDkIKfCGX+Yy//PsgJXm4n1hY/rxF4OkrTXZW2ubhx/0oTbqeniXxeX5V+ /Us807VpBtft+O1oNCFhLFz2h5dmOofbBwhu6weRNbpxwu+MmX1p2QaUfNhr5X9QGMoC uqOPckAN/CWhH+2k1pTWJjIwQZaxT/+fMIRMTPRl30yJSkOsbHVxGqEhc1jJV+vXvHrd nt6hdq+rQIn8ZHPl5kisZAfZFWqpOLfjaPOZzkksRPaWb1rEB0rIHpxpSLA/TuyuM24t Ne5Q== X-Forwarded-Encrypted: i=1; AJvYcCV2z45yN0dCcZHskZpHOhu37S8ALTLY22NMXY82qyRADAAfC+EoO98d012DGrig93d9Bk3y/Au8A0K9sw==@lists.infradead.org X-Gm-Message-State: AOJu0YyN6+6IFCVc+xAcnjYS/t7YevexnMo5t6Js8Fsxez1xivI5EeHr 2CYZZ2KHnzv6rRheKnNLHWU/hCsrmwB38X9ipHeXoBY5WrlqkCs1dH+Px8ZMiJ4= X-Gm-Gg: ASbGncsKViBpgge6OSlerqEqJo36j7JIepU50kNeZTwP/MxR50VNeqUjpM9ZmDWrycg vXPgMfmwbRqjfS7S0+nSkMQkni1jokjFvifGvswtVVu/K5DtSjWNAqMdaiHHZFyhMdcwfx3SAj0 AyexUSzEk3pClpuBOEn+v2gsb/9oJR7jh/mHsWMwEDeOp55qxdqd0282Am78SQEmHCDBK19/ljH 0SDdRJdnvUgIlMmyojTJhXad7clUb14W7LTOtkXz71wYWo09LRUG2LXI46rum1BFQdzS7Wry2Xu W3xCctxy+WTZ8AyYS4S5NzafiPvj5cH+vlWxHIFOZYn43fXfTdxFwnDgLGlSCDv8PQ== X-Google-Smtp-Source: AGHT+IH+xCwTKv/QOpKiV4NU+zofe+/FULTNwPU3AjuhKtiuzJi1q2DiCU071AO1IM2f30UQ36o6NQ== X-Received: by 2002:a17:903:3c6f:b0:224:1ce1:a3f4 with SMTP id d9443c01a7336-225e0a62f48mr57173645ad.1.1741988395873; Fri, 14 Mar 2025 14:39:55 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-225c68a6e09sm33368855ad.55.2025.03.14.14.39.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Mar 2025 14:39:55 -0700 (PDT) From: Deepak Gupta Date: Fri, 14 Mar 2025 14:39:30 -0700 Subject: [PATCH v12 11/28] riscv/shstk: If needed allocate a new shadow stack on clone MIME-Version: 1.0 Message-Id: <20250314-v5_user_cfi_series-v12-11-e51202b53138@rivosinc.com> References: <20250314-v5_user_cfi_series-v12-0-e51202b53138@rivosinc.com> In-Reply-To: <20250314-v5_user_cfi_series-v12-0-e51202b53138@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, Zong Li , Deepak Gupta X-Mailer: b4 0.14.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250314_143956_950926_D68A0A86 X-CRM114-Status: GOOD ( 29.29 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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. Reviewed-by: Zong Li 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 Fri Mar 14 21:39: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: 14017583 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 B9DC6C35FF8 for ; Fri, 14 Mar 2025 22:51:23 +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=tg9qCos8S/cWHtiOacQHLo78HWhLhP1IrZ8I+A1uFPk=; b=1Tyq/S6YuLbdds 4jJvAgKnswh4HC0kh1tV+EpCTV6IZ1LBcmKfCTIBY7v6pYHcEfqyU6k9byZgTi5J98a3rZ7fn5vHK r5efAyK6zXHGoW2gtjW+naDAjudEC/s3BGVvQjk5O/F5hzAaQINpWwnF1i2lU+cPrdIxjE1pl4MYi 0qkerX0fKsTwqAzTHiYkOf0NLnOsvVJGxNPeskTMTpNcNOe4xg28lWkLFEsgszMSUrneEaFfUTfhb 8NaXWPYszVudRAE2x/t2dRlhhqH0gPgkdQ+VOaaqjed4mOGEEqj1Pwx9OwJ0bbX2tKMRsnLSpIMQQ yqT1k1ne6MJH0SH2TflQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1ttDrw-0000000FSsl-2qtZ; Fri, 14 Mar 2025 22:51:08 +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 1ttCl5-0000000FKlQ-0b75 for linux-riscv@lists.infradead.org; Fri, 14 Mar 2025 21:40:00 +0000 Received: by mail-pl1-x630.google.com with SMTP id d9443c01a7336-224171d6826so63847075ad.3 for ; Fri, 14 Mar 2025 14:39:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741988398; x=1742593198; 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=FSXRiaPQhF3C8L1paZRazUCu0Au2I7GQSlOCeptG0mo=; b=Kbr+mT3iXM+eYLqum0vO4+1WvvxUGtUxlFDnxGZk0i2Y4JGrQ9HU9A/mdclsqveIQP bPcdJ6p722wwnrifTVTJDfYplt7tZJqa/56DBhFZbafgNMCJwwQNjHE6VKAtEqynl8UW ErqQ8JEkLCDl9knYHgMfXCduFaBOSOew4lryhCac/+98/aejErKQ+pXhdZ3SkFFiXiPj JatdvhUVByYXIQCeVFumI0v/BL+2DYpCFLT1PNE5648aZW39r8JM8iPDEVapItMbob73 Z2Vjob4KIZECaul13+53AyFeLqCGmmQn06TrCbBUl2ut2GZripb05iOTbuYW/mbtgrX3 Km1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741988398; x=1742593198; 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=FSXRiaPQhF3C8L1paZRazUCu0Au2I7GQSlOCeptG0mo=; b=tFA6X03tW98WSHX1SiVDZTcQA4C9fH2LXovqGaLTctnA63nMGJi3crfDgoAh5SMsGU ir9mgKGFc+ErYkyZYWPMn42+HxBNJYCgXL02XK6vs/KEZdpyj1bpUAAKxB/+Gj56XRgc kEehz5Ea6LOhI87fXMfsh7cz6BtdXIHoSKfK8QhxUX1osUivWf5s/AcBqYj8IeR4mjfv rzhUXEA+UMItKkd8vtJEKozR0Hw2F5TdJCquzbT0rV/71rVwko86rs3/CT2I93w2xmXv DnClZPb72Nc/N03O8MONW/wqL3rXWMAtwLcaMc0s+AC9klKvIYuicsGzRNJxg1Uk0ecn cMRw== X-Forwarded-Encrypted: i=1; AJvYcCVcpIXtICXv8BQVF/Jf+uauu0Nd2i6Ma3Tj49OreNwwet5McEe/CXvIhKZSa7JQMzrL4wRlv0gxIldVTw==@lists.infradead.org X-Gm-Message-State: AOJu0YxfMOwxXOL8vAdNchhl0+UawVCfRycNhY79zFvjWSjH28nmI1NO xVP4toAjucNUaTT8wyES2vd50UttA6X+MnkZv3YUb+qnJLjlvIF0UTHcDMTrweI= X-Gm-Gg: ASbGncsIy46m20F35RKQY2W7ndiW+ZMqPde6Ym+nebIMgPcl+AAGClQtXw3lX7qbMap eaYW5mCsr9KkwatioXFpDqNOsdyfVRYkXxWvuF3AR7C6ZXGTG1KpLcx7hCsrjPokzc+t3GQnttm TRaNs0YYInV82AnlVc2UEY4P0Y6JTLvVfDj4rBNGX1QMPJ6iJugwb5EZFyTmci4Nc0m6X6aestr YmOolPL36cbCsMyeVV4OfDCQWVVLKV9VC+czhldf2KsAeP2WJe0PSu65S7T7CiMMAIkinU0bb38 BaCaJ62VlPvPAXpl4YZSsic/kLzqHQ5qKWvq3QeWx8V6HK6LoIIWN1U= X-Google-Smtp-Source: AGHT+IH7jCIcDWCYr0Yu49RNZKZq6VgNs1mV3Eqj70D9Zm/jlLYpyqAM9w/Tb7MkRDYTOPVbu+3myA== X-Received: by 2002:a17:902:d50b:b0:223:5379:5e4e with SMTP id d9443c01a7336-225e0a62e97mr58609575ad.10.1741988398438; Fri, 14 Mar 2025 14:39:58 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-225c68a6e09sm33368855ad.55.2025.03.14.14.39.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Mar 2025 14:39:58 -0700 (PDT) From: Deepak Gupta Date: Fri, 14 Mar 2025 14:39:31 -0700 Subject: [PATCH v12 12/28] riscv: Implements arch agnostic shadow stack prctls MIME-Version: 1.0 Message-Id: <20250314-v5_user_cfi_series-v12-12-e51202b53138@rivosinc.com> References: <20250314-v5_user_cfi_series-v12-0-e51202b53138@rivosinc.com> In-Reply-To: <20250314-v5_user_cfi_series-v12-0-e51202b53138@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-20250314_143959_231595_B3E9502D X-CRM114-Status: GOOD ( 23.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 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 Reviewed-by: Zong Li --- arch/riscv/include/asm/usercfi.h | 18 ++++++- arch/riscv/kernel/process.c | 8 +++ arch/riscv/kernel/usercfi.c | 110 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 135 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..b93b324eed26 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,26 @@ 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) +{ + if (!cpu_supports_shadow_stack()) + return; + + 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 +292,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 Fri Mar 14 21:39: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: 14017569 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 64937C282EC for ; Fri, 14 Mar 2025 22:51: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=t8i2T4kGBP/My5ahgJcibXbYY7i3OoAt+iwC4R1Hgns=; b=3dkCsIPoP65ETW STb+F9In3ZByCMZxBJ3vzjk5q7i1PB/BUvKMKYSrrFnjLoAK7S7eClpwoEgFtfUbqcJy1SAFuub8P mx9ipTAI0OZDamR2ltdudXKQia4klxxAS8hHIIVhXpVaCr65FE4RPniMULLhyTTIKBVE1ss7J3zKa 30u0GAfMOvOEAWFmXi4eDCbr1xe8cjhOTgeh0NViEwUR64gmqt7+hgqkGb69C8Al/WYn7IQCqvfIb 5hHbPg4ERR2B4Kf4Cjlwjry5OaatNKj/minLDGBHVWVjYy8LV/JrFH/mOC9wz8LQCGBBf5bkcyudg sYu/fBx7KiinZvc+9fQw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1ttDrx-0000000FSsq-11tr; Fri, 14 Mar 2025 22:51:09 +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 1ttCl7-0000000FKpl-2Snk for linux-riscv@lists.infradead.org; Fri, 14 Mar 2025 21:40:02 +0000 Received: by mail-pl1-x636.google.com with SMTP id d9443c01a7336-224171d6826so63847825ad.3 for ; Fri, 14 Mar 2025 14:40:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741988401; x=1742593201; 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=onO6gV+gwl07vxhB4is21Yez1q3KjrVLviDZSIMIBso=; b=biopbiGHmCjBVFOH0EHCKoQ8UUwsYGKTACvVUBZkBR2dkPc5+JzCRq5Kxx3XSUbQ9N 5geybf/gYYEu+am5stK/nNrp4lRe5vuhTwwXAwmxrJlAy8/UvMC/eXAfbImSyYPCJvfN YkHEqw/kOazUlXa8mn8SsigiGfYgvLvEk6+63v1He9oT6pqeZYqOPmC5UKcKkCUq9cUx 7XKT0QHLmKG+L8kOw5nqdkMpzMY8a6vxeFuXaPzsHNMNNICHQZbrnC7NGol5dgWDIUKq UUKfnMRgIQh18Gy3vf1hz6yET1qVNR6IUpE2JC6D/G8hJvJmFaQdwdkZhzL6hAFJJEV3 uMWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741988401; x=1742593201; 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=onO6gV+gwl07vxhB4is21Yez1q3KjrVLviDZSIMIBso=; b=HmHaaEnJ9mJm50rA00anSL326PswF7H91RXnJ3GqnCj74BW0pdE5z02lmGfo8igg6h c3XEZ05ZpKnjMRPpB/vLLRIuasGZtSHST+vBSD9grmNCZtIKNXxbZ47t6TZpePtCyuhf vV1GR6i3re7hBIqMdL58JU1tGylVOAiAyDAkV5jHglxJKK+RDYny8KVF1Z/V0BP1F1Eb GgJA6tbpDeqRYNrg4hTp0tHdztiIJiIL8IPgROGl56O75GutjSiTLWo4ZEySrbpcNsJ2 JhbxkC7X5de8gIB4BTk5kv58qTrmb2c2w7hvY359qsBfL86deQgpPuosA8M3JYTTJUWD ooFw== X-Forwarded-Encrypted: i=1; AJvYcCVYpVq4OPTjXexSPAF/V37egiFtSR5EX3njmHIHSLe/tf3Og6JCzUH6a/YOp2MLtuSPX/c6KAglOdJJTA==@lists.infradead.org X-Gm-Message-State: AOJu0YxEm0HUFW3yNTj20tZbaL9sqfgGlblqytGe3Ain2nWgY5FhvUUm t3QMFx2sy6UEVKc01g2ynFGng0vzkrjb+bS4mQjLzIAfEIAwVYPnHRkvUVV1sb4= X-Gm-Gg: ASbGncu29P8vszUsT5IFjjIe+gCXNzf+/gBRYAknscvMx9BkuIrjPqucN7bFrYy05pr U/d/k3bGgmkRvpeIGk6xgJ09PnKyGLPhbAHPHdo3Ik+Xr+9w47R4oyuUPj/ncuLuhielEAAR5Yl 9LzspOAL9z2y/F7/Sn5x85yrAbVAV8sbqlRfvVei/yX5rCa9mbzPtFQm9vtJVdz3D+u8iMTDG+g aySbCN3eE/47SqUHlctwhFReYx434DrR5bi6PNO/dDmbSKE5Ww9z+D2hHCyAxBgrhoj11k5Lh2i 7xRhTfT9lQqZ470CULjzrMUqBwf6+yhfMW/vZaVwXxGZ7ap/GJemi20= X-Google-Smtp-Source: AGHT+IFRRNpqWVi1Ekf4KJDdZ9UetFfwRyekdTeabqYxkLE9P/UbVTlwOASvnLTEKfIF4erNHXNXJQ== X-Received: by 2002:a17:902:ef51:b0:220:c143:90a0 with SMTP id d9443c01a7336-225e0aaf01dmr67336755ad.24.1741988401012; Fri, 14 Mar 2025 14:40:01 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-225c68a6e09sm33368855ad.55.2025.03.14.14.39.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Mar 2025 14:40:00 -0700 (PDT) From: Deepak Gupta Date: Fri, 14 Mar 2025 14:39:32 -0700 Subject: [PATCH v12 13/28] prctl: arch-agnostic prctl for indirect branch tracking MIME-Version: 1.0 Message-Id: <20250314-v5_user_cfi_series-v12-13-e51202b53138@rivosinc.com> References: <20250314-v5_user_cfi_series-v12-0-e51202b53138@rivosinc.com> In-Reply-To: <20250314-v5_user_cfi_series-v12-0-e51202b53138@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-20250314_144001_651569_896ABE2D X-CRM114-Status: GOOD ( 14.03 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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` arch64 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 Reviewed-by: Zong Li --- include/linux/cpu.h | 4 ++++ include/uapi/linux/prctl.h | 27 +++++++++++++++++++++++++++ kernel/sys.c | 30 ++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) 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 Fri Mar 14 21:39: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: 14017570 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 3B22EC28B30 for ; Fri, 14 Mar 2025 22:51: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=s7oMGkwksoVWvKOiLEIAGf58GuiXh3e0pgo2muQfouI=; b=jeU1vsiIGeWM5t YBIY2jNCJ3XAIV6+sZ9bpvFpsucXarZTMDnZY+eon1aVP3Ue2nwSqEZoEPkQ8mRCkP+7AwZlJYT8n BweK3Rb73f6DA8HuqbeQfnuLbXyeA4A5Bkrb8wTISrVnoHsQBNViv2ayBJOnE5/smhICVozssF/7/ LXyahFc7OVIJG5gx3qaItzDCVjdGhQA6Qcv6IUQBPv1ZqIGMuoRmYtvdGb2qtrJXhO1Bhnd3p5XAG 2p8qXy6Uu3KbzJcW4y+18yN/VGswywusYSOA8IkWnQnUqoWwow2dsRaq4wfXTTDCbKrQJUWAsBzwq 6dezfx9lJ1EnmsfcgpzA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1ttDrx-0000000FSsu-3J7f; Fri, 14 Mar 2025 22:51:09 +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 1ttClQ-0000000FL9d-2k94 for linux-riscv@bombadil.infradead.org; Fri, 14 Mar 2025 21:40:20 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Cc:To:In-Reply-To:References: Message-Id:Content-Transfer-Encoding:Content-Type:MIME-Version:Subject:Date: From:Sender:Reply-To:Content-ID:Content-Description; bh=rWrKgiNxYfbME3P70C/T1Lq76CoQh/bECvkoPEvcu2k=; b=YrDshwGDjY0nyhfDjdkpfm/Nru BNogjnMVAbHWMXIzYG0TwWAGZ17po8APbSbO2PGGrICeC2cpoYWbpO+SdU3g6+zdsYX7sx+9KP1xK GzDsN/pa37dg3KAux3qolhOl+hURSmIDQKG5XtxftsKnyWpWnbTC8wIVWRM6QvyxCuCY/ZH9QPxaW seGF2M7+N+a/xOpbICbB4X++EoZWavu+3GElDjXQ5DHrSK8jpbfM0uGSUrPXkyqSfbS+FftsoF5la 2cPMMAx50+Lypxr8yht9TbucoPIE4UqYPBjvtKJgaUjZMJKXp2USMtGYH/QuC/u3Q491a2rnJtm5b 0aIlFeCw==; 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 1ttClH-00000002x86-2Xzq for linux-riscv@lists.infradead.org; Fri, 14 Mar 2025 21:40:17 +0000 Received: by mail-pl1-x62a.google.com with SMTP id d9443c01a7336-223f4c06e9fso47193665ad.1 for ; Fri, 14 Mar 2025 14:40:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741988404; x=1742593204; 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=rWrKgiNxYfbME3P70C/T1Lq76CoQh/bECvkoPEvcu2k=; b=1pP0Saj+QfVERmdVbx2n+I2aEj5WA2m+R5+8/TXNQfYRKgnytbbS+PUR/H5XLQh57M 6Xlvxj9GsYLieF7/BqkyWnf+xytfhwMl+KjgeiOLcQh+jX61DIiczKPz0caP2LQy89aK H8pWSN1bxYjbVyllbPivYXKZ7KIgLL+oxH2ZFXn6na7SGp0WYTr9lR9ZCrGIpk47C1Eu 3vIvuq/gbnVsTU6TSfuz7FGLsSNoCKunlumH9ro5a229Iy+vS3mFc4zif1iracXMprrP FQHlKVsId2cs9y/Y28duhvr+5s33xlkVvmadsWKYtV2pmEYuOandhV8/i0rqSPEI0K7e ILWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741988404; x=1742593204; 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=rWrKgiNxYfbME3P70C/T1Lq76CoQh/bECvkoPEvcu2k=; b=WpinF7rl/lmfdwWhrv3O366CKieq7jQlrYkPrMwqvK8qEOjDocmPnRuwwQ0GbCWPiB MJEj+3DSTY51vlMMNNgbX7uNdSZiTWcknrz4DKyPjUoM2REUgSI6+74sN3khxDSYQ9l2 ZontxRwL1cvTN3Fxs1+++bW8k16pgEED0pNbXiBdogX4/hjAAaeBuLLyox6V07X1NcRy 7CDXNBQT+jq+y88mlcuWpQH2aBL77gfepw055+n+rUfxPdBpd285d965TE8PH9gAVrqK Ap22Ze8CmmuR92nFtt2inmsrgAnF1FZBBh4Iufkr6J9dEkKdLtHJJyTLfWFiyzyZn7TR 7MfA== X-Forwarded-Encrypted: i=1; AJvYcCVHUTCz8sgkIZ+0DzhJcCavHCNVplYN0FHa8rqMhRMw3FXseEIXtPqEUZqLRPOflCdUQwW2X80b47W2lQ==@lists.infradead.org X-Gm-Message-State: AOJu0Yxi5W8ZM6T12CnL5G6x2A9IHRMDexso+P2YjiIFXt32AD7cR+to eNz0m6DTzqf7ry7cwauaOESm9GJDQgry92ZqrxT7o1wnA3UfAMTCV8FbEbDVjA8= X-Gm-Gg: ASbGncu5rixRqDPMdY1ajBgb/LoZEG7fKZ4w4i6EPCR/3oBNwarhxMV2wtSIx71qcoJ DmCE5oCOXFGuMo0qPLwODXiqBKk5/3ldMe4t+W48pthRR/Q5Gz/Zp9YKEegRSRNleWXGe0TfQf/ mnVzL0/Z8YYJLGxz747DULFHY5SLPhDwK51RiO7UQ8RDX7emX1HuszHwW6iDqWHHkTRsxOuFuw7 WVQdxTnt+TjLpDJ3oz8lO8cwW6VfUC+1o95qITCxmPRI3I3rQKhnUyW05IWF9A914Oolc8P5btW C+0+iSB+h2FzOkn2CLkwbHBAJbmFL7TBMLcJ5ZNMX6PsNfOwhmsp61o= X-Google-Smtp-Source: AGHT+IFB7KLzuKm3iRuPqbgCrAWr33vleUkeuBdLiqwVXE3Idc26W6StRutTcYSVfvNmBJhH5yZY7A== X-Received: by 2002:a17:902:e5c7:b0:216:4676:dfb5 with SMTP id d9443c01a7336-225e177d49amr54022925ad.21.1741988403722; Fri, 14 Mar 2025 14:40:03 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-225c68a6e09sm33368855ad.55.2025.03.14.14.40.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Mar 2025 14:40:03 -0700 (PDT) From: Deepak Gupta Date: Fri, 14 Mar 2025 14:39:33 -0700 Subject: [PATCH v12 14/28] riscv: Implements arch agnostic indirect branch tracking prctls MIME-Version: 1.0 Message-Id: <20250314-v5_user_cfi_series-v12-14-e51202b53138@rivosinc.com> References: <20250314-v5_user_cfi_series-v12-0-e51202b53138@rivosinc.com> In-Reply-To: <20250314-v5_user_cfi_series-v12-0-e51202b53138@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-20250314_214012_931212_DAC63242 X-CRM114-Status: GOOD ( 19.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 prctls implemented are: PR_SET_INDIR_BR_LP_STATUS, PR_GET_INDIR_BR_LP_STATUS and PR_LOCK_INDIR_BR_LP_STATUS Signed-off-by: Deepak Gupta Reviewed-by: Zong Li --- arch/riscv/include/asm/usercfi.h | 16 +++++++- arch/riscv/kernel/entry.S | 2 +- arch/riscv/kernel/process.c | 5 +++ arch/riscv/kernel/usercfi.c | 79 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 100 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 b93b324eed26..7937bcef9271 100644 --- a/arch/riscv/kernel/usercfi.c +++ b/arch/riscv/kernel/usercfi.c @@ -72,6 +72,35 @@ 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) +{ + if (!cpu_supports_indirect_br_lp_instr()) + return; + + 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 @@ -372,3 +401,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; +} From patchwork Fri Mar 14 21:39: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: 14017582 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 A1FE4C28B2F for ; Fri, 14 Mar 2025 22:51:23 +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=eLjBUaJSvHeS+Pvi1YHmXoRETe0+NAMVpylojWqMGNY=; b=jFYXT0uULeuSlc bRIRlPxmEYZXP2rI/gOnjqZ8Um7CWhSubzeB7MlQsbV1XqHFH7si3VABPeD77vDXgAK78WZAKLxi+ ciaWjnQqHANsAHcdEwRt7K8R7K9DJBzTbXjT8i0YSWyvDs7Vy4flL714wArhm/g28kSoGJUv7uzx3 oWwkouzR2CIiem217vFZyZJkiZT+F69BrigfcylND/kaF+C9m/2Mrf1+ydCl8UasaGxgFLNB3R3z9 5A9SL7sCgAHOHj2rkI7JxjwzJILvLtDjAIp01bw30izIkanlMoOcBExZZcnyx13dk7GO6FyR/qHZE r7WSIJQjLYLQN/zfyhiQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1ttDry-0000000FSt0-1UYR; Fri, 14 Mar 2025 22:51:10 +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 1ttClS-0000000FLB9-1ntJ for linux-riscv@bombadil.infradead.org; Fri, 14 Mar 2025 21:40:22 +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=2Qy+LghJ4/M4JzYV3TrsgP9mTB2OIgCnt82uf+yKrnE=; b=QWVpmbQODsZ+mXIi0lMjFzIF3X Q/bP8+Pyy2Fm22U3SjSEyEaDwjgD7jStVK9M9Gta8De2ChUXFVbpBQ1dOypOGb3g2iqwHkvBHW4Qx BOCRWNjDVxb4FwM2AkIyZe9hb8Uz17EvazQPYd8Ke3mSaukuRi5OD/LEQU2LDrLDg/bItgR4p3QIk GxY8sIrOCaaellg9apQEwYD8iOFdi2wOyAU+zN5fTNB8vWnlEDXjA+KuIQuZcD0EvQSVano/SIyPM NjrBkKr9AiqGyZYuSgiLSGVG16NFDI1FE3U8oVU0DeJ7YjT5ZF+T8cyUUBw6DJ5weIBQEQtl2sr1t VMAnb6cw==; Received: from mail-pl1-x62f.google.com ([2607:f8b0:4864:20::62f]) by desiato.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1ttClJ-00000002x8A-2Blx for linux-riscv@lists.infradead.org; Fri, 14 Mar 2025 21:40:19 +0000 Received: by mail-pl1-x62f.google.com with SMTP id d9443c01a7336-22580c9ee0aso48362195ad.2 for ; Fri, 14 Mar 2025 14:40:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741988406; x=1742593206; 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=2Qy+LghJ4/M4JzYV3TrsgP9mTB2OIgCnt82uf+yKrnE=; b=rcEn89yanN51rfe33Mh5NkZJicfjEonEKmeV/K/2YXifFU57zS3VKsfAYnspmTTWwn x1Yfi8RzL+PbLguHXTPFJ9YY0LL3nDqrviGy0IPl/66bQluz1xLnIPxEIdjb4NNyF2GO QTMVnja6hgAQELPt66C4nAk2L/z5dOcqlbNae1VuXn5VoN003xSTVnfXJ33SmtgJ0HSi yJnq9erhfvS56kzLkdSgb4Bv7hgvfgWi0eogvUMDovC4xTl0v6hSU2glnWDIoScOa4aq gAgA/wbi42CaJ2GUDorto/J9Gj1seXOWhy5JWn7Bg7MSt6NShkyHW5bpub99yemTwmde Hxqw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741988406; x=1742593206; 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=2Qy+LghJ4/M4JzYV3TrsgP9mTB2OIgCnt82uf+yKrnE=; b=Ljwl5wwu3t5vGD/4LVFSGun9mFKWbv9FNG/TvpEN5+v/OQIG/xH3nn4mNhn4qmfIP8 wdTGDGNK9nuMXoo6XGTgWQvWtGf3VK4E23OxEKphtu5QRB+XIhOuub47Gd2ugOl4jKN5 M9ReHwI3z5FWE3CTu6DgqqaQj7IBwNhVAkvDvChe0I5YyFwRsD90gC/6Vr55BbuNsbw1 jBqzHhuX6CCeTSCW3Gh6i6a8tBcw2dCC6bOfbIAJcpygDEmCgyselSLzD74BjgwTPzjK +x42Xy+HOqWrNPoG3VSLe8Bdj76u7q/PBNWRzJod9BDq9YtYrr4ZFm80wWFCJPzIXF6y KYCw== X-Forwarded-Encrypted: i=1; AJvYcCUvi2yRYMicuOJBhrBTDCmYEgmLyndn8azKjPFks/0KAhYGq+WL6Ey5Jwy8cnMqXLji8R+bDOuGACJDqQ==@lists.infradead.org X-Gm-Message-State: AOJu0YyKrGQW/Zp1Re090V7Q6URJ+V4DFP/1EwMQKkC35P+qnkW5oeiQ xj4Gj6bDXgK2GOq2eINiDub/mpw/Sqx/ou/4D8y7uVthbXmVX/dDB8DANndT5U0= X-Gm-Gg: ASbGnctWeMDlI60776SQOJBxMmFkLCxEqozyvGpDlYvtIPLSL1qn0HcvBxUvC+hKffM UMmjcSnLE/irivfsx10svgO9VAF4CjpET6Eiml+cMbIdoxdd1eUANnV3bbEM1oLUMs+l6+Fio4/ lShRkrAOMxRvUjDztoTowHRbagS0decKgPD8KMTTrHvc0k/ipxhrS+tkIOfsGCVu6yyCHPtwZMI /31Xk3xH0+f5HEDFb/JxAH6/FVQx2oTNMOYJ+NU/TookD3kUZyWadH+hE9Bi5iF8w4+mTEI+9wF CeSB5NK4/w+eET/94/FIQjPYzZENWALdtlljbA8hkRibA7a3yHULQopfa+X1pCfh1A== X-Google-Smtp-Source: AGHT+IFZGoCstNHVFobdYDMcxqcukAL5Q469eJyWCBWchEfbvVnOKA7zPff+GIgC5IBMZVr9zGx+qg== X-Received: by 2002:a17:903:40cb:b0:224:a74:28cd with SMTP id d9443c01a7336-225e0ab5216mr53565875ad.31.1741988406346; Fri, 14 Mar 2025 14:40:06 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-225c68a6e09sm33368855ad.55.2025.03.14.14.40.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Mar 2025 14:40:06 -0700 (PDT) From: Deepak Gupta Date: Fri, 14 Mar 2025 14:39:34 -0700 Subject: [PATCH v12 15/28] riscv/traps: Introduce software check exception MIME-Version: 1.0 Message-Id: <20250314-v5_user_cfi_series-v12-15-e51202b53138@rivosinc.com> References: <20250314-v5_user_cfi_series-v12-0-e51202b53138@rivosinc.com> In-Reply-To: <20250314-v5_user_cfi_series-v12-0-e51202b53138@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, Zong Li , Deepak Gupta X-Mailer: b4 0.14.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250314_214015_262956_6F78D1C6 X-CRM114-Status: GOOD ( 17.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 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. Reviewed-by: Zong Li 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 Fri Mar 14 21:39: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: 14017571 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 386D8C35FF6 for ; Fri, 14 Mar 2025 22:51:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=4zqeylQw6K1XmeN+KmrsDIr9DHpPuxliJ1VDOvHF6kw=; b=KWR92DI1MLkfsZ BIZ/cPHD7X0p5uKRw3/AkBDApje2Ob7kOmDkO9/myEpsL5KipX4FGcuCc9w7kNLmM8WzHz5OO+nL1 +AUMxYS928MnRi4GjcHXN2ZD2koV2QUHVqP9T4XHHwoZaYMM/uNS41dHsPGqXJL74/26gj2PzMYhb lTPr/1S16vpnUAC4wnWlNG8AZ6spkvSOpEACqwirfj8stWSrDre9PFNzrylbVPJOOIsJ2uZUsrXxh KnCISy8xN2v6vp7CPo/NTN9YmNJudsNMjEfSKRRhc2YtzPnMEpED+Wpq2FK0KfqlaIpiMTBHMY13G 2UfeHazzfsAnt5TpMreQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1ttDry-0000000FStp-3xgz; Fri, 14 Mar 2025 22:51:10 +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 1ttClT-0000000FLBl-1HxE for linux-riscv@bombadil.infradead.org; Fri, 14 Mar 2025 21:40:23 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Cc:To:In-Reply-To:References: Message-Id:Content-Transfer-Encoding:Content-Type:MIME-Version:Subject:Date: From:Sender:Reply-To:Content-ID:Content-Description; bh=N78rd5bu5L3Nb7UCylb6NhXzPkib+ej1wIZfC16ALRg=; b=PDfN8zloXmviQldlcHZ4xjztT7 O+jkb4Vmll/x86rGve+usR1SwBdjgXHcops89By+Oad3ZWHvzWp0PmFUsXJp5m++4GdWcGO0CpKmJ Pgw1WF2B2NikBWsYXk8HU9JfU9vNxj+VWQyMmknQzYUOYuom5hDIjKCfXfef/kTHCavvVyeYyPZzp qt5SD3a1VcORLtNvBFfZBHI6KwkS5JU9cuHURu3UjVC0sUcTfXMcmIw6nyxVErTRI7GYBhY0LL/fL J/SP4c2PO5mM6yFey5yB8HqZUJmSa1veSxrwHaqKcY3W3WNTKclEjOkrZJhrBe74ThjqJj9k+ke5M 2Ku6LDiA==; Received: from mail-pl1-x630.google.com ([2607:f8b0:4864:20::630]) by desiato.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1ttClK-00000002x8K-1S44 for linux-riscv@lists.infradead.org; Fri, 14 Mar 2025 21:40:20 +0000 Received: by mail-pl1-x630.google.com with SMTP id d9443c01a7336-224341bbc1dso51367255ad.3 for ; Fri, 14 Mar 2025 14:40:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741988409; x=1742593209; 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=NLzrouTXJ/TjD5dob7CH37Qg5UEIxHRFrPoer2OOgAcnMRmY7gtotlQ50RbJVFDrS0 69GgH5Y9tc+GsMQISsFR5ALNFC3HEDM35cqqCLzZXUeew4UnbWmi8CoHF4A/qOATd/Mh TqWW2NgD7OMhOGDyV5izL1mTRjx4DeArXI6CDMAyf7f5X+54KMHCPwKBbtbU5Hpp6Fk5 MOFXhxQr5hxG5Ius9wpEpX2XLIxNAHgSbB8AQQTe9dNyYhkLxZkDGM6+A82nRqo0ch/W UDmH1I7HGbq8E/0Q8rQGyGt00dzRTljt/GghRZwGq7mM01PYXkiwMgoFFcCPz+70HBlU ToWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741988409; x=1742593209; 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=kFSYPyB7KWuqiwFNlyW+qecXJjwOKDYHfO2fsBm3myGqNWAjhLw7HV30hbOigqgRI4 R2LT4PBlDPRRtLZYO6KeGPpK/2OYUl0R6FUh4jBu06NoDhjthKeGm7Jzrr+H8fmGL/rk NDM/tBYV4Nr/2CPXKDgx+S0bvASqo6j6IUr8Fh4GY1N3kqcVYASTrB1jI3qG/a0KoLyh RBJZmFQ4TwWsIVTRMtmUWeGsGlz15DUSVzK49JiqLQmIlX4FfTb5ooArc6fd/WsBLBS+ nNXUCq58CdvnRy576Jg+wwkNnIAVB2+XikNqJvEcDE0vzS/o2I/JaxybRRBomgbsvwgL LgiQ== X-Forwarded-Encrypted: i=1; AJvYcCW9DyXZDHEfFuGSnv+ZNvzS+zL5xCipcviC9v7z8+ohk1jBsih7acteW2Aa85O1QYUUEhF0aNtBmBBGPw==@lists.infradead.org X-Gm-Message-State: AOJu0YzzR0C48C7fNldkHddTxpZglt62dsK5s/zc3dIHg73r/OtnznT6 R0VuRsAS021DBNsKeUFz21CNE8t7Ius+rMq4FukxpSXDNl89uK/npbGSgD2DaA4= X-Gm-Gg: ASbGncs0er0STbm19+bVCAbHM+r+h6iaoUdKdgXQ0fN9iJqMDU62ZgQprJyRe4EmHkp GrYIi+uN/cJpZ0nWauBpMUUbx5utxkel5+95l8JYMz6pJpli3MIDjXu/yp3/JJkCBXtBmHlHEX/ VO4vU9E/yNzCco5FOjcre/iHVB+OuyJKYUrVush7R7YCLssu4iN2iYeW4iS1GSUD2yTxxyvIqs+ /aHLoGcCL5ZNiLu7wCCMaxTiZbOfb72M1JRZeEdxAk8GFQW4A4XM5QE3DXm5HIZqgE/Ys2lS7Hc Y+zPn2osF6ltWmZ/z4kyx1JCbjTN3v0gJ/v0HYsNHhkyjA1tpIBkLEY= X-Google-Smtp-Source: AGHT+IFSKd+2xICbPyI9jv8e0eee8m28O0Z2xE2zkf1BJxJdHUC0s7Oxq9dFc7HadAqrreCNrCxPSg== X-Received: by 2002:a17:902:f712:b0:223:42ca:10ef with SMTP id d9443c01a7336-225e0b09816mr52846915ad.43.1741988408934; Fri, 14 Mar 2025 14:40:08 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-225c68a6e09sm33368855ad.55.2025.03.14.14.40.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Mar 2025 14:40:08 -0700 (PDT) From: Deepak Gupta Date: Fri, 14 Mar 2025 14:39:35 -0700 Subject: [PATCH v12 16/28] riscv: signal: abstract header saving for setup_sigcontext MIME-Version: 1.0 Message-Id: <20250314-v5_user_cfi_series-v12-16-e51202b53138@rivosinc.com> References: <20250314-v5_user_cfi_series-v12-0-e51202b53138@rivosinc.com> In-Reply-To: <20250314-v5_user_cfi_series-v12-0-e51202b53138@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-20250314_214015_336988_FE26E81E X-CRM114-Status: GOOD ( 24.76 ) 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 Fri Mar 14 21:39: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: 14017580 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 747E6C28B30 for ; Fri, 14 Mar 2025 22:51:21 +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=SQ40ixJqYz8+wYsmTXDj37sTYfOJDOxzz0J63nTGosg=; b=sPnqiFDayvYGMd jfEy6qOFdc3sVJcrFVvv1Ir7NiURkWXApUUgTnGMUdL9wwHSW3+WCTtKl3VZBEv5MolHLiJ1vdObR uwilwv9MJIdLtIriWvbLD/Q00JAK5g07dTUyViG0AiSztqBNs4kUhJNI0UpKKTGTXPtKKWBSHGp+/ QkqdU+rt85tGcQBEUU85XJmGDWhsF14s5vR2dSdnctRg0f2L4Tbwxm+mlfH7tFqIMhGBYlKootMJk xLvc4NF9y3gwRwb4JpRWIB30+k9AN133T5QmKy/dOufQ/Z2NgyCUKT4CwOLPIfbVChZFlelqAXYXF uKIBHKQJ2GjbSO8opf8g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1ttDs3-0000000FSx9-16av; Fri, 14 Mar 2025 22:51:15 +0000 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1ttCm0-0000000FLYO-0UTZ for linux-riscv@bombadil.infradead.org; Fri, 14 Mar 2025 21:40:56 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Cc:To:In-Reply-To:References:Message-Id :Content-Transfer-Encoding:Content-Type:MIME-Version:Subject:Date:From:Sender :Reply-To:Content-ID:Content-Description; bh=ylS/7auodeR2tA3gsEjRjjzbbCoTIX+8VvBmJDYwjkA=; b=Y86XYp76ZEpvBEhtysGGHE7a9X O+RsvnaiSSdNhMi++Exg+X4Ahm+fZduFUHs4m2/klZYD/2lOsvSoiudw8UabO1wpgMwHita84ynUf JbAMEIdaJjE4WMV4vlo77fYQxNJYimKgnSkCS8EADrwRLbnwoexxRKc787eiS5na5rYEur5fKKRkl ClodWWGHKPc9c4I2m0NUESZQDo7Wj1Boebkx2pUrjS5aAcnMpKLnwxx44UxEIWm8uVs915i9cWTMe 3ZgkGWvZu5UkPcK3cCkJ56dDpXYEChBoUcaXGN55M4Re4BMhyy5f7W6+OSQqbderO97zQDklfcdBC /XBgQbKQ==; Received: from mail-pl1-x62b.google.com ([2607:f8b0:4864:20::62b]) by casper.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1ttClT-000000073tX-1ecc for linux-riscv@lists.infradead.org; Fri, 14 Mar 2025 21:40:52 +0000 Received: by mail-pl1-x62b.google.com with SMTP id d9443c01a7336-223a7065ff8so70885295ad.0 for ; Fri, 14 Mar 2025 14:40:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741988412; x=1742593212; 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=ylS/7auodeR2tA3gsEjRjjzbbCoTIX+8VvBmJDYwjkA=; b=krhwiiiH4nVAFO8p/q6s2z4VxzEh0FVYMSY8PEljahClG3JJwFPWEsJHKCS3YUnUeJ 7Ol2azgQ7nbDKzxgiCTw5ZA+TPxft6tJFtzL3YoFvsdaasrTGcF2qz/3YqFSfwEJGjef tXIFU8KpEn8j93pgTpSVAEdxpzx0FNNrdKObu+1L32g4keHt5IafJhvsOCpsUn2U/Fin 5okSs//Yj1aZb5VzFHmZwnuE6wnw0cHFIHVJNy3mTt2EW0z91Ft8okKOCA/TdOixcQV6 Hq7Ch+GOvyyy/byshSmUoMwYnrcvUFhVnK952WpGuSKfBbdJwrTYaxdVbmnHCQUxe0q8 gfYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741988412; x=1742593212; 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=ylS/7auodeR2tA3gsEjRjjzbbCoTIX+8VvBmJDYwjkA=; b=hHQmANlCNWlC41vpEkKCbnDB/R0+Z3wDPAQQMNTY858PzejV+PdDPc/e5/HhR3d5nT 5XtbJY90XjzX2lan0lKm7YGjDDaNU/SD5Z/sddeXW7pLqEtihQ9PJqsT5TwY93N/PQAI ijcsTp5n1YJ6cK5mql3OSsWJEQfZX7+K+01seM22wTaxCwg2gsc2FJaE2jEJHiBGMwNK VU6yiO5iCOtD7mYO9q2ytiay6SAaSoGhJshDYY2HqxAWNLEa6ujF5T6dY1a4rpBkxj0x /VKFzak0LNosJBV+sWUnBNIO8i2gRCw42qkYUhRrYKk+Zb2WwGcxgywjFbqJ7zl/rCFK aSvg== X-Forwarded-Encrypted: i=1; AJvYcCVDa69iFzkd2l/sUOBPPxpVOmPgvC1twEsjwiEDEt1zyIJePFypO9Pr+OuYi1BDXfbeTLKBNo9tl/gQgw==@lists.infradead.org X-Gm-Message-State: AOJu0YxC3NOL9Ylicq1LqWzyIqKKB+lkmL0kPwvm7jGz/vcHqyhCUi51 yFzoi5g630rL6g3pRYgs9oqzxG8X/kz0ViV8+9tivw83bnt/HwZZLDgA1UBED5A= X-Gm-Gg: ASbGncsL9PL3IUp97p1z26iz3brOrm8v5Xr8JWeve3Ou7M5NIQsL4cuuXF1YEbq3jlT 7U+yB4Wfxe4RDo9bUFGzsoC/vHP3hJiIsCKXR1Gs//Acf/0vTTktPin/kxre2yc0/Co0DqKdsuV c8Q4J9KO6WJz+almjuUVkXcfOfNzKFbCxLWXwee8/X+ZEn8lhcvgH4+VPTqSE0b/snBigl0oRa4 z7eYQmk29gAWVm+sgpb/7sIl0WvUXpNK6hS94B1Bsn1/dnlkoVqcqxVro3JSsZkq8cHVx6PYERP ViJc/SzakY/zYRJAFZJbMIoRjKrAMICv9KWTW82623TMypxsA2Hpd/A= X-Google-Smtp-Source: AGHT+IHOxFmuAegtqCte/c3FDu53tbalBV2n4me+J2S3APOVDJM7l3Em8+cF8KvTD/JuSP72+ZiDAg== X-Received: by 2002:a17:903:2793:b0:224:255b:c92e with SMTP id d9443c01a7336-225e0a62f23mr36836485ad.3.1741988411683; Fri, 14 Mar 2025 14:40:11 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-225c68a6e09sm33368855ad.55.2025.03.14.14.40.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Mar 2025 14:40:11 -0700 (PDT) From: Deepak Gupta Date: Fri, 14 Mar 2025 14:39:36 -0700 Subject: [PATCH v12 17/28] riscv/signal: save and restore of shadow stack for signal MIME-Version: 1.0 Message-Id: <20250314-v5_user_cfi_series-v12-17-e51202b53138@rivosinc.com> References: <20250314-v5_user_cfi_series-v12-0-e51202b53138@rivosinc.com> In-Reply-To: <20250314-v5_user_cfi_series-v12-0-e51202b53138@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-20250314_214024_203368_1D928F2D X-CRM114-Status: GOOD ( 23.98 ) 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 7937bcef9271..d31d89618763 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) { if (!cpu_supports_shadow_stack()) @@ -170,6 +175,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 Fri Mar 14 21:39: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: 14017535 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 6FD6FC282EC for ; Fri, 14 Mar 2025 21:47:28 +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=RRh/9jkSnw5UWu gTxAmaGaSm9IeXjRJkzl0HdEbzlT7Hnmek7U59sU9WJCRUNEwRiC9XE/L2oJmqQ4qk5DGqhz92e71 T8kR63ilIGuh9I3uN9GtovOmH8hU/ERmrZueVgo9vmpyqHQoCYBUAxNRcO0vMZKZX6reDfyxPqb0N 3mP4vp41wWfNfDEu9BdyeQp7kYax/FkZCCK3XQB1m83Nm8jc2lfrBAEEDT+VvjoQ7+0L4waT9BJBa uTFJ8Hnm2/O8b1xy0s9G+QwBcdYrozgmcKJAnrrJNioPHDF4GwnKdRpEqP0l9DldZGW9rM2fb2lTS qPTNQt1AgivL97gHyqXA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1ttCsF-0000000FNKd-1kJv; Fri, 14 Mar 2025 21:47:23 +0000 Received: from mail-qv1-xf2d.google.com ([2607:f8b0:4864:20::f2d]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1ttCsD-0000000FNJZ-1kzz for linux-riscv@lists.infradead.org; Fri, 14 Mar 2025 21:47:22 +0000 Received: by mail-qv1-xf2d.google.com with SMTP id 6a1803df08f44-6e8fd49b85eso37719586d6.0 for ; Fri, 14 Mar 2025 14:47:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741988840; x=1742593640; 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=QQVM3S+AWx7iJPkj2IhmBWsSIwZAOOzeN1SvTnXJjnquD8vXEbQkKxQOJRV+XveIje huXo5uuUorZ8fhRrD6sjsJumHIK7eYwTcUcaC3ugFnLjRnLZTqN+cFZNWZQ3v2OCqjQP IqUlGwNgmqOakV+1vMJrh17FVQwEQycOnkmqesGdWekb+zyFuWFRCp6l6OdkhSFO2CBD kw2GqoQuTHfbqTiCmvg8U3MZW0ZeagKF3CKfZvRODQWNDdkfP/lOudy8Au87oJdif11X XFLBaSOEdyTT35ziq1/AY6Ma4nf18RAjqiyxbCvqJ4URETZjMvGROOaVJN3oC3CSX03G 4L8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741988840; x=1742593640; 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=qxmt/rs6XUZE3u3fiInZ+cjob4f4wvfouXdWyxhO7kElez2rHTOfpcwuji+4ioSPyH AcZ/AvnCLV2BbjWQfyvCs1Zsk5Njd7WI0dVMCDRrFTTHKbxoBZkoWCx4yeKKEYTSm34Z YWSZHHvQFIPassuzEJePFWb8/aL6PZdCRa1fdCCVHFa4f2Xkcksj+ZrteI932dt8I5Lk Gv88vdxU1SOw8sO6AbgYBn5YfEjVVPUHbYAS2f4SJzgR+FmT/NjURW2+NoMJ7rvFS8ms 1+mm/cjCZ09lC2qpMh8B8zvuoWnMof8RaY0rTNZNfqWLIMh2VVFkLY/2Jr7XuOBAcTwi KILg== X-Forwarded-Encrypted: i=1; AJvYcCWLQGgIkreEAB+nVhRulIDQGytW2OZ+3KpYVolr3h58bldaVEKBMys/ii8bhuJHtve+De0U0uQIYdrp2A==@lists.infradead.org X-Gm-Message-State: AOJu0YzwVIc2z/dh3n2EhWH29ZmDunfAlT2sfXgHljHiT1b62VYjzAoe 5dA1HHqixqHsH665j/qQiVvjQEfr1TedGra6bIxz9oXMTWRGKTrXzi+TICk3ks73qPt/MgVijKK d X-Gm-Gg: ASbGncsRNtDNrBtjQeqQkrnO9qD0gLkE5wgi7W4CksihR0iLiTJswsVDJ/NgM+yGtYi ro3aIssR3y+Kt+mEZj7SEc7uzyTvKPvxpMjGJhMw9q+ncVNL3XExL9B8DQGmJd23brS7DPAISjd ChrrHwu+UyIrJken/bdFl0ycbDrVvDV9M/+YiiyjRAPr0pxolIYvWP5dxyaeETqAZUO5Wkc/QH8 bcC6vDc36fEZc4QYWiCJxiRZ46VMMN4PVmevBueKwKfVsVCq9m7P2Ee76BNjG8YxakW6B3/2OCD H5y4yvtrisu8i2214bGETGBNp5aZhJzxR3dG4zDTBsGJZxqkGIc3NXg= X-Google-Smtp-Source: AGHT+IFf7KDxEVU9NtNTwXHHWwqDPalXGdl70DI+8CNTgQz9PWl/u1/S9R+7SGLAPbVztXvT9lTY2A== X-Received: by 2002:a17:903:18f:b0:21f:768:cced with SMTP id d9443c01a7336-225e0a1644emr36247295ad.8.1741988414327; Fri, 14 Mar 2025 14:40:14 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-225c68a6e09sm33368855ad.55.2025.03.14.14.40.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Mar 2025 14:40:14 -0700 (PDT) From: Deepak Gupta Date: Fri, 14 Mar 2025 14:39:37 -0700 Subject: [PATCH v12 18/28] riscv/kernel: update __show_regs to print shadow stack register MIME-Version: 1.0 Message-Id: <20250314-v5_user_cfi_series-v12-18-e51202b53138@rivosinc.com> References: <20250314-v5_user_cfi_series-v12-0-e51202b53138@rivosinc.com> In-Reply-To: <20250314-v5_user_cfi_series-v12-0-e51202b53138@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-20250314_144721_449907_696EF821 X-CRM114-Status: GOOD ( 10.64 ) 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 Fri Mar 14 21:39: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: 14017579 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 A7732C35FFC for ; Fri, 14 Mar 2025 22:51:21 +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=mNGHByxhep+Mp4 izZpJ+N3stA82jLxMuYvUvSH46ltBAP9UMlUU7yMIaR4tN0bhZeGyqNoyhzCKnk8u9BtMo28QqqRJ znBNG5Bb+kfwZUs/PHdkzeqiV/KMXGs1trnCfhJcBqcjd9tZwvsoG173d7UNzNzd+rfaOc+GAR/iP jNNF000kNaxvoJ+7aqHkRldclk9PW9jrJCtJw7qMmqsJYT3wsWqxmY0bW2kohkoWdkgN+HfHqdI1k s0vTQqf1fcNEQ/HPj7aio7eyQDUAoEVkONHOffGJD9kVyUy2rmcdzrh4uWSot/QBbPnU9nVRIDfPY mHhIffRwHXfKrMJrz1fQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1ttDs4-0000000FSzR-3662; Fri, 14 Mar 2025 22:51:16 +0000 Received: from mail-oo1-xc2c.google.com ([2607:f8b0:4864:20::c2c]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1ttCqO-0000000FMnC-4AdK for linux-riscv@lists.infradead.org; Fri, 14 Mar 2025 21:45:30 +0000 Received: by mail-oo1-xc2c.google.com with SMTP id 006d021491bc7-601b1132110so1194166eaf.3 for ; Fri, 14 Mar 2025 14:45:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741988727; x=1742593527; 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=Cb/5G5pcE3Vfuf0/fUw6z9qC80/0IhiljDsPBFzGESPNndEbuhQy0hyvDZ8VCS9zBy NFOCkcYjetXSfUvqtunkD/itrwbFDnDkxnBMgkWyVnQ+vkiyRJxLlph1fRhFKcS43wkt HxiLq24kT3ee888jWBUpbRdxgguuJxehqfMbyJKHBU13Gcr7XUZyVz1YKvzMVAk5mz2Z CC8uLRwr9DV6V83jX0M5dSDURa00o4bFsuHrA2vYI5B+9Xd8Ej2I81cKKDtdNSA6zBjI Jzt41/iycqzw+ioRcAoOycWDx2iFBZDh+3kEh9X7c25P+Sc14FqUKRwikUqN26kVUhXA CbYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741988727; x=1742593527; 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=NpHYAFcF1aU/eV7XENF3eCcDJx8xEWz1TnfyQrg98L86kgXYFaQuyzGHlzY+rW4iFq ECYbuKs/M1wpxZssCXYHugfGvp3bOL+pbXbEQM8nnrgAxJXFjEg3ThJVD/0WW2sVmx4N tNAsP585v8sdJSR3IzbHFRWO1AN8KMOptoWW9J+RSjV8y8nXWp9EQpAcqkg+8TXQBWxz qRjxzeN6UI0LyaNNrxsQ+mzkv73mq34dXFjZu60vwmUrCpQp8pZ0EaSG489GDB23sJZ5 oa1lOLx+cdNelx9AUDVLRFAeaZfdE8k1f86foDQYj6w+0M7rsDRG0cn5xHmDrECVX4/4 LkxA== X-Forwarded-Encrypted: i=1; AJvYcCULOEiC1BjN9BspVyaNYHNeVuCQ6TXfNlkULxnZ0cOCPGCnJ8y09fBmXCZkwHCR6AKH6ST6QSeru9IDPA==@lists.infradead.org X-Gm-Message-State: AOJu0Yx34RXpb1YLk9veHYkU91dDHxtMb93qXnHRK84wuFiPdBx/FQ15 O9t5ovGyifARRORZ6IrqlNkTku1Lp+U0smr6/XuoHInYyTC74AOgNM094g4OFUD4Qn9RiKGDF08 s X-Gm-Gg: ASbGncs1G3+Aeqn5lDjoRXRaHatCWycMx0Mr7A9XURce+clP2+0eklKRsNwepk1uOOL pf6betG0anVoLBC1nPfrGvk89knGVGtrXAeQlGrYOgEDY6eg8o1cur9RUi9sdZ7MXRIzX7msKCB 51pYG4dcHLyyR9esS/Buie+n0uXFcCTzgPEmG9MHZpwP37JCp5uwgWlj9YcYwaV13Wa/MTUr75v xDXNJczmJHtKRWnfY9cZH/uoWI6+NxUBjlYRQEa456RNo4a6DWDpxGN8eFgj+EumtQDx9qKm8Qh xKbjedsOsinqmjwOKH+5Wj4NKMZesRE50svsfHS2SX4TOWjn1yiRznfFq6MhA/PYQw== X-Google-Smtp-Source: AGHT+IEHTIgzeGQs95nPlCKytt5z5yea/NHClPHPp1BUNubi4Uv9wCiZoYw+B2CYuOVgi1HkJrteRg== X-Received: by 2002:a17:902:da90:b0:224:10a2:cae7 with SMTP id d9443c01a7336-225e0af9d92mr50201135ad.40.1741988417057; Fri, 14 Mar 2025 14:40:17 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-225c68a6e09sm33368855ad.55.2025.03.14.14.40.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Mar 2025 14:40:16 -0700 (PDT) From: Deepak Gupta Date: Fri, 14 Mar 2025 14:39:38 -0700 Subject: [PATCH v12 19/28] riscv/ptrace: riscv cfi status and state via ptrace and in core files MIME-Version: 1.0 Message-Id: <20250314-v5_user_cfi_series-v12-19-e51202b53138@rivosinc.com> References: <20250314-v5_user_cfi_series-v12-0-e51202b53138@rivosinc.com> In-Reply-To: <20250314-v5_user_cfi_series-v12-0-e51202b53138@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-20250314_144529_027046_E82F7087 X-CRM114-Status: GOOD ( 20.98 ) 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 Fri Mar 14 21:39: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: 14017573 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 D86D5C28B2F for ; Fri, 14 Mar 2025 22:51: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=XuNnn2Edt64zxLl3a9dOVVny7HRGeEGtTuaTmk7M3gY=; b=w0hOgIaTgxrgb/ CdZV3oOABAJqY/NXThCNkGg3TuooULhJ/8H60R4JydnoTL4Nkk+lcBNi3e0VoLDrUXTemGqr+GFSZ GFttdR3CknZ0YSStE9m9j9mS9MX0ffxAICFV2Rck1VCLVbXNNzJ8JQ4O4iQ3jjGGtMC0onDAxMg2W 3JjwuFO4Pk6qnMpZapmfoqk20/V1sDdGIx0LvN34Ub+dk+W0iWtoslsoS52suPQZxNQ/+E4YCHzpD tkJFDdD+08Yxm8/Xf/zHEQH7ya1R6VHCUNPAdjuRKvaVkKTcppZibWtfhYa7/MwrkkUHo+uNhYN0j VDpKLHEJWw0NPAzZvD9Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1ttDrz-0000000FSuF-2JOf; Fri, 14 Mar 2025 22:51:11 +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 1ttClb-0000000FLIf-0zUg for linux-riscv@bombadil.infradead.org; Fri, 14 Mar 2025 21:40:31 +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=Vuh2Ubzjjn4sOmi3QJw6bgfZV88K7WLhUaWOfvtqDdM=; b=GdAcY4jjiBeW071SMQ1YXJ4KRq ZmlzfYWrCWciel5YS3kFDGQ4o628bzPoD/6K6SGtJYIpO1we+rWE9jeRTe8M8nSaD1lMIdVwlnG7Z xBLNEkwBFAWprS5QLQscwtElItq5jTY82MD+OU4ZB/9QWersyW8dDKTkuDwoNcznLm27ulTB3jp0X YbavM6e+l5BmZuMW98e5diaQVWixAKUoHhIVQHccuBMeN44PGUofdjDUk48sYeLU3bNOOoruOif6l ZS1l++XmkcQWx923zNsl+b76zG9g3j3ttEuMwX2+bsFZHc12vjR5aLwpz6ZxCrCOiN8KEIxxXmyPI OPbF/BbA==; 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 1ttClT-00000002xCS-2Kl5 for linux-riscv@lists.infradead.org; Fri, 14 Mar 2025 21:40:26 +0000 Received: by mail-pl1-x634.google.com with SMTP id d9443c01a7336-22359001f1aso66247905ad.3 for ; Fri, 14 Mar 2025 14:40:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741988420; x=1742593220; 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=Vuh2Ubzjjn4sOmi3QJw6bgfZV88K7WLhUaWOfvtqDdM=; b=ZTRlBUfWMJR+J1zEA1KKKMInKwxkHC0UItXjrhWDK9j/2wNOZQQZwNk500KOdONy/g 43aDx0GRY+h1n1/zm+xPbSBZU7LXdlVQ9NqssqJZ9rqRzVfnaZnC54hCXALWdQ892nta z6stFb+tI/fkFGqdOJ1SNiCj5521GXiJ5mpvTgebhbOl292CeNMx1EPgTR+GX53oKdHC pwqNgxiWkj4LBQQ9RWTSwz+c3axXEKmfMtciFdakLe1UUtiX3vYifrzEzpdFD7fNnG30 azWq+Mvp2GkQFe5TDAv9hnQGrUcOwfl10PFPQhQMHvZ2qSMO+kiD3tk7njtlsdWSuwrB BGwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741988420; x=1742593220; 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=Vuh2Ubzjjn4sOmi3QJw6bgfZV88K7WLhUaWOfvtqDdM=; b=ukm+GMhkXit6mvh1vZ21nIe/gZtWMk7vul+nlOluZ1jN13OocGOPyj6lR/d67QSGn+ Mbxa5NEJ3rmg+G1IWqOoY4P+77VrCCb/PaG42pjTbr15y8O/ws3BjyS97TUdlKhyDd1X Jzo2u4/7HCWOTPvG+4c5saeYBsTL0Q+2xr41Rg/2gb5HZufGjLtMzFwz2Ujaa/CdaNnu Q+gbcw0HOCYkh6lNQfclKCwF1xcc1E0CHgkcXsZHsz8zhqntX9bR1HlGGHO+hM6YP5dt aWOveZ78It1b5vgcoThXQuoU9PYiS8Ng3Zs7qCYNX06B8DHUOLxkXP8Ihy85wm0R6EnM Sd+A== X-Forwarded-Encrypted: i=1; AJvYcCVnhCYatbVWO1KT2O82OIeDzBwQTs39ApKuNSy7kUfWv3yEZ+8kmBSU2yHK0OV4plggNdJeREQrps5taw==@lists.infradead.org X-Gm-Message-State: AOJu0YySjfu/O6rWarxRCi7SB9xrgSDpD2qPywFYiyvZsvW9aTbkiX16 XPZ57zbrNEDFwvgwO50P2CsFkgQBDO5+hSxGtRlRDJ7ozApJ0aMMY528AugOKe8= X-Gm-Gg: ASbGnctOauXJ52fhKd/wVbbeJzIH22HsmLpn0M+KnCR/WfPC2RxrB9S47PuBcfoQHso 3vPk7JU4eoil53NCne7EOIw+GtyV0yfLsNLUiuZU5ZFIdkb/8ib4ufWCRnNMAKUv/2DsIyKjAVE cZ+KCtZnGTnmAhPTck/kVxnyA4ZHINsY3GvupX5MYck7ADWjOEP1qaY9oUd+ZOMCXGC0V433nV8 8OjvN6UtcO/2+VHdLJpw5Glftek8NJhwgU8aPgG6Wqojm9A3x8zysyOzMhHtsuMH19rM+bvm9Un +XnFipxzH06YbOocC75GoHghwjKXX0kHrHxcvKFFMTK/0z7TXLGAeYg= X-Google-Smtp-Source: AGHT+IFEy8ShegH7q+9OBJ6uPIx07DqrMw8WFpOVCCYLkkSk1e6aSP/3QYX2LQ2zeOkBSAdDiCkRtw== X-Received: by 2002:a17:902:d50e:b0:215:94eb:adb6 with SMTP id d9443c01a7336-225e0aee9bbmr48850065ad.40.1741988419768; Fri, 14 Mar 2025 14:40:19 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-225c68a6e09sm33368855ad.55.2025.03.14.14.40.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Mar 2025 14:40:19 -0700 (PDT) From: Deepak Gupta Date: Fri, 14 Mar 2025 14:39:39 -0700 Subject: [PATCH v12 20/28] riscv/hwprobe: zicfilp / zicfiss enumeration in hwprobe MIME-Version: 1.0 Message-Id: <20250314-v5_user_cfi_series-v12-20-e51202b53138@rivosinc.com> References: <20250314-v5_user_cfi_series-v12-0-e51202b53138@rivosinc.com> In-Reply-To: <20250314-v5_user_cfi_series-v12-0-e51202b53138@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, Zong Li , Deepak Gupta X-Mailer: b4 0.14.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250314_214024_819900_CFCD1BAB X-CRM114-Status: UNSURE ( 9.10 ) 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 Adding enumeration of zicfilp and zicfiss extensions in hwprobe syscall. Reviewed-by: Zong Li 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 Fri Mar 14 21:39:40 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 14017578 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 AB1CFC35FFB for ; Fri, 14 Mar 2025 22:51:20 +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=8KVkPAbp4TIk5uhdL3pVS13PRt3ZJdo9EOUC3duI/Pk=; b=qskdtvmMq9o1PW YHg8yaVkJIblX2Z4G4HTwr6QxCkVw7mhglbh2hlVpWSFfOvQqlw7WcXLiCfpGoAQ7njzFTOY5vrs+ 9gKBblnLxNtpjA/M4nvP1cl8zGx5vhcKQRy5b9+rmhaBtd0q1e5BpDyb5DItG14xkhPxhGgN1tWIv fvM5915/zKwvod3YwkLy9PkWWEQRY7xRMVAbPjvNHfRa3tpWxTnqzZotHQlQGpsokQ3pwAzoJzT1+ n5JMnioricn0P+Xi6dUP4ARlaSqwTHWI8S/8V3I5S++pQ78SJa91sy7hKbfWsOv1dPlexDeM2g8XC HiGfcxFQpwgLFsl2O+Lg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1ttDs3-0000000FSyd-48hu; Fri, 14 Mar 2025 22:51:16 +0000 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1ttCm5-0000000FLcD-2GAK for linux-riscv@bombadil.infradead.org; Fri, 14 Mar 2025 21:41:01 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Cc:To:In-Reply-To:References:Message-Id :Content-Transfer-Encoding:Content-Type:MIME-Version:Subject:Date:From:Sender :Reply-To:Content-ID:Content-Description; bh=wxaplZUgROh7QEyvXgaGSGBpfUL9KoHoiOWsJ+5Erjg=; b=VjtuFaVwKDkAPkO6WsbMgvP+mn lPF4wQgkeCUN7cJGuzmT8R98/MxfUxVaLGxYjetBrxH9zEeRSxUlmwpRXb7Q0a8/b2ZG8SS+M5pCc EYd7GoZS0t0Xm+3VRoZXF/u9DuvhBzUGt6386PA06kQ3geHeJ1Xy69F3Pbn4mQ8zy0GYiVdt8Sdo1 MpQNngOLN8PJbIf3sSztn2xntWt7gIORy1CmzuQcRIRbHOYwl98LmMzGJaokQyz84uWuXEkYibcL7 83HiqBtphB8eaifIBXhtQKvR8+XuHHCutTx8xCW69z4Mm3zXDk1qTa3PuTEDEbmN8V/vPNjNGPofa 88RpSgAw==; Received: from mail-pl1-f171.google.com ([209.85.214.171]) by casper.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1ttClv-000000073zl-45tD for linux-riscv@lists.infradead.org; Fri, 14 Mar 2025 21:40:59 +0000 Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-22401f4d35aso50707715ad.2 for ; Fri, 14 Mar 2025 14:40:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741988422; x=1742593222; 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=wxaplZUgROh7QEyvXgaGSGBpfUL9KoHoiOWsJ+5Erjg=; b=kJY0ZdLdnSB6hkf82M5tcaaSbAqEuHIzeB+iaysiTyScuiXocTJKO9fMdl306BmO4L ft+ulAfzXOQtDylRjfEXuT9Cm+/qG/IEwrX2KG1r6K4tVq/MYDgynkCO1eb4BI4ic74i UYUirJCQ2cMRxLcB3ExqA6i/OWrbUkzc+52XbcFxjF0K+ku9RQa0Cu19KCtjVXIiUNFI hbCRLN4CyYw2Fb2L3xQso/gO7WHQCR/3OAwoyfsbUFLJzWtZm4UFPuNAqjbqHVpu+F3K GTOxrrG8T7e3xVrRHEzq0PSCCleq5xlo/ooss1CuOSuFIfMkTGbeVXWaNS+yVCQ3dgbv rEsg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741988422; x=1742593222; 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=wxaplZUgROh7QEyvXgaGSGBpfUL9KoHoiOWsJ+5Erjg=; b=UCcb++U0S9TLOEcPlQB9pdPdj+u/rG3bxXEbiFDRM9FXm4zgsfpEWsetQZ0c+32PLj CoGi9BGVRnKXSVEdH644K/JY/9suFEiEGDYHoheDwZNOc/aVGJe4xaB2ZpkSHFBZRq1O w0YOjzQPQM8he0eCFJF17AahDvacqitgh6KKwjrqXXO5XVhNwKG2l1+8EP9xF29um8RT V4V1nvRpYCE833sjcpl0b7ld3eHOJW7WYCfXEiKWXPiH5JKwbNH+VaZLQFyrm8wblZ1c xgPih1RAsiXLio1BZowLFfJPzvli6Z1MOKZ+lFWeQi0iXiUFnqNXWpxmk4iOand6fukU ZyMA== X-Forwarded-Encrypted: i=1; AJvYcCWWKfeyoB7ieZElMMF608uV6vHtKZlB5/12Tic6WxWeEz/bhgh1A2gZ2O+9cFs6x9txjWSZbIAYbf3xOA==@lists.infradead.org X-Gm-Message-State: AOJu0Yx3AChAbbiBY7F4Rgmn1XH9JfVBgHNY29RAVpV40Hx4983Fc8vS 4+9LgTSqkB5VcU7F84bCNYB+Mgy8FX0tEOa0vzXAdXe6cjryPOjVpwvJMWr8ARw= X-Gm-Gg: ASbGncve6bGmsTWVf12XTx0Ant82KXFMxutR8BKTGvycHhEYbUHdYJqSRA/kYGCf1QT pRh1mUr2hQUVW/M+n047mA3J1XKYINAfOZaUH/TR1oBbJ+G3jr0HV3+qYl0zUAlyv5EPlFAqpez ahGgLfyols8Mp7KPauM42z96zUYhJ1AYUOMQf7m6Olxr+g8nquX1kYLbtC/6moSm5xbDyAazGlU UpaDb3M9EkuwZo7LpT09n8BdMyb/cKyG2HP7S9+y98cZ5l8bKVzfFZeJTt8tBmw4nEfDa0rEumH DWpU8FqYCw7Jg8wO7Gevmh17ZbZ1XY6xKN2LsW/CsYD+1kps/UMkW0I= X-Google-Smtp-Source: AGHT+IFnWo7zLhMGwvu4KurXEf7kIkAyH1HMaWOYapOqu0jjJNCjRbXuH7ozNEXPbffeeQud8LolDg== X-Received: by 2002:a17:902:fc84:b0:223:fb3a:8647 with SMTP id d9443c01a7336-225e0aee9c6mr56406685ad.41.1741988422612; Fri, 14 Mar 2025 14:40:22 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-225c68a6e09sm33368855ad.55.2025.03.14.14.40.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Mar 2025 14:40:22 -0700 (PDT) From: Deepak Gupta Date: Fri, 14 Mar 2025 14:39:40 -0700 Subject: [PATCH v12 21/28] riscv: Add Firmware Feature SBI extensions definitions MIME-Version: 1.0 Message-Id: <20250314-v5_user_cfi_series-v12-21-e51202b53138@rivosinc.com> References: <20250314-v5_user_cfi_series-v12-0-e51202b53138@rivosinc.com> In-Reply-To: <20250314-v5_user_cfi_series-v12-0-e51202b53138@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, Zong Li X-Mailer: b4 0.14.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250314_214053_158570_1E67FDE0 X-CRM114-Status: UNSURE ( 9.39 ) X-CRM114-Notice: Please train this message. X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org From: Clément Léger Add necessary SBI definitions to use the FWFT extension. Signed-off-by: Clément Léger Reviewed-by: Zong Li --- arch/riscv/include/asm/sbi.h | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/arch/riscv/include/asm/sbi.h b/arch/riscv/include/asm/sbi.h index 3d250824178b..23bfb254e3f4 100644 --- a/arch/riscv/include/asm/sbi.h +++ b/arch/riscv/include/asm/sbi.h @@ -35,6 +35,7 @@ enum sbi_ext_id { SBI_EXT_DBCN = 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 Fri Mar 14 21:39: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: 14017574 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 D06E8C35FF8 for ; Fri, 14 Mar 2025 22:51:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=HfwZCr1o++6wQX4oCzYvbe49ZtstdNWvKH9+6jGzdyM=; b=gmZxXEBjRd3zqx afTGun7CBXfwi1BQ7scnjFpi56GpMb8sglblFev8iiv2+3LDd1qLlrhLeTm29rSkpGlFWE8D0U71Z +Yk3O3K//0ygLxJqRZyL6ExQ3sdCuCs4nliKoDaoLov3sviIn7dU3ECFAvKNqOh/HDBktTtQPsayG 8saOM7/76eNDRStcYpD1eidN2QtL5A8jP7mE2JrR3/w2TFsbsw25aquPA/zucYbgNha41hYbtgyua x4nd+uvPn1SHxP4s9aFrCWouPMqLXYLahTB9E1Amamrcyyx23Y6tlSgHoZWLYYbLQ3UnoRBsoDqPE 7ttIY0CLC7D4PyCbRF6g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1ttDs0-0000000FSui-0f33; Fri, 14 Mar 2025 22:51:12 +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 1ttClf-0000000FLLu-2BcO for linux-riscv@bombadil.infradead.org; Fri, 14 Mar 2025 21:40:35 +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=E9n3/K4KBqR/53b0jV+O9N/MxE5W9V2LBO7V34JU3SY=; b=bFIyQpcscN3PX3BqfYrFmmHMDv wCjgsxatAq7FsUjrs/crN9jEq8IsSyVrMX3Dku9TzMupBcNQKKxorpvRiBTTXSaMZmD6JaJUDu+cX KhEZJNSXys8KhOJlSLMsWYV0Nt+EziKkiNb4fhOh92+y2Zc/GZ1hMmZgh4JGgzJpNDi+eTqwQYtLP AVYXoyt+5ECpLQU0Y3ubawsKx4cblqANC1NStl7TsE/ql8SnUQE0ObzquI1eflwIvXW5ZGWc3UQtf tllxYVA1+jTqC2hJx+C+YperLyw523jLZFrM3Y9NTWfth86EL9TMSzf4JSfaOxDICXdlPoPQ+wLO7 JRgQ7SFg==; Received: from mail-pl1-f172.google.com ([209.85.214.172]) by desiato.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1ttClY-00000002xET-2S1E for linux-riscv@lists.infradead.org; Fri, 14 Mar 2025 21:40:32 +0000 Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-2241053582dso61493895ad.1 for ; Fri, 14 Mar 2025 14:40:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741988425; x=1742593225; 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=E9n3/K4KBqR/53b0jV+O9N/MxE5W9V2LBO7V34JU3SY=; b=U8ebbZ1XfcVOFw4WnDivAdagE0Iy/K+JUVPNMNNhsUcMAbmMA2P1FG0f6GjYW8VUs5 nqq4RpiBLQH8kQ1oBpK9PVjPTl30mBVFJ5xp72ZA7miDOE9czd4ytLDZuTtzJA8uLfWc qutgjtqf8LV95OEBBcz5CFzTdOAwsx1JBj2bd+9t4eolth0My4QF9uKbV75wWnKKBIKT YivzRno2oxbp5AvKgzHln9UO1hM5pd9wZrjWzPwTGWLLc6KgaUgYQHn6dvHD8mTwYtoS XC2nFkcg9LGqc7WTk70IS6BpCExm+1+64IVAFb5MB2Y8n6wBPk3igXeXPC3r9yBwhkce dEPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741988425; x=1742593225; 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=E9n3/K4KBqR/53b0jV+O9N/MxE5W9V2LBO7V34JU3SY=; b=nVcY9WLyef2TXRdxQrBDzXjculsoETkeErukny7wpOui51ar5TOCPr+2+C0TQ3DDsL /Oa946VSyMJun20Sj1WtLFoqEAWiykfxlop13lHfx7cYBu3kde10sp1b3y13Jw2SDoXo zC2YehNXPnXXlQClw3j4SvgZ2NtV1qjv6UTGqY+JzHIvkGEHg+kY82PRJDrO9EmW0um4 COBpeFBs7OurXPwUmfLybpFH8RhPzp+cx+Jbv4KJLg+rwUorhJUd58WRSy20UTXDE8+X 0AywH4qFdarbpRyq8D5AP2GjMU1V6P6VsnC6TmzLHLHkc/TAoCdN60p9z8X8VXoWg3TR q80A== X-Forwarded-Encrypted: i=1; AJvYcCXOew/hTJQ0X+nkHuUFmKQeYBKN4jlkDL+KoB1/cYzTXwnhzapJVu7OFSn9Z9To+vHWB9Ah8mqbZDpTkA==@lists.infradead.org X-Gm-Message-State: AOJu0Yw0aUkGw3cB2ydz1xOQRXEcUhdyiCt2r+ZOw+uh7NQUgCV6AQie Xg9/S/qcIBK26o9JXcaX6/0EU64jXJ9fAiUzshPtKHRoz/++Ujc9OHkzHJ8jHlo= X-Gm-Gg: ASbGncu76g8SwA9znjx4U+e7dP0kpyksI6zpicooRGDN9Lf9Do6oqQhZXXwaIcYm8g2 FAHq0IaqMiHB1GFnfZy0pj7Ke9KofZ2vJYtk2X7eAxG3rJuVJUtgtV003fsOTjB9MA4CVF+F7px F8AQ/yPCtVRssTJW5/P/wNZb1sPMIu8x6caV20d/XKEKWsYuiC9tnAJvaqHHsHAPFmy2M39UdPX f9uYl+pDSHQ30Ney2fIqpTV86gXgi2y9Z7UBm8dSa2cdphUNFzMa/N46OODd5Xjy/8wXnupCNLC 7lU5tI58Oz39aaE174h5FOZj2ZOsW/gldRQigtPGCpVPMGcR5JlGE7L5sOFwfT8Ysg== X-Google-Smtp-Source: AGHT+IElyciILdjHw/3i0RPFBnjTqOBGsA25cCwm/QkQU1/hQmx1TwMvm2SBws42y5o8HIBP24yMlw== X-Received: by 2002:a17:902:f645:b0:216:3c36:69a7 with SMTP id d9443c01a7336-225e0b4970dmr65622535ad.45.1741988425325; Fri, 14 Mar 2025 14:40:25 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-225c68a6e09sm33368855ad.55.2025.03.14.14.40.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Mar 2025 14:40:24 -0700 (PDT) From: Deepak Gupta Date: Fri, 14 Mar 2025 14:39:41 -0700 Subject: [PATCH v12 22/28] riscv: enable kernel access to shadow stack memory via FWFT sbi call MIME-Version: 1.0 Message-Id: <20250314-v5_user_cfi_series-v12-22-e51202b53138@rivosinc.com> References: <20250314-v5_user_cfi_series-v12-0-e51202b53138@rivosinc.com> In-Reply-To: <20250314-v5_user_cfi_series-v12-0-e51202b53138@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, Zong Li , Deepak Gupta X-Mailer: b4 0.14.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250314_214030_397681_8FD3EC0B X-CRM114-Status: GOOD ( 10.02 ) 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. Reviewed-by: Zong Li 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 Fri Mar 14 21:39:42 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 14017536 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 3955DC282EC for ; Fri, 14 Mar 2025 21:48:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=qpaxnokOhOxyks8Eda34QYj9DiZH8Xim6deFu8zf900=; b=FZhaNTymZPrO2B /P+iPXWNPXXiv0j8/8gXo3kXQ4P7+Rdoad3AxS/ljMAtasH0xA4EXumPK45WSQeEWLVYudeGYOrB7 m8ajcRUMVpoPcz33iGAQv9b3xSCG0y/rCW77BhI/UttkemhQTj0vVNc4q35jfFpmId8ioOW6j0cRu Caw1j0kWMJB4DJTvlmygPOX26B/ilQGGHb8hNgH4UzEakrUWhh2xxPT6RVxMZuhdgyFsrnntrdTwB PtnbnlvFEhuvHmMjHhwkdPQHZOU/VayP0+ySathhLl/qVSpW4R1JHaQX3ehxRwGryhTV3hypWLFgv N/cVHHQsfUOXHkAT+xRA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1ttCso-0000000FNWL-0ElE; Fri, 14 Mar 2025 21:47:58 +0000 Received: from mail-qk1-x734.google.com ([2607:f8b0:4864:20::734]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1ttCsl-0000000FNVE-3LBh for linux-riscv@lists.infradead.org; Fri, 14 Mar 2025 21:47:56 +0000 Received: by mail-qk1-x734.google.com with SMTP id af79cd13be357-7c53b9d66fdso329929485a.3 for ; Fri, 14 Mar 2025 14:47:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741988874; x=1742593674; 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=a7L6jEjphmSCVBHcxl5XAt3Us7qrk/UvaMSfIdPEx6k=; b=Nye1e9DwpitK5Ai3CkcdrcCTmxhPIdMIF5yMOUCaRluhIZmekzQZEfg2QFdOKXmVLL qqrDVEfOqbOAQ7qOxwy+xGSWgAEFvqgaW7GJH4WbRbUjXjYPwFe2qLaACBr5DGmTpYTW Ju+kOKaHZa+KFqhHL1KLrkYBPPWdnUxKL6iQ03rYNVFK1CdSapTZHqo0oV3whIE3iqbg RnOC5b7QBlQ3qDZd5rUKqd9H5lt+AZWu5HBQ0A/4exaSR3kF4hqr6ohSZIEmlvGOGkid J1b3Ep1L6UBO47Z4F3NJRb6UOt3Xk1sIvSmmSZjssJJIsIiPpIWjPMH5i8xrijVkDS/f UaUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741988874; x=1742593674; 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=a7L6jEjphmSCVBHcxl5XAt3Us7qrk/UvaMSfIdPEx6k=; b=C1ZRrgSzNkvWrDm8pCsWYDG3/F1nchJ0m/wZe6GvxChT/J4VDdI5YdNVZrK/gJrKgO EbE66KqL1sN4WGzaI7JmjWcvmwkmSO5SVLrmgtokAuqNekxiDUbBKuRhwlHkozyIwIbj bvK5e7Pob9Bz4kgzDelTqdxuBMghpKpsyVX5YrWdQ1Sqv+S7VQ6oDUljz2NHVPskKO78 LWmrxAQZK2tabNBlNLRjfedrNHMKQzfmUVjMIwFp5PSfab8EOUKAssYNegFc5682x4PH FkcJvHg6JRvKqh/N5ySG5NtIzY+Nm090pi//+kdmAfZmsC6YD/kRyJ7QZ0U4gmE7+VgH d/lg== X-Forwarded-Encrypted: i=1; AJvYcCVR0AWJhsqklbACRoVr199CTDRrIMwzB07qwks2nmICnOFRDaNYE7jnnEB7YNvVgu/brL8Rin0mtS7vnw==@lists.infradead.org X-Gm-Message-State: AOJu0YzcyOeiMPs+Vqnat/hbB2yFXmxk0o4ClVuQE0UpmoLDP1+bKi/6 GH3cXTisTnBa9RXugfOPkwoHjv+rO2rIgYssQOW3Ws31AEWSlQSithSCd8Vk7lw8qYSCBNSWC+G r X-Gm-Gg: ASbGncskemcl1oaXrg8tyrdqTJq4mZLt9krs4FwAU/n9inonve5az621biUGhYnbHkf jwY8V0p892tWLNwJJUSZWYWQAbBWhAhGZ9sptPajgkENs1zgO+GD3Wu8CIBIrcOUqhOr6nNHdSb XgU8UItX6P6+xlZF6+ep/Fi/U244TvGuXiIfHzIXjlvzn/T2g4uVikQtwZdYjcAn7KuMsGpDTdY U4246QVI+QyNa9iU6nN1rQL8wCcCjs+l0zRsXTU1HSf68VIc9vseLsUSMW7j8OPBJlsz9jZgoHb QD3cDmAZsnoNxJ9W24tQZoBfUGlg6HBh2HK2oS8Bq15twsbe6DbCM6M= X-Google-Smtp-Source: AGHT+IHAGPYqUS71chjGuiJ8YNLnk78WQ4QXPzZzrnBV+tP+kBaD6/rfE4E8XKuF5NR7jHoZswbG+g== X-Received: by 2002:a17:902:aa4a:b0:224:1dd5:4878 with SMTP id d9443c01a7336-225e0a62f2emr37059015ad.7.1741988428224; Fri, 14 Mar 2025 14:40:28 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-225c68a6e09sm33368855ad.55.2025.03.14.14.40.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Mar 2025 14:40:27 -0700 (PDT) From: Deepak Gupta Date: Fri, 14 Mar 2025 14:39:42 -0700 Subject: [PATCH v12 23/28] riscv: kernel command line option to opt out of user cfi MIME-Version: 1.0 Message-Id: <20250314-v5_user_cfi_series-v12-23-e51202b53138@rivosinc.com> References: <20250314-v5_user_cfi_series-v12-0-e51202b53138@rivosinc.com> In-Reply-To: <20250314-v5_user_cfi_series-v12-0-e51202b53138@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-20250314_144755_829155_07980074 X-CRM114-Status: GOOD ( 12.96 ) 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 d31d89618763..813162ce4f15 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) @@ -396,6 +398,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; @@ -475,6 +480,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; @@ -507,3 +515,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 Fri Mar 14 21:39: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: 14017577 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 D5F3FC35FF9 for ; Fri, 14 Mar 2025 22:51: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=ZOmPj3ub2efBnwDvg3p/al3xKGW/kes/BxcXi+bbkrg=; b=SqhANXU2ZDr5CL jJkXikDqonfZsekZo58x9FozGSfjE8WcW/zXWIxsDGyrX365vUigI2XJjq8mHrorw+2zaTpygfY5L 4VqfTJRzeCWqKbn9C88od8O91OMNZ9kRe7zuShAe6YdDwpRHEgmIpNT4Wp7weYyhppN7uKnYcqMu7 6o2mjzgBXVTHjltSMaVBNovMqj7ELd2lYSY69wOW6gafQPVui66Npp+8FRY6SfzAJdOhbLvhNwY72 O+nQvIersifMXc67niv/rFnbhegXi9qecNi7EoPL8/cUic43XulNwzYph6a9sw6xAt6yJAvx8T3rV fOOFxhZk36ku8uPUGXFw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1ttDs0-0000000FSvE-3EmN; Fri, 14 Mar 2025 22:51:12 +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 1ttCli-0000000FLNs-23jR for linux-riscv@bombadil.infradead.org; Fri, 14 Mar 2025 21:40:38 +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=m6UfP2exNMdeoKXUR2vjpjSCKDIb3uPINIMtTyZwoe4=; b=R3/qOVp9KaWX59XX0VSMYmS8aa THadMdC1v7CzphVUWBqnE6KjSOtSHnDjk3HmMm9+sAu1PmY4DG1MhstjaooM0Mjlkj30dCYtG7Kxi vyXbRblqI1N4sNODgNZ0Hj0LBMLUV43c3/xcvg3yTlBJzSVg/27X4TavXb1ljNFXGqGLv/xWKNucY I3QNngGewoBWqTlDwtJddyc6ofHwdnaSZXcxF1rvw9LNMkWfjc4zDs9ZiBi+8VQADrFBUMgi97vWt DVdXUqj7l9wkI+MU33vCQG1lxtlUv7FvD3Uh+gyANj0pcgusRC+GAuB/qGCnOCngDiHSdk8WvxipR rstRy6pA==; 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 1ttClc-00000002xHE-2GQs for linux-riscv@lists.infradead.org; Fri, 14 Mar 2025 21:40:35 +0000 Received: by mail-pl1-x62a.google.com with SMTP id d9443c01a7336-2239aa5da08so52115655ad.3 for ; Fri, 14 Mar 2025 14:40:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741988431; x=1742593231; 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=m6UfP2exNMdeoKXUR2vjpjSCKDIb3uPINIMtTyZwoe4=; b=oVP0ejNMZTbh8RIIq7GWYLYDbu8XvK272js0LgKBlL2TqmVTMXkUa4pb76sKas3v2+ QswRBrHvdRkpD3mEm7baY0Djv6yGnUreIMWQfGaMhQXLmc9DdUm2o5bl5kTT9zKn2Bt0 NpWcbiCVQFvFszNA0bZebfpiFzz5odu5LoJsour5b+M3kXwneYXugj8WWsneG/0H076W jR7DOl2idGR8Vrernb4tkw7l84XJdurxChaHQ0hoIuOCgIR9nIegUF03puelkd3PuIum 3A3EC+LMXTZB9sKi4al+P9+ftTQ2lH4TuESTOAQpXHL/Z6YQMwfCIAkht0whUQ09JYIF Ztrg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741988431; x=1742593231; 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=m6UfP2exNMdeoKXUR2vjpjSCKDIb3uPINIMtTyZwoe4=; b=tVC0VC37vMXIX4LFypzmDduP1i2lMIkA9RNmh30MwslHRRek1CQo9lejQS5bzCg2/3 yblk3gyKiFbIzLlEkbNCs/LAvrYISy1pyUa8Sk3WzFD64AzQm/KAuJPYN7VBk+wsJo4C xETGXk7RZtAyGrZ7IDH9mGDnnYAdK4dxl+7MGASwOj8zeWEq2gCgeerk+FUYTdccJNWE mojKnFxuMT7RpwkpF7OhvN+BYyMl8NB8VW0hlTbHl7iqYLDU9YzZu7BpuQ9i5YNII+Hk nh/Bzev7hm4uFcyd/o1BkM6F6Ts/tVRg3u6KzdHkpZYY2+mwPJ8Mcha4P6c6bKhfCp3H bYKg== X-Forwarded-Encrypted: i=1; AJvYcCV6asEMrdOYbdXybKK759S/xl7GibsW8ret2zUShvLT4yGUohnMYs0t/IYShK3VpgkSd5fwejhvVW8m2w==@lists.infradead.org X-Gm-Message-State: AOJu0Yyur6SUTMGINGiQooRYuZXU0+r91DEdys9jeOB78d6mUHmfeh2G j99cvjkr2uW19SQ1V6EIQ5leB63Ik3N6G9e6GaM8HwS5PnK6bf2Exl2ObDp6vyE= X-Gm-Gg: ASbGnctm7oaSymFC6P6wcKHsOz9yR314onxS5nwt85GfszxU9qSpb7c1HuvxKXji2xn gK8QeAUVbYgcprSHiXbIYyTobyRrv//lv/BvRvaJV9ufXDMvdMfn3GBmSToMeOuUxKfsWrUUvrV KJdiRUBWXbn/KkXfiwTBFKjNqHv/QFuwdtO9MHoGWPJCyhXKe7Pm6lv0gq/exNteWbOkpsVx0qr RQIsZF97VKVv1pg9GRSpfxQGqrk6NRJ9fik3WL7j4MarlM997pjlyIJ2OKlT3kCFSbVUGKaxyLG 92brKN9dFBofdXgniAazExmG4ouRImj7XR4PRZcsJ0JQSvZPC8vxAlQ= X-Google-Smtp-Source: AGHT+IGRsLxN2WiToQ98rm3Kyv/RZmRX4iaBavru7/daCtC/YW0IPfGcg+wLCe+5mRJhPUTio+oyeg== X-Received: by 2002:a17:902:e5d2:b0:21f:ba77:c45e with SMTP id d9443c01a7336-225e0b194c2mr57083025ad.45.1741988430991; Fri, 14 Mar 2025 14:40:30 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-225c68a6e09sm33368855ad.55.2025.03.14.14.40.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Mar 2025 14:40:30 -0700 (PDT) From: Deepak Gupta Date: Fri, 14 Mar 2025 14:39:43 -0700 Subject: [PATCH v12 24/28] arch/riscv: compile vdso with landing pad MIME-Version: 1.0 Message-Id: <20250314-v5_user_cfi_series-v12-24-e51202b53138@rivosinc.com> References: <20250314-v5_user_cfi_series-v12-0-e51202b53138@rivosinc.com> In-Reply-To: <20250314-v5_user_cfi_series-v12-0-e51202b53138@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, Zong Li , Deepak Gupta X-Mailer: b4 0.14.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250314_214033_772709_EF3CBE77 X-CRM114-Status: GOOD ( 15.59 ) 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 Reviewed-by: Zong Li Signed-off-by: Deepak Gupta --- arch/riscv/Makefile | 5 +++- 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, 76 insertions(+), 1 deletion(-) diff --git a/arch/riscv/Makefile b/arch/riscv/Makefile index 13fbc0f94238..eca94246cda6 100644 --- a/arch/riscv/Makefile +++ b/arch/riscv/Makefile @@ -88,9 +88,12 @@ 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 +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 Fri Mar 14 21:39: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: 14017572 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 8C2FEC35FF7 for ; Fri, 14 Mar 2025 22:51:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=UR1+LlIrSYvWOuZqUZXBFYk+a2om5cnc4iuoNRvUgpg=; b=cN0+5bALx3hbEI qeStmbcJeFO4TXbSsZs6fxkGHRxvonUIdHJv2bch2Ans7kukRuF6bD8LiVwK6EQvINFCjo24ddFsI i1RqNxI14LPvBYH2V+IG4MfNFdNvwywnO01JJ5fdwdOyITXc9Swnd4aJHNN6fUmgT1hd2T5ugHg/7 3LaIFlVWTTDk1q6eZcJL24s7kRPDjF1xzc/tFeDQ8o5Kw0WmoeVbVvjpMDQ/roXH1GavzMfvGEF0Q HUa47WE/8g4YqrJOzHsK6sOMlriU61eE6Jng83vxGhrlpQ0r+0DokyQIrADgMASUSEm5xKt48tVGd Jq0R7WGaKuBjgPf/GIcA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1ttDs1-0000000FSvc-1Se0; Fri, 14 Mar 2025 22:51:13 +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 1ttCll-0000000FLPg-1YGW for linux-riscv@bombadil.infradead.org; Fri, 14 Mar 2025 21:40:41 +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=RHgO3WLw+YmwdWGhLYAhQ6tJSqW/uFwu3lLubQj4jZ0=; b=rMuMF7XszuKdUiZNe3pn+G7Fll CgvOoyaY693WINQg8NUt4aoV8XfoL5dipxG/811wVZ8Gnc9tMUGlyxftLgIBOXEe/49jnI3FsP+oi qP/WuZQiWkCc9hZamj6AeYyE+E0knzg7coyoZ0QxnnP5OZs16Hnm9D7CWsLIPfJNlkoeIQ9/V2ecL BB6zT0nEJmJ+5lCnb1pnssfLYvZP7iHeG7+XHwMceR276Gx+i/yaedO4LSDowOD/3aHITo/45ZwrZ w/US6mR3lcpQduQlrDFFK2ZvcvI2ZtHdSyIkD8IRHuSybwKAJUHcps9XSHVy6QDO0pxkRjWXjLoaL 6x5VBGHQ==; 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 1ttClh-00000002xIf-0iJE for linux-riscv@lists.infradead.org; Fri, 14 Mar 2025 21:40:39 +0000 Received: by mail-pl1-x632.google.com with SMTP id d9443c01a7336-22349bb8605so56814425ad.0 for ; Fri, 14 Mar 2025 14:40:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741988434; x=1742593234; 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=RHgO3WLw+YmwdWGhLYAhQ6tJSqW/uFwu3lLubQj4jZ0=; b=Kr82odX6gR++vjYic6H2KQyNIRaKUjReB7v8yKmj7raoFI2uNMrbyJCgnHmRZlew6g eniVzlsWvA7uXdqxxfU2N9z6IfPZ50kA8T+BptNFdmMzCBHdehtberSoE41FNTRzMbnt cSNiXD467M+WVLyLxEjBXM8HhKSKgnz0LWd+iahuV10KSCq87TugALNl1MkZKMq+3alo AEwPY/ByXI36bpi9nGuLlTjrcmlnBbgzWvx21AGk9+yAtxI2LQVJWGEPrKh2R8pVCNxJ R8tuMDosW7BjUrYPguOSaiNtFOCpj0dnB2MxtRUrFzvntUtdcaR9qUNKRr0YM2aywmcS Xpeg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741988434; x=1742593234; 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=RHgO3WLw+YmwdWGhLYAhQ6tJSqW/uFwu3lLubQj4jZ0=; b=YjHCKSG46HvkANu8dGwGimBCwYw6Br5fCdhDXAoOxFaWweIwcK+t3FfQ0go1f5jxy8 VnjZBv4+ByZWc5BaQ8PvetZDDTDWxlLe/H7AgbbA+FUr5qXDEAE1xDvTt0QsokM0kdCV 2abi7Be/U0RJ7oITM79NjBUYOvPRB1o3X+X5z8MZk5fjgKmtfNNshmjlluvNMYZzNePd 4KtRMktRV45Yx04IawecD0zI6amjjuztsnh04kXyrRuq0jGrzSWdYaYpz/YE+9Mrp+80 EaeWBsqSMSvRA/coD69HQF9koYrJ9Ao1cCWFjfcrozK8FO46U0+D8I8iK/XOv2dzIIHF XsBw== X-Forwarded-Encrypted: i=1; AJvYcCWfaosIcHJ8ESkMf38EprRrS2RaowwWftGu7FvN6Kn8AAqQnCxRCQNo/cab/qCva81tRXLgRTazabNIJA==@lists.infradead.org X-Gm-Message-State: AOJu0YxpaZRkwg/GU1qX5985VzajQOEqE1+afDz4L+7Zn0rbpzeN4v8X 78BlEkwXCzrKt/kNU0EOSyUoaQ84qSfersReh3zPQsCBVczwrWj+gbyIlSZ38Ag= X-Gm-Gg: ASbGncvrA/hUfDQcE56Oua2oWWXnWtLtRj4RSZjYtWSC7NFDcpMmasLq47s9MLyWevI mnyFskgospckmv7SV47jt3kpcUtVhlXM9SQWE3dXCpBguLkUCzncmM1u/kRJtruxbXtAar9zvZA x1Wb9uw2ymxYL+diHgXvPcP/5pZc002beGZBp7EMpeRjJgl1/+ELGjbJJCY4lN8bdPXXhmzFMeb KlV/crpbp53oCr5kSFS7dZ67bXEwTui/TLo257etAYaJtwoArC1kZfL2JtKAYl8mRSfPmo6KAAA ftDLmccA+G0H2Cc+10uZKqgFtIg1+meNHLj5caBP7CEaX0bIoKf+O3Y= X-Google-Smtp-Source: AGHT+IGwO6FdmJUbA9RsVYM+YTvEHEJUu4luaiuDWiqjNtcj9WvZlSftHA3OvhahsPKrta0EdbsKUA== X-Received: by 2002:a05:6a21:394c:b0:1f5:7d57:8309 with SMTP id adf61e73a8af0-1f5c119438dmr5984307637.21.1741988433739; Fri, 14 Mar 2025 14:40:33 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-225c68a6e09sm33368855ad.55.2025.03.14.14.40.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Mar 2025 14:40:33 -0700 (PDT) From: Deepak Gupta Date: Fri, 14 Mar 2025 14:39:44 -0700 Subject: [PATCH v12 25/28] riscv: create a config for shadow stack and landing pad instr support MIME-Version: 1.0 Message-Id: <20250314-v5_user_cfi_series-v12-25-e51202b53138@rivosinc.com> References: <20250314-v5_user_cfi_series-v12-0-e51202b53138@rivosinc.com> In-Reply-To: <20250314-v5_user_cfi_series-v12-0-e51202b53138@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, Zong Li , Deepak Gupta X-Mailer: b4 0.14.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250314_214037_487192_4F734482 X-CRM114-Status: GOOD ( 10.89 ) 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. Reviewed-by: Zong Li 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 Fri Mar 14 21:39: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: 14017576 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 2C262C35FFA for ; Fri, 14 Mar 2025 22:51:20 +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=mfiKFN3BRLD0mEz9M1sEFlEWkzXDvyB3nEb56tYRKIg=; b=kmDT3wNQHhN0nd 5+i2QV1CylPwh9ygUs03pE4jsMwSBtduFJ5/JVeSc46ThcNHWXdgYzBr7RzFzEp47f4Mo6mCBUOm9 NIP2un+dLLW+sLJJOd28XsL1rUWkPRh+LrwQ+kKvUmWoLPDawe+HPXq3tTh+cJScDhupwEFC9MRlL qyqK//vMF+DB5NvGjvME3jjClur+iKquJYtl3pecsSRvEIzXkETS/WDIaE0IpyT2pwmX3YtErFPkx 0GigN4r7yXs6k3nC+i2jkiDfIkEHI/jHfd0jinZeo+nPOQwtNAnHsxSAWlS21VESVGaU5Hl/cbtiB ZmXtqhOcKYmfaA9p64Gg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1ttDs2-0000000FSwc-2cxt; Fri, 14 Mar 2025 22:51:14 +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 1ttClx-0000000FLWl-2kqJ for linux-riscv@bombadil.infradead.org; Fri, 14 Mar 2025 21:40:53 +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=S58lE55A7IFUYe9923mKAAObQ+TwOi1jWkuglFq4t0Q=; b=Q8TSJy3fxUIGz2ZQNd9+l3tBw/ JOp+ZagXTNpDo3tMu3Y6O5anCeHSjwTShzJUayr4JZE6nfPEghaDnJEz8iVpd//z1PGoXaqy+dNjW HjKRFf71AIQVqOzDugK18ro4S24hfLVFbPADRpW7aX8ag3sQ+g3pJqRihlMcmIlEuXrznnD3hlciy Tzjht0sIeXaaJGOWDmZs5gi5XkQkE2QOkxsnw0YPtePu64MSARC+PFlP7xOiaADcAsnl/pm3Do31v yl014/qr2tV8eNB5KtSdEy7e5lJM+rGiCtySDmddO4iL5CbLVgzJjgbi5sX1Ige19ib2AO5Jbw2LH qfscOQqg==; 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 1ttClh-00000002xJL-3jnJ for linux-riscv@lists.infradead.org; Fri, 14 Mar 2025 21:40:43 +0000 Received: by mail-pl1-x635.google.com with SMTP id d9443c01a7336-2239c066347so54033235ad.2 for ; Fri, 14 Mar 2025 14:40:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741988436; x=1742593236; 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=S58lE55A7IFUYe9923mKAAObQ+TwOi1jWkuglFq4t0Q=; b=mX/PGE3ZfD5DEc/vxXf9Nbl5drEiSKrHB1h24A5roKQ3T21Se853Wr3gYEzGr/ySbu pzZpCkrWhst5Pme4J3B8uCh53g/e5mnaKd0EhtOYGr5mbwryPYcXIYVxQAoQp2YdnIVZ TiExGVCy//ip9PfWHxMIjbjSKgHgzO5YyUOVl0iTsW+KK9zGqv0Afs4MXbDtDnq1U5d8 ioOoqcTLPG1Xiths1j+ecFQORexxkBQDpXOZHbi1AoTsBg/UDbqummRYJtk9RFFSgvqg AED2IXPepdaVeUITTPEIUSOFcsktgplteHzzse7PDPX/brddF3nMg8OMW7l3BEf93yqA WfMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741988436; x=1742593236; 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=S58lE55A7IFUYe9923mKAAObQ+TwOi1jWkuglFq4t0Q=; b=Qsuh2KEBX0sY95erdCkDh9bSMVDLK7UTdkTGj0azMSuNAt0Wa1079NDAmlOePKHzhz W3/jy4DAJx8hzaVv6eihCBqQ13wQ7jGGrgRuDJwUWLrSIRKKYIvyB+3RCVwMIelgiAPH +Up5Y4w4lBi0oMOLL9QDx/TmJBXNNjHH2OcMNt7moXg/b1CpFjiQ6/f5ugCHJZwGAfBC k4cBqIx1dxwXlKJS3siAg3e4XhRl8C0RIa6TCX6CMd1/mdh+UzoGW5LgctBEpTsJoXQU syaai+z9NMHv5pce5Laukr6JtLvFYQgudCJso7e2LU1z1ZYO7f0dvcLGllai14z3LHXK zpnw== X-Forwarded-Encrypted: i=1; AJvYcCVbEVpbSEx5rSe9FwkDSRI1mRJ5Dy6UTZpGfdjqfIt8B95lLK9IKeVu5wiIwSK01wPXrcUc/AGIo/ja4Q==@lists.infradead.org X-Gm-Message-State: AOJu0YyQ5ICfylFPngoL+6W11tjzceAvfiza5POgBdde4VOp4lDQo5U+ EXicgAsXVkgIZnO+TL/9rwYYjJrmkYZbOSCl1SXyAWKS/lsJyrYJiMjZPk+tH28= X-Gm-Gg: ASbGnculNXiPaxoW5wxAgcSYkItWhVculaTec+pjMnVnseSK5TXGE9W5fJTFnyxIRe5 kf96WSNSN22yA4uBKGuax8gzwphQb7m/+6GS+g4G2mqwR8fQ/q707NJHxmYYgRQvMAxXuZtgcZR 3rd5ugQnPbAM/IWEibcs3j7V0MzgGTcldVKIREGf2VdNuP780z+lpEPdQuPZC27lIqyjmrb7qdR vHYI9WwD+O1AOxlbYC0XVfzINVmt1nGKC8BF/KqdNSg1W+ZAtQ5UItXobgDFq1wPiWgTdUYin2L 2HJSRq+MWASclSEblZLaOWv88fFlG4XJ8NRSO+lDexniJTx/OS9w0PF51XC/dUF9+A== X-Google-Smtp-Source: AGHT+IHxioJjWTqWYTnSKNhIGhNEZD+Fw6H3+v9MmF1O2GdMnurdCyxzj4vsKuZgW7kLmsjMSOe+qA== X-Received: by 2002:a17:902:da90:b0:224:10a2:cae7 with SMTP id d9443c01a7336-225e0af9d92mr50203725ad.40.1741988436457; Fri, 14 Mar 2025 14:40:36 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-225c68a6e09sm33368855ad.55.2025.03.14.14.40.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Mar 2025 14:40:36 -0700 (PDT) From: Deepak Gupta Date: Fri, 14 Mar 2025 14:39:45 -0700 Subject: [PATCH v12 26/28] riscv: Documentation for landing pad / indirect branch tracking MIME-Version: 1.0 Message-Id: <20250314-v5_user_cfi_series-v12-26-e51202b53138@rivosinc.com> References: <20250314-v5_user_cfi_series-v12-0-e51202b53138@rivosinc.com> In-Reply-To: <20250314-v5_user_cfi_series-v12-0-e51202b53138@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, Zong Li , Deepak Gupta X-Mailer: b4 0.14.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250314_214038_194103_074E6263 X-CRM114-Status: GOOD ( 18.96 ) 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. Reviewed-by: Zong Li 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 Fri Mar 14 21:39: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: 14017575 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 C1B71C282EC for ; Fri, 14 Mar 2025 22:51: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=UQAS0rQc+7+LbLP80ouiq9HCyR+CUsPJRwsiKGZpUEs=; b=yCYZKu6anLTXAg 0fMRuxb5KoShhTNa+U+hDIIcGJZy3Ti5CxVyebr8twCdqGBidLLdWAmwvHi0EjRw/ybWjTOITk7B8 N+kjZcCbbplilejCifX7IOKJDo5PlrLgAFyWrGh2WLeZbBZXnkcZUUCAHhvHNvvuU6Y/qIOaMLeMK TTi0EHIrl6Mwlrouecunopc/6VF9XmrjaDKp/+07ru/Zc6008UAARh/FyD67J/mKrcTA6D5iHuX99 KsPPgrQWPPmvyhLutJpb/9xnf7VNRBd4F/MhtmsVxu3V/sZ680dp+Sykzei1wZnCEYpX0xFiBbQ3L dD27LjE3TQdY4lqgrgjw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1ttDs1-0000000FSwE-3z7r; Fri, 14 Mar 2025 22:51:13 +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 1ttClt-0000000FLU7-1HcQ for linux-riscv@bombadil.infradead.org; Fri, 14 Mar 2025 21:40:49 +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=ZcP/paQnVT7Rdltm9MHSxDkYfx/z6vUAqK1vSSq+KwM=; b=fUy5RXodUa8LzkY5qCCM8ytiTE uGNtiepjDL3Qh6xSLzkN2ZYX+ISzjzw59XnqBcLFmaNFxIubxLkfV/ApbdqLP/ICGKN1bZ+nrh4wq /FReN89KDSa0po5lB0QSCXb0rU6hVM3GO6j3NCUnLL6fETWlf47311QUP8gJtq6NuYleWdebqo34m 6WXlsGj4doY2leZdKTUOJyUj9goS5rhQMiS9FqxmhQl/MzVkuf5aoePYjRMjafFuGP33ASE8ZAEDS ygC6rxAS1Yvj2wUFakURS2/Gjwyh0UezC5ACH2+SBilbXVRoBu7T31jrLtvD9hdVhdueEe8IdrfP8 H6+aVosw==; Received: from mail-pl1-f180.google.com ([209.85.214.180]) by desiato.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1ttCll-00000002xNO-0Ddb for linux-riscv@lists.infradead.org; Fri, 14 Mar 2025 21:40:46 +0000 Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-2241053582dso61497205ad.1 for ; Fri, 14 Mar 2025 14:40:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741988439; x=1742593239; 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=ZcP/paQnVT7Rdltm9MHSxDkYfx/z6vUAqK1vSSq+KwM=; b=lwFAhGxkQ+CgPq02I1Z5I2FrWJA15zQJuTZZ7ycRR9KJQgp2VWxwBw3iiRo4TD2vu4 TO5Ky5xjUyiMq3/EsuqrSBXQGRaO+DllVET/7phWO9TCj7kdAYoo77OSe52VsnBKqTmA eUV1ysd94A5sqj3qCHYkDeXy2NDXf++5WaMH445Dsmh90YYqkvP98VJKSlILkMOn9F3u ehbDAoA1KcONY82DJR0SEmvjoaU/XhplThHbYY5dVJWXM2A/j9zVony1eUoAG2jw9IZ0 YgIacGd1oY85guaf7SkF7sv3PGz690lKyTzFXXYnDNk8J90Khy6Ueyo/8j6xXgDoA1AF 0AqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741988439; x=1742593239; 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=ZcP/paQnVT7Rdltm9MHSxDkYfx/z6vUAqK1vSSq+KwM=; b=rLwYZGJTSqPjTij8z7UhN0hIVABpK4aDGGzuRaTCSyQkYl5kQZ7Tm9pKzsX9FIiUoG 27khnN7H5AIkenJazCDr9HJSBD8T5gh0RFIVJc1qFm2wEcjLfHrDyPnhNZ7pYSJX6zhY pZDF3Ivp3JkZOgD9BWYgNTHbOeyUyv3uWvdUwSUGjShZ88zdLnpgOgGwv7yPaZsdtWQW hqWR7dWkBsuKNwAKpuEKdRDs6/b7nl3JSzAi+tUUnFsnTd1g/39eCVM6dlsrIqheH/wT 7fstbqAK6P9xgy9Izhuc60omKb3xO3w/plB6EUino0FXpV2FYu8pUpGQ31kUD66r4sE4 VRuw== X-Forwarded-Encrypted: i=1; AJvYcCXgorINq7suzYXmy3M6DT8lc9EI8w7+XBZ4X/YyzAMov+fLK5fPIEmiyzMDYeAApUCfva7raMS0objy6w==@lists.infradead.org X-Gm-Message-State: AOJu0YxU0V0ldejK/UN48eN+/5nfFw0t5yEPk0bpT2DUeSiolCLa3SvM q25JdfZHBH3nrPygHxgZSnxSVHU2OntNMYy+deB2isVqD6HXHhJD/11bU/P8wWA= X-Gm-Gg: ASbGncsjDS5UjKchTGIG1UJK/Rw2ou51jyD5cneGb8wtcZMpWGuIzkBhFNLAFMDIA0U EBa8DybSSpaKGhwEOmHPsXDmqgiU2Eh7VPWrQaQw10O8Qv57J5VP/VXhxXibZ82cAnNFJ4GZeMR Cy51OwQ2bSe1DgIM4lAdZzXUp4MOlv8Vq7oIYqLo53NYfVsU1uwJOpPDYMRBWax4vZSaymwq5K0 7maHmZlVKLrulJ+HnyfaFwCJer52GJXjfY5mnFAwHWk9D42YlLR0sabcmp4z2p95uwAWHTaut+7 Hx6KI+lvwVHGBsqTa5VIestgI7TgWKTEQGqwSkWSUp5PF38kKGENrYw= X-Google-Smtp-Source: AGHT+IFAr9w0NnfJekHlQM0Z291vOiYWAmpXp0eFWmQKfncujb2meKT0mbAImpm+WZ61lNPTT6Tc/g== X-Received: by 2002:a17:902:f70c:b0:224:191d:8a79 with SMTP id d9443c01a7336-225e0ac31aamr56778535ad.27.1741988439190; Fri, 14 Mar 2025 14:40:39 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-225c68a6e09sm33368855ad.55.2025.03.14.14.40.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Mar 2025 14:40:38 -0700 (PDT) From: Deepak Gupta Date: Fri, 14 Mar 2025 14:39:46 -0700 Subject: [PATCH v12 27/28] riscv: Documentation for shadow stack on riscv MIME-Version: 1.0 Message-Id: <20250314-v5_user_cfi_series-v12-27-e51202b53138@rivosinc.com> References: <20250314-v5_user_cfi_series-v12-0-e51202b53138@rivosinc.com> In-Reply-To: <20250314-v5_user_cfi_series-v12-0-e51202b53138@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, Zong Li , Deepak Gupta X-Mailer: b4 0.14.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250314_214043_580673_88CC531A X-CRM114-Status: GOOD ( 23.41 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org Adding documentation on shadow stack for user mode on riscv and kernel interfaces exposed so that user tasks can enable it. Reviewed-by: Zong Li 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 Fri Mar 14 21:39: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: 14017581 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 3B0BFC35FF6 for ; Fri, 14 Mar 2025 22:51:23 +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=gTq7DbFcCfFEhlPFEWDq0MAm39ymMGY+VCccE6+4q14=; b=zLtjpiQNpYpYiV tlmm+nmJASbZD/FLUiuDyixPnRM1+2yzT76bDFQmQlkg3OD7SXQzgCS7Id0U0Dvw489kr/yGf3fOC npuMidIo9kzSU2ulsQgmrN4e9EROa2eHFDA3rJkpFUzoyHucLpgzBoRHuSNs9Jwtj2XR8iO6fFXed 1CcNMqjZ/tHIxCxD/fCNC/TxhhbaHXN88kVbdz7ZWb0+VLcEs0VSt/iad/SpZChXtMH/GkVjhmXXF EWlpHcugBwpEBCZ1zVanyBMc0R/BgKsoOhutlgPOS4Xj2+RTrhBhqxVsBcs4AmlYwqpnbTUhs2Yf+ jvQxXVRujKMuj5Y9BiAA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1ttDs5-0000000FT0F-1vLA; Fri, 14 Mar 2025 22:51:17 +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 1ttCrQ-0000000FN2F-3mPM for linux-riscv@bombadil.infradead.org; Fri, 14 Mar 2025 21:46:33 +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=RmfcaIG2+ZxQBL2rW8bdYIb6OkoBiSfoVoV6JRsGlDE=; b=UsNm8NWmyEHNbhpJ8cQj2Z4NzK 59tSpQSVDiPBG6Ldw2phvSeTtBifYl+VXHgcNNkSrgeMdHa1yvLd9RGobO3oWFj232r8+N5LkfifV IAcdrY/c+KUKCFInlKas8c+c8XPURnuAGh+69tibbEAVGlq8CFvRBrQsIQoGNGA3xH9nxHyeFjlvL UmeWmWLuVW/87XZSSOeU2QsYPUpRO6/XPYJS6cdDX3z708hBI3wrjjB3Jl0gu0+hd9r35nJ4uXT75 aY4TW7p1z3hUKGRqmYRJYrVakYWNSFz+OgfgrvH2zzTvywNPL6QNW4FASLkORd1Qjh2gsES9EGNs2 2Oj9o2nQ==; Received: from mail-pl1-x631.google.com ([2607:f8b0:4864:20::631]) by desiato.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1ttCrM-00000002yCA-2PpW for linux-riscv@lists.infradead.org; Fri, 14 Mar 2025 21:46:31 +0000 Received: by mail-pl1-x631.google.com with SMTP id d9443c01a7336-22548a28d0cso74412005ad.3 for ; Fri, 14 Mar 2025 14:46:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741988786; x=1742593586; 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=RmfcaIG2+ZxQBL2rW8bdYIb6OkoBiSfoVoV6JRsGlDE=; b=zfP9vJ0DQME0e6TOmBSHFWZGsC26jYvqVVVOHhDU3pt/bIIoNp89ft0tBD1iAVoggl gK2V2p9T1Lld/RX6lR9gkgMWUOrAc+0zY/NKRF9Sqb75iZg1MOOOZ78k9AceVWqIm0kU RqkAU6AitMIc1zIRwYM/xXMGZxwO5BceTC5KnYWvaL7ZW11CL6F2iWSHTYprpSPqw6XN Slv3YmuAk9hEcxISGwhLpfB1dW8B827Mssu8isYuB5sD78085U7DsjrkcinmWvZHFDsq 5S7qFXc5n0kUiKxFI8C8Zw4y0UaM9rYj5Ja1nkhSOfxkHDGvF2t+GwvLjT0mI0Sc9xyB R8Rg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741988786; x=1742593586; 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=RmfcaIG2+ZxQBL2rW8bdYIb6OkoBiSfoVoV6JRsGlDE=; b=v+ryIOjiLURx4vaEXGs6CXOO63AN2zztRv99Seu4i4oI73HobZ8jbXV+P0yEsx9cS2 gPiA3j/m/2eoQE2iGW1t0QuvRYOElz0tYslagxZczTmyTiMuYKiL1jeXNEZL5H0XOsDA xl+Mz9+doULO1Y9+ASUMBg4ZW4+ANNeG1bzw4JdGIc/FMamt6kn/2okCd2PpJsTjEMeu RXKccMKcfZVaZjAmTdlLiV/awK/xPFgJI5bm12kuc/5mRoCiX3VhAY7rWMwxBTYACmd1 NMtwPEoOLBmgL3VD5Cdns0ysz6qtbyeispi7Om8PZhPmFqBrOVbJ351LoZi6d5r2IHAv 8W/A== X-Forwarded-Encrypted: i=1; AJvYcCXiRSnTHT3bKeRRl29trgaTU7YQ6i/qMTzX6TzPbVFfQIJhPRrBFQ4UiZJzAZ+286QTD7qXNXRkP0Ml9g==@lists.infradead.org X-Gm-Message-State: AOJu0YwEMkKAqMbhkzfBiptugQt5uzOYYc097NbppniM3bOrG/tzYyDa 2q+mnIR5NdPRwSlS2c/MN2Yzu4giRnMCI8Lejqrrj7GWDNdBGfCsLeSZCkFMvKB8cnwxNm5HKUH P X-Gm-Gg: ASbGncsOI/oQMfg6vQazGbG94B2gIQUmAqpggOGJGcofiqBb9wYt5mx6g3y+CZD+69H KyQrrTU45p1tqStP0nNN8q6AEnZv5/2GHaqXmhIMI7p7FTA6th6Gqm39jPsGjnfgAnUReQXFvXS Chc99ZMX0I2NfsZHq4l/IH3I3apBkR6ixAKUiscqwbkl+S+KN2BcCfNEkMBGrYdjN6MIraSqh4N q71JdDSn79nfxGf4sTMcPQTMH419y4gxhZ3ja9TIquZ2wXXcPipPyiZ0ggRg9eGdjBnbP5/SeHC FPP0KXlGuI4QKpvyOv/DvJumkuCRv7jOecL/p3IIOtmYxeFzbC2YfnU= X-Google-Smtp-Source: AGHT+IE/Z8vCidWa22GabdcVyMtBUcBNFMuQ7kyBxtmbMBnahHWy+xRZHQk35AiDOY5DS3SuGw5AFw== X-Received: by 2002:a17:903:19ed:b0:224:26f5:9c1e with SMTP id d9443c01a7336-225e0a15b4dmr54069005ad.2.1741988441930; Fri, 14 Mar 2025 14:40:41 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-225c68a6e09sm33368855ad.55.2025.03.14.14.40.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Mar 2025 14:40:41 -0700 (PDT) From: Deepak Gupta Date: Fri, 14 Mar 2025 14:39:47 -0700 Subject: [PATCH v12 28/28] kselftest/riscv: kselftest for user mode cfi MIME-Version: 1.0 Message-Id: <20250314-v5_user_cfi_series-v12-28-e51202b53138@rivosinc.com> References: <20250314-v5_user_cfi_series-v12-0-e51202b53138@rivosinc.com> In-Reply-To: <20250314-v5_user_cfi_series-v12-0-e51202b53138@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-20250314_214628_846423_88D1C9E1 X-CRM114-Status: GOOD ( 23.11 ) 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 | 82 +++++ tools/testing/selftests/riscv/cfi/riscv_cfi_test.c | 78 +++++ tools/testing/selftests/riscv/cfi/shadowstack.c | 385 +++++++++++++++++++++ tools/testing/selftests/riscv/cfi/shadowstack.h | 27 ++ 7 files changed, 586 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..1c8043f2b778 --- /dev/null +++ b/tools/testing/selftests/riscv/cfi/cfi_rv_test.h @@ -0,0 +1,82 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#ifndef SELFTEST_RISCV_CFI_H +#define SELFTEST_RISCV_CFI_H +#include +#include +#include "shadowstack.h" + +#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..53387dbd9cf5 --- /dev/null +++ b/tools/testing/selftests/riscv/cfi/shadowstack.c @@ -0,0 +1,385 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include "../../kselftest.h" +#include +#include +#include +#include +#include +#include "shadowstack.h" +#include "cfi_rv_test.h" + +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) + +/* 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 < RISCV_SHADOW_STACK_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..0be510167de3 --- /dev/null +++ b/tools/testing/selftests/riscv/cfi/shadowstack.h @@ -0,0 +1,27 @@ +/* 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); + +int execute_shadow_stack_tests(void); + +#endif