Received: by 2002:ab2:6486:0:b0:1ef:eae8:a797 with SMTP id de6csp7176lqb; Fri, 15 Mar 2024 13:09:23 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUyjIbomtIvqGQn3laClVSwpTMIZc7LeK3Eib3B3nzb9YucV+AL00EoOXRKsHNvd++RqorPESl81KTUUWmO+KvK8CGdXA3Mc8/TaAJoqw== X-Google-Smtp-Source: AGHT+IEYiOcNgBMTY0CeQQRYwNqwUE+pVkAELKrcleg3EhKW088t+Qetx3981znervE3HmTxilAG X-Received: by 2002:a05:6830:22cc:b0:6e5:3210:e8f5 with SMTP id q12-20020a05683022cc00b006e53210e8f5mr5884008otc.22.1710533363113; Fri, 15 Mar 2024 13:09:23 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710533363; cv=pass; d=google.com; s=arc-20160816; b=jGSZwax2y//J6F3flHtvT0a5w78FrPswZWWnKAeHO5/PfftMH8bWBMSBKhq7fvRXtv lblkAgvI7QEavgxu7ox93Mzpy7cc+qcC5Qkoz3Mb8ZX4anjc9zMPYB1fHjJUuIRwKmCN mN9O8EEVliycE2fNwC6lKysUJIM/OlilAObWht988wGUcZwkdDPGGjpJ3/6qYXHRnaX4 Sng6BZZc3mELHkDRBoG6b93KrWTLEPFjMNkIAFTkj2v6gZeW95XFqBwywCGCUP08VRbE djVo4G04qBiIi13TAPg4LTHZGBl8mu1XqsIKqsiqiycsoWlTQ/nK9pPOj66WjxZSfiMG i5DQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:dkim-signature; bh=XbpSIycwsc2tWO8Q5CI8ZjMC/0xhm3/hIri+gWrvEyg=; fh=xoCv8vCT59v/ZPCYJWrdQL28TdoZH3/K+08pdBVMaro=; b=wCchDI/dupeS2tSbY2jD+b6JzYO3PdTV5YpFxo//4HS+ej4i9lYKRSMJsTakKQM20m ZnCah8VBeO9mxTy7vB4BrEUjk93/07zgvQE1Y6B9k68yWtyIbp4h6oqCVJBcC+cTMcB7 mekkyuAPElIZZpRZAQTyxoqmZJQyQo4C7WUuyWKvmbrff6yStUFcCPw+focIR41Bl3cy tbYFR8ZkoHCSlFYmRHUj+4diM/0W9TrYHhw4p6WhJ3xf0f1Nv5GDpJa3z86vDTOpPdiu WZdA5P4cxxeJrn2gYWo62Lufoq3c09l/tewGVgAJYDufOCEoZQxPkdf5hyHIiXYRVtma ak6A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=NbzuEcPO; arc=pass (i=1 spf=pass spfdomain=flex--justinstitt.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-104869-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-104869-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id bi7-20020a05620a318700b00789e99d58cdsi1053076qkb.501.2024.03.15.13.09.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Mar 2024 13:09:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-104869-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=NbzuEcPO; arc=pass (i=1 spf=pass spfdomain=flex--justinstitt.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-104869-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-104869-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com 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 BC16C1C217CF for ; Fri, 15 Mar 2024 20:09:22 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C3EF3535D6; Fri, 15 Mar 2024 20:09:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="NbzuEcPO" Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (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 A62DE1EB2C for ; Fri, 15 Mar 2024 20:09:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710533356; cv=none; b=azLZybFQUQDaieISgp7HqWwYKDNpHUZdAGjJJtMvetLr0kloRclUGtHCPsXvk2mAWpbAq+eiJF/yI3l+FiDO2cKUFrLwSjipM8ZTjevWIDTPiduGzIGyMGtWKallAR9oN/wTz31KqR/vyt8r2vBiXlYPKVXqASvbXNeUDNpsyhE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710533356; c=relaxed/simple; bh=lAjciVBn7fEdF0vkatR5U3zbNKfVnP57tHInDU09ybk=; h=Date:Mime-Version:Message-ID:Subject:From:To:Cc:Content-Type; b=UZSdwaIHsOgGKRKf9bZaEj6Tl+Ol9BzilWLTqYsqFI3P3N0Vc9w+YjVoLprkCbbPrEqVhLdL6Tx1lijIezQRFPx2Z5ssBZj+2NgY7mjo+TdrDym05D8cL/JMerZHtqExpS1RS07Vqd4SbdEXtVqDXCpto5uDIu76t1xxC/U/b+M= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--justinstitt.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=NbzuEcPO; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--justinstitt.bounces.google.com Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-60a605154d0so30084837b3.0 for ; Fri, 15 Mar 2024 13:09:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1710533352; x=1711138152; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=XbpSIycwsc2tWO8Q5CI8ZjMC/0xhm3/hIri+gWrvEyg=; b=NbzuEcPOBY4VvI64L8EIzsAtCdos3OIYcEbxTucLU1d38+wML7Kgm2iOMJ0WEMSF0r HRuela7Rn3ip05FLPyuU8xSQt+Z01H6JZshINoXwnCogpyJwc5Dx69U8ExZrD2vBV0D2 LZfY18Tsy6gu8WpLv/a7xBbp+qipLIpTLvE71wuXVGa6prSXUUu8fD5fqwf++Q4k3M8m QmYhuCbErwM+FA7uH+1iDQ8dyv+chVSUJqrGSk3pfDpovRZ5HU9sImIrM0sOkO7oSXg0 VUtK0JEOn/6jHEzLc8ynwGjSsGJHFR6vY64sMNtm31B50EV9dk+ZpQwO/BKcdiklXPzH B1HQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710533352; x=1711138152; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=XbpSIycwsc2tWO8Q5CI8ZjMC/0xhm3/hIri+gWrvEyg=; b=A4x2IT8XXdQZI69eRKcmlfu1Krl4DhCh3gVSPe4SdtUXH+qqqbbwcOSSlJANrvCcs6 cOPwEn35vs5GoEINz/mLAZAV3yMzCvs5E5Ep9lCgD0Kj2Bb05YbLuTzX3AMrOiaWdV61 K4rIAWr7aL3udfHAr9lsPXQb/tNXeAsQfQ+mJp6SzWeOFGJR7XO7N3HeOeNhxRil1lIn Hd4R72Fj8PnSCzaqtc5OF8S9qYzrpdGFJs7f1jIUBm1sEMF7c0F+qNPMlHQLUt8PHCF5 sQXy5f4GeEpwE70hvpCc+cn4Bq3cC7mbspaMwNv+N3OBU1HfV4fcQYPDol1cFa8YBnXw ACpQ== X-Gm-Message-State: AOJu0YwIGFkUjNh3SdG2lHef5xMXQCbNpjh8ExjTfQeqdwDsTvB9ruv6 pkDQNfBgYswAOa4WWPr/kDipkL5zK6rSP+3hy/Ry4BUzPLdByFRqH78gWYb3pRNlblRFkQFyyKn 5fxRDdQJ8k0GLH2d1ssj0Qw== X-Received: from jstitt-linux1.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:23b5]) (user=justinstitt job=sendgmr) by 2002:a05:690c:3513:b0:60f:d74f:a5ba with SMTP id fq19-20020a05690c351300b0060fd74fa5bamr454040ywb.2.1710533352687; Fri, 15 Mar 2024 13:09:12 -0700 (PDT) Date: Fri, 15 Mar 2024 20:09:11 +0000 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-B4-Tracking: v=1; b=H4sIAOaq9GUC/43NTQ7CIBCG4as0sxYD9EfblfcwLihMy0SFBhqia Xp3aXdujMv3S+aZBSIGwghdsUDARJG8y1EeCtBWuREZmdwguay4lIJFNwVy88C0RX2f1KwtU+e mQtOcsDcc8uUUcKDXrl5vuS3F2Yf3/iSJbf3tJcEEK5Wuuaz7um3EZfR+fOBR+ydsYJJ/IDIjb a8wC0Zhyb+QdV0/Q4Zf7fwAAAA= X-Developer-Key: i=justinstitt@google.com; a=ed25519; pk=tC3hNkJQTpNX/gLKxTNQKDmiQl6QjBNCGKJINqAdJsE= X-Developer-Signature: v=1; a=ed25519-sha256; t=1710533351; l=2367; i=justinstitt@google.com; s=20230717; h=from:subject:message-id; bh=lAjciVBn7fEdF0vkatR5U3zbNKfVnP57tHInDU09ybk=; b=i4dhQ8hRHLCWukhlCP0iY3njxd/7+izprjcy6rYPHPAKA5+J+JsF2SwDFOHUnQaBn7m+z0px3 aw0OtHtVuojCRayqi83sDz3Y6oz2JQSt1/NMxR2iqAlg2vzIN7rU1x4 X-Mailer: b4 0.12.3 Message-ID: <20240315-snprintf-checkpatch-v3-1-a451e7664306@google.com> Subject: [PATCH v3] checkpatch: add check for snprintf to scnprintf From: Justin Stitt To: 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 , Justin Stitt Content-Type: text/plain; charset="utf-8" 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 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..69dfb7412014 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); } +# snprintf uses that should likely be {v}scnprintf + if ($line =~ /\bsnprintf\s*\(\s*/) { + WARN("SNPRINTF", + "Prefer scnprintf over snprintf - 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