Skip to content

Commit 94f3980

Browse files
aakashksgklassert
authored andcommitted
xfrm: Duplicate SPI Handling
The issue originates when Strongswan initiates an XFRM_MSG_ALLOCSPI Netlink message, which triggers the kernel function xfrm_alloc_spi(). This function is expected to ensure uniqueness of the Security Parameter Index (SPI) for inbound Security Associations (SAs). However, it can return success even when the requested SPI is already in use, leading to duplicate SPIs assigned to multiple inbound SAs, differentiated only by their destination addresses. This behavior causes inconsistencies during SPI lookups for inbound packets. Since the lookup may return an arbitrary SA among those with the same SPI, packet processing can fail, resulting in packet drops. According to RFC 4301 section 4.4.2 , for inbound processing a unicast SA is uniquely identified by the SPI and optionally protocol. Reproducing the Issue Reliably: To consistently reproduce the problem, restrict the available SPI range in charon.conf : spi_min = 0x10000000 spi_max = 0x10000002 This limits the system to only 2 usable SPI values. Next, create more than 2 Child SA. each using unique pair of src/dst address. As soon as the 3rd Child SA is initiated, it will be assigned a duplicate SPI, since the SPI pool is already exhausted. With a narrow SPI range, the issue is consistently reproducible. With a broader/default range, it becomes rare and unpredictable. Current implementation: xfrm_spi_hash() lookup function computes hash using daddr, proto, and family. So if two SAs have the same SPI but different destination addresses, then they will: a. Hash into different buckets b. Be stored in different linked lists (byspi + h) c. Not be seen in the same hlist_for_each_entry_rcu() iteration. As a result, the lookup will result in NULL and kernel allows that Duplicate SPI Proposed Change: xfrm_state_lookup_spi_proto() does a truly global search - across all states, regardless of hash bucket and matches SPI and proto. Signed-off-by: Aakash Kumar S <[email protected]> Acked-by: Herbert Xu <[email protected]> Signed-off-by: Steffen Klassert <[email protected]>
1 parent b05d42e commit 94f3980

File tree

1 file changed

+43
-29
lines changed

1 file changed

+43
-29
lines changed

net/xfrm/xfrm_state.c

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1711,6 +1711,26 @@ struct xfrm_state *xfrm_state_lookup_byspi(struct net *net, __be32 spi,
17111711
}
17121712
EXPORT_SYMBOL(xfrm_state_lookup_byspi);
17131713

1714+
static struct xfrm_state *xfrm_state_lookup_spi_proto(struct net *net, __be32 spi, u8 proto)
1715+
{
1716+
struct xfrm_state *x;
1717+
unsigned int i;
1718+
1719+
rcu_read_lock();
1720+
for (i = 0; i <= net->xfrm.state_hmask; i++) {
1721+
hlist_for_each_entry_rcu(x, &net->xfrm.state_byspi[i], byspi) {
1722+
if (x->id.spi == spi && x->id.proto == proto) {
1723+
if (!xfrm_state_hold_rcu(x))
1724+
continue;
1725+
rcu_read_unlock();
1726+
return x;
1727+
}
1728+
}
1729+
}
1730+
rcu_read_unlock();
1731+
return NULL;
1732+
}
1733+
17141734
static void __xfrm_state_insert(struct xfrm_state *x)
17151735
{
17161736
struct net *net = xs_net(x);
@@ -2555,10 +2575,8 @@ int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high,
25552575
unsigned int h;
25562576
struct xfrm_state *x0;
25572577
int err = -ENOENT;
2558-
__be32 minspi = htonl(low);
2559-
__be32 maxspi = htonl(high);
2578+
u32 range = high - low + 1;
25602579
__be32 newspi = 0;
2561-
u32 mark = x->mark.v & x->mark.m;
25622580

25632581
spin_lock_bh(&x->lock);
25642582
if (x->km.state == XFRM_STATE_DEAD) {
@@ -2572,38 +2590,34 @@ int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high,
25722590

25732591
err = -ENOENT;
25742592

2575-
if (minspi == maxspi) {
2576-
x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family);
2577-
if (x0) {
2578-
NL_SET_ERR_MSG(extack, "Requested SPI is already in use");
2579-
xfrm_state_put(x0);
2593+
for (h = 0; h < range; h++) {
2594+
u32 spi = (low == high) ? low : get_random_u32_inclusive(low, high);
2595+
newspi = htonl(spi);
2596+
2597+
spin_lock_bh(&net->xfrm.xfrm_state_lock);
2598+
x0 = xfrm_state_lookup_spi_proto(net, newspi, x->id.proto);
2599+
if (!x0) {
2600+
x->id.spi = newspi;
2601+
h = xfrm_spi_hash(net, &x->id.daddr, newspi, x->id.proto, x->props.family);
2602+
XFRM_STATE_INSERT(byspi, &x->byspi, net->xfrm.state_byspi + h, x->xso.type);
2603+
spin_unlock_bh(&net->xfrm.xfrm_state_lock);
2604+
err = 0;
25802605
goto unlock;
25812606
}
2582-
newspi = minspi;
2583-
} else {
2584-
u32 spi = 0;
2585-
for (h = 0; h < high-low+1; h++) {
2586-
spi = get_random_u32_inclusive(low, high);
2587-
x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
2588-
if (x0 == NULL) {
2589-
newspi = htonl(spi);
2590-
break;
2591-
}
2592-
xfrm_state_put(x0);
2607+
xfrm_state_put(x0);
2608+
spin_unlock_bh(&net->xfrm.xfrm_state_lock);
2609+
2610+
if (signal_pending(current)) {
2611+
err = -ERESTARTSYS;
2612+
goto unlock;
25932613
}
2614+
2615+
if (low == high)
2616+
break;
25942617
}
2595-
if (newspi) {
2596-
spin_lock_bh(&net->xfrm.xfrm_state_lock);
2597-
x->id.spi = newspi;
2598-
h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
2599-
XFRM_STATE_INSERT(byspi, &x->byspi, net->xfrm.state_byspi + h,
2600-
x->xso.type);
2601-
spin_unlock_bh(&net->xfrm.xfrm_state_lock);
26022618

2603-
err = 0;
2604-
} else {
2619+
if (err)
26052620
NL_SET_ERR_MSG(extack, "No SPI available in the requested range");
2606-
}
26072621

26082622
unlock:
26092623
spin_unlock_bh(&x->lock);

0 commit comments

Comments
 (0)