All of lore.kernel.org
 help / color / mirror / Atom feed
* [RFC][PATCH] wifi: wil6210: Replace strlcat() usage with seq_buf
@ 2023-10-26 17:13 Kees Cook
  2023-10-26 22:03 ` Justin Stitt
  2023-10-31  3:50 ` kernel test robot
  0 siblings, 2 replies; 3+ messages in thread
From: Kees Cook @ 2023-10-26 17:13 UTC (permalink / raw)
  To: Kalle Valo
  Cc: Kees Cook, Johannes Berg, Max Chen, Yang Shen, Steven Rostedt,
	Matthew Wilcox (Oracle), Christoph Hellwig, Justin Stitt,
	Kent Overstreet, Petr Mladek, Andy Shevchenko, Rasmus Villemoes,
	Sergey Senozhatsky, Masami Hiramatsu, Greg Kroah-Hartman,
	Arnd Bergmann, Jonathan Corbet, Yun Zhou, Jacob Keller, Zhen Lei,
	linux-trace-kernel, linux-wireless, linux-kernel, linux-hardening

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 <kvalo@kernel.org>
Cc: Johannes Berg <johannes.berg@intel.com>
Cc: Max Chen <mxchen@codeaurora.org>
Cc: Yang Shen <shenyang39@huawei.com>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: "Matthew Wilcox (Oracle)" <willy@infradead.org>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Justin Stitt <justinstitt@google.com>
Cc: Kent Overstreet <kent.overstreet@linux.dev>
Cc: Petr Mladek <pmladek@suse.com>
Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Cc: Sergey Senozhatsky <senozhatsky@chromium.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Yun Zhou <yun.zhou@windriver.com>
Cc: Jacob Keller <jacob.e.keller@intel.com>
Cc: Zhen Lei <thunder.leizhen@huawei.com>
Cc: linux-trace-kernel@vger.kernel.org
Cc: linux-wireless@vger.kernel.org
Signed-off-by: Kees Cook <keescook@chromium.org>
---
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/20231026170722.work.638-kees@kernel.org/
---
 drivers/net/wireless/ath/wil6210/wmi.c | 23 ++++++++++-------------
 1 file changed, 10 insertions(+), 13 deletions(-)

diff --git a/drivers/net/wireless/ath/wil6210/wmi.c b/drivers/net/wireless/ath/wil6210/wmi.c
index 6fdb77d4c59e..45b8c651b8e2 100644
--- a/drivers/net/wireless/ath/wil6210/wmi.c
+++ b/drivers/net/wireless/ath/wil6210/wmi.c
@@ -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;
-- 
2.34.1


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

* Re: [RFC][PATCH] wifi: wil6210: Replace strlcat() usage with seq_buf
  2023-10-26 17:13 [RFC][PATCH] wifi: wil6210: Replace strlcat() usage with seq_buf Kees Cook
@ 2023-10-26 22:03 ` Justin Stitt
  2023-10-31  3:50 ` kernel test robot
  1 sibling, 0 replies; 3+ messages in thread
From: Justin Stitt @ 2023-10-26 22:03 UTC (permalink / raw)
  To: Kees Cook
  Cc: Kalle Valo, Johannes Berg, Max Chen, Yang Shen, Steven Rostedt,
	Matthew Wilcox (Oracle), Christoph Hellwig, Kent Overstreet,
	Petr Mladek, Andy Shevchenko, Rasmus Villemoes,
	Sergey Senozhatsky, Masami Hiramatsu, Greg Kroah-Hartman,
	Arnd Bergmann, Jonathan Corbet, Yun Zhou, Jacob Keller, Zhen Lei,
	linux-trace-kernel, linux-wireless, linux-kernel, linux-hardening

On Thu, Oct 26, 2023 at 10:13 AM Kees Cook <keescook@chromium.org> wrote:
>
> 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 <kvalo@kernel.org>
> Cc: Johannes Berg <johannes.berg@intel.com>
> Cc: Max Chen <mxchen@codeaurora.org>
> Cc: Yang Shen <shenyang39@huawei.com>
> Cc: Steven Rostedt <rostedt@goodmis.org>
> Cc: "Matthew Wilcox (Oracle)" <willy@infradead.org>
> Cc: Christoph Hellwig <hch@lst.de>
> Cc: Justin Stitt <justinstitt@google.com>
> Cc: Kent Overstreet <kent.overstreet@linux.dev>
> Cc: Petr Mladek <pmladek@suse.com>
> Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk>
> Cc: Sergey Senozhatsky <senozhatsky@chromium.org>
> Cc: Masami Hiramatsu <mhiramat@kernel.org>
> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> Cc: Arnd Bergmann <arnd@arndb.de>
> Cc: Jonathan Corbet <corbet@lwn.net>
> Cc: Yun Zhou <yun.zhou@windriver.com>
> Cc: Jacob Keller <jacob.e.keller@intel.com>
> Cc: Zhen Lei <thunder.leizhen@huawei.com>
> Cc: linux-trace-kernel@vger.kernel.org
> Cc: linux-wireless@vger.kernel.org
> Signed-off-by: Kees Cook <keescook@chromium.org>
> ---
> 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/20231026170722.work.638-kees@kernel.org/

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.

> ---
>  drivers/net/wireless/ath/wil6210/wmi.c | 23 ++++++++++-------------
>  1 file changed, 10 insertions(+), 13 deletions(-)
>
> diff --git a/drivers/net/wireless/ath/wil6210/wmi.c b/drivers/net/wireless/ath/wil6210/wmi.c
> index 6fdb77d4c59e..45b8c651b8e2 100644
> --- a/drivers/net/wireless/ath/wil6210/wmi.c
> +++ b/drivers/net/wireless/ath/wil6210/wmi.c
> @@ -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;
> --
> 2.34.1
>

Thanks
Justin

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

* Re: [RFC][PATCH] wifi: wil6210: Replace strlcat() usage with seq_buf
  2023-10-26 17:13 [RFC][PATCH] wifi: wil6210: Replace strlcat() usage with seq_buf Kees Cook
  2023-10-26 22:03 ` Justin Stitt
@ 2023-10-31  3:50 ` kernel test robot
  1 sibling, 0 replies; 3+ messages in thread
From: kernel test robot @ 2023-10-31  3:50 UTC (permalink / raw)
  To: Kees Cook; +Cc: llvm, oe-kbuild-all

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/202310311131.HryHNtzn-lkp@intel.com/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/202310311131.HryHNtzn-lkp@intel.com/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 <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202310311131.HryHNtzn-lkp@intel.com/

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	

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

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

end of thread, other threads:[~2023-10-31  3:51 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-10-26 17:13 [RFC][PATCH] wifi: wil6210: Replace strlcat() usage with seq_buf Kees Cook
2023-10-26 22:03 ` Justin Stitt
2023-10-31  3:50 ` kernel test robot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.