correctly handle non-matching symbols in dladdr
authorRich Felker <dalias@aerifal.cx>
Wed, 27 Jun 2018 19:32:09 +0000 (15:32 -0400)
committerRich Felker <dalias@aerifal.cx>
Wed, 27 Jun 2018 19:57:57 +0000 (15:57 -0400)
based on patch by Axel Siebenborn, with fixes discussed on the mailing
list after submission and and rebased around the UB fix in commit
e829695fcc880f8578c2b964ea2d090f0016c9d7.

avoid spurious symbol matches by dladdr beyond symbol size. for
symbols with a size recorded, only match if the queried address lies
within the address range determined by the symbol address and size.
for symbols with no size recorded, the old closest-match behavior is
kept, as long as there is no intervening symbol with a recorded size.

the case where no symbol is matched, but the address does lie within
the memory range of a shared object, is specified as success. fix the
return value and produce a valid (with null dli_sname and dli_saddr)
Dl_info structure.

ldso/dynlink.c

index 6b05b5e0613c29adf4392b2fe45e65dcd85c5099..31c50609b8bb0793c4c1a7a51f7007eed9c6c2d4 100644 (file)
@@ -1978,6 +1978,11 @@ int dladdr(const void *addr_arg, Dl_info *info)
                        size_t symaddr = (size_t)laddr(p, sym->st_value);
                        if (symaddr > addr || symaddr < best)
                                continue;
+                       if (sym->st_size && symaddr+sym->st_size <= addr) {
+                               best = 0;
+                               bestsym = 0;
+                               continue;
+                       }
                        best = symaddr;
                        bestsym = sym;
                        if (addr == symaddr)
@@ -1985,13 +1990,17 @@ int dladdr(const void *addr_arg, Dl_info *info)
                }
        }
 
-       if (!best) return 0;
+       info->dli_fname = p->name;
+       info->dli_fbase = p->map;
+
+       if (!best) {
+               info->dli_sname = 0;
+               info->dli_saddr = 0;
+               return 1;
+       }
 
        if (DL_FDPIC && (bestsym->st_info&0xf) == STT_FUNC)
                best = (size_t)(p->funcdescs + (bestsym - p->syms));
-
-       info->dli_fname = p->name;
-       info->dli_fbase = p->map;
        info->dli_sname = strings + bestsym->st_name;
        info->dli_saddr = (void *)best;