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