Alejandro Pacheco via dev <[email protected]> writes:

> From: Alejandro Pacheco <[email protected]>
>
> extract_l4() validates the ICMP header only for IPPROTO_ICMP over IPv4
> and the ICMPv6 header only for IPPROTO_ICMPV6 over IPv6.  Any other
> combination, such as protocol 58 in an IPv4 header, is accepted as a
> protocol without L4 keys and with no length check.  The handler table
> is indexed by the protocol number alone, though, so such a packet gets
> the ICMP or ICMPv6 handler, whose valid_new() reads the type from an
> L4 header that may not be in the packet.
>
> Add l4_proto_get(), which picks a handler from both the address family
> and the protocol in the same way extract_l4() does, and use it for every
> handler lookup.  Other combinations now use the generic handler, which
> does not look at the L4 header.
>
> Fixes: a27d70a89848 ("conntrack: add generic IP protocol support")
> Assisted-by: claude-opus-5-5, Claude Code
> Signed-off-by: Alejandro Pacheco <[email protected]>
> ---
>  lib/conntrack.c       | 25 ++++++++++++++++++++-----
>  tests/ofproto-dpif.at | 41 +++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 61 insertions(+), 5 deletions(-)


The following is an AI LLM review of the patch.  A human has reviewed it
and found the report to be plausible.

> diff --git a/lib/conntrack.c b/lib/conntrack.c
> index d40870c63..a81072f33 100644
> --- a/lib/conntrack.c
> +++ b/lib/conntrack.c
> @@ -157,6 +157,21 @@ expectation_clean(struct conntrack *ct, const struct 
> conn_key *parent_key);
>  
>  static struct ct_l4_proto *l4_protos[UINT8_MAX + 1];
>  
> +/* Returns the l4 protocol handler for 'key'.  ICMP and ICMPv6 handlers are
> + * used only with their own address family, since extract_l4() validates the
> + * ICMP headers only in that case. */
> +static struct ct_l4_proto *
> +l4_proto_get(const struct conn_key *key)
> +{
> +    if ((key->nw_proto == IPPROTO_ICMP
> +         && key->dl_type != htons(ETH_TYPE_IP))
> +        || (key->nw_proto == IPPROTO_ICMPV6
> +            && key->dl_type != htons(ETH_TYPE_IPV6))) {
> +        return &ct_proto_other;
> +    }
> +    return l4_protos[key->nw_proto];
> +}
> +
>  static void
>  handle_ftp_ctl(struct conntrack *ct, const struct conn_lookup_ctx *ctx,
>                 struct dp_packet *pkt, struct conn *ec, long long now,
> @@ -2667,9 +2682,9 @@ conn_update(struct conntrack *ct, struct conn *conn, 
> struct dp_packet *pkt,
>              struct conn_lookup_ctx *ctx, long long now)
>  {
>      ovs_mutex_lock(&conn->lock);
> -    uint8_t nw_proto = conn->key_node[CT_DIR_FWD].key.nw_proto;
>      enum ct_update_res update_res =
> -        l4_protos[nw_proto]->conn_update(ct, conn, pkt, ctx->reply, now);
> +        l4_proto_get(&conn->key_node[CT_DIR_FWD].key)->conn_update(
> +            ct, conn, pkt, ctx->reply, now);
>      ovs_mutex_unlock(&conn->lock);
>      return update_res;
>  }
> @@ -2703,14 +2718,14 @@ conn_expired(const struct conn *conn, long long now)
>  static bool
>  valid_new(struct dp_packet *pkt, struct conn_key *key)
>  {
> -    return l4_protos[key->nw_proto]->valid_new(pkt);
> +    return l4_proto_get(key)->valid_new(pkt);
>  }
>  
>  static struct conn *
>  new_conn(struct conntrack *ct, struct dp_packet *pkt, struct conn_key *key,
>           long long now, uint32_t tp_id)
>  {
> -    return l4_protos[key->nw_proto]->new_conn(ct, pkt, now, tp_id);
> +    return l4_proto_get(key)->new_conn(ct, pkt, now, tp_id);
>  }
>  
>  static void
> @@ -2837,7 +2852,7 @@ conn_to_ct_dpif_entry(const struct conn *conn, struct 
> ct_dpif_entry *entry,
>  
>      long long expiration = conn_expiration(conn) - now;
>  
> -    struct ct_l4_proto *class = l4_protos[key->nw_proto];
> +    struct ct_l4_proto *class = l4_proto_get(key);

Behavior change here is benign: only TCP sets `conn_get_protoinfo`, so
family-mismatched ICMP keys move from an ICMP handler to the generic
one, and neither has the callback. Similarly, dropping the local
`nw_proto` variable in `conn_update()` is a pure substitution under the
same `conn->lock`.

One residual inconsistency, pre-existing and not made worse by this
commit but worth noting since it now diverges from the handler choice:
NAT still decides port translation purely on protocol, `pat_proto =
... || fwd_key->nw_proto == IPPROTO_ICMP` (lib/conntrack.c:2614-2617),
and the ct tuple/entry conversion keys off
`IPPROTO_ICMP`/`IPPROTO_ICMPV6` without checking family
(lib/conntrack.c:767-787, 2796). For a family-mismatched packet these
paths operate on zero-filled port/icmp fields, so they are harmless
today, but they are the next places that assume protocol implies
family. I did not attempt to construct a NAT scenario that turns this
into a real corruption — unverified beyond reading the code.

_______________________________________________
dev mailing list
[email protected]
https://mail.openvswitch.org/mailman/listinfo/ovs-dev

Reply via email to