Skip to content

Commit 21b59ba

Browse files
Paolo Abenibwhacks
authored andcommitted
netfilter: on sockopt() acquire sock lock only in the required scope
commit 3f34cfa upstream. Syzbot reported several deadlocks in the netfilter area caused by rtnl lock and socket lock being acquired with a different order on different code paths, leading to backtraces like the following one: ====================================================== WARNING: possible circular locking dependency detected 4.15.0-rc9+ torvalds#212 Not tainted ------------------------------------------------------ syzkaller041579/3682 is trying to acquire lock: (sk_lock-AF_INET6){+.+.}, at: [<000000008775e4dd>] lock_sock include/net/sock.h:1463 [inline] (sk_lock-AF_INET6){+.+.}, at: [<000000008775e4dd>] do_ipv6_setsockopt.isra.8+0x3c5/0x39d0 net/ipv6/ipv6_sockglue.c:167 but task is already holding lock: (rtnl_mutex){+.+.}, at: [<000000004342eaa9>] rtnl_lock+0x17/0x20 net/core/rtnetlink.c:74 which lock already depends on the new lock. the existing dependency chain (in reverse order) is: -> #1 (rtnl_mutex){+.+.}: __mutex_lock_common kernel/locking/mutex.c:756 [inline] __mutex_lock+0x16f/0x1a80 kernel/locking/mutex.c:893 mutex_lock_nested+0x16/0x20 kernel/locking/mutex.c:908 rtnl_lock+0x17/0x20 net/core/rtnetlink.c:74 register_netdevice_notifier+0xad/0x860 net/core/dev.c:1607 tee_tg_check+0x1a0/0x280 net/netfilter/xt_TEE.c:106 xt_check_target+0x22c/0x7d0 net/netfilter/x_tables.c:845 check_target net/ipv6/netfilter/ip6_tables.c:538 [inline] find_check_entry.isra.7+0x935/0xcf0 net/ipv6/netfilter/ip6_tables.c:580 translate_table+0xf52/0x1690 net/ipv6/netfilter/ip6_tables.c:749 do_replace net/ipv6/netfilter/ip6_tables.c:1165 [inline] do_ip6t_set_ctl+0x370/0x5f0 net/ipv6/netfilter/ip6_tables.c:1691 nf_sockopt net/netfilter/nf_sockopt.c:106 [inline] nf_setsockopt+0x67/0xc0 net/netfilter/nf_sockopt.c:115 ipv6_setsockopt+0x115/0x150 net/ipv6/ipv6_sockglue.c:928 udpv6_setsockopt+0x45/0x80 net/ipv6/udp.c:1422 sock_common_setsockopt+0x95/0xd0 net/core/sock.c:2978 SYSC_setsockopt net/socket.c:1849 [inline] SyS_setsockopt+0x189/0x360 net/socket.c:1828 entry_SYSCALL_64_fastpath+0x29/0xa0 -> #0 (sk_lock-AF_INET6){+.+.}: lock_acquire+0x1d5/0x580 kernel/locking/lockdep.c:3914 lock_sock_nested+0xc2/0x110 net/core/sock.c:2780 lock_sock include/net/sock.h:1463 [inline] do_ipv6_setsockopt.isra.8+0x3c5/0x39d0 net/ipv6/ipv6_sockglue.c:167 ipv6_setsockopt+0xd7/0x150 net/ipv6/ipv6_sockglue.c:922 udpv6_setsockopt+0x45/0x80 net/ipv6/udp.c:1422 sock_common_setsockopt+0x95/0xd0 net/core/sock.c:2978 SYSC_setsockopt net/socket.c:1849 [inline] SyS_setsockopt+0x189/0x360 net/socket.c:1828 entry_SYSCALL_64_fastpath+0x29/0xa0 other info that might help us debug this: Possible unsafe locking scenario: CPU0 CPU1 ---- ---- lock(rtnl_mutex); lock(sk_lock-AF_INET6); lock(rtnl_mutex); lock(sk_lock-AF_INET6); *** DEADLOCK *** 1 lock held by syzkaller041579/3682: #0: (rtnl_mutex){+.+.}, at: [<000000004342eaa9>] rtnl_lock+0x17/0x20 net/core/rtnetlink.c:74 The problem, as Florian noted, is that nf_setsockopt() is always called with the socket held, even if the lock itself is required only for very tight scopes and only for some operation. This patch addresses the issues moving the lock_sock() call only where really needed, namely in ipv*_getorigdst(), so that nf_setsockopt() does not need anymore to acquire both locks. Fixes: 22265a5 ("netfilter: xt_TEE: resolve oif using netdevice notifiers") Reported-by: [email protected] Suggested-by: Florian Westphal <[email protected]> Signed-off-by: Paolo Abeni <[email protected]> Signed-off-by: Pablo Neira Ayuso <[email protected]> Signed-off-by: Ben Hutchings <[email protected]>
1 parent b9d62c1 commit 21b59ba

