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