0002-poison-system-directories.patch 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. From a6d90a842e349545ceb27f022317618dd99378ce Mon Sep 17 00:00:00 2001
  2. From: Romain Naour <romain.naour@gmail.com>
  3. Date: Fri, 25 Dec 2015 11:45:38 +0100
  4. Subject: [PATCH] poison-system-directories
  5. Patch adapted to binutils 2.23.2 and extended to use
  6. BR_COMPILER_PARANOID_UNSAFE_PATH by Thomas Petazzoni.
  7. [Waldemar: rebase on top of 2.39]
  8. Signed-off-by: Waldemar Brodkorb <wbx@openadk.org>
  9. [Romain: rebase on top of 2.33.1]
  10. Signed-off-by: Romain Naour <romain.naour@gmail.com>
  11. [Gustavo: adapt to binutils 2.25]
  12. Signed-off-by: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
  13. Signed-off-by: Gustavo Zacarias <gustavo@zacarias.com.ar>
  14. Upstream-Status: Inappropriate [distribution: codesourcery]
  15. Upstream: N/A [Buildroot specific]
  16. Patch originally created by Mark Hatle, forward-ported to
  17. binutils 2.21 by Scott Garman.
  18. purpose: warn for uses of system directories when cross linking
  19. Code Merged from Sourcery G++ binutils 2.19 - 4.4-277
  20. 2008-07-02 Joseph Myers <joseph@codesourcery.com>
  21. ld/
  22. * ld.h (args_type): Add error_poison_system_directories.
  23. * ld.texinfo (--error-poison-system-directories): Document.
  24. * ldfile.c (ldfile_add_library_path): Check
  25. command_line.error_poison_system_directories.
  26. * ldmain.c (main): Initialize
  27. command_line.error_poison_system_directories.
  28. * lexsup.c (enum option_values): Add
  29. OPTION_ERROR_POISON_SYSTEM_DIRECTORIES.
  30. (ld_options): Add --error-poison-system-directories.
  31. (parse_args): Handle new option.
  32. 2007-06-13 Joseph Myers <joseph@codesourcery.com>
  33. ld/
  34. * config.in: Regenerate.
  35. * ld.h (args_type): Add poison_system_directories.
  36. * ld.texinfo (--no-poison-system-directories): Document.
  37. * ldfile.c (ldfile_add_library_path): Check
  38. command_line.poison_system_directories.
  39. * ldmain.c (main): Initialize
  40. command_line.poison_system_directories.
  41. * lexsup.c (enum option_values): Add
  42. OPTION_NO_POISON_SYSTEM_DIRECTORIES.
  43. (ld_options): Add --no-poison-system-directories.
  44. (parse_args): Handle new option.
  45. 2007-04-20 Joseph Myers <joseph@codesourcery.com>
  46. Merge from Sourcery G++ binutils 2.17:
  47. 2007-03-20 Joseph Myers <joseph@codesourcery.com>
  48. Based on patch by Mark Hatle <mark.hatle@windriver.com>.
  49. ld/
  50. * configure.ac (--enable-poison-system-directories): New option.
  51. * configure, config.in: Regenerate.
  52. * ldfile.c (ldfile_add_library_path): If
  53. ENABLE_POISON_SYSTEM_DIRECTORIES defined, warn for use of /lib,
  54. /usr/lib, /usr/local/lib or /usr/X11R6/lib.
  55. Signed-off-by: Mark Hatle <mark.hatle@windriver.com>
  56. Signed-off-by: Scott Garman <scott.a.garman@intel.com>
  57. ---
  58. ld/config.in | 3 +++
  59. ld/configure | 14 ++++++++++++++
  60. ld/configure.ac | 10 ++++++++++
  61. ld/ld.h | 8 ++++++++
  62. ld/ld.texi | 12 ++++++++++++
  63. ld/ldfile.c | 17 +++++++++++++++++
  64. ld/ldlex.h | 2 ++
  65. ld/ldmain.c | 2 ++
  66. ld/lexsup.c | 21 +++++++++++++++++++++
  67. 9 files changed, 89 insertions(+)
  68. diff --git a/ld/config.in b/ld/config.in
  69. index ad0dc6a106c..d21edaddce7 100644
  70. --- a/ld/config.in
  71. +++ b/ld/config.in
  72. @@ -58,6 +58,9 @@
  73. language is requested. */
  74. #undef ENABLE_NLS
  75. +/* Define to warn for use of native system library directories */
  76. +#undef ENABLE_POISON_SYSTEM_DIRECTORIES
  77. +
  78. /* Additional extension a shared object might have. */
  79. #undef EXTRA_SHLIB_EXTENSION
  80. diff --git a/ld/configure b/ld/configure
  81. index 43b44be1dc6..28f054ea3f8 100755
  82. --- a/ld/configure
  83. +++ b/ld/configure
  84. @@ -839,6 +839,7 @@ with_lib_path
  85. enable_targets
  86. enable_64_bit_bfd
  87. with_sysroot
  88. +enable_poison_system_directories
  89. enable_gold
  90. enable_got
  91. enable_compressed_debug_sections
  92. @@ -1521,6 +1522,8 @@ Optional Features:
  93. --enable-checking enable run-time checks
  94. --enable-targets alternative target configurations
  95. --enable-64-bit-bfd 64-bit support (on hosts with narrower word sizes)
  96. + --enable-poison-system-directories
  97. + warn for use of native system library directories
  98. --enable-gold[=ARG] build gold [ARG={default,yes,no}]
  99. --enable-got=<type> GOT handling scheme (target, single, negative,
  100. multigot)
  101. @@ -15504,7 +15507,18 @@ else
  102. fi
  103. +# Check whether --enable-poison-system-directories was given.
  104. +if test "${enable_poison_system_directories+set}" = set; then :
  105. + enableval=$enable_poison_system_directories;
  106. +else
  107. + enable_poison_system_directories=no
  108. +fi
  109. +
  110. +if test "x${enable_poison_system_directories}" = "xyes"; then
  111. +$as_echo "#define ENABLE_POISON_SYSTEM_DIRECTORIES 1" >>confdefs.h
  112. +
  113. +fi
  114. # Check whether --enable-got was given.
  115. if test "${enable_got+set}" = set; then :
  116. diff --git a/ld/configure.ac b/ld/configure.ac
  117. index 77edac3258c..a74dac63038 100644
  118. --- a/ld/configure.ac
  119. +++ b/ld/configure.ac
  120. @@ -103,6 +103,16 @@ AC_SUBST(use_sysroot)
  121. AC_SUBST(TARGET_SYSTEM_ROOT)
  122. AC_SUBST(TARGET_SYSTEM_ROOT_DEFINE)
  123. +AC_ARG_ENABLE([poison-system-directories],
  124. + AS_HELP_STRING([--enable-poison-system-directories],
  125. + [warn for use of native system library directories]),,
  126. + [enable_poison_system_directories=no])
  127. +if test "x${enable_poison_system_directories}" = "xyes"; then
  128. + AC_DEFINE([ENABLE_POISON_SYSTEM_DIRECTORIES],
  129. + [1],
  130. + [Define to warn for use of native system library directories])
  131. +fi
  132. +
  133. dnl Use --enable-gold to decide if this linker should be the default.
  134. dnl "install_as_default" is set to false if gold is the default linker.
  135. dnl "installed_linker" is the installed BFD linker name.
  136. diff --git a/ld/ld.h b/ld/ld.h
  137. index 05649ff61b8..1818c227473 100644
  138. --- a/ld/ld.h
  139. +++ b/ld/ld.h
  140. @@ -163,6 +163,14 @@ typedef struct
  141. in the linker script. */
  142. bool force_group_allocation;
  143. + /* If TRUE (the default) warn for uses of system directories when
  144. + cross linking. */
  145. + bool poison_system_directories;
  146. +
  147. + /* If TRUE (default FALSE) give an error for uses of system
  148. + directories when cross linking instead of a warning. */
  149. + bool error_poison_system_directories;
  150. +
  151. /* Big or little endian as set on command line. */
  152. enum endian_enum endian;
  153. diff --git a/ld/ld.texi b/ld/ld.texi
  154. index db3a9f09b45..9dacc934f80 100644
  155. --- a/ld/ld.texi
  156. +++ b/ld/ld.texi
  157. @@ -2949,6 +2949,18 @@ string identifying the original linked file does not change.
  158. Passing @code{none} for @var{style} disables the setting from any
  159. @code{--build-id} options earlier on the command line.
  160. +@kindex --no-poison-system-directories
  161. +@item --no-poison-system-directories
  162. +Do not warn for @option{-L} options using system directories such as
  163. +@file{/usr/lib} when cross linking. This option is intended for use
  164. +in chroot environments when such directories contain the correct
  165. +libraries for the target system rather than the host.
  166. +
  167. +@kindex --error-poison-system-directories
  168. +@item --error-poison-system-directories
  169. +Give an error instead of a warning for @option{-L} options using
  170. +system directories when cross linking.
  171. +
  172. @kindex --package-metadata=@var{JSON}
  173. @item --package-metadata=@var{JSON}
  174. Request the creation of a @code{.note.package} ELF note section. The
  175. diff --git a/ld/ldfile.c b/ld/ldfile.c
  176. index b8fd4e5d8e0..1f1d8e23bc9 100644
  177. --- a/ld/ldfile.c
  178. +++ b/ld/ldfile.c
  179. @@ -117,6 +117,23 @@ ldfile_add_library_path (const char *name, bool cmdline)
  180. new_dirs->name = concat (ld_sysroot, name + strlen ("$SYSROOT"), (const char *) NULL);
  181. else
  182. new_dirs->name = xstrdup (name);
  183. +
  184. +#ifdef ENABLE_POISON_SYSTEM_DIRECTORIES
  185. + if (command_line.poison_system_directories
  186. + && ((!strncmp (name, "/lib", 4))
  187. + || (!strncmp (name, "/usr/lib", 8))
  188. + || (!strncmp (name, "/usr/local/lib", 14))
  189. + || (!strncmp (name, "/usr/X11R6/lib", 14))))
  190. + {
  191. + if (command_line.error_poison_system_directories)
  192. + einfo (_("%X%P: error: library search path \"%s\" is unsafe for "
  193. + "cross-compilation\n"), name);
  194. + else
  195. + einfo (_("%P: warning: library search path \"%s\" is unsafe for "
  196. + "cross-compilation\n"), name);
  197. + }
  198. +#endif
  199. +
  200. }
  201. /* Try to open a BFD for a lang_input_statement. */
  202. diff --git a/ld/ldlex.h b/ld/ldlex.h
  203. index 0538f0a06a1..d1de3aec23d 100644
  204. --- a/ld/ldlex.h
  205. +++ b/ld/ldlex.h
  206. @@ -166,6 +166,8 @@ enum option_values
  207. OPTION_CTF_VARIABLES,
  208. OPTION_NO_CTF_VARIABLES,
  209. OPTION_CTF_SHARE_TYPES,
  210. + OPTION_NO_POISON_SYSTEM_DIRECTORIES,
  211. + OPTION_ERROR_POISON_SYSTEM_DIRECTORIES,
  212. OPTION_WARN_EXECSTACK,
  213. OPTION_NO_WARN_EXECSTACK,
  214. OPTION_WARN_RWX_SEGMENTS,
  215. diff --git a/ld/ldmain.c b/ld/ldmain.c
  216. index 9290a189b0d..e2e3074e872 100644
  217. --- a/ld/ldmain.c
  218. +++ b/ld/ldmain.c
  219. @@ -321,6 +321,8 @@ main (int argc, char **argv)
  220. command_line.warn_mismatch = true;
  221. command_line.warn_search_mismatch = true;
  222. command_line.check_section_addresses = -1;
  223. + command_line.poison_system_directories = true;
  224. + command_line.error_poison_system_directories = false;
  225. /* We initialize DEMANGLING based on the environment variable
  226. COLLECT_NO_DEMANGLE. The gcc collect2 program will demangle the
  227. diff --git a/ld/lexsup.c b/ld/lexsup.c
  228. index c5960385572..da0a7829914 100644
  229. --- a/ld/lexsup.c
  230. +++ b/ld/lexsup.c
  231. @@ -613,6 +613,14 @@ static const struct ld_option ld_options[] =
  232. " <method> is: share-unconflicted (default),\n"
  233. " share-duplicated"),
  234. TWO_DASHES },
  235. + { {"no-poison-system-directories", no_argument, NULL,
  236. + OPTION_NO_POISON_SYSTEM_DIRECTORIES},
  237. + '\0', NULL, N_("Do not warn for -L options using system directories"),
  238. + TWO_DASHES },
  239. + { {"error-poison-system-directories", no_argument, NULL,
  240. + OPTION_ERROR_POISON_SYSTEM_DIRECTORIES},
  241. + '\0', NULL, N_("Give an error for -L options using system directories"),
  242. + TWO_DASHES },
  243. };
  244. #define OPTION_COUNT ARRAY_SIZE (ld_options)
  245. @@ -625,6 +633,7 @@ parse_args (unsigned argc, char **argv)
  246. int ingroup = 0;
  247. char *default_dirlist = NULL;
  248. char *shortopts;
  249. + char *BR_paranoid_env;
  250. struct option *longopts;
  251. struct option *really_longopts;
  252. int last_optind;
  253. @@ -1692,6 +1701,14 @@ parse_args (unsigned argc, char **argv)
  254. }
  255. break;
  256. + case OPTION_NO_POISON_SYSTEM_DIRECTORIES:
  257. + command_line.poison_system_directories = false;
  258. + break;
  259. +
  260. + case OPTION_ERROR_POISON_SYSTEM_DIRECTORIES:
  261. + command_line.error_poison_system_directories = true;
  262. + break;
  263. +
  264. case OPTION_PUSH_STATE:
  265. input_flags.pushed = xmemdup (&input_flags,
  266. sizeof (input_flags),
  267. @@ -1837,6 +1854,10 @@ parse_args (unsigned argc, char **argv)
  268. command_line.soname = NULL;
  269. }
  270. + BR_paranoid_env = getenv("BR_COMPILER_PARANOID_UNSAFE_PATH");
  271. + if (BR_paranoid_env && strlen(BR_paranoid_env) > 0)
  272. + command_line.error_poison_system_directories = true;
  273. +
  274. while (ingroup)
  275. {
  276. einfo (_("%P: missing --end-group; added as last command line option\n"));
  277. --
  278. 2.40.1