ip_tables: iptables: counters copy to user failed while replacing table
======================================================
WARNING: possible circular locking dependency detected
4.14.228-syzkaller #0 Not tainted
------------------------------------------------------
syz-executor.2/8007 is trying to acquire lock:
 (&table[i].mutex){+.+.}, at: [<ffffffff85f5155a>] ip_set_nfnl_put+0x11a/0x310 net/netfilter/ipset/ip_set_core.c:732

but task is already holding lock:
 (&xt[i].mutex){+.+.}, at: [<ffffffff85f06ff8>] xt_find_table_lock+0x38/0x3d0 net/netfilter/x_tables.c:1094

which lock already depends on the new lock.


the existing dependency chain (in reverse order) is:

-> #1 (&xt[i].mutex){+.+.}:
       __mutex_lock_common kernel/locking/mutex.c:756 [inline]
       __mutex_lock+0xc4/0x1310 kernel/locking/mutex.c:893
       match_revfn+0x43/0x210 net/netfilter/x_tables.c:332
       xt_find_revision+0x8d/0x1d0 net/netfilter/x_tables.c:380
       nfnl_compat_get+0x1f7/0x870 net/netfilter/nft_compat.c:678
       nfnetlink_rcv_msg+0x9bb/0xc00 net/netfilter/nfnetlink.c:214
       netlink_rcv_skb+0x125/0x390 net/netlink/af_netlink.c:2433
       nfnetlink_rcv+0x1ab/0x1da0 net/netfilter/nfnetlink.c:515
       netlink_unicast_kernel net/netlink/af_netlink.c:1287 [inline]
       netlink_unicast+0x437/0x610 net/netlink/af_netlink.c:1313
       netlink_sendmsg+0x62e/0xb80 net/netlink/af_netlink.c:1878
       sock_sendmsg_nosec net/socket.c:646 [inline]
       sock_sendmsg+0xb5/0x100 net/socket.c:656
       ___sys_sendmsg+0x6c8/0x800 net/socket.c:2062
       __sys_sendmsg+0xa3/0x120 net/socket.c:2096
       SYSC_sendmsg net/socket.c:2107 [inline]
       SyS_sendmsg+0x27/0x40 net/socket.c:2103
       do_syscall_64+0x1d5/0x640 arch/x86/entry/common.c:292
       entry_SYSCALL_64_after_hwframe+0x46/0xbb

-> #0 (&table[i].mutex){+.+.}:
       lock_acquire+0x170/0x3f0 kernel/locking/lockdep.c:3998
       __mutex_lock_common kernel/locking/mutex.c:756 [inline]
       __mutex_lock+0xc4/0x1310 kernel/locking/mutex.c:893
       ip_set_nfnl_put+0x11a/0x310 net/netfilter/ipset/ip_set_core.c:732
       set_target_v0_destroy+0xd7/0x150 net/netfilter/xt_set.c:323
       cleanup_entry+0x1fd/0x2d0 net/ipv4/netfilter/ip_tables.c:666
       __do_replace+0x38d/0x570 net/ipv4/netfilter/ip_tables.c:1086
       do_replace net/ipv4/netfilter/ip_tables.c:1142 [inline]
       do_ipt_set_ctl+0x256/0x3a0 net/ipv4/netfilter/ip_tables.c:1674
       nf_sockopt net/netfilter/nf_sockopt.c:106 [inline]
       nf_setsockopt+0x5f/0xb0 net/netfilter/nf_sockopt.c:115
       ip_setsockopt net/ipv4/ip_sockglue.c:1255 [inline]
       ip_setsockopt+0x94/0xb0 net/ipv4/ip_sockglue.c:1240
       tcp_setsockopt+0x7b/0xc0 net/ipv4/tcp.c:2828
       SYSC_setsockopt net/socket.c:1865 [inline]
       SyS_setsockopt+0x110/0x1e0 net/socket.c:1844
       do_syscall_64+0x1d5/0x640 arch/x86/entry/common.c:292
       entry_SYSCALL_64_after_hwframe+0x46/0xbb

other info that might help us debug this:

 Possible unsafe locking scenario:

       CPU0                    CPU1
       ----                    ----
  lock(&xt[i].mutex);
                               lock(&table[i].mutex);
                               lock(&xt[i].mutex);
  lock(&table[i].mutex);

 *** DEADLOCK ***

