Skip to content

Commit d192ab9

Browse files
committed
[lldb][AArch64] Fix expression evaluation with Guarded Control Stacks
When the Guarded Control Stack (GCS) is enabled, returns cause the processor to validate that the address at the location pointed to by gcspr_el0 matches the one in the link register. ``` ret (lr=A) << pc | GCS | +=====+ | A | | B | << gcspr_el0 Fault: tried to return to A when you should have returned to B. ``` Therefore when an expression wraper function tries to return to the expression return address (usually `_start` if there is a libc), it would fault. ``` ret (lr=_start) << pc | GCS | +============+ | user_func1 | | user_func2 | << gcspr_el0 Fault: tried to return to _start when you should have return to user_func2. ``` To fix this we must push that return address to the GCS in PrepareTrivialCall. This value is then consumed by the final return and the expression completes as expected. ``` ret (lr=_start) << pc | GCS | +============+ | user_func1 | | user_func2 | | _start | << gcspr_el0 No fault, we return to _start as normal. ``` The gcspr_el0 register will be restored after expression evaluation so that the program can continue correctly. However, due to restrictions in the Linux GCS ABI, we will not restore the enable bit of gcs_features_enabled. Re-enabling GCS via ptrace is not supported because it requires memory to be allocated. We could disable GCS if the expression enabled GCS, however this would use up that state transition that the program might later rely on. And generally it is cleaner to ignore the whole bit rather than one state transition of it. We will also not restore the GCS entry that was overwritten with the expression's return address. On the grounds that: * This entry will never be used by the program. If the program branches, the entry will be overwritten. If the program returns, gcspr_el0 will point to the entry before the expression return address and that entry will instead be validated. * Any expression that calls functions will overwrite even more entries, so the user needs to be aware of that anyway if they want to preserve the contents of the GCS for inspection. * An expression could leave the program in a state where restoring the value makes the situation worse. Especially if we ever support this in bare metal debugging. I will later document all this on https://lldb.llvm.org/use/aarch64-linux.html as well. Tests have been added for: * A function call that does not interact with GCS. * A call that does, and disables it (we do not re-enable it). * A call that does, and enables it (we do not disable it again).
1 parent 951a389 commit d192ab9

File tree

4 files changed

+276
-39
lines changed

4 files changed

+276
-39
lines changed

lldb/source/Plugins/ABI/AArch64/ABISysV_arm64.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,59 @@ ABISysV_arm64::CreateInstance(lldb::ProcessSP process_sp, const ArchSpec &arch)
6060
return ABISP();
6161
}
6262

63+
static bool PushToLinuxGuardedControlStack(addr_t return_addr,
64+
RegisterContext *reg_ctx,
65+
Thread &thread) {
66+
// If the Guarded Control Stack extension is enabled we need to put the return
67+
// address onto that stack.
68+
const RegisterInfo *gcs_features_enabled_info =
69+
reg_ctx->GetRegisterInfoByName("gcs_features_enabled");
70+
if (!gcs_features_enabled_info)
71+
return false;
72+
73+
uint64_t gcs_features_enabled = reg_ctx->ReadRegisterAsUnsigned(
74+
gcs_features_enabled_info, LLDB_INVALID_ADDRESS);
75+
if (gcs_features_enabled == LLDB_INVALID_ADDRESS)
76+
return false;
77+
78+
// Only attempt this if GCS is enabled. If it's not enabled then gcspr_el0
79+
// may point to unmapped memory.
80+
if ((gcs_features_enabled & 1) == 0)
81+
return false;
82+
83+
const RegisterInfo *gcspr_el0_info =
84+
reg_ctx->GetRegisterInfoByName("gcspr_el0");
85+
if (!gcspr_el0_info)
86+
return false;
87+
88+
uint64_t gcspr_el0 =
89+
reg_ctx->ReadRegisterAsUnsigned(gcspr_el0_info, LLDB_INVALID_ADDRESS);
90+
if (gcspr_el0 == LLDB_INVALID_ADDRESS)
91+
return false;
92+
93+
// A link register entry on the GCS is 8 bytes.
94+
gcspr_el0 -= 8;
95+
if (!reg_ctx->WriteRegisterFromUnsigned(gcspr_el0_info, gcspr_el0))
96+
return false;
97+
98+
Status error;
99+
size_t wrote = thread.GetProcess()->WriteMemory(gcspr_el0, &return_addr,
100+
sizeof(return_addr), error);
101+
if ((wrote != sizeof(return_addr) || error.Fail()))
102+
return false;
103+
104+
Log *log = GetLog(LLDBLog::Expressions);
105+
LLDB_LOGF(log,
106+
"Pushed return address 0x%" PRIx64 "to Guarded Control Stack. "
107+
"gcspr_el0 was 0%" PRIx64 ", is now 0x%" PRIx64 ".",
108+
return_addr, gcspr_el0 + 8, gcspr_el0);
109+
110+
// gcspr_el0 will be restored to the original value by lldb-server after
111+
// the call has finished, which serves as the "pop".
112+
113+
return true;
114+
}
115+
63116
bool ABISysV_arm64::PrepareTrivialCall(Thread &thread, addr_t sp,
64117
addr_t func_addr, addr_t return_addr,
65118
llvm::ArrayRef<addr_t> args) const {
@@ -103,6 +156,9 @@ bool ABISysV_arm64::PrepareTrivialCall(Thread &thread, addr_t sp,
103156
return_addr))
104157
return false;
105158

