[0/2,PR,<,shlibs/29586>] GDB hangs when trying to resolve memory mapped shared libraries of an inferior under /proc/self/fd/*

Message ID 20221017193858.3006-1-asaffisher.dev@gmail.com
Headers
Series GDB hangs when trying to resolve memory mapped shared libraries of an inferior under /proc/self/fd/* |

Message

Asaf Fisher Oct. 17, 2022, 7:38 p.m. UTC
  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