Skip to content

Commit

Permalink
ldso: support RTLD_NOLOAD
Browse files Browse the repository at this point in the history
So application query if specified module is loaded or not with
dlopen.

Signed-off-by: Timo Teräs <timo.teras@iki.fi>
Signed-off-by: Bernhard Reutner-Fischer <rep.dot.nop@gmail.com>
  • Loading branch information
Timo Teräs authored and aldot committed Nov 7, 2011
1 parent 515d544 commit 0c7690f
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 22 deletions.
6 changes: 4 additions & 2 deletions ldso/include/dl-elf.h
Expand Up @@ -26,16 +26,18 @@ static __inline__ void _dl_map_cache(void) { }
static __inline__ void _dl_unmap_cache(void) { }
#endif

#define DL_RESOLVE_SECURE 0x0001
#define DL_RESOLVE_NOLOAD 0x0002

/* Function prototypes for non-static stuff in readelflib1.c */
extern void _dl_parse_lazy_relocation_information(struct dyn_elf *rpnt,
unsigned long rel_addr, unsigned long rel_size);
extern int _dl_parse_relocation_information(struct dyn_elf *rpnt,
struct r_scope_elem *scope, unsigned long rel_addr, unsigned long rel_size);
extern struct elf_resolve * _dl_load_shared_library(int secure,
extern struct elf_resolve * _dl_load_shared_library(unsigned rflags,
struct dyn_elf **rpnt, struct elf_resolve *tpnt, char *full_libname,
int trace_loaded_objects);
extern struct elf_resolve * _dl_load_elf_shared_library(int secure,
extern struct elf_resolve * _dl_load_elf_shared_library(unsigned rflags,
struct dyn_elf **rpnt, const char *libname);
extern struct elf_resolve *_dl_check_if_named_library_is_loaded(const char *full_libname,
int trace_loaded_objects);
Expand Down
30 changes: 17 additions & 13 deletions ldso/ldso/dl-elf.c
Expand Up @@ -132,7 +132,7 @@ _dl_protect_relro (struct elf_resolve *l)
/* This function's behavior must exactly match that
* in uClibc/ldso/util/ldd.c */
static struct elf_resolve *
search_for_named_library(const char *name, int secure, const char *path_list,
search_for_named_library(const char *name, unsigned rflags, const char *path_list,
struct dyn_elf **rpnt)
{
char *path, *path_n, *mylibname;
Expand Down Expand Up @@ -174,7 +174,7 @@ search_for_named_library(const char *name, int secure, const char *path_list,
_dl_strcpy(mylibname, "."); /* Assume current dir if empty path */
_dl_strcat(mylibname, "/");
_dl_strcat(mylibname, name);
if ((tpnt = _dl_load_elf_shared_library(secure, rpnt, mylibname)) != NULL)
if ((tpnt = _dl_load_elf_shared_library(rflags, rpnt, mylibname)) != NULL)
return tpnt;
path_n = path+1;
}
Expand All @@ -187,7 +187,7 @@ search_for_named_library(const char *name, int secure, const char *path_list,
unsigned long _dl_error_number;
unsigned long _dl_internal_error_number;

struct elf_resolve *_dl_load_shared_library(int secure, struct dyn_elf **rpnt,
struct elf_resolve *_dl_load_shared_library(unsigned rflags, struct dyn_elf **rpnt,
struct elf_resolve *tpnt, char *full_libname, int attribute_unused trace_loaded_objects)
{
char *pnt;
Expand Down Expand Up @@ -216,7 +216,7 @@ struct elf_resolve *_dl_load_shared_library(int secure, struct dyn_elf **rpnt,

if (libname != full_libname) {
_dl_if_debug_dprint("\ttrying file='%s'\n", full_libname);
tpnt1 = _dl_load_elf_shared_library(secure, rpnt, full_libname);
tpnt1 = _dl_load_elf_shared_library(rflags, rpnt, full_libname);
if (tpnt1) {
return tpnt1;
}
Expand All @@ -231,7 +231,7 @@ struct elf_resolve *_dl_load_shared_library(int secure, struct dyn_elf **rpnt,
if (pnt) {
pnt += (unsigned long) tpnt->dynamic_info[DT_STRTAB];
_dl_if_debug_dprint("\tsearching RPATH='%s'\n", pnt);
if ((tpnt1 = search_for_named_library(libname, secure, pnt, rpnt)) != NULL)
if ((tpnt1 = search_for_named_library(libname, rflags, pnt, rpnt)) != NULL)
return tpnt1;
}
#endif
Expand All @@ -240,7 +240,7 @@ struct elf_resolve *_dl_load_shared_library(int secure, struct dyn_elf **rpnt,
/* Check in LD_{ELF_}LIBRARY_PATH, if specified and allowed */
if (_dl_library_path) {
_dl_if_debug_dprint("\tsearching LD_LIBRARY_PATH='%s'\n", _dl_library_path);
if ((tpnt1 = search_for_named_library(libname, secure, _dl_library_path, rpnt)) != NULL)
if ((tpnt1 = search_for_named_library(libname, rflags, _dl_library_path, rpnt)) != NULL)
{
return tpnt1;
}
Expand All @@ -254,7 +254,7 @@ struct elf_resolve *_dl_load_shared_library(int secure, struct dyn_elf **rpnt,
if (pnt) {
pnt += (unsigned long) tpnt->dynamic_info[DT_STRTAB];
_dl_if_debug_dprint("\tsearching RUNPATH='%s'\n", pnt);
if ((tpnt1 = search_for_named_library(libname, secure, pnt, rpnt)) != NULL)
if ((tpnt1 = search_for_named_library(libname, rflags, pnt, rpnt)) != NULL)
return tpnt1;
}
#endif
Expand All @@ -277,7 +277,7 @@ struct elf_resolve *_dl_load_shared_library(int secure, struct dyn_elf **rpnt,
|| libent[i].flags == LIB_ELF_LIBC0
|| libent[i].flags == LIB_ELF_LIBC5)
&& _dl_strcmp(libname, strs + libent[i].sooffset) == 0
&& (tpnt1 = _dl_load_elf_shared_library(secure, rpnt, strs + libent[i].liboffset))
&& (tpnt1 = _dl_load_elf_shared_library(rflags, rpnt, strs + libent[i].liboffset))
) {
return tpnt1;
}
Expand All @@ -288,14 +288,14 @@ struct elf_resolve *_dl_load_shared_library(int secure, struct dyn_elf **rpnt,
/* Look for libraries wherever the shared library loader
* was installed */
_dl_if_debug_dprint("\tsearching ldso dir='%s'\n", _dl_ldsopath);
tpnt1 = search_for_named_library(libname, secure, _dl_ldsopath, rpnt);
tpnt1 = search_for_named_library(libname, rflags, _dl_ldsopath, rpnt);
if (tpnt1 != NULL)
return tpnt1;
#endif
/* Lastly, search the standard list of paths for the library.
This list must exactly match the list in uClibc/ldso/util/ldd.c */
_dl_if_debug_dprint("\tsearching full lib path list\n");
tpnt1 = search_for_named_library(libname, secure,
tpnt1 = search_for_named_library(libname, rflags,
UCLIBC_RUNTIME_PREFIX "lib:"
UCLIBC_RUNTIME_PREFIX "usr/lib"
#ifndef __LDSO_CACHE_SUPPORT__
Expand Down Expand Up @@ -437,7 +437,7 @@ map_writeable (int infile, ElfW(Phdr) *ppnt, int piclib, int flags,
* are required.
*/

struct elf_resolve *_dl_load_elf_shared_library(int secure,
struct elf_resolve *_dl_load_elf_shared_library(unsigned rflags,
struct dyn_elf **rpnt, const char *libname)
{
ElfW(Ehdr) *epnt;
Expand Down Expand Up @@ -474,9 +474,9 @@ struct elf_resolve *_dl_load_elf_shared_library(int secure,
_dl_close(infile);
return NULL;
}
/* If we are in secure mode (i.e. a setu/gid binary using LD_PRELOAD),
/* If we are in secure mode (i.e. a setuid/gid binary using LD_PRELOAD),
we don't load the library if it isn't setuid. */
if (secure) {
if (rflags & DL_RESOLVE_SECURE) {
if (!(st.st_mode & S_ISUID)) {
_dl_close(infile);
return NULL;
Expand All @@ -492,6 +492,10 @@ struct elf_resolve *_dl_load_elf_shared_library(int secure,
return tpnt;
}
}
if (rflags & DL_RESOLVE_NOLOAD) {
_dl_close(infile);
return NULL;
}
header = _dl_mmap((void *) 0, _dl_pagesize, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_UNINITIALIZE, -1, 0);
if (_dl_mmap_check_error(header)) {
Expand Down
4 changes: 3 additions & 1 deletion ldso/ldso/ldso.c
Expand Up @@ -858,7 +858,9 @@ of this helper program; chances are you did not intend to run this program.\n\
if (!_dl_secure || _dl_strchr(str, '/') == NULL) {
_dl_if_debug_dprint("\tfile='%s'; needed by '%s'\n", str, _dl_progname);

tpnt1 = _dl_load_shared_library(_dl_secure, &rpnt, NULL, str, trace_loaded_objects);
tpnt1 = _dl_load_shared_library(
_dl_secure ? DL_RESOLVE_SECURE : 0,
&rpnt, NULL, str, trace_loaded_objects);
if (!tpnt1) {
#ifdef __LDSO_LDD_SUPPORT__
if (trace_loaded_objects || _dl_trace_prelink)
Expand Down
11 changes: 7 additions & 4 deletions ldso/libdl/libdl.c
Expand Up @@ -52,11 +52,13 @@ extern struct link_map *_dl_update_slotinfo(unsigned long int req_modid);

/* When libdl is loaded as a shared library, we need to load in
* and use a pile of symbols from ldso... */

extern struct elf_resolve * _dl_load_shared_library(int, struct dyn_elf **,
#include <dl-elf.h>
#if 0
extern struct elf_resolve * _dl_load_shared_library(unsigned, struct dyn_elf **,
struct elf_resolve *, char *, int);
extern int _dl_fixup(struct dyn_elf *rpnt, struct r_scope_elem *scope, int lazy);
extern void _dl_protect_relro(struct elf_resolve * tpnt);
#endif
extern int _dl_errno;
extern struct dyn_elf *_dl_symbol_tables;
extern struct dyn_elf *_dl_handles;
Expand Down Expand Up @@ -308,7 +310,7 @@ void *dlopen(const char *libname, int flag)
#endif

/* A bit of sanity checking... */
if (!(flag & (RTLD_LAZY|RTLD_NOW))) {
if (!(flag & (RTLD_LAZY|RTLD_NOW|RTLD_NOLOAD))) {
_dl_error_number = LD_BAD_HANDLE;
return NULL;
}
Expand Down Expand Up @@ -378,8 +380,9 @@ void *dlopen(const char *libname, int flag)
/* Try to load the specified library */
_dl_if_debug_print("Trying to dlopen '%s', RTLD_GLOBAL:%d RTLD_NOW:%d\n",
(char*)libname, (flag & RTLD_GLOBAL ? 1:0), (now_flag & RTLD_NOW ? 1:0));
tpnt = _dl_load_shared_library(0, &rpnt, tfrom, (char*)libname, 0);

tpnt = _dl_load_shared_library((flag & RTLD_NOLOAD) ? DL_RESOLVE_NOLOAD : 0,
&rpnt, tfrom, (char*)libname, 0);
if (tpnt == NULL) {
_dl_unmap_cache();
return NULL;
Expand Down
4 changes: 2 additions & 2 deletions libc/sysdeps/linux/common/bits/dlfcn.h
Expand Up @@ -24,9 +24,9 @@
/* The MODE argument to `dlopen' contains one of the following: */
#define RTLD_LAZY 0x00001 /* Lazy function call binding. */
#define RTLD_NOW 0x00002 /* Immediate function call binding. */
#if 0 /* uClibc doesnt support these */
#define RTLD_BINDING_MASK 0x3 /* Mask of binding time value. */
#define RTLD_BINDING_MASK 0x3 /* Mask of binding time value. */
#define RTLD_NOLOAD 0x00004 /* Do not load the object. */
#if 0 /* uClibc doesnt support these */
#define RTLD_DEEPBIND 0x00008 /* Use deep binding. */
#endif

Expand Down

0 comments on commit 0c7690f

Please sign in to comment.