Skip to contentSkip to conversation

wifi: wil6210: Replace strlcat() usage with seq_buf

RFC PATCH In review Kees Cook 3 participants 3 messages Oct 26, 2023 – Oct 31, 2023

Subsystems (2)
lore
Share & tools
lore

Kees Cook

author
#0 lore raw

RFC PATCH 1/1 wifi: wil6210: Replace strlcat() usage with seq_buf

The use of strlcat() is fragile at best, and we'd like to remove it from the available string APIs in the kernel. Instead, use the safer seq_buf APIs.

Cc Kalle Valo <[email protected]>
Cc Johannes Berg <[email protected]>
Cc Max Chen <[email protected]>
Cc Yang Shen <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc "Matthew Wilcox (Oracle)" <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Justin Stitt <[email protected]>
Cc Kent Overstreet <[email protected]>
Cc Petr Mladek <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc Rasmus Villemoes <[email protected]>
Cc Sergey Senozhatsky <[email protected]>
Cc Masami Hiramatsu <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc Yun Zhou <[email protected]>
Cc Jacob Keller <[email protected]>
Cc Zhen Lei <[email protected]>

---
This is mainly an example of where/how to use the ongoing seq_buf refactoring happening in the tracing tree:
https://lore.kernel.org/lkml/[email protected]/

---
 drivers/net/wireless/ath/wil6210/wmi.c | 23 ++++++++++-------------
 1 file changed, 10 insertions(+), 13 deletions(-)
drivers/net/wireless/ath/wil6210/wmi.c +10 -13
index 6fdb77d4c59e..45b8c651b8e2 100644
@@ -3159,36 +3159,34 @@ int wmi_suspend(struct wil6210_priv *wil)
 	return rc;
 }
 
-static void resume_triggers2string(u32 triggers, char *string, int str_size)
+static void resume_triggers2string(u32 triggers, struct seq_buf *s)
 {
-	string[0] = '\0';
-
 	if (!triggers) {
-		strlcat(string, " UNKNOWN", str_size);
+		seq_buf_puts(s, " UNKNOWN");
 		return;
 	}
 
 	if (triggers & WMI_RESUME_TRIGGER_HOST)
-		strlcat(string, " HOST", str_size);
+		seq_buf_puts(s, " HOST")
 
 	if (triggers & WMI_RESUME_TRIGGER_UCAST_RX)
-		strlcat(string, " UCAST_RX", str_size);
+		seq_buf_puts(s, " UCAST_RX");
 
 	if (triggers & WMI_RESUME_TRIGGER_BCAST_RX)
-		strlcat(string, " BCAST_RX", str_size);
+		seq_buf_puts(s, " BCAST_RX");
 
 	if (triggers & WMI_RESUME_TRIGGER_WMI_EVT)
-		strlcat(string, " WMI_EVT", str_size);
+		seq_buf_puts(s, " WMI_EVT");
 
 	if (triggers & WMI_RESUME_TRIGGER_DISCONNECT)
-		strlcat(string, " DISCONNECT", str_size);
+		seq_buf_puts(s, " DISCONNECT");
 }
 
 int wmi_resume(struct wil6210_priv *wil)
 {
 	struct wil6210_vif *vif = ndev_to_vif(wil->main_ndev);
 	int rc;
-	char string[100];
+	DECLARE_SEQ_BUF(s, 100);
 	struct {
 		struct wmi_cmd_hdr wmi;
 		struct wmi_traffic_resume_event evt;
@@ -3203,10 +3201,9 @@ int wmi_resume(struct wil6210_priv *wil)
 		      WIL_WAIT_FOR_SUSPEND_RESUME_COMP);
 	if (rc)
 		return rc;
-	resume_triggers2string(le32_to_cpu(reply.evt.resume_triggers), string,
-			       sizeof(string));
+	resume_triggers2string(le32_to_cpu(reply.evt.resume_triggers), s);
 	wil_dbg_pm(wil, "device resume %s, resume triggers:%s (0x%x)\n",
-		   reply.evt.status ? "failed" : "passed", string,
+		   reply.evt.status ? "failed" : "passed", seq_buf_cstr(s),
 		   le32_to_cpu(reply.evt.resume_triggers));
 
 	return reply.evt.status;
Signature · 1 line
2.34.1
#1 lore raw
Quoted 28 lines · Kees Cook

The use of strlcat() is fragile at best, and we'd like to remove it from the available string APIs in the kernel. Instead, use the safer seq_buf APIs.

Cc Kalle Valo <[email protected]>
Cc Johannes Berg <[email protected]>
Cc Max Chen <[email protected]>
Cc Yang Shen <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc "Matthew Wilcox (Oracle)" <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Justin Stitt <[email protected]>
Cc Kent Overstreet <[email protected]>
Cc Petr Mladek <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc Rasmus Villemoes <[email protected]>
Cc Sergey Senozhatsky <[email protected]>
Cc Masami Hiramatsu <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc Yun Zhou <[email protected]>
Cc Jacob Keller <[email protected]>
Cc Zhen Lei <[email protected]>
Quoted 4 lines

---
This is mainly an example of where/how to use the ongoing seq_buf refactoring happening in the tracing tree:
https://lore.kernel.org/lkml/[email protected]/

I like it. C-strings and many of their associated apis are dodgy. This looks like a worthwhile replacement.

I think many of my strncpy -> strscpy replacements could've easily been something along these lines as well.

Happy to see robustness increasing in the kernel by means of replacing sketchy C-string stuff.

Quoted 67 lines
---
 drivers/net/wireless/ath/wil6210/wmi.c | 23 ++++++++++-------------
 1 file changed, 10 insertions(+), 13 deletions(-)
drivers/net/wireless/ath/wil6210/wmi.c +10 -14
index 6fdb77d4c59e..45b8c651b8e2 100644
@@ -3159,36 +3159,34 @@ int wmi_suspend(struct wil6210_priv *wil)
        return rc;
 }
 
