/src/openssl/crypto/dso/dso_dlfcn.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2000-2024 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (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_local.h" |
20 | | #include "internal/e_os.h" |
21 | | |
22 | | #ifdef DSO_DLFCN |
23 | | |
24 | | #ifdef HAVE_DLFCN_H |
25 | | #ifdef __osf__ |
26 | | #define __EXTENSIONS__ |
27 | | #endif |
28 | | #include <dlfcn.h> |
29 | | #define HAVE_DLINFO 1 |
30 | | #if defined(__SCO_VERSION__) || defined(_SCO_ELF) || (defined(__osf__) && !defined(RTLD_NEXT)) || (defined(__OpenBSD__) && !defined(RTLD_SELF)) || defined(__ANDROID__) || defined(__TANDEM) |
31 | | #undef HAVE_DLINFO |
32 | | #endif |
33 | | #endif |
34 | | |
35 | | /* Part of the hack in "dlfcn_load" ... */ |
36 | | #define DSO_MAX_TRANSLATED_SIZE 256 |
37 | | |
38 | | static int dlfcn_load(DSO *dso); |
39 | | static int dlfcn_unload(DSO *dso); |
40 | | static DSO_FUNC_TYPE dlfcn_bind_func(DSO *dso, const char *symname); |
41 | | static char *dlfcn_name_converter(DSO *dso, const char *filename); |
42 | | static char *dlfcn_merger(DSO *dso, const char *filespec1, |
43 | | const char *filespec2); |
44 | | static int dlfcn_pathbyaddr(void *addr, char *path, int sz); |
45 | | static void *dlfcn_globallookup(const char *name); |
46 | | |
47 | | static DSO_METHOD dso_meth_dlfcn = { |
48 | | "OpenSSL 'dlfcn' shared library method", |
49 | | dlfcn_load, |
50 | | dlfcn_unload, |
51 | | dlfcn_bind_func, |
52 | | NULL, /* ctrl */ |
53 | | dlfcn_name_converter, |
54 | | dlfcn_merger, |
55 | | NULL, /* init */ |
56 | | NULL, /* finish */ |
57 | | dlfcn_pathbyaddr, |
58 | | dlfcn_globallookup |
59 | | }; |
60 | | |
61 | | DSO_METHOD *DSO_METHOD_openssl(void) |
62 | 0 | { |
63 | 0 | return &dso_meth_dlfcn; |
64 | 0 | } |
65 | | |
66 | | /* |
67 | | * Prior to using the dlopen() function, we should decide on the flag we |
68 | | * send. There's a few different ways of doing this and it's a messy |
69 | | * venn-diagram to match up which platforms support what. So as we don't have |
70 | | * autoconf yet, I'm implementing a hack that could be hacked further |
71 | | * relatively easily to deal with cases as we find them. Initially this is to |
72 | | * cope with OpenBSD. |
73 | | */ |
74 | | #if defined(__OpenBSD__) || defined(__NetBSD__) |
75 | | #ifdef DL_LAZY |
76 | | #define DLOPEN_FLAG DL_LAZY |
77 | | #else |
78 | | #ifdef RTLD_NOW |
79 | | #define DLOPEN_FLAG RTLD_NOW |
80 | | #else |
81 | | #define DLOPEN_FLAG 0 |
82 | | #endif |
83 | | #endif |
84 | | #else |
85 | 0 | #define DLOPEN_FLAG RTLD_NOW /* Hope this works everywhere else */ |
86 | | #endif |
87 | | |
88 | | /* |
89 | | * For this DSO_METHOD, our meth_data STACK will contain; (i) the handle |
90 | | * (void*) returned from dlopen(). |
91 | | */ |
92 | | |
93 | | static int dlfcn_load(DSO *dso) |
94 | 0 | { |
95 | 0 | void *ptr = NULL; |
96 | | /* See applicable comments in dso_dl.c */ |
97 | 0 | char *filename = DSO_convert_filename(dso, NULL); |
98 | 0 | int flags = DLOPEN_FLAG; |
99 | 0 | int saveerrno = get_last_sys_error(); |
100 | |
|
101 | 0 | if (filename == NULL) { |
102 | 0 | ERR_raise(ERR_LIB_DSO, DSO_R_NO_FILENAME); |
103 | 0 | goto err; |
104 | 0 | } |
105 | 0 | #ifdef RTLD_GLOBAL |
106 | 0 | if (dso->flags & DSO_FLAG_GLOBAL_SYMBOLS) |
107 | 0 | flags |= RTLD_GLOBAL; |
108 | 0 | #endif |
109 | | #ifdef _AIX |
110 | | if (filename[strlen(filename) - 1] == ')') |
111 | | flags |= RTLD_MEMBER; |
112 | | #endif |
113 | 0 | ptr = dlopen(filename, flags); |
114 | 0 | if (ptr == NULL) { |
115 | 0 | ERR_raise_data(ERR_LIB_DSO, DSO_R_LOAD_FAILED, |
116 | 0 | "filename(%s): %s", filename, dlerror()); |
117 | 0 | goto err; |
118 | 0 | } |
119 | | /* |
120 | | * Some dlopen() implementations (e.g. solaris) do no preserve errno, even |
121 | | * on a successful call. |
122 | | */ |
123 | 0 | set_sys_error(saveerrno); |
124 | 0 | if (!sk_void_push(dso->meth_data, (char *)ptr)) { |
125 | 0 | ERR_raise(ERR_LIB_DSO, DSO_R_STACK_ERROR); |
126 | 0 | goto err; |
127 | 0 | } |
128 | | /* Success */ |
129 | 0 | dso->loaded_filename = filename; |
130 | 0 | return 1; |
131 | 0 | err: |
132 | | /* Cleanup! */ |
133 | 0 | OPENSSL_free(filename); |
134 | 0 | if (ptr != NULL) |
135 | 0 | dlclose(ptr); |
136 | 0 | return 0; |
137 | 0 | } |
138 | | |
139 | | static int dlfcn_unload(DSO *dso) |
140 | 0 | { |
141 | 0 | void *ptr; |
142 | 0 | if (dso == NULL) { |
143 | 0 | ERR_raise(ERR_LIB_DSO, ERR_R_PASSED_NULL_PARAMETER); |
144 | 0 | return 0; |
145 | 0 | } |
146 | 0 | if (sk_void_num(dso->meth_data) < 1) |
147 | 0 | return 1; |
148 | 0 | ptr = sk_void_pop(dso->meth_data); |
149 | 0 | if (ptr == NULL) { |
150 | 0 | ERR_raise(ERR_LIB_DSO, DSO_R_NULL_HANDLE); |
151 | | /* |
152 | | * Should push the value back onto the stack in case of a retry. |
153 | | */ |
154 | 0 | sk_void_push(dso->meth_data, ptr); |
155 | 0 | return 0; |
156 | 0 | } |
157 | | /* For now I'm not aware of any errors associated with dlclose() */ |
158 | 0 | dlclose(ptr); |
159 | 0 | return 1; |
160 | 0 | } |
161 | | |
162 | | static DSO_FUNC_TYPE dlfcn_bind_func(DSO *dso, const char *symname) |
163 | 0 | { |
164 | 0 | void *ptr; |
165 | 0 | union { |
166 | 0 | DSO_FUNC_TYPE sym; |
167 | 0 | void *dlret; |
168 | 0 | } u; |
169 | |
|
170 | 0 | if ((dso == NULL) || (symname == NULL)) { |
171 | 0 | ERR_raise(ERR_LIB_DSO, ERR_R_PASSED_NULL_PARAMETER); |
172 | 0 | return NULL; |
173 | 0 | } |
174 | 0 | if (sk_void_num(dso->meth_data) < 1) { |
175 | 0 | ERR_raise(ERR_LIB_DSO, DSO_R_STACK_ERROR); |
176 | 0 | return NULL; |
177 | 0 | } |
178 | 0 | ptr = sk_void_value(dso->meth_data, sk_void_num(dso->meth_data) - 1); |
179 | 0 | if (ptr == NULL) { |
180 | 0 | ERR_raise(ERR_LIB_DSO, DSO_R_NULL_HANDLE); |
181 | 0 | return NULL; |
182 | 0 | } |
183 | 0 | u.dlret = dlsym(ptr, symname); |
184 | 0 | if (u.dlret == NULL) { |
185 | 0 | ERR_raise_data(ERR_LIB_DSO, DSO_R_SYM_FAILURE, |
186 | 0 | "symname(%s): %s", symname, dlerror()); |
187 | 0 | return NULL; |
188 | 0 | } |
189 | 0 | return u.sym; |
190 | 0 | } |
191 | | |
192 | | static char *dlfcn_merger(DSO *dso, const char *filespec1, |
193 | | const char *filespec2) |
194 | 0 | { |
195 | 0 | char *merged; |
196 | |
|
197 | 0 | if (!filespec1 && !filespec2) { |
198 | 0 | ERR_raise(ERR_LIB_DSO, ERR_R_PASSED_NULL_PARAMETER); |
199 | 0 | return NULL; |
200 | 0 | } |
201 | | /* |
202 | | * If the first file specification is a rooted path, it rules. same goes |
203 | | * if the second file specification is missing. |
204 | | */ |
205 | 0 | if (!filespec2 || (filespec1 != NULL && filespec1[0] == '/')) { |
206 | 0 | merged = OPENSSL_strdup(filespec1); |
207 | 0 | if (merged == NULL) |
208 | 0 | return NULL; |
209 | 0 | } |
210 | | /* |
211 | | * If the first file specification is missing, the second one rules. |
212 | | */ |
213 | 0 | else if (!filespec1) { |
214 | 0 | merged = OPENSSL_strdup(filespec2); |
215 | 0 | if (merged == NULL) |
216 | 0 | return NULL; |
217 | 0 | } else { |
218 | | /* |
219 | | * This part isn't as trivial as it looks. It assumes that the |
220 | | * second file specification really is a directory, and makes no |
221 | | * checks whatsoever. Therefore, the result becomes the |
222 | | * concatenation of filespec2 followed by a slash followed by |
223 | | * filespec1. |
224 | | */ |
225 | 0 | int spec2len, len; |
226 | |
|
227 | 0 | spec2len = strlen(filespec2); |
228 | 0 | len = spec2len + strlen(filespec1); |
229 | |
|
230 | 0 | if (spec2len && filespec2[spec2len - 1] == '/') { |
231 | 0 | spec2len--; |
232 | 0 | len--; |
233 | 0 | } |
234 | 0 | merged = OPENSSL_malloc(len + 2); |
235 | 0 | if (merged == NULL) |
236 | 0 | return NULL; |
237 | 0 | strcpy(merged, filespec2); |
238 | 0 | merged[spec2len] = '/'; |
239 | 0 | strcpy(&merged[spec2len + 1], filespec1); |
240 | 0 | } |
241 | 0 | return merged; |
242 | 0 | } |
243 | | |
244 | | static char *dlfcn_name_converter(DSO *dso, const char *filename) |
245 | 0 | { |
246 | 0 | char *translated; |
247 | 0 | int len, rsize, transform; |
248 | |
|
249 | 0 | len = strlen(filename); |
250 | 0 | rsize = len + 1; |
251 | 0 | transform = (strchr(filename, '/') == NULL); |
252 | 0 | if (transform) { |
253 | | /* We will convert this to "%s.so" or "lib%s.so" etc */ |
254 | 0 | rsize += strlen(DSO_EXTENSION); /* The length of ".so" */ |
255 | 0 | if ((DSO_flags(dso) & DSO_FLAG_NAME_TRANSLATION_EXT_ONLY) == 0) |
256 | 0 | rsize += 3; /* The length of "lib" */ |
257 | 0 | } |
258 | 0 | translated = OPENSSL_malloc(rsize); |
259 | 0 | if (translated == NULL) { |
260 | 0 | ERR_raise(ERR_LIB_DSO, DSO_R_NAME_TRANSLATION_FAILED); |
261 | 0 | return NULL; |
262 | 0 | } |
263 | 0 | if (transform) { |
264 | 0 | if ((DSO_flags(dso) & DSO_FLAG_NAME_TRANSLATION_EXT_ONLY) == 0) |
265 | 0 | BIO_snprintf(translated, rsize, "lib%s" DSO_EXTENSION, filename); |
266 | 0 | else |
267 | 0 | BIO_snprintf(translated, rsize, "%s" DSO_EXTENSION, filename); |
268 | 0 | } else { |
269 | 0 | BIO_snprintf(translated, rsize, "%s", filename); |
270 | 0 | } |
271 | 0 | return translated; |
272 | 0 | } |
273 | | |
274 | | #ifdef __sgi |
275 | | /*- |
276 | | This is a quote from IRIX manual for dladdr(3c): |
277 | | |
278 | | <dlfcn.h> does not contain a prototype for dladdr or definition of |
279 | | Dl_info. The #include <dlfcn.h> in the SYNOPSIS line is traditional, |
280 | | but contains no dladdr prototype and no IRIX library contains an |
281 | | implementation. Write your own declaration based on the code below. |
282 | | |
283 | | The following code is dependent on internal interfaces that are not |
284 | | part of the IRIX compatibility guarantee; however, there is no future |
285 | | intention to change this interface, so on a practical level, the code |
286 | | below is safe to use on IRIX. |
287 | | */ |
288 | | #include <rld_interface.h> |
289 | | #ifndef _RLD_INTERFACE_DLFCN_H_DLADDR |
290 | | #define _RLD_INTERFACE_DLFCN_H_DLADDR |
291 | | typedef struct Dl_info { |
292 | | const char *dli_fname; |
293 | | void *dli_fbase; |
294 | | const char *dli_sname; |
295 | | void *dli_saddr; |
296 | | int dli_version; |
297 | | int dli_reserved1; |
298 | | long dli_reserved[4]; |
299 | | } Dl_info; |
300 | | #else |
301 | | typedef struct Dl_info Dl_info; |
302 | | #endif |
303 | | #define _RLD_DLADDR 14 |
304 | | |
305 | | static int dladdr(void *address, Dl_info *dl) |
306 | | { |
307 | | void *v; |
308 | | v = _rld_new_interface(_RLD_DLADDR, address, dl); |
309 | | return (int)v; |
310 | | } |
311 | | #endif /* __sgi */ |
312 | | |
313 | | #ifdef _AIX |
314 | | /*- |
315 | | * See IBM's AIX Version 7.2, Technical Reference: |
316 | | * Base Operating System and Extensions, Volume 1 and 2 |
317 | | * https://www.ibm.com/support/knowledgecenter/ssw_aix_72/com.ibm.aix.base/technicalreferences.htm |
318 | | */ |
319 | | #include <sys/ldr.h> |
320 | | #include <errno.h> |
321 | | /* ~ 64 * (sizeof(struct ld_info) + _XOPEN_PATH_MAX + _XOPEN_NAME_MAX) */ |
322 | | #define DLFCN_LDINFO_SIZE 86976 |
323 | | typedef struct Dl_info { |
324 | | const char *dli_fname; |
325 | | } Dl_info; |
326 | | /* |
327 | | * This dladdr()-implementation will also find the ptrgl (Pointer Glue) virtual |
328 | | * address of a function, which is just located in the DATA segment instead of |
329 | | * the TEXT segment. |
330 | | */ |
331 | | static int dladdr(void *ptr, Dl_info *dl) |
332 | | { |
333 | | uintptr_t addr = (uintptr_t)ptr; |
334 | | unsigned int found = 0; |
335 | | struct ld_info *ldinfos, *next_ldi, *this_ldi; |
336 | | |
337 | | if ((ldinfos = OPENSSL_malloc(DLFCN_LDINFO_SIZE)) == NULL) { |
338 | | errno = ENOMEM; |
339 | | dl->dli_fname = NULL; |
340 | | return 0; |
341 | | } |
342 | | |
343 | | if ((loadquery(L_GETINFO, (void *)ldinfos, DLFCN_LDINFO_SIZE)) < 0) { |
344 | | /*- |
345 | | * Error handling is done through errno and dlerror() reading errno: |
346 | | * ENOMEM (ldinfos buffer is too small), |
347 | | * EINVAL (invalid flags), |
348 | | * EFAULT (invalid ldinfos ptr) |
349 | | */ |
350 | | OPENSSL_free((void *)ldinfos); |
351 | | dl->dli_fname = NULL; |
352 | | return 0; |
353 | | } |
354 | | next_ldi = ldinfos; |
355 | | |
356 | | do { |
357 | | this_ldi = next_ldi; |
358 | | if (((addr >= (uintptr_t)this_ldi->ldinfo_textorg) |
359 | | && (addr < ((uintptr_t)this_ldi->ldinfo_textorg + this_ldi->ldinfo_textsize))) |
360 | | || ((addr >= (uintptr_t)this_ldi->ldinfo_dataorg) |
361 | | && (addr < ((uintptr_t)this_ldi->ldinfo_dataorg + this_ldi->ldinfo_datasize)))) { |
362 | | char *buffer, *member; |
363 | | size_t buffer_sz, member_len; |
364 | | |
365 | | buffer_sz = strlen(this_ldi->ldinfo_filename) + 1; |
366 | | member = this_ldi->ldinfo_filename + buffer_sz; |
367 | | if ((member_len = strlen(member)) > 0) |
368 | | buffer_sz += 1 + member_len + 1; |
369 | | found = 1; |
370 | | if ((buffer = OPENSSL_malloc(buffer_sz)) != NULL) { |
371 | | OPENSSL_strlcpy(buffer, this_ldi->ldinfo_filename, buffer_sz); |
372 | | if (member_len > 0) { |
373 | | /* |
374 | | * Need to respect a possible member name and not just |
375 | | * returning the path name in this case. See docs: |
376 | | * sys/ldr.h, loadquery() and dlopen()/RTLD_MEMBER. |
377 | | */ |
378 | | OPENSSL_strlcat(buffer, "(", buffer_sz); |
379 | | OPENSSL_strlcat(buffer, member, buffer_sz); |
380 | | OPENSSL_strlcat(buffer, ")", buffer_sz); |
381 | | } |
382 | | dl->dli_fname = buffer; |
383 | | } else { |
384 | | errno = ENOMEM; |
385 | | } |
386 | | } else { |
387 | | next_ldi = (struct ld_info *)((uintptr_t)this_ldi + this_ldi->ldinfo_next); |
388 | | } |
389 | | } while (this_ldi->ldinfo_next && !found); |
390 | | OPENSSL_free((void *)ldinfos); |
391 | | return (found && dl->dli_fname != NULL); |
392 | | } |
393 | | #endif /* _AIX */ |
394 | | |
395 | | static int dlfcn_pathbyaddr(void *addr, char *path, int sz) |
396 | 0 | { |
397 | 0 | #ifdef HAVE_DLINFO |
398 | 0 | Dl_info dli; |
399 | 0 | int len; |
400 | |
|
401 | 0 | if (addr == NULL) { |
402 | 0 | union { |
403 | 0 | int (*f)(void *, char *, int); |
404 | 0 | void *p; |
405 | 0 | } t = { |
406 | 0 | dlfcn_pathbyaddr |
407 | 0 | }; |
408 | 0 | addr = t.p; |
409 | 0 | } |
410 | |
|
411 | 0 | if (dladdr(addr, &dli)) { |
412 | 0 | len = (int)strlen(dli.dli_fname); |
413 | 0 | if (sz <= 0) { |
414 | | #ifdef _AIX |
415 | | OPENSSL_free((void *)dli.dli_fname); |
416 | | #endif |
417 | 0 | return len + 1; |
418 | 0 | } |
419 | 0 | if (len >= sz) |
420 | 0 | len = sz - 1; |
421 | 0 | memcpy(path, dli.dli_fname, len); |
422 | 0 | path[len++] = 0; |
423 | | #ifdef _AIX |
424 | | OPENSSL_free((void *)dli.dli_fname); |
425 | | #endif |
426 | 0 | return len; |
427 | 0 | } |
428 | | |
429 | 0 | ERR_add_error_data(2, "dlfcn_pathbyaddr(): ", dlerror()); |
430 | 0 | #endif |
431 | 0 | return -1; |
432 | 0 | } |
433 | | |
434 | | static void *dlfcn_globallookup(const char *name) |
435 | 0 | { |
436 | 0 | void *ret = NULL, *handle = dlopen(NULL, RTLD_LAZY); |
437 | |
|
438 | 0 | if (handle) { |
439 | 0 | ret = dlsym(handle, name); |
440 | 0 | dlclose(handle); |
441 | 0 | } |
442 | |
|
443 | 0 | return ret; |
444 | 0 | } |
445 | | #endif /* DSO_DLFCN */ |