Message ID | 20240208084512.3803250-7-lee@kernel.org |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel+bounces-57652-ouuuleilei=gmail.com@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:50ea:b0:106:860b:bbdd with SMTP id r10csp29875dyd; Thu, 8 Feb 2024 00:49:37 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCU4NWE6P0AmnEK+JB3pN4OtykQuL5mB7xGLPFFrBX1wyqdFb+RskwPjXzbbRGCgSO9gY5omrY97tilT5Nxp6EK3f48JlA== X-Google-Smtp-Source: AGHT+IGnualld4rQnFf+DrTyiQ9RNGS7rUjc8Txou0MYlu9/pBPDQiHEEkyLD3psjzMXfL3EFHzh X-Received: by 2002:a05:6214:e4b:b0:68c:8ae6:d810 with SMTP id o11-20020a0562140e4b00b0068c8ae6d810mr3602467qvc.0.1707382177574; Thu, 08 Feb 2024 00:49:37 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707382177; cv=pass; d=google.com; s=arc-20160816; b=dhqJTtP2Uh5bPx8ZI7LZ6I3f1yQyAU70BI3KgMohLuaURXwKkPtGC2dNeEUbeBQswH Mbx1SEffMm7u3A4QfKsubrZEL7E4v2v1LGnMrfEZbEzP+ywnYG8ys6ipZsMfafHtfnnb v2Wg8X94nAzBKe1+9x+ntYsasd6cP+0KEcjz3bjo9luDN1u6JmbWFimen1P93kAVJacw 0oa+MXBPgT/moodho4jfFC6aqjnjzxLNyhDiCX4TLbUAfQNP/6lW0QaSzkrReZNCUz+d 2rBEbVlNB7I4Jv5vv3JjbGpC3g/ooxYUhDNMokUxIASBV2LI6+Eq9NO5zXXJDGIuChIc WfTA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=l0L8ciZwLC3c25nf4Mp4T1utbUQPGcw4aR9MOKxycvA=; fh=vG8ABflLYnZktqLqTWKYh3lfW/mdLyXwaPFwkeyE47E=; b=OtskPa1zE69L1hgjr8NdmGEFQEHAK2TZANJDgybwTujt1HdHjKDhmMz1XU9XIBep0e hcFr48/a1SrNGKlFwYB3ziubvSqOS6WtVWF7Kpn7wHgHMyIyHD4nRG51Agpte6cQAvk4 EAZ0W5irVHgkGrT8SbEsIQxBeyeQAKK7zAEoLTq4q0brqadrHQyG75dP0mBsbR9gUYhW AuQKOkzvIuzOrEfgC9tVAj4ksRxJjuIhnleY/ybHbNZsLXuVYsR2IZlzN9mfCyTuiG0B W693wWDQCa/MXXNYPV76HtEfi9nuPkcnwkQtWg4hAJ7Kf0S+81qYVtKWIEQNDI5hZu4G gnmg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=JYv3wWWf; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-57652-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-57652-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=2; AJvYcCWWFWdB6Lzc/X3JOrzqtSboNFISIJ406B7vs6gFsIxXYcUF1A+2kFequAq87k9tQT+odCxLSSWM2wiLPlGVmz48CVk9WA== Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id di2-20020ad458e2000000b0068c405e6bc6si2868381qvb.271.2024.02.08.00.49.37 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Feb 2024 00:49:37 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-57652-ouuuleilei=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=@kernel.org header.s=k20201202 header.b=JYv3wWWf; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-57652-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-57652-ouuuleilei=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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 5CF951C24ECA for <ouuuleilei@gmail.com>; Thu, 8 Feb 2024 08:49:37 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3F35E6E2A8; Thu, 8 Feb 2024 08:45:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="JYv3wWWf" 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 7DC7371B4D; Thu, 8 Feb 2024 08:45:32 +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=1707381933; cv=none; b=mBW3B3d6pI8fMVCqMGsEJ1yCBoWlnRRpAhNhmUckW6SKupjNUijJkYQxxs39wFTbEFdyitu/mLXzWpCxKKlsk7uwoXybugGl/0xrnX8Rb2jRCG2hcjfldQEREL4NdFwbK4iihNJ4WiFjD9c+gpicmKEgotfOD9xJ2VLKEtRJx5g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707381933; c=relaxed/simple; bh=PbzPd7ROc5va5PvBniGi9yT6iZxcViGR7FsGtUiYZPQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Ax340BOOpImeQx4RMtE5KZ8gORbhvmtEyCceuXWAQwz3GV7QtcS30zAWS+bcRwBA5XxycrFrx15NGfQn+J5vBYHk+TDHnePeltwPi1rdUfYYr1ZqmmbiKz9P3eYhzaqEmtjPrdyiUiG2gb9k7RacFeGdEGDdUh2zy8MvBFhXLJw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=JYv3wWWf; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id ECCBEC43394; Thu, 8 Feb 2024 08:45:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1707381932; bh=PbzPd7ROc5va5PvBniGi9yT6iZxcViGR7FsGtUiYZPQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JYv3wWWfitgg/S4wiWCNHUkrl6YEq7UHvBTMANfUjrWE6exn3/m3eE4TUhkWPXN+Y rRCC2FVaAkQZqV1mHCuFnaHh1E4JzU8l38xR/iT/qXy3v/vrN2OxQVG3lrZhMJjvCx o4T+oxyev2+cxar+vWyjTi6fPg7ODFt8JIJFQLVx0HGBORpVmo99miUmNAWAEXz00J mMdCOBIQwXoI84M6/sfhzlFbrNwoOdvX9cGpijg9bbVq5vEsPeuulOXxkJeOp03YD5 OMN6+cLmOLws/InUorErs1VlC3zfGGKrspeOEalGaY6iN4L2ycp8QkTyggjGOJ6JbQ S9VvuxHfX7/Ug== From: Lee Jones <lee@kernel.org> To: lee@kernel.org Cc: linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, "James E.J. Bottomley" <jejb@linux.ibm.com>, "Martin K. Petersen" <martin.petersen@oracle.com>, linux-scsi@vger.kernel.org Subject: [PATCH 06/10] scsi: aha1542: Replace snprintf() with the safer scnprintf() variant Date: Thu, 8 Feb 2024 08:44:18 +0000 Message-ID: <20240208084512.3803250-7-lee@kernel.org> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog In-Reply-To: <20240208084512.3803250-1-lee@kernel.org> References: <20240208084512.3803250-1-lee@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: <linux-kernel.vger.kernel.org> List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org> List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790319974127765906 X-GMAIL-MSGID: 1790319974127765906 |
Series |
scsi: Replace {v}snprintf() variants with safer alternatives
|
|
Commit Message
Lee Jones
Feb. 8, 2024, 8:44 a.m. UTC
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.
Link: https://lwn.net/Articles/69419/
Link: https://github.com/KSPP/linux/issues/105
Signed-off-by: Lee Jones <lee@kernel.org>
---
Cc: "James E.J. Bottomley" <jejb@linux.ibm.com>
Cc: "Martin K. Petersen" <martin.petersen@oracle.com>
Cc: linux-scsi@vger.kernel.org
---
drivers/scsi/aha1542.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
Comments
On Thu, Feb 08, 2024 at 08:44:18AM +0000, Lee Jones wrote: > 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. It may be better to do a global replace of snprintf() where the return value is not used. $ git grep '\bsnprintf\b' | grep -v = | wc -l 6400 There's plenty of work to do to remove just the ones taking the result: $ git grep ' = snprintf\b' | wc -l 764 Regardless, Reviewed-by: Kees Cook <keescook@chromium.org> -Kees > > Link: https://lwn.net/Articles/69419/ > Link: https://github.com/KSPP/linux/issues/105 > Signed-off-by: Lee Jones <lee@kernel.org> > --- > Cc: "James E.J. Bottomley" <jejb@linux.ibm.com> > Cc: "Martin K. Petersen" <martin.petersen@oracle.com> > Cc: linux-scsi@vger.kernel.org > --- > drivers/scsi/aha1542.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/drivers/scsi/aha1542.c b/drivers/scsi/aha1542.c > index 9503996c63256..b5ec7887801a5 100644 > --- a/drivers/scsi/aha1542.c > +++ b/drivers/scsi/aha1542.c > @@ -772,7 +772,7 @@ static struct Scsi_Host *aha1542_hw_init(const struct scsi_host_template *tpnt, > goto unregister; > > if (sh->dma_channel != 0xFF) > - snprintf(dma_info, sizeof(dma_info), "DMA %d", sh->dma_channel); > + scnprintf(dma_info, sizeof(dma_info), "DMA %d", sh->dma_channel); > shost_printk(KERN_INFO, sh, "Adaptec AHA-1542 (SCSI-ID %d) at IO 0x%x, IRQ %d, %s\n", > sh->this_id, base_io, sh->irq, dma_info); > if (aha1542->bios_translation == BIOS_TRANSLATION_25563) > -- > 2.43.0.594.gd9cf4e227d-goog > >
diff --git a/drivers/scsi/aha1542.c b/drivers/scsi/aha1542.c index 9503996c63256..b5ec7887801a5 100644 --- a/drivers/scsi/aha1542.c +++ b/drivers/scsi/aha1542.c @@ -772,7 +772,7 @@ static struct Scsi_Host *aha1542_hw_init(const struct scsi_host_template *tpnt, goto unregister; if (sh->dma_channel != 0xFF) - snprintf(dma_info, sizeof(dma_info), "DMA %d", sh->dma_channel); + scnprintf(dma_info, sizeof(dma_info), "DMA %d", sh->dma_channel); shost_printk(KERN_INFO, sh, "Adaptec AHA-1542 (SCSI-ID %d) at IO 0x%x, IRQ %d, %s\n", sh->this_id, base_io, sh->irq, dma_info); if (aha1542->bios_translation == BIOS_TRANSLATION_25563)