Coverage Report

Created: 2026-08-31 06:56

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