Skip to content

Commit 4921fdd

Browse files
Paolo Abenibwhacks
Paolo Abeni
authored andcommitted
l2tp: fix races with ipv4-mapped ipv6 addresses
commit b954f94 upstream. The l2tp_tunnel_create() function checks for v4mapped ipv6 sockets and cache that flag, so that l2tp core code can reusing it at xmit time. If the socket is provided by the userspace, the connection status of the tunnel sockets can change between the tunnel creation and the xmit call, so that syzbot is able to trigger the following splat: BUG: KASAN: use-after-free in ip6_dst_idev include/net/ip6_fib.h:192 [inline] BUG: KASAN: use-after-free in ip6_xmit+0x1f76/0x2260 net/ipv6/ip6_output.c:264 Read of size 8 at addr ffff8801bd949318 by task syz-executor4/23448 CPU: 0 PID: 23448 Comm: syz-executor4 Not tainted 4.16.0-rc4+ #65 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011 Call Trace: __dump_stack lib/dump_stack.c:17 [inline] dump_stack+0x194/0x24d lib/dump_stack.c:53 print_address_description+0x73/0x250 mm/kasan/report.c:256 kasan_report_error mm/kasan/report.c:354 [inline] kasan_report+0x23c/0x360 mm/kasan/report.c:412 __asan_report_load8_noabort+0x14/0x20 mm/kasan/report.c:433 ip6_dst_idev include/net/ip6_fib.h:192 [inline] ip6_xmit+0x1f76/0x2260 net/ipv6/ip6_output.c:264 inet6_csk_xmit+0x2fc/0x580 net/ipv6/inet6_connection_sock.c:139 l2tp_xmit_core net/l2tp/l2tp_core.c:1053 [inline] l2tp_xmit_skb+0x105f/0x1410 net/l2tp/l2tp_core.c:1148 pppol2tp_sendmsg+0x470/0x670 net/l2tp/l2tp_ppp.c:341 sock_sendmsg_nosec net/socket.c:630 [inline] sock_sendmsg+0xca/0x110 net/socket.c:640 ___sys_sendmsg+0x767/0x8b0 net/socket.c:2046 __sys_sendmsg+0xe5/0x210 net/socket.c:2080 SYSC_sendmsg net/socket.c:2091 [inline] SyS_sendmsg+0x2d/0x50 net/socket.c:2087 do_syscall_64+0x281/0x940 arch/x86/entry/common.c:287 entry_SYSCALL_64_after_hwframe+0x42/0xb7 RIP: 0033:0x453e69 RSP: 002b:00007f819593cc68 EFLAGS: 00000246 ORIG_RAX: 000000000000002e RAX: ffffffffffffffda RBX: 00007f819593d6d4 RCX: 0000000000453e69 RDX: 0000000000000081 RSI: 000000002037ffc8 RDI: 0000000000000004 RBP: 000000000072bea0 R08: 0000000000000000 R09: 0000000000000000 R10: 0000000000000000 R11: 0000000000000246 R12: 00000000ffffffff R13: 00000000000004c3 R14: 00000000006f72e8 R15: 0000000000000000 This change addresses the issues: * explicitly checking for TCP_ESTABLISHED for user space provided sockets * dropping the v4mapped flag usage - it can become outdated - and explicitly invoking ipv6_addr_v4mapped() instead The issue is apparently there since ancient times. v1 -> v2: (many thanks to Guillaume) - with csum issue introduced in v1 - replace pr_err with pr_debug - fix build issue with IPV6 disabled - move l2tp_sk_is_v4mapped in l2tp_core.c v2 -> v3: - don't update inet_daddr for v4mapped address, unneeded - drop rendundant check at creation time Reported-and-tested-by: [email protected] Fixes: 3557baa ("[L2TP]: PPP over L2TP driver core") Signed-off-by: Paolo Abeni <[email protected]> Signed-off-by: David S. Miller <[email protected]> [bwh: Backported to 3.16: Change an additional test of tunnel->v4mapped to use l2tp_sk_is_v6()] Signed-off-by: Ben Hutchings <[email protected]>
1 parent 491de84 commit 4921fdd

File tree

2 files changed

+19
-24
lines changed

2 files changed

+19
-24
lines changed

net/l2tp/l2tp_core.c

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,13 @@ struct l2tp_net {
110110
spinlock_t l2tp_session_hlist_lock;
111111
};
112112

