From: Andrew Burgess <aburgess@redhat.com>
To: John Baldwin <jhb@FreeBSD.org>,
Lancelot SIX <lancelot.six@amd.com>,
gdb-patches@sourceware.org
Cc: lsix@lancelotsix.com
Subject: Re: [PATCH 1/3] gdb/corelow.c: fix use-after-free in build_file_mappings
Date: Thu, 01 Jun 2023 10:57:12 +0100 [thread overview]
Message-ID: <87ttvr4i9z.fsf@redhat.com> (raw)
In-Reply-To: <7de4445c-3b92-61d4-188a-c23cbc7d6802@FreeBSD.org>
John Baldwin <jhb@FreeBSD.org> writes:
> On 5/31/23 9:04 AM, Lancelot SIX via Gdb-patches wrote:
>> 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 after closing the bfd, it is removed from the
>> bfd_map.
>>
>> 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.
>> ---
>> gdb/corelow.c | 5 ++++-
>> 1 file changed, 4 insertions(+), 1 deletion(-)
>>
>> diff --git a/gdb/corelow.c b/gdb/corelow.c
>> index db489b4280e..77fc4453f94 100644
>> --- a/gdb/corelow.c
>> +++ b/gdb/corelow.c
>> @@ -277,7 +277,10 @@ core_target::build_file_mappings ()
>> "during file-backed mapping note processing"),
>> filename, expanded_fname.get ());
>> if (bfd != nullptr)
>> - bfd_close (bfd);
>> + {
>> + bfd_close (bfd);
>> + bfd_map.erase (filename);
>> + }
>> return;
>> }
>> /* Ensure that the bfd will be closed when core_bfd is closed.
>
> Maybe only insert the bfd into the map on success instead of having to do the
> erase, that is something like:
>
> if (bfd == nullptr)
> {
> bfd = bfd_openr (...);
> if (bfd == nullptr)
> return;
> if (!bfd_check_format (...))
> {
> bfd_close (bfd);
> return;
> }
> bfd_map[filename] = bfd;
> }
+1 for this approach.
Thanks,
Andrew
>
> --
> John Baldwin
next prev parent reply other threads:[~2023-06-01 9:57 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-05-31 16:04 [PATCH 0/3] Fix use-after-free in gdb/corelow.c + cleanups Lancelot SIX
2023-05-31 16:04 ` [PATCH 1/3] gdb/corelow.c: fix use-after-free in build_file_mappings Lancelot SIX
2023-05-31 18:30 ` John Baldwin
2023-06-01 9:57 ` Andrew Burgess [this message]
2023-06-01 10:45 ` [PATCH v2 " Lancelot SIX
2023-06-01 17:05 ` John Baldwin
2023-06-07 14:54 ` Andrew Burgess
2023-06-08 13:22 ` Lancelot SIX
2023-05-31 16:04 ` [PATCH 2/3] gdb/corelow.c: avoid repeated warnings " Lancelot SIX
2023-06-01 9:50 ` Andrew Burgess
2023-05-31 16:04 ` [PATCH 3/3] gdb/corelow.c: do not try to reopen a file if open failed once Lancelot SIX
2023-06-01 10:04 ` Andrew Burgess
2023-05-31 18:32 ` [PATCH 0/3] Fix use-after-free in gdb/corelow.c + cleanups John Baldwin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87ttvr4i9z.fsf@redhat.com \
--to=aburgess@redhat.com \
--cc=gdb-patches@sourceware.org \
--cc=jhb@FreeBSD.org \
--cc=lancelot.six@amd.com \
--cc=lsix@lancelotsix.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).