diff --git a/src/regress/ttyname/check_ttyname.c b/src/regress/ttyname/check_ttyname.c index c43ea2188..637d98631 100644 --- a/src/regress/ttyname/check_ttyname.c +++ b/src/regress/ttyname/check_ttyname.c @@ -1,7 +1,7 @@ /* * SPDX-License-Identifier: ISC * - * Copyright (c) 2013-2016 Todd C. Miller + * Copyright (c) 2013-2020 Todd C. Miller * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -19,6 +19,7 @@ #include #include +#include #include #include #include @@ -36,6 +37,27 @@ sudo_dso_public int main(int argc, char *argv[]); int sudo_debug_instance = SUDO_DEBUG_INSTANCE_INITIALIZER; extern char *get_process_ttyname(char *name, size_t namelen); +static int +match_ttys(const char *tty1, const char *tty2) +{ + struct stat sb1, sb2; + + if (tty1 != NULL && tty2 != NULL) { + if (strcmp(tty1, tty2) == 0) + return 0; + /* Could be the same device with a different name. */ + if (stat(tty1, &sb1) == 0 && stat(tty2, &sb2) == 0) { + if (sb1.st_rdev == sb2.st_rdev) + return 0; + } + } else if (tty1 == NULL && tty2 == NULL) { + return 0; + } + + return 1; +} + + int main(int argc, char *argv[]) { @@ -61,13 +83,7 @@ main(int argc, char *argv[]) #endif /* Compare libc and kernel ttys. */ - if (tty_libc != NULL && tty_sudo != NULL) { - if (strcmp(tty_libc, tty_sudo) == 0) - ret = 0; - } else if (tty_libc == NULL && tty_sudo == NULL) { - ret = 0; - } - + ret = match_ttys(tty_libc, tty_sudo); if (ret == 0) { printf("%s: OK (%s)\n", getprogname(), tty_sudo ? tty_sudo : "none"); } else if (tty_libc == NULL) {