1*1b191cb5SApple OSS Distributions #include <stdio.h> 2*1b191cb5SApple OSS Distributions #include <mach/mach_vm.h> 3*1b191cb5SApple OSS Distributions #include <mach/mach_port.h> 4*1b191cb5SApple OSS Distributions #include <mach/mach_error.h> 5*1b191cb5SApple OSS Distributions #include <sys/sysctl.h> 6*1b191cb5SApple OSS Distributions #include <sys/wait.h> 7*1b191cb5SApple OSS Distributions #include <unistd.h> 8*1b191cb5SApple OSS Distributions 9*1b191cb5SApple OSS Distributions #ifdef T_NAMESPACE 10*1b191cb5SApple OSS Distributions #undef T_NAMESPACE 11*1b191cb5SApple OSS Distributions #endif 12*1b191cb5SApple OSS Distributions 13*1b191cb5SApple OSS Distributions #include <darwintest.h> 14*1b191cb5SApple OSS Distributions #include <darwintest_utils.h> 15*1b191cb5SApple OSS Distributions 16*1b191cb5SApple OSS Distributions T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true), 17*1b191cb5SApple OSS Distributions T_META_NAMESPACE("xnu.ipc"), 18*1b191cb5SApple OSS Distributions T_META_RADAR_COMPONENT_NAME("xnu"), 19*1b191cb5SApple OSS Distributions T_META_RADAR_COMPONENT_VERSION("IPC")); 20*1b191cb5SApple OSS Distributions 21*1b191cb5SApple OSS Distributions T_DECL(test_task_filter_msg_flag, "Set the filter msg flag on the task and check if the forked child inherits it", 22*1b191cb5SApple OSS Distributions T_META_ASROOT(true), T_META_CHECK_LEAKS(false)) 23*1b191cb5SApple OSS Distributions { 24*1b191cb5SApple OSS Distributions int ret, dev; 25*1b191cb5SApple OSS Distributions size_t sysctl_size; 26*1b191cb5SApple OSS Distributions 27*1b191cb5SApple OSS Distributions T_SETUPBEGIN; 28*1b191cb5SApple OSS Distributions 29*1b191cb5SApple OSS Distributions dev = 0; 30*1b191cb5SApple OSS Distributions sysctl_size = sizeof(dev); 31*1b191cb5SApple OSS Distributions ret = sysctlbyname("kern.development", &dev, &sysctl_size, NULL, 0); 32*1b191cb5SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "sysctl kern.development failed"); 33*1b191cb5SApple OSS Distributions if (dev == 0) { 34*1b191cb5SApple OSS Distributions T_SKIP("Skipping test on release kernel"); 35*1b191cb5SApple OSS Distributions } 36*1b191cb5SApple OSS Distributions 37*1b191cb5SApple OSS Distributions T_SETUPEND; 38*1b191cb5SApple OSS Distributions 39*1b191cb5SApple OSS Distributions int cur_filter_flag = 0; 40*1b191cb5SApple OSS Distributions int new_filter_flag = 1; 41*1b191cb5SApple OSS Distributions ret = sysctlbyname("kern.task_set_filter_msg_flag", NULL, 0, &new_filter_flag, sizeof(new_filter_flag)); 42*1b191cb5SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "sysctlbyname"); 43*1b191cb5SApple OSS Distributions ret = sysctlbyname("kern.task_set_filter_msg_flag", &cur_filter_flag, &sysctl_size, NULL, 0); 44*1b191cb5SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "sysctlbyname"); 45*1b191cb5SApple OSS Distributions T_ASSERT_EQ(cur_filter_flag, 1, "Task should have filtering on"); 46*1b191cb5SApple OSS Distributions 47*1b191cb5SApple OSS Distributions pid_t pid = fork(); 48*1b191cb5SApple OSS Distributions if (pid == 0) { 49*1b191cb5SApple OSS Distributions cur_filter_flag = 0; 50*1b191cb5SApple OSS Distributions ret = sysctlbyname("kern.task_set_filter_msg_flag", &cur_filter_flag, &sysctl_size, NULL, 0); 51*1b191cb5SApple OSS Distributions if (ret == 0) { 52*1b191cb5SApple OSS Distributions if (cur_filter_flag == 1) { 53*1b191cb5SApple OSS Distributions exit(0); 54*1b191cb5SApple OSS Distributions } 55*1b191cb5SApple OSS Distributions } 56*1b191cb5SApple OSS Distributions exit(1); 57*1b191cb5SApple OSS Distributions } 58*1b191cb5SApple OSS Distributions 59*1b191cb5SApple OSS Distributions int status; 60*1b191cb5SApple OSS Distributions ret = waitpid(pid, &status, 0); 61*1b191cb5SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "waitpid"); 62*1b191cb5SApple OSS Distributions 63*1b191cb5SApple OSS Distributions if (WIFEXITED(status)) { 64*1b191cb5SApple OSS Distributions const int exit_code = WEXITSTATUS(status); 65*1b191cb5SApple OSS Distributions T_ASSERT_EQ(exit_code, 0, "Child inherited the filter msg flag"); 66*1b191cb5SApple OSS Distributions } 67*1b191cb5SApple OSS Distributions 68*1b191cb5SApple OSS Distributions /* Turn off task msg filtering */ 69*1b191cb5SApple OSS Distributions cur_filter_flag = 1; 70*1b191cb5SApple OSS Distributions new_filter_flag = 0; 71*1b191cb5SApple OSS Distributions ret = sysctlbyname("kern.task_set_filter_msg_flag", &cur_filter_flag, &sysctl_size, &new_filter_flag, sizeof(new_filter_flag)); 72*1b191cb5SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "sysctlbyname"); 73*1b191cb5SApple OSS Distributions T_ASSERT_EQ(cur_filter_flag, 1, "Task should have filtering on"); 74*1b191cb5SApple OSS Distributions 75*1b191cb5SApple OSS Distributions T_END; 76*1b191cb5SApple OSS Distributions } 77