netfilter: nf_tables: fix error handling of rule replacement
Subsystems (3)
Pablo Neira Ayuso
maintainerPATCH 1/1 netfilter: nf_tables: fix error handling of rule replacement
[ upstream commit 59900e0a019e7c2bdb7809a03ed5742d311b15b3 ]
In general, if a transaction object is added to the list successfully, we can rely on the abort path to undo what we've done. This allows us to simplify the error handling of the rule replacement path in nf_tables_newrule().
This implicitly fixes an unnecessary removal of the old rule, which needs to be left in place if we fail to replace.
--- net/netfilter/nf_tables_api.c | 6 ------ 1 file changed, 6 deletions(-)
net/netfilter/nf_tables_api.c +0 -6
@@ -2045,12 +2045,6 @@ static int nf_tables_newrule(struct sock *nlsk, struct sk_buff *skb, err3:list_del_rcu (&rule->list); -if (trans) { -list_del_rcu (&nft_trans_rule (trans)->list); -nft_rule_clear (net,nft_trans_rule (trans)); -nft_trans_destroy (trans); - chain->use++; - } err2:nf_tables_rule_destroy (&ctx, rule); err1:
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 1/1 netfilter: Zero the tuple in nfnl_cthelper_parse_tuple()
From: Ian Wilson <[email protected]>
[ upstream commit 78146572b9cd20452da47951812f35b1ad4906be ]
nfnl_cthelper_parse_tuple() is called from nfnl_cthelper_new(), nfnl_cthelper_get() and nfnl_cthelper_del(). In each case they pass a pointer to an nf_conntrack_tuple data structure local variable:
struct nf_conntrack_tuple tuple;
...
ret = nfnl_cthelper_parse_tuple(&tuple, tb[NFCTH_TUPLE]);The problem is that this local variable is not initialized, and nfnl_cthelper_parse_tuple() only initializes two fields: src.l3num and dst.protonum. This leaves all other fields with undefined values based on whatever is on the stack:
tuple->src.l3num = ntohs(nla_get_be16(tb[NFCTH_TUPLE_L3PROTONUM]));
tuple->dst.protonum = nla_get_u8(tb[NFCTH_TUPLE_L4PROTONUM]);The symptom observed was that when the rpc and tns helpers were added then traffic to port 1536 was being sent to user-space.
--- net/netfilter/nfnetlink_cthelper.c | 3 +++ 1 file changed, 3 insertions(+)
net/netfilter/nfnetlink_cthelper.c +3 -0
@@ -77,6 +77,9 @@ nfnl_cthelper_parse_tuple(struct nf_conntrack_tuple *tuple,if (!tb[NFCTH_TUPLE_L3PROTONUM ] || !tb[NFCTH_TUPLE_L4PROTONUM ])return -EINVAL ; +/* Not all fields are initialized so first zero the tuple */ +memset (tuple,0 ,sizeof (struct nf_conntrack_tuple )); + tuple->src.l3num =ntohs (nla_get_be16 (tb[NFCTH_TUPLE_L3PROTONUM ])); tuple->dst.protonum =nla_get_u8 (tb[NFCTH_TUPLE_L4PROTONUM ]);
Signature · 1 line
1.7.10.4
Re: [PATCH -stable] netfilter: Zero the tuple in nfnl_cthelper_parse_tuple()
Quoted 17 lines · Pablo Neira Ayuso
From: Ian Wilson <[email protected]>
[ upstream commit
78146572b9cd20452da47951812f35b1ad4906be]
nfnl_cthelper_parse_tuple()is called fromnfnl_cthelper_new(),nfnl_cthelper_get()andnfnl_cthelper_del(). In each case they pass a pointer to annf_conntrack_tupledata structure local variable:struct nf_conntrack_tuple tuple; ... ret = nfnl_cthelper_parse_tuple(&tuple, tb[NFCTH_TUPLE]);The problem is that this local variable is not initialized, and
nfnl_cthelper_parse_tuple()only initializes two fields: src.l3num and dst.protonum. This leaves all other fields with undefined values based on whatever is on the stack:
Quoted 8 lines
tuple->src.l3num = ntohs(nla_get_be16(tb[NFCTH_TUPLE_L3PROTONUM])); tuple->dst.protonum = nla_get_u8(tb[NFCTH_TUPLE_L4PROTONUM]);The symptom observed was that when the rpc and tns helpers were added then traffic to port 1536 was being sent to user-space.
Cc <[email protected]> # 3.10.xCc <[email protected]> # 3.12.x
Applied only this one to 3.12 (others are not marked as such). Thanks.
Quoted 24 lines
Cc <[email protected]> # 3.14.xCc <[email protected]> # 3.18.xCc <[email protected]> # 3.19.xSigned-off-by Ian Wilson <[email protected]>Signed-off-by Pablo Neira Ayuso <[email protected]>--- net/netfilter/nfnetlink_cthelper.c | 3 +++ 1 file changed, 3 insertions(+)net/netfilter/nfnetlink_cthelper.c +3
-0@@ -77,6 +77,9 @@ nfnl_cthelper_parse_tuple(struct nf_conntrack_tuple *tuple,if (!tb[NFCTH_TUPLE_L3PROTONUM ] || !tb[NFCTH_TUPLE_L4PROTONUM ])return -EINVAL ; +/* Not all fields are initialized so first zero the tuple */ +memset (tuple,0 ,sizeof (struct nf_conntrack_tuple )); + tuple->src.l3num =ntohs (nla_get_be16 (tb[NFCTH_TUPLE_L3PROTONUM ])); tuple->dst.protonum =nla_get_u8 (tb[NFCTH_TUPLE_L4PROTONUM ]);
Signature · 2 lines
js suse labs
Pablo Neira Ayuso
maintainerPATCH 1/1 netfilter: nf_tables: check for overflow of rule dlen field
In reply to Pablo Neira AyusoFrom: Patrick McHardy <[email protected]>
[ upstream commit 9889840f5988ecfd43b00c9abb83c1804e21406b ]
Check that the space required for the expressions doesn't exceed the size of the dlen field, which would lead to the iterators crashing.
--- net/netfilter/nf_tables_api.c | 4 ++++ 1 file changed, 4 insertions(+)
net/netfilter/nf_tables_api.c +4 -0
@@ -1968,6 +1968,10 @@ static int nf_tables_newrule(struct sock *nlsk, struct sk_buff *skb, n++; } } +/* Check for overflow of dlen field */ + err = -EFBIG ; +if (size >=1 <<12 ) + goto err1;if (nla[NFTA_RULE_USERDATA ]) ulen =nla_len (nla[NFTA_RULE_USERDATA ]);
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 1/1 netfilter: nft_compat: set IP6T_F_PROTO flag if protocol is set
In reply to Pablo Neira Ayuso[ upstream commit 749177ccc74f9c6d0f51bd78a15c652a2134aa11 ]
ip6tables extensions check for this flag to restrict match/target to a given protocol. Without this flag set, SYNPROXY6 returns an error.
--- net/netfilter/nft_compat.c | 6 ++++++ 1 file changed, 6 insertions(+)
net/netfilter/nft_compat.c +6 -0
@@ -133,6 +133,9 @@ nft_target_set_tgchk_param(struct xt_tgchk_param *par, entry->e4.ip.invflags = inv ?IPT_INV_PROTO :0 ;break ;case AF_INET6 : +if (proto) + entry->e6.ipv6.flags |=IP6T_F_PROTO ; + entry->e6.ipv6.proto = proto; entry->e6.ipv6.invflags = inv ?IP6T_INV_PROTO :0 ;break ;
@@ -344,6 +347,9 @@ nft_match_set_mtchk_param(struct xt_mtchk_param *par, const struct nft_ctx *ctx, entry->e4.ip.invflags = inv ?IPT_INV_PROTO :0 ;break ;case AF_INET6 : +if (proto) + entry->e6.ipv6.flags |=IP6T_F_PROTO ; + entry->e6.ipv6.proto = proto; entry->e6.ipv6.invflags = inv ?IP6T_INV_PROTO :0 ;break ;
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 1/1 netfilter: restore rule tracing via nfnetlink_log
In reply to Pablo Neira Ayuso[ upstream commit 4017a7ee693d1cae6735c0dac21594a7c6416c4c ]
Since fab4085 ("netfilter: log: nf_log_packet() as real unified interface"), the loginfo structure that is passed to nf_log_packet() is used to explicitly indicate the logger type you want to use.
This is a problem for people tracing rules through nfnetlink_log since packets are always routed to the NF_LOG_TYPE logger after the aforementioned patch.
We can fix this by removing the trace loginfo structures, but that still changes the log level from 4 to 5 for tracing messages and there may be someone relying on this outthere. So let's just introduce a new nf_log_trace() function that restores the former behaviour.
--- include/net/netfilter/nf_log.h | 10 ++++++++++ net/ipv4/netfilter/ip_tables.c | 6 +++--- net/ipv6/netfilter/ip6_tables.c | 6 +++--- net/netfilter/nf_log.c | 24 ++++++++++++++++++++++++ net/netfilter/nf_tables_core.c | 8 ++++---- 5 files changed, 44 insertions(+), 10 deletions(-)
include/net/netfilter/nf_log.h +10 -0
@@ -79,6 +79,16 @@ void nf_log_packet(struct net *net,const struct nf_loginfo *li, const char *fmt, ...); +__printf(8, 9) +void nf_log_trace (struct net *net, +u_int8_t pf, +unsigned int hooknum, +const struct sk_buff *skb, +const struct net_device *in, +const struct net_device *out, +const struct nf_loginfo *li, +const char *fmt, ...); +struct nf_log_buf ;struct nf_log_buf *nf_log_buf_open (void );
net/ipv4/netfilter/ip_tables.c +3 -3
@@ -272,9 +272,9 @@ static void trace_packet(const struct sk_buff *skb, &chainname, &comment, &rulenum) != 0)break ; -nf_log_packet (net,AF_INET , hook, skb, in, out, &trace_loginfo, -"TRACE: %s:%s:%s:%u " , - tablename, chainname, comment, rulenum); +nf_log_trace (net,AF_INET , hook, skb, in, out, &trace_loginfo, +"TRACE: %s:%s:%s:%u " , + tablename, chainname, comment, rulenum); }#endif
net/ipv6/netfilter/ip6_tables.c +3 -3
@@ -298,9 +298,9 @@ static void trace_packet(const struct sk_buff *skb, &chainname, &comment, &rulenum) != 0)break ; -nf_log_packet (net,AF_INET6 , hook, skb, in, out, &trace_loginfo, -"TRACE: %s:%s:%s:%u " , - tablename, chainname, comment, rulenum); +nf_log_trace (net,AF_INET6 , hook, skb, in, out, &trace_loginfo, +"TRACE: %s:%s:%s:%u " , + tablename, chainname, comment, rulenum); }#endif
net/netfilter/nf_log.c +24 -0
@@ -212,6 +212,30 @@ void nf_log_packet(struct net *net, }EXPORT_SYMBOL (nf_log_packet); +void nf_log_trace (struct net *net, +u_int8_t pf, +unsigned int hooknum, +const struct sk_buff *skb, +const struct net_device *in, +const struct net_device *out, +const struct nf_loginfo *loginfo,const char *fmt, ...) +{ +va_list args; +char prefix[NF_LOG_PREFIXLEN ]; +const struct nf_logger *logger; + +rcu_read_lock (); + logger =rcu_dereference (net->nf.nf_loggers[pf]); +if (logger) { +va_start (args, fmt); +vsnprintf (prefix,sizeof (prefix), fmt, args); +va_end (args); + logger->logfn (net, pf, hooknum, skb, in, out, loginfo, prefix); + } +rcu_read_unlock (); +} +EXPORT_SYMBOL (nf_log_trace); +#define S_SIZE (1024 - (sizeof(unsigned int) + 1))struct nf_log_buf {
net/netfilter/nf_tables_core.c +4 -4
@@ -94,10 +94,10 @@ static void nft_trace_packet(const struct nft_pktinfo *pkt, {struct net *net =dev_net (pkt->in ? pkt->in : pkt->out); -nf_log_packet (net, pkt->xt.family, pkt->ops->hooknum, pkt->skb, pkt->in, - pkt->out, &trace_loginfo,"TRACE: %s:%s:%s:%u " , - chain->table->name, chain->name, comments[type], - rulenum); +nf_log_trace (net, pkt->xt.family, pkt->ops->hooknum, pkt->skb, pkt->in, + pkt->out, &trace_loginfo,"TRACE: %s:%s:%s:%u " , + chain->table->name, chain->name, comments[type], + rulenum); }unsigned int
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 1/1 netfilter: nf_tables: allow to change chain policy without hook if it exists
In reply to Pablo Neira Ayuso[ upstream commit d6b6cb1d3e6f78d55c2d4043d77d0d8def3f3b99 ]
If there's an existing base chain, we have to allow to change the default policy without indicating the hook information.
However, if the chain doesn't exists, we have to enforce the presence of the hook attribute.
--- net/netfilter/nf_tables_api.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
net/netfilter/nf_tables_api.c +4 -1
@@ -1225,7 +1225,10 @@ static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb,if (nla[NFTA_CHAIN_POLICY ]) {if ((chain !=NULL && - !(chain->flags &NFT_BASE_CHAIN )) || + !(chain->flags &NFT_BASE_CHAIN ))) +return -EOPNOTSUPP ; + +if (chain ==NULL && nla[NFTA_CHAIN_HOOK ] ==NULL )return -EOPNOTSUPP ;
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 1/1 netfilter: nft_rbtree: fix locking
In reply to Pablo Neira AyusoFrom: Patrick McHardy <[email protected]>
[ upstream commit 16c45eda96038aae848b6cfd42e2bf4b5e80f365 ]
Fix a race condition and unnecessary locking:
* the root rb_node must only be accessed under the lock in nft_rbtree_lookup()
* the lock is not needed in lookup functions in netlink context
--- net/netfilter/nft_rbtree.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
net/netfilter/nft_rbtree.c +2 -4
@@ -37,10 +37,11 @@ static bool nft_rbtree_lookup(const struct nft_set *set, {const struct nft_rbtree *priv =nft_set_priv (set);const struct nft_rbtree_elem *rbe, *interval =NULL ; -const struct rb_node *parent = priv->root.rb_node; +const struct rb_node *parent;int d;spin_lock_bh (&nft_rbtree_lock); + parent = priv->root.rb_node;while (parent !=NULL ) { rbe = rb_entry(parent, struct nft_rbtree_elem, node);
@@ -158,7 +159,6 @@ static int nft_rbtree_get(const struct nft_set *set, struct nft_set_elem *elem)struct nft_rbtree_elem *rbe;int d; -spin_lock_bh (&nft_rbtree_lock);while (parent !=NULL ) { rbe = rb_entry(parent, struct nft_rbtree_elem, node);
@@ -173,11 +173,9 @@ static int nft_rbtree_get(const struct nft_set *set, struct nft_set_elem *elem) !(rbe->flags & NFT_SET_ELEM_INTERVAL_END))nft_data_copy (&elem->data, rbe->data); elem->flags = rbe->flags; -spin_unlock_bh (&nft_rbtree_lock);return 0 ; } } -spin_unlock_bh (&nft_rbtree_lock);return -ENOENT ; }
Signature · 1 line
1.7.10.4
Greg KH
maintainerRe: [PATCH -stable] netfilter: nft_rbtree: fix locking
Quoted 9 lines · Pablo Neira Ayuso
From: Patrick McHardy <[email protected]>
[ upstream commit
16c45eda96038aae848b6cfd42e2bf4b5e80f365]Fix a race condition and unnecessary locking:
* the root
rb_nodemust only be accessed under the lock innft_rbtree_lookup()
* the lock is not needed in lookup functions in netlink context
Quoted 7 lines
Cc <[email protected]> # 3.18.xCc <[email protected]> # 3.19.xSigned-off-by Patrick McHardy <[email protected]>Signed-off-by Pablo Neira Ayuso <[email protected]>--- net/netfilter/nft_rbtree.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
Also 4.0, right? This showed up in Linus's tree in 4.1-rc1.
thanks,
greg k-h
Pablo Neira Ayuso
maintainerRe: [PATCH -stable] netfilter: nft_rbtree: fix locking
Quoted 18 lines · Greg KH
Quoted 9 lines · Pablo Neira Ayuso
From: Patrick McHardy <[email protected]>
[ upstream commit
16c45eda96038aae848b6cfd42e2bf4b5e80f365]Fix a race condition and unnecessary locking:
* the root
rb_nodemust only be accessed under the lock innft_rbtree_lookup()
* the lock is not needed in lookup functions in netlink contextQuoted 7 lines
Cc <[email protected]> # 3.18.xCc <[email protected]> # 3.19.xSigned-off-by Patrick McHardy <[email protected]>Signed-off-by Pablo Neira Ayuso <[email protected]>--- net/netfilter/nft_rbtree.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
Quoted 1 line
Also 4.0, right? This showed up in Linus's tree in 4.1-rc1.
Yes please. thanks!
Quoted 2 lines · Pablo Neira Ayuso
[ upstream commit
59900e0a019e7c2bdb7809a03ed5742d311b15b3]
Thank you, Pablo.
I've took a look at these 7 patches, and most of them seem to be applicable to the 3.16 kernel as well. I'm queuing the following set for the next 3.16 release:
59900e0a019e ("netfilter: nf_tables: fix error handling of rule replacement") 78146572b9cd ("netfilter: Zero the tuple in nfnl_cthelper_parse_tuple()") 9889840f5988 ("netfilter: nf_tables: check for overflow of rule dlen field") 749177ccc74f ("netfilter: nft_compat: set IP6T_F_PROTO flag if protocol is set") d6b6cb1d3e6f ("netfilter: nf_tables: allow to change chain policy without hook if it exists") 16c45eda9603 ("netfilter: nft_rbtree: fix locking")
So, from your initial set, I've dropped only 4017a7ee693d ("netfilter:
restore rule tracing via nfnetlink_log").
Cheers,
--
Luís