Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

net: sched: fix TCF_LAYER_TRANSPORT handling in tcf_get_base_ptr()

syzbot reported that tcf_get_base_ptr() can be called while transport
header is not set [1].

Instead of returning a dangling pointer, return NULL.

Fix tcf_get_base_ptr() callers to handle this NULL value.

[1]
WARNING: CPU: 1 PID: 6019 at ./include/linux/skbuff.h:3071 skb_transport_header include/linux/skbuff.h:3071 [inline]
WARNING: CPU: 1 PID: 6019 at ./include/linux/skbuff.h:3071 tcf_get_base_ptr include/net/pkt_cls.h:539 [inline]
WARNING: CPU: 1 PID: 6019 at ./include/linux/skbuff.h:3071 em_nbyte_match+0x2d8/0x3f0 net/sched/em_nbyte.c:43
Modules linked in:
CPU: 1 UID: 0 PID: 6019 Comm: syz.0.17 Not tainted syzkaller #0 PREEMPT(full)
Call Trace:
<TASK>
tcf_em_match net/sched/ematch.c:494 [inline]
__tcf_em_tree_match+0x1ac/0x770 net/sched/ematch.c:520
tcf_em_tree_match include/net/pkt_cls.h:512 [inline]
basic_classify+0x115/0x2d0 net/sched/cls_basic.c:50
tc_classify include/net/tc_wrapper.h:197 [inline]
__tcf_classify net/sched/cls_api.c:1764 [inline]
tcf_classify+0x4cf/0x1140 net/sched/cls_api.c:1860
multiq_classify net/sched/sch_multiq.c:39 [inline]
multiq_enqueue+0xfd/0x4c0 net/sched/sch_multiq.c:66
dev_qdisc_enqueue+0x4e/0x260 net/core/dev.c:4118
__dev_xmit_skb net/core/dev.c:4214 [inline]
__dev_queue_xmit+0xe83/0x3b50 net/core/dev.c:4729
packet_snd net/packet/af_packet.c:3076 [inline]
packet_sendmsg+0x3e33/0x5080 net/packet/af_packet.c:3108
sock_sendmsg_nosec net/socket.c:727 [inline]
__sock_sendmsg+0x21c/0x270 net/socket.c:742
____sys_sendmsg+0x505/0x830 net/socket.c:2630

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Reported-by: syzbot+f3a497f02c389d86ef16@syzkaller.appspotmail.com
Closes: https://lore.kernel.org/netdev/6920855a.a70a0220.2ea503.0058.GAE@google.com/T/#u
Signed-off-by: Eric Dumazet <edumazet@google.com>
Reviewed-by: Jamal Hadi Salim <jhs@mojatatu.com>
Link: https://patch.msgid.link/20251121154100.1616228-1-edumazet@google.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>

authored by

Eric Dumazet and committed by
Jakub Kicinski
4fe5a00e 8a4dfa8f

+17 -3
+2
include/net/pkt_cls.h
··· 536 536 case TCF_LAYER_NETWORK: 537 537 return skb_network_header(skb); 538 538 case TCF_LAYER_TRANSPORT: 539 + if (!skb_transport_header_was_set(skb)) 540 + break; 539 541 return skb_transport_header(skb); 540 542 } 541 543
+4 -1
net/sched/em_cmp.c
··· 22 22 struct tcf_pkt_info *info) 23 23 { 24 24 struct tcf_em_cmp *cmp = (struct tcf_em_cmp *) em->data; 25 - unsigned char *ptr = tcf_get_base_ptr(skb, cmp->layer) + cmp->off; 25 + unsigned char *ptr = tcf_get_base_ptr(skb, cmp->layer); 26 26 u32 val = 0; 27 27 28 + if (!ptr) 29 + return 0; 30 + ptr += cmp->off; 28 31 if (!tcf_valid_offset(skb, ptr, cmp->align)) 29 32 return 0; 30 33
+2
net/sched/em_nbyte.c
··· 42 42 struct nbyte_data *nbyte = (struct nbyte_data *) em->data; 43 43 unsigned char *ptr = tcf_get_base_ptr(skb, nbyte->hdr.layer); 44 44 45 + if (!ptr) 46 + return 0; 45 47 ptr += nbyte->hdr.off; 46 48 47 49 if (!tcf_valid_offset(skb, ptr, nbyte->hdr.len))
+9 -2
net/sched/em_text.c
··· 29 29 struct tcf_pkt_info *info) 30 30 { 31 31 struct text_match *tm = EM_TEXT_PRIV(m); 32 + unsigned char *ptr; 32 33 int from, to; 33 34 34 - from = tcf_get_base_ptr(skb, tm->from_layer) - skb->data; 35 + ptr = tcf_get_base_ptr(skb, tm->from_layer); 36 + if (!ptr) 37 + return 0; 38 + from = ptr - skb->data; 35 39 from += tm->from_offset; 36 40 37 - to = tcf_get_base_ptr(skb, tm->to_layer) - skb->data; 41 + ptr = tcf_get_base_ptr(skb, tm->to_layer); 42 + if (!ptr) 43 + return 0; 44 + to = ptr - skb->data; 38 45 to += tm->to_offset; 39 46 40 47 return skb_find_text(skb, from, to, tm->config) != UINT_MAX;