bash-4.0-nobits.patch 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. diff -up bash-4.0/execute_cmd.c.nobits bash-4.0/execute_cmd.c
  2. --- bash-4.0/execute_cmd.c.nobits 2009-08-11 11:53:38.000000000 +0200
  3. +++ bash-4.0/execute_cmd.c 2009-08-14 16:18:18.000000000 +0200
  4. @@ -4747,6 +4747,7 @@ shell_execve (command, args, env)
  5. && memcmp (sample, ELFMAG, SELFMAG) == 0)
  6. {
  7. off_t offset = -1;
  8. + int dynamic_nobits = 0;
  9. /* It is an ELF file. Now determine whether it is dynamically
  10. linked and if yes, get the offset of the interpreter
  11. @@ -4756,13 +4757,61 @@ shell_execve (command, args, env)
  12. {
  13. Elf32_Ehdr ehdr;
  14. Elf32_Phdr *phdr;
  15. - int nphdr;
  16. + Elf32_Shdr *shdr;
  17. + int nphdr, nshdr;
  18. /* We have to copy the data since the sample buffer
  19. might not be aligned correctly to be accessed as
  20. an Elf32_Ehdr struct. */
  21. memcpy (&ehdr, sample, sizeof (Elf32_Ehdr));
  22. + nshdr = ehdr.e_shnum;
  23. + shdr = (Elf32_Shdr *) malloc (nshdr * ehdr.e_shentsize);
  24. +
  25. + if (shdr != NULL)
  26. + {
  27. +#ifdef HAVE_PREAD
  28. + sample_len = pread (fd, shdr, nshdr * ehdr.e_shentsize,
  29. + ehdr.e_shoff);
  30. +#else
  31. + if (lseek (fd, ehdr.e_shoff, SEEK_SET) != -1)
  32. + sample_len = read (fd, shdr,
  33. + nshdr * ehdr.e_shentsize);
  34. + else
  35. + sample_len = -1;
  36. +#endif
  37. + if (sample_len == nshdr * ehdr.e_shentsize)
  38. + {
  39. + char *strings = (char *) malloc (shdr[ehdr.e_shstrndx].sh_size);
  40. + if (strings != NULL)
  41. + {
  42. +#ifdef HAVE_PREAD
  43. + sample_len = pread (fd, strings,
  44. + shdr[ehdr.e_shstrndx].sh_size,
  45. + shdr[ehdr.e_shstrndx].sh_offset);
  46. +#else
  47. + if (lseek (fd, shdr[ehdr.e_shstrndx].sh_offset,
  48. + SEEK_SET) != -1)
  49. + sample_len = read (fd, strings,
  50. + shdr[ehdr.e_shstrndx].sh_size);
  51. + else
  52. + sample_len = -1;
  53. +#endif
  54. + if (sample_len == shdr[ehdr.e_shstrndx].sh_size)
  55. + while (nshdr-- > 0)
  56. + if (strcmp (strings + shdr[nshdr].sh_name,
  57. + ".interp") == 0 &&
  58. + shdr[nshdr].sh_type == SHT_NOBITS)
  59. + {
  60. + dynamic_nobits++;
  61. + break;
  62. + }
  63. + free (strings);
  64. + }
  65. + }
  66. + free (shdr);
  67. + }
  68. +
  69. nphdr = ehdr.e_phnum;
  70. phdr = (Elf32_Phdr *) malloc (nphdr * ehdr.e_phentsize);
  71. if (phdr != NULL)
  72. @@ -4792,13 +4841,60 @@ shell_execve (command, args, env)
  73. {
  74. Elf64_Ehdr ehdr;
  75. Elf64_Phdr *phdr;
  76. - int nphdr;
  77. + Elf64_Shdr *shdr;
  78. + int nphdr, nshdr;
  79. /* We have to copy the data since the sample buffer
  80. might not be aligned correctly to be accessed as
  81. an Elf64_Ehdr struct. */
  82. memcpy (&ehdr, sample, sizeof (Elf64_Ehdr));
  83. + nshdr = ehdr.e_shnum;
  84. + shdr = (Elf64_Shdr *) malloc (nshdr * ehdr.e_shentsize);
  85. + if (shdr != NULL)
  86. + {
  87. +#ifdef HAVE_PREAD
  88. + sample_len = pread (fd, shdr, nshdr * ehdr.e_shentsize,
  89. + ehdr.e_shoff);
  90. +#else
  91. + if (lseek (fd, ehdr.e_shoff, SEEK_SET) != -1)
  92. + sample_len = read (fd, shdr,
  93. + nshdr * ehdr.e_shentsize);
  94. + else
  95. + sample_len = -1;
  96. +#endif
  97. + if (sample_len == nshdr * ehdr.e_shentsize)
  98. + {
  99. + char *strings = (char *) malloc (shdr[ehdr.e_shstrndx].sh_size);
  100. + if (strings != NULL)
  101. + {
  102. +#ifdef HAVE_PREAD
  103. + sample_len = pread (fd, strings,
  104. + shdr[ehdr.e_shstrndx].sh_size,
  105. + shdr[ehdr.e_shstrndx].sh_offset);
  106. +#else
  107. + if (lseek (fd, shdr[ehdr.e_shstrndx].sh_offset,
  108. + SEEK_SET) != -1)
  109. + sample_len = read (fd, strings,
  110. + shdr[ehdr.e_shstrndx].sh_size);
  111. + else
  112. + sample_len = -1;
  113. +#endif
  114. + if (sample_len == shdr[ehdr.e_shstrndx].sh_size)
  115. + while (nshdr-- > 0)
  116. + if (strcmp (strings + shdr[nshdr].sh_name,
  117. + ".interp") == 0 &&
  118. + shdr[nshdr].sh_type == SHT_NOBITS)
  119. + {
  120. + dynamic_nobits++;
  121. + break;
  122. + }
  123. + free (strings);
  124. + }
  125. + }
  126. + free (shdr);
  127. + }
  128. +
  129. nphdr = ehdr.e_phnum;
  130. phdr = (Elf64_Phdr *) malloc (nphdr * ehdr.e_phentsize);
  131. if (phdr != NULL)
  132. @@ -4858,8 +4954,15 @@ shell_execve (command, args, env)
  133. {
  134. close (fd);
  135. errno = i;
  136. - sys_error ("%s: %s: bad ELF interpreter", command,
  137. - interp);
  138. + if (dynamic_nobits > 0)
  139. + {
  140. + sys_error ("%s: bad ELF interpreter", command);
  141. + }
  142. + else
  143. + {
  144. + sys_error ("%s: %s: bad ELF interpreter", command,
  145. + interp);
  146. + }
  147. free (interp);
  148. return (EX_NOEXEC);
  149. }