Skip to content

Commit

Permalink
tools: gpio-hammer: Avoid potential overflow in main
Browse files Browse the repository at this point in the history
[ 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 <[email protected]>
Signed-off-by: Bartosz Golaszewski <[email protected]>
Signed-off-by: Sasha Levin <[email protected]>
  • Loading branch information
GabrielRavier authored and gregkh committed Oct 1, 2020
1 parent 20bec89 commit c59dfd1
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion tools/gpio/gpio-hammer.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,29 @@ 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 '?':
print_usage();
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) {
Expand Down

0 comments on commit c59dfd1

Please sign in to comment.