Skip to contentSkip to conversation

tracing: memcpy->strncpy changes for comm strings

PATCH New Tom Zanussi 1 participant 3 messages Mar 5, 2019 Tracing

lore
Share & tools
lore
#0 lore raw

cover tracing: memcpy->strncpy changes for comm strings

From: Tom Zanussi <[email protected]>

Hi Steve,

Here are a couple patches as discussed, replacing memcpy with strncpy for comm strings in trace.c and trace_events_hist.c.

Thanks,

Tom

The following changes since commit 9f0bbf3115ca9f91f43b7c74e9ac7d79f47fc6c2:

tracing: Use strncpy instead of memcpy for string keys in hist triggers (2019-03-05 08:47:46 -0500)

are available in the git repository at:

git://git.kernel.org/pub/scm/linux/kernel/git/zanussi/linux-trace.git ftrace/comm-strncpy

Tom Zanussi (2):
tracing: Use strncpy instead of memcpy when copying comm for hist
triggers
tracing: Use strncpy instead of memcpy when copying comm in trace.c

 kernel/trace/trace.c             | 4 ++--
 kernel/trace/trace_events_hist.c | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)
Signature · 1 line
2.14.1
#1 lore raw

PATCH 1/2 tracing: Use strncpy instead of memcpy when copying comm for hist triggers

From: Tom Zanussi <[email protected]>

Because there may be random garbage beyond a string's null terminator, code that might use the entire comm array e.g. histogram keys, can give unexpected results if that garbage is copied in too, so avoid that possibility by using strncpy instead of memcpy.

---
 kernel/trace/trace_events_hist.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/trace/trace_events_hist.c +2 -2
index c7774fa119a7..ca46339f3009 100644
@@ -2141,7 +2141,7 @@ static inline void save_comm(char *comm, struct task_struct *task)
 		return;
 	}
 
-	memcpy(comm, task->comm, TASK_COMM_LEN);
+	strncpy(comm, task->comm, TASK_COMM_LEN);
 }
 
 static void hist_elt_data_free(struct hist_elt_data *elt_data)
@@ -3557,7 +3557,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)
-		memcpy(track_elt_data->comm, elt_data->comm, TASK_COMM_LEN);
+		strncpy(track_elt_data->comm, elt_data->comm, TASK_COMM_LEN);
 
 	track_data->updated = true;
 
Signature · 1 line
2.14.1
#2 lore raw

PATCH 2/2 tracing: Use strncpy instead of memcpy when copying comm in trace.c

In reply to Tom Zanussi

From: Tom Zanussi <[email protected]>

Because there may be random garbage beyond a string's null terminator, code that might use the entire comm array e.g. histogram keys, can give unexpected results if that garbage is copied in too, so avoid that possibility by using strncpy instead of memcpy.

---
 kernel/trace/trace.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/trace/trace.c +2 -2
index 3835f7ed3293..e9cc47e59d25 100644
@@ -1497,7 +1497,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;
 
-	memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
+	strncpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
 	max_data->pid = tsk->pid;
 	/*
 	 * If tsk == current, then use current_uid(), as that does not use
@@ -1923,7 +1923,7 @@ static inline char *get_saved_cmdlines(int idx)
 
 static inline void set_cmdline(int idx, const char *cmdline)
 {
-	memcpy(get_saved_cmdlines(idx), cmdline, TASK_COMM_LEN);
+	strncpy(get_saved_cmdlines(idx), cmdline, TASK_COMM_LEN);
 }
 
 static int allocate_cmdlines_buffer(unsigned int val,
Signature · 1 line
2.14.1