113+
#if IS_ENABLED(CONFIG_IPV6)
114+
static bool l2tp_sk_is_v6(struct sock *sk)
115+
{
116+
return sk->sk_family == PF_INET6 &&
117+
!ipv6_addr_v4mapped(&sk->sk_v6_daddr);
118+
}
119+
#endif
113120

114121
static inline struct l2tp_tunnel *l2tp_tunnel(struct sock *sk)
115122
{
@@ -1134,7 +1141,7 @@ static int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb,
11341141
/* Queue the packet to IP for output */
11351142
skb->ignore_df = 1;
11361143
#if IS_ENABLED(CONFIG_IPV6)
1137-
if (tunnel->sock->sk_family == PF_INET6 && !tunnel->v4mapped)
1144+
if (l2tp_sk_is_v6(tunnel->sock))
11381145
error = inet6_csk_xmit(tunnel->sock, skb, NULL);
11391146
else
11401147
#endif
@@ -1197,6 +1204,15 @@ int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len
11971204
goto out_unlock;
11981205
}
11991206

1207+
/* The user-space may change the connection status for the user-space
1208+
* provided socket at run time: we must check it under the socket lock
1209+
*/
1210+
if (tunnel->fd >= 0 && sk->sk_state != TCP_ESTABLISHED) {
1211+
kfree_skb(skb);
1212+
ret = NET_XMIT_DROP;
1213+
goto out_unlock;
1214+
}
1215+
12001216
/* Get routing info from the tunnel socket */
12011217
skb_dst_drop(skb);
12021218
skb_dst_set(skb, dst_clone(__sk_dst_check(sk, 0)));
@@ -1216,7 +1232,7 @@ int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len
12161232

12171233
/* Calculate UDP checksum if configured to do so */
12181234
#if IS_ENABLED(CONFIG_IPV6)
1219-
if (sk->sk_family == PF_INET6 && !tunnel->v4mapped)
1235+
if (l2tp_sk_is_v6(sk))
12201236
udp6_set_csum(udp_get_no_check6_tx(sk),
12211237
skb, &inet6_sk(sk)->saddr,
12221238
&sk->sk_v6_daddr, udp_len);
@@ -1635,24 +1651,6 @@ int l2tp_tunnel_create(struct net *net, int fd, int version, u32 tunnel_id, u32
16351651
if (cfg != NULL)
16361652
tunnel->debug = cfg->debug;
16371653

1638-
#if IS_ENABLED(CONFIG_IPV6)
1639-
if (sk->sk_family == PF_INET6) {
1640-
struct ipv6_pinfo *np = inet6_sk(sk);
1641-
1642-
if (ipv6_addr_v4mapped(&np->saddr) &&
1643-
ipv6_addr_v4mapped(&sk->sk_v6_daddr)) {
1644-
struct inet_sock *inet = inet_sk(sk);
1645-
1646-
tunnel->v4mapped = true;
1647-
inet->inet_saddr = np->saddr.s6_addr32[3];
1648-
inet->inet_rcv_saddr = sk->sk_v6_rcv_saddr.s6_addr32[3];
1649-
inet->inet_daddr = sk->sk_v6_daddr.s6_addr32[3];
1650-
} else {
1651-
tunnel->v4mapped = false;
1652-
}
1653-
}
1654-
#endif
1655-
16561654
/* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
16571655
tunnel->encap = encap;
16581656
if (encap == L2TP_ENCAPTYPE_UDP) {
@@ -1661,7 +1659,7 @@ int l2tp_tunnel_create(struct net *net, int fd, int version, u32 tunnel_id, u32
16611659
udp_sk(sk)->encap_rcv = l2tp_udp_encap_recv;
16621660
udp_sk(sk)->encap_destroy = l2tp_udp_encap_destroy;
16631661
#if IS_ENABLED(CONFIG_IPV6)
1664-
if (sk->sk_family == PF_INET6 && !tunnel->v4mapped)
1662+
if (l2tp_sk_is_v6(sk))
16651663
udpv6_encap_enable();
16661664
else
16671665
#endif

net/l2tp/l2tp_core.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,6 @@ struct l2tp_tunnel {
203203
struct sock *sock; /* Parent socket */
204204
int fd; /* Parent fd, if tunnel socket
205205
* was created by userspace */
206-
#if IS_ENABLED(CONFIG_IPV6)
207-
bool v4mapped;
208-
#endif
209206

210207
struct work_struct del_work;
211208

0 commit comments

Comments
 (0)