All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH net v3] ppp: fix refcount underflow on channel unbridge
@ 2021-01-07 18:13 Tom Parkin
  2021-01-08 20:57 ` Guillaume Nault
  0 siblings, 1 reply; 4+ messages in thread
From: Tom Parkin @ 2021-01-07 18:13 UTC (permalink / raw)
  To: netdev; +Cc: gnault, jchapman, Tom Parkin

When setting up a channel bridge, ppp_bridge_channels sets the
pch->bridge field before taking the associated reference on the bridge
file instance.

This opens up a refcount underflow bug if ppp_bridge_channels called
via. iotcl runs concurrently with ppp_unbridge_channels executing via.
file release.

The bug is triggered by ppp_bridge_channels taking the error path
through the 'err_unset' label.  In this scenario, pch->bridge is set,
but the reference on the bridged channel will not be taken because
the function errors out.  If ppp_unbridge_channels observes pch->bridge
before it is unset by the error path, it will erroneously drop the
reference on the bridged channel and cause a refcount underflow.

To avoid this, ensure that ppp_bridge_channels holds a reference on
each channel in advance of setting the bridge pointers.

Signed-off-by: Tom Parkin <tparkin@katalix.com>
Fixes: 4cf476ced45d ("ppp: add PPPIOCBRIDGECHAN and PPPIOCUNBRIDGECHAN ioctls")
---
v3:
 * remove bool tracking variable in ppp_bridge_channels and re-read
   pch->bridge instead
 * add missing tags
v2:
 * rework ppp_bridge_channels code to avoid the race condition in
   preference to holding ppp_mutex while calling ppp_unbridge_channels
---
 drivers/net/ppp/ppp_generic.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/drivers/net/ppp/ppp_generic.c b/drivers/net/ppp/ppp_generic.c
index 09c27f7773f9..d445ecb1d0c7 100644
--- a/drivers/net/ppp/ppp_generic.c
+++ b/drivers/net/ppp/ppp_generic.c
@@ -623,6 +623,7 @@ static int ppp_bridge_channels(struct channel *pch, struct channel *pchb)
 		write_unlock_bh(&pch->upl);
 		return -EALREADY;
 	}
+	refcount_inc(&pchb->file.refcnt);
 	rcu_assign_pointer(pch->bridge, pchb);
 	write_unlock_bh(&pch->upl);
 
@@ -632,19 +633,24 @@ static int ppp_bridge_channels(struct channel *pch, struct channel *pchb)
 		write_unlock_bh(&pchb->upl);
 		goto err_unset;
 	}
+	refcount_inc(&pch->file.refcnt);
 	rcu_assign_pointer(pchb->bridge, pch);
 	write_unlock_bh(&pchb->upl);
 
-	refcount_inc(&pch->file.refcnt);
-	refcount_inc(&pchb->file.refcnt);
-
 	return 0;
 
 err_unset:
 	write_lock_bh(&pch->upl);
+	/* Re-read pch->bridge with upl held in case it was modified concurrently */
+	pchb = rcu_dereference_protected(pch->bridge, lockdep_is_held(&pch->upl));
 	RCU_INIT_POINTER(pch->bridge, NULL);
 	write_unlock_bh(&pch->upl);
 	synchronize_rcu();
+
+	if (pchb)
+		if (refcount_dec_and_test(&pchb->file.refcnt))
+			ppp_destroy_channel(pchb);
+
 	return -EALREADY;
 }
 
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH net v3] ppp: fix refcount underflow on channel unbridge
  2021-01-07 18:13 [PATCH net v3] ppp: fix refcount underflow on channel unbridge Tom Parkin
@ 2021-01-08 20:57 ` Guillaume Nault
  2021-01-09  3:23   ` Jakub Kicinski
  2021-01-11 10:28   ` Tom Parkin
  0 siblings, 2 replies; 4+ messages in thread
From: Guillaume Nault @ 2021-01-08 20:57 UTC (permalink / raw)
  To: Tom Parkin; +Cc: netdev, jchapman

