Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/crypto/dso/dso_dlfcn.c

raw
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 {
63 return &dso_meth_dlfcn;
64 }
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 #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 {
95 void *ptr = NULL;
96 /* See applicable comments in dso_dl.c */
97 char *filename = DSO_convert_filename(dso, NULL);
98 int flags = DLOPEN_FLAG;
99 int saveerrno = get_last_sys_error();
100
101 if (filename == NULL) {
102 ERR_raise(ERR_LIB_DSO, DSO_R_NO_FILENAME);
103 goto err;
104 }
105 #ifdef RTLD_GLOBAL
106 if (dso->flags & DSO_FLAG_GLOBAL_SYMBOLS)
107 flags |= RTLD_GLOBAL;
108 #endif
109 #ifdef _AIX
110 if (filename[strlen(filename) - 1] == ')')
111 flags |= RTLD_MEMBER;
112 #endif
113 ptr = dlopen(filename, flags);
114 if (ptr == NULL) {
115 ERR_raise_data(ERR_LIB_DSO, DSO_R_LOAD_FAILED,
116 "filename(%s): %s", filename, dlerror());
117 goto err;
118 }
119 /*
120 * Some dlopen() implementations (e.g. solaris) do no preserve errno, even
121 * on a successful call.
122 */
123 set_sys_error(saveerrno);
124 if (!sk_void_push(dso->meth_data, (char *)ptr)) {
125 ERR_raise(ERR_LIB_DSO, DSO_R_STACK_ERROR);
126 goto err;
127 }
128 /* Success */
129 dso->loaded_filename = filename;
130 return 1;
131 err:
132 /* Cleanup! */
133 OPENSSL_free(filename);
134 if (ptr != NULL)
135 dlclose(ptr);
136 return 0;
137 }
138
139 static int dlfcn_unload(DSO *dso)
140 {
141 void *ptr;
142 if (dso == NULL) {
143 ERR_raise(ERR_LIB_DSO, ERR_R_PASSED_NULL_PARAMETER);
144 return 0;
145 }
146 if (sk_void_num(dso->meth_data) < 1)
147 return 1;
148 ptr = sk_void_pop(dso->meth_data);
149 if (ptr == NULL) {
150 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 sk_void_push(dso->meth_data, ptr);
155 return 0;
156 }
157 /* For now I'm not aware of any errors associated with dlclose() */
158 dlclose(ptr);
159 return 1;
160 }
161
162 static DSO_FUNC_TYPE dlfcn_bind_func(DSO *dso, const char *symname)
163 {
164 void *ptr;
165 union {
166 DSO_FUNC_TYPE sym;
167 void *dlret;
168 } u;
169
170 if ((dso == NULL) || (symname == NULL)) {
171 ERR_raise(ERR_LIB_DSO, ERR_R_PASSED_NULL_PARAMETER);
172 return NULL;
173 }
174 if (sk_void_num(dso->meth_data) < 1) {
175 ERR_raise(ERR_LIB_DSO, DSO_R_STACK_ERROR);
176 return NULL;
177 }
178 ptr = sk_void_value(dso->meth_data, sk_void_num(dso->meth_data) - 1);
179 if (ptr == NULL) {
180 ERR_raise(ERR_LIB_DSO, DSO_R_NULL_HANDLE);
181 return NULL;
182 }
183 u.dlret = dlsym(ptr, symname);
184 if (u.dlret == NULL) {
185 ERR_raise_data(ERR_LIB_DSO, DSO_R_SYM_FAILURE,
186 "symname(%s): %s", symname, dlerror());
187 return NULL;
188 }
189 return u.sym;
190 }
191
192 static char *dlfcn_merger(DSO *dso, const char *filespec1,
193 const char *filespec2)
194 {
195 char *merged;
196
197 if (!filespec1 && !filespec2) {
198 ERR_raise(ERR_LIB_DSO, ERR_R_PASSED_NULL_PARAMETER);
199 return NULL;
200 }
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 if (!filespec2 || (filespec1 != NULL && filespec1[0] == '/')) {
206 merged = OPENSSL_strdup(filespec1);
207 if (merged == NULL)
208 return NULL;
209 }
210 /*
211 * If the first file specification is missing, the second one rules.
212 */
213 else if (!filespec1) {
214 merged = OPENSSL_strdup(filespec2);
215 if (merged == NULL)
216 return NULL;
217 } 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 int spec2len, len;
226
227 spec2len = strlen(filespec2);
228 len = spec2len + strlen(filespec1);
229
230 if (spec2len && filespec2[spec2len - 1] == '/') {
231 spec2len--;
232 len--;
233 }
234 merged = OPENSSL_malloc(len + 2);
235 if (merged == NULL)
236 return NULL;
237 strcpy(merged, filespec2);
238 merged[spec2len] = '/';
239 strcpy(&merged[spec2len + 1], filespec1);
240 }
241 return merged;
242 }
243
244 static char *dlfcn_name_converter(DSO *dso, const char *filename)
245 {
246 char *translated;
247 int len, rsize, transform;
248
249 len = strlen(filename);
250 rsize = len + 1;
251 transform = (strchr(filename, '/') == NULL);
252 if (transform) {
253 /* We will convert this to "%s.so" or "lib%s.so" etc */
254 rsize += strlen(DSO_EXTENSION); /* The length of ".so" */
255 if ((DSO_flags(dso) & DSO_FLAG_NAME_TRANSLATION_EXT_ONLY) == 0)
256 rsize += 3; /* The length of "lib" */
257 }
258 translated = OPENSSL_malloc(rsize);
259 if (translated == NULL) {
260 ERR_raise(ERR_LIB_DSO, DSO_R_NAME_TRANSLATION_FAILED);
261 return NULL;
262 }
263 if (transform) {
264 if ((DSO_flags(dso) & DSO_FLAG_NAME_TRANSLATION_EXT_ONLY) == 0)
265 BIO_snprintf(translated, rsize, "lib%s" DSO_EXTENSION, filename);
266 else
267 BIO_snprintf(translated, rsize, "%s" DSO_EXTENSION, filename);
268 } else {
269 BIO_snprintf(translated, rsize, "%s", filename);
270 }
271 return translated;
272 }
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 {
397 #ifdef HAVE_DLINFO
398 Dl_info dli;
399 int len;
400
401 if (addr == NULL) {
402 union {
403 int (*f)(void *, char *, int);
404 void *p;
405 } t = {
406 dlfcn_pathbyaddr
407 };
408 addr = t.p;
409 }
410
411 if (dladdr(addr, &dli)) {
412 len = (int)strlen(dli.dli_fname);
413 if (sz <= 0) {
414 #ifdef _AIX
415 OPENSSL_free((void *)dli.dli_fname);
416 #endif
417 return len + 1;
418 }
419 if (len >= sz)
420 len = sz - 1;
421 memcpy(path, dli.dli_fname, len);
422 path[len++] = 0;
423 #ifdef _AIX
424 OPENSSL_free((void *)dli.dli_fname);
425 #endif
426 return len;
427 }
428
429 ERR_add_error_data(2, "dlfcn_pathbyaddr(): ", dlerror());
430 #endif
431 return -1;
432 }
433
434 static void *dlfcn_globallookup(const char *name)
435 {
436 void *ret = NULL, *handle = dlopen(NULL, RTLD_LAZY);
437
438 if (handle) {
439 ret = dlsym(handle, name);
440 dlclose(handle);
441 }
442
443 return ret;
444 }
445 #endif /* DSO_DLFCN */