Received: by 2002:a05:7412:3b8b:b0:fc:a2b0:25d7 with SMTP id nd11csp1386457rdb; Fri, 9 Feb 2024 23:06:24 -0800 (PST) X-Google-Smtp-Source: AGHT+IGzPoT6JRSAjpXwgzizjB7I/7U35bDhMF+Kz44NbdyYoe9NMcdE4/dc0CN01JJMu8nNsuOH X-Received: by 2002:a05:6808:189e:b0:3c0:2b4d:fb6d with SMTP id bi30-20020a056808189e00b003c02b4dfb6dmr1217435oib.15.1707548783800; Fri, 09 Feb 2024 23:06:23 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707548783; cv=pass; d=google.com; s=arc-20160816; b=X32Rk1RvBIHCxP2OISeBu11MQwvuv3NWj/SmmETeiXoErWnzRmp9638IAmPoFzeq/8 x8hk4KvcOwsMHJ6T/EdVV142JiDUxBdlsIAVX4s4y0Lf1S/p7vfiXvTEr35VA+AhV5Mf 2agDn5/TXjQ8+/dpB3ucWHu2NawKSK6gsxj3Y+SwJcWwwQ6wNS0S3u8Q2yXgVCKV/VRc KtvL+iJnnixDE8GvegJL7ziwOAca9+KnrvhVn3wwDkWTcYn2NBalbjzQFwqXit6RKWKZ 0tIhlA/H2MlOri0JZPFcffppSrfSY8orQGJQnpDJiQfkthZzTf3WECOUz7/yuOPq7XZd tkGw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=em67GxBbJuke46FxIVNprDBwCwRfGg+vWznk7EN7Egw=; fh=tepYqIILrl5SKXDNPTmn7lKUN17y560Q5LOhejaRkxY=; b=lERf3XTWdPu/j7AAGnuGJXNN0BhZwi7QH5qjGScBJ6nd2eo8oJB/2xJdWTwsurdtmB fKiTW8v9iicaZSecanld/3pxphOp+364f/c3LWpeqR0oiskOY3gAvm8qymWm8mvBWh7o zaFQQtzblbu5eEDYFL7c0Ll/Iq+128Cqq/CHYGuGfSFoR8TkJpT6jer1sVXsELNWPFjA QqV4Z2vbj8y19ObtqgT6jX0CNFVDDBRxhVujFwAkGKS+iZZa6dxK0EzufXxs+sngAIR1 h4q51rH7GxRunsA+i08ZDV0+cIpOReWNCoooKoKgLzNk8q5P5QvSYl2AiPvSLXnWL0J7 WsmA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=kKyHYZ5e; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-60303-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-60303-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org X-Forwarded-Encrypted: i=2; AJvYcCUHRnfNX1iqofQd+0WQZoNpsGTk/5bt+3B074ix7ovAXr3h1J3l+vh44ttsH2B7495i1BfIGiwSf5GlxUkoLf+sGw53fZMIr/fGjnFdWQ== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id g5-20020a05620a13c500b00783ab755e57si1186760qkl.429.2024.02.09.23.06.23 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Feb 2024 23:06:23 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-60303-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=@chromium.org header.s=google header.b=kKyHYZ5e; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-60303-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-60303-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.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 7CFDA1C21B7D for ; Sat, 10 Feb 2024 07:06:23 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id ECA4726AD9; Sat, 10 Feb 2024 07:06:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="kKyHYZ5e" Received: from mail-pf1-f175.google.com (mail-pf1-f175.google.com [209.85.210.175]) (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 AE2E524214 for ; Sat, 10 Feb 2024 07:06:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.175 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707548774; cv=none; b=gXy3QrQUeYaCJ+3+uS3HiULNGqV82NWqlJ0SmX4/uw+xQGpPc8+vfTKs7WKzz0nFmT8Q2adRf0NpSC6jiXtdsaMO4RFN6P6+8OiOhKVht3ewizPaDcvCDN6QOh1G6oZSYDjxITa/mqh+Ql10tgMqJdybgqnnNlEgVEnJhcnuLGc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707548774; c=relaxed/simple; bh=u5aSZ75Pq8bWKdAw+W5D98hsJd3DcnLzNDzyk+f2erw=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=o6xOYEpK9xEaevlW/AYi8TfVRp13Uus/Wg7Diu77sN3nqDDBQ1V1KVdgHrP11jXoDUJDS/aCwH1h7tW/O4j/AqDLnFcDwEjZGuNblTgo1Dapi+0N66WvndfOIY7Q7/Tx9R5y3p7MUnU9fd9PjhtEC8ZuYo5NhlzOW+bqBtqt7PI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=kKyHYZ5e; arc=none smtp.client-ip=209.85.210.175 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Received: by mail-pf1-f175.google.com with SMTP id d2e1a72fcca58-6e04eceebb5so1247045b3a.1 for ; Fri, 09 Feb 2024 23:06:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1707548772; x=1708153572; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=em67GxBbJuke46FxIVNprDBwCwRfGg+vWznk7EN7Egw=; b=kKyHYZ5eeIdA83nCjq0dmer32Bb/zO2dl9RBubi5d8+1t0CaUxGnvTKtL04UeBF/9n LvsSqnA2KLQpZx3tBC+UhibOX+/FsckbdH0/ETJPp8NwHlclBhJ+qyyO7p1tc+ZTfM4i YHB900v77Q5hgYZtbVzdXnNYqhwT/b+P6mT88= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707548772; x=1708153572; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=em67GxBbJuke46FxIVNprDBwCwRfGg+vWznk7EN7Egw=; b=FopzxF6Uks1xa0mscST5UKk9l9zGMd8BuOnQZ9+Ol7fpFP0HfSlKo7pb5md5i/MBm5 DhnfgY2CdFdvYmZIepTc+fWclwbNmfHEvbW2eS90l5/FIMU1Y/qNISWJ3hb5TNhBsneU PCLseoQH/BZqqlQLMBKDRDn7jDerJ3aJ1Z5A23sQXfauKPNmGEeXv+0UzKYfHRuN6rCy ivyaicRmFYL3mPRWP4ef5mYadi6Wz0yfYiWpa0ONivofAeEqdAcZnjWNzDrkcABXdVvU odShLA9PJ17WPC5K60J6bzuDrhClrevJVmQZna7LHWON23SS3vA6AWfurlpTsXCln1Gy gepQ== X-Gm-Message-State: AOJu0YzcEdO97T22XXHFHlw3e7L8PQp6x3S6Xk0ZLKarIvVRjS7dLQpC sMlbfzjtUVEjY5GsT7nFioRMocj2I/Z28JyR6WJl+xmh9SJy1ziK/j26VlL0dw== X-Received: by 2002:aa7:8697:0:b0:6d9:b4e6:ffb with SMTP id d23-20020aa78697000000b006d9b4e60ffbmr1567996pfo.0.1707548771984; Fri, 09 Feb 2024 23:06:11 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCUG+I9XVE2iar9wTcJDEpMQSTKjIsjT/JLUzg4t3u+93v91KJ4RDRW4qfBv0vgt5g3FgmiCG6Xf+QLvPa251j46JgK1Fpvcp6haSZHhuYtc+QKc9lP/5kMxWBf6DopW9P914Hkl4qU1oJwbvi0KetwpMnfYMUqVNFGHMsrJgt2ra6HN3qSofJDCDzfMrtbSDSkSEZPbGEF41kCZ1Q== Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id m8-20020aa78a08000000b006e05cb7fd1fsm1618491pfa.164.2024.02.09.23.06.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Feb 2024 23:06:11 -0800 (PST) Date: Fri, 9 Feb 2024 23:06:10 -0800 From: Kees Cook To: Lee Jones Cc: linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, "James E.J. Bottomley" , "Martin K. Petersen" , linux-scsi@vger.kernel.org Subject: Re: [PATCH 06/10] scsi: aha1542: Replace snprintf() with the safer scnprintf() variant Message-ID: <202402092303.F5F31A17@keescook> References: <20240208084512.3803250-1-lee@kernel.org> <20240208084512.3803250-7-lee@kernel.org> 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=us-ascii Content-Disposition: inline In-Reply-To: <20240208084512.3803250-7-lee@kernel.org> 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 -Kees > > Link: https://lwn.net/Articles/69419/ > Link: https://github.com/KSPP/linux/issues/105 > Signed-off-by: Lee Jones > --- > Cc: "James E.J. Bottomley" > Cc: "Martin K. Petersen" > 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 > > -- Kees Cook