public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Asaf Fisher <asaffisher.dev@gmail.com>
To: gdb-patches@sourceware.org
Cc: Asaf Fisher <asaffisher.dev@gmail.com>
Subject: [PATCH 0/2][PR < shlibs/29586>] GDB hangs when trying to resolve memory mapped shared libraries of an inferior under /proc/self/fd/*
Date: Mon, 17 Oct 2022 19:38:56 +0000	[thread overview]
Message-ID: <20221017193858.3006-1-asaffisher.dev@gmail.com> (raw)

Hello!
If any of you will try to dlopen a memory mapped file in the following way:`dlopen("/proc/self/fd/4"....)`
The operation will trigger GDB's hook on dlopen and will try to load the file "/proc/self/fd/4".
Obviously GDB's process has different FD's on `/proc/self`, and will read from an arbitrary opened file.
Most likely it will open a pipe which will cause GDB to hang.
Here is a rust snippet that will hang GDB once being debugged:
```
use std::{os::unix::prelude::{AsFd, AsRawFd}, io::Write};

use memfd;
use dlopen_derive::{self, WrapperApi};
#[macro_use]
use dlopen::wrapper::{Container,WrapperApi};

#[derive(WrapperApi)]
struct Api<'a> {
    example_rust_fun: fn(arg: i32) -> u32,
    example_c_fun: unsafe extern "C" fn(),
    example_reference: &'a mut i32,
}
fn main() {
    let opts = memfd::MemfdOptions::default().allow_sealing(true);
    let mfd = opts.create("hellooo").unwrap();
    let buff = std::fs::read("/usr/lib64/ld-linux-x86-64.so.2").unwrap();
    mfd.as_file().write(buff.as_slice()).unwrap();
    let fd = mfd.as_file().as_fd().as_raw_fd();
    let fm = format!("/proc/self/fd/{}", fd);
    println!("{}", fm);
    let mut cont: Container<Api> =
        unsafe { Container::load(fm) }.expect("Could not open library or load symbols");

}
```

To fix the problem I added a function to `solib-svr4.c` that will resolve `/proc/self/fd/[num]` to `/proc/[inferior_pid]/fd/[num]`.
To test it I added a test that simply checks if the warning printed by GDB when resolving the path is correct.

Asaf Fisher (2):
  Add test to check GDB handles dlopen of /proc/self/fd/[num] correctly
  Make GDB resolve dlopen of memory mapped shared libraries

 gdb/solib-svr4.c                           | 58 ++++++++++++++-
 gdb/testsuite/gdb.base/solib-proc-self.cc  | 72 ++++++++++++++++++
 gdb/testsuite/gdb.base/solib-proc-self.exp | 86 ++++++++++++++++++++++
 3 files changed, 214 insertions(+), 2 deletions(-)
 create mode 100644 gdb/testsuite/gdb.base/solib-proc-self.cc
 create mode 100644 gdb/testsuite/gdb.base/solib-proc-self.exp

-- 
2.38.0


             reply	other threads:[~2022-10-17 19:39 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-17 19:38 Asaf Fisher [this message]
2022-10-17 19:38 ` [PATCH 1/2] Add test to check GDB handles dlopen of /proc/self/fd/[num] correctly Asaf Fisher
2022-10-17 19:38 ` [PATCH 2/2] Make GDB resolve dlopen of memory mapped shared libraries Asaf Fisher
2022-10-21 10:50   ` Alexandra Petlanova Hajkova
2022-10-21 10:50     ` Alexandra Petlanova Hajkova

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=20221017193858.3006-1-asaffisher.dev@gmail.com \
    --to=asaffisher.dev@gmail.com \
    --cc=gdb-patches@sourceware.org \
    /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).