VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/posix/ldrNative-posix.cpp

Last change on this file was 109061, checked in by vboxsync, 3 weeks ago

IPRT/ldrNative-posix.cpp: Comment about the dlerror problem from bugref:10892 / ticketref:22193.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 7.8 KB
Line 
1/* $Id: ldrNative-posix.cpp 109061 2025-04-23 11:48:48Z vboxsync $ */
2/** @file
3 * IPRT - Binary Image Loader, POSIX native.
4 */
5
6/*
7 * Copyright (C) 2006-2024 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.215389.xyz.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#define LOG_GROUP RTLOGGROUP_LDR
42#include <dlfcn.h>
43
44#include <iprt/ldr.h>
45#include <iprt/assert.h>
46#include <iprt/path.h>
47#include <iprt/alloca.h>
48#include <iprt/string.h>
49#include <iprt/err.h>
50#include <iprt/log.h>
51#include "internal/ldr.h"
52
53
54/*********************************************************************************************************************************
55* Global Variables *
56*********************************************************************************************************************************/
57#if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
58static const char g_szSuff[] = ".DLL";
59#elif defined(RT_OS_L4)
60static const char g_szSuff[] = ".s.so";
61#elif defined(RT_OS_DARWIN)
62static const char g_szSuff[] = ".dylib";
63#else
64static const char g_szSuff[] = ".so";
65#endif
66
67
68DECLHIDDEN(int) rtldrNativeLoad(const char *pszFilename, uintptr_t *phHandle, uint32_t fFlags, PRTERRINFO pErrInfo)
69{
70 /*
71 * Do we need to add an extension?
72 */
73 if (!RTPathHasSuffix(pszFilename) && !(fFlags & RTLDRLOAD_FLAGS_NO_SUFFIX))
74 {
75 size_t cch = strlen(pszFilename);
76 char *psz = (char *)alloca(cch + sizeof(g_szSuff));
77 if (!psz)
78 return RTErrInfoSet(pErrInfo, VERR_NO_MEMORY, "alloca failed");
79 memcpy(psz, pszFilename, cch);
80 memcpy(psz + cch, g_szSuff, sizeof(g_szSuff));
81 pszFilename = psz;
82 }
83
84 /*
85 * Attempt load.
86 */
87 int fFlagsNative = RTLD_NOW;
88 if (fFlags & RTLDRLOAD_FLAGS_GLOBAL)
89 fFlagsNative |= RTLD_GLOBAL;
90 else
91 fFlagsNative |= RTLD_LOCAL;
92 void *pvMod = dlopen(pszFilename, fFlagsNative);
93 if (pvMod)
94 {
95 *phHandle = (uintptr_t)pvMod;
96 return VINF_SUCCESS;
97 }
98
99 /* Note! In a hardened binary, dlerror may return NULL or obsolete information
100 because we intercept dlopen and dlmopen calls. See @bugref{109892}. */
101 const char *pszDlError = dlerror();
102 RTErrInfoSet(pErrInfo, VERR_FILE_NOT_FOUND, RT_VALID_PTR(pszDlError) ? pszDlError : "unknown dlopen error");
103 LogRel(("rtldrNativeLoad: dlopen('%s', RTLD_NOW | RTLD_LOCAL) failed: %s\n", pszFilename, pszDlError));
104 return VERR_FILE_NOT_FOUND;
105}
106
107
108DECLCALLBACK(int) rtldrNativeGetSymbol(PRTLDRMODINTERNAL pMod, const char *pszSymbol, void **ppvValue)
109{
110 PRTLDRMODNATIVE pModNative = (PRTLDRMODNATIVE)pMod;
111#ifdef RT_OS_OS2
112 /* Prefix the symbol with an underscore (assuming __cdecl/gcc-default). */
113 size_t cch = strlen(pszSymbol);
114 char *psz = (char *)alloca(cch + 2);
115 psz[0] = '_';
116 memcpy(psz + 1, pszSymbol, cch + 1);
117 pszSymbol = psz;
118#endif
119 *ppvValue = dlsym((void *)pModNative->hNative, pszSymbol);
120 if (*ppvValue)
121 return VINF_SUCCESS;
122 return VERR_SYMBOL_NOT_FOUND;
123}
124
125
126DECLCALLBACK(int) rtldrNativeClose(PRTLDRMODINTERNAL pMod)
127{
128 PRTLDRMODNATIVE pModNative = (PRTLDRMODNATIVE)pMod;
129#ifdef __SANITIZE_ADDRESS__
130 /* If we are compiled with enabled address sanitizer (gcc/llvm), don't
131 * unload the module to prevent <unknown module> in the stack trace */
132 pModNative->fFlags |= RTLDRLOAD_FLAGS_NO_UNLOAD;
133#endif
134 if ( (pModNative->fFlags & RTLDRLOAD_FLAGS_NO_UNLOAD)
135 || !dlclose((void *)pModNative->hNative))
136 {
137 pModNative->hNative = (uintptr_t)0;
138 return VINF_SUCCESS;
139 }
140 Log(("rtldrNativeFree: dlclose(%p) failed: %s\n", pModNative->hNative, dlerror()));
141 return VERR_GENERAL_FAILURE;
142}
143
144
145DECLHIDDEN(int) rtldrNativeLoadSystem(const char *pszFilename, const char *pszExt, uint32_t fFlags, PRTLDRMOD phLdrMod)
146{
147 /*
148 * For the present we ASSUME that we can trust dlopen to load what we want
149 * when not specifying a path. There seems to be very little we can do to
150 * restrict the places dlopen will search for library without doing
151 * auditing (linux) or something like that.
152 */
153 Assert(strchr(pszFilename, '/') == NULL);
154
155 uint32_t const fFlags2 = fFlags & ~(RTLDRLOAD_FLAGS_SO_VER_BEGIN_MASK | RTLDRLOAD_FLAGS_SO_VER_END_MASK);
156
157 /*
158 * If no suffix is given and we haven't got any RTLDRLOAD_FLAGS_SO_VER_ range to work
159 * with, we can call RTLdrLoadEx directly.
160 */
161 if (!pszExt)
162 {
163#if !defined(RT_OS_DARWIN) && !defined(RT_OS_OS2) && !defined(RT_OS_WINDOWS)
164 if ( (fFlags & RTLDRLOAD_FLAGS_SO_VER_BEGIN_MASK) >> RTLDRLOAD_FLAGS_SO_VER_BEGIN_SHIFT
165 == (fFlags & RTLDRLOAD_FLAGS_SO_VER_END_MASK) >> RTLDRLOAD_FLAGS_SO_VER_END_SHIFT)
166#endif
167 return RTLdrLoadEx(pszFilename, phLdrMod, fFlags2, NULL);
168 pszExt = "";
169 }
170
171 /*
172 * Combine filename and suffix and then do the loading.
173 */
174 size_t const cchFilename = strlen(pszFilename);
175 size_t const cchSuffix = strlen(pszExt);
176 char *pszTmp = (char *)alloca(cchFilename + cchSuffix + 16 + 1);
177 memcpy(pszTmp, pszFilename, cchFilename);
178 memcpy(&pszTmp[cchFilename], pszExt, cchSuffix);
179 pszTmp[cchFilename + cchSuffix] = '\0';
180
181 int rc = RTLdrLoadEx(pszTmp, phLdrMod, fFlags2, NULL);
182
183#if !defined(RT_OS_DARWIN) && !defined(RT_OS_OS2) && !defined(RT_OS_WINDOWS)
184 /*
185 * If no version was given after the .so and do .so.MAJOR search according
186 * to the range in the fFlags.
187 */
188 if (RT_FAILURE(rc) && !(fFlags & RTLDRLOAD_FLAGS_NO_SUFFIX))
189 {
190 const char *pszActualSuff = RTPathSuffix(pszTmp);
191 if (pszActualSuff && strcmp(pszActualSuff, ".so") == 0)
192 {
193 int32_t const iBegin = (fFlags & RTLDRLOAD_FLAGS_SO_VER_BEGIN_MASK) >> RTLDRLOAD_FLAGS_SO_VER_BEGIN_SHIFT;
194 int32_t const iEnd = (fFlags & RTLDRLOAD_FLAGS_SO_VER_END_MASK) >> RTLDRLOAD_FLAGS_SO_VER_END_SHIFT;
195 int32_t const iIncr = iBegin <= iEnd ? 1 : -1;
196 for (int32_t iMajorVer = iBegin; iMajorVer != iEnd; iMajorVer += iIncr)
197 {
198 RTStrPrintf(&pszTmp[cchFilename + cchSuffix], 16 + 1, ".%d", iMajorVer);
199 rc = RTLdrLoadEx(pszTmp, phLdrMod, fFlags2, NULL);
200 if (RT_SUCCESS(rc))
201 break;
202 }
203 }
204 }
205#endif
206
207 return rc;
208}
209
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette