Skip to contentSkip to conversation

tracepoint: don't make assumptions about length of string on task rename

PATCH In review Sasha Levin 2 participants 4 messages Aug 28, 2015 – Sep 1, 2015 Tracing

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tracepoint: don't make assumptions about length of string on task rename

While the dest comm string size is assured to be at least TASK_COMM_LEN long, doing a memcpy() also adds the assumption that the source is at least that long as well, which isn't assured, and isn't true in cases such as:

	set_task_comm(worker->task, "kworker/dying");

This leads to accessing invalid memory.

---
 include/trace/events/task.h |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/trace/events/task.h +1 -1
index dee3bb1..2cca6cd 100644
@@ -46,7 +46,7 @@ TRACE_EVENT(task_rename,
 	TP_fast_assign(
 		__entry->pid = task->pid;
 		memcpy(entry->oldcomm, task->comm, TASK_COMM_LEN);
-		memcpy(entry->newcomm, comm, TASK_COMM_LEN);
+		strlcpy(entry->newcomm, comm, TASK_COMM_LEN);
 		__entry->oom_score_adj = task->signal->oom_score_adj;
 	),
 
Signature · 1 line
1.7.10.4
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 9 lines · On Fri, 28 Aug 2015 07:06:58 -0400 Sasha Levin <[email protected]> wrote:

While the dest comm string size is assured to be at least TASK_COMM_LEN long, doing a memcpy() also adds the assumption that the source is at least that long as well, which isn't assured, and isn't true in cases such as:

	set_task_comm(worker->task, "kworker/dying");

This leads to accessing invalid memory.

Should this go to stable as well?

Also, as the memcpy was just faster than a strcpy, the static length was used. Perhaps we should convert that to a dynamic length string. But that should be a separate patch as this one fixes a possible bug, and the conversion to a dynamic string is just an enhancement.

-- Steve

Quoted 17 lines
---
 include/trace/events/task.h |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/trace/events/task.h +1 -1
index dee3bb1..2cca6cd 100644
@@ -46,7 +46,7 @@ TRACE_EVENT(task_rename,
 	TP_fast_assign(
 		__entry->pid = task->pid;
 		memcpy(entry->oldcomm, task->comm, TASK_COMM_LEN);
-		memcpy(entry->newcomm, comm, TASK_COMM_LEN);
+		strlcpy(entry->newcomm, comm, TASK_COMM_LEN);
 		__entry->oom_score_adj = task->signal->oom_score_adj;
 	),
 
#2 lore raw
Quoted 9 lines · Steven Rostedt
Quoted 6 lines · On Fri, 28 Aug 2015 07:06:58 -0400 Sasha Levin <[email protected]> wrote:

While the dest comm string size is assured to be at least TASK_COMM_LEN long, doing a memcpy() also adds the assumption that the source is at least that long as well, which isn't assured, and isn't true in cases such as:

	set_task_comm(worker->task, "kworker/dying");
Quoted 7 lines
Quoted 3 lines

This leads to accessing invalid memory.

Should this go to stable as well?

Yup.

Quoted 4 lines

Also, as the memcpy was just faster than a strcpy, the static length was used. Perhaps we should convert that to a dynamic length string. But that should be a separate patch as this one fixes a possible bug, and the conversion to a dynamic string is just an enhancement.

That'll slow things down for the common case, no?

Thanks,
Sasha

maintainer
#3 lore raw
Quoted 6 lines · On Tue, 01 Sep 2015 11:07:32 -0400 Sasha Levin <[email protected]> wrote:
Quoted 4 lines

Also, as the memcpy was just faster than a strcpy, the static length was used. Perhaps we should convert that to a dynamic length string. But that should be a separate patch as this one fixes a possible bug, and the conversion to a dynamic string is just an enhancement.

That'll slow things down for the common case, no?

Not really that much.

The comms should have been dynamic strings too, but the sched_switch tracepoint (the one others have been copied from) was created before we had dynamic strings for tracepoints.

-- Steve