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 BCD7FC369D1 for ; Thu, 24 Apr 2025 07:21:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3522D6B00A4; Thu, 24 Apr 2025 03:21:50 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 301106B00A5; Thu, 24 Apr 2025 03:21:50 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 12DC86B00A6; Thu, 24 Apr 2025 03:21:50 -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 E3DC16B00A4 for ; Thu, 24 Apr 2025 03:21:49 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id B0ACC160874 for ; Thu, 24 Apr 2025 07:21:51 +0000 (UTC) X-FDA: 83368092822.01.A530600 Received: from mail-pl1-f178.google.com (mail-pl1-f178.google.com [209.85.214.178]) by imf12.hostedemail.com (Postfix) with ESMTP id A27D840007 for ; Thu, 24 Apr 2025 07:21:49 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=vaXwkMLL; dmarc=none; spf=pass (imf12.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.178 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1745479309; a=rsa-sha256; cv=none; b=IBu24ABgb14/MO344nhHyzz+cGz3LjQagkeiuEtKzV9nBX9+tgiNVevr2S0BOMmW+qAlvO EuOosvHqdWAHou8twyBJF1JKCk/omLg5cQeg5R9OkUAPKHOV2AXAywqy4MV3vKQPlwNlr1 c5cRJKHdcG3Tn8dRRvyZxsESg8JsVUk= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=vaXwkMLL; dmarc=none; spf=pass (imf12.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.178 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1745479309; 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=OJ+ItfqiER8+6s6TpdQCNRnZQAJK+LDWUGehxiGKKeOqhBMCGG7pR2mJNjv69YEgyjPhA9 FhD9H2n/H/gjU9xx+mLDx5PZFqtrrc98+atbCSuTEUum88YVqceTAh7fIs0P5dBWh5r+80 AzM2lVFG4MqP7t3nK/8kjTQEgHwW0qs= Received: by mail-pl1-f178.google.com with SMTP id d9443c01a7336-225df540edcso19244395ad.0 for ; Thu, 24 Apr 2025 00:21:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1745479308; x=1746084108; 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=vaXwkMLL/qfhKnVObSCCZIhGcUUhsuBX/SvLkc77+7yhqWt9/RByTUqEbnNlsXQpge bItcV0PykIZDTpVaK2bt2g+FDlwI1KXqN8d06EOm3lcyhl3rmLELjqboNqtW6CPGZ6w7 X1IfdgtKPM6YJj+TRFM3wSGEBNVAruEAvJcpuIPKSDyb9jCbZfu2H2rhmFLoKOXUwZQY AJzOf1e7kVRehzdi5roEGrucYk3MbQGwpXHC8CQfLP3gughswwj3fOAHW8E+z/OpX8qO Ejf5sKEy5qS/e3AEa8qxWK2qKlktas3biQOfCUFgo27kMaYCCjHNMaB2QcjKQ1rqW31U R/1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745479308; x=1746084108; 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=ceq+kC4GHcc8dzM1xeyXY0HDBYhrH3mJw2XD3PrcmWwIUwzfUZil0L7qVgfXG6/9uU wYbhvNqJEYY8+SRlv+HX2ZLyL8uIZLcr3frdnjCZdkrvC4JxqtRthDK45fXDuwMol+Xu GuNaVtUsNduNq/K0EiPZrNibXeNGmjlBitRiCeP2TqsGRjPskqGUm1EpLjSQKfbTYAXF EgFy1uLaUwcwWEjdVOTw3QNSHoauLsdl5LwiqTVXgDZQ6cAMakSckvXPuWxr8IKVLCnk NJc8AEm73/HxZtY94FZUs1gk1KBA0U3iTLR9F6Xr9hHvVYaS8BXmYp/MF8YBqPIDYkoe kYkA== X-Forwarded-Encrypted: i=1; AJvYcCUSXUFAnSL+YsxJkKsgCqd8o9z1gi5mdM/cxiTySESIhD73YAi9MbINlPshfpXcxG5VvmPurXES1A==@kvack.org X-Gm-Message-State: AOJu0Yy2Fr3xkHDrCQp8hCHjl0ZI/W26JmyNgCj8r4ZU8IsYmjQjxKoR yNCkMBER+Cqdde2RRA0/unM2ah5FQNX9DoSvbDurIkmcfHO9HXPquDa9wlqPN0I= X-Gm-Gg: ASbGncsHwks5knmtDUpx36EpuyZ16wefFjouEdvfgeVy37LVlDjbjxAXdxnMtvI/dAf b6c+yEMI2yK7vEGIcfdY8OB/Yo4MNDUIWexvmCfC8hBFNsH0FCKkYnHaTQBxk5dRMGNUzg/y8wQ q8sAU44ZBnWkpGTXeccQQzDz6ctHx/dfQSNU+czX1po2jS4pyIdDnGyY2OWmnCcvZfDd/PlnlYo nQN+2HNcUPTmRH9W4FxtqbHRI3iuNs3Hd4vHJUPciXHQNNhmPwaF1PLO1HTebNVh/xlpF8H8MIB OmB5j5EnDwf9kAA+7KJdkjns2IAOw/FP5DQzoFPX9DeXaah9TpE= X-Google-Smtp-Source: AGHT+IFRSogQupOS/1QZfivDKJdkrYWWZNW2ECCAtDP8LuUF+qD+h50jVmSN3NTGLetgwNk+QJCiZQ== X-Received: by 2002:a17:902:d486:b0:21f:6d63:6f4f with SMTP id d9443c01a7336-22db4794b0bmr20237945ad.2.1745479308380; Thu, 24 Apr 2025 00:21:48 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-22db52163d6sm6240765ad.214.2025.04.24.00.21.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Apr 2025 00:21:47 -0700 (PDT) From: Deepak Gupta Date: Thu, 24 Apr 2025 00:20:41 -0700 Subject: [PATCH v13 26/28] 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: <20250424-v5_user_cfi_series-v13-26-971437de586a@rivosinc.com> References: <20250424-v5_user_cfi_series-v13-0-971437de586a@rivosinc.com> In-Reply-To: <20250424-v5_user_cfi_series-v13-0-971437de586a@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-Queue-Id: A27D840007 X-Rspam-User: X-Rspamd-Server: rspam07 X-Stat-Signature: z113t8ukbitnuxj114afoysd3t4q7c5h X-HE-Tag: 1745479309-130699 X-HE-Meta: U2FsdGVkX19ttdg0naS2QTGEO0EqVRQvkqg0IhDQCcDM/AIO/erguSnsbN6BNSzDyA06fKxDUrunCeBYyJJbaN1t5dfVmJz3qqPlPLCjipBjfS2QueVCX1OGIgXylK3lRiHfgP7KG+zY3hXsMRZMFJU3nxEZG9TiUaiuU/b9KBR1UTy7VDwpd4OAgL31dorK4r7oJAJD5FrEGZb4OR5a61wAuNBg/S2dWgNGNpnc39OWzuZGamvfSwuHgOfCf/RiNnYlKlopiAyJijElLBgfecID1Z2kbmoJPlQEkCoJ0HwH5l67/aAih1heYNwiozouryUPhUWq+LqhmqYaYc8AYQog1Qy3xyajeT8bYvE7pSV3AFtUyRPl64tTZvyUyZ7QjRT5WYlNU4DEtjxAUSok3d8ZJyuf2Bp0r+fP/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tyLuuC6UPUM7jsvBrl0ScD/E4aY= 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