netfilter: nf_tables fixes
Subsystems (2)
cover netfilter: nf_tables fixes
The following patches fix a couple of bugs in nf_tables:
- potential NULL pointer dereference when dumping sets
- missing NFT_CT_L3PROTOCOL key in ct expression validation
- the module dependency issues we've discussed
Please note, the last patch is *not* a new feature but part of the fix since NFPROTO_INET was previously also handled by nft_reject. I just put it into a seperate patch for easier review.
Please apply, thanks.
Patrick McHardy (5):
netfilter: nf_tables: fix potential oops when dumping sets
netfilter: nft_ct: fix missing NFT_CT_L3PROTOCOL key in validity checks
netfilter: nf_tables: add AF specific expression support
netfilter: nft_reject: split up reject module into IPv4 and IPv6 specifc parts
netfilter: nf_tables: add reject module for NFPROTO_INET
include/net/netfilter/nf_tables.h | 5 ++ net/ipv4/netfilter/Kconfig | 5 ++ net/ipv4/netfilter/Makefile | 1 + net/ipv4/netfilter/nft_reject_ipv4.c | 75 ++++++++++++++++++++++++++++++ net/ipv6/netfilter/Kconfig | 5 ++ net/ipv6/netfilter/Makefile | 1 + net/ipv6/netfilter/nft_reject_ipv6.c | 76 ++++++++++++++++++++++++++++++ net/netfilter/Kconfig | 6 ++- net/netfilter/Makefile | 1 + net/netfilter/nf_tables_api.c | 30 ++++++++---- net/netfilter/nft_ct.c | 1 + net/netfilter/nft_reject.c | 89 ++++-------------------------------- net/netfilter/nft_reject_inet.c | 63 +++++++++++++++++++++++++ 13 files changed, 268 insertions(+), 90 deletions(-)
PATCH 1/5 netfilter: nf_tables: fix potential oops when dumping sets
Commit c9c8e48597 (netfilter: nf_tables: dump sets in all existing families) changed nft_ctx_init_from_setattr() to only look up the address family if it is not NFPROTO_UNSPEC. However if it is NFPROTO_UNSPEC and a table attribute is given, nftables_afinfo_lookup() will dereference the NULL afi pointer.
Fix by checking for non-NULL afi and also move a check added by that commit to the proper position.
--- net/netfilter/nf_tables_api.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
net/netfilter/nf_tables_api.c +5 -3
@@ -1943,6 +1943,9 @@ static int nft_ctx_init_from_setattr(struct nft_ctx *ctx, }if (nla[NFTA_SET_TABLE ] !=NULL ) { +if (afi ==NULL ) +return -EAFNOSUPPORT ;; + table =nf_tables_table_lookup (afi, nla[NFTA_SET_TABLE ]);if (IS_ERR (table))return PTR_ERR (table);
@@ -2428,6 +2431,8 @@ static int nf_tables_delset(struct sock *nlsk, struct sk_buff *skb,struct nft_ctx ctx;int err; +if (nfmsg->nfgen_family ==NFPROTO_UNSPEC ) +return -EAFNOSUPPORT ;if (nla[NFTA_SET_TABLE ] ==NULL )return -EINVAL ;
@@ -2435,9 +2440,6 @@ static int nf_tables_delset(struct sock *nlsk, struct sk_buff *skb,if (err <0 )return err; -if (nfmsg->nfgen_family ==NFPROTO_UNSPEC ) -return -EAFNOSUPPORT ; - set =nf_tables_set_lookup (ctx.table, nla[NFTA_SET_NAME ]);if (IS_ERR (set))return PTR_ERR (set);
Signature · 1 line
1.8.5.3
Re: [PATCH 1/5] netfilter: nf_tables: fix potential oops when dumping sets
Quoted 7 lines · Patrick McHardy
Commit
c9c8e48597(netfilter:nf_tables: dump sets in all existing families) changednft_ctx_init_from_setattr()to only look up the address family if it is notNFPROTO_UNSPEC. However if it isNFPROTO_UNSPECand a table attribute is given,nftables_afinfo_lookup()will dereference the NULL afi pointer.Fix by checking for non-NULL afi and also move a check added by that commit to the proper position.
Applied, thanks.
PATCH 2/5 netfilter: nft_ct: fix missing NFT_CT_L3PROTOCOL key in validity checks
In reply to Patrick McHardyThe key was missing in the list of valid keys, add it.
--- net/netfilter/nft_ct.c | 1 + 1 file changed, 1 insertion(+)
net/netfilter/nft_ct.c +1 -0
@@ -226,6 +226,7 @@ static int nft_ct_init_validate_get(const struct nft_expr *expr,if (tb[NFTA_CT_DIRECTION ] !=NULL )return -EINVAL ;break ; +case NFT_CT_L3PROTOCOL :case NFT_CT_PROTOCOL :case NFT_CT_SRC :case NFT_CT_DST :
Signature · 1 line
1.8.5.3
Pablo Neira Ayuso
maintainerRe: [PATCH 2/5] netfilter: nft_ct: fix missing NFT_CT_L3PROTOCOL key in validity checks
Quoted 1 line · Patrick McHardy
The key was missing in the list of valid keys, add it.
Also applied, thanks.
PATCH 3/5 netfilter: nf_tables: add AF specific expression support
In reply to Patrick McHardyFor the reject module, we need to add AF-specific implementations to get rid of incorrect module dependencies. Try to load an AF-specific module first and fall back to generic modules.
--- include/net/netfilter/nf_tables.h | 5 +++++ net/netfilter/nf_tables_api.c | 22 ++++++++++++++++------ 2 files changed, 21 insertions(+), 6 deletions(-)
include/net/netfilter/nf_tables.h +5 -0
@@ -252,6 +252,7 @@ void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,* @owner: module reference * @policy: netlink attribute policy * @maxattr: highest netlink attribute number +* @family: address family for AF-specific types */ struct nft_expr_type { const struct nft_expr_ops *(*select_ops)(const struct nft_ctx *,
@@ -262,6 +263,7 @@ struct nft_expr_type {struct module *owner;const struct nla_policy *policy;unsigned int maxattr; +u8 family; };/**
@@ -529,6 +531,9 @@ void nft_unregister_expr(struct nft_expr_type *);#define MODULE_ALIAS_NFT_CHAIN (family, name) \ MODULE_ALIAS("nft-chain-" __stringify(family) "-" name) +#define MODULE_ALIAS_NFT_AF_EXPR (family, name) \ + MODULE_ALIAS("nft-expr-" __stringify(family) "-" name) +#define MODULE_ALIAS_NFT_EXPR (name) \ MODULE_ALIAS("nft-expr-" name)
net/netfilter/nf_tables_api.c +16 -6
@@ -1114,35 +1114,45 @@ void nft_unregister_expr(struct nft_expr_type *type) }EXPORT_SYMBOL_GPL (nft_unregister_expr); -static const struct nft_expr_type *__nft_expr_type_get (struct nlattr *nla) +static const struct nft_expr_type *__nft_expr_type_get (u8 family, +struct nlattr *nla) {const struct nft_expr_type *type;list_for_each_entry (type, &nf_tables_expressions, list) { -if (!nla_strcmp (nla, type->name)) +if (!nla_strcmp (nla, type->name) && + (!type->family || type->family == family))return type; }return NULL ; } -static const struct nft_expr_type *nft_expr_type_get (struct nlattr *nla) +static const struct nft_expr_type *nft_expr_type_get (u8 family, +struct nlattr *nla) {const struct nft_expr_type *type;if (nla ==NULL )return ERR_PTR (-EINVAL ); - type =__nft_expr_type_get (nla); + type =__nft_expr_type_get (family, nla);if (type !=NULL &&try_module_get (type->owner))return type;#ifdef CONFIG_MODULES if (type ==NULL ) {nfnl_unlock (NFNL_SUBSYS_NFTABLES ); +request_module ("nft-expr-%u-%.*s" , family, +nla_len (nla), (char *)nla_data (nla)); +nfnl_lock (NFNL_SUBSYS_NFTABLES ); +if (__nft_expr_type_get (family, nla)) +return ERR_PTR (-EAGAIN ); + +nfnl_unlock (NFNL_SUBSYS_NFTABLES );request_module ("nft-expr-%.*s" ,nla_len (nla), (char *)nla_data (nla));nfnl_lock (NFNL_SUBSYS_NFTABLES ); -if (__nft_expr_type_get (nla)) +if (__nft_expr_type_get (family, nla))return ERR_PTR (-EAGAIN ); }#endif
@@ -1193,7 +1203,7 @@ static int nf_tables_expr_parse(const struct nft_ctx *ctx,if (err <0 )return err; - type =nft_expr_type_get (tb[NFTA_EXPR_NAME ]); + type =nft_expr_type_get (ctx->afi->family, tb[NFTA_EXPR_NAME ]);if (IS_ERR (type))return PTR_ERR (type);
Signature · 1 line
1.8.5.3
Re: [PATCH 3/5] netfilter: nf_tables: add AF specific expression support
Quoted 3 lines · Patrick McHardy
For the reject module, we need to add AF-specific implementations to get rid of incorrect module dependencies. Try to load an AF-specific module first and fall back to generic modules.
Applied, thanks Patrick.
PATCH 4/5 netfilter: nft_reject: split up reject module into IPv4 and IPv6 specifc parts
In reply to Patrick McHardyCurrently the nft_reject module depends on symbols from ipv6. This is wrong since no generic module should force IPv6 support to be loaded. Split up the module into AF-specific and a generic part.
--- net/ipv4/netfilter/Kconfig | 5 ++ net/ipv4/netfilter/Makefile | 1 + net/ipv4/netfilter/nft_reject_ipv4.c | 74 ++++++++++++++++++++++++++++++ net/ipv6/netfilter/Kconfig | 5 ++ net/ipv6/netfilter/Makefile | 1 + net/ipv6/netfilter/nft_reject_ipv6.c | 75 ++++++++++++++++++++++++++++++ net/netfilter/Kconfig | 1 - net/netfilter/nft_reject.c | 89 ++++-------------------------------- 8 files changed, 170 insertions(+), 81 deletions(-) create mode 100644 net/ipv4/netfilter/nft_reject_ipv4.c create mode 100644 net/ipv6/netfilter/nft_reject_ipv6.c
net/ipv4/netfilter/Kconfig +5 -0
@@ -61,6 +61,11 @@ config NFT_CHAIN_NAT_IPV4 packet transformations such as the source, destination address and source and destination ports. +config NFT_REJECT_IPV4 + depends on NF_TABLES_IPV4 + default NFT_REJECT + tristate + config NF_TABLES_ARP depends on NF_TABLES tristate "ARP nf_tables support"
net/ipv4/netfilter/Makefile +1 -0
@@ -30,6 +30,7 @@ obj-$(CONFIG_NF_NAT_PROTO_GRE) += nf_nat_proto_gre.o obj-$(CONFIG_NF_TABLES_IPV4) += nf_tables_ipv4.o obj-$(CONFIG_NFT_CHAIN_ROUTE_IPV4) += nft_chain_route_ipv4.o obj-$(CONFIG_NFT_CHAIN_NAT_IPV4) += nft_chain_nat_ipv4.o +obj-$(CONFIG_NFT_REJECT_IPV4) += nft_reject_ipv4.o obj-$(CONFIG_NF_TABLES_ARP) += nf_tables_arp.o # generic IP tables
net/ipv4/netfilter/nft_reject_ipv4.c new file +74 -0
74 lines not on this page · raw message
net/ipv6/netfilter/Kconfig +5 -0
@@ -50,6 +50,11 @@ config NFT_CHAIN_NAT_IPV6 packet transformations such as the source, destination address and source and destination ports. +config NFT_REJECT_IPV6 + depends on NF_TABLES_IPV6 + default NFT_REJECT + tristate + config IP6_NF_IPTABLES tristate "IP6 tables support (required for filtering)" depends on INET && IPV6
net/ipv6/netfilter/Makefile +1 -0
@@ -27,6 +27,7 @@ obj-$(CONFIG_NF_DEFRAG_IPV6) += nf_defrag_ipv6.o obj-$(CONFIG_NF_TABLES_IPV6) += nf_tables_ipv6.o obj-$(CONFIG_NFT_CHAIN_ROUTE_IPV6) += nft_chain_route_ipv6.o obj-$(CONFIG_NFT_CHAIN_NAT_IPV6) += nft_chain_nat_ipv6.o +obj-$(CONFIG_NFT_REJECT_IPV6) += nft_reject_ipv6.o # matches obj-$(CONFIG_IP6_NF_MATCH_AH) += ip6t_ah.o
net/ipv6/netfilter/nft_reject_ipv6.c new file +75 -0
75 lines not on this page · raw message
net/netfilter/Kconfig +0 -1
@@ -513,7 +513,6 @@ config NFT_QUEUE config NFT_REJECT depends on NF_TABLES - depends on NF_TABLES_IPV6 || !NF_TABLES_IPV6 default m if NETFILTER_ADVANCED=n tristate "Netfilter nf_tables reject support" help
net/netfilter/nft_reject.c +9 -80
119 lines not on this page · raw message
Signature · 1 line
1.8.5.3
Re: [PATCH 4/5] netfilter: nft_reject: split up reject module into IPv4 and IPv6 specifc parts
Quoted 3 lines · Patrick McHardy
Currently the
nft_rejectmodule depends on symbols from ipv6. This is wrong since no generic module should force IPv6 support to be loaded. Split up the module into AF-specific and a generic part.
Applied, thanks. I have included this chunk which was missing:
include/net/netfilter/nft_reject.h new file +18 -0
@@ -0,0 +1,18 @@ +#ifndef _NFT_REJECT_H_ +#define _NFT_REJECT_H_ + +struct nft_reject { +enum nft_reject_types type:8 ; +u8 icmp_code; +u8 family; +}; + +extern const struct nla_policy nft_reject_policy[]; + +int nft_reject_init (const struct nft_ctx *ctx, +const struct nft_expr *expr, +const struct nlattr *const tb[]); + +int nft_reject_dump (struct sk_buff *skb,const struct nft_expr *expr); + +#endif
Re: [PATCH 4/5] netfilter: nft_reject: split up reject module into IPv4 and IPv6 specifc parts
Quoted 6 lines · Pablo Neira Ayuso
Quoted 3 lines · Patrick McHardy
Currently the
nft_rejectmodule depends on symbols from ipv6. This is wrong since no generic module should force IPv6 support to be loaded. Split up the module into AF-specific and a generic part.Applied, thanks. I have included this chunk which was missing:
Oops, sorry.
include/net/netfilter/nft_reject.h line 7
Quoted 11 lines
include/net/netfilter/nft_reject.h new file +7
-0@@ -0,0 +1,18 @@ +#ifndef _NFT_REJECT_H_ +#define _NFT_REJECT_H_ + +struct nft_reject { + enum nft_reject_types type:8; + u8 icmp_code; + u8 family;
Family is actually not needed anymore.
Quoted 11 lines
include/net/netfilter/nft_reject.h new file +11
-0+}; + +extern const struct nla_policy nft_reject_policy[]; + +int nft_reject_init (const struct nft_ctx *ctx, +const struct nft_expr *expr, +const struct nlattr *const tb[]); + +int nft_reject_dump (struct sk_buff *skb,const struct nft_expr *expr); + +#endif
Pablo Neira Ayuso
maintainerRe: [PATCH 4/5] netfilter: nft_reject: split up reject module into IPv4 and IPv6 specifc parts
Quoted 11 lines · Patrick McHardy
Quoted 6 lines · Pablo Neira Ayuso
Quoted 3 lines · Patrick McHardy
Currently the
nft_rejectmodule depends on symbols from ipv6. This is wrong since no generic module should force IPv6 support to be loaded. Split up the module into AF-specific and a generic part.Applied, thanks. I have included this chunk which was missing:
Oops, sorry.
Quoted 1 line
Quoted 12 lines
include/net/netfilter/nft_reject.h line 7
Quoted 10 lines
include/net/netfilter/nft_reject.h new file +7
-0@@ -0,0 +1,18 @@ +#ifndef _NFT_REJECT_H_ +#define _NFT_REJECT_H_ + +struct nft_reject { + enum nft_reject_types type:8; + u8 icmp_code; + u8 family;Family is actually not needed anymore.
Removed, thanks. Will push this shortly.
PATCH 5/5 netfilter: nf_tables: add reject module for NFPROTO_INET
In reply to Patrick McHardyAdd a reject module for NFPROTO_INET. It does nothing but dispatch to the AF-specific modules based on the hook family.
--- net/ipv4/netfilter/nft_reject_ipv4.c | 7 ++-- net/ipv6/netfilter/nft_reject_ipv6.c | 7 ++-- net/netfilter/Kconfig | 5 +++ net/netfilter/Makefile | 1 + net/netfilter/nft_reject_inet.c | 63 ++++++++++++++++++++++++++++++++++++ 5 files changed, 77 insertions(+), 6 deletions(-) create mode 100644 net/netfilter/nft_reject_inet.c
net/ipv4/netfilter/nft_reject_ipv4.c +4 -3
@@ -20,9 +20,9 @@#include <net/netfilter/ipv4/nf_reject.h> #include <net/netfilter/nft_reject.h> -static void nft_reject_ipv4_eval (const struct nft_expr *expr, -struct nft_data data[NFT_REG_MAX +1 ], -const struct nft_pktinfo *pkt) +void nft_reject_ipv4_eval (const struct nft_expr *expr, +struct nft_data data[NFT_REG_MAX +1 ], +const struct nft_pktinfo *pkt) {struct nft_reject *priv =nft_expr_priv (expr);
@@ -37,6 +37,7 @@ static void nft_reject_ipv4_eval(const struct nft_expr *expr, data[NFT_REG_VERDICT ].verdict =NF_DROP ; } +EXPORT_SYMBOL_GPL (nft_reject_ipv4_eval);static struct nft_expr_type nft_reject_ipv4_type;static const struct nft_expr_ops nft_reject_ipv4_ops = {
net/ipv6/netfilter/nft_reject_ipv6.c +4 -3
@@ -19,9 +19,9 @@#include <net/netfilter/nft_reject.h> #include <net/netfilter/ipv6/nf_reject.h> -static void nft_reject_ipv6_eval (const struct nft_expr *expr, -struct nft_data data[NFT_REG_MAX +1 ], -const struct nft_pktinfo *pkt) +void nft_reject_ipv6_eval (const struct nft_expr *expr, +struct nft_data data[NFT_REG_MAX +1 ], +const struct nft_pktinfo *pkt) {struct nft_reject *priv =nft_expr_priv (expr);struct net *net =dev_net ((pkt->in !=NULL ) ? pkt->in : pkt->out);
@@ -38,6 +38,7 @@ static void nft_reject_ipv6_eval(const struct nft_expr *expr, data[NFT_REG_VERDICT ].verdict =NF_DROP ; } +EXPORT_SYMBOL_GPL (nft_reject_ipv6_eval);static struct nft_expr_type nft_reject_ipv6_type;static const struct nft_expr_ops nft_reject_ipv6_ops = {
net/netfilter/Kconfig +5 -0
@@ -520,6 +520,11 @@ config NFT_REJECT explicitly deny and notify via TCP reset/ICMP informational errors unallowed traffic. +config NFT_REJECT_INET + depends on NF_TABLES_INET + default NFT_REJECT + tristate + config NFT_COMPAT depends on NF_TABLES depends on NETFILTER_XTABLES
net/netfilter/Makefile +1 -0
@@ -79,6 +79,7 @@ obj-$(CONFIG_NFT_LIMIT) += nft_limit.o obj-$(CONFIG_NFT_NAT) += nft_nat.o obj-$(CONFIG_NFT_QUEUE) += nft_queue.o obj-$(CONFIG_NFT_REJECT) += nft_reject.o +obj-$(CONFIG_NFT_REJECT_INET) += nft_reject_inet.o obj-$(CONFIG_NFT_RBTREE) += nft_rbtree.o obj-$(CONFIG_NFT_HASH) += nft_hash.o obj-$(CONFIG_NFT_COUNTER) += nft_counter.o
net/netfilter/nft_reject_inet.c new file +63 -0
63 lines not on this page · raw message
Signature · 1 line
1.8.5.3
Pablo Neira Ayuso
maintainerRe: [PATCH 5/5] netfilter: nf_tables: add reject module for NFPROTO_INET
Quoted 2 lines · Patrick McHardy
Add a reject module for
NFPROTO_INET. It does nothing but dispatch to the AF-specific modules based on the hook family.
And also applied, including this chunk:
(quoted diff) +0 -0
b/include/net/netfilter/nft_reject.h
index 6ade783..72a18c0 100644
include/net/netfilter/nft_reject.h +0 -0
@@ -15,4 +15,12 @@ int nft_reject_init(const struct nft_ctx *ctx, int nft_reject_dump(struct sk_buff *skb, const struct nft_expr
*expr);
+void nft_reject_ipv4_eval(const struct nft_expr *expr,
+ struct nft_data data[NFT_REG_MAX + 1],
+ const struct nft_pktinfo *pkt);
+
+void nft_reject_ipv6_eval(const struct nft_expr *expr,
+ struct nft_data data[NFT_REG_MAX + 1],
+ const struct nft_pktinfo *pkt);
+
#endif
thanks Patrick!