Netfilter fixes for net
Subsystems (3)
Pablo Neira Ayuso
maintainercover Netfilter fixes for net
Hi,
The following patchset contains Netfilter fixes for net:
1) Missing netlink attribute sanity check for NFTA_OSF_DREG, from Florian Westphal.
2) Use bitmap infrastructure in ipset to fix KASAN slab-out-of-bounds reads, from Jozsef Kadlecsik.
3) Missing initial CLOSED state in new sctp connection through ctnetlink events, from Jiri Wiesner.
4) Missing check for NFT_CHAIN_HW_OFFLOAD in nf_tables offload indirect block infrastructure, from wenxu.
5) Add __nft_chain_type_get() to sanity check family and chain type.
6) Autoload modules from the nf_tables abort path to fix races reported by syzbot.
7) Remove unnecessary skb->csum update on inet_proto_csum_replace16(), from Praveen Chaudhary.
You can pull these changes from:
git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf.git
Thank you.
----------------------------------------------------------------
The following changes since commit e02d9c4c68dc0ca08ded9487720bba775c09669b:
Merge branch 'bnxt_en-fixes' (2020-01-18 14:38:30 +0100)
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 189c9b1e94539b11c80636bc13e9cf47529e7bba:
net: Fix skb->csum update in inet_proto_csum_replace16(). (2020-01-24 20:54:30 +0100)
----------------------------------------------------------------
Florian Westphal (1):
netfilter: nft_osf: add missing check for DREG attribute
Jiri Wiesner (1):
netfilter: conntrack: sctp: use distinct states for new SCTP connections
Kadlecsik József (1):
netfilter: ipset: use bitmap infrastructure completely
Pablo Neira Ayuso (2):
netfilter: nf_tables: add __nft_chain_type_get()
netfilter: nf_tables: autoload modules from the abort path
Praveen Chaudhary (1):
net: Fix skb->csum update in inet_proto_csum_replace16().
wenxu (1):
netfilter: nf_tables_offload: fix check the chain offload flag
include/linux/netfilter/ipset/ip_set.h | 7 -- include/linux/netfilter/nfnetlink.h | 2 +- include/net/netns/nftables.h | 1 + net/core/utils.c | 20 +++- net/netfilter/ipset/ip_set_bitmap_gen.h | 2 +- net/netfilter/ipset/ip_set_bitmap_ip.c | 6 +- net/netfilter/ipset/ip_set_bitmap_ipmac.c | 6 +- net/netfilter/ipset/ip_set_bitmap_port.c | 6 +- net/netfilter/nf_conntrack_proto_sctp.c | 6 +- net/netfilter/nf_tables_api.c | 155 +++++++++++++++++++++--------- net/netfilter/nf_tables_offload.c | 2 +- net/netfilter/nfnetlink.c | 6 +- net/netfilter/nft_osf.c | 3 + 13 files changed, 146 insertions(+), 76 deletions(-)
Pablo Neira Ayuso
maintainerPATCH 1/7 netfilter: nft_osf: add missing check for DREG attribute
From: Florian Westphal <[email protected]>
syzbot reports just another NULL deref crash because of missing test for presence of the attribute.
--- net/netfilter/nft_osf.c | 3 +++ 1 file changed, 3 insertions(+)
net/netfilter/nft_osf.c +3 -0
@@ -61,6 +61,9 @@ static int nft_osf_init(const struct nft_ctx *ctx,int err;u8 ttl; +if (!tb[NFTA_OSF_DREG ]) +return -EINVAL ; +if (tb[NFTA_OSF_TTL ]) { ttl =nla_get_u8 (tb[NFTA_OSF_TTL ]);if (ttl >2 )
Signature · 1 line
2.11.0
Pablo Neira Ayuso
maintainerPATCH 2/7 netfilter: ipset: use bitmap infrastructure completely
In reply to Pablo Neira AyusoFrom: Kadlecsik József <[email protected]>
The bitmap allocation did not use full unsigned long sizes when calculating the required size and that was triggered by KASAN as slab-out-of-bounds read in several places. The patch fixes all of them.
--- include/linux/netfilter/ipset/ip_set.h | 7 ------- net/netfilter/ipset/ip_set_bitmap_gen.h | 2 +- net/netfilter/ipset/ip_set_bitmap_ip.c | 6 +++--- net/netfilter/ipset/ip_set_bitmap_ipmac.c | 6 +++--- net/netfilter/ipset/ip_set_bitmap_port.c | 6 +++--- 5 files changed, 10 insertions(+), 17 deletions(-)
include/linux/netfilter/ipset/ip_set.h +0 -7
@@ -426,13 +426,6 @@ ip6addrptr(const struct sk_buff *skb, bool src, struct in6_addr *addr) sizeof(*addr)); } -/* Calculate the bytes required to store the inclusive range of a-b */ -static inline int -bitmap_bytes (u32 a,u32 b) -{ -return 4 * ((((b - a +8 ) /8 ) +3 ) /4 ); -} -/* How often should the gc be run by default */ #define IPSET_GC_TIME (3 * 60)
net/netfilter/ipset/ip_set_bitmap_gen.h +1 -1
@@ -75,7 +75,7 @@ mtype_flush(struct ip_set *set)if (set->extensions &IPSET_EXT_DESTROY )mtype_ext_cleanup (set); -memset (map->members,0 , map->memsize); +bitmap_zero (map->members, map->elements); set->elements =0 ; set->ext_size =0 ; }
net/netfilter/ipset/ip_set_bitmap_ip.c +3 -3
@@ -37,7 +37,7 @@ MODULE_ALIAS("ip_set_bitmap:ip");/* Type structure */ struct bitmap_ip { -void *members;/* the set members */ +unsigned long *members;/* the set members */ u32 first_ip;/* host byte order, included in range */ u32 last_ip;/* host byte order, included in range */ u32 elements;/* number of max elements in the set */
@@ -220,7 +220,7 @@ init_map_ip(struct ip_set *set, struct bitmap_ip *map, u32 first_ip, u32 last_ip, u32 elements, u32 hosts, u8 netmask) { - map->members = ip_set_alloc(map->memsize); + map->members = bitmap_zalloc(elements, GFP_KERNEL | __GFP_NOWARN);if (!map->members)return false; map->first_ip = first_ip;
@@ -322,7 +322,7 @@ bitmap_ip_create(struct net *net, struct ip_set *set, struct nlattr *tb[],if (!map)return -ENOMEM ; - map->memsize =bitmap_bytes (0 , elements -1 ); + map->memsize =BITS_TO_LONGS (elements) *sizeof (unsigned long ); set->variant = &bitmap_ip;if (!init_map_ip (set, map, first_ip, last_ip, elements, hosts, netmask)) {
net/netfilter/ipset/ip_set_bitmap_ipmac.c +3 -3
@@ -42,7 +42,7 @@ enum {/* Type structure */ struct bitmap_ipmac { -void *members;/* the set members */ +unsigned long *members;/* the set members */ u32 first_ip;/* host byte order, included in range */ u32 last_ip;/* host byte order, included in range */ u32 elements;/* number of max elements in the set */
@@ -299,7 +299,7 @@ static bool init_map_ipmac(struct ip_set *set, struct bitmap_ipmac *map, u32 first_ip, u32 last_ip, u32 elements) { - map->members = ip_set_alloc(map->memsize); + map->members = bitmap_zalloc(elements, GFP_KERNEL | __GFP_NOWARN);if (!map->members)return false; map->first_ip = first_ip;
@@ -360,7 +360,7 @@ bitmap_ipmac_create(struct net *net, struct ip_set *set, struct nlattr *tb[],if (!map)return -ENOMEM ; - map->memsize =bitmap_bytes (0 , elements -1 ); + map->memsize =BITS_TO_LONGS (elements) *sizeof (unsigned long ); set->variant = &bitmap_ipmac;if (!init_map_ipmac (set, map, first_ip, last_ip, elements)) {kfree (map);
net/netfilter/ipset/ip_set_bitmap_port.c +3 -3
@@ -30,7 +30,7 @@ MODULE_ALIAS("ip_set_bitmap:port");/* Type structure */ struct bitmap_port { -void *members;/* the set members */ +unsigned long *members;/* the set members */ u16 first_port;/* host byte order, included in range */ u16 last_port;/* host byte order, included in range */ u32 elements;/* number of max elements in the set */
@@ -231,7 +231,7 @@ static bool init_map_port(struct ip_set *set, struct bitmap_port *map, u16 first_port, u16 last_port) { - map->members = ip_set_alloc(map->memsize); + map->members = bitmap_zalloc(map->elements, GFP_KERNEL | __GFP_NOWARN);if (!map->members)return false; map->first_port = first_port;
@@ -271,7 +271,7 @@ bitmap_port_create(struct net *net, struct ip_set *set, struct nlattr *tb[],return -ENOMEM ; map->elements = elements; - map->memsize =bitmap_bytes (0 , map->elements); + map->memsize =BITS_TO_LONGS (elements) *sizeof (unsigned long ); set->variant = &bitmap_port;if (!init_map_port (set, map, first_port, last_port)) {kfree (map);
Signature · 1 line
2.11.0
Pablo Neira Ayuso
maintainerPATCH 3/7 netfilter: conntrack: sctp: use distinct states for new SCTP connections
In reply to Pablo Neira AyusoFrom: Jiri Wiesner <[email protected]>
The netlink notifications triggered by the INIT and INIT_ACK chunks for a tracked SCTP association do not include protocol information for the corresponding connection - SCTP state and verification tags for the original and reply direction are missing. Since the connection tracking implementation allows user space programs to receive notifications about a connection and then create a new connection based on the values received in a notification, it makes sense that INIT and INIT_ACK notifications should contain the SCTP state and verification tags available at the time when a notification is sent. The missing verification tags cause a newly created netfilter connection to fail to verify the tags of SCTP packets when this connection has been created from the values previously received in an INIT or INIT_ACK notification.
A PROTOINFO event is cached in sctp_packet() when the state of a connection changes. The CLOSED and COOKIE_WAIT state will be used for connections that have seen an INIT and INIT_ACK chunk, respectively. The distinct states will cause a connection state change in sctp_packet().
--- net/netfilter/nf_conntrack_proto_sctp.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
net/netfilter/nf_conntrack_proto_sctp.c +3 -3
@@ -114,7 +114,7 @@ static const u8 sctp_conntracks[2][11][SCTP_CONNTRACK_MAX] = { {/* ORIGINAL */ /* sNO, sCL, sCW, sCE, sES, sSS, sSR, sSA, sHS, sHA */ -/* init */ {sCW, sCW, sCW, sCE, sES, sSS, sSR, sSA, sCW, sHA}, +/* init */ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sSA, sCW, sHA}, /* init_ack */ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sSA, sCL, sHA}, /* abort */ {sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL}, /* shutdown */ {sCL, sCL, sCW, sCE, sSS, sSS, sSR, sSA, sCL, sSS},
@@ -130,7 +130,7 @@ static const u8 sctp_conntracks[2][11][SCTP_CONNTRACK_MAX] = {/* REPLY */ /* sNO, sCL, sCW, sCE, sES, sSS, sSR, sSA, sHS, sHA */ /* init */ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sSA, sIV, sHA},/* INIT in sCL Big TODO */ -/* init_ack */ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sSA, sIV, sHA}, +/* init_ack */ {sIV, sCW, sCW, sCE, sES, sSS, sSR, sSA, sIV, sHA}, /* abort */ {sIV, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sIV, sCL}, /* shutdown */ {sIV, sCL, sCW, sCE, sSR, sSS, sSR, sSA, sIV, sSR}, /* shutdown_ack */ {sIV, sCL, sCW, sCE, sES, sSA, sSA, sSA, sIV, sHA},
@@ -316,7 +316,7 @@ sctp_new(struct nf_conn *ct, const struct sk_buff *skb, ct->proto.sctp.vtag[IP_CT_DIR_REPLY ] = sh->vtag; } - ct->proto.sctp.state = new_state; + ct->proto.sctp.state =SCTP_CONNTRACK_NONE ; }return true;
Signature · 1 line
2.11.0
Pablo Neira Ayuso
maintainerPATCH 4/7 netfilter: nf_tables_offload: fix check the chain offload flag
In reply to Pablo Neira AyusoFrom: wenxu <[email protected]>
In the nft_indr_block_cb the chain should check the flag with NFT_CHAIN_HW_OFFLOAD.
--- net/netfilter/nf_tables_offload.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/netfilter/nf_tables_offload.c +1 -1
@@ -564,7 +564,7 @@ static void nft_indr_block_cb(struct net_device *dev,mutex_lock (&net->nft.commit_mutex); chain =__nft_offload_get_chain (dev); -if (chain) { +if (chain && chain->flags &NFT_CHAIN_HW_OFFLOAD ) {struct nft_base_chain *basechain; basechain =nft_base_chain (chain);
Signature · 1 line
2.11.0
Pablo Neira Ayuso
maintainerPATCH 5/7 netfilter: nf_tables: add __nft_chain_type_get()
In reply to Pablo Neira AyusoThis new helper function validates that unknown family and chain type coming from userspace do not trigger an out-of-bound array access. Bail out in case __nft_chain_type_get() returns NULL from nft_chain_parse_hook().
--- net/netfilter/nf_tables_api.c | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-)
net/netfilter/nf_tables_api.c +21 -8
@@ -553,14 +553,27 @@ static inline u64 nf_tables_alloc_handle(struct nft_table *table)static const struct nft_chain_type *chain_type[NFPROTO_NUMPROTO ][NFT_CHAIN_T_MAX ];static const struct nft_chain_type * +__nft_chain_type_get (u8 family,enum nft_chain_types type) +{ +if (family >=NFPROTO_NUMPROTO || + type >=NFT_CHAIN_T_MAX ) +return NULL ; + +return chain_type[family][type]; +} + +static const struct nft_chain_type *__nf_tables_chain_type_lookup (const struct nlattr *nla,u8 family) { +const struct nft_chain_type *type;int i;for (i =0 ; i <NFT_CHAIN_T_MAX ; i++) { -if (chain_type[family][i] !=NULL && - !nla_strcmp (nla, chain_type[family][i]->name)) -return chain_type[family][i]; + type =__nft_chain_type_get (family, i); +if (!type) +continue ; +if (!nla_strcmp (nla, type->name)) +return type; }return NULL ; }
@@ -1162,11 +1175,8 @@ static void nf_tables_table_destroy(struct nft_ctx *ctx)void nft_register_chain_type (const struct nft_chain_type *ctype) { -if (WARN_ON (ctype->family >=NFPROTO_NUMPROTO )) -return ; -nfnl_lock (NFNL_SUBSYS_NFTABLES ); -if (WARN_ON (chain_type[ctype->family][ctype->type] !=NULL )) { +if (WARN_ON (__nft_chain_type_get (ctype->family, ctype->type))) {nfnl_unlock (NFNL_SUBSYS_NFTABLES );return ; }
@@ -1768,7 +1778,10 @@ static int nft_chain_parse_hook(struct net *net, hook->num =ntohl (nla_get_be32 (ha[NFTA_HOOK_HOOKNUM ])); hook->priority =ntohl (nla_get_be32 (ha[NFTA_HOOK_PRIORITY ])); - type = chain_type[family][NFT_CHAIN_T_DEFAULT ]; + type =__nft_chain_type_get (family,NFT_CHAIN_T_DEFAULT ); +if (!type) +return -EOPNOTSUPP ; +if (nla[NFTA_CHAIN_TYPE ]) { type =nf_tables_chain_type_lookup (net, nla[NFTA_CHAIN_TYPE ], family, autoload);
Signature · 1 line
2.11.0
Pablo Neira Ayuso
maintainerPATCH 6/7 netfilter: nf_tables: autoload modules from the abort path
In reply to Pablo Neira AyusoThis patch introduces a list of pending module requests. This new module list is composed of nft_module_request objects that contain the module name and one status field that tells if the module has been already loaded (the 'done' field).
In the first pass, from the preparation phase, the netlink command finds that a module is missing on this list. Then, a module request is allocated and added to this list and nft_request_module() returns -EAGAIN. This triggers the abort path with the autoload parameter set on from nfnetlink, request_module() is called and the module request enters the 'done' state. Since the mutex is released when loading modules from the abort phase, the module list is zapped so this is iteration occurs over a local list. Therefore, the request_module() calls happen when object lists are in consistent state (after fulling aborting the transaction) and the commit list is empty.
On the second pass, the netlink command will find that it already tried to load the module, so it does not request it again and nft_request_module() returns 0. Then, there is a look up to find the object that the command was missing. If the module was successfully loaded, the command proceeds normally since it finds the missing object in place, otherwise -ENOENT is reported to userspace.
This patch also updates nfnetlink to include the reason to enter the abort phase, which is required for this new autoload module rationale.
--- include/linux/netfilter/nfnetlink.h | 2 +- include/net/netns/nftables.h | 1 + net/netfilter/nf_tables_api.c | 126 ++++++++++++++++++++++++------------ net/netfilter/nfnetlink.c | 6 +- 4 files changed, 91 insertions(+), 44 deletions(-)
include/linux/netfilter/nfnetlink.h +1 -1
@@ -31,7 +31,7 @@ struct nfnetlink_subsystem {const struct nfnl_callback *cb;/* callback for individual types */ struct module *owner;int (*commit)(struct net *net,struct sk_buff *skb); -int (*abort)(struct net *net,struct sk_buff *skb); +int (*abort)(struct net *net,struct sk_buff *skb,bool autoload);void (*cleanup)(struct net *net);bool (*valid_genid)(struct net *net,u32 genid); };
include/net/netns/nftables.h +1 -0
@@ -7,6 +7,7 @@struct netns_nftables {struct list_head tables;struct list_head commit_list; +struct list_head module_list;struct mutex commit_mutex;unsigned int base_seq;u8 gencursor;
net/netfilter/nf_tables_api.c +86 -40
241 lines not on this page · raw message
net/netfilter/nfnetlink.c +3 -3
@@ -476,7 +476,7 @@ static void nfnetlink_rcv_batch(struct sk_buff *skb, struct nlmsghdr *nlh, } done:if (status &NFNL_BATCH_REPLAY ) { - ss->abort (net, oskb); + ss->abort (net, oskb, true);nfnl_err_reset (&err_list);kfree_skb (skb);module_put (ss->owner);
@@ -487,11 +487,11 @@ static void nfnetlink_rcv_batch(struct sk_buff *skb, struct nlmsghdr *nlh, status |=NFNL_BATCH_REPLAY ; goto done; }else if (err) { - ss->abort (net, oskb); + ss->abort (net, oskb, false);netlink_ack (oskb,nlmsg_hdr (oskb), err,NULL ); } }else { - ss->abort (net, oskb); + ss->abort (net, oskb, false); }if (ss->cleanup) ss->cleanup (net);
Signature · 1 line
2.11.0
Pablo Neira Ayuso
maintainerPATCH 7/7 net: Fix skb->csum update in inet_proto_csum_replace16().
In reply to Pablo Neira AyusoFrom: Praveen Chaudhary <[email protected]>
skb->csum is updated incorrectly, when manipulation for NF_NAT_MANIP_SRC\DST is done on IPV6 packet.
Fix:
There is no need to update skb->csum in inet_proto_csum_replace16(), because update in two fields a.) IPv6 src/dst address and b.) L4 header checksum cancels each other for skb->csum calculation. Whereas inet_proto_csum_replace4 function needs to update skb->csum, because update in 3 fields a.) IPv4 src/dst address, b.) IPv4 Header checksum and c.) L4 header checksum results in same diff as L4 Header checksum for skb->csum calculation.
--- net/core/utils.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-)
net/core/utils.c +17 -3
@@ -438,6 +438,23 @@ void inet_proto_csum_replace4(__sum16 *sum, struct sk_buff *skb, }EXPORT_SYMBOL (inet_proto_csum_replace4); +/** +* inet_proto_csum_replace16 - update layer 4 header checksum field +* @sum: Layer 4 header checksum field +* @skb: sk_buff for the packet +* @from: old IPv6 address +* @to: new IPv6 address +* @pseudohdr: True if layer 4 header checksum includes pseudoheader +* +* Update layer 4 header as per the update in IPv6 src/dst address. +* +* There is no need to update skb->csum in this function, because update in two +* fields a.) IPv6 src/dst address and b.) L4 header checksum cancels each other +* for skb->csum calculation. Whereas inet_proto_csum_replace4 function needs to +* update skb->csum, because update in 3 fields a.) IPv4 src/dst address, +* b.) IPv4 Header checksum and c.) L4 header checksum results in same diff as +* L4 Header checksum for skb->csum calculation. +*/ void inet_proto_csum_replace16 (__sum16 *sum,struct sk_buff *skb,const __be32 *from,const __be32 *to,bool pseudohdr)
@@ -449,9 +466,6 @@ void inet_proto_csum_replace16(__sum16 *sum, struct sk_buff *skb,if (skb->ip_summed !=CHECKSUM_PARTIAL ) { *sum =csum_fold (csum_partial (diff,sizeof (diff), ~csum_unfold (*sum))); -if (skb->ip_summed ==CHECKSUM_COMPLETE && pseudohdr) - skb->csum = ~csum_partial (diff,sizeof (diff), - ~skb->csum); }else if (pseudohdr) *sum = ~csum_fold (csum_partial (diff,sizeof (diff),csum_unfold (*sum)));
Signature · 1 line
2.11.0
David Miller
maintainerFrom: Pablo Neira Ayuso <[email protected]>
Date: Sat, 25 Jan 2020 18:34:08 +0100
Quoted 1 line
The following patchset contains Netfilter fixes for net:
...
Quoted 3 lines
You can pull these changes from:
git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf.git
Pulled, thanks Pablo.