Use strerror_r()/strerror_s() instead of strerror() where possible
[openssl.git] / crypto / dso / dso_dl.c
1 /*
2  * Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (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 #include "dso_locl.h"
11
12 #ifdef DSO_DL
13
14 # include <dl.h>
15
16 /* Part of the hack in "dl_load" ... */
17 # define DSO_MAX_TRANSLATED_SIZE 256
18
19 static int dl_load(DSO *dso);
20 static int dl_unload(DSO *dso);
21 static DSO_FUNC_TYPE dl_bind_func(DSO *dso, const char *symname);
22 static char *dl_name_converter(DSO *dso, const char *filename);
23 static char *dl_merger(DSO *dso, const char *filespec1,
24                        const char *filespec2);
25 static void *dl_globallookup(const char *name);
26
27 static DSO_METHOD dso_meth_dl = {
28     "OpenSSL 'dl' shared library method",
29     dl_load,
30     dl_unload,
31     dl_bind_func,
32     NULL,                       /* ctrl */
33     dl_name_converter,
34     dl_merger,
35     NULL,                       /* init */
36     NULL,                       /* finish */
37     dl_globallookup
38 };
39
40 DSO_METHOD *DSO_METHOD_openssl(void)
41 {
42     return &dso_meth_dl;
43 }
44
45 /*
46  * For this DSO_METHOD, our meth_data STACK will contain; (i) the handle
47  * (shl_t) returned from shl_load(). NB: I checked on HPUX11 and shl_t is
48  * itself a pointer type so the cast is safe.
49  */
50
51 static int dl_load(DSO *dso)
52 {
53     shl_t ptr = NULL;
54     /*
55      * We don't do any fancy retries or anything, just take the method's (or
56      * DSO's if it has the callback set) best translation of the
57      * platform-independent filename and try once with that.
58      */
59     char *filename = DSO_convert_filename(dso, NULL);
60
61     if (filename == NULL) {
62         DSOerr(DSO_F_DL_LOAD, DSO_R_NO_FILENAME);
63         goto err;
64     }
65     ptr = shl_load(filename, BIND_IMMEDIATE |
66                    (dso->flags & DSO_FLAG_NO_NAME_TRANSLATION ? 0 :
67                     DYNAMIC_PATH), 0L);
68     if (ptr == NULL) {
69         char errbuf[160];
70         DSOerr(DSO_F_DL_LOAD, DSO_R_LOAD_FAILED);
71         if (openssl_strerror_r(errno, errbuf, sizeof(errbuf)))
72             ERR_add_error_data(4, "filename(", filename, "): ", errbuf);
73         goto err;
74     }
75     if (!sk_push(dso->meth_data, (char *)ptr)) {
76         DSOerr(DSO_F_DL_LOAD, DSO_R_STACK_ERROR);
77         goto err;
78     }
79     /*
80      * Success, stick the converted filename we've loaded under into the DSO
81      * (it also serves as the indicator that we are currently loaded).
82      */
83     dso->loaded_filename = filename;
84     return (1);
85  err:
86     /* Cleanup! */
87     OPENSSL_free(filename);
88     if (ptr != NULL)
89         shl_unload(ptr);
90     return (0);
91 }
92
93 static int dl_unload(DSO *dso)
94 {
95     shl_t ptr;
96     if (dso == NULL) {
97         DSOerr(DSO_F_DL_UNLOAD, ERR_R_PASSED_NULL_PARAMETER);
98         return (0);
99     }
100     if (sk_num(dso->meth_data) < 1)
101         return (1);
102     /* Is this statement legal? */
103     ptr = (shl_t) sk_pop(dso->meth_data);
104     if (ptr == NULL) {
105         DSOerr(DSO_F_DL_UNLOAD, DSO_R_NULL_HANDLE);
106         /*
107          * Should push the value back onto the stack in case of a retry.
108          */
109         sk_push(dso->meth_data, (char *)ptr);
110         return (0);
111     }
112     shl_unload(ptr);
113     return (1);
114 }
115
116 static DSO_FUNC_TYPE dl_bind_func(DSO *dso, const char *symname)
117 {
118     shl_t ptr;
119     void *sym;
120
121     if ((dso == NULL) || (symname == NULL)) {
122         DSOerr(DSO_F_DL_BIND_FUNC, ERR_R_PASSED_NULL_PARAMETER);
123         return (NULL);
124     }
125     if (sk_num(dso->meth_data) < 1) {
126         DSOerr(DSO_F_DL_BIND_FUNC, DSO_R_STACK_ERROR);
127         return (NULL);
128     }
129     ptr = (shl_t) sk_value(dso->meth_data, sk_num(dso->meth_data) - 1);
130     if (ptr == NULL) {
131         DSOerr(DSO_F_DL_BIND_FUNC, DSO_R_NULL_HANDLE);
132         return (NULL);
133     }
134     if (shl_findsym(&ptr, symname, TYPE_UNDEFINED, &sym) < 0) {
135         char errbuf[160];
136         DSOerr(DSO_F_DL_BIND_FUNC, DSO_R_SYM_FAILURE);
137         if (openssl_strerror_r(errno, errbuf, sizeof(errbuf)))
138             ERR_add_error_data(4, "symname(", symname, "): ", errbuf);
139         return (NULL);
140     }
141     return ((DSO_FUNC_TYPE)sym);
142 }
143
144 static char *dl_merger(DSO *dso, const char *filespec1, const char *filespec2)
145 {
146     char *merged;
147
148     if (!filespec1 && !filespec2) {
149         DSOerr(DSO_F_DL_MERGER, ERR_R_PASSED_NULL_PARAMETER);
150         return (NULL);
151     }
152     /*
153      * If the first file specification is a rooted path, it rules. same goes
154      * if the second file specification is missing.
155      */
156     if (!filespec2 || filespec1[0] == '/') {
157         merged = OPENSSL_strdup(filespec1);
158         if (merged == NULL) {
159             DSOerr(DSO_F_DL_MERGER, ERR_R_MALLOC_FAILURE);
160             return (NULL);
161         }
162     }
163     /*
164      * If the first file specification is missing, the second one rules.
165      */
166     else if (!filespec1) {
167         merged = OPENSSL_strdup(filespec2);
168         if (merged == NULL) {
169             DSOerr(DSO_F_DL_MERGER, ERR_R_MALLOC_FAILURE);
170             return (NULL);
171         }
172     } else
173         /*
174          * This part isn't as trivial as it looks.  It assumes that the
175          * second file specification really is a directory, and makes no
176          * checks whatsoever.  Therefore, the result becomes the
177          * concatenation of filespec2 followed by a slash followed by
178          * filespec1.
179          */
180     {
181         int spec2len, len;
182
183         spec2len = (filespec2 ? strlen(filespec2) : 0);
184         len = spec2len + (filespec1 ? strlen(filespec1) : 0);
185
186         if (spec2len && filespec2[spec2len - 1] == '/') {
187             spec2len--;
188             len--;
189         }
190         merged = OPENSSL_malloc(len + 2);
191         if (merged == NULL) {
192             DSOerr(DSO_F_DL_MERGER, ERR_R_MALLOC_FAILURE);
193             return (NULL);
194         }
195         strcpy(merged, filespec2);
196         merged[spec2len] = '/';
197         strcpy(&merged[spec2len + 1], filespec1);
198     }
199     return (merged);
200 }
201
202 /*
203  * This function is identical to the one in dso_dlfcn.c, but as it is highly
204  * unlikely that both the "dl" *and* "dlfcn" variants are being compiled at
205  * the same time, there's no great duplicating the code. Figuring out an
206  * elegant way to share one copy of the code would be more difficult and
207  * would not leave the implementations independent.
208  */
209 static char *dl_name_converter(DSO *dso, const char *filename)
210 {
211     char *translated;
212     int len, rsize, transform;
213
214     len = strlen(filename);
215     rsize = len + 1;
216     transform = (strstr(filename, "/") == NULL);
217     {
218         /* We will convert this to "%s.s?" or "lib%s.s?" */
219         rsize += strlen(DSO_EXTENSION); /* The length of ".s?" */
220         if ((DSO_flags(dso) & DSO_FLAG_NAME_TRANSLATION_EXT_ONLY) == 0)
221             rsize += 3;         /* The length of "lib" */
222     }
223     translated = OPENSSL_malloc(rsize);
224     if (translated == NULL) {
225         DSOerr(DSO_F_DL_NAME_CONVERTER, DSO_R_NAME_TRANSLATION_FAILED);
226         return (NULL);
227     }
228     if (transform) {
229         if ((DSO_flags(dso) & DSO_FLAG_NAME_TRANSLATION_EXT_ONLY) == 0)
230             sprintf(translated, "lib%s%s", filename, DSO_EXTENSION);
231         else
232             sprintf(translated, "%s%s", filename, DSO_EXTENSION);
233     } else
234         sprintf(translated, "%s", filename);
235     return (translated);
236 }
237
238 static void *dl_globallookup(const char *name)
239 {
240     void *ret;
241     shl_t h = NULL;
242
243     return shl_findsym(&h, name, TYPE_UNDEFINED, &ret) ? NULL : ret;
244 }
245 #endif                          /* DSO_DL */