linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH RESEND] kthread, tracing: Don't expose half-written comm when creating kthreads
@ 2018-07-23 13:42 Snild Dolkow
  2018-07-23 13:55 ` Steven Rostedt
  0 siblings, 1 reply; 9+ messages in thread
From: Snild Dolkow @ 2018-07-23 13:42 UTC (permalink / raw)
  To: linux-kernel, Ingo Molnar, Jens Axboe, Steven Rostedt, Tejun Heo,
	Greg Kroah-Hartman, Linus Torvalds
  Cc: Peter Enderborg, Yoshitaka Seto, Oleksiy Avramchenko,
	KOSAKI Motohiro, John Stultz, Snild Dolkow

There was a window for racing when task->comm was being written. The
vsnprintf function writes 16 bytes, then counts the rest, then null
terminates. In the meantime, other threads could see the non-terminated
comm value. In our case, it got into the trace system's saved cmdlines
and could cause stack corruption when strcpy'd out of there.

The workaround in e09e28671 (use strlcpy in __trace_find_cmdline) was
likely needed because of this bug.

Solved by vsnprintf:ing to a local buffer, then using set_task_comm().

Signed-off-by: Snild Dolkow <snild@sony.com>
---
 kernel/kthread.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/kernel/kthread.c b/kernel/kthread.c
index 481951bf091d..28874afbf747 100644
--- a/kernel/kthread.c
+++ b/kernel/kthread.c
@@ -319,8 +319,10 @@ struct task_struct *__kthread_create_on_node(int (*threadfn)(void *data),
 	task = create->result;
 	if (!IS_ERR(task)) {
 		static const struct sched_param param = { .sched_priority = 0 };
+		char name[TASK_COMM_LEN];
 
-		vsnprintf(task->comm, sizeof(task->comm), namefmt, args);
+		vsnprintf(name, sizeof(name), namefmt, args);
+		set_task_comm(task, name);
 		/*
 		 * root may have changed our (kthreadd's) priority or CPU mask.
 		 * The kernel thread should not inherit these properties.
-- 
2.15.1


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

end of thread, other threads:[~2018-07-24 15:02 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-07-23 13:42 [PATCH RESEND] kthread, tracing: Don't expose half-written comm when creating kthreads Snild Dolkow
2018-07-23 13:55 ` Steven Rostedt
2018-07-23 14:23   ` Snild Dolkow
2018-07-23 15:37     ` Steven Rostedt
2018-07-23 15:49       ` Snild Dolkow
2018-07-23 16:41         ` Steven Rostedt
2018-07-24  8:17           ` Snild Dolkow
2018-07-24 14:48             ` Steven Rostedt
2018-07-24 15:02               ` Snild Dolkow

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).