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