aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/bin/pg_ctl/pg_ctl.c4
-rw-r--r--src/common/exec.c33
-rw-r--r--src/include/pg_config.h.in3
-rw-r--r--src/include/port.h5
-rw-r--r--src/test/regress/pg_regress.c4
5 files changed, 49 insertions, 0 deletions
diff --git a/src/bin/pg_ctl/pg_ctl.c b/src/bin/pg_ctl/pg_ctl.c
index a6f2140e3a0..4f566441226 100644
--- a/src/bin/pg_ctl/pg_ctl.c
+++ b/src/bin/pg_ctl/pg_ctl.c
@@ -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)
{
diff --git a/src/common/exec.c b/src/common/exec.c
index ccdb443ea88..14270083797 100644
--- a/src/common/exec.c
+++ b/src/common/exec.c
@@ -25,6 +25,14 @@
#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
/*
diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in
index 472efea4313..84ee764daf3 100644
--- a/src/include/pg_config.h.in
+++ b/src/include/pg_config.h.in
@@ -612,6 +612,9 @@
/* 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
diff --git a/src/include/port.h b/src/include/port.h
index ecf52bcc909..2229ec79c1c 100644
--- a/src/include/port.h
+++ b/src/include/port.h
@@ -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"
diff --git a/src/test/regress/pg_regress.c b/src/test/regress/pg_regress.c
index bc16d04b9f1..627a5b44b48 100644
--- a/src/test/regress/pg_regress.c
+++ b/src/test/regress/pg_regress.c
@@ -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)
{