Skip to contentSkip to conversation

gdbstub: Add support for info proc mappings

PATCH RESEND Superseded Ilya Leoshkevich 3 participants 13 messages May 10, 2023 – May 24, 2023

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover gdbstub: Add support for info proc mappings

[Apologies to people in To: and Cc:, who will get this the second time - I forgot to Cc: the mailing list initially.]

Hi,

this series partially implements the Host I/O feature of the GDB Remote Serial Protocol in order to make generate-core-file work with qemu-user. It borrows heavily from the abandoned patch by Dominik [1], hence 4/6 carries the respective Co-developed-by: tag. I hope that's okay. I also peeked at gdbserver/hostio.cc quite a few times.

The changes compared to Dominik's patch are:

- Implement readlink.
- Move the main functionality to user-target.c.
- Allocate buffers on heap.
- Add a test.
- Update gdb.rst.
- Split refactorings to the existing code into separate patches.
- Rename do_openat() to do_guest_openat().
- Do not retry pread(), since GDB is capable of doing it itself.
- Add an extra sanity check to gdb_handle_query_xfer_exec_file().
- Replace citations of the spec by a single link.

Best regards,
Ilya

[1] https://lore.kernel.org/all/[email protected]/

Ilya Leoshkevich (6):
linux-user: Expose do_guest_openat() and do_guest_readlink()
gdbstub: Expose gdb_get_process() and gdb_get_first_cpu_in_process()
gdbstub: Report the actual qemu-user pid
gdbstub: Add support for info proc mappings
docs: Document security implications of debugging tests/tcg: Add a test for info proc mappings

 docs/system/gdb.rst                           |  15 ++
 gdbstub/gdbstub.c                             |  86 ++++++++---
 gdbstub/internals.h                           |   7 +
 gdbstub/user-target.c                         | 139 ++++++++++++++++++
 linux-user/qemu.h                             |   3 +
 linux-user/syscall.c                          |  54 ++++---
 tests/tcg/aarch64/Makefile.target             |   3 +-
 tests/tcg/multiarch/Makefile.target           |   7 +
 .../multiarch/gdbstub/test-proc-mappings.py   |  55 +++++++
 tests/tcg/s390x/Makefile.target               |   2 +-
 10 files changed, 332 insertions(+), 39 deletions(-)
 create mode 100644 tests/tcg/multiarch/gdbstub/test-proc-mappings.py
Signature · 1 line
2.40.1
#5 lore raw

PATCH RESEND 3/6 gdbstub: Report the actual qemu-user pid

Currently qemu-user reports pid 1 to GDB. Resolve the TODO and report the actual PID. Using getpid() relies on the assumption that there is only one GDBProcess. Add an assertion to make sure that future changes don't break it.

---
 gdbstub/gdbstub.c | 25 +++++++++++++++++--------
 1 file changed, 17 insertions(+), 8 deletions(-)
gdbstub/gdbstub.c +17 -8
index 207250c1c08..003db59b1b2 100644
@@ -202,13 +202,16 @@ void gdb_memtox(GString *buf, const char *mem, int len)
 
 static uint32_t gdb_get_cpu_pid(CPUState *cpu)
 {
-    /* TODO: In user mode, we should use the task state PID */
+#ifdef CONFIG_USER_ONLY
+    return getpid();
+#else
     if (cpu->cluster_index == UNASSIGNED_CLUSTER_INDEX) {
         /* Return the default process' PID */
         int index = gdbserver_state.process_num - 1;
         return gdbserver_state.processes[index].pid;
     }
     return cpu->cluster_index + 1;
+#endif
 }
 
 GDBProcess *gdb_get_process(uint32_t pid)
