fbsd nat: Disable address space randomization when requested.
authorJohn Baldwin <jhb@FreeBSD.org>
Mon, 14 Jun 2021 21:28:26 +0000 (14:28 -0700)
committerJohn Baldwin <jhb@FreeBSD.org>
Mon, 14 Jun 2021 21:55:48 +0000 (14:55 -0700)
Use procctl(2) with PROC_ASLR_CTL to disable address space
randomization in the current gdb process before forking a child
process for a new inferior when address space randomization is
disabled.

gdb/ChangeLog:

* configure.ac: Check for <sys/procctl.h>.
* config.in, configure: Regenerate.
* fbsd-nat.c: Include <sys/procctl.h> if present.
[PROC_ASLR_CTL] (maybe_disable_address_space_randomization): New.
(fbsd_nat_target::create_inferior)
(fbsd_nat_target::supports_disable_randomization): New.
* fbsd-nat.h (fbsd_nat_target::create_inferior)
(fbsd_nat_target::supports_disable_randomization): New.

gdb/ChangeLog
gdb/config.in
gdb/configure
gdb/configure.ac
gdb/fbsd-nat.c
gdb/fbsd-nat.h

index 2db5abcb65e1afc61fe99034be48e19506be1db1..7f46c4a64822bf5ef239d74975b1b3bd6c2f6e1f 100644 (file)
@@ -1,3 +1,14 @@
+2021-06-14  John Baldwin  <jhb@FreeBSD.org>
+
+       * configure.ac: Check for <sys/procctl.h>.
+       * config.in, configure: Regenerate.
+       * fbsd-nat.c: Include <sys/procctl.h> if present.
+       [PROC_ASLR_CTL] (maybe_disable_address_space_randomization): New.
+       (fbsd_nat_target::create_inferior)
+       (fbsd_nat_target::supports_disable_randomization): New.
+       * fbsd-nat.h (fbsd_nat_target::create_inferior)
+       (fbsd_nat_target::supports_disable_randomization): New.
+
 2021-06-14  Bernd Edlinger  <bernd.edlinger@hotmail.de>
 
        * compile/compile.c: Include missing header signal.h.
index 99c924f9ba066ab224258410db40c1249cc0e050..9342604ac4c915b32963bf31d758599e55d5479c 100644 (file)
 /* Define to 1 if you have the <sys/poll.h> header file. */
 #undef HAVE_SYS_POLL_H
 
+/* Define to 1 if you have the <sys/procctl.h> header file. */
+#undef HAVE_SYS_PROCCTL_H
+
 /* Define to 1 if you have the <sys/procfs.h> header file. */
 #undef HAVE_SYS_PROCFS_H
 
index 3d3977b26a874068cf909470b2096c8e681198e3..c6b5906d9e477de9e9a52424f4ce5da96f024b38 100755 (executable)
@@ -12260,7 +12260,7 @@ fi
 for ac_header in nlist.h machine/reg.h \
                   thread_db.h \
                  sys/file.h sys/filio.h sys/ioctl.h sys/param.h \
-                 sys/resource.h sys/ptrace.h ptrace.h \
+                 sys/procctl.h sys/resource.h sys/ptrace.h ptrace.h \
                  sys/reg.h sys/debugreg.h \
                  termios.h elf_hp.h
 do :
index df340ffa8264395056c40fd14007c0d8ae12ceac..68cf84d6ca3f12f627f5afb1387bad763fb18e36 100644 (file)
@@ -1298,7 +1298,7 @@ AC_HEADER_STDC
 AC_CHECK_HEADERS([nlist.h machine/reg.h \
                   thread_db.h \
                  sys/file.h sys/filio.h sys/ioctl.h sys/param.h \
-                 sys/resource.h sys/ptrace.h ptrace.h \
+                 sys/procctl.h sys/resource.h sys/ptrace.h ptrace.h \
                  sys/reg.h sys/debugreg.h \
                  termios.h elf_hp.h])
 AC_CHECK_HEADERS(sys/user.h, [], [],
index 581c04d5f83defca123c6e4587de98b97c576de3..234e74fcfd430a0e5cccb20ae782b5876aa2e529 100644 (file)
@@ -29,6 +29,9 @@
 #include "gdbsupport/gdb_wait.h"
 #include "inf-ptrace.h"
 #include <sys/types.h>
+#ifdef HAVE_SYS_PROCCTL_H
+#include <sys/procctl.h>
+#endif
 #include <sys/procfs.h>
 #include <sys/ptrace.h>
 #include <sys/signal.h>
@@ -1401,6 +1404,68 @@ fbsd_nat_target::supports_stopped_by_sw_breakpoint ()
 }
 #endif
 
+#ifdef PROC_ASLR_CTL
+class maybe_disable_address_space_randomization
+{
+public:
+  explicit maybe_disable_address_space_randomization (bool disable_randomization)
+  {
+    if (disable_randomization)
+      {
+       if (procctl (P_PID, getpid (), PROC_ASLR_STATUS, &m_aslr_ctl) == -1)
+         {
+           warning (_("Failed to fetch current address space randomization "
+                      "status: %s"), safe_strerror (errno));
+           return;
+         }
+
+       m_aslr_ctl &= ~PROC_ASLR_ACTIVE;
+       if (m_aslr_ctl == PROC_ASLR_FORCE_DISABLE)
+         return;
+
+       int ctl = PROC_ASLR_FORCE_DISABLE;
+       if (procctl (P_PID, getpid (), PROC_ASLR_CTL, &ctl) == -1)
+         {
+           warning (_("Error disabling address space randomization: %s"),
+                    safe_strerror (errno));
+           return;
+         }
+
+       m_aslr_ctl_set = true;
+      }
+  }
+
+  ~maybe_disable_address_space_randomization ()
+  {
+    if (m_aslr_ctl_set)
+      {
+       if (procctl (P_PID, getpid (), PROC_ASLR_CTL, &m_aslr_ctl) == -1)
+         warning (_("Error restoring address space randomization: %s"),
+                  safe_strerror (errno));
+      }
+  }
+
+  DISABLE_COPY_AND_ASSIGN (maybe_disable_address_space_randomization);
+
+private:
+  bool m_aslr_ctl_set = false;
+  int m_aslr_ctl = 0;
+};
+#endif
+
+void
+fbsd_nat_target::create_inferior (const char *exec_file,
+                                 const std::string &allargs,
+                                 char **env, int from_tty)
+{
+#ifdef PROC_ASLR_CTL
+  maybe_disable_address_space_randomization restore_aslr_ctl
+    (disable_randomization);
+#endif
+
+  inf_ptrace_target::create_inferior (exec_file, allargs, env, from_tty);
+}
+
 #ifdef TDP_RFPPWAIT
 /* Target hook for follow_fork.  On entry and at return inferior_ptid is
    the ptid of the followed inferior.  */
@@ -1526,6 +1591,16 @@ fbsd_nat_target::supports_multi_process ()
   return true;
 }
 
+bool
+fbsd_nat_target::supports_disable_randomization ()
+{
+#ifdef PROC_ASLR_CTL
+  return true;
+#else
+  return false;
+#endif
+}
+
 void _initialize_fbsd_nat ();
 void
 _initialize_fbsd_nat ()
index 772655d320e6a5cd4e14cfc4d73acf62b3c63423..1fdb939935c634afa8462c558b3733447342bd77 100644 (file)
@@ -67,6 +67,9 @@ public:
   thread_control_capabilities get_thread_control_capabilities () override
   { return tc_schedlock; }
 
+  void create_inferior (const char *, const std::string &,
+                       char **, int) override;
+
   void resume (ptid_t, int, enum gdb_signal) override;
 
   ptid_t wait (ptid_t, struct target_waitstatus *, target_wait_flags) override;
@@ -98,6 +101,8 @@ public:
 #endif
 
   bool supports_multi_process () override;
+
+  bool supports_disable_randomization () override;
 };
 
 #endif /* fbsd-nat.h */