Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6599d8f

Browse files
committed
Allow root-owned SSL private keys in libpq, not only the backend.
This change makes libpq apply the same private-key-file ownershipand permissions checks that we have used in the backend since commit9a83564. Namely, that the private key can be owned by either thecurrent user or root (with different file permissions allowed in thetwo cases). This allows system-wide management of key files, whichis just as sensible on the client side as the server, particularlywhen the client is itself some application daemon.Sync the comments about this between libpq and the backend, too.Back-patch ofa59c795 and50f0347 into all supported branches.David SteeleDiscussion:https://postgr.es/m/f4b7bc55-97ac-9e69-7398-335e212f7743@pgmasters.net
1 parent9b2d762 commit6599d8f

File tree

3 files changed

+69
-25
lines changed

3 files changed

+69
-25
lines changed

‎doc/src/sgml/libpq.sgml

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7685,23 +7685,35 @@ ldap://ldap.acme.com/cn=dbserver,cn=hosts?pgconnectinfo?base?(objectclass=*)
76857685
<para>
76867686
If the server attempts to verify the identity of the
76877687
client by requesting the client's leaf certificate,
7688-
<application>libpq</application> will send thecertificates stored in
7688+
<application>libpq</application> will send thecertificate(s) stored in
76897689
file <filename>~/.postgresql/postgresql.crt</filename> in the user's home
76907690
directory. The certificates must chain to the root certificate trusted
76917691
by the server. A matching
76927692
private key file <filename>~/.postgresql/postgresql.key</filename> must also
7693-
be present. The private
7694-
key file must not allow any access to world or group; achieve this by the
7695-
command <command>chmod 0600 ~/.postgresql/postgresql.key</command>.
7693+
be present.
76967694
On Microsoft Windows these files are named
76977695
<filename>%APPDATA%\postgresql\postgresql.crt</filename> and
7698-
<filename>%APPDATA%\postgresql\postgresql.key</filename>, and there
7699-
is no special permissions check since the directory is presumed secure.
7696+
<filename>%APPDATA%\postgresql\postgresql.key</filename>.
77007697
The location of the certificate and key files can be overridden by the
7701-
connection parameters <literal>sslcert</literal> and <literal>sslkey</literal> or the
7698+
connection parameters <literal>sslcert</literal>
7699+
and <literal>sslkey</literal>, or by the
77027700
environment variables <envar>PGSSLCERT</envar> and <envar>PGSSLKEY</envar>.
77037701
</para>
77047702

7703+
<para>
7704+
On Unix systems, the permissions on the private key file must disallow
7705+
any access to world or group; achieve this by a command such as
7706+
<command>chmod 0600 ~/.postgresql/postgresql.key</command>.
7707+
Alternatively, the file can be owned by root and have group read access
7708+
(that is, <literal>0640</literal> permissions). That setup is intended
7709+
for installations where certificate and key files are managed by the
7710+
operating system. The user of <application>libpq</application> should
7711+
then be made a member of the group that has access to those certificate
7712+
and key files. (On Microsoft Windows, there is no file permissions
7713+
check, since the <filename>%APPDATA%\postgresql</filename> directory is
7714+
presumed secure.)
7715+
</para>
7716+
77057717
<para>
77067718
The first certificate in <filename>postgresql.crt</filename> must be the
77077719
client's certificate because it must match the client's private key.

‎src/backend/libpq/be-secure-common.c

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ check_ssl_key_file_permissions(const char *ssl_key_file, bool isServerStart)
143143
return false;
144144
}
145145

