Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp7092181imm; Tue, 24 Jul 2018 08:15:43 -0700 (PDT) X-Google-Smtp-Source: AAOMgpdqXpKaqEkwUcevNbamwUQZbHGpX/6jYTAuPjnOxp0O6ZcPd3Xe4cwXL907kHHFs6JqhVa4 X-Received: by 2002:a65:5c03:: with SMTP id u3-v6mr16009027pgr.402.1532445343046; Tue, 24 Jul 2018 08:15:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1532445343; cv=none; d=google.com; s=arc-20160816; b=GUVHlltJDsoSxoEgGGMe6kaR/GYk7CFn7Z8gSQFhzsNCwZTSmW4wgFadhrWMb1ELgD LNyuXgYr8yrk0PftF1OVYquO4+eqd3gnfBmmfrxv5RSqvck0vK3rsmeb++qrDG4dB4yq 0+HSjLCTuzDwzwiGuk5TKPw+SZj55sDpj7nF2FgRxp3GtemVV1IFTDNbba3EhYZHsSwD iT3JdLA8XbIRQ/Z/eKbuAskjCHaTelx7bwjbfQt8ipH5DOv6NLc30wi2P96bh99b7NQe k1bN6zdi+e2/SF+/m90Y+e9GS/VNmXfrDV8lBb+I+xOE7HXIcf9XyTO/5njUCb6osQXb lVww== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:date:subject:cc :to:from:arc-authentication-results; bh=WIwRw0Yo3R3uYU5pkJcUGlipV91lqqyewToGJMl3BpI=; b=xD8ZYlxFbqC+yz1s+huGB/CqPAL6iZz6mnC60oK9z84ofG+HpIHoBAZnuluKkzu5ZI eatOWUcRGNqFkFAwFnoWIwExJD9yKBcyj0ou6VN89O7Xs+JSTBenlH7yN+fKTwVVgneT uNnH1EVPr47ZxgrG3vU1wAN6iUU1ZS5AoJTxWXrDCg/jvHFdpwVAABNRJ9xI4Rc7bEKB 84RDRG4yw1dp9ievIo+i4kuCuSN7vyFK4uNKfwftMYiKB16JRGgx9LninEIKSNHTc/RM 2QUlM1ST12H5MTRQEhyJYN2gHm45VYdY+LgDBvEXymaQsWA8U0k3WmlgKsWGyfGgRuBm KRvQ== 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 w21-v6si11801405pgk.206.2018.07.24.08.15.27; Tue, 24 Jul 2018 08:15:43 -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 S2388400AbeGXQVC (ORCPT + 99 others); Tue, 24 Jul 2018 12:21:02 -0400 Received: from seldsegrel01.sonyericsson.com ([37.139.156.29]:13622 "EHLO SELDSEGREL01.sonyericsson.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388309AbeGXQVC (ORCPT ); Tue, 24 Jul 2018 12:21:02 -0400 From: Snild Dolkow To: Steven Rostedt , CC: Peter Enderborg , Yoshitaka Seto , Oleksiy Avramchenko , KOSAKI Motohiro , John Stultz , Ingo Molnar , Jens Axboe , Tejun Heo , Greg Kroah-Hartman , Linus Torvalds , Snild Dolkow Subject: [PATCH v2] kthread, tracing: Don't expose half-written comm when creating kthreads Date: Tue, 24 Jul 2018 17:12:13 +0200 Message-ID: <20180724151213.12511-1-snild@sony.com> X-Mailer: git-send-email 2.15.1 MIME-Version: 1.0 Content-Type: text/plain Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. Signed-off-by: Snild Dolkow --- kernel/kthread.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/kernel/kthread.c b/kernel/kthread.c index 481951bf091d..1a481ae12dec 100644 --- a/kernel/kthread.c +++ b/kernel/kthread.c @@ -319,8 +319,14 @@ 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); + /* + * 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. -- 2.15.1