5 #include <mach/clock_types.h>
9 #include <darwintest.h>
10 #include <darwintest_utils.h>
13 #include <sys/types.h>
15 #include <uuid/uuid.h>
18 #define EXIT_FAIL() exit((__LINE__ % 255) + 1)
21 * This test expects the entitlement or root privileges for a process to
22 * set the time using settimeofday syscall.
25 #define DAY 86400 //1 day in sec
28 * To run without root privileges
29 * <rdar://problem/28315048> libdarwintest should run leaks even without root
31 static void drop_priv(void){
32 /* determine the less-privileged UID and GID */
34 unsigned long lower_uid
= 0;
35 unsigned long lower_gid
= 0;
38 struct passwd
*pw
= getpwnam("mobile");
40 printf("child: error: get_pwname(\"mobile\") failed %d: %s\n", errno
, strerror(errno
));
44 lower_uid
= pw
->pw_uid
;
45 lower_gid
= pw
->pw_gid
;
47 char *sudo_gid_str
= getenv("SUDO_GID");
49 printf("child: error: SUDO_GID environment variable unset (not run under sudo)\n");
53 char *sudo_uid_str
= getenv("SUDO_UID");
55 printf("child: error: SUDO_UID environment variable unset (not run under sudo)\n");
59 char *end
= sudo_gid_str
;
60 lower_gid
= strtoul(sudo_gid_str
, &end
, 10);
61 if (sudo_gid_str
== end
&& sudo_gid_str
[0] != '\0') {
62 printf("child: error: SUDO_GID (%s) could not be converted to an integer\n", sudo_gid_str
);
66 printf("child: error: less-privileged GID invalid\n");
71 lower_uid
= strtoul(sudo_uid_str
, &end
, 10);
72 if (sudo_uid_str
== end
&& sudo_uid_str
[0] != '\0') {
73 printf("child: error: SUDO_UID (%s) could not be converted to an integer\n", sudo_uid_str
);
77 printf("child: error: less-privileged UID invalid\n");
82 if (setgid(lower_gid
) == -1) {
83 printf("child: error: could not change group to %lu\n", lower_gid
);
86 if (setuid(lower_uid
) == -1) {
87 printf("child: error: could not change user to %lu\n", lower_uid
);
92 T_DECL(settime_32089962_not_entitled_root
,
93 "Verify that root privileges can allow to change the time",
94 T_META_ASROOT(true), T_META_CHECK_LEAKS(NO
))
96 struct timeval settimeofdaytime
;
97 struct timeval adj_time
;
101 T_SKIP("settimeofday_root_29193041 test requires root privileges to run.");
104 /* test settimeofday */
105 T_QUIET
; T_ASSERT_POSIX_ZERO(gettimeofday(&settimeofdaytime
, NULL
), NULL
);
106 T_ASSERT_POSIX_ZERO(settimeofday(&settimeofdaytime
, NULL
), NULL
);
110 adj_time
.tv_usec
= 0;
111 T_ASSERT_POSIX_ZERO(adjtime(&adj_time
, NULL
),NULL
);
113 /* test ntp_adjtime */
114 memset(&ntptime
, 0, sizeof(ntptime
));
115 ntptime
.modes
|= MOD_STATUS
;
116 ntptime
.status
= TIME_OK
;
118 T_ASSERT_EQ(ntp_adjtime(&ntptime
), TIME_OK
, NULL
);
121 T_DECL(settime_32089962_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(NO
))
125 struct timeval settimeofdaytime
;
126 struct timeval adj_time
;
127 struct timex ntptime
;
133 T_SKIP("settimeofday_29193041 test requires no root privileges to run.");
136 T_QUIET
; T_ASSERT_POSIX_ZERO(gettimeofday(&settimeofdaytime
, NULL
), NULL
);
138 /* test settimeofday */
139 #if TARGET_OS_EMBEDDED
140 T_ASSERT_POSIX_ZERO(settimeofday(&settimeofdaytime
, NULL
), NULL
);
142 res
= settimeofday(&settimeofdaytime
, NULL
);
143 T_ASSERT_EQ(res
, -1, NULL
);
148 adj_time
.tv_usec
= 0;
149 res
= adjtime(&adj_time
, NULL
);
150 T_ASSERT_EQ(res
, -1, NULL
);
152 /* test ntp_adjtime */
153 memset(&ntptime
, 0, sizeof(ntptime
));
154 ntptime
.modes
|= MOD_STATUS
;
155 ntptime
.status
= TIME_OK
;
156 res
= ntp_adjtime(&ntptime
);
157 T_ASSERT_EQ(res
, -1, NULL
);
160 T_DECL(settimeofday_29193041_not_entitled_root
,
161 "Verify that root privileges can allow to change the time",
162 T_META_ASROOT(true), T_META_CHECK_LEAKS(NO
))
168 T_SKIP("settimeofday_root_29193041 test requires root privileges to run.");
171 T_QUIET
; T_ASSERT_POSIX_ZERO(gettimeofday(&time
, NULL
), NULL
);
173 /* increment the time of one day */
174 new_time
= time
.tv_sec
+ DAY
;
176 time
.tv_sec
= new_time
;
179 T_ASSERT_POSIX_ZERO(settimeofday(&time
, NULL
), NULL
);
181 T_QUIET
; T_ASSERT_POSIX_ZERO(gettimeofday(&time
, NULL
), NULL
);
183 /* expext to be past new_time */
184 T_EXPECT_GE_LONG(time
.tv_sec
, new_time
, "Time changed with root and without entitlement");
187 T_QUIET
;T_ASSERT_POSIX_ZERO(settimeofday(&time
, NULL
), NULL
);
190 T_DECL(settimeofday_29193041_not_entitled_not_root
,
191 "Verify that the \"com.apple.settime\" entitlement can allow to change the time",
192 T_META_ASROOT(false), T_META_CHECK_LEAKS(NO
))
200 T_SKIP("settimeofday_29193041 test requires no root privileges to run.");
203 T_QUIET
; T_ASSERT_POSIX_ZERO(gettimeofday(&time
, NULL
), NULL
);
205 /* increment the time of one day */
206 new_time
= time
.tv_sec
+ DAY
;
208 time
.tv_sec
= new_time
;
211 #if TARGET_OS_EMBEDDED
212 T_ASSERT_POSIX_ZERO(settimeofday(&time
, NULL
), NULL
);
214 int res
= settimeofday(&time
, NULL
);
215 T_ASSERT_EQ(res
, -1, NULL
);
218 T_QUIET
; T_ASSERT_POSIX_ZERO(gettimeofday(&time
, NULL
), NULL
);
220 #if TARGET_OS_EMBEDDED
221 /* expext to be past new_time */
222 T_EXPECT_GE_LONG(time
.tv_sec
, new_time
, "Time successfully changed without root and without entitlement");
224 T_QUIET
; T_ASSERT_POSIX_ZERO(settimeofday(&time
, NULL
), NULL
);
226 T_EXPECT_LT_LONG(time
.tv_sec
, new_time
, "Not permitted to change time without root and without entitlement");