-static void resume_triggers2string(u32 triggers, char *string, int str_size)
+static void resume_triggers2string(u32 triggers, struct seq_buf *s)
 {
-       string[0] = '\0';
-
        if (!triggers) {
-               strlcat(string, " UNKNOWN", str_size);
+               seq_buf_puts(s, " UNKNOWN");
                return;
        }
 
        if (triggers & WMI_RESUME_TRIGGER_HOST)
-               strlcat(string, " HOST", str_size);
+               seq_buf_puts(s, " HOST")
 
        if (triggers & WMI_RESUME_TRIGGER_UCAST_RX)
-               strlcat(string, " UCAST_RX", str_size);
+               seq_buf_puts(s, " UCAST_RX");
 
        if (triggers & WMI_RESUME_TRIGGER_BCAST_RX)
-               strlcat(string, " BCAST_RX", str_size);
+               seq_buf_puts(s, " BCAST_RX");
 
        if (triggers & WMI_RESUME_TRIGGER_WMI_EVT)
-               strlcat(string, " WMI_EVT", str_size);
+               seq_buf_puts(s, " WMI_EVT");
 
        if (triggers & WMI_RESUME_TRIGGER_DISCONNECT)
-               strlcat(string, " DISCONNECT", str_size);
+               seq_buf_puts(s, " DISCONNECT");
 }
 
 int wmi_resume(struct wil6210_priv *wil)
 {
        struct wil6210_vif *vif = ndev_to_vif(wil->main_ndev);
        int rc;
-       char string[100];
+       DECLARE_SEQ_BUF(s, 100);
        struct {
                struct wmi_cmd_hdr wmi;
                struct wmi_traffic_resume_event evt;
@@ -3203,10 +3201,9 @@ int wmi_resume(struct wil6210_priv *wil)
                      WIL_WAIT_FOR_SUSPEND_RESUME_COMP);
        if (rc)
                return rc;
-       resume_triggers2string(le32_to_cpu(reply.evt.resume_triggers), string,
-                              sizeof(string));
+       resume_triggers2string(le32_to_cpu(reply.evt.resume_triggers), s);
        wil_dbg_pm(wil, "device resume %s, resume triggers:%s (0x%x)\n",
-                  reply.evt.status ? "failed" : "passed", string,
+                  reply.evt.status ? "failed" : "passed", seq_buf_cstr(s),
                   le32_to_cpu(reply.evt.resume_triggers));
 
        return reply.evt.status;
--
Quoted 2 lines

2.34.1

Thanks
Justin

#2 lore raw
In reply to Kees Cook
kernel test robot noticed the following build errors:

Hi Kees,

[This is a private test report for your RFC patch.] kernel test robot noticed the following build errors:

[auto build test ERROR on wireless-next/main]
[also build test ERROR on wireless/main kvalo-ath/ath-next linus/master v6.6 next-20231030] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Kees-Cook/wifi-wil6210-Replace-strlcat-usage-with-seq_buf/20231027-011746
base:   https://git.kernel.org/pub/scm/linux/kernel/git/wireless/wireless-next.git main
patch link:    https://lore.kernel.org/r/20231026171349.work.928-kees%40kernel.org
patch subject: [RFC][PATCH] wifi: wil6210: Replace strlcat() usage with seq_buf
config: arm64-allmodconfig (https://download.01.org/0day-ci/archive/20231031/[email protected]/config)
compiler: clang version 17.0.0 (https://github.com/llvm/llvm-project.git 4a5ac14ee968ff0ad5d2cc1ffa0299048db4c88a)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231031/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All error/warnings (new ones prefixed by >>):

>> drivers/net/wireless/ath/wil6210/wmi.c:3162:57: warning: declaration of 'struct seq_buf' will not be visible outside of this function [-Wvisibility]
    3162 | static void resume_triggers2string(u32 triggers, struct seq_buf *s)
         |                                                         ^
>> drivers/net/wireless/ath/wil6210/wmi.c:3165:3: error: call to undeclared function 'seq_buf_puts'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
    3165 |                 seq_buf_puts(s, " UNKNOWN");
         |                 ^
   drivers/net/wireless/ath/wil6210/wmi.c:3165:3: note: did you mean 'seq_puts'?
   include/linux/seq_file.h:121:6: note: 'seq_puts' declared here
     121 | void seq_puts(struct seq_file *m, const char *s);
         |      ^
   drivers/net/wireless/ath/wil6210/wmi.c:3170:3: error: call to undeclared function 'seq_buf_puts'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
    3170 |                 seq_buf_puts(s, " HOST")
         |                 ^
>> drivers/net/wireless/ath/wil6210/wmi.c:3170:27: error: expected ';' after expression
    3170 |                 seq_buf_puts(s, " HOST")
         |                                         ^
         |                                         ;
>> drivers/net/wireless/ath/wil6210/wmi.c:3189:2: error: call to undeclared function 'DECLARE_SEQ_BUF'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
    3189 |         DECLARE_SEQ_BUF(s, 100);
         |         ^
>> drivers/net/wireless/ath/wil6210/wmi.c:3189:18: error: use of undeclared identifier 's'
    3189 |         DECLARE_SEQ_BUF(s, 100);
         |                         ^
   drivers/net/wireless/ath/wil6210/wmi.c:3204:65: error: use of undeclared identifier 's'
    3204 |         resume_triggers2string(le32_to_cpu(reply.evt.resume_triggers), s);
         |                                                                        ^
>> drivers/net/wireless/ath/wil6210/wmi.c:3206:46: error: call to undeclared function 'seq_buf_cstr'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
    3206 |                    reply.evt.status ? "failed" : "passed", seq_buf_cstr(s),
         |                                                            ^
   drivers/net/wireless/ath/wil6210/wmi.c:3206:59: error: use of undeclared identifier 's'
    3206 |                    reply.evt.status ? "failed" : "passed", seq_buf_cstr(s),
         |                                                                         ^
>> drivers/net/wireless/ath/wil6210/wmi.c:3206:46: error: call to undeclared function 'seq_buf_cstr'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]

3206 | reply.evt.status ? "failed" : "passed", seq_buf_cstr(s),
| ^
drivers/net/wireless/ath/wil6210/wmi.c:3206:59: error: use of undeclared identifier 's'
3206 | reply.evt.status ? "failed" : "passed", seq_buf_cstr(s),
| ^
1 warning and 10 errors generated.

vim +/seq_buf_puts +3165 drivers/net/wireless/ath/wil6210/wmi.c

3161

> 3162	static void resume_triggers2string(u32 triggers, struct seq_buf *s)
  3163	{
  3164		if (!triggers) {
> 3165			seq_buf_puts(s, " UNKNOWN");
  3166			return;
  3167		}
  3168	
  3169		if (triggers & WMI_RESUME_TRIGGER_HOST)
> 3170			seq_buf_puts(s, " HOST")
  3171	
  3172		if (triggers & WMI_RESUME_TRIGGER_UCAST_RX)
  3173			seq_buf_puts(s, " UCAST_RX");
  3174	
  3175		if (triggers & WMI_RESUME_TRIGGER_BCAST_RX)
  3176			seq_buf_puts(s, " BCAST_RX");
  3177	
  3178		if (triggers & WMI_RESUME_TRIGGER_WMI_EVT)
  3179			seq_buf_puts(s, " WMI_EVT");
  3180	
  3181		if (triggers & WMI_RESUME_TRIGGER_DISCONNECT)
  3182			seq_buf_puts(s, " DISCONNECT");
  3183	}
  3184	
  3185	int wmi_resume(struct wil6210_priv *wil)
  3186	{
  3187		struct wil6210_vif *vif = ndev_to_vif(wil->main_ndev);
  3188		int rc;
> 3189		DECLARE_SEQ_BUF(s, 100);
  3190		struct {
  3191			struct wmi_cmd_hdr wmi;
  3192			struct wmi_traffic_resume_event evt;
  3193		} __packed reply = {
  3194			.evt = {.status = WMI_TRAFFIC_RESUME_FAILED,
  3195				.resume_triggers =
  3196					cpu_to_le32(WMI_RESUME_TRIGGER_UNKNOWN)},
  3197		};
  3198	
  3199		rc = wmi_call(wil, WMI_TRAFFIC_RESUME_CMDID, vif->mid, NULL, 0,
  3200			      WMI_TRAFFIC_RESUME_EVENTID, &reply, sizeof(reply),
  3201			      WIL_WAIT_FOR_SUSPEND_RESUME_COMP);
  3202		if (rc)
  3203			return rc;
  3204		resume_triggers2string(le32_to_cpu(reply.evt.resume_triggers), s);
  3205		wil_dbg_pm(wil, "device resume %s, resume triggers:%s (0x%x)\n",
> 3206			   reply.evt.status ? "failed" : "passed", seq_buf_cstr(s),

3207 le32_to_cpu(reply.evt.resume_triggers));
3208
3209 return reply.evt.status;
3210 }
3211

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki