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