Netdev List
 help / color / mirror / Atom feed
From: netdev-bot+sashiko@kernel.org
To: bsdhenrymartin@gmail.com
Cc: steffen.klassert@secunet.com, herbert@gondor.apana.org.au,
	davem@davemloft.net, edumazet@kernel.org, kuba@kernel.org,
	pabeni@redhat.com, horms@kernel.org, netdev@vger.kernel.org,
	ipsec@lists.linux.dev, stable@vger.kernel.org
Subject: Re: [PATCH net] pfkey: fix kernel stack leak in pfkey_migrate()
Date: Tue, 24 Nov 2026 05:39:00 +0000	[thread overview]
Message-ID: <179549874061.3135.6550381232746686569@kernel.org> (raw)
In-Reply-To: <20261120052814.3686573-1-bsdhenrymartin@gmail.com>

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.

> 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?

> diff --git a/net/key/af_key.c b/net/key/af_key.c
> index d215b33b7492c..6d116862b20d8 100644
> --- a/net/key/af_key.c
> +++ b/net/key/af_key.c
> @@ -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 = {}; ?

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

      parent reply	other threads:[~2026-11-24  5:39 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-20  5:28 [PATCH net] pfkey: fix kernel stack leak in pfkey_migrate() Henry Martin
2026-11-20  5:33 ` netdev-bot+sinfo
2026-11-24  5:39 ` netdev-bot+sashiko [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=179549874061.3135.6550381232746686569@kernel.org \
    --to=netdev-bot+sashiko@kernel.org \
    --cc=bsdhenrymartin@gmail.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=herbert@gondor.apana.org.au \
    --cc=horms@kernel.org \
    --cc=ipsec@lists.linux.dev \
    --cc=kuba@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=stable@vger.kernel.org \
    --cc=steffen.klassert@secunet.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox