From bf26d66e6ca098bf3642d1af8b3c66fc525f0e5e Mon Sep 17 00:00:00 2001 From: Laurin Brandner Date: Mon, 16 Nov 2026 08:09:13 +0100 Subject: [PATCH] fix(http2): linearize before returning an error --- beeper/src/http2/parser.bpf.c | 12 ++++++-- beeper/tests/http2.rs | 52 +++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/beeper/src/http2/parser.bpf.c b/beeper/src/http2/parser.bpf.c index 5935ea1..5c0bf40 100644 --- a/beeper/src/http2/parser.bpf.c +++ b/beeper/src/http2/parser.bpf.c @@ -1119,16 +1119,24 @@ static __always_inline int _parse_frame(const struct msg_ctx *ctx, u32 off, u32 // Returns the number of bytes consumed, or a negative error in case of failure. SEC("freplace") int parse_msg(struct sk_msg_md *msg, struct http_parse_res *pres __arg_nonnull, struct http2_frame *frame __arg_nonnull) { + u32 size = msg->size; + if (size < HTTP2_FRAME_HDR_LEN) return 0; + u8 *data = (u8 *)(long)msg->data; u8 *data_end = (u8 *)(long)msg->data_end; - if (data + HTTP2_FRAME_HDR_LEN > data_end) return 0; + if (data + HTTP2_FRAME_HDR_LEN > data_end) { + if (bpf_msg_pull_data(msg, 0, HTTP2_FRAME_HDR_LEN, 0) < 0) return -EFAULT; + + data = (u8 *)(long)msg->data; + data_end = (u8 *)(long)msg->data_end; + if (data + HTTP2_FRAME_HDR_LEN > data_end) return -EPROTO; + } u32 len = data[0] << 16 | data[1] << 8 | data[2]; u8 type = data[3]; u8 flags = data[4]; u32 frame_len = HTTP2_FRAME_HDR_LEN + len; - u32 size = msg->size; *frame = _new_http2_frame(data, len, type, flags); diff --git a/beeper/tests/http2.rs b/beeper/tests/http2.rs index a7ba012..ef97058 100644 --- a/beeper/tests/http2.rs +++ b/beeper/tests/http2.rs @@ -859,6 +859,58 @@ async fn parse_frame_after_an_unknown_one() { ); } +#[tokio::test] +async fn parse_frame_whose_header_spans_two_fragments() { + let addr = server::launch().await.expect("launch server"); + + let mut open_obj = OpenObject::new(); + let (prog, _h1, _h2, mids) = + attach_at(addr, &mut open_obj, Hook::Msg, &[header::ACCEPT.as_str()]); + + let mut client = RawClient::connect(addr).await; + + // the kernel copies a write to the start of the page fragment of this + // thread only once nothing sent before holds on to it anymore. A full + // exchange, and a pause for the acknowledgements the server may delay by + // 40ms, see to that + client + .request(raw_request_block( + &addr.to_string(), + &[(Some(19), "accept", "text/plain")], + )) + .await; + tokio::time::sleep(Duration::from_millis(200)).await; + + // unassigned frame types, which the server discards, up to 4 bytes short + // of the end of the page fragment the write is copied to. The data + // pointers of an `sk_msg` program only cover a single fragment, so the + // header of the frame behind them is split across two. The fragment holds + // 32KiB, or a page if the kernel cannot get that much in one piece, which + // ends at the same offset + const FRAG_LEN: usize = 32 * 1024; + const MAX_FRAME_LEN: usize = 16 * 1024; + let mut out = frame(0xFA, 0, 0, &[0; MAX_FRAME_LEN]); + let rest = FRAG_LEN - 4 - out.len() - 9; + out.extend_from_slice(&frame(0xFA, 0, 0, &vec![0; rest])); + assert_eq!(out.len(), FRAG_LEN - 4); + + let block = raw_request_block(&addr.to_string(), &[(Some(19), "accept", "*/*")]); + let id = client.next_stream_id; + client.next_stream_id += 2; + // END_STREAM | END_HEADERS + out.extend_from_slice(&frame(0x01, 0x05, id, &block)); + + client.send_raw(&out).await; + client.read_frame(0x01).await; + + let accept_val = HeaderValue::from_static("*/*"); + assert_eq!( + prog.get_match(mids[0]).expect("get_match").as_deref(), + Some(accept_val.as_bytes()), + "the parser did not read a frame header split across two fragments" + ); +} + #[tokio::test] async fn update_dynamic_table_size() { let addr = server::launch().await.expect("launch server");