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