v4-0004-libpq-Add-min-max_protocol_version-connection-opt.patch
text/x-patch
Filename: v4-0004-libpq-Add-min-max_protocol_version-connection-opt.patch
Type: text/x-patch
Part: 3
Patch
Format: format-patch
Series: patch v4-0004
Subject: libpq: Add min/max_protocol_version connection options
| File | + | − |
|---|---|---|
| doc/src/sgml/libpq.sgml | 70 | 1 |
| src/interfaces/libpq/fe-connect.c | 103 | 1 |
| src/interfaces/libpq/fe-protocol3.c | 12 | 0 |
| src/interfaces/libpq/libpq-int.h | 4 | 0 |
From 007e89278b3a5ab4c407c86363210205e0eb2b40 Mon Sep 17 00:00:00 2001
From: Jelte Fennema-Nio <postgres@jeltef.nl>
Date: Wed, 14 Aug 2024 18:25:16 +0200
Subject: [PATCH v4 4/7] libpq: Add min/max_protocol_version connection options
All officially supported version of the PostgreSQL server send the
NegotiateProtocolVersion message when an unsupported minor protocol
version is requested by a client. But many other applications that
implement the PostgreSQL protocol (connection poolers, or other
databases) do not, and the same is true for many unspported PostgreSQL
server versions. Connecting to such other applications thus fails if a
client requests a protocol version different than 3.0.
This patch adds a max_protocol_version connection option to libpq that
specifies the protocol version that libpq should request from the
server. Currently all allowed values result in the use of 3.0, but that
will be changed in a future commit that bumps the protocol version. Even
after that version bump the default will likely stay 3.0 for the time
being. Once more of the ecosystem supports the NegotiateProtocolVersion
message we might want to change the default to the latest minor version.
We also add the similar min_protocol_version connection option, to allow
a client to specify that connecting should fail if a lower protocol
version is attempted by the server. This can be used to ensure certain
protocol features are in used, which can be particularly useful if those
features impact security.
---
doc/src/sgml/libpq.sgml | 71 ++++++++++++++++++-
src/interfaces/libpq/fe-connect.c | 104 +++++++++++++++++++++++++++-
src/interfaces/libpq/fe-protocol3.c | 12 ++++
src/interfaces/libpq/libpq-int.h | 4 ++
4 files changed, 189 insertions(+), 2 deletions(-)
diff --git a/doc/src/sgml/libpq.sgml b/doc/src/sgml/libpq.sgml
index ddb3596df83..f249bbd9263 100644
--- a/doc/src/sgml/libpq.sgml
+++ b/doc/src/sgml/libpq.sgml
@@ -2144,6 +2144,56 @@ postgresql://%2Fvar%2Flib%2Fpostgresql/dbname
</listitem>
</varlistentry>
+ <varlistentry id="libpq-connect-min-protocol-version" xreflabel="min_protocol_version">
+ <term><literal>min_protocol_version</literal></term>
+ <listitem>
+ <para>
+ Specifies the minimum protocol version to allow for the connection.
+ The default is to allow any version of the
+ <productname>PostgreSQL</productname> protocol supported by libpq,
+ which currently means <literal>3.0</literal>. If the server
+ does not support at least this protocol version the connection will be
+ closed.
+ </para>
+
+ <para>
+ The current supported values are
+ <literal>3.0</literal>
+ and <literal>latest</literal>. The <literal>latest</literal> value is
+ equivalent to the latest protocol version that is supported by the used
+ libpq version, which currently is <literal>3.2</literal>, but this will
+ change in future libpq releases.
+ </para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry id="libpq-connect-max-protocol-version" xreflabel="max_protocol_version">
+ <term><literal>max_protocol_version</literal></term>
+ <listitem>
+ <para>
+ Specifies the protocol version to request from the server.
+ The default is to use version <literal>3.0</literal> of the
+ <productname>PostgreSQL</productname> protocol, unless the connection
+ string specifies a feature that relies on a higher protocol version, in
+ that case the latest version supported by libpq is used. If the server
+ does not support the requested protocol version of the client the
+ connection will be automatically downgraded to a lower minor protocol
+ version, which the server does support. After the connection attempt has
+ completed you can use <xref linkend="libpq-PQprotocolVersion"/> to find
+ out which exact protocol version was negotiated.
+ </para>
+
+ <para>
+ The current supported values are
+ <literal>3.0</literal>
+ and <literal>latest</literal>. The <literal>latest</literal> value is
+ equivalent to the latest protocol version that is supported by the used
+ libpq version, which currently is <literal>3.0</literal>, but this will
+ change in future libpq releases.
+ </para>
+ </listitem>
+ </varlistentry>
+
<varlistentry id="libpq-connect-ssl-max-protocol-version" xreflabel="ssl_max_protocol_version">
<term><literal>ssl_max_protocol_version</literal></term>
<listitem>
@@ -2482,7 +2532,6 @@ postgresql://%2Fvar%2Flib%2Fpostgresql/dbname
</para>
</listitem>
</varlistentry>
-
</variablelist>
</para>
</sect2>
@@ -9329,6 +9378,26 @@ myEventProc(PGEventId evtId, void *evtInfo, void *passThrough)
linkend="libpq-connect-load-balance-hosts"/> connection parameter.
</para>
</listitem>
+
+ <listitem>
+ <para>
+ <indexterm>
+ <primary><envar>PGMINPROTOCOLVERSION</envar></primary>
+ </indexterm>
+ <envar>PGMINPROTOCOLVERSION</envar> behaves the same as the <xref
+ linkend="libpq-connect-min-protocol-version"/> connection parameter.
+ </para>
+ </listitem>
+
+ <listitem>
+ <para>
+ <indexterm>
+ <primary><envar>PGMAXPROTOCOLVERSION</envar></primary>
+ </indexterm>
+ <envar>PGMAXPROTOCOLVERSION</envar> behaves the same as the <xref
+ linkend="libpq-connect-max-protocol-version"/> connection parameter.
+ </para>
+ </listitem>
</itemizedlist>
</para>
diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c
index 281710b0616..7f08b143fa7 100644
--- a/src/interfaces/libpq/fe-connect.c
+++ b/src/interfaces/libpq/fe-connect.c
@@ -325,6 +325,16 @@ static const internalPQconninfoOption PQconninfoOptions[] = {
"Require-Auth", "", 14, /* sizeof("scram-sha-256") == 14 */
offsetof(struct pg_conn, require_auth)},
+ {"min_protocol_version", "PGMINPROTOCOLVERSION",
+ NULL, NULL,
+ "Min-Protocol-Version", "", 6, /* sizeof("latest") = 6 */
+ offsetof(struct pg_conn, min_protocol_version)},
+
+ {"max_protocol_version", "PGMAXPROTOCOLVERSION",
+ NULL, NULL,
+ "Max-Protocol-Version", "", 6, /* sizeof("latest") = 6 */
+ offsetof(struct pg_conn, max_protocol_version)},
+
{"ssl_min_protocol_version", "PGSSLMINPROTOCOLVERSION", "TLSv1.2", NULL,
"SSL-Minimum-Protocol-Version", "", 8, /* sizeof("TLSv1.x") == 8 */
offsetof(struct pg_conn, ssl_min_protocol_version)},
@@ -483,6 +493,7 @@ static void pgpassfileWarning(PGconn *conn);
static void default_threadlock(int acquire);
static bool sslVerifyProtocolVersion(const char *version);
static bool sslVerifyProtocolRange(const char *min, const char *max);
+static bool pqParseProtocolVersion(const char *value, ProtocolVersion *result, PGconn *conn, const char *context);
/* global variable because fe-auth.c needs to access it */
@@ -2080,6 +2091,42 @@ pqConnectOptions2(PGconn *conn)
}
}
+ if (conn->min_protocol_version)
+ {
+ if (!pqParseProtocolVersion(conn->min_protocol_version, &conn->min_pversion, conn, "min_protocol_version"))
+ return false;
+ }
+ else
+ {
+ conn->min_pversion = PG_PROTOCOL_EARLIEST;
+ }
+
+ if (conn->max_protocol_version)
+ {
+ if (!pqParseProtocolVersion(conn->max_protocol_version, &conn->max_pversion, conn, "max_protocol_version"))
+ return false;
+ }
+ else
+ {
+ /*
+ * To not break connecting to older servers/poolers that do not yet
+ * support NegotiateProtocolVersion, default to the 3.0 protocol at
+ * least for a while longer. Except when min_protocol_version is set
+ * to something larger, then we might as well default to the latest.
+ */
+ if (conn->min_pversion > PG_PROTOCOL(3, 0))
+ conn->max_pversion = PG_PROTOCOL_LATEST;
+ else
+ conn->max_pversion = PG_PROTOCOL(3, 0);
+ }
+
+ if (conn->min_pversion > conn->max_pversion)
+ {
+ conn->status = CONNECTION_BAD;
+ libpq_append_conn_error(conn, "min_protocol_version is greater than max_protocol_version");
+ return false;
+ }
+
/*
* Resolve special "auto" client_encoding from the locale
*/
@@ -3083,7 +3130,7 @@ keep_going: /* We will come back to here until there is
* must persist across individual connection attempts, but we must
* reset them when we start to consider a new server.
*/
- conn->pversion = PG_PROTOCOL(3, 0);
+ conn->pversion = conn->max_pversion;
conn->send_appname = true;
conn->failed_enc_methods = 0;
conn->current_enc_method = 0;
@@ -4094,6 +4141,7 @@ keep_going: /* We will come back to here until there is
/* OK, we read the message; mark data consumed */
pqParseDone(conn, conn->inCursor);
+
goto keep_going;
}
@@ -8149,6 +8197,60 @@ error:
return false;
}
+/*
+ * Parse and try to interpret "value" as a ProtocolVersion value, and if successful,
+ * store it in *result.
+ */
+static bool
+pqParseProtocolVersion(const char *value, ProtocolVersion *result, PGconn *conn,
+ const char *context)
+{
+ char *end;
+ int major;
+ int minor;
+ ProtocolVersion version;
+
+ if (strcmp(value, "latest") == 0)
+ {
+ *result = PG_PROTOCOL_LATEST;
+ return true;
+ }
+
+ major = strtol(value, &end, 10);
+ if (*end != '.')
+ {
+ conn->status = CONNECTION_BAD;
+ libpq_append_conn_error(conn, "invalid %s value: \"%s\"",
+ context,
+ value);
+ return false;
+ }
+
+ minor = strtol(&end[1], &end, 10);
+ if (*end != '\0')
+ {
+ conn->status = CONNECTION_BAD;
+ libpq_append_conn_error(conn, "invalid %s value: \"%s\"",
+ context,
+ value);
+ return false;
+ }
+
+ version = PG_PROTOCOL(major, minor);
+ if (version > PG_PROTOCOL_LATEST ||
+ version < PG_PROTOCOL_EARLIEST)
+ {
+ conn->status = CONNECTION_BAD;
+ libpq_append_conn_error(conn, "invalid %s value: \"%s\"",
+ context,
+ value);
+ return false;
+ }
+
+ *result = version;
+ return true;
+}
+
/*
* To keep the API consistent, the locking stubs are always provided, even
* if they are not required.
diff --git a/src/interfaces/libpq/fe-protocol3.c b/src/interfaces/libpq/fe-protocol3.c
index 68a94281eb4..221627bdea6 100644
--- a/src/interfaces/libpq/fe-protocol3.c
+++ b/src/interfaces/libpq/fe-protocol3.c
@@ -1441,6 +1441,18 @@ pqGetNegotiateProtocolVersion3(PGconn *conn)
goto failure;
}
+ if (their_version < conn->min_pversion)
+ {
+ libpq_append_conn_error(conn, "server only supports protocol version %d.%d, but min_protocol_version was set to %d.%d",
+ PG_PROTOCOL_MAJOR(conn->pversion),
+ PG_PROTOCOL_MINOR(conn->pversion),
+ PG_PROTOCOL_MAJOR(conn->min_pversion),
+ PG_PROTOCOL_MINOR(conn->min_pversion));
+
+ goto failure;
+ }
+
+
conn->pversion = their_version;
for (int i = 0; i < num; i++)
{
diff --git a/src/interfaces/libpq/libpq-int.h b/src/interfaces/libpq/libpq-int.h
index f36f7f19d58..c016408338a 100644
--- a/src/interfaces/libpq/libpq-int.h
+++ b/src/interfaces/libpq/libpq-int.h
@@ -425,6 +425,8 @@ struct pg_conn
char *gsslib; /* What GSS library to use ("gssapi" or
* "sspi") */
char *gssdelegation; /* Try to delegate GSS credentials? (0 or 1) */
+ char *min_protocol_version; /* minimum used protocol version */
+ char *max_protocol_version; /* maximum used protocol version */
char *ssl_min_protocol_version; /* minimum TLS protocol version */
char *ssl_max_protocol_version; /* maximum TLS protocol version */
char *target_session_attrs; /* desired session properties */
@@ -545,6 +547,8 @@ struct pg_conn
void *scram_client_key_binary; /* binary SCRAM client key */
size_t scram_server_key_len;
void *scram_server_key_binary; /* binary SCRAM server key */
+ ProtocolVersion min_pversion; /* protocol version to request */
+ ProtocolVersion max_pversion; /* protocol version to request */
/* Miscellaneous stuff */
int be_pid; /* PID of backend --- needed for cancels */
--
2.43.0