Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.3 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3958AC10F27 for ; Wed, 11 Mar 2020 10:13:38 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 13FFB2071B for ; Wed, 11 Mar 2020 10:13:38 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 13FFB2071B Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=suse.de Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=dri-devel-bounces@lists.freedesktop.org Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 6E0996E84B; Wed, 11 Mar 2020 10:13:37 +0000 (UTC) Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) by gabe.freedesktop.org (Postfix) with ESMTPS id 465706E84B for ; Wed, 11 Mar 2020 10:13:36 +0000 (UTC) X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id CD9D3B05C; Wed, 11 Mar 2020 10:13:34 +0000 (UTC) Subject: Re: [PATCH] drm: sysfs: Use scnprintf() for avoiding potential buffer overflow To: Takashi Iwai References: <20200311073540.7108-1-tiwai@suse.de> From: Thomas Zimmermann Autocrypt: addr=tzimmermann@suse.de; keyd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WGKdDegUR5BkDfDg5O/FISymJBHx2Dyoklv5g4BzkgqTqwmaYzsl8UxZKvbaxq0zb ehDda8lvhFXodNFMAgTLJlLuDYOGLK2AwbrS3Sp0AEbkpdJBb44qVlGm5bApZouHeJ/+n+7r 12+lqdsAEQEAAYkBPAQYAQgAJhYhBHIX+6yM6c9jRKFo5WgNwR1TC3ojBQJbOdLgAhsMBQkD wmcAAAoJEGgNwR1TC3ojpfcIAInwP5OlcEKokTnHCiDTz4Ony4GnHRP2fXATQZCKxmu4AJY2 h9ifw9Nf2TjCZ6AMvC3thAN0rFDj55N9l4s1CpaDo4J+0fkrHuyNacnT206CeJV1E7NYntxU n+LSiRrOdywn6erjxRi9EYTVLCHcDhBEjKmFZfg4AM4GZMWX1lg0+eHbd5oL1as28WvvI/uI aMyV8RbyXot1r/8QLlWldU3NrTF5p7TMU2y3ZH2mf5suSKHAMtbE4jKJ8ZHFOo3GhLgjVrBW HE9JXO08xKkgD+w6v83+nomsEuf6C6LYrqY/tsZvyEX6zN8CtirPdPWu/VXNRYAl/lat7lSI 3H26qrE= Message-ID: Date: Wed, 11 Mar 2020 11:13:31 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.5.0 MIME-Version: 1.0 In-Reply-To: X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: dri-devel@lists.freedesktop.org Content-Type: multipart/mixed; boundary="===============0122819606==" Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --===============0122819606== Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="FmjS1A222AV09WFjDvQdyOo2KaqKAsQvT" This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --FmjS1A222AV09WFjDvQdyOo2KaqKAsQvT Content-Type: multipart/mixed; boundary="YztA9YFAUGtN77kDi7BjyxYAkhZbY2Zc4"; protected-headers="v1" From: Thomas Zimmermann To: Takashi Iwai Cc: Maarten Lankhorst , Maxime Ripard , dri-devel@lists.freedesktop.org Message-ID: Subject: Re: [PATCH] drm: sysfs: Use scnprintf() for avoiding potential buffer overflow References: <20200311073540.7108-1-tiwai@suse.de> In-Reply-To: --YztA9YFAUGtN77kDi7BjyxYAkhZbY2Zc4 Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: quoted-printable Hi Am 11.03.20 um 09:24 schrieb Takashi Iwai: > On Wed, 11 Mar 2020 09:10:56 +0100, > Thomas Zimmermann wrote: >> >> Hi Takashi >> >> Am 11.03.20 um 08:35 schrieb Takashi Iwai: >>> Since snprintf() returns the would-be-output size instead of the >>> actual output size, the succeeding calls may go beyond the given >>> buffer limit. Fix it by replacing with scnprintf(). >>> >>> Signed-off-by: Takashi Iwai >>> --- >>> drivers/gpu/drm/drm_sysfs.c | 2 +- >>> 1 file changed, 1 insertion(+), 1 deletion(-) >>> >>> diff --git a/drivers/gpu/drm/drm_sysfs.c b/drivers/gpu/drm/drm_sysfs.= c >>> index dd2bc85f43cc..9b3180e8c12f 100644 >>> --- a/drivers/gpu/drm/drm_sysfs.c >>> +++ b/drivers/gpu/drm/drm_sysfs.c >>> @@ -230,7 +230,7 @@ static ssize_t modes_show(struct device *device, >>> =20 >>> mutex_lock(&connector->dev->mode_config.mutex); >>> list_for_each_entry(mode, &connector->modes, head) { >>> - written +=3D snprintf(buf + written, PAGE_SIZE - written, "%s\n", >>> + written +=3D scnprintf(buf + written, PAGE_SIZE - written, "%s\n",= >>> mode->name); >>> } >>> mutex_unlock(&connector->dev->mode_config.mutex); >>> >> >> In drm_sysfs.c, there are more _show functions with calls to snprintf(= ) >> that could be replaced by scnprintf(). ATM they don't return the corre= ct >> length for output that exceeds PAGE_SIZE. since you're at it, you may >> replace them as well. >=20 > Well, the rest snprintf() calls are single calls and can't be over > PAGE_SIZE obviously. IOW, they could be rather replaced with > sprintf() instead, for a sake of simplicity.=20 Admittedly, none of these strings look as if they ever go beyond PAGE_SIZE, but sncprintf() is still a simple way of defensive programming here (and returns the correct value). >=20 >> But in any case >> >> Reviewed-by: Thomas Zimmermann >> >> for this patch. >> >> Do you want me to merge the patch into drm-misc-next? >=20 > Yes, please. OK, will do later today. Best regards Thomas >=20 >=20 > thanks, >=20 > Takashi >=20 --=20 Thomas Zimmermann Graphics Driver Developer SUSE Software Solutions Germany GmbH Maxfeldstr. 5, 90409 N=C3=BCrnberg, Germany (HRB 36809, AG N=C3=BCrnberg) Gesch=C3=A4ftsf=C3=BChrer: Felix Imend=C3=B6rffer --YztA9YFAUGtN77kDi7BjyxYAkhZbY2Zc4-- --FmjS1A222AV09WFjDvQdyOo2KaqKAsQvT Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- iQEzBAEBCAAdFiEEchf7rIzpz2NEoWjlaA3BHVMLeiMFAl5oucsACgkQaA3BHVML eiMKWwf/eM5yzbt4PMMYAcGt9JGxUzmBSXQdN/dYO71mzLPzhSnkYzp7raJG62xK /61udtL1kRAoV6U3mnkD9uXocb03b8zhVvNtieuDvdAQ7DXuiOND5dPCN8ZDPv/F rWmdBADcaRAQKnIa286hOCjXuwf473xyzPDnr7079uqluNOz6XZ/b3rnkqhywT/m GBytlUjj+DlSI3RTwQcikGqj7T/QBd1hwvZTRCSsVQuLBqnzd90qg4YvzTt7OzIY 4nNpHLnMS8x/ujcitahFbipoIS0BdaHUl8H5FrwBTrhvCjelthGi5XHchPvJJw8o enr6lEX5ZxvRguFEnEiW8+FMyAdJGw== =fIbv -----END PGP SIGNATURE----- --FmjS1A222AV09WFjDvQdyOo2KaqKAsQvT-- --===============0122819606== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ dri-devel mailing list dri-devel@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/dri-devel --===============0122819606==--