diff mbox series

[net-next] phonet: do not call synchronize_rcu() from phonet_route_del()

Message ID 20241104152622.3580037-1-edumazet@google.com (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series [net-next] phonet: do not call synchronize_rcu() from phonet_route_del() | expand

Checks

Context Check Description
netdev/series_format success Single patches do not need cover letters
netdev/tree_selection success Clearly marked for net-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 3 this patch: 3
netdev/build_tools success No tools touched, skip
netdev/cc_maintainers warning 1 maintainers not CCed: horms@kernel.org
netdev/build_clang success Errors and warnings before: 3 this patch: 3
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success Fixes tag looks correct
netdev/build_allmodconfig_warn success Errors and warnings before: 4 this patch: 4
netdev/checkpatch warning WARNING: Possible repeated word: 'Google'
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
netdev/contest success net-next-2024-11-04--18-00 (tests: 781)

Commit Message

Eric Dumazet Nov. 4, 2024, 3:26 p.m. UTC
Calling synchronize_rcu() while holding rcu_read_lock() is not
permitted [1]

Move the synchronize_rcu() to route_doit().

[1]
WARNING: suspicious RCU usage
6.12.0-rc5-syzkaller-01056-gf07a6e6ceb05 #0 Not tainted
-----------------------------
kernel/rcu/tree.c:4092 Illegal synchronize_rcu() in RCU read-side critical section!

other info that might help us debug this:

rcu_scheduler_active = 2, debug_locks = 1
1 lock held by syz-executor427/5840:
  #0: ffffffff8e937da0 (rcu_read_lock){....}-{1:2}, at: rcu_lock_acquire include/linux/rcupdate.h:337 [inline]
  #0: ffffffff8e937da0 (rcu_read_lock){....}-{1:2}, at: rcu_read_lock include/linux/rcupdate.h:849 [inline]
  #0: ffffffff8e937da0 (rcu_read_lock){....}-{1:2}, at: route_doit+0x3d6/0x640 net/phonet/pn_netlink.c:264

stack backtrace:
CPU: 1 UID: 0 PID: 5840 Comm: syz-executor427 Not tainted 6.12.0-rc5-syzkaller-01056-gf07a6e6ceb05 #0
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 09/13/2024
Call Trace:
 <TASK>
  __dump_stack lib/dump_stack.c:94 [inline]
  dump_stack_lvl+0x241/0x360 lib/dump_stack.c:120
  lockdep_rcu_suspicious+0x226/0x340 kernel/locking/lockdep.c:6821
  synchronize_rcu+0xea/0x360 kernel/rcu/tree.c:4089
  phonet_route_del+0xc6/0x140 net/phonet/pn_dev.c:409
  route_doit+0x514/0x640 net/phonet/pn_netlink.c:275
  rtnetlink_rcv_msg+0x791/0xcf0 net/core/rtnetlink.c:6790
  netlink_rcv_skb+0x1e3/0x430 net/netlink/af_netlink.c:2551
  netlink_unicast_kernel net/netlink/af_netlink.c:1331 [inline]
  netlink_unicast+0x7f6/0x990 net/netlink/af_netlink.c:1357
  netlink_sendmsg+0x8e4/0xcb0 net/netlink/af_netlink.c:1901
  sock_sendmsg_nosec net/socket.c:729 [inline]
  __sock_sendmsg+0x221/0x270 net/socket.c:744
  sock_write_iter+0x2d7/0x3f0 net/socket.c:1165
  new_sync_write fs/read_write.c:590 [inline]
  vfs_write+0xaeb/0xd30 fs/read_write.c:683
  ksys_write+0x183/0x2b0 fs/read_write.c:736
  do_syscall_x64 arch/x86/entry/common.c:52 [inline]
  do_syscall_64+0xf3/0x230 arch/x86/entry/common.c:83
 entry_SYSCALL_64_after_hwframe+0x77/0x7f

Fixes: 17a1ac0018ae ("phonet: Don't hold RTNL for route_doit().")
Reported-by: syzbot <syzkaller@googlegroups.com>
Signed-off-by: Eric Dumazet <edumazet@google.com>
Cc: Kuniyuki Iwashima <kuniyu@amazon.com>
Cc: Remi Denis-Courmont <courmisch@gmail.com>
---
 net/phonet/pn_dev.c     |  4 +++-
 net/phonet/pn_netlink.c | 10 ++++++++--
 2 files changed, 11 insertions(+), 3 deletions(-)

Comments

Kuniyuki Iwashima Nov. 4, 2024, 8:40 p.m. UTC | #1
From: Eric Dumazet <edumazet@google.com>
Date: Mon,  4 Nov 2024 15:26:22 +0000
> Calling synchronize_rcu() while holding rcu_read_lock() is not
> permitted [1]

Thanks for catching this !

> 
> Move the synchronize_rcu() to route_doit().
> 
> [1]
> WARNING: suspicious RCU usage
> 6.12.0-rc5-syzkaller-01056-gf07a6e6ceb05 #0 Not tainted
> -----------------------------
> kernel/rcu/tree.c:4092 Illegal synchronize_rcu() in RCU read-side critical section!
> 
> other info that might help us debug this:
> 
> rcu_scheduler_active = 2, debug_locks = 1
> 1 lock held by syz-executor427/5840:
>   #0: ffffffff8e937da0 (rcu_read_lock){....}-{1:2}, at: rcu_lock_acquire include/linux/rcupdate.h:337 [inline]
>   #0: ffffffff8e937da0 (rcu_read_lock){....}-{1:2}, at: rcu_read_lock include/linux/rcupdate.h:849 [inline]
>   #0: ffffffff8e937da0 (rcu_read_lock){....}-{1:2}, at: route_doit+0x3d6/0x640 net/phonet/pn_netlink.c:264
> 
> stack backtrace:
> CPU: 1 UID: 0 PID: 5840 Comm: syz-executor427 Not tainted 6.12.0-rc5-syzkaller-01056-gf07a6e6ceb05 #0
> Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 09/13/2024
> Call Trace:
>  <TASK>
>   __dump_stack lib/dump_stack.c:94 [inline]
>   dump_stack_lvl+0x241/0x360 lib/dump_stack.c:120
>   lockdep_rcu_suspicious+0x226/0x340 kernel/locking/lockdep.c:6821
>   synchronize_rcu+0xea/0x360 kernel/rcu/tree.c:4089
>   phonet_route_del+0xc6/0x140 net/phonet/pn_dev.c:409
>   route_doit+0x514/0x640 net/phonet/pn_netlink.c:275
>   rtnetlink_rcv_msg+0x791/0xcf0 net/core/rtnetlink.c:6790
>   netlink_rcv_skb+0x1e3/0x430 net/netlink/af_netlink.c:2551
>   netlink_unicast_kernel net/netlink/af_netlink.c:1331 [inline]
>   netlink_unicast+0x7f6/0x990 net/netlink/af_netlink.c:1357
>   netlink_sendmsg+0x8e4/0xcb0 net/netlink/af_netlink.c:1901
>   sock_sendmsg_nosec net/socket.c:729 [inline]
>   __sock_sendmsg+0x221/0x270 net/socket.c:744
>   sock_write_iter+0x2d7/0x3f0 net/socket.c:1165
>   new_sync_write fs/read_write.c:590 [inline]
>   vfs_write+0xaeb/0xd30 fs/read_write.c:683
>   ksys_write+0x183/0x2b0 fs/read_write.c:736
>   do_syscall_x64 arch/x86/entry/common.c:52 [inline]
>   do_syscall_64+0xf3/0x230 arch/x86/entry/common.c:83
>  entry_SYSCALL_64_after_hwframe+0x77/0x7f
> 
> Fixes: 17a1ac0018ae ("phonet: Don't hold RTNL for route_doit().")
> Reported-by: syzbot <syzkaller@googlegroups.com>
> Signed-off-by: Eric Dumazet <edumazet@google.com>
> Cc: Kuniyuki Iwashima <kuniyu@amazon.com>
> Cc: Remi Denis-Courmont <courmisch@gmail.com>
> ---
>  net/phonet/pn_dev.c     |  4 +++-
>  net/phonet/pn_netlink.c | 10 ++++++++--
>  2 files changed, 11 insertions(+), 3 deletions(-)
> 
> diff --git a/net/phonet/pn_dev.c b/net/phonet/pn_dev.c
> index 19234d664c4fb537eba0267266efbb226cf103c3..578d935f2b11694fd1004c5f854ec344b846eeb2 100644
> --- a/net/phonet/pn_dev.c
> +++ b/net/phonet/pn_dev.c
> @@ -406,7 +406,9 @@ int phonet_route_del(struct net_device *dev, u8 daddr)
>  
>  	if (!dev)
>  		return -ENOENT;
> -	synchronize_rcu();
> +
> +	/* Note : our caller must call synchronize_rcu() */
> +
>  	dev_put(dev);

Are these synchronize_rcu() + dev_put() paired with rcu_read_lock()
and dev_hold() in phonet_route_output() ?

If so, we need to move dev_put() too or maybe we can remove
synchronize_rcu() here and replace rcu_read_lock() with
spin_lock(&routes->lock) in phonet_route_output().


>  	return 0;
>  }
> diff --git a/net/phonet/pn_netlink.c b/net/phonet/pn_netlink.c
> index ca1f04e4a2d9eb3b2a6d6cc5b299aee28d569b08..24930733ac572ed3ec5fd142d347c115346a28fa 100644
> --- a/net/phonet/pn_netlink.c
> +++ b/net/phonet/pn_netlink.c
> @@ -233,6 +233,7 @@ static int route_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
>  {
>  	struct net *net = sock_net(skb->sk);
>  	struct nlattr *tb[RTA_MAX+1];
> +	bool sync_needed = false;
>  	struct net_device *dev;
>  	struct rtmsg *rtm;
>  	u32 ifindex;
> @@ -269,16 +270,21 @@ static int route_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
>  		return -ENODEV;
>  	}
>  
> -	if (nlh->nlmsg_type == RTM_NEWROUTE)
> +	if (nlh->nlmsg_type == RTM_NEWROUTE) {
>  		err = phonet_route_add(dev, dst);
> -	else
> +	} else {
>  		err = phonet_route_del(dev, dst);
> +		if (!err)
> +			sync_needed = true;
> +	}
>  
>  	rcu_read_unlock();
>  
>  	if (!err)
>  		rtm_phonet_notify(net, nlh->nlmsg_type, ifindex, dst);
>  
> +	if (sync_needed)
> +		synchronize_rcu();
>  	return err;
>  }
>  
> -- 
> 2.47.0.163.g1226f6d8fa-goog
Eric Dumazet Nov. 5, 2024, 8:46 a.m. UTC | #2
On Mon, Nov 4, 2024 at 9:40 PM Kuniyuki Iwashima <kuniyu@amazon.com> wrote:
>
> From: Eric Dumazet <edumazet@google.com>
> Date: Mon,  4 Nov 2024 15:26:22 +0000
> > Calling synchronize_rcu() while holding rcu_read_lock() is not
> > permitted [1]
>
> Thanks for catching this !
>
> >
> > Move the synchronize_rcu() to route_doit().
> >
> > [1]
> > WARNING: suspicious RCU usage
> > 6.12.0-rc5-syzkaller-01056-gf07a6e6ceb05 #0 Not tainted
> > -----------------------------
> > kernel/rcu/tree.c:4092 Illegal synchronize_rcu() in RCU read-side critical section!
> >
> > other info that might help us debug this:
> >
> > rcu_scheduler_active = 2, debug_locks = 1
> > 1 lock held by syz-executor427/5840:
> >   #0: ffffffff8e937da0 (rcu_read_lock){....}-{1:2}, at: rcu_lock_acquire include/linux/rcupdate.h:337 [inline]
> >   #0: ffffffff8e937da0 (rcu_read_lock){....}-{1:2}, at: rcu_read_lock include/linux/rcupdate.h:849 [inline]
> >   #0: ffffffff8e937da0 (rcu_read_lock){....}-{1:2}, at: route_doit+0x3d6/0x640 net/phonet/pn_netlink.c:264
> >
> > stack backtrace:
> > CPU: 1 UID: 0 PID: 5840 Comm: syz-executor427 Not tainted 6.12.0-rc5-syzkaller-01056-gf07a6e6ceb05 #0
> > Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 09/13/2024
> > Call Trace:
> >  <TASK>
> >   __dump_stack lib/dump_stack.c:94 [inline]
> >   dump_stack_lvl+0x241/0x360 lib/dump_stack.c:120
> >   lockdep_rcu_suspicious+0x226/0x340 kernel/locking/lockdep.c:6821
> >   synchronize_rcu+0xea/0x360 kernel/rcu/tree.c:4089
> >   phonet_route_del+0xc6/0x140 net/phonet/pn_dev.c:409
> >   route_doit+0x514/0x640 net/phonet/pn_netlink.c:275
> >   rtnetlink_rcv_msg+0x791/0xcf0 net/core/rtnetlink.c:6790
> >   netlink_rcv_skb+0x1e3/0x430 net/netlink/af_netlink.c:2551
> >   netlink_unicast_kernel net/netlink/af_netlink.c:1331 [inline]
> >   netlink_unicast+0x7f6/0x990 net/netlink/af_netlink.c:1357
> >   netlink_sendmsg+0x8e4/0xcb0 net/netlink/af_netlink.c:1901
> >   sock_sendmsg_nosec net/socket.c:729 [inline]
> >   __sock_sendmsg+0x221/0x270 net/socket.c:744
> >   sock_write_iter+0x2d7/0x3f0 net/socket.c:1165
> >   new_sync_write fs/read_write.c:590 [inline]
> >   vfs_write+0xaeb/0xd30 fs/read_write.c:683
> >   ksys_write+0x183/0x2b0 fs/read_write.c:736
> >   do_syscall_x64 arch/x86/entry/common.c:52 [inline]
> >   do_syscall_64+0xf3/0x230 arch/x86/entry/common.c:83
> >  entry_SYSCALL_64_after_hwframe+0x77/0x7f
> >
> > Fixes: 17a1ac0018ae ("phonet: Don't hold RTNL for route_doit().")
> > Reported-by: syzbot <syzkaller@googlegroups.com>
> > Signed-off-by: Eric Dumazet <edumazet@google.com>
> > Cc: Kuniyuki Iwashima <kuniyu@amazon.com>
> > Cc: Remi Denis-Courmont <courmisch@gmail.com>
> > ---
> >  net/phonet/pn_dev.c     |  4 +++-
> >  net/phonet/pn_netlink.c | 10 ++++++++--
> >  2 files changed, 11 insertions(+), 3 deletions(-)
> >
> > diff --git a/net/phonet/pn_dev.c b/net/phonet/pn_dev.c
> > index 19234d664c4fb537eba0267266efbb226cf103c3..578d935f2b11694fd1004c5f854ec344b846eeb2 100644
> > --- a/net/phonet/pn_dev.c
> > +++ b/net/phonet/pn_dev.c
> > @@ -406,7 +406,9 @@ int phonet_route_del(struct net_device *dev, u8 daddr)
> >
> >       if (!dev)
> >               return -ENOENT;
> > -     synchronize_rcu();
> > +
> > +     /* Note : our caller must call synchronize_rcu() */
> > +
> >       dev_put(dev);
>
> Are these synchronize_rcu() + dev_put() paired with rcu_read_lock()
> and dev_hold() in phonet_route_output() ?

