Skip to content

Commit 817fd28

Browse files
committedMay 29, 2017
Fix for CVE-2017-1000367, parsing of /proc/pid/stat on Linux when
the process name contains spaces. Since the user has control over the command name this could be used by a user with sudo access to overwrite an arbitrary file. Thanks to Qualys for investigating and reporting this bug. Also stop performing a breadth-first traversal of /dev when looking for the device. Only the directories specified in search_devs[] are checked. --HG-- branch : 1.8
1 parent f85280d commit 817fd28

File tree

1 file changed

+60
-81
lines changed

1 file changed

+60
-81
lines changed
 

‎src/ttyname.c

Lines changed: 60 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012-2016 Todd C. Miller <Todd.Miller@courtesan.com>
2+
* Copyright (c) 2012-2017 Todd C. Miller <Todd.Miller@courtesan.com>
33
*
44
* Permission to use, copy, modify, and distribute this software for any
55
* purpose with or without fee is hereby granted, provided that the above
@@ -145,37 +145,41 @@ sudo_ttyname_dev(dev_t tdev, char *name, size_t namelen)
145145
}
146146
#elif defined(HAVE_STRUCT_PSINFO_PR_TTYDEV) || defined(HAVE_PSTAT_GETPROC) || defined(__linux__)
147147
/*
148-
* Devices to search before doing a breadth-first scan.
148+
* Device nodes and directories to search before searching all of /dev
149149
*/
150150
static char *search_devs[] = {
151151
"/dev/console",
152-
"/dev/wscons",
153-
"/dev/pts/",
154-
"/dev/vt/",
155-
"/dev/term/",
156-
"/dev/zcons/",
152+
"/dev/pts/", /* POSIX pty */
153+
"/dev/vt/", /* Solaris virtual console */
154+
"/dev/term/", /* Solaris serial ports */
155+
"/dev/zcons/", /* Solaris zone console */
156+
"/dev/pty/", /* HP-UX old-style pty */
157157
NULL
158158
};
159159

160+
/*
161+
* Device nodes to ignore when searching all of /dev
162+
*/
160163
static char *ignore_devs[] = {
161-
"/dev/fd/",
162164
"/dev/stdin",
163165
"/dev/stdout",
164166
"/dev/stderr",
165167
NULL
166168
};
167169

168170
/*
169-
* Do a breadth-first scan of dir looking for the specified device.
171+
* Do a scan of a directory looking for the specified device.
172+
* Does not descend into subdirectories.
170173
* Returns name on success and NULL on failure, setting errno.
171174
*/
172175
static char *
173-
sudo_ttyname_scan(const char *dir, dev_t rdev, bool builtin, char *name, size_t namelen)
176+
sudo_ttyname_scan(const char *dir, dev_t rdev, char *name, size_t namelen)
174177
{
175-
size_t sdlen, num_subdirs = 0, max_subdirs = 0;
176-
char pathbuf[PATH_MAX], **subdirs = NULL;
178+
size_t sdlen;
179+
char pathbuf[PATH_MAX];
177180
char *ret = NULL;
178181
struct dirent *dp;
182+
struct stat sb;
179183
unsigned int i;
180184
DIR *d = NULL;
181185
debug_decl(sudo_ttyname_scan, SUDO_DEBUG_UTIL)
@@ -187,6 +191,18 @@ sudo_ttyname_scan(const char *dir, dev_t rdev, bool builtin, char *name, size_t
187191
if ((d = opendir(dir)) == NULL)
188192
goto done;
189193

194+
if (fstat(dirfd(d), &sb) == -1) {
195+
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
196+
"unable to fstat %s", dir);
197+
goto done;
198+
}
199+
if ((sb.st_mode & S_IWOTH) != 0) {
200+
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
201+
"ignoring world-writable directory %s", dir);
202+
errno = ENOENT;
203+
goto done;
204+
}
205+
190206
sudo_debug_printf(SUDO_DEBUG_INFO|SUDO_DEBUG_LINENO,
191207
"scanning for dev %u in %s", (unsigned int)rdev, dir);
192208

