From mboxrd@z Thu Jan 1 00:00:00 1970 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 1204EC5B543 for ; Wed, 4 Jun 2025 17:17:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 22F3C8D0050; Wed, 4 Jun 2025 13:17:35 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 206708D0007; Wed, 4 Jun 2025 13:17:35 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0F6248D0050; Wed, 4 Jun 2025 13:17:35 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id DBB0F8D0007 for ; Wed, 4 Jun 2025 13:17:34 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 85F65B80C9 for ; Wed, 4 Jun 2025 17:17:34 +0000 (UTC) X-FDA: 83518374828.29.8834EDC Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) by imf12.hostedemail.com (Postfix) with ESMTP id 87D594000B for ; Wed, 4 Jun 2025 17:17:32 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=N9bnfeaH; spf=pass (imf12.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.177 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1749057452; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=04baGaWIOcpAM10BsvDKc2YnzV44Y+9Sv7IQZJnFcq0=; b=QT9VRv38x22Pw3/bFTCMmJGZbAH0GJ+ZE2Hq7dPE6R+Z+jXlHU/iGPDmIqmkvE6nBesTh+ W3nkEQd0p2rjRJ5vx27tnth7tEgWamvUJ7edyNZXjIB32Ti0pQsm8c3zEMEtRVm0MFv/5v AEjEj/YdMSr73Ato33MsJYYVfyzBGKY= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=N9bnfeaH; spf=pass (imf12.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.177 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1749057452; a=rsa-sha256; cv=none; b=7PYOqAHgDq5gK71cYz9Zp1SWO+s5YJ+VJbCfUTU6w4D1GFEE9uDdv7lW1DPsYyBooHVrM4 CJQUwOV+MWW2v+AP8aF59kN+Silej0zUa0ZH3wWhb8G7Wf31NsQVSBcHt1TgEzJdQ03GYS QgWGVgrrk/1jw+cNc01trXkf6snAX8Y= Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-234d366e5f2so915615ad.1 for ; Wed, 04 Jun 2025 10:17:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1749057451; x=1749662251; darn=kvack.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=04baGaWIOcpAM10BsvDKc2YnzV44Y+9Sv7IQZJnFcq0=; b=N9bnfeaHo+u3KeNINecB/Ib0oBJgbqmhf1+828zUc2prU/zOYFKQMmcK+8I04mVKXS LOLwEKHKasKt4By8AqpX/6Lt9S6D1X21TJ3fA3aSrpJ9J1feWC0NEstu1SsYK8BNszbQ 4JpCAm3LV7z3IDwd6XKWVUZ23T8/InY5ovXEtJx0/CwOhl1b8gnz5HR102wOE3D0rLLY Cs9c8OEJS1cRpVURS6q36xeVye1ND3E7sihasffX8Ysa71TJe5zZTzQkWGiHve3cD27k zuDlEz1821pOTfphm/Yg16lUJaTgp9DsegTT2pK1siRufBWwp9iHN/KvBs7CnhUX4SqJ lf3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749057451; x=1749662251; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=04baGaWIOcpAM10BsvDKc2YnzV44Y+9Sv7IQZJnFcq0=; b=Es+HopXjzQ3yYhZTvPfDQ4UV874+OEsQzsisdrtNNeTLir4DxORYeqn830jctc2vfZ WTZmUC+gJLJ6KfQG44vErQs+d/PZPgoXNvHGCMr0NqYquD1FwUSvyDsRxrMP8rdrcC6X 22aFOOOybXZrVMZpzVRhceT6puDGdUd9CO2T9pkFIWZahjgwFR+gQgK+c/ZZVeqqkXHR rlVB5IdHZs5VX7nsAkQyrcfckBerfHUcaauvxEoVNfGQKRMHDF5OHYFoXNPbzSdu9bmV 39YGpEKkQp1svqf3iEMpQpZ400AtfT/8pahZ/8bkas32Ji4GvWLWiUMvT9Fpsc0RzSHM CrWA== X-Forwarded-Encrypted: i=1; AJvYcCXnv/wUZ7nSFr5GCsttMOoClgPcJcVG2npCXahI4PxnBumJIaScoUoTi3w6HBuxdPyMkXl/nHx4yw==@kvack.org X-Gm-Message-State: AOJu0YzG9VFpqkOnua2aPeQG555vTXSb+OOLtDAs/6l5i8dy4ppJ9pWj s5KA5PP2jsxM9aYz7eQhyJCeMcjsFWqCg0RcDdQ0AQ9DKWnHjHSxsXAAIJhEfZeyk1E= X-Gm-Gg: ASbGnct8KsfH1QOLm4rcNlptIqJAwZC1C7OG6MLIK7GScFBgwOhXmqFKbKSSAxT3P0m TmzpWCBAFEaEYC7iUAFBfmNHuTKHnA4vtj/DT3LITTLydVSdnTkeZxpFj5fiaTHsbBcUxHvbPZW FiHaNiZ71p8C+S0RN28XpzllDdqkLDWg65ptW53xNBXSKmZ/bp6GOn4k49cbM8dO0Azn+3ABsZz E62wEgyozCMz49uxTD+sn/5ULhtsjh6cteCxnAvIYtCKluVHGs37Zba3GWayiRz9zV1Px9OcCjL AeSNPz8U5p4wPmkKnioH7H5NungV9I6aL/cZto7c6DVW2juAnegdYv4eBQco/A== X-Google-Smtp-Source: AGHT+IFcq5WB+U9iWrH30KPItPtSFCK4VQFpk5Sy2sACj00+P1FwDLODuLCzKbJ98QmMK99qkmq+JQ== X-Received: by 2002:a17:902:e543:b0:235:1706:1fe7 with SMTP id d9443c01a7336-235e1015d8cmr51502395ad.4.1749057451320; Wed, 04 Jun 2025 10:17:31 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3124e2e9c9fsm9178972a91.30.2025.06.04.10.17.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Jun 2025 10:17:30 -0700 (PDT) From: Deepak Gupta Date: Wed, 04 Jun 2025 10:15:49 -0700 Subject: [PATCH v17 25/27] riscv: Documentation for landing pad / indirect branch tracking MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20250604-v5_user_cfi_series-v17-25-4565c2cf869f@rivosinc.com> References: <20250604-v5_user_cfi_series-v17-0-4565c2cf869f@rivosinc.com> In-Reply-To: <20250604-v5_user_cfi_series-v17-0-4565c2cf869f@rivosinc.com> To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Paul Walmsley , Palmer Dabbelt , Albert Ou , Conor Dooley , Rob Herring , Krzysztof Kozlowski , Arnd Bergmann , Christian Brauner , Peter Zijlstra , Oleg Nesterov , Eric Biederman , Kees Cook , Jonathan Corbet , Shuah Khan , Jann Horn , Conor Dooley , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, alistair.francis@wdc.com, richard.henderson@linaro.org, jim.shu@sifive.com, andybnac@gmail.com, kito.cheng@sifive.com, charlie@rivosinc.com, atishp@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, alexghiti@rivosinc.com, samitolvanen@google.com, broonie@kernel.org, rick.p.edgecombe@intel.com, rust-for-linux@vger.kernel.org, Zong Li , Deepak Gupta X-Mailer: b4 0.13.0 X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 87D594000B X-Stat-Signature: oey4erbcgiyss4gip7y7q3k5spbc1a3m X-Rspam-User: X-HE-Tag: 1749057452-349656 X-HE-Meta: U2FsdGVkX1/NKDrDRzRjHiH6DgQNOsm+DMjRbQw4pzxM1MbDt5gBnadAc0Oa/iUmh7UafQDqYNLAZ222m/e1P9zd2V2WY1vU8ybV1tG6/I+twPop2Cr+UIfYBRjvHQmw+atFK5YfeSIX8x6BrA6MLaURpJW2VcEbPMOuTSKfOm67ZYkofFLdy6PS3jC8IxGbUNTcSw/tnhAhceN2hC6qsGaiXEsqFnAevuctQQMiY7PJaiG9bRsKflw63VZ9VZDbymxCmWB1qNqHp4mat9Sw+5m5MHwDiBOERhCn7011QUq42an0vAlIXbLvdxUrV7sGWGUQSJ/xf59VzXyQhzoSsMy+xi0iCnoto5bnnrRcqc5hfGeiyii9V0VQdkDsTRkQ8/54KXJ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zwLVwqFdC+FptqNgz3y6lH2qyIDYo86HD4cGIBE= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Adding documentation on landing pad aka indirect branch tracking on riscv and kernel interfaces exposed so that user tasks can enable it. Reviewed-by: Zong Li Signed-off-by: Deepak Gupta --- Documentation/arch/riscv/index.rst | 1 + Documentation/arch/riscv/zicfilp.rst | 115 +++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+) diff --git a/Documentation/arch/riscv/index.rst b/Documentation/arch/riscv/index.rst index eecf347ce849..be7237b69682 100644 --- a/Documentation/arch/riscv/index.rst +++ b/Documentation/arch/riscv/index.rst @@ -14,6 +14,7 @@ RISC-V architecture uabi vector cmodx + zicfilp features diff --git a/Documentation/arch/riscv/zicfilp.rst b/Documentation/arch/riscv/zicfilp.rst new file mode 100644 index 000000000000..dcf43de73a06 --- /dev/null +++ b/Documentation/arch/riscv/zicfilp.rst @@ -0,0 +1,115 @@ +.. SPDX-License-Identifier: GPL-2.0 + +:Author: Deepak Gupta +:Date: 12 January 2024 + +==================================================== +Tracking indirect control transfers on RISC-V Linux +==================================================== + +This document briefly describes the interface provided to userspace by Linux +to enable indirect branch tracking for user mode applications on RISC-V + +1. Feature Overview +-------------------- + +Memory corruption issues usually result into crashes, however when in hands of +an adversary and if used creatively can result into a variety security issues. + +One of those security issues can be code re-use attacks on program where adversary +can use corrupt function pointers and chain them together to perform jump oriented +programming (JOP) or call oriented programming (COP) and thus compromising control +flow integrity (CFI) of the program. + +Function pointers live in read-write memory and thus are susceptible to corruption +and allows an adversary to reach any program counter (PC) in address space. On +RISC-V zicfilp extension enforces a restriction on such indirect control +transfers: + +- indirect control transfers must land on a landing pad instruction ``lpad``. + There are two exception to this rule: + + - rs1 = x1 or rs1 = x5, i.e. a return from a function and returns are + protected using shadow stack (see zicfiss.rst) + + - rs1 = x7. On RISC-V compiler usually does below to reach function + which is beyond the offset possible J-type instruction:: + + auipc x7, + jalr (x7) + + Such form of indirect control transfer are still immutable and don't rely + on memory and thus rs1=x7 is exempted from tracking and considered software + guarded jumps. + +``lpad`` instruction is pseudo of ``auipc rd, `` with ``rd=x0`` and +is a HINT nop. ``lpad`` instruction must be aligned on 4 byte boundary and +compares 20 bit immediate with x7. If ``imm_20bit`` == 0, CPU doesn't perform +any comparision with ``x7``. If ``imm_20bit`` != 0, then ``imm_20bit`` must +match ``x7`` else CPU will raise ``software check exception`` (``cause=18``) +with ``*tval = 2``. + +Compiler can generate a hash over function signatures and setup them (truncated +to 20bit) in x7 at callsites and function prologues can have ``lpad`` with same +function hash. This further reduces number of program counters a call site can +reach. + +2. ELF and psABI +----------------- + +Toolchain sets up :c:macro:`GNU_PROPERTY_RISCV_FEATURE_1_FCFI` for property +:c:macro:`GNU_PROPERTY_RISCV_FEATURE_1_AND` in notes section of the object file. + +3. Linux enabling +------------------ + +User space programs can have multiple shared objects loaded in its address space +and it's a difficult task to make sure all the dependencies have been compiled +with support of indirect branch. Thus it's left to dynamic loader to enable +indirect branch tracking for the program. + +4. prctl() enabling +-------------------- + +:c:macro:`PR_SET_INDIR_BR_LP_STATUS` / :c:macro:`PR_GET_INDIR_BR_LP_STATUS` / +:c:macro:`PR_LOCK_INDIR_BR_LP_STATUS` are three prctls added to manage indirect +branch tracking. prctls are arch agnostic and returns -EINVAL on other arches. + +* prctl(PR_SET_INDIR_BR_LP_STATUS, unsigned long arg) + +If arg1 is :c:macro:`PR_INDIR_BR_LP_ENABLE` and if CPU supports ``zicfilp`` +then kernel will enable indirect branch tracking for the task. Dynamic loader +can issue this :c:macro:`prctl` once it has determined that all the objects +loaded in address space support indirect branch tracking. Additionally if there +is a `dlopen` to an object which wasn't compiled with ``zicfilp``, dynamic +loader can issue this prctl with arg1 set to 0 (i.e. +:c:macro:`PR_INDIR_BR_LP_ENABLE` being clear) + +* prctl(PR_GET_INDIR_BR_LP_STATUS, unsigned long arg) + +Returns current status of indirect branch tracking. If enabled it'll return +:c:macro:`PR_INDIR_BR_LP_ENABLE` + +* prctl(PR_LOCK_INDIR_BR_LP_STATUS, unsigned long arg) + +Locks current status of indirect branch tracking on the task. User space may +want to run with strict security posture and wouldn't want loading of objects +without ``zicfilp`` support in it and thus would want to disallow disabling of +indirect branch tracking. In that case user space can use this prctl to lock +current settings. + +5. violations related to indirect branch tracking +-------------------------------------------------- + +Pertaining to indirect branch tracking, CPU raises software check exception in +following conditions: + +- missing ``lpad`` after indirect call / jmp +- ``lpad`` not on 4 byte boundary +- ``imm_20bit`` embedded in ``lpad`` instruction doesn't match with ``x7`` + +In all 3 cases, ``*tval = 2`` is captured and software check exception is +raised (``cause=18``) + +Linux kernel will treat this as :c:macro:`SIGSEV`` with code = +:c:macro:`SEGV_CPERR` and follow normal course of signal delivery. -- 2.43.0