8000 Secure Unix-domain sockets of "make check" temporary clusters. · larkly/postgres-docker@556906d · GitHub
[go: up one dir, main page]

Skip to content

Commit 556906d

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 7907098 commit 556906d

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
@@ -60,21 +60,14 @@ gmake check
6060

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

@@ -114,6 +107,17 @@ gmake MAX_CONNECTIONS=10 check
114107
runs no more than ten tests concurrently.
115108
</para>
116109

110+
<para>
111+
To protect your operating system user account, the test driver places the
112+
server's socket in a relative subdirectory inaccessible to other users.
113+
Since most systems constrain the length of socket paths well
114+
below <literal>_POSIX_PATH_MAX</>, testing may fail to start from a
115+
directory with a long name. Work around this problem by pointing
116+
the <envar>PG_REGRESS_SOCK_DIR</> environment variable to a substitute
117+
socket directory having a shorter path. On a multi-user system, give that
118+
directory mode <literal>0700</>.
119+
</para>
120+
117121
<para>
118122
To run the tests after installation<![%standalone-ignore;[ (see <xref linkend="installation">)]]>,
119123
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
@@ -96,6 +96,7 @@ static const char *progname;
9696
static char *logfilename;
9797
static FILE *logfile;
9898
static char *difffilename;
99+
static char *sockdir;
99100

100101
static _resultmap *resultmap = NULL;
101102

@@ -753,8 +754,7 @@ initialize_environment(void)
753754
* the wrong postmaster, or otherwise behave in nondefault ways. (Note
754755
* we also use psql's -X switch consistently, so that ~/.psqlrc files
755756
* won't mess things up.) Also, set PGPORT to the temp port, and set
756-
* or unset PGHOST depending on whether we are using TCP or Unix
757-
* sockets.
757+
* PGHOST depending on whether we are using TCP or Unix sockets.
758758
*/
759759
unsetenv("PGDATABASE");
760760
unsetenv("PGUSER");
@@ -766,7 +766,24 @@ initialize_environment(void)
766766
if (hostname != NULL)
767767
doputenv("PGHOST", hostname);
768768
else
769-
unsetenv("PGHOST");
769+
{
770+
sockdir = getenv("PG_REGRESS_SOCK_DIR");
771+
if (!sockdir)
772+
{
773+
/*
774+
* Since initdb creates the data directory with secure
775+
* permissions, we place the socket there. This ensures no
776+
* other OS user can open our socket to exploit our use of
777+
* trust authentication. Compared to using the compiled-in
778+
* DEFAULT_PGSOCKET_DIR, this also permits testing to work in
779+
* builds that relocate it to a directory not writable to the
780+
* build/test user.
781+
*/
782+
sockdir = malloc(strlen(temp_install) + sizeof("/data"));
783+
sprintf(sockdir, "%s/data", temp_install);
784+
}
785+
doputenv("PGHOST", sockdir);
786+
}
770787
unsetenv("PGHOSTADDR");
771788
if (port != -1)
772789
{
@@ -2185,10 +2202,11 @@ regression_main(int argc, char *argv[], init_function ifunc, test_function tfunc
21852202
*/
21862203
header(_("starting postmaster"));
21872204
snprintf(buf, sizeof(buf),
2188-
SYSTEMQUOTE "\"%s/postgres\" -D \"%s/data\" -F%s -c \"listen_addresses=%s\" > \"%s/log/postmaster.log\" 2>&1" SYSTEMQUOTE,
2189-
bindir, temp_install,
2190-
debug ? " -d 5" : "",
2191-
hostname ? hostname : "",
2205+
SYSTEMQUOTE "\"%s/postgres\" -D \"%s/data\" -F%s "
2206+
"-c \"listen_addresses=%s\" -k \"%s\" "
2207+
"> \"%s/log/postmaster.log\" 2>&1" SYSTEMQUOTE,
2208+
bindir, temp_install, debug ? " -d 5" : "",
2209+
hostname ? hostname : "", sockdir ? sockdir : "",
21922210
outputdir);
21932211
postmaster_pid = spawn_process(buf);
21942212
if (postmaster_pid == INVALID_PID)

0 commit comments

Comments
 (0)
0