Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp2327390imm; Fri, 7 Sep 2018 14:44:18 -0700 (PDT) X-Google-Smtp-Source: ANB0VdaTvoAd9L3baP7toUJrbNjpes9SRIonqWjo0I0YjBe8cK+mgmXTQ0jV3fnBezLSpp+yzw0F X-Received: by 2002:aa7:8713:: with SMTP id b19-v6mr10761173pfo.151.1536356658714; Fri, 07 Sep 2018 14:44:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536356658; cv=none; d=google.com; s=arc-20160816; b=QK10IaVyc2SnzfTwvc5F4S9ZL0RoeWRs+Ee/Vd1QK7hf3rFew2Ts+XrDgbcbqI47aO mNEwGJ81/kcC29Dzvj8BK1lF+ndgQhB7ICi+y4i1D7cPhGBARToyBJe+Tiff5ZfBxf4s mwrr6TBBtr8HShxu5J9kXXRAPRCF8jqkrhw3A0fxZDmbflLzEIMEfj1incGjr+pGVqn5 tCYQwga8OYj3tVXLU8oG2nSk5d9eIsw+nkcGmdv1mYCbBRKpTVoVeKh+1E1sNePmg4fE UwT8JP3WnxRdCeLtk5HJa6L2mDGBVF8if5KCrBYLWrE8rwxIF7UUtCt5S6j638zQJnac boTQ== 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; bh=uDcLyixzdS9jz6mrBpDHjbxAfcu5WBdYYMyuM7/Z6iA=; b=LoiBDf4ExjuCxGTGGGVakOTY/R0Wi/qjUtq6umgI1OHsWI4iMip1l1PjxrylhtCkAN J5Rvj2488TxjBl8w5Q4/YsvKx3zHdeClj52/9h6uYYJEl03hEZkAuhK/yL5JgeuX1NYw 8U0t5+MsxY7FSbwerCexlkvAbmxqfXJLJJZ3597Msje2c2YkCtjppeH8cNaKPIOMPoqN XBWaSpQIbJDnHG1bMEPXXrF8TPWgEe5hUrGyKq5ezM2J/GZ6KIqKDDmPrTH/60IDh7X4 ft7gHCer+1z41sPVwUa/A8951FvYEuAjXDpCD4gf3HTTMNkMVfiR/gALB6qqsxMgaUbC gLfg== 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 k1-v6si9018374pgh.65.2018.09.07.14.44.03; Fri, 07 Sep 2018 14:44:18 -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 S1730779AbeIHCZ4 (ORCPT + 99 others); Fri, 7 Sep 2018 22:25:56 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:56814 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730356AbeIHCZ4 (ORCPT ); Fri, 7 Sep 2018 22:25:56 -0400 Received: from localhost (ip-213-127-74-90.ip.prioritytelecom.net [213.127.74.90]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 93FFDC9A; Fri, 7 Sep 2018 21:43:00 +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 3.18 12/29] kthread, tracing: Dont expose half-written comm when creating kthreads Date: Fri, 7 Sep 2018 23:10:33 +0200 Message-Id: <20180907210910.439637078@linuxfoundation.org> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180907210909.523240901@linuxfoundation.org> References: <20180907210909.523240901@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 3.18-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) [backported to 3.18 / 4.4 by Snild] 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 @@ -309,10 +309,16 @@ struct task_struct *kthread_create_on_no task = create->result; if (!IS_ERR(task)) { static const struct sched_param param = { .sched_priority = 0 }; + char name[TASK_COMM_LEN]; va_list args; va_start(args, namefmt); - 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); va_end(args); /* * root may have changed our (kthreadd's) priority or CPU mask.