tools: debug-events: don't overrun the device array with too many arguments
authorPeter Hutterer <peter.hutterer@who-t.net>
Wed, 4 Dec 2019 00:18:27 +0000 (10:18 +1000)
committerPeter Hutterer <peter.hutterer@who-t.net>
Wed, 4 Dec 2019 02:50:11 +0000 (12:50 +1000)
Only the --device option was checked for argument count, not the rest so it's
easy to overrun the array by specifying too many devices.

Except: this was a theoretical bug only, more than 64 arguments trigger
an assertion in the argv processing in tools/shared.c anyway. Let's drop the
debug-events limit to 60 devices so we can at least have a test for this.

Found by coverity

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
tools/libinput-debug-events.c
tools/test-tool-option-parsing.py

index bdabb5447dc308100a8c212907b717776ef153e5..079aa7c839153af2b578a2a938bc78042f06a47e 100644 (file)
@@ -935,7 +935,7 @@ main(int argc, char **argv)
 {
        struct libinput *li;
        enum tools_backend backend = BACKEND_NONE;
-       char *seat_or_devices[64] = {NULL};
+       char *seat_or_devices[60] = {NULL};
        size_t ndevices = 0;
        bool grab = false;
        bool verbose = false;
@@ -1028,6 +1028,10 @@ main(int argc, char **argv)
                }
                backend = BACKEND_DEVICE;
                do {
+                       if (ndevices >= ARRAY_LENGTH(seat_or_devices)) {
+                               usage();
+                               return EXIT_INVALID_USAGE;
+                       }
                        seat_or_devices[ndevices++] = safe_strdup(argv[optind]);
                } while(++optind < argc);
        } else if (backend == BACKEND_NONE) {
index 0484e55df0df6f9048b8cea4733c1dd10da5dc7a..e96d1abf5724813905436f2164742f7e28cbb2ec 100755 (executable)
@@ -212,6 +212,11 @@ class TestDebugEvents(TestToolWithOptions, TestLibinputTool):
         self.run_command_success(['--device', '/dev/input/event0', '/dev/input/event0'])
         self.run_command_success(['/dev/input/event0', '/dev/input/event1'])
 
+    def test_too_many_devices(self):
+        # Too many arguments just bails with the usage message
+        rc, stdout, stderr = self.run_command(['/dev/input/event0'] * 61)
+        self.assertEqual(rc, 2, msg=(stdout, stderr))
+
 
 class TestDebugGUI(TestToolWithOptions, TestLibinputTool):
     subtool = 'debug-gui'