From e60a4a3c46df1abc6570a026d716c71a6cb1bf5c Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 12 May 2021 13:45:48 +0200 Subject: [PATCH] terminal-util: add extra validity checks that we operate on a TTY before doing so Prompted by #19213, but not fixing it. This is mostly paranoia that we don't do stuff on inodes that aren't actually ttys. --- src/basic/terminal-util.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/basic/terminal-util.c b/src/basic/terminal-util.c index 91c404892ea..9bc6bb2024f 100644 --- a/src/basic/terminal-util.c +++ b/src/basic/terminal-util.c @@ -240,9 +240,11 @@ int reset_terminal_fd(int fd, bool switch_to_text) { assert(fd >= 0); - /* We leave locked terminal attributes untouched, so that - * Plymouth may set whatever it wants to set, and we don't - * interfere with that. */ + if (isatty(fd) < 1) + return log_debug_errno(errno, "Asked to reset a terminal that actually isn't a terminal: %m"); + + /* We leave locked terminal attributes untouched, so that Plymouth may set whatever it wants to set, + * and we don't interfere with that. */ /* Disable exclusive mode, just in case */ if (ioctl(fd, TIOCNXCL) < 0) @@ -1329,6 +1331,9 @@ int vt_restore(int fd) { }; int r, q = 0; + if (isatty(fd) < 1) + return log_debug_errno(errno, "Asked to restore the VT for an fd that does not refer to a terminal: %m"); + if (ioctl(fd, KDSETMODE, KD_TEXT) < 0) q = log_debug_errno(errno, "Failed to set VT in text mode, ignoring: %m"); @@ -1362,6 +1367,9 @@ int vt_release(int fd, bool restore) { * sent by the kernel and optionally reset the VT in text and auto * VT-switching modes. */ + if (isatty(fd) < 1) + return log_debug_errno(errno, "Asked to release the VT for an fd that does not refer to a terminal: %m"); + if (ioctl(fd, VT_RELDISP, 1) < 0) return -errno; -- 2.47.3