2
0
mirror of https://github.com/sudo-project/sudo.git synced 2025-08-22 01:49:11 +00:00

get_process_ttyname: always return the terminal device if we find one.

If sudo cannot map the device number to a device file, set name to
the empty string.  The caller now checks for an empty name and only
passes the tty path to the plugin if it is non-empty.  This allows
sudo to run without warnings in a chroot() jail where the terminal
device files are not present.  GitHub issue #421.
This commit is contained in:
Todd C. Miller 2024-11-15 20:38:14 -07:00
parent abc0baffc4
commit 7e8f006888
2 changed files with 49 additions and 32 deletions

View File

@ -622,10 +622,13 @@ get_user_info(struct user_details *ud)
if (ttydev != (dev_t)-1) {
if (asprintf(&info[++i], "ttydev=%lld", (long long)ttydev) == -1)
goto oom;
info[++i] = sudo_new_key_val("tty", path);
if (info[i] == NULL)
goto oom;
ud->tty = info[i] + sizeof("tty=") - 1;
/* The terminal device file may be missing in a chroot() jail. */
if (path[0] != '\0') {
info[++i] = sudo_new_key_val("tty", path);
if (info[i] == NULL)
goto oom;
ud->tty = info[i] + sizeof("tty=") - 1;
}
} else {
/* tty may not always be present */
if (errno != ENOENT)

View File

@ -94,8 +94,10 @@
#if defined(sudo_kp_tdev)
/*
* Store the name of the tty to which the process is attached in name.
* Returns name on success and NULL on failure, setting errno.
* Look up terminal device that the process is attached to and
* fill in its name, if available. Sets name to the empty string
* if the device number cannot be mapped to a device name.
* Returns the tty device number on success and -1 on failure, setting errno.
*/
dev_t
get_process_ttyname(char *name, size_t namelen)
@ -135,10 +137,11 @@ get_process_ttyname(char *name, size_t namelen)
errno = serrno;
ttydev = (dev_t)ki_proc->sudo_kp_tdev;
if (sudo_ttyname_dev(ttydev, name, namelen) == NULL) {
sudo_warnx(
U_("unable to find terminal name for device %u, %u"),
sudo_debug_printf(SUDO_DEBUG_WARN|SUDO_DEBUG_LINENO,
"unable to find terminal name for device %u, %u",
(unsigned int)major(ttydev), (unsigned int)minor(ttydev));
ttydev = (dev_t)-1;
if (namelen != 0)
*name = '\0';
}
}
} else {
@ -151,8 +154,10 @@ get_process_ttyname(char *name, size_t namelen)
}
#elif defined(HAVE_STRUCT_PSINFO_PR_TTYDEV)
/*
* Store the name of the tty to which the process is attached in name.
* Returns name on success and NULL on failure, setting errno.
* Look up terminal device that the process is attached to and
* fill in its name, if available. Sets name to the empty string
* if the device number cannot be mapped to a device name.
* Returns the tty device number on success and -1 on failure, setting errno.
*/
dev_t
get_process_ttyname(char *name, size_t namelen)
@ -179,10 +184,11 @@ get_process_ttyname(char *name, size_t namelen)
if (ttydev != 0 && ttydev != (dev_t)-1) {
errno = serrno;
if (sudo_ttyname_dev(ttydev, name, namelen) == NULL) {
sudo_warnx(
U_("unable to find terminal name for device %u, %u"),
sudo_debug_printf(SUDO_DEBUG_WARN|SUDO_DEBUG_LINENO,
"unable to find terminal name for device %u, %u",
(unsigned int)major(ttydev), (unsigned int)minor(ttydev));
ttydev = (dev_t)-1;
if (namelen != 0)
*name = '\0';
}
goto done;
}
@ -197,10 +203,11 @@ get_process_ttyname(char *name, size_t namelen)
if (sudo_isatty(i, &sb)) {
ttydev = sb.st_rdev;
if (sudo_ttyname_dev(ttydev, name, namelen) == NULL) {
sudo_warnx(
U_("unable to find terminal name for device %u, %u"),
sudo_debug_printf(SUDO_DEBUG_WARN|SUDO_DEBUG_LINENO,
"unable to find terminal name for device %u, %u",
(unsigned int)major(ttydev), (unsigned int)minor(ttydev));
ttydev = (dev_t)-1;
if (namelen != 0)
*name = '\0';
}
goto done;
}
@ -217,8 +224,10 @@ done:
}
#elif defined(__linux__)
/*
* Store the name of the tty to which the process is attached in name.
* Returns name on success and NULL on failure, setting errno.
* Look up terminal device that the process is attached to and
* fill in its name, if available. Sets name to the empty string
* if the device number cannot be mapped to a device name.
* Returns the tty device number on success and -1 on failure, setting errno.
*/
dev_t
get_process_ttyname(char *name, size_t namelen)
@ -282,10 +291,11 @@ get_process_ttyname(char *name, size_t namelen)
ttydev = (unsigned int)tty_nr;
errno = serrno;
if (sudo_ttyname_dev(ttydev, name, namelen) == NULL) {
sudo_warnx(
U_("unable to find terminal name for device %u, %u"),
sudo_debug_printf(SUDO_DEBUG_WARN|SUDO_DEBUG_LINENO,
"unable to find terminal name for device %u, %u",
(unsigned int)major(ttydev), (unsigned int)minor(ttydev));
ttydev = (dev_t)-1;
if (namelen != 0)
*name = '\0';
}
goto done;
}
@ -310,10 +320,11 @@ get_process_ttyname(char *name, size_t namelen)
if (sudo_isatty(i, &sb)) {
ttydev = sb.st_rdev;
if (sudo_ttyname_dev(sb.st_rdev, name, namelen) == NULL) {
sudo_warnx(
U_("unable to find terminal name for device %u, %u"),
sudo_debug_printf(SUDO_DEBUG_WARN|SUDO_DEBUG_LINENO,
"unable to find terminal name for device %u, %u",
(unsigned int)major(ttydev), (unsigned int)minor(ttydev));
ttydev = (dev_t)-1;
if (namelen != 0)
*name = '\0';
}
goto done;
}
@ -332,8 +343,10 @@ done:
}
#elif defined(HAVE_PSTAT_GETPROC)
/*
* Store the name of the tty to which the process is attached in name.
* Returns name on success and NULL on failure, setting errno.
* Look up terminal device that the process is attached to and
* fill in its name, if available. Sets name to the empty string
* if the device number cannot be mapped to a device name.
* Returns the tty device number on success and -1 on failure, setting errno.
*/
dev_t
get_process_ttyname(char *name, size_t namelen)
@ -354,11 +367,12 @@ get_process_ttyname(char *name, size_t namelen)
errno = serrno;
ttydev = makedev(pst.pst_term.psd_major, pst.pst_term.psd_minor);
if (sudo_ttyname_dev(ttydev, name, namelen) == NULL) {
sudo_warnx(
U_("unable to find terminal name for device %u, %u"),
sudo_debug_printf(SUDO_DEBUG_WARN|SUDO_DEBUG_LINENO,
"unable to find terminal name for device %u, %u",
(unsigned int)pst.pst_term.psd_major,
(unsigned int)pst.pst_term.psd_minor);
ttydev = (dev_t)-1;
if (namelen != 0)
*name = '\0';
}
goto done;
}
@ -373,8 +387,8 @@ done:
}
#else
/*
* Store the name of the tty to which the process is attached in name.
* Returns name on success and NULL on failure, setting errno.
* Look up terminal device that the process is attached to and fill in name.
* Returns the tty device number on success and -1 on failure, setting errno.
*/
dev_t
get_process_ttyname(char *name, size_t namelen)