@@ -224,18 +240,6 @@ sudo_ttyname_scan(const char *dir, dev_t rdev, bool builtin, char *name, size_t
224240
}
225241
if (ignore_devs[i] != NULL)
226242
continue;
227-
if (!builtin) {
228-
/* Skip entries in search_devs; we already checked them. */
229-
for (i = 0; search_devs[i] != NULL; i++) {
230-
len = strlen(search_devs[i]);
231-
if (search_devs[i][len - 1] == '/')
232-
len--;
233-
if (d_len == len && strncmp(pathbuf, search_devs[i], len) == 0)
234-
break;
235-
}
236-
if (search_devs[i] != NULL)
237-
continue;
238-
}
239243
# if defined(HAVE_STRUCT_DIRENT_D_TYPE) && defined(DTTOIF)
240244
/*
241245
* Avoid excessive stat() calls by checking dp->d_type.
@@ -248,39 +252,14 @@ sudo_ttyname_scan(const char *dir, dev_t rdev, bool builtin, char *name, size_t
248252
if (stat(pathbuf, &sb) == -1)
249253
continue;
250254
break;
251-
case DT_DIR:
252-
/* Directory, no need to stat() it. */
253-
sb.st_mode = DTTOIF(dp->d_type);
254-
sb.st_rdev = 0; /* quiet ccc-analyzer false positive */
255-
break;
256255
default:
257-
/* Not a character device, link or directory, skip it. */
256+
/* Not a character device or link, skip it. */
258257
continue;
259258
}
260259
# else
261260
if (stat(pathbuf, &sb) == -1)
262261
continue;
263262
# endif
264-
if (S_ISDIR(sb.st_mode)) {
265-
if (!builtin) {
266-
/* Add to list of subdirs to search. */
267-
if (num_subdirs + 1 > max_subdirs) {
268-
char **new_subdirs;
269-
270-
new_subdirs = reallocarray(subdirs, max_subdirs + 64,
271-
sizeof(char *));
272-
if (new_subdirs == NULL)
273-
goto done;
274-
subdirs = new_subdirs;
275-
max_subdirs += 64;
276-
}
277-
subdirs[num_subdirs] = strdup(pathbuf);
278-
if (subdirs[num_subdirs] == NULL)
279-
goto done;
280-
num_subdirs++;
281-
}
282-
continue;
283-
}
284263
if (S_ISCHR(sb.st_mode) && sb.st_rdev == rdev) {
285264
sudo_debug_printf(SUDO_DEBUG_INFO|SUDO_DEBUG_LINENO,
286265
"resolved dev %u as %s", (unsigned int)rdev, pathbuf);
@@ -296,16 +275,9 @@ sudo_ttyname_scan(const char *dir, dev_t rdev, bool builtin, char *name, size_t
296275
}
297276
}
298277

299-
/* Search subdirs if we didn't find it in the root level. */
300-
for (i = 0; ret == NULL && i < num_subdirs; i++)
301-
ret = sudo_ttyname_scan(subdirs[i], rdev, false, name, namelen);
302-
303278
done:
304279
if (d != NULL)
305280
closedir(d);
306-
for (i = 0; i < num_subdirs; i++)
307-
free(subdirs[i]);
308-
free(subdirs);
309281
debug_return_str(ret);
310282
}
311283

@@ -324,7 +296,7 @@ sudo_ttyname_dev(dev_t rdev, char *name, size_t namelen)
324296
debug_decl(sudo_ttyname_dev, SUDO_DEBUG_UTIL)
325297

326298
/*
327-
* First check search_devs for common tty devices.
299+
* First check search_devs[] for common tty devices.
328300
*/
329301
for (sd = search_devs; (devname = *sd) != NULL; sd++) {
330302
len = strlen(devname);
@@ -349,7 +321,7 @@ sudo_ttyname_dev(dev_t rdev, char *name, size_t namelen)
349321
"comparing dev %u to %s: no", (unsigned int)rdev, buf);
350322
} else {
351323
/* Traverse directory */
352-
ret = sudo_ttyname_scan(devname, rdev, true, name, namelen);
324+
ret = sudo_ttyname_scan(devname, rdev, name, namelen);
353325
if (ret != NULL || errno == ENOMEM)
354326
goto done;
355327
}
@@ -367,9 +339,9 @@ sudo_ttyname_dev(dev_t rdev, char *name, size_t namelen)
367339
}
368340

369341
/*
370-
* Not found? Do a breadth-first traversal of /dev/.
342+
* Not found? Check all device nodes in /dev.
371343
*/
372-
ret = sudo_ttyname_scan(_PATH_DEV, rdev, false, name, namelen);
344+
ret = sudo_ttyname_scan(_PATH_DEV, rdev, name, namelen);
373345

374346
done:
375347
debug_return_str(ret);
@@ -493,28 +465,35 @@ get_process_ttyname(char *name, size_t namelen)
493465
len = getline(&line, &linesize, fp);
494466
fclose(fp);
495467
if (len != -1) {
496-
/* Field 7 is the tty dev (0 if no tty) */
497-
char *cp = line;
498-
char *ep = line;
499-
const char *errstr;
500-
int field = 0;
501-
while (*++ep != '\0') {
502-
if (*ep == ' ') {
503-
*ep = '\0';
504-
if (++field == 7) {
505-
dev_t tdev = strtonum(cp, INT_MIN, INT_MAX, &errstr);
506-
if (errstr) {
507-
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
508-
"%s: tty device %s: %s", path, cp, errstr);
509-
}
510-
if (tdev > 0) {
511-
errno = serrno;
512-
ret = sudo_ttyname_dev(tdev, name, namelen);
513-
goto done;
468+
/*
469+
* Field 7 is the tty dev (0 if no tty).
470+
* Since the process name at field 2 "(comm)" may include spaces,
471+
* start at the last ')' found.
472+
*/
473+
char *cp = strrchr(line, ')');
474+
if (cp != NULL) {
475+
char *ep = cp;
476+
const char *errstr;
477+
int field = 1;
478+
479+
while (*++ep != '\0') {
480+
if (*ep == ' ') {
481+
*ep = '\0';
482+
if (++field == 7) {
483+
dev_t tdev = strtonum(cp, INT_MIN, INT_MAX, &errstr);
484+
if (errstr) {
485+
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
486+
"%s: tty device %s: %s", path, cp, errstr);
487+
}
488+
if (tdev > 0) {
489+
errno = serrno;
490+
ret = sudo_ttyname_dev(tdev, name, namelen);
491+
goto done;
492+
}
493+
break;
514494
}
515-
break;
495+
cp = ep + 1;
516496
}
517-
cp = ep + 1;
518497
}
519498
}
520499
}

0 commit comments

Comments
 (0)
Please sign in to comment.