xfrm: Duplicate SPI Handling – IPsec-v3 Compliance Concern
Subsystems (3)
PATCH 1/1 xfrm: Duplicate SPI Handling – IPsec-v3 Compliance Concern
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 6071, in IPsec-v3, a unicast SA is uniquely identified by the SPI alone. Therefore, relying on additional fields (such as destination addresses, proto) to disambiguate SPIs contradicts the RFC and undermines protocol correctness.
Hence, the change is necessary to enforce strict SPI uniqueness for inbound SAs, ensuring deterministic lookup behavior and compliance with the IPsec specification.
--- net/xfrm/xfrm_hash.h | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-)
net/xfrm/xfrm_hash.h +4 -11
@@ -116,18 +116,11 @@ static inline unsigned int __xfrm_src_hash(const xfrm_address_t *daddr, }static inline unsigned int -__xfrm_spi_hash (const xfrm_address_t *daddr,__be32 spi,u8 proto, -unsigned short family,unsigned int hmask) +__xfrm_spi_hash (const xfrm_address_t *__maybe_unused daddr,__be32 spi, +u8 __maybe_unused proto,unsigned short __maybe_unused family, +unsigned int hmask) { -unsigned int h = (__force u32 )spi ^ proto; -switch (family) { -case AF_INET : - h ^=__xfrm4_addr_hash (daddr); -break ; -case AF_INET6 : - h ^=__xfrm6_addr_hash (daddr); -break ; - } +unsigned int h = (__force u32 )spi;return (h ^ (h >>10 ) ^ (h >>20 )) & hmask; }
Signature · 1 line
2.48.1
Herbert Xu
maintainerQuoted 10 lines · Aakash Kumar S
static inline unsigned int
net/xfrm/xfrm_hash.h +3
-4-__xfrm_spi_hash(const xfrm_address_t *daddr, __be32 spi, u8 proto, - unsigned short family, unsigned int hmask) +__xfrm_spi_hash(const xfrm_address_t * __maybe_unused daddr, __be32 spi, + u8 __maybe_unused proto, unsigned short __maybe_unused family, + unsigned int hmask) { - unsigned int h = (__force u32)spi ^ proto; -switch (family) {
net/xfrm/xfrm_hash.h static inline unsigned int __xfrm_src_hash(const xfrm_address_t *daddr, · line 125
Quoted 10 lines
net/xfrm/xfrm_hash.h +1
-7-case AF_INET : - h ^=__xfrm4_addr_hash (daddr); -break ; -case AF_INET6 : - h ^=__xfrm6_addr_hash (daddr); -break ; - } +unsigned int h = (__force u32 )spi;return (h ^ (h >>10 ) ^ (h >>20 )) & hmask; }
I don't think this patch is sufficient. The logic around state lookups need to be changed to exclude the destination address comparison to achieve your objective.
It's also dangerous to unilaterally do this since existing deployments could rely on the old behaviour. You'd need to add a toggle for compatibility.
Cheers,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt