]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
libkmod: fix possible out-of-bounds memory access
authorDmitry Antipov <dmantipov@yandex.ru>
Fri, 19 May 2023 07:46:38 +0000 (10:46 +0300)
committerLucas De Marchi <lucas.de.marchi@gmail.com>
Tue, 30 May 2023 19:56:54 +0000 (12:56 -0700)
An attempt to pass too long module name to, say, rmmod, may
cause an out-of-bounds memory access (as repoted by UBSan):

$ rmmod $(for i in $(seq 0 4200); do echo -ne x; done)
libkmod/libkmod-module.c:1828:8: runtime error: index 4107 out of bounds for type 'char [4096]'

This is because 'snprintf(path, sizeof(path), ...)' may return the
value which exceeds 'sizeof(path)' (which happens when an output
gets truncated). To play it safe, such a suspicious output is
better to be rejected explicitly.

Reviewed-by: Christophe Leroy <christophe.leroy@csgroup.eu>
Signed-off-by: Dmitry Antipov <dmantipov@yandex.ru>
Link: https://lore.kernel.org/r/20230519074638.402045-1-dmantipov@yandex.ru
libkmod/libkmod-module.c

index 1da64b304ee755497c99088adacc05b01e1c532a..7736b7e106ccd7d9a5b3394088cacc2e9cfdb40a 100644 (file)
@@ -1810,6 +1810,10 @@ KMOD_EXPORT int kmod_module_get_initstate(const struct kmod_module *mod)
 
        pathlen = snprintf(path, sizeof(path),
                                "/sys/module/%s/initstate", mod->name);
+       if (pathlen >= (int)sizeof(path)) {
+               /* Too long path was truncated */
+               return -ENAMETOOLONG;
+       }
        fd = open(path, O_RDONLY|O_CLOEXEC);
        if (fd < 0) {
                err = -errno;