@@ -2127,19 +2130,25 @@ void gdb_read_byte(uint8_t ch)
 void gdb_create_default_process(GDBState *s)
 {
     GDBProcess *process;
-    int max_pid = 0;
+    int pid;
 
+#ifdef CONFIG_USER_ONLY
+    assert(gdbserver_state.process_num == 0);
+    pid = getpid();
+#else
     if (gdbserver_state.process_num) {
-        max_pid = s->processes[s->process_num - 1].pid;
+        pid = s->processes[s->process_num - 1].pid;
+    } else {
+        pid = 0;
     }
+    /* We need an available PID slot for this process */
+    assert(pid < UINT32_MAX);
+    pid++;
+#endif
 
     s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
     process = &s->processes[s->process_num - 1];
-
-    /* We need an available PID slot for this process */
-    assert(max_pid < UINT32_MAX);
-
-    process->pid = max_pid + 1;
+    process->pid = pid;
     process->attached = false;
     process->target_xml[0] = '\0';
 }
Signature · 1 line
2.40.1
#11 lore raw

PATCH RESEND 6/6 tests/tcg: Add a test for info proc mappings

In reply to Ilya Leoshkevich

Add a small test to prevent regressions.
Since there are issues with how GDB interprets QEMU's target.xml, enable the test only on aarch64 and s390x for now.

---
 tests/tcg/aarch64/Makefile.target             |  3 +-
 tests/tcg/multiarch/Makefile.target           |  7 +++
 .../multiarch/gdbstub/test-proc-mappings.py   | 55 +++++++++++++++++++
 tests/tcg/s390x/Makefile.target               |  2 +-
 4 files changed, 65 insertions(+), 2 deletions(-)
 create mode 100644 tests/tcg/multiarch/gdbstub/test-proc-mappings.py
tests/tcg/aarch64/Makefile.target +2 -1
index 03157954871..38402b0ba1f 100644
@@ -97,7 +97,8 @@ run-gdbstub-sve-ioctls: sve-ioctls
 		--bin $< --test $(AARCH64_SRC)/gdbstub/test-sve-ioctl.py, \
 	basic gdbstub SVE ZLEN support)
 
-EXTRA_RUNS += run-gdbstub-sysregs run-gdbstub-sve-ioctls
+EXTRA_RUNS += run-gdbstub-sysregs run-gdbstub-sve-ioctls \
+              run-gdbstub-proc-mappings
 endif
 endif
 
tests/tcg/multiarch/Makefile.target +7 -0
index 373db696481..cbc0b75787a 100644
@@ -81,6 +81,13 @@ run-gdbstub-qxfer-auxv-read: sha1
 		--bin $< --test $(MULTIARCH_SRC)/gdbstub/test-qxfer-auxv-read.py, \
 	basic gdbstub qXfer:auxv:read support)
 
+run-gdbstub-proc-mappings: sha1
+	$(call run-test, $@, $(GDB_SCRIPT) \
+		--gdb $(HAVE_GDB_BIN) \
+		--qemu $(QEMU) --qargs "$(QEMU_OPTS)" \
+		--bin $< --test $(MULTIARCH_SRC)/gdbstub/test-proc-mappings.py, \
+	proc mappings support)
+
 run-gdbstub-thread-breakpoint: testthread
 	$(call run-test, $@, $(GDB_SCRIPT) \
 		--gdb $(HAVE_GDB_BIN) \
tests/tcg/multiarch/gdbstub/test-proc-mappings.py new file +55 -0

55 lines not on this page · raw message

tests/tcg/s390x/Makefile.target +1 -1
index 0031868b136..2934ac9adf2 100644
@@ -74,7 +74,7 @@ run-gdbstub-signals-s390x: signals-s390x
 		--bin $< --test $(S390X_SRC)/gdbstub/test-signals-s390x.py, \
 	mixing signals and debugging)
 
-EXTRA_RUNS += run-gdbstub-signals-s390x
+EXTRA_RUNS += run-gdbstub-signals-s390x run-gdbstub-proc-mappings
 endif
 
 # MVX versions of sha512
Signature · 1 line
2.40.1
#7 lore raw

