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.129.124]) by sourceware.org (Postfix) with ESMTPS id DFEC03857014 for ; Wed, 7 Jun 2023 14:54:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DFEC03857014 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=1686149691; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=tNUBw1x0pg9xXpkoJe/61RgCRVGd8iSgyf+lM7E6xYs=; b=UKGhjMwuv3Re8WAt9v4GrEDgY3gHREkDcO4/V5BnlLpykMRquI7LXdoGFjmCAjywzUG2jP 4rOwF5HC6GsuFrzpaQONFdLChXCF7tKOrkj8SVcb82IjB1gnMm39QIA1IDdWRbialSTRsl iGmGATPaUneqjSbzM4NAZslS5qhdB+o= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-389-7RYpnEVEM4ad8VIziIjxXg-1; Wed, 07 Jun 2023 10:54:50 -0400 X-MC-Unique: 7RYpnEVEM4ad8VIziIjxXg-1 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-3f7ef0e0292so4747955e9.3 for ; Wed, 07 Jun 2023 07:54:50 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686149689; x=1688741689; h=mime-version:message-id:date:references:in-reply-to:subject:cc:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=tNUBw1x0pg9xXpkoJe/61RgCRVGd8iSgyf+lM7E6xYs=; b=GaSckLcEcBoXO6rTTZYIpSJAwcsk84MDIdNPl3oZy3aVfYexQwrqcuzG19A/Zv+PFm LCJXd69pJ1NWsWe7qrztS00kWGnfD7MvRKpOwxRjMVREEkB/4mCIX88T2y66Vs+OBCkV /jvpZvFwgfB3jNpbamTN2lwXmDrIj6Wj9IwWoarPKwlD1TlAJU0wO+KUcOGK5K2toYYs 9vTQdJuo6HCd1o6OiU+0fEch3VFMONspKChiYVSVB1Pdw5qmobNSSTiXMnwuQyeECvWF yGgsbDOtFbrtH4F392clm+ev7rpCDLQiMFye+eBZF7UMoHPOVPF00dHGch7qgDc9ZP/6 ax1g== X-Gm-Message-State: AC+VfDzGFXkvJmNsoIcwI1a0Nzu/blbEu0G/U6TFx9KOCQp9A6LVJZl6 ro6eNotvXupeMHH4+P4KCMUqCcYxLPRuaiq2nIW8Pz0mmTWGuC6le8UIm1Ce19xFmYAbf7+OpTX aUTH2Y/5jq3JRdfkX8jGtUA== X-Received: by 2002:a7b:c44c:0:b0:3f7:ec0c:8799 with SMTP id l12-20020a7bc44c000000b003f7ec0c8799mr4074259wmi.16.1686149689367; Wed, 07 Jun 2023 07:54:49 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5/nNK+EWVpGixLxWNp2ZLK0KZfFMqeIcfxOJgPad7J8oSPvL1Tn7VStVjeatMMQ5xyB9bbgQ== X-Received: by 2002:a7b:c44c:0:b0:3f7:ec0c:8799 with SMTP id l12-20020a7bc44c000000b003f7ec0c8799mr4074244wmi.16.1686149688976; Wed, 07 Jun 2023 07:54:48 -0700 (PDT) Received: from localhost (11.72.115.87.dyn.plus.net. [87.115.72.11]) by smtp.gmail.com with ESMTPSA id f8-20020a1c6a08000000b003f72a7918e7sm2341112wmc.45.2023.06.07.07.54.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Jun 2023 07:54:48 -0700 (PDT) From: Andrew Burgess To: Lancelot SIX , jhb@FreeBSD.org Cc: gdb-patches@sourceware.org, lancelot.six@amd.com, lsix@lancelotsix.com Subject: Re: [PATCH v2 1/3] gdb/corelow.c: fix use-after-free in build_file_mappings In-Reply-To: <20230601104540.156137-1-lancelot.six@amd.com> References: <87ttvr4i9z.fsf@redhat.com> <20230601104540.156137-1-lancelot.six@amd.com> Date: Wed, 07 Jun 2023 15:54:47 +0100 Message-ID: <87bkhr1fwo.fsf@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE 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: Lancelot SIX writes: > Hi, > > Thanks John and Andrew for the reviews and suggestions. Here is a V2 > for patch #1 (I have added Andrew's Reviewed-By tag to patch 2 and 3). > > John, should I add your Reviewed-By tag in patches 2 and 3 as you > reviewed them as well? > > Changes since V1: > - Only register the bfd in bfd_map if it got successfully opened, > following John's suggestion. I have not used the exact pattern > suggested in the review to avoid duplicating the warning message. > > Best, > Lancelot. > > --- > > In core_target::build_file_mappings, GDB tries to open files referenced > in the core dump. > > The process goes like this: > > struct bfd *bfd = bfd_map[filename]; > if (bfd == nullptr) > { > bfd = bfd_map[filename] > = bfd_openr (expanded_fname.get (), "binary"); > if (bfd == nullptr || !bfd_check_format (bfd, bfd_object)) > { > if (bfd != nullptr) > bfd_close (bfd); > return; > } > } > asection *sec = bfd_make_section_anyway (bfd, "load"); > ... > > The problem is that if bfd_check_format fails, we close the bfd but keep > a reference to it in the bfd_map. > > If the same filename appears another time in the NT_FILE note, we enter > this code again. The second time, bfd_map[filename] is not nullptr and > we try to call bfd_make_section_anyway on an already closed BFD, which > is a use-after-free error. > > This patch makes sure that the bfd is only saved in the bfd_map if it > got opened successfully. > > This error got exposed by a recent change in BFD (014a602b86f "Don't > optimise bfd_seek to same position"). Since this change, opening a > coredump which contains mapping to some special files such as a DRI > render node (/dev/dri/renderD$NUM) exposes the issue. This happens for > example for processes using AMDGPU devices to offload compute tasks. All 3 patches LGTM. Approved-By: Andrew Burgess Thanks, Andrew > --- > gdb/corelow.c | 4 ++-- > 1 file changed, 2 insertions(+), 2 deletions(-) > > diff --git a/gdb/corelow.c b/gdb/corelow.c > index db489b4280e..54def4198bc 100644 > --- a/gdb/corelow.c > +++ b/gdb/corelow.c > @@ -258,8 +258,7 @@ core_target::build_file_mappings () > return; > } > > - bfd = bfd_map[filename] = bfd_openr (expanded_fname.get (), > - "binary"); > + bfd = bfd_openr (expanded_fname.get (), "binary"); > > if (bfd == nullptr || !bfd_check_format (bfd, bfd_object)) > { > @@ -284,6 +283,7 @@ core_target::build_file_mappings () > This can be checked before/after a core file detach via > "maint info bfds". */ > gdb_bfd_record_inclusion (core_bfd, bfd); > + bfd_map[filename] = bfd; > } > > /* Make new BFD section. All sections have the same name, > -- > 2.34.1