From ab4d51733f58020dd5f6a3cdf36f57a5d34090ba Mon Sep 17 00:00:00 2001 From: sthen Date: Sat, 20 Aug 2022 14:11:31 +0000 Subject: [PATCH] Support RTLD_NOLOAD in ld.so. From guenther@. OK jca@ guenther@ --- libexec/ld.so/dlfcn.c | 17 ++++++++++++++--- libexec/ld.so/library.c | 16 +++++++--------- libexec/ld.so/library_mquery.c | 15 ++++++--------- libexec/ld.so/library_subr.c | 25 +++++++++++++++---------- libexec/ld.so/resolve.h | 3 ++- 5 files changed, 44 insertions(+), 32 deletions(-) diff --git a/libexec/ld.so/dlfcn.c b/libexec/ld.so/dlfcn.c index ca2d6f137db..adde27790a8 100644 --- a/libexec/ld.so/dlfcn.c +++ b/libexec/ld.so/dlfcn.c @@ -1,4 +1,4 @@ -/* $OpenBSD: dlfcn.c,v 1.110 2022/01/08 17:28:49 deraadt Exp $ */ +/* $OpenBSD: dlfcn.c,v 1.111 2022/08/20 14:11:31 sthen Exp $ */ /* * Copyright (c) 1998 Per Fogelstrom, Opsycon AB @@ -45,6 +45,15 @@ static int _dl_real_close(void *handle); static lock_cb *_dl_thread_fnc = NULL; static elf_object_t *obj_from_addr(const void *addr); +#define OK_FLAGS (0 \ + | RTLD_TRACE \ + | RTLD_LAZY \ + | RTLD_NOW \ + | RTLD_GLOBAL \ + | RTLD_NODELETE \ + | RTLD_NOLOAD \ + ) + void * dlopen(const char *libname, int flags) { @@ -53,7 +62,7 @@ dlopen(const char *libname, int flags) int failed = 0; int obj_flags; - if (flags & ~(RTLD_TRACE|RTLD_LAZY|RTLD_NOW|RTLD_GLOBAL|RTLD_NODELETE)) { + if (flags & ~OK_FLAGS) { _dl_errno = DL_INVALID_MODE; return NULL; } @@ -78,7 +87,9 @@ dlopen(const char *libname, int flags) _dl_loading_object = NULL; obj_flags = (flags & RTLD_NOW ? DF_1_NOW : 0) - | (flags & RTLD_GLOBAL ? DF_1_GLOBAL : 0); + | (flags & RTLD_GLOBAL ? DF_1_GLOBAL : 0) + | (flags & RTLD_NOLOAD ? DF_1_NOOPEN : 0) + ; object = _dl_load_shlib(libname, _dl_objects, OBJTYPE_DLO, obj_flags); if (object == 0) { DL_DEB(("dlopen: failed to open %s\n", libname)); diff --git a/libexec/ld.so/library.c b/libexec/ld.so/library.c index 001c5764162..f8f6b991477 100644 --- a/libexec/ld.so/library.c +++ b/libexec/ld.so/library.c @@ -1,4 +1,4 @@ -/* $OpenBSD: library.c,v 1.86 2022/01/08 06:49:41 guenther Exp $ */ +/* $OpenBSD: library.c,v 1.87 2022/08/20 14:11:31 sthen Exp $ */ /* * Copyright (c) 2002 Dale Rahn @@ -129,18 +129,16 @@ _dl_tryload_shlib(const char *libname, int type, int flags) for (object = _dl_objects; object != NULL; object = object->next) { if (object->dev == sb.st_dev && object->inode == sb.st_ino) { - object->obj_flags |= flags & DF_1_GLOBAL; _dl_close(libfile); - if (_dl_loading_object == NULL) - _dl_loading_object = object; - if (object->load_object != _dl_objects && - object->load_object != _dl_loading_object) { - _dl_link_grpref(object->load_object, - _dl_loading_object); - } + _dl_handle_already_loaded(object, flags); return(object); } } + if (flags & DF_1_NOOPEN) { + _dl_close(libfile); + return NULL; + + } _dl_read(libfile, hbuf, sizeof(hbuf)); ehdr = (Elf_Ehdr *)hbuf; diff --git a/libexec/ld.so/library_mquery.c b/libexec/ld.so/library_mquery.c index 714816e9839..29893bdc6b2 100644 --- a/libexec/ld.so/library_mquery.c +++ b/libexec/ld.so/library_mquery.c @@ -1,4 +1,4 @@ -/* $OpenBSD: library_mquery.c,v 1.66 2022/01/08 06:49:41 guenther Exp $ */ +/* $OpenBSD: library_mquery.c,v 1.67 2022/08/20 14:11:31 sthen Exp $ */ /* * Copyright (c) 2002 Dale Rahn @@ -134,18 +134,15 @@ _dl_tryload_shlib(const char *libname, int type, int flags) for (object = _dl_objects; object != NULL; object = object->next) { if (object->dev == sb.st_dev && object->inode == sb.st_ino) { - object->obj_flags |= flags & DF_1_GLOBAL; _dl_close(libfile); - if (_dl_loading_object == NULL) - _dl_loading_object = object; - if (object->load_object != _dl_objects && - object->load_object != _dl_loading_object) { - _dl_link_grpref(object->load_object, - _dl_loading_object); - } + _dl_handle_already_loaded(object, flags); return(object); } } + if (flags & DF_1_NOOPEN) { + _dl_close(libfile); + return NULL; + } _dl_read(libfile, hbuf, sizeof(hbuf)); ehdr = (Elf_Ehdr *)hbuf; diff --git a/libexec/ld.so/library_subr.c b/libexec/ld.so/library_subr.c index 206255d8fc8..f8c059ee249 100644 --- a/libexec/ld.so/library_subr.c +++ b/libexec/ld.so/library_subr.c @@ -1,4 +1,4 @@ -/* $OpenBSD: library_subr.c,v 1.51 2022/01/08 06:49:41 guenther Exp $ */ +/* $OpenBSD: library_subr.c,v 1.52 2022/08/20 14:11:31 sthen Exp $ */ /* * Copyright (c) 2002 Dale Rahn @@ -241,6 +241,18 @@ _dl_lookup_object(const char *req_name, struct sod *req_sod) return(NULL); } +void +_dl_handle_already_loaded(elf_object_t *object, int flags) +{ + object->obj_flags |= flags & DF_1_GLOBAL; + if (_dl_loading_object == NULL) + _dl_loading_object = object; + if (object->load_object != _dl_objects && + object->load_object != _dl_loading_object) { + _dl_link_grpref(object->load_object, _dl_loading_object); + } +} + static elf_object_t * _dl_find_loaded_shlib(const char *req_name, struct sod req_sod, int flags) { @@ -262,15 +274,8 @@ _dl_find_loaded_shlib(const char *req_name, struct sod req_sod, int flags) req_sod.sod_minor, orig_minor); } - if (object) { /* Already loaded */ - object->obj_flags |= flags & DF_1_GLOBAL; - if (_dl_loading_object == NULL) - _dl_loading_object = object; - if (object->load_object != _dl_objects && - object->load_object != _dl_loading_object) { - _dl_link_grpref(object->load_object, _dl_loading_object); - } - } + if (object) + _dl_handle_already_loaded(object, flags); return (object); } diff --git a/libexec/ld.so/resolve.h b/libexec/ld.so/resolve.h index 6d77f1817ce..ede1701326f 100644 --- a/libexec/ld.so/resolve.h +++ b/libexec/ld.so/resolve.h @@ -1,4 +1,4 @@ -/* $OpenBSD: resolve.h,v 1.100 2022/01/28 05:01:28 guenther Exp $ */ +/* $OpenBSD: resolve.h,v 1.101 2022/08/20 14:11:31 sthen Exp $ */ /* * Copyright (c) 1998 Per Fogelstrom, Opsycon AB @@ -255,6 +255,7 @@ elf_object_t *_dl_finalize_object(const char *objname, Elf_Dyn *dynp, void _dl_remove_object(elf_object_t *object); void _dl_cleanup_objects(void); +void _dl_handle_already_loaded(elf_object_t *_object, int _flags); elf_object_t *_dl_load_shlib(const char *, elf_object_t *, int, int); elf_object_t *_dl_tryload_shlib(const char *libname, int type, int flags); -- 2.20.1