xref: /xnu-8020.121.3/tests/test_sysctl_kern_procargs_25397314.m (revision fdd8201d7b966f0c3ea610489d29bd841d358941)
1*fdd8201dSApple OSS Distributions#include <Foundation/Foundation.h>
2*fdd8201dSApple OSS Distributions#include <darwintest.h>
3*fdd8201dSApple OSS Distributions#include <darwintest_utils.h>
4*fdd8201dSApple OSS Distributions#include <mach-o/dyld.h>
5*fdd8201dSApple OSS Distributions#include <System/sys/codesign.h>
6*fdd8201dSApple OSS Distributions#include <unistd.h>
7*fdd8201dSApple OSS Distributions#include <stdlib.h>
8*fdd8201dSApple OSS Distributions#include <signal.h>
9*fdd8201dSApple OSS Distributions#include <sys/types.h>
10*fdd8201dSApple OSS Distributions#include <sys/sysctl.h>
11*fdd8201dSApple OSS Distributions
12*fdd8201dSApple OSS Distributions
13*fdd8201dSApple OSS DistributionsT_GLOBAL_META(T_META_RUN_CONCURRENTLY(true),
14*fdd8201dSApple OSS Distributions    T_META_ASROOT(true));
15*fdd8201dSApple OSS Distributions
16*fdd8201dSApple OSS Distributionsstruct procargs {
17*fdd8201dSApple OSS Distributions	int argc;
18*fdd8201dSApple OSS Distributions	size_t preflightSize;
19*fdd8201dSApple OSS Distributions	NSString *executablePath;
20*fdd8201dSApple OSS Distributions	NSArray *components;
21*fdd8201dSApple OSS Distributions	NSString *legacyExecutablePath;
22*fdd8201dSApple OSS Distributions	void *rawBuffer;
23*fdd8201dSApple OSS Distributions	size_t rawBufferSize;
24*fdd8201dSApple OSS Distributions};
25*fdd8201dSApple OSS Distributions
26*fdd8201dSApple OSS Distributionsstatic void printHexDump(void* buffer, size_t size);
27*fdd8201dSApple OSS Distributions
28*fdd8201dSApple OSS Distributionstypedef struct procargs *procargs_t;
29*fdd8201dSApple OSS Distributions
30*fdd8201dSApple OSS Distributions#define TEST_ENVIRONMENT_VARIABLE       "TESTENVVARIABLE"
31*fdd8201dSApple OSS Distributions#define TEST_ENVIRONMENT_VARIABLE_VALUE "TESTENVVARIABLE_VALUE"
32*fdd8201dSApple OSS Distributions
33*fdd8201dSApple OSS Distributions
34*fdd8201dSApple OSS Distributionsstatic size_t argmax;
35*fdd8201dSApple OSS Distributions
36*fdd8201dSApple OSS Distributionsstatic procargs_t getProcArgs(int type, pid_t pid, size_t allocSize)
37*fdd8201dSApple OSS Distributions{
38*fdd8201dSApple OSS Distributions	int sysctlArgs[3] = {CTL_KERN, type, pid};
39*fdd8201dSApple OSS Distributions	int argc;
40*fdd8201dSApple OSS Distributions	NSMutableArray *components = [NSMutableArray array];
41*fdd8201dSApple OSS Distributions	procargs_t args = (procargs_t) malloc(sizeof(struct procargs));
42*fdd8201dSApple OSS Distributions	size_t currentLen = 0;
43*fdd8201dSApple OSS Distributions	bool legacyPathPresent = false;
44*fdd8201dSApple OSS Distributions	NSString *current = nil;
45*fdd8201dSApple OSS Distributions	NSString *legacyExecutablePath = nil;
46*fdd8201dSApple OSS Distributions	NSString *executablePath = nil;
47*fdd8201dSApple OSS Distributions	size_t bufferSize;
48*fdd8201dSApple OSS Distributions	size_t preflightSize = 0;
49*fdd8201dSApple OSS Distributions	const char *name = type == KERN_PROCARGS ? "KERN_PROCARGS" : "KERN_PROCARGS2";
50*fdd8201dSApple OSS Distributions	const char *cursor;
51*fdd8201dSApple OSS Distributions	void *buffer;
52*fdd8201dSApple OSS Distributions
53*fdd8201dSApple OSS Distributions	T_LOG("Get proc args for pid %d, allocSize %lu with %s", pid, allocSize, name);
54*fdd8201dSApple OSS Distributions
55*fdd8201dSApple OSS Distributions
56*fdd8201dSApple OSS Distributions	T_ASSERT_TRUE(type == KERN_PROCARGS || type == KERN_PROCARGS2, "type is valid");
57*fdd8201dSApple OSS Distributions
58*fdd8201dSApple OSS Distributions	/* Determine how much memory to allocate. If allocSize is 0 we will use the size
59*fdd8201dSApple OSS Distributions	 * we get from the sysctl for our buffer. */
60*fdd8201dSApple OSS Distributions	T_ASSERT_POSIX_SUCCESS(sysctl(sysctlArgs, 3, NULL, &preflightSize, NULL, 0), "sysctl %s", name);
61*fdd8201dSApple OSS Distributions	T_LOG("procargs data should be %lu bytes", preflightSize);
62*fdd8201dSApple OSS Distributions
63*fdd8201dSApple OSS Distributions	if (allocSize == 0) {
64*fdd8201dSApple OSS Distributions		allocSize = preflightSize;
65*fdd8201dSApple OSS Distributions	}
66*fdd8201dSApple OSS Distributions
67*fdd8201dSApple OSS Distributions	buffer = malloc(allocSize);
68*fdd8201dSApple OSS Distributions	T_QUIET; T_ASSERT_NOTNULL(buffer, "malloc buffer of size %lu", allocSize);
69*fdd8201dSApple OSS Distributions	bufferSize = allocSize;
70*fdd8201dSApple OSS Distributions
71*fdd8201dSApple OSS Distributions	T_ASSERT_POSIX_SUCCESS(sysctl(sysctlArgs, 3, buffer, &bufferSize, NULL, 0), "sysctl %s", name);
72*fdd8201dSApple OSS Distributions	T_ASSERT_LE(bufferSize, allocSize, "returned buffer size should be less than allocated size");
73*fdd8201dSApple OSS Distributions	T_LOG("sysctl wrote %lu bytes", bufferSize);
74*fdd8201dSApple OSS Distributions	if (allocSize >= bufferSize) {
75*fdd8201dSApple OSS Distributions		/* Allocated buffer is larger than what kernel wrote, so it should match preflightSize */
76*fdd8201dSApple OSS Distributions		T_ASSERT_EQ(bufferSize, preflightSize, "buffer size should be the same as preflight size");
77*fdd8201dSApple OSS Distributions	}
78*fdd8201dSApple OSS Distributions
79*fdd8201dSApple OSS Distributions	printHexDump(buffer, bufferSize);
80*fdd8201dSApple OSS Distributions
81*fdd8201dSApple OSS Distributions	if (type == KERN_PROCARGS2) {
82*fdd8201dSApple OSS Distributions		argc = *(int *)buffer;
83*fdd8201dSApple OSS Distributions		cursor = (const char *)buffer + sizeof(int);
84*fdd8201dSApple OSS Distributions	} else {
85*fdd8201dSApple OSS Distributions		/* Without KERN_PROCARGS2, we can't tell where argv ends and environ begins.
86*fdd8201dSApple OSS Distributions		 * Set argc to -1 to indicate this */
87*fdd8201dSApple OSS Distributions		argc = -1;
88*fdd8201dSApple OSS Distributions		cursor = buffer;
89*fdd8201dSApple OSS Distributions	}
90*fdd8201dSApple OSS Distributions
91*fdd8201dSApple OSS Distributions	while ((uintptr_t)cursor < (uintptr_t)buffer + bufferSize) {
92*fdd8201dSApple OSS Distributions		/* Ensure alignment and check if the uint16_t at cursor is the magic value */
93*fdd8201dSApple OSS Distributions		if (!((uintptr_t)cursor & (sizeof(uint16_t) - 1)) &&
94*fdd8201dSApple OSS Distributions			(uintptr_t)buffer + bufferSize - (uintptr_t)cursor > sizeof(uint16_t)) {
95*fdd8201dSApple OSS Distributions			/* Silence -Wcast-align by casting to const void * */
96*fdd8201dSApple OSS Distributions			uint16_t value = *(const uint16_t *)(const void *)cursor;
97*fdd8201dSApple OSS Distributions			if (value == 0xBFFF) {
98*fdd8201dSApple OSS Distributions				/* Magic value that specifies the end of the argument/environ section */
99*fdd8201dSApple OSS Distributions				cursor += sizeof(uint16_t) + sizeof(uint32_t);
100*fdd8201dSApple OSS Distributions				legacyPathPresent = true;
101*fdd8201dSApple OSS Distributions				break;
102*fdd8201dSApple OSS Distributions			}
103*fdd8201dSApple OSS Distributions		}
104*fdd8201dSApple OSS Distributions		currentLen = strnlen(cursor, bufferSize - ((uintptr_t)cursor - (uintptr_t)buffer));
105*fdd8201dSApple OSS Distributions		current = [[NSString alloc] initWithBytes:cursor length:currentLen encoding:NSUTF8StringEncoding];
106*fdd8201dSApple OSS Distributions		T_QUIET; T_ASSERT_NOTNULL(current, "allocated string");
107*fdd8201dSApple OSS Distributions		cursor += currentLen + 1;
108*fdd8201dSApple OSS Distributions
109*fdd8201dSApple OSS Distributions		if (executablePath == nil) {
110*fdd8201dSApple OSS Distributions			executablePath = current;
111*fdd8201dSApple OSS Distributions			[executablePath retain];
112*fdd8201dSApple OSS Distributions			while (*cursor == 0) {
113*fdd8201dSApple OSS Distributions				cursor++;
114*fdd8201dSApple OSS Distributions			}
115*fdd8201dSApple OSS Distributions		} else {
116*fdd8201dSApple OSS Distributions			[components addObject:current];
117*fdd8201dSApple OSS Distributions		}
118*fdd8201dSApple OSS Distributions		[current release];
119*fdd8201dSApple OSS Distributions	}
120*fdd8201dSApple OSS Distributions	if (legacyPathPresent) {
121*fdd8201dSApple OSS Distributions		T_ASSERT_EQ(type, KERN_PROCARGS, "Legacy executable path should only be present for KERN_PROCARGS");
122*fdd8201dSApple OSS Distributions		currentLen = strnlen(cursor, bufferSize - ((uintptr_t)cursor - (uintptr_t)buffer));
123*fdd8201dSApple OSS Distributions		current = [[NSString alloc] initWithBytes:cursor length:currentLen encoding:NSUTF8StringEncoding];
124*fdd8201dSApple OSS Distributions		T_QUIET; T_ASSERT_NOTNULL(current, "allocated string");
125*fdd8201dSApple OSS Distributions		legacyExecutablePath = current;
126*fdd8201dSApple OSS Distributions	}
127*fdd8201dSApple OSS Distributions	args->argc = argc;
128*fdd8201dSApple OSS Distributions	args->executablePath = executablePath;
129*fdd8201dSApple OSS Distributions	args->components = components;
130*fdd8201dSApple OSS Distributions	args->legacyExecutablePath = legacyExecutablePath;
131*fdd8201dSApple OSS Distributions	args->preflightSize = preflightSize;
132*fdd8201dSApple OSS Distributions	args->rawBuffer = buffer;
133*fdd8201dSApple OSS Distributions	args->rawBufferSize = bufferSize;
134*fdd8201dSApple OSS Distributions	return args;
135*fdd8201dSApple OSS Distributions}
136*fdd8201dSApple OSS Distributions
137*fdd8201dSApple OSS Distributionsstatic void printProcArgs(procargs_t procargs) {
138*fdd8201dSApple OSS Distributions	if (procargs->argc == -1) {
139*fdd8201dSApple OSS Distributions		T_LOG("No argument count");
140*fdd8201dSApple OSS Distributions	} else {
141*fdd8201dSApple OSS Distributions		T_LOG("Argc is %d", procargs->argc);
142*fdd8201dSApple OSS Distributions	}
143*fdd8201dSApple OSS Distributions	T_LOG("Executable path: %s (length %lu)", [procargs->executablePath UTF8String], [procargs->executablePath length]);
144*fdd8201dSApple OSS Distributions	for (size_t i = 0; i < [procargs->components count]; i++) {
145*fdd8201dSApple OSS Distributions		NSString *component = [procargs->components objectAtIndex:i];
146*fdd8201dSApple OSS Distributions		const char *str = [component UTF8String];
147*fdd8201dSApple OSS Distributions		size_t len = [component length];
148*fdd8201dSApple OSS Distributions		if (procargs->argc != -1) {
149*fdd8201dSApple OSS Distributions			T_LOG("%s %zu: %s (length %lu)", i >= (size_t)procargs->argc ? "Env var" : "Argument", i, str, len);
150*fdd8201dSApple OSS Distributions		} else {
151*fdd8201dSApple OSS Distributions			T_LOG("Component %zu: %s (length %lu)", i, str, len);
152*fdd8201dSApple OSS Distributions		}
153*fdd8201dSApple OSS Distributions	}
154*fdd8201dSApple OSS Distributions	if (procargs->legacyExecutablePath) {
155*fdd8201dSApple OSS Distributions		T_LOG("Contains legacy executable path: %s (length %lu)", [procargs->legacyExecutablePath UTF8String], [procargs->legacyExecutablePath length]);
156*fdd8201dSApple OSS Distributions	}
157*fdd8201dSApple OSS Distributions	printHexDump(procargs->rawBuffer, procargs->rawBufferSize);
158*fdd8201dSApple OSS Distributions}
159*fdd8201dSApple OSS Distributions
160*fdd8201dSApple OSS Distributionsstatic void printHexDump(void* buffer, size_t size) {
161*fdd8201dSApple OSS Distributions	#define ROW_LENGTH 24
162*fdd8201dSApple OSS Distributions	T_LOG("Buffer %p, size %zu", buffer, size);
163*fdd8201dSApple OSS Distributions	for (size_t row = 0; row < size; row += ROW_LENGTH) {
164*fdd8201dSApple OSS Distributions		NSMutableString *line = [[NSMutableString alloc] initWithCapacity:0];
165*fdd8201dSApple OSS Distributions		NSMutableString *text = [[NSMutableString alloc] initWithCapacity:0];
166*fdd8201dSApple OSS Distributions		[line appendFormat:@"    %04zx    ", row];
167*fdd8201dSApple OSS Distributions		for (size_t col = row; col < row + ROW_LENGTH; col++) {
168*fdd8201dSApple OSS Distributions			if (col < size) {
169*fdd8201dSApple OSS Distributions				char c = ((char *)buffer)[col];
170*fdd8201dSApple OSS Distributions				[line appendFormat:@"%02x ", c];
171*fdd8201dSApple OSS Distributions				if (isprint(c)) {
172*fdd8201dSApple OSS Distributions					[text appendFormat:@"%c", c];
173*fdd8201dSApple OSS Distributions				} else {
174*fdd8201dSApple OSS Distributions					[text appendString:@"."];
175*fdd8201dSApple OSS Distributions				}
176*fdd8201dSApple OSS Distributions			} else {
177*fdd8201dSApple OSS Distributions				[line appendString:@"   "];
178*fdd8201dSApple OSS Distributions			}
179*fdd8201dSApple OSS Distributions		}
180*fdd8201dSApple OSS Distributions		[line appendFormat:@"  %@", text];
181*fdd8201dSApple OSS Distributions		T_LOG("%s", [line UTF8String]);
182*fdd8201dSApple OSS Distributions		[text release];
183*fdd8201dSApple OSS Distributions		[line release];
184*fdd8201dSApple OSS Distributions	}
185*fdd8201dSApple OSS Distributions}
186*fdd8201dSApple OSS Distributions
187*fdd8201dSApple OSS Distributionsstatic void deallocProcArgs(procargs_t procargs)
188*fdd8201dSApple OSS Distributions{
189*fdd8201dSApple OSS Distributions	[procargs->components release];
190*fdd8201dSApple OSS Distributions	[procargs->executablePath release];
191*fdd8201dSApple OSS Distributions	[procargs->legacyExecutablePath release];
192*fdd8201dSApple OSS Distributions	free(procargs->rawBuffer);
193*fdd8201dSApple OSS Distributions	free(procargs);
194*fdd8201dSApple OSS Distributions}
195*fdd8201dSApple OSS Distributions
196*fdd8201dSApple OSS DistributionsT_HELPER_DECL(child_helper, "Child process helper")
197*fdd8201dSApple OSS Distributions{
198*fdd8201dSApple OSS Distributions	while (true) {
199*fdd8201dSApple OSS Distributions		wait(NULL);
200*fdd8201dSApple OSS Distributions	}
201*fdd8201dSApple OSS Distributions}
202*fdd8201dSApple OSS Distributions
203*fdd8201dSApple OSS Distributionsstatic pid_t
204*fdd8201dSApple OSS Distributionslaunch_child_process(NSArray *args, bool cs_restrict)
205*fdd8201dSApple OSS Distributions{
206*fdd8201dSApple OSS Distributions	pid_t pid;
207*fdd8201dSApple OSS Distributions	char path[PATH_MAX];
208*fdd8201dSApple OSS Distributions	uint32_t path_size = sizeof(path);
209*fdd8201dSApple OSS Distributions	uint32_t csopsStatus = 0;
210*fdd8201dSApple OSS Distributions	const char** dt_args;
211*fdd8201dSApple OSS Distributions	size_t dt_args_count;
212*fdd8201dSApple OSS Distributions
213*fdd8201dSApple OSS Distributions	T_ASSERT_POSIX_SUCCESS(_NSGetExecutablePath(path, &path_size), "get executable path");
214*fdd8201dSApple OSS Distributions
215*fdd8201dSApple OSS Distributions	/* We need to add 4 arguments to the beginning and NULL at the end */
216*fdd8201dSApple OSS Distributions	dt_args_count = [args count] + 5;
217*fdd8201dSApple OSS Distributions	dt_args = malloc(sizeof(char *) * dt_args_count);
218*fdd8201dSApple OSS Distributions	dt_args[0] = path;
219*fdd8201dSApple OSS Distributions	dt_args[1] = "-n";
220*fdd8201dSApple OSS Distributions	dt_args[2] = "child_helper";
221*fdd8201dSApple OSS Distributions	dt_args[3] = "--";
222*fdd8201dSApple OSS Distributions	for (size_t i = 0; i < [args count]; i++) {
223*fdd8201dSApple OSS Distributions		NSString *arg = [args objectAtIndex:i];
224*fdd8201dSApple OSS Distributions		dt_args[i + 4] = [arg UTF8String];
225*fdd8201dSApple OSS Distributions	}
226*fdd8201dSApple OSS Distributions	dt_args[[args count] + 4] = NULL;
227*fdd8201dSApple OSS Distributions
228*fdd8201dSApple OSS Distributions	T_LOG("Launching %s", path);
229*fdd8201dSApple OSS Distributions	T_LOG("Arguments: ");
230*fdd8201dSApple OSS Distributions	for (size_t i = 0; i < dt_args_count; i++) {
231*fdd8201dSApple OSS Distributions		T_LOG("     %s", dt_args[i] ? dt_args[i] : "(null)");
232*fdd8201dSApple OSS Distributions	}
233*fdd8201dSApple OSS Distributions	T_ASSERT_POSIX_SUCCESS(dt_launch_tool(&pid, (char **)dt_args, false, NULL, NULL), "launched helper");
234*fdd8201dSApple OSS Distributions	free(dt_args);
235*fdd8201dSApple OSS Distributions
236*fdd8201dSApple OSS Distributions	if (cs_restrict) {
237*fdd8201dSApple OSS Distributions		csopsStatus |= CS_RESTRICT;
238*fdd8201dSApple OSS Distributions		T_ASSERT_POSIX_SUCCESS(csops(pid, CS_OPS_SET_STATUS, &csopsStatus, sizeof(csopsStatus)), "set CS_RESTRICT");
239*fdd8201dSApple OSS Distributions	}
240*fdd8201dSApple OSS Distributions	return pid;
241*fdd8201dSApple OSS Distributions}
242*fdd8201dSApple OSS Distributions
243*fdd8201dSApple OSS DistributionsT_DECL(test_sysctl_kern_procargs_25397314, "Test kern.procargs and kern.procargs2 sysctls")
244*fdd8201dSApple OSS Distributions{
245*fdd8201dSApple OSS Distributions	procargs_t procargs;
246*fdd8201dSApple OSS Distributions	size_t argsize = sizeof(argmax);
247*fdd8201dSApple OSS Distributions	NSString *testArgument1 = @"test argument 1";
248*fdd8201dSApple OSS Distributions	bool containsTestArgument1 = false;
249*fdd8201dSApple OSS Distributions	NSString *testArgument2 = @"test argument 2";
250*fdd8201dSApple OSS Distributions	bool containsTestArgument2 = false;
251*fdd8201dSApple OSS Distributions	NSString *testEnvironmentVariable = @TEST_ENVIRONMENT_VARIABLE;
252*fdd8201dSApple OSS Distributions	bool containsTestEnvironmentVariable = false;
253*fdd8201dSApple OSS Distributions	bool containsPathEnvironmentVariable = false;
254*fdd8201dSApple OSS Distributions	int development = 0;
255*fdd8201dSApple OSS Distributions	size_t development_size = sizeof(development);
256*fdd8201dSApple OSS Distributions	uint32_t csopsStatus = 0;
257*fdd8201dSApple OSS Distributions
258*fdd8201dSApple OSS Distributions
259*fdd8201dSApple OSS Distributions	T_ASSERT_POSIX_SUCCESS(sysctlbyname("kern.development", &development, &development_size, NULL, 0), "sysctl kern.development");
260*fdd8201dSApple OSS Distributions
261*fdd8201dSApple OSS Distributions	T_ASSERT_POSIX_SUCCESS(sysctlbyname("kern.argmax", &argmax, &argsize, NULL, 0), "sysctl kern.argmax");
262*fdd8201dSApple OSS Distributions	procargs = getProcArgs(KERN_PROCARGS2, getpid(), argmax);
263*fdd8201dSApple OSS Distributions	T_ASSERT_NOTNULL(procargs->executablePath, "executable path should be non-null");
264*fdd8201dSApple OSS Distributions	T_ASSERT_GT([procargs->executablePath length], 0, "executable path should not be empty");
265*fdd8201dSApple OSS Distributions	printProcArgs(procargs);
266*fdd8201dSApple OSS Distributions	deallocProcArgs(procargs);
267*fdd8201dSApple OSS Distributions
268*fdd8201dSApple OSS Distributions	procargs = getProcArgs(KERN_PROCARGS2, getpid(), 0);
269*fdd8201dSApple OSS Distributions	T_ASSERT_NOTNULL(procargs->executablePath, "executable path should be non-null");
270*fdd8201dSApple OSS Distributions	T_ASSERT_GT([procargs->executablePath length], 0, "executable path should not be empty");
271*fdd8201dSApple OSS Distributions	printProcArgs(procargs);
272*fdd8201dSApple OSS Distributions	deallocProcArgs(procargs);
273*fdd8201dSApple OSS Distributions
274*fdd8201dSApple OSS Distributions	setenv(TEST_ENVIRONMENT_VARIABLE, TEST_ENVIRONMENT_VARIABLE_VALUE, true);
275*fdd8201dSApple OSS Distributions
276*fdd8201dSApple OSS Distributions	pid_t child = launch_child_process(@[testArgument1, testArgument2], false);
277*fdd8201dSApple OSS Distributions	procargs = getProcArgs(KERN_PROCARGS2, child, argmax);
278*fdd8201dSApple OSS Distributions	T_ASSERT_NOTNULL(procargs->executablePath, "executable path should be non-null");
279*fdd8201dSApple OSS Distributions	T_ASSERT_GT([procargs->executablePath length], 0, "executable path should not be empty");
280*fdd8201dSApple OSS Distributions	printProcArgs(procargs);
281*fdd8201dSApple OSS Distributions
282*fdd8201dSApple OSS Distributions	for (NSString *component in procargs->components) {
283*fdd8201dSApple OSS Distributions		if ([component isEqualToString:testArgument1]) {
284*fdd8201dSApple OSS Distributions			containsTestArgument1 = true;
285*fdd8201dSApple OSS Distributions		}
286*fdd8201dSApple OSS Distributions		if ([component isEqualToString:testArgument2]) {
287*fdd8201dSApple OSS Distributions			containsTestArgument2 = true;
288*fdd8201dSApple OSS Distributions		}
289*fdd8201dSApple OSS Distributions		if ([component containsString:testEnvironmentVariable]) {
290*fdd8201dSApple OSS Distributions			containsTestEnvironmentVariable = true;
291*fdd8201dSApple OSS Distributions		}
292*fdd8201dSApple OSS Distributions	}
293*fdd8201dSApple OSS Distributions	deallocProcArgs(procargs);
294*fdd8201dSApple OSS Distributions	kill(child, SIGKILL);
295*fdd8201dSApple OSS Distributions	T_ASSERT_TRUE(containsTestArgument1, "Found test argument 1");
296*fdd8201dSApple OSS Distributions	T_ASSERT_TRUE(containsTestArgument2, "Found test argument 2");
297*fdd8201dSApple OSS Distributions	T_ASSERT_TRUE(containsTestEnvironmentVariable, "Found test environment variable");
298*fdd8201dSApple OSS Distributions
299*fdd8201dSApple OSS Distributions	if (development) {
300*fdd8201dSApple OSS Distributions		T_LOG("Skipping test on DEVELOPMENT || DEBUG kernel");
301*fdd8201dSApple OSS Distributions	} else {
302*fdd8201dSApple OSS Distributions		containsTestArgument1 = false;
303*fdd8201dSApple OSS Distributions		containsTestArgument2 = false;
304*fdd8201dSApple OSS Distributions		containsTestEnvironmentVariable = false;
305*fdd8201dSApple OSS Distributions
306*fdd8201dSApple OSS Distributions		child = launch_child_process(@[testArgument1, testArgument2], true);
307*fdd8201dSApple OSS Distributions		procargs = getProcArgs(KERN_PROCARGS2, child, argmax);
308*fdd8201dSApple OSS Distributions		T_ASSERT_NOTNULL(procargs->executablePath, "executable path should be non-null");
309*fdd8201dSApple OSS Distributions		T_ASSERT_GT([procargs->executablePath length], 0, "executable path should not be empty");
310*fdd8201dSApple OSS Distributions		printProcArgs(procargs);
311*fdd8201dSApple OSS Distributions		for (NSString *component in procargs->components) {
312*fdd8201dSApple OSS Distributions			if ([component isEqualToString:testArgument1]) {
313*fdd8201dSApple OSS Distributions				containsTestArgument1 = true;
314*fdd8201dSApple OSS Distributions			}
315*fdd8201dSApple OSS Distributions			if ([component isEqualToString:testArgument2]) {
316*fdd8201dSApple OSS Distributions				containsTestArgument2 = true;
317*fdd8201dSApple OSS Distributions			}
318*fdd8201dSApple OSS Distributions			if ([component containsString:testEnvironmentVariable]) {
319*fdd8201dSApple OSS Distributions				containsTestEnvironmentVariable = true;
320*fdd8201dSApple OSS Distributions			}
321*fdd8201dSApple OSS Distributions		}
322*fdd8201dSApple OSS Distributions		deallocProcArgs(procargs);
323*fdd8201dSApple OSS Distributions		kill(child, SIGKILL);
324*fdd8201dSApple OSS Distributions		T_ASSERT_TRUE(containsTestArgument1, "Found test argument 1");
325*fdd8201dSApple OSS Distributions		T_ASSERT_TRUE(containsTestArgument2, "Found test argument 2");
326*fdd8201dSApple OSS Distributions		T_ASSERT_FALSE(containsTestEnvironmentVariable, "No test environment variable");
327*fdd8201dSApple OSS Distributions
328*fdd8201dSApple OSS Distributions
329*fdd8201dSApple OSS Distributions		csopsStatus |= CS_RESTRICT;
330*fdd8201dSApple OSS Distributions		T_ASSERT_POSIX_SUCCESS(csops(getpid(), CS_OPS_SET_STATUS, &csopsStatus, sizeof(csopsStatus)), "set CS_RESTRICT on self");
331*fdd8201dSApple OSS Distributions		procargs = getProcArgs(KERN_PROCARGS2, getpid(), argmax);
332*fdd8201dSApple OSS Distributions		T_ASSERT_NOTNULL(procargs->executablePath, "executable path should be non-null");
333*fdd8201dSApple OSS Distributions		T_ASSERT_GT([procargs->executablePath length], 0, "executable path should not be empty");
334*fdd8201dSApple OSS Distributions		printProcArgs(procargs);
335*fdd8201dSApple OSS Distributions		for (NSString *component in procargs->components) {
336*fdd8201dSApple OSS Distributions			if ([component containsString:@"PATH"]) {
337*fdd8201dSApple OSS Distributions				containsPathEnvironmentVariable = true;
338*fdd8201dSApple OSS Distributions			}
339*fdd8201dSApple OSS Distributions		}
340*fdd8201dSApple OSS Distributions		deallocProcArgs(procargs);
341*fdd8201dSApple OSS Distributions		T_ASSERT_TRUE(containsPathEnvironmentVariable, "Found $PATH environment variable");
342*fdd8201dSApple OSS Distributions	}
343*fdd8201dSApple OSS Distributions}
344