Skip to content

Commit 8cade31

Browse files
committed
Secure Unix-domain sockets of "make check" temporary clusters.
Any OS user able to access the socket can connect as the bootstrap superuser and in turn execute arbitrary code as the OS user running the test. Protect against that by placing the socket in the temporary data directory, which has mode 0700 thanks to initdb. Back-patch to 8.4 (all supported versions). The hazard remains wherever the temporary cluster accepts TCP connections, notably on Windows. Attempts to run "make check" from a directory with a long name will now fail. An alternative not sharing that problem was to place the socket in a subdirectory of /tmp, but that is only secure if /tmp is sticky. The PG_REGRESS_SOCK_DIR environment variable is available as a workaround when testing from long directory paths. As a convenient side effect, this lets testing proceed smoothly in builds that override DEFAULT_PGSOCKET_DIR. Popular non-default values like /var/run/postgresql are often unwritable to the build user. Security: CVE-2014-0067
1 parent 2fa4243 commit 8cade31

File tree

2 files changed

+44
-22
lines changed

2 files changed

+44
-22
lines changed

doc/src/sgml/regress.sgml

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,21 +57,14 @@ gmake check
5757

5858
<warning>
5959
<para>
60-
This test method starts a temporary server, which is configured to accept
61-
any connection originating on the local machine. Any local user can gain
62-
database superuser privileges when connecting to this server, and could
63-
in principle exploit all privileges of the operating-system user running
64-
the tests. Therefore, it is not recommended that you use <literal>gmake
65-
check</> on machines shared with untrusted users. Instead, run the tests
66-
after completing the installation, as described in the next section.
67-
</para>
68-
69-
<para>
70-
On Unix-like machines, this danger can be avoided if the temporary
71-
server's socket file is made inaccessible to other users, for example
72-
by running the tests in a protected chroot. On Windows, the temporary
73-
server opens a locally-accessible TCP socket, so filesystem protections
74-
cannot help.
60+
On systems lacking Unix-domain sockets, notably Windows, this test method
61+
starts a temporary server configured to accept any connection originating
62+
on the local machine. Any local user can gain database superuser
63+
privileges when connecting to this server, and could in principle exploit
64+
all privileges of the operating-system user running the tests. Therefore,
65+
it is not recommended that you use <literal>gmake check</> on an affected
66+
system shared with untrusted users. Instead, run the tests after
67+
completing the installation, as described in the next section.
7568
</para>
7669
</warning>
7770

@@ -111,6 +104,17 @@ gmake MAX_CONNECTIONS=10 check
111104
runs no more than ten tests concurrently.
112105
</para>
113106

107+
<para>
108+
To protect your operating system user account, the test driver places the
109+
server's socket in a relative subdirectory inaccessible to other users.
110+
Since most systems constrain the length of socket paths well
111+
below <literal>_POSIX_PATH_MAX</>, testing may fail to start from a
112+
directory with a long name. Work around this problem by pointing
113+
the <envar>PG_REGRESS_SOCK_DIR</> environment variable to a substitute
114+
socket directory having a shorter path. On a multi-user system, give that
115+
directory mode <literal>0700</>.
116+
</para>
117+
114118
<para>
115119
To run the tests after installation<![%standalone-ignore;[ (see <xref linkend="installation">)]]>,
116120
initialize a data area and start the

src/test/regress/pg_regress.c

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ static const char *progname;
106106
static char *logfilename;
107107
static FILE *logfile;
108108
static char *difffilename;
109+
static char *sockdir;
109110

110111
static _resultmap *resultmap = NULL;
111112

@@ -763,8 +764,7 @@ initialize_environment(void)
763764
* the wrong postmaster, or otherwise behave in nondefault ways. (Note
764765
* we also use psql's -X switch consistently, so that ~/.psqlrc files
765766
* won't mess things up.) Also, set PGPORT to the temp port, and set
766-
* or unset PGHOST depending on whether we are using TCP or Unix
767-
* sockets.
767+
* PGHOST depending on whether we are using TCP or Unix sockets.
768768
*/
769769
unsetenv("PGDATABASE");
770770
unsetenv("PGUSER");
@@ -776,7 +776,24 @@ initialize_environment(void)
776776
if (hostname != NULL)
777777
doputenv("PGHOST", hostname);
778778
else
779-
unsetenv("PGHOST");
779+
{
780+
sockdir = getenv("PG_REGRESS_SOCK_DIR");
781+
if (!sockdir)
782+
{
783+
/*
784+
* Since initdb creates the data directory with secure
785+
* permissions, we place the socket there. This ensures no
786+
* other OS user can open our socket to exploit our use of
787+
* trust authentication. Compared to using the compiled-in
788+
* DEFAULT_PGSOCKET_DIR, this also permits testing to work in
789+
* builds that relocate it to a directory not writable to the
790+
* build/test user.
791+
*/
792+
sockdir = malloc(strlen(temp_install) + sizeof("/data"));
793+
sprintf(sockdir, "%s/data", temp_install);
794+
}
795+
doputenv("PGHOST", sockdir);
796+
}
780797
unsetenv("PGHOSTADDR");
781798
if (port != -1)
782799
{
@@ -2203,10 +2220,11 @@ regression_main(int argc, char *argv[], init_function ifunc, test_function tfunc
22032220
*/
22042221
header(_("starting postmaster"));
22052222
snprintf(buf, sizeof(buf),
2206-
SYSTEMQUOTE "\"%s/postgres\" -D \"%s/data\" -F%s -c \"listen_addresses=%s\" > \"%s/log/postmaster.log\" 2>&1" SYSTEMQUOTE,
2207-
bindir, temp_install,
2208-
debug ? " -d 5" : "",
2209-
hostname ? hostname : "",
2223+
SYSTEMQUOTE "\"%s/postgres\" -D \"%s/data\" -F%s "
2224+
"-c \"listen_addresses=%s\" -k \"%s\" "
2225+
"> \"%s/log/postmaster.log\" 2>&1" SYSTEMQUOTE,
2226+
bindir, temp_install, debug ? " -d 5" : "",
2227+
hostname ? hostname : "", sockdir ? sockdir : "",
22102228
outputdir);
22112229
postmaster_pid = spawn_process(buf);
22122230
if (postmaster_pid == INVALID_PID)

0 commit comments

Comments
 (0)