Skip to content

Commit 0417adf

Browse files
LGA1150Paolo Abeni
authored andcommitted
ppp: fix race conditions in ppp_fill_forward_path
ppp_fill_forward_path() has two race conditions: 1. The ppp->channels list can change between list_empty() and list_first_entry(), as ppp_lock() is not held. If the only channel is deleted in ppp_disconnect_channel(), list_first_entry() may access an empty head or a freed entry, and trigger a panic. 2. pch->chan can be NULL. When ppp_unregister_channel() is called, pch->chan is set to NULL before pch is removed from ppp->channels. Fix these by using a lockless RCU approach: - Use list_first_or_null_rcu() to safely test and access the first list entry. - Convert list modifications on ppp->channels to their RCU variants and add synchronize_net() after removal. - Check for a NULL pch->chan before dereferencing it. Fixes: f6efc67 ("net: ppp: resolve forwarding path for bridge pppoe devices") Signed-off-by: Qingfang Deng <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Paolo Abeni <[email protected]>
1 parent 62c30c5 commit 0417adf

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

drivers/net/ppp/ppp_generic.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <linux/ppp_channel.h>
3434
#include <linux/ppp-comp.h>
3535
#include <linux/skbuff.h>
36+
#include <linux/rculist.h>
3637
#include <linux/rtnetlink.h>
3738
#include <linux/if_arp.h>
3839
#include <linux/ip.h>
@@ -1598,11 +1599,14 @@ static int ppp_fill_forward_path(struct net_device_path_ctx *ctx,
15981599
if (ppp->flags & SC_MULTILINK)
15991600
return -EOPNOTSUPP;
16001601

1601-
if (list_empty(&ppp->channels))
1602+
pch = list_first_or_null_rcu(&ppp->channels, struct channel, clist);
1603+
if (!pch)
1604+
return -ENODEV;
1605+
1606+
chan = READ_ONCE(pch->chan);
1607+
if (!chan)
16021608
return -ENODEV;
16031609

1604-
pch = list_first_entry(&ppp->channels, struct channel, clist);
1605-
chan = pch->chan;
16061610
if (!chan->ops->fill_forward_path)
16071611
return -EOPNOTSUPP;
16081612

@@ -2994,7 +2998,7 @@ ppp_unregister_channel(struct ppp_channel *chan)
29942998
*/
29952999
down_write(&pch->chan_sem);
29963000
spin_lock_bh(&pch->downl);
2997-
pch->chan = NULL;
3001+
WRITE_ONCE(pch->chan, NULL);
29983002
spin_unlock_bh(&pch->downl);
29993003
up_write(&pch->chan_sem);
30003004
ppp_disconnect_channel(pch);
@@ -3515,7 +3519,7 @@ ppp_connect_channel(struct channel *pch, int unit)
35153519
hdrlen = pch->file.hdrlen + 2; /* for protocol bytes */
35163520
if (hdrlen > ppp->dev->hard_header_len)
35173521
ppp->dev->hard_header_len = hdrlen;
3518-
list_add_tail(&pch->clist, &ppp->channels);
3522+
list_add_tail_rcu(&pch->clist, &ppp->channels);
35193523
++ppp->n_channels;
35203524
pch->ppp = ppp;
35213525
refcount_inc(&ppp->file.refcnt);
@@ -3545,10 +3549,11 @@ ppp_disconnect_channel(struct channel *pch)
35453549
if (ppp) {
35463550
/* remove it from the ppp unit's list */
35473551
ppp_lock(ppp);
3548-
list_del(&pch->clist);
3552+
list_del_rcu(&pch->clist);
35493553
if (--ppp->n_channels == 0)
35503554
wake_up_interruptible(&ppp->file.rwait);
35513555
ppp_unlock(ppp);
3556+
synchronize_net();
35523557
if (refcount_dec_and_test(&ppp->file.refcnt))
35533558
ppp_destroy_interface(ppp);
35543559
err = 0;

0 commit comments

Comments
 (0)