159+
if (GetProcessSP()->GetTarget().GetArchitecture().GetTriple().isOSLinux())
160+
PushToLinuxGuardedControlStack(return_addr, reg_ctx, thread);
161+
106162
// Set "sp" to the requested value
107163
if (!reg_ctx->WriteRegisterFromUnsigned(
108164
reg_ctx->GetRegisterInfo(eRegisterKindGeneric,

lldb/source/Plugins/Process/Linux/NativeRegisterContextLinux_arm64.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1063,9 +1063,27 @@ Status NativeRegisterContextLinux_arm64::WriteAllRegisterValues(
10631063
std::bind(&NativeRegisterContextLinux_arm64::WriteFPMR, this));
10641064
break;
10651065
case RegisterSetType::GCS:
1066+
// It is not permitted to enable GCS via ptrace. We can disable it, but
1067+
// to keep things simple we will not revert any change to the
1068+
// PR_SHADOW_STACK_ENABLE bit. Instead patch in the current enable bit
1069+
// into the registers we are about to restore.
1070+
m_gcs_is_valid = false;
1071+
error = ReadGCS();
1072+
if (error.Fail())
1073+
return error;
1074+
1075+
uint64_t enable_bit = m_gcs_regs.features_enabled & 1UL;
1076+
gcs_regs new_gcs_regs = *reinterpret_cast<const gcs_regs *>(src);
1077+
new_gcs_regs.features_enabled =
1078+
(new_gcs_regs.features_enabled & ~1UL) | enable_bit;
1079+
1080+
const uint8_t *new_gcs_src =
1081+
reinterpret_cast<const uint8_t *>(&new_gcs_regs);
10661082
error = RestoreRegisters(
1067-
GetGCSBuffer(), &src, GetGCSBufferSize(), m_gcs_is_valid,
1083+
GetGCSBuffer(), &new_gcs_src, GetGCSBufferSize(), m_gcs_is_valid,
10681084
std::bind(&NativeRegisterContextLinux_arm64::WriteGCS, this));
1085+
src += GetGCSBufferSize();
1086+
10691087
break;
10701088
}
10711089

lldb/test/API/linux/aarch64/gcs/TestAArch64LinuxGCS.py

Lines changed: 159 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,38 @@ def test_gcs_fault(self):
8484
],
8585
)
8686

