Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp131927imm; Thu, 26 Jul 2018 00:17:03 -0700 (PDT) X-Google-Smtp-Source: AAOMgpez9Cku2VfU/JBOqxvX2owc7Jk1qbgD7rSSouaN/8Z1/eswvRf4ZxUbj1XHpYNLk7DodPrJ X-Received: by 2002:a17:902:b48f:: with SMTP id y15-v6mr878844plr.44.1532589423637; Thu, 26 Jul 2018 00:17:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1532589423; cv=none; d=google.com; s=arc-20160816; b=iVGaAblSeW3mzxd0hCwKvYx2D59SEWvj7bK5vJ+lVePGJVVT1KcPv+R1IUvH7GxENY /JXvzirPcVJ9BA0KSHQBpCjb/xwew680yFpPHwFax+kmkTXpZqbg/+giSg33K+OgglPg nq8rQ7TDU4aIZuLLjrjapyBkTJ42aM0JbU6+0jyiqgxV4Q32dQdCKiXjefVpWoQYPoV9 SL4aZjQQEJ+tAgJVtoaBd3//SJbMUzONJCGV7axFb5it64sxnnFREmyO5SZtbC+GL1fr jgy4DQUeSbgQNM8WXqOOHqqnLcS6K2v9avb8SWPswJeTDu+vL2mDaUiK6g1dssSEqAnL iqFw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:arc-authentication-results; bh=CPEVgC4HLwGTiWgiESH9mIcNMQSOj7dCsdKRQCIM/dE=; b=L6OmCV7ydxnoRmhc4x7xqcKxL5hR3NHPplYzUVPMvyC2mFqMpNxmACd/G26PLAy1Py hMZ5hwpt2JTKL/Vk8Cf0hX2QlOMaQY3VUq19s2YsPMfL36fMIyOQckua9LjLN8wDnIyN dYbfD/Rjp0MYtBhE38WWAat2BoFEXOhS+DMPNAwhZJFvqu14DhOYPxFaqZtOaBIwPZBc Lj1mZjMKRaPsbeLeFRReOSF1GUAOqU0vrNBpK9oGu5wW4TB0tfRuJNZE4fZdTT1745o2 CwTUz5SV3EJ4Shej+S6kr7nCY/LuFc+L/DGJcr17aqV+GKQkapeKfJeCYveWSVLucB4K F3YQ== 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 c126-v6si742060pfa.130.2018.07.26.00.16.48; Thu, 26 Jul 2018 00:17:03 -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 S1728899AbeGZIbV (ORCPT + 99 others); Thu, 26 Jul 2018 04:31:21 -0400 Received: from seldsegrel01.sonyericsson.com ([37.139.156.29]:11492 "EHLO SELDSEGREL01.sonyericsson.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727847AbeGZIbU (ORCPT ); Thu, 26 Jul 2018 04:31:20 -0400 From: Snild Dolkow To: CC: , , , , , , , , , , , , , Subject: [PATCH] kthread, tracing: Don't expose half-written comm when creating kthreads Date: Thu, 26 Jul 2018 09:15:39 +0200 Message-ID: <20180726071539.188015-1-snild@sony.com> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20180725112619.00b629cd@gandalf.local.home> References: <20180725112619.00b629cd@gandalf.local.home> 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. Cc: stable@vger.kernel.org Fixes: bc0c38d139ec7 ("ftrace: latency tracer infrastructure") Reviewed-by: Steven Rostedt (VMware) 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