From owner-svn-src-head@freebsd.org Mon May 23 01:01:25 2016 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 107B7B45682; Mon, 23 May 2016 01:01:25 +0000 (UTC) (envelope-from truckman@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id BE59419D7; Mon, 23 May 2016 01:01:24 +0000 (UTC) (envelope-from truckman@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id u4N11NIV098623; Mon, 23 May 2016 01:01:23 GMT (envelope-from truckman@FreeBSD.org) Received: (from truckman@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id u4N11Nr9098622; Mon, 23 May 2016 01:01:23 GMT (envelope-from truckman@FreeBSD.org) Message-Id: <201605230101.u4N11Nr9098622@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: truckman set sender to truckman@FreeBSD.org using -f From: Don Lewis Date: Mon, 23 May 2016 01:01:23 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r300442 - head/bin/sh X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 23 May 2016 01:01:25 -0000 Author: truckman Date: Mon May 23 01:01:23 2016 New Revision: 300442 URL: https://svnweb.freebsd.org/changeset/base/300442 Log: 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 MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D6482 Modified: head/bin/sh/parser.c Modified: head/bin/sh/parser.c ============================================================================== --- head/bin/sh/parser.c Mon May 23 00:58:52 2016 (r300441) +++ head/bin/sh/parser.c Mon May 23 01:01:23 2016 (r300442) @@ -1998,7 +1998,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) { @@ -2011,7 +2011,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)) @@ -2061,8 +2062,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