From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1944) id 20B8938582A6; Wed, 2 Nov 2022 15:43:39 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 20B8938582A6 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1667403820; bh=fFIJOS+xAvUc6laBjRvUNFZeaTeRBWb/k69u65TUxII=; h=From:To:Subject:Date:From; b=IQ01vZUDrY5zGuIKQWT/0ACotpfb0phqhfEdJ0/weeOXy9fe4nJ8Zn/06Okjgx0Zq pbfew0wEScVfS4LOgBMqSIxfLuiyzcUn8jkbApwKPWKU2qLL8XxrzU0o3lIuLF2RfF ImgIkYBs/FBWGGvWcXcaQFntmpCQXickcpCd2OpE= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Szabolcs Nagy To: glibc-cvs@sourceware.org Subject: [glibc] Fix OOB read in stdlib thousand grouping parsing [BZ #29727] X-Act-Checkin: glibc X-Git-Author: Szabolcs Nagy X-Git-Refname: refs/heads/master X-Git-Oldrev: 7457b7eef8dfe8cc48e55b9f9837df6dd397b80d X-Git-Newrev: 17bfe5954baee1f18672aea94caa1126ec36fb81 Message-Id: <20221102154340.20B8938582A6@sourceware.org> Date: Wed, 2 Nov 2022 15:43:39 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=17bfe5954baee1f18672aea94caa1126ec36fb81 commit 17bfe5954baee1f18672aea94caa1126ec36fb81 Author: Szabolcs Nagy Date: Tue Oct 11 15:24:41 2022 +0100 Fix OOB read in stdlib thousand grouping parsing [BZ #29727] __correctly_grouped_prefixmb only worked with thousands_len == 1, otherwise it read past the end of cp or thousands. This affects scanf formats like %'d, %'f and the internal but exposed __strto{l,ul,f,d,..}_internal with grouping flag set and an LC_NUMERIC locale where thousands_len > 1. Avoid OOB access by considering thousands_len when initializing cp. This fixes bug 29727. Found by the morello port with strict bounds checking where FAIL: stdlib/tst-strtod4 FAIL: stdlib/tst-strtod5i crashed using a locale with thousands_len==3. Diff: --- stdlib/grouping.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/stdlib/grouping.c b/stdlib/grouping.c index be7922f5fd..06cbe7b9c7 100644 --- a/stdlib/grouping.c +++ b/stdlib/grouping.c @@ -52,21 +52,19 @@ __correctly_grouped_prefixmb (const STRING_TYPE *begin, const STRING_TYPE *end, #endif const char *grouping) { -#ifndef USE_WIDE_CHAR - size_t thousands_len; - int cnt; -#endif - if (grouping == NULL) return end; -#ifndef USE_WIDE_CHAR - thousands_len = strlen (thousands); +#ifdef USE_WIDE_CHAR + size_t thousands_len = 1; +#else + size_t thousands_len = strlen (thousands); + int cnt; #endif - while (end > begin) + while (end - begin >= thousands_len) { - const STRING_TYPE *cp = end - 1; + const STRING_TYPE *cp = end - thousands_len; const char *gp = grouping; /* Check first group. */