Skip to content

Commit 762fed9

Browse files
committed
postgres_fdw and dblink should check if backend has MyProcPort
before checking ->has_scram_keys. MyProcPort is NULL in background workers. So this could crash for example if a background worker accessed a suitable configured foreign table. Author: Alexander Pyhalov <a.pyhalov@postgrespro.ru> Reviewed-by: Peter Eisentraut <peter@eisentraut.org> Reviewed-by: Matheus Alcantara <matheusssilv97@gmail.com> Discussion: https://www.postgresql.org/message-id/flat/27b29a35-9b96-46a9-bc1a-914140869dac%40gmail.com
1 parent 41aac14 commit 762fed9

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

contrib/dblink/dblink.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2665,7 +2665,7 @@ dblink_connstr_has_required_scram_options(const char *connstr)
26652665
PQconninfoFree(options);
26662666
}
26672667

2668-
has_scram_keys = has_scram_client_key && has_scram_server_key && MyProcPort->has_scram_keys;
2668+
has_scram_keys = has_scram_client_key && has_scram_server_key && MyProcPort != NULL && MyProcPort->has_scram_keys;
26692669

26702670
return (has_scram_keys && has_require_auth);
26712671
}
@@ -2698,7 +2698,7 @@ dblink_security_check(PGconn *conn, const char *connname, const char *connstr)
26982698
* only added if UseScramPassthrough is set, and the user is not allowed
26992699
* to add the SCRAM keys on fdw and user mapping options.
27002700
*/
2701-
if (MyProcPort->has_scram_keys && dblink_connstr_has_required_scram_options(connstr))
2701+
if (MyProcPort != NULL && MyProcPort->has_scram_keys && dblink_connstr_has_required_scram_options(connstr))
27022702
return;
27032703

27042704
#ifdef ENABLE_GSS
@@ -2771,7 +2771,7 @@ dblink_connstr_check(const char *connstr)
27712771
if (dblink_connstr_has_pw(connstr))
27722772
return;
27732773

2774-
if (MyProcPort->has_scram_keys && dblink_connstr_has_required_scram_options(connstr))
2774+
if (MyProcPort != NULL && MyProcPort->has_scram_keys && dblink_connstr_has_required_scram_options(connstr))
27752775
return;
27762776

27772777
#ifdef ENABLE_GSS
@@ -2931,7 +2931,7 @@ get_connect_string(const char *servername)
29312931
* the user overwrites these options we can ereport on
29322932
* dblink_connstr_check and dblink_security_check.
29332933
*/
2934-
if (MyProcPort->has_scram_keys && UseScramPassthrough(foreign_server, user_mapping))
2934+
if (MyProcPort != NULL && MyProcPort->has_scram_keys && UseScramPassthrough(foreign_server, user_mapping))
29352935
appendSCRAMKeysInfo(&buf);
29362936

29372937
foreach(cell, fdw->options)

contrib/postgres_fdw/connection.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ pgfdw_security_check(const char **keywords, const char **values, UserMapping *us
462462
* assume that UseScramPassthrough is also true since SCRAM options are
463463
* only set when UseScramPassthrough is enabled.
464464
*/
465-
if (MyProcPort->has_scram_keys && pgfdw_has_required_scram_options(keywords, values))
465+
if (MyProcPort != NULL && MyProcPort->has_scram_keys && pgfdw_has_required_scram_options(keywords, values))
466466
return;
467467

468468
ereport(ERROR,
@@ -568,7 +568,7 @@ connect_pg_server(ForeignServer *server, UserMapping *user)
568568
n++;
569569

570570
/* Add required SCRAM pass-through connection options if it's enabled. */
571-
if (MyProcPort->has_scram_keys && UseScramPassthrough(server, user))
571+
if (MyProcPort != NULL && MyProcPort->has_scram_keys && UseScramPassthrough(server, user))
572572
{
573573
int len;
574574
int encoded_len;
@@ -743,7 +743,7 @@ check_conn_params(const char **keywords, const char **values, UserMapping *user)
743743
* assume that UseScramPassthrough is also true since SCRAM options are
744744
* only set when UseScramPassthrough is enabled.
745745
*/
746-
if (MyProcPort->has_scram_keys && pgfdw_has_required_scram_options(keywords, values))
746+
if (MyProcPort != NULL && MyProcPort->has_scram_keys && pgfdw_has_required_scram_options(keywords, values))
747747
return;
748748

749749
ereport(ERROR,
@@ -2557,7 +2557,7 @@ pgfdw_has_required_scram_options(const char **keywords, const char **values)
25572557
}
25582558
}
25592559

2560-
has_scram_keys = has_scram_client_key && has_scram_server_key && MyProcPort->has_scram_keys;
2560+
has_scram_keys = has_scram_client_key && has_scram_server_key && MyProcPort != NULL && MyProcPort->has_scram_keys;
25612561

25622562
return (has_scram_keys && has_require_auth);
25632563
}

0 commit comments

Comments
 (0)