From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 3894D3858D35 for ; Tue, 21 Feb 2023 21:10:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3894D3858D35 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1677013850; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=3ODmuTtuVK4KRokuo4K3ocBbpPLMZ8/sq/7rnp9F2RI=; b=QpR0D6nYVSgQgVhlBofjXK2H8fMguvD+qTgvhiIF2VwiZsKqzGG8bI5v3RHG5cOGpxpTux CksCIsn7Dob8HZebnrQRTWWNMPgL0CB5nvWxuqkUSNQVJWc4LaFsLSbxv1U3rth5LILx8u Mz0lUb+Pvnbid+9LHN0MvuxYXNqBFVQ= Received: from mail-io1-f70.google.com (mail-io1-f70.google.com [209.85.166.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-194-kzYL39QrNGGd2lfxpSBd_g-1; Tue, 21 Feb 2023 16:10:49 -0500 X-MC-Unique: kzYL39QrNGGd2lfxpSBd_g-1 Received: by mail-io1-f70.google.com with SMTP id i70-20020a6b3b49000000b0074c7f0b085dso2025007ioa.22 for ; Tue, 21 Feb 2023 13:10:49 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:organization:from:references :to:content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=3ODmuTtuVK4KRokuo4K3ocBbpPLMZ8/sq/7rnp9F2RI=; b=XLc0fbfBCY0pfI+y3MP8rqnKLH8sjJBvXVs2T/vySbGFG1bx2QicIphZVu9kit2vGo tn/l05PnuOu7zMDrvaEhJV0D7uoGVqNGLITJM5x7v9drBisPKGKXPqK1QsfenI12jvm1 odKcqgAZ7J6bgcl51HQbjvd5jjrk+9OQXRFJrcz2d3AJMKV/14VBT+Mva/bzLzTyZe9i NsX1gBseit9JxAs1YgSRRcrrPfaIvhbp4XQV1ZR0oT8IDaai8FatwqfS37WympQKTO7B h8Jh17ZBA3cEMn4P3XxcbM931wWfO/LSOTy59OBTgjRcY1Sppk0cQqxC8WYn3wpG68uU GRDA== X-Gm-Message-State: AO0yUKWPCHmxbOQZx5wY47yE2ouLxiqkEZuzSUExJ2yUeif9wjzrhuFJ CwBrTt3E6dc4nrrLsXJX9r8P4xzLXM9mUfaHNRzJAiUnvkQ+CG8SRLTjgvRVdUulOSLqz7rxOGr 04VTLadJHou+IvQ4iiGLwgr5ZCQ== X-Received: by 2002:a5e:c24d:0:b0:74c:8dc7:aa1 with SMTP id w13-20020a5ec24d000000b0074c8dc70aa1mr1037213iop.17.1677013848702; Tue, 21 Feb 2023 13:10:48 -0800 (PST) X-Google-Smtp-Source: AK7set9Dk7SrV9UWT1TmKTR6rp3n2VqVsp9rYGoxRszSa3d5J78CMR/wr4ziZmj7lAzGWNUq4FpI/Q== X-Received: by 2002:a5e:c24d:0:b0:74c:8dc7:aa1 with SMTP id w13-20020a5ec24d000000b0074c8dc70aa1mr1037164iop.17.1677013846945; Tue, 21 Feb 2023 13:10:46 -0800 (PST) Received: from [192.168.0.241] ([198.48.244.52]) by smtp.gmail.com with ESMTPSA id k23-20020a5d91d7000000b007455d5fb77asm405994ior.11.2023.02.21.13.10.46 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 21 Feb 2023 13:10:46 -0800 (PST) Message-ID: Date: Tue, 21 Feb 2023 16:10:45 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.6.0 Subject: Re: [PATCH v2] gmon: Fix allocated buffer overflow (bug 2944) To: DJ Delorie , libc-alpha@sourceware.org References: From: Carlos O'Donell Organization: Red Hat In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-11.4 required=5.0 tests=BAYES_00,BODY_8BITS,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,NICE_REPLY_A,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On 2/14/23 00:59, DJ Delorie via Libc-alpha wrote: > > Not getting any feedback from my review-with-questions, I'm posting a > patch that assumes I'm right about the change I thought Leonid's patch > needed. Fight! ;-) LGTM. You can keep my Reviewed-by: if you make only the spelling and subject fix. Please do the following: - Post a v3 with my Reviewed-by: included. - Push v3. - Mark v2 superseded in patchwork. Thank you! Reviewed-by: Carlos O'Donell > From f7fddb025720d4e318fe5425f5a3235e73a13282 Mon Sep 17 00:00:00 2001 > From: "Леонид Юрьев (Leonid Yuriev)" > Date: Sat, 4 Feb 2023 14:41:38 +0300 > Subject: gmon: Fix allocated buffer overflow (bug 2944) s/bug 2944/bug 29444/g > MIME-Version: 1.0 > Content-Type: text/plain; charset=UTF-8 > Content-Transfer-Encoding: 8bit > > 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 instuctions and/or callable functions s/instuctions/instructions/g > 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)); OK. Agreed. The size must be rounded to the size of the arc indices. > > 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. OK. > > Co-authored-by: DJ Delorie > > 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)); OK. Agreed. > 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)); OK. Agreed, the size you want is the rounded up size during __monstartup() calculation. - Eventually we call calloc later in the function to allocate this size. In other places we compute like this: 268 from_len = _gmonparam.fromssize / sizeof (*_gmonparam.froms); - So it's expected that the allocations are multiples of sizeof(*p->froms). > p->tolimit = p->textsize * ARCDENSITY / 100; > if (p->tolimit < MINARCS) > p->tolimit = MINARCS; > -- Cheers, Carlos.