Montag, 9. April 2012

Linux kernel 2.4.x: modutils and SIGSEGV

Kernel modules compiled using "modern" compilers like GCC 3.4.x create modules that may raise SIGSEGV errors if accessed by modutil tools. Reason is "richer" ELF tables that may contain entries of type ABS lacking a name entry. Example (using readelf with argument -s):

Symbol table '.symtab' contains 473 entries:
   Num:    Value  Size Type    Bind   Vis      Ndx Name
     0: 00000000     0 NOTYPE  LOCAL  DEFAULT  UND
[...]
    16: 00000000     0 FILE    LOCAL  DEFAULT  ABS 
[...] 

ELF tables use some signal values for struct property st_shndx. Vanilla code of modutils (up 2.4.27) does not respect those special values and use them as array index. SHN_ABS is defined as 0xFFF1 (see elf.h) which is decimal 65521. Using that as index into array pointed to by f->sections creates fairly sure SIGSEGV as mentioned earlier.


Following patch applied on file obj_load.c

--- modutils-2.4.27-orig/obj/obj_load.c 2003-10-27 03:59:12.000000000 +0100
+++ modutils-2.4.27/obj/obj_load.c      2012-04-07 17:52:05.000000000 +0200
@@ -232,10 +232,10 @@
            /* Insert all symbols into the hash table.  */
            for (j = 1, ++sym; j < nsym; ++j, ++sym)
              {
-               const char *name;
+               const char *name = "";
                if (sym->st_name)
                  name = strtab+sym->st_name;
-               else
+               else if (sym->st_shndx < SHN_LORESERVE)
                  name = f->sections[sym->st_shndx]->name;

                 {

fixes that issue.