1 /*
2 * Copyright (c) 2025 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #if __arm64__
30 #include <darwintest.h>
31 #include <signal.h>
32 #include <spawn_private.h>
33 #include <stdlib.h>
34 #include <sysexits.h>
35
36 #include "arm_mte_utilities.h"
37 #include "test_utils.h"
38
39 static void
do_preflight_spawn_test(char * binary_to_launch,bool expects_mte,bool expects_soft)40 do_preflight_spawn_test(
41 char *binary_to_launch,
42 bool expects_mte,
43 bool expects_soft)
44 {
45 char *expectation_arg = expects_mte ? EXPECT_MTE : DO_NOT_EXPECT_MTE;
46 char *next_test_arg = MTE_ENABLEMENT_TEST_DONE_STR;
47 char *argv[] = {
48 binary_to_launch,
49 expectation_arg,
50 next_test_arg,
51 NULL
52 };
53 pid_t child_pid = 0;
54 int waitpid_result = 0;
55 int status = 0;
56 posix_spawn_secflag_options flags = (POSIX_SPAWN_SECFLAG_EXPLICIT_ENABLE |
57 POSIX_SPAWN_SECFLAG_EXPLICIT_PREFLIGHT);
58 posix_spawnattr_t attr = NULL;
59
60 /* Initialize spawnattr */
61 errno_t ret = posix_spawnattr_init(&attr);
62 T_ASSERT_POSIX_ZERO(ret, "posix_spawnattr_init");
63
64 /* Request the process to be suspended upon starting */
65 ret = posix_spawnattr_setflags(&attr, POSIX_SPAWN_START_SUSPENDED);
66 T_QUIET;
67 T_ASSERT_POSIX_SUCCESS(ret, "posix_spawnattr_setflags");
68
69 /* Request soft-mode preflighting */
70 ret = posix_spawnattr_set_use_sec_transition_shims_np(&attr, flags);
71 T_QUIET;
72 T_ASSERT_POSIX_ZERO(ret, "posix_spawnattr_set_use_sec_transition_shims_np");
73
74 /* Spawn the process (suspended) */
75 ret = posix_spawn(&child_pid, argv[0], NULL, &attr, argv, NULL);
76 T_QUIET;
77 T_ASSERT_POSIX_SUCCESS(ret, "posix_spawn");
78
79 /* Validate whether soft mode is enabled or not */
80 validate_proc_pidinfo_mte_soft_mode_status(child_pid, expects_soft);
81
82 ret = posix_spawnattr_destroy(&attr);
83 T_QUIET;
84 T_ASSERT_POSIX_ZERO(ret, "posix_spawnattr_destroy");
85
86 /* Let the child continue */
87 ret = kill(child_pid, SIGCONT);
88 T_QUIET;
89 T_ASSERT_POSIX_SUCCESS(ret, "kill(SIGCONT)");
90
91 /* Handle termination of the child */
92 waitpid_result = waitpid(child_pid, &status, 0);
93 T_QUIET;
94 T_ASSERT_POSIX_SUCCESS(waitpid_result, "waitpid");
95 T_QUIET;
96 T_ASSERT_EQ(WIFEXITED(status), 1, "child should have exited normally");
97 T_QUIET;
98 T_ASSERT_EQ(WEXITSTATUS(status), EX_OK, "child should have exited with success");
99 }
100
101 T_DECL(mte_preflight,
102 "Test that we can enable preflighting on non-entitled binaries",
103 T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE4", 1),
104 XNU_T_META_SOC_SPECIFIC)
105 {
106 do_preflight_spawn_test(SPAWN_HELPER_WITHOUT_ENTITLEMENT, true, true);
107 }
108
109 T_DECL(mte_preflight_no_downgrade,
110 "Test that we cannot downgrade entitled binaries to soft mode",
111 T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE4", 1),
112 XNU_T_META_SOC_SPECIFIC)
113 {
114 do_preflight_spawn_test(SPAWN_HELPER_WITH_ENTITLEMENT, true, false);
115 }
116 #endif /* __arm64__ */
117