-
Notifications
You must be signed in to change notification settings - Fork 0
/
dlmopen.c
87 lines (72 loc) · 1.89 KB
/
dlmopen.c
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
/*
* Copyright 2021-2024 Gaël PORTAY
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
#include <fcntl.h>
#include <dlfcn.h>
#include "iamroot.h"
#ifdef __GLIBC__
static void *(*sym)(Lmid_t, const char *, int);
hidden void *next_dlmopen(Lmid_t lmid, const char *path, int flags)
{
if (!sym)
sym = dlsym(RTLD_NEXT, "dlmopen");
if (!sym)
return __dl_set_errno_and_perror(ENOSYS, NULL);
return sym(lmid, path, flags);
}
void *dlmopen(Lmid_t lmid, const char *path, int flags)
{
char buf[PATH_MAX];
void *ret = NULL;
ssize_t siz;
int err;
/*
* According to dlopen(3):
*
* If filename is NULL, then the returned handle is for the main
* program.
*/
/* Do not proceed to any hack if not in chroot */
if (!path || !__inchroot())
return next_dlmopen(lmid, path, flags);
/*
* If filename contains a slash ("/"), then it is interpreted as a
* (relative or absolute) pathname.
*/
if (strchr(path, '/')) {
siz = path_resolution(AT_FDCWD, path, buf, sizeof(buf), 0);
if (siz == -1)
goto exit;
/*
* Otherwise, the dynamic linker searches for the object as follows
* (see ld.so(8) for further details):
*/
} else {
siz = __dl_access(path, F_OK, buf, sizeof(buf));
if (siz == -1)
goto exit;
}
/*
* If the object specified by filename has dependencies on other shared
* objects, then these are also automatically loaded by the dynamic
* linker using the same rules. (This process may occur recursively, if
* those objects in turn have dependencies, and so on.)
*/
/* Bypass the libdl.so loading of the DT_NEEDED shared objects. */
err = __dlopen_needed(buf, flags);
if (err == -1)
goto exit;
ret = next_dlmopen(lmid, buf, flags);
exit:
__debug("%s(..., path: '%s' -> '%s', flags: 0x%x) -> %p\n", __func__,
path, buf, flags, ret);
return ret;
}
#endif