This dev_put() was before your patch.

It is paired the dev_hold() in phonet_route_add()

>
> If so, we need to move dev_put() too or maybe we can remove
> synchronize_rcu() here and replace rcu_read_lock() with
> spin_lock(&routes->lock) in phonet_route_output().

Not at all, let's not make phonet slower than needed :)
Rémi Denis-Courmont Nov. 5, 2024, 11:03 a.m. UTC | #3
Le 4 novembre 2024 17:26:22 GMT+02:00, Eric Dumazet <edumazet@google.com> a écrit :
>Calling synchronize_rcu() while holding rcu_read_lock() is not
>permitted [1]
>
>Move the synchronize_rcu() to route_doit().
>
>[1]
>WARNING: suspicious RCU usage
>6.12.0-rc5-syzkaller-01056-gf07a6e6ceb05 #0 Not tainted
>-----------------------------
>kernel/rcu/tree.c:4092 Illegal synchronize_rcu() in RCU read-side critical section!
>
>other info that might help us debug this:
>
>rcu_scheduler_active = 2, debug_locks = 1
>1 lock held by syz-executor427/5840:
>  #0: ffffffff8e937da0 (rcu_read_lock){....}-{1:2}, at: rcu_lock_acquire include/linux/rcupdate.h:337 [inline]
>  #0: ffffffff8e937da0 (rcu_read_lock){....}-{1:2}, at: rcu_read_lock include/linux/rcupdate.h:849 [inline]
>  #0: ffffffff8e937da0 (rcu_read_lock){....}-{1:2}, at: route_doit+0x3d6/0x640 net/phonet/pn_netlink.c:264
>
>stack backtrace:
>CPU: 1 UID: 0 PID: 5840 Comm: syz-executor427 Not tainted 6.12.0-rc5-syzkaller-01056-gf07a6e6ceb05 #0
>Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 09/13/2024
>Call Trace:
> <TASK>
>  __dump_stack lib/dump_stack.c:94 [inline]
>  dump_stack_lvl+0x241/0x360 lib/dump_stack.c:120
>  lockdep_rcu_suspicious+0x226/0x340 kernel/locking/lockdep.c:6821
>  synchronize_rcu+0xea/0x360 kernel/rcu/tree.c:4089
>  phonet_route_del+0xc6/0x140 net/phonet/pn_dev.c:409
>  route_doit+0x514/0x640 net/phonet/pn_netlink.c:275
>  rtnetlink_rcv_msg+0x791/0xcf0 net/core/rtnetlink.c:6790
>  netlink_rcv_skb+0x1e3/0x430 net/netlink/af_netlink.c:2551
>  netlink_unicast_kernel net/netlink/af_netlink.c:1331 [inline]
>  netlink_unicast+0x7f6/0x990 net/netlink/af_netlink.c:1357
>  netlink_sendmsg+0x8e4/0xcb0 net/netlink/af_netlink.c:1901
>  sock_sendmsg_nosec net/socket.c:729 [inline]
>  __sock_sendmsg+0x221/0x270 net/socket.c:744
>  sock_write_iter+0x2d7/0x3f0 net/socket.c:1165
>  new_sync_write fs/read_write.c:590 [inline]
>  vfs_write+0xaeb/0xd30 fs/read_write.c:683
>  ksys_write+0x183/0x2b0 fs/read_write.c:736
>  do_syscall_x64 arch/x86/entry/common.c:52 [inline]
>  do_syscall_64+0xf3/0x230 arch/x86/entry/common.c:83
> entry_SYSCALL_64_after_hwframe+0x77/0x7f
>
>Fixes: 17a1ac0018ae ("phonet: Don't hold RTNL for route_doit().")
>Reported-by: syzbot <syzkaller@googlegroups.com>
>Signed-off-by: Eric Dumazet <edumazet@google.com>
>Cc: Kuniyuki Iwashima <kuniyu@amazon.com>
>Cc: Remi Denis-Courmont <courmisch@gmail.com>
>---
> net/phonet/pn_dev.c     |  4 +++-
> net/phonet/pn_netlink.c | 10 ++++++++--
> 2 files changed, 11 insertions(+), 3 deletions(-)
>
>diff --git a/net/phonet/pn_dev.c b/net/phonet/pn_dev.c
>index 19234d664c4fb537eba0267266efbb226cf103c3..578d935f2b11694fd1004c5f854ec344b846eeb2 100644
>--- a/net/phonet/pn_dev.c
>+++ b/net/phonet/pn_dev.c
>@@ -406,7 +406,9 @@ int phonet_route_del(struct net_device *dev, u8 daddr)
> 
> 	if (!dev)
> 		return -ENOENT;
>-	synchronize_rcu();
>+
>+	/* Note : our caller must call synchronize_rcu() */
>+
> 	dev_put(dev);
> 	return 0;
> }
>diff --git a/net/phonet/pn_netlink.c b/net/phonet/pn_netlink.c
>index ca1f04e4a2d9eb3b2a6d6cc5b299aee28d569b08..24930733ac572ed3ec5fd142d347c115346a28fa 100644
>--- a/net/phonet/pn_netlink.c
>+++ b/net/phonet/pn_netlink.c
>@@ -233,6 +233,7 @@ static int route_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
> {
> 	struct net *net = sock_net(skb->sk);
> 	struct nlattr *tb[RTA_MAX+1];
>+	bool sync_needed = false;
> 	struct net_device *dev;
> 	struct rtmsg *rtm;
> 	u32 ifindex;
>@@ -269,16 +270,21 @@ static int route_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
> 		return -ENODEV;
> 	}
> 
>-	if (nlh->nlmsg_type == RTM_NEWROUTE)
>+	if (nlh->nlmsg_type == RTM_NEWROUTE) {
> 		err = phonet_route_add(dev, dst);
>-	else
>+	} else {
> 		err = phonet_route_del(dev, dst);
>+		if (!err)
>+			sync_needed = true;
>+	}
> 
> 	rcu_read_unlock();
> 
> 	if (!err)
> 		rtm_phonet_notify(net, nlh->nlmsg_type, ifindex, dst);
> 
>+	if (sync_needed)
>+		synchronize_rcu();