87+
def check_gcs_registers(
88+
self,
89+
expected_gcs_features_enabled=None,
90+
expected_gcs_features_locked=None,
91+
expected_gcspr_el0=None,
92+
):
93+
thread = self.dbg.GetSelectedTarget().process.GetThreadAtIndex(0)
94+
registerSets = thread.GetFrameAtIndex(0).GetRegisters()
95+
gcs_registers = registerSets.GetFirstValueByName(
96+
r"Guarded Control Stack Registers"
97+
)
98+
99+
gcs_features_enabled = gcs_registers.GetChildMemberWithName(
100+
"gcs_features_enabled"
101+
).GetValueAsUnsigned()
102+
if expected_gcs_features_enabled is not None:
103+
self.assertEqual(expected_gcs_features_enabled, gcs_features_enabled)
104+
105+
gcs_features_locked = gcs_registers.GetChildMemberWithName(
106+
"gcs_features_locked"
107+
).GetValueAsUnsigned()
108+
if expected_gcs_features_locked is not None:
109+
self.assertEqual(expected_gcs_features_locked, gcs_features_locked)
110+
111+
gcspr_el0 = gcs_registers.GetChildMemberWithName(
112+
"gcspr_el0"
113+
).GetValueAsUnsigned()
114+
if expected_gcspr_el0 is not None:
115+
self.assertEqual(expected_gcspr_el0, gcspr_el0)
116+
117+
return gcs_features_enabled, gcs_features_locked, gcspr_el0
118+
87119
@skipUnlessArch("aarch64")
88120
@skipUnlessPlatform(["linux"])
89121
def test_gcs_registers(self):
@@ -108,38 +140,7 @@ def test_gcs_registers(self):
108140

109141
self.expect("register read --all", substrs=["Guarded Control Stack Registers:"])
110142

111-
def check_gcs_registers(
112-
expected_gcs_features_enabled=None,
113-
expected_gcs_features_locked=None,
114-
expected_gcspr_el0=None,
115-
):
116-
thread = self.dbg.GetSelectedTarget().process.GetThreadAtIndex(0)
117-
registerSets = thread.GetFrameAtIndex(0).GetRegisters()
118-
gcs_registers = registerSets.GetFirstValueByName(
119-
r"Guarded Control Stack Registers"
120-
)
121-
122-
gcs_features_enabled = gcs_registers.GetChildMemberWithName(
123-
"gcs_features_enabled"
124-
).GetValueAsUnsigned()
125-
if expected_gcs_features_enabled is not None:
126-
self.assertEqual(expected_gcs_features_enabled, gcs_features_enabled)
127-
128-
gcs_features_locked = gcs_registers.GetChildMemberWithName(
129-
"gcs_features_locked"
130-
).GetValueAsUnsigned()
131-
if expected_gcs_features_locked is not None:
132-
self.assertEqual(expected_gcs_features_locked, gcs_features_locked)
133-
134-
gcspr_el0 = gcs_registers.GetChildMemberWithName(
135-
"gcspr_el0"
136-
).GetValueAsUnsigned()
137-
if expected_gcspr_el0 is not None:
138-
self.assertEqual(expected_gcspr_el0, gcspr_el0)
139-
140-
return gcs_features_enabled, gcs_features_locked, gcspr_el0
141-
142-
enabled, locked, spr_el0 = check_gcs_registers()
143+
enabled, locked, spr_el0 = self.check_gcs_registers()
143144

144145
# Features enabled should have at least the enable bit set, it could have
145146
# others depending on what the C library did.
@@ -161,7 +162,7 @@ def check_gcs_registers(
161162
substrs=["stopped", "stop reason = breakpoint"],
162163
)
163164

164-
_, _, spr_el0 = check_gcs_registers(enabled, locked, spr_el0 - 8)
165+
_, _, spr_el0 = self.check_gcs_registers(enabled, locked, spr_el0 - 8)
165166

