From patchwork Thu Jan 25 06:21:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13530071 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 936C3C47258 for ; Thu, 25 Jan 2024 06:28:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 28DD26B007D; Thu, 25 Jan 2024 01:28:19 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 216AA6B007E; Thu, 25 Jan 2024 01:28:19 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 06A2C6B0080; Thu, 25 Jan 2024 01:28:19 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id E62F26B007D for ; Thu, 25 Jan 2024 01:28:18 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 7A8B11407C3 for ; Thu, 25 Jan 2024 06:28:18 +0000 (UTC) X-FDA: 81716853876.10.67982F5 Received: from mail-oo1-f43.google.com (mail-oo1-f43.google.com [209.85.161.43]) by imf24.hostedemail.com (Postfix) with ESMTP id B492118000F for ; Thu, 25 Jan 2024 06:28:16 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=QQSAXrN5; dmarc=none; spf=pass (imf24.hostedemail.com: domain of debug@rivosinc.com designates 209.85.161.43 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706164096; 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=3e2cTh56cG2GGWTE4NSzcFXiqv/NB8pwrDQ1nq4c+Tw=; b=W7NsVnvRPyTrPthoh9iP39/a37tD+kIKpKrk4t0ZwXP4lN/6vB9Ez/aPLY5eilgXzm+II+ D9HqTudRzqC0gf6rPFT3Ufw2i8tXOr4BwEbZseTZGVz/hOrMJREmtq3hH8M9UsRY651ooB hKbkOYeh4F4O7EiWTS+Kz9frVUoFDq8= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=QQSAXrN5; dmarc=none; spf=pass (imf24.hostedemail.com: domain of debug@rivosinc.com designates 209.85.161.43 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706164096; a=rsa-sha256; cv=none; b=q8iFNTcsVSlvexeTJcroBCgHV+tizAeo6LfHLaV8sEM0dynPIcv/y+BR9tIDvGtjWEe1S+ 3D64Zt1CvngxzFcoBQHLNK/oMgI9oLFfWclquCT+GIYxVo5xGmDuCfF4jdlVVbVguRjOUw jpQ5ovTghpu3ukZVIQE45X6+YOjkggs= Received: by mail-oo1-f43.google.com with SMTP id 006d021491bc7-598a5448ef5so3898005eaf.0 for ; Wed, 24 Jan 2024 22:28:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1706164095; x=1706768895; 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=3e2cTh56cG2GGWTE4NSzcFXiqv/NB8pwrDQ1nq4c+Tw=; b=QQSAXrN57kAQkWx+v1rEu1MXPkZ3MQryFMMpd8jjFXQKgoHSUr+iTQaQytmX/erTFH vH2cf2VilQKXUpr5k1GXdwBoijVG3WLEZ2wUINkgvBOAggXXygY9ww1Rg84L9NBt2/eQ rjbHptf5h5ddQiUY1uqUcgIa/mFFLvHzC9qfL4roWRAbmKLBUwm0F6vIy4KBRhXH23RE jarbll88c47wU7PmdGa8eKFy/Pawsw1AZT0ZRqhgDd0KoTWMjbqrJ5KifX2SeDKOpsbA SGnEcherE9XjdkwMVx4nmbJJtEVDs0HkJy8hdKY68ENOjdidKuqsVbCTagpIunt2Qrbd ooHQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706164095; x=1706768895; 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=3e2cTh56cG2GGWTE4NSzcFXiqv/NB8pwrDQ1nq4c+Tw=; b=Aj8k4V7DbsddVOGSMVNv5ehi1L/wjmqLw0bleBYvnWVvjSE5Ropd8REKSoufAzc4al VR8LDpI+J2Ebf+qycc69bDTLUloS9BSaQTnMZMMgVpH9KQlYkrYKes/gcI7ENIDqpneR 8rx+SfceuWlDRBWVltuBA7fzOYTetfn5ludtRUXzKYQE9bAMGd3aAlDxune3ZH5JfF7E Hf8v5XzPd0UhLtlwpdwzeiFaskS9kDU3ihk8C1eg1PZ8vvx9a5IkShYDo1JY3yEgETEF 6r8WEQMPPwUJwPL+LE5jZDMh+WY4RBRczrKUvPRxNJd9o75+BB9yU1TiLqXfzWtxLP3E r4jA== X-Gm-Message-State: AOJu0YzyaGTEuyBrYoeoLqeSSIcsj/mPrwCRDX9eFKij1wNh25jJospb ixb/gGbAI8y7HFBp1fFOM4Whc5A33cn1ouu8nq5jAQIk4WioJpg91TnOloFwAgE= X-Google-Smtp-Source: AGHT+IHCIEsJoIeVNjGK2Jx0Div0w47akcVOGWIMmQ12q7OuC1Yo4rjhD3STlwvv6dFXihLIPTns6w== X-Received: by 2002:a05:6358:8404:b0:176:5d11:3071 with SMTP id b4-20020a056358840400b001765d113071mr519801rwk.12.1706164095683; Wed, 24 Jan 2024 22:28:15 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id t19-20020a056a00139300b006dd870b51b8sm3201139pfg.126.2024.01.24.22.28.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jan 2024 22:28:15 -0800 (PST) From: debug@rivosinc.com To: rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, paul.walmsley@sifive.com, palmer@dabbelt.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com Cc: corbet@lwn.net, aou@eecs.berkeley.edu, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, guoren@kernel.org, samitolvanen@google.com, evan@rivosinc.com, xiao.w.wang@intel.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, waylingii@gmail.com, greentime.hu@sifive.com, heiko@sntech.de, jszhang@kernel.org, shikemeng@huaweicloud.com, david@redhat.com, charlie@rivosinc.com, panqinglin2020@iscas.ac.cn, willy@infradead.org, vincent.chen@sifive.com, andy.chiu@sifive.com, gerg@kernel.org, jeeheng.sia@starfivetech.com, mason.huo@starfivetech.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bhe@redhat.com, chenjiahao16@huawei.com, ruscur@russell.cc, bgray@linux.ibm.com, alx@kernel.org, baruch@tkos.co.il, zhangqing@loongson.cn, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, joey.gouly@arm.com, shr@devkernel.io, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [RFC PATCH v1 01/28] riscv: abstract envcfg CSR Date: Wed, 24 Jan 2024 22:21:26 -0800 Message-ID: <20240125062739.1339782-2-debug@rivosinc.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240125062739.1339782-1-debug@rivosinc.com> References: <20240125062739.1339782-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: B492118000F X-Stat-Signature: 73xian99z5k8ijceyqe3cu761nm4wfue X-Rspam-User: X-HE-Tag: 1706164096-548154 X-HE-Meta: U2FsdGVkX1/h/rSgnPxIUP9B9tnSm0UbX1+LxIP9pDC1qITJApEaFyopXFSAHC6FUsp9F1JEnrBoosW8Y4dAsn8meWZqhEv3uTSK2LiU+pAN3hlU+6CJtiEwvyEWggBzu9z/6OnpJFrzw4LW0Ao+Nm5egTTzb4jD6S1RouC/OJN4JI/WEoiHeKK546I0qFIo0V5+iTJkj7SaBMxMTJFYpwpyIh6BFgkKoVmJ9pGSypP9xSCSDRhg4LbhegeQbA4KuqaDnFbNtgitxkZ8l6RLpG7ZfDGLYfd46kPTSBtwWXJvidNY/dPyLXcEFkr8IqHt/gGiF3lc0tU9mRWBuOghlN0ljepO9RTTJdEd5EYQOc8hNAwdxo1gbHYqwtOQY17JGhT1UBW0V0kqsCgkSDjD7ZCE7Wps/0ueoMvG0PlFfBPj6cPW4dmeqSq4krwjh55ndWxrEJqhvATl942IgIU3Wb+GYvgPGF0l51wW6tO1qerwrPn/J68qVFaX6jImlYmP/tEN88IO71Rg0hYRA/acazGjeS7r9SHXrhJmOiqZKzQvp2BhhexbP8lEadd2rm7p81EveLpb8OwpZ4A6a3EfcNKBT4jpdvCRcpBJB9iaBBMcs3FvjaUt2gLnn1v3/lIWZaNCzgG5haK63kccGpH3wZAk3/upf3uDvUHMpuo+68TfcIc9LJd/VcculbYrjkbutetEJgh+zO4GfEPW1bKEt6nhzLBXu/ALddvLsA3f2CQan4RqiCdlRC1JV+WZVLZfi87WbgF8XPpUCPoVJtIJuooa2ftqyuvnVOQACMQxVwRQhspMt1p8FlFHjVqMP993bK/w2StCNJjU5JcEgA7NhRaujWVZ5v4VtQ8xdx4LcN8anYO+WWXGpu5UnkwBB4JoFR5PmXItP369fRINjgfc8dcvHBZ+I7ncFfJhWvpEECtPzXllL7G3VvxvF+iQlyP+sNw6ubShY5kp7qY1KA+ KU/UrrLV oedFIl+tx7uiOb8TD8j5xXN2hrYXoV8wGpRiFwGAYMxliq4PjuHX9yfu+jLqXyeljDOmSIu4nouwDCNemMXqcj7vlSoVBRuaXAEdkukCC4WLTx15Kaap3b9hJcjC4rm3OfzbYQXiO6oyKodgQsMiRZW9wY/j26PHzAEHSUEYeE4stDVCVlcgXxoJBP6kGlUn2QgLqDiJjBKXN6pTjMjciay46c6TCvbmvKpX+A3+B8BDBIAYha8Gs5qPkrQUjoR/+NSuebB8H2MWd4Ma2Z8bwKOrpC2FTE6JZ6yS2cpmLlz9gNGLt0qz8r76JPRzQVBX77h+F3gaUcqjLoPEEZr18USfT7EKsbbYs4guWEXaTeb3LCJYjHwLq9twY6Mx+CXG3YYgf3RzihKMMhziyTolmNuxqbQp8lmqrRaTUaIf5G3KjsbaiziAEY0N6dTbQkw5pgzNu 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: Deepak Gupta This patch abstracts envcfg CSR in kernel (as is done for other homonyn CSRs). CSR_ENVCFG is used as alias for CSR_SENVCFG or CSR_MENVCFG depending on how kernel is compiled. Additionally it changes CBZE enabling to start using CSR_ENVCFG instead of CSR_SENVCFG. Signed-off-by: Deepak Gupta Reviewed-by: Andrew Jones --- arch/riscv/include/asm/csr.h | 2 ++ arch/riscv/kernel/cpufeature.c | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/arch/riscv/include/asm/csr.h b/arch/riscv/include/asm/csr.h index 306a19a5509c..b3400517b0a9 100644 --- a/arch/riscv/include/asm/csr.h +++ b/arch/riscv/include/asm/csr.h @@ -415,6 +415,7 @@ # define CSR_STATUS CSR_MSTATUS # define CSR_IE CSR_MIE # define CSR_TVEC CSR_MTVEC +# define CSR_ENVCFG CSR_MENVCFG # define CSR_SCRATCH CSR_MSCRATCH # define CSR_EPC CSR_MEPC # define CSR_CAUSE CSR_MCAUSE @@ -439,6 +440,7 @@ # define CSR_STATUS CSR_SSTATUS # define CSR_IE CSR_SIE # define CSR_TVEC CSR_STVEC +# define CSR_ENVCFG CSR_SENVCFG # define CSR_SCRATCH CSR_SSCRATCH # define CSR_EPC CSR_SEPC # define CSR_CAUSE CSR_SCAUSE diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c index b3785ffc1570..98623393fd1f 100644 --- a/arch/riscv/kernel/cpufeature.c +++ b/arch/riscv/kernel/cpufeature.c @@ -725,7 +725,7 @@ arch_initcall(check_unaligned_access_all_cpus); void riscv_user_isa_enable(void) { if (riscv_cpu_has_extension_unlikely(smp_processor_id(), RISCV_ISA_EXT_ZICBOZ)) - csr_set(CSR_SENVCFG, ENVCFG_CBZE); + csr_set(CSR_ENVCFG, ENVCFG_CBZE); } #ifdef CONFIG_RISCV_ALTERNATIVE From patchwork Thu Jan 25 06:21:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13530072 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 506B6C47258 for ; Thu, 25 Jan 2024 06:28:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D18396B0080; Thu, 25 Jan 2024 01:28:24 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C9ED26B0081; Thu, 25 Jan 2024 01:28:24 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AF0CC6B0082; Thu, 25 Jan 2024 01:28:24 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 990E76B0080 for ; Thu, 25 Jan 2024 01:28:24 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 34661C07CA for ; Thu, 25 Jan 2024 06:28:24 +0000 (UTC) X-FDA: 81716854128.02.4684716 Received: from mail-oi1-f170.google.com (mail-oi1-f170.google.com [209.85.167.170]) by imf28.hostedemail.com (Postfix) with ESMTP id 6B681C0017 for ; Thu, 25 Jan 2024 06:28:22 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=0YlCSHbe; dmarc=none; spf=pass (imf28.hostedemail.com: domain of debug@rivosinc.com designates 209.85.167.170 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706164102; 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=UpWYX6cXeb3EwmSmbUmLWJ8fZpndT9qVanP1zDkk0zs=; b=K7m4WfdQCvqUrZ5T4QHdRNyaFcFVqEdAu/GkYvu1/BnnNgMSSPFxnEsUujjzxxhX1SkjgW w/sHR6Cqv+8tx+fnzWLnKUpQ2TT9XN4b8pUFL0Qxs5vKm2PLkQBjgtU/a+dCU0QhfI5RCB 8FBjfBrlOC4F2gMylCpWKh5Ip75yd74= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=0YlCSHbe; dmarc=none; spf=pass (imf28.hostedemail.com: domain of debug@rivosinc.com designates 209.85.167.170 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706164102; a=rsa-sha256; cv=none; b=Y86XeAoZNnocZUudY/iJiOE16FEyV0u1892rV/uXoVYfJFcXqgAGWfB/+zMASANMiNZBPX cRsnOWgvl8x5AQ/nD5dwuGX2yhOEpj+FNdX0Y0y4nIMnZ5AS991C1XZGLlwqzv8HOvy/XF SNxk/idJA43Krya8Sz9ZOExZ9RIVtzY= Received: by mail-oi1-f170.google.com with SMTP id 5614622812f47-3bd7c15a745so4544393b6e.2 for ; Wed, 24 Jan 2024 22:28:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1706164101; x=1706768901; 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=UpWYX6cXeb3EwmSmbUmLWJ8fZpndT9qVanP1zDkk0zs=; b=0YlCSHbecypQsuolP9Z53It3UJij7mwuFaAvgryBQkEnLCw1f+meuqHtCBb+t0NpBf P6iYe163SVkIILq3dGcCyo38rhD4LK/p1KIHM+mGDVNVe+MZtE8/8lt860SueMrJt1pJ Yyd3tY1qlLfUvGR/fs3BgoA+idBQxvt2OH0toN+4tW7QQKSA3m+vSGgSkOTSVAZi5o2Y yjjZ5hpYe0LEAHcKgYf1BzzjHOU9a8YJ0p0ewMcRhZcpZsv3vbQBGbkJigPbhCSusEWI CjMz1c0b2eQQfLbohwBVF3IBHc9GQZV6pAgO/Wg8OI/+pFiAp4VJCrZnPH/caoC/GM4o Q55A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706164101; x=1706768901; 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=UpWYX6cXeb3EwmSmbUmLWJ8fZpndT9qVanP1zDkk0zs=; b=MbqBN6hiwmgzbBbDn6cYxWKENWXnz23ynZEnk6m95oVReEYwFE0PFb1qyWOJhdd3a5 Zz3ulw0HqRzro+5Ujmj8Q81Ydm7E9t/cOP6Oh0iR7woXVrdVLawFMB8J6oidnCpIvndw PdFemuWJSzXNtCwhcyawEj5VakjNNBLDUPIjTImjs1tkf57hGUpn/M09fLrMRSW/0Q3C WGpXJMTmzpF4TDBtgTjX+lG2oq3rLI5M/6PxbFDSGpT+8DK6ii6rCXt9ZOTv51azLHEb z6xfzJcNI6wvlkm7IcXE3klvns0V8CohJ3zuikVwFMV0xZk2yweruiC8vzEsrSxuAr1G AuqA== X-Gm-Message-State: AOJu0Yx8BYSThMpS4dcv4Mcivg/mQ6M0AwwBc8stacZbjnSEL41kdUTf I8Cbkw/kK2bJT86hzpZldj/+jIQErJ6gda+Dsi/TWeBQlsQ/wP4nbk/J0yUhdKQ= X-Google-Smtp-Source: AGHT+IEsqvMNFQpFlOYcM36mb6P72n2g5IoYJQjyFjdvM0xADABaEzJAYSqo7gQsIel9ygICs1O66Q== X-Received: by 2002:a05:6808:3197:b0:3bd:a590:8a00 with SMTP id cd23-20020a056808319700b003bda5908a00mr578367oib.85.1706164101404; Wed, 24 Jan 2024 22:28:21 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id t19-20020a056a00139300b006dd870b51b8sm3201139pfg.126.2024.01.24.22.28.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jan 2024 22:28:21 -0800 (PST) From: debug@rivosinc.com To: rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, paul.walmsley@sifive.com, palmer@dabbelt.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com Cc: corbet@lwn.net, aou@eecs.berkeley.edu, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, guoren@kernel.org, samitolvanen@google.com, evan@rivosinc.com, xiao.w.wang@intel.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, waylingii@gmail.com, greentime.hu@sifive.com, heiko@sntech.de, jszhang@kernel.org, shikemeng@huaweicloud.com, david@redhat.com, charlie@rivosinc.com, panqinglin2020@iscas.ac.cn, willy@infradead.org, vincent.chen@sifive.com, andy.chiu@sifive.com, gerg@kernel.org, jeeheng.sia@starfivetech.com, mason.huo@starfivetech.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bhe@redhat.com, chenjiahao16@huawei.com, ruscur@russell.cc, bgray@linux.ibm.com, alx@kernel.org, baruch@tkos.co.il, zhangqing@loongson.cn, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, joey.gouly@arm.com, shr@devkernel.io, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [RFC PATCH v1 02/28] riscv: envcfg save and restore on trap entry/exit Date: Wed, 24 Jan 2024 22:21:27 -0800 Message-ID: <20240125062739.1339782-3-debug@rivosinc.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240125062739.1339782-1-debug@rivosinc.com> References: <20240125062739.1339782-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 6B681C0017 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: ebi8wxpsnrf5j5yatp1naf1uaqe5afdj X-HE-Tag: 1706164102-854973 X-HE-Meta: U2FsdGVkX18InXbKz7BflU/3GocPp8gnAFgyMv34yxBR0BRdZ3tJTYzkJdScttf0btYhgoHVx68kpBJ1+IUPBHKCZQ2u3Z3EOgpNH9Pup2l+uYMVpVSUbEvzB1Ofuq+g0/1NDu0f+HhFOgqJ75otgjMqPii3nE6ygJ7PyESSRoIGOt5QTMQ5VJUnwoi97yPaJJfHTqsEVhs/76fXdcAJyzIpsq/JZE1GKozVXxEwkpJaWeyJIAF03mDUNNcHjW2PdZO06nY5hFal4NAEpWztc8Xxm5FBKKmbMipai7mehBQup0zypddI53xMIRpJEJUozLQpVYXbeE8upZ4CQ3PICdseBhQtGo7V0dk3iJkExGGYangLGgrpMljUESiljBAe9MsMf8KWopc6cik8uk5V0qHHUwe0sCLfVB4vQ89Unv9cp9l1nrdU+LCPmaNxFD5fhK6wY1cvL6O7r2ij0poO1TcKWyKNpn2gLgOe4/6lEHGc/FCEa4P3jzR6hYd/WGWRgTihAB2iqVMuZiPEhTYwjT4MMqCXm4c/3yHdt713idCNME8S04arqFbSwtxKIjD5zia5F7kGBaL8+eW3wjhmKtvwA8/SnPYspKwyi/fyt+gVwE9LSd1t9T8gGyr96S+3n5k4T6YRRIQlGNAms4Yo1FziE+7T5ajBeyvnTVu6UmPMWENPu7eP7NFElWZiF2ajCrKh/ZF+8yKJA3Q4bKcHkjdvnyFf8vg8rNwOeKHUsbQ2owpAHT8QuDHAfgLTCPPUgtQ41qgACyRlqKb+PCOJqN8TU6FfOahGHiza5yULrpBM+aT1F6JsOOMNh5KwxKEOkr9XhPPSQ0TyhVo4rBkIm27RcCM2iyfRMXqJw1/vkCy7VOVFQ0uGxa9kEpZutdsCDgvSJR7+kOrLfGj+WoeJf25EzvayIqS/OT7pTqnAGBi0yRf+AIlMRiZoo/sTAfo8wqstKeSCfrD4zEIOJPh YciZu/oz ErujXbqNjTwVeCC3Pa/LKgIz3vGQFC+6vHNprq1rL14kn6BS7GIQYvWxdEP61wmJ+7YjOupG0IMe6b3zazhOWfVPaRi6UH7GZsQPL7Ol5eWqKEg4OWM1hHK2WNLjp46vkTKugt5kEKc+6nTP0DKOpD+6WbA+LkfAWWhos32qWFtv4mpqzswSJA7MvdOlEoIgRIScLkJoK6R6Kv2tZsNPEmZU9m8TuV8JeC8IY5kOMOlpXdOMz5hPbPl6WzFHk93iSJVQSvcZ/tzyNj1oXS5mBcJr82NHRarx8PJyedG0yXpR0BOIHqQfoH1hv6dS1/2HD/lng0+TzuLwhqJxZ1NF+yzAsU7C8PTFWXHuOPeFm/JXmtPECiGSf0u85pWO5z7YFeiKiJ8lEb18WbBKnsJ9C5E64LE8eWw/DsnGpBsx50Zd2DWvxgfo7NDETThzzVn+0FiWu 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: Deepak Gupta envcfg CSR defines enabling bits for cache management instructions and soon will control enabling for control flow integrity and pointer masking features. Control flow integrity enabling for forward cfi and backward cfi is controlled via envcfg and thus need to be enabled on per thread basis. This patch creates a place holder for envcfg CSR in `thread_info` and adds logic to save and restore on trap entry and exits. Signed-off-by: Deepak Gupta --- arch/riscv/include/asm/thread_info.h | 1 + arch/riscv/kernel/asm-offsets.c | 1 + arch/riscv/kernel/entry.S | 4 ++++ 3 files changed, 6 insertions(+) diff --git a/arch/riscv/include/asm/thread_info.h b/arch/riscv/include/asm/thread_info.h index 574779900bfb..320bc899a63b 100644 --- a/arch/riscv/include/asm/thread_info.h +++ b/arch/riscv/include/asm/thread_info.h @@ -57,6 +57,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; diff --git a/arch/riscv/kernel/asm-offsets.c b/arch/riscv/kernel/asm-offsets.c index a03129f40c46..cdd8f095c30c 100644 --- a/arch/riscv/kernel/asm-offsets.c +++ b/arch/riscv/kernel/asm-offsets.c @@ -39,6 +39,7 @@ void asm_offsets(void) OFFSET(TASK_TI_PREEMPT_COUNT, task_struct, thread_info.preempt_count); OFFSET(TASK_TI_KERNEL_SP, task_struct, thread_info.kernel_sp); OFFSET(TASK_TI_USER_SP, task_struct, thread_info.user_sp); + OFFSET(TASK_TI_ENVCFG, task_struct, thread_info.envcfg); #ifdef CONFIG_SHADOW_CALL_STACK OFFSET(TASK_TI_SCS_SP, task_struct, thread_info.scs_sp); #endif diff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S index 54ca4564a926..63c3855ba80d 100644 --- a/arch/riscv/kernel/entry.S +++ b/arch/riscv/kernel/entry.S @@ -129,6 +129,10 @@ SYM_CODE_START_NOALIGN(ret_from_exception) addi s0, sp, PT_SIZE_ON_STACK REG_S s0, TASK_TI_KERNEL_SP(tp) + /* restore envcfg bits for current thread */ + REG_L s0, TASK_TI_ENVCFG(tp) + csrw CSR_ENVCFG, s0 + /* Save the kernel shadow call stack pointer */ scs_save_current From patchwork Thu Jan 25 06:21:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13530073 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 42C45C47258 for ; Thu, 25 Jan 2024 06:28:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B42B46B0082; Thu, 25 Jan 2024 01:28:31 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id AC9656B0083; Thu, 25 Jan 2024 01:28:31 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 91C1F6B0085; Thu, 25 Jan 2024 01:28:31 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 7D4EE6B0082 for ; Thu, 25 Jan 2024 01:28:31 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 242101607BB for ; Thu, 25 Jan 2024 06:28:31 +0000 (UTC) X-FDA: 81716854422.22.B09DA05 Received: from mail-oi1-f179.google.com (mail-oi1-f179.google.com [209.85.167.179]) by imf30.hostedemail.com (Postfix) with ESMTP id 5736A80015 for ; Thu, 25 Jan 2024 06:28:29 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=rI07JGy3; dmarc=none; spf=pass (imf30.hostedemail.com: domain of debug@rivosinc.com designates 209.85.167.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=1706164109; 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=ioE1LIRdb8UGU7iHQDrnlckX+FNXkyoBwT0Lgj/da5g=; b=BDeZT6kwEk3voXosEGk/6PdryZT/Lz6MotWB6q15dIM39Zklyb/MTG9xy9VWoh+ujDHWjO V/K9WT79VrtM/0lSppiawSYv/3e4HHQ7ijL/5CN7SbgZshMY2U0wz0HPdMQMCkJU1y/phC bUd7ZJwgFQdglGD8qh6MimpttyYKhKk= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=rI07JGy3; dmarc=none; spf=pass (imf30.hostedemail.com: domain of debug@rivosinc.com designates 209.85.167.179 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706164109; a=rsa-sha256; cv=none; b=R4NXwBT/B4rwydYSQN/bIzWtFZCdCWTnf9C0H2M6eXhcCr2ME/YPtVXvvuk2eMz7CHvQOD FhY+UCeoLk3ic2v4cJA7DY3HoXGFtXs/4Bb9UFqAzL1ARrMBMCHHt/lW1PGYEdxrsyDAOx x9lpnknpQnsfPcq7r7awoilpsK9GV0o= Received: by mail-oi1-f179.google.com with SMTP id 5614622812f47-3bd6581bca0so3975444b6e.0 for ; Wed, 24 Jan 2024 22:28:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1706164108; x=1706768908; 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=ioE1LIRdb8UGU7iHQDrnlckX+FNXkyoBwT0Lgj/da5g=; b=rI07JGy3fa45VoWSmg7Yykj1eLrPNx4LTYL2vYBRAxBXhvtfv9K10kDhimriLo1Y6y RIUrvXlFaVVZTp8enlePXEWB7uBY+76EMwhH23CE6dU6OemUfM3sSZ4xEIVJM+521kW4 M2Hx6mpmREgbyLXTlG3opiaR2FlgSLt+KGhsCa+3k4SOc5SGG5PGAHPslJ6V+nIPF2bP BhdT5rwI9pDeZvVJk+ZjrfpXLmbJdzGVshyHB1faumLvEcN66Y8iivRGrJWPOSSyC0SB IwUYpKILCHpMy6Wju1CekYmlHe4ZCYZNsvDlOQTk85vmLCNhd4x3YWuXcjNZxe8k2UhE G0lw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706164108; x=1706768908; 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=ioE1LIRdb8UGU7iHQDrnlckX+FNXkyoBwT0Lgj/da5g=; b=gbFQYguGxm1+4byR/jSSWtooKTh4QR32WDgzCCJ8y+tr5SZsKK2LyF2FVWdPPRaXXv My+i4C/jhkq3wY6tuWAvQIUlJanRF7++JLglGXfxGfM+XFEeQQwoGinXk1BoTyr5FS/x NMI2ry7b8ZZ8UssURMNCgMcvtwk0SHqHM6LNDUW70wz5sFLjCjdlT1YJ6JXmPFCn23OI psLbLFcAmTzpqsUmps5m5ugM9aOHBIZUYyH0BcLZPihLRSkn1kZnvq3yLhL5t0OyL57v 5kjOW1vHhXUrackDETvVUu2eGlstFVKBWtbB6+A501OZzWsr0zmRHdRBeR6kB7zMVWAK 7Fbg== X-Gm-Message-State: AOJu0Yyno8l9pFvuKYeuoOrMa92bxx6Uq+dvbxSffxBeHSDWn4A/h9kc 1xTyIh5ndyBz8sKpaYypi2Ib8ZLoXJN7ggkv0eqpqiU/TkZk5RJXtwrHSnZQbEo= X-Google-Smtp-Source: AGHT+IEDX9sIkxnHph4wuJVvd42o38950kc3fb8mUyOs60adtjswg3LDjUQk/s2QDj8rOhFBqYZqog== X-Received: by 2002:a05:6808:159a:b0:3bd:7218:f318 with SMTP id t26-20020a056808159a00b003bd7218f318mr526373oiw.4.1706164108504; Wed, 24 Jan 2024 22:28:28 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id t19-20020a056a00139300b006dd870b51b8sm3201139pfg.126.2024.01.24.22.28.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jan 2024 22:28:28 -0800 (PST) From: debug@rivosinc.com To: rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, paul.walmsley@sifive.com, palmer@dabbelt.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com Cc: corbet@lwn.net, aou@eecs.berkeley.edu, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, guoren@kernel.org, samitolvanen@google.com, evan@rivosinc.com, xiao.w.wang@intel.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, waylingii@gmail.com, greentime.hu@sifive.com, heiko@sntech.de, jszhang@kernel.org, shikemeng@huaweicloud.com, david@redhat.com, charlie@rivosinc.com, panqinglin2020@iscas.ac.cn, willy@infradead.org, vincent.chen@sifive.com, andy.chiu@sifive.com, gerg@kernel.org, jeeheng.sia@starfivetech.com, mason.huo@starfivetech.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bhe@redhat.com, chenjiahao16@huawei.com, ruscur@russell.cc, bgray@linux.ibm.com, alx@kernel.org, baruch@tkos.co.il, zhangqing@loongson.cn, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, joey.gouly@arm.com, shr@devkernel.io, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [RFC PATCH v1 03/28] riscv: define default value for envcfg Date: Wed, 24 Jan 2024 22:21:28 -0800 Message-ID: <20240125062739.1339782-4-debug@rivosinc.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240125062739.1339782-1-debug@rivosinc.com> References: <20240125062739.1339782-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 5736A80015 X-Stat-Signature: ketq5fcfb8fpzcpuzs9rpyk84fs6qhxr X-HE-Tag: 1706164109-732282 X-HE-Meta: U2FsdGVkX1/jBLRQpVwOgwsmYJTYGGAnxtpB+v+mBkXJREtlMGz/nqfDfkhD5CQqgQISdvuoZG873hgH4jJH4JJ61TgQbxASrPRTKnhOPYLOPpdqBdQKVUDylng7NpcB5qeDEsvNguvJLnlc3/GWrL4nXd6IX5217nnF7G4LKZQSpuBj9/oVLAvPrdf9/IdWifv+m58T8nYAkwIRUBTA30DklYNyafvxHp1u4POeBqJQc/1iSCeAqKTRw4VSUDB4O8qeR3Q3Djc0GDPzXD6iByj4I2WrjZny8Jj6M4CK7LQAlMoYfQ68fnZfCiaqe3Av3Np3lQuwpf7yEDfquD2dosfpHcY7dpXiJK0kLuwTZh+d63Uy50fRVWRqT/qWMCrXMx0WPonZ1jJ9NNBgDio+tZNFz36VgFPx764zQilUgdkF7ccSNaRK5jgTMJR257W9n89rUmR5u5B4g9CV4BN+yYhlufPtNZuo0fNUqtn9kLxrlUCQb9ChYNyAU+yq5qwfPqvX9JLqR7R5o8HtCEWOW58t0XvsTPTDs6prN79NVKwUrA82mOm6d38RqayHrMdgKRNRmpDhbgqXwrdaieN0v/ERmOsvV670XKbbiNSKxv8wbhYGrSqwBlRymZHsLD4/Q3rNh88193EleP3wqLqkOkdiS4zkajHMShQn6UxiPDog8oCokPpDAT3xDeqsqpU2zo6HwkE4cNuUMNExte7ec/qruEfIX1lmF57EzprLoZr8/VBWTKRyNtgswakRdNjJYvNfA8Py2iahiz7Sg/uLLXHp2PY0R4NNENRUhtVphlQ34oUAeNIvlIbCXvx5J9lHuFiARNVxwPgP8PzMbnl03WGCkzpDpvgkOWmRmvjDx0wNsBAqVlFhMhQsmguq+k1E0LsDrd+kgjSkmZ+OKfcBoyQx7Bs8TD1jLM2/ctFUhpnqnnb4IMlcB+oSS1K273ZMgNf+Wci1oxCm3NI6Ewk Yk5hDp26 FO7+wQsGY6qVdrqAgFR5AGBvKJbJf1VxtCz8kuI6VyFEEXRvGC9YC3p6he7Kj8Pa3/7xaYoZGSWoRwR/faNUJF6EMOAGgUDMwCAJ2kdSmYkXR+vhKPIcxkEJOtIWxxxRETagypUhxVG5UOwBhdo7DPtbou5ZKT8+xJjq5LXgJozRAuEcJwIZRdqgVPIxOd3AOQK/tnp+a8eDH/SjurgydDuOUEzZaoAHmgF8/LKVBVmmQUzfEc10dzKKWZKvi0+EVnopWA/PO2yFZdBLGRcnTudZwVVgJykSJ2nTWOgkYbzeyIm3JMWcCf4GfpABPtCVpDu2HeopOJ9LB1QatS3vu/qMPmebY0rPzChl9ZzrR9GV/V4FTAbMcuMMERulI7I79mlGe39ggBdcTFxdvXeK4w2yGSCYMqxR7uIIgtR0jiypKj1CTsI6GH66hYVRi5EuZqoIZ 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: Deepak Gupta Defines a base default value for envcfg per task. By default all tasks should have cache zeroing capability. Any future capabilities can be turned on. Signed-off-by: Deepak Gupta --- arch/riscv/include/asm/csr.h | 2 ++ arch/riscv/kernel/process.c | 1 + 2 files changed, 3 insertions(+) diff --git a/arch/riscv/include/asm/csr.h b/arch/riscv/include/asm/csr.h index b3400517b0a9..01ba87954da2 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 4f21d970a129..2420123444c4 100644 --- a/arch/riscv/kernel/process.c +++ b/arch/riscv/kernel/process.c @@ -152,6 +152,7 @@ void start_thread(struct pt_regs *regs, unsigned long pc, else regs->status |= SR_UXL_64; #endif + current->thread_info.envcfg = ENVCFG_BASE; } void flush_thread(void) From patchwork Thu Jan 25 06:21:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13530074 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 737D4C47258 for ; Thu, 25 Jan 2024 06:28:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id ACB726B0085; Thu, 25 Jan 2024 01:28:37 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A554E6B0087; Thu, 25 Jan 2024 01:28:37 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8A7CC6B0088; Thu, 25 Jan 2024 01:28:37 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 71BDF6B0085 for ; Thu, 25 Jan 2024 01:28:37 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 45264A0C9F for ; Thu, 25 Jan 2024 06:28:36 +0000 (UTC) X-FDA: 81716854632.21.7FDFA92 Received: from mail-oi1-f172.google.com (mail-oi1-f172.google.com [209.85.167.172]) by imf15.hostedemail.com (Postfix) with ESMTP id 992C7A001B for ; Thu, 25 Jan 2024 06:28:34 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=QdFnKB12; spf=pass (imf15.hostedemail.com: domain of debug@rivosinc.com designates 209.85.167.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=1706164114; 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=Sk+gUexiyKjcB+TRRECBjEsvOh5bXAF+ZpNuCmy1Ha0=; b=kw5FXE2LubGfaOTDfb3xsUu+DP8TCj5fppTJ4rr52osBkQ3UdrtfhNGQx6Po7wnPrtHRp8 1i4vQG9sqbekZHQKvNnQ1Wg94vuPTrXwOpH5r/mC43XJ0zuB/7X70BbsLFYDhPMHWA5F/N MWt2jGHvRLl1ExcyKZVCmUFUJIj+0WU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706164114; a=rsa-sha256; cv=none; b=pLVl8SQyy0jMLhiqLxgQquHM8vHoWoUAll7L6dx4iK9gLB7mGCEOZveOI6n7OTEeBtWdlo yH7KwwGtnjiknl8uMMvfTPghC6pHWbhAYDUQAkySHt5G/Ty0atX6g0UFxGPz3N/NAHfRIE atNsgTwWp0cz9RVptvEfO3/0Sp7P3Oo= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=QdFnKB12; spf=pass (imf15.hostedemail.com: domain of debug@rivosinc.com designates 209.85.167.172 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-oi1-f172.google.com with SMTP id 5614622812f47-3bd5c4cffefso6300455b6e.1 for ; Wed, 24 Jan 2024 22:28:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1706164114; x=1706768914; 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=Sk+gUexiyKjcB+TRRECBjEsvOh5bXAF+ZpNuCmy1Ha0=; b=QdFnKB12gfOvkHpIm0Hs0Okz94u1LBPnlDenADDqT/xMrHPjx0AAdG7isPakYBfvGW pfcze3YjufP+9Yq/GjNBJXKfCXcVElIPRgkxVt0ksPPCPaWTZhjbNtSDrp5CBFJ3NyG3 DlGjyGKtrNNNizjhuibO2rRppgBlALXbM2O2B7uoUXYuc1iRVet8kD8bpauoa41FXTV7 ij+X96nl7puDfdKNSRpMUYf/CAyNVK8xhN9p++K3iWTIM1EtIHn5ehuN2qXCjuHh5sVA KAr569h2QBw2cbPVvl4GTHKKDgEClWzqVDvWYmcZy3izwc7Vv155QLDgnmrLNoo0ysfF rxYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706164114; x=1706768914; 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=Sk+gUexiyKjcB+TRRECBjEsvOh5bXAF+ZpNuCmy1Ha0=; b=UTe234abEmQO/W4620HV5dVDkQab5iBZdC8F8j4YeREgjtnzGbJ4xLfyef1HIbWKv9 MGS09tTovB5SRYJoYhAv44exJJjtL3ka62zjZWO0O4ING/s9KIWt6sgmNsKmiBUSx3A3 DCKKULVy8CYjy28URAIM4lrN/ivkbPasODnh27x0ko2Sja9jRJprBKkKyLrz/HNTmcW/ zXV+jaqms/fqd9qJIJBTq8jITquVEHZ/p5sf4QvY2GtJD4P/I5YpIy/1GPuj3wxG5IEJ b7R7pU4ZrakYnzP+v6Cd83x2BjQwDq6N9XQ1cIE3F4auaEkJUBK2vOyZucMcl7DQgh3x 7cqQ== X-Gm-Message-State: AOJu0Yy+hKfQgUqFfh0OrCp94vKuyI8xguLOI4+qwpaGQ1Kdi3ft2scm JnPcVB+eaf2/EhYkHRgVFFsnf6IWfAa0z2fyns3T1//q38kkD4UBwzBvTgL90Gw= X-Google-Smtp-Source: AGHT+IHgnfgiRGT+J+bbquDHQHlbWjWMk2ANkfaKEEr2r5iLWrd51iWjTqQbhay0o6iShLRyInjd9g== X-Received: by 2002:a05:6808:e84:b0:3bd:54a2:7df3 with SMTP id k4-20020a0568080e8400b003bd54a27df3mr377689oil.14.1706164113684; Wed, 24 Jan 2024 22:28:33 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id t19-20020a056a00139300b006dd870b51b8sm3201139pfg.126.2024.01.24.22.28.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jan 2024 22:28:33 -0800 (PST) From: debug@rivosinc.com To: rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, paul.walmsley@sifive.com, palmer@dabbelt.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com Cc: corbet@lwn.net, aou@eecs.berkeley.edu, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, guoren@kernel.org, samitolvanen@google.com, evan@rivosinc.com, xiao.w.wang@intel.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, waylingii@gmail.com, greentime.hu@sifive.com, heiko@sntech.de, jszhang@kernel.org, shikemeng@huaweicloud.com, david@redhat.com, charlie@rivosinc.com, panqinglin2020@iscas.ac.cn, willy@infradead.org, vincent.chen@sifive.com, andy.chiu@sifive.com, gerg@kernel.org, jeeheng.sia@starfivetech.com, mason.huo@starfivetech.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bhe@redhat.com, chenjiahao16@huawei.com, ruscur@russell.cc, bgray@linux.ibm.com, alx@kernel.org, baruch@tkos.co.il, zhangqing@loongson.cn, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, joey.gouly@arm.com, shr@devkernel.io, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [RFC PATCH v1 04/28] riscv/Kconfig: enable HAVE_EXIT_THREAD for riscv Date: Wed, 24 Jan 2024 22:21:29 -0800 Message-ID: <20240125062739.1339782-5-debug@rivosinc.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240125062739.1339782-1-debug@rivosinc.com> References: <20240125062739.1339782-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 992C7A001B X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: wuigdbh1s8ir4r5dtagwz9gxi9iu9kbj X-HE-Tag: 1706164114-686867 X-HE-Meta: U2FsdGVkX1/MwJSE96GpgiASvRxvxA1+H/kXyrOLfDPUSN7oeC9zUaquKrW+SAJrD2+SIZMqx37S1cul430Ot/X6Sh62CHHmidpiToBkDN0MCYHrhKD9GY8IT9nzOBWkyqOAjiRoEzU1NkDOcU2LX5Lc6lsHgCX6xCmnvoEWiIc+bcjuN1DqxWVDVTqrIf+bwRSiSmS/ovnSyK/MD/kL0k7ZVlG2zzLJohPd5/VZSKoBfnPrdF8sBhTvV5vH+NS0en1AmhEuHeeRrl1Env75ZE3xc0bd6Txxli4IKKahomz+pOZMIk4BWIADASuCgVUgFKzIjkbMxq/i0FojtKGZEPZAHaErdSwrVZUFX5vLx348KbCVkz7gETntZiEUEnp1K/5DNDYvrtgRUtnTah9ehhzjoBYE5HLJhebWI5sctSHUOO72v13CfEoyvfMCC2E+HOMClg4W0QBk8BnFtiJnIgi5b7m1IBlRUR6I3OzHT6oacO2YeOr71kMLEnCNS4i0llRroqucC7YGj5612dkYNjbeNs/AxGDjUpEjOVmLKRyd1KfyUM0BpgXE8ptF8c3DVmw8lJ5jz4n3lMUnFwgcnEvphYpkkhsyrUKzurB/Q4plwzyxlnXju5vuqjZl+trnwYPrmpkpF1qZ5qC7hmaJQAN85/FN6gMD9OsGVG5KSkz4L+rx+Wb5vH2MmqLxyI5/NOq6y1ZyyoIUGnjdBPDwtJIXtyLcb+kxw6YWVVI8ExDTb2XT5/FpCEqFfWF3vA2810Jbl/yomax3atm83EC/f/QWg7Co1bexEAc3KTzCJiRwh0skuC7LyQzTfsO9oyDo8hQejsEpXQtv/mz5g6vBXykVHD2EMDYbs5HkrvtZjbg2hm6znw5lFaNJTBTr+gIAnbV+mgrsE7vqHsOZhXfNiRr3OtPEDJBNe+00vZkviPR4wbWP7uhtMNx3Wo6Z1Vpko1S9tB0GlahPw071L6o YjvuD5hr XzgCn4z6gx/vlDvhZPMFtfGcstMkbBGXJy23Se1lhSYXd/lZrXoH4MFgvmZYNLfaTPCsp0eZwL/G5ap/YJN0dV9JL/dNuQhUoHTWWbxkOh+tiBa4ibdY8mjralsxNCg0T0CuXhcdPXLvcqV0+vaIVWKJLEPH1sWMke1gIWvm2/OKfXLc2kv5ff/ZSZY7kq+/mvdfCKsFs52s2nH50jGAu3xjW9w== 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: Deepak Gupta riscv will need an implementation for exit_thread to clean up shadow stack when thread exits. If current thread had shadow stack enabled, shadow stack is allocated by default for any new thread. Signed-off-by: Deepak Gupta --- arch/riscv/Kconfig | 1 + arch/riscv/kernel/process.c | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index 95a2a06acc6a..9d386e9edc45 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -142,6 +142,7 @@ config RISCV select HAVE_RSEQ 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 2420123444c4..c249cf3d8083 100644 --- a/arch/riscv/kernel/process.c +++ b/arch/riscv/kernel/process.c @@ -192,6 +192,11 @@ int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src) return 0; } +void exit_thread(struct task_struct *tsk) +{ + return; +} + int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) { unsigned long clone_flags = args->flags; From patchwork Thu Jan 25 06:21:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13530075 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 8A4B1C47DDF for ; Thu, 25 Jan 2024 06:28:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 28E1F6B0088; Thu, 25 Jan 2024 01:28:51 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 23E526B0089; Thu, 25 Jan 2024 01:28:51 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0B9886B008A; Thu, 25 Jan 2024 01:28:51 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id E84A86B0088 for ; Thu, 25 Jan 2024 01:28:50 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 9B0091A0D30 for ; Thu, 25 Jan 2024 06:28:50 +0000 (UTC) X-FDA: 81716855220.25.DFB78BE Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) by imf07.hostedemail.com (Postfix) with ESMTP id CC3CC40011 for ; Thu, 25 Jan 2024 06:28:48 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=Dwee+kuW; spf=pass (imf07.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.179 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706164128; 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=dAejTXU0ubiQ5glx+ACfMnILZ3BSyMGr/22gR94zPLU=; b=JrU1bgsUHqUIt3dsNcu3JUN3XCnoyQT9XvnuZevQklDdg0qCBox+p/RdY2WqyGbGn6aGec fnX32f9geHj+DFEbgTinhoE/6OU852BtRIwWewbdXc4f5V/rG7CQqvBStDMoZXe8N+55bA /FR5zsWW2aPDotHlQgNW1uDZhCUUScg= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=Dwee+kuW; spf=pass (imf07.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.179 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706164128; a=rsa-sha256; cv=none; b=VqiH3sQlXtWjHbXA69QJgndSd+lc0M4773GAFWAbp/6Q6sS1JV0Z/k4eYbOWG7NKMiPhB3 TeQfPytltx6OPZycfQZQ0kBeYmrGQuFyfOiTgwTh6WyzrbW0mwaY5EiRe3YNIaHMAZ46v8 HEOWTU90rJf9XROjR8bwxg/wGxR9gC8= Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-6ddc1b30458so1123303b3a.1 for ; Wed, 24 Jan 2024 22:28:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1706164127; x=1706768927; 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=dAejTXU0ubiQ5glx+ACfMnILZ3BSyMGr/22gR94zPLU=; b=Dwee+kuWqoaZeg/Nrg+Q3qIR/n69wO/BpU3tRm/WnUhSKJieKYaxIKwRuTHJ8wbauz TDkzJ+LXurV00WGGK+T7EjRI1gn6TckxeRKjayKBGqvSUVG7hfVBDNKeJGMicbzjbXvK g9OCowCS7ZH4Ek+b7htYZZj98ZTRp56KFhzg3d/+bgyXSC6UBPjYTTihC3Svr1Yw7Okk M7WE8qpBNWZMTWBLPa36wjjT18dVauNVFD8FN3kWZW7HHKy+YSwhVWRxyiYnNY9wooWm pWr5nN/kuoMx89uxCYnTbzPkkDxzKKX8rtaTT0MxccTqrqkP1zdKZOWokp9oyAT4zHqZ CZbQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706164127; x=1706768927; 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=dAejTXU0ubiQ5glx+ACfMnILZ3BSyMGr/22gR94zPLU=; b=uaJi2pnNNoPtOTGXTKf04afnjvoxUPaQFh3YF/2XhufL+lZWwxNSt1fq+MX69QVZjc JbFCkHiWH3rXQ4VUTm5x6VkrY5zsAOuD4tbHtTqsrCN7c2DLGTkw2mDV6kpoO2pFG8r3 3SdPK/d+rFHmE6TsNW9NVdMNGaJZ1STj1yYVTQfhx6iUQQdgdk3371s2H9i7fA08Wqbl KQjqAF1Vg74rK1v101F0F+pwwGxtWFpjfMT/wp+f015oVeRt9fKH18wwhYw4dLvCGey+ h8/95H2d+PfaCWityxiUSgf7cZiripuLJLHfvjglttd1HNxst28paKx4YDAMwKA0Vc+P YsNg== X-Gm-Message-State: AOJu0YxTPa4EF7NAprh7opGTKX275gLdfiGEjq6on8/dU67CbJgER0D2 BVsw2cua3eVCA5Tyq1USF/tOtsJWW+B2glOn9LNvO2mGhvXa42tj+tj0c+SWW+s= X-Google-Smtp-Source: AGHT+IFqFfG67XTDjoNiouy/3B041dutuvZL3hrVwMOIb2sdzt40BlsHHzBmvSINS1AroXPOL+Y8dw== X-Received: by 2002:a05:6a20:1447:b0:19a:2e13:667a with SMTP id a7-20020a056a20144700b0019a2e13667amr750598pzi.5.1706164127531; Wed, 24 Jan 2024 22:28:47 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id t19-20020a056a00139300b006dd870b51b8sm3201139pfg.126.2024.01.24.22.28.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jan 2024 22:28:47 -0800 (PST) From: debug@rivosinc.com To: rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, paul.walmsley@sifive.com, palmer@dabbelt.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com Cc: corbet@lwn.net, aou@eecs.berkeley.edu, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, guoren@kernel.org, samitolvanen@google.com, evan@rivosinc.com, xiao.w.wang@intel.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, waylingii@gmail.com, greentime.hu@sifive.com, heiko@sntech.de, jszhang@kernel.org, shikemeng@huaweicloud.com, david@redhat.com, charlie@rivosinc.com, panqinglin2020@iscas.ac.cn, willy@infradead.org, vincent.chen@sifive.com, andy.chiu@sifive.com, gerg@kernel.org, jeeheng.sia@starfivetech.com, mason.huo@starfivetech.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bhe@redhat.com, chenjiahao16@huawei.com, ruscur@russell.cc, bgray@linux.ibm.com, alx@kernel.org, baruch@tkos.co.il, zhangqing@loongson.cn, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, joey.gouly@arm.com, shr@devkernel.io, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [RFC PATCH v1 05/28] riscv: zicfiss/zicfilp enumeration Date: Wed, 24 Jan 2024 22:21:30 -0800 Message-ID: <20240125062739.1339782-6-debug@rivosinc.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240125062739.1339782-1-debug@rivosinc.com> References: <20240125062739.1339782-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: CC3CC40011 X-Rspam-User: X-Stat-Signature: isjtfyourdasjbfo41kt1ra1b9zf5psg X-Rspamd-Server: rspam01 X-HE-Tag: 1706164128-476219 X-HE-Meta: U2FsdGVkX1+FWSU7M80bNR1xl2GdP5vxN5EsXc59vr4Hi6CtixYHX0/xDiC7v1psajU4vAiIZ+3HxvFQ4zkQ/BxfINm6gzQHE+ecbtvNszZfQRKt5jSVEnU+vP89OJj31uUhzZGvBB2pHd5dBTfV78hqRQTlzYllgBcdMsSDHlQG+PoqS6cyETKbITYETOCEx4Q9OopBKjOmgAA3LsF3fb6A15KXr3LEg0t8Avr1dUn7IJCS9tAcBbMoSoS/0iB7neLgNXNUo53HEc3qmbx/MYIjbMkEEr33OqonqzpDFgtoVEySj7oDHhrNHpi55cbheLwjkWRBwTieLAieB8I89Q7UnHKEm2ncAIPc91nyIR0q6pQEfMT7MU/J0WVms52X8kka1iRAnMioQSAEDsEfWwYLACZdjvK9eoOhhRFE12t5Afu1i5pEufW6yZ7e96XgFweA/mlh1FbF1YBeUSyqpG4SeCl4WaGQXs0wJB0rOW4VF6DOXSTg/okxLynNcmfTiydXNIlE/XU5oGe110nTVG+v6NxBqwJIBjq8PPwFJ5+LHGkqvrsSzgBF+V/P3HWGHH9YzUY3l4cYfSn3cHnsdGYKb9+/JD4Azwq/xjME4W6JxN+22IRYToTlRXa2ilbrPN+OG6WElQMRRkBX/TWxCg3dJ77zCOkUNnEfqSo00DcnxS/lJ2oY32uF7shKCWv/YjNKJXxvhTGF2GT9Md6QDGZWA8VSLs+ljKdMRgmAWVMAtguttyoUZPTX0e58JEGBy+A2JKHBCpMilRB4AkIkMUPAjCVjAL2/zSM3zhVzK8iUWdP+UQqTKLgVVJtdBeD/4Qc4v1r1TBno+/gvOS4qbTyvz0ZTax7WN7NVfGmDwujlde6d0k0f0I0uIYwkgyJaBqdSG3/ulIJ9QmQweBiOaBiTFJb57TvLK8H3IbfeZePCgKa4ytqta5nrI3XAArKLw5bmobA4qDmThi2NtG8 +IWhyuFh QuqJjSy1zO2xlq73JLWXM6Wm8VYLcAGz6tMH028dsIQxv4AAnJfWDVaJ0vZWK8lPBMNxCHR8vt7pqLuaA/UBfnVBl+hbJzw7HqnG1Fv9ZG2C1v2zkSZFk1cie/Bz80CGCm78IKd2C1Njw9xGFMRweVUjzbr+18RknL+seAtH90c809Ri4oia7K+QxQACqARQFZLJ3xR/PkTE2OmbVbgFuAo3IDwFuIbyQfoglZ1iI0P+NsSjzdhefC87d7QlAz4oXVvJLhje2B92qqYbvc21qsIYHBSaEVMxmkzRZEivT8SeA24dEUOI2DpNVmhevOBOLL5zZq62iKf/4l5WPZ4ZOFX0zNyL1/UjbCrvB9+BmmJELZxD+34UL44sUB5K9xoxWp6617arCxWjQ/CUTeamc603YLe5qhn0txzWl6VlXYDtmuBAzlkYZnrbTgl0DLGyLsvwL 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: Deepak Gupta 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 | 18 ++++++++++++++++++ arch/riscv/include/asm/hwcap.h | 2 ++ arch/riscv/include/asm/processor.h | 1 + arch/riscv/kernel/cpufeature.c | 2 ++ 4 files changed, 23 insertions(+) diff --git a/arch/riscv/include/asm/cpufeature.h b/arch/riscv/include/asm/cpufeature.h index a418c3112cd6..216190731c55 100644 --- a/arch/riscv/include/asm/cpufeature.h +++ b/arch/riscv/include/asm/cpufeature.h @@ -133,4 +133,22 @@ static __always_inline bool riscv_cpu_has_extension_unlikely(int cpu, const unsi return __riscv_isa_extension_available(hart_isa[cpu].isa, ext); } +static inline bool cpu_supports_shadow_stack(void) +{ +#ifdef CONFIG_RISCV_USER_CFI + return riscv_isa_extension_available(NULL, ZICFISS); +#else + return false; +#endif +} + +static inline bool cpu_supports_indirect_br_lp_instr(void) +{ +#ifdef CONFIG_RISCV_USER_CFI + return riscv_isa_extension_available(NULL, ZICFILP); +#else + return false; +#endif +} + #endif diff --git a/arch/riscv/include/asm/hwcap.h b/arch/riscv/include/asm/hwcap.h index 06d30526ef3b..918165cfb4fa 100644 --- a/arch/riscv/include/asm/hwcap.h +++ b/arch/riscv/include/asm/hwcap.h @@ -57,6 +57,8 @@ #define RISCV_ISA_EXT_ZIHPM 42 #define RISCV_ISA_EXT_SMSTATEEN 43 #define RISCV_ISA_EXT_ZICOND 44 +#define RISCV_ISA_EXT_ZICFISS 45 +#define RISCV_ISA_EXT_ZICFILP 46 #define RISCV_ISA_EXT_MAX 64 diff --git a/arch/riscv/include/asm/processor.h b/arch/riscv/include/asm/processor.h index f19f861cda54..ee2f51787ff8 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 98623393fd1f..16624bc9a46b 100644 --- a/arch/riscv/kernel/cpufeature.c +++ b/arch/riscv/kernel/cpufeature.c @@ -185,6 +185,8 @@ const struct riscv_isa_ext_data riscv_isa_ext[] = { __RISCV_ISA_EXT_DATA(svinval, RISCV_ISA_EXT_SVINVAL), __RISCV_ISA_EXT_DATA(svnapot, RISCV_ISA_EXT_SVNAPOT), __RISCV_ISA_EXT_DATA(svpbmt, RISCV_ISA_EXT_SVPBMT), + __RISCV_ISA_EXT_DATA(zicfiss, RISCV_ISA_EXT_ZICFISS), + __RISCV_ISA_EXT_DATA(zicfilp, RISCV_ISA_EXT_ZICFILP), }; const size_t riscv_isa_ext_count = ARRAY_SIZE(riscv_isa_ext); From patchwork Thu Jan 25 06:21:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13530076 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 2D2ADC48260 for ; Thu, 25 Jan 2024 06:29:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B41216B008A; Thu, 25 Jan 2024 01:29:00 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id AC9A86B008C; Thu, 25 Jan 2024 01:29:00 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8F5476B0092; Thu, 25 Jan 2024 01:29:00 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 763C06B008A for ; Thu, 25 Jan 2024 01:29:00 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 37D94A21F3 for ; Thu, 25 Jan 2024 06:29:00 +0000 (UTC) X-FDA: 81716855640.03.A92BE6C Received: from mail-pf1-f170.google.com (mail-pf1-f170.google.com [209.85.210.170]) by imf07.hostedemail.com (Postfix) with ESMTP id 6858D40011 for ; Thu, 25 Jan 2024 06:28:58 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=r0zcwiwN; dmarc=none; spf=pass (imf07.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.170 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706164138; a=rsa-sha256; cv=none; b=BtRAzhLR9BegsACA7xcFJ3Qg/0492LrpcBzk7fQtAI4dbWao709aORry0pLWbwtN5phl/G 1Vq8tuUtgCEs+e35mHTKl+RnyWOAyw2IJl1xBHQkrndTB2IBvPtAJqGgojhDCGAzLypVtt n0RC6KsYQiRMW7OO9KD13vdz/3zt8sw= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=r0zcwiwN; dmarc=none; spf=pass (imf07.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.170 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706164138; 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=EvIQkZQQe0K7W+Tn30d/DsUxvdwzfahlImuX+CJcznc=; b=DANoE1bbWwxm5+x97mDgXtPC0KQwBt3q0Cf0Z1I+UZd85SXXbYFuvzoejth/r+UtKceWAX 3YtiIynGGS1myc8hawgTlw1Zpfr+TKJySmGV0cBvdXwKK/h/iFcq2HacPLzvzPmxiksRGa oy92NL8VLN3SI4uF8l0eX6mQ7Z9WtfY= Received: by mail-pf1-f170.google.com with SMTP id d2e1a72fcca58-6ddc1fad6ddso959248b3a.0 for ; Wed, 24 Jan 2024 22:28:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1706164137; x=1706768937; 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=EvIQkZQQe0K7W+Tn30d/DsUxvdwzfahlImuX+CJcznc=; b=r0zcwiwNgBUS2EbYzpTM1g9+C2cYZfGMTmPhN8B+/FJfLO3PyD9WgII+X0oTH1f5f/ f8SP6RhNEZiuoc7sjKIgocbB2ebqBAPNuo1dmS7Wwbtp/OX/ST6tgcrJtJpQR4LldoTj 6zC/B7XZHksToKcQvzTTdI4eB6ZODlp2rau789Ru2/9IQgbEs2ELlIB93anN6JWdIauk S735dmX3N0/Vgr2rB34eIYwVBdA3634bfBZMbYGHB5/Hf2987ikENZN0AQ7Cmv5IumLc H9iwEG8Qp/SGxZycwUvIuaTOKAXKJ/GTOvxFhPfj0OdAOUPGpZt4k5EnhJnb8YnkZSxm bX9w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706164137; x=1706768937; 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=EvIQkZQQe0K7W+Tn30d/DsUxvdwzfahlImuX+CJcznc=; b=v99e3MfNER58myj87ptworySAqTs2QqvcwFpvSm80/5dtvYAb/Mq2tm6sHA+oiu7UF HhYYNh/l44caXwGjjrHa75ql+Js70yYT7pPbFKq0EFUn9kcrrOm9v84TeKAQu4yFB1FQ MDS5P3HcOL8OT6tSPOzaVXCK8g02wY7aWA/Q9c3WASNFukDv/cHzC4Fd7AJOeZNIpRwf QHRfbcYD0dIfUrsEaPJc9lxiW9gjZ+u8IxpL5zNYMWQsgsLF/3gpP/jqWi6FY4sNQmTd jgNW6LqKH3fxfHGW0FaLjOPWQIWmX2NJdtaJpcUe3QNy1AR+nrwyeszg+h9KNy34TcSf Ytjg== X-Gm-Message-State: AOJu0Yw3pwiFrAgrzHgLka97XnR2a6RGYjxblkR/GlSGpBQqxufqIv35 cBXsQ7/2SmcWkTcIO9cug+4bYjnORjqin+lKyxdtMpqceXYJ/9XkR/Li6rkPrfY= X-Google-Smtp-Source: AGHT+IG62+/0zfbVEYbfQN+QJI5rZ4lnUWc92boKfqE+8XigV4+ljqSZM4hwNuU+ih2eIoSJ6cAqUg== X-Received: by 2002:a05:6a00:2d12:b0:6dd:a004:c193 with SMTP id fa18-20020a056a002d1200b006dda004c193mr601959pfb.12.1706164137214; Wed, 24 Jan 2024 22:28:57 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id t19-20020a056a00139300b006dd870b51b8sm3201139pfg.126.2024.01.24.22.28.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jan 2024 22:28:56 -0800 (PST) From: debug@rivosinc.com To: rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, paul.walmsley@sifive.com, palmer@dabbelt.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com Cc: corbet@lwn.net, aou@eecs.berkeley.edu, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, guoren@kernel.org, samitolvanen@google.com, evan@rivosinc.com, xiao.w.wang@intel.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, waylingii@gmail.com, greentime.hu@sifive.com, heiko@sntech.de, jszhang@kernel.org, shikemeng@huaweicloud.com, david@redhat.com, charlie@rivosinc.com, panqinglin2020@iscas.ac.cn, willy@infradead.org, vincent.chen@sifive.com, andy.chiu@sifive.com, gerg@kernel.org, jeeheng.sia@starfivetech.com, mason.huo@starfivetech.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bhe@redhat.com, chenjiahao16@huawei.com, ruscur@russell.cc, bgray@linux.ibm.com, alx@kernel.org, baruch@tkos.co.il, zhangqing@loongson.cn, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, joey.gouly@arm.com, shr@devkernel.io, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [RFC PATCH v1 06/28] riscv: zicfiss/zicfilp extension csr and bit definitions Date: Wed, 24 Jan 2024 22:21:31 -0800 Message-ID: <20240125062739.1339782-7-debug@rivosinc.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240125062739.1339782-1-debug@rivosinc.com> References: <20240125062739.1339782-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 6858D40011 X-Stat-Signature: yngtbpcnu8aaen47drf7x3djgm76jc96 X-HE-Tag: 1706164138-949730 X-HE-Meta: U2FsdGVkX1+g8z9y8u1F0gw61fLxaC1dK/yvTDgpc6WeOFnlH6SvYfvqZv0OzTksVFMUW6B8LVk6Y1eFWrAFrH5ch58E1hIVtRDaU1h3+rChRTPZnQGMclzVkUl7jBlZkOj3eeyXf1IfjrPRb90UerjKkpq5Zndrt9H8jSRgB6uqoGixzA7pM4b0xFfB5YMQxs7+M58Ys1NkXXwijp5tqRE0Mkv0BLutpoRuO+HY+GPodlsHmSDjtE0Xu3JL1ATlZRnV3shbC1HMTysRW7BqqrUY7nx1jXeLM3pJgQdPUGA9z4JCOx1eaotDmwrfUi8WlyNvidn4e29rqxkx07UhYkgMvM8N0HLnz/epBCrW0BgdWwjYnofoaZH2O46talF4eCZR8l9fy0VRm/wyJVSkr9ljZD1/WGUesaBFRzFKNlpqf8G+9NcWh2IXxs6pTsvoJhISHEUD1P0b/x+bjOTvrrbC3gOrsVBOUMd9Afk8WmXytdWsIxm/+iGio1xb2fDkhOGn3WJ0oe+5vkFKBKWJaQav8fYI99kQjXThaOv1sstq8qzt7qqXEZCu3Un35OzV0rNb3fkiDTlEJqAMXw1n7K2+rKWwhP4/3j50sq8UIZN9ssYi1E3NwMlVMCxdMhvmXNEOmkJ9JXsX+0wCOnQ/9NKi+ARXpKyZTi71/jTr7eewEex5gHK4RTOtf1Ov7g+3UjkXogFGx0iOragVcYufzO0qcm4pzm1LBhXZCGnuG+X4NebcQdMlhmEp2+uofScUttHTRg498fe9YEeEtBF1S6oKvRUwTbzybfwk4ExzP/H2ILWaMNvkrw4ooAqxlw+F+jcFajGbZyEHC8xb9vbZYR0Xlg7c2Q8wv66mzqt4SGxyUNgCSRHUx6J0WpAS0dM5KyVdbVyyHsgXRC17DjCRiRZuHtBxiJGhUmdop5DC0/DuyFA37aD5gEaNxBSpoIBjXd579UcaA0azjtKIxTS s+c/YWBb iKiM3pGqJkd+ZEDd5Lxu+DDEcQb/+4u3IeGsk5zFqAUtZjjIAOZNIIT02+O8zTDNV26/C8Zl3DsqvyRWDE09Lc9/o5xlu/IZv4CXpZLiLIWI5PwHl6ntzQ2AOu9Ii/0PEGsv45HcnYM+/Sxdl6x7PwjGEVqyxWfu1B4rJVTJSxgJQaB3L+gYTdfK6j61nV1kla53fXJJl83ZrCEHP2CBHGlgd+S3XcYE32kDuiKm0n4TQ2keqDjSesUnhVrJ3HmQ/9WJJ1SCCWMBMgL3MzRiA18rHRmVCfDuabvhH0VLsBH2FlooeSAKJ8eN5UJHpl0MiBaV7duWb56vsFbtY70t811bp4LpKaL/ZXggf4umjcFcpOBNWVVGHBAWcWSnaFSxM9bCUxyDhTwL8vfnWSJE3NKFCEui0UXEbMjQ8OVtVvTto0dSOnKWrBjz+AL0NhdarxMkb 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: Deepak Gupta zicfiss and zicfilp extension gets enabled via b3 and b2 in xenvcfg CSR. menvcfg controls enabling for S/HS mode. henvcfg control enabling for VS while senvcfg controls enabling for U/VU mode. zicfilp extension extends xstatus CSR to hold `expected landing pad` bit. A trap or interrupt can occur between an indirect jmp/call and target instr. `expected landing pad` bit from CPU is recorded into xstatus CSR so that when supervisor performs xret, `expected landing pad` state of CPU can be restored. zicfiss adds one new CSR - CSR_SSP: CSR_SSP contains current shadow stack pointer. Signed-off-by: Deepak Gupta --- arch/riscv/include/asm/csr.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/arch/riscv/include/asm/csr.h b/arch/riscv/include/asm/csr.h index 01ba87954da2..80fe38d5de4a 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 Thu Jan 25 06:21:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13530077 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 61F72C47258 for ; Thu, 25 Jan 2024 06:29:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E72566B0092; Thu, 25 Jan 2024 01:29:05 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D895F6B0093; Thu, 25 Jan 2024 01:29:05 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B3FCD6B0095; Thu, 25 Jan 2024 01:29:05 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 9A2886B0092 for ; Thu, 25 Jan 2024 01:29:05 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 6DA3812048E for ; Thu, 25 Jan 2024 06:29:05 +0000 (UTC) X-FDA: 81716855850.29.BE1D3AD Received: from mail-oo1-f43.google.com (mail-oo1-f43.google.com [209.85.161.43]) by imf09.hostedemail.com (Postfix) with ESMTP id 962C414001F for ; Thu, 25 Jan 2024 06:29:03 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=Wf9FbnXG; spf=pass (imf09.hostedemail.com: domain of debug@rivosinc.com designates 209.85.161.43 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=1706164143; 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=rs1o7FymY4gTUwXZVJx54t4Zjv5gF4CojhNG+aQW4Ms=; b=y+l2VbNpfuhCYJnLeDjFVgwICqrQb2eW3YcjSHxc1NvGmbgp3q+XerPH7M2XmHFUlR/OaF r9Dj6cdAbqUMOCO2qvmWwpJkZ+HILHvQ7/bVHDNg8BbZrzektC/ub9koPCFW8oPL0O0zKQ 57oAg6d9wExwmfcxQXAIjFhBe/B6sTo= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706164143; a=rsa-sha256; cv=none; b=HydbyfRxyK/JDljEW5n0UBHIgnWOExRaE8A9ywUAIFJDYgG0oLBsC1HKGlx8crz9s+mrBQ RIOaoPX7ym8qSheN0t9E7psxJ84lktGvo2ibjthqYWZN6P5wMwLPbnULiZEtZ9Ky5PtSNI jzpVJSoy7mUlG9YU8YeBHt5iMn0wabU= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=Wf9FbnXG; spf=pass (imf09.hostedemail.com: domain of debug@rivosinc.com designates 209.85.161.43 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-oo1-f43.google.com with SMTP id 006d021491bc7-59927972125so3173935eaf.3 for ; Wed, 24 Jan 2024 22:29:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1706164142; x=1706768942; 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=rs1o7FymY4gTUwXZVJx54t4Zjv5gF4CojhNG+aQW4Ms=; b=Wf9FbnXGX8ehBVRWbvliEzD5Nhc4hBG2jkrNF864+hCXukySPxquNLi/BTs3jBxWkV 0n0lm8WRLZUk9J4STgRrFscB9l6zPEseRXoOv8kVusy6cJPk7NjxnxOGZxdDCCe+QMZh lHTphb7KyRIrCjSsDyaaCxoWoOvd3i12+351Xc/S1tf735BEG0m49GCtT1/GqbNSujlf owcQRgSPOYDz5OczBGVevjHG/JHU0pknUZ1z9Ryu/RDrPZO+cKJniz0773UUrjoy/CXJ sYP98YInIma9yaGJj8qGSaEAPH7UxmY15JEmjADRLdMJgNUvtaT8lG0nezGt/gr63WXY EUsQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706164142; x=1706768942; 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=rs1o7FymY4gTUwXZVJx54t4Zjv5gF4CojhNG+aQW4Ms=; b=vEv39g8wLeZcS0m/vPLPx8OaGbdEe5dsXgi2RW7fDaI6mEdhERNBR1GOQuN+w3xSfm 4cZ6/KfPTKq1vu6uFOyl/5bic7v6CZ8jmTDArOqxN/IGL6epfPu+RTajAc92JuQYNbMk Mm+c01PAJT/pJf3ZOStInq99GMGAEk6XVbBOASGPFd3GOjDYCmiAN3ws2RMBULTmMJsq aPVeHnv6ReAW4qjjZcg8mADimCqC+/rggF7ko47rulxIPC9jjmsWpdx9+txfsHbjmz/k F3cps7N21x/v2ef5IXGrYjIleUGCcxHrryeGMuJMgOLqfGAVHb772BfJ/a/VLVa3gHQK NpBQ== X-Gm-Message-State: AOJu0YypSW2wMrA0ksnt89cJSZvB+VsT9MKjei3KORP35lPP8ya0pYet YAAUGhqwYSN01b79uIHs7sKFHFIc/wMU6ly7OJU6ERgtGcKq62cfmTf8HlxjrqM= X-Google-Smtp-Source: AGHT+IGQZnueATOgDHrwaXrd5AWfHpxvbbHwGR+jLnJrGj0m/f+oWOE49v8FM+hbtn4VeA1L1U6wQQ== X-Received: by 2002:a05:6358:d388:b0:176:5cad:5144 with SMTP id mp8-20020a056358d38800b001765cad5144mr620289rwb.45.1706164142630; Wed, 24 Jan 2024 22:29:02 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id t19-20020a056a00139300b006dd870b51b8sm3201139pfg.126.2024.01.24.22.28.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jan 2024 22:29:02 -0800 (PST) From: debug@rivosinc.com To: rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, paul.walmsley@sifive.com, palmer@dabbelt.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com Cc: corbet@lwn.net, aou@eecs.berkeley.edu, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, guoren@kernel.org, samitolvanen@google.com, evan@rivosinc.com, xiao.w.wang@intel.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, waylingii@gmail.com, greentime.hu@sifive.com, heiko@sntech.de, jszhang@kernel.org, shikemeng@huaweicloud.com, david@redhat.com, charlie@rivosinc.com, panqinglin2020@iscas.ac.cn, willy@infradead.org, vincent.chen@sifive.com, andy.chiu@sifive.com, gerg@kernel.org, jeeheng.sia@starfivetech.com, mason.huo@starfivetech.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bhe@redhat.com, chenjiahao16@huawei.com, ruscur@russell.cc, bgray@linux.ibm.com, alx@kernel.org, baruch@tkos.co.il, zhangqing@loongson.cn, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, joey.gouly@arm.com, shr@devkernel.io, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [RFC PATCH v1 07/28] riscv: kernel handling on trap entry/exit for user cfi Date: Wed, 24 Jan 2024 22:21:32 -0800 Message-ID: <20240125062739.1339782-8-debug@rivosinc.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240125062739.1339782-1-debug@rivosinc.com> References: <20240125062739.1339782-1-debug@rivosinc.com> MIME-Version: 1.0 X-Stat-Signature: skw57858zggf9ct3ew3sxidp5q7a95w3 X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 962C414001F X-Rspam-User: X-HE-Tag: 1706164143-783453 X-HE-Meta: U2FsdGVkX1/CeIrFpmkRyaf1zNZockb2sg1qq84YEvE4GF57LFNDFtD4LrB7z+QYm5hYjn8gZMnC2uEWno6WHHVE6EXb+bSBEeBvuoOxGiQ4KNrAFwK+zAWiPrPPRhZR9bUUUYbu5BaGCVGcxz4VLsC/MU7/2vPQq9a4C2gIIt7+kgvyVT6QBuMKN8f6HYKEJE1i94J0Fmu29oqHMnc0hgFF1+HrtVgZN9uBm5ATlVDGG5e7wghBwPqX58MGjg4fcXxQ2V3kZzKJndbapIoXpgCfMHxl8kthZsdY82xDeXuS7l4ZrOPMS5zlTZGXZvyG4kZlnooqRTTro2MI5eSUQ4ATEU6gr0UVSAmYk3IiF4flCf4lRPLRhHYSHeLDhdPpBPcDkyuZfZW1dlGtbwirgouSj5tzgFZEtOChAzM4nKt7ebLh1tsyAHggnzdRQ1f2yTzIyiSf8T64Vmxyy/PvojRJb+AvJW6KK7AQsAE598GnUifi/qIjcPG//D/dCprHFJHfUvVgxHo8h0YrYI0zC82vzomWslWT+c8takbWbOj++zUNBbrqYwC4oXi8u+LokhmKiA5d/H6EQ5PlI4jLtdyK15zdGRETsWVy9U9arEUJld1P1zb8AzvGvp7j6vWXPJEpXarC6eF3HIBMVq+JF91pbvOiY+GhW3UFmUgww5gbCIeKoYZDHcITAIuUdUXVesNogyBUnw1fFPdgEYMxQd5312sy9tvfnh11v5FYeyGMUvq93mulBh/gm3AcOqtxcy4jNXvPuHVSWYvxCNEYjftzyiYISuER3PIyAaw9qAlyc+vvDwcKyD0laVKfWmJwU1mSuoXJzB4J4+25gNA/kq3eP74ZYgoEkdtjyJjQnNWo0x4xDSknUnboDWWWoX5/sMf95+r6ASR+4z58xSFe/Zj0Hg0o2Yug/nveJiojxSAdTXt8DgjxKvGIFP8YJkSCiDZXhyrrurzjFpWotGk j4j89sUm pXqIzDakZctO8V3g3sDh3aoiDGHMPTLKR8s9eKPPbHeYrL1nZfRAmm3DmascoEfRzPw1tapYVbk4SyGRUltu+bgfvh17dd6+Ug9l1jth2hu35khUTKcBurCkf0FeQHqIvYT7Y83IiYqciZ+9949LkuOT2jwy7okfeO4k6pct+v2GraMBYGDPFkGkZ2PWuXxkxqW+MTDtesq64Bfr0+fWnhbBuRlQHc0QYiV0uIRr2p8NyQ5fSyAJOBsxa6QRkGQObAGlrN6m4H46GAXqpErgNViPxm1SXWE8xBw9reiiepSq8CBYe34e6JgQAOrTbCjbvW9Lf0ECip5Tv5eS62qJXrDAM6MYX42lfjXwLyBD9fCZ7tg0tDicAA0Lfetu2+jFmVLQKUQXhWAMhY2AmKW/zP/XJM0obKEehpf3b0pzBsUJGaFIjCi+nsZt6BnRKSX98kqf+ X-Bogosity: Ham, tests=bogofilter, spamicity=0.000001, 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: Deepak Gupta 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) Signed-off-by: Deepak Gupta --- arch/riscv/include/asm/processor.h | 1 + arch/riscv/include/asm/thread_info.h | 3 +++ arch/riscv/include/asm/usercfi.h | 24 ++++++++++++++++++++++++ arch/riscv/kernel/asm-offsets.c | 5 ++++- arch/riscv/kernel/entry.S | 25 +++++++++++++++++++++++++ 5 files changed, 57 insertions(+), 1 deletion(-) 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 ee2f51787ff8..d4dc298880fc 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 320bc899a63b..6a2acecec546 100644 --- a/arch/riscv/include/asm/thread_info.h +++ b/arch/riscv/include/asm/thread_info.h @@ -58,6 +58,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..080d7077d12c --- /dev/null +++ b/arch/riscv/include/asm/usercfi.h @@ -0,0 +1,24 @@ +/* SPDX-License-Identifier: GPL-2.0 + * Copyright (C) 2023 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 cdd8f095c30c..5e1f412e96ba 100644 --- a/arch/riscv/kernel/asm-offsets.c +++ b/arch/riscv/kernel/asm-offsets.c @@ -43,8 +43,11 @@ void asm_offsets(void) #ifdef CONFIG_SHADOW_CALL_STACK OFFSET(TASK_TI_SCS_SP, task_struct, thread_info.scs_sp); #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 63c3855ba80d..410659e2eadb 100644 --- a/arch/riscv/kernel/entry.S +++ b/arch/riscv/kernel/entry.S @@ -49,6 +49,21 @@ SYM_CODE_START(handle_exception) REG_S x5, PT_T0(sp) save_from_x6_to_x31 +#ifdef CONFIG_RISCV_USER_CFI + /* + * we need to save cfi status only when previous mode was U + */ + csrr s2, CSR_STATUS + andi s2, s2, SR_SPP + bnez s2, skip_bcfi_save + /* load cfi status word */ + lw s3, TASK_TI_CFI_STATUS(tp) + andi s3, s3, 1 + beqz s3, skip_bcfi_save + csrr s3, CSR_SSP + REG_S s3, TASK_TI_USER_SSP(tp) /* save user ssp in thread_info */ +skip_bcfi_save: +#endif /* * Disable user-mode memory access as it should only be set in the * actual user copy routines. @@ -141,6 +156,16 @@ SYM_CODE_START_NOALIGN(ret_from_exception) * structures again. */ csrw CSR_SCRATCH, tp + +#ifdef CONFIG_RISCV_USER_CFI + lw s3, TASK_TI_CFI_STATUS(tp) + andi s3, s3, 1 + beqz s3, skip_bcfi_resume + REG_L s3, TASK_TI_USER_SSP(tp) /* restore user ssp from thread struct */ + csrw CSR_SSP, s3 +skip_bcfi_resume: +#endif + 1: REG_L a0, PT_STATUS(sp) /* From patchwork Thu Jan 25 06:21:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13530078 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 3A4FBC47DDF for ; Thu, 25 Jan 2024 06:29:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BDF1C6B0095; Thu, 25 Jan 2024 01:29:12 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B66876B0096; Thu, 25 Jan 2024 01:29:12 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 996CB6B0098; Thu, 25 Jan 2024 01:29:12 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 7E4C86B0095 for ; Thu, 25 Jan 2024 01:29:12 -0500 (EST) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 4AEDE8058F for ; Thu, 25 Jan 2024 06:29:12 +0000 (UTC) X-FDA: 81716856144.16.04BC42E Received: from mail-pf1-f180.google.com (mail-pf1-f180.google.com [209.85.210.180]) by imf21.hostedemail.com (Postfix) with ESMTP id 80C1A1C000D for ; Thu, 25 Jan 2024 06:29:10 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=R7DNYqJw; spf=pass (imf21.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.180 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706164150; a=rsa-sha256; cv=none; b=3Q5Dn+vTSpzN3ObiIhLdUERj2sCTNB+RNpl1QShxRQwVxgpAS4C0D/iBIolaOhPRaGFGOK wIVh2yI8DzO4iMe6bbDOf3Ko+mLLYFkZ0Tk1AnHe7jtU9nvEs12zHlKXaRm6tIW+DqpbUj VOotmZzTqAwsusIC80y2rvghGjE2g6E= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=R7DNYqJw; spf=pass (imf21.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.180 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706164150; 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=uqhJyw4qKw6ttxWAjpEt4kkwqgUPwpH+MlNDa4SEbVQ=; b=wfq071xJ2XptcjYRTw87OT+AVvjNqiod9ey+HpVE4V5Tu/2xw3EjzCaGzOklLjnyj7HTN6 z9StXxayxCO5Vs1C5EUkujR5Hhkl7E8I8jQ/mnlZNuiWdJMQX9jN6bDnRHJb/MrA3sZytq OP4Hiy9m3glMMS0q3UzV3ZAoj6K5r8s= Received: by mail-pf1-f180.google.com with SMTP id d2e1a72fcca58-6ddd19552e6so80346b3a.1 for ; Wed, 24 Jan 2024 22:29:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1706164149; x=1706768949; 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=uqhJyw4qKw6ttxWAjpEt4kkwqgUPwpH+MlNDa4SEbVQ=; b=R7DNYqJwZvOygbLEwvhTmxw3iO+bMMAWmwYSCSo1du123iDm0Nl8GHv0QrdQT1ldDz swoUJ+MrbPvVfWQVOMi3lrADtl8bM96AoEE+1e6UiWPk6rzsJPHP0QrtrdFce9asiymI sDUXjgwI9Li3xgQRJhObYIKONZQd/d3GXtJBDvCEzkXUBdOM8ghmpwBfLJnGvUiVQECi cEoIljglXxyUELWZwhIx9lVjY/lJtjs0BvK2g5PKThVPqy/UkW2MEt33JoDXRHS3TGfe egQrymvEv0moQclfr6fbDo3r50G1DfJYnFcR3tWi3IipkA6Cqz4wSa+tXWz5GmvO2EzN GsMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706164149; x=1706768949; 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=uqhJyw4qKw6ttxWAjpEt4kkwqgUPwpH+MlNDa4SEbVQ=; b=TeDkVp3r7Vx4Mkd7GwIQtd3q1LS03FXREgO6iCHsafy3DBsEdn6duEms7Lk1uqxKiS XLkVKfm6AjsYRKYrTTp+mxkVbSkCjPcjdApBgqa/dqB9ldDqweMpuRVT275n0jBiIoJb xfZ03g0kRnxFauB4UKZi/6QIjppXXZXwL607xInz4QJK3ZPn79e+8uByb+4aOXJn3B/A 9Ejk/YvzsZpCwU+SmYp0xIiAyooV0M9j+9haRaNa2JvWC3cP18tW6U6JA+za236s/Qh1 Otx6KMyFF3lmE7nz5Kgu9cmMQ+2eVWt942YscD53vcCvPxX4OyODKzvqd9E9yWYJwRTe vJzA== X-Gm-Message-State: AOJu0YyNz5NeAlPCLFu5oxxlhr9J+l0mNMRWT58XpeZWtKEdd8VMUhDQ iNE3hRi8m4wi8R8P+1t4zRxPS4W5oJ9XxrtlC3q4bVeN+Oj/w8KrAIai/1oXrZc= X-Google-Smtp-Source: AGHT+IEl63VLMXT2mHqAciM7L/Xopf+jIyT34V6he9kHeUuUlxwxwREqJzJ6MMes7H6nQTQcNzI7AA== X-Received: by 2002:a05:6a00:2194:b0:6dd:c69f:1e31 with SMTP id h20-20020a056a00219400b006ddc69f1e31mr202939pfi.51.1706164147768; Wed, 24 Jan 2024 22:29:07 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id t19-20020a056a00139300b006dd870b51b8sm3201139pfg.126.2024.01.24.22.29.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jan 2024 22:29:07 -0800 (PST) From: debug@rivosinc.com To: rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, paul.walmsley@sifive.com, palmer@dabbelt.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com Cc: corbet@lwn.net, aou@eecs.berkeley.edu, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, guoren@kernel.org, samitolvanen@google.com, evan@rivosinc.com, xiao.w.wang@intel.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, waylingii@gmail.com, greentime.hu@sifive.com, heiko@sntech.de, jszhang@kernel.org, shikemeng@huaweicloud.com, david@redhat.com, charlie@rivosinc.com, panqinglin2020@iscas.ac.cn, willy@infradead.org, vincent.chen@sifive.com, andy.chiu@sifive.com, gerg@kernel.org, jeeheng.sia@starfivetech.com, mason.huo@starfivetech.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bhe@redhat.com, chenjiahao16@huawei.com, ruscur@russell.cc, bgray@linux.ibm.com, alx@kernel.org, baruch@tkos.co.il, zhangqing@loongson.cn, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, joey.gouly@arm.com, shr@devkernel.io, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [RFC PATCH v1 08/28] mm: Define VM_SHADOW_STACK for RISC-V Date: Wed, 24 Jan 2024 22:21:33 -0800 Message-ID: <20240125062739.1339782-9-debug@rivosinc.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240125062739.1339782-1-debug@rivosinc.com> References: <20240125062739.1339782-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 80C1A1C000D X-Stat-Signature: nazmympwwgnxqtjnkdpnhcju9rduyfbo X-Rspam-User: X-HE-Tag: 1706164150-523253 X-HE-Meta: U2FsdGVkX1+XSM1pyFmvv6BvHC6NvvQyMhTF7BAhan0pOsxqyigm6YuSFsULro9RkIrBIMCXZpuigKoWhFJybSBp8Sqm7CEBw5+SdO8oV6zS+XIuc/LpMjaaOuku+kGutO5x0OoLkILu0+plDjRg3SBRE1yyHzQtFgPIHpcVR3AAlD+jG00iGtGvoRfe3/wIHrht2kabR5VQZuEBmiRGJk/pYbiC+BL9j7pUOwBQD2osqmEm7/Ky2OXFnUJfTYqXBSO1FMljC4lv5PA7xMXQdq9V2228mksk6b6xugTOs6vaYy78L4IXlBDKzdp4H4duKqQ/gh249nbPqoePJgtltqswRa1HdfGV167ujeiov9s3N4I7gAcZDpyIG9UJRs/HwI5AAxo1iXvDiR7rR/Z/JBRBagkMISqWRTizItyjngQHK3jTvDy/cQKDVVfydif8ZqykWpjjWm90ZvbZsuj9C6AeNU1VFO1fGh4yd6AhsRhxFyIV3hckrXxWo4v7e/wVTV22yObQk4wKkWMXfu4r5XCWkuJ1hzNWa0ePZggetShp6Pqht9hU4ICGzNExBlwIKmOy6v+mi87bakmrcNLiCcYYjpkoBOWx316GxHTb4VD6jXqvIx+kLpeDpiT9r46r0bsDI3mjPkSuhdx4QTWJEar0kaDO2PIkzgIjNPPXZWO/mgXr9pHEQj6w8hRYsuW506KBv4xaeCWtT/482aNTRAPD7e/IYA9aGE7F1/mQsJ2wuWw6DgsXouD3lYY7Tgk1dxrCr/MgSR9C+Bz5gKHyXaL2iBaWimYBKWe+4kLc6lZe0Go8WnKlLW94dFeg5S4WTIslPdOY6gGZLXJHoZkslbN9uT6dxXnTl2Aypx99MjxjWvjfLCrpJIOxOEv0oVZZgGQrG/2tllvu06GAUXW0PDSVfNRoSRN/1826vsci8c7JLygbPIYd9gCG8J0A5I82B2LkU3ZN51u/hTZVWz0 ZuIAnPrX wL0gwwtqFLuVtGiU4o/vdownF1uO0kDcVGGx5GC3x5PAF/RRplO3R8d7Ro5GKX+mbhcnTE4/IvsSmwrJj3rxgESaPLK4SkOh6j+LAKGFqKpQ0Br6H5Lzor8Ky0Qot/DJRbmzuwIbfSfOJkXY0jj1A5oDieMsNEATv4v/8Ew1/T5EceaoANzdcGfDkxxkNmaaydXME9TuDt18mnof7BokMYbfqeppPQplZ/mJTcogPgb6m0iYwwnt8cGeTNNB4Pac+qyg0wblEe9BX6/WioEnrlMb6dSR2F8fMP69jqwjxfz9S3nFvybgtvmf0Yfh4tE4h5SEb4X+bC54aLRVjJLldAeB3zSieCIILGxQoqTfCDKQmraY1xzZN4zOGr33tOtHVNpQGjXEl6NpizN/xBDWMrWYHnBZ/aG/UVc0nidd73xkuYBUEhcGPvSK9p3Oeop6B3/oPpkj6W90G0ooVKA/ZgTDK6kcj3Wm8mVAL8o/ybEZvKnLyX6GLsmr3SrLqo+FeM0HbGRibxhgK4GdafjmUPnNb1Dat+e8NbroVT2fXo9bB/54= 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: Deepak Gupta VM_SHADOW_STACK is defined by x86 as vm flag to mark a shadow stack vma. x86 uses VM_HIGH_ARCH_5 bit but that limits shadow stack vma to 64bit only. arm64 follows same path https://lore.kernel.org/lkml/20231009-arm64-gcs-v6-12-78e55deaa4dd@kernel.org/#r On RISC-V, write-only page table encodings are shadow stack pages. This patch re-defines VM_WRITE only to be VM_SHADOW_STACK. Next set of patches will set guard rail that no other mm flow can set VM_WRITE only in vma except when specifically creating shadow stack. Signed-off-by: Deepak Gupta --- include/linux/mm.h | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 418d26608ece..dfe0e8118669 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -352,7 +352,19 @@ extern unsigned int kobjsize(const void *objp); * for more details on the guard size. */ # define VM_SHADOW_STACK VM_HIGH_ARCH_5 -#else +#endif + +#ifdef CONFIG_RISCV_USER_CFI +/* + * On RISC-V pte encodings for shadow stack is R=0, W=1, X=0 and thus RISCV + * choosing to use similar mechanism on vm_flags where VM_WRITE only means + * VM_SHADOW_STACK. RISCV as well doesn't support VM_SHADOW_STACK to be set + * with VM_SHARED. + */ +#define VM_SHADOW_STACK VM_WRITE +#endif + +#ifndef VM_SHADOW_STACK # define VM_SHADOW_STACK VM_NONE #endif From patchwork Thu Jan 25 06:21:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13530079 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 8F4B9C47DDF for ; Thu, 25 Jan 2024 06:29:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2405E6B0098; Thu, 25 Jan 2024 01:29:16 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1CAC66B0099; Thu, 25 Jan 2024 01:29:16 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EE9936B009A; Thu, 25 Jan 2024 01:29:15 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id D744A6B0098 for ; Thu, 25 Jan 2024 01:29:15 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id B2E8F1C1663 for ; Thu, 25 Jan 2024 06:29:15 +0000 (UTC) X-FDA: 81716856270.18.2C5EF3D Received: from mail-oi1-f174.google.com (mail-oi1-f174.google.com [209.85.167.174]) by imf18.hostedemail.com (Postfix) with ESMTP id E7F551C000D for ; Thu, 25 Jan 2024 06:29:13 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=sLmY6uWC; dmarc=none; spf=pass (imf18.hostedemail.com: domain of debug@rivosinc.com designates 209.85.167.174 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706164153; 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=T6jrxQTzk5v6yDwRhrNBVQOQ/gk4SzlXhOza5budY2g=; b=u8TJDQsKUfS6+/kvADRIc72YdkLyqrz9PAw00ODJcgdknxqeYMhvYl7A1WEsqlDWykpS3J YopcYgPDr79PRG+3J2um3t0SpjKndRkP8xHOEsWhM/LoalfHtKSe61liQC2xlsZtfu1VSw 74P13rbm8ymz7eiDT1BuWph7UGCG/l4= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=sLmY6uWC; dmarc=none; spf=pass (imf18.hostedemail.com: domain of debug@rivosinc.com designates 209.85.167.174 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706164154; a=rsa-sha256; cv=none; b=aPo6icBsQq1pUK6JhElNUmOnP2wNLcVV7lGWf3ify/RNGpREhV4BnkN3bB+oIaHEdA2Um4 7p2CZcfa7mhE9T/LrH4CPqUxoo0p1bwtPjscqQCrUARiB4Gw2SlDFfELrcLCl5Ya3y1Vmj Dewt/3O7lbg9aUyK9oYE8SyWHY20mR0= Received: by mail-oi1-f174.google.com with SMTP id 5614622812f47-3bdbf401bd3so2359227b6e.0 for ; Wed, 24 Jan 2024 22:29:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1706164153; x=1706768953; 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=T6jrxQTzk5v6yDwRhrNBVQOQ/gk4SzlXhOza5budY2g=; b=sLmY6uWCwdPoJdTzoHc7DXo/HkgvgkJpy2pin7E+XwEWLfOyxTKPAuw7Cq3TVXD0I7 9ig/C6oNuepcW2ZmX6v5JEtFEaD4b6HOF8femAokbFMwMonWKu2CSannSHlAUPDRozlx p94dWKuV6NEP4QW5sx8t24hi0qt0dmJDAAw/yC1Tn0gOQKWMq9RqsJV8YZQ+9bUidila SCb3pLBDiyx1rO6AZ5ua2OfipAIaEMXHD2LbkWSfa+s8NR3xTohZ8eJxMr1O51QjlVd/ SGlzjdReob+TXl2qlkpQywguTQWO5ZL3Rz9S4oeve+VZvMxF9xTDAlnN0WXvqKwdM/5b 8dVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706164153; x=1706768953; 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=T6jrxQTzk5v6yDwRhrNBVQOQ/gk4SzlXhOza5budY2g=; b=CvU3J9twLhJK8gOpj7rXPGqILiXwkYWtdYnE+6+ay7VdnrYE5AC7NzAEGo8j1CXZt/ NfAaKUv+tL+9as2CnK+u7/s7BaFbyHEDvf0CB4jelN+2pC4VY1Qlg4DnuoopjPK5o+n/ ajr2aU1dVlwL+p196HozxbL2BIkfd5hI6DRXKLaG8wqJEdI5kM+c+9JnZ04xtxkEgr8C NNXORNeOjFxH49EkXvrxoRHkoFFPhx1PPjpHOlOqs4BfuCLG1CTuZMjsk0TWSRCC+KvP n0hheYsrcFZo494X/2Xasb3qM7hRwPiq3JpBRMmMdTEHZtX7B9BZuMQUneES6D1dGDQR c6iw== X-Gm-Message-State: AOJu0Yxa5OGVnsW44ibCoorFTjJm+efgUG7WTvL+RqE1TUEE/809n3lN EXuUiOSOoOukpqcAO1sf3QsOT3ukjL7tjGCuSXYpZBmQ47LFBZ3KHKxinVZiaT8= X-Google-Smtp-Source: AGHT+IE9c8MEdVglNRelh8ZMz256rscxIEtK+0cPNlGjBMQYH53mysOGbUh9rlxjO/qpIC+YDleATw== X-Received: by 2002:a54:478d:0:b0:3bd:be66:c0c7 with SMTP id o13-20020a54478d000000b003bdbe66c0c7mr441553oic.98.1706164153039; Wed, 24 Jan 2024 22:29:13 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id t19-20020a056a00139300b006dd870b51b8sm3201139pfg.126.2024.01.24.22.29.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jan 2024 22:29:12 -0800 (PST) From: debug@rivosinc.com To: rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, paul.walmsley@sifive.com, palmer@dabbelt.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com Cc: corbet@lwn.net, aou@eecs.berkeley.edu, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, guoren@kernel.org, samitolvanen@google.com, evan@rivosinc.com, xiao.w.wang@intel.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, waylingii@gmail.com, greentime.hu@sifive.com, heiko@sntech.de, jszhang@kernel.org, shikemeng@huaweicloud.com, david@redhat.com, charlie@rivosinc.com, panqinglin2020@iscas.ac.cn, willy@infradead.org, vincent.chen@sifive.com, andy.chiu@sifive.com, gerg@kernel.org, jeeheng.sia@starfivetech.com, mason.huo@starfivetech.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bhe@redhat.com, chenjiahao16@huawei.com, ruscur@russell.cc, bgray@linux.ibm.com, alx@kernel.org, baruch@tkos.co.il, zhangqing@loongson.cn, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, joey.gouly@arm.com, shr@devkernel.io, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [RFC PATCH v1 09/28] mm: abstract shadow stack vma behind `arch_is_shadow_stack` Date: Wed, 24 Jan 2024 22:21:34 -0800 Message-ID: <20240125062739.1339782-10-debug@rivosinc.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240125062739.1339782-1-debug@rivosinc.com> References: <20240125062739.1339782-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: E7F551C000D X-Stat-Signature: fjdnr8ye6zz6k1b8gy93a3xqczrexei6 X-HE-Tag: 1706164153-581664 X-HE-Meta: U2FsdGVkX1974rRnuHY/rVtKwiLrfokHL+MS6XcFtpran6VLAZ5GrhTDRFk/fxhZcKKIcrsE6F8J48bV9ZphKvptpMA20b7JgX15EzfMXimFRbA/B3FP0vhEK1yAg07Uc2bbotIiX9nyiQcXZwFNyx9Ioa2sMm2K4rW8wMgADMiQqyzvdgxiHnYTO48MG8jzU133FN0kCT52aUdrzTZBtn20IlAhWqSljbp2ToNgyXxeXgTGhiGBELMNw1rrHMd4dZ6Z2gb7JYGeiVObRkm0djRtVh8U5MZNLOngbr7AAUUTKok0LjbUniHdHqMPlr8C7uZedrRPmvlvAK6ZKN6AoA7IFr3vr9kyu0sWWLSYwhJqvjIZSA6F5H9ph5gtphC2UYOyAqLDrEp8bh9wBcZ0G0Pc0oFJAqj89ABXa1t62jbCN9OWtSSeArBXIsX5eXBAEKi/rphUA/q36WZCAlFCnzwM9DnX8MdFwcsLd6ML8K/P8PGmZ+vUbRLs9Ho4sSxqosLom6j7yMg8dIgHNmVsEp69x1l47r813bEe5h/lqCTyjfNVIzuD9bsR3S5cxXDYg8IVtv8DMdC/GmCyBC4evKPK19HVOo3jJ/69/EWpw+UQAFTTr9+Zs5kiKbUkD234QMxjb40IGArpl/ET3k4O5tHzpKs+dzs1Q0h0ifdF5XF20KA/bF+TEtXMWUKbPwKBzPBUMFoUlqAqGrw0gJR6TBbIdhYqQcvyGr7hMweGae0tCCGiNK6F/Z55icYAuzu5M/s8mmeqTNXGkklQuFnsFXWOhQUO6HwLmZP33twiBbdjVvXWp6oI6JyR/lZDtL9EAwEtwIbDwzAUwbw/MLR/D+mkXL47uy10HFKfJMq9LZRp0qFu7oEXF8dwgwlZ+yfVzQjKab3Fq3UBosk4PW3NA/2Y8jrO1K8fS5+Okc6HNYXADSfUAYR6smdhBpB+fcbr0q9CLPIOnK61tMqzagd W+xuQcwB E9Fu832fiHi12YHOl/j5nUJq0tAMzREhf//Hp/yNsF+IlyHgC1K6Dz9fqVPlteeQrcBi0yM9a/qgij3iPvSiAT7/tBAcyntQH7O9QkcDl2Nk63NR3Onpm8SKGh89G3fKnXB718HYZPWQej0+zNdYOBBiOEiKnRjK6nPfcdusf7rOSkhfiLStFCGNdDKlDtDj+uiiMj14fAFn3I9qC3SlxHQJxOaWG2zKntfuNH5aeTwoxmod8yPm0S3va4TImdMwAwC6rUT3oRyiVjmQ12klXHVB4q12Xf2tPv8LpBz8jWSpSNqBr6FlajBQoR7CTU3Shb2E2HKbLrXnFwrl3QhLFnLefxwa7ZvqIc9Z38c0efaQ4Msq5j/n1UX0ppYikWXvCDnJMQUhh6eA40Hx/pXIjvS7h5JwqyYhz8YhNWrn6mU+T4TR+vJG0wJHMJWz8A01gP2THs3E0X6gAz0kBCm9XIpZ9gYrx1+l6UsG6rL5b+EYlqnOcmvjkfpcPsQ== 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: Deepak Gupta x86 has used VM_SHADOW_STACK (alias to VM_HIGH_ARCH_5) to encode shadow stack VMA. VM_SHADOW_STACK is thus not possible on 32bit. Some arches may need a way to encode shadow stack on 32bit and 64bit both and they may encode this information differently in VMAs. This patch changes checks of VM_SHADOW_STACK flag in generic code to call to a function `arch_is_shadow_stack` which will return true if arch supports shadow stack and vma is shadow stack else stub returns false. There was a suggestion to name it as `vma_is_shadow_stack`. I preferred to keep `arch` prefix in there because it's each arch specific. Signed-off-by: Deepak Gupta --- include/linux/mm.h | 18 +++++++++++++++++- mm/gup.c | 5 +++-- mm/internal.h | 2 +- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index dfe0e8118669..15c70fc677a3 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -352,6 +352,10 @@ extern unsigned int kobjsize(const void *objp); * for more details on the guard size. */ # define VM_SHADOW_STACK VM_HIGH_ARCH_5 +static inline bool arch_is_shadow_stack(vm_flags_t vm_flags) +{ + return (vm_flags & VM_SHADOW_STACK); +} #endif #ifdef CONFIG_RISCV_USER_CFI @@ -362,10 +366,22 @@ extern unsigned int kobjsize(const void *objp); * with VM_SHARED. */ #define VM_SHADOW_STACK VM_WRITE + +static inline bool arch_is_shadow_stack(vm_flags_t vm_flags) +{ + return ((vm_flags & (VM_WRITE | VM_READ | VM_EXEC)) == VM_WRITE); +} + #endif #ifndef VM_SHADOW_STACK # define VM_SHADOW_STACK VM_NONE + +static inline bool arch_is_shadow_stack(vm_flags_t vm_flags) +{ + return false; +} + #endif #if defined(CONFIG_X86) @@ -3464,7 +3480,7 @@ static inline unsigned long stack_guard_start_gap(struct vm_area_struct *vma) return stack_guard_gap; /* See reasoning around the VM_SHADOW_STACK definition */ - if (vma->vm_flags & VM_SHADOW_STACK) + if (vma->vm_flags && arch_is_shadow_stack(vma->vm_flags)) return PAGE_SIZE; return 0; diff --git a/mm/gup.c b/mm/gup.c index 231711efa390..45798782ed2c 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -1051,7 +1051,7 @@ static int check_vma_flags(struct vm_area_struct *vma, unsigned long gup_flags) !writable_file_mapping_allowed(vma, gup_flags)) return -EFAULT; - if (!(vm_flags & VM_WRITE) || (vm_flags & VM_SHADOW_STACK)) { + if (!(vm_flags & VM_WRITE) || arch_is_shadow_stack(vm_flags)) { if (!(gup_flags & FOLL_FORCE)) return -EFAULT; /* hugetlb does not support FOLL_FORCE|FOLL_WRITE. */ @@ -1069,7 +1069,8 @@ static int check_vma_flags(struct vm_area_struct *vma, unsigned long gup_flags) if (!is_cow_mapping(vm_flags)) return -EFAULT; } - } else if (!(vm_flags & VM_READ)) { + } else if (!(vm_flags & VM_READ) && !arch_is_shadow_stack(vm_flags)) { + /* reads allowed if its shadow stack vma */ if (!(gup_flags & FOLL_FORCE)) return -EFAULT; /* diff --git a/mm/internal.h b/mm/internal.h index b61034bd50f5..0abf00c93fe1 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -572,7 +572,7 @@ static inline bool is_exec_mapping(vm_flags_t flags) */ static inline bool is_stack_mapping(vm_flags_t flags) { - return ((flags & VM_STACK) == VM_STACK) || (flags & VM_SHADOW_STACK); + return ((flags & VM_STACK) == VM_STACK) || arch_is_shadow_stack(flags); } /* From patchwork Thu Jan 25 06:21:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13530080 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 3FFDEC47258 for ; Thu, 25 Jan 2024 06:29:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B1A306B009A; Thu, 25 Jan 2024 01:29:31 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id AA32C6B009B; Thu, 25 Jan 2024 01:29:31 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8F6116B009C; Thu, 25 Jan 2024 01:29:31 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 7C0036B009A for ; Thu, 25 Jan 2024 01:29:31 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 5E35D160D17 for ; Thu, 25 Jan 2024 06:29:31 +0000 (UTC) X-FDA: 81716856942.23.87973A9 Received: from mail-pf1-f172.google.com (mail-pf1-f172.google.com [209.85.210.172]) by imf13.hostedemail.com (Postfix) with ESMTP id 8E7F020008 for ; Thu, 25 Jan 2024 06:29:29 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=VUlVBavM; spf=pass (imf13.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.172 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706164169; 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=9N8f26gdn2cRJikP5qLVCVRN7EIk5o+zwHdVnNZOBlE=; b=d9/16EK0c05bx2vncwhuUPzV0aOR5AvUrcC4O6wz7+tP5xrA5Ocs2gG3eQnt68jUNWxi5V CYD31Ra0aZ+yhAXJfgZmUerxXJaX1rGX5KKc09nnH3a3svYj3VsxjQQe8P2yFR7IigN5LK wYxrMGTpv3AUAvSN2MH34WeVe2pnmOE= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=VUlVBavM; spf=pass (imf13.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.172 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706164169; a=rsa-sha256; cv=none; b=ms6tMNHj30sDkOGFiBDgBsPG/MIiLOfR2KWJg3aS5Eku9BwvF54NjS5qVjHNY9VxuRMmej e/yxY5JRDsWSxyzycxsiv+GpuE5gnD83gkGpVfg9k3mhXkq85iE/fk3NNedL38qTpA/Hrg f9gmhb/Fwniaw1xnRWFedcYu469wn80= Received: by mail-pf1-f172.google.com with SMTP id d2e1a72fcca58-6db0fdd2b8fso3346560b3a.2 for ; Wed, 24 Jan 2024 22:29:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1706164168; x=1706768968; 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=9N8f26gdn2cRJikP5qLVCVRN7EIk5o+zwHdVnNZOBlE=; b=VUlVBavM+9HVVc8wHCM1j3wgLokN5UtlODnDYlymg4wTqa8Dh726tKOmiflg5+R1IE Mb00x5x9Irs5ijeYUFeD0OdAcSOx5EYbGB2dxUo2/kqGgAm+C2l0fqyzUv/pJ5NLk64G P14AHw59qt+DlnBpAscQt+MA8ldvDnZhujxI6C+FqOwu813OjP5j413nYqoj7Cr7xeMp G+5hkWDqRn1DI2s+8Je8AMAt2JaCGx5nrBOEDDYrACo1ZWCec6aZ2Wg4lOLgzyjfQRZR /InnwGAA/MujnRefJLau07Q254qY4Ef2wanSnyPqRreRZVN/xS9Om6xHQVG/DgeqHoS1 w6qg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706164168; x=1706768968; 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=9N8f26gdn2cRJikP5qLVCVRN7EIk5o+zwHdVnNZOBlE=; b=o9HJnRe75PJGRqJggV/0V7U6Tu0AM/g3VQg3abLYsttWF1TcEmJFPfTEbW7Qiujrvp Pl75NEUwpq8jjKGkKPZ2w61P0hgFML6ABBJmI3NnL5YoxeX3MhO821a+ojHgA/5fNM9R noxg0zUecdP7Srr9wZB9DqzY+yCG4fOydC2bgu8oqR9IKHiV8css1p08XSIO9tvQTA2b DHkHG5lD4j0S3lMPDJiJonQ4xxbU08fNJ5oiMhVEQRmZ/kUF/mhPsO1OMs7NVQPzvgc4 9geB/5pqJZ5bGrJ0cOC68ApfVblxXDfn5XMMaeeyPKUzVwEIlPMIJg/fjgTnLrQe4Q/I OVtg== X-Gm-Message-State: AOJu0Yxhn7/z2u9Nh5FyE4vJ1EygGgG/6qypcYaWGwyLQHK6z0Z69TkR Yoj2I4QIaHnZHXuihl9OA7blvk/Tg0ClJmc7TvQZYTXnFOLWNAjbnUPy6d1yNhc= X-Google-Smtp-Source: AGHT+IHrQQhht3UiJlyKEtEWAFFtcgqnTtiAl+0dT+6axrsGaLK/T90q0i7kfWmqNxjWPLmEJD8+XA== X-Received: by 2002:a05:6a00:3d04:b0:6dd:a004:c195 with SMTP id lo4-20020a056a003d0400b006dda004c195mr250813pfb.60.1706164168419; Wed, 24 Jan 2024 22:29:28 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id t19-20020a056a00139300b006dd870b51b8sm3201139pfg.126.2024.01.24.22.29.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jan 2024 22:29:28 -0800 (PST) From: debug@rivosinc.com To: rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, paul.walmsley@sifive.com, palmer@dabbelt.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com Cc: corbet@lwn.net, aou@eecs.berkeley.edu, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, guoren@kernel.org, samitolvanen@google.com, evan@rivosinc.com, xiao.w.wang@intel.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, waylingii@gmail.com, greentime.hu@sifive.com, heiko@sntech.de, jszhang@kernel.org, shikemeng@huaweicloud.com, david@redhat.com, charlie@rivosinc.com, panqinglin2020@iscas.ac.cn, willy@infradead.org, vincent.chen@sifive.com, andy.chiu@sifive.com, gerg@kernel.org, jeeheng.sia@starfivetech.com, mason.huo@starfivetech.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bhe@redhat.com, chenjiahao16@huawei.com, ruscur@russell.cc, bgray@linux.ibm.com, alx@kernel.org, baruch@tkos.co.il, zhangqing@loongson.cn, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, joey.gouly@arm.com, shr@devkernel.io, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [RFC PATCH v1 10/28] riscv/mm : Introducing new protection flag "PROT_SHADOWSTACK" Date: Wed, 24 Jan 2024 22:21:35 -0800 Message-ID: <20240125062739.1339782-11-debug@rivosinc.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240125062739.1339782-1-debug@rivosinc.com> References: <20240125062739.1339782-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 8E7F020008 X-Rspam-User: X-Stat-Signature: itzj73c46izihyde7ow8ac4outuzre9c X-Rspamd-Server: rspam01 X-HE-Tag: 1706164169-885911 X-HE-Meta: U2FsdGVkX1/RrLxci4QIXymBhze1UoNeOgae7PqqqlB4NlA53GM4SaZT3xuPhb7qeJVsAuFvsWO+ohLaCv6WM5d8JPBkaCcMqluPrnHjo9vlgLj8YbT896mgEZBZhS8ijD3V15VOsA/wC9xEw4QeFnqMQU5AvW15gEIEGnQChvFNxjzmpmmLigxpIxdCuZaqqbsq5gJIBZPynzKi9oCIDIXoOYIT8FfyohZCRzznxlAplOfpKWXPuAWeGVgSApHWITQGlXN1DxH24gVtLOXMmCsX0ToqxJYbTV42IlJaS2OX0y2fDp9XFCLP+jWKf2ju5lU9KfwKV5wFOZrdbZM8Z9MzQASTNIb87mX9Zh/siQsLtqPdj7rwHG/wkfxrnifMjJnjRD1Xc0n98j0EZ401xDkydfdegpIA+RFeiIGICpv51m1kYAPiBnms6nRHHA6UkiuJK2OvHNTBtsLEhqVrAUcd0O7uQKyYu5P2t0SupHpXx6OcuLOC6hR2JXg1cnaILReKZnDuQkvnIug/XnBkP00ObjR430i39vXIRO9nMhCqEsaQCDdTgXKM7cTX537WQ5KUUwSzeGW42lD/T/WY8D+qt6DeTEDBcpHqfF8uM3GzVGOleGFXnpJEusUXl4cxWdNTMy0TTczUUfHd1Ks/WdvqpuCHWCmJ7jT/aRs/fQbRS+E8sG7wdNTwNAPLgquBrw82x20pT0XQ1H/umAyF0AS8fMPp/vwd4yNGRn+NyHC+gQZg+CBGIGl5z11s+Wd19R3vT6RkHd3AMJXf3y1hA4OCzqpkHeItdb5EW0LbaOn3U2wRV5o/w7N40+yg6PNssT4V4Um9YGSOHgQjBLQg3e/5DxN2knYyZGSh5KVNkyV6+kTshobM1Xr/4+NcHkXMAmfIJW+j67M1e0t/ZEXTox3zV4UFMn2dYw0Syq5z5nckceQIDo+LX/MoPCzaRS3nJ6VZmwnAMPSKlZ02uWo YKfYmKoV 6Z3OjfIHTOwd5oTITI+HtsHTvR4o7qPAb4P9GTzpODJmFiLqg3k15gKAHXb6Y+wfPh5GqHpjdA8fxTFvPm5ijd213r8boESZry9Te2AAVUSqoxjuZtLiyV5h9VRpeOJmb9wL7PeaQz7bwZKgCfgw1eQTM+qQf725EseNTLR6+g3aLIlGRYPIsJg/bD9A+s7O3Q6W3P3qPL+MBAoJMZJOlYV52BcSAUJ4zgskzMPwUdrR5ZYadV4QdET8HYg99R18LlNvcVjMcZOGTjbToXJXviw3HGuM/r5FfrpwBewCiuCF65Ox72dWlEH5txnwN39wlIAzdGjpfUgVK9X8T/qUOPzo39VOpxidxYEUaN1riGAuYBlACooiVFJV+0+IN8lQQZ8BjhC5eKqIl+JXLu1VcOWuhfhVhGRjWZaDafw3eonJ2LqkVdzP+sObLSDJGc/ElfJy6TsqrQDkr1VRslSqpIG8BcojEA/3s+xkvd5szpzyJiGfYdKHSx6kBSSnk7XoWGpYD8HXHICrYNSgzZ9OwjQ0FcQ== 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: Deepak Gupta x86 and arm64 are using VM_SHADOW_STACK (which actually is VM_HIGH_ARCH_5) vma flag and thus restrict it to 64bit implementation only. RISC-V is choosing to encode presence of only VM_WRITE in vma flags as shadow stack vma. This allows 32bit RISC-V ecosystem leverage shadow stack as well. This means that existing users of `do_mmap` who had been using `VM_WRITE` and expecting read and write permissions will break. Thus introducing `PROT_SHADOWSTACK` to allow `do_mmap` disambiguate between read write v/s shadow stack mappings. Thus any kernel driver/module using `do_mmap` and only passing `VM_WRITE` would still get read-write mappings. Although any user of `do_mmap` intending to map a shaodw stack should pass `PROT_SHADOWSTACK` to get a shadow stack mapping. Although for userspace still want to rely on `map_shadow_stack` and not expose `PROT_SHADOWSTACK` to userspace and that's why this prot flag is not exposed in uapi headers. Signed-off-by: Deepak Gupta --- arch/riscv/include/asm/mman.h | 25 +++++++++++++++++++++++++ mm/mmap.c | 1 + 2 files changed, 26 insertions(+) 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..4902d837e93c --- /dev/null +++ b/arch/riscv/include/asm/mman.h @@ -0,0 +1,25 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __ASM_MMAN_H__ +#define __ASM_MMAN_H__ + +#include +#include +#include + +/* + * Major architectures (x86, aarch64, riscv) have shadow stack now. x86 and + * arm64 choose to use VM_SHADOW_STACK (which actually is VM_HIGH_ARCH_5) vma + * flag, however that restrict it to 64bit implementation only. risc-v shadow + * stack encodings in page tables is PTE.R=0, PTE.W=1, PTE.D=1 which used to be + * reserved until now. risc-v is choosing to encode presence of only VM_WRITE in + * vma flags as shadow stack vma. However this means that existing users of mmap + * (and do_mmap) who were relying on passing only PROT_WRITE (or VM_WRITE from + * kernel driver) but still getting read and write mappings, should still work. + * x86 and arm64 followed the direction of a new system call `map_shadow_stack`. + * risc-v would like to converge on that so that shadow stacks flows are as much + * arch agnostic. Thus a conscious decision to define PROT_XXX definition for + * shadow stack here (and not exposed to uapi) + */ +#define PROT_SHADOWSTACK 0x40 + +#endif /* ! __ASM_MMAN_H__ */ diff --git a/mm/mmap.c b/mm/mmap.c index 1971bfffcc03..fab2acf21ce9 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -47,6 +47,7 @@ #include #include #include +#include #include #include From patchwork Thu Jan 25 06:21:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13530081 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 C586DC47258 for ; Thu, 25 Jan 2024 06:29:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 575826B009C; Thu, 25 Jan 2024 01:29:38 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 525626B009D; Thu, 25 Jan 2024 01:29:38 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 351016B009E; Thu, 25 Jan 2024 01:29:38 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 1DDCA6B009C for ; Thu, 25 Jan 2024 01:29:38 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id EB37A1C1651 for ; Thu, 25 Jan 2024 06:29:37 +0000 (UTC) X-FDA: 81716857194.22.FE04597 Received: from mail-pl1-f178.google.com (mail-pl1-f178.google.com [209.85.214.178]) by imf15.hostedemail.com (Postfix) with ESMTP id 4DD36A001B for ; Thu, 25 Jan 2024 06:29:36 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=kPGYpQcP; spf=pass (imf15.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=1706164176; 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=hEK0nLeeUyyEqPsucfbvDxvRRfgyRHV2ZwAZ0YdFTFk=; b=CkbmLwhNVl1mZFONTtnoUxmk4APMi9FV5LlBGCgldo2IiOpmZYYo/ilWX5RhZK5TeK0WQ5 7FE/7xQX3pp7lm0g92dkG5VaZDVNyEmKJpoDJtfR2/JioCpTH/hXwE/MFu3cGmUspV0rxN fgScPHRnxHy2MFe7gXkFRyUtu3DobfU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706164176; a=rsa-sha256; cv=none; b=qlcZW8ptQI0faeNqD4Uy2qSm9eMk7Qu7C7vPqfYU++Ifjv+cDyiVcY0J+4w+84kuAGahf6 000LhY1CDnhyk7qaVoqJUC9PWDfrD/1N3lH7I1PmDx3h1oz76VGGpxRdeUHU7e7LidJymp QB160WvGqNkpqfHlbcGs3ExTT2L/VJU= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=kPGYpQcP; spf=pass (imf15.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.178 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pl1-f178.google.com with SMTP id d9443c01a7336-1d711d7a940so55945675ad.1 for ; Wed, 24 Jan 2024 22:29:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1706164175; x=1706768975; 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=hEK0nLeeUyyEqPsucfbvDxvRRfgyRHV2ZwAZ0YdFTFk=; b=kPGYpQcPj1kF5+vUsJoSZki0qmhwIzZm5GSmar55nQTDRvPfakJEv59aede6ox5hNx zAuWovxfCqzRWIgm4oUAx+FKVg6PnSVTQWoUY34i+xieJD5YzIOTVYvnFcHJEaUzKdXq e0n5nRDaVg9smYXWHOX/Ej9K5RpoVJ5RGt0BYBGG99dR0b50CEEPBHvu9st+EgKHAYk5 mmPvYfw6muLjoRbGth0Og+lU+1LR+DeNCILUJTwo13+ybQ87vCwuq+b2Dp8xHbUQC6v+ ziVBqgqEJ17vrPTYtwrMrgk0lzzwLjmB+eNIjVx3wf1fry7BvQsT/Bz0qTzKcE4FRJWx htWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706164175; x=1706768975; 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=hEK0nLeeUyyEqPsucfbvDxvRRfgyRHV2ZwAZ0YdFTFk=; b=HosiSS7LqVhRmfxgXboD9J80bmpopytPxL4Xs97eSATVseDHsXtRwDF5etWglSbaKX P8D6KTfCP0LC/Pl7mkgL4Yz5BGJYnHZFemkh6R01Aoh3VHXfGH2JdO8hkEFad3S6eIhQ QhP1WTp1S1EHzcbFc0bZIk9aZyZix9CAbmajPM0KHz33K5LvRZkwHFAWPhQXIW0XZC/8 iTq744cLeJVD3aJYgJqGMish8e5B9L9zxEbwdCTVpyygt2QuRKWL9RDwzHtsn5p462/2 XFE/d5tY3Vs0EiPzfByJe4s4aGxVUHm9ZM7ta2uQJJFM0OPIEaUyj+5s4S5Ka8h2alrq 69nQ== X-Gm-Message-State: AOJu0YyFzNxR92UB86/OOo5jaXKhrOtJ3VDcbabTXfMHos/Sg6yU5U8e qJW4OxYV5IB5nSYXfVw/ThM0wev80gT/98NE0fpyaAU08XwQtgMSpwO0RlUa+tE= X-Google-Smtp-Source: AGHT+IEuES+Nqzdr2BEz6j/Kl5wHqawKSI3UsPoKeKJE5bEvHyihgNT2hQgxZpIvSEF4VhLoqkQkuQ== X-Received: by 2002:a05:6a20:2d25:b0:199:a43f:db4 with SMTP id g37-20020a056a202d2500b00199a43f0db4mr942290pzl.80.1706164175121; Wed, 24 Jan 2024 22:29:35 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id t19-20020a056a00139300b006dd870b51b8sm3201139pfg.126.2024.01.24.22.29.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jan 2024 22:29:34 -0800 (PST) From: debug@rivosinc.com To: rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, paul.walmsley@sifive.com, palmer@dabbelt.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com Cc: corbet@lwn.net, aou@eecs.berkeley.edu, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, guoren@kernel.org, samitolvanen@google.com, evan@rivosinc.com, xiao.w.wang@intel.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, waylingii@gmail.com, greentime.hu@sifive.com, heiko@sntech.de, jszhang@kernel.org, shikemeng@huaweicloud.com, david@redhat.com, charlie@rivosinc.com, panqinglin2020@iscas.ac.cn, willy@infradead.org, vincent.chen@sifive.com, andy.chiu@sifive.com, gerg@kernel.org, jeeheng.sia@starfivetech.com, mason.huo@starfivetech.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bhe@redhat.com, chenjiahao16@huawei.com, ruscur@russell.cc, bgray@linux.ibm.com, alx@kernel.org, baruch@tkos.co.il, zhangqing@loongson.cn, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, joey.gouly@arm.com, shr@devkernel.io, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [RFC PATCH v1 11/28] riscv: Implementing "PROT_SHADOWSTACK" on riscv Date: Wed, 24 Jan 2024 22:21:36 -0800 Message-ID: <20240125062739.1339782-12-debug@rivosinc.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240125062739.1339782-1-debug@rivosinc.com> References: <20240125062739.1339782-1-debug@rivosinc.com> MIME-Version: 1.0 X-Stat-Signature: gds1tyrc1hmnwfgd4t8qouqkmkspjt5z X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 4DD36A001B X-Rspam-User: X-HE-Tag: 1706164176-56326 X-HE-Meta: U2FsdGVkX1/Tj4FizW7BAaNn3jysxFaQSf8fN44SD+5BCdI9Cp7xtC7Hm+3Kd4gull0Yi8TSGv/HBZ5Y8GY1z6qEBFfpKfEYWYw2A0Xeqe0g1pcJKFEa6L1+rNbl64YE0nFItoI1oQ3TGW+WAk+OE55YPXxmyylXF7yL2qJVKujwCnsVhzzGsJ/9dHJJ6cKF2qphU8t1NzSWkt65jd6hBVVb2X/iHxbN6N8CiEFpuvXdWoEbkG8xb9PgL0dO1RMvHpob8D2j7y2ATMTF8ExdEPhZSYL5Erg/fkhuaRcqtwNuPTPLMAf49STtyzJzsxBm2EBRYwJBZZwzlUrUisSle7JB654n+x8e+rOOQdAECht7bjxba7o2HmU8hYW4c3XyHMUUpkMgBzO75S9Phae6qkmuB/dQBdH+6RexRlGufQsbDPOYN744BBOQ9VvPARa4bcH0h+Cex+v1T6MnuhH61ss6z0e89CURqC+sCQojJpFLMrfPatmdMKKARMz1aLrm1P/31Wa1JtHJi3sbfU7p97wMC8iR1KRopPtz06HVN0KmXB8faFkY8h2jwBf3j7rBNSkW6dmeBwusHLRBQcoarkCH2Oo9yksK8SFFXtAJ2ENvKjj0HXGZ7/PQjiAdzVoigdBTOmyVJpe0b847lgkJJrMkjef9M9ZGWuPJkHuha5fG1DGSwgbTsnGD7NCRXIpLBg0HBAWbupqeM6XJcGMRUCoyKwf03AC9mB8zNMWXxfim/1aSsxeItfLOx1MHF+t04hWoPEr8GkCHV4DZCP9D/HQeRwLKiXPEOFUXBiN0FjXfFtVgPcYGylKZKqaUMCqX2m4P8l4xYX9OQirTRGd50Oabz9hmpC264N/rnKOyGjbfaKyD+W7Ecl/BCjueYE1F4z/AJkKXcvhNDMrUAC52uibN03mfVmNB1vxGlG3MBcOYlK4KlMUfBdXVXhBH/wDnOnNWf4CTg7IKxPddepi caCuvvCx DhxNqXRdD4iEjtPhPs0+uojHJ2IjnzaxL/1XhE20FpXTLjQJMZoT8N+cPyO/PtZyVyBfQOut9MMtAV6Pj07Cv4TsOXCW4BC9NXebWl5AlzP0Cu3ve3tM5zSLY284r0K+SqteqolcKoae8Ilvsl+GqQbfj12kBbW8M5bZ40hO43zR11kHCc9teiCYTLm/gp1xvdULqVY25pNX1ykXF1bZFzTxp0w== 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: Deepak Gupta This patch implements new risc-v specific protection flag `PROT_SHADOWSTACK` (only for kernel) on riscv. `PROT_SHADOWSTACK` protection flag is only limited to kernel and not exposed to userspace. Shadow stack is a security construct to prevent against ROP attacks. `map_shadow_stack` is a new syscall to manufacture shadow stack. In order to avoid multiple methods to create shadow stack, `PROT_SHADOWSTACK` is not allowed for user space `mmap` call. `mprotect` wouldn't allow because `arch_validate_prot` already takes care of this for risc-v. `arch_calc_vm_prot_bits` is implemented on risc-v to return VM_SHADOW_STACK (alias for VM_WRITE) if PROT_SHADOWSTACK is supplied (such as call to `do_mmap` will) and underlying CPU supports shadow stack. `PROT_WRITE` will be converted to `VM_READ | `VM_WRITE` so that existing case where `PROT_WRITE` is specified keep working but don't collide with `VM_WRITE` only encoding which now denotes a shadow stack. risc-v `mmap` wrapper enforces if PROT_WRITE is specified and PROT_READ is left out then PROT_READ is enforced. Earlier `protection_map[VM_WRITE]` used to pick read-write (and copy on write) PTE encodings. Now all non-shadow stack writeable mappings will pick `protection_map[VM_WRITE | VM_READ] PTE encodings. `protection[VM_WRITE]` are programmed to pick PAGE_SHADOWSTACK PTE encordings. Signed-off-by: Deepak Gupta --- arch/riscv/include/asm/mman.h | 17 +++++++++++++++++ arch/riscv/include/asm/pgtable.h | 1 + arch/riscv/kernel/sys_riscv.c | 19 +++++++++++++++++++ arch/riscv/mm/init.c | 2 +- 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/arch/riscv/include/asm/mman.h b/arch/riscv/include/asm/mman.h index 4902d837e93c..bc09a9c0e81f 100644 --- a/arch/riscv/include/asm/mman.h +++ b/arch/riscv/include/asm/mman.h @@ -22,4 +22,21 @@ */ #define PROT_SHADOWSTACK 0x40 +static inline unsigned long arch_calc_vm_prot_bits(unsigned long prot, + unsigned long pkey __always_unused) +{ + unsigned long ret = 0; + + if (cpu_supports_shadow_stack()) + ret = (prot & PROT_SHADOWSTACK) ? VM_SHADOW_STACK : 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 294044429e8e..54a8dde29504 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 a2ca5b7756a5..2a7cf28a6fe0 100644 --- a/arch/riscv/kernel/sys_riscv.c +++ b/arch/riscv/kernel/sys_riscv.c @@ -16,6 +16,7 @@ #include #include #include +#include static long riscv_sys_mmap(unsigned long addr, unsigned long len, unsigned long prot, unsigned long flags, @@ -25,6 +26,24 @@ 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; + + /* + * PROT_SHADOWSTACK is a kernel only protection flag on risc-v. + * mmap doesn't expect PROT_SHADOWSTACK to be set by user space. + * User space can rely on `map_shadow_stack` syscall to create + * shadow stack pages. + */ + if (unlikely(prot & PROT_SHADOWSTACK)) + return -EINVAL; + 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 2e011cbddf3a..f71c2d2c6cbf 100644 --- a/arch/riscv/mm/init.c +++ b/arch/riscv/mm/init.c @@ -296,7 +296,7 @@ pgd_t early_pg_dir[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE); static const pgprot_t protection_map[16] = { [VM_NONE] = PAGE_NONE, [VM_READ] = PAGE_READ, - [VM_WRITE] = PAGE_COPY, + [VM_WRITE] = PAGE_SHADOWSTACK, [VM_WRITE | VM_READ] = PAGE_COPY, [VM_EXEC] = PAGE_EXEC, [VM_EXEC | VM_READ] = PAGE_READ_EXEC, From patchwork Thu Jan 25 06:21:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13530082 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 9B0D9C47DDF for ; Thu, 25 Jan 2024 06:29:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3A1BD8D0002; Thu, 25 Jan 2024 01:29:43 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 351088D0001; Thu, 25 Jan 2024 01:29:43 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 17D858D0002; Thu, 25 Jan 2024 01:29:43 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id F33138D0001 for ; Thu, 25 Jan 2024 01:29:42 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id C7390120769 for ; Thu, 25 Jan 2024 06:29:42 +0000 (UTC) X-FDA: 81716857404.24.541F9E4 Received: from mail-pf1-f172.google.com (mail-pf1-f172.google.com [209.85.210.172]) by imf26.hostedemail.com (Postfix) with ESMTP id 04337140002 for ; Thu, 25 Jan 2024 06:29:40 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=eVhgNzsA; spf=pass (imf26.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.172 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706164181; a=rsa-sha256; cv=none; b=dZ9hezbg09wvXSga6d9XKKs4mjjL6qogCIJsySEGDsuwEEnGnSr4uGP3lKsbj1LEmB8r2N JURtTeTBX7JiSYYM5wBJom9o4KbDozeANIIMDWYJFl5cZrND304OqMtBl2EJDcpU7G0FD2 a41S8shWSBnAa9yUUCyEiiFRQLFAOwo= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=eVhgNzsA; spf=pass (imf26.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.172 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706164181; 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=9U4/v4w/mrwXRuPEpy9c03cIo3F2WWlXK10aLJp76Yo=; b=onZAtlTqMnzNdpV7kHY7gq2JV9AkvzRKN0XBz+CwLXlxV3OW/M2qw0gzAmqBfLEUxRvE4L ONLUMlYA47CBJNkKzXTVnR4BGOJEOff3NKuLm2pqlW9RSqBupA/B+9o10eAORFpIxfCruQ 04oeq+UNeNsqN386V6FhaHyWUcZks2o= Received: by mail-pf1-f172.google.com with SMTP id d2e1a72fcca58-6dd7b525cd6so2048282b3a.2 for ; Wed, 24 Jan 2024 22:29:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1706164180; x=1706768980; 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=9U4/v4w/mrwXRuPEpy9c03cIo3F2WWlXK10aLJp76Yo=; b=eVhgNzsApQXrpVHMMJgpVCsYd/iRJSJRIzczkwxDX8N2vQqzLbB+49P1y56PQBzW7k SvQfaEeWqUZvM6wA+6b4OTt0gO5Pxp+dkYWFeIgaynJISDZD8w2scB0gRrQdb5jEl0+x N9RQBAhHMYKHp+BFpi3xBo7pj1hUQ5c96HQA/A2q22lPLhmYICEezD4TAindyJf703W1 eXIMe3CR3UO3U7T5WGxME16Y4EOQ1oc4wbeQD1qrRB3s2OoSCqlBHcAcgQyt0MWG6s10 NrvLk+thzw+LXGK7dAFChOSPYfDoDyf43LglR8pIExgpw1g4xVDC2u1j+/mK9XSRU/X1 oQzA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706164180; x=1706768980; 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=9U4/v4w/mrwXRuPEpy9c03cIo3F2WWlXK10aLJp76Yo=; b=q+ghxCzoiSbtK7YhYBwSzqd42KAplMAAdLlNuGTl8WT+RVOeWt2rT1xes5uhUlk/hw u78cXItCULPxY5mJiv/AouLiCpg50gjYdDucwejiK429a5Q/wVNIoVuXb5AM6etSyuN1 NmlNCqZ9rZaJS+MDvSniU8nZRVTRqGd//TOqBREh86n5FlnOEpENPLIdHycJXET8+xfT ZGTjBWXc5ia3oVBB5aCl+ekWRLInJBtkv/chl8qUx5tVvJeCyOj8qBTxQYuDxpiw9GWV wGAA+tV20fjRDvLANR3Zep75qEWUF34WYvQZXjPZwA9o9AxtQPYAV+itZFjm5bkxYqsD Aw0g== X-Gm-Message-State: AOJu0Yzm7bvY8JWPEzOT7VF08BCerbN2EnWkFlVOIzwb5zEHIOY/S2Du ClAfAe4dSyTiBt21KhL4LL7Rn3SY+sGVb73qM52d8IWHs8sTFGRrl7lp4Qs4MPY= X-Google-Smtp-Source: AGHT+IFE6DJFyN+P/UFNG3VqxG06rTZRsSyHKHiD9aR2/YyBQCkrTLXVopsPYOyeiHivgMNZUq582Q== X-Received: by 2002:a05:6a00:4006:b0:6dd:a072:867 with SMTP id by6-20020a056a00400600b006dda0720867mr290270pfb.15.1706164179881; Wed, 24 Jan 2024 22:29:39 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id t19-20020a056a00139300b006dd870b51b8sm3201139pfg.126.2024.01.24.22.29.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jan 2024 22:29:39 -0800 (PST) From: debug@rivosinc.com To: rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, paul.walmsley@sifive.com, palmer@dabbelt.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com Cc: corbet@lwn.net, aou@eecs.berkeley.edu, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, guoren@kernel.org, samitolvanen@google.com, evan@rivosinc.com, xiao.w.wang@intel.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, waylingii@gmail.com, greentime.hu@sifive.com, heiko@sntech.de, jszhang@kernel.org, shikemeng@huaweicloud.com, david@redhat.com, charlie@rivosinc.com, panqinglin2020@iscas.ac.cn, willy@infradead.org, vincent.chen@sifive.com, andy.chiu@sifive.com, gerg@kernel.org, jeeheng.sia@starfivetech.com, mason.huo@starfivetech.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bhe@redhat.com, chenjiahao16@huawei.com, ruscur@russell.cc, bgray@linux.ibm.com, alx@kernel.org, baruch@tkos.co.il, zhangqing@loongson.cn, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, joey.gouly@arm.com, shr@devkernel.io, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [RFC PATCH v1 12/28] riscv mm: manufacture shadow stack pte Date: Wed, 24 Jan 2024 22:21:37 -0800 Message-ID: <20240125062739.1339782-13-debug@rivosinc.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240125062739.1339782-1-debug@rivosinc.com> References: <20240125062739.1339782-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 04337140002 X-Stat-Signature: 5zcqax8f5rt6um7yn9w6h3wejj6onq6z X-Rspam-User: X-HE-Tag: 1706164180-284688 X-HE-Meta: U2FsdGVkX19nD3M+MbUbTg6/gDc4diO9DGUHAZj6GZ2hOJChfpAjkWAeLRBsljsVzzuJlbEawr38421Zt8rqYXkKzKp926uDUuoVSwQyMgzSaEGKjmL2J+01DFW0zdZkanCmcrvVOF3kSyU1RiGt+elZZLEzF1VnIf0t2LFeXnT0M1QgtqyURwiBFbwPYO5sm2A+rwJbdN3TEi4NIXfoX7ZmV/Xmpq1wsJ2OQiwTS92iEb92kwdlJ7En6XNYpACCl+MB3SgRZbwU/sW+J5QEE/Jz+d8oZ/9cbE22WvuV0d5DlYySp5+Xtj9rT5l4bG+M7qXZYl/jQxev9E39hlnNpKTzJMpwSIQ6XgkMHHPsAKLsEfq0N3o056K0vquWqpQgffOdD+x4cfEBrm0d6aDLHvFpw87b+g10/IJlg7ypqDO0bNQotevtGovRgIR/ERP6mh9vVASVqYdgkfGY5dAtMlNX0saORZVpnc7A8IWGYCxrGO9iTDvKRauwsOdPE8+D12Y365GdH9GszhvZ0LdTEABrlAVEaCgX+Lzo+/EyAALhlJWDdh6+I2m9IcatOWQaOfaXIcMHUkOz75O3dz1tG/qzWa1Q/P9d0fDX98MiE2dJe8blsZeiAJ+QdLUh1zxW1qhhMlULNpjnaFbCurMqvWVK1a8gd66wlI9rr6cCG+ECXIRdITZZ61PyEp4VANWzOff7BYRwHQinI+xI7GV2aa6ZVepT+FtR30q39za+cH3vXitGjX6INATuGi79xxR86WaO7msfq86L7w0S1Q/68gSAHUB2HGjJUDwS7cXqZojc0uSXAXSUQ0R4sJI3IDgmTguql7MMLjeojYJV3foP/xbtEPwcC95F6HPVO71eHBNBKgRVeMFQHJS6YEQsN472969oInbad8C1LNOhUtQdJt3HxGRMxRHnRRFZV34adMMigBpn8EAvVTxH8R0bcf0jlhvPjhbn4lDYmhcC3Fj dAenHTj5 AOqdNeMrwpaIMTfgIuj0OlNmmQQzV8y8V+3qPa00WHPWAdpQpEGFlWh+rM6GeVaTU3pIr8+jaH9ucJUz7xBXCN/Wk3jS0FC6bIM7K2hymmXkJGQS2SNA0ljAVSZiQAGybeAaH/t/LYIR5HiE8KnUUa16GOfzetXbvN2buO+oWtq12vI6Lb+9olaAXJ1RFIJ0mavhIEG56ls3i5YLhptN1o6viL9nLWbFNYFe4/qWIUX+nPN4i8ObmRLvHJ8KCHHWbtrpSP9eZ4ld6rII0VK4NQeOVEbFdeXA2DSe5H2fOD795J2ZjcGJ7vgRmIcIblEtJ3g+pOTjIsBGY3ZY4Y7phuaiG4VMPCDo30jp4tPnot/8lz9tkr+E9H/N9HT8y34N72sRNsHfSzdtCFibb66ZlCyhXrCszqo093VQCX1IxOn/ei4t6t6n0hN3tWrE2ktmvDJcG 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: Deepak Gupta This patch implements creating shadow stack pte (on riscv). Creating shadow stack PTE on riscv means that clearing RWX and then setting W=1. Signed-off-by: Deepak Gupta --- arch/riscv/include/asm/pgtable.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h index 54a8dde29504..7ed00b4cc73d 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) @@ -705,6 +711,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 Thu Jan 25 06:21:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13530083 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 68CC6C47DDF for ; Thu, 25 Jan 2024 06:29:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EC4208D0003; Thu, 25 Jan 2024 01:29:48 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E4CF98D0001; Thu, 25 Jan 2024 01:29:48 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C78718D0003; Thu, 25 Jan 2024 01:29:48 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id B32618D0001 for ; Thu, 25 Jan 2024 01:29:48 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 8C012140334 for ; Thu, 25 Jan 2024 06:29:48 +0000 (UTC) X-FDA: 81716857656.21.67DFDB3 Received: from mail-ot1-f54.google.com (mail-ot1-f54.google.com [209.85.210.54]) by imf25.hostedemail.com (Postfix) with ESMTP id BD7FAA000C for ; Thu, 25 Jan 2024 06:29:46 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=hdRNmLB7; spf=pass (imf25.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.54 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706164186; 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=UJDL1aQMYsZ6x2hXclmWD0oGcQ0Z9qLhCUiJhXHD6LQ=; b=735NDPOUPMU07b/vHabZT8LnOHI8eZag6iafTY/MhLeevQ0W0XmBwfUBJ6iaXb6+XDySq7 CabQQ0uFUG3NvyRfYcPJqa0YI8WRZIFwkb5ZKdyH9AjXF0BMDFN9BmPVhJaj+nN100+e91 fosLxuC6lGrQvJ6VU1IoNpZs4B8elg8= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706164186; a=rsa-sha256; cv=none; b=hXQE/Cy/LKOZwyQZH4gXKShO0d3jrUjIF9AVpLYog+7by2v7RLAVPZueTuORHklCIQOkjA Pvoq0tf68RR/iliJ7n2tAvHZjLrzC5c3s78kjvu6lYE2vUN4aRDDTZWtpLdMScIRkQ7/x7 q07vTRMsETLekPMoY+bd5KJOza7iP6k= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=hdRNmLB7; spf=pass (imf25.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.54 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-ot1-f54.google.com with SMTP id 46e09a7af769-6ddf05b1922so4801805a34.2 for ; Wed, 24 Jan 2024 22:29:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1706164186; x=1706768986; 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=UJDL1aQMYsZ6x2hXclmWD0oGcQ0Z9qLhCUiJhXHD6LQ=; b=hdRNmLB7KMBV1Qpk2ozmcHZOSFk74sjHvnjkNaaHUHjst4xW/+tDDlydpSP09uJW71 YWWVrsiWZru5DfJ7YNnWJ86UrkL0TK/Xsaq37T2Wat+beFdDFbXpyWskmf1FVUciVBKq SUUTMyJQIw6A9T22Q6sTgfeVpcfJWTrak0Iafun3gi1h8ongUVCSTgvD4HYA/cTk2tR1 w1SNbfVcLyd9bZH9CHFjhMjHPMnsnYVqlAZc4D2ujNj3QZ/dHiIgao8sLVYtKO2quX/9 om7p+NDKikdGJ08nUSrtpFtSHkzfcznvtFa/ZidU5g/D7kplA6XPNNhrBt5IjSdIbObC Shcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706164186; x=1706768986; 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=UJDL1aQMYsZ6x2hXclmWD0oGcQ0Z9qLhCUiJhXHD6LQ=; b=o1Fo2cMkC2PyIYdgfBkus9Zz3OfT473uUehfZIreKgfNUXR9DatBTYqfZWaU4NzsKZ kBYZdWLPeA/BuLoG2DLbZ6VAkod4mKBfVolmyyuNB97O4JzsODNzCWGbsgLZBJT0hXBQ 5MEq13fLxHyLJCsWpkY5JzGcfqfOUUIDaBchzHBiNJx+tY8yKxkVNaXqaz7rzagPdQg3 OgtRicvreou9z20G4NSwL+AslWgPrmcOnxTh8Di6TmdhRDs3+O2NKfI8XbpgKLrdqPja Z5sNuR4OYPobl6JScCpaz8oyfHUH0H8KRcajF1gEjXjawRC2zHcqLV/L+qKUlK6ISm2p 90/A== X-Gm-Message-State: AOJu0YwiVi7VP96a/jYQiO5sBNSQd30PFOWKMitKoj0hCENg3fJNpXTT hO4xLy9U7qvecDBY4hbK5Sqy1UB5w2hC/mW0BLfkAkEO1wtxuZJM7Sfx/HRS0vg= X-Google-Smtp-Source: AGHT+IESxe3nC4p6ceWk0Q/8i6vycGlmQHt1EYp+hk+MoL237ObbhpMDmBWtgjNemeeNm1tg7bXXoQ== X-Received: by 2002:a05:6358:6f17:b0:173:33e:ec22 with SMTP id r23-20020a0563586f1700b00173033eec22mr487238rwn.22.1706164185725; Wed, 24 Jan 2024 22:29:45 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id t19-20020a056a00139300b006dd870b51b8sm3201139pfg.126.2024.01.24.22.29.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jan 2024 22:29:45 -0800 (PST) From: debug@rivosinc.com To: rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, paul.walmsley@sifive.com, palmer@dabbelt.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com Cc: corbet@lwn.net, aou@eecs.berkeley.edu, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, guoren@kernel.org, samitolvanen@google.com, evan@rivosinc.com, xiao.w.wang@intel.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, waylingii@gmail.com, greentime.hu@sifive.com, heiko@sntech.de, jszhang@kernel.org, shikemeng@huaweicloud.com, david@redhat.com, charlie@rivosinc.com, panqinglin2020@iscas.ac.cn, willy@infradead.org, vincent.chen@sifive.com, andy.chiu@sifive.com, gerg@kernel.org, jeeheng.sia@starfivetech.com, mason.huo@starfivetech.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bhe@redhat.com, chenjiahao16@huawei.com, ruscur@russell.cc, bgray@linux.ibm.com, alx@kernel.org, baruch@tkos.co.il, zhangqing@loongson.cn, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, joey.gouly@arm.com, shr@devkernel.io, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [RFC PATCH v1 13/28] riscv mmu: teach pte_mkwrite to manufacture shadow stack PTEs Date: Wed, 24 Jan 2024 22:21:38 -0800 Message-ID: <20240125062739.1339782-14-debug@rivosinc.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240125062739.1339782-1-debug@rivosinc.com> References: <20240125062739.1339782-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: BD7FAA000C X-Rspam-User: X-Stat-Signature: 1yqf3ak5fp4qmw5iq9gr31mtsehf7jgw X-Rspamd-Server: rspam03 X-HE-Tag: 1706164186-757355 X-HE-Meta: U2FsdGVkX1+BoilNEHSFIrQjp5/A2SU/3+2fCmabGGpDrPdq/t0/xCD5Op8hCCukQ4om+8/iz/ozADmoZRqdAQQb+WOXFulyeRquvJc+5jf9EM7IweCPJxlEUmVZvCvhYgPYHgDTO1pujHnjxGDmzMH87A0XVzw7uCEX0Pi2Iv94AeDCWHPgDTRLVyT314fW9szvmEKx9xjLOqWCRksnhiwuRZXjc8MMZOzXUJWeJgdLfa+71DmYU4nSbJkCPx6W7X/Qlh3EMGLNLxY7lxuFxPhCWseDjJ/m6GCcGQGQyd0B+L8Ftth0BrAcMwU7TgOJpQkod6/gru9DCqnyf3exOlbxB5Cd+ZJ69mgM2HELXblQgkRlyw/QiDbGmy7NCk519l0JG6kfjampKbF+472e1s55JWopWnrxBNirx3OFNScPBXRfPYwoEaHt+hiM5f7qDvQ9IizAU1LRPyS4tJJe1S6+f8UHW9VijgU6wimm9cjoiBVycev0ovX7FvuQRbpvkltFL18TuRGCSwifDKDTqD6obPnZOUjS88eGUBYvvS9Mj6nvmq7mDkE/0qkH2bNrjyL4JllII9JP4kp3rqxuQQc6MHaJcxFNaDtomzAWInLD8og+91+SiO6HZqOQuNZG66h9Foqnz4Z0JLDKWXZVkmeuTbXayvBUqLk5wndiA/9kI6G14K2wW8bQEZbtQhKzOEw3JcO+ax6/rAjfsq9NA+vH2b1qx98v2JHTGwk/f0wc+33Gs+DpcvmZ9dEyvVMBJy8xNQGOKxtskh67G89MM55JCl2TfBBmVA9vUU3yoyqXyvPaH9xWfLSnSk/5ptkZIKqxzRWnm7l761erZhH4ZlxfL4UDHwiD0BCjIrx6qgBrvjKckQUk1F3hujDYQa2+jqqd+rfTWu7tAwuXpVGfkDws3h6O3i+Rd2gB9eiLj+5uHR7W7aTnD4hJ/s+9qAPTZiS/9dWbt4Ib0Uo0KYz oOVyQvuy pcyTe4w6Qa6Idu+DELYvkHVDDfseHNuHsecExRXtt8rcziXaCuoTYCVX+SQt/j3HAyq0co0Esb8HwEyGB6oN4OehicbO++fwMbFI0i8MJnn51w+noOmuLjNiA4ft3KI6WVwVouXLNv9V47wWQcF6nZqT6d/6SMIDVdgvLnwLveKwZc+nPho5CiroDoGBW2S+KxOyfYuCcQ8Wd/1v/yXyn7632u6vsCZCjQAHRqVk3eJUO6P2mQX00ufYRxjKrlJG2QBhR6stlq+YCievkSc6F89HPDqmiCSm7VFgyNfbyIcnu7SyEj+2ghdvwkfDPnrdy+zsSXsMrifvHsRoLTES+3yg3+5vY40zzhiOltRZT33t69+5nxLIwiGuLDV4YSFW2SiVx26ljH4L6Pq2CigtFh2uucOdIletiMMUENXb0ZEvfCnTwnr1hb3APfWo4rMZ6qLVfbK/9D2Eq+lHBqFCaYAyAu12oCwlxHm/1F+omXNC1XyQ= 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: Deepak Gupta pte_mkwrite creates PTEs with WRITE encodings for underlying arch. Underlying arch can have two types of writeable mappings. One that can be written using regular store instructions. Another one that can only be written using specialized store instructions (like shadow stack stores). pte_mkwrite can select write PTE encoding based on VMA range. On riscv, presence of only VM_WRITE in vma->vm_flags means it's a shadow stack. Signed-off-by: Deepak Gupta rebase with a30f0ca0fa31cdb2ac3d24b7b5be9e3ae75f4175 Implementation of pte_mkwrite and pmd_mkwrite on riscv Signed-off-by: Deepak Gupta --- arch/riscv/include/asm/pgtable.h | 7 +++++++ arch/riscv/mm/pgtable.c | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h index 7ed00b4cc73d..9477108e727d 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); @@ -706,6 +710,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 fef4e7328e49..9b1845f93ea1 100644 --- a/arch/riscv/mm/pgtable.c +++ b/arch/riscv/mm/pgtable.c @@ -101,3 +101,24 @@ pmd_t pmdp_collapse_flush(struct vm_area_struct *vma, return pmd; } #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ + +pte_t pte_mkwrite(pte_t pte, struct vm_area_struct *vma) +{ + if (arch_is_shadow_stack(vma->vm_flags)) + return pte_mkwrite_shstk(pte); + + pte = pte_mkwrite_novma(pte); + + return pte; +} + +pmd_t pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma) +{ + if (arch_is_shadow_stack(vma->vm_flags)) + return pmd_mkwrite_shstk(pmd); + + pmd = pmd_mkwrite_novma(pmd); + + return pmd; +} + From patchwork Thu Jan 25 06:21:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13530084 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 867C8C48260 for ; Thu, 25 Jan 2024 06:29:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 10DC78D0005; Thu, 25 Jan 2024 01:29:52 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0BA958D0001; Thu, 25 Jan 2024 01:29:52 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E4E3B8D0005; Thu, 25 Jan 2024 01:29:51 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id CDB478D0001 for ; Thu, 25 Jan 2024 01:29:51 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id AA7E51403B0 for ; Thu, 25 Jan 2024 06:29:51 +0000 (UTC) X-FDA: 81716857782.30.7D29231 Received: from mail-oi1-f171.google.com (mail-oi1-f171.google.com [209.85.167.171]) by imf20.hostedemail.com (Postfix) with ESMTP id EA4091C0011 for ; Thu, 25 Jan 2024 06:29:49 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="Ry/DkQec"; spf=pass (imf20.hostedemail.com: domain of debug@rivosinc.com designates 209.85.167.171 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706164190; a=rsa-sha256; cv=none; b=CSYUzUXyjjJxV3atvMOTTuWaCdEaP1bAXjnGZN6eoDsLLjqKK70+aOAMmyjmoHA6TsFgNI uSu2w+g0sClSM177PA43JXfbufVxo6Dv+ZTdJqyGdKjHqUdZEDqhMMbsOmVTOBYbMfxDxQ rrFyAuFJWlIfW1gAmLkib6PbCMitg10= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="Ry/DkQec"; spf=pass (imf20.hostedemail.com: domain of debug@rivosinc.com designates 209.85.167.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=1706164189; 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=kRzF2SZSpJKrBhoESLsApoQWcpLzxI/TelseFUZAskk=; b=qBhjMti33TdaO2qCO2TI1m6bFbwq+in5NP2ilJV3EJqpg8rEhPT+zx29QHGul/+1KUjO8n q1FWiQ8F4zMGy7UGQdpqwKNULz0LQWPyvPCGsh5F3Sgz/XhPENmsqQUEGD/gTA4BgA7itL I58KkLs15Ft4QrHwcCAqjuEoGxo5c40= Received: by mail-oi1-f171.google.com with SMTP id 5614622812f47-3bda741ad7dso4930401b6e.1 for ; Wed, 24 Jan 2024 22:29:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1706164189; x=1706768989; 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=kRzF2SZSpJKrBhoESLsApoQWcpLzxI/TelseFUZAskk=; b=Ry/DkQecUD8af2K7Bz65xTr6/77ATvmqqBpMk9ICibvxEHIxr+PmQnDpqauavYij6q ktdJjy3LIQ0rFRMJimu79yMMttJdMrjuO1oHoKlm6Hn6IU5T9I3lkWvCLFN2bcWamzRp 57XugYkBWC8bK3pn5ibqRjcqfPryopyS6t6vkbD5pynkLDHJxWobnt1dtOgOYviEPQxM VW+wpYsMu1ftOlrO7UGNRJio3LHOxOFGF446vEyUAPUHgGWxg0KDbeQYlidufiCjfzhA WI3R2Uo9jjkpnvXztQkC25O3tsYK5z2AnJmeH11oj1cSLWh2GNAt1K/g378hAD6378u2 /b6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706164189; x=1706768989; 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=kRzF2SZSpJKrBhoESLsApoQWcpLzxI/TelseFUZAskk=; b=bXf766DFCsPeu94Wh1DUkUunEfFylebFbVqfxUTmh30SUFvah8Ahww5mmCez2EImH+ vCLKXQT5rSPUgcbYg1I6saRZnOFssAItuk4CWYzjwkSIuqyF8A1W+7pIXFpEVNfWF62P +bHWWteQNOuLUscgeSl7BrvKyJe6lzx+0bKKgqfdwVIWkS4wV1HP0NCKpD+TukhUhyYQ wxmbn/JHTHWmJUqnpWRgAyyD0L/v+o785jJHQX21GdetD6yQgPpXoUF9Z/BG7NBJAfia DD4xsgNhoiEupZm3VQM/NuLmd0fu4R+4Y6Pp3wSF/K49MrTi2z0NBENOEftbg06dFG7V EkJw== X-Gm-Message-State: AOJu0YwX7LHOxSuNLHbZQfA7BrDBBl494X76QFTlPYbpYpoJzY9zlUfl RjZ73LxhoCN5tDJ0fBjJPtWY2li/6/juHcy2n3BN2ZVRjlFqDtZwl3OtyurnsOY= X-Google-Smtp-Source: AGHT+IErjNQJ/wKFk47rmBKTBBYuOvQY9ZOcxL+k+OB28eXMUoqPg8zllrGghQsiXCVST38v7+Fe9g== X-Received: by 2002:aca:181a:0:b0:3bd:cc3b:a894 with SMTP id h26-20020aca181a000000b003bdcc3ba894mr464457oih.93.1706164189075; Wed, 24 Jan 2024 22:29:49 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id t19-20020a056a00139300b006dd870b51b8sm3201139pfg.126.2024.01.24.22.29.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jan 2024 22:29:48 -0800 (PST) From: debug@rivosinc.com To: rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, paul.walmsley@sifive.com, palmer@dabbelt.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com Cc: corbet@lwn.net, aou@eecs.berkeley.edu, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, guoren@kernel.org, samitolvanen@google.com, evan@rivosinc.com, xiao.w.wang@intel.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, waylingii@gmail.com, greentime.hu@sifive.com, heiko@sntech.de, jszhang@kernel.org, shikemeng@huaweicloud.com, david@redhat.com, charlie@rivosinc.com, panqinglin2020@iscas.ac.cn, willy@infradead.org, vincent.chen@sifive.com, andy.chiu@sifive.com, gerg@kernel.org, jeeheng.sia@starfivetech.com, mason.huo@starfivetech.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bhe@redhat.com, chenjiahao16@huawei.com, ruscur@russell.cc, bgray@linux.ibm.com, alx@kernel.org, baruch@tkos.co.il, zhangqing@loongson.cn, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, joey.gouly@arm.com, shr@devkernel.io, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [RFC PATCH v1 14/28] riscv mmu: write protect and shadow stack Date: Wed, 24 Jan 2024 22:21:39 -0800 Message-ID: <20240125062739.1339782-15-debug@rivosinc.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240125062739.1339782-1-debug@rivosinc.com> References: <20240125062739.1339782-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: EA4091C0011 X-Stat-Signature: qdqyadgbxtq517sp1jtz6onwki3ykqgo X-Rspam-User: X-HE-Tag: 1706164189-408628 X-HE-Meta: U2FsdGVkX1+T3CtGwBAfaMuAMareX0ECKlYS1om6u42ggL0MnAx/KHFWKTaJwn1QS/tVSSbp7C0eaks3i7I4VhlqXRW56T9bxNMEQnTvFTRZbNGM4vL83zQwEsS4frtMdTNK4x+lSuK56JoSxoInuK7obIwcc+fCy5FdViFUCFtxZ5tXOyyTYF5wwy1OUqAC1rVRWk6FwSd+HVMBAVlOzzWDzWDw/ht7Q7RCCSrnEdnKb1WlSQyVXvQYEe6tU26i3YaDljkoiZaaOWlhj3rCwMxzZR8MDL/7J6diHGN7YRntnjGz7ezOI8HJaNujTtorIxHmWcHDYcU8Ab+puGvH1cdZ+HCR/l7T3vUHkIDCKw5TxgZSP4IrToMBzoO29LCi5rUKoJNxmlCFIDIfxmx4YsqPGkstb9lOCQeEx8MmHg5K2CMd1w39ilnlcPFxZhDkceAi9o/WT3IZEPZAofs73RJa2Yt0E6bG+71s3CW8MazRnjqyvSBvUmP7DHOjHdUhOGLr0GvRRVgsGOibYWprETDP2EhSmJzEeYTe8kbiA6HWYzMQdE5+t8nBLtlKZWHARZtdOrOcHAOGIStS4YQ3Rb0afLK9OL15ilYOSGjw9+tTTWRg2fpOUrCLdmiABotsje6NahZXwDee+QSsft4RMeVsj9ACSptKKX6o/2skPrnWQPFAl/ZQFMPdGhRBENTFptl4SQX8FRXcU90V+zKognRPWaHeHjHWxr1DhS5X8qbhrJ9+m+x95w0vaPrpoE4SXXpvkVurEzwhAgFe9x2gslJFhAY3l0QfQqHeaCI3Ww0bYAXtXcvsztMr5+eIaeUBuQJZ0XpK+/lsKDixQYkCkzyfZD3c6gZvmxALcBfRLXnYJUMLPHsc0+t608BPWUQQRrUf0XVSVMi5tLmRhEmGLZNLBrz3CCvGjY/SSR08SepswvBnPBJZM44MC0dcqUaqiY79XhT0CoLC/VAFQDO dR7anQAA oM3g11xCHPub/MHRfXXdRC/dpeBXz4QZhINPczThqREjRT+yWmY00XCvLVVYgQG9L1IeZvYtfhZqAwDRTfPW9JKiBPGPsuyr13VdQIYNcS7pu1Ud5S9yzxg6ImNgdTIuJNSHrn0zwUrkBzkVEtzFEv1MI3Tk/wgd1VrQLXdbFg6JjgyOrWe0N8LkMx3or8PmHam+ypB8aFnur1iVfZeryMjLiwjubEkoYiC31auYzkiTPpB12fqy+fQn0JqzCpb2baQoeq9O2pmFBWFtzk2fUpxTrOmK1DsjEPykewjesEezzzcTlu0p3NyNnr3MoAY+WKmziJZJVkCZmGOxWstFR6sJmZWAayyIbtpN/Tp/7e55i4SDFwL6cT4blKTnIe7U5kOrOxpNhIW2JnVxMjjta5nN8PxNt8CTES/CNiuy0e1xrHJrFmTbcK5aruIu/41EsLQsN 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: Deepak Gupta `fork` implements copy on write (COW) by making pages readonly in child and parent both. ptep_set_wrprotect and pte_wrprotect clears _PAGE_WRITE in PTE. Assumption is that page is readable and on fault copy on write happens. To implement COW on such pages, clearing up W bit makes them XWR = 000. This will result in wrong PTE setting which says no perms but V=1 and PFN field pointing to final page. Instead desired behavior is to turn it into a readable page, take an access (load/store) fault on sspush/sspop (shadow stack) and then perform COW on such pages. This way regular reads would still be allowed and not lead to COW maintaining current behavior of COW on non-shadow stack but writeable memory. On the other hand it doesn't interfere with existing COW for read-write memory. Assumption is always that _PAGE_READ must have been set and thus setting _PAGE_READ is harmless. Signed-off-by: Deepak Gupta --- arch/riscv/include/asm/pgtable.h | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h index 9477108e727d..9802e8d48616 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) */ @@ -594,7 +594,15 @@ static inline int ptep_test_and_clear_young(struct vm_area_struct *vma, 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 Thu Jan 25 06:21:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13530085 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 64B8CC47258 for ; Thu, 25 Jan 2024 06:29:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E77918D0006; Thu, 25 Jan 2024 01:29:55 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E005B8D0001; Thu, 25 Jan 2024 01:29:55 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C03B78D0006; Thu, 25 Jan 2024 01:29:55 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id ADB398D0001 for ; Thu, 25 Jan 2024 01:29:55 -0500 (EST) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 81051A0791 for ; Thu, 25 Jan 2024 06:29:55 +0000 (UTC) X-FDA: 81716857950.06.5FF26AE Received: from mail-pf1-f181.google.com (mail-pf1-f181.google.com [209.85.210.181]) by imf10.hostedemail.com (Postfix) with ESMTP id AA7ADC0017 for ; Thu, 25 Jan 2024 06:29:53 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=zj7J2CmE; dmarc=none; spf=pass (imf10.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.181 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706164193; 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=t1B3LjRC8FPiusELMKgJyB91QjKz3YM9voAFk/nY+UE=; b=nMsmkS1sO30d06WnY67wng3Q8A82t2fPYl2C2+b7RYD7HVhSSkaS7WQVpGbbEMGQnhbKY3 pl5r+kNrhERDT3Wj964tewfHozicHclBHzyDwaoczX53b8xay748897vMHWNPl2+sAi9t+ nBpoYZrqqIeDqayAcPRBSLmagq4tajM= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=zj7J2CmE; dmarc=none; spf=pass (imf10.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.181 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706164193; a=rsa-sha256; cv=none; b=8X12UFEfxa2lg/ogKiG5BWva+asa2+I40w68OkCj19ee6RKq/uWu3BelGvUx0h6JNeNHg0 auxtjo2I1U0bVCuwB6GosQyld0Nwd5mUj1lSQuCw0Pw07AhIp5ne0+wrL5MnAbNXJ/+wrk k8KU3qBcf3II41c/BYHTo7xdLsRPqYQ= Received: by mail-pf1-f181.google.com with SMTP id d2e1a72fcca58-6da4a923b1bso4575498b3a.2 for ; Wed, 24 Jan 2024 22:29:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1706164192; x=1706768992; 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=t1B3LjRC8FPiusELMKgJyB91QjKz3YM9voAFk/nY+UE=; b=zj7J2CmECQsW+dT/abTDJLvoSqf2fcPXMqJOrqJ/D9jHLsiORAvvKz8jWfIygSsgRD Iyih3MocjtztIqMfNmzvB/wKjONqSwcBaKhVLUPHD/7hG2yPcfztLQaVUuwxzTqjIUSG 2rWMaUL8QV0wwp7zlYQ7mj6HodTt8N7LEpUxQmM8TKpU/BE+LiMz7atMrXWQinb08FqY S4y3iMAC5lEA0bsP6W25LNEvgAgw5gLEC8S1SBaCKpjottUtYN8cG9LgQRmZ1wDNEuBl lDt1TaR0SJgZp83CYSWAFRIDJMJbEi8ekxQjzpXfDnDXVVcVyBD9FHDvuNKPhFoMPWZR H9Mg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706164192; x=1706768992; 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=t1B3LjRC8FPiusELMKgJyB91QjKz3YM9voAFk/nY+UE=; b=M+WoY1Za5LY/BVK3gwJfdvfY9EnQf7jpoPdM5RaMF7HdtJLv6L9DOLCgp2v/ZOJMrl CfSWKJ21hZl+KPaq9JkXUYVk7v95WUoJrBx/vEbiSdDCqOTsz6o4jEkkLKIYcZ9BQ7gX gruCs/+8i41SGHijsGmBIuQGOk/Pk31Rn38ekFj92MOjnmIuYPim8RBiU72WsXteJFK5 F/vNcl/CXePgruGOMO78/rwOfmlOMZmApTU/2O7CvpG9Zf2GFluBo0Hm7cMkgGEPJREm 8ln5UznVj6x1MfqLOnMf4sZvNkHSQNK0w1vwTqn4NTm3xOODY9JcKhJw9wC8OovVY0PL cqbw== X-Gm-Message-State: AOJu0Yw8qqNvZ8PhWooUl1m7Qe2G5FTd+PsF84t8SEKa/ca3LC8TKHbr SKnZjvXX3agKjhQLWfD/hlBFoSauNJ1YDmeQyzde59Fi5tqY5cV2aL7wWHkFwpU= X-Google-Smtp-Source: AGHT+IFe3XrkFPRdJ2UiIXYbbLc9t81EIm21drknEbRwnjfGJo5iR2n8OKkxXCB/AeScwGUu+RiNSA== X-Received: by 2002:aa7:99cd:0:b0:6dd:c3fd:45fb with SMTP id v13-20020aa799cd000000b006ddc3fd45fbmr215232pfi.24.1706164192388; Wed, 24 Jan 2024 22:29:52 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id t19-20020a056a00139300b006dd870b51b8sm3201139pfg.126.2024.01.24.22.29.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jan 2024 22:29:52 -0800 (PST) From: debug@rivosinc.com To: rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, paul.walmsley@sifive.com, palmer@dabbelt.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com Cc: corbet@lwn.net, aou@eecs.berkeley.edu, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, guoren@kernel.org, samitolvanen@google.com, evan@rivosinc.com, xiao.w.wang@intel.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, waylingii@gmail.com, greentime.hu@sifive.com, heiko@sntech.de, jszhang@kernel.org, shikemeng@huaweicloud.com, david@redhat.com, charlie@rivosinc.com, panqinglin2020@iscas.ac.cn, willy@infradead.org, vincent.chen@sifive.com, andy.chiu@sifive.com, gerg@kernel.org, jeeheng.sia@starfivetech.com, mason.huo@starfivetech.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bhe@redhat.com, chenjiahao16@huawei.com, ruscur@russell.cc, bgray@linux.ibm.com, alx@kernel.org, baruch@tkos.co.il, zhangqing@loongson.cn, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, joey.gouly@arm.com, shr@devkernel.io, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [RFC PATCH v1 15/28] riscv/mm: Implement map_shadow_stack() syscall Date: Wed, 24 Jan 2024 22:21:40 -0800 Message-ID: <20240125062739.1339782-16-debug@rivosinc.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240125062739.1339782-1-debug@rivosinc.com> References: <20240125062739.1339782-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: AA7ADC0017 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: hwpw19m13d8nobjt3es6m963opwjojrm X-HE-Tag: 1706164193-241199 X-HE-Meta: U2FsdGVkX18dLmBtZpgj9dm7/CxXeaz86GlSDmTqo+dcxej2wLb9NsQdX4+LehTmp+gORc/LrjRljjBX+mqOajuqAUiK4rEHirEl7NjM3pdh/Odla/MfSe+df8UkLnjgpxtX9BcgNZgL+wrUqVjmdavC3IONhFnQEQvxmcqIr1KtHQoYDyLVIYWLVqWjg2OHsw4cZqAIC8A7xi6WOzj2/WyxlJZQtgqeXNDfHBvwtcA7VydorZBIOTN1nzlj19BNOzDMTi9bdTNBemf4VjtqoqRuvIn5KQM+0/gjR8AdSh852DhLfpSEW53VDoPHSfYKj1N08TskgmgvdWZgIVCEZovA2JbNvNse9uwQBaMcP+8Ta+Ea6EQ8Z7S0dASvEW3+tuB5Xh646ffxJhYBm+9qe7jzVk/9qfV0eHNzfKpeQ9pv2mJhbOB12oNC37/YRL8c1HvKm93tZQRoo8TNUPZub3QrNxtAtWVrDTB0V61E5pqSmEfB5wFpqFullH2CEuveefctjH8ZTYoT2fgVFRmMOtrAYXBDKsoihq2WKkJXUvdQfwA0hG96ZzlooN2ApvOFtJOH/uDbiPXaiegrWurMVWNDr0FwXCVdsNDIEFdK5oLnZaQXfLaxb6U6PN7jRXcjzGOE70dKaZ5l4mW0AnHN6lfjuv3RtTzqhKNIWneKo890A/GAvLxfs6emUqZfUJBJyCKUo3QYi6aHqwZAlJi0pnyjwjFRphMhf+khlEYY+4uX1MxXXiB374fpUQgBmNaUZPaIb8yzotNC57kmTT7oHvCr/yg+zjtodKHj1TJvs6JZmPNxkU3tmHql/6ED5E6PKry4RRYVOljbCbasbypoddju2ml06rwouVmT1H60AsnHhsHLyIMVxcoq89Mk2IxNo/MxDtWOUYpvQ8FFo0WNswUrzfzDvujd7IGUb+o3nToa+C3R5iZEEvDQs6aSkwh5O4qG8nSI44WqXaE9A/D izRfX589 Zu0OH7eTq+nFGaJ/oWzpHCKxHFWw4kx4fZua14fe/iH/lSgHLhLnDW95TiGxODrNRnDfMHOY3QLkNKxJ5uL+1Rt6oqRof1Xw00HmXIpn/lwDnmsADvx8ciwGEzSZE6XmHstQkXtHsXgHFYRJSXHAFI++RqjXcNHUakt1umkwSCxZTMztiISytOEZhudOwrtyi1vSt6EsjnXp+E7kYmc/tWYOE2iKoJCtd7Al52/5fehA3c7H0iO/ZH/s/EMt3n28iradvSoDv49VIYbzw2lwEzFvZfHtb8TdD80bd4mBuAoqeMJAeKTytnUGzcVkdANJSPOJqeF1e2OhXPKHiPe32HYuUaj0staRlwiVhhvpFl99HXDia9V6IRYrQ0j+ZGf/1PHu5oynNafqWl/Fs8HrvX3xzPEXK2qbfV1ZmZrhzvndUM+ro44RSGtII0U1PwB2guXYHO0B7z7ESVouPM60FmOwl5ro1foapYeY7ysDCdfT7gP4gVClHWX4sOw== 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: Deepak Gupta 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 guarded control stacks since they lead to windows where memory is allocated but not yet protected or stacks which are not properly and safely initialised. Instead a new syscall map_shadow_stack() has been defined which allocates and initialises a shadow stack page. This patch implements this syscall for riscv. riscv doesn't require token to be setup by kernel because user mode can do that by itself. However to provide compatiblity and portability with other architectues, user mode can specify token set flag. Signed-off-by: Deepak Gupta --- arch/riscv/kernel/Makefile | 2 + arch/riscv/kernel/usercfi.c | 150 ++++++++++++++++++++++++++++++++ include/uapi/asm-generic/mman.h | 1 + 3 files changed, 153 insertions(+) create mode 100644 arch/riscv/kernel/usercfi.c diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile index fee22a3d1b53..8c668269e886 100644 --- a/arch/riscv/kernel/Makefile +++ b/arch/riscv/kernel/Makefile @@ -102,3 +102,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..35ede2cbc05b --- /dev/null +++ b/arch/riscv/kernel/usercfi.c @@ -0,0 +1,150 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2023 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) +{ + /* + * 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_volatile_goto( + ".option push\n" + ".option arch, +zicfiss\n" +#ifdef CONFIG_64BIT + "1: ssamoswap.d %0, %2, %1\n" +#else + "1: ssamoswap.w %0, %2, %1\n" +#endif + _ASM_EXTABLE(1b, %l[fault]) + RISCV_ACQUIRE_BARRIER + ".option pop\n" + : "=r" (swap), "+A" (*addr) + : "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_SHADOWSTACK, flags, + VM_SHADOW_STACK, 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 Thu Jan 25 06:21:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13530086 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 1FED1C47DDF for ; Thu, 25 Jan 2024 06:30:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7E8358D0007; Thu, 25 Jan 2024 01:30:00 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7708D8D0001; Thu, 25 Jan 2024 01:30:00 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 54EA68D0007; Thu, 25 Jan 2024 01:30:00 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 3CBCC8D0001 for ; Thu, 25 Jan 2024 01:30:00 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 1AD4D1407FF for ; Thu, 25 Jan 2024 06:30:00 +0000 (UTC) X-FDA: 81716858160.08.67F5DC3 Received: from mail-pg1-f175.google.com (mail-pg1-f175.google.com [209.85.215.175]) by imf11.hostedemail.com (Postfix) with ESMTP id 4A08440007 for ; Thu, 25 Jan 2024 06:29:57 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=lz4Q7XvG; dmarc=none; spf=pass (imf11.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.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=1706164197; 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=ElZ8v003TL24bpwLuLWIXAZR4LH6JaW8OFYo6Q46gr4=; b=hM0EiHrZIvwm68IhhCz4S46snzQ7BM9mssCFHvyQtaCFBNLT19HsW2S+/o/mhNTydc6nuf Xnwfl0/PVcY68jMpm4Ww89A/RoMLhVoABvtMDfsBmjSzM7INBUt/1aQwS1AUJRTEG8ouBF dT1AsMv0fuCD+APcoV52XFi+GXvNs0k= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=lz4Q7XvG; dmarc=none; spf=pass (imf11.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.175 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706164197; a=rsa-sha256; cv=none; b=Gik+PvGSp9TfO1yyt6p+Z/+CyPNe5V/MsXsQhMmHs8kz16Sc55PQQSoyG90lRGKK3wt7eB 1029nkzT4BLLMsfTuQiVMnEPDUOSbviToz6ZdTWORXFNx56+zeiJesG4hNMIaMGTnOgnU6 AjnRdOUw3S0xB1G/7CK7o4nwwuXjtwU= Received: by mail-pg1-f175.google.com with SMTP id 41be03b00d2f7-5cf2d73a183so294458a12.1 for ; Wed, 24 Jan 2024 22:29:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1706164196; x=1706768996; 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=ElZ8v003TL24bpwLuLWIXAZR4LH6JaW8OFYo6Q46gr4=; b=lz4Q7XvGSolcYbUms225MGmMJCmMDiKfsHTP2+gBkNi9FecWQWLVJ6hf69Us8mrDxn XUfHlQak/eznhONTPJODwnocXwc3FoQgVJtdwqxvCG7Nu/Y3uTwYy6AIC3uezKZU5jZI bp/aopqpyo2k58tB5VMH6ClRgUo7vE7dYmjfQhLetU52gpVUv/dvLUdDXwre8hxrE1X3 KWhi8meBBvmKcSl38eRyalSNDSHTFG4yZqN2JZdnRVSzLi6mQDRe9YbRhY6Id+srSpcc K0w2fyHtWM26dxozea1/exWx+hOPpW5rhs7v1IWQBo1kD9jU8vj96WQixBcnrKA9XPEe SFNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706164196; x=1706768996; 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=ElZ8v003TL24bpwLuLWIXAZR4LH6JaW8OFYo6Q46gr4=; b=pMPhiTsqi19geoLrwndgXBbrxuPSXwiOEMdtt8dizrNWamERa+yU031kJUj/tBZPie DCQ+pc7VWAVfaxiRpc7kawg82ZyNjpTPbpw2Lv1ZZtdKVJv4AfCNiL9yRWmkZ1qsc2Cp WL3LtMz1g+qESGdDDEjtsqHTDdgg9udgMVAP0FO2DB6Jwn9LF8GSs/e708jeXKzx5t91 2SxqGCk+y0wH7T6kLASTH8CO1n5vQnwuY4rwDk+et1dmPCL55uFFQZfLkgux8UB1sUw1 R9vyJgOinUGxT0pVELJx7CmUIwRjsDtRm8tZ9e8d9w2yyjOtHPkO9GET+lC1kH7RGNRi ewXA== X-Gm-Message-State: AOJu0Yw3jM9EV8zcdolUW83t+zGjinI0SEst519lkBsHC0gDFK7N/EwO nEooGkNpMDjFzt89dKC6oOdoHYfxcGhu1/j3keRMLfTNV7JUvGS0Wh9yZk8sp0Y= X-Google-Smtp-Source: AGHT+IFX1+d0cQXBDGIq+89psVK7k1OJN/xRk32F1GoAKNNphyd9nJ4SL45lMaDh/Km+dlBcYHzICw== X-Received: by 2002:a05:6a21:3513:b0:19b:7d67:5b35 with SMTP id zc19-20020a056a21351300b0019b7d675b35mr808275pzb.36.1706164196007; Wed, 24 Jan 2024 22:29:56 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id t19-20020a056a00139300b006dd870b51b8sm3201139pfg.126.2024.01.24.22.29.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jan 2024 22:29:55 -0800 (PST) From: debug@rivosinc.com To: rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, paul.walmsley@sifive.com, palmer@dabbelt.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com Cc: corbet@lwn.net, aou@eecs.berkeley.edu, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, guoren@kernel.org, samitolvanen@google.com, evan@rivosinc.com, xiao.w.wang@intel.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, waylingii@gmail.com, greentime.hu@sifive.com, heiko@sntech.de, jszhang@kernel.org, shikemeng@huaweicloud.com, david@redhat.com, charlie@rivosinc.com, panqinglin2020@iscas.ac.cn, willy@infradead.org, vincent.chen@sifive.com, andy.chiu@sifive.com, gerg@kernel.org, jeeheng.sia@starfivetech.com, mason.huo@starfivetech.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bhe@redhat.com, chenjiahao16@huawei.com, ruscur@russell.cc, bgray@linux.ibm.com, alx@kernel.org, baruch@tkos.co.il, zhangqing@loongson.cn, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, joey.gouly@arm.com, shr@devkernel.io, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [RFC PATCH v1 16/28] riscv/shstk: If needed allocate a new shadow stack on clone Date: Wed, 24 Jan 2024 22:21:41 -0800 Message-ID: <20240125062739.1339782-17-debug@rivosinc.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240125062739.1339782-1-debug@rivosinc.com> References: <20240125062739.1339782-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 4A08440007 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: mkesiuounidkakuky6rhwofsaagbjapn X-HE-Tag: 1706164197-491857 X-HE-Meta: U2FsdGVkX18OhqF/c0wT6P8CQIv8DHqJWBy8dNTQznamDny9EocBa8Q6kW4V2/xMm5JmL6d30J6p4ENaahrb5p1KhQWCBs2MJslriPnb3ER0zv2rcluHzTxzRPqCLQdjuEzGiJHd96xcjI8KFywUyepKOWEeabosKUjTFfdf2wCeLCDPc+jvrv+VWKGY4sAKz8q+oJb8EVVl30BrqENsijpQ380Y7hQBPi8nJRqxj4ZIxH7zLdB/xBUlIDIinBp5h89fIUESgZ31z7MZl2j1K3T0lSzL53lA2IDmBm/YdlzpD36yT8JJ53noPB7XhgJJb09JpiND45nFo1CCZ/BkdZRwd7SmYao43svQnpZV2Jc1U2stOamO03Baa3cQdTCDR5AEfBUm37F3TkTCbPgmwLzBBGzCOZ+RoljA3mgBKzuUQLV2QDX1zmNfEqdhG5YL/01JVxEB2KUHWcwh+wS5BnoRri9C1H4X+7KAHVADI49FfsrGsMihZilWi6u3zEqkXhpan5aygaG8qma0SWPDgUCjH1nj5hjtsMPlxG2G8j+AUjRhK3TaxDreNZnCr9E2CsxRvEpSltTY9aawBO9GwL91QFF+VmgYQY++kLg9+6LWsDL6dCCoim9IgFTkFI7JnrG/tT66yVaUSNzSmR8p5uYVHEzZtiKN0GnnS2XmH9YJIVaRZoSQByLlOHDIu9vyjDXucCJpSjpONYxZyKr5OOckAEfjM6ULc3rKt66lo82H/FTQZfheP26OABLbYi/lSpy5aJ1Ff+L/nUJKNBwa3A5IG1x6sMklCpKAlnr71A1YusUg/o2UBnUpA38yINnd78xSwiCtO5WN8sUykvcv+XnO2eb2q3n6Vwk1FGDwtLYz8Gq9S5sN2eUF5x3/sn4JwlY/IhG25HDVMsGXC6VdJGAVpvwHDcghhd4+OLSxz7SNEPzVVeYFRBACUWNBA+ofH7Gqpbi6Zv5AkYbBjRO +MXrFZlk btGqV16llNJSttuzJmF4gSyoP6CNIq0TFQ6FwsZQEO3x7SO7kFPskS5ZZUiIvaajE30/l2zQxLWdihY7PuR1hSk4bA6zlKUmfqk4s83qKeH9UbU2AmVMvmkfIqsi3ywqDrxwc6+a7VFQfn8T67wf7OBLTx+vvni2i+DfyJ14wn4rYk8UsRJ/jCbxN8AYxrizmW76udr8x8or6kkrBO2mntdHZ2GxXeAJbzLFbIaWcS1gY4c5p+pmoC3NvfJGuPWuBXR41D74toX5qgnwTIjNfqs7T4rFrksfG7VHOg7347kTczY6pfWIKIY8B8FbFU1eUKDEpck3E5GuB1dLkJV1tto7RgZODk1Fo+tdZD2aymf7TXF3Bj2ppGOzrZflEwRkqPwF9VumKJkgMTNanTh8xEzy6ejafczZbdDmjRo/iG5XerM3He15Ixs8z7GFLFTnaeKhm 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: Deepak Gupta 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 | 10 +++ arch/riscv/kernel/usercfi.c | 121 +++++++++++++++++++++++++++++++ 3 files changed, 170 insertions(+) diff --git a/arch/riscv/include/asm/usercfi.h b/arch/riscv/include/asm/usercfi.h index 080d7077d12c..eb9a0905e72b 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 c249cf3d8083..a2b2a686a545 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"); @@ -194,6 +195,7 @@ int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src) void exit_thread(struct task_struct *tsk) { + shstk_release(tsk); return; } @@ -202,6 +204,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)); @@ -217,11 +220,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 35ede2cbc05b..36cac0d653f5 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 @@ -148,3 +183,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 Thu Jan 25 06:21:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13530087 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 48E3FC47DDF for ; Thu, 25 Jan 2024 06:30:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C64478D0008; Thu, 25 Jan 2024 01:30:03 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B8DAF8D0001; Thu, 25 Jan 2024 01:30:03 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 993AA8D0008; Thu, 25 Jan 2024 01:30:03 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 839EF8D0001 for ; Thu, 25 Jan 2024 01:30:03 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id BC69C1A0D2B for ; Thu, 25 Jan 2024 06:30:02 +0000 (UTC) X-FDA: 81716858244.13.62CD693 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) by imf18.hostedemail.com (Postfix) with ESMTP id D653E1C0002 for ; Thu, 25 Jan 2024 06:30:00 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=b8rnb5dF; spf=pass (imf18.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.179 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706164200; 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=5zflxddcDQBcU2vSu/tVz8rqPQxJomCL53KP/QLCzbU=; b=SFUow7Z5T5bUXtnYyOQHLRuwIi0DDcALOz3rsxpA40fZbOHpES+Q8gPcXCk4sJOChL/At9 fxQO+BD55kHQcb4GPWKfx4Yzyz+zv/bp8C2LWnfB/aote5er/8PqOR/aaSeu/7AdPYDOC0 1Qh32jlKGT3WuB4dzeYPH0dsyv6bh+Y= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706164200; a=rsa-sha256; cv=none; b=uF3f+zdEO4lnG79CzJ0StEeYQtppy03UbmcfiRso8WpC/Tv0Lpnij++29MaOeP7yEQ1oOV qlZFYuZMvywkICsTHlVGkx/7q8nm/yTGegLUuo5tBB1FiL0sHwP8YfmBD/oPzECGKI7YeG 3ZasO7rLGoIIOXlNFxctGEJHYS78l2U= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=b8rnb5dF; spf=pass (imf18.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.179 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-6ddc1fad6ddso959720b3a.0 for ; Wed, 24 Jan 2024 22:30:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1706164199; x=1706768999; 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=5zflxddcDQBcU2vSu/tVz8rqPQxJomCL53KP/QLCzbU=; b=b8rnb5dFsNk+0eez6dpcmq9ED+O1ibc1edHSIr7VabcGVh1orE4YlgtfhqqS86TdIe rwEj1ue/BJOzF8Y1dElqHzQJcbJ4GDmFDZpQvFgvsg7G2XY+hi76XQsTseqPfvM0Th0T sRJ1XEhPYnaVTCE5nOiQ/a68AucnsMT8gdrcQvfVGgP1JPgod7Bh2tvV0xRnCvAA5ftu ZEDwR8hmDldlUJNumZ3eb5aQDpLEt5jQlwuKj0DM6SzHnOIrkXnp/Lj6yP7Et0r/Zv+v 8pWIFqW9PZWFhqxdKpiOPcJIXamknA5dYV/pOOKp0Pm7DfUsxD1yB0PFrsiKny1EES/K w9iQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706164199; x=1706768999; 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=5zflxddcDQBcU2vSu/tVz8rqPQxJomCL53KP/QLCzbU=; b=Z0/2C+jzERlNHHq/Sm6d9gnLfSd2g3Z97K5UpWhC50xYJMaF9xp83PcummSDG/kls0 Fgy+7QMblWl+SdrtHeA5OZMvPvqFNUFYdlAeNxeeLW1r5LxqeLlKKvAsHA+Pc/4PfZ5Z mXU+49hJHaM8nX/mCyiWIoiY9rLyLRVVbmD4kWybGz9eHsiB0f627gB6jzJ4HJ6jdBg7 IOpn6Jn0q4gC/iI7RkU+bpYP4yZ66A6++jKOnyZvJuV3o+33rWS/FrIARD5yoGdisRQM c6oAvo7pthhM6M1oD4LRh4X8FsZ0kgKnwZcmGPJdx69mCDDG2rsulixMgxiRtGBqUHBY uCOw== X-Gm-Message-State: AOJu0YwZyi3L++cZQ813dzD/bOGX61gxNRtiGw6fYGmInbVYxf8znQmt g+NrkeLGH3ldrObb1PMs+CwMjZq+ECQOgqg0WfbhRNXBjjkjW3C0UHi39kiHR+c= X-Google-Smtp-Source: AGHT+IGVLurI7ehwFwOqiPymyWwdcwBSGSxuQ1bEK3sHE/LCFMR0aIK1c3iw9hogS/394Qm8iTIo1Q== X-Received: by 2002:a05:6a00:198c:b0:6db:ea8b:52ae with SMTP id d12-20020a056a00198c00b006dbea8b52aemr360157pfl.66.1706164199621; Wed, 24 Jan 2024 22:29:59 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id t19-20020a056a00139300b006dd870b51b8sm3201139pfg.126.2024.01.24.22.29.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jan 2024 22:29:59 -0800 (PST) From: debug@rivosinc.com To: rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, paul.walmsley@sifive.com, palmer@dabbelt.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com Cc: corbet@lwn.net, aou@eecs.berkeley.edu, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, guoren@kernel.org, samitolvanen@google.com, evan@rivosinc.com, xiao.w.wang@intel.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, waylingii@gmail.com, greentime.hu@sifive.com, heiko@sntech.de, jszhang@kernel.org, shikemeng@huaweicloud.com, david@redhat.com, charlie@rivosinc.com, panqinglin2020@iscas.ac.cn, willy@infradead.org, vincent.chen@sifive.com, andy.chiu@sifive.com, gerg@kernel.org, jeeheng.sia@starfivetech.com, mason.huo@starfivetech.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bhe@redhat.com, chenjiahao16@huawei.com, ruscur@russell.cc, bgray@linux.ibm.com, alx@kernel.org, baruch@tkos.co.il, zhangqing@loongson.cn, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, joey.gouly@arm.com, shr@devkernel.io, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [RFC PATCH v1 17/28] prctl: arch-agnostic prctl for shadow stack Date: Wed, 24 Jan 2024 22:21:42 -0800 Message-ID: <20240125062739.1339782-18-debug@rivosinc.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240125062739.1339782-1-debug@rivosinc.com> References: <20240125062739.1339782-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: D653E1C0002 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: rs8gn4o4gy4s1ncdrdcxrx39ikfjt7e3 X-HE-Tag: 1706164200-178668 X-HE-Meta: U2FsdGVkX1/Qrrv/PDptnNqbZW+GG2cef3wKyRvTU5jRrXKfb4BS6V+QD8wLuGB3TxHzwa7G0LgXv4M3yDE45lO7GjVpa789zFsx+Of9ow85ESRaaSr1RhWrGEYGABtftUEafx9L544k/O7pr2wYpElpXP39xZIN1civARG50mCYTZN1hJe3qPX2FPI9eZb480bXS1aFUpW342SSNag6O3Z6tiX1K4OcsUgYAFlJwOvpGQlqLXhHxQAdyHS0JqVEKxkNRRZ9mbThlhY51oTj1FONdRYn8H44YCu8O4M04CXgLXoexGXxU/z6tTL+rAjhjdxmn8uRQ7E2lcOjP9yW3cbDr/phUUEb92rlrdxWwC3sb5hNGlp7uJN47Vv1S/qet8uTHIZkDB76GbArqtFtTKGVSW/XGFDerKUy3LFW6VysjFQSNLt9vvE+7K3Thr1fkJmdGYzHoDGjRl1wkdVxtJdKoZNPpXlw9FgK6uCEFHP/ZAzxGc0jBAZ7gKTDkcgdIYVPXPtVX1BsfgfkIHOsx1GKG7Oj9NSjwRX8DnnG96RTsJ3nF1bRnXdoYH+k5Nmlfb1xuZdPlnki71eGPylMyxWegBLnmmqgFZ5brXV4w4cNghOLF8h6IEKVCUOdFzYxTNvrNpdHRjK6NqoxTdi56BF+WjGrNBCEz4J5dO9l+dKDkF1+JrZJbbDRZvkL2BU11hLKaL07G276bNPL/aLOylvW9CTZhhFZUivKq3DoHJkQgyqvuYw+gtm47whYVC55JSddZkjHxu+5AzD18/xDN2hXlfmmJL48kgFTrAtydMIsduCBAuBjSUZM7V+niS6FlBjwek8wUEXqexkThCq6ekA8v3b2e9NcOI5717BXE4DR5VBFE8nMMs2qp1RN24yYCkSJNtTEP6QgWPsbx7Jh9Flstn2eA/oOcDSgG6MuM1y9A+XWQK7N8LtUiV5urkS0WNbEvCPNfw11Iijx5L3 rNRO9pEi 6DnSR5lbm7N0+oT/l+v+Q8NCBiKGq/vZPQeu00h7+Z6Rd3ZsT5Hc28AojiMWJE1oD2gkf5MHPOxxoHyEJ6TyZp2182HCwFi/aY2ejjqKE/qLbHEtPQaK/2iISBDNgZx1iggWiuPI+I/zJmJjJalrrFtw4utWUMzQqLJeUq+3r4wUil5YntrY0L393anrwzOfdSCavXdHnsU0cGOCi3ZP59TJNtCO16ehFuJdPopzKS3Ya0NtRhMQ4zi44Y6HUIQ4pDJRJsgc46PuXHYPaGdo3G7JEY4vHRfkiUpHVg//rNlMVFz5g2MNaBGwhxike14+CBk6YN7z8we5crXtxhCD71iBsjGKwcNFtWgZo06zJ2k5xHpxIFGEua6WFvx1Mr7bTEKJfBiTE5erl0yjG9z0fu6aje3ZA6FpK8ET3h+stimeBh3K3sPigjDC/iLcRFSCl9tUGcrh324qrChBJiGDkqOfqFxquxv9s/OetVgU6Gv3cJfAHFRTF1ZROlHZDeOiAC+Xx X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Mark Brown Three architectures (x86, aarch64, riscv) have announced support for shadow stacks with fairly similar functionality. While x86 is using arch_prctl() to control the functionality neither arm64 nor riscv uses that interface so this patch adds arch-agnostic prctl() support to get and set status of shadow stacks and lock the current configuation to prevent further changes, with support for turning on and off individual subfeatures so applications can limit their exposure to features that they do not need. The features are: - PR_SHADOW_STACK_ENABLE: Tracking and enforcement of shadow stacks, including allocation of a shadow stack if one is not already allocated. - PR_SHADOW_STACK_WRITE: Writes to specific addresses in the shadow stack. - PR_SHADOW_STACK_PUSH: Push additional values onto the shadow stack. - PR_SHADOW_STACK_DISABLE: Allow to disable shadow stack. Note once locked, disable must fail. These features are expected to be inherited by new threads and cleared on exec(), unknown features should be rejected for enable but accepted for locking (in order to allow for future proofing). This is based on a patch originally written by Deepak Gupta but later modified by Mark Brown for arm's GCS patch series. Signed-off-by: Mark Brown Co-developed-by: Deepak Gupta --- 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 15c70fc677a3..df248764bcec 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -4170,5 +4170,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 e219fcfa112d..96e8a6b5993a 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -2301,6 +2301,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 @@ -2743,6 +2758,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 Thu Jan 25 06:21:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13530088 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 1DFA3C47DDF for ; Thu, 25 Jan 2024 06:30:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DBA438D0009; Thu, 25 Jan 2024 01:30:06 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D3C768D0001; Thu, 25 Jan 2024 01:30:06 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B19FC8D0009; Thu, 25 Jan 2024 01:30:06 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 9C0BF8D0001 for ; Thu, 25 Jan 2024 01:30:06 -0500 (EST) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 7267C1207BC for ; Thu, 25 Jan 2024 06:30:06 +0000 (UTC) X-FDA: 81716858412.12.8AA3F4A Received: from mail-pf1-f172.google.com (mail-pf1-f172.google.com [209.85.210.172]) by imf06.hostedemail.com (Postfix) with ESMTP id 6D09318000A for ; Thu, 25 Jan 2024 06:30:04 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=NFqPiFZ9; dmarc=none; spf=pass (imf06.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.172 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706164204; 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=ZGW+3ds4X7hyu8QcIwFbpgQ3FZs37tf1ecbtDkVWTR0=; b=M7vXvW5ZlEt978QD7SkIfBzm6Mopqop+d6+vh81VBp94eFI9cjo//kZXvZt45Z1/FcOKLa BapRREwZdwhXL/6UyRF93/OxYM2M5vBzYKqzXPfesRqvld0Ji2JFOt0YfqTPOzVM9oX6ja E1M7YsYXpHerhrd/SRhx7FiW57d95P8= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=NFqPiFZ9; dmarc=none; spf=pass (imf06.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.172 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706164204; a=rsa-sha256; cv=none; b=ihruMUv9pFFIYHocuZeFQ84wY2f+DRUsaYupNrbNaC80XGbUxHVZhZfbvLUm0+uPqSTts4 CkHfwlCwL2G4hxtfw4NgBI0qSCiu97aPhgcxwFYDuIxXNup8v8q3N5UcnG3WApLlGWsqbG t7tjDo/+9f/FGdy61QfukP6qX7Lc4SE= Received: by mail-pf1-f172.google.com with SMTP id d2e1a72fcca58-6dd853c1f80so290753b3a.1 for ; Wed, 24 Jan 2024 22:30:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1706164203; x=1706769003; 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=ZGW+3ds4X7hyu8QcIwFbpgQ3FZs37tf1ecbtDkVWTR0=; b=NFqPiFZ9AVGQIChzVwUP1UY4zxi9SZITOxouvsYhK56WPzWyQ21hjPs7WPYsdW301n MNxy8Xbw5Jswjh0n4MdViR5Vo5Hy43kasQoT3SaXoWzOF6PMWg+ix4rAWQ+X8SFSr6J3 Oxdq6jokf0KbLI8QA4/xV5zsZ0K75V1H/L8Y7kRG1Kv2Ns8FcJjK4ZCMSmWm3+aT7w3I 4OFPf+10IV9j5N5tm9xKwwtXNTBPfB1ws+iRDiKa7YbS/kGt1G4K+u6JWmjB+f+uHMl0 BqXYdRhSn3DzPg4DdIqUBqt5Lf9OFMro19ovNVV6HdbM9viVFSQIN2XC4js138rLJEUS wjeQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706164203; x=1706769003; 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=ZGW+3ds4X7hyu8QcIwFbpgQ3FZs37tf1ecbtDkVWTR0=; b=LhQxT4HFe7pI8QHzVJRt+RDjrq+IdUMBOBnIbdkgHDp5aPONKdIIrbK3O9Td61eBK0 8oj7/ilPrlxpw38b35hpyPjEYu1N37tTqe9t3coZYuYvXKnurV4CRaYJIgG5y/uShndl u6SVnT1PJcG0ysJiiSJ31SjECUbCi8QO757A/dYm4HG8HfH+sY86QjkMbFWN6J3d7Ut5 JPUYk6BSIM++FfU2cVlChMreU4exZchabZg/GEb+CBQPCQxvJZ3aV+8CdVDLosm8epM9 rRlW7btjIss7eEZfqkXEOwVezC+86kv10tasgW3MWvGtX2CLPdeVhRu1btmkMzL2YVZc YxNg== X-Gm-Message-State: AOJu0YxuWZ+Sci57wkzEKdg2SwJl4cVzRfBJ7gmKL7/wo3xfKh5kWpkp c+idDcjLsp4pq8d9gnmyhHzwCOdPoiPfkvnGWHFd+GpUZ/Md6rKwv6PwVVTNHmg= X-Google-Smtp-Source: AGHT+IFX0O2DMMykvx1boq4E3+vew5Q+ucUD8KeW51e5M6H2MI9XnhwlSZDCE7ZMzSEryAMcjaVeiA== X-Received: by 2002:a05:6a00:b87:b0:6dd:c77a:721f with SMTP id g7-20020a056a000b8700b006ddc77a721fmr862372pfj.0.1706164203244; Wed, 24 Jan 2024 22:30:03 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id t19-20020a056a00139300b006dd870b51b8sm3201139pfg.126.2024.01.24.22.29.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jan 2024 22:30:02 -0800 (PST) From: debug@rivosinc.com To: rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, paul.walmsley@sifive.com, palmer@dabbelt.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com Cc: corbet@lwn.net, aou@eecs.berkeley.edu, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, guoren@kernel.org, samitolvanen@google.com, evan@rivosinc.com, xiao.w.wang@intel.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, waylingii@gmail.com, greentime.hu@sifive.com, heiko@sntech.de, jszhang@kernel.org, shikemeng@huaweicloud.com, david@redhat.com, charlie@rivosinc.com, panqinglin2020@iscas.ac.cn, willy@infradead.org, vincent.chen@sifive.com, andy.chiu@sifive.com, gerg@kernel.org, jeeheng.sia@starfivetech.com, mason.huo@starfivetech.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bhe@redhat.com, chenjiahao16@huawei.com, ruscur@russell.cc, bgray@linux.ibm.com, alx@kernel.org, baruch@tkos.co.il, zhangqing@loongson.cn, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, joey.gouly@arm.com, shr@devkernel.io, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [RFC PATCH v1 18/28] prctl: arch-agnostic prtcl for indirect branch tracking Date: Wed, 24 Jan 2024 22:21:43 -0800 Message-ID: <20240125062739.1339782-19-debug@rivosinc.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240125062739.1339782-1-debug@rivosinc.com> References: <20240125062739.1339782-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 6D09318000A X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: cxkhorgziwx6516wg7t3u94uicf85d84 X-HE-Tag: 1706164204-851319 X-HE-Meta: U2FsdGVkX19qxztHatCLnJzseUr2NRP2F6RUzbmjl8gyR1hr4VY2FuwaGFRopimdYOD2a5bHo9X5lEUkpqMBQNGIQoCTpw2x9dXkJOihXHZ5SaDWndOOZsgvsDrl07g6e492j9zOkq4SHCIwChbmR+4h0PXhX3/wfrNgNoAdEfX7T5FORC09EOuIgCrvieaQ+w0imaKcIw9GCzYatNr1L5vX7xMriHXtdwmsmUeGkVo60E0dFn0dgELJqV7hh//czwiZ14oplAUaRiXZcsuUhEhS85B9w57xJlcyVXL5fYqzS8aMl1fuqQUuRXFeb3m8rqH4ijShBPYk72A6u6v+yJkqPZ1SvUegNIQPeKGqtCrtURQuHZAnPthc2nnrcyzM14Vjv3l9c7ZfoQq5T888N10zXZk+1QULTWD6LOpMibMZSseC//9LAcCExNKPvzGTj8RzyutHOTu5J8TeyoejvUT9Du/4wUI307GIlkKsCTFinY4K0k+VZ84NraHckZ1O44ICXDUihsDFNlEZXwS9O4gvNMSYq4081Eio8cCfy5jUhX+oO7kOQ/KMHnB5VWPsgfgJZvjeHc61otRIIIAseHrl6sX6Umj8guu2qaSp1Tv25sOcS3VMs7aN6shCgEzU9qMYx1FUdgTPyB2VeqdBFwKby4UDwlchT641OxX/Zf6jeMhiAYuZ/8b/o6vkGDmE0XomCuX8As3EXpXbo0tLIbyeqPFlkQ++MgZshHYU6+PuvXd1uPer4s1i0wAM1Nt6mEE1fP7HFkWd0FsvsfkUXa4HQJjWnepDRzyjVHXackehjkwYaYfe6XMH9wc2da0DLyIwMA1EXoTNcFZzbp7F8XWb9oYCuS23Rr1vhp67HmrAKsDDn+JaC4SM09eLc2+er4eJBmRjTuelALWQcNUwCAJ85+JKyNGpP25AN/N2tT9z/AsCYnznVy5qrFnzUxo8dgE7iCn1/QxxOaETZ1z v6AIg9sm IRioo/JB0dEuLcZMBxXBJg+H6QRiripoT4geL7Hb3ixJ+YWmL7sw/D7bQl9kflWS5pH7Kd1FOY/OqaQ+UC8+QYGuk+WuuM6fhL//eFNmNW7LapB2kozk3DrxUTZO+4O2NgAtUSK8v2Fn/2eU3YZAWEizfjnSsKvuqUAzDuJ/7PjWTvOtG1088OajIJaMg/iStXtGHXVYtPZNkGthzw1ATr3za1JwcPWSOQ9WVQ83pF1MrfHLQocA1y7mejQHm1WRNBs36Uw54F4qy/lkWtcjbFvtAi+2HCrQhxMBSeuX/WtkGqYmcjyGCoQd6cm+4ekBXNW95hkuvw8UiX12DUjme6Q7WStEQiMJN2NOBafcIa5KInIhZqTZJaBgrBqzBIViAuIdUL84np4hh/4F6KLp2r9/PQ+8UXN8p/CY9gDbn/c59QpBB3oihoDJISyBt5RNruroq 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: Deepak Gupta 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 96e8a6b5993a..9e2ebf9d9859 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -2316,6 +2316,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 @@ -2773,6 +2788,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 Thu Jan 25 06:21:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13530089 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 4246BC47258 for ; Thu, 25 Jan 2024 06:30:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5EC958D000A; Thu, 25 Jan 2024 01:30:10 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 52BF88D0001; Thu, 25 Jan 2024 01:30:10 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 32BE78D000A; Thu, 25 Jan 2024 01:30:10 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 1E0968D0001 for ; Thu, 25 Jan 2024 01:30:10 -0500 (EST) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id BCF0914046B for ; Thu, 25 Jan 2024 06:30:09 +0000 (UTC) X-FDA: 81716858538.16.BBDDFAC Received: from mail-oi1-f182.google.com (mail-oi1-f182.google.com [209.85.167.182]) by imf02.hostedemail.com (Postfix) with ESMTP id E9C3A8000E for ; Thu, 25 Jan 2024 06:30:07 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=DcN83G6V; dmarc=none; spf=pass (imf02.hostedemail.com: domain of debug@rivosinc.com designates 209.85.167.182 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706164208; 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=uRwHXwAm0bShEXUK35E9/V7WUW2K3zxNpdrnhUWhVek=; b=D60j4mgM39xrnDBlXM34PzBgFAsygb3x+AvaM1OrrHSIjOlFUJzowx27F2DDfi2oYV3srh O//Cf6u6+EL6djk7WZPCCsg5X5UZ7oiJ55n+2nzkGHp1E32zVq7oOrXuiC0EXSZqu0ujzz l/XYiDn/SDEMW5DuavIATUBg8Eq5+7c= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=DcN83G6V; dmarc=none; spf=pass (imf02.hostedemail.com: domain of debug@rivosinc.com designates 209.85.167.182 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706164208; a=rsa-sha256; cv=none; b=YXov9hEOPmMRgnBB9ee76KmV3p/yepGADPMR1abNF/usRohFYvrKuqlZssRubZy73S2WPC 0C6IsW72Qeh93TSlXnhBFOzczo+jqyOAzkpw4YFt7k96RxtfvD1fQmsoOQYB6Ge0g+XR6r lqp1mOirtAfDzarrDKymqyno7AUpj/I= Received: by mail-oi1-f182.google.com with SMTP id 5614622812f47-3bda741ad7dso4930594b6e.1 for ; Wed, 24 Jan 2024 22:30:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1706164207; x=1706769007; 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=uRwHXwAm0bShEXUK35E9/V7WUW2K3zxNpdrnhUWhVek=; b=DcN83G6V3k1rE2xu4heOhaAET0RjyGwLENLGlBJjy5MaVrCCiDc1KRFAqHxCCUgSGM Y+8V3fL/km0vO62lI0+WkPmYiFyTZnwemVICWPreOmNaaRc5bWW1hJ3AjQXt5fZgCiFW BWY9GXfnfKFpuLHbCBV7sFb6vS5sH4rXIHtZ56CUjSi5DusPO9hPnTqm0Tp1ahOZSiuM 2CfOsYXbD1Qt6JZOF0TX4RB8Kj0y70kLlbSfVHNfQCxtByQe1tHvBcWo1VEmWDkNe0g/ QVyzUZQBj0H1r03chG7Cvu6x95ublER70v5dHzEn9DCUJAUs3Myo02//ykfWYIayXSM8 V3UQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706164207; x=1706769007; 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=uRwHXwAm0bShEXUK35E9/V7WUW2K3zxNpdrnhUWhVek=; b=ugmjph6/qstuMW7Piq6gJH/0M7A67ygDw/EXzDib6rG+T9IZWIJVFJkLIbD1C3x6Od IrEgtEf+HcmEVAXKrxURN8+DczXqn2TJh7ofGhFV0ahcfab9Gy7g27M1IzgX59KsFXWP Xe9UebGJtvQPePZZXTXHKSVntaebTrl9q1yKxYjCBmv5c8u6++H4z549n1LbPilbqD2P kdZ0TrTcoC+IurvbOWl25DjT1mSDhH8oV4UjZLFF2WNe7bj2O/s8mebUFJ1Fn+iGcVtR KO4BtfyehuL9MISo5HKgdnF+aogeR1DszC+87opwg01NTzkAL+pHHI26AKJnQGfKn2u/ SmZg== X-Gm-Message-State: AOJu0YyOikUkF0Ykg3/oIERN8+RXvt7QBF9yl0fXs2K8QFvjY1DsyzjM iAFD4ifa2fxe8AM+4IuZsG8ilIyiHavHVjamYSwKKI6G+Pl+wTuxD9Ydz6FsfKw= X-Google-Smtp-Source: AGHT+IF2nRVvpyMXEbPJKSQolKqo6YC/sGsNRR3PtemnrQHSXb7Izvts4H7oiEK6bWKQLfrB7po8uA== X-Received: by 2002:a05:6808:3990:b0:3bc:7171:b7c7 with SMTP id gq16-20020a056808399000b003bc7171b7c7mr569515oib.67.1706164206921; Wed, 24 Jan 2024 22:30:06 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id t19-20020a056a00139300b006dd870b51b8sm3201139pfg.126.2024.01.24.22.30.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jan 2024 22:30:06 -0800 (PST) From: debug@rivosinc.com To: rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, paul.walmsley@sifive.com, palmer@dabbelt.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com Cc: corbet@lwn.net, aou@eecs.berkeley.edu, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, guoren@kernel.org, samitolvanen@google.com, evan@rivosinc.com, xiao.w.wang@intel.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, waylingii@gmail.com, greentime.hu@sifive.com, heiko@sntech.de, jszhang@kernel.org, shikemeng@huaweicloud.com, david@redhat.com, charlie@rivosinc.com, panqinglin2020@iscas.ac.cn, willy@infradead.org, vincent.chen@sifive.com, andy.chiu@sifive.com, gerg@kernel.org, jeeheng.sia@starfivetech.com, mason.huo@starfivetech.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bhe@redhat.com, chenjiahao16@huawei.com, ruscur@russell.cc, bgray@linux.ibm.com, alx@kernel.org, baruch@tkos.co.il, zhangqing@loongson.cn, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, joey.gouly@arm.com, shr@devkernel.io, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [RFC PATCH v1 19/28] riscv: Implements arch agnostic shadow stack prctls Date: Wed, 24 Jan 2024 22:21:44 -0800 Message-ID: <20240125062739.1339782-20-debug@rivosinc.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240125062739.1339782-1-debug@rivosinc.com> References: <20240125062739.1339782-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: E9C3A8000E X-Stat-Signature: tsupifbxq7etio6kwn5zsefbcje39np6 X-HE-Tag: 1706164207-479938 X-HE-Meta: U2FsdGVkX19So1oP9KTKB+EdOBzewMFUbvaybrUgL7cyxRTp48lsgca59ln9s08EOKUFGs4hkQ9Zd5SXP9lV3xwWCnHt9k2RrMEMKVftvEFHTQKcLk6G5E+ESJAabsw2SatYvGCFggyRuVsnL/oVyKPbGV9eIUJuEILwDRBJSDLOIn+7ktJzKPbH4rTKyKDKJKnTJDsYcpExMfZ4HxpWGqaIQbw4KMFnafzMMCsAM/hCJ+Qfnq6nU1pziIrGorlvNbPw5gU0HP/tRZj2bqTb9V+0RDFP3EDw/OggxtdqOzeFm81euowDfvH7U2ooAOUh6PHviifkDTlp3Ps/CcatDdhl7ZybPFkNw09QXYRbUn0JP8x7ryyksoeuMTtirkPx7BtVo+qSIr5bryW7zMkdr4pdDFJZnz9NUIkZ5bfG9l9WbiMCR9bFsuSqZR+uM+EVPFKnZB3CqsGddQ9/j6BgEnU2/CI3Q66vzYxmnZPoau1Uzpplo9sNL1uZYZmz51RnPK37+nnVA3OJpB4LDAY1ooVpnTsCzXkikSSrKKN6gyfnX6hPy2/ZjPUhrSdh5yVdxCf6Bs8hnFlKL2QrKxBmITaZulFY126pZwbYD8XHPHk69vCxRTZ59Gtbe+zklXIDB7VRqjQbXZK3vU4Btu0vK7qNpazbwX8pPxs6ei9YYok7Sjg+hs2c1y88BG3sFqhwffYdGBne8Fh/WnrbcHZCKheGdcxAP9ZahzIeXdLjLD51zl9SAL3CPYdvtaHsUxMdkUGRT+eidESBJCnmk5cVcCwKoErFRGRxnOWrZp4SJFoyRrJW6wyc80Esv5GwY50B5tsH3WTCSHFfDL39QoNB5BgeSWDtsdrL080HbO7NMKa4buK+nt6xEvk0+vJ0f5Y5WTOfbtmpI188seuWsokC1yf3nP/rkCvv1VclBW4H7N2nJGFIf1M1EcilYsAluixXH05RvgLF6LgbSJh2Woo jKUndHWs KI22ItGQLg8rk5vRFcHVQHoy1Cccu2t8YGwxRL/YEyr0WZzIOSFLCJJ3h5/+eEi3WlhaEJXS6AVbsLhP4qLTzOqY6c/fcRp8C3YlzCfagyzoVmDT9tQws1f+pzb2VxOYcGhUsYCPvt1dF93lWqr9lu1IMIMbh7OyMs15Qp3gc2HOsZOlWAEbC9lI+5r1TqpTjxDj/zSym+21S35g7sIXneH1Nq5HDCPHgMhMQc2z38sS2+CSTIGeuT7NEuz82Zdn8FfpEzIRPPSv62h8X1CNAo0My1nP1MP0ky3whQcPO7RJjhaAAzVE2P9ZjLlk0Eb6JbRf2jI5vivV3opyFo6xDBk7j13Kt16laS3UFA9p8QGa45wtx/sVXYUWL+8Mqi35g/ZcadfY22v/jLYOKQsmQThzl1dvwrVQ2BAYpgJdpR2mmR8sneu6UX9g3N6LWDh5IltJg 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: Deepak Gupta 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 Following is not supported "Enable shadow stack, then disable and enable again." It's not sure whether providing such semantics are useful. It's better to return error code when such situation arises. Signed-off-by: Deepak Gupta --- arch/riscv/include/asm/usercfi.h | 12 +++- arch/riscv/kernel/usercfi.c | 105 +++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+), 1 deletion(-) diff --git a/arch/riscv/include/asm/usercfi.h b/arch/riscv/include/asm/usercfi.h index eb9a0905e72b..72bcfa773752 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,9 @@ 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); + +#define PR_SHADOW_STACK_SUPPORTED_STATUS_MASK (PR_SHADOW_STACK_ENABLE) #else @@ -56,6 +61,11 @@ static inline bool is_shstk_enabled(struct task_struct *task) return false; } +static inline bool is_shstk_locked(struct task_struct *task) +{ + return false; +} + #endif /* CONFIG_RISCV_USER_CFI */ #endif /* __ASSEMBLY__ */ diff --git a/arch/riscv/kernel/usercfi.c b/arch/riscv/kernel/usercfi.c index 36cac0d653f5..be3a071272d8 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,21 @@ 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; +} + +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 @@ -269,3 +294,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 Thu Jan 25 06:21:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13530090 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 097BBC47258 for ; Thu, 25 Jan 2024 06:30:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 48C1F8D000B; Thu, 25 Jan 2024 01:30:18 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3EF718D0001; Thu, 25 Jan 2024 01:30:18 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B417A8D000B; Thu, 25 Jan 2024 01:30:13 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 9ABEA8D0001 for ; Thu, 25 Jan 2024 01:30:13 -0500 (EST) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 64ADDC07D1 for ; Thu, 25 Jan 2024 06:30:13 +0000 (UTC) X-FDA: 81716858706.26.DA91B72 Received: from mail-oi1-f176.google.com (mail-oi1-f176.google.com [209.85.167.176]) by imf09.hostedemail.com (Postfix) with ESMTP id A1AD714001F for ; Thu, 25 Jan 2024 06:30:11 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=O22TfsCk; dmarc=none; spf=pass (imf09.hostedemail.com: domain of debug@rivosinc.com designates 209.85.167.176 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706164211; a=rsa-sha256; cv=none; b=sbUiiOgHXi4NdULF6w07cI7ej9rLFyIVCN32OC4qfN9aeM4PVXszSAVbdXCA+C2GXYaNwe L8gvFFe2vdzpNVViQocFTn/iMr7RnoGkJO/4fh7U7Cx8764i8Iu6tQppP1yMMDvRBuhjHE qIYhdPwXnwNUUvrZaNspahaQtLRkmnE= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=O22TfsCk; dmarc=none; spf=pass (imf09.hostedemail.com: domain of debug@rivosinc.com designates 209.85.167.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=1706164211; 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=aZQZJuD25Dv4YRroK4zFhzWBRez2vp1MS493EB8S7EM=; b=Bbjo9K8DADf51H12UfZv2x/f82CGCHN0QhYzpuzr+Q1S6uPUvv2ocUSffqF6bh/SETyGz5 L/YKML7gIwFftKS2jyxzFKjcS91DQgkmWD2W6cwcmcOnI0dE6HBUeqCzrU5mFFUkyV5d67 2/U2YMzuWV5mV4WlwEQlS3mWckJmxN4= Received: by mail-oi1-f176.google.com with SMTP id 5614622812f47-3bd67e9d6ceso3979892b6e.3 for ; Wed, 24 Jan 2024 22:30:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1706164211; x=1706769011; 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=aZQZJuD25Dv4YRroK4zFhzWBRez2vp1MS493EB8S7EM=; b=O22TfsCkNhwpcwXSSmp8p19JSbHl6fmJ6WVT4xHMXCnwicXY/9tWFE38QjmDunrPWU 349TTIBj5PZI1rM+R6uiG9eEBhvzPy5n9NMVOnBEsr1DQvZ+g/ZvWn6kwiApW7D6N001 1T891c01kF3x6niJATtrYFsoylKquCS1cJeg7itDtlNZWythfEZNgCI31CYmmb4M25gK PedrmlJzsh0E6gDBUGjdU6cLgvj0+xTZ+nXQX9GId11q8W8rD3PLeO0gtmIDYvLzQ3Qj tnWI7Ht61CuOzQUQbxUhI8ttcUs0KsXvvCFUYqPdkD9058Nj1x/xk9Fv8JzFty6Gqpb4 Os0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706164211; x=1706769011; 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=aZQZJuD25Dv4YRroK4zFhzWBRez2vp1MS493EB8S7EM=; b=orGdZX6w7ftmnjkwvt+ToXI2Yo6KgpY9FmW+g1dMTlZflts4zruseqwttAN232cAhb 1LxwVFWjXWx6+y8LymZnWlT+9r4a5qp12wC1amX3MilsLSeVuTbhTitz/z1FmaGH2+Ix ghRb1vch2XTwRBu/UaaUA58XGW+fOQFlKxG2uQn3gADgocRtENESr+7fsOegZWI0Zx6n EhAwOTEhGXhBWhVkwQu7L7btMR2VHi9WlFxjzjV8rRytVicXdBNFU6Wr/0yFyUKhpoWR 0GrMuCnUfKWdn40YRN4J4icP2+ORQdqBUfF/0LLzek0+Q/xLJZKdk3+bAr8gBCR3WGfE Wn6A== X-Gm-Message-State: AOJu0YwoUZDL6CHwGJVHxC+7c4JojIcdjgvMxJcbJFqX8PM8Ccckpz2j 7eVmjKGumgZAzJzaMFi3dplWJoJJDbL9QVOQ+14CuHM499msRcQKi0I4EuzKZJI= X-Google-Smtp-Source: AGHT+IEILiG8nfheUeEY3dgaOhbjObj6SeloQjQPpcALaC4ZkHtiDf7mRG4/M+Lq5WuyhX+48/5Osw== X-Received: by 2002:a05:6808:d51:b0:3bd:bf47:d635 with SMTP id w17-20020a0568080d5100b003bdbf47d635mr590089oik.41.1706164210775; Wed, 24 Jan 2024 22:30:10 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id t19-20020a056a00139300b006dd870b51b8sm3201139pfg.126.2024.01.24.22.30.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jan 2024 22:30:10 -0800 (PST) From: debug@rivosinc.com To: rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, paul.walmsley@sifive.com, palmer@dabbelt.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com Cc: corbet@lwn.net, aou@eecs.berkeley.edu, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, guoren@kernel.org, samitolvanen@google.com, evan@rivosinc.com, xiao.w.wang@intel.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, waylingii@gmail.com, greentime.hu@sifive.com, heiko@sntech.de, jszhang@kernel.org, shikemeng@huaweicloud.com, david@redhat.com, charlie@rivosinc.com, panqinglin2020@iscas.ac.cn, willy@infradead.org, vincent.chen@sifive.com, andy.chiu@sifive.com, gerg@kernel.org, jeeheng.sia@starfivetech.com, mason.huo@starfivetech.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bhe@redhat.com, chenjiahao16@huawei.com, ruscur@russell.cc, bgray@linux.ibm.com, alx@kernel.org, baruch@tkos.co.il, zhangqing@loongson.cn, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, joey.gouly@arm.com, shr@devkernel.io, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [RFC PATCH v1 20/28] riscv: Implements arch argnostic indirect branch tracking prctls Date: Wed, 24 Jan 2024 22:21:45 -0800 Message-ID: <20240125062739.1339782-21-debug@rivosinc.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240125062739.1339782-1-debug@rivosinc.com> References: <20240125062739.1339782-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: A1AD714001F X-Stat-Signature: j3whenc4a5bbfckcz3ikjowwenqe3xan X-HE-Tag: 1706164211-905115 X-HE-Meta: U2FsdGVkX1/qhm1HecVoyhewEqKXcLgYdTT8cDhXpzhUg49Tyf+2YibONLygm273f4YJItBwViXS2nuVxbt+XUUS2YEVcBz0SgmP/eSSRiYMhJMXor7zabQlxiuAO9baAGACPzQNJ3SWxEi6qnG5wz126Fg7avKCg5C3PhgEHhxgH2hXX62cwObAELaxK9OAJNkxSfXlL/eRMsbTuleRlZBpOEaDaki5W2IxI1B3eghuktfhN2+XJN+GRDwjoSqiFFwo7mbC5FfqONBDu6Rfah5q2fBfojwoqZVKNEzw0DICS0t56MCNgPaN238PmUhYwm6x1JuacOOLMlgCSd7etg2njujkr4r/bJXOFeuZ1gvz6RLOpGBjnqTOYfM3ul7mBVS3HmRLifjg3szhmTg4ygqOXtnQzxM39ch8jI2gXimTNXWjVLQppr2DVgD/Cvzf/rpWnfMJMFIS0tIJ5wTxye2LCSKnboSzg8MxdsFJ9rn/Jdu1nMWrNxUf1XFWbKYy5fkN/e+YUG/FM4MQy8cU9cpl43U4leV+UbNhUDD8LhBG7quYGEHaHvnY4nqCv06CpsBBnILfcH/AqIQEVgtfXjIFGaDbJGondu/oisN/ix2Fi759HxH72Wf77F2u+ezg0bPVRtWx4+7uca/LL5ltRZ2rz1/cl9UhV3U8N62DGkn1VyL69PXna80jGpkOVkhgMy9329oXZKSplRL+oaqBHkOA9lRqWvEvEciSqf6MQmVa0guRyFb45qhsp9krCof5rWyvWU/ocyQoWgfVQ9ocxcG9LinMAIYskhWpNL5W7dY8N2SE6AfeT1Ail7vF4zmWoeJQEyh1jdsZg9z6Osjx4BU5GuUKKK2nIi9+0bDW8VPgKIIxnFyCTvTAgpcTh3QZWhB36Y1wT87r97qA77ucKDPci1u94YL/5MW2pA6eB1Tom6CHudDIuMbbRT3eYozFI9/EFDfwWl69yvs4ilf diMS9e5l VzSc6PvNz/vz14W/1HwgTsewdnd3mwihKHBBKn02ova65o6ms+dTLLmS+tW160Occ8qWHsK3fPWyX+12jmA3Vhgxwrb3n+lY4mEkqr4b0tqSbfDtHbVlOGR38ZHvM7ApjwaqHwttlmVZylMD/Xh+tFcV9u6B1N0M1L9pOWgcXstdVki/ozDQhqPRMP/sTifOqSWUsWd79JsFuiP1iMfH8OdHlQ06eFIYi5gUzlV1JE9m6n1MTyUKuYRAAwfwwOa93ZfhVlqAd3EkdZ9aOgSKOMaf6uOONv1GU3ZUH2CJV2X8Ur59sgIy6fkVXMe0H3t4l0kXLxNbjdvAGOrseMf4lFB9mOufzYlUJMMkKO58FNheI2K7TY2kSknfB3KgDIrIGRe4zyVnvM8QtMGgvObFHwdyK0KDGNiTP1wjCYfw7caRgt673w98+H80RSTX6fxnZH+xC 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: Deepak Gupta 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 | 17 +++++++- arch/riscv/kernel/usercfi.c | 74 ++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 1 deletion(-) diff --git a/arch/riscv/include/asm/usercfi.h b/arch/riscv/include/asm/usercfi.h index 72bcfa773752..4bd10dcd48aa 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 */ @@ -29,6 +31,8 @@ void set_shstk_base(struct task_struct *task, unsigned long shstk_addr, unsigned void set_active_shstk(struct task_struct *task, unsigned long shstk_addr); bool is_shstk_enabled(struct task_struct *task); bool is_shstk_locked(struct task_struct *task); +bool is_indir_lp_enabled(struct task_struct *task); +bool is_indir_lp_locked(struct task_struct *task); #define PR_SHADOW_STACK_SUPPORTED_STATUS_MASK (PR_SHADOW_STACK_ENABLE) @@ -66,6 +70,17 @@ static inline bool is_shstk_locked(struct task_struct *task) return false; } +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; +} + #endif /* CONFIG_RISCV_USER_CFI */ #endif /* __ASSEMBLY__ */ diff --git a/arch/riscv/kernel/usercfi.c b/arch/riscv/kernel/usercfi.c index be3a071272d8..af8cc8f4616c 100644 --- a/arch/riscv/kernel/usercfi.c +++ b/arch/riscv/kernel/usercfi.c @@ -67,6 +67,30 @@ 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; +} + +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 @@ -374,3 +398,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 Thu Jan 25 06:21:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13530091 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 321E8C47258 for ; Thu, 25 Jan 2024 06:30:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 756718D0001; Thu, 25 Jan 2024 01:30:18 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6AABB8D000D; Thu, 25 Jan 2024 01:30:18 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 32DE58D000C; Thu, 25 Jan 2024 01:30:18 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 32F8D8D0001 for ; Thu, 25 Jan 2024 01:30:17 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 155FF1C1373 for ; Thu, 25 Jan 2024 06:30:17 +0000 (UTC) X-FDA: 81716858874.28.032D266 Received: from mail-ot1-f41.google.com (mail-ot1-f41.google.com [209.85.210.41]) by imf28.hostedemail.com (Postfix) with ESMTP id 3DF54C000B for ; Thu, 25 Jan 2024 06:30:15 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=WdXJkAIF; spf=pass (imf28.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.41 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706164215; 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=CWoZzchBSoMo3jyN4IG6PBlEmlapmOPwf7uW/AAql2Y=; b=VDz4EFCWGk+KYEOR7Ld9xVYpqvLDiC9c7VnSg5H1gWaHeVmCzQizCiJgVEb+FR80bBbumn I+rSyPJu4CAwUddqbfHZtTiRyhVef1KndLylQiqZ90sHZ/0Bi7RAlMWpCmdwKSjqLwmWvc mFTwF3OL3I+lByyaD/RPcMXiUlxHrGI= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706164215; a=rsa-sha256; cv=none; b=WJbY/qj/hIqwiuX5AYnRtP7pR7gJCldygYN7HF4mih3CpZXOWTJ8h9hsLqlMdYhneeLgoC RHwh9mIMnj5hTagq8QzQdpT1EFWLFZzFrwFLndjKnaNfIGzYOE83s+loCUT7b5qeWLOgSS C+Et8FF5nONeZ9XdOSNWq3pty8Rmu7g= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=WdXJkAIF; spf=pass (imf28.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.41 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-ot1-f41.google.com with SMTP id 46e09a7af769-6e0e08c70f7so3310578a34.2 for ; Wed, 24 Jan 2024 22:30:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1706164214; x=1706769014; 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=CWoZzchBSoMo3jyN4IG6PBlEmlapmOPwf7uW/AAql2Y=; b=WdXJkAIFiBPodivTHifLGWfbZSRfklnWfWlV8HlPCOb6h6arZUebAHcyajXQ6JMFpn d8FMVQDKuTkx2te5HupzPxVefK1k/ZZar2Nt2TmpNEuCi2us1NO8dGgYg1HrxEW03MuF sCnFMm2BNgSm3VIMFj0Q1H6nCy+wuoTHs/eB3N05s3Vx8n2qKrySZbkbVRG5Q9zhVw/F e+/zaF+kjk7/EMJeAzIdud5jHE3CiwEEyhRSdXHlIlR9I4s8XWiFEADZbGMp7YOIC8Gy eW4sF7Hbfi1NGmWBXJguqs/d3GHa0r+fW7IB/F/x72bCd1KxB7wOwAXIcH1nvv7VEoxu CaGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706164214; x=1706769014; 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=CWoZzchBSoMo3jyN4IG6PBlEmlapmOPwf7uW/AAql2Y=; b=Pd5/U7/BTK67RWKq6sg3FMsErZgFHqHCutI0vePJebjZ4AY1NqqTaqTDBX9SMRuHVe Rebbw34Xq4FU6oIxIHEvC/peBBR8sRDYB5PRu5StEiP9310I3CMTRsKO7VttHPPr/jtC kmfb+u5vnG1P7mTt5Mb9Xwf1VnC8dIAYzeyzmNdrWmapd4B4E+J78BZWKG3Fd1uoi9Vc IJftiOWot4fzaCU9jmyQ3sVvBUpoEQ6RwtpxjOHCeoOqWYw1WhriAAnHSOKdIHU6lmdV BNRwqM5eHGzTqgQTq4pMs5cjF6fthv8JtKrN1ygF3Z6uU/WW7sgK+50yN5kZVEivg4JG aRDQ== X-Gm-Message-State: AOJu0YwyvqreTUH9nC2IAbcb8/do1bc+r4Y3V3PIdVUNafs7DCfoNqHh 4UImtmg+phmg8JX8StLHBRe4wTwB4zDtVljyYWfyxxBX/S0YjuG071M/XAp2dvw= X-Google-Smtp-Source: AGHT+IGSbF8wk8MWD9JER43fwMC5oLI81Tos6BLVy/7m1/KYy4B5vfc7HAeOJWEYyCkNhHGvZRDwAg== X-Received: by 2002:a9d:4d9a:0:b0:6dd:bf77:480 with SMTP id u26-20020a9d4d9a000000b006ddbf770480mr380479otk.51.1706164214351; Wed, 24 Jan 2024 22:30:14 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id t19-20020a056a00139300b006dd870b51b8sm3201139pfg.126.2024.01.24.22.30.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jan 2024 22:30:14 -0800 (PST) From: debug@rivosinc.com To: rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, paul.walmsley@sifive.com, palmer@dabbelt.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com Cc: corbet@lwn.net, aou@eecs.berkeley.edu, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, guoren@kernel.org, samitolvanen@google.com, evan@rivosinc.com, xiao.w.wang@intel.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, waylingii@gmail.com, greentime.hu@sifive.com, heiko@sntech.de, jszhang@kernel.org, shikemeng@huaweicloud.com, david@redhat.com, charlie@rivosinc.com, panqinglin2020@iscas.ac.cn, willy@infradead.org, vincent.chen@sifive.com, andy.chiu@sifive.com, gerg@kernel.org, jeeheng.sia@starfivetech.com, mason.huo@starfivetech.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bhe@redhat.com, chenjiahao16@huawei.com, ruscur@russell.cc, bgray@linux.ibm.com, alx@kernel.org, baruch@tkos.co.il, zhangqing@loongson.cn, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, joey.gouly@arm.com, shr@devkernel.io, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [RFC PATCH v1 21/28] riscv/traps: Introduce software check exception Date: Wed, 24 Jan 2024 22:21:46 -0800 Message-ID: <20240125062739.1339782-22-debug@rivosinc.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240125062739.1339782-1-debug@rivosinc.com> References: <20240125062739.1339782-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 3DF54C000B X-Rspam-User: X-Stat-Signature: u6csnt5ucsxifjchubphu93y6k3n5rjt X-Rspamd-Server: rspam03 X-HE-Tag: 1706164215-783552 X-HE-Meta: U2FsdGVkX1+DeF5FeEmw7QD2DmlQOfcyYIPJ7rGCKlrZWl2QkPEZK87aSXkJvbRE/6T0iHJ3zcdD0yIlixqgLYzGdcGSIY8hGYMudC5DXL4GZk5a+KeBrIDM2F+DgRc06Bi2prOrfwyeEG1tuIE0M9Q6kogqyjym6YXRRn7XzxJp7g+/NIZP1IAxPHcAfFVrpt28LnYN4QGQ16CGf6FLrwlky/M7Ntcrz3ouLqROagNQlZeAVu1K2OCs13j2DIxoxFeUq3sz5MYoIIFBLybFDGeuEWmaA1PvIkKoUEpRbhQWL7aBQZerxzXrDK8mtuPWr0D67rBc60PxdOSBt0n2XBqd6pfPHgOi3CwCffaDKHIWLT0KU0vv/XTglZoTbCrwi3LmUim7TwTIZC5ABdjbMd1tsP8oei3QtxPaBnjwPyDzmR/5K5R5HBQzDS04qItBf0ed51NtjbaTG7rarAdTHJVleVOt+T7rl3xnxDHbsWwIZugleacI1ZP+A9JzDmRCmn5NMIzkcGFoQ28XcWrOHhVrRcahpQry7cLbSEHBh6hvRckkxSXfZYv5UmTp1GV4Y3kD9C+64NRxlYWz4KxkJgWIhURhfpXoyMMK6ichT4NXSbZ3ylolAlLnFQsaTEdDENoLh4tyiKehA6O64lx+AMOY7UeGxKkSqbkt8fjuABmqfIrFl51pwHD/28FR3p2wzGf8wod2aevlvkgisjYW5lbszw7c5sE9jT7gG4iHkksA5mlH7rDx7ABjP2fV518V4yLuhcJr/lwjX9Rpyxjplo/JFpKSZSJTxT5expPEMOVrjx0nszK0vGVMeYYyVJCHuza9wbTEqQ3/T8BW4+gX+WJsIcAGT78tZxF7oiAW3wImC3AAcWSGlQMJb6XLaYyjfpb/N7I94prDAuerw51mg3xTuMVc1WSAGx3IUEGJqUsMnIJL+OQWdIyvbUEPWkS439AB7LERyRZd2nPmYVB wl8BWctg diFuSKH2ATKTn8p9SqT/SL2TpDKJSAQKfGUZCMvM11wpvfcP48/8C1vqeKlPNFZ3REz12TK5K9HAOTw6UqbGzINRp/UjE5YUIq9dLJAiupJSKne+j6Zx8alF807wlj4WvKhznekJARRG8xEHSzjJqrtP0RYvUffZHyNubZ1vb5T5RS5sb32Qvn4AgK4nkhE2xIdU2GVOON4guqhcbxR0n+iwqvfGAvOLxj+Mlhfo/NlMZjUwNyYMumpQeYv3pxmqJe3dMzmz1UHhjjAhk+wczsgBK3OxoPLi8KHCV8RH+L7szzlh7Sq5AGpc5i9U2WAVu29Da+jHhfNVg/YXFLvoMQ+2ijse9kdI7RLagDbh5qJIFQlTQrAnDTmnUpy3M0TIsTPY8Z401KZhaAd8oQDT0F6qC0txSiMdQC45BnlPj/6dqrl2/hi815lWNNq+vX/jxiTgk 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: Deepak Gupta 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 36b955c762ba..4ba8aea58dd0 100644 --- a/arch/riscv/include/asm/asm-prototypes.h +++ b/arch/riscv/include/asm/asm-prototypes.h @@ -24,6 +24,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 410659e2eadb..56dfe04094c1 100644 --- a/arch/riscv/kernel/entry.S +++ b/arch/riscv/kernel/entry.S @@ -369,6 +369,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 Thu Jan 25 06:21:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13530092 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 7104AC47DDF for ; Thu, 25 Jan 2024 06:30:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 158508D000D; Thu, 25 Jan 2024 01:30:21 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0BA4C8D000C; Thu, 25 Jan 2024 01:30:21 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DD8D08D000D; Thu, 25 Jan 2024 01:30:20 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id C65858D000C for ; Thu, 25 Jan 2024 01:30:20 -0500 (EST) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 8BAD1C07B3 for ; Thu, 25 Jan 2024 06:30:20 +0000 (UTC) X-FDA: 81716859000.01.B2DD7B5 Received: from mail-oo1-f43.google.com (mail-oo1-f43.google.com [209.85.161.43]) by imf29.hostedemail.com (Postfix) with ESMTP id CBC5A120028 for ; Thu, 25 Jan 2024 06:30:18 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=rERrXiap; spf=pass (imf29.hostedemail.com: domain of debug@rivosinc.com designates 209.85.161.43 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=1706164218; 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=4ROIIq+tfSuVQ8U/gtdFWUWXai61z50x8X9vsARg+kI=; b=49v+5U3D5g/MmvzxLVtLL91aF0iH9IQI3pw1rgI1QcnMfns0jR11CSfapmA3R0iYpR2Zag EHh4/l4N47lQ0amLh5o4fn85B5KVF7f36qNXBeL6cj/MZoHBdsYrMItoQ/kXBIrGrry2Uc lktIngkfzOLJb0q0oRY4jvqmHbVIBqw= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706164218; a=rsa-sha256; cv=none; b=w8dPE4D84VeXekRnpyqUkaYyVRSzkddXs8kNR2Og+Wq0hDgAW7Tr9qyGWRYOfaLbztmBcM c/vxaGWqls536OAnaGrsA1i2wT9oH7qkfBkHU2WIS/nYBGWIoFgkRoLhv9h4wX3surYy/X qWE20eUDcpJMnpcOTSJFy7pgiVY1hhI= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=rERrXiap; spf=pass (imf29.hostedemail.com: domain of debug@rivosinc.com designates 209.85.161.43 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-oo1-f43.google.com with SMTP id 006d021491bc7-595aa5b1fe0so3979209eaf.2 for ; Wed, 24 Jan 2024 22:30:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1706164218; x=1706769018; 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=4ROIIq+tfSuVQ8U/gtdFWUWXai61z50x8X9vsARg+kI=; b=rERrXiaptQCllKPlxmgS0ZqfsOwpFj0aO2C4KQNgbA/FDNlu8w95mjr2I1EwITopSO pJGFHJymrhZXfN6sQ53AyEQwWwyQ8UjPFtMiMBeSp1BrMr4J3FHDfoVgtL+QcS6e6yY7 i/JvkqdEy5LNjzkQhkU4+CoRoUA47JS6xjVOu5aWfC8KoNKzha5cuJjRf3R4I7izdQFH QX0K0zyoUuC2w/XZNm5gl0KAj9615V6NWPIQpOs05y07xT7TaU7+gbExa7uevJSDIgTL gBSGvWPlT1Zt+8w4D67LF5vsAa7R5XKRKSeeeszOWZo4GFKhL+SCE0xwgfQjTKSSg63W xZnA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706164218; x=1706769018; 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=4ROIIq+tfSuVQ8U/gtdFWUWXai61z50x8X9vsARg+kI=; b=kCGUo54AkQrsWoZ8ClJjxVn8FvJcb97ve+TomKODdFFgGVGDaVgfGR2TbhdV1GEAP4 7jpT/L+9oDkOPgxmxneYo+AGJI0fWibsc5k3KZrBN7bl4lQOxXqOLN9MGSB4tL76icw4 qBj52+mqSrsVwE2qx+l6zZTE+RmJoaMCf2+i4Jmtw7msufzCRRA6e+7DLB4vqPoqcIsk uCeyOSj0ZsObNLO3PEG+ra9v5QcSXY2a/rdRGZuqjkgC7n/ffkOWKNdcPfkavgP+4aVS GQEMXBTE0OdZ+AZ3/sCsjZSSRp36bOi+p3c0WY80CL85Epw8HNnnLNO0v+dq7UvfX7h6 vjGw== X-Gm-Message-State: AOJu0YxO7VeyZnKfm4Wpi5+8jZt52zTnbLdDdwAtpPLGqJ4TjE9Y2CCv XuwCeSMUBOku/ImyGy5nWbcCiZGvqcCaN5dfocgarqchdJLgClXLgr0EuB/nMKc= X-Google-Smtp-Source: AGHT+IFCTO1aRpreluHW/C5C9tNmtfREjRA/JGLkNEyuOfgIi16e6JM3wL9ZmcIo8frDDBAlk8/Blw== X-Received: by 2002:a05:6358:94a9:b0:176:1227:8735 with SMTP id i41-20020a05635894a900b0017612278735mr688376rwb.58.1706164217947; Wed, 24 Jan 2024 22:30:17 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id t19-20020a056a00139300b006dd870b51b8sm3201139pfg.126.2024.01.24.22.30.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jan 2024 22:30:17 -0800 (PST) From: debug@rivosinc.com To: rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, paul.walmsley@sifive.com, palmer@dabbelt.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com Cc: corbet@lwn.net, aou@eecs.berkeley.edu, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, guoren@kernel.org, samitolvanen@google.com, evan@rivosinc.com, xiao.w.wang@intel.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, waylingii@gmail.com, greentime.hu@sifive.com, heiko@sntech.de, jszhang@kernel.org, shikemeng@huaweicloud.com, david@redhat.com, charlie@rivosinc.com, panqinglin2020@iscas.ac.cn, willy@infradead.org, vincent.chen@sifive.com, andy.chiu@sifive.com, gerg@kernel.org, jeeheng.sia@starfivetech.com, mason.huo@starfivetech.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bhe@redhat.com, chenjiahao16@huawei.com, ruscur@russell.cc, bgray@linux.ibm.com, alx@kernel.org, baruch@tkos.co.il, zhangqing@loongson.cn, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, joey.gouly@arm.com, shr@devkernel.io, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [RFC PATCH v1 22/28] riscv sigcontext: adding cfi state field in sigcontext Date: Wed, 24 Jan 2024 22:21:47 -0800 Message-ID: <20240125062739.1339782-23-debug@rivosinc.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240125062739.1339782-1-debug@rivosinc.com> References: <20240125062739.1339782-1-debug@rivosinc.com> MIME-Version: 1.0 X-Stat-Signature: pjicmcrdicsebfedbtr93bysbbjhb9ki X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: CBC5A120028 X-Rspam-User: X-HE-Tag: 1706164218-899599 X-HE-Meta: U2FsdGVkX18Cdi4Fs3t7m/OwaRuX/GDS8Epr0t0fa1xWHnYyA09PLurU21D02KA2Vkd3yawW7l6z/ax64zuVvjO4ECt3lLoq0wCBxwCtnxNPkahFGE+p/if3HMoX4D/AZXc6XpS/Cg7NKUSAdkZbA6nhe+3k6PfabtpuBB6rPED7C1hN2kNyPRPowmQvgineShi1KPobdc3Ktx9/dPES0KJzBkdYfQreFsfc4sc1BC/AxeEuxrH+OixddvITzwm0avp/Xdzb3nm5RnhfOO2s42MJKxVWDAdV0yVTjnXcyxcNdwYM2Gq/VjrruC3pFOt0kq1Y4F9K0khl3Je+UCdlm2evNfPbB3IUsxQsOmrIsCadqGF98ZM4VT5j66hmS/tY7VlSt9fKNPTBXdD16Lx0wt5ikSzrAoi81hdPtTxJRnYWqRytr3FcUWZwLwzt/k0ByAJ9CBlAr/9gNhCZzzo+L54c4AbmWe7WLgFvXzFxJaX2p9FYU2AAhs0GUVwbn+cfRzWr15MMetook5KOVzIjhB735Pf2vC7ozONLYkalI/IoZCJufQmOYYSMDnhwir92odLmQxZeb2QUK1kXy2h+Znu9r8aa8Nf9jqmuASZAyFgrDtRqcG/kSdceUUKY+JNnNuJOqyv6LiR7lNbstKItrq1YDe42DS24/WyuKiVDKXZ3K23diBaIpWnSzu6k3irzKiQk0RNBrLwzX2sDhphRMyBpJoNUzBasNPBHL6hVO7od7hzxGI7PMAz5kvsTkBi+1zCwllyNUcojje8ymf9RONp8irgjqWj1xFoIXYBf7Wy2sD1hAzrUnpHlcJSggv3fEhKlHuhIzgy6qZrmn2dHvlN4UcRMFG0eTIGyiOHpqDfCAHzSJcKRhUClbm5JrrKc/ArXO2D2E/4AFKSc1YOLYylARJdi+fAie41ajjJ8EjMnwU7KvOfGcYiC9iNO8OJ2iTRcPF60ChqXABJxtaP p5kwzwYz LS1duOQm5AOyLkw73Y+MKvf8L3qzgaWQrMyYOgwtrgPIVLFbQG3MrAQd8KIX2ZDX7AH/S0dynxMYiTGn2P2fuMHvAaBi9f22n4D7ZH7ITsr9Nvz9URc5By0gdgIj4hrROIYZb/4dzKiI7xVxBjz1b3XE81C6fiUIh+bRLclpS77t/x/tDBBZR0e+WDwmd8J3d9BE4dWcviWkB0RHhWP3mG5qKJIrhIoS4FCIptLlTU9DMtgmkDTC6EeYPGUoOwK/IIY3spnwE7zs9y9N9sSpKWuo6gna1+88XKln8b7Ag3WxwLjF2CuhhvMPWeeTsYkTpm588d6iisL4plT+3Fn1sORMxjC1G3B+OHyuI8PrmIK60UcN7gwwV5ImshV0rA0kkb7xugACleM6GDm18AWx+5M4pyY9WRUH/WCEHyljatH+xCeilqDqVyEX5aPmLKYC9Lu4a X-Bogosity: Ham, tests=bogofilter, spamicity=0.000001, 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: Deepak Gupta 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 Thu Jan 25 06:21:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13530093 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 98970C47DDF for ; Thu, 25 Jan 2024 06:30:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DDBE08D000E; Thu, 25 Jan 2024 01:30:24 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D67B48D000C; Thu, 25 Jan 2024 01:30:24 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B19CC8D000E; Thu, 25 Jan 2024 01:30:24 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 96B3A8D000C for ; Thu, 25 Jan 2024 01:30:24 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 760B0A0791 for ; Thu, 25 Jan 2024 06:30:24 +0000 (UTC) X-FDA: 81716859168.29.300E924 Received: from mail-oi1-f170.google.com (mail-oi1-f170.google.com [209.85.167.170]) by imf30.hostedemail.com (Postfix) with ESMTP id AA43680017 for ; Thu, 25 Jan 2024 06:30:22 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=oNAOmu18; dmarc=none; spf=pass (imf30.hostedemail.com: domain of debug@rivosinc.com designates 209.85.167.170 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706164222; 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=Rd0guRwVXbpygS5QcHTElt8jUaWNDA8n6eGW6ag7sBw=; b=thBpcYdy35cdGEJmoR0Hr013lX2RMGHyJfCh/2zCWK9yAUfJQhKTyOg1uSAM7MCWQ/ikMI 6WlTmkaCRyZM+JxeAQETiQuKg68wqOTgVykjl8V2omTqRWNljUmLlIBvfdDkxnCQK0lGJD 9zlpGudAfhMoHJefZGppueptrOPJMGw= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=oNAOmu18; dmarc=none; spf=pass (imf30.hostedemail.com: domain of debug@rivosinc.com designates 209.85.167.170 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706164222; a=rsa-sha256; cv=none; b=LIWybjx+P+x4lGqkadb1uNDc1JwKVxOoaUQFzWspMXPWYI3KCKNzPQjfo3U2PsbhJ3/ks7 7Pj2DEYXYtrQToh+njGfzkrEU2AnZi/c+TINOvjqGkTBldlGuseG4OyD1rR5f/r3SvUgzY rEoC6vvBDnODo5HHtfpHPuZZIuH6xfE= Received: by mail-oi1-f170.google.com with SMTP id 5614622812f47-3bd67e9d6ceso3979978b6e.3 for ; Wed, 24 Jan 2024 22:30:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1706164222; x=1706769022; 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=Rd0guRwVXbpygS5QcHTElt8jUaWNDA8n6eGW6ag7sBw=; b=oNAOmu18RaD/97YYuZwt9WMENEcSJIX8vPmc17bbRThpD8xRUtF0F/Hc2UbWr9v1dM CDLXnE1uyW28UtP9eglTcGi+pyPKUi3n5apFGnXG9MgZT6M8aC+zCghSmkAbtg4EElol PG9LlyZ0dGfv8LlIRthE3DRTXCgp59nl5WCkTJwZabpQ/28VjQWSIxQ9YE8fKQdhnc7a ALHUDfqq73l/NkOy2BG4ra09Afkt/AjR4aNrbXeHTZ1CNZAAja6ekprD1DkwfQebBumQ 61q8WaqLqiDEu+vS8n6gNVZGeEpSRhBUikLLNhSJnzdU7ApzP2ZPmKn1JxrWjD/i2jek C7Vw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706164222; x=1706769022; 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=Rd0guRwVXbpygS5QcHTElt8jUaWNDA8n6eGW6ag7sBw=; b=hnM6B5r4i6YUQY3zMbuqM5uvbIGH8IEbDe9vS64l753mB3b4+/I5i81kZGecKVrSBE QwVN8lC9osxAxiNLVRnTNSBkUNWP/d8g5YXRnng0KpqlqQGI2zHd777dbpYlzSgpBC36 915J0DjjuM1J/mfW14QvcFFExKJiTbeZ4pETcMNwUlJW+7qon4QJrZ8mkEcHr86vYY0U dQp31v0KVPvbM66VC1OBZvL75n/T/RZBhB4syD/3N7Ge4/hXhhNtN9D2yzqBATt4IWhh 4dCNhL54rZSWskqm2HLasrosqkfkPwnB2r53Splzh+DR300p8l8Tdvh87bYtaQtU8iZ/ GU7w== X-Gm-Message-State: AOJu0YwXLnPKjQs+pgbLqlAYykbciJLxLIXIqvQcx0u+gENIhlUzpywS PRHWcxdHUtOjAR0Olj7xZ+ekAY0XSfkJrFQkxQ6x1wEFCu5FioGkYstYX9kfzxM= X-Google-Smtp-Source: AGHT+IEtSqWNKgoD+4o7h4VeWcDOxTfmpZBfxFewovyKCCIR9bqFDHsIO0YxZXMfxploC95qwEYn2g== X-Received: by 2002:a05:6808:319b:b0:3bd:c4e3:455e with SMTP id cd27-20020a056808319b00b003bdc4e3455emr586022oib.61.1706164221729; Wed, 24 Jan 2024 22:30:21 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id t19-20020a056a00139300b006dd870b51b8sm3201139pfg.126.2024.01.24.22.30.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jan 2024 22:30:21 -0800 (PST) From: debug@rivosinc.com To: rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, paul.walmsley@sifive.com, palmer@dabbelt.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com Cc: corbet@lwn.net, aou@eecs.berkeley.edu, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, guoren@kernel.org, samitolvanen@google.com, evan@rivosinc.com, xiao.w.wang@intel.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, waylingii@gmail.com, greentime.hu@sifive.com, heiko@sntech.de, jszhang@kernel.org, shikemeng@huaweicloud.com, david@redhat.com, charlie@rivosinc.com, panqinglin2020@iscas.ac.cn, willy@infradead.org, vincent.chen@sifive.com, andy.chiu@sifive.com, gerg@kernel.org, jeeheng.sia@starfivetech.com, mason.huo@starfivetech.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bhe@redhat.com, chenjiahao16@huawei.com, ruscur@russell.cc, bgray@linux.ibm.com, alx@kernel.org, baruch@tkos.co.il, zhangqing@loongson.cn, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, joey.gouly@arm.com, shr@devkernel.io, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [RFC PATCH v1 23/28] riscv signal: Save and restore of shadow stack for signal Date: Wed, 24 Jan 2024 22:21:48 -0800 Message-ID: <20240125062739.1339782-24-debug@rivosinc.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240125062739.1339782-1-debug@rivosinc.com> References: <20240125062739.1339782-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: AA43680017 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 79u6gbo54zg3kfu57ei14epfoanexqkd X-HE-Tag: 1706164222-273718 X-HE-Meta: U2FsdGVkX18sBqcksrqA75scoCYxGX+cmL55u/67yuTOxACU4ufA6Dd1rD98J5a92sl/VIEah5gXTVEh7qeTQ4P8oI5N8fIaw0DGHvrvhC6KyIPS+v8RnI+KVZc3Bg6GrN3DQe4XXXCHCoeiPFufigmMIK87d9y2xYZp5Usu3fiyriO0ZG0FAnAz7cdgrLkr3Dd/KYwjjcIO2ihg/6UYmJU7UjThKvNk/wNg6Ei/qoNgf3F1TzhYLmLGVhq/SczGAKNvHClr5ROcVN5XgGP8wO/PBOJCiXSM+/0TrK+u7x0mSkISoz/mL8x2yz6voexl5eprkYqaeVlzjQUUeTypBiGDZcTueM/g+1kGCNXjWM3TkQu/HDLB353yIEdsCycqu7PJ7wr6MGsA3d9JBKYkUlG+NJJHd8+mdxXQ+hh338swBguCfrQULlsoRbl2fScGulOrLToZHryKDMQQs77AB7MY4AH2s69HWdbwy7MfEInUbS137zYmpirIoMejFe3tXcAWXhMgVxeEa335BANmp75PuWjbtGf0qN20H5ebiYYxHJBPIZiXo3Brb3J1t01LSmFg7ylU/jGx0NKIBaM+sZHKX5+VEg07a95MAAqHF95pOATRTBgvj4/g3dQlELIf8kkKRMO/+iBaxVZ0W9LfCOYWNS7VF3bORQwUzyFYjjEUCZdFLWKPAr2mQm3hx+RWEFtPwqn49ugCsTXcy8kOo4zzaHNUJ9x1lrttC7LknhzJ/wo0mNe21nej921fvv5bYd+K2hUGqN3wnLGFNko+xGeFbF+8x0rZbw4aKb1POQXV4NL5FBuINUw6KXqwJlTPgxkcCt5iJvdrYV2BtKsKaYQsfbp5cCUO61G75iz7RCxluYSaOGTYV8OSG09Z/Sc+LmBg+/REFqzvGi04Ge3ScsU6KQ6hXC+o/R+MnICrPS0c5t0tW64mbjKJYiYXKsGxpIcm/T5hBL1xzBd2WMq 7e8o8liT waxJEzb4dGc15ROXpISDDllvsgp9RjoEuAXb6b1U2ef8ptSIpNxyuishia/nJs596Io6pEXIJAvomiQM9VmysduaLQttOAAeoQwwM0A3n36QDDRjI1ahkALAr9896hiNgMpDpsXfsOddjyeIXusiyWt2/v3/cQulWvqJ2y1FGk8PJSiQ5/sIEnSUHOe8pybXOtZ9YRKZwCx2i9ASqvtzarHbn6jlhwOubgvkyqce5p1VpdT17nzRwYyV2vS/zy1iE12IZg3Fi1wRVRVBoGVCAr6c/XxePUfTOeabqkjRgCuxcs9PVRdzPcz1nxS8ANtKa65twC5IxqHGdqXhRlTK5nmnGIbvQThOZjK26O5OB8lRlwtbvDC4vxQ5/R7uGHRhhymoCfWC2RwpCg7g65c9DvHDE1lO/lA99RVDwuAnjmIiMkLqkCs1ePciRXRTzuBPiXbTu 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: Deepak Gupta Save shadow stack pointer in sigcontext structure while delivering signal. Restore shadow stack pointer from sigcontext on sigreturn. Signed-off-by: Deepak Gupta --- arch/riscv/include/asm/usercfi.h | 18 ++++++++++++ arch/riscv/kernel/signal.c | 45 ++++++++++++++++++++++++++++++ arch/riscv/kernel/usercfi.c | 47 ++++++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+) diff --git a/arch/riscv/include/asm/usercfi.h b/arch/riscv/include/asm/usercfi.h index 4bd10dcd48aa..28c67866ff6f 100644 --- a/arch/riscv/include/asm/usercfi.h +++ b/arch/riscv/include/asm/usercfi.h @@ -33,6 +33,9 @@ bool is_shstk_enabled(struct task_struct *task); bool is_shstk_locked(struct task_struct *task); bool is_indir_lp_enabled(struct task_struct *task); bool is_indir_lp_locked(struct task_struct *task); +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) @@ -70,6 +73,16 @@ static inline bool is_shstk_locked(struct task_struct *task) return false; } +int restore_user_shstk(struct task_struct *tsk, unsigned long shstk_ptr) +{ + return -EINVAL; +} + +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; @@ -81,6 +94,11 @@ static inline bool is_indir_lp_locked(struct task_struct *task) return false; } +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 88b6220b2608..d1092f0a6363 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; @@ -229,6 +230,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); @@ -251,6 +253,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; @@ -320,6 +342,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); @@ -331,6 +354,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)); @@ -341,6 +381,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 af8cc8f4616c..f5eb0124571b 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; @@ -165,6 +170,48 @@ 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); + + *saved_shstk_ptr = 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) + 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 Thu Jan 25 06:21: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: 13530094 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 EC3A0C47258 for ; Thu, 25 Jan 2024 06:30:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D8E8B8D000F; Thu, 25 Jan 2024 01:30:28 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D16958D000C; Thu, 25 Jan 2024 01:30:28 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id ACCFA8D000F; Thu, 25 Jan 2024 01:30:28 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 8AD428D000C for ; Thu, 25 Jan 2024 01:30:28 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 5E25B407D7 for ; Thu, 25 Jan 2024 06:30:28 +0000 (UTC) X-FDA: 81716859336.22.09D7C35 Received: from mail-pf1-f177.google.com (mail-pf1-f177.google.com [209.85.210.177]) by imf28.hostedemail.com (Postfix) with ESMTP id 80154C0017 for ; Thu, 25 Jan 2024 06:30:26 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="XtU/p2LP"; dmarc=none; spf=pass (imf28.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.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=1706164226; 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=P6BBG9hVElQCtGiSKaNMQBT766TrVfR9hP9RPboZviE=; b=oU5r370eqfcFXegXFiQ9W9URTKG/T3R/ZiG2df7piunpQrIKC7x1+8sNhE7InQt9ePSnZO 0pvn/UaKFMDp0y5947auSq69cPyf2EGKPP/q0U3VKbjvwRy2SEYITetUP27iT/gp3Oszak vcYr0as94SQxrLQTF3C+Iguuv+WPCRA= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="XtU/p2LP"; dmarc=none; spf=pass (imf28.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.177 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706164226; a=rsa-sha256; cv=none; b=tgGWIl2A4CbgWp99PgUhlbXf7JSG4EP8HEgqW/DQbSsw045LaofvpbbRXBnWbwCI2Wf0Pm lDbVieuE0uPfZ0nlr0Go/yaSf4SChZpmARUbDH/l9mEVaeCuMKiu2YGXAT+b89nqb3tEth ncw+RH0EzKJU9jgxL2KUIqy6zZ7PWNQ= Received: by mail-pf1-f177.google.com with SMTP id d2e1a72fcca58-6db599d5cb8so5184017b3a.0 for ; Wed, 24 Jan 2024 22:30:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1706164225; x=1706769025; 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=P6BBG9hVElQCtGiSKaNMQBT766TrVfR9hP9RPboZviE=; b=XtU/p2LPXe5vaDiMoVXXK3Nj2EgjWF/IQIdDptVp1ll+iQPJKvE1lnchdQmiNenlLT spkqFSbAT11EByKIk3b7tJoHpH8sfOy20wwJcsqIiX8QjZa/vCP6Fv54rSUTT9nbxzz9 91CxzjHWWXa+EdeKpO5VUB/apY4Pb6Sl+Sh/CkH/igy9J1mLAD9Bi0Uy1KSSNbmKhqAO SsXSSyRWP1V+6eRVoRMNyqAjGPHo5UkiL9RCgrzKWRmSDdT76AXz3RG/RjaS7PuF3wje 7dOv/8egO2xh0p8gxoS+lqh2uYHSnDiWms8VSfhpCQvuA+A0dLypx7P6hWGtKNcgnaLw HXkg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706164225; x=1706769025; 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=P6BBG9hVElQCtGiSKaNMQBT766TrVfR9hP9RPboZviE=; b=FbpiBM565glhGb4z9Z95cmQjtXQEVPrQC6W2T4niaZPBfYj5nAZQPy3dxuPMYlug5g q0eqoPv/U5Ia78NBxRTuI0RqpUrcjAispdqnY0bDCJfOqjB4bae0Zc3meII1ciXos2af 84p2k6cq+dGFYW9BZg3PjnIJXKDqEtNE1eugq89Pf0oy25fBnDSAinMslZ+dkBz3Of5K UVQtb/Je5u/9MbgIGbtsAVgOtH5cIDxO0T3zvv7XBsh0cnecgoUhQGvzkkyhe+I8fexS GPeyhq7UmiUyWF2ZEzLndP5+IWexD2Wz3o+I6DQaYzpupq+B+thcWAjwPfwJqRjSMmoQ dMlQ== X-Gm-Message-State: AOJu0Yx5kts4eBn41NZk+cM2MY9QLGsRjnZbFoqTuenXdzvUOHfiCHLs i17KHqgcnjsEe17gIGyfKX19srTzF5lqxJ6nOYIYLd8clhSiEw76IYG0ojPvS5U= X-Google-Smtp-Source: AGHT+IGPdTEWV7DVpyBSBSm0VXEKez0sh3BLdrU06vyzOddQ2bIPsNpxM7VseJi4GIrXrQhafpjvgg== X-Received: by 2002:aa7:88c5:0:b0:6d9:9afd:82f9 with SMTP id k5-20020aa788c5000000b006d99afd82f9mr284323pff.51.1706164225355; Wed, 24 Jan 2024 22:30:25 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id t19-20020a056a00139300b006dd870b51b8sm3201139pfg.126.2024.01.24.22.30.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jan 2024 22:30:25 -0800 (PST) From: debug@rivosinc.com To: rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, paul.walmsley@sifive.com, palmer@dabbelt.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com Cc: corbet@lwn.net, aou@eecs.berkeley.edu, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, guoren@kernel.org, samitolvanen@google.com, evan@rivosinc.com, xiao.w.wang@intel.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, waylingii@gmail.com, greentime.hu@sifive.com, heiko@sntech.de, jszhang@kernel.org, shikemeng@huaweicloud.com, david@redhat.com, charlie@rivosinc.com, panqinglin2020@iscas.ac.cn, willy@infradead.org, vincent.chen@sifive.com, andy.chiu@sifive.com, gerg@kernel.org, jeeheng.sia@starfivetech.com, mason.huo@starfivetech.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bhe@redhat.com, chenjiahao16@huawei.com, ruscur@russell.cc, bgray@linux.ibm.com, alx@kernel.org, baruch@tkos.co.il, zhangqing@loongson.cn, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, joey.gouly@arm.com, shr@devkernel.io, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [RFC PATCH v1 24/28] riscv: select config for shadow stack and landing pad instr support Date: Wed, 24 Jan 2024 22:21:49 -0800 Message-ID: <20240125062739.1339782-25-debug@rivosinc.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240125062739.1339782-1-debug@rivosinc.com> References: <20240125062739.1339782-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 80154C0017 X-Stat-Signature: fo6dryfj93r3a9m13rpgct3xto6gtq1i X-HE-Tag: 1706164226-991366 X-HE-Meta: U2FsdGVkX19s5VDWxdmuLAYxwVky18J46QAr9sEke5oPZ5O3dNFr+hdiDrtIXLHTBt6UrhSdXQwB8qC83PmNHIiwwEiUiMKcVHxendd6YGQns2Esv9wsbbbjy6yLZAqNoXu+4DwW50g5m+fhMcDSKuWqYTPL2dKZVDBtvbSCRwbWpOD5q0vWZ0Lj/chRhoVsPoeY4qE5vED5Eqz0yM8vUDsN7BCVMRq2XGBUfH6otEir1DSWu6aw1ajps50PUu9kGOqYl/DfyETmwBeB3oXuZNZ+qyrGypxqFmfHBRnZ8oEBmlWVPmfEdIzrI2OXblpVaHlGx0PXw5a0SurUjiYUWolBjWR6UlxhoMCIh8bEj0urRPR9kQhGR9QDx+KO16Adscs1dzCJAcOMzLsV0OoiRShEwOu5vlQ0WfF1GAW1mwbD+Gg5yVtFHFyM81PQS7rwblXPz8yDvgSPh+fRq0Eu/ft8cmOFioj8TrLmPzNAK4aVLDplaoGandQUfccf446AXKDO+qzvdQnuM6+zzDPF4ukw8Lp9o4AGpp4oma5Yk7XrcR3mp9LLhgUlARVifnaKPHFrxEVi8NdLdHCuMklH5+tXsBLu8awfCBoaxhPfaubN72qeu1WMNH8cixUbmx63CkpLSo/SO/sosFHOBSM7RcK0WQem0CmgrFn11q2tq44V8lsRR+3bon9VxTkG7TEhipwVCuxeaN+Nsi06Fnn7r9ZTmdZ8tf3Tt+/A7rkkGYcGCpdVRfiDFAWy8j3geoN+mhhNlhQ4W1jYOtGGV+IHW54+CBRe9X0pnf+Ro7aL0m9MQq05uwbV6RBwKiOGGyhgiP6H7kfIGDN58KmihOy53MUQ+hIGdVDSLpR8FkAvvVoYDfRTnpOHOaNCNYwmbyShuK2l9RZaq0tZJXjewn9NxvPTvZy3zpgOccfYftYbCDrRrfoJPaQgjZY3P9PtaMlPv3SrEMmWGHjmmxFUKbz QeyFtDmM deRvkZzhcw5tl4oap7ofdgyhZW5j2yi2ou/xk5F7SxciaX9oRxC450LzJ0+NxXYbEFl6b16ypjL3eECeHZ7dmP1Eh7ijcxHxBhHHYxiAg/PZue+5dbHr9zU9HgOfxkAUm22YXmMfMjJ58P4D9QN6gihhdGfWcSkAbuFTAzAuGOHW1WMrccKBkApajVOYoQ53MD43JCErKxeIHTiwmJHGJexTnSSQq0utyAAqOWIotGplhB2EnDRg0yqWxYC1iLjOw3OOWQTD3lEqPLFSAgIwZ9V9yLXx8DaPzPvS+RyPq6d1S1jyXJ5xJc8z/s2f1HulaLSKUGl6GwVc10PcpnXWaJ1vnRghDVZBB6tnzdAATooZ2aXYHKpbx1FiMGwYAqDMSjgacdZyaFzWCs/VMUHySBHhSrPhQgdSQ5gFEaVgpC/pG/OFIVecTWtWYkTikE8/ostDLQ4+Zw40T6I5SF264/nwmqw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000324, 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: Deepak Gupta This patch selects config shadow stack support and landing pad instr support. Shadow stack support and landing instr support is hidden behind `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 | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index 9d386e9edc45..437b2f9abf3e 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -163,6 +163,7 @@ config RISCV select SYSCTL_EXCEPTION_TRACE select THREAD_INFO_IN_TASK select TRACE_IRQFLAGS_SUPPORT + select RISCV_USER_CFI select UACCESS_MEMCPY if !MMU select ZONE_DMA32 if 64BIT @@ -182,6 +183,20 @@ config HAVE_SHADOW_CALL_STACK # https://github.com/riscv-non-isa/riscv-elf-psabi-doc/commit/a484e843e6eeb51f0cb7b8819e50da6d2444d769 depends on $(ld-option,--no-relax-gp) +config RISCV_USER_CFI + bool "riscv userspace control flow integrity" + 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 Thu Jan 25 06:21: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: 13530095 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 D86BEC47258 for ; Thu, 25 Jan 2024 06:30:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 85E2E8D0010; Thu, 25 Jan 2024 01:30:32 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7E6A68D000C; Thu, 25 Jan 2024 01:30:32 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5EC4A8D0010; Thu, 25 Jan 2024 01:30:32 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 47C4C8D000C for ; Thu, 25 Jan 2024 01:30:32 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 1B4D81407EE for ; Thu, 25 Jan 2024 06:30:32 +0000 (UTC) X-FDA: 81716859504.22.4A0D061 Received: from mail-pg1-f181.google.com (mail-pg1-f181.google.com [209.85.215.181]) by imf27.hostedemail.com (Postfix) with ESMTP id 422F040006 for ; Thu, 25 Jan 2024 06:30:30 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=xQh1CGv2; dmarc=none; spf=pass (imf27.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.181 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706164230; 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=ScAq7kzOpFXx9iuEbGscFFsYaARnx8WzTHX43jvaj1I=; b=v7aO7FK2SrN82jQAvgzrlcz26Y1PjmXpWRioiWKLABqs3C9lo5D/0tBTM0DwE45orUtcG2 C9TdSsOECM3gDURE/oyuJUosai9VGEp9g4ecWTy80yzxxGZgaoSmAXBj9sbLFv43qLiP1G QriLRfhpZsl9xljdKJYMYT6JY2jvGk0= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=xQh1CGv2; dmarc=none; spf=pass (imf27.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.181 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706164230; a=rsa-sha256; cv=none; b=hMD4H96ybCySihZTbRBDAry8VpSIE7k5xlc6mFPSHUTOGnj/s7HZFkf5ABFxcEYehbYbJ5 EDZ7Py14C+Ga5v/TRBEzljlrKgL4wkE0B1q2j9WIcYvpGhCzWyft23zN4tRi+bm0/7KNjY GlQ2Ugfa8qp+1u39PB8XfzcrCHiXXNA= Received: by mail-pg1-f181.google.com with SMTP id 41be03b00d2f7-5ce942efda5so4821923a12.2 for ; Wed, 24 Jan 2024 22:30:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1706164229; x=1706769029; 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=ScAq7kzOpFXx9iuEbGscFFsYaARnx8WzTHX43jvaj1I=; b=xQh1CGv2EOyQOgZ5AQl+610m3Kr2n50SnAIsbpFdPvNbjrhB72l6rN+Loa7YOSEdoC ufX+o429LtGi+wslto7wjvuw73ulNEpdWZbV6U0qeGTWvdIzWpWWXkb5xNDFgFvWH/5X ieZXLqE4bjNKQZOopO3Xz+4uqLIp+dC4ESxKlA5OYQ3jL9PCUrhe4o13G8iWds64/uOE 1chDhabEZzrCr1scJ55GlSZS3SOQDy9KJagMj5zS2k3488VzIM6xogVQe27W91JfHsqr Wuyjn09dhuxSYb/th7XKCqvq/ItsUn0ZlVwthKE0Y59D+Q9yxUfyePvDPKYI7pU7uNuR KCgA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706164229; x=1706769029; 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=ScAq7kzOpFXx9iuEbGscFFsYaARnx8WzTHX43jvaj1I=; b=U+wYIh3VlTJvjs2jgMCbBfNxhPwaFYB8TiMqoXnLsNr6wXxC73enbPALe/sjT1sS3i JGw6/UKm4PF2Zt+fXQOTsFIV0CH+BjSD4JP8T39IDHXcfqrQuij9GR4NnmVUToSPaNK2 C7zC/DkJAi0baUJ0ntd+poDwom9a0ugEQlZfYpCynyitnlE8dDvIQzxrpD1klR69gZM1 Pk4lJJz+JVOvSipHBpt14O6wM5x3zSU1rNhqhbhzPxCLbgVNkT80rsxzjM0T5Di6R/QV tvkbY1GZPoJvcHanm+zOe3No5wbp/kO4U9ufe3YLcI+zv0yI2W8HGw9gTt4l+9ZR8Uw1 3lnA== X-Gm-Message-State: AOJu0YzF+kTUBfuzdpD/MlY4njgve4mVrn4f2gRuncETixfEGJHvxtGX KUuul26x6gz68SxiUczbwpSvP0Jt4Zi+4TfvMfqKp/1HfcMEo9gomM0X7WXw2eA= X-Google-Smtp-Source: AGHT+IGFMu/zk3uZuVL2rgIJ/WYlNuRPVsnV3i1mgW4b48YxV5/AYlDfd6d0RfRSciWq754HxSr5pQ== X-Received: by 2002:a05:6a20:a716:b0:19b:e727:800c with SMTP id by22-20020a056a20a71600b0019be727800cmr543022pzb.109.1706164228936; Wed, 24 Jan 2024 22:30:28 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id t19-20020a056a00139300b006dd870b51b8sm3201139pfg.126.2024.01.24.22.30.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jan 2024 22:30:28 -0800 (PST) From: debug@rivosinc.com To: rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, paul.walmsley@sifive.com, palmer@dabbelt.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com Cc: corbet@lwn.net, aou@eecs.berkeley.edu, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, guoren@kernel.org, samitolvanen@google.com, evan@rivosinc.com, xiao.w.wang@intel.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, waylingii@gmail.com, greentime.hu@sifive.com, heiko@sntech.de, jszhang@kernel.org, shikemeng@huaweicloud.com, david@redhat.com, charlie@rivosinc.com, panqinglin2020@iscas.ac.cn, willy@infradead.org, vincent.chen@sifive.com, andy.chiu@sifive.com, gerg@kernel.org, jeeheng.sia@starfivetech.com, mason.huo@starfivetech.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bhe@redhat.com, chenjiahao16@huawei.com, ruscur@russell.cc, bgray@linux.ibm.com, alx@kernel.org, baruch@tkos.co.il, zhangqing@loongson.cn, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, joey.gouly@arm.com, shr@devkernel.io, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [RFC PATCH v1 25/28] riscv/ptrace: riscv cfi status and state via ptrace and in core files Date: Wed, 24 Jan 2024 22:21:50 -0800 Message-ID: <20240125062739.1339782-26-debug@rivosinc.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240125062739.1339782-1-debug@rivosinc.com> References: <20240125062739.1339782-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 422F040006 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: mm5js9138n5zywpzcdcoderhyk58zf6r X-HE-Tag: 1706164230-960620 X-HE-Meta: U2FsdGVkX18hqIJo52Md69NQaJ+c/DLtairMOZthaWyreN9WIiRejdijch+j7o0yESIWgZUaGZEqNitZuvbUPiSlxThngI/k0LOYMA+wj2j2w0y7oPWyQEQgmbJ1FIYPOhxcUf1uisIz4eKPX23vjSqvjiP/Wr5k3KlM4WAim3Gn/uZdGH/638Uh6nDnyX3I0hgGL/9qEZ2Lsi62t+OTehgiW3YI5U0re860h/X5ml+KGyODPmdVYiZdNgK6aIwsM415zNW9r1sYmih4cfiOlEYx2q3F+h3gQNir2d0JU7FoBnoELJv/MIf0UzHd6szyIkQc49p+2LoqdkxzLx2DSajSqp8f1qy+jnqrE2xi2ayXVzEOVI5MVmYjbYRR2zurGNcPN0sZfqJ4sESir+YgAUGktagS+ovuhrGNrgzODDvn5JoAFlZkizb33KWCUDJISjzXtUZiJThyERDjnJUAsyy6K3psu1vZsFnK1Pk5kg9wujTIhPnl//I3WyIOsh0Q3rTHUgVA8q+hsbEusaPf3V18aGWIgGCQQw4Waq95Mn0nSUi3hLd5w9w5wmepuxeA+1YPwpdHFhaNAVsYCXfZ9IT8nfJvVydf11TVJfLwWESnpggBtaUOGQVMcbqA23R4C1oaN9LloECtYVUmyuS23au2gYF37Dje8Ibfm/6OtNkbJSwGUk0H6E8K4jzgXQGkHSf4aGtmkw2wiaXyKbHT1pKJtw7/y+hDraWqBuuu98Y2PdXkKRwoP1tS7SrAmUHqrH24GascNs/OyhfFQkX+UWlOsouCIksZXzTgVtQ0ADT0+hrHzDvWFnKbloN9V0hADkCeUw7SaI1PMEd4VC9IcVKzXyKp4937NUN1x739RrGuKUbjHWuyMOAg/DzuOzdCjZ+M9q25mFvQKBjyXdc4EbgSdAGQV9tivGQp4iu4pgBfEJJPF7lzpvPVrspe73ZvlBQrmbZDniBEKv77FzZ N2JHJw6p tEpfO1EDgHyXq+vvokcYnrp+VtxEnQ5sTwBcTqRpTOUNS3SWN/ODVP2j5q57g1oLYg9CaWcxdv5t1GtaHZPncj+8hXRa/7CS1DjZvHo86i3oO1A9vmlJna4BIxwS7vG4xD5HIbzU/XRFkYSi/sq80PdL+VGefVzFUXe0QvyNDdlsH0nQkynjMFFaAVtkpScK9HgTssutNKAOSlH+El1EeZzC7jYOLMGQGWxX/D0A/mSSs1WH7Ar2P/LeIpac7uFAzEHMZ8lkYY/Il0EVpc+LCrX7D2osGSM3qjS1Ixl+0bhfeBiuUD2qjEQ1zsmOdyQPmxFIhhlgabt9xF8jcGJXWeaoLguxgXnruiUXR644rRbGo/mOsLa+o9ed1wl0yW3KOnLX1T7PaFjq+l0K331p+mXmq7U5/b5ewiE7M9SAEHrgwjRl8yyQa+ba+w0ghd645B5AX 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: Deepak Gupta 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 2afe460de16a..8ddd529bef0b 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, @@ -149,6 +153,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, @@ -179,6 +252,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 Thu Jan 25 06:21: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: 13530096 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 E22E4C47DDF for ; Thu, 25 Jan 2024 06:30:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 096858D0011; Thu, 25 Jan 2024 01:30:36 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 01F0B8D000C; Thu, 25 Jan 2024 01:30:35 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D8B348D0011; Thu, 25 Jan 2024 01:30:35 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id BAA878D000C for ; Thu, 25 Jan 2024 01:30:35 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 9D25A120C24 for ; Thu, 25 Jan 2024 06:30:35 +0000 (UTC) X-FDA: 81716859630.21.E5F1FA3 Received: from mail-oo1-f53.google.com (mail-oo1-f53.google.com [209.85.161.53]) by imf04.hostedemail.com (Postfix) with ESMTP id 9579F40021 for ; Thu, 25 Jan 2024 06:30:33 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=LycvpwZD; spf=pass (imf04.hostedemail.com: domain of debug@rivosinc.com designates 209.85.161.53 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=1706164233; 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=5TueyV1dJH8ZEnO1NGHEO2HsDxLipOlFRpX7S2COUvw=; b=YeN/foPn81S4gVrNKDAPwRMaYnCvxiNp9dTg0p8PGU2cFYMA+2Kxxy8PBur5A57/74pwcF EgZpHGuVvxdGNSN6Kj6v8mI3kPcJ5SB2Remqe4FyIw5IFTvpe0Mhjms67l+yPg55dYjviv 13nidpAS6lqYu4u7yu2sv4s0Pa+1Vpw= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706164233; a=rsa-sha256; cv=none; b=PHvUSGizbFy8+wrf5NZnvvifZ/5+ijhSwjg3G/vViF3lTMD3xKydHGse0SHcVU0Is71dfP 7zqJIFfgIrcxkYQIT/1PC0Gk490Wy07wQS/YuH/8He7M2bnq1bmg53juQbIG3xMKL+Obi5 IdrPIJJm1cAawUsKvmssCElYY1jCc+4= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=LycvpwZD; spf=pass (imf04.hostedemail.com: domain of debug@rivosinc.com designates 209.85.161.53 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-oo1-f53.google.com with SMTP id 006d021491bc7-5986cb7bb61so3528248eaf.2 for ; Wed, 24 Jan 2024 22:30:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1706164232; x=1706769032; 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=5TueyV1dJH8ZEnO1NGHEO2HsDxLipOlFRpX7S2COUvw=; b=LycvpwZDLyRr5H5tP35W3RicsGQLJx9JKfWDNemUGets5O6ITPuV/0NxOIDg+Z41Rs PM9bNboAhQI+QjRG+ySF0cgiNOriup+ZoeIoPQBXJwbH6KwuQI3OjoNvTlc45FST18La UHGBLVBMxiprtNBlbEnNzlR5SwyiABnNokUI0J1JluURkhqUDKAYMH+8PuP1iKemNikF tdqPo+R6fQIM63GaFIwIBg47ielNevCPrOxN121KbdQJKxXnKDRbA9+x0ZQjUfAFboT/ mB6mhpAjJalD+invYaYmUXJU7hqndRn4IPLmMlCYJ75zX7uIh82vrOdm4YvUdeXc+YNF k0mg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706164232; x=1706769032; 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=5TueyV1dJH8ZEnO1NGHEO2HsDxLipOlFRpX7S2COUvw=; b=J8PU/z6bJ3faqUiJmJRwcsUtf9nnpvk3jWDMKd0XbfmlJZ5fBBg42NdkBogqqU26BJ Bi8l88ptBuPuftTCUJxmpEaae0Yqg0p2YQIC58/ZHBrbq/BQwoAQgug2aDNRargKD2cB jzgAx4APnOk/BMKT1+AU6gm0UErgSy97g0O1K2hBiXCDR1mZpk0Gg3Wg06mrj5NtasWl 28FJuWZKYdBTjvAfkJBPaEAblKqbTrlSOX3ZoAzX3S6n6WvMFyhq5f0zaSN36f5LxYLf tI96Hlrhstt268xzeaUoc50TomLILguWrEi11CWIl3qf6CwmmYBUz3Bb8SzZy1TU0/xf JJJQ== X-Gm-Message-State: AOJu0YwVkI2TaxPqNn62nD3u+sqQBwQKnU52hAphGAsqGYzylX3hIqLK 5IBQVlOUdgNuL7SnHzR3rbKRkOmyEzMMQqqj+Rjd3KsdmZHUE1ghagHMp5bSbNo= X-Google-Smtp-Source: AGHT+IG3ZS3I8RexZrCtHXJL2hpjm1xGympWbdAS6583pRHQgHtWbBjFeX0UfIOo/Pb4bRyFTlW1Gg== X-Received: by 2002:a05:6358:261c:b0:176:847d:6b5e with SMTP id l28-20020a056358261c00b00176847d6b5emr707965rwc.9.1706164232507; Wed, 24 Jan 2024 22:30:32 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id t19-20020a056a00139300b006dd870b51b8sm3201139pfg.126.2024.01.24.22.30.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jan 2024 22:30:32 -0800 (PST) From: debug@rivosinc.com To: rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, paul.walmsley@sifive.com, palmer@dabbelt.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com Cc: corbet@lwn.net, aou@eecs.berkeley.edu, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, guoren@kernel.org, samitolvanen@google.com, evan@rivosinc.com, xiao.w.wang@intel.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, waylingii@gmail.com, greentime.hu@sifive.com, heiko@sntech.de, jszhang@kernel.org, shikemeng@huaweicloud.com, david@redhat.com, charlie@rivosinc.com, panqinglin2020@iscas.ac.cn, willy@infradead.org, vincent.chen@sifive.com, andy.chiu@sifive.com, gerg@kernel.org, jeeheng.sia@starfivetech.com, mason.huo@starfivetech.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bhe@redhat.com, chenjiahao16@huawei.com, ruscur@russell.cc, bgray@linux.ibm.com, alx@kernel.org, baruch@tkos.co.il, zhangqing@loongson.cn, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, joey.gouly@arm.com, shr@devkernel.io, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [RFC PATCH v1 26/28] riscv: Documentation for landing pad / indirect branch tracking Date: Wed, 24 Jan 2024 22:21:51 -0800 Message-ID: <20240125062739.1339782-27-debug@rivosinc.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240125062739.1339782-1-debug@rivosinc.com> References: <20240125062739.1339782-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 9579F40021 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: nqhsfqg8reuybtn7o8yj8e93jiz44j9g X-HE-Tag: 1706164233-256539 X-HE-Meta: U2FsdGVkX1+fudOtnc/kF0l7UiiDy31A7X6gPuBc6d6ztHs0Z9j5NG6HlBuNZbV4n29ALD0aRHImjPCRl1WQRMdA29fLaQtXUvsjsJjnxa/OPbuFe0MvWq2zKUonJp/3KMekP7XEXhx35mjwaUeaHmR0VkfZH0xggqo03u9oRarTMLhmoKn2i7YfMKYp1GN1kRhPqZDnQSHC+NTppDAWTllDHC6VywnDCWsfYMpjjR9h4pVfasY1opcdEnNELuevce+nvhQwWDdfpdJWIORCXfmxjLxjHF0SEu3BewD3Ugo6tsOiUjtPMO88sXUbt8JmL9m7dn74l5FU03bHNpKmvVQV2NuVt9wrnBhqdL0ViEix/O2m9SJ8BEv7spC7XRrWAYpM45XnuybamJr30P7ovxmqq1qKhtSbfAHztlxHmgexUE3i4ju0GjhmPqQXF5NuAXzq6OfUkQPAMnpbBXEt3h5d6U0tGMhsPVJ2cQvg5+/SXIJ24qaGa69etGCoPc/ZYSW1sLk7PBAebpK56nERRCeS3unAt/bgnZpSDTc4fyc3r/1tYtkdHpwMtfqZvX6mXJnIuUFhppDythcNqdEFXfWPmsid6flcFxKLLZ9XewhEmw2Nxeb0I0nXlR0tksVw1nZRCimfFsodF+MmyVs7mqEqnoZkYhyKcmFuD6DAmh/GCO1MUs4W/EESbZTRczvWMqi3OuGe/3Msr3dQKPEO4QTtOA/GDGHKUsRRff7QRKaY70XQSEmrIKndLOCVmtE+/n222KJGhZmA10rX0ZCrLyOfb9susojFaqUQVUFl6u5upDDuvKQbti/JRrU42zIiFPvEo8+mXGSyB2XDxYxV+w9sPSK4dURYb70Po9uiX894pNHReLRAaQczzyR3Xw1GYap/edcCTf5YkPvm9uCmsspwkdW42zeIYWcz3PQooe/LL+CaiJOIjn+Nej+kg4WD0OJimSxHQlLD4GjQIFc /xnVzbeo ERgTejmeklGSxSbB1cyUvfqy+b5B5ibsV7Ev4qfbQaZNpDed3MfHTSwpPc1jxKdgP/G1t7TSfUusaz5N5bIxseVMA56ze+YQoJt07lBwTc6llgdF1FddmWszet76zDqTPKUQVorDxJXt/eZmpwtAht0JwiIr7781om+3URV39SHb+hodpvQzaK1l8wuVuamc3UjN8bIu0bdqHKwbGfmwTQZId5GaJlUmqYtXRrk9vnB639syk8mOqrF6h82zszzTX4VbICtOeIAFOq+JJJufLArr8MkPUf9Vv4XYIm2cMC/Ma2TlzROvFRXhmU3OMAVttXQlfy7YGhnhmtaUXZgCqG/HZyvEKoixDm2XHrwbDRQ2gwURKdupmz9ZY8xpFwTVZCAf/KB7MKVhgQkFn/QhjvuKBDPe+vjulFCYfMldINwhK13+caDVqZO56wSuhKJP5Lq4N 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: Deepak Gupta 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 Thu Jan 25 06:21: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: 13530097 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 F1EB0C47258 for ; Thu, 25 Jan 2024 06:30:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5C6D98D0012; Thu, 25 Jan 2024 01:30:39 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 54E908D000C; Thu, 25 Jan 2024 01:30:39 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 306978D0012; Thu, 25 Jan 2024 01:30:39 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 1C7DB8D000C for ; Thu, 25 Jan 2024 01:30:39 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id ECCA21407EE for ; Thu, 25 Jan 2024 06:30:38 +0000 (UTC) X-FDA: 81716859756.22.CC514A5 Received: from mail-oo1-f52.google.com (mail-oo1-f52.google.com [209.85.161.52]) by imf25.hostedemail.com (Postfix) with ESMTP id 5C231A0013 for ; Thu, 25 Jan 2024 06:30:37 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=xCf1V8hC; spf=pass (imf25.hostedemail.com: domain of debug@rivosinc.com designates 209.85.161.52 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706164237; 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=ZP7pro+U4WPopmqRl450Qrpf0qO4ThfR1Q3D92bPINc=; b=NjEJmnxiAFdQiJdCbaWNaS/8+OEEn3lK5qsiLOduzo417z+NfasNviXnFn1pHbv0b82p8i XHnrijyzH43hVC/IgnoTRJjHaZ7nqJov/GdQS80KuXzOTL5hGGUPdwLvg8meumUvd4tR0O TGstqgJGtqIeV4yL3xbixlswRg0JA0k= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706164237; a=rsa-sha256; cv=none; b=e0Gq2hDJc08XqVujBMwEPeIA5BoIkKdORFrshAxGfy7kXwrdOv4fUKxj1z3NHfLk5vgBun ehbZpN+ooOL+1YLaLYCz6vfwbUnUKk2E9zCnY39fqLzrs87t9tXHOqmCzbN55REYGzrLTb m7bmMPheQM5UNdp6lsbRHeXE6fBnzbk= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=xCf1V8hC; spf=pass (imf25.hostedemail.com: domain of debug@rivosinc.com designates 209.85.161.52 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-oo1-f52.google.com with SMTP id 006d021491bc7-5999f3f6ce9so2177970eaf.0 for ; Wed, 24 Jan 2024 22:30:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1706164236; x=1706769036; 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=ZP7pro+U4WPopmqRl450Qrpf0qO4ThfR1Q3D92bPINc=; b=xCf1V8hC/WowO5In3gSigN56ilgOJHMA/mjQp7F0g+HB7x3U3hEzOyL4qWPTnQZYZ4 f4bmk1NjgswUUEkOmlDqStGnfuvBNMgSOU0uopPetisHidlL4ZSXhQWi+Uy6hBaDVNXG z1CPhAbJdhy2ky4hYltZ3DrdHXak1VaAEkx9ptyLOrGn0HwLFhCuQnMIMC9NxmRJKEaT pQd/MG1+YBtxYlRehtjgypQS00pf5fxvzFonHAr8EQXPCF1XiJ1AX+XKMAtMPMV62Chn GQ0xr3ZjZIX1CrmMEjdd6HKHVPDqzADkTCGbYIVFcQyaHwfNJBxeRovqfUMPLVgKqVzN j1eg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706164236; x=1706769036; 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=ZP7pro+U4WPopmqRl450Qrpf0qO4ThfR1Q3D92bPINc=; b=bMKkzvHWPZb8B7G2oD65+30f2Ni9bdshF2CgIRpy2Jdj+QPl5rsPy7viOHdJOkfyD0 C7aogrmtT349eF086EzGBHreNqAoXMyEo1n2zbBCO3xoAwhYdl9bYL9ZwKQ/tOpZ5s7c UTHybpEFNgRtZh0woELgz2ZT7MGBAAq4CjYztv+97+JW3H2oun8TNn/tbbi1XL98g8sa TJ1yQYnOUHuJEc1/1XZLNYk83KcFt0++aBXV8dBx+nejqENYmIBrd6LOEeBhQvd8kYj0 dyUcizK5kOgmp7MercfmBz+lgcTbfGqA2t/KGllm7/jkQBPvKVFm0sVofuRIXSWEST5p oPOA== X-Gm-Message-State: AOJu0Ywm8UnrUfBe6UKpNb6URZzXWmyOr8g1ga2foH/y5mCMX2J/8aka 3cTCPfY6yO0v5/QoIZ2TpQH6IDhqRFxU8hYhrE9ps7sw+qjsRHsyw7L1GdcMtRU= X-Google-Smtp-Source: AGHT+IEUnFo9OyFSd3Rs68nw+k/GOuuePp10Q4ebNQClafZksd6E7VRe2DbLJ7NX3SXPskd8vBCNUQ== X-Received: by 2002:a05:6358:2910:b0:175:4fc2:1ab4 with SMTP id y16-20020a056358291000b001754fc21ab4mr756379rwb.45.1706164236125; Wed, 24 Jan 2024 22:30:36 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id t19-20020a056a00139300b006dd870b51b8sm3201139pfg.126.2024.01.24.22.30.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jan 2024 22:30:35 -0800 (PST) From: debug@rivosinc.com To: rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, paul.walmsley@sifive.com, palmer@dabbelt.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com Cc: corbet@lwn.net, aou@eecs.berkeley.edu, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, guoren@kernel.org, samitolvanen@google.com, evan@rivosinc.com, xiao.w.wang@intel.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, waylingii@gmail.com, greentime.hu@sifive.com, heiko@sntech.de, jszhang@kernel.org, shikemeng@huaweicloud.com, david@redhat.com, charlie@rivosinc.com, panqinglin2020@iscas.ac.cn, willy@infradead.org, vincent.chen@sifive.com, andy.chiu@sifive.com, gerg@kernel.org, jeeheng.sia@starfivetech.com, mason.huo@starfivetech.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bhe@redhat.com, chenjiahao16@huawei.com, ruscur@russell.cc, bgray@linux.ibm.com, alx@kernel.org, baruch@tkos.co.il, zhangqing@loongson.cn, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, joey.gouly@arm.com, shr@devkernel.io, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [RFC PATCH v1 27/28] riscv: Documentation for shadow stack on riscv Date: Wed, 24 Jan 2024 22:21:52 -0800 Message-ID: <20240125062739.1339782-28-debug@rivosinc.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240125062739.1339782-1-debug@rivosinc.com> References: <20240125062739.1339782-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 5C231A0013 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: caz3otwein9dwk5i9i76gg4kezms8xcm X-HE-Tag: 1706164237-485205 X-HE-Meta: U2FsdGVkX1/q5LG2a4O1ZP504she0URXARgtKx482ooKYUdAZNzIp/nbE8PfjlqXlmho/gqdp//Rmias5syiXDpAdppQ7rJFP045dpqhotJdBnwLNqZVgdR48p4IKtyyGIbSebZkr7DJcBXzuiN0qy8nXY5x0RXJdMQX4clpeH//FYVSs+P4pqlvurmIn8B+3DoGXo7s7NKG9Gia5j69ZcaY7LT9rS1kJ0UxN4+Pdjyj9zsi5dfxjwMuRtyG90JWuR5VfmTTXIQCGcGx91qEmxchOmgjwelMdE3t3M/D+xhb5dg6Lh3915EJx90zLANNt64I3Uzyv7VzWaD3fiiQ7Gsxfb6/jOvHX1D7JKVG9BgJy6wBquBgnk7rc7LAcip7XvvsHGHiXmfAX1PL+OTZhEXtNC5TBxKtkLWnf0lc7smWSXMybQGPhB9r6TlRxpCQtjw+0ExThGnIEKHE62PCmh1lDuGyFWNa0y3qBDscU+VPnL/TS2oUIArzTgIl1FYtGamz1cJZ3Yyli6IFoyfNia/yrYF83kP4pCozf5jRCsC5Q8G2GKHeGNFEbdNCLX682gFSe2BTT4fxnOt1vrBQ7cwUN/BAEcyR0dtS/n89onPmZLg0wAQr8nctPYDIcsY3E2L5jpcdO2REBlS+Lxbgsl1oVL2l2hSyHCAE9r6TqBSaP8FnM+TgL0GvY/3Vw32bbQ2sILT2EVflrChDlpQp3lIs+GGNaDMO3wJsvyo0g5E/EWgGlhiMy+XVRgz7vRYkRLjrEhzYtugzUdJra19AGMcGxCLiF9qlMCEEVl016j+7NbFtim/H5Vl/JwYCq+MfQbXkaurR2eb7T3arl7VW8PRNfLP5SD9Wwe6461RskuNXUbISTHuFDCpo4fpgXtKjVnunfy8GmB5MhMaNN90t94WPJY2Iprs+FupFqDdgP1XkEwO0uXivF+Fuxio8/hLD3Q8ctPG2heY+4kbMfT8 kAz4sk+A w7EvXJyTdol7TTGbKcqeCMC4zbX4NP+49kOq+lwdXLuK+4qfylTpzegKuTZ9v7Iz4Fd+WN2T15hqkNDnPgAcdVLYQgCkhqW9DEE9x2CCg/AXHjkeQCAG5jJR+YLQErX48GZo15Wxxmi0+cqmKulyNBEaxzuIbq7Xfq86D 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: Deepak Gupta 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 Thu Jan 25 06:21: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: 13530098 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 1397DC47DDF for ; Thu, 25 Jan 2024 06:30:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3A3198D0013; Thu, 25 Jan 2024 01:30:43 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 32E588D000C; Thu, 25 Jan 2024 01:30:43 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 097038D0013; Thu, 25 Jan 2024 01:30:42 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id DA0678D000C for ; Thu, 25 Jan 2024 01:30:42 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id ABEA4C07B3 for ; Thu, 25 Jan 2024 06:30:42 +0000 (UTC) X-FDA: 81716859924.20.AC3DE2E Received: from mail-pg1-f169.google.com (mail-pg1-f169.google.com [209.85.215.169]) by imf21.hostedemail.com (Postfix) with ESMTP id F26461C0012 for ; Thu, 25 Jan 2024 06:30:40 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=wkOiCvBB; spf=pass (imf21.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.169 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706164241; 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=T7qF2ux/z3bZUzHUpWVLBPHatGr2T7HdgXLVBLlMyVI=; b=Uc+VMK6/VYSn3KHfpH+g/KB4stOQ7qA3cUhMAkcezjhPM8hL/oo3SWuIwy8gDm69ZRtGHZ HQ97L3hJv3odBnbsHCoefAyTL8UAIPoVz5Z+L2yx/0CQpGY6iurAfnfd2wqM03WxsPclua /HRaFRNAWpvf/0VZRKApme3kZKkbx1o= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=wkOiCvBB; spf=pass (imf21.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.169 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706164241; a=rsa-sha256; cv=none; b=sBKyPwDRB/bMLmRVftKFzN5cWc/QmrSj95/nN71YobJJm3iBcWt3O1KvMsmQ1BBBXduS3s zQ3uaYtg1rI2/kXo0QgxzTcBXiaHcQ402rqB+FXHV9XJ0MWCjq9bAz76sqkWfUPOpnzU9a DmT4+Io4mBak7Bg+/QaEb3WBil1Gm2A= Received: by mail-pg1-f169.google.com with SMTP id 41be03b00d2f7-5ce74ea4bf2so300546a12.0 for ; Wed, 24 Jan 2024 22:30:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1706164240; x=1706769040; 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=T7qF2ux/z3bZUzHUpWVLBPHatGr2T7HdgXLVBLlMyVI=; b=wkOiCvBBbz+OYWgkQm9LrT5H/eaQZ2hEVpFZX3OSe1V7P8EVLILaRjinTTyeoHkxL3 P70j7ui4EhYsZMkY0Z29VSk6IktfHj+lCYcgfQ36ogg/XHpLfqYJz9pnNqr2vzHCGb74 SyWl4JYVzYelAouOA/bo03pfxBkh1e2OSfJY7WQbnrPgG9mcC4olK6a/E/Rp1T7S+5v7 W/ER0Pj1SaJiS9PTxUmvag9Tsx37ecu8Pj2+wC2Ti9zkG4nH2tXDj3Abfu4SJ1iX2Xht mZbQudpFTG+4YdGHGyAjkQ3YJLEwZQK6LjJC6pt89fwZ7JZKq2MNpPY/HweHNHqRKMU+ VMoQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706164240; x=1706769040; 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=T7qF2ux/z3bZUzHUpWVLBPHatGr2T7HdgXLVBLlMyVI=; b=MKaote/d+v5mm/A7kCrOD8CteQMo5dwxa/pEdz05iKAKtoOQ3OCUbL6riTITEOlYup EjJPScO2iM9rnXtkpW/FfFd52SdHPxvQRBv//0rZm+RFj0U8ZU6ksKbazI6JTkYTUjCc AfrWsMMukqI+2wwzFAs5Gm7av7Qd6Fh+8uN2CyJSUnMVzR+3tBlxnJTfY56BfvkuV0jB eWm1/GLQVKYn2NnTyIz6p2MPjj3hsaPL1/rtd5c/3lFz904gXywEE6by5Oj6pFlalOqK pHHC5CGBmwvyxo1m26SknaUBm1V5im6GIpSUOvkgCLx/PiP38HjcTw5qIsOho7wi2EWl 3lmw== X-Gm-Message-State: AOJu0Yyjfh0s3OeZTbsjqA4O5CpJU2RwrHn/CGsUICW5E6B65hPzY/rp d8s94cG1xAtHAlmxSZIPVSAO+ZV5nODwDwSZiFJlujHmGuhkAdxFFDy52MEVRqE= X-Google-Smtp-Source: AGHT+IGtFCuAS2jacdNLOQqSegNmk0e7hQgsOogCowyAm8TQfWIjDD/7nLFP+R2B4EfEEhW2CKrN6Q== X-Received: by 2002:a05:6a20:a929:b0:19b:5b08:1f4b with SMTP id cd41-20020a056a20a92900b0019b5b081f4bmr578106pzb.15.1706164239760; Wed, 24 Jan 2024 22:30:39 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id t19-20020a056a00139300b006dd870b51b8sm3201139pfg.126.2024.01.24.22.30.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jan 2024 22:30:39 -0800 (PST) From: debug@rivosinc.com To: rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, paul.walmsley@sifive.com, palmer@dabbelt.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com Cc: corbet@lwn.net, aou@eecs.berkeley.edu, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, guoren@kernel.org, samitolvanen@google.com, evan@rivosinc.com, xiao.w.wang@intel.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, waylingii@gmail.com, greentime.hu@sifive.com, heiko@sntech.de, jszhang@kernel.org, shikemeng@huaweicloud.com, david@redhat.com, charlie@rivosinc.com, panqinglin2020@iscas.ac.cn, willy@infradead.org, vincent.chen@sifive.com, andy.chiu@sifive.com, gerg@kernel.org, jeeheng.sia@starfivetech.com, mason.huo@starfivetech.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bhe@redhat.com, chenjiahao16@huawei.com, ruscur@russell.cc, bgray@linux.ibm.com, alx@kernel.org, baruch@tkos.co.il, zhangqing@loongson.cn, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, joey.gouly@arm.com, shr@devkernel.io, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [RFC PATCH v1 28/28] kselftest/riscv: kselftest for user mode cfi Date: Wed, 24 Jan 2024 22:21:53 -0800 Message-ID: <20240125062739.1339782-29-debug@rivosinc.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240125062739.1339782-1-debug@rivosinc.com> References: <20240125062739.1339782-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: F26461C0012 X-Rspam-User: X-Stat-Signature: ui7gq9rqpgkkeskto5fy9ia4jr8hnmqx X-Rspamd-Server: rspam01 X-HE-Tag: 1706164240-522756 X-HE-Meta: U2FsdGVkX18HHPhoQlmx2+V4p5FaY4Ab8vaWP858H4GIOmLY/cqrI1o1S+D+E3hzUIa8mV1bs9Yy5IU6y6YxMQ/KL1X6WOL/M4f9P5sWcAQNw8WNi1zJ5puVPkcCVgNYptLBBGe0CBR+BNAFxMr6+tDUKKagyD/F+OV63+OIhWYZnciGukBXf6IjlNwiflexSBQm3JDilL+7haIS63/Ro5P/AoTWrVOztTY7bBJ+3oecE6Lno7xd1Nd88e2v21VhEVmFNOL+WsPGaDnXYdd4jSj/8FJY44cV/3il9bzjZ1abjYiX7wts3gR1vEwZrdXFYyFeNQWF2bqgGoKIr54DRhVyUge06ToeQTegU8N57ykG/nWRHpWSCCYAmXJEAfNtkvzY2Nd5AUXipGrMRS0Aic21GT1Mz2lPlOsHG/Wbvq4LZzC+/msc94BHjvhGA0w1NVCCa5i5PKf6yHB0uMzIIOoJyypN7QgGyl5MbgDUsHT/iU1glU3crp3MXDAb2iDQXaL2Yt1GgLZ90PfRBFYLD7Vp0O8Jglhe/opHV+pchhOqp0LvI/T9qdeOnPVNPSDwc1nJDezODZepZ6QbbTTeN2veUQCe6WGeec8Bf4NKM8bo/5rhq+5NzWHIh/2ATHpF/29P3hw+MkHayPtpWR8S8v9yZejA1HmXPSHvBbRj4b9sqiAlLYc2iZCoZREDB6eAOegQPcm18cAB+dFD0yqv4op4svlwS/M6lKqA4ZMhsLbNl01j6sLchTpgmmJni+vkVtjmwQH0fwNuPqny0iPD6lddquHbl7pJ15cMofjzhgptSsxiZRKhco2h9q9HMOWJULmu1xTCmQQVmtAeotrCHiiJxkPJxxW+aQP9gixPHPEuFAYADTkarspEvvJuGLe6XyaC55L2HNrI3mpFhsf6g3RKzLfhtjcuGQhW92z91u1myet4xGvjhgM+wYVHjehf5eIayRAf3SLyIx2B/3f JeC68/Oc l+tUD7+K3QVJAnM+Cuf9MJQOCYo78r3xOhiCTPMs+riIV49d6dhGzPLaR+cdAJ/FU3EbvB0Z1FUSANGyLDjUJyizSPs8WaNC1C4F0UvUD3HPKTGKPmFSKAPERFdj217tYsFfla2Aa7m7qu6GkOwrpJJ81Y33f9DIcsEMl 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: Deepak Gupta Adds kselftest for RISC-V control flow integrity implementation for user mode. There is not a lot going on in kernel for enabling landing pad for user mode. Thus kselftest simply enables landing pad for the binary and a signal handler is registered for SIGSEGV. Any control flow violation are reported as SIGSEGV with si_code = SEGV_CPERR. Test will fail on recieving any SEGV_CPERR. Shadow stack part has more changes in kernel and thus there are separate tests for that - enable and disable - Exercise `map_shadow_stack` syscall - `fork` test to make sure COW works for shadow stack pages - gup tests As of today kernel uses FOLL_FORCE when access happens to memory via /proc//mem. Not breaking that for shadow stack - signal test. Make sure signal delivery results in token creation on shadow stack and consumes (and verifies) token on sigreturn - shadow stack protection test. attempts to write using regular store instruction on shadow stack memory must result in access faults Signed-off-by: Deepak Gupta --- tools/testing/selftests/riscv/Makefile | 2 +- tools/testing/selftests/riscv/cfi/Makefile | 10 + .../testing/selftests/riscv/cfi/cfi_rv_test.h | 85 ++++ .../selftests/riscv/cfi/riscv_cfi_test.c | 91 +++++ .../testing/selftests/riscv/cfi/shadowstack.c | 376 ++++++++++++++++++ .../testing/selftests/riscv/cfi/shadowstack.h | 39 ++ 6 files changed, 602 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/riscv/cfi/Makefile create mode 100644 tools/testing/selftests/riscv/cfi/cfi_rv_test.h create mode 100644 tools/testing/selftests/riscv/cfi/riscv_cfi_test.c create mode 100644 tools/testing/selftests/riscv/cfi/shadowstack.c create mode 100644 tools/testing/selftests/riscv/cfi/shadowstack.h diff --git a/tools/testing/selftests/riscv/Makefile b/tools/testing/selftests/riscv/Makefile index 4a9ff515a3a0..867e5875b7ce 100644 --- a/tools/testing/selftests/riscv/Makefile +++ b/tools/testing/selftests/riscv/Makefile @@ -5,7 +5,7 @@ ARCH ?= $(shell uname -m 2>/dev/null || echo not) ifneq (,$(filter $(ARCH),riscv)) -RISCV_SUBTARGETS ?= hwprobe vector mm +RISCV_SUBTARGETS ?= hwprobe vector mm cfi else RISCV_SUBTARGETS := endif diff --git a/tools/testing/selftests/riscv/cfi/Makefile b/tools/testing/selftests/riscv/cfi/Makefile new file mode 100644 index 000000000000..77f12157fa29 --- /dev/null +++ b/tools/testing/selftests/riscv/cfi/Makefile @@ -0,0 +1,10 @@ +CFLAGS += -I$(top_srcdir)/tools/include + +CFLAGS += -march=rv64gc_zicfilp_zicfiss + +TEST_GEN_PROGS := cfitests + +include ../../lib.mk + +$(OUTPUT)/cfitests: riscv_cfi_test.c shadowstack.c + $(CC) -static -o$@ $(CFLAGS) $(LDFLAGS) $^ diff --git a/tools/testing/selftests/riscv/cfi/cfi_rv_test.h b/tools/testing/selftests/riscv/cfi/cfi_rv_test.h new file mode 100644 index 000000000000..27267a2e1008 --- /dev/null +++ b/tools/testing/selftests/riscv/cfi/cfi_rv_test.h @@ -0,0 +1,85 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#ifndef SELFTEST_RISCV_CFI_H +#define SELFTEST_RISCV_CFI_H +#include +#include +#include "shadowstack.h" + +#define RISCV_CFI_SELFTEST_COUNT RISCV_SHADOW_STACK_TESTS + +#define CHILD_EXIT_CODE_SSWRITE 10 +#define CHILD_EXIT_CODE_SIG_TEST 11 + +#define BAD_POINTER (NULL) + +#define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ +({ \ + register long _num __asm__ ("a7") = (num); \ + register long _arg1 __asm__ ("a0") = (long)(arg1); \ + register long _arg2 __asm__ ("a1") = (long)(arg2); \ + register long _arg3 __asm__ ("a2") = (long)(arg3); \ + register long _arg4 __asm__ ("a3") = (long)(arg4); \ + register long _arg5 __asm__ ("a4") = (long)(arg5); \ + \ + __asm__ volatile ( \ + "ecall\n" \ + : "+r"(_arg1) \ + : "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \ + "r"(_num) \ + : "memory", "cc" \ + ); \ + _arg1; \ +}) + +#define my_syscall3(num, arg1, arg2, arg3) \ +({ \ + register long _num __asm__ ("a7") = (num); \ + register long _arg1 __asm__ ("a0") = (long)(arg1); \ + register long _arg2 __asm__ ("a1") = (long)(arg2); \ + register long _arg3 __asm__ ("a2") = (long)(arg3); \ + \ + __asm__ volatile ( \ + "ecall\n" \ + : "+r"(_arg1) \ + : "r"(_arg2), "r"(_arg3), \ + "r"(_num) \ + : "memory", "cc" \ + ); \ + _arg1; \ +}) + +#ifndef __NR_prctl +#define __NR_prctl 167 +#endif + +#ifndef __NR_map_shadow_stack +#define __NR_map_shadow_stack 453 +#endif + +#define CSR_SSP 0x011 + +#ifdef __ASSEMBLY__ +#define __ASM_STR(x) x +#else +#define __ASM_STR(x) #x +#endif + +#define csr_read(csr) \ +({ \ + register unsigned long __v; \ + __asm__ __volatile__ ("csrr %0, " __ASM_STR(csr) \ + : "=r" (__v) : \ + : "memory"); \ + __v; \ +}) + +#define csr_write(csr, val) \ +({ \ + unsigned long __v = (unsigned long) (val); \ + __asm__ __volatile__ ("csrw " __ASM_STR(csr) ", %0" \ + : : "rK" (__v) \ + : "memory"); \ +}) + +#endif diff --git a/tools/testing/selftests/riscv/cfi/riscv_cfi_test.c b/tools/testing/selftests/riscv/cfi/riscv_cfi_test.c new file mode 100644 index 000000000000..c116ae4bb358 --- /dev/null +++ b/tools/testing/selftests/riscv/cfi/riscv_cfi_test.c @@ -0,0 +1,91 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include "../../kselftest.h" +#include +#include +#include +#include "cfi_rv_test.h" + +/* do not optimize cfi related test functions */ +#pragma GCC push_options +#pragma GCC optimize("O0") + +#define SEGV_CPERR 10 /* control protection fault */ + +void sigsegv_handler(int signum, siginfo_t *si, void *uc) +{ + struct ucontext *ctx = (struct ucontext *) uc; + + if (si->si_code == SEGV_CPERR) { + printf("Control flow violation happened somewhere\n"); + printf("pc where violation happened %lx\n", ctx->uc_mcontext.gregs[0]); + exit(-1); + } + + /* null pointer deref */ + if (si->si_addr == BAD_POINTER) + exit(CHILD_EXIT_CODE_NULL_PTR_DEREF); + + /* shadow stack write case */ + exit(CHILD_EXIT_CODE_SSWRITE); +} + +int lpad_enable(void) +{ + int ret = 0; + + ret = my_syscall5(__NR_prctl, PR_SET_INDIR_BR_LP_STATUS, PR_INDIR_BR_LP_ENABLE, 0, 0, 0); + + return ret; +} + +bool register_signal_handler(void) +{ + struct sigaction sa = {}; + + sa.sa_sigaction = sigsegv_handler; + sa.sa_flags = SA_SIGINFO; + if (sigaction(SIGSEGV, &sa, NULL)) { + printf("registering signal handler for landing pad violation failed\n"); + return false; + } + + return true; +} + +int main(int argc, char *argv[]) +{ + int ret = 0; + unsigned long lpad_status = 0; + + ksft_print_header(); + + ksft_set_plan(RISCV_CFI_SELFTEST_COUNT); + + ksft_print_msg("starting risc-v tests\n"); + + /* + * Landing pad test. Not a lot of kernel changes to support landing + * pad for user mode except lighting up a bit in senvcfg via a prctl + * Enable landing pad through out the execution of test binary + */ + ret = my_syscall5(__NR_prctl, PR_GET_INDIR_BR_LP_STATUS, &lpad_status, 0, 0, 0); + if (ret) + ksft_exit_skip("Get landing pad status failed with %d\n", ret); + + ret = lpad_enable(); + + if (ret) + ksft_exit_skip("Enabling landing pad failed with %d\n", ret); + + if (!register_signal_handler()) + ksft_exit_skip("registering signal handler for SIGSEGV failed\n"); + + ksft_print_msg("landing pad enabled for binary\n"); + ksft_print_msg("starting risc-v shadow stack tests\n"); + execute_shadow_stack_tests(); + + ksft_finished(); +} + +#pragma GCC pop_options diff --git a/tools/testing/selftests/riscv/cfi/shadowstack.c b/tools/testing/selftests/riscv/cfi/shadowstack.c new file mode 100644 index 000000000000..126654801bed --- /dev/null +++ b/tools/testing/selftests/riscv/cfi/shadowstack.c @@ -0,0 +1,376 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include "../../kselftest.h" +#include +#include +#include +#include +#include +#include "shadowstack.h" +#include "cfi_rv_test.h" + +/* do not optimize shadow stack related test functions */ +#pragma GCC push_options +#pragma GCC optimize("O0") + +void zar(void) +{ + unsigned long ssp = 0, swaped_val = 0; + + ssp = csr_read(CSR_SSP); + printf("inside %s and shadow stack ptr is %lx\n", __func__, ssp); +} + +void bar(void) +{ + printf("inside %s\n", __func__); + zar(); +} + +void foo(void) +{ + printf("inside %s\n", __func__); + bar(); +} + +void zar_child(void) +{ + unsigned long ssp = 0; + + ssp = csr_read(CSR_SSP); + printf("inside %s and shadow stack ptr is %lx\n", __func__, ssp); +} + +void bar_child(void) +{ + printf("inside %s\n", __func__); + zar_child(); +} + +void foo_child(void) +{ + printf("inside %s\n", __func__); + bar_child(); +} + +typedef void (call_func_ptr)(void); +/* + * call couple of functions to test push pop. + */ +int shadow_stack_call_tests(call_func_ptr fn_ptr, bool parent) +{ + if (parent) + printf("call test for parent\n"); + else + printf("call test for child\n"); + + (fn_ptr)(); + + return 0; +} + +bool enable_disable_check(unsigned long test_num, void *ctx) +{ + int ret = 0; + + if (!my_syscall5(__NR_prctl, PR_SET_SHADOW_STACK_STATUS, PR_SHADOW_STACK_ENABLE, 0, 0, 0)) { + printf("Shadow stack was enabled\n"); + shadow_stack_call_tests(&foo, true); + + ret = my_syscall5(__NR_prctl, PR_SET_SHADOW_STACK_STATUS, 0, 0, 0, 0); + if (ret) + ksft_test_result_fail("shadow stack disable failed\n"); + } else { + ksft_test_result_fail("shadow stack enable failed\n"); + ret = -EINVAL; + } + + return ret ? false : true; +} + +/* forks a thread, and ensure shadow stacks fork out */ +bool shadow_stack_fork_test(unsigned long test_num, void *ctx) +{ + int pid = 0, child_status = 0, parent_pid = 0; + + printf("exercising shadow stack fork test\n"); + + if (my_syscall5(__NR_prctl, PR_SET_SHADOW_STACK_STATUS, PR_SHADOW_STACK_ENABLE, 0, 0, 0)) { + printf("shadow stack enable prctl failed\n"); + return false; + } + + parent_pid = getpid(); + pid = fork(); + + if (pid) { + printf("Parent pid %d and child pid %d\n", parent_pid, pid); + shadow_stack_call_tests(&foo, true); + } else + shadow_stack_call_tests(&foo_child, false); + + if (pid) { + printf("waiting on child to finish\n"); + wait(&child_status); + } else { + /* exit child gracefully */ + exit(0); + } + + if (pid && WIFSIGNALED(child_status)) { + printf("child faulted"); + return false; + } + + /* disable shadow stack again */ + if (my_syscall5(__NR_prctl, PR_SET_SHADOW_STACK_STATUS, 0, 0, 0, 0)) { + printf("shadow stack disable prctl failed\n"); + return false; + } + + return true; +} + +/* exercise `map_shadow_stack`, pivot to it and call some functions to ensure it works */ +#define SHADOW_STACK_ALLOC_SIZE 4096 +bool shadow_stack_map_test(unsigned long test_num, void *ctx) +{ + unsigned long shdw_addr; + int ret = 0; + + shdw_addr = my_syscall3(__NR_map_shadow_stack, NULL, SHADOW_STACK_ALLOC_SIZE, 0); + + if (((long) shdw_addr) <= 0) { + printf("map_shadow_stack failed with error code %d\n", (int) shdw_addr); + return false; + } + + ret = munmap((void *) shdw_addr, SHADOW_STACK_ALLOC_SIZE); + + if (ret) { + printf("munmap failed with error code %d\n", ret); + return false; + } + + return true; +} + +/* + * shadow stack protection tests. map a shadow stack and + * validate all memory protections work on it + */ +bool shadow_stack_protection_test(unsigned long test_num, void *ctx) +{ + unsigned long shdw_addr; + unsigned long *write_addr = NULL; + int ret = 0, pid = 0, child_status = 0; + + shdw_addr = my_syscall3(__NR_map_shadow_stack, NULL, SHADOW_STACK_ALLOC_SIZE, 0); + + if (((long) shdw_addr) <= 0) { + printf("map_shadow_stack failed with error code %d\n", (int) shdw_addr); + return false; + } + + write_addr = (unsigned long *) shdw_addr; + pid = fork(); + + /* no child was created, return false */ + if (pid == -1) + return false; + + /* + * try to perform a store from child on shadow stack memory + * it should result in SIGSEGV + */ + if (!pid) { + /* below write must lead to SIGSEGV */ + *write_addr = 0xdeadbeef; + } else { + wait(&child_status); + } + + /* test fail, if 0xdeadbeef present on shadow stack address */ + if (*write_addr == 0xdeadbeef) { + printf("write suceeded\n"); + return false; + } + + /* if child reached here, then fail */ + if (!pid) { + printf("child reached unreachable state\n"); + return false; + } + + /* if child exited via signal handler but not for write on ss */ + if (WIFEXITED(child_status) && + WEXITSTATUS(child_status) != CHILD_EXIT_CODE_SSWRITE) { + printf("child wasn't signaled for write on shadow stack\n"); + return false; + } + + ret = munmap(write_addr, SHADOW_STACK_ALLOC_SIZE); + if (ret) { + printf("munmap failed with error code %d\n", ret); + return false; + } + + return true; +} + +#define SS_MAGIC_WRITE_VAL 0xbeefdead + +int gup_tests(int mem_fd, unsigned long *shdw_addr) +{ + unsigned long val = 0; + + lseek(mem_fd, (unsigned long)shdw_addr, SEEK_SET); + if (read(mem_fd, &val, sizeof(val)) < 0) { + printf("reading shadow stack mem via gup failed\n"); + return 1; + } + + val = SS_MAGIC_WRITE_VAL; + lseek(mem_fd, (unsigned long)shdw_addr, SEEK_SET); + if (write(mem_fd, &val, sizeof(val)) < 0) { + printf("writing shadow stack mem via gup failed\n"); + return 1; + } + + if (*shdw_addr != SS_MAGIC_WRITE_VAL) { + printf("GUP write to shadow stack memory didn't happen\n"); + return 1; + } + + return 0; +} + +bool shadow_stack_gup_tests(unsigned long test_num, void *ctx) +{ + unsigned long shdw_addr = 0; + unsigned long *write_addr = NULL; + int fd = 0; + bool ret = false; + + shdw_addr = my_syscall3(__NR_map_shadow_stack, NULL, SHADOW_STACK_ALLOC_SIZE, 0); + + if (((long) shdw_addr) <= 0) { + printf("map_shadow_stack failed with error code %d\n", (int) shdw_addr); + return false; + } + + write_addr = (unsigned long *) shdw_addr; + + fd = open("/proc/self/mem", O_RDWR); + if (fd == -1) + return false; + + if (gup_tests(fd, write_addr)) { + printf("gup tests failed\n"); + goto out; + } + + ret = true; +out: + if (shdw_addr && munmap(write_addr, SHADOW_STACK_ALLOC_SIZE)) { + printf("munmap failed with error code %d\n", ret); + ret = false; + } + + return ret; +} + +volatile bool break_loop; + +void sigusr1_handler(int signo) +{ + printf("In sigusr1 handler\n"); + break_loop = true; +} + +bool sigusr1_signal_test(void) +{ + if (signal(SIGUSR1, sigusr1_handler) == SIG_ERR) { + printf("registerting sigusr1 handler failed\n"); + return false; + } + + return true; +} +/* + * shadow stack signal test. shadow stack must be enabled. + * register a signal, fork another thread which is waiting + * on signal. Send a signal from parent to child, verify + * that signal was received by child. If not test fails + */ +bool shadow_stack_signal_test(unsigned long test_num, void *ctx) +{ + int pid = 0, child_status = 0; + unsigned long ssp = 0; + + if (my_syscall5(__NR_prctl, PR_SET_SHADOW_STACK_STATUS, PR_SHADOW_STACK_ENABLE, 0, 0, 0)) { + printf("shadow stack enable prctl failed\n"); + return false; + } + + pid = fork(); + + if (pid == -1) { + printf("signal test: fork failed\n"); + goto out; + } + + if (pid == 0) { + /* this should be caught by signal handler and do an exit */ + if (!sigusr1_signal_test()) { + printf("sigusr1_signal_test failed\n"); + exit(-1); + } + + while (!break_loop) + sleep(1); + + exit(11); + /* child shouldn't go beyond here */ + } + /* send SIGUSR1 to child */ + kill(pid, SIGUSR1); + wait(&child_status); + +out: + if (my_syscall5(__NR_prctl, PR_SET_SHADOW_STACK_STATUS, 0, 0, 0, 0)) { + printf("shadow stack disable prctl failed\n"); + return false; + } + + return (WIFEXITED(child_status) && + WEXITSTATUS(child_status) == 11); +} + +int execute_shadow_stack_tests(void) +{ + int ret = 0; + unsigned long test_count = 0; + unsigned long shstk_status = 0; + + printf("Executing RISC-V shadow stack self tests\n"); + + ret = my_syscall5(__NR_prctl, PR_GET_SHADOW_STACK_STATUS, &shstk_status, 0, 0, 0); + + if (ret != 0) + ksft_exit_skip("Get shadow stack status failed with %d\n", ret); + + /* + * If we are here that means get shadow stack status succeeded and + * thus shadow stack support is baked in the kernel. + */ + while (test_count < ARRAY_SIZE(shstk_tests)) { + ksft_test_result((*shstk_tests[test_count].t_func)(test_count, NULL), + shstk_tests[test_count].name); + test_count++; + } + + return 0; +} + +#pragma GCC pop_options diff --git a/tools/testing/selftests/riscv/cfi/shadowstack.h b/tools/testing/selftests/riscv/cfi/shadowstack.h new file mode 100644 index 000000000000..92cb0752238d --- /dev/null +++ b/tools/testing/selftests/riscv/cfi/shadowstack.h @@ -0,0 +1,39 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#ifndef SELFTEST_SHADOWSTACK_TEST_H +#define SELFTEST_SHADOWSTACK_TEST_H +#include +#include + +/* + * a cfi test returns true for success or false for fail + * takes a number for test number to index into array and void pointer. + */ +typedef bool (*shstk_test_func)(unsigned long test_num, void *); + +struct shadow_stack_tests { + char *name; + shstk_test_func t_func; +}; + +bool enable_disable_check(unsigned long test_num, void *ctx); +bool shadow_stack_fork_test(unsigned long test_num, void *ctx); +bool shadow_stack_map_test(unsigned long test_num, void *ctx); +bool shadow_stack_protection_test(unsigned long test_num, void *ctx); +bool shadow_stack_gup_tests(unsigned long test_num, void *ctx); +bool shadow_stack_signal_test(unsigned long test_num, void *ctx); + +static struct shadow_stack_tests shstk_tests[] = { + { "enable disable\n", enable_disable_check }, + { "shstk fork test\n", shadow_stack_fork_test }, + { "map shadow stack syscall\n", shadow_stack_map_test }, + { "shadow stack gup tests\n", shadow_stack_gup_tests }, + { "shadow stack signal tests\n", shadow_stack_signal_test}, + { "memory protections of shadow stack memory\n", shadow_stack_protection_test } +}; + +#define RISCV_SHADOW_STACK_TESTS ARRAY_SIZE(shstk_tests) + +int execute_shadow_stack_tests(void); + +#endif