quicly address-token decoder reads the IPv6 scope ID from the address bytes, so IPv6 Retry tokens fail validation
I found that quicly_decrypt_address_token() decodes sin6_scope_id from the wrong offset of an IPv6 address token. After copying the 16-byte address it does not advance src, so ptls_decode32() reads the first four bytes of the address as the scope ID, and the scope ID the encoder actually wrote is skipped. h2o compares sin6_scope_id when it validates a token, so a token issued to a global or link-local IPv6 client never matches that client. Loopback ::1 is not affected because its first four bytes are zero.
The code is in h2o's vendored quicly at deps/quicly/lib/quicly.c. Upstream quicly master has the same code (lib/quicly.c#L8189).
ptls_decode_open_block(src, end, 1, {
switch (end - src) {
/* ... */
case 20: /* ipv6 */
plaintext->remote.sin6 = (struct sockaddr_in6){.sin6_family = AF_INET6};
memcpy(&plaintext->remote.sin6.sin6_addr, src, 16); /* src is not advanced */
if ((ret = ptls_decode32(&plaintext->remote.sin6.sin6_scope_id, &src, end)) != 0) /* reads address bytes 0..3 */
goto Exit;
portaddr = &plaintext->remote.sin6.sin6_port;
break;
/* ... */
}
src = end; /* the real scope-id bytes 16..19 are skipped */
});The encoder writes the address and then the scope ID:
case AF_INET6:
ptls_buffer_pushv(buf, &plaintext->remote.sin6.sin6_addr, 16);
ptls_buffer_push32(buf, plaintext->remote.sin6.sin6_scope_id);h2o's validate_token() compares the decoded address with the packet's source address using h2o_socket_compare_address(), and that comparison includes sin6_scope_id:
token->address_mismatch =
h2o_socket_compare_address(remote, &token->remote.sa, token->type == QUICLY_ADDRESS_TOKEN_TYPE_RETRY) != 0;
switch (token->type) {
case QUICLY_ADDRESS_TOKEN_TYPE_RETRY:
if (token->address_mismatch)
return 0;History: the scope-ID field was added by h2o PR #3299 (quicly PR #563), "[ipv6] fix address token validation failures", to fix #3298 (HTTP/3 over IPv6 failing with retry: ON). The decoder in that change never advances past the address. t/40http3-retry.t tests only 127.0.0.1, and ::1 happens to decode correctly, so tests do not catch it.
Spec: RFC 9000 §8.1.2 and §8.1.3 require the server to validate that a token was issued to the client's address. Address tokens are opaque to the client and are produced and consumed only by the server, so the encoder and decoder must agree on the layout.
Trigger: configure an h2o HTTP/3 listener with quic: { retry: ON }, then connect from a global IPv6 address (for example 2001:db8::1) or a link-local address with a non-zero interface index.
Actual result:
- The server sends a Retry. The client echoes the token.
- The decoded token's
sin6_scope_idis0x20010db8while the socket's is0, sovalidate_token()returns 0 andtokenstays NULL. - The server sends a second Retry. RFC 9000 §17.2.5.2 requires a client to discard any Retry after the first, so the handshake cannot complete.
- Resumption (NEW_TOKEN) tokens from IPv6 clients are all marked
address_mismatch, soaddress_validation.validatedis never set from them (quicly.c#L7323).
Expected result: the decoded sin6_scope_id equals the encoded one, and the token validates for the same client.
Verification: issues/verification/h2o_quicly_token/token_ipv6.c round-trips tokens through the unmodified quicly_encrypt_address_token() and quicly_decrypt_address_token(), built with ASan/UBSan:
::1 scope_id in=0x00000000 out=0x00000000 decrypt_ret=0 -> round-trip OK
2001:db8::1 scope_id in=0x00000000 out=0x20010db8 decrypt_ret=0 -> MISMATCH
fe80::1 scope_id in=0x00000002 out=0xfe800000 decrypt_ret=0 -> MISMATCHImpact: this is a functional and interoperability defect, not a memory-safety issue. Tokens are AEAD-protected, so a remote party cannot choose the decoded value. The consequences are:
- With
retry: ON, IPv6 clients (other than::1) cannot complete HTTP/3 handshakes. This is the same failure #3298 was meant to fix. - IPv6 clients never get address validation from resumption tokens.
I verified the decoder with the harness above. I did not run a full h2o server over IPv6.
Suggested fix: advance past the address before decoding the scope ID, and add an IPv6 case (non-loopback, with a non-zero scope ID) to test_address_token_codec:
memcpy(&plaintext->remote.sin6.sin6_addr, src, 16);
src += 16;
if ((ret = ptls_decode32(&plaintext->remote.sin6.sin6_scope_id, &src, end)) != 0)
goto Exit;Source: h2o/h2o