Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp214192rdb; Thu, 30 Nov 2023 02:55:38 -0800 (PST) X-Google-Smtp-Source: AGHT+IFV3IaWZeCIltyzG69qlC7gb2XvIv6lHa4UgINwq24n8EkMQNzIRxnTHSxAa0ZbK8eg1OrS X-Received: by 2002:a92:c26e:0:b0:35c:baec:7517 with SMTP id h14-20020a92c26e000000b0035cbaec7517mr14884202ild.0.1701341738013; Thu, 30 Nov 2023 02:55:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701341737; cv=none; d=google.com; s=arc-20160816; b=zrQAG7u/QUe3UgX7FZbnaz522UleVfQxBpZ5nQ750xayO36PEMtK0ime3BqRf/HDDq /ZgJO+CnwIheRE3h1ofYxEKYPLiZRQXaPd4rNfKprU9XpAATajZkK2hWKbLxfspG5SM1 ZCYR2lCrKx/iB0BdZbPKR1EfHGWjtr073VPevGHGD981G4FySNmyydpOkzfTDB40rmNm 2XJ+sMV6YB2iLquhZIZ4jLQWksCA5qZeywbYoowi0CZikoSfMUzEnlie0WyvY5syjAcJ FE1N0h53khVdXaLdGKaA9IJwF7yDkfCUYSB8JeSCKWv9apyKjTJ0pEtcknWdPb5T0J91 Lycw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=erMdf6qSi/CsbMklKmqwkvI8eHPHmzna5cn0APZzXfU=; fh=2PoOdWNVrOZRRAvHRfhr0U8Dj+yqCU2JYFKzeVg1R0Q=; b=b2KqWm6/rS5sRbk8OPkwqr+ZAlpFGi5HgGrhwZJJEM5zuBJd19xkP7Hg2EfQvsKS0F WK4EamFO15QWfLfhbVDBRwru/YS8R4rqFbXPVDlf/q9IPD2kl2cwVpzXiek0to/BsBq6 oKJGSml/aoxjMEFzoXioUpOftPDRBW4ms3e5jdpqwixHkuuMR8zTwIFUldGw0R8GOfQK O6vgN3fcviv4r1NFN1Li81cSwyXigYCUL8mry+HA2rlkwzHCGcRNTysm4Mu9C14fPywK Uw6Tu6MV5BSu2DzEzCedvfHdRimFXAAr7LXCo0DvBz7/6A7G1uXvkhe6pBkIi9AtHoAY ZzMA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ODfZqyvs; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id bn1-20020a056a02030100b005b8f61fcba6si1158645pgb.452.2023.11.30.02.55.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Nov 2023 02:55:37 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ODfZqyvs; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 50F418080ED2; Thu, 30 Nov 2023 02:55:34 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345129AbjK3KzQ (ORCPT + 99 others); Thu, 30 Nov 2023 05:55:16 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57796 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345051AbjK3KzL (ORCPT ); Thu, 30 Nov 2023 05:55:11 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8DB8910DB for ; Thu, 30 Nov 2023 02:55:17 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 83373C433C8; Thu, 30 Nov 2023 10:55:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1701341717; bh=I6SFA46IuAEaiFe3yLVvcqPKp8gUZZ0H2L9FiYb6dRg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ODfZqyvsllTcjm4eCiviYTFS9NmmCY4xpKqBkTrGWQ11pzC3k/u7EJKu7gnow+HLD W85Lbo+2NpiarQYU15HIEFe4/VpooV82LNHT4T9ODoCqfOttbCST6L/OSP16CMi47P D+sk//y9F+L+Inr5RB/ovdaJyIlJfU5yN6DUOz88/QUD3IIxNIhG/5yX88mlmgIjOg I2myg9l9zz2tuaunyntzJ4TYi8zqlQxUW13hi8gVKk1P49u90TmebAhmgDvRiKKX4l +1tDauDAmREIua845WZ7QZYEAtWrm39g+qxKLVH9XDYZyfhYX7641iMDAl8yFp4ky6 bMvVtb+cWhNlA== From: Lee Jones To: lee@kernel.org Cc: linux-kernel@vger.kernel.org, Linus Walleij , Greg Kroah-Hartman , Yuan-Hsin Chen , Feng-Hsin Chiang , Po-Yu Chuang , linux-usb@vger.kernel.org Subject: [PATCH 3/5] usb: fotg210-hcd: Replace snprintf() with the safer scnprintf() variant Date: Thu, 30 Nov 2023 10:54:37 +0000 Message-ID: <20231130105459.3208986-4-lee@kernel.org> X-Mailer: git-send-email 2.43.0.rc1.413.gea7ed67945-goog In-Reply-To: <20231130105459.3208986-1-lee@kernel.org> References: <20231130105459.3208986-1-lee@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Thu, 30 Nov 2023 02:55:34 -0800 (PST) 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. The uses in this file both seem to assume that data *has been* written! Link: https://lwn.net/Articles/69419/ Link: https://github.com/KSPP/linux/issues/105 Cc: Linus Walleij Cc: Greg Kroah-Hartman Cc: Yuan-Hsin Chen Cc: Feng-Hsin Chiang Cc: Po-Yu Chuang Cc: linux-usb@vger.kernel.org Signed-off-by: Lee Jones --- drivers/usb/fotg210/fotg210-hcd.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/drivers/usb/fotg210/fotg210-hcd.c b/drivers/usb/fotg210/fotg210-hcd.c index 929106c16b29b..b2f8b53cc8ef5 100644 --- a/drivers/usb/fotg210/fotg210-hcd.c +++ b/drivers/usb/fotg210/fotg210-hcd.c @@ -404,9 +404,9 @@ static void qh_lines(struct fotg210_hcd *fotg210, struct fotg210_qh *qh, else if (td->hw_alt_next != list_end) mark = '/'; } - temp = snprintf(next, size, - "\n\t%p%c%s len=%d %08x urb %p", - td, mark, ({ char *tmp; + temp = scnprintf(next, size, + "\n\t%p%c%s len=%d %08x urb %p", + td, mark, ({ char *tmp; switch ((scratch>>8)&0x03) { case 0: tmp = "out"; @@ -424,17 +424,13 @@ static void qh_lines(struct fotg210_hcd *fotg210, struct fotg210_qh *qh, (scratch >> 16) & 0x7fff, scratch, td->urb); - if (size < temp) - temp = size; size -= temp; next += temp; if (temp == size) goto done; } - temp = snprintf(next, size, "\n"); - if (size < temp) - temp = size; + temp = scnprintf(next, size, "\n"); size -= temp; next += temp; -- 2.43.0.rc1.413.gea7ed67945-goog