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