Skip to content

Commit 138750d

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 ebaaf38 commit 138750d

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
@@ -2643,7 +2643,7 @@ dblink_connstr_has_required_scram_options(const char *connstr)
26432643
PQconninfoFree(options);
26442644
}
26452645

2646-
has_scram_keys = has_scram_client_key && has_scram_server_key && MyProcPort->has_scram_keys;
2646+
has_scram_keys = has_scram_client_key && has_scram_server_key && MyProcPort != NULL && MyProcPort->has_scram_keys;
26472647

26482648
return (has_scram_keys && has_require_auth);
26492649
}
@@ -2676,7 +2676,7 @@ dblink_security_check(PGconn *conn, const char *connname, const char *connstr)
26762676
* only added if UseScramPassthrough is set, and the user is not allowed
26772677
* to add the SCRAM keys on fdw and user mapping options.
26782678
*/
2679-
if (MyProcPort->has_scram_keys && dblink_connstr_has_required_scram_options(connstr))
2679+
if (MyProcPort != NULL && MyProcPort->has_scram_keys && dblink_connstr_has_required_scram_options(connstr))
26802680
return;
26812681

26822682
#ifdef ENABLE_GSS
@@ -2749,7 +2749,7 @@ dblink_connstr_check(const char *connstr)
27492749
if (dblink_connstr_has_pw(connstr))
27502750
return;
27512751

2752-
if (MyProcPort->has_scram_keys && dblink_connstr_has_required_scram_options(connstr))
2752+
if (MyProcPort != NULL && MyProcPort->has_scram_keys && dblink_connstr_has_required_scram_options(connstr))
27532753
return;
27542754

27552755
#ifdef ENABLE_GSS
@@ -2896,7 +2896,7 @@ get_connect_string(const char *servername)
28962896
* the user overwrites these options we can ereport on
28972897
* dblink_connstr_check and dblink_security_check.
28982898
*/
2899-
if (MyProcPort->has_scram_keys && UseScramPassthrough(foreign_server, user_mapping))
2899+
if (MyProcPort != NULL && MyProcPort->has_scram_keys && UseScramPassthrough(foreign_server, user_mapping))
29002900
appendSCRAMKeysInfo(&buf);
29012901

29022902
foreach(cell, fdw->options)

contrib/postgres_fdw/connection.c

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

470470
ereport(ERROR,
@@ -570,7 +570,7 @@ connect_pg_server(ForeignServer *server, UserMapping *user)
570570
n++;
571571

572572
/* Add required SCRAM pass-through connection options if it's enabled. */
573-
if (MyProcPort->has_scram_keys && UseScramPassthrough(server, user))
573+
if (MyProcPort != NULL && MyProcPort->has_scram_keys && UseScramPassthrough(server, user))
574574
{
575575
int len;
576576
int encoded_len;
@@ -748,7 +748,7 @@ check_conn_params(const char **keywords, const char **values, UserMapping *user)
748748
* assume that UseScramPassthrough is also true since SCRAM options are
749749
* only set when UseScramPassthrough is enabled.
750750
*/
751-
if (MyProcPort->has_scram_keys && pgfdw_has_required_scram_options(keywords, values))
751+
if (MyProcPort != NULL && MyProcPort->has_scram_keys && pgfdw_has_required_scram_options(keywords, values))
752752
return;
753753

754754
ereport(ERROR,
@@ -2559,7 +2559,7 @@ pgfdw_has_required_scram_options(const char **keywords, const char **values)
25592559
}
25602560
}
25612561

2562-
has_scram_keys = has_scram_client_key && has_scram_server_key && MyProcPort->has_scram_keys;
2562+
has_scram_keys = has_scram_client_key && has_scram_server_key && MyProcPort != NULL && MyProcPort->has_scram_keys;
25632563

25642564
return (has_scram_keys && has_require_auth);
25652565
}

0 commit comments

Comments
 (0)