]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
modinfo: Prevent undefined behavior with long keys
authorTobias Stoeckmann <tobias@stoeckmann.org>
Wed, 16 Oct 2024 09:43:47 +0000 (11:43 +0200)
committerLucas De Marchi <lucas.de.marchi@gmail.com>
Thu, 17 Oct 2024 21:47:53 +0000 (16:47 -0500)
If a key is longer than INT_MAX, it is possible to trigger a signed
integer overflow. Since this overflow only occurs for formatting,
prevent it by checking if key is longer than 15 characters. If it is,
there is no need to add any more spacing anyway.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/184
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
tools/modinfo.c

index 40f24ea62903f263bf4d75830b6d00a68dd5e896..6f8147ea49e1a12756bdd82c2d7af16c6a870774 100644 (file)
@@ -201,7 +201,7 @@ static int modinfo_do(struct kmod_module *mod)
        kmod_list_foreach(l, list) {
                const char *key = kmod_module_info_get_key(l);
                const char *value = kmod_module_info_get_value(l);
-               int keylen;
+               size_t keylen;
 
                if (field != NULL) {
                        if (!streq(field, key))
@@ -224,7 +224,9 @@ static int modinfo_do(struct kmod_module *mod)
                }
 
                keylen = strlen(key);
-               printf("%s:%-*s%s%c", key, 15 - keylen, "", value, separator);
+               if (keylen > 15)
+                       keylen = 15;
+               printf("%s:%-*s%s%c", key, 15 - (int)keylen, "", value, separator);
        }
 
        if (field != NULL)