Synchronising after sending notifications sounds a bit iffy. Whatever a given notification is about should be fully committed so we don't create a user-visible race here.

Can't we reorder here?

> 	return err;
> }
>
Eric Dumazet Nov. 5, 2024, 12:05 p.m. UTC | #4
On Tue, Nov 5, 2024 at 12:05 PM Rémi Denis-Courmont <remi@remlab.net> wrote:
>
>
>
> Le 4 novembre 2024 17:26:22 GMT+02:00, Eric Dumazet <edumazet@google.com> a écrit :
> >Calling synchronize_rcu() while holding rcu_read_lock() is not
> >permitted [1]
> >
> >Move the synchronize_rcu() to route_doit().
> >
> >[1]
> >WARNING: suspicious RCU usage
> >6.12.0-rc5-syzkaller-01056-gf07a6e6ceb05 #0 Not tainted
> >-----------------------------
> >kernel/rcu/tree.c:4092 Illegal synchronize_rcu() in RCU read-side critical section!
> >
> >other info that might help us debug this:
> >
> >rcu_scheduler_active = 2, debug_locks = 1
> >1 lock held by syz-executor427/5840:
> >  #0: ffffffff8e937da0 (rcu_read_lock){....}-{1:2}, at: rcu_lock_acquire include/linux/rcupdate.h:337 [inline]
> >  #0: ffffffff8e937da0 (rcu_read_lock){....}-{1:2}, at: rcu_read_lock include/linux/rcupdate.h:849 [inline]
> >  #0: ffffffff8e937da0 (rcu_read_lock){....}-{1:2}, at: route_doit+0x3d6/0x640 net/phonet/pn_netlink.c:264
> >
> >stack backtrace:
> >CPU: 1 UID: 0 PID: 5840 Comm: syz-executor427 Not tainted 6.12.0-rc5-syzkaller-01056-gf07a6e6ceb05 #0
> >Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 09/13/2024
> >Call Trace:
> > <TASK>
> >  __dump_stack lib/dump_stack.c:94 [inline]
> >  dump_stack_lvl+0x241/0x360 lib/dump_stack.c:120
> >  lockdep_rcu_suspicious+0x226/0x340 kernel/locking/lockdep.c:6821
> >  synchronize_rcu+0xea/0x360 kernel/rcu/tree.c:4089
> >  phonet_route_del+0xc6/0x140 net/phonet/pn_dev.c:409
> >  route_doit+0x514/0x640 net/phonet/pn_netlink.c:275
> >  rtnetlink_rcv_msg+0x791/0xcf0 net/core/rtnetlink.c:6790
> >  netlink_rcv_skb+0x1e3/0x430 net/netlink/af_netlink.c:2551
> >  netlink_unicast_kernel net/netlink/af_netlink.c:1331 [inline]
> >  netlink_unicast+0x7f6/0x990 net/netlink/af_netlink.c:1357
> >  netlink_sendmsg+0x8e4/0xcb0 net/netlink/af_netlink.c:1901
> >  sock_sendmsg_nosec net/socket.c:729 [inline]
> >  __sock_sendmsg+0x221/0x270 net/socket.c:744
> >  sock_write_iter+0x2d7/0x3f0 net/socket.c:1165
> >  new_sync_write fs/read_write.c:590 [inline]
> >  vfs_write+0xaeb/0xd30 fs/read_write.c:683
> >  ksys_write+0x183/0x2b0 fs/read_write.c:736
> >  do_syscall_x64 arch/x86/entry/common.c:52 [inline]
> >  do_syscall_64+0xf3/0x230 arch/x86/entry/common.c:83
> > entry_SYSCALL_64_after_hwframe+0x77/0x7f
> >
> >Fixes: 17a1ac0018ae ("phonet: Don't hold RTNL for route_doit().")
> >Reported-by: syzbot <syzkaller@googlegroups.com>
> >Signed-off-by: Eric Dumazet <edumazet@google.com>
> >Cc: Kuniyuki Iwashima <kuniyu@amazon.com>
> >Cc: Remi Denis-Courmont <courmisch@gmail.com>
> >---
> > net/phonet/pn_dev.c     |  4 +++-
> > net/phonet/pn_netlink.c | 10 ++++++++--
> > 2 files changed, 11 insertions(+), 3 deletions(-)
> >
> >diff --git a/net/phonet/pn_dev.c b/net/phonet/pn_dev.c
> >index 19234d664c4fb537eba0267266efbb226cf103c3..578d935f2b11694fd1004c5f854ec344b846eeb2 100644
> >--- a/net/phonet/pn_dev.c
> >+++ b/net/phonet/pn_dev.c
> >@@ -406,7 +406,9 @@ int phonet_route_del(struct net_device *dev, u8 daddr)
> >
> >       if (!dev)
> >               return -ENOENT;
> >-      synchronize_rcu();
> >+
> >+      /* Note : our caller must call synchronize_rcu() */
> >+
> >       dev_put(dev);
> >       return 0;
> > }
> >diff --git a/net/phonet/pn_netlink.c b/net/phonet/pn_netlink.c
> >index ca1f04e4a2d9eb3b2a6d6cc5b299aee28d569b08..24930733ac572ed3ec5fd142d347c115346a28fa 100644
> >--- a/net/phonet/pn_netlink.c
> >+++ b/net/phonet/pn_netlink.c
> >@@ -233,6 +233,7 @@ static int route_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
> > {
> >       struct net *net = sock_net(skb->sk);
> >       struct nlattr *tb[RTA_MAX+1];
> >+      bool sync_needed = false;
> >       struct net_device *dev;
> >       struct rtmsg *rtm;
> >       u32 ifindex;
> >@@ -269,16 +270,21 @@ static int route_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
> >               return -ENODEV;
> >       }
> >
> >-      if (nlh->nlmsg_type == RTM_NEWROUTE)
> >+      if (nlh->nlmsg_type == RTM_NEWROUTE) {
> >               err = phonet_route_add(dev, dst);
> >-      else
> >+      } else {
> >               err = phonet_route_del(dev, dst);
> >+              if (!err)
> >+                      sync_needed = true;
> >+      }
> >
> >       rcu_read_unlock();
> >
> >       if (!err)
> >               rtm_phonet_notify(net, nlh->nlmsg_type, ifindex, dst);
> >
> >+      if (sync_needed)
> >+              synchronize_rcu();
>
> Synchronising after sending notifications sounds a bit iffy. Whatever a given notification is about should be fully committed so we don't create a user-visible race here.
>
> Can't we reorder here?

