ppp: fix refcount underflow on channel unbridge
Subsystems (3)
PATCH v3 1/1 ppp: fix refcount underflow on channel unbridge
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.
---
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(-)
drivers/net/ppp/ppp_generic.c +9 -3
@@ -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 ; }
Signature · 1 line
2.17.1
Quoted 15 lines · Tom Parkin
When setting up a channel bridge,
ppp_bridge_channelssets thepch->bridgefield before taking the associated reference on the bridge file instance.This opens up a refcount underflow bug if
ppp_bridge_channelscalled via. iotcl runs concurrently withppp_unbridge_channelsexecuting via. file release.The bug is triggered by
ppp_bridge_channelstaking the error path through the 'err_unset' label. In this scenario,pch->bridgeis set, but the reference on the bridged channel will not be taken because the function errors out. Ifppp_unbridge_channelsobservespch->bridgebefore it is unset by the error path, it will erroneously drop the reference on the bridged channel and cause a refcount underflow.
Quoted 2 lines
To avoid this, ensure that
ppp_bridge_channelsholds a reference on each channel in advance of setting the bridge pointers.
Thanks for following up on this!
Quoted 16 lines · On Fri, 8 Jan 2021 21:57:50 +0100 Guillaume Nault wrote:
Quoted 15 lines · Tom Parkin
When setting up a channel bridge,
ppp_bridge_channelssets thepch->bridgefield before taking the associated reference on the bridge file instance.This opens up a refcount underflow bug if
ppp_bridge_channelscalled via. iotcl runs concurrently withppp_unbridge_channelsexecuting via. file release.The bug is triggered by
ppp_bridge_channelstaking the error path through the 'err_unset' label. In this scenario,pch->bridgeis set, but the reference on the bridged channel will not be taken because the function errors out. Ifppp_unbridge_channelsobservespch->bridgebefore it is unset by the error path, it will erroneously drop the reference on the bridged channel and cause a refcount underflow.
Quoted 6 lines
Quoted 2 lines
To avoid this, ensure that
ppp_bridge_channelsholds a reference on each channel in advance of setting the bridge pointers.Thanks for following up on this!
Acked-by Guillaume Nault <[email protected]>
Applied, thanks!
Quoted 16 lines · Guillaume Nault
Quoted 15 lines · Tom Parkin
When setting up a channel bridge,
ppp_bridge_channelssets thepch->bridgefield before taking the associated reference on the bridge file instance.This opens up a refcount underflow bug if
ppp_bridge_channelscalled via. iotcl runs concurrently withppp_unbridge_channelsexecuting via. file release.The bug is triggered by
ppp_bridge_channelstaking the error path through the 'err_unset' label. In this scenario,pch->bridgeis set, but the reference on the bridged channel will not be taken because the function errors out. Ifppp_unbridge_channelsobservespch->bridgebefore it is unset by the error path, it will erroneously drop the reference on the bridged channel and cause a refcount underflow.
Quoted 6 lines
Quoted 2 lines
To avoid this, ensure that
ppp_bridge_channelsholds a reference on each channel in advance of setting the bridge pointers.Thanks for following up on this!
Acked-by Guillaume Nault <[email protected]>
Thanks again for reviewing, Guillaume.