]>
Commit | Line | Data |
---|---|---|
62f0afa2 | 1 | /* dso_dl.c */ |
d3819813 MTL |
2 | /* |
3 | * Written by Richard Levitte (richard@levitte.org) for the OpenSSL project | |
4 | * 2000. | |
3e575651 SL |
5 | */ |
6 | /* ==================================================================== | |
7 | * Copyright (c) 2000 The OpenSSL Project. All rights reserved. | |
8 | * | |
9 | * Redistribution and use in source and binary forms, with or without | |
10 | * modification, are permitted provided that the following conditions | |
11 | * are met: | |
12 | * | |
13 | * 1. Redistributions of source code must retain the above copyright | |
d3819813 | 14 | * notice, this list of conditions and the following disclaimer. |
3e575651 SL |
15 | * |
16 | * 2. Redistributions in binary form must reproduce the above copyright | |
17 | * notice, this list of conditions and the following disclaimer in | |
18 | * the documentation and/or other materials provided with the | |
19 | * distribution. | |
20 | * | |
21 | * 3. All advertising materials mentioning features or use of this | |
22 | * software must display the following acknowledgment: | |
23 | * "This product includes software developed by the OpenSSL Project | |
24 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | |
25 | * | |
26 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | |
27 | * endorse or promote products derived from this software without | |
28 | * prior written permission. For written permission, please contact | |
29 | * licensing@OpenSSL.org. | |
30 | * | |
31 | * 5. Products derived from this software may not be called "OpenSSL" | |
32 | * nor may "OpenSSL" appear in their names without prior written | |
33 | * permission of the OpenSSL Project. | |
34 | * | |
35 | * 6. Redistributions of any form whatsoever must retain the following | |
36 | * acknowledgment: | |
37 | * "This product includes software developed by the OpenSSL Project | |
38 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | |
39 | * | |
40 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | |
41 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
43 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | |
44 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
45 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
46 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
47 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
49 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
50 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
51 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
52 | * ==================================================================== | |
53 | * | |
54 | * This product includes cryptographic software written by Eric Young | |
55 | * (eay@cryptsoft.com). This product includes software written by Tim | |
56 | * Hudson (tjh@cryptsoft.com). | |
57 | * | |
58 | */ | |
59 | ||
60 | #include <stdio.h> | |
61 | #include "cryptlib.h" | |
62 | #include <openssl/dso.h> | |
63 | ||
64 | #ifndef DSO_DL | |
65 | DSO_METHOD *DSO_METHOD_dl(void) | |
d3819813 MTL |
66 | { |
67 | return NULL; | |
68 | } | |
3e575651 SL |
69 | #else |
70 | ||
d3819813 | 71 | # include <dl.h> |
3e575651 SL |
72 | |
73 | /* Part of the hack in "dl_load" ... */ | |
d3819813 | 74 | # define DSO_MAX_TRANSLATED_SIZE 256 |
3e575651 SL |
75 | |
76 | static int dl_load(DSO *dso); | |
77 | static int dl_unload(DSO *dso); | |
78 | static void *dl_bind_var(DSO *dso, const char *symname); | |
79 | static DSO_FUNC_TYPE dl_bind_func(DSO *dso, const char *symname); | |
d3819813 | 80 | # if 0 |
3e575651 SL |
81 | static int dl_unbind_var(DSO *dso, char *symname, void *symptr); |
82 | static int dl_unbind_func(DSO *dso, char *symname, DSO_FUNC_TYPE symptr); | |
83 | static int dl_init(DSO *dso); | |
84 | static int dl_finish(DSO *dso); | |
85 | static int dl_ctrl(DSO *dso, int cmd, long larg, void *parg); | |
d3819813 | 86 | # endif |
3e575651 | 87 | static char *dl_name_converter(DSO *dso, const char *filename); |
d3819813 MTL |
88 | static char *dl_merger(DSO *dso, const char *filespec1, |
89 | const char *filespec2); | |
90 | static int dl_pathbyaddr(void *addr, char *path, int sz); | |
91 | static void *dl_globallookup(const char *name); | |
3e575651 SL |
92 | |
93 | static DSO_METHOD dso_meth_dl = { | |
d3819813 MTL |
94 | "OpenSSL 'dl' shared library method", |
95 | dl_load, | |
96 | dl_unload, | |
97 | dl_bind_var, | |
98 | dl_bind_func, | |
3e575651 | 99 | /* For now, "unbind" doesn't exist */ |
d3819813 MTL |
100 | # if 0 |
101 | NULL, /* unbind_var */ | |
102 | NULL, /* unbind_func */ | |
103 | # endif | |
104 | NULL, /* ctrl */ | |
105 | dl_name_converter, | |
106 | dl_merger, | |
107 | NULL, /* init */ | |
108 | NULL, /* finish */ | |
109 | dl_pathbyaddr, | |
110 | dl_globallookup | |
111 | }; | |
3e575651 SL |
112 | |
113 | DSO_METHOD *DSO_METHOD_dl(void) | |
d3819813 MTL |
114 | { |
115 | return (&dso_meth_dl); | |
116 | } | |
3e575651 | 117 | |
d3819813 MTL |
118 | /* |
119 | * For this DSO_METHOD, our meth_data STACK will contain; (i) the handle | |
120 | * (shl_t) returned from shl_load(). NB: I checked on HPUX11 and shl_t is | |
121 | * itself a pointer type so the cast is safe. | |
3e575651 SL |
122 | */ |
123 | ||
124 | static int dl_load(DSO *dso) | |
d3819813 MTL |
125 | { |
126 | shl_t ptr = NULL; | |
127 | /* | |
128 | * We don't do any fancy retries or anything, just take the method's (or | |
129 | * DSO's if it has the callback set) best translation of the | |
130 | * platform-independant filename and try once with that. | |
131 | */ | |
132 | char *filename = DSO_convert_filename(dso, NULL); | |
3e575651 | 133 | |
d3819813 MTL |
134 | if (filename == NULL) { |
135 | DSOerr(DSO_F_DL_LOAD, DSO_R_NO_FILENAME); | |
136 | goto err; | |
137 | } | |
138 | ptr = shl_load(filename, BIND_IMMEDIATE | | |
139 | (dso->flags & DSO_FLAG_NO_NAME_TRANSLATION ? 0 : | |
140 | DYNAMIC_PATH), 0L); | |
141 | if (ptr == NULL) { | |
142 | DSOerr(DSO_F_DL_LOAD, DSO_R_LOAD_FAILED); | |
143 | ERR_add_error_data(4, "filename(", filename, "): ", strerror(errno)); | |
144 | goto err; | |
145 | } | |
146 | if (!sk_push(dso->meth_data, (char *)ptr)) { | |
147 | DSOerr(DSO_F_DL_LOAD, DSO_R_STACK_ERROR); | |
148 | goto err; | |
149 | } | |
150 | /* | |
151 | * Success, stick the converted filename we've loaded under into the DSO | |
152 | * (it also serves as the indicator that we are currently loaded). | |
153 | */ | |
154 | dso->loaded_filename = filename; | |
155 | return (1); | |
156 | err: | |
157 | /* Cleanup! */ | |
158 | if (filename != NULL) | |
159 | OPENSSL_free(filename); | |
160 | if (ptr != NULL) | |
161 | shl_unload(ptr); | |
162 | return (0); | |
163 | } | |
3e575651 SL |
164 | |
165 | static int dl_unload(DSO *dso) | |
d3819813 MTL |
166 | { |
167 | shl_t ptr; | |
168 | if (dso == NULL) { | |
169 | DSOerr(DSO_F_DL_UNLOAD, ERR_R_PASSED_NULL_PARAMETER); | |
170 | return (0); | |
171 | } | |
172 | if (sk_num(dso->meth_data) < 1) | |
173 | return (1); | |
174 | /* Is this statement legal? */ | |
175 | ptr = (shl_t) sk_pop(dso->meth_data); | |
176 | if (ptr == NULL) { | |
177 | DSOerr(DSO_F_DL_UNLOAD, DSO_R_NULL_HANDLE); | |
178 | /* | |
179 | * Should push the value back onto the stack in case of a retry. | |
180 | */ | |
181 | sk_push(dso->meth_data, (char *)ptr); | |
182 | return (0); | |
183 | } | |
184 | shl_unload(ptr); | |
185 | return (1); | |
186 | } | |
3e575651 SL |
187 | |
188 | static void *dl_bind_var(DSO *dso, const char *symname) | |
d3819813 MTL |
189 | { |
190 | shl_t ptr; | |
191 | void *sym; | |
3e575651 | 192 | |
d3819813 MTL |
193 | if ((dso == NULL) || (symname == NULL)) { |
194 | DSOerr(DSO_F_DL_BIND_VAR, ERR_R_PASSED_NULL_PARAMETER); | |
195 | return (NULL); | |
196 | } | |
197 | if (sk_num(dso->meth_data) < 1) { | |
198 | DSOerr(DSO_F_DL_BIND_VAR, DSO_R_STACK_ERROR); | |
199 | return (NULL); | |
200 | } | |
201 | ptr = (shl_t) sk_value(dso->meth_data, sk_num(dso->meth_data) - 1); | |
202 | if (ptr == NULL) { | |
203 | DSOerr(DSO_F_DL_BIND_VAR, DSO_R_NULL_HANDLE); | |
204 | return (NULL); | |
205 | } | |
206 | if (shl_findsym(&ptr, symname, TYPE_UNDEFINED, &sym) < 0) { | |
207 | DSOerr(DSO_F_DL_BIND_VAR, DSO_R_SYM_FAILURE); | |
208 | ERR_add_error_data(4, "symname(", symname, "): ", strerror(errno)); | |
209 | return (NULL); | |
210 | } | |
211 | return (sym); | |
212 | } | |
3e575651 SL |
213 | |
214 | static DSO_FUNC_TYPE dl_bind_func(DSO *dso, const char *symname) | |
d3819813 MTL |
215 | { |
216 | shl_t ptr; | |
217 | void *sym; | |
3e575651 | 218 | |
d3819813 MTL |
219 | if ((dso == NULL) || (symname == NULL)) { |
220 | DSOerr(DSO_F_DL_BIND_FUNC, ERR_R_PASSED_NULL_PARAMETER); | |
221 | return (NULL); | |
222 | } | |
223 | if (sk_num(dso->meth_data) < 1) { | |
224 | DSOerr(DSO_F_DL_BIND_FUNC, DSO_R_STACK_ERROR); | |
225 | return (NULL); | |
226 | } | |
227 | ptr = (shl_t) sk_value(dso->meth_data, sk_num(dso->meth_data) - 1); | |
228 | if (ptr == NULL) { | |
229 | DSOerr(DSO_F_DL_BIND_FUNC, DSO_R_NULL_HANDLE); | |
230 | return (NULL); | |
231 | } | |
232 | if (shl_findsym(&ptr, symname, TYPE_UNDEFINED, &sym) < 0) { | |
233 | DSOerr(DSO_F_DL_BIND_FUNC, DSO_R_SYM_FAILURE); | |
234 | ERR_add_error_data(4, "symname(", symname, "): ", strerror(errno)); | |
235 | return (NULL); | |
236 | } | |
237 | return ((DSO_FUNC_TYPE)sym); | |
238 | } | |
3e575651 SL |
239 | |
240 | static char *dl_merger(DSO *dso, const char *filespec1, const char *filespec2) | |
d3819813 MTL |
241 | { |
242 | char *merged; | |
3e575651 | 243 | |
d3819813 MTL |
244 | if (!filespec1 && !filespec2) { |
245 | DSOerr(DSO_F_DL_MERGER, ERR_R_PASSED_NULL_PARAMETER); | |
246 | return (NULL); | |
247 | } | |
248 | /* | |
249 | * If the first file specification is a rooted path, it rules. same goes | |
250 | * if the second file specification is missing. | |
251 | */ | |
252 | if (!filespec2 || filespec1[0] == '/') { | |
253 | merged = OPENSSL_malloc(strlen(filespec1) + 1); | |
254 | if (!merged) { | |
255 | DSOerr(DSO_F_DL_MERGER, ERR_R_MALLOC_FAILURE); | |
256 | return (NULL); | |
257 | } | |
258 | strcpy(merged, filespec1); | |
259 | } | |
260 | /* | |
261 | * If the first file specification is missing, the second one rules. | |
262 | */ | |
263 | else if (!filespec1) { | |
264 | merged = OPENSSL_malloc(strlen(filespec2) + 1); | |
265 | if (!merged) { | |
266 | DSOerr(DSO_F_DL_MERGER, ERR_R_MALLOC_FAILURE); | |
267 | return (NULL); | |
268 | } | |
269 | strcpy(merged, filespec2); | |
270 | } else | |
271 | /* | |
272 | * This part isn't as trivial as it looks. It assumes that the | |
273 | * second file specification really is a directory, and makes no | |
274 | * checks whatsoever. Therefore, the result becomes the | |
275 | * concatenation of filespec2 followed by a slash followed by | |
276 | * filespec1. | |
277 | */ | |
278 | { | |
279 | int spec2len, len; | |
3e575651 | 280 | |
d3819813 MTL |
281 | spec2len = (filespec2 ? strlen(filespec2) : 0); |
282 | len = spec2len + (filespec1 ? strlen(filespec1) : 0); | |
3e575651 | 283 | |
d3819813 MTL |
284 | if (filespec2 && filespec2[spec2len - 1] == '/') { |
285 | spec2len--; | |
286 | len--; | |
287 | } | |
288 | merged = OPENSSL_malloc(len + 2); | |
289 | if (!merged) { | |
290 | DSOerr(DSO_F_DL_MERGER, ERR_R_MALLOC_FAILURE); | |
291 | return (NULL); | |
292 | } | |
293 | strcpy(merged, filespec2); | |
294 | merged[spec2len] = '/'; | |
295 | strcpy(&merged[spec2len + 1], filespec1); | |
296 | } | |
297 | return (merged); | |
298 | } | |
3e575651 | 299 | |
d3819813 MTL |
300 | /* |
301 | * This function is identical to the one in dso_dlfcn.c, but as it is highly | |
302 | * unlikely that both the "dl" *and* "dlfcn" variants are being compiled at | |
303 | * the same time, there's no great duplicating the code. Figuring out an | |
304 | * elegant way to share one copy of the code would be more difficult and | |
305 | * would not leave the implementations independant. | |
306 | */ | |
307 | # if defined(__hpux) | |
3e575651 | 308 | static const char extension[] = ".sl"; |
d3819813 | 309 | # else |
3e575651 | 310 | static const char extension[] = ".so"; |
d3819813 | 311 | # endif |
3e575651 | 312 | static char *dl_name_converter(DSO *dso, const char *filename) |
d3819813 MTL |
313 | { |
314 | char *translated; | |
315 | int len, rsize, transform; | |
316 | ||
317 | len = strlen(filename); | |
318 | rsize = len + 1; | |
319 | transform = (strstr(filename, "/") == NULL); | |
320 | { | |
321 | /* We will convert this to "%s.s?" or "lib%s.s?" */ | |
322 | rsize += strlen(extension); /* The length of ".s?" */ | |
323 | if ((DSO_flags(dso) & DSO_FLAG_NAME_TRANSLATION_EXT_ONLY) == 0) | |
324 | rsize += 3; /* The length of "lib" */ | |
325 | } | |
326 | translated = OPENSSL_malloc(rsize); | |
327 | if (translated == NULL) { | |
328 | DSOerr(DSO_F_DL_NAME_CONVERTER, DSO_R_NAME_TRANSLATION_FAILED); | |
329 | return (NULL); | |
330 | } | |
331 | if (transform) { | |
332 | if ((DSO_flags(dso) & DSO_FLAG_NAME_TRANSLATION_EXT_ONLY) == 0) | |
333 | sprintf(translated, "lib%s%s", filename, extension); | |
334 | else | |
335 | sprintf(translated, "%s%s", filename, extension); | |
336 | } else | |
337 | sprintf(translated, "%s", filename); | |
338 | return (translated); | |
339 | } | |
340 | ||
341 | static int dl_pathbyaddr(void *addr, char *path, int sz) | |
342 | { | |
343 | struct shl_descriptor inf; | |
344 | int i, len; | |
345 | ||
346 | if (addr == NULL) { | |
347 | union { | |
348 | int (*f) (void *, char *, int); | |
349 | void *p; | |
350 | } t = { | |
351 | dl_pathbyaddr | |
352 | }; | |
353 | addr = t.p; | |
354 | } | |
355 | ||
356 | for (i = -1; shl_get_r(i, &inf) == 0; i++) { | |
357 | if (((size_t)addr >= inf.tstart && (size_t)addr < inf.tend) || | |
358 | ((size_t)addr >= inf.dstart && (size_t)addr < inf.dend)) { | |
359 | len = (int)strlen(inf.filename); | |
360 | if (sz <= 0) | |
361 | return len + 1; | |
362 | if (len >= sz) | |
363 | len = sz - 1; | |
364 | memcpy(path, inf.filename, len); | |
365 | path[len++] = 0; | |
366 | return len; | |
367 | } | |
368 | } | |
369 | ||
370 | return -1; | |
371 | } | |
3e575651 | 372 | |
d3819813 MTL |
373 | static void *dl_globallookup(const char *name) |
374 | { | |
375 | void *ret; | |
376 | shl_t h = NULL; | |
3e575651 | 377 | |
d3819813 MTL |
378 | return shl_findsym(&h, name, TYPE_UNDEFINED, &ret) ? NULL : ret; |
379 | } | |
380 | #endif /* DSO_DL */ |