Received: by 2002:a25:e7d8:0:0:0:0:0 with SMTP id e207csp550831ybh; Thu, 12 Mar 2020 06:55:46 -0700 (PDT) X-Google-Smtp-Source: ADFU+vtPCDyYt/7ysxBXU12ggJTjIQQwtDOamK9eV80KqHmTim8bO4fNLA5IayXczDoABGnLp1ig X-Received: by 2002:a4a:a60d:: with SMTP id e13mr4087067oom.57.1584021346685; Thu, 12 Mar 2020 06:55:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1584021346; cv=none; d=google.com; s=arc-20160816; b=FI3++kPeRMHZ/T9Q6Rwl0LV/Wh39E4/Iajs4rdW2sFlaBhBSnmkpqU7maU4BTI7r0i p7nt+r6H+54UfcHU1unO1pu9TJtO542p8WwZaZlQ/8NB0VllDhgSXF2mb/F92BLvCWYA szOczg5WSk8qBsBAYAqBfTzWeaFZn/8IAx4+oMputIyN6PdylARu1HToRvIGgIYqOg/c 3KYDL2NXJjD14KYwjntrPliceC+Rd4OVh1Ey3vJSfBmToH09lPCZWpNNI4h/XOGQOFNk sGB1DKaXVkavHh8CXkKwoCZorQeC1c+K+cM5XvRih50JNY+HnwY0TnFnIZLBRhmMyBnN 1QeQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=YJCbAPM757c7WtP5729q/CvzsGeEr1ydJuKt4rhqYN0=; b=cv5Pi0udRerkFymCTf/8l9DBCYX5jLXFRg3aHpSpwidSCi3h8obpk37WZR4AhxLoSp YO4TjBxF9bn8BSw3XXDlB5DlZQhqOY/8wXsE+ZMHItMMGKeHoPp/o1TQ33p511j+vzoz Ud6a4oPUZa2JANhSwXDO36SEeZqsDEZwfYlZO6o/4FwMUCJ8kTZK0cgX41DHVWpdFrql to3wS9s5sbjQEnSPpRbWiRfjEgSKiYVr/Mfd8ESx1F9eH+Unqcqz/2YIKdcgB5Y5GJ0G zOtOqtNu+irVd7pBrXQafuKRZnNaG2D+lasLK4iRdiEteTGApL5rP4M6jNuPg1RtbOus yu0w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@baylibre-com.20150623.gappssmtp.com header.s=20150623 header.b=Wz9uTaIm; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y9si2997946oia.47.2020.03.12.06.55.34; Thu, 12 Mar 2020 06:55:46 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@baylibre-com.20150623.gappssmtp.com header.s=20150623 header.b=Wz9uTaIm; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727446AbgCLNy1 (ORCPT + 99 others); Thu, 12 Mar 2020 09:54:27 -0400 Received: from mail-qk1-f196.google.com ([209.85.222.196]:34500 "EHLO mail-qk1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726641AbgCLNy0 (ORCPT ); Thu, 12 Mar 2020 09:54:26 -0400 Received: by mail-qk1-f196.google.com with SMTP id f3so6269996qkh.1 for ; Thu, 12 Mar 2020 06:54:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=YJCbAPM757c7WtP5729q/CvzsGeEr1ydJuKt4rhqYN0=; b=Wz9uTaImPoBXmMxauB2gmqMyOC7ZHJIqwDg3dkJxuEtcOEk7VlKjUF7hgOsfXtoDcf j56kSjTCHlUXqEi+p4R2+8g6dT3gOl/SfmGOcvsTWoiIqnsO1gIjCd8nmxxSGGTU+gXg redRm1hKIc7Hw5YBy+xDo0LJcZwVYZbd3/gGlzuiEBUPTOsTKdPvowZBsC/atoHowoQL Wd6J4G7x2CQKp0qB8Q4ri+INEfgSNXrxafpiqxJXlFFMGhj7xVIBbfKyTOmvcpmFwtzH rJWugm3npmE/ewF3g+mT9QpKbmwqmK6NhDhKv8RUCMsY/qAWA4H2bHNlERC3rgkJlNnM +vxw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=YJCbAPM757c7WtP5729q/CvzsGeEr1ydJuKt4rhqYN0=; b=gy81wMNaJa/1wJMos/enNu8QRoePnxiDDBAw8qi6wWCEmOfkJVfE7lV+pY7X0oQtyf 6b0Poh+7PoE6bUyZkPwBly+a1PjASRnbZes2rnNMRslP+PiuUBvPH6hkGGV7olxjtS9w yuN5lyTpbgsgrk8peQE5mJHoeezBnI78MlAdaeA18wKTAP/mVLWG1qjOGCAxjy9BnNp4 eOYMFCJvKChOMLCd7zyUgzeBuVPJLvUriIT4ieIy8NfjoTo8EI9VUTlIZuclKiT2qabJ PWYx4Gt9/29nOD8UUlXRgskx9TrKSFzX6i5pOBYwFOzFy57KkSYJtYpiJ4g+KdXF1QYw cL8A== X-Gm-Message-State: ANhLgQ337vH8BXI8PJWTGwSWgl/VqiCmBZ19orHplm98P1cGeh66slW+ ZpKSm/tWgonNDuqKgFrlsfl6JPJXRIFgO5B25G/XBw== X-Received: by 2002:a05:620a:1362:: with SMTP id d2mr8052260qkl.120.1584021265259; Thu, 12 Mar 2020 06:54:25 -0700 (PDT) MIME-Version: 1.0 References: <20200312094008.1833929-1-gabravier@gmail.com> In-Reply-To: <20200312094008.1833929-1-gabravier@gmail.com> From: Bartosz Golaszewski Date: Thu, 12 Mar 2020 14:54:12 +0100 Message-ID: Subject: Re: [PATCH] gpio-hammer: Avoid potential overflow in main To: Gabriel Ravier Cc: Linus Walleij , linux-gpio , LKML Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org czw., 12 mar 2020 o 10:40 Gabriel Ravier napisa=C5=82= (a): > > If '-o' was used more than 64 times in a single invocation of gpio-hammer= , > this could lead to an overflow of the 'lines' array. This commit fixes > this by avoiding the overflow and giving a proper diagnostic back to the > user > > Signed-off-by: Gabriel Ravier > --- > tools/gpio/gpio-hammer.c | 19 +++++++++++++++++-- > 1 file changed, 17 insertions(+), 2 deletions(-) > > diff --git a/tools/gpio/gpio-hammer.c b/tools/gpio/gpio-hammer.c > index 0e0060a6e..273d33847 100644 > --- a/tools/gpio/gpio-hammer.c > +++ b/tools/gpio/gpio-hammer.c > @@ -77,7 +77,7 @@ int hammer_device(const char *device_name, unsigned int= *lines, int nlines, > > fprintf(stdout, "[%c] ", swirr[j]); > j++; > - if (j =3D=3D sizeof(swirr)-1) > + if (j =3D=3D sizeof(swirr) - 1) Please don't try to sneak in unrelated changes into commits. This is of course correct coding-style-wise but send it in a separate patch. > j =3D 0; > > fprintf(stdout, "["); > @@ -135,7 +135,14 @@ int main(int argc, char **argv) > device_name =3D optarg; > break; > case 'o': > - lines[i] =3D strtoul(optarg, NULL, 10); > + /* > + * Avoid overflow. Do not immediately error, we w= ant to > + * be able to accurately report on the amount of = times > + *'-o' was given to give an accurate error messag= e > + */ > + if (i < GPIOHANDLES_MAX) > + lines[i] =3D strtoul(optarg, NULL, 10); > + > i++; > break; > case '?': > @@ -143,6 +150,14 @@ int main(int argc, char **argv) > return -1; > } > } > + > + if (i >=3D GPIOHANDLES_MAX) { > + fprintf(stderr, > + "Only %d occurences of '-o' are allowed, %d were = found\n", > + GPIOHANDLES_MAX, i + 1); > + return -1; > + } > + > nlines =3D i; > > if (!device_name || !nlines) { > -- > 2.24.1 > Other than that, looks good. Bartosz