Thread View: linux.kernel.netdev
5 messages
5 total messages
Started by Ralf Lici
Thu, 26 Nov 2026 11:14
[PATCH ipsec] xfrm: advance the route generation when policies are removed
Author: Ralf Lici
Date: Thu, 26 Nov 2026 11:14
Date: Thu, 26 Nov 2026 11:14
124 lines
13049 bytes
13049 bytes
Only xfrm_policy_insert and xfrm_set_default advance the IPv4 and IPv6
route generations. Deleting, flushing or expiring a policy does not, so
a route that a socket cached while an allow policy without templates
matched its flow keeps validating after that policy is removed, and the
socket keeps sending in plaintext although a policy requiring IPsec now
matches. xfrm_policy_insert also advances the generation before
unlinking the policy that the new one replaces, which can still be
matched until then, since the new policy can be linked behind it.
Advance the generation once a policy is unlinked by a delete or by its
expiry, once after all policies are unlinked by a flush, and after the
replaced policy is unlinked by an insert. Socket policies only apply to
their own socket and are left out.
Fixes: ee8372dd1989 ("xfrm: invalidate dst on policy insertion/deletion")
Signed-off-by: Ralf Lici <ralf@mandelbit.com>
---
Found while auditing the writers of the route generation, where the
xfrm_policy_insert ordering was also reported by Sashiko:
https://lore.kernel.org/netdev/cover.1795537128.git.ralf@mandelbit.com/
net/xfrm/xfrm_policy.c | 34 ++++++++++++++++++++++++----------
1 file changed, 24 insertions(+), 10 deletions(-)
diff --git a/net/xfrm/xfrm_policy.c b/net/xfrm/xfrm_policy.c
index d2e938d4db9a..47379da24e99 100644
--- a/net/xfrm/xfrm_policy.c
+++ b/net/xfrm/xfrm_policy.c
@@ -1569,6 +1569,14 @@ static struct xfrm_policy *xfrm_policy_insert_list(struct hlist_head *chain,
return delpol;
}
+static void xfrm_policy_bump_rt_genid(struct net *net, unsigned short family)
+{
+ if (family == AF_INET)
+ rt_genid_bump_ipv4(net);
+ else
+ rt_genid_bump_ipv6(net);
+}
+
int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
{
struct net *net = xp_net(policy);
@@ -1589,16 +1597,13 @@ int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
__xfrm_policy_link(policy, dir);
- /* After previous checking, family can either be AF_INET or AF_INET6 */
- if (policy->family == AF_INET)
- rt_genid_bump_ipv4(net);
- else
- rt_genid_bump_ipv6(net);
-
if (delpol) {
xfrm_policy_requeue(delpol, policy);
__xfrm_policy_unlink(delpol, dir);
}
+
+ /* Advance the route generation only once the replaced policy is gone */
+ xfrm_policy_bump_rt_genid(net, policy->family);
policy->index = delpol ? delpol->index : xfrm_gen_index(net, dir, policy->index);
hlist_add_head(&policy->byidx, net->xfrm.policy_byidx+idx_hash(net, policy->index));
policy->curlft.add_time = ktime_get_real_seconds();
@@ -1695,6 +1700,7 @@ xfrm_policy_bysel_ctx(struct net *net, const struct xfrm_mark *mark, u32 if_id,
return pol;
}
__xfrm_policy_unlink(pol, dir);
+ xfrm_policy_bump_rt_genid(net, pol->family);
}
ret = pol;
}
@@ -1735,6 +1741,7 @@ xfrm_policy_byid(struct net *net, const struct xfrm_mark *mark, u32 if_id,
return pol;
}
__xfrm_policy_unlink(pol, dir);
+ xfrm_policy_bump_rt_genid(net, pol->family);
}
ret = pol;
break;
@@ -1835,10 +1842,12 @@ int xfrm_policy_flush(struct net *net, u8 type, bool task_valid)
spin_lock_bh(&net->xfrm.xfrm_policy_lock);
goto again;
}
- if (cnt)
+ if (cnt) {
+ rt_genid_bump_all(net);
__xfrm_policy_inexact_flush(net);
- else
+ } else {
err = -ESRCH;
+ }
out:
spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
return err;
@@ -1875,10 +1884,12 @@ int xfrm_dev_policy_flush(struct net *net, struct net_device *dev,
spin_lock_bh(&net->xfrm.xfrm_policy_lock);
goto again;
}
- if (cnt)
+ if (cnt) {
+ rt_genid_bump_all(net);
__xfrm_policy_inexact_flush(net);
- else
+ } else {
err = -ESRCH;
+ }
out:
spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
return err;
@@ -2365,6 +2376,9 @@ int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
spin_lock_bh(&net->xfrm.xfrm_policy_lock);
pol = __xfrm_policy_unlink(pol, dir);
+ /* Socket policies only apply to their own socket. */
+ if (pol && dir < XFRM_POLICY_MAX)
+ xfrm_policy_bump_rt_genid(net, pol->family);
spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
if (pol) {
xfrm_policy_kill(pol);
base-commit: 2803867cf3defa294710088a4663b434e12b5e93
--
2.56.0
Re: [PATCH ipsec] xfrm: advance the route generation when policies are removed
Author: netdev-bot+sinfo
Date: Thu, 26 Nov 2026 11:18
Date: Thu, 26 Nov 2026 11:18
19 lines
8644 bytes
8644 bytes
Hi! This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information: - Whether the issue was actually triggered, or is only theoretical (e.g. found by LLM and not proven at runtime). If it was triggered please include the symptoms, like the stack trace or kernel messages. Please do not repost the series just to address the above. Instead, reply to this email with the missing information, so that reviewers can take it into account. *IMPORTANT*: If you do repost (for other reasons) - please include the relevant info in the cover letter or commit messages. -- The evaluation is done by an LLM so it may be wrong, if you think that is the case please reply and explain.
Re: [PATCH ipsec] xfrm: advance the route generation when policies are removed
Author: Ralf Lici
Date: Thu, 26 Nov 2026 11:32
Date: Thu, 26 Nov 2026 11:32
53 lines
11199 bytes
11199 bytes
On Thu, 26 Nov 2026 11:18:44 +0000, netdev-bot+sinfo@kernel.org wrote: > Hi! > > This is an automated message. This series looks like a fix, but its > commit messages seem to be missing some information: > > - Whether the issue was actually triggered, or is only theoretical > (e.g. found by LLM and not proven at runtime). If it was triggered > please include the symptoms, like the stack trace or kernel messages. > Yes, the policy removal cases were triggered at runtime, in a VM test setup (not observed in production AFAIK). There is no kernel message, the symptom is plaintext on the wire. A connected UDP socket keeps sending unencrypted datagrams after the allow policy that exempted its flow is removed, although the ESP policy below it now matches. This is roughly the setup, in a netns with a veth pair (IPv4 is the same): ip xfrm state add src fd00::1 dst fd01::2 proto esp spi 0x1001 \ reqid 1 mode transport aead 'rfc4106(gcm(aes))' <key> 128 ip xfrm policy add src fd00::1 dst fd01::2 dir out priority 100 \ tmpl src fd00::1 dst fd01::2 proto esp reqid 1 mode transport ip xfrm policy add src fd00::1 dst fd01::2 proto udp dport 5002 \ dir out priority 10 action allow A UDP socket connected to [fd01::2]:5002 sends one datagram, the allow policy is removed, then the socket sends 10 more, captured on the veth: removal net-next ipsec + this patch delete by selector 10 plain, 0 ESP 0 plain, 10 ESP delete by index 10 plain, 0 ESP 0 plain, 10 ESP hard time expiry 10 plain, 0 ESP 0 plain, 10 ESP flush 10 plain nothing sent The flush case replaces the ESP policy with "ip xfrm policy setdefault out block", so that nothing may be sent once the policies are flushed. The xfrm_policy_insert ordering is a race between the insert and a concurrent lookup, found by code reading. That I did not reproduce at runtime. I can share the reproducer script if useful. Thanks, -- Ralf Lici Mandelbit Srl
Re: [PATCH ipsec] xfrm: advance the route generation when policies are removed
Author: Nicolas Dichtel
Date: Thu, 26 Nov 2026 14:54
Date: Thu, 26 Nov 2026 14:54
52 lines
13749 bytes
13749 bytes
Le 26/11/2026 à 12:32, Ralf Lici a écrit :> On Thu, 26 Nov 2026 11:18:44 +0000, netdev-bot+sinfo@kernel.org wrote: >> Hi! >> >> This is an automated message. This series looks like a fix, but its >> commit messages seem to be missing some information: >> >> - Whether the issue was actually triggered, or is only theoretical >> (e.g. found by LLM and not proven at runtime). If it was triggered >> please include the symptoms, like the stack trace or kernel messages. >> > > Yes, the policy removal cases were triggered at runtime, in a VM test > setup (not observed in production AFAIK). > > There is no kernel message, the symptom is plaintext on the wire. A > connected UDP socket keeps sending unencrypted datagrams after the allow > policy that exempted its flow is removed, although the ESP policy below > it now matches. > > This is roughly the setup, in a netns with a veth pair (IPv4 is the > same): > > ip xfrm state add src fd00::1 dst fd01::2 proto esp spi 0x1001 \ > reqid 1 mode transport aead 'rfc4106(gcm(aes))' <key> 128 > ip xfrm policy add src fd00::1 dst fd01::2 dir out priority 100 \ > tmpl src fd00::1 dst fd01::2 proto esp reqid 1 mode transport > ip xfrm policy add src fd00::1 dst fd01::2 proto udp dport 5002 \ > dir out priority 10 action allow > > A UDP socket connected to [fd01::2]:5002 sends one datagram, the allow > policy is removed, then the socket sends 10 more, captured on the veth: > > removal net-next ipsec + this patch > delete by selector 10 plain, 0 ESP 0 plain, 10 ESP > delete by index 10 plain, 0 ESP 0 plain, 10 ESP > hard time expiry 10 plain, 0 ESP 0 plain, 10 ESP > flush 10 plain nothing sent > > The flush case replaces the ESP policy with "ip xfrm policy setdefault > out block", so that nothing may be sent once the policies are flushed. > > The xfrm_policy_insert ordering is a race between the insert and a > concurrent lookup, found by code reading. That I did not reproduce at > runtime. > > I can share the reproducer script if useful. Maybe adding a selftest would be useful. Regards, Nicolas
Re: [PATCH ipsec] xfrm: advance the route generation when policies are removed
Author: Ralf Lici
Date: Thu, 26 Nov 2026 18:06
Date: Thu, 26 Nov 2026 18:06
59 lines
11766 bytes
11766 bytes
On Thu, 26 Nov 2026 15:54:36 +0100, Nicolas Dichtel <nicolas.dichtel@6wind.com> wrote: > Le 26/11/2026 à 12:32, Ralf Lici a écrit :> On Thu, 26 Nov 2026 11:18:44 +0000, > netdev-bot+sinfo@kernel.org wrote: > >> Hi! > >> > >> This is an automated message. This series looks like a fix, but its > >> commit messages seem to be missing some information: > >> > >> - Whether the issue was actually triggered, or is only theoretical > >> (e.g. found by LLM and not proven at runtime). If it was triggered > >> please include the symptoms, like the stack trace or kernel messages. > >> > > > > Yes, the policy removal cases were triggered at runtime, in a VM test > > setup (not observed in production AFAIK). > > > > There is no kernel message, the symptom is plaintext on the wire. A > > connected UDP socket keeps sending unencrypted datagrams after the allow > > policy that exempted its flow is removed, although the ESP policy below > > it now matches. > > > > This is roughly the setup, in a netns with a veth pair (IPv4 is the > > same): > > > > ip xfrm state add src fd00::1 dst fd01::2 proto esp spi 0x1001 \ > > reqid 1 mode transport aead 'rfc4106(gcm(aes))' <key> 128 > > ip xfrm policy add src fd00::1 dst fd01::2 dir out priority 100 \ > > tmpl src fd00::1 dst fd01::2 proto esp reqid 1 mode transport > > ip xfrm policy add src fd00::1 dst fd01::2 proto udp dport 5002 \ > > dir out priority 10 action allow > > > > A UDP socket connected to [fd01::2]:5002 sends one datagram, the allow > > policy is removed, then the socket sends 10 more, captured on the veth: > > > > removal net-next ipsec + this patch > > delete by selector 10 plain, 0 ESP 0 plain, 10 ESP > > delete by index 10 plain, 0 ESP 0 plain, 10 ESP > > hard time expiry 10 plain, 0 ESP 0 plain, 10 ESP > > flush 10 plain nothing sent > > > > The flush case replaces the ESP policy with "ip xfrm policy setdefault > > out block", so that nothing may be sent once the policies are flushed. > > > > The xfrm_policy_insert ordering is a race between the insert and a > > concurrent lookup, found by code reading. That I did not reproduce at > > runtime. > > > > I can share the reproducer script if useful. > Maybe adding a selftest would be useful. > Sure, I'll turn my reproducer into a selftest and add it in v2. Cheers! -- Ralf Lici Mandelbit Srl
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads