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