sandbox-2.11-exec-hash.patch 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. From e11815bb7f0656f39e122073e0e3284ec7f5d021 Mon Sep 17 00:00:00 2001
  2. From: Mike Frysinger <vapier@gentoo.org>
  3. Date: Tue, 29 Mar 2016 23:35:44 -0400
  4. Subject: [PATCH] libsandbox: fix symtab walking with some ELFs
  5. MIME-Version: 1.0
  6. Content-Type: text/plain; charset=UTF-8
  7. Content-Transfer-Encoding: 8bit
  8. The strtab assumption works if there is no SysV hash table.
  9. Add logic to handle that scenario.
  10. URL: https://bugs.gentoo.org/578524
  11. Reported-by: Toralf Förster <toralf.foerster@gmx.de>
  12. Signed-off-by: Mike Frysinger <vapier@gentoo.org>
  13. ---
  14. libsandbox/wrapper-funcs/__wrapper_exec.c | 30 +++++++++++++++++-------------
  15. 1 file changed, 17 insertions(+), 13 deletions(-)
  16. diff --git a/libsandbox/wrapper-funcs/__wrapper_exec.c b/libsandbox/wrapper-funcs/__wrapper_exec.c
  17. index f7f51ab..d372366 100644
  18. --- a/libsandbox/wrapper-funcs/__wrapper_exec.c
  19. +++ b/libsandbox/wrapper-funcs/__wrapper_exec.c
  20. @@ -83,10 +83,10 @@ static bool sb_check_exec(const char *filename, char *const argv[])
  21. ({ \
  22. Elf##n##_Ehdr *ehdr = (void *)elf; \
  23. Elf##n##_Phdr *phdr = (void *)(elf + ehdr->e_phoff); \
  24. - Elf##n##_Addr vaddr, filesz, vsym = 0, vstr = 0; \
  25. - Elf##n##_Off offset, symoff = 0, stroff = 0; \
  26. + Elf##n##_Addr vaddr, filesz, vsym = 0, vstr = 0, vhash = 0; \
  27. + Elf##n##_Off offset, symoff = 0, stroff = 0, hashoff = 0; \
  28. Elf##n##_Dyn *dyn; \
  29. - Elf##n##_Sym *sym; \
  30. + Elf##n##_Sym *sym, *symend; \
  31. uint##n##_t ent_size = 0, str_size = 0; \
  32. bool dynamic = false; \
  33. size_t i; \
  34. @@ -106,6 +106,7 @@ static bool sb_check_exec(const char *filename, char *const argv[])
  35. case DT_SYMENT: ent_size = dyn->d_un.d_val; break; \
  36. case DT_STRTAB: vstr = dyn->d_un.d_val; break; \
  37. case DT_STRSZ: str_size = dyn->d_un.d_val; break; \
  38. + case DT_HASH: vhash = dyn->d_un.d_val; break; \
  39. } \
  40. ++dyn; \
  41. } \
  42. @@ -123,6 +124,8 @@ static bool sb_check_exec(const char *filename, char *const argv[])
  43. symoff = offset + (vsym - vaddr); \
  44. if (vstr >= vaddr && vstr < vaddr + filesz) \
  45. stroff = offset + (vstr - vaddr); \
  46. + if (vhash >= vaddr && vhash < vaddr + filesz) \
  47. + hashoff = offset + (vhash - vaddr); \
  48. } \
  49. \
  50. /* Finally walk the symbol table. This should generally be fast as \
  51. @@ -130,18 +133,20 @@ static bool sb_check_exec(const char *filename, char *const argv[])
  52. * out there do not export any symbols at all. \
  53. */ \
  54. if (symoff && stroff) { \
  55. - sym = (void *)(elf + symoff); \
  56. + /* Hash entries are always 32-bits. */ \
  57. + uint32_t *hashes = (void *)(elf + hashoff); \
  58. /* Nowhere is the # of symbols recorded, or the size of the symbol \
  59. - * table. Instead, we do what glibc does: assume that the string \
  60. - * table always follows the symbol table. This seems like a poor \
  61. - * assumption to make, but glibc has gotten by this long. We could \
  62. - * rely on DT_HASH and walking all the buckets to find the largest \
  63. - * symbol index, but that's also a bit hacky. \
  64. + * table. Instead, we do what glibc does: use the sysv hash table \
  65. + * if it exists, else assume that the string table always directly \
  66. + * follows the symbol table. This seems like a poor assumption to \
  67. + * make, but glibc has gotten by this long. \
  68. * \
  69. * We don't sanity check the ranges here as you aren't executing \
  70. * corrupt programs in the sandbox. \
  71. */ \
  72. - for (i = 0; i < (vstr - vsym) / ent_size; ++i) { \
  73. + sym = (void *)(elf + symoff); \
  74. + symend = vhash ? (sym + hashes[1]) : (void *)(elf + stroff); \
  75. + while (sym < symend) { \
  76. char *symname = (void *)(elf + stroff + sym->st_name); \
  77. if (ELF##n##_ST_VISIBILITY(sym->st_other) == STV_DEFAULT && \
  78. sym->st_shndx != SHN_UNDEF && sym->st_shndx < SHN_LORESERVE && \
  79. @@ -149,9 +154,8 @@ static bool sb_check_exec(const char *filename, char *const argv[])
  80. /* Minor optimization to avoid strcmp. */ \
  81. symname[0] == '_' && symname[1] == '_') { \
  82. /* Blacklist internal C library symbols. */ \
  83. - size_t j; \
  84. - for (j = 0; j < ARRAY_SIZE(libc_alloc_syms); ++j) \
  85. - if (!strcmp(symname, libc_alloc_syms[j])) { \
  86. + for (i = 0; i < ARRAY_SIZE(libc_alloc_syms); ++i) \
  87. + if (!strcmp(symname, libc_alloc_syms[i])) { \
  88. run_in_process = false; \
  89. goto use_trace; \
  90. } \
  91. --
  92. 2.7.4