[PATCH] w: check that the tty is at minimum a char device

"Thorsten Kukuk" <[email protected]> ("kukuk") Wed, 7 Jun 2023 09:11:51 +0000
Newsgroups gmane.linux.procps.devel
Message-ID <[email protected]>
--VbJkn9YxBvnuCH5J
Content-Type: text/plain; charset=iso-8859-1
Content-Disposition: inline
Content-Transfer-Encoding: 8bit


Hi,

w makes a stat on the TTY (ut_line) from an utmp entry and assumes, that
the entry is a valid TTY.

I saw meanwhile several tools, who write nothing into this line or a
dummy string. The result are funny processes in the w output, since stat
returns a directory, not a device.

Not sure if we can make even better checks.

  Thorsten

-- 
Thorsten Kukuk, Distinguished Engineer, Senior Architect, Future Technologies
SUSE Software Solutions Germany GmbH, Frankenstraße 146, 90461 Nuernberg, Germany
Managing Director: Ivo Totev, Andrew Myers, Andrew McDonald, Martje Boudien Moerman
(HRB 36809, AG Nürnberg)

--VbJkn9YxBvnuCH5J
Content-Type: text/x-patch; charset=us-ascii
Content-Disposition: attachment; filename="0001-w-check-if-tty-is-at-least-a-character-device.patch"

From 37ae0952da88e638438c2126d436034da4d81011 Mon Sep 17 00:00:00 2001
From: Thorsten Kukuk <[email protected]>
Date: Wed, 7 Jun 2023 10:56:52 +0200
Subject: [PATCH 1/1] w: check if tty is at least a character device

Some processes write an empty ut_line or some dummy data into it.
Make sure the device is at least a character device and not only a
directory (e.g. /dev for empty entry) or something else.

Signed-off-by: Thorsten Kukuk <[email protected]>
---
 src/w.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/w.c b/src/w.c
index 093cfbc7..f3c05729 100644
--- a/src/w.c
+++ b/src/w.c
@@ -343,7 +343,7 @@ static int get_tty_device(const char *restrict const name)
 
     for (i=0; dev_paths[i] != NULL; i++) {
         snprintf(buf, 32, dev_paths[i], name);
-        if (stat(buf, &st) == 0)
+        if (stat(buf, &st) == 0 && (st.st_mode & S_IFMT) == S_IFCHR)
             return st.st_rdev;
     }
     return -1;
-- 
2.40.1


--VbJkn9YxBvnuCH5J--