This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] Skip PT_DYNAMIC segment if its p_filesz == 0 [BZ #22101]


On 9/25/17, Zack Weinberg <zackw@panix.com> wrote:
> On Mon, Sep 25, 2017 at 8:33 PM, H.J. Lu <hjl.tools@gmail.com> wrote:
>> ELF object generated with "objcopy --only-keep-debug" has
>>
>> Type     Offset  VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
>> DYNAMIC  0x0+e28 0x0+200e40 0x0+200e40 0x0+    0x0+1a0 RW  0x8
>>
>> with 0 file size. ld.so should skip such PT_DYNAMIC segments.
>
> Why should ld.so skip such PT_DYNAMIC segments?  It seems like loading
> empty segments should be harmless, and ...

Not all valid segments can have zero p_filesz.  The valid PT_DYNAMIC  segment
should have non-zero p_filesz.

>> +  void *h = dlopen ("tst-debug1mod1.so", RTLD_LAZY);
>> +  if (h != NULL)
>> +    {
>> +      puts ("shouldn't load tst-debug1mod1.so");
>
> ... the implications of this test are scary: I suspect there is code
> out there that will break if dlopen starts returning NULL on

My change doesn't simply return NULL.  It just skips PT_DYNAMIC segments
with zero p_filesz.  dlopen returns NULL because invalid PT_DYNAMIC segment.

> completely empty shared objects.

Even completely empty shared objects should have valid PT_DYNAMIC segment:

[hjl@gnu-efi-2 tmp]$ cat foo.s
[hjl@gnu-efi-2 tmp]$ gcc -c foo.s
[hjl@gnu-efi-2 tmp]$ ld -shared foo.o
[hjl@gnu-efi-2 tmp]$ readelf -lW a.out

Elf file type is DYN (Shared object file)
Entry point 0x1f1
There are 4 program headers, starting at offset 64

Program Headers:
  Type           Offset   VirtAddr           PhysAddr
FileSiz  MemSiz   Flg Align
  LOAD           0x000000 0x0000000000000000 0x0000000000000000
0x0001f8 0x0001f8 R   0x200000
  LOAD           0x000f40 0x0000000000200f40 0x0000000000200f40
0x0000c0 0x0000c0 RW  0x200000
  DYNAMIC        0x000f40 0x0000000000200f40 0x0000000000200f40
0x0000c0 0x0000c0 RW  0x8
  GNU_RELRO      0x000f40 0x0000000000200f40 0x0000000000200f40
0x0000c0 0x0000c0 R   0x1

 Section to Segment mapping:
  Segment Sections...
   00     .hash .gnu.hash .dynsym .dynstr
   01     .dynamic
   02     .dynamic
   03     .dynamic
[hjl@gnu-efi-2 tmp]$
[hjl@gnu-efi-2 tmp]$ readelf -d a.out

Dynamic section at offset 0xf40 contains 7 entries:
  Tag        Type                         Name/Value
 0x0000000000000004 (HASH)               0x120
 0x000000006ffffef5 (GNU_HASH)           0x148
 0x0000000000000005 (STRTAB)             0x1d8
 0x0000000000000006 (SYMTAB)             0x178
 0x000000000000000a (STRSZ)              25 (bytes)
 0x000000000000000b (SYMENT)             24 (bytes)
 0x0000000000000000 (NULL)               0x0
[hjl@gnu-efi-2 tmp]$


-- 
H.J.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]