xref: /xnu-12377.61.12/tests/arm_mte_launchd.c (revision 4d495c6e23c53686cf65f45067f79024cf5dcee8)
1 #include <assert.h>
2 #include <darwintest.h>
3 #include <darwintest_multiprocess.h>
4 #include <darwintest_utils.h>
5 #include <libproc.h>
6 #include <mach/mach.h>
7 #include <mach/mach_init.h>
8 #include <mach/vm_map.h>
9 #include <stddef.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <sys/proc_info.h>
14 #include <sys/proc_info_private.h>
15 #include <unistd.h>
16 #include <stdbool.h>
17 
18 #include "arm_mte_utilities.h"
19 #include "test_utils.h"
20 
21 #if (TARGET_OS_OSX || TARGET_OS_IOS) && defined(__arm64__)
22 #if !(TARGET_OS_XR || TARGET_OS_TV || TARGET_OS_WATCH || TARGET_OS_BRIDGE)
23 #define TARGET_SUPPORTS_MTE_EMULATION 1
24 #endif
25 #endif
26 
27 
28 T_GLOBAL_META(
29 	T_META_NAMESPACE("xnu.arm.mte"),
30 	T_META_RADAR_COMPONENT_NAME("Darwin Testing"),
31 	T_META_RADAR_COMPONENT_VERSION("all"),
32 	T_META_OWNER("magarwal23"),
33 	T_META_RUN_CONCURRENTLY(false),
34 	T_META_IGNORECRASHES(".*arm_mte.*")
35 	);
36 
37 static bool
_does_pid_have_mte(pid_t pid)38 _does_pid_have_mte(pid_t pid)
39 {
40 	struct proc_bsdinfowithuniqid info;
41 	int ret = proc_pidinfo(pid, PROC_PIDT_BSDINFOWITHUNIQID, 1, &info,
42 	    PROC_PIDT_BSDINFOWITHUNIQID_SIZE);
43 	if (ret == 0 || ret != sizeof(info)) {
44 		return false;
45 	}
46 
47 	return (info.pbsd.pbi_flags & PROC_FLAG_SEC_ENABLED) != 0;
48 }
49 /* Case 1: Upon reboot (with no MTE disabling boot-arg), launchd should run
50  *  as pid 1, and should be MTE enabled (launchctl procinfo 1 should show MTE
51  *  enabled) */
52 T_DECL(check_launchd_mte_enabled, "launchd__test__1",
53     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE4", 1),
54     XNU_T_META_SOC_SPECIFIC,
55     T_META_REQUIRES_REBOOT(true),
56     T_META_ASROOT(true)) {
57 	#if !__arm64__
58 	T_SKIP("Running on non-arm64 target, skipping...");
59 	#else /* !__arm64__ */
60 	pid_t pid = 1;
61 	T_ASSERT_TRUE(_does_pid_have_mte(pid), "Checking mte flag for launchd with hardware checck for mte");
62 	#endif /* !__arm64__ */
63 }
64 
65 /* Case 2: Upon reboot (with am MTE disabling boot-arg), launchd should
66  *  run as pid 1, and should not be MTE enabled */
67 T_DECL(check_launchd_mte_disabled, "launchd__test__2",
68     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE4", 1), XNU_T_META_SOC_SPECIFIC, T_META_BOOTARGS_SET("-disable_mte"), T_META_ASROOT(true)) {
69 	#if !__arm64__
70 	T_SKIP("Running on non-arm64 target, skipping...");
71 	#else /* !__arm64__ */
72 	pid_t pid = 1;
73 	T_ASSERT_FALSE(_does_pid_have_mte(pid),
74 	    "Checking mte flag for (-disable_mte) launchd");
75 	#endif /* !__arm64__ */
76 }
77