Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9B299C43217 for ; Wed, 10 Nov 2021 20:17:40 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7A9D161268 for ; Wed, 10 Nov 2021 20:17:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232344AbhKJUU0 (ORCPT ); Wed, 10 Nov 2021 15:20:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37208 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231609AbhKJUUY (ORCPT ); Wed, 10 Nov 2021 15:20:24 -0500 Received: from mail-pj1-x1029.google.com (mail-pj1-x1029.google.com [IPv6:2607:f8b0:4864:20::1029]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 10F42C061767 for ; Wed, 10 Nov 2021 12:17:36 -0800 (PST) Received: by mail-pj1-x1029.google.com with SMTP id v23so2487354pjr.5 for ; Wed, 10 Nov 2021 12:17:36 -0800 (PST) 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=kJ30f4yltU8P9D1vIvrgm4JmaUKFVTUymd542MzbX34=; b=g9lNF+pd24Lg+MqAqHCV7wh24XIQNX4IMaEiJo9WPFtT4ECrdlVpmAPO8QVV2WHbFK nQ+O9QSeWTirPqQxEh3uFPZ+WLk8Q0CYxvBO5RUARXtXksYhPr4lmu5668PzlePyOmcl As4OgFb6VXL9nsV00X1BkxHIuNlUxiMXmbd8w= 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=kJ30f4yltU8P9D1vIvrgm4JmaUKFVTUymd542MzbX34=; b=ESZoUQR7lvby5DaOhX72Y6HlJ6lqbTpBLU6pTGV/zBv9P656owxZFj3uYwAjtO5wq3 1sm0VeNV3X6Zu8O6cTmG18CMTvW4kq88fDu0MkZfReCByMQ6ZDOT0qr7tFJN4tBni4sV PepC5+Y3XHzamC40mJQT4XcCE/f7DVCiQ7iu/e+ieflPZwI/ZdO8hCH7Vk8/jQ892P16 Xn9tJaS0+vuNFvH3PCNt054po/n0FmLkuGI85qLjegzq6eE0CKCle01KFWDaxguL/9d1 eDaKp5C2UFKljUyTz/JOhlfx1/PJ5Z8LMyCW486ctv8DJjnZsQv5+Zilqc6hlQ7+524I 4y5Q== X-Gm-Message-State: AOAM532ARRJCnZofyyziuJI/H3j3i/zZSa4pgov3RLnXr6Rc43dw+GUo 9gSW/pDig9ZH7eDD90xH09O/Eg== X-Google-Smtp-Source: ABdhPJy9OJ/YGW/eEJO6u7l+llSGLL86Mlc82irnMKIDFwKNMwM8Slu26rVYUZa6XKnHewItU9EAMw== X-Received: by 2002:a17:90b:33d0:: with SMTP id lk16mr1928527pjb.66.1636575455497; Wed, 10 Nov 2021 12:17:35 -0800 (PST) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id fw21sm6200587pjb.25.2021.11.10.12.17.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Nov 2021 12:17:35 -0800 (PST) Date: Wed, 10 Nov 2021 12:17:34 -0800 From: Kees Cook To: David Hildenbrand Cc: Yafang Shao , akpm@linux-foundation.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, Mathieu Desnoyers , Arnaldo Carvalho de Melo , Alexei Starovoitov , Andrii Nakryiko , Michal Miroslaw , Peter Zijlstra , Steven Rostedt , Matthew Wilcox , Al Viro , Petr Mladek Subject: Re: [PATCH 1/7] fs/exec: make __set_task_comm always set a nul terminated string Message-ID: <202111101215.A42612FEC@keescook> References: <20211108083840.4627-1-laoar.shao@gmail.com> <20211108083840.4627-2-laoar.shao@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Nov 10, 2021 at 09:28:12AM +0100, David Hildenbrand wrote: > On 08.11.21 09:38, Yafang Shao wrote: > > Make sure the string set to task comm is always nul terminated. > > > > strlcpy: "the result is always a valid NUL-terminated string that fits > in the buffer" > > The only difference seems to be that strscpy_pad() pads the remainder > with zeroes. > > Is this description correct and I am missing something important? Yes, this makes sure it's zero padded just to be robust against full tsk->comm copies that got noticed in other places. The only other change is that we want to remove strlcpy() from the kernel generally since it can trigger out-of-bound reads on the source string[1]. So, in this case, the most robust version is to use strscpy_pad(). -Kees [1] https://github.com/KSPP/linux/issues/89 > > > Signed-off-by: Yafang Shao > > Reviewed-by: Kees Cook > > Cc: Mathieu Desnoyers > > Cc: Arnaldo Carvalho de Melo > > Cc: Alexei Starovoitov > > Cc: Andrii Nakryiko > > Cc: Michal Miroslaw > > Cc: Peter Zijlstra > > Cc: Steven Rostedt > > Cc: Matthew Wilcox > > Cc: David Hildenbrand > > Cc: Al Viro > > Cc: Kees Cook > > Cc: Petr Mladek > > --- > > fs/exec.c | 2 +- > > 1 file changed, 1 insertion(+), 1 deletion(-) > > > > diff --git a/fs/exec.c b/fs/exec.c > > index a098c133d8d7..404156b5b314 100644 > > --- a/fs/exec.c > > +++ b/fs/exec.c > > @@ -1224,7 +1224,7 @@ void __set_task_comm(struct task_struct *tsk, const char *buf, bool exec) > > { > > task_lock(tsk); > > trace_task_rename(tsk, buf); > > - strlcpy(tsk->comm, buf, sizeof(tsk->comm)); > > + strscpy_pad(tsk->comm, buf, sizeof(tsk->comm)); > > task_unlock(tsk); > > perf_event_comm(tsk, exec); > > } > > > > > -- > Thanks, > > David / dhildenb > -- Kees Cook