Skip to contentSkip to conversation

netfilter: ensure rcu_read_lock() in ipv4_find_option()

PATCH In review Matteo Croce 2 participants 4 messages Feb 24, 2020

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 netfilter: ensure rcu_read_lock() in ipv4_find_option()

As in commit c543cb4a5f07 ("ipv4: ensure rcu_read_lock() in ipv4_link_failure()") and commit 3e72dfdf8227 ("ipv4: ensure rcu_read_lock() in cipso_v4_error()"), __ip_options_compile() must be called under rcu protection.

Fixes dbb5281a1f84 ("netfilter: nf_tables: add support for matching IPv4 options")
---
 net/netfilter/nft_exthdr.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
net/netfilter/nft_exthdr.c +6 -1
index a5e8469859e3..752264b3043a 100644
@@ -77,6 +77,7 @@ static int ipv4_find_option(struct net *net, struct sk_buff *skb,
 	bool found = false;
 	__be32 info;
 	int optlen;
+	int ret;
 
 	iph = skb_header_pointer(skb, 0, sizeof(_iph), &_iph);
 	if (!iph)
@@ -95,7 +96,11 @@ static int ipv4_find_option(struct net *net, struct sk_buff *skb,
 		return -EBADMSG;
 	opt->optlen = optlen;
 
-	if (__ip_options_compile(net, opt, NULL, &info))
+	rcu_read_lock();
+	ret = __ip_options_compile(net, opt, NULL, &info);
+	rcu_read_unlock();
+
+	if (ret)
 		return -EBADMSG;
 
 	switch (target) {
Signature · 1 line
2.24.1
maintainer
#1 lore raw
Quoted 3 lines · Matteo Croce

As in commit c543cb4a5f07 ("ipv4: ensure rcu_read_lock() in ipv4_link_failure()") and commit 3e72dfdf8227 ("ipv4: ensure rcu_read_lock() in cipso_v4_error()"), __ip_options_compile() must be called under rcu protection.

This is not needed, all netfilter hooks run with rcu_read_lock held.

#2 lore raw
Quoted 8 lines · Florian Westphal
Quoted 3 lines · Matteo Croce

As in commit c543cb4a5f07 ("ipv4: ensure rcu_read_lock() in ipv4_link_failure()") and commit 3e72dfdf8227 ("ipv4: ensure rcu_read_lock() in cipso_v4_error()"), __ip_options_compile() must be called under rcu protection.

This is not needed, all netfilter hooks run with rcu_read_lock held.

Ok, so let's drop it, thanks.

Signature · 2 lines
Matteo Croce
per aspera ad upstream
#3 lore raw
Quoted 4 lines · Matteo Croce
Quoted 2 lines · Florian Westphal

Matteo Croce <[email protected]> wrote:

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

As in commit c543cb4a5f07 ("ipv4: ensure rcu_read_lock() in ipv4_link_failure()") and commit 3e72dfdf8227 ("ipv4: ensure rcu_read_lock() in cipso_v4_error()"), __ip_options_compile() must be called under rcu protection.

This is not needed, all netfilter hooks run with rcu_read_lock held.

Ok, so let's drop it, thanks.

What about adding a RCU_LOCKDEP_WARN() in __ip_options_compile() to protect against future errors? Something like:

----------------------------------%<------------------------------------- @@ -262,6 +262,9 @@ int __ip_options_compile(struct net *net,
unsigned char *iph;
int optlen, l;

+ RCU_LOCKDEP_WARN(!rcu_read_lock_held(),
+ __FUNC__ " needs rcu_read_lock() protection");
+
if (skb) {
rt = skb_rtable(skb);
optptr = (unsigned char *)&(ip_hdr(skb)[1]);
---------------------------------->%-------------------------------------

Bye,

Signature · 2 lines
Matteo Croce
per aspera ad upstream