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 2BA2AC54ED0 for ; Fri, 23 May 2025 05:32:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B7F546B00C0; Fri, 23 May 2025 01:32:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B2FF16B00C2; Fri, 23 May 2025 01:32:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9F82E6B00C3; Fri, 23 May 2025 01:32:39 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 802D16B00C0 for ; Fri, 23 May 2025 01:32:39 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 3D0E21D4823 for ; Fri, 23 May 2025 05:32:39 +0000 (UTC) X-FDA: 83473052838.02.7B7BE74 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) by imf25.hostedemail.com (Postfix) with ESMTP id 5E1A9A0007 for ; Fri, 23 May 2025 05:32:37 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=DzDVPFQC; spf=pass (imf25.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=1747978357; 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=T89YLlO9C0BdS5hnA3OS4aaYzP4k8Lm2xA70IGjdrSw+7pl4gHFXZBAMxb9lx8ivn5XH65 x/DsXCWp4K4Mh9VHWIFVYz1OwHXrcI+4mQ1K2Qdf391kF8wNI6y70lPqKNbUksrcGjvMLN DTnVUmiAzBup36KVHVzAZBlROCO4zes= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=DzDVPFQC; spf=pass (imf25.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=1747978357; a=rsa-sha256; cv=none; b=hf3RYc27OPemjXzU2juhVSZu5QvmhZrkkz1g8L6wDRHs3tY8JElwxizxvS1naj5ZVPsB0m wydHXeIn6f+RD54TvDk6+wR6p5V+1T0mnv6msHXmTuehPHf0NFkcobf3mGPo9VhXH7uMB3 545qDKzK6mN7cHWxlzaraFt09QEda6U= Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-742c035f2afso4003050b3a.2 for ; Thu, 22 May 2025 22:32:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1747978356; x=1748583156; 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=DzDVPFQCd4tmoad4lCAuSp6wFtYjwGJxP6FywUGJEo7jZ/TQDadOwHy+3jDBV9r8z6 Y53WZXZ/ZRlVXDIL6mcVNAxUtVWtezTfADSAATa8ws2zwxzSguuEez7CG0viUHEas7oU 5E+1GHS8CEaYDSLasGAXZmXhsRe21hYa/3Kq1WhC0XAbINnILT29Ef6g/QijJP8yAE5Y 3g8Gu/iFKb5HR54gUdYxupR2aJaKnHMAd0/26E0FtGltK3qL4fO8asUvv7kZdT1lWnVg b3SvthOkgn3qw1E+4U6J+UtiALg93/ago96K6KqmbHnCBXzMO3IgiBxlviDT9afSV9QC xylw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747978356; x=1748583156; 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=gnXC6qjhG7TH0FKwaeN3206o6f6r5seAQAH7krW3RYjujaHK/Z0V2oGddUB7PY/7i4 SKE8cAJa1m+jXxcXJLbtZiFiD66k1xhM1Ot19mkkpnlxgyrNoSedl0xg8bOrdAmdkyMk Z5jya7/g3ae5LQHSsSA5vNCxaMvp7nysNVbcDN4GIDWi7+St/VUvTPMqR0HSXeM0bZCM ViPyck24R4gZChyIDoTbQnGZuDfzpuWQnPj6g/Q1ACzbmRk+mOw2P1Ic7v5jD9dV+p/q VnctKtPb3r67xKXBCb03fE4MtXvm7uU44VjXp+N7s9fw7/TsKpPEpuudTZHOYUHMhOta /bnA== X-Forwarded-Encrypted: i=1; AJvYcCWE1eahISlTJc05K7ikdQqhLf1v30+eJ9RcnaQUW/ycQPDw3I48vMqLpJN3CQUp0ymaSXoxuaggPw==@kvack.org X-Gm-Message-State: AOJu0Yzxx16hgmXSYd//91tAFWTDTeByXv5RqyUD28UCgHIKiIRdKPxu CFHNaAEQEOmB+YpqdbPJ0azCee0lZWokXXlG0l/pSTHmp6pglQWPkL3xKoFGvYrSOcQ= X-Gm-Gg: ASbGncuC4kJiI/g2aZ62Fq+Dwpt5fKaDy9OijFUO09TRoI1Ww1uTpVRf4XoW5SmvK6j o7fB0gkY0iKRX47HnuWA3hQoEseBUVliDedbTl42pTihcCzNNzl2TUcikcCsBBQTn/es4tH6Mh/ paDfxsCHaAitjHWMrAZmabINM6R7sm1ifwRRlnxE9lhB04onBMOaGYh6pC76+/jqONVfeIi7gcc 8vyEaeu/7aTceVUJLKkA6RGrj3Ydv1u9qXsORZcqGxQewOgTexFAidhDvI1Y0FKdP/eC6R+qvrV k/s9cP3/zPsFoLyOJfdYQ/n+5Ty/rGV0bI5uNcMOm0G/Et/2NlBemGMvUiS9Tg== X-Google-Smtp-Source: AGHT+IGY2mml7AXscMSxTVcExivaWeJQg450Faf3IiaTcz1vQGyBogO5Kxf2SHgDF5QZvOcb8ujfTA== X-Received: by 2002:a05:6a00:982:b0:736:a7ec:a366 with SMTP id d2e1a72fcca58-742a97a2bcamr35352139b3a.9.1747978356133; Thu, 22 May 2025 22:32:36 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-742a982a0a4sm12474336b3a.101.2025.05.22.22.32.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 May 2025 22:32:35 -0700 (PDT) From: Deepak Gupta Date: Thu, 22 May 2025 22:31:28 -0700 Subject: [PATCH v16 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: <20250522-v5_user_cfi_series-v16-25-64f61a35eee7@rivosinc.com> References: <20250522-v5_user_cfi_series-v16-0-64f61a35eee7@rivosinc.com> In-Reply-To: <20250522-v5_user_cfi_series-v16-0-64f61a35eee7@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-Stat-Signature: gfauqgdnc711z1sgc6c8nr1um1ecmjfa X-Rspamd-Queue-Id: 5E1A9A0007 X-Rspamd-Server: rspam11 X-HE-Tag: 1747978357-71374 X-HE-Meta: U2FsdGVkX19vs3uCISD8yysZqaGfhXDSsiCp1jLBVYzLDlqPnR99AYK7nrEKtCWIfDiHkkndulm1wBt7XdlLgMxPQUrpm3q+xpSOF+pAkBAPJ8DIeSSDoLjkXZFVOBRhHdVgJl0/bEkEAHR5TCAsZtqsPQjN/uKcDqGY+cNFPZqVvPu8CBCES5sSWNHQCjfwu1Amdvm4z56By3w5qkIL3PEaEK8oYvLvP+GTJbzgqeD+DtsvYlMiF01eGy0RbJs3pYSfh+OHdUw3ayRfed3VWYKZjutD4KrIXWhi7ASyM2xOq0pIAtUd3E8yrOkiYtLoykftpGXfqf9wEGhpX26xUMqoUr6vykG1IE/jLeUEogey6sV8DAzPdqrCwkzKw27fVS65bQsLn3UcDmQcLKt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daQyE4yxu4g= 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