Skip to content

Commit 6b4dba7

Browse files
committed
Make EXEC_BACKEND more convenient on Linux and FreeBSD.
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
1 parent 533cc39 commit 6b4dba7

File tree

7 files changed

+51
-1
lines changed

7 files changed

+51
-1
lines changed

configure

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13328,7 +13328,7 @@ $as_echo "#define HAVE_STDBOOL_H 1" >>confdefs.h
1332813328
fi
1332913329

1333013330

13331-
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
13331+
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
1333213332
do :
1333313333
as_ac_Header=`$as_echo "ac_cv_header_$ac_header" | $as_tr_sh`
1333413334
ac_fn_c_check_header_mongrel "$LINENO" "$ac_header" "$as_ac_Header" "$ac_includes_default"

configure.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,6 +1407,7 @@ AC_CHECK_HEADERS(m4_normalize([
14071407
poll.h
14081408
sys/epoll.h
14091409
sys/ipc.h
1410+
sys/personality.h
14101411
sys/prctl.h
14111412
sys/procctl.h
14121413
sys/pstat.h

src/bin/pg_ctl/pg_ctl.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,10 @@ start_postmaster(void)
459459
fflush(stdout);
460460
fflush(stderr);
461461

462+
#ifdef EXEC_BACKEND
463+
pg_disable_aslr();
464+
#endif
465+
462466
pm_pid = fork();
463467
if (pm_pid < 0)
464468
{

src/common/exec.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@
2525
#include <sys/wait.h>
2626
#include <unistd.h>
2727

28+
#ifdef EXEC_BACKEND
29+
#if defined(HAVE_SYS_PERSONALITY_H)
30+
#include <sys/personality.h>
31+
#elif defined(HAVE_SYS_PROCCTL_H)
32+
#include <sys/procctl.h>
33+
#endif
34+
#endif
35+
2836
/* Inhibit mingw CRT's auto-globbing of command line arguments */
2937
#if defined(WIN32) && !defined(_MSC_VER)
3038
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)
623631
}
624632
}
625633

634+
#ifdef EXEC_BACKEND
635+
/*
636+
* For the benefit of PostgreSQL developers testing EXEC_BACKEND on Unix
637+
* systems (code paths normally exercised only on Windows), provide a way to
638+
* disable address space layout randomization, if we know how on this platform.
639+
* Otherwise, backends may fail to attach to shared memory at the fixed address
640+
* chosen by the postmaster. (See also the macOS-specific hack in
641+
* sysv_shmem.c.)
642+
*/
643+
int
644+
pg_disable_aslr(void)
645+
{
646+
#if defined(HAVE_SYS_PERSONALITY_H)
647+
return personality(ADDR_NO_RANDOMIZE);
648+
#elif defined(HAVE_SYS_PROCCTL_H) && defined(PROC_ASLR_FORCE_DISABLE)
649+
int data = PROC_ASLR_FORCE_DISABLE;
650+
651+
return procctl(P_PID, 0, PROC_ASLR_CTL, &data);
652+
#else
653+
errno = ENOSYS;
654+
return -1;
655+
#endif
656+
}
657+
#endif
658+
626659
#ifdef WIN32
627660

628661
/*

src/include/pg_config.h.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,9 @@
612612
/* Define to 1 if you have the <sys/ipc.h> header file. */
613613
#undef HAVE_SYS_IPC_H
614614

615+
/* Define to 1 if you have the <sys/personality.h> header file. */
616+
#undef HAVE_SYS_PERSONALITY_H
617+
615618
/* Define to 1 if you have the <sys/prctl.h> header file. */
616619
#undef HAVE_SYS_PRCTL_H
617620

src/include/port.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ extern int find_other_exec(const char *argv0, const char *target,
114114
/* Doesn't belong here, but this is used with find_other_exec(), so... */
115115
#define PG_BACKEND_VERSIONSTR "postgres (PostgreSQL) " PG_VERSION "\n"
116116

117+
#ifdef EXEC_BACKEND
118+
/* Disable ASLR before exec, for developer builds only (in exec.c) */
119+
extern int pg_disable_aslr(void);
120+
#endif
121+
117122

118123
#if defined(WIN32) || defined(__CYGWIN__)
119124
#define EXE ".exe"

src/test/regress/pg_regress.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,6 +1175,10 @@ spawn_process(const char *cmdline)
11751175
if (logfile)
11761176
fflush(logfile);
11771177

1178+
#ifdef EXEC_BACKEND
1179+
pg_disable_aslr();
1180+
#endif
1181+
11781182
pid = fork();
11791183
if (pid == -1)
11801184
{

0 commit comments

Comments
 (0)