Fair enough, I will move dev_put() here in V2.
Eric Dumazet Nov. 5, 2024, 12:11 p.m. UTC | #5
On Tue, Nov 5, 2024 at 12:05 PM Rémi Denis-Courmont <remi@remlab.net> wrote:
>

> Synchronising after sending notifications sounds a bit iffy. Whatever a given notification is about should be fully committed so we don't create a user-visible race here.

This is fine, the notification sends the ifindex, after the route is
effectively removed.

Fact that the device is held a bit longer than before should be an
improvement, or just a nop.

However, doing the dev_put() after synchronize_rcu() is probably better.
diff mbox series

Patch

diff --git a/net/phonet/pn_dev.c b/net/phonet/pn_dev.c
index 19234d664c4fb537eba0267266efbb226cf103c3..578d935f2b11694fd1004c5f854ec344b846eeb2 100644
--- a/net/phonet/pn_dev.c
+++ b/net/phonet/pn_dev.c
@@ -406,7 +406,9 @@  int phonet_route_del(struct net_device *dev, u8 daddr)
 
 	if (!dev)
 		return -ENOENT;
-	synchronize_rcu();
+
+	/* Note : our caller must call synchronize_rcu() */
+
 	dev_put(dev);
 	return 0;
 }
diff --git a/net/phonet/pn_netlink.c b/net/phonet/pn_netlink.c
index ca1f04e4a2d9eb3b2a6d6cc5b299aee28d569b08..24930733ac572ed3ec5fd142d347c115346a28fa 100644
--- a/net/phonet/pn_netlink.c
+++ b/net/phonet/pn_netlink.c
@@ -233,6 +233,7 @@  static int route_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
 {
 	struct net *net = sock_net(skb->sk);
 	struct nlattr *tb[RTA_MAX+1];
+	bool sync_needed = false;
 	struct net_device *dev;
 	struct rtmsg *rtm;
 	u32 ifindex;
@@ -269,16 +270,21 @@  static int route_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
 		return -ENODEV;
 	}
 
-	if (nlh->nlmsg_type == RTM_NEWROUTE)
+	if (nlh->nlmsg_type == RTM_NEWROUTE) {
 		err = phonet_route_add(dev, dst);
-	else
+	} else {
 		err = phonet_route_del(dev, dst);
+		if (!err)
+			sync_needed = true;
+	}
 
 	rcu_read_unlock();
 
 	if (!err)
 		rtm_phonet_notify(net, nlh->nlmsg_type, ifindex, dst);
 
+	if (sync_needed)
+		synchronize_rcu();
 	return err;
 }