Skip to content

Commit 78f77d5

Browse files
ylnJDevlieghere
authored andcommitted
[lldb][Darwin] Add process launch --memory-tagging option (llvm#162944)
For debugging and bug-finding workflows on Darwin, support launching processes with memory tagging for binaries that are not entitled. This will cause the process to behave as if the binary was entitled with: ``` <key>com.apple.security.hardened-process.checked-allocations</key> <true/> ``` This has no effect on hardware without MTE support. --------- Co-authored-by: Jonas Devlieghere <[email protected]>
1 parent b120448 commit 78f77d5

File tree

6 files changed

+63
-4
lines changed

6 files changed

+63
-4
lines changed

lldb/include/lldb/lldb-enumerations.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ FLAGS_ENUM(LaunchFlags){
130130
eLaunchFlagInheritTCCFromParent =
131131
(1u << 12), ///< Don't make the inferior responsible for its own TCC
132132
///< permissions but instead inherit them from its parent.
133+
eLaunchFlagMemoryTagging =
134+
(1u << 13), ///< Launch process with memory tagging explicitly enabled.
133135
};
134136

135137
/// Thread Run Modes.

lldb/source/Commands/CommandOptionsProcessLaunch.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ Status CommandOptionsProcessLaunch::SetOptionValue(
127127
break;
128128
}
129129

130+
case 'M':
131+
launch_info.GetFlags().Set(eLaunchFlagMemoryTagging);
132+
break;
133+
130134
case 'c':
131135
if (!option_arg.empty())
132136
launch_info.SetShell(FileSpec(option_arg));

lldb/source/Commands/Options.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,6 +1173,11 @@ let Command = "process launch" in {
11731173
Arg<"Boolean">,
11741174
Desc<"Set whether to shell expand arguments to the process when "
11751175
"launching.">;
1176+
def process_launch_memory_tagging
1177+
: Option<"memory-tagging", "M">,
1178+
Desc<"Set whether to explicitly enable memory tagging when launching "
1179+
"the process. Requires hardware support. "
1180+
"(Only supported on Darwin.)">;
11761181
}
11771182

11781183
let Command = "process attach" in {

lldb/source/Host/macosx/objcxx/Host.mm

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,6 +1210,39 @@ static Status LaunchProcessPosixSpawn(const char *exe_path,
12101210
}
12111211
}
12121212

1213+
if (launch_info.GetFlags().Test(eLaunchFlagMemoryTagging)) {
1214+
// The following function configures the spawn attributes to launch the
1215+
// process with memory tagging explicitly enabled. We look it up
1216+
// dynamically since it is only available on newer OS. Does nothing on
1217+
// hardware which does not support MTE.
1218+
//
1219+
// int posix_spawnattr_set_use_sec_transition_shims_np(
1220+
// posix_spawnattr_t *attr, uint32_t flags);
1221+
//
1222+
using posix_spawnattr_set_use_sec_transition_shims_np_t =
1223+
int (*)(posix_spawnattr_t *attr, uint32_t flags);
1224+
auto posix_spawnattr_set_use_sec_transition_shims_np_fn =
1225+
(posix_spawnattr_set_use_sec_transition_shims_np_t)dlsym(
1226+
RTLD_DEFAULT, "posix_spawnattr_set_use_sec_transition_shims_np");
1227+
if (posix_spawnattr_set_use_sec_transition_shims_np_fn) {
1228+
error =
1229+
Status(posix_spawnattr_set_use_sec_transition_shims_np_fn(&attr, 0),
1230+
eErrorTypePOSIX);
1231+
if (error.Fail()) {
1232+
LLDB_LOG(log,
1233+
"error: {0}, "
1234+
"posix_spawnattr_set_use_sec_transition_shims_np(&attr, 0)",
1235+
error);
1236+
return error;
1237+
}
1238+
} else {
1239+
LLDB_LOG(log,
1240+
"error: posix_spawnattr_set_use_sec_transition_shims_np not "
1241+
"available",
1242+
error);
1243+
}
1244+
}
1245+
12131246
// Don't set the binpref if a shell was provided. After all, that's only
12141247
// going to affect what version of the shell is launched, not what fork of
12151248
// the binary is launched. We insert "arch --arch <ARCH> as part of the

lldb/test/API/macosx/mte/Makefile

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
C_SOURCES := main.c
22

3-
EXE := uaf_mte
3+
EXE := uaf
44

5-
all: uaf_mte sign
5+
binary-plain: uaf
6+
binary-entitled: uaf sign
7+
8+
all: binary-entitled
69

710
include Makefile.rules
811

9-
sign: mte-entitlements.plist uaf_mte
12+
sign: mte-entitlements.plist uaf
1013
ifeq ($(OS),Darwin)
1114
codesign -s - -f --entitlements $^
1215
endif

lldb/test/API/macosx/mte/TestDarwinMTE.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,24 @@
77
from lldbsuite.test import lldbutil
88
import lldbsuite.test.cpu_feature as cpu_feature
99

10-
exe_name = "uaf_mte" # Must match Makefile
10+
exe_name = "uaf" # Must match Makefile
1111

1212

1313
class TestDarwinMTE(TestBase):
1414
NO_DEBUG_INFO_TESTCASE = True
1515

16+
@skipUnlessFeature(cpu_feature.AArch64.MTE)
17+
def test_process_launch_memory_tagging(self):
18+
self.build(make_targets=["binary-plain"])
19+
self.createTestTarget(self.getBuildArtifact(exe_name))
20+
21+
self.expect("process launch", substrs=["exited with status = 0"])
22+
23+
self.expect(
24+
"process launch --memory-tagging",
25+
substrs=["stopped", "stop reason = EXC_ARM_MTE_TAG_FAULT"],
26+
)
27+
1628
@skipUnlessFeature(cpu_feature.AArch64.MTE)
1729
def test_tag_fault(self):
1830
self.build()

0 commit comments

Comments
 (0)