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