Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp877723pxk; Thu, 17 Sep 2020 20:05:56 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwz10AVs91I41PDLxvtpeerIz1Vz5YfQ7cOdSUmaaZLUCBabxZwydbNB63r3lG/foPTAxXp X-Received: by 2002:a50:f197:: with SMTP id x23mr37858305edl.367.1600398356155; Thu, 17 Sep 2020 20:05:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600398356; cv=none; d=google.com; s=arc-20160816; b=MuxSUhhxsEiATy284j8biL3yR43DItYxUsKHGgVabTGopz274asi7v0RNNspshjRLt qx3UapxeG0o1lab0GySE494xlK0+X3vaPq5H72a8NpEWOFEdZ8u1aijj8Q0aVCeOmh3/ cRMaOxRqvu/foW2CTsosxLhAAoYIwyI2CvWgx2761D3vwpmFGwzFLQSdeWuiZb2IdFCq Ona+kAbXvKyt89llL9JB32faBMUQB3HUNFSlZhZwIbLwfFKi+DO7WJjItugnNVikFtiC 8713GtzmGL+zHH2pOOrJkzf8A8USmnWB+MR8nNG2mLy+mac4l/Z/RL0i6ZSBPDlr+OdT jKGA== 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=ZESdVFpYSp6enyNwramjCLDsmfsHz73Mxd+UAqdMtls=; b=yxEldbKujTDnHSr7amgFBcBfnbaaYDavV5ASg08cipPgRfeRsMnjtHnOMILCPoxDFW /5/glqWBXBQ+72NCiKg8tPBgpYtUOdtR+nkJQYI5js4r/UH8JYQ5UJg5JXKDBoCXoAda JQCCppbfTdd/45+Z5CUESlxrzZGpe6QISGD78vIhmmmtySAaV/svhuzLzRAdSb+KkwiD gODF0MfO30t+ap8cElH03axHhX6fG1BttHGGNNvhNDAgXhAG4FYxpGqtNof7/AOVMb75 6HBGpawdzTOjlWNOukR+hYPBvEGWr05ZcdqjE0rilo1OLxSk3DGrwi8tzt23pKceNjyf WxyQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=y4K3AlcU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id q23si1222961ejd.363.2020.09.17.20.05.33; Thu, 17 Sep 2020 20:05:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=y4K3AlcU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730364AbgIRDC4 (ORCPT + 99 others); Thu, 17 Sep 2020 23:02:56 -0400 Received: from mail.kernel.org ([198.145.29.99]:53366 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727472AbgIRCFN (ORCPT ); Thu, 17 Sep 2020 22:05:13 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 5179C23718; Fri, 18 Sep 2020 02:05:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1600394712; bh=E350A/ai66N1iyvBYmzJeu358jXKJGXYFP/kPyZJ50Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=y4K3AlcUu93pfR3PzXwm14J2zELssw3IEClIuJjT7334dkRimjVena0C1qAmREKYx Zcl+wiBOB9zsSgS9qOa0W94uZOShQe41mpYdGvmM1/WTZDkjyRhJTz4edN/VQiz4Bk oixKCutxSja/5AJQ04KFKvtrbyrkKkoSiaZ81wxg= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Gabriel Ravier , Bartosz Golaszewski , Sasha Levin Subject: [PATCH AUTOSEL 5.4 197/330] tools: gpio-hammer: Avoid potential overflow in main Date: Thu, 17 Sep 2020 21:58:57 -0400 Message-Id: <20200918020110.2063155-197-sashal@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200918020110.2063155-1-sashal@kernel.org> References: <20200918020110.2063155-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Gabriel Ravier [ Upstream commit d1ee7e1f5c9191afb69ce46cc7752e4257340a31 ] 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 Signed-off-by: Bartosz Golaszewski Signed-off-by: Sasha Levin --- tools/gpio/gpio-hammer.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/tools/gpio/gpio-hammer.c b/tools/gpio/gpio-hammer.c index 0e0060a6eb346..083399d276e4e 100644 --- a/tools/gpio/gpio-hammer.c +++ b/tools/gpio/gpio-hammer.c @@ -135,7 +135,14 @@ int main(int argc, char **argv) device_name = optarg; break; case 'o': - lines[i] = strtoul(optarg, NULL, 10); + /* + * Avoid overflow. Do not immediately error, we want to + * be able to accurately report on the amount of times + * '-o' was given to give an accurate error message + */ + if (i < GPIOHANDLES_MAX) + lines[i] = strtoul(optarg, NULL, 10); + i++; break; case '?': @@ -143,6 +150,14 @@ int main(int argc, char **argv) return -1; } } + + if (i >= GPIOHANDLES_MAX) { + fprintf(stderr, + "Only %d occurences of '-o' are allowed, %d were found\n", + GPIOHANDLES_MAX, i + 1); + return -1; + } + nlines = i; if (!device_name || !nlines) { -- 2.25.1