2
0
mirror of https://github.com/sudo-project/sudo.git synced 2025-08-31 14:25:15 +00:00

fd_matches_tty: only zero out fd_sb if fstat(2) fails.

We need to preserve the contents of the struct stat if the fd is
some other type so the check for piped output works correctly.
Bug #1057
This commit is contained in:
Todd C. Miller
2023-08-12 10:39:33 -06:00
parent 6f659e2deb
commit d148e7d8f9

View File

@@ -387,11 +387,13 @@ fd_matches_tty(int fd, struct stat *tty_sb, struct stat *fd_sb)
{
debug_decl(fd_is_user_tty, SUDO_DEBUG_EXEC);
if (fstat(fd, fd_sb) == -1 || !S_ISCHR(fd_sb->st_mode)) {
if (fstat(fd, fd_sb) == -1) {
/* Always initialize fd_sb. */
memset(fd_sb, 0, sizeof(*fd_sb));
debug_return_bool(false);
}
if (!S_ISCHR(fd_sb->st_mode))
debug_return_bool(false);
/* Compare with tty_sb if available, else just check that fd is a tty. */
debug_return_bool(tty_sb ? tty_sb->st_rdev == fd_sb->st_rdev : isatty(fd));