diff options
author | truckman <truckman@FreeBSD.org> | 2016-06-01 17:09:50 +0000 |
---|---|---|
committer | truckman <truckman@FreeBSD.org> | 2016-06-01 17:09:50 +0000 |
commit | 841ce9904cc8019ed9d546625507a029293380a6 (patch) | |
tree | 5adc89fbc9b6f59ebe6264a1faeae0cacb880db4 /bin/sh | |
parent | f28839d2d76920726759ae0f3b79fb01d250bf9b (diff) | |
download | FreeBSD-src-841ce9904cc8019ed9d546625507a029293380a6.zip FreeBSD-src-841ce9904cc8019ed9d546625507a029293380a6.tar.gz |
MFC r300442
Hopefully fix Coverity CID 1008328 (Out-of-bounds write) in /bin/sh.
Replace the magic constant 127 in the loop interation count with
"PROMPTLEN - 1".
gethostname() is not guaranteed to NUL terminate the destination
string if it is too short. Decrease the length passed to gethostname()
by one, and add a NUL at the end of the buffer to make sure the
following loop to find the end of the name properly terminates.
The default: case is the likely cause of Coverity CID 1008328. If
i is 126 at the top of the loop interation where the default case
is triggered, i will be incremented to 127 by the default case,
then incremented to 128 at the top of the loop before being compared
to 127 (PROMPTLENT - 1) and terminating the loop. Then the NUL
termination code after the loop will write to ps[128]. Fix by
checking for overflow before incrementing the index and storing the
second character in the buffer.
These fixes are not guaranteed to satisfy Coverity. The code that
increments i in the 'h'/'H' and 'w'/'W' cases may be beyond its
capability to analyze, but the code appears to be safe.
Reported by: Coverity
CID: 1008328
Reviewed by: jilles, cem
Differential Revision: https://reviews.freebsd.org/D6482
Diffstat (limited to 'bin/sh')
-rw-r--r-- | bin/sh/parser.c | 10 |
1 files changed, 6 insertions, 4 deletions
diff --git a/bin/sh/parser.c b/bin/sh/parser.c index a5679b8..70a4979 100644 --- a/bin/sh/parser.c +++ b/bin/sh/parser.c @@ -1974,7 +1974,7 @@ getprompt(void *unused __unused) /* * Format prompt string. */ - for (i = 0; (i < 127) && (*fmt != '\0'); i++, fmt++) + for (i = 0; (i < PROMPTLEN - 1) && (*fmt != '\0'); i++, fmt++) if (*fmt == '\\') switch (*++fmt) { @@ -1987,7 +1987,8 @@ getprompt(void *unused __unused) case 'h': case 'H': ps[i] = '\0'; - gethostname(&ps[i], PROMPTLEN - i); + gethostname(&ps[i], PROMPTLEN - i - 1); + ps[PROMPTLEN - 1] = '\0'; /* Skip to end of hostname. */ trim = (*fmt == 'h') ? '.' : '\0'; while ((ps[i] != '\0') && (ps[i] != trim)) @@ -2037,8 +2038,9 @@ getprompt(void *unused __unused) * Emit unrecognized formats verbatim. */ default: - ps[i++] = '\\'; - ps[i] = *fmt; + ps[i] = '\\'; + if (i < PROMPTLEN - 1) + ps[++i] = *fmt; break; } else |