Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp1154802imm; Wed, 1 Aug 2018 11:05:23 -0700 (PDT) X-Google-Smtp-Source: AAOMgpf1Uv4dkDCsglqS/Fn0B6MrNA+WDCshImKFaay7j6+A24fjvwufN425FKqUho1wZsLsoxFt X-Received: by 2002:a62:cd3:: with SMTP id 80-v6mr28240538pfm.184.1533146723615; Wed, 01 Aug 2018 11:05:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1533146723; cv=none; d=google.com; s=arc-20160816; b=RVrXHaQ+pcxoNjFQnhPc+PQGSarbNoQwkq29IgGk8iTIOinxRAJHwmwzR4TmmgiEs0 ErCYQjbDpWqEeCaf2x5JnrRp8mdzfwLTAQVe9127CXBgxjjHeetr77bMBls+M1ZGe9fn eEc/liA2JHAP+LByKTvhSegguai7ALb0Xj4JABsjMyZBsyt1uajnl0/2xy94l71p/kDm RfJf/D7sKsDNFXoJ0W7BVDtc6mj7I/lgM/mFi5uPzroJD/CNS7jLkrgfaHCnax32NWOk b7Wz4eFCAlxxoxKPe2KbsSb8QjhLhH/tG7zSSW9opu72+lCwpviSsUGmRGrYBMk53jqv hU4Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=cxNs4bKIaIvcVsmNMn2qQqQehDZK0FQG1nAcZmvRC6Y=; b=A+Hk/HDDtT2f9AenosH532VE2G9g0N0gyyf1GUUFPFhoI6noouAAvElUGUENfEPrFR LRX4jmh/THvPzMvycXDgA72jgn+o8WEQEZXS3EGPl+7C4aSTBm5tvemtRMxiBIE14ZE0 ucm8InJNevzHvRMHfNQ/kFCLedq3WgmfaKpTfRE7Gv7IBqPXHsiJtAgUNPs4AaoKsV/o s0QadZb26t/RC18ai3pKP8h2q2b27f/dxPpV1XxSS91LRC72GKO+024dMFza2i+a5nqh YC4m4OcoBya5LboHhUfBViXuSh+tEOZF4BgKH/+y6IATbLXpSKe/7vn8vabKWppHVkuM U4TA== 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 a24-v6si14672460pls.353.2018.08.01.11.05.05; Wed, 01 Aug 2018 11:05:23 -0700 (PDT) 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 S2405865AbeHATIh (ORCPT + 99 others); Wed, 1 Aug 2018 15:08:37 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:49926 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2405721AbeHATIf (ORCPT ); Wed, 1 Aug 2018 15:08:35 -0400 Received: from localhost (D57E6652.static.ziggozakelijk.nl [213.126.102.82]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id C7EAC1333; Wed, 1 Aug 2018 17:12:07 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, "Steven Rostedt (VMware)" , Snild Dolkow Subject: [PATCH 4.14 015/246] kthread, tracing: Dont expose half-written comm when creating kthreads Date: Wed, 1 Aug 2018 18:48:45 +0200 Message-Id: <20180801165012.438897787@linuxfoundation.org> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180801165011.700991984@linuxfoundation.org> References: <20180801165011.700991984@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.14-stable 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 Cc: stable@vger.kernel.org Fixes: bc0c38d139ec7 ("ftrace: latency tracer infrastructure") Reviewed-by: Steven Rostedt (VMware) Signed-off-by: Snild Dolkow Signed-off-by: Steven Rostedt (VMware) Signed-off-by: Greg Kroah-Hartman --- kernel/kthread.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) --- a/kernel/kthread.c +++ b/kernel/kthread.c @@ -311,8 +311,14 @@ struct task_struct *__kthread_create_on_ 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); + /* + * task is already visible to other tasks, so updating + * COMM must be protected. + */ + 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.