From patchwork Tue Sep 29 11:00:12 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 290741 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=-13.5 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT 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 27A71C4727C for ; Tue, 29 Sep 2020 12:43:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D0D472076A for ; Tue, 29 Sep 2020 12:43:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1601383405; bh=XYLW7AwDHfNVBR1Kle+05njd23MtGyHnDr0udEVvZvA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=R5+ks6/M3ptwzVf7uDVj/iv/CoEbdUdWWrxBu9JNTIw1cfVvWGdYgZAlvRZ8WFQpA U+5Xr+jkgagcEV1sFgGUnZyNmt6w/r9103JAIjX5VjegCQmzxvGr7lqAJXQPwjizFi J3+IJAI427ubI55x34s7iza3b0Ao+2QowVyY9BFc= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729633AbgI2MnY (ORCPT ); Tue, 29 Sep 2020 08:43:24 -0400 Received: from mail.kernel.org ([198.145.29.99]:50310 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729096AbgI2LJs (ORCPT ); Tue, 29 Sep 2020 07:09:48 -0400 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 30CE4221E8; Tue, 29 Sep 2020 11:09:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1601377774; bh=XYLW7AwDHfNVBR1Kle+05njd23MtGyHnDr0udEVvZvA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uQstXZsWyOEln63HiIOpx2qdRwllIqsQYjnLOF+WvwpHcYygRxlQkbrgEckk7RYhQ JWUMz5QJ3fxFN5IzWIh8wV5AmBaGhypR+q2HJYbmiImcQB3w4lNTVIWP4jZ7w6V2MA zQgBcN2IEWXMD/SjZtGiZlmHtjAt6cOleBAcVypw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Gabriel Ravier , Bartosz Golaszewski , Sasha Levin Subject: [PATCH 4.9 068/121] tools: gpio-hammer: Avoid potential overflow in main Date: Tue, 29 Sep 2020 13:00:12 +0200 Message-Id: <20200929105933.538410182@linuxfoundation.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200929105930.172747117@linuxfoundation.org> References: <20200929105930.172747117@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: stable@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 37b3f141053df..85f45800f881f 100644 --- a/tools/gpio/gpio-hammer.c +++ b/tools/gpio/gpio-hammer.c @@ -171,7 +171,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 '?': @@ -179,6 +186,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) {