Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/lib/libutil Prevent underflow buffer read in trim_whitespace...



details:   https://anonhg.NetBSD.org/src/rev/86a266ce8b38
branches:  trunk
changeset: 320135:86a266ce8b38
user:      kamil <kamil%NetBSD.org@localhost>
date:      Sun Jun 24 01:53:14 2018 +0000

description:
Prevent underflow buffer read in trim_whitespace() in libutil/passwd.c

If a string is empty or contains only white characters, the algorithm of
removal of white characters at the end of the passed string will read
buffer at index -1 and keep iterating backward.

Detected with MKSANITIZER/ASan when executing passwd(1).

diffstat:

 lib/libutil/passwd.c |  14 +++++++++++---
 1 files changed, 11 insertions(+), 3 deletions(-)

diffs (41 lines):

diff -r 6ae38d670839 -r 86a266ce8b38 lib/libutil/passwd.c
--- a/lib/libutil/passwd.c      Sat Jun 23 22:35:29 2018 +0000
+++ b/lib/libutil/passwd.c      Sun Jun 24 01:53:14 2018 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: passwd.c,v 1.52 2012/06/25 22:32:47 abs Exp $  */
+/*     $NetBSD: passwd.c,v 1.53 2018/06/24 01:53:14 kamil Exp $        */
 
 /*
  * Copyright (c) 1987, 1993, 1994, 1995
@@ -31,7 +31,7 @@
 
 #include <sys/cdefs.h>
 #if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: passwd.c,v 1.52 2012/06/25 22:32:47 abs Exp $");
+__RCSID("$NetBSD: passwd.c,v 1.53 2018/06/24 01:53:14 kamil Exp $");
 #endif /* LIBC_SCCS and not lint */
 
 #include <sys/types.h>
@@ -503,13 +503,21 @@
 
        _DIAGASSERT(line != NULL);
 
+       /* Handle empty string */
+       if (*line == '\0')
+               return;
+
        /* Remove leading spaces */
        p = line;
        while (isspace((unsigned char) *p))
                p++;
        memmove(line, p, strlen(p) + 1);
 
-       /* Remove trailing spaces */
+       /* Handle empty string after removal of whitespace characters */
+       if (*line == '\0')
+               return;
+
+       /* Remove trailing spaces, line must not be empty string here */
        p = line + strlen(line) - 1;
        while (isspace((unsigned char) *p))
                p--;



Home | Main Index | Thread Index | Old Index