From nobody Fri Dec 19 05:42:23 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5D7B3C4167D for ; Wed, 13 Dec 2023 16:43:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1379408AbjLMQnf (ORCPT ); Wed, 13 Dec 2023 11:43:35 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56222 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377827AbjLMQnL (ORCPT ); Wed, 13 Dec 2023 11:43:11 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9447F120 for ; Wed, 13 Dec 2023 08:43:16 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id F38E4C433CB; Wed, 13 Dec 2023 16:43:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1702485796; bh=/VgfUvsmvX2n019MxXLrjKmvUFShp1H3MaszHsAPHGk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=XsBXlv5DeBhXUIpcPuRuzSfg8SfGeHati6aAXIoHbRuo9LG+Q0hQq9DwpNqR5erpU 1bZAemhQZ6c0x1GGAjt+lCl0lAouPl3cx+lmaBEONDONKH8hfJWdy2hdycCC/rs+H+ 4bHr8O1sItmqYGRbqHcIjQundVi/ie3WWHbOQXATp3WoH7wraD7/o3wS71+qjUBdue QcXqX5sN1aoPYitYQN+lqR9tMtWuq7I65TDfDkgWhq2GlcxRTfk1l8ywfHZmGrh9qY iqwVKRqva9DnVXB4Nsomot0VsTMY2oV/ivfmHUv+s5qNVvPUpNvON73Pzosf9dk57q h1PRp1wcEK3Ew== From: Lee Jones To: lee@kernel.org, gregkh@linuxfoundation.org Cc: linux-kernel@vger.kernel.org, linux-usb@vger.kernel.org Subject: [PATCH 10/12] usb: mon_text: Replace snprintf() with the safer scnprintf() variant Date: Wed, 13 Dec 2023 16:42:39 +0000 Message-ID: <20231213164246.1021885-11-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: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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 --- drivers/usb/mon/mon_text.c | 28 +++++----------------------- 1 file changed, 5 insertions(+), 23 deletions(-) diff --git a/drivers/usb/mon/mon_text.c b/drivers/usb/mon/mon_text.c index 39cb141646526..2fe9b95bac1d5 100644 --- a/drivers/usb/mon/mon_text.c +++ b/drivers/usb/mon/mon_text.c @@ -352,7 +352,7 @@ static int mon_text_open(struct inode *inode, struct fi= le *file) rp->r.rnf_error =3D mon_text_error; rp->r.rnf_complete =3D mon_text_complete; =20 - snprintf(rp->slab_name, SLAB_NAME_SZ, "mon_text_%p", rp); + scnprintf(rp->slab_name, SLAB_NAME_SZ, "mon_text_%p", rp); rp->e_slab =3D kmem_cache_create(rp->slab_name, sizeof(struct mon_event_text), sizeof(long), 0, mon_text_ctor); @@ -700,46 +700,28 @@ static const struct file_operations mon_fops_text_u = =3D { =20 int mon_text_add(struct mon_bus *mbus, const struct usb_bus *ubus) { - enum { NAMESZ =3D 10 }; + enum { NAMESZ =3D 12 }; char name[NAMESZ]; int busnum =3D ubus? ubus->busnum: 0; - int rc; =20 if (mon_dir =3D=3D NULL) return 0; =20 if (ubus !=3D NULL) { - rc =3D snprintf(name, NAMESZ, "%dt", busnum); - if (rc <=3D 0 || rc >=3D NAMESZ) - goto err_print_t; + scnprintf(name, NAMESZ, "%dt", busnum); mbus->dent_t =3D debugfs_create_file(name, 0600, mon_dir, mbus, &mon_fops_text_t); } =20 - rc =3D snprintf(name, NAMESZ, "%du", busnum); - if (rc <=3D 0 || rc >=3D NAMESZ) - goto err_print_u; + scnprintf(name, NAMESZ, "%du", busnum); mbus->dent_u =3D debugfs_create_file(name, 0600, mon_dir, mbus, &mon_fops_text_u); =20 - rc =3D snprintf(name, NAMESZ, "%ds", busnum); - if (rc <=3D 0 || rc >=3D NAMESZ) - goto err_print_s; + scnprintf(name, NAMESZ, "%ds", busnum); mbus->dent_s =3D debugfs_create_file(name, 0600, mon_dir, mbus, &mon_fops_stat); =20 return 1; - -err_print_s: - debugfs_remove(mbus->dent_u); - mbus->dent_u =3D NULL; -err_print_u: - if (ubus !=3D NULL) { - debugfs_remove(mbus->dent_t); - mbus->dent_t =3D NULL; - } -err_print_t: - return 0; } =20 void mon_text_del(struct mon_bus *mbus) --=20 2.43.0.472.g3155946c3a-goog