1 /* $OpenBSD: library_mquery.c,v 1.65 2021/03/16 18:03:06 kurt Exp $ */ 2 3 /* 4 * Copyright (c) 2002 Dale Rahn 5 * Copyright (c) 1998 Per Fogelstrom, Opsycon AB 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 17 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 */ 29 30 #define _DYN_LOADER 31 32 #include <sys/types.h> 33 #include <fcntl.h> 34 #include <sys/mman.h> 35 36 #include "syscall.h" 37 #include "archdep.h" 38 #include "resolve.h" 39 #include "sod.h" 40 41 #define PFLAGS(X) ((((X) & PF_R) ? PROT_READ : 0) | \ 42 (((X) & PF_W) ? PROT_WRITE : 0) | \ 43 (((X) & PF_X) ? PROT_EXEC : 0)) 44 45 void 46 _dl_load_list_free(struct load_list *load_list) 47 { 48 struct load_list *next; 49 Elf_Addr align = _dl_pagesz - 1; 50 51 while (load_list != NULL) { 52 if (load_list->start != NULL) 53 _dl_munmap(load_list->start, 54 ((load_list->size) + align) & ~align); 55 next = load_list->next; 56 _dl_free(load_list); 57 load_list = next; 58 } 59 } 60 61 62 void 63 _dl_unload_shlib(elf_object_t *object) 64 { 65 struct dep_node *n; 66 elf_object_t *load_object = object->load_object; 67 68 /* 69 * If our load object has become unreferenced then we lost the 70 * last group reference to it, so the entire group should be taken 71 * down. The current object is somewhere below load_object in 72 * the child_vec tree, so it'll get cleaned up by the recursion. 73 * That means we can just switch here to the load object. 74 */ 75 if (load_object != object && OBJECT_REF_CNT(load_object) == 0 && 76 (load_object->status & STAT_UNLOADED) == 0) { 77 DL_DEB(("unload_shlib switched from %s to %s\n", 78 object->load_name, load_object->load_name)); 79 object = load_object; 80 goto unload; 81 } 82 83 DL_DEB(("unload_shlib called on %s\n", object->load_name)); 84 if (OBJECT_REF_CNT(object) == 0 && 85 (object->status & STAT_UNLOADED) == 0) { 86 struct object_vector vec; 87 int i; 88 unload: 89 object->status |= STAT_UNLOADED; 90 for (vec = object->child_vec, i = 0; i < vec.len; i++) 91 _dl_unload_shlib(vec.vec[i]); 92 TAILQ_FOREACH(n, &object->grpref_list, next_sib) 93 _dl_unload_shlib(n->data); 94 DL_DEB(("unload_shlib unloading on %s\n", object->load_name)); 95 _dl_load_list_free(object->load_list); 96 _dl_remove_object(object); 97 } 98 } 99 100 101 elf_object_t * 102 _dl_tryload_shlib(const char *libname, int type, int flags) 103 { 104 int libfile, i; 105 struct load_list *ld, *lowld = NULL; 106 elf_object_t *object; 107 Elf_Dyn *dynp = NULL; 108 Elf_Ehdr *ehdr; 109 Elf_Phdr *phdp; 110 Elf_Addr load_end = 0; 111 Elf_Addr align = _dl_pagesz - 1, off, size; 112 Elf_Phdr *ptls = NULL; 113 Elf_Addr relro_addr = 0, relro_size = 0; 114 struct stat sb; 115 char hbuf[4096], *exec_start; 116 size_t exec_size; 117 118 #define ROUND_PG(x) (((x) + align) & ~(align)) 119 #define TRUNC_PG(x) ((x) & ~(align)) 120 121 libfile = _dl_open(libname, O_RDONLY | O_CLOEXEC); 122 if (libfile < 0) { 123 _dl_errno = DL_CANT_OPEN; 124 return(0); 125 } 126 127 if ( _dl_fstat(libfile, &sb) < 0) { 128 _dl_errno = DL_CANT_OPEN; 129 return(0); 130 } 131 132 for (object = _dl_objects; object != NULL; object = object->next) { 133 if (object->dev == sb.st_dev && 134 object->inode == sb.st_ino) { 135 object->obj_flags |= flags & DF_1_GLOBAL; 136 _dl_close(libfile); 137 if (_dl_loading_object == NULL) 138 _dl_loading_object = object; 139 if (object->load_object != _dl_objects && 140 object->load_object != _dl_loading_object) { 141 _dl_link_grpref(object->load_object, 142 _dl_loading_object); 143 } 144 return(object); 145 } 146 } 147 148 _dl_read(libfile, hbuf, sizeof(hbuf)); 149 ehdr = (Elf_Ehdr *)hbuf; 150 if (ehdr->e_ident[0] != ELFMAG0 || ehdr->e_ident[1] != ELFMAG1 || 151 ehdr->e_ident[2] != ELFMAG2 || ehdr->e_ident[3] != ELFMAG3 || 152 ehdr->e_type != ET_DYN || ehdr->e_machine != MACHID) { 153 _dl_close(libfile); 154 _dl_errno = DL_NOT_ELF; 155 return(0); 156 } 157 158 /* Insertion sort */ 159 #define LDLIST_INSERT(ld) do { \ 160 struct load_list **_ld; \ 161 for (_ld = &lowld; *_ld != NULL; _ld = &(*_ld)->next) \ 162 if ((*_ld)->moff > ld->moff) \ 163 break; \ 164 ld->next = *_ld; \ 165 *_ld = ld; \ 166 } while (0) 167 /* 168 * Alright, we might have a winner! 169 * Figure out how much VM space we need and set up the load 170 * list that we'll use to find free VM space. 171 */ 172 phdp = (Elf_Phdr *)(hbuf + ehdr->e_phoff); 173 for (i = 0; i < ehdr->e_phnum; i++, phdp++) { 174 switch (phdp->p_type) { 175 case PT_LOAD: 176 off = (phdp->p_vaddr & align); 177 size = off + phdp->p_filesz; 178 179 if (size != 0) { 180 ld = _dl_malloc(sizeof(struct load_list)); 181 if (ld == NULL) 182 _dl_oom(); 183 ld->start = NULL; 184 ld->size = size; 185 ld->moff = TRUNC_PG(phdp->p_vaddr); 186 ld->foff = TRUNC_PG(phdp->p_offset); 187 ld->prot = PFLAGS(phdp->p_flags); 188 LDLIST_INSERT(ld); 189 } 190 191 if ((PFLAGS(phdp->p_flags) & PROT_WRITE) == 0 || 192 ROUND_PG(size) == ROUND_PG(off + phdp->p_memsz)) 193 break; 194 /* This phdr has a zfod section */ 195 ld = _dl_calloc(1, sizeof(struct load_list)); 196 if (ld == NULL) 197 _dl_oom(); 198 ld->start = NULL; 199 ld->size = ROUND_PG(off + phdp->p_memsz) - 200 ROUND_PG(size); 201 ld->moff = TRUNC_PG(phdp->p_vaddr) + 202 ROUND_PG(size); 203 ld->foff = -1; 204 ld->prot = PFLAGS(phdp->p_flags); 205 LDLIST_INSERT(ld); 206 break; 207 case PT_DYNAMIC: 208 dynp = (Elf_Dyn *)phdp->p_vaddr; 209 break; 210 case PT_TLS: 211 if (phdp->p_filesz > phdp->p_memsz) { 212 _dl_printf("%s: invalid tls data in %s.\n", 213 __progname, libname); 214 _dl_close(libfile); 215 _dl_errno = DL_CANT_LOAD_OBJ; 216 return(0); 217 } 218 if (!_dl_tib_static_done) { 219 ptls = phdp; 220 break; 221 } 222 _dl_printf("%s: unsupported TLS program header in %s\n", 223 __progname, libname); 224 _dl_close(libfile); 225 _dl_errno = DL_CANT_LOAD_OBJ; 226 return(0); 227 default: 228 break; 229 } 230 } 231 232 #define LOFF ((Elf_Addr)lowld->start - lowld->moff) 233 234 retry: 235 exec_start = NULL; 236 exec_size = 0; 237 for (ld = lowld; ld != NULL; ld = ld->next) { 238 off_t foff; 239 int fd, flags; 240 void *res; 241 242 flags = MAP_PRIVATE; 243 244 if (ld->foff < 0) { 245 fd = -1; 246 foff = 0; 247 flags |= MAP_ANON; 248 } else { 249 fd = libfile; 250 foff = ld->foff; 251 } 252 253 if (ld == lowld) { 254 /* 255 * Add PROT_EXEC to force the first allocation in 256 * EXEC region unless it is writable. 257 */ 258 int exec = (ld->prot & PROT_WRITE) ? 0 : PROT_EXEC; 259 if (exec && lowld->start == NULL) 260 lowld->start = _dl_exec_hint; 261 res = _dl_mquery((void *)(LOFF + ld->moff), 262 ROUND_PG(ld->size), ld->prot | exec, flags, 263 fd, foff); 264 if (_dl_mmap_error(res)) 265 goto fail; 266 lowld->start = res; 267 } 268 269 res = _dl_mmap((void *)(LOFF + ld->moff), ROUND_PG(ld->size), 270 ld->prot, flags | MAP_FIXED | __MAP_NOREPLACE, fd, foff); 271 if (_dl_mmap_error(res)) { 272 struct load_list *ll; 273 274 /* Unmap any mappings that we did get in. */ 275 for (ll = lowld; ll != NULL && ll != ld; 276 ll = ll->next) { 277 _dl_munmap(ll->start, ROUND_PG(ll->size)); 278 } 279 280 lowld->start += ROUND_PG(ld->size); 281 goto retry; 282 } 283 284 if ((ld->prot & PROT_EXEC) && exec_start == NULL) { 285 exec_start = (void *)(LOFF + ld->moff); 286 exec_size = ROUND_PG(ld->size); 287 } 288 289 ld->start = res; 290 } 291 292 for (ld = lowld; ld != NULL; ld = ld->next) { 293 /* Zero out everything past the EOF */ 294 if ((ld->prot & PROT_WRITE) != 0 && (ld->size & align) != 0) 295 _dl_memset((char *)ld->start + ld->size, 0, 296 _dl_pagesz - (ld->size & align)); 297 load_end = (Elf_Addr)ld->start + ROUND_PG(ld->size); 298 } 299 300 phdp = (Elf_Phdr *)(hbuf + ehdr->e_phoff); 301 for (i = 0; i < ehdr->e_phnum; i++, phdp++) { 302 if (phdp->p_type == PT_OPENBSD_RANDOMIZE) 303 _dl_arc4randombuf((char *)(phdp->p_vaddr + LOFF), 304 phdp->p_memsz); 305 else if (phdp->p_type == PT_GNU_RELRO) { 306 relro_addr = phdp->p_vaddr + LOFF; 307 relro_size = phdp->p_memsz; 308 } 309 } 310 311 _dl_close(libfile); 312 313 dynp = (Elf_Dyn *)((unsigned long)dynp + LOFF); 314 object = _dl_finalize_object(libname, dynp, 315 (Elf_Phdr *)((char *)lowld->start + ehdr->e_phoff), ehdr->e_phnum, 316 type, (Elf_Addr)lowld->start, LOFF); 317 if (object) { 318 char *soname = (char *)object->Dyn.info[DT_SONAME]; 319 320 object->load_size = (Elf_Addr)load_end - (Elf_Addr)lowld->start; 321 object->load_list = lowld; 322 /* set inode, dev from stat info */ 323 object->dev = sb.st_dev; 324 object->inode = sb.st_ino; 325 object->obj_flags |= flags; 326 object->relro_addr = relro_addr; 327 object->relro_size = relro_size; 328 _dl_set_sod(object->load_name, &object->sod); 329 if (ptls != NULL && ptls->p_memsz) 330 _dl_set_tls(object, ptls, (Elf_Addr)lowld->start, 331 libname); 332 333 /* Request permission for system calls in libc.so's text segment */ 334 if (soname != NULL && 335 _dl_strncmp(soname, "libc.so.", 8) == 0) { 336 if (_dl_msyscall(exec_start, exec_size) == -1) 337 _dl_printf("msyscall %lx %lx error\n", 338 exec_start, exec_size); 339 } 340 } else { 341 _dl_load_list_free(lowld); 342 } 343 return(object); 344 fail: 345 _dl_printf("%s: ld.so mmap failed mapping %s.\n", __progname, libname); 346 _dl_close(libfile); 347 _dl_errno = DL_CANT_MMAP; 348 _dl_load_list_free(lowld); 349 return(0); 350 } 351