Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3806124imu; Mon, 12 Nov 2018 00:20:48 -0800 (PST) X-Google-Smtp-Source: AJdET5fTPNerUABYTYFWSOxwaYgPu18bRq3e9f79eXncarTYBTm3DXtrDINPtN/OQ8foCih3ST7y X-Received: by 2002:a63:4a4a:: with SMTP id j10-v6mr30283pgl.0.1542010848821; Mon, 12 Nov 2018 00:20:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1542010848; cv=none; d=google.com; s=arc-20160816; b=YastGYYj4besNKmz9xd/JTGpuCRIiY006swdea5AcdhhrlgrvDvc/Kxe1sqb9II5k/ eRvGXHxfaaGio41s2wA01YNb4GJ4IASX56Lu/gLU9wNaYWQqtlEBsSY1z2TcGKXsYtHi P2GfgOSXMnV6u0gkI78zKw+oWoScfPF4BBENTatS9+ifFQ3VRW4m5hjYVIMer1QvnbJt iLvj0vTisVddIbSE6BQITj8tDD01vW7a7Z01SMpRrCvwMBsYcZP+zT0mjw9IkjeSQRrw Lht0Oyz7idNiOlCaPkglRjJu73S+TeYv/TmFE5bbzHEQirWIY6PTif2XXkz6HfDGLxnw jQNg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject; bh=c9EWzoK+lpZEvLNQoH2xHftdGpnduH1PYfEACWlej4Q=; b=uuH4lux2p4L5yK1hoNXtzLc9HK/qKyUdAX5aslkWIG56vR9fMqXJwhbOydQItcUaMt 9cXp3RTypmpBuLDZEHzMcTjC7Eh4FMkfPr7NoauVcX2JzT4/VceXfZJOZ/8LnPgiePkM f/3xlVDo8Uz3UTA7qafPSVcbWNkaVjlVGGWiGnkSjMPPLR+YxK+orj9koluf9qqojVl6 OYske2f821UdQccjPQ0jA8DiX71QXpuFWYp/h55AaRTylMdWaqGQp5486Jy/K5C+Iuag vD/3MoDdMQ3gD8HG388oILKxIoSSwGFoWibgOI9mGqOsKaYhAfvdS7oX621gevz4ya7V 1hdQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d3si3340599pgu.437.2018.11.12.00.20.33; Mon, 12 Nov 2018 00:20:48 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728529AbeKLSMR (ORCPT + 99 others); Mon, 12 Nov 2018 13:12:17 -0500 Received: from seldsegrel01.sonyericsson.com ([37.139.156.29]:4437 "EHLO SELDSEGREL01.sonyericsson.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727296AbeKLSMR (ORCPT ); Mon, 12 Nov 2018 13:12:17 -0500 X-Greylist: delayed 602 seconds by postgrey-1.27 at vger.kernel.org; Mon, 12 Nov 2018 13:12:15 EST Subject: Re: [PATCH 3.16 298/366] kthread, tracing: Don't expose half-written comm when creating kthreads To: Ben Hutchings , , CC: , "Steven Rostedt (VMware)" References: From: Snild Dolkow Message-ID: <17edd54d-a979-b66d-1b30-1d1f9b1142a3@sony.com> Date: Mon, 12 Nov 2018 09:10:07 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.2.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset="utf-8" Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 11/11/18 8:49 PM, Ben Hutchings wrote: > 3.16.61-rc1 review patch. If anyone has any objections, please let me know. > > ------------------ > > From: Snild Dolkow > > commit 3e536e222f2930534c252c1cc7ae799c725c5ff9 upstream. > > There is a window for racing when printing directly to task->comm, > allowing other threads to see a non-terminated string. The vsnprintf > function fills the buffer, counts the truncated chars, then finally > writes the \0 at the end. > > creator other > vsnprintf: > fill (not terminated) > count the rest trace_sched_waking(p): > ... memcpy(comm, p->comm, TASK_COMM_LEN) > write \0 > > The consequences depend on how 'other' uses the string. In our case, > it was copied into the tracing system's saved cmdlines, a buffer of > adjacent TASK_COMM_LEN-byte buffers (note the 'n' where 0 should be): > > crash-arm64> x/1024s savedcmd->saved_cmdlines | grep 'evenk' > 0xffffffd5b3818640: "irq/497-pwr_evenkworker/u16:12" > > ...and a strcpy out of there would cause stack corruption: > > [224761.522292] Kernel panic - not syncing: stack-protector: > Kernel stack is corrupted in: ffffff9bf9783c78 > > crash-arm64> kbt | grep 'comm\|trace_print_context' > #6 0xffffff9bf9783c78 in trace_print_context+0x18c(+396) > comm (char [16]) = "irq/497-pwr_even" > > crash-arm64> rd 0xffffffd4d0e17d14 8 > ffffffd4d0e17d14: 2f71726900000000 5f7277702d373934 ....irq/497-pwr_ > ffffffd4d0e17d24: 726f776b6e657665 3a3631752f72656b evenkworker/u16: > ffffffd4d0e17d34: f9780248ff003231 cede60e0ffffff9b 12..H.x......`.. > ffffffd4d0e17d44: cede60c8ffffffd4 00000fffffffffd4 .....`.......... > > The workaround in e09e28671 (use strlcpy in __trace_find_cmdline) was > likely needed because of this same bug. > > Solved by vsnprintf:ing to a local buffer, then using set_task_comm(). > This way, there won't be a window where comm is not terminated. > > Link: http://lkml.kernel.org/r/20180726071539.188015-1-snild@sony.com > > Fixes: bc0c38d139ec7 ("ftrace: latency tracer infrastructure") > Reviewed-by: Steven Rostedt (VMware) > Signed-off-by: Snild Dolkow > Signed-off-by: Steven Rostedt (VMware) > [bwh: Backported to 3.16: adjust context] > Signed-off-by: Ben Hutchings > --- > kernel/kthread.c | 8 +++++++- > 1 file changed, 7 insertions(+), 1 deletion(-) > > --- a/kernel/kthread.c > +++ b/kernel/kthread.c > @@ -309,10 +309,16 @@ struct task_struct *kthread_create_on_no > if (!IS_ERR(task)) { > static const struct sched_param param = { .sched_priority = 0 }; > va_list args; > + char name[TASK_COMM_LEN]; > > + /* > + * task is already visible to other tasks, so updating > + * COMM must be protected. > + */ > va_start(args, namefmt); > - vsnprintf(task->comm, sizeof(task->comm), namefmt, args); > + vsnprintf(name, sizeof(name), namefmt, args); > va_end(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. > Reviewed-by: Snild Dolkow