Skip to content

Commit a6b1997

Browse files
committed
tzcode: Fix TZ for non-setugid programs
The previous commit had the desired effect for setugid programs, but broke TZ for everyone else. I didn't notice because my test cases swap out /etc/localtime instead of setting TZ, so add a test case that sets TZ. Fixes: b6ea251 ("tzcode: Limit TZ for setugid programs") Reviewed by: cy Differential Revision: https://reviews.freebsd.org/D52108
1 parent 690ae8a commit a6b1997

File tree

2 files changed

+48
-24
lines changed

2 files changed

+48
-24
lines changed

contrib/tzcode/localtime.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -535,21 +535,22 @@ tzloadbody(char const *name, struct state *sp, bool doextend,
535535
}
536536
if (doaccess && access(name, R_OK) != 0)
537537
return errno;
538+
fid = _open(name, O_RDONLY | O_BINARY);
538539
#else /* __FreeBSD__ */
539-
if (issetugid()) {
540+
{
540541
const char *relname = name;
541542
if (strncmp(relname, TZDIR "/", strlen(TZDIR) + 1) == 0)
542543
relname += strlen(TZDIR) + 1;
543544
int dd = _open(TZDIR, O_DIRECTORY | O_RDONLY);
544545
if (dd < 0)
545546
return errno;
546-
fid = _openat(dd, relname, O_RDONLY | O_BINARY, AT_RESOLVE_BENEATH);
547+
fid = _openat(dd, relname, O_RDONLY | O_BINARY,
548+
issetugid() ? AT_RESOLVE_BENEATH : 0);
547549
serrno = errno;
548550
_close(dd);
549551
errno = serrno;
550-
} else
551-
#endif
552-
fid = _open(name, O_RDONLY | O_BINARY);
552+
}
553+
#endif /* __FreeBSD__ */
553554
if (fid < 0)
554555
return errno;
555556

lib/libc/tests/stdtime/detect_tz_changes_test.c

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,26 @@
2020

2121
#include <atf-c.h>
2222

23+
static const struct tzcase {
24+
const char *tzfn;
25+
const char *expect;
26+
} tzcases[] = {
27+
/*
28+
* A handful of time zones and the expected result of
29+
* strftime("%z (%Z)", tm) when that time zone is active
30+
* and tm represents a date in the summer of 2025.
31+
*/
32+
{ "America/Vancouver", "-0700 (PDT)" },
33+
{ "America/New_York", "-0400 (EDT)" },
34+
{ "Europe/London", "+0100 (BST)" },
35+
{ "Europe/Paris", "+0200 (CEST)" },
36+
{ "Asia/Kolkata", "+0530 (IST)" },
37+
{ "Asia/Tokyo", "+0900 (JST)" },
38+
{ "Australia/Canberra", "+1000 (AEST)" },
39+
{ "UTC", "+0000 (UTC)" },
40+
{ 0 },
41+
};
42+
2343
static const time_t then = 1751328000; /* 2025-07-01 00:00:00 UTC */
2444
static const char *tz_change_interval_sym = "__tz_change_interval";
2545
static int *tz_change_interval_p;
@@ -91,25 +111,6 @@ ATF_TC_HEAD(detect_tz_changes, tc)
91111
}
92112
ATF_TC_BODY(detect_tz_changes, tc)
93113
{
94-
static const struct tzcase {
95-
const char *tzfn;
96-
const char *expect;
97-
} tzcases[] = {
98-
/*
99-
* A handful of time zones and the expected result of
100-
* strftime("%z (%Z)", tm) when that time zone is active
101-
* and tm represents a date in the summer of 2025.
102-
*/
103-
{ "America/Vancouver", "-0700 (PDT)" },
104-
{ "America/New_York", "-0400 (EDT)" },
105-
{ "Europe/London", "+0100 (BST)" },
106-
{ "Europe/Paris", "+0200 (CEST)" },
107-
{ "Asia/Kolkata", "+0530 (IST)" },
108-
{ "Asia/Tokyo", "+0900 (JST)" },
109-
{ "Australia/Canberra", "+1000 (AEST)" },
110-
{ "UTC", "+0000 (UTC)" },
111-
{ 0 },
112-
};
113114
char obuf[1024] = "";
114115
char ebuf[1024] = "";
115116
struct pollfd fds[3];
@@ -272,10 +273,32 @@ ATF_TC_BODY(detect_tz_changes, tc)
272273
ATF_REQUIRE_EQ(0, WEXITSTATUS(status));
273274
}
274275

276+
ATF_TC(tz_env);
277+
ATF_TC_HEAD(tz_env, tc)
278+
{
279+
atf_tc_set_md_var(tc, "descr", "Test TZ environment variable");
280+
}
281+
ATF_TC_BODY(tz_env, tc)
282+
{
283+
char buf[128];
284+
const struct tzcase *tzcase = NULL;
285+
struct tm *tm;
286+
size_t len;
287+
288+
for (tzcase = tzcases; tzcase->tzfn != NULL; tzcase++) {
289+
setenv("TZ", tzcase->tzfn, 1);
290+
ATF_REQUIRE((tm = localtime(&then)) != NULL);
291+
len = strftime(buf, sizeof(buf), "%z (%Z)", tm);
292+
ATF_REQUIRE(len > 0);
293+
ATF_REQUIRE_STREQ(tzcase->expect, buf);
294+
}
295+
}
296+
275297
ATF_TP_ADD_TCS(tp)
276298
{
277299
debugging = !getenv("__RUNNING_INSIDE_ATF_RUN") &&
278300
isatty(STDERR_FILENO);
279301
ATF_TP_ADD_TC(tp, detect_tz_changes);
302+
ATF_TP_ADD_TC(tp, tz_env);
280303
return (atf_no_error());
281304
}

0 commit comments

Comments
 (0)