xref: /xnu-10002.81.5/tests/test_conclave_spawn.c (revision 5e3eaea39dcf651e66cb99ba7d70e32cc4a99587)
1 /*
2  * Copyright (c) 2023 Apple 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 #include <sys/sysctl.h>
30 #include <mach/mach.h>
31 #include <mach/exclaves.h>
32 #include <mach/exclaves_l4.h>
33 #include <libgen.h>
34 #include <darwintest.h>
35 #include <darwintest_utils.h>
36 #include <removefile.h>
37 #include <spawn.h>
38 #include <spawn_private.h>
39 
40 T_GLOBAL_META(
41 	T_META_NAMESPACE("xnu.exclaves"),
42 	T_META_RADAR_COMPONENT_NAME("xnu"),
43 	T_META_RADAR_COMPONENT_VERSION("spawn"));
44 
45 static int64_t
run_sysctl_test(const char * t,int64_t value)46 run_sysctl_test(const char *t, int64_t value)
47 {
48 	char name[1024];
49 	int64_t result = 0;
50 	size_t s = sizeof(value);
51 	int rc;
52 
53 	snprintf(name, sizeof(name), "debug.test.%s", t);
54 	rc = sysctlbyname(name, &result, &s, &value, s);
55 	T_QUIET; T_ASSERT_POSIX_SUCCESS(rc, "sysctlbyname(%s)", name);
56 	return result;
57 }
58 
59 T_DECL(conclave_spawn, "Test the spawn api for conclave")
60 {
61 	posix_spawnattr_t attrs;
62 	char *test_prog_name = "conclave_process";
63 	char *test_prog_path = "./conclave_process";
64 	char *child_args[3];
65 	char *conclave_name = "com.apple.conclave.test1";
66 	pid_t child_pid;
67 	int64_t r = run_sysctl_test("exclaves_hello_exclave_test", 0);
68 
69 	if (r == -1) {
70 		T_SKIP("Exclave not available");
71 		T_END;
72 	}
73 
74 	/* Initialize posix_spawn attributes */
75 	posix_spawnattr_init(&attrs);
76 
77 	int err = posix_spawnattr_set_conclave_id_np(&attrs, conclave_name);
78 	T_EXPECT_POSIX_SUCCESS(err, "posix_spawnattr_set_conclave_id_np");
79 
80 	child_args[0] = test_prog_name;
81 	child_args[1] = conclave_name;
82 	child_args[2] = NULL;
83 
84 	err = posix_spawn(&child_pid, test_prog_path, NULL, &attrs, &child_args[0], NULL);
85 	T_EXPECT_POSIX_SUCCESS(err, "posix_spawn");
86 
87 	T_LOG("Child pid is %d\n", child_pid);
88 
89 	int child_status;
90 	/* Wait for child and check for return value */
91 	if (-1 == waitpid(child_pid, &child_status, 0)) {
92 		T_FAIL("wait4: child mia with errno %d", errno);
93 	}
94 
95 	if (WIFEXITED(child_status)) {
96 		T_EXPECT_EQ_INT(WEXITSTATUS(child_status), 0, "Check if child returned zero on exit");
97 	} else {
98 		T_FAIL("Child %d did not exit normally\n", child_pid);
99 	}
100 	T_END;
101 }
102