lkml.org 
[lkml]   [2021]   [Oct]   [25]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
From
Date
SubjectRe: [PATCH v6 12/12] kernel/kthread: show a warning if kthread's comm is truncated
On Tue, Oct 26, 2021 at 5:35 AM Kees Cook <keescook@chromium.org> wrote:
>
> On Mon, Oct 25, 2021 at 08:33:15AM +0000, Yafang Shao wrote:
> > Show a warning if task comm is truncated. Below is the result
> > of my test case:
> >
> > truncated kthread comm:I-am-a-kthread-with-lon, pid:14 by 6 characters
> >
> > Suggested-by: Petr Mladek <pmladek@suse.com>
> > Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
> > Reviewed-by: Kees Cook <keescook@chromium.org>
> > Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
> > Cc: Arnaldo Carvalho de Melo <arnaldo.melo@gmail.com>
> > Cc: Andrii Nakryiko <andrii.nakryiko@gmail.com>
> > Cc: Peter Zijlstra <peterz@infradead.org>
> > Cc: Steven Rostedt <rostedt@goodmis.org>
> > Cc: Al Viro <viro@zeniv.linux.org.uk>
> > Cc: Kees Cook <keescook@chromium.org>
> > Cc: Petr Mladek <pmladek@suse.com>
> > ---
> > kernel/kthread.c | 7 ++++++-
> > 1 file changed, 6 insertions(+), 1 deletion(-)
> >
> > diff --git a/kernel/kthread.c b/kernel/kthread.c
> > index 5b37a8567168..46b924c92078 100644
> > --- a/kernel/kthread.c
> > +++ b/kernel/kthread.c
> > @@ -399,12 +399,17 @@ struct task_struct *__kthread_create_on_node(int (*threadfn)(void *data),
> > if (!IS_ERR(task)) {
> > static const struct sched_param param = { .sched_priority = 0 };
> > char name[TASK_COMM_LEN];
> > + int len;
> >
> > /*
> > * task is already visible to other tasks, so updating
> > * COMM must be protected.
> > */
> > - vsnprintf(name, sizeof(name), namefmt, args);
> > + len = vsnprintf(name, sizeof(name), namefmt, args);
> > + if (len >= TASK_COMM_LEN) {
>
> And since this failure case is slow-path, we could improve the warning
> as other had kind of suggested earlier with something like this instead:
>

It Makes sense to me. I will do it as you suggested.

> char *full_comm;
>
> full_comm = kvasprintf(GFP_KERNEL, namefmt, args);
> pr_warn("truncated kthread comm '%s' to '%s' (pid:%d)\n",
> full_comm, name);
>
> kfree(full_comm);
> }
> > set_task_comm(task, name);
> > /*
> > * root may have changed our (kthreadd's) priority or CPU mask.
> > --
> > 2.17.1
> >
>
> --
> Kees Cook



--
Thanks
Yafang

\
 
 \ /
  Last update: 2021-10-26 04:24    [W:0.086 / U:0.588 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site