diff --git a/beeper/src/dfa.rs b/beeper/src/dfa.rs index bf4f735..54596dd 100644 --- a/beeper/src/dfa.rs +++ b/beeper/src/dfa.rs @@ -76,6 +76,10 @@ impl DfaBuilder<'_, A> { /// Attaches `action` to every transition that leads into the state the /// pattern has been built up to. + /// + /// # Panics + /// + /// Panics if the pattern has no transition to attach `action` to. pub fn with(&mut self, action: A) -> &mut Self { trace!("with; state={:?}, action={:?}", self.state, action); @@ -83,6 +87,12 @@ impl DfaBuilder<'_, A> { // the routes into it and has to carry the action too self.push_optional_prefixes(); + assert!( + !self.last_edges.is_empty(), + "Cannot attach {action:?} to {:?} without a transition", + self.state + ); + for (from, input, case_sensitive) in self.last_edges.clone() { self.dfa.add_action(from, input, action); diff --git a/beeper/src/http1/parser.bpf.c b/beeper/src/http1/parser.bpf.c index a0c79dd..3467c5e 100644 --- a/beeper/src/http1/parser.bpf.c +++ b/beeper/src/http1/parser.bpf.c @@ -58,17 +58,54 @@ static __always_inline void _next(u16 state, u8 input, u16 *next_state, u16 *act *action = t.action; } +// The start index of a capture that no transition has opened. +#define CIDX_NONE ((u32)-1) + +// Runs the action `a` of a transition after which the message continues at +// index `next`, recording the ranges it captures in `ms`. `cidx` and +// `msg_start` are those of `_parse_from`. +// +// Returns whether the action ends parsing. +static __always_inline bool _run_action(u16 a, u32 next, struct http_match *ms, u32 *cidx, u32 *msg_start) { + struct http1_action act = _action(a); + if (act.kind == HTTP1A_START_CAPTURE) { + u16 mid = act.mid & MAX_MATCH_MASK; + bpf_debug("start capture range (%d) in [%d, ...]", mid, next); + cidx[mid] = next; + } + else if (act.kind == HTTP1A_END_CAPTURE) { + u16 mid = act.mid & MAX_MATCH_MASK; + u32 from = cidx[mid]; + if (from == CIDX_NONE) from = *msg_start; + bpf_debug("end capture range (%d) in [%d, %d]", mid, from, next - from); + + ms[mid] = (struct http_match) { + .idx = from, + .len = next - from, + .source = HTTP_SRC_MSG + }; + } + + if ((act.flags & HTTP1F_DONE) != 0) { + bpf_debug("done parsing at %d", next); + return true; + } + + return false; +} + // Walks the DFA over `data`, starting at offset `start` and in state `*s`, and // records the ranges it captures in `ms`. `cidx` holds the start index of every -// open capture, `s` the state the walk ended in, so that a caller can resume -// where it stopped. +// open capture, or `CIDX_NONE`, `msg_start` the index the message begins at and +// `s` the state the walk ended in, so that a caller can resume where it stopped. // // `null_prefix` is the length of the run of NULL bytes at the beginning of the // buffer that is to be skipped rather than parsed; it is updated as those bytes -// are consumed. It may be NULL if the data cannot carry such a prefix. +// are consumed, and the message begins after them. It may be NULL if the data +// cannot carry such a prefix. // // Returns the number of bytes consumed, or its negative if data ran out. -static __always_inline int _parse_from(u8 *data, u8 *data_end, u16 start, struct http_match *ms, u32* cidx, u16* s, struct null_prefix *null_prefix) { +static __always_inline int _parse_from(u8 *data, u8 *data_end, u16 start, struct http_match *ms, u32* cidx, u32 *msg_start, u16* s, struct null_prefix *null_prefix) { u32 len = (u32)(data_end - data); bpf_clamp_uminmax(len, 0, MAX_BYTES); @@ -84,6 +121,7 @@ static __always_inline int _parse_from(u8 *data, u8 *data_end, u16 start, struct // skb clears the TLS header, but does not remove it if (null_prefix && c == '\0' && i == null_prefix->len) { null_prefix->len = i + 1; + if (*msg_start == i) *msg_start = i + 1; continue; } @@ -94,26 +132,8 @@ static __always_inline int _parse_from(u8 *data, u8 *data_end, u16 start, struct _next(s_any, c, s, &a); } - struct http1_action act = _action(a); - if (act.kind == HTTP1A_START_CAPTURE) { - u16 mid = act.mid & MAX_MATCH_MASK; - bpf_debug("start capture range (%d) in [%d, ...]", mid, i+1); - cidx[mid] = i + 1; - } - else if (act.kind == HTTP1A_END_CAPTURE) { - u16 mid = act.mid & MAX_MATCH_MASK; - bpf_debug("end capture range (%d) in [%d, %d]", mid, cidx[mid], i - cidx[mid] + 1); - - ms[mid] = (struct http_match) { - .idx = cidx[mid], - .len = i - cidx[mid] + 1, - .source = HTTP_SRC_MSG - }; - } - - if ((act.flags & HTTP1F_DONE) != 0) { - bpf_debug("done parsing at %d", i); - return i+1; + if (_run_action(a, i + 1, ms, cidx, msg_start)) { + return i + 1; } } @@ -128,11 +148,12 @@ static __always_inline int _parse_from(u8 *data, u8 *data_end, u16 start, struct // Returns the header length, 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) { - u32 cidx[MAX_MATCHES] = { 0 }; + u32 cidx[MAX_MATCHES] = { [0 ... MAX_MATCHES - 1] = CIDX_NONE }; + u32 msg_start = 0; u16 s = s_init; u8 *data = (u8 *)(long)msg->data; u8 *data_end = (u8 *)(long)msg->data_end; - int res = _parse_from(data, data_end, 0, pres->ms, cidx, &s, NULL); + int res = _parse_from(data, data_end, 0, pres->ms, cidx, &msg_start, &s, NULL); if (res < 0 && msg->size > -res) { if (bpf_msg_pull_data(msg, 0, msg->size, 0) < 0) { @@ -142,7 +163,7 @@ int parse_msg(struct sk_msg_md *msg, struct http_parse_res *pres __arg_nonnull) u8 *data = (u8 *)(long)msg->data; u8 *data_end = (u8 *)(long)msg->data_end; - res = _parse_from(data, data_end, -res, pres->ms, cidx, &s, NULL); + res = _parse_from(data, data_end, -res, pres->ms, cidx, &msg_start, &s, NULL); } if (res > 0) return res; @@ -169,9 +190,10 @@ int parse_skb(struct __sk_buff *skb, u32 off, struct http_parse_res *pres __arg_ data_end = (u8 *)(long)skb->data_end; } - u32 cidx[MAX_MATCHES] = { 0 }; + u32 cidx[MAX_MATCHES] = { [0 ... MAX_MATCHES - 1] = CIDX_NONE }; + u32 msg_start = off; u16 s = s_init; - int res = _parse_from(data, data_end, off, pres->ms, cidx, &s, null_prefix); + int res = _parse_from(data, data_end, off, pres->ms, cidx, &msg_start, &s, null_prefix); if (res > 0) return res - (int)off; return skb->len >= MAX_BYTES ? -E2BIG : -EAGAIN; diff --git a/beeper/src/http1/parser.rs b/beeper/src/http1/parser.rs index bfcd138..a3f4121 100644 --- a/beeper/src/http1/parser.rs +++ b/beeper/src/http1/parser.rs @@ -229,9 +229,9 @@ impl Parser { /// ``` pub fn match_http2_preface(&mut self) -> Result { let mid = self.new_match()?; + // a capture that no transition opens starts with the message self.dfa .start_pattern(INIT_STATE) - .with(Action::StartCapture(mid)) .push(&format!( "PRI * HTTP/2.0{}{}{}{}SM{}{}{}{}", CR, LF, CR, LF, CR, LF, CR, LF @@ -261,7 +261,6 @@ impl Parser { if name == METHOD.as_str() { self.dfa .start_pattern(INIT_STATE) - .with(Action::StartCapture(mid)) .push_options_ci(&methods) .with(Action::EndCapture(mid)) .push(" ") diff --git a/beeper/tests/http1.rs b/beeper/tests/http1.rs index a46637e..0e3af45 100644 --- a/beeper/tests/http1.rs +++ b/beeper/tests/http1.rs @@ -623,3 +623,28 @@ async fn report_the_fields_that_were_matched() { "only the user agent is matched" ); } + +#[tokio::test] +async fn parse_the_status_line_behind_a_null_prefix_in_skb() { + let addr = server::launch().await.expect("launch server"); + + let mut open_obj = OpenObject::new(); + let prog = TestProgram::attach_to(addr, &mut open_obj, Direction::Downstream, Hook::Skb) + .expect("attach"); + let (_h1, mids) = attach_http1_parser( + prog.prog_fd(), + Hook::Skb, + true, + &[pseudo_header::METHOD.as_str(), pseudo_header::PATH.as_str()], + ); + + // kTLS zeroes the 13 bytes of the TLS record header in front of the + // message rather than removing them + let mut req = vec![0; 13]; + req.extend_from_slice(format!("POST /compose HTTP/1.1\r\nHost: {addr}\r\n\r\n").as_bytes()); + + _ = send_raw_bytes(addr, &req).await; + + assert_match_bytes_eq(&prog, mids[0], Some(b"POST")); + assert_match_bytes_eq(&prog, mids[1], Some(b"/compose")); +} diff --git a/utils/src/prog.bpf.c b/utils/src/prog.bpf.c index b84654a..46d9509 100644 --- a/utils/src/prog.bpf.c +++ b/utils/src/prog.bpf.c @@ -512,7 +512,10 @@ int skb_verdict(struct __sk_buff *skb) { } } else { - if (parse_http1_skb(skb, off, &pres, NULL) < 0) return SK_PASS; + // a message behind a run of NULL bytes stands in for one that kTLS + // decrypted, which zeroes the TLS header rather than removing it + struct null_prefix null_prefix = { 0 }; + if (parse_http1_skb(skb, off, &pres, &null_prefix) < 0) return SK_PASS; if (matched_http1(&pres, 0)) { upgrade_conn(&ikey);