public inbox for netdev@vger.kernel.org
 help / color / mirror / Atom feed
From: qianjiaru77@gmail.com
To: steffen.klassert@secunet.com, herbert@gondor.apana.org.au,
	davem@davemloft.net, edumazet@google.com, kuba@kernel.org,
	pabeni@redhat.com
Cc: horms@kernel.org, netdev@vger.kernel.org,
	linux-kernel@vger.kernel.org, qianjiaru <qianjiaru77@gmail.com>
Subject: [PATCH 1/1] Reference Counting Vulnerability in Linux XFRM PolicyManagement
Date: Wed, 27 Aug 2025 00:25:00 +0800	[thread overview]
Message-ID: <20250826162500.34292-1-qianjiaru77@gmail.com> (raw)

From: qianjiaru <qianjiaru77@gmail.com>

A reference counting management vulnerability exists in the 
Linux kernel's XFRM (IPsec Transform) policy subsystem. 
Based on variant analysis of CVE-2022-36879, this 
vulnerability involves improper policy object lifecycle
management in the `__xfrm_policy_check()` function, 
potentially leading to double free conditions 
and system instability.

## Vulnerability Mechanism

The issue follows the same pattern as CVE-2022-36879 but in a different code path:

1. **Policy Reference Acquired**: 
`pols[0]` contains a valid policy with acquired reference
2. **Secondary Lookup Fails**: 
`xfrm_policy_lookup_bytype()` returns error for `pols[1]`
3. **Partial Cleanup**: 
Error path calls `xfrm_pol_put(pols[0])` but doesn't clear policy array state
4. **Caller Confusion**: 
Calling function may not be aware that `pols[0]` has already been released
5. **Double Release Risk**: 
If caller attempts cleanup based on incomplete state information

## Comparison with CVE-2022-36879

**CVE-2022-36879 (Fixed)**: 
`xfrm_expand_policies()` had double reference counting issues
```c
// Original vulnerability pattern (now fixed):
if (IS_ERR(pols[1])) {
    xfrm_pols_put(pols, *num_pols);  // Released references
    return PTR_ERR(pols[1]);         // But didn't set *num_pols = 0
}
```

**This Variant**: 
Similar reference management issues in different function
```c
// Current potential issue:
if (IS_ERR(pols[1])) {
    xfrm_pol_put(pols[0]);     // Releases pols[0]
    return 0;                  // But pols[0] pointer remains unchanged
}
```

## Attack Scenario

1. **Policy Lookup**: 
Network packet triggers `__xfrm_policy_check()` 
with sub-policy configuration
2. **Primary Policy Found**: 
`pols[0]` gets a valid policy reference
3. **Secondary Lookup Fails**: 
`xfrm_policy_lookup_bytype()` fails, returns error in `pols[1]`
4. **Partial Cleanup**: 
Function releases `pols[0]` reference but doesn't clear the pointer
5. **Caller Misunderstanding**: 
Calling function may attempt to use or release `pols[0]` again
6. **Memory Corruption**: 
Double free or use-after-free leads to system instability

## Proposed Fix

The vulnerability should be fixed by ensuring 
complete state cleanup in error paths:

```c
// Current potentially vulnerable code:
if (IS_ERR(pols[1])) {
    XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
    xfrm_pol_put(pols[0]);
    return 0;
}

// Proposed secure fix:
if (IS_ERR(pols[1])) {
    XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
    xfrm_pol_put(pols[0]);
    pols[0] = NULL;   // Clear pointer to prevent reuse
    return 0;
}
```

### Alternative Comprehensive Fix

If the calling context requires more extensive cleanup:

```c
if (IS_ERR(pols[1])) {
    XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
    xfrm_pol_put(pols[0]);
    memset(pols, 0, sizeof(pols));  // Clear entire policy array
    npols = 0;                      // Reset policy count
    return 0;
}
```

## References

- **Original CVE**: 
CVE-2022-36879 (xfrm_expand_policies double free)
- **Linux XFRM Documentation**: 
`Documentation/networking/xfrm_*.txt`
- **XFRM Source**: 
`net/xfrm/xfrm_policy.c`
- **IPsec RFCs**: 
RFC 4301, RFC 4306


Signed-off-by: qianjiaru <qianjiaru77@gmail.com>
---
 net/xfrm/xfrm_policy.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/net/xfrm/xfrm_policy.c b/net/xfrm/xfrm_policy.c
index c5035a9bc..50943fa4e 100644
--- a/net/xfrm/xfrm_policy.c
+++ b/net/xfrm/xfrm_policy.c
@@ -3786,6 +3786,7 @@ int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
 			if (IS_ERR(pols[1])) {
 				XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
 				xfrm_pol_put(pols[0]);
+				pols[0] = NULL;           // Clear pointer to prevent reuse
 				return 0;
 			}
 			/* This write can happen from different cpus. */
-- 
2.34.1


             reply	other threads:[~2025-08-26 16:25 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-26 16:25 qianjiaru77 [this message]
2025-08-26 16:49 ` [PATCH 1/1] Reference Counting Vulnerability in Linux XFRM PolicyManagement Florian Westphal

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=20250826162500.34292-1-qianjiaru77@gmail.com \
    --to=qianjiaru77@gmail.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=herbert@gondor.apana.org.au \
    --cc=horms@kernel.org \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --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