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 20F3CC3ABAA for ; Fri, 2 May 2025 23:32:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 588246B00CA; Fri, 2 May 2025 19:31:58 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 513736B00CB; Fri, 2 May 2025 19:31:58 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2C5116B00CC; Fri, 2 May 2025 19:31:58 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 082486B00CA for ; Fri, 2 May 2025 19:31:58 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 058F8160995 for ; Fri, 2 May 2025 23:31:59 +0000 (UTC) X-FDA: 83399567958.29.4B82B3D Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) by imf05.hostedemail.com (Postfix) with ESMTP id 05E6D100006 for ; Fri, 2 May 2025 23:31:56 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=Bv9YXDw8; dmarc=none; spf=pass (imf05.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.182 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1746228717; a=rsa-sha256; cv=none; b=PDw6JDQ/oYu48QsNmSHV0wGY61F+a1cfsCd0I7DJuPkZzcBcpG0nnKRXbpqiVUFfRtX8t0 vRMK83CMcpyUKakgjCT6Pe80EJxKCdL6kJeXq+8ZlxFTTDA4zC6hqRaGr+wLZaKeV7pDA/ 24mp5XetFgFcLW+TJaK3pBtR4Ln8b50= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=Bv9YXDw8; dmarc=none; spf=pass (imf05.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.182 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1746228717; 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=y1QwJwJxj6QhS34qICAx/nIGoRlWM3QDOkZ/1yhVFwr7g/anynwJCJcLk4kLlsV+pTJvHD But7KMqYc9fiBtOqn66222phDiwkomskTBVfS3UT1XtbP0ZQESFrlgAeD4sbU7jUS2TVQ3 CzgzFOozuHqWLfQrzAFeAak4DAdSbLA= Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-224100e9a5cso37006895ad.2 for ; Fri, 02 May 2025 16:31:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1746228716; x=1746833516; 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=Bv9YXDw8wOI3k7tvGN9kR1u03LkliCs8M4AOHUj9SqlB2BC8ebeovBBD0UGaX+ZSVY FFhgDPClqhB70R3I8kPiWvntpVCp8vLZn0lwiOl6P+Ih48Q8Ggmw82tcI8Q3syd1Uz+0 oXGfkNSRLVsNMMKBe6793FVQScpoSrHiwsf5pvlYBiIJw+5n0Dcqk6xk2o8/a2jNqdHK 0Itc7JrUnr+mTNxX3+8wvRBGsxn06NJVK4xVOSC0nSE2v0uLb/RSJypmyZ0o88P167wT 6QOSwt6qJ96QBAWAGp83xC5MJF06Fl5tPg+VSD9fp8AEnW33QKkHjH5DaMZ2fzDufv7i +row== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746228716; x=1746833516; 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=SbbI3P/fRrYys6btGTpYQIQQ0hUblE8q0s+UJCfj7vekB5bp+ENKlOCksdz28gR6em Cmmw5lVJY6aT1ImlFoxURBTDjQdbYSr3rI2n/oosZHxjPOt/ZwzjswsMjNFXynbAoSxD Q8DAFeS7wbcK+7Hv46T0h9AAaZiNGRs9ArcbsOTtcwFrvYkUP+GNmFCGtuzAa8bO2HUT zwT4Fd/rGsZAlkHhqGAx4vDh4yvDx6+TEqNeaibjdf8xc8Xs61idUn8+s8gaiAF5Y9Xh xLvt7BXBonajM7D6POTurTEWvCcmRZv8ZEC4kTRUX6cQ9Znt3/aj5DXSXJ1LyzLRYqir YGAg== X-Forwarded-Encrypted: i=1; AJvYcCXNUeyvTFT40VsrPTaNRyUaoUoyRmm5HzbDFDtu1WJwTVC+8jK2FCQSh2HJsk1JMOHnzsV91ZjaWw==@kvack.org X-Gm-Message-State: AOJu0Yxc3GLgQxf/6FZPjYsyoFq03phUCYNyze6lD+A+j1QGFI3nSd1U CHAosdVhrmgo99uV2BWbnlkNB3mkVVOQDrAkgESMvO9WsZf0xDvzsm34vr16A9Q= X-Gm-Gg: ASbGnctYI69z8tMFoWsqytS6fSxUyWOh9v2zactBlufxE5Z7EFPTt/xTaCEXrKQxmyd 1TOp5dDWQTbSFXlX8XJIklpSUw4KP8nqFaBMrqws9PiSLuYRuy63BKWkLRrEk9M4E11RZSSRQJm 70m9csswhYnuSaRz7pfdzB2CZqN8vuLIKSi+cvrno9NbsxmGtMKUIw+MjP55Acbp3UopUURHloL gx9fpXkcEWi/PBF18+CCDV3zypaq3lIybD88udKJRsFtToDMdDmxaO5kg65iKVW36fgR+IQgDxy LV74uNEczP4qCkXd2PL568CHaFuTA2NXfIkjI2ohJ6HaFLlTXgw= X-Google-Smtp-Source: AGHT+IHr8oUz9O3rn/d8oeYUeyQirDbOHwD4HhUFbL+jkHVjDDS7CnN8McXq9ZP9XAqku0I//ZjxSQ== X-Received: by 2002:a17:903:228d:b0:224:721:ed9 with SMTP id d9443c01a7336-22e103aa1e0mr81027455ad.44.1746228715831; Fri, 02 May 2025 16:31:55 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-22e15228ff2sm13367055ad.180.2025.05.02.16.31.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 May 2025 16:31:55 -0700 (PDT) From: Deepak Gupta Date: Fri, 02 May 2025 16:30:56 -0700 Subject: [PATCH v15 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: <20250502-v5_user_cfi_series-v15-25-914966471885@rivosinc.com> References: <20250502-v5_user_cfi_series-v15-0-914966471885@rivosinc.com> In-Reply-To: <20250502-v5_user_cfi_series-v15-0-914966471885@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-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 05E6D100006 X-Stat-Signature: b5p3uwpgpqozh5pimfnig4oddfzhmgw7 X-HE-Tag: 1746228716-655238 X-HE-Meta: U2FsdGVkX19WL/asslIxS2h4vnKflT50z2kd4mHaDX1Vhr6awIjlbRTWHSXoz92S/IlOfcZ4rW6XUs0EYVLAzqAyfHgNCu8rMC4rsTdJGIgE5S8AdyB7a3m+sSNQ/DHViNlM1A5zX4m0WqVSSiF2u57crqnHUmfP/NTt6mdHkMhAyjx6IjeyCAWyxtnsJbDFfR5zOjYe5BWoQxBFzcsQVUp+7cFbqwRJcOKDZLGz7LvjkkCCf3kN6JutOzHrLATWoPz0sBbhoSIqajyBsHKFEwCZB+Iwubysnnux79QKXUe7kLt3ueBW4CiA8uhH7WZMSFhFWhRRyPmurXDGLNiDgbRy2nj0JM9fqDTTCEYZOjLykI3D+Wt0sD9zVPphQE62xcmHrs+oyszb6b1AK+UN2JptWGHUPBnibw21QMauhT6wqNASs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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