Skip to content

Commit

Permalink
get_process_ttyname: always return the terminal device if we find one.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
millert committed Nov 16, 2024
1 parent abc0baf commit 7e8f006
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 32 deletions.
11 changes: 7 additions & 4 deletions src/sudo.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
70 changes: 42 additions & 28 deletions src/ttyname.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -217,8 +224,10 @@ get_process_ttyname(char *name, size_t namelen)
}
#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)
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -332,8 +343,10 @@ get_process_ttyname(char *name, size_t namelen)
}
#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)
Expand All @@ -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;
}
Expand All @@ -373,8 +387,8 @@ get_process_ttyname(char *name, size_t namelen)
}
#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)
Expand Down

0 comments on commit 7e8f006

Please sign in to comment.