Received: by 2002:a25:e7d8:0:0:0:0:0 with SMTP id e207csp324205ybh; Thu, 12 Mar 2020 02:41:30 -0700 (PDT) X-Google-Smtp-Source: ADFU+vuywdPIblLfzl37+IYxS8PDUiveleDgoRGe6PJ+ILEzIc5MFL1Naq0adlG8M+XfoPnWsxGc X-Received: by 2002:aca:ad8f:: with SMTP id w137mr1817052oie.104.1584006090120; Thu, 12 Mar 2020 02:41:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1584006090; cv=none; d=google.com; s=arc-20160816; b=jsAP7oZ/3vNXXvp+EpYO2qYkRlH5gpqFY7xqlupb+3ScOn4pbU9enZOoAjMPPeuOUy 3goVigXLnaleybnqbTc3EE1HoJW0lmA9kOpUaJ6/Ga/RRnysHJcvSO4ml6qUJmgYvK2S i4nn159wVnncXgDywdjfmbSybjAJ/PA1R7VGnwafzTocb1GT6MB9pnldvfE2fTXhiXHR DvmuFq2zsPxpDjJnPuk4AApD9UfNxMUeuxMKb7CBJvCG6ssoCgumI3jKuyNf8OpsG02C XOwL8MNpTr4eve+xGnag/pm77Sqo5vui+mCF7n6wHrTfJR+8uAj2KC/NTpctpumS2PxB bfkA== 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=nIZqiO3olanBqpb8V00tr6xlu/aKR3WtYKMWSraEapQ=; b=MjQNA+LC32aN9t9U3OGAnPHee3qulkBMjzdhjVB0iBnS4d4R8x2l1W8DUtWcBq/eTq J65Mc+faTZBgU5GRMpkzhxKsGVvYaIhTvaTT72oMs1Gg1ny9a9zsQf4Lt/hCB1cwpq03 13oD53pwr4qBQ08slXulyI2CLbQKYMABxe9oo1nMVGF+WIirle8m0OYCGCxQ1o3w4/QT CQtC+DomDcGLInYHbQwdy+wL1xeMjaRhT+n0FQjzf18EwhoZQqfNmPTXmVC96AEH0nUa POfvWfJjUCZdb0y2OdCCwyJxC9+vB7dW1J01ZVEs6RSljt/9RmV+aiI6X3erfKx4Vksz dbMQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=kjYPWyyW; 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 i6si2663111otk.220.2020.03.12.02.41.17; Thu, 12 Mar 2020 02:41:30 -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=kjYPWyyW; 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 S1726390AbgCLJkS (ORCPT + 99 others); Thu, 12 Mar 2020 05:40:18 -0400 Received: from mail-wr1-f65.google.com ([209.85.221.65]:34036 "EHLO mail-wr1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725268AbgCLJkS (ORCPT ); Thu, 12 Mar 2020 05:40:18 -0400 Received: by mail-wr1-f65.google.com with SMTP id z15so6516178wrl.1; Thu, 12 Mar 2020 02:40:16 -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=nIZqiO3olanBqpb8V00tr6xlu/aKR3WtYKMWSraEapQ=; b=kjYPWyyWUUxkfuBZcUJPLcIVx7QylnuWHIRi+umPxZ6/c0nPYq8TuurW35PpZmufNQ 5CeFyqpJorb9S0Pj0Z5nItcDT+Uw0O34flWw1cnSaicZ+Llo4lJpU5gMKoKf0leVTP+x Zsq5DeHoJNvtauMDMAyfSfjON5vgNH7c4K3WSHdbq7mM1kLDUfb5D4Gh2JVk57WGjbee FkEWbdAskJ9XHi3EwCVYMPph4iCIN43IXr1ssQeYgFZ1RjjuVkj1L4L0FHZ9OXWGzpsu mikgnPMZ4zt0VOpb4s90Et3BebiDEKjR0UNCOkRcs3rjO3gJvCHZsaqGCDQ1SBsSwpb7 xjoQ== 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=nIZqiO3olanBqpb8V00tr6xlu/aKR3WtYKMWSraEapQ=; b=nqEOyknTC2HvvXfF4xS7LQCIBWpQrPuopWRlGr5IFH1yJDqco50PMejrH/w/o+JYyG rmsgRLKAJQTwWEYRrcCj4LuuA9w8HBhECIpkrmkTTP1IGhiTJNDYAA57JD221WclzIed zylF8aNQhwcQnE1oVfigrEcHZrSSImpY9r0/INIhLak/e/NbvVvAFxA5fHfjH5K+ZZam /yfH9vbXtOCNnsdXkhIMMTYR58oJfLQMF/je5Zho+U4A4+J+bafr9+Zf3zxfNN8rhCtL 3Wk/uJotxjcfNZla1QJVuxWYbXLauPJo0J+VxflwsDLE9PDnyPzk+9uzmjK7IdNoCqT9 PfXg== X-Gm-Message-State: ANhLgQ3fNxk0eKcnzK+/EAlAIUitzrABVoo4w+cg6T/zxmDo1wC0tJhx LIbTBlKQvlSuHoP6uK2+cIg= X-Received: by 2002:adf:b1d2:: with SMTP id r18mr9637346wra.86.1584006016105; Thu, 12 Mar 2020 02:40:16 -0700 (PDT) Received: from localhost.localdomain ([37.58.58.231]) by smtp.gmail.com with ESMTPSA id c26sm11604589wmb.8.2020.03.12.02.40.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Mar 2020 02:40:15 -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] gpio-hammer: Avoid potential overflow in main Date: Thu, 12 Mar 2020 10:40:08 +0100 Message-Id: <20200312094008.1833929-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 | 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 == sizeof(swirr)-1) + if (j == sizeof(swirr) - 1) j = 0; fprintf(stdout, "["); @@ -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