xref: /xnu-8796.141.3/tests/posix_spawn_posix_cred.c (revision 1b191cb58250d0705d8a51287127505aa4bc0789)
1*1b191cb5SApple OSS Distributions #include <darwintest.h>
2*1b191cb5SApple OSS Distributions 
3*1b191cb5SApple OSS Distributions #include <errno.h>
4*1b191cb5SApple OSS Distributions #include <libproc.h>
5*1b191cb5SApple OSS Distributions #include <signal.h>
6*1b191cb5SApple OSS Distributions #include <spawn.h>
7*1b191cb5SApple OSS Distributions #include <spawn_private.h>
8*1b191cb5SApple OSS Distributions #include <stdbool.h>
9*1b191cb5SApple OSS Distributions #include <stdint.h>
10*1b191cb5SApple OSS Distributions #include <stdio.h>
11*1b191cb5SApple OSS Distributions #include <stdlib.h>
12*1b191cb5SApple OSS Distributions #include <string.h>
13*1b191cb5SApple OSS Distributions #include <sys/kauth.h>
14*1b191cb5SApple OSS Distributions #include <sys/proc_info.h>
15*1b191cb5SApple OSS Distributions #include <sys/spawn_internal.h>
16*1b191cb5SApple OSS Distributions #include <sys/sysctl.h>
17*1b191cb5SApple OSS Distributions #include <sysexits.h>
18*1b191cb5SApple OSS Distributions #include <unistd.h>
19*1b191cb5SApple OSS Distributions 
20*1b191cb5SApple OSS Distributions T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true));
21*1b191cb5SApple OSS Distributions 
22*1b191cb5SApple OSS Distributions T_DECL(posix_spawn_posix_cred, "Check posix_spawnattr for POSIX creds",
23*1b191cb5SApple OSS Distributions     T_META_ASROOT(true))
24*1b191cb5SApple OSS Distributions {
25*1b191cb5SApple OSS Distributions 	posix_spawnattr_t attr;
26*1b191cb5SApple OSS Distributions 	int ret;
27*1b191cb5SApple OSS Distributions 
28*1b191cb5SApple OSS Distributions 	ret = posix_spawnattr_init(&attr);
29*1b191cb5SApple OSS Distributions 	T_QUIET;
30*1b191cb5SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "posix_spawnattr_init");
31*1b191cb5SApple OSS Distributions 
32*1b191cb5SApple OSS Distributions 	ret = posix_spawnattr_setflags(&attr, POSIX_SPAWN_START_SUSPENDED);
33*1b191cb5SApple OSS Distributions 	T_QUIET;
34*1b191cb5SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "posix_spawnattr_setflags");
35*1b191cb5SApple OSS Distributions 
36*1b191cb5SApple OSS Distributions 	ret = posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSID);
37*1b191cb5SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "posix_spawnattr_setflags(POSIX_SPAWN_SETSID)");
38*1b191cb5SApple OSS Distributions 
39*1b191cb5SApple OSS Distributions 	ret = posix_spawnattr_set_uid_np(&attr, 502);
40*1b191cb5SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "posix_spawnattr_set_uid_np");
41*1b191cb5SApple OSS Distributions 
42*1b191cb5SApple OSS Distributions 	ret = posix_spawnattr_set_gid_np(&attr, 501);
43*1b191cb5SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "posix_spawnattr_set_gid_np");
44*1b191cb5SApple OSS Distributions 
45*1b191cb5SApple OSS Distributions 	gid_t groups[3] = { 501, 250, 299 };
46*1b191cb5SApple OSS Distributions 	ret = posix_spawnattr_set_groups_np(&attr, 3, &groups, KAUTH_UID_NONE);
47*1b191cb5SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "posix_spawnattr_set_groups_np");
48*1b191cb5SApple OSS Distributions 
49*1b191cb5SApple OSS Distributions 	ret = posix_spawnattr_set_login_np(&attr, "fake-name");
50*1b191cb5SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "posix_spawnattr_set_login_np");
51*1b191cb5SApple OSS Distributions 
52*1b191cb5SApple OSS Distributions 	char * const    prog = "/bin/sh";
53*1b191cb5SApple OSS Distributions 	char * const    argv_child[] = { prog,
54*1b191cb5SApple OSS Distributions 		                         "-c",
55*1b191cb5SApple OSS Distributions 		                         "test $(logname) = \"fake-name\" -a \"$(id -G)\" = \"501 250 299\"",
56*1b191cb5SApple OSS Distributions 		                         NULL, };
57*1b191cb5SApple OSS Distributions 	pid_t           child_pid;
58*1b191cb5SApple OSS Distributions 	extern char   **environ;
59*1b191cb5SApple OSS Distributions 
60*1b191cb5SApple OSS Distributions 	ret = posix_spawn(&child_pid, prog, NULL, &attr, argv_child, environ);
61*1b191cb5SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "posix_spawn");
62*1b191cb5SApple OSS Distributions 
63*1b191cb5SApple OSS Distributions 	T_LOG("parent: spawned child with pid %d\n", child_pid);
64*1b191cb5SApple OSS Distributions 
65*1b191cb5SApple OSS Distributions 	ret = posix_spawnattr_destroy(&attr);
66*1b191cb5SApple OSS Distributions 	T_QUIET;
67*1b191cb5SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "posix_spawnattr_destroy");
68*1b191cb5SApple OSS Distributions 
69*1b191cb5SApple OSS Distributions 	struct proc_bsdinfo info;
70*1b191cb5SApple OSS Distributions 
71*1b191cb5SApple OSS Distributions 	ret = proc_pidinfo(child_pid, PROC_PIDTBSDINFO, 1, &info, sizeof(info));
72*1b191cb5SApple OSS Distributions 	T_QUIET;
73*1b191cb5SApple OSS Distributions 	T_ASSERT_EQ(ret, (int)sizeof(info), "proc_pidinfo(PROC_PIDTBSDINFO)");
74*1b191cb5SApple OSS Distributions 
75*1b191cb5SApple OSS Distributions 	T_EXPECT_TRUE((bool)(info.pbi_flags & PROC_FLAG_SLEADER),
76*1b191cb5SApple OSS Distributions 	    "check setsid happened");
77*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ(info.pbi_uid, 502, "UID was set");
78*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ(info.pbi_gid, 501, "GID was set");
79*1b191cb5SApple OSS Distributions 
80*1b191cb5SApple OSS Distributions 	ret = kill(child_pid, SIGCONT);
81*1b191cb5SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "kill(signal)");
82*1b191cb5SApple OSS Distributions 
83*1b191cb5SApple OSS Distributions 	T_LOG("parent: waiting for child process\n");
84*1b191cb5SApple OSS Distributions 
85*1b191cb5SApple OSS Distributions 	int status = 0;
86*1b191cb5SApple OSS Distributions 	int waitpid_result = waitpid(child_pid, &status, 0);
87*1b191cb5SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(waitpid_result, "waitpid");
88*1b191cb5SApple OSS Distributions 	T_ASSERT_EQ(waitpid_result, child_pid, "waitpid should return child we spawned");
89*1b191cb5SApple OSS Distributions 	T_ASSERT_EQ(WIFEXITED(status), 1, "child should have exited normally");
90*1b191cb5SApple OSS Distributions 	T_ASSERT_EQ(WEXITSTATUS(status), EX_OK, "child should have exited with success");
91*1b191cb5SApple OSS Distributions }
92