Blame SOURCES/sudo-1.8.19p2-get_process_ttyname.patch

110381
diff -ru sudo-1.8.20/src/ttyname.c sudo-1.8.20-Q/src/ttyname.c
110381
--- sudo-1.8.20/src/ttyname.c	2017-05-10 08:38:44.000000000 -0700
110381
+++ sudo-1.8.20-Q/src/ttyname.c	2017-05-19 02:15:48.442705049 -0700
110381
@@ -1,5 +1,5 @@
110381
 /*
110381
- * Copyright (c) 2012-2016 Todd C. Miller <Todd.Miller@courtesan.com>
110381
+ * Copyright (c) 2012-2017 Todd C. Miller <Todd.Miller@courtesan.com>
110381
  *
110381
  * Permission to use, copy, modify, and distribute this software for any
110381
  * purpose with or without fee is hereby granted, provided that the above
110381
@@ -159,6 +159,8 @@
110381
 
110381
 static char *ignore_devs[] = {
110381
     "/dev/fd/",
110381
+    "/dev/mqueue/",
110381
+    "/dev/shm/",
110381
     "/dev/stdin",
110381
     "/dev/stdout",
110381
     "/dev/stderr",
110381
@@ -493,28 +495,35 @@
110381
 	len = getline(&line, &linesize, fp);
110381
 	fclose(fp);
110381
 	if (len != -1) {
110381
-	    /* Field 7 is the tty dev (0 if no tty) */
110381
-	    char *cp = line;
110381
-	    char *ep = line;
110381
-	    const char *errstr;
110381
-	    int field = 0;
110381
-	    while (*++ep != '\0') {
110381
-		if (*ep == ' ') {
110381
-		    *ep = '\0';
110381
-		    if (++field == 7) {
110381
-			dev_t tdev = strtonum(cp, INT_MIN, INT_MAX, &errstr);
110381
-			if (errstr) {
110381
-			    sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
110381
-				"%s: tty device %s: %s", path, cp, errstr);
110381
+	    /*
110381
+	     * Field 7 is the tty dev (0 if no tty).
110381
+	     * Since the process name at field 2 "(comm)" may include spaces,
110381
+	     * start at the last ')' found.
110381
+	     */
110381
+	    char *cp = strrchr(line, ')');
110381
+	    if (cp != NULL) {
110381
+		char *ep = cp;
110381
+		const char *errstr;
110381
+		int field = 1;
110381
+
110381
+		while (*++ep != '\0') {
110381
+		    if (*ep == ' ') {
110381
+			*ep = '\0';
110381
+			if (++field == 7) {
110381
+			    dev_t tdev = strtonum(cp, INT_MIN, INT_MAX, &errstr);
110381
+			    if (errstr) {
110381
+				sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
110381
+				    "%s: tty device %s: %s", path, cp, errstr);
110381
+			    }
110381
+			    if (tdev > 0) {
110381
+				errno = serrno;
110381
+				ret = sudo_ttyname_dev(tdev, name, namelen);
110381
+				goto done;
110381
+			    }
110381
+			    break;
110381
 			}
110381
-			if (tdev > 0) {
110381
-			    errno = serrno;
110381
-			    ret = sudo_ttyname_dev(tdev, name, namelen);
110381
-			    goto done;
110381
-			}
110381
-			break;
110381
+			cp = ep + 1;
110381
 		    }
110381
-		    cp = ep + 1;
110381
 		}
110381
 	    }
110381
 	}
110381