6 #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_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("settime_32089962_entitled_root 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_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
))
126 struct timeval settimeofdaytime
;
127 struct timeval adj_time
;
128 struct timex ntptime
;
133 T_SKIP("settime_32089962_entitled_root test requires no root privileges to run.");
136 /* test settimeofday */
137 T_QUIET
; T_ASSERT_POSIX_ZERO(gettimeofday(&settimeofdaytime
, NULL
), NULL
);
138 T_ASSERT_POSIX_ZERO(settimeofday(&settimeofdaytime
, NULL
), NULL
);
142 adj_time
.tv_usec
= 0;
143 T_ASSERT_POSIX_ZERO(adjtime(&adj_time
, NULL
),NULL
);
145 /* test ntp_adjtime */
146 memset(&ntptime
, 0, sizeof(ntptime
));
147 ntptime
.modes
|= MOD_STATUS
;
148 ntptime
.status
= TIME_OK
;
150 T_ASSERT_EQ(ntp_adjtime(&ntptime
), TIME_OK
, NULL
);
154 T_DECL(settimeofday_29193041_entitled_root
,
155 "Verify that root privileges can allow to change the time",
156 T_META_ASROOT(true), T_META_CHECK_LEAKS(NO
))
162 T_SKIP("settimeofday_root_29193041 test requires root privileges to run.");
165 T_QUIET
; T_ASSERT_POSIX_ZERO(gettimeofday(&time
, NULL
), NULL
);
167 /* increment the time of one day */
168 new_time
= time
.tv_sec
+ DAY
;
170 time
.tv_sec
= new_time
;
173 T_ASSERT_POSIX_ZERO(settimeofday(&time
, NULL
), NULL
);
175 T_QUIET
; T_ASSERT_POSIX_ZERO(gettimeofday(&time
, NULL
), NULL
);
177 /* expext to be past new_time */
178 T_EXPECT_GE_LONG(time
.tv_sec
, new_time
, "Time changed with root and entitlement");
181 T_QUIET
;T_ASSERT_POSIX_ZERO(settimeofday(&time
, NULL
), NULL
);
184 T_DECL(settimeofday_29193041_entitled_not_root
,
185 "Verify that the \"com.apple.settime\" entitlement can allow to change the time",
186 T_META_ASROOT(false), T_META_CHECK_LEAKS(NO
))
194 T_SKIP("settimeofday_29193041 test requires no root privileges to run.");
197 T_QUIET
; T_ASSERT_POSIX_ZERO(gettimeofday(&time
, NULL
), NULL
);
199 /* increment the time of one day */
200 new_time
= time
.tv_sec
+ DAY
;
202 time
.tv_sec
= new_time
;
205 T_ASSERT_POSIX_ZERO(settimeofday(&time
, NULL
), NULL
);
207 T_QUIET
; T_ASSERT_POSIX_ZERO(gettimeofday(&time
, NULL
), NULL
);
209 /* expext to be past new_time */
210 T_EXPECT_GE_LONG(time
.tv_sec
, new_time
, "Time successfully changed without root and with entitlement");
213 T_QUIET
; T_ASSERT_POSIX_ZERO(settimeofday(&time
, NULL
), NULL
);