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