From patchwork Tue Oct 8 22:36:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13827082 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 69AC6CF0438 for ; Tue, 8 Oct 2024 22:37:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A9C2F6B0085; Tue, 8 Oct 2024 18:37:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A4C126B0088; Tue, 8 Oct 2024 18:37:53 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8C58D6B0089; Tue, 8 Oct 2024 18:37:53 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 726FF6B0085 for ; Tue, 8 Oct 2024 18:37:53 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 2B933C0C75 for ; Tue, 8 Oct 2024 22:37:51 +0000 (UTC) X-FDA: 82651898784.20.365A3AE Received: from mail-pf1-f173.google.com (mail-pf1-f173.google.com [209.85.210.173]) by imf15.hostedemail.com (Postfix) with ESMTP id 142F6A0016 for ; Tue, 8 Oct 2024 22:37:50 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=gnzzsIfV; spf=pass (imf15.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.173 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728426935; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=pcb1JUl7nxyzCR2L+K51gN2ROJJ8XrFInAPZeivTlr8=; b=Kc/ofQpHBvNn6iodVPZ70HkVPEEDMvHs1G6HFim2np2m4Xiu5lz5zq9S+8ojJ/g+zKI4ga bmmFvO2zyOpZg5cYVnVAZkSuzVSqitZJo0AdIsjoVzdSdrtPN/dRjrhE3H/cHCMhKIdXMt D3ieEnlh3mSxQqzQ7tj7SvnkycEnI/M= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728426935; a=rsa-sha256; cv=none; b=RxvvGSSCGLdx4TU+Y6VKcP64Spgvj+AIWUQd8oH7+hWoQz1qSrEl+OLeF4E86DKIaz4k4F f3kfpomgXH24sv+3mOkd8XTzbp/yfSjol9BDrygh/hbtEBU7Ha7b+3tBsR/16NZ194tCdF HysBWusE/YHlpA8nZ2WFD1VYuPBk3Aw= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=gnzzsIfV; spf=pass (imf15.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.173 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pf1-f173.google.com with SMTP id d2e1a72fcca58-71dfc250001so2440213b3a.2 for ; Tue, 08 Oct 2024 15:37:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1728427070; x=1729031870; darn=kvack.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=pcb1JUl7nxyzCR2L+K51gN2ROJJ8XrFInAPZeivTlr8=; b=gnzzsIfVWtyarQ+EH9jEPm8VUoG/QM+N9XrTNVwxdRLnOjTpmIQECrSTZmtCgcRva0 cqUPWByNytKApdj/a9ZWvLG+HRlcGeiqSC0UYZHdPsJ/SmqwVWjQSIHJ71PEjNN7IFv+ bHG3gVlEbfQBpGJHCPfwj+5Vf7ef/Pc277ZFJu86onUv4oDt5zLjggAIljmndc694yOI YXMFh56nfcth8H4gfMtxbiCLCJg1GR2OL0ETOD1lfcGF2/8iBfRfjfL2QqjJxG+XO+Z3 gsfSCCXsE0OgVf0mBiXGKscEMfJprCR/zUDbJfA1lxmZNngrhhCHsl5dIcvgqZA7NmSW VArg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728427070; x=1729031870; 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=pcb1JUl7nxyzCR2L+K51gN2ROJJ8XrFInAPZeivTlr8=; b=l93TI5Ywp5O1mOst+8yY9eWyOEiVnNHTEWgBQFUQ/FNsV634Qipl1SXJM3SGjGw9bF pGNzpu2HOgwaj4hEhYWJqhYiDmKuNlaZDhjPaoxhqDcsKvnkK6U5x3qlKrbbCzIsUPXr Xl0oTAeThlOqu3ybh70mipuICl0M8He4ECYDdmDIJeuHIIhHAqaxyCV9bigRMr3dinyd N7Vb8PNeWb+iBfNknyUPafaUyDuhzd5NHUiWEdvn/+WWuIlgoXvDyCkyh3MNm0+rmv7v 8Y/BaXxgn+S7MAjcFJLl7fvHmwcABzGPtMfLt70DZRcNjTPevs82JWbtlbILlxyW2WKX pz9w== X-Forwarded-Encrypted: i=1; AJvYcCWVucbjRcelnI3YwmH+HYOWvKF+cQBoWUk7+VS4i32TubD16neHl2AnnX+eX68AoehAQsXayuDsvw==@kvack.org X-Gm-Message-State: AOJu0Yxz+T7PMe6aWLLxtweAf+tirdqCTMpeoI5W0rEZhvyaulJ2Fh0J 1S1hSxWenEZUu+5e7eMEfwD7WrDh0RXl2bpSLOJUNuagu2O4yRqrBfTZ6NHuRxc= X-Google-Smtp-Source: AGHT+IE5CiqwVffKftT0HA19xNjp1IZD35v50IRAA/ePwNS8IKwtnn3GofL/f9wWS1jXg+8c2GVgUA== X-Received: by 2002:a05:6a00:a8c:b0:71e:210:be12 with SMTP id d2e1a72fcca58-71e1dbb87bdmr617949b3a.21.1728427069795; Tue, 08 Oct 2024 15:37:49 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0ccc4b2sm6591270b3a.45.2024.10.08.15.37.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2024 15:37:49 -0700 (PDT) From: Deepak Gupta Date: Tue, 08 Oct 2024 15:36:43 -0700 Subject: [PATCH v6 01/33] mm: Introduce ARCH_HAS_USER_SHADOW_STACK MIME-Version: 1.0 Message-Id: <20241008-v5_user_cfi_series-v6-1-60d9fe073f37@rivosinc.com> References: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@rivosinc.com> In-Reply-To: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@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 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 , David Hildenbrand , Carlos Bilbao X-Mailer: b4 0.14.0 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 142F6A0016 X-Stat-Signature: qcytpt34zpaibit4fhsa6e74ktsdtj61 X-HE-Tag: 1728427070-128903 X-HE-Meta: U2FsdGVkX19Gtn7+fb8Ut3fAoEeKmUy9AyzF84n38nVySxnJcYdJGG0Tf+cJ9DSpkqawc0sODr5jkX+KgsGD2Qodt4s12lTLJ6APqodTgz3q4sOgm1VEuRGRL3/ZURiLAGCEE0x+hf+obVoY/ye5+9l+eFUBWsR59qPFJSwK3xpODzUaypFwZ9A17zV/OagPaMqJmqrSXL/IMI6iBWa1KS4TZRuyy+c8//jNU9JBvqa/4NOgNDGa44aBXmhhDAeewafsIhzf8roG1G0dTSNVM5SrLgM+/ZiN+91pUMfysK1ESZplOQLBdHSaInLfO+/Zqouud/OiIpz6jGeXMJbIqYZmT11U8dJvvvlPovCjqu3cQ4Actw+QygA9zBPn3uBZhbrPzDpiScF4ZhUT4nqDvFVchu+wiWB9v0NCIwkiUMkH9IYrd572rUVa3S4ERFS99vmE3gongU9DAGLo9TbCzhQLuO4QmWP60sJKNUfKeFRK4gOmAfNMnZvRYVF1H1P4WR9wc4LpIyQ7wEBXuC89gTvd7cuBuUqB3oeCUDhMiW7jmyanrF82wOkcmDsYM9Kc9KekPd8foSCqCwzS5eqrXhpZ3SCKm2/0fpHky2YTzc9XdgQ9SKlO6BPf+ZGRo30yY9H4IZxAJEv9PpgTCGgzDxDRi1WOxsTDEGkgA3Llfi9nJCEE9OwrUNx5DnjceT8toGKNDTTv3ZZeMaM2s8MZZnSogpcDLoQvE9mSfemL4O2i4ySVwlOaSzd6JQtaVe9o5bNRsJ81qRXq+ylH05Mqwk71Gq77xNZHHGHc8qgj3NaDSs+Khs9M1ob/RNUuYX8btJEev4t1gfYUSVTd7TYnzIlbyQGSx1Xg6Os641U+HR2rxIqkRJwEza81Sos0y2aU9k3ItLnAOM/YzOFy6Ay/8sI4zPYjRzKbZ3CLCvmmZ6+ffZKKUABxJ3oGpFoMoei+d727Nu4N92Onj+EAHu8 9GYUKiVv Kyh8Csd2kdp/LYOsXPwpJyI8sM+DtdQiLH0yoGRva5lVSfJYcuHWOji4iGNJoTUxeM+Hvaj8oPan4JySfngLTMBcS/Yjw0XOFYso9vOfmK9ZRuH8lJFhUNL21zNiNfKoFivXVQhYT74OX3RZsSgObhm4Z1x0t82KBdf2/JLCJgoeeeCCFxunnIvKP088oIfBhkSPXIsAmS9pppbqKUSwxjw/EAkO6r8dH3Li6bGMeIXQVt4BfiF8t1gHLkvNox21gX5TqPR5S55GX7dvk2CNol/Bqwd1lbZ3mom1j96zfXmdtNXKnCNT9Zpn1CXNj7CK3BWwtGGWMxLohcDaBrX+VxJ5GP+YkNQc/iLU7XhqDSjN5FyxCb5ytOoBPH3jIxs0qmPv8FQmC9BlNryL8BSekk5czwbFozbeYX9Np478Py5fx+CkiCfCvwRVN2lbLJofzrQ75IIFtnKQVGCk/QczRK8V8Roeh4eyPMispXy1AmPuwanpzvlUfD3GL2Ub9HGsSBZnpKsmhGfdZlxxkA+6jHZfNe4M0mbzjSp9R+h1rcqvqlPb4/kWE1l/HTUcfu03zR8/j/0MM4JQ08Pa6/uVOyZ2IcoUhKYu57tZVkmmeUVnLQNSlM5Lf1v77Dpf2anE3+J0exI7FioQkHER/URDA+HeZsLAovZd3qXH5IhZj7awSYf1ljOoIYAfQDZ45kvuMc211pb4YRIMz2MDIuOuVIpwW6A== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Mark Brown Since multiple architectures have support for shadow stacks and we need to select support for this feature in several places in the generic code provide a generic config option that the architectures can select. Suggested-by: David Hildenbrand Acked-by: David Hildenbrand Signed-off-by: Mark Brown Reviewed-by: Rick Edgecombe Reviewed-by: Deepak Gupta Reviewed-by: Carlos Bilbao --- arch/x86/Kconfig | 1 + fs/proc/task_mmu.c | 2 +- include/linux/mm.h | 2 +- mm/Kconfig | 6 ++++++ 4 files changed, 9 insertions(+), 2 deletions(-) diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 2852fcd82cbd..8ccae77d40f7 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -1954,6 +1954,7 @@ config X86_USER_SHADOW_STACK depends on AS_WRUSS depends on X86_64 select ARCH_USES_HIGH_VMA_FLAGS + select ARCH_HAS_USER_SHADOW_STACK select X86_CET help Shadow stack protection is a hardware feature that detects function diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c index 72f14fd59c2d..23f875e78eae 100644 --- a/fs/proc/task_mmu.c +++ b/fs/proc/task_mmu.c @@ -971,7 +971,7 @@ static void show_smap_vma_flags(struct seq_file *m, struct vm_area_struct *vma) #ifdef CONFIG_HAVE_ARCH_USERFAULTFD_MINOR [ilog2(VM_UFFD_MINOR)] = "ui", #endif /* CONFIG_HAVE_ARCH_USERFAULTFD_MINOR */ -#ifdef CONFIG_X86_USER_SHADOW_STACK +#ifdef CONFIG_ARCH_HAS_USER_SHADOW_STACK [ilog2(VM_SHADOW_STACK)] = "ss", #endif #if defined(CONFIG_64BIT) || defined(CONFIG_PPC32) diff --git a/include/linux/mm.h b/include/linux/mm.h index ecf63d2b0582..57533b9cae95 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -354,7 +354,7 @@ extern unsigned int kobjsize(const void *objp); #endif #endif /* CONFIG_ARCH_HAS_PKEYS */ -#ifdef CONFIG_X86_USER_SHADOW_STACK +#ifdef CONFIG_ARCH_HAS_USER_SHADOW_STACK /* * VM_SHADOW_STACK should not be set with VM_SHARED because of lack of * support core mm. diff --git a/mm/Kconfig b/mm/Kconfig index 4c9f5ea13271..4b2a1ef9a161 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -1296,6 +1296,12 @@ config NUMA_EMU into virtual nodes when booted with "numa=fake=N", where N is the number of nodes. This is only useful for debugging. +config ARCH_HAS_USER_SHADOW_STACK + bool + help + The architecture has hardware support for userspace shadow call + stacks (eg, x86 CET, arm64 GCS or RISC-V Zicfiss). + source "mm/damon/Kconfig" endmenu From patchwork Tue Oct 8 22:36:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13827083 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 05CB8CF042C for ; Tue, 8 Oct 2024 22:37:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6A5346B0089; Tue, 8 Oct 2024 18:37:56 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 62C7A6B008C; Tue, 8 Oct 2024 18:37:56 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 431696B0092; Tue, 8 Oct 2024 18:37:56 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 23BAF6B0089 for ; Tue, 8 Oct 2024 18:37:56 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 0E8CA1C68FB for ; Tue, 8 Oct 2024 22:37:54 +0000 (UTC) X-FDA: 82651898910.26.CFA50E0 Received: from mail-pf1-f169.google.com (mail-pf1-f169.google.com [209.85.210.169]) by imf27.hostedemail.com (Postfix) with ESMTP id CDED340010 for ; Tue, 8 Oct 2024 22:37:53 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=MUzvxPKd; spf=pass (imf27.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.169 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728426938; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=OwXMBbOWCv06xRC7kuAtikxfL3CuNBKfrSTtPkKqEqk=; b=qLqUwxc9ZhEzXgnS1oOPMC20jQhxVI0cFXEzAWw0Jnfta4NSPMQyZdnTUoYQKKqwzW+nmv lnL+ybl1DyC5NlCfijpLZfA/tHVxxrA1ay0rg0lgcOuTVgUHreDeiNETWX+/qRpOoP3R4n aAX8aQ5rn/sruXBqoV38iMroE/E2zlU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728426938; a=rsa-sha256; cv=none; b=APxcCGDXwoTNJyApSx/wIfNPyNYv5ZLbZJX+DjD1GwdAAvsWWv1rpIEQkzkKeHSY89eg7o h4/+r8gSt67nxqPgTtHlg44K/+2AFt2Sm6AJ4kGnz6N+d6GL/49pOnbZK40/zbzgyqCYBv LLdjfQUYtB4XBT29E1k3bNRhrsmaCqw= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=MUzvxPKd; spf=pass (imf27.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.169 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pf1-f169.google.com with SMTP id d2e1a72fcca58-71dfc1124cdso223276b3a.1 for ; Tue, 08 Oct 2024 15:37:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1728427072; x=1729031872; darn=kvack.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=OwXMBbOWCv06xRC7kuAtikxfL3CuNBKfrSTtPkKqEqk=; b=MUzvxPKdho4wFyAGQdGAO+EdByHvQSWjFxnPDr373jw7oHcukG6KBmMoIK35qSvpB1 6bHXq7jp3i7ku2hg4M99D2RFIbjvvbym2EuKZaRNf/D2A1XmpRPTf3e/w7PobI+96kbc 8p5l0OJIpCTVpl3JDMK5Vmon0ySMd6K8jC684bbEeOfD1hHIBNwwuKi6fmPxdhuPsqvh lrrOrB/5jgZjDTWV+pqqN/F+Ej8kykZwYd8yt3ra71LZEyd1SDUMtS6fZw6KVEO7SbNY 6TOI2pwk3P8yYXbOmKG3gdBmfcZrISYQBBNLl+AnLm3s4lHv6wak7lPM9xzq3r/fRDKX 1P7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728427072; x=1729031872; 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=OwXMBbOWCv06xRC7kuAtikxfL3CuNBKfrSTtPkKqEqk=; b=WxSA4ssOByP5sn6mQ1kHaS6lKB7mVAW+Pl1RNBu3SD+CMIYQW0+LCXC31AgItTpbe6 wYttZGN1jAlceD8VSr76cJjaLMNDFVea+AB9JtyMtlfmRF682mKmTVhr+npfFwG/Fium cggoQh3gYoYWE0XUeoCPcdRNNd1gdOVdK6WGzmAAJlEHID8gPBOyHbv0rpxbpze/dYCo +19Hb9yeHujoR9qr8V7cxE7atsxAxpGaI2wV2gqvenOIrVheM3xjRAjqXqiwKyz9Pl0o 8G0+6bJIjheMmT8ndF50HXk6fuQvUiRmNLNWmcw72BGlPWRRZmSc2Hq+VzvSJ0lP1H3e K5Jw== X-Forwarded-Encrypted: i=1; AJvYcCVuH+FxvfrvF3Tv1bQpmQ0DrRH2M03VQ6Kjg/b1AxUbfslsUzpDE77XhrH12wkDfXumA19iBFPxBg==@kvack.org X-Gm-Message-State: AOJu0YxdvJSxmlVotzsMz1bddzuRWROPI98ZW1z297nKxqDRfzSMQtpc 4SKD8Ya/GkKacauELPd/yxHmGA1Z1V+JtPxJY3NNRdrvCdzFZqDShAtONDUOFyE= X-Google-Smtp-Source: AGHT+IE674Tlfxux7he+iKObmN4Z/Vy2GyXvFfvVMkaZszYDuFORWLn02mu8lREUGmUPEtmKzi9hiA== X-Received: by 2002:a05:6a00:c94:b0:71d:f2e1:f02b with SMTP id d2e1a72fcca58-71e1048dfadmr8990425b3a.2.1728427072537; Tue, 08 Oct 2024 15:37:52 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0ccc4b2sm6591270b3a.45.2024.10.08.15.37.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2024 15:37:52 -0700 (PDT) From: Deepak Gupta Date: Tue, 08 Oct 2024 15:36:44 -0700 Subject: [PATCH v6 02/33] mm: helper `is_shadow_stack_vma` to check shadow stack vma MIME-Version: 1.0 Message-Id: <20241008-v5_user_cfi_series-v6-2-60d9fe073f37@rivosinc.com> References: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@rivosinc.com> In-Reply-To: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@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 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-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: CDED340010 X-Stat-Signature: o1ywr1rot65emg4araddnd1jtjdmiq8a X-HE-Tag: 1728427073-20067 X-HE-Meta: U2FsdGVkX18QWO4gknj4iM06ysW6ptx4dV0thn3WG47sWsXRQlkBBgOzGlglMka4Pr27pB9VwIsP/0NC+8IgYrr4hZsqv/VAQ4GFQPQ9E0GaslU5Ff0Jv/6uzSUtORDtk03aPpX42lGUL+sf/4YZxSk8UtWJ4WAvNAHx73xpBpt71r1qlMvxJVjE31oHD3f88zRmdgrKcrsiFp+op2lAkkEkgUuLDl/P0bao5R6ogrpIK3qXF2EoGM3h30HUBEW2yDQL0br3s4SYKmMDN7F0Yhx0WUnq5FLEodLeS9tQ78F6+mYBgfLBW8VOwB3l8bhNMIM6JVvGMwLlONyub9UqhjubVBx72wZOFcK8OGxnq5YnBzHm9fEhzlEEXb+IFAp3iCN8RNym1/wpIXpkfLNil7krQ+6O4jUuhgJy+Srj0EK0HDjdRdI57+PNF9iRXWSbGl+jRL1HSwDjxtBrF5bx5vXWgGNRNrSvQ0HCQWVlQYVyR+w8FPmgFb4KAWkSw0eSmlKSwkrvApk8s0CkKP+hEi5xTNuTu+gQQpcKHKBYI6KhCDnNmZaGCJxQexf3g5WLqjCBlY93sTe0Ycpwr7+7TO/zeM3t8VUbV5ca8v6rW4jhwq5PMDDJ++X5DuHfrcTOoCHC9LUvi+44tSbWIbT7m+UKM36+J4e1xc52sx6jPYjqiWP3bqEm1mdUeIGRgE6Bqm3r6ZHGwLzOq8ANtm2+D64F/N39/EdthV1VltssXX6+FPO303qrX08B5Nt4/mLeY9SAASHumEOn1VBGe1MK37Wp93a0lOkMnggGzSCSfIUy4QOjDn3XNgMkThYX5V2EyXdz22gKt+8KrTRB13bLEHlbUp5qJqveYNfr1CoO7t81999XOWskUkQNF35HNISQu9dOfrgKDq76ElNco083KZBJKG6YJ/0x+mQMBvZOLfOrpBrJNnEJZ817/7rF/efiSy3j42yroWSBleTSSI6 E/6/Ypva xQ48tf9bXiAfDYG5/7yHpRG6hRuU4dhuU+BeLkzgcnfB8wVAdQAqwVCNoKG3F9RYVphGXTVDtxyGLfr41Cn5Ox//I8Oj1v6tLqXHb49xyzAUB8MLTw7B0IHBx32qkLczokjLqtob9n6voJ2z+38dfgzW+WVUYgy6HkEd4oyNVzE725NYBBMl+pM3JF4lWBbqXxtKyPPJVqrMBDed3POYwCaqsBQl1UUb/vP46J/MXlAnjWY66SjVtTI1HBI1/W4P4znz9rQkmORVzFKWpdKDEQfHRSmwmpHLthpnW6t5S5ZNaf6mhokHJjZ+86ABYBzHCNTHVqgeanx2BuWoT7O9cMeEZ/TisDzF5CZAgnrJp9ntVxNetbkJdJfqZTkGJbZnrXz9nUSqVtSZlJuUEfEKEevPzNwv53N3OZL5jugdLiyoFQKMZWVUs3js5WVJZbdAp2MyK5sHR9NzKQO4nHNoJITyVLPTl3j90cO+RjRkJSPjaEa5C5YZWmGFYqt0fjrnHYCNrVWSBwmZSnaEtTDNUJy+VW8TVveAmajygmwv7VfenIaUZMmPG0MhnTs5gOJMoidCOJ3R4rT/yoSGRZ95fgsXhG/Ysi2zHye6/gWjzP9Z3Evg0lsB3Dl+7AQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: VM_SHADOW_STACK (alias to VM_HIGH_ARCH_5) is used to encode shadow stack VMA on three architectures (x86 shadow stack, arm GCS and RISC-V shadow stack). In case architecture doesn't implement shadow stack, it's VM_NONE Introducing a helper `is_shadow_stack_vma` to determine shadow stack vma or not. Signed-off-by: Deepak Gupta --- mm/gup.c | 2 +- mm/vma.h | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/mm/gup.c b/mm/gup.c index a82890b46a36..8e6e14179f6c 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -1282,7 +1282,7 @@ static int check_vma_flags(struct vm_area_struct *vma, unsigned long gup_flags) !writable_file_mapping_allowed(vma, gup_flags)) return -EFAULT; - if (!(vm_flags & VM_WRITE) || (vm_flags & VM_SHADOW_STACK)) { + if (!(vm_flags & VM_WRITE) || is_shadow_stack_vma(vm_flags)) { if (!(gup_flags & FOLL_FORCE)) return -EFAULT; /* hugetlb does not support FOLL_FORCE|FOLL_WRITE. */ diff --git a/mm/vma.h b/mm/vma.h index 819f994cf727..0f238dc37231 100644 --- a/mm/vma.h +++ b/mm/vma.h @@ -357,7 +357,7 @@ static inline struct vm_area_struct *vma_prev_limit(struct vma_iterator *vmi, } /* - * These three helpers classifies VMAs for virtual memory accounting. + * These four helpers classifies VMAs for virtual memory accounting. */ /* @@ -368,6 +368,11 @@ static inline bool is_exec_mapping(vm_flags_t flags) return (flags & (VM_EXEC | VM_WRITE | VM_STACK)) == VM_EXEC; } +static inline bool is_shadow_stack_vma(vm_flags_t vm_flags) +{ + return !!(vm_flags & VM_SHADOW_STACK); +} + /* * Stack area (including shadow stacks) * @@ -376,7 +381,7 @@ static inline bool is_exec_mapping(vm_flags_t flags) */ static inline bool is_stack_mapping(vm_flags_t flags) { - return ((flags & VM_STACK) == VM_STACK) || (flags & VM_SHADOW_STACK); + return ((flags & VM_STACK) == VM_STACK) || is_shadow_stack_vma(flags); } /* @@ -387,7 +392,6 @@ static inline bool is_data_mapping(vm_flags_t flags) return (flags & (VM_WRITE | VM_SHARED | VM_STACK)) == VM_WRITE; } - static inline void vma_iter_config(struct vma_iterator *vmi, unsigned long index, unsigned long last) { From patchwork Tue Oct 8 22:36:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13827084 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id B49CACF042A for ; Tue, 8 Oct 2024 22:37:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 349916B0092; Tue, 8 Oct 2024 18:37:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2FAE96B0093; Tue, 8 Oct 2024 18:37:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1756E6B0095; Tue, 8 Oct 2024 18:37:59 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id E64966B0092 for ; Tue, 8 Oct 2024 18:37:58 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 47915ABF37 for ; Tue, 8 Oct 2024 22:37:55 +0000 (UTC) X-FDA: 82651899036.19.53AF0F3 Received: from mail-pf1-f180.google.com (mail-pf1-f180.google.com [209.85.210.180]) by imf29.hostedemail.com (Postfix) with ESMTP id BDC3A12000F for ; Tue, 8 Oct 2024 22:37:56 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=m0dohY1k; spf=pass (imf29.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.180 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728426940; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=TN/jwUyrk7nWeXz+2PkcX9PcP6JH0CIrLV6B3SOl4g0=; b=AdjMAFnFPnwgrIM/tMR9ZHpqE1dpqLe+fzzE+ZoT/BHrZKKpRVbyPve4RBLp8F8l8Kzctm 6LXhnNtCwXZKtJgh5LlSM74trh6cufTir0N8TaMLT8AIjm4O5ADhafuOYtwS4otE6AqTbr IT0lt8V6jfJJ+3lpqq0E031sUknT3TU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728426940; a=rsa-sha256; cv=none; b=aAcoKA8BtJRtcfHvW5p65cpKXU8omXg/oCAg814jCNip6RHaEyN9umYhPXxaAwpr1tIVhj 6Za6hayltL/J+LHH0dLOFesbD8j2ZJ+NhCKHYk82oRa/22ebgGnF1KZ6pMiWuSx2YjBVqV FJeVQjxuWoFUvqffdk2u9jqfHAyLYDw= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=m0dohY1k; spf=pass (imf29.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.180 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pf1-f180.google.com with SMTP id d2e1a72fcca58-71dff3b3c66so1962924b3a.0 for ; Tue, 08 Oct 2024 15:37:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1728427075; x=1729031875; darn=kvack.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=TN/jwUyrk7nWeXz+2PkcX9PcP6JH0CIrLV6B3SOl4g0=; b=m0dohY1km9Na+J2lX/G4jPvHPd2K2Elg7fCAoMdPOhmqp1JJhHkSbk3+LcYX8oUcou jupYROawRl2N6EJBAYYw+5ub0uubQrCq7H3oSiziHzu+zOuEClSNscuf8fSj9ue0kyen G3gG+/NQTDA+8/AddnzvkxGj91xKjsTupwDQijE/vC9NJNZipX/Vn6nhpvOi2Q3yp5Qr XVqspvVvzOmIUjWNB0QrC3R1SmCJZQRAjC0LN16SRqE2d8Nftd/Zdxz/Zgx2Owi6Fv7r YGvVRGxEu5J5bAmIw3Vi7H8pltyyfIM2TqFwZwsJw14VTDPyH6FqxaB1zs8b45Z22WW9 lfzA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728427075; x=1729031875; 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=TN/jwUyrk7nWeXz+2PkcX9PcP6JH0CIrLV6B3SOl4g0=; b=DtVPiPN/zuhfZ5/R3OEeZOs3hPlWoLlLhgMVpfumKVEbJi7a4WZsUI/FPYLx+c6tnf CFeTt41xkZC02kI/+RWA1+1DAbIS6dXT3uySLsPx7nEAbF7wNLIg9uetAdW6JFhVYzJ2 /BXfop7VhJ/bR4Vcwf+t/98ZoXlySb/4gHZVEKMnuVocrptwWaDc/5ki9asPArmkRgP8 MzeWrzSQdV78m9Hrxp73PQE8uta1SEqYbjZP8fG30DU6LgE+vMAz1yPQGpl+/9Kt4zFb 3wVbuK/sCLcAhWduow/B/ajw9/+4ktXI/bRN5j+erSdpDjRkPh0jz9Jg57V7kFYHk4QN DPZg== X-Forwarded-Encrypted: i=1; AJvYcCXrLB2HJrRkeyubYYjo0hqGS5uwiGV7Ab1MKsos+YAlo7lbuzRdZellcej+VtsBR4iQQht7Y/sHVw==@kvack.org X-Gm-Message-State: AOJu0Yy5UXgoIdRbceBG/ku5tdxrqdy1CeSSb/DXq+aNNekeJvg7t0AB mvuX3j+kzDA5Tdq9kLWOexe1aYR8Kmk+EExkhBXpMgd9D6wQEEWIB496hy0Pd+E= X-Google-Smtp-Source: AGHT+IHjrWqEH6M+PTKFju9GDA7ag0b13dl3Gb4hkRJRFQ30q32KWOk48b2V/zigaYM7P+HHrQ7gww== X-Received: by 2002:a05:6a00:1310:b0:71e:620:8e0a with SMTP id d2e1a72fcca58-71e1db65b25mr707008b3a.5.1728427075480; Tue, 08 Oct 2024 15:37:55 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0ccc4b2sm6591270b3a.45.2024.10.08.15.37.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2024 15:37:55 -0700 (PDT) From: Deepak Gupta Date: Tue, 08 Oct 2024 15:36:45 -0700 Subject: [PATCH v6 03/33] riscv: Enable cbo.zero only when all harts support Zicboz MIME-Version: 1.0 Message-Id: <20241008-v5_user_cfi_series-v6-3-60d9fe073f37@rivosinc.com> References: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@rivosinc.com> In-Reply-To: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@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 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 , Samuel Holland , Andrew Jones , Conor Dooley X-Mailer: b4 0.14.0 X-Stat-Signature: yiukhqgn3ugoxxp9yqszs9sigj4rc5cx X-Rspamd-Queue-Id: BDC3A12000F X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1728427076-657885 X-HE-Meta: U2FsdGVkX1+fGiliu1JhIZ0Zp/YeKwufjCRCjq0xm18DSA2g9gGh0UnQKr4pX1QirzW+/JIdIJnB8XzdF3OxD4QvHF/PEc5BH+skfIz1OWaEm5Tg/C3Yqv4ckRdS1kdiZaWBE+wCHXc9LLnJw1W+lmFu3e79eKuWh4A8Vuf4OVowWFqT8+vIer3corEqCotMgbJPxTn+Ulr8EXjon3EVhciEp7M6NARSiB+6Vd8UFpAKLHKQmWFGF5vuUPgNC4cunHv0FAB46x4r2jPHEBiSCEpc60Kj7UMr9EhMAp5JVa/5L+JrQfH/dRoGq00INp0bTnhaSrPXaH1e6FMl9uVcP6WpRaFHzgXxgpyac9x+fkzF9CXmUamSWdO8nFYFjlDTNCbYVn3RAJi4gb5p2VdftEyeiS8rugqssRVLx0q+9tMHAmhLxugWOI29lFP/3BfaO3VVTIkwEErBqy8hR70NObjJvYxUvzloGdT+LINTcNBTGRdJG0LXIuLolTxK4WWnyHgAIxLgHhWNlGSYh9hZyZ87Rr9o8Ic1YXu9bgy6sE+AEZxO/OBn82A9K32n2N4F86PIY3HDbaBqFyiub5P5g5SIQ/cSDDT11vT276Sh5KDHuYYCV57QwsmVS+ylxmfTai7Cr7ae4DWkxH3pexlXQUSJDujYpWI7cz3wizT8Y77hWmwRfBO/qPWhxg826kyGQg/nUOCXP48tBcJKLiqlRDj9LKQ12c+HssSwCQAmk/E+g+9t+iEO184Y1mTno4KfAf+n22ruza4WG1Qcj/oZumv2aMphaynyyYvhSPZXC/R5o2uJnO2LcoipDzweCL1RqtNW822XJQU4I6dRzmqpN7yIwiXuu0bYWmFrQPYXcMgRT+ewGepSBnNmc5i1F0aJO3XEopOODQ+YXiKpRX8MR630MpvvYwDDC9ncxo23dwWCm+Fchp8wH00+gbmq4IOV0RAXYXMTjxPQHmZ9Bjp x+1EPSQ8 hxEMmGaCvycIWv/6P7gEx7yHypRUTgcTRUOj3QCEFoGXYONnKa/00gdLPCQ00VGQ4XI1zouig102JQKFaSzAYlbLf2H1hwfEY77Gx156GGKpCUkOYb0hPuwPSk+xoDcI/G/Ilrjx7uUlZUjo/96CmBzrIBpd3fF/K+/Z7GIV8mlY6zaH4NZjd0QD6mOG2xtVNW0li9RPrvmMebz7hvSqgkgu/4O4ku3IyQjD4hM0qHd4mjYRwW9ByFSibe3ZLM4V48TyQSJVZlBgJIf4okBxswRnm40JeotoAAKTMqvHfxvWq/m0JMORS25WujAZBz5QZX9HUhAT1uQ9hWVUxHfOoETn8y9PFJoF/yz5UPrXR3lPS0RNEjwXeUL/hEV82RJ19srknPHghYnCrsQ0YsiR1vkQYj2NrTs3BO94Iwaz7CFQ6GEmLmqwhl+aKiKQanrf4riINvKgNaqSP0+mqNL34IiSWxDwtA2Ig0HjMBPq26fhrP5IXqzoJA70KGaA7j8OWcTAeipm8SSx7QQy5ugsAuE1T51JRPBKLcvk6hIe6sh58SYWupn78jAolf9zAKvLQxziDcBkDPszKDALhyfoxr14gJGIaEwYLtSLHgHuXhkIwK/Xsa4uWn3y9De5YhcwTnu4VlTRP3x7w+60P3wwzOaSNZj2g+eFFhNV2gSdwfUfkWHyHq/0soBXV1hFvAUV0Lqce7a4aj0IisEaQmDITwicYYyWfWD/d9DWUDFTu2/wbh5jtCau7URe0BbxuaGUlNcXAst84gKGd3py6n6qoNqLUqg== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Samuel Holland Currently, we enable cbo.zero for usermode on each hart that supports the Zicboz extension. This means that the [ms]envcfg CSR value may differ between harts. Other features, such as pointer masking and CFI, require setting [ms]envcfg bits on a per-thread basis. The combination of these two adds quite some complexity and overhead to context switching, as we would need to maintain two separate masks for the per-hart and per-thread bits. Andrew Jones, who originally added Zicboz support, writes[1][2]: I've approached Zicboz the same way I would approach all extensions, which is to be per-hart. I'm not currently aware of a platform that is / will be composed of harts where some have Zicboz and others don't, but there's nothing stopping a platform like that from being built. So, how about we add code that confirms Zicboz is on all harts. If any hart does not have it, then we complain loudly and disable it on all the other harts. If it was just a hardware description bug, then it'll get fixed. If there's actually a platform which doesn't have Zicboz on all harts, then, when the issue is reported, we can decide to not support it, support it with defconfig, or support it under a Kconfig guard which must be enabled by the user. Let's follow his suggested solution and require the extension to be available on all harts, so the envcfg CSR value does not need to change when a thread migrates between harts. Since we are doing this for all extensions with fields in envcfg, the CSR itself only needs to be saved/ restored when it is present on all harts. This should not be a regression as no known hardware has asymmetric Zicboz support, but if anyone reports seeing the warning, we will re-evaluate our solution. Link: https://lore.kernel.org/linux-riscv/20240322-168f191eeb8479b2ea169a5e@orel/ [1] Link: https://lore.kernel.org/linux-riscv/20240323-28943722feb57a41fb0ff488@orel/ [2] Reviewed-by: Andrew Jones Reviewed-by: Conor Dooley Reviewed-by: Deepak Gupta Signed-off-by: Samuel Holland --- arch/riscv/kernel/cpufeature.c | 7 ++++++- arch/riscv/kernel/suspend.c | 4 ++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c index 3a8eeaa9310c..e560a253e99b 100644 --- a/arch/riscv/kernel/cpufeature.c +++ b/arch/riscv/kernel/cpufeature.c @@ -28,6 +28,8 @@ #define NUM_ALPHA_EXTS ('z' - 'a' + 1) +static bool any_cpu_has_zicboz; + unsigned long elf_hwcap __read_mostly; /* Host ISA bitmap */ @@ -98,6 +100,7 @@ static int riscv_ext_zicboz_validate(const struct riscv_isa_ext_data *data, pr_err("Zicboz disabled as cboz-block-size present, but is not a power-of-2\n"); return -EINVAL; } + any_cpu_has_zicboz = true; return 0; } @@ -919,8 +922,10 @@ unsigned long riscv_get_elf_hwcap(void) void riscv_user_isa_enable(void) { - if (riscv_cpu_has_extension_unlikely(smp_processor_id(), RISCV_ISA_EXT_ZICBOZ)) + if (riscv_has_extension_unlikely(RISCV_ISA_EXT_ZICBOZ)) csr_set(CSR_ENVCFG, ENVCFG_CBZE); + else if (any_cpu_has_zicboz) + pr_warn_once("Zicboz disabled as it is unavailable on some harts\n"); } #ifdef CONFIG_RISCV_ALTERNATIVE diff --git a/arch/riscv/kernel/suspend.c b/arch/riscv/kernel/suspend.c index c8cec0cc5833..9a8a0dc035b2 100644 --- a/arch/riscv/kernel/suspend.c +++ b/arch/riscv/kernel/suspend.c @@ -14,7 +14,7 @@ void suspend_save_csrs(struct suspend_context *context) { - if (riscv_cpu_has_extension_unlikely(smp_processor_id(), RISCV_ISA_EXT_XLINUXENVCFG)) + if (riscv_has_extension_unlikely(RISCV_ISA_EXT_XLINUXENVCFG)) context->envcfg = csr_read(CSR_ENVCFG); context->tvec = csr_read(CSR_TVEC); context->ie = csr_read(CSR_IE); @@ -37,7 +37,7 @@ void suspend_save_csrs(struct suspend_context *context) void suspend_restore_csrs(struct suspend_context *context) { csr_write(CSR_SCRATCH, 0); - if (riscv_cpu_has_extension_unlikely(smp_processor_id(), RISCV_ISA_EXT_XLINUXENVCFG)) + if (riscv_has_extension_unlikely(RISCV_ISA_EXT_XLINUXENVCFG)) csr_write(CSR_ENVCFG, context->envcfg); csr_write(CSR_TVEC, context->tvec); csr_write(CSR_IE, context->ie); From patchwork Tue Oct 8 22:36:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13827086 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 91C57CF042A for ; Tue, 8 Oct 2024 22:38:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 176E76B0096; Tue, 8 Oct 2024 18:38:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1252A6B0098; Tue, 8 Oct 2024 18:38:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EE02F6B0099; Tue, 8 Oct 2024 18:38:01 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id CC8436B0096 for ; Tue, 8 Oct 2024 18:38:01 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 162AB40C4D for ; Tue, 8 Oct 2024 22:38:00 +0000 (UTC) X-FDA: 82651899162.10.1DAE537 Received: from mail-pf1-f175.google.com (mail-pf1-f175.google.com [209.85.210.175]) by imf25.hostedemail.com (Postfix) with ESMTP id 9EE50A0004 for ; Tue, 8 Oct 2024 22:37:59 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="A/66Wa7l"; dmarc=none; spf=pass (imf25.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.175 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728427053; a=rsa-sha256; cv=none; b=JZLHvJ4E+FKoGn6vF0feVQheI1cKBov9vaJMVnkAQD8Uv/d2hZbkkk7Zj9ghiXV1T9O2Mt BLK3SVrkB01PuLsa+jnqcLU6LwDFpRj++4VVJqNkKNH9E0B/8sarcIy0lHREyAhtstW62R qEs7mLmcU8L2vAPwCOE32YzOj4FvAZk= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="A/66Wa7l"; dmarc=none; spf=pass (imf25.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.175 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728427053; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=jmcYgz7XnLLJto60RpG9fxf/3nbG0BDRV+8az326H18=; b=YhZqeXpK5IQbygPaGaEs5pkv7c2u0hlO4WeJVzPCvEV+plMazVu2Yvp8YEcla3AY3BB8qY NIZ3+XzdpmH0htwHQkZjSF4wEPEzi8i4c9SO9n5B/UgMGRXLcD4bm1M6iON5DGIj3DgNzA Z4lV8cRieXK9j38d+4R4D+5eTPOUkDI= Received: by mail-pf1-f175.google.com with SMTP id d2e1a72fcca58-71e0c3e85c5so1751395b3a.2 for ; Tue, 08 Oct 2024 15:37:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1728427078; x=1729031878; darn=kvack.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=jmcYgz7XnLLJto60RpG9fxf/3nbG0BDRV+8az326H18=; b=A/66Wa7lmXkfBvN0HB124EyRr7ccdKMYaN+7JMfP5P8fEfnQK26RJ2x9R4jX9Uc0MP QWzE2BuD1BXibp1vio3t+aP6oelOySat5jJFkbuZnZ3h37lNekN2UWjJ8g2vaTYT7Nq0 QgudR0ZZ9zDgf8sy0Vn3OXMIIpHDPVUT6e8biESOKCdj9ZiPTBDPiKZM5cn8VIJ1eYgy fU4af8erGjmGlCPFLkaAZv0P2n/jx+7oP8iUGOJSepuRIxodGod56m7yAjt78zhyEfe6 yXFb7itPs1bsTchSKHsPCgU+ARZTD7Bu12FfKAHy0+SKvf7+ccJmvyhaqAeGNa1m7pbg oGyQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728427078; x=1729031878; 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=jmcYgz7XnLLJto60RpG9fxf/3nbG0BDRV+8az326H18=; b=ibuum/boXidCv4RzhZ4bzNtKY+36uyAcP0j/qrJ4bZimNX6p3y5H7AmM28Xzvz2txo tCzkBXJ94PkWC+eCwV2ZQjzk2OpC8DBN5Q+pSn2tokE2d4cZAiYzAwlZA5rMdcSM/Go6 DzR7jihkFFQYJFJtBuQw/5DXfB2sJ3MOyr7Xe+GrGAyn3jQ45dEdBSis13ZzVXK+//Ev FMaQpFm89kSlzYa1XKGkI4LbzDUc1NVunhuoEVtbOr0AQ5QNt5TVKZIeY03sGu77OwaC xFwnq+ktf81YnSR3412ic3ilO9VO6UjNJKjsCFZJIEQvExSB+TAoqZW5o6lryQPcWnIk AjeQ== X-Forwarded-Encrypted: i=1; AJvYcCU1CPp4dO1oz+rxgmJRTqJLAv6SqoHLQrmCCqxPRw71fdBNf5PXEy3+8z5HnxEaHJEkw/Kcq/cnyg==@kvack.org X-Gm-Message-State: AOJu0YzcaJNpyzKNAhKHy7ZeBDG2owgk9niS0jG9pswnQEW9sOXo8TPc aLcER83hBvyOQ9nhcN/XpeWY71dwTzEyyAWBn+Xm7MVrs6lF2MCTnd2f6flZw7I= X-Google-Smtp-Source: AGHT+IErKskUpIBiFKlwdYnNS9we5iBG5psWxaDz48JOAw5L1P7wN1gzBxCeW201xTOm4LwBlfF9lQ== X-Received: by 2002:a05:6a00:816:b0:71e:19d0:2988 with SMTP id d2e1a72fcca58-71e1db85912mr671554b3a.14.1728427078379; Tue, 08 Oct 2024 15:37:58 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0ccc4b2sm6591270b3a.45.2024.10.08.15.37.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2024 15:37:58 -0700 (PDT) From: Deepak Gupta Date: Tue, 08 Oct 2024 15:36:46 -0700 Subject: [PATCH v6 04/33] riscv: Add support for per-thread envcfg CSR values MIME-Version: 1.0 Message-Id: <20241008-v5_user_cfi_series-v6-4-60d9fe073f37@rivosinc.com> References: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@rivosinc.com> In-Reply-To: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@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 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 , Samuel Holland , Andrew Jones X-Mailer: b4 0.14.0 X-Rspam-User: X-Stat-Signature: h3ibn9im8cjuuw5usttrkutb75uy4qtp X-Rspamd-Queue-Id: 9EE50A0004 X-Rspamd-Server: rspam02 X-HE-Tag: 1728427079-440007 X-HE-Meta: U2FsdGVkX1+jHo5G95qb37+ASlaKk1lF2KfhQHTRcMI+SQiofBcsIgr1T5ob1dPYVuSpEJpgI2mOh8L8rfCKtF6OPUAnBUk7zgoW6LHt5mL5kd1L69FU2ko01bL4C0A9iuKqCEPEya/QWpSDoreg0w3z0tZp239raXHMBRYkZ2Ouof3t5kKNAib7ej0L7LBpdvz+emV27hjIno2QzwtZ/EvfkL27Y1o41P6Vpj07WfBQYrudw4tHOe7xMeHYzePZicG7NNKiwVOIlGD4IIyxo2p6ksiKuwSGrxiZmijIPf7rL0bUsCC1xLl4J0trqaP9pFkUGhDevqu51nEuQLTPfhPPOYrJBJsxJ/JNo7LTf1wijPi2fhIcPfW6FNfqAdIlRA1aSz5R2dB6Rt3Fg5YQpFxNfhz16/Gs/pPmhPWSTcJAbO+EFHZF1t4qRj3f5q05E+fKMRLsbpNvAuXbrRNCaWpz+3rc2ZwTwmkvieLLvRGVRQUbbiT10e7thYcULgIm49d4h4ctCC5mv7lOsiGiYtXD1BxGFCNKSRFk3GuU78INYc/brLE8tpWMeAextM3D6Mzd3aHSStYPg5jVx0niTWecmiJ3mbmbUagMwNqf/UQSvRyUx5ooejTRI9tayJhJsbQaRBHgZA/OUv5UCf6fxZheXwbpeMF4hkGcgEShNHIErMnA0oqU6bc2EQ871ucGulh67p3cX2aHrvWDal44HAx4fs9Ukj0M4NGg6VPf1bHSVRlW0a3YZPyBPwHmhxNPi8dupo21KGDrYGga0Aw6DGLFAu8SyU3yzCAkZDsGJkPtmd9Vs+I72MGZ7obnGeokhx5Cur4upziqTdfZM9zJAvKgFdESLaeLrQqm63JYiCD49HvK3Nyh2KKcJHfR4EimyTHv15nkkREy/xnKaXIG93i+fB/8lh55kGg4Kbr3wohaP6CSorDkq4oJyCId2frVfCxgWe4Eou6bOhJO5j2 wNXKfdt6 YhRs87ClVaFrE47J7nDdrXU9FMpdznks1tQfaumep2VZb0E3m4OKTP4l/De9/Y0SQb8DHRxwxRfJ91b40CE6rfgPv7PBYgNP6k3piAJja4FJz17phLeAOH8CG6j+24Fm5v1OnhTjK1fjz5ZKEHHdDkqDc0m51yFxrL3FhHuklAngpY0n2YBypFgpdBI66m+AEKSAUR9qa1GLM0hylmbCtMxzbDC3ZKvXR6waK1g8+aJkn4scRFNCuHjEAKkQ8p8SnOQeU/dEriXVdvy1tossADK45piAAoc8luePaqv8ss5Ovl/RFxRs3LFxln8tC3HpKMAvDFyXpppv8WgKCzCtENvfAaLNVGPnl4fkJtXZmNglBEQQQsGTAMA7FRKg1nC+bZZCh7AgG2ngJGi4aNB0Sid0uyb4P4aC972TOxv7CirdMCMprFaEy9lxPnMG399w+dUD17/mNlVOVIGqy3SkMQQ0Yo5Baq8n35iKbDI3KZP3TYdqWlTn5f/CDOiA8nUf7saWjj8oRAu7n5P9o4pCNQczy2+GrSZOFAH3k9mGhr/9QB4Dw7sbeuzjMWW2Rq4A3bOw4W6RZ7NZVIus= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Samuel Holland Some bits in the [ms]envcfg CSR, such as the CFI state and pointer masking mode, need to be controlled on a per-thread basis. Support this by keeping a copy of the CSR value in struct thread_struct and writing it during context switches. It is safe to discard the old CSR value during the context switch because the CSR is modified only by software, so the CSR will remain in sync with the copy in thread_struct. Use ALTERNATIVE directly instead of riscv_has_extension_unlikely() to minimize branchiness in the context switching code. Since thread_struct is copied during fork(), setting the value for the init task sets the default value for all other threads. Reviewed-by: Andrew Jones Reviewed-by: Deepak Gupta Signed-off-by: Samuel Holland --- arch/riscv/include/asm/processor.h | 1 + arch/riscv/include/asm/switch_to.h | 8 ++++++++ arch/riscv/kernel/cpufeature.c | 2 +- 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/arch/riscv/include/asm/processor.h b/arch/riscv/include/asm/processor.h index efa1b3519b23..c1a492508835 100644 --- a/arch/riscv/include/asm/processor.h +++ b/arch/riscv/include/asm/processor.h @@ -102,6 +102,7 @@ struct thread_struct { unsigned long s[12]; /* s[0]: frame pointer */ struct __riscv_d_ext_state fstate; unsigned long bad_cause; + unsigned long envcfg; u32 riscv_v_flags; u32 vstate_ctrl; struct __riscv_v_ext_state vstate; diff --git a/arch/riscv/include/asm/switch_to.h b/arch/riscv/include/asm/switch_to.h index 7594df37cc9f..9685cd85e57c 100644 --- a/arch/riscv/include/asm/switch_to.h +++ b/arch/riscv/include/asm/switch_to.h @@ -70,6 +70,13 @@ static __always_inline bool has_fpu(void) { return false; } #define __switch_to_fpu(__prev, __next) do { } while (0) #endif +static inline void __switch_to_envcfg(struct task_struct *next) +{ + asm volatile (ALTERNATIVE("nop", "csrw " __stringify(CSR_ENVCFG) ", %0", + 0, RISCV_ISA_EXT_XLINUXENVCFG, 1) + :: "r" (next->thread.envcfg) : "memory"); +} + extern struct task_struct *__switch_to(struct task_struct *, struct task_struct *); @@ -103,6 +110,7 @@ do { \ __switch_to_vector(__prev, __next); \ if (switch_to_should_flush_icache(__next)) \ local_flush_icache_all(); \ + __switch_to_envcfg(__next); \ ((last) = __switch_to(__prev, __next)); \ } while (0) diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c index e560a253e99b..27bafc5dd62d 100644 --- a/arch/riscv/kernel/cpufeature.c +++ b/arch/riscv/kernel/cpufeature.c @@ -923,7 +923,7 @@ unsigned long riscv_get_elf_hwcap(void) void riscv_user_isa_enable(void) { if (riscv_has_extension_unlikely(RISCV_ISA_EXT_ZICBOZ)) - csr_set(CSR_ENVCFG, ENVCFG_CBZE); + current->thread.envcfg |= ENVCFG_CBZE; else if (any_cpu_has_zicboz) pr_warn_once("Zicboz disabled as it is unavailable on some harts\n"); } From patchwork Tue Oct 8 22:36:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13827087 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 77E97CF0429 for ; Tue, 8 Oct 2024 22:38:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0D8F86B0099; Tue, 8 Oct 2024 18:38:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 088E06B009B; Tue, 8 Oct 2024 18:38:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DF74A6B009C; Tue, 8 Oct 2024 18:38:04 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id BB38B6B0099 for ; Tue, 8 Oct 2024 18:38:04 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 0DD2380CB9 for ; Tue, 8 Oct 2024 22:38:03 +0000 (UTC) X-FDA: 82651899288.17.E92F147 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) by imf25.hostedemail.com (Postfix) with ESMTP id 87013A0006 for ; Tue, 8 Oct 2024 22:38:02 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=QGNnf6CH; dmarc=none; spf=pass (imf25.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.179 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728427039; a=rsa-sha256; cv=none; b=XC/KsuZwY1L1sqEnYnlv6HLF13uCh0VszWNF8NZqB6HhSfs5GWZofAaChcFxb+vwt0+ZHD mLuof8+hfefbT2uMxMGKPn1BQuuiEKjj5WQtUsvm0zaqzqyg5zr4sWloKVsi46u9vEa8yl 2/KCmHUryojTTlGxlYTeuDd2sEDNXd4= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=QGNnf6CH; dmarc=none; spf=pass (imf25.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.179 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728427039; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=aVxR1oWgZ2k5hHFq9uLsQO1od960zXed/8DpSx7GGdM=; b=dIguNocoPheda/mAb7ciRMVQZV0mDOSMtqrSFYoyL+WI/9stYhfnkDsigLSTkYf3ihp6NX +CHnHPt5syHfJPoyEbn5EnAQ9C7uPkP/41asXavcd0eu09DqJ7BiPwc5Lag/RIjC0SMLqv NCoacnp63jya8prKHtOqjiVxR6RiQUw= Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-71df4620966so3402892b3a.0 for ; Tue, 08 Oct 2024 15:38:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1728427081; x=1729031881; darn=kvack.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=aVxR1oWgZ2k5hHFq9uLsQO1od960zXed/8DpSx7GGdM=; b=QGNnf6CH+3mMAnFa2o46VNeK/VDCOkc3qorwqf5u3ZPevbqKnKrS+bNZZKJLEC5reW DpDTyJ1GQE6Upu5IWxHw9dGQ+hmk9gXau4D6D0tz3yf2ZIbcU3DNVjTPM/RbIjArY+Pl gcZkjQU0kFFohfJtTnan+w/X9YwZCVaUg7Peli1aq4hzB0FcgEKylaWbtrD3vIfCCWOZ FXiR0RhajqyVjsGWN5EumIpjdk2Zy83qgJ800n9RyaZ+QVSvjGsu25F1yrHETT+tfzVL 7RVwoZbZfxcMYHq7ar7RuswxfY/ZwjxU3TH1zk+n9QQxXredrdOijaA1/UEi/mcE82k7 7MAg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728427081; x=1729031881; 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=aVxR1oWgZ2k5hHFq9uLsQO1od960zXed/8DpSx7GGdM=; b=PH6X6f2OhvWTmNCou1SLlDG/sC2QXyvL8WRPwIjZQrEGsJEoSC/cpbb4V+fPeIRGjT DaeeiZyDpH/QzAtdHYgZ0UDYAYK913iTKPU12zh2HcSoswlQfmN4jZYHxUYDpIPU1yBh dFczQ/LuWZ0KHLwSj683DjD3WUwlzs2sHGvAiIh2NVqqYanKNqCXH489xWjHNfkCIWqw sbXmEyTvx6AEy8HHRUjJs8Maaebt4uKZAUGjuIZSNUuAPP1WotUz3hRsAX7/XSU+LuV2 2HHnhWcjWPNY7NG4yzFbrJJaHbMdDP7MWEFhCccYoPsh0YHYgcpUPxNY77KQ/kl3A8J2 wr3A== X-Forwarded-Encrypted: i=1; AJvYcCU9Q6ihtfN/kgCAZwf8Cqbz8EZD7ta33t1jeJRRekbVm+FhyYFDbG6uU/56PEPRdlKZDw/U1wBzhg==@kvack.org X-Gm-Message-State: AOJu0Yzw/XzFf7LtUM5GgPRnpoBEdLRKrqtcG32J362Eho45JqNDzZ45 0F4mdS+QrLX+sFZZgmk/rp9O3wVKAtsHokjK0i3UTUyviInr23mLgWi1KEGk1ow= X-Google-Smtp-Source: AGHT+IE/RTGphmtify7ZaBGz4r8U96xtkLO6P3lDZmR5UXWHj3NjFx9egQQunrEc3RZFFoV8wwYuqg== X-Received: by 2002:a05:6a20:929d:b0:1d7:5a8:379d with SMTP id adf61e73a8af0-1d8a3bff07bmr624695637.15.1728427081301; Tue, 08 Oct 2024 15:38:01 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0ccc4b2sm6591270b3a.45.2024.10.08.15.37.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2024 15:38:00 -0700 (PDT) From: Deepak Gupta Date: Tue, 08 Oct 2024 15:36:47 -0700 Subject: [PATCH v6 05/33] riscv: Call riscv_user_isa_enable() only on the boot hart MIME-Version: 1.0 Message-Id: <20241008-v5_user_cfi_series-v6-5-60d9fe073f37@rivosinc.com> References: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@rivosinc.com> In-Reply-To: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@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 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 , Samuel Holland , Andrew Jones , Conor Dooley X-Mailer: b4 0.14.0 X-Rspam-User: X-Rspamd-Queue-Id: 87013A0006 X-Rspamd-Server: rspam01 X-Stat-Signature: syij98zni971efp5fu8797xb9odjiduf X-HE-Tag: 1728427082-467061 X-HE-Meta: U2FsdGVkX19osH740vf9ibrJ+xcuCJOYDOFlpBi/FNOKkQm9WD+ovuprBTTVWk+d5yDvQg+xcn7vvRpQXFQbo3xbFFvn91NQL8mmNCdaUaoKnqxdy/8O9BDgEocZqcY5BbIsaAf0H4KMIwzXtu+HclENKguJHTNWSe1ZJkE2kJGCw/fTtBJ1mr0Hs97lOcDyyXtLl4gneZcSPmFjh0/ZtEMXRaons7FWT67k/3RditxxvL3I8hHww9lLB2Mj+63U9N8IIu/AXtT6NgglTDkwdq1LSs0yqddVet0xo80ZXjWBNBzUwd9ZwXpc2l5Z1rJobqFRVYhAAncKwBZdSshFI/A7xvm6jYfRstzfHDhHvv1ISAlbx4AGoXV+QBfQtnrIagC2Ml64HMHagW/82qMk0UrNVX4LQo2ax+SVu2qFrjokaya5wtqYhr/kQsHjsVTe2Uw8tog4AKinSCLCWk3HzPlqAgqgOF1E9LWF4ipUHAAVx34h9cUishegMmNjPXR8cZp6EwFPTW3xSF1IOrbarEBl/LOgBWkOf+BHxAeKenKCSOqpfxtPiIEZ5sd/dOLxVHqouUhHunTKFVRMewSKac1zTaVF01/ZJonAjzcPnaaqwG9zkhjb24cboDkARiM3oPpadTlVqe3w3FcyLRAkp1Nz+Nsz21NCAEs70Llcvn6wln4PLlt8AH/JWmHmWqkEWqGA9/lCTiv4Ax/4RJv7M+N63gaOPPXX4QojOjLON4P9cZxejwTlaEThymooozpUtDq8SYFrjPV8p3LnJBXS27vtrx06742BNYMnep6qERCQwiqhh3HW/PUMPjOOC8BvlJHaKW3HNTXEA/bVuqfsfgosPKxRRPkoOpJgPc4o1qN9CXVA/eE00seRY824kBpPmfGfLhdqRE55Zlt7RPA3QIb/8jcsnIZbzagigDFmgaqL4N91f0BS54xNO/7Ivt9WFBQDo/E3XvlVaoi2H8j ZltVe3Mn 4Nh6VMIO4iVHV8t1UZw+5goU3w1Rvb5aRLVEKeQFNfu0OVMwEBfHWToxrp5aYQ3vsGTy7pdJmpBLfBVfTRf9kgk8uHbC8ipM0TFN3vtJG+VhEG6L0GZX+nivR/kEzYwBKcrkR8VzbdUMJezZix5l+y2184Z6Zv/1I7SMXD0EDF7LD/XBXiqYLHyhyBBqu6xROodurrzkPVdKRliki/9OLAZLF4a5UhypQKIhaCsWn4wx+MH6w5NVLozNvxEWNR1/i8vxuS7i7YeZb7Nb8sXu3+PLP3F5pgME8UtOdIaNqnX0s/yySCq/CJnRmV1ql/f0l1BEG4h0EIKhkBk0b37YvWbm1tnZv3gjAdgbjx0A3W0CafYQUhb3zhcx1nxE7xVB3Qez9cnzP/Dx/pbvtLDRaFnhvn5z0dBcRf2rzR7nxNDaf6PJsay8wuRBkJnZBmCEVnhL5trFjf2qeGAczaWUIB5WqhnT7tLxA8IHFrsdqOV9Ek33VqTkqxGOt+fs28MsMQORkOvsErc7hiTROsEtliJ7kUUOMj0nN/rSUG7B09WnGlN4AncAn6hlkCndL7FXXprcs9F/w0QCN6MaW4nMaqugo/ylluryUeJ0h/N3d0Ps+zz5sEPHnymPfRMXSZLrWSh4SLVQUQp+0sP4nMHX1hZ1T512HB6yizPofAzmhaII7DsdvoPeHeQVqaQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Samuel Holland Now that the [ms]envcfg CSR value is maintained per thread, not per hart, riscv_user_isa_enable() only needs to be called once during boot, to set the value for the init task. This also allows it to be marked as __init. Reviewed-by: Andrew Jones Reviewed-by: Conor Dooley Reviewed-by: Deepak Gupta Signed-off-by: Samuel Holland --- arch/riscv/include/asm/cpufeature.h | 2 +- arch/riscv/kernel/cpufeature.c | 4 ++-- arch/riscv/kernel/smpboot.c | 2 -- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/arch/riscv/include/asm/cpufeature.h b/arch/riscv/include/asm/cpufeature.h index 45f9c1171a48..ce9a995730c1 100644 --- a/arch/riscv/include/asm/cpufeature.h +++ b/arch/riscv/include/asm/cpufeature.h @@ -31,7 +31,7 @@ DECLARE_PER_CPU(struct riscv_cpuinfo, riscv_cpuinfo); /* Per-cpu ISA extensions. */ extern struct riscv_isainfo hart_isa[NR_CPUS]; -void riscv_user_isa_enable(void); +void __init riscv_user_isa_enable(void); #define _RISCV_ISA_EXT_DATA(_name, _id, _subset_exts, _subset_exts_size, _validate) { \ .name = #_name, \ diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c index 27bafc5dd62d..b3a057c36996 100644 --- a/arch/riscv/kernel/cpufeature.c +++ b/arch/riscv/kernel/cpufeature.c @@ -920,12 +920,12 @@ unsigned long riscv_get_elf_hwcap(void) return hwcap; } -void riscv_user_isa_enable(void) +void __init riscv_user_isa_enable(void) { if (riscv_has_extension_unlikely(RISCV_ISA_EXT_ZICBOZ)) current->thread.envcfg |= ENVCFG_CBZE; else if (any_cpu_has_zicboz) - pr_warn_once("Zicboz disabled as it is unavailable on some harts\n"); + pr_warn("Zicboz disabled as it is unavailable on some harts\n"); } #ifdef CONFIG_RISCV_ALTERNATIVE diff --git a/arch/riscv/kernel/smpboot.c b/arch/riscv/kernel/smpboot.c index 0f8f1c95ac38..e36d20205bd7 100644 --- a/arch/riscv/kernel/smpboot.c +++ b/arch/riscv/kernel/smpboot.c @@ -233,8 +233,6 @@ asmlinkage __visible void smp_callin(void) numa_add_cpu(curr_cpuid); set_cpu_online(curr_cpuid, true); - riscv_user_isa_enable(); - /* * Remote cache and TLB flushes are ignored while the CPU is offline, * so flush them both right now just in case. From patchwork Tue Oct 8 22:36:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13827088 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3BC33CF042C for ; Tue, 8 Oct 2024 22:38:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1EA436B009C; Tue, 8 Oct 2024 18:38:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 19A636B009D; Tue, 8 Oct 2024 18:38:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F300E6B009E; Tue, 8 Oct 2024 18:38:07 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id CFCF26B009C for ; Tue, 8 Oct 2024 18:38:07 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 40001A0CA9 for ; Tue, 8 Oct 2024 22:38:04 +0000 (UTC) X-FDA: 82651899414.13.9E79B6A Received: from mail-pg1-f174.google.com (mail-pg1-f174.google.com [209.85.215.174]) by imf07.hostedemail.com (Postfix) with ESMTP id 69F2940004 for ; Tue, 8 Oct 2024 22:38:05 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="y/sMB0kZ"; spf=pass (imf07.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.174 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728426950; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=pxvPbFdPQ1tC+j3wFHGGOQBUzN++1F4GTwbS3f8RUbU=; b=YrpSjmCes/fVrzStzwtlldgLnKqUOEe6vOtymKT2MZWInboZljlKwwKPsNC338OH2+1iid CkhJwRfxx6SZRkMsvRCxoDOS/HsuhbA6+eyGE5uh/TitYwUenRWpIJrCCR64EsPVX8toK/ LporMhSqQQWsHckRRrmDvVw79A6vuaI= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728426950; a=rsa-sha256; cv=none; b=HYEahG1wB/HCfi5XByfwPxrPlZlDZgDxVhmq/GnXpZ0EWZHhRNG/LgbEq2GhrlUOiuPq+p AZ8ADW34xC7bTxsOn6yp+zZ3GehWEmlksqMTN7NA4b/SUl4mFN0vdidVqIGTbaoFwtiTSv 1/PYNq+Xxab4h5WmHw4ns1psldBzINs= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="y/sMB0kZ"; spf=pass (imf07.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.174 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pg1-f174.google.com with SMTP id 41be03b00d2f7-7ea12e0dc7aso1717639a12.3 for ; Tue, 08 Oct 2024 15:38:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1728427084; x=1729031884; darn=kvack.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=pxvPbFdPQ1tC+j3wFHGGOQBUzN++1F4GTwbS3f8RUbU=; b=y/sMB0kZHvclnzk5t6MT80F72QxXXAX5CyZzeVjbNnvv3R/fHsXGLYW9FNl9ZzZG5C PwRIdjY2yg/yUVp3bA3+NOnpXEgIKVr4QfNRvQ/+EQXkmGNkFFYa8WeO4WuIAFMGUfhn 9fw3n+KrDtrn+QPBJ96hGJ6LQn6Oh8oBAnmiYojmqoeeFaMvJpUF5Fhs5AE7SPi1mj7q cCZ4WB3aV0bE3qgCn/NxIsm2i9x79XEGDtENrOtvTFDzm+NIGVaFpdA6N0+WlIOkMp8A PBnES49Zrwclf+G8PbQjhVHv05A2WyEfGVEJe5rzEx+kLbaxmuXhFRVPIkxWTFR8Ob06 nXDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728427084; x=1729031884; 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=pxvPbFdPQ1tC+j3wFHGGOQBUzN++1F4GTwbS3f8RUbU=; b=EG652KFOOr3/2TRtGOIUZxrai2Vef1CECGRM9JoTyPyvRG3DVQsrdFiyaSOMrarR3s XZR0cvrqNEbFo6I7vHQn/hZ3zugky0vJ4gRNlCSXYUt2HG0L8JR0cme0oh5tep3jzLpw FGq4FeauCOTAa5QNXJGLNh2tnkMMpgjkxQf4wTu38x7e84V9DWMGmPu+suwtmMksRQbB aEBYmikd3e4RlXSfm+EVGhgQ8/G5ceKZGHaLPRN7uyt9Ze+ND9jJeXS4i6+Ohw2RWp/O uVkz89r368I+N5QS+spd3Po2Um8I2DBtnbOP+I61scmybL56nMl+zNzaA4AUowrMIpXd tmDw== X-Forwarded-Encrypted: i=1; AJvYcCWAFV5axFsmCeJHFUkWJdPZOD6ECbhMWpA74SlM7CNRXsfxEix86UyJGnCmN8pF8jvyyPTomybq9A==@kvack.org X-Gm-Message-State: AOJu0Yy2MtWbingpF34e7P3Eg0kqMjtGPaFYWdIBCdropIlFPoQfMhT5 JvxQdzC6JFAycTDYPJcVGoNB2qpxxo/UJ9MaldopeXlTjapqCJPO1KVy291RF3g= X-Google-Smtp-Source: AGHT+IF3M/VQF4JxFEIfdPLZxd9ZoguSrfvN9rKCL1mqQD6cOVqVplTxyPp4R1gZRhnFMnmKa4HdWw== X-Received: by 2002:a05:6a21:9102:b0:1d6:d5c1:e504 with SMTP id adf61e73a8af0-1d8a3c1e587mr859595637.26.1728427084123; Tue, 08 Oct 2024 15:38:04 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0ccc4b2sm6591270b3a.45.2024.10.08.15.38.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2024 15:38:03 -0700 (PDT) From: Deepak Gupta Date: Tue, 08 Oct 2024 15:36:48 -0700 Subject: [PATCH v6 06/33] riscv/Kconfig: enable HAVE_EXIT_THREAD for riscv MIME-Version: 1.0 Message-Id: <20241008-v5_user_cfi_series-v6-6-60d9fe073f37@rivosinc.com> References: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@rivosinc.com> In-Reply-To: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@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 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-Rspamd-Queue-Id: 69F2940004 X-Stat-Signature: 7wnjhdk14ce8aipt8pnuzm1uh3ctdpka X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1728427085-775400 X-HE-Meta: U2FsdGVkX1+VP4z1C1pMdf8ctsifODZFgjb3AHKXixF3j1XC36jx+1X13Qy4F4X/SE9s25Chgewekba0Xlf1jp8xd3TevsJ0uee2BvX6cyyOiX4cZTt/NAOGPHCxcvIXiroGk87t8IWGOQM7EVPhWx53rKn0DzxN6VqMurhrNuZczMa/lX+pGEsntSyiKZEsJzXaHI8k8Bm3y+aTMWnFbqmDT603rPFR4/i5+tqfvj00hChMOvr4n5KK1G6y/Xo4EmfAg57l3otbWxKuBpmMxy80ZGPMRp+wzPPcCdbLPp07qPDaKTSZ4rAoNzXKvGIs4NBKzUAWf2Wlj4FbZrjX3UndLNbd6VALV8gfebXvBgT0QND943aLzXEuK0npxAMN/51R8GqDQYjUOQ6RtVZSLkdSmnvW0SAlPSJS9UGKsbAketHeD8z3pnoyhgD5Lr9V32v9PpNhrIiXRSbnC+wsVqBfw7OcUHQwe3zXRwunkCgzv4GkOww6Ifm1v8gerqBa+plhmHAh8XD7IBFT/omal40eT01xonPxmbo6szSoez4DWr9dEA9le8c0epHr6swFQjiIEIJCztkwbPmyXxchzkpCU4Dqub6BOs8Kj5qYhA6vts0ebE4ZpEE5foKXl+8vZ0WrkHLTOrteUcaXKesZNA2tJb+FQ0k0tgLcM8ai9fFke77i8KIzHc4E0FnOJlqoEc7jHOSozfjg2tvm1kDhBi5hqiDk7iPCDWruyAt8wYZCLDQS3dMC6unpG+dphTNUIoVUJzm3bIIKaWX+RVy794g7Rx5yz4nofmznqUS179uFI/2EcdyHl1baF2+lg7QSTTccnZSaiJevo9os48GLg7lbKgbU5qlQFYOlBBxe9fobouTeXxb/rq0pl1f+f13YPdKRcfHw0RCZKeoCAC6Q/nWxzge+Zt23EEG0Ds4uVhSfq7WuyNz9Vr4qTSsiSeIjUNzss4GPHQ7tY8+/2KY Hllq8eaD 5eCJGVr4SeYMB+jG0FMSvknR4jayA+HeGkmhw8diBKVq193QkM5SuCJDHRNx2cBqRoCZDlHWjXRTBqsLzwlak5RfDPRslC/2H+YdYrhrltrJreMRI21t5n7oa1fHyek/vjCuBJnw2Ue3noXGMQriNh8ceY5U/c0xGoWH7yl3l8NSRvGo4gbRMXye1SPKCO/5KlntT9iVsnLQ4ozmVg875KGh1C1zOapZ6W4ld4wXQ0MqECploRv95IK81DIpRE0ADrfdzM8lfEKSurzQgjchs20NNYu3h/JwqK/0jOs43gnUBBaOEah2uqNPYTc0IsyJrV3Cr1r4v6m2rSDb3a+npbQU99cqDormyz/1mNlE0/3x82ZnGdGLo3ncqLtTdENypPUNjalHavI3Hx1TVPrS8erkKnQVnoYsI7XG8FQSqPXjfzeoLSRu/+knJrp9B4hVgMkf3CjYcwqCMhAyI602zpjdsKWNsm43OP4PbWPiakBtgBSZhQJqAPUaandNWRi31ht2TK0nb1bthjya/+RYqJ5Vk+gUTTB7pUnQYUu7eU9UwYnfh3uBfjCaicvyI3C9iI0yg9NMtr5GyJoM7db8Jyi/xe5gq5hm79A28pgVVdYyxdn5k0+OoEY/gmQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: riscv will need an implementation for exit_thread to clean up shadow stack when thread exits. If current thread had shadow stack enabled, shadow stack is allocated by default for any new thread. Signed-off-by: Deepak Gupta Reviewed-by: Charlie Jenkins --- arch/riscv/Kconfig | 1 + arch/riscv/kernel/process.c | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index 22dc5ea4196c..808ea66b9537 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -182,6 +182,7 @@ config RISCV select HAVE_SAMPLE_FTRACE_DIRECT_MULTI select HAVE_STACKPROTECTOR select HAVE_SYSCALL_TRACEPOINTS + select HAVE_EXIT_THREAD select HOTPLUG_CORE_SYNC_DEAD if HOTPLUG_CPU select IRQ_DOMAIN select IRQ_FORCED_THREADING diff --git a/arch/riscv/kernel/process.c b/arch/riscv/kernel/process.c index e3142d8a6e28..1f2574fb2edb 100644 --- a/arch/riscv/kernel/process.c +++ b/arch/riscv/kernel/process.c @@ -201,6 +201,11 @@ int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src) return 0; } +void exit_thread(struct task_struct *tsk) +{ + +} + int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) { unsigned long clone_flags = args->flags; From patchwork Tue Oct 8 22:36:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13827089 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 34580CF0429 for ; Tue, 8 Oct 2024 22:38:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A27FB6B009E; Tue, 8 Oct 2024 18:38:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9AFD36B009F; Tue, 8 Oct 2024 18:38:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 78D656B00A0; Tue, 8 Oct 2024 18:38:10 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 512F26B009E for ; Tue, 8 Oct 2024 18:38:10 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 8D36D120D0B for ; Tue, 8 Oct 2024 22:38:08 +0000 (UTC) X-FDA: 82651899540.25.EF297DA Received: from mail-pf1-f180.google.com (mail-pf1-f180.google.com [209.85.210.180]) by imf03.hostedemail.com (Postfix) with ESMTP id 2465C20005 for ; Tue, 8 Oct 2024 22:38:07 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=kxewPc+x; spf=pass (imf03.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.180 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728426952; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=KtNVXjjPkEHV4V6z1fTmchaZwC5pGCKRUL8HB/rDqj8=; b=bbjiiVoQGHwPn+uFHbclH2cyE+DyysdyabJbkK/tIFqVEciqx1TQqWER+w4diAgDkCfmx/ c677Wt572VcX+A1xCqRqK2XpoZUttzjhAsD75TVQElP8ErzWLT+n3xedNyOxijmhQb3BW3 xjaJbXrPFzi0T7BGxs0cFlO6FkmR5+c= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728426952; a=rsa-sha256; cv=none; b=Q0Q9jkJQqmKPqoTb4eELR1kg70IflTKkrnanHs3jAAqaH/RTP2n7kh6Jn3zIhP8DFzb2Gn enrm0npPjHT1oHEpDxpeq9olsZDMFXbJjUcd+ij7MQSkmFHhyptkVajuZL1h+ny8W6KgyW LczMvlOQn66fBgiS9x2fqJaOiAzsr/Q= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=kxewPc+x; spf=pass (imf03.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.180 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pf1-f180.google.com with SMTP id d2e1a72fcca58-71e06ba441cso1997861b3a.1 for ; Tue, 08 Oct 2024 15:38:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1728427087; x=1729031887; darn=kvack.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=KtNVXjjPkEHV4V6z1fTmchaZwC5pGCKRUL8HB/rDqj8=; b=kxewPc+x6+JWS4iyhDN6muKwxIKSXoMhLKA0XJ/gpkTbjzdM5kN3KW1u/Dwh2Ihdzo 8MlGDPUOE03oOnQ8Wvn5a6DzvAmiHH7tf1xrTatvmpg9r6GgnQREeu0kEz2pK5q/AQ/l PrgVI+aCknpItlMx5wrVICTVatgwrS7iwgGE2nA834wRFlKJaj38GkAQbDO3uOeVEFZ7 8pzZyZ24pXCZ52Je5+FAs80TImtRdxekz/t4cJXeg6JPO9zZGVzZAH/tk942hTLg2oPV zZbWVyItjFVhahJMJc3gjaHzWTXa6D7enIC1hWC5wqEtTyUCFnf54lEh6YbUzPaJZcn/ N1Wg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728427087; x=1729031887; 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=KtNVXjjPkEHV4V6z1fTmchaZwC5pGCKRUL8HB/rDqj8=; b=ga6yg5uaiAndW2JJf/KF7Eq0V+LNxM0+luRzEbOcitT3pN6AOL7+oUWrfVtVlUUqwX MUkXAQNTFOx+qsVTJO2fIS0NUbFbzVl+w4AwoCZ3tMmpECKmV5SU7afrRJqb+yzoVAKK bVz6uitjjDeDUcYd3kHdr/MpTcgSaNybkSjfTjw8cAhh31BZlNGgPBC1VxCQvuWMb/PG ah6htdYQ6K6O3lNAWfQrJlQQ7exoAE9Ep1hyjp8vz0r8TP/sVhVL9twRvRIlN37zrnUd 1do8kE4qlnwwug5LT3NRZIG86pOa9CsmyX11H4beTmsjI59b7iS1bN3Z/3fV5euWURfF oQeg== X-Forwarded-Encrypted: i=1; AJvYcCUIBIoJX5TQxPaEYN1mw8vrucfYfoyBI7m9Has6qpnNFZOFh0oYvdEo1gbnG5rxqEUSGm4DH5hXJg==@kvack.org X-Gm-Message-State: AOJu0YyQ1ldy0a5ElMmh3vQ21/jZ/dHx6zOLw9POvzj1VVbvH38B6w0p ZP/iGDsq2hiFm8/2bZnsHgq2TIwRBVo5Ex5nSun10/Jjqk10nHiArc/1kjUestk= X-Google-Smtp-Source: AGHT+IFTRRphTHkXxNtHYde9BX+luSQbkm62edHJi19X7C1fs0jGU+IiZUI2T3klTsFQ7+LS2WdKDA== X-Received: by 2002:a05:6a00:3d51:b0:71e:f7d:5c64 with SMTP id d2e1a72fcca58-71e1db648bamr555718b3a.6.1728427086877; Tue, 08 Oct 2024 15:38:06 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0ccc4b2sm6591270b3a.45.2024.10.08.15.38.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2024 15:38:06 -0700 (PDT) From: Deepak Gupta Date: Tue, 08 Oct 2024 15:36:49 -0700 Subject: [PATCH v6 07/33] dt-bindings: riscv: zicfilp and zicfiss in dt-bindings (extensions.yaml) MIME-Version: 1.0 Message-Id: <20241008-v5_user_cfi_series-v6-7-60d9fe073f37@rivosinc.com> References: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@rivosinc.com> In-Reply-To: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@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 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-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 2465C20005 X-Stat-Signature: xyqxwe1trm8mpqku5rub7sbcsdn9sw8f X-HE-Tag: 1728427087-464732 X-HE-Meta: U2FsdGVkX1/LP3ALkpqN8dRSNPCwdREwGU3Fh1MnHs5dC0YJEpcEg/rjShLxCGQl1yV4I1NdGoxi4qLhMEtpN0lHHzR36s9hAbRYJNbwb5pUc8aitMFJ7F0Irf5RZ4BlxP3ICnkLlAGI976/yZRmbAgI7/bV3aXTQNjmJiS2/QSOXiFAs5jypp0FHUd3+FvOiDT5VXLNd5xUmJA3O2K3FNBYJ5jtoGYie7iZrpHPr3rym00a29Y0T1P8uSEl1N/Z+SokFrliDnI5L1lXfZeUuUozE0XGuCB+VxbySlH3Xm6kY+3IUbhCxT1nEUhwKAJIRpggBPGGJEuBKBb/GwzanMZWIcYhCq+V+9y/9Bdn0ziFaxlSsmwikHBCLpYpn+ZSh8QfOiOmXP7C01RMh9No0cmDlJzPCDJR7LNkKLs8bp7sXeDro5bEYzgRz37N1Ertjl28XuNWnTsPK1eQ/SxGGy3kFCbUGH1s+px3ljlnWE2gYZZN3noFII8UgbXXRpMBuxB2wni3bnvYY7VhURp1JANNTu6x5G140lfoUcYPT4nH6Iy6Oba/usx2A1HlNekxXDx9p2DYIi+ZLcufNMuiNoyYLSdRY6ETjCG0GfT8tHK5gEprV827HVGC2rjhHUvedzC/TeCPmhYwqhDvR2+KEBNoeTROAeQYTPGcqydWPv7oVJn0FV6EmCIItWd9AZ97mxMIdHLagBdyg+8sN8wexyA44In+hl9+Kx6QAYHABGQADeaC28cQK7hAHv8y50bkSaUFGAn2yN3kAcsJNBmNwC2UwK2EVG4KPptsWOsMLXHaXPxw4xsB6BnYgDkIuCQOys2Ao6gbvScctdidmDjvc7ceyDqgJBpWua30o3/8An1WDSD6LJqWgsxcp1+vtyTT48JXT8JWNyjpJOMWpo3V2vVlcVNycjROtBp2vxeY7jiBfpDiyS4rJ+Pf7lmEw0CEw5a65Jt4PQmOwoBKzlY kCmlD78X hxLCmQrvxvhVphlHBX1zQuj33EUCEMMtHgdwmi2tlQQTCEsy/5WY/BTuBx3ZrFLL/8z54EY+KxgRj0dxEx4narLPD5Haj/MjVgWghQ49QF04mBVibsyVLPxTdOLb1LlP9QOuQkm2NN9i++t6WSgMAIkjFdP5a1pMy/gR0CgrcnvWwvq9HI2g460yNVdaL9p+rlF1LKnaCivThT4ujRBPSewpaiNWh3Kgh5jcxt46EUGwiEMDIK4zh6Lexssv4LQm/daVu66hZGMUwjSnjUnWG+3doz/XXuHWIuu31wI3yBv136Ex9xlD26ECmsGM2C7Y77kRnAuELz7yLT+1mMoAriKZqATjmnQl4zfYf0ocsUFrzlQ2KrbZW7SWyfPtzRla9aQWuJwXyS/8DaBLdcVr9aCMFWBTQ2zUC+8Euyyr2z5q7VwvV2hCNer5D06bJZxeGomDcNZ8fPPHRdpTWqrKnk41nFwD0Sj25cjYeGSzxLKxFI4PtzbnLrfmq/m3Z8B5v6GThlrVZvwdc3o/N8J2Pbx/s7F7M56Q96+wp7ES95/LqJUpkZ7oAtIONq+o5DaTuDUCfQOty/12PoQo= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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 2cf2026cff57..356c60fd6cc8 100644 --- a/Documentation/devicetree/bindings/riscv/extensions.yaml +++ b/Documentation/devicetree/bindings/riscv/extensions.yaml @@ -368,6 +368,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 Tue Oct 8 22:36:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13827090 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 10E36CF0429 for ; Tue, 8 Oct 2024 22:38:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 03D686B00A0; Tue, 8 Oct 2024 18:38:14 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F2FBD6B00A1; Tue, 8 Oct 2024 18:38:13 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DA7BE6B00A2; Tue, 8 Oct 2024 18:38:13 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id BA3A96B00A0 for ; Tue, 8 Oct 2024 18:38:13 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id DD46F80CB4 for ; Tue, 8 Oct 2024 22:38:11 +0000 (UTC) X-FDA: 82651899666.01.7F1A97C Received: from mail-pg1-f182.google.com (mail-pg1-f182.google.com [209.85.215.182]) by imf11.hostedemail.com (Postfix) with ESMTP id 4B0B740011 for ; Tue, 8 Oct 2024 22:38:11 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=VVZhsiDv; dmarc=none; spf=pass (imf11.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.182 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728426942; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=1PgKK5psJ4uqI8azQmoLe2esobQYgrwfNA+bFUurkQQ=; b=ztiT53RYzrgAVzqla8yjAhw/ynBtOzOG5l02aKvrK9HFEQn2Mq2x3Be7VBijEnKygYp3+d TVcJS+vMaxvfy9OPic1I/Dk3viIZDDvNU/15cmoL1N6HYyXFWQrqhY/3X3k8cRuvDyfOk/ 2dc1jUe0byXe51etroCbxaVUDYJiznc= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728426942; a=rsa-sha256; cv=none; b=rl6gpeWcpip/S1+09kW+MKtGnmWDOJCQoutQzAjMLBgo9mDJseJbaZOKiRGzRbFNO7gtuY ufoz/4yssGk0uSCNeghUJvtQnNgD34hwlqBKY4ySBVY/J2wgw+T8rjLtxfM2OSKNsWWbIE N2RkPzc/n/BHQqRq8LvCzanDeUY9nsw= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=VVZhsiDv; dmarc=none; spf=pass (imf11.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.182 as permitted sender) smtp.mailfrom=debug@rivosinc.com Received: by mail-pg1-f182.google.com with SMTP id 41be03b00d2f7-7e9fd82f1a5so2174439a12.1 for ; Tue, 08 Oct 2024 15:38:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1728427090; x=1729031890; darn=kvack.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=1PgKK5psJ4uqI8azQmoLe2esobQYgrwfNA+bFUurkQQ=; b=VVZhsiDvnrSlTs/17xVtjpuEc0xbEJl3clw8T6+XAsSWp9U2XY5h+qZ/3KrJitSm3W gXKpSeso6WuiY+S+RHtzEAY3D7Lz+CozcP/MTLGYjzQpS10/xUFUqUxETkcG8FSv5i2v Opd2PgQtQIjrAs29t52G7aPfVrrDFUtZeeqNjcUErE32tnRzny39TB56SpNwtMSagkbL 8/VC+dcarwU+HB/idbDh5GgFT0j2d1opOB/fQlO/nDQlqtuvrGOgZr1DgnApr6sN3hwt zGK2w2g+x1kgLVKI4JrW/Q68GU2dB6YC9+hdfoRxmo/gwlkdn1Nc6NfIkU1UzZSa/kNk X5Qw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728427090; x=1729031890; 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=1PgKK5psJ4uqI8azQmoLe2esobQYgrwfNA+bFUurkQQ=; b=dLyVJr4Idm1AEr/Azqhy/CH56I799LGywfYvcArem14J26PqjUP2opH6Oyqmqcyhg6 E+Qu6JJRFDfI31jBdUhyFY5El5GmoSE8zvL30vhWktmD0EIkO/HcZKSmqHgcaUSH2ky+ 4eb1ihSJXA67Kk7aXUBji6V5y9JBSjIHSIOveRDJcOJZ/K30WQ4HetkBpe8/e7dr1O3A rSyPpYB6doZZyK8q68H4ljdOZo6b4zbwEdgksDN0xZcIInUpEkYnZDXFeiafJYjcH7+X NrhPJSUUmQl5s64kOPzpFmk3v+UXvph1H88Db9gDolmvVi7oSZ7uRiGsiQ55XhybUi8g QxvA== X-Forwarded-Encrypted: i=1; AJvYcCU+cYB9GT3jXm7OxW1zyW8F8YhoTbgxMy5Zz6Rdhfwl9YMm6n/K8iuv+N9FphsNdESSXlV4eQdu5A==@kvack.org X-Gm-Message-State: AOJu0YydUUsEUZmCfA68oogSycLFwDZ/41wr+IEON2JYDs5JldBC79uC htvxSYVl32FPC/CtOp5Iqb1eLS3n12iZPBZhP48mmGKvUT2xqKGKJhCbiGh88b8= X-Google-Smtp-Source: AGHT+IFY7chdq/RlO1VqqZI5iRB44TlQ/FpJUC1y/VZKQga2d2VHN1qSiIxLmjb9SvMQ9TpE0KT+Pw== X-Received: by 2002:a05:6a20:9f9b:b0:1d7:f7d:5cf3 with SMTP id adf61e73a8af0-1d8a3c1d7c4mr777890637.25.1728427089845; Tue, 08 Oct 2024 15:38:09 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0ccc4b2sm6591270b3a.45.2024.10.08.15.38.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2024 15:38:09 -0700 (PDT) From: Deepak Gupta Date: Tue, 08 Oct 2024 15:36:50 -0700 Subject: [PATCH v6 08/33] riscv: zicfiss / zicfilp enumeration MIME-Version: 1.0 Message-Id: <20241008-v5_user_cfi_series-v6-8-60d9fe073f37@rivosinc.com> References: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@rivosinc.com> In-Reply-To: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@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 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-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 4B0B740011 X-Stat-Signature: qbdo3smqq1cknn1db6gjfjt93m34eyue X-Rspam-User: X-HE-Tag: 1728427091-383568 X-HE-Meta: U2FsdGVkX18Tkh3qS/QCHpQhr0q5lyMV9VlQPIO90H2jMvpYPu/M8ITwCe/cXZ+saOFnZiE1olTovtoImQL1QxHJFNSiaUdLZcG23SoOgbnCn0M4d8sHkTWgx2UTw7nDM9YDY/tjInSu+dIhBYm5ponAl5lKZIXcGL8YhVlPSawo13UmGWyGJT2MfCd3AbEBE8fOVwQOfU73U+gWVMvcfPtfI4FBkFndBs9Gpz7feBdFDBOv4301853leJ0tr35zR+Cj4FcemCXTXZzRslxOdV6buzUEJUf9uRoJTyEO2g/Bfcg9+obH07hwVVEr96OqqSY4N6vaagpe+ps5F9RT0oyp1S6p8Wh18wuJYhpLUBCTelnXUkB/zBeEfcb9SQEXLIZ8nZOkI5hBmVrzi1HnmVK/TDJxYG4VSy/3iMpw8BtVyr1gBxKGwsqNPFKmSLPlQ86vGk17FNNSm3faDGU5qkqn6kxvvb1/pP2nPDEn/sJQjyMmtBgB9303yTjqvS7aMb+Duzq8rsaw4Or1rgqGfmvvpA1JmYMzZhz74UMZ/8FQfHKfM5Fk4MqxnPqm3BqDy9UsBANPYiHLxQxDXN4w6Un0XSJonoBzpfsX3DsfdG2E5JCcjAZpMmv6GccqFZlFGnuI67CKaP94vWQNLd88B17Tnbaso8n0UK0bV3je38L2aRj6qsmQ67Gq59Mpl9bLCm2/4pYvjloG98x2vboIO5vdChzLJcwbwS7lm4VDpGIlpvWDZu4UlN94z4+UpT/Kuzj5AVybwqgL6Tj8h8i++QeOYXMVFsiVAgKoYBtTc4cIbmxlKv3UbaRIjqdXCsdCf87548X4846y2UhRpUmxIB0ixxffLmotzdM9vEQJyF/9Nsii0JkYHh6TCXl95DPPShaucFMXs4JBxSRCZ62CjMiSjmXpggDrBAbI8ofysxONQUo+rXnw909xToGxGgeTlFj0vb3Ky+H02suqsyx 7kcp6/JF cymyqb7wOS2LStOmgrS95qIGTYQdc00SRMTrVjlwJqGcVlP80iKsdvsIHpGcuP6KB509Tms9ZmkxuvAjBNyebFQzkiqQN4SwCZdxSlmSFDuSLTnzcaLkhReB79VMEoICegJC/r9OhDH382fcG/RlnVstgVmWSJb0XNjyFuGohcBgYoak8gTvNRszHCiMRkBTToNB4Il9LbpYWDbYI42Zqzshp8tGCcPKbyLPZA3l8kzv+QV+15Z9qp1pW/mnjscSikXHkmrGNAL9/gDq207qFRAFFYewas9AP+4tZL69z3ICMG/VoTLte/n9/yM+hm/UiZHy29XnsQc4jmuPRyT4Fia7ajtZfrGtwVpvbUWA7OeqLhMge27EJklRtN3YQIuvb5XysB7UMneRJOeQs9yGl3WuI3sPZP5c5V2t1lkgvG01I7ualVVFKungUteS+nAEhqyBNpPA5fAsDbEWW3RJVmtIp4irk5DZ9zQMKXgOnKluaJjZAKTy1qBP5BC4mBcNgbrPJgWSkidohg4PTtX7Cfgltmd0okPVKEUYjtM8m0j0JJ4uCk+cABjDVN3Ri8Hc/oCbVIAcotaPrWDp2PPDZK0HdK9ix708i0UPGL4kTuIeNtkI= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: This patch adds support for detecting zicfiss and zicfilp. zicfiss and zicfilp stands for unprivleged integer spec extension for shadow stack and branch tracking on indirect branches, respectively. This patch looks for zicfiss and zicfilp in device tree and accordinlgy lights up bit in cpu feature bitmap. Furthermore this patch adds detection utility functions to return whether shadow stack or landing pads are supported by cpu. Signed-off-by: Deepak Gupta --- arch/riscv/include/asm/cpufeature.h | 13 +++++++++++++ arch/riscv/include/asm/hwcap.h | 2 ++ arch/riscv/include/asm/processor.h | 1 + arch/riscv/kernel/cpufeature.c | 2 ++ 4 files changed, 18 insertions(+) diff --git a/arch/riscv/include/asm/cpufeature.h b/arch/riscv/include/asm/cpufeature.h index ce9a995730c1..344b8e8cd3e8 100644 --- a/arch/riscv/include/asm/cpufeature.h +++ b/arch/riscv/include/asm/cpufeature.h @@ -8,6 +8,7 @@ #include #include +#include #include #include #include @@ -180,4 +181,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 46d9de54179e..10d315a6ef0e 100644 --- a/arch/riscv/include/asm/hwcap.h +++ b/arch/riscv/include/asm/hwcap.h @@ -93,6 +93,8 @@ #define RISCV_ISA_EXT_ZCMOP 84 #define RISCV_ISA_EXT_ZAWRS 85 #define RISCV_ISA_EXT_SVVPTC 86 +#define RISCV_ISA_EXT_ZICFILP 87 +#define RISCV_ISA_EXT_ZICFISS 88 #define RISCV_ISA_EXT_XLINUXENVCFG 127 diff --git a/arch/riscv/include/asm/processor.h b/arch/riscv/include/asm/processor.h index c1a492508835..aec3466a389c 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 b3a057c36996..70803aa66332 100644 --- a/arch/riscv/kernel/cpufeature.c +++ b/arch/riscv/kernel/cpufeature.c @@ -317,6 +317,8 @@ const struct riscv_isa_ext_data riscv_isa_ext[] = { riscv_ext_zicbom_validate), __RISCV_ISA_EXT_SUPERSET_VALIDATE(zicboz, RISCV_ISA_EXT_ZICBOZ, riscv_xlinuxenvcfg_exts, riscv_ext_zicboz_validate), + __RISCV_ISA_EXT_SUPERSET(zicfilp, RISCV_ISA_EXT_ZICFILP, riscv_xlinuxenvcfg_exts), + __RISCV_ISA_EXT_SUPERSET(zicfiss, RISCV_ISA_EXT_ZICFISS, riscv_xlinuxenvcfg_exts), __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 Tue Oct 8 22:36:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13827092 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6774BCF0429 for ; Tue, 8 Oct 2024 22:38:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6A6086B00A3; Tue, 8 Oct 2024 18:38:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6585B6B00A4; Tue, 8 Oct 2024 18:38:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4A94A6B00A5; Tue, 8 Oct 2024 18:38:16 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 2B0F46B00A3 for ; Tue, 8 Oct 2024 18:38:16 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 22838C0C7F for ; Tue, 8 Oct 2024 22:38:14 +0000 (UTC) X-FDA: 82651899750.21.AE5BC34 Received: from mail-pf1-f171.google.com (mail-pf1-f171.google.com [209.85.210.171]) by imf20.hostedemail.com (Postfix) with ESMTP id 0B9851C001D for ; Tue, 8 Oct 2024 22:38:13 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=tsvZg+ex; dmarc=none; spf=pass (imf20.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.171 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728426983; a=rsa-sha256; cv=none; b=kDat8gxVbXjXp/At05vaaNFtP1Lyc+p4N52mRtvArGppdLeOK0z92HkLDaNod1UWFkjr09 spv2jvkJJuRQ7Zv6SNax9+SfouMAUiTJhEavJqbg8n0SpsbL3GM0hZw/vF7e0ZBfVy5uT3 RRa4PaHpa5h6Ta4sf5nnvfkZlzyATow= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=tsvZg+ex; dmarc=none; spf=pass (imf20.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.171 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728426983; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=YzRPf3gSm1oBJZUnyk2tQznTd/0pm5tuC+PNjTYOgOw=; b=OR8TWUUz29LH0THh3r91pqzvQ4HIWPEe6PlpCEF9h84ZO55Hp7CHRiTzqgQESfRgfuBaaX FMfY4TqZLVDJVE9V48LuIiYpWpkaVcDkzqbP1+ggk5LSnCKTmkQ/rP0NSRDrgH1/udh1jx tgE+gCghelj7d7Tfndkc/vmIm7I+9XU= Received: by mail-pf1-f171.google.com with SMTP id d2e1a72fcca58-718e9c8bd83so164856b3a.1 for ; Tue, 08 Oct 2024 15:38:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1728427093; x=1729031893; darn=kvack.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=YzRPf3gSm1oBJZUnyk2tQznTd/0pm5tuC+PNjTYOgOw=; b=tsvZg+exdHZNRvMv0yxRaCNuHIiuRvj+9tqfs0s5WNUjaBPd1/nszMB5L9GISsFYFF EiCHt5y9C7VmgffkUpfcIR/LYXQ3/oqRvoGvEPX5iW4lDgp096Mf4PZD5vAHU1LYYW7x Csku2Nycxi74EANt3azvn98Cx/ALgfvBZPIwpyEvQB90RvyOsqKSRQnL7lbC9+/E1h1h 3Rrmmc6KExrtbr37I7DCbwK6Ka/nm7wG+Kl1U9Xm+bOLj7h2dYjcfyH5V4YADpgsEjPG e4sUqA3OUgYEdmAC2+qpoxNq7ZdBKCVKaK0Td66RRod4JU+KVbuSua0PycFlBcgr3QWW aZIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728427093; x=1729031893; 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=YzRPf3gSm1oBJZUnyk2tQznTd/0pm5tuC+PNjTYOgOw=; b=rKQZqRqtjYiscFfl+obxeJNRwHRXAKtziE5OoFhP9mZM4FF43gGhKPZnmuxmMN+2rB lcmjVG1yWud76ybrIu2LkP4qRnr0CIzac5DCDreNjKnbOF8pMhqsVe5400bdcptj7f5J dxKrLA61wH+O4WSs0+TuaIFk4/mGq3+SkIybTLilFuL15MZ2vg0YmnMJeR3VWAl3Q+L/ 7029ziz11dHb7Ze1sViLvFMEwITCHiPM2ovyqAPu5tsoq8M/5idOHGOEXpK9//UGlphR NMNaRBtRVqrff0x3Sh2xE8DpUUXP99RJwXLZcInT3GFCvvuxcYc5oAqBBJ0GQ6Fc5BE9 zGiQ== X-Forwarded-Encrypted: i=1; AJvYcCWT8h1e6IhMOE5asXEhoKXWpl53Y+tqflAB6XPsB/6ZFWaxjybl9BHzOc3ojJ/zXWLfMpbyZgez+Q==@kvack.org X-Gm-Message-State: AOJu0YycP2fv6H4WaTerfK/LSSXU4Pg7pO/G7oCYn44n8a3jEpTPtnL6 /hg7/ChXRWlvpDvtqtcAzKmZj4ov5JQY/biFoFYPcv7WsoDP9CU386CqBLYmi74= X-Google-Smtp-Source: AGHT+IHJXkTKywlrqzkXfqvL2/GTskz4hDTJPpvFgwQjWfvgYkv2MchiugGtJMvE5DvtJQZv+TfEgg== X-Received: by 2002:a05:6a21:1192:b0:1d5:118a:b53a with SMTP id adf61e73a8af0-1d8a35401e9mr1074844637.21.1728427092833; Tue, 08 Oct 2024 15:38:12 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0ccc4b2sm6591270b3a.45.2024.10.08.15.38.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2024 15:38:12 -0700 (PDT) From: Deepak Gupta Date: Tue, 08 Oct 2024 15:36:51 -0700 Subject: [PATCH v6 09/33] riscv: zicfiss / zicfilp extension csr and bit definitions MIME-Version: 1.0 Message-Id: <20241008-v5_user_cfi_series-v6-9-60d9fe073f37@rivosinc.com> References: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@rivosinc.com> In-Reply-To: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@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 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-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 0B9851C001D X-Stat-Signature: 8cwter847p5e1htqgdnctht8pcrak4oa X-Rspam-User: X-HE-Tag: 1728427093-423542 X-HE-Meta: U2FsdGVkX1+H7ZWNbsDHVP3L8vyvYLUM7uTltYK4ucVA3Yaof2EkAtsWGehzavljI7Ha5CiYpAbAGvpRNJWL7TlKWFZkJuit/NN2jpp117FyX2EXBO3rdQyk1c2J58C00qXoqnMIdLsl/sD+rACpKwrNyZpz4Qgeb1BVejK4lKzsjvW24IPyPTFdAm1EopP8RlZR1qt2poTjZRpfghxWw3uNDJUpvo4AePR4IedvqMgHckZWzK4GpD0Y/TPSICDVG95fvdHg7V6IDQ/OKCTUNR4U79FYg/N+pAigNXCz8qwC1zURSRqVq/YL+mDn19D2J/75Guz7htQxdKWVY0xeXf+UqIZRtXan50KwzpY4gqwQHJA3rp8LWws/B5mPEapkxREuLVbkiW6RHowy2M5n47Pcw84OIXdGPJTOQGRGKlE4IvqY8n3+qVUGJaaVBi0Rk9s1yMixuSYW3HCTfb6ls1oC+zdLtGxTpyKB0eXkaymS+eX+/WCIEhN4GbKDzhyp1pVaxVC4tKi49n1TOLsxOMcfDgGuRBZcc/xd+RWYtZv4kbQVj0hv84am+r1Pcd2uwknLPvd5yd7b70jVhH6HOF0bWSTzUMmZvcioGKvDdtYRRo7QAhWSRC6FUImLzOvEq43evR2zd+d0zdqV0RUKhVQ90foT8kRUuk89P74V0EJbCGhNzyAfrgQTFVmaIbec6OV6pQQpRC+/wftbgmVimP5xuU1o7x7Ilq5vwH/EzFbQWve6d/fXz9ZaS+nQj3jEC0GmaJai/yqcKAt089IH6XEt8F8iiu+I63tlBqXXnPYRJJ82YXUYzwH+FVRwbu3PTkirOGoaJoZEOQ9IqR4ZOZ2UwUSDXlzBxYGpj7tRBTcZ94iC/fJVhZTSz+SXy+BvPLXe5rX8a0rf3/mTG5mUm1BeQWbfTxJOzVHeriQ9FlgvluytN9/IiwxMuch/lihaD6LopObD6CGpjjQ4Y6S Gixhd1fi 1ZtUfSyriQlA5G9mG1NB9W/7PosjIfnyh17ETiQn/p1N7JeyI93/LITP2OOJgwDAPQX4QSMg2tJtKNVCDRMeqCq4daWXgfxn1EXc0lcA0JX1GGgESSxltKHVPid+UwlU0YqC+6r6FWF+35bvoZtclPZ7LzurFHk70SyXEp+IHfcs5N6X43FYdZFLe+Nxg2hU8kjWM5XQws1Xtc68M4oiKty1Wj6ECzKutV7Ptwf7bHR198zVNwoy1vPHy99y4X6h1vhKEKCHFCTdV0ta8FWiT6G2SSUb2kN98r1GbuTdf1ekWhyXOzdovZvfOaC1zIori0du1NB6QzxA1p+OCknLFQuFYpwRgO90qXbTwBT1VAFDs1jBhBtXmHtz1qQ2QgYtelEMA+O2L/XmeuUL1aVvcgPfhqd8Ox2mKopjccFxaJOxnWzjo5dzD3CnSUX3+UZWCsuqlNoDxLLB/DlwI6N92ijswg74NKehkCN11LP/SPPbJMrVou56npH+aMf8Yr5r7dSNhBcpeiGY3oneDO79tHYnHsSU7IXxxz6cYq3j41kaqavZbmJJ4tFoGRjPVHx6aVGkrk7iEwdC/T3Y= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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 25966995da04..af7ed9bedaee 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) @@ -197,6 +206,8 @@ #define ENVCFG_PBMTE (_AC(1, ULL) << 62) #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) @@ -215,6 +226,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 /* symbolic CSR names: */ #define CSR_CYCLE 0xc00 From patchwork Tue Oct 8 22:36:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13827093 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7FBCECF0429 for ; Tue, 8 Oct 2024 22:38:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6CE016B00A5; Tue, 8 Oct 2024 18:38:19 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 67D096B00A6; Tue, 8 Oct 2024 18:38:19 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3BDEC6B00A7; Tue, 8 Oct 2024 18:38:19 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 1C8966B00A5 for ; Tue, 8 Oct 2024 18:38:19 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 792B2160C53 for ; Tue, 8 Oct 2024 22:38:17 +0000 (UTC) X-FDA: 82651899876.12.57A8B41 Received: from mail-pg1-f174.google.com (mail-pg1-f174.google.com [209.85.215.174]) by imf28.hostedemail.com (Postfix) with ESMTP id DD462C0009 for ; Tue, 8 Oct 2024 22:38:16 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=gCJV7N9n; spf=pass (imf28.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.174 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728427053; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=HqH/AhXY0hzM1+WX3zWeccxGSysMowKCTsAPVGtoEZU=; b=nxmStENeVowI3AvwYfkalGaFKql8ZeW+lR4SHgiesFGB/AklyhnpH4CsB/hP9Ek+V5Ew0S w/1D2mqa8qHS+uy6OE85zsRWa/WTshkRfnjp5lC/5M8I3M1IoF9v6xGglqZwNIO7B2JDjD FVGfDR8UbSTmyntwSQP2jJaI8VYvM40= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=gCJV7N9n; spf=pass (imf28.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.174 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728427053; a=rsa-sha256; cv=none; b=AiKpjZFE8YpRoJYxDIcsNZTrnd0D7xV/oJmxL56KHeBEfoXhwtKL7DazY7xuOs9dHULcBk bMQTZ41ofF2Q7EMfG5OyQTU2sDxA0opjq2bIt1RtMd9SA92SOxSN7C8+ZIG6WfaaXJ3roe /DnPiyGrF+ptU/wxjQFBIZ+wp+jWMWM= Received: by mail-pg1-f174.google.com with SMTP id 41be03b00d2f7-7e9ff6fb4c6so4606760a12.3 for ; Tue, 08 Oct 2024 15:38:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1728427095; x=1729031895; darn=kvack.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=HqH/AhXY0hzM1+WX3zWeccxGSysMowKCTsAPVGtoEZU=; b=gCJV7N9ngTZqqIng+KYwUUgyuWhtOhNehnJtZ+LkTk5i/qWjq4hpD9V0ghJVH7P07G fvy/PhH/vJhrbG4RC5pA3aCBolkWGRoqLQsd2Xj0ve35iwkYVIy9gjTUSNonuQ/ZK8gf HQtV0PGgryYhY7mKWo7KDbfyvdK7vjIJOmEL0t55Et9givcnNetrIFb+U2lCTMkO3+IE F0+G0+M3phbfzvU5LzJMEWpyQZ0bqsrW/d22np0su/hxJjP7h22NUsi7hyU7P4Fmg2m0 cLQzrEuSfQUFVKBETbkdja2LQRk9+Etb1FgCZaEHk57jMq9ZYXU+RwlbCj7kLi4LqHls 0DiQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728427095; x=1729031895; 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=HqH/AhXY0hzM1+WX3zWeccxGSysMowKCTsAPVGtoEZU=; b=gbokeeZNlph4cAYuUQ/D+Ab9ysIpn4aicPLWIkJIl7PEm7gSTwBdh+xikiURyk1sL/ hqWEM6frG8PvrNZMb+6Uf5OFzpFD/QDpbAqo7LuYEC6gN0Ff/fFkUGjBVtqfNDkQy7T4 G5PPgDLhVlZnCX0SvA2IV+Cg5QpmubWVom2A51FzjdJiDJiwyqy/w41W7HWgde70sTa2 zaep8TdwJUE00+UfWnDUprAULvL4C1d2CIc6luXwgleq4IWqfd6WbMDHyNg8CevTxUqH Erv8TgZK7S7aMpyJfQL1aJjTzoLzP+Tvik9XE0gg4ourcazxgXQBijbQ6kCSLM/xkbTo 5S0A== X-Forwarded-Encrypted: i=1; AJvYcCUe8ccmfBeiNJQO0jljTbtxigjU2WNS6L5B2WWBnWg1Nju7Y9JukFf7WB65qDe8eki27zAxRgKJ5Q==@kvack.org X-Gm-Message-State: AOJu0YwYlBtPj3dtE5+JGQunYwVo2W6RVcuU9NONjkezIK2dc7yhf58f UD5FSPm5kb8Rtk9xLnS3HfMRVP2XYgVkRk5b0XRdJMsAv9iGTovToNTUsvbU0o8= X-Google-Smtp-Source: AGHT+IGmH0pAT3k+B512WMEtl+jnoszZMXZJopQwCiYj/5o19BiNvFPol3OfLSxKWlmI805IJexCZA== X-Received: by 2002:a05:6a21:78d:b0:1d7:7ea:2f2d with SMTP id adf61e73a8af0-1d8a3be37d1mr856278637.6.1728427095593; Tue, 08 Oct 2024 15:38:15 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0ccc4b2sm6591270b3a.45.2024.10.08.15.38.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2024 15:38:15 -0700 (PDT) From: Deepak Gupta Date: Tue, 08 Oct 2024 15:36:52 -0700 Subject: [PATCH v6 10/33] riscv: usercfi state for task and save/restore of CSR_SSP on trap entry/exit MIME-Version: 1.0 Message-Id: <20241008-v5_user_cfi_series-v6-10-60d9fe073f37@rivosinc.com> References: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@rivosinc.com> In-Reply-To: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@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 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-Rspam-User: X-Stat-Signature: 564z9d7c8cc6337nadci38oguk8nkkuh X-Rspamd-Queue-Id: DD462C0009 X-Rspamd-Server: rspam11 X-HE-Tag: 1728427096-710553 X-HE-Meta: U2FsdGVkX1+oqSegDHycOjyNGtDz6YvNPIiEhlRw3uM+jyQ1bA2xVRvOCdvDzP4r9JDFterWIr8SKYzXVmqKNYWBmmTvXUMM+tyNSAriZ4htO4XF3qRqWt1V6XEJu2souOkZ3h3AfGuyFI6tJdd378HKXHy/UYGNie/Mxpk9f1X7bC4fm2ZJ9nVXu2WupJ3MDvqlE7umJdj9SyLcavMqPKMVAS6Tp0O06dyT9Nt9tgX//qrGmkDC4VDW2idUr8PsBCIH/djkkw3ux39D2xGChnIocxUtuPLbxcOI6nLOjc2r9VxsCpRtmfN/KjREtX7AD4g/NJnlBQWArYQcl96EGDD7nxVb+FCzasly/WZWWQUqopDeIPWJDWx+10BWTv1qsIBVU2Bb9HyeWDZP8FwKyluxCWzs3xGGBjFBLzkvpS9BT6hQVhPWB195eW2YOz6Q5R46jioOW5AS//lF/5IZvhqUYrQG4cblELcQi3QzM9ONzhis6vHhS4imoEzQwdkXK3QwEQw/G2AHaS0DDXPZqV3szfcFYzj+/gFM+izTfXJonFBxVa/o++1g1kltSzQnQNRNG6/R3HxR3FqBtvehorA2kAkpkT/5KKfV/tFL5i+QfLe3RHcVmBHp2V/z46jIcX3gSa2IAr/De++84+c8vvB2b4QjjCZJE9KtyZCkf2BZkEGb1KxkkuqY4gB9uvK/E+rGpwzAdBuJEslgCl78jCgH2ShAeA6ws35R5+ABHQVOOHciTnThz6fhdBeZYfMtvhwdMC5xoRe/Kk//jbnIp+2S+GqotHL6sJmTNbB939i2slE737slAa9ie7xUGqaLf1bw2gSqM5zaQh9n9Cql9LxcSjOHn/+ciMducKUP/+ZiJkE/l8BGcnz81UEGdqhRj1xdAGW6Pdf82IToalEaTblhADCuN9cCq9I7f4eyDStDL98zQb4J24m53DS6DGProPu//kVl/m04gl0cTS9 Sby2D0mg pZLVuhcISKNVbmc5eB0pwsKEmEQozIHr1WSUzWVdiykCOFfRW2EAUC8BggWSmS1cPWjbl2H0FXvyG2ANjP1eUe45toQriDPl8k3qbCZlyfernE+Hx9W6OkJmbsBG30YL8nccj54oxTZTUCZYVfP+mO7plNTu4Y84FT/Aaui6FUcmmBgekizjdTFAoEGY4upfsmm4nVDLfgzCi8w+dRKpTXtdU1Pqcb37I5GTMm6xbJZuMUGHbnRZWjTqDXY7ow5jJtFH9i/LS8YzXGuoI8YJEgU3oNlrvsvJSDrjzp2fw8KCOpE9+7QQfr4VYxXJuAROjR0rwgUbMfueCGRwMaxifHtXT4OqOi9vGiS7MVCYty49DGdsQ1U0+H7DjN71ukPCsZlN5mTa9xQnhh2vFvxbZTK4uWjzJ8o3UorLOAsc3AMtSNA5NQ8ma4uPfSpcGVhd4lxgbasXX+UjVjo7RxBt51f0DDl65slO2Go9m21MN4Xp6c3n6CclLqj1VThH92LyJCAIdodldjhjq0WF9s4bX7jV36n/bLbcZWU0r/5kqH4DEsx4= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Carves out space in arch specific thread struct for cfi status and shadow stack in usermode on riscv. This patch does following - defines a new structure cfi_status with status bit for cfi feature - defines shadow stack pointer, base and size in cfi_status structure - defines offsets to new member fields in thread in asm-offsets.c - Saves and restore shadow stack pointer on trap entry (U --> S) and exit (S --> U) Shadow stack save/restore is gated on feature availiblity and implemented using alternative. CSR can be context switched in `switch_to` as well but soon as kernel shadow stack support gets rolled in, shadow stack pointer will need to be switched at trap entry/exit point (much like `sp`). It can be argued that kernel using shadow stack deployment scenario may not be as prevalant as user mode using this feature. But even if there is some minimal deployment of kernel shadow stack, that means that it needs to be supported. And thus save/restore of shadow stack pointer in entry.S instead of in `switch_to.h`. Signed-off-by: Deepak Gupta Reviewed-by: Charlie Jenkins --- arch/riscv/include/asm/processor.h | 1 + arch/riscv/include/asm/thread_info.h | 3 +++ arch/riscv/include/asm/usercfi.h | 24 ++++++++++++++++++++++++ arch/riscv/kernel/asm-offsets.c | 4 ++++ arch/riscv/kernel/entry.S | 26 ++++++++++++++++++++++++++ 5 files changed, 58 insertions(+) diff --git a/arch/riscv/include/asm/processor.h b/arch/riscv/include/asm/processor.h index aec3466a389c..5a8031384021 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 ebe52f96da34..12263cef7518 100644 --- a/arch/riscv/include/asm/thread_info.h +++ b/arch/riscv/include/asm/thread_info.h @@ -57,6 +57,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..4fa201b4fc4e --- /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 e94180ba432f..766bd33f10cb 100644 --- a/arch/riscv/kernel/asm-offsets.c +++ b/arch/riscv/kernel/asm-offsets.c @@ -52,6 +52,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 c200d329d4bd..8f7f477517e3 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 Tue Oct 8 22:36:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13827094 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0823BCF0429 for ; Tue, 8 Oct 2024 22:38:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 19CE56B00A6; Tue, 8 Oct 2024 18:38:22 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 14DC76B00A7; Tue, 8 Oct 2024 18:38:22 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F08C06B00A8; Tue, 8 Oct 2024 18:38:21 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id CDCEB6B00A6 for ; Tue, 8 Oct 2024 18:38:21 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 7F762A0298 for ; Tue, 8 Oct 2024 22:38:18 +0000 (UTC) X-FDA: 82651900002.24.95DD7D3 Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.178]) by imf21.hostedemail.com (Postfix) with ESMTP id AB1531C0011 for ; Tue, 8 Oct 2024 22:38:19 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=cP04C43z; spf=pass (imf21.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.178 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728426965; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Jf2x3pfGFzhqVTB4n6XHdAtkUN+75WMOXWy8ywZVarA=; b=7dpBm4e/LNFQ+BwXMREYL7FcNjWWKIUcEkne3JqV+DjbSFXr4EMR4Tf/nLzlxixf4sxJnC 0Z1nnXWzJaDJpv/V8B+rREWrMDU+/te60BG9Pp8moO/Nu0MB5QloF4J8HMalQmtvHvLOBl 7Hi0RHXaxTEkrIXBwxTlglS+UB9MtNU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728426965; a=rsa-sha256; cv=none; b=yktkj+nGEooQxG0RxKha/v5HhK9vaxpozeEDSezqMJEEFz6UDilbv2VpqCcThqqH8lUih+ W/6qiwBMB4kQCFKKIfEVIsuEbuJGbui4194zn5rvoEsPN+a+JIhCqTMY4PUlQZnfK7V9ox 0dKsS5vaZpm8WvpOl0qbPUkbxwD5lok= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=cP04C43z; spf=pass (imf21.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.178 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-71dfc1124cdso223510b3a.1 for ; Tue, 08 Oct 2024 15:38:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1728427098; x=1729031898; darn=kvack.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=Jf2x3pfGFzhqVTB4n6XHdAtkUN+75WMOXWy8ywZVarA=; b=cP04C43zN5DPrkGusGPilLbRY7Dr1/mPTqZW5uufXHUsAXNmK3+DTJ/wbw1rsDdDTg uANMOywRLzHv9Jbw57JxxmLK8LAUKVZONcNMCK0gVX7jTK0be5ERAwEkxGbkewGWQA2k A7LTOXfbS80KKA1KrLiYfqxsCwbXSc08t2rqp2P078few4WwgiieGck4iErWIiopkf7V sxhNb8Er0SzT3LUSwpowk1K2oZ8mSLMZHjR9+1FmiNCY31lSm2RvB//1JgHQEMY5smbG 7F7jAkt2oIv9ZgRWYEFnFSZN/HOcnqOy0FUszGeXoV44c0uuGeY8BKxqkIlbRLSSY7It EP1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728427098; x=1729031898; 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=Jf2x3pfGFzhqVTB4n6XHdAtkUN+75WMOXWy8ywZVarA=; b=tEtM2QnOWSmI0lEqWIPD1M60JJufB2KDGO6EhTK2Y25MFVkqstHEj/WwYetr6awA4u u0bnO6gDu4f6A80CUcCWZhHTZavn3nopslTUXT8BqM/eg5HUtwbXCAaYhNrSmA1rtmV+ IXvDt/r7lHGUC+Rcpskz0s0cR4XdenuS1ZFuQZzhQmD4uRTA0ZB+4gwOZvzhTS5jTTII byyYTaUOezqjqoyvRnDOhPZN15mOt1Yjx6F8QjiQFBMcCYb2kjMdYi/24h/kx98w2kQd fjQKPlHHJbtqOu5Z9BQk5WPwPRqwer9oHdM0yttE5FLGbeek2vt3WmNCsoiaTc+fNRl7 7OTA== X-Forwarded-Encrypted: i=1; AJvYcCVtIq62af0Gi9hIVr5nk9RW8vM/Z3zZqItkKcCGbhmfU/gbGgUhLJefP7y+PLdpRWoqEqoOsIWyMw==@kvack.org X-Gm-Message-State: AOJu0Yzd8Tmse0IajYIrIumhDnrlA+ptuiNb52ZWIbgXZvxzcCdPolEQ Pw2bK6eZAhucIpxVGI7ht7P5qgRIa7ioy5CNFMYgcYJ7riCHhyrqXakCZ8nFB1Y= X-Google-Smtp-Source: AGHT+IHSAqsobXk9Y+XBm/TMrjzhYSrUi/BmV9fcp34QsDNZFgu887tqUAdrSExRA5PWk074Td46aA== X-Received: by 2002:a05:6a00:2e08:b0:71d:f459:6df3 with SMTP id d2e1a72fcca58-71e1d6a47f2mr971683b3a.10.1728427098329; Tue, 08 Oct 2024 15:38:18 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0ccc4b2sm6591270b3a.45.2024.10.08.15.38.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2024 15:38:18 -0700 (PDT) From: Deepak Gupta Date: Tue, 08 Oct 2024 15:36:53 -0700 Subject: [PATCH v6 11/33] riscv/mm : ensure PROT_WRITE leads to VM_READ | VM_WRITE MIME-Version: 1.0 Message-Id: <20241008-v5_user_cfi_series-v6-11-60d9fe073f37@rivosinc.com> References: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@rivosinc.com> In-Reply-To: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@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 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-Rspamd-Queue-Id: AB1531C0011 X-Stat-Signature: 9qupjh4ewx19kafyk37ogtmbxbd11rp9 X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1728427099-319115 X-HE-Meta: U2FsdGVkX1/tKbU9d0ZOrX83R8sl+rpgdCgtPzuiEp/tXif0coLczFVhdFgDqlM6vsvKRBzWfS5rdVXdbGkTSK0YKsmCI9BlpUw1Jg3GYAVfoi9J+nRUfYyYftJobtTlCsAeah5ouePXc7GyonKbBPEfoJC7ew9esvpJCWJg5s9kzeB33KvKSpLIbBILCFX63XPYDZOIeQFOt0TE9o5QTY/TW2+sM8BxMaaMIrGQyg6KmWLm2+r+abRCIzKy91NnrBnNLj1esW1vQ0QxCNjbIWHKzdyGTJBjaQAdjMwwKgbKKy6uT2y1bn51bI5OJ7WNeJmn+BeQ6YJZ0+FOUT8cuFHvYNxn29u0fcrm+b30wYy9NSaktYIGSDL5WuJXxHIVk5VwlH8BzqohtF6wKl9iePstGM7y9jb/AMrf2ab0c/lwbJs/PNd401sA5tcNmOE+emLY5TVNLgf5NnhjZ54/hJdRIDPmWQma5zzVGIHsD4pQPnQ78puBVfKsmYZCLeZmVGYnJ8N8T7pOIZuQTkbDmnqHqnKNOXb5hS7i9eegMWSgNKt5fDR+n67n7+b2cu1r+XkQPddjSUhpzbIBJd7XkC1yU2kNN8sVBeyVhTnG2+EduYnQqgeX/XdHtZHIWVs40nsF/xKPOX8EGTMF1F433SIvoiOri6IKhHbRcHvkcUSeG6npTvtRNpBpLZ87SqR7c3pNhv0wqjBUg9N5TpRzrtmZt/2RP4Go3F1so9DNso48R19AuFaH14rgTMp/68F/Vezvaoj4x6h2gvW3xFlgLcvFewruMHp3vljp1N6K/ovHs3Gldnj9v7k9qubpG1qQh0U1NE5TtNxC4PssP4ml3u+1IBt3I/kVkAMdHYOLgItiiGQmRqSWWFwNEoSbrsgdzdr/U4TmLU3bYD8WRyF+Wvk9wzlgR1aoc/hky6gnJbhuI1LLGbZwHFmq9YGPfHM3DJsKjqDDlmGM9EsVHhs H4mNL6gb x9SdvvHCHk5B7slavNv8EdJ43haVJxjSioBorr3mu6TazXkpymwf/4efubig0daNEY01ZWmbKJFf5HFBOSZ98uC1zZPt9YNzMXBv3BRJlWteyAxV7rXfkkEhYb3dyF+qCdCLki6lV+qe1n1jfVguw/QuMp5xZ1tYoH86XW9BMQAP3UtP7yy2gtBOgv1CIANrazwwKgnSBxBckvX7vmbKncPfRZXS4pREhrudQPAWTXgVwRQgXyDQbeecXTMDZT8iy44q+OfJwguko8tAfoEArvY+hRjPAjopSI5EE75Sav0VycmZJu15up9n63v+u/EjQEil0HqhoRxPCVm6JNA7AET8gT6OIiDEAX+RvvnvcprImf81Y7v0VMKy0cJmVai8FLUSIDrur0+fXbG4MF4zOBWAUO8R3h4RbhpgGqlg02XD6bC6EHc1xtq80VsyolGq75sORcz7M2v8URaPb3c/4wYuxZFzTaRnX0jdEZ6ayx7k7TBfb+Bp+e/MbUlAnJGElklVtsjOxlCNIzLWu6ucvLUXm8xbDW6tP43vvTxmSP5TRLHS97xNXeLD5KZmoZIKEvJKqONAWPjU0pY8ckhNHccvi0OeW/tKL6//u3LrHJ6I/KLj8L35Y0TP4mA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: `arch_calc_vm_prot_bits` is implemented on risc-v to return VM_READ | VM_WRITE if PROT_WRITE is specified. Similarly `riscv_sys_mmap` is updated to convert all incoming PROT_WRITE to (PROT_WRITE | PROT_READ). This is to make sure that any existing apps using PROT_WRITE still work. Earlier `protection_map[VM_WRITE]` used to pick read-write PTE encodings. Now `protection_map[VM_WRITE]` will always pick PAGE_SHADOWSTACK PTE encodings for shadow stack. Above changes ensure that existing apps continue to work because underneath kernel will be picking `protection_map[VM_WRITE|VM_READ]` PTE encodings. Signed-off-by: Deepak Gupta --- arch/riscv/include/asm/mman.h | 24 ++++++++++++++++++++++++ arch/riscv/include/asm/pgtable.h | 1 + arch/riscv/kernel/sys_riscv.c | 10 ++++++++++ arch/riscv/mm/init.c | 2 +- mm/mmap.c | 1 + 5 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..ef9fedf32546 --- /dev/null +++ b/arch/riscv/include/asm/mman.h @@ -0,0 +1,24 @@ +/* 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 e79f15293492..4948a1f18ae8 100644 --- a/arch/riscv/include/asm/pgtable.h +++ b/arch/riscv/include/asm/pgtable.h @@ -177,6 +177,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 0e8c20adcd98..964810aeb405 100644 --- a/arch/riscv/mm/init.c +++ b/arch/riscv/mm/init.c @@ -326,7 +326,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, diff --git a/mm/mmap.c b/mm/mmap.c index dd4b35a25aeb..b56f1e8cbfc6 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -47,6 +47,7 @@ #include #include #include +#include #include #include From patchwork Tue Oct 8 22:36:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13827095 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id A5084CF0429 for ; Tue, 8 Oct 2024 22:38:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B11FD6B0082; Tue, 8 Oct 2024 18:38:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AC4476B00A8; Tue, 8 Oct 2024 18:38:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8797A6B00A9; Tue, 8 Oct 2024 18:38:24 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 637AA6B0082 for ; Tue, 8 Oct 2024 18:38:24 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id C0F6D80CAB for ; Tue, 8 Oct 2024 22:38:22 +0000 (UTC) X-FDA: 82651900128.30.5C95ABB Received: from mail-pg1-f182.google.com (mail-pg1-f182.google.com [209.85.215.182]) by imf19.hostedemail.com (Postfix) with ESMTP id 4BB801A0005 for ; Tue, 8 Oct 2024 22:38:22 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=GPZjRKp4; dmarc=none; spf=pass (imf19.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.182 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728427075; a=rsa-sha256; cv=none; b=xfpiIdbkSHNgFelfCJgQbhSEeHdXdooOhTfc1PymKhTVpz1eSUTsOnTfNKyvVFa5nAanpD X64qS+gCcmEdPmn6SuMHyUvH7dDdpDGZ2WT7K4FW398U0KYZPLgKlaDOO9aFlfdOII/1b0 LR7J2eg3IL7ENA6DViD0bRaoNGzN8HQ= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=GPZjRKp4; dmarc=none; spf=pass (imf19.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.182 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728427075; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=HvFaRabdOYgj/FaUxHA5lpjBxuSoaWHPSkkVGjDU8NY=; b=2C90J9RYuhUspTM29RNlFy9KZ9EfD49VXDsueXaYW9WoaSwodyuYvuGjj3lpVGfTPdpXHm s1ggsM3vDR/82MISCjT2MvLZxRhHWoZLr+1rV7oMj7PEEJSr2ZiZtCgwOMOBhpGvesR6C7 Y2SikwpJ9hqmpJOZoQx/S6ClXB2xGYk= Received: by mail-pg1-f182.google.com with SMTP id 41be03b00d2f7-7e9ff6fb4c6so4606853a12.3 for ; Tue, 08 Oct 2024 15:38:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1728427101; x=1729031901; darn=kvack.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=HvFaRabdOYgj/FaUxHA5lpjBxuSoaWHPSkkVGjDU8NY=; b=GPZjRKp4IPX6OoAUerp5MlE/VIwR/Y56IZ+W0m0oV9MtSW2Ayui3l0PTHx1qrRCqHy qkUKcnyYf4oXXV/KNp6kE7Ce3l+Jwea/U2W5TrAflMF07l55y0DC2/nOog4yEJvexnpY HYA28o/8hc1fX8NQuZWrqS/QRr4iDsqdvW2ftkefLlTyoXHVKOMsHV6ccaDeLCzpnyII 4XKw1CVrFSBcYLdtOCEBkH8JeerF/nK4XIl+pO7dezHr9gJpmCfwkzXbpL3AUYK9mshX VUQg0HB2X86YInL5PEcVxiiuN0ODrJU+CtFzZX3oYCkm/Ig7DcQn38ObKaFHhCGJn/rL XMuw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728427101; x=1729031901; 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=HvFaRabdOYgj/FaUxHA5lpjBxuSoaWHPSkkVGjDU8NY=; b=LPuQYfjLTSif7q74qPZAcs43AljO3YhJMgvqAAWed4klIATHyKdY+TZmiOySZrN3Eq C8+FJGan3WTz8uzgyZtwLFI8a79BBVmVONeQ+0thZejeT4lhRJFHwufI/8SRPSo6O17x V+unJGzdgEqY74DYgF4yAeqUPHwihBKNMVzpN4tGEMmT6ZJ1wDo2L0bPbt8bfpIoeAEX r77ei5HJlfvM0AHNhvxGcEwbM9Vt2e8g7J7550RlkbcY3BtQdX7SA6pnJv67bC488jS6 8EXP5dXLlv0mxWmdrUT69IUYiw1UQJ8xloRrk24XLE6hGo38hGx2tgoR6Ka1saBAPcGb YkuQ== X-Forwarded-Encrypted: i=1; AJvYcCWhCP8waNsicc/U7z8Cd0Z4jbU/yWZi4cUcDFQ2fYjHOr8O8C1c4sAuVwn3s8gb2o7fLvM2O6UZxQ==@kvack.org X-Gm-Message-State: AOJu0YwxxBFVY4ozKbzJoaplvLJqF8uHkG4qz4WxSTR5agSFrjEwXt2d jh1QxeL2KHTd4OocDf3wktv3/kT0QbDlYwWyqEq2Te+yZeOksfI7x1EkYgTGXDc= X-Google-Smtp-Source: AGHT+IHvesfHqSkO6ZkkqxS8iUZFbPtASty24Hzhh3e/eaCzge7K6ZuvofdLUxv7u/b5HvamsjHRPQ== X-Received: by 2002:a05:6a21:6f83:b0:1d0:7df8:2484 with SMTP id adf61e73a8af0-1d8a3cd928amr676575637.43.1728427101109; Tue, 08 Oct 2024 15:38:21 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0ccc4b2sm6591270b3a.45.2024.10.08.15.38.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2024 15:38:20 -0700 (PDT) From: Deepak Gupta Date: Tue, 08 Oct 2024 15:36:54 -0700 Subject: [PATCH v6 12/33] riscv mm: manufacture shadow stack pte MIME-Version: 1.0 Message-Id: <20241008-v5_user_cfi_series-v6-12-60d9fe073f37@rivosinc.com> References: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@rivosinc.com> In-Reply-To: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@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 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-Rspam-User: X-Stat-Signature: b8myp8y1fsi14yw881dhnuesn1zzm8kg X-Rspamd-Queue-Id: 4BB801A0005 X-Rspamd-Server: rspam02 X-HE-Tag: 1728427102-219914 X-HE-Meta: U2FsdGVkX1/Pc4Lwf9ln4Ux/Tw9zXIJ4rNGAJGyG5J/mGGddNFAjHOhQYgBftWZk74HmERdkLyVJcHYIIhRPS+K8ngno0yuAn2Iqmm2DsxP/R+mUKVj6qMvgixzo/gm+vBCuzanuLZ+xlXNG/oN06YcbbF14kpxkiFB1MONsBUIEhsjHO3KOnzgV39ggU/Bv6f1+KHAm0WRUSaxtnbcM691Hk2VVzhN3XCykhnmG70AnF3cy8AQ6WsyzqEiPrkiMEQywyj297XDvxTGjk05uzm5ve7lONZst+yxVXjWwSs2CV/ChLAgxJeAYVCBvBtcPKPLaOSAilxhjli107Fvbj6glA0hc4FhnNgTQrAtDt8C6BcjaMNFzc4eHRvODJceuN8nPXQkgfWwNfGk/am/YNZ3jG/HNHwL1em3efrLy4LFikHjay6icTQI9KeJffx5ACTgc81hVjCiRWRDlRugmc2BcR+Gm+OX9ehVkuh0JbuiBB09PaV1BRiWj8HKxx3Iz4lFG3JRcWYfJE/mTsUkRQe/HOCgMVDALCJYzlYMBUlEFSf6SFUdQYnnOYNaRUYlfB4tc5xd9ejQ5XSekIReqtRVyWW20XCBfJ/PjRNtz3MZ3N+jbbxVDyvO112tnhOzPX9DGUvoa4zvk3XD0ThsXggrwGR7Syxe8/tzmobsDD7Xj4wt1Gpf7wL72tF/fis6s+M2QCFAReXNGthZ54qV7sB+3wtvhCi7TBQn5nnqFlhnuFMxvFE0xEP8mgIWF3xyakTf0tVDHiKKxXEdTdZ4oG9azvtctDC1ZX1hICGYQLg5AUsC8WNuxrxjUvTjxzMX682nf2TxkramzFputGYAl7fgKc0Fq0IVHesOMPMRefCpCCbCRjUXhgf8uSCmT5bOh5TRdY8FpC2RmEu3ekBtlKD+A7158d/4znTyjYxE/RLR4aZme2GsYm4DhHc6R20bdVqrJzKK0lS8c2bBk5Gq /Ge8DYkC X+mnP4mtOgINqF1bvKsalLw59sRb818gjIkBy33TdDtRWhXTfqplMc07vTip8bQF4fItlrJFGflb41bfD+kMOJ0S6B1AZ/NWB9+D4gknyIIN8vBCLXf9uvlwqeUnKGqMbXCALoaexBvq19w76hlH91kg7N3AxNUFvUg5ys3YLWXyXI7O53MEAtCz0CO3oMVL5GyAQr1qDEeRcE+olOqGQDMnYFU+oCQ8tLisIZTzq7W/lnMWLweyHqO9XVNbiYe/pQ+/CjB6F2TtUuZpU4ALfgnHzZdZ/8hXRe8nAexZQ62yda473UDWwC30TYdEJxgPqNNmcIAy5y7TArBH4sHMiMeUcUKy1sBKV3Rcukg5ly3xRh9SzWjBvm7bfty/5XCr635wkYgAJ30NkhCIveW6Bb0yL1toVg7o3Aky02vAKDOeM7uu0iHbbCOovvaepRzYx74NX6bzFT8KJEte/mZpo8q9KkoV3vVUItr3BSaXoraJg0qf65d8Tkms8WvF+oiQXr+TkM+48k+OxpFJiKRSTbtjYtN4qaefYzpz7pS1qVWbUkpSz6Z1HH858sL/rKQeBs60ZVzAiPNuSDDI= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: This patch implements creating shadow stack pte (on riscv). Creating shadow stack PTE on riscv means that clearing RWX and then setting W=1. Signed-off-by: Deepak Gupta Reviewed-by: Alexandre Ghiti --- arch/riscv/include/asm/pgtable.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h index 4948a1f18ae8..2c6edc8d04a3 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) @@ -738,6 +743,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 Tue Oct 8 22:36:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13827096 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 111FCCF0429 for ; Tue, 8 Oct 2024 22:38:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DD3C96B00AA; Tue, 8 Oct 2024 18:38:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D837B6B00AB; Tue, 8 Oct 2024 18:38:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BD3A26B00AC; Tue, 8 Oct 2024 18:38:27 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 9DB886B00AA for ; Tue, 8 Oct 2024 18:38:27 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 086ABA0C84 for ; Tue, 8 Oct 2024 22:38:24 +0000 (UTC) X-FDA: 82651900254.21.776DE92 Received: from mail-pg1-f170.google.com (mail-pg1-f170.google.com [209.85.215.170]) by imf03.hostedemail.com (Postfix) with ESMTP id 222E220005 for ; Tue, 8 Oct 2024 22:38:24 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="1AR/wIqz"; spf=pass (imf03.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.170 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728426969; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=I6L9CXOfGC8FGkDgNo32YOnEr2hJuvgG2CDkhLFheFM=; b=cEruvkNJCq/YP6ADkR0dC7+1BKcqx+VoM78LXCQhoY00KdFKOLWlx2z7QgL/TRd9Oy3xlm 5CPQqsRNEKGs0iXXTLy801ejmcyZCr7YP/McmP5+3onztVdN8P+vUCs+M+nHGYvvSnyrD3 9IxKUQfheSaLH3VYnafLf78kmuOeojc= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728426969; a=rsa-sha256; cv=none; b=QmR+jyjYIp2hMspbdZ8Fa5za4mR53Qrs+YrQf9ApoSMRNbIZvahrMBKP90+l/lFI6uc4w9 oLvJXw3ewxZhPLR4kEsfGR79+kvFbIXrIQYqO9J29lAj6hr27nCzANgchY3hprgFMYzmCy GKu1sS5Ksmot6m0RS3Iqd1brrEWuLsc= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="1AR/wIqz"; spf=pass (imf03.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.170 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pg1-f170.google.com with SMTP id 41be03b00d2f7-7163489149eso5379070a12.1 for ; Tue, 08 Oct 2024 15:38:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1728427104; x=1729031904; darn=kvack.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=I6L9CXOfGC8FGkDgNo32YOnEr2hJuvgG2CDkhLFheFM=; b=1AR/wIqzYMzPtzFvPocwV7rCwkGKjh+SdBRINNOHkGiT0Mr9I5ju8SDTByPdt0zxZx vjniQOT5qWoFZa+NzoTm1Xmmmq48jJSe4hBhjrzYmAJQiSZXcJAQ3zupR6nEi8blhCk1 PNbEh6WKoVPGnLMGu2mwcyiWEUGKcMRU0DEdfqyAqxolknsG+1yavLFjEbna3oWwQdfD AO+hOpCj6DQY7VjkUFhIE34cQzeor5uKTb7YoHos9UJj/ZLK7jGHsNT2fzemfaZzDABV ZzdzuW+fA9M/+NiwW3DphD/mRa1t/6ITbCbMNBl7lceKwJ6OkHnIQVnvw9npYzCa+SUp eU/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728427104; x=1729031904; 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=I6L9CXOfGC8FGkDgNo32YOnEr2hJuvgG2CDkhLFheFM=; b=MDMUnBVDKD5n9Zu5Wc4ELdyPpmYJGWq6zvM2NKb8l6tdvmVjVrPqlzcG+j20NaHfR2 g9fDCY4CgjaVstAQSjRmgxlGGy483+8HIxfq9AxPEA/vNVKvtB0ri0It4/dyOaSRAQ2s l3T/wKjOk1Oia8u2QlU4Lm+hEz6dZR0Mc3yH6VjLYzFGGJ8tbEITH1x428AY1FAepiYD Cx7DwR0CJg0yQAx4NqED0pd1scvBWxSTPjqe+EHtniYjOCUlhXvlpraGhzOzmkjpPIQQ XTURN1j5nOs55Lgo9lNEdLa3WTUWJhGuNHWX8taHLr0mXnuIQQ60ptp3/HkLl60mV4Ec LFAQ== X-Forwarded-Encrypted: i=1; AJvYcCW9ryLYb0HHZb3KycEVTMLpBOplpplm7uOj2tX3hq8uv7foK2xXwWadKXRcgbQTLDf2IiLOcch9+Q==@kvack.org X-Gm-Message-State: AOJu0YxA9TMWQyh0A7UgGV5vkXaQOrFhUynr6XGdV+6t+iOK5mRugTBY 77JugExHduEwPv2EhajUTBcv84SF6B+ntP5g2p+k3T8aeDNlh2tXokbASh/lqhQ= X-Google-Smtp-Source: AGHT+IH2MMc22LEfeLpFr/+vzLpFm6wThOwZwpheAqw6wq1Vnq109QUbVNEO0CejcV+v5vQMgM/88g== X-Received: by 2002:a05:6a21:1192:b0:1d8:a3ab:720d with SMTP id adf61e73a8af0-1d8a3b5cafamr1049827637.0.1728427103894; Tue, 08 Oct 2024 15:38:23 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0ccc4b2sm6591270b3a.45.2024.10.08.15.38.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2024 15:38:23 -0700 (PDT) From: Deepak Gupta Date: Tue, 08 Oct 2024 15:36:55 -0700 Subject: [PATCH v6 13/33] riscv mmu: teach pte_mkwrite to manufacture shadow stack PTEs MIME-Version: 1.0 Message-Id: <20241008-v5_user_cfi_series-v6-13-60d9fe073f37@rivosinc.com> References: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@rivosinc.com> In-Reply-To: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@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 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-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 222E220005 X-Stat-Signature: sq8n4z63hdjkfjoow946tdnizb71744e X-HE-Tag: 1728427104-565795 X-HE-Meta: U2FsdGVkX1+yDetRpB7TeWr3heBcsPkz/KCu/X591wIYzH+SOvIGdyLmGxBI6IOY05VT8sJre5PE/6OzjJnY68RTdM+vXTmDBk/POXNJddVixUP4ywncbCQzx5qGeaN1SJqKzebJu27IJsSR9/7Md7nMD+GK3jkqm0kUcC4tCDMxJSvk6lTWRT1wyF32khgmq9hv3toTSwOQB10KVINoHu+F7Bozj45QCGGpk/YctCidShoPXLadcANzssOqsV2aKknj4lsRbBr3x0fvazZ77cZqfotqPTYzui2GK6Ifx5kqEy7uatvhFBN/yYrfw4PuW9n+ewXTowfaDhxUvwI+Y+USUSoMA/5sBoZWKElJpH+zKFgUwM2lu/Skp6WtM9TtWraskLwaxeH/UvURwBCzjlPgbvhyFpL84yQM80VHhU2/iFVnt4uiMnWZnjVjHY6Ydyl5lUs+4pVQnrSdvLbIo63ntroi8TU4QardBxvcpc48LH9zcXtA71KJ9zuiJ36MZzVaP/mvcxX0lOcK+aK6KgSDKRDGOCSVyKyHFVTL+lwzpw8jmjtv5NubxRwmgDXPLcWHKZKYCfO4L8IxmatFwo2yqNxZciYRYF7PFBTK5jvDj1juSZFGIGdSJxUU55WROhxbGi+nIQxfpfWQ5LF8w7SpS+UHQNWB64rdKAtLlpb8jnSU59yi89UwzxDQ9200aAqsMGGw6XHqgVOL95WJEfJDpjcSEpADregsc0ibKSAXWouIH4KD2zJAfdKRSI4kOPIo6h68HKmaMvq0+XcxdyKcAvqpKBENINxSfSMTHpIosdbBB//fiTSwyKEoS6LYJFLZM+Gqulj4DZQMCWSNbYHSbXOJF4hA/bTm+8CqpI9sI5vBq5fzoUNNdwPaFCg6gT6oXggoRqJb2MI168CYsLJqY8hbJE7LEbur2K9dtAUWK6YH+Sko1TLhVV3XiTw8bMfe++12yQz/4tZfGWD QjlY3QKp Jei3rcRoGvPnaNTk1QpoJx/+eMiVgRDlzepaejE9KjHz8Q5vqSMV1hoHuEjfoGCUS03aF1pdSgUqJvAd5EwaBAE+8bCEQLbMsxOx6vpv9pDIHQxDNl9sKJRF850iQIAmqpzk2Ju04ekgn/MEZgUMvHZrmHNUOFbaHlxioK3L+/Hk7T7o6IGh/WOqbWwGqaZ82sUxRTEfgtgTvoxPkO680vlfYZbr5d3TrYuTa+xtZO8/BoLKTD85js22kKoP3VL/WQCZqqxJpern8hQ1X90f68UNJS7FK4Ul3w3Ylp7jKuIf6vNGzUeizJkmt9hU95VGbtnIcDGZy5pOryj5DLxCa6I6maUeX0NZA492mWjUnbdKWB/w4NV026VZv3MyLpWCGtlmcLFmHejg4DvZhAKSOvZTHZvXjpfvj3aJIUQMY8oHNuEj+AJI8C6bLZI6oj3yf/4GuEHfSv5aY7WDo4LkQhoSW45uDh8IGx0/iCBC2Ep1UzbsaKji4XtwjrLtByMXB0Xzwp0EjHftHGR+1PX1V40e2zFpldEz7NYfRjWlzYs0b3gTwOGzgR/nQNSxTs0so0a66HQq5w6Nms/WngHIBAJLY96zCK8FEiGGaXxasS5iYEmHqgzc47fGOfA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: pte_mkwrite creates PTEs with WRITE encodings for underlying arch. Underlying arch can have two types of writeable mappings. One that can be written using regular store instructions. Another one that can only be written using specialized store instructions (like shadow stack stores). pte_mkwrite can select write PTE encoding based on VMA range (i.e. VM_SHADOW_STACK) Signed-off-by: Deepak Gupta Reviewed-by: Alexandre Ghiti --- arch/riscv/include/asm/pgtable.h | 7 +++++++ arch/riscv/mm/pgtable.c | 17 +++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h index 2c6edc8d04a3..7963ab11d924 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); @@ -738,6 +742,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 Tue Oct 8 22:36:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13827097 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id BC833CF042B for ; Tue, 8 Oct 2024 22:38:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7B9C16B00AB; Tue, 8 Oct 2024 18:38:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 743A46B00AC; Tue, 8 Oct 2024 18:38:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5B8EF6B00AD; Tue, 8 Oct 2024 18:38:30 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 35DB96B00AB for ; Tue, 8 Oct 2024 18:38:30 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id E1CF6A0CA3 for ; Tue, 8 Oct 2024 22:38:26 +0000 (UTC) X-FDA: 82651900338.08.3E3B707 Received: from mail-pg1-f174.google.com (mail-pg1-f174.google.com [209.85.215.174]) by imf06.hostedemail.com (Postfix) with ESMTP id EDD4D18000A for ; Tue, 8 Oct 2024 22:38:27 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=YInKPItM; dmarc=none; spf=pass (imf06.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.174 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728426998; a=rsa-sha256; cv=none; b=Ei0JSZOeeLBqKYTkjUyGFrcbWZeiYqlSugGC6B4/2MW9sNQF+rxJveuU/K/8XO3j4et/Yr WcN/bG3rXOigfi6DCcBhbjsvmf9U5M+wxCwv8h7XBWMIrhTIWMzlXWmli5k0q3Zs47uO8j gyGK62eBu+Qm+HPquO3/7+d5C1DIrQg= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=YInKPItM; dmarc=none; spf=pass (imf06.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.174 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728426998; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=AMPZIqdzSjfHq7xJb4YaIgEu2GPeUmiCYK4jkAGMc38=; b=dshDpmtrrJCSoHThKKW8S9emrK2dpYEGbp3GrIevPubeT5Dsbdy1bV64Az2MwEfAc7ZKXB MdaGcw/Um+Ctycl5lp2et4b79gtGe8P4UsntmIOublaqbzox6UXVkfHDh4J60cGEBviEIB ABilh0jJhfFO0oHNUUoD7cNWg0KE05c= Received: by mail-pg1-f174.google.com with SMTP id 41be03b00d2f7-7ea07610762so1959689a12.0 for ; Tue, 08 Oct 2024 15:38:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1728427107; x=1729031907; darn=kvack.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=AMPZIqdzSjfHq7xJb4YaIgEu2GPeUmiCYK4jkAGMc38=; b=YInKPItMMbSw+lsGxXnlguoF+ZpDV0KyyYBwDI+D11krF9w81tt+N1uiv6QNBJ1gu8 OI9vjMR9luaH0gP8fKPFG5frh+lf29xtN0kPlUEl/9apbPgd3iKrXkOwblSKO7DFSoGy xXdl1FOheTa0LnK2PckOeE/LRCoCxhEvqfok+xGe1giUheKwXNbb7F4WkpwfUajVvlUJ FfWH/GEypZt+JZn3MzhaJzu54/4fU+RzNkK49rMrLkFZ7icPzpwCCf97MD6LgCqO9Omz sdOIxe5/+8pgYUMAm9Ju+PtpIXjNAfXpR03UI9Yj7ldmJaWd14fV1/X/Plny8/2UQeGD kztA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728427107; x=1729031907; 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=AMPZIqdzSjfHq7xJb4YaIgEu2GPeUmiCYK4jkAGMc38=; b=s4ecDSXgjFtYYkVkBarMYDTx06QdUKJ2mY9NMciQ2+4dDsucF6DCrZETUMZx70fYvM 9DW7x5G/q6wb26CyosJT6OePYsSYsp9NQonyndYtLdHx/FkcrkWXNXfwjA8C6fYubEH8 6/04Fnu/nSIy1PnK1GBpOIPa2TkQgzRtyiKvkHgX0C4r8QRwwJD6ScnDJ6NgnxsXZ/mo G9d8g9+NnBFwI2n4QdGbMlwZ/mElBliFpkh0/5tACCBNFcCUXaoUoRWRtkEfN0NtZWoW Hzghz7kMOg2tqe6P5qcydTkf8c0pweRijz5LNgx2tXzZ1PRPmLjyq5oCniImAOKPHqeK jv/w== X-Forwarded-Encrypted: i=1; AJvYcCWAfv1xKsNvbCWX4o848I5yqLmoW4U3ey30vJzBMp/XHv4xaNdfV+SZXSK808y73fmHSgumaBnHQQ==@kvack.org X-Gm-Message-State: AOJu0YznqCIifNJd0/N1gQfjYpY7UuxFDMJoCvKyBg027H45fvCvxCCA PNWHQknHKXBUsF9bXVBsWUwlFEsSwSvfLHrUNUbrRHcDIWeFUDSHgnFqjlUTQEE= X-Google-Smtp-Source: AGHT+IHWWiD7Qfl6QcTOp/Fh8ZvI7bj5RBVwxNM0ruqC/BqZklsNYndBJ5uZwxRV/4BsJaJcvUdg6w== X-Received: by 2002:a05:6a21:3983:b0:1cf:23cb:b927 with SMTP id adf61e73a8af0-1d8a3c5656dmr945264637.34.1728427106662; Tue, 08 Oct 2024 15:38:26 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0ccc4b2sm6591270b3a.45.2024.10.08.15.38.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2024 15:38:26 -0700 (PDT) From: Deepak Gupta Date: Tue, 08 Oct 2024 15:36:56 -0700 Subject: [PATCH v6 14/33] riscv mmu: write protect and shadow stack MIME-Version: 1.0 Message-Id: <20241008-v5_user_cfi_series-v6-14-60d9fe073f37@rivosinc.com> References: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@rivosinc.com> In-Reply-To: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@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 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-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: EDD4D18000A X-Stat-Signature: 33j5r4cnsxkm5cybapdgqnijxzzfwfrn X-Rspam-User: X-HE-Tag: 1728427107-938218 X-HE-Meta: U2FsdGVkX19jYVoXX0EkYsgErEXvaHmIgTaY/EUsiIVVKnUImJ+smrFBprtvooblFqaZ27fZ3udcBiyt1XxVny8Bm/4MoB10g/bqyWBrsC1pPaCVxP8L/3CAuP5/ghiiSNMlnVfDYjzLN4W/pvBZiKIevP7+Qee+1B9y8nDXePUrAySvrtXZgkEcFXcOXZUvZIE1cldxePeesIv0LGJIwICWtmYAMOrlxrNDRrRZIWzPiVxK4zTisQv3qYS5hb5Na3TTEBrI63TGBRkaIYXrTZ2sP1Qy6cvJnDFAzxg3cPNyj0kBiEYxsRkubAIejd2YlbU0bNVyfqvYOiZi36GTTGYbWKAaS9N5H2eraOofr5Q/n56Jv+YQesfTmA1eRK8qPvErJMmPBLiM6+q36S8CjWHcSqkAgrvgA3HcqJRrfpSEkK6Z1ZKuZSTl0s7Joq0/Wl0LKG4WPIY0Rm2sv4HNF/Qsq4oZ0ePRIL7CEn/yi3/uHYs3ee9hGNWKmB1ca40+NzlT04k5Rz4gJMSZ/V4V+4ZApgaz4P9wkLwwU70h9vrwTZMFrRQmopIh75HH3Gbsdr+iOXuFSdrtBtRXEwueLP7OKEbXMDmXnaXPTtCnzwbGj4sLAR/cC0/XCfQ4nNX7cjjAlXgeFYQpXObGXlYS1RbZDKPGrOvlNGFkE56kUwifkiB8r/HYdP0mJPS1kHNm2/BvyWy7vP+VQ3XTQfAEZ5ifLLoMnTlYPxxbb4G3BJWn88rPpNoMYk9SOvXJRIgdcOdOzJ2tbKn9terxXroJF0CkMsb1gX5uMySJ062estxFjIJqasQ+EpFLjJEA3coZ7sbFRHLuU2nnWDv1ZUHOzL3qoOZ2Peqntp2l52+1rwxth3Ahn3hszU3QmVyf1il+9Vn/Wl5V1USdRXq1lp5ct90Gh2fwmNrBoIcHbF2J1w8xGHuc2bHjdI3IWOZkXFLbtvH9yekN3GSGhI8tUBt PZv2PhHv pYiOHXxdOJ6q17Me668pQZP0z/Ry+AJkmuZNgzOoKHSrjrjsm+NW1WteunvbTxVRbNKkLATlfQEnPGicYWvHtqLoNes5YQv87FPZy0C+1IUHKrDiclAp4ZLiZmXM446v5r42iDxG89jNtHni2CqMV63SbEgsY9PmPMHadque4xIwxfJcVrZbGNpJVsOoAqM4uDFxrGcf2ttxyR8impWMfRfxku3D8+9PK77fs+5C2k1hiUQy2QTW89j4Bg6H756ob5kymnMmMMveYhHDhqjMpM/sf3vS36EGXMfH6dxcYAbHxWq19xWb1KcbGB0qaTbgdQuMry7PwE9c0drzE+BPVyM0hz0IxjTs+2PcxIxC+QuLyMTgOWm6iEwr9lXdgEqosCOWpoKEkmtnNMCShnp6QmbhF2qNgskxQBPrZb98WFSD3OHEJj5+GmL/qFv0Qr/+oVox8eU7dKkxw45vEEL2VZGBp7QD3qZ/Ej+qx+AgnuGxpjAjxRrt2D/7FIknasEXlpKeOZZDmr4AkAOtFEVIvqWs7vzjuJchWpufgPg9Rlgzp9s8ubUtnr5mVfR3U1/cd10I4kLkDfBvflUw= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: `fork` implements copy on write (COW) by making pages readonly in child and parent both. ptep_set_wrprotect and pte_wrprotect clears _PAGE_WRITE in PTE. Assumption is that page is readable and on fault copy on write happens. To implement COW on shadow stack pages, clearing up W bit makes them XWR = 000. This will result in wrong PTE setting which says no perms but V=1 and PFN field pointing to final page. Instead desired behavior is to turn it into a readable page, take an access (load/store) fault on sspush/sspop (shadow stack) and then perform COW on such pages. This way regular reads would still be allowed and not lead to COW maintaining current behavior of COW on non-shadow stack but writeable memory. On the other hand it doesn't interfere with existing COW for read-write memory. Assumption is always that _PAGE_READ must have been set and thus setting _PAGE_READ is harmless. Signed-off-by: Deepak Gupta Alexandre Ghiti --- arch/riscv/include/asm/pgtable.h | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h index 7963ab11d924..fdab7d74437d 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 Tue Oct 8 22:36:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13827098 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id D39D2CF042A for ; Tue, 8 Oct 2024 22:38:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4D1EC6B00AD; Tue, 8 Oct 2024 18:38:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 480726B00AE; Tue, 8 Oct 2024 18:38:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2D3C66B00AF; Tue, 8 Oct 2024 18:38:33 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 0D95D6B00AD for ; Tue, 8 Oct 2024 18:38:33 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 16F5A1C52A7 for ; Tue, 8 Oct 2024 22:38:31 +0000 (UTC) X-FDA: 82651900464.02.C23E22C Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) by imf11.hostedemail.com (Postfix) with ESMTP id BB84F40011 for ; Tue, 8 Oct 2024 22:38:30 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=asnR4QXC; spf=pass (imf11.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.179 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728426975; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=dAoDcoPzZ5rcQ2NjFDcOAOgedZarol+OJwK/f74o/Zc=; b=FLDCzIwyzopTI0oc31pVRLvdi28ZlYJgnDwn3CQg2wB+503kFwfsGqVbfHQwMiutRtwcIv DO84OAigQ24xqUM6Y5k1d5AnVHie8uHhQfrOHD2ZjXcCaXJAwRqGeFiWYUwrR1Qt5+Six8 92SOSfPIQS9ZLXYCus/oOzwwIj6AeIA= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728426975; a=rsa-sha256; cv=none; b=DGbZ7yyCmLoUpl9HLKZtUogxebnO4kEy8yIGrYqhZLKNVdNZQ6QyicrjL3QYf0nRYMJ+Oj VC9NyUcxiC+0anO+muuZirQA1j4i6+Zck0jRhDvL2fERQ2AAZShTcQT8ZltvF1g8tvAnat D99yyMUs7KEESa8O3WwOYy4aD0hZUYE= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=asnR4QXC; spf=pass (imf11.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.179 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-71e050190ddso1945731b3a.0 for ; Tue, 08 Oct 2024 15:38:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1728427109; x=1729031909; darn=kvack.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=dAoDcoPzZ5rcQ2NjFDcOAOgedZarol+OJwK/f74o/Zc=; b=asnR4QXCPfOYx36Hh4DBwAOR0yFQXH8Ds0o6ZO8asbXXGQsN8uvNJlLNFM/aFd7LR7 WS/5qvUkBtmlPF8mL4j3iZ2R31NGxDFTUW+L0YIxd/hKo/0DSCLGB8QIm/Zvw3Evl+h9 d23DiIE5sGOHoN6WFgRTjU+hkm8ZsJMpBkPpxGcx7OeG+9inwXhvTL2hI4iKLKOOWh99 60ER2GCAHe8lSMPbMQJ/91Kbzu+FPpm49/EI3ltfR0H9iNjf2UFB006d8AvAIlg8eThw Mo5Gb3GYNRaRda2nsjt8kq9I9va/+QPmd6/lJTx+30buvBRge7T5ODzAz0UENYxmaar0 xFIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728427109; x=1729031909; 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=dAoDcoPzZ5rcQ2NjFDcOAOgedZarol+OJwK/f74o/Zc=; b=bUdEGmzu1gTyCnO47rEH+sEyIC+JWmlRiPO6pd9h8BfCRKLcKERFepuycDtQG+73ql v024CnW5wYq3jzO2uJFnagDdzy7o6qDHQRA1d0FavOG8kzJfCRF/PJzB1eMo0wVPKcVa qDZ1lU1B7MV+cG+HMvxQyJxXvlA4Uhh8It7U74zk1d9zClhGLSC+Q88gYsTPa8GvJb9f ZVbUcPs6EBamdimOoujTdwb3ODhicSMhZ7EcVRP/fMk4P5bUr9iLPyqCrVk2gFxrnr8z C3IodUNyJ1kpu0f7h7HXtPGOGvi2F/o3rypnjqxMOvatTOdJAZdEc6UFQFOX/ukSt0wF 9GRw== X-Forwarded-Encrypted: i=1; AJvYcCVKgfoMzB7UBzU8K3KxkkoJthZ4yj92qNQthI0OcX7G4ElEMsiBfHnHMjbo3x8hQX0KiSG1CoPmPQ==@kvack.org X-Gm-Message-State: AOJu0YyAx/je3EQhpsnPb2R59alAFcO+sye2yRop6F7XrJjrDrCbfa2O 2X9XMbJGzWvHfswv/pX6R876rId5jQfUnRCsZ/SP+PChNchB1s3guzWpvGBh8dw= X-Google-Smtp-Source: AGHT+IHxf+f6wcxCIHEfxEyDGg+gobzmOwypcXcxAkWl4oxuTMRGEzHBbNlZ3Xhs/sYeCJgcbfyheQ== X-Received: by 2002:a05:6a00:928a:b0:71d:e93e:f542 with SMTP id d2e1a72fcca58-71e1dbb7c5cmr701400b3a.21.1728427109458; Tue, 08 Oct 2024 15:38:29 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0ccc4b2sm6591270b3a.45.2024.10.08.15.38.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2024 15:38:29 -0700 (PDT) From: Deepak Gupta Date: Tue, 08 Oct 2024 15:36:57 -0700 Subject: [PATCH v6 15/33] riscv/mm: Implement map_shadow_stack() syscall MIME-Version: 1.0 Message-Id: <20241008-v5_user_cfi_series-v6-15-60d9fe073f37@rivosinc.com> References: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@rivosinc.com> In-Reply-To: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@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 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-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: BB84F40011 X-Stat-Signature: t6do5yiiu64hbbx1uzn981ewwe6r36ep X-HE-Tag: 1728427110-538325 X-HE-Meta: U2FsdGVkX1+AhDhE8FXnR3SLRYKf5o99LxHfG9g9XpdbDm83NKn8IBQ/gze31ZBkVlj8I+gAGcMTUs9Fk3SNChuE8C/ubJO/ExQx723chO79UDIrKrAlQKZ+AfTCTRi/LPYivk5SMzRMu25ll/KoGb+Slm33w9XYDM/Mt5OGTz39jz1Q7PesZlRZdRReWPDQ2GFxSO+Qq2tuhZ0QJ1Q3FvSnvYTEWs1y51ozVQeCaQCak/HkE//1VnXXHkkQ+qGlasFEjjysggpABSJ5biK9Bhppdh3gJyA7hCCOH/wkpOTAOgRAx6+Ok8EJruYGdnfRE8ZGvzsjdtczWT4SWdYhw44voN9eAJDuVC48ilxqNdLXbj2JO94TH8AgCad/IB6GTG0HCPfcpjqUr5Uo/KZNiJF19EgD/xT4Mrf9H32cz0jxtziZxKrXzMBbF24yzT3mhNu8ZSNeZqE+b4WzfqfsudMKH7hbOH3M1+mFssB/yBGvJrmzVqLfWYv4k6qgbVwhc1sQk/EvfgOly9tPuGHLfkLUJ3A9oGhDRS9Dy4SIpweQD35EZg4psGEjwtWxDtMTtqJB5xyJmX9Z8gx3umyrig9TJomsS03Q34c1qInmccoaLVj4tz9tt5D2nM4WKBC5/K6dZ4pM507h0PbrxV8bUU8W3HYVJFggYoDtWh8YOTvHoL0e/Ld+7UicqZJ/Z40rqKdR0WljGkT1fnmljk6DknPfTcXCGQv9pov8vw0RW6nATjj9kxleNln+vx3WzsifIMQsWkUkz/B9aNAht9xbv2vJWM7tcwOFyV9eFrKrfzrxAzPVICOTb449qH6rROTBeQBCMSH+p3zQRVatsSjJ5Pi1KURJHelNDUMQZ/NXZpVobFu+Mz5z7ZEdC0APSY4K8JmAlmwdxbkYcdMzHxMEBxdPsJ+r030f3GM0oeJHGUbUg/jSXMRWzfPUyQFyieLGEkezwvap2Z1CFGrx3GC ioClkDxR r1Y/s1TnCkmqnGNSgApZ/7lIz6FylZYIddJSaBh6l8G9q5/+QfbiTW8Wz+s3/lTREjX7dpgQd27IVY86OiCasGoso3z3sFzh1n13ioLL/O3D7qCSMlf6Toc0ZjjPeksJ2eItmM9zmRrOKoeCi7fr/byVVmUjJGQ+cc/ZPQgNTrt4a7Wi1SWYLLxX09aOBv8N17wfVPxMzuRkDLFNjIZ/4RepLp6M4d1EZcLZyEHEi+wJjtJFv4QeI8u/VrsCLnC2iIhjimtivQUq+Puz8wxeWuFwcEoXsKafOl20ahOA2R2gaytdYN1m3Ox4dW0q1KebN3RqVOLBo7bREr1wR6aulIOoJ6Y7ehuWOZVBBiclpa/fR2YYh1ey17jTgqj+pTT5dnj8z5uoQCVhxGvFJHCcyDsoCNP0Q5Cg+zNmzxgVCC48jc6bTnSd5JHkqXiiMQK2OW/NrvXpjXKiklwsmO/V/lczsrm5CcMjEdLzK9TZ1lbjEjfDK0y5ogw4fS0nrjs5J8MmVUhk/5jtFCGgaSgxLVH0eplRLZLyRX2QrvxUd6F7RZqubf5c55i6/AOH+4q9cafvDTVaMAuKJpnrFVTsFYEEwts04zNmsPzJ7JCLRC6OxsZStrT5pHfJuyw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: As discussed extensively in the changelog for the addition of this syscall on x86 ("x86/shstk: Introduce map_shadow_stack syscall") the existing mmap() and madvise() syscalls do not map entirely well onto the security requirements for shadow stack memory since they lead to windows where memory is allocated but not yet protected or stacks which are not properly and safely initialised. Instead a new syscall map_shadow_stack() has been defined which allocates and initialises a shadow stack page. This patch implements this syscall for riscv. riscv doesn't require token to be setup by kernel because user mode can do that by itself. However to provide compatibility and portability with other architectues, user mode can specify token set flag. Signed-off-by: Deepak Gupta --- arch/riscv/kernel/Makefile | 2 + arch/riscv/kernel/usercfi.c | 145 ++++++++++++++++++++++++++++++++++++++++ include/uapi/asm-generic/mman.h | 4 ++ 3 files changed, 151 insertions(+) diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile index 7f88cc4931f5..eb2c94dd0a9d 100644 --- a/arch/riscv/kernel/Makefile +++ b/arch/riscv/kernel/Makefile @@ -117,3 +117,5 @@ obj-$(CONFIG_COMPAT) += compat_vdso/ obj-$(CONFIG_64BIT) += pi/ obj-$(CONFIG_ACPI) += acpi.o obj-$(CONFIG_ACPI_NUMA) += acpi_numa.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..96bb324abafb --- /dev/null +++ b/arch/riscv/kernel/usercfi.c @@ -0,0 +1,145 @@ +// 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); +} diff --git a/include/uapi/asm-generic/mman.h b/include/uapi/asm-generic/mman.h index 57e8195d0b53..9cfb3c1e337d 100644 --- a/include/uapi/asm-generic/mman.h +++ b/include/uapi/asm-generic/mman.h @@ -19,4 +19,8 @@ #define MCL_FUTURE 2 /* lock all future mappings */ #define MCL_ONFAULT 4 /* lock all pages that are faulted in */ +/* Set up a restore token in the shadow stack */ +#define SHADOW_STACK_SET_TOKEN (1ULL << 0) +/* Set up a top of stack marker in the shadow stack */ +#define SHADOW_STACK_SET_MARKER (1ULL << 1) #endif /* __ASM_GENERIC_MMAN_H */ From patchwork Tue Oct 8 22:36:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13827099 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id DC348CF042C for ; Tue, 8 Oct 2024 22:38:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D839E6B00AE; Tue, 8 Oct 2024 18:38:35 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D33886B00AF; Tue, 8 Oct 2024 18:38:35 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BACB36B00B0; Tue, 8 Oct 2024 18:38:35 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 96C796B00AE for ; Tue, 8 Oct 2024 18:38:35 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 4CCF7A0CA9 for ; Tue, 8 Oct 2024 22:38:32 +0000 (UTC) X-FDA: 82651900590.27.97DB104 Received: from mail-pg1-f172.google.com (mail-pg1-f172.google.com [209.85.215.172]) by imf18.hostedemail.com (Postfix) with ESMTP id 76AEA1C0003 for ; Tue, 8 Oct 2024 22:38:33 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="VwnK/VG/"; spf=pass (imf18.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.172 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728426933; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=p0l61LuSLCFf9/34wsnCi4H9e2Hj5jZha+XFal8qvJU=; b=IxzngqMgBc8goAqEZ9/cxKTxzXD/6tqDkOrH+yNHgdz/hKR5vfkrDqdZbclbMRgEkw4Ee9 ZQKVJj55PZmjtc8JYj6on9vvXmuYBoaolX3kYW7WAPDQBFUa0uM/y8GQyHAsdoupXx53xS DgySpWXAzHVY2VJTVeFf9cMyB659r2o= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="VwnK/VG/"; spf=pass (imf18.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.172 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728426933; a=rsa-sha256; cv=none; b=DGunjJ/CvjHltiLDAP1Pl9OmeoNAlbY3e156n7hhSc5ziLtfpUQ6mnPU7jwLRYQCvLNVHI V7Vjun6TareC2sYqznqoG8SDX+pIam1QaQqehW7AwOEeHzFKYappwdQtAZ8JLcu8X5b5mW ZSyBxIXkd8iBgsSohTAenpM+usMoDjg= Received: by mail-pg1-f172.google.com with SMTP id 41be03b00d2f7-7e9f8d67df9so2484393a12.1 for ; Tue, 08 Oct 2024 15:38:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1728427112; x=1729031912; darn=kvack.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=p0l61LuSLCFf9/34wsnCi4H9e2Hj5jZha+XFal8qvJU=; b=VwnK/VG/02x+xrye4XMda/yoE4r4ewL0vADVxn5BwsL+z9nPf427g5DNx+nV50t7rv vsJldlndPP4ChS79QTWX+Fup4oejF4rqANgr1vD1EgTtobkn0ZU8fArtlfRy3KpJ8ody INSiz9zgnOdv1m5G/HJ/uGRuQpN7pZPO455XyZnIhAkwMhN4eLXyoBVQf+PpJvqsjjDQ +S2s/nIitfEd7GGQgCarOT1UArqaOAPBmEhN+g3Feln2aogZhaNsHcBmQlyBGMh6VYZT QkLwB/6JcpTIGZJnpROsutLL+pSA9UyUulGEdqPLsBU9yHo0y0Z2sDcj1+8sn/yZFjtc AnUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728427112; x=1729031912; 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=p0l61LuSLCFf9/34wsnCi4H9e2Hj5jZha+XFal8qvJU=; b=bOzUIHSWVVw6TFRXvf6AJyrOyB9JDSUE/Xh8QpCDdeW+wbhZMYue9t0d3a3tMpAwmJ qgHIuf16iD2j6W33Mfr6EWUtKymKPIr6qqLogxcSIXQZZgzyTU2QxcQeZ8LAgwl7CyNx QEG29JhjCbZUDXqyBmQ1ASPnCM+ETvxCzCxmxshRl9MwgO8MBd2OwUDBS+aUz6M1i+If u54hladHhFzTwGbrPwUBQssxdS8wae1Y3HpRmzGXS60suyZqhh9mPdNdinozsyzKwwEF ZwrArQt6t/S9zp0tSuJ3+RLRMIYEEobA/d/yAtI5VQBpkDobp0gChxc9kOeh+S58gQft 4jYw== X-Forwarded-Encrypted: i=1; AJvYcCWGz1RKy4L0M1VzFkT3mDNEBlO6ICwsEFfQhnwIV2IDH0IFA32up7iiLzgo9zf+WLALPQ2NZWVLyw==@kvack.org X-Gm-Message-State: AOJu0YyxqjRY+HcnioknAZX7tA1hcSTiqJTR1JuvWOn7A8sAiNhLw51Y WSKhnFxElrp4MQQH7QIfzjT/dXktNHxCusgPpI3Nleju4XENVydk1bHNG5ht0ho= X-Google-Smtp-Source: AGHT+IHGbV2Hustmc59dcHOay4JYR2mV4PpmDuCNKma4PHc0cBJNaAo/EJdPa0wvDdanoUjR9UT9pQ== X-Received: by 2002:a05:6a21:6e4a:b0:1d6:e227:c34 with SMTP id adf61e73a8af0-1d8a3b59370mr878981637.0.1728427112219; Tue, 08 Oct 2024 15:38:32 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0ccc4b2sm6591270b3a.45.2024.10.08.15.38.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2024 15:38:31 -0700 (PDT) From: Deepak Gupta Date: Tue, 08 Oct 2024 15:36:58 -0700 Subject: [PATCH v6 16/33] riscv/shstk: If needed allocate a new shadow stack on clone MIME-Version: 1.0 Message-Id: <20241008-v5_user_cfi_series-v6-16-60d9fe073f37@rivosinc.com> References: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@rivosinc.com> In-Reply-To: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@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 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-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 76AEA1C0003 X-Stat-Signature: fbbn1i3pzb3x8tw8z4oq7ncc7rxidkmo X-Rspam-User: X-HE-Tag: 1728427113-58054 X-HE-Meta: U2FsdGVkX188a8tSp7w+nldYTEadewDLUaAO5jCDF4s1sAMt0JCfGbvH8Jm8f3ltVS3AT8VOB83qfo3Brv/k1j8J82c+oXhuOGG+2z6lLauLjGtJfwWDCrJBuaD2z5vO9wO90BVEzQJQEVB5w0ecgUVNYOGMcwskDhQlqikNhw3o1pcoqY62ccUWZkdHDK4Vx9g6M9omgRoT+PInsG2oCKmYYAJKA8a/CjDKrHPBNeqL0yiX+TRe7sZmcaF++mLbWkeDtmL+Rtc61BD2aRDwxsXeQJqZicN05L4ohMWz/ss+JyDicu8l4fnQ4xqwz8P73sluUG7IIFtJylR18EFNe39aDRowTfLTnQ6vnPpJsZd+RCHuTrgGGO4yckWc/fp5VqH6LDoSoRpwauIq60ZT0Hlb/YLWQ3lNS1941uGFUddgeVuK3R36NIQkEbk6CZibpOMjT1uTMELGoNmAqwLm9IQfR58hLWk/vSrcIfpvF2yJ+5haLn3P8Fcguh8ljOUF5U8MEZbmP6zN7YnmHiVfhzKNnYaPGhxMtI+TwRptBJduHQryoW/RziyjfH+hMZ8W6/6mEuYTg/CMdy3eO7Dw3E19HhpNabDFSKXVgC4TQieEoMACPVeAIdfAwM4GopbFnfPItElvyZLIW8MscvzApdNZ/Ec3d3HQGiOuj5EIuGPNfUYb6kfd1O7F5oX+rREG7WaalzH/W/XCA8aXhsqEnCjAnS2wXRXeYVgfR12qtbsTK0N3Dc5WQd13o8l/uiyazCPCUX/iB208ckyCzBz72q/hz1KD5WublarR+NF5YtRRgl4n6u6fJ/gcexvj18ylQsi9NLYjwXgepaupMJ0r8f6u4QK6wFAnh8/TjCayXO/Hg6DCEVgXRdZ+VPLzGMKDG0jJR2k9vhuaQx1kj5OecE4MvzNQH4wWYC20WEgP5xxQ0F8QT77UxjL4vvGSjH8v6t7TG4kMO4Z0tX3U/up 6k97CBEL axlt2j9PhDIIScSofvxDXgnipWBj4u11t+znzM8rv0sgRM0W7uldE/LqMh8+7F6pPiL8EJLS+wVh9taaE3Soib5xQrevQ2lLnfEiKqIyksEcQOs1wWSV90bZ0fmGBlwQ2cGXPm/yiusj5oKLXtOhaU7nLAf1v3x/nwc64kJpCbLMVHTiWz20k37z5FBDEPxE8efLAqEBHQInk3oUFo0QNtharnObjhHVoGGPc4x7/umbjHjxnf91dOhFJAb9140prk/cClag5lCIilhl65geYNp+EWGooG499MsrZq0u+xXhX3DEIamIzV4um2ehG8spaXV18HetMBqMut0l2EGrJDLe73F6zrmAay0axTkBNCZMbbDtXdDWxMAVFgem54NOMg38k5ZBfNcvP0nKlx7b2BC+jYc5c2/pixSKgUdhCfqXCbrws0VVNjLreKKdniS8fZpL5Bf3Q9vKcm7ab4WK0XXLZnlz3DFxa2R3uByOzItPI/Z+mTr4wO3/PLRlGBRLyx25Hem4zK3ofprui71IVxknSuh5mMv4pWOtMU8zjmCjgJjkkm1Ik9iVlKjwjnVDIkqUULX+laZxSPC8= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Userspace specifies CLONE_VM to share address space and spawn new thread. `clone` allow userspace to specify a new stack for new thread. However there is no way to specify new shadow stack base address without changing API. This patch allocates a new shadow stack whenever CLONE_VM is given. In case of CLONE_VFORK, parent is suspended until child finishes and thus can child use parent shadow stack. In case of !CLONE_VM, COW kicks in because entire address space is copied from parent to child. `clone3` is extensible and can provide mechanisms using which shadow stack as an input parameter can be provided. This is not settled yet and being extensively discussed on mailing list. Once that's settled, this commit will adapt to that. Signed-off-by: Deepak Gupta --- arch/riscv/include/asm/usercfi.h | 25 ++++++++ arch/riscv/kernel/process.c | 11 +++- arch/riscv/kernel/usercfi.c | 121 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 156 insertions(+), 1 deletion(-) diff --git a/arch/riscv/include/asm/usercfi.h b/arch/riscv/include/asm/usercfi.h index 4fa201b4fc4e..4da9cbc8f9b5 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 1f2574fb2edb..f6f58b1ed905 100644 --- a/arch/riscv/kernel/process.c +++ b/arch/riscv/kernel/process.c @@ -28,6 +28,7 @@ #include #include #include +#include #if defined(CONFIG_STACKPROTECTOR) && !defined(CONFIG_STACKPROTECTOR_PER_TASK) #include @@ -203,7 +204,7 @@ int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src) void exit_thread(struct task_struct *tsk) { - + shstk_release(tsk); } int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) @@ -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); memset(&p->thread.s, 0, sizeof(p->thread.s)); @@ -225,11 +227,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 96bb324abafb..6cd166b73316 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 @@ -143,3 +178,89 @@ 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 Tue Oct 8 22:36:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13827100 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 009FCCF0429 for ; Tue, 8 Oct 2024 22:38:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F31886B00B0; Tue, 8 Oct 2024 18:38:38 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EE2136B00B1; Tue, 8 Oct 2024 18:38:38 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B89CB6B00B2; Tue, 8 Oct 2024 18:38:38 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 967516B00B0 for ; Tue, 8 Oct 2024 18:38:38 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id A5FCE1C52A7 for ; Tue, 8 Oct 2024 22:38:36 +0000 (UTC) X-FDA: 82651900716.16.A69231B Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) by imf07.hostedemail.com (Postfix) with ESMTP id 755F340004 for ; Tue, 8 Oct 2024 22:38:36 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=CmfR6lV5; dmarc=none; spf=pass (imf07.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.179 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728427016; a=rsa-sha256; cv=none; b=YaaK2AIjTU2JYu2UIX0KXjYd0qenqIcThd4vHiWIjJXI/lbTZB2W90GnI/SrgQhuGg/2vD wCaopAR8VSR3WJ3P8fV6vXuTYbPmKh+//8I6U2olKvWXDNlb+VFYyXEm5nRZ2SJL+vV43t fAVbouikH3hq8GaC7FXHtGyBMDv/nPg= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=CmfR6lV5; dmarc=none; spf=pass (imf07.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.179 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728427016; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=1UgKthNvwCJdtX42RjeHROfs17OIsFbY3pdtzFTA+rQ=; b=5oXfpB2CFoCZ/2bcJc4gMfR//JIzlyAju4jeRQdB43+8Tyc+8UCvcOIIO2UShSTZN1D3ub /Un4wAqtI0qNMS5F4AnelL2QwpYOWsychv8eYk92RHItlndux59XMNPkndTc9xX1EVwfCp +YWGdTa+zDiEo6Xl3SFnpMBuHYpcl3E= Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-71e10ae746aso1244766b3a.2 for ; Tue, 08 Oct 2024 15:38:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1728427115; x=1729031915; darn=kvack.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=1UgKthNvwCJdtX42RjeHROfs17OIsFbY3pdtzFTA+rQ=; b=CmfR6lV5uxu0U7Dv1vr3H+XgvH3BP6u7PjaMbwiIdtUp5Q4F7puRjFfyXweVf8c0F+ SvFXpLW6DH4SEJ4srzoUTt9cV+rfa6UXX0eKehZiSzygk+sapmkZhtv/4FZfwtwBsnPO GVJ06Ce2yP/3FgwcojS4xymxd4D9WhGfC2LB5zFFQNkGEWQmN8rWmHOniC3Uyk/bLieX nBvsMq8hWQuzACZvFaRPUdgw0loGkU0FzSKuH+2fVFQrzzqTX0RI5J0p64Yc2t0gAmTE RFRTV1yW+4hkAh3lHfFJLUKpugqyA0KV4XSNnF9/JrRUQBcpa5/Ep0OZnChEFXN0OCmw OheQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728427115; x=1729031915; 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=1UgKthNvwCJdtX42RjeHROfs17OIsFbY3pdtzFTA+rQ=; b=AQ07sZgTl6dBYXtlYEUA3B1lIao4cfO6/LKXaEyO5stvqxzTAnL2S0k81M89Kh4Jwn dMZbCZKU6RrjMxclTQIAnzLkvvMMdbwvOqlO6ZtLexaVKqXlsQ7mQZ4x49LvtjR5NbVc RuH3FzgFrwGspdclcJvpoTt1YGqwQpvAPTkn1ni66WqX5bNAKW+J/PILnE6bhBZEJfar jYmzUlemR1sqYaWTHVDRSll2aEgf/g95mXHX57l6XOmBLlIBFgDFhBJHyZsF7BXp7gBK kwf1YXiSSAu7TbchA4xOp6vQlK+qIihySct9DveW1V2Gj6UFNdO0Az4of17EV0llNnuP YzxA== X-Forwarded-Encrypted: i=1; AJvYcCUGg9GwZiO4VHcN+NHicvzURr+5rpbPA1XQNa2PVS/4C7ESeQuEoNSlJEflys5Tpxii77aGV/8FyQ==@kvack.org X-Gm-Message-State: AOJu0Ywjsv0N3vkK/YaUaTKj15mb3Mnd11K1Rgx2Cts4P33BpzwP/hvs FyIy7hWSQ5TwxHbEANTBMqqcTnWy3e8mBoybW5alZoL+2vGsgZFJ8y3vXAUZK2c= X-Google-Smtp-Source: AGHT+IGY6A1n9vb88kLWbxlmOlKkbeCFyOVTdVthRp7tlV6ruNsdclzAw5LAouQ/eSKzZkmvFgjuWw== X-Received: by 2002:a05:6a00:8d0:b0:71d:fe40:7e68 with SMTP id d2e1a72fcca58-71e1db647efmr649067b3a.1.1728427115200; Tue, 08 Oct 2024 15:38:35 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0ccc4b2sm6591270b3a.45.2024.10.08.15.38.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2024 15:38:34 -0700 (PDT) From: Deepak Gupta Date: Tue, 08 Oct 2024 15:36:59 -0700 Subject: [PATCH v6 17/33] prctl: arch-agnostic prctl for shadow stack MIME-Version: 1.0 Message-Id: <20241008-v5_user_cfi_series-v6-17-60d9fe073f37@rivosinc.com> References: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@rivosinc.com> In-Reply-To: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@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 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-Rspamd-Queue-Id: 755F340004 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: d7nqqkxr6hcqxrbuj9915g8geumqc4zs X-HE-Tag: 1728427116-280415 X-HE-Meta: U2FsdGVkX18DXJumDEdn9p8+70ca3ROVD/ip/hkHDLsTR6vXWIPBNggd20IdSVgiuuj6QlLQ9j5iYAMUBhbPA/+DePgW5p59krwth1abYyxaourfvHfxEHymN04Vivq08MJCdcXKycbytngJN3D5aj6oxtbsnnzsgWD4WcbiVfLotd8HC/BHI35yeZZ2SDQgQ7Fg6fZJf8mbjFTyeVG/cyfzSqE8SYG73TZeMVGp8FD4NN1uesnWt9voUY07qwgfbNm6SzNxQoqib/5EiolRQK7ZHYVk2R1TDVEXK2UbQgeCvjQCcNINKQJWnoGRldLZPPfCGsdZib3Ry1M/zi0Oru4YhAuvHMy76mMOZatsAxC5PXUXSbHhc2Bw9IMUQ0gXL/Yqe7z1AZxNhxgptO5OnCuwbr8cYcOg6NJum0IWACM0Cg7w53ZtpaU4TXREjzJYNo721u3PoHYiegSzZgSAgFRjoY24K6WMl6Jq0DhJ2XkFdiNISqponlVOueWl3raMIj0nz5zGGryC2UfLvmZ9cY9jOad9Q8u3eW9uT474sk7PDLMNzJVVRjB7uSajeYa5uD/lrJDL/P6iM6bceFaLyc3Egb879QpV3iHEWXDDSRHCXluNURyfBYbfTsQbKrVSAdidKg+yl/sKVDoAT+RvkRI1vVp7icjTLi8CG/E4BlWDpWqS+SDjdewPXUf3wIBe/1IIPG68qQ2ToxJVNkjvMT/RNX006OZnp4cHtbXZHfcvoYVqjYTfn97evrkpXMafm/rLvgvlG5M3KPfjfwM1wFBu5UBQBGuOjGb84DDv0F4KUQn4+EheHZD4HTWBx/Dk0e0ONAuxCUY8Vj7Hz8iWOJ7VwQKq+AWdqm09migOra7ShaAoNAL1aC1SYrvFs7tVeMUs6oAx9KLgER6aQIOz+OKoT4Qj5Hs9r+5NjuwXiUva5wKn10NV2l0XQqnA4Q6WaThWw+Fwlu3hjNbJOvy yqXsl97W 2fPROEPhJVzJyo4hioftgsNxvnmf2Iz3x/i90KkexThf32qc/XhSXVfQqVrYCTP0QMHZTRYHfu4FYv2QoY8cUR8xgtSHC0s4NUVQHNMKwXq3CtyNrA6v79nGpd4xpOQNvGEi0Oi07jUkxtvf5LAWGAmLa0umSFpZR+OtUbapwnbeZRY4AQ0rgkiFYSXlTexmqA0Rg8AhJtH0Ky8dCbMPl2YdlgRbjbu9/v7YHwyBfvSEMFS2z0Nx4x158K3Z9kdAVu7tx0mxwkCt9Nd1W2kFYUZt82SlS+ce1eRq6ZbRGzGWwKspwAD9C50PjsR3/1HwFyOOT1j+pRTua8F3I/0EEd2UlaatnQcgaO+++Nrbh4gas8fX7gNPe/RI+jeoJJh4HbG8koVNyB2OHNz3cgKjCpN2OJVB9QO/yKq+K5eq7h8eKSAmY16Gor6WATFRSEWnq2CEAzj0CYLZrrJ35KEihDBYXDane3rPUwjHWpXU+uPaIISPtDC2Pvnc6Mj/n5YsHikRkVaGttWjf7ITJXfu0sK2+2himn2YLcETanxplrBV7aXe07ZrxFQkS/Jt1cMLq5EFJhZp8qmC1lH3yNSTQr6+tuZqJxmvhwxNm X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Mark Brown Three architectures (x86, aarch64, riscv) have announced support for shadow stacks with fairly similar functionality. While x86 is using arch_prctl() to control the functionality neither arm64 nor riscv uses that interface so this patch adds arch-agnostic prctl() support to get and set status of shadow stacks and lock the current configuration to prevent further changes, with support for turning on and off individual subfeatures so applications can limit their exposure to features that they do not need. The features are: - PR_SHADOW_STACK_ENABLE: Tracking and enforcement of shadow stacks, including allocation of a shadow stack if one is not already allocated. - PR_SHADOW_STACK_WRITE: Writes to specific addresses in the shadow stack. - PR_SHADOW_STACK_PUSH: Push additional values onto the shadow stack. - PR_SHADOW_STACK_DISABLE: Allow to disable shadow stack. Note once locked, disable must fail. These features are expected to be inherited by new threads and cleared on exec(), unknown features should be rejected for enable but accepted for locking (in order to allow for future proofing). This is based on a patch originally written by Deepak Gupta but later modified by Mark Brown for arm's GCS patch series. Signed-off-by: Mark Brown Signed-off-by: Deepak Gupta --- include/linux/mm.h | 3 +++ include/uapi/linux/prctl.h | 21 +++++++++++++++++++++ kernel/sys.c | 30 ++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+) diff --git a/include/linux/mm.h b/include/linux/mm.h index 57533b9cae95..54e2b3f1cc30 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -4146,6 +4146,9 @@ static inline bool pfn_is_unaccepted_memory(unsigned long pfn) { return range_contains_unaccepted_memory(pfn << PAGE_SHIFT, PAGE_SIZE); } +int arch_get_shadow_stack_status(struct task_struct *t, unsigned long __user *status); +int arch_set_shadow_stack_status(struct task_struct *t, unsigned long status); +int arch_lock_shadow_stack_status(struct task_struct *t, unsigned long status); void vma_pgtable_walk_begin(struct vm_area_struct *vma); void vma_pgtable_walk_end(struct vm_area_struct *vma); diff --git a/include/uapi/linux/prctl.h b/include/uapi/linux/prctl.h index 35791791a879..b8d7b6361754 100644 --- a/include/uapi/linux/prctl.h +++ b/include/uapi/linux/prctl.h @@ -327,5 +327,26 @@ struct prctl_mm_map { # define PR_PPC_DEXCR_CTRL_SET_ONEXEC 0x8 /* Set the aspect on exec */ # define PR_PPC_DEXCR_CTRL_CLEAR_ONEXEC 0x10 /* Clear the aspect on exec */ # define PR_PPC_DEXCR_CTRL_MASK 0x1f +/* + * Get the current shadow stack configuration for the current thread, + * this will be the value configured via PR_SET_SHADOW_STACK_STATUS. + */ +#define PR_GET_SHADOW_STACK_STATUS 74 + +/* + * Set the current shadow stack configuration. Enabling the shadow + * stack will cause a shadow stack to be allocated for the thread. + */ +#define PR_SET_SHADOW_STACK_STATUS 75 +# define PR_SHADOW_STACK_ENABLE (1UL << 0) +# define PR_SHADOW_STACK_WRITE (1UL << 1) +# define PR_SHADOW_STACK_PUSH (1UL << 2) + +/* + * Prevent further changes to the specified shadow stack + * configuration. All bits may be locked via this call, including + * undefined bits. + */ +#define PR_LOCK_SHADOW_STACK_STATUS 76 #endif /* _LINUX_PRCTL_H */ diff --git a/kernel/sys.c b/kernel/sys.c index 4da31f28fda8..3d38a9c7c5c9 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -2324,6 +2324,21 @@ int __weak arch_prctl_spec_ctrl_set(struct task_struct *t, unsigned long which, return -EINVAL; } +int __weak arch_get_shadow_stack_status(struct task_struct *t, unsigned long __user *status) +{ + return -EINVAL; +} + +int __weak arch_set_shadow_stack_status(struct task_struct *t, unsigned long status) +{ + return -EINVAL; +} + +int __weak arch_lock_shadow_stack_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 @@ -2784,6 +2799,21 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3, case PR_RISCV_SET_ICACHE_FLUSH_CTX: error = RISCV_SET_ICACHE_FLUSH_CTX(arg2, arg3); break; + case PR_GET_SHADOW_STACK_STATUS: + if (arg3 || arg4 || arg5) + return -EINVAL; + error = arch_get_shadow_stack_status(me, (unsigned long __user *) arg2); + break; + case PR_SET_SHADOW_STACK_STATUS: + if (arg3 || arg4 || arg5) + return -EINVAL; + error = arch_set_shadow_stack_status(me, arg2); + break; + case PR_LOCK_SHADOW_STACK_STATUS: + if (arg3 || arg4 || arg5) + return -EINVAL; + error = arch_lock_shadow_stack_status(me, arg2); + break; default: error = -EINVAL; break; From patchwork Tue Oct 8 22:37:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13827101 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8D15DCF042B for ; Tue, 8 Oct 2024 22:38:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A77936B00B2; Tue, 8 Oct 2024 18:38:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9D8276B00B3; Tue, 8 Oct 2024 18:38:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 803EB6B00B4; Tue, 8 Oct 2024 18:38:41 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 61FFB6B00B2 for ; Tue, 8 Oct 2024 18:38:41 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 838511A0C8D for ; Tue, 8 Oct 2024 22:38:38 +0000 (UTC) X-FDA: 82651900842.25.270E96D Received: from mail-pf1-f170.google.com (mail-pf1-f170.google.com [209.85.210.170]) by imf16.hostedemail.com (Postfix) with ESMTP id 34F4A180008 for ; Tue, 8 Oct 2024 22:38:39 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=RxGlstgC; spf=pass (imf16.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.170 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728426983; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=H2HJsBemoUtn69Y0WdrOXv10G4III+URyauyH8vTu7Q=; b=JOhFNq6y5YBG9ybP4VPiVv7v+WHfV0oLdkR64G27Yl0j+/67JDlIqampFYdKhnm5ADaRB+ MDeRkgx3t0ieFV6RdG7NyTlOTQlQd5gVgyzy739t4fbj6dtvKrB7B8Q+UHLZhMupYvRLoY nuDqSlr9RgK0uKp+hl1vb/6NOivS3C4= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728426983; a=rsa-sha256; cv=none; b=MbkKHnXAiXbuGNezlljQbDANnAaObSUNdfQA74RAwBLsPhI35Oosxi58KfAJgr39/cVODb YTXp/kJdPE2Rlz6RTcfty3fy6lnUOF3lzJDZacCDKAklJOdhZUnodYn+7iOxbQgXxfge+N NlayqD/PYV5ublIvXG4IYZc5pIWs7VM= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=RxGlstgC; spf=pass (imf16.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.170 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pf1-f170.google.com with SMTP id d2e1a72fcca58-71dfc1124cdso223664b3a.1 for ; Tue, 08 Oct 2024 15:38:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1728427118; x=1729031918; darn=kvack.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=H2HJsBemoUtn69Y0WdrOXv10G4III+URyauyH8vTu7Q=; b=RxGlstgCBbc7eQyQRLs4M7YDcM7b6tL7b+ZxW6U9b1pM4YlAXMJ3FSsuYdtHhtQi1J 8/I00wh6nYrWnYwfm4dBz8/T69TYtylRDB5eVttlFkM+FxazMM1I1OHLTWILuqz4sOwO Tqp4BEqob9NvKF0oAoM/HS2RdoOx9b5sGvJOVqdGgwbZWOB/B8+DSevvy7fVCsJ/rJ5o MNxW9qTXAzGv7x8xmdA0TPRo5dldGqqSeDyfcLQqx8kMItr+KGa4aqQl9XhmQvEI3uwd 0X7coVboR+pMM5E1zwdrVnnK98bkE+sZbM0rZrkxkHUTUJXQVe7fSjHIKIKZ/DLKpnki w40w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728427118; x=1729031918; 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=H2HJsBemoUtn69Y0WdrOXv10G4III+URyauyH8vTu7Q=; b=HXpIJf9P9e7rK3qQdS+Oatn3g1ZALuW1SpGbUk2ZYfUswRbP1ZfEV7nLk8gtVoJjUP w+5j9ctT3QlL2/9iYdTzMclcz3BCsOEmBW+Cnb8n1z1cQEH7MlHhy07vXsr6JWFU2+AH waU2sZ2pYj1uL3XIB6cGo5jB4ElMG/bKO7Th59/Y+LpKAr7Q7em64kFlWn5ut2nxVfS/ j/aFySYfU8WJD8kRpNjaAohcwS8fwPeCUKCYST7577qW7n/IGv51YINSs77OvauBQZ4C frKAfZPQpOh+/TLUR8ywU1b/UEQ1p4K+oOB2GyG1wNGhulKls0NN+IpoQlTb4pvpJvfz tXTQ== X-Forwarded-Encrypted: i=1; AJvYcCXj4IYiIgGsyVZ4uRzQTzMoXxi1rhsEfs3BHbg4L+O4KKydwVbxSJ5Rax9mQASakH3Brevrc+HRpA==@kvack.org X-Gm-Message-State: AOJu0YwAo8yna1/Jv3sQD2Jncn4SIut59qLEaB5vRJ909OE9RgCPEoNt cKYKk0vyrGYjhDBewUsPhPDHKoDMyTaQtLQUT5YxJ/4UU+3eOAbQBhIuUUV2pAE= X-Google-Smtp-Source: AGHT+IG1wGSlqMBE3PBvpa6FjggvdA8TOV0s8RC2JgpdWWcLYtxJbO7UMJi9+bIzOEiti8s0uhAHTQ== X-Received: by 2002:a05:6a00:2e08:b0:71d:f459:6df3 with SMTP id d2e1a72fcca58-71e1d6a47f2mr972694b3a.10.1728427117956; Tue, 08 Oct 2024 15:38:37 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0ccc4b2sm6591270b3a.45.2024.10.08.15.38.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2024 15:38:37 -0700 (PDT) From: Deepak Gupta Date: Tue, 08 Oct 2024 15:37:00 -0700 Subject: [PATCH v6 18/33] prctl: arch-agnostic prctl for indirect branch tracking MIME-Version: 1.0 Message-Id: <20241008-v5_user_cfi_series-v6-18-60d9fe073f37@rivosinc.com> References: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@rivosinc.com> In-Reply-To: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@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 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-Stat-Signature: qxcj7bmxe81o1j888kxkp4f8atxcennm X-Rspamd-Queue-Id: 34F4A180008 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1728427119-421936 X-HE-Meta: U2FsdGVkX1+O8GiHIpEGgz1VgnjRWk4tdF6c5nlvMm5a9BsTg0XCSBvbSF+zFuTwvotEUjFB0LVDJHD0kElD66nImktJGeFDhdpRSLdXpsunL7vyfWimQCZuSEAwDpk2MEdL4I8tvuID7TumTr/jywv0lLC01CQcMCcj82eac/L2tTGrlY/cNd472R/EapRWogBUJq+sBfABglOIrqPTGP/vCCDDAgTSFgJz2V7iLhjfdQdJHjGQR4xhlgNDLxSVfO7xxATTjV77AmrbLGsyU/LXiceCeuIIGOk8Z4unC69soLURTDY1GPuGAueoJ/UGyRmkF+j9oA1MHILDlkgbXzT4o7NCM0aWwbMBfm5Qs89iyQtdkApvU7B3MkP/Hgf5SfLqSt8xxmKGgRrZqW4BBpuSDRKb4fgeOsgWuKFM6csw31GAmEzv2ROxokPmEVmWIZd2EoQ9lGLCP6OobGf0USLHMrieR1fBfhm1pEqPy7I6zt+zYlJtm4FmMtn4k3Bi1DO461jkr7jCBuBWzLCjwks8FgjJSftpxzPAcCotU5hLG91TsDYgIEgohkPhTdDgKFtHXF1XnL4nrKeXHigk/C4Qi5vVpBIlwGCXbIYRRKkKNeCD2SsbthkoDV9gPPdtg0cgGWm0JoGuqwzc4Jr7VHHQJtbPmfThf2qolrZI+NQzLYmwjKhV7fECYPVCmTa4UehI6Gp6OEQXhumsT6ASJhsHPbmw56ZfnbKMsvSwOxTyx5GEf2MKoArrx1wG3V4eNIzbHvEzkZrtSfjrVfZy6uAf8R3C+IfNwPzB7M0iJN+aO2zy1pPsCaI3Cuan84eNHxnubDl6J+6o1UzlAg0eXkB87PLL359zH7XmpzbDkK0sPkx5rvcpteerfSf6jAtecpkdDUXYRc7oP5dotC5xnfda/fOsPyqgrwmjwKrZYIKFsGx8G8OYGsMyG50fZjMI1FBdY/+WaeJswqBRb2k ZOCisI/z AYy9Z1hDuGRdtXO7476yG8/tQtQwvy5MtR3rQsHbUXumuYeomnOR5s1LNhS81V5h7i8clgTuYYFjmvEIiaEMdMAOFydkICKpA7LvxmweroEQqoZiNk6xlMIXiZWUuk5+o+UTsx9eq9gBZYPDca8JHWgTOQQV1NeUzTjj/le0AwLJaO8KzqH5/92K89pVs0yrO8hlD6JE6RKLa10lvwGhxmJYrOsiMHJkYd7MzKco8z+rPSbWJ9ixlW4Go8VdSxXglTOKKpCEhNUTRycELm3JZ3zRgCZZNVK8j6lLYL0WOmWTfvXuPW0EbYZ1fX+vR0T5rDZ1WhNr3R22cWXS6oqOthfQMFBGECRQEwPn0xmO29tZg6d6ZOv38jwidd4V+fIBhq7A8i5bpOXIcyqJAMgOHAGxPtF+M7jeeT+2TPCW2032twjPnZOpWEXKf6Kc7x/YrFheDbTM2GlbceXTno/mE8wUayqPguklLfRk9qn+ZMMwbQNsg4pfwXcOejlZ1Lwiqoln0Mi6wq+ENxrVTb8tFEk2EWV+2gPPfunEcd+Ww1CKC3XXJwM8tXUDXuzx4wd6pNRqkzsw33nJb7l8= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Three architectures (x86, aarch64, riscv) have support for indirect branch tracking feature in a very similar fashion. On a very high level, indirect branch tracking is a CPU feature where CPU tracks branches which uses memory operand to perform control transfer in program. As part of this tracking on indirect branches, CPU goes in a state where it expects a landing pad instr on target and if not found then CPU raises some fault (architecture dependent) x86 landing pad instr - `ENDBRANCH` aarch64 landing pad instr - `BTI` riscv landing instr - `lpad` Given that three major arches have support for indirect branch tracking, This patch makes `prctl` for indirect branch tracking arch agnostic. To allow userspace to enable this feature for itself, following prtcls are defined: - PR_GET_INDIR_BR_LP_STATUS: Gets current configured status for indirect branch tracking. - PR_SET_INDIR_BR_LP_STATUS: Sets a configuration for indirect branch tracking. Following status options are allowed - PR_INDIR_BR_LP_ENABLE: Enables indirect branch tracking on user thread. - PR_INDIR_BR_LP_DISABLE; Disables indirect branch tracking on user thread. - PR_LOCK_INDIR_BR_LP_STATUS: Locks configured status for indirect branch tracking for user thread. Signed-off-by: Deepak Gupta Reviewed-by: Mark Brown --- 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 bdcec1732445..eff56aae05d7 100644 --- a/include/linux/cpu.h +++ b/include/linux/cpu.h @@ -203,4 +203,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 b8d7b6361754..41ffb53490a4 100644 --- a/include/uapi/linux/prctl.h +++ b/include/uapi/linux/prctl.h @@ -349,4 +349,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 3d38a9c7c5c9..dafa31485584 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -2339,6 +2339,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 @@ -2814,6 +2829,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: error = -EINVAL; break; From patchwork Tue Oct 8 22:37:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13827102 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 47293CF0429 for ; Tue, 8 Oct 2024 22:39:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B13A66B00B4; Tue, 8 Oct 2024 18:38:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AC3C16B00B5; Tue, 8 Oct 2024 18:38:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8C72F6B00B6; Tue, 8 Oct 2024 18:38:44 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 6C8F16B00B4 for ; Tue, 8 Oct 2024 18:38:44 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 75E9E120CDA for ; Tue, 8 Oct 2024 22:38:42 +0000 (UTC) X-FDA: 82651900926.17.D6EE6E0 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) by imf22.hostedemail.com (Postfix) with ESMTP id 13B5FC0010 for ; Tue, 8 Oct 2024 22:38:41 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=VTTIHE0K; dmarc=none; spf=pass (imf22.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.179 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728427078; a=rsa-sha256; cv=none; b=ZRbkSGbe/VV0u/9bcnVQSqP8qzF5Q+4dNv8N+OBOfdneNBWRKB5JGH1ALGXkQ0egZ77eqf eXHY+/oQsap32I2JPF0FXnarFrjQUDc3rX1+9C2aM2VQtkEvynygEnPbf8khpBwtbsJPWx fptTdQWeLzKmEb22hxu1S2XEpt0SoqI= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=VTTIHE0K; dmarc=none; spf=pass (imf22.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.179 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728427078; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=+IBv6y5g/XQfRfM7PVd+NrzWS0a9vjXfXxzedLB6inI=; b=IgVPhDERt3Zf0UNBXsaDzeP5nsW7a8kDpp1Fteaw7HhGINxiGZLe09d86Ksll/oJOd0aBv f62bK6R6nnDat6nJzIVT6fc1A2IZSPqpJaLXACYBEWUeo0lHggkG13rsk5zH4MAKZtYIlg S210tl3vtqGQYdSkQEKiDz7S8v2cgXA= Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-71dfc250001so2440692b3a.2 for ; Tue, 08 Oct 2024 15:38:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1728427121; x=1729031921; darn=kvack.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=+IBv6y5g/XQfRfM7PVd+NrzWS0a9vjXfXxzedLB6inI=; b=VTTIHE0K3PPzQs9QzuAQ3DuqqpXxL5VUe+MC83Iu9jgGUjMjIYpXNJP7eRYWG07KrH i6JQ238YD7Jk96f23sOS3q7CEs4SjM9+9zbdYfSRi3J9U6ifSB6EDUpLoRADkvNGdu3a pgKvNv6ddmdaRFZEmrHyeTC+poFed2i/0JaxQyPUv4c47za10lji78J15ebN3YnyRqK4 QvdHnfP+Sl+ma84QWP86WWuFLjTxsoQWh2+Y4RdxwnAPGFcrq+sQrpt9v6s1kiQ95M8+ vS9/54gRChLQ67nUMmhRePDgXEVGWIyIZ5fvhZsA1+wER4TJM/DACisIqAvAv8isrJgU ShTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728427121; x=1729031921; 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=+IBv6y5g/XQfRfM7PVd+NrzWS0a9vjXfXxzedLB6inI=; b=dTIaL0UutCkpOQ9v9HW/ie4zvv/3985w/0j2MEzbq3WxuZnQzqK7kYlWZHqxLXBLW5 G97qjETvvnS/T4T8Rp4AwKOrT+oYhCLlSmV5KYZaCOzXonfo7cDpm3nZv34D1FIzAmrH qdJpUYp9bPKbWzRNly9Bqz+EttCQH6u0PmBi9ki0igT9w7C+Jq9ua4RWr/wLuF2Xshbk WNyhAHSZ33Wsv+fkL5gFt+Uf18dkHxiSWdQ83K/EJk+/0PJ3fIQk7HJ/h3RefY6J9OWx gjNOQXeMK3MWp/19WiGjKUsiiGS77aV6cxyubtI/mTeLLjzTDC9q7/PjEeXo1nn9TYs0 qCKQ== X-Forwarded-Encrypted: i=1; AJvYcCW1Nb9Kt6tkwtI3/8AY0bc/2cIS9XyW0PEIsj49as/FGBTVALkOBRVwf6Yb48m0AubbW1cvCGcAuw==@kvack.org X-Gm-Message-State: AOJu0YwKiv+jcRZkx10eAfQHuWmKIxQcUJrbJYIWXuFJ7+m/wXbBacy7 86KH+zkKwRqCCjREGdaqk4lhSQRfFbZTiCTuWD203S2PHw4CsyE19LiZSij3Ll8= X-Google-Smtp-Source: AGHT+IEEDZEG6ZHaENLvzcfnn+S6ck1KUevwKGyZCx7BXAYd7lvydRa0tA4mrbzbJrIZdKFkpkoDsQ== X-Received: by 2002:a05:6a00:2d89:b0:717:945e:effd with SMTP id d2e1a72fcca58-71e1db65b58mr647001b3a.1.1728427120735; Tue, 08 Oct 2024 15:38:40 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0ccc4b2sm6591270b3a.45.2024.10.08.15.38.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2024 15:38:40 -0700 (PDT) From: Deepak Gupta Date: Tue, 08 Oct 2024 15:37:01 -0700 Subject: [PATCH v6 19/33] riscv: Implements arch agnostic shadow stack prctls MIME-Version: 1.0 Message-Id: <20241008-v5_user_cfi_series-v6-19-60d9fe073f37@rivosinc.com> References: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@rivosinc.com> In-Reply-To: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@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 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-Rspam-User: X-Rspamd-Queue-Id: 13B5FC0010 X-Rspamd-Server: rspam01 X-Stat-Signature: keipj9a9shqwcqgiewrisbfk7ah45ay6 X-HE-Tag: 1728427121-470311 X-HE-Meta: U2FsdGVkX19slhhnjCSkcQ6CUQ3I2+t7N3PaPKXZZ20y/nm0gDQmLSpMr4YinJHB2ZdDPkH7c2zLZ4qHNa8cRoJg8Ku3FnOYTFNnFIyKb0dwRG3fawhhXLdCkSI9XnokjPLJ8u0wMB8CM3VjTzbLahEIln48Ps9POCMb58mU8nvKWo0vp4BHY2KoxOAK6s4y/T4NIi9pWCtecOR45xC6h8/tGHjQa52DEdQnPnLsYaKMRKcGOcNoD8PfFPopudgcBw00XtNlH0zvAa+3br7NcOO0DuotpMF/4o3bw4U2B7ono3RQ8lOG4Sg1pT9LhEdNchmJUdJswMpAdzhpkQluhGnfMUUAfktVao/Gdtou9yY1cb5riQz2RzhLG5yqbfvRAtlGbfpSIqEURS1xUhe0cL807O9cQGJ3nSIZk/HxpncvwTXH/kR/+pXFmFekzEX8PlAJyMH1pJxYC0ta6yEqdgZviluzGDx0EepgI+3zeyEcruFSkcLGVNF91YLvvkkYuhNwxuEose6qdLEl8bQca96/SVSAVFbJdafedpUxJIfT0Hue7XsyQQWHJs/LKb9YVNvCcSGsh2xABblGb8vVWQZjNnuD0nvUHDQGgoNwkBsI6Mj5orL3e97ir+H0afQrW/X69CC2XfTZneb4/wmK7y62zMkp08MB3/OcNkrilJ1EyYpaqIXtO/PVcxURcUvsqjc/jzOFangXLBSdat2qfENFRYrDdqdftPZOfs97ub5rqRuqdtTyBosNTVegXe3LTTn3D2FujSxSkafB3PTQLRsxkl83DYffVi0FI8P1lbghCGR4h6by3LbfRtNeTwy++rkRhb2Oc61K8WtnNvnW0pgE39ezmq01GU24v9ftDyPl8E2dAdm5RmsQWa7AD2g0zKdEXIRo+HcotDjNlzSFaSk3+vFeqtsJTDXl54vP5V10er/ZJgUTvqwvXplflGEZhxM4i7ZJe87AkxFKK7t MnK9/a9E n/kXwtiq7o6LDWzwZ6lDyXpROV0b+ww8bQQZrId+yF37bsmWPrN1/1qaRHHJzc7qjoR7cfPoafGMecvqq+yKSRoli/3bNphKvc2huAblkQRdxIFcC4OhSyg+DhUi/w4+1DI7X6pqQKGhOMYicafoLhRPTDVcb+M/fzH/i7BW/NUBCm/IkEtA4pBtOuIYKzoVRP5q0VcLWKmxkdtK02CnpZuCrELPw3p6m250COTrmcw7dm48gnsLIZfezCOyCllV6N/AoSdSlyOBNLladTLIACWdYVaGoozmTwf2CsI4T9J87pa1tuwIWaQkDHRc5lSdH6d8Ijv4EXS2DWi15YNav4n6DbsQ1MQkYdOZUgsVFyDY+LUHaUytU7XiYlbXcChwBD7EaPySdOqoM04osFIN23//PPhX1i6UTHjp1NbX8AfnJft9Le2dpx4jNGFCjwPBeDpbLX9tZ+SRK0sc= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Implement architecture agnostic prctls() interface for setting and getting shadow stack status. prctls implemented are PR_GET_SHADOW_STACK_STATUS, PR_SET_SHADOW_STACK_STATUS and PR_LOCK_SHADOW_STACK_STATUS. As part of PR_SET_SHADOW_STACK_STATUS/PR_GET_SHADOW_STACK_STATUS, only PR_SHADOW_STACK_ENABLE is implemented because RISCV allows each mode to write to their own shadow stack using `sspush` or `ssamoswap`. PR_LOCK_SHADOW_STACK_STATUS locks current configuration of shadow stack enabling. Signed-off-by: Deepak Gupta --- arch/riscv/include/asm/usercfi.h | 18 ++++++- arch/riscv/kernel/process.c | 8 +++ arch/riscv/kernel/usercfi.c | 107 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 132 insertions(+), 1 deletion(-) diff --git a/arch/riscv/include/asm/usercfi.h b/arch/riscv/include/asm/usercfi.h index 4da9cbc8f9b5..0b3aff008c85 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 f6f58b1ed905..f7dec532657f 100644 --- a/arch/riscv/kernel/process.c +++ b/arch/riscv/kernel/process.c @@ -152,6 +152,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 6cd166b73316..6ac5e87b4c70 100644 --- a/arch/riscv/kernel/usercfi.c +++ b/arch/riscv/kernel/usercfi.c @@ -24,6 +24,16 @@ bool is_shstk_enabled(struct task_struct *task) return task->thread_info.user_cfi_state.ubcfi_en ? true : false; } +bool is_shstk_allocated(struct task_struct *task) +{ + return task->thread_info.user_cfi_state.shdw_stk_base ? true : false; +} + +bool is_shstk_locked(struct task_struct *task) +{ + return task->thread_info.user_cfi_state.ubcfi_locked ? true : false; +} + void set_shstk_base(struct task_struct *task, unsigned long shstk_addr, unsigned long size) { task->thread_info.user_cfi_state.shdw_stk_base = shstk_addr; @@ -42,6 +52,23 @@ void set_active_shstk(struct task_struct *task, unsigned long shstk_addr) task->thread_info.user_cfi_state.user_shdw_stk = shstk_addr; } +void set_shstk_status(struct task_struct *task, bool enable) +{ + task->thread_info.user_cfi_state.ubcfi_en = enable ? 1 : 0; + + if (enable) + task->thread.envcfg |= ENVCFG_SSE; + else + task->thread.envcfg &= ~ENVCFG_SSE; + + csr_write(CSR_ENVCFG, task->thread.envcfg); +} + +void set_shstk_lock(struct task_struct *task) +{ + task->thread_info.user_cfi_state.ubcfi_locked = 1; +} + /* * If size is 0, then to be compatible with regular stack we want it to be as big as * regular stack. Else PAGE_ALIGN it and return back @@ -264,3 +291,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)) + return -EINVAL; + + set_shstk_lock(task); + + return 0; +} From patchwork Tue Oct 8 22:37:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13827103 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id E10E7CF042C for ; Tue, 8 Oct 2024 22:39:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 657946B00B5; Tue, 8 Oct 2024 18:38:47 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5E03D6B00B6; Tue, 8 Oct 2024 18:38:47 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3BF546B00B7; Tue, 8 Oct 2024 18:38:47 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 1C4406B00B5 for ; Tue, 8 Oct 2024 18:38:47 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 7483C80CB6 for ; Tue, 8 Oct 2024 22:38:45 +0000 (UTC) X-FDA: 82651901052.29.FC7DFB2 Received: from mail-pg1-f182.google.com (mail-pg1-f182.google.com [209.85.215.182]) by imf08.hostedemail.com (Postfix) with ESMTP id E55F616000D for ; Tue, 8 Oct 2024 22:38:44 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=0Jz6AjlT; dmarc=none; spf=pass (imf08.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.182 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728426975; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=CFzRn7yK+zb2Tiv6yx2qVf0tzcUK9b+o3waufo2cSWk=; b=3NV+Kd24QQpu7QEViQ5lIBi6CF6JZQOnI8gnDJjWdd0s3EpZOvM7xqbRP+IoQ/D0/7OT8s 3M8bLakbHnKmVKNIUYEYvj7eibdkGIKEiinHbMJkntlY1qRKPWeaGMX7Lt5ilyrO4zcXkX JOI+HxfFpmDa0OZP2UGR8z76jhrCNpo= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728426975; a=rsa-sha256; cv=none; b=MG28HnN6hBJmhF7IadupVimiB6QuWu5/LyZn37GDo/o5qAL5cKDjntqnPT9B5QF1CPiqBU XMqn4GqflaXug6OKdv/Z1u7aKXnPiN67QhmmUic44pUf2VceHsc5+69aSW9edFXqsX8ErT zFY2K9AR2j6360D8lNTIDLS+sq09I8M= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=0Jz6AjlT; dmarc=none; spf=pass (imf08.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.182 as permitted sender) smtp.mailfrom=debug@rivosinc.com Received: by mail-pg1-f182.google.com with SMTP id 41be03b00d2f7-7e6cbf6cd1dso4066955a12.3 for ; Tue, 08 Oct 2024 15:38:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1728427124; x=1729031924; darn=kvack.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=CFzRn7yK+zb2Tiv6yx2qVf0tzcUK9b+o3waufo2cSWk=; b=0Jz6AjlTpXrU61Y2pFNDAxF1yjAdFm5bPLi5HscBQWU3PCSV61OGyL+o/svpO0d8R2 qOd4bN5+fz+Sb6yHHWIj/s9yfwbtAWW4tmGdSiWQryLTKTmoJTCrgAX5kftupSz0mtNs JczikwxXHvdUswm1dfriUUpuBekqJdI9kL92KD3FYvN1rrlWC3SyT/vat39WQfGLo+Od ffs+VOE3HLq1LQ/VgFvsElPojbgA4OU1aX8CKCgHJDQyTh5OKZg79O9+1Iuj0E4wWRjW 5eX/PPXjUl9p9tpOKKD6Un9VeQk0aElZHXBZE+xUC6BOkxnx6RJ4wtWvf90fkXSoP020 SxcA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728427124; x=1729031924; 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=CFzRn7yK+zb2Tiv6yx2qVf0tzcUK9b+o3waufo2cSWk=; b=n9AKwkkO3eAQL/m7itX/EOxstAz5DmWI7WyTDl3c82G4WC+j+hQWn1J2Fj6P/YiNY9 PBRMp3Tavf8Wa+aai2I2bnr6Fpz1ieD4dSZuWSX40BagEKaoiGLvNbSG5/2xVjpJ5xxf SQLDlOodh3ZRM2NfX4i1+Vrfvk/FLafgSATh/sBwyLwZl+p3Z1HiVn0qEDV64eA4LKxm PelV4LjVm4a6MHTz/gbejZUevSkSAMDibH/bfMoFPBZAkt1dUyMmDhrkFGsKopEf6mks ZvwHMvWgRY++XmV7Bi+Z7rGI6632ATYtT6zrYLOk2ZtK/u+8ZOsY2+FtU3qTesJO9W+E S54Q== X-Forwarded-Encrypted: i=1; AJvYcCUqF60uzJ5CpUh7pSck5pY9KrAY/gLaFs7HQXTzEm83q0QeBKituuoYXlA3j2Zd/m394OWWSARICw==@kvack.org X-Gm-Message-State: AOJu0Yy8eesS6ZEDyxQziO5+YozV7J4sBmGvL0n5wu+jl4ILQIddg2zi 42oEN52Oo1sjnP/E5fEWxlVkI9kQAqq3SdCV6AKDQB4icz75e3Cwl8bsrRuALZ0= X-Google-Smtp-Source: AGHT+IGZjOWVQEC+/AZqsbvhkjiPPt2n9H7gy7d45UMFh/5VTYsXbaB4Vw0qZcuPqja1eH/XHEMmDg== X-Received: by 2002:a05:6a21:7108:b0:1cf:2513:8a01 with SMTP id adf61e73a8af0-1d8a3c129edmr804139637.26.1728427123465; Tue, 08 Oct 2024 15:38:43 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0ccc4b2sm6591270b3a.45.2024.10.08.15.38.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2024 15:38:43 -0700 (PDT) From: Deepak Gupta Date: Tue, 08 Oct 2024 15:37:02 -0700 Subject: [PATCH v6 20/33] riscv: Implements arch agnostic indirect branch tracking prctls MIME-Version: 1.0 Message-Id: <20241008-v5_user_cfi_series-v6-20-60d9fe073f37@rivosinc.com> References: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@rivosinc.com> In-Reply-To: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@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 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-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: E55F616000D X-Stat-Signature: 4js5ftm6bzc8xx4zjky8g11iyo5wbatf X-Rspam-User: X-HE-Tag: 1728427124-201699 X-HE-Meta: U2FsdGVkX190QQ7YQ768qVqgiyJqtA070uT4AIn2v4IMYmgZDW54VDag4qbBSRKu73JWioMIA8T42+o+QGFn+dnmSb0W/k2M7Hyo6NOjMnNrGuYFCI3QtqnzkhvcaZqEyxm8KkcvsweQ1vMd8NexQXH9p2TBQukKTeQsVG4dHkkLtJvz5T+EU+tSZ8v85NrqbIOBuhHzAbkx2cwCHBQkhweGuBETfnMBKUSepo8SDJ5CyjXxFlPAQZHoz6fU+tZz3zMHyi2gUMICIdw/5InOSG5L5Nyxlb0f+RtxQ80OGYICh6VZGwZvAMFnaYhr/BeB818x3GNNQz99cupiAj3gey6FNT4b7GwyZITQ0CTjS3pvBYC853O2h5nQK0GDO+C6IlFHgczpmClc6KealwEBi4suckVr+R3fkXVETl9ZjNsjlNQVuYVL0FVBrg6oZE4fpHXq1cXvo4F94d2xyUD5mJY5hFDaEfWZKhv6vDq9KTZWYZ/IPWSsawXC/9f4pXojpfV1lwZyXtRVF8ah26+/lXRhvis2VjbTRt5S8JYSzh/THPenbrjxU4NlBNEmAHM36zB0DYKh4vqzbUPnDJcSU03jC6I2SYkYifIxjHpSjFKKMBCj/v7ak7sJpenXHcFpDNJDEKi1jOBQtn9v1z//oCl8vtXgirBS00k512kuoDWlW41wJbgM2lV/T1Wpcj7jPIn7JK4KLQWXb48WRgGnqNwISHakLUiueRY5bzidlC2+gu253kvbt78YYeeteu4YLrx+y18MHSEvD/fh3F1QwDBtpA20E90OATVsNM3eeBd9M8tXivtt6LZ4gWREPeWh413ESnH2VDF6ucGD6dbbDTDsH41HkLnOF6q97X3V7cM5daQ0gFQT5/9hye9fGOYQ/IbxfM+7tMnPus7oSjxtB67/7DDhPtB3SGesGCz4ua2OkA9IbnyfCUS08Zq+H1glalM0EY7P5Gg1Zoe5mzm Icf26qL0 XbhoWMKaPhn/PYoNOBXE9kthcLr58shywjbln7YISPd0RduB/jBE/IobavXT70HMKeBHNRfNYDhX9oye+fGGGPgsmaoPs18RpGXKi8OFTVzpzxv87bPfUCV9/gKWWC5DrHqkkBQCnXJ2G70C3e1kl14KgybaAJbZtOpJK5dnTyZHURfPl2cN7R270Ls0WK+Aou1Nveaa785g1iQCLAiCnaAdgRwMnankYhP058eOCCkIYryxDgSh7uwuriF16ulOHj5BOrxbvco1Xv97jlg+PDGbNWmHJHtJNoAYx3hbd7B/slJyRQeEQHFlzG3kDito85vUQoQAHLTtKX3GS/LyGGGZ8/I0bEDyq6YOfYAR2kqwR6JXCaVnfcjMcAJkNMEKP3pocXG9iv+eswc3UPwR5BM2fGY6hIreExXXr23fQYI2N9N4x6PhEku8pt2YXH74L84a6n8r0Mb+DWJY= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: prctls implemented are: PR_SET_INDIR_BR_LP_STATUS, PR_GET_INDIR_BR_LP_STATUS and PR_LOCK_INDIR_BR_LP_STATUS. On trap entry, ELP state is recorded in sstatus image on stack and SR_ELP in CSR_STATUS is cleared. Signed-off-by: Deepak Gupta --- arch/riscv/include/asm/usercfi.h | 16 ++++++++- arch/riscv/kernel/entry.S | 2 +- arch/riscv/kernel/process.c | 5 +++ arch/riscv/kernel/usercfi.c | 76 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 97 insertions(+), 2 deletions(-) diff --git a/arch/riscv/include/asm/usercfi.h b/arch/riscv/include/asm/usercfi.h index 0b3aff008c85..19ee8e7e23ee 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 8f7f477517e3..a1f258fd7bbc 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 f7dec532657f..5207f018415c 100644 --- a/arch/riscv/kernel/process.c +++ b/arch/riscv/kernel/process.c @@ -159,6 +159,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 6ac5e87b4c70..21ea2237efcf 100644 --- a/arch/riscv/kernel/usercfi.c +++ b/arch/riscv/kernel/usercfi.c @@ -69,6 +69,32 @@ void set_shstk_lock(struct task_struct *task) task->thread_info.user_cfi_state.ubcfi_locked = 1; } +bool is_indir_lp_enabled(struct task_struct *task) +{ + return task->thread_info.user_cfi_state.ufcfi_en ? true : false; +} + +bool is_indir_lp_locked(struct task_struct *task) +{ + return task->thread_info.user_cfi_state.ufcfi_locked ? true : false; +} + +void set_indir_lp_status(struct task_struct *task, bool enable) +{ + task->thread_info.user_cfi_state.ufcfi_en = enable ? 1 : 0; + + if (enable) + task->thread.envcfg |= ENVCFG_LPE; + else + task->thread.envcfg &= ~ENVCFG_LPE; + + csr_write(CSR_ENVCFG, task->thread.envcfg); +} + +void set_indir_lp_lock(struct task_struct *task) +{ + task->thread_info.user_cfi_state.ufcfi_locked = 1; +} /* * If size is 0, then to be compatible with regular stack we want it to be as big as * regular stack. Else PAGE_ALIGN it and return back @@ -371,3 +397,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)) + return -EINVAL; + + set_indir_lp_lock(task); + + return 0; +} From patchwork Tue Oct 8 22:37:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13827104 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 25BEFCF042A for ; Tue, 8 Oct 2024 22:39:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F0B436B00B7; Tue, 8 Oct 2024 18:38:49 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EB9066B00B8; Tue, 8 Oct 2024 18:38:49 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D0DE16B00B9; Tue, 8 Oct 2024 18:38:49 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id ACC986B00B7 for ; Tue, 8 Oct 2024 18:38:49 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 196C2140D4F for ; Tue, 8 Oct 2024 22:38:48 +0000 (UTC) X-FDA: 82651901178.06.3A1DCEE Received: from mail-pf1-f170.google.com (mail-pf1-f170.google.com [209.85.210.170]) by imf27.hostedemail.com (Postfix) with ESMTP id 87A8C40002 for ; Tue, 8 Oct 2024 22:38:47 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=Wp6MzIct; spf=pass (imf27.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.170 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728427059; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=j4AiFsRLeV62lBIMG1PY0Kuggf3Da9jB0k3/CgFujB4=; b=aerWvlClZgeDy9ExmdpZN09dL3228ivBvlLeyE3jxdxuAYSXvwdWCmo1NamTSoetzUceOu lth5OjyEIy5cXNbartpCJESbEFdX9ORjsqh+evljAhPSXuxVlXynthQ5Dz0HEGbaJ7gTQw q4SFDTSRzJnFy+oQ7AMpnsc0v3QHvUM= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=Wp6MzIct; spf=pass (imf27.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.170 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728427059; a=rsa-sha256; cv=none; b=v4cey4OO6h9scqpGrU8pBAWahTiph7RTX9HEvMF+1S0UAey9mgI4cGXylncFWCypMQX63o PFI7xICpMDvBznNhSUmN3eWX8R8z39SydJBKFKNQt+MeF7uXrmvuvHkXHEphPiukmXcMpV Cq29SIFwK4+27pYqpdDIRR/13NDxB3I= Received: by mail-pf1-f170.google.com with SMTP id d2e1a72fcca58-71dec1cf48fso3808902b3a.0 for ; Tue, 08 Oct 2024 15:38:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1728427126; x=1729031926; darn=kvack.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=j4AiFsRLeV62lBIMG1PY0Kuggf3Da9jB0k3/CgFujB4=; b=Wp6MzIctOZRVJt0p+To5Q4wH0zm/8w5Og5Es9TXkN6xeqBwbqUqOvwflZyeVUhwrII gIhDvVutg4hUA6DXpYEIpwKkXQr5F/FSEuRDdd8d1b5fd/30sRJgOwX61VmoZzebS2Xw IgTZoHOFgiF49az/oFP6VVOxFWXqDXnkr/Ei0/ix7jjtI4FRB128mq2PEQdQhk+QPJRO kHtEy15dDtQpvcW9Yb/k3TBTnMhmajWX5urdRsGGkmgGTAVLvNTKAHjQTSZ153FTF4tw sJPtXMg8acefA4kd+9SKEdLOKmjF6gp9zqxPzhVPkjwXP+uT6Yj+4LgqjytvaMzwYey5 ySqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728427126; x=1729031926; 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=j4AiFsRLeV62lBIMG1PY0Kuggf3Da9jB0k3/CgFujB4=; b=tE0WVeEPoZ2RkiyGUifBJ7gvQlip1ITlDJHQdadaaqz5u0Qjcw7ARpFNEAR+Z19Uzf I3ImmNDgpAbz1rYUQjUBQ6Wo5ZgWyr5JGs85r9Jsbj0FEKElILvWoa1ugHPtnDS+4/rs Mh4vthJBzTpthFwe7haWqd/H+tToUR66qAdQVfUO5skLdrQk0HHXTKm3imtC3A8cOBI7 Wp0erzgWiyQIyu4x4QfDq0tyH6BXzlXT1nqZma0rrdXEuzbgN8P0Oru7/NbnTMj//kbU EtjlPLHx6Dv9plGprjgz5x9SlcxwolGv1JXQdl4y7pqCdeawH1n4xbVy+IMvYakCqA64 RNnQ== X-Forwarded-Encrypted: i=1; AJvYcCX4c7DNEY2D/0et1kmrlfB7CjH2wb20AuTTnrKtgI8czrjIlse/QUJOfIak3CeMMJlMh9Zjtvr0lQ==@kvack.org X-Gm-Message-State: AOJu0YzAViXI0a0y+kBDgqDZcwrDs/A8scITn338udo9s1SxvWx2plDf 9Mq09VfP2lgZs9/nSprdGzWHRTQbLrIboZwWxuI7+hfhHgZ+/JllJtFkosy2YGA= X-Google-Smtp-Source: AGHT+IHj+wxxQcaXZGibjg+ho7ihWvomHSyh8+LLrL/jLeKQmqCGUMNbfsMhMaogGrs0QJ7WN9Lawg== X-Received: by 2002:a05:6a00:198b:b0:71e:135a:ae5d with SMTP id d2e1a72fcca58-71e1db7555emr586407b3a.10.1728427126363; Tue, 08 Oct 2024 15:38:46 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0ccc4b2sm6591270b3a.45.2024.10.08.15.38.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2024 15:38:46 -0700 (PDT) From: Deepak Gupta Date: Tue, 08 Oct 2024 15:37:03 -0700 Subject: [PATCH v6 21/33] riscv/traps: Introduce software check exception MIME-Version: 1.0 Message-Id: <20241008-v5_user_cfi_series-v6-21-60d9fe073f37@rivosinc.com> References: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@rivosinc.com> In-Reply-To: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@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 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-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 87A8C40002 X-Stat-Signature: yq3yjhz7imzwoj7k4s6gi7168b3zk3rm X-HE-Tag: 1728427127-607724 X-HE-Meta: U2FsdGVkX1+B/ttZRwZEu+PNn+JadzQjxTmo084U5SM/QusjN7RCUG/fbx7R0r9uqdf+4ZaEyFGX70CSdOSpYUo4p+U70rj+zK4es2Dx23mP5ak/Lr9owj3kgqqnXva5UEoytFn6KUi4cii6RfajF2EmB2XRUi9ld+XbmXV4zQxFtpbVGbYFJsUGOMTSL+YYTT+vAtn66MeMIX2vrVgp84IHWvCg2WJlIcYJZCZWAoDnunlCd/dHhPFBd1L83WrLi6Hu8oqD0HddbE2oFP/FpbowSR9D5/6yKVzsNHznn41zSpopB6NrkQO17AB7jrRJF1HrEh+xAbu8Eby2fpEE7zY6aXbVt8/9tKe10dp7noAZ6sVCkD4QKT4Wcwooiz94uZ8cipm/p/e0rujcuntBbv0slPh8bIi0o5NraeJjEBakZM2IhzQ03tUWXrtinJB/EBIEdD42rqU836Pa3ai06xv8CimAOdvMyZONM7/g0sQVz+lOMZvBNaqIngNg0i052vk5zv9ImJHyvK1WSwF5UQPYExF9WsfFnVhMFi6nKIrY+miIBy5ctgH6QssaP/BmXx1y42mCW9mpsQU1aREKYo7HP6Mmp5cBW0j5SCaUpS0ufgqIzrWm02vY9I8BX42TLdGHxtMRMHCdbKQ7vkpxQwUHiVc3/UUaHnk2qFt0mtkusKD2FAR/iUzlCZpR/eq/YMw9EqsyBowaST9xbaYA/ccMynzNiNtSt3NqtI06GT+eqncatH9K3qFDgNumsH3cfz4LSTFk6+K8QlSLXwx/U6HD69O50nMxdtHqN9ohLeBSpXXGg7K06b5Qh9havDxiBplKOQ6RiK1uu3IPzilUXTuyfa9M8UTDvug1Ml6nY0aGYpvfk2oY5M1gYSr0xvcFo7G/K9j5wOnxO3qflV7Kuda0vn1WfOgMFrh3dyHGekSjpAUEXt903lxIykbvkjR8RMwerQFn3ptFwbEixkg 2RIolt6t bSqWVMUSRYjZX/1TqiiZLCw8kABE6Q67eHwyC1QwB1qpMy+JhxMaduUlqRgHWOH3tJLel0BxYx6DGSTObwTuHtKVMaYV5gSzzBPf0AEueAAFqsb1SogLidA8iE6GQZv/2OhHi7g6fkVvf/Yfkftb5D4uK1TKdQH2nmTvYCEHZFScr6VreYwgO1xkCd6dVnFx5gud+JKtw/wZI3EezfMUNXwu9yvkIkkxHSLR8IG7yo70UpgBSuoHMn/567NoSBu7dQxhKNoFnmfkBF9hrSMBZqGCdRpT9kB6t0e/U4zJ30mUehrsppIFQRby6PvF0IntDrbWCSzXwbX1SAnIkr6K//OLiOQjpKoizY8kfSeDXpnBBOTmW5FKAUhYjMiopK662X9vfnLnLoQ16cbDdXxgJElBa5rbYU+OXNBDqJQ9+nBIY4rpJa2U66XCn2sIz8NT3x6eQ9N4I9fYHywqecdsNz26YFP146fZHRni9ziRUbaZiYTiJIFOGqJo1t2R/09L/Z8vPSrBK9AU1kXld3Ov8b9OAgR0o9rQamjlR73GNNg5HN3FM7yxhrL4pjvm+vlF0A5DVR3JyQVjcPls= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: zicfiss / zicfilp introduces a new exception to priv isa `software check exception` with cause code = 18. This patch implements software check exception. Additionally it implements a cfi violation handler which checks for code in xtval. If xtval=2, it means that sw check exception happened because of an indirect branch not landing on 4 byte aligned PC or not landing on `lpad` instruction or label value embedded in `lpad` not matching label value setup in `x7`. If xtval=3, it means that sw check exception happened because of mismatch between link register (x1 or x5) and top of shadow stack (on execution of `sspopchk`). In case of cfi violation, SIGSEGV is raised with code=SEGV_CPERR. SEGV_CPERR was introduced by x86 shadow stack patches. Signed-off-by: Deepak Gupta --- arch/riscv/include/asm/asm-prototypes.h | 1 + arch/riscv/include/asm/entry-common.h | 2 ++ arch/riscv/kernel/entry.S | 3 +++ arch/riscv/kernel/traps.c | 42 +++++++++++++++++++++++++++++++++ 4 files changed, 48 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 2293e535f865..4068c7e5452a 100644 --- a/arch/riscv/include/asm/entry-common.h +++ b/arch/riscv/include/asm/entry-common.h @@ -39,4 +39,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 a1f258fd7bbc..aaef4604d841 100644 --- a/arch/riscv/kernel/entry.S +++ b/arch/riscv/kernel/entry.S @@ -471,6 +471,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 51ebfd23e007..225b1d198ab6 100644 --- a/arch/riscv/kernel/traps.c +++ b/arch/riscv/kernel/traps.c @@ -354,6 +354,48 @@ 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 Tue Oct 8 22:37:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13827105 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id A75D8CF042B for ; Tue, 8 Oct 2024 22:39:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C78226B00B9; Tue, 8 Oct 2024 18:38:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C249F6B00BA; Tue, 8 Oct 2024 18:38:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A9D096B00BB; Tue, 8 Oct 2024 18:38:52 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 86C706B00B9 for ; Tue, 8 Oct 2024 18:38:52 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id E393F140D47 for ; Tue, 8 Oct 2024 22:38:50 +0000 (UTC) X-FDA: 82651901304.03.68A3490 Received: from mail-pf1-f175.google.com (mail-pf1-f175.google.com [209.85.210.175]) by imf25.hostedemail.com (Postfix) with ESMTP id 4D4B4A000A for ; Tue, 8 Oct 2024 22:38:50 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=UX28OeQp; spf=pass (imf25.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.175 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728426994; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=ktGiW9xDXTg+EugQzK6XNlnuaeQzDVGAh9/x4DoNqSE=; b=CBz9g/zcoNMamUpb1jy9Vc7PztpithmC+oradGxZ4369dzZpbdaRUGRLNCXVF0MLBKOHwR YW8GvvzbCe7vDSnqvN1Iv/WVWXZNsCrrbr0/hd202qKvcECp80Danl18XqFcOuFx1bVjh+ HrbXYh7+aJGfiamEQbmvEMKWE5Cn3x4= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728426994; a=rsa-sha256; cv=none; b=xZmLmzyySHnU71rmdyOkZo3rjD1woDK2agAc3qQ9jMQnb0Mrz50UP71AwAb2l1tCHy+27B FJ228K08gy5AMdH4e7PQwhfnYt8kPVnb4TLqMgy07o7tguCN9z4ct0uSzf+GuMFBkbPSW7 dx5NndZNSkFKaNqRDupxKg+7+f6QbO0= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=UX28OeQp; spf=pass (imf25.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.175 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pf1-f175.google.com with SMTP id d2e1a72fcca58-71e038f3835so2292655b3a.0 for ; Tue, 08 Oct 2024 15:38:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1728427129; x=1729031929; darn=kvack.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=ktGiW9xDXTg+EugQzK6XNlnuaeQzDVGAh9/x4DoNqSE=; b=UX28OeQpyo6sMT//R7K6JRfdckGfunlHa212L33kZNqdG8cuK4Z+zDn2RtlaMFaFQT Mr6SDi/9hINBFoZuN6/crTZsPLuK2q6eslSEz1UnepEoDxrpzOQEIqtrORWOfODaaHej ZeYBRNt/w5mede0tM344YQuccHAKgsFTVbo1wZLk5Jm7LIuCvI4F9O87MWXdGVr4G164 cUHY2d4f5ngU5nmAnhCFxgf4hUwVH4qrWYXKmHPqXMdnDETCVxCFS2+NAm3HAhqcZstO /bV9OtV4TGs0JPzIGex06YzyzyYjyDl/e1esIsqjJSZY4OsJjCD5EHvE7yNR9GlQSq4h 8mGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728427129; x=1729031929; 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=ktGiW9xDXTg+EugQzK6XNlnuaeQzDVGAh9/x4DoNqSE=; b=Rki0c3QjtjkVGbwj8IDAiNfrfHxjtCqFamGe//o8Xi1hd6WNRU1gKbfY6mmDOg2Ot4 iPhyYberpmI6rXPT9MW8JXnhsUMdB6JJnWriM4vm8Ou8nJio8/uZ6b12yvJhDzw9DYG7 lHY5z6ohQV6qptYDWOd7s8SaYlkUoC4RJPuUGB2uOAGmYdRT+YBbXaO8yhaMbFKnBM28 dmOXpHtqgP/Jij2FpVeQFN50XB7Dns7Dwy6FcsNneLlXS13UDwX9zXlNdP0ckRZOKkaX 0A4ubMBIOXLRBkafyK8+Ee5TKDZHhl06f0eYdOmYrGpq+XkvVDs9CoPsGRcTf+ee4Oi1 IN/g== X-Forwarded-Encrypted: i=1; AJvYcCVqmM3IzNh1gQUEx4II89muh+vLrfZzY2RA8ic8Pc3PGHNVrrbeq6QBcop6xw8t/MmHlW7mDOSXhQ==@kvack.org X-Gm-Message-State: AOJu0YxhB6cW036nT3eRdi9a2biMnv/lurSEQx7Np9loQ7nIsO5C78Oh g3X5Dn7To3t6lEwHxKEPlSKOYdDJm4IPupshHthvpClxkAePiNUfqUXodcjGCEY= X-Google-Smtp-Source: AGHT+IFbq9QIJjJjQDHcirR6qnZcfNmFS1LzJegIAUAS6YpdNUb2o9k5do2wAMds8jeC3sH1edL3Vw== X-Received: by 2002:a05:6a00:22c6:b0:71d:fb83:62ef with SMTP id d2e1a72fcca58-71e1db88990mr606368b3a.14.1728427129165; Tue, 08 Oct 2024 15:38:49 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0ccc4b2sm6591270b3a.45.2024.10.08.15.38.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2024 15:38:48 -0700 (PDT) From: Deepak Gupta Date: Tue, 08 Oct 2024 15:37:04 -0700 Subject: [PATCH v6 22/33] riscv: signal: abstract header saving for setup_sigcontext MIME-Version: 1.0 Message-Id: <20241008-v5_user_cfi_series-v6-22-60d9fe073f37@rivosinc.com> References: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@rivosinc.com> In-Reply-To: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@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 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-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 4D4B4A000A X-Stat-Signature: 5ys8yh9attj4umz7adt8s8g8odio7d39 X-HE-Tag: 1728427130-804139 X-HE-Meta: U2FsdGVkX19SEDxRqLdjtajP1w6hJhwmHT2gUvnNGgJoS753gZXVINGWgEEIbhLqPfSC4NcFsYlJoI5xVGOmycBZB3jpEIQykbKiwB1q2CeIG0NFrlGyThzbsXfWCbqjD5ZZnmY/Raa1edmYO6RTZ2JEaBe10/IpA6Y9PpJHDVuaqvRPUsDe+je6V5CoMrWJPZ0XvtKTKIobkWiT8aD/VFC8HyRyylVQfSAhx6xIMPdKm8kLcBCMZJYTjsOPtz9Gjsa6x+RqLsdYpF972+wz4jTE+CYXoxa0WWd+k13PEMVeevr2EATBr7rEZwdCTxWeYw5cuPglCWijf+m1QmeDZONcNKi+pbz5ACTbU57kINxOypB4bdwYilZanEqxkKth28Ibqz6inWD2h9vIPpP5usEtawpYmA5xSzUH4zlEMUMqL3Vv757y+njVYZCagCN8xeZxLx4EqiJhcLmeQhRXPGzyjHvdAzxbQ25c0CWxKdKVX9f26AaYdbmT03aINTeSala//eK4ngdAUDtNyoI0m/GkEIvB6D9p0/Pnxm6gsZkKKPmAY9iMfeEIWQI1vkGNiQIWA34CDEC3AvB6Dmu4qqmXk81n2sB0UKDDHrz9UeuQj6KcYfQQUS7eUlpk8HMa2Z5qysb621Zc2A0QmswbWXXERvBWwdAiYzpe60JxtLuOvFf9pAhlXv19O8A9g5316m/QDGPhIH3Vb51cGhUhWbGk+VlUFaPYNpuUgxwtxNFRaHDiU4sM90OaSnr01YScQ7lH+JSh/i/qzsNznvWi4FnUQMGzsgc17K4Rp89iWEaYPXmKDo/5rRbIbQP5Htq79rvq59cUtOR7NunMHCegq7Kb51wman6O+r8YUZHrPlIOrNEFjmf9Z7QHFxAxSquxIHbZFGVuBM7q6dAxtZ9r4FfFJHuwPb8PedVeehj8vU3NWv08KA6ecFeiRpi2w9IozR0muphK5nLPIICtDaR 1uHGiieA 62hcwg/c8mqTsqTS2q3TLUPMP8QGocbANIRHP7uY2/KTf1jJdsy71W3abM6Rb5skTMYE1Munkuc69x5c4K+uQZwOGi3YO1pb4UtH1QQf+CsMnR4rESPPu2ACKfGGgmiVf+YbFBEZ9wF0YfGjOgRXT5uZk/hTIF7LbVZey1ZqpaKUpJ0fFwqwbCc5hKRv+MydlkVNUUW1a3Xoi8aHYGQ48zeP1ygp7MwEZJXBA+kxhxAh65qluzpGqrQJW2LmC+4Qg91Pw0bNSxgunV6DCBbnvnUeqcTDVxyVheaoFN0attq7gV9gnGUsOhP96J7bitq1XqGAeTsNefJE0Ky7UqfGlOGpmiRGTkuxva7+Ukjx94z1fdDNnF3TnmcWV4IUVYPh/qWEqCoJcBeG8bO68GTxMRNRHMLANaAW/HdVzClT64YhHWblFmS5iPwQP14+yIDazUIw3tmIb0fVdp5c4Sshyd3jpDKR2TxKXTaaYrz0qVPKea2hc1UJrnVtTGCRCWUo57Za0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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 | 60 ++++++++++++++++++++++++++--------------- 2 files changed, 42 insertions(+), 21 deletions(-) diff --git a/arch/riscv/include/asm/vector.h b/arch/riscv/include/asm/vector.h index be7d309cca8a..2d2ec6ca3abb 100644 --- a/arch/riscv/include/asm/vector.h +++ b/arch/riscv/include/asm/vector.h @@ -281,6 +281,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 dcd282419456..014ac1024b85 100644 --- a/arch/riscv/kernel/signal.c +++ b/arch/riscv/kernel/signal.c @@ -68,18 +68,18 @@ 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() && 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 +97,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 +138,19 @@ 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 +281,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 +290,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() && 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 Tue Oct 8 22:37:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13827106 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4E06BCF042C for ; Tue, 8 Oct 2024 22:39:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0647F6B00BB; Tue, 8 Oct 2024 18:38:56 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 015326B00BC; Tue, 8 Oct 2024 18:38:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D84FB6B00BD; Tue, 8 Oct 2024 18:38:55 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id A01E36B00BB for ; Tue, 8 Oct 2024 18:38:55 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 85C91C0C97 for ; Tue, 8 Oct 2024 22:38:53 +0000 (UTC) X-FDA: 82651901430.02.E30B11B Received: from mail-pf1-f172.google.com (mail-pf1-f172.google.com [209.85.210.172]) by imf11.hostedemail.com (Postfix) with ESMTP id 59D2940005 for ; Tue, 8 Oct 2024 22:38:53 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=b3ojVBP9; spf=pass (imf11.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.172 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728426997; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Cvf94GEIgwf2Foso+zLwniKx8edkk099I9p4mhmxmYo=; b=FMABqD7TxyCTtH370lIpbpp7bLKmzHE+f4pVrG54fQ/kqrWJFYKJGVWpH6kMFssZmZu8J0 7cKoGDdS2mbEndjV/+AmEZ2OQFUONHkVVar4VtH/mGrVeCorwFzTkExor1C3oFAnSWVNA5 KOAur0M0WfMWyb8odynPyBHUXK2dlt4= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728426997; a=rsa-sha256; cv=none; b=pS91ijtVBQU53fLrNLxPEr3o9ha6UCYLX54X6S+gk4K3ULH51U++sfBHBtZIu/TE7uCJvk AZ+ZJoMiZU+BAkQRCqMjtPgKIEqmdqA9nAWve0Y6xoIfC+FKbndEYB5rLd6LMFOOFRrt2R QUY9YCQ2GSczxDpfU+FwejDQAN5dm7s= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=b3ojVBP9; spf=pass (imf11.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.172 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pf1-f172.google.com with SMTP id d2e1a72fcca58-71e10ae746aso1244904b3a.2 for ; Tue, 08 Oct 2024 15:38:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1728427132; x=1729031932; darn=kvack.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=Cvf94GEIgwf2Foso+zLwniKx8edkk099I9p4mhmxmYo=; b=b3ojVBP95MsyxDFj2ppFInZ4O2mMxBpJP70LDb8C/u13QtYdZLTtscqgsZvWOmu3nm sdoQih6n2703Jp2rXdCF7cYCKNON6rS+MbryRaLsY4e5+CDbEcrUejtswOXcIYCtMWeF 9gg7hxtpELQQwRQo8mxEDNZKHVc1+zfXqHu4nBgh9qm2EY4OswkrEU28BUEkV4hEkeDS QkLZG1muqWbTsP4bfT17sGKcXKRgETgdM1g6ntVY2UlIEBIZ6qPA3EigIH9uO628lRxs 5YKJlhi6gUzaB7zWbTiCuCUW1QQ2Rp6wI+ed9lDiH6+a2NHZK6aKKlXKFE7xqgjsl7qI JLyQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728427132; x=1729031932; 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=Cvf94GEIgwf2Foso+zLwniKx8edkk099I9p4mhmxmYo=; b=YIjfSgobqie4/kcTEX0fss6LzFH+Wcdy8kOgrhy6ISLtpNOTYOIQ7kHdvP+lzymqms hRPBAuw3JS/U/TNNT5Iu+ItAJcRrTczhTOnOdKA/UCouMSTzVwf1wtWx3zrcH6XAwSWR rAzCsbxIlgdlpY0keuKOe0n16hWhsOXjR1SMf4F6fwmw8vMvD/YOrUbJM24GMqRoO/TK EthZtGw5VIdKMXj71jP1w0SEB5tBdubBX3S1/zkqhCTKAk5iArCUm9DsreH96CjoXX2x iyc0ubhVFqHNB2bczaAWsDYuoDeUpK1PbLCxI7L3f+XKmG7Xovtl3PX/VisXhh2jLMhk DnrA== X-Forwarded-Encrypted: i=1; AJvYcCUKniFAJ1k3rkugCfDDAeX+zV4UzUJnixbaDLSLAv/Xb4wbYNYkFXRlfwuSljxfQySLPracRCMuvw==@kvack.org X-Gm-Message-State: AOJu0Yy8vZ7ZNBOomEIvt+LnBKBGtciUGFKo35Yy3ws+Hs88BtXey7C+ Mn1+GY+dXyd2iutcXbazq70QoNR5nq82Ds0Igp3N2G1OE6Glkcb5pHoRvECvdlU= X-Google-Smtp-Source: AGHT+IFw849tD9l67Z3DZwekOsxo5/O1v6KLpQTn2rb/VuA09HTuP31YK0G061/MF4vLMxRUfv1e2A== X-Received: by 2002:a05:6a00:2d1c:b0:71e:4bf:3707 with SMTP id d2e1a72fcca58-71e1db858afmr575668b3a.14.1728427131994; Tue, 08 Oct 2024 15:38:51 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0ccc4b2sm6591270b3a.45.2024.10.08.15.38.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2024 15:38:51 -0700 (PDT) From: Deepak Gupta Date: Tue, 08 Oct 2024 15:37:05 -0700 Subject: [PATCH v6 23/33] riscv/signal: save and restore of shadow stack for signal MIME-Version: 1.0 Message-Id: <20241008-v5_user_cfi_series-v6-23-60d9fe073f37@rivosinc.com> References: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@rivosinc.com> In-Reply-To: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@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 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-Stat-Signature: 8tcinj171afhfspa3ktddyp998zycgm5 X-Rspamd-Queue-Id: 59D2940005 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1728427133-697893 X-HE-Meta: U2FsdGVkX1+zXX7AX1j6EutQCifKpngdlpcsqcHvUaVzJ6ecqLdgX3bsp4agW5DsX46SYUijXHY1Ctzw5do4pCMjo42mzydOjCE8oYXDUCENJJTqMHRS3/+IYf7RUvftEwKwz0LQ5EnPG/v4abBJSUgmaa0H2HxUWOUDvd7dMG4G35ecnP3z+9sZHYwfY4KnibCN4EoxfZG48dUwZlm9yYcpxfK6QiYcXProLBHuZjcURkCIj1cGPh7Wt7dbZLF88Loxm/HkUZVZd5vXNDEtaolLGnHSmARXeMP4GigEU75QZQzBb5JwwO8qLHP1TWFbS6AyOOAhIlnvIqF4Z0OBVBxu45bxHZJ6pQ1xWaUxaSw1BBMdQU6cam/atQMXrILCRWTiW2Yv66VBb+xV2B/C+ff9NMX8XzerbGUanU9GZavBmJIocne8lAIPmEmrlWMwwRLdddtrx7o0p2yXlgfWR0fbqxLvF4XWDr0aOJrzzQC7docrTdlk4nxNkFpEtDudPOl2k7r9nP6K/OfLhR6lEE+2K07/g0jDvA68GAmDx1Lp+l4OONrwWKgUlQ/nrNOGiC6tM1cugY8OrKRyk2/V4IftghtDPFb3IAGkGNg56Xga6MQrG4V/e+h6SiMrU+GbNjNHPwEbJr2BEqNAWTp0x6BI0eufE9kBGHC3RLQiSa4C7WJPZWCj9HdrA6S78a4spdloUlFlFiopZ2zQgNChdOZanim94lL+DKoc+zV6VRw/e7tNn6VDc2EDST+RLWrvCmDMDkuhi7mXo6WgVi/KDfpjJMXtL2LeOxIf69No+HgieJbjV9A6Y+FM84ICVvcKL5cl6WYRqasIKYlJtQT5+QYZJNTNnCllJiDUVWXSLmpdTcQ6f9uIk+fRUi9mkujrbaGwFg6EkvsXEF0oohLzQL7+WumDTHIHGXrISGz6cxtDo8DcqQiGilPbYRNGdWhpOXweWi30wseXMADnSwu gqt0He/c figchqtlLaUekeDe/7gLn5syrA8Z3IiBLscGFv1DYHFRv+aVRAiCWSHg6JEhbnILhg2Ig6yufkpxo3oQrU1Tq2qA+ws7rRmRiTXqsFfl9Q6r6AlIhRWTaf5lBJrBUcObZSX8uyVIQNUdUBnrvnvQFjPmH+pywhgOX75NzZ5mqqjbrOgGmQhzLIsvj/TT5Wf25MoxXbFtncBHiUuLwmxG5xLurJtBMljukp8NXTBfYhRgMe/I/Fv/9k/7XicYV/t4B6iBmr684Qe9MCYPQjlz0vZ+Suo8ImO5JQvyIYtgUeC94uCwXG70rlIxq3HbNo/0Nf3YmWzBKW76QkyTHaZMH0mMRrqSLOgz/5KRcYYgZR9UR+pKgc49GcSng+RkMCYtaBcUkV14sfWITgelB2Zf5kGIv7xQdqyavfTD44Rjm1NulItJrdf3JF5859ssAwpcZnyNXbvWZJ2g1amldziY28YjLYZqgi+EWTL72ZUcQ2rXkyXSUxFJuJ9vhCsplEl5/1Gk4AB4og4IrA+amlKKYjfhWyqO/Fs6y5tkYJhTHI5I95DbHpFgFSoowVb188VK+ezhwoy0T5QcmjLjiv6m7hJl+efAs2XUZF9Zgu2y3F7aip+dvz1knK8feaZSSWdHyMn3t X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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 | 57 +++++++++++++++++++++++ 5 files changed, 152 insertions(+) diff --git a/arch/riscv/include/asm/usercfi.h b/arch/riscv/include/asm/usercfi.h index 19ee8e7e23ee..fe58b13b5fa6 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 014ac1024b85..77cbc4a01e49 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 @@ -139,6 +141,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); @@ -149,6 +207,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); @@ -200,6 +262,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; } @@ -220,6 +288,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 @@ -363,6 +435,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 @@ -491,6 +568,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 21ea2237efcf..92d03eb76c03 100644 --- a/arch/riscv/kernel/usercfi.c +++ b/arch/riscv/kernel/usercfi.c @@ -52,6 +52,11 @@ void set_active_shstk(struct task_struct *task, unsigned long shstk_addr) task->thread_info.user_cfi_state.user_shdw_stk = shstk_addr; } +unsigned long get_active_shstk(struct task_struct *task) +{ + return task->thread_info.user_cfi_state.user_shdw_stk; +} + void set_shstk_status(struct task_struct *task, bool enable) { task->thread_info.user_cfi_state.ubcfi_en = enable ? 1 : 0; @@ -164,6 +169,58 @@ 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 Tue Oct 8 22:37:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13827107 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id B7AA5CF042A for ; Tue, 8 Oct 2024 22:39:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 45DAE6B00BD; Tue, 8 Oct 2024 18:38:58 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3E76E6B00BE; Tue, 8 Oct 2024 18:38:58 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2121A6B00BF; Tue, 8 Oct 2024 18:38:58 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id F123D6B00BD for ; Tue, 8 Oct 2024 18:38:57 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 54928140D3D for ; Tue, 8 Oct 2024 22:38:56 +0000 (UTC) X-FDA: 82651901514.11.819FA34 Received: from mail-pg1-f170.google.com (mail-pg1-f170.google.com [209.85.215.170]) by imf15.hostedemail.com (Postfix) with ESMTP id C9AB0A0019 for ; Tue, 8 Oct 2024 22:38:55 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=t+tIIaEc; spf=pass (imf15.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.170 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728427000; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=CDZ6InrT+eerMmZ4qSLMUgeWEoIog/HDXgAiPQQVAR8=; b=t64dpEVtlJ6v+T59O5eYcL9g+Plo6Xl2TudyIxCjDK2GT+lRK6QG3Uc2MA0hWzFdekeGvS dcG9ai6gqJ/QQUA0Z9OW9YsVCrWnWaPq3HtzPt0ptDB6oJvoK2ckQuxb2Vrkqi0Z4vO0fw wYGFHf0jvSmm21g1f5HjcZ/Nr64ApQk= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728427000; a=rsa-sha256; cv=none; b=GAEKeCniiEHtdLc0UiX2B+x6bT7grk4xwoDAYOKttqi9qJdRZvPT34MT5LEly3TvEWb3Jd BYW2YCooDTGmvv6P1VdwIuXhf18qsVmJLrX6viUG15eTHyOy8id1HsySxLnUC5MlBc+ffg rJmviY8aHxkqs4oRv6NRJAfixFGhZkM= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=t+tIIaEc; spf=pass (imf15.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.170 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pg1-f170.google.com with SMTP id 41be03b00d2f7-7ea0ae67df5so2167132a12.2 for ; Tue, 08 Oct 2024 15:38:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1728427135; x=1729031935; darn=kvack.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=CDZ6InrT+eerMmZ4qSLMUgeWEoIog/HDXgAiPQQVAR8=; b=t+tIIaEcEfztAKp7bu27rXpbuxoR40srOSjv0RFXTODy+qvoDuv6xLru1bDoruR61N PXjbUAPGVqCFschokCnDG7kpFLCUgTUPMrmDiop0WLPJnZ5VkYRnzp12c0ISUjQZqj4X fnC9a67vplz3jklsqndo9VQI/GS8AJdLlzNRxo56OPSzNLsmsRN94/LRiElT3JsQB0jd ezSL7mgXNR9mHUnNwtMTyE5We4gtk9aHbh6T5fJ18c2wHT5puxUQrPiZ4Cyr0cHQyJy8 LuyuRrfZsxxzAxbcOeJxM/xHumPQ8xi1/Rndaqd+pJW6D+KxaBYtaYdGxKRZKFu7k67S KnqA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728427135; x=1729031935; 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=CDZ6InrT+eerMmZ4qSLMUgeWEoIog/HDXgAiPQQVAR8=; b=l77hpBbB5F0Q0O3Rtk/eLbODNcSKqLaJB9touSWUSz78oQ4hUAWuFEcFiYNcjJu5TG 0naCiWj78SD3sgWBOWCz0yC1CuHtvdwNvW/XvBRcJlMPX/UT6MfvWvFEtUMwbfI77khW 7iknlUHgAm+MWtZMCgGM+xzBjvK9JqUWT59PZ9njHyrkIW0WHQHSHqsznwTEncAdNPwd lNr/QDjc3Dz3TJOevMWRzdlQPMsRrCi7++oB6PRwMEcUBAUFMfzZ2w/9VdSS3Dq/dqIW VDksvS5katQxR7e/HtVWFmSXlPBXA4xFOdy+h015KM3ZISm5uuT2p7JHAtqbJkOwpMRE mXKA== X-Forwarded-Encrypted: i=1; AJvYcCUzecUvb+R+ZWVcpZgZYxphhCnf0VK15JeTZgovrbf/LpB4rxmYDdcuvkGRWbOtYqkwi3+NTgVigQ==@kvack.org X-Gm-Message-State: AOJu0Yy8iMotrL2xF6K9jDLv9tKVv4JahOu8VYBvapplJsYDKF2+oZMT 9UNZCqHbL2oQBasNqZpvztqxwdEZM8Oan4A7hlFjcjK3qs09fkXsjtTerx3kq9Q= X-Google-Smtp-Source: AGHT+IFzhCfWL4XbvpqcLHDV0XpzTcXk+O5rvrW6YFnboaoUbbqBOGRh/OM2xOQqx+du7y1kOQQxZA== X-Received: by 2002:a05:6a21:78d:b0:1d2:eb9d:997d with SMTP id adf61e73a8af0-1d8a3bc3334mr715893637.7.1728427134747; Tue, 08 Oct 2024 15:38:54 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0ccc4b2sm6591270b3a.45.2024.10.08.15.38.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2024 15:38:54 -0700 (PDT) From: Deepak Gupta Date: Tue, 08 Oct 2024 15:37:06 -0700 Subject: [PATCH v6 24/33] riscv/kernel: update __show_regs to print shadow stack register MIME-Version: 1.0 Message-Id: <20241008-v5_user_cfi_series-v6-24-60d9fe073f37@rivosinc.com> References: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@rivosinc.com> In-Reply-To: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@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 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-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: C9AB0A0019 X-Stat-Signature: 5j3cqp7e13wpj6my8kg4qu9joakjhhqm X-HE-Tag: 1728427135-781300 X-HE-Meta: U2FsdGVkX19kJAnT2/vHm/pjhE5frbYAdWlLdY/wJS3h7mEQU1pam7ABU8ygvbWs4n7R3w5TiZfiFNGVVYuHOylQO9zseIhiO1etNrCBdXIMZSKwdKS+swcLg1pdivm+QyJsLxS+TDhAKqWR74Cmie+2PRhF7x0/c6jkig2xpbm22XyHB4Aqp56mO+FK2/S539xRB3IJX/BXYYrvQ8yZ6muUycr3Jf7C1t0eWdPemJToivFEkmkFp3Z7Ao8TgLBm92KjgC3LQxqH3p+3Hc5WgW8iGwg7+8rzPNi4shCL+UfSvSwy9a9R3rzCef7KVkrloBgPrtriZcMsCu6U0QZWCwPb8aQmGV1pkPMv/S4uOV2hTfT6VI0ZA4l0lWoFHIdqfuomgCW46I9J5KOY+N2Vcd9aefMTV8tehXx6bKgKd3w1jz+GBKIz/RR7SwlRRf7K79fMXHBA7al1/H08p0VPLvCMD1Rtgt3gwF23o6uN4BCMXWgKhjSHAka/jQ2CP8SEVwLoju2EZox0szuCBK2Q6DCXuI00l5ou+TctaY7dVDn0KFWNvD5bfINXPjgdLGbds8WUp2wqIrHL6/Sp6kVlP73Ijexw6CMi1uVuyZ0HkMerBQ43GH9RxmvnK/1x1GovNwt2q4ieKsZmCQJPjO81QSZpsyDdS/u04h3EPoY9JugIIR4qm8U103GcojrWYHWkGMSn4N3CdUSHQlfLakV+EoFN6JeDSYtSba0s6WTtMR7sLQ+GH9i3JAQcC22gUuHbRqMQm2ovN0Xzaw0g0lWLhPouHFGEMIWG5+pLlQV9Dtx9f4vyjItEAHmT6Vg2qKcS0EVPFw5oYkPfLovYDJzkTmnvewH3ZG6et/fGrHsKqwtiiRkiVBRZFVdNGVGpNvuOjwG3ityZuZMUdsExYmr9oXBFuqNhNC0FbZRGz2OF6MeUCasRIYWPrSYsoaU5Qd0uYX+/+Dpbrqx5f0it+aJ H0Jk1tKB AP75eMVQj94g6LMIAczytdiS7f42HbhYPMrFkjZuH0W/spKD3WXbwhIJZwfQZ5BZwMHbaMLDCBaeFrE3K0DsFqqUjnqgggOmcbDtATg8p1TV3ZA9xyHoTT813kbJCZ50xvTfn/J7JXqexOVLlB+Vmc8NZYhxKa33LqLd9MTEbjaJHRIMDP4bMbi0zTmN2Y3adoPagLj6Z5hKJSqE7yJGrdfnThCGogyPTE4ORcDQ7T2CK4O3+qCnkWjjUqnG5umw98a0a9zLolXqZkzGKDNGNVpSQAsyAfEx+Ry89Hm7600PKxahFu1XvP2gDJTg5Ud+wwk2R4kIUCj5LHL1X760L9WwjOUp0+I/P+g4xsfSNWXWtZU7TBc241M0GJGztjtoIBr8hYajA7JiYK/NCCSL1V/0NM5e+YJJv3kXMkM2Xp0DPGLUXG9D09lvpeMy5OvNyajBvlZSfb+DO4LRkG6JWAC0G35bTyTO6x8KCkETiDkVxMxDgOM7qrq2/9BCZuihL76VKwiOuco3yG+6UpCCsNFxrSfp2/5zKeO/sVER7LTwzA9JDCMEjwQr4qYzLWhrg8vFU2lX2t6SfAUlMQCeI6ABnKzevlH084cxftLdzNZgZxJMxIYFyQ9laWg== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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 5207f018415c..6db0fde3701e 100644 --- a/arch/riscv/kernel/process.c +++ b/arch/riscv/kernel/process.c @@ -89,8 +89,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 Tue Oct 8 22:37:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13827108 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2106FCF0429 for ; Tue, 8 Oct 2024 22:39:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 45FB86B00BF; Tue, 8 Oct 2024 18:39:01 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 40E686B00C0; Tue, 8 Oct 2024 18:39:01 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 24A0F6B00C1; Tue, 8 Oct 2024 18:39:01 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id E11D56B00BF for ; Tue, 8 Oct 2024 18:39:00 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 981D7A0CAE for ; Tue, 8 Oct 2024 22:38:57 +0000 (UTC) X-FDA: 82651901640.09.EC4CCDA Received: from mail-pg1-f177.google.com (mail-pg1-f177.google.com [209.85.215.177]) by imf09.hostedemail.com (Postfix) with ESMTP id BC645140004 for ; Tue, 8 Oct 2024 22:38:58 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="DbF/ciP8"; spf=pass (imf09.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.177 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728427070; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=WrqzwiSz6htfj/EZwiH/KuGtkUyrK9yji0Ttd1ipzTM=; b=8S2Y7DOxZtyx18SYFdwUxs3mRASYR7xi04h2TkCtynHC839gyJfxRiXg8MevaMs7Z2xbwA M7BaxmCxanQvYPbd7b0EwtnRntEFt8C/VxrFewxUWmExCgIhx6ctQQ4vjczf4waKEKOE/K scqhz5G0DXSHZVLiKGz/vVRbZu14/6Q= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="DbF/ciP8"; spf=pass (imf09.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.177 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728427070; a=rsa-sha256; cv=none; b=QbtDylo1MQnFJlveqCkYhLH5iNeNNI6AR6u5QVUYU8bnHPZDvxCQvHh3QUtZKuxFQ9pwxW hidSS67Ff3KUOW9tI+lyET3CH66Yi9YW7LU4GLmpEgWEgFrogEC0pfyEcpkHQ98OBdUivY d9j2dK8IJKjQL/6yxeCCGkbb3dRIAwM= Received: by mail-pg1-f177.google.com with SMTP id 41be03b00d2f7-7db637d1e4eso4559298a12.2 for ; Tue, 08 Oct 2024 15:38:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1728427137; x=1729031937; darn=kvack.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=WrqzwiSz6htfj/EZwiH/KuGtkUyrK9yji0Ttd1ipzTM=; b=DbF/ciP8MFYbhiOtquJgqUQrCOJPe6nOK2hkcyKLFjvMGt9zIg9l421VCtfKYxfHS7 GP7H3J1x6Oz7E8Npj5evMa1M/bkNVoWM/5pfW1hhZhsmGjkPMrk3cXzYu/Uz2DmIqPGh /l7Wc1OoqkUwOTaCL+M5zKrs3DfCesKmjd1kCGIP3Xh2Drer12G/Kx4y/SF4TiexFGVI bZmE94SAsrJiveRyLkmhaXjYqU02QyMf2RLAHZs+mJrLxNA6WrP8iE6Kd2P6TtH9fMKY ri3g0V8cJfaoZG/DZrAGHTFJDDhVBICJXLbRDc4he9CcmuTETSIsZuPa2tqEU8EKGgyb YyiQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728427137; x=1729031937; 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=WrqzwiSz6htfj/EZwiH/KuGtkUyrK9yji0Ttd1ipzTM=; b=vVh7Efb/TpbxlAQW+ph6QubgfAopt3KS6KTPhcm808HWQvjfXav6sem7sy3vVmPVoi vIRlCdO1gIz7aaXzKtTSleJR2sU1ESrsZ7ohdae3tNCwMLSAL+KUAryuHJP8+mGILKZj FOvYZ5RfiKrSt2x3XJlrX9Y2IUvEGbP+rLJjAdW8wRKy28aTWho0AaFbWnr5q9J2Z5pU /7pJvIo3u6ewK3J/nQkHTlLQktobPglL2krAKBw3zsXHDK5b3XQ9G6JTm/l5qs2MSmL2 nsKmGTapq1WV6DxjlkzesrQFiKUe5qyE4Ppjhlj4mSZpji1YGCtQxv/94d3YjzNML3Rk ZMhg== X-Forwarded-Encrypted: i=1; AJvYcCVHedzUPnmDoPq1MQB9adyraSjNNfL0H9MDWF1x3mEHDvVkAAjux6k9hnHcF6gB5VMvgI+AvIKIpw==@kvack.org X-Gm-Message-State: AOJu0YwfGuUOt77Rf/jOUQxPjQ6b1kZBbBDJTPh97UF1WJi6F3gVezOo rP9fPudQdUY9Co/b2sM5Tbo1D1eLdA3O+L9tKm/kzEBcQbFnXRWNeUhTjmUmFh8= X-Google-Smtp-Source: AGHT+IGQ05neSG2IuG81r7ADE35WiCZgz6o+dIVAqrO0BfikMLQrNlst67jW46BkHck9gikn8+b4wQ== X-Received: by 2002:a05:6a21:478b:b0:1cf:4da0:d95c with SMTP id adf61e73a8af0-1d8a3c2e5a6mr677088637.23.1728427137484; Tue, 08 Oct 2024 15:38:57 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0ccc4b2sm6591270b3a.45.2024.10.08.15.38.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2024 15:38:57 -0700 (PDT) From: Deepak Gupta Date: Tue, 08 Oct 2024 15:37:07 -0700 Subject: [PATCH v6 25/33] riscv/ptrace: riscv cfi status and state via ptrace and in core files MIME-Version: 1.0 Message-Id: <20241008-v5_user_cfi_series-v6-25-60d9fe073f37@rivosinc.com> References: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@rivosinc.com> In-Reply-To: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@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 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-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: BC645140004 X-Stat-Signature: 9qds6p494kyhry4ojkc93hosrippzbz9 X-HE-Tag: 1728427138-451829 X-HE-Meta: U2FsdGVkX199QqZjgIVuiRw0regWE7G8Th+FZ83OKpGsWlVw+7WSXYUrsN6lxFOhRMCP6cpJ/6w55DQ5GCjVDsiwnzvdxAz8zADUOW1Zb5j1+g8PZX5aj6jfMojEAaLVpfrBVgqmvVUpO0SY4FFLeVwpD6LVGH4EPc+UQEEV6dcUHAyH1EwIBh1qZz00fL5q1IrwdV+dYYojuS+2PEA4cwRRIUVLUAFFwQXSnbU7knpH3bDlOfcnLbdgQKoRsU3tdV3mCsi262WBKrPXEtlfi1iEuC/c/4yCR8F1s/EuRa2YnBN2k7Y1llC4QTPN/ItbuT56OWGMVxHW6tPlHpduSnAEoscQFldZaxbEx2SkHy50t97mhA9wFqySol5kgI8tambwet/uL9Csh8aJykCXLsDeWaiY3JFkLZ3MC/MDH9v7qzjoGFznwmXIZ7GwX4mToSRdf3RXgA0hqIAUaO5VWqLlbqzMHt6ZN+4ZxPDkGOWoKnf5T9drwROQdoLGHGY7GjzcgKNTTw0oy1MxtyoIZaO/XuU92tEF7zbfxvO5Q4OrrXnD8HG4mkcIeU7zHP7rwTvmfD8M2W2fyKohvNsXAzjKUMFZb8j3ijYtvHvOi9m2Yn6XSmMCrTa6p15cUty5/bnOsLLmd3YAbC6Uv5OkgE1Pj59zvfe7O9k7Ua1F3ulRaepLaLpUKptifyRKxf9/2jGwNwxq7S92zbqrLv4BF9gialC9XQfQpcHwdfYzO4V+DB+0HFuT6iGortJV1Ihq10RZZMCbs8p9vwbsQjmkQMfbQQhPplTMtMX64/5DwPOnmljg2Aw1/rS8WMQsNWDKhr3od1WJ5XUbqMm6nBSpPRk5mysMbu8Ja78DTGZia9yrvFO83NW8N4in2vAHnemvtZ/PSbNsmrvsfEPEgQwqjH5eceqyuQPQvdrxe+4hm/IRkj+8YWlhvrCmpoNpZcctXyOXKL63v4rik3R84Wo 8wAkoYNC rPk+XYENzCB7IuXwEhHCh8awSlcLIf2gO0hQwmmAMIzabjsrU0Q04fNLB70kJ3uFIlHDhXmXtIDMmzIEB9AQyzWcIzSYKL5eyB17XYVozGQq9n66Z3ZrIif7KUqTMSd8mugw+ifppiRFTKuSG2JwlqKFV5LBKk/kFQq4fuYXQmaZNg2KQMg8sy10hmswe5JAL9vsYu8WMmuo2R50XBDVmRcJyO+qyAH4Ah86fQueS9wwo0on3IPRUijhFsL0AgeLACh0F4dexY0vkckM+RumIoSw4aEgfjup6JPo+11rp6+icc525FDVV8CebsgMKyghB2Xl/SXZw9ncvUN4/dhHhoiO90aZjambrL0x57tJyblKjuIYgf8k1nkmsiNU/eqnW1KWGESmB23AvUX2RYD0IRfboDThD5vav8yQ9Q/DeHUCfU/Uh9I67XTn7LNgkOuL/lb1y2TyAWTWRVbLmXPw/8rvE6xTXkyUzCs4hHm3tHl7a3r10ETUZXejyLF4NFvdRoEzCUedmDvUrX70vf4jQCRvWpByynKr9fw66fm9PpVoqNSNh3/gOtr9RqphD+Vf0S+V16Trmc2Cnbdc= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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 92731ff8c79a..c69b20ea6e79 100644 --- a/arch/riscv/kernel/ptrace.c +++ b/arch/riscv/kernel/ptrace.c @@ -19,6 +19,7 @@ #include #include #include +#include enum riscv_regset { REGSET_X, @@ -28,6 +29,9 @@ enum riscv_regset { #ifdef CONFIG_RISCV_ISA_V REGSET_V, #endif +#ifdef CONFIG_RISCV_USER_CFI + REGSET_CFI, +#endif }; static int riscv_gpr_get(struct task_struct *target, @@ -152,6 +156,75 @@ static int riscv_vr_set(struct task_struct *target, } #endif +#ifdef CONFIG_RISCV_USER_CFI +static int riscv_cfi_get(struct task_struct *target, + const struct user_regset *regset, + struct membuf to) +{ + struct user_cfi_state user_cfi; + struct pt_regs *regs; + + regs = task_pt_regs(target); + + user_cfi.cfi_status.lp_en = is_indir_lp_enabled(target); + user_cfi.cfi_status.lp_lock = is_indir_lp_locked(target); + user_cfi.cfi_status.elp_state = (regs->status & SR_ELP); + + user_cfi.cfi_status.shstk_en = is_shstk_enabled(target); + user_cfi.cfi_status.shstk_lock = is_shstk_locked(target); + user_cfi.shstk_ptr = get_active_shstk(target); + + return membuf_write(&to, &user_cfi, sizeof(user_cfi)); +} + +/* + * Does it make sense to allowing enable / disable of cfi via ptrace? + * Not allowing enable / disable / locking control via ptrace for now. + * Setting shadow stack pointer is allowed. GDB might use it to unwind or + * some other fixup. Similarly gdb might want to suppress elp and may want + * to reset elp state. + */ +static int riscv_cfi_set(struct task_struct *target, + const struct user_regset *regset, + unsigned int pos, unsigned int count, + const void *kbuf, const void __user *ubuf) +{ + int ret; + struct user_cfi_state user_cfi; + struct pt_regs *regs; + + regs = task_pt_regs(target); + + ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &user_cfi, 0, -1); + if (ret) + return ret; + + /* + * Not allowing enabling or locking shadow stack or landing pad + * There is no disabling of shadow stack or landing pad via ptrace + * rsvd field should be set to zero so that if those fields are needed in future + */ + if (user_cfi.cfi_status.lp_en || user_cfi.cfi_status.lp_lock || + user_cfi.cfi_status.shstk_en || user_cfi.cfi_status.shstk_lock || + !user_cfi.cfi_status.rsvd) + return -EINVAL; + + /* If lpad is enabled on target and ptrace requests to set / clear elp, do that */ + if (is_indir_lp_enabled(target)) { + if (user_cfi.cfi_status.elp_state) /* set elp state */ + regs->status |= SR_ELP; + else + regs->status &= ~SR_ELP; /* clear elp state */ + } + + /* If shadow stack enabled on target, set new shadow stack pointer */ + if (is_shstk_enabled(target)) + set_active_shstk(target, user_cfi.shstk_ptr); + + return 0; +} +#endif + static const struct user_regset riscv_user_regset[] = { [REGSET_X] = { .core_note_type = NT_PRSTATUS, @@ -182,6 +255,16 @@ static const struct user_regset riscv_user_regset[] = { .set = riscv_vr_set, }, #endif +#ifdef CONFIG_RISCV_USER_CFI + [REGSET_CFI] = { + .core_note_type = NT_RISCV_USER_CFI, + .align = sizeof(__u64), + .n = sizeof(struct user_cfi_state) / sizeof(__u64), + .size = sizeof(__u64), + .regset_get = riscv_cfi_get, + .set = riscv_cfi_set, + } +#endif }; static const struct user_regset_view riscv_user_native_view = { diff --git a/include/uapi/linux/elf.h b/include/uapi/linux/elf.h index b9935988da5c..7ef63b2b67a1 100644 --- a/include/uapi/linux/elf.h +++ b/include/uapi/linux/elf.h @@ -450,6 +450,7 @@ typedef struct elf64_shdr { #define NT_MIPS_MSA 0x802 /* MIPS SIMD registers */ #define NT_RISCV_CSR 0x900 /* RISC-V Control and Status Registers */ #define NT_RISCV_VECTOR 0x901 /* RISC-V vector registers */ +#define NT_RISCV_USER_CFI 0x902 /* RISC-V shadow stack state */ #define NT_LOONGARCH_CPUCFG 0xa00 /* LoongArch CPU config registers */ #define NT_LOONGARCH_CSR 0xa01 /* LoongArch control and status registers */ #define NT_LOONGARCH_LSX 0xa02 /* LoongArch Loongson SIMD Extension registers */ From patchwork Tue Oct 8 22:37:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13827109 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4DF75CF042A for ; Tue, 8 Oct 2024 22:39:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E20D56B00C1; Tue, 8 Oct 2024 18:39:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DD0776B00C2; Tue, 8 Oct 2024 18:39:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C23966B00C3; Tue, 8 Oct 2024 18:39:03 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 99E756B00C1 for ; Tue, 8 Oct 2024 18:39:03 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 7773CC0BAE for ; Tue, 8 Oct 2024 22:39:01 +0000 (UTC) X-FDA: 82651901766.19.E354680 Received: from mail-pf1-f173.google.com (mail-pf1-f173.google.com [209.85.210.173]) by imf11.hostedemail.com (Postfix) with ESMTP id 5402440003 for ; Tue, 8 Oct 2024 22:39:01 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=IjXxePJ1; dmarc=none; spf=pass (imf11.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.173 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728427098; a=rsa-sha256; cv=none; b=hJu3Pnat+3zBwtX+4kFC+BzVDvEf3dWrHOjSx3dWVkaIz0Wva31DnQnUvMs+6UqQh/Lxsz QKT6u/mi5FdZYi2hwHjYcp5qMxwSdbSa4ZkZ6JUXgVQ0KI16GCmKHCQTng4+/UgNVilzUQ qpucZbFeZEeIubI58Tc+2cEdeos6UAI= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=IjXxePJ1; dmarc=none; spf=pass (imf11.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.173 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728427098; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=IjvUiWFDgNIgR9D1bPvnv0vD2BBeNN4jEZqEwYO5LXU=; b=F8IOzm7SX9ZHPmi5Mi42wNQnpX4TBvf0LALKldTlRYtfQL8h+xdrjNsXFmq00/9QljZRt0 oAFkMKPQ3wlv4dM77JVS7IYt2Xce/XWqf/x6qY5xUKWFU5hqUjuciqPnfHGy/ddGZdbl9w BQhNLPLG5cgtScofl6pP6s35cSGAq+0= Received: by mail-pf1-f173.google.com with SMTP id d2e1a72fcca58-71e1a715c72so554926b3a.1 for ; Tue, 08 Oct 2024 15:39:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1728427140; x=1729031940; darn=kvack.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=IjvUiWFDgNIgR9D1bPvnv0vD2BBeNN4jEZqEwYO5LXU=; b=IjXxePJ1TQ9eIm/SeuOYsOeV1yLGOaxWIr/nX3kSj4aUJaDW8Eh9Cg0XWLDZKIq0PR 4UW8z3P42HSaWU/93FB00bVyvTHxd7Y7uazxde+P942LaEHE0cRMFnnAAyos0G08FecZ nexk8KfYNid/scB8cvjX3awKBCuPo+1/TuajEpseS7co14o2BZvO10NkGBQyDkgwuQbF zJHqhCywsfddiBs15AF2pjtMZQX9ASTe3g6Th6HbIR8harcXIBL0D3kByt25iWDJ6ckJ RjbLoQSagdu6UbQXAKA54V55nxvKoi6jqcJqQ7aheT63XNbwlcysP7cPxK43MgbBdDhF 2tog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728427140; x=1729031940; 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=IjvUiWFDgNIgR9D1bPvnv0vD2BBeNN4jEZqEwYO5LXU=; b=bMxVv4Cxe5Zsmw65A8c34ejHMh2ape2HLIj3RPYU7aYJcTUWXL6SyE082qZ98cpk+3 FUuljeY7OA+BSDhacpkoBtPUzGFkG91S2g171aTnEac5+Av/BeEFRsCnZ2G6A4osLOoj wsKcZFAy8ghSjMkECeTa5ZrQrQy79vZtPl0OQalbJtGZSVloKfCVyqqCkS7pqXan9oL4 w2Dsko+OGCGKLYP1TKLBD4HUh6pJlhIW7Byq2q5i9z6YPTvBfdh0kyLtRT640nenUbzG Ijcw7Q/YXSef9DImc2F7mLTut0/Ls9TEYXRPnNv2LTZu88x6VOsfpSiqKLk81tRlP6tJ Vy/g== X-Forwarded-Encrypted: i=1; AJvYcCUHCo8Q24Y37fN7FI7007/e1aqmaD8hRL2phFwLwsOj+Gyuzb+4QoBUPRwChWwC0ny4BYbB4AmheQ==@kvack.org X-Gm-Message-State: AOJu0Yyq517SfLv4CyoYoxVSTdf/flAUTcHD9HieaS4zNZCnd2t0Xb4z cLr7uZ4qriCRrKSW3VvFLdSGGe4v7Se6Ooq6p7slHpz1Ur3xo0gcl5fGFkx8huI= X-Google-Smtp-Source: AGHT+IHmwPn5jM9quB16FyWf+V+eEat1fojl2g1P7nEi3JtER4PWJx1A+GiQhOrwkldgMrai14wFrA== X-Received: by 2002:a05:6a00:1495:b0:71d:f510:b791 with SMTP id d2e1a72fcca58-71e1db7734amr622915b3a.12.1728427140255; Tue, 08 Oct 2024 15:39:00 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0ccc4b2sm6591270b3a.45.2024.10.08.15.38.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2024 15:38:59 -0700 (PDT) From: Deepak Gupta Date: Tue, 08 Oct 2024 15:37:08 -0700 Subject: [PATCH v6 26/33] riscv/hwprobe: zicfilp / zicfiss enumeration in hwprobe MIME-Version: 1.0 Message-Id: <20241008-v5_user_cfi_series-v6-26-60d9fe073f37@rivosinc.com> References: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@rivosinc.com> In-Reply-To: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@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 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-Rspam-User: X-Rspamd-Queue-Id: 5402440003 X-Rspamd-Server: rspam01 X-Stat-Signature: sfkt96dsfaqn3e3itntqygzc8dgb744g X-HE-Tag: 1728427141-838731 X-HE-Meta: U2FsdGVkX18tOAMCFNqlSlR3F9lsPsoKJEg3xtapzuN7CSw8hZ3SRrpUvKSIcxGoIt/ox+pJi2IfrUrsQrZ6N0smnDzFVBqV6Ix0qP8Pq2aLSgoZO9CkI+ZBKTgVJgsmUe42ojemxfT5jmPGrEApoqxV35vLh3hKVZ9Ei7KS+MoGP9/MeDraDrkL5FgdL61s6OwOFJHmXw9puDtwSWVijMHSzrp3D/6Eq58gP9F6eo4QSoRLm0ShEmN8VUno8wUvXBLC/Jcc/5hXmiaxDSNsY0jOgc+ReqMHcD6n14eFNhGV0sGk2lPke0okxmY8ZZhcJitSpZil59Ant9JNyvN7X+7HQ+OaufWOBYOdTq7u6YQo2tW4TAZnZ/2iGGdTeb/z5GWckzmyv3BdnQomQq1GUu6bJGrPn6i+UrqfQB1zxzGqrftxjpiCV3tWZKqZyiauIk7UaBIJY9wWhiDwKZSxxjAJ4ZR1BK46hVK3Wl2V9gbxXbAI0JZxI4BOcYxBOYCOdMeB8ncE3lVoEJDcvLntcLvFAqsE5SMOdeGiN8PU3k3XtiaLTcjIDYviYgPdIB8vYRrj+1NfshrN3/areIvxm/ERShTlN5b4A/zRQUa5bufR8mL5ZkRrOk3eC9M/8zYSaEq+wTaitl7qiHAnrzwmxW36hIcZkwcEVe/3bu7cVxpiI76ujMW/ytzgSkenJLvpMjBZG+REmbyue386UwZgKFbaB8tLGAPvegyRwpy/ws+0Tcn7iYDk125VqLp0Vmb/AdWUPy5Y6G+rjFZ8Fw+C1lJzLhCAojU4XQ+Zgg6Vjvo4m8sp3EgyHfleh3ArSxlu1apbczrTAngLVuQThBpRpOWc5wdNMkdptPs8OPLf92VuIpcO2H0V8ujfWUPPgigf2RUe8wlDJJvAxrXNfHbqbsAbghbyNa6jnEO6FAjROI85oJaz3LeEh1Q5+tLXQg/z6QW9MXvZMHMx9JvMLGf Ut5nr5hO 46xI62vw7+zrCBOEksjRI9oaHivy0zBru8iSvb41rVyghvW47gBMiYzBuM5ai07DFauMKq8DyBo1jNcCRUZKwND9+Pevom99XKDX9c3duj/VLCc71iYiyr0dFDlJTeqL1UP0RoDZMRMusPEW6qOCWsM5krkHuHgS9mj/Znwxk84UVhzLSLgmg2HiJfaPZp5HY7G1rA4+g7skqlnmmFCfxObLlyYfG0h4lZKRE3G7TD6LJLvTf/ojpmgLJr+XvvM/pd1nT3C3e9aLysRx4kybct8nKA1Cqc53iTlQ08oSwNxKrdo9HdQbDrEgX8oHhmkvqS8Y2qkTakF5mjX8subLHECfZ75q8euu5y/IIbj8gQAJ0AGjrPLdfaqMEXZe5VtJVZgPiQnf4ZlI+pm0L4JMlkOxTLfuM1iKLukWtfqTsVexJLdeLfiNcJ+kCucZslUMKU+xbWKz5KY5SCYB/yQ0enBzcEFPOjXHmA5k6/SAAWp3CdtVQPooddHV8ST0+CT9VFqKh6nqT5HLP5aZZHrZbA3poIUVFVSNdjzpGwGPamCTZ9Ske4bjcErqIGNNUerwSsvrh X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Adding enumeration of zicfilp and zicfiss extensions in hwprobe syscall. Signed-off-by: Deepak Gupta --- arch/riscv/include/uapi/asm/hwprobe.h | 2 ++ arch/riscv/kernel/sys_hwprobe.c | 2 ++ 2 files changed, 4 insertions(+) diff --git a/arch/riscv/include/uapi/asm/hwprobe.h b/arch/riscv/include/uapi/asm/hwprobe.h index 1e153cda57db..d5c5dec9ae6c 100644 --- a/arch/riscv/include/uapi/asm/hwprobe.h +++ b/arch/riscv/include/uapi/asm/hwprobe.h @@ -72,6 +72,8 @@ struct riscv_hwprobe { #define RISCV_HWPROBE_EXT_ZCF (1ULL << 46) #define RISCV_HWPROBE_EXT_ZCMOP (1ULL << 47) #define RISCV_HWPROBE_EXT_ZAWRS (1ULL << 48) +#define RISCV_HWPROBE_EXT_ZICFILP (1ULL << 49) +#define RISCV_HWPROBE_EXT_ZICFISS (1ULL << 50) #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 cea0ca2bf2a2..98f72ad7124f 100644 --- a/arch/riscv/kernel/sys_hwprobe.c +++ b/arch/riscv/kernel/sys_hwprobe.c @@ -107,6 +107,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 Tue Oct 8 22:37:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13827110 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 97021CF0429 for ; Tue, 8 Oct 2024 22:39:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A51D06B007B; Tue, 8 Oct 2024 18:39:06 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9FFEE6B00C3; Tue, 8 Oct 2024 18:39:06 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7DD6B6B00C4; Tue, 8 Oct 2024 18:39:06 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 584FB6B007B for ; Tue, 8 Oct 2024 18:39:06 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 9079240B79 for ; Tue, 8 Oct 2024 22:39:04 +0000 (UTC) X-FDA: 82651901892.20.AB9D519 Received: from mail-pg1-f177.google.com (mail-pg1-f177.google.com [209.85.215.177]) by imf02.hostedemail.com (Postfix) with ESMTP id 2C1FE80012 for ; Tue, 8 Oct 2024 22:39:03 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=yQnXuCJp; spf=pass (imf02.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.177 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728427075; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=hq3DaQ4YpzTwJN9yZW6Sl3gPyL1m035+b/05XFtk9T8=; b=2feEsuCNPQZgSTScvdClESnZOO5US25XdhOkV8rtmiRvpg5RlQWj/Rg6+r2q8S2+2t7sEU Kg6QssK9dO5Aum9X5zWQFOrrRzKa7hbxT65uttaNWQvPQQyIRDqPNEJYUFgISJoGmJFHhA Fvr58SGxzxgILwApotUoUwvmdJbfpF0= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=yQnXuCJp; spf=pass (imf02.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.177 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728427075; a=rsa-sha256; cv=none; b=8oEaohfs0ErsHLRJI8C4xp25vbIlX8PK4riWMduuUgNYLIweOa89N6OArKnHJz5A81wQR2 nhetspkIXrx3MXt10rGe0+tJ8qOJhz/xIfuAeNGJfQWXxZxnOVO9y9Ow0btFe10g/LwjLP WfSjlCCo22VB5UBkr2IfF0b8GnKaL4o= Received: by mail-pg1-f177.google.com with SMTP id 41be03b00d2f7-7ea12e0dc7aso1717892a12.3 for ; Tue, 08 Oct 2024 15:39:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1728427143; x=1729031943; darn=kvack.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=hq3DaQ4YpzTwJN9yZW6Sl3gPyL1m035+b/05XFtk9T8=; b=yQnXuCJpS6i0IA2jBRF0tj22jBw/OfkzcYaRnhXL2Iv3PSW4adZ1fjwZxu0YFf1THu cHjhhO24B6IlQvGIqVXw4s0cp3xwB9yzbVOPvaKqcOFFp5AaphwpQtRI43zYc4/hzo+o Wbg51D2qDJ0TNkgiE5jCXmbmG5YRWWY+CRNG8zppCq+VIJ/fw4n+DHy0nC/FlozW9sgj 3T2R83APwFNbFVO/J19wzN4FzVJFJraLkRrKpc704NnyTytQiSYhbJr0fNE7YybNBXnF D1eSOPY2XBs/XEtl04M0FQK0rsH0hAw92ePwPIeV8gdgR5exdFVbcF/8hIIz/OnrySVw PoZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728427143; x=1729031943; 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=hq3DaQ4YpzTwJN9yZW6Sl3gPyL1m035+b/05XFtk9T8=; b=NosLMVA0916lQxrxkM/kg0tiS8RcvZSSkM+IvEkZAkUglHlcVyjwy+Q4e0S4EYyYZ6 H85BUyyof7haC64PgIwFsDVxNZfsGtN1szs8zImShGcmZaACaagCrlZ5ilGPhGRf6cNn erisDxVrLfv5YZbUUOtgtjKLatA81oFOm7kcd5aAlaVcYe+bnIJPbEzIRw3gzgfxExiP u9N9t0iZT9C6iBHXWlBH7gSPgblS2loMl83lZo2X7j1pEz22bIVnB7hthK2qga86x3Jn lTtkP69RdhMy2nntKyx6YkJXKi2T5za2C6qbyfIR3wej5p6AAhobadHyhOjExtBUFJGU gMaw== X-Forwarded-Encrypted: i=1; AJvYcCWbFxUjsq9M3evG/cnrUKA5YkvLdJx5fQhKkXIdVLYmrW2Fz3chb5dHXqvy/nu3FCEflsugmQjJXA==@kvack.org X-Gm-Message-State: AOJu0YwCiWiS14aF96S0xzxa1Nb94d28Xtiu7+N+TPSfr4VcB+rF38Fl 4uRukz4YEgVXToFPchyUEJ1/4u8IdLs2bOr8WGmHryGN2iCQF3MAhZl5vLHEzWY= X-Google-Smtp-Source: AGHT+IHPfRT151eXbUVtRylXwb5fgy4YgXEg3lvahlH6Tqn3Wx4wYmo7An3u+lQTNTEkhVfnPF9JZw== X-Received: by 2002:a05:6a21:3511:b0:1cf:3c60:b8dc with SMTP id adf61e73a8af0-1d8a3c4b957mr670691637.34.1728427142989; Tue, 08 Oct 2024 15:39:02 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0ccc4b2sm6591270b3a.45.2024.10.08.15.39.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2024 15:39:02 -0700 (PDT) From: Deepak Gupta Date: Tue, 08 Oct 2024 15:37:09 -0700 Subject: [PATCH v6 27/33] riscv: Add Firmware Feature SBI extensions definitions MIME-Version: 1.0 Message-Id: <20241008-v5_user_cfi_series-v6-27-60d9fe073f37@rivosinc.com> References: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@rivosinc.com> In-Reply-To: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@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 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-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 2C1FE80012 X-Stat-Signature: ndy6rjd96qtqyrrsfqexnpwfgnghc3qz X-HE-Tag: 1728427143-367463 X-HE-Meta: U2FsdGVkX18weJLQ9HGBZWqKtFD8QurtVU/vTqK7AiFzE8xexNKh43wlC5d7NfCypD6uoGQgSVxQktQnoujTf2Pdj84gaeNs3KORrZM/Dh1d4142ZZBuOoeaOFwl/dgV2SM3lqTSc8KqPmoouRQeGvlDUBIpCnJw8PC+xEHICf2B6DlGFD3zNbpVuPdS83UDKnQHq1+X7NqLzgBCjRl2rGsFdNBQBJMAow/38++zzPzInzEKMAdiE1iJ71OhUjKlqeQsKw9c002DccDNtEiTLgnLyy5O1DjUtFlGq6up4n02SdeCZlO+Ujw0H6YkkZRY5kFDwAnBIiCSk1w6LKQBeOESZMwjWFjq8/mIc1M01Njiu4NxSRlHm8oo0bp5CnoQuBRmQo5sSVGg2VDGp1NIpmVblfcn8pnuDm+eLP5ixDMVnReREBIVmt3mqJNJ/Fsmhe2yjspxnZa5sMNRj1eOJft8Nt6yFu0tLMsvpKkVygS2KeZ/9oqPzfqJ3WQLE3ADorSMCAO64elLuSwUAlY3qgn6XiNaJpdEU8lHac5fJxWvBOM4OrhvSCvOkYP3A7qOIgCZpmuyzpezC1Q91R6VaWphRaqdcIctzATzHXhGFm1IcNBkdrs+XxtQklpc7dMNjAid7hsEmOiURRSrGuFk1z8+HATh6QuM5JSL2car1Zw0o9G0BGsDbjLkmuJmvAEykwZZR5B8/JOSrjmxSCLXpFjS6YHfAPgtrubyYfB/qQpXkzrcQyDYWEamXQAoQBG0LomH2S3uBdGf4FBxIdmC12eQVgq39rJM+Y6h7WihSFcJCkuKNdfN6jJj3TRue3jOmIERZE2iYrKUwuZOuWRCezOX1a5FNE/Xx0Zgm1To3H5tPf91+vMBKrRcKOF/YTqXzRZabjk8QvTb5b4hl9i6yz5sCBV4EsRqMU7AgiPB9YFW+iEUUTfVABV9WpWS86xrNMsLPnYgqAzelP9GGO2 KtETZstJ 5OOOR/GCdl4L7S2QCnsVPKF+1gpM9QpGguHWNBhXpNvVN9xKk1ASeh2RRxnVGXDUhD24wfYcD416hr7k7bf3spAQsKnR2ceqdtDftdjtkFtPc+JvKizMEOerx8D/X2kL6IUzPBbxuE8S1M24EfOxwjzQWrlgSFJbojI3/Qt4jvcV1CL/zEouKFI5OFVWDQgfpPuivSHxYSkntzJoX48o4JWDOy/jz8l/IdAK6Tv3FLYEygGkMCY8L8zzOEOnHACr9OodJeCEIFOAFb5Fy+sRkinnwnpJSJHEA8OVvYsaIgVKNIBF1pRrgdVcs7Yl0HysulReDRWhCtp0ycmvxn2GmKMNX9S8lj2RvaoBGbDbAdw/2JOaN8CNHdJmb4A6g8XTI02EATcepie+pP5eobg24IGpYlyi+0nq9HOHyjGiiJJGCgublrvWW5OzJCa+H7pGMgwPYyvmQUnWDfkENRDQu6fSOZe0K+7tPzaMemiJQZOO5vT1ZcZvdpZFhAbffo/T8TfBObotcZNVaOgjHhCOEOS0dtB8P9yDeomjTe54z8tkyKjLMAf4dQqrBxPY0mQyrAtmipyPGC1qp/Wb+a0rUWeo3E4Zx9cZa/Lj3 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Clément Léger Add necessary SBI definitions to use the FWFT extension. Signed-off-by: Clément Léger --- arch/riscv/include/asm/sbi.h | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/arch/riscv/include/asm/sbi.h b/arch/riscv/include/asm/sbi.h index 98f631b051db..754e5cdabf46 100644 --- a/arch/riscv/include/asm/sbi.h +++ b/arch/riscv/include/asm/sbi.h @@ -34,6 +34,7 @@ enum sbi_ext_id { SBI_EXT_PMU = 0x504D55, SBI_EXT_DBCN = 0x4442434E, SBI_EXT_STA = 0x535441, + SBI_EXT_FWFT = 0x46574654, /* Experimentals extensions must lie within this range */ SBI_EXT_EXPERIMENTAL_START = 0x08000000, @@ -281,6 +282,32 @@ struct sbi_sta_struct { #define SBI_SHMEM_DISABLE -1 +/* 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 #define SBI_SPEC_VERSION_MAJOR_SHIFT 24 From patchwork Tue Oct 8 22:37:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13827111 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id DB6C2CF042A for ; Tue, 8 Oct 2024 22:39:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9B6406B00C6; Tue, 8 Oct 2024 18:39:09 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 963326B00C8; Tue, 8 Oct 2024 18:39:09 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 78ED46B00C7; Tue, 8 Oct 2024 18:39:09 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 590B86B00C5 for ; Tue, 8 Oct 2024 18:39:09 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 46AD5C0ACB for ; Tue, 8 Oct 2024 22:39:07 +0000 (UTC) X-FDA: 82651902018.04.E96D832 Received: from mail-pf1-f180.google.com (mail-pf1-f180.google.com [209.85.210.180]) by imf26.hostedemail.com (Postfix) with ESMTP id 298D2140011 for ; Tue, 8 Oct 2024 22:39:06 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=xY+2Rjbh; spf=pass (imf26.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.180 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728427103; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=COCNpmNzP6WC0TkslCp4YYWczFrCYYnCulBqOF+s4dc=; b=pCjvMBb1PFqYIycB4lTMM+R+qlsroeQQc33vp6xzTOTvegeuF1IZSUYG9aqSM057OkMdAM 65XWOLmBbGlOMLckjuYkWWDj2PEkYv2ne84ESMTpWT8fmiaXF+aKA1NTEVxJ8cYLqZVjK1 QlA4iW5RdeGdMu/RZM82x1XxYHDKhGo= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=xY+2Rjbh; spf=pass (imf26.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.180 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728427103; a=rsa-sha256; cv=none; b=U+maj5xKcRMS8/sASN+KWtv681r5yAOQhdqPEyXuxd+3jvwph9+tXapweEor6h6IOoUkJn v9QcgkJ+8nSuBBnkVigJ8oofYBCrjtvN+Q443aoFDrE/vQUFvKXG9f+ceXhQUqDtUuLuLr GXct0rPAgCKn1/0BwprWMsYpoaumt2k= Received: by mail-pf1-f180.google.com with SMTP id d2e1a72fcca58-71de9e1f374so3229910b3a.1 for ; Tue, 08 Oct 2024 15:39:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1728427146; x=1729031946; darn=kvack.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=COCNpmNzP6WC0TkslCp4YYWczFrCYYnCulBqOF+s4dc=; b=xY+2Rjbh5ExBS37jyFdvVoTaKOIjnq4WXDuMjN5HZFOfAuFiD9aR79RDAaqfO/D5YQ K0cevCiX+ubRhgTpji2BP37KgNyvOd2h4LebkBXdOUYAjGzmVAFux7zd8YQ1dPFOv4bk lWlfD/04GCrZrlCRycl7EYOVoV/WsMq296p5vejp3Fo0BjaBMLA2945dQvDUPQ+BVlQD PJU+gK3HkjUMBYgTVJ10ZmFAKa93P3VfIwS5nnDzo91eGFhLk49jnvamfEX67oa7LtI2 7wYcRYEX8a4jDN98u27dTVdY2CXiLgOMoDlyc22Ld//t5SKC+j97h+YouIf+Aftxz3kx 0g6w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728427146; x=1729031946; 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=COCNpmNzP6WC0TkslCp4YYWczFrCYYnCulBqOF+s4dc=; b=BpFjso8HSgTrmvZT8Dnk15H4JVuTJVf3Uj2pJKHjuPuhjMaoa6h2Szk1WPMNX/bXIW xanJJRYklrSY5wUaAwvh+iSsYPVdVBrnwNRUrRYEh2/KxvUIpVKYhLAFNUH7UnjYfQKl Tz44cuxmiPWkbq5WGQVXFoQTtBwPRkfz0N7N3cnbuSq+/CTZJj5K3w+kGDKGDBUvod65 gaugQNr0ZDDnVVmKTjAsINm7cQg9MBgDwZ1OtjK4gkQM7ULaOQb9L9FQocgLv656vqg6 hCR/8Mj681NWCh53BVucbBzPefTawjsjXC7heX7ymUTOWv+RNy4t5LLKlSVGJJ7gEwX/ F/lw== X-Forwarded-Encrypted: i=1; AJvYcCUCigBQ9FCfsrbMai+1KcaoaKFyXNdHtOhFviW9Bo740H1lgruNtBtPExiqPLIExMmU/e4A7amL8A==@kvack.org X-Gm-Message-State: AOJu0YyXUbppP8lekejLpqApNPfv1pMacu2QGpQ+GNMtHR2kuoYXv7VG l3uoFrHwHncgOpGtpGCRBqQwU5gG7Ybu/m9mSXLiY+lVayI0+uocammE175d+Z4= X-Google-Smtp-Source: AGHT+IG6yIXYzR9JD2NjDYdt+nbX1nqBZC/ZCSHomZxkbURMdjlHvJ/tAFXpjz5XbsMyqRUISAOAUw== X-Received: by 2002:a05:6a00:1487:b0:71e:49b:59c9 with SMTP id d2e1a72fcca58-71e1dbc7550mr621721b3a.24.1728427145912; Tue, 08 Oct 2024 15:39:05 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0ccc4b2sm6591270b3a.45.2024.10.08.15.39.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2024 15:39:05 -0700 (PDT) From: Deepak Gupta Date: Tue, 08 Oct 2024 15:37:10 -0700 Subject: [PATCH v6 28/33] riscv: enable kernel access to shadow stack memory via FWFT sbi call MIME-Version: 1.0 Message-Id: <20241008-v5_user_cfi_series-v6-28-60d9fe073f37@rivosinc.com> References: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@rivosinc.com> In-Reply-To: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@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 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-Rspam-User: X-Stat-Signature: o3s3ctknr76rpfu5eof8auj3c8nd4q5w X-Rspamd-Queue-Id: 298D2140011 X-Rspamd-Server: rspam11 X-HE-Tag: 1728427146-486736 X-HE-Meta: U2FsdGVkX19oCuNqm2hYhIb3E0rmY1fmrss3HHK23TTNjVLo+XzfLFkzenBKriRv0pwoIZGK/Nfyjd3xopMmSEKDHa76W9WPtqz/vgTjC+8J9QAOfISsj1WHSrs3LwJXf2cwfoLRNDg8W3Dwz1Gd5OC1avOC3YGkoEOGCCgIMC7vhN/d2X4ESsWG9AE6SrNNvlDUnPYKcXrlK0FUI3t7vUT3NuY7KUEAo5/8QstIx+AqNmL+t5jBL/e4cayRCJBaeepZuNY2irEqLKNQu/Jwp/NGEBg1v7rRWLuDVwrCvwOtoDaBNAB4kyTwyvhbIdqA0ewnwmUNXJckAetA1hIUi/6KLFMPRI9nLZVDTsNupJXRDXwt9bbDj4nLtpJm+YzhRUThp50S96TQK1FrGsQmv7FjPEuIfDiLkwLjpHXBUeEISkVUXRXAXf53ho29iuby3qdXi0rrao2e9IJ+c9LA/1RiyU7qK5mje5m913DDGCbA/ZpPbczJdL23dlqSpiDTCfhPSEvXC/pWOCIlVouWhDpP2ckIma4u/8b2MXcJ6K53CZhs9tZ9EwD2O5idB+6hno4wmAt+6IqQKxQVDPhplY8isSVZ15nYbQKyHYKtvPGRfyQZUZ2CoV76U3GUAKLqJ5kfzBv8lPX7P42L3bkzW/cLpJQgBfb4ayx3j4mhpmBKXnTSmyO35H4eqEPJYNMmmcSvlLn3IqZYuthCUn1yE9AfpHV1sRi/FDl7dslNwb34/Q/pMnYATc4l8hWUfpVFZjTL1atAP3+mJt1VLmQpbfkvx3+sK+KlFCXsjQj5ExzWYHOygj6deFMbPoMfjTp5SgqGeUEyL/CP7n2YRTGunEG9tWkBpazL/ESEu4Wmmbnhk4VfmrpXthPjn6g6XG2QN+eG6+exKMIN7kjy9JjbDcJi0TskUqD7tBsZplrD/B0ug4ZUEDDVxUIfiWJINH136cRFu3xQQ7cLRol2uEr Gfs3R1p/ rQQIcmwHEvOAJz9A31KACtUSWYwDXfBwNoHbYz2ygQ7Z2k8Pf3MUZFsRjlI08a8NpTXcy1gtAA8UStgJRegrjTsQVwmXuanUQJbaww1SVOCDjofoyrKU1TWrnQrI7J+XAmkMIfJiG+CcHCfVKxascF+Hsf94jw4DtCyRvpR+CWpsiMMR3E4OBnSo9egyOrXDpkhe23JrORK2jbpL2Iy/8z4arUmBpj7ExBqpFt47xqtIpD2E8cczDKVCxtKC3449hhvjT9z9KFAKUBWnZqVGzB0UySvk/NTCOK4WqPhfQF4wAw7dmDLQfBqYWeQZm6rF0V018bwXxa2yhgxnU0aSvrlPLj3VkfN4Q3DxLflSgVf7IFLSnCEwfkZuz8msaUx4/xe5BiVO7QcKKWW7anYIzYJlLf9tlt2U7OD9F/JwdJz9W1k7lkJ0bs2PyGpHmYBsx9KdAhTmDLFKfh1bQzSC6fMZcNwQff8RDLfoTf3fq0B4iBiGIDPJwkMJ67YjwtWL050PV3cXOGLefBSiABob4fZG3N/N7ddAB9pv+EbuHnzfJOQHQW2LfC2DBtEx7Svi+dyoBdV8C7ZRiKig= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Kernel will have to perform shadow stack operations on user shadow stack. Like during signal delivery and sigreturn, shadow stack token must be created and validated respectively. Thus shadow stack access for kernel must be enabled. In future when kernel shadow stacks are enabled for linux kernel, it must be enabled as early as possible for better coverage and prevent imbalance between regular stack and shadow stack. After `relocate_enable_mmu` has been done, this is as early as possible it can enabled. Signed-off-by: Deepak Gupta --- arch/riscv/kernel/asm-offsets.c | 4 ++++ arch/riscv/kernel/head.S | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/arch/riscv/kernel/asm-offsets.c b/arch/riscv/kernel/asm-offsets.c index 766bd33f10cb..a22ab8a41672 100644 --- a/arch/riscv/kernel/asm-offsets.c +++ b/arch/riscv/kernel/asm-offsets.c @@ -517,4 +517,8 @@ void asm_offsets(void) DEFINE(FREGS_A6, offsetof(struct ftrace_regs, a6)); DEFINE(FREGS_A7, offsetof(struct 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 Tue Oct 8 22:37:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13827112 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2B4E0CF0429 for ; Tue, 8 Oct 2024 22:39:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E224A6B00C7; Tue, 8 Oct 2024 18:39:12 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DD1206B00CE; Tue, 8 Oct 2024 18:39:12 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C4BBE6B00CF; Tue, 8 Oct 2024 18:39:12 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 1BFD36B00C7 for ; Tue, 8 Oct 2024 18:39:12 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 3FC2D1A0C8E for ; Tue, 8 Oct 2024 22:39:09 +0000 (UTC) X-FDA: 82651902102.14.5405652 Received: from mail-pf1-f174.google.com (mail-pf1-f174.google.com [209.85.210.174]) by imf09.hostedemail.com (Postfix) with ESMTP id E4F35140004 for ; Tue, 8 Oct 2024 22:39:09 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=PpmoIxwN; spf=pass (imf09.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.174 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728427014; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Iv4iGVPETdra3MKQf2NWKMBAajr7s4N4hA6eTIOU8rY=; b=Yjb7DDk0wZMNDEJjgGK4JAQDdYmYWB42XjWCmMEWtRdEmsnDSSZqWlbdynM4nLVrnWYNka PmKTvDjO3fOG5kOIReLEyXqBhUwpneiknvSptL1x2jitx9yaDVXsLZ4aQvOxZwGyjHqjMp RBucv0QdHFttCTcMSVmYhfpUAgAaIHI= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728427014; a=rsa-sha256; cv=none; b=7t9PRb+YkRpnxjacXT5jv6AFM27IXueggOWe2UYUS9UslvzYS4vh85f/sIXKgF8yLVUo7f aOrxhFruFd0c5SPM52Men2+kTT2bWmGvdEUB3VrAKEEgECkreuW6Hc+6Z2PfyCFCjPohvo wxlCFdWsTsZZ3GN8JS4TQWKNpfDZUiI= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=PpmoIxwN; spf=pass (imf09.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.174 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pf1-f174.google.com with SMTP id d2e1a72fcca58-71df2b0a2f7so3055183b3a.3 for ; Tue, 08 Oct 2024 15:39:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1728427149; x=1729031949; darn=kvack.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=Iv4iGVPETdra3MKQf2NWKMBAajr7s4N4hA6eTIOU8rY=; b=PpmoIxwN7A8/K1zzD8PyR943VaWbiOdwk9lfedCp5JLZxK4rI5ruKArKmmpQrde82v ChUsKBZpQkGrS/UrEha5+V2dPb5djJY7I8GFyEtC+rbSSIcpkFiVnb6YJSGietbcXYZQ 4N46V5hMhmkTdvk6dNeWzAo9hTQGUNm6FcC5vd0w7W0sX0iffepwpT23MBbWQAVCgm6c X7TZjcA8I5B0O9IGb2awDyRZMgDKLFM5YjGdZUhgM9aPzILHeRDeNivXWCjBsxsR9Mm5 47EWBuvVYMEbveRZZHahllhFZ0E4ylc2+O19/Ax/VNgnD3k1eyZC/ZO0p5HWpHzjUM1m mHKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728427149; x=1729031949; 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=Iv4iGVPETdra3MKQf2NWKMBAajr7s4N4hA6eTIOU8rY=; b=Q/5h2HINcEX3L6Ft6z01vW0iwtmGe90cJWidtAIh3lO8I6fI/BCZC/y4Xa07Vy7xDO JFSGRm91Lpa8feh3YNrn26sGkm4U5swBUmjhSCpIN896IX2/mATMsZU7UcFxU4cBxeUH 0RaK5ZyEgomuKhuvvmID193abKC8eaHRGR01zXPF1GQaaPiZhsq+8HtMOnw0cc5g+mIL 8mkeLms/g4/A6TeC7eF587Ij+amC/QCJRJ4f2+lkGGdywGR84K2ebWey3D3kMHqdn+zh cTJO9JlXOTVfMF1SkVGC/GMF5ZYnw6mCYitPv7kys9HE++jj4nUfEuACibofgN1uGooS 6Usg== X-Forwarded-Encrypted: i=1; AJvYcCXBZRTEOnIIgMW3ZUdrh1qpkJ2jKP/+eWWF0Txzs0bWZIb6Ip9P6/RHq3WjOMbdZAqIBTT6Dbo9JQ==@kvack.org X-Gm-Message-State: AOJu0YztKObor2rJ48HwgUVikbFYVHdzP+gGwQ7J/kHxvRLPoYU9Pm+6 Zeg38XA1ksypL8snQ+H8YgohzxHOdc3uluvcMBr/GytKPG1JYsspWABTfIBoVDs= X-Google-Smtp-Source: AGHT+IHjoVQEvv2ZL9MtUd0PoitnGhc0oOx8qTU8cQ9IzAvoigYxCtNZAaIOLoagpVlmMdjHekGPKw== X-Received: by 2002:a05:6a00:244d:b0:71d:f423:e6d8 with SMTP id d2e1a72fcca58-71e1db680ecmr668933b3a.6.1728427148723; Tue, 08 Oct 2024 15:39:08 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0ccc4b2sm6591270b3a.45.2024.10.08.15.39.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2024 15:39:08 -0700 (PDT) From: Deepak Gupta Date: Tue, 08 Oct 2024 15:37:11 -0700 Subject: [PATCH v6 29/33] riscv: kernel command line option to opt out of user cfi MIME-Version: 1.0 Message-Id: <20241008-v5_user_cfi_series-v6-29-60d9fe073f37@rivosinc.com> References: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@rivosinc.com> In-Reply-To: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@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 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-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: E4F35140004 X-Stat-Signature: 7f19emsc54q5qp68tuu739figt7cerhb X-HE-Tag: 1728427149-601059 X-HE-Meta: U2FsdGVkX189mDFwnnCyCd9HH8wwcR3uKGvXiE1S44fiUGZxUrzBQVz2fb0z7MOGCchSj/OptabpL+uhJmT67K+oskmVUWdkuf7hyHYar9kPE/DaxhhND3BTTBcQNG9BA+7Itr2RaFrgel6jgsxKEfwFteGfIyEmTvfKDXwZZIEKcPQGrIanOi0tXHoCv9bpx510HTwmz3xqpZv5mTcdyxI0hiuMJPrn1wctFwxvMgCDLubFmkvC5VSzvtQ7JqD2UwHv8I0E8sN73Hxes7uX+heyvILzGvgPRwFvB/BJbCodrmXxh5xWZikS2dYmkWbt9m+X6V69TJmAXlH3Payag1biLjwhVIF5jnEmfNq2He5B/1y244pMSKLJfGy+JcBWZBgcrMXi94gFZB6n6UGNhVcEKhKYvHSP/6ZI3aCq8p3rGDqxJ5rsb6Dv86BKDsnFKv0tvIWBHVDMtMzVxKZkuxpumdU5DSwR1Gl0agLm7QNAXTf5pUp6d6VTTYMcRQaxzMnqLSzkPB8CiaeXBN7drz2mUsqjIH+UIstKAxCnvlgxlNrLgyla9EdOBLSUwmKtv/tD2hE14qK8bAU07KWvFy3CYbR10tRxfPQdd+xkp+vV6l8hgAIB8VkP03nIzKmkPXtmDORHGp9P8B7eyIpvcwyMvrxaPMkCZ2sI4zMa1fGUci4Ro0t2TAIuIxxDbaYgLpnt4lLFscu9EbLxnVe2OFOuEo7ZNLXE8NvwqwGQj72kpG8ww6XEcRJOzVd/s2qd0qAU3JiHPtb+LHLVXmzRgEdG/Y6oCREY14IMfszC2R1LNqc/uZbCfLnWw85GnE3BaSEmSd8MO0x9ki0hIIkBsRIvycFsQRvqK4nL+bJfmAkEI4p7+MAK1CGmyZ6vuQvcUX8hlAAVgrrmInINxrO2d/BIaX4rvPN7LHkMTsUmhtgVpeM6b3jJtl/IrK1Zs9aywdyrcxYHDQBbxHCyNUg jirQFs6h 8s7+/9ZB/D5yTf526dDbEoaOfNYnzUGV/PQcyfPfkjO9rckAXPtvF+RP76KKjFH41w+D8VlJFeFZJ8XZMuObZtO+Aal8Py/g9hZDCHdQf27teOK66occF/qLRS0xMWFLE+NesoLBkpITBxxo2BqN8fNca1iWXPGYt4tQVt0ETcqYaeHTY7pO1g4RAixjX7E0C+YbVGeuWr2NjZxdbaG8Rs282Lb2AJMsQ8mqSPR/442m6t2Saq6k6qs1TACik6KhV+d6NTClV1SD/Ard0dN8q2ZNCIPhFvlPw0KCVLczfEnMfg3wxPcKbiEZOGCIMcWSs4Yhu4cG4O7yRxH1TDf+4DbO0msmVDcbHA626G4C+iAPK/hRIAy3gKBsMbn87X07Tpe4LsFFizpsOLwJJBK09jID+b5taSfZPaZMlznKGFkcaT6XgZOJzjilGHiCSsBjfhlWdmHCfcg+J82qfj9upEn8O/tGIAUWX8H9l8oh4IERPrSI22uasCwEoifR2FkNQwMs8zrluC+SlCU2cET6wKP3Jhvcw4tKsTRIFwspXlNBBOqe6rdXRekr/1PxblChztZ8tb3JCzfhku8A= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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 | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/arch/riscv/kernel/usercfi.c b/arch/riscv/kernel/usercfi.c index 92d03eb76c03..fb17a67568a8 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) @@ -393,6 +395,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; @@ -472,6 +477,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; @@ -504,3 +512,15 @@ 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 Tue Oct 8 22:37:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13827113 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 65C16CF042A for ; Tue, 8 Oct 2024 22:39:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1C6036B00CE; Tue, 8 Oct 2024 18:39:15 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 175AC6B00D0; Tue, 8 Oct 2024 18:39:15 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EE23E6B00D1; Tue, 8 Oct 2024 18:39:14 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id CCB8C6B00CE for ; Tue, 8 Oct 2024 18:39:14 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id F0C971A0C8E for ; Tue, 8 Oct 2024 22:39:11 +0000 (UTC) X-FDA: 82651902228.10.4572668 Received: from mail-pg1-f180.google.com (mail-pg1-f180.google.com [209.85.215.180]) by imf22.hostedemail.com (Postfix) with ESMTP id A771CC000B for ; Tue, 8 Oct 2024 22:39:12 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=gNdZ6ed0; dmarc=none; spf=pass (imf22.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.180 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728427109; a=rsa-sha256; cv=none; b=cWVIqac9w+FQ3dAjA2WPf2IGvfG9OUUwJWUcRGFFp46fpVfwv2AIl26WiHz7WZY9RGi9hZ IogEd42iKmR6L4LQ6eDmtwMdbhQ6R/lxwCsP/jYPywexbDMbP4FnRXgqvIOpr3Cr0zoYU3 98MtCkf8VgRgTzdoJqumL/aUU80srG0= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=gNdZ6ed0; dmarc=none; spf=pass (imf22.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.180 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728427109; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=asJrmHut1Q6UlWCCNbkBgTT2I9NUCgfcqtf1L3nHA+A=; b=TucOKJqN1nuc6X1ZJdEUHpNs+vOO69Zwvd1Syz94aAMqgSD3tu7eavOhi9vUlSFvX5/AtA mb0b3Nn4LzV0UUEEnF/YWpPRVAlXerLXaFAk1KTdv8MyjtDYfELkL4guN+LNb0Qo6TMjOC WOyECDruJFThaDIHL6FJ/XAvIuq2nOU= Received: by mail-pg1-f180.google.com with SMTP id 41be03b00d2f7-7e9e38dd5f1so4577053a12.0 for ; Tue, 08 Oct 2024 15:39:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1728427151; x=1729031951; darn=kvack.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=asJrmHut1Q6UlWCCNbkBgTT2I9NUCgfcqtf1L3nHA+A=; b=gNdZ6ed0xew2dpC4tgS0Y5hVCuvepflrtpHFuXmJIOGW4E0OvKbKVmM3jvxhv+mP4n sUW3tT0alNsnsHoaGeEHb1+DCNI2IZg/9+lDiXuj0eZngAytA1nbo3sy46nvPo4qK/0E nZOLluWDGAs0CqP8tDoLXcZquRQn+OH5d31laMqm+aNfPt3WPItx81umo8fHxdDfn7gV zzHsUNCuyS4+o14CTR9nuAMOEabwjveYX/tHccP720boaYHrLBYG0ABHRny6X3iUPuz2 rJtFhKaNaVDNxMACjf9G9lmGluZzjK3340TLYDzNJvS/eHaSh/xAa27Ygu+Jp3Ru+phe ONwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728427151; x=1729031951; 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=asJrmHut1Q6UlWCCNbkBgTT2I9NUCgfcqtf1L3nHA+A=; b=FI6xjkc3qU8QfY8FbVecnEm7LD6Uiy2YcYyFXiwNwQhkvfdh5SocZTm0tfFzptDqZ7 zhTDuieeSYZxUfqlLd28Lec0YoGCpCudaIJW4t3CNRcaoSrQJ0w1qAu/LJdL9CRjSqLQ wmToyPBzGo84u5IbQuIBrzMGMeglKb5S+ffzbziHr+C8GSQmxLdUpCR/1VBZh0e/lr16 wbHdJ+faludBXjzIy6aNCtqpr6g5QCC50GIv1AAg7Q81r5MKLtuQwOy+3I9tyaywlZq0 KysGJamB/G8rPObwA+DFByW/omCALR9Q6nzas8LLk8964iH0XOo9jTAjUVd6T/Y4zPdp qdJA== X-Forwarded-Encrypted: i=1; AJvYcCWfFPbUBSk09zVYlxWPLOSIsX2ZziFAp1B5d+gTziFA0zqGF2g54LAevyOsPaG+DEVgTgKod9xyoA==@kvack.org X-Gm-Message-State: AOJu0YxHGj0asTMhhUW7jel4YTG9hxlgVnaPknMyg7u/LG/dGkiOMxp2 jw3QyibX4h7Ghh/q0V3676HFqVviLXFadZpMIAqbQQNYEAmFxAK3IWYwkTo9ovw= X-Google-Smtp-Source: AGHT+IEDqNZ//hNrN/QcTqq3QMYiauQAdv4OvwSpudvpfbUKRWUptDxZ/4bPpZt2oZiP3IfdvQ1Z7g== X-Received: by 2002:a05:6a21:31c8:b0:1d2:e9e8:5e78 with SMTP id adf61e73a8af0-1d8a3c1e496mr717902637.23.1728427151493; Tue, 08 Oct 2024 15:39:11 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0ccc4b2sm6591270b3a.45.2024.10.08.15.39.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2024 15:39:11 -0700 (PDT) From: Deepak Gupta Date: Tue, 08 Oct 2024 15:37:12 -0700 Subject: [PATCH v6 30/33] riscv: create a config for shadow stack and landing pad instr support MIME-Version: 1.0 Message-Id: <20241008-v5_user_cfi_series-v6-30-60d9fe073f37@rivosinc.com> References: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@rivosinc.com> In-Reply-To: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@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 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-Rspam-User: X-Rspamd-Queue-Id: A771CC000B X-Rspamd-Server: rspam01 X-Stat-Signature: zknfg1tnqajtbxhxigntqptqhkfbz5iq X-HE-Tag: 1728427152-536379 X-HE-Meta: U2FsdGVkX18bfVGKPauqqDMJ0tQMgl/VGriP1q2WIpi2g/nBSpNjNLHNCKr1Pd1YwHvztqZ7HtsQNseTe8jP4DPdjm9wXih6pctSkMf5AynSEeVaJgq6iOzwD1Kteam5lOisAgfiB31kJLKl1Z92rxXTm5FcNUTJpzTGSe5IiwZBNTgdaqkAirCvcdXkpyDGk4c8ad+QoT/vFsUefYyIO89+pzRYesiYLl4AKrFZoNbnTi1/vSQjoUMwQHZAc2m7yRc3M0ZxtcGH881LD7bGcnV1lW3gVJetgM56iMuOBz1HfBAwb53XGOJwStJpN3rGd3f2hKC1lE9Tgc24fB0O9Y8xV1BGVJdn4ESbjJ4ewZoNWh8e0i1thbwn/jJLlCeL9q73HmfvLA6CaCoXFUvImqR2ajDwMbyybEvxjNEt46uyGjO1mz2mxDyziB3RDGhZHvMDHH0KBUabUekmnNl5jHOJEodcLMvYfoG+7ArWi9RmQLJn+P+NtTzmXRI0MY17y3V9doMyxmY/SP0i6g+1MGru7QvX6VMtzF0b2TCAnNThAfCbcQpxK37QT4IFT62l/sfaPU9fG8HdB08Lpc+kUNlB/ylpkB8JEzP9iMGaOgTgXWMvES2czxAVeHyn9fxInVIzTbnVA8lBwUL1Wr5/ZzXNcPQYXHx9XZ1WYAPTcLEN0oaPCGcJe8tkDuB9JWQdcb56EWb59U438srasMA9ev4c6vqCOJO1cIzlMaAXr/ZSuZbvdFyZqoNKp6BuvyHzjFAKJPimqhN811IhiF4oRd5ToH0bNK6khNcDig06WuUqHDvM686eZ57aWPZmWIW6z1wO2xMBhK0rpKYzjLC48/47TqQopJrRdxj9JUhNOoQQSmb2Fkiszbp+IcEfYbTJfKSr0t990n1c2nGx6gt3p7rcUcF5gKiO+wUTWqQqRihkUojBZfi8c6RDA7nsfchyd7dhMi6oscPPsSuTMej NFSqAE/D 3OKMyN3736S8W9XeicXg92lwQ9uVI0SlIGqNbiDSFQSF1qmJqmHyrpWoIhlyPzjyzamVUOChEM1dPxJ7RQuSTYByIX2KXRbnp4xeUEpelro/YWPEHDTwLfsfLT+TimtOdA38om/jv0sgGX2xbKYDLVtc4U/hRn2w0h50rXZRqe+OZKx4ZLSEMsCj3ZLT65d1GhVr3TAgeuea1xz3Ldtxf/8uvcng0U2PDZrMvcWnW95/HT4VPfNE/RqOqcsStAg1aP6sAdcZWigLkcoPER1rwI6wHl0VeUtCWuHAYk3qy5KSkGTaKf9M9felXE6vIqCtSFB/OKDmKe1RcuGKWtJNCpwhiJh1Wvuo9OJOtZDAJy6wn4pEui+pCpuufMmKkfQT4wBVHGsa/xzbCn6cxxTokUm8f5e2Csi5sh/FiwQRYFHqeX9HP90XVcPon7cSSZ1kdQtY8Mauf5yG29cGQfu5DeqI3Km7D7r6l6Joe4GSgFerjpU8= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: This patch creates a config for shadow stack support and landing pad instr support. Shadow stack support and landing instr support can be enabled by selecting `CONFIG_RISCV_USER_CFI`. Selecting `CONFIG_RISCV_USER_CFI` wires up path to enumerate CPU support and if cpu support exists, kernel will support cpu assisted user mode cfi. If CONFIG_RISCV_USER_CFI is selected, select `ARCH_USES_HIGH_VMA_FLAGS`, `ARCH_HAS_USER_SHADOW_STACK` and DYNAMIC_SIGFRAME for riscv. Signed-off-by: Deepak Gupta --- arch/riscv/Kconfig | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index 808ea66b9537..1335dbe91ab9 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -245,6 +245,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 Tue Oct 8 22:37:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13827114 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id B6029CF042B for ; Tue, 8 Oct 2024 22:39:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0B5CB6B00D0; Tue, 8 Oct 2024 18:39:18 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F313D6B00D2; Tue, 8 Oct 2024 18:39:17 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D5D496B00D3; Tue, 8 Oct 2024 18:39:17 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id B6DE86B00D0 for ; Tue, 8 Oct 2024 18:39:17 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 6277EA0C9A for ; Tue, 8 Oct 2024 22:39:14 +0000 (UTC) X-FDA: 82651902354.22.65855A9 Received: from mail-pf1-f169.google.com (mail-pf1-f169.google.com [209.85.210.169]) by imf30.hostedemail.com (Postfix) with ESMTP id 7D7938000A for ; Tue, 8 Oct 2024 22:39:15 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=iALgD3uf; dmarc=none; spf=pass (imf30.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.169 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728427006; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=IhmPwONIKNZXNlLItF/RqS4Q7SWFxV+hpF07L6+CC7I=; b=AxCKtvE9dc5ZWwn0aqTpCvXJ0wgQ+W93ynoncoc0Y5FptYttaWYsGPlgxsR+KxAGszyeZ4 X757hxxXRhFAT6TtVB/8VucRC2yVW/ShLnG/04jT/X0/Wok+LaLOSgnr8oCXb9nxpaKbsw 3Sc1uJKbR/7427zQHtgYG/ymAFVAuow= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728427006; a=rsa-sha256; cv=none; b=MFsYNqZ20R30VV73ttfcSfyX9b9g7dl9dcXHyvZbvKByg7r2iCcwOtzQghbv1/UxYGqqPs l/54B7ZP3z+vlL9r1F4oj4lXpXJqM/APo2kDJk/pQcRu2dHu3rNyIU2/McBim0uXnI4K6b gBOtk3l7acshzITPUhfwttTjAqxA60o= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=iALgD3uf; dmarc=none; spf=pass (imf30.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.169 as permitted sender) smtp.mailfrom=debug@rivosinc.com Received: by mail-pf1-f169.google.com with SMTP id d2e1a72fcca58-71dfc250001so2441004b3a.2 for ; Tue, 08 Oct 2024 15:39:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1728427154; x=1729031954; darn=kvack.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=IhmPwONIKNZXNlLItF/RqS4Q7SWFxV+hpF07L6+CC7I=; b=iALgD3ufSKjee1CgQyoKfahG2HyNJUKbSp1ZpYzbzfNVk+pv77cOL8NPp6k/NBa3HH l5TXt/I1/2sflvlxtaofmDeBsxfUtGpYEVQxMGayDOR4iTURd0J53vOvZyh1lbb+ECFS zcgT23XA6F8yuM1W+fAXjhFVM+Sto4g5gBJUhv2rEsBrwvsBLIQRT6IaBvEScHq4oJGL quhnfgLZHgAU91ONQobZXxcQGmLd8HU89d/czNjVGM8PHH0b8vLEo9xBCGFJR5VkhTEB cFFRaJn7/1xw8OjERwdWwlsfgzxkRHwCfAW7/7JaoLEaiEear9O3KKWqF6iQHV4/npyh zm0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728427154; x=1729031954; 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=IhmPwONIKNZXNlLItF/RqS4Q7SWFxV+hpF07L6+CC7I=; b=vJEMT5BFg+/2oHRAaMtx8kcswe6fyglho+IwOMjMEmOuPZQyOMSws3iNR43ReJLve0 CPGYAEXBfgmCGBEKYrRBIFYaq7vu2Ntfgrjbg8X3jiQU/GT/EPUt8OobtPMKlc9zsGcF edlUJ8Gy5X6QNE0B2hkDPAbCEmwrQqEJ29OLv+KKXezoolN9lCDZrlawKqKmFqj0r1po SR+lCBeEQ4/dLolKsxw5JQclg7CTDYx6fZppCKpLcRY8hduSEqRNhgPeYOrhBebGpyYv qUAkxZzvgbSj1PgH3j9/IImd0ysK73O/zYHAKgGKPRaZvfF373f8d80gaVhtJ9zmmQJq PcBg== X-Forwarded-Encrypted: i=1; AJvYcCW5R9N8eQMw7kVoWC1f2YoFjVM8ADunrxsjg943KlzfZfIKk2hWRmEGN4XLs6AQZoEcrJ46ciQw/Q==@kvack.org X-Gm-Message-State: AOJu0YwuO9HNhgn5c68qQAJ4DzsX3OpoOj4J5H6oUzEUZ8NnEHWoBrzW zO7bvk6koRogMntTYKjXU09FJSvr9oF1nrNTVHTCsI1kNtZAv8dwNlWmKRjjW94= X-Google-Smtp-Source: AGHT+IFhrrVCAvMr1wC5DJnZtVwaJuBEeuj1lNCuixeCmraJtyPtBPhxB7iJ82BXGIs1drH6LcqhSg== X-Received: by 2002:a05:6a00:2e1d:b0:717:839c:6838 with SMTP id d2e1a72fcca58-71e1db87d63mr607121b3a.14.1728427154202; Tue, 08 Oct 2024 15:39:14 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0ccc4b2sm6591270b3a.45.2024.10.08.15.39.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2024 15:39:13 -0700 (PDT) From: Deepak Gupta Date: Tue, 08 Oct 2024 15:37:13 -0700 Subject: [PATCH v6 31/33] riscv: Documentation for landing pad / indirect branch tracking MIME-Version: 1.0 Message-Id: <20241008-v5_user_cfi_series-v6-31-60d9fe073f37@rivosinc.com> References: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@rivosinc.com> In-Reply-To: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@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 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-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 7D7938000A X-Stat-Signature: ixm4u4zzucfi9xggsrgjc8pgzh1huedq X-Rspam-User: X-HE-Tag: 1728427155-274658 X-HE-Meta: U2FsdGVkX1/gnM0+DgyxQJkPfRHJzA+Ln96SGnXhww+3bl3WR519ydwXAbbgiCTzi4+T6WSAJI6n1G4ElafYaFR/tA0wjZzzAqICN/Yu8SKiKcb7m39kebOnmBRh+shl9TsYhlQVi7ZK8xY0Ph3CMvluVJ06uEkcayu+nvSJJ20/upFNwcoxHwGGYsZ65piE/ZG9uAsVeLwDl9qsS7ydcqp05j3LebV/gTwafpiZIlgeI6V+ye20CDWFVQ5y00fvgZz6R6OL5hbYdWbbwjgbxboiZn3hGVfaE2K3gxc88y0cAvQeI7syqjmSLG48tnsZFayiz0pI4BfnV9lfL2Q1/ykDB8rNZ3juLv0dJXKCVZT7CT/EeDFHrN9PTDDxE6IYViPRdrGiE5JfypPm8DuCD15YA7/V7KldIdcZBjiCsAtLrJ5AgG8svDOmMmZTF7ADt+ji2vE6j5HzQwoY1pVUKrKHN7pl20zyQiqs0PxIRu9dV7EgJXKIPGT3EYeSs31MTsr7x8cXTqFaRi88O4aPgxJP+Gz5R9Xwc9en0xwTLe9abQQELnstiAVz7OpxlYTa/XLUqktYbFeIK3+wc1X3WMI1frQtu4zi1guD6n2GPKGLeFuQFZcS+bmY/ukrOUNQFSd021+v3LF2inBXSWOadU1SIYl00W71fNkMxniIy9EMR65s5P22DIZ5Mgfk+mqo4Jd2z540U6HtWn1XRJ2kE+1jTZDBzHZ9cyhD9PzlwYOBRvw8Sg+0dQCv1G1I4/sXtyNcJ5kGFbj9CTzS98D3GU9x1q20tV/vU2pd7QgHXb7zkZFCxen6p5wvpQrP3AkI7uoI/B0JY4vjh3q2XL4XoZ5wefyQMlyRgnQI1HmnbfdBMF6MZxaWZ8xuhay6t/F8IcT84qzomKr2ltoebkZ2LwVL2/BqJbe6Fdsq6VKlEvqYd3Qf58Fo32gC/fr+khLVi4FHZVZGB1BZPBzPRmH C2iVfCwk GvURYlStZCN/QttyNdsLJQ7VMohff5fJldRhqGbdWIoLCaLadXWX0I1zLHjSrDDq4qPhoYDlimW6OOBY2vhYsAWHlygWLMIU6AM1fuUf934mnU4XwZIn/mp6BlAFZw1cjFLx65IQ4uP8QJwntEnI2bgr2y8uzxxETKlN4DnOkrpEYMujYCUWUqVGfPn7ZQD7yXH/DHflg1H4inz3XDuP3u+auINZ/NJ2+lkjATO6kaV/7W7R0It03HRoBIQ8ZPlnzJrn3+y1wjtATotgM0YO6Fz3KZVLW756mx//lhvsxh9lf9lyCzHdnvWHn9lonjDjqNkpXMsJsph4rc9U92IImiCZYxvqxfcOf7cnZDtKuDWhx/Roe4fI6hvcGqKChoiDV03VbC1Q2R5JvsjLXKVYNEJ+Vcg/gX5B+LOFEFJrUiLCdJRVawDICjq1xhEED66x19zaSycVZmZSEf+Hk1ObB9zNqShdXG8eoCeeYpeO2NQJhAaYSlWhkk73IlDbnjcDZxMrEIb98XmhN/7KGtaC3UdZRew1l4WsYkkLA1G6YJSzriGHpRZHI5YwE5MdRVNhUA7KJSdk5m44/7UI= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Adding documentation on landing pad aka indirect branch tracking on riscv and kernel interfaces exposed so that user tasks can enable it. Signed-off-by: Deepak Gupta --- Documentation/arch/riscv/index.rst | 1 + Documentation/arch/riscv/zicfilp.rst | 115 +++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+) diff --git a/Documentation/arch/riscv/index.rst b/Documentation/arch/riscv/index.rst index eecf347ce849..be7237b69682 100644 --- a/Documentation/arch/riscv/index.rst +++ b/Documentation/arch/riscv/index.rst @@ -14,6 +14,7 @@ RISC-V architecture uabi vector cmodx + zicfilp features diff --git a/Documentation/arch/riscv/zicfilp.rst b/Documentation/arch/riscv/zicfilp.rst new file mode 100644 index 000000000000..a188d78fcde6 --- /dev/null +++ b/Documentation/arch/riscv/zicfilp.rst @@ -0,0 +1,115 @@ +.. SPDX-License-Identifier: GPL-2.0 + +:Author: Deepak Gupta +:Date: 12 January 2024 + +==================================================== +Tracking indirect control transfers on RISC-V Linux +==================================================== + +This document briefly describes the interface provided to userspace by Linux +to enable indirect branch tracking for user mode applications on RISV-V + +1. Feature Overview +-------------------- + +Memory corruption issues usually result in to crashes, however when in hands of +an adversary and if used creatively can result into variety security issues. + +One of those security issues can be code re-use attacks on program where adversary +can use corrupt function pointers and chain them together to perform jump oriented +programming (JOP) or call oriented programming (COP) and thus compromising control +flow integrity (CFI) of the program. + +Function pointers live in read-write memory and thus are susceptible to corruption +and allows an adversary to reach any program counter (PC) in address space. On +RISC-V zicfilp extension enforces a restriction on such indirect control +transfers: + +- indirect control transfers must land on a landing pad instruction ``lpad``. + There are two exception to this rule: + + - rs1 = x1 or rs1 = x5, i.e. a return from a function and returns are + protected using shadow stack (see zicfiss.rst) + + - rs1 = x7. On RISC-V compiler usually does below to reach function + which is beyond the offset possible J-type instruction:: + + auipc x7, + jalr (x7) + + Such form of indirect control transfer are still immutable and don't rely + on memory and thus rs1=x7 is exempted from tracking and considered software + guarded jumps. + +``lpad`` instruction is pseudo of ``auipc rd, `` with ``rd=x0`` and +is a HINT nop. ``lpad`` instruction must be aligned on 4 byte boundary and +compares 20 bit immediate withx7. If ``imm_20bit`` == 0, CPU don't perform any +comparision with ``x7``. If ``imm_20bit`` != 0, then ``imm_20bit`` must match +``x7`` else CPU will raise ``software check exception`` (``cause=18``) with +``*tval = 2``. + +Compiler can generate a hash over function signatures and setup them (truncated +to 20bit) in x7 at callsites and function prologues can have ``lpad`` with same +function hash. This further reduces number of program counters a call site can +reach. + +2. ELF and psABI +----------------- + +Toolchain sets up :c:macro:`GNU_PROPERTY_RISCV_FEATURE_1_FCFI` for property +:c:macro:`GNU_PROPERTY_RISCV_FEATURE_1_AND` in notes section of the object file. + +3. Linux enabling +------------------ + +User space programs can have multiple shared objects loaded in its address space +and it's a difficult task to make sure all the dependencies have been compiled +with support of indirect branch. Thus it's left to dynamic loader to enable +indirect branch tracking for the program. + +4. prctl() enabling +-------------------- + +:c:macro:`PR_SET_INDIR_BR_LP_STATUS` / :c:macro:`PR_GET_INDIR_BR_LP_STATUS` / +:c:macro:`PR_LOCK_INDIR_BR_LP_STATUS` are three prctls added to manage indirect +branch tracking. prctls are arch agnostic and returns -EINVAL on other arches. + +* prctl(PR_SET_INDIR_BR_LP_STATUS, unsigned long arg) + +If arg1 is :c:macro:`PR_INDIR_BR_LP_ENABLE` and if CPU supports ``zicfilp`` +then kernel will enabled indirect branch tracking for the task. Dynamic loader +can issue this :c:macro:`prctl` once it has determined that all the objects +loaded in address space support indirect branch tracking. Additionally if there +is a `dlopen` to an object which wasn't compiled with ``zicfilp``, dynamic +loader can issue this prctl with arg1 set to 0 (i.e. +:c:macro:`PR_INDIR_BR_LP_ENABLE` being clear) + +* prctl(PR_GET_INDIR_BR_LP_STATUS, unsigned long arg) + +Returns current status of indirect branch tracking. If enabled it'll return +:c:macro:`PR_INDIR_BR_LP_ENABLE` + +* prctl(PR_LOCK_INDIR_BR_LP_STATUS, unsigned long arg) + +Locks current status of indirect branch tracking on the task. User space may +want to run with strict security posture and wouldn't want loading of objects +without ``zicfilp`` support in it and thus would want to disallow disabling of +indirect branch tracking. In that case user space can use this prctl to lock +current settings. + +5. violations related to indirect branch tracking +-------------------------------------------------- + +Pertaining to indirect branch tracking, CPU raises software check exception in +following conditions: + +- missing ``lpad`` after indirect call / jmp +- ``lpad`` not on 4 byte boundary +- ``imm_20bit`` embedded in ``lpad`` instruction doesn't match with ``x7`` + +In all 3 cases, ``*tval = 2`` is captured and software check exception is +raised (``cause=18``) + +Linux kernel will treat this as :c:macro:`SIGSEV`` with code = +:c:macro:`SEGV_CPERR` and follow normal course of signal delivery. From patchwork Tue Oct 8 22:37:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13827115 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1A779CF042C for ; Tue, 8 Oct 2024 22:39:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DF98F6B00D2; Tue, 8 Oct 2024 18:39:20 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DA8BF6B00D4; Tue, 8 Oct 2024 18:39:20 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BFD846B00D5; Tue, 8 Oct 2024 18:39:20 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 9B2366B00D2 for ; Tue, 8 Oct 2024 18:39:20 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id B9FA01A0C80 for ; Tue, 8 Oct 2024 22:39:17 +0000 (UTC) X-FDA: 82651902480.11.892DE72 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) by imf10.hostedemail.com (Postfix) with ESMTP id 4CAA4C0015 for ; Tue, 8 Oct 2024 22:39:18 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=P52wkH0Q; spf=pass (imf10.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.179 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728427022; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=NYP5wvilXBl9e2AdD/12DI5Fd1eAjNDT/0JkZD4bKgk=; b=1P6FPh0gnccpTFSu2vUjSccxNPtTe9L6STAXtUpBZAe4fkzvRw6UzPNFPI6JxLosCIUAjd pv8BRTmFwMPyeleXtBqvkUvUtQEdldlnDwpxkno5LFd6RBn52EubFGr9OxoZLxPRBzekhk feUPSb1I+kBkRrpfYu+Y5vCqj0mFfBU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728427022; a=rsa-sha256; cv=none; b=UJRPICo38LiC/VVZScsZWz1nozCZfH7aj2XM1MOG/M7pzmLT/GRtzgBj3OSxXLQfkK6SgR gbBCjd2khVO9fdGBamlexiShuEhFjgwRF9h+Rnf4GfRtzANX8ziiGionxpg35fAxx7hYb/ zm0qGMj39xmvNBREim1i/vYnplXWurY= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=P52wkH0Q; spf=pass (imf10.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.179 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-71e03be0d92so1982398b3a.3 for ; Tue, 08 Oct 2024 15:39:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1728427157; x=1729031957; darn=kvack.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=NYP5wvilXBl9e2AdD/12DI5Fd1eAjNDT/0JkZD4bKgk=; b=P52wkH0Qa3RSp1LW6Qyq7QwL5GqMg2+EjAy08nPNZRERV03PfYzVzDEp8xR6FvPHYA zE9r2/SqR10r6P1UH9uVcDZCETMx8SH1oFG7GbelR9vavrH8/Wc/HFXi3KbDHHjE/msV zL/x8KIAv2eE3+/F7tmtkXzWD+rEQlruPz+t97Eix1o5ZHdRLPG/sWpiOFVelI4F37+b KYteCm+ICuyw1TCI289FcQzrzDYJjf7jZ7h3A+CJ0pTuL01RDJQTwZ5rOxjtEfKIG0Y8 OAdsaq5rmS8ioqfE5Ee/AWb0rWC3Odq4EyjVu7OFTfYzHrGoH4i5JtcjFNA6iW1camZV qf7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728427157; x=1729031957; 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=NYP5wvilXBl9e2AdD/12DI5Fd1eAjNDT/0JkZD4bKgk=; b=KhAX15m40xty1BOUydZ2XsWsfC20VcD1ic8Z1cMVdiZoXIR66prXfErvs2P0el7/1s B2DhijUtXBDgWtH0eqLEooQUs/oTdYdT8IkhZDg2dkCvO42hDNyScApWwOZ3B1CuVUkM mbT4TZWjYG6wYcp/8WHb1vOlKiEnzkmHJMPTJlUXcoUoh8U06DF37CVV0JGIi0TD+dE2 gWkydUWS0Q5bD6aqHAtoEqlrQZEXpsC5ybd5j0PQ3OqNHwSwyJ2pBpu5Mmj0aWfZ30RU /CSpj9T/f00OWJBIabAsIu1k8g6aUel9hVOAu1VEkBlfTnTYkAfEzZ6ZnAnc6Rr9t5tD 7OXg== X-Forwarded-Encrypted: i=1; AJvYcCXSK4mNFtXyxithW2p161HUi/u36hthdaCU9yv/g+MJZjQCeplI50J2ob64+xS7UkyA4vNP/XMuXw==@kvack.org X-Gm-Message-State: AOJu0YxHcey4FDQrQp3fvwSzAmspfaZ09oCrJGXi8+TDQ97vWFZJ3JIY YhhBHrbca2rBuXouL4nnzkwFlDYsjLS2GVTr1UcdYkgg3nI2YCLbxtMvHGr0lrQ= X-Google-Smtp-Source: AGHT+IEF+hZ8cFpEbHxK77l5nSPNL0l3e1unluSpt6JIy+85sceEzMd/bxxFkjVH5urq0w0ZUmcKkw== X-Received: by 2002:a05:6a00:178c:b0:71e:179f:e8c4 with SMTP id d2e1a72fcca58-71e1db85907mr588322b3a.14.1728427156969; Tue, 08 Oct 2024 15:39:16 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0ccc4b2sm6591270b3a.45.2024.10.08.15.39.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2024 15:39:16 -0700 (PDT) From: Deepak Gupta Date: Tue, 08 Oct 2024 15:37:14 -0700 Subject: [PATCH v6 32/33] riscv: Documentation for shadow stack on riscv MIME-Version: 1.0 Message-Id: <20241008-v5_user_cfi_series-v6-32-60d9fe073f37@rivosinc.com> References: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@rivosinc.com> In-Reply-To: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@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 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-Stat-Signature: opq3pmnk1haxfoekgk5ng9cfwueb3oc5 X-Rspamd-Queue-Id: 4CAA4C0015 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1728427158-706323 X-HE-Meta: U2FsdGVkX1/DDzz+S+LcGGxCO/K2R1MsxE8N8dT97ZxJ/h56YBsd8zDXEYXeMOGrDfHXo9j6oXO2iO3U+RVd1MO5clkXCu9Jti/D7X/W2eqikURibn+WphtEN+w2nxtlS035kWGhnNl+N4Qk59VMXtmN7CEG9fDUy2DWv5HBD0tcNRenhjnfA2fffS4Y5DYRgOEE5W6OKFEIVc9aTORkR6hUdAQLqsgvm0BVdHx6GHKQWWulmvf/uMqYjZjFr+Nebtxsa831vcYK/IRzFFkqiyiQGyV+AWoM1TDlUIwJz1644F2z4jh6gZycwXWQf5NmG9u/ikiXDsOkn243N6Ro5HWjCWV0iHmyowBgq1Jn/ZIxFM9qME6+hEMubM7T9TzSuuDjG6io9HhQ136X1vgxrK2iWmgqnutj2OQ57bfSHRAQZMlLrkljfi/i2FPiU+vy6GOK32qnXM3Zotd0zWEtgppDRaG1kDCdDFwqpOKcpyKSpPfIw7cFVR0biKKI4+siS7dTxH/H3kfxQG8Mc1gi149C7DbeKo/RNTzw1TBkd1BiAaOw+xkexeYpTdGnND2U+z/1jelMzXPVPW95mqIzJ0jQLYXIUdjR7KKKMzP+MhrP0hbcAmgsBAOCqBREqvPzbK3bDgtimIdTrxshR/QY7AurP4vUBPvvRtMWPZmUMMWE+4/Om6OXMgON1X2O3RWyXKwnXcfxA3LSCA6HHWiitU25lk/6IPB7eGvnWqOuaBBJwG4pfrSw7d24EB/WPvVTgqXaGKGlXW20QNjc/sK+0Tyi5rTw/eVGP0/67D7cv2zZJwmHTM7WYV38WenbqLzrp1hsyH9UO2FM2wdV347O2oLi4A4izhpVaRZI6e1cCGc0i8n3+vbG3KeoXvIcT9E8H2f+t/7CRRQYxTgrUQGuteHRR6T0Ck8SPQAYBD8BWm9X+uzNtT2Ddu6EQDqCfkSg9tzY1BVMEfWI+GjErgE zkcwx/Ki vbA2giy40XJQ1BwYLD2jpBTE3EUL4dOgF2UCSqNChN+X+TUE1zmul1z77UQofjrxLpC0akXXgaHJuRFNRJPdru0+szDzWI+bCWJKQ3p/MXKSCEAFqjd4OEijkLVmYenitpzM7cASXN+HmaeuJD7xD8IhnC1vZ/a3nbz+J0MbG8Sqic9XgjZjl+cA6OwUJ/45U645Sm41KT9BH931gfbvcPGD7fZflbsthRCT0Spgp5+F8vLkhApB1V9hYky4nvYwSTjrKVl9gQu6L6X9tu1h7afn1wr1HKgJ3cAiWYR+K8WNnWfl5NqTe32qHDwexll2R5VT0Agenh0uEWq5ka1ulhmT1jpUaq79A+UIMbFTs/CYscG/rqQsy7z1q34Nn+IcrcSmM5KzeymtXHdHXCHvedlV87CHOuV1LecLs0Gyx2pJcZhPlZMlei7EMrzdafRrDup+me4oq8SglTobzJX7RvtHGDg/Gcf9OlbA+fq/z0xA5LzqwIGyCGCqun2a/xQdNNDEpPJUD9Jq87A1fPWK7Wbhsho3vjSBeRwRPGHiw581cy55NN93Qoob4ykqoN7/fVxMrAEkwsijBJ90= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Adding documentation on shadow stack for user mode on riscv and kernel interfaces exposed so that user tasks can enable it. Signed-off-by: Deepak Gupta --- Documentation/arch/riscv/index.rst | 1 + Documentation/arch/riscv/zicfiss.rst | 176 +++++++++++++++++++++++++++++++++++ 2 files changed, 177 insertions(+) diff --git a/Documentation/arch/riscv/index.rst b/Documentation/arch/riscv/index.rst index be7237b69682..e240eb0ceb70 100644 --- a/Documentation/arch/riscv/index.rst +++ b/Documentation/arch/riscv/index.rst @@ -15,6 +15,7 @@ RISC-V architecture vector cmodx zicfilp + zicfiss features diff --git a/Documentation/arch/riscv/zicfiss.rst b/Documentation/arch/riscv/zicfiss.rst new file mode 100644 index 000000000000..5ba389f15b3f --- /dev/null +++ b/Documentation/arch/riscv/zicfiss.rst @@ -0,0 +1,176 @@ +.. SPDX-License-Identifier: GPL-2.0 + +:Author: Deepak Gupta +:Date: 12 January 2024 + +========================================================= +Shadow stack to protect function returns on RISC-V Linux +========================================================= + +This document briefly describes the interface provided to userspace by Linux +to enable shadow stack for user mode applications on RISV-V + +1. Feature Overview +-------------------- + +Memory corruption issues usually result in to crashes, however when in hands of +an adversary and if used creatively can result into variety security issues. + +One of those security issues can be code re-use attacks on program where +adversary can use corrupt return addresses present on stack and chain them +together to perform return oriented programming (ROP) and thus compromising +control flow integrity (CFI) of the program. + +Return addresses live on stack and thus in read-write memory and thus are +susceptible to corruption and allows an adversary to reach any program counter +(PC) in address space. On RISC-V ``zicfiss`` extension provides an alternate +stack termed as shadow stack on which return addresses can be safely placed in +prolog of the function and retrieved in epilog. ``zicfiss`` extension makes +following changes: + +- PTE encodings for shadow stack virtual memory + An earlier reserved encoding in first stage translation i.e. + PTE.R=0, PTE.W=1, PTE.X=0 becomes PTE encoding for shadow stack pages. + +- ``sspush x1/x5`` instruction pushes (stores) ``x1/x5`` to shadow stack. + +- ``sspopchk x1/x5`` instruction pops (loads) from shadow stack and compares + with ``x1/x5`` and if un-equal, CPU raises ``software check exception`` with + ``*tval = 3`` + +Compiler toolchain makes sure that function prologue have ``sspush x1/x5`` to +save return address on shadow stack in addition to regular stack. Similarly +function epilogs have ``ld x5, offset(x2)`` followed by ``sspopchk x5`` to +ensure that popped value from regular stack matches with popped value from +shadow stack. + +2. Shadow stack protections and linux memory manager +----------------------------------------------------- + +As mentioned earlier, shadow stack get new page table encodings and thus have +some special properties assigned to them and instructions that operate on them +as below: + +- Regular stores to shadow stack memory raises access store faults. This way + shadow stack memory is protected from stray inadvertant writes. + +- Regular loads to shadow stack memory are allowed. This allows stack trace + utilities or backtrace functions to read true callstack (not tampered). + +- Only shadow stack instructions can generate shadow stack load or shadow stack + store. + +- Shadow stack load / shadow stack store on read-only memory raises AMO/store + page fault. Thus both ``sspush x1/x5`` and ``sspopchk x1/x5`` will raise AMO/ + store page fault. This simplies COW handling in kernel During fork, kernel + can convert shadow stack pages into read-only memory (as it does for regular + read-write memory) and as soon as subsequent ``sspush`` or ``sspopchk`` in + userspace is encountered, then kernel can perform COW. + +- Shadow stack load / shadow stack store on read-write, read-write-execute + memory raises an access fault. This is a fatal condition because shadow stack + should never be operating on read-write, read-write-execute memory. + +3. ELF and psABI +----------------- + +Toolchain sets up :c:macro:`GNU_PROPERTY_RISCV_FEATURE_1_BCFI` for property +:c:macro:`GNU_PROPERTY_RISCV_FEATURE_1_AND` in notes section of the object file. + +4. Linux enabling +------------------ + +User space programs can have multiple shared objects loaded in its address space +and it's a difficult task to make sure all the dependencies have been compiled +with support of shadow stack. Thus it's left to dynamic loader to enable +shadow stack for the program. + +5. prctl() enabling +-------------------- + +:c:macro:`PR_SET_SHADOW_STACK_STATUS` / :c:macro:`PR_GET_SHADOW_STACK_STATUS` / +:c:macro:`PR_LOCK_SHADOW_STACK_STATUS` are three prctls added to manage shadow +stack enabling for tasks. prctls are arch agnostic and returns -EINVAL on other +arches. + +* prctl(PR_SET_SHADOW_STACK_STATUS, unsigned long arg) + +If arg1 :c:macro:`PR_SHADOW_STACK_ENABLE` and if CPU supports ``zicfiss`` then +kernel will enable shadow stack for the task. Dynamic loader can issue this +:c:macro:`prctl` once it has determined that all the objects loaded in address +space have support for shadow stack. Additionally if there is a +:c:macro:`dlopen` to an object which wasn't compiled with ``zicfiss``, dynamic +loader can issue this prctl with arg1 set to 0 (i.e. +:c:macro:`PR_SHADOW_STACK_ENABLE` being clear) + +* prctl(PR_GET_SHADOW_STACK_STATUS, unsigned long *arg) + +Returns current status of indirect branch tracking. If enabled it'll return +:c:macro:`PR_SHADOW_STACK_ENABLE`. + +* prctl(PR_LOCK_SHADOW_STACK_STATUS, unsigned long arg) + +Locks current status of shadow stack enabling on the task. User space may want +to run with strict security posture and wouldn't want loading of objects +without ``zicfiss`` support in it and thus would want to disallow disabling of +shadow stack on current task. In that case user space can use this prctl to +lock current settings. + +5. violations related to returns with shadow stack enabled +----------------------------------------------------------- + +Pertaining to shadow stack, CPU raises software check exception in following +condition: + +- On execution of ``sspopchk x1/x5``, ``x1/x5`` didn't match top of shadow + stack. If mismatch happens then cpu does ``*tval = 3`` and raise software + check exception. + +Linux kernel will treat this as :c:macro:`SIGSEV`` with code = +:c:macro:`SEGV_CPERR` and follow normal course of signal delivery. + +6. Shadow stack tokens +----------------------- +Regular stores on shadow stacks are not allowed and thus can't be tampered +with via arbitrary stray writes due to bugs. Method of pivoting / switching to +shadow stack is simply writing to csr ``CSR_SSP`` changes active shadow stack. +This can be problematic because usually value to be written to ``CSR_SSP`` will +be loaded somewhere in writeable memory and thus allows an adversary to +corruption bug in software to pivot to an any address in shadow stack range. +Shadow stack tokens can help mitigate this problem by making sure that: + +- When software is switching away from a shadow stack, shadow stack pointer + should be saved on shadow stack itself and call it ``shadow stack token`` + +- When software is switching to a shadow stack, it should read the + ``shadow stack token`` from shadow stack pointer and verify that + ``shadow stack token`` itself is pointer to shadow stack itself. + +- Once the token verification is done, software can perform the write to + ``CSR_SSP`` to switch shadow stack. + +Here software can be user mode task runtime itself which is managing various +contexts as part of single thread. Software can be kernel as well when kernel +has to deliver a signal to user task and must save shadow stack pointer. Kernel +can perform similar procedure by saving a token on user shadow stack itself. +This way whenever :c:macro:`sigreturn` happens, kernel can read the token and +verify the token and then switch to shadow stack. Using this mechanism, kernel +helps user task so that any corruption issue in user task is not exploited by +adversary by arbitrarily using :c:macro:`sigreturn`. Adversary will have to +make sure that there is a ``shadow stack token`` in addition to invoking +:c:macro:`sigreturn` + +7. Signal shadow stack +----------------------- +Following structure has been added to sigcontext for RISC-V:: + + struct __sc_riscv_cfi_state { + unsigned long ss_ptr; + }; + +As part of signal delivery, shadow stack token is saved on current shadow stack +itself and updated pointer is saved away in :c:macro:`ss_ptr` field in +:c:macro:`__sc_riscv_cfi_state` under :c:macro:`sigcontext`. Existing shadow +stack allocation is used for signal delivery. During :c:macro:`sigreturn`, +kernel will obtain :c:macro:`ss_ptr` from :c:macro:`sigcontext` and verify the +saved token on shadow stack itself and switch shadow stack. From patchwork Tue Oct 8 22:37:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13827116 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9CD39CF042B for ; Tue, 8 Oct 2024 22:39:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C999A6B00D6; Tue, 8 Oct 2024 18:39:23 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C4C4F6B00D7; Tue, 8 Oct 2024 18:39:23 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A4DFD6B00D8; Tue, 8 Oct 2024 18:39:23 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 78F4B6B00D6 for ; Tue, 8 Oct 2024 18:39:23 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 299EBA0CB6 for ; Tue, 8 Oct 2024 22:39:20 +0000 (UTC) X-FDA: 82651902606.17.53B8666 Received: from mail-pf1-f176.google.com (mail-pf1-f176.google.com [209.85.210.176]) by imf18.hostedemail.com (Postfix) with ESMTP id 33BD91C001D for ; Tue, 8 Oct 2024 22:39:21 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="fF/wrN1d"; spf=pass (imf18.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.176 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728427025; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=I90Q7zJCK83e81q4awX7MorQ6XlbKJy0BT8WfAURPyY=; b=THTv4DGrdYpzZRY2NXtxU71Yrc6rFGFAgucYGFIpcD1JNp9kscEwZwVI/Tp16dQS2X5kO9 tVEx8tW0ArVXitLcob3/l7TGc6HwJsQxyuHYOw8T76Xd3uyz6+unUPWCKgxv5A3qlGJyOb OQfcXUrIr+5X1UxWIvSth24WlvsY4Dk= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728427025; a=rsa-sha256; cv=none; b=aQk3+Iv2b53X0NJd7nv2QrOZiTNOm2Q1AbnBYsUVus01YkoKm263Kzb5hITG+hpHrC0qEy VPk3eQsgcQ1Y3uR3hAdChI/DqY069GeUzc5DMOxOsvhOp8yR7RYJkxdhTTJB/sYSqgTr2E nizYEJTJ9BbR5zdXnhTP7UJSMnUmMQA= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="fF/wrN1d"; spf=pass (imf18.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.176 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pf1-f176.google.com with SMTP id d2e1a72fcca58-71e05198d1dso1809289b3a.1 for ; Tue, 08 Oct 2024 15:39:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1728427160; x=1729031960; darn=kvack.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=I90Q7zJCK83e81q4awX7MorQ6XlbKJy0BT8WfAURPyY=; b=fF/wrN1de/jQvt70d2nkg7ZjULFkpKKJkqs8MMliOEEbMlbYQDOqfw7d4q+abBEMPI QMs4BGMY6lxVAWrCtnLqu+lzEtI4OXZLj4OkZWdKzqVFFU26GAxWWyOJTDzRpeL0pj5S EOXFe0xxKwdUy1j7ToomTKDY+omZYUyfsoVHbckjk/JfsmnSyokDidHFOb0ou2GT/dgL ECSC+fxSJvgimyyiI0CCHUwX4idVTSxWCEo18N7/4MY9Tpc9Qc3PUzjUS6LcpIGTaAUe FYQJp2K2Jx1zc7FTHRTd03Fs1oucemCe/EXlTpuQoFcSG2/xB3IZX6eHkZFq3056saNe 3+MQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728427160; x=1729031960; 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=I90Q7zJCK83e81q4awX7MorQ6XlbKJy0BT8WfAURPyY=; b=jyLVPI3W1pCxO1cFS1hwl/Gf7+uNDIyGAcJ5abdhmSI/p1CmxccejL9Dg2Ll+5evkc ajeIPqlOe32DZupV1EuCHtKMvVw0PkLHcp0RBTnYKeTGkCFForx7Icm7E9tcDTbz/Jto h0N8pXlfYAekDRyHEriAHFw7RgG8OJiFBTWsS08cFv7H9geFI+izw2w3aW7JCvBi6Q9Y WhmOjbosQBgZezVvaBui5IIYUV42R8ZRhIXH1/FMcKffdoK1U5cOrhFEKM79ev/f6kG3 2ETMX9ATCF2fC0OqjsJnP2S1z313G9VEAiMIiny9LdaJBCX2RowkvgDYpQqnD0+zmS++ +7wA== X-Forwarded-Encrypted: i=1; AJvYcCW7qeCCjSgSevuS92Xx9ukl2bn0Xx+lX3MLMpu+20/xTjIEgiAplHArs7OF8jEmYIkW3A/llgmX/A==@kvack.org X-Gm-Message-State: AOJu0YwyfnNq9Fd729xXfP/N/rBOaO9DBNQWt1PJOCN3DIgkiXWOahvH GpmDqCIybVzNquczvMhp/w3GKn+GVp7fmjuQsCFrsyqyWKHbYxrLZP7dLeLF538= X-Google-Smtp-Source: AGHT+IEbCDPXJ2G52awK8tnlat/iuEvp7PZGYyBu7JA39kKwuDr0Ciz/XeWbnF8LQymUFvQZ2UJ6Kw== X-Received: by 2002:a05:6a00:1ac9:b0:71d:fd40:b484 with SMTP id d2e1a72fcca58-71e1dbc7b3amr628154b3a.24.1728427159803; Tue, 08 Oct 2024 15:39:19 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0ccc4b2sm6591270b3a.45.2024.10.08.15.39.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2024 15:39:19 -0700 (PDT) From: Deepak Gupta Date: Tue, 08 Oct 2024 15:37:15 -0700 Subject: [PATCH v6 33/33] kselftest/riscv: kselftest for user mode cfi MIME-Version: 1.0 Message-Id: <20241008-v5_user_cfi_series-v6-33-60d9fe073f37@rivosinc.com> References: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@rivosinc.com> In-Reply-To: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@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 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-Stat-Signature: ire8r4grjgrdru4488jmaibzbh5ygz9i X-Rspamd-Queue-Id: 33BD91C001D X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1728427161-317853 X-HE-Meta: U2FsdGVkX1+B/q24mT3W00ipHzyVwdGSYIz/KfZOgKRAZc848DBp0QjyNiblA8vUmTjd1kWbNY7VFNhlGTYZtRhqHodS2TZv081WLyhdWXeomSyTzmhuI6Va/qMAeaFrs3TU0Up+HmFkenFOMvo+3BVKaLfbeyz9KIm3lxqdkVxKDltNWShQnumuINDSXaDzhTjHmDmVnMXzXvYZrg8DF7iRLHS5laqPDd4dWeHv8sIsisKOxwBpF3GPrcXcezljFu7OqzRrFBBBsPU/5EKOrIAsaIhYVL7zL/q2Ozjo2EstuHUHkMgbJ5cgRjn1BqEro+zcjJX3byB9TL9Rdbndrds+a7hUuZcPBzyxAtCh211WvQGN5k29OZLzg6VQwV6iFVXrdrh3bb6KxAmyvrGY8BXVwbYfliRjpwCmBxMEMlgTR8AZipw6ydBvJlAC7S+3klZ0dtnXtODxwR4lAOgtNj+w9o91btP+gykTlirNAJjkXPxLwzfqkrijZlGWtlLbaD/EyRok1zu/xRP1J+vBK4xeP6u/9lhwjA4IURYa8pofhlAnSAlIOtHwiqd4NKq7tsrKUYsS3astWCM/A53Xf8WNa5MykYQqooitOu913NpskGL4SpMy4ZOF9imrQUbg94j36QUhppd00eP7QEpatfwSD1GxUFr2ejJ95lD6jsCMWZZGeQMK/66bywfdqTIHACueeFzPWyoyDCY0rgwL7xoBwZKAuCMrIsymb0CXBGMn0R+QkD6BdBgifCfecCg2TYL/iY8miNpyPHWhngSirAOgasE528Yw8cGIAD9peNUnbIqeCGbDoqMcXsQBYOGbkBfsTCjSVhWQUXd5n8sMDMtHdyfVySRG5KGpB89K/74vUbLeKL6btA99T7mmAoocOue6knrQdzjV7aUyg0RS9+2uR1Kk04ObMKy9kIqIyLm0RgmJmyVxuOFOGFeISY0fW+hzYBLhvUDOTTNwZf+ MBlKd2Fi 1IqpQMO1ij5KQTPxLoKXEaBehY1KD10P+2AgDRebt0fRT3VDI1WY1JZcjYQqe1RMxrzyrhXX+Ex70JP2bYsZEyVJZuUDnB5+qH3ox4OqX+suJ1SrD6Ya1QBkc338NBW6SsMePO5OZ5wSD4RXGHCIZMQOBEUtbt4jYLEY94GgTzyHEzUiCeekgLCgDXhALdc/HyH0hXqdkZvwr8+BDPdZrdgVWQYxix3VPGiMSoXwR/5b/IFyElczVi4mCRxoPwNH6ADHm7cS9l64iwnrYZil1Cq8gmPgUl7QlNATFgI+Z6pcWqU8wudKixPd+IHY7Zeh+ewJqTwtWgWrK2/jBVw3+qhipnT3aIq3l46Jq3GIGVqcx5rbBVjikBilxmpOkaznrnihmgynD4UQab2S5+vL5IAoFDVOpjMWT0VCX8biuSWpu+kVatWdX+JSiQ680OVZdbkcAQxceJAYHVJB1LsJ24iLcSA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Adds kselftest for RISC-V control flow integrity implementation for user mode. There is not a lot going on in kernel for enabling landing pad for user mode. cfi selftest are intended to be compiled with zicfilp and zicfiss enabled compiler. Thus kselftest simply checks if landing pad and shadow stack for the binary and process are enabled or not. selftest then register a signal handler for SIGSEGV. Any control flow violation are reported as SIGSEGV with si_code = SEGV_CPERR. Test will fail on receiving any SEGV_CPERR. Shadow stack part has more changes in kernel and thus there are separate tests for that - Exercise `map_shadow_stack` syscall - `fork` test to make sure COW works for shadow stack pages - gup tests Kernel uses FOLL_FORCE when access happens to memory via /proc//mem. Not breaking that for shadow stack. - signal test. Make sure signal delivery results in token creation on shadow stack and consumes (and verifies) token on sigreturn - shadow stack protection test. attempts to write using regular store instruction on shadow stack memory must result in access faults Test outut ========== """ TAP version 13 1..5 This is to ensure shadow stack is indeed enabled and working This is to ensure shadow stack is indeed enabled and working ok 1 shstk fork test ok 2 map shadow stack syscall ok 3 shadow stack gup tests ok 4 shadow stack signal tests ok 5 memory protections of shadow stack memory """ Signed-off-by: Deepak Gupta --- tools/testing/selftests/riscv/Makefile | 2 +- tools/testing/selftests/riscv/cfi/.gitignore | 3 + tools/testing/selftests/riscv/cfi/Makefile | 10 + tools/testing/selftests/riscv/cfi/cfi_rv_test.h | 84 +++++ tools/testing/selftests/riscv/cfi/riscv_cfi_test.c | 78 +++++ tools/testing/selftests/riscv/cfi/shadowstack.c | 373 +++++++++++++++++++++ tools/testing/selftests/riscv/cfi/shadowstack.h | 37 ++ 7 files changed, 586 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/riscv/Makefile b/tools/testing/selftests/riscv/Makefile index 7ce03d832b64..6e142fe004ab 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 ?= hwprobe vector mm sigreturn +RISCV_SUBTARGETS ?= hwprobe vector mm sigreturn 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..0fefdc33f71e --- /dev/null +++ b/tools/testing/selftests/riscv/cfi/cfi_rv_test.h @@ -0,0 +1,84 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#ifndef SELFTEST_RISCV_CFI_H +#define SELFTEST_RISCV_CFI_H +#include +#include +#include "shadowstack.h" + +#define RISCV_CFI_SELFTEST_COUNT RISCV_SHADOW_STACK_TESTS + +#define CHILD_EXIT_CODE_SSWRITE 10 +#define CHILD_EXIT_CODE_SIG_TEST 11 + +#define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ +({ \ + register long _num __asm__ ("a7") = (num); \ + register long _arg1 __asm__ ("a0") = (long)(arg1); \ + register long _arg2 __asm__ ("a1") = (long)(arg2); \ + register long _arg3 __asm__ ("a2") = (long)(arg3); \ + register long _arg4 __asm__ ("a3") = (long)(arg4); \ + register long _arg5 __asm__ ("a4") = (long)(arg5); \ + \ + __asm__ volatile( \ + "ecall\n" \ + : "+r" \ + (_arg1) \ + : "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \ + "r"(_num) \ + : "memory", "cc" \ + ); \ + _arg1; \ +}) + +#define my_syscall3(num, arg1, arg2, arg3) \ +({ \ + register long _num __asm__ ("a7") = (num); \ + register long _arg1 __asm__ ("a0") = (long)(arg1); \ + register long _arg2 __asm__ ("a1") = (long)(arg2); \ + register long _arg3 __asm__ ("a2") = (long)(arg3); \ + \ + __asm__ volatile( \ + "ecall\n" \ + : "+r" (_arg1) \ + : "r"(_arg2), "r"(_arg3), \ + "r"(_num) \ + : "memory", "cc" \ + ); \ + _arg1; \ +}) + +#ifndef __NR_prctl +#define __NR_prctl 167 +#endif + +#ifndef __NR_map_shadow_stack +#define __NR_map_shadow_stack 453 +#endif + +#define CSR_SSP 0x011 + +#ifdef __ASSEMBLY__ +#define __ASM_STR(x) x +#else +#define __ASM_STR(x) #x +#endif + +#define csr_read(csr) \ +({ \ + register unsigned long __v; \ + __asm__ __volatile__ ("csrr %0, " __ASM_STR(csr) \ + : "=r" (__v) : \ + : "memory"); \ + __v; \ +}) + +#define csr_write(csr, val) \ +({ \ + unsigned long __v = (unsigned long) (val); \ + __asm__ __volatile__ ("csrw " __ASM_STR(csr) ", %0" \ + : : "rK" (__v) \ + : "memory"); \ +}) + +#endif diff --git a/tools/testing/selftests/riscv/cfi/riscv_cfi_test.c b/tools/testing/selftests/riscv/cfi/riscv_cfi_test.c new file mode 100644 index 000000000000..720a001f7c31 --- /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..9d5301914578 --- /dev/null +++ b/tools/testing/selftests/riscv/cfi/shadowstack.c @@ -0,0 +1,373 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include "../../kselftest.h" +#include +#include +#include +#include +#include +#include "shadowstack.h" +#include "cfi_rv_test.h" + +/* do not optimize shadow stack related test functions */ +#pragma GCC push_options +#pragma GCC optimize("O0") + +void zar(void) +{ + unsigned long ssp = 0; + + ssp = csr_read(CSR_SSP); + ksft_print_msg("Spewing out shadow stack ptr: %lx\n" + " This is to ensure shadow stack is indeed enabled and working\n", + ssp); +} + +void bar(void) +{ + zar(); +} + +void foo(void) +{ + bar(); +} + +void zar_child(void) +{ + unsigned long ssp = 0; + + ssp = csr_read(CSR_SSP); + ksft_print_msg("Spewing out shadow stack ptr: %lx\n" + " This is to ensure shadow stack is indeed enabled and working\n", + ssp); +} + +void bar_child(void) +{ + zar_child(); +} + +void foo_child(void) +{ + bar_child(); +} + +typedef void (call_func_ptr)(void); +/* + * call couple of functions to test push pop. + */ +int shadow_stack_call_tests(call_func_ptr fn_ptr, bool parent) +{ + ksft_print_msg("Exercising 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\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("Write suceeded on shadow stack memory, shadow stack protection test" + " failed\n"); + return false; + } + + /* if child reached here, then fail */ + if (!pid) { + ksft_print_msg("Shadow stack protection test: 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 protection test: child wasn't signaled for write on" + " shadow stack\n"); + return false; + } + + ret = munmap(write_addr, SHADOW_STACK_ALLOC_SIZE); + if (ret) { + ksft_print_msg("Shadow stack protection test: munmap failed with error code %d\n", + ret); + return false; + } + + return true; +} + +#define SS_MAGIC_WRITE_VAL 0xbeefdead + +int gup_tests(int mem_fd, unsigned long *shdw_addr) +{ + unsigned long val = 0; + + lseek(mem_fd, (unsigned long)shdw_addr, SEEK_SET); + if (read(mem_fd, &val, sizeof(val)) < 0) { + ksft_print_msg("Reading shadow stack mem via gup failed\n"); + return 1; + } + + val = SS_MAGIC_WRITE_VAL; + lseek(mem_fd, (unsigned long)shdw_addr, SEEK_SET); + if (write(mem_fd, &val, sizeof(val)) < 0) { + ksft_print_msg("Writing shadow stack mem via gup failed\n"); + return 1; + } + + if (*shdw_addr != SS_MAGIC_WRITE_VAL) { + ksft_print_msg("GUP write to shadow stack memory failed\n"); + return 1; + } + + return 0; +} + +bool shadow_stack_gup_tests(unsigned long test_num, void *ctx) +{ + unsigned long shdw_addr = 0; + unsigned long *write_addr = NULL; + int fd = 0; + bool ret = false; + + ksft_print_msg("Exercising shadow stack gup tests\n"); + shdw_addr = my_syscall3(__NR_map_shadow_stack, NULL, SHADOW_STACK_ALLOC_SIZE, 0); + + if (((long) shdw_addr) <= 0) { + ksft_print_msg("map_shadow_stack failed with error code %d\n", (int) shdw_addr); + return false; + } + + write_addr = (unsigned long *) shdw_addr; + + fd = open("/proc/self/mem", O_RDWR); + if (fd == -1) + return false; + + if (gup_tests(fd, write_addr)) { + ksft_print_msg("gup tests failed\n"); + goto out; + } + + ret = true; +out: + if (shdw_addr && munmap(write_addr, SHADOW_STACK_ALLOC_SIZE)) { + ksft_print_msg("munmap failed with error code %d\n", ret); + ret = false; + } + + return ret; +} + +volatile bool break_loop; + +void sigusr1_handler(int signo) +{ + break_loop = true; +} + +bool sigusr1_signal_test(void) +{ + struct sigaction sa = {}; + + sa.sa_handler = sigusr1_handler; + sa.sa_flags = 0; + sigemptyset(&sa.sa_mask); + if (sigaction(SIGUSR1, &sa, NULL)) { + ksft_print_msg("Registering signal handler for SIGUSR1 failed\n"); + return false; + } + + return true; +} +/* + * shadow stack signal test. shadow stack must be enabled. + * register a signal, fork another thread which is waiting + * on signal. Send a signal from parent to child, verify + * that signal was received by child. If not test fails + */ +bool shadow_stack_signal_test(unsigned long test_num, void *ctx) +{ + int pid = 0, child_status = 0, ret = 0; + unsigned long ss_status = 0; + + ksft_print_msg("Exercising shadow stack signal test\n"); + + ret = my_syscall5(__NR_prctl, PR_GET_SHADOW_STACK_STATUS, &ss_status, 0, 0, 0); + if (ret) { + ksft_print_msg("Shadow stack get status prctl failed with errorcode %d\n", ret); + return false; + } + + if (!(ss_status & PR_SHADOW_STACK_ENABLE)) + ksft_print_msg("Shadow stack is not enabled, should be enabled via glibc\n"); + + /* this should be caught by signal handler and do an exit */ + if (!sigusr1_signal_test()) { + ksft_print_msg("Registering sigusr1 handler failed\n"); + exit(-1); + } + + pid = fork(); + + if (pid == -1) { + ksft_print_msg("Signal test: fork failed\n"); + goto out; + } + + if (pid == 0) { + while (!break_loop) + sleep(1); + + exit(11); + /* child shouldn't go beyond here */ + } + + /* send SIGUSR1 to child */ + kill(pid, SIGUSR1); + wait(&child_status); + +out: + + return (WIFEXITED(child_status) && + WEXITSTATUS(child_status) == 11); +} + +int execute_shadow_stack_tests(void) +{ + int ret = 0; + unsigned long test_count = 0; + unsigned long shstk_status = 0; + bool test_pass = false; + + ksft_print_msg("Executing RISC-V shadow stack self tests\n"); + ksft_set_plan(RISCV_SHADOW_STACK_TESTS); + + ret = my_syscall5(__NR_prctl, PR_GET_SHADOW_STACK_STATUS, &shstk_status, 0, 0, 0); + + if (ret != 0) + ksft_exit_fail_msg("Get shadow stack status failed with %d\n", ret); + + /* + * If we are here that means get shadow stack status succeeded and + * thus shadow stack support is baked in the kernel. + */ + while (test_count < ARRAY_SIZE(shstk_tests)) { + test_pass = (*shstk_tests[test_count].t_func)(test_count, NULL); + ksft_test_result(test_pass, shstk_tests[test_count].name); + test_count++; + } + + ksft_finished(); + + return 0; +} + +#pragma GCC pop_options diff --git a/tools/testing/selftests/riscv/cfi/shadowstack.h b/tools/testing/selftests/riscv/cfi/shadowstack.h new file mode 100644 index 000000000000..b43e74136a26 --- /dev/null +++ b/tools/testing/selftests/riscv/cfi/shadowstack.h @@ -0,0 +1,37 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#ifndef SELFTEST_SHADOWSTACK_TEST_H +#define SELFTEST_SHADOWSTACK_TEST_H +#include +#include + +/* + * a cfi test returns true for success or false for fail + * takes a number for test number to index into array and void pointer. + */ +typedef bool (*shstk_test_func)(unsigned long test_num, void *); + +struct shadow_stack_tests { + char *name; + shstk_test_func t_func; +}; + +bool shadow_stack_fork_test(unsigned long test_num, void *ctx); +bool shadow_stack_map_test(unsigned long test_num, void *ctx); +bool shadow_stack_protection_test(unsigned long test_num, void *ctx); +bool shadow_stack_gup_tests(unsigned long test_num, void *ctx); +bool shadow_stack_signal_test(unsigned long test_num, void *ctx); + +static struct shadow_stack_tests shstk_tests[] = { + { "shstk fork test\n", shadow_stack_fork_test }, + { "map shadow stack syscall\n", shadow_stack_map_test }, + { "shadow stack gup tests\n", shadow_stack_gup_tests }, + { "shadow stack signal tests\n", shadow_stack_signal_test}, + { "memory protections of shadow stack memory\n", shadow_stack_protection_test } +}; + +#define RISCV_SHADOW_STACK_TESTS ARRAY_SIZE(shstk_tests) + +int execute_shadow_stack_tests(void); + +#endif