|
| 1 | +From 6edeea11b1f8fb5df9e421834da6f2d5c2bec21c Mon Sep 17 00:00:00 2001 |
| 2 | +From: Keno Fischer < [email protected]> |
| 3 | +Date: Sat, 11 Jan 2025 19:06:34 +0000 |
| 4 | +Subject: [PATCH] dl-load: Reject musl-linked libraries |
| 5 | + |
| 6 | +This prevents the glibc dynamic linker from considering shared |
| 7 | +libraries linked against musl (similar to how it would reject |
| 8 | +libraries for other architectures). |
| 9 | + |
| 10 | +See https://github.com/JuliaPackaging/BinaryBuilder.jl/issues/297 |
| 11 | +--- |
| 12 | + elf/dl-load.c | 80 ++++++++++++++++++++++- |
| 13 | + elf/ldconfig.c | 10 +++ |
| 14 | + sysdeps/generic/ldconfig.h | 1 + |
| 15 | + sysdeps/unix/sysv/linux/x86_64/ldconfig.h | 3 +- |
| 16 | + 4 files changed, 92 insertions(+), 2 deletions(-) |
| 17 | + |
| 18 | +diff --git a/elf/dl-load.c b/elf/dl-load.c |
| 19 | +index 70fe78f3e7..aadc810a45 100644 |
| 20 | +--- a/elf/dl-load.c |
| 21 | ++++ b/elf/dl-load.c |
| 22 | +@@ -72,6 +72,7 @@ |
| 23 | + # define MAP_BASE_ADDR(l) 0 |
| 24 | + #endif |
| 25 | + |
| 26 | ++#include <ldconfig.h> |
| 27 | + |
| 28 | + #include <endian.h> |
| 29 | + #if BYTE_ORDER == BIG_ENDIAN |
| 30 | +@@ -1649,6 +1650,60 @@ print_search_path (struct r_search_path_elem **list, |
| 31 | + else |
| 32 | + _dl_debug_printf_c ("\t\t(%s)\n", what); |
| 33 | + } |
| 34 | ++ |
| 35 | ++struct known_names |
| 36 | ++{ |
| 37 | ++ const char *soname; |
| 38 | ++ int flag; |
| 39 | ++}; |
| 40 | ++ |
| 41 | ++static struct known_names known_libs[] = |
| 42 | ++{ |
| 43 | ++#ifdef SYSDEP_KNOWN_LIBRARY_NAMES |
| 44 | ++ SYSDEP_KNOWN_LIBRARY_NAMES |
| 45 | ++#endif |
| 46 | ++}; |
| 47 | ++ |
| 48 | ++static int validate_lib(int fd, unsigned int dynamic_addr, unsigned int dynamic_size) |
| 49 | ++{ |
| 50 | ++ ElfW(Dyn) *dyn_entry; |
| 51 | ++ ElfW(Dyn) *dynamic_segment = alloca(dynamic_size); |
| 52 | ++ char fname[255]; |
| 53 | ++ __lseek (fd, dynamic_addr, SEEK_SET); |
| 54 | ++ if ((size_t) __libc_read (fd, (void *) dynamic_segment, dynamic_size) != dynamic_size) |
| 55 | ++ { |
| 56 | ++ return -2; |
| 57 | ++ } |
| 58 | ++ // Find the string table |
| 59 | ++ unsigned int string_offset = 0; |
| 60 | ++ for (dyn_entry = dynamic_segment; dyn_entry->d_tag != DT_NULL; ++dyn_entry) |
| 61 | ++ { |
| 62 | ++ if (dyn_entry->d_tag == DT_STRTAB) { |
| 63 | ++ string_offset = dyn_entry->d_un.d_val; |
| 64 | ++ } |
| 65 | ++ if (string_offset != 0) { |
| 66 | ++ for (dyn_entry = dynamic_segment; dyn_entry->d_tag != DT_NULL; ++dyn_entry) |
| 67 | ++ { |
| 68 | ++ if (dyn_entry->d_tag == DT_NEEDED) { |
| 69 | ++ __lseek (fd, string_offset + dyn_entry->d_un.d_val, SEEK_SET); |
| 70 | ++ ssize_t nchars = __libc_read (fd, (void *)fname, 254); |
| 71 | ++ if (nchars == -1) |
| 72 | ++ return -2; |
| 73 | ++ fname[nchars] = 0; |
| 74 | ++ for (int j = 0; j < sizeof (known_libs) / sizeof (known_libs [0]); ++j) { |
| 75 | ++ if (strcmp (fname, known_libs [j].soname) == 0) |
| 76 | ++ { |
| 77 | ++ if (known_libs [j].flag == FLAG_ELF_MUSL) |
| 78 | ++ return 1; |
| 79 | ++ } |
| 80 | ++ } |
| 81 | ++ } |
| 82 | ++ } |
| 83 | ++ } |
| 84 | ++ } |
| 85 | ++ return 0; |
| 86 | ++} |
| 87 | ++ |
| 88 | + |
| 89 | + /* Open a file and verify it is an ELF file for this architecture. We |
| 90 | + ignore only ELF files for other architectures. Non-ELF files and |
| 91 | +@@ -1690,6 +1745,8 @@ open_verify (const char *name, struct filebuf *fbp, struct link_map *loader, |
| 92 | + } expected_note = { 4, 16, 1, "GNU" }; |
| 93 | + /* Initialize it to make the compiler happy. */ |
| 94 | + const char *errstring = NULL; |
| 95 | ++ unsigned int dynamic_addr = 0; |
| 96 | ++ unsigned int dynamic_size = 0; |
| 97 | + int errval = 0; |
| 98 | + |
| 99 | + #ifdef SHARED |
| 100 | +@@ -1856,8 +1913,15 @@ open_verify (const char *name, struct filebuf *fbp, struct link_map *loader, |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | ++ dynamic_addr = 0; |
| 105 | ++ dynamic_size = 0; |
| 106 | + /* Check .note.ABI-tag if present. */ |
| 107 | +- for (ph = phdr; ph < &phdr[ehdr->e_phnum]; ++ph) |
| 108 | ++ for (ph = phdr; ph < &phdr[ehdr->e_phnum]; ++ph) { |
| 109 | ++ if (ph->p_type == PT_DYNAMIC) |
| 110 | ++ { |
| 111 | ++ dynamic_addr = ph->p_offset; |
| 112 | ++ dynamic_size = ph->p_filesz; |
| 113 | ++ } |
| 114 | + if (ph->p_type == PT_NOTE && ph->p_filesz >= 32 && ph->p_align >= 4) |
| 115 | + { |
| 116 | + ElfW(Addr) size = ph->p_filesz; |
| 117 | +@@ -1905,6 +1969,20 @@ open_verify (const char *name, struct filebuf *fbp, struct link_map *loader, |
| 118 | + |
| 119 | + break; |
| 120 | + } |
| 121 | ++ } |
| 122 | ++ /* Check the dynamic section */ |
| 123 | ++ if (dynamic_addr != 0) { |
| 124 | ++ int err = validate_lib(fd, dynamic_addr, dynamic_size); |
| 125 | ++ if (err == -2) { |
| 126 | ++ errstring = N_("failed to read file"); |
| 127 | ++ goto call_lose; |
| 128 | ++ } |
| 129 | ++ else if (err != 0) { |
| 130 | ++ __close (fd); |
| 131 | ++ __set_errno (ENOENT); |
| 132 | ++ fd = -1; |
| 133 | ++ } |
| 134 | ++ } |
| 135 | + } |
| 136 | + |
| 137 | + return fd; |
| 138 | +diff --git a/elf/ldconfig.c b/elf/ldconfig.c |
| 139 | +index 453cd6278d..03bd57da92 100644 |
| 140 | +--- a/elf/ldconfig.c |
| 141 | ++++ b/elf/ldconfig.c |
| 142 | +@@ -882,6 +882,16 @@ search_dir (const struct dir_entry *entry) |
| 143 | + add_to_aux_cache (&lstat_buf, flag, osversion, soname); |
| 144 | + } |
| 145 | + |
| 146 | ++ /* Don't try to load MUSL-dependent libraries */ |
| 147 | ++ if ((flag & FLAG_TYPE_MASK) == FLAG_ELF_MUSL) |
| 148 | ++ { |
| 149 | ++ if (real_name != real_file_name) |
| 150 | ++ free (real_name); |
| 151 | ++ if (opt_verbose) |
| 152 | ++ error (0, 0, _("Skipping musl-linked library %s"), file_name); |
| 153 | ++ continue; |
| 154 | ++ } |
| 155 | ++ |
| 156 | + if (soname == NULL) |
| 157 | + soname = implicit_soname (direntry->d_name, flag); |
| 158 | + |
| 159 | +diff --git a/sysdeps/generic/ldconfig.h b/sysdeps/generic/ldconfig.h |
| 160 | +index 43cb9bdb0c..83d3572c34 100644 |
| 161 | +--- a/sysdeps/generic/ldconfig.h |
| 162 | ++++ b/sysdeps/generic/ldconfig.h |
| 163 | +@@ -25,6 +25,7 @@ |
| 164 | + #define FLAG_ELF 0x0001 |
| 165 | + #define FLAG_ELF_LIBC5 0x0002 |
| 166 | + #define FLAG_ELF_LIBC6 0x0003 |
| 167 | ++#define FLAG_ELF_MUSL 0x0004 |
| 168 | + #define FLAG_REQUIRED_MASK 0xff00 |
| 169 | + #define FLAG_SPARC_LIB64 0x0100 |
| 170 | + #define FLAG_IA64_LIB64 0x0200 |
| 171 | +diff --git a/sysdeps/unix/sysv/linux/x86_64/ldconfig.h b/sysdeps/unix/sysv/linux/x86_64/ldconfig.h |
| 172 | +index 6f5b828f44..3bac734e22 100644 |
| 173 | +--- a/sysdeps/unix/sysv/linux/x86_64/ldconfig.h |
| 174 | ++++ b/sysdeps/unix/sysv/linux/x86_64/ldconfig.h |
| 175 | +@@ -23,4 +23,5 @@ |
| 176 | + { "/lib64/ld-linux-x86-64.so.2", FLAG_ELF_LIBC6 }, |
| 177 | + #define SYSDEP_KNOWN_LIBRARY_NAMES \ |
| 178 | + { "libc.so.6", FLAG_ELF_LIBC6 }, \ |
| 179 | +- { "libm.so.6", FLAG_ELF_LIBC6 }, |
| 180 | ++ { "libm.so.6", FLAG_ELF_LIBC6 }, \ |
| 181 | ++ { "libc.musl-x86_64.so.1", FLAG_ELF_MUSL } |
| 182 | +-- |
| 183 | +2.31.0 |
| 184 | + |
0 commit comments