Received: by 2002:ab2:6203:0:b0:1f5:f2ab:c469 with SMTP id o3csp2789778lqt; Tue, 23 Apr 2024 01:18:34 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCU9BfzFWqWGVgjB21Al922PXndHskHjfqzBsBYXKA78IfMjgxwHMmn0nbQ+r+P0sLRvWdcd4Z3kv3v5I7s1vvUiI/I1t3ic5i0x5/6fzw== X-Google-Smtp-Source: AGHT+IEu+xFCgA2Kao+Mx2pDydE71Un8VY9ekOSLBXzV5EvZmQns+K4OeHpZlnJvITuTcB1Vng4k X-Received: by 2002:a05:620a:57c3:b0:78d:556b:fcb0 with SMTP id wl3-20020a05620a57c300b0078d556bfcb0mr13600968qkn.40.1713860313915; Tue, 23 Apr 2024 01:18:33 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713860313; cv=pass; d=google.com; s=arc-20160816; b=qCADsqE7a8bHTqgRo4v2sn74O6sp3bBDYsSpcujMMbx4B8tfo1PfKAGNUqw/4Kqnm9 Bk8BAg3wifkGMRFMewL2sN8qP0Xu82bD3AQbCKvQW73BP7Powdwx1iLsDPEfWSLaSI2a +iZu6U89yEXR6euGJJSsclaNhTGVMV8+jbLxQuSK4aLiHH95R1w1zTtBLpFaaJfgBrG3 /JV0smLeo7du2dz7YsrXddivolX7MpVdQAIajuMR9vPBLU2RUUpz3gqU20Ny3C21Qe6I D7Bb0v138dyrnmlskxpeRCIM2qfr2bMr3g2ovdLNGU817PTEzk//sLJhRtoEKxilCRvl llBQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id:dkim-signature; bh=f/VsrIrGMARLF7VxqFkpnHr/IAHwcjt8sUjQE+/j5as=; fh=Zx2aZNNYAnocg97yfOPxIB65QWAwqCuDJOA1QZ47s2g=; b=jgbYK8Z93sikRNS9RHWElFOlfcqQmmkupItMk6NKO98TxotQZ4cGoaItwRq6r92l3s 1U5lII/p+fN3PR34zwrEGkGTMb1QKWhgj/D5fQ7H8bVmsvo+/XUDoqhakr0l6kOvwIua +qk+cB8hmRb+6b7IBMHh7Us6rd0sPlSsbW0I1z/A/MDqzXO1A9qAuJsNkV+NCX191vAS r7VTIOpQYdGaTnnXIT65DhOfp/LqkMUOwPlNOGPj5kwFIQvaKdwux5WxJrZhm+yOVRhL 0Gz9ZkrjA8WGtS/EqwGgd9UVTvxuVU3wxh2OEuHhlQbW2h7KCGzGUhAj9VdjMkFGJy0o 7FIQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@wanadoo.fr header.s=t20230301 header.b=JfIv657S; arc=pass (i=1 spf=pass spfdomain=wanadoo.fr dkim=pass dkdomain=wanadoo.fr dmarc=pass fromdomain=wanadoo.fr); spf=pass (google.com: domain of linux-kernel+bounces-154669-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-154669-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=wanadoo.fr Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id wi22-20020a05620a571600b0078f04a6ac98si11822057qkn.321.2024.04.23.01.18.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Apr 2024 01:18:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-154669-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@wanadoo.fr header.s=t20230301 header.b=JfIv657S; arc=pass (i=1 spf=pass spfdomain=wanadoo.fr dkim=pass dkdomain=wanadoo.fr dmarc=pass fromdomain=wanadoo.fr); spf=pass (google.com: domain of linux-kernel+bounces-154669-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-154669-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=wanadoo.fr 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 ny.mirrors.kernel.org (Postfix) with ESMTPS id A1C4F1C21599 for ; Tue, 23 Apr 2024 08:18:33 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3C5A450297; Tue, 23 Apr 2024 08:18:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=wanadoo.fr header.i=@wanadoo.fr header.b="JfIv657S" Received: from msa.smtpout.orange.fr (msa-211.smtpout.orange.fr [193.252.23.211]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D4E3C2B9CA; Tue, 23 Apr 2024 08:18:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.252.23.211 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713860306; cv=none; b=kGJgXGpNtvzq+tkJ3ST9DN8lhrLV7fB34BuWOX/t/QY74y5HhMez+5Dmvn5TTOXUiJh/StviInMJSnkRWIQ+GlZ+tCLiXItwpWoQVq95VKCtLJWqzlPJWiYE64owzdGPD/IcjY9HRwxwu2Ca95Kk7/E6kR168BeIg2PmVMhjibE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713860306; c=relaxed/simple; bh=xtv4BPwGy3ZyqEsmZOD2D5Ii5lu5tCcCxDspMWBzbkE=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=VWzpKWaa83QSAHohR+Q8TmtBqZR+wkF83D0tlnNQsrYvNhzQMtUrmqLkp8/OlVxCuDtS/doiuKE1Y8H3SLcBZ7bWR5Amnp4Hz3x+Sr4tUnqvC159HvoSw9kMot1in1ijqky4XloNYZeJ3quZJZ3FfC/q7PxnpGnQ+Cp7+AiF6dA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=wanadoo.fr; spf=pass smtp.mailfrom=wanadoo.fr; dkim=pass (2048-bit key) header.d=wanadoo.fr header.i=@wanadoo.fr header.b=JfIv657S; arc=none smtp.client-ip=193.252.23.211 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=wanadoo.fr Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=wanadoo.fr Received: from [192.168.1.18] ([86.243.17.157]) by smtp.orange.fr with ESMTPA id zBKvrN5EReDY4zBKvrdaV6; Tue, 23 Apr 2024 10:17:11 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=wanadoo.fr; s=t20230301; t=1713860231; bh=f/VsrIrGMARLF7VxqFkpnHr/IAHwcjt8sUjQE+/j5as=; h=Message-ID:Date:MIME-Version:Subject:To:From; b=JfIv657SnHdT/BHZ1u8q7jpA+0EQq2HvogX+eDqGqiUIxZJENxnjEtSvbW02gPq5C NYXt8aHMpZ83zcuTfnZkWc7wwRsuKP9CovoovVMqFvJUAvmmNfhO+Syu7dN6OqQX0v m/NOkkT6+/9YX41PxtIDhAzHYHRLhUavEnh1NK/kPZlN39W3NXIW76t9CrmKDw9daA RQ932dljQxjd1zzqK7UuxPcOAlyUiVSBUkrd800i2vdd4CwQ0NnD3k8/dIq91dfGXl 8IQO0mvpFf+3dHw/I4B+jyepaydB3fV0Tpe/yqVWakcvo/3vHoei8r/yX34XaeY53F 0+VOt9e3trZNA== X-ME-Helo: [192.168.1.18] X-ME-Auth: Y2hyaXN0b3BoZS5qYWlsbGV0QHdhbmFkb28uZnI= X-ME-Date: Tue, 23 Apr 2024 10:17:11 +0200 X-ME-IP: 86.243.17.157 Message-ID: Date: Tue, 23 Apr 2024 10:17:09 +0200 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v5] checkpatch: add check for snprintf to scnprintf To: Justin Stitt , Andy Whitcroft , Joe Perches , Dwaipayan Ray , Lukas Bulwahn Cc: linux-kernel@vger.kernel.org, Lee Jones , linux-hardening@vger.kernel.org, Kees Cook , Finn Thain References: <20240422-snprintf-checkpatch-v5-1-f1e90bf7164e@google.com> Content-Language: en-MW From: Christophe JAILLET In-Reply-To: <20240422-snprintf-checkpatch-v5-1-f1e90bf7164e@google.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Le 22/04/2024 à 23:33, Justin Stitt a écrit : > 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 v5: > - use capture groups to let the user know which variation they used > - Link to v4: https://lore.kernel.org/r/20240408-snprintf-checkpatch-v4-1-8697c96ac94b@google.com > > Changes in v4: > - also check for vsnprintf variant (thanks Bill) > - Link to v3: https://lore.kernel.org/r/20240315-snprintf-checkpatch-v3-1-a451e7664306@google.com > > Changes in v3: > - fix indentation > - add reference link (https://github.com/KSPP/linux/issues/105) (thanks Joe) > - Link to v2: https://lore.kernel.org/r/20240221-snprintf-checkpatch-v2-1-9baeb59dae30@google.com > > 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/ > --- > scripts/checkpatch.pl | 6 ++++++ > 1 file changed, 6 insertions(+) > > diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl > index 9c4c4a61bc83..b7404e53c554 100755 > --- 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); > } > > +# {v}snprintf uses that should likely be {v}scnprintf > + if ($line =~ /\b((v|)snprintf\s*\()/) { Nit: I think that /\b((v|)snprintf)\s*\(/) would be nice. Otherwise, ( would be added to the message. CJ > + WARN("SNPRINTF", > + "Prefer ${2}scnprintf over $1 - see: https://github.com/KSPP/linux/issues/105\n" . $herecurr); > + } > + > # ethtool_sprintf uses that should likely be ethtool_puts > if ($line =~ /\bethtool_sprintf\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) { > if (WARN("PREFER_ETHTOOL_PUTS", > > --- > base-commit: b401b621758e46812da61fa58a67c3fd8d91de0d > change-id: 20240221-snprintf-checkpatch-a864ed67ebd0 > > Best regards, > -- > Justin Stitt > > >