166167
# Modify the control stack pointer to cause a fault.
167168
spr_el0 += 8
@@ -217,3 +218,128 @@ def check_gcs_registers(
217218
"exited with status = 0",
218219
],
219220
)
221+
222+
@skipUnlessPlatform(["linux"])
223+
def test_gcs_expression_simple(self):
224+
if not self.isAArch64GCS():
225+
self.skipTest("Target must support GCS.")
226+
227+
self.build()
228+
self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET)
229+
230+
# Break before GCS has been enabled.
231+
self.runCmd("b main")
232+
# And after it has been enabled.
233+
lldbutil.run_break_set_by_file_and_line(
234+
self,
235+
"main.c",
236+
line_number("main.c", "// Set break point at this line."),
237+
num_expected_locations=1,
238+
)
239+
240+
self.runCmd("run", RUN_SUCCEEDED)
241+
242+
if self.process().GetState() == lldb.eStateExited:
243+
self.fail("Test program failed to run.")
244+
245+
self.expect(
246+
"thread list",
247+
STOPPED_DUE_TO_BREAKPOINT,
248+
substrs=["stopped", "stop reason = breakpoint"],
249+
)
250+
251+
# GCS has not been enabled yet and the ABI plugin should know not to
252+
# attempt pushing to the control stack.
253+
before = self.check_gcs_registers()
254+
expr_cmd = "p get_gcs_status()"
255+
self.expect(expr_cmd, substrs=["(unsigned long) 0"])
256+
self.check_gcs_registers(*before)
257+
258+
# Continue to when GCS has been enabled.
259+
self.runCmd("continue")
260+
self.expect(
261+
"thread list",
262+
STOPPED_DUE_TO_BREAKPOINT,
263+
substrs=["stopped", "stop reason = breakpoint"],
264+
)
265+
266+
# This time we do need to push to the GCS and having done so, we can
267+
# return from this expression without causing a fault.
268+
before = self.check_gcs_registers()
269+
self.expect(expr_cmd, substrs=["(unsigned long) 1"])
270+
self.check_gcs_registers(*before)
271+
272+
@skipUnlessPlatform(["linux"])
273+
def test_gcs_expression_disable_gcs(self):
274+
if not self.isAArch64GCS():
275+
self.skipTest("Target must support GCS.")
276+
277+
self.build()
278+
self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET)
279+
280+
# Break after GCS is enabled.
281+
lldbutil.run_break_set_by_file_and_line(
282+
self,
283+
"main.c",
284+
line_number("main.c", "// Set break point at this line."),
285+
num_expected_locations=1,
286+
)
287+
288+
self.runCmd("run", RUN_SUCCEEDED)
289+
290+
if self.process().GetState() == lldb.eStateExited:
291+
self.fail("Test program failed to run.")
292+
293+
self.expect(
294+
"thread list",
295+
STOPPED_DUE_TO_BREAKPOINT,
296+
substrs=["stopped", "stop reason = breakpoint"],
297+
)
298+
299+
# Unlock all features so the expression can enable them again.
300+
self.runCmd("register write gcs_features_locked 0")
301+
# Disable all features, but keep GCS itself enabled.
302+
PR_SHADOW_STACK_ENABLE = 1
303+
self.runCmd(f"register write gcs_features_enabled 0x{PR_SHADOW_STACK_ENABLE:x}")
304+
305+
enabled, locked, spr_el0 = self.check_gcs_registers()
306+
# We restore everything apart GCS being enabled, as we are not allowed to
307+
# go from disabled -> enabled via ptrace.
308+
self.expect("p change_gcs_config(false)", substrs=["true"])
309+
enabled &= ~1
310+
self.check_gcs_registers(enabled, locked, spr_el0)
311+
312+
@skipUnlessPlatform(["linux"])
313+
def test_gcs_expression_enable_gcs(self):
314+
if not self.isAArch64GCS():
315+
self.skipTest("Target must support GCS.")
316+
317+
self.build()
318+
self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET)
319+
320+
# Break before GCS is enabled.
321+
self.runCmd("b main")
322+
323+
self.runCmd("run", RUN_SUCCEEDED)
324+
325+
if self.process().GetState() == lldb.eStateExited:
326+
self.fail("Test program failed to run.")
327+
328+
self.expect(
329+
"thread list",
330+
STOPPED_DUE_TO_BREAKPOINT,
331+
substrs=["stopped", "stop reason = breakpoint"],
332+
)
333+
334+
# Unlock all features so the expression can enable them again.
335+
self.runCmd("register write gcs_features_locked 0")
336+
# Disable all features. The program needs PR_SHADOW_STACK_PUSH, but it
337+
# will enable that itself.
338+
self.runCmd(f"register write gcs_features_enabled 0")
339+
340+
enabled, locked, spr_el0 = self.check_gcs_registers()
341+
self.expect("p change_gcs_config(true)", substrs=["true"])
342+
# Though we could disable GCS with ptrace, we choose not to to be
343+
# consistent with the disabled -> enabled behaviour.
344+
enabled |= 1
345+
self.check_gcs_registers(enabled, locked, spr_el0)

