Add fallback if /proc/self/stat or /proc/pid/psinfo is missing or invalid.
If the /proc file indicates no terminal is present there is no fallback. Bug #1020
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
/*
|
/*
|
||||||
* SPDX-License-Identifier: ISC
|
* SPDX-License-Identifier: ISC
|
||||||
*
|
*
|
||||||
* Copyright (c) 2012-2020 Todd C. Miller <Todd.Miller@sudo.ws>
|
* Copyright (c) 2012-2022 Todd C. Miller <Todd.Miller@sudo.ws>
|
||||||
*
|
*
|
||||||
* Permission to use, copy, modify, and distribute this software for any
|
* Permission to use, copy, modify, and distribute this software for any
|
||||||
* purpose with or without fee is hereby granted, provided that the above
|
* purpose with or without fee is hereby granted, provided that the above
|
||||||
@@ -174,6 +174,17 @@ get_process_ttyname(char *name, size_t namelen)
|
|||||||
goto done;
|
goto done;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
struct stat sb;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
/* Missing /proc/pid/psinfo file. */
|
||||||
|
for (i = STDIN_FILENO; i <= STDERR_FILENO; i++) {
|
||||||
|
if (isatty(i) && fstat(i, &sb) != -1) {
|
||||||
|
ret = sudo_ttyname_dev(sb.st_rdev, name, namelen);
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
errno = ENOENT;
|
errno = ENOENT;
|
||||||
|
|
||||||
@@ -196,6 +207,7 @@ get_process_ttyname(char *name, size_t namelen)
|
|||||||
char *cp, buf[1024];
|
char *cp, buf[1024];
|
||||||
char *ret = NULL;
|
char *ret = NULL;
|
||||||
int serrno = errno;
|
int serrno = errno;
|
||||||
|
pid_t ppid = 0;
|
||||||
ssize_t nread;
|
ssize_t nread;
|
||||||
int fd;
|
int fd;
|
||||||
debug_decl(get_process_ttyname, SUDO_DEBUG_UTIL);
|
debug_decl(get_process_ttyname, SUDO_DEBUG_UTIL);
|
||||||
@@ -232,7 +244,8 @@ get_process_ttyname(char *name, size_t namelen)
|
|||||||
while (*++ep != '\0') {
|
while (*++ep != '\0') {
|
||||||
if (*ep == ' ') {
|
if (*ep == ' ') {
|
||||||
*ep = '\0';
|
*ep = '\0';
|
||||||
if (++field == 7) {
|
field++;
|
||||||
|
if (field == 7) {
|
||||||
int tty_nr = sudo_strtonum(cp, INT_MIN, INT_MAX,
|
int tty_nr = sudo_strtonum(cp, INT_MIN, INT_MAX,
|
||||||
&errstr);
|
&errstr);
|
||||||
if (errstr) {
|
if (errstr) {
|
||||||
@@ -253,12 +266,27 @@ get_process_ttyname(char *name, size_t namelen)
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
if (field == 3) {
|
||||||
|
ppid = sudo_strtonum(cp, INT_MIN, INT_MAX, NULL);
|
||||||
|
}
|
||||||
cp = ep + 1;
|
cp = ep + 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (ppid == 0) {
|
||||||
|
struct stat sb;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
/* No parent pid found, /proc/self/stat is missing or corrupt. */
|
||||||
|
for (i = STDIN_FILENO; i <= STDERR_FILENO; i++) {
|
||||||
|
if (isatty(i) && fstat(i, &sb) != -1) {
|
||||||
|
ret = sudo_ttyname_dev(sb.st_rdev, name, namelen);
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
errno = ENOENT;
|
errno = ENOENT;
|
||||||
|
|
||||||
done:
|
done:
|
||||||
|
Reference in New Issue
Block a user