xfrm: Fix device state teardown races
Subsystems (3)
cover xfrm: Fix device state teardown races
Three fixes for the teardown of xfrm device offload state and one revert, for ipsec.
xfrm_dev_state_free() runs for the same state from the state GC, from the device flush and from xfrm_state_find(). Commit 89fefad9f971 ("xfrm: serialize state GC with device state flush") serializes the first two with a mutex, but xfrm_state_find() runs under RCU and still races with the flush.
Let only one caller free the device state of an SA, hold a state reference in the flush and wait for an RCU grace period before the free. The mutex is not needed after that, so revert that commit.
1. xfrm: Give GC list nodes their own storage
Stop sharing the GC list nodes with the bydst and bysrc hash nodes. Patch 2 uses gclist on states that RCU readers may still walk.
2. xfrm: Serialize device state teardown
Let only one caller free the device state of an SA.
3. xfrm: Wait for an RCU grace period before freeing device state Let RCU readers of the driver state finish before the free.
4. Revert "xfrm: serialize state GC with device state flush" Remove the mutex that the fixes above make redundant.
Jianbo Liu (4):
xfrm: Give GC list nodes their own storage
xfrm: Serialize device state teardown
xfrm: Wait for an RCU grace period before freeing device state Revert "xfrm: serialize state GC with device state flush"
include/net/xfrm.h | 10 +++---- net/xfrm/xfrm_device.c | 4 +-- net/xfrm/xfrm_state.c | 61 ++++++++++++++++++++++++++---------------- 3 files changed, 44 insertions(+), 31 deletions(-)
base-commit: b88c75488ecc768acadb14e980bbfa087b1dbf38
Signature · 1 line
2.39.3
PATCH 1/4 xfrm: Give GC list nodes their own storage
gclist currently shares storage with bydst, and dev_gclist shares storage with bysrc.
This is unsafe for bydst and bysrc, which can be traversed under RCU without holding xfrm_state_lock. __xfrm_state_delete() unlinks these nodes with hlist_del_init_rcu(), so a reader that started before the unlink may still be following their linkage after the state has been removed from the hash table.
Later, when the last reference is dropped, __xfrm_state_destroy() adds x->gclist to xfrm_state_gc_list. Since gclist and bydst currently share the same storage, adding the state to the GC list can overwrite the bydst linkage while an RCU reader is still using it. The same issue applies to dev_gclist and bysrc.
Give gclist, bydst, and bysrc separate storage so their hash-list linkage remains valid for outstanding RCU readers. Share dev_gclist with byseq instead. byseq has no RCU readers, only __xfrm_find_acq_byseq() under xfrm_state_lock, and the state is unlinked from byseq before dev_gclist is used. Sharing keeps the growth of struct xfrm_state at 16 bytes rather than 32.
--- include/net/xfrm.h | 10 ++++------ net/xfrm/xfrm_state.c | 1 + 2 files changed, 5 insertions(+), 6 deletions(-)
include/net/xfrm.h +4 -6
@@ -189,16 +189,14 @@ enum xfrm_replay_mode {/* Full description of state of transformer. */ struct xfrm_state {possible_net_t xs_net; -union { -struct hlist_node gclist; -struct hlist_node bydst; - }; +struct hlist_node bydst; +struct hlist_node bysrc; +struct hlist_node gclist;union {struct hlist_node dev_gclist; -struct hlist_node bysrc; +struct hlist_node byseq; };struct hlist_node byspi; -struct hlist_node byseq;struct hlist_node state_cache;struct hlist_node state_cache_input;
net/xfrm/xfrm_state.c +1 -0
@@ -751,6 +751,7 @@ struct xfrm_state *xfrm_state_alloc(struct net *net)INIT_HLIST_NODE (&x->state_cache);INIT_HLIST_NODE (&x->bydst);INIT_HLIST_NODE (&x->bysrc); +INIT_HLIST_NODE (&x->gclist);INIT_HLIST_NODE (&x->byspi);INIT_HLIST_NODE (&x->byseq); hrtimer_setup(&x->mtimer, xfrm_timer_handler, CLOCK_BOOTTIME,
Signature · 1 line
2.39.3
PATCH 2/4 xfrm: Serialize device state teardown
In reply to Jianbo LiuCommit 89fefad9f971 ("xfrm: serialize state GC with device state flush") stopped the state GC and xfrm_dev_state_flush() from freeing the same device state at once, but a third caller is not covered.
xfrm_state_find() frees a packet offload acquire state when km_query() fails. It runs under RCU and can't take the mutex. xfrm_dev_state_delete() first puts the state on xfrm_state_dev_gc_list with xso->dev still set, so a device flush can find it there and call xfrm_dev_state_free() too. Both callers see xso->dev set and both unlink the state, so the second hlist_del() hits a poisoned node.
Claim the device in xfrm_dev_state_free() by clearing xso->dev under x->lock and xfrm_state_dev_gc_lock, so only one caller unlinks the state, invokes xdo_dev_state_free() and drops the netdevice reference. x->lock also keeps xso->dev stable for the stats updates that hold it.
In the flush, take a state reference before removing a state from the device GC list, so it stays alive while the flush owns it. Skip a state with no reference left, the state GC frees it.
Keep xso->type unchanged after detaching the device, so lockless readers of xso->type do not see a packet offload state as a software state. Reject offloaded states with no device in xfrm_dev_offload_ok(): a packet offload state would otherwise pass the !dev check there, and xfrm_output() would send the packet with no ESP or use the NULL device in xfrm_dev_direct_output().
Found by the AI review of the commit above (see Link) and reproduced on ConnectX-7 with a packet offload policy that has no SA, an acquire listener that never reads, UDP senders hitting the policy and the device taken down in a loop:
[ 167.689163] Oops: general protection fault, probably for non-canonical address 0xdead000000000122: 0000 [#1] SMP [ 167.690938] CPU: 11 UID: 0 PID: 2093 Comm: python3 Not tainted 7.4.0-rc1+ #10 PREEMPTLAZY [ 167.694337] RIP: 0010:xfrm_dev_state_free+0x41/0xe0 [ 167.699068] RAX: dead000000000122 RBX: ff1100014f543800 RCX: ff11000103bbf800 [ 167.700247] RDX: dead000000000100 RSI: 0000000000000001 RDI: ffffffff82da8c68 [ 167.716897] Call Trace: [ 167.717405] <TASK> [ 167.717863] xfrm_state_find+0x19b0/0x1ab0 [ 167.718629] xfrm_resolve_and_create_bundle+0x17d/0x11e0 [ 167.722070] xfrm_lookup_with_ifid+0x1e7/0xb50 [ 167.722872] xfrm_lookup_route+0x1d/0xa0 [ 167.723593] udp_sendmsg+0x8da/0xc90 [ 167.724273] __sock_sendmsg+0x38/0x70 [ 167.724988] __sys_sendto+0x119/0x180 [ 167.727954] __x64_sys_sendto+0x20/0x30 [ 167.728666] do_syscall_64+0x72/0x3b0 [ 167.729357] entry_SYSCALL_64_after_hwframe+0x4b/0x53 [ 167.741064] </TASK> [ 167.752388] Kernel panic - not syncing: Fatal exception in interrupt
With this series the same test ran 400 cycles with no crash, and 150 cycles on a kernel with KASAN and lockdep.
--- net/xfrm/xfrm_device.c | 4 ++-- net/xfrm/xfrm_state.c | 51 ++++++++++++++++++++++++++++-------------- 2 files changed, 36 insertions(+), 19 deletions(-)
net/xfrm/xfrm_device.c +2 -2
@@ -422,11 +422,11 @@ bool xfrm_dev_offload_ok(struct sk_buff *skb, struct xfrm_state *x)int mtu;struct dst_entry *dst =skb_dst (skb);struct xfrm_dst *xdst = (struct xfrm_dst *)dst; -struct net_device *dev = x->xso.dev; +struct net_device *dev =READ_ONCE (x->xso.dev);bool check_tunnel_size;if (!x->type_offload || - (x->xso.type ==XFRM_DEV_OFFLOAD_UNSPECIFIED && x->encap)) + (x->xso.type ==XFRM_DEV_OFFLOAD_UNSPECIFIED ? !!x->encap : !dev))return false; if ((!dev || dev == xfrm_dst_path(dst)->dev) &&
net/xfrm/xfrm_state.c +34 -17
81 lines not on this page · raw message
Signature · 1 line
2.39.3
PATCH 3/4 xfrm: Wait for an RCU grace period before freeing device state
In reply to Jianbo Liuxfrm_dev_state_flush() can call xdo_dev_state_free() immediately after xdo_dev_state_delete(), without waiting for pre-existing RCU readers of the driver's SA state to complete.
The normal state GC path waits for an RCU grace period before destroying the state, but the device flush path does not.
Remove the immediate xfrm_dev_state_free() following xfrm_state_delete(). The delete path queues the state on xfrm_state_dev_gc_list. The subsequent collection pass picks it up if a reference is left. If the flush dropped the last one, the state GC frees it after its own grace period and xfrm_flush_gc() waits for it.
Wait for one RCU grace period after collecting the states and before freeing them. This establishes the required xdo_dev_state_delete() -> synchronize_rcu() -> xdo_dev_state_free() ordering while using a single grace period for the whole flush.
--- net/xfrm/xfrm_state.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
net/xfrm/xfrm_state.c +3 -1
@@ -1000,7 +1000,6 @@ int xfrm_dev_state_flush(struct net *net, struct net_device *dev, bool task_valispin_unlock_bh (&net->xfrm.xfrm_state_lock); err =xfrm_state_delete (x); -xfrm_dev_state_free (x);xfrm_audit_state_delete (x, err ?0 :1 , task_valid);
@@ -1038,6 +1037,9 @@ int xfrm_dev_state_flush(struct net *net, struct net_device *dev, bool task_valispin_unlock_bh (&xfrm_state_dev_gc_lock);mutex_unlock (&xfrm_state_gc_mutex); +if (!hlist_empty (&flush_list)) +synchronize_rcu (); +hlist_for_each_entry_safe (x, tmp, &flush_list, gclist) {hlist_del_init (&x->gclist);xfrm_dev_state_free (x);
Signature · 1 line
2.39.3
PATCH 4/4 Revert "xfrm: serialize state GC with device state flush"
In reply to Jianbo LiuThis reverts commit 89fefad9f971bc637fb22373078144f2563c4be9.
The mutex kept the state GC from destroying a state while xfrm_dev_state_flush() was freeing its device state. It can't cover xfrm_state_find(), which frees device state under RCU.
The flush now holds a reference on each state it frees, and xfrm_dev_state_free() lets only one caller free the device state. Nothing is left for the mutex to protect, so remove it.
This needs "xfrm: Serialize device state teardown". Without that patch the flush frees the device state of a state it holds no reference on.
--- net/xfrm/xfrm_state.c | 5 ----- 1 file changed, 5 deletions(-)
net/xfrm/xfrm_state.c +0 -5
@@ -226,7 +226,6 @@ static struct xfrm_state_afinfo __rcu *xfrm_state_afinfo[NPROTO];static DEFINE_SPINLOCK (xfrm_state_gc_lock);static DEFINE_SPINLOCK (xfrm_state_dev_gc_lock); -static DEFINE_MUTEX (xfrm_state_gc_mutex);int __xfrm_state_delete (struct xfrm_state *x);
@@ -639,10 +638,8 @@ static void xfrm_state_gc_task(struct work_struct *work)synchronize_rcu (); -mutex_lock (&xfrm_state_gc_mutex);hlist_for_each_entry_safe (x, tmp, &gc_list, gclist)xfrm_state_gc_destroy (x); -mutex_unlock (&xfrm_state_gc_mutex); }static enum hrtimer_restart xfrm_timer_handler (struct hrtimer *me)
@@ -1018,7 +1015,6 @@ int xfrm_dev_state_flush(struct net *net, struct net_device *dev, bool task_vali out:spin_unlock_bh (&net->xfrm.xfrm_state_lock); -mutex_lock (&xfrm_state_gc_mutex);spin_lock_bh (&xfrm_state_dev_gc_lock);hlist_for_each_entry_safe (x, tmp, &xfrm_state_dev_gc_list, dev_gclist) { xso = &x->xso;
@@ -1035,7 +1031,6 @@ int xfrm_dev_state_flush(struct net *net, struct net_device *dev, bool task_vali } }spin_unlock_bh (&xfrm_state_dev_gc_lock); -mutex_unlock (&xfrm_state_gc_mutex);if (!hlist_empty (&flush_list))synchronize_rcu ();
Signature · 1 line
2.39.3