Commit e0049825 authored by Kees Cook's avatar Kees Cook Committed by Sam Ravnborg

kbuild: make modpost detect unterminated device id lists

Cause modpost to fail if any device id lists are incorrectly terminated,
after reporting the offender.
Improved reporting by akpm
Signed-off-by: default avatarKees Cook <kees@ubuntu.com>
Cc: Greg KH <greg@kroah.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Jeff Garzik <jeff@garzik.org>
Cc: Ben Collins <bcollins@ubuntu.com>
Cc: Michael Wu <flamingice@sourmilk.net>
Cc: Rusty Russell <rusty@rustcorp.com.au>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarSam Ravnborg <sam@ravnborg.org>
parent 295ac051
...@@ -55,10 +55,14 @@ do { \ ...@@ -55,10 +55,14 @@ do { \
* Check that sizeof(device_id type) are consistent with size of section * Check that sizeof(device_id type) are consistent with size of section
* in .o file. If in-consistent then userspace and kernel does not agree * in .o file. If in-consistent then userspace and kernel does not agree
* on actual size which is a bug. * on actual size which is a bug.
* Also verify that the final entry in the table is all zeros.
**/ **/
static void device_id_size_check(const char *modname, const char *device_id, static void device_id_check(const char *modname, const char *device_id,
unsigned long size, unsigned long id_size) unsigned long size, unsigned long id_size,
void *symval)
{ {
int i;
if (size % id_size || size < id_size) { if (size % id_size || size < id_size) {
fatal("%s: sizeof(struct %s_device_id)=%lu is not a modulo " fatal("%s: sizeof(struct %s_device_id)=%lu is not a modulo "
"of the size of section __mod_%s_device_table=%lu.\n" "of the size of section __mod_%s_device_table=%lu.\n"
...@@ -66,6 +70,20 @@ static void device_id_size_check(const char *modname, const char *device_id, ...@@ -66,6 +70,20 @@ static void device_id_size_check(const char *modname, const char *device_id,
"in mod_devicetable.h\n", "in mod_devicetable.h\n",
modname, device_id, id_size, device_id, size, device_id); modname, device_id, id_size, device_id, size, device_id);
} }
/* Verify last one is a terminator */
for (i = 0; i < id_size; i++ ) {
if (*(uint8_t*)(symval+size-id_size+i)) {
fprintf(stderr,"%s: struct %s_device_id is %lu bytes. "
"The last of %lu is:\n",
modname, device_id, id_size, size / id_size);
for (i = 0; i < id_size; i++ )
fprintf(stderr,"0x%02x ",
*(uint8_t*)(symval+size-id_size+i) );
fprintf(stderr,"\n");
fatal("%s: struct %s_device_id is not terminated "
"with a NULL entry!\n", modname, device_id);
}
}
} }
/* USB is special because the bcdDevice can be matched against a numeric range */ /* USB is special because the bcdDevice can be matched against a numeric range */
...@@ -168,7 +186,7 @@ static void do_usb_table(void *symval, unsigned long size, ...@@ -168,7 +186,7 @@ static void do_usb_table(void *symval, unsigned long size,
unsigned int i; unsigned int i;
const unsigned long id_size = sizeof(struct usb_device_id); const unsigned long id_size = sizeof(struct usb_device_id);
device_id_size_check(mod->name, "usb", size, id_size); device_id_check(mod->name, "usb", size, id_size, symval);
/* Leave last one: it's the terminator. */ /* Leave last one: it's the terminator. */
size -= id_size; size -= id_size;
...@@ -535,7 +553,7 @@ static void do_table(void *symval, unsigned long size, ...@@ -535,7 +553,7 @@ static void do_table(void *symval, unsigned long size,
char alias[500]; char alias[500];
int (*do_entry)(const char *, void *entry, char *alias) = function; int (*do_entry)(const char *, void *entry, char *alias) = function;
device_id_size_check(mod->name, device_id, size, id_size); device_id_check(mod->name, device_id, size, id_size, symval);
/* Leave last one: it's the terminator. */ /* Leave last one: it's the terminator. */
size -= id_size; size -= id_size;
...@@ -557,14 +575,21 @@ void handle_moddevtable(struct module *mod, struct elf_info *info, ...@@ -557,14 +575,21 @@ void handle_moddevtable(struct module *mod, struct elf_info *info,
Elf_Sym *sym, const char *symname) Elf_Sym *sym, const char *symname)
{ {
void *symval; void *symval;
char *zeros = NULL;
/* We're looking for a section relative symbol */ /* We're looking for a section relative symbol */
if (!sym->st_shndx || sym->st_shndx >= info->hdr->e_shnum) if (!sym->st_shndx || sym->st_shndx >= info->hdr->e_shnum)
return; return;
/* Handle all-NULL symbols allocated into .bss */
if (info->sechdrs[sym->st_shndx].sh_type & SHT_NOBITS) {
zeros = calloc(1, sym->st_size);
symval = zeros;
} else {
symval = (void *)info->hdr symval = (void *)info->hdr
+ info->sechdrs[sym->st_shndx].sh_offset + info->sechdrs[sym->st_shndx].sh_offset
+ sym->st_value; + sym->st_value;
}
if (sym_is(symname, "__mod_pci_device_table")) if (sym_is(symname, "__mod_pci_device_table"))
do_table(symval, sym->st_size, do_table(symval, sym->st_size,
...@@ -637,6 +662,7 @@ void handle_moddevtable(struct module *mod, struct elf_info *info, ...@@ -637,6 +662,7 @@ void handle_moddevtable(struct module *mod, struct elf_info *info,
do_table(symval, sym->st_size, do_table(symval, sym->st_size,
sizeof(struct ssb_device_id), "ssb", sizeof(struct ssb_device_id), "ssb",
do_ssb_entry, mod); do_ssb_entry, mod);
free(zeros);
} }
/* Now add out buffered information to the generated C source */ /* Now add out buffered information to the generated C source */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment