Linux Hardening
 help / color / mirror / Atom feed
* [PATCH v3 0/3] Add str_alloc_free() helper
@ 2026-05-07 11:03 Jiazi Li
  2026-05-07 11:03 ` [PATCH v3 1/3] lib/string_choices: " Jiazi Li
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Jiazi Li @ 2026-05-07 11:03 UTC (permalink / raw)
  To: Kees Cook, Andy Shevchenko, Ping-Ke Shih, Julia Lawall,
	Nicolas Palix
  Cc: Jiazi Li, linux-hardening, linux-wireless

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)

Patch 3 is based on
https://lore.kernel.org/linux-hardening/CAHp75VdHY814NRfyUfWeqRd4-DwYzHGh25OTsUB5EFpRKpVQLQ@mail.gmail.com/T/#t.

---
Changes in v3: Add first user for this helper

v2: https://lore.kernel.org/linux-hardening/CAHp75Vdvdr0TPzuDDPSoJ4Nixk-6qO_x6Or9iY9LNwSO1Q6mHg@mail.gmail.com/T/#mfb405519f7b3a06f4fcaa8e5a799da5299d9ee85

Jiazi Li (3):
  lib/string_choices: Add str_alloc_free() helper
  wifi: rtw89: Make use of str_alloc_free helper
  coccinelle: Add rules to find str_alloc_free() replacements

 drivers/net/wireless/realtek/rtw89/fw.c     |  4 +--
 include/linux/string_choices.h              |  6 ++++
 scripts/coccinelle/api/string_choices.cocci | 38 +++++++++++++++++++++
 3 files changed, 46 insertions(+), 2 deletions(-)

-- 
2.49.0


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

* [PATCH v3 1/3] lib/string_choices: Add str_alloc_free() helper
  2026-05-07 11:03 [PATCH v3 0/3] Add str_alloc_free() helper Jiazi Li
@ 2026-05-07 11:03 ` Jiazi Li
  2026-05-08  5:18   ` Greg KH
  2026-05-07 11:03 ` [PATCH v3 2/3] wifi: rtw89: Make use of str_alloc_free helper Jiazi Li
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 6+ messages in thread
From: Jiazi Li @ 2026-05-07 11:03 UTC (permalink / raw)
  To: Kees Cook, Andy Shevchenko, Ping-Ke Shih, Julia Lawall,
	Nicolas Palix
  Cc: Jiazi Li, linux-hardening, linux-wireless, 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().

Tested-by: mingzhu.wang <mingzhu.wang@transsion.com>
Signed-off-by: Jiazi Li <jqqlijiazi@gmail.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..65f346db95ba 100644
--- a/include/linux/string_choices.h
+++ b/include/linux/string_choices.h
@@ -17,6 +17,12 @@
 
 #include <linux/types.h>
 
+static inline const char *str_alloc_free(bool v)
+{
+	return v ? "alloc" : "free";
+}
+#define str_free_alloc(v)		str_alloc_free(!(v))
+
 static inline const char *str_assert_deassert(bool v)
 {
 	return v ? "assert" : "deassert";
-- 
2.49.0


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

* [PATCH v3 2/3] wifi: rtw89: Make use of str_alloc_free helper
  2026-05-07 11:03 [PATCH v3 0/3] Add str_alloc_free() helper Jiazi Li
  2026-05-07 11:03 ` [PATCH v3 1/3] lib/string_choices: " Jiazi Li
@ 2026-05-07 11:03 ` Jiazi Li
  2026-05-07 11:04 ` [PATCH v3 3/3] coccinelle: Add rules to find str_alloc_free() replacements Jiazi Li
  2026-05-07 11:10 ` [PATCH v3 0/3] Add str_alloc_free() helper Johannes Berg
  3 siblings, 0 replies; 6+ messages in thread
From: Jiazi Li @ 2026-05-07 11:03 UTC (permalink / raw)
  To: Kees Cook, Andy Shevchenko, Ping-Ke Shih, Julia Lawall,
	Nicolas Palix
  Cc: Jiazi Li, linux-hardening, linux-wireless, mingzhu.wang

The helper str_alloc_free is introduced to return "alloc/free"
string literal. We can simplify this format by str_alloc_free.

Tested-by: mingzhu.wang <mingzhu.wang@transsion.com>
Signed-off-by: Jiazi Li <jqqlijiazi@gmail.com>
---
 drivers/net/wireless/realtek/rtw89/fw.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/net/wireless/realtek/rtw89/fw.c b/drivers/net/wireless/realtek/rtw89/fw.c
index 17704f054727..6cc68494b3a1 100644
--- a/drivers/net/wireless/realtek/rtw89/fw.c
+++ b/drivers/net/wireless/realtek/rtw89/fw.c
@@ -2554,7 +2554,7 @@ int rtw89_fw_h2c_ba_cam(struct rtw89_dev *rtwdev,
 		 */
 		rtw89_debug(rtwdev, RTW89_DBG_TXRX,
 			    "failed to %s entry tid=%d for h2c ba cam\n",
-			    valid ? "alloc" : "free", params->tid);
+			    str_alloc_free(valid), params->tid);
 		return 0;
 	}
 
@@ -2690,7 +2690,7 @@ int rtw89_fw_h2c_ba_cam_v1(struct rtw89_dev *rtwdev,
 		 */
 		rtw89_debug(rtwdev, RTW89_DBG_TXRX,
 			    "failed to %s entry tid=%d for h2c ba cam\n",
-			    valid ? "alloc" : "free", params->tid);
+			    str_alloc_free(valid), params->tid);
 		return 0;
 	}
 
-- 
2.49.0


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

* [PATCH v3 3/3] coccinelle: Add rules to find str_alloc_free() replacements
  2026-05-07 11:03 [PATCH v3 0/3] Add str_alloc_free() helper Jiazi Li
  2026-05-07 11:03 ` [PATCH v3 1/3] lib/string_choices: " Jiazi Li
  2026-05-07 11:03 ` [PATCH v3 2/3] wifi: rtw89: Make use of str_alloc_free helper Jiazi Li
@ 2026-05-07 11:04 ` Jiazi Li
  2026-05-07 11:10 ` [PATCH v3 0/3] Add str_alloc_free() helper Johannes Berg
  3 siblings, 0 replies; 6+ messages in thread
From: Jiazi Li @ 2026-05-07 11:04 UTC (permalink / raw)
  To: Kees Cook, Andy Shevchenko, Ping-Ke Shih, Julia Lawall,
	Nicolas Palix
  Cc: Jiazi Li, linux-hardening, linux-wireless

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

Tested-by mingzhu.wang <mingzhu.wang@transsion.com>
Signed-off-by: Jiazi Li <jqqlijiazi@gmail.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 7b2b76a42acc..516b30828380 100644
--- a/scripts/coccinelle/api/string_choices.cocci
+++ b/scripts/coccinelle/api/string_choices.cocci
@@ -7,6 +7,44 @@ virtual patch
 virtual context
 virtual report
 
+@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)
+
 @str_enable_disable depends on patch@
 expression E;
 @@
-- 
2.49.0


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

* Re: [PATCH v3 0/3] Add str_alloc_free() helper
  2026-05-07 11:03 [PATCH v3 0/3] Add str_alloc_free() helper Jiazi Li
                   ` (2 preceding siblings ...)
  2026-05-07 11:04 ` [PATCH v3 3/3] coccinelle: Add rules to find str_alloc_free() replacements Jiazi Li
@ 2026-05-07 11:10 ` Johannes Berg
  3 siblings, 0 replies; 6+ messages in thread
From: Johannes Berg @ 2026-05-07 11:10 UTC (permalink / raw)
  To: Jiazi Li, Kees Cook, Andy Shevchenko, Ping-Ke Shih, Julia Lawall,
	Nicolas Palix
  Cc: linux-hardening, linux-wireless

On Thu, 2026-05-07 at 19:03 +0800, Jiazi Li wrote:
> 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)

It's what, three characters shorter for four users each? Is there really
much point?

Reading just the name also is really confusing - yes it lines up with
str_assert_deassert, but "alloc_free" really doesn't seem very obvious -
why would you alloc and free a string in one function call?

johannes

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

* Re: [PATCH v3 1/3] lib/string_choices: Add str_alloc_free() helper
  2026-05-07 11:03 ` [PATCH v3 1/3] lib/string_choices: " Jiazi Li
@ 2026-05-08  5:18   ` Greg KH
  0 siblings, 0 replies; 6+ messages in thread
From: Greg KH @ 2026-05-08  5:18 UTC (permalink / raw)
  To: Jiazi Li
  Cc: Kees Cook, Andy Shevchenko, Ping-Ke Shih, Julia Lawall,
	Nicolas Palix, linux-hardening, linux-wireless, mingzhu.wang

On Thu, May 07, 2026 at 07:03:22PM +0800, Jiazi Li 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().
> 
> Tested-by: mingzhu.wang <mingzhu.wang@transsion.com>

Needs a name, not an email alias.

thanks,

greg k-h

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

end of thread, other threads:[~2026-05-08  5:18 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-05-07 11:03 [PATCH v3 0/3] Add str_alloc_free() helper Jiazi Li
2026-05-07 11:03 ` [PATCH v3 1/3] lib/string_choices: " Jiazi Li
2026-05-08  5:18   ` Greg KH
2026-05-07 11:03 ` [PATCH v3 2/3] wifi: rtw89: Make use of str_alloc_free helper Jiazi Li
2026-05-07 11:04 ` [PATCH v3 3/3] coccinelle: Add rules to find str_alloc_free() replacements Jiazi Li
2026-05-07 11:10 ` [PATCH v3 0/3] Add str_alloc_free() helper Johannes Berg

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