public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 0/2][PR < shlibs/29586>] GDB hangs when trying to resolve memory mapped shared libraries of an inferior under /proc/self/fd/*
@ 2022-10-17 19:38 Asaf Fisher
  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
  0 siblings, 2 replies; 5+ messages in thread
From: Asaf Fisher @ 2022-10-17 19:38 UTC (permalink / raw)
  To: gdb-patches; +Cc: Asaf Fisher

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


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-10-21 10:50 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-17 19:38 [PATCH 0/2][PR < shlibs/29586>] GDB hangs when trying to resolve memory mapped shared libraries of an inferior under /proc/self/fd/* Asaf Fisher
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

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).