From: Tobias Stoeckmann Date: Wed, 16 Oct 2024 09:43:47 +0000 (+0200) Subject: modinfo: Prevent undefined behavior with long keys X-Git-Tag: v34~219 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7b416d59e4505b8f0a59dd0111ed738b79de2629;p=thirdparty%2Fkmod.git modinfo: Prevent undefined behavior with long keys 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 Reviewed-by: Emil Velikov Link: https://github.com/kmod-project/kmod/pull/184 Signed-off-by: Lucas De Marchi --- diff --git a/tools/modinfo.c b/tools/modinfo.c index 40f24ea6..6f8147ea 100644 --- a/tools/modinfo.c +++ b/tools/modinfo.c @@ -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)