Netfilter fixes for net
Subsystems (4)
Pablo Neira Ayuso
maintainercover Netfilter fixes for net
Hi David,
The following patchset contains Netfilter fixes for your net tree, they are:
1) Fix OOM that syskaller triggers with ipt_replace.size = -1 and IPT_SO_SET_REPLACE socket option, from Dmitry Vyukov.
2) Check for too long extension name in xt_request_find_{match|target} that result in out-of-bound reads, from Eric Dumazet.
3) Fix memory exhaustion bug in ipset hash:*net* types when adding ranges that look like x.x.x.x-255.255.255.255, from Jozsef Kadlecsik.
4) Fix pointer leaks to userspace in x_tables, from Dmitry Vyukov.
5) Insufficient sanity checks in clusterip_tg_check(), also from Dmitry.
You can pull these changes from:
git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf.git
Thanks!
P.S: Another batch is following up soon, there are more fixes cooking on
the mailing list.
----------------------------------------------------------------
The following changes since commit d1616f07e8f1a4a490d1791316d4a68906b284aa:
net: fec: free/restore resource in related probe error pathes (2018-01-05 11:19:11 -0500)
are available in the git repository at:
git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf.git HEAD
for you to fetch changes up to 3f34cfae1238848fd53f25e5c8fd59da57901f4b:
netfilter: on sockopt() acquire sock lock only in the required scope (2018-01-31 16:37:47 +0100)
----------------------------------------------------------------
Dmitry Vyukov (3):
netfilter: x_tables: fix int overflow in xt_alloc_table_info()
netfilter: x_tables: fix pointer leaks to userspace
netfilter: ipt_CLUSTERIP: fix out-of-bounds accesses in clusterip_tg_check()
Eric Dumazet (1):
netfilter: x_tables: avoid out-of-bounds reads in xt_request_find_{match|target}Jozsef Kadlecsik (1):
netfilter: ipset: Fix wraparound in hash:*net* types
Paolo Abeni (1):
netfilter: on sockopt() acquire sock lock only in the required scope
net/ipv4/ip_sockglue.c | 14 +++-------- net/ipv4/netfilter/ipt_CLUSTERIP.c | 16 +++++++++--- net/ipv4/netfilter/nf_conntrack_l3proto_ipv4.c | 6 ++++- net/ipv6/ipv6_sockglue.c | 17 ++++--------- net/ipv6/netfilter/nf_conntrack_l3proto_ipv6.c | 18 ++++++++----- net/netfilter/ipset/ip_set_hash_ipportnet.c | 26 +++++++++---------- net/netfilter/ipset/ip_set_hash_net.c | 9 +++---- net/netfilter/ipset/ip_set_hash_netiface.c | 9 +++---- net/netfilter/ipset/ip_set_hash_netnet.c | 28 ++++++++++----------- net/netfilter/ipset/ip_set_hash_netport.c | 19 +++++++------- net/netfilter/ipset/ip_set_hash_netportnet.c | 35 +++++++++++++------------- net/netfilter/x_tables.c | 9 +++++-- net/netfilter/xt_IDLETIMER.c | 1 + net/netfilter/xt_LED.c | 1 + net/netfilter/xt_limit.c | 3 +-- net/netfilter/xt_nfacct.c | 1 + net/netfilter/xt_statistic.c | 1 + 17 files changed, 114 insertions(+), 99 deletions(-)
Pablo Neira Ayuso
maintainerPATCH 1/6 netfilter: x_tables: fix int overflow in xt_alloc_table_info()
From: Dmitry Vyukov <[email protected]>
syzkaller triggered OOM kills by passing ipt_replace.size = -1 to IPT_SO_SET_REPLACE. The root cause is that SMP_ALIGN() in xt_alloc_table_info() causes int overflow and the size check passes when it should not. SMP_ALIGN() is no longer needed leftover.
Remove SMP_ALIGN() call in xt_alloc_table_info().
--- net/netfilter/x_tables.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
net/netfilter/x_tables.c +1 -2
@@ -39,7 +39,6 @@ MODULE_LICENSE("GPL");MODULE_AUTHOR ("Harald Welte <[email protected]>" );MODULE_DESCRIPTION ("{ip,ip6,arp,eb}_tables backend module" ); -#define SMP_ALIGN (x) (((x) + SMP_CACHE_BYTES-1) & ~(SMP_CACHE_BYTES-1))#define XT_PCPU_BLOCK_SIZE 4096struct compat_delta {
@@ -1000,7 +999,7 @@ struct xt_table_info *xt_alloc_table_info(unsigned int size)return NULL ;/* Pedantry: prevent them from hitting BUG() in vmalloc.c --RR */ -if ((SMP_ALIGN (size) >>PAGE_SHIFT ) +2 > totalram_pages) +if ((size >>PAGE_SHIFT ) +2 > totalram_pages)return NULL ; info =kvmalloc (sz,GFP_KERNEL );
Signature · 1 line
2.11.0
Pablo Neira Ayuso
maintainerPATCH 2/6 netfilter: x_tables: avoid out-of-bounds reads in xt_request_find_{match|target}
In reply to Pablo Neira AyusoFrom: Eric Dumazet <[email protected]>
It looks like syzbot found its way into netfilter territory.
Issue here is that @name comes from user space and might not be null terminated.
Out-of-bound reads happen, KASAN is not happy.
v2 added similar fix for xt_request_find_target(), as Florian advised.
--- net/netfilter/x_tables.c | 6 ++++++ 1 file changed, 6 insertions(+)
net/netfilter/x_tables.c +6 -0
@@ -209,6 +209,9 @@ xt_request_find_match(uint8_t nfproto, const char *name, uint8_t revision) {struct xt_match *match; +if (strnlen (name,XT_EXTENSION_MAXNAMELEN ) ==XT_EXTENSION_MAXNAMELEN ) +return ERR_PTR (-EINVAL ); + match =xt_find_match (nfproto, name, revision);if (IS_ERR (match)) {request_module ("%st_%s" , xt_prefix[nfproto], name);
@@ -251,6 +254,9 @@ struct xt_target *xt_request_find_target(u8 af, const char *name, u8 revision) {struct xt_target *target; +if (strnlen (name,XT_EXTENSION_MAXNAMELEN ) ==XT_EXTENSION_MAXNAMELEN ) +return ERR_PTR (-EINVAL ); + target =xt_find_target (af, name, revision);if (IS_ERR (target)) {request_module ("%st_%s" , xt_prefix[af], name);
Signature · 1 line
2.11.0
Pablo Neira Ayuso
maintainerPATCH 3/6 netfilter: ipset: Fix wraparound in hash:*net* types
In reply to Pablo Neira AyusoFrom: Jozsef Kadlecsik <[email protected]>
Fix wraparound bug which could lead to memory exhaustion when adding an x.x.x.x-255.255.255.255 range to any hash:*net* types.
Fixes Netfilter's bugzilla id #1212, reported by Thomas Schwark.
--- net/netfilter/ipset/ip_set_hash_ipportnet.c | 26 ++++++++++----------- net/netfilter/ipset/ip_set_hash_net.c | 9 ++++--- net/netfilter/ipset/ip_set_hash_netiface.c | 9 ++++--- net/netfilter/ipset/ip_set_hash_netnet.c | 28 +++++++++++----------- net/netfilter/ipset/ip_set_hash_netport.c | 19 ++++++++------- net/netfilter/ipset/ip_set_hash_netportnet.c | 35 ++++++++++++++-------------- 6 files changed, 63 insertions(+), 63 deletions(-)
net/netfilter/ipset/ip_set_hash_ipportnet.c +13 -13
@@ -168,7 +168,7 @@ hash_ipportnet4_uadt(struct ip_set *set, struct nlattr *tb[],struct hash_ipportnet4_elem e = { .cidr =HOST_MASK -1 };struct ip_set_ext ext =IP_SET_INIT_UEXT (set);u32 ip =0 , ip_to =0 , p =0 , port, port_to; -u32 ip2_from =0 , ip2_to =0 , ip2_last, ip2; +u32 ip2_from =0 , ip2_to =0 , ip2;bool with_ports = false;u8 cidr;int ret;
@@ -269,22 +269,21 @@ hash_ipportnet4_uadt(struct ip_set *set, struct nlattr *tb[],ip_set_mask_from_to (ip2_from, ip2_to, e.cidr +1 ); } -if (retried) +if (retried) { ip =ntohl (h->next.ip); + p =ntohs (h->next.port); + ip2 =ntohl (h->next.ip2); + }else { + p = port; + ip2 = ip2_from; + }for (; ip <= ip_to; ip++) { e.ip =htonl (ip); - p = retried && ip ==ntohl (h->next.ip) ?ntohs (h->next.port) - : port;for (; p <= port_to; p++) { e.port =htons (p); - ip2 = retried && - ip ==ntohl (h->next.ip) && - p ==ntohs (h->next.port) - ?ntohl (h->next.ip2) : ip2_from; -while (ip2 <= ip2_to) { + do { e.ip2 = htonl(ip2); - ip2_last =ip_set_range_to_cidr (ip2, ip2_to, - &cidr); + ip2 = ip_set_range_to_cidr(ip2, ip2_to, &cidr); e.cidr = cidr - 1; ret = adtfn(set, &e, &ext, &ext, flags);
@@ -292,9 +291,10 @@ hash_ipportnet4_uadt(struct ip_set *set, struct nlattr *tb[],return ret; ret =0 ; - ip2 = ip2_last +1 ; - } + }while (ip2++ < ip2_to); + ip2 = ip2_from; } + p = port; }return ret; }
net/netfilter/ipset/ip_set_hash_net.c +4 -5
@@ -143,7 +143,7 @@ hash_net4_uadt(struct ip_set *set, struct nlattr *tb[],ipset_adtfn adtfn = set->variant->adt[adt];struct hash_net4_elem e = { .cidr =HOST_MASK };struct ip_set_ext ext =IP_SET_INIT_UEXT (set); -u32 ip =0 , ip_to =0 , last; +u32 ip =0 , ip_to =0 ;int ret;if (tb[IPSET_ATTR_LINENO ])
@@ -193,16 +193,15 @@ hash_net4_uadt(struct ip_set *set, struct nlattr *tb[], }if (retried) ip =ntohl (h->next.ip); -while (ip <= ip_to) { +do { e.ip =htonl (ip); - last =ip_set_range_to_cidr (ip, ip_to, &e.cidr); + ip =ip_set_range_to_cidr (ip, ip_to, &e.cidr); ret =adtfn (set, &e, &ext, &ext, flags);if (ret && !ip_set_eexist (ret, flags))return ret; ret =0 ; - ip = last +1 ; - } + }while (ip++ < ip_to);return ret; }
net/netfilter/ipset/ip_set_hash_netiface.c +4 -5
@@ -200,7 +200,7 @@ hash_netiface4_uadt(struct ip_set *set, struct nlattr *tb[],ipset_adtfn adtfn = set->variant->adt[adt];struct hash_netiface4_elem e = { .cidr =HOST_MASK , .elem =1 };struct ip_set_ext ext =IP_SET_INIT_UEXT (set); -u32 ip =0 , ip_to =0 , last; +u32 ip =0 , ip_to =0 ;int ret;if (tb[IPSET_ATTR_LINENO ])
@@ -255,17 +255,16 @@ hash_netiface4_uadt(struct ip_set *set, struct nlattr *tb[],if (retried) ip =ntohl (h->next.ip); -while (ip <= ip_to) { +do { e.ip =htonl (ip); - last =ip_set_range_to_cidr (ip, ip_to, &e.cidr); + ip =ip_set_range_to_cidr (ip, ip_to, &e.cidr); ret =adtfn (set, &e, &ext, &ext, flags);if (ret && !ip_set_eexist (ret, flags))return ret; ret =0 ; - ip = last +1 ; - } + }while (ip++ < ip_to);return ret; }
net/netfilter/ipset/ip_set_hash_netnet.c +14 -14
@@ -169,8 +169,8 @@ hash_netnet4_uadt(struct ip_set *set, struct nlattr *tb[],ipset_adtfn adtfn = set->variant->adt[adt];struct hash_netnet4_elem e = { };struct ip_set_ext ext =IP_SET_INIT_UEXT (set); -u32 ip =0 , ip_to =0 , last; -u32 ip2 =0 , ip2_from =0 , ip2_to =0 , last2; +u32 ip =0 , ip_to =0 ; +u32 ip2 =0 , ip2_from =0 , ip2_to =0 ;int ret;if (tb[IPSET_ATTR_LINENO ])
@@ -247,27 +247,27 @@ hash_netnet4_uadt(struct ip_set *set, struct nlattr *tb[],ip_set_mask_from_to (ip2_from, ip2_to, e.cidr[1 ]); } -if (retried) +if (retried) { ip =ntohl (h->next.ip[0 ]); + ip2 =ntohl (h->next.ip[1 ]); + }else { + ip2 = ip2_from; + } -while (ip <= ip_to) { +do { e.ip[0 ] =htonl (ip); - last =ip_set_range_to_cidr (ip, ip_to, &e.cidr[0 ]); - ip2 = (retried && - ip ==ntohl (h->next.ip[0 ])) ?ntohl (h->next.ip[1 ]) - : ip2_from; -while (ip2 <= ip2_to) { + ip =ip_set_range_to_cidr (ip, ip_to, &e.cidr[0 ]); +do { e.ip[1 ] =htonl (ip2); - last2 =ip_set_range_to_cidr (ip2, ip2_to, &e.cidr[1 ]); + ip2 =ip_set_range_to_cidr (ip2, ip2_to, &e.cidr[1 ]); ret =adtfn (set, &e, &ext, &ext, flags);if (ret && !ip_set_eexist (ret, flags))return ret; ret =0 ; - ip2 = last2 +1 ; - } - ip = last +1 ; - } + }while (ip2++ < ip2_to); + ip2 = ip2_from; + }while (ip++ < ip_to);return ret; }
net/netfilter/ipset/ip_set_hash_netport.c +10 -9
@@ -161,7 +161,7 @@ hash_netport4_uadt(struct ip_set *set, struct nlattr *tb[],ipset_adtfn adtfn = set->variant->adt[adt];struct hash_netport4_elem e = { .cidr =HOST_MASK -1 };struct ip_set_ext ext =IP_SET_INIT_UEXT (set); -u32 port, port_to, p =0 , ip =0 , ip_to =0 , last; +u32 port, port_to, p =0 , ip =0 , ip_to =0 ;bool with_ports = false;u8 cidr;int ret;
@@ -239,25 +239,26 @@ hash_netport4_uadt(struct ip_set *set, struct nlattr *tb[],ip_set_mask_from_to (ip, ip_to, e.cidr +1 ); } -if (retried) +if (retried) { ip =ntohl (h->next.ip); -while (ip <= ip_to) { + p =ntohs (h->next.port); + }else { + p = port; + } +do { e.ip =htonl (ip); - last =ip_set_range_to_cidr (ip, ip_to, &cidr); + ip =ip_set_range_to_cidr (ip, ip_to, &cidr); e.cidr = cidr -1 ; - p = retried && ip ==ntohl (h->next.ip) ?ntohs (h->next.port) - : port;for (; p <= port_to; p++) { e.port =htons (p); ret =adtfn (set, &e, &ext, &ext, flags); -if (ret && !ip_set_eexist (ret, flags))return ret; ret =0 ; } - ip = last +1 ; - } + p = port; + }while (ip++ < ip_to);return ret; }
net/netfilter/ipset/ip_set_hash_netportnet.c +18 -17
@@ -184,8 +184,8 @@ hash_netportnet4_uadt(struct ip_set *set, struct nlattr *tb[],ipset_adtfn adtfn = set->variant->adt[adt];struct hash_netportnet4_elem e = { };struct ip_set_ext ext =IP_SET_INIT_UEXT (set); -u32 ip =0 , ip_to =0 , ip_last, p =0 , port, port_to; -u32 ip2_from =0 , ip2_to =0 , ip2_last, ip2; +u32 ip =0 , ip_to =0 , p =0 , port, port_to; +u32 ip2_from =0 , ip2_to =0 , ip2;bool with_ports = false;int ret;
@@ -288,33 +288,34 @@ hash_netportnet4_uadt(struct ip_set *set, struct nlattr *tb[],ip_set_mask_from_to (ip2_from, ip2_to, e.cidr[1 ]); } -if (retried) +if (retried) { ip =ntohl (h->next.ip[0 ]); + p =ntohs (h->next.port); + ip2 =ntohl (h->next.ip[1 ]); + }else { + p = port; + ip2 = ip2_from; + } -while (ip <= ip_to) { +do { e.ip[0 ] =htonl (ip); - ip_last =ip_set_range_to_cidr (ip, ip_to, &e.cidr[0 ]); - p = retried && ip ==ntohl (h->next.ip[0 ]) ?ntohs (h->next.port) - : port; + ip =ip_set_range_to_cidr (ip, ip_to, &e.cidr[0 ]);for (; p <= port_to; p++) { e.port =htons (p); - ip2 = (retried && ip ==ntohl (h->next.ip[0 ]) && - p ==ntohs (h->next.port)) ?ntohl (h->next.ip[1 ]) - : ip2_from; -while (ip2 <= ip2_to) { +do { e.ip[1 ] =htonl (ip2); - ip2_last =ip_set_range_to_cidr (ip2, ip2_to, - &e.cidr[1 ]); + ip2 =ip_set_range_to_cidr (ip2, ip2_to, + &e.cidr[1 ]); ret =adtfn (set, &e, &ext, &ext, flags);if (ret && !ip_set_eexist (ret, flags))return ret; ret =0 ; - ip2 = ip2_last +1 ; - } + }while (ip2++ < ip2_to); + ip2 = ip2_from; } - ip = ip_last +1 ; - } + p = port; + }while (ip++ < ip_to);return ret; }
Signature · 1 line
2.11.0
Pablo Neira Ayuso
maintainerPATCH 4/6 netfilter: x_tables: fix pointer leaks to userspace
In reply to Pablo Neira AyusoFrom: Dmitry Vyukov <[email protected]>
Several netfilter matches and targets put kernel pointers into info objects, but don't set usersize in descriptors. This leads to kernel pointer leaks if a match/target is set and then read back to userspace.
Properly set usersize for these matches/targets.
Found with manual code inspection.
--- net/netfilter/xt_IDLETIMER.c | 1 + net/netfilter/xt_LED.c | 1 + net/netfilter/xt_limit.c | 3 +-- net/netfilter/xt_nfacct.c | 1 + net/netfilter/xt_statistic.c | 1 + 5 files changed, 5 insertions(+), 2 deletions(-)
net/netfilter/xt_IDLETIMER.c +1 -0
@@ -252,6 +252,7 @@ static struct xt_target idletimer_tg __read_mostly = { .family = NFPROTO_UNSPEC, .target = idletimer_tg_target, .targetsize = sizeof(struct idletimer_tg_info), + .usersize = offsetof(struct idletimer_tg_info, timer), .checkentry = idletimer_tg_checkentry, .destroy = idletimer_tg_destroy, .me = THIS_MODULE,
net/netfilter/xt_LED.c +1 -0
@@ -198,6 +198,7 @@ static struct xt_target led_tg_reg __read_mostly = { .family = NFPROTO_UNSPEC, .target = led_tg, .targetsize = sizeof(struct xt_led_info), + .usersize = offsetof(struct xt_led_info, internal_data), .checkentry = led_tg_check, .destroy = led_tg_destroy, .me = THIS_MODULE,
net/netfilter/xt_limit.c +1 -2
@@ -193,9 +193,8 @@ static struct xt_match limit_mt_reg __read_mostly = { .compatsize = sizeof(struct compat_xt_rateinfo), .compat_from_user = limit_mt_compat_from_user, .compat_to_user = limit_mt_compat_to_user, -#else - .usersize = offsetof(struct xt_rateinfo, prev), #endif + .usersize = offsetof(struct xt_rateinfo, prev), .me = THIS_MODULE, };
net/netfilter/xt_nfacct.c +1 -0
@@ -62,6 +62,7 @@ static struct xt_match nfacct_mt_reg __read_mostly = { .match = nfacct_mt, .destroy = nfacct_mt_destroy, .matchsize = sizeof(struct xt_nfacct_match_info), + .usersize = offsetof(struct xt_nfacct_match_info, nfacct), .me = THIS_MODULE, };
net/netfilter/xt_statistic.c +1 -0
@@ -84,6 +84,7 @@ static struct xt_match xt_statistic_mt_reg __read_mostly = { .checkentry = statistic_mt_check, .destroy = statistic_mt_destroy, .matchsize = sizeof(struct xt_statistic_info), + .usersize = offsetof(struct xt_statistic_info, master), .me = THIS_MODULE, };
Signature · 1 line
2.11.0
Pablo Neira Ayuso
maintainerPATCH 5/6 netfilter: ipt_CLUSTERIP: fix out-of-bounds accesses in clusterip_tg_check()
In reply to Pablo Neira AyusoFrom: Dmitry Vyukov <[email protected]>
Commit 136e92bbec0a switched local_nodes from an array to a bitmask but did not add proper bounds checks. As the result clusterip_config_init_nodelist() can both over-read ipt_clusterip_tgt_info.local_nodes and over-write clusterip_config.local_nodes.
Add bounds checks for both.
--- net/ipv4/netfilter/ipt_CLUSTERIP.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-)
net/ipv4/netfilter/ipt_CLUSTERIP.c +13 -3
@@ -431,7 +431,7 @@ static int clusterip_tg_check(const struct xt_tgchk_param *par)struct ipt_clusterip_tgt_info *cipinfo = par->targinfo;const struct ipt_entry *e = par->entryinfo;struct clusterip_config *config; -int ret; +int ret, i;if (par->nft_compat) {pr_err ("cannot use CLUSTERIP target from nftables compat\n" );
@@ -450,8 +450,18 @@ static int clusterip_tg_check(const struct xt_tgchk_param *par)pr_info ("Please specify destination IP\n" );return -EINVAL ; } - -/* FIXME: further sanity checks */ +if (cipinfo->num_local_nodes >ARRAY_SIZE (cipinfo->local_nodes)) { +pr_info ("bad num_local_nodes %u\n" , cipinfo->num_local_nodes); +return -EINVAL ; + } +for (i =0 ; i < cipinfo->num_local_nodes; i++) { +if (cipinfo->local_nodes[i] -1 >= +sizeof (config->local_nodes) *8 ) { +pr_info ("bad local_nodes[%d] %u\n" , + i, cipinfo->local_nodes[i]); +return -EINVAL ; + } + } config =clusterip_config_find_get (par->net, e->ip.dst.s_addr,1 );if (!config) {
Signature · 1 line
2.11.0
Pablo Neira Ayuso
maintainerPATCH 6/6 netfilter: on sockopt() acquire sock lock only in the required scope
In reply to Pablo Neira AyusoFrom: Paolo Abeni <[email protected]>
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+ #212 Not tainted
------------------------------------------------------
syzkaller041579/3682 is trying to acquire lock:
(sk_lock-AF_INET6){+.+.}, at: [<000000008775e4dd>] lock_sockinclude/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/0x20net/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/0xa0other 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/0x20net/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.
--- net/ipv4/ip_sockglue.c | 14 ++++---------- net/ipv4/netfilter/nf_conntrack_l3proto_ipv4.c | 6 +++++- net/ipv6/ipv6_sockglue.c | 17 +++++------------ net/ipv6/netfilter/nf_conntrack_l3proto_ipv6.c | 18 ++++++++++++------ 4 files changed, 26 insertions(+), 29 deletions(-)
net/ipv4/ip_sockglue.c +4 -10
@@ -1251,11 +1251,8 @@ int ip_setsockopt(struct sock *sk, int level,if (err == -ENOPROTOOPT && optname !=IP_HDRINCL && optname !=IP_IPSEC_POLICY && optname !=IP_XFRM_POLICY && - !ip_mroute_opt (optname)) { -lock_sock (sk); + !ip_mroute_opt (optname)) err =nf_setsockopt (sk,PF_INET , optname, optval, optlen); -release_sock (sk); - }#endif return err; }
@@ -1280,12 +1277,9 @@ int compat_ip_setsockopt(struct sock *sk, int level, int optname,if (err == -ENOPROTOOPT && optname !=IP_HDRINCL && optname !=IP_IPSEC_POLICY && optname !=IP_XFRM_POLICY && - !ip_mroute_opt (optname)) { -lock_sock (sk); - err =compat_nf_setsockopt (sk,PF_INET , optname, - optval, optlen); -release_sock (sk); - } + !ip_mroute_opt (optname)) + err =compat_nf_setsockopt (sk,PF_INET , optname, optval, + optlen);#endif return err; }
net/ipv4/netfilter/nf_conntrack_l3proto_ipv4.c +5 -1
@@ -218,15 +218,19 @@ getorigdst(struct sock *sk, int optval, void __user *user, int *len)struct nf_conntrack_tuple tuple;memset (&tuple,0 ,sizeof (tuple)); + +lock_sock (sk); tuple.src.u3.ip = inet->inet_rcv_saddr; tuple.src.u.tcp.port = inet->inet_sport; tuple.dst.u3.ip = inet->inet_daddr; tuple.dst.u.tcp.port = inet->inet_dport; tuple.src.l3num =PF_INET ; tuple.dst.protonum = sk->sk_protocol; +release_sock (sk);/* We only do TCP and SCTP at the moment: is there a better way? */ -if (sk->sk_protocol !=IPPROTO_TCP && sk->sk_protocol !=IPPROTO_SCTP ) { +if (tuple.dst.protonum !=IPPROTO_TCP && + tuple.dst.protonum !=IPPROTO_SCTP ) {pr_debug ("SO_ORIGINAL_DST: Not a TCP/SCTP socket\n" );return -ENOPROTOOPT ; }
net/ipv6/ipv6_sockglue.c +5 -12
@@ -923,12 +923,8 @@ int ipv6_setsockopt(struct sock *sk, int level, int optname,#ifdef CONFIG_NETFILTER /* we need to exclude all possible ENOPROTOOPTs except default case */ if (err == -ENOPROTOOPT && optname !=IPV6_IPSEC_POLICY && - optname !=IPV6_XFRM_POLICY ) { -lock_sock (sk); - err =nf_setsockopt (sk,PF_INET6 , optname, optval, - optlen); -release_sock (sk); - } + optname !=IPV6_XFRM_POLICY ) + err =nf_setsockopt (sk,PF_INET6 , optname, optval, optlen);#endif return err; }
@@ -958,12 +954,9 @@ int compat_ipv6_setsockopt(struct sock *sk, int level, int optname,#ifdef CONFIG_NETFILTER /* we need to exclude all possible ENOPROTOOPTs except default case */ if (err == -ENOPROTOOPT && optname !=IPV6_IPSEC_POLICY && - optname !=IPV6_XFRM_POLICY ) { -lock_sock (sk); - err =compat_nf_setsockopt (sk,PF_INET6 , optname, - optval, optlen); -release_sock (sk); - } + optname !=IPV6_XFRM_POLICY ) + err =compat_nf_setsockopt (sk,PF_INET6 , optname, optval, + optlen);#endif return err; }
net/ipv6/netfilter/nf_conntrack_l3proto_ipv6.c +12 -6
@@ -226,20 +226,27 @@ static const struct nf_hook_ops ipv6_conntrack_ops[] = {static int ipv6_getorigdst (struct sock *sk,int optval,void __user *user,int *len) { -const struct inet_sock *inet =inet_sk (sk); +struct nf_conntrack_tuple tuple = { .src.l3num =NFPROTO_IPV6 };const struct ipv6_pinfo *inet6 =inet6_sk (sk); +const struct inet_sock *inet =inet_sk (sk);const struct nf_conntrack_tuple_hash *h;struct sockaddr_in6 sin6; -struct nf_conntrack_tuple tuple = { .src.l3num =NFPROTO_IPV6 };struct nf_conn *ct; +__be32 flow_label; +int bound_dev_if; +lock_sock (sk); tuple.src.u3.in6 = sk->sk_v6_rcv_saddr; tuple.src.u.tcp.port = inet->inet_sport; tuple.dst.u3.in6 = sk->sk_v6_daddr; tuple.dst.u.tcp.port = inet->inet_dport; tuple.dst.protonum = sk->sk_protocol; + bound_dev_if = sk->sk_bound_dev_if; + flow_label = inet6->flow_label; +release_sock (sk); -if (sk->sk_protocol !=IPPROTO_TCP && sk->sk_protocol !=IPPROTO_SCTP ) +if (tuple.dst.protonum !=IPPROTO_TCP && + tuple.dst.protonum !=IPPROTO_SCTP )return -ENOPROTOOPT ;if (*len <0 || (unsigned int ) *len <sizeof (sin6))
@@ -257,14 +264,13 @@ ipv6_getorigdst(struct sock *sk, int optval, void __user *user, int *len) sin6.sin6_family =AF_INET6 ; sin6.sin6_port = ct->tuplehash[IP_CT_DIR_ORIGINAL ].tuple.dst.u.tcp.port; - sin6.sin6_flowinfo = inet6->flow_label &IPV6_FLOWINFO_MASK ; + sin6.sin6_flowinfo = flow_label &IPV6_FLOWINFO_MASK ;memcpy (&sin6.sin6_addr, &ct->tuplehash[IP_CT_DIR_ORIGINAL ].tuple.dst.u3.in6,sizeof (sin6.sin6_addr));nf_ct_put (ct); - sin6.sin6_scope_id =ipv6_iface_scope_id (&sin6.sin6_addr, - sk->sk_bound_dev_if); + sin6.sin6_scope_id =ipv6_iface_scope_id (&sin6.sin6_addr, bound_dev_if);return copy_to_user (user, &sin6,sizeof (sin6)) ? -EFAULT :0 ; }
Signature · 1 line
2.11.0
David Miller
maintainerFrom: Pablo Neira Ayuso <[email protected]>
Date: Thu, 1 Feb 2018 19:02:11 +0100
Quoted 12 lines
The following patchset contains Netfilter fixes for your net tree, they are:
1) Fix OOM that syskaller triggers with ipt_replace.size = -1 and
IPT_SO_SET_REPLACEsocket option, from Dmitry Vyukov.2) Check for too long extension name in xt_request_find_{match|target} that result in out-of-bound reads, from Eric Dumazet.
3) Fix memory exhaustion bug in ipset hash:*net* types when adding ranges that look like x.x.x.x-255.255.255.255, from Jozsef Kadlecsik.
Quoted 7 lines
4) Fix pointer leaks to userspace in
x_tables, from Dmitry Vyukov.5) Insufficient sanity checks in
clusterip_tg_check(), also from Dmitry.You can pull these changes from:
git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf.git
Pulled, thanks.