Coverage Report

Created: 2018-08-29 13:53

/src/openssl/crypto/dso/dso_lib.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2000-2017 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
#include "internal/refcount.h"
12
13
static DSO_METHOD *default_DSO_meth = NULL;
14
15
static DSO *DSO_new_method(DSO_METHOD *meth)
16
0
{
17
0
    DSO *ret;
18
0
19
0
    if (default_DSO_meth == NULL) {
20
0
        /*
21
0
         * We default to DSO_METH_openssl() which in turn defaults to
22
0
         * stealing the "best available" method. Will fallback to
23
0
         * DSO_METH_null() in the worst case.
24
0
         */
25
0
        default_DSO_meth = DSO_METHOD_openssl();
26
0
    }
27
0
    ret = OPENSSL_zalloc(sizeof(*ret));
28
0
    if (ret == NULL) {
29
0
        DSOerr(DSO_F_DSO_NEW_METHOD, ERR_R_MALLOC_FAILURE);
30
0
        return NULL;
31
0
    }
32
0
    ret->meth_data = sk_void_new_null();
33
0
    if (ret->meth_data == NULL) {
34
0
        /* sk_new doesn't generate any errors so we do */
35
0
        DSOerr(DSO_F_DSO_NEW_METHOD, ERR_R_MALLOC_FAILURE);
36
0
        OPENSSL_free(ret);
37
0
        return NULL;
38
0
    }
39
0
    ret->meth = default_DSO_meth;
40
0
    ret->references = 1;
41
0
    ret->lock = CRYPTO_THREAD_lock_new();
42
0
    if (ret->lock == NULL) {
43
0
        DSOerr(DSO_F_DSO_NEW_METHOD, ERR_R_MALLOC_FAILURE);
44
0
        sk_void_free(ret->meth_data);
45
0
        OPENSSL_free(ret);
46
0
        return NULL;
47
0
    }
48
0
49
0
    if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
50
0
        DSO_free(ret);
51
0
        ret = NULL;
52
0
    }
53
0
54
0
    return ret;
