xref: /xnu-8792.41.9/tests/posix_spawn_filtering.c (revision 5c2921b07a2480ab43ec66f5b9e41cb872bc554f) !
1*5c2921b0SApple OSS Distributions #include <darwintest.h>
2*5c2921b0SApple OSS Distributions #include <darwintest_utils.h>
3*5c2921b0SApple OSS Distributions 
4*5c2921b0SApple OSS Distributions #include <errno.h>
5*5c2921b0SApple OSS Distributions #include <fcntl.h>
6*5c2921b0SApple OSS Distributions #include <signal.h>
7*5c2921b0SApple OSS Distributions #include <spawn.h>
8*5c2921b0SApple OSS Distributions #include <spawn_private.h>
9*5c2921b0SApple OSS Distributions #include <stdbool.h>
10*5c2921b0SApple OSS Distributions #include <stdint.h>
11*5c2921b0SApple OSS Distributions #include <stdio.h>
12*5c2921b0SApple OSS Distributions #include <stdlib.h>
13*5c2921b0SApple OSS Distributions #include <string.h>
14*5c2921b0SApple OSS Distributions #include <sys/spawn_internal.h>
15*5c2921b0SApple OSS Distributions #include <sys/stat.h>
16*5c2921b0SApple OSS Distributions #include <sys/sysctl.h>
17*5c2921b0SApple OSS Distributions #include <sys/syslimits.h>
18*5c2921b0SApple OSS Distributions #include <sysexits.h>
19*5c2921b0SApple OSS Distributions #include <unistd.h>
20*5c2921b0SApple OSS Distributions 
21*5c2921b0SApple OSS Distributions static char tmp_path_filter_rules[PATH_MAX] = "";
22*5c2921b0SApple OSS Distributions static char tmp_path_env_output[PATH_MAX] = "";
23*5c2921b0SApple OSS Distributions 
24*5c2921b0SApple OSS Distributions static void
cleanup_tmpfiles(void)25*5c2921b0SApple OSS Distributions cleanup_tmpfiles(void)
26*5c2921b0SApple OSS Distributions {
27*5c2921b0SApple OSS Distributions 	if (tmp_path_filter_rules[0] != '\0') {
28*5c2921b0SApple OSS Distributions 		unlink(tmp_path_filter_rules);
29*5c2921b0SApple OSS Distributions 	}
30*5c2921b0SApple OSS Distributions 	if (tmp_path_env_output[0] != '\0') {
31*5c2921b0SApple OSS Distributions 		unlink(tmp_path_env_output);
32*5c2921b0SApple OSS Distributions 	}
33*5c2921b0SApple OSS Distributions }
34*5c2921b0SApple OSS Distributions 
35*5c2921b0SApple OSS Distributions /*
36*5c2921b0SApple OSS Distributions  * Creates a filtering rules file that says "when launching sh, add this env
37*5c2921b0SApple OSS Distributions  * var". The we launch "sh -c env", redirect the output to a file, read the file
38*5c2921b0SApple OSS Distributions  * and check that the added env var is present.
39*5c2921b0SApple OSS Distributions  */
40*5c2921b0SApple OSS Distributions T_DECL(posix_spawn_filtering,
41*5c2921b0SApple OSS Distributions     "Check posix_spawn_filtering",
42*5c2921b0SApple OSS Distributions     T_META_ENVVAR("FEATUREFLAGS_ENABLED=Libsystem/posix_spawn_filtering"))
43*5c2921b0SApple OSS Distributions {
44*5c2921b0SApple OSS Distributions #if TARGET_OS_OSX
45*5c2921b0SApple OSS Distributions 	const char *tmpdir = dt_tmpdir();
46*5c2921b0SApple OSS Distributions 	T_LOG("tmpdir: %s\n", tmpdir);
47*5c2921b0SApple OSS Distributions 
48*5c2921b0SApple OSS Distributions 	strlcat(tmp_path_filter_rules, tmpdir ? tmpdir : "/tmp", sizeof(tmp_path_filter_rules));
49*5c2921b0SApple OSS Distributions 	strlcat(tmp_path_filter_rules, "/filter.rules.XXXXX", sizeof(tmp_path_filter_rules));
50*5c2921b0SApple OSS Distributions 	int filter_rules_fd = mkstemp(tmp_path_filter_rules);
51*5c2921b0SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(filter_rules_fd, "create temporary file 1");
52*5c2921b0SApple OSS Distributions 
53*5c2921b0SApple OSS Distributions 	const char *filter_rules_contents =
54*5c2921b0SApple OSS Distributions 	    "binary_name:sh\n"
55*5c2921b0SApple OSS Distributions 	    "add_env:ADDED_VAR=VIA_RULES\n";
56*5c2921b0SApple OSS Distributions 	ssize_t bytes_written = write(filter_rules_fd, filter_rules_contents, strlen(filter_rules_contents));
57*5c2921b0SApple OSS Distributions 	T_ASSERT_EQ(bytes_written, (long)strlen(filter_rules_contents), "write should write all contents");
58*5c2921b0SApple OSS Distributions 	close(filter_rules_fd);
59*5c2921b0SApple OSS Distributions 
60*5c2921b0SApple OSS Distributions 	strlcat(tmp_path_env_output, tmpdir ? tmpdir : "/tmp", sizeof(tmp_path_env_output));
61*5c2921b0SApple OSS Distributions 	strlcat(tmp_path_env_output, "/env.output.XXXXX", sizeof(tmp_path_env_output));
62*5c2921b0SApple OSS Distributions 	int env_output_fd = mkstemp(tmp_path_env_output);
63*5c2921b0SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(env_output_fd, "create temporary file 2");
64*5c2921b0SApple OSS Distributions 
65*5c2921b0SApple OSS Distributions 	T_ATEND(cleanup_tmpfiles);
66*5c2921b0SApple OSS Distributions 
67*5c2921b0SApple OSS Distributions 	char * const    prog = "/bin/sh";
68*5c2921b0SApple OSS Distributions 	char * const    argv_child[] = { prog,
69*5c2921b0SApple OSS Distributions 		                         "-c",
70*5c2921b0SApple OSS Distributions 		                         "/usr/bin/env",
71*5c2921b0SApple OSS Distributions 		                         NULL, };
72*5c2921b0SApple OSS Distributions 
73*5c2921b0SApple OSS Distributions 	char rules_path_env[PATH_MAX + 100] = {0};
74*5c2921b0SApple OSS Distributions 	sprintf(rules_path_env, "POSIX_SPAWN_FILTERING_RULES_PATH=%s", tmp_path_filter_rules);
75*5c2921b0SApple OSS Distributions 	char * const    envp_child[] = {
76*5c2921b0SApple OSS Distributions 		"HELLO=WORLD",
77*5c2921b0SApple OSS Distributions 		rules_path_env,
78*5c2921b0SApple OSS Distributions 		NULL,
79*5c2921b0SApple OSS Distributions 	};
80*5c2921b0SApple OSS Distributions 
81*5c2921b0SApple OSS Distributions 	pid_t           child_pid;
82*5c2921b0SApple OSS Distributions 
83*5c2921b0SApple OSS Distributions 	posix_spawn_file_actions_t      file_actions;
84*5c2921b0SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(posix_spawn_file_actions_init(&file_actions), "posix_spawn_file_actions_init");
85*5c2921b0SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(posix_spawn_file_actions_adddup2(&file_actions, env_output_fd, STDOUT_FILENO), "posix_spawn_file_actions_addup2");
86*5c2921b0SApple OSS Distributions 
87*5c2921b0SApple OSS Distributions 	int ret;
88*5c2921b0SApple OSS Distributions 	ret = posix_spawn(&child_pid, prog, &file_actions, NULL, argv_child, envp_child);
89*5c2921b0SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "posix_spawn");
90*5c2921b0SApple OSS Distributions 	T_LOG("parent: spawned child with pid %d, waiting for child to exit\n", child_pid);
91*5c2921b0SApple OSS Distributions 
92*5c2921b0SApple OSS Distributions 	ret = posix_spawn_file_actions_destroy(&file_actions);
93*5c2921b0SApple OSS Distributions 	T_QUIET;
94*5c2921b0SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "posix_spawn_file_actions_destroy");
95*5c2921b0SApple OSS Distributions 
96*5c2921b0SApple OSS Distributions 	int status = 0;
97*5c2921b0SApple OSS Distributions 	int waitpid_result = waitpid(child_pid, &status, 0);
98*5c2921b0SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(waitpid_result, "waitpid");
99*5c2921b0SApple OSS Distributions 	T_ASSERT_EQ(waitpid_result, child_pid, "waitpid should return child we spawned");
100*5c2921b0SApple OSS Distributions 	T_ASSERT_EQ(WIFEXITED(status), 1, "child should have exited normally");
101*5c2921b0SApple OSS Distributions 	T_ASSERT_EQ(WEXITSTATUS(status), EX_OK, "child should have exited with success");
102*5c2921b0SApple OSS Distributions 
103*5c2921b0SApple OSS Distributions 	T_ASSERT_EQ(lseek(env_output_fd, 0, SEEK_SET), 0ull, "lseek should succeed");
104*5c2921b0SApple OSS Distributions 	struct stat s;
105*5c2921b0SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(fstat(env_output_fd, &s), "fstat should succeed");
106*5c2921b0SApple OSS Distributions 	T_ASSERT_GT(s.st_size, 0ll, "s.st_size > 0");
107*5c2921b0SApple OSS Distributions 	char env_file_content[s.st_size + 1];
108*5c2921b0SApple OSS Distributions 	memset(env_file_content, 0, s.st_size + 1);
109*5c2921b0SApple OSS Distributions 	T_ASSERT_EQ((long)read(env_output_fd, env_file_content, (size_t)s.st_size), (long)s.st_size, "read should load the whole file");
110*5c2921b0SApple OSS Distributions 
111*5c2921b0SApple OSS Distributions 	T_ASSERT_NOTNULL(strstr(env_file_content, "HELLO=WORLD\n"), "original env var present");
112*5c2921b0SApple OSS Distributions 	T_ASSERT_NOTNULL(strstr(env_file_content, "ADDED_VAR=VIA_RULES\n"), "added env var present");
113*5c2921b0SApple OSS Distributions 
114*5c2921b0SApple OSS Distributions 	T_PASS("posix_spawn_filtering did succeed to set an env var");
115*5c2921b0SApple OSS Distributions 
116*5c2921b0SApple OSS Distributions #else // TARGET_OS_OSX
117*5c2921b0SApple OSS Distributions 	T_SKIP("posix_spawn_filtering only supported on macOS");
118*5c2921b0SApple OSS Distributions #endif // TARGET_OS_OSX
119*5c2921b0SApple OSS Distributions }
120