Fix misaligned header pointer access in CreateTcpResetPacket. ### Why are we doing this? `//third_party/quic/qbone/platform:tcp_packet_test` currently passes on `x86_64` (`--cpu=k8`) but fails on `Arm` (`--cpu=arm`) (see [go/multiarch-fix](http://go/multiarch-fix)). Under `--config=ubsan`, `//third_party/quic/qbone/platform:tcp_packet_test` failed on Arm because `[tcp_packet.cc](http://cs/f:google3/third_party/quic/qbone/platform/tcp_packet.cc)` cast the raw byte buffer from `original_packet.data()` directly to `const ip6_hdr*` via `reinterpret_cast`. On Arm, `kReferenceTCPSYNPacket` in `[tcp_packet_test.cc](http://cs/f:google3/third_party/quic/qbone/platform/tcp_packet_test.cc)` happened to be placed at an address that was not 4-byte aligned (e.g. ending in `...b2`), whereas on x86_64 it happened to be aligned. Accessing struct members through a misaligned pointer is undefined behavior and triggered UndefinedBehaviorSanitizer's `misaligned-pointer-use` runtime error on Arm. ### What changed In [tcp_packet.cc](http://cs/f:google3/third_party/quic/qbone/platform/tcp_packet.cc), copy `ip6_hdr` and `tcphdr` into naturally aligned stack structures using `memcpy` rather than casting unaligned byte pointers with `reinterpret_cast`. Also verify that `original_packet.size()` is large enough to contain both `ip6_hdr` and `tcphdr` before copying the TCP header. ### Verification Verified with `blaze test --nocache_test_results`: - Pre-fix `--cpu=k8` (`PASS`): http://sponge2/dd1bf5ff-8512-4572-aad5-ac7dd22d6c8b - Pre-fix `--cpu=arm` (`FAIL`): http://sponge2/8f3ea8a2-678c-4166-995e-2a6c846a7e40 - Post-fix `--cpu=arm` (`PASS`): http://sponge2/c76c4440-ca59-4047-acfb-d26fc3b3eac7 - Post-fix `--cpu=k8` (`PASS`): http://sponge2/cf42a087-6bfc-4f94-b77c-876d041c62f1 Generated by [Lightway](http://go/lightway) for `multiarch_divergence_fixer`: [Pipeline summary](http://g3doc/.journal/g3doc/pipeline_summary?cl=groupagent-lightway-ai/1111606) | [Run dashboard](http://go/lightway-runs?f=RunId:eq:937aa2b5-4a8c-416f-9c42-c81a7fa6127e) #multiarch_divergence_fixer #fix-divergence #lightway Tested: Local presubmit tests passed. PiperOrigin-RevId: 1008932221
diff --git a/quiche/quic/qbone/platform/tcp_packet.cc b/quiche/quic/qbone/platform/tcp_packet.cc index 6d6bd9a..c63a9fa 100644 --- a/quiche/quic/qbone/platform/tcp_packet.cc +++ b/quiche/quic/qbone/platform/tcp_packet.cc
@@ -6,6 +6,8 @@ #include <netinet/ip6.h> +#include <cstring> + #include "absl/base/optimization.h" #include "absl/strings/string_view.h" #include "quiche/quic/platform/api/quic_logging.h" @@ -41,18 +43,24 @@ if (ABSL_PREDICT_FALSE(original_packet.size() < sizeof(ip6_hdr))) { return; } - auto* ip6_header = reinterpret_cast<const ip6_hdr*>(original_packet.data()); - if (ABSL_PREDICT_FALSE(ip6_header->ip6_vfc >> 4 != 6)) { + ip6_hdr ip6_header; + memcpy(&ip6_header, original_packet.data(), sizeof(ip6_hdr)); + if (ABSL_PREDICT_FALSE(ip6_header.ip6_vfc >> 4 != 6)) { return; } - if (ABSL_PREDICT_FALSE(ip6_header->ip6_nxt != IPPROTO_TCP)) { + if (ABSL_PREDICT_FALSE(ip6_header.ip6_nxt != IPPROTO_TCP)) { return; } if (ABSL_PREDICT_FALSE(quiche::QuicheEndian::NetToHost16( - ip6_header->ip6_plen) < sizeof(tcphdr))) { + ip6_header.ip6_plen) < sizeof(tcphdr))) { return; } - auto* tcp_header = reinterpret_cast<const tcphdr*>(ip6_header + 1); + if (ABSL_PREDICT_FALSE(original_packet.size() < + sizeof(ip6_hdr) + sizeof(tcphdr))) { + return; + } + tcphdr tcp_header; + memcpy(&tcp_header, original_packet.data() + sizeof(ip6_hdr), sizeof(tcphdr)); // Now that the original packet has been confirmed to be well-formed, it's // time to make the TCP RST packet. @@ -69,12 +77,12 @@ tcp_packet.ip_header.ip6_hops = kTcpTtl; // Since the TCP RST is impersonating the endpoint, flip the source and // destination addresses from the original packet. - tcp_packet.ip_header.ip6_src = ip6_header->ip6_dst; - tcp_packet.ip_header.ip6_dst = ip6_header->ip6_src; + tcp_packet.ip_header.ip6_src = ip6_header.ip6_dst; + tcp_packet.ip_header.ip6_dst = ip6_header.ip6_src; // The same is true about the TCP ports - tcp_packet.tcp_header.dest = tcp_header->source; - tcp_packet.tcp_header.source = tcp_header->dest; + tcp_packet.tcp_header.dest = tcp_header.source; + tcp_packet.tcp_header.source = tcp_header.dest; // There are no extensions in this header, so size is trivial tcp_packet.tcp_header.doff = sizeof(tcphdr) >> 2; @@ -95,15 +103,15 @@ // If the incoming segment has an ACK field, the reset takes its sequence // number from the ACK field of the segment, - if (tcp_header->ack) { - tcp_packet.tcp_header.seq = tcp_header->ack_seq; + if (tcp_header.ack) { + tcp_packet.tcp_header.seq = tcp_header.ack_seq; } else { // Otherwise the reset has sequence number zero and the ACK field is set to // the sum of the sequence number and segment length of the incoming segment tcp_packet.tcp_header.ack = 1; tcp_packet.tcp_header.seq = 0; tcp_packet.tcp_header.ack_seq = quiche::QuicheEndian::HostToNet32( - quiche::QuicheEndian::NetToHost32(tcp_header->seq) + 1); + quiche::QuicheEndian::NetToHost32(tcp_header.seq) + 1); } TCPv6PseudoHeader pseudo_header{};
diff --git a/quiche/quic/qbone/platform/tcp_packet_test.cc b/quiche/quic/qbone/platform/tcp_packet_test.cc index c9ada0c..fe93e62 100644 --- a/quiche/quic/qbone/platform/tcp_packet_test.cc +++ b/quiche/quic/qbone/platform/tcp_packet_test.cc
@@ -5,8 +5,11 @@ #include "quiche/quic/qbone/platform/tcp_packet.h" #include <netinet/ip6.h> +#include <netinet/tcp.h> #include <cstdint> +#include <cstring> +#include <string> #include "absl/strings/string_view.h" #include "quiche/quic/platform/api/quic_test.h" @@ -108,10 +111,58 @@ absl::string_view expected_packet = absl::string_view(reinterpret_cast<const char*>(kReferenceTCPRSTPacket), sizeof(kReferenceTCPRSTPacket)); - CreateTcpResetPacket(syn, [&expected_packet](absl::string_view packet) { + bool callback_called = false; + CreateTcpResetPacket(syn, [&](absl::string_view packet) { + callback_called = true; QUIC_LOG(INFO) << quiche::QuicheTextUtils::HexDump(packet); ASSERT_EQ(packet, expected_packet); }); + EXPECT_TRUE(callback_called); +} + +TEST(TcpPacketTest, PacketTooShortForTcpHeaderDoesNotGenerateReset) { + absl::string_view truncated_packet( + reinterpret_cast<const char*>(kReferenceTCPSYNPacket), + sizeof(ip6_hdr) + sizeof(tcphdr) - 1); + bool callback_called = false; + CreateTcpResetPacket(truncated_packet, + [&](absl::string_view) { callback_called = true; }); + EXPECT_FALSE(callback_called); +} + +TEST(TcpPacketTest, ResetForPacketWithAckTakesSequenceFromAckSeq) { + std::string packet(reinterpret_cast<const char*>(kReferenceTCPSYNPacket), + sizeof(kReferenceTCPSYNPacket)); + ip6_hdr ip6; + memcpy(&ip6, packet.data(), sizeof(ip6_hdr)); + ip6.ip6_dst.s6_addr[15] = 2; + memcpy(packet.data(), &ip6, sizeof(ip6_hdr)); + + tcphdr tcp_hdr; + memcpy(&tcp_hdr, packet.data() + sizeof(ip6_hdr), sizeof(tcphdr)); + tcp_hdr.ack = 1; + tcp_hdr.ack_seq = 0x12345678; + memcpy(packet.data() + sizeof(ip6_hdr), &tcp_hdr, sizeof(tcphdr)); + + bool callback_called = false; + CreateTcpResetPacket(packet, [&](absl::string_view rst_packet) { + callback_called = true; + ASSERT_EQ(rst_packet.size(), sizeof(ip6_hdr) + sizeof(tcphdr)); + + ip6_hdr rst_ip6; + memcpy(&rst_ip6, rst_packet.data(), sizeof(ip6_hdr)); + EXPECT_EQ(rst_ip6.ip6_src.s6_addr[15], 2); + EXPECT_EQ(rst_ip6.ip6_dst.s6_addr[15], 1); + + tcphdr rst_tcp; + memcpy(&rst_tcp, rst_packet.data() + sizeof(ip6_hdr), sizeof(tcphdr)); + EXPECT_EQ(rst_tcp.rst, 1); + EXPECT_EQ(rst_tcp.ack, 0); + EXPECT_EQ(rst_tcp.seq, 0x12345678); + EXPECT_EQ(rst_tcp.source, tcp_hdr.dest); + EXPECT_EQ(rst_tcp.dest, tcp_hdr.source); + }); + EXPECT_TRUE(callback_called); } } // namespace quic