From patchwork Thu Feb 9 13:19:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Kissane X-Patchwork-Id: 64533 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 00FA63858281 for ; Thu, 9 Feb 2023 13:21:42 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 00FA63858281 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1675948902; bh=MBYsGl9xCwHGwnsCtj+GFj4hrn7dDIqeZhpl/iKnlRk=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=nuUTYT1p1U/bwuC7XyuyuCdirWNdmgIM/+FN4QJpS+rdPrmAyF73xpiTQKsI9x2ps 1UH7cFtTejEMRf7cqKnKhU9NymDQHVb3CwaoFwD9QK8RAPuniRh5d6sxJc6Zdt7umO JII5d5F94Ammw+rl3N2if7kPg81ggrb8TzjbZkDQ= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pj1-x1034.google.com (mail-pj1-x1034.google.com [IPv6:2607:f8b0:4864:20::1034]) by sourceware.org (Postfix) with ESMTPS id 9C11E3858C50 for ; Thu, 9 Feb 2023 13:21:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9C11E3858C50 Received: by mail-pj1-x1034.google.com with SMTP id j1so2137432pjd.0 for ; Thu, 09 Feb 2023 05:21:18 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=MBYsGl9xCwHGwnsCtj+GFj4hrn7dDIqeZhpl/iKnlRk=; b=RrmUu8RMrjsPEmq9oUfy+SL0RlfwfFVb9wu0VaJWjtb59cUAVA5RoxYMt9WeBtRucY ayERvfkKvkUAbAWJbGODhOx/ZjKr8r6G98OZKTxk2T2GzC5kF9Ob9GSJB7oFtmeGXML3 cvipDNs1xqvCW+thE0hxmgcikTOnae70nNggFBdHPzmsurDYttliN7OnEC+8UHrnYN72 9ZunwwY2DSA4vPcgwqwEQyzYN/LVoDAyArpYIPJEsg41+cz/Pf/onhgL2xQGVRNGLWPB nbm3ljVhoHfOtVe2ZG4qxxU4bR4X6ZjKPRRY+xHdjInSVZsRGLuzWs6TSuwvWAv9u10K HYww== X-Gm-Message-State: AO0yUKVadha48yuNTRYBIzZ/Kl8jrLGCbnDy1Q5YpZuOgIl/CBroAlsx VVtiS0GxavhSyeojPhuhmQbi7t3bvTG0EA== X-Google-Smtp-Source: AK7set+lgG6bLvi3yubPrSjrc0oPgolULaGfk9ku3UVq3g8/avm3rclKLsPF3Brk49+BlIkbX7rkbA== X-Received: by 2002:a17:903:2342:b0:196:3e25:16dc with SMTP id c2-20020a170903234200b001963e2516dcmr12321896plh.5.1675948876484; Thu, 09 Feb 2023 05:21:16 -0800 (PST) Received: from localhost.localdomain (2403-5807-276c-0-cc36-c46f-88c3-b021.ip6.aussiebb.net. [2403:5807:276c:0:cc36:c46f:88c3:b021]) by smtp.googlemail.com with ESMTPSA id x24-20020a63db58000000b004df4fbb9823sm1267552pgi.68.2023.02.09.05.21.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Feb 2023 05:21:15 -0800 (PST) To: libc-alpha@sourceware.org Cc: Simon Kissane Subject: [PATCH] gmon: fix memory corruption issues [BZ# 30101] Date: Fri, 10 Feb 2023 00:19:55 +1100 Message-Id: <20230209131955.935342-1-skissane@gmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Spam-Status: No, score=-10.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, 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 X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Simon Kissane via Libc-alpha From: Simon Kissane Reply-To: Simon Kissane Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" 1. Prevent double free, and reads from unallocated memory, when _mcleanup is (incorrectly) called two or more times in a row, without an intervening call to __monstartup; with this patch, the second and subsequent calls effectively become no-ops instead. While setting tos=NULL is minimal fix, safest action is to zero the whole gmonparam buffer. 2. Prevent memory leak when __monstartup is (incorrectly) called two or more times in a row, without an intervening call to _mcleanup; with this patch, the second and subsequent calls effectively become no-ops instead. 3. After _mcleanup, treat __moncontrol(1) as __moncontrol(0) instead. With zeroing of gmonparam buffer in _mcleanup, this stops the state incorrectly being changed to GMON_PROF_ON despite profiling actually being off. If we'd just done the minimal fix to _mcleanup of setting tos=NULL, there is risk of far worse memory corruption: kcount would point to deallocated memory, and the __profil syscall would make the kernel write profiling data into that memory, which could have since been reallocated to something unrelated. 4. Ensure __moncontrol(0) still turns off profiling even in error state. Otherwise, if mcount overflows and sets state to GMON_PROF_ERROR, when _mcleanup calls __moncontrol(0), the __profil syscall to disable profiling will not be invoked. _mcleanup will free the buffer, but the kernel will still be writing profiling data into it, potentially corrupted arbitrary memory. Also adds a test case for (1). Issues (2)-(4) are not feasible to test. Signed-off-by: Simon Kissane --- gmon/Makefile | 15 ++++++++++++++- gmon/gmon.c | 22 +++++++++++++++------- gmon/tst-mcleanup.c | 31 +++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 8 deletions(-) create mode 100644 gmon/tst-mcleanup.c diff --git a/gmon/Makefile b/gmon/Makefile index 4dd5adb80b..33eb5f4320 100644 --- a/gmon/Makefile +++ b/gmon/Makefile @@ -1,4 +1,5 @@ # Copyright (C) 1995-2023 Free Software Foundation, Inc. +# Copyright The GNU Toolchain Authors. # This file is part of the GNU C Library. # The GNU C Library is free software; you can redistribute it and/or @@ -25,7 +26,7 @@ include ../Makeconfig headers := sys/gmon.h sys/gmon_out.h sys/profil.h routines := gmon mcount profil sprofil prof-freq -tests = tst-sprofil tst-gmon +tests = tst-sprofil tst-gmon tst-mcleanup ifeq ($(build-profile),yes) tests += tst-profile-static tests-static += tst-profile-static @@ -56,6 +57,14 @@ ifeq ($(run-built-tests),yes) tests-special += $(objpfx)tst-gmon-gprof.out endif +CFLAGS-tst-mcleanup.c := -fno-omit-frame-pointer -pg +tst-mcleanup-no-pie = yes +CRT-tst-mcleanup := $(csu-objpfx)g$(start-installed-name) +tst-mcleanup-ENV := GMON_OUT_PREFIX=$(objpfx)tst-mcleanup.data +ifeq ($(run-built-tests),yes) +tests-special += $(objpfx)tst-mcleanup.out +endif + CFLAGS-tst-gmon-static.c := $(PIE-ccflag) -fno-omit-frame-pointer -pg CRT-tst-gmon-static := $(csu-objpfx)g$(static-start-installed-name) tst-gmon-static-no-pie = yes @@ -103,6 +112,10 @@ $(objpfx)tst-gmon.out: clean-tst-gmon-data clean-tst-gmon-data: rm -f $(objpfx)tst-gmon.data.* +$(objpfx)tst-mcleanup.out: clean-tst-mcleanup-data +clean-tst-mcleanup-data: + rm -f $(objpfx)tst-mcleanup.data.* + $(objpfx)tst-gmon-gprof.out: tst-gmon-gprof.sh $(objpfx)tst-gmon.out $(SHELL) $< $(GPROF) $(objpfx)tst-gmon $(objpfx)tst-gmon.data.* > $@; \ $(evaluate-test) diff --git a/gmon/gmon.c b/gmon/gmon.c index dee64803ad..290934046b 100644 --- a/gmon/gmon.c +++ b/gmon/gmon.c @@ -97,11 +97,8 @@ __moncontrol (int mode) { struct gmonparam *p = &_gmonparam; - /* Don't change the state if we ran into an error. */ - if (p->state == GMON_PROF_ERROR) - return; - - if (mode) + /* Treat start request as stop if error or gmon not initialized. */ + if (mode && p->state != GMON_PROF_ERROR && p->tos != NULL) { /* start */ __profil((void *) p->kcount, p->kcountsize, p->lowpc, s_scale); @@ -111,7 +108,9 @@ __moncontrol (int mode) { /* stop */ __profil(NULL, 0, 0, 0); - p->state = GMON_PROF_OFF; + /* Don't change the state if we ran into an error. */ + if (p->state != GMON_PROF_ERROR) + p->state = GMON_PROF_OFF; } } libc_hidden_def (__moncontrol) @@ -125,6 +124,14 @@ __monstartup (u_long lowpc, u_long highpc) char *cp; struct gmonparam *p = &_gmonparam; + /* + * If we are incorrectly called twice in a row (without an + * intervening call to _mcleanup), ignore the second call to + * prevent leaking memory. + */ + if (p->tos != NULL) + return; + /* * round lowpc and highpc to multiples of the density we're using * so the rest of the scaling (here and in gprof) stays in ints. @@ -440,9 +447,10 @@ _mcleanup (void) { __moncontrol (0); - if (_gmonparam.state != GMON_PROF_ERROR) + if (_gmonparam.state != GMON_PROF_ERROR && _gmonparam.tos != NULL) write_gmon (); /* free the memory. */ free (_gmonparam.tos); + memset(&_gmonparam, 0, sizeof _gmonparam); } diff --git a/gmon/tst-mcleanup.c b/gmon/tst-mcleanup.c new file mode 100644 index 0000000000..b259653ec8 --- /dev/null +++ b/gmon/tst-mcleanup.c @@ -0,0 +1,31 @@ +/* Test program for repeated invocation of _mcleanup + Copyright The GNU Toolchain Authors. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +/* Intentionally calls _mcleanup() twice: once manually, it will be + called again as an atexit handler. This is incorrect use of the API, + but the point of the test is to make sure we don't crash when the + API is misused in this way. */ + +#include + +int +main (void) +{ + _mcleanup(); + return 0; +}