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