From patchwork Wed Apr 3 23:34: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: 13616765 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 49E82CD1292 for ; Wed, 3 Apr 2024 23:41:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D2B856B009A; Wed, 3 Apr 2024 19:41:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C8C956B009B; Wed, 3 Apr 2024 19:41:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B2F556B009C; Wed, 3 Apr 2024 19:41:36 -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 9576F6B009A for ; Wed, 3 Apr 2024 19:41:36 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 557C71C0D69 for ; Wed, 3 Apr 2024 23:41:36 +0000 (UTC) X-FDA: 81969844992.10.0A40D28 Received: from mail-pf1-f169.google.com (mail-pf1-f169.google.com [209.85.210.169]) by imf30.hostedemail.com (Postfix) with ESMTP id 5DE0680006 for ; Wed, 3 Apr 2024 23:41:34 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=klz58drD; dmarc=none; spf=pass (imf30.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.169 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1712187694; 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=B1lL5f6yq9LaiF8GnxAfpEDQQL4kAkav9nCQOWb1C3Y=; b=mUG7+4OYra+2dhSpP64M17n2+bFQcSm78o+m+Sy/sxHtI9O879iJLtmXL0o6boH3h6jOTq a4lvsRvK09bPYm+quwzD9T4h6+jUJpDKrZxYaxX0qnzbJyotBgYh1Da7xkdCkQUVRR0+t+ 8ZCLLWlZk7L8QlSZPCfBEZcHH+TDw90= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=klz58drD; dmarc=none; spf=pass (imf30.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.169 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712187694; a=rsa-sha256; cv=none; b=PiJMHrljlttyaj7EQyPIW9hZNIl64xC8iZP+I89C8dWvlnG4nZjq+FaC8ewIABdNqfazq3 iPYGB2UFfw9AG42hzPCfKTQUM0dj+OcDUOzvnt84OGxdQd3mjCzJLaafESBfiwdhQ6joLf M8r5qbtTzflgta/fyx9yhY8szK6+cOs= Received: by mail-pf1-f169.google.com with SMTP id d2e1a72fcca58-6eced1dab1fso27702b3a.1 for ; Wed, 03 Apr 2024 16:41:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1712187693; x=1712792493; 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=B1lL5f6yq9LaiF8GnxAfpEDQQL4kAkav9nCQOWb1C3Y=; b=klz58drDpBRD0zaYLftvO1P9XhqT7veg/8xAeK8aTkmCLf+4b85mMOoaXrPMnikkMM Hn+2nuSE4BWVqb+cMmtataOLFFn7D9OA9Yu56jNl/W6YrltSp74f6nXhb3pQl+6k85XI ujsyR77YvuqsbHeaJhIjPImLoSqsNLGpfKb442pHOkjDWZzpX6HhalknIg/bGobtEgP9 D5jovzJjs+MfSoVmms/gDS4fNJf+1Blp5DHekibxeO8jqwkeRnWfxPfJUIJGYOt+5cgw jJzJeq/rIlYosjwUq4SVwKdWlPyGIon1/BOIRvfVaxssJMygjMo7X6jDK7SKjeeqJ/SR TSDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712187693; x=1712792493; 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=B1lL5f6yq9LaiF8GnxAfpEDQQL4kAkav9nCQOWb1C3Y=; b=MxsfHUlqryK9YSHu8tpDpFfmEeTuzQklAjrneDvjoCDR87i9KrGxJ4M5QGB5o9UQ6x 7W+3ttIoyTfbC3tNwMeZQX5WHX2Gkkl8B69esDoc6msHNmxLiFrvbxmqB4Mgbo9aoZWA hCkBCcO1uRiz5cZ+dClKiEA+KhEiPonXtLei7/KZncMJCFgDFbJ6NsF7vykKRfmaReYN feNwh3T1ZSxfn7Oijqga0iTovif0NHTdLTd5EJ+rV3u9IJSkUMBVub9T1RWmUxgKesEz xgD5Z/Aopbp7zt+r5UohB+GzPpoP/lGjQqB7N/2j5zsxm3VozQ0bW4abqJxizuwCTCA7 nMMg== X-Forwarded-Encrypted: i=1; AJvYcCXl8pkUf3+ITPsnNbKaxer/vnrbc4tFNpmz/U0xJOtH6w3XQDaNXWHbtE4IuGNXuUkIuLLAmSD6mBG6ZwuC+kHBYQs= X-Gm-Message-State: AOJu0Yy1X3Tb6G4asuaExaJZACdn2QcBI3o0nMeOU4p1qu/ioI7N6qCr +RahjAaKdLoNlRcs9sxrcx90tELMmoPN0SBTtyjiwFW2Whg2U/mEsXPnhG5HAGA= X-Google-Smtp-Source: AGHT+IExKU8/z/0Uf5Y5wAMW8Wut8qP6zJCIHgHh0jSkN2g23nIAgrJWZfYYYsTzy3HfTPFShjsbrQ== X-Received: by 2002:a05:6a20:de16:b0:1a7:1df5:1699 with SMTP id kz22-20020a056a20de1600b001a71df51699mr975407pzb.27.1712187693134; Wed, 03 Apr 2024 16:41:33 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id b18-20020a170902d51200b001deeac592absm13899117plg.180.2024.04.03.16.41.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 16:41:32 -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, conor@kernel.org Cc: 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, corbet@lwn.net, 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 v3 01/29] riscv: envcfg save and restore on task switching Date: Wed, 3 Apr 2024 16:34:49 -0700 Message-ID: <20240403234054.2020347-2-debug@rivosinc.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240403234054.2020347-1-debug@rivosinc.com> References: <20240403234054.2020347-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 5DE0680006 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: soyt8zqguao8t5ti1qe99zh8irbkj6nr X-HE-Tag: 1712187694-531152 X-HE-Meta: U2FsdGVkX18tTdMv0qKj9XhUMEQR+sWguHI0kX0IY13yCvCTlF9kIMBkL0xxdqkKNIXyYrfHm6f2VcoLCMaSp5qSd+0cu92HcS/ZbnQFQ810pI+p3fr+501t+Drcf7ukmtCDhrPNgWJQD80l1Z6lXl027eg4UFmG5Ga5FIDKVCjXAHa0xx3ZGYE3wcG0gDvoHKg6HrQwRRz1m82GNBFwRYg1w2z2oqhg3OCT9vOn7PQ6cIC9QtpEwXD9ipOgp1yawFjC7SvsdOLKMIzlZd4xvIWIXn8vA3DdHZP7cPnGBgVdRWpYsyRddCLvl6jQN6sbC7ktYkn7UgIfCGB7tmgqW+zMHrVGvYwzsQkqAaQ1Dy/EnOM3QzFJTmDofC19UBa5gWiRdaYwF8fuTj5UXJiLOM6aL76gzqLkesizMtLhrbfZ38U6P7VaFrB1NUci5ChAqGr5E5hGtVRMFSNnssHeeXVT3YvM8G5XYLkXBK+3aqlunbEX5Gmq5d0ZOo3otu5FasGpbv6yRaJPRnRvCFZk7kbu3t10JwdSfbg7dV95M/oILOgbN/uT0n9+2WM2nMXd+RRltIkofx3gBJ5FFTKqlFGFT/Bbk+Tj6EDAg+b6GqGycSuXb4cQE3yGNmZrHe59ighG7sijp+cqwnAnnbU/XAVYf0zzaUzP1Gy5YJTf0mODGJ4q59DJQXR0HVEn0gXq6Hqp+JoILYUgZX3emNB7go2X8Uj5Ud+jCyN7WYymoUiwfy6BhbgYcLrm2RG8DTnEPaTbcvPl3L+e/T+BSG9wQoB3wdE8qLr5xc4EzbyyuHQJhELI3syJSal1YdS4w0L8Rl4HgJms9Y7TCklAhnM6kTxmx6oPYrb04M6JghKH7pBO7TwFqVd/QzNeEHkt21VRZ5JYo4kQWbgLuQTa29b+k5ML1v0ymvQyt0xabYuH4bB9mqtinzteZ+FHuapmgeLbVvIXdwxntX9h3eux20Z lcZdzowJ AltiZ0dkPoBorqO7Ex6C+dhI4NMUp/Gx1ctNEGI0K7Pk4OuIw9sSH7ScBemW1KxoQc/RwRO/mkmCAZQiOO3jZIXoKUJAbxxPtEaxYeJOp+KuAJEC+/bvmDJ6RHaG7/z0cyhhVsl1nas+vFCf6Hw/fo5jDVzXpmLzXxx2m/mbzwY4vg2a5sK6pLS4S1EW4pfJpEPk2C/3SjqWyEOU/vSjV2UDz2IfkygCi6IoRSk3qUNMq+eNfSC6X8dHZd4ZoIDaiEVmHwma14Lv8l7Rus46B5DWNZFvI7fXQuAByZHn2yT8KkcCVc2pSwFB8xrhgTyOvB9izZ+W9hGu2lPgcY348X7EZMhM2EgwZ/UJ7e05d6Cm6gzw6z8i92QDntseKs1+nGaCg2K/Nd025OUoQfOtdh1So25mQmY00rp03Iro9eJcE4RTVof578U58cqsCxF75xDUjY9FxFu+zaHzXXi6PSmms1E4Gg6IFUp35xbsaJImWdmqtpHZKepkrIP5liVRNeKqANeOVQenp+PY= 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 are 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 Wed Apr 3 23:34: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: 13616766 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 EA83CCD1288 for ; Wed, 3 Apr 2024 23:41:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5DA806B009B; Wed, 3 Apr 2024 19:41:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 586F56B009C; Wed, 3 Apr 2024 19:41:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 38C6B6B009D; Wed, 3 Apr 2024 19:41:39 -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 125296B009B for ; Wed, 3 Apr 2024 19:41:39 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id AA62BA0891 for ; Wed, 3 Apr 2024 23:41:38 +0000 (UTC) X-FDA: 81969845076.20.8E6242C Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.169]) by imf01.hostedemail.com (Postfix) with ESMTP id D3A3740008 for ; Wed, 3 Apr 2024 23:41:36 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="Rb/j1Kp+"; dmarc=none; spf=pass (imf01.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.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=1712187696; 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=wZyd6VrWHdcQvYv7dwWjrHMr68qSu7kdpqu35DQkCbw=; b=cQU7c2vq5IIlO+75Pc7ruAwMr3a3yO0DtINgW5j29hjCq/gnS6NFBCxdd1Zrnvd+lrvgSZ eHpLOV9fXjYa5Dn7Sb0nSl/1DR4bOcAE4jmSIEBKZpN1ObE8EudaSmc7tWLq/Cmk7XP2AK vnlTCSdcauVNtn/At7lsYVbOzi2tsys= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="Rb/j1Kp+"; dmarc=none; spf=pass (imf01.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.169 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712187696; a=rsa-sha256; cv=none; b=K+kihZmxNAQBlMWPBa9t8M33PbjW9TG5+nhvb9ocM4Cp2Z7j0TBTUHy/ytREvxOHDTLEJy WDOc4NU/FwkfWqOAzVCAGCuFtlw0A1327p1zYYP+/hu1LohoQ0+OBzZ48ellHdy9rGiNr3 /g22Sxoe8/haAuNqDpk/FFG+/6O+0Cw= Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-1e27fadbbe1so3261515ad.1 for ; Wed, 03 Apr 2024 16:41:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1712187696; x=1712792496; 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=wZyd6VrWHdcQvYv7dwWjrHMr68qSu7kdpqu35DQkCbw=; b=Rb/j1Kp+gjeBAvedbMp77loODhMlTaglrh2H42QkBWL+8zhGdGNP5aLBsjcnhk24ab 2De7NtGkXZ+HwxThRQxRL9V82BV6uSQd/z5naANktmnXV6BLpPiTZSAV/V0ZB5F64i4I kZ/HkRfLULZdMEg9wAS2MqCibHdvbtc9T6qilExU9tkzYE8SyW15feND3JL6wh2mJfXW 0pJ+U4nKSAhPEYvA2OP3bVrEg2gcVBhnHz3pr2Xo4SzgFwc8dfuLUv9VvOuuppGX7g03 UyzaokTTFhes2vPcfG6w3oWZTdTWF/2kwxenHGVMwufZOVNVqU37B85V4HZWFujgMFtD ZXCQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712187696; x=1712792496; 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=wZyd6VrWHdcQvYv7dwWjrHMr68qSu7kdpqu35DQkCbw=; b=o8QPuqhOzG1AKyexlhPtpvdezgtGw95SSa9YWp2a511X5u8RwLBNU9R7ruEUV2ewF+ H3mHoCYoB+f1Ck40PNhuKGe+vuruVGNjPBFoHZxqTBY3FCQzCYE3qrZx9pGf7oEWhpuS LEu5nQtjA2oFojAYveKsoQvU7lpuVnGqOCnqauX43VolHOac2w5eANrs5cnUywhm2vzv NqQujF/HDDLGM2tNQfMbFxOgjq4Oa5quWMY2h/gx/XtrZNE/gVY1zVn0ENWKpRfwBcEa hLiCrXtdJSlok6lH2QksCoGHeO0DMdviWu7jol+5vAWMy2Mk6vSETbEC9dCczqEdgR0k iJmQ== X-Forwarded-Encrypted: i=1; AJvYcCURNCHI03SD/ZcpooucRCYQNYgAJ9KI2W3Es3bvCBmgTzGxGRfju5uTgXRn+pF7WOAjl4Jqi0eJ6i5mLGJKeV1r55c= X-Gm-Message-State: AOJu0Yzn6cHUtmNT+ioLvRHQwto77nyj2byK1YYyLlBtjMW724VKf8ms JIfVbpoG9LO/r+LVUITWVz0GiVPGzy6nCiR6q4NroERGhCH0g0lq/Z1nXKT/zgk= X-Google-Smtp-Source: AGHT+IHZ1DeudjBR3oiYEW2qXm9zKF1SD2kfhoLrPxZT3yYrf04a37H+jHkRe47joBAEcaPm46pZuA== X-Received: by 2002:a17:902:778c:b0:1e2:aa07:37d7 with SMTP id o12-20020a170902778c00b001e2aa0737d7mr551009pll.22.1712187695773; Wed, 03 Apr 2024 16:41:35 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id b18-20020a170902d51200b001deeac592absm13899117plg.180.2024.04.03.16.41.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 16:41:35 -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, conor@kernel.org Cc: 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, corbet@lwn.net, 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 v3 02/29] riscv: define default value for envcfg for task Date: Wed, 3 Apr 2024 16:34:50 -0700 Message-ID: <20240403234054.2020347-3-debug@rivosinc.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240403234054.2020347-1-debug@rivosinc.com> References: <20240403234054.2020347-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: D3A3740008 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: x8j8sbbitys48z1ibqcazfsq1r4ipmws X-HE-Tag: 1712187696-500404 X-HE-Meta: U2FsdGVkX19RJ4xDycyOWBfUCBkxlEUb0Af9imMPRH5h8CEl5VV13AKsq5SBDM5JiVH2OHE2Mc4NWxE7FtjiB/AM96mCsbk9c/jPnlrqfB+LmJdvXym4gP7uJNgkCo1k2DIj6sUOKBxMW96SnJNopaE6DW2qm2Hp/wb1BCXiouuG3/wv1V9ok948EAR0PZCdcNYlgEpNMVgEqhm6qJnCRJSKjv39i60CYn61VdNsS0WiU9U8UKH6FQEAq58G4B4zaxGHccrE/v4/n1hImPxvSOMJ0snIvTmpQHtyxlJTZXXtAYzALipTyYwhBImq4hrtiU77L4/roVlyomiziwKpRjySYRSAyxiaD4eXwR9GUWKH7cGzxl8/6vvtk8SxZgRSX24/XSmeTSvz5d0m+w7iOjmSUjh88gTTtyW9SHCljzFd6P0UuDWKLZTYn8fsQ8YIBUPUnCsrqhTWRWiWUv/jwZaDA8DZt9ueMMYMZh7NK+Do6HS02yoR6D89ZpcO81IxNpbwviBQLumLTAkBqNILJ+EnVHO8Aqv0L0xvoRM+vKutLtAE/BQlYPgsTDZL2IzLYTXoPU/eBReCzC/TwIsjFsWBclAgE5Zbl0kR5DYWGTt2/36vj/41Ygy4q1m4TsqNnmydgyFnJIgL9BN75q/Oh1UuXu+Wt7iCGEs4O4Bd4OoswRXMtALA+PLoEO3bjGoydL/P+wjRENIY700aXpW1ZzV+YeDnvPgGzff6ym6I9iHXlnp3Sh3mAso0Hfl9ng9a4z1JJDjkCfeujPY5BkRTe8WFKY+D4HG0Na8HktT06SqEkLBgcqFEoBTXh8w5ClKQZwmROJYWrb/ecvQLpwmPzlMCPRvxswukVswM1IkdI9CqTUSoqEycIBDwt0amJRwmLfbYXU5pVkkj2Vx0c0ZAn0GexESc8Ye9VuYwqD0HOjNyO7QFDHER00afFnW1SfHjSCjV9cw7GtFRECQgveI YErlX+tq EL4Lrzm23TeJgu7iHPxCrHLR3pxcKCiOr983hm+IRZ3iNfQKgI5JLRp47kEu4zCgh+N08BNJdVzVDAVoEk6GM99B3Vt25y6ta0X2Xefj4SH5akakwZFoqfMjo/Dh3aeyqLMQ9euqqTJMDghp1zHmAcEYMiEcf83QI8iUsrver/JbR06mWZ/NI5kgNuXJCrmo1bBjhHgPr8y4VLy1uqyl9VsPysiSTUKNDiU1JrSzpGS+dB9iuQTkQY9H95teFhWjJv+uZhCGZ8N/dyJ1lcdHplk0zpgcQKf0YnzAp9jqPHxNqmyh05Ory/iDjKOou3hhP1xGcHO5kn+s5RKbpd3C9GtPDgVoCtz/UXNTmjXAuNdIaYdwQpxam3P4L7O/tURT/f/C7k8R4BSNGxYYd4KNWTMTqbzYn4ln7z6x0nMgPcBDW5qivcUh1DeuEgZlvlQwQNGkjI3T6zzgfbaTiylujuZWKn2ym4pe+KQ4cnuHd5ftQXYm2o41jDdjav1nOT6q2sBF7nTwkwIa1DJM= 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 | 6 ++++++ 2 files changed, 8 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..d3109557f951 100644 --- a/arch/riscv/kernel/process.c +++ b/arch/riscv/kernel/process.c @@ -152,6 +152,12 @@ void start_thread(struct pt_regs *regs, unsigned long pc, else regs->status |= SR_UXL_64; #endif + /* + * read current envcfg settings, AND it with base settings applicable + * for all the tasks. Base settings should've been set up during CPU + * bring up. + */ + current->thread_info.envcfg = csr_read(CSR_ENVCFG) & ENVCFG_BASE; } void flush_thread(void) From patchwork Wed Apr 3 23:34: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: 13616767 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 46C4ECD128A for ; Wed, 3 Apr 2024 23:41:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BCE8A6B009D; Wed, 3 Apr 2024 19:41:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B7F706B009E; Wed, 3 Apr 2024 19:41:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9A8EA6B009F; Wed, 3 Apr 2024 19:41:41 -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 7251F6B009D for ; Wed, 3 Apr 2024 19:41:41 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 4628B1A062D for ; Wed, 3 Apr 2024 23:41:41 +0000 (UTC) X-FDA: 81969845202.30.4D30FFE Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) by imf13.hostedemail.com (Postfix) with ESMTP id 6A6DF20013 for ; Wed, 3 Apr 2024 23:41:39 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=SOnEzq5X; spf=pass (imf13.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.181 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1712187699; 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=FP/iR/CjhZ8+5rFjFCPv+Z1AgkLCTj0+E8uD6j+a7EM=; b=Vot/dYq+MFGmxo88tETrdZRSxIoreE27kH91sr9sMY0fN3KI398JujrZSvOeMfc87TiNfw 27BtuIRCOnELcscgwVBCq9XJ/qGgj8hjbufJRj1/8KXpLgfKRvi/HyyQxMOvcW1UisQNED hgQfsV22e/7lxSxajTCi1UwfgG7XaPQ= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712187699; a=rsa-sha256; cv=none; b=MnysracXgpg6ymnIXZcZUzn/SSU4ijXoQpe6pph8rF4v6KEwy19W0ZbNdRfXt1kp8J8/tH 43odrYvGcCRG/TdsZ4zI6hO0mHFPjP8mal5rRsQSN0a+sSMrXktBXG3i6aJCMDbw9gGvFP V+U3mQVl1zhkn4t5mj4BzDETIKRGUZ0= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=SOnEzq5X; spf=pass (imf13.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.181 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-1def89f0cfdso11752925ad.0 for ; Wed, 03 Apr 2024 16:41:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1712187698; x=1712792498; 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=FP/iR/CjhZ8+5rFjFCPv+Z1AgkLCTj0+E8uD6j+a7EM=; b=SOnEzq5XKuBo4bOMmVoddiptA4egHrqwZbC7gkmEVzkIkaeY3OdxDHYR9Od8mugOrI x15U+hHM3HIL654zbLwYOFewSjPl43wOMLucqGMj1LAApFyEjfSaAY4p+EiEjPmAmteT nMoaCqq4o2FMB8qdOvshP/4nxfOVGF6E+sZyKvKsWbsCVgfpTntMyuliddrybNCMeZb8 7sv5IkzjC7p0D64j9nYHImhjmfjxqmvIqZSFxR4BWPx4dm/wO1ZBLOjpbBwucx6EbyeT OfD5KaB/BT9JWXsRuYuyidExZWGkbNtPZ+istl3pZnBX2AXED0hO7IpfL1RR13KmYWib Xr3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712187698; x=1712792498; 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=FP/iR/CjhZ8+5rFjFCPv+Z1AgkLCTj0+E8uD6j+a7EM=; b=N57tM+3wEUt+jwd2KDTmmmswbuFl/AolwIPk9ngus47/g6w7N3vYAH0n7RGYUIuRrz CLcWZWmeteSbAtwMu2xe8vBO+L2jiXuyp7LGUcc7JsAMvBqHcfEsCWtGpyorfnG5dk58 99f65gHHym9KhzoRqrzFjZx2fUi5Ap6UImDR6sV1ZGxAtvjwU1a6QZ2eQjoDW3zWVPso GvOEo0z065WlJ8UJZy1k43Q3mcBmMNrXZUDjKQDMOWWTGZOnZKOiRsV/rAvrrVPAHpiU o6MdrXXpgGOR0n0JdMR0qtOY0b3Cd4hdaCdEYD+NMNjSQ+aSNX5g5Z4TBFEb/y2NUjfo yN4g== X-Forwarded-Encrypted: i=1; AJvYcCXDX3CRsMbqTJZxNuxuXXKZgpFcqA32QSum9dchjbc9mrRBcD70u5HgAA1/+wT7tt5Nll4XflVRmZ3KT/jtfAzpG3Q= X-Gm-Message-State: AOJu0Yx2ElRuQz67Sa1bdJ1zY9Bbzzy53Jr74M/hgPHmHWZeXRuo+Oaj F5QWDj/FrQ+PRl+hO1Fi3SCyNTj7iDrIxwimdzUNY08uNc8PD7LEk2abXg4TjK0= X-Google-Smtp-Source: AGHT+IGz7S4BbcAxWhl0gW+uX+xLjSyvbd4TctmglI8Ag3ABaLDho4nYEEcOpuaastWra5ggKM/l9g== X-Received: by 2002:a17:902:ea08:b0:1e2:8f1d:6fa8 with SMTP id s8-20020a170902ea0800b001e28f1d6fa8mr5120728plg.19.1712187698319; Wed, 03 Apr 2024 16:41:38 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id b18-20020a170902d51200b001deeac592absm13899117plg.180.2024.04.03.16.41.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 16:41:38 -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, conor@kernel.org Cc: 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, corbet@lwn.net, 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 v3 03/29] riscv/Kconfig: enable HAVE_EXIT_THREAD for riscv Date: Wed, 3 Apr 2024 16:34:51 -0700 Message-ID: <20240403234054.2020347-4-debug@rivosinc.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240403234054.2020347-1-debug@rivosinc.com> References: <20240403234054.2020347-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 6A6DF20013 X-Rspam-User: X-Stat-Signature: 4iy893ke7t6g65jy6fnaabo4dsbb94fb X-Rspamd-Server: rspam03 X-HE-Tag: 1712187699-499731 X-HE-Meta: U2FsdGVkX1/M2LsWvhMZUgrRAWcP2/kssyEifmPMOl4NEyVOSXX75ATN5ke/N3GRLqLjthEoOkuqKZOgYIjLLAzGoF6WCfSMMJ1T+1MZEwV7NhkZ0irzVM4JldTbKuJqAnknfGvCUGJBYC/P9sqvS3/yelipNyjcz168vANXswxYcqD7Aqd1Gk438OrzCxHoZsceKGP0beboOr6PwX9iSBzh0Nt520ig64BsS8ZdG3oxPamL/dA5fYlGJFKvy42ghj766nCSQP4wCPDjmlkcquwYPRDi9n71yloj/aoIJoQSUch94RywwMNIAY34u1xYqsrrYSA//wo/rhZEbuoArlkjlkWYHtUrhxOedrnxcmqOAMLmHP/vRALbdH6Ye4tb67KKq8c4ylCnbQd/U73C/DysgCB7aZi9741sJZ8QxtKWNMEHr1SLJKHpeHZeHhCVVI5G9WyAWfr2wCNQ0agFueI7MlSnn2pUa5T39zXhX2skZgEU2ZQiarCRI2vH6mEj22sckIsyUoh09WDuASIS/NMTMSHCuwtsgwE1yZ6z47s13onCGE3PIkIOEnHNJP5BAfwM5lxB6qHohchaA8fmkktEe5vfc5qb9RqHsZ/7Rcf8GF86HeMaT+8JNOBzWzR6Z3r88tO0uRCpfU3cczHDaFrWf0L9GLCr0KKLy2mO8e5qHcFs4lYE7sppsy9QJcTgcVQ+vO3EQ4T1BxQjIJ72qRXOzY6wmABca9ZUNpyrreJqhjPmuhnjSvTVuCKfQIO3knomZB8Pkowmi0iGruox7fdzY0hXbvoqT+tw6DV6hB6LFIQg5g+UGfzYdYI9NgJNBeqYavucypdtVMgRvnff4oAedmOi2nGQNJQQjdS7ge+TdwWI7X4uQOlSbaIozgZs3bwf/5MylDHOP5YojZRZxQxC6aREPBF7bm0wL8XOZX5f8HFCCK2TT+OvKG2CLwKNwOOREhihBqbzslh7Mnt O/KUH2kk bMjTsIzD6y6Ikw46s/hKeNuiEvEGP+hRPrye4incIivgk4L+Vjl3DvsCuf3ruHVNI38bj2QzVPRDqIuFTRkJLeKOpaLiTR/aYPU/CULr6/BVcDw7Ubsz6CDQe9BxPHyM0PUt7E8FaY+jsTYhkjuZJLRyY7f+lqcYJZ7qRylYvwxxILUaTVtSr46ZFmyaNwU9IgMUHYPl8kXcsO25A63VtTrBQRFlSBkWcTu95KFtxG/NOR53dIe5GkQ08L+7qhnepLtj1OSBdo92qlkJVIsQ/0zBuf4i39QKhMYqbcz2Zb1MjFdlPtnRmT6rxQOTIVHRKzS6bGIcZbkJSIDvZx0DUjJu8c5yDA1X+WtXJsQLunC9T2qqyxMwxMj5MP5yb5Ln26QZLEEvus5Y0FIo+zsbVb8ElPniDpleNIgVbHru93b9FKDLCISdzHzeY/9XAPABvIh/98N4sFU2rl6UuNmBcFSVLVVDX9VTSisB82KDHzIldb5qe7V+JRYPh6gAAn9EZg1S69DTb9SCBQK9bU3fCgeGLnEZ//goxf7lgvERwNwL2sgQNy5RH9EAxhwVb4w1k0VnA X-Bogosity: Ham, tests=bogofilter, spamicity=0.000008, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: riscv will need an implementation for exit_thread to clean up shadow stack when thread exits. If current thread had shadow stack enabled, shadow stack is allocated by default for any new thread. Signed-off-by: Deepak Gupta Reviewed-by: Charlie Jenkins --- arch/riscv/Kconfig | 1 + arch/riscv/kernel/process.c | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index 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 d3109557f951..ce577cdc2af3 100644 --- a/arch/riscv/kernel/process.c +++ b/arch/riscv/kernel/process.c @@ -200,6 +200,11 @@ int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src) return 0; } +void exit_thread(struct task_struct *tsk) +{ + +} + int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) { unsigned long clone_flags = args->flags; From patchwork Wed Apr 3 23:34: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: 13616768 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 EB0A4CD1288 for ; Wed, 3 Apr 2024 23:41:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6EB1C6B009F; Wed, 3 Apr 2024 19:41:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6C0DF6B00A0; Wed, 3 Apr 2024 19:41:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 514996B00A1; Wed, 3 Apr 2024 19:41:44 -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 28D526B009F for ; Wed, 3 Apr 2024 19:41:44 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id EFB001C0F34 for ; Wed, 3 Apr 2024 23:41:43 +0000 (UTC) X-FDA: 81969845286.25.5C120E0 Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) by imf26.hostedemail.com (Postfix) with ESMTP id 1B652140017 for ; Wed, 3 Apr 2024 23:41:41 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=1op2tO3g; spf=pass (imf26.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.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=1712187702; 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=orqHimAqeIVvjnhm+Lh8YT/bDSQ5kt/uWL0O/MbcU4M=; b=s3Oa8BKQ6TZEHgc1w/7WnAKnn4eAK6+RyLx1pHzT9S+cBSaH/O+nLy5Lx4BLw8BX6Uhkx/ MBY0LpeRE631p0Cklx6oRzoqTJJ67aKqU0Kx2C5KRjBkd1r1la2gSwsrS2FACn97QvE9EB xiqMVQueYUd4wq0H+rTHSkPY1zSmcI0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712187702; a=rsa-sha256; cv=none; b=A2H0V4FsjO00CxLCsMxfcVmGIQA4zA9IdjC+pSJSaJ+WDRIKQOt8/Nwzlbpqw+fZYniDIo 0x0d9XVFDufvkv35lc0pJbRyCiiCxE/lC/ic00wkX2tvlqriWlLqnE5XUVm16/Vx2q/wlh uCzvcjoTFAIij954+w6D+8i68XR2KhE= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=1op2tO3g; spf=pass (imf26.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.170 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-1e0d8403257so3257495ad.1 for ; Wed, 03 Apr 2024 16:41:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1712187701; x=1712792501; 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=orqHimAqeIVvjnhm+Lh8YT/bDSQ5kt/uWL0O/MbcU4M=; b=1op2tO3gNuT8uiCKBL8B/3T3cOOIByHNwl7M4AkXBnsiwtPE9D/auSMNPn0O3ViA7t me9ZOzFUuUa7GKt9j3xnmxjzogt37nudlPvqChNk+AGkwXyqYEgiuJxnIs2G49lndA6n RxI8eVmH8GtotNfAM+/NDqBow7A9usT7mNz/WE/8prah5xHJ5FKvrqzeoJA4Ua6hxDI5 Et7aMqu8O8HDt1x/+7Xdyy8SheY5yUSdpM1BqjX0rPReFN7F/aOAUg9PzJnz+bgVAh/m aBE+DSBhy/RXvuoSCUtzuCk/FmEn/cg8xK+FwC1RvZHAOFgf63uAjb80xH0DaH11ivUN gzqA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712187701; x=1712792501; 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=orqHimAqeIVvjnhm+Lh8YT/bDSQ5kt/uWL0O/MbcU4M=; b=wXOegAGoa2FnuHjYPx83hM4Wuygpg2M4T58orjNPNkX+QNrIu1yRwP/ZDouqN/sRhI qw7aqIBXNTP15ICTPgxZuynebJvc66PAwFsQRlrVlVwFNW0jq14RpeY4vbxT5zvz1upD CXGAZVOvx6WPtwMpynp/fTC24Bq7+avLKyi19MkqyaNYzUCeQaVXeGVdFgaPeA9qrw9y eTTPi9CoQORvbifoR7a9uuQZszRTxnf6ZL+4txz7zWU9oxne1q1S34eXMiuLDSemm43m E9q/7O+XCRTxso5dL8j4XPpWOVMe1ReaAYSWcvuxiJ0yTKLPBnG997S4pKHmKMDCMBQo XdBg== X-Forwarded-Encrypted: i=1; AJvYcCW+Emo5KVIrOMojZlaiyD8InypiOXlWRSO2K7rfoI3yE6AO9Bc0eNTJYWjJdAYSC4AAkT2zA1tYr7FowX5BNd+CnMo= X-Gm-Message-State: AOJu0YxmzgdPtDVlrBTPgT82hMkITM00SXf/rKSyBdvvwxs5La0hOB7N iowACkiW9ZeSPUpQooMRyHCuIMIzP1BJo7YjmbLaknx7PFukLFm6JxqDqUiGsK0= X-Google-Smtp-Source: AGHT+IHS90cA+AgUmlXmCnhm/ecMbFJNWD0dZq3yi8zpTRzaElJ0z3lk+4PPNraXYIXlpU0z512YdA== X-Received: by 2002:a17:902:d4d1:b0:1e2:8bce:b338 with SMTP id o17-20020a170902d4d100b001e28bceb338mr994151plg.5.1712187700925; Wed, 03 Apr 2024 16:41:40 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id b18-20020a170902d51200b001deeac592absm13899117plg.180.2024.04.03.16.41.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 16:41:40 -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, conor@kernel.org Cc: 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, corbet@lwn.net, 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 v3 04/29] riscv: zicfilp / zicfiss in dt-bindings (extensions.yaml) Date: Wed, 3 Apr 2024 16:34:52 -0700 Message-ID: <20240403234054.2020347-5-debug@rivosinc.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240403234054.2020347-1-debug@rivosinc.com> References: <20240403234054.2020347-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 1B652140017 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: zggje353fjitmjcz53t1zuf93kanbgnf X-HE-Tag: 1712187701-262559 X-HE-Meta: U2FsdGVkX1/5yTDWibWlGuax5OwrMZqjB2xxty02g/f3oOHsnBauHCoJCGEuYDWvGrqAZP8xyjh52unTkzX4xuIn/Oe18PnUoyDM2sC04htvS2KJYzdWYx1qWG9/MKFn02ppfK0H/kZyEkQVPX1KzRRdNRx5ZCHzJHkKpncZOucdjEMmdBw+C6GPnQX+Dk4lkkqm2+zhL1746/FtaM+N2hbqExT9Ui/XQI6YYJ/70mBxQ7iNjWFe/fah9OEDCZq/2iuNP5EclNw+BfzXNL0am9VOuRtpecmLI42mtSLExvKy2ly4s/JYr8iVZuCfZcgzvraW4XcfZlBjmKu+8hidf5FwI9v2KjC3DZBxwh/8lcfqj+eyQeS49tWJfIBi7N/jdTuDhIl67olYy1Olx0VjSWyyQyiGF65XXy42Frp/eNfu8quh5SA+tkwdIKwkxEHLIBY7apvFHuzbL79caHpVaiygBdjZua3pywBABuQefKvtp+uEwd1nLpLOdQ/8DQRgHSnK5SrKbJKlOGHnbW0uNlThYjYll+8MB2bUBRSuVaB3dUpY6z2XiizoY4izig2TwjQXR5f2IW42IAR0fuGccbjHF+3DR/9dZQeZIORACGHYCjAcMqiQ2Pcw/Z44Vwx6ybH9e6Ir7ZvukGtqC6wal8NY5p1T5lhFCkfgbMyfxJ5JSJFvH/SvL7DyDMMXP0+ecaD4HrDKDdJYqXDAwWtHjquSs3JDJFjnDdm0C75INDpKAylLNuJW1IT019vlBOgv2ACUEjxPIT7MiuPowxwkCozGTbpP5yDDjP0JrDQQpcRridvhIDpTqsnh+YwPopuhwXwv0HP43CyGqQ0+uvIYzVTzsf2DW4WCsehDexTSYuiI5pEaHZmLWufFBfDB9OB8JObTUFc6YcUqGlcwTbGw7bennCCX8qDX4i9q6E54SnCZ18zNhnS/KWIjtA/4ZkKr7TpEch/2zShcNgcYYqg NhtPahFV P87t3TbMNoig5jmiiafaGGR9ZO8QyPcVaqg4op8CGguEhnHawsCkybE6SgVCIQ5nE1eIFKhUW4u80KdUQgkzCqUd2ySV4rthokWdmGMeMLmyWtXtB+0pfpAmsSnlUl1x1Fbj7VrHUDUsi/R81SdQv6yJCrKhTmyXwUizjlG0rxSMLVTuLG8Exy4km/DcCeixWm3Ub/4Qf5tROwa2ud0RyG2QRTnADxK72BFz1kQCfei9kjgUKlMc+Y+5JRCoN4cpumW8Sx9feyw8k6xYzYDdi0lc3r/hkbGvheWlg+Kf6q4rsueDvvzmrQa+EWIMKk2bwdUjCI0aMef5WWY98JHtsBe1b7b10DDno5sHwsoVEKH01PPOCpASlcHfKXsxPPtynJzk0MewzRmxdqwTeV3bYaMnpNOyjx1HY3m3rnHQB7IDDKrv1Lp/ajqFI0txb858+X0ERTVg7q1kL+5svIAXWIcSm7hsQPZcX6qqVxvDvh77am9LXMD74J4HNT9l54FUC2Jj2KNX/0B8gTfk= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000006, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Make an entry for cfi extensions in extensions.yaml. Signed-off-by: Deepak Gupta --- .../devicetree/bindings/riscv/extensions.yaml | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Documentation/devicetree/bindings/riscv/extensions.yaml b/Documentation/devicetree/bindings/riscv/extensions.yaml index 63d81dc895e5..45b87ad6cc1c 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 in commit 3a20dc9 of riscv-cfi and is in public review. + + - const: zicfiss + description: + The standard Zicfiss extension for enforcing backward edge control-flow + integrity in commit 3a20dc9 of riscv-cfi and is in publc review. + - const: zicntr description: The standard Zicntr extension for base counters and timers, as From patchwork Wed Apr 3 23:34: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: 13616769 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 8E7EACD1288 for ; Wed, 3 Apr 2024 23:41:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E3F136B00A1; Wed, 3 Apr 2024 19:41:46 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DEC606B00A2; Wed, 3 Apr 2024 19:41:46 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B80BC6B00A3; Wed, 3 Apr 2024 19:41:46 -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 92F836B00A1 for ; Wed, 3 Apr 2024 19:41:46 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 4F0C4C03C4 for ; Wed, 3 Apr 2024 23:41:46 +0000 (UTC) X-FDA: 81969845412.07.18E2655 Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) by imf09.hostedemail.com (Postfix) with ESMTP id 98AA4140010 for ; Wed, 3 Apr 2024 23:41:44 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=tuVyeevz; spf=pass (imf09.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.173 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1712187704; 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=htkBMuDJ5iEzOh5mmiAVjKhgj30cv1A8JpdqcqJ1q2g=; b=Mhn0f6nhxJnv9MAMtiS8yvQvTWJOJb3XyjsCaOqAqiisMFgpTPWTYdJRwOjpny0W/CwCyt bdYcyUIG4q7MersqLi0TMgZa13c1sx3y4ZPVDktZxIiLckxqW6Uzt6IzNvpiqpK3o67oRx AmV72yBIcfyCDaKPgQQEtLvI4oCFaLc= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712187704; a=rsa-sha256; cv=none; b=8FVHfPPCuommVXIT0dpOkF+4WgMT3B2U009EXZFcUx2DtKBHFpvmE0kT/pRSiLoU0D+1n4 WAyeTPoHAlWkL7yBJ3lZiHkpdN+1w69AcmzIMBE7mDigUR0Y6R8Qt/3s1uJ7j+ZP68n/wB /nShGnU1Z6H9DRtQ1if9lclKhlAB5ZE= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=tuVyeevz; spf=pass (imf09.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.173 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-1e220e40998so3080165ad.1 for ; Wed, 03 Apr 2024 16:41:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1712187703; x=1712792503; 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=htkBMuDJ5iEzOh5mmiAVjKhgj30cv1A8JpdqcqJ1q2g=; b=tuVyeevzjRyyFyjX/9uvEf22x4S5iUvIYmEIdSIMgb/DfkPcV209TGC8TD6zl7e9bP WUKs2Edrty9G939/pJJrwVtX3bzkqPmcrrqYogAQiNec/atz6T3R00obmy2y5Ws0UCHn g7xTAawC703+P+utoFz2oa1Bh0PzC8EbETUcyVk7PrqTRWh3KJr2HZaHUQlE1YrWqmsl VKzypkcjerU6lXH2o4DbnfIyN5YylekIEmTJ2ercDtnZT+gfJUFWrgpNG41hsTF0rKTX yeUZBWhnhqMxmrn8BVxjJaVQ5nn0KURUOK5TSTvJ3Y8xFxhy6QsXnvdd+bpK7ssRMAIV ryiQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712187703; x=1712792503; 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=htkBMuDJ5iEzOh5mmiAVjKhgj30cv1A8JpdqcqJ1q2g=; b=GOfHNuikcbZgOPtglsV006B0BiAZ2PG/ng9yo0+grTpbDoeOKDeIuY42pCrFLbKhsf fvdqWtyOpopAYn2YYEj0nm3nTpC7r4oR83DkQ4+g9sGj6Xa26Tu2MxVRedsf1jpc+3l2 0Bz8oc14lJ4nMejRScmqsL1EqzxHVmR2YtaQj0SLnOTJHGTy1ZTHltlg8YZBYuSPCFXd cGO8dBj9Ul1aKfj00FFu01rj9wiQWPJzhlyjJ/jJMT2l7FpQbY9mj3/Dm3ybcw2LD86u Hqsg9sjbHj7VkzoDs4R8d0TS9jm+Z0zVaYFAZoAvZCFLfNVxu55kiO6P9qwh4zc75lH7 8Acg== X-Forwarded-Encrypted: i=1; AJvYcCWE1OlTkTobsO2e71nc74bok2BDjgrAQCtQMg+4q5/BfDVN1hiDpx6oSmPXk7n7QBXJYS1Kd1QP2L3VVpAcct6tDjY= X-Gm-Message-State: AOJu0YwrCtqe3WWorjZRUHI95BLHd1+eSu+C0EJqPP/0qbHrH7ChD/Vr /S23Uor8F/zqPt2nq/6BAhqG5b7zUvsra613D6OevZo0SCxl/MRR8azLKmssGQI= X-Google-Smtp-Source: AGHT+IH7W+PqbZqQgLqe7/1okN5ltEP1rpCUpt8LdmXzCJjFoz54mr5WWDJ+p0nFbKNWmMer9ORIlw== X-Received: by 2002:a17:902:e0d2:b0:1e0:c3b5:1c5 with SMTP id e18-20020a170902e0d200b001e0c3b501c5mr748168pla.24.1712187703505; Wed, 03 Apr 2024 16:41:43 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id b18-20020a170902d51200b001deeac592absm13899117plg.180.2024.04.03.16.41.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 16:41: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, conor@kernel.org Cc: 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, corbet@lwn.net, 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 v3 05/29] riscv: zicfiss / zicfilp enumeration Date: Wed, 3 Apr 2024 16:34:53 -0700 Message-ID: <20240403234054.2020347-6-debug@rivosinc.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240403234054.2020347-1-debug@rivosinc.com> References: <20240403234054.2020347-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 98AA4140010 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: dz7k1m6owk7k3eef5yfxt3dgpaww9kbm X-HE-Tag: 1712187704-720393 X-HE-Meta: U2FsdGVkX19h81L2PbQYlUkQaKaYG8nAwlb3ABqyMRLBD1VVLuHTxlkl/GVu1ZN4SmYX+3xYoTPGEnbxll71YNQoraezL4+7KK4OeFCF95UIiZi7VMiMWBxBcft0ekyDtwI4JWKUOsvkYFHL8LvpSYMQslfQizT4fLIGxerzAW8hzlQTFTLt4SOs5CO3EcTYd0i1Xv0sE3TTUWJ2UKCTaI/DTOotrPa5wbP8pPyXqmj6sEJj15z70R/d6aaftRnzVtuo42T7HeEJKPiOoAKy1GxWSEHijJYQCpfQT7JUfirs3MbpknvlJMTRnIGwXOoYdYEUxLjElFRhuG5f4H4EmzG4PFvEXTUxrOEjti4Dnk+hbmFvGQLIDq59HugGjZH0mmGOgFPcxIDBwG42f8qzkV6b6rO8SMEJUFovwMx/c9+cmp0gt2GW6Wx4AwPZtTydHEEE+mCOZY7XJcyh3NBZVLdRs851etWHU3UQ6JkovJDRH3551u+Huo6Mp9Uvu4YMZi2t0RSNlfMUoz2ppLaA1FdqEkqVWNC4eyYoJLRNRedECWTR11io0OKhSYBKFeyVTtKiVJ/jBld8gWlqfgzdYl/3UkFBKfU5vfi7jsga8w7wipJFJ9EdpNM66sbd3F/x0lTRWv0xPRBmit3GmeDth9yoDI9hFbBjubkk8q2MSjJuqClLpPD+KWuWYGKREcC17auOOW5GWg6NOpR0COVggCus+HW27T/d7QLUU1Cgjz2xleyi/V7ckC4318ulwUqSWu/MDQGIj4EOaJwiU7LYAWB3zIpDR2j9NwC+mVDuMJ0sYfpkGzCR/PAamgMD2XY6y1z84OvWG43aU5aF8FULtnXYpzGdxdZF3FzCTgNR6FkriS40Bv6IhZE9p1WLapr6bCExTCY6SORhB0g2yjuO5JC7dyx9gGxCb5KrDm25hrmpSqpSxwYr5qJVr222mhpwKzB0mapynfpE6B52t5t mc/+PUTj lwqnG+qksc2PYE9ChIlR6hCUh/pN3S+rERFz0dcIsYAeEaO6VrkPNOXefO6wDZc2q3KOX3Qmy3N8J3QUm3/x9wv58TzQhch6z4WUtPrEyxvzl5jYPm0DlIC9GvRyCIGajfn0R/ISMq9PgY8iNQ0YAkCIfJaDfSVFyVN4R09pTjOG+wyelNWBY2Z3Kjj6KQ/0AZ787NN4ajJCTHqBPI7b5wop+vPoE0+2AWg4wXnHGbKIHEktjvLEHg3Gnxg== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: This patch adds support for detecting zicfiss and zicfilp. zicfiss and zicfilp stands for unprivleged integer spec extension for shadow stack and branch tracking on indirect branches, respectively. This patch looks for zicfiss and zicfilp in device tree and accordinlgy lights up bit in cpu feature bitmap. Furthermore this patch adds detection utility functions to return whether shadow stack or landing pads are supported by cpu. Signed-off-by: Deepak Gupta --- arch/riscv/include/asm/cpufeature.h | 13 +++++++++++++ arch/riscv/include/asm/hwcap.h | 2 ++ arch/riscv/include/asm/processor.h | 1 + arch/riscv/kernel/cpufeature.c | 2 ++ 4 files changed, 18 insertions(+) diff --git a/arch/riscv/include/asm/cpufeature.h b/arch/riscv/include/asm/cpufeature.h index 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 Wed Apr 3 23:34: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: 13616770 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 34707CD129D for ; Wed, 3 Apr 2024 23:41:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B3B3A6B00A3; Wed, 3 Apr 2024 19:41:49 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AE1EF6B00A4; Wed, 3 Apr 2024 19:41:49 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 82D926B00A5; Wed, 3 Apr 2024 19:41:49 -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 5D5126B00A3 for ; Wed, 3 Apr 2024 19:41:49 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 21828140141 for ; Wed, 3 Apr 2024 23:41:49 +0000 (UTC) X-FDA: 81969845538.21.C0E35F7 Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) by imf09.hostedemail.com (Postfix) with ESMTP id 3EBEF14002A for ; Wed, 3 Apr 2024 23:41:47 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="WZV/FL4J"; spf=pass (imf09.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.177 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712187707; a=rsa-sha256; cv=none; b=ogRLlsGMeN3T2O2Yk6Z389Se+VZ5S07nUZ5ge/mKI6qpD7hU/GUIn0BK58twjDUW6YqTjT Ii1ZPgYLVDrNI4pAJ7O7ut3z1IsWtHznZHdzp40vTQTSv9zItRyz2pM7rllC1jBDVt17yY FN6DuZOsg1pvvINd/fcsGJKxrwZisus= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="WZV/FL4J"; spf=pass (imf09.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.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=1712187707; 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=0eUDSnaDyxqGg81dPEndGGDTB0RLZ2EAet5NZFMb2JI=; b=quDzDWilVnAvsn9PGcZ7labx2xblKMfNW8qTk5dF2OVBoDB6pGP1pacaZEtgE0gP/IIb/Y DmdAs3lO30hhH5b+xBTyMAjlj6+yK2+2m14YxZEvcwM73LXpsD4AcUfKZ7B8TPJdlbYd+x 1PXrsov+I6mXpXoe87S0nv0wy1YRxJI= Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-1e2987e9d67so2184735ad.1 for ; Wed, 03 Apr 2024 16:41:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1712187706; x=1712792506; 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=0eUDSnaDyxqGg81dPEndGGDTB0RLZ2EAet5NZFMb2JI=; b=WZV/FL4J2XunfXkRIMK+GjS004pTRZpT7a/V2hUlj98fUcWvxmE4FCHSAj/2n8v319 aOocLfSJgGIT0yfa+HpLBGv2PV79846Nftq8nvLGLiwiZ88xvsSTIXGdugqDb4RjfNIC V4YbmvTK4Lk6BrVZmkPGpPI0/yEJXH8Mj9xL+ZetWQb0UQXRjFRDBx86WoqDqm5TSUSp f5Y0nJK0bLu5fUDy+1JZusgzAyz7qDTRWEPQD0BGXFTAeV9+r5uIUZi7/MBBa8aM9fK0 42KrAe9mh8ns8TMWhCn/qg1IaPxtvJ68Fr+01So6WiNPaYojGltcH8hU13Y/zQC94hqX E+1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712187706; x=1712792506; 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=0eUDSnaDyxqGg81dPEndGGDTB0RLZ2EAet5NZFMb2JI=; b=nUfzo8fjrFxLiOamCqQiKTTIZG/XLlb7dgyjFK3RAT9L1Dl8W0lBVh5ddNEoHjdEfa X/x6hjCHs7EWH3VKrgFJbI2kpXeSJyhDa1vp4Q5JSMNDBTq1CsOM3IplWx96vHg3CoTv +z0LDjtpAPWY5eTRbH7Ww1OPz7t3nnwaQaihf9U3MVRz0eqIxk4sHoCD+oDxgeDYvPg+ OOsYmX79IfrGnA4Y5B0aJdAbbx0Fkt0d/asz/VHSQnTNOY0jM9ug1jIDi5GP7U/DsvjR VIHz8ARmyTXqZI+H6U9wOwy0Ysbtu03iLTRvd99FeWAnM1st228QBa+GmgOcsnf0XOlZ B1Dg== X-Forwarded-Encrypted: i=1; AJvYcCWak2oiizqI+JXlmzOOrPaYPihVElbRpu8t9LiynIns7TSXYGXWro3Sa6MiL7d9kNbuYzxI/Hmqr0iuuK0WQpAooCA= X-Gm-Message-State: AOJu0YyDnvD66Iw9aefbxOH0+KxOibMyUjwGhFKsgxU5+SHCRsZppKe0 czFTnjYQC1sxxG4fGcfmKhLkTmM6GcF6dhVmsXTC7jfs262PZeWcFBPCYpQy8gI= X-Google-Smtp-Source: AGHT+IFyjyOK4imuMufx/V8FtRsE6RLkCpoEsDyebREmag+E/YQOk+Aiq+uxRiaeRtydulTn7Ig+Gg== X-Received: by 2002:a17:902:d2c4:b0:1e2:a61e:47fa with SMTP id n4-20020a170902d2c400b001e2a61e47famr1344360plc.15.1712187706092; Wed, 03 Apr 2024 16:41:46 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id b18-20020a170902d51200b001deeac592absm13899117plg.180.2024.04.03.16.41.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 16:41:45 -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, conor@kernel.org Cc: 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, corbet@lwn.net, 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 v3 06/29] riscv: zicfiss / zicfilp extension csr and bit definitions Date: Wed, 3 Apr 2024 16:34:54 -0700 Message-ID: <20240403234054.2020347-7-debug@rivosinc.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240403234054.2020347-1-debug@rivosinc.com> References: <20240403234054.2020347-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 3EBEF14002A X-Stat-Signature: g14ddgcy9ypitb3xtshxgkz81msomuu7 X-Rspam-User: X-HE-Tag: 1712187707-635495 X-HE-Meta: U2FsdGVkX1/P3Vpy55xwrXyAWVz2oeqjrx5iiSK+z5UiA1o1oGZattG9e0ExMwkdMrTKevW92t3sF8r2t/PD0VTDomFbEQd/WX0WY4p2hA1yVupQ74pv9ef2fJHxYLkr71cOE91/zFa9QxvqkZWAn36aqLJiIKdzPOKWw394KzsWSyOuudsbhoGVbUaQUSS4kBaWbxypj6mSfuHpgkGe4BoBfPOekeCqbrPo7TY3aTQw8CFhXGgQDn4/xrsSZjDon7e92VqNsHsB75OJtg4cFpELNXDeMWkR22VwK+cLvFQ2uvsTqJOzoLLZ+K8p3UX0BTrk9Mhug+ktg1AdWH65r2NY42eoGikqjM8EAAS+XWT3MDxQbzX5zvQ5DMtg/j4yf4SptVzm8R0eKAYKoKpA4xOX172MKWF8ICgR3R0Guzz3OAZRC+f2+34gceyWV6BCvqCUpuro+Va9HaBEJlYqi5evCl2iVRpIqBtF8DEKn5FoxXaWbcvWXVT4ElCNqsntEkFV7XfPaS6jUDoYuJZIhq7nicFcMOF3kxbH7TvuADOY8f1QFVzMELJ/hfcY0joqda1hf2vBBMU9q68E+KbCjyMnCoiH4BIkIhi42fcKcpFgzNGy//5sKbhAtz10CspCiTKlBlNSeOlec9+DGUB9ptCWVZ5Fww8XIY6Iw1e5ksnbXTKZ1t0D9XaqZmKklMM5oBl/864iakSluLY5vPguJJyFX8THRmBnPQ1zVZfSMnxYGNdAtp2SK86SWAS8oQy+oq6SdvWCdRdU+G6uolZDhupLvoZcInbgMgCucvSCvrtv/y30xPiF+eS+RrYRbid8skwVLJtRpYvvtrJ4lVr1k6UXQmKETLxighIsSbQ/9Cm+N9CVSHnvVGHy4wQkilStu0bpWl7S00FIogN+wuo9DiLkAA5fozcCbD3Ye97GTP5yU2vsplK7yfnEq1Oh6u17Fq7YsALkz1Xp7ozKVSN h+EsnveG R9bc307x8/cOzimeVz9vxViy9xSx/O/whB0FRD7zah77AEuBKFL7p5bBa9afVg7kWTC0Lg6taC1aaYSArehCoKt4BWk0Dvtq2n1I+v1TPjgINcITN1jzI6r9E4m201eKgC8pvh5AFGbqU6kpZLCQLWKll+k1GbdrWAnbGuMw1qdEVZ65ao1yPzeXh0LJmhugibBW3NamxGUG4hWxveDJpCZQKGKQexNgMBGx/ZxSgrgXsKCv1wiQRtzNJpw6+zW1Gn4HT2xUaZPwxJQWejYYu1zGbBI0esS7oBNU1HYSF8/HLwwlhP7ucsG98Kybjkdw2KOb2n3gJv9/uQ/l3OEmyDzPgqRGkVV2hEtdUSIVoCa0IveYUTu3pzQT6VqTbRXGiMBOlLo9VkvxoIulW9utc+cmw6aCvm4se5SIz0Hs42BDo2qj5FauPapHhThv1HtEEh73k7lKbsf/neRvfUWofF2hViJZQRgnrR+83v7vX7nfDze+IUWeyxwrS/+uHL7D2Gj+Mkzptrh7y8kg= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: zicfiss and zicfilp extension gets enabled via b3 and b2 in *envcfg CSR. menvcfg controls enabling for S/HS mode. henvcfg control enabling for VS while senvcfg controls enabling for U/VU mode. zicfilp extension extends *status CSR to hold `expected landing pad` bit. A trap or interrupt can occur between an indirect jmp/call and target instr. `expected landing pad` bit from CPU is recorded into xstatus CSR so that when supervisor performs xret, `expected landing pad` state of CPU can be restored. zicfiss adds one new CSR - CSR_SSP: CSR_SSP contains current shadow stack pointer. Signed-off-by: Deepak Gupta Reviewed-by: Charlie Jenkins --- arch/riscv/include/asm/csr.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/arch/riscv/include/asm/csr.h b/arch/riscv/include/asm/csr.h index 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 Wed Apr 3 23:34: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: 13616771 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 D51E4CD1292 for ; Wed, 3 Apr 2024 23:41:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 369976B00A5; Wed, 3 Apr 2024 19:41:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 318A26B00A6; Wed, 3 Apr 2024 19:41:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0F6BB6B00A7; Wed, 3 Apr 2024 19:41:52 -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 E088C6B00A5 for ; Wed, 3 Apr 2024 19:41:51 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id A16EC403F2 for ; Wed, 3 Apr 2024 23:41:51 +0000 (UTC) X-FDA: 81969845622.02.4AB1835 Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) by imf11.hostedemail.com (Postfix) with ESMTP id D3BBF40004 for ; Wed, 3 Apr 2024 23:41:49 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=fazsv9Bk; dmarc=none; spf=pass (imf11.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.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=1712187709; 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=Rwd1Rqhx7Lp5GXVDXChDZGcUyS7lp4m5P80feoXs3uc=; b=VrvEw7MEqsvFL0vVL1HREel0AajPULafR8rx1Gjtyb54byVLIR5YrfkSAIGIzAtrAIJqkS TQshWSqpFoW6Y4Ml6QBg4cflTj33xv8ueOqJ4039XHV/VKQvQRDgro8++fYxZs00m3D4oc gpVJrFWnSUNF23rIPmvntah1T6XjHnY= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=fazsv9Bk; dmarc=none; spf=pass (imf11.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.181 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712187709; a=rsa-sha256; cv=none; b=a07cGMz7VZPugF0/yfPgK6p+KVLg6pwFvmkyC97mqR1mzJjeMHgJr7XLBaeqKECx4jupon eGmw8wpmzVA9AYg5agsxHvaKQvUU4SsZY/PxH+vLxM2PYGwRgB/QP3TOpjzIJ3CciTLeMr OQFRlKrYizxfwHUDi2FCSr0nUDGyQ98= Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-1e0bec01232so3326905ad.3 for ; Wed, 03 Apr 2024 16:41:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1712187709; x=1712792509; 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=Rwd1Rqhx7Lp5GXVDXChDZGcUyS7lp4m5P80feoXs3uc=; b=fazsv9BkXxzj3Vhgm8x+G00fuPx8xohDllKVxwu06cz7gUDg5iXIbA5sNOxdR0280w 6YtLP/dw5H6rPGg5EeDsO0BOQaM4gE8qKUEZMXCYpx0i/FX7IpJwTkesyvFe7KpNyWnp TEZL2opqM/umsP+llxJ7gqeTX1sysAiA5Vcb9YzWYrFe6UwhyiVvGoIt+0ERfD7hlhZI R71wq/8wC/TXk4sXgl/2n1ifNhATKsWvwLU9VjvjOWcTv+PjtmQmZUZq5zWqNaXGRlGg 5ANEBB2GMk5MlM+OqPKH7UexNkAVpZNOAjXPQ8bbk++aXBH9dfUL3z9ceOziTU/inH7a YNZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712187709; x=1712792509; 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=Rwd1Rqhx7Lp5GXVDXChDZGcUyS7lp4m5P80feoXs3uc=; b=lJ3CkSggpMG++rf8jz6kq0b7Io9QEYX3l9OWTvjkIZTsosCAiJ0P94mKAHK8H8svhd byHX4SaCJ5Wzi/sco6nEKe+vZjy3v6CJJotJShWo/DtU5azN5XazPyEgM4p7Vs6YPcAV /sRBPt0ss7nXQT3piILSd44daDNNZE3F7/6fUNqJdkldLCuG1p2d4ugf3MHpFGSMmajT JCDV8O34LJMLN7qlXBP/L17a5mZFeHQHlv2JCfulK2g0+YhQ3rfaOtsarmtipWH3Wckl 0NwKfZdlpFZXN+RAfI+1EjeD4nFgIR7e7orKggsHaBnyI8mmnyaQ9hZWD10ikW5Pv44r Uzaw== X-Forwarded-Encrypted: i=1; AJvYcCVsLfCwqXwuq6kk56Wf7YXCZXKeo8a3NnoG1YVGEe/vCjD09v+OoEfw6wB9l+U2UeEboFmEEF7x2nc7HcpWdN3Y5mo= X-Gm-Message-State: AOJu0Yx8xKYFgTuKJVt9dv3rOqa6HZC0BSLTE72V6EpEmFecrDZopR0S wWrKRXT3foOzSIFGIZSzVswxEkEHYAfY3L7lKxOWmrgwcLmxZXu06UffvHNKp8U= X-Google-Smtp-Source: AGHT+IF22hy01NLtT1jUYsdu0c7KIFU3cgj1Pn2y4vxU+c5imdCSWG28M/uPDSgI7K6xqeeQiw5lvA== X-Received: by 2002:a17:902:dacd:b0:1de:f29a:36ac with SMTP id q13-20020a170902dacd00b001def29a36acmr758881plx.68.1712187708628; Wed, 03 Apr 2024 16:41:48 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id b18-20020a170902d51200b001deeac592absm13899117plg.180.2024.04.03.16.41.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 16:41:48 -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, conor@kernel.org Cc: 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, corbet@lwn.net, 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 v3 07/29] riscv: usercfi state for task and save/restore of CSR_SSP on trap entry/exit Date: Wed, 3 Apr 2024 16:34:55 -0700 Message-ID: <20240403234054.2020347-8-debug@rivosinc.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240403234054.2020347-1-debug@rivosinc.com> References: <20240403234054.2020347-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: twtcq6fuhcwx4ggqs8skuyi6cx6s6coc X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: D3BBF40004 X-HE-Tag: 1712187709-153689 X-HE-Meta: U2FsdGVkX1/XgX43EDskHECLPFrVk/EZzy8LDbzJJ0nuxAooFV4J3hQ2eTf860wJ4vc4NWz8oradkdtVUYBZpcXa0JZVtgyVPONIChj9C6yWtK/SZnwc1HyoRN2UF+swq76Lb6dVEsxMwgv+ue8YKDRQn4+2rCK2W3JlHnIVFl0iUb2oIs1A0AM8Gm1oa3vMxhG7ruJaHJJm0b1+dq6W9gKuRpWfzUCu/qffzTynVqnizoAVoIWShBiF4ssObjoF7GeRcILvby0G0PKtU86xykS8e7ZNtP2UkUEzHTngkdJqnl4a9ikQodcopLyP1cs6EeMNmv1ESABFv/a+BMykDEGYZW5pNcLrNzAo2VLKRqdgbZ8J7prdlIVnZ0ukf5SMmw9KlNyf2759ySEcd0TGje51kr07tWOiWlLFPeezTVo2q5QRpoSc2M5zuszM+gv58bbn4WupyjrDqtEovGc040Uv9UaDH5fMRwOomRkyFDdTC2nE9ySc3YIWEgbcrWKgSW8Nzydzv0V0/s6nsrPlyADJQwXLZYKbmvq2FYvWomg696oBZhjP7bUmXx2EFbU/t6WP1ShMxgVBmzOZB4ELEneA1lNMbzLGYetJRg1DyTGCxZ3Ftq4PkfT6gkGXRDQtGwY5n3NhOHZKFn04pR3wuzEMfoWLeti8WBcVmqhXPvIk5bf9YbRfFXLl0Z0P6vttMg0HJXjVbmKmOKi3wyomozYxNfdEVqVQG3ZSvx3TK4ZD5EG9NTA8JPmYsj1A1eaawUFPN3waCraq1hRyMlrrE4ybV3avmZmRE0PY/xg1WKTSs2hdIHd6ND5cWsai3FZ3XbK1pR854BSMB1hwreyWxqp9U5K7hloW3q/Atm2qf04soT3kPDDdurcGUewUlyztVnkwkzpQ3maJwTJabC1FrAT0SftHEoNs12jk9ASYbLGin4+jbChgvvDbX0vGDOAs69FxjOgciDVq53HL3Iw 34wuYdpv 9zLSNF/6okDJK4qALFYr4P6NaAEfR6ePQ2ygKO7wp7kdBCE+zS3s5FMxG+gVkzmRTAHo96N3sa5Cy6AdIazj7dQuhauWAkChF7hkETOaoQNVIhZMUrj2FP2Y8PmwgC4xZF2CoeYfPoB4N9+xKCOLUUO711lXQA3nfSBzXpAmprJpkmV1gff19TQb6xXD5t6J0T9J1e7lHs7d93cawdKDxpUEFz8mvhia5YJvnwjzGD1PUnzZI/jykzCkt5/iushhP28DGFK6AGLnMVz+GWizw2gzKgSCSrbb+9p241YS9d5axVOJe91LoG3XD0dEfBrFiAGm0Ux9d+AuYCXgeXlxR8dh+H28CmBo7xqzPWgZmOVeRScXVmnIiFda9Z2xH2JCwiRNI8JH1+mUqAaMSq9fijj1sLcbAmSAini0DzN1bTHgrsW3pXLkd6tVH/Vye73+aduLW0fUVyP7pdnmkTzscwiM1BJiWGxwmQiGLMDfQ29TA+nZC5duv4eTXVRICzyBgl5oNokw8r7VhZuLV4lTa9frL+4CHaA8vnRT0mvaQ2sPbbwrle6B5EauwebD5xvpUUh/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: Carves out space in arch specific thread struct for cfi status and shadow stack in usermode on riscv. This patch does following - defines a new structure cfi_status with status bit for cfi feature - defines shadow stack pointer, base and size in cfi_status structure - defines offsets to new member fields in thread in asm-offsets.c - Saves and restore shadow stack pointer on trap entry (U --> S) and exit (S --> U) Shadow stack save/restore is gated on feature availiblity and implemented using alternative. CSR can be context switched in `switch_to` as well but soon as kernel shadow stack support gets rolled in, shadow stack pointer will need to be switched at trap entry/exit point (much like `sp`). It can be argued that kernel using shadow stack deployment scenario may not be as prevalant as user mode using this feature. But even if there is some minimal deployment of kernel shadow stack, that means that it needs to be supported. And thus save/restore of shadow stack pointer in entry.S instead of in `switch_to.h`. Signed-off-by: Deepak Gupta Reviewed-by: Charlie Jenkins --- arch/riscv/include/asm/processor.h | 1 + arch/riscv/include/asm/thread_info.h | 3 +++ arch/riscv/include/asm/usercfi.h | 24 ++++++++++++++++++++++++ arch/riscv/kernel/asm-offsets.c | 4 ++++ arch/riscv/kernel/entry.S | 26 ++++++++++++++++++++++++++ 5 files changed, 58 insertions(+) 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 Wed Apr 3 23:34: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: 13616772 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 B6EC3CD1288 for ; Wed, 3 Apr 2024 23:41:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3DEEA6B00A7; Wed, 3 Apr 2024 19:41:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 33F7E6B00A8; Wed, 3 Apr 2024 19:41:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 16C0A6B00A9; Wed, 3 Apr 2024 19:41:55 -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 DF1156B00A7 for ; Wed, 3 Apr 2024 19:41:54 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 78BD2A0E9D for ; Wed, 3 Apr 2024 23:41:54 +0000 (UTC) X-FDA: 81969845748.02.077767C Received: from mail-pg1-f179.google.com (mail-pg1-f179.google.com [209.85.215.179]) by imf29.hostedemail.com (Postfix) with ESMTP id 943D9120002 for ; Wed, 3 Apr 2024 23:41:52 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=GcQt0gss; spf=pass (imf29.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.179 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1712187712; 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=ABpeeFCkULjAH2+wvqdO28185Mvpk++2Yk/j/QF7GJ0=; b=u2dakd0JrC50yt00Qt2lAv9vkAMyHGrcNx0xvK7u+xovkmiG7dooRREmi4JR24KrkI9Wiz z/h6eJNny0j8lPeW7hH/k+x4n0zAzm0UNdpYJVa2DsRaqVMrznCCeUF6gWy4xIqpc5JAf+ 5X8J4CVLyFgdYxkATL0aeLF1Lx2WPxM= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712187712; a=rsa-sha256; cv=none; b=Lord73JjRras4ngosPEodHUqwqDf5GQ5C/5aNHZe42rSLnyfd0VtIYtIXonvCf9Ipsz15V sz9aY/o1Ma/R/WvktPqsENl4ZGjfLjTZHzehdxfC/N8LHcQRahss1tvM05Vg7opvMF1fg8 mJp029uvjF9lmaDh2ClQ6aYZx/HMYLI= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=GcQt0gss; spf=pass (imf29.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.179 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pg1-f179.google.com with SMTP id 41be03b00d2f7-5d8b70b39efso395561a12.0 for ; Wed, 03 Apr 2024 16:41:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1712187711; x=1712792511; 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=ABpeeFCkULjAH2+wvqdO28185Mvpk++2Yk/j/QF7GJ0=; b=GcQt0gssqGXcdrGi4R8pi/d+UQd3nPT8tRseE+erdYWrKXC0pO1kj4DwXfVNYjP8Eb E3Iy/lrkJTBYsIIchXc9Aap0sIFigQDZ8TIBzhxNWhm0356CcFbUt8R5ndBAOUHlJUMs C3yM/XJu7RYRBSAeo7fRFaMBf02598G95SBfe8JGCL9qYPzoaWAT6r4FTy0lGg5HSZ73 FQ1vMYCVPd7FPDkA+kKZsOqPmOzzLkeyscPemhOyBDl8SdXLqhy8jERUF4+o4MsEYMJq lsPxoPVfnFOZE9XSL9EmrQgA8vCzdyoM5czKkjKbnITPtJ4bT4B1QsUL7PKafNE+f0tL lFdw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712187711; x=1712792511; 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=ABpeeFCkULjAH2+wvqdO28185Mvpk++2Yk/j/QF7GJ0=; b=XulM/rgn9fPRALCvhn24AoNRTGvj9FVb3eulOIgwiojB2OAH1c1CfWow9eXLh/glFi aGTcv9lTFzeFyKLeTAH3cIBDdEAeIcyKQMhjon5CTZG7dmNPPJBH5lPCc95tcCVJXlsa QUbJtwm4KqHJry5kKSFIWGfhEYE+f7twEzAwmZUCerapEzqGjLimdXH/82hQTHRdcKF7 ZoDw8Ea+3UBMMun/FZ+COqjMHFLS4JZx4tX6Wsj/DHE6g5VFTRlMGcKLlv593+qBDP5Z RJuUECJDlqy43leWFqWTUCsrcbxkJMLHPoVxtiBVQ6ncpJPXZaELj39q/R1cSkJK/dII pfhQ== X-Forwarded-Encrypted: i=1; AJvYcCUZ5893d78SWB22iT6BqJAa0U4zZihKflJ2IEs+B6RaSTdNHst+ItwWsr8BeCH2aYwprailtaqWPzucR8UrLicXTlA= X-Gm-Message-State: AOJu0YzviZFha97NIrGr1JZJi6g9JfhvmoZiW1PiYECsORET5MlPSzMN 1P+lUzhrvEbjfOAF83q/tJX/9JQSSunqk9d4cmSsMm7aKiNUruZhw3JEZBT1OB4= X-Google-Smtp-Source: AGHT+IEuPdJ9+VZyZuaG1MlOsTEyLTpxiJH28qDOFi58HVozeCfEc+yN58hPfTIqNV3cM5o/KXJ7iw== X-Received: by 2002:a05:6a21:339e:b0:1a6:f8cf:1e23 with SMTP id yy30-20020a056a21339e00b001a6f8cf1e23mr1102834pzb.41.1712187711360; Wed, 03 Apr 2024 16:41:51 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id b18-20020a170902d51200b001deeac592absm13899117plg.180.2024.04.03.16.41.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 16:41:50 -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, conor@kernel.org Cc: 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, corbet@lwn.net, 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 v3 08/29] mm: Define VM_SHADOW_STACK for RISC-V Date: Wed, 3 Apr 2024 16:34:56 -0700 Message-ID: <20240403234054.2020347-9-debug@rivosinc.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240403234054.2020347-1-debug@rivosinc.com> References: <20240403234054.2020347-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 943D9120002 X-Rspam-User: X-Stat-Signature: nect9jimirsy4r3c678n9ossghq7frja X-Rspamd-Server: rspam03 X-HE-Tag: 1712187712-360451 X-HE-Meta: U2FsdGVkX1/0hyG2utu3csc0w3o3m8gcTA073IlERTDG7dE8N/HJ6NGfRX9yASu1LAt/89rSDveq16Fs3bKB4sl9oPP/4J/QD4sBDk8WaoqQdIikQazAke7Ab1a9tr4Wz15UawPobWgBPR87SFpI7FmvPpoM+leFdQfF/i1m30jBI8AQLa3oyeC23mHDn0PJH/FPnM2RVNnN7xi6+mNcdSgOrRtzr1CD1Y3Ws31CohBlKR2iTzwuUhpznWuGH5sMdJrw2Iwmr3GIwYDYZAL9f+cLWMFVZ6LecrFU7Y5E9zNf5yLPAeMoT8I8O/dGW32oLeukbASW78JWcFX7x33aBjUP/70/PDGNzICgzInxnv7GlQo00lCxhOz1c5GyOHmrY9tJbaZDZG6COlI+RfhoVbydZ615SdzB5W35KHoq9iZN8InRbgAqWbuAd5esaIrH3KsJaLYs4iQO+xlUU/t9woenQCHsHaKLne6s7JjzngD50yWfQmkq0IOTdDrILon76Pc8xRstbucgJtZKzi+eYLdgfOKWW4rJMof/scUM/huo8/x2u0r6yDjPdD7vDePDZjuGwHnYVS7YN63CPT0+3nAanlX3l8x5+72VuD310fOcSaJsdPbC8q9kBeEpXKxOs8c0OJtQvOo9ufNPPehokGHE5Sgtu4fNJUtBjDSE/1NgopkSSXHaceeoGK5/B8F3mSERwK0GFWGr1ZHLbEiavsAFXqtWgXcsh7PcmedYAc0WUoqfh8w8xHg/W31a4pXBLwTW2RuV4UPgRNCvvmxC0H7nt4PBNsd+jKbzj1UsLsQW0jvsUiGQSlVIXByov3UyL9vRhNDY5KAWPSmOzIwzPfsbEGAfiYvSG7AoELqI6J4vpKndqE4qR2yIzBJxIxN6DOR0yhMcJcGRMCtyVuoOP+OBFP4oRKB+TR5VDzhHxrH1nlGq5Alqw4JtZBLEEa45Hy7ljuEsbuJGpLiHk90 pAjfZiEu Y73XFUqcCUVk7TIeJPzi1/owl6kRfgkih86SSCQ9g385xjTR1E7rkaMyn0UQxCKI3aXKN0Jsu9PglQEHo8V/2LqAIRDvgjab3myO8NdTKzuBwYQj+Va9/BKbMoHNgmSm/XdQ5M575smHjU6VZzsg8/9Uhjgv7pdK69vYSGwu9IMtDROtVFfKx3eZJXuJ+9oOYCVRKQpHh2ITO98nvbuKJpiYS8YK4HXbvqy9N/Yzjcj4Zk7g7oBDemWT7laza2LGsEVlTwxCYIV/s3/PYtS3dgoEcJKyyQ3iX3Hev2J0GleKq6n6O3jdGKSP+i7lik4o3TnLqWn8ipw0X/3kaux3XDPAnzTgAt4Y8WJGCVh3y3bckZ/qQ7rmn5rZ9wdV/65cxs5nIcmvN3KSl2K+SSxg9uuRM5ekCFpS5ECiADjxsFklXtpDLTLe8xSjtb4JrGuxID7WvwhQikEhPnbKtrEcwCJ40Gqs0fiJUCCfkRAjZhkaRTgsEQjc2cw/RKlPMDqK6C2CBIPRMcojuIqTxubjUJeoI/QM0k1sm81nb5CCRObuq4gpr3NT/9u2M06HSKWBpGK0MBpE9JM33Dn2A3zqRvys6kVRo8EO5wNdMYQx0/BS7b7vMn76Uxy4RbQ== 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 (see links) To keep things simple, RISC-V follows the same. This patch adds `ss` for shadow stack in process maps. Links: https://lore.kernel.org/lkml/20231009-arm64-gcs-v6-12-78e55deaa4dd@kernel.org/#r Signed-off-by: Deepak Gupta --- fs/proc/task_mmu.c | 3 +++ include/linux/mm.h | 11 ++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c index 3f78ebbb795f..d9d63eb74f0d 100644 --- a/fs/proc/task_mmu.c +++ b/fs/proc/task_mmu.c @@ -702,6 +702,9 @@ static void show_smap_vma_flags(struct seq_file *m, struct vm_area_struct *vma) #endif /* CONFIG_HAVE_ARCH_USERFAULTFD_MINOR */ #ifdef CONFIG_X86_USER_SHADOW_STACK [ilog2(VM_SHADOW_STACK)] = "ss", +#endif +#ifdef CONFIG_RISCV_USER_CFI + [ilog2(VM_SHADOW_STACK)] = "ss", #endif }; size_t i; diff --git a/include/linux/mm.h b/include/linux/mm.h index f5a97dec5169..64109f6c70f5 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -352,7 +352,16 @@ 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 +/* + * RISC-V is going along with using VM_HIGH_ARCH_5 bit position for shadow stack + */ +#define VM_SHADOW_STACK VM_HIGH_ARCH_5 +#endif + +#ifndef VM_SHADOW_STACK # define VM_SHADOW_STACK VM_NONE #endif From patchwork Wed Apr 3 23:34: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: 13616773 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 9EACDCD1292 for ; Wed, 3 Apr 2024 23:41:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 711176B00A8; Wed, 3 Apr 2024 19:41:57 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6C0496B00A9; Wed, 3 Apr 2024 19:41:57 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 519376B00AA; Wed, 3 Apr 2024 19:41:57 -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 2C3946B00A8 for ; Wed, 3 Apr 2024 19:41:57 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id E125E407D6 for ; Wed, 3 Apr 2024 23:41:56 +0000 (UTC) X-FDA: 81969845832.05.360F337 Received: from mail-pg1-f182.google.com (mail-pg1-f182.google.com [209.85.215.182]) by imf01.hostedemail.com (Postfix) with ESMTP id 157824001B for ; Wed, 3 Apr 2024 23:41:54 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=MG51YGxy; spf=pass (imf01.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.182 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1712187715; 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=54i5AOpZ8eaoGeKx2/tq2pnpvo0kkfTAlmLvvQQ7IdM=; b=6uD9aLiIVVilqD8yZ2IHdrKmSHD/flWz3VrXSDhQmJOMkPKDvtsdA/Z9G3IcU40YMZ/4PN aFknSnWDC+zsudWp2xwj2taVBmaK9OQYy/IoAXQQYoS+BZBg50HMIVNAm44PatyTG2kJVw ve+CdrK59bY2oU63POXSBtNszoDHbfk= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712187715; a=rsa-sha256; cv=none; b=BRLhGjXsjSchBaBZribpR98O5oNy0YrtNfs5+Ae01hD833c3Ez5zpnmD9iKgyRgvvY4xX1 eu2x6LrchGRNc3mYxt2K3YwrNcEiz5+kQZGPZSnx/VD8ttZaKYvoNTaQ1fZkmsInUPHpKC fNwp1DgsbwRt4iEU61jjaBbmcFGZFWA= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=MG51YGxy; spf=pass (imf01.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.182 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pg1-f182.google.com with SMTP id 41be03b00d2f7-5ca29c131ebso331159a12.0 for ; Wed, 03 Apr 2024 16:41:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1712187714; x=1712792514; 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=54i5AOpZ8eaoGeKx2/tq2pnpvo0kkfTAlmLvvQQ7IdM=; b=MG51YGxyciMvtwyLXx3DCy6twja7YfXSUoun4FryhCxxookSrGWwukTotMPA59ISq+ /G6/E0tvJjcMbk1DKofiArzWrT2Yef2jpdJaZq7wgYEkLoqqc6AWnM1HD91T/NxeCKso H/UAsqyPib4K3OeDP2mZYKR2Vxund1fDLISUHngGu6ukem0pRq/3Nev+P7KxOAOCj8Da Kg4SPvc9cPxkkl/qgJcmwurGUqSIrmb0379wHiYOW4f0IfaFIjfl9gzUVORgke6RLa92 aB7hxFlsFS8Z+8veYy8EpYJ0/MiAqjrD8oyYiHMKNTpI3OmhBAMaDOAu+hKGmiAsfvLi rzrg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712187714; x=1712792514; 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=54i5AOpZ8eaoGeKx2/tq2pnpvo0kkfTAlmLvvQQ7IdM=; b=pLD2JpXtYOFfTiqn2B4wfuLKKzBAq281s2ENJwc6EP2vAiznGnZJgXplhvl/7yT6oh 1WgyUD9GN2+mvfuUrTjy8Y/Y0ow/YZZzaKhp2rFCrzy0ASyKEfv0P6xZX7pkC77pviXC bgUNNU3RjjirunGlDdfp7hU0Tb0J+jFKRzI8x7bw/RZ7UDKKqsvQFBiFTCdPGPlWzLNQ nkJVJsyLq/Zy8t3C6JcQsyZDDGWsaHHKCPRHO9CdiIOWR0AXk9w+zeo3AgxD58+KUzu3 ziP20WXGdEgJVJrowMpyFY5B/+vhISydkFlsGKujliOawzn/FFjC+lfqpVL7GF7N92ON 6rCQ== X-Forwarded-Encrypted: i=1; AJvYcCUY65NM5RLWB4mrmThG9gE898skxMCA8/6w3z0z5lFabcoVuudB36dwmYxgDpQakxU/ycfBXVmgdMWIsXSLWt1doTo= X-Gm-Message-State: AOJu0Yzrsjmj1dF116jtcbHnFryBckMHy4T+bP+pK8R2+9uEXhgFhD5R X+8WjrY7S86Q6ImO0xCDSzfC9DDW/o2Y12Ra1HUT/V/F/od1qo9KaLUsShss8qE= X-Google-Smtp-Source: AGHT+IG6GlC+ZgzT+vgM3OEvECwnyeJlz7W2zSZ3hW3riRhMkNJ5fFd9UMQXlTdOrd5KXzyMbVV4Yg== X-Received: by 2002:a17:902:f085:b0:1e2:a5b2:ee0f with SMTP id p5-20020a170902f08500b001e2a5b2ee0fmr628269pla.24.1712187714025; Wed, 03 Apr 2024 16:41:54 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id b18-20020a170902d51200b001deeac592absm13899117plg.180.2024.04.03.16.41.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 16:41: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, conor@kernel.org Cc: 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, corbet@lwn.net, 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, Mike Rapoport Subject: [PATCH v3 09/29] mm: abstract shadow stack vma behind `vma_is_shadow_stack` Date: Wed, 3 Apr 2024 16:34:57 -0700 Message-ID: <20240403234054.2020347-10-debug@rivosinc.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240403234054.2020347-1-debug@rivosinc.com> References: <20240403234054.2020347-1-debug@rivosinc.com> MIME-Version: 1.0 X-Stat-Signature: og4bfxzzs4yaiiyy1camen8gf6qo1nr7 X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 157824001B X-Rspam-User: X-HE-Tag: 1712187714-317514 X-HE-Meta: U2FsdGVkX1/LfHlpW7kXhg5mM633zRaZFZB4qO8ilkW1wzy31+jZNZf0vlg6tEEIAlAzafhTNKSoMIojeiLZ6fjPCRJD/xyw2vrpJZGEUvDE3ixpKbpL04ozcq+TiPWHTOgWsU5uqJ8aTrhAuq29XQFKEpxhhobnmE5OenVFSgzrSqgbnifiueEMSBEgrm5zff4pzc67joom3LXlD846OW8nk6F7D9MnDr5KaPdL2uJAU12rcnRH+arG3D4IN9UeZN63Bs+V8B8c7S1L9hXNV/iI6B6dOWH59Y3mYuqhPtM9b2TLDue4xmDmoYvnslhoMNR2WVVs4vAYVRDKAV6X9ehR3ab6gLYSnNgIDHgHw1E+dpASkg5JRLUAERldujIjvwiD6YmhPGJ6Br44z28wqcM6+XFJ1gZYb4CfehxCBFKqF0FqBPbpUdo9u2qvS15RZCKBAkxGpisTNb7B9UvwKgiOANa3y6J1Hj4zwIe8zmmOjJGgxeJcmGoFtITRs364Ogz0SMWenok5c03S7Ze6T3tjwslbQ/Is/0Lad9movUkZP7FpkBcLAcpPgL9aE9FaGPxMXyYr1dgwOcjle3AqFyk7xEO0BFJ0iNegi5wXNGVmRZgYD8iFRPWL9AnQsNykZXEgbNOyl1RILkrnvRp5hQkXbg7bGF/+1N6q9kV4BD+hP9CshKuB98XbT7cLYLYByKQDmCWRTTCzuHFGSway60Sq30uDFWkZJoahwSvr0ZAP22qk52ml+rwCD1/rn/GHWTSPLLykg26h59gu2FO9wMkdSn8b2oCzZJyVhhw8S94QoMvtD0eKeiggGR11gXqalngUv80cAzA3Rlo5BsFqxBPd+pHc642g2WUY4fUr1T0KzI5QH+NxW3cHh7JwraX3k2C74dH1yFcBCwXhdcBaLV7pp3eCXa4XTwhPXe8fgKx5x4onJ0td7KK8R2Mdq3i/Fmxaky5kn2V4ri+odYK 9uTBl3x8 nIKHRJPPmGHFS9EpsjC3WpDLypFgJpsmRHg66m4OMsHhH7m6gJpPbxd4Vj9S17vc9ytu/0ItYWqTrXor7CELri6P77G4bL3X/igjnnOKwetCuzY2uoeSWQHpD0SwJbnH8oY+3+raI9AyAKHbL3tQgOKaTDrHCzl+RYLE6fPcTMFnYKK+ZbOgUUgOYB4FKBnScwpibSA/TfKpEsfFC/Fg4NVTUMSrWr39MUV/n4tpqmCl8MhfjNbJbv4hQcBdsFT/6gWBBF6f546BiLNGvbCgFPL/yh9H33zRHr0yMuMLvDqZiennElxF/yYK4mNWBSvWuQtveRm8ryEPCPWamWIeE6isK5K1IxTMyMG5I8WPnohfr/YX8oMJyLS9jSSJXT1u1ZG6eknWvZiSf/kzOOgxKUERBC1opZzdkoapF7g83tJQqPF6PI5AEf8djM51crRfGQU/sUxZ4xdeL2ZVIpQuffQM+llOCX9ypRqmWz/kXF8LEwiL4UmaIcp3K/8meB6plMNGCYcRTi9d2xH3+7l1W4FWWE+kNhtzLHu2Yk8XNMWcjer/GMersXrvPatkBlZkY35QvZ1TwXtYzOjEnuNgHQx51NEDyR6dwxxhJYyZdVmOe4rTDg7VtXtwvZA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: VM_SHADOW_STACK (alias to VM_HIGH_ARCH_5) to encode shadow stack VMA. This patch changes checks of VM_SHADOW_STACK flag in generic code to call to a function `vma_is_shadow_stack` which will return true if its a shadow stack vma and default stub (when support doesnt exist) returns false. Signed-off-by: Deepak Gupta Suggested-by: Mike Rapoport --- include/linux/mm.h | 13 ++++++++++++- mm/gup.c | 5 +++-- mm/internal.h | 2 +- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 64109f6c70f5..9952937be659 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -363,8 +363,19 @@ extern unsigned int kobjsize(const void *objp); #ifndef VM_SHADOW_STACK # define VM_SHADOW_STACK VM_NONE + +static inline bool vma_is_shadow_stack(vm_flags_t vm_flags) +{ + return false; +} +#else +static inline bool vma_is_shadow_stack(vm_flags_t vm_flags) +{ + return (vm_flags & VM_SHADOW_STACK); +} #endif + #if defined(CONFIG_X86) # define VM_PAT VM_ARCH_1 /* PAT reserves whole VMA at once (x86) */ #elif defined(CONFIG_PPC) @@ -3473,7 +3484,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 && vma_is_shadow_stack(vma->vm_flags)) return PAGE_SIZE; return 0; diff --git a/mm/gup.c b/mm/gup.c index df83182ec72d..a7a02eb0a6b3 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) || vma_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) && !vma_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..5035b5a58df0 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) || vma_is_shadow_stack(flags); } /* From patchwork Wed Apr 3 23:34: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: 13616774 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 7FBB0CD1292 for ; Wed, 3 Apr 2024 23:42:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 341166B00AA; Wed, 3 Apr 2024 19:42:00 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2C9126B00AB; Wed, 3 Apr 2024 19:42:00 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0F5D66B00AC; Wed, 3 Apr 2024 19:42:00 -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 E21556B00AA for ; Wed, 3 Apr 2024 19:41:59 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id A040880620 for ; Wed, 3 Apr 2024 23:41:59 +0000 (UTC) X-FDA: 81969845958.09.20AFD5C Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) by imf18.hostedemail.com (Postfix) with ESMTP id BB6A51C000A for ; Wed, 3 Apr 2024 23:41:57 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=bvTeEPO1; dmarc=none; spf=pass (imf18.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.173 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1712187717; 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=6lX+9+cMysL2s2bh0/j36OWWXHnHHgmfQfoaQ0p+lk8=; b=KKCoTmct9WIVIsw7DTIdcuR0y5KzH7wWoYBhxNbsba66U9uUGdvaFljTHqSDzrvgWmm8/C HdBwNYCybAS6W9DPGrF3P+BUvRQUcdMUn/D5Il9FI0u+5BAVPaBWtlFtnx6+5kkh5qWjLF hMjsLpVUEusIiR62Z8avU3GdvltkHLU= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=bvTeEPO1; dmarc=none; spf=pass (imf18.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.173 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712187717; a=rsa-sha256; cv=none; b=IN4qKf3Ws4yx0Gg4GzK4qbQffLq/j+E0RH1eF/EUwuqMbodoJUfEdRcB7xcOMdcm/pkj+8 rgQj0GqfZf0wac5I62SRaS2fadAWOWdA7+BGICoOGgqzZ8EYVs10cc1P5Vr2wuIMow+FCO 9+y3BWmVhQuzpG74Be16d3ZCJ6xP5Uw= Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-1e0878b76f3so3542565ad.0 for ; Wed, 03 Apr 2024 16:41:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1712187716; x=1712792516; 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=6lX+9+cMysL2s2bh0/j36OWWXHnHHgmfQfoaQ0p+lk8=; b=bvTeEPO1eNVtH8A+ZZvgl0hIcMBXZgVS8vEArUh8FAcXjCcWVDwxFP9RP+36B1sgZ2 y5H/wdiGCbmWV+FDaXI2I0RgGgJbvxU2RI3CUIUGz0rSp3apJbgdi59blrqOCN3L8M0L hLyWhyxhxGZoJZj64G4AY/0dQqSgnjX/Jm+Ky+ShlO7KYI4Xm6l+plMnXQzOItRkUkH/ 4Go2F0X6b/kSlV8e4iAcZaTZMXLGyqFhYwsOmM+m+93g4Q3zXwDm64yTSuti0/vv+hUx uryeyDIfDikDoxBH6ymgozWf672sCfLj58FF8DULP906PfK189bpzJmoeDuiEJDRu+X4 G40w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712187716; x=1712792516; 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=6lX+9+cMysL2s2bh0/j36OWWXHnHHgmfQfoaQ0p+lk8=; b=LxdOhYcqSKb/UlZPvfxMqVRmnAsAY2nfCRrkKkqe/fJgoPTGjXSU/56GEMhKZ7DFyE lUTfa02vasRtJKrRWw9Bm/MRaKfQdpA3z9/DJXcgyShvxT9/1goeeh+v+6jcBb6VKJGU vlcWATSPIJmYhDPxB/XFL8KMYtCsU/M7P98RLHXQ1LZtan1U4HSpG2pC3s+Nv2WcSuKC jK5wrVlKTHUWSiSW0yeoZBq2cz7d8BlkVx3wX2faq6aPRYCLuVHA12psQbAZ3mHVVvoA tcSQpzTqFAVkQ/tOYKmlYMST7AMSNgB6pzc2drzilQGZF4WafypaJkQSnEtkXkRk0CUe fNiQ== X-Forwarded-Encrypted: i=1; AJvYcCXaqqr2DOZ8Bfclru3zvp0VATb0Nt2VUoZZ7aXxMc8jNsw1hxskB/vBb4eADGnGyGvqzt6EL0g1lD0GISkWA3cQtHU= X-Gm-Message-State: AOJu0YzDAGWo5wq+bQSG6PYJnYA4Hr1T9zQNY5bmg9hFl/kByqjkNP5e YPYr+lX5GsRtG1R4Eo2G//SFDZm8r20JBr1tvWIacbIXFY27ou7CusA1wvnoioc= X-Google-Smtp-Source: AGHT+IHn70WZbF+jlCONe2EcRRr6ZCh1IRqHu+QHyp4yjMBW+7E5Ek9bYAqg4ymMIf0JA4HyBbr9VA== X-Received: by 2002:a17:902:e80e:b0:1e0:b677:293b with SMTP id u14-20020a170902e80e00b001e0b677293bmr5847942plg.29.1712187716595; Wed, 03 Apr 2024 16:41:56 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id b18-20020a170902d51200b001deeac592absm13899117plg.180.2024.04.03.16.41.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 16:41: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, conor@kernel.org Cc: 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, corbet@lwn.net, 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 v3 10/29] riscv/mm : ensure PROT_WRITE leads to VM_READ | VM_WRITE Date: Wed, 3 Apr 2024 16:34:58 -0700 Message-ID: <20240403234054.2020347-11-debug@rivosinc.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240403234054.2020347-1-debug@rivosinc.com> References: <20240403234054.2020347-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: BB6A51C000A X-Stat-Signature: xsrb8gjo6efqhwmmts535uhudtcaoti6 X-Rspam-User: X-HE-Tag: 1712187717-719015 X-HE-Meta: U2FsdGVkX1/g7qAT7MEHHSvRg/pY4oikthAU88LNPgEH8X35wnGwBGXRroNst/HH7pz0WYisWOCqZ5iA33ZTnE4ul+8EgtWKO43/4Qtn9HrvM1oLD/9HLcxLz23YzFcu0epOgnQtfwRRRaOCiMN1BQJuM4NFK+u8UnUORd1GvBUIxBpPDhE1WCLx4ThFXsaZFgdDMFq299UXvSSg+AzuTGRVznzV+z1k59P72BMJBjBrR0lycm0ibwUPlqcxrmS2UPQDSz+0ALSSNTp+dM2Ic/6quqSHWid/bvNO3iCFLNmd2mBnNzclpupAczc9cY4pZM8bKKEVMyTtcR8P6qrkB9zuCekU+VVGS5yQKIkTp/rhBoRt9RevfWVNS1VS3D81R7XPmI94Cdl3SZzxDQUILC082dCjC1Py60AeJ18v3IPwKioXzwLxcD0UTUTj30mhBq0biRgVdYRqcsJfisL/b94+TzKzDY//L7cwNxuScbrs4h/yE0+TnhnGUAXsKIRrtM3KCAa9p3vkH1wfoBgIEIFSRLpbtj8TUmFXiSUBrN3s4DWmymvktaniv+drdaX3FItQYg20UxXVkEgCAUId+JxA9k5sOBPnnNEubwA++lbFy1zyDLwcJze4eO8D72beaxItYnSetyerpkb9NJFiVUE05kAqsh4bisY1YVkcRnN0B28ZVm9zoohAO+6XTgeJBxbKZJxmMaIAtuKPhfQF4/UlX0WxfLo68xAE0zWra+svhpJ5j7TXYsFcxp6LZzMLcIMHxU3m0Q7l7pS0mO+qziADEQbfigBmqERU4EKmNhT4FewEZNXdiH+uPV+ORI//0FjuS9vAZLAJNm4WJ9YA+JqrPvrfTzWxfFtia3ELffllrvW42o3r83aAEPaXDtcp6V3H/c3w5+ZqCe3PPjJ70dIIpklgoviIzisGQLBOEfIqt3964BXWDGTnL95JLvNSFfuE+FxjJlOe9y4M/LY oum9EIKs i8fsX6uYjRY/yOV/DwBlg44IDh/cG9aiDqPE161zo2kgNzwCCLPO/0EkYIQjOTSdLnPTk93ZY9c95MaGRtCWrPKUFYpid8baL/RJCzXdmjm6yAht3ZXuDqbSD8xdRa60+SSPCjJG2wCwYTm9ZxL7tNJX/tgwK2exhY2N4ZkaDBRmAi4mu6IL2G/rIBGlUa1ANQ5vMgabX9+Fsz6EwWqRLz1w3NmkB7AO4HtJAK9OoqvfFV09BjtmGChvtlLx6Sf0kKA155FKRSeSIAseIHKO0M1n53Yv1EWTHtmcFeaotngddMbCbnPSeOWZBYSgMsT76WQWRYBQFFwp9pyY+Yl8oTHtnaoPC+La567RU1r5xq2mvinFDY8cDVfS9xfWvcMwm+soR5dE9koAI+4mfB7BM1OcBaXDrdsLOi1eZEpP+Ywthcgpx/9u5dIvVJjOgTw2vNOXpzpPPTnv4H45CGFMjWCCOPQyL19Pg71W09KQO/hbqCCmw06uwCyqjeJFG9yKO7OAKh3XGR/ApghVONBJJrXt+OTMWD2/vmY4qEEZ354ZahYVSsU8wBA7k+tdA0YzrizgJ 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 Wed Apr 3 23:34: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: 13616775 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 B7FFECD1288 for ; Wed, 3 Apr 2024 23:42:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6C41F6B00AB; Wed, 3 Apr 2024 19:42:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6738D6B00AC; Wed, 3 Apr 2024 19:42:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 477E56B00AD; Wed, 3 Apr 2024 19:42:02 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 28C3B6B00AB for ; Wed, 3 Apr 2024 19:42:02 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id F2B321205DF for ; Wed, 3 Apr 2024 23:42:01 +0000 (UTC) X-FDA: 81969846042.09.FDC70DF Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) by imf27.hostedemail.com (Postfix) with ESMTP id 4159640008 for ; Wed, 3 Apr 2024 23:42:00 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=L3VwW358; dmarc=none; spf=pass (imf27.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.179 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1712187720; 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=UedSpkjPZERpvMedykq6buX1juwVXHZ9WNliDzoXMOa5CqRpqIxf5OAaDM3v0+f+xJ1CDu ynr3rupQEnw8EsabX32kEvEZsdBQ5MyXf8W117KDspuiR/v+8h6l1KGi/oJ9czkCnsvRRg GL7qT0Xm9q5v3H9PbzKwpykNLGAepbA= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=L3VwW358; dmarc=none; spf=pass (imf27.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.179 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712187720; a=rsa-sha256; cv=none; b=4CQdKXOHrEsgDYR7APwlWwSmI+kz2O2hP3yYYFnOhuNHJ5wIAPxZpZjERzQj+1/DxCURGW WrLlZxhHZ609X1hfclWpiC3r1Xs1X9lm52wXObd7wxg+7cJEnFE9+v6s1LnlBiocp2N+H8 Ui7Mf5+HHEWEa24WqeKaHHy1GSMw6eI= Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-1e0d82d441bso3505585ad.3 for ; Wed, 03 Apr 2024 16:42:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1712187719; x=1712792519; 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=L3VwW358XQqzhQkaYAuCM/cbpQEaam4C75vpZB/qHgKXgz3K3jEWCHPYFhgCDG6d7G 9edIsXE0VvF27rJOYmZbbujBRJolPWfXfkTukMxGbMopA3wgy6yNnb6nQs53wy8Rh+yc 4VDoUJpEP1d0y3Dc5pSfoyf5tWwVFD9OfYb1NVqYAGFGGoYBRGzXnalTA2zmt8SZu7Wu +umHcS3r9wwoudHkUJ8m+VPwfCoYdPl5ugHGv0+D0hr40dqVRaUMkZ/RQEMj4Ssp5YI+ a5CIlAqYoWAmGegqrReuHY3CZWxBngjLwAMMCQ1CeAVStooJQ6myDINsgl+Dgmz1RjAG Hvxw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712187719; x=1712792519; 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=i5yZJ97GGb8TErkkjKwQXXix7WX+LX9JB9maC4bA/O4YucWXVZHuuHRaqAGlotw7Bm bxUChagNhNn7yt6u6FwLCDIPktWxbi3rkYO1z0G/MKeQTtQgwnYF3yhvX5NbKqxC7lsV 3sMdDcGi13BIY+KJuTblSmroIq55UioQpNtxBYJazNKdvT5kMMQzmllvAvG9hv0ON43S 3CmGtWTD/9hMcBy6Ekl8dWGNryT4LTmRiqCjXjEQsHw1fRk5utAMPhU3cDVaZkJzjziw jUAH3MD+nCL0kuV41JBw0XmnsJFKvJIy4K46nv4odEtL7ANuK8OMB+nDNiEAbLNaj4Wy o3yg== X-Forwarded-Encrypted: i=1; AJvYcCUM9bqaTUXvcOMhdXD7tJHI8ptpiyjyWUvPTXDXRGLCcWDPmbjzT5V7ZQDuJpqWCRl+ztnN/CEScupCW0Tukkpn5rs= X-Gm-Message-State: AOJu0Yz53/w15tMq5tQxVZFPeB2rrQ29Gu22ORcltbyxXyRJC3+YXPKK vN4BgZmOnW4N8Y1XcJ0GzW97KdVKDV4TukTp5wddi8f/uX6yRMsPkHZBSuYn7yI= X-Google-Smtp-Source: AGHT+IGh4iyC/GhUryA4pkxFBs7yNJ36fG0Y8vGsIbyj+jFCsDK2F7CISUUTPnXh7UOmjs+99OfNvA== X-Received: by 2002:a17:902:f105:b0:1e2:718c:61e with SMTP id e5-20020a170902f10500b001e2718c061emr712135plb.27.1712187719240; Wed, 03 Apr 2024 16:41:59 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id b18-20020a170902d51200b001deeac592absm13899117plg.180.2024.04.03.16.41.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 16:41:58 -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, conor@kernel.org Cc: 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, corbet@lwn.net, 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 v3 11/29] riscv mm: manufacture shadow stack pte Date: Wed, 3 Apr 2024 16:34:59 -0700 Message-ID: <20240403234054.2020347-12-debug@rivosinc.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240403234054.2020347-1-debug@rivosinc.com> References: <20240403234054.2020347-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 4159640008 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: g8pta1nm9rznxn9nar5171s9un7kds1h X-HE-Tag: 1712187720-119212 X-HE-Meta: U2FsdGVkX18JoVfQ6r0Q9vd57W5yQr7Uh/8mCHsKtw77zMs6pLRway/tA3uV3ZTFtCCSESd06XUl+b0LWdVvAonSKRdq6fLO3cDyOWtZVYU/rgDdvYloD9Jo75AY7BAuQrk2tffaK3ZrFSDr2DYtHui/7mXqNlXT01+p9QphehY/0oeX9+LtOai8q214IHFG697oqFRtqtiF5Nw/qB2V5RKzF0RDRXcyedlqclwuR86vT9/HZG8toWz1oP6NPQeh5F63vfLgReYlK7l5GAp0VuHPypB6BFd4dKKO8zoE+ZZE/3tUs/1xlKFt7R3chOeaHWaZUqowsfN9bAXxqG8WSYtZ3grCpZd7Xy3+dK0Oqev4a8NpkWxnkl1k6QuoGGYPDpcx2K5+gQYS1i207X8avkT9OnloQ7CIYSnI7a87kPz2hzpCMg42tUzSFteoX6B3YFBq91sYfd027yFdA4OqRUrGQDXA+QsLvd/b3hD0LIEZLLnQdIndkwB/3aKUf6jR/fPYklcmwRmwX7rwalC+bsoiAxG8AxC0RGxP6nvSgT2tEegEBQaNCIpc41FoGBdTwOqF7NcbrPXpLQBzA3/gz8uVBDrf5NM1GWI1Ex0/IHzJKJDIhnkF06rFg2THdrmtRILmMdaD4D3KjhKbsCSpJWft4oz2OP2nOrTP+41tTKsmhtbBS22ujEGY2sl1k+qXgP5uLq8tsqXrpT4zpZmm7nLMZeXHoPoD0mSz7/9RjuRcpAgVtozgTG8yTkU/MoKnP4OBqpygW0MshDff+IwI4rhdY/sWvsRjpTeqxIMqI1G+enw7sEW7D6kN2idf2dccKY5zJsjM6cI72VhzVjaag+yewEge8b0aITTnEQI8DV6WBvYkcmJ5TaxYx9lu4Ue62nFoX9WAn9eho7S/Xy09DEFJhBcLSzwv0SA2tpVq4p88xPpCbPwqW7TD03ReVsCXjVPu4ZtlyGJ6JO08QY7 CzF3/Tis alMI6UjAGSemdfRlErm30t/3zU+mUlG0TX3/o6W4VSseHJGMRHEf+4WOES3ZmjXnJQoGded3Mi7m3H1JV/a3dj743OiVn+Tl+X7N88wpJ1Tq+hVZoYMRqR3neSPssLOxIgNmu/T45tilPE1zxqqV4YGRt9epZJhwcrzBjxcAy2p9WpU9G8G6nJHh5PNnHXVwFVvMzAw2NcAP8orsFN3T9HLh0nFrWW/A2kRt2fAy40kJhJ8g= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000016, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: This patch implements creating shadow stack pte (on riscv). Creating shadow stack PTE on riscv means that clearing RWX and then setting W=1. Signed-off-by: Deepak Gupta Reviewed-by: Alexandre Ghiti --- arch/riscv/include/asm/pgtable.h | 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 Wed Apr 3 23:35:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13616776 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 05ED9CD1288 for ; Wed, 3 Apr 2024 23:42:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4C6B96B00AD; Wed, 3 Apr 2024 19:42:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 44F966B00AE; Wed, 3 Apr 2024 19:42:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2A1E46B00AF; Wed, 3 Apr 2024 19:42:05 -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 0AC1C6B00AD for ; Wed, 3 Apr 2024 19:42:05 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id CF3A31409E6 for ; Wed, 3 Apr 2024 23:42:04 +0000 (UTC) X-FDA: 81969846168.30.E236BD3 Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) by imf11.hostedemail.com (Postfix) with ESMTP id 0713540004 for ; Wed, 3 Apr 2024 23:42:02 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=DwMpDvvb; dmarc=none; spf=pass (imf11.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.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=1712187723; 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=ir+lgmmL0ZmHswiBuJpYbgwfILZ/3vCYSTqJIvKZkPQ=; b=wt/cscaFmVNOlIBzUVd15lqaUyTnxXc1Mw6j+OdbjB0bvssf0iMIVxWg3FTK/09gE1Hn+q 58qfiwfV6DJNP8Iic/C0p8sUtfwz+4fLJ4KSpV7qWbBoZyr1xxpxR/cHt4KS+rYy/DArB5 6zzwaO6qd9Czx5rhIVdU1gEWVc1i9cU= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=DwMpDvvb; dmarc=none; spf=pass (imf11.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.182 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712187723; a=rsa-sha256; cv=none; b=Ii0lmQ0gltRCjFBp1jpg+ltDWjDkZVGF/6KitlpzY2bOW5w9enEygRIhTwsoz7JaU5LeZs 5EOKMSkUqxCOrmgbv0da+LC0IQ4WfniTuprGgskZzT/KSx5rRhCOUaWIyVGKmRJStbcT7V PIlb/7didZ5jKOnB6oDT9uC64bJlETU= Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-1e27fadbbe1so3263485ad.1 for ; Wed, 03 Apr 2024 16:42:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1712187722; x=1712792522; 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=ir+lgmmL0ZmHswiBuJpYbgwfILZ/3vCYSTqJIvKZkPQ=; b=DwMpDvvbVKNHPW01AewN7KAQ5NVIzXtrPQSoUdCyaI2DCFJ9YccZ5CCZRiiFMYQaCO d1t5dFd+Rq4J1djEjIVYNRlxbbRrmZHYo5j8L+B+x8k+ywGGcxhE7uFSlmfyH0P9vIQv teayj0juFO2fi6KridUEV5aYiZdlfnAc6KTeJNPWC2cQHikYWJmPQ2hfqQirmuGyYas5 +QLpe8TwZ8WUxIpbwfml2KIIuuceTprUDUT+vgvHLWL5HuAkjl4p0YsIIyou/pOxbpmg Gpgj0E9CjTZmvSLvJE/th87XCwgBtAzx6i74YG5oGVJhSH9IOguVTErPeMk0CfZ7vJ2a usLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712187722; x=1712792522; 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=ir+lgmmL0ZmHswiBuJpYbgwfILZ/3vCYSTqJIvKZkPQ=; b=NZS/JQKlrjCdHl9dQJ1BwQMSKnZ+Xwf3SuO0kfYYCpvSSPcH9Vlk3Un6lSYftpudMX 9G+6bR8mn86xrt90qzVhifV19ObpIZ7ze1poTaFcMfCprfmRoITZHU8DTDq53N1Jalyv VgrUbcVsSPhmIskGLulv3AdC2eNfaP2S5INfKij3a32PF7ojlJjA+y68NSUghUM/xFju NJ9IPyRaCOwoi5qFOBM3Vtu0qfQ2D5URy9MEd4Dv0dA/AKQZLZI0a1gSC5B/MDIRfnCH mWxCrikGCfLc0RmoQ7Ny6eaKMR/8pL5KSDzRpNHkz0gDOX7dmaWLruFZ26dqcV96lr4W q4Lw== X-Forwarded-Encrypted: i=1; AJvYcCXyLS/Zvste9g6OZBwu7xAHpAXS9c3KxiSLD/nninFwrvmgwY24oyB7xhZAYdJWIw2EYiUMqIW55E2ZqPqRTbjAJZw= X-Gm-Message-State: AOJu0YxsaamxH5ptW+nvVHgQ9Y4Du7Q1sbKbCOc6MM3rWXsvr+9rwJ1E 1PF6EFaxHGrM2j1mhKtROLL/AatjFz0ZDfTJhvskUUtTK5eUNCfsLn2Wp92Ym/s= X-Google-Smtp-Source: AGHT+IE44MbJu6BIDOc3JQGIbnw05ABlT/b1420//7Gxn6oIy2GZnOOaOXzQhU0JUqBs5V+YKteclQ== X-Received: by 2002:a17:903:22ca:b0:1e2:3851:6b6a with SMTP id y10-20020a17090322ca00b001e238516b6amr860088plg.65.1712187721821; Wed, 03 Apr 2024 16:42:01 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id b18-20020a170902d51200b001deeac592absm13899117plg.180.2024.04.03.16.41.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 16:42:01 -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, conor@kernel.org Cc: 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, corbet@lwn.net, 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 v3 12/29] riscv mmu: teach pte_mkwrite to manufacture shadow stack PTEs Date: Wed, 3 Apr 2024 16:35:00 -0700 Message-ID: <20240403234054.2020347-13-debug@rivosinc.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240403234054.2020347-1-debug@rivosinc.com> References: <20240403234054.2020347-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: piei7i71yb6xka5mt4im85rutw4dnhwu X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 0713540004 X-HE-Tag: 1712187722-255480 X-HE-Meta: U2FsdGVkX1+5EyMub2Apf1MJz67k2WBDHIl4SG5ZR5njbzUwCYuLXqVRaAh2js6p/1gqX86DyXUOA/FWAGhfLZufiF2j6NmWZ9SeB1TRNUP+S/BNmsJ0KwHBn3tGeLIZF6enasKAEhXKY+2oOjp/Sap+9dOKQpdzKAEJZz22owBZ3iRrokZ42HbaBtn/kzkkAXy2Skl7RdXsm7Vxt+Euwqn8UN+cmAPbU7B8h14ujrwRTrzbk/vhGW7fsJjajwPyebmhS/MZ/tymdCjyQIuX0qT7122NG7BNefTg9BQnBiJCZos5zPDo3AjAcWVuNQ354sMhcixZVFT8tAUa5pHe1Ce5UhoNjQGA97+XtBL2ZLQorkNc7uN6OuiibeAWr87fua20xS6xZ5kF06a5uB69XD4Yo6F+0pyBnRgQhg+MzksAVuPhf96rItOGQ5euRcSHQ1NUxoP01NspmJeGki8qXHnr0PDFUwdALd6TCbcMsyRiiP849CAT+gHw4ieta0Dl9+W5wXBkeM9tIMigCruza65ZrCN5bbGDU/Jqe2w5kQ8hxq6gRkT2wE0F5X+fLbG684bryKTRcrbSxCRpBbfxOOYNHLvM4aVk+8dvQydD3t0teH3tk4bBzH1nbmaDnNG/Y6g/AwRU2hiqFwMorNypg1VXg0AtAzs9a/lDw8Ha3WGs9NOw9lfe+AEe0I57A3MiVEEq78fMJC+BDiY3F5LuZ83ks5dYweSRAmxCxnOyltzDIgTkKP1GX5BEwb6TCmyAg2f/X1+hjOGzhkjhjFsj+XDZSdD6i/MojTmkrYxgDIESjTFYeJXKBBBKQCEom/91IYTsBsP3vEMpbP2uDJajunVUroaPheyMV4kVAVxrWv+F2mRUkZ4qYiuk/2fEYnGTTl9fZA869qQWMU2BIISI4ZPSOtb1DqmUi6glXmO3JqOwfH9uXmyApcIcMOQF9b3bA5Aneo68p0+8mBkDIMa VO2K86Ub e4xg3pphjqiNROh3S2cmguT/4h05xTi5Evn+QymJRsK1+T7ZEozAx2YUdvpY24m69/d29NI7tfWQp1buPqFUXVNyP/ybtguLRIq4Gj2v0xx0DmuoXV74LBV+8C7gRT8SXI0BvJknd8tYqzSTaIQBVjCwjkt158BJC0umm4V89hiz+2MBQz4tAJPMDQ02XHrhzYw9NRjT2ikU2EGRHPUXONloY2VzJ+Cwq6x4Qgi7WpVc2r6DbT3MTwcWPelXVe6YIzYjcUHpxH0x60E/jexPaYzv1M85HXogN+Uz14jQpO+e7R02U6td6/7xezBOKz+mma9HGNr5Qf3I2gmplVER03kyUh1XOTijDhHZKw33WThzvSmYQ2omIf6YyEpCTH2tclueGz+4BwuF+xwMCj/AfAWE9nxPDwPrwGbNce38otvsiVADXee/C0WX7ibkuhJ4opPlg3BFimw2R5nttNpVDNHRQneKpVJOewZtmmZuTsF2Ey6e7/fXo8zbGpsWbYO2mcS+vraud7yJdPi0hfm87K8MY607AR1XAs4kQmhd8a/es19QrS/vS7K6s8Yek/6hDHa4D X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: pte_mkwrite creates PTEs with WRITE encodings for underlying arch. Underlying arch can have two types of writeable mappings. One that can be written using regular store instructions. Another one that can only be written using specialized store instructions (like shadow stack stores). pte_mkwrite can select write PTE encoding based on VMA range (i.e. VM_SHADOW_STACK) Signed-off-by: Deepak Gupta Reviewed-by: Alexandre Ghiti --- arch/riscv/include/asm/pgtable.h | 7 +++++++ arch/riscv/mm/pgtable.c | 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..c84ae2e0424d 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 (vma_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 (vma_is_shadow_stack(vma->vm_flags)) + return pmd_mkwrite_shstk(pmd); + + pmd = pmd_mkwrite_novma(pmd); + + return pmd; +} + From patchwork Wed Apr 3 23:35:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13616777 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 2D0C0CD128A for ; Wed, 3 Apr 2024 23:42:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E8FD66B00AF; Wed, 3 Apr 2024 19:42:07 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DED806B00B2; Wed, 3 Apr 2024 19:42:07 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C17D46B00B1; Wed, 3 Apr 2024 19:42:07 -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 A2FDD6B00AF for ; Wed, 3 Apr 2024 19:42:07 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 69ACD801E9 for ; Wed, 3 Apr 2024 23:42:07 +0000 (UTC) X-FDA: 81969846294.18.F24394F Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) by imf26.hostedemail.com (Postfix) with ESMTP id 9A54514000A for ; Wed, 3 Apr 2024 23:42:05 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=yQaTLo6s; spf=pass (imf26.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.172 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712187725; a=rsa-sha256; cv=none; b=WIJ7NrRAmLOZgpOKOW95lows2usmVe/Byu8d/rrcqjhAtbhubR8gqn5GH7/ZewAGAD3ykk XFgbb9Z5rfFDRF8CUQWOdEHdRSA3ex28bVtIqK0MdBVJyvZ+hApupz6LR5tirCCtg5wext gqZwzwHb3IC6C7lP7AI/GFS5uswXLK0= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=yQaTLo6s; spf=pass (imf26.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.172 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1712187725; 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=ztoivkeDxnyQWb6Qdj4Y0JdEjR6rGcTRSSTmB8Fn8HBYlZP5C06extxf/vwWFltHBRGWLL ft5SmyFB0dn28BDsUnDgnEKS+1k8woH23cEqwZnX7j7rHjE27Wi8zIeSw6kB7vOiZCIbG+ mu8Bs13AvcJeNbcRpX0lN5tXM7KZ6yU= Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-1dff837d674so3265845ad.3 for ; Wed, 03 Apr 2024 16:42:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1712187724; x=1712792524; 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=yQaTLo6s3ooTDaXTzmnUZf6L2xv2Yeq9wrVwpXLngziGKM7RcsPZG4stYDqX9edp4D 7IQ+hAo+mTp+hQGyjh/NQcEIW9RLSeJUDUDgRsW2lkMjTSr3TtQtxe666mhTxFPZHd5H dFe/PfeTGqNXT7Ot/VFmJA0dXP8YfNFRZt6d5/rQy8dJNp22U4YvnNDHoultcdG4M7bu e4YnxS6WgW67iW/ZPHqQ13648Pr7YCGmsD9MoS92ktQqhr1DW2zEnoy7s8gW8iMo3x9p oMexoJWfRIBS+Ae6TFtaj5lAmDI4Md9j5xLuuOcQbm9SDKJwpmAhVfnjZ6LLYcr6h7g5 RC/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712187724; x=1712792524; 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=pOAw1XJYz/p19vgdWe/CaaCARcUKwTEKGsMmLUN5vWc+vUYRjJJxsWJAh9R7P8wdgf Jj0tSw3CcQ0jmfOYwkNGiRajALiC8HJxJDMOaAAEgy/XIkC5bLWQhyZwohJ4FWFR/UYY HDm5V3M0zVGZSGlx7rZN1plcodyIMPq5V3puCHQnrrmPIe7zpSuKyD/hSwJCIvtsobAP wQ+UqAgmXtQSpzcaFOhWRiuytaUdJVfK8xM1/ZzcV7xK4XrucgraqOEvDEXiZ8FzwoXm BEgnOMJj79qgaAaxrZlB6wKEcvj2FJ7vemEkcakPAbf+W8utuDui9cOuRrJERpo8sFCZ KSAA== X-Forwarded-Encrypted: i=1; AJvYcCWv1IdyAJT+ghWIIn4NuO2MGBvUKHhO871ohtpAlb2NhM9GdeUOpmwTfj9EV36pIk5xGjVEJvTB2tMeWgT4pQrkFBo= X-Gm-Message-State: AOJu0YwaHrzNxwEnZYx3kUJExRxyfPIAVsNIdfibZoNAbj13XJVqZt5e GpLDvBDNC2s9irZ521miSU9P9XImjU9rHpkHt+5BmRgNCmutTJe4NFolM9kogV4= X-Google-Smtp-Source: AGHT+IE5OtNNwwi7V8fcgm4SldB7youBAC6PguykSuN4bSQ+2Jz71QrTBvPX9ID197EWuQqic+Bd6Q== X-Received: by 2002:a17:903:2448:b0:1e2:62c9:6ac7 with SMTP id l8-20020a170903244800b001e262c96ac7mr772428pls.41.1712187724491; Wed, 03 Apr 2024 16:42:04 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id b18-20020a170902d51200b001deeac592absm13899117plg.180.2024.04.03.16.42.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 16:42: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, conor@kernel.org Cc: 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, corbet@lwn.net, 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 v3 13/29] riscv mmu: write protect and shadow stack Date: Wed, 3 Apr 2024 16:35:01 -0700 Message-ID: <20240403234054.2020347-14-debug@rivosinc.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240403234054.2020347-1-debug@rivosinc.com> References: <20240403234054.2020347-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 9A54514000A X-Stat-Signature: fkgufoe76d637aqf5x1zd1dar47o9ngo X-Rspam-User: X-HE-Tag: 1712187725-660321 X-HE-Meta: U2FsdGVkX1+Ul2qZkMa1TySQTLooe7U10RZGqC4rBZKVCbu0+68Iy464E985+9Etp4pDknnpQ8ju4RZRqyPJ3csD2Sc44GJxcJyYPdLRsPs31JMYN9boax19VkZQU5cHe0qECNTRNOfGCG2TFRR8jZFfC6tRlAPqoR6VaE5GBRzLpzHkYVDYWbKWvCF+ACthk4DZbtDlAkHCzoGRsNb0nOtprHCh2HE7NTm0zDf5zQTg4HuvpBVEVAahAlKjlaVXOg7qHIzuvm2OFd1ccCkNT1oqt+l/GN04p72VaETL7GYlCVE4sVX1lPqFCisCz97EfD9ApXGzkCQ1VTgdlCKbbmtYssCFLnV6Z2KSPovp2/lULHGO8Y9ggzoQ9TcGZk9Z0bWfy+PjhfzVn4ri43P0CV2E5cmZeB0Cx3nru0gfxmDqz+Bu8YSEn3vu+mjjzgykvbK4eCHF+bsC846RhTd2/yLdwoJCEq3efFfJaTZFCKhIiY4k5uvsPULp650diKH78OZOASR6fXsPfZffwCKbRu4AJFEdyhDkQzuYpbBZLDwAyoHd5YVb8MrKDF3mI3GcoDPCwpvnq0BnwtL0I85bkJcSBs0URZHowPQw4mvGEd2zZKwfI5yhdDjitZLNHgR1B1KeK4e9TFiwhQEdEyztvOJXtV5i8cpoHQ9Uxk62MYBEuXsvAkI5ZqnGXjWc044noShipnQXv9AHUew91quiu9HtvDn1BCIDKMtGY+WOtMdwQr6nHn8Otd6RBS5xgUVCtmT1Oq3mI2GxzcxOxNo/URBtzpvTeC8FOkG0+EVxO6/vzjirRsx34TkM/HXSMwXb5G/+EhzunGc3iATyWSZ19yAf5IZTQno/34mzM+Pt0EvxZzdANw8bWHoN6p/i5oxUkXD9Fju45vAEQ4V7HDWysgQZwrAtLyUpf1LZNYr0YWjZ2GNKu2PKxXoF9HS0lhkqd0DDlgGUQBTLf00qJ1b VoqPwymh 2gkA1MnYRn021yx8LUiT8t0pJqY+OjA6HN1p+G5COGaxMh+2uUTgkFd0mGTSAc4lGiUE7bR0dscKej8bMdMs6fupn571UylCYj+j9Ah1bQ2WL6XVnSp6Ibp4wIxQR5lFCTO7sePw+eMHp9bOvZGiHLpzJSLYYblg5w8TAjMUPhyI6/1owTNYvSJQx2D74wDV2EF91ts3C7YvY5moS9T2TbTh/WdbtAK3nAezd1eD70Mt1bkmU64WOztQLPoj7B+0crs3GDGyV5iI7DvDjHuMO3oNM1mP4AAJ7QPMOidSyYlZQ5hn8eNi31Tw1ntovi5Z6k0N1MylWWbgnbJYzM2BnpNyz4LvXUOpYJPMH5hWYIyBgK5snKCJu55PprXbkDrcvr9nIE1I9kj0Z7KYG+kpFRvlki1QvZ2fE6tVs0TlPgWOR4wwWHiOCeUm6kQPQjA1T9rxUzZiHLAUX5DtL3+szUN/itmq8rEfbynnf1uklOyObvwbP+j8K+LCcaBgSgyooeSvvCoELNbkJKIU= 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 Reviewed-by: Alexandre Ghiti --- arch/riscv/include/asm/pgtable.h | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h index 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 Wed Apr 3 23:35:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13616778 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 1E05FCD1292 for ; Wed, 3 Apr 2024 23:42:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8E6476B00B2; Wed, 3 Apr 2024 19:42:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8966E6B00B3; Wed, 3 Apr 2024 19:42:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6C44A6B00B4; Wed, 3 Apr 2024 19:42:10 -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 4B9936B00B2 for ; Wed, 3 Apr 2024 19:42:10 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 1B0281C0015 for ; Wed, 3 Apr 2024 23:42:10 +0000 (UTC) X-FDA: 81969846420.06.A7F0707 Received: from mail-pg1-f174.google.com (mail-pg1-f174.google.com [209.85.215.174]) by imf21.hostedemail.com (Postfix) with ESMTP id 3C74E1C000E for ; Wed, 3 Apr 2024 23:42:08 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=0Aaljs3I; spf=pass (imf21.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.174 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1712187728; 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=nZzDaIjjjD8VhJdhh5IUepl1HTzhfTHUUaj9lSQCyR8=; b=z/Nm0qGCl0xUdsbfkcbTRAFJyE6qwd3XbDBlbcftXK7FxVBWN+SwJKN5xPv7ZwySJw1EUg KUR2X+5KqShzsaaO/JQF+3B0Uw40T/ArHycRkd+A56MSEpVP9ejlXiBT9nNwiVACek0qL+ EQCEZB7Eiala6humdVWLV2axfjBpKwA= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712187728; a=rsa-sha256; cv=none; b=2IKvU3y7GR1BrKEPfTPERP+I+XY/PF5ahO7e/yJU5r1PxlYW+NWNDhZO7wYCy3w3ucgDn0 LC8B/J+fdZ4bFeLaKLXWfZTnMkE8bshVBXjd32Q1wXZU9Y1VRH4saJIphVBjJn3ukTdp8F /i8BW4AqCF3XoElgf3XHWU0k+JpLCfY= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=0Aaljs3I; spf=pass (imf21.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.174 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pg1-f174.google.com with SMTP id 41be03b00d2f7-5c229dabbb6so347209a12.0 for ; Wed, 03 Apr 2024 16:42:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1712187727; x=1712792527; 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=nZzDaIjjjD8VhJdhh5IUepl1HTzhfTHUUaj9lSQCyR8=; b=0Aaljs3IAZbjPJZjhE/nPSgibw4CUxU36MlJt+r+/tjSnErKWhDX8j7j+5IzMuBqmt wwxVXr/u1S+fPaAKLxugVGyEtKnjK6FsvC+hJvOqBHoIuH9uBQ8cWittoeVMQFigIeEX Hz9r6cMUajVPeywvQdlJqFVqU6PTopIKCSg8ZIkzd/X6vBrwleM5Sp89anApoSGqypV8 SdPj8k0+LOcvU8dzPa9zZX3hi7kMLEtNc5yazibHs3O0MntqWDbb+rBE0ndcEIZX5C6B p7tGTi3MyAN/iKZkXSw7c91atHw5ebSRwTHVUg3/Gr6R+EBuajwGvfE4LKb/tclBdoPZ 8v+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712187727; x=1712792527; 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=nZzDaIjjjD8VhJdhh5IUepl1HTzhfTHUUaj9lSQCyR8=; b=dQ92KyvjnZfVyyFOAHth9ULQyjLJK2EEOxppPpmFxJxiTrCdQZKPfOlYbKKEylehu9 C/iG2A8apcwoxah5MVzYuqZqbLphOVGx0Mr1s0+gkWF2J3J2o7NcKuvt3vHCW5MNnPF+ YYpnrrHMK6j8XX4CN4VUwPbIjybmpxy7ASV8rs0I9SibsWjCmDGAIlHt3vuIzRx2ovWs z6NFXDj98qj+bbdbNV3CdxCL1IXf4ghG/65acdEIDwynh41ZzZvOrm3qHZlAosfaWDYv c/XujX0TGlXJWa4jE8kv4b1rBXWalLu9ENBpsLKdOvuD68WPPK6/PQA2RssOoFxVbG+5 QB2w== X-Forwarded-Encrypted: i=1; AJvYcCW0vnGNXSuqGYETcG1e2gegguYE71STE3MhtT6FDRswLRb2ICB4Ec64ui1iAzCn87QWIPYI/je7mx6pg5gaoqICGfw= X-Gm-Message-State: AOJu0YwoZhmWtHAWBf7t5JGIm/HpfVssv9jZii6eVMZZr1EU1weDIBl3 omhSkEOgEjp96fZdloycbVMNRqA/Zabsg7FsxEUNUKaS2mHKq76KH42KIuHrd8E= X-Google-Smtp-Source: AGHT+IGK408qRBDCWMc9cgA3P53YsEUkHm94AsTnnAuBMG78k0asshcIWXaS1TByR/Xziq/68i5yvA== X-Received: by 2002:a05:6300:8086:b0:1a3:e4fe:f6f1 with SMTP id ap6-20020a056300808600b001a3e4fef6f1mr1126332pzc.58.1712187727071; Wed, 03 Apr 2024 16:42:07 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id b18-20020a170902d51200b001deeac592absm13899117plg.180.2024.04.03.16.42.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 16:42:06 -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, conor@kernel.org Cc: 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, corbet@lwn.net, 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 v3 14/29] riscv/mm: Implement map_shadow_stack() syscall Date: Wed, 3 Apr 2024 16:35:02 -0700 Message-ID: <20240403234054.2020347-15-debug@rivosinc.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240403234054.2020347-1-debug@rivosinc.com> References: <20240403234054.2020347-1-debug@rivosinc.com> MIME-Version: 1.0 X-Stat-Signature: jzjys47f99bieaak98ju4x8fwtdehzu6 X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 3C74E1C000E X-Rspam-User: X-HE-Tag: 1712187728-706301 X-HE-Meta: U2FsdGVkX1+XmdHwbAAe1VPgVYLFTMGD9jI5iXq3JvApzS4ciJZwLtsxMbIL+/W6hrLMLnv+tuzUyAsIupxDb4Gw0vnzNv7hwY0ViRuVQbud7OXwv862tesZwCiIBWy9+6ncIvOl+hrgBm+RvAyhZOvpohhgVhhW1nVZeWAHYXHrcRek31u73IcEVfi2Fjp5wlFKIHem2jmc4e9ZbcrJf+0E02S2/gbJ6vFf6ABdFsSZ2g7NmgCFFKMWVoyTLXJh8dvR4jLxqEHleL6/XWjGrPW9gdHrScgXXNHn1j6DJFWYzowTauXXoVWKr3+dbBb2+EH2KLAWkGq/IKxxnH9mW4T9uCpDZG/koQgHtne1EnBVn7JMlBo5heVoUyrTtgUCyPcM4lSyIton04/NF8zb6nFb/NB79R/jwGv+y1k0q1cWs0msuRx6iU3SfG8cWA0m4+75YYdHU9WxQgVbfPAOxhPeKkaIMWHSY1TRHaF9ojqucFlN7mbG0g6+7NkelygtwAt50NCpDrc6q0d9Iu5mcoeUVPBoXznq1SamdG4Qhm2fP0M9xfKz2DTxAet23b+lUpUGTOc6pwQMUnCTB76Esvj6u6puutyFe+qumzbQFVoJuBYlNoQh5IrQtAK+qeVWsRfc4zVb1mshKUxtnaW2gEx/6PWCUk+pXGVeq9R/8jkAqI3ppOyPocPoRcPWMGS6YXXFu+lLe3g0n5KSKbaxG+RtvBSs182rrtt5Fl9YlOAwir2jK8IbeIKpF7AFwtkRBGYLFuY/p93+SazGGEym/kCKuuvDVKoXe1CLv3jDwpkrFGaS7Yrvs5clJtGFNhHehsorMQiY/QMNSMyW1FCARY9oC3psXvBFV3EU8KzyHQxLmEXFhmZHES17ni1fHDXSA+xaPN3bFn/GTaEfIpe1t4a928yckrU2EZOIJBNJEWTpTOz+fEbI+RY8F7x1+h8j2DrA9KTo4+WO0oZpP++ 6o6C+EFR T6h8nQDvwB54psmmH1MjO84MSVnjeBDW2tR5CSDNueJt5McSbwBkxwcT6bGmVfCSEOCyfxOB8ufA7q2S/tqNMNyzWi7TF1ENRF3AFmkCvgSmh1JMjT9Bqlt+dCHfwRD8aaqrWvpzr3d5c+DL5GvpbS10ZCY0d2uD1W8kNub8F/KffBdDDG7RgK66TGFyoZLVXubfK7qO0xX/P0Ro29vagNwRZXw6IbiOlXWA66u5OTFMNz4bDN7/dbt0VJCzYmu3R2n85JFmUHCm8tKRDFrcajmQDgwl3cnUjgkF1R+r35Y7rUFNrgZMcjk+qF4OnfZ18EHwUmwa/dn8natNhjJVP2louA/apGcRr/gjsiwu8IkOhXY/S+5VNfRUKjDP+CuT5j2BDy19PAj1cd+Bl/tX/GhKJQlBdR93jAQsL6l7TCLX9d6MjKVg82RG+g612AqY0h/bCvcflsBBDo63A6RH4XdJLubZC//nJdZcvOiNZNSv0Ey0pGE0Us6p0HeV+PQKf4/4BusMt7XjDLXbkbQidGnXm3iuFZ1I0YuNzlfxGqzQBMGuU+rVMPh76yv4ky2biyGY6 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: As discussed extensively in the changelog for the addition of this syscall on x86 ("x86/shstk: Introduce map_shadow_stack syscall") the existing mmap() and madvise() syscalls do not map entirely well onto the security requirements for shadow stack memory since they lead to windows where memory is allocated but not yet protected or stacks which are not properly and safely initialised. Instead a new syscall map_shadow_stack() has been defined which allocates and initialises a shadow stack page. This patch implements this syscall for riscv. riscv doesn't require token to be setup by kernel because user mode can do that by itself. However to provide compatibility and portability with other architectues, user mode can specify token set flag. Signed-off-by: Deepak Gupta --- arch/riscv/kernel/Makefile | 2 + arch/riscv/kernel/usercfi.c | 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 Wed Apr 3 23:35:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13616779 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 5B84BCD128A for ; Wed, 3 Apr 2024 23:42:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4EEE96B00B3; Wed, 3 Apr 2024 19:42:13 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 49E3D6B00B4; Wed, 3 Apr 2024 19:42:13 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2A4086B00B5; Wed, 3 Apr 2024 19:42:13 -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 007F66B00B3 for ; Wed, 3 Apr 2024 19:42:12 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id C0A16A0129 for ; Wed, 3 Apr 2024 23:42:12 +0000 (UTC) X-FDA: 81969846504.05.A084C8A Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) by imf08.hostedemail.com (Postfix) with ESMTP id E3C21160014 for ; Wed, 3 Apr 2024 23:42:10 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=SM+aPwgv; spf=pass (imf08.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.174 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712187731; a=rsa-sha256; cv=none; b=Oh0MkW709pKbeLwqly/gLhvLNpQZgXIN3h7sMmbwVG1zVIYZiE4f5CilXEJIiO7BaDR+sE 6nnQNHN0KhVGHyu3p60n4pGyrsZljnb9NY4tCgfc2BEAZXW13I4FyepvP84qvwAymKXByV dd8r2wEA0jPsopu1XWFwMikGiMfMOq4= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=SM+aPwgv; spf=pass (imf08.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.174 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1712187731; 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=psj+OseZc+8KPm/HqbdNZUd3eG1YiWsTUgUhoWSY+VA=; b=smGjPnave/u7YvqWOxti4TUU1hN2j5SEPTd1LdlzUXAkrc/QC3LUXYjHcZDXgZiSwhUB86 CiPYzy/So45omKgzENIGzPvHJbCy4OD5PQT9gEmSLbTFne83Ok1ip3cqMUkM7Df6j/uA9e I8m9LhBhLcmNyAIYldaa5KLXe7WKi8Q= Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-1e223025ccbso2054995ad.3 for ; Wed, 03 Apr 2024 16:42:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1712187730; x=1712792530; 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=psj+OseZc+8KPm/HqbdNZUd3eG1YiWsTUgUhoWSY+VA=; b=SM+aPwgv0Q+xNHwZDV1yu5hAAphSn9fUcFJzA7fGBN6AfsPBD3w9zH2TYZxCzoEz3W j1REpryIw4x6J6+kWDoN6lHPErMJVL4DiyUs1yS6e8RPvmkGRSUCiXLXaEEcm0jsL2ki vNDK0S0Y5Rovt6pbtQaRJL/49rOd3sAShMLNBcfkj6ALkGh5+SyXrNEmjRzii8RYcSM6 eWS+3ct19CD6zoq9bCwUFOHJANuSIf9lryQVC8U+t4vb1qzxcsMP9hBahzBqwHGF34fW Tha4MRmoZ0Hub75GprRqXz4VtIUgsMhstCzPlBHcUiXPnKGh7+6vVlldy14iUiJ7gFWx xJTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712187730; x=1712792530; 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=psj+OseZc+8KPm/HqbdNZUd3eG1YiWsTUgUhoWSY+VA=; b=tC42U4Ih61n2UgaPWfbDHkacw4FiT1f4HpgLLTqSdI0o2joRWF+AZxTx5ixDpG7nHL e3og1MGWTbMoriqzPN4Am3aWaZ/GBqSgylb9QwMwlslgSQc14sdQhRGW59U2RC5t/hYW MWbYOYnk6w1MvhdGA8Re7IIkM+fCIjmnBk1O9/FLi5O+k97XdLqo+kjosJ54yg8UYDIt jGMpAfSZPjBtdRx2wzbBlJ7n+nz3tD4xezREJ5qOd5k1k7Z9VEY7Jiue6pGjMsQ7Ypaz zDEXQjPZ5EqI4M59qOkmQNHoOx12MP6uq8p0E9NfGzHmnSZfhV/M8vv7Z/WFHB+iJ3LA VdqA== X-Forwarded-Encrypted: i=1; AJvYcCVYVNmVUauKpdSzsGecKzWO0jlG1wr2tLFiJxESsDS0IMYuBk8+xA6jJB5g5EpREXyfwyJKuKUsz1LPqbJTp7m0EKI= X-Gm-Message-State: AOJu0Yzx350CYExPisoj/R56GXw4VbFrQxQG0WURUYW1BYhCLuTNqqAR RHrFLS1xz3YpJ22b64Gca8xpzF+dcBptZU7fYWv18hJjTk91WHvEMMzV0WnZWxc= X-Google-Smtp-Source: AGHT+IFOeWXopoIAOl/E2pm7Fe9NTtywf9IyxEpXwVfgoOIZpkKANuKFSCozkyD2lYgORvDFgKEZ4Q== X-Received: by 2002:a17:902:c407:b0:1e0:f5b7:d6e8 with SMTP id k7-20020a170902c40700b001e0f5b7d6e8mr768005plk.61.1712187729642; Wed, 03 Apr 2024 16:42:09 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id b18-20020a170902d51200b001deeac592absm13899117plg.180.2024.04.03.16.42.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 16:42: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, conor@kernel.org Cc: 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, corbet@lwn.net, 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 v3 15/29] riscv/shstk: If needed allocate a new shadow stack on clone Date: Wed, 3 Apr 2024 16:35:03 -0700 Message-ID: <20240403234054.2020347-16-debug@rivosinc.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240403234054.2020347-1-debug@rivosinc.com> References: <20240403234054.2020347-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: E3C21160014 X-Stat-Signature: urtpkxot34mubp7n4finbi4nqxanf3ph X-Rspam-User: X-HE-Tag: 1712187730-817542 X-HE-Meta: U2FsdGVkX1/Yf2egwHbK0Zf0o3WLJMFVXTb7l3SKHSmje5wYhoJGnsm6sRNi2WYPpYOGP9KZaaFKpuFLSOGnTYjfi7NqIA/kPoe4RvFJUfQONdzWKdmBTMU3ir2mZXmI8CxiW2qWamvXpFF+8f62Uyt37viwAHQeYYkCKwtTl6jVJJykJrSPkKY105YlG+EXO/V9AMi656qXfkqE2xQ4g9iDOYuqCu88x+9Zwhe5383yl2VfQHfWfE4H3otqVKIYjW3247LVpLftMIYw0WL0sOCgw8OOolZlmvaoz28ihAf/7M6u3mv463Ei4DeghvhxWdgJdNpENnjxOWF8ZF80UDMDjZ9P3Tu9NYrYRk+wUoO/Bezp1ZdNZiZ2kHIdbwxnvkeh0qLkoi3Ekf/mTTF093Ts7vNPSqFiOhkNoHS3/uB7NyR3aNAIhgeoajsmvdeZODnBgyaFyONu667TTHji9JTGX2jMzhbm6Qnvkn+y+1PHRCWQkRpr3zrHrOq+vPnCRtVhd1jd28i+A70lyx56a3vF6/tpHnGNRgKk4OXyKWsXltUe3IMbNGfNOU0WeI61K5IHHmwAR+h0G6bJsvBqE43+WvRE2QXVli4+kOZlH2zD5HM3IZ13eT72r3xQ5mhDk6MGqZe/GhLIzXRcOQe9XEitzRTDFWfbm+f8k9kiPlOANep0lh4bDugK6MdeYWI0gTN9oBkW64NsYR4m0ki/4s5rya9yP+ng9D6AwYuXfDtCUITOG19vWHzBK2RmTQ3jEwDTOXvWFiZ2q6uEtf/QQ8I5uoFgwZY4Fn1yoL7c0tNUeAJt/OsyFOvrZZqe6aZYa3dmqKRk3xtfzoQTGWcqhjqGDygTpKDmywUFz6nJ+KdkffIJ0XNn2aGGtCE73XH4LiqCF3PxUZQVwIWFQSa64QOaMhptl/ogX1aO/m3b9NyJD46MxxVP8P2UC1gNRefiJSRjLTiMRHcXS85ilz0 nIY7iE7G QvUFpTqzjeNjwtm6j7zeRXXwJ16bXWWvEEUakSr4nOsft/vlHd7fNW7yCwzZfPzMLGM0ZpFTYbPvl/AzCvln+Y/CF2OUW8MNHgr/lTO0kZY8xIejwh6mzteE/7TqAyS0e+Ed5mfKfRX9mgcbztppVYf3EGbaXEDmsFLqDWUgU9CfOtSdxyh553+1tVP6TV7LTCamiLSF9BU+s3WBwntEdliuw4pnbOq9OV2MKAlFYtAvn46VUldeSSGwnOU47ATR470pGWLOxkYBdVw6cgY/SgH8Z8ekfJ98n9Piht2GDonVbqFrdjA878K1ZaKe18ZlcCCuN1btuh+V94/oxCX5NC7RlOsPgw9oacaLbco9ksr9eYs106EXupZMhPw6+sJFA3ewkTN2eHXacbmTvb5BX+08T5lcMcVk0hr6BJBeoSHgcl3iWTvTqSBF2kBxNi8MtNucQ+HBvGzcjVFrlARXwKUaoNZIyatdjeqZX9e25UBgN9VChLLR44KeTzSgaj0C+wWlDe7oIdIGLJmE= 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, 171 insertions(+), 1 deletion(-) 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 ce577cdc2af3..ef48a25b0eff 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"); @@ -202,7 +203,8 @@ 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); } int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) @@ -210,6 +212,7 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) unsigned long clone_flags = args->flags; unsigned long usp = args->stack; unsigned long tls = args->tls; + unsigned long ssp = 0; struct pt_regs *childregs = task_pt_regs(p); memset(&p->thread.s, 0, sizeof(p->thread.s)); @@ -225,11 +228,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 Wed Apr 3 23:35:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13616780 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 7645ACD1288 for ; Wed, 3 Apr 2024 23:42:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9D1456B00B5; Wed, 3 Apr 2024 19:42:15 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9818F6B00B6; Wed, 3 Apr 2024 19:42:15 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7ABBA6B00B7; Wed, 3 Apr 2024 19:42:15 -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 5B6CA6B00B5 for ; Wed, 3 Apr 2024 19:42:15 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 298AF120288 for ; Wed, 3 Apr 2024 23:42:15 +0000 (UTC) X-FDA: 81969846630.16.2E975AB Received: from mail-pg1-f169.google.com (mail-pg1-f169.google.com [209.85.215.169]) by imf15.hostedemail.com (Postfix) with ESMTP id 54A5CA000A for ; Wed, 3 Apr 2024 23:42:13 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=a0ab2hNs; dmarc=none; spf=pass (imf15.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.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=1712187733; 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=oZ8ix94mmk4BD8MMBywPtAuOuTf4nkdzJ2/QigtbDFI=; b=dckuRLZ9v/EFzqLSu5IzkDIX3gqj1e2RApSFTf3HToq7rgdOjaWGkcN8lgD1oM9w0owEzK XzlVeLCCqBgAWswCTaqZ6hN7/YqRg+8CRgerd31UArC4XutdiwuksLn5DOkIpI7Atr8nTa Ku5/4fP1GY0rpMsjrUPmsGP5fxO46wc= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=a0ab2hNs; dmarc=none; spf=pass (imf15.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.169 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712187733; a=rsa-sha256; cv=none; b=gAHjuXgjXvuZOAfURKPkMCdzDyddAxjRRrUFOVoiwk9yy42TBc9kYTLcXVyPvC6KdCGgsb T9ShN2j4ndSWh/Wk6jUbRjt/TFhDxlDCmstBvmhwoIqbv9xRLNqWlB9GXUBeF6RS3jeZKt GbhGQra+Jv3JujaNsBJLIv+hurRzl/Q= Received: by mail-pg1-f169.google.com with SMTP id 41be03b00d2f7-5d8b519e438so375434a12.1 for ; Wed, 03 Apr 2024 16:42:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1712187732; x=1712792532; 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=oZ8ix94mmk4BD8MMBywPtAuOuTf4nkdzJ2/QigtbDFI=; b=a0ab2hNsHYwQhDJXanuJOF19pp8TGUS69Y7cLaE2TC28qpC3Ubkrzk8Vif3WBMOOsK TJKHLxFYJTW5BYm88zq5oR/OQSEbpgqLMvUu0cpE7OEyKo+3uyMB8cuGQ+NPYvtRhOln NE7eLx/jML8f3RgFnQOo3zEw1kY88fS0m19UviXrE4jttWoDEJpQnsHbQ+99IqJ+OQl9 IjAutVWRSWeOTf3cHJDZ0wvDu1N7fz6tTgJFlWSdGvMZzbwcQox7u/EVzWD1ea2oE0Xa SrnM0KSu63pOAobYdUG/8D5U7rBR9dVQE2iF16Z+1Xb23IguqgGtRfGrMF0QAjsKXK2H fjvA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712187732; x=1712792532; 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=oZ8ix94mmk4BD8MMBywPtAuOuTf4nkdzJ2/QigtbDFI=; b=rk6obPk0PfewvyPF5tZTfarTlcgbV9KrZYF/+hBXPcnWUS7DNbYzcekUSYY82ipq60 xypYRyE7Itsk/VjgkdC4ufRSiXrmYZc0CmgLAHUZ9IlgyA2TDjLkdGufHAUB3o6BJ3VQ ZARjOly/13kTCzF4nKl5LvmGhmhMqUdF5lUc24kxnxHXZgC0NOewVvcsOBHc9eAXnjTU jQrNPg8J0fL6r7HETlkMCwJvIokG6s0WOS635l2U4dZ7PVwUHtqPQeVHFpXu5mHub5V9 5U3HaMb9Znc7YPZ+I7+xIoYn3e1Xqmnz5edRoS5Ll9YZ+x66PR1MA5Le6I117rgqy6k3 NVSQ== X-Forwarded-Encrypted: i=1; AJvYcCW6Dfe1eNh1Czm1oh4z/tFjDIWo2u0vv/aXX9pUUQNzGtX3A+CzraQppIsmK4bzqnNGex4LMQtJv+8GpBkZtDdGYqE= X-Gm-Message-State: AOJu0Yz6FkPWCI2ogNLdcOY4xtRrT60SpbTN9UDjxL3V9Hu/+XZumx3r mqRzcEY5x0M3DBuvJdowuuRADGMTTwqtDCG1cv+QsMaJMCfUX0o0IutYh+97avE= X-Google-Smtp-Source: AGHT+IGqMNLnQifKehVyTniVZ6at0ArgKKWUBm835f674K8o1H2lf66wElH3Yt8D0mQ5+pAZCM+6nA== X-Received: by 2002:a17:90a:134c:b0:299:3035:aede with SMTP id y12-20020a17090a134c00b002993035aedemr916968pjf.44.1712187732279; Wed, 03 Apr 2024 16:42:12 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id b18-20020a170902d51200b001deeac592absm13899117plg.180.2024.04.03.16.42.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 16:42:11 -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, conor@kernel.org Cc: 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, corbet@lwn.net, 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 v3 16/29] prctl: arch-agnostic prctl for shadow stack Date: Wed, 3 Apr 2024 16:35:04 -0700 Message-ID: <20240403234054.2020347-17-debug@rivosinc.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240403234054.2020347-1-debug@rivosinc.com> References: <20240403234054.2020347-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 54A5CA000A X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: wufzmwgxddbgpe5g3esis6ebm7icqg1e X-HE-Tag: 1712187733-531360 X-HE-Meta: U2FsdGVkX1+vUGzQ6ztiQH6GZw8jYw3C4cFKF6ETufmrDyLigtY3f0pRvNUCaC/uEa45gv+miF44nIUn4o5do390ohhb0AXy2rk8fYwPB8SSm3o/g1RQKzvYO4E+TCp7Q6WL40y90h7ZzaSHJS28XD9Pn6Qlsd7bk4Az0Ph6/4KRLSaaOvlCKmMsUQJ6UXM48qNPPb7ddp+hQkzTERWYlNG3rC+RzlP5HQMZTgNF41o4DjFdcsKqQ+FAww3ss8xdPvmzlEiebIIGTs9YXf24ADNm/85fzbZaWdtFyAaC4Ed0PFGzBoPmI3rog4AEGUIhal6Ek5ulvaqz4dhdNYbRRDlUcT32D4Aq4EbtAcw9p74fckBHUaYpAOzaO9TKM8QEqGe16Js8/6amgPgGg/niuDk86Y9JbJBJHOb65H1MfyVK8OEARWqmBbVOjawDl49K14+iiHkW79MyAF09L+jfQcQuCvLuvB/qsDEHWnaePYyi2shhuoEFGlEqDvQPcTCg6qe4d1slSjDrzScAxxULSRzXlS1FUUBCDcXS7as24g45WIphTYbuCdNWcI7SESh/ooeyXyQI8PrscuXosVXIluR8eKmJ5J95oarLWbgaUEZ9SA4dtKvLNw0SeTVG8kUzzmRSbtIA6I9fkiPTFhCpYSKQMLgQn/fWctSrbDl9/B/5nW2atJ88+ZHpXY1I03IwKiA7vZ3rVNpHCnL55u9iLqFgwtE4kps5XUF6WXYk/HCu/oh8hzb8mU3KUWfXCjfVA/S/OVBrex6oxK9lDwoS6AlaQDPbjFy04LL8LgZ3wNyOh0SCXJhl69D0dpuwxTfiySTiU22aRV7TNejRRt7TXTxX+8s45d6np708G0dpQpONVdxFh5O5BfbJWpJ8iU7csVn0QaIqTJRrrrnPD1PgL+DlQ92YhkQ6hjZcp6nZY6DwL9AAGj3mbZTYDBvMdAyuAEkX4kWqpZSL9AEe5rf o6ZF8eiI hfIv/skBy+EbpzGQbbtn2R5J3caSgCXk9wy5YGoHN0B+H1MzedzWDwPxj8m7kdGrxAadK3LHOnow7tfNk9QiOmUFI/ww7UOUKC1eGWuZr9vqi6hW5+46CI3g6U+Q5wwYaSAVcz90ChKrYSwHn8XEKjgwMaMf35/LOfOiJetxgzzgh7+GYzH8z56fDgsizmbP1HdVESAdeNonEIZUtdaKJKlABZsOXZvX/9RFV930D+4eDJ928+Kg0W3Spl8/HCNDsKld8CXxLxl3H52q7x5MXVSMnwzBfQjoZ4iCN+Iouufw99kcEzMeNR5sokUBhgeDLsrl9vUxwLujgag6/F7bJ/jyWRqQtIbFJvuUVvoqwv52KFeBiYC4aG1K/oGWg8EWChCmRbfLn9/8aeEGQJ5FySO61R71zvjO3pbHbs+mdS9RGTLcwwiw2xUzWy9ty/H3lNenSDQZMwB2OM1CCd4euv/EBaVrzfbse41I5kWdYmSbobnELVr+vmiYV7gRr23ppgt93wnFdQvo/TthH2UCydchEG3T0/vJ9tbriiEpsOYE+x/2pI8zL++hahja6jbQVKWm+KkiRf3DQ0FU= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Mark Brown Three architectures (x86, aarch64, riscv) have announced support for shadow stacks with fairly similar functionality. While x86 is using arch_prctl() to control the functionality neither arm64 nor riscv uses that interface so this patch adds arch-agnostic prctl() support to get and set status of shadow stacks and lock the current configuration to prevent further changes, with support for turning on and off individual subfeatures so applications can limit their exposure to features that they do not need. The features are: - PR_SHADOW_STACK_ENABLE: Tracking and enforcement of shadow stacks, including allocation of a shadow stack if one is not already allocated. - PR_SHADOW_STACK_WRITE: Writes to specific addresses in the shadow stack. - PR_SHADOW_STACK_PUSH: Push additional values onto the shadow stack. - PR_SHADOW_STACK_DISABLE: Allow to disable shadow stack. Note once locked, disable must fail. These features are expected to be inherited by new threads and cleared on exec(), unknown features should be rejected for enable but accepted for locking (in order to allow for future proofing). This is based on a patch originally written by Deepak Gupta but later modified by Mark Brown for arm's GCS patch series. Signed-off-by: Mark Brown Co-developed-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 9952937be659..1d08e1fd2f6a 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -4201,5 +4201,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 Wed Apr 3 23:35:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13616781 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 71239CD1292 for ; Wed, 3 Apr 2024 23:42:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8238A6B00B6; Wed, 3 Apr 2024 19:42:18 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7AE7E6B00B7; Wed, 3 Apr 2024 19:42:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5FEC96B00B9; Wed, 3 Apr 2024 19:42:18 -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 4300B6B00B6 for ; Wed, 3 Apr 2024 19:42:18 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id D4A4A1C0D69 for ; Wed, 3 Apr 2024 23:42:17 +0000 (UTC) X-FDA: 81969846714.19.0FB6BCD Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) by imf07.hostedemail.com (Postfix) with ESMTP id 0B40B40003 for ; Wed, 3 Apr 2024 23:42:15 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=WAA5MF7U; dmarc=none; spf=pass (imf07.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.174 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712187736; a=rsa-sha256; cv=none; b=O0hq7mLZUL6P29g1Kgp/JFCU/9L752hqTaGqyj5mBmJW8dUpUx5cYAqXWfnLWAAtW49Os0 ZjIF5xQf/MR++SMq/sPTxzCvzP9c/di0rTKE/5Er3IOucK/ENb92XePsz8xQY7ql8xlcpe zC0g07Zud97fe8MxwqzMzEMHkJZfPJ8= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=WAA5MF7U; dmarc=none; spf=pass (imf07.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.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=1712187736; 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=WP6SydUIEnIf7iJga0ecJJ8VROQQxV6mEOJWURQ0TH0=; b=CxVjhz4+RTd8b5fjnlM6tQ3/u2/CQOywDxk08cJOyuzwu53L/mtex/3TDvh74Cuhpx1T8B JFLvAijITA2o4EQH3qUPqRM8XOdK4PmulDTPJb0iI2+3oFvNVLRZ9G7MCKkeaVXaL8+1uC F3LcddLE9z1yn1WhA4OoKrOUaLgaZaI= Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-1e2987e9d67so2186925ad.1 for ; Wed, 03 Apr 2024 16:42:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1712187735; x=1712792535; 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=WP6SydUIEnIf7iJga0ecJJ8VROQQxV6mEOJWURQ0TH0=; b=WAA5MF7UOxQJhmXM/Zzc1dmK61kvakQuA7HRUPsObvVpOpnsH3Nqb6XssgTKPB+IIe oOB8HUuqzzZhxZlIxCzS8kY9Im3HaFEy+jSeeEEIak/lRve3uC6yiwymcfmkW4q1NP58 ACYLHOur0bv7ilXkC5/FkXeC0fAHmzghSX4mgzGa5OVtm/y7w68tiDbF1nhVBJKyTnlS xLmeMGpxj9/BO8azJybQFOyYy/xcuI2WEtMisOU2WhFU2onQiIAYuclDofjsyedIJ2Xm jooQDrmAikF5F1hWyGr3/QWQmjcVrN2vMqfZj/NlOB4f3d80kswG5bdovlC5rF8AGQzL B+Zg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712187735; x=1712792535; 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=WP6SydUIEnIf7iJga0ecJJ8VROQQxV6mEOJWURQ0TH0=; b=QELAMW6N0tSRSHRfsdtovxClZZfFL4egtaK9LHRkVldxWW27YRU3qhqTAOjNKZ48XQ tZchloXKiD2cqU+3dae5JFTtrJpYrUBXK0AVWonAl008JQdOBMQN6s8PzN0Q95LWRP4W twnsR5iK++VrhCV6y2TjsC8HWpBDWOWZ+uqAGouxRetbaAqMXjCK+wYAj7c2J/WSgj4w CZDWoFRbKVRCL55LdgnnF5ba/+E268bltno5pYCIumYhZ5q24S+suOq7MtiKZJmaoAco J0TNulqaaiw8apPXB2EDh2QFW5M471j0fyGNfIXK+CiC35CphLUtS01kCcK3CXwofYb3 xMNg== X-Forwarded-Encrypted: i=1; AJvYcCUiYTOSzSdOcFVOxuxHuE2w72dYajZsWjNuzbhIux7RyNGw+OyCgxIMzyh/d98bS6IEKcQbuhjEc2lFU0g92cf1OQo= X-Gm-Message-State: AOJu0Yygntn69KpPPqpDEq22KSA+uJrq9Rys4FVOF6Dg1GXmKtsmDy5e /8Fnr7BpmQ1p4Z6BHgEW2v0L8mYemoLJ1jI6Y8/eaFLOG4721hKsPbM6tsO1/io= X-Google-Smtp-Source: AGHT+IG94ZCsKxcUmluscIk5n5e6k4tHROtb3Z4W0JVAcaXjptzg9lzBPMJPJjbNEv/0oFOKMb0Hdg== X-Received: by 2002:a17:902:d4cb:b0:1e0:e14:b19d with SMTP id o11-20020a170902d4cb00b001e00e14b19dmr1403019plg.18.1712187734816; Wed, 03 Apr 2024 16:42:14 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id b18-20020a170902d51200b001deeac592absm13899117plg.180.2024.04.03.16.42.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 16:42:14 -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, conor@kernel.org Cc: 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, corbet@lwn.net, 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 v3 17/29] prctl: arch-agnostic prctl for indirect branch tracking Date: Wed, 3 Apr 2024 16:35:05 -0700 Message-ID: <20240403234054.2020347-18-debug@rivosinc.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240403234054.2020347-1-debug@rivosinc.com> References: <20240403234054.2020347-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 0B40B40003 X-Stat-Signature: orbqnuja6r7bd961ns36b9hpsihj1f5t X-HE-Tag: 1712187735-742986 X-HE-Meta: U2FsdGVkX1+/LcJB8/Jf3r/jkdNdDTMOuEfuI+Vs58VQ5TiPmX+jgVbLbfkH3LQ4u45YR2SOCnPQs03ZaErfyoha3heN8z1Pr4OrlJcbF78FzOpACJwEMra7qMhdD6OkQDpH2D+uah3L82j13BHrvN9FWMIIzqqpEFdwawV1NncKD76B1MBHLqoXvJljSqYV/kpM3tFSlEfIBltWIP4a5kzUei8O9zAQKp+wo97Jr4dhkOyF5kSu/vx30WZyp0mrxoqYhdooLUygXhIHyHPHXcqQpfbRH6t+DDL82M7Ca5m370qKIBxHGpwF1MQed6VTGzOfhSvZn1nvO3vlPoGE0WO9iVNvuZMyiaDQPQ2JaBOrJMOc7z0YPN6vmrlVbSgQfHi98N/1b8LTgaQC8WxQC36H/TELRSxX+XvsmbbeA9eGzm1RL0zP/mhivqBdbihXN0XGj+bSz6Lvn3LaIZaHaBYwx+BiwkOyJuSJ8V8bgivUpUC4YFzShpNr44nG+8kJfpQcU/OKc3rGfTByn0Qsxy01xzeSo0pcISCOTz0yT0teXnN7X+/1hQUdh/hOtBKU5BD3qOTasZffP+n2N9dCxJbRXe0NGMnUlx1jrwGARqmMFq9xPTLWf2SCWFQl67v7RgAKa060bmViHeDkqUd79AyrX5AimIKJvrPl6jyIIBdGlX8ukPHUqpoU2F7VkU7fJVeZsn1cR/C9J0v2TGLfJayk5IoZ4kpvo+JByA8LBIoi+OcXytGXp9pP0RI5S+TgIK+Ow+fj2Q+cgu835buPa+sy/xY6HCMGZiQ1mjYrABaC63/rmghhKJBoFBE1V4Ej2KoKwoFgcHyR/icmxl6kRCQ2s+dS17gDFOWciFH29gSnv+HwJ63lIVA1alkfZFboxKGKHVvI0aLf10YUe7eiY7yl0m7Ab5QmQ7Lu4t62AvaHD2EwL7I6hqw1MmKAjhDyaGRu+iOIzne9DHYSg0L PWPZZI5S CZAviaHxNU5+qvJExp8pN9GcNOceHDyCwH3NGTAp5zfl3yYa3Mhppq/K8UgtkZuDrP6Wgf8/3jzp3s7aK/xksbWQGM+oJieZ+k9nrlahnQ/7SuVCHBXmbMTwXWdLZrBrNmzRRAWp/NOPOLC7fcF4/ZvXvi254RCseepJbyXJOK7DJc5LfAZ304oj//2vK/OTGjStMNbYMiqcWmvP9FD7OvBABsFSiojbiPqFdHGJoCEVpLoPwWBRJa3bildC4sBFoVe6w0Fekpz1Qb1k01s0y5ZgXv9LxRsDjGMTl6B+4bboMYmoWyevQHe39gR+lul9OgX6l5ZsDXSqzHpNMiUnqatuCHf+wMF/cmt3qZVk5mPUo+OWx9X5TqSIWPvkKEFfyT7K2Gef51KaJyhG6iNbE9alBuYgabPZJnHe1Cggy3pxP+YJOb86HHEQ/ymklzZ+/Jdfuq3SdB2QoISP2cCCfMpYAeKNJIAN9/msJsPLfohX6Mtdi6azwTpCavqCNGrSpB8jqxWjCPrlMspE= 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 Wed Apr 3 23:35:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13616813 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 1E15ECD128A for ; Wed, 3 Apr 2024 23:42:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A94BA6B00BC; Wed, 3 Apr 2024 19:42:29 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A455A6B00BD; Wed, 3 Apr 2024 19:42:29 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8700D6B00BE; Wed, 3 Apr 2024 19:42:29 -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 674626B00BC for ; Wed, 3 Apr 2024 19:42:29 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 2E41D1205DF for ; Wed, 3 Apr 2024 23:42:29 +0000 (UTC) X-FDA: 81969847218.08.26676AB Received: from mail-pl1-f178.google.com (mail-pl1-f178.google.com [209.85.214.178]) by imf11.hostedemail.com (Postfix) with ESMTP id 73C7B40008 for ; Wed, 3 Apr 2024 23:42:27 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=jS6zYaWX; spf=pass (imf11.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.178 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1712187747; 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=LYjXbKsZWObysa039N5fpdCtqQMANxENOQ8uS4k07nc=; b=k2ZNYhN1EP8WR7cczwu4ATtLFKXCMlDeVTY0zxKyHBzaMr3ROph72yoUJadiCjgdZ2dmw+ VV3IbNCQsRDsu3BLIhDtCsAW6Kl5CV4bPmlYC21PDVXhbAgX/4DWmUQnBy1MMIF6IttTbk a0yn/WJDpgICKn4A2AEjEPda370kwzQ= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=jS6zYaWX; spf=pass (imf11.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.178 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712187747; a=rsa-sha256; cv=none; b=iWGTPaEnk5RTqjHdw2N+rAvPWGk1z5oAMUG2VPuB27Pq/64hjKgjLfYXYxB1rTxGUAAXOR 6fbbl8rzZtEDFJb3h9QHCHu6H+ae+SnQUxmLY8qMBXNjQyKVeQqBC/ZZ1apv6glv4H7/KR Szo3lRgsZfI7GKwI5LCDCwe3N7HceYo= Received: by mail-pl1-f178.google.com with SMTP id d9443c01a7336-1e0edd0340fso3359315ad.2 for ; Wed, 03 Apr 2024 16:42:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1712187746; x=1712792546; 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=LYjXbKsZWObysa039N5fpdCtqQMANxENOQ8uS4k07nc=; b=jS6zYaWXE15siouAJcxVFmFBpp6audQYImo+Wq8lLAhqQ6sG8PnAmMDGVt52ePAVYv VcvyKlfABmHP1iiVhWLoaJCPiAVSImEAQwZzX2/N/Rt4kXRWzRqdV+0cEvo+vqN4PWkj NB+eT67sbcYhFa9GYICYbLpuyTWROUpIYWvMRf1x3cWuRzmar9Gg04WkqKIqEeC0ZmMn 1yJ7y/JFDvFm5YYejSR8n6omS7B3ikHl+G8pohf9WDDmI3HUvy2hXAuJ5es2jC6FxTCd Dpnm66pY3+2V31iNlPeFMhNADzj+jLM3zN56IVRxVEu4mIykUvKIHIw/KjYpcUjDyR5a gAjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712187746; x=1712792546; 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=LYjXbKsZWObysa039N5fpdCtqQMANxENOQ8uS4k07nc=; b=dobNRXKzoVV1t/xHcJVHB0K5cOOUUy9cV15iEZIOtkLMKPviVTpXgojTDXGBnGutLv xKvelbXxY92TscFff6FfFVpluFWyy3md7Wkts+myxkaic0hxGebdvx0wpXEONz80U/L3 6gEx7ra/+SQXrkybd0ZEvsoLJCACiMOPN+RaQz4ReHd6ZHDGrUT3z9VsL8HDD6uP5b+J yx5q/ec8RotuURNKg1Y+SBWIcWHfZfIp3MvR31X+baX2nRWWFf3PkUPVtpkQfYlBns97 gNEOrBsAHtuJvAxi0cBi+7qDyVE2awkDy9BJkfS2JRQJtKCnTAmIh258Cw0tUFAsOu4E mnMw== X-Forwarded-Encrypted: i=1; AJvYcCWZiMPHDisP1dyXV+d3ktatKF+2akD29nUJIc5CCjSUA/XlWTlqHlD4XGdk2EqaSXkPqjCoY5HP1X1h9pbGeuDOums= X-Gm-Message-State: AOJu0YzvRxeAoupcEcsqqP7qS/W8PFzt9w+X+edJ/qpo40NSELlpE998 yHD1wpr8ofOb3NOKwj87oieJmv1P2ykyKyEUvqQYDJglGnVvIASO3hxyHcRo29w= X-Google-Smtp-Source: AGHT+IGqtyBuB5j2we7P/N7XV7ZwjjMEdcimxTlF2S48pW5kJG8K4vLP5TYFGyEOra5BjXZZI2wVPA== X-Received: by 2002:a17:903:244d:b0:1e0:e85b:3389 with SMTP id l13-20020a170903244d00b001e0e85b3389mr1058260pls.3.1712187746310; Wed, 03 Apr 2024 16:42:26 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id b18-20020a170902d51200b001deeac592absm13899117plg.180.2024.04.03.16.42.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 16:42:17 -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, conor@kernel.org Cc: 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, corbet@lwn.net, 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 v3 18/29] riscv: Implements arch agnostic shadow stack prctls Date: Wed, 3 Apr 2024 16:35:06 -0700 Message-ID: <20240403234054.2020347-19-debug@rivosinc.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240403234054.2020347-1-debug@rivosinc.com> References: <20240403234054.2020347-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 73C7B40008 X-Rspam-User: X-Stat-Signature: xaufcckbj7zex8marnpkiguqoxdck64s X-Rspamd-Server: rspam01 X-HE-Tag: 1712187747-43584 X-HE-Meta: U2FsdGVkX19qfSjIyKr1PT9a9CHzVTbqzfarleimeYoYvK11m5OE1gIaXpb/br3/0iyiiLbclnXVFTl5ciQBA+1VmZbl7TLddtJ8zBA4mUX/2WA40cTpCtblbUqPFrPf7q96/Vtoa9FvKsZvD1Ixy8NPPwfn3CJptr7yy26r1TzvdHoAdeM6LaZgpEif32QIPENjRu+N0Ut8zmqk9kB7exc0edWnPsDEDoSPyylVwg0voVSB8xLU1Udr80bLDQwqw3T5zzFTI3znYdMK4rKohUOII/pSVhDluAGOefewxc6G7mdOyub58A7xdVcSHWDwOeAJRoGKUBoXrF83NqO/5S0aeHDt87+vn/uPHs3Ey9S3LRTqfgdVZDATddRP+ppQAv6tt8HOoOxmHnJtWCl/laLw+syw/lWTshfioootQaDmgsdWKnvGpOguebVRT1lZMDKRyfrN05iEfU+DtjNkZ6qvCHJq/Khs8fKANpTbf0TI1Sv7s6dwlepsZXeOn9CLJPbnGUUfURxBYpxSaWR+t8rsA9oX6Jn0fr/CXCR364RLeeoUvfbyoLSYDlMsvcrPd6iOW0HtGlH/56+o+3KB+EFpFgIw+QkpO4//H0tNVt3KuezFv/DOAobaU/LoFK6IlZmMfJ1dn2MdJgUV1HhFbxwsEp80stDCz+XZ9lsWOxrf6SlR2FJoUumwWDCYh9E6Ea4j5JJlSGQ01n07ICGN6tSOnXJ0OyGPVpfvMVGJHfEXNXqYxt2v20VaIag1Qo+9ALzKaFdiFzpoKqhrG0YR2kl4YMVtCkA+GZyvdmziz3mBelHDqBLkq3rmdJdHTXCSi2KR/UGv2I/4AxCD/5zit+gVppFhDHinFl94QKd1TKQaminTMDBBxiDevdBz0UcZsj40jUd331WmqooAEE3W4O3tTvOG2sQd3UXbPbIjd7zO8EySx2HNa/cG6+nuZjVHx/GKYlWsD/6baTd7crH 2CMJU2J9 BPSDhpZ7J/G21NSfNFeJx9yKkBgmIyFbAJQgL+U0sHzPTSEyYznZte/Tczvd6W1vUTSkScOIUcusBEe+5OomL7MKISaFq786WPUDKc9KEgJTtJ/DfVNqeCY/VS1Iu2rUzvxksxIUqHt4fORiMiNTjBy7pNxgyO/cf9TRYpvmSZ1B8+Tj1AFE3KtNA9TCR8oeuEhl5zwUuP9xc0nLZID9bXBffVnQBGdOJ8ddL4W02mEd78iA= 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 ef48a25b0eff..3fb8b23f629b 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 Wed Apr 3 23:35:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13616814 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 AD48FCD1292 for ; Wed, 3 Apr 2024 23:42:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 369696B00BD; Wed, 3 Apr 2024 19:42:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 31C126B00BE; Wed, 3 Apr 2024 19:42:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 11DEA6B00BF; Wed, 3 Apr 2024 19:42:32 -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 E52C56B00BD for ; Wed, 3 Apr 2024 19:42:31 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id B64E1A0F83 for ; Wed, 3 Apr 2024 23:42:31 +0000 (UTC) X-FDA: 81969847302.11.56EFC33 Received: from mail-pf1-f173.google.com (mail-pf1-f173.google.com [209.85.210.173]) by imf06.hostedemail.com (Postfix) with ESMTP id 15589180019 for ; Wed, 3 Apr 2024 23:42:29 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=azBBlXyH; dmarc=none; spf=pass (imf06.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.173 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1712187750; 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=nZuOqDvC0Z7orZ7kqn38837045KjWIfnwy63b75kPss=; b=4I2NHhqrYmRCysxb9PxL9oL9/uNEB2Czo1pTyD+/lZhxbMdxruovmQQi51Fag1BcosJ6+T n5BscUjfvp+X5kWHJLZkGJ0ba2hMOLwwLm8nrxKJuN7Shwyr5xmSmES6cNKv8JkHXeCRbt ecUJEwcDMwPS8cCmKfzDv0GiFh9t3JM= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=azBBlXyH; dmarc=none; spf=pass (imf06.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.173 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712187750; a=rsa-sha256; cv=none; b=HzJvF8JP+3DhkYMx4LE49Pz+amJbkIqN+4lRKLvnQ3HW9A6BgdrLusi8nBBMzQ4cqfcFJW 5Fr7QPkInF33t54sQW+DhnsRjxg6QZGro1NmBT8VziHSoR6zfJU7IDDwdC4cc7y+i7biEM gYDMaWDnK72JwW4LKOYTWR9efQAYxLA= Received: by mail-pf1-f173.google.com with SMTP id d2e1a72fcca58-6e6b6f86975so284452b3a.1 for ; Wed, 03 Apr 2024 16:42:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1712187749; x=1712792549; 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=nZuOqDvC0Z7orZ7kqn38837045KjWIfnwy63b75kPss=; b=azBBlXyHjpVERx3HyLoooSaOqvir6pFnMvsHcRwpuHe8Pm84+SzYYqwJyEywbncOGW +UC+73KLCG83aZ9GsYjBqSesgV9vZjcq8em1b1rgm2JthbDay5kzIiq5JbiDBIOfgvpZ jG4CVdHHiVlUZq4D6gH1NgjjIL0K6u7WRHwnqOV72KurGEtsPX34H4AkaRHVy0Edr91C Yip5wHn3kkdjUbk7QYrSFUaCddPKAEJzwZHAR4qUt26KZZXuRcmlSivg+vK4uPP2Jc0Z WzfYSm/Cy3qIzHWICN/feQ2ldqF1BQTR37JltNRyt7cPFYAZuoe25amDfO+ZuxkfnhBU 8w5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712187749; x=1712792549; 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=nZuOqDvC0Z7orZ7kqn38837045KjWIfnwy63b75kPss=; b=GxXej8IY9WkSmd3kjLFIhIwLf3rTBuzQ0CPycLWfL/wT8aqGg+gCDTdqefvy0H1j1B 8kMfgcZj22bCqA4bFbnErkVItlXRLfrf1FZ+LcjjUl18acqsrPlnE6Sw0GdGmXZhMK8e ScAEWLYruKa1Rrgk0ubpSXVxL5zYea3//33s9H+5upH8BBl2q8Ck33RdXHhExz1Nqv0G FW+NL5nIjYdS/VqKH3cTm2erb+mwB0pPo2bLi0RRqSuQzQXjQcBNruM/utec1hz7AbEb yPWA1FMkHpjac+lRHiHUb6gBbgRYQi1qPg5p9WDaHO3zt9izOYFndxVqvasnvijCJwTL 7q3w== X-Forwarded-Encrypted: i=1; AJvYcCVvX8KdDgtvKrNtGmVR6agvFRyzOzsgh1NFjS8KC3PP3HeGHGEZfdxURmlPSBz/kF4Ib4Wuwnt3KAcQlBX8m6EsrbA= X-Gm-Message-State: AOJu0Yy0rYSmLU2YDT1TX6UjL4GDNnUymOmEd8AXOwK0NbQkhsEP4EDN 3oeRpTUqIZjV7ZGR3sdRmGawoIU/irFAhrJcS2QofGNmNTAe3LtnajJosLGnhTs= X-Google-Smtp-Source: AGHT+IFlDR3O+4GEi6icblP42gxGgn6vi8m6/NIcpSF/j+yTm57ooaZOA9iB5WMfSYbFBKo9qpApOQ== X-Received: by 2002:a05:6a20:3953:b0:1a3:466d:d33 with SMTP id r19-20020a056a20395300b001a3466d0d33mr1558419pzg.9.1712187748866; Wed, 03 Apr 2024 16:42:28 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id b18-20020a170902d51200b001deeac592absm13899117plg.180.2024.04.03.16.42.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 16:42:28 -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, conor@kernel.org Cc: 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, corbet@lwn.net, 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 v3 19/29] riscv: Implements arch agnostic indirect branch tracking prctls Date: Wed, 3 Apr 2024 16:35:07 -0700 Message-ID: <20240403234054.2020347-20-debug@rivosinc.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240403234054.2020347-1-debug@rivosinc.com> References: <20240403234054.2020347-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: 6my6pibrmtkrgacsj5fger3oyysjd54k X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 15589180019 X-HE-Tag: 1712187749-645281 X-HE-Meta: U2FsdGVkX1/lTM/OOrnEo/LH1iD7NJAVv9s3EgO4n0f9PBn2kLEv+JHJBMP10dtDMxM+gTi5Sknk9XKsMaWor70NoN8r5HXScIUw9SDWs8b/rcE/btLRUeKo+rmyr3WDf71i/FW/y3RI6sLKVb39F5gJGmjUUkK0tLi9fZlfc+ziKigpiDuf/LoT8bpMG8/AV24mO4jKH/iLSCDTqDFiL84+tKcZz92EHBbgIAVitvW6D9wRe3YvnOAK2EmpaLE6o31Psx6v8VjqfvGH/xaTrezoQuUpe+QqCQI7/DSTGIeztcpzclZEpxtAiW2lBTEtaUkgqoyLRKjgjBUCpq/VWjkjPbb5D4KpdqshV2VUQTxqZEFFF1MYuvwDIpho62JyR+ZPdlVrV9HwZGEoNhfCmfTMAX6gpVX+6sQwrRxzRQp15Mq9enTtuJsiahVOhX+u/XziWJhNOeL3tq4G3Nsfb3BXrpccvhz3TsuJE6GrapqJj1UA54/JXRMrHf10RTO2RDgf8Ihh+KSFHK51LmffT0zQXWD5r+h9PbeC+RpJ8Rvwq1/DANL64JCVwdiCCxV4F2Xp0iaw5tneOoAc/t6jvE3k8MSbMY8WG6EnJMKXIEk3WHf2FZsxtT/GiRBf88fSsr8l5Da+3FZfTUvqzz3ypQ7Mvm5U2Ny7PNFk7nq4cUOnByW0+vVSbqwyRGjO+QaYpv9QX2CELPsSlJ3ndCwarrlI581utke5CZN7cTACKn59v654TbNQ9ME40TLK1kkv+feHFn3x/azfgQ73COnPjQlOee/ohqWGcn5hHxMnINkzuyNA8fQoNi4Ehn5VJpa1C7mfsacNFsPHU6r/idFWuV1QtmqAjR/DtJpersicZx0N+x6HKAhQAMDjmVb8hfxdLU00buOraL42BbcLMNkjYI8JbbMXkAL/YQPQiJy1vN7rbFOQZgtFwHid5Ndzrdd8tE6Otx6oU1Teo6voCKf r8DaTuRP X5MFvT6dJ3gGhEF8bWrlmE1hEZ3OqvHlNXp/dZmUQbG8boqkyU8JCrRl1RVi0gyIsIWeRCLhA6NNbQ2yyRCj5Gm2a32rOXXFWK4DbHilriG5y6mfbouZPpEbaznb3cR2QwLUDdMNDkbFovW88Z8bHRrRTWbi+KlmacYNhGtNbH5D9ajZ1cTxNDB3ntoLXBey0kdVCkZUc3QnxCSsPRuPTP25BgZ2Kxfa2mNizFoo4ZT+tCSg= 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 3fb8b23f629b..ebed7589c51a 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 Wed Apr 3 23:35:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13616815 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 86293CD1288 for ; Wed, 3 Apr 2024 23:42:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1493A6B00BE; Wed, 3 Apr 2024 19:42:35 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E3D9D6B00BF; Wed, 3 Apr 2024 19:42:34 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B7DB76B00C0; Wed, 3 Apr 2024 19:42:34 -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 94B666B00BE for ; Wed, 3 Apr 2024 19:42:34 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 6063E160392 for ; Wed, 3 Apr 2024 23:42:34 +0000 (UTC) X-FDA: 81969847428.25.FC17555 Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) by imf26.hostedemail.com (Postfix) with ESMTP id 89D0814001D for ; Wed, 3 Apr 2024 23:42:32 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=QvZ41arO; dmarc=none; spf=pass (imf26.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.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=1712187752; 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=MSiG1xhiflfyI9bPxIgqPsLfJsD+ZxoOiAqpJ0kRoxs=; b=wfaIxkKh8vwhn8aKk2R7hZ9KZmOcHzGfFQ370tvMjsxe3499izxijk0lHnmtc9l/UGqm1F aohTmQY8F86Il43PPckoGfyDhIwjV3Kn1/ZJ4obT1zMVUYxNVvB7H2U/0jsC2JMJTlemML aD+AkFw8SMPUc6L/g61VSFVrG2wS7P8= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=QvZ41arO; dmarc=none; spf=pass (imf26.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.177 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712187752; a=rsa-sha256; cv=none; b=F7w0ExS1vQ8PXQ5l0HPOqiaTXzD7tRAOMIssPLHZ6dbkJZFCy3MNpzcFXRTDTGpNSRc1mQ I7Dq1Psvokup1pMWRP59wMuryhAIRGdcmvhh8C44RONeE9L7thWRbfl2M8H7d2bHijHrSW 2oeiczpuVjurTETRmukGzQQP3PUBRGU= Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-1dff837d674so3269045ad.3 for ; Wed, 03 Apr 2024 16:42:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1712187751; x=1712792551; 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=MSiG1xhiflfyI9bPxIgqPsLfJsD+ZxoOiAqpJ0kRoxs=; b=QvZ41arOZHruCVBFqXINmkv42R6lyIqndrQJJYCITwsBLKlf7Su+vMfTi8301K8n9d CmJ498pkrlpJtg4PU8hN4iAbtj0e0J4MiZ9fF9DPVzC07mIjAH0fFDdOvBRy6aFg1UA0 ycN0ldWFVW8GlcVAOOuyAYopQey1tL3YbHvyfBbAKgUWS8IxK9szrNWiKh3KNJLKFE3A 3cY4cpZ0VbwItBE6hw+I+7RVr1zyX9eRiWytRSKvop0WA5aZeZlwYZdVior0vu4iNR7d U95+CrX41wmbN6h5kP45QeBo2LW6b0vmL7JvxbuUk3sGp+LbRgzETQmvBIMeYIWivqdl kT9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712187751; x=1712792551; 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=MSiG1xhiflfyI9bPxIgqPsLfJsD+ZxoOiAqpJ0kRoxs=; b=aaejaYzXiuNsD5dMfpBlSDS4kMzVttjtyaV1hBpUlGo9FwcuyYPm6vQthqkGaEQMJd Zup2HO5v0/MuK2vbZXlbz0fn5A9n0OZcOOJsCHdaTDWQRfo6Ptz3eaECU8IVeOhYEJRY njyNZTwQw+IUnMTPhBIsqDBBKhrHO5hakHyJRfzE107X3oOGg3u6nGX80MLBLpTmZXsh s9Am6z1CVJYPqCNNQh2Kdaaa04uR6kA+ArArIrp3fw66USws9XBdyWVovVnDa3uepkWn ulnBR0SSYdaiCOcDsoJAaPls+LXpYKqslzidd9DrSfrR4XX9+SD3SroE7kVpRudVzyuc ASAA== X-Forwarded-Encrypted: i=1; AJvYcCViicugUlqV57Sv2KMEaUK4fFsD3gPxNhX3g/1VrPJ2YE3Yi4NrTIf0HtNTcjiksjqKCXRvq8zrKJja3G/VKxe0q4c= X-Gm-Message-State: AOJu0YywNLZstvkCuRg1wVmrk4bkyg1e8xSHDV4d14kqwpVe99ZyPM2+ QaOyuHQhKbotthtR2YlFf4rWWq40dov1JfoyOJxZOD8Nd5QnBpeEX7j3GugTW0s= X-Google-Smtp-Source: AGHT+IFMW5tbyyAjWJEE1nX9dbuRREm7VgmFXgOohN5Mrb4B/RniQlSV9gFzsZCwXmsG8xWhwnLhWw== X-Received: by 2002:a17:903:487:b0:1df:f6ce:c9b3 with SMTP id jj7-20020a170903048700b001dff6cec9b3mr824638plb.43.1712187751469; Wed, 03 Apr 2024 16:42:31 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id b18-20020a170902d51200b001deeac592absm13899117plg.180.2024.04.03.16.42.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 16:42:31 -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, conor@kernel.org Cc: 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, corbet@lwn.net, 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 v3 20/29] riscv/kernel: update __show_regs to print shadow stack register Date: Wed, 3 Apr 2024 16:35:08 -0700 Message-ID: <20240403234054.2020347-21-debug@rivosinc.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240403234054.2020347-1-debug@rivosinc.com> References: <20240403234054.2020347-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 89D0814001D X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: o3hmhjdwaaifio69nask4hmx55wqfmhf X-HE-Tag: 1712187752-550708 X-HE-Meta: U2FsdGVkX19xItkzv2qHKtdxdvZP+PbdTa93Fixu3zAbCOyZ1BjOhQwjXD9ibNpQisAOsRaDqDPJXElF83Ch+WnvyzlGFXwACat2abtodVtnbsbz108l+rJsXmXTV8tHMCKEaecFj/exGGbSsJtjL8b7pjq8J8tv0rSekRJD5NMUjaPxLLOTge8OaWqrYfGJUfNcBBsJ+iG9gG1cZ8tJxfMNak9wTcsKSwBW6YNSjcAjrfGUjV31qlxPyCvVgRugrm53FAKqvfiHoQvHFWkL/Ofw6cYKp/9QN+/zSuNsGMojPBdAqrySoM81VxrWIMcKjac7/Cx1soI6hKWxWtUBz15D/Ak6doqFqiMl9urPF7Ow4kNthwXXg1Vvmb8bYxD7N6JSBDtOdi/dMHG3L2q/dwujMRsg1/bWKx7JRcFtrppjuKyoWiz6xbDXhGLvSTNg4wfeVfoGoQfIe9WisLY9GcNBPjWKV5+DF8JfiGn/wv++QsR/zRID58tKihtfeGWaEd7KxZpiBIcR7hWBKYkfGvrGutneKpv3xG/kyDoy5j6XUY9TPRx+jTly4pwIUT1cKFgx5NIrSG2oT/QIba0yFPZX7hInq5VsDhBRfxowFmlhdNqvZ2Mb5MA3nSuVG6/ZPwh/AIXmUfulnI6eex7vgK+TRTVdeGpcf+lfzMyr0aTkPf5semMPFAB5hi/CSBv3Js4NJgE/HVm8DSNF25CI34uVJJ1Xwu0CP1pb1tsuge1UXCeXAVeTVI81wByrl8sp+LZSAUNhEiHyRw5QOroh6k68lzt3w2nJZ4utL2f9GX4HLrLZNrIjkmZvGKt2/NfXlwMdWx27sKmgzxI0q0nfXP60+W293O0MHzGdyvZJlL9/0fnjBEvuj968Ec+yljS/pSLgJudvK945xxVpQV3YFkXu50j+rl33HUznkhIKiqc4XzNoHlLYN6U59mcPLWoy95iTHmGtK95tK0gcTp0 YT7pOYSQ +Pf+BJsGtyhb8YqzC927ru6BM7jBSPE2B0znWtABh9MzbQO9hXjRfFGk1i/5O3UcKhSi4dbe/KMASBwayBR3UOE1WfBMXEVQXDJC3vFylSvy75QhSbssNOj4HWnVae9zLJabQObjxZXl/UnPVxAOtrDlG7weDpP1Lpjj/qv2l1RuwdoyIj2UApHToHC6ywylqBmY3YLe+OQH5AAR7NqSW8BVPY1wJcb9hduQ/sO6rEzRsXpSEXBIayAs0hIVxYrPGGiYS529Zr7FPsokGIGiqv6BKCOkS5llZ5lcMe/UeaxvLQENdrC4Ler5/Swdr5ZToSOU9xU5AKOWIkoiLIUpIsRYh+2FyUSxoAAdSI2zqImi2rzKqkOHCCxIqp3KN8mZBJRnMxTqpO7lSHdGjDNW6DvgQgk9R7NHRvyMPhCznqqD0Ro2Bca8zrpx8vgDg9MmDlVeSpTjuo0abAwV9kvV4G08c8n3wINO/23fwEE4ntG9CVe5rxjR9VibppUN6Ti45gbEwOr4YLQ0U5vyVWYcU289ZbsrNYGMy8jZJfDYue3Dt0FoJ9SbtgwiKzTELzabWAvS2 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Updating __show_regs to print captured shadow stack pointer as well. On tasks where shadow stack is disabled, it'll simply print 0. Signed-off-by: Deepak Gupta Reviewed-by: Alexandre Ghiti --- arch/riscv/kernel/process.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arch/riscv/kernel/process.c b/arch/riscv/kernel/process.c index ebed7589c51a..079fd6cd6446 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 Wed Apr 3 23:35:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13616816 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 67022CD1288 for ; Wed, 3 Apr 2024 23:42:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 58C826B00BF; Wed, 3 Apr 2024 19:42:37 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4EFAF6B00C0; Wed, 3 Apr 2024 19:42:37 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2590F6B00C1; Wed, 3 Apr 2024 19:42:37 -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 E71096B00BF for ; Wed, 3 Apr 2024 19:42:36 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id B1C58140AED for ; Wed, 3 Apr 2024 23:42:36 +0000 (UTC) X-FDA: 81969847512.01.E33CD7A Received: from mail-pf1-f175.google.com (mail-pf1-f175.google.com [209.85.210.175]) by imf09.hostedemail.com (Postfix) with ESMTP id 19839140012 for ; Wed, 3 Apr 2024 23:42:34 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=ijQP5OU3; dmarc=none; spf=pass (imf09.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.175 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712187755; a=rsa-sha256; cv=none; b=JsyRbfDV4Ea+CwXrI2dosEHySrCdlB/ncHCLaMPlhaB+cI83H54OHGkLhcw0IsUhyg/pIa cQYo2BO37ao+xn2FWnKIPf6u6CNYLS/skEmgHy1DLTAn0KV+KIS/crQHv6/DSjsdnD31t0 LcxjS96E7zKRil8a22GEoUReC2ggCjQ= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=ijQP5OU3; dmarc=none; spf=pass (imf09.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.175 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1712187755; 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=0OEwRph0hX4JYK2tSpTwj/IHXbbgfxXFL6/9jyQkq+Y=; b=yGbkO8zwzqJpss0IDxAfE//sP+uDgHvr1qeoaYpdVDxcGzywFmGoBxKX9/8/i4xCrppsrc Zzzsfh/iA4ZnhKXlzh39dgLo8SxFyBzqo2k8A6cxdUG1xPAbG+SseiDSsEzGV7VNNrEMc6 HUKjphQ2xF5PWb1KjrsQQ2U+wpX2Vns= Received: by mail-pf1-f175.google.com with SMTP id d2e1a72fcca58-6eaf1005fcaso274832b3a.3 for ; Wed, 03 Apr 2024 16:42:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1712187754; x=1712792554; 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=0OEwRph0hX4JYK2tSpTwj/IHXbbgfxXFL6/9jyQkq+Y=; b=ijQP5OU3YoV/pNf8iyaPbWZh2KsBudnjHQF7R3+nZmnE9esvrqg5ybW0dhkOJXBCjV f67qQu/DWYdg2t0pWLHtQgeId3nmdV35ArZtSpzr2XajbAyannnVzLNE6m2nmPp9ynTo PduKl1lb3hyW8bmKfxco6x2MUYQ1jEIgJOE5XN24PRW2FEVbV+1OveV2Wpj4dOf7/frw cztFCz+BB6dbNluEtY9J2lLQCpwzaYIBj6kQ/zgoO5A/HX9Z4nJyUQw8yO17od57jzWF e/J8mwyQ8NamM1BT3zWjKak+0mVgtyTgJbIB1LqToNDG2DggiwiyBIxP0D6qqN7Fc8/y 4cVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712187754; x=1712792554; 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=0OEwRph0hX4JYK2tSpTwj/IHXbbgfxXFL6/9jyQkq+Y=; b=lvy9FCQ7AQ9i4QikEaCUAJFewzm8vPNOUCzsHhNmzIliVoMd9waf7w6aO+7CjQ+5ko hUIXVBPy5+L6XJGqZfHGcvRyz+g2Gm8I1W1QufywEr1RL6Ch2roVd38a6RHV122/2Xqp TArYbfX2sITwMOAQfataRfP3+wFzMmN6g7Jg6jkZzBs5dDvdX2spuf1OGgOdPgP5SyhC tIccGlIAoKFAkuwgapv+R2JGRj1knXbmRt9CtMLvSJle10jFgTgiD7GoYeWnsqaYTkVQ ULnU3/HTJERP4EBt8hp4UQ0/G2rHsJYR7gIxPC6eVnwqxlj3WAmu0FtSkK6NBEoDyi2Z bM2Q== X-Forwarded-Encrypted: i=1; AJvYcCWLr87PcHf0s8llUpIErV5TMCQHh6hUV/c66ShFbIcM3LolDbSz1eDGq1TJSRDKzK4jGs+mLVSIwWvcJss7MICrwSA= X-Gm-Message-State: AOJu0Yx4thxc/XzcuY8SBl4kZsX014XgoskwCgEeMQSCJ6XJ5MrbZgGq KbNEy0cvgGABhzbJXhR4UdBQX+R3udPQu+/DbVXFgIcEwoNA7WJ0ZOOj8Rw8gZc= X-Google-Smtp-Source: AGHT+IFFY1JJO6kTtTJnAWqFL/OBzyhhxXawvUaTnL2Gmm8PkkBK+tirtybimz9kqqx/Fu1xtzZ+EQ== X-Received: by 2002:a05:6a20:6d9c:b0:1a3:6833:1cf5 with SMTP id gl28-20020a056a206d9c00b001a368331cf5mr947386pzb.29.1712187754011; Wed, 03 Apr 2024 16:42:34 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id b18-20020a170902d51200b001deeac592absm13899117plg.180.2024.04.03.16.42.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 16:42: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, conor@kernel.org Cc: 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, corbet@lwn.net, 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 v3 21/29] riscv/traps: Introduce software check exception Date: Wed, 3 Apr 2024 16:35:09 -0700 Message-ID: <20240403234054.2020347-22-debug@rivosinc.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240403234054.2020347-1-debug@rivosinc.com> References: <20240403234054.2020347-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 19839140012 X-Stat-Signature: 6znmrxwu7rpnk6hd369sseuiqxxazrch X-HE-Tag: 1712187754-441133 X-HE-Meta: U2FsdGVkX1+KBelQiAwMTuY+ZL1l+2XsGKsR86NxBPsEbUID7lJE8MKfJaoNmfB4G9vLXcx+BzmJP542IUlrzNDvzFNLVc5kVGB3WUfMbvTxHpzKZgLq43M4ndsxV3iOh8ICnEgDseWTTMWNDJQS0WVDognv7mMMFQKXBPbI5nV8IyhYCWcybLbMfF+YSuR8XmwHQsqyvxDRhuZFNGVHu5GhgTLzhWr0d7I7sjKFbCA2DeA9ipBaToztRv9fAgzHmuVSyEQ0QOIyj5aD1F8OTVnW739b7kFD30vnYPErMTKY9lO/7jku69R3Vir2UjLwyA7CJQoAiCC5Pe36R8SWFi08XScG4zlL5bUTyDFkCmHX7jlfaizVtVmoyZgXOKXaTAAU9wpBMzzl+E6TKqZek8ZSu/pWnJpDqWQnO3qfJiJFrIsGNYsVVEB3zurc/FW3dysRHKJCqBzTdP111OLiY0wp06c6VWD1rrUjhIbRCrKvIgmbgUJtMtSAsQXcgLLExAnFRfS+IgUA2KfcSM5T+CJVfJxUkAcFt+VCul492HQlbJIhfLaSpR+jxmdIr3/aUPuDeBdmyzna/woICUU4xyL5oj3QR7eBeobRh9PbER1SkDU3oNgTVRCp+GYdvWOxaSyIbLgvlUehhnMZcnu4OUa8XJbsnF/5YQsarYQA6HIwXQV02jJsVNgZCOhSMNX0Gw9pE+Q3aKgiuzKRkurkXpGqLTu7F/F1ZtPX9rk413AMXwtY8KhVweQiYIHN0kGTa0TItErns7/5LQyIXSiM+eF7RZEehx4bYWNS2PYWz6hEZHLEukSFsTgPAkyQrU9EBouwLQZ4P8BZvJiOC2I+PRNzbSJR590WQFqUGv76TnW82MQvczKPex3/H90aMlIpaali5ya2HTxPSKysC+1P665BA9lOKF52/cVbVQDBIM4DqMEnLkGtlg5fYvtXnZYtEy6fdtgG22blh9sFInB +SaASP0r tyJqz4zcEEKOzMOvE8+Iy9f9CbIHKxAh9ASeOGR6vFcWzd6Go8JGeuudiYpuyBBkR1CRy6O9pMqVryTfrybLrxwjmWGjNSkzaZs9L7GomlpdhoLmDYM5Hvb+lb2sutihQm5jU7SaA3MUJJ2IkdByrOZax4pXjX5nOjlS0LJMX98RP6Hwyp+wILpIBY+5tIXUVNEXiC7PHs9CDgL7InVqQC5XJCcXghuB/N+LTTJYVi5BJwjw= 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 Wed Apr 3 23:35:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13616817 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 890CCCD1292 for ; Wed, 3 Apr 2024 23:42:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 47C7A8D0002; Wed, 3 Apr 2024 19:42:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4045F8D0001; Wed, 3 Apr 2024 19:42:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 195F28D0002; Wed, 3 Apr 2024 19:42:40 -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 E096E8D0001 for ; Wed, 3 Apr 2024 19:42:39 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id AA1CAA0ED3 for ; Wed, 3 Apr 2024 23:42:39 +0000 (UTC) X-FDA: 81969847638.18.7772C4E Received: from mail-pl1-f178.google.com (mail-pl1-f178.google.com [209.85.214.178]) by imf26.hostedemail.com (Postfix) with ESMTP id D11F3140011 for ; Wed, 3 Apr 2024 23:42:37 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=y7L0XcN7; dmarc=none; spf=pass (imf26.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.178 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1712187757; 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=d2oWt9LDdgEXqZoqmgtZyb5OtVc0MRfnGzJdb6Z6vKHJadWsIEmd9S4vMRFa0a0l8lMCfZ kZYF6oVO2YJW6rq3ZAJDdwcBseLaCzlSFxT7qMnxlrwAKSX2ndKHjPob+oD3Zb4/OseYcY QHN3Gd3n/+7MbFtBTwFeTKIbILvW5v8= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=y7L0XcN7; dmarc=none; spf=pass (imf26.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.178 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712187757; a=rsa-sha256; cv=none; b=lz9fMBjMNXtvB6p+DzoW4HF8o1rU5mqtIEolqW6tmmtg1zBGozehe03EJOGN2ZUH1H1j0i qssyG8Xww/+V7MYD1yrt79sSzUkIYX7GiQuNf5HNVCk9wEQ9xZJBYsTC/ytOCAmzK1OyaD 89iPmK5XfZVmKA5nTGJCivwZIuM93mI= Received: by mail-pl1-f178.google.com with SMTP id d9443c01a7336-1e244c7cbf8so3460095ad.0 for ; Wed, 03 Apr 2024 16:42:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1712187757; x=1712792557; 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=y7L0XcN7JL/lJ375Axwx/szC+4QBs+tXTAiIgP7n/aej4g4BceZSjr0higtgU/eCzb YsrmkNHpCIwh2myCBNcU8SguVD5En3DghfKqz76NkFmHxA1m9QHWBpFirodrYYtBmbhj 1tVg4dBT/zwPQHtrRv8P+TLTz/b0xFVQhlqvL74ZSVv1loIeDeHxhKP4aJ7dLhRragXg urHLFKp9NayyTFvPHfnYWKAswQsYxEK7I/5FSoyC55IB/vuLhY6tMgnUmDyzn8zj5G1O nwiVj20OFzx3taWO2j/7IcvDa7pLkjUX0aTK7DYxQarMtWYbshsi3CUsRaFye0ftZWwE T8fg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712187757; x=1712792557; 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=rLOkIv0kdy6Trjdb5vvd4w4t2F/J0t/JZ3ys+9Uy5V4BcJDAphl3wVSI90EYLou1To x68VZNXLo2nyh+7MccV5s5945ECn/0MFMDeCXXINXzSPVu+7+dct5h/hSBYypzWbUNZz 2aroSTYAP+5GWlrjv0RdC8nDoHtgoQjnYRRPa2vhuCb1O2Vl7bXIP2VSwlIcdmC+FZHh z1J7//2wF6cTgawK0DZwOtLgSFoeoXOMvwCukBFFRZxs9r4iLV5z6bBBWumkN2gOmhMP 52ea0IraqlAoGECRttxM9Cf2HAcBlWdU2UiUHIDh+z+YTWqHBxM79IoFr13hftL+Fb/O z8Xg== X-Forwarded-Encrypted: i=1; AJvYcCVL0e7xbU96V6sxmOCrY9OZMozZf8vu2Ci6fg6ZBkLH3kNvt6rjgNKIm311m7btXSTEVG8n/pHGQF+x2x2aEaOyydw= X-Gm-Message-State: AOJu0Yxhd3B8ERHBeTluapCaYKuyGm7RbPo0MP9D8PRwItvpLtGxF0II zsvgDgRSIyxkUAMJC5+eRw/R/LbTO4eRFDoEZqSog8OtSE/2o7dkG377YyBoTJM= X-Google-Smtp-Source: AGHT+IEuhCBj/2TDLKKsCyPyIBeMUKZ3rkD1bUlrx/H25eFGajkGFoQcn3/TeMJd7JPKfofV2mRlIQ== X-Received: by 2002:a17:902:d506:b0:1e0:cdbf:24c2 with SMTP id b6-20020a170902d50600b001e0cdbf24c2mr951254plg.29.1712187756641; Wed, 03 Apr 2024 16:42:36 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id b18-20020a170902d51200b001deeac592absm13899117plg.180.2024.04.03.16.42.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 16:42: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, conor@kernel.org Cc: 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, corbet@lwn.net, 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 v3 22/29] riscv sigcontext: adding cfi state field in sigcontext Date: Wed, 3 Apr 2024 16:35:10 -0700 Message-ID: <20240403234054.2020347-23-debug@rivosinc.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240403234054.2020347-1-debug@rivosinc.com> References: <20240403234054.2020347-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: D11F3140011 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: yg5ms9qe7a73fmg4a9szdrdk3ked1a19 X-HE-Tag: 1712187757-750605 X-HE-Meta: U2FsdGVkX1/cG95Vx1l6it61/0K9ZN5nCCzIJ5aHol7L/aIj3ak7jQBrFayZ/QCz2HpqmcZlEZQHcVaSEvg9HjFHEG4YqZ0w1AcPmWFrqeSyglnY4ArQ4pUiPMfJMPgZFa3Pr20HTcmHx1hgEJDubCKA4PU73bmGDFOF7q3EooulMryetT12xYKBjDsp2opMoU9XXYUM+9493fhBW7M7ZATlFn0dYccyZ2NKu/5aqZ/qW5jvHPizSYeikM7R6JoAu/kfBB8DqwLy9DGnLE6TuhX3USd8zKXE8+5KW/fBLKSCUlwy7+aVovPOKpitu8jeUhwMisXwlMOYwqAfY7F5ZkxJsbrhRix0jhICxMaTggPpwnAk2AFizPNQ4La5Uj5Vrw6aYx+xFQ3/oUT3G/lkWvB5vjVa9EhpBax66ucjVo+UXfkMrvVF+L+WYuRtyMhK97b8RBE454lbL+EIMp7lmd8uZLZjQJ5+nMqU8t5QtT7j98HFRSmqX+L6djxz35+Zv5G+C4kTvET3D1uJ84/XKTMnDz2W4GMxrOarFy3SFeWk30nmkePWJpuo1M0nL4ga+3dnq+88cGNBI2DPQ1Lm9CL2bJBwsWHXD+XuwX0Z6vcZuDmsudlwuWLANZ8DbcrR9txtrvpoCwF8AzAOwKs9V/GyMDuivv8lRVdYKdCLhr4mEWsZV7z5T5Bl6AWX2pyL9DIHNSSwCfTMK3e1MpE0RvD4fol5rPVdX/7pUcFu4ut+SITWHpZBOCfSXQjSK3SF9QaKymVwX4sJYKZmglAw67IEkmPZjDWT6JKkagTVYPx6merWLxCZV6zciGv0IrluezMCtbsdLxYHAG4Q0rPlg433mGOi8e6egBo7ALn23dEYOv7hYUFq5lmedv+qpB674rpURIB9c4ypG39+DZPrm7QqAoSc4uV9UIEKl+wDre5q++GSOIIBvVlt+GRYWQYMM7U/95vpfp4hPO/8uRJ nhTGUAja 0xkelOSSHROdcHT4SkZCuH2dkc5HfSI3SiDsCuRXb6grTNuNdpxfQlE8HHGAIFwLI/u1ILHloyMVQqaDwWMSByOrPGB9o5RZ6kdJ5NJHwlPsK0VgMwR8c5eSU8CjcbRyjEPUEzXF28FL8cFHWqc0PKrJJ5B2LI769FrRMvAebUPh1I0Goa236/CuSp6xglNMR04ODfNh5zlGuJgt3wf/E15WsLsEqdxrNni/vzdXF02oEIn9k5cXPEfzLjzQ0Vxs9B+Lbuv0tOurLQvXdSf1RjFl47tNlMKP+LLjSACpXnSIRqfZvgAvuWHfOTyymULnJg8Is6nrlTGsCltqPcZ1vttu+jMv+EgjmDGQObTiwoS14nt84A2dqXH+ir+ccTRvk0qNQ5z7eQh14Oo7yoSg/YNGd+KVV5VA4fHVE0Px6WZDjOznbIZxq/zUYoehYeAiWyk+EAGBqjld1WQNLM4mwR05awCB4dNosSJLrCi9Cg2DiUk1AcPZ4orHqVuqmsIKISbAVlJOFkivd3oE= 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 Wed Apr 3 23:35:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13616818 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 2A621CD128A for ; Wed, 3 Apr 2024 23:42:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2658E8D0003; Wed, 3 Apr 2024 19:42:43 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1E1868D0001; Wed, 3 Apr 2024 19:42:42 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D2DC18D0003; Wed, 3 Apr 2024 19:42:42 -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 A42988D0001 for ; Wed, 3 Apr 2024 19:42:42 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 63E0F140785 for ; Wed, 3 Apr 2024 23:42:42 +0000 (UTC) X-FDA: 81969847764.04.AB1130E Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) by imf17.hostedemail.com (Postfix) with ESMTP id 8B7E94000E for ; Wed, 3 Apr 2024 23:42:40 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=1iZ1zIEE; dmarc=none; spf=pass (imf17.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.172 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1712187760; 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=Khz8ikyQpN700q+teRby/p+dyzEQ886SCy2AoWH5oD0=; b=JZMqv8xG/HFiAQSRTETRB+sn2HjGBqc1ngKu+URxFVvtOhmimQlZvLaCbBlJPLIkQlruO6 FeS9A5q07iVrBotHz+Vr3iBlP175Wh8s7FquZEQRhaPULgb2kR7INhhd18/D5vOAPhdrPy vOf/3uxfYSiC0XEHfO4ZfxXO3Myp9II= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=1iZ1zIEE; dmarc=none; spf=pass (imf17.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.172 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712187760; a=rsa-sha256; cv=none; b=sIM9ygAPAYWmx+2pWRaKs3j05MxI93Yp/o8E6Gp1+SmfoTljcKiIfOP5oQ04iQ05jZvGg9 Ar1XpwZ1mw4yso0prF92Y0d1CGnKXtQ5C1ZIJi2sHwztswQqXB/pQeFTOTd2gnc+t5UW67 qZ30/7CDXJ1ApPV9QquJa3md+TtbPg0= Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-1e252f2bf23so3149055ad.2 for ; Wed, 03 Apr 2024 16:42:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1712187759; x=1712792559; 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=Khz8ikyQpN700q+teRby/p+dyzEQ886SCy2AoWH5oD0=; b=1iZ1zIEEf7NkhihLgkw54c+e+G20FEmiLd2lWRfzKcxknJZs2qkKGaCWc1BkN9CM8g xQ3b+NvJ+5T94vm+GozV2DzR/5rhXVmdOUCDlfoLVLY/ajgvgVBjti2mONriAYxoujDG Erq6Ue1jOXOvL2jTpdq9Yhpq2Kx9WqXKmj69P2ozBFtZrno8/HV1bIgOxZ773QZVKqd8 w22B/4z0ZkS8lSEfpf8O8jhAuAbQ9OyHYO6epDWQ/6UM+mKKiQTCcqkyVf7s7Phh00S7 rNP8Mj6gmo1C+jYi+yzj2Z+d3zpupDJ4zJWChBBvEFppxuKOvkhbdA0v6FUDZtiv85Yc rV+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712187759; x=1712792559; 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=Khz8ikyQpN700q+teRby/p+dyzEQ886SCy2AoWH5oD0=; b=DnN5m9grXL8j3xBVt8Pf/9xy6i+kheW3xNXiDVsEdh2Df/V6USzQ06eKNflOVHNG8Q BJD+JKfX40rgyRwRJbN6chd1WYxdw4rQ+JIqvUdht+vzZ4WSH+26R0DEQoi/tFWnobUV M91XYJThA+W58pXOXwZ8fTFHw5lSkWCtKyZ4k8Txbd188OlReh0EkoqANpa6wi8iOUwr M8OcoQnFtEMBo3ppsUSdnQpQcbbnz3C5hVhp9gGsWs4YslhV6E7w+WuguqfcdsTOco5p 6DsZxTGGegP7Xkc6Z96yIU6fAnSDJgDb19+UizGdUbWoP4ltev/fU9CJ1yQKjvlxQ8r5 rMXA== X-Forwarded-Encrypted: i=1; AJvYcCWX7TBug+v8YdkcDFQ+x8uenBxPE4h6As3kveolz8cTOKOK22LeXc/43Tx9V8VnWwAWN97eCiZF6/n7SEBO7eGVjck= X-Gm-Message-State: AOJu0YyGrxGG0dQ1NK97L5P3h0c7qLS+mE9XVBl+xLBOlAbsNIB4yYPa rLFKvsGld8SUNTbwG/g0hfhnaKk/oH06EzXADzDi9MvNAFmJVjxI4hhqIrl4a8A= X-Google-Smtp-Source: AGHT+IE68qWmATPhXV1Rat+Ver9WaYarHZF7n/8/iZq8gRLxh8kCGb8Ckpz5WZ6DY6GOuSzrKLE5Sg== X-Received: by 2002:a17:902:e5d2:b0:1dd:c288:899f with SMTP id u18-20020a170902e5d200b001ddc288899fmr843683plf.18.1712187759354; Wed, 03 Apr 2024 16:42:39 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id b18-20020a170902d51200b001deeac592absm13899117plg.180.2024.04.03.16.42.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 16:42:38 -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, conor@kernel.org Cc: 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, corbet@lwn.net, 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 v3 23/29] riscv signal: Save and restore of shadow stack for signal Date: Wed, 3 Apr 2024 16:35:11 -0700 Message-ID: <20240403234054.2020347-24-debug@rivosinc.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240403234054.2020347-1-debug@rivosinc.com> References: <20240403234054.2020347-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 8B7E94000E X-Stat-Signature: 9q3hd9c9uwui3x69ztgyt3bhcr1zczzc X-HE-Tag: 1712187760-328389 X-HE-Meta: U2FsdGVkX1+nQLNlCPnwYw3n5MWMbbrES/aSEz2MFScpRLNbAeq0Aha9mxopPD79nPoEHG8Av0j3kJuC7zCcIBgg9+UcdH9rRkQahGt86jdntp7fXHZUBsJbVSVVkde1SnAZ70AyTXMl7de735MGTyZAz2WGhbbvVzpJgZSUdiLKM0F6rip86YBgunIgnfTVUA8b2Zvs6vfdsZ8B+UinOZY4uq1XmP1Pgbq7NbC0kcAB2689NAHK2NYQBcxCEstmh6HzusduZK5zHcSZHijrcnsh0D5/RFiXbrW1oC4rgatQ3gLBP9pZQHXBt6PBHT+xVMh09X0n5XpCUjbajV/zin6fpIO8SvHPopoMvAU8AaRM8mpkukDpI6FiiZsZN+PrdTERucDyp3n/opFJKzRf5Rst3BfZl3iRwVrf+vWsNt2v1O5kAX9p2dMiIU+HM6CSgrBadMqh3kraUXs9Bs6mIddL5tIGP5PTWIDslF+vAzcP88S7HBmu3aSWArWTELUr015IA1UkI1QoXIPokgVe38Yo2+ZVQYgi41aaWKOvRuAk4W0O2RLefhcOSM4j3mezVklsBEPPa8TL4FoSGmdVt+OkQJ6k9DHK6WkOLeh/ohr0bNwvwi2pjV/ORTIUhAMW/nOiN0adxCaeK1AqsDbnKzHj2JIDRvOSAe0idM46svgmaoPuFa4xlEB/Rv6o/9zi92hVY8v0sRrhTkjGueiLcPEQfFKeR39waUroNR2uynReVZEK6RayFwpmiRlA+OZ7lxgQzrVTz/kByRTDLxEayJK3LUF2mkStamoA77njval7xK1fguwoBoDmCWG+RKdeM2zIAEJaYD9pDoDqyToSYW9wIF2QsvGNqe5OqEKEcB3LcDDhmtd/aWt3SZzK2SvhgN8ndwwFOQWQRwCDR0nQCnWSgMFxO6aeRTZEJy2IMV2D/Stf7HLIXX5r8Z+5XVol3XClNelM7Ksjnb1pGOb Tq6qe0YU bBBTRHATNA4X6T7axb8bKzkhpUe12IKIT+eegE56LdyeFNF0sAjG5gVSJl+9LjQmrfMXfae6b5JdcWh3O0HpWUzZRarTsPtS5Czf9EfiSmOcKU+yfGpCVoRRMeG8qxZrKX+ey6sibaQZaj2S3juLns3Qhq97v3eEnqC5GREPS1bdtx6qrTYFXF7j5tERk1C6ClacFb6sDKpTHSMY3GZ2Qsn+orKHmtT93qshmOaNTW0epcjWXWjn15Rk+CwON9T4uVLcKp2rPtM2MR537UouZXiBWzo+o4caWOWjHGtTUXHgk+bgoZTIP/QqMeSg6wFDEqS/HGyxIBoNhhxlLBZZWqHA2GLUkO9HvzmdZcHWN3C3rO+faLOZKHtStogx+V9f8RZz0se3RxVdwI037guvNmc2KPJtHih+fOMq8Pwr+51LDw5pdr00HifIR9yNSjL7aSF8Dv/U5YAZ1KdkVHvh15T2CzE5u+982GigXx/17KA+SMHz5y+S8glEf1MP5b52wc/8pX9+ys0QTPUU= 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 Wed Apr 3 23:35:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13616819 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 CAE11CD1288 for ; Wed, 3 Apr 2024 23:42:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DCBB88D0005; Wed, 3 Apr 2024 19:42:45 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CBC778D0001; Wed, 3 Apr 2024 19:42:45 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A455A8D0005; Wed, 3 Apr 2024 19:42:45 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 7AC478D0001 for ; Wed, 3 Apr 2024 19:42:45 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 49014801E9 for ; Wed, 3 Apr 2024 23:42:45 +0000 (UTC) X-FDA: 81969847890.25.B1EA1B4 Received: from mail-pg1-f182.google.com (mail-pg1-f182.google.com [209.85.215.182]) by imf04.hostedemail.com (Postfix) with ESMTP id 6DE9F40008 for ; Wed, 3 Apr 2024 23:42:43 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=rbGh8mI0; dmarc=none; spf=pass (imf04.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.182 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1712187763; 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=N6kkFLieBdVgkT/dKS0SQr7BbCzZrJNZK3OukPXx9BWYz7uxeaxxzh4l6jyiMcG0hpI/kM Lj4Tm6+/csugUeZbh1P6xRCyS0DX8+w+3Ra1SbzzdPMrwpe86E+YnEmgbs2G9i/2R5TGOO GBz0IARbUBLUI9tfpS1fJ7V4MArEfk8= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=rbGh8mI0; dmarc=none; spf=pass (imf04.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.182 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712187763; a=rsa-sha256; cv=none; b=3/MIuESqbey1YqDxW2MjGTLUUTXR5E8pIROQLli9ICTvUD+MWGsSq8mib/UXUe0P3XLfjY bcyAklomYXMf7phXFP9x6rewTDovk3xqH7DD5R9aUp2AHp0iWUteh321GurpWrUKPnMtjF SIZ1mc1kBSLjwiCUTwEL4DjH/1ONCiU= Received: by mail-pg1-f182.google.com with SMTP id 41be03b00d2f7-5d8b70b39efso396100a12.0 for ; Wed, 03 Apr 2024 16:42:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1712187762; x=1712792562; 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=rbGh8mI0lSIPwCI1gCMbxy0Tie1tjUKh5kxtdqkroUlcYNz6DKEUzXUAlRpPJQlSv5 9gu9bbasK4FAZAhIm7n4Ibi8CkTK9ypk5b4Oh7JXZNktsMBHbhDqpLzRFgjfi3x5TZTo UVwQRaAYa0Ef+75U6ThLGlhkmTRsmqXO3yoTyMPhXKsn4zj4y8sYx12mm7TgLfsCN//3 2d4GhMeFCOcyJG7EXbxrSG9Q3HwA+GKoqEHHr/KEXHzZn+LcuE6qJ4l5Zlgp4PqKno/c R3aIiNWoZu55bQmk+OvJ8SFiSUJOHhOc4uoUwxLrSWVJoR3AbQ3fLsu1n2sqUsb7X6Hn Fxww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712187762; x=1712792562; 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=iT4k1tKNEtkN6FAWxUoZbVyLiWnBtTH3MZ+/6uT1zZko4LQwox4wj+esNEamc/SmAo NJwj48YezbbWXMDjj2Exv0AFZLf+1nkBNQ9XRkSSypGg2PJHaq/nR9DG8yb0p3CAPms3 sy8VGdeR2tttNBNJ4HOtMC2b1Y6iHrKgvv7F6pR8vlVySfia0jZCxleqafVb2i3nQyIl v0kpJFZy2AWB0u8dJqLN6wpdEq4cwoQkYMzrw+ikDRdfmi27hq3Tc0ZlDJ9bYKsaz8QD pHak+IYtETJFONspFo5YahMSGicaMrRBeaNre/DowaSyOSja73ZBZy7+kFwVL9NaLxnV 8HQA== X-Forwarded-Encrypted: i=1; AJvYcCVV/uqdOY6ehvR+kyz6MVrefOQlW5ETqC0MlAgD6kHpYymRWmMuiJr2bYrvkk687e3VbFn/stDDWnhEMnBbmHHh/Pw= X-Gm-Message-State: AOJu0Ywbqs1n3TMww/+lzJyaqx8YAG9nbplzZP3rQ8kJUbk5J2Gzm+rq 5gPgdQ15KqZtb2mHDttA/m5tBiDgXX1upLkYtj0SucV9A4WIjH1D8aqS9746BZA= X-Google-Smtp-Source: AGHT+IG6JjvBwkxgqryw6XYXQuT6s6l7JFwHNmCUllxfSiPnkNHrCcfUHOA44/lFJ8XzXC2yFAlGAA== X-Received: by 2002:a05:6a20:6a0b:b0:1a7:ea4:e13a with SMTP id p11-20020a056a206a0b00b001a70ea4e13amr1275128pzk.54.1712187761904; Wed, 03 Apr 2024 16:42:41 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id b18-20020a170902d51200b001deeac592absm13899117plg.180.2024.04.03.16.42.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 16:42:41 -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, conor@kernel.org Cc: 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, corbet@lwn.net, 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 v3 24/29] riscv/ptrace: riscv cfi status and state via ptrace and in core files Date: Wed, 3 Apr 2024 16:35:12 -0700 Message-ID: <20240403234054.2020347-25-debug@rivosinc.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240403234054.2020347-1-debug@rivosinc.com> References: <20240403234054.2020347-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 6DE9F40008 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 6qtqo3jdjeqj757denpbrikodidhf9f9 X-HE-Tag: 1712187763-515942 X-HE-Meta: U2FsdGVkX18vvG0cSaCJ7weOY4fvXkNw9xPm8JaHJ5JCfLomdlvO0sq8cKE5vZXP42L+DdzsS5+EsTA9ww3aUXOO11E9W04v2VkjshMsTk5nsjtmxql7P9z2fXN0HLxTqt5bW279rjX+YGDISl25KrwLboG5PthLsRWICSjdee2p3lJdXRCH18xx7UZiwea/Sz/Hr5COwHBaRfIkbIQxqiQmOjmu80RQsV1lnSRrAavsMkZwmFOHBZouq6XJD6fIroos9YE2qpXhv5GRF+oup8LmOHMGeGo69rbDAYXVg/mi0qlBSejyJ50ZdAQMJcNU1bIzZAdXltzK7QKzY7Em+Ly2Kpvw38kHxdwHnPzx/mxSSGuSBwvO9o1hlHK36PRuWwfjf7Z61OgYOA/6OFXBj5dKCd1ZkDzJiz907Xd0IzqtE7KYcRsZJoG4kMRxP+2+TBx/jwVNHhejGZMO7eJWgZOOpTz/tPMEJng8KhyP4GgeNBfmtboacq1LoXrJ8MFSG1jQxTI8jyuZQRP/7sIGgEQV6uK/yIPiKatVNYt/O2JXCrSeVF7U9WUtnJHNICJWxuTlZ7bI53H5hNTo06fqtmOoOxt6L7OzRJe8F0mJdYzhwpsPz+eY4ojvEyezb5vMWdnhM/cQFGkUyQ4Y1cegpjOTWKG2BpsLfxXCs2f5Bnx5Jak/epeGRtDGJ0H9M574jffOKi+KPIGJBckS3PZmmbx2KHRJZmIkeMSSTmjcpwjnGrcoJtQefPRbHDgIjkAUzDP08IhVhsjHwa8IMq2WBqLUeJ39xsKOif2RryYjoQ7ZhKa+J/v1rK1w1EiLC2YhxQuz6z/zcP7b1fB3Y0AvdhdcwXT5Y3CqUXqSDGXnnhGIGp6Gs23+mocvycMs2qxMp6PWtL3Tw9ppG4Gqia5n8MPU9PT+E0MWSJ2QVA86pmynWKzyKRLYmSvv4CxWYYo/sTaTGfPKDtQ1SttCenw bl6+25KR 1+p7IvddHuH82YSQ+0b6OC60cHf8BDPBigcSRWElJEO7O59wFo4jaJvyuzDU1dkC4kM3vyFh+fNQH0iS7rwtJYH8EsdNdDfdLR1SsaYgpzI5vtOlcMnsYMcx5hJcjkLGLi05RLJDZXc+o3Z13q3qSc7gN2x5eYM4Gq58+6wucyf8RxN9vIWKRb7gVcBMmggqifJaWoxpoDsEwXV1uaTjf5OKsEbQljjHeDBRsxalTM14fDQzo47T9gK4VLHmY77S4gyvqB8hPER5P0lRNFRl7yJ3JyXr6uGqoWckls4XlYrOFenAPQPNRuuXQFheFhne9jIqWqqQtH0PEyTywCYc9uEAYDfr4gRvFa0n0yjydfC/yf+MUE3uypUMihvKv83GAcEA+MCav/VJPSx5nRWp3DOTfcDcfj3ZHmo61u6T3Jq4g9qD68TejHKxDdP/bXB75sxPydoVxstKjUkwp/b3cnivcbQButDTd1cFnqpGkC3me5aRDfCsUTXvSyNBnuSBMLYjne1ha3zlEa6s= 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 Wed Apr 3 23:35:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13616820 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 2BE05CD1299 for ; Wed, 3 Apr 2024 23:42:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6043D8D0006; Wed, 3 Apr 2024 19:42:48 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 53D578D0001; Wed, 3 Apr 2024 19:42:48 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 31C038D0006; Wed, 3 Apr 2024 19:42:48 -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 F22D68D0001 for ; Wed, 3 Apr 2024 19:42:47 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id BC199801E9 for ; Wed, 3 Apr 2024 23:42:47 +0000 (UTC) X-FDA: 81969847974.25.9988FBD Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) by imf17.hostedemail.com (Postfix) with ESMTP id DA4E54000F for ; Wed, 3 Apr 2024 23:42:45 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=sgVuH2jS; dmarc=none; spf=pass (imf17.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.179 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1712187765; 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=aqdiAdr1yNbBMpdTZAViS/5DaSW59POCvFmbCyQTXxo=; b=4txAxcRY3fboz9a3fxStF+mnZ7DQPoNdNcPnzaCO5RztIYki/YpASXLvrmXIBUx742k9b9 OpnG9Z+i6GvqxwQ7QSdsg6rOS6nz8OH34qWoimfpa/MG0sFj1dsLlhaQ0OnOJ06H6m8N3I sxu9OJYKr7WxnlWoNUEQZnIbIIwM8HI= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=sgVuH2jS; dmarc=none; spf=pass (imf17.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.179 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712187765; a=rsa-sha256; cv=none; b=XvkwNR7bDA/CMbpXn0rftot9jkx+AcfaDSQcpa08aOOIvw/5HBU/PjERWK7jl5B9KW0WLn 9AUOdMhipt1sHjlo8kvyKv6sHEHtdtswrzXAX3Fzp8vyfT/3acXDhEtUnH9p72b6dbwlIo ZHDV74uUcrcKGAf5jlbdvFXevwDcno8= Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-6eaf7c97738so297115b3a.2 for ; Wed, 03 Apr 2024 16:42:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1712187765; x=1712792565; 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=aqdiAdr1yNbBMpdTZAViS/5DaSW59POCvFmbCyQTXxo=; b=sgVuH2jSDWC9N2mq15U3E7Bmhn0UoGK/dWsDSZverc8AV/mn8XeYRQD1hfxWmouo4o YphDhbN0PElUODPlGY4bk9bX2Iu08PFu+nmq1F5rqNTEaRT/EsMilHfMbueq+hfCdaTy nPD7b7jSDlV4UWkchAgMjdZd4svDUxPTOciFyTqn0+YHI/wW0C9eOPdGJnAbA+lPEMIa y4DCqs9OJKF6uXliEhrScwwrTSfdGUQc/OdCdCzgPXihC4Ud30Ou/i5EXFlwGMVNIfTB WJae8zVh0/xDKcpwHoVlIFCeU4fX+pPlvs3qK8gumsDAvk7i4u8zxKtQoZcE/ySBbWwr 4FoA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712187765; x=1712792565; 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=aqdiAdr1yNbBMpdTZAViS/5DaSW59POCvFmbCyQTXxo=; b=PgGMUi7g0iJo3pMKQvgiQu/MRWLGOCF2ouo6kviFM560M7l0Ucq2B9DVlSQFrMUHkI 1c9xSZtC+IWFiwZiv+sbJZCY3n2h+j0YIrWO8y5rKB3/9xkeNvdhvHq2tzDtNAiZKET1 3dmav8bZADYfrSD2ckA6wus+ccSU/Iy9/9Z/+7tONZArLW1b9x+GaWksL5JpRI0DplWA YwcswuPELihkSg3nZ9e2CyvYO5g5sMOiJ3E8dhh6hC7vFkKHUTypZb2EC6+AwlgmTlxe KPLHjYod9umqlYxdjqD6FmPsdhN5bMSr67KtENHTWEToPW5o2YtSHu0mKNHFlkN8IdPv SCJw== X-Forwarded-Encrypted: i=1; AJvYcCV0Ij456CXHOhCTw5s3ov5zkE/Wog/y86r6mw3nS/rBly1hdCPft7BiRMTYdFOhK0KPlxEBl92/Fm0ZJvAnlo1MCT0= X-Gm-Message-State: AOJu0YzhqAN7epdoA5pMjw0C9t32xy+qN90gt5ht9N9BWqgsKdqWNt3P zw1864brcH1fPva6pwkd/vLfA4GKW2BNVTFi/ZSbicsD9E46e1ztDFzuuLXb+2M= X-Google-Smtp-Source: AGHT+IFo9UTPYbI7WojHDrQA4c7uxF6nWpUROcJy8v8Ru9VCeqKQcLt71oVNVn3YtJu+elhu+4bSwA== X-Received: by 2002:a05:6a20:3212:b0:1a7:4df:d052 with SMTP id hl18-20020a056a20321200b001a704dfd052mr985278pzc.61.1712187764833; Wed, 03 Apr 2024 16:42:44 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id b18-20020a170902d51200b001deeac592absm13899117plg.180.2024.04.03.16.42.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 16:42:44 -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, conor@kernel.org Cc: 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, corbet@lwn.net, 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 v3 25/29] riscv/hwprobe: zicfilp / zicfiss enumeration in hwprobe Date: Wed, 3 Apr 2024 16:35:13 -0700 Message-ID: <20240403234054.2020347-26-debug@rivosinc.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240403234054.2020347-1-debug@rivosinc.com> References: <20240403234054.2020347-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: DA4E54000F X-Stat-Signature: ihoox6xxphq7wf4g3fkbjngrgkqhrizc X-HE-Tag: 1712187765-954072 X-HE-Meta: U2FsdGVkX1+J4oXaTbzsVlWW+TwqD+hXGgXyRG0yHrQppEfMwRQs9nliGrapXMsSw+fz7z+gIYDvmc8vAuGKb9eb9zoqKxndjg+NCxxGN6Cu3USlwTWXfkVMSBw3ZPLrtGsT3fmTxAPbYxQVHYfL15HKjmdfw8YPJdsNjUPPnWhDJl6aHYI9OnmVDS8FTJUXuuKf2P08lYcI+W7DxX15oIKnqit80gsfiYbP5acp2QXCs4+PbZlFejSw4mj1raua+f51vuiAAF6kwHtxhI0/TCYXZmf3Pvrk4sTjAI6eZAoEWlrJLy4ufYfLUbDtVKVPCLOVGIpo6kvc2xXPb9XaHuQ29uikTgQ6zHcrT0YQuBv+wDrcBpP2fd9xQaoxjlGN9p2NBxnXBrn22FxUdR4dPvj0BHcv4mz+2rsEMnGgilBme98NJkuJ6VqNIuDpHgPjtWkgwuw63lvDaR/Y3nTI35462kH4fd6HF/YBNVs4RJUNJ0+JgFlS2RFnvjfe+7BBxpJvrLqa6JL99l8m7+fTz92I4MT7sP8b/FpoIckVC2kCPWPyWrt3zGwmzmmsM4eVwghK0VfHn+XgqaNtb/7p/lPFB9mlGKEalACtUQlUG78EkRbw3sXazmQb5vpTvlbXLfZ4trON5jWiT/SQ8+3+i1zs9SE3AF/G+k9mPmc9cGmVKu2JHyXjoVMoyxu1GjOv9GY+SDuWxLvrNk6QowlqYu8K0MNVT+QTHInlhrXKsCn4kfeBBewcqP9DWTRP3wK7i41vzP4lmaHsMCpvoQXH8dHDDYxKFkYWf7kiVIylJeVx7tBiHwA/XOtz/ABjvQ2U7ma3JrH6AzvyFEasoQ3N5vPxW+tJAkqTLqwAEffe5q20Pkrdr/eFpFt0ljBxG07LYQJkHQ+hKo+11n38jWCoxgDO49NyfkuzkUFmuWfvgOz5PE9UNVnJ1QugVIfKPOFmOwnpaltaP4Qldjovb3z IYx0mLg/ usOQeJZvDfsz0ADXpR3pAsy+o81r6p3KU/ONL32Ijf70KDGnEc/wB60KaURHheSO+5GIpfLoyI/j+t89YqzHfY104pafXKn2wu8Ju9uYkyUtt4BkY6wHqHGw/kuv/82IllKcDqoiNHu13xHEhozKKNIyBjXBiNsYcz8G/HTGdFHHhekxuBcsn7a1DvWrLNCNPUsWVgo+v6Ug0AmSuwqQexQ1qjfea15G6dx/PMg22H2iQdt5gFs5/ko5KXopaY7Q58YFBniK7R7i2wu/+LjGKHmfU2/VCdhqJMTaPUoFXM8kh3rPBPigPB38mCley2JzAvrW7yVIy44+8Ei0SFErMMMGaxJcuc9sq67cjH4MdIwedTQfoEnvfZpUuRR+X/gBUoW5crbBqtk4qOQylZOrQwix1ATtZSx2dDXSTdFiKXoY2wRxILqSH6NhpWWMR2tuNk+spEShECBW6TQlccf/+Di5/fHw14ePFGQmHMOuJQFVdr3J6UdTpqFL6vqz6PPQDDLMsidbC52GTtQs= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Adding enumeration of zicfilp and zicfiss extensions in hwprobe syscall. Signed-off-by: Deepak Gupta --- arch/riscv/include/uapi/asm/hwprobe.h | 2 ++ arch/riscv/kernel/sys_hwprobe.c | 2 ++ 2 files changed, 4 insertions(+) diff --git a/arch/riscv/include/uapi/asm/hwprobe.h b/arch/riscv/include/uapi/asm/hwprobe.h index 9f2a8e3ff204..4ffc6de1eed7 100644 --- a/arch/riscv/include/uapi/asm/hwprobe.h +++ b/arch/riscv/include/uapi/asm/hwprobe.h @@ -59,6 +59,8 @@ struct riscv_hwprobe { #define RISCV_HWPROBE_EXT_ZTSO (1ULL << 33) #define RISCV_HWPROBE_EXT_ZACAS (1ULL << 34) #define RISCV_HWPROBE_EXT_ZICOND (1ULL << 35) +#define RISCV_HWPROBE_EXT_ZICFILP (1ULL << 36) +#define RISCV_HWPROBE_EXT_ZICFISS (1ULL << 37) #define RISCV_HWPROBE_KEY_CPUPERF_0 5 #define RISCV_HWPROBE_MISALIGNED_UNKNOWN (0 << 0) #define RISCV_HWPROBE_MISALIGNED_EMULATED (1 << 0) diff --git a/arch/riscv/kernel/sys_hwprobe.c b/arch/riscv/kernel/sys_hwprobe.c index a7c56b41efd2..ddc7a9612a90 100644 --- a/arch/riscv/kernel/sys_hwprobe.c +++ b/arch/riscv/kernel/sys_hwprobe.c @@ -111,6 +111,8 @@ static void hwprobe_isa_ext0(struct riscv_hwprobe *pair, EXT_KEY(ZTSO); EXT_KEY(ZACAS); EXT_KEY(ZICOND); + EXT_KEY(ZICFILP); + EXT_KEY(ZICFISS); if (has_vector()) { EXT_KEY(ZVBB); From patchwork Wed Apr 3 23:35:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13616821 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 A5B65CD128A for ; Wed, 3 Apr 2024 23:42:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D580B8D0007; Wed, 3 Apr 2024 19:42:50 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CB6138D0001; Wed, 3 Apr 2024 19:42:50 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A6C538D0007; Wed, 3 Apr 2024 19:42:50 -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 801698D0001 for ; Wed, 3 Apr 2024 19:42:50 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 5032F401E3 for ; Wed, 3 Apr 2024 23:42:50 +0000 (UTC) X-FDA: 81969848100.20.0790709 Received: from mail-pf1-f171.google.com (mail-pf1-f171.google.com [209.85.210.171]) by imf18.hostedemail.com (Postfix) with ESMTP id 7E9571C0012 for ; Wed, 3 Apr 2024 23:42:48 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="TZOKS/dc"; spf=pass (imf18.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.171 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712187768; a=rsa-sha256; cv=none; b=xOd4tWkzLKo0wlmZcQJ24idGo1C9/8T2IAiXEdmqCJQvtjcaD7UgjZ4Jjiz1V/VBqEonNG wH0MS8xQuRAT9O8PhyMZo2VtdPLgKPPXghcoNZGfjGuGYxIsRklhzlob5lab0n2BE3aR37 CZiyuLxtKNbl72OJNedBLdzlb6jxnQo= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="TZOKS/dc"; spf=pass (imf18.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=1712187768; 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=9DtlDgd/GAT0EKcB2AjZTXvg/KwB3A8UKH6yER0NyfA=; b=KPFBKdMf2Eu3IHWqS2Ua5GdbA43unDiTP4em5N+FV95t7UfraGpszGHtFv7fwhEgJwOMXv I5//RYbs/z9m3709xL64zdvQyqg3gHbuTZYH4AMoWn5rkIpwuW6yikRtVEfCI0RWyXsMbg K13W4JRM9CQW9sM7ITZh/xwkGvJ2418= Received: by mail-pf1-f171.google.com with SMTP id d2e1a72fcca58-6e6c0098328so333599b3a.3 for ; Wed, 03 Apr 2024 16:42:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1712187767; x=1712792567; 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=9DtlDgd/GAT0EKcB2AjZTXvg/KwB3A8UKH6yER0NyfA=; b=TZOKS/dcfnWxrYdWSwqR9KXc+JDG9uh0e2AeFueOC2giWl63CtIJuHgpNXYmtBtiT1 5lX5+rJJd0aC3te8WmAVb4Pzk+8HNeDoVZ03RKPCktsun1W6ORGeqioCKo+U6q5lo6PX F8G/xGrAPRjaC7KgHNFnaFNIKBc1SHk3OnD+I0NzypTGGiDN4uNCYiMk53wVFzY4w1U7 RDIhJrwNofXibsn9FYEM/ANtem2J/o9gVIDK02IwleTk/ymSFgge+GwytAIcBK6mWsRc RYkG3Ie/cjJXPQM7Bi4jBEsJUYfFrGws2F3qhJ4sQUP2giZqO2/I0Fi+1zNaq2IDyQi6 N7OQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712187767; x=1712792567; 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=9DtlDgd/GAT0EKcB2AjZTXvg/KwB3A8UKH6yER0NyfA=; b=mWPiU4el7qpMJfnaE49MDLBHlrJ4xmfBYCHzHOLWhIioG8F0zMpPmxpLFhKV1nle7p K9w3Gfvv3nxDdRoP2oCBU71svJ7tGJ3uY2EzVB9s0sm5ihNCtwvEEGsmjYWyY3qH7c0M qSrNWd/0IijHYhmSItNA29FdIWq4byCWLDLHr/GnNQ+vka3yB3Y5ja5SO+Jif0q/oFG+ latemYkvMfRPdNfX8/QYWerwlF3fAVHYID8U7a6T/k6s5zPJaW6DFNgb17zaEoR5BLxG weHSAwu3ruBSAYEFiIoa/F94/HYM+Nw1tJ7ZvtZONQMY707/MkJauW0vaAXRY70KZSe0 eLIA== X-Forwarded-Encrypted: i=1; AJvYcCUo3wkPEcN/cMP76NiCfk8CLnjyVDpD53W2FanGxNVJmwJB1PV+9/xZBa7PaqJ7xBirMLkIOX3jPqcAIuHxTb5vYvc= X-Gm-Message-State: AOJu0YzcCgUi4DAyW6nBfC+Jz3WZCt39NI/0uNviZpg6XynKmewJegl2 O7TcCUjjQ6tcLQKTjBd8QuhABFcKJqK6leCHfx2QwPpbOurynUu4BBoRVSDdNRE= X-Google-Smtp-Source: AGHT+IGTlfFxuJFVldaZ8jYPJUINkZRKqV1i5a/wExLO9N1TqU40XTAEu7HYkbstJURl0N5AQEsIcg== X-Received: by 2002:a05:6a20:7285:b0:1a7:2437:3d58 with SMTP id o5-20020a056a20728500b001a724373d58mr1369671pzk.13.1712187767375; Wed, 03 Apr 2024 16:42:47 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id b18-20020a170902d51200b001deeac592absm13899117plg.180.2024.04.03.16.42.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 16:42:47 -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, conor@kernel.org Cc: 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, corbet@lwn.net, 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 v3 26/29] riscv: create a config for shadow stack and landing pad instr support Date: Wed, 3 Apr 2024 16:35:14 -0700 Message-ID: <20240403234054.2020347-27-debug@rivosinc.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240403234054.2020347-1-debug@rivosinc.com> References: <20240403234054.2020347-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 7E9571C0012 X-Stat-Signature: gng8pjyo3cns6469rrty77sfsmc84nqh X-Rspam-User: X-HE-Tag: 1712187768-408951 X-HE-Meta: U2FsdGVkX183ERw1qscDfO4ZGr+3GlrOnWT68PdR2mvyySzpLycAAoo6SH+6V6/2npqAs9GNKZo24TGP87k4mQK85YiGtQJHgP7ZtCjNlzLpjnsyEOD5X/vGDJm6diKr7KfzA6SvPlrLoGGL7i2GqZ9inenxipXN0BZUi42COcN24ZHNF6JgRBYGfxM/M33K9/FdNOtFcdBkRVtkItIdqlWZfiBXpVheHO7K7jCRawWbowmnm74qK9/gYenj/7pLNfU/xnpzApjwDVy0T+05uUx/EgSAo63nEQHZgCa5TO4BIzdnFboMVFZtKs4dBIfMlkZV8NEoVnXdQVtAeYlUiO9ZxtV5xd5Z4TK9YSGXFOxLBekTp+5C8fmCSOkzd4DB1b/ozcau8PKI/3gFu9UzaUCEQ9NHiCoF5PvizH/NZfBd0Rm0oStKMMzYmsGi6aB2pxG0JqKL4/LTtBKNwY7GWJf8vzGEUD29sKS/r9bIj+XYaW3ZZzMR9oPBxs3qTBHx5fNugOZnPlh5sEz334AQhuV6ws5GAV3kw+5q14rNK05mliZZfC2zWajs+bDMKinoPYn/j6kiZXGC5BBxZl/D2Od4vpfVJqj0wiiic10iTyGRbbznKsTGpRMxFQ+OYxmwv7P8Xb4BSKTCKEocZEo/Z6UGKAAJMjPTfuRJyTBBuPRSn/CHLpRmIDlT4iz9+HfWULG2P5tnHeX+HB/QXOXyDBZ3o6zgDZao0mkTxrO43tpGkFzZWenXcIBgib1qfIyg9taZ/27qKC2Vwy8wAeEyCUdHGuy46j9S7FWL5hbFk6B9oq8QcwXpuptUlI37wCLLR52Gvedviz5LUM7hadNE0i6KJHLXetn9pClv1FfZaQLT90qbFxVeQa3zfYhkrvSdK19g9O45rOLG47cgjpDAwJdZYGveKEY4P12sdwgJnBQjMgwhxbS5lIlRsyOc9KwfxQYPCL06xSa+unfE+cp 0gp5WH2Q dabSWbiJFvBF9cZl2uSivHvkB0S4eljfrBrefgL/bno4beOp1ozUiLJWibmXaBgy0h1o0VFJdt12Cmo8te5IbNuEsodhSBhEpG2J0IsFRDNSKWnP2viWhNwW9aTSNV+x7wXpnrR61bOObuub6fV0/9osR24gzzRv3iQ6uU7DLojCcwB0UsnytV/g7Jnd1azP2sRCbIA1YlXyHq0Faoht/c2JPAyE9f4Vsk89DD+WAARD/91yuVzudP0/H16daNS8zbGxYDpW5U/+UCifBRiudpDkFsd3Z0oWguVhdaynpAy5HIM9303Pxk6F12s2l74XMdDFV99t59I8FRctSwX76pbAL3kRQ16z28fCW9AfXQlVdLfbjXrQNgH78eY5/R+qU7/md0D7ksIYw0jZdvEaaaMP077sdAQghjMANdPfQmXOoipKFpstTt5CU1OfFqa8AM4g70Ru5pLychK6W7lI7RSCB4dL+s5/xlIzsz7cH8+GBSkqK2MrYfvxBWDoqdQaI5bvAz6TfAcj2bASppSMLz/PDTl/bVM235Mgn X-Bogosity: Ham, tests=bogofilter, spamicity=0.009741, 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 Wed Apr 3 23:35:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13616822 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 1FCA9CD1292 for ; Wed, 3 Apr 2024 23:42:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6C5798D0008; Wed, 3 Apr 2024 19:42:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 64FB78D0001; Wed, 3 Apr 2024 19:42:53 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 392BA8D0008; Wed, 3 Apr 2024 19:42:53 -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 1329E8D0001 for ; Wed, 3 Apr 2024 19:42:53 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id D3BC1160392 for ; Wed, 3 Apr 2024 23:42:52 +0000 (UTC) X-FDA: 81969848184.22.3E07543 Received: from mail-pf1-f176.google.com (mail-pf1-f176.google.com [209.85.210.176]) by imf29.hostedemail.com (Postfix) with ESMTP id 23912120020 for ; Wed, 3 Apr 2024 23:42:50 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=MCp+bsnZ; dmarc=none; spf=pass (imf29.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.176 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=1712187771; 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=uUhzNqFH615pxYtDXJqIw1KYQZW4Zl5Hbz1kz+jwqO0hO6ZE/lhlJI3CQMOZJvrj4vLOpj VohQ+VoKOrRgTqhmh2brVu0UsSyITzngDXp2y51o/39yg8JjN+ZljvCiHlspWFEgXJf5On UCyCgCdZa8+9L2TjDgJyFpkmR8lnxnI= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=MCp+bsnZ; dmarc=none; spf=pass (imf29.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.176 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712187771; a=rsa-sha256; cv=none; b=CdL3sWDnriX4o1RojkOR42/zv/xo3to09z/6VW8cMdKloZ62d6rAc4IqK0ptvI2jS9Dmzt r18losXFhNW0UPtPIXY6+84tz637WumPvUPhbRUKh3lkYSbOR5v3PiCVekPHoy6VniT63E Ee6LE3qX/VhACEgsZtDtlgjY6+Yqaic= Received: by mail-pf1-f176.google.com with SMTP id d2e1a72fcca58-6ece8991654so153640b3a.3 for ; Wed, 03 Apr 2024 16:42:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1712187770; x=1712792570; 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=MCp+bsnZfQZShyQ8EmY3ML3yLO/Ca7axCG9Xhm/iWt5DxwGw3GUG+VTPRAJjk39520 oF3RNoOXaHnnGgWmvkcHGwyxkG42Pj+wQ6W4lim7K453DbFS1UZ0eUX2aUWjuBh0j0yU 9XMU1KCBACCO5bUPp35CyrxYHNHUpMTFosmGj0p3pk+3eugv9Of8+LL0d5GKKKHL5QS8 Kbqn9hjgduC3wuYFMMbkTkm9gsboVKFk3umyoxO3KNeKRdXI6eUwF6YntjDucLRr72zj 4LxCv0wQxoqpVKLEk+cbbAW6KCCBijqTE/+AY5SaA7biRPbpM/VB4GcscuL95/LDgb0o mqiw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712187770; x=1712792570; 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=Fd/RYvsQsqqGoMz2OwFLNuXHJe8zjMLxmYKXrdvs7hpCvCETuSkxzB3EKGDCuv4hSm nJaBN0+JZ76LfCQsnOgChnyYWHbVAORAwRRHNFAKzavzWEHPomEy6p5pRoycGbW2s8Dy txFf4rbVk/3eEUl6kNMar1PYxZbofJzaz0F/eWM1sLX9rB0hFHwzAePwW0bUdl5rYUW8 U3lGNCT0cP3MWggWRsEk33P/7jbD7gbOZkvSvMM1twDReBHtSCA2yM6cewAoddCqGdsA UaKCHsCvz6rsgfILcj1+V4KWUuOTPYrem3g/oeg5BSeF8UbWi1nK8pdnSNPcphdi/rLB IttQ== X-Forwarded-Encrypted: i=1; AJvYcCVKAXKQNoSM1kY+xNxeycmJ3AuaGC5pZ7ZJkhy+YnUUZ63hKqMwraMB3qr50wdhK1r8ftTj6Tvsuw6ONLwW0QIe0q0= X-Gm-Message-State: AOJu0YyFenLyW+iS5DFUk1OucSpiCUgRGc+HwG0wNO7xEDK3eoh/bowv Br9vTV5pw9zFquJhjJvIIk4asapS9/QAWzey1f1llnt1yeUWMA6ApShSHakuvKQ= X-Google-Smtp-Source: AGHT+IFOcGirbQfLQaLb5+nO2ahsn6EhtiBDapL2BrJtt/8rvvrO0dXtCiXExOYOg70SFiumQnh5iA== X-Received: by 2002:a05:6a20:3d03:b0:1a3:3fbd:f859 with SMTP id y3-20020a056a203d0300b001a33fbdf859mr1489558pzi.2.1712187769990; Wed, 03 Apr 2024 16:42:49 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id b18-20020a170902d51200b001deeac592absm13899117plg.180.2024.04.03.16.42.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 16:42: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, conor@kernel.org Cc: 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, corbet@lwn.net, 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 v3 27/29] riscv: Documentation for landing pad / indirect branch tracking Date: Wed, 3 Apr 2024 16:35:15 -0700 Message-ID: <20240403234054.2020347-28-debug@rivosinc.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240403234054.2020347-1-debug@rivosinc.com> References: <20240403234054.2020347-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: qfwsm391m9ptsdy64tfiqby1bxxzbr8n X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 23912120020 X-HE-Tag: 1712187770-262865 X-HE-Meta: U2FsdGVkX189KFlaDZ5q+31enPfz2V4duT6WhMUa5JF9uWl9vpsC5eE1t9WfLDlbHITPLBvybK7rdchRbgGFHX6Y0OTxpPQ+LbPlA3ne2v/C00iouQkLeRFUmRkkG6GoO2i59dTumwL6yLTZslHdUcunQetn9B+hjWD7/aLeS7N6zZRXi6shixfvKBhITfRw4uhGeQc5p7NhQZ5TxC4M9G9KrtSz2d/cz9YD1+1CcMcrh88fG70fNl4TBV3ZaakxSdQlp7FZ0QVSeHCxpU+fsZXchOC0dIOzWUsYxcQExnEQyO6q2WkHK/hIdYBbYu2qFZPCLfuD9XizS2Xq0jUqIMorbBonNeW3thVz8FJFC8g9d6rg7GO9I0FsOiDGwXbYWNU2XLVx2kDCVCf8cvjaZ04QNY5jgyKqqKwnebgj3rFlrGYgoKapkVBPWKEv/3v7QiG8p1kzZY575/Nq9T3l1hYqTy6GiN909h1Ehx38stf7cyfmVBpG2vWTB3CQ0kI/+8IbaB1EpG4xcdjmQfJjg7sV4GohjWTmoQ4jDyRJepb2y7tr2xcTRZ9YqvFcVWER7OIneVtJFudjfQ7O4o+bg7oC1q1VnBECrJYnQKrG8bBOLH2xZnnfmrHby2ew0KYg7PP/CuSN8PThvEUjeU4x1tmcDjO9yqgQjl9J7c6LQxmB9+0XYXHl3GzOq6cNm4EPdFcfw6coPzyrpPKpXZAaUlw/G5V5cPvNMTQ0DCCXeOig4LIiMFkriYgOjOgmn7PgLDKO0In3IOXY6qYQZuvCl8NmTQ2sxcZdGfKbHBfhGBGSkIF3tEcjktTA4ar2diqRFgN1qYOWugpGkgjoKBbqpUtKBS+oMx7O/oWUvNRG7gmbRMYFqiyCPbb/acfomccVEbUO5SBQnSTnQJlkSFfTqYWtbp8NZ5UWPaL1MJF14V1RrmyXO6bJv5BkfY+Sv1fcpraDnoHqDEyVf/NX9A5 CVjErvRV a//PH2VP3iZ8GUC/sVJULl7tIRMSfdCdwbSmWxzUACWFSpRRkL3bB/5vun0hUjvBhk9cAvxhDL6/QG7YDpeJQ3vAhRch/A0O1i4iorWP8NiTkYtJKuDxdBgQwZHMpum75fqlmENlbK402PNMVQkkXdrqxOKKPnD3VJARwGIoS33r88UFafWmkuhWaSRG7zV1khVrygk96SgnLMB+NDJdrVKIYbqd6tgbE67PTvDx7uCncz6U= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000002, 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 Wed Apr 3 23:35:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13616823 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 04612CD128A for ; Wed, 3 Apr 2024 23:42:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7B1318D0009; Wed, 3 Apr 2024 19:42:56 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 73A878D0001; Wed, 3 Apr 2024 19:42:56 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 42B8D8D0009; Wed, 3 Apr 2024 19:42:56 -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 1FBB08D0001 for ; Wed, 3 Apr 2024 19:42:56 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id E5D46160139 for ; Wed, 3 Apr 2024 23:42:55 +0000 (UTC) X-FDA: 81969848310.24.B58F2ED Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) by imf07.hostedemail.com (Postfix) with ESMTP id 37EB740006 for ; Wed, 3 Apr 2024 23:42:54 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=TbOqfKyR; spf=pass (imf07.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.182 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1712187774; 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=Sb6BP+LZRDglssaRxU/XTQ+JD51IxZf+/auP6KhCKqPPWwZpX/gQuUscvTqyDKVSC1rdJQ BHl3iGzvUZcNaFwVhuPBnbHWNRwtme8V/HYaDFsLJv+b71Rx6/mrNJ1/ojYfQGmFpoJlbr qeQWHgQ3cAzmqVOSJKboYeMLAQ/62WU= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=TbOqfKyR; spf=pass (imf07.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.182 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712187774; a=rsa-sha256; cv=none; b=yqzQit2gHTy0hgH1TaaxWhyA8BZcyWMRVhZ2Co7gPVIFUPRBJf4+DcgcZCc//L90pzwH6t Dx0axwhAY87cNNqCNTRAkIO37gRYsxVeUzoAUq3t/o9b7KROq8UVtUnIppAk/uEvg1T1ST GPxCWP6NoPPHhyLLXFFFPL0PVzWUEtA= Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-1e0d82c529fso3646845ad.2 for ; Wed, 03 Apr 2024 16:42:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1712187773; x=1712792573; 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=TbOqfKyR4ozyJqpvVXVlRI4ypbpiYLyCFyXmUnIxltbIdO5ZvzXPcjwLu0ZzQ4R2jJ tyhM4wR9eZdsSivWfzwAa952gYee4DDugN4BIWgEQulhUNWjc/ueMA5+V0/B7XU7gY2U hwRbgyInCqLWjbO+cnT60OwtHKTucwpJONU0S46Kv/Xbp+F4enOyDkpmDontav2htQ1K QlC3JsHR92AqkXE2z1JfJM2DU5XWZlHqC8kmTjDSXIj9lnVB72xvH9DpeUIndigi+VWA dRarK5GidG1lSQ1gus7HoaMMMSl3faR5PQH9q/mwuwpyRPcn9TW7N5MXZ1kKW8nyW5YW F6Bg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712187773; x=1712792573; 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=xEr3xgM9HswJUcDdjcACG/WiJkd6IkNoACDIpwhY/HFoeV6GOE0QckZiM9WK4lwNIi wrwXcIi2ED5c4cWahrGDtjWgU5ykH/3usP9rPfKFCk9fXD9rFZrlz2/9HQnUENpWn8Mi EEx3zlszojkmdwenmbpbfF+KpDDxE9UDL0jpIwWaaebeYnInv4EVAXQL3SSI8E9ZyppQ aF091oq6Gg0m1hyUZaJWk3++a4qY3ZVa1WArmaUZrIbC0W13pjwHlBO5UD7o7UZ7aGev U0bjnEzkLSjcSZVcjMiySFKsBX4NeBvhyz6Joarkk9i3fYV7sQcsrWiJSApuElh32FCz jsSg== X-Forwarded-Encrypted: i=1; AJvYcCV8yKdyX3zSiDjuiQe2ICh2s9VhWBqe1g33hN7OxLGXq1CYYkXnakeoS6HgpmS25N1yN79Y0mBkmUEFRWZ7F3Lj6Gg= X-Gm-Message-State: AOJu0YzxNdL9VNaf/E+NUv7E5WmsuWvFiqACvLmYD3nmsY1Ck7wINXhD jRFZtcGCVbe6sZ9JrmJkDwXm7URdYqx0L5j0cgdMgulqeRolIHJglBDgFWpbzSU= X-Google-Smtp-Source: AGHT+IGyHrC5sNUT9pcK2iklG5WODUuUgmhPv4JAEYAUc1x1R1udLgCfkk9z11+70BxIHUT0LuGezw== X-Received: by 2002:a17:902:b788:b0:1e0:4dfd:c121 with SMTP id e8-20020a170902b78800b001e04dfdc121mr665961pls.68.1712187772970; Wed, 03 Apr 2024 16:42:52 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id b18-20020a170902d51200b001deeac592absm13899117plg.180.2024.04.03.16.42.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 16:42:52 -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, conor@kernel.org Cc: 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, corbet@lwn.net, 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 v3 28/29] riscv: Documentation for shadow stack on riscv Date: Wed, 3 Apr 2024 16:35:16 -0700 Message-ID: <20240403234054.2020347-29-debug@rivosinc.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240403234054.2020347-1-debug@rivosinc.com> References: <20240403234054.2020347-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 37EB740006 X-Rspam-User: X-Stat-Signature: k6nrnpei93kkysgq9jzd6qogy86ozdb4 X-Rspamd-Server: rspam01 X-HE-Tag: 1712187774-382100 X-HE-Meta: U2FsdGVkX19Np6/2695hWRbYTahR8+Tw7vchC3rqwIk2qiJxPJ07GCeYzj6Qn20f5gseRwGmvyrlZ12oeSiZMxs+us77/DL8uCT7ke39hVQ6NmHFplAI4GciZsOch1COB+3CHbf50cr13A+oB2RbmfURHftFXp3B5PkSbzFPDpYdNYlxFpwvoDWJwnrOPnoPkwmk1T71UDOPJARD16iGtJnw0mM5MDSdXjashQAv0hfxP+xHGQAMkyt912WrPVXYrW+wCIf3je4RkIKH+18kBNa5HrvnCeU1sTa22FuOmMqCkJAkDd8DxaP4Uu3xlGf1Jh09VNVRzDpmZnKUQjw+jyOiho0Q+/T+tu9H9aNsnVvtmoTImBj1V2fuz9KkqqZE1NLNGcRs64+/QxvLD20ZZoSdNU8leAVBzuinoMz2nA04hkLAPDWRD83FHqGsXT9pIVgqrxT5uS+85KtJBsP67xiOvCbRMgBUPIyS3v6seTRZVNsdbOTAIsVcgc1RSKnL8fZE+bvrct7izE+UE3A6VczWEBroMvz5Qpy1+AGQ3U/aF9segTTOLWlTe8kFT6jR5k4vqNeMdGpy+uQVG/VNJtY5VgrY1Ghm7Gdv0zVYowiztjpBCK+ZO7CzEKg7oA3MgsQ15dHei0N3/WgqrE/Le05ShqpL+ZkXqwb3kf0pEoVU2GKfEEn/nKnTRtZL1Mz0GQwhSd4aJkkhWMfm+mMKnyk4CuAXrhrqsRkAMommyLhcbGyp/bI/SYBf4+8qNvMIJvXAIo0Kxd2FhUyTiKTQRrMy1ln6wa/AVC6ciRbC9CG40qB8YZXgo+A+UF8tbdzkMsKll7oy8cL4gB9VQnFZKHq45IVkTQzwTdX1tMamZnolMeLuRipz8f9/b/kjeRxn94BNOEbFYhg/h1pmX/0jl632BX0q/mcCbB1T6DQtFA4gTDJBI+oX+3eTP5jkaEafOl6jYh7lH0JRJCEqr7w 7WonvCFo pwlkfCURgOKKct63hqIST6eT7MYwY5rn6CFEn2V1C9YS7s1e+SFiMVRItEIN51tlZ8U+jcG9jTBdn6HG3VeYK4OiZv0IXkVKPA5XvnQDfs/Sgs51dF3U3FcTll1EfO+IoIEdY2XRcmbYa/MQHFU5FXP7/KDnbwQOJN9fYCxWwuOa0wAcwj+vrunAqgESDFaCi9RIXVeWDHCtlc5VWwy3snUDab2tAp96NUsTryetQhQlHyhKeNlmF7Wl/Dw== 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 Wed Apr 3 23:35:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13616824 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 A313DCD1292 for ; Wed, 3 Apr 2024 23:42:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 123058D000A; Wed, 3 Apr 2024 19:42:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0A9608D0001; Wed, 3 Apr 2024 19:42:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D7A788D000A; Wed, 3 Apr 2024 19:42:58 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id AE82D8D0001 for ; Wed, 3 Apr 2024 19:42:58 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 7BB7F1604FC for ; Wed, 3 Apr 2024 23:42:58 +0000 (UTC) X-FDA: 81969848436.20.CD4558F Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.178]) by imf28.hostedemail.com (Postfix) with ESMTP id BD2BFC000E for ; Wed, 3 Apr 2024 23:42:56 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=dj4Tc2r5; dmarc=none; spf=pass (imf28.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.178 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1712187776; 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=jCZt7vKeX3h2VzHWMgW6/9bkQAkx6Q0eYEezt2dQjzs=; b=AKoJBQSvTLq2MOGnngfIsH8/rkLVMMShRZi935UxW5dLd3dMZ/0wQopy4h3eS4j2yLyxr/ Ql19FIQKCJlYKJ5c4ufPR6Zp7tpfPcWSFoOYh1DOZJsCjiUSXGgnS07y2kouawbsMZV3vG DvAK4MRwvIaA75xmFXrkotLFt6IzDL0= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=dj4Tc2r5; dmarc=none; spf=pass (imf28.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.178 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712187776; a=rsa-sha256; cv=none; b=4JgM7OUCxF7vgflXUcOIS+s0oPy1UDQXemNeUWWCeYqSfbLcSxg1RSm62jEE5kkiVUYQuC JsD2UgwAtW+ehWqGxcVBg0IIF9wFsMHXW0kC4FVuHeYFov2WgKuXYrLdv3WwOOiLRDqwth aGReNpu/FPwG/QHqos4ua8BjfDjX7oU= Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-6ecec796323so63611b3a.3 for ; Wed, 03 Apr 2024 16:42:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1712187776; x=1712792576; 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=jCZt7vKeX3h2VzHWMgW6/9bkQAkx6Q0eYEezt2dQjzs=; b=dj4Tc2r5ZLLFDTpq7IJ1VgwXOiEvBExeJoODpt5NSbIZHhkftQB1qbVMVtlcA6Wjfv Op2x5ElFcSf1t0x7ZRZE09GxF7O71X6JQn4CyxaZpoJtIqlSCAkJBrV6HKG5zs41QhJD nFmEfPUe8uN4zcwx3cre63lX+RVgw/30vCW6eTbKOdkFyMl91vFlFDG+gRluDZULN1l4 T59SUEKuSgSzIoLF2xuklap1JbQkEHQEyrRnMSvk24AiJLG+lKiOJW0aeNaRdinSKW74 fzw0mbT8fSQaUGzz1MPrM10TiUTcY2afaK5GTQ9HT7RLQcL3wWTZ5J0gRqXPvtC4cn5s A4kg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712187776; x=1712792576; 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=jCZt7vKeX3h2VzHWMgW6/9bkQAkx6Q0eYEezt2dQjzs=; b=PygzeiTLBEbtbPF67YcOyGAbWteVwdzHKPk7geFgg9CEKdYcHHHYtT/jBK+P/pj1Tb ufFZWv0budjqsuEJ80uCkNnsNZ4bqvEv9CKRdu53BjGHEZfUkC9KXq31+vnmw+c3rnM3 YPmFSGVzPXOdc60aqbwceuCBaIMYxD3TScRDAYi0W/hY68rXjROkww19C4WT5EIAy9l8 rPlOCwIbgJC5grFM6ey1UkI85Bpn34dSvfsk9MD1cYkM7SliMe3Ctevr/y1UuPAzf8bO jJvVUsKNoz87gkCKiP4SCTfDyZVhxHK5uNAmpMwWK6inyXTuygiRlUPv0sYwaWWuJIqd WZ+A== X-Forwarded-Encrypted: i=1; AJvYcCV5XKxWgrAi4K6W7cXIOICvVEyvew+XgpD5dxVmQokBHS1MOr/2XhqInEBHAhoYAaz4DCpdS6QaD3NZx1Sd5AYx8og= X-Gm-Message-State: AOJu0YwzGG9ZX+eRog/QrZWE3D5C3f2AvNXhEXv4dXxrLr1ZCQoO9QrE sYt9c6FXg6gFe9/nElmVhhJ/zE+r/xXcfQv35tnBb89iOyHohdwvjW2fGjAS1yY= X-Google-Smtp-Source: AGHT+IENevriXhB3ugnMlKFADV1UHn7XO56vSIJvTBybhvmCCkxbsJiZ6HAZpUe0I08ROmsL/aj3bg== X-Received: by 2002:a05:6a21:3405:b0:1a3:a039:d11b with SMTP id yn5-20020a056a21340500b001a3a039d11bmr1282351pzb.24.1712187775532; Wed, 03 Apr 2024 16:42:55 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id b18-20020a170902d51200b001deeac592absm13899117plg.180.2024.04.03.16.42.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 16:42:55 -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, conor@kernel.org Cc: 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, corbet@lwn.net, 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 v3 29/29] kselftest/riscv: kselftest for user mode cfi Date: Wed, 3 Apr 2024 16:35:17 -0700 Message-ID: <20240403234054.2020347-30-debug@rivosinc.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240403234054.2020347-1-debug@rivosinc.com> References: <20240403234054.2020347-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: BD2BFC000E X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: 5jotb48uqjdgxud8o3jgmomioaj66dru X-HE-Tag: 1712187776-804418 X-HE-Meta: U2FsdGVkX1+U/ThIB+D2BMbYmuBTzEUOfF1al7qWRJoGz6Kp4R0nG/T1nOQF8qu2fVoIn9/fjx4snQKTyrbcfsiIiRvsky8tBbfGwak4aFfId+9f7k3zXzUGhG2kIyaV6Jwxix4DjAkjuNOzkEv7wgQu7VL6Y4EU1wj1wjaU5TpUiKMWwPpu83oCDUzah9jJ0Ybvv6w5AGTaMEsLFF26PxorZTM9IUnTtyNLCcZPzDdNsGK5P4XXXCb0B/v8mpC6hLogVOp1cjo3nhuz/Sr7TFarTUlyPTqk0VaGIUJpsLTp6RHMMVQ+6t1S8FZ1gr3RE600bvmPNlvwUHcHacIV/F2Fd1IRP05GweUoWwmqNgjrSdR2FryENDziqGIbPjOJP6DEWnGwYk+jb/zImaQ7h9SWt4J01HbtRI0OPhQb6gDxdeQHSU5xKxDdPi+UAQ+XQ3bkpzu8Cwy6wuO48C5pdX9ohjt1KcpkwJqSY+kbj8+XUxfIAvZH/dooe0D3v8NrWb1FqxQcNJgrbC63M8CTLNIOlO/GGUei6KIjkNK87SNV0aAECotcgK11lNiJc1ZsYgAiA56Hq9m7yL26O59w8w278BZvro2ea8sJHfXRql6NCgigS30YnOpgLbK8yeeC1Whn1W77Jri8virwpGgL3cCLogm3Ww+fWy9TYJgMq8HzGK91u1G5rxMfpw0xAnZrB+GcUFHhd6WpKGv/BWWwOqKmZgtQpNl+9UJjZdcQ73xwSCMYCcWSVAszN/7NfqWLzVXfJqCtknfLMYpDpoNNZpNXIlzzCxxVLAkFncDdIgrZ6vRTBbk3FOJ9N2d0AXgIhyIrUXF0qru+kwYWq7qJTeZl2kIEHz5dR4CvncLkkzG1jxj3ryeW2K9hzT4AbT+mno5z7+zBSP/pKXYKqPRWJglA2V2uKyiRbUXASpwrGdl7SlfSj8394r/8427wqGjUM8AKPZMb2BTVJcJi4I5 mnVWRmns Of3z8ax4MKic5vMBGvyYWcO9VAy4+C7rLVpaEevAzuejrv+mkpuUB/29F0ePkG1S+V3L5QliwRkfl6iymfMnHyvYygfUmaU9I3lfWEQQQtgwse90L/NwDAAxKU9mj8bk7NnHXkl2KoZOvvGW3eWi0NtQvZbkBH/AAGlFGVF+8lEgmam8x99WUPaQBC9RaVjaRuB5JYxhDPST06KtLBgUsMXcKulsyKBLIUNvre3GxLpSRMKiEee8G7Ioewg== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Adds kselftest for RISC-V control flow integrity implementation for user mode. There is not a lot going on in kernel for enabling landing pad for user mode. cfi selftest are intended to be compiled with zicfilp and zicfiss enabled compiler. Thus kselftest simply checks if landing pad and shadow stack for the binary and process are enabled or not. selftest then register a signal handler for SIGSEGV. Any control flow violation are reported as SIGSEGV with si_code = SEGV_CPERR. Test will fail on recieving any SEGV_CPERR. Shadow stack part has more changes in kernel and thus there are separate tests for that - Exercise `map_shadow_stack` syscall - `fork` test to make sure COW works for shadow stack pages - gup tests 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/.gitignore | 3 + tools/testing/selftests/riscv/cfi/Makefile | 10 + .../testing/selftests/riscv/cfi/cfi_rv_test.h | 83 ++++ .../selftests/riscv/cfi/riscv_cfi_test.c | 82 ++++ .../testing/selftests/riscv/cfi/shadowstack.c | 362 ++++++++++++++++++ .../testing/selftests/riscv/cfi/shadowstack.h | 37 ++ 7 files changed, 578 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/riscv/cfi/.gitignore 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/.gitignore b/tools/testing/selftests/riscv/cfi/.gitignore new file mode 100644 index 000000000000..ce7623f9da28 --- /dev/null +++ b/tools/testing/selftests/riscv/cfi/.gitignore @@ -0,0 +1,3 @@ +cfitests +riscv_cfi_test +shadowstack \ No newline at end of file diff --git a/tools/testing/selftests/riscv/cfi/Makefile b/tools/testing/selftests/riscv/cfi/Makefile new file mode 100644 index 000000000000..b65f7ff38a32 --- /dev/null +++ b/tools/testing/selftests/riscv/cfi/Makefile @@ -0,0 +1,10 @@ +CFLAGS += -I$(top_srcdir)/tools/include + +CFLAGS += -march=rv64gc_zicfilp_zicfiss + +TEST_GEN_PROGS := cfitests + +include ../../lib.mk + +$(OUTPUT)/cfitests: riscv_cfi_test.c shadowstack.c + $(CC) -o$@ $(CFLAGS) $(LDFLAGS) $^ diff --git a/tools/testing/selftests/riscv/cfi/cfi_rv_test.h b/tools/testing/selftests/riscv/cfi/cfi_rv_test.h new file mode 100644 index 000000000000..fa1cf7183672 --- /dev/null +++ b/tools/testing/selftests/riscv/cfi/cfi_rv_test.h @@ -0,0 +1,83 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#ifndef SELFTEST_RISCV_CFI_H +#define SELFTEST_RISCV_CFI_H +#include +#include +#include "shadowstack.h" + +#define RISCV_CFI_SELFTEST_COUNT RISCV_SHADOW_STACK_TESTS + +#define CHILD_EXIT_CODE_SSWRITE 10 +#define CHILD_EXIT_CODE_SIG_TEST 11 + +#define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ +({ \ + register long _num __asm__ ("a7") = (num); \ + register long _arg1 __asm__ ("a0") = (long)(arg1); \ + register long _arg2 __asm__ ("a1") = (long)(arg2); \ + register long _arg3 __asm__ ("a2") = (long)(arg3); \ + register long _arg4 __asm__ ("a3") = (long)(arg4); \ + register long _arg5 __asm__ ("a4") = (long)(arg5); \ + \ + __asm__ volatile ( \ + "ecall\n" \ + : "+r"(_arg1) \ + : "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \ + "r"(_num) \ + : "memory", "cc" \ + ); \ + _arg1; \ +}) + +#define my_syscall3(num, arg1, arg2, arg3) \ +({ \ + register long _num __asm__ ("a7") = (num); \ + register long _arg1 __asm__ ("a0") = (long)(arg1); \ + register long _arg2 __asm__ ("a1") = (long)(arg2); \ + register long _arg3 __asm__ ("a2") = (long)(arg3); \ + \ + __asm__ volatile ( \ + "ecall\n" \ + : "+r"(_arg1) \ + : "r"(_arg2), "r"(_arg3), \ + "r"(_num) \ + : "memory", "cc" \ + ); \ + _arg1; \ +}) + +#ifndef __NR_prctl +#define __NR_prctl 167 +#endif + +#ifndef __NR_map_shadow_stack +#define __NR_map_shadow_stack 453 +#endif + +#define CSR_SSP 0x011 + +#ifdef __ASSEMBLY__ +#define __ASM_STR(x) x +#else +#define __ASM_STR(x) #x +#endif + +#define csr_read(csr) \ +({ \ + register unsigned long __v; \ + __asm__ __volatile__ ("csrr %0, " __ASM_STR(csr) \ + : "=r" (__v) : \ + : "memory"); \ + __v; \ +}) + +#define csr_write(csr, val) \ +({ \ + unsigned long __v = (unsigned long) (val); \ + __asm__ __volatile__ ("csrw " __ASM_STR(csr) ", %0" \ + : : "rK" (__v) \ + : "memory"); \ +}) + +#endif diff --git a/tools/testing/selftests/riscv/cfi/riscv_cfi_test.c b/tools/testing/selftests/riscv/cfi/riscv_cfi_test.c new file mode 100644 index 000000000000..f22b3f0f24de --- /dev/null +++ b/tools/testing/selftests/riscv/cfi/riscv_cfi_test.c @@ -0,0 +1,82 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include "../../kselftest.h" +#include +#include +#include +#include "cfi_rv_test.h" + +/* do not optimize cfi related test functions */ +#pragma GCC push_options +#pragma GCC optimize("O0") + +void sigsegv_handler(int signum, siginfo_t *si, void *uc) +{ + struct ucontext *ctx = (struct ucontext *) uc; + + if (si->si_code == SEGV_CPERR) { + printf("Control flow violation happened somewhere\n"); + printf("pc where violation happened %lx\n", ctx->uc_mcontext.gregs[0]); + exit(-1); + } + + printf("In sigsegv handler\n"); + /* all other cases are expected to be of shadow stack write case */ + exit(CHILD_EXIT_CODE_SSWRITE); +} + +bool register_signal_handler(void) +{ + struct sigaction sa = {}; + + sa.sa_sigaction = sigsegv_handler; + sa.sa_flags = SA_SIGINFO; + if (sigaction(SIGSEGV, &sa, NULL)) { + 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, ss_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); + + if (!(lpad_status & PR_INDIR_BR_LP_ENABLE)) + ksft_exit_skip("landing pad is not enabled, should be enabled via glibc\n"); + + ret = my_syscall5(__NR_prctl, PR_GET_SHADOW_STACK_STATUS, &ss_status, 0, 0, 0); + if (ret) + ksft_exit_skip("Get shadow stack failed with %d\n", ret); + + if (!(ss_status & PR_SHADOW_STACK_ENABLE)) + ksft_exit_skip("shadow stack is not enabled, should be enabled via glibc\n"); + + if (!register_signal_handler()) + ksft_exit_skip("registering signal handler for SIGSEGV failed\n"); + + ksft_print_msg("landing pad and shadow stack are 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..2f65eb970c44 --- /dev/null +++ b/tools/testing/selftests/riscv/cfi/shadowstack.c @@ -0,0 +1,362 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include "../../kselftest.h" +#include +#include +#include +#include +#include +#include "shadowstack.h" +#include "cfi_rv_test.h" + +/* do not optimize shadow stack related test functions */ +#pragma GCC push_options +#pragma GCC optimize("O0") + +void zar(void) +{ + unsigned long ssp = 0; + + ssp = csr_read(CSR_SSP); + 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; +} + +/* forks a thread, and ensure shadow stacks fork out */ +bool shadow_stack_fork_test(unsigned long test_num, void *ctx) +{ + int pid = 0, child_status = 0, parent_pid = 0, ret = 0; + unsigned long ss_status = 0; + + printf("exercising shadow stack fork test\n"); + + ret = my_syscall5(__NR_prctl, PR_GET_SHADOW_STACK_STATUS, &ss_status, 0, 0, 0); + if (ret) { + printf("shadow stack get status prctl failed with errorcode %d\n", ret); + return false; + } + + if (!(ss_status & PR_SHADOW_STACK_ENABLE)) + ksft_exit_skip("shadow stack is not enabled, should be enabled via glibc\n"); + + parent_pid = getpid(); + pid = fork(); + + if (pid) { + 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; + } + + 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) +{ + struct sigaction sa = {}; + + sa.sa_handler = sigusr1_handler; + sa.sa_flags = 0; + sigemptyset(&sa.sa_mask); + if (sigaction(SIGUSR1, &sa, NULL)) { + printf("registering signal handler for SIGUSR1 failed\n"); + return false; + } + + return true; +} +/* + * shadow stack signal test. shadow stack must be enabled. + * register a signal, fork another thread which is waiting + * on signal. Send a signal from parent to child, verify + * that signal was received by child. If not test fails + */ +bool shadow_stack_signal_test(unsigned long test_num, void *ctx) +{ + int pid = 0, child_status = 0, ret = 0; + unsigned long ss_status = 0; + + ret = my_syscall5(__NR_prctl, PR_GET_SHADOW_STACK_STATUS, &ss_status, 0, 0, 0); + if (ret) { + printf("shadow stack get status prctl failed with errorcode %d\n", ret); + return false; + } + + if (!(ss_status & PR_SHADOW_STACK_ENABLE)) + ksft_exit_skip("shadow stack is not enabled, should be enabled via glibc\n"); + + /* this should be caught by signal handler and do an exit */ + if (!sigusr1_signal_test()) { + printf("registering sigusr1 handler failed\n"); + exit(-1); + } + + pid = fork(); + + if (pid == -1) { + printf("signal test: fork failed\n"); + goto out; + } + + if (pid == 0) { + while (!break_loop) + sleep(1); + + exit(11); + /* child shouldn't go beyond here */ + } + + /* send SIGUSR1 to child */ + kill(pid, SIGUSR1); + wait(&child_status); + +out: + + return (WIFEXITED(child_status) && + WEXITSTATUS(child_status) == 11); +} + +int execute_shadow_stack_tests(void) +{ + int ret = 0; + unsigned long test_count = 0; + unsigned long shstk_status = 0; + + 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..b43e74136a26 --- /dev/null +++ b/tools/testing/selftests/riscv/cfi/shadowstack.h @@ -0,0 +1,37 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#ifndef SELFTEST_SHADOWSTACK_TEST_H +#define SELFTEST_SHADOWSTACK_TEST_H +#include +#include + +/* + * a cfi test returns true for success or false for fail + * takes a number for test number to index into array and void pointer. + */ +typedef bool (*shstk_test_func)(unsigned long test_num, void *); + +struct shadow_stack_tests { + char *name; + shstk_test_func t_func; +}; + +bool shadow_stack_fork_test(unsigned long test_num, void *ctx); +bool shadow_stack_map_test(unsigned long test_num, void *ctx); +bool shadow_stack_protection_test(unsigned long test_num, void *ctx); +bool shadow_stack_gup_tests(unsigned long test_num, void *ctx); +bool shadow_stack_signal_test(unsigned long test_num, void *ctx); + +static struct shadow_stack_tests shstk_tests[] = { + { "shstk fork test\n", shadow_stack_fork_test }, + { "map shadow stack syscall\n", shadow_stack_map_test }, + { "shadow stack gup tests\n", shadow_stack_gup_tests }, + { "shadow stack signal tests\n", shadow_stack_signal_test}, + { "memory protections of shadow stack memory\n", shadow_stack_protection_test } +}; + +#define RISCV_SHADOW_STACK_TESTS ARRAY_SIZE(shstk_tests) + +int execute_shadow_stack_tests(void); + +#endif