mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(dsql): refuse a startup packet or client message outside DSQL's limits (#6258)
* fix(dsql): refuse a startup packet or client message outside DSQL's limits The endpoint took a client message up to 64 MB and dropped the connection without a word past that; DSQL takes 10 MiB and answers FATAL 08P01 "invalid message length" as soon as a longer, or shorter than 4, length arrives. Measuring the startup packet alongside showed it one byte off as well: DSQL takes 9,999 bytes and answers "invalid length of startup packet" from 10,000, where the endpoint took 10,000 and refused more in silence. Both are DSQL's now, for the startup inside TLS, the password message and every message after signing in. What the database sends is not DSQL's to limit, so it is still read up to 64 MB. PostgresWire.checkedLength throws its own InvalidLengthException, still an IOException, so a reader can tell such a packet from a broken connection; the RDS proxy and Redshift's bridge end the connection on it as before. * fix(dsql): refuse any packet before TLS as not TLS, whatever its length The first head capped the packets read before TLS at the startup limit too, so a 10,000-byte plaintext startup packet, first or after a GSSENCRequest, closed the connection in silence where it had been refused as not TLS; above 10,000 bytes main closed in silence as well. Measured on DSQL, it reads no more than such a packet's length and code: a plaintext startup packet of any length is refused with "SSL is mandatory", and one under 8 bytes ends the connection without a word. The endpoint now reads the body only when it fits a startup packet, so the refusal reaches the client in order, and refuses a longer one unread.
This commit is contained in:
1 parent
8d73535826
commit
564ae350a1
5 files changed
+205
-19
No files matched your search
@@ -36,6 +36,16 @@ public final class PostgresWire {
|
||||
public record CString(String value, int next) {
|
||||
}
|
||||
|
||||
/**
|
||||
* A packet whose length is outside what its reader takes. Nothing after the length was read, so
|
||||
* a server can still answer the way PostgreSQL does before it closes the connection.
|
||||
*/
|
||||
public static final class InvalidLengthException extends IOException {
|
||||
InvalidLengthException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
// ── Framing ───────────────────────────────────────────────────────────────
|
||||
|
||||
/** Reads a startup-phase packet: its length, refused outside 8 to {@code maxLength}, code and body. */
|
||||
@@ -89,14 +99,14 @@ public final class PostgresWire {
|
||||
return out.toByteArray();
|
||||
}
|
||||
|
||||
/** {@code length}, refused when below {@code minimum} or above {@code maximum}. */
|
||||
/** {@code length}, refused with an {@link InvalidLengthException} when below {@code minimum} or above {@code maximum}. */
|
||||
public static int checkedLength(int length, int minimum, int maximum, String packetName) throws IOException {
|
||||
if (length < minimum) {
|
||||
throw new IOException("PostgreSQL " + packetName + " length is below the "
|
||||
throw new InvalidLengthException("PostgreSQL " + packetName + " length is below the "
|
||||
+ minimum + " byte minimum: " + length);
|
||||
}
|
||||
if (length > maximum) {
|
||||
throw new IOException("PostgreSQL " + packetName + " length exceeds the "
|
||||
throw new InvalidLengthException("PostgreSQL " + packetName + " length exceeds the "
|
||||
+ maximum + " byte limit: " + length);
|
||||
}
|
||||
return length;
|
||||
|
||||
@@ -43,9 +43,14 @@ import java.util.stream.Stream;
|
||||
final class DsqlSession {
|
||||
|
||||
static final Duration CONNECTION_LIFETIME = Duration.ofHours(1);
|
||||
// A startup packet is refused above PostgreSQL's own limit, 10,000 bytes, and a message above 64 MB.
|
||||
private static final int MAX_STARTUP = 10_000;
|
||||
private static final int MAX_MESSAGE = 64 * 1024 * 1024;
|
||||
// DSQL's (measured): a startup packet of 10,000 bytes or more, and a client message over 10 MiB,
|
||||
// are refused with a FATAL before the connection closes. What the database sends is not DSQL's
|
||||
// limit to apply, and is read up to 64 MB.
|
||||
private static final int MAX_STARTUP = 9_999;
|
||||
private static final int MAX_CLIENT_MESSAGE = 10 * 1024 * 1024;
|
||||
private static final int MAX_DATABASE_MESSAGE = 64 * 1024 * 1024;
|
||||
private static final String INVALID_STARTUP_LENGTH = "invalid length of startup packet";
|
||||
private static final String INVALID_MESSAGE_LENGTH = "invalid message length";
|
||||
private static final String SERIALIZATION_FAILURE = "40001";
|
||||
private static final String QUERY_CANCELED = "57014";
|
||||
private static final String ACCESS_DENIED = "unable to accept connection, access denied";
|
||||
@@ -103,7 +108,7 @@ final class DsqlSession {
|
||||
socket.setSoTimeout(endpoint.handshakeTimeoutMillis());
|
||||
InputStream rawIn = socket.getInputStream();
|
||||
OutputStream rawOut = socket.getOutputStream();
|
||||
PostgresWire.Startup first = PostgresWire.readStartup(rawIn, MAX_STARTUP, "startup message");
|
||||
PostgresWire.Startup first = readPlaintextPacket(rawIn);
|
||||
if (first.code() == PostgresWire.CANCEL_REQUEST) {
|
||||
endpoint.forwardCancel(first.body());
|
||||
return;
|
||||
@@ -111,7 +116,7 @@ final class DsqlSession {
|
||||
if (first.code() == PostgresWire.GSSENC_REQUEST) {
|
||||
rawOut.write('N');
|
||||
rawOut.flush();
|
||||
first = PostgresWire.readStartup(rawIn, MAX_STARTUP, "startup message");
|
||||
first = readPlaintextPacket(rawIn);
|
||||
}
|
||||
if (first.code() != PostgresWire.SSL_REQUEST) {
|
||||
refuse(rawOut, "08P01", "unable to accept connection, SSL is mandatory. AWS Authentication is required.",
|
||||
@@ -139,7 +144,13 @@ final class DsqlSession {
|
||||
|
||||
InputStream in = new BufferedInputStream(tls.getInputStream());
|
||||
OutputStream out = new BufferedOutputStream(tls.getOutputStream());
|
||||
PostgresWire.Startup startup = PostgresWire.readStartup(in, MAX_STARTUP, "startup message");
|
||||
PostgresWire.Startup startup;
|
||||
try {
|
||||
startup = PostgresWire.readStartup(in, MAX_STARTUP, "startup message");
|
||||
} catch (PostgresWire.InvalidLengthException e) {
|
||||
refuse(out, "08P01", INVALID_STARTUP_LENGTH, null);
|
||||
return;
|
||||
}
|
||||
if (startup.code() == PostgresWire.CANCEL_REQUEST) {
|
||||
endpoint.forwardCancel(startup.body());
|
||||
return;
|
||||
@@ -154,7 +165,13 @@ final class DsqlSession {
|
||||
|
||||
PostgresWire.write(out, (byte) 'R', PostgresWire.int32(3));
|
||||
out.flush();
|
||||
PostgresWire.Message password = PostgresWire.read(in, MAX_MESSAGE, "message");
|
||||
PostgresWire.Message password;
|
||||
try {
|
||||
password = PostgresWire.read(in, MAX_CLIENT_MESSAGE, "message");
|
||||
} catch (PostgresWire.InvalidLengthException e) {
|
||||
refuse(out, "08P01", INVALID_MESSAGE_LENGTH, null);
|
||||
return;
|
||||
}
|
||||
if (password.type() != 'p') {
|
||||
refuse(out, "08006", ACCESS_DENIED, null);
|
||||
return;
|
||||
@@ -228,7 +245,13 @@ final class DsqlSession {
|
||||
|
||||
private void relayToBackend(InputStream in, OutputStream backendOut) throws IOException {
|
||||
while (true) {
|
||||
PostgresWire.Message message = PostgresWire.read(in, MAX_MESSAGE, "message");
|
||||
PostgresWire.Message message;
|
||||
try {
|
||||
message = PostgresWire.read(in, MAX_CLIENT_MESSAGE, "message");
|
||||
} catch (PostgresWire.InvalidLengthException e) {
|
||||
close("FATAL", "08P01", INVALID_MESSAGE_LENGTH, "Session Id: " + sessionId);
|
||||
return;
|
||||
}
|
||||
if (message.type() != 'X') {
|
||||
lastClientMessage = now();
|
||||
if (transactionStatus == 'T' && pastAgeLimit()) {
|
||||
@@ -579,7 +602,7 @@ final class DsqlSession {
|
||||
List<PostgresWire.Message> startup = new ArrayList<>();
|
||||
boolean started = false;
|
||||
while (true) {
|
||||
PostgresWire.Message message = PostgresWire.read(backendIn, MAX_MESSAGE, "message");
|
||||
PostgresWire.Message message = PostgresWire.read(backendIn, MAX_DATABASE_MESSAGE, "message");
|
||||
if (!started) {
|
||||
// DSQL refuses a connection instead of authenticating it, so what the database
|
||||
// answers the startup waits for its ReadyForQuery, and a refusal takes its place.
|
||||
@@ -749,6 +772,23 @@ final class DsqlSession {
|
||||
return PostgresWire.fields(fields);
|
||||
}
|
||||
|
||||
/**
|
||||
* A packet sent before TLS. DSQL refuses one that is no SSLRequest, GSSENCRequest or
|
||||
* CancelRequest as not TLS whatever length it claims, and ends the connection without a word on
|
||||
* one under 8 bytes (measured). So a length past what a startup packet may hold is refused with
|
||||
* its body left unread; a shorter body is read, so the refusal reaches the client in order.
|
||||
*/
|
||||
private static PostgresWire.Startup readPlaintextPacket(InputStream in) throws IOException {
|
||||
int length = PostgresWire.checkedLength(PostgresWire.readInt32(in), 8, Integer.MAX_VALUE, "startup message");
|
||||
int code = PostgresWire.readInt32(in);
|
||||
if (length > MAX_STARTUP) {
|
||||
return new PostgresWire.Startup(code, new byte[0]);
|
||||
}
|
||||
byte[] body = new byte[length - 8];
|
||||
PostgresWire.readFully(in, body);
|
||||
return new PostgresWire.Startup(code, body);
|
||||
}
|
||||
|
||||
/** An ErrorResponse body of {@code severity} (FATAL, ERROR), as DSQL orders its fields. */
|
||||
static byte[] error(String severity, String state, String message, String detail, String hint) {
|
||||
Map<Character, String> fields = new LinkedHashMap<>();
|
||||
|
||||
Reference in new issue
Block a user