]> git.saurik.com Git - apple/xnu.git/blob - tests/no32exec_35914211.c
xnu-6153.41.3.tar.gz
[apple/xnu.git] / tests / no32exec_35914211.c
1 #include <spawn.h>
2 #include <sys/wait.h>
3 #include <darwintest.h>
4 #include <mach-o/dyld.h>
5 #include <errno.h>
6 #include <unistd.h>
7 #include <stdlib.h>
8 #include <signal.h>
9
10 T_DECL(no32exec_bootarg_with_spawn, "make sure we can't posix_spawn 32-bit")
11 {
12 int spawn_ret, pid;
13 char path[1024];
14 uint32_t size = sizeof(path);
15
16 T_QUIET; T_ASSERT_EQ(_NSGetExecutablePath(path, &size), 0, NULL);
17 T_QUIET; T_ASSERT_LT(strlcat(path, "_helper", size), (unsigned long)size, NULL);
18
19 spawn_ret = posix_spawn(&pid, path, NULL, NULL, NULL, NULL);
20 if (spawn_ret == 0) {
21 int wait_ret = 0;
22 waitpid(pid, &wait_ret, 0);
23 T_ASSERT_FALSE(WIFEXITED(wait_ret), "i386 helper should not run");
24 }
25 T_ASSERT_EQ(spawn_ret, EBADARCH, NULL);
26 }
27
28 T_DECL(no32_exec_bootarg_with_exec, "make sure we can't fork and exec 32-bit")
29 {
30 int pid;
31 char path[1024];
32 uint32_t size = sizeof(path);
33
34 T_QUIET; T_ASSERT_EQ(_NSGetExecutablePath(path, &size), 0, NULL);
35 T_QUIET; T_ASSERT_LT(strlcat(path, "_helper", size), (unsigned long)size, NULL);
36
37 pid = fork();
38 T_QUIET; T_ASSERT_POSIX_SUCCESS(pid, "fork");
39
40 if (pid == 0) { /* child */
41 execve(path, NULL, NULL); /* this should fail, resulting in the call to exit below */
42 exit(errno);
43 } else { /* parent */
44 int wait_ret = 0;
45 waitpid(pid, &wait_ret, 0);
46 T_QUIET; T_ASSERT_TRUE(WIFEXITED(wait_ret), "child should have called exit()");
47 T_ASSERT_EQ(WEXITSTATUS(wait_ret), EBADARCH, "execve should set errno = EBADARCH");
48 }
49 }
50
51 T_DECL(no32exec_bootarg_with_spawn_binprefs, "make sure we honor no32exec, using posix_spawn with binprefs on a fat i386/x86_64 Mach-O")
52 {
53 int pid, ret;
54 posix_spawnattr_t spawnattr;
55 cpu_type_t cpuprefs[] = { CPU_TYPE_X86, CPU_TYPE_X86_64 };
56
57 char path[1024];
58 uint32_t size = sizeof(path);
59 T_QUIET; T_ASSERT_EQ(_NSGetExecutablePath(path, &size), 0, NULL);
60 T_QUIET; T_ASSERT_LT(strlcat(path, "_helper_binprefs", size), (unsigned long)size, NULL);
61
62 ret = posix_spawnattr_init(&spawnattr);
63 T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "posix_spawnattr_init");
64
65 ret = posix_spawnattr_setbinpref_np(&spawnattr, sizeof(cpuprefs) / sizeof(cpuprefs[0]), cpuprefs, NULL);
66 T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "posix_spawnattr_setbinpref_np");
67
68 ret = posix_spawn(&pid, path, NULL, &spawnattr, NULL, NULL);
69 T_ASSERT_EQ(ret, 0, "posix_spawn should succeed despite 32-bit binpref appearing first");
70
71 int wait_ret = 0;
72 ret = waitpid(pid, &wait_ret, 0);
73 T_QUIET; T_ASSERT_EQ(ret, pid, "child pid");
74
75 T_QUIET; T_ASSERT_EQ(WIFEXITED(wait_ret), 1, "child process should have called exit()");
76 T_ASSERT_EQ(WEXITSTATUS(wait_ret), 8, "child process should be running in 64-bit mode");
77
78 ret = posix_spawnattr_destroy(&spawnattr);
79 T_QUIET; T_ASSERT_EQ(ret, 0, "posix_spawnattr_destroy");
80 }
81
82 T_DECL(no32exec_bootarg_with_32only_spawn_binprefs, "make sure we honor no32exec, using posix_spawn with 32-bit only binprefs on a fat i386/x86_64 Mach-O")
83 {
84 int pid, ret, spawn_ret;
85 posix_spawnattr_t spawnattr;
86 cpu_type_t cpuprefs[] = { CPU_TYPE_X86 };
87
88 char path[1024];
89 uint32_t size = sizeof(path);
90 T_QUIET; T_ASSERT_EQ(_NSGetExecutablePath(path, &size), 0, NULL);
91 T_QUIET; T_ASSERT_LT(strlcat(path, "_helper_binprefs", size), (unsigned long)size, NULL);
92
93 ret = posix_spawnattr_init(&spawnattr);
94 T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "posix_spawnattr_init");
95
96 ret = posix_spawnattr_setbinpref_np(&spawnattr, sizeof(cpuprefs) / sizeof(cpuprefs[0]), cpuprefs, NULL);
97 T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "posix_spawnattr_setbinpref_np");
98
99 spawn_ret = posix_spawn(&pid, path, NULL, &spawnattr, NULL, NULL);
100 T_ASSERT_EQ(spawn_ret, EBADARCH, "posix_spawn should return EBADARCH since only 32-bit binpref is requested");
101
102 ret = posix_spawnattr_destroy(&spawnattr);
103 T_QUIET; T_ASSERT_EQ(ret, 0, "posix_spawnattr_destroy");
104 }