From patchwork Mon Nov 11 20:53: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: 13871338 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 40136D3ABF5 for ; Mon, 11 Nov 2024 20:54:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AA7056B00BB; Mon, 11 Nov 2024 15:54:00 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A56916B00BC; Mon, 11 Nov 2024 15:54:00 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8823A6B00BD; Mon, 11 Nov 2024 15:54:00 -0500 (EST) 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 612706B00BB for ; Mon, 11 Nov 2024 15:54:00 -0500 (EST) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 015F5A1B02 for ; Mon, 11 Nov 2024 20:53:59 +0000 (UTC) X-FDA: 82775015442.04.AC272CB Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) by imf20.hostedemail.com (Postfix) with ESMTP id 1C6EA1C0011 for ; Mon, 11 Nov 2024 20:53:06 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=103CRVDf; spf=pass (imf20.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.181 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=1731358296; 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=Ziaktb89WDbwUZ535GFbmTELWOQHyNOPVHZfKRHAH5o=; b=OxO0ZxXEJrXwayrVV021LYGJioogq8orAkuAlkmtjGEQG6UBWpfjGs63Bs6eb1ErKCPWXR WRaHDiZQpV2mozCwgPqkdr2sQkGrKDgPM9zcq3mfMnQpXcBswR67XE5BjiDqKZxVmIR6Jl TYR664hQFe6Y5gzvXJ4HCgWfdaYlGFg= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731358296; a=rsa-sha256; cv=none; b=c+G8gtePMJIX61A0vedBObq4QvUwAlx01cyNwQMpeH0vp/yHX/8wiF8ybsdaE0syzmQvm0 GH/YYt4jB4GaWuTqHQO64X7C3RA2tbwExhUzITRjlsm8zKpNqNfobIHhTfUs/enzc2TNGt h59xwJBWDlkH+bnCgb8lnsCg1jKDOxA= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=103CRVDf; spf=pass (imf20.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.181 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-2114214c63eso40990195ad.3 for ; Mon, 11 Nov 2024 12:53:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1731358437; x=1731963237; 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=Ziaktb89WDbwUZ535GFbmTELWOQHyNOPVHZfKRHAH5o=; b=103CRVDfjsNiUoldoOMOW88ufdELTs0JjbfFgy46HwEo20om4r/9FpHFIBJLtuRshd S+tHrO/3+3Cb7YSoVPJL2B1pGWjHPhRMUy6XV4TC3ax80xS5dEtM/5b+FU6iVHR4xmYa UTv4QfTXThW8g0DPKh9P6pGGiG+DmCdWM9gCbNlvz8e0oBHwY0aYJXJh675SUoUH/ZuM 1IMSIskN2JKC3SUCGVSrevCPJmto8gHnkXw6lqSbfdRkk2FbR2AvUtwHBuokUqToxjkh C7fWmKlEipojJG+ELdp1DfDiKkbQ1AVu4xN3x2nHvNWi2lm+w53f/7bFpiM94VmD9i3N MdTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731358437; x=1731963237; 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=Ziaktb89WDbwUZ535GFbmTELWOQHyNOPVHZfKRHAH5o=; b=dVmjUfDGuwS8vVzHRC8oTG/FAqs6paLY4NKWGY21vtaoKNfLcxSd732Pofmtjd9ISE O3ZY7UFUfoAMHUZT/HS/E2JsQ1lZQ13qM5oozRba2xKdrvhIbNhKXTxfEx6CXNEySF6h qxcCnNj5GS1VQ9vzNaZhPNRJncBPuT2vUMIhYTEsPBk+v44WjBLepWGfVg12PB+JYHm3 DvdSf2hjHg4xy/hUKaEulhh5yqAbuMgqv2G2TU9CT95mJhrBevoOw7YtqDJF0r8DW5RR XCVRYlO3V+XBqZaccLhDovTHnHeh73eKZGgZSHw+h5o+XSub2Chehuc76r2AmxzTfDRz yt/A== X-Forwarded-Encrypted: i=1; AJvYcCUZVbLeAOjcYhiuUueNH5UfozR9/xw8Gjs6l5uzXqI4OwgleeKODIxUoEkFQiEchHuf+X7biURd2A==@kvack.org X-Gm-Message-State: AOJu0YyERM5jf1hJf1Gyx6/lXzbDvhnu+bRG+01poMjcStFbPeyskcsg iF95kS8qcq7ruEQaE8Zvi7rLFpTU/nELpQGxG6NGLBwbUTZbleiFUqsXAvPRScQ= X-Google-Smtp-Source: AGHT+IEyGabgVA75UQcsueBpJ7RpOtRzKc95ETC4lvjZRAohKnXlL6Ozkv/pWiQeL+cJUAE6GKxHGg== X-Received: by 2002:a17:902:db0b:b0:20b:5b16:b16b with SMTP id d9443c01a7336-2118359cb22mr204360415ad.36.1731358436616; Mon, 11 Nov 2024 12:53:56 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e9a5fd1534sm9059974a91.42.2024.11.11.12.53.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Nov 2024 12:53:56 -0800 (PST) From: Deepak Gupta Date: Mon, 11 Nov 2024 12:53:46 -0800 Subject: [PATCH v8 01/29] mm: Introduce ARCH_HAS_USER_SHADOW_STACK MIME-Version: 1.0 Message-Id: <20241111-v5_user_cfi_series-v8-1-dce14aa30207@rivosinc.com> References: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@rivosinc.com> In-Reply-To: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@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, David Hildenbrand , Deepak Gupta , Carlos Bilbao X-Mailer: b4 0.14.0 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 1C6EA1C0011 X-Stat-Signature: qd9y83c9eix5oom7m9azh84p7e5dbzpi X-HE-Tag: 1731358386-546463 X-HE-Meta: U2FsdGVkX1+OJCipooLt6T0MycTVMtjjwzBaOj3jmN62qWTU/zQkIqAGy9G4XOzP8WUu2wa2uyTQr55XVWwur1Z5DEpjQSNDLCa74hPukhVJTCxymRJiiWeH+pOjsBxMep60DXX3gPT//ysjYQjaZDiGQzt7y4heoViTHD5UNJeLEH/OEKIWJOQooZnn8xLKim1Z8P1Le/kfDsPCfRLNdxqxnbp1cO4e/+mzo/O4IOwNH84i/0vHer1Z4R31k1EOvM7iCb8ppNW6w8fvLpNQ/8whKlY3XUu72cv4DNTLKqrFYOCobErtXXlbM4rQKuHAoItzPFyqIBMfSfcUxh3kLEMHphPYg9NSiFkFD3AgL+ySZjIJ7OIt/mQXwF46Qm3HE69t+07BtMZSkaq5KMFTu3C3iBjNyKXxqqRe+Atz6ZBKQYY+JolwiW0FJ3WJw/fZP44xEcmEDPcLdtpeDk7lnnERXTxXLG1f6jZN7jqGf9TJhYZZnGt+QMXD+oETP+JktxBmNLanvVz+Vfkqp9HieWI2WTrtv8VvxiD+bvi6l1Uhs58qqaxkDVY8DH5+OoB+00CSUxOQmGwJ5PJVA3ytmi5MMQwlJm1S3HuhOL7w+YcjPu78SIZ8XcLWYTxmzbIZ1aISJqkJJC6J+altp24oDN6RTyZT2rJfVbZuRawoqbI/IfIiESQVJ5p70ey/+V8hm+cCKM2m3qDZBOpZQ7MD2rVXibR5gfZvRk77Kfjf+edmIUJtVXcSqaBFjDELgoNafftyh9F7H3IkV14uTLD2wgxzlMnleiTcOHtM5tGdlz30uDRbpvcojFKdrhf1Y6u96DLubQ7A1SffLlJqkxe2yJ+XdvDib0bTGB4GKyJFmkHXj/CxkjnWkf6MtVayexJZ+yLC1YfzuYVdmddozLA7YtF0qZRztr5NMby+XVRN7j7FrBf7Ii0moG8r3aNDvv8LtLlPJ5ok+vsnBeEhR14 fLOIX8jm EHXGYfN9vwrltYrNamAfqQn1BmO4fRufwrFCGjy13ymziKMCSKoUUQ+vttd40fcSG0Ww3sOyymlTORUnlLFCTxcnKsmIt3PKyhbBUj8KjZiO2WIniiQ/bpcCEiP/WBlzgdxj9aHl2XD52wsshHrKndBXkF4uDos7iAwao3o/EcsumkE9sfXC0zxlPNCd7Cc58cqcn1VA+OBSg7nGNZDGn8LRrFuzWvUezxo6p1VQrBj5THYPVUwI1K6HnH7GgxryAM4syfk2hdntPTOohjieaoHhHYDSm9/gv4BWBNVHqVrXSEAJdQEwoTQEQDMyEwfydkjo6b0kWjOreHl16g6X5JgGr0jqJ2S83hpvD0ZlQ/K9VZQ55FInsKtIQVUaBOvJP5Pptikrz6342Z5H7r9tRF3vt/OTYyrH/TmSvKVLbinOf17ojZYt+HIBE6RBBvkgc1PzhoPtrNmga2t8iRreNfqKEVMbtjZpWqSRnvmSp0WTIYHR8wswC1utl5HlY8X8tcG/h2Jah/YC/QcBsnk6K9PLPGe/xiG+vZ6jb/RJb1ggVHb/lnMvGyUTXcVM4cntreNiQNzvnZdmN1uXpPOZBRcgelBNx+NTeNMdU 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 16354dfa6d96..fe6e9ece64b9 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 e52bd96137a6..f57ea9b308bb 100644 --- a/fs/proc/task_mmu.c +++ b/fs/proc/task_mmu.c @@ -978,7 +978,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 61fff5d34ed5..2291c02c74d2 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 Mon Nov 11 20:53: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: 13871339 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 C2102D3ABF5 for ; Mon, 11 Nov 2024 20:54:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 54F596B0082; Mon, 11 Nov 2024 15:54:03 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4D7E46B009B; Mon, 11 Nov 2024 15:54:03 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 305C86B00A6; Mon, 11 Nov 2024 15:54:03 -0500 (EST) 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 0620F6B0082 for ; Mon, 11 Nov 2024 15:54:03 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id ADCAD121AD9 for ; Mon, 11 Nov 2024 20:54:02 +0000 (UTC) X-FDA: 82775013468.17.19BC942 Received: from mail-pj1-f52.google.com (mail-pj1-f52.google.com [209.85.216.52]) by imf18.hostedemail.com (Postfix) with ESMTP id A49931C0010 for ; Mon, 11 Nov 2024 20:53:42 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=oX3+3LmW; spf=pass (imf18.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.52 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=1731358353; 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=/atH3Dmc2hDWuHIRnNBwkfWUhbP1w3P23HyX8egA5iY=; b=62jH+EjZ6ABBFSeTu43bkSNZd0jkw86Pxbz6PV8N7hwdt5U7u2Dz6m1UcQSZMG/vVgzZ4M 9rUzuCV7vAKP4NTs7YcPhxiE/hlaiEGSY6OyV41XOgvRDS3vqWTnsqyantjA50CO+lenRK qReaHCfPMk616D3auPMgySjiwJBWbK8= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731358353; a=rsa-sha256; cv=none; b=4t60Cz/fhlQgtrrHblPSxRnQkOMFmLpgM5zbRFwO/jnSsLZL7eZvWpMGFGdpzcOLg4usF1 uS7846ayiFtumcvvVVcvEUp+0f0vPWgUtU8hyh64x17vGawT5MwYwvkAgbnrTYqHsIw3Zs 3usSX/j3VjAGkuUh0lmqQnjPRhMRSzo= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=oX3+3LmW; spf=pass (imf18.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.52 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pj1-f52.google.com with SMTP id 98e67ed59e1d1-2e2c6bc4840so3927830a91.2 for ; Mon, 11 Nov 2024 12:54:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1731358439; x=1731963239; 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=/atH3Dmc2hDWuHIRnNBwkfWUhbP1w3P23HyX8egA5iY=; b=oX3+3LmWo4X1r2AYtTleRJYnr/7h/WRZ3TfyuzYX2+Ke0SIIk4VyXgicQJ9Shu+QEz X/o5B5ga5CldLHFQmpmVurDa1qo00FzlWjFL2dVXpktGtMwr4ULyD2fM8DxFveWGBzPH 4dLYsAFQl6QxOg6vWLFY56XOWixxTYS3YkLo1dLwE+fwGqwVM3oaJds6+o5piaK2zDlv NjczMI3cDWGZnM8r+lLJ9HI8RYQAWo7s1z6CxJJHCnaeN6/cmQH+8rQOTyepaxUxwS0J Mu04CEEv2ICZZveOfYPPCQYigJxi6T9pRqke7e9vUdy/xqrUSEGWK2JTXGaJl9vZ4ANV Dlmg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731358439; x=1731963239; 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=/atH3Dmc2hDWuHIRnNBwkfWUhbP1w3P23HyX8egA5iY=; b=UsFpmGvAWWwC2pWnoCTJBtTNaFCwuSqgSibIblfMCfez5afxEp3tTmB3ZRvJdaXg8/ XV/tx0wLKu4Wn6c/HTrjV0lDTfrGQUjXQVodfzUbbX+wVdDV5YL4qMmfigaRcHq1+6lX 2ZwUMAkXVNuoUKNpzuQDW6g4r/bbXDcbWOrdf51CnwmHcSzfZ8vt4Pc9OixH8JzJWzQk j49nZnJbWgcD8in3xEy15DslU2y6ZKBrb8J+JDtZRykEBW4f8FwHqu/njQn56sQGhVdf KlQlFTvHw+/OASQzcps57AetbrBFto5vfjSPsD9ia2s70AYbraUEAZYYSrBbp3v8zP61 sXJA== X-Forwarded-Encrypted: i=1; AJvYcCUueAOo96GnI5TO4L0GdlRuHgu1uvVtNhTjatGM8/PcyMERHpO5+eYlzPeJBciJMVLJb7q16b69BA==@kvack.org X-Gm-Message-State: AOJu0YzxtArewRrspHXdviP7QKf3oVCQnHrmu2Yzhhfqs4LIb49/Bd5/ 07ZoIcn510IxdZweL1yE6C8j1uopGUyH9JqxCjRfo6ELFtH6hv5T2SBtdoKV2zI= X-Google-Smtp-Source: AGHT+IEvbaDX9G6RBqut4iScbl/I53KZSOyAqnYCopkb1nPpVHZJcEx6PnWjjFsFphBR/XrDfiSPvQ== X-Received: by 2002:a17:90b:2e07:b0:2e2:bd32:f60 with SMTP id 98e67ed59e1d1-2e9b1780a4bmr18569491a91.32.1731358439483; Mon, 11 Nov 2024 12:53:59 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e9a5fd1534sm9059974a91.42.2024.11.11.12.53.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Nov 2024 12:53:59 -0800 (PST) From: Deepak Gupta Date: Mon, 11 Nov 2024 12:53:47 -0800 Subject: [PATCH v8 02/29] mm: helper `is_shadow_stack_vma` to check shadow stack vma MIME-Version: 1.0 Message-Id: <20241111-v5_user_cfi_series-v8-2-dce14aa30207@rivosinc.com> References: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@rivosinc.com> In-Reply-To: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@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: rspam10 X-Stat-Signature: 1hfy48qt1xhfunr9fmh5mzu9f9nbc16q X-Rspamd-Queue-Id: A49931C0010 X-Rspam-User: X-HE-Tag: 1731358422-192927 X-HE-Meta: U2FsdGVkX1+Jtx3oRQxlKZ+xa/g5woRjA4OtTEKSeVX88QLFMS0AzCbaDD1iOp8G/HnG5HowVcdKWH9L8N8rrMhHfpCbjxC6BlwxsyM6nweSZakAkCv7ODSLTc4Xlcx0GM0whczH3gQpF6g27PQQ8Z8FQjBMHPZ9OYf4137dlvcZ56wT+8HcdGh2I7orawyhXKgLPUGE3YRXt/o2kTSSey5axQ5CJVoS645LqXbfDwQMNCbAghVAIoYeMkWZXyv6cQfBKUd4wlyc2r/MknhBjpmaCUexKwEjFlls9qWDlwmpKubZn5PYM8LxnjnulIg+OOva+BmVdkJpLAUQgVJfzMXvS+Ku297r85DCbhCOQjORNBtqsHcx+sewNzTyi+rITMgSHirUJ6UvyskWAmaXPgm3Foi16rf+SbUG0pJq4tmLKUAxo44h564xyhxGpsF4hGJ03/BezPhViYYphhv0LleXZnEsieFSoHQXvMxqYQaTJ2oFLhuU7+a5fbjuWdO5KMjkNebHRPdVWpX5wZpwubRViF5RorWLSyTY0DPDB8RI2XHGWCOlnEJNRMuqI9nGLEqeJfzGS06jKSfmlRidcg65qsOQYVflc0f7ZJeRZ7E/4FmeWnGukxbEQ1JNQEnH8s36By+ewR4AFVKDRjn4ICbeNQuWAuUEBkgwvfqoFhmT8qJeiNVmPlvRAGcqvnN9fWHa0/0noFV/EhesZN+9/X5iTNtpz8WFYAQlUjhansqpeMPjWg1tT44pl1B4T3Tbs+JuVfLG3hkvOabc+L9m0eQ/91hxeOJ3dvsgheXV51i0Rb6JXtccLTAIIJLKV0KZmpaV2c1zPCFoEfwi3g8wOyWGL+DV+YzjPt8ffvK2rAXaOAViMSuILAfJMLGvW8kL3LMMCqHnkjSdApI6x+MIEp49oBWUeEpMOI9hu8p88mMRf8WCS8JLFT6EssVKjy9vK4PTq9rybFv5RzH8nTn wdFVz+Y4 L49fhPhrY8QwZ1VcfUK0YoxeTEc5HiywA7ETbPq1bsw6IN4mtXXagkh9LZS0Cx4WEl7lDYnocfCaKZKtqc5WeinuLBDgvDOyRc2ixUbpnNf6W6GOMydq5MJU7QpxxDl6w7RSyqmIm/eY0pQYnJN0vYMu7V4l+7xOYlOfa/E24hI5mLgTUpCAF9rYXXJ5U2zy7wvzzTXVBhS1gKcP/octUg89PGXRgAGeCCcCR5ii652ra6aVdGK5JoTXTTxZU8jl3ntR/ArVieFeVggsShthCKAiGScOP9+hH6nOfrAhE2qhiosd8t2skafmhNFcOfYOcJqyjE1oVQ+XH1yOeaqWVQx4cAqL0vtoXGceA7YCvzlwUTNLbQBXlE3JrcZa/KKsLbUAra6KbkjpJoIWImJ+/rhr2mlif2rcjINcGlARNcMi8RWH7EO/kPz2SLecFnIA/425BUeQWbprvPf7SSyVpo8qOdZx3c7JyCusqxdWfKouuRzaCP0rVp+a2y5Thbr8a5FDaTEy434B5iNze5xzM8qDVFA== 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 Reviewed-by: Mark Brown --- mm/gup.c | 2 +- mm/mmap.c | 2 +- mm/vma.h | 10 +++++++--- 3 files changed, 9 insertions(+), 5 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/mmap.c b/mm/mmap.c index 9c0fb43064b5..f17573469c42 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -708,7 +708,7 @@ static unsigned long unmapped_area_topdown(struct vm_unmapped_area_info *info) */ static inline unsigned long stack_guard_placement(vm_flags_t vm_flags) { - if (vm_flags & VM_SHADOW_STACK) + if (is_shadow_stack_vma(vm_flags)) return PAGE_SIZE; return 0; 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 Mon Nov 11 20:53: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: 13871340 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 DDFB5D3ABF5 for ; Mon, 11 Nov 2024 20:54:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BDF346B00A6; Mon, 11 Nov 2024 15:54:05 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B693C6B00BC; Mon, 11 Nov 2024 15:54:05 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 96D856B00BD; Mon, 11 Nov 2024 15:54:05 -0500 (EST) 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 72BAE6B00A6 for ; Mon, 11 Nov 2024 15:54:05 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 3858EA1AD1 for ; Mon, 11 Nov 2024 20:54:05 +0000 (UTC) X-FDA: 82775014602.08.9D482EE Received: from mail-pj1-f50.google.com (mail-pj1-f50.google.com [209.85.216.50]) by imf20.hostedemail.com (Postfix) with ESMTP id 5CCB11C000E for ; Mon, 11 Nov 2024 20:53:12 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=WA5T0tZS; dmarc=none; spf=pass (imf20.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.50 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=1731358268; 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=WIZVW5kCW5Dqg3JTts8BKHfPCkqfunLzJ4lU/RN+S5A=; b=VwAbnNNqxuAuPkmFTwDA5TblloqAMetty/Wih0X/DHs/LJERcySFtoiBFDGqzF0do67QSJ JoLEVykFj3pA67ydAh0tRTBcQBhfOMpou6UDbY1TUKmPWs+76UGtJWl0nY+nS3kGjAkdJg MVC+/I+jJqdc5vjGjF3dthD1dgHHgX0= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=WA5T0tZS; dmarc=none; spf=pass (imf20.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.50 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731358268; a=rsa-sha256; cv=none; b=wRx31G/3B5m1F7fgJzqLyQFoeLY0iMMYcJzCVReRVJg3x7CLHMR8TqvkFgubscfBd1/26d PhrRCJi5vl5G7mqmJdmNm1AXFgQZkAp1YtSLxzsFLXdHQahMOLQ0k32qFYfFnUzVjyzpdz Yg5MZeSmDthrMogCn3MNndUeBTyy7aU= Received: by mail-pj1-f50.google.com with SMTP id 98e67ed59e1d1-2e3fca72a41so3942408a91.1 for ; Mon, 11 Nov 2024 12:54:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1731358442; x=1731963242; 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=WIZVW5kCW5Dqg3JTts8BKHfPCkqfunLzJ4lU/RN+S5A=; b=WA5T0tZSKtDhDVNLIPBPfHLKvznjhWdvDyUB/sVIdaiLutglwSJdJiwA115h8IphBZ jXBfjrRQ2+lCf5298twJAPG1XO6nVvv6LXnlROUstzqDuLEpoqlFqyiOh4qkTpSIeyHV QE4FLraqA42etreqRUu7vd9EVBEp8eb0IVVSpPUIlAfrLqRQjRP855Ia8vU5oEsbVvis OObsDo83PJ/3WHdi/gOoRST9ykalkWPYvLkgg88cTDcP+Hkv3Q9fai5TLw98W7OnytcI 6ptSQhA7ljrXU283N3IU8l+YjoXdhRyMUzG/v5hzaicH85BlTyjcr6dpZBYb1mKtn4ey Pq5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731358442; x=1731963242; 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=WIZVW5kCW5Dqg3JTts8BKHfPCkqfunLzJ4lU/RN+S5A=; b=dUpwWo4ga6kHMfp2r0icihFak7WTn8C7YQ5e4PydK0zxel976i3xcCkdFy3p/d2zBH 4mJYgun5fmDeuPULkSLsHszkeIgOoFJG+MHlLE8/iOdlbMtqpc1VUxv5s4aFfDSeD8C5 IzfEmw0fcqpSWaknF9t6eFupWyHa/fEdZ1OO6uCAYAdE6A0iuR/qTW2UYK7hTWjgIZNt Pe6xoyC2oq5s6FEiXC7V9k+vXWyxBfPLUy8tuG4OhP4c2ix14diVbfZpwTleRf7YBQSu hQ/qLHFM8qz/CXc60U2kcA76hHG/GNeVdJXtB4laxO6dGfJNoMwG1L4+QrBoY5tRVG46 dcCg== X-Forwarded-Encrypted: i=1; AJvYcCWpVUgqMTSw3b/scxVdZ8VvjilPTTifbRSjQvf+umHK5aFBna13aMQ2xC/lEEXrCAJYEn4EsDmTMQ==@kvack.org X-Gm-Message-State: AOJu0YwyO8F4K6WRS+8VvZcm3AkDQT43WiKosAykT+8LPMXAyRkPyszV h2tTQ2AjsGo7KjWDy9KhtM5yi59YlZDyl59971B8dTYsDGGSODroNAurieax5Eo= X-Google-Smtp-Source: AGHT+IGa2fbiAJuGCBnF9rSK98Eqsv5rxxJ/njsQk9gbKD4n0fa6MYIbIMWZY1h1CaKZlLTFD7l4AA== X-Received: by 2002:a17:90b:3941:b0:2e2:d239:84be with SMTP id 98e67ed59e1d1-2e9b166bc4dmr18469289a91.5.1731358442231; Mon, 11 Nov 2024 12:54:02 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e9a5fd1534sm9059974a91.42.2024.11.11.12.53.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Nov 2024 12:54:01 -0800 (PST) From: Deepak Gupta Date: Mon, 11 Nov 2024 12:53:48 -0800 Subject: [PATCH v8 03/29] dt-bindings: riscv: zicfilp and zicfiss in dt-bindings (extensions.yaml) MIME-Version: 1.0 Message-Id: <20241111-v5_user_cfi_series-v8-3-dce14aa30207@rivosinc.com> References: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@rivosinc.com> In-Reply-To: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@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: rspam04 X-Rspamd-Queue-Id: 5CCB11C000E X-Stat-Signature: b9jsphbmhhq6i4eg5qznbktzzqp841gi X-Rspam-User: X-HE-Tag: 1731358392-989361 X-HE-Meta: U2FsdGVkX1/zoAih10210diloMwEa9A5yHRGbPJ68SnucrhcHpgyrpkmgU6Hv0iIPY2cJTemFTaUeBrzyofFXpV2HKG/IzQjTK5z/0504IR5SdAiqsLFewbG7dfX9zp116RkA1sGAlaW+163q1onXlpF9CPPpQcwfPdwYYp9qyAjfoXr9CV14mqhcqN1Shlt/7RHNXn97P0+8aNU9acmps8K6blx3t5cyfJRdHEfGPt/SjpiDVTHznDGtx8AWxDwPBKiIBvO1w+K8LcmqED3fh5E8Rwh+dX1NuCGqysCBpMQE5O99fL2gXi7Jwtl52D1xc4C2VTL3ybMyXHP2G90WpbeCjtIdwCxvyIVU00eohoFityOLSBAOB0ctVdUHydbl3WIkkYi3hJhji/La7oFgSP4ALxzPRNX9mx759PggHIZsLSlnKh522Cb+IoUs17/XnzBtD4IDZZXeg/L8CKH+RmSupw23NCoOKKfJvvG1bWla1pQdqQcD9IeAC3dI8f85SlYGmogeqEDLH3LSP455p7HpxyAwUTaBPegej+zL2LMgiiMY3sfap0ZYiVIq7kwoiLFKrMAr3BHQQIQGTuwCicGJweSQ5MJ/OgJpNh4Ui58X0BnFdIjjkK9iFm6GJtVUI/gDrd3xxL07oYOWSwotWixgXU2LE2wPW+P4meclZlr0bQGPfk8eXIMZuuWrpAduv4EXAPzhEPXKu8WNOcVEp/tbZGU7w8OHg3WKYpRcJWXtsohBjkmu1rcm1qLLQanL1TlyrauUveRSoZb+JmkXoO31DDDLzJIhdDWywc1ovo8ofqoMswuyZVpT4mVhJOn7Qznbdxxuedh14Bu5KUyKhY3ZvLEx1ckMINbBtcP1uQVIFQoALeLzOAoPojS7LHVbxcR2w9GQfC7WuWHUKtPN2hF8ZnJVHPGwUFVboJSrkhqM7cHHqi7Iau0Tk2Y4rHKKBg6l+py53jkIFiwujx 9/LNDZpJ iqm23TPcGnllMVW8taBX54prkryY0B9OnNNjMjx6DeBJ/VbD6BF3hX1U4ntOmtbhsIgmrcUESDsm1Gum8XHotUxtCSVqf5EzI6jsdZD0JfN+/Pb0tZkoYk3DpHaD44YxDz5pWQ4GPAafiqc3vezl95agZcnwfhaMILLOs45VUWrlhG75TdXDNJsTuz57vllYi2Gic06cvIPShyrLUkC7iEcTPOXdt6I5k/nbaFgcg5NM8fP2S424Fd6ywYgXOwu6aB0iwDhXFcjLsbp9w4E3SAglORm371u2zV31DOSip/B9ygoco00keMZyZtk7lGF+EgfzF4VxjBJ4O4myOTUyNSlM7eM10gXHqDsyS4JbLnMEJjAa0wtTd71zH51ywoyqlrN6B2Bj2752SWOl6RFsymc42RnRzT8PhPNwzEHJlNbAaQh/oCNQxvExdwl9xAFPTNSMCYVgJEfT1m5aFtXu/9pZKzcdrem14f1+5lHRcTEUvmf8= 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 af7e5237b2c0..5fb40665fbaf 100644 --- a/Documentation/devicetree/bindings/riscv/extensions.yaml +++ b/Documentation/devicetree/bindings/riscv/extensions.yaml @@ -398,6 +398,20 @@ properties: The standard Zicboz extension for cache-block zeroing as ratified in commit 3dd606f ("Create cmobase-v1.0.pdf") of riscv-CMOs. + - const: zicfilp + description: | + The standard Zicfilp extension for enforcing forward edge + control-flow integrity as ratified in commit 3f8e450 ("merge + pull request #227 from ved-rivos/0709") of riscv-cfi + github repo. + + - const: zicfiss + description: | + The standard Zicfiss extension for enforcing backward edge + control-flow integrity as ratified in commit 3f8e450 ("merge + pull request #227 from ved-rivos/0709") of riscv-cfi + github repo. + - const: zicntr description: The standard Zicntr extension for base counters and timers, as From patchwork Mon Nov 11 20:53: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: 13871341 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 08DE9D3ABF5 for ; Mon, 11 Nov 2024 20:54:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E047E6B00BD; Mon, 11 Nov 2024 15:54:08 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D667D6B00BE; Mon, 11 Nov 2024 15:54:08 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BB9556B00BF; Mon, 11 Nov 2024 15:54:08 -0500 (EST) 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 893EB6B00BD for ; Mon, 11 Nov 2024 15:54:08 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 3F5B4ADB8C for ; Mon, 11 Nov 2024 20:54:08 +0000 (UTC) X-FDA: 82775015022.25.8258027 Received: from mail-pj1-f54.google.com (mail-pj1-f54.google.com [209.85.216.54]) by imf14.hostedemail.com (Postfix) with ESMTP id E5602100004 for ; Mon, 11 Nov 2024 20:53:20 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=s7ib8dEN; spf=pass (imf14.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.54 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=1731358214; 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=kCloQ3kpRaUIw71nS1Yef0RT6Cd+Bh0EPCOA/9JkyG8=; b=Pvrc6rvsYKw0xg1mVzYEbNNjWEd23jEpQsGNuRyB5eUSIS08rAt4kOiY29GTLuOGu2kmjI AKxxH9022rSBVmAc+QhKqp840YlSSIeOIt7VYTPE8WCRMCmyN7lNBZiqBYV5Ng+jLalVZj EVRRdzgrlcEYnNq6FJqF0YGfvA90T40= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=s7ib8dEN; spf=pass (imf14.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.54 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731358214; a=rsa-sha256; cv=none; b=0WOa12oZF0oZ1gltrpFiiXw8uWTTZj3ZdXpg6BId6DldJlNN5iervxyvdhDvT9xtpMcHh7 2Is0YpfcoOnJiYxK/TnYFjbsHhYqW6wUg+xuJ+AUSo5XqTWt8UDCSvwWIbdHtS4ucgM7zx d4gZHUFHilBVVn9Ptopj2OZ3tetbeoA= Received: by mail-pj1-f54.google.com with SMTP id 98e67ed59e1d1-2e9b4a4182dso2333389a91.0 for ; Mon, 11 Nov 2024 12:54:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1731358445; x=1731963245; 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=kCloQ3kpRaUIw71nS1Yef0RT6Cd+Bh0EPCOA/9JkyG8=; b=s7ib8dENKMp5Ect09ZoyvhgsRCNLBKXGTNn/3JpGMPaJfSrnSTyqtegjmf7MSHaEGU I3UJ+TdFL99o5BufsBh095DcsW81GlO+26Jt8dWyMEv0apRJvsfywKeNxlPRPwiBAwdE +dB1AV5AmnnSPur9TNMiuf2ysiMfxT8puBXUZGJZnFYCkiKD/+ubxozenWecS3u2QnWb 32zdTn/DWMbxHQf57J8FF11N33SSLMTTtgNzCQkPa8xxETojcc30ARkgEpIuGwswSq4j 3So22MYZOlRMUGKEq1ZVzgQIqzzWyZCBJ8KmrjDFiHse6HbVhxaIhmJK4HyVDxQwr8cr fsOA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731358445; x=1731963245; 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=kCloQ3kpRaUIw71nS1Yef0RT6Cd+Bh0EPCOA/9JkyG8=; b=xUVuEisGXVG/DrNtWpkJgk+OYaFMQ8f9V8Kjz6WKOdVEnXlYsi4gYEQAk/a2PkEVJQ kY4KJTyMu6akRZ1QCreqwQ191ZKa0o36rOzqq880LTi7qgnaAte3RrJQIRw/gvq8IuX6 PVzhVySxMQR+zVSa5qZhGfcc5lMraRA4RNK/iyrACp3Uiu0v/l7sU2Ohw5N9Htb9pwlX 9/zCo2al+R9MYp9MP4xxNzaBPGI0opep3ORAjFXI0tOFMCC/UpnaklMik1klotJMQa57 awhAq3sky1vE19QQYGvEJ7U4EDffgfZO6wTGGONJqBMeE6CvHAFt9cvlTL1GIUzTTOCv OQJA== X-Forwarded-Encrypted: i=1; AJvYcCVA1ge5DQLsXm8r4FXeHttIM4oNtMs3IzbayH1A06JOnCfCAhwBHHJiO4HzxOxyH/fRaTwqu8E9Xg==@kvack.org X-Gm-Message-State: AOJu0Yzw1ujH/xGGo57OSkLi+R2l34ChAmT6bmQjrxzlHOA4SCvgMz+I HkhvT4FVTu0Z1wdS1jivzcrcULAzD9dGNAheVeoBl+i837IAeB2jYuh4sPtH+3E= X-Google-Smtp-Source: AGHT+IEzLgiBrXYk8UpI73d5WjrE2IsuXIp2E2P6Y/oemnmdip5WKObNchwxhppbPh61kx0lDDr4mA== X-Received: by 2002:a17:90b:1bc3:b0:2e0:7b03:1908 with SMTP id 98e67ed59e1d1-2e9b0a57d33mr21146939a91.10.1731358445109; Mon, 11 Nov 2024 12:54:05 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e9a5fd1534sm9059974a91.42.2024.11.11.12.54.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Nov 2024 12:54:04 -0800 (PST) From: Deepak Gupta Date: Mon, 11 Nov 2024 12:53:49 -0800 Subject: [PATCH v8 04/29] riscv: zicfiss / zicfilp enumeration MIME-Version: 1.0 Message-Id: <20241111-v5_user_cfi_series-v8-4-dce14aa30207@rivosinc.com> References: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@rivosinc.com> In-Reply-To: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@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: E5602100004 X-Stat-Signature: cxsq7wtoh18ynbkyje7dxwp5xd1yo488 X-Rspam-User: X-HE-Tag: 1731358400-607875 X-HE-Meta: U2FsdGVkX18o6I/MkQtxjFVRxw407buGdfWNlhmvHgUOhwF2Mv5ixjMIpSRS0tU8/ZkIWe2fmVLKo9+DUfLL1vdDukNwzyw3eOzhdGAUa/UaoC+IRoiSZE0nxuDgINYQXPpybyI8/jrfbXGPsm/V58ohU9t5odwxsUDucpy94ObooOUsGqd5dX2Y0H4lzB0R6ZzFYh2O6FDKZFPfQxq+g+Z30MVzF55wjPmxDg1A3Q59catVCgkrDTulUGR3gNW8bC4l9Zgxp3V766CZSXNHtQybbHP8hp/viiQDW1kYSxGvbsdd/rRnLA4X3dtnMU5B6LEpVleWDH9bC/C6fpDGQVU/P9k5rjZfhwltalIeavUdHL2AS/oM/o8jAr7JPQQymZ9YkjjQPpaaUc3aUI2WygowlV9Scfw0cFN6N4byK3yPWnPAyQvB8LAEiKR4vSl62G03aeUmSz2yFWmo5dnuv4AEaJShgxizPWbTa5C/8vxyffRb/n/G5N4utiTX5rgG554xidoF3puyw662n31pMrXgKPLbFFzRY64g8IaWidEWzBvcrCVnIC4hIlBv3RPKyBnKI+ZVEX5yCOmAuesg13HemuV+KZ//7b5rXmCOhssmOcF6KZEqttEgUapoVt8yzsfWKHQd1t3yPlcxmeZS4YliyomEMYcfKEbC/h+x06t4OXWtrAqyzoaI4EjbufWeH3y8ziMk+7GsPvOWB8pM/Ian3ErdtQFC1CR9T6vS2ay5J1us9Ne6dAxBL7bfxoQG23EDgy/L0gnYnduBAjmgelEZVWG8v6O1tmraU4uB5F7LjX2G55YBzuIM6UVEUnW51SWvw3DgF4iOg/XNkT1LnixERW1ng+qeEfjGQLUMcXaw34wzAAWG1f+GYR3XJuEeOjSwMThzVidXDPtjD6XfEqlh4DmM51DbyAcTROUC2Q5SiqOosNDofozPY+lmrOA+j4qMU61JGxa6J52WT/8 DGzZRClj DUFflyawOoLqtIkUQAFKRd5objR3m5fE51QWgaMBBZFysCDchpGxX8GlyKEsdzEJn0Pfr8UAWBc/YwgdXFETjLjVVOTiKixBhCD6ZJvwctK2EHYMF+5WYJgcFGAfmywR/t6dRgYOdy+oQ4PIffKu+OqPyX3GLcFR3H/Ig//pevgo+AxO4qzU9JgulXhgCvjG6nsAZ6a//83WrNHcIvh+LDp7qwqlnaRvhJVOC/FhVf9GBPADPNyq26Oby3f15E8AZXMhH25xfdPx5g0Y2T9uW8G1CMLUiwqRTIASM2iQmD3GnaGbSJVitdh+zW51L9dNrS2S9+1J0PW96oQw15PBu80jXs1WK1PhNSn29wvfIivw6XpH2vUlC4hwRYOTty6QocoruMtyOIV0N5xsmUZVfG2m01g8EqMGFYmasry6o2B3Cn1RELgL5G0sM2Hkqe7kW8jj8mlxCglfkSgX/Z++8OruEuPDZHKtUX5pyEG2x2GRroYfjoN+FHaXSlg== 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 4bd054c54c21..8d12dc6c7d44 100644 --- a/arch/riscv/include/asm/cpufeature.h +++ b/arch/riscv/include/asm/cpufeature.h @@ -12,6 +12,7 @@ #include #include #include +#include #include #include @@ -135,4 +136,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 08d2a5697466..f81f62b32f72 100644 --- a/arch/riscv/include/asm/hwcap.h +++ b/arch/riscv/include/asm/hwcap.h @@ -98,6 +98,8 @@ #define RISCV_ISA_EXT_SSNPM 89 #define RISCV_ISA_EXT_ZABHA 90 #define RISCV_ISA_EXT_ZICCRSE 91 +#define RISCV_ISA_EXT_ZICFILP 92 +#define RISCV_ISA_EXT_ZICFISS 93 #define RISCV_ISA_EXT_XLINUXENVCFG 127 diff --git a/arch/riscv/include/asm/processor.h b/arch/riscv/include/asm/processor.h index 5f56eb9d114a..e3aba3336e63 100644 --- a/arch/riscv/include/asm/processor.h +++ b/arch/riscv/include/asm/processor.h @@ -13,6 +13,7 @@ #include #include +#include #define arch_get_mmap_end(addr, len, flags) \ ({ \ diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c index eb904ca64ad0..0f2c466a51ee 100644 --- a/arch/riscv/kernel/cpufeature.c +++ b/arch/riscv/kernel/cpufeature.c @@ -318,6 +318,8 @@ const struct riscv_isa_ext_data riscv_isa_ext[] = { __RISCV_ISA_EXT_SUPERSET_VALIDATE(zicboz, RISCV_ISA_EXT_ZICBOZ, riscv_xlinuxenvcfg_exts, riscv_ext_zicboz_validate), __RISCV_ISA_EXT_DATA(ziccrse, RISCV_ISA_EXT_ZICCRSE), + __RISCV_ISA_EXT_SUPERSET(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 Mon Nov 11 20:53: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: 13871342 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 88D45D3ABF4 for ; Mon, 11 Nov 2024 20:54:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C55976B00BE; Mon, 11 Nov 2024 15:54:12 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id BB7AE6B00BF; Mon, 11 Nov 2024 15:54:12 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A31886B00C0; Mon, 11 Nov 2024 15:54:12 -0500 (EST) 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 7FD346B00BE for ; Mon, 11 Nov 2024 15:54:12 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 17A80121ADB for ; Mon, 11 Nov 2024 20:54:12 +0000 (UTC) X-FDA: 82775013888.17.5515682 Received: from mail-pg1-f182.google.com (mail-pg1-f182.google.com [209.85.215.182]) by imf30.hostedemail.com (Postfix) with ESMTP id 164BC80018 for ; Mon, 11 Nov 2024 20:52:51 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=R0EH+W9S; dmarc=none; spf=pass (imf30.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=1731358387; a=rsa-sha256; cv=none; b=ZNQ6y22XWsndMP87Nskt3s86jOQELloez283FwzXvheA5OxEaq1CuAadHdFgHTArR5SoTA 9C1POCNds0pfYqWDcQ58AaGzaP/Mb2NS72Gyk5hiHbMxYN/y3TraEgMNZ83ZuYtk6dy/bv xr2gfTDhAwt8gmswx1EhMsVgt5Tc7Cg= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=R0EH+W9S; dmarc=none; spf=pass (imf30.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=1731358387; 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=i4teOkO5NS+a5tpc/KVRBm9B9NcWtrxKvqevEBspNrc=; b=awE10109w0tmL6HPlaPIVbGzt/3JJxzfYK8FjTlYRkZuWGESfgb9iedJzGSvhnHw+R0b21 UayIgUcu0xpC+ydC89SdaiQaBUx7VGk8AlVU50nXfQ5JE5X1PlunQSqLQig0pO8kaAhspX 8jfkgVFjpkmdH4ljGxOwQvonAIyX39k= Received: by mail-pg1-f182.google.com with SMTP id 41be03b00d2f7-7ea8de14848so2973422a12.2 for ; Mon, 11 Nov 2024 12:54:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1731358448; x=1731963248; 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=i4teOkO5NS+a5tpc/KVRBm9B9NcWtrxKvqevEBspNrc=; b=R0EH+W9Scqn4oeIGUDIIwicSoulqw1c1jomU2MDVJb/5qSDeNuHd+YoEqVZAmft1Yf HsIaoRcTKq1bBRPVr1fSFafjm9tC1230IZ0FFoDwQoMq3st1H5adHGEfu6nR86iBHc+h cBdLLoT0he4GMcOglcitKi33qWLhtKanfMUib5J0wW/IPQFRWrgmqdDwsXRI1Xu1LoD5 K70vPu5qAuCO68zYym2IcWX2GARvM3lkY1efbw27c2VfLR5Cc6zN/BdrEm3/AdkapLjN 4OOx+tVQ1xcS08PRrtikCJZ4yqeuRWgXHJyBYSv5RS7e52lVgPkl63PjsHU35XAR05Ii SEpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731358448; x=1731963248; 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=i4teOkO5NS+a5tpc/KVRBm9B9NcWtrxKvqevEBspNrc=; b=XKpUNRObiFtiC9seJ2AKg2Fb/s4ZtEoI+ov5h+qLXLMj9t69hB5tMS1XzkuiAC2ht5 QRIRRPejeZB2hfiovsqZEmT2UcOxvzTV8dHojSn6UesCWXb7qBTOEPDVZ+qgDvMnFOrJ +6QAMkMuuJ1IMMOCo69hHRE/L6kTlvyr7L4p0B8b+aoHA2yKa/YjKY0qYCk7/8Ze48NB pjmESZKajDiZcZxLdps8+fbJyZGibvdPo2RTFlgWMo8WOes8oset27/f416Vhd2IFzSM RcQIL65NHFYa+z/TVtymZNSQ7kAQr39RAm5i18/sAPrR5p7oB4fUzmA4R24iESTMKKoG dRWA== X-Forwarded-Encrypted: i=1; AJvYcCVdcNIbtaureDwdKFPCzVubF/pzIBZlfPnIMRfToZwsejOqZ6sPnQ+QzEz2+MrhAQgFecsxFIz6kw==@kvack.org X-Gm-Message-State: AOJu0Yx0vRmJGEept38J0LDPC3BI8NXwG4UYpVeSalMMc0Vylq+M5VFH 1/lOvULqMpiN8ZJVpPEv9kBG9jSuaeJP0uo4bVitWiqQ/ICFFM+lEYW+asvPDyY= X-Google-Smtp-Source: AGHT+IG8dw9+pHf+sZxzmda/NztuR0nyiy+ZmJTvX1U9rTy5Vh0+vbtPqkUx+XtKAlxwthAnEmC8gQ== X-Received: by 2002:a17:90b:4f49:b0:2e2:9a48:dbb7 with SMTP id 98e67ed59e1d1-2e9b1740ea7mr18361077a91.29.1731358447878; Mon, 11 Nov 2024 12:54:07 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e9a5fd1534sm9059974a91.42.2024.11.11.12.54.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Nov 2024 12:54:07 -0800 (PST) From: Deepak Gupta Date: Mon, 11 Nov 2024 12:53:50 -0800 Subject: [PATCH v8 05/29] riscv: zicfiss / zicfilp extension csr and bit definitions MIME-Version: 1.0 Message-Id: <20241111-v5_user_cfi_series-v8-5-dce14aa30207@rivosinc.com> References: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@rivosinc.com> In-Reply-To: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@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: 164BC80018 X-Rspamd-Server: rspam11 X-Stat-Signature: scfh9zf5mfz8juiz9uxd3suts9ybro4a X-HE-Tag: 1731358371-252285 X-HE-Meta: U2FsdGVkX18bHk6bhgM5HOnvSBTCZDYQ4Z5ihU4Qf2OND3O2v4mo8Z2mSESvU1vm+R/RfO+bfcPWhxWO34vYfCY08MK3xbXLN8vrHNNoJyvmaG4dmfTzZBK/Vzj/HkgXaJvjir9h5lTDNhZEYJNud0NZfKBLJXzAXr7hasiwrMKXBt3gDN+bTo1ifYz+cDLHGpuCmEa0Xz5IxA0WIEY/BNHPjl9wkpNDzQD70xirQ/YePZnX/exArd/YYCW/8ku6x4xF0ty3HZSCRmOWs0ovsLPPoQmQ7iNXJ1Y1w5A7cGVGnBeC11tg4CQ5O8PZFZYFLC17NjbX1mMBKNi9r+4nGM177v0SUp405ePvoKBkhp4rKAalxQ0rnht03QLqunYwOUoAS1HqP7Cr4uLUo7p/HgHkqk6OKSbhiVggsVrCEUP1RsUROMD1/WiHXlSZLrPS3LJ68Thf/A0hvy3vnuJjCetdL3zpSxh7lIQLmtGG/SEaExlIrL3n20X/yEJtkCzjgPawxfKOSlfdYtDk0hdEOTjXmIcYLF0GveAukP0i4eKPeFEgcmKwUJGKqfUgCfBrmrHuV7W/LcIdbaBdbZ7ymCae34LzdnoHlakGli+hUtd502EIWDWXk8mb3cZoGodiYTWCGFbSSJC/kpfG4xGR2FRlYtGfuKFF+DZjHLt3hXkuBtffp2OLazcmgdwOZG6QIZuH/e3WK7wYX4LD+blaXtoucAX9v3D7fwqvyyCKobg4VM+qUwuyBrD6jC4uxIPmrvA6Lu52IxnrTD9pbV97ZF8ice0RltV7q3VaryK7Yl4kgnDk5jVrst+0cmZm6b9AyGdnUQqJL9VrKASgiW8kGVQ/JJSkAbJTVWZtwJNFhcaJhttvQjkawL+iapVZlNfrU4Kv0G6l+fQH2GGLFDnY4JLFKs/+bfAvV5M2QQ6jjVGFu8GD7nzLbu/x83IHPqWvLdZRtR0LTkrqkDZBlrj tumxIc6O UP/I/MrBPwoNFsuR4bbi3qOQXENWZFaYS39PY9ird1iRwk7GtcqJ+4k//9Mfi0W7hQX0fVjAHf0ad4p702hdwqxpTQocDtj/7kzw9V3s70m49ZyHXDxLoDCsb4dK9Lvne5jDhRsmyvOqhxkWnOHT98OWb+3hdDcP0SGETnGdr6e2prTMeZgxM4aTG75WBe9lQgzXYxefy7oip+VtQ74yUi4NOjN4AhkvilXu51QECKwgd7mokcQFoCaDTYzs8ddlVW/B1RUluqVXH4084HH2HLm1O1RoMF+0+KJLq1wFU744CMqprP8eaBdnnC2qRZlUihicBeuwSJfpmqcZqvFwx3siLA5p8nF/umKurEaRGkR59CU5gmLuHZ62/63xSEQ6gEq2D5JBar2+IxXJ77+Fv160/MJJ8SHH5Dm0KlpjJSwzw1GKvLCWWaFs3mgGJjFYqGpy5Whob7MLdU5I5LoT8gr1uV0TwZZWJn5pLNM8niKvfrEbpepWOoCZ9kkesQRzPtlZ1 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 fe5d4eb9adea..e07b55028cc0 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) @@ -205,6 +214,8 @@ #define ENVCFG_PMM_PMLEN_16 (_AC(0x3, ULL) << 32) #define ENVCFG_CBZE (_AC(1, UL) << 7) #define ENVCFG_CBCFE (_AC(1, UL) << 6) +#define ENVCFG_LPE (_AC(1, UL) << 2) +#define ENVCFG_SSE (_AC(1, UL) << 3) #define ENVCFG_CBIE_SHIFT 4 #define ENVCFG_CBIE (_AC(0x3, UL) << ENVCFG_CBIE_SHIFT) #define ENVCFG_CBIE_ILL _AC(0x0, UL) @@ -223,6 +234,11 @@ #define SMSTATEEN0_HSENVCFG (_ULL(1) << SMSTATEEN0_HSENVCFG_SHIFT) #define SMSTATEEN0_SSTATEEN0_SHIFT 63 #define SMSTATEEN0_SSTATEEN0 (_ULL(1) << SMSTATEEN0_SSTATEEN0_SHIFT) +/* + * zicfiss user mode csr + * CSR_SSP holds current shadow stack pointer. + */ +#define CSR_SSP 0x011 /* mseccfg bits */ #define MSECCFG_PMM ENVCFG_PMM From patchwork Mon Nov 11 20:53: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: 13871343 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 7B2C2D3ABF4 for ; Mon, 11 Nov 2024 20:54:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1313F6B00BF; Mon, 11 Nov 2024 15:54:15 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0BBC46B00C0; Mon, 11 Nov 2024 15:54:15 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E4FC96B00C1; Mon, 11 Nov 2024 15:54:14 -0500 (EST) 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 C238C6B00BF for ; Mon, 11 Nov 2024 15:54:14 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id F0468141A50 for ; Mon, 11 Nov 2024 20:54:13 +0000 (UTC) X-FDA: 82775014728.07.5B93A21 Received: from mail-pg1-f178.google.com (mail-pg1-f178.google.com [209.85.215.178]) by imf26.hostedemail.com (Postfix) with ESMTP id 0233D140010 for ; Mon, 11 Nov 2024 20:53:40 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=hUYIRPNQ; spf=pass (imf26.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.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=1731358279; 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=/wlYhlTfRbz917f0tizIL6fD52nDOL2jxneRKObslEg=; b=W+U5lJU/E+v6RqOOxppNehxbUFpXSmSXGbkXNpJwphSQLw0HteMZksrPdFaZyXh5x28Xhs +6202j30pWyTNHB7qKWw/uBNDQpTQKVKfIJ+t+zrkT4LeTGhBz6slxqqRAi6VVrhja+oJP Jg4P/J9QABpSdyUwO9to61BEM+JX0AA= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=hUYIRPNQ; spf=pass (imf26.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.178 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731358279; a=rsa-sha256; cv=none; b=0skPxIAjr1RLm1VRrv69ecihFr6JEUJSH9feV4WH4zRUDM/q5dp2N+5PmgAo4YfUBIyY1t xagHsKE/4qrbiL/m4zDwJaAEjBKZV05fmBpBT/Js9rUazva1CKhnryCen8/RaWhD2/f0fW zmhLuW5d506phulpem/dLLVPI7mIodQ= Received: by mail-pg1-f178.google.com with SMTP id 41be03b00d2f7-7ea9739647bso3401706a12.0 for ; Mon, 11 Nov 2024 12:54:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1731358451; x=1731963251; 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=/wlYhlTfRbz917f0tizIL6fD52nDOL2jxneRKObslEg=; b=hUYIRPNQcR/wrv938JAIs6vi9o4edjGuwXLYtliZTt/k334daE476YGH2yzgaxbBti ryvPQFtHEm7UFXBC9fy378PT/qGgmJ79sK9/0hDQxdOVw8fjTEX6uaKLsE5yf0AMErHz 99s6BeXkykNR/lZi42B6fLYNEGR4g3Atx/bjYTY33zQM/w8siVBg9BzxnsH/QbMPGIbs J9M7qy0y39hEYNjPwMZmQm6buBf27Yrrd12Dkwmkc1Q0Fe336m6lN630ZN/Wje6VxtRB zFaC6POVqGAh/A1n+oRNknW7iPNIr9oTaHd0VVohajWeXaClmhsB7KCHjVB5bxl4iqLm KIvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731358451; x=1731963251; 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=/wlYhlTfRbz917f0tizIL6fD52nDOL2jxneRKObslEg=; b=KCEe553dlCDcV7O98arHFVB1gfitRdHi7YZ3WdcpticMrCX3bU17SDmiLsDIPRxjcl WKJ9ZfRbI3Gid1LsRR0pTkFk8Rzbh/vfKCdrsnHYz1SeKE2d/wa6N7maZE7XEZ0PCXRB toQYBhqie/occdb37hF5lYI/1XGDkTaQ5vSERo5s17CD8NA93MrcNHmRgDgfrNFvmjkJ ZGTCh1Hw/61GfdkalD7S8NIhgdJPkWqbGSsmcGTAtV2UaRXWZWK65LT2YIvsSYoQU17Q 59Q5/0gycuZnNgXdd75LYVxHu99TIEekxwmJj05j+vvsASCbbmxF/s2UpuFblaN7z34h QxPw== X-Forwarded-Encrypted: i=1; AJvYcCV8+BlmK8pFznTAlmK0AqqNnhopWv0qAk6FjXrKu/UuClW7iiTkM+H5uc9EA9yiICvhJaFZdUElnw==@kvack.org X-Gm-Message-State: AOJu0YzW3rZdqPpS9sU+J8nqZtxd6OIliXJX//4xGiBYxSKXT8r6k4ku C3z5+jZESB3gdLXZUoExOvnsnVQwDz64vAhPQYlqu85gYtAYjuG0iUxHD+6Os5g= X-Google-Smtp-Source: AGHT+IHCStdYWzF0a25kqcOGnG0Pu4JGvRjMiuZ8NNCn1MCLP5RJQJdVrm6DVqL2Hypx+qG3O/OjVw== X-Received: by 2002:a17:90b:2747:b0:2e2:cf5c:8ee3 with SMTP id 98e67ed59e1d1-2e9b17138bcmr21222036a91.10.1731358450607; Mon, 11 Nov 2024 12:54:10 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e9a5fd1534sm9059974a91.42.2024.11.11.12.54.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Nov 2024 12:54:10 -0800 (PST) From: Deepak Gupta Date: Mon, 11 Nov 2024 12:53:51 -0800 Subject: [PATCH v8 06/29] riscv: usercfi state for task and save/restore of CSR_SSP on trap entry/exit MIME-Version: 1.0 Message-Id: <20241111-v5_user_cfi_series-v8-6-dce14aa30207@rivosinc.com> References: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@rivosinc.com> In-Reply-To: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@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: rspam09 X-Rspamd-Queue-Id: 0233D140010 X-Stat-Signature: 6b14r4wyup51sgwd8wzp8oiyph1yuoyn X-Rspam-User: X-HE-Tag: 1731358420-224526 X-HE-Meta: U2FsdGVkX19r+YSt+2v/KZkc44Ly8Guo36LQCMujqdABbzkgpHaIIaqCFm29zYkMPjIUFexBclyyoAhW8NkvBbVkGnMk3PcgkEx+G8+vktvbro9rYMpdGefcyakRZfwqRGY1hChbSyzsQsLV8kapEMWZw3w/4p27bnbOJEWqh2EOhYqrMW2cv0WUlLOXJrlW5d0zPHbU/C8F1w/8p+5ZvjIlRZ4RNKnW3/SUQhtabQCBlyRBlkMeGFfp78+0WT/Yka+eh7vGrxn5amQS7uHVI1TvWIs+aBSMRLZ4S+zOGcW4KiLhWi3HMP6kH/xiIUqVMAZ13Gnt8FMnl8LGxI6dblpKt3Vlddh7Ilmk3yVXTaCK9AGYmUpE7ekrgyLRDGmGXlsXuBgjmVF5xYtCYPV/tTsyt6P0QXvbg5SXCUjzyNcNlkpImLiH/4/tUUA5rcihdMRxrwM84OSkRfPcQlTlu/yVpWiDOmNXPZiIyLeQWq5bISnz2OmnljbNVf+vm6Gyp8PlglesUwrNLru8UEHt8P0LWZXJT01CGh+9eYPTFRljupkYWPYp05+X9oqIJi+2RdvdKC9u2ePdEf/TQAYWNx6ZYrQo5YRDdC9ee0ImSPtQWYNd4MiVOzDO/0Ng8epndqsO0uRQ9bsW5RKnQDRR3LqfKNPdnsOkpUc32ldZVxvgmShpxf2KQU4iEaFE2KIv4t354o9aq2UkP+K+AIPGiQ1USg93+cIF78ixx14h1QbHp6jkRs9XVXX/PVz3AvvJMGGHDfBe4FF3dglegVej6OLYIEFkuHytw4M2aCwHKUr5SN1LrgT07YHIP21J/lVYCaZ9BTrWUfyajHHehBwkO1ocEC2INIOecCa20vMm2/0eN27/3rU6oIyDn1PcV13XvZsBd+yn85fSbM4RMe+B/HeABbaXevk3DxloldaqRuLpQcgu/1rrMm3YMoE1EHXgubvM9ryHBVn7FAO3dPU vFirNeu2 qF5oQcWhtTIZ+ilNg4SJxItmyuqK8xrmu30NYzgIQMov8D1vCxAYAiqfMroegFnHzeGr/IMynrQtXphsPV3gQ/hC5StMTvN8EJjbQNtOyB4OvjgqiLOD4IFxDaQvzWY+NezcOo8gI0F/ddzZOtYs03qJDr7fVnwPTkyimrM/x1mJut+0y2P8+rpW9+OkYVAXrSXsRHa2ULn9ZldtiouG2Y54LnItV43I/p35gsGmR3gldtfyKRxbBIEC0N1n5bGMn0WhZJ8wzDCi7pZgGHImfeB5GmZIF+X7h5MKOESeEqa2y/Xlhd0qkuFls/nMkdUdiYNe5qdOsgDEVBrz1fYGz/QBbXeN49pfxCHWvOmcMADBgcl0HPcW/Kmo93QQ/0Cfg///eadSfJuDYpxBJ0a88deDBlrdsxY4Rl0PMnYjFYIgXAPib8PvqSqLXenpOc0mUX1g0mg0xHQGg2CC64YZvegKQhu4SjaCfz7rymAdXOxOLV6tuPEYYg1EC5qGfIraV+MUtCHC+coT88l/GQeIj+O3MC7I9uMMFQuRY2ghXmABKZI4= 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 e3aba3336e63..d851bb5c6da0 100644 --- a/arch/riscv/include/asm/processor.h +++ b/arch/riscv/include/asm/processor.h @@ -14,6 +14,7 @@ #include #include +#include #define arch_get_mmap_end(addr, len, flags) \ ({ \ diff --git a/arch/riscv/include/asm/thread_info.h b/arch/riscv/include/asm/thread_info.h index 9c10fb180f43..bb20bb7d17c3 100644 --- a/arch/riscv/include/asm/thread_info.h +++ b/arch/riscv/include/asm/thread_info.h @@ -62,6 +62,9 @@ struct thread_info { long user_sp; /* User stack pointer */ int cpu; unsigned long syscall_work; /* SYSCALL_WORK_ flags */ +#ifdef CONFIG_RISCV_USER_CFI + struct cfi_status user_cfi_state; +#endif #ifdef CONFIG_SHADOW_CALL_STACK void *scs_base; void *scs_sp; diff --git a/arch/riscv/include/asm/usercfi.h b/arch/riscv/include/asm/usercfi.h new file mode 100644 index 000000000000..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 Mon Nov 11 20:53: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: 13871344 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 49ABCD3ABF5 for ; Mon, 11 Nov 2024 20:54:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 21CF66B00AD; Mon, 11 Nov 2024 15:54:17 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1A51A6B00AE; Mon, 11 Nov 2024 15:54:17 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EC37B6B00C3; Mon, 11 Nov 2024 15:54:16 -0500 (EST) 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 CCB756B00C1 for ; Mon, 11 Nov 2024 15:54:16 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 6549D81B00 for ; Mon, 11 Nov 2024 20:54:16 +0000 (UTC) X-FDA: 82775015610.10.5522D7E Received: from mail-pj1-f54.google.com (mail-pj1-f54.google.com [209.85.216.54]) by imf12.hostedemail.com (Postfix) with ESMTP id 0AB054000D for ; Mon, 11 Nov 2024 20:53:55 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="sRwwIuz/"; dmarc=none; spf=pass (imf12.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.54 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731358326; a=rsa-sha256; cv=none; b=RBIxJf77zLYJO4z/Jn2jlwFqOXpWqxmXO54l8q2cVCiQLMaR4oXummm3/LPnDBgwaA5RyV Glk8jg4unEbyZywAZKg4fPAQuJSpFLz3PGFSySWzKS5tX0+up9IS4oeizMq2hZLg5TLg8p +JJ/snTfPAK/WilOAJTEafBrC5DooLY= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="sRwwIuz/"; dmarc=none; spf=pass (imf12.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.54 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=1731358326; 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=KPIeex8TzLPUDDSwLAkzt/bShtl/Fs0bUTE8bjGtA9Y=; b=8SGHU9CJbi7wKAw9mVSs1JvFlq+cdFdZi0lqnqVkjPRjHdTSUz6p8rIl6ki8pNjpc8dsQG hz7ihK9i0f59bhFtj5JzVzPWg1+InMPElppJINbspfPRsU9c7sDDoqAb7dxCzAT4W3yb6F pA439daOAtb7tjJBBbB+9yKtP25q7dc= Received: by mail-pj1-f54.google.com with SMTP id 98e67ed59e1d1-2e2a999b287so3952314a91.0 for ; Mon, 11 Nov 2024 12:54:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1731358453; x=1731963253; 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=KPIeex8TzLPUDDSwLAkzt/bShtl/Fs0bUTE8bjGtA9Y=; b=sRwwIuz/ObP8/3Ud4Axte0bSadfD+676wIKyTtZnl/sifyNpMehdkoCngMrUAYk8oI oAv89zbqoYbMWhg0o3v27NzPkGJPHXBtx+eWHqeHSt4XPrcE7PM0hoX4RE1MbU/d0ZOs GHUcTsyv/E2sEt0V5/PXhd+JO1AA9528QY+ZlxoJvNoDad43UVGAZveZEsYk2AOfuZ3/ NSqzaNVS5yAUO3ernsTORR4VBZnbshj3eGvROmAaCcurbdQHgtn5rY9UutJRRod+9RxC lUE24FqfoMzgLWuhgDRupDEgxIQbC6h9oChI5cH7RQQpUiq1ExKhzDW02mCtMfQ9CM3x 2ulw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731358453; x=1731963253; 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=KPIeex8TzLPUDDSwLAkzt/bShtl/Fs0bUTE8bjGtA9Y=; b=XmmKc/QIzZpC3zrEB7JpVZqZY7Ex70Ttwtogoax2+UIJq5Gwg0W7tCKuztNdVcSRyn f0Tdz51KeJCpUwBpv1KcsfoCb+lcl5jXC6bZ58iRxXmmH3CIU2SLDAJfMmmSTnm7TxXW EZxKrzXnh8jnKIuzH/MpbIdcFHXTi6VcFdvDgKazrZtyzq0p843KkDZYQ8TdOrrVdcgx tBFxt4vG/P0BYU0xtYw5SNLwEt2GCMFGuLLLCB8L0lwhQ8DbYU5/sweKVsnt/J59bgVZ mJQq9dqurdV/tN/H6/2ELRNq0iNllHtdm6WaNmffZiqqauuJqoAVSwGGNNMsfPjOAgPK ZGNg== X-Forwarded-Encrypted: i=1; AJvYcCWhV+x/XN0+wsDfWwEXYwJmcQ5ZopN9jA1yAt9dWtvlA/aOGvH3fleRj9pIgKejMkz3hfJN7+KqUA==@kvack.org X-Gm-Message-State: AOJu0Yxi3xDlY9tLAquw3ZpWzV65ecjEIC0Y96geXdL5n2Xt2O/R3c4+ 0q+opfvnpiLjzPTbFxoequszJWydNrExOV/wl5Ls1FJbfODMstmMzFKd5TDVCZM= X-Google-Smtp-Source: AGHT+IHxrhthgasHXbf/UNeTJ/b0NrvHjmhrINV/NhvROg5Fgd3+BWnrPq93OPr1Rkxjz5Z0BvI9CQ== X-Received: by 2002:a17:90b:2642:b0:2d3:ca3f:7f2a with SMTP id 98e67ed59e1d1-2e9b16c9138mr21909754a91.22.1731358453300; Mon, 11 Nov 2024 12:54:13 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e9a5fd1534sm9059974a91.42.2024.11.11.12.54.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Nov 2024 12:54:12 -0800 (PST) From: Deepak Gupta Date: Mon, 11 Nov 2024 12:53:52 -0800 Subject: [PATCH v8 07/29] riscv/mm : ensure PROT_WRITE leads to VM_READ | VM_WRITE MIME-Version: 1.0 Message-Id: <20241111-v5_user_cfi_series-v8-7-dce14aa30207@rivosinc.com> References: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@rivosinc.com> In-Reply-To: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@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: 0AB054000D X-Stat-Signature: ja68w116rt3gsso4hnzs8xhth4f94tp1 X-Rspam-User: X-Rspamd-Server: rspam05 X-HE-Tag: 1731358435-644481 X-HE-Meta: U2FsdGVkX1+Ul/aoTO4PX+/d4hCjiErmvKLlhrfy4rat6w7DUj5tzrtT8vPuMU0+DTHYiPUtjVcwxVTu99nXdVyEVSl61z3d7Qe0kweTGk5JC6VdXJrxP5NQamkueuIrXWlLkJ+pEuiw5fF5N56N7lAjM+Ma5Bssq4x4zGRzfpsHyzAv0mQt5NpT1/wtG98LzNeW//U4YvlpCO+cQLD96qb0dvLFD8U64QjBIT+wbRrLX2ewABpxBsHhA0u3rz+lwhKUr+0T7XPVTI10yxYWD6bTzOGRGXgJ7fGWOWoJEuefqJYdqgqb9VeONtYaEHn5z6efeRrSEpWuev3PMmllGcPID3BwLZrTeP1asfKGtj8pI5vZdrpUU/Y19+n25o/yO6M3dxyrmLqI0JbYA8Qng3s2eBabeP+QI2aMZrnikbVzEUBvFOj9xPNZaHvC8ErnLgsqUsUn+WxxynL7+EBjBkKedFfCgUDCvFarVDzBfbJ22bEtNO8tZenipxpw5LdQQ04Pe4Zkt1PdeRgKiNwcENh2zWBqHTGO+QyOspAi+AdrYjKs+7NoHI8BZAtexY2RKEo6uV4rhY8cjC6JoioPY8wA8gDIjKWQLvYSFEGPkauWZDW+DSiueICrYvqvG+OKLkmsa5u7qJSvBqOS7iANTlO+Scd+N/lks7BGQ/7+rA+t1oO5SJ1tAl1jIUmuQnQFSx8oyVV9gRCr8KrpTtsxlDHVE5vmORwdDwRfNlzIk9vpueChvH9cfkYh+8XNctyzRXKTIksrx7ucbKVodad4hm6gPV3rgrRAgrfEZNJAVsTKXHXZ7BIJ/pbufcBRqCanY7JUcZXige2IvWs59lJpVqhbDMGV4R/4sd4L8IaR9RHyajODtRmnQ+cIulu5ntsb0FvN1v5UoiNKKso6rZJoXwJh3aw8jU7qnlQeJwtdPaIZOr7W9GRsFq3BJCWj4AUCXlKo1wVzTGnNLtX30Xm dPTMB3Ez 6HblMqmEDLYHijJ0hTWCSJZ7LQQDIhqstsAYOLsQLTr7vx0gZxjyxYCCruIBVlUC14fR7rSuipTJRquWStXQpLGVQAz424lVVVgE6s8Jo95Kp+KxB/Nwklf4F0hpXoVN5uF21SIU91MkD/3Bf9cw9GrO8le6JJVLTLT0/wZbYAcDa5nl47X/HSt424fao34a1K6qr8T0WhCHrlaCxwzjoaYMdiwK2F2j2UODh7oXEUSWO9ozl1NoAmmKXMIt11L1nCDsmTJ4HXuKSl8HUuX31y95yDgYzIR2WWm0tyobd3oSKS0U7V6NFmPgU77kvaDiKzSzsJQRls0LWRVz6ExOAD6tALVNz1P1LjmRHx0uo3nty9kZQiF2YZky37vuf12eJXTkkLQakDVqK20BLNJ2L363AQ9GUTjCjdkUDkyPa42k7Id06KgG8JXzo76mP3QUc+lnRDTY+wSP6OXdRcXi31/T3PSuQRei1mVmvCNGJpsNw1nlpIiZrFBdsIKgwIHQFOmZWL+qfNthejIdJPcbqrJh6MABMy7zw5TiwI2FpGvrO2Fg= 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 +- 4 files changed, 36 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, From patchwork Mon Nov 11 20:53: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: 13871345 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 2A612D3ABF5 for ; Mon, 11 Nov 2024 20:54:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A5BC96B00AE; Mon, 11 Nov 2024 15:54:19 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9E5DB6B00B0; Mon, 11 Nov 2024 15:54:19 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 835606B00B1; Mon, 11 Nov 2024 15:54:19 -0500 (EST) 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 634DB6B00AE for ; Mon, 11 Nov 2024 15:54:19 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 1F4411C7781 for ; Mon, 11 Nov 2024 20:54:19 +0000 (UTC) X-FDA: 82775016618.29.C0BA735 Received: from mail-pg1-f182.google.com (mail-pg1-f182.google.com [209.85.215.182]) by imf14.hostedemail.com (Postfix) with ESMTP id C2098100004 for ; Mon, 11 Nov 2024 20:53:31 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=PDGDYEn0; spf=pass (imf14.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.182 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=1731358369; 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=ZjKnr+WeWcB+MbC/JyrGPA/Gzf64lZh8XHGKCrdssr95sdlAtxAKAFoWXHNsJAIXXNNAsb wuWzntVPzqQ3eWHIbft3f/C4BumpdfWrwZBnqQqKuI0WXrT2EC/QEvuyqGMGJ2WTyl+w/D 0rb2ffcSL455ocs50uZ2gtZZ67ih5eQ= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731358369; a=rsa-sha256; cv=none; b=Gc0Uvxy6enBh/cGBFU048g4Wc2t83kW+Csi3mIpkwRiH1k35nBaukePXHql7wdmRw4o845 RsMSAUj75XXb7z5cNpumG4EITNv4Jb8XI163CYgW158tcGC7tbQkvhQnw/JG4q6l2/VBq/ WfzUQ8UDQ8thfKgkuXSDYeqamXOe/P4= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=PDGDYEn0; spf=pass (imf14.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.182 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pg1-f182.google.com with SMTP id 41be03b00d2f7-7f4324503d7so2291775a12.3 for ; Mon, 11 Nov 2024 12:54:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1731358456; x=1731963256; 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=PDGDYEn0/hbnPINHNwv6v69Msjg1eJh9Y9d9aTDUMkGH8utGiQX5WUw4ktEWzeLMd/ dpxsPab2ft6qRWEA8Olb/Vf3qd+RqAN2/+/yeEWn5jklNBugo45dxB1wVjXYLtqaR6R5 dJo1c9Ki4lax5ggxtJaRNIMxRDxLKYSi8UBpOxsFIO/YROmfCGiHJZyrV6T/CpJ9wyWJ v3363c2rb5YJYFKL41NR7HukZqad6Gplv7NrtTFZf8UEVVNBxaER0GrBYaOIM0+a6tRK phCG3gTm9/sjYlPPRrnQNKOFzZmQYZZaIYo7WrRd6oTzoY6j7QakE5hXjeAGiINPf1Kp JyJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731358456; x=1731963256; 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=e8+z86ZRucBUtIURUBVo1JYE/L+MgORXmTMjpjpoNw+BWmtPlY4J/K5eu2lgMi6WJf LcwbMCbnAw+irO9kpuUTbiht2II2E2YZ5xYRUHwVOlUu35tnHEWDys+1tc0qlVcVYQSu ZoTXdJhqpxGXeeE2zuCUd4E9jKi4CZ8TmU8lfznZiQWaXOPFCw6Fy7J81cJ66+XVjGSx 35JSb3OsKgQ8SoKm4QGQFAtbmv337NdQguVMZrP1U0DDWFYRh97ltFdpYcgr9iJCXY4k K2zRdoaor1KBHFEtBnMpCSH57gcBhcJ0EBimiuwQRTujKoWWg60+mQ0CefRmXi2735ch Xgjw== X-Forwarded-Encrypted: i=1; AJvYcCW6/GTQgVwKjLg7TUh626apDWKgwOfEMb0DbuxOU58KqiDgU76eBy3Bg1qNswtCVDneIOhBshQnpA==@kvack.org X-Gm-Message-State: AOJu0Yy8jU8ht+NaEh5aLTpA7NXJXz/VIJxw5+2BxRf2GJ9NZiz9WEJw ykMWQm09UQWNWgzVCoEkPD83Uk3yEODZ0uqND0nrlLtQ4JUV1Qa3y4x7jj74NRI= X-Google-Smtp-Source: AGHT+IHkeWur3sgROxcRoGTFfP+xBQFc9m8RxiHforXzu5iQMPat3nHq0j8ICMSCMJgsQveJpQW4TA== X-Received: by 2002:a17:90b:4acb:b0:2e2:af53:9326 with SMTP id 98e67ed59e1d1-2e9b177fc76mr19427178a91.30.1731358455997; Mon, 11 Nov 2024 12:54:15 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e9a5fd1534sm9059974a91.42.2024.11.11.12.54.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Nov 2024 12:54:15 -0800 (PST) From: Deepak Gupta Date: Mon, 11 Nov 2024 12:53:53 -0800 Subject: [PATCH v8 08/29] riscv mm: manufacture shadow stack pte MIME-Version: 1.0 Message-Id: <20241111-v5_user_cfi_series-v8-8-dce14aa30207@rivosinc.com> References: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@rivosinc.com> In-Reply-To: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@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: rspam10 X-Stat-Signature: epuxxn9u57r1ms4stpm3xay37qnur91b X-Rspamd-Queue-Id: C2098100004 X-Rspam-User: X-HE-Tag: 1731358411-130214 X-HE-Meta: U2FsdGVkX1/CUOU/6CzJPH95/5vfyZJIRC6jKKtv4vsGJWe0FFgZnyTCvgLG1NzlSSJqqw6AtRFmzbWAuAweA2wuXK347Z/s14PPyzNiWOM0ZPILCJQYGicdXeQryn376QbGn/3uVSSUahf/P6V6aa60N+QtlDt6RocepSmMjSWSlJ5jLWDGjoTGq4USeUkfpDX0zRASBCVB5yCrCSzZL4lK8WRMjs+Gq5U2f2vZzkpcZgCjhxXnVSN95o+rycv1icekg/OiSz5gmG8j3s1YztkBdvEhsCqj7qr5XLtLgXJzl3CZImbvdBICPp9eMvlIgBwuPkXY2vLj4nKunD8a/xxGse12J4DNNF+FijW2n4eFToZM5NRtKQ5/q3wdSnwCQ3bFLXjozzsW8N1LfB0AiLm+0QyeYCr/xvyW6aT++BGJdFdqAVMKTy6Y7I9eegpAhUxkRGRgG+ZKhnYDbTPzNU7u7L5o1TBxO91OIM7klQ3gOeOVEv+0q3Xo2haX4MHvK6RfPTqd/9VHce+EP1cNuvlqzBo/UkjKmZVzrUX7xiPikjHMs+ZD4nUHkuLg9AvshoDXKbwgJobtr/Ky9tSVKtn4k74O9pD9It50RK21jIZyN/tjieTp3W6iZlHIWGl81NaOZPnRMp9WD6GQmy0h0KPvWYbQrJbCO1uhRZewIH5hNhqirKIoKAganZi8+XPhJDr2ar66xmZy0vUC+JbzL5rfR/LULNrLAUDqyAd0CnNCSCFebbWIEcp2T/igWIZAnUQkxBJl+UkbctvtqFC2NyhuQZyfhb8A3iV5wmdmuH8/G458/NVXLV3V2LQ29wENEJ+h6+aVHnr/gT/aAaAkbwpdZdx5UBYc4Wib9L1+orbAGIXJlLvjY1D0+Dls6JtKFo/atFzcGcIwgNH7W5MPKOBWw98JUQrUEKAQXuYuk+jBSOnTKbQKy0GCq9RYGYIx03WlaaqJYnclpoU9oDG BMXdciTY WDEogRafZFCNSK2VJVJp/RFZ5mzPCzFGI+XdzPsBGzmbcbEF96NWX69iZpRQVuL31duXcHK92/MU2pkIGw/IhouK9g2nIesJQYWXWyBj0rNwuTVzOtQj/r9R54fuPmaVFqv1PpMzi2KdNsHrFdY5H2C6TkdWkEoE+Pw22Jp+2dDCprlj/Zi/rRizsJC0GqLqjOLZUEOd/xkRa31PIOiOH+Nd1aZVrGvygSAxMppWjj7NfpWi3gfcJxRM3NpO3sR/5kTl6AnQxFc5YttQ2FXyUP44uVpV2yUIo9E+D46DpnSxM2myzRduix0YFqSknWaexBOiNJ+FSHn8J43HPTDWKMdekDYCXcto8hclzoof0hUekIlmduI4r3JPo4vaZLRP8ZoFmq7r0wh4nj/GoqzKlH5E3vtNC0fdu4lqxlDg97h1rHY99NRACmTsNyltlFVBLxNBKpR/G6Vow12y7bSMTt5jRfA== 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 Mon Nov 11 20:53: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: 13871346 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 4A667D3ABF5 for ; Mon, 11 Nov 2024 20:54:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 837806B00B0; Mon, 11 Nov 2024 15:54:22 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7E9CC6B00B1; Mon, 11 Nov 2024 15:54:22 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6122B6B00C1; Mon, 11 Nov 2024 15:54:22 -0500 (EST) 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 3D14B6B00B0 for ; Mon, 11 Nov 2024 15:54:22 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id E97CF1A1B9E for ; Mon, 11 Nov 2024 20:54:21 +0000 (UTC) X-FDA: 82775015820.21.AEBE5F0 Received: from mail-pg1-f174.google.com (mail-pg1-f174.google.com [209.85.215.174]) by imf05.hostedemail.com (Postfix) with ESMTP id D5940100028 for ; Mon, 11 Nov 2024 20:53:02 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=VCZaXPIR; spf=pass (imf05.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=1731358228; 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=wdF8zQfFsgCxNaQrVsEP/APt6bV4nXDNgLjXOFd5KUeWUgCIFwDCuCMJRRTuje8ftAnFjO OBIlf6eeA2XeVoEQ9ZKGKuIE0qPHxbrE+WQldYPqgItBZWiXFc0NvTU8znfjCgT7KfXQZ5 o4QPMuHcMTryfeQfjDOFYFXqEU5oquM= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=VCZaXPIR; spf=pass (imf05.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=1731358228; a=rsa-sha256; cv=none; b=sxro3YpjVflbwD0+NnPzlhri/Yr86pq/uxS5h9CwIQbn2yYIwL4dPPATM9Q6KPOv9Wbitg qV6fnPvVywZD2lblM9PCC62F9d5+HDsRZ/4kQjKWNzwr5kkEsDYJzVrfAT7HQ1LD3kav6n uqsyLvdAPBDFqFMuSABrwZDQQCkFIkI= Received: by mail-pg1-f174.google.com with SMTP id 41be03b00d2f7-7f45ab88e7fso1007525a12.1 for ; Mon, 11 Nov 2024 12:54:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1731358459; x=1731963259; 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=VCZaXPIRrvo7n4kIsJmM6U/C3MyFIfnu5/D+mlSY3EGs0BOGvl6G7BPVYTUJW93a8u x0rg1MkuoUYp4w7/NswnsascjEc5Z9D0ASaWRp+XeWBZoI6Bz5EAoCWpziUFNBAjTg8c GXjOpVSO89dipzG/LTdvXcTjlkWePBDAiQUucZgb2yT61eosqc/5Av69GyJyMyXXEYrg WdwMbAsaIQohLsQ4kGgvIN4hb7HDrwvmQRLdgIorH+2TrkoFMsTGC6G7ootBBINPaHZh Z89Iylyo/JPhaPn4Mpcs41tPICfz+z4lzcStYCmBgstcPOcFVNNvm46iy7FDS5hdLiZu fcrA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731358459; x=1731963259; 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=bryArB1JfpWD37QhluqPrl566by7kqTdUtmQ7zlFqtRWedq1wSlq8ISpjzE22LBQKV dGrkWPcIGB52KNalMDrUBJ1dQebIhRoimriNPpuMIPNy1OFuQNPTdP0BjQ4lEWzDgiYr D9Kn8lH2alrV4GVXwwmsb8cakfOsocAJSyrajb/qBPYOIB/zHwQ2ZiEes9u9IvKq0E78 vsPbMMKg1RLsV5jrIuryVOKQTfMzkIicrjJPFY9vycWuEgd9WDEgr536S+2TVxN/KZJn WXpqzTGpnRRYQACMKRPfnsstzAZ370xGZmc+h2zdRYsPdRK3NC/+Y+mXcL0OLeQe258W 2F0w== X-Forwarded-Encrypted: i=1; AJvYcCUPuJxp0YfHwvK5mVNS+ZMKcbP9UhK9E6Eu2ZG0ls8dwfRfsQL00gVOBrN7NDXsYJ87TFYr1P3POw==@kvack.org X-Gm-Message-State: AOJu0Yy7ErOo9jj8GenB6mEUmhKkApvy6smMdvlMPRVZ/CwenT5Y55q+ CrZUmUEqL1QLq4ZR9VLcAyu95ofL22Y11fizEze1EakV4RJwP1viV2LUYOIwM0Q= X-Google-Smtp-Source: AGHT+IEjvMy0kDgdhVCgK7uRX+xkeiQXZuQFJUruwKRcfTXpudULSRLKQcZE75uv6i6SPaD/tVV5LA== X-Received: by 2002:a17:90b:52c3:b0:2e2:e5d0:dabc with SMTP id 98e67ed59e1d1-2e9b171635bmr19132907a91.11.1731358458754; Mon, 11 Nov 2024 12:54:18 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e9a5fd1534sm9059974a91.42.2024.11.11.12.54.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Nov 2024 12:54:18 -0800 (PST) From: Deepak Gupta Date: Mon, 11 Nov 2024 12:53:54 -0800 Subject: [PATCH v8 09/29] riscv mmu: teach pte_mkwrite to manufacture shadow stack PTEs MIME-Version: 1.0 Message-Id: <20241111-v5_user_cfi_series-v8-9-dce14aa30207@rivosinc.com> References: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@rivosinc.com> In-Reply-To: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@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: D5940100028 X-Stat-Signature: knzc4a6b31zdcajjpwfy1d65jgkaepwx X-Rspam-User: X-HE-Tag: 1731358382-664576 X-HE-Meta: U2FsdGVkX1+MSBCyKcOKvgEStIUNSjz3Fb0M+03dIG+HSKEcQxFgkAwdSkwxdmjT7vXLwlLRwZ2SI3cT4UFxUbzDt2v144BEgKcyy8Ezy/O9PPcwgDKUL7zozHs0ce9ua5Fm0IpFGBT4K2VRpR+npKO1Tn+TJ0qA0vabr9zX1jNzr2M6GVDvymMQrLWfa/5D1+P9/IKLNR6mm9meIR76/y3OU/WJp+50Y0xEhCNTfel2vLfNyQLI90s7ITScd8rYpx9uPXOCIyENUfHUHNwWEtXKk2+OC76LgYrUY+PCVyPIQ16LhRK2IwAbikMXcpa/a8iOAnR8lTHrxtSCZ/S3BaRlbt+ODUXb4PLsarq2ANVKy+ONPW64ceFGZHQmO7cYc6ynzX7Q+JZGGbrSvydNhcvTZ5Pzl5viA4bAm2ePmwU3Y6da1Zo1uHk19ihHo4MVGFADZFhi3gOuFMnnQbz4kcKHE603GTzSoOLlmZc+NVxY2WSQJu4xtTPBs0Cspq+LSPe5SgyRcn+ZbiYRhx6iFjCOaSQ8asfgWxX1PwSgrDeepDOWtjKu32Zl35NRGRnOVXtJ0YQ871jWx2gIXOPRkPgJqd5cGgPCCcI36ggSoggvG9HWh+5VDypV5EkzR7G3wM+bLOVozDIJnX0icrzRduZIUn7yZDkjcgKatUHY00QsSh279wf3vw6gD17yn+nl3SE/rU+I6TC++zPODDYEPx42X4Ct3lAZQqanO1C1Of0l1lHgo/lmeyFcjYRFDn8HlL3a9qV9UFXdc5KLJKdMawwefMCOX5ex2xBwkOVSkr7by4oY9oGi2AzDmUWBn5id1enMyL4l16ulGtPv+Tz77eI276tCHNjHDuGIPp/CjJl3wb/RXhO0Vy7MtoKBR3y7D6kXwdbEAEiOzSEEciHRnP+1RsnuJc2vCyyT/ljq23/LCTgIdKRdnMIdWVnohxzX2edwzfkRH2vu8XI9pMC wq7gBOzA vnM6TXoqy9oxNNunNrEz1KJVgO93u5r5RD+pGCvsHuuo8ZxgAXHvk1O4d+CPNG6pTJiVNzy9dlDe7OBbIuzeCYgJv4wML1PHVhyY4vdKXl3FWk/hzTnh44520OMFxQ0Hid6fZImhohcyceGNKB+VGfBSP4uA6Q3ku9fQU7flWsExXSzf2oRy3F/34GiwPAp6z3dIGUre9ly4ZgFNI7GclC3Wl5+lZ/LGheTmQnWv2Ftj4gtJzcM/c6xVagGUO70irA9b6lDpBtBwfpJSxMLAUmVOWbbZihMENlzDYTCPbC1gdGOsPGrvrb081mc8zt/98XmBgBeLwYRsigPnzULphXdaYxPUAdm7lprJpOV0DtBQcFEKS7dRyYm9qQMgBQGdtVZJCVOMlrEHpAqJr4j5sPpRcPsmED7Y9qkEiI+NGpE8OCQyhaFsSeMrnnxe378iR7ZWIQNI+jVcLaD5NNcFNacvCPeAeSh4O79z9x+Pw4kaUGCXTAaNmNvPQtsqHq9+6adnc 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 Mon Nov 11 20:53: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: 13871347 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 1CFA5D3ABF6 for ; Mon, 11 Nov 2024 20:54:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 662DB6B00B2; Mon, 11 Nov 2024 15:54:25 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5EAAA6B00C1; Mon, 11 Nov 2024 15:54:25 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 43DFC6B00C2; Mon, 11 Nov 2024 15:54:25 -0500 (EST) 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 240D46B00B2 for ; Mon, 11 Nov 2024 15:54:25 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id A25CFA038E for ; Mon, 11 Nov 2024 20:54:24 +0000 (UTC) X-FDA: 82775015400.08.45C1B47 Received: from mail-pj1-f51.google.com (mail-pj1-f51.google.com [209.85.216.51]) by imf29.hostedemail.com (Postfix) with ESMTP id 74F0F12001D for ; Mon, 11 Nov 2024 20:53:27 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=yWVfHQwi; spf=pass (imf29.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.51 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=1731358230; 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=E/jvKfwHYiC6j1pmAXg77EqoUT9eHzQ6zC5oL8Cp9Bd7D5p7YnDcsw3a0Y0WJ4ChewkmmW D622Vp3dMegTjNjaiuwtHIMyTD7PHzzmUEDMwtSJpRavxHg7rcDIdpWchnfaKh33/Kug+w gves9+RPaSqSdgKBFB8boClUbD1zNEI= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=yWVfHQwi; spf=pass (imf29.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.51 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731358230; a=rsa-sha256; cv=none; b=TSWjGLT6PdUp1Bvl/iylQ/yFfqyavYbEhLeOtxBkE6KvTM0Uq/idZ62i4Ho01dLPDNDbLk 8GK5J/7vnpcnvf2Y7eVRdht1X2t4QzaLFaeeBH+KA+cehSOPAtunLWEDfXsbMIdikaZsEi F2gZunf5E6FUS8K0tI7qjaPupPerxpc= Received: by mail-pj1-f51.google.com with SMTP id 98e67ed59e1d1-2e2ed2230d8so3946763a91.0 for ; Mon, 11 Nov 2024 12:54:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1731358461; x=1731963261; 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=yWVfHQwi+SwpUpDV1MHpYrX/sNLdIGrvx4nAyfVcRnGI0zHZekYyxhooX+m42qOgbq x2x4Zxu5QUEhBk/hedureZpt7FbtarakRMm5WqQ2RqpCoFwzt2hI4CpLQgPLxw4xhb42 5WWNf8BRow0xhC1G+EhhDb275RlrsP5+FlEnPzN0sCk1IPR+E+nmvVd/WJqJexuMamNO 56kNU5rgcRAK9aLzN0GUFpZdbXQ49vT7MfEpTuL2F1ucp4QJrWde94rVKdvJnb3xQdqZ C78LA2+2idckZjKGDuZinZEjOUMNEUFXOL+0dK24GwpOHancy7RH/Mqpr4lECjuBcArb HatA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731358461; x=1731963261; 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=tMv8mRuooH7jwV4/K3LKQPg1oq49Mvw8mAlAmQ0Bi34+MZRJVYaOk4CkRjAWC9XMj/ Bh5k3txKdlvP0pOjOgCZ5eRfqd7Eo92gMOreBapDckCFKXnCyulvWbxElp5LYyJCfi/n TWnyWXCmubIMw60MFBzyV4czW1uLH0f1jlF5zdilhAEyjEp2isSPR+Xf08wmPZkzSKzW 7Ty0l4TtrW87ROxCWL0ycIy25Qq6rQzgroqRYIH69rQ685DSejXpZl9YFioVJTKFklln ZkJXvFGnwPq5E/BDOv8yuHU8LhCTeuR+NQLFT+OITpNh6Rq56krb9+TqFB9+mqEwJxRq ZbAA== X-Forwarded-Encrypted: i=1; AJvYcCUcN2+uOofwmU33ifLUiHtIaq9cIp4FI5WBaSPt64vPnwJwRbCZ5e7ehm1GP50RYaY8Laspomu77A==@kvack.org X-Gm-Message-State: AOJu0YxkSvuuYCtmt+S5LPD04CIUsD6XiRQ4dW+7XClcX5P/hW7DhyRy mOKvSQz1aAz8z5xVnNBeqYOkGnyZSQ6fIf64FdPu937WpkGhgwr1s1EIBZgcC14= X-Google-Smtp-Source: AGHT+IGRLWN5kqeXRNQn8yzpCXFOSvMQOUs+A8CKa7xWJ/vZOijjwVJZJo9LQV0BVahQpTbnhrdFqQ== X-Received: by 2002:a17:90b:2b50:b0:2e2:de27:db08 with SMTP id 98e67ed59e1d1-2e9b1770bffmr17545652a91.23.1731358461465; Mon, 11 Nov 2024 12:54:21 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e9a5fd1534sm9059974a91.42.2024.11.11.12.54.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Nov 2024 12:54:21 -0800 (PST) From: Deepak Gupta Date: Mon, 11 Nov 2024 12:53:55 -0800 Subject: [PATCH v8 10/29] riscv mmu: write protect and shadow stack MIME-Version: 1.0 Message-Id: <20241111-v5_user_cfi_series-v8-10-dce14aa30207@rivosinc.com> References: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@rivosinc.com> In-Reply-To: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@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: 74F0F12001D X-Stat-Signature: u4iftsupmip1ubazquy8uke355euy31j X-Rspam-User: X-HE-Tag: 1731358407-783262 X-HE-Meta: U2FsdGVkX19eiQwnwTs88gYGUYXG05jnaOT1TZHTcvtMK9HKhmDR9+O43+5bCPw+lds7r5xjhEtmenLNLGEUITst+k2b4Pq6wbD5OqS8sz10aNldS0bXeyY8lU3KWIGU74Jxjnnt1m21hxSypMTUjhTngLbH9sP5pR9JZE6FFvKCMjO9WYmHvli5yXIhMUfcEu3c+EHk845Kybi6kC8xw3+q+anI17QOmFUqD1EPkR+eZM2VrzugViWylOdBl17NSMXRy5g2ovpti0TWYZJ58YZwyr9PIAav4VSMKra6OecTtAwo55ZdhJYkJh3uXpFsOzXFFUtghNKvPX3dnGuMlhAllcE/KHWcX+hIUSiQzFmap+EzVPyUu6ypeRXv3dZVSGBFJG2LzXIWVgOFjRxrjRAwAfQBzcnJ177/glH3DMmu+EycviJKolQymerdV9DCyZGR+LYyFPrgWdAwoRQIYXW8OLyRs69mMzOhAodGET0iSsjPdd3SKOamE7BZTelFqGAbHXzW2iQ4cRthX4oKGkI8bQPF6Wi5cNEdhDzVOLZcmKNLOCrZXzWi+iXGDewZ+J/oYUwHh4mRFIL/W9Wbe5AfbEjftlay2vVUHWOcic9dCNDT33D7Y1tfevqFG5vOPt3rhvdLmwvWNExO+LZNMVXKvVOfbyDD6AExlVjqPepGPTLk1V88tp6BvH1mKsuuvP1JHANHIox/gwAUoICWbtIEGTT2pBJZxKdG+AmoPJWzM2g2lRc4h1y1PtyLuT8ghnJTEV0zAHVIgUpeAYjHtkHBKo75198TgC5Z+T10bHgcCMAqfo3K3zSLi4qocQw6nq9UUOIsKoeIKmr57uBXpW0J1LGJXSpoPxw7SrD1Z0bChFvUFHN5eUmUE4qYluJ5of0mbGkDqqyTdJx3AFfWLh5A1ZifTgu45bUijGtTryAdZcbXJukHkTlPySweMyq3dMoXoHDSNixKiX25rZd fm9H0O1P 4AsVq8Kq6lRIiYKYM7JcCIPY0PTUgfYEA4jEfoCzSnTKNEzeb3J0iDZkEcwEo+AIO9pHsZgRZNPMxqnIE5sEUSNu5kYnFJgRpWyJ9ybJpE+DgE7a9FLd8w7g98X6KnXd6aLQihA7L/4zpoYz/Nvh0Nqjx8hUDJLYzBU7nzq+ZhIU/XuOIXP/B30enVoT7S5wNlmfU+ulTGhhvZD35ua3Vf6p9+Tg8ZRagP8sAph0jNDsfI2lPSEx/vf3j9EQgF/wPtkRDthtfJBvwXqFqiyPl/qGSjXlPLqy4BzGyOW3RvMy42I901Ytm/AgD+GQedTr69HnGEatUgrZeRVtnbe71JqaMmyDSTADtwR7cD821/CEdR2Qv1NiRT/N4aiEdPoIfzs9/1IFwBISrUK1vlcPaS6SjXrWmCInZLgeQUWB/REZ9TCuomWtnyIcGP+hSdL4KEpegQgDv+xMfcQ0C8njM3rg+LA== 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 Mon Nov 11 20:53: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: 13871348 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 BB25BD3ABF4 for ; Mon, 11 Nov 2024 20:54:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E2B726B00C2; Mon, 11 Nov 2024 15:54:27 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id DB2E76B00C3; Mon, 11 Nov 2024 15:54:27 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B93326B00C4; Mon, 11 Nov 2024 15:54:27 -0500 (EST) 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 971BA6B00C2 for ; Mon, 11 Nov 2024 15:54:27 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 53B37A038E for ; Mon, 11 Nov 2024 20:54:27 +0000 (UTC) X-FDA: 82775016072.10.29B7493 Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) by imf26.hostedemail.com (Postfix) with ESMTP id 600EC14000C for ; Mon, 11 Nov 2024 20:53:54 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=n4YqddSn; spf=pass (imf26.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.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=1731358273; 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=WJnRmE7nzvvUdOgcQw+N8m7/qUjw6OBKlSgXkJR7mUo=; b=K2zCbhbGJoG/WIP99fZphZFoJYFxo7G/mfiji3HA0WEH9TonTk4GMSphkYPylXXpl37QBP 8BTmNet1kyq05m4ncHWez48knjFFfSkrrVjU3ngBvO1CZYFW+xQcNBRIiTBw3x7DQeWVQR m147FYmsnvvB1P7w3jXitkC6fkD8V7w= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=n4YqddSn; spf=pass (imf26.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.174 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731358273; a=rsa-sha256; cv=none; b=mPZ/HXBBkpzPQjQ2LVWDGpgZqIUlg3cDFyM+umGQkoGjDCLT2gcOUvG4XIJ0sCxSLR8GA/ iBMuyvSKvfCZ1aUA2s1oAgg689cfBUYWrTV5bIfMae3NlygtpQReyjQcT9i9H3MujwqUy/ W8Sao89779/ac5u8Dc3myZLFjdebeJI= Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-20ce5e3b116so43978045ad.1 for ; Mon, 11 Nov 2024 12:54:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1731358464; x=1731963264; 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=WJnRmE7nzvvUdOgcQw+N8m7/qUjw6OBKlSgXkJR7mUo=; b=n4YqddSnaD6ZPr+QAbC1lVE6mopfzOSSr/8QBw0TO+tn2FSSAfyMc7La8CPnpsdKv4 83LCrV+mHfIzUPttpkC9PCGmnUnPBlzTs9CS7J2dv4+OG624rff7RxH5NZROuh9qg9PX l0MpO3kwuIpBmGWRSGx4Dl7Cravd0CLmXsfT5+6nggNsZLAwsSafr/VYNhFk72y28OnX u8rZKjeKWf0Vfn6t/e0KYKSeI00f9m+nqfpVWpp4pxPYERVsnWKBXUTHiioGAbpdBeyL wzP+vcfgt9u3yGJgtA7gqDxgd3n2x0FAAXqiZDXOJ1e8KBlf5kJnOYTzB7wSgHU4FGQI ewYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731358464; x=1731963264; 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=WJnRmE7nzvvUdOgcQw+N8m7/qUjw6OBKlSgXkJR7mUo=; b=WUEvD/6e5I+6rA+qZhZ7em3+x/J9ajzrdXyx+sfyKLP+BS+q6MnvPVw/nyHhIlG3zv gQnn/s/3eQZz0hhafUgWRxzQ4fdW86fkxmVNuapEr5cBO3Et1nbwF2v5s8DCl9bhY2p3 08iN5TYVGbokPoVq1C9jltYgPAoKDrdL4NdG14xoBNSJ8CYEHA8Ka7B7IEfMNu6gn5ou pmfy6RnwHZKQHUxsYSV1PVV1iSAEU0ERD1bWeEJGS39uG2989sk1uZdUVJZDxCze+UB/ 64lHdAO75OWONqJfLYEzHjgUstTnBCC/mMqqDinlmWxY2uVk85QDNFthcDDANsHB0CNw FCdw== X-Forwarded-Encrypted: i=1; AJvYcCXRYA332UOh5QgbLSJjcuzGJRd2hOiIqByxdWzMDUbzEaNTz/07TrnsB74eiAlTQkHVXRDBgMQJeg==@kvack.org X-Gm-Message-State: AOJu0YzL0a2lI3QzaV6mlVpizWX3tVNumOUPeEhVQoaoffO7rcUwmUBo ySOK7MJof3dZsmjYqJQvl0eiCZ55U1aKIJroesg+w4DEYCgEuf5L9LDh7Taxh2Y= X-Google-Smtp-Source: AGHT+IGBjJ2qmkhI/9KkSAaUQ4/thfZExOudz5gsftSVr6ezuggghKNqKZsYYiLOvkR07Zl8T47kbA== X-Received: by 2002:a17:903:2cd:b0:20b:a10c:9be3 with SMTP id d9443c01a7336-21183d4e6d4mr180460215ad.21.1731358464144; Mon, 11 Nov 2024 12:54:24 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e9a5fd1534sm9059974a91.42.2024.11.11.12.54.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Nov 2024 12:54:23 -0800 (PST) From: Deepak Gupta Date: Mon, 11 Nov 2024 12:53:56 -0800 Subject: [PATCH v8 11/29] riscv/mm: Implement map_shadow_stack() syscall MIME-Version: 1.0 Message-Id: <20241111-v5_user_cfi_series-v8-11-dce14aa30207@rivosinc.com> References: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@rivosinc.com> In-Reply-To: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@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: 600EC14000C X-Stat-Signature: sirtupxad4o7teyiske9myq7h36n7ehy X-Rspam-User: X-HE-Tag: 1731358434-519078 X-HE-Meta: U2FsdGVkX1/IWoOH4NoHK84v/Vj8oWfBMzTVjD8k+w5s7Je8d5cp6HYNQxxPh3gdY0KHthtDK6sSeNYDP9lIOPLH+ZO+kDDxIdr6HrvOwuUrqw2uWS7haVYOYW5b1O5TfTIXDMMNU8TnnYJAcQooxcnGQDOSTS1Lp1QrbaLAX39o79PT9iJ2T+wFFTO7AUYh/d40AIqdMOlOeGefK2RWvXYEC2D+9xOqsUCeAclv+Mz7UUgsk8K6nXyS1CQ4Aw06CcDacNyynD0DCbA09qKidcflNGYEXq2ka1R/7ceauu5jw9cr1qUqkh/oa/OoI29FTcjCjb2J2WOvhTf/GJ4A2X2Y0CvYYl7bT5z9lYo6KuzxoZ4rXQ0a9gI4+IaF14t1H7IrLBv2SKyb+bKrR5aBo8L2awnBM7gyF2wJyUoTicnkqhcCZq54v5WtjIIj3/Es/l27ZE8YX8YjGycOk0iwpCpKt4uJ8wtzM5TfrEmR8rikOI4amHS2bv+gnI7O2fAjeBgrZAaV8Z32b5IfzfCLaKRp/Pfu8VgzYkSSB+X240+Zi0gxnGxGxfYX+Q66lMx3oXyFZJ0A8S86MWTs32BQYGNttwcOVhHG1YkScxZpR990NmMfdNBby4J3Xcjnx9O9dSv/+UQrWgu0uLCYTf3okMe/kQQa/mcAkxHN3yViWMoxQF2FWZGJS3TP/4oaxhpK2+AHrWpYH1rQGe1QUojhPvr2sgorVj7YxSaN7S6jwrll1ZbQV1NHZXttwycxk4O0tAKpKO1AAx0SzeHifqzFZIfI+nL1DkixTRHOzCVeO7Ej1la5lKuheid8aGbp2h1t+yMTNHoNFfa8pdgLA+JrsF9Rf++5I6XTjGHn/LL03PG9qC1D9M92seXLc1KK1KlIVLSYhzcxcY6sidg2YBNKsGphljKlVKU/9W1BMuhBL8tgrSTZnS/Y7Hce8xOXhWyRiqqccMC7LcpBLblS+ux 2pDBTe8Y rO7TbUjYEX0wYd4poyNvQ7SUTZAH1o1IPWeVfpDdL00GBa2f75IN5yGdw845be/r5D2TbdIWXQ6Q7qSJENkTNlU/eNcfnuoC6KNAZKDOWaQkY54G0GkCQrIZ6bRxzdFHY+FBb4hVedXqR3RXeumXPBmASB14fsk10G31PiSrtloY3GPN1bwngzoecDITPmRFGReKMk1vI0fLmyqe7MvuvbGYr43iBvE5yVB5CZmRXQ0cgYcG1yGyuLcgaVQr+3wxCc6bNTR0U//2Bw0S2Y8xzfW6jfJKDlUnkMXLgaGX0VyBkrNPeofTeTMuPpbFVlxRla0mDv/xj/Nc0sGKTZJgahLgG1ny52z/X/+YIxb3R7naGZOR+soOZ2bgKJrTIz2zJPGkRvI59Gg7Me/e3pp3B2r1CU4mpqc50cdCtJIt1o0LHNVJIk0SZlLSyS/TxvBg0kCfwTJkh/gDoYt+q7/AOfLqH9AyT4xQkcrpR/L9ljK4M5RG21o2wmtHcBbFVYsYNNC/c 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 30db92672ada..3f5a8eafe578 100644 --- a/arch/riscv/kernel/Makefile +++ b/arch/riscv/kernel/Makefile @@ -118,3 +118,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 Mon Nov 11 20:53: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: 13871349 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 60705D3ABF6 for ; Mon, 11 Nov 2024 20:54:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EC7DC6B00C5; Mon, 11 Nov 2024 15:54:30 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E50A56B00C7; Mon, 11 Nov 2024 15:54:30 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C546E6B00C6; Mon, 11 Nov 2024 15:54:30 -0500 (EST) 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 9B00A6B00C4 for ; Mon, 11 Nov 2024 15:54:30 -0500 (EST) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 53A7B1C77AE for ; Mon, 11 Nov 2024 20:54:30 +0000 (UTC) X-FDA: 82775016324.12.9D85BA2 Received: from mail-pj1-f48.google.com (mail-pj1-f48.google.com [209.85.216.48]) by imf18.hostedemail.com (Postfix) with ESMTP id 3A0F91C0010 for ; Mon, 11 Nov 2024 20:54:10 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=mtmA7miy; dmarc=none; spf=pass (imf18.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.48 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731358340; a=rsa-sha256; cv=none; b=6wsi6fbkQHrfPALJLECTr8cRiLBZDX3CmS9fcwSo40F/okLKUdr+9zk1oO7iQ27QxUqibm PusppucZpYXrEod2ldU+ARcEb0cirxKaBlbGs8Nd179cu10GK0KbiWLPTDT7jupAXCOx8U YxyQYjfqHbbOPBQZpCkAuE5QbqPQhME= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=mtmA7miy; dmarc=none; spf=pass (imf18.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.48 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=1731358340; 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=RH5rxiNwtFqdQzt6ugWkK3TScTNeIYF9L+FeOjwkDo4=; b=1y/Cv/qoDg34DxphKj58WVkMxy4YZHHwNa7u2WrZ/ECrMxlDprobW6RlmqdZvNrhFoh2SU Ty0f+2AzM6aEQo5uRXdhsp6PsLC60WrBnFVHWiQBBR1tE0iGkNk4mIbl7yVaC95bwtZlGH Pztrx9ki7w9pOAz1ZCz31ihh3yrqIcQ= Received: by mail-pj1-f48.google.com with SMTP id 98e67ed59e1d1-2e2eba31d3aso3708833a91.2 for ; Mon, 11 Nov 2024 12:54:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1731358467; x=1731963267; 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=RH5rxiNwtFqdQzt6ugWkK3TScTNeIYF9L+FeOjwkDo4=; b=mtmA7miy0Rq9dCdTzAcse4e3SXeHRCp58E+m3LKM4w+LMiIR1idyLQ4RZj7rWkkwEt A3kD4VlsfokyUeYzR0HcML/PhhFTqsT6D3NgI/r3t37S+56Nue942N/0QW3d71HPszVN CXgQgvqrtt4tcxSDWLUraCg0JikXtvjHxB3ro2UWWntXIQL9QmhepmNGomDRK7hXjq0+ tnXbsnS4PxOpIOo/lGEAX4Y8GxdvQOz6BprqjPR++VSGyutLYFwPpaICmlRrWy3y2MBA asHo3R9miSV6hZD986DvAatmy7PRd+c4Mm6pJvcVmjTBjYKwDn+hY5q+uuTBJVSCAaGS NXSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731358467; x=1731963267; 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=RH5rxiNwtFqdQzt6ugWkK3TScTNeIYF9L+FeOjwkDo4=; b=wsrDcJRWITyRsvlZrTxEsLoB+2t5ibahDu40Pu84QzWiVqUTCnvss8+bEcVOIef8z/ T3kGLSeMXOCtE967T2u3IqAfQIhyhMvZ56ZbfTUh+35kgN2mBueTRm3k628BCm5fBGBi Yp1WlWTmXj42YkMQndajat5C2kLGMfdM1EQca8ak8lIZyt9L7u4+BIC37/+WMNUY1MQ9 zVK2FS/DPmn/ZsjXvx4YvFd9qmGFi+U64gbIFZEATx/DNRM64+bSKGPliA1tSej6jZvZ gFTI8ACLDTB5lynVzUUroddwv3V7G4ey1bZmLd5UgUAzlPQwPGt5m3OC0mB8bfbRVBbl LNVQ== X-Forwarded-Encrypted: i=1; AJvYcCUTraGpLZq1mtvSQCRri+XF2zd/ycY5shlGQ2yFU2pLQ20cwCWDijHSYpm24sW8aXEF8RG4mmYiBg==@kvack.org X-Gm-Message-State: AOJu0YxRy47hQ26scP3RPnA2xRA5LGtnfWmo2zyzEMu9A8tLOTV0+/we eiGREdCR/UO/94ZgjMfYtl2ABubSHWAjfe0ANpE4EUMDhzxOuz9iQNn74okNQ60= X-Google-Smtp-Source: AGHT+IEtP8frhYbRTRuF1KOavXy8aPf0itxkB4eaPzAkU1V40SKdq3DPh12v7ey506ueuvFbyGlpcA== X-Received: by 2002:a17:90b:4b4f:b0:2e1:e19f:609b with SMTP id 98e67ed59e1d1-2e9b177fc81mr18945649a91.24.1731358466840; Mon, 11 Nov 2024 12:54:26 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e9a5fd1534sm9059974a91.42.2024.11.11.12.54.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Nov 2024 12:54:26 -0800 (PST) From: Deepak Gupta Date: Mon, 11 Nov 2024 12:53:57 -0800 Subject: [PATCH v8 12/29] riscv/shstk: If needed allocate a new shadow stack on clone MIME-Version: 1.0 Message-Id: <20241111-v5_user_cfi_series-v8-12-dce14aa30207@rivosinc.com> References: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@rivosinc.com> In-Reply-To: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@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: 3A0F91C0010 X-Stat-Signature: uc3866ysrkagxgr7r89cndny9p7qx9a8 X-Rspam-User: X-Rspamd-Server: rspam05 X-HE-Tag: 1731358450-328614 X-HE-Meta: U2FsdGVkX1/LtJFuZdwf2f+/UgT7mJD8yaw2c5LVCYAyWTojOzoiPjgbJvkb0WPf905OyCMWojlopDnOm4ue825Ej38VLfy9l3vN12+CwiLc0TUbZxVALGxUUsJ21OrO3jVQOjS0xOLDYXm6X4aHNnwra5uhxv2GGNJ7X50d3ypPmLhNngd2DTZm6L7CmRXlucQotaU7aeSXNX0+N1Eb/kHALLuffc3DqCwv68qur6Ba+NnAIOiOaaGMDIRMLK+fBoXX7mR0yAHSz7YiSI0WcKJISQz0KZoxr5lVlAS3VceP0uea/+ZNRwDluMvc0700B2xbSifkUL7cKbYWVEGAirGeoVsP+ix49VACJsu0IJz/ib7867d3dni9L42728H35Gn7fsM6GhmUpyD0/AySy62ql+fGMlFBHscUNcA+rWY6Nmp5HEA9LAUpB1asKfdnjzdk1vFpmJRlDoywwI14cSl/Fmn24+00Lvd+c37os+0Uo2VHVwjy9gG+Qdp2naS2tVBXAMo7ujITjZrbXhQm2rgye+HTTeEchWaq2Sz4XbofK4MfGB11jhkdXAy/zoxqhmGCAAoa9bPkZ+FnMdbUl1bejkhdkIk767t2dK1VQfd6meRDY0Xq+HEWYa9UCoxcRpfh9qdpNJNePzZSNFjxvCSC3NL4Sc0M4ek7AtyKJje/F8YhSS7/pgUFJFe8aFogTt/EYgEpNpiSrPhaXJMbyae+N+LQNZLnzaFcoUlM0zPNchHpqnxO17t1yz89UFYnmQ1uaitJIaMXFanwszZ4PJAU2ssoS6nssw3xmffCIajzUgu0eH18RB5vkeGcJbrp0mIQvVB8THNn8YVbM3QMXhuXIVbA7aK6/cwNwNcgpCWyyuMyuMlHJLsRMUyBnFIpHUHYsErU8wPyetEEp3YZDr6EANse+jfYIMaXmnMbjCmAbjAxjBZmZBEfwIiMe/VxW5PLsA7kvGrDsLj29Mq Xur+zOIV nmgT7SVlyl6tJnhjb3m8/rA84EVW8Kv2zgHe0YnAWCiOueDl1KUrMq0jvzRLhl5ljzfGICES8+q4oeHL0iQtDwhdKFUEbWOH1Ae/T5is9oGUBx1zHUfqbXFY8YnJM1NI/vJMAzuOEyxYeWSP5X2s5ffFmrVgx82PK/GYlyyE7Z1Yhe82bud3ZpkvQHz+qB5a194nKkYLWoH8nSyTSPocIugJLr0fmwsZPA+mMztXGXuSvyXa6run8qp4WlDYEi/Kl8WMnnYuleLF0lBBD6ekKjz0f2GoV98HdrRgqk8HFDt4nSLOzoKVnDCUOVNukcUWoql6Ty1fbSdZuHwGz/qVRrQ2yUtU2GjnVayCBBAyUasdBObQk+4DZAqkf1mrtbdHgzoK83mBbYgmLwl/eaa/JXCqsvj14fyjxxQzMkk+hXWCYT2+sl5uIYSNhgUu53goKbXjpJZ+Ca1dLgIo= 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/mmu_context.h | 7 ++ arch/riscv/include/asm/usercfi.h | 25 ++++++++ arch/riscv/kernel/process.c | 9 +++ arch/riscv/kernel/usercfi.c | 121 +++++++++++++++++++++++++++++++++++ 4 files changed, 162 insertions(+) diff --git a/arch/riscv/include/asm/mmu_context.h b/arch/riscv/include/asm/mmu_context.h index 8c4bc49a3a0f..44d3843e8c14 100644 --- a/arch/riscv/include/asm/mmu_context.h +++ b/arch/riscv/include/asm/mmu_context.h @@ -48,6 +48,13 @@ static inline unsigned long mm_untag_mask(struct mm_struct *mm) } #endif +#define deactivate_mm deactivate_mm +static inline void deactivate_mm(struct task_struct *tsk, + struct mm_struct *mm) +{ + shstk_release(tsk); +} + #include #endif /* _ASM_RISCV_MMU_CONTEXT_H */ diff --git a/arch/riscv/include/asm/usercfi.h b/arch/riscv/include/asm/usercfi.h index 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 58b6482c2bf6..2e8bd30a2b49 100644 --- a/arch/riscv/kernel/process.c +++ b/arch/riscv/kernel/process.c @@ -29,6 +29,7 @@ #include #include #include +#include #if defined(CONFIG_STACKPROTECTOR) && !defined(CONFIG_STACKPROTECTOR_PER_TASK) #include @@ -211,6 +212,7 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) unsigned long clone_flags = args->flags; unsigned long usp = args->stack; unsigned long tls = args->tls; + unsigned long ssp = 0; struct pt_regs *childregs = task_pt_regs(p); /* Ensure all threads in this mm have the same pointer masking mode. */ @@ -229,11 +231,18 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) p->thread.s[0] = (unsigned long)args->fn; p->thread.s[1] = (unsigned long)args->fn_arg; } else { + /* allocate new shadow stack if needed. In case of CLONE_VM we have to */ + ssp = shstk_alloc_thread_stack(p, args); + if (IS_ERR_VALUE(ssp)) + return PTR_ERR((void *)ssp); + *childregs = *(current_pt_regs()); /* Turn off status.VS */ riscv_v_vstate_off(childregs); if (usp) /* User fork */ childregs->sp = usp; + /* if needed, set new ssp */ + ssp ? set_active_shstk(p, ssp) : 0; if (clone_flags & CLONE_SETTLS) childregs->tp = tls; childregs->a0 = 0; /* Return value of fork() */ diff --git a/arch/riscv/kernel/usercfi.c b/arch/riscv/kernel/usercfi.c index 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 Mon Nov 11 20:53: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: 13871350 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 686A9D3ABF4 for ; Mon, 11 Nov 2024 20:54:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6B3926B00C6; Mon, 11 Nov 2024 15:54:33 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 63C306B00C7; Mon, 11 Nov 2024 15:54:33 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 48F476B00C8; Mon, 11 Nov 2024 15:54:33 -0500 (EST) 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 2051F6B00C6 for ; Mon, 11 Nov 2024 15:54:33 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id D3C58AD72E for ; Mon, 11 Nov 2024 20:54:32 +0000 (UTC) X-FDA: 82775016408.22.E124FE1 Received: from mail-pg1-f177.google.com (mail-pg1-f177.google.com [209.85.215.177]) by imf19.hostedemail.com (Postfix) with ESMTP id B4FED1A0008 for ; Mon, 11 Nov 2024 20:53:39 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=0Pip85dt; dmarc=none; spf=pass (imf19.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.177 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731358409; a=rsa-sha256; cv=none; b=tcNaipmNzUoLe0J6UlrOWXU9rQavGQk7bKrwuAdt8RmCeFjZ2DRJVcs4x14Om8NezYE5Nk abM5c72xg78w+FNv50ddaHaVK+X5/6Ov360OJh00x+BvNf0Q+h9sq+9+c9WRbexdj1JbDP H00s2Sxg/LXtQKCNqWbjhRdLgbb0Uz0= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=0Pip85dt; dmarc=none; spf=pass (imf19.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.177 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=1731358409; 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=zttDuuB8Jg0F5HVXQ122SU8MUC6GU3do+W5lCjsZcck=; b=jvH9jcXOcodscEQsAlF2dzyI3aM1KCAkQxv638l1dEgY+9D8F729w2sXn1Y+lk2uwiT21R gEYj2n1qNPuUtfehME7DHMnAEJcalcCUUxq5f4QD1SwStTjNl5PnqYYBhT556GJc6XrtMV 1tMPmn7SGhU8bqQlRY4sB4+hxIeICys= Received: by mail-pg1-f177.google.com with SMTP id 41be03b00d2f7-7ee4c57b037so3563117a12.0 for ; Mon, 11 Nov 2024 12:54:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1731358470; x=1731963270; 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=zttDuuB8Jg0F5HVXQ122SU8MUC6GU3do+W5lCjsZcck=; b=0Pip85dtHawKAUyFlJvkwhyQvXXlraL6C1c73jqZCKtl51Z2TyfeheM1chEuzwGoFP pKl5dyWavuUTBgx2CBjEJKlBNQk9YKfci/wUDu6v2LHVCchnfu29PVTodz//ptlqAiUA 1rvtAkLEl439AJnwdm55MXt7dWtJgdEWQNngn5xY6zW3aJIDdUxKzjyexvYgSbuZck9l oZ7KsU+rxgBhMbPOHysz17xx2F3Sl2q+C4On0LG7TBDReDtrhf4+9yOUkjXiSn/lDLso seR1gp+LHhwUPylJF+PfjtEm5JE9hnALDdaWFmNHKSgOhLl2OmtY3sW71cjdETVzCVMz 1LeA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731358470; x=1731963270; 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=zttDuuB8Jg0F5HVXQ122SU8MUC6GU3do+W5lCjsZcck=; b=IWqXde4/cGmacWR4FjVOIkm58QYOYInr89rfoorWw1FWeKVh4XgcVITENlNZmilzvb 0zYLCj02r3/I3BniwIp+sj11ay9KMSb+x/LqCJ8MHdn9PfA7KLyEfCfxH67pVwq45D62 LzkM7ZthwoECvPGC5yg71KQZS3GfkYDUbE1S2UL6sMnhcQrG7MEmmUj7dKXKsVaqEfDX 7EjEpWDSuHvOW7x0JqTCaQRwCwctXCoPJMDtxKb7S9TqNHqI/yl9k+fhDAdFhjZbCASr ifuB1XmvU990e/vXa1QvBHcw0Dh6EtbTrOfpT6sbUj9A+lzJw2Mb6UjJPOAI0xlqLB+2 huvw== X-Forwarded-Encrypted: i=1; AJvYcCX2DS4Vf0PUbfNfP6Tmhp7H7d+cp2MAEIpJqpSCUhnT3wKYaqFy3DAQ6WI3Esk4Ioo5DFjd8WOjtA==@kvack.org X-Gm-Message-State: AOJu0YzyLpzYuoyiEEnvSevakDd5NB4k+GnqtqSvGRjnDXu1FgOlqjwp zbgZpHXcFpeV3fSXbYBAAftMhXzh4agZCSLSOEBBym9vD+d1yO5ThVxbyG/v++g= X-Google-Smtp-Source: AGHT+IFcd53uKcSM2vImgVUqVtCSgX2iqqP0I62SSblxY8DhVnmUdx3hmCD82D3gLR73X/oNRmz2JQ== X-Received: by 2002:a17:90b:17d2:b0:2e2:d3e9:eb33 with SMTP id 98e67ed59e1d1-2e9b16fcabdmr19214158a91.11.1731358469615; Mon, 11 Nov 2024 12:54:29 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e9a5fd1534sm9059974a91.42.2024.11.11.12.54.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Nov 2024 12:54:29 -0800 (PST) From: Deepak Gupta Date: Mon, 11 Nov 2024 12:53:58 -0800 Subject: [PATCH v8 13/29] prctl: arch-agnostic prctl for shadow stack MIME-Version: 1.0 Message-Id: <20241111-v5_user_cfi_series-v8-13-dce14aa30207@rivosinc.com> References: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@rivosinc.com> In-Reply-To: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@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: B4FED1A0008 X-Rspamd-Server: rspam01 X-Stat-Signature: 13kno3ifuw8tb3zaj7r7eehhwqmwedzh X-HE-Tag: 1731358419-12188 X-HE-Meta: U2FsdGVkX1/uBIP5hWRVmlYz4xJ0HMx0A72WLMgV/npvM6CV5tSuRBmIIztegHExCoWZE0Q9TH1daew2X1tuFyFyH0yOrCYv4ghkVpsZT26tinozUasVZdm4aSMbuWvmpm1QMl1LkWnJGTM4UbQN1aV+rnDSQMIbo5GU5tozUXSCtN4By+xP4BzWZrosFSam25gCl5bV6bKw0qMpSUAbGwmIxu0D0hlHdCAcYMMy33Bqp8Y1Vw38tDOA0k+cpPItcqK1yhzXjp3nOuZ2/t/M7/4pZeLIik59LrCdhMCoN9qx0EkKp9/SOmeDpwH+D4IiAjSe0sNO2w5erXVsosVZRVWAJYqPixPYCIGc2pk2Ugkmb6pugfzSwdDkYnpz1wm2i/D3TRnJ95Oa599L8MzhQG474ViKSw7BwUKm6nKyXtrCAUnOMyBU8nrqbv4mmy33Em6FxDAfqovUQ+e4mc6cKLYXBvxKINzmcK17D2TgxZMAgH6LMW9v9pimK78PUB7b0dM1UE5AFG7chml/6CKl3YmTfR9UuSolX3Z8XicvleeupwnnflWHLxJJmd9Y9wzP5YCxBFlQFmsth2uFaXP5U/+vuoBbFYKMIHoo41XJE+wnoCPSiG7Qltr8neHXjezvQS9CP9pd9jNz3uvmV3sFBBM+hJWU7k0VFywfh2ZHsH9orhQgmfs8x1lMIQv0VzIkfJCB3aspg7hZ025maYMTceE+CHLRy4yRZp7jIHyq6C9jZe9HqYJV4KwawVBqJxoPQvDmuWdy1/ZA7/5Bugdy9Bxh4SYT1awQPZWEpR3e76zNVTnVdqn2hsyfSEBnJqSGeWW+UzuoFqC+KDsRnw+S9ve0dm4UAG/oZXfWZlK1xH1Gc3gSvLahtaFNHWeT7TCbH9jlUok7D9Iac2XUN5R+Kh+5x43wEMUvv6mYlxeqzkiJFneGPfZ1NeLbc+kxbnTv5Va09asC7uryDg/Ah/Y 90/mkp8N 7d1ohU8sRSGmgyjBel076vYmuOwUSYTilxJFUJsEFaudVYqNdhsDp5Xvu8hdUb6CVKy3s3fcG6I8ryJb6YkWxBqChpZRVB+g7YJXYdG22pR66Z0RTBfHf6B8bwyVEM90kKQ2t2OuMxgm59eeaqHbiCEc2+vF5dJNV0wNUI5AXtFHXKGBGcTHWN0qu9JvgLgxC4mUqxOkmh4CH+/imE0yY4zp+8mkxF0L9xgfMA7qR3dCjRvmK2zOvgG9/32YtbKOfQ2iHjjrex7+8oCNr6H2gCmyXeJbBDliVjPbrTDnmHdeHC7zbVcSVkAT9KTHtuUFDM1CEwaCD+0OA+Lgz5K7RZi6cqhybY0JI0nhC09bygjy0N31O510IRB++X17qaW6OB5U1rHJZWNp3m059/RrR/20oc33DVpaIAHUHUjhkzRMqYRYTgi/9oSG6FqQJ6w02Xeu84/MfomYpEsqolFRFd+1kyO97JO2niH21d/veA2GaDjk= 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 2291c02c74d2..772516264a38 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -4147,6 +4147,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 cefd656ebf43..c42ed44e86bc 100644 --- a/include/uapi/linux/prctl.h +++ b/include/uapi/linux/prctl.h @@ -330,5 +330,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 Mon Nov 11 20:53: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: 13871351 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 4EA52D3ABF4 for ; Mon, 11 Nov 2024 20:54:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 296386B00C7; Mon, 11 Nov 2024 15:54:36 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 21B2E6B00C8; Mon, 11 Nov 2024 15:54:36 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EEB2D6B00C9; Mon, 11 Nov 2024 15:54:35 -0500 (EST) 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 CF8646B00C7 for ; Mon, 11 Nov 2024 15:54:35 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 96FF51A06D0 for ; Mon, 11 Nov 2024 20:54:35 +0000 (UTC) X-FDA: 82775017332.29.479CECC Received: from mail-pj1-f45.google.com (mail-pj1-f45.google.com [209.85.216.45]) by imf07.hostedemail.com (Postfix) with ESMTP id 152A440017 for ; Mon, 11 Nov 2024 20:53:35 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=GIgMcsdY; dmarc=none; spf=pass (imf07.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.45 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731358411; a=rsa-sha256; cv=none; b=fK6OqQZn9PH/KfI65yTkTNMVND+7M6hrhrTwr97HXV5QclSpSVLPBErQol6sus/8s34Bk8 Tj1yHuRcDXlnBlevx66T1+EhZ1UAsIz+CxV6giIoZHKh2orBkmGMNntugH7fCk9LWfmRBM cuRgmUPbw4GHpYjKtJ5WtcET3m9Et/0= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=GIgMcsdY; dmarc=none; spf=pass (imf07.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.45 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=1731358411; 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=tXX3HktdfdUDYxlxepoMs9/t12X7ubBzoX+ktyscBn0=; b=kKWDFrbZBxxBJCvpl23/GdKvU+k3mfspkH9a0cB0zexkZWXrhtJAhDMdcgNZLdIB7lCV9W 5KG215hJYAmXOEEGTSHjDuTYPvYp5+u7zg1EE98eDm6QoPNEM7TgJvRXY4wAi8krlntQzP 5SOEToLLH5wyisE4uSr9Z/BOMBGhHFU= Received: by mail-pj1-f45.google.com with SMTP id 98e67ed59e1d1-2e31af47681so3979518a91.2 for ; Mon, 11 Nov 2024 12:54:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1731358472; x=1731963272; 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=tXX3HktdfdUDYxlxepoMs9/t12X7ubBzoX+ktyscBn0=; b=GIgMcsdYm+rhuct+zOzoHDZqOQKoXjg7Hsa4sY+FQkYqkiHiiOOy33NJo7S+QVYZev tsKkEkx4L/QgOv4sVv+jbYZZqkLk4b5oIIRQgwEro7hQ+RTJ1hI4Hq005CR9+6jPTdgQ suFbcZEQVs29uwgW7SNGro6xeIlEXsrJmKzSBq9f0Q22R/ENB6UgSk/sEJ7soH7pQaUZ wookIBBPXcd6uKI52AkKESx9rFVDRdrL2qWU8Wz0Qvk9sxQq5gftJcpVlUdSOJbNXMEi bX17bxC6Ygjw4qXsNgkWd3rsuFvcI4svi2hDRft+bCmf6JXLHcvXvrdM1GViqq1kOn96 lCTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731358472; x=1731963272; 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=tXX3HktdfdUDYxlxepoMs9/t12X7ubBzoX+ktyscBn0=; b=L8bwql4T3chE91Lvext8CkbTNebVMqHkE8hZYi/tqT/LzT5AHOyGn5Amy4R+UlDtIy ei8APLuB70z2Ikk0+rNZr0aJ1Rpq9ZiYBmMw8jhtBc4MGwIX//BVFWT7FSyOwC1ZhEn1 t379axf79fKMYZlKNl+XguxDOEyNoLCYYWdDDEyh9Zior+W4TT+Hbox4M585JC8DLvSz afkvyWzWpjdcTzAklfsNgobzFhwI7r2AFSfMcyuDwj7iwkdEb1GCwXGuBCiqUOtFJtVc nzgNzaRFWEidciTAFT6ViYcs/mZ7Y6KL8uDHga/LTipn0/sBFmYzG0RKFMtsMPl8QaDe NBgg== X-Forwarded-Encrypted: i=1; AJvYcCXnflNeAyxXqqcgonrtfPzeBLgmPpRTLgFjntK3LYOE4bs4G6B9pw5ZYZSySvH/nKDoKcoCIBrVUw==@kvack.org X-Gm-Message-State: AOJu0Yxt9AS8SfzPws49OGfonbbiithqRgqhTqNXvPTBzG8lISkzKNJH P6L0Itr4UGd6U5WRNRhBe//P1nTE7ejKcQWB6CwFtt5ZczbkB0a1Zc/soB8VlIQ= X-Google-Smtp-Source: AGHT+IHq5Z7Xi/84uV65vfYB2LFlDXOy+r9mzEGPAb0M8IJd9siANoKs1hUvfB42yyVVA4FysR6vOw== X-Received: by 2002:a17:90b:250f:b0:2e2:c421:894b with SMTP id 98e67ed59e1d1-2e9b1682496mr17398074a91.14.1731358472327; Mon, 11 Nov 2024 12:54:32 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e9a5fd1534sm9059974a91.42.2024.11.11.12.54.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Nov 2024 12:54:32 -0800 (PST) From: Deepak Gupta Date: Mon, 11 Nov 2024 12:53:59 -0800 Subject: [PATCH v8 14/29] prctl: arch-agnostic prctl for indirect branch tracking MIME-Version: 1.0 Message-Id: <20241111-v5_user_cfi_series-v8-14-dce14aa30207@rivosinc.com> References: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@rivosinc.com> In-Reply-To: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@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: 152A440017 X-Rspamd-Server: rspam01 X-Stat-Signature: yua7qiko4tigmyxuoskztysi7re3tetw X-HE-Tag: 1731358415-750819 X-HE-Meta: U2FsdGVkX18HTahYMzbDMFaCQOaUiOpHW8NXg6/DT0tJxDnKmfJ1L6S/8GlUw6u9Ji418I7uT/Vugo0IldCAA77MDKy1tavO53ozXSOoy6si9M7G/4LCMsEoC3NcO+8vgKSrMM8SBlW2kqTP7Zx2TK3moHtJsJVjg6mfFkwHdz+f9YEzgfKsTbo5EmYg5+u0nueYVqN3aUGqyy3jZHy94J7eAAsyDGolBq6zxVbfzkygwk2mVOhYqixWUuLIFPpfwVLc6AS2ioJq3VHPCaYleH4e+vzjyqEj6OLFawXsumDyfGytnMrNb0BBkIfJo3zF1Oo4cNdj5ZoLRl0IlhqQijHyVlMCouQTTcTQC3WBDmlYbAU3uhz969PPDe/y+5bXaEiMb2QMwcmJySRCEO/twxRAyoLRhw6oujszXifNcfsrWOIfwuAfFkUsMQ4BBFXhZ39gIB3KDASKYk/xYlWj8R8of8Wh3V3SIVMD6Viu/JY/7B4C7Hd9P0h0q1Xo5Pm52BM+GefQbnSkuV5PCfYbFKAXybfI1UIyIi8zdyBJ5qmMnT7PlW+7PJVkcJ37Xf2vEOftuCspnzs4ei7YtqFxx7CK5iFlp+/LjZlQ3W9dVDpIinmC5LLoVBWvF3XcBEBmn6uPr8l8L5zM68nSWQ3jLLoQlqzfI9qYo4L8RaIYB0O4fuYJULvZzi2UWSl3qmt3JSzIdrvy6uNLU4UFjXZG/IvSUscqlNuJRGd8u9Z930zjYLuiphrWdIfX6vMdkXx3G8qkdOgk6plvwtML48XT3dkPUfEe9i4DduZfontFOJMzODV/BorQX+I3KRymdZDJSzUP0qJMcS0OwmcAN9y6CjoyBvbIeu74FJbgl4CroXhbYsbrjVC83R+j+m502KdQgC8290E2RUCGJRmLH8Sx0QEVy2kr+HjsB0yIqGoAeKncz6pVKM7tSECcR/xC3p0LZcBJIvWL+Olsi7Qp2mb +R3v6Vm5 6rwidcgga1LfGKUiGZ/keI49aP7EXjbjq8nw5qWItgEePn++uKnZKZVUYtLLeE/pUZVvbFtTrjcwy47lB3m8Evs86D2kjzBn1f6CAg5RH28URaQVhmtA5vsw/PUCJoR7lEzeEgLxjIQDwpYwb31+7Ai/KnYCqDiSBg9aF9ea7slS/qI9O+aZ8EclWqmQ8Ujwj8Dv2CgJ+3gu9d/AhMJPecq9aeDnJthah9uT7h/KZa/fQE3oJQWnCPRvBN93cJe7ElAko2ZuoJSQoSmrV9jCzjGuWt4IaeO1mcYmeNqtOKg5hpiyLKByT/12S6cJKrpRr9vBAy+N3fJxI8PEdDa0YdLt8hMIfDsCyoHKVdLFmbC1jOEA6KkPKcgRZ/h0Do74015Pa1nI44g7A+O8WljStPMVBLLMFWtQlZHKItJJDQkp88pAA0lURZs4bZGThSPP9sFgaR0eay4LA+uULCMP62U+9igzVXvOKSV52qYQSeIuo/9A= 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 c42ed44e86bc..8afd5cb70237 100644 --- a/include/uapi/linux/prctl.h +++ b/include/uapi/linux/prctl.h @@ -352,4 +352,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 Mon Nov 11 20:54: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: 13871352 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 30C74D3ABF4 for ; Mon, 11 Nov 2024 20:54:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DDDAB6B00B3; Mon, 11 Nov 2024 15:54:38 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D66806B00B4; Mon, 11 Nov 2024 15:54:38 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BB8A56B00C9; Mon, 11 Nov 2024 15:54:38 -0500 (EST) 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 8FECD6B00B3 for ; Mon, 11 Nov 2024 15:54:38 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 51D521C7781 for ; Mon, 11 Nov 2024 20:54:38 +0000 (UTC) X-FDA: 82775016996.18.4B920C7 Received: from mail-pj1-f49.google.com (mail-pj1-f49.google.com [209.85.216.49]) by imf06.hostedemail.com (Postfix) with ESMTP id 5E8D1180008 for ; Mon, 11 Nov 2024 20:54:07 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=qJXrXtGr; dmarc=none; spf=pass (imf06.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.49 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731358301; a=rsa-sha256; cv=none; b=EbkCWutl4W3YafCIYlt91C/0dX8HumA+D7kW9rrLU80wUyZlbqxp0yQNmibGdgGTzAzdBI Wexe0SF6FtDG7ikxXg79+ir0MinzLIcZIf6mnAGxytyBNJ/I/AgPO9PbdVU8Hsi6SW0rdN DgJSW6El7Izus/i/DzOMAfWP2vecdXs= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=qJXrXtGr; dmarc=none; spf=pass (imf06.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.49 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=1731358301; 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=Bc9do2wGuzj1eP6qZZgzxfwHTGGkfddAzVikzQI29pQ=; b=gcMZsXLPhfI7FYsSGk8qyn5dKvVV2SXic/ThCcX4Bwugdy1P0G2TO9LbEFk15eDu8zQ0Mr GSVkL6AGfKA92BpziX9RMVgCKqH2mYj7eM/IYTpk48buvKUN+JSEV6hL8z154kKCPYp5Be +wprQW4Lg2FKJtBNB4QKn+zxocmrDL0= Received: by mail-pj1-f49.google.com with SMTP id 98e67ed59e1d1-2e34a089cd3so4001198a91.3 for ; Mon, 11 Nov 2024 12:54:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1731358475; x=1731963275; 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=Bc9do2wGuzj1eP6qZZgzxfwHTGGkfddAzVikzQI29pQ=; b=qJXrXtGr+uk2SDbvX92lYFYXdtuO6dN4Nvn4zf3sdHr+rLTXiElpNmNEGYdqcwt3R1 Reg1rmldLVIZ1rxbuYDfkgK7hOAJZekq0asLMIG9qo42sh9deyvIRBkf2EwMHnreN+V8 voHBpnYu2tGkPLGLFLj4l1ZTr+hV+JJ+m7h2IoIR1kI09fe4b0Hl4Ad5xOjbA3bOIGm4 tlbwqmoJqp6Y1WBuEDHHzBG+e/bsJcBxcVGevW6QF1yGo7ATCSYGm0LEOuV8CXDolmVz QSpFTYFhKe/PRBWiQhR1mlUvUdX18QE5RLLXeb3BxXjOQv1BZO4XmJ9ImlyBvT7FfE0z svuQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731358475; x=1731963275; 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=Bc9do2wGuzj1eP6qZZgzxfwHTGGkfddAzVikzQI29pQ=; b=ZqO9ozYiFvxhi6CuwU2vlFIVwIwNtLVNOHuLVu3+9CPUnHZ0bVaRBNkJz87gtippaM NikjqKGjcten0OM+HusrZ5tcl5jUhkDxDOIhCvo60nmKfMmf/YuSMh4NVYZH5ad1cHh/ 37BjBXBkFpb0uVv21e/o0yI1ed0wCNN0Azw8cN6KnBeHeVlnLqTsOYxXU8ezpp3jzPic R2E1SwFGz2gA4fpx5nOy1XJV3WxRqgH8SHMrkpC6Jom07QVsF23tSwSBTSQw0Rmi83qd 5YEQrm8N0/c9Rb54sAxrLc6vwyEIVHG5DTZG8dGaku9jlexJVI102eqLcfqO1rfK6V1f XVEg== X-Forwarded-Encrypted: i=1; AJvYcCWsVEQwsCUo51s0waqMeT4Tq9GI+oi1ezF8/HL3IjGAezkJH3qnNKrCfZ+qEJYm3UkrndcDMKdFeg==@kvack.org X-Gm-Message-State: AOJu0Yy/lj/EHlBGE38TpaT3zEuCwazVBiEJsoW9kNsCG923rfmz5QjM 6w8IPRy/fcCXd2v3m+BMLp0VdnP5ghJXisDVjx6/ilau7fXB6gkRFXaKRlYcnSB7Rr7ZtF58U+W b X-Google-Smtp-Source: AGHT+IF4wkFd85cIkcI7NHWbdE7I1jkIGxMhtMNyVn10dQdo2iBskWZP06IO2hI/qj5cM/xUlknvQg== X-Received: by 2002:a17:90b:2ecb:b0:2e2:c6b9:fd4a with SMTP id 98e67ed59e1d1-2e9b173ee41mr21139525a91.18.1731358475186; Mon, 11 Nov 2024 12:54:35 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e9a5fd1534sm9059974a91.42.2024.11.11.12.54.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Nov 2024 12:54:34 -0800 (PST) From: Deepak Gupta Date: Mon, 11 Nov 2024 12:54:00 -0800 Subject: [PATCH v8 15/29] riscv: Implements arch agnostic shadow stack prctls MIME-Version: 1.0 Message-Id: <20241111-v5_user_cfi_series-v8-15-dce14aa30207@rivosinc.com> References: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@rivosinc.com> In-Reply-To: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@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: b6qzp1jd8rcimo4ui44yw74bon88ys16 X-Rspamd-Queue-Id: 5E8D1180008 X-Rspamd-Server: rspam08 X-Rspam-User: X-HE-Tag: 1731358447-395723 X-HE-Meta: U2FsdGVkX1++5fy1G/MRfvbCNniZg/uzxOXNra+zvj9vbPMU//PS3Y1HOnkaf3/Qu9HKNdlAO5K9Bqo7Xd7k+QdvnVmiHdTpzAZT09nTNQhfdL2pZ8/NFeYoJWykyqBJz62dcuYeLq4dYsMeG5uOw4TsRHm/VNHra31x8ki808IMrY8tYUpyF/3TxL++QeXHdlBrze/SX32Vs6cJm6ruwpUW9ZxxFGpFhJhdRd7UK/50pa0DWkWt93+JPdg0FJf9fFYXHbwwSVop4PFJSkhS18MCuK1zwv5yzIzKWwruR4CWRyAulUfmchSGFRyPqtgzhhaiun/+EtaSblZjO6qKAr1+swVCOFyfqILBA1BQ7d5ELZQq09oIFLS88CRugg6dE1yiHz8BjgvqDhX8jtGksfYAsPv8ccvJSQUPE2j/VChDgTOoQUZturJgTHY50tcKHV5WsVk/FyLMhXFeNAsWrOaL/qSWKYWPzMdJkuxFMvdZOgqSG513K3iNIkT44T7VZZa78SMSOXkiL38pOOcmW4+oiBtOw+5NUydnlcJZ5RVQPZUolQdEnPlDhZeh2JbVy+nfhRdRsCgC3ftjaYHE+o4qKJ2i9MRFcyONbhNIuhV8uNNwvHFaGsXUcLnpMv9NuFWEwQAcRjmfU/fuQ3RdnQ5mk8MW9457BeGhqO7BiFoMnVIVQbVjeDesL1JT6PC+5CkOTofxwVBqNykPCsJpjvMCx6B1JhKisHmJugCwyAotlOH9Ariqv4vUtFDclam07P37rZB18LSPTm09vrenOHyMhDdAcS5mj5jw+PspPI0jqdoTRGs4kxByvCd/tDFqKkueLm9NwOUTwCLRiS2nl9BXNNSthNm0wwtksxAgkOxr9wOkeVaQEzmh6SG3CyGhkulyN4FABhI6uldcOpSS5lkvsbjUXwvJMp4nAdzAlcH1ufVKuZ9k4G/uwCbSd87YkWfjDzpie2vJRG1QvPr Sqs4jDmw e2MzsaAireL7YfPS7katwOzQslXvJvmdNlCvGMcHC64nfUMbJp898Grxx/FUd6BOvfVYCv3Zw8gDWkSqG9+ART+o3KP6u/mngTlw3q0Howq49GcD1UquJU0NaREGAdlaPO5OMlC2RtfPbnsewa1iMewitH7FzmZYQ7dKZm5GDSRA8fyg8T80mHm/qPLIDdoffP/TVm+lQUyeBUJrI776E2mZ3NXO0x1VkrVw4I8AlVwmbqyERHEgTEVXmUW2RRMyjrbIjpTrYhD1GVOSqYiL8v0lmncNzsYvejezBSVwFspPQXbFSj+1kllu69Z3/T3nMe93jmwZ+TRWiziMx36VOtfT0cg3gJJRqAPDBOLg2gdRgHUsbLTlNXbqDqhUg2n20+0EM8NELcxDegYYxwKI5JwPj263g8XLkszm1Dr7TWi3AJS4ethNvpQAot0Cq1XFNMa/AROSsXVMZCt0= 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 2e8bd30a2b49..d75721b243af 100644 --- a/arch/riscv/kernel/process.c +++ b/arch/riscv/kernel/process.c @@ -153,6 +153,14 @@ void start_thread(struct pt_regs *regs, unsigned long pc, regs->epc = pc; regs->sp = sp; + /* + * clear shadow stack state on exec. + * libc will set it later via prctl. + */ + set_shstk_status(current, false); + set_shstk_base(current, 0, 0); + set_active_shstk(current, 0); + #ifdef CONFIG_64BIT regs->status &= ~SR_UXL; diff --git a/arch/riscv/kernel/usercfi.c b/arch/riscv/kernel/usercfi.c index 6cd166b73316..141ef70b08a3 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) || (arg != 0)) + return -EINVAL; + + set_shstk_lock(task); + + return 0; +} From patchwork Mon Nov 11 20:54: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: 13871353 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 C24B0D3ABF5 for ; Mon, 11 Nov 2024 20:54:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AAAC06B00C9; Mon, 11 Nov 2024 15:54:41 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A32736B00CA; Mon, 11 Nov 2024 15:54:41 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8AB976B00CB; Mon, 11 Nov 2024 15:54:41 -0500 (EST) 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 64E4D6B00C9 for ; Mon, 11 Nov 2024 15:54:41 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 1BC7681ADB for ; Mon, 11 Nov 2024 20:54:41 +0000 (UTC) X-FDA: 82775016744.24.C270F53 Received: from mail-pj1-f52.google.com (mail-pj1-f52.google.com [209.85.216.52]) by imf07.hostedemail.com (Postfix) with ESMTP id A24EA40017 for ; Mon, 11 Nov 2024 20:53:41 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=M+3hj9MG; dmarc=none; spf=pass (imf07.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.52 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731358417; a=rsa-sha256; cv=none; b=WPQ5U9yqkZljHQr+mB5i/GxxPOiQYG7ocY8VRzO7hCp75m3SSs43KWlsaoPpAspyVcIkVI sUJ6Hs1Xr7llqPr+NIjsPmac25gHpL5R+4osDwuV8jZjumykguEf+yOBvUU8V8JAiJW49m iXxA/dgDETiemYFBjUwh+aiNHkjrbGw= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=M+3hj9MG; dmarc=none; spf=pass (imf07.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.52 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=1731358417; 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=TFNPsAE/MCASi0DtuuV1hQ64B2wpIFg14y7h/q5gtQY=; b=0OqbHFcilSWgwTVvBL6x1cYQTTAce9ZcqW99KfUjUdFy+gZ0OszlYuDedqZvWxP94E7sOk lt0w5D/pDeAr4Y07J8XEgZRZ5NbnbhVWOOOHTs9a2EOg5aaIjfFZ3R408cWXEyQvd7BGOQ L/BBEmvkevlDZpv4KXmgQdoNFNUP+Bw= Received: by mail-pj1-f52.google.com with SMTP id 98e67ed59e1d1-2e2c6bc4840so3928185a91.2 for ; Mon, 11 Nov 2024 12:54:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1731358478; x=1731963278; 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=TFNPsAE/MCASi0DtuuV1hQ64B2wpIFg14y7h/q5gtQY=; b=M+3hj9MG20QpFGEbzFBGikGztMePZGoYUR2CQAvfFT712FXzFh3hpQvXYDHtJv10Ag th0dYn1Mv1TD/QviDtUQ9m+T5TmNFVzxCxXMMxp6UWesaS6DHJovfIWeJw+GUs0HiCFK zZhQf9D9wRYK5haiZgDwIIZg3gv8drJY8OJ24OZ3+CQKtSnC7fdutisbBp3GjDGdX/Gj U9lu/5Jqsnp0tcVUl98FqHyrtCsnNfXVz6PqWUJFAXKb1VmX7mzWRnm1KZo5xsrqD2DQ gLdAobhDt94U5CIGmRPi0ySMqmNIf6dxMD8kJU/zCtQn16hrHlXzjO35DA3mWe1MKgml K25Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731358478; x=1731963278; 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=TFNPsAE/MCASi0DtuuV1hQ64B2wpIFg14y7h/q5gtQY=; b=gXuyMB1/saQBlTYNSghhXNeuP1lmg6Dc0L+VU12yDWnYO+4ut28LhiNtguuheGeA6o FrQmXLd06+XrRTPfrhiXBD4QJHEwKbrlgTJLzCrIQ8nRdVVrT+OORw80CA2/3xWDfYIi 9ByIrBFma/N6pEiBttjrItTBOG4lpyyEJrtdjBrIA7hakafwShYpbeN/be/FFZ3rCaFy e1mg7VQEPOxuL5E58SaHWIzRv3zDbscOLStNy6RKOqWlLS8gRpKNKnPv6ki0wnLQv2A7 heI/F/bcbT6bwmETLU8t5qXjxQ1D8Y8OpqWj0cH4j5nHTOY46NoFe8suQTL1aOVM2lVb LFoA== X-Forwarded-Encrypted: i=1; AJvYcCXM6MNspn7musGYXQ/YM7nSI9LZwDL6ef0DhO1kbjtUv7zqAIdjBu9i1GrhoMbeUMZ/6g1zhjoDIg==@kvack.org X-Gm-Message-State: AOJu0Yybq0NlNentwFZx1gfwJouyIvbhchm1Wq+wrmFMNrtqPF6rYKwO Rr5R0EGkKybbCM7xyDmmeQ1baebgMtRzRAxpGj0VuChyKlXzwqH+fVz+qa2Uem4= X-Google-Smtp-Source: AGHT+IHIfomtohL6kx2t85fEx5d43nzdH+dIMS2KALBG0aydy87wm9h8k+dWkqI5x2dkp2kFgPdLcQ== X-Received: by 2002:a17:90b:2e07:b0:2e2:bd32:f60 with SMTP id 98e67ed59e1d1-2e9b1780a4bmr18571349a91.32.1731358477866; Mon, 11 Nov 2024 12:54:37 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e9a5fd1534sm9059974a91.42.2024.11.11.12.54.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Nov 2024 12:54:37 -0800 (PST) From: Deepak Gupta Date: Mon, 11 Nov 2024 12:54:01 -0800 Subject: [PATCH v8 16/29] riscv: Implements arch agnostic indirect branch tracking prctls MIME-Version: 1.0 Message-Id: <20241111-v5_user_cfi_series-v8-16-dce14aa30207@rivosinc.com> References: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@rivosinc.com> In-Reply-To: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@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: A24EA40017 X-Rspamd-Server: rspam01 X-Stat-Signature: wbqthoe19hfsrpey47onwcnxydsup49w X-HE-Tag: 1731358421-799734 X-HE-Meta: U2FsdGVkX184hIkBCuKSJUvIurC9dAhjpSFZ47jYLgCXcs6yz7aRLQGqEyd65FjjP8ivLN5DaeFzw4tyCZR6LLikYJIM84whQBzSjh86+5avQUV05YIca96Fh1+MTIpmcEhevKACb8fMTY7tEbUXtOxq3AhZP17n/W5ElmUCE9plxHpNxFyKU/idGFbnMcP/XXh6y8pimT7SgNRVLUV4GM6OWn2J9qDHVwoVl2S9BlOA44d3N1gTr4T4rh8fR8gVDIfKb4Mw4+595hBJOhOKIDLCtHgvOlmdq8j3OyaLypnho4/bJ1k2uJ0LO/nIrrhh8kEyKHczNjjkThnzKwJcwuLIK7vy/XiE47vFVY2mBixmygwTJOOv8u2JCgwcf6EfCHqcdEX1yW1vXSij4z93Sgko/Oi9XBdtFagJ+dqa+H/UdjeDeHrjGvnVMTHjKS7163f7DTuxhuvngzlOQWwj1NLbZrcR8dUPbc81eJgv90TvR+AnpajQOQ+g+cSXRxEJUXnm6lwcp4gt0AqY4vlU0HCtZN38Awmq3d5iMoNr6WN8LkLs/AfPSNRXc8WKSHRJ8ruRcv+MSeeKsUpU9DiORoOpGNcSA7ytWZf7EENOg/JGvr+NWJGhCpZNCl0SW36MVJh8D985J7pQi2RADT/u7YrdaAJD9FBKMxdlvXSokjYnu3ivnsL7bQpa6titM4j5yRAfxE0/hl9JHeqgd0TGoWPU6bGESe8DY/hBRsLrGs4BOniWP3cmECHaEwqmXZgBxdl/vw9Yvxczk1ZJbKignqR9FrSn6zIEE2GlSTjuKehfEGLOMk0W1jl7OI9CFFHZJrsWsCXkYE7XoOBOiEZTtTFm/S5eJLdzeNqPS1bxfctMBy6huMHXxM73LoqHWYrdZJ9sJXMKrylHpF69524BTZFQJW3S7vmSLfi5t/7+2FLrUIFTx08MR22fe5MIekNJd+0lpqcQGSRDyzSjji7 DS6iyTkG Di5DfgARUbvxUumObgYHgGrAB9qqxVH/X6JF3uy05wpyaeSUNSlHJmKRiXNvjw/LU+PTpUG/290yoH4uUPeKuJ+gZ2SbE9qnEQOq1LiICZf9b58xpyE6Xhf15jcKqd2/fpqykZsp+d4VCy36Q3+qcI51VbLe0XFP7tBIzJGpc8uFjyC7suP87HAeEnvlVik31q96PFdvbQnNkE0TkW/5/Qidbe7ep8j3wouu3r/3vZGHNDF5BSliMSUjTi+2wMstE+GBnjN9YLxb6zxubVocHoVXvxnLnt5BtFB1hlGWFe6sWyWsFu3Rb1OB5q6QUIyRVN6Wqp4mPNvpHifHGSkjC+caZCir2vfPiDT9vgTgKFiFEhlYgpCt9yPBttR+wN/lEnmIK1P9l4197/fRHhEFMfgvTmEFPT3AV+ry/XQHWEyA9JMll0E4OJgj9vYudA5EEOz2b9E4OLV9dKkuieIJlOwyr5A== 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 d75721b243af..226054db6c42 100644 --- a/arch/riscv/kernel/process.c +++ b/arch/riscv/kernel/process.c @@ -160,6 +160,11 @@ void start_thread(struct pt_regs *regs, unsigned long pc, set_shstk_status(current, false); set_shstk_base(current, 0, 0); set_active_shstk(current, 0); + /* + * disable indirect branch tracking on exec. + * libc will enable it later via prctl. + */ + set_indir_lp_status(current, false); #ifdef CONFIG_64BIT regs->status &= ~SR_UXL; diff --git a/arch/riscv/kernel/usercfi.c b/arch/riscv/kernel/usercfi.c index 141ef70b08a3..c5fa90711089 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) || (arg != 0)) + return -EINVAL; + + set_indir_lp_lock(task); + + return 0; +} From patchwork Mon Nov 11 20:54: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: 13871354 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 5FBAFD3ABF4 for ; Mon, 11 Nov 2024 20:54:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AF6BB6B00CB; Mon, 11 Nov 2024 15:54:44 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A31386B00CC; Mon, 11 Nov 2024 15:54:44 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 836416B00CD; Mon, 11 Nov 2024 15:54:44 -0500 (EST) 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 611236B00CB for ; Mon, 11 Nov 2024 15:54:44 -0500 (EST) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id DFD3B417C8 for ; Mon, 11 Nov 2024 20:54:43 +0000 (UTC) X-FDA: 82775016912.12.F25E475 Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) by imf27.hostedemail.com (Postfix) with ESMTP id 370B84000D for ; Mon, 11 Nov 2024 20:53:59 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=PaKMZdgX; dmarc=none; spf=pass (imf27.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.170 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731358353; a=rsa-sha256; cv=none; b=6eZ7NjgQoPGT7EP+kQZDBme7DsjESf6uhO/pk1f/deaDMmSTFiXRJ0rhwpv/2piessFfhe qqMiW50MxuVYl4YoMYiv7F023rBeeV3YUYvvUazImdYPoG0JP5pq8hI7v0JICdFVn6XEcy Tr9ZHJv5QcRkAhEWz8JF295Bogw1iJ0= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=PaKMZdgX; dmarc=none; spf=pass (imf27.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.170 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=1731358353; 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=uXq9FOpnEwBGdDHGF4C6R3WEUyw3maf3vhv8uLPc14I=; b=Nb/URmLhAD3fpMEmXOmwnNSVgvM8fH//E+xw2AqU5tvbNB/JpWC2TZONmZybiJRzMsDb4H wW1dbl2I+6fkkruxy6Y0a6dTQ3gXt9LVb0DYZkZC5fnLZVOrTJvrzu9YQwwQL9cDIFJuTl ajLe4ARQXnSQbYmPQctQbvRGYkxSp/I= Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-20cf3e36a76so50568575ad.0 for ; Mon, 11 Nov 2024 12:54:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1731358481; x=1731963281; 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=uXq9FOpnEwBGdDHGF4C6R3WEUyw3maf3vhv8uLPc14I=; b=PaKMZdgXBY3mYs57wh2KjxxMREAJBFWaSaltRM6s7cykPce4oQrQoSIjs31zSXfBVm raHEZMH5kNlWqOk7eGBYMDjJNhS1osROPqDCzNBTKZuJeGY6eypXkZx+q5jhu78VBJQ1 GOg1FuWQg5gxrWPUHk9nXH2UXpO3JGe94K81QbyqUsKnr7CsC54nIh3/MYn5/e6FYY8k //R2I5P6dOY7Ea3t20rCHnl/5NGpSMQrtsZYrl0RKLDqg11DfLbrRGF91eu3Yh5jliqs CWKpVKA4gV2fTgP4J+9rNSvUgod9PljThgwngTpu9S+uYEluCTXGBT11T3SdKQANx/9W TWOQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731358481; x=1731963281; 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=uXq9FOpnEwBGdDHGF4C6R3WEUyw3maf3vhv8uLPc14I=; b=Fp0JNaIW2Yw73/81qN+2vCrKoqQ3F9EBrEozQrnwyCCRFWj962onE+MgANH3e1JFUk DIj3/qCl+Bq/5zROEbbFCz/oCkRatB4bxnBK1oEfowScbRd788nHpxGHezYY0dWplQxs vYn/iUoUXTHpmt1en3eHtjZaa9/w9oSOS7Lwj9umqrvoEys15+yYr/Fo39g1Tduo6aXS owXVaONrdcXU9uveBIKaAyD3rUtsMGWop7tzP/wmom2vgb2SUZCfiTKlclRVJiQLF8WU 786L16nlQohOjxg2W13puG+X8VWgVtSZM0ZAGeSLwkGOk9aJ4K2i5uQm9LvPEr5XOr1S TfiQ== X-Forwarded-Encrypted: i=1; AJvYcCX0MwTUjZzn44PVZ3CWEkQdnRKmA7ea49rZyvkqk7ZiAMwvsv4DJASLevvPvpJSeY5GBYuHhMY9Cg==@kvack.org X-Gm-Message-State: AOJu0YzGQX/VO6eQp2XgXeWSokGi1GCIKZrL4vmpJnDP30svPvVQ7F1n LHwxLUs1HjnBbDjIdbg5+D3y1byHeEJmbMzVniyw+aqjLObKoJHo+5pFJHUhWvU= X-Google-Smtp-Source: AGHT+IGuaCu5TPxyLG+pHWtm3gJIshsFTNXGOdv+oe4DDvgdaliGfH59bsMMSDiiU80wx6Lj9jjwkA== X-Received: by 2002:a17:90b:2251:b0:2e2:b64e:f501 with SMTP id 98e67ed59e1d1-2e9b1740f12mr19141587a91.30.1731358480687; Mon, 11 Nov 2024 12:54:40 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e9a5fd1534sm9059974a91.42.2024.11.11.12.54.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Nov 2024 12:54:40 -0800 (PST) From: Deepak Gupta Date: Mon, 11 Nov 2024 12:54:02 -0800 Subject: [PATCH v8 17/29] riscv/traps: Introduce software check exception MIME-Version: 1.0 Message-Id: <20241111-v5_user_cfi_series-v8-17-dce14aa30207@rivosinc.com> References: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@rivosinc.com> In-Reply-To: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@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: 370B84000D X-Stat-Signature: tiwpodsgtkcjp9ep9torhhmubwxi1yyg X-Rspam-User: X-Rspamd-Server: rspam05 X-HE-Tag: 1731358439-120625 X-HE-Meta: U2FsdGVkX184JpqaxGxN/dBkFZo3q3iHDR3coVkGMFqJCr98ktZISvX0QmQlBhD+IcNfpreeQoyyUAbmwWT37VVtp7jF8p4bqG+0YXwYpyMdT9dnfU0PZ4LhEUUsaaMSc0UqZMAtZSmDyr4xSNB7YXzB5lbl1RT+YL+aA+QEoh921lVcJngY+qZLi2RlP/ND0KwX0Sfs6QxnM00UtShiTcZ6+ffRq6E8d+QWS6hzkPeKvZi4ZudmFxzcoU2A2zw5NdFQPQ4bxBOkxgTvZfV3ExizjUgMh/gYvpQRxNkuD8gD5ZGsq1fdUBXM4tRTY4VCetCu/DZYkNTWoCZq/5QddSkfMZj//pKLMnmLhC08ORZG6w9cBdrlJcoF58EPIRnotPJ2s2q02L0vl/pAGCnAPq4v9YMQZW4HzOwjioTIYMovknYF09lPxdcmqAU8eMcmdZYT7u+Y6t13Zo/RLYYSEVGggAILiHhZzR3hlMlaighZl1GOtgFcqpktscgBK/BnAhg7Rprf77TSTe+tGCXtBZHDqVpwK7D7MerPN84ye5J6w19VlYBtBbSHwSYEoZn4/NcDUgX5CO9zjTcPNKBHKiUsevIu4fJXNUA82qrQjELKRmc8hFUj87qu7hmy0XFOjbtK8eXyU8zIwbKDZ9KVrRo+6DTndrjo7aDtBH/nhr6VJa4ffqijbJDcmL3WXkfDj3MeH2gZgtuIZ2su8y8enWMPIdfsrhHayaWjss7rPOUowTAMk3WpXedr9QRtoz1iK+NmGxF5lW+ft4CARDqTyJlz/ldfSYcx8ytRqkDnD+yTdxH0H6mVqjg8j3FGatVz3n020GHnilV+eydXcHmhRnw9pO5y2tRSH/FuaUGlLj/J5byrp97vy1pOQYk66b852bAyI0A66X7Y1DcbC/w9ZRkqvo6xUw+mv9AE8IFsQZQDDrEvYXHGZ6404IA9HEg8ZRhCaW+ebwjGWRyH3b3 IylkgbWQ jDSsmAKLwJM7JPVsMZG/0p28GY+n8ExU36D9wdpdvFdm1bX0rH6nRDmMu4T1Euu7gU6ktu0sTM979A5V8H4QR+ytZTOdY48rdxHGJtJjZWEK+4CPMnjEeQGubLBS6Uj1RvH6ZNwIpHAYWvj8Bo8mW3X/82gt/SDBE7rONJvATu4JWoz1+p4cGAPGs79ydNfQisxFmh7iZhgYQgULgaATsgzeQ9HM7kkIq0OP4w+0SCxz4gohGa9GO2IHBrZEvLmp4m3Uy8N7G3y3wOkEtWBsdKRA+Y8GVgy+A2PMRpXgiR4tpaJ0ZzvBjiBytJdJWiM743pZifUkBDGegJbA00+FZU80Y0LrWh5ksSpUGZaxyyEOTKO1Z2WOCkKmb51jGoWAIJfAAZMlhCAxgWwegZCfJtn4x/0/Vu4k4uOtg1a3YI1wRWbrpDkM7V9Osn3ip2RLBaA2sGUg0ioJc6+8= 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 7b32d2b08bb6..0f6448583a87 100644 --- a/arch/riscv/include/asm/entry-common.h +++ b/arch/riscv/include/asm/entry-common.h @@ -28,4 +28,6 @@ void handle_break(struct pt_regs *regs); int handle_misaligned_load(struct pt_regs *regs); int handle_misaligned_store(struct pt_regs *regs); +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 Mon Nov 11 20:54: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: 13871355 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 C607CD3ABF6 for ; Mon, 11 Nov 2024 20:54:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 52FB96B00AB; Mon, 11 Nov 2024 15:54:47 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 43F2D6B00AC; Mon, 11 Nov 2024 15:54:47 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 241CC6B00B5; Mon, 11 Nov 2024 15:54:47 -0500 (EST) 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 03C986B00AB for ; Mon, 11 Nov 2024 15:54:46 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id A863BA1B1E for ; Mon, 11 Nov 2024 20:54:46 +0000 (UTC) X-FDA: 82775016912.10.9492882 Received: from mail-pj1-f48.google.com (mail-pj1-f48.google.com [209.85.216.48]) by imf30.hostedemail.com (Postfix) with ESMTP id 8963E80004 for ; Mon, 11 Nov 2024 20:53:27 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=0Cr45pA9; dmarc=none; spf=pass (imf30.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.48 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731358309; a=rsa-sha256; cv=none; b=A9rP7/Yg/iqiiOK3PnW1swYowTQeLTRajx08Z8IiOcIlZU3DVHECvvmd/kloQSgMRz7KCL u75+45Sco5y77lYYzXvV6Z6FhIF4gISOQlGUWcX+Ca37f/v5JeHVXMnEmqTPTQproFypwL xGGr92BswvayZ005HeUpZnX88cO+Skg= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=0Cr45pA9; dmarc=none; spf=pass (imf30.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.48 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=1731358309; 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=AiAoAt/atgDgHIo+7vUn0I6AlUMwVku2xnmdwPnM988=; b=zAIinxjDbVltPXwRnL308Kdm7NZt/NyeYZsPwMefDztt5sIgCTC83iOdFlfygDlLjgKBv9 ZbZhrUUCRZH0O+1RXaa/6x8q/rrDBX9rRW/fchp0gdcUjgkz9drW3xWckefT23VbAsTRvv YHLZZ0v5qiGtnreIOfjy0Z1TsEK8LtU= Received: by mail-pj1-f48.google.com with SMTP id 98e67ed59e1d1-2e2eb9dde40so3997794a91.0 for ; Mon, 11 Nov 2024 12:54:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1731358483; x=1731963283; 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=AiAoAt/atgDgHIo+7vUn0I6AlUMwVku2xnmdwPnM988=; b=0Cr45pA9ilQnHsXFWJLAkDwFDM3/JmBXavqKozq4RbnNcPOAbxW6OKPJLF3sS4sr+v kVK9rwTf1G2iK+mue1WGqy4dr70fH6YOeA541Yb7bbfuRO+4XaI9uaJcjWDKY8csSEj6 iK64TiuuADLLqaxYpcRSOB9xxDAlE4TsQBBt/ufUhyawqstGW7d+dOUeNzmtYo4Ecjc8 TVUHfdpJpaHy4EUe0UmBssXI/tuGWLp3UeOKWg/Hu59BIURY1gmiozQSXQLyH1sHRRvY pJw8fo/KJqIh7kJyvDONise8yBCUNVQzzlynxxfRah671lzIZtqEpqMCM/MDJAYGD0FT HOOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731358483; x=1731963283; 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=AiAoAt/atgDgHIo+7vUn0I6AlUMwVku2xnmdwPnM988=; b=enqI4TxZaYS1N6FhOzbRXLIXfj2isEhp9BB3kED9XNzo73gMJN3bZS8xYYkeOnGxfY a5VY6rqOR2RPF5bSW9OtHUu9JHZFlsh/iPSGGSnR8uGvTQvrI/gjyJ2JpGuODKSOB2fo m0ohMr7CKpbydEw9CNWPUygUkthoSaUC06ztYTMR5IbZYak78telKbrrhePrECa6180T BcMSsBsctct6OWJZRMgSovbQwMfBA2ocD4etpg6mhtyB4VsZ688S7r/PiJxfPFwU89JQ dh1TxvKfv4RDEKzGT7Z584zdakeqmSvSrA82BTDEtC9UJ6JNBdzq9/u87Qcn1Y6Y09j/ +q4g== X-Forwarded-Encrypted: i=1; AJvYcCXDxwRz2Wf/5Es5AfVtlJXDD6TOOmJVvEpfy93jIoLiiXdcTkL7hNLQYnmLtFsOL8j6f+/Lxly8AA==@kvack.org X-Gm-Message-State: AOJu0YwfHjekY9YK+2cR8sA5xKHhaSP+UorexERkEAOVuKK8TaXlXDXD NPDuX4U4q8OMcfgpsnlC6DsICKJbo1msEHHTWsUePOGn/t+MGfDIVi0VGPIY6NM= X-Google-Smtp-Source: AGHT+IG9l53EM31RiSyqGG37/k+bDpkQY0fPocWmR7TDVciLiq+WzkkxtUuwaAGl1gcAKVBTiyo/CQ== X-Received: by 2002:a17:90b:4b87:b0:2e2:cc47:ce1a with SMTP id 98e67ed59e1d1-2e9b1655632mr20237191a91.1.1731358483361; Mon, 11 Nov 2024 12:54:43 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e9a5fd1534sm9059974a91.42.2024.11.11.12.54.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Nov 2024 12:54:43 -0800 (PST) From: Deepak Gupta Date: Mon, 11 Nov 2024 12:54:03 -0800 Subject: [PATCH v8 18/29] riscv: signal: abstract header saving for setup_sigcontext MIME-Version: 1.0 Message-Id: <20241111-v5_user_cfi_series-v8-18-dce14aa30207@rivosinc.com> References: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@rivosinc.com> In-Reply-To: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@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, Andy Chiu X-Mailer: b4 0.14.0 X-Stat-Signature: i1ysfsz47emkootn3g8cxbjjnd7ugue3 X-Rspamd-Queue-Id: 8963E80004 X-Rspamd-Server: rspam08 X-Rspam-User: X-HE-Tag: 1731358407-877311 X-HE-Meta: U2FsdGVkX18ym8sl4zEdaKivbVaj/SqNWhreQiOeKngRZn8Jh7rFgAP2l1ekLtIYDViE+tNMSDr14yGq62i9pLgA3m5ibiLrsbFzYVscJ6t8HGUdVeMCzAokYjTqAUCj68sCKrKwtc1wCmSb3tQ+SEaidogmsf/rz9CsnJb3XRoSaO3dhppJsu87esNdiST71/4SeTE0ZQEbLeRWhprLZS1rzIu60P9gBG5VmeSv2nMDLeYylxnPTvhP1oXrDHZyV1/eLWjwgtnu1pj2y34tbv8nB1GcY96/3g3J8oXpDrF1u7cpSVI4iKsINSzPRc8wPur/E+LcjXvpgxIHAZeD2ducPY04GzoSdv2wSNTi9IqBrl0HJDdRYXXWrU/tX/I8NHWYrKv+JUfG+E/cAa/uowCCNuFqUzT56LGfu8dmqcef+lS/qFqJPRPRU50p+oQZXAsb6DX1KZg5vXm46P+aH4mmbEveAkYr4Fd13m3fkxDDwIDz9N7Xu/lhAf/PDyJpih+tHDQ4pj0lWQkHIj9dLiNCLMASExckYoZRDIJSGaWWYaGBP3FzjBam3gsXvuXFiaXRjvt/nX2BsRL6VDgSAUp3uoQuseedX8dF+7oG0hUqh+zg1/0TKuuXf9RUzTtKWmqMMnxBz2cnqPzoZsmFRdSW8HiT+Rshblhu0FqPW6JxWPgAVx1HiFdf5L743bqU0QGKTiKXTSavU+YiS6H2+N6Fczb57JL+MHeb5Z9NQQFVw9rotfSv7PUXylKy47ERpM/DzZvzbdPuhCLXVOyIp6IJZovge0GG67QeM2wyTto95SsLfWU9zyhQ2W7DjZu7YlG2J7LqUrLaTeTuY5emUy4MEUbIG5uQXfeVUHCu6KfVINOnEEUBuyEhzVNpL9pw5qG0ZlXSiaLhyZgfcERc08u0zaJPM3rVLjobW7Cgbqs2lkHrCpi0DG9oohPlO3YtdHI8xh+bSKn16uuC9Ua gejcDXVt hVWr90DwbpokQZdc0ufehxaLmTyTxpWudyAgpIqqdhNeW+c/F13h9n9n4y1vuT9AUhoE2HDDB/6nVoJlxPoNqJgdwDARdf9doLWtnqq7J6W2sMTPGvkm06csXIRNymmzQdhg9yoTVc9zN+uaKBL+R7ecfJGPI52UT1dd+w5WSBGiGppJsWPIlMbZ5CRPHS1FX9kgJBefkEIRQ6Jx/yi7j0lW4mHnHutzJFHNBdCZ1LmL9cjVTWztgFflk4EpmZ0767LryLtcVJlVXDPS+vxgINshVNjfI5MiRtH5RGxl0Xug0/Z0JDcxr9UYaEp74Gj5W3vexuZeZcYpQDQcEzxMU23g73zFWjlURU7apdW+v0v4FJ0mGEP2WD7LqjKVQGBSFesBJSQe2TqA9Yjx/OthDf/DgzhoG74t9ZcWgXFSoaqNdq4F4oV8skQI9lYBwU0TG/f+bbHBA5soO3q2gFX+Ay/COkgxnvlo2QDJgDmA8s5S27rJ+fDX05MZ7QdhIxV6HGCggn2q3U9ymzBahYRtl74TFgw== 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 c7c023afbacd..921b0fdcc3fb 100644 --- a/arch/riscv/include/asm/vector.h +++ b/arch/riscv/include/asm/vector.h @@ -283,6 +283,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 Mon Nov 11 20:54: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: 13871356 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 85579D3ABF5 for ; Mon, 11 Nov 2024 20:54:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 06FC26B00AC; Mon, 11 Nov 2024 15:54:50 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id F3A196B00B5; Mon, 11 Nov 2024 15:54:49 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D65386B00CC; Mon, 11 Nov 2024 15:54:49 -0500 (EST) 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 AF0636B00AC for ; Mon, 11 Nov 2024 15:54:49 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 63EAFC1383 for ; Mon, 11 Nov 2024 20:54:49 +0000 (UTC) X-FDA: 82775017920.14.F6343E3 Received: from mail-pj1-f50.google.com (mail-pj1-f50.google.com [209.85.216.50]) by imf23.hostedemail.com (Postfix) with ESMTP id ADA6A140004 for ; Mon, 11 Nov 2024 20:54:20 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=Rhja+OXo; spf=pass (imf23.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.50 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=1731358255; 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=z+JY4EnCyesBOcOYTGlNWsMV0NqEPsvrB2LCsJo5/Po=; b=6d6A0u3GSaKakt0+BBvmfUPHase5yAhu/Bd24+NlaUbVX3w5W0PjAtXy4TOYIG+CWkm6mG C0uMOtF3CgU6lVDx/RDNwK/jNPp9n1fB2xk0lh0+s0uWa022b+c2vcRaZUD1xYfjAhdFRA xVN6hKicm2enomm5CoDidEyo9kiUKUE= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=Rhja+OXo; spf=pass (imf23.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.50 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731358255; a=rsa-sha256; cv=none; b=Pfr7VwY0FsvwyAo/Y/0YJNMlrQQvlrGeiL/fTYpAelydiLlUPVXI8d4121lplvRDaxzCic 6eRLXh9lNAKCspkYKnaz6j1egT5kaq2GfzJv3EVxG5prblVcmm6qe3q61IHTGnM4IADYX+ L0i+xojjW0f5CbqVEnKDGhGbB7GRu1A= Received: by mail-pj1-f50.google.com with SMTP id 98e67ed59e1d1-2e34a089cd3so4001323a91.3 for ; Mon, 11 Nov 2024 12:54:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1731358486; x=1731963286; 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=z+JY4EnCyesBOcOYTGlNWsMV0NqEPsvrB2LCsJo5/Po=; b=Rhja+OXo4pMEzwU06qx4x+cYf4iIiPJ1ifR/CFSvLdMk8uiy6uaWPFpmAZP8EJqRee 5/aczmB2SYErS70YrLBIv2aukJ2GOCF+4NAS9sxEwGt3hHgemAPuDUbBiUU+xHAUfXil bCNhIE5nHlZKsxiMFaH0OAFX4q5WMA8i+hdT9iigKm04g32VlcEl01C0g1OfnjenrR6c 2yGDKkfsTCIIveF0uuKwCb8NF6SSlM8jf83xWGX78Hivy3DZhKMLKr0uSZNPJsKQUgaI 5m89ZdyIl3CtNBVWPJFnzfy159QCl0/Swj+HldH4nNCAWUnnKZKtCuwh7t/v8w3Njrlf HHNA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731358486; x=1731963286; 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=z+JY4EnCyesBOcOYTGlNWsMV0NqEPsvrB2LCsJo5/Po=; b=Ti5BLIMSntb6z7S+EvIAFa0N03tz7cGQdcUfjK2UQTFEUPYIQ4lexhSNAKBxhueRH+ PGCyn7yIlglcf3+seZK/N8x5sgdNfuE32n5OXFnA3SFyRrNTQNqsPKWwmrP4jKvGgrCS Qnv77+P9+Zv1t8AVpwB3ybWjO6Ygq433xsR/yUJkK0VfgHQXTCmeUucmN4HCfPLwdr7N 14HMYqPjAKncC5dey6Srg/j0HQnmELGqFgzghT6eW1iUyJ066iAhxeJJoOjwA8AORdAf ux0H9RDQyaFePLeMLoxkTOLHgziuG00/+2JZYPM2yd4/S1+6io/cBoNEA0qIE/CRyeRN VFsw== X-Forwarded-Encrypted: i=1; AJvYcCUqkIddNGSaRtOYRTCJonwapPCX9p/vrikhsxaNIDMbva9lePFYz5geYd20PjzreS4Z+N3TXMQbew==@kvack.org X-Gm-Message-State: AOJu0YyuY6BB3vv3JN1l++smHIn1akJGgrJ2B5AVb7kICRB1yTUuW3ll oGgnYbsbgoXJtSSTvhB1B/JJVV1P8hA/SI1T8dTGUje5eTu993oHbvwPI80E+RI= X-Google-Smtp-Source: AGHT+IHQ29budGld49oCAouGxSmWf/Ckm5wmN0jqWT/TwSKM8GUoErNvmliPWNSMD6jlTcKWcdiNpw== X-Received: by 2002:a17:90b:35c8:b0:2e2:a96c:f00d with SMTP id 98e67ed59e1d1-2e9b17415e9mr20023311a91.21.1731358486139; Mon, 11 Nov 2024 12:54:46 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e9a5fd1534sm9059974a91.42.2024.11.11.12.54.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Nov 2024 12:54:45 -0800 (PST) From: Deepak Gupta Date: Mon, 11 Nov 2024 12:54:04 -0800 Subject: [PATCH v8 19/29] riscv/signal: save and restore of shadow stack for signal MIME-Version: 1.0 Message-Id: <20241111-v5_user_cfi_series-v8-19-dce14aa30207@rivosinc.com> References: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@rivosinc.com> In-Reply-To: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@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-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: ADA6A140004 X-Stat-Signature: bi53ryw3317axyi1sx7q7g9d7j6b1uig X-Rspam-User: X-HE-Tag: 1731358460-76030 X-HE-Meta: U2FsdGVkX1/JFQ7vSjMFAFVGJ9jU58YajHqGBJsewnHPIfIol64RHKOoifw4lCcwDbQDxTftVDtkK03iu9XR+uAqyodOuPANuc+s7s+4QzaFpnsSqGlYX6o+2BqCzpEz5THlTDIautCc6U+rjpG3mSYA6r7OJ80FBJiUfTxnsPkP2HPNTZo9tWEi1pw6xfx+ysoElXp7xFaz5+BJu5USe26ofeI7taZVPfTV8H0HP6cavgopvDtgl31zPT/hezpqxz0DbLu3O/9rTy5uVmI5EKS6/hLlJXDkYwXywIwiIuGTFSwpDZk8indLNrVqj0iV/4yGlZYkf+2yJ6VXXUy4+F8VhDPO0tjZ7OVs6uSyc93+eC9tw/jRhsBr5leO6+IHeSvUlHzmsj8JVIVD6hn4q+kUJhXAN7MAimbZF5OZgf9OZyR7ub40alM/f/rSda66oaCylkehFr2UzWG3NgxBmGQzBxGX3HkTgfH9dr8Y9zWf3UwbqosIMjVWCpzRJOrsv3cmijMHQaYnmEqsyOb55436bp93tnYFxyn9Cvbtk4RZJyeTe5V3uqcsvak1MzMRPojW6oAj28ONrqG9jDHEHKL0TDR3a5LzjrXWChKf864vK92Y4MI6tucrbHP/iQnVgsTH28e7zVlG7esvEONwIRId/cADxiRymL3GzAfIoEv88IuesEPrJbMCO8MFpq4n6J/K3r+fIyggPThNXapE3DxjFcBbYAltlHYltXB651M5w6WkY0n0hFoeanKsKco82R9eXSlmWUelamP9FrIxWF3XI9GYkTwdvUVLoVZcsnEc5ogIQa6ZkYcU1JOGXi0MtYqXjSg11JaWXyad77/Xs6pES9+SVmt7GHvupJiW8P1JW5EVi9501qg+hm1hDhdcMNBDn83kL+6WfznsGWj+L90xvYEHFhYDFPbuE6vzNAAP9in6o40Syt/at/KZ2yN7f5ajKUnJ0Qd1qbRbQD1 lu3RChog jwCYOYt5utxVxMbs7PE8i1IBplzc6kCViYzYi7Ri8/modCbMh6TPDxiNd9qWtezNEj3ZDm8+OGPlT7ugwOuqAKLoaWpEzcK2zRhlWDqxK15ZInPTUofdS9JaDWgtm81y9eoP2hykQh+xEkIVXTojs29tpofomXbfSb2bhzvxJNt99K4kzmT6lHeAEghAAjojv1JZN5+WTIu/EpvZDpP3u58T8i1CfnWHcOXHOUNOaHRplnqAFaxgSl7rDVdtqsqRz5bIRf076dZdgtpdUq5+wju3HQJFmlHlE3uPU8kBKmWYgqFlbhjfagj7zldsUp4KHifYkTBnLa8BJHzVe2MX7M5VBQUqzAH7/K/3QBeBgCD3i2ewGDmJkuJm1V4fcJmbGm5iXtfip8paCU70YaMpyHl9nqyEJdd/Mip4PlO+IttrbT+JctbOMwSxT4ksg6uDQ8rxtUdT2IuGWtxs1XBfAUZY6GNxP9p6hsSdsygXzXv1c8vEpxWdMiTrdndJYE7jAHCnLRJEmffbrwqA= 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 c5fa90711089..04b0305943b1 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 Mon Nov 11 20:54: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: 13871357 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 516C8D3ABF6 for ; Mon, 11 Nov 2024 20:54:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D6CFA6B007B; Mon, 11 Nov 2024 15:54:52 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D173E6B00B6; Mon, 11 Nov 2024 15:54:52 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B6AC36B00B7; Mon, 11 Nov 2024 15:54:52 -0500 (EST) 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 8BFF16B007B for ; Mon, 11 Nov 2024 15:54:52 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 4AE73120D31 for ; Mon, 11 Nov 2024 20:54:52 +0000 (UTC) X-FDA: 82775017164.30.FD92870 Received: from mail-pl1-f176.google.com (mail-pl1-f176.google.com [209.85.214.176]) by imf06.hostedemail.com (Postfix) with ESMTP id 5376818000B for ; Mon, 11 Nov 2024 20:54:21 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=mNDrJDpl; spf=pass (imf06.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.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=1731358436; 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=LCvJScsVwtSSPvsUmMz3Q3/hMnyxUHsF6MHlE4BY4IU=; b=r0cK4M4iqBbs1kxQ9qY3U9MgxvhpOEjrFFciOc7NGYAoXSXw2tSn765Q6twQBe1BlEVcgg b8uDmllDprSsDvGsbZDgyepJa2FTeddiQhsDdd+mJWSsSxaBrrhykSQyIQaf6WOuLPOOmh 7we6sUKkBQi8AKJ1KfT+Gxe0I/XuORM= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731358436; a=rsa-sha256; cv=none; b=6PVt1SMs6z1nAh4hSShsDAseJrzvMAd5SOUoL/93oNwIQ3iuVUIf1vrel7HwCjgasP+R5H Fl3CZ1VtOOuJpTfvX1YEOWwI0SqUcTVDN5Kjc16lnYI4c/EDcTiq7r0KpUNv8eE3KWvMfg 8nDD77ZPgyM/vJQ2qbMNaAt9GzQ5tCg= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=mNDrJDpl; spf=pass (imf06.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.176 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pl1-f176.google.com with SMTP id d9443c01a7336-20ce5e3b116so43982535ad.1 for ; Mon, 11 Nov 2024 12:54:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1731358489; x=1731963289; 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=LCvJScsVwtSSPvsUmMz3Q3/hMnyxUHsF6MHlE4BY4IU=; b=mNDrJDplUL0t/fr4ltPM6YJJ/ipoBnUaiohKwOIHtkrsfgETUglQLD59psDxzTT8M1 d2uFutd5xbhvkpnxq5EHAntiuRy3cA9yrxvi7qpG3G1U9bYp70tSRcWNjX0Qu6ZbD1dN j8eLg30QtSJEn3J9HcsN1F/pjzKDcXHywNLfCd63FIMiwiUMieK/oQnugYyeVASNh6BB LfRojLpH5eaVbQx8je9VrCGnsRu20StwYa9JeBlmV819Vgq6gqng/17Cb4n28BL09LMe IKkOaC8vDmwVsrJT90Bbg3vMUwkv7D2OJy5v2BNbT2iy3MvC+SIwQ7Sx0/9wrMyDpjcl acDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731358489; x=1731963289; 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=LCvJScsVwtSSPvsUmMz3Q3/hMnyxUHsF6MHlE4BY4IU=; b=ViED4HD43Qbp9kQcxn40lOPjeJQtT/Y5mU4xBNwDEmhaz/BRrFLll4fH8UKBOdP6Vv xXxreFl5LzSRUyUgckLdvQ1w/R/5+7gxSQ/Cb1Y+Nkr80bBrHdi83UPzo5TM+UroGo9D jbQb46yk4Vh0hEC4kF5pIhUP+Bd+mbd3P+yyHHDafN0cn0iE6aC5kwq4XQK9S4RciOBx QVTthPgoPfbV91GlN+9IMzZLBIwbbpKqCzCpXWc8q4CfhU6Wa0O49If54L4Rondw4zcv UtH8iZkFLN2ScQxFliHYVxqkvdQ8m3q7JKgwzywrteVdoKTt+tPSxeIGV+vaHvTStNIq cr/w== X-Forwarded-Encrypted: i=1; AJvYcCXlDY2fV2WbKWBn7G2ie1GzbsAd9HlZfyfYC7oFNmMBKXj8PjeGbRkC6VHen3rpEHPjTPdS6U+yfA==@kvack.org X-Gm-Message-State: AOJu0Ywjg4Mq/XDrCs6dDmHgPkQfAhiAeCzo4EaRBPjxW+xY2qjWWvnr ugKVveYzo4qzGYPCtVExHe3Jez1dBJawp7JXVMldtC7CBHlK4HchfCE8hNYB2Pw= X-Google-Smtp-Source: AGHT+IHDimCQOVgf3cz6B2MDVaQCn9zqN59aHOrw12G3nQFTvSfb8MPteRYBnMY91dT3qMMvMiS9YQ== X-Received: by 2002:a17:902:d482:b0:20c:8f78:67be with SMTP id d9443c01a7336-21183e11a8cmr198773145ad.40.1731358489062; Mon, 11 Nov 2024 12:54:49 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e9a5fd1534sm9059974a91.42.2024.11.11.12.54.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Nov 2024 12:54:48 -0800 (PST) From: Deepak Gupta Date: Mon, 11 Nov 2024 12:54:05 -0800 Subject: [PATCH v8 20/29] riscv/kernel: update __show_regs to print shadow stack register MIME-Version: 1.0 Message-Id: <20241111-v5_user_cfi_series-v8-20-dce14aa30207@rivosinc.com> References: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@rivosinc.com> In-Reply-To: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@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: o9wsx3ghrth5q5dzrpofz34egobbx6xr X-Rspam-User: X-Rspamd-Queue-Id: 5376818000B X-Rspamd-Server: rspam02 X-HE-Tag: 1731358461-196539 X-HE-Meta: U2FsdGVkX18NMUNz7m/l3C80PlowK3P1BCf9ICh5QjgGFXLnQ43nMTgr7stxyTA04O25Gmks8VvojJ79GaXuLfU+qpyUkEdl32Pzac7aC+oDoT0esVoR374p9XacPKitnh3ZCii0Ef2pO+PIlkZa761e6xxgUvX2vthCO4kEmLMONFqLllqz8H+jUg85p1/0sSMhzzCOJGgpCqr4uB9UYzJjwIr3wiVGIgLLJX84pZaVEHVarQYyXeQqPzJn0rhzHzopnGHvXCuBr591kHoX7zOFyZC8asaSrbaZvKwJ95Buxqu6ECn6o5IoJPiGmY16Vm1jy5jaOZD6IM/vjd30RuAEDMgpHywvteH//+2SH8KVj2xeKHL/+Xn6/PEMastWqhUXDub1vFjYnn66NJf7DnagmDAgp3OAfrxEtts9+8rhjQdru+sUNq7pswbi9DXaov4Nct4ZJb0d4ff20x7+OEQRTRq1JhScBUgq6g7IPWx92kXHCg999FduN/sVZPMCrKuaLQLBvJpDr8OJfO1Qw6P41zs+PWyC/qLaIAagnI5PL86hTk5CalibLjNUz0LcAQUOfWLawL7xEhP/n+1EumtURlGdAhZAE4K7OpvyCFr3lDH30A21StM589Rzjr4RFhwJ39XUMRMKun7Mjv7roEDzdnlA1kPgG/NI8nfvTuRBIbwh2TKCqI5R74Jqy6HhTfxSjCkioLJ6CYP6XYmgaddnG7o3QnGLLg6JIMLgHjbVXdEDHaGlLCgPtycgxiIZdU19RSJ1LBtrW3pcOkPULNlvAEZK+8bVArA9sPTv3FTIF04832joBP9ZfZBiwueACwvbCeWvNY0GEz177PaMsH6wOdScfo36ouUPc4iv0fGzroWqTbN8Bv0XkoJjt1A1Inq24kmO2JsDv+2LS3hXwTzDVYHWVdulFPincjMSu/oS9++N8/GoZm/rHKSP8dXeCByC47aI3BmAh7jWtWK llRGGLkK H7mPirIeiF2tlGW+yBZkvfeFoJSrRLnIWti51ySXs1mpEyJ07Dz3fkm48EqQXo1Yj9oPWmTfJ44lAvNrFuim/PscBtFeTlfXOMPVQ3TRxxU44zarYP7GFO7SUJ2pyPBLnYdfHDKIOHm/g4A8d1G6VUU/QNJCcRUeedpoShMpn7J+pMbOk5BSZBSvDENkBdf6Ge4T10jm8qnPzKOnGEaTM56WMYeQDcHqsTkVDnM8qwkrjKKC5UmfDfKwbCDd58XLMfPnlVCtWM49CHoTgxhSSKAU3E/o0OinZ4WHbmua23QIWAeL02dgAHcjvui3UOJtkRd+a3l3siK5RBdxnDbPb+Mcr4a34fTdtT6IeTL059hP9xr/7jXSDge3Q/EALevnqvuJs9bVkvOiSuMVjQCfs66ynxVxr9lG1B3sUh5n3pCGeOuHlt/IQ81swvDWFbbBE/zEWcRqCV3UwnsCvfFQtqhB3q6NEWVK/X03QE2sA0BboJd+OBMdduU32uuY3NSJqhtef 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 226054db6c42..1aeb5c08a3c6 100644 --- a/arch/riscv/kernel/process.c +++ b/arch/riscv/kernel/process.c @@ -90,8 +90,8 @@ void __show_regs(struct pt_regs *regs) regs->s8, regs->s9, regs->s10); pr_cont(" s11: " REG_FMT " t3 : " REG_FMT " t4 : " REG_FMT "\n", regs->s11, regs->t3, regs->t4); - pr_cont(" t5 : " REG_FMT " t6 : " REG_FMT "\n", - regs->t5, regs->t6); + pr_cont(" t5 : " REG_FMT " t6 : " REG_FMT " ssp : " REG_FMT "\n", + regs->t5, regs->t6, get_active_shstk(current)); pr_cont("status: " REG_FMT " badaddr: " REG_FMT " cause: " REG_FMT "\n", regs->status, regs->badaddr, regs->cause); From patchwork Mon Nov 11 20:54: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: 13871358 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 16EFCD3ABF5 for ; Mon, 11 Nov 2024 20:55:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 99A2B6B00CD; Mon, 11 Nov 2024 15:54:55 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8D1726B00CE; Mon, 11 Nov 2024 15:54:55 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6D5A86B00CF; Mon, 11 Nov 2024 15:54:55 -0500 (EST) 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 495026B00CD for ; Mon, 11 Nov 2024 15:54:55 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 04547161756 for ; Mon, 11 Nov 2024 20:54:54 +0000 (UTC) X-FDA: 82775018130.29.37FFD06 Received: from mail-pg1-f181.google.com (mail-pg1-f181.google.com [209.85.215.181]) by imf11.hostedemail.com (Postfix) with ESMTP id 7AD654000D for ; Mon, 11 Nov 2024 20:54:02 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=qfYei0GY; spf=pass (imf11.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.181 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=1731358351; 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=GP0I58BLUE60FYcCdRZWgegjor4Rhw7u4n7VUbDEgHI=; b=zqDrRsGaRB1DryG07EGXrYVP/Bc/lrPYDLaThmGjMvepYkotkYPk+o9kyTpWt0/Wy/0W4z q77HRVeAHf8S5y1yceM+1y4yb3VPpLoCSabe2sKG3jlqxsn5W6pCnpQtkI8/qBqpcW91LY Yfv7fPG8useQncCwA4CU41DtM0lXpKA= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731358351; a=rsa-sha256; cv=none; b=knC3qecWVD0D78Riv3RiHlB8+YNf+OJIkb3VIgZMQaoX7Qgx68PJYFkaUUMhAKVb/rgf1C 1XQsKWi8ZnTo0yXYUpyvrtiOrDRWHNjYMA+0yV/aFUfmftSATUbJMa+YCDLW97h0U1szXu ic1Rxj77wxrpEPUnSRGGm8AEx39poF0= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=qfYei0GY; spf=pass (imf11.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.181 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pg1-f181.google.com with SMTP id 41be03b00d2f7-7f3f1849849so3350467a12.1 for ; Mon, 11 Nov 2024 12:54:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1731358492; x=1731963292; 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=GP0I58BLUE60FYcCdRZWgegjor4Rhw7u4n7VUbDEgHI=; b=qfYei0GY1pKogG1Ate61p53FEwexdgsz6kkyOxQi8J/yqqEfXbmtLeXScI8oMzZbkM UUoBPAobrdW4FkokU7SUM23ttNMoPRkl3VlEtfvJUjCPV097aiRZy9HB9hJcNcvvPEnk zvK0yE8MHWuO+OX7u4hxmJ5U54Lkli1EiQ57Gf7ut/6wDqjYip8ksPM4GJWwdl9LxbHl Tgx6H/22jfP8sficC7bUr4ngH6kM5XDCdmrhig1hMQrMj/Z2OLC2OZLlEopwRxF760BP WDxuhWkKl/rZpEKFLA+iHkJkOzLUBkPjwXY7s6Jxhx/ZUMMc+Yl7EZQ75X6nApe8T0yf 4lWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731358492; x=1731963292; 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=GP0I58BLUE60FYcCdRZWgegjor4Rhw7u4n7VUbDEgHI=; b=lc0GLTXiva6Yp71R+jisItJ8OY2RvYcS4rX4DBhMwz7HKrHDm3njGInoX5e3s4QV6d s4ireDjoWWANay4kHqBEt5uo7cPSYn89b/hoDA1VOpgzsipt7PQwNrNQ5jK8PdDCYEHI fVwEuwwaXpnR3tJzZj27zFlO5pjkR6AZMiKI+m9BHMkTbwj6QoV8nm+Cqsm8bGNfsL+/ 80eqe+F19690zE2KmmYPvvY8pJ9vvaon8H1tmzphFCW6xJWSHE4ycw5bbhRHro8N8f7O Wb6NGn1Thkhus2kaDRfzscDk75BPA1MjTDfcaHwdUYCyBLBOwiWiW9GYnSGw/x53JABH zREw== X-Forwarded-Encrypted: i=1; AJvYcCV+mB9FFpOAQglUzQwLumMzxftSWZVpqfkxtJMDDDSYExG2O6Q6HInttij+sH8DkM8uNRrARQ/8qw==@kvack.org X-Gm-Message-State: AOJu0YwvqL3CqaPX30zGsLDTM1DNTy1HPHIqonnZfPp0kmpaCVF7DwfE SSnhZzrb7i5N12NRLi7XC7TJZxDQbJlW/VoL+DsmVYLkK0qKaP0f6/e2si71GaY= X-Google-Smtp-Source: AGHT+IEIBfOZ1jmuiPm63k8prxDErgx1l4b0Xg9s/9vxhWpYcoduBzr4Dyzk8y8uGrWaFpzc99rxZg== X-Received: by 2002:a17:90b:4a49:b0:2e2:92dc:6fd4 with SMTP id 98e67ed59e1d1-2e9e4c73b28mr403622a91.23.1731358491799; Mon, 11 Nov 2024 12:54:51 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e9a5fd1534sm9059974a91.42.2024.11.11.12.54.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Nov 2024 12:54:51 -0800 (PST) From: Deepak Gupta Date: Mon, 11 Nov 2024 12:54:06 -0800 Subject: [PATCH v8 21/29] riscv/ptrace: riscv cfi status and state via ptrace and in core files MIME-Version: 1.0 Message-Id: <20241111-v5_user_cfi_series-v8-21-dce14aa30207@rivosinc.com> References: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@rivosinc.com> In-Reply-To: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@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: rspam12 X-Rspamd-Queue-Id: 7AD654000D X-Stat-Signature: t43qk5euuwkh7zdbphqcoimdgic4ri6n X-HE-Tag: 1731358442-481269 X-HE-Meta: U2FsdGVkX197mYREVeaJKavS97bmX87JwAt8qWT26llzwVkwU3CcxgEe+uFkvqBlQX4FYH1ZmRAi3x+GFtg+CbR4vGVZLMYahCgRh5U6gfgwEvcOErXsfd5CB3N1GGCp3wlkBJdtlApxhZIYkOhTG5s9Wo2KlGTfmrnsYd5ZoLY1wYDcwvAV3xhyVmiFJ9HFHwFMu1A/INxD0XsLbmcMCjf6z100rElTNgTzrVdtJBcOc2JJwrmYgvTjXQcAaI6FoiJkQF7t27UPY/m0nybQLHbW0AoUe+bzVM8TOe+/gpYP0tulVCfHu2nXrI2cbjLjc1SWPBtMEQcEVJVRniq1GWRsTT35F8rK0BjMvP8Mm9ZFIP/8VQIfBaDi+H7P6i/l0ZZWMNXAxo5ttso8dX8Ww7xicKmQbh6fiDjp5sAXcP9+RokXUEmFO2jniyfLazLQFr5NnT05yrxCnm+VhSMPVe+h11MJJY90g0bm2aqz87ij0jPzNu6LfwTpSAV2/Ck396xa0boJ9m1+veyVRDtgsOOYwa4Y4HawTY6C5/h0qnuQakNdlq1FJz+n0RFpyZMWWYSRyliq0JyPn6ySE1M3ahk3YGGogUtcBWsSrpAZkJjTUg80UflPc0UbFovJnIQis0vODV//K2U/3BczHsAI8+4wAFF9FH9oXnZIiy0rOtEFiwxqTHyLQeMPGJbV4Cl1UBc1g9JIwndaXPTqme9oZgeml9kk/1uvqg7rygrOz0f4qV0ir+V4f9ZI0qFWhSFDJCnmt34hbbe+Lf26R88fy+4dZ5vSETCV1b9jsTBwccDnpd3mWFnWovusDbUA8X7Xi+gmVHkfuq9TXCZ4YW9MnonR28TkgziMOQbKTOdLUo3L7+DCdT6lQbEH3Li4MR2wxKmPrhBqjYiUDzS0iGGBvSY4/J2GYz7NuzwTPoKItYwrRgFYt06wGgrxYtMI1XiuwCu2djA4FrNGAc4dqqg ePkPsf3J l2iO5onOoedU7wOp0ZqA1B+mzvFptYVTuTIoNy3+hS05EwNHUSToCu9+rAXedU9vvdxHAyid0OjRRpnMTn8TDIqmVmzTsN619U5aT0YnmUOo6PDdTIugPF+60MEa3W0FOrnhJmvOR+pk85lzlFUK6ggkmt+WZYfWu4jyFJ26R1yDhV+o6TYL3j+ZyKZIu56q8l7H0kaR3TAWlMJQCrPu4854koJN4s8vDi3bSIcgmp6JNcr7+q8WhPvYU91OFQeVgC7ywjzRYs1itFQYKBB5Q4ZmVoKWSMA9iYfmYked9NphLnyl1q2LuHyEI2OKb4dNYgtp9PxTebxoY1+an9hpsKkUCbkuJENUtjThdxXq8VBGp2QSKpnvQtbMVeWszTiKor4qxLrNGHisEHEfcyXLxI1kWYYYqczpS2I3eEVFa7hpFSFSAHp/8Rfin2LM/4sD0SOryjFgv0764MITvUpILy8pqGA== 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 ea67e9fb7a58..1c546c01715e 100644 --- a/arch/riscv/kernel/ptrace.c +++ b/arch/riscv/kernel/ptrace.c @@ -19,6 +19,7 @@ #include #include #include +#include enum riscv_regset { REGSET_X, @@ -31,6 +32,9 @@ enum riscv_regset { #ifdef CONFIG_RISCV_ISA_SUPM REGSET_TAGGED_ADDR_CTRL, #endif +#ifdef CONFIG_RISCV_USER_CFI + REGSET_CFI, +#endif }; static int riscv_gpr_get(struct task_struct *target, @@ -184,6 +188,75 @@ static int tagged_addr_ctrl_set(struct task_struct *target, } #endif +#ifdef CONFIG_RISCV_USER_CFI +static int riscv_cfi_get(struct task_struct *target, + const struct user_regset *regset, + struct membuf to) +{ + struct user_cfi_state user_cfi; + struct pt_regs *regs; + + regs = task_pt_regs(target); + + user_cfi.cfi_status.lp_en = is_indir_lp_enabled(target); + user_cfi.cfi_status.lp_lock = is_indir_lp_locked(target); + user_cfi.cfi_status.elp_state = (regs->status & SR_ELP); + + user_cfi.cfi_status.shstk_en = is_shstk_enabled(target); + user_cfi.cfi_status.shstk_lock = is_shstk_locked(target); + user_cfi.shstk_ptr = get_active_shstk(target); + + return membuf_write(&to, &user_cfi, sizeof(user_cfi)); +} + +/* + * Does it make sense to allowing enable / disable of cfi via ptrace? + * Not allowing enable / disable / locking control via ptrace for now. + * Setting shadow stack pointer is allowed. GDB might use it to unwind or + * some other fixup. Similarly gdb might want to suppress elp and may want + * to reset elp state. + */ +static int riscv_cfi_set(struct task_struct *target, + const struct user_regset *regset, + unsigned int pos, unsigned int count, + const void *kbuf, const void __user *ubuf) +{ + int ret; + struct user_cfi_state user_cfi; + struct pt_regs *regs; + + regs = task_pt_regs(target); + + ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &user_cfi, 0, -1); + if (ret) + return ret; + + /* + * Not allowing enabling or locking shadow stack or landing pad + * There is no disabling of shadow stack or landing pad via ptrace + * rsvd field should be set to zero so that if those fields are needed in future + */ + if (user_cfi.cfi_status.lp_en || user_cfi.cfi_status.lp_lock || + user_cfi.cfi_status.shstk_en || user_cfi.cfi_status.shstk_lock || + !user_cfi.cfi_status.rsvd) + return -EINVAL; + + /* If lpad is enabled on target and ptrace requests to set / clear elp, do that */ + if (is_indir_lp_enabled(target)) { + if (user_cfi.cfi_status.elp_state) /* set elp state */ + regs->status |= SR_ELP; + else + regs->status &= ~SR_ELP; /* clear elp state */ + } + + /* If shadow stack enabled on target, set new shadow stack pointer */ + if (is_shstk_enabled(target)) + set_active_shstk(target, user_cfi.shstk_ptr); + + return 0; +} +#endif + static const struct user_regset riscv_user_regset[] = { [REGSET_X] = { .core_note_type = NT_PRSTATUS, @@ -224,6 +297,16 @@ static const struct user_regset riscv_user_regset[] = { .set = tagged_addr_ctrl_set, }, #endif +#ifdef CONFIG_RISCV_USER_CFI + [REGSET_CFI] = { + .core_note_type = NT_RISCV_USER_CFI, + .align = sizeof(__u64), + .n = sizeof(struct user_cfi_state) / sizeof(__u64), + .size = sizeof(__u64), + .regset_get = riscv_cfi_get, + .set = riscv_cfi_set, + }, +#endif }; static const struct user_regset_view riscv_user_native_view = { diff --git a/include/uapi/linux/elf.h b/include/uapi/linux/elf.h index a920cf8934dc..83d16bc61709 100644 --- a/include/uapi/linux/elf.h +++ b/include/uapi/linux/elf.h @@ -451,6 +451,7 @@ typedef struct elf64_shdr { #define NT_RISCV_CSR 0x900 /* RISC-V Control and Status Registers */ #define NT_RISCV_VECTOR 0x901 /* RISC-V vector registers */ #define NT_RISCV_TAGGED_ADDR_CTRL 0x902 /* RISC-V tagged address control (prctl()) */ +#define NT_RISCV_USER_CFI 0x903 /* RISC-V shadow stack state */ #define NT_LOONGARCH_CPUCFG 0xa00 /* LoongArch CPU config registers */ #define NT_LOONGARCH_CSR 0xa01 /* LoongArch control and status registers */ #define NT_LOONGARCH_LSX 0xa02 /* LoongArch Loongson SIMD Extension registers */ From patchwork Mon Nov 11 20:54: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: 13871359 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 8E827D3ABF4 for ; Mon, 11 Nov 2024 20:55:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4B4758D000A; Mon, 11 Nov 2024 15:54:58 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 463F08D0001; Mon, 11 Nov 2024 15:54:58 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 269EA8D000A; Mon, 11 Nov 2024 15:54:58 -0500 (EST) 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 031668D0001 for ; Mon, 11 Nov 2024 15:54:57 -0500 (EST) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id AE787C1383 for ; Mon, 11 Nov 2024 20:54:57 +0000 (UTC) X-FDA: 82775017836.16.8ECAF50 Received: from mail-pg1-f181.google.com (mail-pg1-f181.google.com [209.85.215.181]) by imf03.hostedemail.com (Postfix) with ESMTP id EE89C20006 for ; Mon, 11 Nov 2024 20:54:36 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=SqiSc2X8; dmarc=none; spf=pass (imf03.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.181 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731358367; a=rsa-sha256; cv=none; b=TmIpaBzodeNJRCUkgCI3wkBjgnxMilWWodTD6KtaySJLcOGyjqgPfoD4kVKTpXfvja2GF5 z9L6sBJ7CmS7AEeTD6qZrn4v6MrQEOgTj3/Xc4h7p23eWUaULX/JTjcJQsYDZSZIkPIKMx ZoglUpOfVbPrfP70VNzMmA6te/Vc+rA= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=SqiSc2X8; dmarc=none; spf=pass (imf03.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.181 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=1731358367; 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=289Nwoms47JcaO8kSApuumW+ImHm5WLKWOrCLSEYkbY=; b=z5yA8Llj8FEBDefXb2xxvlYxt7UrxV6L5ja4hoV6vBTS+35c0Mw581oDBJZ7rWjODa1RJq cmp87N/gML+uck9Kb1EEP26EfTSsCIDfM2gYfwEP9s9YdjS8IhWsjCtWsUZg10ZhwlF86A PE0ymSXp+xtSH5j6AD1QfilmZom9/yc= Received: by mail-pg1-f181.google.com with SMTP id 41be03b00d2f7-7ea8c4ce232so4338613a12.0 for ; Mon, 11 Nov 2024 12:54:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1731358494; x=1731963294; 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=289Nwoms47JcaO8kSApuumW+ImHm5WLKWOrCLSEYkbY=; b=SqiSc2X8SoOqZXgDE+Dl03Iq5DZF6oQZdkQRE/fh4t2c9O7i7E5Z3/zY+UcF9c1Bia PnQDIEKbf04geIlEZbWERKXOc8WjVWpYrIBArIi9X8+U2JPzVBhbGiuyjxYPIKNEraWF Q4PiG7S/cCd8PjtZkfwM69SFOHOSJmuNYyNdaPyilnOSJuxnBjt79066Z1qH9V7YkXAL z2lvg59WjFE4B7SLkk3GK3MxTRZMgrP0kB5+XayPZzerRlEqGYoUHfn9y/A3+CON50B2 qDfeeSf4qy24aCPiliIEiC4kfrl7ObdSI4ScoFBcuqaX/XbJ0lu38dj7pZH4aDE7lJLF bxNA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731358494; x=1731963294; 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=289Nwoms47JcaO8kSApuumW+ImHm5WLKWOrCLSEYkbY=; b=cEs8inVnz2MuJDHAZ4pwSzULwq4CZAgge+Nn5SL3mwkUcUv0fVU9BDxueXiwFV299t spiodaKilRW7JhLkK8e/ob9NMwEcb9iCOLunlINJcRMlOiQq/7U67J0KA/PkOqchNZpE jkaVwaKfo3bwCwUVjQE5+Nofgh4gW/GT7lAeOUjijwslUTAiQ2BH8QF2+CrpFBUyltx4 7TlyImoz+6z4ApOFaHKETNGVyalAL2kfbX60R98I0gPe4gwRdUXnJGTtLrWa5LmcyplT a6nUyP/VmTr0KzP2sMSan0XglFJsOUXMfAbJpKoeKdZsokIDyxpaNThyML8jyYvuGHgS tjyg== X-Forwarded-Encrypted: i=1; AJvYcCVS7vC3wdYzlAphZDlnfzx1+7r3vBYGrBl/rWxXY8H3vztzpNAy8vSmskHPkd3JBquS+XA7bEPNCw==@kvack.org X-Gm-Message-State: AOJu0Yxh/QKGYs0ELilZdZY4Giqv9nhiJHI2NDCYRWp/57nZm6IZbl2M eTbLmFPSY3DSQh3nziOiQ5o8A7EtyHWiB/tw5RZZVQ86Fc8lkOI5M7ZE3mCQuas= X-Google-Smtp-Source: AGHT+IFgfaWfDVg2ag1aCv+RvJc7LebaoP9wiPFi7bt9E035lNfk9/OuTdzMOTjykOKHjKVWywhO+A== X-Received: by 2002:a17:90b:5310:b0:2d3:cd27:c480 with SMTP id 98e67ed59e1d1-2e9b1787df7mr18295211a91.33.1731358494513; Mon, 11 Nov 2024 12:54:54 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e9a5fd1534sm9059974a91.42.2024.11.11.12.54.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Nov 2024 12:54:54 -0800 (PST) From: Deepak Gupta Date: Mon, 11 Nov 2024 12:54:07 -0800 Subject: [PATCH v8 22/29] riscv/hwprobe: zicfilp / zicfiss enumeration in hwprobe MIME-Version: 1.0 Message-Id: <20241111-v5_user_cfi_series-v8-22-dce14aa30207@rivosinc.com> References: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@rivosinc.com> In-Reply-To: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@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: EE89C20006 X-Stat-Signature: nyxhgm39ksyfy1gdwp4qmym6eqg5bzte X-Rspam-User: X-Rspamd-Server: rspam05 X-HE-Tag: 1731358476-793413 X-HE-Meta: U2FsdGVkX1+qhMbWj2tnSI9Enos5eyCfAfffxzTkBwAyP2BOrT++fiAmxpPnfAIdPV7yMAE/ARCmLzBUwe8t4mM49NSwLGctxCSeKLPhv7nlJuZUFK1a8oCK+9Fku6DKnAWrqBehAnB0CG39OZiWM2FSn+FMntN/o/2fA+f7Kan7z7Wf+Voa1nwjnBPN2odjjM5pnVHg1tvBEgA9QGF65numZqAh4d2PG2QNV9ZoYevwUlm+EEGr6znrnRZzUxqKl68suimF1Jnmj1ruYFHmwISYa1JBXDrqFB1r3rAwhur+Elhc0mII4dbOcuYug9KSv8dvLW4bnQf1rS7eYNfPEOJZw4Mx+UNeifNTn8DQZdou3kuLNC0qw13jjXg4zTHHyg7dIxbcIfWGofey9E72toCsp+FkLBB0bOWJW0AByh4rbDiv0Z3RQIiEYEew/rI68pQTZEvnzkTBVTcOXpAcXgxAYdDM7xty1OReBnHtBoz354s4wmk2W6ZrhULFYXttWKPZcL/7Hg4GWafUkpAsCK07LENavUsDfBH0Q7bVXDgCl/ps6lx86lppykPaIsLTFUok5PfcfWROAimAexw/yufHS5GtigG5XKhOJjdLwQNawdTA4BBd/vijddA4yBDhxSrSp2D4DpQKQ6cHOJvRD1hAWreE/RDphMYLnO/y1mU5yOoUHf3r4IlTTORtatMbP9FBs5kV0HZOdtyQEbSY7HthaMT1UDCPQ9UaJ1zjR69OJc5AoMgvlwerMc4q3E0PT6bAhnrS1BLTSUHw30eI+Aq9sXiSQxs4HoyQyvdmsnZ5nvoCgOg4x1rHLmTXJX7S8SO8IfAHTjZe81i6JStp/LYJ3MKTyRVFEVLzbrQt295IhCooP4DhlgJunEjcEbsOSDkrBQ31SIxbDkOjPF4nPm/+KSPQPblf0i58Y6bUflBSR42MMfbiEg5o+Y28gG51OBFi9LjpWuRQ9KEn4DS KZVZrtBo OdUN/84FgmRGHEn50oPzmXUQpL5UKqoB4tvd2N4/2/zKAO+htEIEXJWd6adh1up6TFDpR89KGfCiOL9pUIe3sDT+DTXrkdDg6Bm2MBajp5P2mcVNUXFspX57O95AyWy4hIbHOreOYbngECEIagNz8s/531MTDbjz851p7RVGt/RplyQDGrS52XpeedEy4EDFsaN0R4+W6xP0h1YooWRDgI3RRTnaqYtkJL4v8Ucsl2npjKkDQqOpkpNPviK56FlbzJAWRfjfzZzwzcNuGQCJBkPHcOUbemqUMTlzH+KcjUe7Vc7+UhCSFqlzH7Yrtg09jNNbK+IJ92dq+gmM8HC1u6yWF1bRF8HGt2zHpeCpj6lz44b/j2gxPdhC0xAPhfaUFD+rPlCn9qZBSqWnvb8/PNSALDuWL55JnLzelwizAGpHGal/LqXWVbMNFWVvRzKbMno/2hmc5oBaoih8= 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 3af142b99f77..ca64b07927cb 100644 --- a/arch/riscv/include/uapi/asm/hwprobe.h +++ b/arch/riscv/include/uapi/asm/hwprobe.h @@ -73,6 +73,8 @@ struct riscv_hwprobe { #define RISCV_HWPROBE_EXT_ZCMOP (1ULL << 47) #define RISCV_HWPROBE_EXT_ZAWRS (1ULL << 48) #define RISCV_HWPROBE_EXT_SUPM (1ULL << 49) +#define RISCV_HWPROBE_EXT_ZICFILP (1ULL << 50) +#define RISCV_HWPROBE_EXT_ZICFISS (1ULL << 51) #define RISCV_HWPROBE_KEY_CPUPERF_0 5 #define RISCV_HWPROBE_MISALIGNED_UNKNOWN (0 << 0) #define RISCV_HWPROBE_MISALIGNED_EMULATED (1 << 0) diff --git a/arch/riscv/kernel/sys_hwprobe.c b/arch/riscv/kernel/sys_hwprobe.c index 9050f3246264..716d72abec60 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 Mon Nov 11 20:54:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13871360 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 F17CAD3ABF7 for ; Mon, 11 Nov 2024 20:55:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E53AC8D000B; Mon, 11 Nov 2024 15:55:00 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D8F1F8D0001; Mon, 11 Nov 2024 15:55:00 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BBA378D000B; Mon, 11 Nov 2024 15:55:00 -0500 (EST) 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 988A58D0001 for ; Mon, 11 Nov 2024 15:55:00 -0500 (EST) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 5E3CEC1910 for ; Mon, 11 Nov 2024 20:55:00 +0000 (UTC) X-FDA: 82775017920.16.FC399A3 Received: from mail-pj1-f44.google.com (mail-pj1-f44.google.com [209.85.216.44]) by imf25.hostedemail.com (Postfix) with ESMTP id E34E9A0003 for ; Mon, 11 Nov 2024 20:54:28 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=RmWkyBce; dmarc=none; spf=pass (imf25.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.44 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=1731358323; 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=T1QM+ilWvuEvSun1tHtXjpBQUlKBmvzGJs0C0a3DCl9vV0zMZMSqADE3Hthx9rnSAi/nQS QtK4y0yJn+DkIEAEa/S2WuaKGVE/tuEyP0blTiWQfI/nuKr1HedznF2e/yRNEyENbL7BBk 5vDj89YRrx0Bn5zvy6Hc0np9ZyMKZ9g= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=RmWkyBce; dmarc=none; spf=pass (imf25.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.44 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731358323; a=rsa-sha256; cv=none; b=NFhY2vithSKOOhqii7rLOEjyyHWIBF7jbJRLP5zUAUZ2duQ6W/BNj2lQ/U9iLlmLvvFfJ5 qMGnDa8YQftLAXUUSPpS2ec8ji2Z4OjNut89gFMwySFoWHQH/qLGfQiKVi4rQeRmT6gsuw yzR5qjCdODj9YNvexVHlL9lsM4FiWiM= Received: by mail-pj1-f44.google.com with SMTP id 98e67ed59e1d1-2e2e6a1042dso3952569a91.2 for ; Mon, 11 Nov 2024 12:54:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1731358497; x=1731963297; 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=RmWkyBceEmohfbJdQpxJnC4xAyNfrfr5t53oIYdkOs246LREioppM3qJMOqmxjHv41 DKxQXln/L8NKyv/N7ooFLqGsnV57HnR97rYir02fgwDMEhk1mPrsMBLFZ7w4dO5FNT7d QfeQl5LfXsnbr9C16AJVIzZT2EZy3DPYETCHz4xAQRmoeN8eAubOCBjsTABqMncOe/bY w2FL9dIxGly3rcBb6hYXc12A9riUBTTNTnmbfo0Wqr9+J9m7xg76lsLXDT5b0XK53zJc 6B4MpzJmwp3BvYhNFAuVixMeaOfQqr+VXmKGM+HQcQvt47wksSXv6O/gw0q6UH7ERziF yvQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731358497; x=1731963297; 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=CCDAOSE5zr4MSqG1GpbW6e+Zj3k6NWVTuPKMpypt3JCrfyZ6rEdqqRmLeHhWkKaeCm raYFJwMCJeV7WOhAWcAN+lzyXb33a/GSJEq/S98a5FORKs1I4IJQ59FIwtMlkAaOZCT0 q3TPDYvyiAbkCENJO7h5BYUis0LotvBUMZAxbUwlqMRdn6GrRZslMHYRvC6xL0+0Iz11 jxliaTr3Q4CyxpM/OUbpnwpNvHJKhLDR70nrfiV2QWnt/d2bVnPZllqCd9BvihPsxuPF SPDlqwD6AVh6vs4otphpCSbg3JZ/jpa1irKPzXcmQuIZd797MoyA/hGaGsZBe9Nb9GEI ujEA== X-Forwarded-Encrypted: i=1; AJvYcCWFykxFzzfm4k6dbR86ZfUam8uN/7iYjRYNTjC5OFvL54kkxKNHfmUA41STenbkqJHMAwRq4GrPPQ==@kvack.org X-Gm-Message-State: AOJu0YzzoXDgC5xHRVU+cS4RiHEhM2xT7cflGZ0athZAqxXwq7tAXtyM rImt3XDFVpSpycoqEIlvB+CPdf3xbPAkvhlXWT2W8Pl0a62KeUFwc5FyxXhzNx8= X-Google-Smtp-Source: AGHT+IEEKaEBcK4MgBqd/tJtgjOiUzsMic6zpl+RlG/WSuE/W94Zyl8KoJt+g6fHJT4sQqdqWl8Dag== X-Received: by 2002:a17:90a:d88d:b0:2cb:5aaf:c12e with SMTP id 98e67ed59e1d1-2e9b1788b6amr17044135a91.37.1731358497234; Mon, 11 Nov 2024 12:54:57 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e9a5fd1534sm9059974a91.42.2024.11.11.12.54.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Nov 2024 12:54:56 -0800 (PST) From: Deepak Gupta Date: Mon, 11 Nov 2024 12:54:08 -0800 Subject: [PATCH v8 23/29] riscv: Add Firmware Feature SBI extensions definitions MIME-Version: 1.0 Message-Id: <20241111-v5_user_cfi_series-v8-23-dce14aa30207@rivosinc.com> References: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@rivosinc.com> In-Reply-To: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@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 X-Mailer: b4 0.14.0 X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: E34E9A0003 X-Stat-Signature: kkpujt1qtm3dsxyi5xkaddy6w88d3by3 X-Rspam-User: X-HE-Tag: 1731358468-480722 X-HE-Meta: U2FsdGVkX1+xYo5sDpu/jp89OdRrCg5NBwGNffFeWvNyVHL+FUj1wBDht545UseSjp0IzBoVyGIsXnqRRwJU8OcWDEBI76yUZLnR3UH9H6uMEKb+OdZqVnIK/am5xaxb7e9eLN5zmQl2hllpMDC/vWBJ+hjYrCAergxI2+mGtbNt7UEIFk8gUcXyRpxprIFGqeVjDOORPg3w+xYZ0Fh7Zx3PH4ThFOl8T+PNMb0ZC+/Ehe8F0RZpZoxLRAhjtWewupq45mGyekwt447WRQ4SfxfX+Bi3HlwQ08hkK6ZE4MVPBljIoMaR557TGmevgvWzsgETWj4vxg7V84bJgACOBD2TcYznrlFuz7Hp+6DNtypdGl3hA9oexXdttdVrmXux5+Z0yK0Vb8yCmAmNqS451AOPlZRXd3u5ibbRvG+j1Y1Us8CbXDetcJf4QtHtruNFhi8f7Xt3OtFzsSt9cMXSIp0IczxHKaMFOMf4IQQBfcROQpRgGi2x9j53Ao8ElTu6+shZYwIUVMIasu3pnkFNMpj9q0jTb1PUMqwkTQO1D/gdp43+YbVx2Q1CtezFUdMrF9E/JtuHkvv0khImLjvtPID3CNwp+FpoogZWn/vj0/bAq8teVpFwZCx4K07alvjqyEyojiVE1bJXepkxnDBSa/JeSS7oDsqXI6CocUgwqMWO57D3UuGoMS6t5EJaBUvIr73xMQL+3Npfbeadp8uPrGZnMaLoN9Jq9fpthC07CgfevgASe/aOwng2QahZUQ3a4FpLFTd7csHrY2fY57FknaFCaEDW8vsezFY3stTdp6T0liOncu0m7irdShbS8RVDPmwpspWmCkOrGNIztqta+FrL/Ern91rBZdigbAGH/KlT4thhjKaZjm0bQwlbJQCKnNOLY+3NAz+0J05Fzllxrk3pbqF6au/P20BepCEqYmApj7EfD5iCUI3TyFyzhjyzPBll8BCXRmYCdYozM3X XpC6yoz/ mWM+FDTCESldr203KuXP1VbdrNlMXESPkaSM1hCms3F6E2VubgS67ObVVS82IoB+1cmElqO21GbBmyHI+IEeKlUWUt7Vw5X2ZIaHMYE1EvoiirWPJmTdwAxDxfB/vALKp1YSRay1BuOvHPUjnKObtgNXVAJwy8um2ymI0N06pfKfCF5p2fJ8PptKzkD2seHxr+76x6u3NTNaurBSlvbUSAcWnrcK2b2Kq2+Ka66lwwf5iLcUQHg/Lxu3yeZgYuHmf5M3oBpqUPeMZEeShX1T0GCj7p6Xf6PXBPr08fJSZlljQgKIgjrflp9i6oBNf45BfhgFMp2tvc9fCFAC7WYbogyU/xEaVe7ZWk4lEwwilPyaEp/UtyMApCmI55KK4U5qgV63DZpmRPw16+qwUpsO58nU7miIqfT4Rt1UhpcH6sayU5NtDYDheRMDgYVulJ/j1qjeW62hN2OPNF/mBO52+Jbck0tP1rbkOcU6pPvTQFOsyO4g= 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 Mon Nov 11 20:54:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13871361 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 92417D3ABF5 for ; Mon, 11 Nov 2024 20:55:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 41CC78D000C; Mon, 11 Nov 2024 15:55:04 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3A1158D0001; Mon, 11 Nov 2024 15:55:04 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 130468D000C; Mon, 11 Nov 2024 15:55:04 -0500 (EST) 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 E641B8D0001 for ; Mon, 11 Nov 2024 15:55:03 -0500 (EST) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id A4F4D81ADB for ; Mon, 11 Nov 2024 20:55:03 +0000 (UTC) X-FDA: 82775016828.01.2EF377B Received: from mail-pj1-f53.google.com (mail-pj1-f53.google.com [209.85.216.53]) by imf25.hostedemail.com (Postfix) with ESMTP id 2A117A000F for ; Mon, 11 Nov 2024 20:54:31 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=kKg2mmH8; dmarc=none; spf=pass (imf25.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.53 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=1731358327; 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=5+EezQvTDxjJ9wgBc6UaahgaRgMWMoCCxz19b3wLQbkAr6lHgrdZKX2sMd4ywJV/vgzi8f qqrWnhZaSntQd9WmnTGTtZ7cEUA9TZBEfQpkfBSQr3Qz3HW9m90Z3PRCebnCqNFj0oPLYJ iSvQyl3XvXhhCSMJ8UuSIvEzjYN4HXY= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=kKg2mmH8; dmarc=none; spf=pass (imf25.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.53 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731358327; a=rsa-sha256; cv=none; b=DEvvijcEYPqlt3HQCyftquE574Jq0gmm43J5EoSAlvIM610el9XCy8WFUiRKXGer4hEp0x kbunFip2u5/Ho7O84m9g6QOUYePnR5NKkLb3aTjIeNNacUMf27iZh7bYmP71lIOWGq6MxL LSNDSLAPJ2d3uNtS6Jw+UbEQCxwcO0g= Received: by mail-pj1-f53.google.com with SMTP id 98e67ed59e1d1-2e2ad9825a7so3725047a91.0 for ; Mon, 11 Nov 2024 12:55:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1731358500; x=1731963300; 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=kKg2mmH897CpTDLG8pzDfT+Y3/qwSa+dlL1ZW4dF4ZqIrFpZvkNgOlqz9lW73Yp3dz cBYpRV57MHuxkTbwqZgIURu6kJR0SdExlXXiOR29YoZJBksbgh4zFgfBpb7U2M6NyCo2 srNmU15EzQvRoVmiZUT7LGgSOvKI+wRQjish3SgouLG8LOEhfTWOfhJqDW1+hWrHWvQS 8ZnWgarrDvjHn7KQkSqKtVUL6i/SFEqI+NRIsJpIG1aQ0o0wTbO+KoehGkp4iLSMN1fY YMTM+W515rMDKSMrLqvPmAsvR/Ps71Is1MA91rdA7Wi3R5hwTxG8/qN0h3bQCQUQg/EB Z4zQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731358500; x=1731963300; 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=WHUIwmmbhLzvwZfwBEZr/wX7BiKt23iHT4RVwrGf51KFA73lgJx7ednpN9KubqCORI HAm0hspewfF4Wfsh6BkEluWbB3fuUf2JDzRg0BZt32aNLOIe00j64D0UQ+jIV4iAVmCt mZxOnnlYGOrrHpbydwdMy2A2yduvFzhvxynwwQ6GtHSh/1SaX3I66BQUhMda2GpQkBmu 3f3Z+gP3/Vsw0e+n6pAm/mUlbCDcTZ7s7ZtT36/J9MljbtlZX+r060lkru2haKCoa+Gc GjgLUjS3tICGTnKoANa164bO2BN+ZKoggfdz8nAd5tOLglQkh4D492g5tR471zizuBcW eOjA== X-Forwarded-Encrypted: i=1; AJvYcCXQ336kWGPbVJhZhdOD4LEI0grU6WuOg9j9Ns/aY49kGo52Fdh8i9LSa2ELAT+dt4Wy88o/Y73keg==@kvack.org X-Gm-Message-State: AOJu0YxI5MuEh5aheORquB9SbZnr/BZWmyrq8aISl5PlWTrSAL9NyDej LxZG/SmuoKlx9Udq2K/vbkruBHRp0rxNpYPgo1CNMyETN3J1QYSwzDD8ftgZQo0= X-Google-Smtp-Source: AGHT+IFvSKyWTz2kcUW7fEzv9S54SRzP8RrRWSKRH6ajavmfQ0Uq0te3J0ha2EpgH4EyhyNJMDyysA== X-Received: by 2002:a17:90b:2dcc:b0:2e9:2bef:6552 with SMTP id 98e67ed59e1d1-2e9b1793d60mr18560030a91.32.1731358500420; Mon, 11 Nov 2024 12:55:00 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e9a5fd1534sm9059974a91.42.2024.11.11.12.54.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Nov 2024 12:54:59 -0800 (PST) From: Deepak Gupta Date: Mon, 11 Nov 2024 12:54:09 -0800 Subject: [PATCH v8 24/29] riscv: enable kernel access to shadow stack memory via FWFT sbi call MIME-Version: 1.0 Message-Id: <20241111-v5_user_cfi_series-v8-24-dce14aa30207@rivosinc.com> References: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@rivosinc.com> In-Reply-To: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@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: rspam04 X-Rspamd-Queue-Id: 2A117A000F X-Stat-Signature: 94opqr53aa5ewdhfrakr7bbpu46otqq6 X-Rspam-User: X-HE-Tag: 1731358471-772451 X-HE-Meta: U2FsdGVkX18ZGY5KI9trduIiZBeDWe+Hzbbv51mCps7Fajf8B5yu9PZd9JcesiGpfPbx4hhnObCqh7BCs6t/li0dT0NslWNpPHa/hiJX9InyopVkRIDZ42Az1gnnpjkdPizEU22eTqTDMh5ZUI4AQCtCt6gRuj7fdTCgpB/93bukev3PfLInfAuct2PvoMhVXUXzPCJoWi+5CDnM1BUl3r9YV7sstku+ITKzxeyXpP50xGBj2k8eIKXENd4GQ92twjFK6Uw1sRMamN1woxEOCtwkG9iVh9R2ouGgc+ZBnWoh6DILFtLDrWSz0aTPwvZBep4lC9034V0B5f8NpNYSLxlvnqXGE6sLzN448ylaMoe6NW41qJXxGwFF3zN0Do5wlozd9op5r3FAX9CVUNN+urks9Ji3pKKFEn7wIn/NI5TMX3u+VGyYM/VgEY+1DcH4DdF4a99fNDv5+vO7KNDliYniOpB8CNj8AgRVIiKv7wTXoT1r+pB3+JSq15qfJ2gen3fWxX5myZHRChoHH5V3aLtsOIciixBGvO8IeQ6cZHDQ4FR0Bk8mt3fty53oRC5fyv2EXM++MDmJEMIPmK+QcWTMTmsz58DGdqW84yffN+/5IUYQq+wjpOqA0pXZb6gr6FnEkNpJtr34QbbgqrlnATodnsMlbRAXzCqy4u9YyYfvgVqTCj9NauZECqbUCWIe/ib2tQ+XjSWUsmS9r39gZMzLkwnTKerfGHTlGB/pcfZU4HBHBCWhGGk5cD61mk5il3cN0MUCIG4zzJdgc15WallCtp1QSZHc9n9bEKS2Odk5EZXNmjcJ1TFKwD4d+seOUmwh4NJPQMvBmmmf/RgtuApL/SBPak0SwTBK1ytnrWjYKxLyeJaecCiRD0xbpDj+bMUxGDJ6wabHozgQZl8RG29xcnj7q3P9wKWJbPwNKEB+j2CqeibNb1+jUQp34P3ftNIr4H7y9049/hqDC7J qui7FEgU M9FNR4V1A7qz4uyjfFK7LCuVni19WPPMFOqJEl/zBG2m7lu62cybsIboDbIX7Fny4BG9eWgfXcjbCcuy0XKydLSASOyJvEAgBMgDbN1DAa42+IdhPaiNkDgHh05H5TVpxTol08k+6B1onX23Qkf9GtHz6uPkO6sp5O/9un658gvuCDfKa2p6r5lw+WWIUoTa/VACb5yM9ezk3n7yddCgDNx8y9Ype0e+jI0ihrPPjlaID/6/n/b3pa3N6TOdjPJJitW6rBtpJ8CPDUUEWrtEVeMZF0klvffomCjSzH01bpULfapK0AEXIHxI1Xn2E+hEUlAnUjt6DiSaVcYckGy3+1DdJ5sfpYx5iiTdjguRBj4ZOnVZ4JhpyCWUhz6iyQWE5U+0kSWb1micO0mzt1WtSjLSf6JvhdFNnk+evUwLXv5XM19TIXtr0uEVv8xeILzP26qlQXJCK0m11Zt/+75UWFp+/6Q== 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 Mon Nov 11 20:54: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: 13871362 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 79E47D3ABF5 for ; Mon, 11 Nov 2024 20:55:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B6DC08D000D; Mon, 11 Nov 2024 15:55:06 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B1F618D0001; Mon, 11 Nov 2024 15:55:06 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8D36D8D000D; Mon, 11 Nov 2024 15:55:06 -0500 (EST) 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 6AABB8D0001 for ; Mon, 11 Nov 2024 15:55:06 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 35264160D74 for ; Mon, 11 Nov 2024 20:55:06 +0000 (UTC) X-FDA: 82775018130.18.4A58411 Received: from mail-pg1-f174.google.com (mail-pg1-f174.google.com [209.85.215.174]) by imf09.hostedemail.com (Postfix) with ESMTP id EF52C140009 for ; Mon, 11 Nov 2024 20:54:35 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=Dhr4uZGJ; dmarc=none; spf=pass (imf09.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=1731358442; a=rsa-sha256; cv=none; b=TFKHcfemj06ZVuuYfxG7yEVMccqu6dQs+BzjnyEStomYJBC1oA75/r6TUonl3EG5rvUEM3 8/PgezYQnCPI9n038KrQGscXIHBw7/VaBXb8GclW/r5mHwQxNfNFRyhT/WKwBY8+cSzOE0 3/ULF8ZYADsN5+MXrk9lZY3MDNC4SBs= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=Dhr4uZGJ; dmarc=none; spf=pass (imf09.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=1731358442; 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=v87E2FN7teOOU1wWTBhK9gVAN0VjVVh/KdbYNd+fh68=; b=vdJcyR/1xT7RJzL0pa3vAa6LbaTzzv7oO/z7JKPUfJg+4D5KfLgkGPWCJGCFeX6WQRJcvK oc0AAyiWS/MfV/rpjTPC9n2uK/hCMieXYZ7oCmFQ/2U2BHrYC+DiAZ/FoGkXzU/53NfGHQ bJvbhq3C/ZsS5FA7sAsyg2tMqr3UAO4= Received: by mail-pg1-f174.google.com with SMTP id 41be03b00d2f7-7ee020ec76dso3825232a12.3 for ; Mon, 11 Nov 2024 12:55:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1731358503; x=1731963303; 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=v87E2FN7teOOU1wWTBhK9gVAN0VjVVh/KdbYNd+fh68=; b=Dhr4uZGJE1r6RCpIqKcyTq1nPyrfaKhZc5ZiQu/hNE/aUhHCAg6+ML3BmDw2vPscFY 0a3kBzMRRmDGvryFtkeSiD8A2b1Ro8ZDxuQj61G3u3xjajDJv/x0aRJnrmoXI8IexJkC 8UNjhZqBl3stfCiCWpyK4mJdAGiA6Uup8RmCtz34T3JO+bvAEZ94XCY4Pr3aJjN1fD5q CyyHDFgumxHCp1VQfmnMwoKR/8IoiKraahUqAzMCwT65qcKKi4+cKNKJoY10mqY8pxZb IFULvkIm6cABzGXcU3OvdGP9SSooux76hdhWSRn4zXTqJY4aDh87aM9+WM+h1Jq34qfa unRA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731358503; x=1731963303; 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=v87E2FN7teOOU1wWTBhK9gVAN0VjVVh/KdbYNd+fh68=; b=KWQHZJKN+VY8fNCeZxSQBF7whVtSL4PhtEEIRmPDdB5BL5jK2ErlwzH3yNEJ3yOgq2 Uk2kO7wUYW+TeurL4SWODvnUVgckGZQ6rNa3fAP6dFe+7oOwnPCplsasEEiJ4l7T1rec SODJ2YCQ1BgTeSoIeI7tC0bP8yxdn2qN2rf5fnnbUmjexw4iEQujhQk1cKtY0SeACAm/ 6Ec6VqUJTi85pvjtvXTS6rs1ydcwX724/GUwUwEONCvQMtb8eZsz6Dij9uccKGxDscE4 fEZZpxlPsWUUIJZqCJL3jLnybCbDx+7GMDyR1Q5H2d6MIa8ZdKaLZlzb9EzsrDhSWy3X Yb5A== X-Forwarded-Encrypted: i=1; AJvYcCU0NZG2Rl0TNSixlE2xgM0uDbA15me7ru6VljHGRshPVFbHeJi9hhUc1nQGwMxbbNdX6PPT8eSCLA==@kvack.org X-Gm-Message-State: AOJu0Yw8/klBdWp5Te6lUd5gKW+rOmBin/NbqBtoXmqySqIrH208Dlvb LB9jQwx00vYCv7gcvAgOes3LvA+OlAJR5JPWdGJO4cKxhi37Xgd/hcO/k9TSIW4= X-Google-Smtp-Source: AGHT+IE6pHU7K0D6Kovlo0iT4mPH8p4N2kcP8nKM47bgfKvZsvt7nFC76OWezD1by9y3vt8KBs+5Mg== X-Received: by 2002:a17:90b:1a92:b0:2e2:cd22:b092 with SMTP id 98e67ed59e1d1-2e9b171f93amr20303584a91.16.1731358503151; Mon, 11 Nov 2024 12:55:03 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e9a5fd1534sm9059974a91.42.2024.11.11.12.55.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Nov 2024 12:55:02 -0800 (PST) From: Deepak Gupta Date: Mon, 11 Nov 2024 12:54:10 -0800 Subject: [PATCH v8 25/29] riscv: kernel command line option to opt out of user cfi MIME-Version: 1.0 Message-Id: <20241111-v5_user_cfi_series-v8-25-dce14aa30207@rivosinc.com> References: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@rivosinc.com> In-Reply-To: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@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: EF52C140009 X-Rspamd-Server: rspam01 X-Stat-Signature: m9niau89fdf8bp4wjnaeqj5g8iwsdcwx X-HE-Tag: 1731358475-427242 X-HE-Meta: U2FsdGVkX1/tjyOqDrw+jTRToHirRcZLhqN1NiNfqAYGwLN8hrMSEkaIN35MfqJ9DGKnr1xnm9DOny4kDz/kGjcvizjJVTEVa32y53jZbNMqUozoWb3tGnLLtUm7rwsN1vz+ftgjIrjKrUYs7cCJWPheV4+76ktug9Ns2weXV4RCBTfw0cY4vxCWisrPUYTUZEoKo3kNDFWvIna5epGoIvz532+LqikRywdftKOhZayIrm9m3z3Qm5HByZd2Q127hMKX97tjnRlYeGTUqdOIto72z2cpEvFWN4lXGQ8NSeafq3uJZTYk5xQWQY+i/16dxfJJDesObrNHCf/vd1op6VRoTbGNeRsDVXXnvoakrdp4wmxB4wstSbjvaMY9q5c+slYGvxHvssyIjXhXW3QKGWG6Ash+iUMm3ZLhs69UrBbryAy9Icwo7LmBaXZvHwAcP+oOySMJY6PmzPUOZh+5vwB9KEFPPm/RljQe5gLbhftvPsIFOStH/gG5SwFZH1EyBWP9r0fp+xeyvKr9cTNrLKu5m6roLNJsKDtTgfBZV+q3zTc1qbns7EyBlWojIkLFShuZbxJhu0Rz+D7z6Y2GCrkXXtPPn4JvELPivUCIpAueSywOI1xm+fE4VkVCvZ067FdbXF152+KDhrNYHdXrWKV6Qw3csuQXoOsvoAxFMaJwEHH9WX044L6zG9jBsG55xRiCF9Qrv9Rvl3/QcyiDqroGE6yA6hy4mEJ9All2qFUZiWvGUllIS4X2WMMooyZTJ19oE49dNnalpYNafqzRgfy4xLaeSIp9X51E4GwsH9td/MZg6oDgDIrOusNyBLY6Gglv4l/Oi/AuNbrD+obn7tOh29wHmUcOrZbycwhLfazGVFZM7k171Uv3AIEMAqeBZuC1qzXYdhSElzz9CKK/77uECRLd7KMVD6JOxJmY5dLv3bl1JIf1QG18+XtvLC6/zl4z8QDp/mU3mbyCido JqpodTp1 E5veILaJVPevsR2hZTRrmRzdEUT1hET9hYPHlq+Pw/RkQth28kMlzzdLuFVLTL0Nkqz/jMHeQvPpJ+Hm9nnrPfTK0OKVwoHVQtzmaLoWlXo9qXWcETc1qGHJbGaPdkaKNWal5tk9mW3kzBMW/ivhK/31V1GMtxIExOolB0vereUEWqoXp75yxJxHED8piyvlItGloX8ceLgoNBWTx42lAfht/kwNXm/Xa7FfPwhiXIaZZbMpWYWY6l2y6gV14Ktw9ePRIkQQJQIvbb5IhIyjMMNmiAE0aV0xX7zUQLhbG+I17EbhrXrL5okzLYidN1XMXI6u18cUotkIg6838Rhctkv43syfAb06YY8ynMtJ5PnU58bOxW+Q61QU9qVMoSM7n9d+VMnIxNvJRiu/gKRtguWLlXo8P4qzjUeVxYMgmBOJfAsWoiavqBlvWpw4LKQY0zQWzUgmh+GFAHoSJfXgw7pB4mg== 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 04b0305943b1..223dfa482deb 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 Mon Nov 11 20:54: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: 13871363 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 F2D28D3ABF4 for ; Mon, 11 Nov 2024 20:55:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 976CE8D000E; Mon, 11 Nov 2024 15:55:09 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8F8E38D0001; Mon, 11 Nov 2024 15:55:09 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6B0828D000E; Mon, 11 Nov 2024 15:55:09 -0500 (EST) 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 43C2C8D0001 for ; Mon, 11 Nov 2024 15:55:09 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 062421A0D15 for ; Mon, 11 Nov 2024 20:55:09 +0000 (UTC) X-FDA: 82775017542.25.2DA37A7 Received: from mail-pj1-f52.google.com (mail-pj1-f52.google.com [209.85.216.52]) by imf18.hostedemail.com (Postfix) with ESMTP id F0CC71C0010 for ; Mon, 11 Nov 2024 20:54:48 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=xSfh49Li; dmarc=none; spf=pass (imf18.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.52 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=1731358420; 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=sEpSBMtfQIdn0VsL6AOSyBd/7ZO5cM9GZrrfF6sqPzw=; b=2qzMUCQ1SGvSZPAKnEnBImhM1D3mcRJ85rUyK0MLUam16OD5BK/MikG+9aDL/s1RNnvAna ae5dQ3wBWMF5WXxGFj1ueBj+Iol4fTKgtw7BRKYxan6qsn+g9MNc7mUuViNXn3b3W0pSfV x//HLflawtO+PBozx0E//sTFOUHJsn8= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=xSfh49Li; dmarc=none; spf=pass (imf18.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.52 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731358420; a=rsa-sha256; cv=none; b=n/S61xnFEtfZYZnVSjQeAz3dlCQUJCLyeMLKF/Wiy9M2LeIiGKMVqjIQ0WkvEB7ZEpOEDi 1KnXrBSp76XTKaumwajHijM4wjQTEvxftiTEy8dZmWznTUGilYvWQn3khttU0agKijTk1k wzGmY/2X9Yiv/Lcfnw+KFuX5lQZtK00= Received: by mail-pj1-f52.google.com with SMTP id 98e67ed59e1d1-2e2e87153a3so3568117a91.3 for ; Mon, 11 Nov 2024 12:55:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1731358506; x=1731963306; 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=sEpSBMtfQIdn0VsL6AOSyBd/7ZO5cM9GZrrfF6sqPzw=; b=xSfh49Li1mZT8iY2gXT/zCZXVtzog5NSDBTYYjUTeoRS5LiVCx8hGjyaYchCXppxtH Ss2SJZn6AJy+oUSAn2+F5+L4XcQ7YqXnyIjjLk3rdcbj0lCMQ0oTb5q3beVGDWNficFs ViloRt6vc9ZkcrwmAe/bvcm6STW73Y46SCuNJkkbbTh/79Xg3s/+ij/53jFNYHR+og/9 yEw7bYQOVk/Fu5DP8fOnfODL5qcqL4D6cb39DC+jWJUJ0nWsFNlEFBG16/QuEUEXrX7H CwVuTrZCUhT2+z9jQ8TvJ2Ge2wXIqsLGAvg6tgtpJyB8uEbAD0o2Y6Aa3fDPGKlRgHor aLXA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731358506; x=1731963306; 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=sEpSBMtfQIdn0VsL6AOSyBd/7ZO5cM9GZrrfF6sqPzw=; b=BXSDhVEWLYYTITKxI1ZlsDkuiO/Z4WVGgn/7+K+maTFjoYq0UQbXIwhm6nWC0TGQGc aTtOIa/SX4n8lW5NnVWQYYst1RyLCPzdmMyNUTL/Ftq+mC1lUIILy1cwy9dthW4MKQ+V il154+B95YKQwvceXu+FzGUwNpYeNNcTryJBLZ+YDXDZnssWuyL1R1S41MRFZoUoJtDi BuKIupgF885ylog8ibgxVxJisWIgw2jVKaGH61SC/BTYwpZCg5QPgMVz1mNy/RIF/jo9 tHChQB8rcdM1lcQ4C0ro/uqazZ0EvpbXqYry2nJ60WqvIhGZTgN0AQ1NojDmS27f5/Dz cwqg== X-Forwarded-Encrypted: i=1; AJvYcCUn5PIGFSgDjVNJChMByO65gUsxlb8mUaG+HRc1mgWKUsCMRKHrm/graRojtDzNR1p9KjxtB844UA==@kvack.org X-Gm-Message-State: AOJu0YwByNrttBgr0RpIAdqGMyXn4jdrEJcLpGCZMihiFIEf1hg6gfJL YoCnwmAaknQVRayDIAXEwCRAys9vWWJiHAXvDHoifTUDvOQwUmpNTIMaF/IO0yU= X-Google-Smtp-Source: AGHT+IFzBuraYyYmpM+mIAMFI16Yg/jetlEaju2rqc5UFMlTYdZs+XaMbAKQKjfpaEa7usCG1NdnaQ== X-Received: by 2002:a17:90b:4d11:b0:2e2:b922:48a with SMTP id 98e67ed59e1d1-2e9b173aff9mr21100247a91.18.1731358505869; Mon, 11 Nov 2024 12:55:05 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e9a5fd1534sm9059974a91.42.2024.11.11.12.55.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Nov 2024 12:55:05 -0800 (PST) From: Deepak Gupta Date: Mon, 11 Nov 2024 12:54:11 -0800 Subject: [PATCH v8 26/29] riscv: create a config for shadow stack and landing pad instr support MIME-Version: 1.0 Message-Id: <20241111-v5_user_cfi_series-v8-26-dce14aa30207@rivosinc.com> References: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@rivosinc.com> In-Reply-To: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@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: rspam03 X-Rspamd-Queue-Id: F0CC71C0010 X-Stat-Signature: 84owusabdnwyzk6jn3nbcuxpwo6rog3d X-HE-Tag: 1731358488-634386 X-HE-Meta: U2FsdGVkX185sCTfr1cneIHrne6j03ycfbh8DqEzP6yjFOZ/4C3I56HfVZvEoB2FsjNHO4iTLgUi6T/TgokiUYLdLCv2FHq+FvBtSfe43eRazRy5QbpudpnUWnOf1omRmmZQSWjY5gJspj5qZPenczCZ4G6AkpthI4Ei9ks5q1ZePXP68ngGEckCXo1nydhFVau15iNAmP9L44NHkJ9RkJAQ2yeaU7VVnc8cJOMkwsJD1e7+MHlU/JfMGI9uaGJtN2dMEn0m40fCuD6yXZJmubnnzMHpVdSNCLPBzMyf4bXAvmAcpKc2uiFagHRwQtdcOUewIuNC+qgr78rKGo3ndjm6unWi92bzMdlOKKQobSNntHb+Mrm9K7UOTEOPdOhLHsWR7Ab++tnmTlz8Hw//DFv/U8W1O4ce+oihr7kslXUi14XHPPN8c/WKoLcWtNW7Dw5XmiKK3m1WM6emGghNltvOkxnkdN0K1zmdfibk594N8szu51eTbFOjI5RhNV7GAfNI1xT/OJ2XqT4A2BCjeIRWksF3onAp3y9Y1HspcfxDBqFcZZVX49k1VrGfXSRoeFPNp7Q4kOnul9wpKb08/LRxlBSP43TMIo1alJBgs+v2/GTpjPXAVMlGfXODwE3m7+RUzmz1hTND2tBNc1j4jRolBCiP9Fh8WCPoyqFSBTtJBJsmuOzEzNR9rFRrRsQzyas7eujNBOb4CgukYZuCeiimO8DYJk+77n3dTXJ/DOv2NjhCMJwzdEMALoYZAxBg1tLlTfj6mityknuFWmZfvNNVA7pi+WvIpz2TI0d1yys+h3ygrBZqUM3nm9nWLVJrwXMVU+mC+p7ji5pqvcInqiff89D9ee8rpgabz3T4QNLSvKH7k9jFpdeHE2srLCKPM5R7r/yvBMvEWRUPWaLrFMT1dBvMZ+784DcBt2FRCK2/osGT7ZAA3gaknag4XVozSo3TkwajkiV+FIewk6Z nDks3zDJ 9kLQiKdS8WAF4mZN/bVKjEhCkRaHlv3BGwJ83Yh7Lh3hsTr4ZL9rMk+WugvJ/rN3PQeW418RlXp1rCJT/znoTkTGCsGCeHnQa3TBVT2/5qvdSYHvUzXbfei/9L0DuofC+NWc4TTSUWv8DCI1NeP7xjYonTjE74iJ5n2WqUIp+hLPXugwFN8uPUVPH5L9Zv8TI+mFbTRinnQlZYN1Q1/OBF9BuQh3X5MqY0eHcTwYeN39h+hbozcM1bTjry6tU3oecPg06OnmAJgbG9ZAIHs1qBtt9jFWKZHLPb1JyI7rnARkQSyVZcN1ebYTwy7IBFomA3xpXvCrY3OiCfPkE54+oSOyN3HPirPKevaVKKW/PJy5J/Kq3RzZfQJq22eCBuRg4jMTIOnmu2+5j81O/4qs+cP0JT0z2N8WuDXbpqW3zKZnIqaCbThQX/iw2ibUthoiSyu6lqFAdVfj0PDX0tFIlbTawVOl/ZJBIb5peATGGcHgv/Nw= 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 5bdda86ada37..0887a9ae0c20 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -246,6 +246,26 @@ config ARCH_HAS_BROKEN_DWARF5 # https://github.com/llvm/llvm-project/commit/7ffabb61a5569444b5ac9322e22e5471cc5e4a77 depends on LD_IS_LLD && LLD_VERSION < 180000 +config RISCV_USER_CFI + def_bool y + bool "riscv userspace control flow integrity" + depends on 64BIT && $(cc-option,-mabi=lp64 -march=rv64ima_zicfiss) + depends on RISCV_ALTERNATIVE + select ARCH_HAS_USER_SHADOW_STACK + select ARCH_USES_HIGH_VMA_FLAGS + select DYNAMIC_SIGFRAME + help + Provides CPU assisted control flow integrity to userspace tasks. + Control flow integrity is provided by implementing shadow stack for + backward edge and indirect branch tracking for forward edge in program. + Shadow stack protection is a hardware feature that detects function + return address corruption. This helps mitigate ROP attacks. + Indirect branch tracking enforces that all indirect branches must land + on a landing pad instruction else CPU will fault. This mitigates against + JOP / COP attacks. Applications must be enabled to use it, and old user- + space does not get protection "for free". + default y + config ARCH_MMAP_RND_BITS_MIN default 18 if 64BIT default 8 From patchwork Mon Nov 11 20:54: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: 13871364 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 9BE1FD3ABF5 for ; Mon, 11 Nov 2024 20:55:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B93CB8D000F; Mon, 11 Nov 2024 15:55:12 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B1B208D0001; Mon, 11 Nov 2024 15:55:12 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 96DFC8D000F; Mon, 11 Nov 2024 15:55:12 -0500 (EST) 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 670578D0001 for ; Mon, 11 Nov 2024 15:55:12 -0500 (EST) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 2301FAD401 for ; Mon, 11 Nov 2024 20:55:12 +0000 (UTC) X-FDA: 82775017668.06.13B6A6E Received: from mail-pj1-f46.google.com (mail-pj1-f46.google.com [209.85.216.46]) by imf01.hostedemail.com (Postfix) with ESMTP id 3641D40004 for ; Mon, 11 Nov 2024 20:54:37 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=GuL2tsqq; spf=pass (imf01.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.46 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=1731358422; 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=ksO8SqAXJqFaIqtJzAZ8SD31dOTCRbGn0K1W12lh9q7mlBJdC3NUcHgOyKJJicJiLtn8i/ GSm5YwgNfBG8KG6iIImrn74i3kXMPX2vsHALnLW8YwMGcWfNxzJT5yZ89K8FIpkMdUjOpH rHrpyoGVdcO+enbD/7egcWkvI8OjzgU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731358422; a=rsa-sha256; cv=none; b=ssrVgr3vRKK319yw4rWIhmeGsZv7TlpTTkRakItL3dV9nmpa942v3zufAIGJqLqcuEYNDL IqV8hEsVPHLTKgRrSwLn4mLRfVaQauhPlvfoSX0mx/GjIjiJWJHAzZrUPNnjKxo0i6hGxQ ylR08NE+bKTlak9y6rBgRl55DVdv9jE= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=GuL2tsqq; spf=pass (imf01.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.46 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pj1-f46.google.com with SMTP id 98e67ed59e1d1-2e2ed59a35eso4020645a91.0 for ; Mon, 11 Nov 2024 12:55:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1731358509; x=1731963309; 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=GuL2tsqqEmRZzanrdCyxh13p2SsESjcncEXwK0v1dgtm4ks7g+35wJDxJgyR+UKRDi LpoN197e+QVgbBLPAIXIPfSiLWTyKxXjiYRfNU2Ov+3L76FFLRMbkZFSkzwrjabiXRUN 2cAmTKNiD75c7DBQvDKB2WBUsybMug/m2/PB733zeSz4XbSkYUPwZyg4DIzj51zZ1HW9 p50fQDIfz1JAnFB9RAtZB7UfzmlEgMc9QItgit0b9ntsKH4rC/yA9RAtGd0qrvN0W19H bbulNLXAs04yFiZ4pQLLDCNnfpoOg8wpb48lc/GHMBuNtQ9xAzO4Au00ovbwzydtCA42 7UEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731358509; x=1731963309; 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=AO8JdmLAN+tF4DHjz3hPPGjlFUfsz77HaTOjHDo1a2lAziB8yoHeLBPj7kxklw2yHD WBbiL/1QRIL9I+godLNkEozEuZWL68ZpURWwU9loA08/Qtrv0SmYrQRoLxHrm1wsEo0m ROHPV5fhGMqT1kZTAxpYIX6qtW6+CIm7OlowwRb+SYBl1cSame43aHZg1CGYEKxavLWB Gyd7X1NVOY1CliAd0QQWdSBncZG7NjBVo/WleFXR1PVPqj1FpgVkOKLwVJarD+5eStz5 SPCNoK86DSdGOTLOKFrRe08zkHb6XH9emfRsubDz3ynE0t03cd55dd4L+U2Ax1FshyVH q3Gw== X-Forwarded-Encrypted: i=1; AJvYcCWjh3v/v6uO60gAil+ahVmqr7oMiRmdyuoO5cf7jVw3+4P8MNqS7Drm5kOLKvHztgauI8M8zdvUvg==@kvack.org X-Gm-Message-State: AOJu0YxqkDecEaYBCRQWd7PUUqySGYqIbhuKqN20kUrbSwzgqAUbXIcY Ef6vhrxW76qR7zug+dCB3LsDrCt1sfhrvwoSVk4zIQa0mmeBzBEiTzeV6fSwt2A= X-Google-Smtp-Source: AGHT+IF0EBbCrqmpMhAGCsvP469pTxbIurWJLKmyeQcT28uswVwp6gE4Luabd0L5cnP4I+jH5QjLew== X-Received: by 2002:a17:90b:4a87:b0:2e2:aef9:8f60 with SMTP id 98e67ed59e1d1-2e9b14ddb3bmr18445116a91.0.1731358508607; Mon, 11 Nov 2024 12:55:08 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e9a5fd1534sm9059974a91.42.2024.11.11.12.55.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Nov 2024 12:55:08 -0800 (PST) From: Deepak Gupta Date: Mon, 11 Nov 2024 12:54:12 -0800 Subject: [PATCH v8 27/29] riscv: Documentation for landing pad / indirect branch tracking MIME-Version: 1.0 Message-Id: <20241111-v5_user_cfi_series-v8-27-dce14aa30207@rivosinc.com> References: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@rivosinc.com> In-Reply-To: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@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: rspam10 X-Stat-Signature: mkzriijqs4ge8scs3i66b94wwcns9syg X-Rspamd-Queue-Id: 3641D40004 X-Rspam-User: X-HE-Tag: 1731358477-554377 X-HE-Meta: U2FsdGVkX18yCqfHcPPywWm7iTKA6pMGAVhawCdRpMyzXKmKj4eCK0veU8ZSZt0StyTLg5W16fkvSofdV2sx+BAJ8PHFK/OKGA0QAobIK7P2KrWPhb/Jam1yFsAcYFDnFPykCEHNw3gv/2eO0RLH80DJ4WLWYlesNfrgqjZsqomF1MjcWQSe46HaOOHUX+9wRg1jFU5lfjjM4qqZKTpAS+QqckT5Ngm9+5byJrT3ZQ+y7a4eZaSseuMYYi6LTcQAOmqt064xjK/5rbDsvyljGQZFD4fMDTgKHzt7OHrkTDuJ+ZVC3rtQLmeSTWgdnio6Qg58tr8MmZzWv4xyx/KmsgS40vPZOYnk64C7xoJJuMnsmErI6FS4Mi4AWuECpDHX6rqprN+dDAoL53t3pK8S9Uo1nxiS+4LuDoLUvazh47lOwDNsmWxOs7ri/VbGu3piMqgIz4mSmRUr8yYcWeXKr/y9fWGydWYAzuKeLyIR2UTeNO3LUmouJE3oggNSgQ02L/6+56wWHYtZMksHpiD8YeZIWdIfMpBl2azNDSkI6BhEzDkHjGjxJ2Rm/KUa7ylQPaqDoG+ympzeMGOdkkywAsSvSIpJKa4ky99bi+NWXAHk6zBLWZQgVEfR2eXVoUMwLLmZsoRF+ZYSgWoEUL9vgijkqIi0i80H+hMYwfNjGv++n6jSzjpQbRqeefbFqZH9ekVZK54l+4qZjmeKkR9LhgdvKnA1MWeahaBAhkAuSJLvGnje56yn/nQeLj7jzcaoRG0r0adMv3RoVcttQ2g4MwVraTavnWVjjX5SOqREn5Q19idNp8tqYTTTIDQaHLIfk70j+QwcWEebEy7gChFYhF+wh7Z65eIOa+AxrtwyagQ7v7T8tj8mlknIUydIyxPI0d4PcDckXDYwN3UCgobRJAO1FxP8SNHpCyXYldZoyKOfjx4ujJzYeobgKmiEImZwifyToKGKNBk8cBsdXfA IoTpqocN oY/ZP3YE/lOU0Yj6iDa9rKXTmwTat+ptXIa8JBAGBpUIKiTQVuritvMi7M6EtArcW3iopEqYmJ+82MC7I45Am42ilONKv0i9vO9mL/Mm6VClO0eRIwKDYmzEy8hmSrW1uiNMGC3SdmHEp5sg3/k0kTyu+MDfAeZWuejlL1g2SZ0LMuojOh4OQMlP4I/6/CQdZnX7rDozteU6BOxdd5BGhYEhuvsiebqZ7PIQrG2z7+RCLjni4obOlPIanhps6nuaI4O1cfb4ia/BoEWbRpxXXpKXFXxj2RNAzuXDWcP1fiGJ0GC+m2YSW68oDUzoowBKaKaHhpwDaqYE66bVDRHDWXg5Og0VO7Z3TYC9kF/QmcMa77QnJ2FeK5f9KTwsHwfF9kQEEBi/ZoeVGdkf0iGHTHezJ3Blv7rNGjbzmcewertsnjC2LjpFjghiFEiG8pTWPTIcCUBF3zB0+gRXJCtXhJIVmhQ== 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 Mon Nov 11 20:54: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: 13871366 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 C78E9D3ABF5 for ; Mon, 11 Nov 2024 20:55:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7C5336B00CF; Mon, 11 Nov 2024 15:55:15 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 774436B00D0; Mon, 11 Nov 2024 15:55:15 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 59E226B00D1; Mon, 11 Nov 2024 15:55:15 -0500 (EST) 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 305AD6B00CF for ; Mon, 11 Nov 2024 15:55:15 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id EEE5CA1AD1 for ; Mon, 11 Nov 2024 20:55:14 +0000 (UTC) X-FDA: 82775018382.15.2186BD1 Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) by imf06.hostedemail.com (Postfix) with ESMTP id DC09E180011 for ; Mon, 11 Nov 2024 20:54:43 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=x0+4JcaC; dmarc=none; spf=pass (imf06.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.172 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731358451; a=rsa-sha256; cv=none; b=aZhFGD6BTYzkjMZ4iRtLLHcnLIs8Ifo4vtnjA5sUTdPn0n82xiQ67iEXGkSqaPcSFfLnCP oBCDwZj+T9mO36azRjwvooE/HR7qJbmzAu2q76HaC4vCGJfO10W6MyT7Cc7F4tuZ2qE4wf Q0687LC4YDGvF/t6tcwKmC6ZzGsR6rg= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=x0+4JcaC; dmarc=none; spf=pass (imf06.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.172 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=1731358451; 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=oUb2gWG/kfPY6eSO7lvgU6ajCCP9kjd8Fhj5tF2zscTU9P2qBA1PiBSubJE7s9InesN1nQ dvJ15f+9cP4GmnqmfC1l5L5zuEHsem913gu27m3HjSBNNrh7TM3D9Plav9SSobnJJ4ydZu tUpTP3TkdDcHxHJ15iix/GAVBwIu1LE= Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-20c714cd9c8so49964465ad.0 for ; Mon, 11 Nov 2024 12:55:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1731358512; x=1731963312; 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=x0+4JcaCCjHqpiYi+v8vDeiYZm0E9DpWeDpw59a/Q8sXkel/CxbSP6vN4lOOKreMaA cc8KM4XcGiAR3CbMwFwBQnmTyxVQChYu90EoQ7PFSvzInnONI1LbNdXvZVn6+o3f0nx9 R3jWpltpmYbZYnP76CmcbfhEInQrhO1aL63o8fIaJwLJ96a6kwxGsryJPy8Fkjvyh4fZ aWzcbrxJj+kg+Y3m4OJIrJgPoLtexEeEKRzy228C5RrBpgg48rgtmsMrL76233hBhWrV ZTg0zS9t1+xXNxyHanAS2p5EHRKsmRTeJme+ghlxupV3gO9F44+wuZdSUWGQBd6hkDXp vhmg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731358512; x=1731963312; 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=iJtdZVVX06D1gQBqrWmrVA4oDMaCDj1Ueg9WenVLohH4SwelYhfuohwawERak0auTZ rbtxF/zbPmiYqIQY+yYiqFYhINMhGQuRr1nF/1UHLtLOv7f63F50Rlvp5XKGRIJ0F9lQ YCaCZpQ84ToAwvjQWjThsNJ3uDdKIE3cR8fCPsenrY1EyeoCL+HfxZQpJyp//jze68IA Lk2Ikghj+ERJzjqKGpipcl8geFDv2mbI6/llOgIS3HQewgGtRU4cIIr4oyc2139tSuFX zQbOkubB0YG70VpiLZ2CbP6DagnxFfHsD/nGYbYzKctKbahvmAO5Pte3P6BDSzZTHl+C 1jcQ== X-Forwarded-Encrypted: i=1; AJvYcCXqa6/btuyBZ2+KML6Hdiq7H7tFlCdA7VaegVeLebdOdxSr0P55n3AexY/LmORVL1LUstAr7K/60w==@kvack.org X-Gm-Message-State: AOJu0YwBriBV0a1denSA7gmD7L6cZ22nLVhktyfEjREExl8WvEXH6o4l AnJpB0G8ys9IEa431V05kRsu9PCUgqIMSvNiMxyL65M0ec/KAQmqcjtnIUio6Eo= X-Google-Smtp-Source: AGHT+IGvZ1Qr8oDf21hnmw+UY/eeYz7wQwk8M8v9lcOAYNLAnYOjMSOnfptGUiFBUzt6eZ3xPP39jw== X-Received: by 2002:a17:902:db12:b0:211:6b25:d824 with SMTP id d9443c01a7336-21183e11eacmr202524465ad.35.1731358511517; Mon, 11 Nov 2024 12:55:11 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e9a5fd1534sm9059974a91.42.2024.11.11.12.55.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Nov 2024 12:55:11 -0800 (PST) From: Deepak Gupta Date: Mon, 11 Nov 2024 12:54:13 -0800 Subject: [PATCH v8 28/29] riscv: Documentation for shadow stack on riscv MIME-Version: 1.0 Message-Id: <20241111-v5_user_cfi_series-v8-28-dce14aa30207@rivosinc.com> References: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@rivosinc.com> In-Reply-To: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@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: DC09E180011 X-Rspamd-Server: rspam01 X-Stat-Signature: 8oqkaq3c5ukxx3dsizesch3dda87esy7 X-HE-Tag: 1731358483-210280 X-HE-Meta: U2FsdGVkX18115BqxFS3xlvnQgRWsxiemV1t6ud/xiMTdSsi9oA15RsAUMjXTgJPMAeUT4RZWNs6QiSRhmW1TWCA2r05P0SHFXP7jnwt6xS6bkpVFzseUyGrPAGCBXwYrUnDTOFycpaiSxtsEWtTtVtwgg/d4f0D4v1LiFHQ+uMQzPWIoRUkj131n/TTmL1V6Znhmteu2+pNIzw9R6m2jhqNwnxEmXY4fNKs3WTiUK76YH/gcgnbyjAQ3AeR8Z3Yr50zyYUv+fMTjZ16qCG/w0+tm1kfGouOwVXMslw/yuAuZlL54SMYgIOCa9X/PEDK7joocnhbiMZlKF0SQSVql8vMQs3cVJ5wFa7mnjKe4ItKcKTMuhXMCwlyfmlCiBLQyfDslxSFgsDLkpAY1/ziPcRJQ5f/LPLyxcw9F2aHjAEpKx7rjCSpWrnD6ICoRGPSMwiS5/aEzY5W20vWjRmjaBTIurJGYi0ElXnU4eAEX6oKC6fQatXRT3x3cdQL7nebGMeTIjoIR9cSk9+nCFxGyW8B82X2vRGjQ0VX1Rzjpt5eZaC7i+yQA7NwvWoC8XPdSuRlI+Z25PoOff76IaKb6OkiYysgWFGDduX3UmDH4zoTqEm7K+0foOgN1cYgIEI/qJE3C2gdN1nna1JeGTr2vEP+tfYeFsQeh7S5lL2s2gLsJBPzIiPVDV4pyvZP2SjoH9jOgWb24JP/bSzqgeJ/LzhBIbH9mue9S3j0rq/+0TyufqaantlYEZwWTJJ+jF8t9WJkgWWFFDNk4FRq/HpFmMDHY9FQpRtIc3dtiG7DmtXMBS9xCsM6b2mp4KVuD5w7OoeWLbOel+3QHHWX4xUiupN2kzZgwdgb6j6Ksji9n3BxKQzuayXUS+xIWzMEfj7TK6zSKCSXW/AXRBnXR+y5ryVy2rB3kPP/r2W73zNyfeBTEKBg+lIOebrhdJS53DxRN36iAWoyat4g4ayk/lT Srr00ZEl Ypx1S4bRbuGiy44ZRd05MkDew4BneNV2F2gjgbvpKWJRy7LskAZWVoOyseHsu2lIBHXGUMVC/EKkLKpHSNTtYeixs0AdWMC4T/+niQ36w6TbaiwlqikZrrksd/DiAaRJZaZPoQikMVXstjIZLD7lyE9CvQO9U4s7yEMUTeNr28ofhIV7BTnpK+oh9pZlY+MYg6X5w1oV0f1J3hft4HZ5FFjPhmv+NBMrwu94pJvEaZCVS2Df8+LNvjAX2rHZ91pyDIQwuxCh3GTbRxUQ+j7gQAmDiADvG+JQLicYKb33mZig6NYixXQ+VCHezXEuR/YeJanMBK4PxPxC1fn3NB5iI+PDEBEzx9GHsnMQB6a6Gf0inEB+Hms7LUZaETAVyakpAcer9+cb8Lapmk3gygqhXGrL5JIyJVEczTESwaZU5z1jbo8PjlhObIiRtqvE336Fo1gUO2LePBCm2zgCZqU0mGiGRCQ== 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 Mon Nov 11 20:54: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: 13871369 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 4E127D3ABF6 for ; Mon, 11 Nov 2024 20:55:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8603B6B00D2; Mon, 11 Nov 2024 15:55:18 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7E7696B00D4; Mon, 11 Nov 2024 15:55:18 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5299B6B00D5; Mon, 11 Nov 2024 15:55:18 -0500 (EST) 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 2679C6B00D2 for ; Mon, 11 Nov 2024 15:55:18 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id BCD2C14179B for ; Mon, 11 Nov 2024 20:55:17 +0000 (UTC) X-FDA: 82775016618.17.F4C0F16 Received: from mail-pg1-f172.google.com (mail-pg1-f172.google.com [209.85.215.172]) by imf21.hostedemail.com (Postfix) with ESMTP id 3A2601C0015 for ; Mon, 11 Nov 2024 20:53:58 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=L1shpT8I; dmarc=none; spf=pass (imf21.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.172 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=1731358429; 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=CxVA8bUtehxXCmHJsBv9IThS3ma8UxKbLeU3wFZgv9c=; b=gXofGjn5WUKDyG8vy4Bsfb0jMSBHUXWB0VqJg31fQDeBy9Jwj0WaWy9bo8v6CA4K2zSS9n vtzA+COhNdMscIj0y8A/l+q0TJ+psdyltmr6KKiSHB6lZft8bMWwzpuNq8ZNhMH3PPzoEx NeQ63cxZmX5OFdbLQl1KS9It5vADHnI= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=L1shpT8I; dmarc=none; spf=pass (imf21.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.172 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731358429; a=rsa-sha256; cv=none; b=ONo4g5mRnuhVj+M8QNCvcFkQBQa8ozG1ju3QCt285UqLkTp6ieikqO5Gtx39r91q0x1an+ lsdRPJeIMFroha5+P1zsjk023AXvqqoq5ILcQlHln/XS9CKf4ABLFDMXaZ/CAKuRczLSw2 NjLO1g58S1nEbmMOMRO4Ahsy5BZAsF4= Received: by mail-pg1-f172.google.com with SMTP id 41be03b00d2f7-7ae3d7222d4so3563225a12.3 for ; Mon, 11 Nov 2024 12:55:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1731358514; x=1731963314; 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=CxVA8bUtehxXCmHJsBv9IThS3ma8UxKbLeU3wFZgv9c=; b=L1shpT8IlVKJpVRT5TesZUfBOu4JNnDfHEMT3G9v+PXLpDF9xvvs33V/CDEMxPMyy6 twcpj212ukuGURf1VuB9vwCMrck2tR3w6tRK7qMHQ1x4x7Zqxm1ajqaeyTC64l0r+DZv Wtj0xVPHdtpQwQeMxlK4XkfXWGsK1ICqkCnNceaelLxOJsnqeZpQ18YGuMOz1+XZp6yA yTGcoiuprI97qXbtmZu81YxdTu2E+nx4+qWqNA2Lwareip7fAVwAUk8EVeobydC9qt9l sbidoHHTliXq2r77E+CZIcCmiLqexjzrZn9v+tibQJgSuolu2JO0f0TM3Y9znboMHdEP 5Lgg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731358514; x=1731963314; 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=CxVA8bUtehxXCmHJsBv9IThS3ma8UxKbLeU3wFZgv9c=; b=ehQmFzOBmNJcRAGBEWWxF12+WgAsyciZWR/2nMRJO9r7bVMKj8anjVsCHDviG3h1er gC3MiKdTYwVn5omVDefGIRbWSWCcIGDnjm4TvvI2XykUKTZzavX898KRNEGVnQs0Q/x5 j14h0SUMLBHO6pkOpynp/bwTKVmvDKvyt28IuVHCI/cqSDZ9LV54U+TPf25qBHkKmoav LjKrOCfqScp5Yj4Msdxp9pYeLUP5w9b8N3GtksTeFfPTTW70dEpx+IE6R3L07/GgeKGk Z6ccmDIK2ZR1gcn5bLUHRHr9hYn7g5HOAp0tAed6IX4uAKhzIy9lP+Q7DQ806oaUxkAK FsIw== X-Forwarded-Encrypted: i=1; AJvYcCXb3GJiplAL/JrN48oICAYK7jb5grDPDp1RUCS2cKjtXWFfLPApKt1EPEMZ52I/BLz17AW7fiIyUw==@kvack.org X-Gm-Message-State: AOJu0Yx7Qj/jVi+GQuwSzL32tRCymTVz8GxrIX1ekJk0F690Tx3sGEOz +Dx4sAF7x0PhbnFfX9QqW7+eRwaAo4iyDS6cvK2Di60Yiuj2JmZsexG4AZM92cA= X-Google-Smtp-Source: AGHT+IGWgZ3+075V2zu2hjdNdbSQgE3eKf0Iz3jht7z8A1cfbiXXecqdNd9tbmwxIYmnswW6Ohq2cg== X-Received: by 2002:a17:90b:2d8b:b0:2e2:b462:1d21 with SMTP id 98e67ed59e1d1-2e9b168245fmr20187650a91.10.1731358514346; Mon, 11 Nov 2024 12:55:14 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e9a5fd1534sm9059974a91.42.2024.11.11.12.55.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Nov 2024 12:55:13 -0800 (PST) From: Deepak Gupta Date: Mon, 11 Nov 2024 12:54:14 -0800 Subject: [PATCH v8 29/29] kselftest/riscv: kselftest for user mode cfi MIME-Version: 1.0 Message-Id: <20241111-v5_user_cfi_series-v8-29-dce14aa30207@rivosinc.com> References: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@rivosinc.com> In-Reply-To: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@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: rspam03 X-Rspamd-Queue-Id: 3A2601C0015 X-Stat-Signature: a1m4ocbx9dazo3tp5urzw7g479awsa5h X-HE-Tag: 1731358437-914080 X-HE-Meta: U2FsdGVkX18JpWnvP/k+pXEQFZMWOqxbM4/9AR/7MkbhW640oi4NWhJwZJKOZ/36xlnZPeXgF+hydreTyUHpZar7od1+yogbTc+r/UZ51P7iKFWGDHWEUKzBInL6ve2a7FpEuo+yWWQCuRBGTCm1aHLAxvxk53lgk4QybcEQF6kD/XeSnqasjrrsOQkXg0VTr0Hq6tin4zVXv+gHt1a6YgeK6boLC47PbF+K4ONYzYWX/qIMRgIgnEfpwNg8LtW3AMPgKxz/OPbBfuCvehk7b3ajklhl6d1qmJhna++bpp2+j/tavgugARSiluvApLVHdNBMPS/MhiytS7ch21/t5bufqJG1ToA9KBh+5BmwjZMdAje6nAI0xgJJ4ymTQN8xiYnoxs73Nba619XOW7FEXCWn8ZV8Wum4gG62UVvWz1RV9xRJaQ9Q2pkNRHqHMzfX5EBwbzuAigCJKMNtesMZKo/C6kL0TGbWL7IH9edx5JCRdzxIzVSJX7oJb9H/3xFnHH7TlGmRmhXyY6lqUAE2vZncVq4BNPdpXy2+My1O9D8S8ai0aLhrnUTfz+SAkl4lhqL/t9qubKpjUuPwvcFB7oBbccAnVm1EjuGoQ5xwNCs/mNmNGw2UxCOV16KuyoEQVfx2susvDWN8qsdHeiA8onazddahXgrh6CM4+NwCqDHgW2dmwD7bnj5CCXIFng97aQvW+R8YDSlLV2fYkAP3jr1W09bI/Cdb2oYGvxPMZMi3xFFAK3YXTszPguYaKtY0525RTpB/UzJGsSjvRUic0hToF7pYWG2gqKsjDzt1e0VVHv/Tt7jlbsu+UYQghLo0w5nXptLJRzkZ5LZhFj2/h83cmhQ97e3uKB5n1pqIYnYPgFt6BsLk4fYket9bcLUtLKCMmCGcm8kuqaGQND7ezqz+RQsEjUX4i23w6iyjiXS7nKhjXqBMzQINMo8tujbfzkzftEXzk5tG3WR+gBt iGDTP3Uh IBzMNe7pE+xaAql3I1/GiCn0LChHOKjDC4S1hABgPXCOj5tH0nRpGJjzVTzv7cSYNM7YIR2c3qO7pGquLZi5pWwyJ+PQBDvyZyVVYJkxU28B7k4NN6rLF28EyeLNkm+WzR69vqAvBPu6nKkg9mXTo4bNZ5G9qZuhRSfdTbQSQ7eIzZVMSItuUr2KtYmSAMYdDwEStyRS3rP4ybkUnihfGF8cBDaREvUr/j5zXSjzucV/zfkL7W/az/rlym7Mxtw8qlkPwwpTxhec/ahBAQXbj7itaoR/Ce9As8SUaJ3PZDTNPe1I403cpwdjefisodo22/1mopKQDkS2pUL1/gG5j2Tt2WGa2WMwZI2DENhtcP7KNuJ+qWjS6zYTB/00fnN1kMTUvqRd/BFRTC0+y5t9Tce3nJnc2+zuKa93LDvAEjpnebUgGmTUSg3kbYusheM8X55NNn37y7Rv0N6Y= 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 099b8c1f46f8..5671b4405a12 100644 --- a/tools/testing/selftests/riscv/Makefile +++ b/tools/testing/selftests/riscv/Makefile @@ -5,7 +5,7 @@ ARCH ?= $(shell uname -m 2>/dev/null || echo not) ifneq (,$(filter $(ARCH),riscv)) -RISCV_SUBTARGETS ?= abi hwprobe mm sigreturn vector +RISCV_SUBTARGETS ?= abi hwprobe mm sigreturn vector cfi else RISCV_SUBTARGETS := endif diff --git a/tools/testing/selftests/riscv/cfi/.gitignore b/tools/testing/selftests/riscv/cfi/.gitignore new file mode 100644 index 000000000000..82545863bac6 --- /dev/null +++ b/tools/testing/selftests/riscv/cfi/.gitignore @@ -0,0 +1,3 @@ +cfitests +riscv_cfi_test +shadowstack diff --git a/tools/testing/selftests/riscv/cfi/Makefile b/tools/testing/selftests/riscv/cfi/Makefile new file mode 100644 index 000000000000..b65f7ff38a32 --- /dev/null +++ b/tools/testing/selftests/riscv/cfi/Makefile @@ -0,0 +1,10 @@ +CFLAGS += -I$(top_srcdir)/tools/include + +CFLAGS += -march=rv64gc_zicfilp_zicfiss + +TEST_GEN_PROGS := cfitests + +include ../../lib.mk + +$(OUTPUT)/cfitests: riscv_cfi_test.c shadowstack.c + $(CC) -o$@ $(CFLAGS) $(LDFLAGS) $^ diff --git a/tools/testing/selftests/riscv/cfi/cfi_rv_test.h b/tools/testing/selftests/riscv/cfi/cfi_rv_test.h new file mode 100644 index 000000000000..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..7f04a935f79f --- /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