From: Ella Ma <alansnape3058@gmail.com>
To: julia.lawall@inria.fr
Cc: alansnape3058@gmail.com, cocci@inria.fr,
linux-kernel@vger.kernel.org, nicolas.palix@imag.fr
Subject: [PATCH v3] coccinelle: free: add a checker for `__cleanup(kfree)` usage
Date: Wed, 7 Oct 2026 18:43:16 +0200 [thread overview]
Message-ID: <20261007164316.15102-1-alansnape3058@gmail.com> (raw)
In-Reply-To: <ec578f57-a7f5-7afc-ec2-9156c336187@inria.fr>
Using __cleanup(kfree) will pass the stack address of the annotated
local variable to kfree functions. This will lead to invalid
deallocation issues. Inspired by CVE-2026-45959 and similar bugs
recently detected.
Signed-off-by: Ella Ma <alansnape3058@gmail.com>
---
Changes in v2:
- Add virtual rules `report` and `org` as suggested by Sashiko
- Remove `kvfree_sensitive` from rule `cleanup` as suggested by Sashiko
- Add `free_percpu` to rule `free`
- Add `kfree_const` to rule `cleanup`
Changes in v3:
- Edit the error messages to clarify the reasons and results
scripts/coccinelle/free/cleanup-free.cocci | 112 +++++++++++++++++++++
1 file changed, 112 insertions(+)
create mode 100644 scripts/coccinelle/free/cleanup-free.cocci
diff --git a/scripts/coccinelle/free/cleanup-free.cocci b/scripts/coccinelle/free/cleanup-free.cocci
new file mode 100644
index 000000000000..48e47ed2186a
--- /dev/null
+++ b/scripts/coccinelle/free/cleanup-free.cocci
@@ -0,0 +1,112 @@
+// SPDX-License-Identifier: GPL-2.0-only
+///
+/// Find __cleanup(kfree)
+/// Using __cleanup(kfree) will pass the stack address of the annotated
+/// local variable to kfree, causing an invalid free.
+/// I.e., `T v __cleanup(kfree);` --> `kfree(&v);`
+/// Such usage is impossible to be correct.
+///
+// Confidence: High
+// Copyright: (C) 2026 Ella Ma
+// Options: --no-includes --include-headers
+
+virtual report
+virtual org
+
+// Suggesting using __free for the functions with a DEFINE_FREE definition.
+// Update this list when new DEFINE_FREE definitions are added.
+@free@
+attribute name __cleanup;
+symbol kfree, kfree_sensitive, kvfree, kvfree_atomic, free_percpu;
+type T;
+identifier v, n;
+position p;
+@@
+
+(
+ T v __cleanup@p(
+(
+ n
+&
+(
+ kfree \| kfree_sensitive \| kvfree \| kvfree_atomic \| free_percpu
+)
+)
+ );
+|
+ T v __cleanup@p(
+(
+ n
+&
+(
+ kfree \| kfree_sensitive \| kvfree \| kvfree_atomic \| free_percpu
+)
+)
+ ) = ...;
+)
+
+@script:python depends on report@
+p << free.p;
+n << free.n;
+@@
+
+msg = f"ERROR: __cleanup({n}) will cause an invalid free: the stack address of the annotated variable will be passed to {n}; use __free({n}) to free the pointee"
+coccilib.report.print_report(p[0], msg)
+
+@script:python depends on org@
+p << free.p;
+n << free.n;
+@@
+
+msg = f"ERROR: __cleanup({n}) will cause an invalid free: the stack address of the annotated variable will be passed to {n}; use __free({n}) to free the pointee"
+coccilib.org.print_todo(p[0], msg)
+
+
+// Reporting __cleanup usage for the functions without a DEFINE_FREE definition.
+// The following list only contains frequently used functions. Supplement it if
+// necessary.
+@cleanup@
+attribute name __cleanup;
+symbol vfree, vfree_atomic, kfree_const;
+type T;
+identifier v, n;
+position p;
+@@
+
+(
+ T v __cleanup@p(
+(
+ n
+&
+(
+ vfree \| vfree_atomic \| kfree_const
+)
+)
+ );
+|
+ T v __cleanup@p(
+(
+ n
+&
+(
+ vfree \| vfree_atomic \| kfree_const
+)
+)
+ ) = ...;
+)
+
+@script:python depends on report@
+p << cleanup.p;
+n << cleanup.n;
+@@
+
+msg = f"ERROR: __cleanup({n}) will cause an invalid free: the stack address of the annotated variable will be passed to {n}"
+coccilib.report.print_report(p[0], msg)
+
+@script:python depends on org@
+p << cleanup.p;
+n << cleanup.n;
+@@
+
+msg = f"ERROR: __cleanup({n}) will cause an invalid free: the stack address of the annotated variable will be passed to {n}"
+coccilib.org.print_todo(p[0], msg)
--
2.34.1
next prev parent reply other threads:[~2026-10-07 16:43 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-31 13:43 [PATCH] " Ella Ma
2026-08-31 15:01 ` [cocci] " Markus Elfring
2026-09-24 16:25 ` [PATCH v2] " Ella Ma
2026-09-27 9:43 ` Julia Lawall
2026-10-07 16:43 ` Ella Ma [this message]
2026-10-07 18:27 ` [PATCH v3] " Julia Lawall
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=20261007164316.15102-1-alansnape3058@gmail.com \
--to=alansnape3058@gmail.com \
--cc=cocci@inria.fr \
--cc=julia.lawall@inria.fr \
--cc=linux-kernel@vger.kernel.org \
--cc=nicolas.palix@imag.fr \
/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
all inboxes | Powered by JetHome®