Linux Hardening
 help / color / mirror / Atom feed
* [PATCH 0/2] Add str_alloc_free()/str_free_alloc helper and related rules
@ 2026-04-29 11:22 Jiazi Li
  2026-04-29 11:23 ` [PATCH 1/2] lib/string_choices: Add str_alloc_free() helper Jiazi Li
  2026-04-29 11:23 ` [PATCH 2/2] coccinelle: Add rules to find str_alloc_free() replacements Jiazi Li
  0 siblings, 2 replies; 5+ messages in thread
From: Jiazi Li @ 2026-04-29 11:22 UTC (permalink / raw)
  To: Kees Cook, Andy Shevchenko, Julia Lawall, Nicolas Palix
  Cc: Jiazi Li, linux-hardening

Currently finds 4 locations:
./drivers/net/wireless/realtek/rtw89/fw.c:2557:7-12: opportunity for str_alloc_free(valid)
./drivers/net/wireless/realtek/rtw89/fw.c:2693:7-12: opportunity for str_alloc_free(valid)
./drivers/android/tests/binder_alloc_kunit.c:196:6-21: opportunity for str_alloc_free(alloc -> pages [ i ])
./mm/slub.c:1634:3-8: opportunity for str_alloc_free(alloc)


Jiazi Li (2):
  lib/string_choices: Add str_alloc_free() helper
  coccinelle: Add rules to find str_alloc_free() replacements

 include/linux/string_choices.h              |  6 ++++
 scripts/coccinelle/api/string_choices.cocci | 38 +++++++++++++++++++++
 2 files changed, 44 insertions(+)

-- 
2.49.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 1/2] lib/string_choices: Add str_alloc_free() helper
  2026-04-29 11:22 [PATCH 0/2] Add str_alloc_free()/str_free_alloc helper and related rules Jiazi Li
@ 2026-04-29 11:23 ` Jiazi Li
  2026-04-29 13:26   ` Andy Shevchenko
  2026-04-29 11:23 ` [PATCH 2/2] coccinelle: Add rules to find str_alloc_free() replacements Jiazi Li
  1 sibling, 1 reply; 5+ messages in thread
From: Jiazi Li @ 2026-04-29 11:23 UTC (permalink / raw)
  To: Kees Cook, Andy Shevchenko, Julia Lawall, Nicolas Palix
  Cc: Jiazi Li, linux-hardening, mingzhu.wang

Add str_alloc_free() helper to return "alloc" or "free"
string literal depending on the boolean argument. Also add the
inversed variant str_free_alloc().

Signed-off-by: Jiazi Li <jqqlijiazi@gmail.com>
Tested-by: mingzhu.wang <mingzhu.wang@transsion.com>
---
 include/linux/string_choices.h | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/include/linux/string_choices.h b/include/linux/string_choices.h
index ee84087d4b26..9150ac47f70b 100644
--- a/include/linux/string_choices.h
+++ b/include/linux/string_choices.h
@@ -83,6 +83,12 @@ static inline const char *str_yes_no(bool v)
 }
 #define str_no_yes(v)		str_yes_no(!(v))
 
+static inline const char *str_alloc_free(bool v)
+{
+	return v ? "alloc" : "free";
+}
+#define str_free_alloc(v)		str_alloc_free(!(v))
+
 /**
  * str_plural - Return the simple pluralization based on English counts
  * @num: Number used for deciding pluralization
-- 
2.49.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 2/2] coccinelle: Add rules to find str_alloc_free() replacements
  2026-04-29 11:22 [PATCH 0/2] Add str_alloc_free()/str_free_alloc helper and related rules Jiazi Li
  2026-04-29 11:23 ` [PATCH 1/2] lib/string_choices: Add str_alloc_free() helper Jiazi Li
@ 2026-04-29 11:23 ` Jiazi Li
  2026-04-29 13:28   ` Andy Shevchenko
  1 sibling, 1 reply; 5+ messages in thread
From: Jiazi Li @ 2026-04-29 11:23 UTC (permalink / raw)
  To: Kees Cook, Andy Shevchenko, Julia Lawall, Nicolas Palix
  Cc: Jiazi Li, linux-hardening, mingzhu.wang

Add rules for finding places where str_alloc_free()/str_free_alloc
can be used.

Signed-off-by: Jiazi Li <jqqlijiazi@gmail.com>
Tested-by: mingzhu.wang <mingzhu.wang@transsion.com>
---
 scripts/coccinelle/api/string_choices.cocci | 38 +++++++++++++++++++++
 1 file changed, 38 insertions(+)

diff --git a/scripts/coccinelle/api/string_choices.cocci b/scripts/coccinelle/api/string_choices.cocci
index 375045086912..5e5f0518b90e 100644
--- a/scripts/coccinelle/api/string_choices.cocci
+++ b/scripts/coccinelle/api/string_choices.cocci
@@ -300,3 +300,41 @@ e << str_yes_no_r.E;
 @@
 
 coccilib.report.print_report(p[0], "opportunity for str_yes_no(%s)" % e)
+
+@str_alloc_free depends on patch disable neg_if_exp@
+expression E;
+@@
+-      ((E) ? "alloc" : "free")
++      str_alloc_free(E)
+
+@str_alloc_free_r depends on !patch disable neg_if_exp@
+expression E;
+position P;
+@@
+*      E@P ? "alloc" : "free"
+
+@script:python depends on report@
+p << str_alloc_free_r.P;
+e << str_alloc_free_r.E;
+@@
+
+coccilib.report.print_report(p[0], "opportunity for str_alloc_free(%s)" % e)
+
+@str_free_alloc depends on patch disable neg_if_exp@
+expression E;
+@@
+-      ((E) ? "free" : "alloc")
++      str_free_alloc(E)
+
+@str_free_alloc_r depends on !patch disable neg_if_exp@
+expression E;
+position P;
+@@
+*      E@P ? "free" : "alloc"
+
+@script:python depends on report@
+p << str_free_alloc_r.P;
+e << str_free_alloc_r.E;
+@@
+
+coccilib.report.print_report(p[0], "opportunity for str_free_alloc(%s)" % e)
-- 
2.49.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/2] lib/string_choices: Add str_alloc_free() helper
  2026-04-29 11:23 ` [PATCH 1/2] lib/string_choices: Add str_alloc_free() helper Jiazi Li
@ 2026-04-29 13:26   ` Andy Shevchenko
  0 siblings, 0 replies; 5+ messages in thread
From: Andy Shevchenko @ 2026-04-29 13:26 UTC (permalink / raw)
  To: Jiazi Li
  Cc: Kees Cook, Andy Shevchenko, Julia Lawall, Nicolas Palix,
	linux-hardening, mingzhu.wang

On Wed, Apr 29, 2026 at 2:24 PM Jiazi Li <jqqlijiazi@gmail.com> wrote:
>
> Add str_alloc_free() helper to return "alloc" or "free"
> string literal depending on the boolean argument. Also add the
> inversed variant str_free_alloc().

...

>  #define str_no_yes(v)          str_yes_no(!(v))
>
> +static inline const char *str_alloc_free(bool v)
> +{
> +       return v ? "alloc" : "free";
> +}
> +#define str_free_alloc(v)              str_alloc_free(!(v))

The appearance of the function is sorted alphabetically.

-- 
With Best Regards,
Andy Shevchenko

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 2/2] coccinelle: Add rules to find str_alloc_free() replacements
  2026-04-29 11:23 ` [PATCH 2/2] coccinelle: Add rules to find str_alloc_free() replacements Jiazi Li
@ 2026-04-29 13:28   ` Andy Shevchenko
  0 siblings, 0 replies; 5+ messages in thread
From: Andy Shevchenko @ 2026-04-29 13:28 UTC (permalink / raw)
  To: Jiazi Li
  Cc: Kees Cook, Andy Shevchenko, Julia Lawall, Nicolas Palix,
	linux-hardening, mingzhu.wang

On Wed, Apr 29, 2026 at 2:24 PM Jiazi Li <jqqlijiazi@gmail.com> wrote:
>
> Add rules for finding places where str_alloc_free()/str_free_alloc

str_free_alloc()

> can be used.

...

>  coccilib.report.print_report(p[0], "opportunity for str_yes_no(%s)" % e)

> +coccilib.report.print_report(p[0], "opportunity for str_alloc_free(%s)" % e)

Perhaps also makes sense to keep coccinelle stances sorted in the same
order as they appear in the header.

-- 
With Best Regards,
Andy Shevchenko

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-04-29 13:29 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-29 11:22 [PATCH 0/2] Add str_alloc_free()/str_free_alloc helper and related rules Jiazi Li
2026-04-29 11:23 ` [PATCH 1/2] lib/string_choices: Add str_alloc_free() helper Jiazi Li
2026-04-29 13:26   ` Andy Shevchenko
2026-04-29 11:23 ` [PATCH 2/2] coccinelle: Add rules to find str_alloc_free() replacements Jiazi Li
2026-04-29 13:28   ` Andy Shevchenko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox