1*a1e26a70SApple OSS Distributions /* 2*a1e26a70SApple OSS Distributions * Copyright (c) 2023 Apple Computer, Inc. All rights reserved. 3*a1e26a70SApple OSS Distributions * 4*a1e26a70SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ 5*a1e26a70SApple OSS Distributions * 6*a1e26a70SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code 7*a1e26a70SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License 8*a1e26a70SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in 9*a1e26a70SApple OSS Distributions * compliance with the License. The rights granted to you under the License 10*a1e26a70SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of, 11*a1e26a70SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to 12*a1e26a70SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any 13*a1e26a70SApple OSS Distributions * terms of an Apple operating system software license agreement. 14*a1e26a70SApple OSS Distributions * 15*a1e26a70SApple OSS Distributions * Please obtain a copy of the License at 16*a1e26a70SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file. 17*a1e26a70SApple OSS Distributions * 18*a1e26a70SApple OSS Distributions * The Original Code and all software distributed under the License are 19*a1e26a70SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 20*a1e26a70SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 21*a1e26a70SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 22*a1e26a70SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 23*a1e26a70SApple OSS Distributions * Please see the License for the specific language governing rights and 24*a1e26a70SApple OSS Distributions * limitations under the License. 25*a1e26a70SApple OSS Distributions * 26*a1e26a70SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 27*a1e26a70SApple OSS Distributions */ 28*a1e26a70SApple OSS Distributions 29*a1e26a70SApple OSS Distributions #include <darwintest.h> 30*a1e26a70SApple OSS Distributions 31*a1e26a70SApple OSS Distributions #include <fcntl.h> 32*a1e26a70SApple OSS Distributions #include <stdlib.h> 33*a1e26a70SApple OSS Distributions #include <spawn.h> 34*a1e26a70SApple OSS Distributions #include <string.h> 35*a1e26a70SApple OSS Distributions #include <unistd.h> 36*a1e26a70SApple OSS Distributions #include <sys/codesign.h> 37*a1e26a70SApple OSS Distributions #include <sys/mman.h> 38*a1e26a70SApple OSS Distributions #include <sys/stat.h> 39*a1e26a70SApple OSS Distributions #include <sys/wait.h> 40*a1e26a70SApple OSS Distributions 41*a1e26a70SApple OSS Distributions T_GLOBAL_META( 42*a1e26a70SApple OSS Distributions T_META_NAMESPACE("xnu.vm"), 43*a1e26a70SApple OSS Distributions T_META_RADAR_COMPONENT_NAME("xnu"), 44*a1e26a70SApple OSS Distributions T_META_RADAR_COMPONENT_VERSION("VM")); 45*a1e26a70SApple OSS Distributions 46*a1e26a70SApple OSS Distributions T_DECL(vm_tainted_executable, "Test that a tainted executable gets killed", 47*a1e26a70SApple OSS Distributions T_META_TAG_VM_PREFERRED, 48*a1e26a70SApple OSS Distributions T_META_IGNORECRASHES(".*hell0.*")) 49*a1e26a70SApple OSS Distributions { 50*a1e26a70SApple OSS Distributions char tmp_path[] = "/tmp/hell0-XXXXXX"; 51*a1e26a70SApple OSS Distributions int fd1, fd2; 52*a1e26a70SApple OSS Distributions struct stat fs; 53*a1e26a70SApple OSS Distributions char *mapaddr1; 54*a1e26a70SApple OSS Distributions size_t fsize; 55*a1e26a70SApple OSS Distributions char *big_sp, *big_cp, *big_ep, *little_cp; 56*a1e26a70SApple OSS Distributions size_t little_len; 57*a1e26a70SApple OSS Distributions char *child_argv[2]; 58*a1e26a70SApple OSS Distributions pid_t child_pid; 59*a1e26a70SApple OSS Distributions int child_status; 60*a1e26a70SApple OSS Distributions int cs_status; 61*a1e26a70SApple OSS Distributions 62*a1e26a70SApple OSS Distributions T_SETUPBEGIN; 63*a1e26a70SApple OSS Distributions /* copy "./hello" to "/tmp/hell0" */ 64*a1e26a70SApple OSS Distributions fd1 = open("./hello", O_RDONLY); 65*a1e26a70SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(fd1, "open(./hello)"); 66*a1e26a70SApple OSS Distributions fd2 = mkstemp(tmp_path); 67*a1e26a70SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(fd2, "mkstemp(%s)", tmp_path); 68*a1e26a70SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(fstat(fd1, &fs), NULL); 69*a1e26a70SApple OSS Distributions fsize = (size_t)fs.st_size; 70*a1e26a70SApple OSS Distributions mapaddr1 = mmap(NULL, fsize, PROT_READ, MAP_FILE | MAP_PRIVATE, fd1, 0); 71*a1e26a70SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(mapaddr1, NULL); 72*a1e26a70SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(write(fd2, mapaddr1, fsize), NULL); 73*a1e26a70SApple OSS Distributions /* change "hello, world!" to "hell0, world!" */ 74*a1e26a70SApple OSS Distributions big_sp = &mapaddr1[0]; /* start pointer in "big" byte string */ 75*a1e26a70SApple OSS Distributions big_ep = &mapaddr1[fsize]; /* end pointer in "big" byte string */ 76*a1e26a70SApple OSS Distributions little_cp = "hello, world!"; /* little byte string */ 77*a1e26a70SApple OSS Distributions little_len = strlen(little_cp); /* length of little byte string */ 78*a1e26a70SApple OSS Distributions big_cp = big_sp; /* start pointer in "big" byte string */ 79*a1e26a70SApple OSS Distributions for (;;) { 80*a1e26a70SApple OSS Distributions char zero = '0'; 81*a1e26a70SApple OSS Distributions big_cp = memmem(big_cp, (size_t)(big_ep - big_cp), 82*a1e26a70SApple OSS Distributions little_cp, little_len); 83*a1e26a70SApple OSS Distributions if (big_cp == NULL) { 84*a1e26a70SApple OSS Distributions break; 85*a1e26a70SApple OSS Distributions } 86*a1e26a70SApple OSS Distributions T_LOG("found string at offset 0x%llx", (off_t) (big_cp - big_sp)); 87*a1e26a70SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(pwrite(fd2, &zero, 1, 88*a1e26a70SApple OSS Distributions (big_cp - big_sp + 4)), NULL); 89*a1e26a70SApple OSS Distributions big_cp += little_len; 90*a1e26a70SApple OSS Distributions } 91*a1e26a70SApple OSS Distributions /* make the new binary "r-x" */ 92*a1e26a70SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(fchmod(fd2, S_IRUSR | S_IXUSR), NULL); 93*a1e26a70SApple OSS Distributions /* cleanup */ 94*a1e26a70SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(close(fd1), NULL); 95*a1e26a70SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(close(fd2), NULL); 96*a1e26a70SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(munmap(mapaddr1, fsize), NULL); 97*a1e26a70SApple OSS Distributions T_SETUPEND; 98*a1e26a70SApple OSS Distributions /* spawn the newly-tainted binary */ 99*a1e26a70SApple OSS Distributions T_LOG("launching '%s'", tmp_path); 100*a1e26a70SApple OSS Distributions child_argv[0] = tmp_path; 101*a1e26a70SApple OSS Distributions child_argv[1] = NULL; 102*a1e26a70SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(posix_spawn(&child_pid, tmp_path, NULL, NULL, child_argv, NULL), NULL); 103*a1e26a70SApple OSS Distributions /* check our code-signing policy, assuming the child has same policy */ 104*a1e26a70SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(csops(getpid(), CS_OPS_STATUS, &cs_status, sizeof(cs_status)), NULL); 105*a1e26a70SApple OSS Distributions T_LOG("parent %d cs status 0x%x CS_KILL:%s", getpid(), cs_status, 106*a1e26a70SApple OSS Distributions (cs_status & CS_KILL) ? "yes" : "no"); 107*a1e26a70SApple OSS Distributions /* get child's exit status */ 108*a1e26a70SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(waitpid(child_pid, &child_status, 0), NULL); 109*a1e26a70SApple OSS Distributions T_LOG("child %d exit status 0x%x", child_pid, child_status); 110*a1e26a70SApple OSS Distributions /* we no longer need our modified binary */ 111*a1e26a70SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(unlink(tmp_path), NULL); 112*a1e26a70SApple OSS Distributions if (cs_status & CS_KILL) { 113*a1e26a70SApple OSS Distributions /* check that child got SIGKILL */ 114*a1e26a70SApple OSS Distributions T_QUIET; T_ASSERT_TRUE(WIFSIGNALED(child_status), NULL); 115*a1e26a70SApple OSS Distributions T_QUIET; T_ASSERT_TRUE(WTERMSIG(child_status) == SIGKILL, NULL); 116*a1e26a70SApple OSS Distributions T_PASS("enforced process launched from modified binary got SIGKILL"); 117*a1e26a70SApple OSS Distributions } else { 118*a1e26a70SApple OSS Distributions /* check that child exited with 0 */ 119*a1e26a70SApple OSS Distributions T_QUIET; T_ASSERT_TRUE(WIFEXITED(child_status), NULL); 120*a1e26a70SApple OSS Distributions T_QUIET; T_ASSERT_TRUE(WEXITSTATUS(child_status) == 0, NULL); 121*a1e26a70SApple OSS Distributions T_PASS("non-enforced process launched from modified binary exited with 0"); 122*a1e26a70SApple OSS Distributions } 123*a1e26a70SApple OSS Distributions } 124