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