PATCH RESEND 4/6 gdbstub: Add support for info proc mappings

In reply to Ilya Leoshkevich

Currently the GDB's generate-core-file command doesn't work well with
qemu-user: the resulting dumps are huge [1] and at the same time incomplete (argv and envp are missing). The reason is that GDB has no access to proc mappings and therefore has to fall back to using heuristics for discovering them. This is, in turn, because qemu-user does not implement the Host I/O feature of the GDB Remote Serial Protocol.

Implement vFile:{open,close,pread,readlink} and also qXfer:exec-file:read+.

With that, generate-core-file begins to work on aarch64 and s390x, albeit with two deficiencies:

* GDB still tries to dump the host mappings, because QEMU does not fake /proc/$PID/smaps (as opposed to /proc/$PID/maps). The user-visible effect is only a bunch of warnings.
* PT_LOAD segments lack PF_X flags (I have not debugged this).

The impact of these issues on usability is fairly low, so they can be resolved later.

[1] https://sourceware.org/pipermail/gdb-patches/2023-May/199432.html

---
 gdbstub/gdbstub.c     |  45 +++++++++++++-
 gdbstub/internals.h   |   5 ++
 gdbstub/user-target.c | 139 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 187 insertions(+), 2 deletions(-)
gdbstub/gdbstub.c +43 -2
index 003db59b1b2..c4112d6eacd 100644
@@ -1326,6 +1326,36 @@ static const GdbCmdParseEntry gdb_v_commands_table[] = {
         .cmd = "Kill;",
         .cmd_startswith = 1
     },
+#ifdef CONFIG_USER_ONLY
+    /*
+     * Host I/O Packets. See [1] for details.
+     * [1] https://sourceware.org/gdb/onlinedocs/gdb/Host-I_002fO-Packets.html
+     */
+    {
+        .handler = gdb_handle_v_file_open,
+        .cmd = "File:open:",
+        .cmd_startswith = 1,
+        .schema = "s,L,L0"
+    },
+    {
+        .handler = gdb_handle_v_file_close,
+        .cmd = "File:close:",
+        .cmd_startswith = 1,
+        .schema = "l0"
+    },
+    {
+        .handler = gdb_handle_v_file_pread,
+        .cmd = "File:pread:",
+        .cmd_startswith = 1,
+        .schema = "l,L,L0"
+    },
+    {
+        .handler = gdb_handle_v_file_readlink,
+        .cmd = "File:readlink:",
+        .cmd_startswith = 1,
+        .schema = "s0"
+    },
+#endif
 };
 
 static void handle_v_commands(GArray *params, void *user_ctx)
@@ -1471,11 +1501,14 @@ static void handle_query_supported(GArray *params, void *user_ctx)
             ";ReverseStep+;ReverseContinue+");
     }
 
-#if defined(CONFIG_USER_ONLY) && defined(CONFIG_LINUX)
+#if defined(CONFIG_USER_ONLY)
+#if defined(CONFIG_LINUX)
     if (gdbserver_state.c_cpu->opaque) {
         g_string_append(gdbserver_state.str_buf, ";qXfer:auxv:read+");
     }
 #endif
