Message ID | 20231213164246.1021885-5-lee@kernel.org |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:3b04:b0:fb:cd0c:d3e with SMTP id c4csp7908279dys; Wed, 13 Dec 2023 08:43:21 -0800 (PST) X-Google-Smtp-Source: AGHT+IGbD49rbmtp7IWnyQwoyVQh3r3Yiw3yOI3XHALgxNkq5I4kS9TIricxVVqruAYVylEKRLG+ X-Received: by 2002:a05:6a00:14d4:b0:6b2:7a88:7128 with SMTP id w20-20020a056a0014d400b006b27a887128mr5195969pfu.22.1702485800991; Wed, 13 Dec 2023 08:43:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702485800; cv=none; d=google.com; s=arc-20160816; b=F3AenoXTm1V94+79IsbOVP0c9rlY//LvINkwfL/wTweocDtL0oajspRxxfVXeevKib oTFzEYwfW+wmKcmppcqT5BWxo3fKi22Cemuxz1bLf/rb9KTCipu5mhw5WUS6n3Y3RI7R eIxngxcs32CgODU41HDmr1mLb7HLAdR05mnRBEE1rPOy4cH9nqabytuw5DNa0RC3mCIi nuyE6Db5+NnD8ZGNJZXT2pjvkJqV3ve8iUbvbiwUFAfi4VqJSx6OSsvHCcoR/hztThn9 Ik0p1A8i3eNAqsFdjcxOa+S0j0rTQTChb3wFnVh2IOr2sVprZ/7m41ZzgKFBte4W10xC CtKQ== 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=fMcPMJrT/gMHPT69xmloavxCCY2S7vDlNBCDxAkv9l0=; fh=S9RrJvYQpLBCVFAXnFS3lZeOY4lSXgNmcB2eiPUvOZM=; b=yEBdPRjw7TbA1LJ7sTvBFJw3ZgM6Dm5U0JZUJcXJtYGhMJzmfJF1pigbcyFVDA3btl r0LiDVqT706zhqeOpuSHNV9QHgumkTeffoYNT72j6jEnFRV3Zo6MP/wnbc9LF9OxYkiR 4QBmNfYODfsg+RUOacXqTOwRc6ucpdMU61STYvFL/vHxsYxuMPs9Q8Y5qP3pzdQn5zw8 cdiH9n8l8JVQ0U7AE0kXvGh+8RRChmAP1PcyKm500FQd7H/cOUgdhIpdo7To98tlBNVX L4ePwzSvpHtNnhpXNK2wdtt1L8oAX6Zi5w8OdtM+90J4EQHc2UDRndkW3JY3xYiUbf7F ewfg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=gykg4kIB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id fh30-20020a056a00391e00b006cefb79b3adsi7150467pfb.175.2023.12.13.08.43.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Dec 2023 08:43:20 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=gykg4kIB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 snail.vger.email (Postfix) with ESMTP id 18C44802FD04; Wed, 13 Dec 2023 08:43:20 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233625AbjLMQnH (ORCPT <rfc822;dexuan.linux@gmail.com> + 99 others); Wed, 13 Dec 2023 11:43:07 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55900 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235321AbjLMQnD (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Wed, 13 Dec 2023 11:43:03 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 79FB7F4 for <linux-kernel@vger.kernel.org>; Wed, 13 Dec 2023 08:43:05 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7DDE1C433C7; Wed, 13 Dec 2023 16:43:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1702485785; bh=iLYjIbUlVCLTe0kMorEK0VE5bRrv64V6utYWmUc7b34=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gykg4kIB0fJzVUy7m3W/GPqLEYkG+uUFedlst3B6F+OV8ZdO3lVqs7eJpYz7fLtaz G/PZ7imQUxBO1VJ0b1NQApfsPHV0rWqwI/JsbEcVI+6JjbBhi4M2/9yIeTPOcvvzpG Thx+2ud4DU3x7X6U2hxwf0ISktRgf7pkKHQMQnt7HCWlyAAmneXrFyQA8Fb0VnEx+p hnS0cJRhQ+abCt4i2dAhQMhuMTGdlm94dA5ZScOQNDX7J/Dm3belaMi5lvhkEeHzfa kWQf7LbFACW14jelerfw9Hl3O6YA4UbwmfnOi/9HQFIp+bSB3IzyoNQ9TS4MERp7X+ tI1w4j7iumgrw== From: Lee Jones <lee@kernel.org> To: lee@kernel.org, gregkh@linuxfoundation.org Cc: linux-kernel@vger.kernel.org, linux-usb@vger.kernel.org, Laurent Pinchart <laurent.pinchart@ideasonboard.com>, Daniel Scally <dan.scally@ideasonboard.com>, Andrzej Pietrasiewicz <andrzejtp2010@gmail.com> Subject: [PATCH 04/12] usb: gadget: uvc: Replace snprintf() with the safer scnprintf() variant Date: Wed, 13 Dec 2023 16:42:33 +0000 Message-ID: <20231213164246.1021885-5-lee@kernel.org> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog In-Reply-To: <20231213164246.1021885-1-lee@kernel.org> References: <20231213164246.1021885-1-lee@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Wed, 13 Dec 2023 08:43:20 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785185751470955736 X-GMAIL-MSGID: 1785185751470955736 |
Series |
usb: Replace {v}snprintf() variants with safer alternatives
|
|
Commit Message
Lee Jones
Dec. 13, 2023, 4:42 p.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
Cc: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Cc: Daniel Scally <dan.scally@ideasonboard.com>
Cc: Andrzej Pietrasiewicz <andrzejtp2010@gmail.com>
Signed-off-by: Lee Jones <lee@kernel.org>
---
drivers/usb/gadget/function/uvc_configfs.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
Comments
Hi Lee, Thank you for the patch. On Wed, Dec 13, 2023 at 04:42:33PM +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. > > Link: https://lwn.net/Articles/69419/ > Link: https://github.com/KSPP/linux/issues/105 > Cc: Laurent Pinchart <laurent.pinchart@ideasonboard.com> > Cc: Daniel Scally <dan.scally@ideasonboard.com> > Cc: Andrzej Pietrasiewicz <andrzejtp2010@gmail.com> > Signed-off-by: Lee Jones <lee@kernel.org> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> > --- > drivers/usb/gadget/function/uvc_configfs.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/drivers/usb/gadget/function/uvc_configfs.c b/drivers/usb/gadget/function/uvc_configfs.c > index 9bf0e985acfab..7e704b2bcfd1c 100644 > --- a/drivers/usb/gadget/function/uvc_configfs.c > +++ b/drivers/usb/gadget/function/uvc_configfs.c > @@ -3414,7 +3414,7 @@ static ssize_t f_uvc_opts_string_##cname##_show(struct config_item *item,\ > int result; \ > \ > mutex_lock(&opts->lock); \ > - result = snprintf(page, sizeof(opts->aname), "%s", opts->aname);\ > + result = scnprintf(page, sizeof(opts->aname), "%s", opts->aname);\ > mutex_unlock(&opts->lock); \ > \ > return result; \
diff --git a/drivers/usb/gadget/function/uvc_configfs.c b/drivers/usb/gadget/function/uvc_configfs.c index 9bf0e985acfab..7e704b2bcfd1c 100644 --- a/drivers/usb/gadget/function/uvc_configfs.c +++ b/drivers/usb/gadget/function/uvc_configfs.c @@ -3414,7 +3414,7 @@ static ssize_t f_uvc_opts_string_##cname##_show(struct config_item *item,\ int result; \ \ mutex_lock(&opts->lock); \ - result = snprintf(page, sizeof(opts->aname), "%s", opts->aname);\ + result = scnprintf(page, sizeof(opts->aname), "%s", opts->aname);\ mutex_unlock(&opts->lock); \ \ return result; \