|
| 1 | +From ab1f75bb30336948c3de6a8355994314b3fdc4a3 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Miao Wang < [email protected]> |
| 3 | +Date: Sun, 14 Jan 2024 13:28:41 +0800 |
| 4 | +Subject: [PATCH 09/10] prepend trusted-dirs |
| 5 | + |
| 6 | +and search prepended trusted-dirs before other pathes |
| 7 | +--- |
| 8 | + elf/Makefile | 3 ++- |
| 9 | + elf/dl-load.c | 26 ++++++++++++++++++++++++-- |
| 10 | + 2 files changed, 26 insertions(+), 3 deletions(-) |
| 11 | + |
| 12 | +diff --git a/elf/Makefile b/elf/Makefile |
| 13 | +index c00e2ccf..ed1f709c 100644 |
| 14 | +--- a/elf/Makefile |
| 15 | ++++ b/elf/Makefile |
| 16 | +@@ -1355,9 +1355,10 @@ endif |
| 17 | + $(objpfx)trusted-dirs.h: $(objpfx)trusted-dirs.st; @: |
| 18 | + $(objpfx)trusted-dirs.st: Makefile $(..)Makeconfig |
| 19 | + $(make-target-directory) |
| 20 | +- echo "$(subst :, ,$(default-rpath) $(user-defined-trusted-dirs))" \ |
| 21 | ++ echo "$(subst :, ,$(user-defined-trusted-dirs-pre) $(default-rpath) $(user-defined-trusted-dirs))" \ |
| 22 | + | $(AWK) -f gen-trusted-dirs.awk > ${@:st=T}; |
| 23 | + echo '#define DL_DST_LIB "$(notdir $(slibdir))"' >> ${@:st=T} |
| 24 | ++ echo '#define SYSTEM_DIRS_PRE_COUNT $(words $(subst :, ,$(user-defined-trusted-dirs-pre)))' >> ${@:st=T} |
| 25 | + $(move-if-change) ${@:st=T} ${@:st=h} |
| 26 | + touch $@ |
| 27 | + CPPFLAGS-dl-load.c += -I$(objpfx). -I$(csu-objpfx). |
| 28 | +diff --git a/elf/dl-load.c b/elf/dl-load.c |
| 29 | +index 9a87fda9..d11309f2 100644 |
| 30 | +--- a/elf/dl-load.c |
| 31 | ++++ b/elf/dl-load.c |
| 32 | +@@ -123,6 +123,9 @@ static const size_t system_dirs_len[] = |
| 33 | + SYSTEM_DIRS_LEN |
| 34 | + }; |
| 35 | + #define nsystem_dirs_len array_length (system_dirs_len) |
| 36 | ++#define normal_system_dirs_len (nsystem_dirs_len - SYSTEM_DIRS_PRE_COUNT) |
| 37 | ++ |
| 38 | ++static_assert (SYSTEM_DIRS_PRE_COUNT < nsystem_dirs_len, "should have at least one system dir"); |
| 39 | + |
| 40 | + static bool |
| 41 | + is_trusted_path_normalize (const char *path, size_t len) |
| 42 | +@@ -723,7 +726,7 @@ _dl_init_paths (const char *llp, const char *source, |
| 43 | + |
| 44 | + /* First set up the rest of the default search directory entries. */ |
| 45 | + aelem = __rtld_search_dirs.dirs = (struct r_search_path_elem **) |
| 46 | +- malloc ((nsystem_dirs_len + 1) * sizeof (struct r_search_path_elem *)); |
| 47 | ++ malloc ((nsystem_dirs_len + 2) * sizeof (struct r_search_path_elem *)); |
| 48 | + if (__rtld_search_dirs.dirs == NULL) |
| 49 | + { |
| 50 | + errstring = N_("cannot create search path array"); |
| 51 | +@@ -748,11 +751,17 @@ _dl_init_paths (const char *llp, const char *source, |
| 52 | + pelem = GL(dl_all_dirs) = __rtld_search_dirs.dirs[0]; |
| 53 | + strp = system_dirs; |
| 54 | + idx = 0; |
| 55 | ++ aelem = &__rtld_search_dirs.dirs[normal_system_dirs_len + 1]; |
| 56 | + |
| 57 | + do |
| 58 | + { |
| 59 | + size_t cnt; |
| 60 | + |
| 61 | ++ if (aelem == &__rtld_search_dirs.dirs[nsystem_dirs_len + 1]){ |
| 62 | ++ *aelem = NULL; |
| 63 | ++ aelem = &__rtld_search_dirs.dirs[0]; |
| 64 | ++ } |
| 65 | ++ |
| 66 | + *aelem++ = pelem; |
| 67 | + |
| 68 | + pelem->what = "system search path"; |
| 69 | +@@ -2022,9 +2031,22 @@ _dl_map_object (struct link_map *loader, const char *name, |
| 70 | + |
| 71 | + fd = -1; |
| 72 | + |
| 73 | ++ if (SYSTEM_DIRS_PRE_COUNT > 0 |
| 74 | ++ && ((l = loader ?: GL(dl_ns)[nsid]._ns_loaded) == NULL |
| 75 | ++ || __glibc_likely (!(l->l_flags_1 & DF_1_NODEFLIB))) |
| 76 | ++ && __rtld_search_dirs.dirs != (void *) -1) |
| 77 | ++ { |
| 78 | ++ struct r_search_path_struct pre_sp = { |
| 79 | ++ .dirs = &__rtld_search_dirs.dirs[normal_system_dirs_len + 1], |
| 80 | ++ .malloced = 0, |
| 81 | ++ }; |
| 82 | ++ fd = open_path (name, namelen, mode, &pre_sp, |
| 83 | ++ &realname, &fb, l, LA_SER_DEFAULT, &found_other_class); |
| 84 | ++ } |
| 85 | ++ |
| 86 | + /* When the object has the RUNPATH information we don't use any |
| 87 | + RPATHs. */ |
| 88 | +- if (loader == NULL || loader->l_info[DT_RUNPATH] == NULL) |
| 89 | ++ if (fd == -1 && (loader == NULL || loader->l_info[DT_RUNPATH] == NULL)) |
| 90 | + { |
| 91 | + /* This is the executable's map (if there is one). Make sure that |
| 92 | + we do not look at it twice. */ |
| 93 | +-- |
| 94 | +2.43.0 |
| 95 | + |
0 commit comments