Bug Technical Breakdown and Root Cause Analysis
1. Involved Components and Data Structures
- Files:
drivers/net/ppp/ppp_generic.c
- Functions:
ppp_unregister_channel(), ppp_disconnect_channel(), ppp_connect_channel(), ppp_release_channel()
- Structures and Fields:
struct channel: represents a PPP channel (allocated from kmalloc-1k in ppp_register_net_channel()).
pch->chan: pointer to public struct ppp_channel; set to NULL upon unregistration.
pch->ppp: RCU-protected pointer to associated struct ppp unit.
pch->clist: struct list_head node linking this channel into ppp->channels (at offset 528 / 0x210).
pch->upl: spinlock protecting pch->ppp and pch->bridge.
pch->downl: spinlock protecting pch->chan.
struct ppp: represents a PPP unit device (struct net_device).
ppp->channels: struct list_head containing attached channels.
ppp->wlock / ppp->rlock: spinlocks acquired jointly by ppp_lock(ppp) protecting ppp->channels.
2. Root Cause Mechanism
The defect is caused by an unsynchronized race window during channel teardown and reconnection, allowing a dying channel to be linked back onto a PPP unit's channels list (ppp->channels) while in the process of being unregistered and freed.
A. Reconnection Race During ppp_unregister_channel()
In ppp_unregister_channel() (drivers/net/ppp/ppp_generic.c:3071):
void ppp_unregister_channel(struct ppp_channel *chan)
{
struct channel *pch = chan->ppp;
...
ppp_disconnect_channel(pch);
mutex_lock(&pch->chan_sem);
spin_lock_bh(&pch->downl);
pch->chan = NULL;
spin_unlock_bh(&pch->downl);
mutex_unlock(&pch->chan_sem);
...
ppp_release_channel(pch);
}
- In
ppp_unregister_channel(), ppp_disconnect_channel(pch) is invoked before clearing pch->chan = NULL.
- Inside
ppp_disconnect_channel(pch):
pch->ppp is replaced with NULL under pch->upl.
pch->clist is removed from ppp->channels under ppp_lock(ppp).
synchronize_net() is called to wait for packet processing RCU readers to drain.
- While
ppp_disconnect_channel(pch) sleeps in synchronize_net() (or immediately after it returns before pch->downl is locked to clear pch->chan), pch->ppp is NULL, but pch->chan is still non-NULL.
- In
ppp_ioctl(), PPPIOCCONNECT does not hold pch->chan_sem (which only guards default: ioctls). When a concurrent thread invokes ioctl(chan_fd, PPPIOCCONNECT, &unit) on an open channel file descriptor, ppp_connect_channel() (drivers/net/ppp/ppp_generic.c:3576) runs:
- It checks
if (pch->ppp || pch->bridge) under pch->upl, which passes because pch->ppp was set to NULL.
- It acquires
ppp_lock(ppp) and then pch->downl.
- It validates
if (!pch->chan) (/* Don't connect unregistered channels */). Because pch->chan has not been cleared yet, this check passes!
- It inserts
pch->clist back onto ppp->channels via list_add_tail_rcu(&pch->clist, &ppp->channels), sets rcu_assign_pointer(pch->ppp, ppp), and increments ppp->file.refcnt.
- Now
ppp_unregister_channel() proceeds: it sets pch->chan = NULL, unlinks pch->list from pn->all_channels, marks pch->file.dead = 1, and drops its reference via ppp_release_channel(pch). It never calls ppp_disconnect_channel() again.
- When userspace subsequently closes
chan_fd, ppp_release() (drivers/net/ppp/ppp_generic.c:419) calls ppp_release_channel(pch), dropping the last refcount. call_rcu(&pch->rcu, ppp_release_channel_free) is scheduled, and pch is freed by rcu_core -> kfree(pch).
- Invariant violation:
pch is freed while pch->clist is still actively linked into ppp->channels.
B. Premature Lock Release in ppp_disconnect_channel()
A related synchronization flaw exists in ppp_disconnect_channel() (drivers/net/ppp/ppp_generic.c:3634):
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) {
ppp_lock(ppp);
list_del_rcu(&pch->clist);
...
ppp_unlock(ppp);
pch->upl is released after clearing pch->ppp before taking ppp_lock(ppp) to delete pch->clist, despite the documented lock ordering (channel.upl -> ppp.wlock -> ppp.rlock -> channel.downl). During this window, pch->ppp == NULL while pch->clist is still linked in ppp->channels. If PPPIOCCONNECT runs concurrently on another thread, it sees pch->ppp == NULL, takes ppp_lock(ppp), and adds pch->clist onto ppp->channels again (a double list_add), followed by list_del_rcu(&pch->clist) from the disconnect thread, leaving corrupted list pointers.
3. How the Reported Failure Occurs
- In iteration $N$, channel
pch_N is attached to unit ppp.
- Teardown begins (
close(pty_fd) -> ppp_unregister_channel()), and a concurrent worker executing PPPIOCCONNECT re-attaches pch_N into ppp->channels.
chan_fd is closed, queuing pch_N for RCU destruction (ppp_release_channel_free()).
- In iteration $N+1$, a new channel
pch_{N+1} connects to the same ppp unit. list_add_tail_rcu(&pch_{N+1}->clist, &ppp->channels) links pch_{N+1}->clist.prev to &pch_N->clist.
- RCU grace period elapses and
pch_N is freed (kfree()).
- Channel
pch_{N+1} is subsequently disconnected (close(pty_fd) -> ppp_asynctty_close() -> ppp_unregister_channel() -> ppp_disconnect_channel()).
ppp_disconnect_channel() calls list_del_rcu(&pch_{N+1}->clist).
__list_del_entry_valid_or_report() inspects entry->prev->next (line 62 of lib/list_debug.c). Because entry->prev points to &pch_N->clist at offset 528 (0x210) inside the freed pch_N allocation, KASAN detects a slab-use-after-free read at ffff88807ee8aa10.