File tree

4 files changed

+26
-29
lines changed

4 files changed

+26
-29
lines changed

net/ipv4/ip_sockglue.c

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,11 +1106,8 @@ int ip_setsockopt(struct sock *sk, int level,
11061106
if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
11071107
optname != IP_IPSEC_POLICY &&
11081108
optname != IP_XFRM_POLICY &&
1109-
!ip_mroute_opt(optname)) {
1110-
lock_sock(sk);
1109+
!ip_mroute_opt(optname))
11111110
err = nf_setsockopt(sk, PF_INET, optname, optval, optlen);
1112-
release_sock(sk);
1113-
}
11141111
#endif
11151112
return err;
11161113
}
@@ -1135,12 +1132,9 @@ int compat_ip_setsockopt(struct sock *sk, int level, int optname,
11351132
if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
11361133
optname != IP_IPSEC_POLICY &&
11371134
optname != IP_XFRM_POLICY &&
1138-
!ip_mroute_opt(optname)) {
1139-
lock_sock(sk);
1140-
err = compat_nf_setsockopt(sk, PF_INET, optname,
1141-
optval, optlen);
1142-
release_sock(sk);
1143-
}
1135+
!ip_mroute_opt(optname))
1136+
err = compat_nf_setsockopt(sk, PF_INET, optname, optval,
1137+
optlen);
11441138
#endif
11451139
return err;
11461140
}

net/ipv4/netfilter/nf_conntrack_l3proto_ipv4.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,15 +269,19 @@ getorigdst(struct sock *sk, int optval, void __user *user, int *len)
269269
struct nf_conntrack_tuple tuple;
270270

271271
memset(&tuple, 0, sizeof(tuple));
272+
273+
lock_sock(sk);
272274
tuple.src.u3.ip = inet->inet_rcv_saddr;
273275
tuple.src.u.tcp.port = inet->inet_sport;
274276
tuple.dst.u3.ip = inet->inet_daddr;
275277
tuple.dst.u.tcp.port = inet->inet_dport;
276278
tuple.src.l3num = PF_INET;
277279
tuple.dst.protonum = sk->sk_protocol;
280+
release_sock(sk);
278281

