tcp: fix SELinux/Smack corruptions
Subsystems (3)
cover tcp: fix SELinux/Smack corruptions
James Morris reported kernel stack corruption bug that we tracked back to commit 971f10eca186 ("tcp: better TCP_SKB_CB layout to reduce cache line misses")
First patch needs to be backported to kernels >= 3.18, while second patch needs to be backported to kernels >= 4.9, since this was the time when inet_exact_dif_match appeared.
David Ahern (1):
tcp: use IPCB instead of TCP_SKB_CB in inet_exact_dif_match()
Eric Dumazet (1):
tcp: add tcp_v4_fill_cb()/tcp_v4_restore_cb()
include/net/tcp.h | 3 +-- net/ipv4/tcp_ipv4.c | 59 ++++++++++++++++++++++++++++++++++++----------------- net/ipv6/tcp_ipv6.c | 10 +++++---- 3 files changed, 47 insertions(+), 25 deletions(-)
Signature · 1 line
2.15.0.531.g2ccb3012c9-goog
PATCH 1/2 tcp: add tcp_v4_fill_cb()/tcp_v4_restore_cb()
James Morris reported kernel stack corruption bug [1] while running the SELinux testsuite, and bisected to a recent commit bffa72cf7f9d ("net: sk_buff rbnode reorg")
We believe this commit is fine, but exposes an older bug.
SELinux code runs from tcp_filter() and might send an ICMP, expecting IP options to be found in skb->cb[] using regular IPCB placement.
We need to defer TCP mangling of skb->cb[] after tcp_filter() calls.
This patch adds tcp_v4_fill_cb()/tcp_v4_restore_cb() in a very similar way we added them for IPv6.
[1]
[ 339.806024] SELinux: failure in selinux_parse_skb(), unable to parse packet [ 339.822505] Kernel panic - not syncing: stack-protector: Kernel stack is corrupted in: ffffffff81745af5 [ 339.822505] [ 339.852250] CPU: 4 PID: 3642 Comm: client Not tainted 4.15.0-rc1-test #15 [ 339.868498] Hardware name: LENOVO 10FGS0VA1L/30BC, BIOS FWKT68A 01/19/2017 [ 339.885060] Call Trace: [ 339.896875] <IRQ> [ 339.908103] dump_stack+0x63/0x87 [ 339.920645] panic+0xe8/0x248 [ 339.932668] ? ip_push_pending_frames+0x33/0x40 [ 339.946328] ? icmp_send+0x525/0x530 [ 339.958861] ? kfree_skbmem+0x60/0x70 [ 339.971431] __stack_chk_fail+0x1b/0x20 [ 339.984049] icmp_send+0x525/0x530 [ 339.996205] ? netlbl_skbuff_err+0x36/0x40 [ 340.008997] ? selinux_netlbl_err+0x11/0x20 [ 340.021816] ? selinux_socket_sock_rcv_skb+0x211/0x230 [ 340.035529] ? security_sock_rcv_skb+0x3b/0x50 [ 340.048471] ? sk_filter_trim_cap+0x44/0x1c0 [ 340.061246] ? tcp_v4_inbound_md5_hash+0x69/0x1b0 [ 340.074562] ? tcp_filter+0x2c/0x40 [ 340.086400] ? tcp_v4_rcv+0x820/0xa20 [ 340.098329] ? ip_local_deliver_finish+0x71/0x1a0 [ 340.111279] ? ip_local_deliver+0x6f/0xe0 [ 340.123535] ? ip_rcv_finish+0x3a0/0x3a0 [ 340.135523] ? ip_rcv_finish+0xdb/0x3a0 [ 340.147442] ? ip_rcv+0x27c/0x3c0 [ 340.158668] ? inet_del_offload+0x40/0x40 [ 340.170580] ? __netif_receive_skb_core+0x4ac/0x900 [ 340.183285] ? rcu_accelerate_cbs+0x5b/0x80 [ 340.195282] ? __netif_receive_skb+0x18/0x60 [ 340.207288] ? process_backlog+0x95/0x140 [ 340.218948] ? net_rx_action+0x26c/0x3b0 [ 340.230416] ? __do_softirq+0xc9/0x26a [ 340.241625] ? do_softirq_own_stack+0x2a/0x40 [ 340.253368] </IRQ> [ 340.262673] ? do_softirq+0x50/0x60 [ 340.273450] ? __local_bh_enable_ip+0x57/0x60 [ 340.285045] ? ip_finish_output2+0x175/0x350 [ 340.296403] ? ip_finish_output+0x127/0x1d0 [ 340.307665] ? nf_hook_slow+0x3c/0xb0 [ 340.318230] ? ip_output+0x72/0xe0 [ 340.328524] ? ip_fragment.constprop.54+0x80/0x80 [ 340.340070] ? ip_local_out+0x35/0x40 [ 340.350497] ? ip_queue_xmit+0x15c/0x3f0 [ 340.361060] ? __kmalloc_reserve.isra.40+0x31/0x90 [ 340.372484] ? __skb_clone+0x2e/0x130 [ 340.382633] ? tcp_transmit_skb+0x558/0xa10 [ 340.393262] ? tcp_connect+0x938/0xad0 [ 340.403370] ? ktime_get_with_offset+0x4c/0xb0 [ 340.414206] ? tcp_v4_connect+0x457/0x4e0 [ 340.424471] ? __inet_stream_connect+0xb3/0x300 [ 340.435195] ? inet_stream_connect+0x3b/0x60 [ 340.445607] ? SYSC_connect+0xd9/0x110 [ 340.455455] ? __audit_syscall_entry+0xaf/0x100 [ 340.466112] ? syscall_trace_enter+0x1d0/0x2b0 [ 340.476636] ? __audit_syscall_exit+0x209/0x290 [ 340.487151] ? SyS_connect+0xe/0x10 [ 340.496453] ? do_syscall_64+0x67/0x1b0 [ 340.506078] ? entry_SYSCALL64_slow_path+0x25/0x25
--- net/ipv4/tcp_ipv4.c | 59 ++++++++++++++++++++++++++++++++++++----------------- net/ipv6/tcp_ipv6.c | 10 +++++---- 2 files changed, 46 insertions(+), 23 deletions(-)
net/ipv4/tcp_ipv4.c +40 -19
@@ -1591,6 +1591,34 @@ int tcp_filter(struct sock *sk, struct sk_buff *skb) }EXPORT_SYMBOL (tcp_filter); +static void tcp_v4_restore_cb (struct sk_buff *skb) +{ +memmove (IPCB (skb), &TCP_SKB_CB (skb)->header.h4, +sizeof (struct inet_skb_parm )); +} + +static void tcp_v4_fill_cb (struct sk_buff *skb,const struct iphdr *iph, +const struct tcphdr *th) +{ +/* This is tricky : We move IPCB at its correct location into TCP_SKB_CB() +* barrier() makes sure compiler wont play fool^Waliasing games. +*/ +memmove (&TCP_SKB_CB (skb)->header.h4,IPCB (skb), +sizeof (struct inet_skb_parm )); +barrier (); + +TCP_SKB_CB (skb)->seq =ntohl (th->seq); +TCP_SKB_CB (skb)->end_seq = (TCP_SKB_CB (skb)->seq + th->syn + th->fin + + skb->len - th->doff *4 ); +TCP_SKB_CB (skb)->ack_seq =ntohl (th->ack_seq); +TCP_SKB_CB (skb)->tcp_flags =tcp_flag_byte (th); +TCP_SKB_CB (skb)->tcp_tw_isn =0 ; +TCP_SKB_CB (skb)->ip_dsfield =ipv4_get_dsfield (iph); +TCP_SKB_CB (skb)->sacked =0 ; +TCP_SKB_CB (skb)->has_rxtstamp = + skb->tstamp ||skb_hwtstamps (skb)->hwtstamp; +} +/* * From tcp_input.c */
@@ -1631,24 +1659,6 @@ int tcp_v4_rcv(struct sk_buff *skb) th = (const struct tcphdr *)skb->data; iph =ip_hdr (skb); -/* This is tricky : We move IPCB at its correct location into TCP_SKB_CB() -* barrier() makes sure compiler wont play fool^Waliasing games. -*/ -memmove (&TCP_SKB_CB (skb)->header.h4,IPCB (skb), -sizeof (struct inet_skb_parm )); -barrier (); - -TCP_SKB_CB (skb)->seq =ntohl (th->seq); -TCP_SKB_CB (skb)->end_seq = (TCP_SKB_CB (skb)->seq + th->syn + th->fin + - skb->len - th->doff *4 ); -TCP_SKB_CB (skb)->ack_seq =ntohl (th->ack_seq); -TCP_SKB_CB (skb)->tcp_flags =tcp_flag_byte (th); -TCP_SKB_CB (skb)->tcp_tw_isn =0 ; -TCP_SKB_CB (skb)->ip_dsfield =ipv4_get_dsfield (iph); -TCP_SKB_CB (skb)->sacked =0 ; -TCP_SKB_CB (skb)->has_rxtstamp = - skb->tstamp ||skb_hwtstamps (skb)->hwtstamp; - lookup: sk =__inet_lookup_skb (&tcp_hashinfo, skb,__tcp_hdrlen (th), th->source, th->dest, sdif, &refcounted);
@@ -1679,14 +1689,19 @@ int tcp_v4_rcv(struct sk_buff *skb)sock_hold (sk); refcounted = true; nsk =NULL ; -if (!tcp_filter (sk, skb)) +if (!tcp_filter (sk, skb)) { + th = (const struct tcphdr *)skb->data; + iph =ip_hdr (skb); +tcp_v4_fill_cb (skb, iph, th); nsk =tcp_check_req (sk, skb, req, false); + }if (!nsk) {reqsk_put (req); goto discard_and_relse; }if (nsk == sk) {reqsk_put (req); +tcp_v4_restore_cb (skb); }else if (tcp_child_process (sk, nsk, skb)) {tcp_v4_send_reset (nsk, skb); goto discard_and_relse;
@@ -1712,6 +1727,7 @@ int tcp_v4_rcv(struct sk_buff *skb) goto discard_and_relse; th = (const struct tcphdr *)skb->data; iph =ip_hdr (skb); +tcp_v4_fill_cb (skb, iph, th); skb->dev =NULL ;
@@ -1742,6 +1758,8 @@ int tcp_v4_rcv(struct sk_buff *skb)if (!xfrm4_policy_check (NULL ,XFRM_POLICY_IN , skb)) goto discard_it; +tcp_v4_fill_cb (skb, iph, th); +if (tcp_checksum_complete (skb)) { csum_error:__TCP_INC_STATS (net,TCP_MIB_CSUMERRORS );
@@ -1768,6 +1786,8 @@ int tcp_v4_rcv(struct sk_buff *skb) goto discard_it; } +tcp_v4_fill_cb (skb, iph, th); +if (tcp_checksum_complete (skb)) {inet_twsk_put (inet_twsk (sk)); goto csum_error;
@@ -1784,6 +1804,7 @@ int tcp_v4_rcv(struct sk_buff *skb)if (sk2) {inet_twsk_deschedule_put (inet_twsk (sk)); sk = sk2; +tcp_v4_restore_cb (skb); refcounted = false; goto process; }
net/ipv6/tcp_ipv6.c +6 -4
@@ -1454,7 +1454,6 @@ static int tcp_v6_rcv(struct sk_buff *skb)struct sock *nsk; sk = req->rsk_listener; -tcp_v6_fill_cb (skb, hdr, th);if (tcp_v6_inbound_md5_hash (sk, skb)) {sk_drops_add (sk, skb);reqsk_put (req);
@@ -1467,8 +1466,12 @@ static int tcp_v6_rcv(struct sk_buff *skb)sock_hold (sk); refcounted = true; nsk =NULL ; -if (!tcp_filter (sk, skb)) +if (!tcp_filter (sk, skb)) { + th = (const struct tcphdr *)skb->data; + hdr =ipv6_hdr (skb); +tcp_v6_fill_cb (skb, hdr, th); nsk =tcp_check_req (sk, skb, req, false); + }if (!nsk) {reqsk_put (req); goto discard_and_relse;
@@ -1492,8 +1495,6 @@ static int tcp_v6_rcv(struct sk_buff *skb)if (!xfrm6_policy_check (sk,XFRM_POLICY_IN , skb)) goto discard_and_relse; -tcp_v6_fill_cb (skb, hdr, th); -if (tcp_v6_inbound_md5_hash (sk, skb)) goto discard_and_relse;
@@ -1501,6 +1502,7 @@ static int tcp_v6_rcv(struct sk_buff *skb) goto discard_and_relse; th = (const struct tcphdr *)skb->data; hdr =ipv6_hdr (skb); +tcp_v6_fill_cb (skb, hdr, th); skb->dev =NULL ;
Signature · 1 line
2.15.0.531.g2ccb3012c9-goog
PATCH 2/2 tcp: use IPCB instead of TCP_SKB_CB in inet_exact_dif_match()
In reply to Eric DumazetFrom: David Ahern <[email protected]>
After this fix : ("tcp: add tcp_v4_fill_cb()/tcp_v4_restore_cb()"), socket lookups happen while skb->cb[] has not been mangled yet by TCP.
--- include/net/tcp.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
include/net/tcp.h +1 -2
@@ -844,12 +844,11 @@ static inline int tcp_v6_sdif(const struct sk_buff *skb) }#endif -/* TCP_SKB_CB reference means this can not be used from early demux */ static inline bool inet_exact_dif_match (struct net *net,struct sk_buff *skb) {#if IS_ENABLED (CONFIG_NET_L3_MASTER_DEV )if (!net->ipv4.sysctl_tcp_l3mdev_accept && - skb &&ipv4_l3mdev_skb (TCP_SKB_CB (skb)->header.h4.flags)) + skb &&ipv4_l3mdev_skb (IPCB (skb)->flags))return true;#endif return false;
Signature · 1 line
2.15.0.531.g2ccb3012c9-goog
David Miller
maintainerFrom: Eric Dumazet <[email protected]>
Date: Fri, 1 Dec 2017 15:08:11 -0800
Quoted 7 lines
James Morris reported kernel stack corruption bug that we tracked back to commit
971f10eca186("tcp: betterTCP_SKB_CBlayout to reduce cache line misses")First patch needs to be backported to kernels >= 3.18, while second patch needs to be backported to kernels >= 4.9, since this was the time when
inet_exact_dif_matchappeared.
I don't know why but patch #2 never made it into patchwork which makes it really difficult for me to queue it up for -stable properly and reliably.
Can you please repost this series? Hopefully it will work.
If it doesn't work we'll ask the patchwork folks to take a look.
Thanks!
Quoted 16 lines · David Miller
From: Eric Dumazet <[email protected]>
Date: Fri, 1 Dec 2017 15:08:11 -0800Quoted 7 lines
James Morris reported kernel stack corruption bug that we tracked back to commit
971f10eca186("tcp: betterTCP_SKB_CBlayout to reduce cache line misses")
First patch needs to be backported to kernels >= 3.18, while second patch needs to be backported to kernels >= 4.9, since this was the time wheninet_exact_dif_matchappeared.I don't know why but patch #2 never made it into patchwork which makes
it really difficult for me to queue it up for -stable properly and reliably.
Quoted 5 lines
Can you please repost this series? Hopefully it will work.
If it doesn't work we'll ask the patchwork folks to take a look.
Thanks!
Sure, I have rebased and resent.
Thanks.