1 lock held by syz-executor.2/8007:
 #0:  (&xt[i].mutex){+.+.}, at: [<ffffffff85f06ff8>] xt_find_table_lock+0x38/0x3d0 net/netfilter/x_tables.c:1094

stack backtrace:
CPU: 0 PID: 8007 Comm: syz-executor.2 Not tainted 4.14.228-syzkaller #0
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+0x1b2/0x281 lib/dump_stack.c:58
 print_circular_bug.constprop.0.cold+0x2d7/0x41e kernel/locking/lockdep.c:1258
 check_prev_add kernel/locking/lockdep.c:1905 [inline]
 check_prevs_add kernel/locking/lockdep.c:2022 [inline]
 validate_chain kernel/locking/lockdep.c:2464 [inline]
 __lock_acquire+0x2e0e/0x3f20 kernel/locking/lockdep.c:3491
 lock_acquire+0x170/0x3f0 kernel/locking/lockdep.c:3998
 __mutex_lock_common kernel/locking/mutex.c:756 [inline]
 __mutex_lock+0xc4/0x1310 kernel/locking/mutex.c:893
 ip_set_nfnl_put+0x11a/0x310 net/netfilter/ipset/ip_set_core.c:732
 set_target_v0_destroy+0xd7/0x150 net/netfilter/xt_set.c:323
 cleanup_entry+0x1fd/0x2d0 net/ipv4/netfilter/ip_tables.c:666
 __do_replace+0x38d/0x570 net/ipv4/netfilter/ip_tables.c:1086
 do_replace net/ipv4/netfilter/ip_tables.c:1142 [inline]
 do_ipt_set_ctl+0x256/0x3a0 net/ipv4/netfilter/ip_tables.c:1674
 nf_sockopt net/netfilter/nf_sockopt.c:106 [inline]
 nf_setsockopt+0x5f/0xb0 net/netfilter/nf_sockopt.c:115
 ip_setsockopt net/ipv4/ip_sockglue.c:1255 [inline]
 ip_setsockopt+0x94/0xb0 net/ipv4/ip_sockglue.c:1240
 tcp_setsockopt+0x7b/0xc0 net/ipv4/tcp.c:2828
 SYSC_setsockopt net/socket.c:1865 [inline]
 SyS_setsockopt+0x110/0x1e0 net/socket.c:1844
 do_syscall_64+0x1d5/0x640 arch/x86/entry/common.c:292
 entry_SYSCALL_64_after_hwframe+0x46/0xbb
RIP: 0033:0x467a9a
RSP: 002b:00007fff6128ea38 EFLAGS: 00000202 ORIG_RAX: 0000000000000036
RAX: ffffffffffffffda RBX: 0000000000000000 RCX: 0000000000467a9a
RDX: 0000000000000040 RSI: 0000000000000000 RDI: 0000000000000003
RBP: 00007fff6128ea60 R08: 0000000000000240 R09: 00007fff6128eaf0
R10: 00000000005451a0 R11: 0000000000000202 R12: 00007fff6128eac0
R13: 0000000000000003 R14: 00007fff6128ea5c R15: 0000000000545140
x_tables: ip_tables: osf match: used from hooks INPUT/POSTROUTING, but only valid from PREROUTING/INPUT/FORWARD
xt_dscp: dscp 40 out of range
xt_connbytes: cannot load conntrack support for proto=2
xt_connbytes: Forcing CT accounting to be enabled
xt_SECMARK: target only valid in the 'mangle' or 'security' tables, not 'filter'.
Cannot find set identified by id 0 to match
Cannot find set identified by id 0 to match
xt_HL: TTL: invalid or unknown mode 3
x_tables: ip_tables: rpfilter match: used from hooks OUTPUT, but only valid from PREROUTING
--map-set only usable from mangle table
xt_TPROXY: Can be used only in combination with either -p tcp or -p udp
xt_TPROXY: Can be used only in combination with either -p tcp or -p udp
x_tables: ip_tables: osf match: used from hooks PREROUTING/OUTPUT/POSTROUTING, but only valid from PREROUTING/INPUT/FORWARD
Cannot find set identified by id 0 to match
x_tables: ip_tables: TCPMSS target: only valid for protocol 6
x_tables: ip_tables: SNAT target: used from hooks OUTPUT/POSTROUTING, but only usable from INPUT/POSTROUTING