1*c54f35caSApple OSS Distributions #include <stdio.h> 2*c54f35caSApple OSS Distributions #include <errno.h> 3*c54f35caSApple OSS Distributions #include <string.h> 4*c54f35caSApple OSS Distributions #include <unistd.h> 5*c54f35caSApple OSS Distributions #include <sys/mman.h> 6*c54f35caSApple OSS Distributions #include <mach/clock_types.h> 7*c54f35caSApple OSS Distributions #include <sys/timex.h> 8*c54f35caSApple OSS Distributions #include <spawn.h> 9*c54f35caSApple OSS Distributions #include <darwintest.h> 10*c54f35caSApple OSS Distributions #include <darwintest_utils.h> 11*c54f35caSApple OSS Distributions 12*c54f35caSApple OSS Distributions /* 13*c54f35caSApple OSS Distributions * This test expects the entitlement or root privileges for a process to 14*c54f35caSApple OSS Distributions * set the time using settimeofday syscall. 15*c54f35caSApple OSS Distributions */ 16*c54f35caSApple OSS Distributions 17*c54f35caSApple OSS Distributions #define DAY 86400 //1 day in sec 18*c54f35caSApple OSS Distributions 19*c54f35caSApple OSS Distributions T_DECL(settime_32089962_entitled_root, 20*c54f35caSApple OSS Distributions "Verify that root privileges can allow to change the time", 21*c54f35caSApple OSS Distributions T_META_ASROOT(true), T_META_CHECK_LEAKS(false)) 22*c54f35caSApple OSS Distributions { 23*c54f35caSApple OSS Distributions struct timeval settimeofdaytime; 24*c54f35caSApple OSS Distributions struct timeval adj_time; 25*c54f35caSApple OSS Distributions struct timex ntptime; 26*c54f35caSApple OSS Distributions 27*c54f35caSApple OSS Distributions if (geteuid() != 0) { 28*c54f35caSApple OSS Distributions T_SKIP("settime_32089962_entitled_root test requires root privileges to run."); 29*c54f35caSApple OSS Distributions } 30*c54f35caSApple OSS Distributions 31*c54f35caSApple OSS Distributions /* test settimeofday */ 32*c54f35caSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(gettimeofday(&settimeofdaytime, NULL), NULL); 33*c54f35caSApple OSS Distributions T_ASSERT_POSIX_ZERO(settimeofday(&settimeofdaytime, NULL), NULL); 34*c54f35caSApple OSS Distributions 35*c54f35caSApple OSS Distributions /* test adjtime */ 36*c54f35caSApple OSS Distributions adj_time.tv_sec = 1; 37*c54f35caSApple OSS Distributions adj_time.tv_usec = 0; 38*c54f35caSApple OSS Distributions T_ASSERT_POSIX_ZERO(adjtime(&adj_time, NULL), NULL); 39*c54f35caSApple OSS Distributions 40*c54f35caSApple OSS Distributions /* test ntp_adjtime */ 41*c54f35caSApple OSS Distributions memset(&ntptime, 0, sizeof(ntptime)); 42*c54f35caSApple OSS Distributions ntptime.modes |= MOD_STATUS; 43*c54f35caSApple OSS Distributions ntptime.status = TIME_OK; 44*c54f35caSApple OSS Distributions 45*c54f35caSApple OSS Distributions T_ASSERT_EQ(ntp_adjtime(&ntptime), TIME_OK, NULL); 46*c54f35caSApple OSS Distributions } 47*c54f35caSApple OSS Distributions 48*c54f35caSApple OSS Distributions T_DECL(settime_32089962_entitled_not_root, 49*c54f35caSApple OSS Distributions "Verify that the \"com.apple.settime\" entitlement can allow to change the time", 50*c54f35caSApple OSS Distributions T_META_ASROOT(false), T_META_CHECK_LEAKS(false)) 51*c54f35caSApple OSS Distributions { 52*c54f35caSApple OSS Distributions struct timeval settimeofdaytime; 53*c54f35caSApple OSS Distributions struct timeval adj_time; 54*c54f35caSApple OSS Distributions struct timex ntptime; 55*c54f35caSApple OSS Distributions 56*c54f35caSApple OSS Distributions if (geteuid() == 0) { 57*c54f35caSApple OSS Distributions T_SKIP("settime_32089962_entitled_root test requires no root privileges to run."); 58*c54f35caSApple OSS Distributions } 59*c54f35caSApple OSS Distributions 60*c54f35caSApple OSS Distributions /* test settimeofday */ 61*c54f35caSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(gettimeofday(&settimeofdaytime, NULL), NULL); 62*c54f35caSApple OSS Distributions T_ASSERT_POSIX_ZERO(settimeofday(&settimeofdaytime, NULL), NULL); 63*c54f35caSApple OSS Distributions 64*c54f35caSApple OSS Distributions /* test adjtime */ 65*c54f35caSApple OSS Distributions adj_time.tv_sec = 1; 66*c54f35caSApple OSS Distributions adj_time.tv_usec = 0; 67*c54f35caSApple OSS Distributions T_ASSERT_POSIX_ZERO(adjtime(&adj_time, NULL), NULL); 68*c54f35caSApple OSS Distributions 69*c54f35caSApple OSS Distributions /* test ntp_adjtime */ 70*c54f35caSApple OSS Distributions memset(&ntptime, 0, sizeof(ntptime)); 71*c54f35caSApple OSS Distributions ntptime.modes |= MOD_STATUS; 72*c54f35caSApple OSS Distributions ntptime.status = TIME_OK; 73*c54f35caSApple OSS Distributions 74*c54f35caSApple OSS Distributions T_ASSERT_EQ(ntp_adjtime(&ntptime), TIME_OK, NULL); 75*c54f35caSApple OSS Distributions } 76*c54f35caSApple OSS Distributions 77*c54f35caSApple OSS Distributions T_DECL(settimeofday_29193041_entitled_root, 78*c54f35caSApple OSS Distributions "Verify that root privileges can allow to change the time", 79*c54f35caSApple OSS Distributions T_META_ASROOT(true), T_META_CHECK_LEAKS(false)) 80*c54f35caSApple OSS Distributions { 81*c54f35caSApple OSS Distributions struct timeval time; 82*c54f35caSApple OSS Distributions long new_time; 83*c54f35caSApple OSS Distributions 84*c54f35caSApple OSS Distributions if (geteuid() != 0) { 85*c54f35caSApple OSS Distributions T_SKIP("settimeofday_root_29193041 test requires root privileges to run."); 86*c54f35caSApple OSS Distributions } 87*c54f35caSApple OSS Distributions 88*c54f35caSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(gettimeofday(&time, NULL), NULL); 89*c54f35caSApple OSS Distributions 90*c54f35caSApple OSS Distributions /* increment the time of one day */ 91*c54f35caSApple OSS Distributions new_time = time.tv_sec + DAY; 92*c54f35caSApple OSS Distributions 93*c54f35caSApple OSS Distributions time.tv_sec = new_time; 94*c54f35caSApple OSS Distributions time.tv_usec = 0; 95*c54f35caSApple OSS Distributions 96*c54f35caSApple OSS Distributions T_ASSERT_POSIX_ZERO(settimeofday(&time, NULL), NULL); 97*c54f35caSApple OSS Distributions 98*c54f35caSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(gettimeofday(&time, NULL), NULL); 99*c54f35caSApple OSS Distributions 100*c54f35caSApple OSS Distributions /* expext to be past new_time */ 101*c54f35caSApple OSS Distributions T_EXPECT_GE_LONG(time.tv_sec, new_time, "Time changed with root and entitlement"); 102*c54f35caSApple OSS Distributions 103*c54f35caSApple OSS Distributions time.tv_sec -= DAY; 104*c54f35caSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(settimeofday(&time, NULL), NULL); 105*c54f35caSApple OSS Distributions } 106*c54f35caSApple OSS Distributions 107*c54f35caSApple OSS Distributions T_DECL(settimeofday_29193041_entitled_not_root, 108*c54f35caSApple OSS Distributions "Verify that the \"com.apple.settime\" entitlement can allow to change the time", 109*c54f35caSApple OSS Distributions T_META_ASROOT(false), T_META_CHECK_LEAKS(false)) 110*c54f35caSApple OSS Distributions { 111*c54f35caSApple OSS Distributions struct timeval time; 112*c54f35caSApple OSS Distributions long new_time; 113*c54f35caSApple OSS Distributions 114*c54f35caSApple OSS Distributions if (geteuid() == 0) { 115*c54f35caSApple OSS Distributions T_SKIP("settimeofday_29193041 test requires no root privileges to run."); 116*c54f35caSApple OSS Distributions } 117*c54f35caSApple OSS Distributions 118*c54f35caSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(gettimeofday(&time, NULL), NULL); 119*c54f35caSApple OSS Distributions 120*c54f35caSApple OSS Distributions /* increment the time of one day */ 121*c54f35caSApple OSS Distributions new_time = time.tv_sec + DAY; 122*c54f35caSApple OSS Distributions 123*c54f35caSApple OSS Distributions time.tv_sec = new_time; 124*c54f35caSApple OSS Distributions time.tv_usec = 0; 125*c54f35caSApple OSS Distributions 126*c54f35caSApple OSS Distributions T_ASSERT_POSIX_ZERO(settimeofday(&time, NULL), NULL); 127*c54f35caSApple OSS Distributions 128*c54f35caSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(gettimeofday(&time, NULL), NULL); 129*c54f35caSApple OSS Distributions 130*c54f35caSApple OSS Distributions /* expext to be past new_time */ 131*c54f35caSApple OSS Distributions T_EXPECT_GE_LONG(time.tv_sec, new_time, "Time successfully changed without root and with entitlement"); 132*c54f35caSApple OSS Distributions 133*c54f35caSApple OSS Distributions time.tv_sec -= DAY; 134*c54f35caSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(settimeofday(&time, NULL), NULL); 135*c54f35caSApple OSS Distributions } 136