Debian Codegraph @

Documentation/arch/riscv/zicfilp.rst

raw
1 .. SPDX-License-Identifier: GPL-2.0
2
3 :Author: Deepak Gupta <[email protected]>
4 :Date: 12 January 2024
5
6 ====================================================
7 Tracking indirect control transfers on RISC-V Linux
8 ====================================================
9
10 This document briefly describes the interface provided to userspace by Linux
11 to enable indirect branch tracking for user mode applications on RISC-V.
12
13 1. Feature Overview
14 --------------------
15
16 Memory corruption issues usually result in crashes. However, in the
17 hands of a creative adversary, these can result in a variety of
18 security issues.
19
20 Some of those security issues can be code re-use attacks, where an
21 adversary can use corrupt function pointers, chaining them together to
22 perform jump oriented programming (JOP) or call oriented programming
23 (COP) and thus compromise control flow integrity (CFI) of the program.
24
25 Function pointers live in read-write memory and thus are susceptible
26 to corruption. This can allow an adversary to control the program
27 counter (PC) value. On RISC-V, the zicfilp extension enforces a
28 restriction on such indirect control transfers:
29
30 - Indirect control transfers must land on a landing pad instruction ``lpad``.
31 There are two exceptions to this rule:
32
33 - rs1 = x1 or rs1 = x5, i.e. a return from a function and returns are
34 protected using shadow stack (see zicfiss.rst)
35
36 - rs1 = x7. On RISC-V, the compiler usually does the following to reach a
37 function which is beyond the offset of possible J-type instruction::
38
39 auipc x7, <imm>
40 jalr (x7)
41
42 This form of indirect control transfer is immutable and doesn't
43 rely on memory. Thus rs1=x7 is exempted from tracking and
44 these are considered software guarded jumps.
45
46 The ``lpad`` instruction is a pseudo-op of ``auipc rd, <imm_20bit>``
47 with ``rd=x0``. This is a HINT op. The ``lpad`` instruction must be
48 aligned on a 4 byte boundary. It compares the 20 bit immediate with
49 x7. If ``imm_20bit`` == 0, the CPU doesn't perform any comparison with
50 ``x7``. If ``imm_20bit`` != 0, then ``imm_20bit`` must match ``x7``
51 else CPU will raise ``software check exception`` (``cause=18``) with
52 ``*tval = 2``.
53
54 The compiler can generate a hash over function signatures and set them
55 up (truncated to 20 bits) in x7 at callsites. Function prologues can
56 have ``lpad`` instructions encoded with the same function hash. This
57 further reduces the number of valid program counter addresses a call
58 site can reach.
59
60 2. ELF and psABI
61 -----------------
62
63 The toolchain sets up :c:macro:`GNU_PROPERTY_RISCV_FEATURE_1_FCFI` for
64 property :c:macro:`GNU_PROPERTY_RISCV_FEATURE_1_AND` in the notes
65 section of the object file.
66
67 3. Linux enabling
68 ------------------
69
70 User space programs can have multiple shared objects loaded in their
71 address spaces. It's a difficult task to make sure all the
72 dependencies have been compiled with indirect branch support. Thus
73 it's left to the dynamic loader to enable indirect branch tracking for
74 the program.
75
76 4. prctl() enabling
77 --------------------
78
79 Per-task indirect branch tracking state can be monitored and
80 controlled via the :c:macro:`PR_GET_CFI` and :c:macro:`PR_SET_CFI`
81 ``prctl()`` arguments (respectively), by supplying
82 :c:macro:`PR_CFI_BRANCH_LANDING_PADS` as the second argument. These
83 are architecture-agnostic, and will return -EINVAL if the underlying
84 functionality is not supported.
85
86 * prctl(:c:macro:`PR_SET_CFI`, :c:macro:`PR_CFI_BRANCH_LANDING_PADS`, unsigned long arg)
87
88 arg is a bitmask.
89
90 If :c:macro:`PR_CFI_ENABLE` is set in arg, and the CPU supports
91 ``zicfilp``, then the kernel will enable indirect branch tracking for
92 the task. The dynamic loader can issue this ``prctl()`` once it has
93 determined that all the objects loaded in the address space support
94 indirect branch tracking.
95
96 Indirect branch tracking state can also be locked once enabled. This
97 prevents the task from subsequently disabling it. This is done by
98 setting the bit :c:macro:`PR_CFI_LOCK` in arg. Either indirect branch
99 tracking must already be enabled for the task, or the bit
100 :c:macro:`PR_CFI_ENABLE` must also be set in arg. This is intended
101 for environments that wish to run with a strict security posture that
102 do not wish to load objects without ``zicfilp`` support.
103
104 Indirect branch tracking can also be disabled for the task, assuming
105 that it has not previously been enabled and locked. If there is a
106 ``dlopen()`` to an object which wasn't compiled with ``zicfilp``, the
107 dynamic loader can issue this ``prctl()`` with arg set to
108 :c:macro:`PR_CFI_DISABLE`. Disabling indirect branch tracking for the
109 task is not possible if it has previously been enabled and locked.
110
111
112 * prctl(:c:macro:`PR_GET_CFI`, :c:macro:`PR_CFI_BRANCH_LANDING_PADS`, unsigned long * arg)
113
114 Returns the current status of indirect branch tracking into a bitmask
115 stored into the memory location pointed to by arg. The bitmask will
116 have the :c:macro:`PR_CFI_ENABLE` bit set if indirect branch tracking
117 is currently enabled for the task, and if it is locked, will
118 additionally have the :c:macro:`PR_CFI_LOCK` bit set. If indirect
119 branch tracking is currently disabled for the task, the
120 :c:macro:`PR_CFI_DISABLE` bit will be set.
121
122
123 5. violations related to indirect branch tracking
124 --------------------------------------------------
125
126 Pertaining to indirect branch tracking, the CPU raises a software
127 check exception in the following conditions:
128
129 - missing ``lpad`` after indirect call / jmp
130 - ``lpad`` not on 4 byte boundary
131 - ``imm_20bit`` embedded in ``lpad`` instruction doesn't match with ``x7``
132
133 In all 3 cases, ``*tval = 2`` is captured and software check exception is
134 raised (``cause=18``).
135
136 The kernel will treat this as :c:macro:`SIGSEGV` with code =
137 :c:macro:`SEGV_CPERR` and follow the normal course of signal delivery.