From: Jan Vesely Subject: Re: [PATCH libdrm] xf86drm: fix valgrind warning in drmParsePciBusInfo Date: Fri, 13 Jan 2017 06:34:39 -0500 Message-ID: <1484307279.8721.2.camel@rutgers.edu> References: <90effe53-ad38-3b45-ca16-029269a406b4@gmail.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="===============0576363549==" Return-path: Received: from mail-qk0-x243.google.com (mail-qk0-x243.google.com [IPv6:2607:f8b0:400d:c09::243]) by gabe.freedesktop.org (Postfix) with ESMTPS id BB8E76EDCE for ; Fri, 13 Jan 2017 11:35:10 +0000 (UTC) Received: by mail-qk0-x243.google.com with SMTP id e1so6658298qkh.1 for ; Fri, 13 Jan 2017 03:35:10 -0800 (PST) In-Reply-To: <90effe53-ad38-3b45-ca16-029269a406b4@gmail.com> List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" To: Nicholas Miell , dri-devel@lists.freedesktop.org List-Id: dri-devel@lists.freedesktop.org --===============0576363549== Content-Type: multipart/signed; micalg="pgp-sha256"; protocol="application/pgp-signature"; boundary="=-/ArQwV5pIClzStTHcnc3" --=-/ArQwV5pIClzStTHcnc3 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Thu, 2017-01-12 at 18:16 -0800, Nicholas Miell wrote: > From 714d07f600db39498c87d7816f4dd3a7e6d9bbca Mon Sep 17 00:00:00 2001 > From: Nicholas Miell > Date: Thu, 12 Jan 2017 15:43:07 -0800 > Subject: [PATCH libdrm] xf86drm: fix valgrind warning in drmParsePciBusIn= fo >=20 > The current implementation reads (up to) 513 bytes, overwrites the 513th > byte with '\0' and then passes the buffer off to strstr() and sscanf() > without ever initializing the middle bytes. This causes valgrind > warnings and potentially fails to parse PCI_SLOT_NAME if the uevent is > unexpectedly large. a simpler fix should also get rid of the valgrind warning: - ret =3D read(fd, data, sizeof(data)); - data[sizeof(data)-1] =3D '\0'; + ret =3D read(fd, data, sizeof(data) - 1); close(fd); if (ret < 0) return -errno + data[ret] =3D '\0'; I think that dynamic memory allocation is still a more robust approach. regards, Jan >=20 > Rewrite it using getline() to fix the valgrind errors and future-proof > the function against uevent files larger than 513 bytes. >=20 > Signed-off-by: Nicholas Miell > --- > xf86drm.c | 34 +++++++++++++++++----------------- > 1 file changed, 17 insertions(+), 17 deletions(-) >=20 > diff --git a/xf86drm.c b/xf86drm.c > index b8b2cfe..33261ac 100644 > --- a/xf86drm.c > +++ b/xf86drm.c > @@ -2919,31 +2919,31 @@ static int drmParsePciBusInfo(int maj, int min, d= rmPciBusInfoPtr info) > { > #ifdef __linux__ > char path[PATH_MAX + 1]; > - char data[512 + 1]; > - char *str; > + FILE *uevent; > + char *line =3D NULL; > + size_t n =3D 0; > + bool found =3D false; > int domain, bus, dev, func; > - int fd, ret; > =20 > snprintf(path, PATH_MAX, "/sys/dev/char/%d:%d/device/uevent", maj, m= in); > - fd =3D open(path, O_RDONLY); > - if (fd < 0) > + uevent =3D fopen(path, "r"); > + if (uevent =3D=3D NULL) > return -errno; > =20 > - ret =3D read(fd, data, sizeof(data)); > - data[sizeof(data)-1] =3D '\0'; > - close(fd); > - if (ret < 0) > - return -errno; > + while (getline(&line, &n, uevent) !=3D -1) { > + if (sscanf(line, "PCI_SLOT_NAME=3D%04x:%02x:%02x.%1u", > + &domain, &bus, &dev, &func) =3D=3D 4) > + { > + found =3D true; > + break; > + } > + } > + free(line); > =20 > -#define TAG "PCI_SLOT_NAME=3D" > - str =3D strstr(data, TAG); > - if (str =3D=3D NULL) > - return -EINVAL; > + fclose(uevent); > =20 > - if (sscanf(str, TAG "%04x:%02x:%02x.%1u", > - &domain, &bus, &dev, &func) !=3D 4) > + if (!found) > return -EINVAL; > -#undef TAG > =20 > info->domain =3D domain; > info->bus =3D bus; --=20 Jan Vesely --=-/ArQwV5pIClzStTHcnc3 Content-Type: application/pgp-signature; name="signature.asc" Content-Description: This is a digitally signed message part Content-Transfer-Encoding: 7bit -----BEGIN PGP SIGNATURE----- iQIcBAABCAAGBQJYeLtPAAoJEGPjP7c/SLIgXLAP/jeseMTEBMO6g8Ta1qE8mgVj nf874w13I8EXgJJ3nNPKUe2+OSUV2AJSkxvNb64OwcZOOAya8YLsYx6UjKhMlj4u P9EqpR2hwWbLj2Bs3M8kSVcx8kruFOyWik4NWc5b+VbsXVOdFg8IJJo13XDSFfAG nzeGaG8NGBUltKzZiJUDGH68FIz6nhsK3LwMPKcndZB7mwn7U60+/5O8x+bE9RWH PjV+RN2JeXHyjZJ+p3y2AJuJUysTG833besRV+EsKbgOOpXpFV19T07X67j2H/jA Z0RvFiMPYIgKllUVRVWaZtYebN2w1VRhG/QWqDJG3H+lcCb1feSgZRPpb2AAQOTk eqbNJKJZl0ZhI8vnRPZAXHOl3hFHCKPZXslzYSHDMQVccB8GvDpH5c/qE9tSl/bC wIBCyqGXm6yJT5a48SWhGvRmQCnX2pSSw6ZMKje3uhpXVJ6x8Ia5Z168SyBBCFJn l8LuqbcC9oGVf3FXxMF+rKfsTB/col4BMmQDuV4ENVW1J1erXqp87rcoenQqxpCG TWxH2sy515j6WYbcDk33VH7Bi2ua39BIz+40de3MyCgkR+PZAfoGRhmtn2MSmB4M 8qdmhPrK2VXOtkbjn24byCONA1XLiU0Y7+9pHFrPQS4ZNQphDIbJD1Yd+4Tv35jy nE4ABPfFwsGmuUsil27J =/Cwj -----END PGP SIGNATURE----- --=-/ArQwV5pIClzStTHcnc3-- --===============0576363549== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: base64 Content-Disposition: inline X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX18KZHJpLWRldmVs IG1haWxpbmcgbGlzdApkcmktZGV2ZWxAbGlzdHMuZnJlZWRlc2t0b3Aub3JnCmh0dHBzOi8vbGlz dHMuZnJlZWRlc2t0b3Aub3JnL21haWxtYW4vbGlzdGluZm8vZHJpLWRldmVsCg== --===============0576363549==--