]> git.saurik.com Git - apple/xnu.git/blob - tests/settimeofday_29193041.c
xnu-4903.241.1.tar.gz
[apple/xnu.git] / tests / settimeofday_29193041.c
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 #if CONFIG_EMBEDDED
13 #include <sys/types.h>
14 #include <pwd.h>
15 #include <uuid/uuid.h>
16 #endif
17
18 /*
19 * This test expects the entitlement or root privileges for a process to
20 * set the time using settimeofday syscall.
21 */
22
23 #define DAY 86400 //1 day in sec
24
25 T_DECL(settime_32089962_not_entitled_root,
26 "Verify that root privileges can allow to change the time",
27 T_META_ASROOT(true), T_META_CHECK_LEAKS(false))
28 {
29 struct timeval settimeofdaytime;
30 struct timeval adj_time;
31 struct timex ntptime;
32
33 if (geteuid() != 0){
34 T_SKIP("settimeofday_root_29193041 test requires root privileges to run.");
35 }
36
37 /* test settimeofday */
38 T_QUIET; T_ASSERT_POSIX_ZERO(gettimeofday(&settimeofdaytime, NULL), NULL);
39 T_ASSERT_POSIX_ZERO(settimeofday(&settimeofdaytime, NULL), NULL);
40
41 /* test adjtime */
42 adj_time.tv_sec = 1;
43 adj_time.tv_usec = 0;
44 T_ASSERT_POSIX_ZERO(adjtime(&adj_time, NULL),NULL);
45
46 /* test ntp_adjtime */
47 memset(&ntptime, 0, sizeof(ntptime));
48 ntptime.modes |= MOD_STATUS;
49 ntptime.status = TIME_OK;
50
51 T_ASSERT_EQ(ntp_adjtime(&ntptime), TIME_OK, NULL);
52 }
53
54 T_DECL(settime_32089962_not_entitled_not_root,
55 "Verify that the \"com.apple.settime\" entitlement can allow to change the time",
56 T_META_ASROOT(false), T_META_CHECK_LEAKS(false))
57 {
58 struct timeval settimeofdaytime;
59 struct timeval adj_time;
60 struct timex ntptime;
61 int res;
62
63 if (geteuid() == 0){
64 T_SKIP("settimeofday_29193041 test requires no root privileges to run.");
65 }
66
67 T_QUIET; T_ASSERT_POSIX_ZERO(gettimeofday(&settimeofdaytime, NULL), NULL);
68
69 /* test settimeofday */
70 #if TARGET_OS_EMBEDDED
71 T_ASSERT_POSIX_ZERO(settimeofday(&settimeofdaytime, NULL), NULL);
72 #else
73 res = settimeofday(&settimeofdaytime, NULL);
74 T_ASSERT_EQ(res, -1, NULL);
75 #endif
76
77 /* test adjtime */
78 adj_time.tv_sec = 1;
79 adj_time.tv_usec = 0;
80 res = adjtime(&adj_time, NULL);
81 T_ASSERT_EQ(res, -1, NULL);
82
83 /* test ntp_adjtime */
84 memset(&ntptime, 0, sizeof(ntptime));
85 ntptime.modes |= MOD_STATUS;
86 ntptime.status = TIME_OK;
87 res = ntp_adjtime(&ntptime);
88 T_ASSERT_EQ(res, -1, NULL);
89 }
90
91 T_DECL(settimeofday_29193041_not_entitled_root,
92 "Verify that root privileges can allow to change the time",
93 T_META_ASROOT(true), T_META_CHECK_LEAKS(false))
94 {
95 struct timeval time;
96 long new_time;
97
98 if (geteuid() != 0){
99 T_SKIP("settimeofday_root_29193041 test requires root privileges to run.");
100 }
101
102 T_QUIET; T_ASSERT_POSIX_ZERO(gettimeofday(&time, NULL), NULL);
103
104 /* increment the time of one day */
105 new_time = time.tv_sec + DAY;
106
107 time.tv_sec = new_time;
108 time.tv_usec = 0;
109
110 T_ASSERT_POSIX_ZERO(settimeofday(&time, NULL), NULL);
111
112 T_QUIET; T_ASSERT_POSIX_ZERO(gettimeofday(&time, NULL), NULL);
113
114 /* expext to be past new_time */
115 T_EXPECT_GE_LONG(time.tv_sec, new_time, "Time changed with root and without entitlement");
116
117 time.tv_sec -= DAY;
118 T_QUIET;T_ASSERT_POSIX_ZERO(settimeofday(&time, NULL), NULL);
119 }
120
121 T_DECL(settimeofday_29193041_not_entitled_not_root,
122 "Verify that the \"com.apple.settime\" entitlement can allow to change the time",
123 T_META_ASROOT(false), T_META_CHECK_LEAKS(false))
124 {
125 struct timeval time;
126 long new_time;
127
128 if (geteuid() == 0){
129 T_SKIP("settimeofday_29193041 test requires no root privileges to run.");
130 }
131
132 T_QUIET; T_ASSERT_POSIX_ZERO(gettimeofday(&time, NULL), NULL);
133
134 /* increment the time of one day */
135 new_time = time.tv_sec + DAY;
136
137 time.tv_sec = new_time;
138 time.tv_usec = 0;
139
140 #if TARGET_OS_EMBEDDED
141 T_ASSERT_POSIX_ZERO(settimeofday(&time, NULL), NULL);
142 #else
143 int res = settimeofday(&time, NULL);
144 T_ASSERT_EQ(res, -1, NULL);
145 #endif
146
147 T_QUIET; T_ASSERT_POSIX_ZERO(gettimeofday(&time, NULL), NULL);
148
149 #if TARGET_OS_EMBEDDED
150 /* expext to be past new_time */
151 T_EXPECT_GE_LONG(time.tv_sec, new_time, "Time successfully changed without root and without entitlement");
152 time.tv_sec -= DAY;
153 T_QUIET; T_ASSERT_POSIX_ZERO(settimeofday(&time, NULL), NULL);
154 #else
155 T_EXPECT_LT_LONG(time.tv_sec, new_time, "Not permitted to change time without root and without entitlement");
156 #endif
157
158 }