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