geneve_build_header() announces info->options_len in the wire header unconditionally, but only copies the option bytes when the tunnel metadata carries the geneve option flag. With metadata of another tunnel type (vxlan, erspan), set for example by a tc tunnel_key action, the announced option space is never written: the packet goes out with whatever bytes __skb_push() left in the skb headroom, and the peer can read 4 or 12 stale kernel bytes per packet, repeatedly. Only announce and emit options that are actually geneve options. Other metadata is dropped from the wire header instead of being replaced by stale headroom bytes: the option space sizing and the GRO hint offset now both follow the same condition as the copy. This issue was discovered by Tencent CodeBuddy Security. Cc: stable@vger.kernel.org Fixes: 256c87c17c53 ("net: check tunnel option type in tunnel flags") Signed-off-by: Henry Martin --- drivers/net/geneve.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/drivers/net/geneve.c b/drivers/net/geneve.c index 0ab729f055ea0..dd1734b82af59 100644 --- a/drivers/net/geneve.c +++ b/drivers/net/geneve.c @@ -1231,7 +1231,7 @@ static void geneve_build_header(struct genevehdr *geneveh, __be16 inner_proto) { geneveh->ver = GENEVE_VER; - geneveh->opt_len = info->options_len / 4; + geneveh->opt_len = 0; geneveh->oam = test_bit(IP_TUNNEL_OAM_BIT, info->key.tun_flags); geneveh->critical = test_bit(IP_TUNNEL_CRIT_OPT_BIT, info->key.tun_flags); @@ -1240,8 +1240,15 @@ static void geneve_build_header(struct genevehdr *geneveh, geneveh->proto_type = inner_proto; geneveh->rsvd2 = 0; - if (test_bit(IP_TUNNEL_GENEVE_OPT_BIT, info->key.tun_flags)) + /* Only announce and copy options that are actually geneve + * options. Metadata of another tunnel type (vxlan, erspan) + * must not go on the wire: there is nothing to copy it from, + * and the option space would carry stale headroom bytes. + */ + if (test_bit(IP_TUNNEL_GENEVE_OPT_BIT, info->key.tun_flags)) { + geneveh->opt_len = info->options_len / 4; ip_tunnel_info_opts_get(geneveh->options, info); + } } static int geneve_build_gro_hint_opt(const struct geneve_dev *geneve, @@ -1336,7 +1343,10 @@ static int geneve_build_skb(struct dst_entry *dst, struct sk_buff *skb, skb_reset_mac_header(skb); skb_scrub_packet(skb, xnet); - opt_size = info->options_len + cb->gro_hint_len; + if (test_bit(IP_TUNNEL_GENEVE_OPT_BIT, info->key.tun_flags)) + opt_size = info->options_len + cb->gro_hint_len; + else + opt_size = cb->gro_hint_len; min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len + GENEVE_BASE_HLEN + opt_size + ip_hdr_len; err = skb_cow_head(skb, min_headroom); @@ -1353,7 +1363,8 @@ static int geneve_build_skb(struct dst_entry *dst, struct sk_buff *skb, geneve_build_header(gnvh, info, inner_proto); if (cb->gro_hint_len) - geneve_put_gro_hint_opt(gnvh, info->options_len, &cb->gro_hint); + geneve_put_gro_hint_opt(gnvh, opt_size - cb->gro_hint_len, + &cb->gro_hint); udp_tunnel_set_inner_protocol(skb, double_encap, inner_proto); return 0; -- 2.39.5