Skip to content

Commit 7e8f006

Browse files
committed
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.
1 parent abc0baf commit 7e8f006

File tree

2 files changed

+49
-32
lines changed

2 files changed

+49
-32
lines changed

src/sudo.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -622,10 +622,13 @@ get_user_info(struct user_details *ud)
622622
if (ttydev != (dev_t)-1) {
623623
if (asprintf(&info[++i], "ttydev=%lld", (long long)ttydev) == -1)
624624
goto oom;
625-
info[++i] = sudo_new_key_val("tty", path);
626-
if (info[i] == NULL)
627-
goto oom;
628-
ud->tty = info[i] + sizeof("tty=") - 1;
625+
/* The terminal device file may be missing in a chroot() jail. */
626+
if (path[0] != '\0') {
627+
info[++i] = sudo_new_key_val("tty", path);
628+
if (info[i] == NULL)
629+
goto oom;
630+
ud->tty = info[i] + sizeof("tty=") - 1;
631+
}
629632
} else {
630633
/* tty may not always be present */
631634
if (errno != ENOENT)

src/ttyname.c

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,10 @@
9494

9595
#if defined(sudo_kp_tdev)
9696
/*
97-
* Store the name of the tty to which the process is attached in name.
98-
* Returns name on success and NULL on failure, setting errno.
97+
* Look up terminal device that the process is attached to and
98+
* fill in its name, if available. Sets name to the empty string
99+
* if the device number cannot be mapped to a device name.
100+
* Returns the tty device number on success and -1 on failure, setting errno.
99101
*/
100102
dev_t
101103
get_process_ttyname(char *name, size_t namelen)
@@ -135,10 +137,11 @@ get_process_ttyname(char *name, size_t namelen)
135137
errno = serrno;
136138
ttydev = (dev_t)ki_proc->sudo_kp_tdev;
137139
if (sudo_ttyname_dev(ttydev, name, namelen) == NULL) {
138-
sudo_warnx(
139-
U_("unable to find terminal name for device %u, %u"),
140+
sudo_debug_printf(SUDO_DEBUG_WARN|SUDO_DEBUG_LINENO,
141+
"unable to find terminal name for device %u, %u",
140142
(unsigned int)major(ttydev), (unsigned int)minor(ttydev));
141-
ttydev = (dev_t)-1;
143+
if (namelen != 0)
144+
*name = '\0';
142145
}
143146
}
144147
} else {
@@ -151,8 +154,10 @@ get_process_ttyname(char *name, size_t namelen)
151154
}
152155
#elif defined(HAVE_STRUCT_PSINFO_PR_TTYDEV)
153156
/*
154-
* Store the name of the tty to which the process is attached in name.
155-
* Returns name on success and NULL on failure, setting errno.
157+
* Look up terminal device that the process is attached to and
158+
* fill in its name, if available. Sets name to the empty string
159+
* if the device number cannot be mapped to a device name.
160+
* Returns the tty device number on success and -1 on failure, setting errno.
156161
*/
157162
dev_t
158163
get_process_ttyname(char *name, size_t namelen)
@@ -179,10 +184,11 @@ get_process_ttyname(char *name, size_t namelen)
179184
if (ttydev != 0 && ttydev != (dev_t)-1) {
180185
errno = serrno;
181186
if (sudo_ttyname_dev(ttydev, name, namelen) == NULL) {
182-
sudo_warnx(
183-
U_("unable to find terminal name for device %u, %u"),
187+
sudo_debug_printf(SUDO_DEBUG_WARN|SUDO_DEBUG_LINENO,
188+
"unable to find terminal name for device %u, %u",
184189
(unsigned int)major(ttydev), (unsigned int)minor(ttydev));
185-
ttydev = (dev_t)-1;
190+
if (namelen != 0)
191+
*name = '\0';
186192
}
187193
goto done;
188194
}
@@ -197,10 +203,11 @@ get_process_ttyname(char *name, size_t namelen)
197203
if (sudo_isatty(i, &sb)) {
198204
ttydev = sb.st_rdev;
199205
if (sudo_ttyname_dev(ttydev, name, namelen) == NULL) {
200-
sudo_warnx(
201-
U_("unable to find terminal name for device %u, %u"),
206+
sudo_debug_printf(SUDO_DEBUG_WARN|SUDO_DEBUG_LINENO,
207+
"unable to find terminal name for device %u, %u",
202208
(unsigned int)major(ttydev), (unsigned int)minor(ttydev));
203-
ttydev = (dev_t)-1;
209+
if (namelen != 0)
210+
*name = '\0';
204211
}
205212
goto done;
206213
}
@@ -217,8 +224,10 @@ get_process_ttyname(char *name, size_t namelen)
217224
}
218225
#elif defined(__linux__)
219226
/*
220-
* Store the name of the tty to which the process is attached in name.
221-
* Returns name on success and NULL on failure, setting errno.
227+
* Look up terminal device that the process is attached to and
228+
* fill in its name, if available. Sets name to the empty string
229+
* if the device number cannot be mapped to a device name.
230+
* Returns the tty device number on success and -1 on failure, setting errno.
222231
*/
223232
dev_t
224233
get_process_ttyname(char *name, size_t namelen)
@@ -282,10 +291,11 @@ get_process_ttyname(char *name, size_t namelen)
282291
ttydev = (unsigned int)tty_nr;
283292
errno = serrno;
284293
if (sudo_ttyname_dev(ttydev, name, namelen) == NULL) {
285-
sudo_warnx(
286-
U_("unable to find terminal name for device %u, %u"),
294+
sudo_debug_printf(SUDO_DEBUG_WARN|SUDO_DEBUG_LINENO,
295+
"unable to find terminal name for device %u, %u",
287296
(unsigned int)major(ttydev), (unsigned int)minor(ttydev));
288-
ttydev = (dev_t)-1;
297+
if (namelen != 0)
298+
*name = '\0';
289299
}
290300
goto done;
291301
}
@@ -310,10 +320,11 @@ get_process_ttyname(char *name, size_t namelen)
310320
if (sudo_isatty(i, &sb)) {
311321
ttydev = sb.st_rdev;
312322
if (sudo_ttyname_dev(sb.st_rdev, name, namelen) == NULL) {
313-
sudo_warnx(
314-
U_("unable to find terminal name for device %u, %u"),
323+
sudo_debug_printf(SUDO_DEBUG_WARN|SUDO_DEBUG_LINENO,
324+
"unable to find terminal name for device %u, %u",
315325
(unsigned int)major(ttydev), (unsigned int)minor(ttydev));
316-
ttydev = (dev_t)-1;
326+
if (namelen != 0)
327+
*name = '\0';
317328
}
318329
goto done;
319330
}
@@ -332,8 +343,10 @@ get_process_ttyname(char *name, size_t namelen)
332343
}
333344
#elif defined(HAVE_PSTAT_GETPROC)
334345
/*
335-
* Store the name of the tty to which the process is attached in name.
336-
* Returns name on success and NULL on failure, setting errno.
346+
* Look up terminal device that the process is attached to and
347+
* fill in its name, if available. Sets name to the empty string
348+
* if the device number cannot be mapped to a device name.
349+
* Returns the tty device number on success and -1 on failure, setting errno.
337350
*/
338351
dev_t
339352
get_process_ttyname(char *name, size_t namelen)
@@ -354,11 +367,12 @@ get_process_ttyname(char *name, size_t namelen)
354367
errno = serrno;
355368
ttydev = makedev(pst.pst_term.psd_major, pst.pst_term.psd_minor);
356369
if (sudo_ttyname_dev(ttydev, name, namelen) == NULL) {
357-
sudo_warnx(
358-
U_("unable to find terminal name for device %u, %u"),
370+
sudo_debug_printf(SUDO_DEBUG_WARN|SUDO_DEBUG_LINENO,
371+
"unable to find terminal name for device %u, %u",
359372
(unsigned int)pst.pst_term.psd_major,
360373
(unsigned int)pst.pst_term.psd_minor);
361-
ttydev = (dev_t)-1;
374+
if (namelen != 0)
375+
*name = '\0';
362376
}
363377
goto done;
364378
}
@@ -373,8 +387,8 @@ get_process_ttyname(char *name, size_t namelen)
373387
}
374388
#else
375389
/*
376-
* Store the name of the tty to which the process is attached in name.
377-
* Returns name on success and NULL on failure, setting errno.
390+
* Look up terminal device that the process is attached to and fill in name.
391+
* Returns the tty device number on success and -1 on failure, setting errno.
378392
*/
379393
dev_t
380394
get_process_ttyname(char *name, size_t namelen)

0 commit comments

Comments
 (0)