Skip to content

Commit

Permalink
ipv4: Fix potential uninit variable access bug in __ip_make_skb()
Browse files Browse the repository at this point in the history
Like commit ea30388 ("ipv6: Fix an uninit variable access bug in
__ip6_make_skb()"). icmphdr does not in skb linear region under the
scenario of SOCK_RAW socket. Access icmp_hdr(skb)->type directly will
trigger the uninit variable access bug.

Use a local variable icmp_type to carry the correct value in different
scenarios.

Fixes: 96793b4 ("[IPV4]: Add ICMPMsgStats MIB (RFC 4293)")
Reviewed-by: Willem de Bruijn <willemb@google.com>
Signed-off-by: Ziyang Xuan <william.xuanziyang@huawei.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
Ziyang Xuan authored and davem330 committed Apr 22, 2023
1 parent 6e79bd2 commit 99e5aca
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions net/ipv4/ip_output.c
Original file line number Diff line number Diff line change
Expand Up @@ -1570,9 +1570,19 @@ struct sk_buff *__ip_make_skb(struct sock *sk,
cork->dst = NULL;
skb_dst_set(skb, &rt->dst);

if (iph->protocol == IPPROTO_ICMP)
icmp_out_count(net, ((struct icmphdr *)
skb_transport_header(skb))->type);
if (iph->protocol == IPPROTO_ICMP) {
u8 icmp_type;

/* For such sockets, transhdrlen is zero when do ip_append_data(),
* so icmphdr does not in skb linear region and can not get icmp_type
* by icmp_hdr(skb)->type.
*/
if (sk->sk_type == SOCK_RAW && !inet_sk(sk)->hdrincl)
icmp_type = fl4->fl4_icmp_type;
else
icmp_type = icmp_hdr(skb)->type;
icmp_out_count(net, icmp_type);
}

ip_cork_release(cork);
out:
Expand Down

0 comments on commit 99e5aca

Please sign in to comment.