From patchwork Fri Mar 29 04:44:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13610045 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 49E57CD1283 for ; Fri, 29 Mar 2024 04:45:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BC1846B0088; Fri, 29 Mar 2024 00:45:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B70696B0089; Fri, 29 Mar 2024 00:45:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9F3A96B008A; Fri, 29 Mar 2024 00:45:16 -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 7FF696B0088 for ; Fri, 29 Mar 2024 00:45:16 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 38E37160F62 for ; Fri, 29 Mar 2024 04:45:16 +0000 (UTC) X-FDA: 81948837432.27.06B0E02 Received: from mail-oi1-f175.google.com (mail-oi1-f175.google.com [209.85.167.175]) by imf20.hostedemail.com (Postfix) with ESMTP id 6BA671C0015 for ; Fri, 29 Mar 2024 04:45:14 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=tIBu7NHx; dmarc=none; spf=pass (imf20.hostedemail.com: domain of debug@rivosinc.com designates 209.85.167.175 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1711687514; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=txT/LccY8POETQDQkXcfynUkPCv6cjOV6zf46WX/Pcw=; b=biBaVajOHzpPCJAp156ZRAnaJ9PlZH4cysbhyKv2hk9X8cg6KgCZ++UZfuDBkgbuodbXAM kmYEUGcN2TGasdLy0ggWZRLfxkQOJ6XBGJ1ESTbN6Hz1DV75PcZKaf65KWYjrODEd3V82Z Mzt9zNQQ3ZwmQbHHEgP0+f2AczrH0Zw= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=tIBu7NHx; dmarc=none; spf=pass (imf20.hostedemail.com: domain of debug@rivosinc.com designates 209.85.167.175 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711687514; a=rsa-sha256; cv=none; b=aQf92QBuvOQsupPtuRgbrI8amPHuDiPLmkpOm8uXc7C4cTRgy+7jHO0TGxe8otWyYa80wn YDfVxoRELP6j2HdYsyp9xhTAdc8YIPFkmmROf6ImM4+4otx2PAtcB1DG38TL3trtU54Jbb kb06+yf7Lp9+i7Ya1F53mipe5e9s2Ds= Received: by mail-oi1-f175.google.com with SMTP id 5614622812f47-3c3df13fe31so1005496b6e.0 for ; Thu, 28 Mar 2024 21:45:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1711687513; x=1712292313; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=txT/LccY8POETQDQkXcfynUkPCv6cjOV6zf46WX/Pcw=; b=tIBu7NHx5g8viX1waTh8TArkMedsLhf2dVrugpuXC4gVrgYmnMvNlKC9SR/v++0RlF 6ODQ9PJSbXVEQXYcjL3H8rekfABk+3rxqS8L2mZNs+FIhEm5QxZb7zydY8MY8G1Lu7S4 qkLG4+TELla19S//nNQnZT15Qrs7JriEIZa9qFvzJHoRHTkfit+y6ZZvZLsx/rZFU1Nv sh+hnS5zKbC0cZMiMLv2UGaecbwHoYLLbNidofCkNePJhBM6KxQjPGs8KtjdYKDZoIwB qz45vbZ7e1Rzyq55p4G/QR1aZhaHe4U8KRFLMYPFR3FH74aTEZAGXwGCPwjwbE3URB5V eb6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711687513; x=1712292313; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=txT/LccY8POETQDQkXcfynUkPCv6cjOV6zf46WX/Pcw=; b=l4babBDv4cfxsID7mq+crc2qTXWTZX0BxXNguEJEQ0wcUtQY3vn0G5dYpkB3CJ5drl c9V6RB8Lubgfz3GV7O8bIGws9hckRUmX8xA5o+uNoutjMgzXnwkEHndLSjTw3TvHrKoR lLmp3foIqiVb1W0Y5BAKDBnBYHU6IJgHBD6xOQwoMDp19TqaxI2vlKeHhL/9swQ6AqN8 NbH7/NkBvwNdjkA6SNkx/swebHO6wUryLjAolNZh1OhgqVlNiRtRwHroycVifeQe4mvi +Be8g5KOtwAJdAs7Ps6DyPXVBeycvtWApuAoWpHrrpyOnGFjAo3BUAnECS02Tdr1iWNX mWng== X-Forwarded-Encrypted: i=1; AJvYcCXDeCEa7il+ETMOVPED2ef6eYJDKAU75tkucUpcuq90v1xXXzkKNT6OGbDK4687ThjZSG9QSnX3p/LT/Et/587MX3A= X-Gm-Message-State: AOJu0Yy1sOdvfDHiszGL8NKx/zbAbyRGzTayHVpC9Ur6vEg55FFphGx6 rL4wrX6D9qaERcGMaoMeNQIgBCPfBBFyk3ixIPucdeaK25yjDp3VoDI/SYgREus= X-Google-Smtp-Source: AGHT+IEg1KTq7jE0OLYYVBAFrVsprg4r4Srip8vRVS899kAu1nJanfI5DM3TwMhj/+BfX0hVCMH91w== X-Received: by 2002:a05:6808:1584:b0:3c3:e05c:f499 with SMTP id t4-20020a056808158400b003c3e05cf499mr1508075oiw.39.1711687513312; Thu, 28 Mar 2024 21:45:13 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id i18-20020aa78b52000000b006ea7e972947sm2217120pfd.130.2024.03.28.21.45.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 21:45:12 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com, samuel.holland@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, tech-j-ext@lists.risc-v.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh+dt@kernel.org, krzysztof.kozlowski+dt@linaro.org, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, Liam.Howlett@oracle.com, vbabka@suse.cz, lstoakes@gmail.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, hankuan.chen@sifive.com, greentime.hu@sifive.com, evan@rivosinc.com, xiao.w.wang@intel.com, charlie@rivosinc.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, dbarboza@ventanamicro.com, sameo@rivosinc.com, shikemeng@huaweicloud.com, willy@infradead.org, vincent.chen@sifive.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, gerg@kernel.org, heiko@sntech.de, bhe@redhat.com, jeeheng.sia@starfivetech.com, cyy@cyyself.name, maskray@google.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bgray@linux.ibm.com, mpe@ellerman.id.au, baruch@tkos.co.il, alx@kernel.org, david@redhat.com, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, shr@devkernel.io, deller@gmx.de, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com Subject: [PATCH v2 01/27] riscv: envcfg save and restore on task switching Date: Thu, 28 Mar 2024 21:44:33 -0700 Message-Id: <20240329044459.3990638-2-debug@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240329044459.3990638-1-debug@rivosinc.com> References: <20240329044459.3990638-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 6BA671C0015 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: 3r89uu6cumhzg3qpybf85ayyttz6pt8a X-HE-Tag: 1711687514-334722 X-HE-Meta: U2FsdGVkX1+ygXBtRr8toDOwbis/e9cnT0Kx1ArvDr/HjZmrzpVlbi2B/R8EUrWqDEXndHmLHtyU/tA59v0JRtr6Ocn3Xv9Jf395L/MwBJhDV2X1tT9RcJ0zsDW7ktDRIIvMdFjyxy40PVp0B0gom3rGkl+CmKAgi9imJFQfM4mjtUcZQAaqHw5lX2RkShzOPgEbPiDnuZMi3UbajI1TK0F5cnm3x6vTt+Cv1eo01+4mYECTpBOCjOEvxMkTVnGAgqJ5ZlDu4Rvv+wwEBAJsCjb0epVkD7V1IkZnv9QVL71u7Jwwy7Xr7rlEeYq6JpUg0dalb9zOxWmXjnQmz7RB5eshR+SUb1hAXsTzaRAp+kf6Wa86izDrpWDOFgqApjOHvOcSK7a+3vWoWUZM4DaBtQj/0qqEeuXSs2Y3psgNEiyj2NTlV1mP8fKDgL29SxAYjuYG8HFUcIjLJBe2BgaTwFlvP0C6QXnXd2b3Db2EU2z1LVVgKjLJRlx9NbmNs4qxed8LyDx2+pinXayMPssv03xbMSsEWdpdhzvuN8wZWBcztMyjzVWB82fVtAfd1fdnLI5v37MHl4w5XyIIj0tso56bsNjBbQJK9esmj1hTL6RrbR4AR7plQ7kpAbXHYuUqHVt4cf0uNsLnYokNg37QYieQv1xbhzu6+buoIYG0CU+wKB7KphmtVsEmoud8Y6HoeRy/+1Pqp/8l/8G2Xjqkt3a3RJ6NHuGpg+NkMuXBR5q4oLfplrAq7tD0NRFfVXSOwQyafFVJzlYf4QtJhNJivN66FhQgeA6KMr3Q+4/fUKhaj6qyCec3GO4X51tcguHoGZGiZwBkxI3nG2NORMIqByz496+M0pvopwJmYZz/QlqG8xPSr8dswdNqjq/9PF8tlDaD3ZdVM0r64Sl3+GNz2ZCyTKpEcp0YH7UUFH1Gi9ifss84RGOrB5aIGzPLMBuf8F9g7OC+281fH1tUFCW teA0mthp vo35J9v7sK+bLQwjRCEr34cbxayB1JaX5jsfNgHc9uUNf3BVw7mBndEcbZK0hdtw0rpqP3Ore3frwQBhRUF2gxvAGb+u743xUoaS6HBAYr/VeucmjPfHFs2sv5UitXat5RbqwF+gtzlv7w7bGq7wgac7vCFfnHkmCKIgARbwQhBBtl/JeKK7E7gTQptPBdKUWw8d/BVTRT/9x+urV0N2aVkhWQepbhf1sG4kqdxZcOILvelmKvis/1uBRoskPfMBNa2vcn/2EfGBBXnF6OEuXhDBPuw5QZSa7AE147yBEcX1QgpC9xH4lTuMROChq4qKfrsAAiVlxVwFTIYlnh+iVGAfAPZwG9jrGME8++3M5YDVqruyF+J9ej4gVVtsrofCTS1OPiQ8aCn9D59AfGXdeeI+mFSADTi0nQklVAA7R3SYUHhkDp0j34DphAlRcYzJaTQb91G6OZy2BLok0HcIsiBvfZZMHAyMZfBO4/XHU5uZuLdPJB+L+5A/7rhjnPxR739R+BPCE+vsRpEw= 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: envcfg CSR defines enabling bits for cache management instructions and soon will control enabling for control flow integrity and pointer masking features. Control flow integrity enabling for forward cfi and backward cfi is controlled via envcfg and thus need to be enabled on per thread basis. This patch creates a place holder for envcfg CSR in `thread_info` and adds logic to save and restore on task switching. Signed-off-by: Deepak Gupta --- arch/riscv/include/asm/switch_to.h | 10 ++++++++++ arch/riscv/include/asm/thread_info.h | 1 + 2 files changed, 11 insertions(+) diff --git a/arch/riscv/include/asm/switch_to.h b/arch/riscv/include/asm/switch_to.h index 7efdb0584d47..2d9a00a30394 100644 --- a/arch/riscv/include/asm/switch_to.h +++ b/arch/riscv/include/asm/switch_to.h @@ -69,6 +69,15 @@ 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) +{ + register unsigned long envcfg = next->thread_info.envcfg; + + asm volatile (ALTERNATIVE("nop", "csrw " __stringify(CSR_ENVCFG) ", %0", 0, + RISCV_ISA_EXT_XLINUXENVCFG, 1) + :: "r" (envcfg) : "memory"); +} + extern struct task_struct *__switch_to(struct task_struct *, struct task_struct *); @@ -80,6 +89,7 @@ do { \ __switch_to_fpu(__prev, __next); \ if (has_vector()) \ __switch_to_vector(__prev, __next); \ + __switch_to_envcfg(__next); \ ((last) = __switch_to(__prev, __next)); \ } while (0) diff --git a/arch/riscv/include/asm/thread_info.h b/arch/riscv/include/asm/thread_info.h index 5d473343634b..a503bdc2f6dd 100644 --- a/arch/riscv/include/asm/thread_info.h +++ b/arch/riscv/include/asm/thread_info.h @@ -56,6 +56,7 @@ struct thread_info { long user_sp; /* User stack pointer */ int cpu; unsigned long syscall_work; /* SYSCALL_WORK_ flags */ + unsigned long envcfg; #ifdef CONFIG_SHADOW_CALL_STACK void *scs_base; void *scs_sp; From patchwork Fri Mar 29 04:44:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13610046 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 56BD3C6FD1F for ; Fri, 29 Mar 2024 04:45:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DE2996B008A; Fri, 29 Mar 2024 00:45:23 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D93186B0092; Fri, 29 Mar 2024 00:45:23 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BBE976B0095; Fri, 29 Mar 2024 00:45:23 -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 9964D6B008A for ; Fri, 29 Mar 2024 00:45:23 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 58884160F62 for ; Fri, 29 Mar 2024 04:45:23 +0000 (UTC) X-FDA: 81948837726.24.369BEF6 Received: from mail-pf1-f181.google.com (mail-pf1-f181.google.com [209.85.210.181]) by imf08.hostedemail.com (Postfix) with ESMTP id 82C5516000C for ; Fri, 29 Mar 2024 04:45:21 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=gzC3s+aU; dmarc=none; spf=pass (imf08.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=1711687521; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=3HlYvLaJr4l3bVtwLdV9ET8gn8bkDD3ErKtF/Cp9Ezk=; b=LkjtXqr8M0FT90yzDR07OPXDu+l1nJOfHvSClBmvY3CIKv6xAWZ4/EyS7STXYsSl6zWLxR goIbd1Xx1lSP1HgmEWs71GEjZ23OzHC6BXwp2Trt95ZRYZ1j3jv2US/k5yww3+jGlfFejq YFKMuV5s4DYki/eGWO7nuxgTGP7CKSU= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=gzC3s+aU; dmarc=none; spf=pass (imf08.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=1711687521; a=rsa-sha256; cv=none; b=nUdieyp9L/1Xl5SzTY+vQqYojYCB5rYdsy1BlI8bSdlNHCZP+TAV6yhAePc9bTnSXg6tgI eDnY58OUrFOo1xCFQLM7hbUKrQveM/LwNVaBGnslNNS5CowEDmpm7wRebsNxWPpguY4bat 2IUhXqipKMSkpzzQ8+lf9uPY9wcx5+Y= Received: by mail-pf1-f181.google.com with SMTP id d2e1a72fcca58-6e6082eab17so1501181b3a.1 for ; Thu, 28 Mar 2024 21:45:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1711687520; x=1712292320; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=3HlYvLaJr4l3bVtwLdV9ET8gn8bkDD3ErKtF/Cp9Ezk=; b=gzC3s+aUCJgkJNbuciXpYP9AOkY8z66FAtVI6pziilEDOTXXqGKS0GJDj8C0H45G6l vNprqAkZC6xTuFW+EQBpY9Xhi0Z2sMJ3Oo/NIGKCeGK06Do0eapy8NUHKxFg/t58Z1F+ lX3psSzMuTIdbdUuPVnFlxIdSQa9evjLHBOr5Nu2gCHfsZSqnJIJgcGvBXtXTtDD6d/I OECPhY0AiBprLA/VXgQGIznTK386uBkBb0fSDwoqrapHFeSKw0JA44Q4zJk37ECyCvK7 q93G40IVIwTnhshibmi5MNw0rpBsDbpRzyC+SAIlOSnl3kwlf7nvKkUV6LJ1J9OF2ZHz byXQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711687520; x=1712292320; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=3HlYvLaJr4l3bVtwLdV9ET8gn8bkDD3ErKtF/Cp9Ezk=; b=mGNqlGv32GbyV5/dsmp9Yc5WBvwBdDHaI9Ggybpj1K39GZ8mAPdzV0BYP/Ik2Gps+G SqepYA2cLkI2Yix29VjDVyZn67epxvRKFsVNaOQUXpseqa3V6MngIBVJ82w83RbUS69+ JYLHBZsA1uaFclA41TLRij6OX9gQPZVaBPAwMXmm9aDbUx5KSq4Zine7fUhXDy6YpmFo HGd0MYrgoP8EHWs9mfZKI09iR3t6gchPMBLCf7g2+BAJMq0Y9LM5ni2WYTtE44sxTmRN zw50Bf+ho1DXZtp6h2aQzsaN+bXQqiOU5cCvck3L9R/cv3+JwuV8/H/IrRX0c56eh5I1 J+6g== X-Forwarded-Encrypted: i=1; AJvYcCWzlIQVk2Ax/+B3alRq01mAGy8aIF4EUDI+2Jtfl4dJRFLmdzTKfTynBKkBMZQ6GsjALUogtKnA3nfeMQt2JG81pWM= X-Gm-Message-State: AOJu0YzFLilwIoED80NIwgS9tBI4pNBdu9HAmtRtFMOaWwcfdJNtIH2r aPB+kv8I1Ur3HnpK4sE6OmjRzNMDc7lql36kdj4aEhOzoKC/AB6pC0xSeg09Y5Y= X-Google-Smtp-Source: AGHT+IFTo+txCMZKEE06IZ+V4dIOgngRem/aEEKbckfraBrrpL/AYZj752mW4B4fwSBOz0hRyqWgfA== X-Received: by 2002:a05:6a00:1c85:b0:6ea:c7bb:187b with SMTP id y5-20020a056a001c8500b006eac7bb187bmr1078563pfw.5.1711687520251; Thu, 28 Mar 2024 21:45:20 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id i18-20020aa78b52000000b006ea7e972947sm2217120pfd.130.2024.03.28.21.45.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 21:45:19 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com, samuel.holland@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, tech-j-ext@lists.risc-v.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh+dt@kernel.org, krzysztof.kozlowski+dt@linaro.org, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, Liam.Howlett@oracle.com, vbabka@suse.cz, lstoakes@gmail.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, hankuan.chen@sifive.com, greentime.hu@sifive.com, evan@rivosinc.com, xiao.w.wang@intel.com, charlie@rivosinc.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, dbarboza@ventanamicro.com, sameo@rivosinc.com, shikemeng@huaweicloud.com, willy@infradead.org, vincent.chen@sifive.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, gerg@kernel.org, heiko@sntech.de, bhe@redhat.com, jeeheng.sia@starfivetech.com, cyy@cyyself.name, maskray@google.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bgray@linux.ibm.com, mpe@ellerman.id.au, baruch@tkos.co.il, alx@kernel.org, david@redhat.com, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, shr@devkernel.io, deller@gmx.de, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com Subject: [PATCH v2 02/27] riscv: define default value for envcfg Date: Thu, 28 Mar 2024 21:44:34 -0700 Message-Id: <20240329044459.3990638-3-debug@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240329044459.3990638-1-debug@rivosinc.com> References: <20240329044459.3990638-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 82C5516000C X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: fsm1g7bo5dupx7kbp8undtw85zk1o56g X-HE-Tag: 1711687521-944358 X-HE-Meta: U2FsdGVkX19XtwrjaAMcKrb6XpKy5zYOrAxrk2tJhaRRhfhvkoa0z94boYqdyTbcO7Oe+UgXG2gPs/6Jyu/4zTVnyLGJ14a1s/daeV4TQdMhML5NPBf21m9JB4yeJ7fT3sHD+IS/BAyhGL7AeDeViDgB9tus4lvobc+UFaELZXLs2S5T7Egz0k62kgEZ4WkGYmjKdAm5kf+DBUgMOB7lvaUUb6s0sBAV3EzO9geher0QPhdQx/XU6GGJ/8uQLoNucfYSSL71JDDO1r0bkwRjUIdRTH+wRbiQPN1pBHnyjl28un9gZHGQpsbYu4yMEB9uamy37+9G5V8SX/bo58EpNnV7ojg7hyL2AbbH5wyDHoLxdIltYUsGiJQFWJfdMkiVHcvSv2GJiWxuqMPd0MDdUVA6ZyftoxqijNbcO1nSOufTscNBgJF0/7K1LxH4uQNSpdJ5vPkLmibTrLA1MCEAB60BpoeLZMl98s3o1Gt72p7vFBVboMhIc4VVYFlWXp0nX5bWf69dqfUbY3kMHoiccH6BezkHPaIFTJxCYZ5CjmyfGGybwvPthqrM4O6H4oFjnRWU3p0T5G+GH4rKQu+yX6WuB2dPZ4kFOJfKqYeHgLUJ1Ov5YaygUk8ryOAEe5WN1OelNSeS9yCKtSNVnMNE/yCOUJdfx8UBgCi4QmPKwOJVYufdYoTDqUaBzvHIH7P6iYavOb2Wm7l816vjvmWra6L2KbU1bIE2r94gefQDRjdwmAtQ+iJbgfiWuawfLKpcw8x0pUUFoE6IYj1ywpJ6wkFgXeE+hOPLDCWLTzpxNbMhU6qVUHHgskNJ1dGwwBJhfxrOiuj6ZNLnGTDmbAomyN4JJoIYjpd+6NuzAd/jMHY04qK5bxxMdN+2N/GDs/NNhagHMa1CdxaM8Y1fMDsuIyKcAUryHXWdF2F6BIqnqT/XrhXsosS8eUeFIWWlCSBmH/pmLh/qEMGsWVHfuCG 13rcM3DX HQdvpLZf28FSZvakZDkeFZgojEXZXRF4968sA7XKyehIvx43gPb3TkNV1N3cx1DRAn9L8/MdbA9vGfaOhqak0YwB/Sb1PC7KOFsAL2cC2ZjTQGlKASumVNEokJTPLRhn753su1spcyG9oOiHHPWdKQTz66WxIN7glBWDBZjBXKcnE8w3euUjTNUsWQu5QR/nYV63CwSFg79/ZKyfKpXz/ltRU8KdCX5Q7YG2LRyNs8MdJ4CkeLKx8aMQhFfks4WWrjOBd+Eh7OGt9eK+jW6giFPKTx4Z+co5f/1SDcLmL+yk+WFDFs8Frx41kDTOUBDcD5Op697xkhdfIePON9l88AuCXoUdLs7S33LPA6kHGG/qsK7dN9nDMQf3aKqlRbi7nV9XA5jNc/iofippljEEAJ1LZTru9d/e+TBnY4aCGQ7MiSea29ZK3YwRRgyoP9zcRjNg98so1ml61y7ZObrn43OuU6CXiggee40dp4nNN7KmDbTmXnKjuV7TTMgvEV2/SPoph1ydyxkurRy8= 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: Defines a base default value for envcfg per task. By default all tasks should have cache zeroing capability. Any future base capabilities that apply to all tasks can be turned on same way. Signed-off-by: Deepak Gupta --- arch/riscv/include/asm/csr.h | 2 ++ arch/riscv/kernel/process.c | 1 + 2 files changed, 3 insertions(+) diff --git a/arch/riscv/include/asm/csr.h b/arch/riscv/include/asm/csr.h index 2468c55933cd..bbd2207adb39 100644 --- a/arch/riscv/include/asm/csr.h +++ b/arch/riscv/include/asm/csr.h @@ -202,6 +202,8 @@ #define ENVCFG_CBIE_FLUSH _AC(0x1, UL) #define ENVCFG_CBIE_INV _AC(0x3, UL) #define ENVCFG_FIOM _AC(0x1, UL) +/* by default all threads should be able to zero cache */ +#define ENVCFG_BASE ENVCFG_CBZE /* Smstateen bits */ #define SMSTATEEN0_AIA_IMSIC_SHIFT 58 diff --git a/arch/riscv/kernel/process.c b/arch/riscv/kernel/process.c index 92922dbd5b5c..9a85c9d4c902 100644 --- a/arch/riscv/kernel/process.c +++ b/arch/riscv/kernel/process.c @@ -152,6 +152,7 @@ void start_thread(struct pt_regs *regs, unsigned long pc, else regs->status |= SR_UXL_64; #endif + current->thread_info.envcfg = ENVCFG_BASE; } void flush_thread(void) From patchwork Fri Mar 29 04:44:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13610047 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 09A3CCD1283 for ; Fri, 29 Mar 2024 04:45:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8B5C06B0095; Fri, 29 Mar 2024 00:45:58 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 863C76B0096; Fri, 29 Mar 2024 00:45:58 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6DD2C6B0098; Fri, 29 Mar 2024 00:45:58 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 5191A6B0095 for ; Fri, 29 Mar 2024 00:45:58 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id DC3FD1410CF for ; Fri, 29 Mar 2024 04:45:57 +0000 (UTC) X-FDA: 81948839154.13.E42C941 Received: from mail-ot1-f45.google.com (mail-ot1-f45.google.com [209.85.210.45]) by imf07.hostedemail.com (Postfix) with ESMTP id 1766D40010 for ; Fri, 29 Mar 2024 04:45:55 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=00wb5pqw; dmarc=none; spf=pass (imf07.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.45 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711687556; a=rsa-sha256; cv=none; b=3whAr6PykZLGooSvNevnBHTSSQt5VrCkf5Zt0+yYV83PTLGqBU6Uu11WTr6ZlXrWf15LCS VI024mXk391yzGsaqbAtRO1Lii8rNUeebi767P5RTJVtPwGlVw5OCfDo8JDfV2zkoSR51b aXlrgXh8tFtpO5y0RM5TZ5yeDHkpcpQ= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=00wb5pqw; dmarc=none; spf=pass (imf07.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.45 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1711687556; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=qng37K3M8eikqBpPaGOFEE/tLaKw4gdTCP/Sk21HmlA=; b=rWX/hThcFgUOA0Er7dhj8ppXUNlsco9FRcGzBHOQjvZM4oHFp+h+1pREdEVvTolJhpP2Cc M9XLlDZSelGO0Sa/kQE/t6o7gBY8fLw4D9XvjAwHf9Bsf3Qc4shFWRlbPNsYTsF8ZLfibO +khtbr3/JuFNJny6KSbZC6zwWT9kbjo= Received: by mail-ot1-f45.google.com with SMTP id 46e09a7af769-6e445b4f80bso656309a34.0 for ; Thu, 28 Mar 2024 21:45:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1711687555; x=1712292355; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=qng37K3M8eikqBpPaGOFEE/tLaKw4gdTCP/Sk21HmlA=; b=00wb5pqw2RalWFL+VI3c/5yRRRqBO+ZUoMr0aDiWeBdRf1kPIx3+QhEx6woyqE57dS KwOwHaQP+Jvgx1E9NRdIg8cLUp545VV3SVMqG+dlFWPBPI3IhcE3jwSBUsTIAL2Y1+6v zxWkDJ2bGhEA8RUIDpiyPv4sQWINmcviDUfcz7ACRzhaPfjU0qr79CjAfRIm5B/fB/5s 8QTi8Oj+9ywamyrwy/K6MrVnd00iEZhterz8us1EEXTzKaiBcA7ZduiKZmjIc9ZFeL0Y vfeWfG0ekY1TKnvUNIUDgEx9XVbIbr4xXQoRpAfrgWlhpTR4vKAxDTqN3Xk41Pp3O3/7 9etA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711687555; x=1712292355; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=qng37K3M8eikqBpPaGOFEE/tLaKw4gdTCP/Sk21HmlA=; b=XVXZ4+trjSAwGxBZRn/oay+5oD6oeWjSB6uUlao+j1FkY2QJsAGrR2dVTsrUTc76LY ms9lDLJZTHbLaFSJVDOH71/Bn9P5oKhzcmEnpiT0n+5jU91h9EvEh523HKbnFI4wDLqy C8sELa1MTlyJBWkwhvSaBeKAKVPGgMLw0+0fRhzXwP+dFvKayTFwcdFyofIk+A5MiXKw WUKvAuVL80FgPpgLXO0RyQET65ZB6F+ntbr9YSWylDBzQ9KbS7hP2wAAnH3BARngVYXf 8pK7X+ynoevzASjl4Ak05bTPvzxy18n1BCeOHd+a73Q4hLTd4zAxLODGHb9orM52TH54 3y8Q== X-Forwarded-Encrypted: i=1; AJvYcCX/jXT2mvtgOLFUBzmDtyBohZOS8i1XvAPgp0ONs94NfGTSB3SuIouYcqNZScQ1h4Ex818EVOHFOvaoknCKvYNRZyY= X-Gm-Message-State: AOJu0YwtUidvnxKaPCO0T7WD/z6TsVXAT7uDghuHkg79gUIyY34pKu8e MAgiVJTWjVSCcinc7ZmrB/dzbtRoodlzKrWLZdIO41nofSo7XRwzZaLJ3/oj0wE= X-Google-Smtp-Source: AGHT+IEKOjTE3dTftWOMe2SOt89v3AzQ5X6q/KOwkPfLyYHAC9S2vY+NFOYACNL9schcS27A//V0AA== X-Received: by 2002:a05:6871:4096:b0:229:9cb5:4fe3 with SMTP id kz22-20020a056871409600b002299cb54fe3mr1713256oab.14.1711687554880; Thu, 28 Mar 2024 21:45:54 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id i18-20020aa78b52000000b006ea7e972947sm2217120pfd.130.2024.03.28.21.45.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 21:45:54 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com, samuel.holland@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, tech-j-ext@lists.risc-v.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh+dt@kernel.org, krzysztof.kozlowski+dt@linaro.org, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, Liam.Howlett@oracle.com, vbabka@suse.cz, lstoakes@gmail.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, hankuan.chen@sifive.com, greentime.hu@sifive.com, evan@rivosinc.com, xiao.w.wang@intel.com, charlie@rivosinc.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, dbarboza@ventanamicro.com, sameo@rivosinc.com, shikemeng@huaweicloud.com, willy@infradead.org, vincent.chen@sifive.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, gerg@kernel.org, heiko@sntech.de, bhe@redhat.com, jeeheng.sia@starfivetech.com, cyy@cyyself.name, maskray@google.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bgray@linux.ibm.com, mpe@ellerman.id.au, baruch@tkos.co.il, alx@kernel.org, david@redhat.com, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, shr@devkernel.io, deller@gmx.de, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com Subject: [PATCH v2 03/27] riscv/Kconfig: enable HAVE_EXIT_THREAD for riscv Date: Thu, 28 Mar 2024 21:44:35 -0700 Message-Id: <20240329044459.3990638-4-debug@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240329044459.3990638-1-debug@rivosinc.com> References: <20240329044459.3990638-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 1766D40010 X-Stat-Signature: t9jddat8e9wrrziquzcxhgzuw95mzncb X-HE-Tag: 1711687555-268216 X-HE-Meta: U2FsdGVkX1/8HiGoygi4+X5O6o9Kf/FN2RdpGt5WBqDVvgCOpVu6TVRraderWAsOVi7AE/71TX73hYjBsKpcXGITV3OPAKBIcyHBj8kaounjU5CyIM6zTKtSPuLM4chSFWiXN/uG9KzIWn6IDXZDhkwikbyXHZB+50sQeMCoEEioPs6ZXfSznOjL8vdy9PlwSTUJrVhK5NxkCaax6oPz5GvQVp6cJ18Ny334Pw8elB29+vyTrozoDgE74ci5lTXcRrNVo+MNLk3S14V6EunIR5GuGZSveSin3pfkShBie4rZK9+MHhtMk8Otv/VGjWHyiKzx9xv7IyxfKDcz7szgCuUr2x+z/2Cz7iJbwQpcXaQhO15fHhPtT+wh5dldT2a9BwS0Ei4Qk3FPnCeAnT3dFSTeiLT7JB7F8HytuuoNqbjzzF8VMDfd7VuHb4B7lj2AzkMAs7QxrxTtnwyUKkg9Il3z6KlyOfqmVDgETWUWllhyyMIGI0rcCwLTz2rrhNcMcvMlWEySmnRHf1MJTMuM7TxzNVkqVY/kclJLfIyQNDTwu3WN4ur4a/A8zi2rjToxGscWS4ct+Ep/Fg5nYUvQsyDoEMtqPckgcNxij1Bt7paOp9atXaJ6vrvnKyV9pVDsfVVrd4ofkfsf1+ANGYHCPMjZyn3hsbP22VfRFjhukwD0AZc6tKt4yTs1Wux+ouXex6hROo3Qny53QVVpRv2Le63cZerRyu7/pl0daaMt8dGXDaMvj4rpz21FWP7Q/niAls1FEvheJbeDQXKbWDVu1ekVFk/UctY58Ixy2o14UVmXU5HlVl6zhU6fVYRPgfLyc9a92QTSw1m52P7Wzk3FjPXY7Eqdhct4JSWIykxBUX7tx1wC9LMC/AdMQF5L633U0prIfvR/dnojRYkbg8UY78pqP3UU0VyeJOoA2eivnR0aRrAI5yyMDqI+3tc8+nIhdq5tzHzlEruG0MV+IMW XC8RsJsf A14itOJCaRSWQRPKfSwfDSV5T+Pa5XAzh05nhn8HXLkfkAl776SNJkE1IGxouSJQGbylCHxvcSWFXaXFVYwbB7MOO+xSS5uK6GKMHAKPNy79s/qd9lAs8VyCv5HM4fNz2m0sNcgUcO8sdXmAu/iifqAIRBzw1AM6CeaVXqTqMRY/SlcZ+OINRGRfGjpbkzQg6vgWAoNtDwq2Iox671JFmiO5Cmlp9Uwhm2/p7ehktiumUzyd4h7u5rBupBMQjpBnNBrZc7yIrNNRsmqJLuV2kOu+u7ATgLOdhOkcQgraT+Yu7nrygX1gMHxheRiJiHqUaCK4XSPHoccoTOlv2UWNrKtSqUh7uKGMifr4NiiOo4zkwYtYWbEnpwJaj4dKw1ZAfRb51iSM2A3WSFQpci6pHT0K8zFjgbyMU+b4aYIXQFyMqDp2ZrAMRABjdteKGQfzfgWx2VkMpGeCEvqij117XTGpBRjeINMvgHu8/4OJTnkbHW4kyBDwmaaEENdP5eXMX5LCyLe+EkvDpI72N9semOw473hawJ5pr6jCoBoz410k59NRp1xgmH1fWwQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: riscv will need an implementation for exit_thread to clean up shadow stack when thread exits. If current thread had shadow stack enabled, shadow stack is allocated by default for any new thread. Signed-off-by: Deepak Gupta --- arch/riscv/Kconfig | 1 + arch/riscv/kernel/process.c | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index e3142ce531a0..7e0b2bcc388f 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -149,6 +149,7 @@ config RISCV select HAVE_SAMPLE_FTRACE_DIRECT_MULTI select HAVE_STACKPROTECTOR select HAVE_SYSCALL_TRACEPOINTS + select HAVE_EXIT_THREAD select HOTPLUG_CORE_SYNC_DEAD if HOTPLUG_CPU select IRQ_DOMAIN select IRQ_FORCED_THREADING diff --git a/arch/riscv/kernel/process.c b/arch/riscv/kernel/process.c index 9a85c9d4c902..d864eef5a10d 100644 --- a/arch/riscv/kernel/process.c +++ b/arch/riscv/kernel/process.c @@ -195,6 +195,11 @@ int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src) return 0; } +void exit_thread(struct task_struct *tsk) +{ + return; +} + int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) { unsigned long clone_flags = args->flags; From patchwork Fri Mar 29 04:44:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13610048 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 94FBECD1283 for ; Fri, 29 Mar 2024 04:46:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 224046B0098; Fri, 29 Mar 2024 00:46:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1D5876B0099; Fri, 29 Mar 2024 00:46:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 026BF6B009A; Fri, 29 Mar 2024 00:46:02 -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 D92FB6B0098 for ; Fri, 29 Mar 2024 00:46:02 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 5666681136 for ; Fri, 29 Mar 2024 04:46:02 +0000 (UTC) X-FDA: 81948839364.19.76D82F8 Received: from mail-ot1-f46.google.com (mail-ot1-f46.google.com [209.85.210.46]) by imf13.hostedemail.com (Postfix) with ESMTP id 85C132000C for ; Fri, 29 Mar 2024 04:46:00 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=XosRXdBz; spf=pass (imf13.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.46 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711687560; a=rsa-sha256; cv=none; b=Ew9ZXTxCGbONHV6ZeX5pFoHP6XnOznBlcvpKazYAnuBcBwu5lx9iriD0NmQakQ4TZ4AgYj o8aa5TpUOzj+rS0qJf7eGrL5eSPjlfEjRLKMAqSec7kTvmWixBgQGFROzOWlpwSYBuYUSG m9cndmLqYjcJcHVuvbXkFg4n4GofiDE= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=XosRXdBz; spf=pass (imf13.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.46 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1711687560; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=BgqTcRj2U8Se9b0MFn8iBZ1MBZsdImVYllpKzi7FoF0=; b=kTJyo/QRriLaqmiKgJXhfdGRPVtF1rzpLSN+ksHVVqKrocOM57rn3CQzThCWX5/4P3JPVc TgqDRBTD/eiiCRmpxDsK7dDixmYHtcVlXP4k1r8xTPXMDfixQxpeBOD3AV0AuLKF0bWK7y T0ZWCvw42mkvrUCamm7XCLPqGneUXXg= Received: by mail-ot1-f46.google.com with SMTP id 46e09a7af769-6e6d063e88bso1053382a34.3 for ; Thu, 28 Mar 2024 21:46:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1711687559; x=1712292359; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=BgqTcRj2U8Se9b0MFn8iBZ1MBZsdImVYllpKzi7FoF0=; b=XosRXdBz7GLaZJC6446cqWjr00suO+6Dso7Hs7XfCgiQR7JJdUF0tDkmu/ngQcjd/J b2qY6q+7c/5v1cpDYUixBB+KEUlADw0dergl7gqMzFVlh8BmX9OjPLcwMcfqSDiH+CL3 QnpG8FEIlRWI+WFAlXYZf0WT4ATXEbJFB3z9hhYPUyyppXFNo/ufayRxgCV1nSV67AKS OoOivbQw4pnQXav0DK4j0L1AaFeiOkpQugHUZ0K+2EgP7etDVauMgr5FM4F0A2TcmQEy 2yiXWa0bBhbbxBm98tbCx3EdokBw9OzGkUAWUxtsXP/MZcGuBZeQ/Bm1ZhlqjVNjPU0U JdMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711687559; x=1712292359; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=BgqTcRj2U8Se9b0MFn8iBZ1MBZsdImVYllpKzi7FoF0=; b=tKtSvn9R/4rdrnDixPzB+n9V53ZZ4srVvG666k2X7H78qkOhmxyTeUN98aX2x+iSxd Z+UW5OcjM57PKfizzWy2q5JnNRPceRmP7AnA3jJ60ZLK5LmRRNFBBVxgaccohc2FiFu0 oWfk6EJFcgxXAZ+5YkR6qXRP+Xct7c4EhFMwGhDR7LrH9wA3l7qZaiuEvMF4TVbIYhLZ oR87z0vwDq72I5GpWtYN50BG3eZNfBE/8gbU5nc3KGXbwWETw+E5gEib2Hdz9Uj8Atwj jdUr4Z2JKCr4qxrZqfFmX2udjNjGxt09nWM79LQyMpAF4Bzl/5VjEfjcm+Sg/vftyg9+ Q7sA== X-Forwarded-Encrypted: i=1; AJvYcCX1cFJrSdWOA6a7CNzHkT0vWOZ87LrBoct7GzEUhpYkEQYnm2UrAOoaIbSihMWA/ZwelH30ZCIEEy/GMgCCEK6caQs= X-Gm-Message-State: AOJu0Yzk7lVQ321l+Oh8pH0K7xEiLOSn5T2h/wPU+wpULc59Up/nodzX 1EAbNxYoUEcVIXaAsmnxip/fbthqQ32kx9Up7yNCJo0ptZSaaxtBUN2r5eFbNPg= X-Google-Smtp-Source: AGHT+IF8V+3oImQuk/A4crk7sPM/VnCwTyQ17ZYhfUB3qy96qUK1l2ScpaHLIegUvWCFYCKqh7Y5ZQ== X-Received: by 2002:a05:6808:1a05:b0:3c3:d477:62d3 with SMTP id bk5-20020a0568081a0500b003c3d47762d3mr1372369oib.44.1711687559607; Thu, 28 Mar 2024 21:45:59 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id i18-20020aa78b52000000b006ea7e972947sm2217120pfd.130.2024.03.28.21.45.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 21:45:59 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com, samuel.holland@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, tech-j-ext@lists.risc-v.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh+dt@kernel.org, krzysztof.kozlowski+dt@linaro.org, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, Liam.Howlett@oracle.com, vbabka@suse.cz, lstoakes@gmail.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, hankuan.chen@sifive.com, greentime.hu@sifive.com, evan@rivosinc.com, xiao.w.wang@intel.com, charlie@rivosinc.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, dbarboza@ventanamicro.com, sameo@rivosinc.com, shikemeng@huaweicloud.com, willy@infradead.org, vincent.chen@sifive.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, gerg@kernel.org, heiko@sntech.de, bhe@redhat.com, jeeheng.sia@starfivetech.com, cyy@cyyself.name, maskray@google.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bgray@linux.ibm.com, mpe@ellerman.id.au, baruch@tkos.co.il, alx@kernel.org, david@redhat.com, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, shr@devkernel.io, deller@gmx.de, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com Subject: [PATCH v2 04/27] riscv: zicfiss/zicfilp enumeration Date: Thu, 28 Mar 2024 21:44:36 -0700 Message-Id: <20240329044459.3990638-5-debug@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240329044459.3990638-1-debug@rivosinc.com> References: <20240329044459.3990638-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 85C132000C X-Stat-Signature: 8e8emmeof31zh7i31fq4iprh1hzabi6k X-Rspam-User: X-HE-Tag: 1711687560-224292 X-HE-Meta: U2FsdGVkX180JQ2qXvGTXxf/R1TqrJDji9TXK2BixJIDMwRapul9AaQ+i6tEIK9Di66JgMBjvT8sIUQNQXFo8aQS3ZTdbExrwmTKEdSlPrYvOXRLzymB/P6JPBccLfGEUDIaTa6HNopHF/D59G7Zjtwx2oRtW3ZtJ4KX5eMA2lG+YzWUmzXzRXIBM+tPMH5QtLlnjsoDFjxyfWrIp7fJ/uHx3g3JvSOXdhLFhkeCZjZKIDAqIbyAihNya3CgTZ0Wi4c6SqfIC2OBzhv7BO0nK+3viwgWQvMOro2fKmVatLnB0fhpIwhp+1khBPBmayasS0EYHJyoRZUNnKupaXLs93Zj9Rj5WTEBgJdCf61kOY8peOv0d30lIaNH2NEAomWBth6bnC6z3hWhLH4+vA+ZrY+bDZgRQsSx1qFtAttztlAbjP0BZFuS8Fe0aPwTE2cZ7Ub1yMMTETwq3makgthPTi0U/yCfW0IppgjQWrdby1Cwu/mOsS0Jm3ogBqaavJ4vTXtPt+iMBj/Sg7JR8JDmiv+3ARHQVOhdtiX5hz2blpiPKZ0SPqK8AIRbW6bCvQb3KenqKBJhDkKUA3rYOOXyXppBijUqGz9nmdnlP9tUs/UFYbZMH0TxCd1hHRy0s9qrKTM189L4aRpdOFBAmYJfmNjmJVt6NWVfQTeLw36UpMDTWvg7Yrg2KNaDe6LjZNEub6yjTGoBBQNw6MzCfwX8EoBv4c+wA2fwGp2dT/jjj0Noa8Ub3eyE8Zh9Ppi7gpy2GwNDZJ6CMCWJXBLWRI/JZO88OcYa15puCLuB2xq+uicAcEQYC2cpj7QlrP7PSEHT6GgSIFzCsKNgqLzsY76OW8h2IAfbxXzy+hJapSBkQXdWj+llBakw2AKzQVdNCN+eGMIb+n60mH8fN2RIJ1uuSFVbfJScY7ZHLAygWA1qiARt1xoaK+ZQhe19VJSxfitH/VzYX+fhqQv0RN/fhV6 AFPkO116 dUirWkI9+ENwNGm+maJJIe+3aziVxmv3nSVvuF+d1+EMQsVJul1PSxSQ4Zi/A2dfCs3448xdKFt9z6hPHIckK9jLs/LjVMitelu0wLRPtgwRMeRLekARPAOVZUZHwH3jzkjvjLhFsASeysRrGuarpYJahRrt4z1hZ+EoaJlW6fBv2iJe4d1lPtlf9G0cz0EaqKBKB7w7ZrwRSK0gaYWkVOPMZ5oWKEJBvdkT+OQ3mp599/4OcP4MHPYwnKmB600CI4VpdUWevWuueP61BDboaC4O+2co8e7GINpVZxD4yZtIsHhduhRqZvbqeGYSjwMOH+rMCRau51IbYXD1MMkexnoruZUVAYDVlz1CH6afYI97iMocV0BQ4KfUHeDN3RsZ2GAhmpKzEkagSSknx9rD7s5+0v1Wz/XmNNf+9UoeGUmF6287BiWD1HiD0390r+KPSXYT3MwiH9cLY9YSeSac6ItpZ80nQxkcm2+JOuUW8Nz/yLzoDN/eKO5V0dEugLAVxtR83VSiO3fjkkY4= 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 description in dt-bindings (extensions.yaml) 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 --- .../devicetree/bindings/riscv/extensions.yaml | 10 ++++++++++ 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 ++ 5 files changed, 28 insertions(+) diff --git a/Documentation/devicetree/bindings/riscv/extensions.yaml b/Documentation/devicetree/bindings/riscv/extensions.yaml index 63d81dc895e5..f8d78bf7400b 100644 --- a/Documentation/devicetree/bindings/riscv/extensions.yaml +++ b/Documentation/devicetree/bindings/riscv/extensions.yaml @@ -317,6 +317,16 @@ 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 0036ff2 of riscv-cfi. + + - const: zicfiss + description: + The standard Zicfiss extension for enforcing backward edge control-flow + integrity as ratified in commit 0036ff2 of riscv-cfi. + - const: zicntr description: The standard Zicntr extension for base counters and timers, as diff --git a/arch/riscv/include/asm/cpufeature.h b/arch/riscv/include/asm/cpufeature.h index 0bd11862b760..f0fb8d8ae273 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 @@ -137,4 +138,16 @@ static __always_inline bool riscv_cpu_has_extension_unlikely(int cpu, const unsi DECLARE_STATIC_KEY_FALSE(fast_misaligned_access_speed_key); +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 1f2d2599c655..74b6c727f545 100644 --- a/arch/riscv/include/asm/hwcap.h +++ b/arch/riscv/include/asm/hwcap.h @@ -80,6 +80,8 @@ #define RISCV_ISA_EXT_ZFA 71 #define RISCV_ISA_EXT_ZTSO 72 #define RISCV_ISA_EXT_ZACAS 73 +#define RISCV_ISA_EXT_ZICFILP 74 +#define RISCV_ISA_EXT_ZICFISS 75 #define RISCV_ISA_EXT_XLINUXENVCFG 127 diff --git a/arch/riscv/include/asm/processor.h b/arch/riscv/include/asm/processor.h index a8509cc31ab2..6c5b3d928b12 100644 --- a/arch/riscv/include/asm/processor.h +++ b/arch/riscv/include/asm/processor.h @@ -13,6 +13,7 @@ #include #include +#include #ifdef CONFIG_64BIT #define DEFAULT_MAP_WINDOW (UL(1) << (MMAP_VA_BITS - 1)) diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c index 79a5a35fab96..d052cad5b82f 100644 --- a/arch/riscv/kernel/cpufeature.c +++ b/arch/riscv/kernel/cpufeature.c @@ -263,6 +263,8 @@ const struct riscv_isa_ext_data riscv_isa_ext[] = { __RISCV_ISA_EXT_DATA(h, RISCV_ISA_EXT_h), __RISCV_ISA_EXT_SUPERSET(zicbom, RISCV_ISA_EXT_ZICBOM, riscv_xlinuxenvcfg_exts), __RISCV_ISA_EXT_SUPERSET(zicboz, RISCV_ISA_EXT_ZICBOZ, riscv_xlinuxenvcfg_exts), + __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 Fri Mar 29 04:44:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13610049 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 41BABC6FD1F for ; Fri, 29 Mar 2024 04:46:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C0F846B009A; Fri, 29 Mar 2024 00:46:07 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BBFC36B009B; Fri, 29 Mar 2024 00:46:07 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A38946B009C; Fri, 29 Mar 2024 00:46:07 -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 86C2E6B009A for ; Fri, 29 Mar 2024 00:46:07 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 4DF2B121102 for ; Fri, 29 Mar 2024 04:46:07 +0000 (UTC) X-FDA: 81948839574.15.7EB544B Received: from mail-pf1-f174.google.com (mail-pf1-f174.google.com [209.85.210.174]) by imf29.hostedemail.com (Postfix) with ESMTP id 736EB120017 for ; Fri, 29 Mar 2024 04:46:05 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=tOGQYywU; spf=pass (imf29.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=1711687565; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=8I6BxlsTuM8aPnLudo3zS/ouO48NeQKH2Qm0sPb12tM=; b=u9CFm11l0fjZMirUufuC6eqfqTvmXqfIbxfO6vKfGDo4jTBYqgrj/HIXxHq8dcWUZ74479 fdumGFeOU52NSmZfAxdDfyKseq54xVyO2X1TqbfMBjyu44liUfzocc2FRggdQh2QuuWw7u 62XwWpl4uj+GqbWN45xgJu2N31MRGYk= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711687565; a=rsa-sha256; cv=none; b=Pr06P1xaNXpD3ZUBy1cy0ugSQur+tVQMjRfCNvXzyx9XjD1VJCrbo86gDmOYRXBhrrL9/A U27MtVBwXV5khOs0UyrUvHGlAFyh1JLXPw2/ylVCta5KJDlv7B6oQ4CKZxff+NKkz9E9Ss sY/cSVplYbbEv7JjAz+b9OuZtY/gGOc= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=tOGQYywU; spf=pass (imf29.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.174 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pf1-f174.google.com with SMTP id d2e1a72fcca58-6e8f51d0bf0so1448740b3a.3 for ; Thu, 28 Mar 2024 21:46:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1711687564; x=1712292364; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=8I6BxlsTuM8aPnLudo3zS/ouO48NeQKH2Qm0sPb12tM=; b=tOGQYywUcxa6y2yLXTqnxB6gXy+pBZwawZjjU2wiQg8w5zckhIYWDlDN+UYjunTbNj s4YR1lw0WSRRZ7K8IsZ8p6TU/GS63KJoG5a2IrtQ6I/mdggzTo/Dle5SYllJlpdDZ7xo VIWs43xQw3qFhfRhz58693SrWID/xCItCAOL5HRmuJ4v3MJ4vZ7WrCTKkbdxm5CNxSG9 nUgaOChjUSUu5UxOvXWOdGsk8zgSiY42IGmori72Q6i3pluH/LgC3v346BmnuY4eFWGt vsY3cap/eSihVtKslr2zLPuW8gU0QxVxpdvao4AfSLFqC2NssLQKNJJB4feuKRs9bb9G ybrg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711687564; x=1712292364; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=8I6BxlsTuM8aPnLudo3zS/ouO48NeQKH2Qm0sPb12tM=; b=j7LOoZX18zTcS+S1iT1QschkaRLto1OvquoLpTsh27ITRfh7PjMmcexRQ46z/U+vx5 DekyL5wktZu5HTGYNUkLBtsiIffNWs9xJGvXN/mHDq/HLjW69gQt2D6isUM7k8za8vBR 3sToxA+2Win8iVo2WSMN++Niq+XnA1Bq3csBCe/ORFC4Pa8UR349oaGRXRnQUcvfqvEC tH4x8ZHuLXyxNH4QnZCtvgYbdrFzO858ex1IDrEdjoW2j8gAl2j+YinFicnBMN5VHy/T +kRuT+OLQg+Y/d3hDReIw4scSWRCfNfDWETTW+xhBG5rBaD6I8Y+E54LFcELnasLzCqp 5L4g== X-Forwarded-Encrypted: i=1; AJvYcCVDhKTw/YniX2CoVTGu3G/eigDV8bOxwrOi2EKdwpG44oqaLLKeuG+4nNO5V13n49lFJTVKm9ObNbCcQq0rECJ7wj8= X-Gm-Message-State: AOJu0YyK8LS/fW6+nzLXDLqzFKPXElRdPa6IOFOZEUiGG5dGbR2Tw4js 9h4ptAJo39xDZYCllgTHlrTuCwzui9J4UWZw8TCJGcGaZ9UuObJZSy0X4t8aiPY= X-Google-Smtp-Source: AGHT+IHE6usyMFBQxvFagxY/LvFEXsQVHbAuxvgF7MaJ2nwwxjQI5uC10jSUwmgfFjplPX6uFLGn9A== X-Received: by 2002:a05:6a00:b46:b0:6ea:d792:b721 with SMTP id p6-20020a056a000b4600b006ead792b721mr1470047pfo.14.1711687564387; Thu, 28 Mar 2024 21:46:04 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id i18-20020aa78b52000000b006ea7e972947sm2217120pfd.130.2024.03.28.21.46.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 21:46:04 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com, samuel.holland@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, tech-j-ext@lists.risc-v.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh+dt@kernel.org, krzysztof.kozlowski+dt@linaro.org, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, Liam.Howlett@oracle.com, vbabka@suse.cz, lstoakes@gmail.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, hankuan.chen@sifive.com, greentime.hu@sifive.com, evan@rivosinc.com, xiao.w.wang@intel.com, charlie@rivosinc.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, dbarboza@ventanamicro.com, sameo@rivosinc.com, shikemeng@huaweicloud.com, willy@infradead.org, vincent.chen@sifive.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, gerg@kernel.org, heiko@sntech.de, bhe@redhat.com, jeeheng.sia@starfivetech.com, cyy@cyyself.name, maskray@google.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bgray@linux.ibm.com, mpe@ellerman.id.au, baruch@tkos.co.il, alx@kernel.org, david@redhat.com, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, shr@devkernel.io, deller@gmx.de, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com Subject: [PATCH v2 05/27] riscv: zicfiss/zicfilp extension csr and bit definitions Date: Thu, 28 Mar 2024 21:44:37 -0700 Message-Id: <20240329044459.3990638-6-debug@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240329044459.3990638-1-debug@rivosinc.com> References: <20240329044459.3990638-1-debug@rivosinc.com> MIME-Version: 1.0 X-Stat-Signature: 7ikpm9u1ip64gchhi1drbeixftrd57e5 X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 736EB120017 X-Rspam-User: X-HE-Tag: 1711687565-890271 X-HE-Meta: U2FsdGVkX18prR9jrLo15pRI6t4poVWyoX8W+XLktBov5JAbGu1akAO2FBNHZEDVTgC+xWFa7Nbskl8uIhs7ahSyzvBWF1Csqcxiy/6b7donN8B2vxJIMcbNc8y1K7nYM3kdrnbgFA2J0P5dX9YbYimvUcGKjb/o3mOLEvYyv2Jh3I7Qs1AhqgGMd2dxY6NQ7oAlio0S0QHpAjmYesrLXeIHM4+rspBDJx5/CQodEjUmDUfioYoFPadCkx71eBV0rt8Y5hLF50HQKt76M9kmzL7hQiI9W+nryy9KI+1PKKRT+Z79qo519IsSFbx5cmllyJhTC2B7SdT/2pziW8qu9Jhrtu8X0qmtbydGQuqiVqlSxFydgRrzuucTCp6wdR29GflbKfIkCYx0OLKip3wvg+/fbWhymXSvEPE9z+cbjLL8IRP1KLIpoR6QWITN1UkSv/Ap5h1m/BW7EHzP1R/kwJWMtVzN1Rjhl6C3BuzLaXgqyC0eULV6HX5nt2UADqq5PuSaI+IoIVUZhSBoVh8NW8n2+hGmI8AWiDn7o+GhUb6YkEYIJ2ZO74g9SSkz2yD9PK8QTHYOkf3s5MQ/1Nw7r8tNrQuSO/byjAdZRLCTbHXpwgtKU3J1AmGkXp4eIyMR1107KFrjq8c/BRBLtmCbUK5j4b7ZPE4RmabKCgK4yA8twz9G/i17/oyq3E5moZuUv74hCuTYkscFQLYiMVaJCf59or0kMoQATaVYkdEPNmTalz1CiVj4NpLcrWoxwSSHQ846FUwqYJsbW2aTprVH1xB6fozJHtyIsDfHin5iYq5OYom2+yIYQ+c26gYoq6k2F5tm+oxkRkWhyC2z1t/yGO67F4gRqjZ51o/JNgzUmqwb9aNIMbk2SbMHrA9ciM+A2IyebiOj4NDjJLcB7FPwOET5mkx2Rh7Ha/jhTWVvFP3xE44rQLhK7rUrWTVsNCmHJ4BK6xn3IcR4atbJRk+ O7E+bs77 8l1vLMgzTlT8KkMRh9u8JWsolHowrv/n+kxE5OCVCBQpg1GoeglBUt7xLBjiBbpibEl4U7QAByYVbVAsqz5tV9fg1+ElWdnrzgk33uCQvQSUizQMkccbPVdpaC6uO+K3wwC2V8AWiCeKIJ+/c/XTT3BsWoHW6UWKawdlTeVZFjeiY9Sw+56WPTqgB//ZJUsRsSKT6Px/Um8QvlRXxLuPX1HM5mdyt1162yHcimHtINF4zepaS1UshvB9sRzEaIZDTkDm6nj98S8Dj7J7Foz3H+GBjsCX5BKURxmg2Lc4A10bVlXVp4X9OPWrUejhYPSVNOBqSExOkK4tl/iM+tOa6vXveLXR09Q9uBfrvjTMbR8v/4V7DrejJqKjEvPFrZSHzNW4I9ElO4xXDqYSTGPVN627JXdFWYB0s9px603CM5U5trZSwBkb2es3bb1eMd4dNFQJWIVTV/eAn1ww6rauu8hI5vbvN7RFVLDg6YrdZl93L5QOywJhE8LB+0YJoBSXjfUlG7wHYAPOgp+E= 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 --- 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 bbd2207adb39..3bb126d1c5ff 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) @@ -196,6 +205,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) @@ -216,6 +227,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 Fri Mar 29 04:44:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13610050 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 2BA4FCD1288 for ; Fri, 29 Mar 2024 04:46:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B29736B009C; Fri, 29 Mar 2024 00:46:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A87706B009D; Fri, 29 Mar 2024 00:46:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8B3676B009E; Fri, 29 Mar 2024 00:46:10 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 6859A6B009C for ; Fri, 29 Mar 2024 00:46:10 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 2F789A10BB for ; Fri, 29 Mar 2024 04:46:10 +0000 (UTC) X-FDA: 81948839700.21.6F7ECAF Received: from mail-oo1-f41.google.com (mail-oo1-f41.google.com [209.85.161.41]) by imf11.hostedemail.com (Postfix) with ESMTP id 5642740004 for ; Fri, 29 Mar 2024 04:46:08 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=GGvtlEpq; dmarc=none; spf=pass (imf11.hostedemail.com: domain of debug@rivosinc.com designates 209.85.161.41 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=1711687568; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=iaRh0bZD1J49LQcAlor7p4SEY3y50QUlPea61yaZwvU=; b=WJHsrUqBWBWt1jmAJIZU9lV0YrpamGVlI9y7zpGVUiyrMwif4Kjo2zBlkMCO+Z9d9nBoCk StJn2aqNJ2ESzUaxR5aKV0XNaXwOlXXlN0jfmIlHruut5r8VaBpDKGtWZgMjTigtKbJAZD wLx/Dc4t3NzutKsLBjkCCiBC0OK5vcE= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=GGvtlEpq; dmarc=none; spf=pass (imf11.hostedemail.com: domain of debug@rivosinc.com designates 209.85.161.41 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711687568; a=rsa-sha256; cv=none; b=7I3hCDwLwu3Slo3jRsbJPil/dUtxU/IQorEdRuslEWDROESJOAMVy49L2LMc6zPUfwd6Kf TPHJ7pKG5ltHRvX9emJN3F2ZhcxpPjoB+n6WmnneNeJ2Z6ozs5cupsfUOVuCH2yt0SKQJX fySOGdncsgbevWOosGWtUjsYzSrRWGo= Received: by mail-oo1-f41.google.com with SMTP id 006d021491bc7-5a5362ceb7bso896130eaf.1 for ; Thu, 28 Mar 2024 21:46:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1711687567; x=1712292367; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=iaRh0bZD1J49LQcAlor7p4SEY3y50QUlPea61yaZwvU=; b=GGvtlEpqe3w5Obv6NHSX3cBiTVvcBNLq/6AlY4wQF1azEBaeEjB3Nv3vVBe+e+jgsy I1kGT8mDXldYgPmVMSwIUQnix+d6QHoKVoyWsQdNuaau7/Y4qx58dWi9ZZd5XXNHr2Si Ng8/pyAtb/xYQQNUUkR+dWbC1JVX9KipXLu+SWqqrDlCJgVuQHQXgB4jENoFJxIYx2JE 3V212VZ7VLIuSochPfExawtCLYRB/PK3XZCg4dGrHBgSO8LGlb4O2zMx6e95B0f0chAW co0dF+c4YWO7tDq8OxH1jriz17GS2NkFYl2RXQLjcFlmEedG/OokGVhfCvcWef1V8/es 6YWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711687567; x=1712292367; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=iaRh0bZD1J49LQcAlor7p4SEY3y50QUlPea61yaZwvU=; b=D4GCusQ5ejpgWB9k/JHWAFAFDGGq7ImkfmZ7PziHd4rO+Aot+VCXd8ilxomBDjHwwI tqhrAWM4kbsL4vFuhZyjMeMmPVQrMVswf5uD/eyNt/VXNeblM5s71WQT80RWLu7M2ynt oIGjIfJ4tttTWqzJB3PFZ8tkWKwm2yyYHayU3wV7c1SZ5WWQMPmSHkv1aBj3n+ZtGfgj yPMaW3cDw9kzIo6hrb9BYQV4I150yOeQ3SBoiV4jBpwloyBIgmayG1YonDrzyD0GzJ2L 94RAM7Md4/kPPe9wsogLoyOnckHMxqb/veVCs7Zq3SIy25rKHpM2X4qh0ozS2ma14hTE yXoA== X-Forwarded-Encrypted: i=1; AJvYcCWIDHeegPlG6eX3bzQQFBBWBwOwf/ktvWqoz/ove784I2dc+F/1ebFtii6JB4qVcUAUyEffIUKlxSaAJGH4Gf+fA/0= X-Gm-Message-State: AOJu0YwKLLZHFeBJ5m4rTUNaSSax/aTLiYHHOzBlgOZe4NpQYtWFQYl1 AfwQGOZDggpmtVI9A0UmasxcRi55R6xHu2VfLUN8tYRyvcm7omQdN+4oWFD+9Ms= X-Google-Smtp-Source: AGHT+IE9kWmyXBbaE5AFJPfIKBuGwtogZOPz1PpuRSBvlhnYIYRCVsT5HXaIBxM8bJ6Pf6POxqvejw== X-Received: by 2002:a05:6870:96a4:b0:22a:5f92:96fd with SMTP id o36-20020a05687096a400b0022a5f9296fdmr1377784oaq.25.1711687567318; Thu, 28 Mar 2024 21:46:07 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id i18-20020aa78b52000000b006ea7e972947sm2217120pfd.130.2024.03.28.21.46.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 21:46:07 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com, samuel.holland@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, tech-j-ext@lists.risc-v.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh+dt@kernel.org, krzysztof.kozlowski+dt@linaro.org, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, Liam.Howlett@oracle.com, vbabka@suse.cz, lstoakes@gmail.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, hankuan.chen@sifive.com, greentime.hu@sifive.com, evan@rivosinc.com, xiao.w.wang@intel.com, charlie@rivosinc.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, dbarboza@ventanamicro.com, sameo@rivosinc.com, shikemeng@huaweicloud.com, willy@infradead.org, vincent.chen@sifive.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, gerg@kernel.org, heiko@sntech.de, bhe@redhat.com, jeeheng.sia@starfivetech.com, cyy@cyyself.name, maskray@google.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bgray@linux.ibm.com, mpe@ellerman.id.au, baruch@tkos.co.il, alx@kernel.org, david@redhat.com, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, shr@devkernel.io, deller@gmx.de, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com Subject: [PATCH v2 06/27] riscv: usercfi state for task and save/restore of CSR_SSP on trap entry/exit Date: Thu, 28 Mar 2024 21:44:38 -0700 Message-Id: <20240329044459.3990638-7-debug@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240329044459.3990638-1-debug@rivosinc.com> References: <20240329044459.3990638-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 5642740004 X-Stat-Signature: w7uhemtrdb9sfg977uo6hmigrdg58ix9 X-Rspam-User: X-HE-Tag: 1711687568-703752 X-HE-Meta: U2FsdGVkX1/5CBd8bXvUJ7swihFfZE35pmMIxcSyZk3v8/sFA1lw/efJpQEf1u6h4QHgIrDSIYbTvLB2BKeKFJZEg5pyBWYEsC8RUD/10lPs23V8flauK3sFet4x/6UkvowWRj36cRm8FubRHQvpffOpVxyODc1mzJGEjJnJS0xC1nJyHQze9p+xTy21lU8BS/bg76rHLo4iQVOUvmPR1zC5rFe0L9/2eXdRaFKwCiF4+yoGhNY7QEqEbFH1Vp+EVp4VbJLm03VzqDFiofVhDKuIOrbi1EdIKrLGRu/vgAI3SB8hqpp8Luu4ufpl3CspYb8AGmOOpkSs00eWxXcY9msGIoRmNt3tudNzah+zc+gfnma/ff3bwBX4j8qj3m7aeF4sttQJcr61muJbL4j65dnXPLgEN/cnREIUnD3C3l0PCfTuXD0LfzOonwRyMFujciDGB5Hs0SvD0+GuVJAfuwMLdJzJilrn20FUfzA24vEaaELkhAjDrK/yqx9vz/w9NoOcMjA2d0/bOnvCWFd7RnaCuTtyZqGlBX0OudOBY13kUTUP7Gdb3h9f0rdLGd0mgdtFtszrNChT7e6Czzge5c1rMBtpJFkfVjaDeUv6B7s5nnUIWwsDe7+xG/x8Ja53XUrOl8+f+z9EEX5+Rm1042OQsN5i8wXajHgX7Kp45EwFPQCHAec92zOGTHfgtUbH2MTkBiFVBJ4LXepwrtAvYpQGBoWfzA1KRaGyCs5GQPseNNLliI/KqO0zbTaDDqDUAnlkjR7fS6IXfjdcNtolHwNz51pa06rw23lIS0AGENOgkfvtEulx/fctWOr7rEqmNEvfDvVbtkrMmz0Lm+8YFAkEKydYqgATniUuI8jsS9GtQsiKCOOcSFiooalLbxEnUlzH1grWLCS/Yy7crE2F+omtgP2d3peVOJXfUpk5eCxTqjyNJTe2cMWyxO3JzYKTFMXql15qrW1P0xtbeq9 zjvSDE88 +krLwSYHVfiDrx8pLrkAQvBpaPLw8YokCErJZx196Eb5vMHdFU4y9JZniQQEmfTUREwq5IXGOQW7UGCpS1YI5GDBaSMLnmtnNWS23D1AzHACA8tCRDV+KwKJvzafgNq2q18Ey3ojADYA5n3ZJQwqsWkjWDACYLvhg4cyyWaH0EBcrYye3DNuuMmm/y+yFuGunQUm6zmP6zKzOv+H9XWqkzxLJFddQg2R/Ja10sL88wbQlYCZ+tBRu/RMKecHYmFzGhIxPrcR9g9WDhQ2KQXnnrSQC2bouRrQHQ4XZCVvrNxIOt2S9m5szJp1fSA1o1iiToTjL5qgMmae6Xh6e5sKRAzAScXBgSarPR0U82wh99PxK5H7GsIqFA7yb6gpiaB89CR626YLs+mXQmWK+W+KQEXlQ/wXCegIUanPZ3hxHNPK2m1UXlZFgnDzs8W4dwoh7JxyJpiqqb1H6ZqvwHQgdsOLl8PJJXMLCylmsFUtZ/HTa0m2IOOVcpK7QtXRhJIh15KScoGmTklE+alddlbJQinPsIqjHQaCkfImL9EB04SmMoujyI19qEPXr/yDxaDR3k16q 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 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 --- 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(+) create mode 100644 arch/riscv/include/asm/usercfi.h diff --git a/arch/riscv/include/asm/processor.h b/arch/riscv/include/asm/processor.h index 6c5b3d928b12..f8decf357804 100644 --- a/arch/riscv/include/asm/processor.h +++ b/arch/riscv/include/asm/processor.h @@ -14,6 +14,7 @@ #include #include +#include #ifdef CONFIG_64BIT #define DEFAULT_MAP_WINDOW (UL(1) << (MMAP_VA_BITS - 1)) diff --git a/arch/riscv/include/asm/thread_info.h b/arch/riscv/include/asm/thread_info.h index a503bdc2f6dd..f1dee307806e 100644 --- a/arch/riscv/include/asm/thread_info.h +++ b/arch/riscv/include/asm/thread_info.h @@ -57,6 +57,9 @@ struct thread_info { int cpu; unsigned long syscall_work; /* SYSCALL_WORK_ flags */ unsigned long envcfg; +#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 a03129f40c46..5c5ea015c776 100644 --- a/arch/riscv/kernel/asm-offsets.c +++ b/arch/riscv/kernel/asm-offsets.c @@ -44,6 +44,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 9d1a305d5508..7245a0ea25c1 100644 --- a/arch/riscv/kernel/entry.S +++ b/arch/riscv/kernel/entry.S @@ -60,6 +60,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 @@ -141,6 +155,18 @@ SYM_CODE_START_NOALIGN(ret_from_exception) * structures again. */ csrw CSR_SCRATCH, tp + + /* + * Going back to U mode, restore shadow stack pointer + */ + ALTERNATIVE("nop; nop", + __stringify( \ + REG_L s3, TASK_TI_USER_SSP(tp); \ + csrw CSR_SSP, s3), + 0, + RISCV_ISA_EXT_ZICFISS, + CONFIG_RISCV_USER_CFI) + 1: #ifdef CONFIG_RISCV_ISA_V_PREEMPTIVE move a0, sp From patchwork Fri Mar 29 04:44:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13610051 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 70BF6CD1283 for ; Fri, 29 Mar 2024 04:46:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 002AA6B009E; Fri, 29 Mar 2024 00:46:14 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id ECC8C6B009F; Fri, 29 Mar 2024 00:46:13 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CF9AC6B00A0; Fri, 29 Mar 2024 00:46:13 -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 ACA7A6B009E for ; Fri, 29 Mar 2024 00:46:13 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 655748113C for ; Fri, 29 Mar 2024 04:46:13 +0000 (UTC) X-FDA: 81948839826.22.3A262FB Received: from mail-pf1-f171.google.com (mail-pf1-f171.google.com [209.85.210.171]) by imf22.hostedemail.com (Postfix) with ESMTP id 92482C0009 for ; Fri, 29 Mar 2024 04:46:11 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=g3FslMew; spf=pass (imf22.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.171 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=1711687571; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=T4DMhRzx46RlnO0Cr4Xo2ac2UzARWIzMLM1MQTYcvtg=; b=BRLlN/UwoD2ZFFAeyPrkSr15qnrgufy+a31fdl0CMEzMeU2eF30yZi9Uhk36mq/+StlKcY J7AA8Xwsv/bdKqCgmOoF34rDaf490bwlnG5+sBgAkPHBUa7HlC2zYKuTtUoM+8gbk0IMEV Rgd828ATkgbyOwyzOvlUljkQi6iMnVc= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711687571; a=rsa-sha256; cv=none; b=vLYhggW/h6PYpKGz5VLWe9ELOLr1Wea8GXkkBjCydah7z7ogYxPp6Ri9hJdkwpYhjaWK95 tBKA96y9JZ9owAD/dlo0DPlv167+WZwhv+IPjCgsfogYHAcGPphMjm3NVDQLxcQ+wEqLeH m6bnaQbdEzqS+jHgMS1uU5ziaSWJrsU= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=g3FslMew; spf=pass (imf22.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.171 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pf1-f171.google.com with SMTP id d2e1a72fcca58-6e6ee9e3cffso1196535b3a.1 for ; Thu, 28 Mar 2024 21:46:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1711687570; x=1712292370; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=T4DMhRzx46RlnO0Cr4Xo2ac2UzARWIzMLM1MQTYcvtg=; b=g3FslMewlcaqzudh77fDtQpVneKGn7OvEi/FCFEnaOw0XA5os7BwNh0D3AMHz9FN7Y 2nTXKQCWpsgPMs7kNvKMV+lVCFnkSaKPp3Ob9rMPo4nnLiezt1ozPbuclnWHvdM+EdDT vrjQ3PWErmNvSs/1d4mQJax7Arpg/f0cHE52fBRdM4YiV3AS5Ga8SNDppeWODdSkj3/2 qsHDgVYrRW2WJQtLTUO+6fCdd0BCB/cMakSGeJdEI9ts3QsGadbmk7Oh3wDQ7yUzyK8n WaNsrIxYXTl+FonpACNp48N5pM6vcmNIWelCNcmiEosE/WOhsLh5Vk61MhpdGfU0fsNm YfKA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711687570; x=1712292370; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=T4DMhRzx46RlnO0Cr4Xo2ac2UzARWIzMLM1MQTYcvtg=; b=g5FovKESBg5IN1Irdtfos8V84LDRMcJGyDj585aOOXLDOuVPFuTRyn+mDcX96/oW4l bBytq7vgmINwWGyPy8pGKY398dGj4nnUMfpU1orco/IWiNHB9AJnAf3J6ll+XCTMOpkN vIgJnjLJhHpMIkg5oTfrleImvE0MXZPkqufNDGQI031f5TG5jjM/uLp+NbTEDhnD14vV S/GtmqFMARItYulcE23HiPQUoAVYS+4P0qa3lQCvP130qk16vgITxbD5WDrFVUdsGP6c oqcmrRDD1n3CnYmAxNW8kpx2zAS6JCWDGk5QgZtUNvSOet0KUntZIh33E2uveNQYm0Dc Ualg== X-Forwarded-Encrypted: i=1; AJvYcCV19rbr0cOsdolV/jmIMwGKG4ysAJXvw+hBn7iKeQLnv3H/P7/GiZuTgoJSHlt12MyQUZpwd3PqJr4tYQmP/9p4Nx0= X-Gm-Message-State: AOJu0YzmAkdzzPuazyRiJAjTSRMOtSuJ20N+CTfMrc3wuvbO7DFR4rfu RSO+fqJM2nNXeZ8ye6qRVcw9tSwrfONzaymg/nbpB3Wo8ZOEnr1RiuyIXsWvjH8= X-Google-Smtp-Source: AGHT+IHO8nERPTyOdDNYqVk3BGsMD1zsiX2YsaTJD0v0/gtzyZaZ9G+70tI4pNq8Z6R8uDRiI7Pabw== X-Received: by 2002:a05:6a00:4b49:b0:6ea:b606:6548 with SMTP id kr9-20020a056a004b4900b006eab6066548mr1848385pfb.7.1711687570371; Thu, 28 Mar 2024 21:46:10 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id i18-20020aa78b52000000b006ea7e972947sm2217120pfd.130.2024.03.28.21.46.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 21:46:09 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com, samuel.holland@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, tech-j-ext@lists.risc-v.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh+dt@kernel.org, krzysztof.kozlowski+dt@linaro.org, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, Liam.Howlett@oracle.com, vbabka@suse.cz, lstoakes@gmail.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, hankuan.chen@sifive.com, greentime.hu@sifive.com, evan@rivosinc.com, xiao.w.wang@intel.com, charlie@rivosinc.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, dbarboza@ventanamicro.com, sameo@rivosinc.com, shikemeng@huaweicloud.com, willy@infradead.org, vincent.chen@sifive.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, gerg@kernel.org, heiko@sntech.de, bhe@redhat.com, jeeheng.sia@starfivetech.com, cyy@cyyself.name, maskray@google.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bgray@linux.ibm.com, mpe@ellerman.id.au, baruch@tkos.co.il, alx@kernel.org, david@redhat.com, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, shr@devkernel.io, deller@gmx.de, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com Subject: [PATCH v2 07/27] mm: Define VM_SHADOW_STACK for RISC-V Date: Thu, 28 Mar 2024 21:44:39 -0700 Message-Id: <20240329044459.3990638-8-debug@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240329044459.3990638-1-debug@rivosinc.com> References: <20240329044459.3990638-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 92482C0009 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: 94tq6uqd59wm5d3sybxa8wgnr7rxjcb4 X-HE-Tag: 1711687571-609144 X-HE-Meta: U2FsdGVkX19/ZQfSqwXEbDku+ki03x2kqCm49rQlPEk2qTV/E/ax1vMpdsghw+t/balFHZFwX0OIpASeUCt5azZJyNEo5cQG3bUrGUZ6q+hfLzPdfjQ+OTp+6xeJwJJL0u2Ss3nw3Z8QKn4V4Ir8HBoManmQmzZf/WycyaunqD/wN3a7HxNTCuW/GirHRpo8mhwiO2Z8ZwjIipPZKRS8OjhwNzAoPiFElu/L3kyAN/QU6ERokT8wmfoqsULSqjunT2i4DO91GwYPr7QVorQaRR8NwvhkdeKJJMv/y3pnKK6mRnJ/CdMsHK4ZvjuyxHv70Ji0zm7tR+v3vING7wjv1LhdcYxNlvZw2GEMdgl/muxoikCNYRN3o+Ib6JWFR+iWNX0PGPIDPz3SsqHYut9SIemsawusi9VhaeyBcgN6lPgBSa8SBZhCMko+X0+SUbApHkct4vaP/SA89ULvdlrITnbNSQJQOKRYnVSkcAbdxxf3d2h2BDZ9pq4OGc77wvSjp1IpRFV/SPabX46wpXGKoWkL7WrJfcl1Z2sLybEuyL2Y5czEC3jOjIUCozh8iL+/KU2lqIQ5PkElpRiqOhLN9Lwssdj9NiZJaxI+SaMXN+BSWCDMn+rs/vBnkYFYqqGe9MbNxeO/yhSNRgRxyEkSGtqbxtoocWnMG7Zz7CON+JvtDRWROYnMy0c7cHODFd1KvSKtotWkYNNgfw8vFyfKj0Fzoy+LQlXFAzGo1ysFxRhPQoYFmVRF5SSEc+ouGu0Wj9R9xbW00irocQ2/xE/v2zIHyYTlFTPmfr0e1OnNx/sCH2ADyNX9cW0d/lweVjDG5MrFiiwqsNtkSIOyvwvGL1evDFCRfnSiIw82UX/zt8fv6ne64OJiSPryiuODrKMiPHfET1A6HU0I3fN3VBLYggceflzUUQyGvwjnHfq5hjr8XkDIg+ueATt/WtYa2avdF8WR7i5qfpdelwVQn+3 y4Ae5mn4 KLe7iNtlcoHWUFNgamA5pnMCmE2r9BkPDvIjm/v57+1G5EIy1cXvtKfHqi0/Y50KMDmC0FECOLLcGP4UTSebA4uhCLfo3dfynZC57SmaY9pyaS8YBz5uD06vP0pB6EjfuVfauK1ipbBbzsXwbM0KO0EROFXI+W1vSaL9YQjWtJctEEzyKk8BiMR+WOQ+lIA+x2lKhGvGYMVUxRyoqbCHKNLascwabwTNK2MwnRGRviuFo+aqIApoHpf+CXG+3bHqBD36nZ9NbqiHCwnaaeNnAwxY4vm7wyWQRUvYLoMqPHK2kYWYooXoQSeZ5T5sdlZRz0T7dAWLFhSGNnpWCkJFhO86mZdIhKE78DID6oAu8jBmOKQ83HyvjuQoUWI95gocZje0OW2URM7Y//wYD7yHbNsFJ5C+g9TAgjyYgGDUXtHIcfrqIaX0blsLH5VsFm6GLlXUiGIjfqYC4+RLqmMhXX8Cuy8KQ3WQfSapv6Q606Vs7OFKU+rFVAEOvKone+hDN3Vv70TsDe4q1yTbl8fhwjVJuqKJBfhTKxciJTRif3Ohm9xTksYMUktutJimXqY9CIRZFi5I/WDssFTQniDUpEN1M3V7YGemMKlRdQ+ize0NnVrjDSrTRfsBLXg== 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 is defined by x86 as vm flag to mark a shadow stack vma. x86 uses VM_HIGH_ARCH_5 bit but that limits shadow stack vma to 64bit only. arm64 follows same path https://lore.kernel.org/lkml/20231009-arm64-gcs-v6-12-78e55deaa4dd@kernel.org/#r To keep things simple, RISC-V follows the same. Signed-off-by: Deepak Gupta --- include/linux/mm.h | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index f5a97dec5169..225af437ecba 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -352,7 +352,25 @@ extern unsigned int kobjsize(const void *objp); * for more details on the guard size. */ # define VM_SHADOW_STACK VM_HIGH_ARCH_5 -#else +#endif + +#ifdef CONFIG_RISCV_USER_CFI +/* + * On RISC-V pte encodings for shadow stack is R=0, W=1, X=0 and thus RISCV + * choosing to use similar mechanism on vm_flags where VM_WRITE only means + * VM_SHADOW_STACK. RISCV as well doesn't support VM_SHADOW_STACK to be set + * with VM_SHARED. + */ +#define VM_SHADOW_STACK VM_HIGH_ARCH_5 + +static inline bool arch_is_shadow_stack(vm_flags_t vm_flags) +{ + return (vm_flags & VM_SHADOW_STACK); +} + +#endif + +#ifndef VM_SHADOW_STACK # define VM_SHADOW_STACK VM_NONE #endif From patchwork Fri Mar 29 04:44:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13610052 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 2443FCD1283 for ; Fri, 29 Mar 2024 04:46:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AB10D6B009F; Fri, 29 Mar 2024 00:46:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A61246B00A0; Fri, 29 Mar 2024 00:46:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 88CCD6B00A1; Fri, 29 Mar 2024 00:46:16 -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 69C916B009F for ; Fri, 29 Mar 2024 00:46:16 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id F2074121161 for ; Fri, 29 Mar 2024 04:46:15 +0000 (UTC) X-FDA: 81948839910.10.32D8DF0 Received: from mail-oi1-f174.google.com (mail-oi1-f174.google.com [209.85.167.174]) by imf25.hostedemail.com (Postfix) with ESMTP id 380EAA0003 for ; Fri, 29 Mar 2024 04:46:14 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=DnY956JY; dmarc=none; spf=pass (imf25.hostedemail.com: domain of debug@rivosinc.com designates 209.85.167.174 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711687574; a=rsa-sha256; cv=none; b=OcP81TXL4Up4Yw/jTai/1OUl0DLuob6QVrzP5smWC3vd3dHC8hQID2vGfb5nh8OvfOVLza jX772Nwh+67x0RrkyLf99sjZ7zoB7nk+6ys/DS+QAN/fwNhW0VmKbnlabtQY6IK99c0Aqa xGOJ5PifuS11zAYizyiJIsNymragJyo= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=DnY956JY; dmarc=none; spf=pass (imf25.hostedemail.com: domain of debug@rivosinc.com designates 209.85.167.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=1711687574; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=+qNedlIktUfQrTR049/Z3OzWmqNS/siPqaKIdhPjXgQ=; b=EVGjHdwIHXYe8hNrCm3hKpi28Rr0MEHaSMffQ3mWOSYUhxS1UDcfpSR87KwNgFxet3PVXX 6NTOEflDU8PHzmaXYNv+S+X2eU/56RQZrUrxOIVOZPB+xIB9GILgEldryt1zBNNSNLj8Su zEXvDS0T7Ff/VfBT70BD6k0bGbADkCk= Received: by mail-oi1-f174.google.com with SMTP id 5614622812f47-3c397193878so1046992b6e.3 for ; Thu, 28 Mar 2024 21:46:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1711687573; x=1712292373; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=+qNedlIktUfQrTR049/Z3OzWmqNS/siPqaKIdhPjXgQ=; b=DnY956JYOiiEH8NMqCUuMcAf4oQKHqxsjYp8DHxWIDUPXmTnaBpY9oc3TwPygf6Qld cRAc8tktN3E+2eD1vrPxO4fwQ0ED2EO+ZhSCARPRXno+MVm2IT/y4J3r0/x3yeYDQk3K IavQVoC1+dNIPYM5ZwvUOWf39qYVCXoEITWlxvPxKdApjWQIbu9JZp4HUxeW7r2AsH+a Zs3uOO103tOHjJvCX6OgWUuuVNPGSq85zb58VIQot3hVL8SNNNLSf/Eci33DrTmfF/uY KzeYk8WrGbMx84CY6lNVgXyW8a5ZiLeN9imPkcol9FbdfvKHcgWItuPcOE7dPglV85ys IuZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711687573; x=1712292373; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=+qNedlIktUfQrTR049/Z3OzWmqNS/siPqaKIdhPjXgQ=; b=QAZjs6ieofuG7yzeFuCTf4MpY+WmfLAvH8CnPpTu1K3HUtDzJNTV5/JiPBoMhOg5Eq xIlGdP1bfhkEycRtISbliM73I8jPHRhcKW4M2JENSimb2e3xCZgn9K9YUqi63nH1DMG3 4tNYid2gXzGOCTTA0J2OuuoP5eg0q82G1rR+MnRyCMifIhcEdjWqWn7wbrP3hpWusp7y XfBTIa5H8MCk5t2grI3l0s2uM1SOuy281RUX44kqgkXXVULt3CwnIyG2GWExy0Vdb5zp zj2nTdbbQ5hMxJunC5+CMjDfsm4X8Jm4bn91tNfRWh+K4iGCfD9Nnh+2zn6Q7kZG77/w Z3Xw== X-Forwarded-Encrypted: i=1; AJvYcCXEeGuY1LkartvozYGdyNegNhfd/yIYKy4KHpgpiyB7m5WkQB7UwKHW4NKKySrQVM5665ehcD3+F/K2tvRex5af00Y= X-Gm-Message-State: AOJu0Yy1o8BJH3Yx6p8WH+ZKprlGm0jnD8icYss0z4jE4yccMKW8mXFx ojaLjopUpjVubOX8Pb/oti63hRk9IgfVDul7kzQCXA0NcpyDhzV3IYvhdsXHSTI= X-Google-Smtp-Source: AGHT+IHTQWx1tR1j7s3+pvyrI5hMSSRXmYpDonpnfYPB/9nbQQQRNm8eLeO96l4IIa6gD4IvOzseMg== X-Received: by 2002:a05:6808:398c:b0:3c3:d2ca:a4fe with SMTP id gq12-20020a056808398c00b003c3d2caa4femr1622664oib.7.1711687573325; Thu, 28 Mar 2024 21:46:13 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id i18-20020aa78b52000000b006ea7e972947sm2217120pfd.130.2024.03.28.21.46.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 21:46:13 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com, samuel.holland@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, tech-j-ext@lists.risc-v.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh+dt@kernel.org, krzysztof.kozlowski+dt@linaro.org, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, Liam.Howlett@oracle.com, vbabka@suse.cz, lstoakes@gmail.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, hankuan.chen@sifive.com, greentime.hu@sifive.com, evan@rivosinc.com, xiao.w.wang@intel.com, charlie@rivosinc.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, dbarboza@ventanamicro.com, sameo@rivosinc.com, shikemeng@huaweicloud.com, willy@infradead.org, vincent.chen@sifive.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, gerg@kernel.org, heiko@sntech.de, bhe@redhat.com, jeeheng.sia@starfivetech.com, cyy@cyyself.name, maskray@google.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bgray@linux.ibm.com, mpe@ellerman.id.au, baruch@tkos.co.il, alx@kernel.org, david@redhat.com, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, shr@devkernel.io, deller@gmx.de, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com Subject: [PATCH v2 08/27] mm: abstract shadow stack vma behind `arch_is_shadow_stack` Date: Thu, 28 Mar 2024 21:44:40 -0700 Message-Id: <20240329044459.3990638-9-debug@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240329044459.3990638-1-debug@rivosinc.com> References: <20240329044459.3990638-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 380EAA0003 X-Stat-Signature: 8r8383hq4ydeobt5e87ezkc7wc9b1wd7 X-HE-Tag: 1711687574-211905 X-HE-Meta: U2FsdGVkX1/X8JWRVNtlxj4fz6fzMshmX1RLdl8XNBDlHwqOpH742iAjOF02Wkr8tMBENwZ0IwmOnLMbd89RQc0Sn7Tw1kBnO3ivdFo26aWPhPABCQZADPDPh/Sp1lyV9MRLL5J0LjBNMlbLQDZ7uDnzZEMg2MPlRB0F7Mn5itmlJfVuyzOPV32wbIwrsv4fVyPq54KZ5dxQHsLpHK8umFAHSnJ2Yyw2Qi4gOjUP4SuZXEFaJHCf2RpRnHjcjeo13fVrzvsfMZ2lp9sIUzA46Ah0hiJhima1QUQ/cVNaXI7V08Ta+XvlNlV7tEGs+DJv+GXnW4ThCM8zxxOzYQtmZNOXz8eI3dWIhB6ksz90VM3xDfhau9/uYlPjOvi03let8/yM7+7Y17E9l3GfhVLa+UBOV5cZGgDK0DLx7gsy388a36PyL0HlUIIJXMsK7ml2sbBUZD1mBN0DiZ0zzUgAbZnF3IfhPqV8DaaIwAUhKB5a8S7bSv6xh06PdUgWXyw0qrLXbfXswTEedGhpRdxsfRAf8E4BUb3foZ5BBs1bxWdhRdqXd2CybvuHbHOVpkGUto9252NHXpfXOS7TlK0tBycnnbLZ3GFcsVYWCwIg090w2FUPdRiK1yVPqniQY3/q2q14hZdfGnfq0FZs7jFpO2vNZMGU60/1lMFzD0Wa7dpJbHDHDGu6+EuxCbKDLGREKbX4vfaT1uRtcx8XDFoaMtdQMi5BLIb3Z1MJ+DLhVD2fzXYM5aNn9uO9PAHOTVedfe8ZD9foLzckFZQ1uRfQLeo9tTUpCaf3ktnR7gDHAEb/5K19bkXqxZzhxgT8ghXxPP5kkM+GqIk0PsHR8dSc/akZiEUtwnfFgkMvljKsddEr6erc/XDayZXpqVrg1sVNYOqyiYT0bKRzzQleJ7JT265LJ6zEMGGoqKC4ayHNYY2ckgY5QKZfHENfP0fN01byO9+DmFq514nkYGHHf+X aS3dT8RX n0Y7gbW8FwRa2iFvnn0FsrMqQ4Hgoom7RvpT9omlOl6wDtkIZef4yY4E8+V230qJwFE1ODHCPCtPne+93tqjeXp9aeYZZuieVgVHSjoUWSI/bMFtKYPUA1rGGIOiuNEwQ/OAUaVE1euu1i9pVym8OI/mYR/8CRG4bPD2FRmm9fu1MLUNA1j1lvG7LGO+ee4EHhhmNmcMiTN3nHS7GXrRZgFsYTFsXbDMZcbrTwOKFZ8ffIPU65vspmM2G4IcaKm0W6FQM8pU+QhSLhY5RmYwsqtWnHX24odddYabhBbfR5ZdX7GZqTLE14bUl98Vgp68g1KDxyN+KMpHD72KeaySktLD2QnGzcC0oa2IJQm+QvN7PTG1Qy0QPe6AcwHtuIyxQcFnYs4t+HJz2hxzIXUguIUde9BgEI/NSn2HVcJWU/MszsZKbBhnY+pObITwYrPlTcBLHvfmPKmvPj9kNVFKXrPy80awWr2ll/NzZYTP51CsxDdQGe//DvdCKf6on/5GaHIFPnRPuVUELEedB7Akn57aP1VmVxU8X2qH3SuH4iUhWqnRK2Q6Wj8XcIY22Ciup2a56 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: x86 has used VM_SHADOW_STACK (alias to VM_HIGH_ARCH_5) to encode shadow stack VMA. VM_SHADOW_STACK is thus not possible on 32bit. Some arches may need a way to encode shadow stack on 32bit and 64bit both and they may encode this information differently in VMAs. This patch changes checks of VM_SHADOW_STACK flag in generic code to call to a function `arch_is_shadow_stack` which will return true if arch supports shadow stack and vma is shadow stack else stub returns false. There was a suggestion to name it as `vma_is_shadow_stack`. I preferred to keep `arch` prefix in there because it's each arch specific. Signed-off-by: Deepak Gupta --- include/linux/mm.h | 12 +++++++++++- mm/gup.c | 5 +++-- mm/internal.h | 2 +- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 225af437ecba..9e6a4fbfccac 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -352,6 +352,10 @@ extern unsigned int kobjsize(const void *objp); * for more details on the guard size. */ # define VM_SHADOW_STACK VM_HIGH_ARCH_5 +static inline bool arch_is_shadow_stack(vm_flags_t vm_flags) +{ + return (vm_flags & VM_SHADOW_STACK); +} #endif #ifdef CONFIG_RISCV_USER_CFI @@ -372,6 +376,12 @@ static inline bool arch_is_shadow_stack(vm_flags_t vm_flags) #ifndef VM_SHADOW_STACK # define VM_SHADOW_STACK VM_NONE + +static inline bool arch_is_shadow_stack(vm_flags_t vm_flags) +{ + return false; +} + #endif #if defined(CONFIG_X86) @@ -3482,7 +3492,7 @@ static inline unsigned long stack_guard_start_gap(struct vm_area_struct *vma) return stack_guard_gap; /* See reasoning around the VM_SHADOW_STACK definition */ - if (vma->vm_flags & VM_SHADOW_STACK) + if (vma->vm_flags && arch_is_shadow_stack(vma->vm_flags)) return PAGE_SIZE; return 0; diff --git a/mm/gup.c b/mm/gup.c index df83182ec72d..a96043429b31 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -1053,7 +1053,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) || arch_is_shadow_stack(vm_flags)) { if (!(gup_flags & FOLL_FORCE)) return -EFAULT; /* hugetlb does not support FOLL_FORCE|FOLL_WRITE. */ @@ -1071,7 +1071,8 @@ static int check_vma_flags(struct vm_area_struct *vma, unsigned long gup_flags) if (!is_cow_mapping(vm_flags)) return -EFAULT; } - } else if (!(vm_flags & VM_READ)) { + } else if (!(vm_flags & VM_READ) && !arch_is_shadow_stack(vm_flags)) { + /* reads allowed if its shadow stack vma */ if (!(gup_flags & FOLL_FORCE)) return -EFAULT; /* diff --git a/mm/internal.h b/mm/internal.h index f309a010d50f..005761d754f6 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -572,7 +572,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) || arch_is_shadow_stack(flags); } /* From patchwork Fri Mar 29 04:44:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13610053 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 6D771C6FD1F for ; Fri, 29 Mar 2024 04:46:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id ED1586B00A1; Fri, 29 Mar 2024 00:46:19 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E7F926B00A2; Fri, 29 Mar 2024 00:46:19 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C85906B00A3; Fri, 29 Mar 2024 00:46:19 -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 A72616B00A1 for ; Fri, 29 Mar 2024 00:46:19 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 38811A1119 for ; Fri, 29 Mar 2024 04:46:19 +0000 (UTC) X-FDA: 81948840078.02.02767C9 Received: from mail-pf1-f177.google.com (mail-pf1-f177.google.com [209.85.210.177]) by imf28.hostedemail.com (Postfix) with ESMTP id 63C6DC0007 for ; Fri, 29 Mar 2024 04:46:17 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=VV+S4kcz; spf=pass (imf28.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=1711687577; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=pODlhOn2Qlpzyvoy16i8OpTFZF278Z1sqN5yeQeAUQM=; b=tSMo+dAsF5K5L5rHWJkpM7C8+dWWo/CMPE4dXnMUJ/gEHbp3kjqtBzpZjffHalm1k8BMMN nLRkFVuY3hJd3y0h0IIlvGDva3HTdJzGw5OOFZTPJd9d/a1asEdaYLV898isiR8srWIpYZ jMxC2abue5Iozi7YSzRcrtDiNLPFYN8= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711687577; a=rsa-sha256; cv=none; b=2xUsmU1LPUhT9gg6X/ruC0ABA6x6Ymus+Y/CeF7IhBIHW8krcsgN9gWGjqDEGXeX2ZXGyx WRn5cCxIth5YkIkm0GTFUdmx8cT9Tp6H39AfS6LWRlmMYBfpOsKP1IjZOTvLpYH8VwBH2a lNXf56mM5z/b7YrRE/ppivTPV4wka2o= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=VV+S4kcz; spf=pass (imf28.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-6e782e955adso1551909b3a.3 for ; Thu, 28 Mar 2024 21:46:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1711687576; x=1712292376; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=pODlhOn2Qlpzyvoy16i8OpTFZF278Z1sqN5yeQeAUQM=; b=VV+S4kczzSNNZ7aiX2lG714UtDtSkNa+B6GVFstVZ7W7OLHqNlspuPjB4wfaA3LBOv CtVkYcg0nHrMiOXcfxIFPNi2PWc/HZ9WlM0eH3MBd8IznVDVgaSaDM1jERnD3cvJXQSU 1Rpid20Ux73UZaTaWJtNNW6YD33wGXnMBiYIWMoQpGcQM3IVztbvsbUai3ciSCSsFjJa 9XSCc/8/+8A2QIfgakViLw9SqvJnKbAgSIRy0zFDITcBTAjVI53cQi2FR2K45TsKMAYF wuyfyKrWk9D1YYmq8viZXdcp02eloKaKfHFJ8zLWqX67eOIpH/MyxRp+rj8ep1Ts9etm ggsw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711687576; x=1712292376; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=pODlhOn2Qlpzyvoy16i8OpTFZF278Z1sqN5yeQeAUQM=; b=C9qJ2L/Ac03YIXdix4dWuKgWKAyMJkGuiU4qUV36kLgZ0AO69ANyBnX40LiHrCHn7I lh9kHLm0Qjrg9NMN+fCdZR7TMJ2H7vHx/Q5hEzWifXz7v60zt+iRYmolZlKLa1qtXSFd ga1CzSkHdFTmuYIDqOzLXeqGj/XaBhkoqJ7sY01GknEibmaqwtLur+YvdKrF0A/jIvA7 GXuQ146CLH7FwsbrPdJxiknfbs3SwKl/BOTMFSLgX1QBXl/AoqFiiwUiMMN+foRJ1q2w 8ud9X0p8l5V6SHtz4AlBPGC0FadZPESz74w/K2dQfpX7C8N1Qa6DSTTD8NiTWCT4/Gk+ LrwQ== X-Forwarded-Encrypted: i=1; AJvYcCUkowVftkh9uJ4GTpKcBeFhMNUaBiQVVk/h5s/Tz68uoet0Q+oTtRGGYHZ7rAiCizz8jajqRCvS5Mf+eDsqcH8rz6g= X-Gm-Message-State: AOJu0YwqCiNDCiPeroalUkPK5ew0TUFxHP5KcVTNoN6/e8UuJdeswQwV 4cJiGtaV3LdX65PrxYbf62iKhkC5EHlFUzqLjbth88/pXLpqg0Dls3u5PPNXLNk= X-Google-Smtp-Source: AGHT+IEEpy9pSrtr+BvW+AqTbkR2NWQ8ISKhZnwm1fAksXWCTeDRwW/aV3jv7PvAv025C+VRS8TIYg== X-Received: by 2002:a05:6a00:3ccb:b0:6ea:74d4:a01c with SMTP id ln11-20020a056a003ccb00b006ea74d4a01cmr1489683pfb.14.1711687576218; Thu, 28 Mar 2024 21:46:16 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id i18-20020aa78b52000000b006ea7e972947sm2217120pfd.130.2024.03.28.21.46.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 21:46:15 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com, samuel.holland@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, tech-j-ext@lists.risc-v.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh+dt@kernel.org, krzysztof.kozlowski+dt@linaro.org, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, Liam.Howlett@oracle.com, vbabka@suse.cz, lstoakes@gmail.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, hankuan.chen@sifive.com, greentime.hu@sifive.com, evan@rivosinc.com, xiao.w.wang@intel.com, charlie@rivosinc.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, dbarboza@ventanamicro.com, sameo@rivosinc.com, shikemeng@huaweicloud.com, willy@infradead.org, vincent.chen@sifive.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, gerg@kernel.org, heiko@sntech.de, bhe@redhat.com, jeeheng.sia@starfivetech.com, cyy@cyyself.name, maskray@google.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bgray@linux.ibm.com, mpe@ellerman.id.au, baruch@tkos.co.il, alx@kernel.org, david@redhat.com, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, shr@devkernel.io, deller@gmx.de, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com Subject: [PATCH v2 09/27] riscv/mm : ensure PROT_WRITE leads to VM_READ | VM_WRITE Date: Thu, 28 Mar 2024 21:44:41 -0700 Message-Id: <20240329044459.3990638-10-debug@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240329044459.3990638-1-debug@rivosinc.com> References: <20240329044459.3990638-1-debug@rivosinc.com> MIME-Version: 1.0 X-Stat-Signature: 63e1iajgg8ewwju7fxdx1ct8cfxf76jb X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 63C6DC0007 X-Rspam-User: X-HE-Tag: 1711687577-457027 X-HE-Meta: U2FsdGVkX1/oJjx46Zbhm1aieQfxv1jiQt86+1W0vyD6FGGSAAglLxYcvUS2eaSzNVfSB29/75gcUtI0Xu5X/oTi/GTRO6d06uy/y//jEh1aBxpXoiMXM/7Ew3SMDmnvxvknYh/1l2MrT20DQaTGxx+2XTotFv09c+ybSEnfjxGdeTv4qq2/rIhYMCUhtWhHn5xmK6n4tuwX66PkievG8zrWb3LwmwoERVDzig0NpibNKxCdZI9+AY3teH3NAsv8J7QavKyJPPxrmxXC5OGHx/JYOg+f6loNjT0re0GwL5BQc0P6XMzS4kPSJyIXZVyDg1bUc38goADA+9W9qBUjlUQ4UcMqMgfRolw/iQYaqX3V4WldUmvQ8bL7Vdfma9db2TIcFkQw4v8VM/aRw3NTikgVW+zolAmMqKobI3aPPLDgfTD4fxQu+UkIH8tAZEGLD811iSWU4HH2JPW5UEyP4f/tvRbV1LNEw5SShWYdOfSwE/cGzpaaLGXQCDOBHpOhB/QcPD7LVU+vd/wJDPlPQgHEOQLRAoU4m0wbyeXD3nxrLPTQHYP6ohTYU7sfBRYyeJ2ojCtqAnd70mWvFbRP+rE6GymzvLNqadbHJk4DhcLXA+CSuTgl3JBtUe1bWAzIueWJ3+SGclRPpb7xi4IS7Cj9KaXovdvHTTQnEzKcDuwTFS3U3FT52B2k6xYy9THGTtOiOfmiVB0GTgWxFhsm2NYKPNiorJk6aiJKlS15tQTqMXtIw967FFYw1mzRIADvHeVM7YgYWPE+SwSYOh80UvNxjhAka333kScNZ//plKMKF70/opPzI1GxoRhRd7ftDwv2AaqFOD4eslGhZlapCnP9rrYIh1PpD/kdY1atzh81VKtO8/g1CGJrL3kKAqtSUCVTCAJQobLPGz+nMvWm+/6mjv1rYAbYbPfpAEEPfGn1mNj8I+MOufk9XKTFlMsovQNVGleud+aUPJOWESh TBRuyE+h pPppn9bngl4LUl/lcM4TC0uO3sb6WRs6lyS7fXN0cUN2l1m6k1xU4GL8UAu3STNGYnOnRAJ216aRQJHTpFOYHL//lMY9368XArfnevMR3CMf33Dqm6TEH2mlr+C3PCN/7ouvCvK27Q7AK7Ik9C4Ks68V1SOE+UnnpQc1IWwu0wmGuxv++SpDTRP6gokf0d4pnWkic6WfXVTjFX2sh1HRlT4TKTAc5KZIQ8lyH5PfLdQ9PAkxB6GRiA5WzcJbE4WxXyVa8l0N2kbNX5IYZeYMIj3Wywdzbo45RxNnIna5m4NSitNdjSg7g6e7tQT0H3THJMmsW+pmplb4N8AvvIoK8TYXmDY1nZ7ouYk1kq1i03DAevXaVeT58lW8+Cyc6JFc3JCWutf/6l+AJwTDyQNZFh3SUg77Zq/hCEBUYJGjeHdNmdpMd9X0ia8Ybvq2SheQEemhNvbHCbvC0zMuooTtvF62kHoMF5RbWUCvnO+1BAZgk7wAVozH2YUpvUT6XR1M/wMJ9nI1KpYXbrbDmQ32RNWo6W79cHjoZ0HkxWkD1nx8LPuV4H3H1UuQmZESgZOnu+OQQ 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 | 11 +++++++++++ arch/riscv/mm/init.c | 2 +- mm/mmap.c | 1 + 5 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 arch/riscv/include/asm/mman.h 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 6066822e7396..4d5983bc6766 100644 --- a/arch/riscv/include/asm/pgtable.h +++ b/arch/riscv/include/asm/pgtable.h @@ -184,6 +184,7 @@ extern struct pt_alloc_ops pt_ops __initdata; #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 f1c1416a9f1e..846c36b1b3d5 100644 --- a/arch/riscv/kernel/sys_riscv.c +++ b/arch/riscv/kernel/sys_riscv.c @@ -8,6 +8,8 @@ #include #include #include +#include +#include static long riscv_sys_mmap(unsigned long addr, unsigned long len, unsigned long prot, unsigned long flags, @@ -17,6 +19,15 @@ static long riscv_sys_mmap(unsigned long addr, unsigned long len, if (unlikely(offset & (~PAGE_MASK >> page_shift_offset))) return -EINVAL; + /* + * If only 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 fa34cf55037b..98e5ece4052a 100644 --- a/arch/riscv/mm/init.c +++ b/arch/riscv/mm/init.c @@ -299,7 +299,7 @@ pgd_t early_pg_dir[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE); static const pgprot_t protection_map[16] = { [VM_NONE] = PAGE_NONE, [VM_READ] = PAGE_READ, - [VM_WRITE] = PAGE_COPY, + [VM_WRITE] = PAGE_SHADOWSTACK, [VM_WRITE | VM_READ] = PAGE_COPY, [VM_EXEC] = PAGE_EXEC, [VM_EXEC | VM_READ] = PAGE_READ_EXEC, diff --git a/mm/mmap.c b/mm/mmap.c index d89770eaab6b..57a974f49b00 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -47,6 +47,7 @@ #include #include #include +#include #include #include From patchwork Fri Mar 29 04:44:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13610054 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 2FA1DCD1283 for ; Fri, 29 Mar 2024 04:46:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6BBD06B00A3; Fri, 29 Mar 2024 00:46:22 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 642D96B00A4; Fri, 29 Mar 2024 00:46:22 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 41F6A6B00A5; Fri, 29 Mar 2024 00:46:22 -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 1F7016B00A3 for ; Fri, 29 Mar 2024 00:46:22 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id D2EC8A127A for ; Fri, 29 Mar 2024 04:46:21 +0000 (UTC) X-FDA: 81948840162.21.27A6528 Received: from mail-oi1-f180.google.com (mail-oi1-f180.google.com [209.85.167.180]) by imf06.hostedemail.com (Postfix) with ESMTP id 2CF33180009 for ; Fri, 29 Mar 2024 04:46:19 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=F7FBNfbj; spf=pass (imf06.hostedemail.com: domain of debug@rivosinc.com designates 209.85.167.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=1711687580; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=w1ajd+NFF25bzcoUpj6TOVq3gYUXEAPUF2iej3ctG8E=; b=7DezdBbNg0n1cCx5tBHxyBXVbrCzsnNNZNzb3lZLOlmvYne+5x6CfqVEmBKDVOFTgee/ZY 61Wu6F/zCdcfbY4DCNFd/iMxvjg8Wn4Evx2fab4qBcIulAGkIREy5f0svsr/np1cRexfaO 6KEATqPZKAX+ZOoy8sYahRtam4wOgcs= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711687580; a=rsa-sha256; cv=none; b=Gb2K6CmUhg4fS5tM2J6nqhT1I+NoRDZXi8iLkplMiU2MsRLseIrWMqTlbrBs7c+YZWflDv dzLmOy09Q7ZthOIfXu58tTp6zLRdbyVn4eumn1hdJvqcw8ko/GdxOclPyQDpP/itRe/uZW wr2FZA6v3jf1PV3NP4b4Be3PdLPFgEA= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=F7FBNfbj; spf=pass (imf06.hostedemail.com: domain of debug@rivosinc.com designates 209.85.167.180 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-oi1-f180.google.com with SMTP id 5614622812f47-3c3d7e7402dso1225031b6e.1 for ; Thu, 28 Mar 2024 21:46:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1711687579; x=1712292379; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=w1ajd+NFF25bzcoUpj6TOVq3gYUXEAPUF2iej3ctG8E=; b=F7FBNfbjd0qjfPZE5c64GDyQHu4SmORqQYXvKI7jKW4RjX4jCYSwzZg9YXRv2Wge9f vrDx3BPBAGWDK+1db+xLBobfXXAIlLEbQL2066jfqzk33OdSNmAVys5/+N3LpXKz8OVI OMlL3gdiFunElsYjR2Mp6KKU9HUB3rI+LXr9LcjFmuebRltI89Gri3lqvt1rk9f+/zL8 C6e0lHso+cf7okoICFJBJI94UNle5RqgQrLNGMn71EzcmtgmhGO60Fzj7NzyvKxSNeng 3zUSqv6cAFlhQ5hp7uxSU7QL5GjsWkstDkmz/LqD1w02i5PpjzXshivRjyc/bI2fWBSr qtTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711687579; x=1712292379; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=w1ajd+NFF25bzcoUpj6TOVq3gYUXEAPUF2iej3ctG8E=; b=AePUjvMffVchVlIrWK1NI5pOJPnNWWXxEyeh2Y9cvBcnC+7mnPgwCfw3VbBgmjdzfo UcdTR9XcD0Jkwux7Keuwyztd2w1yiF1q1Q0RAhp/pXeIv7puYH2FXIqMV+xYM9Kt1HYE ILD7lMuSXUZTpT/ktxAzvDFuT+alMyVEKVoPiRFSTEqUWP+4KLuUKfV7tdhs1EMoOr6v P4Fg5rHCZXfOIcHPTAtNFy+1r6YwUKG6BjVEXFAqliCtZ7cyOR0OxSlqLCl6aj++cV35 DFDT4qTXz4zIm5D6KQhLuPW3180DDXbcqBQfcXwfe6oVl9YqmjobpUhLNIKoYZmvo0T2 VTSg== X-Forwarded-Encrypted: i=1; AJvYcCWdLLtO/g31Fb1/rB2J6dbvhjzdPa18v7tFOBkxBsKM16eDCOMLGnr4h2qNwKOQLRKTEAvQkZplFqakIKh/5cuDI/Y= X-Gm-Message-State: AOJu0Yw58BcYg4/yZBFzRFrz94fEFAcTFVHnlbWfr9SW1jD1iLJ297Im D0443QbIrLvAACkKF3NkceoYKRFlpzarxh84QTCpsPWFn6dug+uyxPANdk+J0hI= X-Google-Smtp-Source: AGHT+IH3K7i4PAKAFUMOdgNrbbbhpm+48RguclKkN/rSLzo7SW0Yi7+dWUtv4WvqoPHa2AHkl7gVfA== X-Received: by 2002:a05:6808:2126:b0:3c3:ed9f:7e65 with SMTP id r38-20020a056808212600b003c3ed9f7e65mr1522250oiw.21.1711687579203; Thu, 28 Mar 2024 21:46:19 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id i18-20020aa78b52000000b006ea7e972947sm2217120pfd.130.2024.03.28.21.46.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 21:46:18 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com, samuel.holland@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, tech-j-ext@lists.risc-v.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh+dt@kernel.org, krzysztof.kozlowski+dt@linaro.org, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, Liam.Howlett@oracle.com, vbabka@suse.cz, lstoakes@gmail.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, hankuan.chen@sifive.com, greentime.hu@sifive.com, evan@rivosinc.com, xiao.w.wang@intel.com, charlie@rivosinc.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, dbarboza@ventanamicro.com, sameo@rivosinc.com, shikemeng@huaweicloud.com, willy@infradead.org, vincent.chen@sifive.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, gerg@kernel.org, heiko@sntech.de, bhe@redhat.com, jeeheng.sia@starfivetech.com, cyy@cyyself.name, maskray@google.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bgray@linux.ibm.com, mpe@ellerman.id.au, baruch@tkos.co.il, alx@kernel.org, david@redhat.com, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, shr@devkernel.io, deller@gmx.de, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com Subject: [PATCH v2 10/27] riscv mm: manufacture shadow stack pte Date: Thu, 28 Mar 2024 21:44:42 -0700 Message-Id: <20240329044459.3990638-11-debug@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240329044459.3990638-1-debug@rivosinc.com> References: <20240329044459.3990638-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 2CF33180009 X-Rspam-User: X-Stat-Signature: 7fryd8s7hkjp73c3xehredzw7xyogc4y X-Rspamd-Server: rspam03 X-HE-Tag: 1711687579-791036 X-HE-Meta: U2FsdGVkX1+m0nC7ulrLG4YY8FZ6xR85+B8MPwbUFqq9SgnZsdJ17RvlUGO6I3EaKpJ5CS6TRndfHKUPIBdq2pNuIcoK3abr5cNhEnHOhzuMujLWSDB6DeEOkGXD0M77lb6d3n0y/6jVJUkDx+Ha/zOOiTxNyXTwewxwLgetN/DEWQWI3LRIDBb82jE63D2sckp4qYOfNhsqf6nqr/oJ3d7e1/SmkAdfxk5NIjDua72pVbkCkbi8/lAODVQetJ9W9WNB1E/NS2UykO4rSAKvZYPHLnguh9EtZvxAUt+b7iiKbrMvXCzKTCc0MueQOFXFnG7UMDYN2h4ps81e1ZTVEP49bTLUFH2GCfAHcfTEbiCbyVuaXO3dCPZgW2Hwhy+XcWS+3CyeMBMQ0GzKf8nVwPdhlUQH/yFsGeKpIBWQ9MvlfSzbTjyAs0hNHnMs7ZZwpe1vVaZAaVTs2XefrTi3Im5fyGP5U5NPdn5pX+42lawy+/SmkDwaS6hhdX3uOuG37I0Gw5GmNBqYPFguvP7UAYxYYhxgKTvcIKFs3OoBWQvPbR6HCdwaEcE1XuJbtcSU477+7nPH2qT7jATd9E3Qvb+sWMGQKsOpGkwWYPhS3FUEcNFp3CF1Ho9J10hoqpaVnKCb6CqcYD3qdOSLd65ahthl99AShado49z4NM6Im3mTJHEwl9mWEgfX9UQZHY6TO8Jg4K/gbENjXQmLrntDOszudBaY+7ELlg9p5d37yh+wr7+uHwhTDFaDu0vMLt3ecOVHcAyQwpAaPeTUWLJzEnJxEw544YO+/BtfjnUXea93n7Tdw/avQdkZ6lEf+OyOn4xeRyfpGauSJ6X3udW0G42yaZDk3WegY3Ksc4AXUfjjoy6Dho3DdwIkW54gugDWgKE7XCeoloifuxyTto6Shq5PT5A1PkUL+KRMTo44A7QBvRlUwvfxGPMx6C0HkVuD+X5YqFIsEq1mD0LD59J cJJ0HdTL CUCh0BicPTl7VUzKzUbuKvReDoO/6Rt3vwzu2ReMDLtVbQHHSEyuTzri2HTraTa3xIIi7s3lGpWwgTXNLRZjXAVkR7MLCGfVmp+nMLPCkILFeoL+DhwxGc4DoNNdpMpZtIsAXfI8RsRnr8r0e82gZNVa2+XCPbKVKEfJaox7cYCZHXHbDH+IasAo2VDAtoT7x5nj7nxy1JoCowP86eLB3Vr68N9uAf1EHdVu5cfPohtWihCc= 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 --- arch/riscv/include/asm/pgtable.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h index 4d5983bc6766..6362407f1e83 100644 --- a/arch/riscv/include/asm/pgtable.h +++ b/arch/riscv/include/asm/pgtable.h @@ -408,6 +408,12 @@ 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) +{ + /* shadow stack on risc-v is XWR = 010. Clear everything and only set _PAGE_WRITE */ + 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) @@ -693,6 +699,12 @@ 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) +{ + /* shadow stack on risc-v is XWR = 010. Clear everything and only set _PAGE_WRITE */ + return __pmd((pmd_val(pte) & ~(_PAGE_LEAF)) | _PAGE_WRITE); +} + static inline pmd_t pmd_wrprotect(pmd_t pmd) { return pte_pmd(pte_wrprotect(pmd_pte(pmd))); From patchwork Fri Mar 29 04:44:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13610055 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 03F6FCD1288 for ; Fri, 29 Mar 2024 04:46:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8B9E76B00A5; Fri, 29 Mar 2024 00:46:25 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 819F76B00A7; Fri, 29 Mar 2024 00:46:25 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 643666B00A6; Fri, 29 Mar 2024 00:46:25 -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 4492D6B0083 for ; Fri, 29 Mar 2024 00:46:25 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id CCF01C1131 for ; Fri, 29 Mar 2024 04:46:24 +0000 (UTC) X-FDA: 81948840288.25.38695EF Received: from mail-oi1-f170.google.com (mail-oi1-f170.google.com [209.85.167.170]) by imf28.hostedemail.com (Postfix) with ESMTP id 08963C0009 for ; Fri, 29 Mar 2024 04:46:22 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=im6vyX6v; dmarc=none; spf=pass (imf28.hostedemail.com: domain of debug@rivosinc.com designates 209.85.167.170 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1711687583; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=fEO9ZqLZNdtWaEHlnNa/VDfM4+2OUD4uT/m4nT0q2gI=; b=llD0779hNEPd5cvgO885GNst2q7ou19mRloYPf47z8qCmcL99VujTd9U2z7CehQicd5F/Z y0QOOMmkM8ClIJndzcMG3fBB+6NM+XWxNIcp23XREY0ASfZncUz99WyF5qbeqbwhT7pV7P T/yhr3UmtBzR5F11UjLgt3/2iXQSWxU= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=im6vyX6v; dmarc=none; spf=pass (imf28.hostedemail.com: domain of debug@rivosinc.com designates 209.85.167.170 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711687583; a=rsa-sha256; cv=none; b=QFaculWVoYIObr0iZ/ApY2+U9xtA6uotj9GgIJbJA8JsRObGOLpI7JF5R7wlgTvppF3/un HbYlEM3rRhz/qqkuB3gl/c7QEq8qbqd7zpikxzASJGaxhKUfPriZ1sQcekQyoFh43cbriz z3sI6V3pZ8jFe5xB/wgCGb4QCJn8UfE= Received: by mail-oi1-f170.google.com with SMTP id 5614622812f47-3c3bc867eaaso1151848b6e.1 for ; Thu, 28 Mar 2024 21:46:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1711687582; x=1712292382; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=fEO9ZqLZNdtWaEHlnNa/VDfM4+2OUD4uT/m4nT0q2gI=; b=im6vyX6va8QDo51PH2Cn8tx24sh8T7RR41ykVkW3SxUvuhBHmupf9LTc0z5UQmGDcG 9SoEy2LOAc5UL6LUMTkFN6FHTImi2B6epMasas9svPZR6VBQcBuf1SmubhnkPARWNw9L OlCMMKlWywVbbJnTKRqgYCAkDDppeuV00LgAlIOTpyjhrhYnrAoycRsjY83pUs2fD4ut YbLtRzjtWD7bTSEmTbneHf8uajoaE9rjEjyT+YGK+2Q89cs1u6sQoF//FTjYIwC+0fkN sO+9PjOLIOCBOWqF2ZDwhYKiy+v0ZMji5byhQRXtgd7UcAzOCy0s73hKWlYsooAPJNuj zKcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711687582; x=1712292382; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=fEO9ZqLZNdtWaEHlnNa/VDfM4+2OUD4uT/m4nT0q2gI=; b=OgQTXT21eGGQkkw3TVJFu3ENeioQLURhmn5YeLQJRJsDdF9ORY6yN1bErJYnsUzX4O BB3ZBZB9dd6Ku1QJdTpJ4ABv6CC6YMKbNthi92Gjgs6ssdkNRoe0lMFKTeV1WWF/xEDC //CpLY6SlP+lpLwnWkJYJ6ARDlzdis9+McO40Pkd4kkFKRg7xAQH+J8j6PNAbABCUMoG XYwn8hZcokhx4/+3fTdMs26WceCSOaqUbAsHCpgsgSSKKjKlgvUpKw/zglAbLJcSmAgw NPgt6txjq3OnHw4F2+RknGqEfFk+nq0LuEnxszuUi1wqh5l5xxa+Hq15L4q9Fp+y2WRs nQZQ== X-Forwarded-Encrypted: i=1; AJvYcCWmkwIDGk7u4pYWq6WYcBGGXb5R11936XbhC6h4eFs+GIdTeYfwwDXjKN7+SiGZkXQHvHVXnaQ3iVkeLWWY4McAtN8= X-Gm-Message-State: AOJu0Yz52p0db9vOLBpF+NcaHLi9VDkQYgNEHINOyD+jJlVXSyJ/q0T6 C7KdKuCOLG42AD0S2FmQUUJV1d4kIOjF5/T0NrwTqF4cguZ1smd0bDiZ1L9ib74= X-Google-Smtp-Source: AGHT+IHkNGwGYsa0lxSVV6I7rhUYi4wQsB/IhRzR2mYXyandS5eg+YGt4M7UftNEIAsc9qZb2HdgcA== X-Received: by 2002:a05:6808:3385:b0:3c3:ebba:2cdc with SMTP id ce5-20020a056808338500b003c3ebba2cdcmr1352197oib.17.1711687582182; Thu, 28 Mar 2024 21:46:22 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id i18-20020aa78b52000000b006ea7e972947sm2217120pfd.130.2024.03.28.21.46.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 21:46:21 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com, samuel.holland@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, tech-j-ext@lists.risc-v.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh+dt@kernel.org, krzysztof.kozlowski+dt@linaro.org, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, Liam.Howlett@oracle.com, vbabka@suse.cz, lstoakes@gmail.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, hankuan.chen@sifive.com, greentime.hu@sifive.com, evan@rivosinc.com, xiao.w.wang@intel.com, charlie@rivosinc.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, dbarboza@ventanamicro.com, sameo@rivosinc.com, shikemeng@huaweicloud.com, willy@infradead.org, vincent.chen@sifive.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, gerg@kernel.org, heiko@sntech.de, bhe@redhat.com, jeeheng.sia@starfivetech.com, cyy@cyyself.name, maskray@google.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bgray@linux.ibm.com, mpe@ellerman.id.au, baruch@tkos.co.il, alx@kernel.org, david@redhat.com, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, shr@devkernel.io, deller@gmx.de, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com Subject: [PATCH v2 11/27] riscv mmu: teach pte_mkwrite to manufacture shadow stack PTEs Date: Thu, 28 Mar 2024 21:44:43 -0700 Message-Id: <20240329044459.3990638-12-debug@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240329044459.3990638-1-debug@rivosinc.com> References: <20240329044459.3990638-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 08963C0009 X-Stat-Signature: 6hmu3nink98toubn6goj4jq14z8z61zz X-HE-Tag: 1711687582-515121 X-HE-Meta: U2FsdGVkX1/ddYYB+Ifbq4SAPxq9sgCXe4dTzf6p8oGpcjdECLDz6AQiI1tOpD6u6EKs9NzgekYgZy0Xd7iVjPgMe7OI6eXb6iwSIiYYS3DY3NAgvaYKKGcL4NeLIJc8oJZACPmUvoOmFDjN43/s6iIB5bBdgo4pQOW48CLCltrcb7BQLXcKqTf+7ZsoGjsIpyRvJY3o0PQ6gjcnA5qzCDmfolGV7TQ03JJOGkKCZ/0gTFOijPYevfgOOF/Z74AR0ksqxKNDZnXqZikCFi9PkbAa9uIkM6HMXjpQoWpOhr1O44x0cNlR2lNdtP5PSUWLYE3BB2X3/nboVjcEG8VEDYnWF99lp0q6v+P5rjwPHjX3eC2wCutHHLM7dJetHnCu14SAAWy8Ol9t6KWiiSooGkQPZY6xJblTYXtx707oRd1Nb/GCMjro5myq2bbuZXwzOelnkvXOkxz+sYtmKHo20zSQthiyI9GGvt3WtP5j5r95Pt7s75gbCUQ4wVlrAjXWISSSVwVzsW4uxboa6an9Xi2RpUaAZASnEfebwH9foruIgdhilEkt3qAdMkO3372JNNu8H9CkrdAIo6LC6BAb2HRGRwYn3lT5gZq1+8CYRWwfHbSx8FTy2j0hL9x0w1rwC1Fl3p5URSwo/bpKeef1Lam1/HGbGBhQVlQYU3ne4VztBhFF4d68j1ms+/Z9A4tX0Bu1Z4bSG+pq+ZpNTa/xX7XFLVUtvVWWDJfpReERWpI8mKr7S/Vzj4WoNHwEbqqCPVhe3u8N4K9PsomnRPpKLrh7CIUvA3RWp/hQRxZLHkjO9ovajqzTjmb8fp6vdm1D0j2Q5D4xnflgeL/3Nivxxm4RxdRz4RsAy/mn4q3FmAuuAb0faV43qveujxmvbpMr8ay+ktxj7EGCtGjaZNQnBhKcNoSdoLY3O9KzcGYdmdQ7Ia3kMnDqMt05moyzEiBBuP0raKYTYKRYXbItyxM CIJicQIT QS6uBpz8eowIPkaDil/fw5OJUuEIWl7rCEeZwZFSsYbRIA8TSG4ohjq/psMVLJ6hYr4HwsN31y7WJwTEJN2TAL/YM+bz9dfVV7//1Lz1Pi6tWPXnqcjDoJuoAWL68dbANN+kmgeUn3ApcH02bOQGR1G6GOgRSv/8zfcgS/eyAcfXbXbEw7sChKDBT+Kp3GoKMTarTONpTndmBzfWf9rAg1zTume0MdSVV7pg7MrufZPwfF21RmrFQ/hidKErG/H3A1lU4zD+3mPup5RhNVxjU64OOllDtJZoQzOO/+IrayidisdihtZc3KQGx5pQO00NJ9pb4tVmyb7scpOpVr8RgJCwYq1z2Itxt5z4pS91Rgh1fCIBI8QewkqN8PPWqqz2bGFpk8qnuVD2ptFN7HBd6WhCDX1x91qAPCzSq11CwZzqmL027lBsQS/XIQNhTevQgRfPMChK9HCy2QKAAFUk0F+TDF8Xmbbu2WudhURwV8CHlCxx+GsNguHQxba9M1pMJz2iN+k0tTIq5/VCK51Ux1au+gqZakiTAEunFniRJi/ciBmuHyDsSzSTpd3w67EeDdTUe 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. On riscv, presence of only VM_WRITE in vma->vm_flags means it's a shadow stack. Signed-off-by: Deepak Gupta rebase with a30f0ca0fa31cdb2ac3d24b7b5be9e3ae75f4175 Implementation of pte_mkwrite and pmd_mkwrite on riscv Signed-off-by: Deepak Gupta --- arch/riscv/include/asm/pgtable.h | 7 +++++++ arch/riscv/mm/pgtable.c | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h index 6362407f1e83..9b837239d3e8 100644 --- a/arch/riscv/include/asm/pgtable.h +++ b/arch/riscv/include/asm/pgtable.h @@ -403,6 +403,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); @@ -694,6 +698,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 ef887efcb679..933c5f23ef73 100644 --- a/arch/riscv/mm/pgtable.c +++ b/arch/riscv/mm/pgtable.c @@ -142,3 +142,24 @@ 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 (arch_is_shadow_stack(vma->vm_flags)) + return pte_mkwrite_shstk(pte); + + pte = pte_mkwrite_novma(pte); + + return pte; +} + +pmd_t pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma) +{ + if (arch_is_shadow_stack(vma->vm_flags)) + return pmd_mkwrite_shstk(pmd); + + pmd = pmd_mkwrite_novma(pmd); + + return pmd; +} + From patchwork Fri Mar 29 04:44:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13610056 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 6B333C6FD1F for ; Fri, 29 Mar 2024 04:46:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E59D36B00A7; Fri, 29 Mar 2024 00:46:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E0AA16B00A8; Fri, 29 Mar 2024 00:46:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B9AA46B00A9; Fri, 29 Mar 2024 00:46:28 -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 946736B00A7 for ; Fri, 29 Mar 2024 00:46:28 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 64C5B8113B for ; Fri, 29 Mar 2024 04:46:28 +0000 (UTC) X-FDA: 81948840456.15.A0438E2 Received: from mail-pf1-f181.google.com (mail-pf1-f181.google.com [209.85.210.181]) by imf14.hostedemail.com (Postfix) with ESMTP id 5CF19100002 for ; Fri, 29 Mar 2024 04:46:26 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=LiIWK3xI; spf=pass (imf14.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.181 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711687586; a=rsa-sha256; cv=none; b=ScsAGkkInQSXsymPmedNpsR48erA/yXzeVT7oQ8VqNnLRP3by5NcYTp1I4moAv1m5VH4kN aASrqWBBM2TtCfcQlKg2Cxny41c1vB/vnRjq3acC3+ujvM0XNBM6nA/5JB2TvxOIhnWixp nL2Yczem1v8sZrty7RSKuoTcf9AaU2g= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=LiIWK3xI; spf=pass (imf14.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=1711687586; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=+4XcZbT52YyPYTbBbJHKVVBDZyhFzr+R5No8SlxzOH0=; b=x0pKGWhywvY6u+wDf7yPl+ZXAYVQxiPw4U92vlEu+rENce1IspkCONuuRuGBLlFu6PTAyC 4bYfU+0FjSbyv+zlPXcGqeuFBRwSO/d4MvxGkndTGP8SSCBgwVtgOcsyPNBl+XedlqA69G cOf8t4QXK8tlaqxpJOuyaARgloQd9yI= Received: by mail-pf1-f181.google.com with SMTP id d2e1a72fcca58-6e6ee9e3cffso1196634b3a.1 for ; Thu, 28 Mar 2024 21:46:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1711687585; x=1712292385; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=+4XcZbT52YyPYTbBbJHKVVBDZyhFzr+R5No8SlxzOH0=; b=LiIWK3xIHSEQtX4bxE/j9KpvIYIxpzHqInr4ZwXsKIqdglpEn20VV+GgT9Lyoi+3Hq Heb142SUcujRy+NF73GkkexVj0lrra5Nwwut3vmMwe/xlkKxYq6V1+oAYqFZHk2jFH/H mjxQmVkiA9xIT+PkbVlyGDktVsVNW1pEfu+ISo1qlRN0GJVBv3mP3uTPaEdk41mYcyF8 neDvZkqIENaQvoHNSA7ILBsHlDcNkjZ2LeNMvs9bEj73+P0TYgv8VYUB5PY7arshVf0+ 4RaNIioX7k0KTLhXxN5CnrKxfQgz9mAqfhhkrNs7whn3Mu/1jBg0GPGAA/2oMO9jjPpC M+Hw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711687585; x=1712292385; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=+4XcZbT52YyPYTbBbJHKVVBDZyhFzr+R5No8SlxzOH0=; b=oIdglR9swUiCSS6aulgmcgTUzPI+vhmeuPee/PUZ2bgI5GAOzAWQA6pSJLmfZqwTl5 EnBrbA5iVbgNJmrA18vZQrWW5VlHoIlI/vxNFIqN9s/qRptN+WHCBhccPaYIA9Bh6EWy G0hazHZlppDdfSN06r9FUp80Di5jP9B0NM7aczphbB139i6EfHmHM+Z1xRvjeeWmp6BL 6LCNlFhNXFPSUbSMkQSE0yM4bN32FQqv8Xkm2+78pa8B5NwekRdiYtfLVmGUEQjFnFAw 981H+ojbzneLnYzoN/H0MskwazbaG8BSMG2DLd6APDpb2EpU4YdtKPeXrNn3MYFwTDoD 8czg== X-Forwarded-Encrypted: i=1; AJvYcCWsccHWJSv853zWNMnPq0jnzy4EjVX/ESkdc+vmXbFxNIoIhuNcdhYAsk4qgzNOMgI3/LzzHgbhACtNB/haMtVDqHI= X-Gm-Message-State: AOJu0YwD02xQjqVtcnijGct1RKd0EiQP4k06MjJxMB2DUovB/UScp+QU c+YnQsgLQVl7JPlccVYl2n2S9Db+hiLOXkf6w4G4pXeEJn97USxzlVYOeWcaMn0= X-Google-Smtp-Source: AGHT+IGU4KJtKBTHpZlkqt9cg1BlBHp1APuo4lcElwp7mwPxz8+nlm5g5TQdTRKqVuVUt4gfjoQ+Lw== X-Received: by 2002:a05:6a00:a29:b0:6ea:df49:c8b1 with SMTP id p41-20020a056a000a2900b006eadf49c8b1mr2139229pfh.13.1711687585128; Thu, 28 Mar 2024 21:46:25 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id i18-20020aa78b52000000b006ea7e972947sm2217120pfd.130.2024.03.28.21.46.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 21:46:24 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com, samuel.holland@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, tech-j-ext@lists.risc-v.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh+dt@kernel.org, krzysztof.kozlowski+dt@linaro.org, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, Liam.Howlett@oracle.com, vbabka@suse.cz, lstoakes@gmail.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, hankuan.chen@sifive.com, greentime.hu@sifive.com, evan@rivosinc.com, xiao.w.wang@intel.com, charlie@rivosinc.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, dbarboza@ventanamicro.com, sameo@rivosinc.com, shikemeng@huaweicloud.com, willy@infradead.org, vincent.chen@sifive.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, gerg@kernel.org, heiko@sntech.de, bhe@redhat.com, jeeheng.sia@starfivetech.com, cyy@cyyself.name, maskray@google.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bgray@linux.ibm.com, mpe@ellerman.id.au, baruch@tkos.co.il, alx@kernel.org, david@redhat.com, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, shr@devkernel.io, deller@gmx.de, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com Subject: [PATCH v2 12/27] riscv mmu: write protect and shadow stack Date: Thu, 28 Mar 2024 21:44:44 -0700 Message-Id: <20240329044459.3990638-13-debug@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240329044459.3990638-1-debug@rivosinc.com> References: <20240329044459.3990638-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 5CF19100002 X-Stat-Signature: syi5fqzh5nbgsxmejzmy8wobfa9oydrd X-Rspam-User: X-HE-Tag: 1711687586-232416 X-HE-Meta: U2FsdGVkX1822Og5iaNYfq1KkeBO8K9t4KsxFBC4oYHqJCJ4BwhDiNVHKkaVe7jR8HJqHcJAs8MXFnR8l4sb631cBqbEopg9KkJJqXsjelxDKiPJOorcL4T4Yv/IB0QJt1FabAAUcbBdTCWU3nZni5n8Xh54A7Fz7Aw2TXGp0433zIv8JNmPrtiY/KBHhedn/gYu5+wJnA4S/Sk1e6DFFb4dbyQsB9+jl9ktWjHqiK5YKYxcIyCojRsF06zfFYMzw5SrjkDWNBimRWxaxR01IF7iCq+kkOwWT5C/X441FNZJJ+EGdBlyhAQ081jjoE+qV5Nq3udDCFvrAg3lOuQU9j59N5UdIaCdRAgfgGQ7m2mIXQzMtSFd/8y2peTT9d7qLap32noAwyHEe+vkdAYYS0bH3u6dhMwiLctZo86eIUCUH2F1cwmBkQu/Z0IpSXJitTGwvGti1/+s6aD5L4gZQTbwaa3i/QwdXTum3MehgSPgKWmi774657UeZj00QukumW3osirz1k+IL8pPHkcJgwUcdwcdfN4krVFm5saNr59dDGKLeweYSAx2YmfYV3fOSiP7e96N71xjXboReW9vNwUxegkqr2YcVOrEa9aZzFkLl/ux0c/1c2dSd9ONZaGrsQPOzSasIT48ea1wZ2essWgLSQ3+CP4xKnCvcr2sZmYzO+UyRxFz8gxcKtqBVQiSomlgQ+usFMdKehsoY6sCLI/qrDt3Mk/4jF2EGiMAyqvMRd0whvFgM7mG1UNg4nR6x+rfaAo7RXwO18/nY2Y9QqfLFQK7s3JwZcmomA9PF5XwKsoYWmJLUTuR/1yYTNdoQczMyPaq/70DbLfVorow1unDRF6nbFsIPk6k/LUj0D2Yz9enoyCtJyfwvZZfL0Ie2KCOATMHMcYoZkGhKjCrJVYLtsqYjeYlJAqJIo9dJkTF6KRIYkWWnXjOplMXMHcAY6i5OIgMO/MCCaFwAZ/ Usp69NxH i6686d2H/7Jltn7d0xTh98SRKiJxgLvyUPtE4zEdBIjLhp6U4XG5e2K9N1VKS8HqcSjjWdxVGORFEFdeGJKN7vRnBAml3z8Bj3JzkkMdRshpj+Tpp0Q/chv+kKzBQxwasGK14kIdqWmYkueD8Gpn/89JbTYBV2pAeD24qyR73U2DCTrSvxxxMIK6KFVgi0/r+ebNlk8ky5yVxQzO6pTUhXPnRWvKlSPZqfl0wXZDEpVyXptO3ey6Uv3k0Oc0s+wOyaB22/SB1k3Fa/pqmXq4edgVeEcrF+WN9E5M5HiEyw//4Esqd/LF+QaEwrwER2k5iKGPkYfjtqHwsv5uknzEHjHIHFlU32NcBny9L03BoQELEy3bt4tfpiiyJbCAc0Rf2aAKJxN7i3eFvzsICjGmq7880/731Ba/3YHSMwRYE6U5dqLWEXt1/0NSNwrMzvdCxKdmE75IHbq+keq2X8VcmFVy3i0xQhT81rVjFAHIv5nvcmABzJy8rkotaZIHIIULAJB1dN4/+/0CFNS4= 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 such 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 --- 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 9b837239d3e8..7a1c2a98d272 100644 --- a/arch/riscv/include/asm/pgtable.h +++ b/arch/riscv/include/asm/pgtable.h @@ -398,7 +398,7 @@ static inline int pte_special(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) */ @@ -581,7 +581,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); + volatile pte_t read_pte = *ptep; + /* + * ptep_set_wrprotect can be called for shadow stack ranges too. + * shadow stack memory is XWR = 010 and thus clearing _PAGE_WRITE will lead to + * encoding 000b which is wrong encoding with V = 1. This should lead to page fault + * but we dont want this wrong configuration to be set in page tables. + */ + atomic_long_set((atomic_long_t *)ptep, + ((pte_val(read_pte) & ~(unsigned long)_PAGE_WRITE) | _PAGE_READ)); } #define __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH From patchwork Fri Mar 29 04:44:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13610057 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 5347ECD1283 for ; Fri, 29 Mar 2024 04:46:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 81DAD6B00A9; Fri, 29 Mar 2024 00:46:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7A2166B00AA; Fri, 29 Mar 2024 00:46:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5A6766B00AB; Fri, 29 Mar 2024 00:46:31 -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 35C146B00A9 for ; Fri, 29 Mar 2024 00:46:31 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 024F11A1092 for ; Fri, 29 Mar 2024 04:46:30 +0000 (UTC) X-FDA: 81948840582.21.2D2E4E9 Received: from mail-oa1-f43.google.com (mail-oa1-f43.google.com [209.85.160.43]) by imf06.hostedemail.com (Postfix) with ESMTP id 28C6F180007 for ; Fri, 29 Mar 2024 04:46:28 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=29WptZ+9; dmarc=none; spf=pass (imf06.hostedemail.com: domain of debug@rivosinc.com designates 209.85.160.43 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=1711687589; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=tRYuUV/HDlw+87fPFfw891sAepuDTsGv4tytTH7IM+U=; b=75OSTV2ZtQopg1gdYc8ywPu3M6ToP1fdYXcTjjEn1k/Umk5GY2NUJEIMRmU5W2JDljowJt nnys+5TmJ1R5FIY57Ja/48nAEB5ieFEzG8ozLP+RrPKoHz7Gf1YLeh3gnd8gQpdxo8GD+P l6MFPHv3Pdo3Er+QfAUl/jqpWPb64Ho= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=29WptZ+9; dmarc=none; spf=pass (imf06.hostedemail.com: domain of debug@rivosinc.com designates 209.85.160.43 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711687589; a=rsa-sha256; cv=none; b=30k0ecSvgVWmiJbTQdUJoi3f45pe9xuAiI2gcsb/pVg3sLNtBMxDE6cVyXBfV+0Uhl8MWb nHp06invcJf40gUhDCFfPdjETCKQTLBEcRZDYAMiGLMVGFW3ZNjXsl7ZMC8mPllTy3f7jD luGthJGmgoBY40LTk2JfyIdNYhwBVZA= Received: by mail-oa1-f43.google.com with SMTP id 586e51a60fabf-221816e3ab9so778219fac.2 for ; Thu, 28 Mar 2024 21:46:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1711687588; x=1712292388; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=tRYuUV/HDlw+87fPFfw891sAepuDTsGv4tytTH7IM+U=; b=29WptZ+9peZUk4JxkRl+bFz8h9bIly9YOgeinWGpUpcI6QJF/AQz8PGMjtFuNUOqWy RPkEpiKfViqo/LNTgGpSvHtsvRa44K9lxzw0YxAd8raGLDtyq79rn/JF61VmSauXXQD2 sDxw8MEZjcGrhGmJCq67n/Nbbn7kBGd8aAoOpNyeS2y0728ALi5/K7IfcTBpD79skfu0 HujesVyq04W2xDaCIY6NXjfAnB/H/mCE6sq3R1m82pbe+XecLlwOwEB2GU+zWu+dXtf+ zBc1jpSz/jZy0on5YbPEAo5nITM9fH7RMWOdlNMTNcZnRysqB724hd2F80LGtq4nRq7r D5eQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711687588; x=1712292388; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=tRYuUV/HDlw+87fPFfw891sAepuDTsGv4tytTH7IM+U=; b=b5vXn3PixpCjCP+pdN7KzBYYJb2v0HIoOjVQfYMnmd+1HCweQLNvTMYuC/GQkY1qme 2oVnQWhagF+zpMXGUs9BtVQ63WislZE9JulE9IS6ccUS77I77XLxk4L+xRmMDQv80g6j Izeo5/4NVoeJln2N5bS0faVoS2U3pShUswpdFALHeTpU/+4ea93ZzIKeA3s10e9ostx/ zCylJhTTCZ8OHDdruxHEYCz9+BM5nhqGUIVulKEPvgV1eF7Cc7s+A8NvIP0lbnundOee fyZB0Gr6IPVHvLtRG3qAs9cuc8mkz472qnfSpA57K0akZgN2rqwSnF3wMQ7NOKi3ItTa HdqA== X-Forwarded-Encrypted: i=1; AJvYcCXl2FDhEnLmTHN5i1loD+B068ymp+9JpI+TnPIU9eYsaTRyC1TmAeJgZQPHUDWAarbDvXNrh+16NvWk7eQK5X9lELk= X-Gm-Message-State: AOJu0Yz9L60zUExcaVop5DNZWJD9QWB9eayV5XmZ5BSKypqdRLZAWY/0 93yYAekLe4H8Yf7STaLLPA9RqurprKSt/bbPLeBFjkwOT3fXgCvjimVdEKNDaAY= X-Google-Smtp-Source: AGHT+IExHTclA4kNK7S7bjrk+V0W0UHQqweUym9pGZMbj3dJny2cMk1eOLNjAwq+8+i1fBbvmLIbAg== X-Received: by 2002:a05:6870:c48:b0:22a:6ad4:5c4 with SMTP id lf8-20020a0568700c4800b0022a6ad405c4mr1448163oab.3.1711687588092; Thu, 28 Mar 2024 21:46:28 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id i18-20020aa78b52000000b006ea7e972947sm2217120pfd.130.2024.03.28.21.46.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 21:46:27 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com, samuel.holland@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, tech-j-ext@lists.risc-v.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh+dt@kernel.org, krzysztof.kozlowski+dt@linaro.org, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, Liam.Howlett@oracle.com, vbabka@suse.cz, lstoakes@gmail.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, hankuan.chen@sifive.com, greentime.hu@sifive.com, evan@rivosinc.com, xiao.w.wang@intel.com, charlie@rivosinc.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, dbarboza@ventanamicro.com, sameo@rivosinc.com, shikemeng@huaweicloud.com, willy@infradead.org, vincent.chen@sifive.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, gerg@kernel.org, heiko@sntech.de, bhe@redhat.com, jeeheng.sia@starfivetech.com, cyy@cyyself.name, maskray@google.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bgray@linux.ibm.com, mpe@ellerman.id.au, baruch@tkos.co.il, alx@kernel.org, david@redhat.com, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, shr@devkernel.io, deller@gmx.de, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com Subject: [PATCH v2 13/27] riscv/mm: Implement map_shadow_stack() syscall Date: Thu, 28 Mar 2024 21:44:45 -0700 Message-Id: <20240329044459.3990638-14-debug@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240329044459.3990638-1-debug@rivosinc.com> References: <20240329044459.3990638-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 28C6F180007 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: 7g4d6h5747icewep9q8s5mt8k1ua93rp X-HE-Tag: 1711687588-354577 X-HE-Meta: U2FsdGVkX199aT629JJY3fxRzoh/2g1FVflXNESF1PF4CGQ7HUPo4O65CZQHLsZJ1o+uNgQ3kxKE7HdDvC1mb1ZNMLe0HhNZX53SCRCyrCX+QrADw41PTNSiKpsyvwb8kt+T+gEH2jCN6Rdis9k3ryjVeW5p/qZBYfh18msvEW6rRHD9DjbYNRQUAjCeFqnwhmtO+D358gjuHIXvREhauoeQSd5McPRRJSC85OpxMEVKfj4udbcJDluHXZkbtYoV2ybsWE+YgjP07bBXTeRRegZ3/7lLF+vI8/9NjhNrBYqpVNEXQ6Z5rpEZ7CN1WdORcJ+MLjYEgKZzW7/ZuiuweBDDUVb+Yd+xH0cfer0VoxIVbgzRSZ18APsylqL9UiIRFNt2vt0lgqfjtYxzeEOAK2mgxtTNlB6MxNfD0LR1dkrmkmeipHJE0TuzSUcnjFpcILfwJGZB+ISJcF/XQquWkeSy4zjCTeWsCNicT5SEQ3EOPlMQycwFYC9Q5pZ1aAV0688CH8zJZigl4i+lPP+tqOirxwtPjUHou/W1BFQZqpbgTaK/3fji4XTN0w0meb2T05RI6MTfiBGqsOw1YB28vo4rL+B+qgerWBYF2v3jxu1PJQVPURltXP/MYxn0+pPamUAF2AWYtlcTUpxZ3OI9lnhayfXEsPGarpvYFz+zToxQ+NkMxKSPHxJoLrctmV7M+quhEPJF10+K/v0ZMq1bL+s9OvDbEh4SkE/QLoU4eNgHrmj+RYDbpSxmlrm+CekMDxJQk5qBKumlO/kA81wQvXZZZj6pBF9KSkpk74CZeb2LRe+78bjQcbwpCbnQKUaDjx0iCmNbeu+PZQN4OcWon/wivuZyPwbqmaPE9kpNt67VCr7hmXtFYDClF9bzm+4YBb6WclOIJk2ZV3qgiZa52BpGHeV+RSbKEuAkjJVDwdmSjZZZFWz8FZe22lwtxxu6EM+718ikW44id6g2hiz prsg8dRB 1eSk72hwjJgN3YeYwGsjPJWUnuuC8+P3ShIhviVIZnBTE//iOf1LokTBa0tlOqH6jPEPYRLroym+CSIUg9cmDmRm/fourJaKI71htQ0t3G5cX0zSRMY4YvbvT9LNO4uybZJ4l0dqTKM6hFRHOaA82XlO5g5gbqr6ZI+haWkVfSuLgBwMPIoku64cHC9cTDntWHhdXY8eaBHTKiFYb1eZWHvcy08prATRjkIHq9Fi6+WX72CHn2PXJbRSA2S0ctO/qf431V69n9Tnywis/WdAxJmcGAUKzOKK0bNNuwIz9/FvMlSL2dyUcvV7CxRop+QYNACOeEttQHdP0V/3fxzKMTOP6S2aUZPcbskvefx8hMC1YIS4W7wB1/W/+Z+EIs2lXT1YXyCEVxGWmkt4v0uM8IR0A8sfa+JGtm2aAU1tj7UDlD+VyQAWsPE7dpcpAClZP3owZS8aju0jSN3+Ih6UiCJaSuPfbgRriwGhVyw0G2bMCKd4n5xP9S2J9ruB8alByFVu1qGH12E+mA3q9NgGbI2QzcE9OjVQHaPhLTmhzOQe7dgE2euCFvnfqDtfOozu/qDG1 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 compatiblity 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 | 149 ++++++++++++++++++++++++++++++++ include/uapi/asm-generic/mman.h | 1 + 3 files changed, 152 insertions(+) create mode 100644 arch/riscv/kernel/usercfi.c diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile index 604d6bf7e476..3bec82f4e94c 100644 --- a/arch/riscv/kernel/Makefile +++ b/arch/riscv/kernel/Makefile @@ -107,3 +107,5 @@ obj-$(CONFIG_COMPAT) += compat_vdso/ obj-$(CONFIG_64BIT) += pi/ obj-$(CONFIG_ACPI) += acpi.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..c4ed0d4e33d6 --- /dev/null +++ b/arch/riscv/kernel/usercfi.c @@ -0,0 +1,149 @@ +// 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) +{ + /* + * Since shadow stack is supported only in 64bit configuration, + * ssamoswap.d is used below. CONFIG_RISCV_USER_CFI is dependent + * on 64BIT and compile of this file is dependent on CONFIG_RISCV_USER_CFI + * In case ssamoswap faults, return -1. + * 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)) + 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..0c0ac6214de6 100644 --- a/include/uapi/asm-generic/mman.h +++ b/include/uapi/asm-generic/mman.h @@ -19,4 +19,5 @@ #define MCL_FUTURE 2 /* lock all future mappings */ #define MCL_ONFAULT 4 /* lock all pages that are faulted in */ +#define SHADOW_STACK_SET_TOKEN (1ULL << 0) /* Set up a restore token in the shadow stack */ #endif /* __ASM_GENERIC_MMAN_H */ From patchwork Fri Mar 29 04:44:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13610058 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 23EC7CD1288 for ; Fri, 29 Mar 2024 04:46:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A151A6B00AB; Fri, 29 Mar 2024 00:46:34 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9EC7C6B00AC; Fri, 29 Mar 2024 00:46:34 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 81ACA6B00AD; Fri, 29 Mar 2024 00:46:34 -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 5FDAC6B00AB for ; Fri, 29 Mar 2024 00:46:34 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 2647FC087E for ; Fri, 29 Mar 2024 04:46:34 +0000 (UTC) X-FDA: 81948840708.11.418C480 Received: from mail-oa1-f41.google.com (mail-oa1-f41.google.com [209.85.160.41]) by imf28.hostedemail.com (Postfix) with ESMTP id 5D523C000A for ; Fri, 29 Mar 2024 04:46:32 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="e5A/UgqL"; spf=pass (imf28.hostedemail.com: domain of debug@rivosinc.com designates 209.85.160.41 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711687592; a=rsa-sha256; cv=none; b=hgt7m6KXBNNveDz5FyqXXMrDU+x5HbRAKPJ98+aoRZ+Tls/usFGmZi7tS1qxYpWAHv8o2L Eyr7Nn4gEQ/vxqf1t9CdCmwJ4TGjmk4iKPrtwxg3nmTsJc2fgrt9dltl2mU2WO+1MAefgl Hoob8eFirKXMjRPhxwmrIosj3CQPGrU= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="e5A/UgqL"; spf=pass (imf28.hostedemail.com: domain of debug@rivosinc.com designates 209.85.160.41 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=1711687592; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=vWiplIlW31qhAH9G0EN+9lbe2UL6EQeHM69L+wLLGcE=; b=BEGatrI3kIndfHO/OcoYvxoFlhigAiTfOqz/FSpH0pwul3aOytHKZDqTp9OulPs7uR9cT6 prTYk3BWlVpynIMxNyFIum/eh/VNLZWxD/3QLAEQkKdRXdVpL/ljQyHZDliRRFcCDT982l n+zdeVSgJ/rP/dAf6xbYCSgs3Wfcyrw= Received: by mail-oa1-f41.google.com with SMTP id 586e51a60fabf-229b2396180so1019424fac.0 for ; Thu, 28 Mar 2024 21:46:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1711687591; x=1712292391; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=vWiplIlW31qhAH9G0EN+9lbe2UL6EQeHM69L+wLLGcE=; b=e5A/UgqLlwECD02tscQquxVCuuAdaUU1KlSo9HUQR8JQ2uf9h2Gr19ALryT0wyN1Az 5zZCXkS070lVZCMwcHwtSeOmKn0ju2XKiZqH9WgDQLbUzgUG905Ia0KLCW83mlkEloK3 lTc8tw/bGiGs0y1GRi+lEjQ0h7c1nEEBac0AKtk91ZhcctEEDe/L5s2MTFqkNU1UXDbD n0K1FINsdDd0Yx7UFr/XX/3oUdXUDprmEA+zr/GYXpXtdH0o7TMis+s2tjoLILzftE3D 6iKU1XSE/+Rx0QCXMBBYikQwZh21/ZqP5/NPE8MhrSw+g3euE8TEjjtprkaLDXSqsF78 mRyQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711687591; x=1712292391; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=vWiplIlW31qhAH9G0EN+9lbe2UL6EQeHM69L+wLLGcE=; b=NlEu/sBWfi8AbA72uWjcmOWRU6XVXQrQCTaMMW3Y2iNX6sdZWd0tdt/0BaIpVZjgZz wSUTODy5vI/L37QobVDRwaIvKzh5okXCH1O/DDT42LyjhnR+ZBBCWmu0b7aqjQs7lcdI UDZEYlfDpTwZih9cPIflCAaTGVy0kJ4xqKH4syqRWpl8eGfzseuzAyiS23vNZSNO195w v7ZGxFFpr1gObffmAPNe/mhGDei5B5OFxc7AkN0l3DNCkZF6RqLH2/F0S+xl2kHaPj/S qiSsqvx1eABjfT4FDH83MeAR/j/Oq1KRFW7awWQ7qI0vqiKik4RksysmmsuUzsHoK6bC au7w== X-Forwarded-Encrypted: i=1; AJvYcCXxWDDPILBTuqeZDBHiUX3PxillW6h2rYfxgyHYgNMtYBnpLKjRtUUVSInlAqX8xzedlwnEnn4mE70nxjOjvQCPFd8= X-Gm-Message-State: AOJu0YzFd3R9YgcgZW5nEXD1wA1R/M9Rl1A4odpn6tHxTQzOTdjV/W4/ 5LULmL5Ndf5wna2kWFGLS6vHBaYIPDDh8gzChLdL5igjH4+NGcQ6ZRM9rSnxdZY= X-Google-Smtp-Source: AGHT+IG2q899R9JGeS5nozXHnh9Ry9gbEzktPA3yoTFDILtnZi8qjOW1iZz6YGnMIm2Gd0G3acAhqg== X-Received: by 2002:a05:6871:76e5:b0:220:e608:89c with SMTP id od37-20020a05687176e500b00220e608089cmr680595oac.28.1711687591158; Thu, 28 Mar 2024 21:46:31 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id i18-20020aa78b52000000b006ea7e972947sm2217120pfd.130.2024.03.28.21.46.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 21:46:30 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com, samuel.holland@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, tech-j-ext@lists.risc-v.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh+dt@kernel.org, krzysztof.kozlowski+dt@linaro.org, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, Liam.Howlett@oracle.com, vbabka@suse.cz, lstoakes@gmail.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, hankuan.chen@sifive.com, greentime.hu@sifive.com, evan@rivosinc.com, xiao.w.wang@intel.com, charlie@rivosinc.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, dbarboza@ventanamicro.com, sameo@rivosinc.com, shikemeng@huaweicloud.com, willy@infradead.org, vincent.chen@sifive.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, gerg@kernel.org, heiko@sntech.de, bhe@redhat.com, jeeheng.sia@starfivetech.com, cyy@cyyself.name, maskray@google.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bgray@linux.ibm.com, mpe@ellerman.id.au, baruch@tkos.co.il, alx@kernel.org, david@redhat.com, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, shr@devkernel.io, deller@gmx.de, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com Subject: [PATCH v2 14/27] riscv/shstk: If needed allocate a new shadow stack on clone Date: Thu, 28 Mar 2024 21:44:46 -0700 Message-Id: <20240329044459.3990638-15-debug@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240329044459.3990638-1-debug@rivosinc.com> References: <20240329044459.3990638-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 5D523C000A X-Stat-Signature: exttokypfuh9j1b9gmhppx96h7zjyzik X-Rspam-User: X-HE-Tag: 1711687592-588111 X-HE-Meta: U2FsdGVkX19qvgubjVjrRgonMhQJY9IBgmWSi1M80muGNJMonqsx61WDFqdFpPN6r31QBczsd9XuWEtGWgeUlPJwwfjBm2J9TmQD6yqTsI/gGhrTc3A6F/OVVQ9L6Esi2SducDXwk1LijO2xx4J7Q89II3Q8JiQXj9/VAkYxSKFeLeYhCv1jPORpDasrzBqTeItXqUFtJiXqy+jh+UpVChXTgdzTd9hgHoBay66heKoYti1cMvssknGi4+tkqZiQUZ0WTdG5BYnT354hCY8nZUGqEgF2r1211iMh8oGQI1fQKxblqmI/E9OhfnI3zUFLnJqWy0BTA83/yaxTFyJZoZriVkz8j8De6bxbthWSE1ufqmpdXkTwS6/AGJz+2BrkC6LRpUj0wXoDXB0GVPxVrCm/7uj6arIsN1VHjhYumG+T6UCaHsiH0nE0kRBdz+dzFVK6GCROuYpQn/csO8ReAdkx5911tvSRO9Y60xGh3lAd7ONDULOmcc00zRzJIVU8PvsFQWBX/WszGrBER1KSM1DLIBNOMlg+rEofpZHiYi44FdYNqZ34vfpNmnpVjZ0Um1CdGVeCknIecAsZhdX0EsL8LcZ7uUKVdpftJy6DBku0Yk6+5yDzd2eAN7V8BHt9d0ZQy2kRxgrkimEXJWN2EfX56dvpPt9n+sObynEXdWlQBwdKL0dNQ2piIC1nilJnJkgpJ+ClLhzbHeEQQDXo9dgI4vfk9IzQh281YgH1mJ/BpmdRYIL2wbOdUGqEnBlHDQBOmdghIWgqyNF33t0GzbByAAKoJcsySPmRicSsjDDyBahozaQsZhuP0YWsu2/S8wvxuOjCyX47LeIOSPqnFBCdaABSXNiPPGuHpKNpLkvIXTr8+W5+ktClp7J7XSoQhFUWdijRmIVAtdOkkqZC6gAqEOg2tGCaZnIhxmtFCS90UsqUPmP9iPa5O+tCTeSyJgp3LFxcpzud25HTXDq /Tsu1+gq TJYMQbSZWB+7FL8G6M6kgkz53Cvt9nZiZwiqXYLgNBeGd82LDqzF+rNT5jtKgLGxYU0P37sNFx2/RS06v+R/HYfKBluZ2hK4hgrFa7vU9C3/QZLHPOnbUWx02CqP3VRpdEvdSJo6eCjB2TR4rThMC24yINlIswmH3McAZPLOIgsh/63liETNPeiCDP+1aQhsJOM/ncp09OPHywZ577qO9FF+PcELy0VGXXWjGLOJsIhgCmDGTLt2gT8mvu3I/wZ42DAuVnT2LpKlTi7TBYuxid3g8Wme22NK7z9JySgpkRUn0rSYZgYrBz+cEId4MnZ8xUIImhLtfzR4fIqyNsZZrv137GT8vDw1IXfE1ZLnaLXwUnpfrS1dNKzwfTY+JW42gbA7KXj0RtfJTzLnrBUx62ewZIjzihcR3IWIBzbxae1e+XxfC/toLA7l5Od4DUW4VdP/BG4ww6ujOtiTiRCnumRk7b4x+cP0Xe41cb8GYxe7ctLyfJH10nEcce/RTHmbIyBVMOTdyaEWt4Rc= 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 VM_CLONE 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 VM_CLONE is given. In case of VM_FORK, parent is suspended until child finishes and thus can child use parent shadow stack. In case of !VM_CLONE, COW kicks in because entire address space is copied from parent to child. `clone3` is extensible and can provide mechanisms using which shadow stack as an input parameter can be provided. This is not settled yet and being extensively discussed on mailing list. Once that's settled, this commit will adapt to that. Signed-off-by: Deepak Gupta --- arch/riscv/include/asm/usercfi.h | 39 ++++++++++ arch/riscv/kernel/process.c | 12 +++ arch/riscv/kernel/usercfi.c | 121 +++++++++++++++++++++++++++++++ 3 files changed, 172 insertions(+) diff --git a/arch/riscv/include/asm/usercfi.h b/arch/riscv/include/asm/usercfi.h index 4fa201b4fc4e..b47574a7a8c9 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,42 @@ 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); +void set_active_shstk(struct task_struct *task, unsigned long shstk_addr); +bool is_shstk_enabled(struct task_struct *task); + +#else + +static inline unsigned long shstk_alloc_thread_stack(struct task_struct *tsk, + const struct kernel_clone_args *args) +{ + return 0; +} + +static inline void shstk_release(struct task_struct *tsk) +{ + +} + +static inline void set_shstk_base(struct task_struct *task, unsigned long shstk_addr, + unsigned long size) +{ + +} + +static inline void set_active_shstk(struct task_struct *task, unsigned long shstk_addr) +{ + +} + +static inline bool is_shstk_enabled(struct task_struct *task) +{ + return false; +} + #endif /* CONFIG_RISCV_USER_CFI */ #endif /* __ASSEMBLY__ */ diff --git a/arch/riscv/kernel/process.c b/arch/riscv/kernel/process.c index d864eef5a10d..9551017d16db 100644 --- a/arch/riscv/kernel/process.c +++ b/arch/riscv/kernel/process.c @@ -26,6 +26,7 @@ #include #include #include +#include register unsigned long gp_in_global __asm__("gp"); @@ -197,6 +198,9 @@ int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src) void exit_thread(struct task_struct *tsk) { + if (IS_ENABLED(CONFIG_RISCV_USER_CFI)) + shstk_release(tsk); + return; } @@ -205,6 +209,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 +225,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 (ssp) /* if needed, set new ssp */ + set_active_shstk(p, ssp); 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 c4ed0d4e33d6..11ef7ab925c9 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 @@ -147,3 +182,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 Fri Mar 29 04:44:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13610059 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 4613FC6FD1F for ; Fri, 29 Mar 2024 04:46:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AB2116B00AD; Fri, 29 Mar 2024 00:46:37 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A629E6B00AE; Fri, 29 Mar 2024 00:46:37 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8659A6B00AF; Fri, 29 Mar 2024 00:46: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 5B8E96B00AD for ; Fri, 29 Mar 2024 00:46:37 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id E30281410CF for ; Fri, 29 Mar 2024 04:46:36 +0000 (UTC) X-FDA: 81948840792.12.595B4AC Received: from mail-oi1-f175.google.com (mail-oi1-f175.google.com [209.85.167.175]) by imf22.hostedemail.com (Postfix) with ESMTP id 226FCC000F for ; Fri, 29 Mar 2024 04:46:34 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=H1l1+P0E; spf=pass (imf22.hostedemail.com: domain of debug@rivosinc.com designates 209.85.167.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=1711687595; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=RhhwcNmyZsW8Rn+io2bmrU6vZaKqFl7C8RSpnBjD5D4=; b=GZBJ8NSHjDOsD9FVJn0a4Len5KxpKZNADccBSsAY/fRy5bgemru9pXRjckO1s9GsbjfAB7 ElD3CqCDzGcvcIYRYbxGge3pM7idmIbKOJ9v2chpJN7Dv5Ink6GJ9vhZCOTWDQIPRpiKbQ t4v3HwfJ8ni2y8SaAa3Q5f4TliHNT7s= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711687595; a=rsa-sha256; cv=none; b=EiBLJT57oCIgahxUqNDLFt1PV3dzi4Kixeh3KZgwzW8a0n/8Eu/JWQEm/uh+5WvhiohRy3 9IadRUDU7fUDGzJRDk2qSJW0i4uvZpodJcNAWsVLYUsoythZL28X7XQHcZlQ0CFh6/42jQ 33cFaMSRo8rH0RbrbgUAZORVI62kVbY= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=H1l1+P0E; spf=pass (imf22.hostedemail.com: domain of debug@rivosinc.com designates 209.85.167.175 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-oi1-f175.google.com with SMTP id 5614622812f47-3c3df13fe31so1005762b6e.0 for ; Thu, 28 Mar 2024 21:46:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1711687594; x=1712292394; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=RhhwcNmyZsW8Rn+io2bmrU6vZaKqFl7C8RSpnBjD5D4=; b=H1l1+P0Eu8z+EbUZQzT+A6FPVb1nLL1ylp1Oz7f0XBM/ITHjMT8auH66zHESbL4rg8 aA5u6PxQqTV48iHuqja0b+MSMi663cwFdbP9QyIzxAO8zHlKwEzYJPQumG8+GshT2Vp0 Y9scNw/3iD9kbhkKwb06NMOLsz/P3RrG7IRkp+noZs2xZWV6gf5VzBhAobH1i2NjF/cI h4USNQ36+FksLgwRXy3fXASvjyghIttYJhnknJ91tPhz9ScglfznnNC5x8IXOsGyT5YW 52RhYNH3oXqKxFzDvP8Qks7OVhgQX/y8YHO1mrO6wsQNqB6Y6tehWxPbswLAGY70Jlk4 yFNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711687594; x=1712292394; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=RhhwcNmyZsW8Rn+io2bmrU6vZaKqFl7C8RSpnBjD5D4=; b=Uc0/U1c8jL0cohJmQXGb4s13AaCAIVDqLZowHN7MbPL2dMjObaL/eAj24lsJF+Mf8Q SJp0LHuLPRBTSxW1xe9qfU+XgeyASV01xGw2PPfEEY5UzTP50525RwFnCjQFQ/QPN9dF KVvBJVxZfGM7AtSgvtdV4OP5PqTHCNQlHjVf/o3LZ5yXAF7CLz7uPsElCRCxQpXWwpzL C4ubiYRa2zrFiwP4/l4sxY+SxLdLSGCQLGLHw9SeY2rO9EyWCetC2SFW0zXc+IyOVu0c jJWfoVHRmchjf6khNVnAqHGn5HYHMEh8fhy6ZcA7SXVgk4XP8rEfOkn+EjGjPmNhXIsP X32Q== X-Forwarded-Encrypted: i=1; AJvYcCX5CQiIj3bR/69ws1S9NAu3VXzC5h0J8xhoOsIu3tQhCzFbwsdrBLmUaM59QJEDUDuzP90hJJbfHaqztgfSgtQTIXk= X-Gm-Message-State: AOJu0YzInFoAYkmqSaslgl6nD6i9KFo1djU/t02gEMHAOTAzKl8Xpz7h Ye6zrWBxGtlN0/Aef/SU4Tyf5SfuHOL3myDfPigPKajBxdl+cAVqhY4+WV29U9s= X-Google-Smtp-Source: AGHT+IHzCsJ/EULMJ2X0/zuKDOZ5gM2wFJf32HTwGAn7YaJ1oTsNWG5xRVVZi9OrlqC1Skwk/hVE2A== X-Received: by 2002:a05:6808:2d4:b0:3c3:d6d7:e57e with SMTP id a20-20020a05680802d400b003c3d6d7e57emr1229439oid.15.1711687594157; Thu, 28 Mar 2024 21:46:34 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id i18-20020aa78b52000000b006ea7e972947sm2217120pfd.130.2024.03.28.21.46.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 21:46:33 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com, samuel.holland@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, tech-j-ext@lists.risc-v.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh+dt@kernel.org, krzysztof.kozlowski+dt@linaro.org, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, Liam.Howlett@oracle.com, vbabka@suse.cz, lstoakes@gmail.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, hankuan.chen@sifive.com, greentime.hu@sifive.com, evan@rivosinc.com, xiao.w.wang@intel.com, charlie@rivosinc.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, dbarboza@ventanamicro.com, sameo@rivosinc.com, shikemeng@huaweicloud.com, willy@infradead.org, vincent.chen@sifive.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, gerg@kernel.org, heiko@sntech.de, bhe@redhat.com, jeeheng.sia@starfivetech.com, cyy@cyyself.name, maskray@google.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bgray@linux.ibm.com, mpe@ellerman.id.au, baruch@tkos.co.il, alx@kernel.org, david@redhat.com, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, shr@devkernel.io, deller@gmx.de, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com Subject: [PATCH v2 15/27] prctl: arch-agnostic prctl for shadow stack Date: Thu, 28 Mar 2024 21:44:47 -0700 Message-Id: <20240329044459.3990638-16-debug@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240329044459.3990638-1-debug@rivosinc.com> References: <20240329044459.3990638-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 226FCC000F X-Rspam-User: X-Stat-Signature: otouxded1jznu9wi8migwi91ybtq4i74 X-Rspamd-Server: rspam03 X-HE-Tag: 1711687594-311507 X-HE-Meta: U2FsdGVkX18Grkv0/4UZy1R7bFLslF31qWT95DElmBpFr+czMYDWmr4h8H+CAW/vZeycJXGoUmGJq8rUomW6O0tLjbgI7AmULIdWpyULbQwm689ohSP5tGSlOF8bW5JOZiRqgZGZd3tkIbFXi9V/GPnKDuYJHP2u8v9pUOm//Ailo7d8c3SEHplg9cOjZnAvWb3tM8a3NIljEgl6dqsyKzITjKLQomk+6uIui08IlCdKNspl4/n2CgpNUnEAV1E0O/vgo4nzlp85wbO6/KF2FoR+O31K9l3q1KZmn+Qpt5UINoHST5sRODmv+Ns5ULKJV6kfk639ALSgh+rfy1Gs1vJs7QKc8cEWMVoKAo3ywCax6ZJmDHB9FkWWyE3XbvR8ODfoqzBj3DixlWuDXGfNWLPZbCD0qoCFqhoAMjRm8IV2W+XUjiMYJ0ca8nLAu1mMiQeK9AspDKicSGrJaMkDSp5P4SLZEXbVzpjlXH3d8ce6d+s2S7DPn+X3SJ6sqvW/hx5vvP7mGWxNf+sYeuhT0bf3Zs9KLWp+vgfKNu/9Vi742oxMGp6CPbg5+8Xr+XO6bSgS9mjsllm5a/hbtEsyxoqq145hmgRp2gNtsik0S0dEVPxYgLhReBReIcE12wOihWB1xiFuyo5yZu//7DovBsOTMm+I/wIE44Mgo40JdJqHBWdop55LVxdwZQV916YDZqALL6rBh9XnJnvRumA0t4gUp6F+uDbCiHIp1cx6ifRE2psqUjQYI3HfGQvP0rolliQOPLW7ld9jf3BS0YaL9sS+wWZBk1DO8tVQqfAdXdiwDYbHa6JLth4BfEYGaYNdJHuVvsVRj4fM4dgjy4J1U0VVMb8sEcgHNbZnbfpZuCKlIiANXdu2LDj12sgluzjH+vf7kiSwbSIwRlSQZ9EjypcO4xchpgBD4F0vxiulBbsinRt4emPiSddp/CMNcEw4u10DNsikze4kUGZL3X+ 31zDoCWq RmHEaiTFeujjgO7EuIMH1eDt7Wiv0hf55WOFQxjixx8V8Bgo2r4ySeA+5Rkzm84ffsCaehUoYObnjI3n4Z5dvbcYrmtM3XIRseKMBFNpFdWznAsQNVMF5t0+G48zOKmlQXeKYtBf4hvnU2FnugaS2gReaclxhUC/xFNsor6TEd6j40nAfWzlYaIQD2HTW2Lu54oRWz8SNovqDgrx1IedVw8Dvyfs3wnk+DZtJdNGcg54iTIBAVJuH6+thCFB//5q2pNy0eDe2QQz7UWuU1TNCUUSgpNyU4Bcwm+/faYfVe6GVM/T/WuT5B68MtSQ1CQ7KBcv+GDAF8EEGBTw+ndFrbSi2ckRSNdapaYenZKPZbGyJPalM/KBePLNJnjYovyQaW0bsHAulen8com+SCcbGEpiXFj7bdkElK3xEah4Q6I8nEiAk8JkBhSDUNitVn06R2vJAZElA3nQXhG9pgSugnA12Btu5bu+umRFpcUUC4DInAqP/avBr/tIfb9kaBzfTww5jFVV76G0mA3felnYa8bddPnFG8N0A/Vl74oBc4vG5GaU4RT/ISbafOuhirw4jPqArV99FP+w/pcgsCcx+9guVCA== 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 configuation 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 Co-developed-by: Deepak Gupta Signed-off-by: Deepak Gupta --- include/linux/mm.h | 3 +++ include/uapi/linux/prctl.h | 22 ++++++++++++++++++++++ kernel/sys.c | 30 ++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+) diff --git a/include/linux/mm.h b/include/linux/mm.h index 9e6a4fbfccac..5ea49b9232fe 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -4209,5 +4209,8 @@ static inline bool pfn_is_unaccepted_memory(unsigned long pfn) return range_contains_unaccepted_memory(paddr, paddr + 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); #endif /* _LINUX_MM_H */ diff --git a/include/uapi/linux/prctl.h b/include/uapi/linux/prctl.h index 370ed14b1ae0..3c66ed8f46d8 100644 --- a/include/uapi/linux/prctl.h +++ b/include/uapi/linux/prctl.h @@ -306,4 +306,26 @@ struct prctl_mm_map { # define PR_RISCV_V_VSTATE_CTRL_NEXT_MASK 0xc # define PR_RISCV_V_VSTATE_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 71 + +/* + * 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 72 +# 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 73 + #endif /* _LINUX_PRCTL_H */ diff --git a/kernel/sys.c b/kernel/sys.c index f8e543f1e38a..242e9f147791 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -2315,6 +2315,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 @@ -2757,6 +2772,21 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3, case PR_RISCV_V_GET_CONTROL: error = RISCV_V_GET_CONTROL(); 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 Fri Mar 29 04:44:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13610060 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 29646CD1283 for ; Fri, 29 Mar 2024 04:46:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A8BBE6B00AF; Fri, 29 Mar 2024 00:46:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A147E6B00B0; Fri, 29 Mar 2024 00:46:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 866E86B00B1; Fri, 29 Mar 2024 00:46:40 -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 5EF016B00AF for ; Fri, 29 Mar 2024 00:46:40 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 26C6FC087E for ; Fri, 29 Mar 2024 04:46:40 +0000 (UTC) X-FDA: 81948840960.26.1F5662B Received: from mail-oa1-f43.google.com (mail-oa1-f43.google.com [209.85.160.43]) by imf09.hostedemail.com (Postfix) with ESMTP id 4B750140019 for ; Fri, 29 Mar 2024 04:46:38 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="ekq/jReQ"; dmarc=none; spf=pass (imf09.hostedemail.com: domain of debug@rivosinc.com designates 209.85.160.43 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=1711687598; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=jOjKUP7R2Pn5KHy+bcQHndffhGuedIhGkkiUJ74Ko2E=; b=Hnrdecn1v32XIBkx7XF2eOApcvX8xudfutanAwzo0o5I0RLWtbcbbXHi4Zh3GmP0h+Ogp0 7ZK/jHslsyI22Ue3vnMu7Hr8XU4SrRhYE8RdVm+QlmaH1MXl4FBmPWEu+/9I9N8FKYCRrs VyYQ4w3E0LSS6iMCFOilUf/dcYqooHU= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="ekq/jReQ"; dmarc=none; spf=pass (imf09.hostedemail.com: domain of debug@rivosinc.com designates 209.85.160.43 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711687598; a=rsa-sha256; cv=none; b=Ed7IO7+ms9and/mwZmYpkmInrkZo9nVQ7y8DPAnXDgai5bhKEk4AgP0I55cvgGdXxdIE4M +tE+ElRwVOvECLUN5iWnzif4VC2cjk8YRDeaZiWYw2fflRkGbEm/pm9qR457oFVNVX+Mag h0YYiEQ0IMPcAm+OVO6sMOPWbwkESZk= Received: by mail-oa1-f43.google.com with SMTP id 586e51a60fabf-222ba2a19bdso1020961fac.1 for ; Thu, 28 Mar 2024 21:46:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1711687597; x=1712292397; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=jOjKUP7R2Pn5KHy+bcQHndffhGuedIhGkkiUJ74Ko2E=; b=ekq/jReQ8BhqkTdtyuKcBSaNHN/ORoRNrx1ZPHwrCwrG2gi9Co0uxsqRqZuOIxu5/U 4l5kwYJQSi8dRu4V1PrhK2MvMRYMMaTsO7nt8mas4IWWYE3HvD+aRyE8tRIQMojlKm9y P0mLfKcchHpfk7Bm36PvSVyD6HDhEhzGdNSB6vRrRbXwOG7NNfAoeWZbZZjeRDjFgu08 8nEKRiTTrvaGALCE/QinDIHVpc/nWLPN0st10+HJuA5i5qETHVxerfUpdNlNC+u8dZd3 fJAj1HHMX1gKr4e83GyoMXOZCT2kU8iGj3TnZP6fym6+x1bsc52kTLYGricXGn++z9AN 4+vw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711687597; x=1712292397; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=jOjKUP7R2Pn5KHy+bcQHndffhGuedIhGkkiUJ74Ko2E=; b=IoPL90bu1lm+QsD4ktzj7L8XA5og6yVTVyuBh795Z0FaiY+W+bTuQxN3a7M+rNRmT0 3oHOXF8kCInt1szb9V0Rc+NlCXPcJY5tL/dZ5YkLPEj7ZigmPR/pqGKirnHRpqXBcBzS pDINSH0QDhvMt2/SMwcaz33r1e0uuLdvaBKKiXVsGAcyTdSrR9iyEDLRxdk4dzxaHOcB WuYUfun/EDVdF51WraPPSJGekgQgXan7/ujOv0HnUY4pj5gcn4FlEVkOcNJXa23dHlhG 2+d5g54vxRaYlyMoCpF3zaJReK44x8q2go+Jh8mYUjOIfvClCzL/Pjqv5yPK76LHG05q pTYA== X-Forwarded-Encrypted: i=1; AJvYcCW5XjFRPPoLpOlFekI4NQGF8mJZUaYw0bJdmGJ1GcK8PlMIfKjA2MlsBxyTn30n7L5Trs/mYv1znZ00zuPooVLbHsw= X-Gm-Message-State: AOJu0YzwLMrxIW/Ip++pOGsHG9Dk/phnE+ZczF9OL7VVmwB/Fizsjm+v j0vc4DModIjqGe1Qwu/eTNKe0/g6WTFtIDSKOZ0F9NqAzIooWah/7WaqfzY1TPY= X-Google-Smtp-Source: AGHT+IGJQSx7xjJ7n2mbejRm8WQQAwrhRnla3swVvKyB0ZaA2dVNGQk8U096glMvDQJ5OLMIlZU/5g== X-Received: by 2002:a05:6870:b698:b0:229:e636:921f with SMTP id cy24-20020a056870b69800b00229e636921fmr1250647oab.49.1711687597188; Thu, 28 Mar 2024 21:46:37 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id i18-20020aa78b52000000b006ea7e972947sm2217120pfd.130.2024.03.28.21.46.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 21:46:36 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com, samuel.holland@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, tech-j-ext@lists.risc-v.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh+dt@kernel.org, krzysztof.kozlowski+dt@linaro.org, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, Liam.Howlett@oracle.com, vbabka@suse.cz, lstoakes@gmail.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, hankuan.chen@sifive.com, greentime.hu@sifive.com, evan@rivosinc.com, xiao.w.wang@intel.com, charlie@rivosinc.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, dbarboza@ventanamicro.com, sameo@rivosinc.com, shikemeng@huaweicloud.com, willy@infradead.org, vincent.chen@sifive.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, gerg@kernel.org, heiko@sntech.de, bhe@redhat.com, jeeheng.sia@starfivetech.com, cyy@cyyself.name, maskray@google.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bgray@linux.ibm.com, mpe@ellerman.id.au, baruch@tkos.co.il, alx@kernel.org, david@redhat.com, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, shr@devkernel.io, deller@gmx.de, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com Subject: [PATCH v2 16/27] prctl: arch-agnostic prtcl for indirect branch tracking Date: Thu, 28 Mar 2024 21:44:48 -0700 Message-Id: <20240329044459.3990638-17-debug@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240329044459.3990638-1-debug@rivosinc.com> References: <20240329044459.3990638-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 4B750140019 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 6ci97g6yg4k4ihya3cgcjw8yztd6zm5s X-HE-Tag: 1711687598-993491 X-HE-Meta: U2FsdGVkX1+Wkqvv3ChKRC+sugBUFkvFBsimzCUP5qmKIYHe0gpyx9+hXU+7Yf4eHlK6y7XdjiLrS5iWpQgj8L5aHS80iCcHG3DBic5VqcwB64XEhKSzcHGae4ybP18VsAL7GR5AA0SBN0CnBRlpwevSKdud8KtY9RCl/2lbXw9hzm4f+ifxc/3mniulS9Waojl0WHGBDYdfcDecdhwHZKAv+9Vyjfnd5gTYvbptClmn75Vh5f3JXMtb3+umb8JI5q+pb0yCEo36si/6AJLy6ASnl2vtRkrMrREyfaVkD//qFyX0VLj9tRaY9HqUlgo/ne3BMaMYUICiK2OJkLUKftpmlfIEZ7jWTIsQKib9VTecV/pLDT4EjPUZ0SfAfgyXBt0I0Mgv+2sx71tQGhFgBwUwnybJlzOu/SljuhGWGNvFXK39XOVcDf+3qrS39lap85vtJvXhYXyOsTY5jYUOc2hGCPSivPlI8DbtLKg1Zc203v6/WY1eFpwGmRfSI0HKC4wHRtk9DOrK+MdqF1aC5kbKlN30E0zJthCCsckLdbzB1E5Vj5T1T9X3Y9RZQVTsaHPO63EenEH9mzssRadGrMJ4SrWQv/eaK5A1+gbp+dC83VwO6iZXUJKhStbm+yOz6GOMKGjZCdUK1593afNEvUj3nbsLbRjJ3Ja3GF38LKbIX27Z2nRekbOId8H34hu/KsLIAPn+MYGRATZPCoq4bqjtb0lyYXiDoNcwUCP+Ah8aWtx4moxGT2VJt6s6FTJt1W5W+hMiogkwhmiGDPe6N+FIKv2H9Jf/irJ0H2yEO7zXP4d7Kra5XLEMLi/Sebw9RCXrZPbEY4OL0TWW2a6uIrKIp2fD5DgvpvOsNWVF9fWCIqHncPQA/ccm/pBFBmOnkuMPnb9rUcVtxhahHIHrMZ0P4Zfy1rmAAlmBurlJCCorVd8rvQMp8ezOmcdxpBmbGETI9phe7PD6sODaHU+ kDJ1ylbj Fzt8I/i4pMUlsSDMujdjXk9BaenskqI6+3qHcELoWhZS6aEo9aL38z7fOFDb7tbHcCVVQvrxDVfNyAn/xDctu3GaG6i6laaOl8xn1DhIWpxOaA6W8WMjKFG/HZIT9OpcgKn90GFrttaj9s/53/vEYxGlLU5tvRnVcp15p1uKPXI9CIqD9wYOmM4RPXro+wZ0aDD6kLc+G9SPlopu874MiZeBcBDVd0c7Y0ztiLVvT5LfL1NRwbdHzljh3smMURWK3hMvMqW+/PG6iv597bfkc6DQFxFW+YAsNN1FUP8O17Apdc8MCqan8ajsRG+RRgBsMcN3O5+tFRN+joooY4uikLDGTbuB+l/18DtfohSOsKs+J8JXDamtszen4OGwPQwR1vR1vgVVF1FLMqrf3PvQk/cT7EOA7n3J2y4fiuOYnPgtKOQz0oe4pQNSrzqThZvtx4h3yCK9RadeaC29yUiv0ET11HrImwLmO3X1J+rM3ebYBkXH4/+wjq7f5WUAyAJMjp3kFj1IUbX7+SBE= 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 --- include/uapi/linux/prctl.h | 27 +++++++++++++++++++++++++++ kernel/sys.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/include/uapi/linux/prctl.h b/include/uapi/linux/prctl.h index 3c66ed8f46d8..b7a8212a068e 100644 --- a/include/uapi/linux/prctl.h +++ b/include/uapi/linux/prctl.h @@ -328,4 +328,31 @@ struct prctl_mm_map { */ #define PR_LOCK_SHADOW_STACK_STATUS 73 +/* + * 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 74 + +/* + * 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 75 +# 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 76 + #endif /* _LINUX_PRCTL_H */ diff --git a/kernel/sys.c b/kernel/sys.c index 242e9f147791..c770060c3f06 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -2330,6 +2330,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 __user *status) +{ + return -EINVAL; +} + +int __weak arch_lock_indir_br_lp_status(struct task_struct *t, unsigned long __user *status) +{ + return -EINVAL; +} + #define PR_IO_FLUSHER (PF_MEMALLOC_NOIO | PF_LOCAL_THROTTLE) #ifdef CONFIG_ANON_VMA_NAME @@ -2787,6 +2802,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, (unsigned long __user *) arg2); + break; + case PR_LOCK_INDIR_BR_LP_STATUS: + if (arg3 || arg4 || arg5) + return -EINVAL; + error = arch_lock_indir_br_lp_status(me, (unsigned long __user *) arg2); + break; default: error = -EINVAL; break; From patchwork Fri Mar 29 04:44:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13610061 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 4C00CC6FD1F for ; Fri, 29 Mar 2024 04:46:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D4AD56B00B1; Fri, 29 Mar 2024 00:46:43 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CD2706B00B2; Fri, 29 Mar 2024 00:46:43 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AFF996B00B3; Fri, 29 Mar 2024 00:46:43 -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 8FDF36B00B1 for ; Fri, 29 Mar 2024 00:46:43 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 60950C0843 for ; Fri, 29 Mar 2024 04:46:43 +0000 (UTC) X-FDA: 81948841086.23.453A028 Received: from mail-pf1-f180.google.com (mail-pf1-f180.google.com [209.85.210.180]) by imf04.hostedemail.com (Postfix) with ESMTP id B4E3040004 for ; Fri, 29 Mar 2024 04:46:41 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=kNXAhu39; spf=pass (imf04.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.180 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711687601; a=rsa-sha256; cv=none; b=s5P9duz9LmiEJTTehmNuW1y9ZBucRUy0/EJdELcrAwlWnMiCeJKvXOVzTjhb3bJT4MMSmg cBDvUG6OiNsoiwXDWsoHD8A6rswtX8214OopQJosaA7naPx2FJc35KccjmsusHyzAnCP/A ZJSXRp4pFWBkvTk3b3wqeYcdF6xZZo0= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=kNXAhu39; spf=pass (imf04.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.180 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1711687601; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=OSTo4BG6xg4Q2onv+lGORpCI7MnnKplqFj/mRoj65CU=; b=cB7n69249Bis631Z1eb3y9BNGE5ich6Nddr1WEIkO/IOIAGJ/3YyAq33BWbV505O4kD+v+ YSdIlRb3aq9Hn3bO42vrJuaLxX2ZW+Hv7ruE4kDHoQtVSgMDUwlb6eBC5Ruhul2dQzI+86 W5zSPN1oqoWmVLAPwvJU97yhL58VWAY= Received: by mail-pf1-f180.google.com with SMTP id d2e1a72fcca58-6ea8a0d1a05so1782344b3a.1 for ; Thu, 28 Mar 2024 21:46:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1711687600; x=1712292400; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=OSTo4BG6xg4Q2onv+lGORpCI7MnnKplqFj/mRoj65CU=; b=kNXAhu39r7Q8REV0ostWw7G8IMftId493m5DXxWIWYHi+WeOOFPIVNbZQEpI8UcVC0 IGEwQYoyDaz3QmDuduO0+Q7VrktUbmBoo8hxgB0T2aSsE/Fsjz3JOlLK3ZqXGWkA1stW lAFvwZWfGT3ecnhFu4D9W+EgVzOBzOy/6UQmpeqZxvoEXvOs1LHb3EtgG7+R+wILToZK eaHfV3dFt3icyjgJq3XlTqLvtMyBlljalDMOi/AKBTL+2kwO5HMmT5HQtlrrmKkcuVzJ 4w3XSELia+dkxmF9owkBCFjYUz0RBpwdyvsEDDsd+WbpwE9Jp4WXpXqOqE2Cx03Qk7N4 8l0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711687600; x=1712292400; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=OSTo4BG6xg4Q2onv+lGORpCI7MnnKplqFj/mRoj65CU=; b=s1oUBJ5NPvPEp4kI4b6Wh2zYWlOoLpOA53U6TWW3EMp5A/5+Lscc1hukvernTfmUCg rLvpu1WmlROMLv687ui0G/1nT57EA7v5wO2c5QwG4fw0Ep3jG36jIHDGeWDuacQ+dAzT 6uYHfB8Wdz+oBH8pyKHQYMKH5GOzfE/NXnkeCTlIt6urM5yUCrCRPT0gUmKO+XHRL5ib wjduGojZkxyDiMT5KQ3QT9MDYjjyBDAqKOTpXxLj9qCMRTajwLpCRHVIbz+qLKb+T8mp 7ZvG5mJPUpeBx1StZ8q8WoL/01+8g3f2k3Wg82sAmwtAytlVfBfnDtzmOpWlDQDCn7LF s+hA== X-Forwarded-Encrypted: i=1; AJvYcCWTFnjO8fIfY0Y33pPzv9EOSnV1dn3mCLK2M8OTaqmGw9DdwQPR7QVNR2ae03883qBGpxtcoQnamwpEkcp5mv1FKUA= X-Gm-Message-State: AOJu0YwCcsIwTjPglgSAmwxs262YQ4BnxHmCeR9KFhlfq4feSllMQVMk pxc8GpMqZRmYhxxEUUw5o21dTYhWw6oe10lsePrXZZKS44Jl3WWmnePFCZ/ubeg= X-Google-Smtp-Source: AGHT+IGy01J0/WrcquoydjAoH+JlQeZkJBLUoT7UPHOF1/Tfs8zhjBY9fqykQxytj7scXuJBpM8nqw== X-Received: by 2002:a05:6a20:5489:b0:1a5:6d3a:65ed with SMTP id i9-20020a056a20548900b001a56d3a65edmr2080319pzk.3.1711687600379; Thu, 28 Mar 2024 21:46:40 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id i18-20020aa78b52000000b006ea7e972947sm2217120pfd.130.2024.03.28.21.46.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 21:46:39 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com, samuel.holland@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, tech-j-ext@lists.risc-v.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh+dt@kernel.org, krzysztof.kozlowski+dt@linaro.org, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, Liam.Howlett@oracle.com, vbabka@suse.cz, lstoakes@gmail.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, hankuan.chen@sifive.com, greentime.hu@sifive.com, evan@rivosinc.com, xiao.w.wang@intel.com, charlie@rivosinc.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, dbarboza@ventanamicro.com, sameo@rivosinc.com, shikemeng@huaweicloud.com, willy@infradead.org, vincent.chen@sifive.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, gerg@kernel.org, heiko@sntech.de, bhe@redhat.com, jeeheng.sia@starfivetech.com, cyy@cyyself.name, maskray@google.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bgray@linux.ibm.com, mpe@ellerman.id.au, baruch@tkos.co.il, alx@kernel.org, david@redhat.com, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, shr@devkernel.io, deller@gmx.de, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com Subject: [PATCH v2 17/27] riscv: Implements arch agnostic shadow stack prctls Date: Thu, 28 Mar 2024 21:44:49 -0700 Message-Id: <20240329044459.3990638-18-debug@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240329044459.3990638-1-debug@rivosinc.com> References: <20240329044459.3990638-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: B4E3040004 X-Stat-Signature: fxybndyip6su7q6hkz4qkhakxfeqhc6d X-Rspam-User: X-HE-Tag: 1711687601-886572 X-HE-Meta: U2FsdGVkX19UUCSeDwvli/+bSdVnv99GjhM984RhIfO+L2X7FbPcyPU3aUg1ErcBL2NBG7OTvG4DeHIW99htm9myRdgLfADtArjzhXUsOYzA3bCkxk81c43yg2FdGqeueLsq7g0Ip4HXckm4wcNCwNTGZDZK2uS2C+P3QEPJjCW8s7AaUG+fwRidVLjIvWkoHt/IlPgs7NY6Wo1plyjM8PMblcFSlKCGS1BDC8gMumxhkTahexwLeFRp5RhXnjppuc1bIS7I7oDbgjOJfjLYy5CTypGmTr++1SfiUNgBnJ8yJTsLvDJzEM8WW2W9AdGjlVdG0ShMJu6S+OpO8qzsnBx2a370H3IqCzRmuxp1pKn7DeMF1eP8A2SlFgyMgD9vpczYGs50kLNG/QxyPdsL3TK0li7ZR+lZuswKuKdK1WGT3y6LkBBT6TqwP/rjl2wEgDCZ9NTeB0wpzIyRTVayBQV2xx2tNKhG2kYImm1YHgIh7g9xtbG/jW4+PmJQaUWA8OeUc7RVbqnDvCeZWCKO/imQp6t6+lxGe20EpnTgnOpNVV+j+xvf7slzc2WXkLmIWpxykDlr5ToTN+RF3uqB0GAFnLnVWS6HbV/YWD/ya+54o/rTcU5z28wXiiTX2ycH27+VmP/7bvgq1LGZVgm84zZPM9FGshSg7LWJ3DgPbjtBC/SNG9ujzsikKS4ZG1/XaLTAanECT00DKz3igznylQ66TR8/bLT3j/3H2YsJWlARBbEbWJxr1dTKVanScjtJ1ZGmv46uVcDVtbZRdpy5SPz6LMWxTTudWumtlW56wjCMByXmSqn2MZTGBiPdbVLs1UdQU3KuZ4vriTg4faDXOBdHdxd3/HNSKJcknrktp/v2B1bgRouJ7HIDhWlkfRlV3xP5/oqoW2cFQChSajsZVdFRVxGOoz1ASQwlyXF+5+s6fHGwtINoPLO31CT++fAR9DyFzB7XtVsJLBSVUMg 13/x207b tm5URCWmfaO11RtHWjb9X31aj1dZkMyb/TGnWvGjPAsBJkn4UPe5QsIznAGvMaJ6lTvRs02fxi5Rktpq1ycJA5XgDXaLu4sC3KizvXKJ6ppFZfHx3AVCv5xJ2LR0uqPNlSaDxwRjsaXhpVib0oGpNgBYc3GFjOmAw78wovSx/DYUO+YspqVv86cQS0odbIy+FnFZE2y6AOws6v3omVwYBb0DzH7CJAlNwBmYgvxz6rtxapY4= 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 b47574a7a8c9..a168ae0fa5d8 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 */ @@ -26,6 +28,10 @@ void shstk_release(struct task_struct *tsk); void set_shstk_base(struct task_struct *task, unsigned long shstk_addr, 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); +void set_shstk_status(struct task_struct *task, bool enable); + +#define PR_SHADOW_STACK_SUPPORTED_STATUS_MASK (PR_SHADOW_STACK_ENABLE) #else @@ -56,6 +62,16 @@ static inline bool is_shstk_enabled(struct task_struct *task) return false; } +static inline bool is_shstk_locked(struct task_struct *task) +{ + return false; +} + +static inline void set_shstk_status(struct task_struct *task, bool enable) +{ + +} + #endif /* CONFIG_RISCV_USER_CFI */ #endif /* __ASSEMBLY__ */ diff --git a/arch/riscv/kernel/process.c b/arch/riscv/kernel/process.c index 9551017d16db..d6dfef22c274 100644 --- a/arch/riscv/kernel/process.c +++ b/arch/riscv/kernel/process.c @@ -145,6 +145,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 11ef7ab925c9..cdedf1f78b3e 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_info.envcfg |= ENVCFG_SSE; + else + task->thread_info.envcfg &= ~ENVCFG_SSE; + + csr_write(CSR_ENVCFG, task->thread_info.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 @@ -268,3 +295,83 @@ void shstk_release(struct task_struct *tsk) vm_munmap(base, size); set_shstk_base(tsk, 0, 0); } + +int arch_get_shadow_stack_status(struct task_struct *t, unsigned long __user *status) +{ + unsigned long bcfi_status = 0; + + if (!cpu_supports_shadow_stack()) + return -EINVAL; + + /* this means shadow stack is enabled on the task */ + bcfi_status |= (is_shstk_enabled(t) ? PR_SHADOW_STACK_ENABLE : 0); + + return copy_to_user(status, &bcfi_status, sizeof(bcfi_status)) ? -EFAULT : 0; +} + +int arch_set_shadow_stack_status(struct task_struct *t, unsigned long status) +{ + unsigned long size = 0, addr = 0; + bool enable_shstk = false; + + if (!cpu_supports_shadow_stack()) + return -EINVAL; + + /* Reject unknown flags */ + if (status & ~PR_SHADOW_STACK_SUPPORTED_STATUS_MASK) + return -EINVAL; + + /* bcfi status is locked and further can't be modified by user */ + if (is_shstk_locked(t)) + return -EINVAL; + + enable_shstk = status & PR_SHADOW_STACK_ENABLE; + /* Request is to enable shadow stack and shadow stack is not enabled already */ + if (enable_shstk && !is_shstk_enabled(t)) { + /* shadow stack was allocated and enable request again + * no need to support such usecase and return EINVAL. + */ + if (is_shstk_allocated(t)) + return -EINVAL; + + size = calc_shstk_size(0); + addr = allocate_shadow_stack(0, size, 0, false); + if (IS_ERR_VALUE(addr)) + return -ENOMEM; + set_shstk_base(t, addr, size); + set_active_shstk(t, addr + size); + } + + /* + * If a request to disable shadow stack happens, let's go ahead and release it + * Although, if CLONE_VFORKed child did this, then in that case we will end up + * not releasing the shadow stack (because it might be needed in parent). Although + * we will disable it for VFORKed child. And if VFORKed child tries to enable again + * then in that case, it'll get entirely new shadow stack because following condition + * are true + * - shadow stack was not enabled for vforked child + * - shadow stack base was anyways pointing to 0 + * This shouldn't be a big issue because we want parent to have availability of shadow + * stack whenever VFORKed child releases resources via exit or exec but at the same + * time we want VFORKed child to break away and establish new shadow stack if it desires + * + */ + if (!enable_shstk) + shstk_release(t); + + set_shstk_status(t, enable_shstk); + return 0; +} + +int arch_lock_shadow_stack_status(struct task_struct *task, + unsigned long arg) +{ + /* If shtstk not supported or not enabled on task, nothing to lock here */ + if (!cpu_supports_shadow_stack() || + !is_shstk_enabled(task)) + return -EINVAL; + + set_shstk_lock(task); + + return 0; +} From patchwork Fri Mar 29 04:44:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13610062 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 5A38AC6FD1F for ; Fri, 29 Mar 2024 04:46:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DBFF96B00B3; Fri, 29 Mar 2024 00:46:46 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D716E6B00B4; Fri, 29 Mar 2024 00:46:46 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B9C3D6B00B5; Fri, 29 Mar 2024 00:46:46 -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 975296B00B3 for ; Fri, 29 Mar 2024 00:46:46 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 5E09E410B3 for ; Fri, 29 Mar 2024 04:46:46 +0000 (UTC) X-FDA: 81948841212.01.9CD0782 Received: from mail-oi1-f177.google.com (mail-oi1-f177.google.com [209.85.167.177]) by imf14.hostedemail.com (Postfix) with ESMTP id 7D739100005 for ; Fri, 29 Mar 2024 04:46:44 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=GesjQ7BA; dmarc=none; spf=pass (imf14.hostedemail.com: domain of debug@rivosinc.com designates 209.85.167.177 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1711687604; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=YmrHp67H+XCJtaWoTSn26HwuzF4qGVlhViFutzTbJ64=; b=HmGHOxhBXGdmvo2S40uYnaSpzA5NIVaOBbLPeu0EInpkQNiN0U9cv3GDkT48qRWtYcY8U+ jtOsgwDp4I/GK7aIZ+6GAjBInu6a3KLB6YENr+oULxJMLMuRa7JAqufr/N0UFV0eaU7C6C i96USUFzsjDVq+RjQDwp+vllUwPXqW4= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=GesjQ7BA; dmarc=none; spf=pass (imf14.hostedemail.com: domain of debug@rivosinc.com designates 209.85.167.177 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711687604; a=rsa-sha256; cv=none; b=2nVUzAifRdT/lHY5iEVlTI41G8yF2ZfTAzdnO4JNn5LJKFlBNgXebHBCplwUKse7RjhkBs 7gvhvhDBmoVSzflwKNkxMGukHwm+6t13EBKE+iPqsGnHeKA8tnDsRz2szebQSjUQa8qSiJ 78kR4dV0aQdAWgoCXb3FD9QIvlQOWzU= Received: by mail-oi1-f177.google.com with SMTP id 5614622812f47-3bd4e6a7cb0so964977b6e.3 for ; Thu, 28 Mar 2024 21:46:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1711687603; x=1712292403; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=YmrHp67H+XCJtaWoTSn26HwuzF4qGVlhViFutzTbJ64=; b=GesjQ7BAXE0Q5ygdh9rs8pVMNlimUNjKN9nkRQHxzwR0LOydiV91gpe95BbDER6tUU wzRrvz5VLC9d1BAuj6KQwSwPy/7CP8PCGQKo3TePFU5uDvTKSiWvLPdd7tinpDBNTfSH Iwr2O3OiKMh8f6Mere+UvkIHvtEFiSnjGe8oLyfDknwZbs0yB9CQ8yjV6S+qAV46uk+P WN37rBX79Ts5GGrZkbFguOMdEg/LjLfFZ134uQsgQyL9nh506UK+lUF3k27DcyVEpvoZ kbW7HFE7SXS6vpIMdSXCU3H2xL2ilf5GkL1qz81XtL4ip60f90Y/j+qRP/DYKpLfAZpV lqew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711687603; x=1712292403; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=YmrHp67H+XCJtaWoTSn26HwuzF4qGVlhViFutzTbJ64=; b=vumpUn3FJrUdv45UkLVeMaemdIhKWDuE4QRtVv8fJS/0NyCMyUS7VNOWJMC2lM1XeZ mAHApd8a8JyDgIg+3/C9+v7+kchfUPQi43mHxjWEb+pt3+7X6KqZLG2GestIfgN10R0J /OHe1kjHSNsguOtITNKeJGrEwXexe84OGY/MzaO+vbHIrSpMJQkVXNui4wUZl0NcpkWk FU4ClbfLYzrbZm5S486uEcum8htcgHvNaAxbinOwe0H1EHikO8pcw54EPjZXh28cxJP4 4T1uw1zdXD55c+Sg1oHsgCj0l3DZnRzDO4LM7VZ3JhlAuT8g0qCLa3LEbrBTTTVWXzNY 3euA== X-Forwarded-Encrypted: i=1; AJvYcCXGRgQsv9njT9gUtf9qF1jtVGMHKxV9rgmARFBuEWJ0CqK/RPjUQHOML88ZwEVMjMvyH9WnFPwqfbEE2pKQKEhOcUs= X-Gm-Message-State: AOJu0Ywrs3gWWLVONRlapE1Zf+axqANao1+UWslieYlSOaZ4f/gJqCBO SCev5/3glF+lYN/aZKtq6UHn5T3hOIhddbRQSjpJP9WYe3A5n5uUNEH3dlgVvAY= X-Google-Smtp-Source: AGHT+IGNXVgsJhuruVdNoXrAX5QvhHzDZ/ypkAvKiPh6A+vsQQkyT8+weoD2+0apPvMAl8Da1TUaIg== X-Received: by 2002:a05:6808:648f:b0:3c3:d72b:3851 with SMTP id fh15-20020a056808648f00b003c3d72b3851mr1338208oib.7.1711687603411; Thu, 28 Mar 2024 21:46:43 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id i18-20020aa78b52000000b006ea7e972947sm2217120pfd.130.2024.03.28.21.46.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 21:46:43 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com, samuel.holland@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, tech-j-ext@lists.risc-v.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh+dt@kernel.org, krzysztof.kozlowski+dt@linaro.org, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, Liam.Howlett@oracle.com, vbabka@suse.cz, lstoakes@gmail.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, hankuan.chen@sifive.com, greentime.hu@sifive.com, evan@rivosinc.com, xiao.w.wang@intel.com, charlie@rivosinc.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, dbarboza@ventanamicro.com, sameo@rivosinc.com, shikemeng@huaweicloud.com, willy@infradead.org, vincent.chen@sifive.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, gerg@kernel.org, heiko@sntech.de, bhe@redhat.com, jeeheng.sia@starfivetech.com, cyy@cyyself.name, maskray@google.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bgray@linux.ibm.com, mpe@ellerman.id.au, baruch@tkos.co.il, alx@kernel.org, david@redhat.com, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, shr@devkernel.io, deller@gmx.de, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com Subject: [PATCH v2 18/27] riscv: Implements arch argnostic indirect branch tracking prctls Date: Thu, 28 Mar 2024 21:44:50 -0700 Message-Id: <20240329044459.3990638-19-debug@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240329044459.3990638-1-debug@rivosinc.com> References: <20240329044459.3990638-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 7D739100005 X-Stat-Signature: f5ktxeduff76je5do7p6nqxyggkh6bgj X-Rspam-User: X-HE-Tag: 1711687604-539844 X-HE-Meta: U2FsdGVkX1/3akzezYAwtl6ITCqJdPTIfARy7UtL404kH6HFuSigaOKX93RDhhXWy8QaZu5aXeSGymUNYzezjd3a32MJUHqFAnVDb/D0AxDF0XTbwThP9EY5+xHXCf5tOFjt01BDEdiOYi/ljudy/7Vpp2lFcTbTudHD/jbcr0QwAM1vGGW+KanJFVwnJXXiaI/bBucz/kS9pNE157kdBdQtXYz7zautXi8EO5o897Mm3+78Tk6gB/JKGwF72pCaeZqzOnCfVvMum0V6D3Ka/r4E16A4zOX+2z5l1y214OeL4b1od2t988v6efTg3QpLOGAYOaMOhwvw7d2g4g1/5f+gvVX1uwrw/4Mr4wekdpZRVx+7LODznlZihP2MpDnLZFUyurLg8ez74+2evzHU9s7/+gsj841+6en5MInGxpDKw9jr/VuAAVwbrhI7dKuW1W1zJCYzyx3dkFNWQCBQeZUCxlBiJ/CozLVN5wzi7Kn1jHJXRFkW+TFxnYlW5CpsATBf6iEgOG2dWZIk/ilAMU8pY7tDRFSTBK7dOQw7GHCHjWKH0q+Xn71CLmRdq4KNj0IxAi20CKkOy2cRPBqHXNNsCG233RXtXdjbIuqW7Gku1ycLj2mANFFZRm3gMwKuH/GA2GJcbiUSMhY5+iNGzVp1FhM++8S0ah+ewl0XQBdjn9CfGCPxPop+8sJqXY8+0o1yNhSWLXSdnjlcWxXKJmKyaSJ0Ta3eBo/joWbStmgamKdpyQanC+vzHGvsInuOYOhKeaQ9WbHWXDDgyxnSGpJg2axzVludV+FuYCYTlbEZxIq+yXcV44IeHlBU3wr6OCeO3CmBLTYIdDRUTKY4RYMft9jiVqU0+BLSXw71hniSc6jHcPczN0SDUbn/KUsqDYSVYZJtW/cPtknceN82Vmcwk7PO5JVYlkWyE75+ydcA/HlQVK5oPASeXNljH85aBTyuqS5Cb1fEM0WVpuS zsS2zsu9 JMrC/fcZ0XfFtJb7Mn9PfntW2sqYIjXdTIUgrGSGF+hYTsWqofC8csJuzTwN52Kbv9HAH9txcg7kvCdLuvl4nveA7uHhSJGxB8q7uVX67Ycrd1ZdX+IKaXSsBnSVhvkfx26MGCL+oCOxG4iY+VfQIrwZJWL+r1qodOgoh8drgnT2zKm2feytCyImHqReBn6NQmVQI21LsjLSshYUncx46xCOhoXwCuATNIOF3S7A22HJQBFNOQW9ew/cU4ZlWhWf1p4keUJvngdSpyc2brR3sGXkQq8PE99KwVKiJhWOm7FXm8qNpBYbAG6p2TcPbIYUw3b3Nns8MNnVgoFUO2iSkoxgZOVhuUfI4XGyTLwxFfPVlhYHY7D/vqlR68yAjP6ya487f0CC75jCL/YptUgDjxNm3LwqC24ytmP9N0xKon4TZz6CnPS+LgJBDzV2Wtzf0teD5JN89i+Kkoxsjs5kh7Jw5aQ8YnHxJijsyBde0OQ16iOJa5n9ZoBTHl/6jBfyYNANu58Lng/zKdq0= 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. Signed-off-by: Deepak Gupta --- arch/riscv/include/asm/usercfi.h | 22 ++++++++- arch/riscv/kernel/process.c | 5 +++ arch/riscv/kernel/usercfi.c | 76 ++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+), 1 deletion(-) diff --git a/arch/riscv/include/asm/usercfi.h b/arch/riscv/include/asm/usercfi.h index a168ae0fa5d8..8accdc8ec164 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 */ @@ -30,6 +32,9 @@ 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); 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); #define PR_SHADOW_STACK_SUPPORTED_STATUS_MASK (PR_SHADOW_STACK_ENABLE) @@ -72,6 +77,21 @@ static inline void set_shstk_status(struct task_struct *task, bool enable) } +static inline bool is_indir_lp_enabled(struct task_struct *task) +{ + return false; +} + +static inline bool is_indir_lp_locked(struct task_struct *task) +{ + return false; +} + +static inline void set_indir_lp_status(struct task_struct *task, bool enable) +{ + +} + #endif /* CONFIG_RISCV_USER_CFI */ #endif /* __ASSEMBLY__ */ diff --git a/arch/riscv/kernel/process.c b/arch/riscv/kernel/process.c index d6dfef22c274..105ceb8880c1 100644 --- a/arch/riscv/kernel/process.c +++ b/arch/riscv/kernel/process.c @@ -152,6 +152,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 cdedf1f78b3e..13920b9d86f3 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_info.envcfg |= ENVCFG_LPE; + else + task->thread_info.envcfg &= ~ENVCFG_LPE; + + csr_write(CSR_ENVCFG, task->thread_info.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 @@ -375,3 +401,53 @@ int arch_lock_shadow_stack_status(struct task_struct *task, return 0; } + +int arch_get_indir_br_lp_status(struct task_struct *t, unsigned long __user *status) +{ + unsigned long fcfi_status = 0; + + if (!cpu_supports_indirect_br_lp_instr()) + return -EINVAL; + + /* indirect branch tracking is enabled on the task or not */ + fcfi_status |= (is_indir_lp_enabled(t) ? PR_INDIR_BR_LP_ENABLE : 0); + + return copy_to_user(status, &fcfi_status, sizeof(fcfi_status)) ? -EFAULT : 0; +} + +int arch_set_indir_br_lp_status(struct task_struct *t, unsigned long status) +{ + bool enable_indir_lp = false; + + if (!cpu_supports_indirect_br_lp_instr()) + return -EINVAL; + + /* indirect branch tracking is locked and further can't be modified by user */ + if (is_indir_lp_locked(t)) + return -EINVAL; + + /* Reject unknown flags */ + if (status & ~PR_INDIR_BR_LP_ENABLE) + return -EINVAL; + + enable_indir_lp = (status & PR_INDIR_BR_LP_ENABLE) ? true : false; + set_indir_lp_status(t, enable_indir_lp); + + return 0; +} + +int arch_lock_indir_br_lp_status(struct task_struct *task, + unsigned long arg) +{ + /* + * If indirect branch tracking is not supported or not enabled on task, + * nothing to lock here + */ + if (!cpu_supports_indirect_br_lp_instr() || + !is_indir_lp_enabled(task)) + return -EINVAL; + + set_indir_lp_lock(task); + + return 0; +} From patchwork Fri Mar 29 04:44:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13610063 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 6EB50C6FD1F for ; Fri, 29 Mar 2024 04:46:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 005556B00B5; Fri, 29 Mar 2024 00:46:50 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F1BA76B00B6; Fri, 29 Mar 2024 00:46:49 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D95EF6B00B7; Fri, 29 Mar 2024 00:46:49 -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 B788A6B00B5 for ; Fri, 29 Mar 2024 00:46:49 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 78CF816112F for ; Fri, 29 Mar 2024 04:46:49 +0000 (UTC) X-FDA: 81948841338.20.5D19A40 Received: from mail-pf1-f176.google.com (mail-pf1-f176.google.com [209.85.210.176]) by imf10.hostedemail.com (Postfix) with ESMTP id 9FA25C0002 for ; Fri, 29 Mar 2024 04:46:47 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="RWkVWN/W"; spf=pass (imf10.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=1711687607; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=+jzaUmiaVJT+7AndpgdFUVtlaA8dH1VTGpl1Yp0ZHRo=; b=iJLJLP0Mojv5OeGS6Vxpt3/NvGTK1UzabMKdbzbPEelIVhUe7vdjRT1F1bOLKeSS7jzjH3 juvdeRdeFS1wyxeFRYM81CQjM19hLACkwUIf/07mPSTH5wZg/lFtgAuGoNkHqoeZWoc5Xc EiUPdHqj6k/+H9qM8ozfjZTEZzI61LU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711687607; a=rsa-sha256; cv=none; b=ylcBLRt4xxHTpBsKwz9ZLvmfHULmKJ5EpSVYHWZXwYC7j72su9trJ0hHddJ+5xtrz+9DXq EVfiQq8A3JTetQe6hNCpVbCyjPKVKmGq8Mc0h0ZugOqq20ALc1xSekzEN/SJwp6pb7PgQX TTNXNwzsV6A0/krEhv/9tIgdqwcp4Cs= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="RWkVWN/W"; spf=pass (imf10.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.176 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pf1-f176.google.com with SMTP id d2e1a72fcca58-6e6b22af648so2012832b3a.0 for ; Thu, 28 Mar 2024 21:46:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1711687606; x=1712292406; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=+jzaUmiaVJT+7AndpgdFUVtlaA8dH1VTGpl1Yp0ZHRo=; b=RWkVWN/WhsVsyHIHXSFDMKXoEYULcutU8ONHo4VHu2o/zbYiZm3sWqn+EDwcQrzRGY d8iyMysXA+SojKYqEI2aT3p5StV4/3qolM4ctX6zkJ2mTHJZF0ET+NisKNmyofqEwBYF QYJ3mJozeDQQiXdMiEGzdrRDRudR4pvErZpUPeuNHueJjiG6XMmy1PEQNHIxUbeTxVyM WcB+Gc9NxWjEH2J+85cIYPNd4K2aqZ8PJOCrPRnR2VQzOC7AGYrgtmOyqs0huDD8z2VG RkCsGCPWtPW4bdNyeXZFMcWF32iEYZdGe3UnC1MkCd4AYj/pSMd/VD9jKwyMMy+ONtjt YdsA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711687606; x=1712292406; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=+jzaUmiaVJT+7AndpgdFUVtlaA8dH1VTGpl1Yp0ZHRo=; b=SMCFYeBH+1FPek6BZAqVVQBjGb/NXbrxhgFlaXSEjZhEWaCm9Lw/FUtvyC9DMbop/d uG/IzqiFEGJkdXkFRLzc+ZlQKZNw2p8v9MYVMEQb/qUJ++1V+QAuBSixDOmJbBfJwNV5 A5iWZmno0TSQOui7PpbLsiaTt55rolJruOoWPeqzW49ra2S82mrmGX5Fzg7sqN+J+Toi Y/x8P2iJLfuPVjudkbPvHsc7REdirVPOj7AnhKwjNdoeruFTzsvvo/jyvvyXSGBGDign 1r53mhfOF1gzDRv+VxecwCEF2luNZVR4MG1snrR57LF1fNKsjNNU8VtdafNI2k9k6Lar Rk+w== X-Forwarded-Encrypted: i=1; AJvYcCV6g03vRNt7eiWl5dOwnF39bIHmP1XcL0/SWqVL4LhNiL+FBYV5S6yDmOZP8icSA/75m492Tm6RxhDTbHiKYYLDq+w= X-Gm-Message-State: AOJu0Yzc+/v/jyb6DKHfp4txXOgZu3WKLXYGzlrf+aQ/DxkOt46M17Gr Kyy+Rnk7CVXA5eyqv30dcmwLu4Ogf4H7hgct7tDh3KTWtro3AuU4aReKPX5IuLk= X-Google-Smtp-Source: AGHT+IFw+iNT/fZ7ZDIngivJRJR8RkOVDCFWMSifsiSz96HEhcDTVYzJoolJ7eKeIz36X1Q6OzEzmg== X-Received: by 2002:a05:6a20:8414:b0:1a5:6c7a:a60b with SMTP id c20-20020a056a20841400b001a56c7aa60bmr1845203pzd.11.1711687606505; Thu, 28 Mar 2024 21:46:46 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id i18-20020aa78b52000000b006ea7e972947sm2217120pfd.130.2024.03.28.21.46.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 21:46:46 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com, samuel.holland@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, tech-j-ext@lists.risc-v.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh+dt@kernel.org, krzysztof.kozlowski+dt@linaro.org, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, Liam.Howlett@oracle.com, vbabka@suse.cz, lstoakes@gmail.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, hankuan.chen@sifive.com, greentime.hu@sifive.com, evan@rivosinc.com, xiao.w.wang@intel.com, charlie@rivosinc.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, dbarboza@ventanamicro.com, sameo@rivosinc.com, shikemeng@huaweicloud.com, willy@infradead.org, vincent.chen@sifive.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, gerg@kernel.org, heiko@sntech.de, bhe@redhat.com, jeeheng.sia@starfivetech.com, cyy@cyyself.name, maskray@google.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bgray@linux.ibm.com, mpe@ellerman.id.au, baruch@tkos.co.il, alx@kernel.org, david@redhat.com, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, shr@devkernel.io, deller@gmx.de, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com Subject: [PATCH v2 19/27] riscv/kernel: update __show_regs to print shadow stack register Date: Thu, 28 Mar 2024 21:44:51 -0700 Message-Id: <20240329044459.3990638-20-debug@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240329044459.3990638-1-debug@rivosinc.com> References: <20240329044459.3990638-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 9FA25C0002 X-Rspam-User: X-Stat-Signature: rngdqf9oofpbrbj5sos6q69y79mdk899 X-Rspamd-Server: rspam03 X-HE-Tag: 1711687607-474861 X-HE-Meta: U2FsdGVkX1+KMxnaBSjOQmD42URGxkSfeYXq/jMn0kqRiyLxyYTeWPeqUJF/0/AZn1+R7aXH6HzQsV3JEDxh0y8tb1Aqyq5exuau2lBNI+XuAIXasNdjZPVSlN2g8AQdNNEJaGeaHBKeVMXzGzh7cTAstOsKuxpJMpLJnP7RldEsDjRmFy83t4Thd8U9CH7Um3Dc59IA+Kw4zZKcs+3/q6xGqaWMCff6m7H1OEtWb/3/MK1BYquHxdoG6khfugpOKY0i8mECr/HkveMiwyAN5IQghGlyDaKcl543kjpjU6iOXznniNodkWCxmfY6PjUDkuXqWVISiF8Iy1YWmuBuzS+qBL1w+NJzAwgFnSdz/xeLE5mwLk5o4wqwmH2N7c/p9w8qMwtrPtsL2ThmyKTTsLHvFtdvv5B8feaoYjZyw2/yVzoXJgbmP/S6nh/S1tbqCgwq94EPocZt20Yi5+M92WazYNWfcbUbBwC9YftmfNlgv2TGYfuUz6Z5nDGW3JFkMWO9Th8PGUqBZ2E5SfS1JB45KvBrl7LvhxQiW6t2/Nlg2Upj4Wl4JwOLP2ANOxlvntwZKZLlxx4S1uEK1l6TLrjWIkq34qbLvtzyJZIVrMb0GtM9MO32nNOwLwl/T3Lr98q5fjGDm33XXdlXKaW22GhDndV5t97jpa68fzfK52hHdtwe3fmVYDhS+f2NEBe1OZ8tArzLoCDiCpLpPGJy9gSkB7MlfBfgA/RcPuDAmw3z7s07wJbM/KywN7KvgOzZ/398taKR25DmIlw6AWVk0OhKvmVrDfYh7XwF+Oa7ZUxXgWJqsrfwDja+CzOaalCgNDkUHM2o/vdExv2jx1BMLrMSijB29tmr3o78tElhujvg6sBAaX8WAgJLloczr4eY/FfpkLpUmw9pMpAa2YMQD0L1oFTikDxYuR3j21A0XnpwN6nM4F/4mxyrmPnAuFXyyIJ0pSgUZX9SmHb/vQn +PPhqBwQ UETFlFbyvPsMBDUFOq2ne2jPfYWmtsntcFinxp1m4EP4ZswLDgxXAqE/0xjP/82+SPaGhkrGbr5WDi10BXJMd5iNVgXakDYyakLIzeedW71TA5neRAOL1l6pXfRLZafewbL3II9vmZX4QqR5Zpe7Nftp1yD3oqnsxg7nhvPYPUrxLoau0x6bEN3Bzheiz5fFABu6c/555jniBIxb/Q+aboWfY5aAiPJW2Oi9Sn5oE5nj1aKxRwH0N95hOQBetIR2cQyB8eh/WGRZR6vo0p638eW1Sl/3ZSekyo7xQu5mKEGSRjWdIKxxFvZzFMUwY8DieofRc4GGrIoyg++IFSUycNCjWkZ8624TCmfkrL/a5Q+1Nqbh1bJvUFH1E3Ee5yL1t7nN6fJxBDQ2a3dClukLBc/IjViWxaM7xibrkqAaGjw1CJMIwDik+Fm3xfl7fp9IMVHDrTsUkU/g+xdBC3cWNZF7zzbCaPjxTKEDNJXdYJ4aDFEUw1fXm9NXtY5hTzFgUeXqt9ZSICjeUwa8H7XWc8wcmcZb8vc1+NMYNlS3l1klHmF/fnoO6vfwQ/z+c79LJqX/g 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 --- 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 105ceb8880c1..97c812753c9c 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 Fri Mar 29 04:44:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13610064 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 5FAC6C6FD1F for ; Fri, 29 Mar 2024 04:46:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E32FB6B00B8; Fri, 29 Mar 2024 00:46:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E0B056B00B9; Fri, 29 Mar 2024 00:46:53 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C5E136B00BA; Fri, 29 Mar 2024 00:46:53 -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 9EA756B00B8 for ; Fri, 29 Mar 2024 00:46:53 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 71E35A0504 for ; Fri, 29 Mar 2024 04:46:53 +0000 (UTC) X-FDA: 81948841506.12.BAE06CC Received: from mail-oi1-f182.google.com (mail-oi1-f182.google.com [209.85.167.182]) by imf26.hostedemail.com (Postfix) with ESMTP id A1217140009 for ; Fri, 29 Mar 2024 04:46:51 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=VE3Aqg3f; dmarc=none; spf=pass (imf26.hostedemail.com: domain of debug@rivosinc.com designates 209.85.167.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=1711687611; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=YO0iTqSJGMZjgI8FFL6Efs68zyEh3di0+Ee8Co7Xl3I=; b=L7AWEtHYfG/2ujEvK3nLFEYmtxqdkGhiK4nBiB1DUjCNYZ6MDMAQyi8sv/DvAjkJZYZQ9+ hs0rxGUjNO3Xlft3VNS0U3WMoBi/g4U9POV3c5w9P8kNoycnVUqvczDXlTvsSpuKFiFiBM Jpd1rKNm6IrtH5DCZXUNcbpjCCB+3QI= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=VE3Aqg3f; dmarc=none; spf=pass (imf26.hostedemail.com: domain of debug@rivosinc.com designates 209.85.167.182 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711687611; a=rsa-sha256; cv=none; b=JWpSALa6HU+XwJCLYSlAJpzMuEmtkw34Ukak/GfUIJMntPERNDZh+pamZNCjtJcAWXWy6g 8EU5bFHhWz/9KmmODgyl+oqBn7vABGVDAQQoNqlcR6MKHq4suvq9iOj2LQZgyAcsFIJPdf HjNCj8J8sS1j3h9LfgClE3dymYPnwns= Received: by mail-oi1-f182.google.com with SMTP id 5614622812f47-3c36ecdb8cdso778611b6e.1 for ; Thu, 28 Mar 2024 21:46:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1711687610; x=1712292410; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=YO0iTqSJGMZjgI8FFL6Efs68zyEh3di0+Ee8Co7Xl3I=; b=VE3Aqg3fTugZ1wWcTpmhlww+mfLiOGBHuY66eDsG+jfzpVl5pAAQCwYg9iFKgdjmVZ uYegAB8dLCMcI1qG+DTwFp5/0wPlS+0/P7lNPddwOeIFPKiyu/Z47/Csd36/gCo/1cUO Ve4k9mvhQ9YClhSofa8NNlbUaCK3UJCGdasBVm7nCihbgD3mLNwdKfliHUgHxhB9zk35 w8n9LgIMWPhxmv+R2iIFsEDivIiswedyRzo77Dlh7sWhcnLybbxW9JmaXArKb15wD99H V/L/I/pfVEUav6e2yY0j8Yjpr8Ea4uhMq30yNWiteuuPkumu1f0iKwO6euqdKdiyBVSH hyog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711687610; x=1712292410; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=YO0iTqSJGMZjgI8FFL6Efs68zyEh3di0+Ee8Co7Xl3I=; b=VHfsCIZffs/vgpJ5AruuJP20lKpauH48/LoEsKnZe9ER6js3Y/WPS+btjheI666mPY /L+JwvUbIU6AQgqkXYW4yY0DstkBaIKAJaXrYPMhCTtya9zq97Q6RXfX6n4+fmVF/+dS TZK3zeUC/LBGBBP6fWwkPN7ExkRioNPldThIls9opH7JBDjFkP8cVdHvMl9y+KT+QYD3 PGZhxbyZ/7JUsTvI3r7rJEqqFhU4mEf247yJhtyIwha2HLeGAbhk99kpI0gMNF2RO6+N v0kS2s1Xpb2VEYPCZ+Dkpr64YYw374iHFzS3Mv6RziXewM3puJy6K39J/nw7TlNOscBY Kgpg== X-Forwarded-Encrypted: i=1; AJvYcCUJGYDNojRvkU5T55/7RRE0/z1+AlGBVCS/l7oRKhxtTGMpbOafPr0JYqxjuk+cLc5s11CAfCU14k6wi4E6DQZZE1o= X-Gm-Message-State: AOJu0YzpOFohLC4GJiUIEBG+4zCiNFmURoVoMF+mW6HcD726Qf5oXGZN /TkZe95puoKsU8x71XuPaM9Nt/PdvUs2alNpiCa4ziWXOxIQxf9kjIBxdOI29+k= X-Google-Smtp-Source: AGHT+IEEkqqoh9ZCSipkYJyi1sYiOeAupA2Zd/Tw6FmqvNWOmZwwVKoooYj2QwBiFKxvGUt1R6r40A== X-Received: by 2002:a05:6808:1885:b0:3c3:e436:881f with SMTP id bi5-20020a056808188500b003c3e436881fmr1602543oib.36.1711687610580; Thu, 28 Mar 2024 21:46:50 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id i18-20020aa78b52000000b006ea7e972947sm2217120pfd.130.2024.03.28.21.46.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 21:46:49 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com, samuel.holland@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, tech-j-ext@lists.risc-v.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh+dt@kernel.org, krzysztof.kozlowski+dt@linaro.org, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, Liam.Howlett@oracle.com, vbabka@suse.cz, lstoakes@gmail.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, hankuan.chen@sifive.com, greentime.hu@sifive.com, evan@rivosinc.com, xiao.w.wang@intel.com, charlie@rivosinc.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, dbarboza@ventanamicro.com, sameo@rivosinc.com, shikemeng@huaweicloud.com, willy@infradead.org, vincent.chen@sifive.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, gerg@kernel.org, heiko@sntech.de, bhe@redhat.com, jeeheng.sia@starfivetech.com, cyy@cyyself.name, maskray@google.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bgray@linux.ibm.com, mpe@ellerman.id.au, baruch@tkos.co.il, alx@kernel.org, david@redhat.com, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, shr@devkernel.io, deller@gmx.de, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com Subject: [PATCH v2 20/27] riscv/traps: Introduce software check exception Date: Thu, 28 Mar 2024 21:44:52 -0700 Message-Id: <20240329044459.3990638-21-debug@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240329044459.3990638-1-debug@rivosinc.com> References: <20240329044459.3990638-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: A1217140009 X-Stat-Signature: 3yzs3ythz945tdfnink7j4x3yftfgf9o X-Rspam-User: X-HE-Tag: 1711687611-695807 X-HE-Meta: U2FsdGVkX1/3eyjW+z9DJ38giiim5f6ZV0nR5FGY2S4sM2CwoeM90n2C+5ZoIwU4bmwVFG7jmHxwko1jo/qmuQv/FSRKD9yOg+QNHSmNW5ty/Vfl6VGOiBt+Js8C0RIqLqkkMZHACmgqJztYeM+1ZIjQ6IFhEGuubgi7bqgeIIZvkdUgiHBG9lHRxDlMaUXd8mjHT9FnZqSnTylkx+j1W+fUKOina+0APwZ74fw/CBFIY2f6FHRpr8q0QVrzzp4or+u/cW1PPJk8LnEO60JNAdSYDY7WmE+ji8+R5a19fhledQ4W7u6EuD+KG79tEl128MCXuXyt39/H/6CFaojz1GwoQ47OLtxg8r6sjdgS4wKmdLpo438wUV+aHuAPqylQ/bba34BXWG8Rn3XG7P1DyWtsVbV1K/mWdIJFlXeUtxNiEkqdz5LUHdB//YWk0V00ne6vmLql+0PP5gV63grrUH9QqSjDUnE5QA1MFDcGglOJsDHau5Tv2OBYTJHtFCo3LA5tbVuAvTpG+4E5Mzv6OedeHWNBHuNTi5rktW11gF4IFkUgXhN+6SGP93AFXCbabY6rx3Uc9Qte7BfTNjeENjow+Db7obFsiZZ4No9yYLQ9joxxTSNFVEJhuowXdhXDx1Av0X/dnKG52rK+2CdTsZV8uyBfWi5K5SptzqzIBzHtwOUiK6/FnBUcsnHkUSvgabcEUJq0arHFaC25PvKyDB7qfEcdt3SVL7hnypiF9wQNFwGSXFPY6fjF+tUGp6mJ5DDqUwZ4N3jzh0VNFx6Jtnf0lrfsqs3BrJxm5SjgEibND0uPYUdrDWYu2siy2fgL4Qjw6DT7FjL9miYVRZsFg3u6IfDZG7EHv50kqdKdrAPLBE9a4CzXLT0y1MPw7AMuwimyV5q9NcqjA+iOgJGOBK/Dtv389As9GmQRE963ZEXL2PAyTMF57raSO5k/KJVCRNvIUqwvaZJeM4qmxRB jXdbXFZx AfgdQGkhIH++3ojr5XkyUVDHnDp2TfuPhic9nJYUVZYAuuXUYTD3gBlRsmLL0b9BCFRfSgoCXKOL2nppzuJzsFX1Ed1U+tNJIyaCoU3x95YkZXJ3Tsmo+7pJmxfMwZH2u1AwmLZfKxXT0cgZA2fvJNNX/UolpsDAEzMSSOdPWMThy35eYVURfg7j28dS2RqUzGO9IHTUPgi289/Y737l8tsV/wzbARI/HEgbujyhkjJsQJadiJ+ck1PbL3xUVHtjShLxHMKEt8j+5vK3zTciEOVIzQ7azXa7qgDaTvareM8WI0jPUjE4ueYOAuTeYYZmQhowxxFf+MlcOiSgE4LYTZYg5UElLkQ2WuraP5ZnE0n9n11FDzgpsq6FrVIjHaZMWJaa20aJS+LBiz6QOFsKmbV6axziPQk+NmKvznNl1v8OaVdusBQAE4ws2Tl7PiPHak9CrSXCJHcmgl1T3LZNozw5ojFVsez24jCs7saG8J74DgQs0mgM0QTm8Lk+UVI8aJvKJfT3cinrIe00= 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/kernel/entry.S | 3 ++ arch/riscv/kernel/traps.c | 38 +++++++++++++++++++++++++ 3 files changed, 42 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/kernel/entry.S b/arch/riscv/kernel/entry.S index 7245a0ea25c1..f97af4ff5237 100644 --- a/arch/riscv/kernel/entry.S +++ b/arch/riscv/kernel/entry.S @@ -374,6 +374,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 a1b9be3c4332..9fba263428a1 100644 --- a/arch/riscv/kernel/traps.c +++ b/arch/riscv/kernel/traps.c @@ -339,6 +339,44 @@ asmlinkage __visible __trap_section 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)) { + /* not a cfi violation, then merge into flow of unknown trap handler */ + if (!handle_user_cfi_violation(regs)) + do_trap_unknown(regs); + } else { + /* sw check exception coming from kernel is a bug in kernel */ + die(regs, "Kernel BUG"); + } +} + #ifdef CONFIG_MMU asmlinkage __visible noinstr void do_page_fault(struct pt_regs *regs) { From patchwork Fri Mar 29 04:44:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13610065 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 69C27CD1283 for ; Fri, 29 Mar 2024 04:46:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E83206B00BA; Fri, 29 Mar 2024 00:46:56 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E31D46B00BC; Fri, 29 Mar 2024 00:46:56 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C5DDD6B00BB; Fri, 29 Mar 2024 00:46:56 -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 A187F6B00B9 for ; Fri, 29 Mar 2024 00:46:56 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 66198410B3 for ; Fri, 29 Mar 2024 04:46:56 +0000 (UTC) X-FDA: 81948841632.14.F902BB5 Received: from mail-pf1-f176.google.com (mail-pf1-f176.google.com [209.85.210.176]) by imf23.hostedemail.com (Postfix) with ESMTP id 94E75140012 for ; Fri, 29 Mar 2024 04:46:54 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=NfQz715x; spf=pass (imf23.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=1711687614; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Qc6tDQVZMFWZI8yrez6t0wejTQ8xJVxgJAnHf9JsPCA=; b=Cq1CSuTaMxvWM87Thd+u9NfRT381xElf57qxmCs9O/Jpy/UUr3DgqCvV77OB5vOw+t6ct5 ryYP5/iHE8l+xMaZJRgaYDUbPY8reCPyoucqHRxjIi2xQU1scc+puy4elcY12HsxIdcU8d LytZtvnHk/NfpB0HKJS2jTSoU5by0sg= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711687614; a=rsa-sha256; cv=none; b=Jr8ZPzxD4Axccbq/M/EPy2a8MKdwgnZ/bbbjTJJjYcpmsXmBipV+fZHMAZ1VeQEoejOkr5 Fo+O8GQA9MbkjcoaqiRoCNXEt3WMyNJppqjuo8cgcU7zB2P0jOIbvgs7cJ2ELVO4Zjejhw 1XWRFaeEkAAXvQ/WuQnbtD5mXloOQyk= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=NfQz715x; spf=pass (imf23.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.176 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pf1-f176.google.com with SMTP id d2e1a72fcca58-6e73e8bdea2so1540786b3a.0 for ; Thu, 28 Mar 2024 21:46:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1711687613; x=1712292413; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Qc6tDQVZMFWZI8yrez6t0wejTQ8xJVxgJAnHf9JsPCA=; b=NfQz715x7U7Z36kOeZJdfAfv4gZ4Wg0b4LBLbLdE6YtY5NGas0w5t529qJthsphsc5 u/ovrHtzHxfZ5NOFPe01ToxgmndAMdOr8qrJvnFz2xJamU39++Ojj0nMNaxBrB2xR80o U44USr6FsdFG+HKW0uQglEY55r42tlqDcnl+2mpSvl7c0B5vCj7AGdVozhD73U8opmk2 847U+zSCs6/EJT20zWcP6XPKjICULNbHZrWE6IOVP6S7CkZoNddLTbrgGiBD8A4v9RZs DG5LOHatsm9vxMVgpqAvXN9qKdikqa8pjaROy4a/hrDjCmSi07ZsshppCXKNEjRD03qF N2Kw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711687613; x=1712292413; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Qc6tDQVZMFWZI8yrez6t0wejTQ8xJVxgJAnHf9JsPCA=; b=FsGgkMqE+YkRq32AgUo3bhp+d57LxqQZr02WDd5+C4c2k4BcA5vPJhBEWzoSLFewk+ EgWgVf0LXxSVxfH30/ae+3uiiGSUXQIAtUKOKt9qUy9LIcle3WjZNsLiHLWHxMKp8ntx KQz4dgK2msXVvQXcAfu9KP/gNwJOTq+c/Gb7Op6slfXOkv7bWeYxZBMk9CP8VjqPjBh/ 95Gquu3l1KlfM2rSL2UGtDtiy1qaXp0yhlqRXkUPxhOxA1hRcmX2+1qgaHexlGMugT8U EyxMIZLiaaoDVdceuirGT2U3OLxXjPe49GW9l1vXqgdjZZN0uDY++NrjK117qiSdJ2q4 YegQ== X-Forwarded-Encrypted: i=1; AJvYcCX+9eh0i0cyCJMU8BvtuddV3M1R0w1XyM19HRXGRlKBYjNc0e/NMqXI4FWHMH2NB98dydVtuRaLckRMFrs/EKbY/jM= X-Gm-Message-State: AOJu0YxD823WYWEHIdKxSbfDMPnrNNsrJWNHQYpGS/DEGIqCKcHeWNEN hotgk7z0SqWEhvqzQeNozc7XLG+7FB41grTfHawhyTYDkLaE9eoB9BqBCTQ1B4M= X-Google-Smtp-Source: AGHT+IFBk57ft/Dc+APgv9AeEcPOk/3EIqo0N/RGN7QySteb+aQBmtUdNC7Hb3LMWHIl2juUfBH7Ig== X-Received: by 2002:a05:6a21:151b:b0:1a3:dc13:9146 with SMTP id nq27-20020a056a21151b00b001a3dc139146mr1392928pzb.8.1711687613465; Thu, 28 Mar 2024 21:46:53 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id i18-20020aa78b52000000b006ea7e972947sm2217120pfd.130.2024.03.28.21.46.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 21:46:53 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com, samuel.holland@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, tech-j-ext@lists.risc-v.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh+dt@kernel.org, krzysztof.kozlowski+dt@linaro.org, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, Liam.Howlett@oracle.com, vbabka@suse.cz, lstoakes@gmail.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, hankuan.chen@sifive.com, greentime.hu@sifive.com, evan@rivosinc.com, xiao.w.wang@intel.com, charlie@rivosinc.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, dbarboza@ventanamicro.com, sameo@rivosinc.com, shikemeng@huaweicloud.com, willy@infradead.org, vincent.chen@sifive.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, gerg@kernel.org, heiko@sntech.de, bhe@redhat.com, jeeheng.sia@starfivetech.com, cyy@cyyself.name, maskray@google.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bgray@linux.ibm.com, mpe@ellerman.id.au, baruch@tkos.co.il, alx@kernel.org, david@redhat.com, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, shr@devkernel.io, deller@gmx.de, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com Subject: [PATCH v2 21/27] riscv sigcontext: adding cfi state field in sigcontext Date: Thu, 28 Mar 2024 21:44:53 -0700 Message-Id: <20240329044459.3990638-22-debug@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240329044459.3990638-1-debug@rivosinc.com> References: <20240329044459.3990638-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 94E75140012 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: tascu7mjzaa48tqo8gbd8gk4e1st3qsg X-HE-Tag: 1711687614-665975 X-HE-Meta: U2FsdGVkX189DoNgjkO0DdQaKDZdvKXI8fF2XaHPvacmhJp6AJqkFTgR5OHwaBjsUGjE3Jjl/RfBslNOr7Zfp42DpC66UjC5wAyJbuA3Yg8nQpcaxdIGF5K7H16bKpBe+RVVe8lz+peKzrXoeVw+aZLB12di8Zb8pbL5veCkppXCy4saUE+K++BDwJ+VnMBx8Et7Xs9yWeOzbTy9XtL5R/J9hBgW9f5ApdJwrmJGyKr0VRo9OMj97Jhv3K3b7UX7Ir6A4qKNhqfQea01GcdXmdkUApogtvzQqk/clbE2/XLlNO5lT6oTTBnU8bgeAxl4PXhOdSjwm8wB01aO2BzdgdKuhMyI1fnhFWBUtea9thLah+Twm1DLgxGpMriI4dV33cTvjXbjzraXlH49nA3AjfEv04osL/Hn3xy2g/qfnYhezcnODLo/HLQYM8obKLDK4YWhqyhPRUNIsFUhr5pvjKFWhk4gQAI/H5L4Y7JnKb3BK/B/XlyIKuarhfTGLmpYVSyHSMtt/S6so0f99nqjoSmBidNs1XFfuNaSXYxtcaTaAh3EwBEySVmBoVDGSCp7DUWZJi0q8bSyBh+Drgna6lnGetDo48scr7fLIQNDhCGn6nzXRwbngjOFtR+Yq4vdFCne2XKM/YLtUdrYhIWw7cwM4mWQSBmNIGBJw+pZ8jB9Ny38+zgTAH2rVCVEWNX4oH2YJmSNr/h4GIN3aL39Np0rFPGGgnT8XiWhmVcG+FPp2Qe/YqBmxme9nqs9LKiVLKxfr2VZbd7OVCE7hqLMQUbv56t/y5llpfgcazVZ52bIwo762FtgUoQoyd0v7LAk+eYbkUznVeq+M7/KyTfpxZmpJuC73pXfpEnZLWJv3AgqkrF6GJ4RBWX8KteesCd76yqklhiFC0EEMSBVKo7V0icIiJXuuPAWRlQme836ywJHSxhS4YvFCu9z5T/k/SJ9vXSMy44CV5mbrcBlbqL 7DA3hLHy dZpSdt79qgdhQHpFH6llbJrVUWOYbCmL+IFOwnOMq9pEKkLi3HyEoMfEZ/XjfKDOWuuiria6vtTbCQbX1zTOZw5arx23zuFc2/suxlPedB5oZnOO7o8eO9uSF5UvzSe4hocmAATQmHKom/vB30Z0m+WOGODdJqDfHHlnfLvfZIJBSibapqltkNPGeY6CfwQzNTEzfd1QD/B/Lba9SRwHUzIVB4XdgiPn4FCTItSC043hmEOBvhCiaMtzYOQDNLJzO45x6U4RMPrqBTKq1dJicumotZX/L7Gj0jPFKTNmO7AovAVyCQIPdzQXeb222/ghIdBftLC7j5S58NpZ1fHOdxeK2eFNCWZwhQreGonpUq6hRZma/aDigaT5ntEyGcy6kNmT/QJYtnFSvAVi6aeqQP5ST1aUY0TBjF1wxvNeZ/xbHbpuJDmYyQTuoJ7D/v8XphzRqS3Oe1xl0lbhMZfHOmyWVXClTTIcu28Um/ufPZYE8NUb5FiptbkUsmqviQ8K7K3sLgOGl+pMrmlg= 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: Shadow stack needs to be saved and restored on signal delivery and signal return. sigcontext embedded in ucontext is extendible. Adding cfi state in there which can be used to save cfi state before signal delivery and restore cfi state on sigreturn Signed-off-by: Deepak Gupta --- arch/riscv/include/uapi/asm/sigcontext.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/arch/riscv/include/uapi/asm/sigcontext.h b/arch/riscv/include/uapi/asm/sigcontext.h index cd4f175dc837..5ccdd94a0855 100644 --- a/arch/riscv/include/uapi/asm/sigcontext.h +++ b/arch/riscv/include/uapi/asm/sigcontext.h @@ -21,6 +21,10 @@ struct __sc_riscv_v_state { struct __riscv_v_ext_state v_state; } __attribute__((aligned(16))); +struct __sc_riscv_cfi_state { + unsigned long ss_ptr; /* shadow stack pointer */ + unsigned long rsvd; /* keeping another word reserved in case we need it */ +}; /* * Signal context structure * @@ -29,6 +33,7 @@ struct __sc_riscv_v_state { */ struct sigcontext { struct user_regs_struct sc_regs; + struct __sc_riscv_cfi_state sc_cfi_state; union { union __riscv_fp_state sc_fpregs; struct __riscv_extra_ext_header sc_extdesc; From patchwork Fri Mar 29 04:44:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13610066 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 47EC7CD1283 for ; Fri, 29 Mar 2024 04:47:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C86666B0087; Fri, 29 Mar 2024 00:46:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C0EAF6B0089; Fri, 29 Mar 2024 00:46:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A14026B00BB; Fri, 29 Mar 2024 00:46:59 -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 7CEFC6B0087 for ; Fri, 29 Mar 2024 00:46:59 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 452B21A101F for ; Fri, 29 Mar 2024 04:46:59 +0000 (UTC) X-FDA: 81948841758.08.B884D82 Received: from mail-yb1-f174.google.com (mail-yb1-f174.google.com [209.85.219.174]) by imf30.hostedemail.com (Postfix) with ESMTP id 7358580002 for ; Fri, 29 Mar 2024 04:46:57 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=jedL57bb; spf=pass (imf30.hostedemail.com: domain of debug@rivosinc.com designates 209.85.219.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=1711687617; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=8xPSsLde4QcON9OUri1oh5/a4S8lr1+b1lPRkRykHsg=; b=E69jzwPoSjhda0oVJ5qKY6mLn8BM1NNeIHU6Haynt8h9sKy6zCgP7khRPDilUw2DLnRTWY jB4xxf2RR3+0mHZaQiB0OC/uP+bx0lxypwxA6tSjqSlYUyqHEg0rXb3lVUmfdu8Dz9vSVF /NQ/0UKo5l72vyMradOP5x8DOSeKk3c= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711687617; a=rsa-sha256; cv=none; b=c3J3W6/2c4x6Ly2spFfaU31E24zQlcU6FwYAjkujUO9UoWRJr+qdf/PTKhY9ZkV7rYeOy/ br1ZyCkICosbJFOutz1AUix0niO9xrDJEt/VquF1U0WWAyFOWOmdmypS9BRelke+7DCwZB ujmLUhbHtncnBSk5wpkWmeTLmGojpXk= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=jedL57bb; spf=pass (imf30.hostedemail.com: domain of debug@rivosinc.com designates 209.85.219.174 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-yb1-f174.google.com with SMTP id 3f1490d57ef6-dcc84ae94c1so1665595276.1 for ; Thu, 28 Mar 2024 21:46:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1711687616; x=1712292416; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=8xPSsLde4QcON9OUri1oh5/a4S8lr1+b1lPRkRykHsg=; b=jedL57bbnAi+iqPRJGW02upyUvjxjA3tDa3vu9PxttzdO27b2SHRe+mn5ju5IknRix Tcd5ok3Dj/pNAv4SUbk8I/6xJNXjuK4mVSH1+KsoG6QW+A8UxLIjAmaw/ICwxaH+Fxsw lMuRiTPznFnfo1FuQ5jJyByqPviwVliBOgXv+fSk1Y7vq75zwTGq7z1xYPsjco7Bb+9q aQeLjexgafDBXK542zj0hacpAgijGZeD2h7tzHvbNt6et7nYMUGOa5L6HL1CBZUURRh5 F3eDZlG1N5jUoIEvBvyF0be6hWgjOgVm/ZdfxyPhw4mGGoHRc1fEhtP4HWN0CY93qdLC zboA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711687616; x=1712292416; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=8xPSsLde4QcON9OUri1oh5/a4S8lr1+b1lPRkRykHsg=; b=OksHhMRH/qiRRRXeLhnCT0Wm3FMrCOfQtA78Kv6GESFWViD1CY6TemlZ938fIB5LIk 2YGiGj/vn6mff5UC+IPxXM2CwFnT9tNbV7BeeuhxKpUwWW0ZdXtD5FKGSCUkWx2LpdTF IepeiqU0kM0olk/HNsjwCMgaQwaWERfBy0LAf0asFYB5ore00PC6/VaQtnLyjWapyY9X menRZwpr4+4byleg+I/rf9M+cQ0EegjPfBbs4SpSs8Jl+84QBESmF4WvLxY42WF0rRqj TtR8WRUZRMKTGkkJbEaVhNDPLI32PP6wbZPl+ukAKkPWRx819FCPHSngczdjbdDxofgp 3XIA== X-Forwarded-Encrypted: i=1; AJvYcCWZbH0DLeMMYwbsPpwUr40FbaTgerK9sYWa+vq/w4qojW+fH33JlykoE5MVAYZmHLYovctHE+bbPA05FaHqvd5IH6Q= X-Gm-Message-State: AOJu0Yx/1WryLZZ3vRbQpGWkndx1xUhR9kTO9Apb8yDwOV5JiG2dGP02 XU4YdXXuxeT+1eoUIE9OdG63gyLO22EmV4N5OBQkzCzjJRKfd4VozYL2irBBSy0= X-Google-Smtp-Source: AGHT+IG37uJ/8ULWQEDE9K/aNOWfaGhUBUQD9mzyCMKEi7OurcbLU8SW7cmlTnAwB08yd2tdRDG0ag== X-Received: by 2002:a25:3dc4:0:b0:dc6:b088:e742 with SMTP id k187-20020a253dc4000000b00dc6b088e742mr1330005yba.8.1711687616422; Thu, 28 Mar 2024 21:46:56 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id i18-20020aa78b52000000b006ea7e972947sm2217120pfd.130.2024.03.28.21.46.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 21:46:56 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com, samuel.holland@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, tech-j-ext@lists.risc-v.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh+dt@kernel.org, krzysztof.kozlowski+dt@linaro.org, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, Liam.Howlett@oracle.com, vbabka@suse.cz, lstoakes@gmail.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, hankuan.chen@sifive.com, greentime.hu@sifive.com, evan@rivosinc.com, xiao.w.wang@intel.com, charlie@rivosinc.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, dbarboza@ventanamicro.com, sameo@rivosinc.com, shikemeng@huaweicloud.com, willy@infradead.org, vincent.chen@sifive.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, gerg@kernel.org, heiko@sntech.de, bhe@redhat.com, jeeheng.sia@starfivetech.com, cyy@cyyself.name, maskray@google.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bgray@linux.ibm.com, mpe@ellerman.id.au, baruch@tkos.co.il, alx@kernel.org, david@redhat.com, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, shr@devkernel.io, deller@gmx.de, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com Subject: [PATCH v2 22/27] riscv signal: Save and restore of shadow stack for signal Date: Thu, 28 Mar 2024 21:44:54 -0700 Message-Id: <20240329044459.3990638-23-debug@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240329044459.3990638-1-debug@rivosinc.com> References: <20240329044459.3990638-1-debug@rivosinc.com> MIME-Version: 1.0 X-Stat-Signature: 1s8e8jfotj77esitiqybnp1p9uq5cb5b X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 7358580002 X-Rspam-User: X-HE-Tag: 1711687617-885643 X-HE-Meta: U2FsdGVkX1+BqxOftkKJbjiyynR4Uqij2BDsbP78ZeCrKeJKBofF3x6ovyS7i3KkjhoyBjcSC5SptALv/7eRMMbwJIpgquozMDLM8Famp5X7dx4H1FKWy7ZAMLIX5r6I0UzSoG919/5h0TRBej0Q9ljTi1lXTWmdLdat9/sHf5BKRPtdtq3F7B2w6GCHNM0kxqRU07YdbDdWnel3RXWdC4QVRpNwJZovy2rD2+2uuycLQxhEhsCEGmWRHSq9egmNeFMeqGS+79ozEpmk7c4Nr9ynE5CSdjNEnTl4o+Fm7la/Qk5s4rg6oNqxUmlctWp380KK414z5dI40K5R7kXobKCHqUGYt7pRvL/Tp9v9zOxM0IW3LBdMj9PfFOPQa7EhB04ggqWejR6lEjQ0cdF+3cGnZr0sqFDr3UoN+SS6LyWQzmuJo/vzsYG/YXj70gWhAv98qgIhsZHxPb40Rq+Ir7QviynKnPTRJSaBQJ+mqGIAlGO/0sztmdRtoHDZs7E0aDAHXGQ8KRqmbKXdhHfrmH+hISp2kW3plXbZAF8PUKmOixPHE8iuB2vh50LFu0GkDIcnno98zh7EvnUcPy/5qyPx7CwUl4Znu9dNOK7KPUNRGK6uB3oq39wF/lXMcQY0VIYVdJ8xlarCwzSiXHf8ye+ujl1yZRN2Xpre3DJI03WJjFxDv5hfKQ9J+ExGW9ZNJcpojlxnf2CaSn+rtxghehYCmCXpOhIR8nGWiFLrzsLfNpwWYOR2GLWwARz63bWUFZD2C0doycbaksCE9JDLP60EFZSjQnJ6XIx5jUZC5g5h9fHP4siiXYS07T/iF7NouZeetjiVysPslZ5oOMv2rj7hoVfJuVAqioHuwzf/uqfkH+ddUfsVjeex2hw9aNlA+si63AGT/NsTWjBtuzfhLGoKTWD1Ccd9zwMjpiRQb/jTTRjv0Nym1QA4nGdB6u0s/vh71GRXItmGfd0ZfuB 9jfqpJ1I eW1gyyYTRN8fydHPQkQblDVCAS8zT6ptFKjWmwiPR8rD5H05kqyWZcNeoC277ZS81CotNgHoaFz5ZuxdPHj4xnK0/g9ab+uahuSWYUy99FFKsH71YM/YWyr5rOkwiy/dj9zbUgMXV5CUncdqHy7AeePJM1o5IwDZe1f7IHDjAj6M1gyEwexG1TKsvgnHBl6/1kcEJBYK2iGUwh3/yg+jbnDrL/uW9Eb4GHFRwulUK1XttqiDupWR5LFqEwfrVv3Cym84+1hGDc2y0GlKNgOoCpbXqhPdZT795ZhukAQQjCa8cv89Y5nPuSgHiRJLNTRpYmQQOhwn7VCSKHkH9iifPNv14BmxFAfQ1jp+PG17055JbpMuFXYe0hQB9rG0rHns0pLp/60AE4ZLTQzzmlP9hQooTExYkX9lEXhioBJTD3Lr/I+CKKHnIgnZy+DpiH5uFx3H/CTvvm+k5v2JAubsUhk3He/l8w7Ot2Ddsu0lSNxohw3QaIXv31CGVy0QdNOQYcnn2uiEKhzvhhuA= 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. Signed-off-by: Deepak Gupta --- arch/riscv/include/asm/usercfi.h | 19 +++++++++++ arch/riscv/kernel/signal.c | 45 +++++++++++++++++++++++++ arch/riscv/kernel/usercfi.c | 57 ++++++++++++++++++++++++++++++++ 3 files changed, 121 insertions(+) diff --git a/arch/riscv/include/asm/usercfi.h b/arch/riscv/include/asm/usercfi.h index 8accdc8ec164..507a27d5f53c 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 @@ 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); +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); #define PR_SHADOW_STACK_SUPPORTED_STATUS_MASK (PR_SHADOW_STACK_ENABLE) @@ -77,6 +81,16 @@ static inline void set_shstk_status(struct task_struct *task, bool enable) } +static inline int restore_user_shstk(struct task_struct *tsk, unsigned long shstk_ptr) +{ + return -EINVAL; +} + +static inline int save_user_shstk(struct task_struct *tsk, unsigned long *saved_shstk_ptr) +{ + return -EINVAL; +} + static inline bool is_indir_lp_enabled(struct task_struct *task) { return false; @@ -92,6 +106,11 @@ static inline void set_indir_lp_status(struct task_struct *task, bool enable) } +static inline unsigned long get_active_shstk(struct task_struct *task) +{ + return 0; +} + #endif /* CONFIG_RISCV_USER_CFI */ #endif /* __ASSEMBLY__ */ diff --git a/arch/riscv/kernel/signal.c b/arch/riscv/kernel/signal.c index 501e66debf69..428a886ab6ef 100644 --- a/arch/riscv/kernel/signal.c +++ b/arch/riscv/kernel/signal.c @@ -22,6 +22,7 @@ #include #include #include +#include unsigned long signal_minsigstksz __ro_after_init; @@ -232,6 +233,7 @@ SYSCALL_DEFINE0(rt_sigreturn) struct pt_regs *regs = current_pt_regs(); struct rt_sigframe __user *frame; struct task_struct *task; + unsigned long ss_ptr = 0; sigset_t set; size_t frame_size = get_rt_frame_size(false); @@ -254,6 +256,26 @@ SYSCALL_DEFINE0(rt_sigreturn) if (restore_altstack(&frame->uc.uc_stack)) goto badframe; + /* + * 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. + */ + if (__copy_from_user(&ss_ptr, &frame->uc.uc_mcontext.sc_cfi_state.ss_ptr, + sizeof(unsigned long))) + goto badframe; + + if (is_shstk_enabled(current) && restore_user_shstk(current, ss_ptr)) + goto badframe; + regs->cause = -1UL; return regs->a0; @@ -323,6 +345,7 @@ static int setup_rt_frame(struct ksignal *ksig, sigset_t *set, struct rt_sigframe __user *frame; long err = 0; unsigned long __maybe_unused addr; + unsigned long ss_ptr = 0; size_t frame_size = get_rt_frame_size(false); frame = get_sigframe(ksig, regs, frame_size); @@ -334,6 +357,23 @@ static int setup_rt_frame(struct ksignal *ksig, sigset_t *set, /* Create the ucontext. */ err |= __put_user(0, &frame->uc.uc_flags); err |= __put_user(NULL, &frame->uc.uc_link); + /* + * 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. + */ + if (is_shstk_enabled(current)) { + err |= save_user_shstk(current, &ss_ptr); + err |= __put_user(ss_ptr, &frame->uc.uc_mcontext.sc_cfi_state.ss_ptr); + } + err |= __save_altstack(&frame->uc.uc_stack, regs->sp); err |= setup_sigcontext(frame, regs); err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)); @@ -344,6 +384,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 diff --git a/arch/riscv/kernel/usercfi.c b/arch/riscv/kernel/usercfi.c index 13920b9d86f3..db5b32500050 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; @@ -168,6 +173,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 Fri Mar 29 04:44:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13610067 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 2D4C9C6FD1F for ; Fri, 29 Mar 2024 04:47:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B9F896B00BD; Fri, 29 Mar 2024 00:47:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B4CC26B00BF; Fri, 29 Mar 2024 00:47:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 929496B00BE; Fri, 29 Mar 2024 00:47:02 -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 73DBB6B00BC for ; Fri, 29 Mar 2024 00:47:02 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 44BA9C10A7 for ; Fri, 29 Mar 2024 04:47:02 +0000 (UTC) X-FDA: 81948841884.27.4EB9968 Received: from mail-pf1-f170.google.com (mail-pf1-f170.google.com [209.85.210.170]) by imf04.hostedemail.com (Postfix) with ESMTP id 894024000E for ; Fri, 29 Mar 2024 04:47:00 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=F4XUy8q2; spf=pass (imf04.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.170 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711687620; a=rsa-sha256; cv=none; b=bhNEQNoGzhit1H60yrztNvN7M2ExB3K4EQ6VqQv+H/AiGGOjrNkhL95mkA1QGCl3bvGh+l 6z+v9dpx9nIjmYvGAYg7BpNoSmG8JGbzfMoPzVKjVWz+YblgNO2EnTny7+BAWeaAfQ0lOz jEoBwIcrcMy6ik56/OCkoZVN9sWxg7E= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=F4XUy8q2; spf=pass (imf04.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.170 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1711687620; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=J4h29gLixoMV8jqguvN/5Pzgz4C0EFoOwStmiEzJSk4=; b=UlDO9nZqwHfcx17dlOAhnOW/JNTcCs1MMqQDaWFjG5E2XZrHo66b5gvKqNw86L2/Zonaal kpVD9kAi5vKgdRHqJCGcugjsUWWnspVgyyazaSzFHJlsAWB3RTnNYLnhpVaxTC4z7JCAp9 ecgQdb6jNZEQEl582bVA5cF0W4qPo+I= Received: by mail-pf1-f170.google.com with SMTP id d2e1a72fcca58-6ea9a616cc4so1288271b3a.1 for ; Thu, 28 Mar 2024 21:47:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1711687619; x=1712292419; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=J4h29gLixoMV8jqguvN/5Pzgz4C0EFoOwStmiEzJSk4=; b=F4XUy8q2UIoobryLwrpiJE+qZJkLxqAfcL5jrKZYxtV4O8Kteh5Ur03s34M2Auadu1 RfrnjLDtrboRak20/Cm6mIy7cdXBzX/KzPoUzfopwb4N7N6Z6gouL7TrKr5LHmIf7iDR xTKLesU9I+r9LQB8UkQ5W2UMBClnSy7j42eua4HKv/12I9Dc7NURx5HKYpOlS40G83Qq yCVbBZ1WTinsEorSEQoBEr45EPS6f6LABuHsvPrer/AhGP6omagTj0jm455Mjr32H96S tAng+r3HmwmCo3PHqwBSTegj1/rQ6NoFIxDc4uA94FBjccgLlMd3XoU5C9zBB/vcmmHT CFbw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711687619; x=1712292419; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=J4h29gLixoMV8jqguvN/5Pzgz4C0EFoOwStmiEzJSk4=; b=MzUMzsW4N3nXNL089z+N17BojrCDAfAsrDXMh2Uc1mKfvqGyAhV6JTFYWWDamv+D/s uAF54PYx2slZEZRCa9Dawm7FchaZKPiTHzjxdTdZXCHLU8Z/ABGk2mZkZrNvqydgR7oo xLo3GbWYpusTfD2EtAksQykQmXjK9Q7bjw6enBa833vgFh8pL9zhf0NF1YyfzSlI+ina 143hMLRsUxAhugZQG5pOWuIESgehf0Aq2BPOu40P2dDWCXlKYTyxETxeGiIfLYBdleYM XokpDI6S0WkoNhIbIN/dgP2fnZCTv4mJmYncRDkK2Pz9MlTVrJZtJWBF5HkDyjQp4mXN WHtw== X-Forwarded-Encrypted: i=1; AJvYcCXX+KbRlfZ7DA/9sx67XBYQ56qdcWAIkzOunRMu4I2bJEHsjLKYPDx43PXjFvB7k3ZO3FBeRRHv06rv3E6Es21Wlys= X-Gm-Message-State: AOJu0Yy/GT1aBUvi+eFAUmPJSleW4MAO1sq6W0tCN+2mRqgF54qfxCAg 2kU9fEam1Vb6FkOzvalRoPYktln9tcBje6OX66Il8cIekPvU/FHro4lzygC/oLc= X-Google-Smtp-Source: AGHT+IF2Qlx2fW0K+tYyoJfMDwHqIC/6lXzl1sxupcwhY7+4lXjfHF7ZdfX+bDYaTDAXzjEckCXang== X-Received: by 2002:a05:6a00:1481:b0:6e6:9c79:87e9 with SMTP id v1-20020a056a00148100b006e69c7987e9mr1470495pfu.34.1711687619345; Thu, 28 Mar 2024 21:46:59 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id i18-20020aa78b52000000b006ea7e972947sm2217120pfd.130.2024.03.28.21.46.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 21:46:59 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com, samuel.holland@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, tech-j-ext@lists.risc-v.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh+dt@kernel.org, krzysztof.kozlowski+dt@linaro.org, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, Liam.Howlett@oracle.com, vbabka@suse.cz, lstoakes@gmail.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, hankuan.chen@sifive.com, greentime.hu@sifive.com, evan@rivosinc.com, xiao.w.wang@intel.com, charlie@rivosinc.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, dbarboza@ventanamicro.com, sameo@rivosinc.com, shikemeng@huaweicloud.com, willy@infradead.org, vincent.chen@sifive.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, gerg@kernel.org, heiko@sntech.de, bhe@redhat.com, jeeheng.sia@starfivetech.com, cyy@cyyself.name, maskray@google.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bgray@linux.ibm.com, mpe@ellerman.id.au, baruch@tkos.co.il, alx@kernel.org, david@redhat.com, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, shr@devkernel.io, deller@gmx.de, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com Subject: [PATCH v2 23/27] riscv/ptrace: riscv cfi status and state via ptrace and in core files Date: Thu, 28 Mar 2024 21:44:55 -0700 Message-Id: <20240329044459.3990638-24-debug@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240329044459.3990638-1-debug@rivosinc.com> References: <20240329044459.3990638-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 894024000E X-Stat-Signature: 3a4izhf3wcbnwacgzjg9kaieawkszycc X-Rspam-User: X-HE-Tag: 1711687620-299052 X-HE-Meta: U2FsdGVkX18cshmmllsoNx6i5mU2smHmh1pYI+LF9fOr9AEOU86cu1eJ0s7XxnO7lgwtk8tEll8wmmnAsYQaSRL/Lqhn5CO83um8Pi7mLJbNFJs76+esTIMLM47MNDx6b2tKoqeMsJPIQDtimxwQCIatyBtf/l7oOfVH4T0GV/WjaRwfCa9mxRb6xEFva/2lVKgGxE1kMpFoGuWOb/xtS/GuwhjphXbvPPV3hxGOqzltARvp2PH0/bdqxl/B6+cj8H/zR977SIvcU6J3rfA50aajnhToPnRO3utLSwjF24mb90qNjdNy+Od0OoKvyOrGt05gmEWcip/d9CshRen9LFnTpIhHFGiPzvTuGFxcZpvhhiE696zYVDrA8uwgf+K4zyivBh+6Uq1wZUKSokVOy5B6egPeufziTlHETsNJ/vTy4A3cUs09OpdGXVSC2mRwr6+zMdabHqnAaThzUgaDtpdmqL2rxCDuIusYDmDtzdBwawib4B/yjzlitGKArWIQbeATcWEAzhD9vH1uxGFRFS4KXLhwPQdlZhRQHK1AaKcvF4o7uKmrCon7PMT6iDRl5zMwRssVWPutQ1lAOLmRJxQgHN7JbDhXAPb/SvPkQhaY7/ZVEZWS3rP4hjRDG8Cv8P2R9PqjZhSOC6Oxvu5bF+mcQgyLEfpJbQ4NjOocQ8myX4/vgGrID9QPSM/R9mHzoKdZ0jK281LQCEJleWgRPKAW//HhgH/qxl+YBySWfi/atH/5e58xa7FKoPnMEE2vAKhXzw+UEm3XhZJNIjOU9NdE99YGGNMCxzhqKjvF3B0iSGQjCZwQ90c86aDrLS3dozPuSK/tAq0rTH+rLSylwl6N8d5gxjbXaE6ldHMO8p7ogMKydeEdzXfdLFdLh31VCUYt2WPdBgxsxoCqK3G33seUkslfLf++y/JABIdkstf3100LopKoXnPN9W8F5p6S8JFRDOI2bE7UP7HCnv7 jZFHJNCb K2OirYQQfXy1BjY/Xqn9yaeaCv4MagTlzCs+oNftoLwAwAJpfRd/i9q+5rY2kjCah3GU+QvGI3ZXyzfdYPRzD1cLAwnFfBDfq/aPucNWZCs7rpI4mm9nTdx1mreUAWA34fY5q+dV4xzBFFX6k5jsfgE0jmvLwOmfixiCzJpMKAewD3WxW8NfDRv93icYiTXsbOS+mSEsgfU3d0NbcJ5EnlMvhFLMtiIKNR4dof6NFL/kRbjK917oGejmWSg== 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 a38268b19c3d..512be06a8661 100644 --- a/arch/riscv/include/uapi/asm/ptrace.h +++ b/arch/riscv/include/uapi/asm/ptrace.h @@ -127,6 +127,24 @@ struct __riscv_v_regset_state { */ #define RISCV_MAX_VLENB (8192) +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 e8515aa9d80b..33d4b32cc6a7 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 9417309b7230..f60b2de66b1c 100644 --- a/include/uapi/linux/elf.h +++ b/include/uapi/linux/elf.h @@ -447,6 +447,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 Fri Mar 29 04:44:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13610068 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 49731C6FD1F for ; Fri, 29 Mar 2024 04:47:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CFE746B00BE; Fri, 29 Mar 2024 00:47:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C5CB56B00BF; Fri, 29 Mar 2024 00:47:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A3CD86B00C0; Fri, 29 Mar 2024 00:47:05 -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 7DCE96B00BE for ; Fri, 29 Mar 2024 00:47:05 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 498CFC0843 for ; Fri, 29 Mar 2024 04:47:05 +0000 (UTC) X-FDA: 81948842010.11.D76950E Received: from mail-pf1-f169.google.com (mail-pf1-f169.google.com [209.85.210.169]) by imf07.hostedemail.com (Postfix) with ESMTP id 6C13B40005 for ; Fri, 29 Mar 2024 04:47:03 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=EzxmspcM; dmarc=none; spf=pass (imf07.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=1711687623; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=hGZuEOPDrPSpE9XmexYpP1TU1T3x9oK+sun0qc4e7DM=; b=1afgW5vSvZq7PF/CpUujwC5Dg+2fTDUKswoSQQihQjQ4t5Coy9V0OIpRrBsdA9ywkRaP09 qjIXiRsTNfaeJy242qODhOowpJt1tCIa6DnVZWIYm6fF5aPx8pGT2+Qd7tEoo6e/yQjvuy upOC5YoZGjo9QdW8qJINM22/dvkC2Es= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=EzxmspcM; dmarc=none; spf=pass (imf07.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=1711687623; a=rsa-sha256; cv=none; b=F3NSp1kL0lAoZ/ptezLhZ2g8OhHZS9jZfBVzRA8sPBwqK3CPkBK7LWmsLcZd9/lTCT8j1l rRsjrROw7QkkrxLq49a0Y/n0Cz6ML8wx8VGuUHWU8cM0b4wa7JCU1lNRUOMGvz7+y/ncnO iqcgSp0iZ8nYDoboFc77msLmIVN8ZAc= Received: by mail-pf1-f169.google.com with SMTP id d2e1a72fcca58-6e6ca2ac094so1581612b3a.0 for ; Thu, 28 Mar 2024 21:47:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1711687622; x=1712292422; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=hGZuEOPDrPSpE9XmexYpP1TU1T3x9oK+sun0qc4e7DM=; b=EzxmspcMbetoofz4KYIdn0i+8x7zvrSSqk7Oqyx65+ece1dQYuaxLIXojjsEli+EeQ VOZkgF8+B1jTuAunR/n7qedXIlUPa5ksC0zI5jfBZuznsB+LJ07HhNt0vqtNiHCHN7GF YhfZyqSkpD9OSqDARDyrUvYzFpt1MFWATeNBlNe2lPMkxrc77ab7yMBkex+AS47MzDVU na5OrGFc5rvVFMsEGbxFIK7TqQ1Lzh2dOOfAnUl7te5bnDwJDTeub9PRi497/zi9l1Nj B/wPrea3CbN9QJTWWEpCUWn0cnlsp+eePpBHYbeUS5plDov9jE7SQHD3j/X0+Vg6lQBV VvDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711687622; x=1712292422; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=hGZuEOPDrPSpE9XmexYpP1TU1T3x9oK+sun0qc4e7DM=; b=wCio72imibJA6owyk/5pGkQ915QGDTnWROEQE1tXEXlKumxn6ZREdqmgrZOLmmggk6 7ADd6ij446vN8zKQi4eicfnSGDbok/Fcq3iM+Adbx8izkrho/L/lredlJ1IbGOfayIDc VqdR11WcyzQ9thJ1VcXLz//CDE015PF0gUWZ44cE5cizlDOHXWR43WIwnP36tYaLQwDX qv2fvzPUKF5lIT36L0O+5xj/mxmMTu6AeS47EVKv0b3AwAGU0EF7bzF9B8ojb1Yofnxx LIgyrhashB1pJpmN1kTVt62ktvyR/88ALwsSTDpkObhX1NuqswkblhBdBZY7GjRW0LvF xQ0g== X-Forwarded-Encrypted: i=1; AJvYcCWHihBGofuSRTEnZZloXmAbVqUULZJU6eBAvA74vBu8vzba7HDR+1aNVcTtcsctCogWUSxFobFqcgn2n0JzXEh12xg= X-Gm-Message-State: AOJu0YyxNun4szt4JlZ76JD28xQY94nHH0JTaFKfP5kcE7F49fEk54sW Xxwo4bxvICf/F8KviFacd2YphrosuQaBxEVSUs1MHKefKzB/20Qarob2cMkkv3k= X-Google-Smtp-Source: AGHT+IGVNl5i8Vrjt70n++oCEY2675qx+nSiOe/0hD9dLIqDDPWOmm3HrHLeaGTRiVH879DeLLhxrQ== X-Received: by 2002:a05:6a00:a82:b0:6ea:d10d:c96d with SMTP id b2-20020a056a000a8200b006ead10dc96dmr1470360pfl.19.1711687622313; Thu, 28 Mar 2024 21:47:02 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id i18-20020aa78b52000000b006ea7e972947sm2217120pfd.130.2024.03.28.21.46.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 21:47:02 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com, samuel.holland@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, tech-j-ext@lists.risc-v.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh+dt@kernel.org, krzysztof.kozlowski+dt@linaro.org, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, Liam.Howlett@oracle.com, vbabka@suse.cz, lstoakes@gmail.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, hankuan.chen@sifive.com, greentime.hu@sifive.com, evan@rivosinc.com, xiao.w.wang@intel.com, charlie@rivosinc.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, dbarboza@ventanamicro.com, sameo@rivosinc.com, shikemeng@huaweicloud.com, willy@infradead.org, vincent.chen@sifive.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, gerg@kernel.org, heiko@sntech.de, bhe@redhat.com, jeeheng.sia@starfivetech.com, cyy@cyyself.name, maskray@google.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bgray@linux.ibm.com, mpe@ellerman.id.au, baruch@tkos.co.il, alx@kernel.org, david@redhat.com, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, shr@devkernel.io, deller@gmx.de, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com Subject: [PATCH v2 24/27] riscv: create a config for shadow stack and landing pad instr support Date: Thu, 28 Mar 2024 21:44:56 -0700 Message-Id: <20240329044459.3990638-25-debug@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240329044459.3990638-1-debug@rivosinc.com> References: <20240329044459.3990638-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 6C13B40005 X-Stat-Signature: bzjctaix4d8cgi6r8ii8qiexgbkwowe9 X-Rspam-User: X-HE-Tag: 1711687623-923711 X-HE-Meta: U2FsdGVkX1888x2Y9nwmFN0x4L+LLpwd4YNRj53QbR8PhwZiYiyoBkXGrgGd71fIBlrwlsnwaM/yPlFwupiIdBE4OSr0lP+2JSWtgTGR+oy5orAcQ1YUmMg8izYYf5hXl/Xgh/L5UNUlcqt+QJHsL/pMAzEnX4fS+SPW01JOQB+rDmRFl7zxqOivfZqvJK0FK8CwN5Wk/AGYvgv3wFAnNzvRY3oLkM0ATOQpPNHQHf+Oli8ZCU47CvAIXnezCWGhU6UDGmMb0yPDmffxs4wSbBrpn/ts36PRJbWdN2mZQZxGjEJtFvsmhzQ061WzXJ71M6EvSmQTZvQcm7sssc8R2oTE4L8RpnFq5auqtMoi9x5IkG7eu6Kai3lvcw4ektZRCLPDpck9E3sWNG+cj0RFJg7E+qRti25beqik4SgcGijJ0+12jVacpx2Z3LjjWTCPoHRerTSYaNRk68AlOoIx9YrkSpqgUDVwTKuXzHmfvtyiSumez8B/tLTEzu6GZ03S4iDh1PqdeBvQyKNk/PvGdxhDtQvoXwIji0PS6mjAaaZAFPYDJN2D0F/gjqkNcXKeyYVZrhTD7ZJGlvDrgko/xa3gTyYzdH7XtNlPKSxGKC147kPuwyppZtLy1HuIgDW6TTIW0IKUnIWkXuQvsxSpolMr20LBWDs1K2zyt0En64/+uIs1wh2mkHh2XvprSE0xbfw25TGOzxcHuPh9qQGflu1PCU5Nb60xxShteXSVoXuhMfzpBzh4ARALGudD58odedEKMe72pkEsT+yjaHxhSqNOexhNVIpBz252jvyCrm9hSUlxtk/q2yF0UVMtBP/fyQqQm81McT4a3rGjbhK9vEgpy6NaN/T0LaCnLVTGF9SKtBI2VZJoat7fOHbNHdO4G1QZ+6FG7P3RiNg3r+o/yAM2Krb72sgrDYIydcCihmVuXnVlCASloHhc+STzkG9UwjaeYWdggVUPED+A6TR VWMnVyQW p9YCURvc7ZJxmId3twA3s3ewdp+DM7AGtxD8so7NIuQG5JRM5Nu+rM4uO3nCMksC3vnON9HG3BtM13j36MFXPpqgf5tLmAXWmu+nZoIWL9O+LFLIalUnf6iOSGiSJ8dQr8VxZITkqeA6ErsC3ooGQ1XI8IFWAN/qvQogzqXphf0hGVlshcfGGwLRUaTPWmm6+D5jZWoZA3qDRPPf4EHXnt8K0/y10+FYxyMLymr6F6/3p1jvJTBxlHuRd7vFZWT5vzwLRGKvnHvW1sJdkDbjNBGCCT9AV4nCp4ZYb2OOm6JeJdqWhgfUhVYM4/HgpHpWsbfEqJlPSi+SJY19NU/Gpc1Q6J7GDZy5uIh9sef5JKgsQmg2QUdA6ELu6HQ5p84jH0q2CnEAL/AF4US5aSmnvZTg9aOyYfamNFQAa/nKtwVt7L6aYIRYVtGSgzzuMY0ABhbpYcSYNGbsTVfSiXWRQ0pJteUoYy706N2vlYk31LwvEGWHP5DocOX9ua2pIgMcISv9kPCA5+j+wQZZHvGDS0PcPlqwOeIpGW9R0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.001847, 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. Signed-off-by: Deepak Gupta --- arch/riscv/Kconfig | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index 7e0b2bcc388f..d6f1303ef660 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -203,6 +203,24 @@ 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_USES_HIGH_VMA_FLAGS + help + Provides CPU assisted control flow integrity to userspace tasks. + Control flow integrity is provided by implementing shadow stack for + backward edge and indirect branch tracking for forward edge in program. + Shadow stack protection is a hardware feature that detects function + return address corruption. This helps mitigate ROP attacks. + Indirect branch tracking enforces that all indirect branches must land + on a landing pad instruction else CPU will fault. This mitigates against + JOP / COP attacks. Applications must be enabled to use it, and old user- + space does not get protection "for free". + default y + config ARCH_MMAP_RND_BITS_MIN default 18 if 64BIT default 8 From patchwork Fri Mar 29 04:44:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13610069 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 23391CD1283 for ; Fri, 29 Mar 2024 04:47:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AB15A6B00C1; Fri, 29 Mar 2024 00:47:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A61516B00C2; Fri, 29 Mar 2024 00:47:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 88EDC6B00C3; Fri, 29 Mar 2024 00:47:08 -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 68C3F6B00C1 for ; Fri, 29 Mar 2024 00:47:08 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 3637916112D for ; Fri, 29 Mar 2024 04:47:08 +0000 (UTC) X-FDA: 81948842136.04.3C082AF Received: from mail-oa1-f51.google.com (mail-oa1-f51.google.com [209.85.160.51]) by imf15.hostedemail.com (Postfix) with ESMTP id 5B15AA000A for ; Fri, 29 Mar 2024 04:47:06 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=YYsi9OhF; spf=pass (imf15.hostedemail.com: domain of debug@rivosinc.com designates 209.85.160.51 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1711687626; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=f5ifazHbpOl/UA3v79b+PzDDuljcd/dBpmVrUfFdj7c=; b=wdskGhMGnMlN/YKkj2szqXcZLbAMxJ0FN2W+7XAotuHK7gEwV77k+ISEpomsftGZQRjc7Q CasabfJuB6qmoTn85gONgv+guzZ3wiw8MWgdvqmTNVzb9lYK1HknVuQN6p9G8NhxPi4KOD EZpcUuzio4gHhomqAxGIsBzV7cY0Tbs= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711687626; a=rsa-sha256; cv=none; b=z6oWamhsejVfA4UA4sMSXp3zvgNRXNNTmAWClCogA7s50jYc4lVupCr+XahyYFCG/7+qMy zVdn9vg/CFkF/qQJKL/o2ucj3iQ+cEFswHyAVwZG2hpke/eOb2mCjQ3GkQELzoqa2sd3PC PX3nlRHsiuleUDybe73bpT2BGEQ6wmU= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=YYsi9OhF; spf=pass (imf15.hostedemail.com: domain of debug@rivosinc.com designates 209.85.160.51 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-oa1-f51.google.com with SMTP id 586e51a60fabf-2218a0f55e1so924401fac.1 for ; Thu, 28 Mar 2024 21:47:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1711687625; x=1712292425; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=f5ifazHbpOl/UA3v79b+PzDDuljcd/dBpmVrUfFdj7c=; b=YYsi9OhF+9WxwhS5YRta8QxW3PLPzsF8BLo557Mv0Cv3Xx5ECM7jpVhLJop3JGrwbD iMYHELz2j9ftF4ww++5BuAwK4S8N/KjDF/0XuJd+f7EnbaOyYPvQv2Fl5Cgwe4eDZ6Gt LmDYju/LktsQ+HFkOjbV6gL187Gpeu2Kqvg6pGdYztAwMrqB/5Rkwjz68ENxbPjCQxFT wfWCboJFTcMvA1hBI5Y1SWyB4i2Q9fVWC1pncqWWZtDg3/u6DksEyjDeHZuP6+REQ06q aDaDEUIu34s4L5jf3nHdvN32BxmrQNg5/vQV/CrRylmH+cFCTcTJsnlVJONsaokAzHhF TG7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711687625; x=1712292425; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=f5ifazHbpOl/UA3v79b+PzDDuljcd/dBpmVrUfFdj7c=; b=El+td3JUIRNY1Vv/AKplfJNKex7z2Ax1/bBRN955je2hCLHktKRmj5hGyscfu4aApy AlWfxwS+6bkXHvF6z8kAhclwzssODSTC8jZtW0PofMZIjqp/DnZp0/tc1m2MJvyUFBdw 0kxyMaks14SvvmALtLfwxAhaD6NR7AZ0/OTODXohgUT0GLX5FMyi8Wt6PfxPvq5yI0a5 KVWOJDFQzs5fL34JfQ0pVfIhYlMR+aDUuzW8NAq8XjrlmbDZoudhcvPnBksDuaNB2+Ha pr1vqMTBxFyNnuSl+ZuvsMF8/y46z8je8wmlUIOcbwUDJ4z6eFhox0K1KLjkihPjhkj1 8pkw== X-Forwarded-Encrypted: i=1; AJvYcCVUnW0tTrPTmhWqHrYvv/T6FA1vmzdjB/JzhyElSAqlzoi9eMlC1GPckd3eBKAmUgzl1r5uZzEUSQv0kCVNocavqK0= X-Gm-Message-State: AOJu0YwE205g9Aj47R4GgUDPDGvGAT/dpwI+Cej/mbF7ff+BMipM+cQu cvde+/7TsCmvWbCnCt4CXCrBmccxZ3zaU/ehx3ALyyN+N2p6/7yhm1Narw+iL+0= X-Google-Smtp-Source: AGHT+IErdAfZ2YKd7ZlzSeS592zYA4TFUt0LCB93K8r2LIl39Tat2IOKwkbBJaRLu5ZvxofXUIE51w== X-Received: by 2002:a05:6870:3912:b0:229:819a:d2e6 with SMTP id b18-20020a056870391200b00229819ad2e6mr1199667oap.15.1711687625326; Thu, 28 Mar 2024 21:47:05 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id i18-20020aa78b52000000b006ea7e972947sm2217120pfd.130.2024.03.28.21.47.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 21:47:04 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com, samuel.holland@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, tech-j-ext@lists.risc-v.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh+dt@kernel.org, krzysztof.kozlowski+dt@linaro.org, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, Liam.Howlett@oracle.com, vbabka@suse.cz, lstoakes@gmail.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, hankuan.chen@sifive.com, greentime.hu@sifive.com, evan@rivosinc.com, xiao.w.wang@intel.com, charlie@rivosinc.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, dbarboza@ventanamicro.com, sameo@rivosinc.com, shikemeng@huaweicloud.com, willy@infradead.org, vincent.chen@sifive.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, gerg@kernel.org, heiko@sntech.de, bhe@redhat.com, jeeheng.sia@starfivetech.com, cyy@cyyself.name, maskray@google.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bgray@linux.ibm.com, mpe@ellerman.id.au, baruch@tkos.co.il, alx@kernel.org, david@redhat.com, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, shr@devkernel.io, deller@gmx.de, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com Subject: [PATCH v2 25/27] riscv: Documentation for landing pad / indirect branch tracking Date: Thu, 28 Mar 2024 21:44:57 -0700 Message-Id: <20240329044459.3990638-26-debug@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240329044459.3990638-1-debug@rivosinc.com> References: <20240329044459.3990638-1-debug@rivosinc.com> MIME-Version: 1.0 X-Stat-Signature: j9kj1co1a9z5obo95zwr8sopgp8qw9yz X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 5B15AA000A X-Rspam-User: X-HE-Tag: 1711687626-659119 X-HE-Meta: U2FsdGVkX19GOjeeDgh+fYTunbL2O9mqbbEoOHlTYeCUeuHVY3MQ7FaQPJkl22f4jUGksX0/ifzI1j8Dm1t5vkUnB/1UJ7xvtctMmb44yUpi0+wpkVe8/kAbA+sF8dudlKzMiWjPSLD71Ws4hy8NigOuLBIT28xAHmJKxDHTML/4gh3LG/yHIUcRvqsfck6/fycMoOy8bKcPgwiJNZU7cqvnan1zu63Dh/JP+4IMEUFe85q0UeMO2vyMQoYJhYOoldqDCUarZwB9LhSVXDmQuS2fao3b/qtj/j0J+hIiIDzFLeeMxiLpoMV3aZ/23DacL25sDT2Laujp/kfI8218iOvvvBPlUdhQSNc9GcMi4QhAzpYClBAgLJpETrWWEDfh7F6RmDrLBbO1cMughLIyIDANCLK63ndLNpKJjUD/4CFBWOA8w+5W2ori9dFwiAvrZCDwIoLBplOV9gXR3leSBnFO8y+X2ePGo5j8nxknY8c69JKrHjGJj8cohxBZpXyd+DTV1JQhQhtPFuY8BHNShktovFuth/fCh81nlMaKiEl94mTkZAAdF9L8XDIiWrFLUtxOedof7vv+N7QGwTCs69g7zlknqX7KZL27sPCfVVqbtLuybVE0lQOgginUJExap0DqCy4xvfEoAG+49UYXkNCaattjoqpcABC3anzwuIUTOKRfcxWMhmDmZHmO+DZtz2smq8SDZLldp/Mg80SP/6O9kihNv1Q4pp8XCxbOdikx7+Ma6Jnjc12yJ8v6eyTkbmt+RZSeHsNCI5DUg+JqFk1H0XZchkk4mFWaS1Z67jrjhgUJ87PdkhLJbsyC/rxihN8kWVT9S7/xTGlav8YRY6isy7YirLV7ZWdWN+5adDVjNiSLrlXK8cWZfmUNZRY0z+6ysGPA0Mt5lp8NAVERIt7E/Jm9yrwzzmwYKjIouODRMWXj347KiXyFg38roi+dbRZPKmTjy9eZUN4hW90 4BwjJTtW 1FS934aK1ueGsy0vEElwrtRlxuA7S9zXvGnEsMANmMMKOhiYLXhb61QzM0/om14t0YTIQAhb3bJ2l5bpEooy+txZtvdtvy72vEWwQGcW7Sif+sSJcnMIOq3IFdT9W4nUb7pTLxjiXRJzvLG188LehiSzJmyhOLEsFBFSLXLxiBke39sbrurMaBdd+3m75tso61G4lDzmlQNzM1FiEAkprBsYi0f+MjBjXrAykbNM5vDIKmmw= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000020, 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/zicfilp.rst | 104 +++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 Documentation/arch/riscv/zicfilp.rst diff --git a/Documentation/arch/riscv/zicfilp.rst b/Documentation/arch/riscv/zicfilp.rst new file mode 100644 index 000000000000..3007c81f0465 --- /dev/null +++ b/Documentation/arch/riscv/zicfilp.rst @@ -0,0 +1,104 @@ +.. 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, ` and is a HINT nop. `lpad` +instruction must be aligned on 4 byte boundary and compares 20 bit immediate with x7. +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 proglogs 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 `GNU_PROPERTY_RISCV_FEATURE_1_FCFI` for property +`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 +-------------------- + +`PR_SET_INDIR_BR_LP_STATUS` / `PR_GET_INDIR_BR_LP_STATUS` / +`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. + +`PR_SET_INDIR_BR_LP_STATUS`: If arg1 `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 `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. `PR_INDIR_BR_LP_ENABLE` being clear) + +`PR_GET_INDIR_BR_LP_STATUS`: Returns current status of indirect branch tracking. +If enabled it'll return `PR_INDIR_BR_LP_ENABLE` + +`PR_LOCK_INDIR_BR_LP_STATUS`: 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 `SIGSEV`` with code = `SEGV_CPERR` and follow +normal course of signal delivery. From patchwork Fri Mar 29 04:44:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13610070 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 4046CCD1283 for ; Fri, 29 Mar 2024 04:47:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9EDC66B00C2; Fri, 29 Mar 2024 00:47:12 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 99D686B00C5; Fri, 29 Mar 2024 00:47:12 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7A2E36B00C6; Fri, 29 Mar 2024 00:47:12 -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 5796C6B00C2 for ; Fri, 29 Mar 2024 00:47:11 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 29240A178A for ; Fri, 29 Mar 2024 04:47:11 +0000 (UTC) X-FDA: 81948842262.24.3679DAD Received: from mail-oo1-f54.google.com (mail-oo1-f54.google.com [209.85.161.54]) by imf08.hostedemail.com (Postfix) with ESMTP id 6A7D1160012 for ; Fri, 29 Mar 2024 04:47:09 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=sUPMyesV; dmarc=none; spf=pass (imf08.hostedemail.com: domain of debug@rivosinc.com designates 209.85.161.54 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1711687629; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=9MOz30J7VxjH8ZagxzRr7bVMOq1rQVKKUTnSRMNh/j0=; b=br1xSHf+4vBp2B/xFiB4PKr9W1h0esNiiusmN0MqkRqUY/U9GC+D3xI/U5XudtEE3MZLi/ FKn51YXYnZWIFQeyfFTejVcGaCi2ewKKfeqXjwtVW+jNj34YzCaDLZ9Urj8C2W6N8CimSb c0tmozNkfKyFpYTR2nQ8MUQSWDkR9/k= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=sUPMyesV; dmarc=none; spf=pass (imf08.hostedemail.com: domain of debug@rivosinc.com designates 209.85.161.54 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711687629; a=rsa-sha256; cv=none; b=katIqOlWyx5FjJ+aAJfdTUU3GjC08qNO4I3mt2JlggTRXRn2ceb3vV+WH4CFWLG9ACs+hL Xtd9m3/E6y+n8pIHE3GIyHmu9H5yl7zBIk1ENQDKtPrhjynbA+Z72J23MOoztjnmt344kv W72KGH/U1C6AQVoTcV+ycjYn85Bh+zk= Received: by mail-oo1-f54.google.com with SMTP id 006d021491bc7-5a4859178f1so1040348eaf.0 for ; Thu, 28 Mar 2024 21:47:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1711687628; x=1712292428; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=9MOz30J7VxjH8ZagxzRr7bVMOq1rQVKKUTnSRMNh/j0=; b=sUPMyesVJMMtbbUVSx2FNPUzcJfEpzp27LgoZ4TSXOr1x/UbDMXVim5qrfEIIutqXq fWlev5QrEk/xbiUuyRuuWT11qXxDhuuBqfNpDkzxcs/9lmgV5jjMc/2Zn4hVkn3RLhdZ GZCvMUFYUOSKsdu31Jesh8KZEFPAJNW4YFQ7IlZiWGzYYN5ywAlDf5E15ZBlgzfNlT9L XOzCOK7irJX8ezAiOoWE4+7/Wj1q9r1u1aXvlRUt46QBBo0RwBdKim/qRkHKhX5WdOzm rfzWSQlTrvjMj53STh0XnjUxOutdf6BHIy3B6Z95W/Dbl5xOarbkEuMUUkk6+1J5qese r28w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711687628; x=1712292428; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=9MOz30J7VxjH8ZagxzRr7bVMOq1rQVKKUTnSRMNh/j0=; b=D/vpNoA/r3YXZuXFxPlIqRPb7y8uoL+REIPp64hopO09uAcVUE1sPDXgj1FZ31ISp5 7ZfgWOtxh1K/nbfrUJoTd7YnASjcp+A3jXgaU6pMJ1FFx26UnIoodl9SsSNwVVfu8DeJ 3ZO/32o+gM23XflkLHa8QddJoxacYhznUCWkHhMr7veD4Ww+F6Oi4jV+Yd9kJq7dsgUO M591ohzZbqtpmSUyAifn4hTEUEApMlukbyYzqSFtKV1VhqhCt6+MauGO4PHQf9haMTIh zrwmy7MsQtgm0uK5YOrSin1GsKgoo/UyiN2YNmcgKfV0qPEn7h+p+hdWR+DSytSVFlCd 39nA== X-Forwarded-Encrypted: i=1; AJvYcCXDvmtN44zU8iOaVhIrmMrpgZEYHgmdzCCZ3/Pqqb4n528To9UKDfh7hantyDwsgZPWh4nQrnQ7SO2lRjt0un35BIk= X-Gm-Message-State: AOJu0YwOyUHzt3KxCciYUBftczRWU5kX2asqoRx3bd86Cw5hGrst+jS4 SsJ3Rd/fgfTlee54Myb80hbfEqpUPBJXDWp5RszEECtZgkDObXo77cQUcRZjLnk= X-Google-Smtp-Source: AGHT+IGB+y7twcpxmoV8AXjju7qFPWK1mID4DF6lwVek2eRahswiUwgjJa6zI5wt5U4lG69wDyeurg== X-Received: by 2002:a05:6871:410b:b0:22a:9e26:4789 with SMTP id la11-20020a056871410b00b0022a9e264789mr1115705oab.13.1711687628350; Thu, 28 Mar 2024 21:47:08 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id i18-20020aa78b52000000b006ea7e972947sm2217120pfd.130.2024.03.28.21.47.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 21:47:07 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com, samuel.holland@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, tech-j-ext@lists.risc-v.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh+dt@kernel.org, krzysztof.kozlowski+dt@linaro.org, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, Liam.Howlett@oracle.com, vbabka@suse.cz, lstoakes@gmail.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, hankuan.chen@sifive.com, greentime.hu@sifive.com, evan@rivosinc.com, xiao.w.wang@intel.com, charlie@rivosinc.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, dbarboza@ventanamicro.com, sameo@rivosinc.com, shikemeng@huaweicloud.com, willy@infradead.org, vincent.chen@sifive.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, gerg@kernel.org, heiko@sntech.de, bhe@redhat.com, jeeheng.sia@starfivetech.com, cyy@cyyself.name, maskray@google.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bgray@linux.ibm.com, mpe@ellerman.id.au, baruch@tkos.co.il, alx@kernel.org, david@redhat.com, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, shr@devkernel.io, deller@gmx.de, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com Subject: [PATCH v2 26/27] riscv: Documentation for shadow stack on riscv Date: Thu, 28 Mar 2024 21:44:58 -0700 Message-Id: <20240329044459.3990638-27-debug@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240329044459.3990638-1-debug@rivosinc.com> References: <20240329044459.3990638-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 6A7D1160012 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: 8b7wie6nfdux5qw5i1umhtw3hgaiwu13 X-HE-Tag: 1711687629-652005 X-HE-Meta: U2FsdGVkX1+1yBrAXWitHphSqs+7AuISXOZgtqeTt98d1VI7ltqvfL6eNsMY06mvqGpF67gwfdaQOC0BzRpZ+riDBTg6Ex8X3Hw1RsVW62tfa566uMKusKvtuxgiB9NYFEVeK3hJAMMXsfpdkUHBCksFdQc6qu4eqsXdMd/V4Bf/R8uYtdyjM3i7ecgDrw6u3BsKp/54+40tIpow8QWBZ8PpIgs2RMdiAYdhgf/0t4HpDeRGV136op6yopAYccjD9GPFUTu6RF5cc6B2d/9Hxi6h9liW1p7QMNWDIkDt0MK6Pau45tNk8zCWKVsoTRv24yO8DDjQiGEv9mMQdzd2E9fGFE+f8WZIL2/y0g5kzzCMaybDVNt7334JBN3Nl2eX2I6+XvGRnGFyQm8eWamkDajmrP8mrqGid7GTyGcDyNqLlH6hSgzLu3zHPHnoHO9jsWiwbb2VHUvk2cjGY8isQ1w0FrTvCOMZg9OIXnDXxDp9bjNs0Oj0uyF5fXdFZj8FwGlwWkgFkPyJlynwexVbZZU96+SDz3/KHkNmWTxP3q7TKWFb3yN0vZm8O2mOCbKO9MSG/S530i0GhwkoEnf3HbhcG/ScbuT9tsPRj3NHwcnmB7SDDi24xSod9X6s1S87UHF/Dj0IhVd35PoB94Zi5PW3CPPhRS68xeYfEDfut5L9cdqj38iAIH+1vHKnC8/BXiBUgzlHBr1P9yK0X1qunmHLm81YtfY/XUrQii6X1ysitRB/czEr13dYpcBQ1c1SxOnavC/g7x70L/BVNk63mswEAhpGPnI104uBFupF+OP1gAYyHuVCVhBGMAU7LDKyJ7shfH5uaKistHvz4VEGOZcQV65cyuSXRFSlbYaQbxexvjcj+vVA2vDy4MHdHHc0N17kMSVMqOMIE0wTG/Zkq5YtaWIiTofcqGUjuHlkMQEeTixyWDeYwWVlwtUk9TDULiQbmc2i166pVsFl3iK udVkmSg+ AAf6xSWQrithECl6e5OdvRF4P/cEVQV2ZPwqCz33eTOwOGA4rDVAOB3UrkZ5gO16maCh3wMVMDb8SfLUq7RAi06P6qEf3e1j1EUagy0xROtdN1WXOLWQwvFbL34pUr5KxDRZ0PPKgCjw258Jzy/G0B++EJ/HW3ox9V0JDN6bu5qiAmQR50w7PJtctp+Xr6U6bxgXFutpf1q9I/TRiAJOAL6YftD83qOd/sxZXn21PM7bkqdk= 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/zicfiss.rst | 169 +++++++++++++++++++++++++++ 1 file changed, 169 insertions(+) create mode 100644 Documentation/arch/riscv/zicfiss.rst diff --git a/Documentation/arch/riscv/zicfiss.rst b/Documentation/arch/riscv/zicfiss.rst new file mode 100644 index 000000000000..f133b6af9c15 --- /dev/null +++ b/Documentation/arch/riscv/zicfiss.rst @@ -0,0 +1,169 @@ +.. 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 +`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 prologs have `sspush x1/x5` to save return +address on shadow stack in addition to regular stack. Similarly function epilogs have +`ld x5, offset(x2)`; `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 `GNU_PROPERTY_RISCV_FEATURE_1_BCFI` for property +`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 +-------------------- + +`PR_SET_SHADOW_STACK_STATUS` / `PR_GET_SHADOW_STACK_STATUS` / +`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. + +`PR_SET_SHADOW_STACK_STATUS`: If arg1 `PR_SHADOW_STACK_ENABLE` and if CPU supports +`zicfiss` then kernel will enable shadow stack for the task. Dynamic loader can +issue this `prctl` once it has determined that all the objects loaded in address +space have support for shadow stack. Additionally if there is a `dlopen` to an +object which wasn't compiled with `zicfiss`, dynamic loader can issue this prctl +with arg1 set to 0 (i.e. `PR_SHADOW_STACK_ENABLE` being clear) + +`PR_GET_SHADOW_STACK_STATUS`: Returns current status of indirect branch tracking. +If enabled it'll return `PR_SHADOW_STACK_ENABLE` + +`PR_LOCK_SHADOW_STACK_STATUS`: 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 `SIGSEV`` with code = `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 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 `sigreturn`. Adversary will have to +make sure that there is a `shadow stack token` in addition to invoking `sigreturn` + +7. Signal shadow stack +----------------------- +Following structure has been added to sigcontext for RISC-V. `rsvd` field has been kept +in case we need some extra information in future for landing pads / indirect branch +tracking. It has been kept today in order to allow backward compatibility in future. + +struct __sc_riscv_cfi_state { + unsigned long ss_ptr; + unsigned long rsvd; +}; + +As part of signal delivery, shadow stack token is saved on current shadow stack itself and +updated pointer is saved away in `ss_ptr` field in `__sc_riscv_cfi_state` under `sigcontext` +Existing shadow stack allocation is used for signal delivery. During `sigreturn`, kernel will +obtain `ss_ptr` from `sigcontext` and verify the saved token on shadow stack itself and switch +shadow stack. From patchwork Fri Mar 29 04:44:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13610071 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 43AE6C6FD1F for ; Fri, 29 Mar 2024 04:47:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AB2E46B00C5; Fri, 29 Mar 2024 00:47:14 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A3D316B00C7; Fri, 29 Mar 2024 00:47:14 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 83F266B00C8; Fri, 29 Mar 2024 00:47:14 -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 5FD656B00C5 for ; Fri, 29 Mar 2024 00:47:14 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 1E4A8C087E for ; Fri, 29 Mar 2024 04:47:14 +0000 (UTC) X-FDA: 81948842388.06.697AFFC Received: from mail-oi1-f180.google.com (mail-oi1-f180.google.com [209.85.167.180]) by imf26.hostedemail.com (Postfix) with ESMTP id 468BD140006 for ; Fri, 29 Mar 2024 04:47:12 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=SyNTEMSF; dmarc=none; spf=pass (imf26.hostedemail.com: domain of debug@rivosinc.com designates 209.85.167.180 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1711687632; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=3tG+Jx5/gzFczlI2wB8kX45jgH5TbH7NSGm/DUHiBYE=; b=1Kc1Pjuwk4YPa5G7oPJIES179ljAImksawuXXDIJ7iM8EfCvUsmtNQrg3uw6nlknjo0ARP 1WERS2c1xPO02YicofVD46ngJq1UcCB1qYFCCOTqLpXz1TMYZfcYFshIy+NQ5wBeG4JRJ4 76bywjmo3rowVK4SuaOZCy/jV6wYymk= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=SyNTEMSF; dmarc=none; spf=pass (imf26.hostedemail.com: domain of debug@rivosinc.com designates 209.85.167.180 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711687632; a=rsa-sha256; cv=none; b=N6sEnDSQosH7xnBSSZQKDSfWi2d4nH36Ra0Yrotb9cCzqeHPCgR7GIUepTa+pCeD57Oy/D 0bL00tlNcWZnZi85BVSTSgqIK2vN+rpGvukx34OHkmhL+oMNWwBeVR/c3x9PWRNb0Yqkr2 6D8ghO7o2fr2NrcegxUmC2ZrPSBwfcE= Received: by mail-oi1-f180.google.com with SMTP id 5614622812f47-3c3f3806d88so90780b6e.3 for ; Thu, 28 Mar 2024 21:47:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1711687631; x=1712292431; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=3tG+Jx5/gzFczlI2wB8kX45jgH5TbH7NSGm/DUHiBYE=; b=SyNTEMSFz/jmD4V7yBZGNPfiPwWWYp5yS9O6IXf/glKc9OsQKT5fxwGHHSQu9s446j abJaf5Jqb5hOa5yhNK12e4whsxYfhCCIA9UrKtrC3Xki2Z5Ajlns02nPw6KYhVJFFO4f 2xGn49/iDQ0o/bUc1JWPYNXjacqubWeCloywxylEvOU0sVgc0CfgJs86pz1mEkWwNeSQ 9lTxBWUQ3M4jGfoobNHPTkOE9PC5GcM8gdeNP1Fj91QUrWAtobm1dukLJfecbEi7Jptz Iut7UbwXv2Y+9ShnARR2+dggVib+TJ6koKFzpfmK7UiJy03dSbBY35VHmOBxdTxJoBI0 2HuA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711687631; x=1712292431; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=3tG+Jx5/gzFczlI2wB8kX45jgH5TbH7NSGm/DUHiBYE=; b=Qg3FcILWvze6u384A/gKMo61UPdYKY+qLLMb+UZ9kU9Djd/DVyeL7b0Tc0apC7vb/K apCwhUHDGfgoQ07nbgOJ8cpn7CqT6R3vGXb34S+qCpkgJQ6ePuCstyyASxzr9x+24ZOL 2djfSEV+s5p23KoLs64XP/HP27AS9zkfuc9v/OFwBYkJYnlMS9wqLQw7kjV460IbAnKk g76Ri+AP4QBBXPICmubXS2Kd08kXDOjVWf6h7ptY0T7MN5//i9hY73sjgdUL/l9MYBuk 4huSgSxV9eeumc6P3sMPaDS5G6+57jw8BmobusRXhdqZ4rAS7ui7uGGFz8X5XNp1OMfI PndQ== X-Forwarded-Encrypted: i=1; AJvYcCVdMlExB74a9GqigI+bwOLxPMViJU4uOwdmIriwIdfB+BZsXcgK59zmfF9KvbM1meVQLMYIgTo0oM4I3173Ac5q8ew= X-Gm-Message-State: AOJu0Yyx+eotxFsbysBSOBIdFXCNEDYL8tmLvEXXgBcOxU99uivt7wxt 8YgYTf9uPqHcQ34cKug2c2+AIIbsMcNV0Hp+iMO7nAH8/vat1CEBExQ0QW3wm6w= X-Google-Smtp-Source: AGHT+IEylrMRw+RDZjCdkhLpY+5Rfn62pYEHlVD8jofAxi9f0lRwzxXK0jjjWZ+UZ/V6TYwT8xg2dw== X-Received: by 2002:a54:450d:0:b0:3c3:a9ca:82bf with SMTP id l13-20020a54450d000000b003c3a9ca82bfmr1101481oil.57.1711687631288; Thu, 28 Mar 2024 21:47:11 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id i18-20020aa78b52000000b006ea7e972947sm2217120pfd.130.2024.03.28.21.47.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 21:47:10 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com, samuel.holland@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, tech-j-ext@lists.risc-v.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh+dt@kernel.org, krzysztof.kozlowski+dt@linaro.org, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, Liam.Howlett@oracle.com, vbabka@suse.cz, lstoakes@gmail.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, hankuan.chen@sifive.com, greentime.hu@sifive.com, evan@rivosinc.com, xiao.w.wang@intel.com, charlie@rivosinc.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, dbarboza@ventanamicro.com, sameo@rivosinc.com, shikemeng@huaweicloud.com, willy@infradead.org, vincent.chen@sifive.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, gerg@kernel.org, heiko@sntech.de, bhe@redhat.com, jeeheng.sia@starfivetech.com, cyy@cyyself.name, maskray@google.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bgray@linux.ibm.com, mpe@ellerman.id.au, baruch@tkos.co.il, alx@kernel.org, david@redhat.com, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, shr@devkernel.io, deller@gmx.de, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com Subject: [PATCH v2 27/27] kselftest/riscv: kselftest for user mode cfi Date: Thu, 28 Mar 2024 21:44:59 -0700 Message-Id: <20240329044459.3990638-28-debug@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240329044459.3990638-1-debug@rivosinc.com> References: <20240329044459.3990638-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 468BD140006 X-Stat-Signature: 9ubbcretg68esjnqq83766acaagdirt6 X-HE-Tag: 1711687632-872203 X-HE-Meta: U2FsdGVkX1/CEUoPBgYTTCtzsJOgWSHL8rX+qGBP/EYQvr5nFHBsem26Xf3izJ4jKiOfQf0sdF5cyDW/BXNbGJsEzPwal5Y+/FeRAJ4a4xl6Ls5F5QMTXL0LpuqqJR79PHrkmGtLCffPquC7OW8yzIC1FaPi4EAgc3APV6Wh2Uqdw4+rfum8NOMN/iDZHhRZO2g2y4qP1CLAdDukpaoqnwwrD7f53t4hNG56ipLYkEeflqEzM3QoRJdjHiQLjF7RIoUndLFZ0u981jnLe2MMQ9tjli+GCqTUYgvuhwnA5Rb4yiTraD0tkqV9BsbHg8oYXYPINxeuDLYnWZgCuPPA5GjTtbt36pK/Dkz21MnqrGzYv4QLpW6xfE/OS+ZKXPIJGzsiXmg1wrEmeQZNuMEONmQubpQYGVesnKfODaDbjTYenWOo/5rouMAsN2RiSsVNI5zQvXlMrPtqNzzrBAguhkpaMR1woG72fzCpQH4csqZBWvToWdRmygyNJeUlVtbdr2Xir2f1ybi89c4crq4yVpkFcqkqZvHcwKqk+j+0hp5b21QD6PJbs8JggKeuoFj4EYy/Pbkk7GQjpbEaaJXoZ8rVNb7evO0BLfjXCFObw9KX6FXVoDr/OFmspZYKmM+ebJM3QHZeVIN04cFmpETrt6aUirx2ICBpfe/J4LMgzxysGgvJMdLRNBaiamq5P7I9ydpkt5RNzbi/1TnKtDBdKBJRyHs57dE3a7fAt9V7cpRC1knf34jwB5SzZ7mM5e7FiXAtv0gm9ABv+dgDaBoSLiXrh2rfaNofZH3c8r8+eE5jhhSr6096leK1pxj6wWkKR+yjf/oP4gcehckSga91To5rVw5/v6ZDrDBBVlOMEJ9wdQkrq1LipbSRH2+TTs5e66sdDBAwWg27dosiZZpfOw57v3nGHYWA0teAnQQgEzPT8Fnd6dCR/vKXzHNBQryTftw7X8IHgFdXjcJmzt+ wZi2x3yq M7dKp1MAAnZwPuWN1qHr7f4TZ8dWDuRXPqMxjgyStVUfChyi0SyLiHHk+nstKdPLJ1o5ew2nanu/8xE8ysTuGPtllFUIipWKutlrQqcQwhJMXRwQTlDWalMRvLJyALpZFcC/04vezs+p1yLW0A1PXjmu6IzwYJbS3Tq+5AV+5ydvLPydjZnU6ps0slbY84ryjO7Pb5TtJMYMcVehOI36YZ/M1ROnC3WeMC+ANO5aK2CP603FOXdRNsZoG13ye0XkjPuvXu+xg8OTxATEs5NP8TO+dv2gDRT86a2ld5O/L9BnuKwo8nEnCD96uU2JqSu0fOHjmd/vZVOO76zG2UG0l5u8q7u4rMBPh9shLzjdvlQBhw2CuLjK/MrRJV+sp6UnPEfom0sQ1yKRlAeiBjhNNrE+AM+RprZ0Hx5SPu8adHNVE32HlnvTzdDpTVTBKH7C+qTcIEmte27LFVcZEeC/3ALYMe8EoZSV31+iL4vYch/2SgmwLGrxQ2ntMZdjO2zIN7TnL6FobP2eGycA= 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. Thus kselftest simply enables landing pad for the binary and a signal handler is registered for SIGSEGV. Any control flow violation are reported as SIGSEGV with si_code = SEGV_CPERR. Test will fail on recieving any SEGV_CPERR. Shadow stack part has more changes in kernel and thus there are separate tests for that - enable and disable - Exercise `map_shadow_stack` syscall - `fork` test to make sure COW works for shadow stack pages - gup tests As of today 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 Signed-off-by: Deepak Gupta --- tools/testing/selftests/riscv/Makefile | 2 +- tools/testing/selftests/riscv/cfi/Makefile | 10 + .../testing/selftests/riscv/cfi/cfi_rv_test.h | 85 ++++ .../selftests/riscv/cfi/riscv_cfi_test.c | 91 +++++ .../testing/selftests/riscv/cfi/shadowstack.c | 376 ++++++++++++++++++ .../testing/selftests/riscv/cfi/shadowstack.h | 39 ++ 6 files changed, 602 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/riscv/cfi/Makefile create mode 100644 tools/testing/selftests/riscv/cfi/cfi_rv_test.h create mode 100644 tools/testing/selftests/riscv/cfi/riscv_cfi_test.c create mode 100644 tools/testing/selftests/riscv/cfi/shadowstack.c create mode 100644 tools/testing/selftests/riscv/cfi/shadowstack.h diff --git a/tools/testing/selftests/riscv/Makefile b/tools/testing/selftests/riscv/Makefile index 4a9ff515a3a0..867e5875b7ce 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 +RISCV_SUBTARGETS ?= hwprobe vector mm cfi else RISCV_SUBTARGETS := endif diff --git a/tools/testing/selftests/riscv/cfi/Makefile b/tools/testing/selftests/riscv/cfi/Makefile new file mode 100644 index 000000000000..77f12157fa29 --- /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) -static -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..27267a2e1008 --- /dev/null +++ b/tools/testing/selftests/riscv/cfi/cfi_rv_test.h @@ -0,0 +1,85 @@ +/* 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 BAD_POINTER (NULL) + +#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..c116ae4bb358 --- /dev/null +++ b/tools/testing/selftests/riscv/cfi/riscv_cfi_test.c @@ -0,0 +1,91 @@ +// 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") + +#define SEGV_CPERR 10 /* control protection fault */ + +void sigsegv_handler(int signum, siginfo_t *si, void *uc) +{ + struct ucontext *ctx = (struct ucontext *) uc; + + if (si->si_code == SEGV_CPERR) { + printf("Control flow violation happened somewhere\n"); + printf("pc where violation happened %lx\n", ctx->uc_mcontext.gregs[0]); + exit(-1); + } + + /* null pointer deref */ + if (si->si_addr == BAD_POINTER) + exit(CHILD_EXIT_CODE_NULL_PTR_DEREF); + + /* shadow stack write case */ + exit(CHILD_EXIT_CODE_SSWRITE); +} + +int lpad_enable(void) +{ + int ret = 0; + + ret = my_syscall5(__NR_prctl, PR_SET_INDIR_BR_LP_STATUS, PR_INDIR_BR_LP_ENABLE, 0, 0, 0); + + return ret; +} + +bool register_signal_handler(void) +{ + struct sigaction sa = {}; + + sa.sa_sigaction = sigsegv_handler; + sa.sa_flags = SA_SIGINFO; + if (sigaction(SIGSEGV, &sa, NULL)) { + printf("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; + + ksft_print_header(); + + ksft_set_plan(RISCV_CFI_SELFTEST_COUNT); + + 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_skip("Get landing pad status failed with %d\n", ret); + + ret = lpad_enable(); + + if (ret) + ksft_exit_skip("Enabling landing pad failed with %d\n", ret); + + if (!register_signal_handler()) + ksft_exit_skip("registering signal handler for SIGSEGV failed\n"); + + ksft_print_msg("landing pad enabled for binary\n"); + ksft_print_msg("starting risc-v shadow stack tests\n"); + execute_shadow_stack_tests(); + + ksft_finished(); +} + +#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..126654801bed --- /dev/null +++ b/tools/testing/selftests/riscv/cfi/shadowstack.c @@ -0,0 +1,376 @@ +// 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, swaped_val = 0; + + ssp = csr_read(CSR_SSP); + printf("inside %s and shadow stack ptr is %lx\n", __func__, ssp); +} + +void bar(void) +{ + printf("inside %s\n", __func__); + zar(); +} + +void foo(void) +{ + printf("inside %s\n", __func__); + bar(); +} + +void zar_child(void) +{ + unsigned long ssp = 0; + + ssp = csr_read(CSR_SSP); + printf("inside %s and shadow stack ptr is %lx\n", __func__, ssp); +} + +void bar_child(void) +{ + printf("inside %s\n", __func__); + zar_child(); +} + +void foo_child(void) +{ + printf("inside %s\n", __func__); + 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) +{ + if (parent) + printf("call test for parent\n"); + else + printf("call test for child\n"); + + (fn_ptr)(); + + return 0; +} + +bool enable_disable_check(unsigned long test_num, void *ctx) +{ + int ret = 0; + + if (!my_syscall5(__NR_prctl, PR_SET_SHADOW_STACK_STATUS, PR_SHADOW_STACK_ENABLE, 0, 0, 0)) { + printf("Shadow stack was enabled\n"); + shadow_stack_call_tests(&foo, true); + + ret = my_syscall5(__NR_prctl, PR_SET_SHADOW_STACK_STATUS, 0, 0, 0, 0); + if (ret) + ksft_test_result_fail("shadow stack disable failed\n"); + } else { + ksft_test_result_fail("shadow stack enable failed\n"); + ret = -EINVAL; + } + + return ret ? false : true; +} + +/* 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; + + printf("exercising shadow stack fork test\n"); + + if (my_syscall5(__NR_prctl, PR_SET_SHADOW_STACK_STATUS, PR_SHADOW_STACK_ENABLE, 0, 0, 0)) { + printf("shadow stack enable prctl failed\n"); + return false; + } + + parent_pid = getpid(); + pid = fork(); + + if (pid) { + printf("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) { + printf("waiting on child to finish\n"); + wait(&child_status); + } else { + /* exit child gracefully */ + exit(0); + } + + if (pid && WIFSIGNALED(child_status)) { + printf("child faulted"); + return false; + } + + /* disable shadow stack again */ + if (my_syscall5(__NR_prctl, PR_SET_SHADOW_STACK_STATUS, 0, 0, 0, 0)) { + printf("shadow stack disable prctl 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; + + shdw_addr = my_syscall3(__NR_map_shadow_stack, NULL, SHADOW_STACK_ALLOC_SIZE, 0); + + if (((long) shdw_addr) <= 0) { + printf("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) { + printf("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; + + shdw_addr = my_syscall3(__NR_map_shadow_stack, NULL, SHADOW_STACK_ALLOC_SIZE, 0); + + if (((long) shdw_addr) <= 0) { + printf("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) { + printf("write suceeded\n"); + return false; + } + + /* if child reached here, then fail */ + if (!pid) { + printf("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) { + printf("child wasn't signaled for write on shadow stack\n"); + return false; + } + + ret = munmap(write_addr, SHADOW_STACK_ALLOC_SIZE); + if (ret) { + printf("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) { + printf("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) { + printf("writing shadow stack mem via gup failed\n"); + return 1; + } + + if (*shdw_addr != SS_MAGIC_WRITE_VAL) { + printf("GUP write to shadow stack memory didn't happen\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; + + shdw_addr = my_syscall3(__NR_map_shadow_stack, NULL, SHADOW_STACK_ALLOC_SIZE, 0); + + if (((long) shdw_addr) <= 0) { + printf("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)) { + printf("gup tests failed\n"); + goto out; + } + + ret = true; +out: + if (shdw_addr && munmap(write_addr, SHADOW_STACK_ALLOC_SIZE)) { + printf("munmap failed with error code %d\n", ret); + ret = false; + } + + return ret; +} + +volatile bool break_loop; + +void sigusr1_handler(int signo) +{ + printf("In sigusr1 handler\n"); + break_loop = true; +} + +bool sigusr1_signal_test(void) +{ + if (signal(SIGUSR1, sigusr1_handler) == SIG_ERR) { + printf("registerting sigusr1 handler 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; + unsigned long ssp = 0; + + if (my_syscall5(__NR_prctl, PR_SET_SHADOW_STACK_STATUS, PR_SHADOW_STACK_ENABLE, 0, 0, 0)) { + printf("shadow stack enable prctl failed\n"); + return false; + } + + pid = fork(); + + if (pid == -1) { + printf("signal test: fork failed\n"); + goto out; + } + + if (pid == 0) { + /* this should be caught by signal handler and do an exit */ + if (!sigusr1_signal_test()) { + printf("sigusr1_signal_test failed\n"); + exit(-1); + } + + while (!break_loop) + sleep(1); + + exit(11); + /* child shouldn't go beyond here */ + } + /* send SIGUSR1 to child */ + kill(pid, SIGUSR1); + wait(&child_status); + +out: + if (my_syscall5(__NR_prctl, PR_SET_SHADOW_STACK_STATUS, 0, 0, 0, 0)) { + printf("shadow stack disable prctl failed\n"); + return false; + } + + 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; + + printf("Executing RISC-V shadow stack self tests\n"); + + ret = my_syscall5(__NR_prctl, PR_GET_SHADOW_STACK_STATUS, &shstk_status, 0, 0, 0); + + if (ret != 0) + ksft_exit_skip("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)) { + ksft_test_result((*shstk_tests[test_count].t_func)(test_count, NULL), + shstk_tests[test_count].name); + test_count++; + } + + 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..92cb0752238d --- /dev/null +++ b/tools/testing/selftests/riscv/cfi/shadowstack.h @@ -0,0 +1,39 @@ +/* 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 enable_disable_check(unsigned long test_num, void *ctx); +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[] = { + { "enable disable\n", enable_disable_check }, + { "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