lldb/test/API/linux/aarch64/gcs/main.c

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <asm/hwcap.h>
2+
#include <stdbool.h>
23
#include <sys/auxv.h>
34
#include <sys/prctl.h>
45

@@ -8,7 +9,12 @@
89

910
#define PR_GET_SHADOW_STACK_STATUS 74
1011
#define PR_SET_SHADOW_STACK_STATUS 75
11-
#define PR_SHADOW_STACK_ENABLE (1UL)
12+
#define PR_LOCK_SHADOW_STACK_STATUS 76
13+
14+
#define PR_SHADOW_STACK_ENABLE (1UL << 0)
15+
#define PR_SHADOW_STACK_WRITE (1UL << 1)
16+
#define PR_SHADOW_STACK_PUSH (1UL << 2)
17+
1218
#define PRCTL_SYSCALL_NO 167
1319

1420
// Once we enable GCS, we cannot return from the function that made the syscall
@@ -36,6 +42,36 @@ unsigned long get_gcs_status() {
3642
return mode;
3743
}
3844

45+
extern void _start();
46+
bool change_gcs_config(bool enable) {
47+
// The test unlocks and disables all features (excluding the main enable bit)
48+
// before calling this expression. Enable them again.
49+
unsigned long new_status =
50+
enable | PR_SHADOW_STACK_PUSH | PR_SHADOW_STACK_WRITE;
51+
52+
if (enable) {
53+
// We would not be able to return from prctl().
54+
my_prctl(PR_SET_SHADOW_STACK_STATUS, new_status, 0, 0, 0);
55+
56+
// This is a stack, so we must push in reverse order to the pops we want to
57+
// have later. So push the return of __lldb_expr (_start), then the return
58+
// address of this function (__lldb_expr).
59+
__asm__ __volatile__("sys #3, C7, C7, #0, %0\n" // gcspushm _start
60+
"sys #3, C7, C7, #0, x30\n" // gcspushm x30
61+
:
62+
: "r"(_start));
63+
} else {
64+
if (prctl(PR_SET_SHADOW_STACK_STATUS, new_status, 0, 0, 0) != 0)
65+
return false;
66+
}
67+
68+
// Turn back on all locks.
69+
if (prctl(PR_LOCK_SHADOW_STACK_STATUS, ~(0UL), 0, 0, 0) != 0)
70+
return false;
71+
72+
return true;
73+
}
74+
3975
void gcs_signal() {
4076
// If we enabled GCS manually, then we could just return from main to generate
4177
// a signal. However, if the C library enabled it, then we'd just exit
@@ -50,10 +86,11 @@ void gcs_signal() {
5086
}
5187

5288
// These functions are used to observe gcspr_el0 changing as we enter them, and
53-
// the fault we cause by changing its value.
54-
void test_func2() { volatile int i = 99; }
89+
// the fault we cause by changing its value. Also used to check expression
90+
// eval can handle function calls.
91+
int test_func2() { return 99; }
5592

56-
void test_func() { test_func2(); }
93+
int test_func() { return test_func2(); }
5794

5895
int main() {
5996
if (!(getauxval(AT_HWCAP) & HWCAP_GCS))
@@ -71,7 +108,7 @@ int main() {
71108
// By now we should have one memory region where the GCS is stored.
72109

73110
// For register read/write tests.
74-
test_func();
111+
volatile int i = test_func();
75112

76113
// If this was a register test, we would have disabled GCS during the
77114
// test_func call. We cannot re-enable it from ptrace so skip this part in

0 commit comments

Comments
 (0)