Received: by 2002:a25:e7d8:0:0:0:0:0 with SMTP id e207csp606415ybh; Thu, 12 Mar 2020 07:51:50 -0700 (PDT) X-Google-Smtp-Source: ADFU+vuMZiu1HUtoZm9PvZa07nhlYDThfqE7EghitUjwvjjmKB/OSSimqdjYv7oeLQLJX5kkjk1K X-Received: by 2002:aca:b4c3:: with SMTP id d186mr2771838oif.131.1584024710571; Thu, 12 Mar 2020 07:51:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1584024710; cv=none; d=google.com; s=arc-20160816; b=uBxN9EXor3pO4SreNBwq2xsxHILZ9HfJSwDn5+LEFBKmg8zqNtNbcKFc3Ii0XPeTr1 2VaOg/QkbZwdDPwdINQ4jX8vA7QI1jyOC7XztOFGWjmSQ4M5ctvCrkwwO9PvekeJA/bI cEUSA4SoPVewGmqq+CKx2hhtAoDgUMqcaNQ6Wg7BGVkTa0uARon8kJOSDVWNLdqp2cpV tZ3ieLyZ5O8ue71DlacMZyKvhOJ7O45yhekX1GawsslnkapHoiy6Vi9f0Wdtw3IMOat7 77egNTQhckYJLScwebNAKZaB2uGOuF05nLfpwtRQSPj1e21oso6hLNLPHk+QaMjLj6l0 m07g== 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:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=PHZkXHpRcWeTU1239EHZ2Ly4RecY9yEad1dwpk6VHw8=; b=iDxHA6G1nr6qTYjPpYJv2A72jgre5bESm7LrjlXiSDvkZSLCvsyC6j4sTkAouZk2ka qGwSmHE5s9OF9C+7hOqNcTZJ7+YQ2yasV8HzxTb82j3D+t1C1FyNTpS5iEZwxPTC5RjQ OZ5eagQxJCGdZzI664RjWjfLVR9OojF1+KUR2MwP6KQdt7YefBKVrlBhcgIuglhxYSPk jtsPCoAbCGbrJ343Rznrp5ocOvjruZJPSEouIhJsGXXAJwTYJJAadmBD8HVSw0mw/RD3 KuKSEeuARUWZqTP2XcI56wsXorpxq0TWPn04ZnpiWlL2z7LL1fVW0XEYPStqdj9Jh2If ckRg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=syE3p4O0; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b20si2898221otp.82.2020.03.12.07.51.38; Thu, 12 Mar 2020 07:51:50 -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=@gmail.com header.s=20161025 header.b=syE3p4O0; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727766AbgCLOue (ORCPT + 99 others); Thu, 12 Mar 2020 10:50:34 -0400 Received: from mail-wm1-f67.google.com ([209.85.128.67]:39468 "EHLO mail-wm1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727072AbgCLOue (ORCPT ); Thu, 12 Mar 2020 10:50:34 -0400 Received: by mail-wm1-f67.google.com with SMTP id f7so6651569wml.4; Thu, 12 Mar 2020 07:50:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=PHZkXHpRcWeTU1239EHZ2Ly4RecY9yEad1dwpk6VHw8=; b=syE3p4O0jn/1GAsJX/jw5OA79C1I/g+qpiUG80axtmdid0Xqdc2cGeHRbfprQbCZcC gohq/Rogk0uqknpp30s7QLQoEyziguK64nwTsAoVhAhEr3Cx3cFCe+Jz02XpQJKVQfzQ aUy1vi+ClscneXPqH0xiXzSTjQzaynDlv6nokRZbaf3zSgY6yIU94BNmKIOsyeDUNBWd Nkiwro+2jyGXpbKKtuCc5eb4oLgtEqNPuUaqlZt23+BS0LlHYkVvywVkNPDtvRlL4qBE 7DlxgJ2mVV7CgueiwnAs9VLEAQvdTTIfipcIxhJcK+kj2xIxvEE6kmZufOKPeK76plWh THhw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=PHZkXHpRcWeTU1239EHZ2Ly4RecY9yEad1dwpk6VHw8=; b=saO3tWhQDOlE0HK/NkB1OZRIf/K9NoLO/sP7QDFjXXfMhjnm4+3QDJKlIJU5IWS5Nv WFqMcR5nxu9lm+bDtUkEqUJZJhJvZi2UjHKPZ5WWvgUdDHHMeEJCHAtX3BuiCPY76ATj d/TrCguq7krdOqzNXBbnODbkZTFbKVGc+xdbiX8ZSW5xx/3+Pya4Ex1ddpYDSFGWm1qd 618lYsjQOvvury5ZY6w4ie5pTPvRtqQrwFGe5ZfLA7+5c11paEEvR3nQ5djLrjt+UfuB mTSl0ben2eH0maHZYi0IZD+mnf7S5d4dPMYoXcDZ+OoPbM+xOYWJwY9cjQyaQMuaPnWv ohaw== X-Gm-Message-State: ANhLgQ2EpRj8DdttSRnaI12EQGLnZVokJAFDrFTH3RVlGnwgl+d+3OuX nDORn2QqsC3wPvMdM8W/c/0= X-Received: by 2002:a05:600c:286:: with SMTP id 6mr5367770wmk.56.1584024632141; Thu, 12 Mar 2020 07:50:32 -0700 (PDT) Received: from gabrielDell.schule.local ([37.58.58.229]) by smtp.gmail.com with ESMTPSA id f127sm13068727wma.4.2020.03.12.07.50.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Mar 2020 07:50:31 -0700 (PDT) From: Gabriel Ravier To: linus.walleij@linaro.org, bgolaszewski@baylibre.com Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Gabriel Ravier Subject: [PATCH v3] gpio-hammer: Avoid potential overflow in main Date: Thu, 12 Mar 2020 15:50:21 +0100 Message-Id: <20200312145020.1848727-1-gabravier@gmail.com> X-Mailer: git-send-email 2.24.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/tools/gpio/gpio-hammer.c b/tools/gpio/gpio-hammer.c index 0e0060a6e..083399d27 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.24.1