]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
libkmod: check for trailing \0 in __ksymtab_strings
authorEmil Velikov <emil.l.velikov@gmail.com>
Thu, 24 Oct 2024 22:18:28 +0000 (23:18 +0100)
committerLucas De Marchi <lucas.de.marchi@gmail.com>
Fri, 15 Nov 2024 16:03:46 +0000 (10:03 -0600)
As per the documentation (man 5 elf) the section must be null
terminated. Move the check further up and remove the no longer needed
code trying to workaround non-compliant instances.

Note: drop the erroneous +1 in the overflow (malloc size) calculation

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

index 85c1a95963101eb2e7e0ce9e0bf440f2d00aed34..a1868815aee1840dab4597116bf7fdd8b9b4dee1 100644 (file)
@@ -647,7 +647,7 @@ static int kmod_elf_get_symbols_symtab(const struct kmod_elf *elf,
        char *itr;
        struct kmod_modversion *a;
        int count, err;
-       size_t vec_size, tmp_size, total_size;
+       size_t vec_size, total_size;
 
        *array = NULL;
 
@@ -664,6 +664,11 @@ static int kmod_elf_get_symbols_symtab(const struct kmod_elf *elf,
        if (size <= 1)
                return 0;
 
+       if (strings[size - 1] != '\0') {
+               ELFDBG(elf, "section __ksymtab_strings does not end with \\0 byte");
+               return -EINVAL;
+       }
+
        last = 0;
        for (i = 0, count = 0; i < size; i++) {
                if (strings[i] == '\0') {
@@ -675,13 +680,10 @@ static int kmod_elf_get_symbols_symtab(const struct kmod_elf *elf,
                        last = i + 1;
                }
        }
-       if (strings[i - 1] != '\0')
-               count++;
 
-       /* sizeof(struct kmod_modversion) * count + size + 1 */
+       /* sizeof(struct kmod_modversion) * count + size */
        if (umulsz_overflow(sizeof(struct kmod_modversion), count, &vec_size) ||
-           uaddsz_overflow(size, vec_size, &tmp_size) ||
-           uaddsz_overflow(1, tmp_size, &total_size)) {
+           uaddsz_overflow(size, vec_size, &total_size)) {
                return -ENOMEM;
        }
 
@@ -708,15 +710,6 @@ static int kmod_elf_get_symbols_symtab(const struct kmod_elf *elf,
                        last = i + 1;
                }
        }
-       if (strings[i - 1] != '\0') {
-               size_t slen = i - last;
-               a[count].crc = 0;
-               a[count].bind = KMOD_SYMBOL_GLOBAL;
-               a[count].symbol = itr;
-               memcpy(itr, strings + last, slen);
-               itr[slen] = '\0';
-               count++;
-       }
 
        return count;
 }