Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for negotiating the protocol version with the client #1007

Merged
merged 3 commits into from May 6, 2024

Conversation

JelteF
Copy link
Member

@JelteF JelteF commented Jan 12, 2024

PgBouncer currently only supports protocol version 3.0. However, it was
still accepting connections if the client asked for the 3.1 protocol
version. This is obviously a bug, because PgBouncer likely won't
support the changes introduced in 3.1 of the protocol without
any code changes.

This starts sending the NegotiateProtocolVersion message when the client
asks for a minor version that PgBouncer does not support. Also, instead
of erroring when it receives unsupported protocol extensions parameters
it will include these in this NegotiateProtocolVersion message.

For reference this is the Postgres commit introducing NegotiateProtocolVersion: postgres/postgres@ae65f60

I tested this manually using this patchset that bumps the protocol version. https://www.postgresql.org/message-id/flat/CAGECzQScQ3N-Ykv2j4NDyDtrPPc3FpRoa%3DLZ-2Uj2ocA4zr%3D4Q%40mail.gmail.com#cd9e8407820d492e8f677ee6a67c21ce

@JelteF JelteF force-pushed the better-protocol-version-handling branch 2 times, most recently from b3e885a to 5308f66 Compare January 12, 2024 10:54
@@ -102,8 +102,10 @@ bool get_header(struct MBuf *data, PktHdr *pkt)
type = PKT_SSLREQ;
} else if (code == PKT_GSSENCREQ) {
type = PKT_GSSENCREQ;
} else if ((code >> 16) == 3 && (code & 0xFFFF) < 2) {
Copy link
Member Author

@JelteF JelteF Jan 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to be clear, this was accepting protocol 3.1. It should have been this to only support 3.0:

-		} else if ((code >> 16) == 3 && (code & 0xFFFF) < 2) {
+		} else if ((code >> 16) == 3 && (code & 0xFFFF) < 1) {

@levkk
Copy link

levkk commented Mar 9, 2024

How do you test this? I'm trying to send the protocol negotiation packet while connecting with psql and it always returns this error message:

psql: error: connection to server at "127.0.0.1", port 6432 failed: expected authentication request from server, but received v

which is really weird because when I'm reading the implementation in libpq, it clearly should support v at this particular stage in the negotiation: https://github.com/postgres/postgres/blob/b0289574bdf1202248201a3143d1459bdf5727fd/src/interfaces/libpq/fe-connect.c#L3541-L3548

@JelteF
Copy link
Member Author

JelteF commented Mar 9, 2024

Sounds like you're using a libpq from before bbf9c282ce92272ed7bf6771daf0f9efa209e61b

But even after that libpq will complain. Just with a more helpful message. Better handling is in my patchset here: https://www.postgresql.org/message-id/flat/CAGECzQScQ3N-Ykv2j4NDyDtrPPc3FpRoa%3DLZ-2Uj2ocA4zr%3D4Q%40mail.gmail.com#cd9e8407820d492e8f677ee6a67c21ce

@JelteF JelteF force-pushed the better-protocol-version-handling branch from f0004d7 to bfd7660 Compare May 6, 2024 09:42
JelteF added 3 commits May 6, 2024 11:45
This is done so we can introduce PKT_STARTUP_V4 in a follow up commit.
Passing NULL to memcpy is undefined behaviour, even if len is 0. This
avoids doing that that, so it starts to allow passing NULL as the data
pointer if len is 0.
PgBouncer currently only supports protocol version 3.0. However, it was
still accepting connections if the client asked for the 3.1 protocol
version. This is obviously a bug.

This starts sending the NegotiateProtocolVersion message when the client
asks for a minor version that PgBouncer does not support. Also, instead
of erroring when it receives unsupported protocol extensions parameters
it will include these in this NegotiateProtocolVersion message.
@JelteF JelteF force-pushed the better-protocol-version-handling branch from bfd7660 to 6a39179 Compare May 6, 2024 09:46
@JelteF JelteF enabled auto-merge May 6, 2024 09:46
@JelteF JelteF merged commit 3f6b3dc into pgbouncer:master May 6, 2024
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants