]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
terminal-util: add extra validity checks that we operate on a TTY before doing so
authorLennart Poettering <lennart@poettering.net>
Wed, 12 May 2021 11:45:48 +0000 (13:45 +0200)
committerLennart Poettering <lennart@poettering.net>
Wed, 19 May 2021 14:53:50 +0000 (16:53 +0200)
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

index 91c404892ea636eed1472159cc6ee26bd2c20365..9bc6bb2024f8f73115fa7ba67b453d8e03c38e89 100644 (file)
@@ -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;