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