Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp784011pxb; Mon, 25 Oct 2021 18:55:55 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx8gAcyfFgeWabdqNJ2tzFRJsPJMDDNct3nrvmcXhY0sSRk9ZrWSEH4cIsUVFG0LamXAXgr X-Received: by 2002:a17:906:5d0a:: with SMTP id g10mr25424140ejt.494.1635213355732; Mon, 25 Oct 2021 18:55:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1635213355; cv=none; d=google.com; s=arc-20160816; b=mWHf8QUJRsBQX9gDQpLi3SN4PnImHnYs7XuOlE3tmwDnJAnGmimIiz5191BRrxY3Qw GS24ZK7dTy2C287H9HEHtvvwLpZmRWfWw+avwJ58thWllOGpqxuJJ3nH69IAWvfC0/vp i9yrvIpIZDZWFW0mCa2qppyEuJ1rcWDUfOSIml7X/E+c34ji3hIK4Xk5xSdvn0E4A+5c ZGFITXSp3bY4HIUsDk6/ewe7dbJAYU3aAri1iXnk9PHdiwjQFEtnkvHKS+noE5vN8r1f ldgJCVuaf01ZDcWb9fJ1kO6hx6aHEOzaHt05GCNLQk69IPKwynsuN8ygWR8vJU+xSdET owWg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=iHfgSZESuBtNRWpoQ0O3+KS410Lf4cM3SnOfKO347UQ=; b=bOwdu9O6G+PiG9K9VPNuQZ/5/rTSspKNDnv6ZXBl2S8Pl5ad++qlcJpF2lReFceu7q +I1AkFLmiwETHj4jBpiVWpuU6zcn1sg75x80dcW+WJCVqp5yRuxyKNrOMZwbosWsLL0a 0+etmnWs8S+Jghw/JSI0iATZ/fe+rRvhRCPGJQrNnGO9w7qI33I9l1kbapNUTAeVxG7M J3uNTXFtROyD2E0Zm4fIrPkYAPL3JaA1uh+A2hlNrbpKF6JfOJ0RkPZilPs6GHbZ8SaI /AbBpOBrPyWffUSybkjQhEyhHpFlgmbNQaVhNvy+weTZWRh4OUoI4wpXoCt8kgbPlsd3 mtlA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=Reu7Zfhw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id w13si18437507edr.368.2021.10.25.18.55.31; Mon, 25 Oct 2021 18:55:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=Reu7Zfhw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234938AbhJYViP (ORCPT + 99 others); Mon, 25 Oct 2021 17:38:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53920 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233940AbhJYViG (ORCPT ); Mon, 25 Oct 2021 17:38:06 -0400 Received: from mail-pj1-x102d.google.com (mail-pj1-x102d.google.com [IPv6:2607:f8b0:4864:20::102d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1312EC061767 for ; Mon, 25 Oct 2021 14:35:44 -0700 (PDT) Received: by mail-pj1-x102d.google.com with SMTP id n36-20020a17090a5aa700b0019fa884ab85so457566pji.5 for ; Mon, 25 Oct 2021 14:35:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=iHfgSZESuBtNRWpoQ0O3+KS410Lf4cM3SnOfKO347UQ=; b=Reu7Zfhwb2ow26i8wER7gTUYsj+/+vm3SHpVkiU5o03ZeeoZzyL2Vgkqxh4inOpYnO sIv6S7DVrUX4hxLj1wywwk4ilQARf94QkoGSKt86yRTCISAioxSgdb+DyznYDGjhuSoo ZelV/oR2YAMluKuDU0FY/KCCbUgaliM74RtVk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=iHfgSZESuBtNRWpoQ0O3+KS410Lf4cM3SnOfKO347UQ=; b=j1j0CgmQnaMU1Qx/6ehNSeRctVI4eWQr8xJGNnbVC0q5uTMuGh402ZJMuRL3TPYYlm 6Wi0bSDgXBx4yzfuOtE+kqwkPCxygTepz4GJHMm4IqSZUcBBSUYRj7lf3bdenL681GhC i2DQFxDSm8XoR4zblBcEE9r5yhiMz+dZEd/C6Z1/74KfrgV2hzxEJR62i9r8jvu+B2aP k90SHHtwksIZZ2d95u45opSi4BweIO5mf6AzDtaUNC83rCLrwNKYZqTwtdBpBBWM6qwe Izh3Pm/Y9SF4E10BsB65LEQYX0sa8nX0KMD+K+JTZgjrtfIlHn9oaGovMpSJm1yFfDlh Z+2Q== X-Gm-Message-State: AOAM530wlszwIxV4Kr45ekXo1zdQshmxAnnNC/8ZDTrgZQ2Mru+8YiZH KJOybFUPhtueIRhv9RtbFSsFJw== X-Received: by 2002:a17:902:e5cb:b0:13f:25b7:4d50 with SMTP id u11-20020a170902e5cb00b0013f25b74d50mr18287509plf.38.1635197743615; Mon, 25 Oct 2021 14:35:43 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id a20sm19562774pfn.136.2021.10.25.14.35.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Oct 2021 14:35:43 -0700 (PDT) Date: Mon, 25 Oct 2021 14:35:42 -0700 From: Kees Cook To: Yafang Shao Cc: akpm@linux-foundation.org, rostedt@goodmis.org, mathieu.desnoyers@efficios.com, arnaldo.melo@gmail.com, pmladek@suse.com, peterz@infradead.org, viro@zeniv.linux.org.uk, valentin.schneider@arm.com, qiang.zhang@windriver.com, robdclark@chromium.org, christian@brauner.io, dietmar.eggemann@arm.com, mingo@redhat.com, juri.lelli@redhat.com, vincent.guittot@linaro.org, davem@davemloft.net, kuba@kernel.org, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, dennis.dalessandro@cornelisnetworks.com, mike.marciniszyn@cornelisnetworks.com, dledford@redhat.com, jgg@ziepe.ca, linux-rdma@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, oliver.sang@intel.com, lkp@intel.com, Andrii Nakryiko Subject: Re: [PATCH v6 12/12] kernel/kthread: show a warning if kthread's comm is truncated Message-ID: <202110251431.F594652F@keescook> References: <20211025083315.4752-1-laoar.shao@gmail.com> <20211025083315.4752-13-laoar.shao@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20211025083315.4752-13-laoar.shao@gmail.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Oct 25, 2021 at 08:33:15AM +0000, Yafang Shao wrote: > Show a warning if task comm is truncated. Below is the result > of my test case: > > truncated kthread comm:I-am-a-kthread-with-lon, pid:14 by 6 characters > > Suggested-by: Petr Mladek > Signed-off-by: Yafang Shao > Reviewed-by: Kees Cook > Cc: Mathieu Desnoyers > Cc: Arnaldo Carvalho de Melo > Cc: Andrii Nakryiko > Cc: Peter Zijlstra > Cc: Steven Rostedt > Cc: Al Viro > Cc: Kees Cook > Cc: Petr Mladek > --- > kernel/kthread.c | 7 ++++++- > 1 file changed, 6 insertions(+), 1 deletion(-) > > diff --git a/kernel/kthread.c b/kernel/kthread.c > index 5b37a8567168..46b924c92078 100644 > --- a/kernel/kthread.c > +++ b/kernel/kthread.c > @@ -399,12 +399,17 @@ struct task_struct *__kthread_create_on_node(int (*threadfn)(void *data), > if (!IS_ERR(task)) { > static const struct sched_param param = { .sched_priority = 0 }; > char name[TASK_COMM_LEN]; > + int len; > > /* > * task is already visible to other tasks, so updating > * COMM must be protected. > */ > - vsnprintf(name, sizeof(name), namefmt, args); > + len = vsnprintf(name, sizeof(name), namefmt, args); > + if (len >= TASK_COMM_LEN) { And since this failure case is slow-path, we could improve the warning as other had kind of suggested earlier with something like this instead: char *full_comm; full_comm = kvasprintf(GFP_KERNEL, namefmt, args); pr_warn("truncated kthread comm '%s' to '%s' (pid:%d)\n", full_comm, name); kfree(full_comm); } > set_task_comm(task, name); > /* > * root may have changed our (kthreadd's) priority or CPU mask. > -- > 2.17.1 > -- Kees Cook