Received: by 2002:a05:7412:798b:b0:fc:a2b0:25d7 with SMTP id fb11csp869819rdb; Fri, 23 Feb 2024 02:41:10 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUEobiUYiYI+b09eaidR1JSlM42PbzPUQmXmx/dPs7CpHLfKRnl6ZzGKrXlmhzorU3VJjOIQlGC6mTkRte5cDlphLVdheI2eh4f7Q5iMg== X-Google-Smtp-Source: AGHT+IG0ocCnX0UfDXe4usXUfmkEBkprXVve7Gi0Z843ILjMz2sdXfpjRE9fIzvXRC++BM9jZiRp X-Received: by 2002:a9d:4f0c:0:b0:6e2:e674:8cb9 with SMTP id d12-20020a9d4f0c000000b006e2e6748cb9mr1395564otl.36.1708684869748; Fri, 23 Feb 2024 02:41:09 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708684869; cv=pass; d=google.com; s=arc-20160816; b=OZ0+b3ND+wakOrvn2peyDvNggZOt3iW3k+18undWHOwqQVr+mxx8oXf5E59KXdZ6eg vchTe/o63iOhXtt0syqIzElKOMz/wYcWTXLs4Avn6XQtaOGkZHnHCi1eC5gxcsri7SBn 5PjIJKawH57CitYA3mKOOyIkPDohwVqgYGwYlDqkHuQo4iBeRFE9TGSxkbQm5HtvOalt uQ/ie6WWYoVlMMRYppnk1emZeses29vy7id6a8V/djZAozjO3n2jF822OJ36HqAqQCW2 +Lvc4TX4Dm/HwxXM+rOqSACnwaLq5rAUZiNmHeBCoUrVrSOpxCpowQcm3YmkFQeV1AFr YoJQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :references:message-id:subject:cc:to:from:date:dkim-signature; bh=sk1z/DRvYWPZ4VeVyrKZEsgqQu7xFdDIEV/S/aN503U=; fh=8MxWmPi1LrixGbp++g/jB7Lx65JEiLCtIi5wxAvEaHA=; b=BVnZUcBfxZUtIwoptqRzD8oVpA3Gg8541lH7t4PxjO72HW5s+67PjtRuibV02MmZAi f86oai3j4Jl8vmZ4Kpbbbn2daqnoxvKsQ96qNZldM+RZwrIlqGaKlZhdA4o9cJwV7iq0 FF3eDdrmXZYJXJmPbQRXg9XrNQ9/Fsxp6OxUbf9b+g9uildripJ15ZXWEVfMeo/TwOdq 35kg3f6PbFpgOrmjaMhQ/8h0E5jYQ3PEni5DCxl9JqXzAboKxZlBoXeT46jd1AkuzJVl CQmY0djG45M2SKZ8IfszxrrxlHk1PyAgBUW6CSLrG1KmqUABN+sGRNDOPN9bIB1DMT5l JmIw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=r2HjcuNN; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-78154-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-78154-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id b17-20020a639311000000b0058986c61bb6si11968936pge.706.2024.02.23.02.41.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Feb 2024 02:41:09 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-78154-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=r2HjcuNN; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-78154-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-78154-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 2BF1D287362 for ; Fri, 23 Feb 2024 10:41:08 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3739F76907; Fri, 23 Feb 2024 10:38:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="r2HjcuNN" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5E30B67C7D; Fri, 23 Feb 2024 10:38:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708684730; cv=none; b=pCR/QhomsLymZ8iVrBa1DgzcZFFRS2gXHJb1hNtViRBpYuDpZJI6vLry5pXNSGXfF96quzsKHvi/2zjY2AtE/Fi5j9vGeR7ifU1gt7VXs+2VzPQBd5x9WE3AVMXXQ9NTL8SrrIWBWpimmyayYLMwg89f/o73U9fpwAngoO2Wiug= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708684730; c=relaxed/simple; bh=NWT5bX994Gc9URSqTJ1uQ/MVapIMzP60ER2OflZLuLw=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=pkdVSY2y3vhycJzy+3QgAkiiCqy7at0z8HM2APMwuaWZfk3XakvUEs4+DapT+0ADZbIC1bt5OCktAwwtViqTfd/4CMJdDDp3G8zZ7a+FyOwHCbYYcvFQjwCpXTPQvQjLDqxbnwLQhcENUjfvO/7DLvb1CSeIPUJ2U22KlYblBos= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=r2HjcuNN; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1BDDAC433C7; Fri, 23 Feb 2024 10:38:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1708684730; bh=NWT5bX994Gc9URSqTJ1uQ/MVapIMzP60ER2OflZLuLw=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=r2HjcuNNUujkUWMNDNnrB78JazeIbOj63Yi6su96i4X1jHKfp+xNSEA+JErWEz3r+ ZhCP+6MzK7joA79HaNeDScuq08FfZidzymx3wWrBVCF3kv4zJV+knzxB5QZF06Wh9D ayjLt+1eFkSpXsU0G3AYZuytji4M94H4Er3PpEHCAUyZTv7lmG/EHA/3tQJ8KZgja4 1p/HW8uThdRxNOSLfBLdS/TbtwGO1Ze83JzlfXwYiwu8kpAutImi6gSzcB2RlpKpc4 9tmIjQDK5WDf2koNRQoVNCvqc8gzyHbTmhwVPzBY7hExmClb1fjq3c1o2alhjh6u// CiVB5R+MMpy/w== Date: Fri, 23 Feb 2024 10:38:45 +0000 From: Lee Jones To: Joe Perches Cc: Justin Stitt , Andy Whitcroft , Dwaipayan Ray , Lukas Bulwahn , linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, Kees Cook , Finn Thain Subject: Re: [PATCH v2] checkpatch: add check for snprintf to scnprintf Message-ID: <20240223103845.GP10170@google.com> References: <20240221-snprintf-checkpatch-v2-1-9baeb59dae30@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: On Wed, 21 Feb 2024, Joe Perches wrote: > On Wed, 2024-02-21 at 22:11 +0000, Justin Stitt wrote: > > I am going to quote Lee Jones who has been doing some snprintf -> > > scnprintf refactorings: > > > > "There is a general misunderstanding amongst engineers that > > {v}snprintf() returns the length of the data *actually* encoded into the > > destination array. However, as per the C99 standard {v}snprintf() > > really returns the length of the data that *would have been* written if > > there were enough space for it. This misunderstanding has led to > > buffer-overruns in the past. It's generally considered safer to use the > > {v}scnprintf() variants in their place (or even sprintf() in simple > > cases). So let's do that." > > > > To help prevent new instances of snprintf() from popping up, let's add a > > check to checkpatch.pl. > > > > Suggested-by: Finn Thain > > Signed-off-by: Justin Stitt > > --- > > Changes in v2: > > - Had a vim moment and deleted a character before sending the patch. > > - Replaced the character :) > > - Link to v1: https://lore.kernel.org/r/20240221-snprintf-checkpatch-v1-1-3ac5025b5961@google.com > > --- > > From a discussion here [1]. > > > > [1]: https://lore.kernel.org/all/0f9c95f9-2c14-eee6-7faf-635880edcea4@linux-m68k.org/ > > > diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl > [] > > @@ -7012,6 +7012,12 @@ sub process { > > "Prefer strscpy, strscpy_pad, or __nonstring over strncpy - see: https://github.com/KSPP/linux/issues/90\n" . $herecurr); > > } > > > > +# snprintf uses that should likely be {v}scnprintf > > + if ($line =~ /\bsnprintf\s*\(\s*/) { > > + WARN("SNPRINTF", > > + "Prefer scnprintf over snprintf\n" . $herecurr); > > There really should be some sort of reference link here > similar to the one above this. > > Also, I rather doubt _all_ of these should be changed just > for churn's sake. This is for new implementations only. Kees is planning on changing all of the current instances kernel-wide. > Maybe add a test for some return value use like > > if (defined($stat) && > $stat =~ /$Lval\s*=\s*snprintf\s*\(/) { > etc... > > Maybe offer to --fix it too. > -- Lee Jones [李琼斯]