Skip to content

Commit f3e7806

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. Suggested-by: Andres Freund <andres@anarazel.de> Tested-by: Bossart, Nathan <bossartn@amazon.com> Discussion: https://postgr.es/m/20210806032944.m4tz7j2w47mant26%40alap3.anarazel.de
1 parent ee41960 commit f3e7806

File tree

8 files changed

+52
-1
lines changed

8 files changed

+52
-1
lines changed

configure

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

1360513605

13606-
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/sockio.h sys/tas.h sys/uio.h sys/un.h termios.h ucred.h wctype.h
13606+
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/sockio.h sys/tas.h sys/uio.h sys/un.h termios.h ucred.h wctype.h
1360713607
do :
1360813608
as_ac_Header=`$as_echo "ac_cv_header_$ac_header" | $as_tr_sh`
1360913609
ac_fn_c_check_header_mongrel "$LINENO" "$ac_header" "$as_ac_Header" "$ac_includes_default"

configure.ac

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1404,6 +1404,7 @@ AC_CHECK_HEADERS(m4_normalize([
14041404
sys/epoll.h
14051405
sys/event.h
14061406
sys/ipc.h
1407+
sys/personality.h
14071408
sys/prctl.h
14081409
sys/procctl.h
14091410
sys/pstat.h

src/bin/pg_ctl/pg_ctl.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,10 @@ start_postmaster(void)
451451
fflush(stdout);
452452
fflush(stderr);
453453

454+
#ifdef EXEC_BACKEND
455+
pg_disable_aslr();
456+
#endif
457+
454458
pm_pid = fork();
455459
if (pm_pid < 0)
456460
{

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
/*
2937
* Hacky solution to allow expressing both frontend and backend error reports
3038
* in one macro call. First argument of log_error is an errcode() call of
@@ -470,6 +478,31 @@ set_pglocale_pgservice(const char *argv0, const char *app)
470478
}
471479
}
472480

481+
#ifdef EXEC_BACKEND
482+
/*
483+
* For the benefit of PostgreSQL developers testing EXEC_BACKEND on Unix
484+
* systems (code paths normally exercised only on Windows), provide a way to
485+
* disable address space layout randomization, if we know how on this platform.
486+
* Otherwise, backends may fail to attach to shared memory at the fixed address
487+
* chosen by the postmaster. (See also the macOS-specific hack in
488+
* sysv_shmem.c.)
489+
*/
490+
int
491+
pg_disable_aslr(void)
492+
{
493+
#if defined(HAVE_SYS_PERSONALITY_H)
494+
return personality(ADDR_NO_RANDOMIZE);
495+
#elif defined(HAVE_SYS_PROCCTL_H) && defined(PROC_ASLR_FORCE_DISABLE)
496+
int data = PROC_ASLR_FORCE_DISABLE;
497+
498+
return procctl(P_PID, 0, PROC_ASLR_CTL, &data);
499+
#else
500+
errno = ENOSYS;
501+
return -1;
502+
#endif
503+
}
504+
#endif
505+
473506
#ifdef WIN32
474507

475508
/*

src/include/pg_config.h.in

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

617+
/* Define to 1 if you have the <sys/personality.h> header file. */
618+
#undef HAVE_SYS_PERSONALITY_H
619+
617620
/* Define to 1 if you have the <sys/prctl.h> header file. */
618621
#undef HAVE_SYS_PRCTL_H
619622

src/include/port.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ extern char *pipe_read_line(char *cmd, char *line, int maxsize);
140140
/* Doesn't belong here, but this is used with find_other_exec(), so... */
141141
#define PG_BACKEND_VERSIONSTR "postgres (PostgreSQL) " PG_VERSION "\n"
142142

143+
#ifdef EXEC_BACKEND
144+
/* Disable ASLR before exec, for developer builds only (in exec.c) */
145+
extern int pg_disable_aslr(void);
146+
#endif
147+
143148

144149
#if defined(WIN32) || defined(__CYGWIN__)
145150
#define EXE ".exe"

src/test/regress/pg_regress.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,6 +1104,10 @@ spawn_process(const char *cmdline)
11041104
if (logfile)
11051105
fflush(logfile);
11061106

1107+
#ifdef EXEC_BACKEND
1108+
pg_disable_aslr();
1109+
#endif
1110+
11071111
pid = fork();
11081112
if (pid == -1)
11091113
{

src/tools/msvc/Solution.pm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@ sub GenerateFiles
397397
HAVE_SYS_EPOLL_H => undef,
398398
HAVE_SYS_EVENT_H => undef,
399399
HAVE_SYS_IPC_H => undef,
400+
HAVE_SYS_PERSONALITY_H => undef,
400401
HAVE_SYS_PRCTL_H => undef,
401402
HAVE_SYS_PROCCTL_H => undef,
402403
HAVE_SYS_PSTAT_H => undef,

0 commit comments

Comments
 (0)