From owner-freebsd-bugs@freebsd.org Tue Jan 12 22:40:10 2016 Return-Path: Delivered-To: freebsd-bugs@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 2D6FBA6D195 for ; Tue, 12 Jan 2016 22:40:10 +0000 (UTC) (envelope-from bugzilla-noreply@freebsd.org) Received: from kenobi.freebsd.org (kenobi.freebsd.org [IPv6:2001:1900:2254:206a::16:76]) (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 14510149D for ; Tue, 12 Jan 2016 22:40:10 +0000 (UTC) (envelope-from bugzilla-noreply@freebsd.org) Received: from bugs.freebsd.org ([127.0.1.118]) by kenobi.freebsd.org (8.15.2/8.15.2) with ESMTP id u0CMe9SK067029 for ; Tue, 12 Jan 2016 22:40:09 GMT (envelope-from bugzilla-noreply@freebsd.org) From: bugzilla-noreply@freebsd.org To: freebsd-bugs@FreeBSD.org Subject: [Bug 206177] Out-of-bounds read in wcsncat(3) Date: Tue, 12 Jan 2016 22:40:10 +0000 X-Bugzilla-Reason: AssignedTo X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: Base System X-Bugzilla-Component: bin X-Bugzilla-Version: 11.0-CURRENT X-Bugzilla-Keywords: X-Bugzilla-Severity: Affects Some People X-Bugzilla-Who: cherepan@mccme.ru X-Bugzilla-Status: New X-Bugzilla-Resolution: X-Bugzilla-Priority: --- X-Bugzilla-Assigned-To: freebsd-bugs@FreeBSD.org X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version rep_platform op_sys bug_status bug_severity priority component assigned_to reporter attachments.created Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: https://bugs.freebsd.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: freebsd-bugs@freebsd.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: Bug reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 12 Jan 2016 22:40:10 -0000 https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=3D206177 Bug ID: 206177 Summary: Out-of-bounds read in wcsncat(3) Product: Base System Version: 11.0-CURRENT Hardware: Any OS: Any Status: New Severity: Affects Some People Priority: --- Component: bin Assignee: freebsd-bugs@FreeBSD.org Reporter: cherepan@mccme.ru Created attachment 165467 --> https://bugs.freebsd.org/bugzilla/attachment.cgi?id=3D165467&action= =3Dedit Patch The wcsncat function could read several bytes behind the end of its input buffer. This could lead to a crash if the buffer happens to immediately pre= cede an unmapped page. According to C11, 7.29.4.3.2, wcsncat(s1, s2, n) will append s2 to s1 but w= ill stop as soon as null wide char is met or n wide chars are read. In particul= ar, s2 is not required to be a null-terminated wide string. (The man page[1] for wcsncat refers to the man page[2] for strncat which erroneously talks about strings only. I filed [3] about it.) When s2 is an array containing exactly n non-null wide chars, FreeBSD implementation of wcsncat will read one extra wide char. The code[4] for traversing the source array: 50 r =3D s2; 51 while (*r && n) { 52 *q++ =3D *r++; 53 n--; 54 } "n" in the loop controlling expression makes sure that the loop is terminat= ed after n chars are copied but the dereference in "*r" happens before the "n" check. For example, there would be one dereference when n=3D0. Calling the function as wcsncat(s1, NULL, 0) will crash. (Formally speaking, passing NULL as s2 is invalid in C11, a valid crashing testcase is attached= .) To fix it, it's enough to swap the checks in the while loop (patch attached= ). Or all the code could be changed to match strncat. The issue has security consequences but the function is rarely used so seve= rity seems very low. Other BSDs are affected. [1] https://svnweb.freebsd.org/base/head/lib/libc/string/wmemchr.3?revision=3D2= 51069&view=3Dmarkup [2] https://svnweb.freebsd.org/base/head/lib/libc/string/strcat.3?revision=3D26= 2890&view=3Dmarkup [3] https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=3D206176 [4] https://svnweb.freebsd.org/base/head/lib/libc/string/wcsncat.c?revision=3D1= 88080&view=3Dmarkup#l50 --=20 You are receiving this mail because: You are the assignee for the bug.=