From 4eec04afc3730282d2e54e2ec3e899f7d7bc4878 Mon Sep 17 00:00:00 2001 From: espie Date: Mon, 27 Jun 2016 16:52:30 +0000 Subject: [PATCH] make proper use of fstatat, as suggested by guenther@ okay millert@ --- lib/libc/gen/ttyname.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/lib/libc/gen/ttyname.c b/lib/libc/gen/ttyname.c index 456fe798087..5efbc0d220f 100644 --- a/lib/libc/gen/ttyname.c +++ b/lib/libc/gen/ttyname.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ttyname.c,v 1.17 2016/06/27 06:10:04 espie Exp $ */ +/* $OpenBSD: ttyname.c,v 1.18 2016/06/27 16:52:30 espie Exp $ */ /* * Copyright (c) 1988, 1993 * The Regents of the University of California. All rights reserved. @@ -122,16 +122,14 @@ oldttyname(struct stat *sb, char *buf, size_t len) while ((dirp = readdir(dp))) { if (dirp->d_type != DT_CHR && dirp->d_type != DT_UNKNOWN) continue; - if (dirp->d_namlen > len - sizeof(_PATH_DEV)) { - (void)closedir(dp); + if (fstatat(dirfd(dp), dirp->d_name, &dsb, AT_SYMLINK_NOFOLLOW) + || !S_ISCHR(dsb.st_mode) || sb->st_rdev != dsb.st_rdev) + continue; + (void)closedir(dp); + if (dirp->d_namlen > len - sizeof(_PATH_DEV)) return (ERANGE); - } memcpy(buf + sizeof(_PATH_DEV) - 1, dirp->d_name, dirp->d_namlen + 1); - if (lstat(buf, &dsb) || !S_ISCHR(dsb.st_mode) || - sb->st_rdev != dsb.st_rdev) - continue; - (void)closedir(dp); return (0); } (void)closedir(dp); -- 2.20.1