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