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