146+
/* Key file must be a regular file */
146147
if (!S_ISREG(buf.st_mode))
147148
{
148149
ereport(loglevel,
@@ -153,9 +154,19 @@ check_ssl_key_file_permissions(const char *ssl_key_file, bool isServerStart)
153154
}
154155

155156
/*
156-
* Refuse to load key files owned by users other than us or root.
157+
* Refuse to load key files owned by users other than us or root, and
158+
* require no public access to the key file. If the file is owned by us,
159+
* require mode 0600 or less. If owned by root, require 0640 or less to
160+
* allow read access through either our gid or a supplementary gid that
161+
* allows us to read system-wide certificates.
157162
*
158-
* XXX surely we can check this on Windows somehow, too.
163+
* Note that similar checks are performed in
164+
* src/interfaces/libpq/fe-secure-openssl.c so any changes here may need
165+
* to be made there as well.
166+
*
167+
* Ideally we would do similar permissions checks on Windows, but it is
168+
* not clear how that would work since Unix-style permissions may not be
169+
* available.
159170
*/
160171
#if !defined(WIN32)&& !defined(__CYGWIN__)
161172
if (buf.st_uid!=geteuid()&&buf.st_uid!=0)
@@ -166,20 +177,7 @@ check_ssl_key_file_permissions(const char *ssl_key_file, bool isServerStart)
166177
ssl_key_file)));
167178
return false;
168179
}
169-
#endif
170180

171-
/*
172-
* Require no public access to key file. If the file is owned by us,
173-
* require mode 0600 or less. If owned by root, require 0640 or less to
174-
* allow read access through our gid, or a supplementary gid that allows
175-
* to read system-wide certificates.
176-
*
177-
* XXX temporarily suppress check when on Windows, because there may not
178-
* be proper support for Unix-y file permissions. Need to think of a
179-
* reasonable check to apply on Windows. (See also the data directory
180-
* permission check in postmaster.c)
181-
*/
182-
#if !defined(WIN32)&& !defined(__CYGWIN__)
183181
if ((buf.st_uid==geteuid()&&buf.st_mode& (S_IRWXG |S_IRWXO))||
184182
(buf.st_uid==0&&buf.st_mode& (S_IWGRP |S_IXGRP |S_IRWXO)))
185183
{

‎src/interfaces/libpq/fe-secure-openssl.c

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,11 +1187,45 @@ initialize_SSL(PGconn *conn)
11871187
fnbuf);
11881188
return-1;
11891189
}
1190-
#ifndefWIN32
1191-
if (!S_ISREG(buf.st_mode)||buf.st_mode& (S_IRWXG |S_IRWXO))
1190+
1191+
/* Key file must be a regular file */
1192+
if (!S_ISREG(buf.st_mode))
1193+
{
1194+
printfPQExpBuffer(&conn->errorMessage,
1195+
libpq_gettext("private key file \"%s\" is not a regular file"),
1196+
fnbuf);
1197+
return-1;
1198+
}
1199+
1200+
/*
1201+
* Refuse to load key files owned by users other than us or root, and
1202+
* require no public access to the key file. If the file is owned by
1203+
* us, require mode 0600 or less. If owned by root, require 0640 or
1204+
* less to allow read access through either our gid or a supplementary
1205+
* gid that allows us to read system-wide certificates.
1206+
*
1207+
* Note that similar checks are performed in
1208+
* src/backend/libpq/be-secure-common.c so any changes here may need
1209+
* to be made there as well.
1210+
*
1211+
* Ideally we would do similar permissions checks on Windows, but it
1212+
* is not clear how that would work since Unix-style permissions may
1213+
* not be available.
1214+
*/
1215+
#if !defined(WIN32)&& !defined(__CYGWIN__)
1216+
if (buf.st_uid!=geteuid()&&buf.st_uid!=0)
1217+
{
1218+
printfPQExpBuffer(&conn->errorMessage,
1219+
libpq_gettext("private key file \"%s\" must be owned by the current user or root\n"),
1220+
fnbuf);
1221+
return-1;
1222+
}
1223+
1224+
if ((buf.st_uid==geteuid()&&buf.st_mode& (S_IRWXG |S_IRWXO))||
1225+
(buf.st_uid==0&&buf.st_mode& (S_IWGRP |S_IXGRP |S_IRWXO)))
11921226
{
11931227
printfPQExpBuffer(&conn->errorMessage,
1194-
libpq_gettext("private key file \"%s\" has group or world access;permissions should beu=rw (0600) or less\n"),
1228+
libpq_gettext("private key file \"%s\" has group or world access;file must have permissionsu=rw (0600) or less if owned by the current user, or permissions u=rw,g=r (0640) or less if owned by root\n"),
11951229
fnbuf);
11961230
return-1;
11971231
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp