Linux PPP protocol development
 help / color / mirror / Atom feed
* [PATCH net] ppp: serialize channel connect with unregister
@ 2026-11-16 12:27 Haoli Zhang
  2026-11-19  3:28 ` netdev-bot+sashiko
  0 siblings, 1 reply; 2+ messages in thread
From: Haoli Zhang @ 2026-11-16 12:27 UTC (permalink / raw)
  To: andrew+netdev, davem, edumazet, kuba, pabeni
  Cc: linux-ppp, netdev, linux-kernel, syzbot+6b18da3ef22e33061add,
	qingfang.deng, 25031212117, byu, Haoli Zhang

ppp_unregister_channel() disconnects a channel before clearing pch->chan.
During that interval, a concurrent PPPIOCCONNECT can observe pch->ppp as
NULL while pch->chan is still valid. It can reconnect the channel and add
its clist to a PPP unit, after which unregistering releases the channel
without removing the new list entry.

Serialize PPPIOCCONNECT with channel unregistering using chan_sem. Take
the mutex before disconnecting the channel and hold it until pch->chan
is cleared. A connect already in progress completes before unregistering
disconnects the channel; a later connect waits until pch->chan is NULL and
is rejected by the existing check in ppp_connect_channel().

This preserves the disconnect-before-null ordering needed by the transmit
path. The change adds no locking to the packet fast path.

Fixes: 6a196e83a1a7 ("ppp: disconnect channel before nullifying pch->chan")
Reported-by: syzbot+6b18da3ef22e33061add@syzkaller.appspotmail.com
Closes: https://syzbot.org/bug?extid=6b18da3ef22e33061add
Co-developed-by: Jinning Zhang <25031212117@stu.xidian.edu.cn>
Signed-off-by: Jinning Zhang <25031212117@stu.xidian.edu.cn>
Co-developed-by: Bin Yu <byu@xidian.edu.cn>
Signed-off-by: Bin Yu <byu@xidian.edu.cn>
Signed-off-by: Haoli Zhang <26031212468@stu.xidian.edu.cn>

---

diff --git a/drivers/net/ppp/ppp_generic.c b/drivers/net/ppp/ppp_generic.c
--- a/drivers/net/ppp/ppp_generic.c
+++ b/drivers/net/ppp/ppp_generic.c
@@ -175,7 +175,7 @@
 	struct ppp_file	file;		/* stuff for read/write/poll */
 	struct list_head list;		/* link in all/new_channels list */
 	struct ppp_channel *chan;	/* public channel data structure */
-	struct mutex chan_sem;		/* protects `chan' during chan ioctl */
+	struct mutex chan_sem;		/* protects chan ioctl; serializes connect/unregister */
 	spinlock_t	downl;		/* protects `chan', file.xq dequeue */
 	struct ppp __rcu *ppp;		/* ppp unit we're connected to */
 	struct net	*chan_net;	/* the net channel belongs to */
@@ -755,7 +755,9 @@
 		case PPPIOCCONNECT:
 			if (get_user(unit, p))
 				break;
+			mutex_lock(&pch->chan_sem);
 			err = ppp_connect_channel(pch, unit);
+			mutex_unlock(&pch->chan_sem);
 			break;
 
 		case PPPIOCDISCONN:
@@ -3093,11 +3095,14 @@
 	chan->ppp = NULL;
 
 	/*
+	 * Prevent PPPIOCCONNECT from reattaching the channel during removal.
+	 */
+	mutex_lock(&pch->chan_sem);
+	/*
 	 * This ensures that we have returned from any calls into
 	 * the channel's start_xmit or ioctl routine before we proceed.
 	 */
 	ppp_disconnect_channel(pch);
-	mutex_lock(&pch->chan_sem);
 	spin_lock_bh(&pch->downl);
 	pch->chan = NULL;
 	spin_unlock_bh(&pch->downl);


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

* Re: [PATCH net] ppp: serialize channel connect with unregister
  2026-11-16 12:27 [PATCH net] ppp: serialize channel connect with unregister Haoli Zhang
@ 2026-11-19  3:28 ` netdev-bot+sashiko
  0 siblings, 0 replies; 2+ messages in thread
From: netdev-bot+sashiko @ 2026-11-19  3:28 UTC (permalink / raw)
  To: 26031212468
  Cc: andrew+netdev, davem, edumazet, kuba, pabeni, linux-ppp, netdev,
	linux-kernel, syzbot+6b18da3ef22e33061add, qingfang.deng,
	25031212117, byu

Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.

Critical: 0 · High: 1 (1) · Medium: 0 · Low: 1

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [High] ppp: race between PPPIOCDISCONN and ppp_unregister_channel()
- [Low] In ppp_unregister_channel()
  (drivers/net/ppp/ppp_generic.c:3097-3105), the patch moves…

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but the patch
  closes the PPPIOCCONNECT re-attach race and leaves the other channel-…

--

ppp: serialize channel connect with unregister

This commit takes pch->chan_sem around ppp_connect_channel() in the
PPPIOCCONNECT ioctl. In ppp_unregister_channel() it moves the mutex_lock()
to before ppp_disconnect_channel(), so a concurrent connect cannot re-add
the channel's clist to a PPP unit between the disconnect and the clearing
of pch->chan.

Closes: https://syzbot.org/bug?extid=6b18da3ef22e33061add

> diff --git a/drivers/net/ppp/ppp_generic.c b/drivers/net/ppp/ppp_generic.c
> index 29847dc4bb454..ba2359f0899f3 100644
> --- a/drivers/net/ppp/ppp_generic.c
> +++ b/drivers/net/ppp/ppp_generic.c

[ ... ]

> @@ -755,7 +755,9 @@ static long ppp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
>  		case PPPIOCCONNECT:
>  			if (get_user(unit, p))
>  				break;
> +			mutex_lock(&pch->chan_sem);
>  			err = ppp_connect_channel(pch, unit);
> +			mutex_unlock(&pch->chan_sem);
>  			break;
>  
>  		case PPPIOCDISCONN:

[Severity: High]
This is a pre-existing issue. The commit message says the patch
preserves the disconnect-before-null ordering that the transmit path
needs. Does that ordering still hold when PPPIOCDISCONN races with
ppp_unregister_channel()?

The PPPIOCDISCONN case right below calls ppp_disconnect_channel()
without taking chan_sem. ppp_disconnect_channel() clears pch->ppp
under pch->upl and then drops upl. Only after that does it take
ppp_lock(ppp), unlink pch->clist and wait in synchronize_net().

Consider this sequence:

1. Thread A issues PPPIOCDISCONN on the channel's /dev/ppp file.
   ppp_disconnect_channel() replaces pch->ppp with NULL under upl and
   drops upl. It has not yet taken ppp_lock(ppp).

2. Thread B runs ppp_unregister_channel() on the same channel, for
   example because the pppoe socket or the tty is closed. It takes
   chan_sem without contention. Its ppp_disconnect_channel() sees
   pch->ppp == NULL and returns at once.

3. Thread B sets pch->chan = NULL under downl and returns. The
   channel driver is now free to release its struct ppp_channel.

4. pch->clist is still on ppp->channels. ppp_push(), running under
   ppp_xmit_lock(), picks pch, takes pch->downl, loads pch->chan and
   dereferences chan->direct_xmit.

ppp_mp_explode() and ppp_fill_forward_path() also dereference
pch->chan without a NULL check. Those checks were removed on the
assumption that unregister fully disconnects the channel before
clearing chan.

Thread A may already have done list_del_rcu() by the time B runs. B
still does not wait for A's synchronize_net(), so RCU readers such as
ppp_fill_forward_path() can keep using pch->chan after the driver has
freed it.

That is a NULL dereference or a use-after-free on the transmit path.
ppp_open() only requires CAP_NET_ADMIN in the user namespace, so an
unprivileged user namespace can reach this.

Would taking chan_sem around ppp_disconnect_channel() in the
PPPIOCDISCONN case close the race? Unregister would then wait for an
in-progress disconnect to finish its list_del_rcu() and
synchronize_net(). Alternatively, should ppp_disconnect_channel() be
reworked so that pch->ppp is not cleared until the channel has been
unlinked from the unit?

[Severity: High]
This is a pre-existing issue, but now that the PPPIOCCONNECT re-attach race
is closed, can the PPPIOCBRIDGECHAN case next to it still attach a channel
that is being unregistered?

ppp_bridge_channels() looks only at ->ppp and ->bridge under upl. It does
not check pch->chan or pch->file.dead:

drivers/net/ppp/ppp_generic.c:ppp_bridge_channels() {
	spin_lock(&pch->upl);
	if (rcu_dereference_protected(pch->ppp, lockdep_is_held(&pch->upl)) ||
	    rcu_dereference_protected(pch->bridge, lockdep_is_held(&pch->upl))) {
	...
	refcount_inc(&pchb->file.refcnt);
	rcu_assign_pointer(pch->bridge, pchb);
	...
}

The PPPIOCBRIDGECHAN case does not take chan_sem. ppp_unregister_channel()
calls ppp_unbridge_channels() only once, after chan_sem is dropped and
after list_del():

	mutex_unlock(&pch->chan_sem);
	...
	list_del(&pch->list);
	...
	ppp_unbridge_channels(pch);

Consider this sequence:

1. Channel A is unregistered while a /dev/ppp file is still attached to
   it. A stays alive with chan, ppp and bridge all NULL.

2. PPPIOCBRIDGECHAN is issued on A's file:

   ppp_ioctl()
     ppp_find_channel(pn, unit)          finds B under all_channels_lock
     refcount_inc(&pchb->file.refcnt)
     spin_unlock_bh(&pn->all_channels_lock)

3. At the same time, B's driver calls ppp_unregister_channel(B), for
   example because the pppoe socket is closed:

   ppp_unregister_channel(B)
     list_del(&B->list)
     ppp_unbridge_channels(B)            B->bridge is NULL, returns -EINVAL
     B->file.dead = 1
     ppp_release_channel(B)

4. ppp_bridge_channels(A, B) then sets A->bridge = B and B->bridge = A.
   Each channel takes a reference on the other.

ppp_unregister_channel() never runs again for A or B, and ppp_release()
only calls ppp_release_channel().

Once A's file is closed, is each channel's refcount held only by the
other? If so, ppp_release_channel() never gets to put_net_track().

That would leak both struct channel objects and keep their network
namespace pinned. ppp_open() only requires CAP_NET_ADMIN in the user
namespace. A failed attempt is harmless, so the race can be retried.

This doesn't look like a use-after-free, because
ppp_channel_bridge_input() checks pchb->chan under downl. A fix probably
needs a chan/dead check under upl in ppp_bridge_channels(), not wider
chan_sem coverage.

The new comments "Prevent PPPIOCCONNECT from reattaching the channel during
removal" and "serializes connect/unregister" are accurate for connect only.
The bridge attach path is still not serialized against unregister.

[ ... ]

> @@ -3092,12 +3094,15 @@ ppp_unregister_channel(struct ppp_channel *chan)
>  
>  	chan->ppp = NULL;
>  
> +	/*
> +	 * Prevent PPPIOCCONNECT from reattaching the channel during removal.
> +	 */
> +	mutex_lock(&pch->chan_sem);
>  	/*
>  	 * This ensures that we have returned from any calls into
>  	 * the channel's start_xmit or ioctl routine before we proceed.
>  	 */
>  	ppp_disconnect_channel(pch);

[Severity: Low]
This isn't a bug, but after this change the old comment about returning
from the channel's start_xmit or ioctl routine sits directly above
ppp_disconnect_channel(). The comment on chan_sem now mentions only
PPPIOCCONNECT.

ppp_disconnect_channel() only unlinks clist and calls synchronize_net(),
so it does not wait for chan->ops->ioctl().

The ioctl drain described in include/linux/ppp_channel.h and
Documentation/networking/ppp_generic.rst comes from chan_sem. The default
case in ppp_ioctl() holds it around chan->ops->ioctl():

	default:
		mutex_lock(&pch->chan_sem);
		chan = pch->chan;
		err = -ENOTTY;
		if (chan && chan->ops->ioctl)
			err = chan->ops->ioctl(chan, cmd, arg);
		mutex_unlock(&pch->chan_sem);

Could the comments say that the mutex drains channel ioctls as well as
blocking PPPIOCCONNECT?

> -	mutex_lock(&pch->chan_sem);
>  	spin_lock_bh(&pch->downl);
>  	pch->chan = NULL;
>  	spin_unlock_bh(&pch->downl);

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261116122750.23294-1-26031212468%40stu.xidian.edu.cn

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

end of thread, other threads:[~2026-11-19  3:28 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-16 12:27 [PATCH net] ppp: serialize channel connect with unregister Haoli Zhang
2026-11-19  3:28 ` netdev-bot+sashiko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox