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