From 98e4cb42ffdf7b36598968a5e31d50c4a1ca73f9 Mon Sep 17 00:00:00 2001 From: John Marino Date: Sun, 30 Nov 2014 12:52:58 +0100 Subject: [PATCH] rtld: Add support for LD_LIBRARY_PATH_FDS env variable This variable allows loading of shared libraries via directory descriptors rather than via library paths. if LD_LIBRARY_PATH_FDS=3:4:12, the directories represented by file descriptors 3, 4, and 12 will be searched for shared libraries before the normal path-based mechanisms are used. This allows the execution of unpriviledge binaries from within a Capsicum sandbox even if they require shared libraries. Note that Capsicum is not available on DragonFly yet. While here, adjust whitespace and rearrange a bit to minimize differences with FreeBSD rtld. Taken from: FreeBSD svn 267678 (20 June 2014) --- libexec/rtld-elf/rtld.1 | 13 +- libexec/rtld-elf/rtld.c | 318 +++++++++++++++++++++------------ libexec/rtld-elf/rtld_printf.c | 12 ++ libexec/rtld-elf/rtld_printf.h | 2 + 4 files changed, 232 insertions(+), 113 deletions(-) diff --git a/libexec/rtld-elf/rtld.1 b/libexec/rtld-elf/rtld.1 index f4cf1bd64b..a871661ff4 100644 --- a/libexec/rtld-elf/rtld.1 +++ b/libexec/rtld-elf/rtld.1 @@ -28,7 +28,7 @@ .\" .\" $FreeBSD$ .\" -.Dd May 6, 2012 +.Dd June 20, 2014 .Dt RTLD 1 .Os .Sh NAME @@ -119,7 +119,7 @@ The .Pa /lib and .Pa /usr/lib -directory unless the referencing object was linked using the +directories, unless the referencing object was linked using the .Dq Fl z Ar nodefaultlib option .El @@ -128,7 +128,8 @@ The .Nm utility recognizes a number of environment variables that can be used to modify -its behaviour as follows: +its behaviour. +for example: .Bl -tag -width ".Ev LD_LIBMAP_DISABLE" .It Ev LD_DUMP_REL_POST If set, @@ -177,6 +178,12 @@ the directories specified by will be searched first followed by the set of built-in standard directories. This variable is unset for set-user-ID and set-group-ID programs. +.Ev LD_LIBRARY_PATH_FDS +A colon separated list of file descriptor numbers for library directories. +This is intended for future use within capsicum +sandboxes, when global namespaces such as the filesystem are unavailable. +It is consulted just after LD_LIBRARY_PATH. +This variable is unset for set-user-ID and set-group-ID programs. .It Ev LD_BIND_NOW When set to a nonempty string, causes .Nm diff --git a/libexec/rtld-elf/rtld.c b/libexec/rtld-elf/rtld.c index f630d6754d..b7756ed318 100644 --- a/libexec/rtld-elf/rtld.c +++ b/libexec/rtld-elf/rtld.c @@ -94,7 +94,7 @@ static bool donelist_check(DoneList *, const Obj_Entry *); static void errmsg_restore(char *); static char *errmsg_save(void); static void *fill_search_info(const char *, size_t, void *); -static char *find_library(const char *, const Obj_Entry *); +static char *find_library(const char *, const Obj_Entry *, int *); static const char *gethints(bool); static void init_dag(Obj_Entry *); static void init_rtld(caddr_t, Elf_Auxinfo **); @@ -118,6 +118,7 @@ static void objlist_push_head(Objlist *, Obj_Entry *); static void objlist_push_tail(Objlist *, Obj_Entry *); static void objlist_put_after(Objlist *, Obj_Entry *, Obj_Entry *); static void objlist_remove(Objlist *, Obj_Entry *); +static int parse_libdir(const char *); static void *path_enumerate(const char *, path_enum_proc, void *); static int relocate_object_dag(Obj_Entry *root, bool bind_now, Obj_Entry *rtldobj, int flags, RtldLockState *lockstate); @@ -132,6 +133,7 @@ static int rtld_dirname_abs(const char *, char *); static void *rtld_dlopen(const char *name, int fd, int mode); static void rtld_exit(void); static char *search_library_path(const char *, const char *); +static char *search_library_pathfds(const char *, const char *, int *); static const void **get_program_var_addr(const char *, RtldLockState *); static void set_program_var(const char *, const void *); static int symlook_default(SymLook *, const Obj_Entry *refobj); @@ -175,10 +177,11 @@ static bool dangerous_ld_env; /* True if environment variables have been used to affect the libraries loaded */ static const char *ld_bind_now; /* Environment variable for immediate binding */ static const char *ld_debug; /* Environment variable for debugging */ -static const char *ld_library_path; /* Environment variable for search path */ +static const char *ld_library_path; /* Environment variable for search path */ +static const char *ld_library_dirs; /* Env variable for library descriptors */ static char *ld_preload; /* Environment variable for libraries to load first */ -static const char *ld_elf_hints_path; /* Environment variable for alternative hints path */ +static const char *ld_elf_hints_path; /* Env var. for alternative hints path */ static const char *ld_tracing; /* Called from ldd to print libs */ static const char *ld_utrace; /* Use utrace() to log events. */ static int (*rtld_functrace)( /* Optional function call tracing hook */ @@ -326,7 +329,6 @@ ld_utrace_log(int event, void *handle, void *mapbase, size_t mapsize, func_ptr_type _rtld(Elf_Addr *sp, func_ptr_type *exit_proc, Obj_Entry **objp) { - Obj_Entry *last_interposer; Elf_Auxinfo *aux_info[AT_COUNT]; int i; int argc; @@ -337,12 +339,12 @@ _rtld(Elf_Addr *sp, func_ptr_type *exit_proc, Obj_Entry **objp) const char *argv0; Objlist_Entry *entry; Obj_Entry *obj; + Obj_Entry *last_interposer; /* marino: DO NOT MOVE THESE VARIABLES TO _rtld Obj_Entry **preload_tail; Objlist initlist; from global to here. It will break the DWARF2 unwind scheme. - The system compilers were unaffected, but not gcc 4.6 */ /* @@ -405,14 +407,15 @@ _rtld(Elf_Addr *sp, func_ptr_type *exit_proc, Obj_Entry **objp) if ( unsetenv("LD_DEBUG") || unsetenv("LD_PRELOAD") || unsetenv("LD_LIBRARY_PATH") + || unsetenv("LD_LIBRARY_PATH_FDS") || unsetenv("LD_ELF_HINTS_PATH") || unsetenv("LD_LIBMAP") || unsetenv("LD_LIBMAP_DISABLE") || unsetenv("LD_LOADFLTR") || unsetenv("LD_SHAREDLIB_BASE") ) { - _rtld_error("environment corrupt; aborting"); - die(); + _rtld_error("environment corrupt; aborting"); + die(); } } __ld_sharedlib_base = _getenv_ld("LD_SHAREDLIB_BASE"); @@ -420,6 +423,7 @@ _rtld(Elf_Addr *sp, func_ptr_type *exit_proc, Obj_Entry **objp) libmap_disable = _getenv_ld("LD_LIBMAP_DISABLE") != NULL; libmap_override = (char *)_getenv_ld("LD_LIBMAP"); ld_library_path = _getenv_ld("LD_LIBRARY_PATH"); + ld_library_dirs = _getenv_ld("LD_LIBRARY_PATH_FDS"); ld_preload = (char *)_getenv_ld("LD_PRELOAD"); ld_elf_hints_path = _getenv_ld("LD_ELF_HINTS_PATH"); ld_loadfltr = _getenv_ld("LD_LOADFLTR") != NULL; @@ -537,7 +541,7 @@ _rtld(Elf_Addr *sp, func_ptr_type *exit_proc, Obj_Entry **objp) strcmp(obj_main->interp, obj_rtld.path) != 0) { free(obj_rtld.path); obj_rtld.path = xstrdup(obj_main->interp); - __progname = obj_rtld.path; + __progname = obj_rtld.path; } digest_dynamic(obj_main, 0); @@ -574,12 +578,12 @@ _rtld(Elf_Addr *sp, func_ptr_type *exit_proc, Obj_Entry **objp) /* Make a list of all objects loaded at startup. */ last_interposer = obj_main; for (obj = obj_list; obj != NULL; obj = obj->next) { - if (obj->z_interpose && obj != obj_main) { - objlist_put_after(&list_main, last_interposer, obj); - last_interposer = obj; - } else { - objlist_push_tail(&list_main, obj); - } + if (obj->z_interpose && obj != obj_main) { + objlist_put_after(&list_main, last_interposer, obj); + last_interposer = obj; + } else { + objlist_push_tail(&list_main, obj); + } obj->refcount++; } @@ -767,7 +771,7 @@ _rtld_bind(Obj_Entry *obj, Elf_Size reloff, void *stack) if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) target = (Elf_Addr)rtld_resolve_ifunc(defobj, def); else - target = (Elf_Addr)(defobj->relocbase + def->st_value); + target = (Elf_Addr)(defobj->relocbase + def->st_value); dbg("\"%s\" in \"%s\" ==> %p in \"%s\"", defobj->strtab + def->st_name, basename(obj->path), @@ -1150,28 +1154,28 @@ digest_dynamic1(Obj_Entry *obj, int early, const Elf_Dyn **dyn_rpath, obj->init = (Elf_Addr) (obj->relocbase + dynp->d_un.d_ptr); break; - case DT_FINI: - obj->fini = (Elf_Addr)(obj->relocbase + dynp->d_un.d_ptr); - break; - case DT_PREINIT_ARRAY: obj->preinit_array = (Elf_Addr)(obj->relocbase + dynp->d_un.d_ptr); break; + case DT_PREINIT_ARRAYSZ: + obj->preinit_array_num = dynp->d_un.d_val / sizeof(Elf_Addr); + break; + case DT_INIT_ARRAY: obj->init_array = (Elf_Addr)(obj->relocbase + dynp->d_un.d_ptr); break; - case DT_FINI_ARRAY: - obj->fini_array = (Elf_Addr)(obj->relocbase + dynp->d_un.d_ptr); + case DT_INIT_ARRAYSZ: + obj->init_array_num = dynp->d_un.d_val / sizeof(Elf_Addr); break; - case DT_PREINIT_ARRAYSZ: - obj->preinit_array_num = dynp->d_un.d_val / sizeof(Elf_Addr); + case DT_FINI: + obj->fini = (Elf_Addr)(obj->relocbase + dynp->d_un.d_ptr); break; - case DT_INIT_ARRAYSZ: - obj->init_array_num = dynp->d_un.d_val / sizeof(Elf_Addr); + case DT_FINI_ARRAY: + obj->fini_array = (Elf_Addr)(obj->relocbase + dynp->d_un.d_ptr); break; case DT_FINI_ARRAYSZ: @@ -1211,8 +1215,8 @@ digest_dynamic1(Obj_Entry *obj, int early, const Elf_Dyn **dyn_rpath, obj->z_nodelete = true; if (dynp->d_un.d_val & DF_1_LOADFLTR) obj->z_loadfltr = true; - if (dynp->d_un.d_val & DF_1_INTERPOSE) - obj->z_interpose = true; + if (dynp->d_un.d_val & DF_1_INTERPOSE) + obj->z_interpose = true; if (dynp->d_un.d_val & DF_1_NODEFLIB) obj->z_nodeflib = true; break; @@ -1487,6 +1491,7 @@ gnu_hash(const char *s) return (h & 0xffffffff); } + /* * Find the library with the given name, and return its full pathname. * The returned string is dynamically allocated. Generates an error @@ -1495,18 +1500,23 @@ gnu_hash(const char *s) * If the second argument is non-NULL, then it refers to an already- * loaded shared object, whose library search path will be searched. * + * If a library is successfully located via LD_LIBRARY_PATH_FDS, its + * descriptor (which is close-on-exec) will be passed out via the third + * argument. + * * The search order is: * DT_RPATH in the referencing file _unless_ DT_RUNPATH is present (1) * DT_RPATH of the main object if DSO without defined DT_RUNPATH (1) * LD_LIBRARY_PATH * DT_RUNPATH in the referencing file - * ldconfig hints (if -z nodefaultlib, filter out /usr/lib from list) - * /usr/lib _unless_ the referencing file is linked with -z nodefaultlib + * ldconfig hints (if -z nodefaultlib, filter out default library directories + * from list) + * /lib:/usr/lib _unless_ the referencing file is linked with -z nodefaultlib * * (1) Handled in digest_dynamic2 - rpath left NULL if runpath defined. */ static char * -find_library(const char *xname, const Obj_Entry *refobj) +find_library(const char *xname, const Obj_Entry *refobj, int *fdp) { char *pathname; char *name; @@ -1541,8 +1551,9 @@ find_library(const char *xname, const Obj_Entry *refobj) (pathname = search_library_path(name, ld_library_path)) != NULL || (objgiven && (pathname = search_library_path(name, refobj->runpath)) != NULL) || - (pathname = search_library_path(name, gethints(nodeflib))) != NULL || - (objgiven && !nodeflib && + (pathname = search_library_pathfds(name, ld_library_dirs, fdp)) != NULL || + (pathname = search_library_path(name, gethints(nodeflib))) != NULL || + (objgiven && !nodeflib && (pathname = search_library_path(name, STANDARD_LIBRARY_PATH)) != NULL)) return (pathname); @@ -2105,29 +2116,34 @@ load_object(const char *name, int fd_u, const Obj_Entry *refobj, int flags) struct stat sb; char *path; + fd = -1; if (name != NULL) { for (obj = obj_list->next; obj != NULL; obj = obj->next) { if (object_match_name(obj, name)) return (obj); } - path = find_library(name, refobj); + path = find_library(name, refobj, &fd); if (path == NULL) return (NULL); } else path = NULL; - /* - * If we didn't find a match by pathname, or the name is not - * supplied, open the file and check again by device and inode. - * This avoids false mismatches caused by multiple links or ".." - * in pathnames. - * - * To avoid a race, we open the file and use fstat() rather than - * using stat(). - */ - fd = -1; - if (fd_u == -1) { + if (fd >= 0) { + /* + * search_library_pathfds() opens a fresh file descriptor for the + * library, so there is no need to dup(). + */ + } else if (fd_u == -1) { + /* + * If we didn't find a match by pathname, or the name is not + * supplied, open the file and check again by device and inode. + * This avoids false mismatches caused by multiple links or ".." + * in pathnames. + * + * To avoid a race, we open the file and use fstat() rather than + * using stat(). + */ if ((fd = open(path, O_RDONLY | O_CLOEXEC)) == -1) { _rtld_error("Cannot open \"%s\"", path); free(path); @@ -2136,17 +2152,9 @@ load_object(const char *name, int fd_u, const Obj_Entry *refobj, int flags) } else { fd = fcntl(fd_u, F_DUPFD_CLOEXEC, 0); if (fd == -1) { - /* - * Temporary, remove at 3.6 branch - * User might not have latest kernel installed - * so fall back to old command for a while - */ - fd = dup(fd_u); - if (fd == -1 || (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) { - _rtld_error("Cannot dup fd"); - free(path); - return (NULL); - } + _rtld_error("Cannot dup fd"); + free(path); + return (NULL); } } if (fstat(fd, &sb) == -1) { @@ -2193,7 +2201,7 @@ do_load_object(int fd, const char *name, char *path, struct stat *sbp, if (dangerous_ld_env) { if (fstatfs(fd, &fs) != 0) { _rtld_error("Cannot fstatfs \"%s\"", printable_path(path)); - return NULL; + return NULL; } if (fs.f_flags & MNT_NOEXEC) { _rtld_error("Cannot execute objects on %s\n", fs.f_mntonname); @@ -2275,9 +2283,9 @@ preinit_main(void) for (index = 0; index < obj_main->preinit_array_num; index++) { if (preinit_addr[index] != 0 && preinit_addr[index] != 1) { dbg("calling preinit function for %s at %p", obj_main->path, - (void *)preinit_addr[index]); + (void *)preinit_addr[index]); LD_UTRACE(UTRACE_INIT_CALL, obj_main, (void *)preinit_addr[index], - 0, 0, obj_main->path); + 0, 0, obj_main->path); call_init_pointer(obj_main, preinit_addr[index]); } } @@ -2323,29 +2331,30 @@ objlist_call_fini(Objlist *list, Obj_Entry *root, RtldLockState *lockstate) lock_release(rtld_bind_lock, lockstate); /* - * It is legal to have both DT_FINI and DT_FINI_ARRAY defined. When this - * happens, DT_FINI_ARRAY is processed first, and it is also processed - * backwards. It is possible to encounter DT_FINI_ARRAY elements with - * values of 0 or 1, but they need to be ignored. + * It is legal to have both DT_FINI and DT_FINI_ARRAY defined. + * When this happens, DT_FINI_ARRAY is processed first. + * It is also processed backwards. It is possible to encounter + * DT_FINI_ARRAY elements with values of 0 or 1, but they need + * to be ignored. */ fini_addr = (Elf_Addr *)elm->obj->fini_array; if (fini_addr != NULL && elm->obj->fini_array_num > 0) { for (index = elm->obj->fini_array_num - 1; index >= 0; index--) { if (fini_addr[index] != 0 && fini_addr[index] != 1) { dbg("calling fini array function for %s at %p", - elm->obj->path, (void *)fini_addr[index]); + elm->obj->path, (void *)fini_addr[index]); LD_UTRACE(UTRACE_FINI_CALL, elm->obj, (void *)fini_addr[index], 0, 0, elm->obj->path); call_initfini_pointer(elm->obj, fini_addr[index]); } - } + } } if (elm->obj->fini != (Elf_Addr)NULL) { dbg("calling fini function for %s at %p", elm->obj->path, (void *)elm->obj->fini); LD_UTRACE(UTRACE_FINI_CALL, elm->obj, (void *)elm->obj->fini, 0, 0, elm->obj->path); - call_initfini_pointer(elm->obj, elm->obj->fini); + call_initfini_pointer(elm->obj, elm->obj->fini); } wlock_acquire(rtld_bind_lock, lockstate); /* No need to free anything if process is going down. */ @@ -2402,12 +2411,12 @@ objlist_call_init(Objlist *list, RtldLockState *lockstate) lock_release(rtld_bind_lock, lockstate); /* - * It is legal to have both DT_INIT and DT_INIT_ARRAY defined. When - * this happens, DT_INIT is processed first. It is possible to - * encounter DT_INIT_ARRAY elements with values of 0 or 1, but they - * need to be ignored. + * It is legal to have both DT_INIT and DT_INIT_ARRAY defined. + * When this happens, DT_INIT is processed first. + * It is possible to encounter DT_INIT_ARRAY elements with values + * of 0 or 1, but they need to be ignored. */ - if (elm->obj->init != (Elf_Addr)NULL) { + if (elm->obj->init != (Elf_Addr)NULL) { dbg("calling init function for %s at %p", elm->obj->path, (void *)elm->obj->init); LD_UTRACE(UTRACE_INIT_CALL, elm->obj, (void *)elm->obj->init, @@ -2483,18 +2492,18 @@ objlist_push_tail(Objlist *list, Obj_Entry *obj) static void objlist_put_after(Objlist *list, Obj_Entry *listobj, Obj_Entry *obj) { - Objlist_Entry *elm, *listelm; - - STAILQ_FOREACH(listelm, list, link) { - if (listelm->obj == listobj) - break; - } - elm = NEW(Objlist_Entry); - elm->obj = obj; - if (listelm != NULL) - STAILQ_INSERT_AFTER(list, listelm, elm, link); - else - STAILQ_INSERT_TAIL(list, elm, link); + Objlist_Entry *elm, *listelm; + + STAILQ_FOREACH(listelm, list, link) { + if (listelm->obj == listobj) + break; + } + elm = NEW(Objlist_Entry); + elm->obj = obj; + if (listelm != NULL) + STAILQ_INSERT_AFTER(list, listelm, elm, link); + else + STAILQ_INSERT_TAIL(list, elm, link); } static void @@ -2540,26 +2549,26 @@ relocate_object(Obj_Entry *obj, bool bind_now, Obj_Entry *rtldobj, { if (obj->relocated) - return (0); + return (0); obj->relocated = true; if (obj != rtldobj) - dbg("relocating \"%s\"", obj->path); + dbg("relocating \"%s\"", obj->path); if (obj->symtab == NULL || obj->strtab == NULL || - !(obj->valid_hash_sysv || obj->valid_hash_gnu)) { - _rtld_error("%s: Shared object has no run-time symbol table", - obj->path); - return (-1); + !(obj->valid_hash_sysv || obj->valid_hash_gnu)) { + _rtld_error("%s: Shared object has no run-time symbol table", + obj->path); + return (-1); } if (obj->textrel) { - /* There are relocations to the write-protected text segment. */ - if (mprotect(obj->mapbase, obj->textsize, - PROT_READ|PROT_WRITE|PROT_EXEC) == -1) { - _rtld_error("%s: Cannot write-enable text segment: %s", - obj->path, rtld_strerror(errno)); - return (-1); - } + /* There are relocations to the write-protected text segment. */ + if (mprotect(obj->mapbase, obj->textsize, + PROT_READ|PROT_WRITE|PROT_EXEC) == -1) { + _rtld_error("%s: Cannot write-enable text segment: %s", + obj->path, rtld_strerror(errno)); + return (-1); + } } /* Process the non-PLT relocations. */ @@ -2589,17 +2598,16 @@ relocate_object(Obj_Entry *obj, bool bind_now, Obj_Entry *rtldobj, } } - /* Set the special PLT or GOT entries. */ init_pltgot(obj); /* Process the PLT relocations. */ if (reloc_plt(obj) == -1) - return (-1); + return (-1); /* Relocate the jump slots if we are doing immediate binding. */ if (obj->bind_now || bind_now) - if (reloc_jmpslots(obj, flags, lockstate) == -1) - return (-1); + if (reloc_jmpslots(obj, flags, lockstate) == -1) + return (-1); /* * Set up the magic number and version in the Obj_Entry. These @@ -2801,6 +2809,69 @@ search_library_path(const char *name, const char *path) return (p); } + +/* + * Finds the library with the given name using the directory descriptors + * listed in the LD_LIBRARY_PATH_FDS environment variable. + * + * Returns a freshly-opened close-on-exec file descriptor for the library, + * or -1 if the library cannot be found. + */ +static char * +search_library_pathfds(const char *name, const char *path, int *fdp) +{ + char *envcopy, *fdstr, *found, *last_token; + size_t len; + int dirfd, fd; + + dbg("%s('%s', '%s', fdp)", __func__, name, path); + + /* Don't load from user-specified libdirs into setuid binaries. */ + if (!trust) + return (NULL); + + /* We can't do anything if LD_LIBRARY_PATH_FDS isn't set. */ + if (path == NULL) + return (NULL); + + /* LD_LIBRARY_PATH_FDS only works with relative paths. */ + if (name[0] == '/') { + dbg("Absolute path (%s) passed to %s", name, __func__); + return (NULL); + } + + /* + * Use strtok_r() to walk the FD:FD:FD list. This requires a local + * copy of the path, as strtok_r rewrites separator tokens + * with '\0'. + */ + found = NULL; + envcopy = xstrdup(path); + for (fdstr = strtok_r(envcopy, ":", &last_token); fdstr != NULL; + fdstr = strtok_r(NULL, ":", &last_token)) { + dirfd = parse_libdir(fdstr); + if (dirfd < 0) + break; + fd = openat(dirfd, name, O_RDONLY | O_CLOEXEC); + if (fd >= 0) { + *fdp = fd; + len = strlen(fdstr) + strlen(name) + 3; + found = xmalloc(len); + if (rtld_snprintf(found, len, "#%d/%s", dirfd, name) < 0) { + _rtld_error("error generating '%d/%s'", + dirfd, name); + die(); + } + dbg("open('%s') => %d", found, fd); + break; + } + } + free(envcopy); + + return (found); +} + + int dlclose(void *handle) { @@ -2966,7 +3037,7 @@ dlopen_object(const char *name, int fd, Obj_Entry *refobj, int lo_flags, * This has to be done after we have loaded all of the * dependencies, so that we do not miss any. */ - if (obj != NULL) + if (obj != NULL) process_nodelete(obj); } else { /* @@ -3136,11 +3207,8 @@ do_dlsym(void *handle, const char *name, void *retaddr, const Ver_Entry *ve, /* * The value required by the caller is derived from the value - * of the symbol. For the ia64 architecture, we need to - * construct a function descriptor which the caller can use to - * call the function with the right 'gp' value. For other - * architectures and for non-functions, the value is simply - * the relocated value of the symbol. + * of the symbol. this is simply the relocated value of the + * symbol. */ if (ELF_ST_TYPE(def->st_info) == STT_FUNC) return (make_function_pointer(def, defobj)); @@ -3810,8 +3878,8 @@ symlook_needed(SymLook *req, const Needed_Entry *needed, DoneList *dlp) (res = symlook_list(&req1, &n->obj->dagmembers, dlp)) != 0) continue; if (def == NULL || ELF_ST_BIND(req1.sym_out->st_info) != STB_WEAK) { - def = req1.sym_out; - defobj = req1.defobj_out; + def = req1.sym_out; + defobj = req1.defobj_out; if (ELF_ST_BIND(def->st_info) != STB_WEAK) break; } @@ -3883,7 +3951,7 @@ symlook_obj(SymLook *req, const Obj_Entry *obj) /* Symbol match routine common to both hash functions */ static bool matched_symbol(SymLook *req, const Obj_Entry *obj, Sym_Match_Result *result, - const unsigned long symnum) + const unsigned long symnum) { Elf_Versym verndx; const Elf_Sym *symp; @@ -3943,7 +4011,7 @@ matched_symbol(SymLook *req, const Obj_Entry *obj, Sym_Match_Result *result, } else if (verndx >= VER_NDX_GIVEN) { if ((obj->versyms[symnum] & VER_NDX_HIDDEN) - == 0) { + == 0) { if (result->vsymp == NULL) result->vsymp = symp; result->vcount++; @@ -4272,7 +4340,7 @@ tls_get_addr_common(Elf_Addr** dtvp, int index, size_t offset) dtv[index + 1] = (Elf_Addr)allocate_module_tls(index); lock_release(rtld_bind_lock, &lockstate); } - return (void*) (dtv[index + 1] + offset); + return ((void *)(dtv[index + 1] + offset)); } #if defined(RTLD_STATIC_TLS_VARIANT_II) @@ -4767,6 +4835,36 @@ symlook_init_from_req(SymLook *dst, const SymLook *src) dst->lockstate = src->lockstate; } + +/* + * Parse a file descriptor number without pulling in more of libc (e.g. atoi). + */ +static int +parse_libdir(const char *str) +{ + static const int RADIX = 10; /* XXXJA: possibly support hex? */ + const char *orig; + int fd; + char c; + + orig = str; + fd = 0; + for (c = *str; c != '\0'; c = *++str) { + if (c < '0' || c > '9') + return (-1); + + fd *= RADIX; + fd += c - '0'; + } + + /* Make sure we actually parsed something. */ + if (str == orig) { + _rtld_error("failed to parse directory FD from '%s'", str); + return (-1); + } + return (fd); +} + #ifdef ENABLE_OSRELDATE /* * Overrides for libc_pic-provided functions. diff --git a/libexec/rtld-elf/rtld_printf.c b/libexec/rtld-elf/rtld_printf.c index 7d939941f2..c899d4722b 100644 --- a/libexec/rtld-elf/rtld_printf.c +++ b/libexec/rtld-elf/rtld_printf.c @@ -428,6 +428,18 @@ number: #undef PCHAR } +int +rtld_snprintf(char *buf, size_t bufsize, const char *fmt, ...) +{ + va_list ap; + int retval; + + va_start(ap, fmt); + retval = rtld_vsnprintf(buf, bufsize, fmt, ap); + va_end(ap); + return (retval); +} + int rtld_vsnprintf(char *buf, size_t bufsize, const char *fmt, va_list ap) { diff --git a/libexec/rtld-elf/rtld_printf.h b/libexec/rtld-elf/rtld_printf.h index 6125732cb6..aaf2971acc 100644 --- a/libexec/rtld-elf/rtld_printf.h +++ b/libexec/rtld-elf/rtld_printf.h @@ -31,6 +31,8 @@ #include #include +int rtld_snprintf(char *buf, size_t bufsize, const char *fmt, ...) + __printflike(3, 4); int rtld_vsnprintf(char *buf, size_t bufsize, const char *fmt, va_list ap); int rtld_vfdprintf(int fd, const char *fmt, va_list ap); int rtld_fdprintf(int fd, const char *fmt, ...) __printflike(2, 3); -- 2.41.0