From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2178) id 2AD843858C52; Fri, 28 Apr 2023 17:23:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2AD843858C52 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1682702611; bh=6CdGCR6pLZn9KHICSo27dHDDNmmly2hsqcyjjwdVywM=; h=From:To:Subject:Date:From; b=LIZyYuQuc+xJ4e10wOg6JdDl26rpEuYIo24j+py1/5QQ7ePIh50TT/PKPkOgVc+lu 3undKHF2MIeQmcJqrGj6khMT9JKdebTdPUDBe+gl1CCya4ThqyQY1B5jnCHWw6LVLX eYcHW9iHdrjVwGVglFLbaW3UZ/w8EBQVR+lPGdiQ= MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="utf-8" From: Florian Weimer To: glibc-cvs@sourceware.org Subject: [glibc/release/2.34/master] gmon: Fix allocated buffer overflow (bug 29444) X-Act-Checkin: glibc X-Git-Author: =?utf-8?b?0JvQtdC+0L3QuNC0INCu0YDRjNC10LIgKExlb25pZCBZdXJpZXYp?= X-Git-Refname: refs/heads/release/2.34/master X-Git-Oldrev: 567f7413fb1179acd936766b542ea72f7ef60d8b X-Git-Newrev: 8e1a8e04b153739a77289e6fc07cbfc252d87e02 Message-Id: <20230428172331.2AD843858C52@sourceware.org> Date: Fri, 28 Apr 2023 17:23:31 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=8e1a8e04b153739a77289e6fc07cbfc252d87e02 commit 8e1a8e04b153739a77289e6fc07cbfc252d87e02 Author: Леонид Юрьев (Leonid Yuriev) Date: Sat Feb 4 14:41:38 2023 +0300 gmon: Fix allocated buffer overflow (bug 29444) The `__monstartup()` allocates a buffer used to store all the data accumulated by the monitor. The size of this buffer depends on the size of the internal structures used and the address range for which the monitor is activated, as well as on the maximum density of call instructions and/or callable functions that could be potentially on a segment of executable code. In particular a hash table of arcs is placed at the end of this buffer. The size of this hash table is calculated in bytes as p->fromssize = p->textsize / HASHFRACTION; but actually should be p->fromssize = ROUNDUP(p->textsize / HASHFRACTION, sizeof(*p->froms)); This results in writing beyond the end of the allocated buffer when an added arc corresponds to a call near from the end of the monitored address range, since `_mcount()` check the incoming caller address for monitored range but not the intermediate result hash-like index that uses to write into the table. It should be noted that when the results are output to `gmon.out`, the table is read to the last element calculated from the allocated size in bytes, so the arcs stored outside the buffer boundary did not fall into `gprof` for analysis. Thus this "feature" help me to found this bug during working with https://sourceware.org/bugzilla/show_bug.cgi?id=29438 Just in case, I will explicitly note that the problem breaks the `make test t=gmon/tst-gmon-dso` added for Bug 29438. There, the arc of the `f3()` call disappears from the output, since in the DSO case, the call to `f3` is located close to the end of the monitored range. Signed-off-by: Леонид Юрьев (Leonid Yuriev) Another minor error seems a related typo in the calculation of `kcountsize`, but since kcounts are smaller than froms, this is actually to align the p->froms data. Co-authored-by: DJ Delorie Reviewed-by: Carlos O'Donell (cherry picked from commit 801af9fafd4689337ebf27260aa115335a0cb2bc) Diff: --- NEWS | 1 + gmon/gmon.c | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/NEWS b/NEWS index 01f05ce75e..73bf72c2a5 100644 --- a/NEWS +++ b/NEWS @@ -127,6 +127,7 @@ The following bugs are resolved with this release: [29305] Conserve NSS buffer space during DNS packet parsing [29402] nscd: nscd: No such file or directory [29415] nscd: Fix netlink cache invalidation if epoll is used + [29444] gmon: Fix allocated buffer overflow (bug 29444) [29446] _dlopen now ignores dl_caller argument in static mode [29490] alpha: New __brk_call implementation is broken [29528] elf: Call __libc_early_init for reused namespaces diff --git a/gmon/gmon.c b/gmon/gmon.c index dee64803ad..bf76358d5b 100644 --- a/gmon/gmon.c +++ b/gmon/gmon.c @@ -132,6 +132,8 @@ __monstartup (u_long lowpc, u_long highpc) p->lowpc = ROUNDDOWN(lowpc, HISTFRACTION * sizeof(HISTCOUNTER)); p->highpc = ROUNDUP(highpc, HISTFRACTION * sizeof(HISTCOUNTER)); p->textsize = p->highpc - p->lowpc; + /* This looks like a typo, but it's here to align the p->froms + section. */ p->kcountsize = ROUNDUP(p->textsize / HISTFRACTION, sizeof(*p->froms)); p->hashfraction = HASHFRACTION; p->log_hashfraction = -1; @@ -142,7 +144,7 @@ __monstartup (u_long lowpc, u_long highpc) instead of integer division. Precompute shift amount. */ p->log_hashfraction = ffs(p->hashfraction * sizeof(*p->froms)) - 1; } - p->fromssize = p->textsize / HASHFRACTION; + p->fromssize = ROUNDUP(p->textsize / HASHFRACTION, sizeof(*p->froms)); p->tolimit = p->textsize * ARCDENSITY / 100; if (p->tolimit < MINARCS) p->tolimit = MINARCS;