[PATCH net] af_key: validate the second sockaddr length in parse_sockaddr_pair()

From: Fabrice Derepas

Date: Wed Dec 02 2026 - 04:06:10 EST


parse_sockaddr_pair() first checks that the extension is large enough to
hold two sockaddrs of the first sockaddr's family:

if (ext_len < 2 || ext_len < pfkey_sockaddr_pair_size(sa->sa_family))
return -EINVAL;

It then extracts the first address, derives socklen from the first
family, and extracts a second address at offset socklen. The second
pfkey_sockaddr_extract() decides how many bytes to copy from the second
sockaddr's own sa_family field, which is independent attacker-controlled
data, and it copies the address before the caller's "!= af" family-match
check runs. When the first family is AF_INET (socklen 16, guard 32) and
the second sockaddr declares AF_INET6, the sin6_addr copy reads bytes
[24, 40) of the extension, 8 bytes past the 32-byte region the length
guard validated, before the family mismatch is detected and -EINVAL
returned.

On the PF_KEY message path those 8 bytes lie inside the skb head
allocation that holds the message. On the setsockopt path the policy is
an exact-size kmalloc() copy of the user buffer, so when the sockaddr
pair is the last item in the policy, the read runs 8 bytes past the end
of that object. The copied bytes are dropped with the policy on the
-EINVAL return; nothing is written out of bounds or returned to user
space.

The read is reachable from PF_KEY SADB_X_SPDADD (a tunnel-mode
ipsecrequest, via parse_ipsecrequest()), from setsockopt(IP_IPSEC_POLICY
/ IPV6_IPSEC_POLICY) via xfrm_user_policy() -> pfkey_compile_policy(),
and from the MIGRATE path (CONFIG_NET_KEY_MIGRATE). Each requires
CAP_NET_ADMIN in the socket netns' user namespace, which an unprivileged
process can obtain by unshare(CLONE_NEWUSER|CLONE_NEWNET).

Validate that the extension also covers the second sockaddr's declared
family before extracting it, so the second read never exceeds the length
that was checked. Messages whose two families match are unaffected (the
added bound equals the existing pair-size guard); a mismatched, larger
second family is now rejected before the copy instead of after it.

Found with AWP (https://github.com/canonical/awp).

Fixes: 5f95ac9111f7 ("key: Share common code path to extract address from sockaddr{}.")
Signed-off-by: Fabrice Derepas <fabrice.derepas@xxxxxxxxxxxxx>
---

This was first sent to security@xxxxxxxxxx, who asked me to post it to
netdev directly, given the limited impact and PF_KEY being deprecated
and disabled by default upstream. It is an 8-byte out-of-bounds read,
reachable by an unprivileged user through a user+network namespace. As
far as I can tell it has no effect beyond the read itself (no write,
nothing returned to user space), so I consider this a hardening fix.

Testing (real kernel runs, v7.4-rc4 b6a9fa34ce41, x86_64 defconfig +
CONFIG_KASAN=y/KASAN_INLINE + CONFIG_NET_KEY=y + CONFIG_IPV6=y +
CONFIG_USER_NS=y, booted under QEMU/TCG):
- A KUnit case calling parse_sockaddr_pair() with a 32-byte kmalloc
object (first sockaddr AF_INET, second AF_INET6) gives, unpatched:
BUG: KASAN: slab-out-of-bounds in parse_sockaddr_pair
Read of size 16 ... located 24 bytes inside of ... kmalloc-32
i.e. the sin6_addr copy reads [24,40), 8 bytes past the object.
Patched: the case returns -EINVAL with no KASAN report.
- Reachability: an unprivileged process (uid 1000) that does
unshare(CLONE_NEWUSER|CLONE_NEWNET) reaches the second extract with
an INET-then-INET6 pair both via a PF_KEY SADB_X_SPDADD tunnel
ipsecrequest and via setsockopt(IPV6_IPSEC_POLICY) (xfrm_user_policy
-> pfkey_compile_policy -> parse_ipsecrequests ->
parse_ipsecrequest); a temporary trace printk confirmed both. The
pair is rejected with -EINVAL after the copy on every path, and a
valid AF_INET/AF_INET tunnel policy is accepted before and after
the patch.

What KASAN did and did not show: the only KASAN report is the KUnit case
above (a tightly sized 32-byte object). Neither live syscall path
produced one. On the PF_KEY sendmsg path that is expected: the 8 bytes
are inside the skb head allocation, which KASAN does not flag. On the
setsockopt path memdup_sockptr() makes an exact-size copy (64 bytes in
my test), so, from reading the source, the copy runs 8 bytes past that
object. 20000 such calls gave no report, but they ran after the KUnit
report in the same boot, which had already consumed KASAN's one-shot
report (kasan_multi_shot was not set), so that run neither confirms nor
refutes KASAN visibility on this path. The read past the allocation is
therefore observed for the KUnit case only, and inferred from the
source for setsockopt.

The read has no further effect: the copied bytes land in daddr, which is
discarded when the containing xfrm_policy is destroyed on the -EINVAL
return.

Note that PF_KEY was marked deprecated in commit 6b3b6e59c4f8 ("pfkey:
Deprecate pfkey") with removal planned for 2027; the code is still built
and reachable where CONFIG_NET_KEY is enabled (common in distros).

The C reproducer and the KUnit harness are available on request.

net/key/af_key.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/net/key/af_key.c b/net/key/af_key.c
index d215b33b7..ef2c360e6 100644
--- a/net/key/af_key.c
+++ b/net/key/af_key.c
@@ -2491,6 +2491,7 @@ static int parse_sockaddr_pair(struct sockaddr *sa, int ext_len,
xfrm_address_t *saddr, xfrm_address_t *daddr,
u16 *family)
{
+ struct sockaddr *sa2;
int af, socklen;

if (ext_len < 2 || ext_len < pfkey_sockaddr_pair_size(sa->sa_family))
@@ -2501,8 +2502,10 @@ static int parse_sockaddr_pair(struct sockaddr *sa, int ext_len,
return -EINVAL;

socklen = pfkey_sockaddr_len(af);
- if (pfkey_sockaddr_extract((struct sockaddr *) (((u8 *)sa) + socklen),
- daddr) != af)
+ sa2 = (struct sockaddr *)(((u8 *)sa) + socklen);
+ if (ext_len < socklen + pfkey_sockaddr_len(sa2->sa_family))
+ return -EINVAL;
+ if (pfkey_sockaddr_extract(sa2, daddr) != af)
return -EINVAL;

*family = af;

base-commit: b6a9fa34ce4187cb69d3c588a4bbffc31ce5db8f
--
2.53.0