Commit 625cccfa authored by jan.koester's avatar jan.koester
Browse files

test

parent a5b6d53a
Loading
Loading
Loading
Loading
+38 −10
Changes for src/quic.cpp: 38 added lines, 10 removed lines.
Original line number Diff line number Diff line
@@ -4115,10 +4115,14 @@ void quic::processAckFrame(const uint8_t* data, size_t len, size_t& offset) {
    // attacker-controlled value here (up to 2^62-1) would otherwise let a
    // single frame drive an unbounded number of loop iterations/allocations
    // even though each individual decodeVarInt call is itself bounds-safe.
    // MAX_ACK_RANGES_PER_FRAME is shared with buildAckFrame()'s own
    // truncation so a peer running this same library never legitimately
    // exceeds it (see the constant's comment in socket.h for why
    // _app_pn_recv_ranges can otherwise grow unboundedly over a long
    // connection's lifetime).
    uint64_t ack_range_count;
    if (!readVarInt(ack_range_count)) return;
    static constexpr uint64_t kMaxAckRangesPerFrame = 256;
    if (ack_range_count > kMaxAckRangesPerFrame) {
    if (ack_range_count > MAX_ACK_RANGES_PER_FRAME) {
        abortFrameParsing(len, offset, 0x01, "too many ACK ranges");
        return;
    }
@@ -5335,8 +5339,19 @@ size_t quic::sendStreamData(uint64_t stream_id, const uint8_t* data, size_t len,
    const size_t max_datagram = _handshake_complete
        ? static_cast<size_t>(_max_udp_payload)
        : 1200;
    // Reserve space for piggybacked ACK frame in first packet (~20 bytes worst case)
    const size_t ack_reserve = _app_ack_pending ? 40 : 0;
    // Reserve space for a piggybacked ACK frame in the first packet. Must
    // reflect the true worst case, not a fixed guess: the piggyback ACK
    // encoder below writes directly into pkt_buf with no further bounds
    // check, so under-reserving here would let it overwrite the space
    // this chunk size assumes is free for stream data. Sized from the
    // actual (capped, per MAX_ACK_RANGES_PER_FRAME) range count that will
    // be encoded: type(1) + largest_ack(8) + delay(8) + range_count(8) +
    // first_range(8) + additional_ranges * (gap(8) + range(8)).
    const size_t ack_reserve = _app_ack_pending
        ? 33 + std::min<uint64_t>(
              _app_pn_recv_ranges.empty() ? 0 : _app_pn_recv_ranges.size() - 1,
              MAX_ACK_RANGES_PER_FRAME) * 16
        : 0;
    const size_t max_chunk = max_datagram - header_overhead - frame_hdr_max - ack_reserve;

    // Pre-fetch keys once outside the loop
@@ -5441,12 +5456,18 @@ size_t quic::sendStreamData(uint64_t stream_id, const uint8_t* data, size_t len,
                
                vlen = encodeVarInt(la, vbuf); std::memcpy(pkt_buf + frame_off, vbuf, vlen); frame_off += vlen;
                vlen = encodeVarInt(0, vbuf); std::memcpy(pkt_buf + frame_off, vbuf, vlen); frame_off += vlen; // delay=0
                uint64_t rc = _app_pn_recv_ranges.size() - 1;
                // Capped the same way as buildAckFrame() — see
                // MAX_ACK_RANGES_PER_FRAME's comment in socket.h. This
                // matters even more here than there: this writes directly
                // into the fixed-size pkt_buf with no further bounds
                // check, so an unbounded range count would be an actual
                // buffer overflow, not just an oversized packet.
                uint64_t rc = std::min<uint64_t>(_app_pn_recv_ranges.size() - 1, MAX_ACK_RANGES_PER_FRAME);
                vlen = encodeVarInt(rc, vbuf); std::memcpy(pkt_buf + frame_off, vbuf, vlen); frame_off += vlen;
                uint64_t fr = it->second - it->first;
                vlen = encodeVarInt(fr, vbuf); std::memcpy(pkt_buf + frame_off, vbuf, vlen); frame_off += vlen;
                uint64_t prev_smallest = it->first;
                while (it != _app_pn_recv_ranges.begin()) {
                for (uint64_t encoded = 0; encoded < rc && it != _app_pn_recv_ranges.begin(); ++encoded) {
                    --it;
                    uint64_t gap = prev_smallest - it->second - 2;
                    vlen = encodeVarInt(gap, vbuf); std::memcpy(pkt_buf + frame_off, vbuf, vlen); frame_off += vlen;
@@ -5730,8 +5751,15 @@ std::vector<uint8_t> quic::buildAckFrame(uint64_t largest_ack, uint64_t ack_dela
        vlen = encodeVarInt(ack_delay, vbuf);
        frame.insert(frame.end(), vbuf, vbuf + vlen);

        // Count additional ranges (all except the first/highest)
        uint64_t range_count = recv_ranges.size() - 1;
        // Count additional ranges (all except the first/highest) — capped
        // at MAX_ACK_RANGES_PER_FRAME, keeping the MOST RECENT ranges (the
        // walk below is already highest-to-lowest) and dropping older
        // ones once the connection has accumulated more gaps than fit in
        // one frame. See MAX_ACK_RANGES_PER_FRAME's comment in socket.h:
        // this must match processAckFrame()'s own cap, or a peer running
        // this same library could eventually send an ACK its own decoder
        // would reject.
        uint64_t range_count = std::min<uint64_t>(recv_ranges.size() - 1, MAX_ACK_RANGES_PER_FRAME);
        vlen = encodeVarInt(range_count, vbuf);
        frame.insert(frame.end(), vbuf, vbuf + vlen);

@@ -5742,8 +5770,8 @@ std::vector<uint8_t> quic::buildAckFrame(uint64_t largest_ack, uint64_t ack_dela

        uint64_t prev_smallest = it->first;

        // Encode remaining ranges from high to low
        while (it != recv_ranges.begin()) {
        // Encode remaining ranges from high to low, stopping at range_count
        for (uint64_t encoded = 0; encoded < range_count && it != recv_ranges.begin(); ++encoded) {
            --it;
            uint64_t gap = prev_smallest - it->second - 2;
            vlen = encodeVarInt(gap, vbuf);
+21 −0
Changes for src/socket.h: 21 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -1173,6 +1173,27 @@ namespace netplus {
		// Loss detection
		static constexpr uint64_t PACKET_THRESHOLD = 3;     // RFC 9002 kPacketThreshold
		static constexpr double   TIME_THRESHOLD   = 9.0/8; // RFC 9002 kTimeThreshold

		// Shared by both buildAckFrame() (encoder) and processAckFrame()
		// (decoder) so they can never disagree about how many ACK ranges
		// are acceptable. _app_pn_recv_ranges can grow one entry per lost
		// packet for the lifetime of the connection — this implementation
		// always retransmits lost data under a brand new packet number
		// (see checkLossAndRetransmit()), so the original, now-superseded
		// PN is a gap that will never be filled; it's a permanent range,
		// not a transient one healed by an eventual retransmission at the
		// same PN. A long-lived connection with any sustained loss can
		// therefore accumulate far more ranges than fit in one packet.
		// buildAckFrame() truncates to the MOST RECENT ranges (RFC 9000
		// §13.2.3 explicitly allows this: older ranges matter less since
		// the peer has likely already given up on/retransmitted that data
		// under other packet numbers by the time they'd be reported), and
		// processAckFrame() rejects anything beyond this as malformed —
		// keeping the two sides consistent by construction. Sized so
		// MAX_ACK_RANGES_PER_FRAME ranges (worst case 16 bytes each: two
		// 8-byte varints) plus the fixed fields comfortably fit within one
		// packet at the smallest allowed datagram size (1200 bytes).
		static constexpr uint64_t MAX_ACK_RANGES_PER_FRAME = 64;
		int _pto_count = 0;           // PTO back-off counter (RFC 9002 §6.2)

		// Throttle for pumpNetwork()'s opportunistic checkLossAndRetransmit()