]> git.ipfire.org Git - thirdparty/kmod.git/blobdiff - libkmod/libkmod-elf.c
gitignore: ignore .cache.mk when building modules
[thirdparty/kmod.git] / libkmod / libkmod-elf.c
index 8052e4e671031bbc02645d9dd04577f7ad34dc54..ef4a8a3142a1f48dbbe913b9c8a16ac41d205bf8 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * libkmod - interface to kernel module operations
  *
- * Copyright (C) 2011  ProFUSION embedded systems
+ * Copyright (C) 2011-2013  ProFUSION embedded systems
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <assert.h>
 #include <elf.h>
+#include <errno.h>
 #include <stdlib.h>
 #include <string.h>
-#include <assert.h>
-#include <errno.h>
+
+#include <shared/util.h>
 
 #include "libkmod.h"
-#include "libkmod-private.h"
+#include "libkmod-internal.h"
 
 enum kmod_elf_class {
        KMOD_ELF_32 = (1 << 1),
@@ -45,12 +46,6 @@ struct kmod_modversion64 {
        char name[64 - sizeof(uint64_t)];
 };
 
-#ifdef WORDS_BIGENDIAN
-static const enum kmod_elf_class native_endianess = KMOD_ELF_MSB;
-#else
-static const enum kmod_elf_class native_endianess = KMOD_ELF_LSB;
-#endif
-
 struct kmod_elf {
        const uint8_t *memory;
        uint8_t *changed;
@@ -223,7 +218,7 @@ static inline const void *elf_get_section_header(const struct kmod_elf *elf, uin
                return NULL;
        }
        return elf_get_mem(elf, elf->header.section.offset +
-                          idx * elf->header.section.entry_size);
+                          (uint64_t)(idx * elf->header.section.entry_size));
 }
 
 static inline int elf_get_section_info(const struct kmod_elf *elf, uint16_t idx, uint64_t *offset, uint64_t *size, uint32_t *nameoff)
@@ -243,20 +238,20 @@ static inline int elf_get_section_info(const struct kmod_elf *elf, uint16_t idx,
        elf_get_uint(elf, off + offsetof(typeof(*hdr), field), sizeof(hdr->field))
 
        if (elf->class & KMOD_ELF_32) {
-               const Elf32_Shdr *hdr = (const Elf32_Shdr *)p;
+               const Elf32_Shdr *hdr _unused_ = (const Elf32_Shdr *)p;
                *size = READV(sh_size);
                *offset = READV(sh_offset);
                *nameoff = READV(sh_name);
        } else {
-               const Elf64_Shdr *hdr = (const Elf64_Shdr *)p;
+               const Elf64_Shdr *hdr _unused_ = (const Elf64_Shdr *)p;
                *size = READV(sh_size);
                *offset = READV(sh_offset);
                *nameoff = READV(sh_name);
        }
 #undef READV
 
-       min_size = *offset + *size;
-       if (min_size > elf->size) {
+       if (addu64_overflow(*offset, *size, &min_size)
+           || min_size > elf->size) {
                ELFDBG(elf, "out-of-bounds: %"PRIu64" >= %"PRIu64" (ELF size)\n",
                       min_size, elf->size);
                return -EINVAL;
@@ -277,13 +272,14 @@ static const char *elf_get_strings_section(const struct kmod_elf *elf, uint64_t
 struct kmod_elf *kmod_elf_new(const void *memory, off_t size)
 {
        struct kmod_elf *elf;
-       size_t hdr_size, shdr_size, min_size;
+       uint64_t min_size;
+       size_t shdrs_size, shdr_size;
        int class;
 
-       assert(sizeof(uint16_t) == sizeof(Elf32_Half));
-       assert(sizeof(uint16_t) == sizeof(Elf64_Half));
-       assert(sizeof(uint32_t) == sizeof(Elf32_Word));
-       assert(sizeof(uint32_t) == sizeof(Elf64_Word));
+       assert_cc(sizeof(uint16_t) == sizeof(Elf32_Half));
+       assert_cc(sizeof(uint16_t) == sizeof(Elf64_Half));
+       assert_cc(sizeof(uint32_t) == sizeof(Elf32_Word));
+       assert_cc(sizeof(uint32_t) == sizeof(Elf64_Word));
 
        class = elf_identify(memory, size);
        if (class < 0) {
@@ -311,14 +307,12 @@ struct kmod_elf *kmod_elf_new(const void *memory, off_t size)
        elf->header.strings.section = READV(e_shstrndx);        \
        elf->header.machine = READV(e_machine)
        if (elf->class & KMOD_ELF_32) {
-               const Elf32_Ehdr *hdr = elf_get_mem(elf, 0);
+               const Elf32_Ehdr *hdr _unused_ = elf_get_mem(elf, 0);
                LOAD_HEADER;
-               hdr_size = sizeof(Elf32_Ehdr);
                shdr_size = sizeof(Elf32_Shdr);
        } else {
-               const Elf64_Ehdr *hdr = elf_get_mem(elf, 0);
+               const Elf64_Ehdr *hdr _unused_ = elf_get_mem(elf, 0);
                LOAD_HEADER;
-               hdr_size = sizeof(Elf64_Ehdr);
                shdr_size = sizeof(Elf64_Shdr);
        }
 #undef LOAD_HEADER
@@ -335,8 +329,9 @@ struct kmod_elf *kmod_elf_new(const void *memory, off_t size)
                       elf->header.section.entry_size, shdr_size);
                goto invalid;
        }
-       min_size = hdr_size + shdr_size * elf->header.section.count;
-       if (min_size >= elf->size) {
+       shdrs_size = shdr_size * elf->header.section.count;
+       if (addu64_overflow(shdrs_size, elf->header.section.offset, &min_size)
+           || min_size > elf->size) {
                ELFDBG(elf, "file is too short to hold sections\n");
                goto invalid;
        }
@@ -375,7 +370,32 @@ const void *kmod_elf_get_memory(const struct kmod_elf *elf)
        return elf->memory;
 }
 
-static int kmod_elf_get_section(const struct kmod_elf *elf, const char *section, const void **buf, uint64_t *buf_size)
+static int elf_find_section(const struct kmod_elf *elf, const char *section)
+{
+       uint64_t nameslen;
+       const char *names = elf_get_strings_section(elf, &nameslen);
+       uint16_t i;
+
+       for (i = 1; i < elf->header.section.count; i++) {
+               uint64_t off, size;
+               uint32_t nameoff;
+               const char *n;
+               int err = elf_get_section_info(elf, i, &off, &size, &nameoff);
+               if (err < 0)
+                       continue;
+               if (nameoff >= nameslen)
+                       continue;
+               n = names + nameoff;
+               if (!streq(section, n))
+                       continue;
+
+               return i;
+       }
+
+       return -ENOENT;
+}
+
+int kmod_elf_get_section(const struct kmod_elf *elf, const char *section, const void **buf, uint64_t *buf_size)
 {
        uint64_t nameslen;
        const char *names = elf_get_strings_section(elf, &nameslen);
@@ -488,10 +508,10 @@ int kmod_elf_get_modversions(const struct kmod_elf *elf, struct kmod_modversion
        int i, count, err;
 #define MODVERSION_SEC_SIZE (sizeof(struct kmod_modversion64))
 
-       assert(sizeof(struct kmod_modversion64) ==
+       assert_cc(sizeof(struct kmod_modversion64) ==
                                        sizeof(struct kmod_modversion32));
 
-       if (elf->class == KMOD_ELF_32)
+       if (elf->class & KMOD_ELF_32)
                offcrc = sizeof(uint32_t);
        else
                offcrc = sizeof(uint64_t);
@@ -550,26 +570,29 @@ int kmod_elf_get_modversions(const struct kmod_elf *elf, struct kmod_modversion
 
 int kmod_elf_strip_section(struct kmod_elf *elf, const char *section)
 {
-       uint64_t size, off;
+       uint64_t off, size;
        const void *buf;
-       int err = kmod_elf_get_section(elf, section, &buf, &size);
-       if (err < 0)
-               return err;
+       int idx = elf_find_section(elf, section);
+       uint64_t val;
+
+       if (idx < 0)
+               return idx;
 
+       buf = elf_get_section_header(elf, idx);
        off = (const uint8_t *)buf - elf->memory;
 
-#define WRITEV(field, value)                   \
-       elf_set_uint(elf, off + offsetof(typeof(*hdr), field), sizeof(hdr->field), value)
        if (elf->class & KMOD_ELF_32) {
-               const Elf32_Shdr *hdr = buf;
-               uint32_t val = ~(uint32_t)SHF_ALLOC;
-               return WRITEV(sh_flags, val);
+               off += offsetof(Elf32_Shdr, sh_flags);
+               size = sizeof(((Elf32_Shdr *)buf)->sh_flags);
        } else {
-               const Elf64_Shdr *hdr = buf;
-               uint64_t val = ~(uint64_t)SHF_ALLOC;
-               return WRITEV(sh_flags, val);
+               off += offsetof(Elf64_Shdr, sh_flags);
+               size = sizeof(((Elf64_Shdr *)buf)->sh_flags);
        }
-#undef WRITEV
+
+       val = elf_get_uint(elf, off, size);
+       val &= ~(uint64_t)SHF_ALLOC;
+
+       return elf_set_uint(elf, off, size, val);
 }
 
 int kmod_elf_strip_vermagic(struct kmod_elf *elf)
@@ -611,7 +634,6 @@ int kmod_elf_strip_vermagic(struct kmod_elf *elf)
                        i += strlen(s);
                        continue;
                }
-               s += len;
                off = (const uint8_t *)s - elf->memory;
 
                if (elf->changed == NULL) {
@@ -705,7 +727,6 @@ static int kmod_elf_get_symbols_symtab(const struct kmod_elf *elf, struct kmod_m
                a[count].symbol = itr;
                memcpy(itr, strings + last, slen);
                itr[slen] = '\0';
-               itr += slen + 1;
                count++;
        }
 
@@ -726,6 +747,31 @@ static inline uint8_t kmod_symbol_bind_from_elf(uint8_t elf_value)
        }
 }
 
+static uint64_t kmod_elf_resolve_crc(const struct kmod_elf *elf, uint64_t crc, uint16_t shndx)
+{
+       int err;
+       uint64_t off, size;
+       uint32_t nameoff;
+
+       if (shndx == SHN_ABS || shndx == SHN_UNDEF)
+               return crc;
+
+       err = elf_get_section_info(elf, shndx, &off, &size, &nameoff);
+       if (err < 0) {
+               ELFDBG("Cound not find section index %"PRIu16" for crc", shndx);
+               return (uint64_t)-1;
+       }
+
+       if (crc > (size - sizeof(uint32_t))) {
+               ELFDBG("CRC offset %"PRIu64" is too big, section %"PRIu16" size is %"PRIu64"\n",
+                      crc, shndx, size);
+               return (uint64_t)-1;
+       }
+
+       crc = elf_get_uint(elf, off + crc, sizeof(uint32_t));
+       return crc;
+}
+
 /* array will be allocated with strings in a single malloc, just free *array */
 int kmod_elf_get_symbols(const struct kmod_elf *elf, struct kmod_modversion **array)
 {
@@ -809,6 +855,7 @@ int kmod_elf_get_symbols(const struct kmod_elf *elf, struct kmod_modversion **ar
                uint32_t name_off;
                uint64_t crc;
                uint8_t info, bind;
+               uint16_t shndx;
 
 #define READV(field)                                                   \
                elf_get_uint(elf, sym_off + offsetof(typeof(*s), field),\
@@ -818,11 +865,13 @@ int kmod_elf_get_symbols(const struct kmod_elf *elf, struct kmod_modversion **ar
                        name_off = READV(st_name);
                        crc = READV(st_value);
                        info = READV(st_info);
+                       shndx = READV(st_shndx);
                } else {
                        Elf64_Sym *s;
                        name_off = READV(st_name);
                        crc = READV(st_value);
                        info = READV(st_info);
+                       shndx = READV(st_shndx);
                }
 #undef READV
                name = elf_get_mem(elf, str_off + name_off);
@@ -835,7 +884,7 @@ int kmod_elf_get_symbols(const struct kmod_elf *elf, struct kmod_modversion **ar
                else
                        bind = ELF64_ST_BIND(info);
 
-               a[count].crc = crc;
+               a[count].crc = kmod_elf_resolve_crc(elf, crc, shndx);
                a[count].bind = kmod_symbol_bind_from_elf(bind);
                a[count].symbol = itr;
                slen = strlen(name);
@@ -1049,6 +1098,7 @@ int kmod_elf_get_dependency_symbols(const struct kmod_elf *elf, struct kmod_modv
        if (count == 0) {
                free(visited_versions);
                free(symcrcs);
+               *array = NULL;
                return 0;
        }