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