Root Cause Analysis
1. Involved Entities and Files
- Source File:
drivers/net/ppp/ppp_generic.c
- Functions:
ppp_disconnect_channel(struct channel *pch) (lines 3636–3657)
ppp_connect_channel(struct channel *pch, int unit) (lines 3579–3631)
ppp_unregister_channel(struct ppp_channel *chan) (lines 3074–3106)
ppp_ioctl(struct file *file, unsigned int cmd, unsigned long arg) (lines 712–797)
- Structs and Fields:
struct channel (pch->upl, pch->ppp, pch->clist, pch->chan, pch->downl)
struct ppp (ppp->channels, ppp->n_channels, ppp->wlock, ppp->rlock)
2. Technical Mechanism and Invariant Violation
In the PPP subsystem, a channel (struct channel *pch) represents a communication link that can be connected to a PPP network interface unit (struct ppp *ppp).
Flaw in ppp_disconnect_channel()
In ppp_disconnect_channel():
static int
ppp_disconnect_channel(struct channel *pch)
{
struct ppp *ppp;
int err = -EINVAL;
spin_lock(&pch->upl);
ppp = rcu_replace_pointer(pch->ppp, NULL, lockdep_is_held(&pch->upl));
spin_unlock(&pch->upl);
if (ppp) {
/* remove it from the ppp unit's list */
ppp_lock(ppp);
list_del_rcu(&pch->clist);
...
ppp_unlock(ppp);
...
}
return err;
}
At line 3643, pch->ppp is cleared to NULL while holding pch->upl. However, pch->upl is immediately unlocked at line 3644 before acquiring ppp_lock(ppp) and calling list_del_rcu(&pch->clist) at line 3648. This creates an inconsistent intermediate state where pch->ppp == NULL, but pch->clist remains linked to ppp->channels.
The Race with ppp_connect_channel()
ppp_unregister_channel() is invoked when an underlying channel driver or socket is closed/released (e.g., pppox_unbind_sock(), ppp_asynctty_close(), ppp_sync_close()). It calls ppp_disconnect_channel(pch) without holding ppp_mutex.
At the same time, userspace can execute ioctl(fd, PPPIOCCONNECT, &unit) on an open /dev/ppp channel descriptor (pf->kind == CHANNEL). This enters ppp_ioctl() and calls ppp_connect_channel(pch, unit):
static int
ppp_connect_channel(struct channel *pch, int unit)
{
...
mutex_lock(&pn->all_ppp_mutex);
ppp = ppp_find_unit(pn, unit);
...
spin_lock(&pch->upl);
ret = -EINVAL;
if (rcu_dereference_protected(pch->ppp, lockdep_is_held(&pch->upl)) ||
rcu_dereference_protected(pch->bridge, lockdep_is_held(&pch->upl)))
goto outl;
ppp_lock(ppp);
spin_lock_bh(&pch->downl);
if (!pch->chan) {
...
}
...
list_add_tail_rcu(&pch->clist, &ppp->channels);
...
rcu_assign_pointer(pch->ppp, ppp);
...
ppp_unlock(ppp);
...
outl:
spin_unlock(&pch->upl);
When ppp_disconnect_channel() releases pch->upl between lines 3644 and 3647:
ppp_connect_channel() acquires pch->upl.
- It evaluates
if (pch->ppp || pch->bridge) at lines 3596–3597. Because ppp_disconnect_channel() already set pch->ppp = NULL, this check passes.
ppp_connect_channel() proceeds to acquire ppp_lock(ppp) for the PPP unit before ppp_disconnect_channel() does.
- Under
pch->downl, pch->chan is still non-NULL because ppp_unregister_channel() only sets pch->chan = NULL at lines 3090–3092 after ppp_disconnect_channel() returns.
ppp_connect_channel() executes list_add_tail_rcu(&pch->clist, &ppp->channels) at line 3619.
3. Failure Manifestation
Because pch->clist was never unlinked by ppp_disconnect_channel(), pch->clist is already in ppp->channels.
In list_add_tail_rcu(new, head):
new = &pch->clist (0xffff88803602fa10)
next = head = &ppp->channels (0xffff88802da99020)
prev = head->prev (0xffff88803602fa10), which already points to &pch->clist
Validation inside __list_add_valid_or_report() (lib/list_debug.c:35-37) evaluates:
CHECK_DATA_CORRUPTION(new == prev || new == next, NULL,
"list_add double add: new=%px, prev=%px, next=%px.\n",
new, prev, next)
Since new == prev, the check fails, outputs:
list_add double add: new=ffff88803602fa10, prev=ffff88803602fa10, next=ffff88802da99020.
and triggers kernel BUG at lib/list_debug.c:37!.