Make EXEC_BACKEND more convenient on Linux and FreeBSD.
authorMichael Paquier
Wed, 8 Feb 2023 04:09:27 +0000 (13:09 +0900)
committerMichael Paquier
Wed, 8 Feb 2023 04:09:27 +0000 (13:09 +0900)
Try to disable ASLR when building in EXEC_BACKEND mode, to avoid random
memory mapping failures while testing.  For developer use only, no
effect on regular builds.

This has been originally applied as of f3e7806 for v15~, but
recently-added buildfarm member gokiburi tests this configuration on
older branches as well, causing it to fail randomly as ASLR would be
enabled.

Suggested-by: Andres Freund
Tested-by: Bossart, Nathan
Discussion: https://postgr.es/m/20210806032944.m4tz7j2w47mant26%40alap3.anarazel.de
Backpatch-through: 12

configure
configure.ac
src/bin/pg_ctl/pg_ctl.c
src/common/exec.c
src/include/pg_config.h.in
src/include/port.h
src/test/regress/pg_regress.c
src/tools/msvc/Solution.pm

index 863f9eb9cceb6ae00d07652f3b558a8b5ed46fc7..abaebc0e3ed954382be122b15f488d8a55f58e17 100755 (executable)
--- a/configure
+++ b/configure
@@ -13781,7 +13781,7 @@ $as_echo "#define HAVE_STDBOOL_H 1" >>confdefs.h
 fi
 
 
-for ac_header in atomic.h copyfile.h execinfo.h getopt.h ifaddrs.h langinfo.h mbarrier.h poll.h sys/epoll.h sys/event.h sys/ipc.h sys/prctl.h sys/procctl.h sys/pstat.h sys/resource.h sys/select.h sys/sem.h sys/shm.h sys/signalfd.h sys/sockio.h sys/tas.h sys/uio.h sys/un.h termios.h ucred.h wctype.h
+for ac_header in atomic.h copyfile.h execinfo.h getopt.h ifaddrs.h langinfo.h mbarrier.h poll.h sys/epoll.h sys/event.h sys/ipc.h sys/personality.h sys/prctl.h sys/procctl.h sys/pstat.h sys/resource.h sys/select.h sys/sem.h sys/shm.h sys/signalfd.h sys/sockio.h sys/tas.h sys/uio.h sys/un.h termios.h ucred.h wctype.h
 do :
   as_ac_Header=`$as_echo "ac_cv_header_$ac_header" | $as_tr_sh`
 ac_fn_c_check_header_mongrel "$LINENO" "$ac_header" "$as_ac_Header" "$ac_includes_default"
index 0d8a2965d8a5f2ec9dbb8dcbd58d8a724dd6f87d..350a1d4842a1c70e63ae543d38d5b1b145021ec9 100644 (file)
@@ -1424,6 +1424,7 @@ AC_CHECK_HEADERS(m4_normalize([
    sys/epoll.h
    sys/event.h
    sys/ipc.h
+   sys/personality.h
    sys/prctl.h
    sys/procctl.h
    sys/pstat.h
index f21849a257eddf2fb5ff8c97e746795e7cd0d860..552e3a6a1c8f76950c915034b2c437ea55128e0d 100644 (file)
@@ -453,6 +453,10 @@ start_postmaster(void)
    fflush(stdout);
    fflush(stderr);
 
+#ifdef EXEC_BACKEND
+   pg_disable_aslr();
+#endif
+
    pm_pid = fork();
    if (pm_pid < 0)
    {
index 7dd2f8c49421f734bbc444aa62a37a7d832fc07a..95e1c30d790e3ddb3f9f4e9291e8803dccdd9344 100644 (file)
 #include 
 #include 
 
+#ifdef EXEC_BACKEND
+#if defined(HAVE_SYS_PERSONALITY_H)
+#include 
+#elif defined(HAVE_SYS_PROCCTL_H)
+#include 
+#endif
+#endif
+
 /* Inhibit mingw CRT's auto-globbing of command line arguments */
 #if defined(WIN32) && !defined(_MSC_VER)
 extern int _CRT_glob = 0; /* 0 turns off globbing; 1 turns it on */
@@ -475,6 +483,31 @@ set_pglocale_pgservice(const char *argv0, const char *app)
    }
 }
 
+#ifdef EXEC_BACKEND
+/*
+ * For the benefit of PostgreSQL developers testing EXEC_BACKEND on Unix
+ * systems (code paths normally exercised only on Windows), provide a way to
+ * disable address space layout randomization, if we know how on this platform.
+ * Otherwise, backends may fail to attach to shared memory at the fixed address
+ * chosen by the postmaster.  (See also the macOS-specific hack in
+ * sysv_shmem.c.)
+ */
+int
+pg_disable_aslr(void)
+{
+#if defined(HAVE_SYS_PERSONALITY_H)
+   return personality(ADDR_NO_RANDOMIZE);
+#elif defined(HAVE_SYS_PROCCTL_H) && defined(PROC_ASLR_FORCE_DISABLE)
+   int         data = PROC_ASLR_FORCE_DISABLE;
+
+   return procctl(P_PID, 0, PROC_ASLR_CTL, &data);
+#else
+   errno = ENOSYS;
+   return -1;
+#endif
+}
+#endif
+
 #ifdef WIN32
 
 /*
index 5d12b21ccda0b8cede8c42ba05dca57829843120..06fcfb87f61453cd63cb66442d543ca9cb3d8cf4 100644 (file)
 /* Define to 1 if you have the  header file. */
 #undef HAVE_SYS_IPC_H
 
+/* Define to 1 if you have the  header file. */
+#undef HAVE_SYS_PERSONALITY_H
+
 /* Define to 1 if you have the  header file. */
 #undef HAVE_SYS_PRCTL_H
 
index 4c1c1b32272ae05497306317f47dd55a1821d12a..676a93d7399797530dc5a43366eaa68595a7715a 100644 (file)
@@ -138,6 +138,11 @@ extern char *pipe_read_line(char *cmd, char *line, int maxsize);
 /* Doesn't belong here, but this is used with find_other_exec(), so... */
 #define PG_BACKEND_VERSIONSTR "postgres (PostgreSQL) " PG_VERSION "\n"
 
+#ifdef EXEC_BACKEND
+/* Disable ASLR before exec, for developer builds only (in exec.c) */
+extern int pg_disable_aslr(void);
+#endif
+
 
 #if defined(WIN32) || defined(__CYGWIN__)
 #define EXE ".exe"
index 362b638f5050c8472c2619db4736b6b6ac67dfd5..7f8e7e76f46ae856b061a665f88332719d2e8334 100644 (file)
@@ -1207,6 +1207,10 @@ spawn_process(const char *cmdline)
    if (logfile)
        fflush(logfile);
 
+#ifdef EXEC_BACKEND
+   pg_disable_aslr();
+#endif
+
    pid = fork();
    if (pid == -1)
    {
index acd3b8de0661d90d8b81e3a0d4037a5bff2cb428..ad3d6a9075cfdc663432f9a8216fa71336005ee3 100644 (file)
@@ -399,6 +399,7 @@ sub GenerateFiles
        HAVE_SYS_EPOLL_H                         => undef,
        HAVE_SYS_EVENT_H                         => undef,
        HAVE_SYS_IPC_H                           => undef,
+       HAVE_SYS_PERSONALITY_H                   => undef,
        HAVE_SYS_PRCTL_H                         => undef,
        HAVE_SYS_PROCCTL_H                       => undef,
        HAVE_SYS_PSTAT_H                         => undef,