]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Make EXEC_BACKEND more convenient on Linux and FreeBSD.
authorMichael Paquier <michael@paquier.xyz>
Wed, 8 Feb 2023 04:09:52 +0000 (13:09 +0900)
committerMichael Paquier <michael@paquier.xyz>
Wed, 8 Feb 2023 04:09:52 +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 <andres@anarazel.de>
Tested-by: Bossart, Nathan <bossartn@amazon.com>
Discussion: https://postgr.es/m/20210806032944.m4tz7j2w47mant26%40alap3.anarazel.de
Backpatch-through: 12

configure
configure.in
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

index 997a852bb51c51100248c2d5ff453eb219cd5a07..648e367678fd214fa726faa3f67dab797be7ffcd 100755 (executable)
--- a/configure
+++ b/configure
@@ -13328,7 +13328,7 @@ $as_echo "#define HAVE_STDBOOL_H 1" >>confdefs.h
 fi
 
 
-for ac_header in atomic.h copyfile.h crypt.h fp_class.h getopt.h ieeefp.h ifaddrs.h langinfo.h mbarrier.h poll.h sys/epoll.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/sockio.h sys/tas.h sys/un.h termios.h ucred.h utime.h wchar.h wctype.h
+for ac_header in atomic.h copyfile.h crypt.h fp_class.h getopt.h ieeefp.h ifaddrs.h langinfo.h mbarrier.h poll.h sys/epoll.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/sockio.h sys/tas.h sys/un.h termios.h ucred.h utime.h wchar.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 70e86fde3f28e64eb568280f025992ec4806eed3..91d12c7de42aa136956f7020d25d597d82bc5d03 100644 (file)
@@ -1407,6 +1407,7 @@ AC_CHECK_HEADERS(m4_normalize([
        poll.h
        sys/epoll.h
        sys/ipc.h
+       sys/personality.h
        sys/prctl.h
        sys/procctl.h
        sys/pstat.h
index a6f2140e3a009cb4b59477a761b58d0f89370238..4f566441226cd3ea39a8dca332c481a44df97744 100644 (file)
@@ -459,6 +459,10 @@ start_postmaster(void)
        fflush(stdout);
        fflush(stderr);
 
+#ifdef EXEC_BACKEND
+       pg_disable_aslr();
+#endif
+
        pm_pid = fork();
        if (pm_pid < 0)
        {
index ccdb443ea8853c31159e0ca33eb4f2729d5dbdcd..142700837977c6764ba1b1c34a61f582ac199d87 100644 (file)
 #include <sys/wait.h>
 #include <unistd.h>
 
+#ifdef EXEC_BACKEND
+#if defined(HAVE_SYS_PERSONALITY_H)
+#include <sys/personality.h>
+#elif defined(HAVE_SYS_PROCCTL_H)
+#include <sys/procctl.h>
+#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 */
@@ -623,6 +631,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 472efea4313a96e8ca23ea8dd73befa7b34c081f..84ee764daf34cb696ea4889eadbcae4465ec0f15 100644 (file)
 /* Define to 1 if you have the <sys/ipc.h> header file. */
 #undef HAVE_SYS_IPC_H
 
+/* Define to 1 if you have the <sys/personality.h> header file. */
+#undef HAVE_SYS_PERSONALITY_H
+
 /* Define to 1 if you have the <sys/prctl.h> header file. */
 #undef HAVE_SYS_PRCTL_H
 
index ecf52bcc9096cbba76d3008651d40bb24246649f..2229ec79c1cb611e02ce0793acb287c70afb5886 100644 (file)
@@ -114,6 +114,11 @@ extern int find_other_exec(const char *argv0, const char *target,
 /* 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 bc16d04b9f13fff6d833502d0cd63c5a537f3684..627a5b44b48d6c60dbb9de217f56b7afe0333c35 100644 (file)
@@ -1175,6 +1175,10 @@ spawn_process(const char *cmdline)
        if (logfile)
                fflush(logfile);
 
+#ifdef EXEC_BACKEND
+       pg_disable_aslr();
+#endif
+
        pid = fork();
        if (pid == -1)
        {