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