Skip to contentSkip to conversation

pfkey: fix kernel stack leak in pfkey_migrate()

PATCH New Henry Martin 3 participants 3 messages Nov 20 – Nov 24

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 pfkey: fix kernel stack leak in pfkey_migrate()

pfkey_migrate() declares struct xfrm_migrate m[XFRM_MAX_DEPTH] on the stack without initializing it. For AF_INET endpoints pfkey_sockaddr_extract() writes only the 4-byte a4 member, leaving the remaining 12 bytes of each xfrm_address_t as stack residue. xfrm_state_clone_and_setup() then memcpy()s the full 16-byte new_daddr and new_saddr into the cloned xfrm_state, and a subsequent XFRM_MSG_GETSA dump copies them back to userspace as-is. Each migrated SA leaks 24 bytes of kernel stack, which can contain kernel pointers. The same uninitialized bytes also feed the 16-byte memcmp() in xfrm_migrate_check(), making duplicate detection unreliable.

Zero-initialize the array, as xfrm_do_migrate() in xfrm_user.c already does.

This issue was discovered by Tencent CodeBuddy Security.

Fixes 08de61beab8a ("[PFKEYV2]: Extension for dynamic update of endpoint address(es)")
---
 net/key/af_key.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/key/af_key.c +1 -1
index d215b33b7492..6d116862b20d 100644
@@ -2571,7 +2571,7 @@ static int pfkey_migrate(struct sock *sk, struct sk_buff *skb,
 	struct sadb_x_policy *pol;
 	struct sadb_x_ipsecrequest *rq;
 	struct xfrm_selector sel;
-	struct xfrm_migrate m[XFRM_MAX_DEPTH];
+	struct xfrm_migrate m[XFRM_MAX_DEPTH] = {};
 	struct xfrm_kmaddress k;
 	struct net *net = sock_net(sk);
 
Signature · 1 line
2.43.7
#1 lore raw
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:

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.

Signature · 2 lines
The evaluation is done by an LLM so it may be wrong, if you think
that is the case please reply and explain.
#2 lore raw
In reply to Henry Martin
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

Critical: 0 · High: 1 · Medium: 0 · Low: 1

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [High] The fix is incomplete: pfkey_migrate() (net/key/af_key.c) still leaks kernel stack through the uninitialized struct xfrm_kmaddress k.
- [Low] The commit message understates the leak and leaves out its most direct path.

--

pfkey: fix kernel stack leak in pfkey_migrate()

This commit zero-initializes the on-stack struct xfrm_migrate m[] array in pfkey_migrate(). The unused 12 bytes of each AF_INET xfrm_address_t then no longer carry stack residue into the cloned xfrm_state or back to userspace.

Quoted 9 lines

pfkey_migrate() declares struct xfrm_migrate m[XFRM_MAX_DEPTH] on the stack without initializing it. For AF_INET endpoints pfkey_sockaddr_extract() writes only the 4-byte a4 member, leaving the remaining 12 bytes of each xfrm_address_t as stack residue. xfrm_state_clone_and_setup() then memcpy()s the full 16-byte new_daddr and new_saddr into the cloned xfrm_state, and a subsequent XFRM_MSG_GETSA dump copies them back to userspace as-is. Each migrated SA leaks 24 bytes of kernel stack, which can contain kernel pointers.

[Severity: Low]
Is this description of the leak complete? Every successful migrate also goes through the key manager notification path:

xfrm_migrate()
km_migrate()
xfrm_send_migrate()
build_migrate()
copy_to_user_migrate()

net/xfrm/xfrm_user.c:copy_to_user_migrate() {
    ...
	memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
	memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
	um.new_family = m->new_family;
	memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
	memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
    ...
}

The result is multicast to XFRMNLGRP_MIGRATE listeners without any GETSA request. For AF_INET, that was 48 uninitialized bytes per m[] entry (old_daddr, old_saddr, new_daddr and new_saddr). This included entries that matched no SA.

The = {} change does close this path for m[], so the code looks fine for m[].

Could the commit message describe the multicast path and the per-entry size as well?

net/key/af_key.c static int pfkey_migrate(struct sock *sk, struct sk_buff *skb, · line 2575

Quoted 11 lines
net/key/af_key.c +1 -1
index d215b33b7492c..6d116862b20d8 100644
@@ -2571,7 +2571,7 @@ static int pfkey_migrate(struct sock *sk, struct sk_buff *skb,
 	struct sadb_x_policy *pol;
 	struct sadb_x_ipsecrequest *rq;
 	struct xfrm_selector sel;
-	struct xfrm_migrate m[XFRM_MAX_DEPTH];
+	struct xfrm_migrate m[XFRM_MAX_DEPTH] = {};
 	struct xfrm_kmaddress k;

[Severity: High]
Can pfkey_migrate() still leak kernel stack through k here?

When the request carries a SADB_X_EXT_KMADDRESS extension, only parse_sockaddr_pair() fills k.local and k.remote:

	ret = parse_sockaddr_pair((struct sockaddr *)(kma + 1),
				  8*(kma->sadb_x_kmaddress_len) - sizeof(*kma),
				  &k.local, &k.remote, &k.family);

That call goes through pfkey_sockaddr_extract(). For AF_INET it writes only 4 of the 16 bytes:

	case AF_INET:
		xaddr->a4 =
			((struct sockaddr_in *)sa)->sin_addr.s_addr;
		return AF_INET;

pfkey_migrate() then passes kma ? &k : NULL to xfrm_migrate(). On success, the key manager path copies out the full addresses:

xfrm_migrate()
km_migrate(sel, dir, type, m, num_migrate, k, net, encap)
xfrm_send_migrate()
build_migrate()
copy_to_user_kmaddress()

net/xfrm/xfrm_user.c:copy_to_user_kmaddress() {
	...
	memset(&uk, 0, sizeof(uk));
	uk.family = k->family;
	uk.reserved = k->reserved;
	memcpy(&uk.local, &k->local, sizeof(uk.local));
	memcpy(&uk.remote, &k->remote, sizeof(uk.remote));

	return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
}

xfrm_send_migrate() then multicasts this to XFRMNLGRP_MIGRATE. For an AF_INET kmaddress, would 24 bytes of uninitialized stack (12 from k.local and 12 from k.remote) still reach userspace?

On the netlink side, xfrm_do_migrate() and copy_from_user_migrate() fill the kmaddress with full 16-byte copies. pfkey appears to be the only producer that passes in a partially written k.

pfkey_create() only checks ns_capable(net->user_ns, CAP_NET_ADMIN). That is the same privilege level needed for the GETSA leak described in the commit message.

Should this also be struct xfrm_kmaddress k = {}; ?

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261120052814.3686573-1-bsdhenrymartin%40gmail.com