diff mbox series

tracing: Replace strncpy() with strscpy() when copying comm

Message ID 20240731075058.617588-1-ruanjinjie@huawei.com (mailing list archive)
State Superseded
Delegated to: Steven Rostedt
Headers show
Series tracing: Replace strncpy() with strscpy() when copying comm | expand

Commit Message

Jinjie Ruan July 31, 2024, 7:50 a.m. UTC
Replace the depreciated[1] strncpy() calls with strscpy()
when copying comm.

Link: https://github.com/KSPP/linux/issues/90 [1]
Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
---
 kernel/trace/trace.c              | 2 +-
 kernel/trace/trace_events_hist.c  | 4 ++--
 kernel/trace/trace_sched_switch.c | 2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)

Comments

Justin Stitt Aug. 27, 2024, 6:22 p.m. UTC | #1
Hi,

On Wed, Jul 31, 2024 at 03:50:58PM GMT, Jinjie Ruan wrote:
> Replace the depreciated[1] strncpy() calls with strscpy()
> when copying comm.
> 
> Link: https://github.com/KSPP/linux/issues/90 [1]
> Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>

Reviewed-by: Justin Stitt <justinstitt@google.com>

> ---
>  kernel/trace/trace.c              | 2 +-
>  kernel/trace/trace_events_hist.c  | 4 ++--
>  kernel/trace/trace_sched_switch.c | 2 +-
>  3 files changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
> index d0af984a5337..73cfdc704eec 100644
> --- a/kernel/trace/trace.c
> +++ b/kernel/trace/trace.c
> @@ -1907,7 +1907,7 @@ __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
>  	max_data->critical_start = data->critical_start;
>  	max_data->critical_end = data->critical_end;
>  
> -	strncpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
> +	strscpy(max_data->comm, tsk->comm);
>  	max_data->pid = tsk->pid;
>  	/*
>  	 * If tsk == current, then use current_uid(), as that does not use
> diff --git a/kernel/trace/trace_events_hist.c b/kernel/trace/trace_events_hist.c
> index 6ece1308d36a..4ee0e64719fa 100644
> --- a/kernel/trace/trace_events_hist.c
> +++ b/kernel/trace/trace_events_hist.c
> @@ -1599,7 +1599,7 @@ static inline void save_comm(char *comm, struct task_struct *task)
>  		return;
>  	}
>  
> -	strncpy(comm, task->comm, TASK_COMM_LEN);
> +	strscpy(comm, task->comm);
>  }
>  
>  static void hist_elt_data_free(struct hist_elt_data *elt_data)
> @@ -3405,7 +3405,7 @@ static bool cond_snapshot_update(struct trace_array *tr, void *cond_data)
>  	elt_data = context->elt->private_data;
>  	track_elt_data = track_data->elt.private_data;
>  	if (elt_data->comm)
> -		strncpy(track_elt_data->comm, elt_data->comm, TASK_COMM_LEN);
> +		strscpy(track_elt_data->comm, elt_data->comm);
>  
>  	track_data->updated = true;
>  
> diff --git a/kernel/trace/trace_sched_switch.c b/kernel/trace/trace_sched_switch.c
> index 8a407adb0e1c..573b5d8e8a28 100644
> --- a/kernel/trace/trace_sched_switch.c
> +++ b/kernel/trace/trace_sched_switch.c
> @@ -187,7 +187,7 @@ static inline char *get_saved_cmdlines(int idx)
>  
>  static inline void set_cmdline(int idx, const char *cmdline)
>  {
> -	strncpy(get_saved_cmdlines(idx), cmdline, TASK_COMM_LEN);
> +	strscpy(get_saved_cmdlines(idx), cmdline, TASK_COMM_LEN);
>  }
>  
>  static void free_saved_cmdlines_buffer(struct saved_cmdlines_buffer *s)
> -- 
> 2.34.1
> 

Thanks
Justin
Steven Rostedt Oct. 30, 2024, 11:40 p.m. UTC | #2
On Wed, 31 Jul 2024 15:50:58 +0800
Jinjie Ruan <ruanjinjie@huawei.com> wrote:

> diff --git a/kernel/trace/trace_events_hist.c b/kernel/trace/trace_events_hist.c
> index 6ece1308d36a..4ee0e64719fa 100644
> --- a/kernel/trace/trace_events_hist.c
> +++ b/kernel/trace/trace_events_hist.c
> @@ -1599,7 +1599,7 @@ static inline void save_comm(char *comm, struct task_struct *task)
>  		return;
>  	}
>  
> -	strncpy(comm, task->comm, TASK_COMM_LEN);
> +	strscpy(comm, task->comm);
>  }
>  

Was this even compiled?

In file included from /work/git/test-linux.git/include/linux/container_of.h:5,
                 from /work/git/test-linux.git/include/linux/list.h:5,
                 from /work/git/test-linux.git/include/linux/module.h:12,
                 from /work/git/test-linux.git/kernel/trace/trace_events_hist.c:8:
/work/git/test-linux.git/kernel/trace/trace_events_hist.c: In function ‘save_comm’:
/work/git/test-linux.git/include/linux/build_bug.h:16:51: error: negative width in bit-field ‘<anonymous>’
   16 | #define BUILD_BUG_ON_ZERO(e) ((int)(sizeof(struct { int:(-!!(e)); })))
      |                                                   ^
/work/git/test-linux.git/include/linux/compiler.h:243:33: note: in expansion of macro ‘BUILD_BUG_ON_ZERO’
  243 | #define __must_be_array(a)      BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
      |                                 ^~~~~~~~~~~~~~~~~
/work/git/test-linux.git/include/linux/string.h:79:47: note: in expansion of macro ‘__must_be_array’
   79 |         sized_strscpy(dst, src, sizeof(dst) + __must_be_array(dst) +    \
      |                                               ^~~~~~~~~~~~~~~
/work/git/test-linux.git/include/linux/args.h:25:24: note: in expansion of macro ‘__strscpy0’
   25 | #define __CONCAT(a, b) a ## b
      |                        ^
/work/git/test-linux.git/include/linux/args.h:26:27: note: in expansion of macro ‘__CONCAT’
   26 | #define CONCATENATE(a, b) __CONCAT(a, b)
      |                           ^~~~~~~~
/work/git/test-linux.git/include/linux/string.h:113:9: note: in expansion of macro ‘CONCATENATE’
  113 |         CONCATENATE(__strscpy, COUNT_ARGS(__VA_ARGS__))(dst, src, __VA_ARGS__)
      |         ^~~~~~~~~~~
/work/git/test-linux.git/kernel/trace/trace_events_hist.c:1602:9: note: in expansion of macro ‘strscpy’
 1602 |         strscpy(comm, task->comm);
      |         ^~~~~~~

Bah!

-- Steve
Jinjie Ruan Oct. 31, 2024, 1:47 a.m. UTC | #3
On 2024/10/31 7:40, Steven Rostedt wrote:
> On Wed, 31 Jul 2024 15:50:58 +0800
> Jinjie Ruan <ruanjinjie@huawei.com> wrote:
> 
>> diff --git a/kernel/trace/trace_events_hist.c b/kernel/trace/trace_events_hist.c
>> index 6ece1308d36a..4ee0e64719fa 100644
>> --- a/kernel/trace/trace_events_hist.c
>> +++ b/kernel/trace/trace_events_hist.c
>> @@ -1599,7 +1599,7 @@ static inline void save_comm(char *comm, struct task_struct *task)
>>  		return;
>>  	}
>>  
>> -	strncpy(comm, task->comm, TASK_COMM_LEN);
>> +	strscpy(comm, task->comm);
>>  }
>>  
> 
> Was this even compiled?

Sorry, the trace_events_hist.c was left out, will fix it.

> 
> In file included from /work/git/test-linux.git/include/linux/container_of.h:5,
>                  from /work/git/test-linux.git/include/linux/list.h:5,
>                  from /work/git/test-linux.git/include/linux/module.h:12,
>                  from /work/git/test-linux.git/kernel/trace/trace_events_hist.c:8:
> /work/git/test-linux.git/kernel/trace/trace_events_hist.c: In function ‘save_comm’:
> /work/git/test-linux.git/include/linux/build_bug.h:16:51: error: negative width in bit-field ‘<anonymous>’
>    16 | #define BUILD_BUG_ON_ZERO(e) ((int)(sizeof(struct { int:(-!!(e)); })))
>       |                                                   ^
> /work/git/test-linux.git/include/linux/compiler.h:243:33: note: in expansion of macro ‘BUILD_BUG_ON_ZERO’
>   243 | #define __must_be_array(a)      BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
>       |                                 ^~~~~~~~~~~~~~~~~
> /work/git/test-linux.git/include/linux/string.h:79:47: note: in expansion of macro ‘__must_be_array’
>    79 |         sized_strscpy(dst, src, sizeof(dst) + __must_be_array(dst) +    \
>       |                                               ^~~~~~~~~~~~~~~
> /work/git/test-linux.git/include/linux/args.h:25:24: note: in expansion of macro ‘__strscpy0’
>    25 | #define __CONCAT(a, b) a ## b
>       |                        ^
> /work/git/test-linux.git/include/linux/args.h:26:27: note: in expansion of macro ‘__CONCAT’
>    26 | #define CONCATENATE(a, b) __CONCAT(a, b)
>       |                           ^~~~~~~~
> /work/git/test-linux.git/include/linux/string.h:113:9: note: in expansion of macro ‘CONCATENATE’
>   113 |         CONCATENATE(__strscpy, COUNT_ARGS(__VA_ARGS__))(dst, src, __VA_ARGS__)
>       |         ^~~~~~~~~~~
> /work/git/test-linux.git/kernel/trace/trace_events_hist.c:1602:9: note: in expansion of macro ‘strscpy’
>  1602 |         strscpy(comm, task->comm);
>       |         ^~~~~~~
> 
> Bah!
> 
> -- Steve
>
diff mbox series

Patch

diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
index d0af984a5337..73cfdc704eec 100644
--- a/kernel/trace/trace.c
+++ b/kernel/trace/trace.c
@@ -1907,7 +1907,7 @@  __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
 	max_data->critical_start = data->critical_start;
 	max_data->critical_end = data->critical_end;
 
-	strncpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
+	strscpy(max_data->comm, tsk->comm);
 	max_data->pid = tsk->pid;
 	/*
 	 * If tsk == current, then use current_uid(), as that does not use
diff --git a/kernel/trace/trace_events_hist.c b/kernel/trace/trace_events_hist.c
index 6ece1308d36a..4ee0e64719fa 100644
--- a/kernel/trace/trace_events_hist.c
+++ b/kernel/trace/trace_events_hist.c
@@ -1599,7 +1599,7 @@  static inline void save_comm(char *comm, struct task_struct *task)
 		return;
 	}
 
-	strncpy(comm, task->comm, TASK_COMM_LEN);
+	strscpy(comm, task->comm);
 }
 
 static void hist_elt_data_free(struct hist_elt_data *elt_data)
@@ -3405,7 +3405,7 @@  static bool cond_snapshot_update(struct trace_array *tr, void *cond_data)
 	elt_data = context->elt->private_data;
 	track_elt_data = track_data->elt.private_data;
 	if (elt_data->comm)
-		strncpy(track_elt_data->comm, elt_data->comm, TASK_COMM_LEN);
+		strscpy(track_elt_data->comm, elt_data->comm);
 
 	track_data->updated = true;
 
diff --git a/kernel/trace/trace_sched_switch.c b/kernel/trace/trace_sched_switch.c
index 8a407adb0e1c..573b5d8e8a28 100644
--- a/kernel/trace/trace_sched_switch.c
+++ b/kernel/trace/trace_sched_switch.c
@@ -187,7 +187,7 @@  static inline char *get_saved_cmdlines(int idx)
 
 static inline void set_cmdline(int idx, const char *cmdline)
 {
-	strncpy(get_saved_cmdlines(idx), cmdline, TASK_COMM_LEN);
+	strscpy(get_saved_cmdlines(idx), cmdline, TASK_COMM_LEN);
 }
 
 static void free_saved_cmdlines_buffer(struct saved_cmdlines_buffer *s)