1 | /*
|
---|
2 | * Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
|
---|
3 | *
|
---|
4 | * Licensed under the OpenSSL license (the "License"). You may not use
|
---|
5 | * this file except in compliance with the License. You can obtain a copy
|
---|
6 | * in the file LICENSE in the source distribution or at
|
---|
7 | * https://www.openssl.org/source/license.html
|
---|
8 | */
|
---|
9 |
|
---|
10 | /*
|
---|
11 | * We need to do this early, because stdio.h includes the header files that
|
---|
12 | * handle _GNU_SOURCE and other similar macros. Defining it later is simply
|
---|
13 | * too late, because those headers are protected from re- inclusion.
|
---|
14 | */
|
---|
15 | #ifndef _GNU_SOURCE
|
---|
16 | # define _GNU_SOURCE /* make sure dladdr is declared */
|
---|
17 | #endif
|
---|
18 |
|
---|
19 | #include "dso_locl.h"
|
---|
20 |
|
---|
21 | #ifdef DSO_DLFCN
|
---|
22 |
|
---|
23 | # ifdef HAVE_DLFCN_H
|
---|
24 | # ifdef __osf__
|
---|
25 | # define __EXTENSIONS__
|
---|
26 | # endif
|
---|
27 | # include <dlfcn.h>
|
---|
28 | # define HAVE_DLINFO 1
|
---|
29 | # if defined(_AIX) || defined(__CYGWIN__) || \
|
---|
30 | defined(__SCO_VERSION__) || defined(_SCO_ELF) || \
|
---|
31 | (defined(__osf__) && !defined(RTLD_NEXT)) || \
|
---|
32 | (defined(__OpenBSD__) && !defined(RTLD_SELF)) || \
|
---|
33 | defined(__ANDROID__)
|
---|
34 | # undef HAVE_DLINFO
|
---|
35 | # endif
|
---|
36 | # endif
|
---|
37 |
|
---|
38 | /* Part of the hack in "dlfcn_load" ... */
|
---|
39 | # define DSO_MAX_TRANSLATED_SIZE 256
|
---|
40 |
|
---|
41 | static int dlfcn_load(DSO *dso);
|
---|
42 | static int dlfcn_unload(DSO *dso);
|
---|
43 | static DSO_FUNC_TYPE dlfcn_bind_func(DSO *dso, const char *symname);
|
---|
44 | static char *dlfcn_name_converter(DSO *dso, const char *filename);
|
---|
45 | static char *dlfcn_merger(DSO *dso, const char *filespec1,
|
---|
46 | const char *filespec2);
|
---|
47 | static int dlfcn_pathbyaddr(void *addr, char *path, int sz);
|
---|
48 | static void *dlfcn_globallookup(const char *name);
|
---|
49 |
|
---|
50 | static DSO_METHOD dso_meth_dlfcn = {
|
---|
51 | "OpenSSL 'dlfcn' shared library method",
|
---|
52 | dlfcn_load,
|
---|
53 | dlfcn_unload,
|
---|
54 | dlfcn_bind_func,
|
---|
55 | NULL, /* ctrl */
|
---|
56 | dlfcn_name_converter,
|
---|
57 | dlfcn_merger,
|
---|
58 | NULL, /* init */
|
---|
59 | NULL, /* finish */
|
---|
60 | dlfcn_pathbyaddr,
|
---|
61 | dlfcn_globallookup
|
---|
62 | };
|
---|
63 |
|
---|
64 | DSO_METHOD *DSO_METHOD_openssl(void)
|
---|
65 | {
|
---|
66 | return &dso_meth_dlfcn;
|
---|
67 | }
|
---|
68 |
|
---|
69 | /*
|
---|
70 | * Prior to using the dlopen() function, we should decide on the flag we
|
---|
71 | * send. There's a few different ways of doing this and it's a messy
|
---|
72 | * venn-diagram to match up which platforms support what. So as we don't have
|
---|
73 | * autoconf yet, I'm implementing a hack that could be hacked further
|
---|
74 | * relatively easily to deal with cases as we find them. Initially this is to
|
---|
75 | * cope with OpenBSD.
|
---|
76 | */
|
---|
77 | # if defined(__OpenBSD__) || defined(__NetBSD__)
|
---|
78 | # ifdef DL_LAZY
|
---|
79 | # define DLOPEN_FLAG DL_LAZY
|
---|
80 | # else
|
---|
81 | # ifdef RTLD_NOW
|
---|
82 | # define DLOPEN_FLAG RTLD_NOW
|
---|
83 | # else
|
---|
84 | # define DLOPEN_FLAG 0
|
---|
85 | # endif
|
---|
86 | # endif
|
---|
87 | # else
|
---|
88 | # define DLOPEN_FLAG RTLD_NOW /* Hope this works everywhere else */
|
---|
89 | # endif
|
---|
90 |
|
---|
91 | /*
|
---|
92 | * For this DSO_METHOD, our meth_data STACK will contain; (i) the handle
|
---|
93 | * (void*) returned from dlopen().
|
---|
94 | */
|
---|
95 |
|
---|
96 | static int dlfcn_load(DSO *dso)
|
---|
97 | {
|
---|
98 | void *ptr = NULL;
|
---|
99 | /* See applicable comments in dso_dl.c */
|
---|
100 | char *filename = DSO_convert_filename(dso, NULL);
|
---|
101 | int flags = DLOPEN_FLAG;
|
---|
102 |
|
---|
103 | if (filename == NULL) {
|
---|
104 | DSOerr(DSO_F_DLFCN_LOAD, DSO_R_NO_FILENAME);
|
---|
105 | goto err;
|
---|
106 | }
|
---|
107 | # ifdef RTLD_GLOBAL
|
---|
108 | if (dso->flags & DSO_FLAG_GLOBAL_SYMBOLS)
|
---|
109 | flags |= RTLD_GLOBAL;
|
---|
110 | # endif
|
---|
111 | ptr = dlopen(filename, flags);
|
---|
112 | if (ptr == NULL) {
|
---|
113 | DSOerr(DSO_F_DLFCN_LOAD, DSO_R_LOAD_FAILED);
|
---|
114 | ERR_add_error_data(4, "filename(", filename, "): ", dlerror());
|
---|
115 | goto err;
|
---|
116 | }
|
---|
117 | if (!sk_void_push(dso->meth_data, (char *)ptr)) {
|
---|
118 | DSOerr(DSO_F_DLFCN_LOAD, DSO_R_STACK_ERROR);
|
---|
119 | goto err;
|
---|
120 | }
|
---|
121 | /* Success */
|
---|
122 | dso->loaded_filename = filename;
|
---|
123 | return (1);
|
---|
124 | err:
|
---|
125 | /* Cleanup! */
|
---|
126 | OPENSSL_free(filename);
|
---|
127 | if (ptr != NULL)
|
---|
128 | dlclose(ptr);
|
---|
129 | return (0);
|
---|
130 | }
|
---|
131 |
|
---|
132 | static int dlfcn_unload(DSO *dso)
|
---|
133 | {
|
---|
134 | void *ptr;
|
---|
135 | if (dso == NULL) {
|
---|
136 | DSOerr(DSO_F_DLFCN_UNLOAD, ERR_R_PASSED_NULL_PARAMETER);
|
---|
137 | return (0);
|
---|
138 | }
|
---|
139 | if (sk_void_num(dso->meth_data) < 1)
|
---|
140 | return (1);
|
---|
141 | ptr = sk_void_pop(dso->meth_data);
|
---|
142 | if (ptr == NULL) {
|
---|
143 | DSOerr(DSO_F_DLFCN_UNLOAD, DSO_R_NULL_HANDLE);
|
---|
144 | /*
|
---|
145 | * Should push the value back onto the stack in case of a retry.
|
---|
146 | */
|
---|
147 | sk_void_push(dso->meth_data, ptr);
|
---|
148 | return (0);
|
---|
149 | }
|
---|
150 | /* For now I'm not aware of any errors associated with dlclose() */
|
---|
151 | dlclose(ptr);
|
---|
152 | return (1);
|
---|
153 | }
|
---|
154 |
|
---|
155 | static DSO_FUNC_TYPE dlfcn_bind_func(DSO *dso, const char *symname)
|
---|
156 | {
|
---|
157 | void *ptr;
|
---|
158 | union {
|
---|
159 | DSO_FUNC_TYPE sym;
|
---|
160 | void *dlret;
|
---|
161 | } u;
|
---|
162 |
|
---|
163 | if ((dso == NULL) || (symname == NULL)) {
|
---|
164 | DSOerr(DSO_F_DLFCN_BIND_FUNC, ERR_R_PASSED_NULL_PARAMETER);
|
---|
165 | return (NULL);
|
---|
166 | }
|
---|
167 | if (sk_void_num(dso->meth_data) < 1) {
|
---|
168 | DSOerr(DSO_F_DLFCN_BIND_FUNC, DSO_R_STACK_ERROR);
|
---|
169 | return (NULL);
|
---|
170 | }
|
---|
171 | ptr = sk_void_value(dso->meth_data, sk_void_num(dso->meth_data) - 1);
|
---|
172 | if (ptr == NULL) {
|
---|
173 | DSOerr(DSO_F_DLFCN_BIND_FUNC, DSO_R_NULL_HANDLE);
|
---|
174 | return (NULL);
|
---|
175 | }
|
---|
176 | u.dlret = dlsym(ptr, symname);
|
---|
177 | if (u.dlret == NULL) {
|
---|
178 | DSOerr(DSO_F_DLFCN_BIND_FUNC, DSO_R_SYM_FAILURE);
|
---|
179 | ERR_add_error_data(4, "symname(", symname, "): ", dlerror());
|
---|
180 | return (NULL);
|
---|
181 | }
|
---|
182 | return u.sym;
|
---|
183 | }
|
---|
184 |
|
---|
185 | static char *dlfcn_merger(DSO *dso, const char *filespec1,
|
---|
186 | const char *filespec2)
|
---|
187 | {
|
---|
188 | char *merged;
|
---|
189 |
|
---|
190 | if (!filespec1 && !filespec2) {
|
---|
191 | DSOerr(DSO_F_DLFCN_MERGER, ERR_R_PASSED_NULL_PARAMETER);
|
---|
192 | return (NULL);
|
---|
193 | }
|
---|
194 | /*
|
---|
195 | * If the first file specification is a rooted path, it rules. same goes
|
---|
196 | * if the second file specification is missing.
|
---|
197 | */
|
---|
198 | if (!filespec2 || (filespec1 != NULL && filespec1[0] == '/')) {
|
---|
199 | merged = OPENSSL_strdup(filespec1);
|
---|
200 | if (merged == NULL) {
|
---|
201 | DSOerr(DSO_F_DLFCN_MERGER, ERR_R_MALLOC_FAILURE);
|
---|
202 | return (NULL);
|
---|
203 | }
|
---|
204 | }
|
---|
205 | /*
|
---|
206 | * If the first file specification is missing, the second one rules.
|
---|
207 | */
|
---|
208 | else if (!filespec1) {
|
---|
209 | merged = OPENSSL_strdup(filespec2);
|
---|
210 | if (merged == NULL) {
|
---|
211 | DSOerr(DSO_F_DLFCN_MERGER, ERR_R_MALLOC_FAILURE);
|
---|
212 | return (NULL);
|
---|
213 | }
|
---|
214 | } else {
|
---|
215 | /*
|
---|
216 | * This part isn't as trivial as it looks. It assumes that the
|
---|
217 | * second file specification really is a directory, and makes no
|
---|
218 | * checks whatsoever. Therefore, the result becomes the
|
---|
219 | * concatenation of filespec2 followed by a slash followed by
|
---|
220 | * filespec1.
|
---|
221 | */
|
---|
222 | int spec2len, len;
|
---|
223 |
|
---|
224 | spec2len = strlen(filespec2);
|
---|
225 | len = spec2len + strlen(filespec1);
|
---|
226 |
|
---|
227 | if (spec2len && filespec2[spec2len - 1] == '/') {
|
---|
228 | spec2len--;
|
---|
229 | len--;
|
---|
230 | }
|
---|
231 | merged = OPENSSL_malloc(len + 2);
|
---|
232 | if (merged == NULL) {
|
---|
233 | DSOerr(DSO_F_DLFCN_MERGER, ERR_R_MALLOC_FAILURE);
|
---|
234 | return (NULL);
|
---|
235 | }
|
---|
236 | strcpy(merged, filespec2);
|
---|
237 | merged[spec2len] = '/';
|
---|
238 | strcpy(&merged[spec2len + 1], filespec1);
|
---|
239 | }
|
---|
240 | return (merged);
|
---|
241 | }
|
---|
242 |
|
---|
243 | static char *dlfcn_name_converter(DSO *dso, const char *filename)
|
---|
244 | {
|
---|
245 | char *translated;
|
---|
246 | int len, rsize, transform;
|
---|
247 |
|
---|
248 | len = strlen(filename);
|
---|
249 | rsize = len + 1;
|
---|
250 | transform = (strstr(filename, "/") == NULL);
|
---|
251 | if (transform) {
|
---|
252 | /* We will convert this to "%s.so" or "lib%s.so" etc */
|
---|
253 | rsize += strlen(DSO_EXTENSION); /* The length of ".so" */
|
---|
254 | if ((DSO_flags(dso) & DSO_FLAG_NAME_TRANSLATION_EXT_ONLY) == 0)
|
---|
255 | rsize += 3; /* The length of "lib" */
|
---|
256 | }
|
---|
257 | translated = OPENSSL_malloc(rsize);
|
---|
258 | if (translated == NULL) {
|
---|
259 | DSOerr(DSO_F_DLFCN_NAME_CONVERTER, DSO_R_NAME_TRANSLATION_FAILED);
|
---|
260 | return (NULL);
|
---|
261 | }
|
---|
262 | if (transform) {
|
---|
263 | if ((DSO_flags(dso) & DSO_FLAG_NAME_TRANSLATION_EXT_ONLY) == 0)
|
---|
264 | sprintf(translated, "lib%s" DSO_EXTENSION, filename);
|
---|
265 | else
|
---|
266 | sprintf(translated, "%s" DSO_EXTENSION, filename);
|
---|
267 | } else
|
---|
268 | sprintf(translated, "%s", filename);
|
---|
269 | return (translated);
|
---|
270 | }
|
---|
271 |
|
---|
272 | # ifdef __sgi
|
---|
273 | /*-
|
---|
274 | This is a quote from IRIX manual for dladdr(3c):
|
---|
275 |
|
---|
276 | <dlfcn.h> does not contain a prototype for dladdr or definition of
|
---|
277 | Dl_info. The #include <dlfcn.h> in the SYNOPSIS line is traditional,
|
---|
278 | but contains no dladdr prototype and no IRIX library contains an
|
---|
279 | implementation. Write your own declaration based on the code below.
|
---|
280 |
|
---|
281 | The following code is dependent on internal interfaces that are not
|
---|
282 | part of the IRIX compatibility guarantee; however, there is no future
|
---|
283 | intention to change this interface, so on a practical level, the code
|
---|
284 | below is safe to use on IRIX.
|
---|
285 | */
|
---|
286 | # include <rld_interface.h>
|
---|
287 | # ifndef _RLD_INTERFACE_DLFCN_H_DLADDR
|
---|
288 | # define _RLD_INTERFACE_DLFCN_H_DLADDR
|
---|
289 | typedef struct Dl_info {
|
---|
290 | const char *dli_fname;
|
---|
291 | void *dli_fbase;
|
---|
292 | const char *dli_sname;
|
---|
293 | void *dli_saddr;
|
---|
294 | int dli_version;
|
---|
295 | int dli_reserved1;
|
---|
296 | long dli_reserved[4];
|
---|
297 | } Dl_info;
|
---|
298 | # else
|
---|
299 | typedef struct Dl_info Dl_info;
|
---|
300 | # endif
|
---|
301 | # define _RLD_DLADDR 14
|
---|
302 |
|
---|
303 | static int dladdr(void *address, Dl_info *dl)
|
---|
304 | {
|
---|
305 | void *v;
|
---|
306 | v = _rld_new_interface(_RLD_DLADDR, address, dl);
|
---|
307 | return (int)v;
|
---|
308 | }
|
---|
309 | # endif /* __sgi */
|
---|
310 |
|
---|
311 | static int dlfcn_pathbyaddr(void *addr, char *path, int sz)
|
---|
312 | {
|
---|
313 | # ifdef HAVE_DLINFO
|
---|
314 | Dl_info dli;
|
---|
315 | int len;
|
---|
316 |
|
---|
317 | if (addr == NULL) {
|
---|
318 | union {
|
---|
319 | int (*f) (void *, char *, int);
|
---|
320 | void *p;
|
---|
321 | } t = {
|
---|
322 | dlfcn_pathbyaddr
|
---|
323 | };
|
---|
324 | addr = t.p;
|
---|
325 | }
|
---|
326 |
|
---|
327 | if (dladdr(addr, &dli)) {
|
---|
328 | len = (int)strlen(dli.dli_fname);
|
---|
329 | if (sz <= 0)
|
---|
330 | return len + 1;
|
---|
331 | if (len >= sz)
|
---|
332 | len = sz - 1;
|
---|
333 | memcpy(path, dli.dli_fname, len);
|
---|
334 | path[len++] = 0;
|
---|
335 | return len;
|
---|
336 | }
|
---|
337 |
|
---|
338 | ERR_add_error_data(2, "dlfcn_pathbyaddr(): ", dlerror());
|
---|
339 | # endif
|
---|
340 | return -1;
|
---|
341 | }
|
---|
342 |
|
---|
343 | static void *dlfcn_globallookup(const char *name)
|
---|
344 | {
|
---|
345 | void *ret = NULL, *handle = dlopen(NULL, RTLD_LAZY);
|
---|
346 |
|
---|
347 | if (handle) {
|
---|
348 | ret = dlsym(handle, name);
|
---|
349 | dlclose(handle);
|
---|
350 | }
|
---|
351 |
|
---|
352 | return ret;
|
---|
353 | }
|
---|
354 | #endif /* DSO_DLFCN */
|
---|