Skip to content

Commit 8e43ecb

Browse files
committed
Improve TZ="/..." security and speed (NetBSD only)
* NEWS: Mention security. * localtime.c (O_REGULAR): Default to 0. (tzloadbody): Open TZif files with O_REGULAR. Suppress unnecessary call to 'stat' if O_REGULAR is present.
1 parent 9d97da0 commit 8e43ecb

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ Unreleased, experimental changes
4040
and reject relative names containing ".." directory components;
4141
formerly, only privileged programs did those two things.
4242
These changes were inspired by similar behavior in FreeBSD.
43+
On NetBSD, unprivileged programs now use O_REGULAR to check
44+
whether a TZ setting starting with '/' names a regular file,
45+
avoiding a minor security race still present elsewhere.
4346
TZ strings taken from tzalloc arguments are now treated with
4447
no less caution than TZ strings taken from the environment, as
4548
the old undocumented behavior would have been hard to explain.

localtime.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,9 @@ static int openat(int dd, char const *path, int oflag) { unreachable (); }
355355
#ifndef O_PATH
356356
# define O_PATH 0
357357
#endif
358+
#ifndef O_REGULAR
359+
# define O_REGULAR 0
360+
#endif
358361
#ifndef O_RESOLVE_BENEATH
359362
# define O_RESOLVE_BENEATH 0
360363
#endif
@@ -900,7 +903,7 @@ tzloadbody(char const *name, struct state *sp, char tzloadflags,
900903
register int tzheadsize = sizeof(struct tzhead);
901904
int dd = AT_FDCWD;
902905
int oflags = (O_RDONLY | O_BINARY | O_CLOEXEC | O_CLOFORK
903-
| O_IGNORE_CTTY | O_NOCTTY);
906+
| O_IGNORE_CTTY | O_NOCTTY | O_REGULAR);
904907
int err;
905908
struct stat st;
906909
st.st_ctime = 0;
@@ -927,7 +930,7 @@ tzloadbody(char const *name, struct state *sp, char tzloadflags,
927930
continue;
928931
else if (issetugid())
929932
return ENOTCAPABLE;
930-
else {
933+
else if (!O_REGULAR) {
931934
/* Check for devices, as their mere opening could have
932935
unwanted side effects. Though racy, there is no
933936
portable way to fix the races. This check is needed

0 commit comments

Comments
 (0)