From patchwork Tue Oct 29 23:44: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: 13855717 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 256B4D7494D for ; Tue, 29 Oct 2024 23:44:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A575B6B0088; Tue, 29 Oct 2024 19:44:21 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A08116B008C; Tue, 29 Oct 2024 19:44:21 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 880586B0093; Tue, 29 Oct 2024 19:44:21 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 6BAA96B0088 for ; Tue, 29 Oct 2024 19:44:21 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 1069F1A076F for ; Tue, 29 Oct 2024 23:44:21 +0000 (UTC) X-FDA: 82728270828.02.DC526A6 Received: from mail-pg1-f172.google.com (mail-pg1-f172.google.com [209.85.215.172]) by imf20.hostedemail.com (Postfix) with ESMTP id 0E68F1C0006 for ; Tue, 29 Oct 2024 23:43:47 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=uY5eYLK2; spf=pass (imf20.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.172 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730245378; a=rsa-sha256; cv=none; b=lcTVWjTsP8tUhzrIl1Bfzy7ZIKKRhv9SxRnQcUiK4ypa/Akbgva5i6MQ6EmdHVu0tm/R27 CAZDnyziKSnqcc3O4T4CNJYGYpIVY1qZaKm/FiCMha1vVXtPkDzlPfbLkMdkHL5pCbVO25 nCykcHkJPsl3AAF/2wYg6ClKoF7tjoQ= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=uY5eYLK2; spf=pass (imf20.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.172 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730245378; 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=A27TSZWEvDZAFIGCzgSTP5vgsRCljOGvihxqekfN1m4=; b=jk8ZpUH+JuKsWI5IXLl/gsc2hCXEjkqMVl06kBUgp1IoVsq33bxdYostKK/9TRB1GLV9nA V+ahANQ3eAJsddeHGapY+sDkEafxAFhIhmxfSJzFsJBu2LhsyEOI+Llr5jHilgkVqgKixy nwGwC+z/XgLIa5O4vrJxEfp0FMH0xlE= Received: by mail-pg1-f172.google.com with SMTP id 41be03b00d2f7-7ea16c7759cso3070101a12.1 for ; Tue, 29 Oct 2024 16:44:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1730245458; x=1730850258; 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=A27TSZWEvDZAFIGCzgSTP5vgsRCljOGvihxqekfN1m4=; b=uY5eYLK2+Rsqej2SoR/PTF08Cxi/HRGrT1ATiw32EmvygDSiFt6So0DIitNlQtGdFY 6DBBnZwAeXDixyH4MMhyMGmBfiLlmsgy+ym6bZvJEPiUdBDECPFKpGLpxTaEej0fYfDj PnEava71kNF89Q0bpl5lpzvCe4PsqGWVXxPKAWWi0XenGqjKHBtXz+ZiNi73s3MDwmkj /cidQK762Tr2Dju6pgI23ncN+bhfP0IJ3kObdCbDMUWtqISJA+AOTByjHcK4K1US3HLj Cf+OEDc9xr8r2YeA3/6gcVyZTdfww1jmZbzOC6qXlFJC1pNf8LYBQWVy/ZB8ZtMk8yrf O+7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730245458; x=1730850258; 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=A27TSZWEvDZAFIGCzgSTP5vgsRCljOGvihxqekfN1m4=; b=jYic7XQGnf4QnEKeefcxrWmAlGMHQxXkQf1XdFtlYE6FQvfti6669r0xxun0WXc/pk zybTbOiM/gjuLrlaOIT9YdzZ0C3vJFGoz7jK04WNnkQBPIhRIprcHrr1vQkY9jxvs/ww yDjIgmYR++gtNy5DRIZRnUUr9iMZHBcdiZC/RblcsA7PW0UOK5ohhcBIoIpeWdg2qblw sCVazZ8M+GsNkA9Wu652jIAPIngt/JRwSzof0XAG06r3qdio3JOQ5sR+wCorWnv7plgN pjh54Vm5T32klxArq3gYygvg/PQIhCpstAE1FUf/P19zAZ9OEqsKClUX1BcjFtb6FHXS zhDQ== X-Forwarded-Encrypted: i=1; AJvYcCWGjcU5Muu3luiN+uR8KQxHr5mbNfTqR8e3w2rrEQ9wrENTm0o/GqcqIuFdP66+RWysfnhGC8CjTg==@kvack.org X-Gm-Message-State: AOJu0Yw6WyS64QhHIicvaN05n9P6kqwd/NWBtYnXwvWs9cgVvzelklJa oPEGazWsEhdbHZTEUOK7z9Ccob7Nq7DGG1pRep4NLfP5wR173A4SOiZiHKzx6i8= X-Google-Smtp-Source: AGHT+IEl2QOb2QqMdMEvJJy28IFG3kLdP55DbuAo5+86qcvJIXvWMGt1Fwpgzfx+V0+HBr7ZrtesUw== X-Received: by 2002:a05:6a20:d521:b0:1d9:181f:e6d8 with SMTP id adf61e73a8af0-1d9a84dd394mr17393682637.31.1730245457862; Tue, 29 Oct 2024 16:44:17 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72057921863sm8157643b3a.33.2024.10.29.16.44.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Oct 2024 16:44:17 -0700 (PDT) From: Deepak Gupta Date: Tue, 29 Oct 2024 16:44:01 -0700 Subject: [PATCH v7 01/32] mm: Introduce ARCH_HAS_USER_SHADOW_STACK MIME-Version: 1.0 Message-Id: <20241029-v5_user_cfi_series-v7-1-2727ce9936cb@rivosinc.com> References: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@rivosinc.com> In-Reply-To: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@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-Stat-Signature: o9q94n4xr8c5jre7dg4sjsk4ub7ygpfq X-Rspamd-Queue-Id: 0E68F1C0006 X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1730245427-384669 X-HE-Meta: U2FsdGVkX186mBiBU6lq8OE3ua3nfPlXlE3wati1oga9wWm0p2hXxxbOoJ9S/Ea6AXVqh/9OFWjCTwDrje7p1cIT3JTikJEu/HutbssScHGHGtZoJ8iYLPbrl+K1BnkOtAeQDK9dxMaXP2fj/ewGrjorAkVZDYmYwFsihYunaD0TCH32Y1g5RUA2lV0DTFs0lNfFDCJ+uTzi/Z4/XDXnligYG15d+rSuAOPin9Di9xkMbrfKA2SSuYzduT8X3cUfrMvm/N+UYD14OqrEuzx0bdLf447QRbAHdgPk1+MVGVbzmHV+yKXhzOlPe4kXvFbZ7++owCUCl13pXq1tyPZnsKy468JrKa2c34dpCF2WfrxDORXAlZEybFFQvLVQay5S+w2aJruyxn1ubpfmoXXHLKEwRGOEyJtf9NRJeJJ6dEmK13+IJAeeQNl1Qw0f6ariR6VAr/5z99PBR69cKVZDsb+8KafUUozbojx//KYi0XOj5PdSfHKH7z7/jafhRn/4Sciwr5N6mtyWGtytbMSMXm2GxfN9zT5VhoD9OahjIFh+b9bDDzTclnFCjOudIQnMmxVwZCJIx9A4u4xIiNKNft9yLQvkrHLAFsMlRYgRq99FPARHr7IuaoP/VVp0sRrnwoo1ciFTn0h9AWgQeYOQzW985QtlQxEgo3WzkqxLt7yDl/LaolvCKZBEBbLbJrJcw3VmxTFW3GabvnET/8BTi8UmgCA1phrm66LUJhvNGgY7zeaQ168m6ZPB34WBUn1yVENBrRq/yHZsRmYTZdZAroXtSpYg+lMvsId2nJQbxHaAMfLwoQUBPnI5vczgl56vjlrsC4j/KBSkc7ypdzi0kjOW0QhhlFQdBY9Dh44GXZj00fNPN766rvnr4yxa/971bP5OF0bm0yKChOknuXZSd9yRi0k+9H7PAaTTCf3p402+u2ygpZxklQpRj2omnExB6JeAsmIlbpv7rb23Ab+ SjSfHsi2 ccaaliKP5Oo30e59BfvBl7aTX7O1RcUnmaULXWhxuqqF2qkAjEw5FUaJjiJUEIa5GZ/a2rlRzSG8DqoILBOxVhjetYaOaeYv+wVUjiJst/yn0xjU1mlyZwUM3siu3F9IvAcyhRqs/n5yiqIbu6FDjWPvNUiHCYpCcJbZEIPTEAZZ7IUgqIZmjzl3DBMp4GLq0WkBfczSeoTKhNqxULtxY1VDIjfMJRCoIRlouJCke50QmRij07IXbKGqNibiRr9wfhmdyyk+wFXzPsVMovfauv94HGZwcW9J/AQ5mKLim+3qu+SWPI2Wa+U8qnm9SSUwEUfTXv0POtEOV2Vmj3MsaM1mNJ4QU6BBFNIWWr7l/IUmcsDIuZ90g+qLTXftmrxVCiMl3aorwnwVWeerT9JdoVzS/GVsnIxMQ8NCcLR3xAN0jRL1GSbMyk886Egvfz2D2/TPiZfC++9+VJVBOf+n18c8u4y5TJSTfewneBT5jXlc0NbRUBMTaMAi1sYRcQtF2YwLanKLPYtLxvUMbbX1kPtTw/VyM1569QhfJ2uR+O6IxJzARXU1eP+0nA9JVm3cuzlkTPAARYEamGgPyoB3qKkteuqXW7hZoM1T0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Mark Brown Since multiple architectures have support for shadow stacks and we need to select support for this feature in several places in the generic code provide a generic config option that the architectures can select. Suggested-by: David Hildenbrand Acked-by: David Hildenbrand Signed-off-by: Mark Brown Reviewed-by: Rick Edgecombe Reviewed-by: Deepak Gupta Reviewed-by: Carlos Bilbao --- arch/x86/Kconfig | 1 + fs/proc/task_mmu.c | 2 +- include/linux/mm.h | 2 +- mm/Kconfig | 6 ++++++ 4 files changed, 9 insertions(+), 2 deletions(-) diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 2852fcd82cbd..8ccae77d40f7 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -1954,6 +1954,7 @@ config X86_USER_SHADOW_STACK depends on AS_WRUSS depends on X86_64 select ARCH_USES_HIGH_VMA_FLAGS + select ARCH_HAS_USER_SHADOW_STACK select X86_CET help Shadow stack protection is a hardware feature that detects function diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c index 72f14fd59c2d..23f875e78eae 100644 --- a/fs/proc/task_mmu.c +++ b/fs/proc/task_mmu.c @@ -971,7 +971,7 @@ static void show_smap_vma_flags(struct seq_file *m, struct vm_area_struct *vma) #ifdef CONFIG_HAVE_ARCH_USERFAULTFD_MINOR [ilog2(VM_UFFD_MINOR)] = "ui", #endif /* CONFIG_HAVE_ARCH_USERFAULTFD_MINOR */ -#ifdef CONFIG_X86_USER_SHADOW_STACK +#ifdef CONFIG_ARCH_HAS_USER_SHADOW_STACK [ilog2(VM_SHADOW_STACK)] = "ss", #endif #if defined(CONFIG_64BIT) || defined(CONFIG_PPC32) diff --git a/include/linux/mm.h b/include/linux/mm.h index ecf63d2b0582..57533b9cae95 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -354,7 +354,7 @@ extern unsigned int kobjsize(const void *objp); #endif #endif /* CONFIG_ARCH_HAS_PKEYS */ -#ifdef CONFIG_X86_USER_SHADOW_STACK +#ifdef CONFIG_ARCH_HAS_USER_SHADOW_STACK /* * VM_SHADOW_STACK should not be set with VM_SHARED because of lack of * support core mm. diff --git a/mm/Kconfig b/mm/Kconfig index 4c9f5ea13271..4b2a1ef9a161 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -1296,6 +1296,12 @@ config NUMA_EMU into virtual nodes when booted with "numa=fake=N", where N is the number of nodes. This is only useful for debugging. +config ARCH_HAS_USER_SHADOW_STACK + bool + help + The architecture has hardware support for userspace shadow call + stacks (eg, x86 CET, arm64 GCS or RISC-V Zicfiss). + source "mm/damon/Kconfig" endmenu From patchwork Tue Oct 29 23:44: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: 13855718 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 9103BD7494D for ; Tue, 29 Oct 2024 23:44:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D16736B008C; Tue, 29 Oct 2024 19:44:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CC9626B0093; Tue, 29 Oct 2024 19:44:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id ACA1E6B0095; Tue, 29 Oct 2024 19:44:24 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 8A0F56B008C for ; Tue, 29 Oct 2024 19:44:24 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id DF2FAC065D for ; Tue, 29 Oct 2024 23:44:23 +0000 (UTC) X-FDA: 82728269526.17.8E44E22 Received: from mail-pg1-f172.google.com (mail-pg1-f172.google.com [209.85.215.172]) by imf30.hostedemail.com (Postfix) with ESMTP id 9FFF980006 for ; Tue, 29 Oct 2024 23:43:34 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=M+ubaysV; spf=pass (imf30.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.172 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730245249; 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=c+Jub7HVMuRDhaa+pd538ONaRqIx67oBmBuHa5UsgJY=; b=1M/vin6JTydAGvbpLFUueXotwx+YzIuNFhIZ3Q0YGqXZuIN5p74cgjCbdfyvo7EbFLziOU vlQ955lBoCWMw/29MJKW5IVzSlmBWxrBzOGwueGu/WIvcl5MyHUKhkOWRbnPyYsDRDmYeI NWBMaVwZTM17/CNHEOr+6SbmYmzMIuA= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=M+ubaysV; spf=pass (imf30.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.172 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730245249; a=rsa-sha256; cv=none; b=QnrlOLkv5hUZbxkNuOmfw16srk25Kt9E4q3XKbj7pxJYn8eUrGzLSA55npCC6db7sy+xO1 nf9u6KOHH/fBeXY5ObpKsUkhKeHiwP/awzrqd71kKzZCPThb8R/EeEjhjrTBxu6Hjx9mKf 5gZFAuOB63wKLMsucqNvkfrqEQJ+Lis= Received: by mail-pg1-f172.google.com with SMTP id 41be03b00d2f7-7ed9f1bcb6bso4111171a12.1 for ; Tue, 29 Oct 2024 16:44:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1730245461; x=1730850261; 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=c+Jub7HVMuRDhaa+pd538ONaRqIx67oBmBuHa5UsgJY=; b=M+ubaysVBgYYHORul+E0BId8e/yn8E/IhQIK9QmKj+4bfO+fZTyZA2KV6zdQDHz6xm uZgMsbi6qfS4v9L5n5poxk/AO+LgosBFkqaP/aD9Tn2m2C5zQovBOgekcAktx+rhRJU/ 3B+OW/X1w3aLPtOb/oCbDkImwbgNasLA36aOKIg3u3R6K79XAkmM7lBm/CtiQw1mUouV ZBGAQU6STbpLL0J40ER7g3HegYG97c5uZPgnOcfqYPwleLULd84SIE6u+kiRdWxr3hHI /2cKHZnNr/vSQHco/u6JS3KtX1ATbsGn3vCprJvlz/4t/jBre5HbgdBkn9jVOD0sWifS COIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730245461; x=1730850261; 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=c+Jub7HVMuRDhaa+pd538ONaRqIx67oBmBuHa5UsgJY=; b=alnb+ax5+XO5e/lnJq0Z+U2krkWA9bvhwS34NTSoHSnhJ4Ly40z+Z21l+A2MqFQ/ye zy5NdYKzLasNVHUiLYcr2pW6uMj/bZjxl9sNnnJImjIetKt1C3FF/zU0eodZe1OhhQxk L1H/AU6C2Kw/rFLXy6/IDSP8bIUvJYYAj2TLD/hQZpspcxuEqOgq6M9EkRCbymMnyoIn NHENk6syY74Pc6DSjx7CI8rFSfq2tQNAiXevuQFk5MVa5FCdiKnU1UNnKs+sRmYEfiRB m6BdkaUUSI7toYy7yPYdLu71KJixLRgK8Td1g4XS64SUD12M00OPfTyJu0vArwoTeipl j5OQ== X-Forwarded-Encrypted: i=1; AJvYcCVOkqaRJ2kizCmZ7cCNuqoU2GV+zugdrA+bSNKPoG65nIbcDAI49RDIZ9oGbsQGm8cF0Og83jgIhg==@kvack.org X-Gm-Message-State: AOJu0YxMb3OZH9ASiyeo2tW3NlamvtsBSsCs/h6aFfSXjMTHZuBhxFqi YfLB4om+iLnRuIhYFrlxZ3C2xaGBoKI5p6K5RXtyDvstAyzYk4kRu0V/0tpGooQ= X-Google-Smtp-Source: AGHT+IHoPlZyTKRFIXDxqY1vxKZ9N2lRuciBVtR3dVasMoQZpZgRn99fcKxS5v1VlmJJEo1cy5cChA== X-Received: by 2002:a05:6a20:2d0a:b0:1d8:ac0b:2f63 with SMTP id adf61e73a8af0-1d9a853aa7amr16243300637.47.1730245460885; Tue, 29 Oct 2024 16:44:20 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72057921863sm8157643b3a.33.2024.10.29.16.44.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Oct 2024 16:44:20 -0700 (PDT) From: Deepak Gupta Date: Tue, 29 Oct 2024 16:44:02 -0700 Subject: [PATCH v7 02/32] mm: helper `is_shadow_stack_vma` to check shadow stack vma MIME-Version: 1.0 Message-Id: <20241029-v5_user_cfi_series-v7-2-2727ce9936cb@rivosinc.com> References: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@rivosinc.com> In-Reply-To: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@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: 9FFF980006 X-Stat-Signature: ce8cnt3m4ziptbo8pyjb8fjfmdwm9jep X-Rspam-User: X-HE-Tag: 1730245414-593500 X-HE-Meta: U2FsdGVkX1+vtSAjr621+4mbHjZkHoQnaheRhMnSPgJOlGB0hN5m6oM7LbeRkDatRiZNlcD8G6sEUTxnQ75mEXeuVhUs62y0bNVkGZrnyZSll+oieQHujJkMXKUv4Df8nVGpEqJLrhdY8s1S06p6yxv5R0TnfqDYlmay0DTQL5Ir67qDtcwV5vAFNBziILKQbWcaRZlFiAvuaHfkOj1sj4H3WafxeerIB0BvpjA1baaDR6LpRQekMDOIlYmlIC+xEJL1JrkJ/CdfvX6MNJ+wLLcSKmivQKbbVH7dBEGWar18croC7WkA45+HLVLr6vioPIgFNrJNnmAxeT39fFyLO4d3dt2zfrxksX8mNK0xK5+Hz5lamsJdhaPMDKBj5K2GvsImQtTea9GoN1HpKKXl5BmbqjMtpM7waz9iEOHvufyNWuE9qiquGx/ULd82gTrl85zRqggGJKPjMibKPvnaKUYjk81ps7q7sSyuqP6Lg434JUUWSbEptmTdaB1h5qLkOWbasUslUxkSZO4jGr4n6lqasIyM6BBnOo5TUla0vIZpwKUBXlgWNy4SswinkkXoJDjMGolWPCGj50yV3FG4T7Nkaj1RrENTxLdXcTd+53tED2ev0i43sHjn/E3FWmKkGaqCTdWD0eZDm6pC1a0l/HHCRwnvfnnyfbB6PF6vSeS4Fg3FRCH41uqxtBEPR85XY9PrJo8jgX52mCxAE3GtCxidaAWIsgQAof+b5qVklEGqQ5wPehApO1nBTv0trdJF3dXz6l03cIEwal5qJb9QlZGmTzacQ3v6i4aDud2oZQIKfMTtLbFfpkWIe7uSXlZKIY6l0pOZqhnvzcT8WLMG/pE770C1i2UVWQFlqBsYGs99PWY4YhcuvJGMhkXqjgQYM15WxTfC7jpp1czr7ePd/tS3xks8SP/LOi2gIttmsx/ONT0uOKthGcKbEc1MZMIpNQnrXG2b/iZFi4sHM0G 7JzAq2Mm Ct6ROC9WO7c4Vsqk7ulGtKQ0L4uz1apOBQK1xvP6j4jFGjW7Bojbqh3+zbBcZb1BLXhkcGY9zsbQvUEziqZiR2nL/2Sv/R46IOdTw/7vymnjclXblZ9f2b5cOcRMEM1xRNApOKn6Tl8tTPAKBQzMPYg15A04MLlN4j9feTyaJ0vu561Wh56NzIem3JZAoS3wXx/Lr967PX5xDpA/eN0COHHVQJjRLR7QYVFQYo0BeyhBEsQ1rozimTEgikdGQAtkZAW3ncteQQtfkfPWiKmA+0MGGMaWqQA0wqPedqN8Gx/vt7UWoRrOkV2PM/ZP2odhvAyIp5HvgQ6t71pW082welhSTNtKo6n72AB9xIdtY46LR9Dp7jB7Ka2m/chanPB/UM8IvFn0KoBdBUsXY6MBnw4MXK6OQQ3oLeMQDF889mn+eVHwlPs4m92wC6zzNg9oK4G1BvgUcnaSExztKgozUkfsZvpNT72qPrGSo/KnsV5A+sfeISD80kvpS/+j5MnvCcYIMv25TwKwAnyht3HVo4TZs/w== 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 dd4b35a25aeb..0853e6784069 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 Tue Oct 29 23:44: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: 13855719 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 D3299D7494E for ; Tue, 29 Oct 2024 23:44:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3F03A6B0095; Tue, 29 Oct 2024 19:44:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 39F786B0096; Tue, 29 Oct 2024 19:44:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 219B16B0098; Tue, 29 Oct 2024 19:44:27 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 0197A6B0095 for ; Tue, 29 Oct 2024 19:44:26 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 948C91607DB for ; Tue, 29 Oct 2024 23:44:26 +0000 (UTC) X-FDA: 82728270576.30.F1C1485 Received: from mail-pf1-f181.google.com (mail-pf1-f181.google.com [209.85.210.181]) by imf05.hostedemail.com (Postfix) with ESMTP id 25C79100027 for ; Tue, 29 Oct 2024 23:43:36 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=NYlO4CBu; spf=pass (imf05.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.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=1730245304; 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=32Fr64bQ4i4M4Gkdnki92yyt1KBIEMerrOdU8b6lAUE=; b=360+if933QqEtITnCRfyMZG9Yl0XHG9yfPnDB5i+mST/7hWZo887uNXslOcTiYkhmliKUI 2OH+I/WWvjccFaCskLh5a/Qum0kDphwiG0OWgm8cT3NQiamioCTSLzJqUY20903HjdPhMW BkPKfl1ugUFiqW6/jDGvHJyzp0TljZs= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730245304; a=rsa-sha256; cv=none; b=ZHclZY4fzeiC1q0N0pVB85HEG9DkWsH1njCs1QXFet1cj9rcBizWNyzLTihEeYhr17bHMa o5gJZsLoroHZAzqhR30Q616wZjrHYyJUWjwabffpK3VU9CnQB40sfSC9aZafLfR9P1evPf MGmyQfnEo8QiMGvMraXapqyiQ/DVV8Q= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=NYlO4CBu; spf=pass (imf05.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.181 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pf1-f181.google.com with SMTP id d2e1a72fcca58-71e625b00bcso4518691b3a.3 for ; Tue, 29 Oct 2024 16:44:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1730245463; x=1730850263; 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=32Fr64bQ4i4M4Gkdnki92yyt1KBIEMerrOdU8b6lAUE=; b=NYlO4CBuqEGcHDL4pFB8/7dUwm+7EomD54LQXoKNiOhHLtHFD7T27UqPHNw0I8ntD+ OkW2kM3zdigpWYypyC/TE7sX8Xx2I9918gcuNYSFMsr81QgazYoTCafaJR+3tGgPopyW n6boWjrU4YNopEkpiBmbjRHSmuPG2h8QY9ASQfaXd+ZcNhMOeJVzwjmcaj/xv9JbzptN tSv/EFf/HYmy8CLj3bUdfdgP9JAkuIEVhQvjtcl7730VXn+XG/Lrbp+7aWNrWj8GSxRM HpiRRF0AzWyUY/TTTlYM9AwJkDuvio4vBn1hUz3BxYIBWycfqtMnMZ9g5xiXIbR/7zY7 eHmA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730245463; x=1730850263; 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=32Fr64bQ4i4M4Gkdnki92yyt1KBIEMerrOdU8b6lAUE=; b=F0kQjkkx+OChLiISYQJ3nyAPIO4OBxJbjVqj5g0GazjwNSft5DGv2ew3zYjCKl+i34 zTmZigI1QvyuOjuhZevzG+I+KznMx/qcOPbLPS2RvwBb4d+yNIE2cJlxPiXHkf/dJHCc UXIlsrhVlFIc7xRI/j9zLS64PKW0cwnvwtBHIP9dSj2ReRt6n2RTreZWT+gsRbMxdgVB me+MMjSGhrx3h9TXcINheZgiRObxTo1ZrQF/lDN+K/CPlCX51wU7Hz910QovXek3700i dX6/06s3WfTlM6IfvNmZ11AtwylQN5I/GQr4wFuFUZLxhVARSGuLgp5xAOyGOIHGiihb YnyA== X-Forwarded-Encrypted: i=1; AJvYcCVVAz8B+14Dwgs8WOTL15hXPguYKd0tcFX3arqQlPw+sUYICfm5r1EF/xMMSNShQDVqzqLJe6K5kQ==@kvack.org X-Gm-Message-State: AOJu0YyX1AMidVhBPQ8CLyNjsgcauOa8xBBEhtW9VXdT9KN3cfzT+9g6 oXqUbiVlebxYB2osVwSalo8Xqf3c6CflV5QVnAKSo6PkXpwXSPrgidvRpbeYY54= X-Google-Smtp-Source: AGHT+IF9XBA82NtKTvuWXQFZShmgImM2eVgaE8hxac78pAdDA8yPlrXMXV9vOyyNJTLolGWQzFlm1Q== X-Received: by 2002:a05:6a00:2d17:b0:71e:5fa1:d3e4 with SMTP id d2e1a72fcca58-72062f86747mr18631012b3a.2.1730245463422; Tue, 29 Oct 2024 16:44:23 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72057921863sm8157643b3a.33.2024.10.29.16.44.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Oct 2024 16:44:23 -0700 (PDT) From: Deepak Gupta Date: Tue, 29 Oct 2024 16:44:03 -0700 Subject: [PATCH v7 03/32] riscv: Enable cbo.zero only when all harts support Zicboz MIME-Version: 1.0 Message-Id: <20241029-v5_user_cfi_series-v7-3-2727ce9936cb@rivosinc.com> References: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@rivosinc.com> In-Reply-To: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@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, Samuel Holland , Andrew Jones , Conor Dooley , Deepak Gupta X-Mailer: b4 0.14.0 X-Stat-Signature: w7xwpd5j1c1o3yp5fmmhrcqat1ym5iix X-Rspamd-Queue-Id: 25C79100027 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1730245416-192901 X-HE-Meta: U2FsdGVkX18p9oPTQLlw9M4FC0VB1mU+XQW2SvNusp2jGcyTbWCWRz9BomeB7bMlDzszbAHCnX7Jh/wiEl2iWdpEb5RU3gJmLp35M5yQXUbmFM2ZvVF41WIMLh+5OE1qoDsiD9bhFMSYFW5mZirEy7NXDjG/ns/d30IjLI24YjHDumjYD+D55rrXnr4ZWR9JIZ70zF9t2Gy06hCoYBU5gn8bWvM7p22ICT55NlM0CTl7g6KKmXSXxhTG9A6Jd6Kn6yeroRayiH+XgWFPPk73ww7USIA3v77+Psa2OAxCk4nSCx4IfGs3sm3SOYiwiHssc7u4D+UqfNEKzIG70s9qD5pa/pR59mmRMORe1pfaNvpcgeOxFUGC1BAPSN5JiCFHFSU6VRVAcNG/cuMxKNKF5UrjP8NtKstXpMN+Xk5BDhNh6E5+NaIkhXQbARJn76+Sx5ml5yWzgA8nv6IGQIEfM9joJdUxAn5A2hKQty2iP5/EBP0liggc1HmzWh8ZFhZ3k9O8atgs6RraxiFY2uaxQ/YzXHGuTGJeDJz4a5sJacVb7IRwMF+mCvX8Zsito13QxWjWPKaz/c5TULKfNpVSo4Z1yRCVv+ipI5wue88ZtiCK2FT0b2sViJgongU+JWeGMypUiTjb/VC/rItvo4kld62DU1AReaEkOPv7ZJ78JjNqn1ME3KR+TC3PJN7qaotmm6gKMSw5b2CHrhNt19iU2rxjeIy0c28dby5JibHj3ETqkcoueCsS/ZrOXKYWT9nn2Qm9bdOV7R5EKnwsv28VqHpTVmC+Msqx1pEznN2xG/l04roElg3lckk+f+2DCAo0FqehnvSJudPvfA2ZWSKd7ibhofwMLvFeL4y9oEC3idB+brXxw5nytivQ/8WdxWxIyVkdkgWwcBhaHrfM08IJL974ly4SMhQP/RRC1W0EcMxSEHOp6eQK/bLfLFNa/vTbO9eoJ+YmeILvtWkyq9k HwoS4qVN WenBtRXp1XLv7vvIyLFSPnG6WIX/RSMiQnSHmNVVRfBiKHKWTyMYRkgvkzzxIhwHLkia4hrN0NwN4i078uWgPnTUmP4Ws3etDWxYoliMhkrZ3IpG5W7iA594tfmIu8IMJB01RoXTfV0ViJ9EmOZMFBDpK8JYumpaVJ6MMrtKMukRdTXxVs/NCaxE41x8iZ5iOfblialIwW9ZmOLEhMEPrsoDj06IoLy1Vb3j4CHkgsDabZHXlcj08161DyWGBMP/tvMLTfPduZNb0u1yF+wTOMsfvkRMsl0LHPxZJfN15hvm5+B8AQcQnlJ1j8fWxYPQImQmHRUZ/hc/CRfydhRfViynTC3QsYP7m2kjUTNZrPABxZymIpJrdTaCEkAssetHXy/G6YMQ6vATL4T8zvllmKFZ6Ua8BhxWnyVCYZOdnZ+3gKhjEI5IuGsfv7EYPMci9K9F+g3tO+1j7YRVfaHSfp5GSt5zMLo2jklgk8488frBIyIgs8bw+5inuTfyN6XMHzOemVNWrJzcd0pDcCqrR4KrTFV7u0tStNZ/L7LFYQ0gYBzW9Lrc6tj5T/wPDp/j3Qf6Fu9jW4ODMuLfpw8RLQmsShsMWBSTTX5KnVppgBj9GrjIWFXfvu9+kJDEaehMJ92k8ZUieMtgT5VQl9Cwi/4BIkfJ+FmZFiOn9 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Samuel Holland Currently, we enable cbo.zero for usermode on each hart that supports the Zicboz extension. This means that the [ms]envcfg CSR value may differ between harts. Other features, such as pointer masking and CFI, require setting [ms]envcfg bits on a per-thread basis. The combination of these two adds quite some complexity and overhead to context switching, as we would need to maintain two separate masks for the per-hart and per-thread bits. Andrew Jones, who originally added Zicboz support, writes[1][2]: I've approached Zicboz the same way I would approach all extensions, which is to be per-hart. I'm not currently aware of a platform that is / will be composed of harts where some have Zicboz and others don't, but there's nothing stopping a platform like that from being built. So, how about we add code that confirms Zicboz is on all harts. If any hart does not have it, then we complain loudly and disable it on all the other harts. If it was just a hardware description bug, then it'll get fixed. If there's actually a platform which doesn't have Zicboz on all harts, then, when the issue is reported, we can decide to not support it, support it with defconfig, or support it under a Kconfig guard which must be enabled by the user. Let's follow his suggested solution and require the extension to be available on all harts, so the envcfg CSR value does not need to change when a thread migrates between harts. Since we are doing this for all extensions with fields in envcfg, the CSR itself only needs to be saved/ restored when it is present on all harts. This should not be a regression as no known hardware has asymmetric Zicboz support, but if anyone reports seeing the warning, we will re-evaluate our solution. Link: https://lore.kernel.org/linux-riscv/20240322-168f191eeb8479b2ea169a5e@orel/ [1] Link: https://lore.kernel.org/linux-riscv/20240323-28943722feb57a41fb0ff488@orel/ [2] Reviewed-by: Andrew Jones Reviewed-by: Conor Dooley Reviewed-by: Deepak Gupta Signed-off-by: Samuel Holland --- arch/riscv/kernel/cpufeature.c | 7 ++++++- arch/riscv/kernel/suspend.c | 4 ++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c index 3a8eeaa9310c..e560a253e99b 100644 --- a/arch/riscv/kernel/cpufeature.c +++ b/arch/riscv/kernel/cpufeature.c @@ -28,6 +28,8 @@ #define NUM_ALPHA_EXTS ('z' - 'a' + 1) +static bool any_cpu_has_zicboz; + unsigned long elf_hwcap __read_mostly; /* Host ISA bitmap */ @@ -98,6 +100,7 @@ static int riscv_ext_zicboz_validate(const struct riscv_isa_ext_data *data, pr_err("Zicboz disabled as cboz-block-size present, but is not a power-of-2\n"); return -EINVAL; } + any_cpu_has_zicboz = true; return 0; } @@ -919,8 +922,10 @@ unsigned long riscv_get_elf_hwcap(void) void riscv_user_isa_enable(void) { - if (riscv_cpu_has_extension_unlikely(smp_processor_id(), RISCV_ISA_EXT_ZICBOZ)) + if (riscv_has_extension_unlikely(RISCV_ISA_EXT_ZICBOZ)) csr_set(CSR_ENVCFG, ENVCFG_CBZE); + else if (any_cpu_has_zicboz) + pr_warn_once("Zicboz disabled as it is unavailable on some harts\n"); } #ifdef CONFIG_RISCV_ALTERNATIVE diff --git a/arch/riscv/kernel/suspend.c b/arch/riscv/kernel/suspend.c index c8cec0cc5833..9a8a0dc035b2 100644 --- a/arch/riscv/kernel/suspend.c +++ b/arch/riscv/kernel/suspend.c @@ -14,7 +14,7 @@ void suspend_save_csrs(struct suspend_context *context) { - if (riscv_cpu_has_extension_unlikely(smp_processor_id(), RISCV_ISA_EXT_XLINUXENVCFG)) + if (riscv_has_extension_unlikely(RISCV_ISA_EXT_XLINUXENVCFG)) context->envcfg = csr_read(CSR_ENVCFG); context->tvec = csr_read(CSR_TVEC); context->ie = csr_read(CSR_IE); @@ -37,7 +37,7 @@ void suspend_save_csrs(struct suspend_context *context) void suspend_restore_csrs(struct suspend_context *context) { csr_write(CSR_SCRATCH, 0); - if (riscv_cpu_has_extension_unlikely(smp_processor_id(), RISCV_ISA_EXT_XLINUXENVCFG)) + if (riscv_has_extension_unlikely(RISCV_ISA_EXT_XLINUXENVCFG)) csr_write(CSR_ENVCFG, context->envcfg); csr_write(CSR_TVEC, context->tvec); csr_write(CSR_IE, context->ie); From patchwork Tue Oct 29 23:44: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: 13855720 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 47120D74950 for ; Tue, 29 Oct 2024 23:44:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AF48B6B0098; Tue, 29 Oct 2024 19:44:29 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AA3436B0099; Tue, 29 Oct 2024 19:44:29 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8D14A6B009A; Tue, 29 Oct 2024 19:44:29 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 6E2416B0098 for ; Tue, 29 Oct 2024 19:44:29 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 27AE8A07F2 for ; Tue, 29 Oct 2024 23:44:29 +0000 (UTC) X-FDA: 82728270660.26.D1F89AE Received: from mail-pg1-f175.google.com (mail-pg1-f175.google.com [209.85.215.175]) by imf05.hostedemail.com (Postfix) with ESMTP id BBC2E100006 for ; Tue, 29 Oct 2024 23:43:39 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="ycBrjs/a"; spf=pass (imf05.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.175 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730245412; 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=CCKzg74NjcYHO/B30bowh1owAHwLHbAhOOOeEQpeDk4=; b=yOR0rcMXILG6a5jpNz/zvD7M8yTHc/rQUpvpeFfwoVdVe/t9E8x1RnRZXYBHeJMU7yApQ1 xoQPxF/GzeSAfhxEAaawT16aT4cDZ5sBCNrOdlm5sYuGSWN9pN+Y91rnnIj8kdy6o2Ens2 dhcws5GbKxKZihn0I7Qyp0SxZgHWYuM= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="ycBrjs/a"; spf=pass (imf05.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.175 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730245412; a=rsa-sha256; cv=none; b=TjcictaoYVrzyDPD2+gxKwWKwm7idw9G2ORjJWCe7ny/aR3g/oHC5BRTK5bCIpPLrbGClY cnXJglfX2S0UdgZKZonI1eYK55sUZfjyOhXwpY3/25bFogtOAZNxRVuMcEH7TjDFVKocTq WU9q2OPQyQQ6Uy+Q2oFvCNiI+GfQBHU= Received: by mail-pg1-f175.google.com with SMTP id 41be03b00d2f7-7ea7ad1e01fso279378a12.0 for ; Tue, 29 Oct 2024 16:44:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1730245466; x=1730850266; 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=CCKzg74NjcYHO/B30bowh1owAHwLHbAhOOOeEQpeDk4=; b=ycBrjs/auYUwCfIJBy7WzB+91VUuwFTmcyzzR72oX+ubgd4SIs8JHGdP3YWvb//f2W S9tgIwqNRnZO6hA+sXCiI9ka1shsJkoh1OPrr4KxnOUg7IVvrzp61FXrG41AjBjCoj00 A5bwWlv0kDkVfIwKbiYTVwczolw5bq8thOSuNwbk/Bd+PGCiyNw33Ijd98Me0MOPgBr0 tI+bAXPPB/jO4XCSootXJGf/bqml8XUY6IEbsKVIB7RQfYBRf/NwRFTbjJzj0WqTMGDg RkLQ53M7TZ3vEYTOgTVyvbnqF24lr+8qxM7ZHakhdEwDvuj+0APOf1Dv5ayOTDVuCJor 4DGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730245466; x=1730850266; 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=CCKzg74NjcYHO/B30bowh1owAHwLHbAhOOOeEQpeDk4=; b=giSR6ijMUCHA+yF/1EHWwJJgB7qj0ovVlTm8c2gkiVO4OrM9O2hZrY7D/Pbg26oYkP /uS0ohW+lU05dXEZF9cdhIyPcZKeBYu0dDl/9qYKyn77/55wXDSd8dfDursPyRyUK/Yh 5xNAYrgUS8Ix0WpsBwdkVdulHrJIrJq/v17iNR6srJbsToE1fiYgWaK0A+lLBDgkmb/b 8UHma7HaNmrTP7oy0DqeQriDSgdnewrCz6PBCO0VwptNYiiKsXCfCFb24y1QQCE7RvvY N2YVgE8sCsFEkQR+Hf163skPOo2vaA8AsQLgJXYhsE5G72GDdBZkt7nSIKG93Ua/yFr/ ZdGw== X-Forwarded-Encrypted: i=1; AJvYcCWEiCu2cqQt0n6JU3vZkVxwewaxFd0RGtwM8uDcyqa/MLBaKEg3IV/YPDKHHuM6UeO4nEoXf/tzkA==@kvack.org X-Gm-Message-State: AOJu0Yx8Xt6uiRaYkM2KEbvDjOowVWtgzl7Ls9aRT5FmlvzvnbU/7yYT FQo3kr8Ii/wt4uFxS30PSI/AexxxsECfsejEdTibrkoOzepaJY/eMfbS1vVl++8= X-Google-Smtp-Source: AGHT+IHJh+1ZXsYwxslZQeQFaVaIosIBJ9byAeINs1Cby21LH+tx3YEEJESgsOz7GEIKZ2P3AAXg4A== X-Received: by 2002:a05:6a21:99a0:b0:1d4:f7b7:f20 with SMTP id adf61e73a8af0-1db7ff5e5e7mr749841637.21.1730245465983; Tue, 29 Oct 2024 16:44:25 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72057921863sm8157643b3a.33.2024.10.29.16.44.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Oct 2024 16:44:25 -0700 (PDT) From: Deepak Gupta Date: Tue, 29 Oct 2024 16:44:04 -0700 Subject: [PATCH v7 04/32] riscv: Add support for per-thread envcfg CSR values MIME-Version: 1.0 Message-Id: <20241029-v5_user_cfi_series-v7-4-2727ce9936cb@rivosinc.com> References: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@rivosinc.com> In-Reply-To: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@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, Samuel Holland , Andrew Jones , Deepak Gupta X-Mailer: b4 0.14.0 X-Rspam-User: X-Stat-Signature: cyr731yq4fuxt1xqmiqwdoith9bq5fi6 X-Rspamd-Queue-Id: BBC2E100006 X-Rspamd-Server: rspam11 X-HE-Tag: 1730245419-16177 X-HE-Meta: U2FsdGVkX1/0ZBpWCP1AE3ozN1Nkln4dr4AJqTbUtczgXmfsSW/fCcDxtS74hqualg69G6BRo9qYVY7g690GQQ7koaNNR4qj/5ScJHN5PWb5nmrDkE8nizMZdvJ8ZysgWGC7of2C7y4uE6UsYD9hBvJoT5w6zhJBjpevvOxQTAq5hFA0ffnMv0tnyX0BV/todDliJsyH9d/bbGJvsuWZ+apoTi8VyryzhnIeDRqnmI7npDo948etb15vojmoeNqn2ddlIGeRMKXG3uNQJMf+IxlrcbXHFsp38mR/QHS9YqeoqYXprdv0KLkje28AsQoxp+Vhedd7tppq2lGG+n3FkwvL/01KGw2uBal0ZYE+NN+h1T6spDJRmtmlcxx/zGEDuKIHnTDKAL2iWYuSaWe2GwIEmpi5oBSbWQ2IfSajKHyDyTpX3smxddt5TSIHrg2vesEdgREgUauY8/ztvywdlYjnegCaeR944h4eb+L4vtwTFbaTvpuZANTud+ST468RK4jNA+58hA/O6zr7o8ALloPqDBCRzjEENPyZQv+xz5GsX4dm2ctILerDc6f5NIpaPn1tKRYx/rL4pR/oqvEkW71NZMrqrBl2T8aZK1m3K+8r9DguiA4by1gC2VfAfesd6UpFAozh8teIZdqEu475MCVDulZ6Ox04f70zAn42zIAecCUdW55ZzPk9sDRFqjyD16h490iuMOD2pl6ocgyrCcGG6k3PfwgY4IlgDaJdd9nbLgV+1bpKtgDjrWVhPlBO9FA8E5hlnCpqYehJ/k4c+tTa0mUJWwUOPo2A2YZLk66JMZlYm1ptRAYne2guH+4EG/nIyde3Ha314i7j6bbKGlry+tHCmIquVtK3pAEItY+zwBzj8gx+how2IW7QWMjYugRnTo0HOqLxKVckdLNEwaGPWy+563hQHo9N1RQy7MB+EfVkMly3Jh9asSvaHM3N0j3rz/YlDVPpKxfQQvq 7/fnPHEK JDmpzX/CtujQUAiWF+Rn0zBoYssfppb23tgd/HViIGpvePaJqODAKpvo4ItVsYYK92a0LWvevqFEvTXGiXLRVVsFQQg6xNOeTUMlwD47bXwfofQe21uJS0Ndn7oXvaA0F9MTp54SMBxf+39jZG+kuLtKouTCXHBpxtHSVytGAdCr1gmh5TyUe6xD10u7gFJvHx6kN1rX2mAieJyZdfVl5Z0j/q1JzGOre8Wa6AM68qL291L9G9bARV0b53yEXQHKw+DrF6DLOKtHyxB84lfCjlt+C71V8NFNw4VYlmE4KMtrow21OQnTc/svEP6R41os2GGXsyWfdKMaCYfNlLJi2fQz67C3Jb8R3+DW6wMMHddpeCWUIhKjkCqcFbuy7gVv+dQR020fKOBdlXiU0Zu2mJ0F+TRlrxhH5uNj1ctnEOTUupsUeSHHu62nAQJ8J5ifQfRtj11GAyFUYaJph2GhB2x+THSSR5tB7PbnoRlsjpn8vh5NL8sScShrcB6OYasCw7iK78OE0RfA3YsVvHhz3H1yXnVbgHYV5MhuHoCsKeaOVd3EKy528ZNMFueXOrsIKubX97C8yaQSArZo= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Samuel Holland Some bits in the [ms]envcfg CSR, such as the CFI state and pointer masking mode, need to be controlled on a per-thread basis. Support this by keeping a copy of the CSR value in struct thread_struct and writing it during context switches. It is safe to discard the old CSR value during the context switch because the CSR is modified only by software, so the CSR will remain in sync with the copy in thread_struct. Use ALTERNATIVE directly instead of riscv_has_extension_unlikely() to minimize branchiness in the context switching code. Since thread_struct is copied during fork(), setting the value for the init task sets the default value for all other threads. Reviewed-by: Andrew Jones Reviewed-by: Deepak Gupta Signed-off-by: Samuel Holland --- arch/riscv/include/asm/processor.h | 1 + arch/riscv/include/asm/switch_to.h | 8 ++++++++ arch/riscv/kernel/cpufeature.c | 2 +- 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/arch/riscv/include/asm/processor.h b/arch/riscv/include/asm/processor.h index efa1b3519b23..c1a492508835 100644 --- a/arch/riscv/include/asm/processor.h +++ b/arch/riscv/include/asm/processor.h @@ -102,6 +102,7 @@ struct thread_struct { unsigned long s[12]; /* s[0]: frame pointer */ struct __riscv_d_ext_state fstate; unsigned long bad_cause; + unsigned long envcfg; u32 riscv_v_flags; u32 vstate_ctrl; struct __riscv_v_ext_state vstate; diff --git a/arch/riscv/include/asm/switch_to.h b/arch/riscv/include/asm/switch_to.h index 7594df37cc9f..9685cd85e57c 100644 --- a/arch/riscv/include/asm/switch_to.h +++ b/arch/riscv/include/asm/switch_to.h @@ -70,6 +70,13 @@ static __always_inline bool has_fpu(void) { return false; } #define __switch_to_fpu(__prev, __next) do { } while (0) #endif +static inline void __switch_to_envcfg(struct task_struct *next) +{ + asm volatile (ALTERNATIVE("nop", "csrw " __stringify(CSR_ENVCFG) ", %0", + 0, RISCV_ISA_EXT_XLINUXENVCFG, 1) + :: "r" (next->thread.envcfg) : "memory"); +} + extern struct task_struct *__switch_to(struct task_struct *, struct task_struct *); @@ -103,6 +110,7 @@ do { \ __switch_to_vector(__prev, __next); \ if (switch_to_should_flush_icache(__next)) \ local_flush_icache_all(); \ + __switch_to_envcfg(__next); \ ((last) = __switch_to(__prev, __next)); \ } while (0) diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c index e560a253e99b..27bafc5dd62d 100644 --- a/arch/riscv/kernel/cpufeature.c +++ b/arch/riscv/kernel/cpufeature.c @@ -923,7 +923,7 @@ unsigned long riscv_get_elf_hwcap(void) void riscv_user_isa_enable(void) { if (riscv_has_extension_unlikely(RISCV_ISA_EXT_ZICBOZ)) - csr_set(CSR_ENVCFG, ENVCFG_CBZE); + current->thread.envcfg |= ENVCFG_CBZE; else if (any_cpu_has_zicboz) pr_warn_once("Zicboz disabled as it is unavailable on some harts\n"); } From patchwork Tue Oct 29 23:44: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: 13855721 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 1900AD74952 for ; Tue, 29 Oct 2024 23:44:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A09BD6B009A; Tue, 29 Oct 2024 19:44:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9B9E06B009B; Tue, 29 Oct 2024 19:44:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7E8C66B009C; Tue, 29 Oct 2024 19:44:32 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 58F846B009A for ; Tue, 29 Oct 2024 19:44:32 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id C8755120793 for ; Tue, 29 Oct 2024 23:44:31 +0000 (UTC) X-FDA: 82728271332.29.11E2FF1 Received: from mail-pf1-f181.google.com (mail-pf1-f181.google.com [209.85.210.181]) by imf16.hostedemail.com (Postfix) with ESMTP id 1252318000B for ; Tue, 29 Oct 2024 23:44:04 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=WZUu58ev; dmarc=none; spf=pass (imf16.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.181 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730245415; a=rsa-sha256; cv=none; b=MP0GTnlKTPNSZO2cRD2veXjfiSnO2+hi7/hPXF6db2Ci4FAsf6h7P1OyUKqkNxqf0S8pDa zfFFJNQ6oE/egfo8YylbFDd5d1iY/sgDdvQnPzvOJHIXvIE9aRlTAml1OPnZpN4H3DTiJp Pjg0Vhm2qSgVK1bGhaP4HC4TSszYyxU= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=WZUu58ev; dmarc=none; spf=pass (imf16.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.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=1730245415; 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=lBYlqLcHDdUtNQoLJCRBG8R/P8wZP2gIPf6gwWr372U=; b=2FNzZyLHyxwfi1MqXpEF2Grk02nxXCoEbVzoJEE7Jhb/9AIRKCLTIE0JYtVsFINxkjamq/ 4tt4FvznuWmBVHUIiLbWLGsMOy2IWVvB1kpbIIycym1ybM+QpFNowF5BbpXqt5Y5AJ6SJX iI4vLYw/ZgDkj0e3WYYcdiPWh6Fj68c= Received: by mail-pf1-f181.google.com with SMTP id d2e1a72fcca58-71e3fce4a60so4357925b3a.0 for ; Tue, 29 Oct 2024 16:44:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1730245469; x=1730850269; 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=lBYlqLcHDdUtNQoLJCRBG8R/P8wZP2gIPf6gwWr372U=; b=WZUu58evQVuiQ5oOGcfu70vUDMQKaFnsuqZVLGiSzTqH3MaOoMC4bwjwJLvlnPET9v Pcz2LyRgB4sdQQmgeJG1wdvBZPSViOo1d/QTMdeIzRDk8PANq+fQ+FpCwfYo2c7X2m4P Z/Kpc9085ZimdH8KrcpxbTBpx8ArfnETpKwHyTigjo2bDk0FtfnHbGu6s43ST3J5HA4D i2LaSaurJf3dC/7WuG8/zdZJXfy8aBsij0WGM7eingUGcbRUWrQOECfhdfNeyVTWVYjh oapeBAFrW/79ogthLu/5c8DpmTphzIfiB9Tcs4z2+vzJLGoiUdTs3OKfkwvJ28NAfGhJ 0WhQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730245469; x=1730850269; 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=lBYlqLcHDdUtNQoLJCRBG8R/P8wZP2gIPf6gwWr372U=; b=MUfDBpdAowD0Hb4/3spu/QsO7ww1h/LmPpurS+8B6PzngOOUQDtPbdPZcvF9omTPMu zWU89x3kR/rOVHgrmdx0Ca4fYi7vO6WrhbCwJyTpawAupEyNG/cQ4aNSGPNztzvujaZ1 giWNxxgSkB1AUkuaeDtlcngKRt6anbwqf2iUO2WEnrDxal1/r0SLvdHRuqPKiSl4cDqy za2QHoWysms2iWRan4Njum0di9fP8r2echDrg88X4fg7pLn3uh2aOEoG5OYA8NObN4SM RlZU2v5AZi0tn1Uzf1vXtUMTpoUKNWERJpB8o8+WCVzW5LubYmRIJyzK7FJ8GkwGr778 mMxA== X-Forwarded-Encrypted: i=1; AJvYcCXrLFNe5Cq0tbZkrv2gYJXhYU0UMPze3byxTcZnGt2XLjljE7AhpVozIvXwpaye0hWlRpeT+dEpag==@kvack.org X-Gm-Message-State: AOJu0YwE/6CDmkxLgAxenWxFIublydeOHMrklaAMF/LGa0h2vnaDtyU8 Gmt8nQzuQaSdGdxWNiKiF4FtQAZPR3E/YhYGZnTbbH1ZCQ1AL+gCqUfkV4atS1A= X-Google-Smtp-Source: AGHT+IELZ2VSZVfVna2i/qkCG/IWw0r7P7GSQ8cnF6JfcdOHDu45D3ZJHWKDe58LG2WiYKkWATRH8g== X-Received: by 2002:a05:6a00:10c7:b0:717:9154:b5d6 with SMTP id d2e1a72fcca58-72063028d41mr20002790b3a.22.1730245468803; Tue, 29 Oct 2024 16:44:28 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72057921863sm8157643b3a.33.2024.10.29.16.44.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Oct 2024 16:44:28 -0700 (PDT) From: Deepak Gupta Date: Tue, 29 Oct 2024 16:44:05 -0700 Subject: [PATCH v7 05/32] riscv: Call riscv_user_isa_enable() only on the boot hart MIME-Version: 1.0 Message-Id: <20241029-v5_user_cfi_series-v7-5-2727ce9936cb@rivosinc.com> References: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@rivosinc.com> In-Reply-To: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@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, Samuel Holland , Andrew Jones , Conor Dooley , Deepak Gupta X-Mailer: b4 0.14.0 X-Rspam-User: X-Rspamd-Queue-Id: 1252318000B X-Rspamd-Server: rspam01 X-Stat-Signature: zwb914rmqwr9sphf5e8b6ocztbxei3zj X-HE-Tag: 1730245444-695110 X-HE-Meta: U2FsdGVkX1+y7WkfEOkNv1U2HR8j+KFUZs5Bua+jRtJ89FXaSdxYbaFqKq+uiks8tGcg8SXSm/EFvbT2QcxDRmWz7I7A0jbBiYZxModiFaX2j33rnhBGl3VA/g6ZBLr+7dFx1hLUcGij913jKRBHfKkZaP12CCDc/viYrl/iBiIgD4Y70+p95xmWuwobmGpbtMUU/QwHQiVU9ZwQBpy86z+7kgtF4VwxxtoxgJyLLJ1gQWS7c/QmCF6cMBn5tgjC1pKrLAKkgM9sYwA9V/DFjyzj6xY8YCPt6lkGelP0hqL+KUvQgNNigkK7gxxLwExm80x16h4Pira7OcAC6kQ+aGmeIZJgF59JCR6Kl1xl0kzSTENRumgHk+pdA1lPfE75vqXnpOaQs93FlUMyRjEpEte6S0GLO+bebo5KCt6pR9zIvtTm5nU0sJToQxvfluHX3br/2B9Fw6kz/ekgyxsZj57HWqhPATXLxNiOci2YUFoe+VfgBAK7nOQrc9ONkAJDbhauWCKihCfqg/zVKE0RJan39ETRoNz6BFHwc0O1CBEk6hRvdhpW2JKf2Gm+UQ76NZK4r45qX9XtDBSf/qfdBAKwutgzx78tjhw08K/e6hSOQOneuCjxJi8E9dmXaQeh+l11NLi+skYNsHB++CjR1ErmdvLImUXHEi/qlnzQjwFIt6pCL3rX0sYPepCykvONYNBM9D8XiKelrt/tYedIhzQtQYAPkDE4kz3Jm4TpJRbKgVmePZ9e0pZDjYE1IVz11PfHzNuw3x0/lmbyotPf6d9bUBf2NlLkm8qlFrEwhp4Hrf0Y4KcjoF2M1GDFsx58ZdjNW+Wqu8i8hUmukxUq16NU8EBkNqca4tJeRa0CrsVtAwucZp2FLv+fHu0uHVzOLoWxVxlGQ3MW6q1GKVVNDnzHJDIMclbzrMXaKGgWdIh9plTH0Mzejir7DT4av74nCuyk8zz8cvSqzmImpz/ P5Y1VDXT qjKMEvOuFVvCI4oWx6eaLeYKrIhm+BZ/BGvPzMt9EUGFZ88uf8S/1lUvLDgO4fAMqyL0SMIOBB/Oa911nIAw+siB0mm/C9Et7IP0jqo2yhXEIgLA3MI0aw3pXZNLIteTY5wKhx1AqBcPG23DlmXcyANs7SUQQYHK5LJfiraUZqKUvm+CEfIq52OfyuCXwLUm1eP5htP38UVNLHMSFslcWKcqL5QYtyjXMl60eT9vPBYBL0yMle+LRpzqcHiweUNsDhmwJHUOjflF6LQvPPebecn4zNPOvystsLObr14Gdx4wRGEGsbt+jygSuaYgbs2PqHJRQi8wBP98aqkCFNeJBMKB/u6FFah916qxV7GuaM8Jy+7jNbH/iKhDCl0EwCZ6H/IAkZ1qzItjtM0kr+EssA16A8Z/O0kfv49d2aEInW+1Xg/bMkAcLVX7csDijPGEB15P8/6uosK8DU7mh7Nv5sVLUiJpVbnzSTWSuXsxg0/nrB7sI/lgJs5/uYAlWnXgF+caytULj9spkOhh6va1Z8BEyOmjEZoEHVzq/EBBBzGpXEo9vJYPSBJKc9+NFKDJPSonRC5yZfad9qp8CL60i0njstj8I37n3yWVs9VbvpV3Kty41AteWRsfiGy7U6Soj2gOMsjb93dm1VKr/1x3Qv29catoSpCyClnCBYU+fQnKeS5WPWbGkrgicQA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Samuel Holland Now that the [ms]envcfg CSR value is maintained per thread, not per hart, riscv_user_isa_enable() only needs to be called once during boot, to set the value for the init task. This also allows it to be marked as __init. Reviewed-by: Andrew Jones Reviewed-by: Conor Dooley Reviewed-by: Deepak Gupta Signed-off-by: Samuel Holland --- arch/riscv/include/asm/cpufeature.h | 2 +- arch/riscv/kernel/cpufeature.c | 4 ++-- arch/riscv/kernel/smpboot.c | 2 -- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/arch/riscv/include/asm/cpufeature.h b/arch/riscv/include/asm/cpufeature.h index 45f9c1171a48..ce9a995730c1 100644 --- a/arch/riscv/include/asm/cpufeature.h +++ b/arch/riscv/include/asm/cpufeature.h @@ -31,7 +31,7 @@ DECLARE_PER_CPU(struct riscv_cpuinfo, riscv_cpuinfo); /* Per-cpu ISA extensions. */ extern struct riscv_isainfo hart_isa[NR_CPUS]; -void riscv_user_isa_enable(void); +void __init riscv_user_isa_enable(void); #define _RISCV_ISA_EXT_DATA(_name, _id, _subset_exts, _subset_exts_size, _validate) { \ .name = #_name, \ diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c index 27bafc5dd62d..b3a057c36996 100644 --- a/arch/riscv/kernel/cpufeature.c +++ b/arch/riscv/kernel/cpufeature.c @@ -920,12 +920,12 @@ unsigned long riscv_get_elf_hwcap(void) return hwcap; } -void riscv_user_isa_enable(void) +void __init riscv_user_isa_enable(void) { if (riscv_has_extension_unlikely(RISCV_ISA_EXT_ZICBOZ)) current->thread.envcfg |= ENVCFG_CBZE; else if (any_cpu_has_zicboz) - pr_warn_once("Zicboz disabled as it is unavailable on some harts\n"); + pr_warn("Zicboz disabled as it is unavailable on some harts\n"); } #ifdef CONFIG_RISCV_ALTERNATIVE diff --git a/arch/riscv/kernel/smpboot.c b/arch/riscv/kernel/smpboot.c index 0f8f1c95ac38..e36d20205bd7 100644 --- a/arch/riscv/kernel/smpboot.c +++ b/arch/riscv/kernel/smpboot.c @@ -233,8 +233,6 @@ asmlinkage __visible void smp_callin(void) numa_add_cpu(curr_cpuid); set_cpu_online(curr_cpuid, true); - riscv_user_isa_enable(); - /* * Remote cache and TLB flushes are ignored while the CPU is offline, * so flush them both right now just in case. From patchwork Tue Oct 29 23:44: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: 13855722 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 D6B12D74950 for ; Tue, 29 Oct 2024 23:44:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0235A6B009C; Tue, 29 Oct 2024 19:44:35 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F133D6B009D; Tue, 29 Oct 2024 19:44:34 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D8C136B009E; Tue, 29 Oct 2024 19:44:34 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id B66D86B009C for ; Tue, 29 Oct 2024 19:44:34 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 5C09112076A for ; Tue, 29 Oct 2024 23:44:34 +0000 (UTC) X-FDA: 82728271080.15.A6C5BCF Received: from mail-pg1-f180.google.com (mail-pg1-f180.google.com [209.85.215.180]) by imf13.hostedemail.com (Postfix) with ESMTP id A1A4C20006 for ; Tue, 29 Oct 2024 23:44:06 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=mKAoNavU; spf=pass (imf13.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.180 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730245312; 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=UqlGzJEOA0rIxMbra4aAEQkUCqIib9RSzM8TmOHOkBA=; b=vvVmdHm5yw5PqBAtsjJbsCcfTzpIXnYKLKhVr0CtUWkMFCgC4i6TQ7tlOz7KN7L1P3kDRr 1JZdnDm/1xMonUJtqZ3CcSWNjl+TY/0mrAs7ZfzE6I8tx/eW1jGHpaYn03xrzQoFvH47tE knkmFQSK+zhXcb0Vw0t3PK3EQnr5t1w= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730245312; a=rsa-sha256; cv=none; b=3CDcoDj/QpWqUdvqBR2rE+eDDEnxJJdGWrl6wBZ+EEX1JKGZjAG8wBOCITdmQTXYrlrmu/ aFsTOnb1keoMU82Ak0Od/yWaKzPcggP6XkgDRMCa3i/6Sisof4x0EPuZyBukHTTFHh8Fy2 XfH1zGJ+38SszoUMrrfPduN3KeVNX2A= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=mKAoNavU; spf=pass (imf13.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.180 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pg1-f180.google.com with SMTP id 41be03b00d2f7-7edb6879196so4004378a12.3 for ; Tue, 29 Oct 2024 16:44:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1730245471; x=1730850271; 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=UqlGzJEOA0rIxMbra4aAEQkUCqIib9RSzM8TmOHOkBA=; b=mKAoNavU9K0kyNWG+vfsxyZy5jYsFSAxxgOgugTASe7BD55+crKs9x9QIg6X1jfsLV zK7MpA8IeXa5oZX1810gk06Dau9auvTzhwa1e7LXhKxnWEnBwYCZLBJ70ZMtbvh2YrZJ PVErgaWfKK/0ssmKUDFsu6rC7yjra/Zc5UyW8Ld4ZkF/rx17pLm2qyj6BzRn1nA1aaKZ SFJCK12SPFqG7ZkEPh1+RNoCEyT0UF9Irl6ETo3/0p1SUcMcpKPpiLr8mcffluzorSzr lFSLtXeuRuWmVBLg0D+whVLuz/M1zjI2NWHI+AAkINt3Ru5Q38EJTvOPGh3DclV2w8c9 cLJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730245471; x=1730850271; 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=UqlGzJEOA0rIxMbra4aAEQkUCqIib9RSzM8TmOHOkBA=; b=A6PVgq6kY/zdd5A0T/+O8NxEc+IhwVVF7m/4mHogGdFqUf6jcXl73ZsgKCPfFo6kbV dA9bsbB8Auds9nVC0DI87peHvKKXgufFzZ6GAhgMZFgth4+prlzzx451ZgTlXNzofgxl wzVOcjyFekkAZdrtEPFbhq01fj6NOpBXInuDUlkTt+mA+EeFq0eV93j/EJ3gbEhCgrB/ zh2X6WPS711lMFWT8oKrCzRJ9hRYvPc6XD9r3ydhOz/XJhdpjqfNyGZ64tjD4EO+lrge VIRbyb3KTv0tr1EqWJ6gfKpOoM9s2FuKVwcAO1sMy47TvyS5nJxXgnoBIIynIkHjaIhw LX6w== X-Forwarded-Encrypted: i=1; AJvYcCXfu2YTcvkxFyZZhyCA1H4QjY85VOQWaDEq3MkqYLfgRoYU2uWEKJqnom/K4Q1u4dZsaDuZUypcAQ==@kvack.org X-Gm-Message-State: AOJu0Yz40uT+ePpB++vL10XWalgVttuH4rdggZHCuaoJF/uggV2LYLNK 3ptHeiXJdavR1Riw2N8hBJ+874TrdrwyU+C1k2TFoUcXmRFh7RFUyKfHL0PH8D0= X-Google-Smtp-Source: AGHT+IHPZtS5V83Y7JMjzODTd/hxnTyAzMxstiwXVpmrwh9UhJDy1/DHcrx/nIgBTJ/vKGmAOmeiGw== X-Received: by 2002:a05:6a21:4d8b:b0:1d9:b78:2dd3 with SMTP id adf61e73a8af0-1d9a840aaf4mr18555145637.26.1730245471399; Tue, 29 Oct 2024 16:44:31 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72057921863sm8157643b3a.33.2024.10.29.16.44.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Oct 2024 16:44:30 -0700 (PDT) From: Deepak Gupta Date: Tue, 29 Oct 2024 16:44:06 -0700 Subject: [PATCH v7 06/32] dt-bindings: riscv: zicfilp and zicfiss in dt-bindings (extensions.yaml) MIME-Version: 1.0 Message-Id: <20241029-v5_user_cfi_series-v7-6-2727ce9936cb@rivosinc.com> References: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@rivosinc.com> In-Reply-To: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@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: uaq7kj9ziz157cpug9kmgrbqwfmd7w9i X-Rspamd-Queue-Id: A1A4C20006 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1730245446-296297 X-HE-Meta: U2FsdGVkX195VFjYromlAtDndGNzmezV9XRHgxze5fxoyUpzbFViyu7Fj/msgCGRgzhZiQ9aMjj0Zl46wahullHmAaJxypVNWC0ot61nJcVTZSKZCHWfhqUoz3qCGH4PPpiC7FKJUBRNLY7oZoEGSFyRa+kWKm85+cvIo28ctQSlo32jDPJ3/bj2bK320hXFQMoj0v+ls55X4Bnd1wAjN5jo61vIHtnDb6qvyJYRWJ1dt9929jRmr/De8RlFf3VM6fj7sO13IIjVFiIGrVKV6ffKF3119FJjxerQIRWOSvfedJVbfMbwCkE1BUcd1UgtvgmdhGjJcwQhQn2dBkjuD8Q7f2QcbaTLZf0VaHDa6ttnRc7RViRpR0xs14ZsYB32cnarhwldei5TlN1qTFyyabFSaZ2U2NBer8N3iIAf/6zj0c5D/QYnPrDNaJLMxIa2j7L6uwNi5/6SI+NRmUDl96W7VEF691IPfEjWJpFOr8L8q/pdjZ3hcK1XcHVBuM9EZH78BbHopAmxYwJ0hJ/BK4I+i/afMePmefC+/0+9JIrlrnj6N8UAa/6XxPxp6Z1uGT5R5hih5hnAwAx6j9yda6jTmf2o2KhKlz2Xst4cXEgh5RvMVIasLE0G88VMl4qgjSypzFpxKTI6pzKSA91WGzqP4O/bpZNzTdC5bJjHIEAhILoERM+rhZvjL2Sffph5Yn4Jv+PIK9M7Ql9hUJlnOdw9AzHmqSyol7kVB8D1E9SEtaUyYOveRvU2dsFt1UlYickvM47oetNxl12Dv/oup7PZ9nEbfaHFnV+kJPE3awCt7HBQqwi670ftP4ibGWKZS3QXPY6C9OA7LMG2ZFnmIewBP2h7XqYQicq3BylAOshUwMdBsXjE19GzZlntQf2jIoyXBszWOSLa+8CN3CZxTI2aNbI1PUXrypWnE12RS5MyC0NHpp6njBtowxfKHNS69hWvgP6rXW2YJvX9ue2 sGAo42fy IAberpkQ+G17cyWKk7phB3BTSafeHWOqHqXsZ9fbioUlx43HN4r8sQhnGzVHFi6zgNGSktmjuuBQ1Lc28AU894dNZGxL/rUjtFN+1wWFpuBkjgzFeluiT86QZf9+3mKEFfCceEWKQVJmrzlLIPwhr9nH4ClC/e8X1oZsQNbjquvrW49hllpx57rZBkLG43JYtzXX5pxXL2HSr/XYlmIHVPY/jQO1DYcKHpmXbsQIu3jFxpoikl60PF9KGjKdcRui8RrODQ33fbgLEyzrf/uh5UJj7xVrt10BCt1x/qCfwIVy2gPZLFyjqiZNs6qh4R78alniCEcADasYZt6tNlQQ6U6SdVWDvb6YGiL5s8QEwSsN6FLho6bCcjz3TlfhPDC3SThyr1sNyujiXJnplovUtwj836lgs+cIkMDKjGFE1pLAa1DWLaWfrsQPCyxJVl2k6M7LP/D6nO2lGENNqf1wP4x0mp/CTJoc7jU0kCw+ajug9AsI= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Make an entry for cfi extensions in extensions.yaml. Signed-off-by: Deepak Gupta Acked-by: Rob Herring (Arm) --- Documentation/devicetree/bindings/riscv/extensions.yaml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Documentation/devicetree/bindings/riscv/extensions.yaml b/Documentation/devicetree/bindings/riscv/extensions.yaml index 2cf2026cff57..356c60fd6cc8 100644 --- a/Documentation/devicetree/bindings/riscv/extensions.yaml +++ b/Documentation/devicetree/bindings/riscv/extensions.yaml @@ -368,6 +368,20 @@ properties: The standard Zicboz extension for cache-block zeroing as ratified in commit 3dd606f ("Create cmobase-v1.0.pdf") of riscv-CMOs. + - const: zicfilp + description: | + The standard Zicfilp extension for enforcing forward edge + control-flow integrity as ratified in commit 3f8e450 ("merge + pull request #227 from ved-rivos/0709") of riscv-cfi + github repo. + + - const: zicfiss + description: | + The standard Zicfiss extension for enforcing backward edge + control-flow integrity as ratified in commit 3f8e450 ("merge + pull request #227 from ved-rivos/0709") of riscv-cfi + github repo. + - const: zicntr description: The standard Zicntr extension for base counters and timers, as From patchwork Tue Oct 29 23:44: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: 13855723 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 7C2A5D7494F for ; Tue, 29 Oct 2024 23:44:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 995A66B009E; Tue, 29 Oct 2024 19:44:37 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 945736B009F; Tue, 29 Oct 2024 19:44:37 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7BF2F6B00A0; Tue, 29 Oct 2024 19:44:37 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 5AD5B6B009E for ; Tue, 29 Oct 2024 19:44:37 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 1B8801607A9 for ; Tue, 29 Oct 2024 23:44:37 +0000 (UTC) X-FDA: 82728270828.25.A5249EF Received: from mail-pf1-f172.google.com (mail-pf1-f172.google.com [209.85.210.172]) by imf14.hostedemail.com (Postfix) with ESMTP id 2A3BD100004 for ; Tue, 29 Oct 2024 23:44:06 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=znTUPQDR; dmarc=none; spf=pass (imf14.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.172 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730245345; a=rsa-sha256; cv=none; b=2iHxd4tTssSl/cY6RGnabMgj0a929VybWCQuakgNbYZOdLsrC5xy0vCTCmiz/72ZMyP+yP CkRXhEGAi4CfZcwOYKLMAyAc1PmCxP09zjJzsQPFZyvFzPaD81PVIAZvHzj1gzzB4Y+gI+ EtMEFCefh7vzHl7my0aVLwdJXCoDqyc= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=znTUPQDR; dmarc=none; spf=pass (imf14.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.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=1730245345; 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=vrN7VRJjMxZnUtDJzv737m358kNyMhJhWFYpC3gmmrw=; b=LISvTkHG+lRiN2oZg+Ydquq44VTA6EbgsIuOgJEU06OzPudV2v/jjuQwfwc4UgjFJgmMrm 2pW4iGtNR0xzIuGrRVZd8atOJyJjUT5T/vnCgtYIqLGehXNOPC2ZRO4rJbPUjzfB+6mZ1V CIxE32j98n3S1Z4CE4EZRJDiaaegmPg= Received: by mail-pf1-f172.google.com with SMTP id d2e1a72fcca58-7206304f93aso3261773b3a.0 for ; Tue, 29 Oct 2024 16:44:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1730245474; x=1730850274; 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=vrN7VRJjMxZnUtDJzv737m358kNyMhJhWFYpC3gmmrw=; b=znTUPQDRieblsSO56JEBWt6YH39sBVpEKzcuE2vscz6DQ4xlaTmJjXyGx7TAIEngy9 3hoyxmIedD8ptAMqtffLYXErE96FTbbgFXWFd3JYdURomvk16TYcNfbKaUtVw8KiuE9S as84dbXdwGMVZ7EYYN9yfIK0+BOrWzOIJyKDFaEXt5XUKhMv8DuwUx73JgrvIZoCSZDR Em8w2xNpbOhkJV6zMX026SVZkeZMnMn5k3mN0bO717AjF92PcO4eNAhpDNy+6ECqwyh7 XIQfEKZguhjcfeKkY+JTLWMM1uIyIVTF38vY9Xng+R7DvexivNyKo9cI6PxBiiqKfhSo GOxw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730245474; x=1730850274; 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=vrN7VRJjMxZnUtDJzv737m358kNyMhJhWFYpC3gmmrw=; b=KISnREbY1Md7h94+9q+NaL//Ejvy98tyAF3lCkA9/WEWc4EsW3qa+0ROPB5bmJzBio UQYt7xJYdk5P4gUkh46zKBONjwkDBPa/yqnnUbmZ3ayRkSQ0XxeXG1UQCjUEtHovdSZ5 6mh0IBA2DEMRoDr/FQeYNJ/9Bf5UbT1P6YpUXVgYkcoZX1iwKzRF0aJWCAHVQKdmOndK 3LDZRHoo2PP4TeTpVPJtlC7zQFcu7tRFOcRKd+uT2Kkcn3XyUNL4wQB0ATFpiZ5mmlkL Ii2YVU3c0pF7n0LApEAf5cyFMdQWZQag/kiZ1sbGC16mNm8cAjbty/PWWAPu0cda17vh sbPA== X-Forwarded-Encrypted: i=1; AJvYcCWnb3/uzojb7O6FkSz6tRfzYgiY4QF3O38XzF4NwgM37zHDAqZkOnr8ZWdq5AWrfdFXlE7VgQA0ew==@kvack.org X-Gm-Message-State: AOJu0YyiJHtrYRUYdLLRr+A2YMyPce4co/KpqzJofaKHlQ7DNcp7StdM ZvvjzlD7C3KXGwEGy5q2hjlg0beUhdfMiticq62qK85gnRIZOLbtWme0UodCJmc= X-Google-Smtp-Source: AGHT+IGbttT3C4P2rGsS0S9777Dzd9ptF+wePIA2fozj8noiXmSjA3kv6jrWQfh6v4MsVFem5sX1tw== X-Received: by 2002:a05:6a21:e92:b0:1d9:3955:6e6a with SMTP id adf61e73a8af0-1d9a8408a30mr18363490637.22.1730245473986; Tue, 29 Oct 2024 16:44:33 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72057921863sm8157643b3a.33.2024.10.29.16.44.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Oct 2024 16:44:33 -0700 (PDT) From: Deepak Gupta Date: Tue, 29 Oct 2024 16:44:07 -0700 Subject: [PATCH v7 07/32] riscv: zicfiss / zicfilp enumeration MIME-Version: 1.0 Message-Id: <20241029-v5_user_cfi_series-v7-7-2727ce9936cb@rivosinc.com> References: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@rivosinc.com> In-Reply-To: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@rivosinc.com> To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Paul Walmsley , Palmer Dabbelt , Albert Ou , Conor Dooley , Rob Herring , Krzysztof Kozlowski , Arnd Bergmann , Christian Brauner , Peter Zijlstra , Oleg Nesterov , Eric Biederman , Kees Cook , Jonathan Corbet , Shuah Khan Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, alistair.francis@wdc.com, richard.henderson@linaro.org, jim.shu@sifive.com, andybnac@gmail.com, kito.cheng@sifive.com, charlie@rivosinc.com, atishp@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, alexghiti@rivosinc.com, samitolvanen@google.com, broonie@kernel.org, rick.p.edgecombe@intel.com, Deepak Gupta X-Mailer: b4 0.14.0 X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 2A3BD100004 X-Stat-Signature: utekbrzchnxsa4xx1sewqqekxbhoig4f X-Rspam-User: X-HE-Tag: 1730245446-953909 X-HE-Meta: U2FsdGVkX1/Gx6XNpg7rVZKZI4NpxTUPTiP9IQgmRPGXnxXkhyN1HAtaYDPSNJUyry5qIv1EFdDec/YybTl+tG1yzDul+nc2uMjUf40W98bZTmJ7kd+uDjRwx0xE+COhUOEyElEfqBk+++e1SWHIcbCNqrPbQadH/9lf9l797eBQZFnT7UPcz33s1i83KgEgTARGmhYwz0p0wmdFwjeuaJq1R9XV5OrRjGyKDshsojAkOtQBmUC++3on7JVojsxKbbqslCvC2cheMYWCoHy6KanFUaDXUFjyqErmRnq+giHaXOqcgF5MiYjkalKF16dVAMCk5w61G+2qTdhgBxxnhV0BDZ6oxeKR9rjRHniIbWNNWPiDwACrKZsP3Js2Bbt1RrK2QwryfOhjEsHcMeO0G/WYuyaM6P04/qn5+rqXlKCXACYr+6Hq5C4IPXe1ym1AC4+d6pQpO+tSbRpvZMqMNwah+iIStIBCGzc7/ozAaIrZ7tOsAGOAsqimVPPLknzKMFA9XoZPsDhuvV2lS6+L8q9FCXbp4mwjEW7/nCvC8NhurwjTfHCTIBF23ialjYEm7TdrQ40dpnXE0iPJuMiiJsrtyFuYSm1pi2fVtnp44eB7pYArBf5NDnd7X0Dgq9G2IrxmJGAPCE0PFg5Xq1w/y1EITpaJAXXnB9vFGYtIuTqiACIJnfwm7WIt6tUDcuEuYAqZlR4510fPcjCIgMFaPIvRlS9ZbaikQVwGnik1bJ/wSdO9Ygh3mpogQcrWDEUI067EjFJ8QZ6qcTHra29G3oOFzI+xNPpI4zLFpWYJO5HzircsFnt4zB+GduKuQYQbG+cXm6THXe+CYckJLK7+CxtrTOMomPR7r7TO83OCjVX87N+OY/JW0xgdTt7OiF95d/zmPH+Kj5+Wffw4Zz3eqO0OP3JEoCj2JvbDrm3a5Tq7QSoD6RHaLIDREhL5x7XJMow+Uxa9TexfgKHgmJL 2oaqQ6yy 1FKB4bFg0+AbnuiZhZ5RRBMHf5Y+gWXF6QcmAxzR+iZA27eQZNEMvWSBqAs9e2q4c5NNtNUFAr2+MDY/c2mBRcAkW/AtESxQ921jdWHoHpndC5YkyJ7QCQno9effPV1QMC7r+L9bp2AgM0g1TA9cWgUbLsIY26ESnMtQL4D7GbP0/flP4jrO5nZ7jPiPYickc2R1tG9YcIS8ZKRFRz90jipoI4yFY+0UYZLYGUd7/IjmIdNs1cn7M/C/qvhQTcvzrZMaSOpN+mzKbrgxyQG6eGnv/rrtoJCA1ajZMz0Hlc3+CqDIC30kytrdRUAfUznjllfLofk4JlW46NXxm5e3cGOStcGc+pTzGN6vjzdcGxc+A+4/pYgVIa1FdmdKzPAZnYx1e27UabzAYSMp0czzV9YXpmewylvX/ONjp9v1z3KVT3bKkQXo6NDamZQvLmBvl8PX3RfnY+pdth8bfoXgI5X44kvlyuY5BOrQ6tO2go23Bh9aOlUJiBAsEWQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: This patch adds support for detecting zicfiss and zicfilp. zicfiss and zicfilp stands for unprivleged integer spec extension for shadow stack and branch tracking on indirect branches, respectively. This patch looks for zicfiss and zicfilp in device tree and accordinlgy lights up bit in cpu feature bitmap. Furthermore this patch adds detection utility functions to return whether shadow stack or landing pads are supported by cpu. Signed-off-by: Deepak Gupta --- arch/riscv/include/asm/cpufeature.h | 13 +++++++++++++ arch/riscv/include/asm/hwcap.h | 2 ++ arch/riscv/include/asm/processor.h | 1 + arch/riscv/kernel/cpufeature.c | 2 ++ 4 files changed, 18 insertions(+) diff --git a/arch/riscv/include/asm/cpufeature.h b/arch/riscv/include/asm/cpufeature.h index ce9a995730c1..344b8e8cd3e8 100644 --- a/arch/riscv/include/asm/cpufeature.h +++ b/arch/riscv/include/asm/cpufeature.h @@ -8,6 +8,7 @@ #include #include +#include #include #include #include @@ -180,4 +181,16 @@ static __always_inline bool riscv_cpu_has_extension_unlikely(int cpu, const unsi return __riscv_isa_extension_available(hart_isa[cpu].isa, ext); } +static inline bool cpu_supports_shadow_stack(void) +{ + return (IS_ENABLED(CONFIG_RISCV_USER_CFI) && + riscv_cpu_has_extension_unlikely(smp_processor_id(), RISCV_ISA_EXT_ZICFISS)); +} + +static inline bool cpu_supports_indirect_br_lp_instr(void) +{ + return (IS_ENABLED(CONFIG_RISCV_USER_CFI) && + riscv_cpu_has_extension_unlikely(smp_processor_id(), RISCV_ISA_EXT_ZICFILP)); +} + #endif diff --git a/arch/riscv/include/asm/hwcap.h b/arch/riscv/include/asm/hwcap.h index 46d9de54179e..10d315a6ef0e 100644 --- a/arch/riscv/include/asm/hwcap.h +++ b/arch/riscv/include/asm/hwcap.h @@ -93,6 +93,8 @@ #define RISCV_ISA_EXT_ZCMOP 84 #define RISCV_ISA_EXT_ZAWRS 85 #define RISCV_ISA_EXT_SVVPTC 86 +#define RISCV_ISA_EXT_ZICFILP 87 +#define RISCV_ISA_EXT_ZICFISS 88 #define RISCV_ISA_EXT_XLINUXENVCFG 127 diff --git a/arch/riscv/include/asm/processor.h b/arch/riscv/include/asm/processor.h index c1a492508835..aec3466a389c 100644 --- a/arch/riscv/include/asm/processor.h +++ b/arch/riscv/include/asm/processor.h @@ -13,6 +13,7 @@ #include #include +#include #define arch_get_mmap_end(addr, len, flags) \ ({ \ diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c index b3a057c36996..70803aa66332 100644 --- a/arch/riscv/kernel/cpufeature.c +++ b/arch/riscv/kernel/cpufeature.c @@ -317,6 +317,8 @@ const struct riscv_isa_ext_data riscv_isa_ext[] = { riscv_ext_zicbom_validate), __RISCV_ISA_EXT_SUPERSET_VALIDATE(zicboz, RISCV_ISA_EXT_ZICBOZ, riscv_xlinuxenvcfg_exts, riscv_ext_zicboz_validate), + __RISCV_ISA_EXT_SUPERSET(zicfilp, RISCV_ISA_EXT_ZICFILP, riscv_xlinuxenvcfg_exts), + __RISCV_ISA_EXT_SUPERSET(zicfiss, RISCV_ISA_EXT_ZICFISS, riscv_xlinuxenvcfg_exts), __RISCV_ISA_EXT_DATA(zicntr, RISCV_ISA_EXT_ZICNTR), __RISCV_ISA_EXT_DATA(zicond, RISCV_ISA_EXT_ZICOND), __RISCV_ISA_EXT_DATA(zicsr, RISCV_ISA_EXT_ZICSR), From patchwork Tue Oct 29 23:44:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13855724 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 642DCD7494D for ; Tue, 29 Oct 2024 23:44:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 21E4C6B00A0; Tue, 29 Oct 2024 19:44:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1C9246B00A1; Tue, 29 Oct 2024 19:44:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 06C256B00A2; Tue, 29 Oct 2024 19:44:40 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id DAB466B00A0 for ; Tue, 29 Oct 2024 19:44:39 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id A48EA1C5DCA for ; Tue, 29 Oct 2024 23:44:39 +0000 (UTC) X-FDA: 82728270786.19.93E85A7 Received: from mail-pg1-f174.google.com (mail-pg1-f174.google.com [209.85.215.174]) by imf02.hostedemail.com (Postfix) with ESMTP id 21B4B80009 for ; Tue, 29 Oct 2024 23:43:49 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=SfLSppfw; dmarc=none; spf=pass (imf02.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=1730245434; a=rsa-sha256; cv=none; b=ynZ8EDt2CmtNiAZVzPsHTEggUt8Oa3LT0TmOUhgvenKNljKsHFja3Vlq/8d2jZWwTR4TZE kdIxdZT+7YvAzjA9ZqlEoBiHRagtxaPopVfLTJsuFWyHKWmELDQOxtcPCCaRgvBW3WgGH9 Sr5542O+6xqkXeyeoD1sLMnJSFjG8IE= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=SfLSppfw; dmarc=none; spf=pass (imf02.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=1730245434; 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=Vc3xum6aGJsQxt7BwGXU6tv/D45yqqkNrU/wViua7no=; b=pJ2mqJWoBakUTKaEwDJEdWDZqoQyNI9MLsQxTg0fCfzM2mpy0BSskeLZa8wEAJWY8bvMVi yXwp2N0NOzMugxW9dWGid3FNI40vk9NzEWqkJoQ1vrK0hR/i57ylf3fT8+qLzlW9+Aupss i5vQ4ROPksz221ThQ1OQyvBv/00ns9c= Received: by mail-pg1-f174.google.com with SMTP id 41be03b00d2f7-7ea76a12c32so4653380a12.1 for ; Tue, 29 Oct 2024 16:44:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1730245476; x=1730850276; 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=Vc3xum6aGJsQxt7BwGXU6tv/D45yqqkNrU/wViua7no=; b=SfLSppfwzh1DtIROgefZPUlubw/ju5frl8eHF2KGL9nNP33MPTDd5vqF3aEtxzQGf0 +4PemUxKi/trOGewpyqNdpNzznGjraDEUi6CNQZKjv1isayOXqbDbEozuKq0flD41vki bw5CyXuZDwtU6Z5tBZbtxoE3srtNCu4Oeyq1sitnbBWCyVY1WeaAJk94Lb29SGNp5Eni xiBSDNbQraRHfOlPGnLw80e8V02yaO+h1U/N7OwmLF4bt0YpYRuo6Y6StyAxyeEl4blU myzoqIWgurl5ri81hpMQaGICogqofO3uVix+SlgMw5HbN17wvpQ4PQtDLZ69lH339jFG nnWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730245476; x=1730850276; 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=Vc3xum6aGJsQxt7BwGXU6tv/D45yqqkNrU/wViua7no=; b=sLxZ2nW6BkrhbWjLJOCxIlOC8pFt6uUa3KLoRIdL3NFnhfOVGojeXbjdJKt/y3iRNt twCE+ANm4dWHJcXUfD5RnDEiPrsU8qTCybTf2wu0Cl5BcT/S3c2W8xP8zUEku7pbU4+U Fwkkg3xH8bQiTsHu4NB57Al8MvzB3Bm+oMds4ZpHK/qkeDbPIcEgqrAl4sz2PFr8PKfY HjSiksW0W6voxapxhuaM1FvSZyP4zGL93qptT/K7kgbGDTWi1SfjDoO2xjwjTv7EazpF gp7NWqEk6TyArYr8WFj+0krSXhWa2J7XjLzBxh4UsFWgRqYUYInyWs/N7I1oZth7uuRd +7dw== X-Forwarded-Encrypted: i=1; AJvYcCUSt5HSihvsmgD1yLOmGE7jEx7wDmauoE3QHfy4pSXL4bxR0t2GvmfOv59UNyR3HDDyqU3wPWyWTA==@kvack.org X-Gm-Message-State: AOJu0YyNPQiYsovU47oRQs9UrAvr3Z9onZsWqiNJSd8pUb5Z/RuBGtGJ tdUWflLZf02YLr+TanoP64OP8pZFwPb2klCR295yMBd4WkHRQnhXDvnMf6wuH5M= X-Google-Smtp-Source: AGHT+IGvhAl0GoTCR6wpiYv9Nay6FcDGtnYTBZHKhrK9M6c3oN51TKIQjb4l2hAH4eTwfkMYzc6GGA== X-Received: by 2002:a05:6a21:1190:b0:1d9:215c:c63a with SMTP id adf61e73a8af0-1d9a850aaf7mr18291142637.47.1730245476543; Tue, 29 Oct 2024 16:44:36 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72057921863sm8157643b3a.33.2024.10.29.16.44.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Oct 2024 16:44:36 -0700 (PDT) From: Deepak Gupta Date: Tue, 29 Oct 2024 16:44:08 -0700 Subject: [PATCH v7 08/32] riscv: zicfiss / zicfilp extension csr and bit definitions MIME-Version: 1.0 Message-Id: <20241029-v5_user_cfi_series-v7-8-2727ce9936cb@rivosinc.com> References: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@rivosinc.com> In-Reply-To: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@rivosinc.com> To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Paul Walmsley , Palmer Dabbelt , Albert Ou , Conor Dooley , Rob Herring , Krzysztof Kozlowski , Arnd Bergmann , Christian Brauner , Peter Zijlstra , Oleg Nesterov , Eric Biederman , Kees Cook , Jonathan Corbet , Shuah Khan Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, alistair.francis@wdc.com, richard.henderson@linaro.org, jim.shu@sifive.com, andybnac@gmail.com, kito.cheng@sifive.com, charlie@rivosinc.com, atishp@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, alexghiti@rivosinc.com, samitolvanen@google.com, broonie@kernel.org, rick.p.edgecombe@intel.com, Deepak Gupta X-Mailer: b4 0.14.0 X-Rspam-User: X-Stat-Signature: shpy6dagcogrzx55st7ppq3fq3i8mien X-Rspamd-Queue-Id: 21B4B80009 X-Rspamd-Server: rspam02 X-HE-Tag: 1730245429-939011 X-HE-Meta: U2FsdGVkX1/hgAY+FfrEw44upTag0UGR5NFKr4hL8o2kLeQxp71tQrGDRQ2Tk/78miixSYP/NzZ3XzG70i4GqoFcSs2EChbBbEF26tKvOFWFipX3fx7Jzpu7N1C3ARl5pt11g1KpifVaRdBFlbQxiO/HZ/rQ/JrxgeaPJ7gWuKxxpXgsqbeBoiSqPx9BytFX5NHDphAUEMMyCyGhA+pJn9jtSF/1nMhy6Qg8Tr1gwThpE33ggNEaXMoiy0F19GtOhe5Wpl5ii6Xc19tiTT2uYr7ISrdWu5T7fvQ67vluTX6GZ3U9I7YN0wLLWecSXp5MUpzK2Mxj1tXgxS2iVIVhfGVsp3rIhzlD4xrvihmG22uL9122NRHnP6yJ90Fczc19dfvxZROXy7k5YRTGctSStPdw+6Vz3LsEdZieHDN70v9/MAxyLjYjbOifOG+ZYEy2HNQ3pR7WR1D4kRHlS+cxeADiPb7HN+frMZE7H1BhNO0SfU6TDZzX2br8ec+YOlSmruYC+6BtjKorV3pwoMImh7NcwdXNMWS9oh9nvgxyApLy8xv1GfZpCzsqXZocycJ3zhNqlkRLTIWuyODrF+1YEX0dEfuprZyFeCdWJOJHCw1Jk7JQVgf4M92iW1PI9VbAZqaZyw30+K45b6x4gAikQEhw3iE57Kh0lcROGDyffthmGms4okBtXPJuGO/2cFfGwPxyDI+1Rga2Lf97vmjiKBvtcD4fcclaFKiUAgk3w8JJT227M3ZOBExCoO7ZKQtkRIjsT7ZQ1RLVTQNWM1cjJvTGF5qqPjVqiIw9dtuivIJ04LzK8sEfgsSKfSm7evHo7PY7wMgfMRj3jpMjM7P1q9rwYqgYPoiSKjJRpYgFXSRi+oDhTNBHNRFG7+elZxzPrTdBptC8aTctNkER4Ybd2REZlqP4bMutL3Xj7a+DqCToJv4kAbrjfDla/wOjrHpk/jPhclemoLJC6BUHDC5 AFQWDZ6y ITo7B5K9xB+B3FN7Vwe0YuiAsVgF7g8mxGq14DS4DApo/mZiLsuIntipPoAt4hTmHadJu+p5jpKtCQ7KvXDN/5z97SNDGX79DOC6zazii1Wh+pr3o689Nfk/jTlZ8ihjbZoiKO6zcv855B2oe959SpTO2rXnI6LMRf+FqtNPwyW7ZtfljF8a+fEYh7DPRk0G24obEhktDxAeu7VNfASP4Z1l1FQsOdW7f0s0Ld6ne1gk9jG9/NEOWzHVEsDFP+I8RHZfe/ic1vETiFqSfHtL9rZsvBu9VgFQ0rL+X0FaGLVaboyCTvI9UfLZpPqhkVpqeMk9Wxy2FxO93LR0oqzpzUqsh5iVlJbe7r/HQ/uAXdofbmyZAyiVs3UQkD8FklM0d5zIqwwxhYmXa4YHMCiZN02QNHtnPY+SxdW4KVGE1y0sErmr3tD12+EBGSvytnjRA/bN6N054nMCpgAc= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: zicfiss and zicfilp extension gets enabled via b3 and b2 in *envcfg CSR. menvcfg controls enabling for S/HS mode. henvcfg control enabling for VS while senvcfg controls enabling for U/VU mode. zicfilp extension extends *status CSR to hold `expected landing pad` bit. A trap or interrupt can occur between an indirect jmp/call and target instr. `expected landing pad` bit from CPU is recorded into xstatus CSR so that when supervisor performs xret, `expected landing pad` state of CPU can be restored. zicfiss adds one new CSR - CSR_SSP: CSR_SSP contains current shadow stack pointer. Signed-off-by: Deepak Gupta Reviewed-by: Charlie Jenkins --- arch/riscv/include/asm/csr.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/arch/riscv/include/asm/csr.h b/arch/riscv/include/asm/csr.h index 25966995da04..af7ed9bedaee 100644 --- a/arch/riscv/include/asm/csr.h +++ b/arch/riscv/include/asm/csr.h @@ -18,6 +18,15 @@ #define SR_MPP _AC(0x00001800, UL) /* Previously Machine */ #define SR_SUM _AC(0x00040000, UL) /* Supervisor User Memory Access */ +/* zicfilp landing pad status bit */ +#define SR_SPELP _AC(0x00800000, UL) +#define SR_MPELP _AC(0x020000000000, UL) +#ifdef CONFIG_RISCV_M_MODE +#define SR_ELP SR_MPELP +#else +#define SR_ELP SR_SPELP +#endif + #define SR_FS _AC(0x00006000, UL) /* Floating-point Status */ #define SR_FS_OFF _AC(0x00000000, UL) #define SR_FS_INITIAL _AC(0x00002000, UL) @@ -197,6 +206,8 @@ #define ENVCFG_PBMTE (_AC(1, ULL) << 62) #define ENVCFG_CBZE (_AC(1, UL) << 7) #define ENVCFG_CBCFE (_AC(1, UL) << 6) +#define ENVCFG_LPE (_AC(1, UL) << 2) +#define ENVCFG_SSE (_AC(1, UL) << 3) #define ENVCFG_CBIE_SHIFT 4 #define ENVCFG_CBIE (_AC(0x3, UL) << ENVCFG_CBIE_SHIFT) #define ENVCFG_CBIE_ILL _AC(0x0, UL) @@ -215,6 +226,11 @@ #define SMSTATEEN0_HSENVCFG (_ULL(1) << SMSTATEEN0_HSENVCFG_SHIFT) #define SMSTATEEN0_SSTATEEN0_SHIFT 63 #define SMSTATEEN0_SSTATEEN0 (_ULL(1) << SMSTATEEN0_SSTATEEN0_SHIFT) +/* + * zicfiss user mode csr + * CSR_SSP holds current shadow stack pointer. + */ +#define CSR_SSP 0x011 /* symbolic CSR names: */ #define CSR_CYCLE 0xc00 From patchwork Tue Oct 29 23:44: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: 13855725 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 5117ED7494E for ; Tue, 29 Oct 2024 23:44:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AF7FD6B00A2; Tue, 29 Oct 2024 19:44:42 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AA2DC6B00A3; Tue, 29 Oct 2024 19:44:42 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8F6626B00A4; Tue, 29 Oct 2024 19:44:42 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 6B1006B00A2 for ; Tue, 29 Oct 2024 19:44:42 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 302DD140820 for ; Tue, 29 Oct 2024 23:44:42 +0000 (UTC) X-FDA: 82728271752.14.3C1A929 Received: from mail-pf1-f169.google.com (mail-pf1-f169.google.com [209.85.210.169]) by imf25.hostedemail.com (Postfix) with ESMTP id 514B2A0022 for ; Tue, 29 Oct 2024 23:44:22 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=0ov1IlhB; spf=pass (imf25.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.169 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730245399; a=rsa-sha256; cv=none; b=u6OHWfXpFDimcP8DTmKmhn4mM20ePqLEZw/YWmGGbPB01PtVY14tnpGgEbotrnyqgMEJTD 9s08QrI8E+jqBrwOmyOwW8cGPGxKDzKLMbwPF7+Aa0GGYWXccm2Jan0CuZNU2AebzZWVcZ TSm90izYPcQ4MlYO6Hg4W6cmPwdCTec= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=0ov1IlhB; spf=pass (imf25.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.169 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730245399; 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=SNfZZs476YzcsSH6yeSdP6ZT36oaA2c3QmON0KQSDC4=; b=KQ6QcYBK6dCAG4Dc1oydBLQWyOp14IleG2vTbe18I0AyOcl5Prm3OvFc8y5toqrXmZmB5t Z+Sq5t9Fl18j7TvPa3MJfk5qDUCX2Vka6sZ0BdCcvawwM2bR/LDNs7bXpd5G1mHIi5nbpO D7wQzZuQ9QIDxOVexxkQVDt5EEm5CEk= Received: by mail-pf1-f169.google.com with SMTP id d2e1a72fcca58-71e592d7f6eso4199694b3a.3 for ; Tue, 29 Oct 2024 16:44:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1730245479; x=1730850279; 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=SNfZZs476YzcsSH6yeSdP6ZT36oaA2c3QmON0KQSDC4=; b=0ov1IlhBCD7wZYUBMDxiLyPL2Ih3rmuip9eE7pgrolsEyOLAHreB6I0KUXaHKg/jlk 1PCfus0mwPG2xwcgYQv+jTkowBKONFXYzUYNzyluQ3mBZGcuIT6DlMNGBmAO08xE2d/c 1cw+xV3di8zU9YUiPZZuw/K1Hq6Ze1kEy0B9EW9gzO0GgS92ON3U7auaYh+i1hZQTgPg Hl1DO9MbWTQ6ZLOusiMq/m+Njynn1p8N+nu07ByNEv+/ncwRtOy1o2W/jN9RT8X3i0j1 qVBgSNpwn7GGBZBENnrOkzxRc8VNqXxfniTFXlH0SzBj9nyPAZVJRXiz9N3RLwpY7XP9 H6/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730245479; x=1730850279; 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=SNfZZs476YzcsSH6yeSdP6ZT36oaA2c3QmON0KQSDC4=; b=UaLUmyT0jyuDI6d6gPbDRA0+54aqHOCPHnKDacYqp2NwZe60Oe3fqGDrmymEbcLhKh shHTPePRkBrtDhzCX7vJTDG5BSxNiO+/dHbmZmTCbz2dmZHahT2PRyKrtx2+VKk1qObB a8O7tytmH2E4312oQz3nde0yH6/nBP8SlveT7LcqPn56PYjNn/xtM9syFed+EL7k8KnG dJs7YjpMIfxcPUZOj6MSlUfBHbj3LskRoyVsQ5XUk5zPpNGr5od8MaJEIblVFwJ17GB0 gjim5t9zD2im9S6SK10vC3r46EzPchX2LmBMcAGg4BJPsdGe0AuK6YLnAfsp+tpXbmtZ 4NOA== X-Forwarded-Encrypted: i=1; AJvYcCXS7nOZmvOWwKFfEDU75+/ijcKkOpSLtb2UqwY5gdG04jE8LD7KEmyRTkMfB1gVVzcb4PUhCxqGiw==@kvack.org X-Gm-Message-State: AOJu0YyP1FVr7zBkh4HILhnrCGAm/7Jru2HBh5+uNcZ+xEdCA5pYWi9V heNGmWin1nUVXfnYyKOqYyMU9PQYRXzVi3X5ze6MuutooPWC3oVYmwRxN23BG/M= X-Google-Smtp-Source: AGHT+IFWMMLwtG8UV6QhOdFu9KwNMwuiaXKBVbMM5qrfFdip4zC2zSk3P3saktQ5u5KEFmifDMFhVg== X-Received: by 2002:a05:6a00:b54:b0:710:6e83:cd5e with SMTP id d2e1a72fcca58-72062ab834amr19403115b3a.0.1730245479084; Tue, 29 Oct 2024 16:44:39 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72057921863sm8157643b3a.33.2024.10.29.16.44.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Oct 2024 16:44:38 -0700 (PDT) From: Deepak Gupta Date: Tue, 29 Oct 2024 16:44:09 -0700 Subject: [PATCH v7 09/32] riscv: usercfi state for task and save/restore of CSR_SSP on trap entry/exit MIME-Version: 1.0 Message-Id: <20241029-v5_user_cfi_series-v7-9-2727ce9936cb@rivosinc.com> References: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@rivosinc.com> In-Reply-To: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@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: 3e1a8ybjkwqyh859tj1rhurhsbjnwpam X-Rspamd-Queue-Id: 514B2A0022 X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1730245462-751967 X-HE-Meta: U2FsdGVkX1/0xUjcBqttDNo7k02i0ZR4OoFlkEJKtyvRI2GHc8h3Fc/mLXKyQX0gZu3eoq4oFQWQC8BkgHs+MudDvJtHp4AmRKYVUR+JWzm5PP/Qi4RBf525PaRyKiRsaOD8Jc3rknOrrfJrKSJvrElWQTTEic+/yTfRT/UM1aorvPTjZUF6rYwSzyb38HqabUB9Fjl0WyfrLgtEZZUIbA/Mv4gLYbghsU9+Sjj1aeMmrEwuyBqS+o+dm0YrJKAK5PI1hb/gEuYQX5nR2Cw8xUevty51/iDkzsDxCW+pj7mDzzCzDnqdouOyLqvev7+d2F700xpU/P2Z6RBfyP2ytmxJozW2IMj8+eIY6JCo0Dp+qMHhs7x9rYkFWPl10JfujeLtrY4eqmDZmAHLbfVj9nhPZCwCLqnq3EXh19X5YbTbCHPJTBKfj/k9tvzva7sL0tZ8y/nxxJiTBgQQkotHMh2XVt3YbWls4oA+PP176yIcEluCkjZYpc8SVD2tQTnEZois/RrDturHGl0xayaB9wmCdCs1/9Ctoqg1WWq34sNczmVwrn0SnGaZL9uGZaJ3BX044GiA8XR/CHWm8YxMicin2TH3AUvcxGh1W2r9p+EeD5iAdPJMea2PA9U8jZ8o+efujNKLyT+seW9WJNa2somOY6Zp9cA0zlDDcxyjYYAnkxQV6/YKBAoqWAnuceS1duIPKLKJWN6IozCoAldxfzWPVpxGiToHnIspri7bUgTNDx8+tPzxs3Ddyop0n4pFSdRaeUzdhh4BbsRaNXOUs5FTFZq6MpJQs3tg4diwArjW4vOomNYGXxKCfhcNyzbyTfQpzm2O8mrfMPQXOnnIORRFuyJbfhJEwoRLHH5ndGEzYRH5LKI8lTWoDMWljNMyE8OWS5x6p5LmfPI3LE8CFB9b8IkxhnIM+8E0tq+VOtlH6h/d7ChpJkrsRuZz2KyQPwepX/6TU0lfkVbPfT9 g4uqT5T2 Hwpq03Gv/iK0J+QG82GfPWtKrzwpzneAAqkOql5ffQKvVUrxdZfS2XgN53ilUx7ydS07Bb75+OamiEGq2dJbj5Jcei4PR35CV9/46Si6tyXULeOYXcWIdFCERfaMVnmk8QGKxPGCbIY2FyD7s0WjV/VKB166SGsRbmv+PQ3vUogVFebrVWG7/yk9+myK3g34x34GuowpxlKplTDcOTNaKWXwkBJx4JLtZ/OCasRZR0WPYp1GjWBRGbZlv44S1jYNsyf6ftdajab5ggXVuEM0BkZqdA4sC/xNmHGYbT4bm6eduSKTczCOoDaH6daumEGhGeDmWk654QUnIGgKCPYd4e5wLjZhhZ7CqkNMWb/+FyHt5+myY/86rlOVmP6INwtxjkXHEcivA/W4lBxtIEWBqP+isfazAWswGSmmbTA64zGIks4uoZmosnHlnXuDKFsVG9LSsb6y4jRnyVItq2skaqDKF2zlcjJGovhPWIq5zhjVsEEFNte3oYUCN4RYBRUGV9YaIfFb3wnm2WA05QSW8+3+LZB/dhsQj7ZARtUy+c0xdE1k= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Carves out space in arch specific thread struct for cfi status and shadow stack in usermode on riscv. This patch does following - defines a new structure cfi_status with status bit for cfi feature - defines shadow stack pointer, base and size in cfi_status structure - defines offsets to new member fields in thread in asm-offsets.c - Saves and restore shadow stack pointer on trap entry (U --> S) and exit (S --> U) Shadow stack save/restore is gated on feature availiblity and implemented using alternative. CSR can be context switched in `switch_to` as well but soon as kernel shadow stack support gets rolled in, shadow stack pointer will need to be switched at trap entry/exit point (much like `sp`). It can be argued that kernel using shadow stack deployment scenario may not be as prevalant as user mode using this feature. But even if there is some minimal deployment of kernel shadow stack, that means that it needs to be supported. And thus save/restore of shadow stack pointer in entry.S instead of in `switch_to.h`. Signed-off-by: Deepak Gupta Reviewed-by: Charlie Jenkins --- arch/riscv/include/asm/processor.h | 1 + arch/riscv/include/asm/thread_info.h | 3 +++ arch/riscv/include/asm/usercfi.h | 24 ++++++++++++++++++++++++ arch/riscv/kernel/asm-offsets.c | 4 ++++ arch/riscv/kernel/entry.S | 26 ++++++++++++++++++++++++++ 5 files changed, 58 insertions(+) diff --git a/arch/riscv/include/asm/processor.h b/arch/riscv/include/asm/processor.h index aec3466a389c..5a8031384021 100644 --- a/arch/riscv/include/asm/processor.h +++ b/arch/riscv/include/asm/processor.h @@ -14,6 +14,7 @@ #include #include +#include #define arch_get_mmap_end(addr, len, flags) \ ({ \ diff --git a/arch/riscv/include/asm/thread_info.h b/arch/riscv/include/asm/thread_info.h index ebe52f96da34..12263cef7518 100644 --- a/arch/riscv/include/asm/thread_info.h +++ b/arch/riscv/include/asm/thread_info.h @@ -57,6 +57,9 @@ struct thread_info { long user_sp; /* User stack pointer */ int cpu; unsigned long syscall_work; /* SYSCALL_WORK_ flags */ +#ifdef CONFIG_RISCV_USER_CFI + struct cfi_status user_cfi_state; +#endif #ifdef CONFIG_SHADOW_CALL_STACK void *scs_base; void *scs_sp; diff --git a/arch/riscv/include/asm/usercfi.h b/arch/riscv/include/asm/usercfi.h new file mode 100644 index 000000000000..4fa201b4fc4e --- /dev/null +++ b/arch/riscv/include/asm/usercfi.h @@ -0,0 +1,24 @@ +/* SPDX-License-Identifier: GPL-2.0 + * Copyright (C) 2024 Rivos, Inc. + * Deepak Gupta + */ +#ifndef _ASM_RISCV_USERCFI_H +#define _ASM_RISCV_USERCFI_H + +#ifndef __ASSEMBLY__ +#include + +#ifdef CONFIG_RISCV_USER_CFI +struct cfi_status { + unsigned long ubcfi_en : 1; /* Enable for backward cfi. */ + unsigned long rsvd : ((sizeof(unsigned long)*8) - 1); + unsigned long user_shdw_stk; /* Current user shadow stack pointer */ + unsigned long shdw_stk_base; /* Base address of shadow stack */ + unsigned long shdw_stk_size; /* size of shadow stack */ +}; + +#endif /* CONFIG_RISCV_USER_CFI */ + +#endif /* __ASSEMBLY__ */ + +#endif /* _ASM_RISCV_USERCFI_H */ diff --git a/arch/riscv/kernel/asm-offsets.c b/arch/riscv/kernel/asm-offsets.c index e94180ba432f..766bd33f10cb 100644 --- a/arch/riscv/kernel/asm-offsets.c +++ b/arch/riscv/kernel/asm-offsets.c @@ -52,6 +52,10 @@ void asm_offsets(void) #endif OFFSET(TASK_TI_CPU_NUM, task_struct, thread_info.cpu); +#ifdef CONFIG_RISCV_USER_CFI + OFFSET(TASK_TI_CFI_STATUS, task_struct, thread_info.user_cfi_state); + OFFSET(TASK_TI_USER_SSP, task_struct, thread_info.user_cfi_state.user_shdw_stk); +#endif OFFSET(TASK_THREAD_F0, task_struct, thread.fstate.f[0]); OFFSET(TASK_THREAD_F1, task_struct, thread.fstate.f[1]); OFFSET(TASK_THREAD_F2, task_struct, thread.fstate.f[2]); diff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S index c200d329d4bd..8f7f477517e3 100644 --- a/arch/riscv/kernel/entry.S +++ b/arch/riscv/kernel/entry.S @@ -147,6 +147,20 @@ SYM_CODE_START(handle_exception) REG_L s0, TASK_TI_USER_SP(tp) csrrc s1, CSR_STATUS, t0 + /* + * If previous mode was U, capture shadow stack pointer and save it away + * Zero CSR_SSP at the same time for sanitization. + */ + ALTERNATIVE("nop; nop; nop; nop", + __stringify( \ + andi s2, s1, SR_SPP; \ + bnez s2, skip_ssp_save; \ + csrrw s2, CSR_SSP, x0; \ + REG_S s2, TASK_TI_USER_SSP(tp); \ + skip_ssp_save:), + 0, + RISCV_ISA_EXT_ZICFISS, + CONFIG_RISCV_USER_CFI) csrr s2, CSR_EPC csrr s3, CSR_TVAL csrr s4, CSR_CAUSE @@ -236,6 +250,18 @@ SYM_CODE_START_NOALIGN(ret_from_exception) * structures again. */ csrw CSR_SCRATCH, tp + + /* + * Going back to U mode, restore shadow stack pointer + */ + ALTERNATIVE("nop; nop", + __stringify( \ + REG_L s3, TASK_TI_USER_SSP(tp); \ + csrw CSR_SSP, s3), + 0, + RISCV_ISA_EXT_ZICFISS, + CONFIG_RISCV_USER_CFI) + 1: #ifdef CONFIG_RISCV_ISA_V_PREEMPTIVE move a0, sp From patchwork Tue Oct 29 23:44: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: 13855726 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 977C3D7494F for ; Tue, 29 Oct 2024 23:44:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 353896B00A3; Tue, 29 Oct 2024 19:44:45 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 301736B00A4; Tue, 29 Oct 2024 19:44:45 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1552B6B00A6; Tue, 29 Oct 2024 19:44:45 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id E7D756B00A3 for ; Tue, 29 Oct 2024 19:44:44 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id AE30912076A for ; Tue, 29 Oct 2024 23:44:44 +0000 (UTC) X-FDA: 82728271374.12.7AA5982 Received: from mail-pf1-f171.google.com (mail-pf1-f171.google.com [209.85.210.171]) by imf23.hostedemail.com (Postfix) with ESMTP id 87770140018 for ; Tue, 29 Oct 2024 23:44:26 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=sTmpCCW8; dmarc=none; spf=pass (imf23.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.171 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730245353; a=rsa-sha256; cv=none; b=wneGPE3n97nhYVXtONNfMnsghuqA8SK9bTIfDqmWDlZQKiHOT3JAEbUrsKraaa39rkdE+4 GJU3semXbmhUq00iPzU34vGSdDdKcMzp+b6NgTl3aYHGRMfGUvFKERe7Y/vkRsE8KqWGf+ CPNV8Qgham+3dXwQ8/BTT+1AdqZu41A= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=sTmpCCW8; dmarc=none; spf=pass (imf23.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.171 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730245353; 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=2uB6wgL7AUIk24SoLAfGxw+LLkCCjYapUwagzJacJBw=; b=biAGvCzZbon/daTNmDTotNufMOyV//71SivSa4jIkFxl4VQvJEfIsYbPku4O1Kvza97CZv IvHBM772/OIpkCOxp2eZjQa77LzfrtnLX3wjfJTTuWTtbqjwi0oxIGMJes5lLX84vMzY8A GyY07BybVnzXO6fSt+D9iCBtnNWvANA= Received: by mail-pf1-f171.google.com with SMTP id d2e1a72fcca58-7209415ca51so1143712b3a.2 for ; Tue, 29 Oct 2024 16:44:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1730245482; x=1730850282; 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=2uB6wgL7AUIk24SoLAfGxw+LLkCCjYapUwagzJacJBw=; b=sTmpCCW8saSelxpn9yJP/ggrdVr3yvepfcVv0wvMlJpdTJ15iAttQDOB26r11Qt8ii L+Y8IkAskk3IhA6a6S86eb3l3TCT4V7C47wv7rPcA/0syO0Fnz66aFZv2biDGrZNu6Bg BN4w/6sldpo3yQX9P6iwh9dMVcmEr4g2spoveHjV+qZyS0P7dLLzjnf0GyeHe3GKKHqr BzMtlp9fHJQ0DsYy9kXzgBZefxrZ+Vj5vsqf4rvSYA5EVzesWEsLDcsiLQDMZcPovFkX 1+X0iyMOw0M4BQlkKscG+BQK5UB985vWqfujN0Ja45Qs3KMbyahTHyVr4ThMzohEBe/O O86A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730245482; x=1730850282; 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=2uB6wgL7AUIk24SoLAfGxw+LLkCCjYapUwagzJacJBw=; b=lc9sAlAsIbOq32yQLq1KxqPtckhs6IdTDMnTt3VqjnsRxEZHnuvoKjRqY8nBQcxTXx MZfmm/wfRsFHMm4FJ89nid7PToUJ6yrrRa2fTXRoUcTI/2oM7KOJzntmQNHoAsX669Nu 1trxMwyE/VNv9w9WNHyxsPDArQBGj3RLH6sXyZIYyH+eAkjUeSnNqV4V3rlIbALlr0/L ncDoAZzdevdSHl0BJ/FRPOyVb2DAe67cvZwcqECEB+OQ2NAikMrDDUOlqyZzJTgEo5EI yKVw05i4ReMidLDHiQrfq2An+QM76gEK7+/H7jCKk8V2weHk2E7LYFzufAT9sekEm/16 /Y7Q== X-Forwarded-Encrypted: i=1; AJvYcCU+OIts5+QDWR8YcKmDE5cRPYXiBufTz2q44yv683S12tQeLYMl4nqfVOmDBO+wTxZ1ylU4mDNaYg==@kvack.org X-Gm-Message-State: AOJu0YyH2zoond9maxgXSVEnGh1QdgrCrwHcG2axukWcU28UlkFX1wuK 46c7j+/jfV9sLJuCmiFZcBd7pH2PA0pU1f6WbmKT714AquMwFVZ5yUjiF0ZXmM0= X-Google-Smtp-Source: AGHT+IE221LbNxuFRa65lyyYoevgLhuJqbF2HZlgl90nwLOZSnaHzzPzVHYzhAHk2jRfxhGW9tswvg== X-Received: by 2002:a05:6a00:2d84:b0:71e:693c:107c with SMTP id d2e1a72fcca58-72062fb8107mr18791154b3a.11.1730245481560; Tue, 29 Oct 2024 16:44:41 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72057921863sm8157643b3a.33.2024.10.29.16.44.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Oct 2024 16:44:41 -0700 (PDT) From: Deepak Gupta Date: Tue, 29 Oct 2024 16:44:10 -0700 Subject: [PATCH v7 10/32] riscv/mm : ensure PROT_WRITE leads to VM_READ | VM_WRITE MIME-Version: 1.0 Message-Id: <20241029-v5_user_cfi_series-v7-10-2727ce9936cb@rivosinc.com> References: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@rivosinc.com> In-Reply-To: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@rivosinc.com> To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Paul Walmsley , Palmer Dabbelt , Albert Ou , Conor Dooley , Rob Herring , Krzysztof Kozlowski , Arnd Bergmann , Christian Brauner , Peter Zijlstra , Oleg Nesterov , Eric Biederman , Kees Cook , Jonathan Corbet , Shuah Khan Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, alistair.francis@wdc.com, richard.henderson@linaro.org, jim.shu@sifive.com, andybnac@gmail.com, kito.cheng@sifive.com, charlie@rivosinc.com, atishp@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, alexghiti@rivosinc.com, samitolvanen@google.com, broonie@kernel.org, rick.p.edgecombe@intel.com, Deepak Gupta X-Mailer: b4 0.14.0 X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 87770140018 X-Stat-Signature: faihbo3yjk4qfe5fyma5579p8gjqidpm X-Rspam-User: X-HE-Tag: 1730245466-800273 X-HE-Meta: U2FsdGVkX18be7R2oASyDZIW8qwS0cPn6gaEnszdUOq8zlVMCIeCgBdKo7P1AVaqYjd142fFu+klzAkzpkxdSUamw3Zn7tKdNlDv6kJ0o2w/Y/CvJA6LiuUnarg4S1SZDGP7hdfCBbXntml6ME/GdbW9638VYR9a4p6+SacuQ2jpM3HZk24CbpInA6SVb1hwO/Hy/B0n5+AEfF8HduNN0GYyFKTbwDPr4Ace5GSeyWaFOA/GHon7IdujaeFsUNN4uUjRaj+fbRtAWjoPaK9PVT+Uf0mzo9QTyvjzcgz2VhgIURdj/qinH+b1dkKPEIpbeEyZ87PlOgjAEZznxjK6t7j2NPz+yrUvrgA1Gy3V12i6ePnHGZmLQilZoNFue25WidVyP5ErgDUKC3ecsQ2hipgayir5SBgC6XrbwzNgeamHrjj/aHRwMx/oCF1slWUFSDRcD370NQSmC/kySECKLt1QKJu3AMtilwPyN6tqi08E1/2kd3RymLAzUivWXWuGMCZBjquN7hgJZHrL21pozBQpMfL7U9+SG4cAVb1UaOVJMP6mTGVKHHzcGRjeDJBb+AXh7l+bn+ynwoOXU8Z/Q9TZHdK0iEpFXCNc3fCTNAfTRwXSqAhgfyG4jlnHZwNhUmH45WOM3M+cAkDe9CONX9Y5yYddXrRwz9ktXExe0zDRq5lxhTtrEYxQEuGY+svPDR1vA4o8dxl6zMk1Ch/OSW9Ib/GLV2VYQ2F/STKn8jPamIwGWZ6Y1xtZWIkpvAkloTnS+fUdf7ZtBOmIMjMf9oLXsNiEX3eevXSzQSTNyPe2/sSQkquVktvF+f/bjqCFNAHCzLSU/KkYVR9LiL5DVWQWXxFqGh4nZlkrquVdP1/fHJKZ2LIyDyr4LYk667xo7yvDCV6oUhnj39JOifZhuyzljKe8bPTxEuZ3z0nF+ARQGm6Fmn/ZKKyJCgbj6jsy8HRrZ8ljD0wFmlmbHru eW9V9pAe 39BpO1dyOxYS/NVreNAORtzYewW6njV22el5OPRII6Qs/2z+6R8sz7H638xGJIg9fUeU6YetCHNtAEHCic+5wZ7HNuCV3nC9I+VfNiXgP9YBaj3tgrDByJO5u6M9ydR5foS1TFvx1c62lqJf/X3h2KjZWpnTjSuUca0u2ApLIYvhUa7K+0wsfjw0uAQ6wvwY4eTDS3kz5O4tkf23ycJmQn7Cv9yPimXuE6ALtTMKkxc1iyA9CiptbCHq/ystNnk5INqwrrNM3QiKVeC0V1x4HYQAAJ6gIRQueuDeQLxVYoJp8DGSFVFtGqnnWcDYNVNbVS+2Zf6XptEfSDU9HavXAiL8StbWwxBaHbcV6/n6qOyQLVV3CTpHyCYzoB0OghXhJCUdko+KCa3qYRpgb68mrCYxLm5DRsdoxS8bMUwGG3HYwvakabnw6dj45AMb2OXTGlGCSH/SLLIR5rOQFwGMq72BpE1l68QO2WmvRGGtqOSgouhDQ8FrOPFhKwF10G5eLgSTKKwidNRFrMrFp08u3VZ9rNZdRYLpILCPkJPkMrRh5jOI= 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 Tue Oct 29 23:44: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: 13855727 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 890FCD7494D for ; Tue, 29 Oct 2024 23:44:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E508A6B00A4; Tue, 29 Oct 2024 19:44:47 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DFF796B00A6; Tue, 29 Oct 2024 19:44:47 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C53116B00A7; Tue, 29 Oct 2024 19:44:47 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id A09A16B00A4 for ; Tue, 29 Oct 2024 19:44:47 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 56B91AC1DB for ; Tue, 29 Oct 2024 23:44:47 +0000 (UTC) X-FDA: 82728271416.10.2482C98 Received: from mail-pg1-f180.google.com (mail-pg1-f180.google.com [209.85.215.180]) by imf24.hostedemail.com (Postfix) with ESMTP id CC438180015 for ; Tue, 29 Oct 2024 23:44:41 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=hAWGannz; spf=pass (imf24.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.180 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730245273; 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=CzIJ0ckbf1GHBD2tYvzNgj5PuC5n3mvmNJyw5lLXOXo=; b=A+cX6Q3stG35TgEKUjMCgDKeMZ27sxAgo7c7KRpqkBjgqUSlagTfL2iLhmM92eys+e6zjN hHqxAC9nPhu+DilszgdJlIAgQFfBw9pNHAWYpM3HNBNutlJCNRimwWPp2Nc6RzXiMqagbo qxxvSAyF7vL7zsRov97/BDV145ykwRg= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=hAWGannz; spf=pass (imf24.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.180 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730245273; a=rsa-sha256; cv=none; b=ggQBr3R5AW3Sj1LGRULisdYjA2ce9nC9wKW1NEl+nWmAIFLr56UvFHNi+j8eu5+fQT9Gor gTYnxF3qKVxtVKMeD1vKfw9Bmto3kHBt6XzTL4qiSg4WEZEO5eXSrku6TUFN5HE61NyKO0 YfeGKit5AuqyLbcWEU8bNMSHeCuWyag= Received: by mail-pg1-f180.google.com with SMTP id 41be03b00d2f7-7ea7e2ff5ceso4444678a12.2 for ; Tue, 29 Oct 2024 16:44:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1730245484; x=1730850284; 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=CzIJ0ckbf1GHBD2tYvzNgj5PuC5n3mvmNJyw5lLXOXo=; b=hAWGannzMOiQiBdFeZqkZ8+aKvJHPFfLgPojPnSD0RkNhVJS2mxw+uMyQhnQloh+ZP kLTHZFaSE+bAxDw6/3eLwVho0a9AiNg5WsGGVtkwyV9R5UG3Yomuiv3z4awY6zqqwh4R RsUU+zjyVbiKZcN72eB8+lvp9X1NuAaW6C0q5LjHKhW1pEeUKUBdiQGasz1EP0xr8oxi BLtsmbzHWraD5zYswJYkGFZu5Xuf/Dn775WudM2ma9hhTz+nJ+GzpaEQRguD3YDQ21PN rijCfWrIEcaivg9TfHzbqgYDkUvjDUEE9MthDCsfZOj8zrB9nTFgogZWqNMzBEgLEFAd l/8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730245484; x=1730850284; 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=CzIJ0ckbf1GHBD2tYvzNgj5PuC5n3mvmNJyw5lLXOXo=; b=YMHa+6lB8/85klaggwNPz86gAk88Mep0nP/AT5Z7xuS4go6NGL+wzehmULY1WMVbRY OL99ddUHfbtQiOSOzm9YNuH2MvEsEVjkp3EeQ3LEqCobilrCdIRYeUksQbwsTXT3yfuy cI0mWDGVZ6I4V4RHypv1PSGbzxfZnlgmZikCN3QamsJEGCKmkVlhzT0eV+xWSgAQWQTD YPqSbQoSTruz2o01BTLvxtLzt0qEHtNujArjV7GrldkaufpjeCUms8YSXQk9iRP5euD6 RPOm+OJzZwBG43F+hMps/qEBCHcC4ZFMb9J0nFsUan/srJZ/0OZZ5A5GNbhE353dZPmV muOQ== X-Forwarded-Encrypted: i=1; AJvYcCVS88Cf+prBHCqaOsLy8cVhg/tAK4C/o/HylItLStiIZSGTG+zNWQZGtlizy2C7Y+QdHIbbDoAjuw==@kvack.org X-Gm-Message-State: AOJu0YyP64kU29IAWG+Ntv5LIX6NpW4o/awk+hxpmlW9++MwHDpdsfm8 Bxmnk3SFcF3dqfzRyZW2Z0UrLApFkBDMcW9u8TF6mWTJ6SxZreqaLeu8ACOtyDM= X-Google-Smtp-Source: AGHT+IHO9NpgsFfUG2/uawtteNyrFufrWe38CJ3nbx9BvyIZZlCpghacJ/qvTmf6Sjhfn55Wj0xhbA== X-Received: by 2002:a05:6a21:920b:b0:1cf:573a:bb58 with SMTP id adf61e73a8af0-1d9eee1e178mr2067308637.40.1730245484368; Tue, 29 Oct 2024 16:44:44 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72057921863sm8157643b3a.33.2024.10.29.16.44.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Oct 2024 16:44:43 -0700 (PDT) From: Deepak Gupta Date: Tue, 29 Oct 2024 16:44:11 -0700 Subject: [PATCH v7 11/32] riscv mm: manufacture shadow stack pte MIME-Version: 1.0 Message-Id: <20241029-v5_user_cfi_series-v7-11-2727ce9936cb@rivosinc.com> References: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@rivosinc.com> In-Reply-To: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@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: CC438180015 X-Stat-Signature: okpkeip8x17pbcgbjw4zwcsahh8u8gbh X-Rspam-User: X-HE-Tag: 1730245481-277797 X-HE-Meta: U2FsdGVkX19TZbQEacLaj1E6fEBstZT97IfbHh5jjXwb9GBVQwna/8WAAipg800kEMwHDj0BNVSuzfSGJuQwLh4crh99HHyHWbRgCVjMExGP+IhfQFRd7+0LhJN/xxHBSsJfQ7XrkVteYh5ihFuzW49NCiaktLE/DbV/ScJlcS/YL4UUzD7scca6UVbPB30pj5nxBtlji+Y14fhD2vht7pwKFKQOWZ0bOwoVd02dPOGlv0twUza0KoCz4ukBxwDCCBr2bqJMXj3vDnRPDa/z6ojS1irtcbiBrVEaFg1neh3SqkqRT9fdyy9N8gshtIiBPPSs5GzFygPNhVu3D5nIuJ1e6iMsYr4GlfLK2u/1MXiHf2b6hZr1uMRV+DgXkzu5ngz3WqmpqR1BcWMIlE4/13tdSeouWcf7K/TYCMolPj9pPoy1CcKyzHnh5gvDz8iKGlzr7sWUMHPxtD5L+zvLb6b5R/40gxiircuAX4Wu5QdkU43m+oyPY+XNonrUXKjftLxB63iChiiihGVB4zkIkD+2KbP4Vt1ka+pKbu5wrLJVweehS6zrLmgHTDn7GVz7qgYpXG13FqjVhH6fR4hLnk2ChfeCTUSCxVKtwC25T/v8tEk08rgkq6CoU6hvQS8Pq2CrE0mLFMnOyPdIeny/2nb9lFNqCnjAQijjdtevsa7w5rLntARxTS8okOozBhNet4W6Mt6nk8f4qabx4wPZazuNYeFX1AlQLGOwZ51VM6NWEroIQ4fdKtldsNumcieaVuNwJ6zPTSM8ePZ70UOyVNzFNVTmuZVLxMKUiGZNjKMaD6SzW9+QnG9bmB7U+og3cFqf97fpQBDBlFFPSrY0s52PNUvzzaSV0EaXbHnQLLqC4e1bRevpL1FDi27HaxbwABoBHpUrksG8ZVdo1fOcKHGThJ7ATgIqNeY+eMb4KnX68XSOoYSnzP8/XVV+HYBmcDOI1YXMxZQhNKOiyz/ fBZphEeV qe2BzYHUxp6NQEMDpEcMBSa6+etnoayU5LO7hCiqDHy0QTIplsQAb5jOHeAtk5iHvaFzvtzjtN617w85X33J+T846D38YuEIPH+WvneylDAfeUOCPFUidEQ5Ez6c/7TE4jN8HJUPKyTGEgWlHFw2OScCZld4JtF+VkTPGJiFs5pi4VRA6hSMdo8vK7dPDyK/BbnDwBKSwYKdvRY5ZG2XhNtboudE73SLwxH/nKeYB5geS9Ya2S3GCoXl/99hKxrpzeWKdu6OPpBK4HRoqKD5GzakBBW5WsAea8xi6tQTQb1HSLFRXkg0V1Su/CcIcsPsD9mDcEUTH3MU6wxvHigJz0erNTJYXhJKNlc7M4lw09J2brxcKXuDr6gfGmnFYGiHeSePZ7QzN42w5A8ZqYlzqcktEOf0un96z6FD2DhG0q5/WDsRaLrf63nVhufkM8feHq5InoQUBf6Z0PeY= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: This patch implements creating shadow stack pte (on riscv). Creating shadow stack PTE on riscv means that clearing RWX and then setting W=1. Signed-off-by: Deepak Gupta Reviewed-by: Alexandre Ghiti --- arch/riscv/include/asm/pgtable.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h index 4948a1f18ae8..2c6edc8d04a3 100644 --- a/arch/riscv/include/asm/pgtable.h +++ b/arch/riscv/include/asm/pgtable.h @@ -421,6 +421,11 @@ static inline pte_t pte_mkwrite_novma(pte_t pte) return __pte(pte_val(pte) | _PAGE_WRITE); } +static inline pte_t pte_mkwrite_shstk(pte_t pte) +{ + return __pte((pte_val(pte) & ~(_PAGE_LEAF)) | _PAGE_WRITE); +} + /* static inline pte_t pte_mkexec(pte_t pte) */ static inline pte_t pte_mkdirty(pte_t pte) @@ -738,6 +743,11 @@ static inline pmd_t pmd_mkwrite_novma(pmd_t pmd) return pte_pmd(pte_mkwrite_novma(pmd_pte(pmd))); } +static inline pmd_t pmd_mkwrite_shstk(pmd_t pte) +{ + return __pmd((pmd_val(pte) & ~(_PAGE_LEAF)) | _PAGE_WRITE); +} + static inline pmd_t pmd_wrprotect(pmd_t pmd) { return pte_pmd(pte_wrprotect(pmd_pte(pmd))); From patchwork Tue Oct 29 23:44: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: 13855728 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 84D92D7494E for ; Tue, 29 Oct 2024 23:44:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7E7E36B00A7; Tue, 29 Oct 2024 19:44:50 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 796106B00A8; Tue, 29 Oct 2024 19:44:50 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5C3BC6B00A9; Tue, 29 Oct 2024 19:44:50 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 3AE866B00A7 for ; Tue, 29 Oct 2024 19:44:50 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id F3A9D12079E for ; Tue, 29 Oct 2024 23:44:49 +0000 (UTC) X-FDA: 82728271584.22.76E3039 Received: from mail-pf1-f182.google.com (mail-pf1-f182.google.com [209.85.210.182]) by imf05.hostedemail.com (Postfix) with ESMTP id 84B27100006 for ; Tue, 29 Oct 2024 23:44:00 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=rbwegUCY; dmarc=none; spf=pass (imf05.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.182 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730245358; a=rsa-sha256; cv=none; b=BOzB+7pE9n4abeOmhgd0TQLVr8idxy/VUKXrRXvyjpZAlvZIh/PIBMds+0EmTX3gmqofTt piBBBGGg0IG6ppIBgO9OBxUtFw/kZp+L22gn0TWW+gM6K8XltraTvU+jVvOlnDcu5rII/6 GLB9lXlq9AECvivtpfXVFqN2KxbKCL4= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=rbwegUCY; dmarc=none; spf=pass (imf05.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.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=1730245358; 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=vcvA1auSaL3VFQzS81Rs8lzkbF+oHOz6Si8rlZEwmTE=; b=tNaKI2MXKBrK4BCWbxPXHG4CRaCRTDuIImLxE1Y7RakkyJLGZcqxUxn6B37oML4i0KdFhQ IzmRrC/HMyNUJmF8AZst2V/+8Dvt1V9gVDWw5bHpjyg2cKhah0xJiUoF+f4oDxG/aoFPpG KfxZupkCzmYTNmLLjKpL01t++6eCyAw= Received: by mail-pf1-f182.google.com with SMTP id d2e1a72fcca58-71ec12160f6so4359747b3a.3 for ; Tue, 29 Oct 2024 16:44:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1730245487; x=1730850287; 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=vcvA1auSaL3VFQzS81Rs8lzkbF+oHOz6Si8rlZEwmTE=; b=rbwegUCYW4F0z1+BbJ7wKfTIYUvXvNOJR1aI0gqTvLhGFup1/Xolcm5ucZ79UgUMga TzRtVfdzZzwNhbmTkZvdb6AzBd0cwq7VOdue0laujLzIWIgyXx8wc/QEiU1mRl5a0SFd lnBO4LNqFvVpgkfi7icCDdkEIm0ZSYx4934nWy5QG+tbKXD9sI047K1jpAtkjrfIvyA0 YyUspLqOE5Hp+HW9Lad5jCu3ZfZrF07qCNdZLO4xzK+oQ5YrMNaHtZBUWaQVlRbYP47d M0CK21fHgx9mKXKav/MrW9E1n84e7tCrM5kcjBhzqapjgXu9fw5w9gb12zJpLh7LBNLp XO8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730245487; x=1730850287; 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=vcvA1auSaL3VFQzS81Rs8lzkbF+oHOz6Si8rlZEwmTE=; b=SjlmNwtTtRtqvdj3JWf7VjDIaGk9yWCmihySAe+/y+i3klorHxiJL4aLI8jasCetKE ULiRbe0pktfkD1fA3VHg0OBuQfsPhA8o2SOr46L9c5MUNOihG5tCfF1IycbLBcvne2de mRs2sSVbOojcCJ30qZPyawx7Nhax3DezSBCTeYdJtblsOF4z5RR08UIByK6Z41T5BDio 6VW5tWLEH8A55xeNQZuR3kOXV6SxxAz3tD8uSXGfchpYJ2S80YtH4bnASWj1xuyXfPFw s/XCPWmSJ2N97Nu/pMU/pdR2DZqzphwQPVZTP2P/yP6lWBBpmk5UIV0r29G+55c/92CJ P/Gg== X-Forwarded-Encrypted: i=1; AJvYcCWFwvgULg1W/9h8qJMQUVBroHVMiFndctSTbCspzxLKWXYYmBtpuwkzG3sCH4Tpgr++utd0+M6D2w==@kvack.org X-Gm-Message-State: AOJu0YxUgwJoLe0BHpoyD3su83k4f0b0kZJImAPlwARqjWOLoBoaMVsd Ae6KCGHqfza9bVO0Ni7P1aIhARXFiTDZ3DXU/kKV0Ch84UR7rwxxTR0bIFdJil4= X-Google-Smtp-Source: AGHT+IGGMP4Pw+lp+LPCrok5EqwNoJFdizqNNUNEZiV3YZc6kA7wl06mzdMMPrxb/Znt26d5Smw70g== X-Received: by 2002:a05:6a20:c797:b0:1d4:fcd0:5bea with SMTP id adf61e73a8af0-1d9a83ab7f2mr18393767637.6.1730245486894; Tue, 29 Oct 2024 16:44:46 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72057921863sm8157643b3a.33.2024.10.29.16.44.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Oct 2024 16:44:46 -0700 (PDT) From: Deepak Gupta Date: Tue, 29 Oct 2024 16:44:12 -0700 Subject: [PATCH v7 12/32] riscv mmu: teach pte_mkwrite to manufacture shadow stack PTEs MIME-Version: 1.0 Message-Id: <20241029-v5_user_cfi_series-v7-12-2727ce9936cb@rivosinc.com> References: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@rivosinc.com> In-Reply-To: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@rivosinc.com> To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Paul Walmsley , Palmer Dabbelt , Albert Ou , Conor Dooley , Rob Herring , Krzysztof Kozlowski , Arnd Bergmann , Christian Brauner , Peter Zijlstra , Oleg Nesterov , Eric Biederman , Kees Cook , Jonathan Corbet , Shuah Khan Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, alistair.francis@wdc.com, richard.henderson@linaro.org, jim.shu@sifive.com, andybnac@gmail.com, kito.cheng@sifive.com, charlie@rivosinc.com, atishp@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, alexghiti@rivosinc.com, samitolvanen@google.com, broonie@kernel.org, rick.p.edgecombe@intel.com, Deepak Gupta X-Mailer: b4 0.14.0 X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 84B27100006 X-Stat-Signature: smmz3mb3acd3sopnm3rf1duq47zzbd9q X-Rspam-User: X-HE-Tag: 1730245440-505059 X-HE-Meta: U2FsdGVkX18ZpiTCeVoGOz5Ef+QZSodv05KylJflCf5XJpkDoBneczXKIztdS0Z2zilJl84AJGUFTIguBlfAVBttRvDHNQAmzbeQac6b3tC6kqsS5OcOip+HMNhGvOEV9/Ayheog/AiO2qhmUIsJdRIid7AgMH0uqtGel8KFmOM9aON6wbnyFV/D9w2BZ3izp0STqHHaKDkOg3lZjNbPZBaqcMX3wZBSFuho2W5A1DSUn7p5fQHJSi/heyjwRYRO58hiifZwc4kogr7mHhhh1JcoWkufg6qZbE64Eni1bWMN0vE77PaC6rJ6Vj2LGmIPQa4QUKILHSROxIHt+1VBtOGemOrleWlfscFWy+l/NiOxncM/eIxUOzSfTKr3HgMc+GD8Iz7Ts2STVvxLGuOco0OkWaLMQjkQ53YtkAAD7hiNFVY6jwM70fm+nhlXcxsUMPYff1NdL8IMhf3O6eYgQjHflNIWOZQnKNGqZ4IAaRFYkk4VX3ljt0OUJ/XaNmAPOo6fu2c0ysyc7r3bNCbLQhDoMvZiFzFzlWqrV3iYu2p3xKUvHBakdGnXD6PKtgoR8ZsV+8kUpOBT7TBI26Fvg455V/qVjMqNIKIn1RgNQO7vHtcA9spuyx0u/KFfWqQY5/CAdatwLgY97ac6Ti2UFD6GB02gd2+Z/HSzZ3zCk2Y6EDcBBHGDbGJE5dARuU0XipACImPbIBKa2EitdvAxeEhf4tiIO6hUnkhdjGdoFbtRoDYKZm5JLoefyDMxd9ch3HDcUdHM3Nw9HKsn57/wELsb+I8sEpd2yqC3yBGnh8ayGzc9+el4TEPZsvcgcHKAlEr7PBrz1QVBSqPXmYyGyTuUtfqaHQ6V8FyR+qPxpgPJttAf9fS/8BFm415I1Z4N1l4A8Y75sanwBVrEV1SXw4OM6s5ly4WFJbyYjvUo2kpuu7QTweDvDWeCbd8+H/IlhKwS58K8fP7kupLdU5p e6T9zJem 28ZQB6ZiwOQIk7+i9wpmzAQ97zOGgajWGG4KRIzI5XI2A+8MyY8foPfPVPY9xGKQqIGkuXJDoebW8HG0E9C7Fo59yAnHjAQIKZwfWSUFgqRRyp7g0BTXjN1i79gnAfa2YdMWxAU+hHO7Q02o0TWxQsIp//A4ISXfivjoLhGoV6rhhqMU6SPfnRHjjHmRqtHUZL/yee3Lkyaxp0bCsHs2cx9ap/Ib6GKrccS3abX1cUGbqojkPPd7cBtoHm17C1+6wUXRHhwcBy1px9aFbxrQV5y8vc35NM633PuhQbPQO/Lo0uQxhdM05Lve84KwFk3kdhAaPt0I3eyygYNkJzwNjoChM9D0JURR1Fb9fxQ50BCAyQBg5XZadRae0481D4ZAMn8NKAS/CcQi2T507XGN3usOLXvyIijj8gIhIDdIAWu4avanRAxN0PGGE3otxUDpMAsoqjFHkEN+z2O1nTNYfkQueX40CLr8vmxMts7NDH+wxx/5HpJcRCwkrCIC38LNE8a4G X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: pte_mkwrite creates PTEs with WRITE encodings for underlying arch. Underlying arch can have two types of writeable mappings. One that can be written using regular store instructions. Another one that can only be written using specialized store instructions (like shadow stack stores). pte_mkwrite can select write PTE encoding based on VMA range (i.e. VM_SHADOW_STACK) Signed-off-by: Deepak Gupta Reviewed-by: Alexandre Ghiti --- arch/riscv/include/asm/pgtable.h | 7 +++++++ arch/riscv/mm/pgtable.c | 17 +++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h index 2c6edc8d04a3..7963ab11d924 100644 --- a/arch/riscv/include/asm/pgtable.h +++ b/arch/riscv/include/asm/pgtable.h @@ -416,6 +416,10 @@ static inline pte_t pte_wrprotect(pte_t pte) /* static inline pte_t pte_mkread(pte_t pte) */ +struct vm_area_struct; +pte_t pte_mkwrite(pte_t pte, struct vm_area_struct *vma); +#define pte_mkwrite pte_mkwrite + static inline pte_t pte_mkwrite_novma(pte_t pte) { return __pte(pte_val(pte) | _PAGE_WRITE); @@ -738,6 +742,9 @@ static inline pmd_t pmd_mkyoung(pmd_t pmd) return pte_pmd(pte_mkyoung(pmd_pte(pmd))); } +pmd_t pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma); +#define pmd_mkwrite pmd_mkwrite + static inline pmd_t pmd_mkwrite_novma(pmd_t pmd) { return pte_pmd(pte_mkwrite_novma(pmd_pte(pmd))); diff --git a/arch/riscv/mm/pgtable.c b/arch/riscv/mm/pgtable.c index 4ae67324f992..be5d38546bb3 100644 --- a/arch/riscv/mm/pgtable.c +++ b/arch/riscv/mm/pgtable.c @@ -155,3 +155,20 @@ pmd_t pmdp_collapse_flush(struct vm_area_struct *vma, return pmd; } #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ + +pte_t pte_mkwrite(pte_t pte, struct vm_area_struct *vma) +{ + if (vma->vm_flags & VM_SHADOW_STACK) + return pte_mkwrite_shstk(pte); + + return pte_mkwrite_novma(pte); +} + +pmd_t pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma) +{ + if (vma->vm_flags & VM_SHADOW_STACK) + return pmd_mkwrite_shstk(pmd); + + return pmd_mkwrite_novma(pmd); +} + From patchwork Tue Oct 29 23:44: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: 13855729 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 B4082D7494F for ; Tue, 29 Oct 2024 23:44:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 63B316B00A8; Tue, 29 Oct 2024 19:44:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5EA646B00A9; Tue, 29 Oct 2024 19:44:53 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 43B906B00AA; Tue, 29 Oct 2024 19:44:53 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 23CAD6B00A8 for ; Tue, 29 Oct 2024 19:44:53 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id D8F291C1413 for ; Tue, 29 Oct 2024 23:44:52 +0000 (UTC) X-FDA: 82728271752.12.014DACA Received: from mail-pf1-f169.google.com (mail-pf1-f169.google.com [209.85.210.169]) by imf24.hostedemail.com (Postfix) with ESMTP id 3D90F180013 for ; Tue, 29 Oct 2024 23:44:47 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=iKioVAwl; spf=pass (imf24.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.169 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730245436; 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=c0B+zv7jbCWDse6UooVJF1UXIWGMB9DKJ0N/6xrNRow=; b=pH8VeuMNuNWkQ8mgJiMOVsTsDlJ7Vpy4bLwCieHj391af/UAm0bknh+k/w3zt7Bi8/4u+v tR2x0W/cxBrWHfIQn2M9045QtZodewSSlexVNV0wvxclfbR0MVCak/ZT6Go6mk7R0WC13k XyZP/mzTEnCo1EwBA3EyB00jMP5DdBs= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=iKioVAwl; spf=pass (imf24.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.169 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730245436; a=rsa-sha256; cv=none; b=CqsLgCWuE8Yj/G1OzfQjLdDASEC405nf7DlljKPG//ehc6Zeixu5wv5Crv8iLgDBHqC3Ym B87gVRBtfSmoDCh3sGzq0wKJZOVIkVN8QwoOwSPeHko87AjPXJDY/bVRTyQ/2OMS6SSB5s NbP4UAODXoQn8WRb+l0DvymF8noMQbo= Received: by mail-pf1-f169.google.com with SMTP id d2e1a72fcca58-72041ff06a0so4003023b3a.2 for ; Tue, 29 Oct 2024 16:44:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1730245490; x=1730850290; 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=c0B+zv7jbCWDse6UooVJF1UXIWGMB9DKJ0N/6xrNRow=; b=iKioVAwlF/o0RQn+M74X8EwFnkdBYhtveyrPoKFRSP0KogByA/lcTw7RZjUY1DWTww IwJ/xJF16OOwLBLaDX48UAPOeUtwzzHRYQ0bVpPoNmCkqb8y6q9bF8UEumQHtiIbr02E yMtL8BHjySBxl9Mx22FYGp1N71/OskxvNLL2OVxCVPOFVm5yslHB5V4YKGjLPZ7G4RWM TueCgls2UkWFdRHN+cmwwJIGNJcr1rxAa3cOJrJnL6bgVDMN4YAuvcBwbi+37euzAAxE ZZSReh+V6kx0m+/EjsM+aUnGk7gC0QU/N38yPH5tWCn1JHJoNGHvZoyyukUO3F0ZdJVQ JTGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730245490; x=1730850290; 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=c0B+zv7jbCWDse6UooVJF1UXIWGMB9DKJ0N/6xrNRow=; b=c/w+sofMywQz/UmrrBZUc9gqtWDNVCrS9HTbZy9sUIQyV1f/feKmw33J/J/a5zQOka VAzQIk21IFNYHwbQgji26YPI6vykHldlD2LhCaBtg/o3kNkEJyCsBnyAm10JtG0QhQNc 1TtYT14XFGzGLkHfeQYbCJ3xy48KkLjlETsN+WMxSt2/CDTWbAjaiMiVsBCNfkSgOVvs 4QGxhW21BV6cvtcK5kZNCelhdWadpUT2buDGDFIfmXuudvqrxnZtHo7koy0OMJRDX0A2 hoe5gQRkGx5O54lCgkKZAXn9Qh8Tt1hiMziCTasu7kdD2qB1xUKQYYo26F8BhzpNTtUn BX0g== X-Forwarded-Encrypted: i=1; AJvYcCUo6NgenMF7PcrXqEO2UYc4SRWBfb601+F43jJfDOafS67ZY2tlilUTdF/N27hFQRAOaNJiHRBPFg==@kvack.org X-Gm-Message-State: AOJu0YyQzhBFFRKOh0I6atENeEuvy502qsRcfEoFi9hwYVzjN+dvHjh2 53g1+3z95vPGExv4ti6MmDcmBXonuR+oPhaX8BrdELcXHJZeBz65I7mAyLwncNk= X-Google-Smtp-Source: AGHT+IHBXcg721hFEfeXndFELLRbZaKej/qAWRXm0LpVc3pKofZsRnhCap8fGfOK1cqNWExKjLYj9A== X-Received: by 2002:a05:6a00:3d4d:b0:71e:a3:935b with SMTP id d2e1a72fcca58-7206306ebbfmr19631100b3a.25.1730245489674; Tue, 29 Oct 2024 16:44:49 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72057921863sm8157643b3a.33.2024.10.29.16.44.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Oct 2024 16:44:49 -0700 (PDT) From: Deepak Gupta Date: Tue, 29 Oct 2024 16:44:13 -0700 Subject: [PATCH v7 13/32] riscv mmu: write protect and shadow stack MIME-Version: 1.0 Message-Id: <20241029-v5_user_cfi_series-v7-13-2727ce9936cb@rivosinc.com> References: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@rivosinc.com> In-Reply-To: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@rivosinc.com> To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Paul Walmsley , Palmer Dabbelt , Albert Ou , Conor Dooley , Rob Herring , Krzysztof Kozlowski , Arnd Bergmann , Christian Brauner , Peter Zijlstra , Oleg Nesterov , Eric Biederman , Kees Cook , Jonathan Corbet , Shuah Khan Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, alistair.francis@wdc.com, richard.henderson@linaro.org, jim.shu@sifive.com, andybnac@gmail.com, kito.cheng@sifive.com, charlie@rivosinc.com, atishp@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, alexghiti@rivosinc.com, samitolvanen@google.com, broonie@kernel.org, rick.p.edgecombe@intel.com, Deepak Gupta X-Mailer: b4 0.14.0 X-Rspam-User: X-Stat-Signature: 5n6mb1rd93abzj3kcja3n93dmmbzrfcs X-Rspamd-Queue-Id: 3D90F180013 X-Rspamd-Server: rspam11 X-HE-Tag: 1730245487-44482 X-HE-Meta: U2FsdGVkX1+OQeBmU63DlgwWVGJLQUeuW4DBZvFJt+u/jajCLuibrjDenDUBrAczdGmpUNjlnWZraOMe8nVQp4RAerHQEaJES2+4Y+FP0aUl61L22gFvq/Qo10YaefJpVivmzFwNOWipKNbJrQl0USc+4wE5FRgjSIT3Spuhxi2/t+irVd1RYFK7V/km6GnNmUrFLnqV96dGlkFo0NWK8q6mGzfJEUJU60qsiiBSZsZf2+GT+e/kSiX/dy9C1Gncur58p+AQmKBZ7Mbn73w+DjQ1nKQUMPKhDCabGGoROJ0DOnFAXSbjzncTS3Ni7Tha1drOapmeDV1tcpzB456LxOjYKOCd0B8mZFB6Nyhxf/GVBDgtFFi3gH/Yt8ehyojrPWQxmpnRhoz0kJWqcsKF3GD/CQceI7WrV2xR7gAF5MPTVVgC+LCfk5HqB9YZZAk7TCqIlKccA1+ra9TYgCLXn+kOGMht0UyNZ9zPwIhi7cptXwiYAMEwfmoB6TtVCnL6hvQhh3eUSMU86wFD3qxQO3sFvp2MRJJTI5DvLz3jv1MhF6bbh9Gk6Sr9oadGtfLgyclh0J6iIF5KCPa3gH2PFzcXIlW1i4wBa9OYjge7aIjDnxKs9HQo448LKzBNHWQ5T72GxBOcy5ms5/K6rBoVmSxN+bON0rRCZ4BDz+RIXxFudfnXXLaUHysaUsORLZyXkRXASmuI4pDF1RVfSi6+ZYD2XXRC/UTtTXVon6esjx6dUW1xJJ8FhWozO8K56mDFC8hvr6Eb+6eiWi/pcn/V/7lYNfEdE9debAnGzBrET28g2bSX049Owh3soK57nlkTbaBI8b6CKqHDgVTUiRy7C/rsNlzIunXgjvxS8gYxDDSl/9Qf0DfFFNxCiYOyRgarYDMxxjAZJNcyeolWo0WChPDS1ELYbp74EZMM7yTvOW3GDto6UUlG4YMMAsDp82gV3OPage4EeNZD7ekFWVP efkcHpM5 sOs/O1QUZNZzcK1y+9C3pGXH0bAhUKmzQ1+/18/jCLo0atCbm1OvVzhs66mdJsRqy46LmrxQk6OVbFCNtoWhOUUcrDt50JJ4I4iB/XxOwU4W25nEagSNOfvR3o8X4yu6Fl+maKP3cf3LlKW43jtgb4hhXvYVpQzmm7iSHOuyUWDfpsYbArQm0UD51IhbgnpRWgsSmAPxr+8Ql5s8eOTzMiJn0cAyrBXRoHrVBrbfXi0GuDuIwns6I3SBLEWSeEt9JQCWwFrXe86Wi2u7/8PXNnmY4PbZAygPQYpi8vwA/LcR3Tc9ntNvNU+09o6DRq3FakcBPifuFrkpN5asoLc3OC1BYYQezKMk9tM/sDWG7/276qT5o1QR2TMpF9hw98gVN6S4vx10wNvu3vo+QwdoFp4pB/DAAvww5Uzj2NTAjpR/A8YvqOXLKK+ICmZ8tPma3vnJUlykjlZyMDN0= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: `fork` implements copy on write (COW) by making pages readonly in child and parent both. ptep_set_wrprotect and pte_wrprotect clears _PAGE_WRITE in PTE. Assumption is that page is readable and on fault copy on write happens. To implement COW on shadow stack pages, clearing up W bit makes them XWR = 000. This will result in wrong PTE setting which says no perms but V=1 and PFN field pointing to final page. Instead desired behavior is to turn it into a readable page, take an access (load/store) fault on sspush/sspop (shadow stack) and then perform COW on such pages. This way regular reads would still be allowed and not lead to COW maintaining current behavior of COW on non-shadow stack but writeable memory. On the other hand it doesn't interfere with existing COW for read-write memory. Assumption is always that _PAGE_READ must have been set and thus setting _PAGE_READ is harmless. Signed-off-by: Deepak Gupta Alexandre Ghiti --- arch/riscv/include/asm/pgtable.h | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h index 7963ab11d924..fdab7d74437d 100644 --- a/arch/riscv/include/asm/pgtable.h +++ b/arch/riscv/include/asm/pgtable.h @@ -411,7 +411,7 @@ static inline int pte_devmap(pte_t pte) static inline pte_t pte_wrprotect(pte_t pte) { - return __pte(pte_val(pte) & ~(_PAGE_WRITE)); + return __pte((pte_val(pte) & ~(_PAGE_WRITE)) | (_PAGE_READ)); } /* static inline pte_t pte_mkread(pte_t pte) */ @@ -612,7 +612,15 @@ static inline pte_t ptep_get_and_clear(struct mm_struct *mm, static inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long address, pte_t *ptep) { - atomic_long_and(~(unsigned long)_PAGE_WRITE, (atomic_long_t *)ptep); + pte_t read_pte = READ_ONCE(*ptep); + /* + * ptep_set_wrprotect can be called for shadow stack ranges too. + * shadow stack memory is XWR = 010 and thus clearing _PAGE_WRITE will lead to + * encoding 000b which is wrong encoding with V = 1. This should lead to page fault + * but we dont want this wrong configuration to be set in page tables. + */ + atomic_long_set((atomic_long_t *)ptep, + ((pte_val(read_pte) & ~(unsigned long)_PAGE_WRITE) | _PAGE_READ)); } #define __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH From patchwork Tue Oct 29 23:44: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: 13855730 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 0BFF7D7494E for ; Tue, 29 Oct 2024 23:45:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E78B66B00A9; Tue, 29 Oct 2024 19:44:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E27E06B00AA; Tue, 29 Oct 2024 19:44:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C54226B00AB; Tue, 29 Oct 2024 19:44:55 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 982086B00A9 for ; Tue, 29 Oct 2024 19:44:55 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 602691A07EE for ; Tue, 29 Oct 2024 23:44:55 +0000 (UTC) X-FDA: 82728271290.03.3D6F1B2 Received: from mail-pf1-f177.google.com (mail-pf1-f177.google.com [209.85.210.177]) by imf13.hostedemail.com (Postfix) with ESMTP id 94D512001E for ; Tue, 29 Oct 2024 23:44:27 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=vENxF+iW; spf=pass (imf13.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.177 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730245335; 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=WTy3jgo4L5ghf6CNp6bUsFhPOXP78WxPgtMQHKh4Cn4=; b=tEnemZpPIK1EOVxkvIIZD2ScjFOyj3BC+nFpJt6TEWJwk1Zywq0xbmpxouxmx7Zj1MDV/r nxnPKury103B9stqoRFxUMWBt+AHegKqWhcrQ7jXcMrZbKi3MCsMY5HI7ODOxMyS4eprrE GBFoWYlxoes7bzoYEjjDeiBGHfvIvj4= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730245335; a=rsa-sha256; cv=none; b=cENKMatkND7q/bW+pjSRwtHEtWISZALavKh/iPcOdzE9eVXG+stDUMvseXTzLDB9wTkCUg WDpF/LX8odI/XFl3CQuBAeqeiP82KltMaxzMzOD32OKAs3ElnO+5M59ujmqDW42tV7IAiM DLarbBGwWiQU0ySTyi6UwT8kg/v7u4Q= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=vENxF+iW; spf=pass (imf13.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.177 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pf1-f177.google.com with SMTP id d2e1a72fcca58-71e681bc315so285558b3a.0 for ; Tue, 29 Oct 2024 16:44:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1730245492; x=1730850292; 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=WTy3jgo4L5ghf6CNp6bUsFhPOXP78WxPgtMQHKh4Cn4=; b=vENxF+iWKE/vmc8PdzwOURNMjhFqk06snUjWRO/6b2235CGh55LG3vZKUeGymDwg4S yyVjnRbPUemkXbC2Gf61Y3KS6C1v1PpMDxNIkyXperO8KNQsUhqOCtgIUYVQ2YWD28Cp apTjYC9KrEVlwin06M9eJu4+a9dZCTHkPfAM1bmH770Xru9xE5+2dh5kCt+4qpFyRLqU PfMsVis9Ua3XEQCVm661xdLMrEkIZsmCYP4ZNhl46NPDAs8JbNtavRh99yu3bTew1oWk PfnlIWfpUoFUZOgimTL2Se1hKAOJbZp3dE5Nwk82ghwSCi5sjMVrTaKS9GlFIbc4HEHR Vz8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730245492; x=1730850292; 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=WTy3jgo4L5ghf6CNp6bUsFhPOXP78WxPgtMQHKh4Cn4=; b=OaBt++n5Wn+QR1Aajbqer0J9dxP2Ylb+dp7HA/BJ3q+LYQMVBF8ioe7B6cAuZkajNr 1gZ1r/zvKqNy8bddl+A0Y/+avQGzNi2/tpsrCj5+Pe+sm84f0ddziveZ7vRm2sOQUz+q 5tYaSoPnFauVoPPoPNfG0hQGBUr7UYgTuZB9Vr58J5vBIIPb+aF+33917qzugcKQVHfG G6tyVt7D+x0rGeoxit+bLyAROHSA9ZLWzGCUkfH9Q46FGdjq7L4TPq2OQoq5gRd/67kU /P0/F4bd22QVYWcE+X36LpGnqdhyajsotAVPtIwE7p1WQ0UC5BVBgG/SxwrTE3hdKsT9 h47Q== X-Forwarded-Encrypted: i=1; AJvYcCVXlQr00u8NsAjcaVT1N6tbyi0jI8s/lyGUWaP+BXr7gUCs/kiItEMrqzXSZ9hHwrleTf29VBg8Kw==@kvack.org X-Gm-Message-State: AOJu0YyZGYTLoe+Y2fHGGEl/98zccqgIStaluUlwpaG9PIkJsve+cZzZ Fq0gZB2SXJ2lkQ9s0onT1w3GomyubzuuN7BPgY4oa/ffBBxovfSBtuJX2FbasC8= X-Google-Smtp-Source: AGHT+IGHQOnVKyl9WNd/iu5rfPsmyEZbKQZ5k/k8VUqC0xQ35expKJ9db4FlQ44USVq+RzbAsl599g== X-Received: by 2002:a05:6a00:14d3:b0:71e:7f08:492c with SMTP id d2e1a72fcca58-72096b9a8c8mr5130679b3a.1.1730245492144; Tue, 29 Oct 2024 16:44:52 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72057921863sm8157643b3a.33.2024.10.29.16.44.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Oct 2024 16:44:51 -0700 (PDT) From: Deepak Gupta Date: Tue, 29 Oct 2024 16:44:14 -0700 Subject: [PATCH v7 14/32] riscv/mm: Implement map_shadow_stack() syscall MIME-Version: 1.0 Message-Id: <20241029-v5_user_cfi_series-v7-14-2727ce9936cb@rivosinc.com> References: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@rivosinc.com> In-Reply-To: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@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: 94D512001E X-Stat-Signature: cxqu3wi6mjqkx6cc8kou3j7oig7w5qpe X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1730245467-674413 X-HE-Meta: U2FsdGVkX18mYTF1Mol5Jgxz7e0F6wglw21VKyBVtdYAgCClj6l3Rv3W0/BDXGly9t8Uv+H+9k8BtgFNkcluko7QLQcbAgyrRRF7pdXYh1+Cq39r8f77XPjSs3s6Q+TgM/bYDccXDWkgpIPAF6Co4xIYu63T6yrd9hEI0kzXWcrLVT+nH1RwhPVwF/PvuDd7elKf5WY4ECcVS0r89HF4qKknH+tN6jw8QfyNb1JmpWq2IVDlLQdZ/jxxrwaQ7fUZhu2fp37BJ0xeh/8arlXRXE+tt8U9juLN6JLXnBYhDm5g4jwkNfnMao2zJoJ1tyMIDw1nnKwES45L1/iVFmHxMNlrB74wkcR2whupvwJ5IL98nQH94RK5XxB4NweskqYh7TmThmqvM3Ynrzy9qx3H0jUd+XGbi1hE4wFmtChlby3J/4x961H5g5pD6xRQ8PKQCoDQvErhHP10/znmQaMKgvjhpLIKzfcPHIgiGzKhyeRiDTLEDZIcWdP55E2xPMRmGe7nlOZfFfxmWvkTIAyDEkB3qBZDVDXldLjbEGrtzgFqu2TdFHVJXYUDQBtvkZfpEMmp44uUu3vCS8GQNp47F1m72zb32dN7ZSFV28bLZUXiQjOh1oj6xo1hg5zAH1QsyJtOeHYtJJgB9k5zxyp5OScd3KZJM2TmhB7QTRAmUGjRlBGhCirY6yZ6R7IzX6LXhZYS/FcRFRys0HSmGLdr6t+mlW/ZO5KtMkx6+U1UEYuQnlemk6c4LvJ8m/EkUpKCf2ehzwRzhoiYAY637W4z3O4P/qFFiONM5nTYYlxfnETM8zMjLwVn8GSOhwVGtYAO7CDYpHjxNDBt+9p2UoA47kifs61Vj3N0yiUNJfFUxlGBCJPdICHiuSDhqfJJcRVEDwRNcyQDcztyA7bn0Kp3oN1i4BBQzeHpDQijod2ZKpdu7qa9ubozKHEvRqcXJ5EaS9Gw3D8I7Zxt7mBLuob cb0i0NUg ZRp33WhsWyX0ZuxKhkP7zakIDTtJ4sRCWIPvHJWieBu1ns9JhUUc7qeiwtKH0lSBluxWHpUz57QnOrwTfYzD0khzPLT+/gikyqt2OLPnKtpalXasinOfFHQ9zlY4LdK4Ml0qmhS5zIx+0eaod9FHn6+utf7kRZUdeSFJhtlBU4bv2FiJJrqFskg3lsy5SNiehSeFg+C4e/KXTyl2gmRzVTgcuEGkayIp1bCrHsxeus9eIqdKWh/jfnc/8xU3dcMOiTsGaC4Dk2j9s5SYzjU7h3AK1y2LDpgkNwwAdhMP9YzsPFO97PTp8u3jQZcsxkChP3CuwqGoskMoa+5wVBZtS9UYiXeQvGuyLtAcjqDapHZiO8Jd+ALIvGRMY1b7qjfyccpAVFFJWPZGXMnlW4Z4Bw3GxBMdxc5OMv0JdGOaaUxeoKksBeOeJ84T81GR23KvRllcdTrTBGLG744H4qw6nLXjKZAEcJ1aS0h1JZiSOSVC9266GNJRY9IY10dJ/gx6k0d1m X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: As discussed extensively in the changelog for the addition of this syscall on x86 ("x86/shstk: Introduce map_shadow_stack syscall") the existing mmap() and madvise() syscalls do not map entirely well onto the security requirements for shadow stack memory since they lead to windows where memory is allocated but not yet protected or stacks which are not properly and safely initialised. Instead a new syscall map_shadow_stack() has been defined which allocates and initialises a shadow stack page. This patch implements this syscall for riscv. riscv doesn't require token to be setup by kernel because user mode can do that by itself. However to provide compatibility and portability with other architectues, user mode can specify token set flag. Signed-off-by: Deepak Gupta --- arch/riscv/kernel/Makefile | 2 + arch/riscv/kernel/usercfi.c | 145 ++++++++++++++++++++++++++++++++++++++++ include/uapi/asm-generic/mman.h | 4 ++ 3 files changed, 151 insertions(+) diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile index 7f88cc4931f5..eb2c94dd0a9d 100644 --- a/arch/riscv/kernel/Makefile +++ b/arch/riscv/kernel/Makefile @@ -117,3 +117,5 @@ obj-$(CONFIG_COMPAT) += compat_vdso/ obj-$(CONFIG_64BIT) += pi/ obj-$(CONFIG_ACPI) += acpi.o obj-$(CONFIG_ACPI_NUMA) += acpi_numa.o + +obj-$(CONFIG_RISCV_USER_CFI) += usercfi.o diff --git a/arch/riscv/kernel/usercfi.c b/arch/riscv/kernel/usercfi.c new file mode 100644 index 000000000000..96bb324abafb --- /dev/null +++ b/arch/riscv/kernel/usercfi.c @@ -0,0 +1,145 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2024 Rivos, Inc. + * Deepak Gupta + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define SHSTK_ENTRY_SIZE sizeof(void *) + +/* + * Writes on shadow stack can either be `sspush` or `ssamoswap`. `sspush` can happen + * implicitly on current shadow stack pointed to by CSR_SSP. `ssamoswap` takes pointer to + * shadow stack. To keep it simple, we plan to use `ssamoswap` to perform writes on shadow + * stack. + */ +static noinline unsigned long amo_user_shstk(unsigned long *addr, unsigned long val) +{ + /* + * Never expect -1 on shadow stack. Expect return addresses and zero + */ + unsigned long swap = -1; + + __enable_user_access(); + asm goto( + ".option push\n" + ".option arch, +zicfiss\n" + "1: ssamoswap.d %[swap], %[val], %[addr]\n" + _ASM_EXTABLE(1b, %l[fault]) + RISCV_ACQUIRE_BARRIER + ".option pop\n" + : [swap] "=r" (swap), [addr] "+A" (*addr) + : [val] "r" (val) + : "memory" + : fault + ); + __disable_user_access(); + return swap; +fault: + __disable_user_access(); + return -1; +} + +/* + * Create a restore token on the shadow stack. A token is always XLEN wide + * and aligned to XLEN. + */ +static int create_rstor_token(unsigned long ssp, unsigned long *token_addr) +{ + unsigned long addr; + + /* Token must be aligned */ + if (!IS_ALIGNED(ssp, SHSTK_ENTRY_SIZE)) + return -EINVAL; + + /* On RISC-V we're constructing token to be function of address itself */ + addr = ssp - SHSTK_ENTRY_SIZE; + + if (amo_user_shstk((unsigned long __user *)addr, (unsigned long) ssp) == -1) + return -EFAULT; + + if (token_addr) + *token_addr = addr; + + return 0; +} + +static unsigned long allocate_shadow_stack(unsigned long addr, unsigned long size, + unsigned long token_offset, + bool set_tok) +{ + int flags = MAP_ANONYMOUS | MAP_PRIVATE; + struct mm_struct *mm = current->mm; + unsigned long populate, tok_loc = 0; + + if (addr) + flags |= MAP_FIXED_NOREPLACE; + + mmap_write_lock(mm); + addr = do_mmap(NULL, addr, size, PROT_READ, flags, + VM_SHADOW_STACK | VM_WRITE, 0, &populate, NULL); + mmap_write_unlock(mm); + + if (!set_tok || IS_ERR_VALUE(addr)) + goto out; + + if (create_rstor_token(addr + token_offset, &tok_loc)) { + vm_munmap(addr, size); + return -EINVAL; + } + + addr = tok_loc; + +out: + return addr; +} + +SYSCALL_DEFINE3(map_shadow_stack, unsigned long, addr, unsigned long, size, unsigned int, flags) +{ + bool set_tok = flags & SHADOW_STACK_SET_TOKEN; + unsigned long aligned_size = 0; + + if (!cpu_supports_shadow_stack()) + return -EOPNOTSUPP; + + /* Anything other than set token should result in invalid param */ + if (flags & ~SHADOW_STACK_SET_TOKEN) + return -EINVAL; + + /* + * Unlike other architectures, on RISC-V, SSP pointer is held in CSR_SSP and is available + * CSR in all modes. CSR accesses are performed using 12bit index programmed in instruction + * itself. This provides static property on register programming and writes to CSR can't + * be unintentional from programmer's perspective. As long as programmer has guarded areas + * which perform writes to CSR_SSP properly, shadow stack pivoting is not possible. Since + * CSR_SSP is writeable by user mode, it itself can setup a shadow stack token subsequent + * to allocation. Although in order to provide portablity with other architecture (because + * `map_shadow_stack` is arch agnostic syscall), RISC-V will follow expectation of a token + * flag in flags and if provided in flags, setup a token at the base. + */ + + /* If there isn't space for a token */ + if (set_tok && size < SHSTK_ENTRY_SIZE) + return -ENOSPC; + + if (addr && (addr & (PAGE_SIZE - 1))) + return -EINVAL; + + aligned_size = PAGE_ALIGN(size); + if (aligned_size < size) + return -EOVERFLOW; + + return allocate_shadow_stack(addr, aligned_size, size, set_tok); +} diff --git a/include/uapi/asm-generic/mman.h b/include/uapi/asm-generic/mman.h index 57e8195d0b53..9cfb3c1e337d 100644 --- a/include/uapi/asm-generic/mman.h +++ b/include/uapi/asm-generic/mman.h @@ -19,4 +19,8 @@ #define MCL_FUTURE 2 /* lock all future mappings */ #define MCL_ONFAULT 4 /* lock all pages that are faulted in */ +/* Set up a restore token in the shadow stack */ +#define SHADOW_STACK_SET_TOKEN (1ULL << 0) +/* Set up a top of stack marker in the shadow stack */ +#define SHADOW_STACK_SET_MARKER (1ULL << 1) #endif /* __ASM_GENERIC_MMAN_H */ From patchwork Tue Oct 29 23:44:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13855731 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 3BF9ED7494D for ; Tue, 29 Oct 2024 23:45:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C2DE26B00AB; Tue, 29 Oct 2024 19:44:58 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BDB326B00AC; Tue, 29 Oct 2024 19:44:58 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A08D86B00AD; Tue, 29 Oct 2024 19:44:58 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 80B016B00AB for ; Tue, 29 Oct 2024 19:44:58 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 3ECBC80367 for ; Tue, 29 Oct 2024 23:44:58 +0000 (UTC) X-FDA: 82728272214.11.503072E Received: from mail-pf1-f177.google.com (mail-pf1-f177.google.com [209.85.210.177]) by imf24.hostedemail.com (Postfix) with ESMTP id B3457180002 for ; Tue, 29 Oct 2024 23:44:52 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="st/mlFIB"; spf=pass (imf24.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.177 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730245338; 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=hNRREKXIol+zn7NHo8wrO8lt9q1atoxXotkDizp4KWc=; b=T6wu0ia3Dl9Dc9XGl8D25OxvVM0dybvWPUGtGI92XFeNBSa5uT5R9ixEolQ8SZ751tCOSJ xJRzdQTsCXHUiEouvF4RJV5yCAh95hOM0f+OsSBgO00q8HCJ3QwFC0DjrdsrfYcoSwCJtG +gq5ut3WJL2tS0btUaMaqqyDmUlQ+LU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730245338; a=rsa-sha256; cv=none; b=xevt3heu4BmafhSwS3mBupooLjV5U6FnIL2NqsWp6osbK+Jo210xsTS9dfvucGj+06yj0c ymI4uhcXne6y+X4kJjTfKEmjJj76YiR24zfC++I9llXjyaZl/CQ/zbhc7RNFUz6AVHBNWt y6qiSq+D42XHfnu6aQP/S3+sQmJlbUQ= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="st/mlFIB"; spf=pass (imf24.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.177 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pf1-f177.google.com with SMTP id d2e1a72fcca58-71e8235f0b6so4598527b3a.3 for ; Tue, 29 Oct 2024 16:44:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1730245495; x=1730850295; 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=hNRREKXIol+zn7NHo8wrO8lt9q1atoxXotkDizp4KWc=; b=st/mlFIBUSyr9LjGiVXpwXUXiajC87ciCQ+gTOYEzC2FkZJsuo8gfy0UKbORFyRzq7 ir9WffnqVDXub3lPGBSxy2J1eM6KD3cEAXkgE4l4vCHFRjDTs9H7HC8VFp7p6dEi7kUm CNS0nwG1EWGMSEJlzqmvQ73PWQXASUj8ZQJR6cI0H0q3r4QqhRu3thquOBDB3mM6BSR4 yEPDU+08k4D91CY8alihAbhy6kat1dTRZNWElvhp1143sWhsH8hnEhGaLydj54HaGKT8 v8tD28tT5lqxUslibpOoeg9sYF6KOTkRSJdjM1TJ31+VDvdAVmIEm6ZZqJL9OkKyk0lm amhg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730245495; x=1730850295; 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=hNRREKXIol+zn7NHo8wrO8lt9q1atoxXotkDizp4KWc=; b=dTq3CqhcHUwE1qqadotG7+N4xLMqCVhaeh0rLOSIPPZ5wmZf9NXZtqtkWhxAh+VB1V tVseIgrkGoqHNhyu5jckNJJttbwHiOF7q28QHf+32htRFCCSisqSya/eGf5zF9viK6AR 0mvqk/bfOJL+FtlWMMzj03yC4R8fIf1d+jKhcTCABEe/3ks+RIFfVwMQ7LmjQfvHzAuF 3UqDggRaT570oTOSczKDS3IwYKQ2DoEarI//J+rY2n7BoCboVoGGSYlneeIenwidVapY 7K32C66UVTmULqI8XkkpKv7mKj1jsgHGo7hdecYgPCXmieHUtqNjl2kx8xYbuIIL+vMR i3yQ== X-Forwarded-Encrypted: i=1; AJvYcCV1kVefgIoA2bn92bD8T6eyfuqZigPfl801O74Pfrv/KPquD80dP8SkVNPDovUEWjmbgV9YE34TZg==@kvack.org X-Gm-Message-State: AOJu0YxDqhMa+UOqygHx9i4Yafkxt8+OSJbHh+ZEe6KFbwtGVBLHnNcO 617UIEbZwMIMxP4NAa72NlTdamsGkv4aYLte4UeVCc51z0rpBAldPW7LBpMERjs= X-Google-Smtp-Source: AGHT+IFML8Lb42dKqClxemrPSZcKrg8g9X461U+Y/Tl3xJP5MBi9Wc3gH0NfVIyPMGXIby79FMBbgQ== X-Received: by 2002:aa7:9385:0:b0:71e:7d52:fa8c with SMTP id d2e1a72fcca58-7206306f0f8mr15309521b3a.22.1730245495095; Tue, 29 Oct 2024 16:44:55 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72057921863sm8157643b3a.33.2024.10.29.16.44.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Oct 2024 16:44:54 -0700 (PDT) From: Deepak Gupta Date: Tue, 29 Oct 2024 16:44:15 -0700 Subject: [PATCH v7 15/32] riscv/shstk: If needed allocate a new shadow stack on clone MIME-Version: 1.0 Message-Id: <20241029-v5_user_cfi_series-v7-15-2727ce9936cb@rivosinc.com> References: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@rivosinc.com> In-Reply-To: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@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: B3457180002 X-Stat-Signature: ecii34saetop7o7tnszh845kikpdj5oo X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1730245492-472039 X-HE-Meta: U2FsdGVkX18CMq9NpWn+kNyrdj1MH+BvbMloowq+zQZaXRuQS7aGYGeA2KIUe2oAV3FVWowDv8S0iMstKz8kff7P3Dsx62InfDPVsKw5Iyr4CH08abmaEfeSwgtYmu1bTHchAs2cdvZcAniT6hdqjiKEo52uCiOxnKFWxKBMc42wySO0qOjV93Qj6LEVyr1J/7CTHm7/PP63RWVKUx5PK9KmMc9yX4l/3vD0aJ4MPecGUfK6Rvu/kHCFydOpbPUhcHkzBNX0V5vAXlojGsiUtmYZpDEPOgJFzzBYDRU908lDQ5CZ42Bxo14UWQ/ExIxts4JiD6kL/rx51wphE5v6dNcNlV/0nGdY7shQfSxhtFcGqK271HD3qA0x3J8AcfwtzJ34I4d+GYyFBEzJfX0VmtUvPg7zT+1/5xH9ULrqYnVLcbKz0e+LYnA8oFw1TdCTA8yn2DPQPFEFcPLGL47iY4lMf/4FZ2EAbgkHEgDBnic+KCFm81FRtZGCsIVZZckK9OshP9tEgEzA2gzuemzLw8KEemMgQBom9mRSMzl22XLwt0eECK1BTvMNt4v7fbAw09lYg7rFvrNHKyW8ZQ6K0seOPsLnYd4IXm1zQSHZLVbZOVSfDUYXb587iXHsa3k9Q3zQn2VOmtMUgMDpXGea7Pok7mIpKSJv6yMnmJm7sAHqLzJU3sN44aMftACLo6TNyi8xEgfsm0uc/2HPW/bytBPnss5sJCEshsc+tIpH52XyQiHS7fDBw+5+jR8746blKsGUjrlTmomPQrI2Tyeb/1aIMyTpdpEKzfgf5UOrfInefIsa24gZ/Jb7UaOGOuP5iGt+8wejTt+sclNpBEwKDCklD0tKb4lpvu0dk9Y1wHAMMCaqmu3OnZMsYOh1uQrUfSYS4hkpk2+BQ0BOjK7H2ey4U8oouvlH3RPlRlc19KSnMMQfEKEz/gebIvsy++48JsrYA+hrlf0y4dNXyp5 GMvKdmsK MYgngbRsLkHawSEkSStzIC+Wnp0AniHz1EMgSCSkSDtAtJvoSPo5+GewPU+2n0OT4LuJRC8aX0MjWF0xHSqSiN8jnf/hBXzYOAXD48wsZ/4rqNjOXnoDNKY4aN3/U7y9gys7WsjxMQToxTZ8H1qY6gOk+50VorLSzbn0y3mEip2hq46TjsQlHnxXYR/HbknL+QhT/ety2hDKjHWGhWvjNYAwDZtqZEk1Tzof1r3po3c9E3H3KjEmu2YpT8h6D0CkTKOXCeGEM9Hi1ZIsM7JB8y/8izEx2l1isCuZL3tiubzYb6xeaN3Z7zrDg63VL+hUkTnRa/bmuuf4U18igkw2HQIOBneo9tlzl19b5ZsU0x0pC/m7EaVh5YTnFTrgUmzHB6jXCNlFFM1YP9lPdY+Vs+2oIEBqF9cwYDFGYJwnze0BNHmKXldYh7eXfvcPYXv7o71RzDAL4gyXwN1sog9cna1c/OA== 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 7030837adc1a..d4432a46164c 100644 --- a/arch/riscv/include/asm/mmu_context.h +++ b/arch/riscv/include/asm/mmu_context.h @@ -35,6 +35,13 @@ static inline int init_new_context(struct task_struct *tsk, DECLARE_STATIC_KEY_FALSE(use_asid_allocator); +#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 e3142d8a6e28..632c621682f6 100644 --- a/arch/riscv/kernel/process.c +++ b/arch/riscv/kernel/process.c @@ -28,6 +28,7 @@ #include #include #include +#include #if defined(CONFIG_STACKPROTECTOR) && !defined(CONFIG_STACKPROTECTOR_PER_TASK) #include @@ -206,6 +207,7 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) unsigned long clone_flags = args->flags; unsigned long usp = args->stack; unsigned long tls = args->tls; + unsigned long ssp = 0; struct pt_regs *childregs = task_pt_regs(p); memset(&p->thread.s, 0, sizeof(p->thread.s)); @@ -220,11 +222,18 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) p->thread.s[0] = (unsigned long)args->fn; p->thread.s[1] = (unsigned long)args->fn_arg; } else { + /* allocate new shadow stack if needed. In case of CLONE_VM we have to */ + ssp = shstk_alloc_thread_stack(p, args); + if (IS_ERR_VALUE(ssp)) + return PTR_ERR((void *)ssp); + *childregs = *(current_pt_regs()); /* Turn off status.VS */ riscv_v_vstate_off(childregs); if (usp) /* User fork */ childregs->sp = usp; + /* if needed, set new ssp */ + ssp ? set_active_shstk(p, ssp) : 0; if (clone_flags & CLONE_SETTLS) childregs->tp = tls; childregs->a0 = 0; /* Return value of fork() */ diff --git a/arch/riscv/kernel/usercfi.c b/arch/riscv/kernel/usercfi.c index 96bb324abafb..6cd166b73316 100644 --- a/arch/riscv/kernel/usercfi.c +++ b/arch/riscv/kernel/usercfi.c @@ -19,6 +19,41 @@ #define SHSTK_ENTRY_SIZE sizeof(void *) +bool is_shstk_enabled(struct task_struct *task) +{ + return task->thread_info.user_cfi_state.ubcfi_en ? true : false; +} + +void set_shstk_base(struct task_struct *task, unsigned long shstk_addr, unsigned long size) +{ + task->thread_info.user_cfi_state.shdw_stk_base = shstk_addr; + task->thread_info.user_cfi_state.shdw_stk_size = size; +} + +unsigned long get_shstk_base(struct task_struct *task, unsigned long *size) +{ + if (size) + *size = task->thread_info.user_cfi_state.shdw_stk_size; + return task->thread_info.user_cfi_state.shdw_stk_base; +} + +void set_active_shstk(struct task_struct *task, unsigned long shstk_addr) +{ + task->thread_info.user_cfi_state.user_shdw_stk = shstk_addr; +} + +/* + * If size is 0, then to be compatible with regular stack we want it to be as big as + * regular stack. Else PAGE_ALIGN it and return back + */ +static unsigned long calc_shstk_size(unsigned long size) +{ + if (size) + return PAGE_ALIGN(size); + + return PAGE_ALIGN(min_t(unsigned long long, rlimit(RLIMIT_STACK), SZ_4G)); +} + /* * Writes on shadow stack can either be `sspush` or `ssamoswap`. `sspush` can happen * implicitly on current shadow stack pointed to by CSR_SSP. `ssamoswap` takes pointer to @@ -143,3 +178,89 @@ SYSCALL_DEFINE3(map_shadow_stack, unsigned long, addr, unsigned long, size, unsi return allocate_shadow_stack(addr, aligned_size, size, set_tok); } + +/* + * This gets called during clone/clone3/fork. And is needed to allocate a shadow stack for + * cases where CLONE_VM is specified and thus a different stack is specified by user. We + * thus need a separate shadow stack too. How does separate shadow stack is specified by + * user is still being debated. Once that's settled, remove this part of the comment. + * This function simply returns 0 if shadow stack are not supported or if separate shadow + * stack allocation is not needed (like in case of !CLONE_VM) + */ +unsigned long shstk_alloc_thread_stack(struct task_struct *tsk, + const struct kernel_clone_args *args) +{ + unsigned long addr, size; + + /* If shadow stack is not supported, return 0 */ + if (!cpu_supports_shadow_stack()) + return 0; + + /* + * If shadow stack is not enabled on the new thread, skip any + * switch to a new shadow stack. + */ + if (!is_shstk_enabled(tsk)) + return 0; + + /* + * For CLONE_VFORK the child will share the parents shadow stack. + * Set base = 0 and size = 0, this is special means to track this state + * so the freeing logic run for child knows to leave it alone. + */ + if (args->flags & CLONE_VFORK) { + set_shstk_base(tsk, 0, 0); + return 0; + } + + /* + * For !CLONE_VM the child will use a copy of the parents shadow + * stack. + */ + if (!(args->flags & CLONE_VM)) + return 0; + + /* + * reaching here means, CLONE_VM was specified and thus a separate shadow + * stack is needed for new cloned thread. Note: below allocation is happening + * using current mm. + */ + size = calc_shstk_size(args->stack_size); + addr = allocate_shadow_stack(0, size, 0, false); + if (IS_ERR_VALUE(addr)) + return addr; + + set_shstk_base(tsk, addr, size); + + return addr + size; +} + +void shstk_release(struct task_struct *tsk) +{ + unsigned long base = 0, size = 0; + /* If shadow stack is not supported or not enabled, nothing to release */ + if (!cpu_supports_shadow_stack() || + !is_shstk_enabled(tsk)) + return; + + /* + * When fork() with CLONE_VM fails, the child (tsk) already has a + * shadow stack allocated, and exit_thread() calls this function to + * free it. In this case the parent (current) and the child share + * the same mm struct. Move forward only when they're same. + */ + if (!tsk->mm || tsk->mm != current->mm) + return; + + /* + * We know shadow stack is enabled but if base is NULL, then + * this task is not managing its own shadow stack (CLONE_VFORK). So + * skip freeing it. + */ + base = get_shstk_base(tsk, &size); + if (!base) + return; + + vm_munmap(base, size); + set_shstk_base(tsk, 0, 0); +} From patchwork Tue Oct 29 23:44:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13855732 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 5B41DD7494D for ; Tue, 29 Oct 2024 23:45:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 305506B00AD; Tue, 29 Oct 2024 19:45:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2B71D6B00AE; Tue, 29 Oct 2024 19:45:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 108F66B00AF; Tue, 29 Oct 2024 19:45:02 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id D74886B00AD for ; Tue, 29 Oct 2024 19:45:01 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 86291AC2E5 for ; Tue, 29 Oct 2024 23:45:01 +0000 (UTC) X-FDA: 82728272550.29.A69FC34 Received: from mail-pf1-f172.google.com (mail-pf1-f172.google.com [209.85.210.172]) by imf14.hostedemail.com (Postfix) with ESMTP id 92086100014 for ; Tue, 29 Oct 2024 23:44:31 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="0Uc8KU/y"; spf=pass (imf14.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.172 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730245341; 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=MX17XgayDE8GvInbCjC/4NqKFQNSI1sjKyctPVZ+mGg=; b=IfRJE2U+eXoA6qf+fRQgr+peQc6f5aKwu2t9AEYbZD9wAlOmvEbEQzK7SNfjF0OrerpZy+ UMXbH9FxJ2yfUvGobTyEgzAsHdJwPpmYVqOmouFvlsWZGn3Zo1HVsmp8kqHExNbehgYHqA vrotv1Jml6sUkh2k7d5B/cCP9j9qbcI= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730245341; a=rsa-sha256; cv=none; b=f3wSHatsgnzv0dkWOo1WT1v/8ppGR5pu3Kq41KDlSUVD84TeqwPGpHpp6YEKsaAztqeuNS vELBwriIRK5I1iPZUHOgXbjK54SYUe59wld6THihuvxsAgS1T87E+iAhOeJgxNTZ3bNRG5 n4mXWWoWAnFhlkE4K2mT6JajuUsuiZA= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="0Uc8KU/y"; spf=pass (imf14.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.172 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pf1-f172.google.com with SMTP id d2e1a72fcca58-71e79f73aaeso4229402b3a.3 for ; Tue, 29 Oct 2024 16:44:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1730245498; x=1730850298; 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=MX17XgayDE8GvInbCjC/4NqKFQNSI1sjKyctPVZ+mGg=; b=0Uc8KU/yppNzOSpgNXbE+4+A5qNccQwlGs47m3JPxjvjb334sPF9nr8r82P0XN1ujV Uz0ZUWw8RvyoQPlKF5nI5zp0iWUX10JNKEggST4YNYsPEsc/Dag7srcqB5mhnWs3CWqs 5v4mNqniCZiEFAqydIAbKkfegAmC16a4Uqrfr3dTxaG6YbTUiFMMa5DclGMZlYHMOR8e 2jZuWc3WRA8OBrptxirozh8piW3BN4qgtmoL4pY2GN/yRJaAN8/lubQ9FC4NrpX6B2t0 PLF6Nwtd7Xk6ockqW70bTYOHv218dbWy6nU+SRJBxRO67QOxjllyw0EIuSwuHNn/es7I TR5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730245498; x=1730850298; 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=MX17XgayDE8GvInbCjC/4NqKFQNSI1sjKyctPVZ+mGg=; b=gL/XAHQ2R729OdXPAA2HLtbAs94T1a3E6EKkrok4kcVJFs5vGHzvxCXxgtYIdIKSbx SYqLrKsJMMnTQT9kJl20Ijj/b8QbElSuyWwb4D1dMD9gjL1KX20gpaAkn3Lz0+zhmA8B bLx4aMUnMG2rTiLBkQm+njFKuhrGBcQrZ5msJTVpp73z3WyAkKz+gbeZ39BGPe6uRqf7 y6RMGIEkJnuaNK7qWyuJ0AidzHgNFvuGu664tbgXyv8zCEi6LPiuE1rhJWsrkFRUIk79 /QJblAt750wyLv+jyRNL+N69FXUOG+6/aXCIcz6mcEJ9bdNef3qPxxyy4l7wo6qncTdb 6MDg== X-Forwarded-Encrypted: i=1; AJvYcCWQfjLm8oHgNGRiD7/QVgxFf1cKgnvhmlFaT4K94fL0TREDKqXVal9Ob05VD3cEUt6ZNBF8tG+Yaw==@kvack.org X-Gm-Message-State: AOJu0YyaxxewrJZSGQr4s6XbNBXUnvu2oJYc7YF3ddk5k8qljpAXEQ/f z2tr3OaPh1YORw8ivR4vZXEeDvjtP0fi9OmQnM/KX3cYIxtFF/ve5zuT3+LxJno= X-Google-Smtp-Source: AGHT+IExoxYnLt1GvbT6MZVWFjKFOSbr7AEUolTL6dzHWvO6oME8gZh14LO3LJsC9IhYy2Z5JyMh8A== X-Received: by 2002:a05:6a00:4610:b0:71e:4fe4:282e with SMTP id d2e1a72fcca58-72063096385mr19719551b3a.28.1730245498419; Tue, 29 Oct 2024 16:44:58 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72057921863sm8157643b3a.33.2024.10.29.16.44.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Oct 2024 16:44:57 -0700 (PDT) From: Deepak Gupta Date: Tue, 29 Oct 2024 16:44:16 -0700 Subject: [PATCH v7 16/32] prctl: arch-agnostic prctl for shadow stack MIME-Version: 1.0 Message-Id: <20241029-v5_user_cfi_series-v7-16-2727ce9936cb@rivosinc.com> References: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@rivosinc.com> In-Reply-To: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@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: 92086100014 X-Stat-Signature: u3txu7ze7maho6io6wstuojbxt3xwnng X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1730245471-526809 X-HE-Meta: U2FsdGVkX19flp0C8GPc1AocCiILxslW2Dn1zYxaNXdBZCUyXu5l6zorr6ZSFb/p8Nh1ahteSC/nw0GYzKNKN2XNZRsfnuKXOYIwUqyJN87l+zDXEJ4Zdt17P2oUw+ccbKzZBYat4cbLbfCPXsd1vJ+kNaXxc0ixNfLS3eliboAeWseMQOEb+B4sQjBkUZcWWsJ8+1JuASXngcPeatSN/oIWiote6rDRGSDQnrmfSLgjHP4xBDI4MOKCjWPvJeJVKMmXV5e1NLF0Fzh0rB+xa2SZ94fSUhVjtRxASfoc4cd3llhhHeaZ3U6eZez3AwELJDFBzF6thjVAX7pmQS7hyI3w0PncVOMBC9PX2pn5n0vD2NO3V21AK4heM5x5lWqJhpTPYdfsxf5TIRc7L4fyuwC5SO9sZxZLipRTfc/D19T90AEtC6y5HHmmAl3W2z4STw+SPt35svNnAKrXtt5MuC2diSefvtYoTq4YeaesdCJszIPvXaUW6HGvHvKG3TZTGKXQC8Rwym5ZXOwyXcUWhORHdYCaoHog4frAU58r8cCu3eO/MBNyQBfSutwoykSfcazF/4uoNnS/EGGZJIG7xSA5LOS+EGOmEaCwxctY1J5BmcyC3vpGQILss1ha0/gxJfEmXiCSE0BG83WGmhED+L4zR5PwfRNZQ0kBDAHeIva99juCkCKJunUvQLZORlIHfQF0EqySR8MzcIhPgX3/nePQHylheUhkx5rh2VX7o5YZe1oC9DjA7UzY1P7+jQ/wSCq2fEc7hdnmfE8Ill5YPTAzlUgoDDC59k0LB6TQnkb6OJUR0Q9vey0wR3A+JE3NzPw4wCgxdsuwrFJK2qL9QNwTWmxXJYIi3PnsI75RoBgI1EUknpgPv7eKhrjxS6kHKfi08Z4zFxxvLp+NMdbzsNXiK5gewqEXGZBcVv/LGMoLwj3LC/cZgNXb5+8tcawZkA71nJ2AdpxbuNLcRjg RtP2sZcX G33jRe1uv+x0MyDYjAMq+Ia9O6OixZ9R4Gl0DWlfMMZGs6ZsJi0zYtDnZVFXb9Ioqwd+pwBgEiecFkHmQmc56LIVZafuRba9ha5lwbXUAcEdx4JuwMLjrOe5B1Syw9SOThadvYEN1srDJdtk0QmUHScyhl0T8o2LTzx5uAX/OfdDCAO+c2xFLvaTwI75G0hVq58YVyq7yWRZjGMKX7O+zlbEgHfDDaK3G8nZdw+e7+8+rmfYbW/7NY2msZqLZR8WTw7y075tr0FJb1YK6bM86k2EL4KD6cwyBjD1FL0rS7AB7jcEPSEBeOkEECRU9WkfOKyDhko2+ZmJjMoVUxT31EPRhxRk/gN4LqqxNzbRaSbUl4YBtg87HTMoQL3tWp15haguyQuvi260FHnZMzVmjtvLZ7YZiZz6eD/+2T2pVciSAjAI6lK8fxfv9XNYIaOmuWzyK/5u+bavH9qTiG1Yzpcvi+zBnHeF7kGEEqot+7bpl7RA= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Mark Brown Three architectures (x86, aarch64, riscv) have announced support for shadow stacks with fairly similar functionality. While x86 is using arch_prctl() to control the functionality neither arm64 nor riscv uses that interface so this patch adds arch-agnostic prctl() support to get and set status of shadow stacks and lock the current configuration to prevent further changes, with support for turning on and off individual subfeatures so applications can limit their exposure to features that they do not need. The features are: - PR_SHADOW_STACK_ENABLE: Tracking and enforcement of shadow stacks, including allocation of a shadow stack if one is not already allocated. - PR_SHADOW_STACK_WRITE: Writes to specific addresses in the shadow stack. - PR_SHADOW_STACK_PUSH: Push additional values onto the shadow stack. - PR_SHADOW_STACK_DISABLE: Allow to disable shadow stack. Note once locked, disable must fail. These features are expected to be inherited by new threads and cleared on exec(), unknown features should be rejected for enable but accepted for locking (in order to allow for future proofing). This is based on a patch originally written by Deepak Gupta but later modified by Mark Brown for arm's GCS patch series. Signed-off-by: Mark Brown Signed-off-by: Deepak Gupta --- include/linux/mm.h | 3 +++ include/uapi/linux/prctl.h | 21 +++++++++++++++++++++ kernel/sys.c | 30 ++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+) diff --git a/include/linux/mm.h b/include/linux/mm.h index 57533b9cae95..54e2b3f1cc30 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -4146,6 +4146,9 @@ static inline bool pfn_is_unaccepted_memory(unsigned long pfn) { return range_contains_unaccepted_memory(pfn << PAGE_SHIFT, PAGE_SIZE); } +int arch_get_shadow_stack_status(struct task_struct *t, unsigned long __user *status); +int arch_set_shadow_stack_status(struct task_struct *t, unsigned long status); +int arch_lock_shadow_stack_status(struct task_struct *t, unsigned long status); void vma_pgtable_walk_begin(struct vm_area_struct *vma); void vma_pgtable_walk_end(struct vm_area_struct *vma); diff --git a/include/uapi/linux/prctl.h b/include/uapi/linux/prctl.h index 35791791a879..b8d7b6361754 100644 --- a/include/uapi/linux/prctl.h +++ b/include/uapi/linux/prctl.h @@ -327,5 +327,26 @@ struct prctl_mm_map { # define PR_PPC_DEXCR_CTRL_SET_ONEXEC 0x8 /* Set the aspect on exec */ # define PR_PPC_DEXCR_CTRL_CLEAR_ONEXEC 0x10 /* Clear the aspect on exec */ # define PR_PPC_DEXCR_CTRL_MASK 0x1f +/* + * Get the current shadow stack configuration for the current thread, + * this will be the value configured via PR_SET_SHADOW_STACK_STATUS. + */ +#define PR_GET_SHADOW_STACK_STATUS 74 + +/* + * Set the current shadow stack configuration. Enabling the shadow + * stack will cause a shadow stack to be allocated for the thread. + */ +#define PR_SET_SHADOW_STACK_STATUS 75 +# define PR_SHADOW_STACK_ENABLE (1UL << 0) +# define PR_SHADOW_STACK_WRITE (1UL << 1) +# define PR_SHADOW_STACK_PUSH (1UL << 2) + +/* + * Prevent further changes to the specified shadow stack + * configuration. All bits may be locked via this call, including + * undefined bits. + */ +#define PR_LOCK_SHADOW_STACK_STATUS 76 #endif /* _LINUX_PRCTL_H */ diff --git a/kernel/sys.c b/kernel/sys.c index 4da31f28fda8..3d38a9c7c5c9 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -2324,6 +2324,21 @@ int __weak arch_prctl_spec_ctrl_set(struct task_struct *t, unsigned long which, return -EINVAL; } +int __weak arch_get_shadow_stack_status(struct task_struct *t, unsigned long __user *status) +{ + return -EINVAL; +} + +int __weak arch_set_shadow_stack_status(struct task_struct *t, unsigned long status) +{ + return -EINVAL; +} + +int __weak arch_lock_shadow_stack_status(struct task_struct *t, unsigned long status) +{ + return -EINVAL; +} + #define PR_IO_FLUSHER (PF_MEMALLOC_NOIO | PF_LOCAL_THROTTLE) #ifdef CONFIG_ANON_VMA_NAME @@ -2784,6 +2799,21 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3, case PR_RISCV_SET_ICACHE_FLUSH_CTX: error = RISCV_SET_ICACHE_FLUSH_CTX(arg2, arg3); break; + case PR_GET_SHADOW_STACK_STATUS: + if (arg3 || arg4 || arg5) + return -EINVAL; + error = arch_get_shadow_stack_status(me, (unsigned long __user *) arg2); + break; + case PR_SET_SHADOW_STACK_STATUS: + if (arg3 || arg4 || arg5) + return -EINVAL; + error = arch_set_shadow_stack_status(me, arg2); + break; + case PR_LOCK_SHADOW_STACK_STATUS: + if (arg3 || arg4 || arg5) + return -EINVAL; + error = arch_lock_shadow_stack_status(me, arg2); + break; default: error = -EINVAL; break; From patchwork Tue Oct 29 23:44:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13855733 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 976EBD7494F for ; Tue, 29 Oct 2024 23:45:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 80D946B00B0; Tue, 29 Oct 2024 19:45:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7BF976B00B1; Tue, 29 Oct 2024 19:45:04 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6112C6B00B2; Tue, 29 Oct 2024 19:45:04 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 3D7876B00B0 for ; Tue, 29 Oct 2024 19:45:04 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id E8AFA407BA for ; Tue, 29 Oct 2024 23:45:03 +0000 (UTC) X-FDA: 82728272130.26.6CC104C Received: from mail-pg1-f178.google.com (mail-pg1-f178.google.com [209.85.215.178]) by imf25.hostedemail.com (Postfix) with ESMTP id 15C4EA001A for ; Tue, 29 Oct 2024 23:44:43 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=cB0DNC7r; spf=pass (imf25.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=1730245343; 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=GnRpg73oVkF58mc5nN4qjvbtKg9UllFNYh9JE289A1s=; b=aSILdQU7dwF2OFeid/QHWrZsMte3fnPYbV9J6bxQ4NazlYMHclQdRMFlKkVzXa975mR11y 037rCNGb2xPF9H5noPkb5KMf6eqLOEtN/8nzkONeq/8UWF86SpMXlSRMZ8V7nromBcAi8u hE81lwBgrFSdWf9V7k8Pu06FX6sRLDk= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730245343; a=rsa-sha256; cv=none; b=57+yQu66aLY8JgTVTPp+ZPkPZ65pRXnwHccFn12F6ExgPc10x66eIQgpFmUzLQ8CaDZ0is qPZKyDJGNBJtrERUO0lKPPMLVD4S6XfJYf9y8aCm6033CC7Iefp55jL8/hncVpMWe4ucBI nqSoeBiIBdaL/gRXwObo5lGxQeWF2cA= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=cB0DNC7r; spf=pass (imf25.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.178 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pg1-f178.google.com with SMTP id 41be03b00d2f7-7ea76a12c32so4653602a12.1 for ; Tue, 29 Oct 2024 16:45:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1730245501; x=1730850301; 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=GnRpg73oVkF58mc5nN4qjvbtKg9UllFNYh9JE289A1s=; b=cB0DNC7rF7D3+/jpFB0MQpbxx5JED3YP9CQZu5KqCBY5BiZctOTCiCDeN3uimHwJUl sjfE7qIs1MsQrp/jTjzcCxPEKaLBnIf4HzGGyR+z+U7+QLoA6atvj6+Dxnie5aPqbuCU Ld06ryU7uZ68xjpU3za2K8Na9JzAFSFrV1sIC5chXdrpaWDrwP+NEhAheyUYsdRgJ/7b VpWgDonwLeTTwAQI1waBDq47138rKbDolsVXueiTXJIGBYjqTJr2bvj7WJ3HrBwHwdT5 m3v7tdkc9ZqGZVcclE84N/OV2c0nOUCCSM562lOb4l8g5gn9MzyAi5ttKZKuZH7TWgfA G0gg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730245501; x=1730850301; 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=GnRpg73oVkF58mc5nN4qjvbtKg9UllFNYh9JE289A1s=; b=R18b9Zs6A+Wd203vFm60yATiZD0FGtRXw01Um71Kx8fEHbogYJ3tBBSHGO4pIy8GMP Twvr78JOGsH0+cfutfWl8e6ZVbCOmZVpBCi58oSNrSdZ00oCEgpiogJaMaokz0ix66Xv T8gcawisi/pB0cAZVUlLGsJ9ong1hMEPYdAgavsf8f0XR6NymysIr1nQOr33ZkGg7vOF qgl8A//PdC+aRgcXHPoy9bZuXKAUaME4oj/Hz2WY/WAcWbQSDhqCEnpnh0MAWhBH/ZQ4 6OrGCEiVYG3Eu69rIKbIy7qsLtwD9qmHrS2bdkJz6ueNVmlCClXXCcIn6hvLI8krNOBx MYVg== X-Forwarded-Encrypted: i=1; AJvYcCW39a2EDIgmdBUyUZC4O9reKzlOUQ4iaAsoPvoMI07GO432+mRNWOEDRH1ku10k+boSPMs+eQ6JKw==@kvack.org X-Gm-Message-State: AOJu0Yza5osQX3JWwXP3B8Wbz7P9Msm1fJp/H9yrA9nJ2YEtVwDksgNs pN+pPA6c4t7fYGV5p2PxGkJ2LD1naEk/scHnlJMAETmPRfeB+L5phSV0ioI+nrA= X-Google-Smtp-Source: AGHT+IGC3Qid8/qIQwFQKJjq9xjWWIqVoJYycU5kLlngrCggKnSEPnv8BOJ9Ejv3TGwjaCoV88BMQg== X-Received: by 2002:a05:6a20:e30b:b0:1d9:2a76:e245 with SMTP id adf61e73a8af0-1d9a83cabf0mr17942587637.16.1730245500871; Tue, 29 Oct 2024 16:45:00 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72057921863sm8157643b3a.33.2024.10.29.16.44.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Oct 2024 16:45:00 -0700 (PDT) From: Deepak Gupta Date: Tue, 29 Oct 2024 16:44:17 -0700 Subject: [PATCH v7 17/32] prctl: arch-agnostic prctl for indirect branch tracking MIME-Version: 1.0 Message-Id: <20241029-v5_user_cfi_series-v7-17-2727ce9936cb@rivosinc.com> References: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@rivosinc.com> In-Reply-To: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@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: 15C4EA001A X-Stat-Signature: uccismq5hf1hdddh9jqutjws9ypfmx88 X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1730245483-487854 X-HE-Meta: U2FsdGVkX1+xdIsYh8m3zxh9X/zCBWHrQ/1wgdd+mFyZhhMouoKcyctvwKl2gw2nEIbfyTpPsAT2BoUn1/hrm8KSeNJ1Q2eMfLFJzcNHaECBDzzRQO911WRH3wORji3S9gttMtQEM/1rT+oTBNyNaOh9FdLnOb0GDYbLAfOsLtO8az2OedA9MZFVeLCgkT8NQ7Wrb9Scaj/FrcVOEKMg8Ef0GytDLl5PKs6G1zyq6cMjHTKSpaRGxA0W/v5HtYSPislfmyXM0tsmRAgRLnoYC+Bis2wWLo5Y03Un2nrOp+RcVwDpOCQeQZVnp3ebFe/+ACOEvPbiorL5vsQUtg67Sw+FwxfDA0HaS/GwnhWTvqJqRosTczkWv+zxaLEDJw3bdhUTevm4zUPBx6nYTUISeCzlqUgY4OqLtg9UGUMvNn4V8ExpPNNNPtIN20wWlrfq5D0bBx5SUtR48aJ+Y2DT/1e5+2f9goacLSKZuNUuQSca04F8XX9Zp/X3NpQg5tlrmfqRwv+5HLGMUPa99lBp41glOMwcoLjnCVo+nejMeBH1vL4/ysapGcGm9RBgqfIzTfcr1TPwWqMWTpHvHO9rRN2RfECUWUSzxXmAeQyLJoTAtjGDRJbQSJe6TksjHKiBvJ3zg0Oj4ttGq8gSwTa/ZBLt25ctJhyXrBoL5EqUbCz58R2obvghUYw7rcsDA2sFQvC+gZuN/iws6JbKRHiz/mkICG3P0mETVTfcR0DoIKD4t75m8kzFaV1GY1xgR2c7fzY4ARRG6WlPTd03FQoOsJFMREVKbFuyDG1GQZd4qK5f4ZyPaZmfqPDbEalgIWXJxBoVQwJ3QfdHvZYilh5HTJJuISkeLK+Q1IREivP/6EVwjI+wWWunKA66o6Rh0SxwOt9IPw0K6YufSNmdb431ajAow6a1yclku1QawPOiECqiSk1m8S7z2iEIEVUfZ6BblNoesXZ0hWl+F0VfD8I JBtwvGva /rsLlpEArHH8ohV4D9TCWb5l2bPghZzohP7MRUxl6gIKbVDj9BoA9fX/fV6m3tS3/TV6VxzWGJLKLStKnXxH7QRcDmpckajQC5pYGgbtaJREoM/Yu/IOaEhPO8iWbjMh6XECEfFp3BhGO1kO1QazgKXv2c0uQ0ypsinM968zJmcZgDnw3XYgulvICTou6y5S6ZJWBjZhWGu6Cjd3Cfn9LY1E4gD+Ew37tOOrKyZrd2pIfsts1iUYMWCzqXHEPYRTZyNAs5tvegTSWyVxSfmpw1OoJSo7OAtGLF/RdFKgbJlQwE4I3ZL45WR3Rc1McBz7h9LKmh5thUbHlSTH/GpUFy6IZEUXpdKcZDFvMsEMx1jaO19QpN4itFwbd8I9cQI8nPNkgZTfTQC3qvUwfPjOLyuhlaNCq/mWMxyh0QLHSzP6QkY7g6CC75cJJCxJQJvr/d6Das80RE2my94VJtiFqRos/k5doUeZyAt5ppyEuT0ZifXA= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Three architectures (x86, aarch64, riscv) have support for indirect branch tracking feature in a very similar fashion. On a very high level, indirect branch tracking is a CPU feature where CPU tracks branches which uses memory operand to perform control transfer in program. As part of this tracking on indirect branches, CPU goes in a state where it expects a landing pad instr on target and if not found then CPU raises some fault (architecture dependent) x86 landing pad instr - `ENDBRANCH` aarch64 landing pad instr - `BTI` riscv landing instr - `lpad` Given that three major arches have support for indirect branch tracking, This patch makes `prctl` for indirect branch tracking arch agnostic. To allow userspace to enable this feature for itself, following prtcls are defined: - PR_GET_INDIR_BR_LP_STATUS: Gets current configured status for indirect branch tracking. - PR_SET_INDIR_BR_LP_STATUS: Sets a configuration for indirect branch tracking. Following status options are allowed - PR_INDIR_BR_LP_ENABLE: Enables indirect branch tracking on user thread. - PR_INDIR_BR_LP_DISABLE; Disables indirect branch tracking on user thread. - PR_LOCK_INDIR_BR_LP_STATUS: Locks configured status for indirect branch tracking for user thread. Signed-off-by: Deepak Gupta Reviewed-by: Mark Brown --- include/linux/cpu.h | 4 ++++ include/uapi/linux/prctl.h | 27 +++++++++++++++++++++++++++ kernel/sys.c | 30 ++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) diff --git a/include/linux/cpu.h b/include/linux/cpu.h index bdcec1732445..eff56aae05d7 100644 --- a/include/linux/cpu.h +++ b/include/linux/cpu.h @@ -203,4 +203,8 @@ static inline bool cpu_mitigations_auto_nosmt(void) } #endif +int arch_get_indir_br_lp_status(struct task_struct *t, unsigned long __user *status); +int arch_set_indir_br_lp_status(struct task_struct *t, unsigned long status); +int arch_lock_indir_br_lp_status(struct task_struct *t, unsigned long status); + #endif /* _LINUX_CPU_H_ */ diff --git a/include/uapi/linux/prctl.h b/include/uapi/linux/prctl.h index b8d7b6361754..41ffb53490a4 100644 --- a/include/uapi/linux/prctl.h +++ b/include/uapi/linux/prctl.h @@ -349,4 +349,31 @@ struct prctl_mm_map { */ #define PR_LOCK_SHADOW_STACK_STATUS 76 +/* + * Get the current indirect branch tracking configuration for the current + * thread, this will be the value configured via PR_SET_INDIR_BR_LP_STATUS. + */ +#define PR_GET_INDIR_BR_LP_STATUS 77 + +/* + * Set the indirect branch tracking configuration. PR_INDIR_BR_LP_ENABLE will + * enable cpu feature for user thread, to track all indirect branches and ensure + * they land on arch defined landing pad instruction. + * x86 - If enabled, an indirect branch must land on `ENDBRANCH` instruction. + * arch64 - If enabled, an indirect branch must land on `BTI` instruction. + * riscv - If enabled, an indirect branch must land on `lpad` instruction. + * PR_INDIR_BR_LP_DISABLE will disable feature for user thread and indirect + * branches will no more be tracked by cpu to land on arch defined landing pad + * instruction. + */ +#define PR_SET_INDIR_BR_LP_STATUS 78 +# define PR_INDIR_BR_LP_ENABLE (1UL << 0) + +/* + * Prevent further changes to the specified indirect branch tracking + * configuration. All bits may be locked via this call, including + * undefined bits. + */ +#define PR_LOCK_INDIR_BR_LP_STATUS 79 + #endif /* _LINUX_PRCTL_H */ diff --git a/kernel/sys.c b/kernel/sys.c index 3d38a9c7c5c9..dafa31485584 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -2339,6 +2339,21 @@ int __weak arch_lock_shadow_stack_status(struct task_struct *t, unsigned long st return -EINVAL; } +int __weak arch_get_indir_br_lp_status(struct task_struct *t, unsigned long __user *status) +{ + return -EINVAL; +} + +int __weak arch_set_indir_br_lp_status(struct task_struct *t, unsigned long status) +{ + return -EINVAL; +} + +int __weak arch_lock_indir_br_lp_status(struct task_struct *t, unsigned long status) +{ + return -EINVAL; +} + #define PR_IO_FLUSHER (PF_MEMALLOC_NOIO | PF_LOCAL_THROTTLE) #ifdef CONFIG_ANON_VMA_NAME @@ -2814,6 +2829,21 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3, return -EINVAL; error = arch_lock_shadow_stack_status(me, arg2); break; + case PR_GET_INDIR_BR_LP_STATUS: + if (arg3 || arg4 || arg5) + return -EINVAL; + error = arch_get_indir_br_lp_status(me, (unsigned long __user *) arg2); + break; + case PR_SET_INDIR_BR_LP_STATUS: + if (arg3 || arg4 || arg5) + return -EINVAL; + error = arch_set_indir_br_lp_status(me, arg2); + break; + case PR_LOCK_INDIR_BR_LP_STATUS: + if (arg3 || arg4 || arg5) + return -EINVAL; + error = arch_lock_indir_br_lp_status(me, arg2); + break; default: error = -EINVAL; break; From patchwork Tue Oct 29 23:44:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13855734 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 C6AD5D7494E for ; Tue, 29 Oct 2024 23:45:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 352AF6B00B1; Tue, 29 Oct 2024 19:45:09 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 301DA6B00B8; Tue, 29 Oct 2024 19:45:09 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 12E686B00B7; Tue, 29 Oct 2024 19:45:09 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id DFA4E6B00B1 for ; Tue, 29 Oct 2024 19:45:06 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 8BE208046B for ; Tue, 29 Oct 2024 23:45:06 +0000 (UTC) X-FDA: 82728272424.15.057EA5C Received: from mail-pg1-f172.google.com (mail-pg1-f172.google.com [209.85.215.172]) by imf18.hostedemail.com (Postfix) with ESMTP id 8D1CE1C000D for ; Tue, 29 Oct 2024 23:44:53 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=kvgarZxq; spf=pass (imf18.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.172 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730245423; a=rsa-sha256; cv=none; b=AD4kWb7inwBo80XxRydTmQxCxXFKIx78mEK5dKPG99//IRYv31DD8NsPgo1kEdNyar/tyE F7ibmNnvQeO2mnv99YA3d6qk8JOqD3OXcV/XAe2lwF1UJuycvDtIDOASKcTmj87B+XsexJ JT+VcvXFBToG5iFMhcJCzmJRq2ww7Zc= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=kvgarZxq; spf=pass (imf18.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.172 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730245423; 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=6TVX3BLcn87m0lpyR2AYtrzuqtp3pcfnfCPWtAWnF6I=; b=xqcutPU8mYbK5aBT7KJANvvUScu0F+aVuDee8Q71YaxC9BJJYYWtRMfbLCXAQIcQstOrBA T3v9gf01apumD71cQQ5gXMLsTkrRnRsXD655H6HZPkqEPCpbFF2KReoxIbmb7nug1plTYn VXI8JlTzMf+DzOcq4UM805NW32mV6Hs= Received: by mail-pg1-f172.google.com with SMTP id 41be03b00d2f7-7eb0bc007edso3029923a12.3 for ; Tue, 29 Oct 2024 16:45:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1730245503; x=1730850303; 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=6TVX3BLcn87m0lpyR2AYtrzuqtp3pcfnfCPWtAWnF6I=; b=kvgarZxq6EF/g9BQPVDn1gMAEv8aBIb6fFKjMKepMpkoT1nANtJdplRxVkcyvF6L4z q4v28hnczaRKcSq15eLAEh7va1RdGzA1eWbW2nehlb7Zjh9uTq3HMljADpWhA199w8X8 QdULEg2fNQ8UYORk1dSBG2ocu4HjGbKeSVPR5kFM6WWmYM7WWKpPItstdVnDN9P8yBCo DIPNKrP5obLvBuXLTskqkGSHdps9rKCON6DTtN/xTBHFPtNnPMowu7fVHeh5QJuNKOQb myTqxcZGYvp5HeHLmAWfNnZwbwGNcIl60syMH37pIk7IqYDVmt87JFOV/7aHmxoE/3l1 +IKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730245503; x=1730850303; 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=6TVX3BLcn87m0lpyR2AYtrzuqtp3pcfnfCPWtAWnF6I=; b=iu/mTWgrvsDbl6WkqDl+EPlPpdS6w1rlEoOkj1mG8wpQoAx3qISlx4fN4wgNFYNQi1 nC+TFLKsAfa+XApvgHglG9kziRe/BDpg9D+1tLdqVYNTeo1eP+Kzf6MFSilGKvhBfZ/h GaIROci+oTgXngKnLVPagD3uQ61b+hfwEt2L4NT7Y1uCgmoD2XVYhhfkYKlCZ8fRyfsY SK8DW8/aevOeLrKyxvm6IKHl10w2ro+BCkN7OeA7HU6AS+QB3vqe+ppZwq2DLr7E5J1h bY3h2Wr1C3znEQoawj9oXdQ1c+Id6scinmXq9kp1ulcofExQkEHe59pLUvNpOql5ESrl V40g== X-Forwarded-Encrypted: i=1; AJvYcCVHU4Ni41ydORjJrR+7DA3Kad1+OwFjX9cMhvddeQLv8U/YSvJk/B9n/96w6W0OwFIESUhJv3Ehxg==@kvack.org X-Gm-Message-State: AOJu0YwUPaeWsfae/4tUc2Vuou4BncF8aYoEaf20aO8R6XzJmWCm4+O+ unR3/ZeVVfUUa8c6qTa3H+XVe76BmeBY+fq5kvEaCU0TRL3llQPxzzpE0KrKR/8= X-Google-Smtp-Source: AGHT+IFNg5o6rO3j8JKAbw0m86pKoaLglZmNpPu6e6Ng3OXhO9Lt2u49bgj7cspmJy/j4LUmb5jQTw== X-Received: by 2002:a05:6a21:1813:b0:1d6:d330:2417 with SMTP id adf61e73a8af0-1d9a84d9eb7mr16511530637.40.1730245503345; Tue, 29 Oct 2024 16:45:03 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72057921863sm8157643b3a.33.2024.10.29.16.45.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Oct 2024 16:45:02 -0700 (PDT) From: Deepak Gupta Date: Tue, 29 Oct 2024 16:44:18 -0700 Subject: [PATCH v7 18/32] riscv: Implements arch agnostic shadow stack prctls MIME-Version: 1.0 Message-Id: <20241029-v5_user_cfi_series-v7-18-2727ce9936cb@rivosinc.com> References: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@rivosinc.com> In-Reply-To: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@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: wij346zzm9bo4a6ebkt7qitr5mcpcwya X-Rspamd-Queue-Id: 8D1CE1C000D X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1730245493-265789 X-HE-Meta: U2FsdGVkX1+HsUQNx+zssQ5/FBmREchOAUeZzGrJeLWRNqTH0/7t7+SlRo18iABYeKD0cJxggFacmnvBKUL226Ddx8RQX3p9goT15g/7gyPGXfTqhkdvCEt9ocjSSsyBPTOW2l/wzDjcDp81XS4QRb92m/Dfzno1aaJJrwRGj+kOdMwvBXRhzS5vHZbXGBKA9Q5ZcOcju4tGX6dGg3WdcGrEsUw5hYMjVAyDVH1YM0aL6HobeanIRr/vofDlzH1rS5YQFdBMHY1b4BMupbZQelwBBZs65nhq2FTtkjKBZmpk3BYyQIBQujtdEi6TmgsLoahf4EuzWcPa+LQX8QVpX7Lt/Qlh+DdSONmc+qlihwgPkukj4ppVkbnj/tc9ZqqAEiMUUI+oz0RQ9zIPUChYLwV/fYUuhgL55xLOx/4I4x2c+hzeRKkxwKqHVnPt9hjSyY99B5JnszfJYui9C7UzO9j3kWWD9xnAMBEQnUjXNzh6pEl1j/TccSTBQjb8TA9YiVE9+ZGDwCDZTjtGl0U5mXiLzF0IcdnQ2rD81fFM1CaaMvE/bk5YiLmpdszGHD6l7+5PLOVA3MOWnfqHtaR0O4ZXeoJaS8+32e1EifMX4PFgakao/gHkBjmIgiwygMTmUVkSNb5XwBftKnNnUL2wQ1nYzt3I3pTq9/Y8BATuZI8xovPYln1boFgjIVs8rhJG0XAolyrnGca1vGV1FfQ0UsUO50V4hHVZOT7ey9NCfJPFSiba0emyKitUcfhvt5iu9sbDu4rGJFhPU66bYNEP6xDoScYZYsNZt8QWe+kmpqb+5g/ARE4ShiGu4MLxVvsbzA8wo9dKRSuG0+DinFLB6dABJx2cY+9qVIGYGPwdRZto7AYS0DbYv+WJWqswF31D/YhXlsyglPAxJ2mEQpSSUOsreQzDue1kF0JtgpsnjMuc0D8bo+Hs0iSDRHVL1SKhRnjUe5saVw9NeSA+qLD QNydbI+l pGze8IOJp8vRjb9jnVjBUVV4QD6BhyM34SVk8AinhA9egdAVJpITqjCBebFawT3WLOsvGt4ZiFfTtv67jyet2vQ4YpaLNUFlMfknk+xHMpUAGsI6njOEM6E/Js1xPwBTnz2CvlNmNTWH5fpDUlTW1mw2hpNVfvqBq+D6kWI8115/0mtNZnuSu5cd2sj2MNVB9FzjWqN08MCkofb+c1q/60liNoaiuMoDJKogEx0slhFaQ4dYedZmeVbDbSBCxsEIEttaLDAMhesuABhjCBKO2UNYJG1DBf0HqNEO71UYEjjwkJHyt1uf+iwjm/EzQBFuGULWFoOQzEiUdSxE5O7uJSa0OGz/3g3YaXKA0kzcyvNljZ+S7AuC2Mgi8G6da5nD731wNy+peOjpi88pDiXPhIQXCnhzjo0GyrnUS8Z93/lz58ehsw8c1NAaMRApCyESedGuVPFXVpoxj9eo= 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 632c621682f6..850128427936 100644 --- a/arch/riscv/kernel/process.c +++ b/arch/riscv/kernel/process.c @@ -152,6 +152,14 @@ void start_thread(struct pt_regs *regs, unsigned long pc, regs->epc = pc; regs->sp = sp; + /* + * clear shadow stack state on exec. + * libc will set it later via prctl. + */ + set_shstk_status(current, false); + set_shstk_base(current, 0, 0); + set_active_shstk(current, 0); + #ifdef CONFIG_64BIT regs->status &= ~SR_UXL; diff --git a/arch/riscv/kernel/usercfi.c b/arch/riscv/kernel/usercfi.c index 6cd166b73316..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 Tue Oct 29 23:44:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13855735 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 A8CE4D7494D for ; Tue, 29 Oct 2024 23:45:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 799BB6B00B6; Tue, 29 Oct 2024 19:45:09 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7476A6B00B7; Tue, 29 Oct 2024 19:45:09 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 589326B00B9; Tue, 29 Oct 2024 19:45:09 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 2C4A76B00B6 for ; Tue, 29 Oct 2024 19:45:09 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id E1B1D1C23C4 for ; Tue, 29 Oct 2024 23:45:08 +0000 (UTC) X-FDA: 82728272340.21.3EAD87E Received: from mail-pf1-f176.google.com (mail-pf1-f176.google.com [209.85.210.176]) by imf28.hostedemail.com (Postfix) with ESMTP id A3EDBC0014 for ; Tue, 29 Oct 2024 23:44:41 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=OLLs4O7q; spf=pass (imf28.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.176 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730245427; 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=KPxKGzxEi/0hgJefbaFEQhmtveG3vMbuHajNfmroGxQ=; b=KrSKinTyZPIWAp2H754qNGaZeNxr0ib7LkwjEtUidEbJrD1EUWN2384xiopiNP2B725jeJ 581Mdzipjwz9cnXNtWIKO6bMLC/C0lro5YnOu5M5AmFCo7LA1Zclm2b1/Y8sVlsQIz5s43 inrKoJMvh4dSyNT86cR98fLuooB5D2A= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=OLLs4O7q; spf=pass (imf28.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.176 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730245427; a=rsa-sha256; cv=none; b=iun9sI1evEqnCrOwnzALPMQY/J8ok691i76A5B2KHUnIWTK5khTJ6n4T2jg2F1uN9kktZO 9NLNDRUSAVnYR1jOTM1ucCHizA10Ri69CJvnX4gEbqhY+SwB+M8AN3t5DFLp5GjA9haa+2 ToibPzCknv391F3EngTzj0lHUInTllM= Received: by mail-pf1-f176.google.com with SMTP id d2e1a72fcca58-71e8235f0b6so4598659b3a.3 for ; Tue, 29 Oct 2024 16:45:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1730245506; x=1730850306; 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=KPxKGzxEi/0hgJefbaFEQhmtveG3vMbuHajNfmroGxQ=; b=OLLs4O7qVXWziyq11CCQTD+/0yM4zTiM6o+lidkGIQPtBygDt8GJqes8mmEqe6ymdk Cte4I3mWZ6HjR3BtwHduZgZd6mLEbCfRSuVNIBGBm8tiYL06ZnVLM7VpZWPoHCLRIhou 7xGsfj8dy392dw2M3dff4CyskGSoaqtzL8iItdEr9hh7M/R4RPWRSF58nZJ/9UYzVR7A 7rsWWd6xoY0m75jhny9P6u4xeyJvNvWUlovD3XPsRHvtG23KfVwMIIOAu1zG/BePv6ud FQ7C3c5lYL0a4vRtlrGUPpIHvORHcmT3+GVOLc/ItjGL5WIsO09mksfdncsGYtLPD6fL MQ/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730245506; x=1730850306; 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=KPxKGzxEi/0hgJefbaFEQhmtveG3vMbuHajNfmroGxQ=; b=cj0o2z52RYSy75M9Q3L1JpK6HCqILhGeb9UVQjU3AzagEqMVwdFtF7OO+84OWEql6p xGJnp59cqIPHupSNuURstb48IOoTf0p23qynlxUtNyRfHGnpCT8lC+fIoRnaXtYWDgw/ G4s90XqqbTL25Ho2YrJY3+1tUwrkb4MPlrORFHPDaPV8DpHGYION0AVzv87qbf5I7YQh kALHBMKWXvrVoyiXgkdFK1lbibWzbIuFZgG37sM4xYqDovgZd8kPr2WLyulze4CUekdE W0x53eLnKmuYuJ2s96Huc1eTPKvnOXf+mQsJB/wy5KmulIPwbE/Su6inLRrujhAE3vro iJuw== X-Forwarded-Encrypted: i=1; AJvYcCXCqRyc17703+clsNYuPogLLjAJ7/Kfdlhzp0nbn56QuZ4AA5X/hz0ELPuQikPdYUzlyeNnnPb5Pw==@kvack.org X-Gm-Message-State: AOJu0YzQTuVirtYI/6Vt2/fZgie9USdb8Pg9LReGm+7Vg37r5Tc+uUmF wq8fhWVJt/dISdB0bvStZ5J8syDkxSdZootpoJvuMmx3iU7/Wj+xLJ+X7QeRolg= X-Google-Smtp-Source: AGHT+IEuFmVYZ1jy/33kF2WStN/il8EUrK9psJOjehX1WLShpLPHHGb9nhbp4djvoS4ETi059uFvTw== X-Received: by 2002:a05:6a00:4610:b0:71e:7a56:3eaf with SMTP id d2e1a72fcca58-7206309376bmr19849204b3a.24.1730245505859; Tue, 29 Oct 2024 16:45:05 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72057921863sm8157643b3a.33.2024.10.29.16.45.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Oct 2024 16:45:05 -0700 (PDT) From: Deepak Gupta Date: Tue, 29 Oct 2024 16:44:19 -0700 Subject: [PATCH v7 19/32] riscv: Implements arch agnostic indirect branch tracking prctls MIME-Version: 1.0 Message-Id: <20241029-v5_user_cfi_series-v7-19-2727ce9936cb@rivosinc.com> References: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@rivosinc.com> In-Reply-To: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@rivosinc.com> To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Paul Walmsley , Palmer Dabbelt , Albert Ou , Conor Dooley , Rob Herring , Krzysztof Kozlowski , Arnd Bergmann , Christian Brauner , Peter Zijlstra , Oleg Nesterov , Eric Biederman , Kees Cook , Jonathan Corbet , Shuah Khan Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, alistair.francis@wdc.com, richard.henderson@linaro.org, jim.shu@sifive.com, andybnac@gmail.com, kito.cheng@sifive.com, charlie@rivosinc.com, atishp@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, alexghiti@rivosinc.com, samitolvanen@google.com, broonie@kernel.org, rick.p.edgecombe@intel.com, Deepak Gupta X-Mailer: b4 0.14.0 X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: A3EDBC0014 X-Stat-Signature: zrh77bdpeugsqpykgenabt7dh9dk33wc X-HE-Tag: 1730245481-45772 X-HE-Meta: U2FsdGVkX18Y/wmIW2BI4RkuHn80hBZSHSNSNGhgkvjK0gUPwAUapjFnQdrt2R7sBXJ7OomsOLOdYrx2JDmbUizrVIaXeQvhzi/GYVvVuxdnn3s63IAHpXwmTlpksKHYltHeFw3kVDPyq+KgUZB7clgO7oNJmoUHwr09jHJgzDSb8AeZGxclWW4g0IKyn3G97OlxTZMLo2gxuSWe8lX90skNhYQbAKEOm6xee9Tc/cLfwcchVuvLYfsQAMwzKWocIBBgQNSdp+ddiTsK7oZamvcKT/wZC3bNr1SQuhHBpVIxaFt/Pr6YiaXvAfpyq4miyLje8Dkx17mLKBBerWICIpv6sOBtFIoKRfoFCy/QA1I7+1o1odNkIRxnQ1Lv1IU54exfSRdY7eloCL+gB1HYilqElw//bY/uybCNAnh7pDEMxiIhLK4nufm8TuZHI+MKAKRHp2VXJOLhV/8QxhCA9I32lAYgUGgzOoB/qvdef/l4vBTScEZq0s7z3DRbjR74HaesElNrIEY3VsRI+fGsMSHhcwfFg0OpqGetU/Y2VRe+HjjbWMyzAXWt6C1XVoyqwk2R9GSJSJYA07sj7SVZsg+NfBL7jtiVaD8JXUe/Wa+Q/thKiBrYLLCSJm3BFqL90mTN7py4rhDDXVBErpgvNmGJOSMopIU4hg8EvTGg9KLJX2OM3KomDh95OxkWG5Aq8gjpTw+zcsbjNmIwIyO3ohZ2w1GooiHkwAsQjT9Q0nVOpMOxo517d74DWJQKeHBknv6HmaN5YLV0adLJ79n1Ru3Y7CSqiLJCyZ5Fjix1BrnD5sFO3ffjDVLLAkXdMZj4lG0iShjcTemdbabHy00y60agLdgcEXz9vKoOaYoyfs57pKi1tyCX5QmXZCzO+rmt4KcnQgDwvxDBCqP2MLTsCu3EwMBw6Da/ZCGyKl2SGSDrsqMztj0hOYUBdyMAoj5hwOEx/CcXT45v9iiHvsi 5kFE/hNs lw1DDDlrU3F2RhgKtJyHm4E58DZytCKrT60vmZ6JC0MMVORUAMw/j+1KYrvMN5yWqxP9BdwW+Z5vBDb8jzrY5SBjqvWnCXbl2Vcz7I9IHlkNH8yHQCfOQYWLgqFat36zrldRnKFt51lYzGDZUIYncbBOewo9URzl7ZaYSaszSBREcFzdwNqnuUYjxcV0vNRLbzU5ClUAUeGUf32dmlesRMvQ+PsdPYuRNktn0JO2n2mp8vlFOB8wQU3UfJ6+mEsMr2kb90J3xF8RLidHDWy+eNvh/yEMprjeXgMlEsQaLmYD/6cjiuXvHogQ4E42nIgoXoBkNkb0Hhi93Gg97mdgyYadMNBN+hLKKhzWYB3/BV50fM9Qld/jd5z8rjy5tUXaWmR3NY/dnvShmRSu87wMNecSaUXuz9mD1sbRbe+GmU0N44g/MrLwjctjcEwpYFF2tjnX3y2jcq7R4gXA= 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 850128427936..013bc3805afd 100644 --- a/arch/riscv/kernel/process.c +++ b/arch/riscv/kernel/process.c @@ -159,6 +159,11 @@ void start_thread(struct pt_regs *regs, unsigned long pc, set_shstk_status(current, false); set_shstk_base(current, 0, 0); set_active_shstk(current, 0); + /* + * disable indirect branch tracking on exec. + * libc will enable it later via prctl. + */ + set_indir_lp_status(current, false); #ifdef CONFIG_64BIT regs->status &= ~SR_UXL; diff --git a/arch/riscv/kernel/usercfi.c b/arch/riscv/kernel/usercfi.c index 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 Tue Oct 29 23:44:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13855736 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 5B33DD7494E for ; Tue, 29 Oct 2024 23:45:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 268FC6B00B7; Tue, 29 Oct 2024 19:45:12 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1F2166B00BA; Tue, 29 Oct 2024 19:45:12 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 047126B00BB; Tue, 29 Oct 2024 19:45:11 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id D58616B00B7 for ; Tue, 29 Oct 2024 19:45:11 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 9735F1607CF for ; Tue, 29 Oct 2024 23:45:11 +0000 (UTC) X-FDA: 82728272298.25.2641D11 Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.178]) by imf07.hostedemail.com (Postfix) with ESMTP id A60F84001A for ; Tue, 29 Oct 2024 23:44:34 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=LfzOa5a9; spf=pass (imf07.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.178 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730245454; 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=CTbsvOHRfAmS9fg/eDt9XRAZ9JnlumsLrbz7p3xsVRs=; b=NcyXTjgNPNkQD3ihsoJbOwuGIiaqRiyhnGYs19eSo1jd3g4qInU3NXB5vhbLLqjZgMLvDH 79S3eI7YBJs4sXWzZ8EtIiNCBTC25oaOswwBf2+R0HriN4CuNwrCIUyEnFa72SIJHN0ype Y/0cPf2h8F7oAfjuQZOUil1WgyEIKHk= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=LfzOa5a9; spf=pass (imf07.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.178 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730245454; a=rsa-sha256; cv=none; b=UVfvdXUSlutMaudKMtVWYaNr24Z61zyR3p+2AW9w04wDe4gaqBFjj4uiX5zERsEim6Jp23 JZYJjWMcbje9Oz6EGISNiwDhX/RBfYQyedKTyTrnTt77j5cNei2a4I0Cg9pjRTlkZtdIjV m9x+ZJ4Yd45homp6BljxsZXto2CEAUI= Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-71e3fce4a60so4358273b3a.0 for ; Tue, 29 Oct 2024 16:45:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1730245508; x=1730850308; 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=CTbsvOHRfAmS9fg/eDt9XRAZ9JnlumsLrbz7p3xsVRs=; b=LfzOa5a9CBbOa66mxHwOb8OPQF/3Nog4iKsE3pt2rv8MphzbbZuI0yKaBeyDwext/t 3L1SK1ol6cNsB1ouZSXHKYn3gs6VDYfhps/RN84idhN8QQrgcVvQ5PpmVkue+b4+k1Cl L3s4NhnA0xljObN1kEKLZ31tUic1jVcJOmNyxePhYOmJ6OcCyts+hIen1XePcSjAHwlG ji6pLsLzj8fU7Jf9jVk3Bpz4+UkJPNrOa/5RYV1p3KNz7ckKzZmRZrHqD/VBsxRKT40M 0RqbEnrQXr5nH1qpj1UGqcCGpc7gHl5LRTpa/qGlvkXl3W7DYnMEPsCvuh0QTy9606u7 5fXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730245508; x=1730850308; 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=CTbsvOHRfAmS9fg/eDt9XRAZ9JnlumsLrbz7p3xsVRs=; b=sBvy1DKD2kt7cAZLndDHmO4y4iQ/IwsaY15WadOsUTsEWKQ5mMoJIIlwwWnlg0JWv0 0q1aOs+5i0vaMNZZrQdvTCNXIJsqc9NHjD28fSeQYwFJkPcK5imNLTCv4Vewvqp12xj6 caaiilHbCdyDLKYwgfZoIbfESAgy1pPNpiMsViDW6eyEBfYAsV1pyKShY0zAdgJud0/V UL+NDf2NJQRcgDi3m0jwrxkJfDwDir/I9V0l2wuFX5l8pWfevaa7Nhne6N321hmc+VZU tZcWxsOcVnPxrmivaAwaipBrb8WjeuASMj7VMaIwsgmw4uUq1lKJOlFbuEIK/GfbWZHe raSA== X-Forwarded-Encrypted: i=1; AJvYcCVklszkbGDbJJv0V5iJkFZ5ad5SMJgSBAve2jYqPJ48vHtI3XAccuLOqGtRJ2+A+wNJUWHMFz5wrw==@kvack.org X-Gm-Message-State: AOJu0Yz4tR0wM3Stq+lzTobyigzh/cuCyvOLuIHWoGcku0X9tiUrdSf4 /F7cpDk+zgw3OHMqxcBilbZiLvCZwUs6L6YL4CxEecX5E4SQLFnbBkiTcKFLtHM= X-Google-Smtp-Source: AGHT+IGGDI9GHaayPj4Cs7Vha9dStUeFmWxlHEYmFUmhCBfJrJaPT/+XmpxPiVHSTz0RAhEYFmvh6w== X-Received: by 2002:a05:6a00:1892:b0:71e:4c20:75fc with SMTP id d2e1a72fcca58-72062f67d62mr19737734b3a.10.1730245508363; Tue, 29 Oct 2024 16:45:08 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72057921863sm8157643b3a.33.2024.10.29.16.45.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Oct 2024 16:45:07 -0700 (PDT) From: Deepak Gupta Date: Tue, 29 Oct 2024 16:44:20 -0700 Subject: [PATCH v7 20/32] riscv/traps: Introduce software check exception MIME-Version: 1.0 Message-Id: <20241029-v5_user_cfi_series-v7-20-2727ce9936cb@rivosinc.com> References: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@rivosinc.com> In-Reply-To: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@rivosinc.com> To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Paul Walmsley , Palmer Dabbelt , Albert Ou , Conor Dooley , Rob Herring , Krzysztof Kozlowski , Arnd Bergmann , Christian Brauner , Peter Zijlstra , Oleg Nesterov , Eric Biederman , Kees Cook , Jonathan Corbet , Shuah Khan Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, alistair.francis@wdc.com, richard.henderson@linaro.org, jim.shu@sifive.com, andybnac@gmail.com, kito.cheng@sifive.com, charlie@rivosinc.com, atishp@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, alexghiti@rivosinc.com, samitolvanen@google.com, broonie@kernel.org, rick.p.edgecombe@intel.com, Deepak Gupta X-Mailer: b4 0.14.0 X-Rspam-User: X-Stat-Signature: 6dthrpdfps4bmu39z9ti7jskpbf1b8e9 X-Rspamd-Queue-Id: A60F84001A X-Rspamd-Server: rspam11 X-HE-Tag: 1730245474-695444 X-HE-Meta: U2FsdGVkX1/XbxSZW8p/jfNCXW6tn3KTD6K0eXflmW7X6tyRcuD2TerHeSf1XpG4N8cxZr4H1ZxXza1aGklaHSkau8TakCjjQsnrZAO539nW1UN2a0+OtRu90L1Ulx3hJPQWu4eW/ASKkO9yTdqo7wSb4zwVVotYW6ODq2/uRe29NtNjN7CdDLSt9zafnWEpBtMoDyiAhLPCaygCmYQklLZX8NNPlxeEF68NImUJDHvPi2lh1CfSBKPOxA7xvtbHtJmtUhdfkRNwdvajWaQM2CAHSGMjs/T3kmFLa1X/v4vLzqoJcgPrYqb8owqjvCpUm0+yuMmUyM/0lXaixrtO/OlW9Q984rwS1cARzSpWdioUM5RZMK18/BlJ1s1KhvDdCVutmhB/cnOwdT+b/uBZCanj3RwwTv+dC0R45deMW+k5OmYL+jY2dPoeI0eRm/Ik9MCx62z5/S65OL28nkZvflyIEGjqHQKHhkpTHTGJd4hfx7PESzgbwXi0LRL9mIYQYBc1cEoyE+pdij36tOS3jqu2ke1OPIpS0Fs3BpFFazzD+DI0QQHkLp9pb7G4jIJwoHDlUa1gKcTKdvHCIoTolUtjK/QeJjOrdYzMdOaAjWxJJr+0GNiZf2PVqhSovoWjvhSQ5mfVTm8JxjiN7NtnTKhDbGzU2s5OLfS+aZ5lFe3mhJKP05VevVVTkiawFltcFfF/FsAtsJkmMkv/ChBeXtzFFlHbuUvZg1rB9ozK2EhHG8RWj2W+EozXeD1fRl8Y5zcJgMJGI+pPTRRPuiCWEFQ4hfZf3znZj1sVuCF/nGiVkBe8S6IEbz1bXJEI15bDhTiJrxzdZcngB7vLNYsIKdWpezhE6U0E3hf9CMWQv//0s3VPBOTyC5SFSlInDBNNncUb8FCw1xL2ZGu1iLJ5yFl6NDZnk8rJ61p1gBKpeBx2tO5oD0HJf19NJTYXMTYG+9yrt7kAY237SBJRuTE VdvX4x6T w3702r43AuJfKPudFhgRx7ilHaGs2XOmK9kNrANiJpsw9JIr6eRo3xZTchxsSoCioZuPj2qHbZyCj36DVd/XYKRbEeDxz7ezq1sVJzr4pp83D23/yI348sA6YcoyF9+gXQfboB0kgeK1lWIxRgqcpsefHfghSQxzYq9LvILD0zhVGFepCpzqI7HeZh8QP2rxNoL6hfYL9dxIufZvKh1OGPTifNUnv5HN36h8YXvZkRDhQ6G9NwjiCkwMjHHE/LqWqoxPy+763UARbKLMGQQ6oIV4IkAivJgtnh8rYS+QS9vNFgGBv3hzG936QVLRLyWonDTPQh9VSBpqHn2mILqpSKWPYANGGnXTaiIuVT0Ba0GaDUpRbrhwQqr7vlACCuf5zNM6tq2K6TQE19mTot6HX5UHzdKCXgDGenBe+zFdwOmRR5OxsnE9FhbXHzTDHn6221HvzIGq//gk93mENp147J1O6rw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: zicfiss / zicfilp introduces a new exception to priv isa `software check exception` with cause code = 18. This patch implements software check exception. Additionally it implements a cfi violation handler which checks for code in xtval. If xtval=2, it means that sw check exception happened because of an indirect branch not landing on 4 byte aligned PC or not landing on `lpad` instruction or label value embedded in `lpad` not matching label value setup in `x7`. If xtval=3, it means that sw check exception happened because of mismatch between link register (x1 or x5) and top of shadow stack (on execution of `sspopchk`). In case of cfi violation, SIGSEGV is raised with code=SEGV_CPERR. SEGV_CPERR was introduced by x86 shadow stack patches. Signed-off-by: Deepak Gupta --- arch/riscv/include/asm/asm-prototypes.h | 1 + arch/riscv/include/asm/entry-common.h | 2 ++ arch/riscv/kernel/entry.S | 3 +++ arch/riscv/kernel/traps.c | 42 +++++++++++++++++++++++++++++++++ 4 files changed, 48 insertions(+) diff --git a/arch/riscv/include/asm/asm-prototypes.h b/arch/riscv/include/asm/asm-prototypes.h index cd627ec289f1..5a27cefd7805 100644 --- a/arch/riscv/include/asm/asm-prototypes.h +++ b/arch/riscv/include/asm/asm-prototypes.h @@ -51,6 +51,7 @@ DECLARE_DO_ERROR_INFO(do_trap_ecall_u); DECLARE_DO_ERROR_INFO(do_trap_ecall_s); DECLARE_DO_ERROR_INFO(do_trap_ecall_m); DECLARE_DO_ERROR_INFO(do_trap_break); +DECLARE_DO_ERROR_INFO(do_trap_software_check); asmlinkage void handle_bad_stack(struct pt_regs *regs); asmlinkage void do_page_fault(struct pt_regs *regs); diff --git a/arch/riscv/include/asm/entry-common.h b/arch/riscv/include/asm/entry-common.h index 2293e535f865..4068c7e5452a 100644 --- a/arch/riscv/include/asm/entry-common.h +++ b/arch/riscv/include/asm/entry-common.h @@ -39,4 +39,6 @@ static inline int handle_misaligned_store(struct pt_regs *regs) } #endif +bool handle_user_cfi_violation(struct pt_regs *regs); + #endif /* _ASM_RISCV_ENTRY_COMMON_H */ diff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S index a1f258fd7bbc..aaef4604d841 100644 --- a/arch/riscv/kernel/entry.S +++ b/arch/riscv/kernel/entry.S @@ -471,6 +471,9 @@ SYM_DATA_START_LOCAL(excp_vect_table) RISCV_PTR do_page_fault /* load page fault */ RISCV_PTR do_trap_unknown RISCV_PTR do_page_fault /* store page fault */ + RISCV_PTR do_trap_unknown /* cause=16 */ + RISCV_PTR do_trap_unknown /* cause=17 */ + RISCV_PTR do_trap_software_check /* cause=18 is sw check exception */ SYM_DATA_END_LABEL(excp_vect_table, SYM_L_LOCAL, excp_vect_table_end) #ifndef CONFIG_MMU diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c index 51ebfd23e007..225b1d198ab6 100644 --- a/arch/riscv/kernel/traps.c +++ b/arch/riscv/kernel/traps.c @@ -354,6 +354,48 @@ void do_trap_ecall_u(struct pt_regs *regs) } +#define CFI_TVAL_FCFI_CODE 2 +#define CFI_TVAL_BCFI_CODE 3 +/* handle cfi violations */ +bool handle_user_cfi_violation(struct pt_regs *regs) +{ + bool ret = false; + unsigned long tval = csr_read(CSR_TVAL); + + if (((tval == CFI_TVAL_FCFI_CODE) && cpu_supports_indirect_br_lp_instr()) || + ((tval == CFI_TVAL_BCFI_CODE) && cpu_supports_shadow_stack())) { + do_trap_error(regs, SIGSEGV, SEGV_CPERR, regs->epc, + "Oops - control flow violation"); + ret = true; + } + + return ret; +} +/* + * software check exception is defined with risc-v cfi spec. Software check + * exception is raised when:- + * a) An indirect branch doesn't land on 4 byte aligned PC or `lpad` + * instruction or `label` value programmed in `lpad` instr doesn't + * match with value setup in `x7`. reported code in `xtval` is 2. + * b) `sspopchk` instruction finds a mismatch between top of shadow stack (ssp) + * and x1/x5. reported code in `xtval` is 3. + */ +asmlinkage __visible __trap_section void do_trap_software_check(struct pt_regs *regs) +{ + if (user_mode(regs)) { + irqentry_enter_from_user_mode(regs); + + /* not a cfi violation, then merge into flow of unknown trap handler */ + if (!handle_user_cfi_violation(regs)) + do_trap_unknown(regs); + + irqentry_exit_to_user_mode(regs); + } else { + /* sw check exception coming from kernel is a bug in kernel */ + die(regs, "Kernel BUG"); + } +} + #ifdef CONFIG_MMU asmlinkage __visible noinstr void do_page_fault(struct pt_regs *regs) { From patchwork Tue Oct 29 23:44:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13855737 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 77610D7494F for ; Tue, 29 Oct 2024 23:45:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8A9826B00BA; Tue, 29 Oct 2024 19:45:14 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 85A896B00BC; Tue, 29 Oct 2024 19:45:14 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 689636B00BD; Tue, 29 Oct 2024 19:45:14 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 42B636B00BA for ; Tue, 29 Oct 2024 19:45:14 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id F219F4079C for ; Tue, 29 Oct 2024 23:45:13 +0000 (UTC) X-FDA: 82728272718.15.C443931 Received: from mail-pg1-f172.google.com (mail-pg1-f172.google.com [209.85.215.172]) by imf04.hostedemail.com (Postfix) with ESMTP id 2673A4000B for ; Tue, 29 Oct 2024 23:44:39 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=bnNEt6C8; dmarc=none; spf=pass (imf04.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=1730245457; a=rsa-sha256; cv=none; b=ee5vHjZgug9j12ej5U/AbfHNxIj4qM5vYoGJ9DTceyt7xeb6HjZVeogYj2iUi3V0sUYN0a G/bezpdaao7HB+YhZbAaKTyZcMv/xx2W1TM9T/1ydqnDSgxPhJctqxc3InerG4ZPfPlVKW bJv1+UqZxB/LWe/n8njdPSnmDL0SpA0= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=bnNEt6C8; dmarc=none; spf=pass (imf04.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=1730245457; 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=yi9c4PfgxIrsXFqGtUWAjsswGeaf0ww2Eq0Goi40tzg=; b=LfmVz60fQsQR65LfMGDKSPUcNWnMdLSe5iVYj3IjViRIA2H2mC9HKWlOmYI6rRPZmoABen Z4DzCUVCdGCoxUN1G/e9GnCdTJDUS4n3F160ncU0m/fO7I5EOouV71fcFnlEG/334ve42P bOVbQDscX7jg8iZL78F7pqkGDbbWDIo= Received: by mail-pg1-f172.google.com with SMTP id 41be03b00d2f7-7ae3d7222d4so4663609a12.3 for ; Tue, 29 Oct 2024 16:45:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1730245511; x=1730850311; 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=yi9c4PfgxIrsXFqGtUWAjsswGeaf0ww2Eq0Goi40tzg=; b=bnNEt6C8SNqedlh0m0aHTX9MnyuxB4pwKXVJGCnPsAQTAHED8/O59y1B2RBgRrQl2y mvjU0G2P3yH0AQv+zl/39qgPEZDeNtoWj7KDirGKK9Yd4rHlgchvZF+uSIIF0nizrSAQ lNdPgfM47g4FnaVer05IMyQzpkd/ivoi41D/U89NCZBgHaNJ36jGWb8fZhsuU3F1rC0i s9cBzz6KT8xbXKCf92xpXhN3aqqqWzMNDiE2T9ClJyH/6x6p6ikkmwSPqG9i5fnZQsOT 0xEpVqj0o3jJIbeiGszcd14+5I17lks6XLXY4i652JplUeSnXEBA7axBSmRXZ1PvXR26 DXog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730245511; x=1730850311; 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=yi9c4PfgxIrsXFqGtUWAjsswGeaf0ww2Eq0Goi40tzg=; b=TQVpQCX08EwolHg20IjskpTzn6JRr7nMa/ZNQqYz+N+Mu7P6yBuMWVd+nwV+2pN8L6 qYz/AaKbcIsOVXlgXMtsu39N42uM7pZiYA/9AxuxfEBNG27rDl5MVSvUsk0xgeZ/Mu+P xyINLv1nn7tWo505ReCBqCaPhYW7ypZyIwnNmDOZE/UcXCmHbTN+mrw0DhSvGzagazMY BJSqHhIaOij5/yFaup4N9DZIhXY7IRyUKNuq+8VLmwwkgOttdX68tmQvO7Ofe7IDibfy 5l6ZIGxgplGIGTi7oRkj/WVckpCYCT96wjLOqaD000FOREzDE1KuJGUXKp7T2e5PhYIV 4Wrw== X-Forwarded-Encrypted: i=1; AJvYcCUijNS3PMZ5Ehkpjv+6jTmJELnQTSiYoUjgcP7jPIvsYzjnP3Y3LzCdrCj4GKtg3/3LCV+Abx1IJA==@kvack.org X-Gm-Message-State: AOJu0Yy+RapHsHiu2X4AUIaSqTQ+XYTXQs8ESuV3ChtSZvphc1qduWXw 2qsPm8r3XY4QuZhoi1i3JNJWElPG4WLhr1A21Bkdn+BrUkJWJYIWl7CIt33bHUY= X-Google-Smtp-Source: AGHT+IF51lB1bvJMVNfZsGgqLWarH+JYdED//undtRt22viNiy18bh6cEWI+2sRnPRU3+1w1VPg/Hg== X-Received: by 2002:a05:6a21:3384:b0:1d9:c569:15f2 with SMTP id adf61e73a8af0-1d9c56916aamr12728492637.2.1730245510969; Tue, 29 Oct 2024 16:45:10 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72057921863sm8157643b3a.33.2024.10.29.16.45.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Oct 2024 16:45:10 -0700 (PDT) From: Deepak Gupta Date: Tue, 29 Oct 2024 16:44:21 -0700 Subject: [PATCH v7 21/32] riscv: signal: abstract header saving for setup_sigcontext MIME-Version: 1.0 Message-Id: <20241029-v5_user_cfi_series-v7-21-2727ce9936cb@rivosinc.com> References: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@rivosinc.com> In-Reply-To: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@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-Rspam-User: X-Rspamd-Queue-Id: 2673A4000B X-Rspamd-Server: rspam01 X-Stat-Signature: d758kg9krrzjk3aqxbxhtinmoco6odh1 X-HE-Tag: 1730245479-281494 X-HE-Meta: U2FsdGVkX1+2WkfPw/5NzI6URVPhyUiHk5j6Tlc2XiJxB/qhXFu64c22rdeBxL18SE7Ac2vqk2G0Drd5ZcAFijDVSAOnu7zvY043VlfRs6n4ZBVqDV4w4BNWFtlETlEeQa2m9ADXpwrdYpFrQo3oZtqqJ0jB+CxAC/yK9EYqk8fnuZ2mfwYUJsrYuqCBloFKvyCR3WTNNaLCAqsxzxABHBHhUc/W7CKo3H4PBzzWCyG81bk+PMddr5ZW7q9VbvM6X5YapZvDuC0WSqPNvSp4HpR8M/zNLldjDY54KNSkJHAGvqKT47gbcKL0sHm/28rB9ZiGUc0mj9KECiZuwKYcljE9F9xEZrQd+RH28WyD4F7a6//ic+g06hYSlH3UpNEiEUQyT+cFE9KSAn1lMO1dbjPiTBlIwDNvwX430nRt58VtIKQX7KRjIHpwfzbtKkXNJPm9lNA2sdCHV+FJYsaLj76DCmLW4iWT9C8mpaTOEv0xFE9LslnOA99EARFrODdef0yp6JYKIvvRGT47qN9MnEqY3dXOGgq5vtdawfETGYQTfrl8I7lo9CyEfncJL1M4WKdaw0HYbi3GZen/0L8cTL8duJa5M65V5YKZG38jOWta922uG2cGhueu7pHAFMjIO6689xH6MPtQhOKjyoT71TeRFqS/FngGlixeM7xq0VPqnPSp22Fpf96fTe/8jDlkjRcAVzMmhzkO3BsrKhniq9WKn4SRl5i8SL1xzLpuD1pUwttjpwtfV5lGjS9EiCWf0hjceHrjCfQHkya1GDDTkQIm+wiIW703wvZk5mi33RWaaZZ0YvKpdRzl2zdsPFwZir9qLayK+e//2Cmd0iba6BrvvZhiPlwAO80US03ZESdFZII7dwArKRSfRK+627mP5sEHbe2eVY2r64yz6GRYbY9hN7RUag7Akmw0CtWeX5XYB1LsbvgDFt595N29ku1RLx6Hgk7upaq+McSwiG+ xzNApYjb HKrp6D6pxGCHrjAc0NnvLNFZXbI7kZqrZrS+RO2GpE5UGdZDKg1VfcJp5heGsD+WS4i6DqoENL6dEHp+AJq4oMdCRKt2oADGlNpcLPRhSI92qGo3iVJvYduL2MX6ucVRLTZtOyXs6nNZGK4/PzbTpjiNfm0EsWXCdjhc0flmPo5SYV+XTPPJemhHQJqAeJw2lSmjlRDQemAa2MkWuduBWxeon/39AEwDySuUfqE8R/5O0cTlDphhNV25tgvz4nkylWqIvXyjy6VzVMXxxfeRZFzLiAg7U7znkijbWg5T3YRD8EHnDFUSALs2jHF2t5SbBpWrA02iFghx6ogoKUt/yYFS7hBYP4N1Dby1n+6izK0qSbTx4Tt3fx6RyO4w4aSqyKuJQJEsUmwlbKOT+H8SU4Ow6gv0q4ZBl9gydKMCRxIGTh/CwKglFMGI2G745VpEwd+mkAGvf1gOaVP0i43JDaS0orUGF7DB68A8NJiizETeP9hvHbxdZE06eFupbCZXSOpWr X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Andy Chiu The function save_v_state() served two purposes. First, it saved extension context into the signal stack. Then, it constructed the extension header if there was no fault. The second part is independent of the extension itself. As a result, we can pull that part out, so future extensions may reuse it. This patch adds arch_ext_list and makes setup_sigcontext() go through all possible extensions' save() callback. The callback returns a positive value indicating the size of the successfully saved extension. Then the kernel proceeds to construct the header for that extension. The kernel skips an extension if it does not exist, or if the saving fails for some reasons. The error code is propagated out on the later case. This patch does not introduce any functional changes. Signed-off-by: Andy Chiu --- arch/riscv/include/asm/vector.h | 3 +++ arch/riscv/kernel/signal.c | 60 ++++++++++++++++++++++++++--------------- 2 files changed, 42 insertions(+), 21 deletions(-) diff --git a/arch/riscv/include/asm/vector.h b/arch/riscv/include/asm/vector.h index be7d309cca8a..2d2ec6ca3abb 100644 --- a/arch/riscv/include/asm/vector.h +++ b/arch/riscv/include/asm/vector.h @@ -281,6 +281,9 @@ static inline bool riscv_v_vstate_ctrl_user_allowed(void) { return false; } #define riscv_v_thread_free(tsk) do {} while (0) #define riscv_v_setup_ctx_cache() do {} while (0) #define riscv_v_thread_alloc(tsk) do {} while (0) +#define get_cpu_vector_context() do {} while (0) +#define put_cpu_vector_context() do {} while (0) +#define riscv_v_vstate_set_restore(task, regs) do {} while (0) #endif /* CONFIG_RISCV_ISA_V */ diff --git a/arch/riscv/kernel/signal.c b/arch/riscv/kernel/signal.c index dcd282419456..014ac1024b85 100644 --- a/arch/riscv/kernel/signal.c +++ b/arch/riscv/kernel/signal.c @@ -68,18 +68,18 @@ static long save_fp_state(struct pt_regs *regs, #define restore_fp_state(task, regs) (0) #endif -#ifdef CONFIG_RISCV_ISA_V - -static long save_v_state(struct pt_regs *regs, void __user **sc_vec) +static long save_v_state(struct pt_regs *regs, void __user *sc_vec) { - struct __riscv_ctx_hdr __user *hdr; struct __sc_riscv_v_state __user *state; void __user *datap; long err; - hdr = *sc_vec; - /* Place state to the user's signal context space after the hdr */ - state = (struct __sc_riscv_v_state __user *)(hdr + 1); + if (!IS_ENABLED(CONFIG_RISCV_ISA_V) || + !(has_vector() && riscv_v_vstate_query(regs))) + return 0; + + /* Place state to the user's signal context spac */ + state = (struct __sc_riscv_v_state __user *)sc_vec; /* Point datap right after the end of __sc_riscv_v_state */ datap = state + 1; @@ -97,15 +97,11 @@ static long save_v_state(struct pt_regs *regs, void __user **sc_vec) err |= __put_user((__force void *)datap, &state->v_state.datap); /* Copy the whole vector content to user space datap. */ err |= __copy_to_user(datap, current->thread.vstate.datap, riscv_v_vsize); - /* Copy magic to the user space after saving all vector conetext */ - err |= __put_user(RISCV_V_MAGIC, &hdr->magic); - err |= __put_user(riscv_v_sc_size, &hdr->size); if (unlikely(err)) - return err; + return -EFAULT; - /* Only progress the sv_vec if everything has done successfully */ - *sc_vec += riscv_v_sc_size; - return 0; + /* Only return the size if everything has done successfully */ + return riscv_v_sc_size; } /* @@ -142,10 +138,19 @@ static long __restore_v_state(struct pt_regs *regs, void __user *sc_vec) */ return copy_from_user(current->thread.vstate.datap, datap, riscv_v_vsize); } -#else -#define save_v_state(task, regs) (0) -#define __restore_v_state(task, regs) (0) -#endif + +struct arch_ext_priv { + __u32 magic; + long (*save)(struct pt_regs *regs, void __user *sc_vec); +}; + +struct arch_ext_priv arch_ext_list[] = { + { + .magic = RISCV_V_MAGIC, + .save = &save_v_state, + }, +}; +const size_t nr_arch_exts = ARRAY_SIZE(arch_ext_list); static long restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc) @@ -276,7 +281,8 @@ static long setup_sigcontext(struct rt_sigframe __user *frame, { struct sigcontext __user *sc = &frame->uc.uc_mcontext; struct __riscv_ctx_hdr __user *sc_ext_ptr = &sc->sc_extdesc.hdr; - long err; + struct arch_ext_priv *arch_ext; + long err, i, ext_size; /* sc_regs is structured the same as the start of pt_regs */ err = __copy_to_user(&sc->sc_regs, regs, sizeof(sc->sc_regs)); @@ -284,8 +290,20 @@ static long setup_sigcontext(struct rt_sigframe __user *frame, if (has_fpu()) err |= save_fp_state(regs, &sc->sc_fpregs); /* Save the vector state. */ - if (has_vector() && riscv_v_vstate_query(regs)) - err |= save_v_state(regs, (void __user **)&sc_ext_ptr); + for (i = 0; i < nr_arch_exts; i++) { + arch_ext = &arch_ext_list[i]; + if (!arch_ext->save) + continue; + + ext_size = arch_ext->save(regs, sc_ext_ptr + 1); + if (ext_size <= 0) { + err |= ext_size; + } else { + err |= __put_user(arch_ext->magic, &sc_ext_ptr->magic); + err |= __put_user(ext_size, &sc_ext_ptr->size); + sc_ext_ptr = (void *)sc_ext_ptr + ext_size; + } + } /* Write zero to fp-reserved space and check it on restore_sigcontext */ err |= __put_user(0, &sc->sc_extdesc.reserved); /* And put END __riscv_ctx_hdr at the end. */ From patchwork Tue Oct 29 23:44:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13855738 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 194E1D7494E for ; Tue, 29 Oct 2024 23:45:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3CC456B00BC; Tue, 29 Oct 2024 19:45:17 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3530F6B00BE; Tue, 29 Oct 2024 19:45:17 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1A5746B00BF; Tue, 29 Oct 2024 19:45:17 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id E89D96B00BC for ; Tue, 29 Oct 2024 19:45:16 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 9AACA8062D for ; Tue, 29 Oct 2024 23:45:16 +0000 (UTC) X-FDA: 82728272340.19.7BCA98A Received: from mail-pg1-f181.google.com (mail-pg1-f181.google.com [209.85.215.181]) by imf18.hostedemail.com (Postfix) with ESMTP id A79A91C0015 for ; Tue, 29 Oct 2024 23:45:03 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=yUgz4Tg0; spf=pass (imf18.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=1730245434; 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=xY1HZyB3+5HtdLtliWcm2BAH7v3JklK3IzP6KuOCLQM=; b=NycUCyq8tKU2OrwpgER1az75fVKmaRXiW6QAMNp5/rCLdDIf3eZWHkYzlFoM0bPWDlh/tO 0JoWXiZe4WuasW4PxzXe5b3qxpMVcQOU/2jld1jeUuXojnnC2XalGlK5tAMTHscZSbCMMI j2T5vWnhwUH+ai794uFXlSYPfSKC+oM= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=yUgz4Tg0; spf=pass (imf18.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.181 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730245434; a=rsa-sha256; cv=none; b=Jv8uQN7XpCgURznRpE4g4SnN46pOiOm07nqotudO3VgGeuoxLISWt6HRkUxTqk8rMhqMcB 6YpvOnrWNQ/5tuDm87FpB9wTB49KmagKOQKE7iYSoA6heMJ0TDlc6TYzgcCKe6z0d5Uzs+ ZU3PVhYRlvmZrkA79hJyGDh1jk/lFLU= Received: by mail-pg1-f181.google.com with SMTP id 41be03b00d2f7-7ae3d7222d4so4663634a12.3 for ; Tue, 29 Oct 2024 16:45:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1730245513; x=1730850313; 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=xY1HZyB3+5HtdLtliWcm2BAH7v3JklK3IzP6KuOCLQM=; b=yUgz4Tg0oQmG4fRcRF6dwfnZgQPEp/dkn9nn6ITEWxvsrqyYUWkRTaeGKLw5z9BGwI 1IBUz0/ywTBKTTTtUJU+wm4YzaSo7wVvgQr1x0Hh2q0yXjtwOCN5NtYYjuBa/NBt3Kg0 BfF3tea3wNGTjwtuVLBuRKYQDM2QglYMGVZn4SGw0izQuvRaLce8SwkhXHIpTpATQDsA Izkh7pZiwcelrx9zD9qtBGLRDCXhEht7tIZ6ewSlT3w3oIZv6Yshi0lyM9zsGo5S1bqV IvXe/PZBFPJkJ4f9IpRfsVAYPDy/nMQXQIYEvFwQbK3Y/PugbMshFBJsCkHejVVUZOvQ pTMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730245513; x=1730850313; 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=xY1HZyB3+5HtdLtliWcm2BAH7v3JklK3IzP6KuOCLQM=; b=Unh3P9YeiQmSsuHoi2SeJxjTCQgzGfzQkqHYXO2nygvFcQNpaenYFApIGMg4OGrZi4 3a5khhoXRkMTLBUqj0bZNRUq5jO8/Nk+9BrilUu3OZJeK9/bGJO4zVf5ZOVABWB7LfWP 06L94GCgRYK8Zd5D+KC+FXa8od3Xayxz5PaoBis5e8vgF2Hp7qsLJosH63axTqo9nH6J mDp/WOw8AkeSnP7h9YKlTEY5sQgxauAPn0/etNZOdJL2ypNaLQxebG3ysWYGYfUDbqg2 2nYT89aFRc2CgTFp1eDctargpPP0uItvRrhdv3YcqsueInxkGZdNzScRMDrJVK7K0dyI 2Hwg== X-Forwarded-Encrypted: i=1; AJvYcCW+GoBIB8gJBuFMtOrKlY9mrNHG9iPF8GKfQtn6h3EQT99YU/GVOeKUG92F7A7mJ1mG8d87U/xQOg==@kvack.org X-Gm-Message-State: AOJu0Yy4xAjw6ChvWLxebkPeazH3AqHGDe6VRoK1QXlqT9dKcjX/6BAs Boe63D8fZBJUffoCdOlEMbCBxBXb4eEoczPMpHYU7Z263MeYraB/bwk5P40W/o8= X-Google-Smtp-Source: AGHT+IGNHK2yOYn367ndK2+Uka9OrsN6gkzcSCIsuTCT7jivG85bFoFBKYZ78LeUDOhKt6cWL6F7rg== X-Received: by 2002:a05:6a20:d805:b0:1d8:b8da:d658 with SMTP id adf61e73a8af0-1d9a8402d77mr17502118637.27.1730245513447; Tue, 29 Oct 2024 16:45:13 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72057921863sm8157643b3a.33.2024.10.29.16.45.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Oct 2024 16:45:13 -0700 (PDT) From: Deepak Gupta Date: Tue, 29 Oct 2024 16:44:22 -0700 Subject: [PATCH v7 22/32] riscv/signal: save and restore of shadow stack for signal MIME-Version: 1.0 Message-Id: <20241029-v5_user_cfi_series-v7-22-2727ce9936cb@rivosinc.com> References: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@rivosinc.com> In-Reply-To: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@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 , Deepak Gupta X-Mailer: b4 0.14.0 X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: A79A91C0015 X-Stat-Signature: nbaeg1ewap63wqaob56hbyipa3a78fgz X-HE-Tag: 1730245503-303567 X-HE-Meta: U2FsdGVkX18JcUZQja+JaZpujD9ss8EY7Z+mp9o9Yz3chRGSBoX/VLwtODCkUZeb4G1TK2EuFYhkiFnae+oZfNOhsYOTtkGOwV1kojq4CpZXN8nFPGEpEPh0rPWbVGROfownbtZIr7uQBeXm3a52fKpUS6MxHeva3VLDWyE9WD+MtXC0T5RuCiJSIYZdxfgpDxNugGw0h8AJziJ7T4Uv1fj0/OcautDde+/YJyUf5Tl6i81mfT0rGb3GyELVAAi8wjKRJs5BK6N8FgHew0bxtGTSYhk9I2WEVKRLnqoc9Zn7gf1AB8BdhOAeOhQfcbPXEApLs0gcts6yy38b+nfmR84M3WBhfgj4Pi/cl5Joj9/b+3oC9Wo3jL2O7Kk1Jq1DcTtDdPwmq6qFVOJX87TkNbqeEV5I/GXAr25ZrfWNjsGCQegf0N8qewjlJRg0N3aEbS51rUgGfnak5OpxYTHRVJu2mwIfGCuPsBM6kUrxm7fCjXfOFHKsCuBbRV7TBrNwnm3qXDqMvZD5O7hlLlYYw0Fv++r+uxneoLdMR3YiCKCU2M6J4Q+2EY/7MS70CUFCFBgCbWJkXyYiDAGYmcyy5NdkxYFpiESics5MFGz7rq6d+kk3DkVjIMTktHTj0siecAr99vmIwLG65CDR6rRyUw/0nPTNCAvkNK96SQhXZ0X6Bpfahjy1rjGokPKzVyMB1vaU8MIHbwaC/NyR40pcfBJv8TI3gkJTRh5/MNeY9aH28ZKCJGHFd4RAt3xn8hXg28NkmihAQCPmhEXgfU7zzIztfU0wln723YJ41AMwk8cC94u+FalVXaeqQafasCiLspT66UU17IGcpW8PiUwUfakMGu1lK7gRtXgGjJP0GK1T+m7xlFk2jd3F8ykEIiYMi7/GMgZo7lfFQZ2rVF+8h3duyiIzzQ7e1oYYajhprnZGQUxM1DG0cmOPI/0hAzo/48PFPYrrF5O9twCzEPZ XxgYkYmK /fNkQ4bt0hxA32RojuKpMvHVxn/cJ6ctCuBteSWc74jEaVX2hDlBJ1069LR20b9g/yCp5DKOB7vmBk37FFLrYs3Lq3QHpo4DYo7ksTOUCD+Vbq8Nm1GBU1vlX48T9tTCO/XexT0DGJy8nF3ZDYFR1slBP4s1KqGKNR19uFRrjxxTLcO6UKfjshWUQMIb298USFqO5KBXumme0qmxpIolRjXGLhQB7k6ub7hr1CiuI1cpigMsifrfUbwhpvrPWjMjbWcZjbPhZ6hqGqsHo5D0zOrc4iCEjwLIRQF3zH26YtcLsbLNpnk26IjaA45XVHUub0meawCXYq6RpBy2rhPPLalIg1FKB36lYDMmCKnbIVgGHdZVa7/go4nqheLIKotgJtC3xGpIBxuQSnPwruk8Ok8+flOIdpzFY+pQXLUDDSFzLBq3Yg8JHuhJwohFWcqgfMpp2vgEowmPw97ld8YtYhYM+dMdvgtqCo5jHhMjZi4iVMqiyD/XdzhFUKRIBbhB35NumtZ6fhcghfDk= 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 Tue Oct 29 23:44:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13855739 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 EBC5DD74950 for ; Tue, 29 Oct 2024 23:45:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 57AB76B00BE; Tue, 29 Oct 2024 19:45:19 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 525176B00C0; Tue, 29 Oct 2024 19:45:19 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 39F6A6B00C1; Tue, 29 Oct 2024 19:45:19 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 18DAA6B00BE for ; Tue, 29 Oct 2024 19:45:19 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id C590F1607EA for ; Tue, 29 Oct 2024 23:45:18 +0000 (UTC) X-FDA: 82728273264.28.0C4CE40 Received: from mail-pg1-f179.google.com (mail-pg1-f179.google.com [209.85.215.179]) by imf07.hostedemail.com (Postfix) with ESMTP id 0E6FC40019 for ; Tue, 29 Oct 2024 23:44:41 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=aYcngHcd; spf=pass (imf07.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.179 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730245304; 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=2F6Yb7wvkADRBV85CoJaexMx2ZeCvBhuuEpWc4ZWwJ8=; b=ZdQSl74ccVpSeZ22anJCtkykwqIVigQElZR0oq8RG2lDhQa2y8Gm224JKNkoK15xb3kJUm efThyr53wPO+xCcQi9sAH53PsWhHAJQ45G+IJYf7gVMZA31u49efrNm37+0b3Ab18gyr2D bUR4bNF49Lh/xKunewqMJzi3pC/vjYI= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=aYcngHcd; spf=pass (imf07.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.179 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730245304; a=rsa-sha256; cv=none; b=C78EheznqlxnJ158Gr+GuyQPRMjowJv33AfHdBq/Q1tflm4orN0NsJG1I/c90tbkpIo35w cr06kUIEJMbmvXe3amrm3Q7rw/cMd792oXx18QqKuEqfYkhnYGJ9kJ+W1MnHUkUAf3zQDx EzdFpM1W/DsNObnJdZI+5hlYVGEb/tc= Received: by mail-pg1-f179.google.com with SMTP id 41be03b00d2f7-7e6ed072cdaso4330532a12.0 for ; Tue, 29 Oct 2024 16:45:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1730245516; x=1730850316; 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=2F6Yb7wvkADRBV85CoJaexMx2ZeCvBhuuEpWc4ZWwJ8=; b=aYcngHcdhZa2h42IjBQD/nVxwrIVARMRz07TWibQn9bei81qgg8E62b8cft2Nqo9YU D/XROehXUkVHQjpRv3AOCKah7TDdbdxmsgxa0RfYALL7tsIvXo5pWQD8HiY9WGoVjO5b w0OMi6Hlh0dON8rvyoMt9/UYDOkZMr5ZyJWs3VvpQqC4yUnKYUNMByT5dRkr7XgRqlD2 5uzlhgk9evd1WQZ1dgbGO07F2Yos63zxgiYPmM6snxOJN5twtTucQAZfG79IjaKeGnWA frXRb977bxGToxQy4P2E9O4mPpBTDsxtHrx7V9iKDDjLNWFQs/1HnkHcrgMml5YjDRNV mJLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730245516; x=1730850316; 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=2F6Yb7wvkADRBV85CoJaexMx2ZeCvBhuuEpWc4ZWwJ8=; b=ux+/bj4eqlui/kv0f/4Yt9bPMMa7MzUGkupu3SetHrGk65Jnn6jT+a+O/Dgs59fdfG 6sgicmv0xgEbE4aG9ebWYpJHfDZegSoGyB5wmr4gU6tuD8m2pqHNn2iOOqoVOU2P8+Cc LfOuhmUdd8S6kW0qU26c7UP1FjkaeqFAA6sxI2MLXnwRcVfzm2O7tNkSbVdL6UBIG4T2 lequ4uKoOBCNPNgZVHbrigbj3RAX/ZBzjUZ3xz7Dqpa8tqUtb+VSTL1YcomfjEbRySpc ubuokeIheisydRzOtLiVdjUup1zE7ICrH3Aj9WtRNeQmigAGC+HzU7CThRm0gnQetmVU D53w== X-Forwarded-Encrypted: i=1; AJvYcCU44N892FZ/BpzpSnMTaAY/wQrtjdIvwZ0KQgXMuo7BLi62FeP3+6S2DTi0p8DZTGgxTYLuPVpZdA==@kvack.org X-Gm-Message-State: AOJu0YxVaDxheM+Bf/m9s99gt0Lfxu18xN4POVd1wT9yfNHaXeVA3+g2 VaiiaHxGjQu7/s6JXiWXIDnNXKmEd305q4o3UScpRd4k55bNMInFMhfvk2wVeSk= X-Google-Smtp-Source: AGHT+IFymY57Q4keJ+3CC+aQlV02UPkTqGn3YIXKiyS+cNgyPpergOLYnge8dObsMTdwpbmiiUHDiw== X-Received: by 2002:a05:6a21:3a85:b0:1d4:fc66:30e8 with SMTP id adf61e73a8af0-1d9a83a6524mr18658257637.10.1730245515950; Tue, 29 Oct 2024 16:45:15 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72057921863sm8157643b3a.33.2024.10.29.16.45.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Oct 2024 16:45:15 -0700 (PDT) From: Deepak Gupta Date: Tue, 29 Oct 2024 16:44:23 -0700 Subject: [PATCH v7 23/32] riscv/kernel: update __show_regs to print shadow stack register MIME-Version: 1.0 Message-Id: <20241029-v5_user_cfi_series-v7-23-2727ce9936cb@rivosinc.com> References: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@rivosinc.com> In-Reply-To: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@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: 0E6FC40019 X-Stat-Signature: 64ia39s77cybx7bmaipahum8gmum8hm3 X-Rspam-User: X-HE-Tag: 1730245481-953994 X-HE-Meta: U2FsdGVkX1+XBUn9wbRFsA/gyw3yDs4HYeYbJsWi5BlkzGK18/SPy+bluMinwaVanvFbwrtvjvmlKPUepNmzUdzxQZZRea/E2FHnaroQ2R0Byzh2iKksjrzpaqPoPfzTPltk0oWnGMlHe+bfZd2vpWlPm1yE6BPbW3xRAFYj/U3RDJaKEUqJwTDlAZYl1No4KCE8bS9BaAinrQhf+8fbpF85GBmV/KKfIfUo+BUpBJWh8fBDPxRF9Ecn5/FLNTmfWqfLo4isT1BxADLvusNA3rf1gQNNZZoqgkWMdmuyJ9kc7V1aOlFs/kibqr/GNqDFe5QirTinD2llNDgt6//cpsnFnBGHEExLRJZtbwbG8Vd7dxDeHdXfQDLmhikS/8krDikK9JyeArQFkW0Eu0cQxHUFKlwETa4ZwU2/5L6SThG4VSq9rhYPzXOW/9NG7VR+S6nKCH+5+X3Ljzj7IzgMB1rZFa/a0+NnGhfL9XgAhNLgoGyw/97zj5SsiMBuvv6FkppTzMht1REJ/q/uM0JNb0xxk2YTQrhGHBNql5bXYEU8LdQLWRsPmbyY5a4Ft3gvYebT3DRaKMGZV0IdMRkCLwG1vqbVt4eMRenQuO2duvXCjyWe5+noU7vuo4cD9BSBKsIO+/oV00yCGEKdnpEQsYnoIZZxjflbWm1XjsVUDeDdlkVfg1bsvLWXu4E0UTjECS2aPDQZMVZQFDPrhMmrizth4tPYyQ0YVHtkALc59Lkepdl90UIuRarWHXd7hw56Tktx/p5H9Of1NMwTnp6M2dl6aOiSz6FWWzvR6Ca8rLbtXSqhJB5wDR+BK79Povki2ZzCaJuM29gGnVsqLkdryqcP0Na8XTqTOGQpZB1W9J+rIiNQ69CNqRXtYoWeJtyNcTOgVgm54xGwnPH6+X5XlVDU2fAd5DbFqqAjG9sdYkpkFadEzjkHAvfqz5JFTpFWiBEI1zhozBmpz22NHG9 kC/yh5/2 ptIeXRQSwK9j4LFa1WQVkaq93RTEVt3Bk8JrzP6ob7JVenrk0r5y17DZkmkEB0tCy7Juz6N4tM/PqaGLo5JkrV+9GcVqWx0mgwd9tZw2EDd8CAKn3gVdzjd4t8TRCbEZsktbxCHXAEiuCNFMX6ibPG1zJUwLL50DP6q82dYzyXVO4qYiBH0FyMjkZ5akKEzYTXKRtVv55180FL5QrqVhrrCqVoyQBo03NXhCNvRheOVeB87SGsjahV+isXJHQ0DsXpTOkWhs8DsmyphfbQZb6h3eGTrHtOFBfehbTu5Z4YBFo9EmdKDL6OE7zKsleogfbManjk2FOWLcNx3oUtFfEferpX6e1QkkEvr7gVPU2XRbDWWFW4Q7ZXSZfUw9h5DEi1g70dUfsCl9HdaagLV1vXTo5e3ZsCf3yyM5S6jyFBCayMwN63hMyepOM00wcSQeKdL3y4bI2r8aqIpffBmV1JUsUZhLsfzJQypOZaWHV6QEoQYyzZkxxhtuzwcpReG5u1xS5 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 013bc3805afd..6eb4b8e0f07e 100644 --- a/arch/riscv/kernel/process.c +++ b/arch/riscv/kernel/process.c @@ -89,8 +89,8 @@ void __show_regs(struct pt_regs *regs) regs->s8, regs->s9, regs->s10); pr_cont(" s11: " REG_FMT " t3 : " REG_FMT " t4 : " REG_FMT "\n", regs->s11, regs->t3, regs->t4); - pr_cont(" t5 : " REG_FMT " t6 : " REG_FMT "\n", - regs->t5, regs->t6); + pr_cont(" t5 : " REG_FMT " t6 : " REG_FMT " ssp : " REG_FMT "\n", + regs->t5, regs->t6, get_active_shstk(current)); pr_cont("status: " REG_FMT " badaddr: " REG_FMT " cause: " REG_FMT "\n", regs->status, regs->badaddr, regs->cause); From patchwork Tue Oct 29 23:44:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13855740 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 7F51FD7494D for ; Tue, 29 Oct 2024 23:45:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 37DF76B00C0; Tue, 29 Oct 2024 19:45:22 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2DEED6B00C2; Tue, 29 Oct 2024 19:45:22 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 157DE6B00C3; Tue, 29 Oct 2024 19:45:22 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id E2A416B00C0 for ; Tue, 29 Oct 2024 19:45:21 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 9AC2240110 for ; Tue, 29 Oct 2024 23:45:21 +0000 (UTC) X-FDA: 82728272886.26.EF711FA Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.178]) by imf19.hostedemail.com (Postfix) with ESMTP id 5FA541A0022 for ; Tue, 29 Oct 2024 23:44:48 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=bd5NGkdz; spf=pass (imf19.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.178 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730245359; 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=QwpCOYgd3zL3s/Z/4VzYPZAj3WNIfv9ehALpGT8zNys=; b=MstPbzd1KsheK70cUJ7pVLAy9P1ydCnys34A6qK0+eKUh//5GVRbsCulaVOGg4N2QaCzq1 ArfbXgnIZkwgspcHifWHQTpFlnvEl1r3BZ5eF4Muhl9S1MBuFGERCsg7/e52YC9EvVXWF2 2kDU7Ku38Av4/4gIOkd2o3diEJ1mev0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730245359; a=rsa-sha256; cv=none; b=Zdxl5/9yXEJPyH3UV+n6rty/NX1rdeMNjdrVRnDtHsuPPo4ShUkStukxdm6ChDrUIaOYYg 0Shd0c3876GU8zqU1kj64FlhtmvRM6jNTb+woFkvigoBmgpcaGMajxp2sEBe6e5JUt4N0K hzaW77wMoieekG7DJmazVPeKDmBoLmk= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=bd5NGkdz; spf=pass (imf19.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.178 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-72061bfec2dso3095908b3a.2 for ; Tue, 29 Oct 2024 16:45:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1730245518; x=1730850318; 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=QwpCOYgd3zL3s/Z/4VzYPZAj3WNIfv9ehALpGT8zNys=; b=bd5NGkdzNfJBwJNwtb9ZdonVSAHz6HQB2cSvt5d3CjLfSqp3Tjfso/u04WQF0NntOx +8VZiQM+s199tSFeFygaxiKvT+oSYfz/CFB3QBZz8MgGa6JayzCsPnrOBKY7pxLnduGs W0WimWcS+5OAdVkvLWyX4m7jII+wjjfYTC/ZKEt+gQwAgBgGGda4c0ZMZrYDoLXx0lrz 7qlh1zDqZUkpTjBnJhB9v1sNNImyNtVrEOtoRebycRxQtJYti69aIp4WpALvuiBoCsqI K9qX4t4xIWYXOBWKod77FbBW3ShgEMg9R+tatOPKvc/zKpFwzx2rgdBHI6//zsqBq5Ie 1o+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730245518; x=1730850318; 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=QwpCOYgd3zL3s/Z/4VzYPZAj3WNIfv9ehALpGT8zNys=; b=X0qEAlwtYThESrqobArZXG/Cmp+8041lJmXg98v3Amc/3PztuOe22AT6H0Oqyr9L7e wZqP+sAI6i901VO3QbutSqcMCWbX1kyBygCBy2/vVrvYvl6ylV8NSumXSSNtUhbXP0k9 b4gzF2Tkn6BiC5QFAEPnWx0p2SPYEfRZYVrRZKtH10uGdQvHENg/i2CBJxOdhxFELoNy AHvsi3IcmNsYo83A7pOSgdARwfmwOmH9lOr2zBjNuXEL2ihvmkQyTiDEJIiKMKNjmMX/ Uz0qnULfLiKs/3iSsJN/juyinl51ZKfAZXKq/zguQIbEvmhzJxdo8oGpIZ2Hdso+Kt75 uSKw== X-Forwarded-Encrypted: i=1; AJvYcCV/WffsFOocYQ1itif9pnlsVAtzIfRVQ/4n93bghk1cc/OMhpKTki30Se6njhMbk5pDRMtQH5rZzA==@kvack.org X-Gm-Message-State: AOJu0YwcAJo+MIvxesXpi7kKBpRRm1samloaie0zA7vso1RFohQVwXdw zLDhcW29YOh/PWIanZ9X7JQPF5yr7FGb9AINwigSi31RoJorbp0jD2UEnkrNVEE= X-Google-Smtp-Source: AGHT+IExLMOGxhufhJ9GiTbSJsixKi2sjadKTrXlO6y9aTTbq314FpS9FDERV0yzRC2SPTmvP4cF1Q== X-Received: by 2002:a05:6a00:98b:b0:71e:c0c:5996 with SMTP id d2e1a72fcca58-72062f84e28mr19138366b3a.7.1730245518438; Tue, 29 Oct 2024 16:45:18 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72057921863sm8157643b3a.33.2024.10.29.16.45.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Oct 2024 16:45:18 -0700 (PDT) From: Deepak Gupta Date: Tue, 29 Oct 2024 16:44:24 -0700 Subject: [PATCH v7 24/32] riscv/ptrace: riscv cfi status and state via ptrace and in core files MIME-Version: 1.0 Message-Id: <20241029-v5_user_cfi_series-v7-24-2727ce9936cb@rivosinc.com> References: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@rivosinc.com> In-Reply-To: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@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: 9jj1wob56yxj95nd41p6pzzg6quyxss7 X-Rspamd-Queue-Id: 5FA541A0022 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1730245488-670841 X-HE-Meta: U2FsdGVkX19S8YY558vjvarg21rOoZmHu3Jd1j137obNI9qavrzRdEsCGba68Q/DzPCJbTx/iKuZdfHtFlVcxZ+WV7C71c0n2u6JHCgAOMm92H4MZLtd1VQayo4fxptgCTscMcc0aEybmK5aEg/a66JC7NS8XHo677mwG+fyFu3Pqbm2WqEzehSofsE80CMAMF/xTj524kLw+NH/OPW668SLCi1PqL3/SujvQixB8Yj5aeOgDgNAitFM/j3A82AUihEWkM4ioy0ZP3iurpw860csN9dpCrvEgQ8NY8AExIz9VGQqb2vUJYUe8iPQGcDHvl+VMnWaDQG0zDFzv6fK5Eereq/hNa8EO2x2IAmcTh41Xn0qLjGiiSFzeORdfT1mbjBSmDhouD/4NtDKRKyYGMPLwSCnvysWI/LzFaHqZQO7yk2VtONiGkNoahuON0R5pkUTzbh3ZlCDs+sQHNdusPOaetGFLS1jpwVKItZliKdzcjj2n+Y1GGUS2p32ewVrKou0cofsXA4bzAG6DMOSW5W7uoeYhc1PlazpU2+c7mvU26L2AsUHtS05+/K4CdJhAbFF5f1EruC5QSYS3fMv78ATO6dvvqzCFOukO2Os3hsVkJsj54698AtmpXBqdslJWpIJ/0KeaWAHyaSVHjx8aOuCIGWu9Xs7ZuYZZScwwmiiA66xh93STA9YOioyegENDu1rk1xCArfeRXdfT3ayLqC6E1xP/7MxQtk/7UmJBCKHkPw0rcZOw5GNncv1Rrtb101cGGuolRRbuFCKaFv37Kl4Ci3qo048b+dtTlPXQ3rnenB7h+LgI4Xd8r+g4GB8ZLESEBOWEe2rxGzMvsDak2dNhiHbDbZCJRHbsYVb/NpkJvA200l/BdpUQnPIq5a2xnEDmfn6eqL5MHRlJ42zoK/a+yfmTBlSUvebOyhRPFFpyoxpdTqIwV7GywufShOOo8s+L6SXHZKfZOSm5ef 6lGIy7fJ 1qyHIqgUJKhkvWRfS0t2iZjJEQsHrtWs4ShUzIl/yxExTCWGPWHORdP0d76Y2bonWXEDPIP+OCEbTSUFqekqjl3qUqEQtO3eVHb0BpyAgO0Plh2QVcKFgtD/T1oe/C9BaH9P7DufITs8MWlYf6xx5XXJYQ1jfk+h4PbR+SSq1nYzqCq9zrC4Ewf+MK5M8W1HvTpmxNgUAOVVKYWJKfrZXn7nKnFBaNuIja4UdHf83ZmofogN+bLnguUSWt75jS55e48DngzuvWJPpTYfWIh96YgVk/3bsbxkZXC7AGzae5Vr5ftGsHnu2IFJ5UxnTGIsWMbU7psxHuVxCG8RR++PkuuNFdBH7vy8WEuFCFUbM6S6raSKJF4JdTx51ZJpcanbEBY+Y64qIWV1gLQXFZlFCBSWxR6IUMiKsB3/H4jYvCMFbAe7XlslsnasdSpAhFCccafZfUuPQiu1s3Gk= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Expose a new register type NT_RISCV_USER_CFI for risc-v cfi status and state. Intentionally both landing pad and shadow stack status and state are rolled into cfi state. Creating two different NT_RISCV_USER_XXX would not be useful and wastage of a note type. Enabling or disabling of feature is not allowed via ptrace set interface. However setting `elp` state or setting shadow stack pointer are allowed via ptrace set interface. It is expected `gdb` might have use to fixup `elp` state or `shadow stack` pointer. Signed-off-by: Deepak Gupta --- arch/riscv/include/uapi/asm/ptrace.h | 18 ++++++++ arch/riscv/kernel/ptrace.c | 83 ++++++++++++++++++++++++++++++++++++ include/uapi/linux/elf.h | 1 + 3 files changed, 102 insertions(+) diff --git a/arch/riscv/include/uapi/asm/ptrace.h b/arch/riscv/include/uapi/asm/ptrace.h index 659ea3af5680..e6571fba8a8a 100644 --- a/arch/riscv/include/uapi/asm/ptrace.h +++ b/arch/riscv/include/uapi/asm/ptrace.h @@ -131,6 +131,24 @@ struct __sc_riscv_cfi_state { unsigned long ss_ptr; /* shadow stack pointer */ }; +struct __cfi_status { + /* indirect branch tracking state */ + __u64 lp_en : 1; + __u64 lp_lock : 1; + __u64 elp_state : 1; + + /* shadow stack status */ + __u64 shstk_en : 1; + __u64 shstk_lock : 1; + + __u64 rsvd : sizeof(__u64) - 5; +}; + +struct user_cfi_state { + struct __cfi_status cfi_status; + __u64 shstk_ptr; +}; + #endif /* __ASSEMBLY__ */ #endif /* _UAPI_ASM_RISCV_PTRACE_H */ diff --git a/arch/riscv/kernel/ptrace.c b/arch/riscv/kernel/ptrace.c index 92731ff8c79a..c69b20ea6e79 100644 --- a/arch/riscv/kernel/ptrace.c +++ b/arch/riscv/kernel/ptrace.c @@ -19,6 +19,7 @@ #include #include #include +#include enum riscv_regset { REGSET_X, @@ -28,6 +29,9 @@ enum riscv_regset { #ifdef CONFIG_RISCV_ISA_V REGSET_V, #endif +#ifdef CONFIG_RISCV_USER_CFI + REGSET_CFI, +#endif }; static int riscv_gpr_get(struct task_struct *target, @@ -152,6 +156,75 @@ static int riscv_vr_set(struct task_struct *target, } #endif +#ifdef CONFIG_RISCV_USER_CFI +static int riscv_cfi_get(struct task_struct *target, + const struct user_regset *regset, + struct membuf to) +{ + struct user_cfi_state user_cfi; + struct pt_regs *regs; + + regs = task_pt_regs(target); + + user_cfi.cfi_status.lp_en = is_indir_lp_enabled(target); + user_cfi.cfi_status.lp_lock = is_indir_lp_locked(target); + user_cfi.cfi_status.elp_state = (regs->status & SR_ELP); + + user_cfi.cfi_status.shstk_en = is_shstk_enabled(target); + user_cfi.cfi_status.shstk_lock = is_shstk_locked(target); + user_cfi.shstk_ptr = get_active_shstk(target); + + return membuf_write(&to, &user_cfi, sizeof(user_cfi)); +} + +/* + * Does it make sense to allowing enable / disable of cfi via ptrace? + * Not allowing enable / disable / locking control via ptrace for now. + * Setting shadow stack pointer is allowed. GDB might use it to unwind or + * some other fixup. Similarly gdb might want to suppress elp and may want + * to reset elp state. + */ +static int riscv_cfi_set(struct task_struct *target, + const struct user_regset *regset, + unsigned int pos, unsigned int count, + const void *kbuf, const void __user *ubuf) +{ + int ret; + struct user_cfi_state user_cfi; + struct pt_regs *regs; + + regs = task_pt_regs(target); + + ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &user_cfi, 0, -1); + if (ret) + return ret; + + /* + * Not allowing enabling or locking shadow stack or landing pad + * There is no disabling of shadow stack or landing pad via ptrace + * rsvd field should be set to zero so that if those fields are needed in future + */ + if (user_cfi.cfi_status.lp_en || user_cfi.cfi_status.lp_lock || + user_cfi.cfi_status.shstk_en || user_cfi.cfi_status.shstk_lock || + !user_cfi.cfi_status.rsvd) + return -EINVAL; + + /* If lpad is enabled on target and ptrace requests to set / clear elp, do that */ + if (is_indir_lp_enabled(target)) { + if (user_cfi.cfi_status.elp_state) /* set elp state */ + regs->status |= SR_ELP; + else + regs->status &= ~SR_ELP; /* clear elp state */ + } + + /* If shadow stack enabled on target, set new shadow stack pointer */ + if (is_shstk_enabled(target)) + set_active_shstk(target, user_cfi.shstk_ptr); + + return 0; +} +#endif + static const struct user_regset riscv_user_regset[] = { [REGSET_X] = { .core_note_type = NT_PRSTATUS, @@ -182,6 +255,16 @@ static const struct user_regset riscv_user_regset[] = { .set = riscv_vr_set, }, #endif +#ifdef CONFIG_RISCV_USER_CFI + [REGSET_CFI] = { + .core_note_type = NT_RISCV_USER_CFI, + .align = sizeof(__u64), + .n = sizeof(struct user_cfi_state) / sizeof(__u64), + .size = sizeof(__u64), + .regset_get = riscv_cfi_get, + .set = riscv_cfi_set, + } +#endif }; static const struct user_regset_view riscv_user_native_view = { diff --git a/include/uapi/linux/elf.h b/include/uapi/linux/elf.h index b9935988da5c..7ef63b2b67a1 100644 --- a/include/uapi/linux/elf.h +++ b/include/uapi/linux/elf.h @@ -450,6 +450,7 @@ typedef struct elf64_shdr { #define NT_MIPS_MSA 0x802 /* MIPS SIMD registers */ #define NT_RISCV_CSR 0x900 /* RISC-V Control and Status Registers */ #define NT_RISCV_VECTOR 0x901 /* RISC-V vector registers */ +#define NT_RISCV_USER_CFI 0x902 /* RISC-V shadow stack state */ #define NT_LOONGARCH_CPUCFG 0xa00 /* LoongArch CPU config registers */ #define NT_LOONGARCH_CSR 0xa01 /* LoongArch control and status registers */ #define NT_LOONGARCH_LSX 0xa02 /* LoongArch Loongson SIMD Extension registers */ From patchwork Tue Oct 29 23:44:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13855741 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 E6572D7494F for ; Tue, 29 Oct 2024 23:45:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 921C96B007B; Tue, 29 Oct 2024 19:45:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8D0A06B00C2; Tue, 29 Oct 2024 19:45:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7246B6B00C4; Tue, 29 Oct 2024 19:45:24 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 454766B007B for ; Tue, 29 Oct 2024 19:45:24 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 02279A018B for ; Tue, 29 Oct 2024 23:45:23 +0000 (UTC) X-FDA: 82728272508.03.FE185B2 Received: from mail-pf1-f169.google.com (mail-pf1-f169.google.com [209.85.210.169]) by imf15.hostedemail.com (Postfix) with ESMTP id 7E61BA0028 for ; Tue, 29 Oct 2024 23:44:56 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=pp3eXBEV; spf=pass (imf15.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.169 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730245361; 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=Kn/sn+D1z+93mY85aikbkRt+t3FRvwF0B8Ok3zV+OA0=; b=Tx6OGPrLw6YBEa5zPimS9WBhUb2GouJIY9CpHKK9FeRjWzE2hMVqaU1MJna4rIVn5Bm3o6 86a6vFejFxHqJKXp+eZdXsQi5EK/3bHZKUFIU8F7K2Kh/K9mGaECOuiW/tR4vgd+NeQAJO vcO/hdC5onat7ky815QFEhgtNJTtLq8= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730245361; a=rsa-sha256; cv=none; b=g2R/qTuAlNbacE9vksXhNj9eIbM1rXpRBqza8eH5+Aip3AseKgQ2yz5cXHUUQQfwWGqD8e fhCdKb6tDTux+pgLGgmeUQNiVX0ypG9G+5zhf/6QZlIJoXChSiuUlbqjBDnjYla1CGfDbZ 0sm0FoVVYUTX+XsSnhkqrgbGmTy/7hk= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=pp3eXBEV; spf=pass (imf15.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.169 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pf1-f169.google.com with SMTP id d2e1a72fcca58-71e74900866so4572155b3a.1 for ; Tue, 29 Oct 2024 16:45:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1730245521; x=1730850321; 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=Kn/sn+D1z+93mY85aikbkRt+t3FRvwF0B8Ok3zV+OA0=; b=pp3eXBEVr0tOH1eKVWnYuujDBDQt5BkK5O7d4z4e6OGKQ3dCuC8lpHFNX3yZ8KtXeI kBSkCo1jecU5n52mK1vb11IO5SAKS9gm4odxhS6zreG0kPDGtiojgvzpQaWCj0NirLd3 Ttt//Oc/+gJxPexTi49/hSDMnpZmO79E19Wn6dKHnIYOZxmTzIQdSxlgZJeeQ7WVq2D0 wJBDaFk7XehG2c204WgxmT1780SUbm4vhJtRqrZ7gcYjc3pnW6hcPeYziLV2dpwUmFfc KguwJin3mMeaXBuoVbb5iLATf0TRZqRbhPCDTAl/FqqDuw7tChIs6eNxMpNMngNSCUwZ 7oEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730245521; x=1730850321; 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=Kn/sn+D1z+93mY85aikbkRt+t3FRvwF0B8Ok3zV+OA0=; b=lo0C8SbQ1CjaW1b4OlagnGvQafU2IMfygOym/vX6mEG8bpbboK1EGUNi26Pyk3DevE QCie6Q0AOR6ytl4PTilAFM1Wluqy8PYI4POaEc54QaybmLnRQBSA8GNYS6h12PHRXpN+ FHaVGk/qNE4FRwMuZbWR0bmt2OIr2yWvZGH0GL4d1F+h1OfkBz5DQD3FSeSRNxE+ifr3 HWnrtQetrgjcWT6YFhsRYhnLknjIdnuTK+iFzk7EzUHqBPSScf9qKXJq9icBotK6UyVv 6oTPifj2WafIvJyv3bb8MwRmPwK8nxNkMUVNhWM/mMAXjTQxnVpgOaCee/rqC9zFkwLH t0dA== X-Forwarded-Encrypted: i=1; AJvYcCVUC+Rci+cWve5DaUIRZF4YnSbGWs/NZs2t7AicRXnLswyyzL+I4luLO8Sc0PsI1SyFlpR3TStApQ==@kvack.org X-Gm-Message-State: AOJu0Yy/WRth5VCPCiQPICseZ8oXl6FE8/G7vF03vM/TwbiHn4KYFpXb AN29wsW3P76clmdgazotn+fPJtcEV70vrGWYa439K49qdi9lpdwWbxlDkOtP+mg= X-Google-Smtp-Source: AGHT+IFSRRwDJwzMEik28WuiCu9TazkISCRtSEmteAY39kMNDbrF9hMfjaoxtBbs1QyHM1hw6YiZLw== X-Received: by 2002:a05:6a21:e96:b0:1d2:eaca:4fa8 with SMTP id adf61e73a8af0-1d9a84d9df9mr21491704637.35.1730245520903; Tue, 29 Oct 2024 16:45:20 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72057921863sm8157643b3a.33.2024.10.29.16.45.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Oct 2024 16:45:20 -0700 (PDT) From: Deepak Gupta Date: Tue, 29 Oct 2024 16:44:25 -0700 Subject: [PATCH v7 25/32] riscv/hwprobe: zicfilp / zicfiss enumeration in hwprobe MIME-Version: 1.0 Message-Id: <20241029-v5_user_cfi_series-v7-25-2727ce9936cb@rivosinc.com> References: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@rivosinc.com> In-Reply-To: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@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: wweuwo7z56jap3ih7899ef84hkrpddtf X-Rspamd-Queue-Id: 7E61BA0028 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1730245496-651507 X-HE-Meta: U2FsdGVkX19nXJziYBve7dKwGdxaNbn1ELBfXFbfQNord+InJ4VX1VduALD2TTfjjQByUzhj5iQeDhT/VJOXbOfDRUiu9NAd0ODGkKUuZCFnG46qC894QrHRDbsq13qS4A8Y3BXvHDpoKTOLNqpqf7Wx2+cgoDA2cIGe6rr0YUKkLakiJuH2E3HrtjSTuSwlRGW+SNPxzZ9IUP2hgairG81InAiJnQayVYxNjNyTxvPvuvqs/q2vIV2FJHu6i7MIyUbrm1bqw+DIq4S/XudUBoKhgufnrzHBI4I8VCkEbI+AJGWkPRbzzHGeDHnek0DwPfoNDP4vpRfePN3RPRcsI2AyYT5w5C6oVHBYx5EV4kJJQN8oJSKfISSB3g1QYbOXXvnht/CqFP5s9OikeHY9SuTZ0s0eIEFgVqW9yBfLU1jmiIGSlUoykl3cYdy7iOjAAeF5/rV34Kn4dlXWIU6u3Yf+MLD4Jr54qAGtYxMcr0HP59FGGOZOayL2QrkSU/HpObCpb6eYuBvRq265uYfOpKPrzWsTMQn921kVLoVGDNeiyK9l1voVpQZHBhQv4Uned5AuG5scchRoH8TOdVrjSehJYYO5krMIHNnA+7W8XlMth551VIK8pjnp7i0BlVjERP5NCHdRKmfcKGMmtxhiygxJaEIhzaNgX52sgMoHEVlnupF+CZPwb6k4QMnMP/10Zk8uhsAYfFIZelEsdePJlWqJTI5Xf+z7uInXydYMJIgQKs3f7zYsV1y28m2mZt+q3A2Qf0OS7Hnu0QOn97U0TjCPCc8NSVvBNcepaPPhbuN1Bx/rd7sNFsBjPmUXwCa1s/9gUEfJOltTYltLBynYEGRP6DOPFGad+ZlaPGNZNsWJe1ScbO02bkPTGwfVrmka5sW9kJAYfszdopJZD7sXNIoU961z3ZRfwwSYK6R8vwqrC6qG1caMXMbUtnkLP0HBU0ZpqYa5V9PfSXJ2u6n K5vnTmd7 roT6pORbRqr3D1ckAjPBTVUKBYYf3+LlMPCYmc8b00vGAurGyZRmWXgY851TbKxUuIqpwPGBuuuuilHiBQUxk0oykB2gTHSpz16US4PQtRu3+hQHjLq8zhXZvTzkw+DcTpB0RBw5u5arynTN9A3na1Y9N9IEr70c7F7CllpAfntwR2gnb5Nx83242qZaAMP59Qs2Tb1Od2wjfYWEf2PNZ5gonL8iG5SEABzjYw/o66nkmYjWL7mnCqU2kVIgP8HrcxoyI/+UB+RLXCEQ72gD9UeSHDdhtaVdKngqaIam+RsDsSRtZ3P4gUuB80OEIhjrIYxGmEtsAOFTEK3L8LkQYoJqo4RB5xOarpD2wRt2RWko/P05BNqyRlTBVKPMlewEmffBKv1FSEDVOBKwvfqj9YkrHj5HYA70uhtcaOJrsFLlOt/+pjTY7hGU+rETlyFtVDUrwZzfr0iRv9faITiRVcGMMpQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Adding enumeration of zicfilp and zicfiss extensions in hwprobe syscall. Signed-off-by: Deepak Gupta --- arch/riscv/include/uapi/asm/hwprobe.h | 2 ++ arch/riscv/kernel/sys_hwprobe.c | 2 ++ 2 files changed, 4 insertions(+) diff --git a/arch/riscv/include/uapi/asm/hwprobe.h b/arch/riscv/include/uapi/asm/hwprobe.h index 1e153cda57db..d5c5dec9ae6c 100644 --- a/arch/riscv/include/uapi/asm/hwprobe.h +++ b/arch/riscv/include/uapi/asm/hwprobe.h @@ -72,6 +72,8 @@ struct riscv_hwprobe { #define RISCV_HWPROBE_EXT_ZCF (1ULL << 46) #define RISCV_HWPROBE_EXT_ZCMOP (1ULL << 47) #define RISCV_HWPROBE_EXT_ZAWRS (1ULL << 48) +#define RISCV_HWPROBE_EXT_ZICFILP (1ULL << 49) +#define RISCV_HWPROBE_EXT_ZICFISS (1ULL << 50) #define RISCV_HWPROBE_KEY_CPUPERF_0 5 #define RISCV_HWPROBE_MISALIGNED_UNKNOWN (0 << 0) #define RISCV_HWPROBE_MISALIGNED_EMULATED (1 << 0) diff --git a/arch/riscv/kernel/sys_hwprobe.c b/arch/riscv/kernel/sys_hwprobe.c index cea0ca2bf2a2..98f72ad7124f 100644 --- a/arch/riscv/kernel/sys_hwprobe.c +++ b/arch/riscv/kernel/sys_hwprobe.c @@ -107,6 +107,8 @@ static void hwprobe_isa_ext0(struct riscv_hwprobe *pair, EXT_KEY(ZCB); EXT_KEY(ZCMOP); EXT_KEY(ZICBOZ); + EXT_KEY(ZICFILP); + EXT_KEY(ZICFISS); EXT_KEY(ZICOND); EXT_KEY(ZIHINTNTL); EXT_KEY(ZIHINTPAUSE); From patchwork Tue Oct 29 23:44:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13855742 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 850EDD7494E for ; Tue, 29 Oct 2024 23:45:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 885F56B00C2; Tue, 29 Oct 2024 19:45:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8332D6B00C5; Tue, 29 Oct 2024 19:45:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 687756B00C6; Tue, 29 Oct 2024 19:45:27 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 46E226B00C2 for ; Tue, 29 Oct 2024 19:45:27 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id F1CA38046B for ; Tue, 29 Oct 2024 23:45:26 +0000 (UTC) X-FDA: 82728273264.05.BCD7ED2 Received: from mail-pg1-f176.google.com (mail-pg1-f176.google.com [209.85.215.176]) by imf04.hostedemail.com (Postfix) with ESMTP id 29B3340009 for ; Tue, 29 Oct 2024 23:44:52 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=nHlamR92; spf=pass (imf04.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.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=1730245365; 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=7vRNSIBiNXIf2UJE+KhXd4ECbBBsrxcPJxpg99mApzo=; b=MPTKUFSPjXu/odNRg4ksnw1s8TAl5RTpLCSSamPa5ts5iNPwO3gwqY3xJ2Z20pyOvUuLiR 18Dyf7nClq9hNEMLubXqVPLcS2KGFSNPvz4X1HQTH9nOPXxu7OpUJblnsfcK3W5iUNetP/ zIcIf7IIvFozP2z/O7cS2JJUpqTTgN4= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730245365; a=rsa-sha256; cv=none; b=jqSTVddA7sgGuN6DNdf3a0kJGdAATYCFz29CFAlfZI71zkR4ncr22GRBzkxCgxCY3le4Eb unk/Wo0PJidmcZ+ATWj5KYdoRVHImTEktivCDhIwHSKbAjVA1aNzDJLgS6KHbNuK6oFSaL E3Bc0GSy6lBLCIvZ9AkfBjdZlClnPM8= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=nHlamR92; spf=pass (imf04.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.176 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pg1-f176.google.com with SMTP id 41be03b00d2f7-7d916b6a73aso3884616a12.1 for ; Tue, 29 Oct 2024 16:45:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1730245524; x=1730850324; 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=7vRNSIBiNXIf2UJE+KhXd4ECbBBsrxcPJxpg99mApzo=; b=nHlamR92Kd0tZ/ZSxZpZ3mR4IH6A6CDOZrc2Yt1t8hBhpFlV0g5aj7YYl9Rqy4rdIQ xTqIX1FEbOoUTQZNYl4xL6ts6hjwazwzMXGituMVKO5ulikoJQsGOukvEFgON2NkNbWP DHYREatlJwp3kd2RLMfMmotxdvgIOJEkxsRMM6prL1OltP6PI+BR4YGRsA2XBI6w/5UZ OsGgC55jXBSwOc6oZIp3XhxIiK3wCMuDNYLWOJJqhkLZXbeQyyuTQQJgmC/d45NhONIe oYTl8G0p+YSE/6eC9IwWnXI2IIDTAoRPw8oo0wOG5do5OoQGeCZeM/zfufxyxdWSUu9g jWIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730245524; x=1730850324; 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=7vRNSIBiNXIf2UJE+KhXd4ECbBBsrxcPJxpg99mApzo=; b=GNKSvBrijIzX5HJtA4x4hZ8LJMHVw+Eh3bJCWprq4NXyV9BDNrb+mkkXwi+WHC+EIu uMjtGuwCQtXCaaLyfDfUiP6EZCJ1GmneIFhDPVv1auSC3DQ5cNcBv5cJO3uRIrknQVRp A/3LIDo9I/nJVs/BJwrDhjNtvopp5kyfP1fCOWGIQ0sNB4/WqDcOenU2px05djtzCzc3 j3k4EVspzllBTfz/d6Dn/lcb6mTX9va/g4yn2uq+q9cjhL8gElJ85MCs35oz3tUoltrz IaV6bvY6JMkCZJKrpwpvJ3WtYvUB+yjBoLqSzfrVEKKiqShIHTXXU5wRW+DCnUQl4vVP dFOg== X-Forwarded-Encrypted: i=1; AJvYcCWq8qacFsAvV3u5jqdcAzBdVmXebD4Mu/i1U/Mv8gqul+a9x3bO6/y0cIANVy4IjQBNAQIh+ti90g==@kvack.org X-Gm-Message-State: AOJu0YzPlFWROZNoyRCZLvC2+qzIlo2CUBl37YQlRyW1FrnLFjnbCd3Z H4/kA43hQ7h/hwTDgX/cKTwx72BqNkxBhyTbdoGK28I9OqzG80J9rRyjBUe7ERs= X-Google-Smtp-Source: AGHT+IEYWYuZ3O/cciBKJ5slqaSkPGyDa205Q6lm+0Ith+W/z7NIQt+rMOIaCZW78WCxle4V1PPCdg== X-Received: by 2002:a05:6a21:3998:b0:1d9:ab7:a835 with SMTP id adf61e73a8af0-1d9a84d990cmr18634439637.33.1730245523952; Tue, 29 Oct 2024 16:45:23 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72057921863sm8157643b3a.33.2024.10.29.16.45.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Oct 2024 16:45:23 -0700 (PDT) From: Deepak Gupta Date: Tue, 29 Oct 2024 16:44:26 -0700 Subject: [PATCH v7 26/32] riscv: Add Firmware Feature SBI extensions definitions MIME-Version: 1.0 Message-Id: <20241029-v5_user_cfi_series-v7-26-2727ce9936cb@rivosinc.com> References: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@rivosinc.com> In-Reply-To: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@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-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 29B3340009 X-Stat-Signature: mpqtb3tq5hch4peqeys88xqed9a3najw X-HE-Tag: 1730245492-419369 X-HE-Meta: U2FsdGVkX18vZ06Il0kDLtW5AGd1qz9cMMPZ5Sy7Iy2cEgx+8r4m7q4IKADJ7bVx0W5Y5QyoY5D2Kmlw1a3/H9FUcyxAiZQ6EE27LrJfQd84tilRHLN6Lg/9VK2jj2JTFArxfe77pPHAXY8X3wv0xxovpZixdTCcX2y4fty9U7g3m3NcmMSavSUJZKMXVwvi5IoQ8xf/rmqHx/eNS91QYxF7qFatbFXa5zalx3Z0pIAeME9nD7eJgTvTsrigUR5Qjuj+zw166Yvxk0nrJ5ZTirVsDGQYewgMfU7mwFAe9oI45mKdArN5hmK84+tTtq/cCeEl5BM5hT7/44FjEH358LuDQTd9WeVg+5rlqh7li96uVeEpQ23koyWfTkzAYxkN15K7yNnvAlwCibkFlzlA6hHyq4kuAeLVOu7OFV32iACoITxMjvRE6mfQHSJqZXQfBVGYeI8MuhTWjvbO7Ra2Tnd9g7jBXfeYoOIIRLF1JxS4NiSctm6Wk0dAVXKnbDNV+yMv842Om5CzzOeJQ7z8y1lYHt5G2Rlc30XOalWoZHIHjeIX8zY7HVXabEimh4DruEl8UVQ/U3zJ+OqJFavX8M7hdgfPrM8PLXjCrNPX/yj3Pa9Br337OGjEJFBqL67JHq3bontcPbZmH8y8nIBdrA9Is5IvxqeSo4999AZOBSL2zBL7NKcvYCgixzU4JdyHPNKR1mSa1JD2bgDc6I6DIx0gP4qEvbY8Fyuo9CIDh9lcA5zn3qD1hqQOaRtrj5EwWkvd0qy3KY+Cfzcp8dJSXVG8vikYEz3VQ8Qf8NoIRo9UH2N9OlZ/fHEZcFLvYQNub0QIXQvEeXMHZsW00ISskTBorI91cb6I1b3qeDnZNPH8btqEhgoGZvuzW+IEf1iFcr97acTsWBE2Il0ifr9k/1a+a+Adf4/hKlWju4V5bQFIJTkDY35iR991IhuhTa7JJmN4KveyJ9fP6PrtIRa azClCTP/ NdngfqPfTHBcyQIsYjGEskVypjfQx5cm825xElbs+JhHp0RkeoyasZjyMBfbIqhL2G+n3EiIJ5jVcHrY1kniYe9ib+RhRsPpK8LDhb8gQ4Axm5lVqWUnMd2PPAy9mfWfmEAUVrnuv7XYaP7rFMOyHeCkxCJ1Bk65XTDRo+RHN2q1REbUGt3v3rcyxJJ3v66uUdlwZzxv3AK3Z/zxiTJJyLAeiyn8X1s3z6XCYTDlLCs/SF1mHC61IenFJ6UVvv5VdHyLoEdWyjlEVqt3mq1KEldi65bOiu0W5Dm2yKbjc4LkN53nRjJms/S/NG6WPweO9CfcPk7+IlO7zfOQf8hZCJDcBR0v96wEh5/ckaxd6yygAecva2aW5Fg2g/EcKWWZE/zO1JxB5tHmgWyRcMALUPKiQ+r4Zx+QCXxBj57TvFpoAWkdx/UIdNT4ei+7l6XQc0aaadYOA7RY1mzW8r7cvUf27zuufLeRCNd/KW7Y8qmahmfk= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Clément Léger Add necessary SBI definitions to use the FWFT extension. Signed-off-by: Clément Léger --- arch/riscv/include/asm/sbi.h | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/arch/riscv/include/asm/sbi.h b/arch/riscv/include/asm/sbi.h index 98f631b051db..754e5cdabf46 100644 --- a/arch/riscv/include/asm/sbi.h +++ b/arch/riscv/include/asm/sbi.h @@ -34,6 +34,7 @@ enum sbi_ext_id { SBI_EXT_PMU = 0x504D55, SBI_EXT_DBCN = 0x4442434E, SBI_EXT_STA = 0x535441, + SBI_EXT_FWFT = 0x46574654, /* Experimentals extensions must lie within this range */ SBI_EXT_EXPERIMENTAL_START = 0x08000000, @@ -281,6 +282,32 @@ struct sbi_sta_struct { #define SBI_SHMEM_DISABLE -1 +/* SBI function IDs for FW feature extension */ +#define SBI_EXT_FWFT_SET 0x0 +#define SBI_EXT_FWFT_GET 0x1 + +enum sbi_fwft_feature_t { + SBI_FWFT_MISALIGNED_EXC_DELEG = 0x0, + SBI_FWFT_LANDING_PAD = 0x1, + SBI_FWFT_SHADOW_STACK = 0x2, + SBI_FWFT_DOUBLE_TRAP = 0x3, + SBI_FWFT_PTE_AD_HW_UPDATING = 0x4, + SBI_FWFT_LOCAL_RESERVED_START = 0x5, + SBI_FWFT_LOCAL_RESERVED_END = 0x3fffffff, + SBI_FWFT_LOCAL_PLATFORM_START = 0x40000000, + SBI_FWFT_LOCAL_PLATFORM_END = 0x7fffffff, + + SBI_FWFT_GLOBAL_RESERVED_START = 0x80000000, + SBI_FWFT_GLOBAL_RESERVED_END = 0xbfffffff, + SBI_FWFT_GLOBAL_PLATFORM_START = 0xc0000000, + SBI_FWFT_GLOBAL_PLATFORM_END = 0xffffffff, +}; + +#define SBI_FWFT_GLOBAL_FEATURE_BIT (1 << 31) +#define SBI_FWFT_PLATFORM_FEATURE_BIT (1 << 30) + +#define SBI_FWFT_SET_FLAG_LOCK (1 << 0) + /* SBI spec version fields */ #define SBI_SPEC_VERSION_DEFAULT 0x1 #define SBI_SPEC_VERSION_MAJOR_SHIFT 24 From patchwork Tue Oct 29 23:44:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13855743 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 E0235D7494F for ; Tue, 29 Oct 2024 23:45:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DB7DC6B00C8; Tue, 29 Oct 2024 19:45:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D144B6B00C7; Tue, 29 Oct 2024 19:45:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B6CCB6B00C8; Tue, 29 Oct 2024 19:45:30 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 987D36B00C5 for ; Tue, 29 Oct 2024 19:45:30 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 5A8F4A0166 for ; Tue, 29 Oct 2024 23:45:30 +0000 (UTC) X-FDA: 82728272298.17.0E75CF7 Received: from mail-pf1-f172.google.com (mail-pf1-f172.google.com [209.85.210.172]) by imf12.hostedemail.com (Postfix) with ESMTP id E99884000D for ; Tue, 29 Oct 2024 23:45:16 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=rVBMCE0U; spf=pass (imf12.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.172 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730245448; 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=4xlMf/FGu+cOf/Hv7qUX6Wk1q9xFnQqCr1v5Bv8qZmQ=; b=awMAi6IYJJEXH5KfXei4mPne3pJBlWTen5DH+yPbg5KqFvWWH6B6dwBMIGintzOLLTHZfB bC/T0UP//z559qT4jyAdQuiyB9U19d4LTdksEUMbkybyUVEzDwTAzGw8EehpiFSfNqEpsq NVFvwVqlM9zuaF2zRwMgKJuCqSwux/Q= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=rVBMCE0U; spf=pass (imf12.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.172 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730245448; a=rsa-sha256; cv=none; b=Rkmgk7UhLYeFerBTfOTSlL5xIRCN+mUexAe60uIRFUAG6jYpP93MmF9rohDi39bqGUtNLj 6dDJFohxUOwPRqSJRT5CAP3bN1CVA5pM7z0AeYp4wO45TngCxd/fqO1BdmYaqgbkl368aI 3xR/AnoeQVr/Nghexo3QPr/1DxecP7o= Received: by mail-pf1-f172.google.com with SMTP id d2e1a72fcca58-72070d341e5so2665779b3a.1 for ; Tue, 29 Oct 2024 16:45:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1730245527; x=1730850327; 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=4xlMf/FGu+cOf/Hv7qUX6Wk1q9xFnQqCr1v5Bv8qZmQ=; b=rVBMCE0U/ueE/wLfwgcQgo1933yNMVMOXeF1bZINHkyiqUcHOVQ3sis2PlqwwA0kYf MldxSPHABLOkJsCQPn9z5kmGii8h7i66dDZ2T0QCFTMnJi2xtPiWaTtiaD7jKl/IxYyd byJSmgOwKNzg6juzIhysFYB3fXW5hoz15pvPXqYa22XYeXBsJYw29XC0rrtz/BmVSpP+ AQpXtSrkCQfONTiU9LNAQhUUpZfWDPa2z22/K/PQ/VzTvgjttl9WSlBNlKb5bQJR5HhG 3a1TbLWp/syyFgZIohF1gbkdl5U1NE/4NyfxcpHZ4LH7Gu5gxsjyXtSO9RStN3/XRv6I XXjA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730245527; x=1730850327; 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=4xlMf/FGu+cOf/Hv7qUX6Wk1q9xFnQqCr1v5Bv8qZmQ=; b=PW3FG8wYiP3eHoZ2TvjuxHYIqLZ/owz5RIJMYQVotcb7qvKckQFGfHFSNDDNUG0nb2 kfjLJcRj1HfdOzQy+WXJPEpv+nDx+CVAzLNrN0lny6/H87RgJ2fCuf35HTiOMnhheFVG 5c9X1tggzLBM3aUBoYBRennRYvki28LTDQyrhwfRnKKd/dc1E6FLaXzHzSkSCVVm3bTO 94UNObunA46UlUZwahtL2ruq2cIbSESv4dQmvbKNcHf/wagydFz69fAIS4AN7+ap0sH+ QxyHw48TpTumgX191eUJu7CECsN19YKKmBu+51K4u9ocwao4fAyqR1Oeqi4ZB/vAU2ho oqxw== X-Forwarded-Encrypted: i=1; AJvYcCX97Fr/tW5bCT9jPB4PTcZhZowkZVxz2Edf+RFDmdufoGCMf4Be1ADcSoxGAtp4KxFECgRIKPgaZA==@kvack.org X-Gm-Message-State: AOJu0Yy7SbmZkH+jlDC90Qql7t4D3k6gTXPFx5zl/j/S8FU4q5j3AtEU R8pZ5at1mlOPly5MZc9r1t+jUOPxQVYWSYjVOK8v5WRCAPEg2Rt/HspNucIXe2Y= X-Google-Smtp-Source: AGHT+IELr6IagcEBTdUxKgRdLblmitKpEm/GHFYUpbKbGEH4mdOch+QEksQsRpJQnLoaEfYjLi/gYQ== X-Received: by 2002:a05:6a00:2d17:b0:71e:5fa1:d3e4 with SMTP id d2e1a72fcca58-72062f86747mr18633884b3a.2.1730245527282; Tue, 29 Oct 2024 16:45:27 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72057921863sm8157643b3a.33.2024.10.29.16.45.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Oct 2024 16:45:26 -0700 (PDT) From: Deepak Gupta Date: Tue, 29 Oct 2024 16:44:27 -0700 Subject: [PATCH v7 27/32] riscv: enable kernel access to shadow stack memory via FWFT sbi call MIME-Version: 1.0 Message-Id: <20241029-v5_user_cfi_series-v7-27-2727ce9936cb@rivosinc.com> References: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@rivosinc.com> In-Reply-To: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@rivosinc.com> To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Paul Walmsley , Palmer Dabbelt , Albert Ou , Conor Dooley , Rob Herring , Krzysztof Kozlowski , Arnd Bergmann , Christian Brauner , Peter Zijlstra , Oleg Nesterov , Eric Biederman , Kees Cook , Jonathan Corbet , Shuah Khan Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, alistair.francis@wdc.com, richard.henderson@linaro.org, jim.shu@sifive.com, andybnac@gmail.com, kito.cheng@sifive.com, charlie@rivosinc.com, atishp@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, alexghiti@rivosinc.com, samitolvanen@google.com, broonie@kernel.org, rick.p.edgecombe@intel.com, Deepak Gupta X-Mailer: b4 0.14.0 X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: E99884000D X-Stat-Signature: a68srtw753fjpo6tb6gt3cmmh414pju4 X-HE-Tag: 1730245516-750216 X-HE-Meta: U2FsdGVkX1+9b/TdUEKMBW/kvpRn6OMRprve5gw3c6ihBLy45hTuOrcHaDROcT6pcRDvWCs0exjzJquvHZ455b5jlmywbWtRVOjVCu0XlnA/JvcDu6QgxBTWnDd4lN3S71rnE8XHPq5vqyZ6oG18UpeRXZwkvYv0sZ0d8M0UjjgMEgalBj3k1+1hY3qenczlxVsI2MQF6oZv+r7h80IKpkc4qrwRsoI2Jc15FnRwYRclDCyy3HBekWA+BKDHQb7OFgDOLz+2jhGxnR3gdFMOMzBvhfAyZIaq6ei8A13hun8r8gv4bNpt2TE1a7sNPPC36RNXjd5D29PxUWLVh91h9RvQjbfbGJ9MS01C638iDURq0UI/0VRm7JsABpbERs7Rv5EBCBtDLJsAD8iz8nu2LhmF2T5neIVwpqdw/zEzDbS9a93pap+k16PUss71tRi165oUF7zC3Y5VnpnugiIw2CZpRx/yn3t2Su9UumjnixfFbfGiZmOakZxEA5l4UPnvi4gIPjmrObQ8P/oxSFAxtbjIkIyrAukhofVxv/mMf8o/jZ3ed899bvQk2gDcg6XPsOTlPZE7lj2CjcXGn/wI6IMAtqtPw5+0AAdAX1ZHCvoS4d2NTRnLqPyTpXrKzj1Mpr0KYdl7Nk/FWxHaigyI6C37HKvsXGlZLYut8Mu+zl1JmIuAFiRminBRoEo5h9p8KlBStOB0wOQ1NYvYKRqFBmcaGjoLxbh8fn8QTc6jniidPvabdSGMDmb5yqEzSZ0lfaDZnWhpcYX5uVakNJHSRrdpEDh09mSn2qXaZJpMtqN3IRAvpif8bt3Pl0fpfQH36l/8oLbX68h0tQViUcEO2+SE0Lr2pln0gNFQ9zgwtOmn0hfYHyPJyYxRJYCI8LLz7q7yT4sJoxOSm0Gf6UM06OElxqGW9dVC3jdtR9rAUqef23yeISmfi77dV3NLXgQwKkEuKobNeW5T1VWr9Ll gNjTZPwm EfrOnDyFtJ6yfzUpaDtES4J9nRemRMtftig7q0W14PizoBuTGKG8k5zh4f+zUzChtpB1FfdaJkhC+ne/dLO5og/NWpTfv5AftO6c4j/zdeKsWJ0w9zivK7tUe3624elXDG3h9alT2vXfMkBlG+TxqqJFIOyWHVXYh0M/Tmb3FWAvrXPiIw3fdYZ0O8sruVdtnvT8NGxQAuRdSKd9p793AL3A7yqys5beugq6gu/pT7dW50Gd0Rtpuf/1KUR57CQD6H03LMmFgF4tr4/EjzK3DxCqhTSPZxS8Rst49648yy30DzEXkNbGBFiVAn/m8bST61JELVkWHI8J+f1Hpndldy2GvZeJDnpKLq/VlecA4y271353eVWAaYbrBWFpiuOCpcb5wA/vmgFVV7zamd0i5H4dk8YIfn114N8uVROebjNjr2jYFBggfYThxXnvQFtGy4W6GnVIBuCCeSi0= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Kernel will have to perform shadow stack operations on user shadow stack. Like during signal delivery and sigreturn, shadow stack token must be created and validated respectively. Thus shadow stack access for kernel must be enabled. In future when kernel shadow stacks are enabled for linux kernel, it must be enabled as early as possible for better coverage and prevent imbalance between regular stack and shadow stack. After `relocate_enable_mmu` has been done, this is as early as possible it can enabled. Signed-off-by: Deepak Gupta --- arch/riscv/kernel/asm-offsets.c | 4 ++++ arch/riscv/kernel/head.S | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/arch/riscv/kernel/asm-offsets.c b/arch/riscv/kernel/asm-offsets.c index 766bd33f10cb..a22ab8a41672 100644 --- a/arch/riscv/kernel/asm-offsets.c +++ b/arch/riscv/kernel/asm-offsets.c @@ -517,4 +517,8 @@ void asm_offsets(void) DEFINE(FREGS_A6, offsetof(struct ftrace_regs, a6)); DEFINE(FREGS_A7, offsetof(struct ftrace_regs, a7)); #endif + DEFINE(SBI_EXT_FWFT, SBI_EXT_FWFT); + DEFINE(SBI_EXT_FWFT_SET, SBI_EXT_FWFT_SET); + DEFINE(SBI_FWFT_SHADOW_STACK, SBI_FWFT_SHADOW_STACK); + DEFINE(SBI_FWFT_SET_FLAG_LOCK, SBI_FWFT_SET_FLAG_LOCK); } diff --git a/arch/riscv/kernel/head.S b/arch/riscv/kernel/head.S index 356d5397b2a2..6244408ca917 100644 --- a/arch/riscv/kernel/head.S +++ b/arch/riscv/kernel/head.S @@ -164,6 +164,12 @@ secondary_start_sbi: call relocate_enable_mmu #endif call .Lsetup_trap_vector + li a7, SBI_EXT_FWFT + li a6, SBI_EXT_FWFT_SET + li a0, SBI_FWFT_SHADOW_STACK + li a1, 1 /* enable supervisor to access shadow stack access */ + li a2, SBI_FWFT_SET_FLAG_LOCK + ecall scs_load_current call smp_callin #endif /* CONFIG_SMP */ @@ -320,6 +326,12 @@ SYM_CODE_START(_start_kernel) la tp, init_task la sp, init_thread_union + THREAD_SIZE addi sp, sp, -PT_SIZE_ON_STACK + li a7, SBI_EXT_FWFT + li a6, SBI_EXT_FWFT_SET + li a0, SBI_FWFT_SHADOW_STACK + li a1, 1 /* enable supervisor to access shadow stack access */ + li a2, SBI_FWFT_SET_FLAG_LOCK + ecall scs_load_current #ifdef CONFIG_KASAN From patchwork Tue Oct 29 23:44:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13855744 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 65346D7494D for ; Tue, 29 Oct 2024 23:45:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8FCE96B00C7; Tue, 29 Oct 2024 19:45:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8A7E26B00CA; Tue, 29 Oct 2024 19:45:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 770E96B00CB; Tue, 29 Oct 2024 19:45:33 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 54AE96B00C7 for ; Tue, 29 Oct 2024 19:45:33 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 1727F1A054F for ; Tue, 29 Oct 2024 23:45:33 +0000 (UTC) X-FDA: 82728272886.07.1562ED9 Received: from mail-pg1-f173.google.com (mail-pg1-f173.google.com [209.85.215.173]) by imf07.hostedemail.com (Postfix) with ESMTP id 3AC2D40008 for ; Tue, 29 Oct 2024 23:44:56 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=VEPefLcK; spf=pass (imf07.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.173 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730245450; a=rsa-sha256; cv=none; b=umlODLXiKSBCEzrrGKdVsYmP9u4mm/B5xaZY7/6PqdAeZGkljirm+Y0l+5rVxyiM3Psqgl esGhwksYbTT/4H/rabILEGvp+Ll4uT8iGppnRWCHw1JQfm8ugHWcY5c7oETI8KF8bRIP+L Rd/RtJp8Wg9U/8CKCWRy/bfGHgDAmFQ= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=VEPefLcK; spf=pass (imf07.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.173 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730245450; 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=nRy4y69JgL5sB6geChrc5T0OHtDNFkvTlVG645IsDrc=; b=6NO3eI0T7gwNxX/QfDPi/w3hvTJbtfCXTZzrSqZRJxE3rUIY3bSwX/FhwXUkA7ckxRf4tV NaCfEF7feWcvBWPqEHolfDy4+yFEMWDun+x0K2DHk5i+FWZ1EpwKoFhrwnHFBR8WgwKtoB 3/l0iJEJ4GZfrBhrT5uWwCD77L5d1NY= Received: by mail-pg1-f173.google.com with SMTP id 41be03b00d2f7-656d8b346d2so4118761a12.2 for ; Tue, 29 Oct 2024 16:45:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1730245530; x=1730850330; 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=nRy4y69JgL5sB6geChrc5T0OHtDNFkvTlVG645IsDrc=; b=VEPefLcKz50v9c6PJzaKscCPxpjPou1+aiSIWZOK2gV9rRNtIOopFtIaMk9lXe8GIn DI+aEfTywGbeDZP6Zk97LbWtL+yLvKEF6oMS/uasmRUFFBe23dwD7kmjj+KUe0pFDLXl 87d5jZxr3B9ciPov3HwDgDSWBnZNNyV/Z58kcdBsjiCTFKa4QRMsErKCLPf68U/4iRw0 usqaW/qbHXx8uvB9kRpJHCd0VFDiTrsu7t/YV5mcT54fYk850NmMfmQ2TZaIfp7Ah58t HxWa0QByMPa4NklBK8s2H7hkxZ7jpu1lAoo3CI2Sj9zHkXzN7iqRqJPg3fd4Tlzi3nhN uqLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730245530; x=1730850330; 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=nRy4y69JgL5sB6geChrc5T0OHtDNFkvTlVG645IsDrc=; b=Ct+keSKse0Q6M7hIhEPzYY7fbZEtNXi5pWACfdgTL8aV5jmqOAPCxkhocndXG2qNGF VOQ66C6GzImY3/hYalXdepr5BB5iDVfXfqlRNsDb2H/KuUXDE+qxTT1P4B+dGvwUnUmu 2AY+zKgDFGCHa0YKHRWd3omu0LxV+Sj3D2E8y3nQqm2dHcwsnxP5V711VD6GKWgjWPv6 8UeSBv56XgpajtPfAUTMEpUKOHxGDrClx+zbgFMapdiXgCijX3koP2Hia0sgU9ZSXEtm 9XNpamLxYhqCzqNrqlYaa/Pf5STcr0N9LGUvfMpU+BQwHht6tlkBcKrhXV9InPBYXSo0 G2EA== X-Forwarded-Encrypted: i=1; AJvYcCW9Ti8s/2tMiVfaFH3bcB8L1vhnIFxHiBSMtZklTEANuIJZ8FMVoEPI5Cp3lJVTlR1nWDtJSJVDiQ==@kvack.org X-Gm-Message-State: AOJu0YzldIAYrgOgAR02wWV2Oo+iPiffMMr+kbbXO/0dQaVkyy3NIJr0 AWGzuq6TUB4j1bbckXmn5ThDNr+de/PogQ+16u78UYlVV6EdSUIF6Z1aybi5/Hk= X-Google-Smtp-Source: AGHT+IEJnoB5pjGPkuu5fVIOqNL3hz9Y12W/UY6bmoQpgc9NgKdtyZXYcI4NaGAWLxyKkPpDfpG9Xg== X-Received: by 2002:a05:6a20:e198:b0:1d6:fd8c:fa16 with SMTP id adf61e73a8af0-1d9a850541dmr17895816637.46.1730245530007; Tue, 29 Oct 2024 16:45:30 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72057921863sm8157643b3a.33.2024.10.29.16.45.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Oct 2024 16:45:29 -0700 (PDT) From: Deepak Gupta Date: Tue, 29 Oct 2024 16:44:28 -0700 Subject: [PATCH v7 28/32] riscv: kernel command line option to opt out of user cfi MIME-Version: 1.0 Message-Id: <20241029-v5_user_cfi_series-v7-28-2727ce9936cb@rivosinc.com> References: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@rivosinc.com> In-Reply-To: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@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: aprnw4k1ynao6shat8s6hapmzdhykqhe X-Rspamd-Queue-Id: 3AC2D40008 X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1730245496-194348 X-HE-Meta: U2FsdGVkX1/IsUiSCjSpujxbcpCJO5aZH8Nb23ZUcp0xTdgUDY7sDnTe8Lv0n9H0MrelWFl3WZev5tW+IaJssiI3WKwcA91nW3draxQcC7jXDGNyQ5Wk6MsiIEFnWJ4jRiC+cWoseXyF/8G2W8fDup2rTuNwyBJTzbJdKv5zRfGrSRxiZk310s/OCHGidZDzSh9bWbC1TcmaCLx8Hq4PQI4gTKLcCKs/zwHKv+hXcEFZzPt+wzPycsH2eMdokHfQk+Cr65Zo+klTvJ5ZN9oUbpes8XwaZH1u0QArgDR0PDI8IjHtxcKVToshaIWhIyfH/QK0mt0zggtz4BCep2ftsv82cCSQDeXdjbNbo2k5+bVmMN2qz+jyuaqesc0gSJkqiWBJP3IBUZ2zGcThxvEwf+Ni6h1Cc30FLVDusPoWPy37E+Z4J5GwaWqGkNVEEfAMbN2Ur2a7CQzdTD+1+4Z4ys1SP9bcy/Nrb3Ii2BLeseV6nnLn+FwdXk8K7dDJXUbwzk1TrypIsx2MLC5qy9CxKa3OQEnnN1gVx0XRSoW5WLFAOkOrfZjq19fJ9c2gw9EpJ26yhEAqYhdW+SvMzdaXvlZ0jyJN9kIUQbPvkwEGkkuC89mskiOelbhNsFuUSMwc4608P3N3i4eUVl1mM9pkkQEs0R0tr/V40822qwoh+rK+w+6uQU3ORtBvH4rEmXuWV+O6PhXfPjiqJsnGxAr6m7RV0WLsfDl9jF4hv/k0FWmbmBTTiBAC1HFTQVfcs97eZFIvee3Tiidgup2WJ9nBNif4nKMgoaw0Hms0OFEy3F3qvbypyu1Q6Zun/7S8NVLCkSqNQ+0zIHo9enXooY/FLSiziai4IOsguMCHbaThYvRvG4fyVgFeYAxKDJ5uraUXThPLLRfThqg58jw4fA3VwKAkXnFZ0+EnaA7V6eBuKKSK58KyBiNZzC7ti52EOq3O6kAocQCldxdi/yfmvvj oBaNvoeV 6q1M7sco4eFrai5Y+6kdJu7KmJbU1W56AaQTbXcQgMXN/knUTM/20nou+BZfLPXNdUHCYp3/rBcI1ZTqPkCpVGILAyVTb1ga8ks4jztNgoM9r9QAt4jl8vNWbTbXUhdt7iL6AJx5Nf+h6bYg5Jw/AT7qOqFlZw8GFuQ9Kn4DFvul2giXxjCiEHgQjt35QQqRch4rslsKbYYEL0I8xJWNACGMmYMI9+usEWS7BYwuP4d5sD801skAJ8JcCfyrFq4PxX7Uf31ObH19v8Ue7nAVEs0QxSsqzuRUwGoA9vNwwscXpgalnutSXrQywYn8XsT8Mo/QXnAL1RhFFYK/xWCDBupw+12U3bRPczZqupoL3lUOSbLEt3mTGzoytChC5xG9g8SKB0EZ8wpC7JmU/rJoqRGolGrU4AzZ6mG0bmO4njm3KOYYEuGwOO6B1I+OdGokUD3yDf8ALZQn6KtY= 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 Tue Oct 29 23:44:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13855745 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 C303CD7494E for ; Tue, 29 Oct 2024 23:45:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 290F46B00CA; Tue, 29 Oct 2024 19:45:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 240C46B00CC; Tue, 29 Oct 2024 19:45:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EEA6E6B00CD; Tue, 29 Oct 2024 19:45:35 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id D0A966B00CA for ; Tue, 29 Oct 2024 19:45:35 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 910271C618E for ; Tue, 29 Oct 2024 23:45:35 +0000 (UTC) X-FDA: 82728273978.29.D5386CB Received: from mail-pf1-f169.google.com (mail-pf1-f169.google.com [209.85.210.169]) by imf08.hostedemail.com (Postfix) with ESMTP id ED32916001F for ; Tue, 29 Oct 2024 23:45:16 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=cKMCNvKF; dmarc=none; spf=pass (imf08.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.169 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730245478; a=rsa-sha256; cv=none; b=yzs1ZU8U1z5i1GdopIp63iNzyHlHp2AdrIbRAUjMIAZ+4KiQRXCuWEL9h5Jd9JHynBILqK kEtqHWJiWs8s4AKrVS+2hffPf1z/kW6xUbfEhWVVTzu1PrCy7XuBSWvK99r0akGRY52MiI RV0j6UqL7kY8A/HKSEdkB332020qD20= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=cKMCNvKF; dmarc=none; spf=pass (imf08.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.169 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730245478; 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=Ev1Mbj3LeOLd13TT7hxU6pIG2vcpaYTYXYx00sbXeTw=; b=596RRZV96QlaT+Kl4Si6DPu23a0bSsKmI7afRyt23MBKxGJ+r7jb//JBL5u1hfT3OMJFma 5Xv7qdHqS+6omKy9TyukvRfcVlBfAyni7HYyUm5bkHH18DibqAmXtVqkxaBDdCJpiWE7wu mCglsm67l+WhOOk+gwUHDR2IHrNV8ZY= Received: by mail-pf1-f169.google.com with SMTP id d2e1a72fcca58-71e7086c231so4671875b3a.0 for ; Tue, 29 Oct 2024 16:45:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1730245532; x=1730850332; 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=Ev1Mbj3LeOLd13TT7hxU6pIG2vcpaYTYXYx00sbXeTw=; b=cKMCNvKFKzls9a4RDnBg0lwwzeW/8LVBHhtz/407N0vvZcP3ouvfWjrPcy/XfjFIq8 ouKdOjsXDvUJBkE1KopPc5afa5XAZavFO2LtyZDNPcY47F+sFTwgt0XimP08aTtLRsa0 Ti9N0Pm26INZCyQ/suxeYl7F1PiNAOfAoLmdBr2EEKFluZnywkBPewQRSLbmmgF8ro82 z676bmqUogPlAu9+dEaNyweFffrGHxCiICAY94MAoyW1uXeX/sdE2Gmh67Oz4XuATHQE 4n4dWdPcOBQF2PCzVEP9N2SKwkWTVHnWF9E8iKw0LBMAlzXAtLwZUn4XDQZ/JSLgfMFH 4e+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730245532; x=1730850332; 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=Ev1Mbj3LeOLd13TT7hxU6pIG2vcpaYTYXYx00sbXeTw=; b=Po5z22+x9w32pK1ThttT9h0FS5cE7ccz5dZg9L845IUAgUni/fKjNEz6xDIvu4Ds/X 3AQpj7H6oW40mVdWLq0n75MRmGbpLz4GsAkwvMowsduP7RD10PoLjq1Se5tZICii9lqm O4DiP45pSTsWOAd3Lti0U/7v1d5wz9C9zJuWyTbRUdezoeRXKEDoSKowrX9HFbUySGbJ MS1ZvFIgPv7kxUK9AbGhsz8mVVbFPj8Ihb9DTrNzlEjluEAvZb/BjRg09cMy0wcsVrug dk9Mq7Ne9E5U7loOFEk/ujkjzVHuUoFTv9tjSrChVxDq/g7/P/Gv8IK4SofIsOgXArNC rD5Q== X-Forwarded-Encrypted: i=1; AJvYcCXFyvDtb6rj/Jebv6QBE5uve8G4KEnVdKNJ2xU9j6YisztxfMO/NGT23ILPjNockp5vKnfTnBhxrQ==@kvack.org X-Gm-Message-State: AOJu0YxfWCkHAWYQVY9TmPqJuYf3AYTC+B/7xx8mhGw3YBfc1MM/7+BH 49w4paGA7WCMkvu215IP8GDBeSijKpfjB2pTLu0Ax70pbX1VBPvBp1hqsiFXMMI= X-Google-Smtp-Source: AGHT+IEfSyF62ivzJTKgFpp0kGdt3sdSzoahMwleruIgutJftkJDtf+kKS4NEl+lwlKC7xKag1P7PQ== X-Received: by 2002:a05:6a00:18a3:b0:71e:6c65:e7c8 with SMTP id d2e1a72fcca58-72063093579mr19109726b3a.23.1730245532485; Tue, 29 Oct 2024 16:45:32 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72057921863sm8157643b3a.33.2024.10.29.16.45.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Oct 2024 16:45:32 -0700 (PDT) From: Deepak Gupta Date: Tue, 29 Oct 2024 16:44:29 -0700 Subject: [PATCH v7 29/32] riscv: create a config for shadow stack and landing pad instr support MIME-Version: 1.0 Message-Id: <20241029-v5_user_cfi_series-v7-29-2727ce9936cb@rivosinc.com> References: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@rivosinc.com> In-Reply-To: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@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: ED32916001F X-Rspamd-Server: rspam01 X-Stat-Signature: txerwbpo3khsi5u5jpsw63itbnqkptmr X-HE-Tag: 1730245516-523543 X-HE-Meta: U2FsdGVkX19L5SuDaSHKQYQmR7r1FjDLmC2iWvB8N5gny/u25k/t+sPuQDO6UCCp8vQAJhUHmb5VDhfdjVf2622OqVUgn+jMBrERiZuKpFZzvWc2f4nYGpa7jt4zSMkPyDnBee95iH5L6d8yI43mPkGS0aiGwWbbqtGDolplQjnX2p0sPL6ayjT3bXESeiHt/OiPaiACF1N4kPq0vx0rgAThU0xPIIrRy7hJr5qwqDo8FXwADTM42BiwlGGVq5YjT4UwVi4YgOZNoX5ZS7h2390tGnkQkcNaX2rI0N38ttIp4niucCDjCkbTtGBxamR0qUYxSDavdthq1YmFxLq3xImFxVgk18+bxrM4/ZynFN7pMoRDf891Zuz6ICz5RFxg2apzWbhc/L9C4aSIgu8V3Kjb/5thwdH4EwD9eGVDo1rDDyn0/sAeslTshe+LV4WNMhYYDkap6k3OoilDbMr7uGy++2qeLKJD44mE+U3mPxWGVLmS2SvDxAH0ngftYlc/mW9qz0tBVHdFr/7jxwCRh29t7U1UEwPQR6odKV0AgzHv4/RpLimwG7d2zgIpVw5ld+hUs1/fOSCKb/StouKOm15T2N7C53BGY4pab48B6nOLwunLFJn1vDI7N1G/icCwC+ufaGAYJhMUssWTK9YZi7yplJG/dCjVMw4DSEtiLecrzFGUg2gc3LE60eXeCnxlqdMkVtJuyXtDt4d/yH3btkxHz5OYf20oZuxkLULY1ktwJtnQ7x/HM4/sYjsCRwB9iQbT1YYkZ7M77P2MtsNMpBKKwbnxj4aJ4nRjNBBj6apg1/ikyfM9TSOU1Kyvz+0hqMkXn4P3wzUdlGd0uYtETmU6gUaPEPnaZpCAXKQdj8EHSg/xNtAEjA7YeysK3omwrq6glgC1diytubjxm5x8hzsRvSikvoFtco1LWPW+7qJPD/BM52g4LgrNt9hDyRC7l775hRSJZs/PszJIrS4 FhInAMVf v1aMvZfzkz9aI64zvuNOjceV/TD//kGi6esi7NfYke29GKCiC1LYfel1YBi2KXH/3W1y+kwcnysM6kf3q5t7L64teKnkOFRaCKareCZISf4SEuwt3V8m3LbR6yVwFsx9RrdfW25HUEBjeMmSq3HGvt6IgrwmsagNi1Z8wlSxXHj0al/kXNIjGVruGA26FnaYO7PgmBNe2PnYsvtRPVIHw1Etdxnd5mWa+uE0Qu4cl2LaEDtBk1pjhajEtQi6QXZHpXOoroh1QMHXxtMhGdTN0qiBCEThpbE76kNmy3rJ2ob9Y+umDSIv8h+jqd1PArSDyEKPHs2Q+ZypSeLXetqJYelElZWuDjqY2vw/rHpcgwE/rvGLr6Pe1yUYOBxFG9mv/dG6G/B5CwBN0fkE4C39wcTMoUx2RnG34OjXzPbce3Qdkk/+snk4ZGOuHkfKgG2UaQVWRSdffL8ziQ/qS2b1gVEpLcyRhwjcS2sNThhojvkR6kYE= 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 22dc5ea4196c..53f367609c70 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -244,6 +244,26 @@ config ARCH_HAS_BROKEN_DWARF5 # https://github.com/llvm/llvm-project/commit/7ffabb61a5569444b5ac9322e22e5471cc5e4a77 depends on LD_IS_LLD && LLD_VERSION < 180000 +config RISCV_USER_CFI + def_bool y + bool "riscv userspace control flow integrity" + depends on 64BIT && $(cc-option,-mabi=lp64 -march=rv64ima_zicfiss) + depends on RISCV_ALTERNATIVE + select ARCH_HAS_USER_SHADOW_STACK + select ARCH_USES_HIGH_VMA_FLAGS + select DYNAMIC_SIGFRAME + help + Provides CPU assisted control flow integrity to userspace tasks. + Control flow integrity is provided by implementing shadow stack for + backward edge and indirect branch tracking for forward edge in program. + Shadow stack protection is a hardware feature that detects function + return address corruption. This helps mitigate ROP attacks. + Indirect branch tracking enforces that all indirect branches must land + on a landing pad instruction else CPU will fault. This mitigates against + JOP / COP attacks. Applications must be enabled to use it, and old user- + space does not get protection "for free". + default y + config ARCH_MMAP_RND_BITS_MIN default 18 if 64BIT default 8 From patchwork Tue Oct 29 23:44:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13855746 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 01A39D7494D for ; Tue, 29 Oct 2024 23:45:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EA1146B00CC; Tue, 29 Oct 2024 19:45:38 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E4DC46B00CE; Tue, 29 Oct 2024 19:45:38 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C2B776B00CF; Tue, 29 Oct 2024 19:45:38 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id A2B0B6B00CC for ; Tue, 29 Oct 2024 19:45:38 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 5F825807AC for ; Tue, 29 Oct 2024 23:45:38 +0000 (UTC) X-FDA: 82728274104.28.2A3D230 Received: from mail-pf1-f174.google.com (mail-pf1-f174.google.com [209.85.210.174]) by imf01.hostedemail.com (Postfix) with ESMTP id AE91440004 for ; Tue, 29 Oct 2024 23:45:16 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=XBOh8kf4; spf=pass (imf01.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.174 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730245455; a=rsa-sha256; cv=none; b=ljjPkrMvcTKbJr1ayRjs/SXH7QLzGG+ci86lcKuMcUdwBjpRC6X9KBj8wMW67hWTFi/rG7 MczfNcDwF6z7zGapLpLKK6MbL/tJPkLmPizF6AwhLC6reqjyLFdw/qkWGdgLxebH/E0lSK mFuprcs/vrdBlN72qxsfwR0ZNivJn+w= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=XBOh8kf4; spf=pass (imf01.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.174 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730245455; 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=vh4CUvpz/fzJO7m3g4Lrx30VWLQNLgBESiJnDVTGIvg=; b=ZPju//DFpZMp4e3epZ+nX+loAXLgMDLGGFUqnsqprGwKUdu1dRzFl4I6oxmj6RI1//8cr2 VKg+Ht7ZDb06EE1ogmOuMPTcMUC/YNU0sWwbkOxcfeUfJVosjeYpmARLQ9FptdNKUBufXe LywA8ly48glXU2G/2j/4rt39TjE4Wcg= Received: by mail-pf1-f174.google.com with SMTP id d2e1a72fcca58-71e7086c231so4671898b3a.0 for ; Tue, 29 Oct 2024 16:45:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1730245535; x=1730850335; 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=vh4CUvpz/fzJO7m3g4Lrx30VWLQNLgBESiJnDVTGIvg=; b=XBOh8kf4HtEAVlAPml/Bf3tA1hMPJKmM5Ai6VFaaWDzER19DZDUek9si4tbGX5svnG h8s5i1mWjFk60QBOsiFijz+9tiQGEEQKwkSbU0VfmP1IYeW1t/I5O1WcX6D3/TQIUgVy NaPhbjFHE9fbORUHRbg9sH08l2L91WwMAUWKQqxLy/qcQc5TW7JudthHduOVE6Q4ySWq apHobEljnb2M4/8jHrKPnqIblYEycY1jaopxJga3aSYMh+MUh6uIj8r6shdRtdb2lsUJ LhSRAnpVg010y0qUvBnyh+cWH2cLXZIiEESiThUvYIYZrpPtbPL2fTVv4g/Urn7SpWs8 tiyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730245535; x=1730850335; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=vh4CUvpz/fzJO7m3g4Lrx30VWLQNLgBESiJnDVTGIvg=; b=JQM6hLPKgM/tQKpZBrT1dFWnlgL0f9IBxt6/5bI4WHVQ/3JYqcmNURH05J/dIvM5un 7k1cuouj03zqXbgvh3C7ToJyfvGFR0bhb5RqCuUfAVrhUKHl/8WFsi+tTcp+/A3+Sxgv 7MMxsE3mgWWfRKOBZRC8g0Be2wQAStH4seZzOTcZ1rxKkr3LZF/9+sqxlf60xtAcxn5L bt40BEupTiJM+6bHfgpMItfyIpnjdZfnVItgdUYTawPK05QXYXkhdMqE8Qvt68M4NjIg TsdV2ZPhPMVBfAT3Z1oDmiGuT4GrqoqpTXLwApU/v9zl5nAO0e81Fi2mOVECg+jmxW24 MMqA== X-Forwarded-Encrypted: i=1; AJvYcCXXU0pGLQruluCiiM2J1ngoTnPerz+N52nPmrJL5ESjPkFXBxT6+KOgX5bRGuoFQwEh5QEyYM4zew==@kvack.org X-Gm-Message-State: AOJu0Yz5ZycGq/doyv6MkwdtAYTiAtMh388w9qBFwVVUQEvejeiDIrhV dmkLDe2SwlnGul0CtmmUfufltkewzaVMv8mWiiVWb+OCC/bnAHS6QBeXU9E2vcY= X-Google-Smtp-Source: AGHT+IFcFdWc4q0yPw9J+zdcSou/ZZ/AKdZ4hdWaRHS8crpC8IYDjFupTjnAqz5ms9Fd+Onc4xeMtA== X-Received: by 2002:a05:6a00:3cc8:b0:71e:780e:9c1 with SMTP id d2e1a72fcca58-7206306ecf4mr18625578b3a.18.1730245535073; Tue, 29 Oct 2024 16:45:35 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72057921863sm8157643b3a.33.2024.10.29.16.45.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Oct 2024 16:45:34 -0700 (PDT) From: Deepak Gupta Date: Tue, 29 Oct 2024 16:44:30 -0700 Subject: [PATCH v7 30/32] riscv: Documentation for landing pad / indirect branch tracking MIME-Version: 1.0 Message-Id: <20241029-v5_user_cfi_series-v7-30-2727ce9936cb@rivosinc.com> References: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@rivosinc.com> In-Reply-To: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@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: 8j9bp1orwzjak53pfkxbt98eud8xm5dj X-Rspamd-Queue-Id: AE91440004 X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1730245516-458497 X-HE-Meta: U2FsdGVkX1+jbpiHIaFHO/Qv6Ky3HSe92IjQbs6+ELBh4/QA5+Zzs7C9Apwhub+SC1bpnq4WyG3jGUtEZl/aZ/b6eBKRhuB1oxLlER4yqlYQZctK20cgt4nKtWLjDrzyEPNFfg6Pr4bT458QnzQGjFrQw2RK1nyAmIMUdcZo3oIO0h2iQbuEWo6z5Lb6C9lLm8EkNnDRpPjUlvA8f3gvDUsdqzibOj83JNCfqYRUF/70/ucoqQhY//7YK8vrPrtycFr5CW/DMknFqhNuRC8wYLTP7mq+LUN+Mtdp0HoAc4DS6Xjc2zheYwgIPydrjdOuDJK+YST7X1Zjv66xT+7on0LzR2kHk02tyRTnx2LIjnNaU5Eor1wxHYP1SmzW6HSua7fxqZWPG9QgaVcukLAjmpWzNXskW0WvUJkpAkWUBgcfZmuK5YDNFc5Lnf0xwyHEPj4O02btWOUhTS8ZfZw+w6HDhhVKoj5smgWS1a0ko5Ztq0OLLdltm8mGc2RDf46PlQfiso5H5jTOnbHWGx4AbCF2IykmgU3vNvRjTK7BdpEGVU3oFBlK0cizitdqKpiY4wclBJ3I6IV1AFEQC4npkBDkFxd+l94AILAoxE+0NXlVAcoCwxDnOOcht8TFV1sOu9Ge20SBo2jOn7+bTbRyan3oqoOy2utPVaarD1E9SXaPec6MPBFPUizpwN3NsIEsupg52ahDogWHw+ECXNDeeBHRi+evEpaG/4KdXCEWLGrDq90DrFbufGg1p9G0Fy4R22gW0mdPIyncMPl+gFliuxxSF6zVif5N6kz7tR34v7ledgk6RlaIYi9rrgrqcu2i9HNUj2l0Heh6rCbm0asWYBdotv7ZaJfVTMBOsLkPjn00iq8/hSNwd9WAZ9JqwVF90hf5PLgHw2JtdnqNNKeQErAEGH7VnM6wPsjhwuHNl0qIu+MwMwQJTIYxlYDQWLkbwJNSvfQyv98BfEC9FAJ m/wwjMMt V0r/4kbrQrcG207UJfr/hd5FqnpeWIdbelmCSD9AokxqL3RhqMv7U8YV2dA/0TSxdYT/HqVYVy7JbCHXrSFZL9kvb884zJ5Sdt1RAOb4YxZfXZBFRoLTOLgdVvRIg6MaDtcQ4mCx1UkMgG/Z9Y6RYJtxVLIziyxzOV3QCDlZABSa1d+lVK2cYVwZvXaCfeaEYPh3oOvlkhhqJcxjw1J4QZL21HDMtVKr3QJtDkkIIptxIVtd1xIKIvFcVeG1CEcUpa++wQ8VJtPP7iFgld9cFuv/suVPtdne9KkmvdbW051HxncQocUP9oDZJ0pp4L78oQcf5Rh0c559VSszt0GqNfdNP1o0HGuRIQOJqeBmu+7hZ7eFN9b7U/eQGfiJv1ReL8YfZZAya2RGvIsYvxav7+r4qgvDqtYXf2Q626F67ahUin3mmhY7NZi4WwHSIHwTAOzGmxoNB2vKYlR1lKVPA1NW51g== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Adding documentation on landing pad aka indirect branch tracking on riscv and kernel interfaces exposed so that user tasks can enable it. Signed-off-by: Deepak Gupta --- Documentation/arch/riscv/index.rst | 1 + Documentation/arch/riscv/zicfilp.rst | 115 +++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+) diff --git a/Documentation/arch/riscv/index.rst b/Documentation/arch/riscv/index.rst index eecf347ce849..be7237b69682 100644 --- a/Documentation/arch/riscv/index.rst +++ b/Documentation/arch/riscv/index.rst @@ -14,6 +14,7 @@ RISC-V architecture uabi vector cmodx + zicfilp features diff --git a/Documentation/arch/riscv/zicfilp.rst b/Documentation/arch/riscv/zicfilp.rst new file mode 100644 index 000000000000..a188d78fcde6 --- /dev/null +++ b/Documentation/arch/riscv/zicfilp.rst @@ -0,0 +1,115 @@ +.. SPDX-License-Identifier: GPL-2.0 + +:Author: Deepak Gupta +:Date: 12 January 2024 + +==================================================== +Tracking indirect control transfers on RISC-V Linux +==================================================== + +This document briefly describes the interface provided to userspace by Linux +to enable indirect branch tracking for user mode applications on RISV-V + +1. Feature Overview +-------------------- + +Memory corruption issues usually result in to crashes, however when in hands of +an adversary and if used creatively can result into variety security issues. + +One of those security issues can be code re-use attacks on program where adversary +can use corrupt function pointers and chain them together to perform jump oriented +programming (JOP) or call oriented programming (COP) and thus compromising control +flow integrity (CFI) of the program. + +Function pointers live in read-write memory and thus are susceptible to corruption +and allows an adversary to reach any program counter (PC) in address space. On +RISC-V zicfilp extension enforces a restriction on such indirect control +transfers: + +- indirect control transfers must land on a landing pad instruction ``lpad``. + There are two exception to this rule: + + - rs1 = x1 or rs1 = x5, i.e. a return from a function and returns are + protected using shadow stack (see zicfiss.rst) + + - rs1 = x7. On RISC-V compiler usually does below to reach function + which is beyond the offset possible J-type instruction:: + + auipc x7, + jalr (x7) + + Such form of indirect control transfer are still immutable and don't rely + on memory and thus rs1=x7 is exempted from tracking and considered software + guarded jumps. + +``lpad`` instruction is pseudo of ``auipc rd, `` with ``rd=x0`` and +is a HINT nop. ``lpad`` instruction must be aligned on 4 byte boundary and +compares 20 bit immediate withx7. If ``imm_20bit`` == 0, CPU don't perform any +comparision with ``x7``. If ``imm_20bit`` != 0, then ``imm_20bit`` must match +``x7`` else CPU will raise ``software check exception`` (``cause=18``) with +``*tval = 2``. + +Compiler can generate a hash over function signatures and setup them (truncated +to 20bit) in x7 at callsites and function prologues can have ``lpad`` with same +function hash. This further reduces number of program counters a call site can +reach. + +2. ELF and psABI +----------------- + +Toolchain sets up :c:macro:`GNU_PROPERTY_RISCV_FEATURE_1_FCFI` for property +:c:macro:`GNU_PROPERTY_RISCV_FEATURE_1_AND` in notes section of the object file. + +3. Linux enabling +------------------ + +User space programs can have multiple shared objects loaded in its address space +and it's a difficult task to make sure all the dependencies have been compiled +with support of indirect branch. Thus it's left to dynamic loader to enable +indirect branch tracking for the program. + +4. prctl() enabling +-------------------- + +:c:macro:`PR_SET_INDIR_BR_LP_STATUS` / :c:macro:`PR_GET_INDIR_BR_LP_STATUS` / +:c:macro:`PR_LOCK_INDIR_BR_LP_STATUS` are three prctls added to manage indirect +branch tracking. prctls are arch agnostic and returns -EINVAL on other arches. + +* prctl(PR_SET_INDIR_BR_LP_STATUS, unsigned long arg) + +If arg1 is :c:macro:`PR_INDIR_BR_LP_ENABLE` and if CPU supports ``zicfilp`` +then kernel will enabled indirect branch tracking for the task. Dynamic loader +can issue this :c:macro:`prctl` once it has determined that all the objects +loaded in address space support indirect branch tracking. Additionally if there +is a `dlopen` to an object which wasn't compiled with ``zicfilp``, dynamic +loader can issue this prctl with arg1 set to 0 (i.e. +:c:macro:`PR_INDIR_BR_LP_ENABLE` being clear) + +* prctl(PR_GET_INDIR_BR_LP_STATUS, unsigned long arg) + +Returns current status of indirect branch tracking. If enabled it'll return +:c:macro:`PR_INDIR_BR_LP_ENABLE` + +* prctl(PR_LOCK_INDIR_BR_LP_STATUS, unsigned long arg) + +Locks current status of indirect branch tracking on the task. User space may +want to run with strict security posture and wouldn't want loading of objects +without ``zicfilp`` support in it and thus would want to disallow disabling of +indirect branch tracking. In that case user space can use this prctl to lock +current settings. + +5. violations related to indirect branch tracking +-------------------------------------------------- + +Pertaining to indirect branch tracking, CPU raises software check exception in +following conditions: + +- missing ``lpad`` after indirect call / jmp +- ``lpad`` not on 4 byte boundary +- ``imm_20bit`` embedded in ``lpad`` instruction doesn't match with ``x7`` + +In all 3 cases, ``*tval = 2`` is captured and software check exception is +raised (``cause=18``) + +Linux kernel will treat this as :c:macro:`SIGSEV`` with code = +:c:macro:`SEGV_CPERR` and follow normal course of signal delivery. From patchwork Tue Oct 29 23:44:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13855747 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 698F5D7494F for ; Tue, 29 Oct 2024 23:45:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4DF416B00CE; Tue, 29 Oct 2024 19:45:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 43D956B00D0; Tue, 29 Oct 2024 19:45:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 290796B00D1; Tue, 29 Oct 2024 19:45:41 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 084696B00CE for ; Tue, 29 Oct 2024 19:45:41 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id ADE8DC04CC for ; Tue, 29 Oct 2024 23:45:40 +0000 (UTC) X-FDA: 82728273096.09.78AB0EF Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.178]) by imf02.hostedemail.com (Postfix) with ESMTP id 2B15C80006 for ; Tue, 29 Oct 2024 23:44:51 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=BlwS9O0F; dmarc=none; spf=pass (imf02.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.178 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730245484; a=rsa-sha256; cv=none; b=pmvrCTPz9R6jE3pMp7o4726jQCdWqXpKZDA+RFIhd5Z7lhxwawYAx29e+G7UvN5kEJuq29 mQBXHkMRaB3K6qjiXje8GQ9h1KmMLEPeKL/vnqAC50XCcTZf/zNq5fap14ISgXQ50vOjhN KkoW7kUekNQrE2krLOiWK32e/LO/EbU= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=BlwS9O0F; dmarc=none; spf=pass (imf02.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.178 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=1730245484; 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=tdDNQkSVTKtNTxsV0uSmAnlAiDQNW7ggaTdWphzDp8I=; b=4YH5JdFLYfxCBE0c9zm1my2ipH/KJ7NZYHXRVA0dCHrVFf8qVHk2PZVlz3i8rFIumdD9Eh uKZCiG502xbiOPc5raPRXOHIotZT5E6GhxCjfXXaOjxdE2T4w+qgRSd4t27bbe0/aNEWA8 VsXkZ8YeegKw69g0+worIaq7XE9b+eY= Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-7203c431f93so4876423b3a.1 for ; Tue, 29 Oct 2024 16:45:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1730245538; x=1730850338; 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=tdDNQkSVTKtNTxsV0uSmAnlAiDQNW7ggaTdWphzDp8I=; b=BlwS9O0FT6ZUI/yP/U8ckcKsVzreLPg/PewTEPg8e5Po52rYbuaIaLO9KNfaqC06/0 at/8StriwmD+7E06nwsbIH7qTk5vpJRS68kgl6Ljc17MzI87ofakKGh94EXqWvJdzllo /c3VJU48kwcX7aeUy6o1YUbFcGV3CzmsyTGnPnDRVRHsmi0ngCYdQtZtPhQZipv3NRbz rWbPtkpIirjKYUrGf8/ke1zZSk4QT4a2RmC3F5C+vRDVUPkziYzATsJFsrZeoRKQDdMo 1cvln6yUuDcuS2BXpXZy1/b9E9k+2E4VHWuxRq0D336JGum3WY+b2gfzQBa4KpUORC3/ eynw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730245538; x=1730850338; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=tdDNQkSVTKtNTxsV0uSmAnlAiDQNW7ggaTdWphzDp8I=; b=nvSImWynUkNhjlPWC4fYK0MDvyKvf1lg6/TGflxP9CQJ7xUTXpaRdmN/pTNMzJdG+y u24pmJMwL3LMkYGPZT+jFh5SoxpyObWu16KExZN+D3TFv9ca1XmY3GS9p9shX35Pqvtl Phhi+Fyu8FzYaIGltUlouKfD5FyI99KtrEKlliAW07XJ2MIJwRw0F2u3pbyZ3WlSn7Sh 0jILQiuHYtG8KDaNKOwgysTQMpszw9temcDFNObz3AAoSvDzQ07BzuFZ/ar2MHvj19kq vtJcGEXUrECM8SR6yg9YncJKcl65vldHwNbHGTEnRzgnZ0WHq+z7qevWL+zp+DGpjJ9M axVw== X-Forwarded-Encrypted: i=1; AJvYcCVVvTce/xyTHO6nb8R6kKbipWpHbPlPJMtcFF4P5i7bT+Yi2uwYr1MBFsaa5OCMstpF1VxzGfmLYA==@kvack.org X-Gm-Message-State: AOJu0YwNRySan3xpjW+f1SoFS5Jgx51A2KIGKqzHUy4C+toO6LIysi2/ PTgOozCwo4MAMziW+jlnDgmAAluTfZzBwtoeyjQaK6OD5yNDNiRRQBLydDP2/so= X-Google-Smtp-Source: AGHT+IGStYEiFHNsr7wQiKA1IQWMIQ+/hyMXXJBof0RITbOXtrG/e7hp0ApyoGk/JQdjAfpgnE2Mpw== X-Received: by 2002:a05:6a00:2315:b0:71e:5f2c:c019 with SMTP id d2e1a72fcca58-72062fb2169mr19182991b3a.9.1730245537605; Tue, 29 Oct 2024 16:45:37 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72057921863sm8157643b3a.33.2024.10.29.16.45.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Oct 2024 16:45:37 -0700 (PDT) From: Deepak Gupta Date: Tue, 29 Oct 2024 16:44:31 -0700 Subject: [PATCH v7 31/32] riscv: Documentation for shadow stack on riscv MIME-Version: 1.0 Message-Id: <20241029-v5_user_cfi_series-v7-31-2727ce9936cb@rivosinc.com> References: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@rivosinc.com> In-Reply-To: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@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: 2B15C80006 X-Rspamd-Server: rspam01 X-Stat-Signature: hg8u3dt4tgkdmp96a3xxcr3keec8b4qk X-HE-Tag: 1730245491-655674 X-HE-Meta: U2FsdGVkX182DFQLFd3H1SCt4LWI0V4dy5D31IB71GLJxyOZlB6VM389gOrzeSXRC8q0iM4i4mrcWZ1KMAHiK08Q7aTdLVxegd0rypljkmMi3piqMZQTHGu75y0edilwTEeKmakwGHRkZA9+FLKl+2LzN2az5Sqcx/C6SdNbjMenflKIx06pWi7f/vN0d+EsRYsgqUAtNPfrU+mtsNPYovNCLL9kcPhgQXQRSQt8UmjUvQUm+Sw4Nr6EztjS82UgAH7UmuN7dNjmjHCGnXBpFkX04NEBRf0N3mwzFiQbLocyvToIdU5Hm6NV4iPwATAnq9BiABmre4T+eM7E3CRdcTbTZTitlFE4pltZgQ7Ccvz71xpwmYe15ughaMX1Bxll4qSeSOjpGkTHe2+jO5MrQwh+1cY5T6bbNhXAAWirZaBfzGV99VvvWO/7N14BZOc8nVX0maSYRAf5rr4TBzJUgTucXhiqsLaoWrljPQHid4ns2QkiUOo3XSCE/AtKiELqqg9l1JZcxETFJkhaqPEv0dASbQPPalYcYqUijZibUFyYeqigUnZSZSRgrNKT823z7v1VtHdT/owGnG8WvGx/gKF18QiuSeFC4G+GP4HOKh2Q34ojBjEUBSU9h2RSVg3+jWzgbYYjIZdO5fgdvNzVqFSxn/xhyg6V7ZHfinY7oz7mG2Q4TybKbNstv1Ta4Ml3GK8PJUybGwBkaEqPDtyj6lSmHln6rOxlWlN56eVsF5FM7VvJ9C9M6U06ZltfTCYsgSzcuDiFV87yZzv3qnvW50yzzULnzeI2n5gcI/YQ8mY9d3D9HdWRzHx+2NhHCnzBaU64z4RlxgA2lGGs+at32A73NYCx+qbvOP7YCT0IB3l6CuJvItl5wF+M15WeYwqHtHPqD8JWrsyudfnfS0FSUIueGhR6n2OjUopskhgiYcS9p3kuV3gpMMjAmFKdbCBefkOo3KuNhjMTpnWyt2Y HiJct42j 6cgmpMfY2jLCkotdbmSsklG+lN1NO5Ba6LKJmvkXsH1lPoXaFctlcxI27WxiDyGliONqxKziBf2t9wgy0iIJ5O1rCHKIvxi+jb7Fj6MRMkg8t0mPoipMDOmKYb2gXD0E5ieuKTBYawyR2KGUpEpDGjmzFAAjDsrbosAsg7ufWqvbKhtrRFZVIlYIa20XQY6ctbNJEDd0QRVjqR3u+AfVkntOZHTdE31ixyFZY46/ASO7WwwDLiKPn29yBIDl04/pRAzRbkClNjd9P5gU6Gl8xAUabW37hWHFAKgXg9v7+i7hUQQnFZlrI24rtLN0g+7VdDuhMWDeUy0wt6c6XOoJiHYY8OZbZVVwyae6Tvjw0VDlqfUV8LyYCr3nF0mfD+bkZ3iriL95KX6+Ck3NTpTRXZmM5W3GOAfuQHcYbUVs5eXKONn0thHCXKsKNHiQa0ei4ffGnBxiK/gpBFOpRobopy47csQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Adding documentation on shadow stack for user mode on riscv and kernel interfaces exposed so that user tasks can enable it. Signed-off-by: Deepak Gupta --- Documentation/arch/riscv/index.rst | 1 + Documentation/arch/riscv/zicfiss.rst | 176 +++++++++++++++++++++++++++++++++++ 2 files changed, 177 insertions(+) diff --git a/Documentation/arch/riscv/index.rst b/Documentation/arch/riscv/index.rst index be7237b69682..e240eb0ceb70 100644 --- a/Documentation/arch/riscv/index.rst +++ b/Documentation/arch/riscv/index.rst @@ -15,6 +15,7 @@ RISC-V architecture vector cmodx zicfilp + zicfiss features diff --git a/Documentation/arch/riscv/zicfiss.rst b/Documentation/arch/riscv/zicfiss.rst new file mode 100644 index 000000000000..5ba389f15b3f --- /dev/null +++ b/Documentation/arch/riscv/zicfiss.rst @@ -0,0 +1,176 @@ +.. SPDX-License-Identifier: GPL-2.0 + +:Author: Deepak Gupta +:Date: 12 January 2024 + +========================================================= +Shadow stack to protect function returns on RISC-V Linux +========================================================= + +This document briefly describes the interface provided to userspace by Linux +to enable shadow stack for user mode applications on RISV-V + +1. Feature Overview +-------------------- + +Memory corruption issues usually result in to crashes, however when in hands of +an adversary and if used creatively can result into variety security issues. + +One of those security issues can be code re-use attacks on program where +adversary can use corrupt return addresses present on stack and chain them +together to perform return oriented programming (ROP) and thus compromising +control flow integrity (CFI) of the program. + +Return addresses live on stack and thus in read-write memory and thus are +susceptible to corruption and allows an adversary to reach any program counter +(PC) in address space. On RISC-V ``zicfiss`` extension provides an alternate +stack termed as shadow stack on which return addresses can be safely placed in +prolog of the function and retrieved in epilog. ``zicfiss`` extension makes +following changes: + +- PTE encodings for shadow stack virtual memory + An earlier reserved encoding in first stage translation i.e. + PTE.R=0, PTE.W=1, PTE.X=0 becomes PTE encoding for shadow stack pages. + +- ``sspush x1/x5`` instruction pushes (stores) ``x1/x5`` to shadow stack. + +- ``sspopchk x1/x5`` instruction pops (loads) from shadow stack and compares + with ``x1/x5`` and if un-equal, CPU raises ``software check exception`` with + ``*tval = 3`` + +Compiler toolchain makes sure that function prologue have ``sspush x1/x5`` to +save return address on shadow stack in addition to regular stack. Similarly +function epilogs have ``ld x5, offset(x2)`` followed by ``sspopchk x5`` to +ensure that popped value from regular stack matches with popped value from +shadow stack. + +2. Shadow stack protections and linux memory manager +----------------------------------------------------- + +As mentioned earlier, shadow stack get new page table encodings and thus have +some special properties assigned to them and instructions that operate on them +as below: + +- Regular stores to shadow stack memory raises access store faults. This way + shadow stack memory is protected from stray inadvertant writes. + +- Regular loads to shadow stack memory are allowed. This allows stack trace + utilities or backtrace functions to read true callstack (not tampered). + +- Only shadow stack instructions can generate shadow stack load or shadow stack + store. + +- Shadow stack load / shadow stack store on read-only memory raises AMO/store + page fault. Thus both ``sspush x1/x5`` and ``sspopchk x1/x5`` will raise AMO/ + store page fault. This simplies COW handling in kernel During fork, kernel + can convert shadow stack pages into read-only memory (as it does for regular + read-write memory) and as soon as subsequent ``sspush`` or ``sspopchk`` in + userspace is encountered, then kernel can perform COW. + +- Shadow stack load / shadow stack store on read-write, read-write-execute + memory raises an access fault. This is a fatal condition because shadow stack + should never be operating on read-write, read-write-execute memory. + +3. ELF and psABI +----------------- + +Toolchain sets up :c:macro:`GNU_PROPERTY_RISCV_FEATURE_1_BCFI` for property +:c:macro:`GNU_PROPERTY_RISCV_FEATURE_1_AND` in notes section of the object file. + +4. Linux enabling +------------------ + +User space programs can have multiple shared objects loaded in its address space +and it's a difficult task to make sure all the dependencies have been compiled +with support of shadow stack. Thus it's left to dynamic loader to enable +shadow stack for the program. + +5. prctl() enabling +-------------------- + +:c:macro:`PR_SET_SHADOW_STACK_STATUS` / :c:macro:`PR_GET_SHADOW_STACK_STATUS` / +:c:macro:`PR_LOCK_SHADOW_STACK_STATUS` are three prctls added to manage shadow +stack enabling for tasks. prctls are arch agnostic and returns -EINVAL on other +arches. + +* prctl(PR_SET_SHADOW_STACK_STATUS, unsigned long arg) + +If arg1 :c:macro:`PR_SHADOW_STACK_ENABLE` and if CPU supports ``zicfiss`` then +kernel will enable shadow stack for the task. Dynamic loader can issue this +:c:macro:`prctl` once it has determined that all the objects loaded in address +space have support for shadow stack. Additionally if there is a +:c:macro:`dlopen` to an object which wasn't compiled with ``zicfiss``, dynamic +loader can issue this prctl with arg1 set to 0 (i.e. +:c:macro:`PR_SHADOW_STACK_ENABLE` being clear) + +* prctl(PR_GET_SHADOW_STACK_STATUS, unsigned long *arg) + +Returns current status of indirect branch tracking. If enabled it'll return +:c:macro:`PR_SHADOW_STACK_ENABLE`. + +* prctl(PR_LOCK_SHADOW_STACK_STATUS, unsigned long arg) + +Locks current status of shadow stack enabling on the task. User space may want +to run with strict security posture and wouldn't want loading of objects +without ``zicfiss`` support in it and thus would want to disallow disabling of +shadow stack on current task. In that case user space can use this prctl to +lock current settings. + +5. violations related to returns with shadow stack enabled +----------------------------------------------------------- + +Pertaining to shadow stack, CPU raises software check exception in following +condition: + +- On execution of ``sspopchk x1/x5``, ``x1/x5`` didn't match top of shadow + stack. If mismatch happens then cpu does ``*tval = 3`` and raise software + check exception. + +Linux kernel will treat this as :c:macro:`SIGSEV`` with code = +:c:macro:`SEGV_CPERR` and follow normal course of signal delivery. + +6. Shadow stack tokens +----------------------- +Regular stores on shadow stacks are not allowed and thus can't be tampered +with via arbitrary stray writes due to bugs. Method of pivoting / switching to +shadow stack is simply writing to csr ``CSR_SSP`` changes active shadow stack. +This can be problematic because usually value to be written to ``CSR_SSP`` will +be loaded somewhere in writeable memory and thus allows an adversary to +corruption bug in software to pivot to an any address in shadow stack range. +Shadow stack tokens can help mitigate this problem by making sure that: + +- When software is switching away from a shadow stack, shadow stack pointer + should be saved on shadow stack itself and call it ``shadow stack token`` + +- When software is switching to a shadow stack, it should read the + ``shadow stack token`` from shadow stack pointer and verify that + ``shadow stack token`` itself is pointer to shadow stack itself. + +- Once the token verification is done, software can perform the write to + ``CSR_SSP`` to switch shadow stack. + +Here software can be user mode task runtime itself which is managing various +contexts as part of single thread. Software can be kernel as well when kernel +has to deliver a signal to user task and must save shadow stack pointer. Kernel +can perform similar procedure by saving a token on user shadow stack itself. +This way whenever :c:macro:`sigreturn` happens, kernel can read the token and +verify the token and then switch to shadow stack. Using this mechanism, kernel +helps user task so that any corruption issue in user task is not exploited by +adversary by arbitrarily using :c:macro:`sigreturn`. Adversary will have to +make sure that there is a ``shadow stack token`` in addition to invoking +:c:macro:`sigreturn` + +7. Signal shadow stack +----------------------- +Following structure has been added to sigcontext for RISC-V:: + + struct __sc_riscv_cfi_state { + unsigned long ss_ptr; + }; + +As part of signal delivery, shadow stack token is saved on current shadow stack +itself and updated pointer is saved away in :c:macro:`ss_ptr` field in +:c:macro:`__sc_riscv_cfi_state` under :c:macro:`sigcontext`. Existing shadow +stack allocation is used for signal delivery. During :c:macro:`sigreturn`, +kernel will obtain :c:macro:`ss_ptr` from :c:macro:`sigcontext` and verify the +saved token on shadow stack itself and switch shadow stack. From patchwork Tue Oct 29 23:44:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13855748 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 C74B0D7494E for ; Tue, 29 Oct 2024 23:45:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 133B96B00D0; Tue, 29 Oct 2024 19:45:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0E1BF6B00D2; Tue, 29 Oct 2024 19:45:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E288C6B00D3; Tue, 29 Oct 2024 19:45:43 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id BCB3A6B00D0 for ; Tue, 29 Oct 2024 19:45:43 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 705C31A076F for ; Tue, 29 Oct 2024 23:45:43 +0000 (UTC) X-FDA: 82728273810.30.DE71AE0 Received: from mail-pf1-f181.google.com (mail-pf1-f181.google.com [209.85.210.181]) by imf16.hostedemail.com (Postfix) with ESMTP id 819EA180008 for ; Tue, 29 Oct 2024 23:45:16 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=dtRHEDXG; spf=pass (imf16.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.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=1730245381; 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=ESn9fCOiwm5fyNjtFShGIII5bqF5wu11eYPz8YfBFBM=; b=Vg3zRuVnjgXA6vhxPLtXeCPyZqQncAxo4qjgAun800snY/eTJ2BwgfxZmrv6zaP6QeBYyY hvY/WZYmcHpXyRU+Ng5H7qiXOgtUf+ubRs7JLzHy1KFYGZZXA6v0ZCTG2MOIGOFhEAHwpp GI2/AQObLA2ZyJhD7vOLwRdr5uZKqhQ= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730245381; a=rsa-sha256; cv=none; b=Sf8oSTK5VdCMZ9419xWkXplmQAN9YoY7NgPw+81Q+IYVQTyMl5NIVDIxLE9dwNjDq86smX oxeVeosp0UfADmV5hTurR2GY4wibRtr+ff29yPZiWI4ifyUhty1gBfNjsDQtm5wXmF8jNI ha4k6Z2mU1+zJ/aKzWzCPbmhPBBJoXs= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=dtRHEDXG; spf=pass (imf16.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.181 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pf1-f181.google.com with SMTP id d2e1a72fcca58-7203c74e696so4404488b3a.0 for ; Tue, 29 Oct 2024 16:45:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1730245540; x=1730850340; 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=ESn9fCOiwm5fyNjtFShGIII5bqF5wu11eYPz8YfBFBM=; b=dtRHEDXG7azwDxuvmP0aX9XwNgozWTudJozIupB/vLv9ZqLtH6etpwVwlhoXY207+p 82KMPwpKILhkWhk0rXPlZ/kpv3L4pPSNTuPiHqkf+XXeRZ/J6Xtq4IBaHQHX7dxlX6Pl 1hUeF5UG8fzACJ5O9eHOnP2kofa9TteXrdb46TRMkZMQ4fhATCLyB9yj002saEMBNix6 npn3yg6OgzhzpezoirkgLphk05NhN4EsmbnsWuI5qXPTS04mf0i75SVDa2GFcliAL7ME wAv23jYZPSRoKPt2HzRt9M+iGsqnYXYLrrgGDGetK5tPapvZneeodrM4oBQAw+V3CE+q DvLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730245540; x=1730850340; 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=ESn9fCOiwm5fyNjtFShGIII5bqF5wu11eYPz8YfBFBM=; b=kjusSaiWtfOcPNzq2/ZekuCfdHsXf27M+RI7RyD74qHA4P22zBi+9wifx8cCF7zwNZ 1gagbrjgeOO1y76/6af050YivNcc1+BdQEcPBFCej2RmthOiuNiYBvIlfY/QkqF34HqU H5/Bir0H+3ttiixYMgvKpexHSJRxMh5rSKmxdyAFhos5C1yducJfhlHTw5sgqnbk5+Qe 4UfKhdtG9y66R4zhyXeo2q2/lV+IqRbZ+RSmOm2ecckilVtBNnIW1jeSLq5UZpPAn28e iUmExu8sDc43B6BCy+9LnF2ECU2a8DyueWFir7aEaPKsqh5kYPtonhGlAvhaQERjkMzq UjdA== X-Forwarded-Encrypted: i=1; AJvYcCWsgJUHdlPFV2HvD8LVjbZ6kvkw2LN7XbpRFsdC1sjAKybjDkYRJhoBPPmy+DnCkD4Ajo9Ql9B8vw==@kvack.org X-Gm-Message-State: AOJu0YzUQLlKMpyVqaxRIEnRDmWSEH6VW/HgZ6qNgQblOPfCGLmsVeIo YNzu9yQ9Z7MMDS2eigKVGf17uEJw6KGj5FFMu9O7j6NCMpFdoM+4dVYdsDBQ2So= X-Google-Smtp-Source: AGHT+IEytQZucsJ+hJdUwJv+i4MvSm0k7IRK7pe4lpkwmubFdxEfm5N4vCdn3llj+Ve3wC6Nnd0sOg== X-Received: by 2002:a05:6a20:b68a:b0:1d9:18e2:e09d with SMTP id adf61e73a8af0-1d9a850a6e0mr18880194637.44.1730245540131; Tue, 29 Oct 2024 16:45:40 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72057921863sm8157643b3a.33.2024.10.29.16.45.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Oct 2024 16:45:39 -0700 (PDT) From: Deepak Gupta Date: Tue, 29 Oct 2024 16:44:32 -0700 Subject: [PATCH v7 32/32] kselftest/riscv: kselftest for user mode cfi MIME-Version: 1.0 Message-Id: <20241029-v5_user_cfi_series-v7-32-2727ce9936cb@rivosinc.com> References: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@rivosinc.com> In-Reply-To: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@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: 7mkpfkgeoxk796fyirtyin7drqtt53kd X-Rspamd-Queue-Id: 819EA180008 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1730245516-552482 X-HE-Meta: U2FsdGVkX1938GvcQGLzp+bJQ6A70eCYYlCCjpfHbW9u5wq3ecV/TBjRPqEWqEjvr5PHyhC6VZgh0HuKar/fXuLJk/3KYrnbsRBIuWi3LXVLBtjZltQujH99NPwfoNzK4uKD9hM0evZGdNiNJqq7v732MJtPqx0r9GzFHQ5CkPCR0vQhPauJl1vCPhV9y56hWrXOkTEEVZg382Ax9Qj8F/7LqKnDt6gi2snVQv22Q0TqGPkr4qT/aPb1Ruc9bFMO1ZTAq6JYakl3x7h9cBv1FTMyQxWZAAHDc7vGzPo9iXXzKAHikr7g1OOUVNHVoEfs5d6G0BeFpt7FDpBXyHoPSGfMxsZTe5O6BLA9FQcLR3U9KJYvUMoYkZDfeO6fbA2vXnf8wFRhnUwt+bA0yyQGfbNnmsSR71jiF2qxDru7UrE/7Tzua+21vs/pzKwl3XatXIBqVy7f+6UdEzia8+J45X+Zpd9Q5pn4qmtB1YbhSqQaEoD+uS5gChUN30Y5VK2lbIZbdoukll9CF+YdEh1NgANHpmrgmG61JZwTE3IcrVifnshKSqkJRlv9j7nk5RuNRuVWcrYQAKYsm1U4WXWPkbTwGeKKs7vEF7wYEjohJgKxskUDiw20S0QpUk6eStmqZi7YKqJLQPK9s7zsLMsSIDs7uTDvC2bqL9yOcZJKwupg26lL69jwNzOVOBz64xS2W3Uerpq78HhDj9RLWdRhLX9J1R6g5dwNIaqzfLDlxWBiAS4F1gd6Ccf17PW9dOpI7ZSItNXS6I1VmCTPqAiqSC0PZiA4oA+lcRGH5ck2vpqtnvQ9fZFeup8RqToGGIvcT2Kb5mecAjSmW5EMOc77TL2UmCXWw70Md7Q2JVxN2pREHfPNKotjUJF7flBHLu4dE49c2g5yZ3p5v7Q5beqax7a6azySofFZfy3RW4BFGKUGYjQdtlW+FRUQvIoPb2SufCglvexl6pXMNFhN9o6 KrqThc6+ 3jGUfF3XkAgAcyV2aO0em77wnn2H3qQrYWjGDFoBIdeovxceTVNzfC+G02K7cLBKk9EftFdZobX7F8yJJvQ0yx8/qr2x/uOgl7fhwUW0oJRNmoXKSw3GrbBspkxEKd3QgmJ30UgaIAUNH+9ZfjKXhM72PXRfL1GDlLcOuKaL6baGNoQDPjrij8YlkvmqxgP6RnvM0MunCM0fSI5Dy7y2VxvDSD+V+lUaRwS628lixl6seL1XZgiKf7tUaPuLVZoQDoTC2zxvDOqX/Wf9ZM3HtD/3dFaYKm5/3opH34v3qLue5/EFDZns3TX2X1HXEofsXj+Py2Rqt2AXnkz1DKlJDX4jrk7KetN7J9lH9NDpVM9Stgl01IxBArxCM7LjRWMSK9l3gvQ29I2XINRzj3i5TUV5x2/7fpIFcq1wNmnLIeTuXlAYrCnfG8cuqlBU1X8fFkCTFj6Mzd+kdq6MmGYALijecuQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Adds kselftest for RISC-V control flow integrity implementation for user mode. There is not a lot going on in kernel for enabling landing pad for user mode. cfi selftest are intended to be compiled with zicfilp and zicfiss enabled compiler. Thus kselftest simply checks if landing pad and shadow stack for the binary and process are enabled or not. selftest then register a signal handler for SIGSEGV. Any control flow violation are reported as SIGSEGV with si_code = SEGV_CPERR. Test will fail on receiving any SEGV_CPERR. Shadow stack part has more changes in kernel and thus there are separate tests for that - Exercise `map_shadow_stack` syscall - `fork` test to make sure COW works for shadow stack pages - gup tests Kernel uses FOLL_FORCE when access happens to memory via /proc//mem. Not breaking that for shadow stack. - signal test. Make sure signal delivery results in token creation on shadow stack and consumes (and verifies) token on sigreturn - shadow stack protection test. attempts to write using regular store instruction on shadow stack memory must result in access faults Test outut ========== """ TAP version 13 1..5 This is to ensure shadow stack is indeed enabled and working This is to ensure shadow stack is indeed enabled and working ok 1 shstk fork test ok 2 map shadow stack syscall ok 3 shadow stack gup tests ok 4 shadow stack signal tests ok 5 memory protections of shadow stack memory """ Signed-off-by: Deepak Gupta --- tools/testing/selftests/riscv/Makefile | 2 +- tools/testing/selftests/riscv/cfi/.gitignore | 3 + tools/testing/selftests/riscv/cfi/Makefile | 10 + tools/testing/selftests/riscv/cfi/cfi_rv_test.h | 84 +++++ tools/testing/selftests/riscv/cfi/riscv_cfi_test.c | 78 +++++ tools/testing/selftests/riscv/cfi/shadowstack.c | 373 +++++++++++++++++++++ tools/testing/selftests/riscv/cfi/shadowstack.h | 37 ++ 7 files changed, 586 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/riscv/Makefile b/tools/testing/selftests/riscv/Makefile index 7ce03d832b64..6e142fe004ab 100644 --- a/tools/testing/selftests/riscv/Makefile +++ b/tools/testing/selftests/riscv/Makefile @@ -5,7 +5,7 @@ ARCH ?= $(shell uname -m 2>/dev/null || echo not) ifneq (,$(filter $(ARCH),riscv)) -RISCV_SUBTARGETS ?= hwprobe vector mm sigreturn +RISCV_SUBTARGETS ?= hwprobe vector mm sigreturn cfi else RISCV_SUBTARGETS := endif diff --git a/tools/testing/selftests/riscv/cfi/.gitignore b/tools/testing/selftests/riscv/cfi/.gitignore new file mode 100644 index 000000000000..82545863bac6 --- /dev/null +++ b/tools/testing/selftests/riscv/cfi/.gitignore @@ -0,0 +1,3 @@ +cfitests +riscv_cfi_test +shadowstack diff --git a/tools/testing/selftests/riscv/cfi/Makefile b/tools/testing/selftests/riscv/cfi/Makefile new file mode 100644 index 000000000000..b65f7ff38a32 --- /dev/null +++ b/tools/testing/selftests/riscv/cfi/Makefile @@ -0,0 +1,10 @@ +CFLAGS += -I$(top_srcdir)/tools/include + +CFLAGS += -march=rv64gc_zicfilp_zicfiss + +TEST_GEN_PROGS := cfitests + +include ../../lib.mk + +$(OUTPUT)/cfitests: riscv_cfi_test.c shadowstack.c + $(CC) -o$@ $(CFLAGS) $(LDFLAGS) $^ diff --git a/tools/testing/selftests/riscv/cfi/cfi_rv_test.h b/tools/testing/selftests/riscv/cfi/cfi_rv_test.h new file mode 100644 index 000000000000..0fefdc33f71e --- /dev/null +++ b/tools/testing/selftests/riscv/cfi/cfi_rv_test.h @@ -0,0 +1,84 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#ifndef SELFTEST_RISCV_CFI_H +#define SELFTEST_RISCV_CFI_H +#include +#include +#include "shadowstack.h" + +#define RISCV_CFI_SELFTEST_COUNT RISCV_SHADOW_STACK_TESTS + +#define CHILD_EXIT_CODE_SSWRITE 10 +#define CHILD_EXIT_CODE_SIG_TEST 11 + +#define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ +({ \ + register long _num __asm__ ("a7") = (num); \ + register long _arg1 __asm__ ("a0") = (long)(arg1); \ + register long _arg2 __asm__ ("a1") = (long)(arg2); \ + register long _arg3 __asm__ ("a2") = (long)(arg3); \ + register long _arg4 __asm__ ("a3") = (long)(arg4); \ + register long _arg5 __asm__ ("a4") = (long)(arg5); \ + \ + __asm__ volatile( \ + "ecall\n" \ + : "+r" \ + (_arg1) \ + : "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \ + "r"(_num) \ + : "memory", "cc" \ + ); \ + _arg1; \ +}) + +#define my_syscall3(num, arg1, arg2, arg3) \ +({ \ + register long _num __asm__ ("a7") = (num); \ + register long _arg1 __asm__ ("a0") = (long)(arg1); \ + register long _arg2 __asm__ ("a1") = (long)(arg2); \ + register long _arg3 __asm__ ("a2") = (long)(arg3); \ + \ + __asm__ volatile( \ + "ecall\n" \ + : "+r" (_arg1) \ + : "r"(_arg2), "r"(_arg3), \ + "r"(_num) \ + : "memory", "cc" \ + ); \ + _arg1; \ +}) + +#ifndef __NR_prctl +#define __NR_prctl 167 +#endif + +#ifndef __NR_map_shadow_stack +#define __NR_map_shadow_stack 453 +#endif + +#define CSR_SSP 0x011 + +#ifdef __ASSEMBLY__ +#define __ASM_STR(x) x +#else +#define __ASM_STR(x) #x +#endif + +#define csr_read(csr) \ +({ \ + register unsigned long __v; \ + __asm__ __volatile__ ("csrr %0, " __ASM_STR(csr) \ + : "=r" (__v) : \ + : "memory"); \ + __v; \ +}) + +#define csr_write(csr, val) \ +({ \ + unsigned long __v = (unsigned long) (val); \ + __asm__ __volatile__ ("csrw " __ASM_STR(csr) ", %0" \ + : : "rK" (__v) \ + : "memory"); \ +}) + +#endif diff --git a/tools/testing/selftests/riscv/cfi/riscv_cfi_test.c b/tools/testing/selftests/riscv/cfi/riscv_cfi_test.c new file mode 100644 index 000000000000..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