+    g_string_append(gdbserver_state.str_buf, ";qXfer:exec-file:read+");
+#endif
 
     if (params->len &&
         strstr(get_param(params, 0)->data, "multiprocess+")) {
@@ -1614,13 +1647,21 @@ static const GdbCmdParseEntry gdb_gen_query_table[] = {
         .cmd_startswith = 1,
         .schema = "s:l,l0"
     },
-#if defined(CONFIG_USER_ONLY) && defined(CONFIG_LINUX)
+#if defined(CONFIG_USER_ONLY)
+#if defined(CONFIG_LINUX)
     {
         .handler = gdb_handle_query_xfer_auxv,
         .cmd = "Xfer:auxv:read::",
         .cmd_startswith = 1,
         .schema = "l,l0"
     },
+#endif
+    {
+        .handler = gdb_handle_query_xfer_exec_file,
+        .cmd = "Xfer:exec-file:read:",
+        .cmd_startswith = 1,
+        .schema = "l:l,l0"
+    },
 #endif
     {
         .handler = gdb_handle_query_attached,
gdbstub/internals.h +5 -0
index 235f2551bd4..c1217337812 100644
@@ -184,6 +184,11 @@ typedef union GdbCmdVariant {
 void gdb_handle_query_rcmd(GArray *params, void *user_ctx); /* softmmu */
 void gdb_handle_query_offsets(GArray *params, void *user_ctx); /* user */
 void gdb_handle_query_xfer_auxv(GArray *params, void *user_ctx); /*user */
+void gdb_handle_v_file_open(GArray *params, void *user_ctx); /* user */
+void gdb_handle_v_file_close(GArray *params, void *user_ctx); /* user */
+void gdb_handle_v_file_pread(GArray *params, void *user_ctx); /* user */
+void gdb_handle_v_file_readlink(GArray *params, void *user_ctx); /* user */
+void gdb_handle_query_xfer_exec_file(GArray *params, void *user_ctx); /* user */
 
 void gdb_handle_query_attached(GArray *params, void *user_ctx); /* both */
 
gdbstub/user-target.c +139 -0

148 lines not on this page · raw message

Signature · 1 line
2.40.1
#3 lore raw

PATCH RESEND 2/6 gdbstub: Expose gdb_get_process() and gdb_get_first_cpu_in_process()

In reply to Ilya Leoshkevich

These functions will be needed by user-target.c in order to retrieve the name of the executable.

---
 gdbstub/gdbstub.c   | 16 ++++++++--------
 gdbstub/internals.h |  2 ++
 2 files changed, 10 insertions(+), 8 deletions(-)
gdbstub/gdbstub.c +8 -8
index 0760d786858..207250c1c08 100644
@@ -211,7 +211,7 @@ static uint32_t gdb_get_cpu_pid(CPUState *cpu)
     return cpu->cluster_index + 1;
 }
 
-static GDBProcess *gdb_get_process(uint32_t pid)
+GDBProcess *gdb_get_process(uint32_t pid)
 {
     int i;
 
@@ -247,7 +247,7 @@ static CPUState *find_cpu(uint32_t thread_id)
     return NULL;
 }
 
-static CPUState *get_first_cpu_in_process(GDBProcess *process)
+CPUState *gdb_get_first_cpu_in_process(GDBProcess *process)
 {
     CPUState *cpu;
 
@@ -325,7 +325,7 @@ static CPUState *gdb_get_cpu(uint32_t pid, uint32_t tid)
             return NULL;
         }
 
-        return get_first_cpu_in_process(process);
+        return gdb_get_first_cpu_in_process(process);
     } else {
         /* a specific thread */
         cpu = find_cpu(tid);
@@ -354,7 +354,7 @@ static const char *get_feature_xml(const char *p, const char **newp,
     size_t len;
     int i;
     const char *name;
-    CPUState *cpu = get_first_cpu_in_process(process);
+    CPUState *cpu = gdb_get_first_cpu_in_process(process);
     CPUClass *cc = CPU_GET_CLASS(cpu);
 
     len = 0;
@@ -490,7 +490,7 @@ void gdb_register_coprocessor(CPUState *cpu,
 
 static void gdb_process_breakpoint_remove_all(GDBProcess *p)
 {
-    CPUState *cpu = get_first_cpu_in_process(p);
+    CPUState *cpu = gdb_get_first_cpu_in_process(p);
 
     while (cpu) {
         gdb_breakpoint_remove_all(cpu);
@@ -653,7 +653,7 @@ static int gdb_handle_vcont(const char *p)
                 goto out;
             }
 
-            cpu = get_first_cpu_in_process(process);
+            cpu = gdb_get_first_cpu_in_process(process);
             while (cpu) {
                 if (newstates[cpu->cpu_index] == 1) {
                     newstates[cpu->cpu_index] = cur_action;
@@ -1274,7 +1274,7 @@ static void handle_v_attach(GArray *params, void *user_ctx)
         goto cleanup;
     }
 
-    cpu = get_first_cpu_in_process(process);
+    cpu = gdb_get_first_cpu_in_process(process);
     if (!cpu) {
         goto cleanup;
     }
@@ -1392,7 +1392,7 @@ static void handle_query_curr_tid(GArray *params, void *user_ctx)
      * first thread).
      */
     process = gdb_get_cpu_process(gdbserver_state.g_cpu);
-    cpu = get_first_cpu_in_process(process);
+    cpu = gdb_get_first_cpu_in_process(process);
     g_string_assign(gdbserver_state.str_buf, "QC");
     gdb_append_thread_id(cpu, gdbserver_state.str_buf);
     gdb_put_strbuf();
gdbstub/internals.h +2 -0
index 94ddff44958..235f2551bd4 100644
@@ -124,6 +124,8 @@ void gdb_read_byte(uint8_t ch);
  */
 bool gdb_got_immediate_ack(void);
 /* utility helpers */
+GDBProcess *gdb_get_process(uint32_t pid);
+CPUState *gdb_get_first_cpu_in_process(GDBProcess *process);
 CPUState *gdb_first_attached_cpu(void);
 void gdb_append_thread_id(CPUState *cpu, GString *buf);
 int gdb_get_cpu_index(CPUState *cpu);
Signature · 1 line
2.40.1
#1 lore raw

PATCH RESEND 1/6 linux-user: Expose do_guest_openat() and do_guest_readlink()

In reply to Ilya Leoshkevich

These functions will be required by the GDB stub in order to provide the guest view of /proc to GDB.

---
 linux-user/qemu.h    |  3 +++
 linux-user/syscall.c | 54 ++++++++++++++++++++++++++++----------------
 2 files changed, 38 insertions(+), 19 deletions(-)
linux-user/qemu.h +3 -0
index e2e93fbd1d5..08bcdd7b7c5 100644
@@ -165,6 +165,9 @@ typedef struct TaskState {
 } TaskState;
 
 abi_long do_brk(abi_ulong new_brk);
+int do_guest_openat(CPUArchState *cpu_env, int dirfd, const char *pathname,
+                    int flags, mode_t mode);
+ssize_t do_guest_readlink(const char *pathname, char *buf, size_t bufsiz);
 
 /* user access */
 
linux-user/syscall.c +35 -19

84 lines not on this page · raw message

Signature · 1 line
2.40.1
#8 lore raw

PATCH RESEND 5/6 docs: Document security implications of debugging

In reply to Ilya Leoshkevich

Now that the GDB stub implements reading host files, concerns may arise that it undermines security. Document the status quo, which is that the users are already responsible for securing the GDB connection themselves.

---
 docs/system/gdb.rst | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
docs/system/gdb.rst +15 -0
index 453eb73f6c4..3cc5167d928 100644
@@ -192,3 +192,18 @@ The memory mode can be checked by sending the following command:
 
 ``maintenance packet Qqemu.PhyMemMode:0``
     This will change it back to normal memory mode.
+
+Security considerations
+=======================
+
+Connecting to the GDB socket allows running arbitrary code inside the guest;
+in case of the TCG emulation, which is not considered a security boundary, this
+also means running arbitrary code on the host. Additionally, when debugging
+qemu-user, it allows directly downloading any file readable by QEMU from the
+host.
+
+The GDB socket is not protected by authentication, authorization or encryption.
+It is therefore a responsibility of the user to make sure that only authorized
+clients can connect to it, e.g., by using a unix socket with proper
+permissions, or by opening a TCP socket only on interfaces that are not
+reachable by potential attackers.
Signature · 1 line
2.40.1
#12 lore raw

PING: [PATCH RESEND 0/6] gdbstub: Add support for info proc mappings

In reply to Ilya Leoshkevich
Quoted 54 lines · Ilya Leoshkevich

[Apologies to people in To: and Cc:, who will get this the second time -
I forgot to Cc: the mailing list initially.]

Hi,

this series partially implements the Host I/O feature of the GDB Remote
Serial Protocol in order to make generate-core-file work with qemu- user.
It borrows heavily from the abandoned patch by Dominik [1], hence 4/6 carries the respective Co-developed-by: tag. I hope that's okay. I also
peeked at gdbserver/hostio.cc quite a few times.

The changes compared to Dominik's patch are:

- Implement readlink.
- Move the main functionality to user-target.c.
- Allocate buffers on heap.
- Add a test.
- Update gdb.rst.
- Split refactorings to the existing code into separate patches.
- Rename do_openat() to do_guest_openat().
- Do not retry pread(), since GDB is capable of doing it itself.
- Add an extra sanity check to gdb_handle_query_xfer_exec_file().
- Replace citations of the spec by a single link.

Best regards,
Ilya

[1]
https://lore.kernel.org/all/[email protected]/

Ilya Leoshkevich (6):
  linux-user: Expose do_guest_openat() and do_guest_readlink()
  gdbstub: Expose gdb_get_process() and gdb_get_first_cpu_in_process()
  gdbstub: Report the actual qemu-user pid
  gdbstub: Add support for info proc mappings
  docs: Document security implications of debugging tests/tcg: Add a test for info proc mappings

 docs/system/gdb.rst                           |  15 ++ gdbstub/gdbstub.c                             |  86 ++++++++--- gdbstub/internals.h                           |   7 + gdbstub/user-target.c                         | 139
++++++++++++++++++
 linux-user/qemu.h                             |   3 + linux-user/syscall.c                          |  54 ++++--- tests/tcg/aarch64/Makefile.target             |   3 +- tests/tcg/multiarch/Makefile.target           |   7 + .../multiarch/gdbstub/test-proc-mappings.py   |  55 +++++++ tests/tcg/s390x/Makefile.target               |   2 +-

Quoted 2 lines
 10 files changed, 332 insertions(+), 39 deletions(-)

 create mode 100644 tests/tcg/multiarch/gdbstub/test-proc-mappings.py

Ping.

Reviewed-by on v1 3/6
#6 lore raw

Re: [PATCH RESEND 3/6] gdbstub: Report the actual qemu-user pid

In reply to Ilya Leoshkevich
Quoted 6 lines · Ilya Leoshkevich

Currently qemu-user reports pid 1 to GDB. Resolve the TODO and report the actual PID. Using getpid() relies on the assumption that there is only one GDBProcess. Add an assertion to make sure that future changes don't break it.

Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
Reviewed-by on v1 5/6
#9 lore raw

Re: [PATCH RESEND 5/6] docs: Document security implications of debugging

In reply to Ilya Leoshkevich
Quoted 6 lines · Ilya Leoshkevich

Now that the GDB stub implements reading host files, concerns may arise that it undermines security. Document the status quo, which is that the users are already responsible for securing the GDB connection themselves.

Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#10 lore raw

Re: [PATCH RESEND 5/6] docs: Document security implications of debugging

Hi,

Just to add two cents here: the commit message is a bit not true because the qemu-user GDB stub could always read host files by just changing the emulated code to open and read those files. Apart from that, I like the documentation additions.

Best regards,
Dominik 'Disconnect3d' Czarnota

Quoted 16 lines · Alex Bennée
Quoted 6 lines · Ilya Leoshkevich

Now that the GDB stub implements reading host files, concerns may arise that it undermines security. Document the status quo, which is that the users are already responsible for securing the GDB connection themselves.

--
Alex Bennée
Virtualisation Tech Lead @ Linaro