On Thu, Jan 07, 2021 at 06:13:15PM +0000, Tom Parkin wrote:
> When setting up a channel bridge, ppp_bridge_channels sets the
> pch->bridge field before taking the associated reference on the bridge
> file instance.
> 
> This opens up a refcount underflow bug if ppp_bridge_channels called
> via. iotcl runs concurrently with ppp_unbridge_channels executing via.
> file release.
> 
> The bug is triggered by ppp_bridge_channels taking the error path
> through the 'err_unset' label.  In this scenario, pch->bridge is set,
> but the reference on the bridged channel will not be taken because
> the function errors out.  If ppp_unbridge_channels observes pch->bridge
> before it is unset by the error path, it will erroneously drop the
> reference on the bridged channel and cause a refcount underflow.
> 
> To avoid this, ensure that ppp_bridge_channels holds a reference on
> each channel in advance of setting the bridge pointers.

Thanks for following up on this!

Acked-by: Guillaume Nault <gnault@redhat.com>


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH net v3] ppp: fix refcount underflow on channel unbridge
  2021-01-08 20:57 ` Guillaume Nault
@ 2021-01-09  3:23   ` Jakub Kicinski
  2021-01-11 10:28   ` Tom Parkin
  1 sibling, 0 replies; 4+ messages in thread
From: Jakub Kicinski @ 2021-01-09  3:23 UTC (permalink / raw)
  To: Guillaume Nault, Tom Parkin; +Cc: netdev, jchapman

On Fri, 8 Jan 2021 21:57:50 +0100 Guillaume Nault wrote:
> On Thu, Jan 07, 2021 at 06:13:15PM +0000, Tom Parkin wrote:
> > When setting up a channel bridge, ppp_bridge_channels sets the
> > pch->bridge field before taking the associated reference on the bridge
> > file instance.
> > 
> > This opens up a refcount underflow bug if ppp_bridge_channels called
> > via. iotcl runs concurrently with ppp_unbridge_channels executing via.
> > file release.
> > 
> > The bug is triggered by ppp_bridge_channels taking the error path
> > through the 'err_unset' label.  In this scenario, pch->bridge is set,
> > but the reference on the bridged channel will not be taken because
> > the function errors out.  If ppp_unbridge_channels observes pch->bridge
> > before it is unset by the error path, it will erroneously drop the
> > reference on the bridged channel and cause a refcount underflow.
> > 
> > To avoid this, ensure that ppp_bridge_channels holds a reference on
> > each channel in advance of setting the bridge pointers.  
> 
> Thanks for following up on this!
> 
> Acked-by: Guillaume Nault <gnault@redhat.com>

Applied, thanks!

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH net v3] ppp: fix refcount underflow on channel unbridge
  2021-01-08 20:57 ` Guillaume Nault
  2021-01-09  3:23   ` Jakub Kicinski
@ 2021-01-11 10:28   ` Tom Parkin
  1 sibling, 0 replies; 4+ messages in thread
From: Tom Parkin @ 2021-01-11 10:28 UTC (permalink / raw)
  To: Guillaume Nault; +Cc: netdev, jchapman

[-- Attachment #1: Type: text/plain, Size: 1185 bytes --]

On  Fri, Jan 08, 2021 at 21:57:50 +0100, Guillaume Nault wrote:
> On Thu, Jan 07, 2021 at 06:13:15PM +0000, Tom Parkin wrote:
> > When setting up a channel bridge, ppp_bridge_channels sets the
> > pch->bridge field before taking the associated reference on the bridge
> > file instance.
> > 
> > This opens up a refcount underflow bug if ppp_bridge_channels called
> > via. iotcl runs concurrently with ppp_unbridge_channels executing via.
> > file release.
> > 
> > The bug is triggered by ppp_bridge_channels taking the error path
> > through the 'err_unset' label.  In this scenario, pch->bridge is set,
> > but the reference on the bridged channel will not be taken because
> > the function errors out.  If ppp_unbridge_channels observes pch->bridge
> > before it is unset by the error path, it will erroneously drop the
> > reference on the bridged channel and cause a refcount underflow.
> > 
> > To avoid this, ensure that ppp_bridge_channels holds a reference on
> > each channel in advance of setting the bridge pointers.
> 
> Thanks for following up on this!
> 
> Acked-by: Guillaume Nault <gnault@redhat.com>

Thanks again for reviewing, Guillaume.

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-01-11 10:29 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-07 18:13 [PATCH net v3] ppp: fix refcount underflow on channel unbridge Tom Parkin
2021-01-08 20:57 ` Guillaume Nault
2021-01-09  3:23   ` Jakub Kicinski
2021-01-11 10:28   ` Tom Parkin

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.