Skip to content

Commit 860fe27

Browse files
committed
Fix up usage of krb_server_keyfile GUC parameter.
secure_open_gssapi() installed the krb_server_keyfile setting as KRB5_KTNAME unconditionally, so long as it's not empty. However, pg_GSS_recvauth() only installed it if KRB5_KTNAME wasn't set already, leading to a troubling inconsistency: in theory, clients could see different sets of server principal names depending on whether they use GSSAPI encryption. Always using krb_server_keyfile seems like the right thing, so make both places do that. Also fix up secure_open_gssapi()'s lack of a check for setenv() failure --- it's unlikely, surely, but security-critical actions are no place to be sloppy. Also improve the associated documentation. This patch does nothing about secure_open_gssapi()'s use of setenv(), and indeed causes pg_GSS_recvauth() to use it too. That's nominally against project portability rules, but since this code is only built with --with-gssapi, I do not feel a need to do something about this in the back branches. A fix will be forthcoming for HEAD though. Back-patch to v12 where GSSAPI encryption was introduced. The dubious behavior in pg_GSS_recvauth() goes back further, but it didn't have anything to be inconsistent with, so let it be. Discussion: https://postgr.es/m/2187460.1609263156@sss.pgh.pa.us
1 parent e665769 commit 860fe27

File tree

5 files changed

+31
-32
lines changed

5 files changed

+31
-32
lines changed

doc/src/sgml/client-auth.sgml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,11 +1265,7 @@ omicron bryanh guest1
12651265

12661266
<para>
12671267
The location of the server's keytab file is specified by the <xref
1268-
linkend="guc-krb-server-keyfile"/> configuration
1269-
parameter. The default is
1270-
<filename>FILE:/usr/local/pgsql/etc/krb5.keytab</filename>
1271-
(where the directory part is whatever was specified
1272-
as <varname>sysconfdir</varname> at build time).
1268+
linkend="guc-krb-server-keyfile"/> configuration parameter.
12731269
For security reasons, it is recommended to use a separate keytab
12741270
just for the <productname>PostgreSQL</productname> server rather
12751271
than allowing the server to read the system keytab file.

doc/src/sgml/config.sgml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,10 +1057,16 @@ include_dir 'conf.d'
10571057
</term>
10581058
<listitem>
10591059
<para>
1060-
Sets the location of the Kerberos server key file. See
1061-
<xref linkend="gssapi-auth"/>
1062-
for details. This parameter can only be set in the
1060+
Sets the location of the server's Kerberos key file. The default is
1061+
<filename>FILE:/usr/local/pgsql/etc/krb5.keytab</filename>
1062+
(where the directory part is whatever was specified
1063+
as <varname>sysconfdir</varname> at build time; use
1064+
<literal>pg_config --sysconfdir</literal> to determine that).
1065+
If this parameter is set to an empty string, it is ignored and a
1066+
system-dependent default is used.
1067+
This parameter can only be set in the
10631068
<filename>postgresql.conf</filename> file or on the server command line.
1069+
See <xref linkend="gssapi-auth"/> for more information.
10641070
</para>
10651071
</listitem>
10661072
</varlistentry>

src/backend/libpq/auth.c

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,29 +1054,18 @@ pg_GSS_recvauth(Port *port)
10541054
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
10551055
errmsg("GSSAPI is not supported in protocol version 2")));
10561056

1057-
if (pg_krb_server_keyfile && strlen(pg_krb_server_keyfile) > 0)
1057+
/*
1058+
* Use the configured keytab, if there is one. Unfortunately, Heimdal
1059+
* doesn't support the cred store extensions, so use the env var.
1060+
*/
1061+
if (pg_krb_server_keyfile != NULL && pg_krb_server_keyfile[0] != '\0')
10581062
{
1059-
/*
1060-
* Set default Kerberos keytab file for the Krb5 mechanism.
1061-
*
1062-
* setenv("KRB5_KTNAME", pg_krb_server_keyfile, 0); except setenv()
1063-
* not always available.
1064-
*/
1065-
if (getenv("KRB5_KTNAME") == NULL)
1063+
if (setenv("KRB5_KTNAME", pg_krb_server_keyfile, 1) != 0)
10661064
{
1067-
size_t kt_len = strlen(pg_krb_server_keyfile) + 14;
1068-
char *kt_path = malloc(kt_len);
1069-
1070-
if (!kt_path ||
1071-
snprintf(kt_path, kt_len, "KRB5_KTNAME=%s",
1072-
pg_krb_server_keyfile) != kt_len - 2 ||
1073-
putenv(kt_path) != 0)
1074-
{
1075-
ereport(LOG,
1076-
(errcode(ERRCODE_OUT_OF_MEMORY),
1077-
errmsg("out of memory")));
1078-
return STATUS_ERROR;
1079-
}
1065+
/* The only likely failure cause is OOM, so use that errcode */
1066+
ereport(FATAL,
1067+
(errcode(ERRCODE_OUT_OF_MEMORY),
1068+
errmsg("could not set environment: %m")));
10801069
}
10811070
}
10821071

src/backend/libpq/be-secure-gssapi.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -525,8 +525,16 @@ secure_open_gssapi(Port *port)
525525
* Use the configured keytab, if there is one. Unfortunately, Heimdal
526526
* doesn't support the cred store extensions, so use the env var.
527527
*/
528-
if (pg_krb_server_keyfile != NULL && strlen(pg_krb_server_keyfile) > 0)
529-
setenv("KRB5_KTNAME", pg_krb_server_keyfile, 1);
528+
if (pg_krb_server_keyfile != NULL && pg_krb_server_keyfile[0] != '\0')
529+
{
530+
if (setenv("KRB5_KTNAME", pg_krb_server_keyfile, 1) != 0)
531+
{
532+
/* The only likely failure cause is OOM, so use that errcode */
533+
ereport(FATAL,
534+
(errcode(ERRCODE_OUT_OF_MEMORY),
535+
errmsg("could not set environment: %m")));
536+
}
537+
}
530538

531539
while (true)
532540
{

src/backend/utils/misc/postgresql.conf.sample

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
#db_user_namespace = off
9393

9494
# GSSAPI using Kerberos
95-
#krb_server_keyfile = ''
95+
#krb_server_keyfile = 'FILE:${sysconfdir}/krb5.keytab'
9696
#krb_caseins_users = off
9797

9898
# - SSL -

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy