file-truncated-while-reading-soname-after-patchelf.patch 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. [PATCH] ldconfig: file truncated while reading soname after patchelf [BZ #23964]
  2. The way loadaddr is computed from the first LOAD segment in process_elf_file
  3. assumes .dynstr is also contained in that segment. That is not necessarily
  4. true, especially for libraries that have been touched by patchelf.
  5. With this patch, the address read from the dynamic segment is checked against
  6. all applicable segments instead of only the first one.
  7. [BZ #23964]
  8. * elf/readelflib.c: Fix resolving of loadaddr for .dynstr vaddr.
  9. ---
  10. elf/readelflib.c | 33 ++++++++++++++++-----------------
  11. 1 file changed, 16 insertions(+), 17 deletions(-)
  12. diff --git a/elf/readelflib.c b/elf/readelflib.c
  13. index 5a1e2dc2df..bc1195c175 100644
  14. --- a/elf/readelflib.c
  15. +++ b/elf/readelflib.c
  16. @@ -98,11 +98,6 @@ process_elf_file (const char *file_name, const char *lib, int *flag,
  17. switch (segment->p_type)
  18. {
  19. - case PT_LOAD:
  20. - if (loadaddr == (ElfW(Addr)) -1)
  21. - loadaddr = segment->p_vaddr - segment->p_offset;
  22. - break;
  23. -
  24. case PT_DYNAMIC:
  25. if (dynamic_addr)
  26. error (0, 0, _("more than one dynamic segment\n"));
  27. @@ -176,11 +171,6 @@ process_elf_file (const char *file_name, const char *lib, int *flag,
  28. }
  29. }
  30. - if (loadaddr == (ElfW(Addr)) -1)
  31. - {
  32. - /* Very strange. */
  33. - loadaddr = 0;
  34. - }
  35. /* Now we can read the dynamic sections. */
  36. if (dynamic_size == 0)
  37. @@ -190,22 +180,31 @@ process_elf_file (const char *file_name, const char *lib, int *flag,
  38. check_ptr (dynamic_segment);
  39. /* Find the string table. */
  40. - dynamic_strings = NULL;
  41. for (dyn_entry = dynamic_segment; dyn_entry->d_tag != DT_NULL;
  42. ++dyn_entry)
  43. {
  44. check_ptr (dyn_entry);
  45. if (dyn_entry->d_tag == DT_STRTAB)
  46. - {
  47. - dynamic_strings = (char *) (file_contents + dyn_entry->d_un.d_val - loadaddr);
  48. - check_ptr (dynamic_strings);
  49. - break;
  50. - }
  51. + break;
  52. }
  53. - if (dynamic_strings == NULL)
  54. + for (i = 0, segment = elf_pheader;i < elf_header->e_phnum; i++, segment++)
  55. + {
  56. + ElfW(Addr) vaddr = dyn_entry->d_un.d_ptr;
  57. + if (segment->p_type == PT_LOAD &&
  58. + vaddr >= segment->p_vaddr &&
  59. + vaddr < segment->p_vaddr + segment->p_filesz)
  60. + {
  61. + loadaddr = segment->p_vaddr - segment->p_offset;
  62. + break;
  63. + }
  64. + }
  65. + if (loadaddr == (ElfW(Addr)) -1)
  66. return 1;
  67. + dynamic_strings = (char *) (file_contents + dyn_entry->d_un.d_val - loadaddr);
  68. + check_ptr (dynamic_strings);
  69. +
  70. /* Now read the DT_NEEDED and DT_SONAME entries. */
  71. for (dyn_entry = dynamic_segment; dyn_entry->d_tag != DT_NULL;
  72. ++dyn_entry)
  73. --
  74. 2.19.2