]> granicus.if.org Git - sudo/commitdiff
Avoid sign extension when assigning the value of tty_nr in
authorTodd C. Miller <Todd.Miller@courtesan.com>
Sat, 3 Jun 2017 14:45:29 +0000 (08:45 -0600)
committerTodd C. Miller <Todd.Miller@courtesan.com>
Sat, 3 Jun 2017 14:45:29 +0000 (08:45 -0600)
/proc/self/stat on Linux.  It is an unsigned int value that
is printed as a signed int but dev_t is unsigned long long.
We need to cast to unsigned int before assigning to a dev_t.

src/ttyname.c

index 8c6fdc88df740a8b12f6ada3e349cc47defd1771..f3a427c177c569667da2a3b20cf29973f0a8731a 100644 (file)
@@ -496,12 +496,19 @@ get_process_ttyname(char *name, size_t namelen)
                    if (*ep == ' ') {
                        *ep = '\0';
                        if (++field == 7) {
-                           dev_t tdev = strtonum(cp, INT_MIN, INT_MAX, &errstr);
+                           int tty_nr = strtonum(cp, INT_MIN, INT_MAX, &errstr);
                            if (errstr) {
                                sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
                                    "%s: tty device %s: %s", path, cp, errstr);
                            }
-                           if (tdev > 0) {
+                           if (tty_nr != 0) {
+                               /*
+                                * Avoid sign extension when assigning tdev.
+                                * tty_nr in /proc/self/stat is printed as a
+                                * signed int but the actual device number is an
+                                * unsigned int and dev_t is unsigned long long.
+                                */
+                               dev_t tdev = (unsigned int)tty_nr;
                                errno = serrno;
                                ret = sudo_ttyname_dev(tdev, name, namelen);
                                goto done;