|
| 1 | +// Flags: --experimental-quic --no-warnings |
| 2 | + |
| 3 | +// Regression test for an unauthenticated remote crash in the QUIC Version |
| 4 | +// Negotiation path. |
| 5 | +// |
| 6 | +// ngtcp2_pkt_decode_version_cid() does not clamp the connection ID lengths |
| 7 | +// to NGTCP2_MAX_CIDLEN (20) when the packet's version is unsupported -- it |
| 8 | +// returns the raw single-byte length fields from the wire, which can be up |
| 9 | +// to 255. Endpoint::Receive() used to build CID objects (each backed by a |
| 10 | +// fixed 20-byte buffer) directly from those lengths before any bound check, |
| 11 | +// so a single crafted UDP datagram with an oversized DCID/SCID length would |
| 12 | +// overflow the buffer and abort the process before the handshake. |
| 13 | +// |
| 14 | +// This test sends such a datagram directly with node:dgram and asserts the |
| 15 | +// endpoint drops it without crashing, while a well-formed unsupported-version |
| 16 | +// datagram still produces exactly one Version Negotiation response. |
| 17 | + |
| 18 | +import { hasQuic, skip, mustCall } from '../common/index.mjs'; |
| 19 | +import assert from 'node:assert'; |
| 20 | + |
| 21 | +const { strictEqual } = assert; |
| 22 | + |
| 23 | +if (!hasQuic) { |
| 24 | + skip('QUIC is not enabled'); |
| 25 | +} |
| 26 | + |
| 27 | +const { createSocket } = await import('node:dgram'); |
| 28 | +const { listen } = await import('../common/quic.mjs'); |
| 29 | + |
| 30 | +// A long-header QUIC packet must be at least NGTCP2_MAX_UDP_PAYLOAD_SIZE |
| 31 | +// (1200) bytes for an unsupported version to decode as a VN trigger. |
| 32 | +const PACKET_SIZE = 1200; |
| 33 | + |
| 34 | +// Build a QUIC long-header packet with an unsupported version and the given |
| 35 | +// DCID/SCID lengths. Lengths greater than 20 are only expressible because the |
| 36 | +// on-wire length field is a single byte (0-255). |
| 37 | +function buildLongHeaderPacket(dcidLen, scidLen) { |
| 38 | + const packet = Buffer.alloc(PACKET_SIZE); |
| 39 | + // Header form bit (0x80) + fixed bit (0x40). |
| 40 | + packet[0] = 0xc0; |
| 41 | + // Version 0x0a0a0a0a: nonzero (so it is not a real VN packet) and not a |
| 42 | + // version Node supports, which forces the NGTCP2_ERR_VERSION_NEGOTIATION |
| 43 | + // decode path. |
| 44 | + packet.writeUInt32BE(0x0a0a0a0a, 1); |
| 45 | + packet[5] = dcidLen; // DCID length byte |
| 46 | + // DCID bytes occupy [6, 6 + dcidLen); SCID length byte follows them. |
| 47 | + packet[6 + dcidLen] = scidLen; |
| 48 | + // Remaining bytes stay zero-filled as padding to reach PACKET_SIZE. |
| 49 | + return packet; |
| 50 | +} |
| 51 | + |
| 52 | +const serverEndpoint = await listen(mustCall(() => {}, 0)); |
| 53 | +const { address, port } = serverEndpoint.address; |
| 54 | + |
| 55 | +const socket = createSocket('udp4'); |
| 56 | + |
| 57 | +function send(packet) { |
| 58 | + return new Promise((resolve, reject) => { |
| 59 | + socket.send(packet, port, address, (err) => err ? reject(err) : resolve()); |
| 60 | + }); |
| 61 | +} |
| 62 | + |
| 63 | +// 1. Oversized DCID (21 > NGTCP2_MAX_CIDLEN). Before the fix this aborted the |
| 64 | +// process. After the fix it must be dropped silently. |
| 65 | +await send(buildLongHeaderPacket(21, 0)); |
| 66 | + |
| 67 | +// 2. A well-formed unsupported-version packet with valid (<= 20 byte) CIDs. |
| 68 | +// This must still elicit exactly one Version Negotiation response, proving |
| 69 | +// the fix did not break legitimate version negotiation. |
| 70 | +await send(buildLongHeaderPacket(8, 8)); |
| 71 | + |
| 72 | +// Poll until the valid packet has been processed into a VN response. |
| 73 | +const deadline = Date.now() + 2000; |
| 74 | +while (serverEndpoint.stats.versionNegotiationCount === 0n) { |
| 75 | + if (Date.now() > deadline) break; |
| 76 | + await new Promise((resolve) => setTimeout(resolve, 25)); |
| 77 | +} |
| 78 | + |
| 79 | +// Exactly one VN response: the oversized packet was dropped (not crashed, not |
| 80 | +// negotiated), the valid packet was negotiated. |
| 81 | +strictEqual(serverEndpoint.stats.versionNegotiationCount, 1n); |
| 82 | + |
| 83 | +socket.close(); |
| 84 | +await serverEndpoint.close(); |
0 commit comments