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