]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
xfrm: state: initialize state_ptrs earlier in xfrm_state_find
authorSabrina Dubroca <sd@queasysnail.net>
Fri, 23 May 2025 15:11:17 +0000 (17:11 +0200)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 1 Aug 2025 08:48:40 +0000 (09:48 +0100)
[ Upstream commit 94d077c331730510d5611b438640a292097341f0 ]

In case of preemption, xfrm_state_look_at will find a different
pcpu_id and look up states for that other CPU. If we matched a state
for CPU2 in the state_cache while the lookup started on CPU1, we will
jump to "found", but the "best" state that we got will be ignored and
we will enter the "acquire" block. This block uses state_ptrs, which
isn't initialized at this point.

Let's initialize state_ptrs just after taking rcu_read_lock. This will
also prevent a possible misuse in the future, if someone adjusts this
function.

Reported-by: syzbot+7ed9d47e15e88581dc5b@syzkaller.appspotmail.com
Fixes: e952837f3ddb ("xfrm: state: fix out-of-bounds read during lookup")
Signed-off-by: Sabrina Dubroca <sd@queasysnail.net>
Reviewed-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Steffen Klassert <steffen.klassert@secunet.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
net/xfrm/xfrm_state.c

index 7a298058fc16cf7096984ead4f14c7c87fe394dd..912a4161a742076da065b7170f965acfaeece133 100644 (file)
@@ -1324,6 +1324,8 @@ xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr,
        sequence = read_seqcount_begin(&net->xfrm.xfrm_state_hash_generation);
 
        rcu_read_lock();
+       xfrm_hash_ptrs_get(net, &state_ptrs);
+
        hlist_for_each_entry_rcu(x, &pol->state_cache_list, state_cache) {
                if (x->props.family == encap_family &&
                    x->props.reqid == tmpl->reqid &&
@@ -1364,8 +1366,6 @@ cached:
        else if (acquire_in_progress) /* XXX: acquire_in_progress should not happen */
                WARN_ON(1);
 
-       xfrm_hash_ptrs_get(net, &state_ptrs);
-
        h = __xfrm_dst_hash(daddr, saddr, tmpl->reqid, encap_family, state_ptrs.hmask);
        hlist_for_each_entry_rcu(x, state_ptrs.bydst + h, bydst) {
 #ifdef CONFIG_XFRM_OFFLOAD