55
0
}
56
57
DSO *DSO_new(void)
58
0
{
59
0
    return DSO_new_method(NULL);
60
0
}
61
62
int DSO_free(DSO *dso)
63
0
{
64
0
    int i;
65
0
66
0
    if (dso == NULL)
67
0
        return 1;
68
0
69
0
    if (CRYPTO_DOWN_REF(&dso->references, &i, dso->lock) <= 0)
70
0
        return 0;
71
0
72
0
    REF_PRINT_COUNT("DSO", dso);
73
0
    if (i > 0)
74
0
        return 1;
75
0
    REF_ASSERT_ISNT(i < 0);
76
0
77
0
    if ((dso->flags & DSO_FLAG_NO_UNLOAD_ON_FREE) == 0) {
78
0
        if ((dso->meth->dso_unload != NULL) && !dso->meth->dso_unload(dso)) {
79
0
            DSOerr(DSO_F_DSO_FREE, DSO_R_UNLOAD_FAILED);
80
0
            return 0;
81
0
        }
82
0
    }
83
0
84
0
    if ((dso->meth->finish != NULL) && !dso->meth->finish(dso)) {
85
0
        DSOerr(DSO_F_DSO_FREE, DSO_R_FINISH_FAILED);
86
0
        return 0;
87
0
    }
88
0
89
0
    sk_void_free(dso->meth_data);
90
0
    OPENSSL_free(dso->filename);
91
0
    OPENSSL_free(dso->loaded_filename);
92
0
    CRYPTO_THREAD_lock_free(dso->lock);
93
0
    OPENSSL_free(dso);
94
0
    return 1;
95
0
}
96
97
int DSO_flags(DSO *dso)
98
0
{
99
0
    return ((dso == NULL) ? 0 : dso->flags);
100
0
}
101
102
int DSO_up_ref(DSO *dso)
103
0
{
104
0
    int i;
105
0
106
0
    if (dso == NULL) {
107
0
        DSOerr(DSO_F_DSO_UP_REF, ERR_R_PASSED_NULL_PARAMETER);
108
0
        return 0;
109
0
    }
110
0
111
0
    if (CRYPTO_UP_REF(&dso->references, &i, dso->lock) <= 0)
112
0
        return 0;
113
0
114
0
    REF_PRINT_COUNT("DSO", r);
115
0
    REF_ASSERT_ISNT(i < 2);
116
0
    return ((i > 1) ? 1 : 0);
117
0
}
118
119
DSO *DSO_load(DSO *dso, const char *filename, DSO_METHOD *meth, int flags)
120
0
{
121
0
    DSO *ret;
122
0
    int allocated = 0;
123
0
124
0
    if (dso == NULL) {
125
0
        ret = DSO_new_method(meth);
126
0
        if (ret == NULL) {
127
0
            DSOerr(DSO_F_DSO_LOAD, ERR_R_MALLOC_FAILURE);
128
0
            goto err;
129
0
        }
130
0
        allocated = 1;
131
0
        /* Pass the provided flags to the new DSO object */
132
0
        if (DSO_ctrl(ret, DSO_CTRL_SET_FLAGS, flags, NULL) < 0) {
133
0
            DSOerr(DSO_F_DSO_LOAD, DSO_R_CTRL_FAILED);
134
0
            goto err;
135
0
        }
136
0
    } else
137
0
        ret = dso;
138
0
    /* Don't load if we're currently already loaded */
139
0
    if (ret->filename != NULL) {
140
0
        DSOerr(DSO_F_DSO_LOAD, DSO_R_DSO_ALREADY_LOADED);
141
0
        goto err;
142
0
    }
143
0
    /*
144
0
     * filename can only be NULL if we were passed a dso that already has one
145
0
     * set.
146
0
     */
147
0
    if (filename != NULL)
148
0
        if (!DSO_set_filename(ret, filename)) {
149
0
            DSOerr(DSO_F_DSO_LOAD, DSO_R_SET_FILENAME_FAILED);
150
0
            goto err;
151
0
        }
152
0
    filename = ret->filename;
153
0
    if (filename == NULL) {
154
0
        DSOerr(DSO_F_DSO_LOAD, DSO_R_NO_FILENAME);
155
0
        goto err;
156
0
    }
157
0
    if (ret->meth->dso_load == NULL) {
158
0
        DSOerr(DSO_F_DSO_LOAD, DSO_R_UNSUPPORTED);
159
0
        goto err;
160
0
    }
161
0
    if (!ret->meth->dso_load(ret)) {
162
0
        DSOerr(DSO_F_DSO_LOAD, DSO_R_LOAD_FAILED);
163
0
        goto err;
164
0
    }
165
0
    /* Load succeeded */
166
0
    return ret;
167
0
 err:
168
0
    if (allocated)
169
0
        DSO_free(ret);
170
0
    return NULL;
171
0
}
172
173
DSO_FUNC_TYPE DSO_bind_func(DSO *dso, const char *symname)
174
0
{
175
0
    DSO_FUNC_TYPE ret = NULL;
176
0
177
0
    if ((dso == NULL) || (symname == NULL)) {
178
0
        DSOerr(DSO_F_DSO_BIND_FUNC, ERR_R_PASSED_NULL_PARAMETER);
179
0
        return NULL;
180
0
    }
181
0
    if (dso->meth->dso_bind_func == NULL) {
182
0
        DSOerr(DSO_F_DSO_BIND_FUNC, DSO_R_UNSUPPORTED);
183
0
        return NULL;
184
0
    }
185
0
    if ((ret = dso->meth->dso_bind_func(dso, symname)) == NULL) {
186
0
        DSOerr(DSO_F_DSO_BIND_FUNC, DSO_R_SYM_FAILURE);
187
0
        return NULL;
188
0
    }
189
0
    /* Success */
190
0
    return ret;
191
0
}
192
193
/*
194
 * I don't really like these *_ctrl functions very much to be perfectly
195
 * honest. For one thing, I think I have to return a negative value for any
196
 * error because possible DSO_ctrl() commands may return values such as
197
 * "size"s that can legitimately be zero (making the standard
198
 * "if (DSO_cmd(...))" form that works almost everywhere else fail at odd
199
 * times. I'd prefer "output" values to be passed by reference and the return
200
 * value as success/failure like usual ... but we conform when we must... :-)
201
 */
202
long DSO_ctrl(DSO *dso, int cmd, long larg, void *parg)
203
0
{
204
0
    if (dso == NULL) {
205
0
        DSOerr(DSO_F_DSO_CTRL, ERR_R_PASSED_NULL_PARAMETER);
206
0
        return -1;
207
0
    }
208
0
    /*
209
0
     * We should intercept certain generic commands and only pass control to
210
0
     * the method-specific ctrl() function if it's something we don't handle.
211
0
     */
212
0
    switch (cmd) {
213
0
    case DSO_CTRL_GET_FLAGS:
214
0
        return dso->flags;
215
0
    case DSO_CTRL_SET_FLAGS:
216
0
        dso->flags = (int)larg;
217
0
        return 0;
218
0
    case DSO_CTRL_OR_FLAGS:
219
0
        dso->flags |= (int)larg;
220
0
        return 0;
221
0
    default:
222
0
        break;
223
0
    }
224
0
    if ((dso->meth == NULL) || (dso->meth->dso_ctrl == NULL)) {
225
0
        DSOerr(DSO_F_DSO_CTRL, DSO_R_UNSUPPORTED);
226
0
        return -1;
227
0
    }
228
0
    return dso->meth->dso_ctrl(dso, cmd, larg, parg);
229
0
}
230
231
const char *DSO_get_filename(DSO *dso)
232
0
{
233
0
    if (dso == NULL) {
234
0
        DSOerr(DSO_F_DSO_GET_FILENAME, ERR_R_PASSED_NULL_PARAMETER);
235
0
        return NULL;
236
0
    }
237
0
    return dso->filename;
238
0
}
239
240
int DSO_set_filename(DSO *dso, const char *filename)
241
0
{
242
0
    char *copied;
243
0
244
0
    if ((dso == NULL) || (filename == NULL)) {
245
0
        DSOerr(DSO_F_DSO_SET_FILENAME, ERR_R_PASSED_NULL_PARAMETER);
246
0
        return 0;
247
0
    }
248
0
    if (dso->loaded_filename) {
249
0
        DSOerr(DSO_F_DSO_SET_FILENAME, DSO_R_DSO_ALREADY_LOADED);
250
0
        return 0;
251
0
    }
252
0
    /* We'll duplicate filename */
253
0
    copied = OPENSSL_strdup(filename);
254
0
    if (copied == NULL) {
255
0
        DSOerr(DSO_F_DSO_SET_FILENAME, ERR_R_MALLOC_FAILURE);
256
0
        return 0;
257
0
    }
258
0
    OPENSSL_free(dso->filename);
259
0
    dso->filename = copied;
260
0
    return 1;
261
0
}
262
263
char *DSO_merge(DSO *dso, const char *filespec1, const char *filespec2)
264
0
{
265
0
    char *result = NULL;
266
0
267
0
    if (dso == NULL || filespec1 == NULL) {
268
0
        DSOerr(DSO_F_DSO_MERGE, ERR_R_PASSED_NULL_PARAMETER);
269
0
        return NULL;
270
0
    }
271
0
    if ((dso->flags & DSO_FLAG_NO_NAME_TRANSLATION) == 0) {
272
0
        if (dso->merger != NULL)
273
0
            result = dso->merger(dso, filespec1, filespec2);
274
0
        else if (dso->meth->dso_merger != NULL)
275
0
            result = dso->meth->dso_merger(dso, filespec1, filespec2);
276
0
    }
277
0
    return result;
278
0
}
279
280
char *DSO_convert_filename(DSO *dso, const char *filename)
281
0
{
282
0
    char *result = NULL;
283
0
284
0
    if (dso == NULL) {
285
0
        DSOerr(DSO_F_DSO_CONVERT_FILENAME, ERR_R_PASSED_NULL_PARAMETER);
286
0
        return NULL;
287
0
    }
288
0
    if (filename == NULL)
289
0
        filename = dso->filename;
290
0
    if (filename == NULL) {
291
0
        DSOerr(DSO_F_DSO_CONVERT_FILENAME, DSO_R_NO_FILENAME);
292
0
        return NULL;
293
0
    }
294
0
    if ((dso->flags & DSO_FLAG_NO_NAME_TRANSLATION) == 0) {
295
0
        if (dso->name_converter != NULL)
296
0
            result = dso->name_converter(dso, filename);
297
0
        else if (dso->meth->dso_name_converter != NULL)
298
0
            result = dso->meth->dso_name_converter(dso, filename);
299
0
    }
300
0
    if (result == NULL) {
301
0
        result = OPENSSL_strdup(filename);
302
0
        if (result == NULL) {
303
0
            DSOerr(DSO_F_DSO_CONVERT_FILENAME, ERR_R_MALLOC_FAILURE);
304
0
            return NULL;
305
0
        }
306
0
    }
307
0
    return result;
308
0
}
309
310
int DSO_pathbyaddr(void *addr, char *path, int sz)
311
0
{
312
0
    DSO_METHOD *meth = default_DSO_meth;
313
0
    if (meth == NULL)
314
0
        meth = DSO_METHOD_openssl();
315
0
    if (meth->pathbyaddr == NULL) {
316
0
        DSOerr(DSO_F_DSO_PATHBYADDR, DSO_R_UNSUPPORTED);
317
0
        return -1;
318
0
    }
319
0
    return (*meth->pathbyaddr) (addr, path, sz);
320
0
}
321
322
DSO *DSO_dsobyaddr(void *addr, int flags)
323
0
{
324
0
    DSO *ret = NULL;
325
0
    char *filename = NULL;
326
0
    int len = DSO_pathbyaddr(addr, NULL, 0);
327
0
328
0
    if (len < 0)
329
0
        return NULL;
330
0
331
0
    filename = OPENSSL_malloc(len);
332
0
    if (filename != NULL
333
0
            && DSO_pathbyaddr(addr, filename, len) == len)
334
0
        ret = DSO_load(NULL, filename, NULL, flags);
335
0
336
0
    OPENSSL_free(filename);
337
0
    return ret;
338
0
}
339
340
void *DSO_global_lookup(const char *name)
341
0
{
342
0
    DSO_METHOD *meth = default_DSO_meth;
343
0
    if (meth == NULL)
344
0
        meth = DSO_METHOD_openssl();
345
0
    if (meth->globallookup == NULL) {
346
0
        DSOerr(DSO_F_DSO_GLOBAL_LOOKUP, DSO_R_UNSUPPORTED);
347
0
        return NULL;
348
0
    }
349
0
    return (*meth->globallookup) (name);
350
0
}