summaryrefslogtreecommitdiffstats
path: root/main/libc0.9.32/0008-ldso-support-RTLD_NOLOAD.patch
blob: 3902df31e7be3638ffc5d47b9d0466e0e3859d75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
From e6fa350aa23280795a912d0edd989d7c98c62710 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timo=20Ter=C3=A4s?= <timo.teras@iki.fi>
Date: Fri, 18 Mar 2011 11:40:04 +0200
Subject: [PATCH] ldso: support RTLD_NOLOAD
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

So application query if specified modile is loaded or not with
dlopen.

Signed-off-by: Timo Teräs <timo.teras@iki.fi>
Signed-off-by: Natanael Copa <ncopa@alpinelinux.org>
---
 ldso/include/dl-elf.h                  |    6 ++++--
 ldso/ldso/dl-elf.c                     |   30 +++++++++++++++++-------------
 ldso/ldso/ldso.c                       |    4 +++-
 ldso/libdl/libdl.c                     |    5 +++--
 libc/sysdeps/linux/common/bits/dlfcn.h |    4 ++--
 5 files changed, 29 insertions(+), 20 deletions(-)

diff --git a/ldso/include/dl-elf.h b/ldso/include/dl-elf.h
index e7203fd..753c346 100644
--- a/ldso/include/dl-elf.h
+++ b/ldso/include/dl-elf.h
@@ -25,16 +25,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,
 	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(int resolve_flags,
 	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(int resolve_flags,
 	struct dyn_elf **rpnt, char *libname);
 extern struct elf_resolve *_dl_check_if_named_library_is_loaded(const char *full_libname,
 	int trace_loaded_objects);
diff --git a/ldso/ldso/dl-elf.c b/ldso/ldso/dl-elf.c
index 558176a..e32591a 100644
--- a/ldso/ldso/dl-elf.c
+++ b/ldso/ldso/dl-elf.c
@@ -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, int resolve_flags, const char *path_list,
 	struct dyn_elf **rpnt, const char *origin)
 {
 	char *mylibname;
@@ -162,7 +162,7 @@ search_for_named_library(const char *name, int secure, const char *path_list,
 
 		if (plen >= 7 && _dl_memcmp(p, "$ORIGIN", 7) == 0) {
 			int olen;
-			if (secure && plen != 7)
+			if ((resolve_flags & DL_RESOLVE_SECURE) && plen != 7)
 				continue;
 			if (origin == NULL)
 				continue;
@@ -182,7 +182,7 @@ search_for_named_library(const char *name, int secure, const char *path_list,
 		_dl_strcat(mylibname, "/");
 		_dl_strcat(mylibname, name);
 
-		tpnt = _dl_load_elf_shared_library(secure, rpnt, mylibname);
+		tpnt = _dl_load_elf_shared_library(resolve_flags, rpnt, mylibname);
 		if (tpnt != NULL)
 			return tpnt;
 	}
@@ -194,7 +194,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(int resolve_flags, struct dyn_elf **rpnt,
 	struct elf_resolve *tpnt, char *full_libname, int attribute_unused trace_loaded_objects)
 {
 	char *pnt;
@@ -223,7 +223,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(resolve_flags, rpnt, full_libname);
 		if (tpnt1) {
 			return tpnt1;
 		}
@@ -238,7 +238,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,
+		if ((tpnt1 = search_for_named_library(libname, resolve_flags, pnt, rpnt,
 						      tpnt->libname)) != NULL)
 			return tpnt1;
 	}
@@ -247,7 +247,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)) != NULL)
+		if ((tpnt1 = search_for_named_library(libname, resolve_flags, _dl_library_path, rpnt, NULL)) != NULL)
 		{
 			return tpnt1;
 		}
@@ -261,7 +261,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)) != NULL)
+		if ((tpnt1 = search_for_named_library(libname, resolve_flags, pnt, rpnt, NULL)) != NULL)
 			return tpnt1;
 	}
 #endif
@@ -284,7 +284,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(resolve_flags, rpnt, strs + libent[i].liboffset))
 			) {
 				return tpnt1;
 			}
@@ -295,14 +295,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, NULL);
+	tpnt1 = search_for_named_library(libname, resolve_flags, _dl_ldsopath, rpnt, NULL);
 	if (tpnt1 != NULL)
 		return tpnt1;
 
 	/* 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, resolve_flags,
 					UCLIBC_RUNTIME_PREFIX "lib:"
 					UCLIBC_RUNTIME_PREFIX "usr/lib"
 #ifndef __LDSO_CACHE_SUPPORT__
@@ -444,7 +444,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(int resolve_flags,
 	struct dyn_elf **rpnt, char *libname)
 {
 	ElfW(Ehdr) *epnt;
@@ -483,7 +483,7 @@ struct elf_resolve *_dl_load_elf_shared_library(int secure,
 	}
 	/* If we are in secure mode (i.e. a setu/gid binary using LD_PRELOAD),
 	   we don't load the library if it isn't setuid. */
-	if (secure) {
+	if (resolve_flags & DL_RESOLVE_SECURE) {
 		if (!(st.st_mode & S_ISUID)) {
 			_dl_close(infile);
 			return NULL;
@@ -499,6 +499,10 @@ struct elf_resolve *_dl_load_elf_shared_library(int secure,
 			return tpnt;
 		}
 	}
+	if (resolve_flags & 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)) {
diff --git a/ldso/ldso/ldso.c b/ldso/ldso/ldso.c
index df16b4f..5cefc22 100644
--- a/ldso/ldso/ldso.c
+++ b/ldso/ldso/ldso.c
@@ -642,7 +642,9 @@ void _dl_get_ready_to_run(struct elf_resolve *tpnt, DL_LOADADDR_TYPE load_addr,
 			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)
diff --git a/ldso/libdl/libdl.c b/ldso/libdl/libdl.c
index 52c77b0..49e90d0 100644
--- a/ldso/libdl/libdl.c
+++ b/ldso/libdl/libdl.c
@@ -288,7 +288,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;
 	}
@@ -358,8 +358,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;
diff --git a/libc/sysdeps/linux/common/bits/dlfcn.h b/libc/sysdeps/linux/common/bits/dlfcn.h
index 4bfbbff..47b42ad 100644
--- a/libc/sysdeps/linux/common/bits/dlfcn.h
+++ b/libc/sysdeps/linux/common/bits/dlfcn.h
@@ -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
 
-- 
1.7.8