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 <mach/clock_types.h> 6*c54f35caSApple OSS Distributions #include <sys/mman.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_not_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 /* test settimeofday */ 28*c54f35caSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(gettimeofday(&settimeofdaytime, NULL), NULL); 29*c54f35caSApple OSS Distributions T_ASSERT_POSIX_ZERO(settimeofday(&settimeofdaytime, NULL), NULL); 30*c54f35caSApple OSS Distributions 31*c54f35caSApple OSS Distributions /* test adjtime */ 32*c54f35caSApple OSS Distributions adj_time.tv_sec = 1; 33*c54f35caSApple OSS Distributions adj_time.tv_usec = 0; 34*c54f35caSApple OSS Distributions T_ASSERT_POSIX_ZERO(adjtime(&adj_time, NULL), NULL); 35*c54f35caSApple OSS Distributions 36*c54f35caSApple OSS Distributions /* test ntp_adjtime */ 37*c54f35caSApple OSS Distributions memset(&ntptime, 0, sizeof(ntptime)); 38*c54f35caSApple OSS Distributions ntptime.modes |= MOD_STATUS; 39*c54f35caSApple OSS Distributions ntptime.status = TIME_OK; 40*c54f35caSApple OSS Distributions 41*c54f35caSApple OSS Distributions T_ASSERT_EQ(ntp_adjtime(&ntptime), TIME_OK, NULL); 42*c54f35caSApple OSS Distributions } 43*c54f35caSApple OSS Distributions 44*c54f35caSApple OSS Distributions T_DECL(settime_32089962_not_entitled_not_root, 45*c54f35caSApple OSS Distributions "Verify that the \"com.apple.settime\" entitlement can allow to change the time", 46*c54f35caSApple OSS Distributions T_META_ASROOT(false), T_META_CHECK_LEAKS(false)) 47*c54f35caSApple OSS Distributions { 48*c54f35caSApple OSS Distributions struct timeval settimeofdaytime; 49*c54f35caSApple OSS Distributions struct timeval adj_time; 50*c54f35caSApple OSS Distributions struct timex ntptime; 51*c54f35caSApple OSS Distributions int res; 52*c54f35caSApple OSS Distributions 53*c54f35caSApple OSS Distributions if (geteuid() == 0) { 54*c54f35caSApple OSS Distributions T_SKIP("settimeofday_29193041 test requires no root privileges to run."); 55*c54f35caSApple OSS Distributions } 56*c54f35caSApple OSS Distributions 57*c54f35caSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(gettimeofday(&settimeofdaytime, NULL), NULL); 58*c54f35caSApple OSS Distributions 59*c54f35caSApple OSS Distributions /* test settimeofday */ 60*c54f35caSApple OSS Distributions #if (TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR) 61*c54f35caSApple OSS Distributions T_ASSERT_POSIX_ZERO(settimeofday(&settimeofdaytime, NULL), NULL); 62*c54f35caSApple OSS Distributions #else 63*c54f35caSApple OSS Distributions res = settimeofday(&settimeofdaytime, NULL); 64*c54f35caSApple OSS Distributions T_ASSERT_EQ(res, -1, NULL); 65*c54f35caSApple OSS Distributions #endif 66*c54f35caSApple OSS Distributions 67*c54f35caSApple OSS Distributions /* test adjtime */ 68*c54f35caSApple OSS Distributions adj_time.tv_sec = 1; 69*c54f35caSApple OSS Distributions adj_time.tv_usec = 0; 70*c54f35caSApple OSS Distributions res = adjtime(&adj_time, NULL); 71*c54f35caSApple OSS Distributions T_ASSERT_EQ(res, -1, NULL); 72*c54f35caSApple OSS Distributions 73*c54f35caSApple OSS Distributions /* test ntp_adjtime */ 74*c54f35caSApple OSS Distributions memset(&ntptime, 0, sizeof(ntptime)); 75*c54f35caSApple OSS Distributions ntptime.modes |= MOD_STATUS; 76*c54f35caSApple OSS Distributions ntptime.status = TIME_OK; 77*c54f35caSApple OSS Distributions res = ntp_adjtime(&ntptime); 78*c54f35caSApple OSS Distributions T_ASSERT_EQ(res, -1, NULL); 79*c54f35caSApple OSS Distributions } 80*c54f35caSApple OSS Distributions 81*c54f35caSApple OSS Distributions T_DECL(settimeofday_29193041_not_entitled_root, 82*c54f35caSApple OSS Distributions "Verify that root privileges can allow to change the time", 83*c54f35caSApple OSS Distributions T_META_ASROOT(true), T_META_CHECK_LEAKS(false)) 84*c54f35caSApple OSS Distributions { 85*c54f35caSApple OSS Distributions struct timeval time; 86*c54f35caSApple OSS Distributions long new_time; 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 without 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_not_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 #if (TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR) 127*c54f35caSApple OSS Distributions T_ASSERT_POSIX_ZERO(settimeofday(&time, NULL), NULL); 128*c54f35caSApple OSS Distributions #else 129*c54f35caSApple OSS Distributions int res = settimeofday(&time, NULL); 130*c54f35caSApple OSS Distributions T_ASSERT_EQ(res, -1, NULL); 131*c54f35caSApple OSS Distributions #endif 132*c54f35caSApple OSS Distributions 133*c54f35caSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(gettimeofday(&time, NULL), NULL); 134*c54f35caSApple OSS Distributions 135*c54f35caSApple OSS Distributions #if (TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR) 136*c54f35caSApple OSS Distributions /* expext to be past new_time */ 137*c54f35caSApple OSS Distributions T_EXPECT_GE_LONG(time.tv_sec, new_time, "Time successfully changed without root and without entitlement"); 138*c54f35caSApple OSS Distributions time.tv_sec -= DAY; 139*c54f35caSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(settimeofday(&time, NULL), NULL); 140*c54f35caSApple OSS Distributions #else 141*c54f35caSApple OSS Distributions T_EXPECT_LT_LONG(time.tv_sec, new_time, "Not permitted to change time without root and without entitlement"); 142*c54f35caSApple OSS Distributions #endif 143*c54f35caSApple OSS Distributions } 144