279282
/* We only do TCP and SCTP at the moment: is there a better way? */
280-
if (sk->sk_protocol != IPPROTO_TCP && sk->sk_protocol != IPPROTO_SCTP) {
283+
if (tuple.dst.protonum != IPPROTO_TCP &&
284+
tuple.dst.protonum != IPPROTO_SCTP) {
281285
pr_debug("SO_ORIGINAL_DST: Not a TCP/SCTP socket\n");
282286
return -ENOPROTOOPT;
283287
}

net/ipv6/ipv6_sockglue.c

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -871,12 +871,8 @@ int ipv6_setsockopt(struct sock *sk, int level, int optname,
871871
#ifdef CONFIG_NETFILTER
872872
/* we need to exclude all possible ENOPROTOOPTs except default case */
873873
if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
874-
optname != IPV6_XFRM_POLICY) {
875-
lock_sock(sk);
876-
err = nf_setsockopt(sk, PF_INET6, optname, optval,
877-
optlen);
878-
release_sock(sk);
879-
}
874+
optname != IPV6_XFRM_POLICY)
875+
err = nf_setsockopt(sk, PF_INET6, optname, optval, optlen);
880876
#endif
881877
return err;
882878
}
@@ -907,12 +903,9 @@ int compat_ipv6_setsockopt(struct sock *sk, int level, int optname,
907903
#ifdef CONFIG_NETFILTER
908904
/* we need to exclude all possible ENOPROTOOPTs except default case */
909905
if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
910-
optname != IPV6_XFRM_POLICY) {
911-
lock_sock(sk);
912-
err = compat_nf_setsockopt(sk, PF_INET6, optname,
913-
optval, optlen);
914-
release_sock(sk);
915-
}
906+
optname != IPV6_XFRM_POLICY)
907+
err = compat_nf_setsockopt(sk, PF_INET6, optname, optval,
908+
optlen);
916909
#endif
917910
return err;
918911
}

net/ipv6/netfilter/nf_conntrack_l3proto_ipv6.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -240,20 +240,27 @@ static struct nf_hook_ops ipv6_conntrack_ops[] __read_mostly = {
240240
static int
241241
ipv6_getorigdst(struct sock *sk, int optval, void __user *user, int *len)
242242
{
243-
const struct inet_sock *inet = inet_sk(sk);
243+
struct nf_conntrack_tuple tuple = { .src.l3num = NFPROTO_IPV6 };
244244
const struct ipv6_pinfo *inet6 = inet6_sk(sk);
245+
const struct inet_sock *inet = inet_sk(sk);
245246
const struct nf_conntrack_tuple_hash *h;
246247
struct sockaddr_in6 sin6;
247-
struct nf_conntrack_tuple tuple = { .src.l3num = NFPROTO_IPV6 };
248248
struct nf_conn *ct;
249+
__be32 flow_label;
250+
int bound_dev_if;
249251

252+
lock_sock(sk);
250253
tuple.src.u3.in6 = sk->sk_v6_rcv_saddr;
251254
tuple.src.u.tcp.port = inet->inet_sport;
252255
tuple.dst.u3.in6 = sk->sk_v6_daddr;
253256
tuple.dst.u.tcp.port = inet->inet_dport;
254257
tuple.dst.protonum = sk->sk_protocol;
258+
bound_dev_if = sk->sk_bound_dev_if;
259+
flow_label = inet6->flow_label;
260+
release_sock(sk);
255261

256-
if (sk->sk_protocol != IPPROTO_TCP && sk->sk_protocol != IPPROTO_SCTP)
262+
if (tuple.dst.protonum != IPPROTO_TCP &&
263+
tuple.dst.protonum != IPPROTO_SCTP)
257264
return -ENOPROTOOPT;
258265

259266
if (*len < 0 || (unsigned int) *len < sizeof(sin6))
@@ -271,14 +278,13 @@ ipv6_getorigdst(struct sock *sk, int optval, void __user *user, int *len)
271278

272279
sin6.sin6_family = AF_INET6;
273280
sin6.sin6_port = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u.tcp.port;
274-
sin6.sin6_flowinfo = inet6->flow_label & IPV6_FLOWINFO_MASK;
281+
sin6.sin6_flowinfo = flow_label & IPV6_FLOWINFO_MASK;
275282
memcpy(&sin6.sin6_addr,
276283
&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u3.in6,
277284
sizeof(sin6.sin6_addr));
278285

279286
nf_ct_put(ct);
280-
sin6.sin6_scope_id = ipv6_iface_scope_id(&sin6.sin6_addr,
281-
sk->sk_bound_dev_if);
287+
sin6.sin6_scope_id = ipv6_iface_scope_id(&sin6.sin6_addr, bound_dev_if);
282288
return copy_to_user(user, &sin6, sizeof(sin6)) ? -EFAULT : 0;
283289
}
284290

0 commit comments

Comments
 (0)