1 #include <errno.h>
2 #include <stdlib.h>
3 #include <libgen.h>
4 #include <limits.h>
5 #include <mach-o/dyld.h>
6 #include <sys/types.h>
7 #include <sys/sysctl.h>
8 #include <xlocale.h>
9
10 #include <darwintest.h>
11 #include <darwintest_utils.h>
12
13 #include "drop_priv.h"
14 #include "test_utils.h"
15
16 #if ENTITLED
17 #define SET_TREATMENT_ID set_treatment_id_entitled
18 #define SET_TREATMENT_ID_DESCR "Can set treatment id with entitlement"
19 #else /* ENTITLED */
20 #define SET_TREATMENT_ID set_treatment_id_unentitled
21 #define SET_TREATMENT_ID_DESCR "Can't set treatment id without entitlement"
22 #endif /* ENTITLED */
23
24 T_DECL(SET_TREATMENT_ID, "Verifies that EXPERIMENT sysctls can only be set with the entitlement", T_META_ASROOT(false))
25 {
26 #define TEST_STR "testing"
27 #define IDENTIFIER_LENGTH 36
28
29 int ret;
30 errno_t err;
31 char val[IDENTIFIER_LENGTH + 1] = {0};
32 size_t len = sizeof(val);
33 char new_val[IDENTIFIER_LENGTH + 1] = {0};
34
35 if (!is_development_kernel()) {
36 T_SKIP("skipping test on release kernel");
37 }
38
39 strlcpy(new_val, TEST_STR, sizeof(new_val));
40 if (running_as_root()) {
41 drop_priv();
42 }
43
44 ret = sysctlbyname("kern.trial_treatment_id", val, &len, new_val, strlen(new_val));
45 err = errno;
46 #if ENTITLED
47 len = sizeof(val);
48 memset(new_val, 0, sizeof(new_val));
49 T_ASSERT_POSIX_SUCCESS(ret, "set kern.trial_treatment_id");
50 /* Cleanup. Set it back to the empty string. */
51 ret = sysctlbyname("kern.trial_treatment_id", val, &len, new_val, 1);
52 T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "reset kern.trial_treatment_id");
53 #else
54 T_ASSERT_POSIX_FAILURE(ret, EPERM, "set kern.trial_treatment_id");
55 #endif /* ENTITLED */
56 }
57
58 #if ENTITLED
59 /* Check min and max value limits on numeric factors */
60 T_DECL(experiment_factor_numeric_limits,
61 "Can only set factors within the legal range.",
62 T_META_ASROOT(false))
63 {
64 #define kMinVal 5 /* The min value allowed for the testing factor. */
65 #define kMaxVal 10 /* The max value allowed for the testing factor. */
66 errno_t err;
67 int ret;
68 unsigned int current_val;
69 size_t len = sizeof(current_val);
70 unsigned int new_val;
71
72 if (running_as_root()) {
73 drop_priv();
74 }
75 new_val = kMinVal - 1;
76 ret = sysctlbyname("kern.testing_experiment_factor", ¤t_val, &len, &new_val, sizeof(new_val));
77 err = errno;
78 T_ASSERT_POSIX_FAILURE(ret, EINVAL, "set kern.testing_experiment_factor below range.");
79
80 new_val = kMaxVal + 1;
81 ret = sysctlbyname("kern.testing_experiment_factor", ¤t_val, &len, &new_val, sizeof(new_val));
82 err = errno;
83 T_ASSERT_POSIX_FAILURE(ret, EINVAL, "set kern.testing_experiment_factor above range.");
84
85 new_val = kMaxVal;
86 ret = sysctlbyname("kern.testing_experiment_factor", ¤t_val, &len, &new_val, sizeof(new_val));
87 T_ASSERT_POSIX_SUCCESS(ret, "set kern.testing_experiment_factor at top of range.");
88
89 new_val = kMinVal;
90 ret = sysctlbyname("kern.testing_experiment_factor", ¤t_val, &len, &new_val, sizeof(new_val));
91 T_ASSERT_POSIX_SUCCESS(ret, "set kern.testing_experiment_factor at bottom of range.");
92 }
93
94 static uint64_t original_libmalloc_experiment_value = 0;
95
96 static void
reset_libmalloc_experiment()97 reset_libmalloc_experiment()
98 {
99 int ret = sysctlbyname("kern.libmalloc_experiments", NULL, NULL, &original_libmalloc_experiment_value, sizeof(original_libmalloc_experiment_value));
100 T_ASSERT_POSIX_SUCCESS(ret, "reset kern.libmalloc_experiments");
101 }
102
103 static void
set_libmalloc_experiment(uint64_t val)104 set_libmalloc_experiment(uint64_t val)
105 {
106 T_LOG("Setting kern.libmalloc_experiments to %llu", val);
107 size_t len = sizeof(original_libmalloc_experiment_value);
108 int ret = sysctlbyname("kern.libmalloc_experiments", &original_libmalloc_experiment_value, &len, &val, sizeof(val));
109 T_ASSERT_POSIX_SUCCESS(ret, "set kern.libmalloc_experiments");
110 T_ATEND(reset_libmalloc_experiment);
111 }
112
113 #define PRINT_APPLE_ARRAY_TOOL "tools/print_apple_array"
114 /*
115 * Spawns a new binary and returns the contents of its apple array
116 * (after libsystem initialization).
117 */
118 static char **
get_apple_array(size_t * num_array_entries)119 get_apple_array(size_t *num_array_entries)
120 {
121 int ret;
122 char stdout_path[MAXPATHLEN] = "apple_array.txt";
123 dt_resultfile(stdout_path, MAXPATHLEN);
124 int exit_status = 0, signum = 0;
125 char binary_path[MAXPATHLEN], binary_dir[MAXPATHLEN];
126 char *char_ret;
127 const static size_t kMaxNumArguments = 256;
128 size_t linecap = 0;
129 ssize_t linelen = 0;
130 char **apple_array;
131 char **line = NULL;
132 size_t num_lines = 0;
133 FILE *stdout_f = NULL;
134 uint32_t name_size = MAXPATHLEN;
135
136 ret = _NSGetExecutablePath(binary_path, &name_size);
137 T_QUIET; T_ASSERT_EQ(ret, 0, "_NSGetExecutablePath");
138 char_ret = dirname_r(binary_path, binary_dir);
139 T_QUIET; T_ASSERT_TRUE(char_ret != NULL, "dirname_r");
140 snprintf(binary_path, MAXPATHLEN, "%s/%s", binary_dir, PRINT_APPLE_ARRAY_TOOL);
141
142 char *launch_tool_args[] = {
143 binary_path,
144 NULL
145 };
146 pid_t child_pid;
147 ret = dt_launch_tool(&child_pid, launch_tool_args, false, stdout_path, NULL);
148 T_WITH_ERRNO; T_ASSERT_EQ(ret, 0, "dt_launch_tool: %s", binary_path);
149
150 ret = dt_waitpid(child_pid, &exit_status, &signum, 60 * 5);
151 T_ASSERT_EQ(ret, 1, "dt_waitpid");
152 T_QUIET; T_ASSERT_EQ(exit_status, 0, "dt_waitpid: exit_status");
153 T_QUIET; T_ASSERT_EQ(signum, 0, "dt_waitpid: signum");
154
155 stdout_f = fopen(stdout_path, "r");
156 T_WITH_ERRNO; T_ASSERT_NOTNULL(stdout_f, "open(%s)", stdout_path);
157 apple_array = calloc(kMaxNumArguments, sizeof(char *));
158 T_QUIET; T_ASSERT_NOTNULL(apple_array, "calloc: %lu\n", sizeof(char *) * kMaxNumArguments);
159 while (num_lines < kMaxNumArguments) {
160 line = &(apple_array[num_lines++]);
161 linecap = 0;
162 linelen = getline(line, &linecap, stdout_f);
163 if (linelen == -1) {
164 break;
165 }
166 }
167 *num_array_entries = num_lines - 1;
168
169 ret = fclose(stdout_f);
170 T_ASSERT_POSIX_SUCCESS(ret, "fclose(%s)", stdout_path);
171
172 return apple_array;
173 }
174
175 #define LIBMALLOC_EXPERIMENT_FACTORS_KEY "MallocExperiment="
176
177 /*
178 * Get the value of the MallocExperiment key in the apple array.
179 * Returns true iff the key is present.
180 */
181 static bool
get_libmalloc_experiment_factors(char ** apple_array,size_t num_array_entries,uint64_t * factors)182 get_libmalloc_experiment_factors(char **apple_array, size_t num_array_entries, uint64_t *factors)
183 {
184 bool found = false;
185 for (size_t i = 0; i < num_array_entries; i++) {
186 char *str = apple_array[i];
187 if (strstr(str, LIBMALLOC_EXPERIMENT_FACTORS_KEY)) {
188 found = true;
189 if (factors != NULL) {
190 str = strchr(str, '=');
191 T_ASSERT_NOTNULL(str, "skip over =");
192 ++str;
193 *factors = strtoull_l(str, NULL, 16, NULL);
194 }
195 break;
196 }
197 }
198 return found;
199 }
200
201 T_DECL(libmalloc_experiment,
202 "libmalloc experiment flags show up in apple array if we're doing an experiment",
203 T_META_ASROOT(false))
204 {
205 uint64_t new_val, apple_array_val = 0;
206 size_t num_array_entries = 0;
207 char **apple_array;
208 bool found = false;
209
210 if (running_as_root()) {
211 drop_priv();
212 }
213 new_val = (1ULL << 63) - 1;
214 set_libmalloc_experiment(new_val);
215
216 apple_array = get_apple_array(&num_array_entries);
217 found = get_libmalloc_experiment_factors(apple_array, num_array_entries, &apple_array_val);
218 T_ASSERT_TRUE(found, "Found " LIBMALLOC_EXPERIMENT_FACTORS_KEY " in apple array");
219 T_ASSERT_EQ(apple_array_val, new_val, "Experiment value matches");
220 free(apple_array);
221 }
222
223 T_DECL(libmalloc_experiment_not_in_array,
224 "libmalloc experiment flags do not show up in apple array if we're not doing an experiment",
225 T_META_ASROOT(false))
226 {
227 size_t num_array_entries = 0;
228 char **apple_array;
229 bool found = false;
230
231 if (running_as_root()) {
232 drop_priv();
233 }
234 set_libmalloc_experiment(0);
235
236 apple_array = get_apple_array(&num_array_entries);
237 found = get_libmalloc_experiment_factors(apple_array, num_array_entries, NULL);
238 T_ASSERT_TRUE(!found, "Did not find " LIBMALLOC_EXPERIMENT_FACTORS_KEY " in apple array");
239 free(apple_array);
240 }
241 #endif /* ENTITLED */
242