Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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.

Signed-off-by: Tom Parkin <tparkin@katalix.com>
Fixes: 4cf476ced45d ("ppp: add PPPIOCBRIDGECHAN and PPPIOCUNBRIDGECHAN ioctls")
Acked-by: Guillaume Nault <gnault@redhat.com>
Link: https://lore.kernel.org/r/20210107181315.3128-1-tparkin@katalix.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>

authored by

Tom Parkin and committed by
Jakub Kicinski
c1787ffd fd2ddef0

+9 -3
+9 -3
drivers/net/ppp/ppp_generic.c
··· 623 623 write_unlock_bh(&pch->upl); 624 624 return -EALREADY; 625 625 } 626 + refcount_inc(&pchb->file.refcnt); 626 627 rcu_assign_pointer(pch->bridge, pchb); 627 628 write_unlock_bh(&pch->upl); 628 629 ··· 633 632 write_unlock_bh(&pchb->upl); 634 633 goto err_unset; 635 634 } 635 + refcount_inc(&pch->file.refcnt); 636 636 rcu_assign_pointer(pchb->bridge, pch); 637 637 write_unlock_bh(&pchb->upl); 638 - 639 - refcount_inc(&pch->file.refcnt); 640 - refcount_inc(&pchb->file.refcnt); 641 638 642 639 return 0; 643 640 644 641 err_unset: 645 642 write_lock_bh(&pch->upl); 643 + /* Re-read pch->bridge with upl held in case it was modified concurrently */ 644 + pchb = rcu_dereference_protected(pch->bridge, lockdep_is_held(&pch->upl)); 646 645 RCU_INIT_POINTER(pch->bridge, NULL); 647 646 write_unlock_bh(&pch->upl); 648 647 synchronize_rcu(); 648 + 649 + if (pchb) 650 + if (refcount_dec_and_test(&pchb->file.refcnt)) 651 + ppp_destroy_channel(pchb); 652 + 649 653 return -EALREADY; 650 654 } 651 655