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