Coverage Report

Created: 2018-08-29 13:53

/src/openssl/crypto/engine/eng_dyn.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2001-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 "eng_int.h"
11
#include "internal/dso.h"
12
#include <openssl/crypto.h>
13
14
/*
15
 * Shared libraries implementing ENGINEs for use by the "dynamic" ENGINE
16
 * loader should implement the hook-up functions with the following
17
 * prototypes.
18
 */
19
20
/* Our ENGINE handlers */
21
static int dynamic_init(ENGINE *e);
22
static int dynamic_finish(ENGINE *e);
23
static int dynamic_ctrl(ENGINE *e, int cmd, long i, void *p,
24
                        void (*f) (void));
25
/* Predeclare our context type */
26
typedef struct st_dynamic_data_ctx dynamic_data_ctx;
27
/* The implementation for the important control command */
28
static int dynamic_load(ENGINE *e, dynamic_data_ctx *ctx);
29
30
0
#define DYNAMIC_CMD_SO_PATH             ENGINE_CMD_BASE
31
0
#define DYNAMIC_CMD_NO_VCHECK           (ENGINE_CMD_BASE + 1)
32
0
#define DYNAMIC_CMD_ID                  (ENGINE_CMD_BASE + 2)
33
0
#define DYNAMIC_CMD_LIST_ADD            (ENGINE_CMD_BASE + 3)
34
0
#define DYNAMIC_CMD_DIR_LOAD            (ENGINE_CMD_BASE + 4)
35
0
#define DYNAMIC_CMD_DIR_ADD             (ENGINE_CMD_BASE + 5)
36
0
#define DYNAMIC_CMD_LOAD                (ENGINE_CMD_BASE + 6)
37
38
/* The constants used when creating the ENGINE */
39
static const char *engine_dynamic_id = "dynamic";
40
static const char *engine_dynamic_name = "Dynamic engine loading support";
41
static const ENGINE_CMD_DEFN dynamic_cmd_defns[] = {
42
    {DYNAMIC_CMD_SO_PATH,
43
     "SO_PATH",
44
     "Specifies the path to the new ENGINE shared library",
45
     ENGINE_CMD_FLAG_STRING},
46
    {DYNAMIC_CMD_NO_VCHECK,
47
     "NO_VCHECK",
48
     "Specifies to continue even if version checking fails (boolean)",
49
     ENGINE_CMD_FLAG_NUMERIC},
50
    {DYNAMIC_CMD_ID,
51
     "ID",
52
     "Specifies an ENGINE id name for loading",
53
     ENGINE_CMD_FLAG_STRING},
54
    {DYNAMIC_CMD_LIST_ADD,
55
     "LIST_ADD",
56
     "Whether to add a loaded ENGINE to the internal list (0=no,1=yes,2=mandatory)",
57
     ENGINE_CMD_FLAG_NUMERIC},
58
    {DYNAMIC_CMD_DIR_LOAD,
59
     "DIR_LOAD",
60
     "Specifies whether to load from 'DIR_ADD' directories (0=no,1=yes,2=mandatory)",
61
     ENGINE_CMD_FLAG_NUMERIC},
62
    {DYNAMIC_CMD_DIR_ADD,
63
     "DIR_ADD",
64
     "Adds a directory from which ENGINEs can be loaded",
65
     ENGINE_CMD_FLAG_STRING},
66
    {DYNAMIC_CMD_LOAD,
67
     "LOAD",
68
     "Load up the ENGINE specified by other settings",
69
     ENGINE_CMD_FLAG_NO_INPUT},
70
    {0, NULL, NULL, 0}
71
};
72
73
/*
74
 * Loading code stores state inside the ENGINE structure via the "ex_data"
75
 * element. We load all our state into a single structure and use that as a
76
 * single context in the "ex_data" stack.
77
 */
78
struct st_dynamic_data_ctx {
79
    /* The DSO object we load that supplies the ENGINE code */
80
    DSO *dynamic_dso;
81
    /*
82
     * The function pointer to the version checking shared library function
83
     */
84
    dynamic_v_check_fn v_check;
85
    /*
86
     * The function pointer to the engine-binding shared library function
87
     */
88
    dynamic_bind_engine bind_engine;
89
    /* The default name/path for loading the shared library */
90
    char *DYNAMIC_LIBNAME;
91
    /* Whether to continue loading on a version check failure */
92
    int no_vcheck;
93
    /* If non-NULL, stipulates the 'id' of the ENGINE to be loaded */
94
    char *engine_id;
95
    /*
96
     * If non-zero, a successfully loaded ENGINE should be added to the
97
     * internal ENGINE list. If 2, the add must succeed or the entire load
98
     * should fail.
99
     */
100
    int list_add_value;
101
    /* The symbol name for the version checking function */
102
    const char *DYNAMIC_F1;
103
    /* The symbol name for the "initialise ENGINE structure" function */
104
    const char *DYNAMIC_F2;
105
    /*
106
     * Whether to never use 'dirs', use 'dirs' as a fallback, or only use
107
     * 'dirs' for loading. Default is to use 'dirs' as a fallback.
108
     */
109
    int dir_load;
110
    /* A stack of directories from which ENGINEs could be loaded */
111
    STACK_OF(OPENSSL_STRING) *dirs;
112
};
113
114
/*
115
 * This is the "ex_data" index we obtain and reserve for use with our context
116
 * structure.
117
 */
118
static int dynamic_ex_data_idx = -1;
119
120
static void int_free_str(char *s)
121
0
{
122
0
    OPENSSL_free(s);
123
0
}
124
125
/*
126
 * Because our ex_data element may or may not get allocated depending on
127
 * whether a "first-use" occurs before the ENGINE is freed, we have a memory
128
 * leak problem to solve. We can't declare a "new" handler for the ex_data as
129
 * we don't want a dynamic_data_ctx in *all* ENGINE structures of all types
130
 * (this is a bug in the design of CRYPTO_EX_DATA). As such, we just declare
131
 * a "free" handler and that will get called if an ENGINE is being destroyed
132
 * and there was an ex_data element corresponding to our context type.
133
 */
134
static void dynamic_data_ctx_free_func(void *parent, void *ptr,
135
                                       CRYPTO_EX_DATA *ad, int idx, long argl,
136
                                       void *argp)
137
0
{
138
0
    if (ptr) {
139
0
        dynamic_data_ctx *ctx = (dynamic_data_ctx *)ptr;
140
0
        DSO_free(ctx->dynamic_dso);
141
0
        OPENSSL_free(ctx->DYNAMIC_LIBNAME);
142
0
        OPENSSL_free(ctx->engine_id);
143
0
        sk_OPENSSL_STRING_pop_free(ctx->dirs, int_free_str);
144
0
        OPENSSL_free(ctx);
145
0
    }
146
0
}
147
148
/*
149
 * Construct the per-ENGINE context. We create it blindly and then use a lock
150
 * to check for a race - if so, all but one of the threads "racing" will have
151
 * wasted their time. The alternative involves creating everything inside the
152
 * lock which is far worse.
153
 */
154
static int dynamic_set_data_ctx(ENGINE *e, dynamic_data_ctx **ctx)
155
0
{
156
0
    dynamic_data_ctx *c = OPENSSL_zalloc(sizeof(*c));
157
0
    int ret = 1;
158
0
159
0
    if (c == NULL) {
160
0
        ENGINEerr(ENGINE_F_DYNAMIC_SET_DATA_CTX, ERR_R_MALLOC_FAILURE);
161
0
        return 0;
162
0
    }
163
0
    c->dirs = sk_OPENSSL_STRING_new_null();
164
0
    if (c->dirs == NULL) {
165
0
        ENGINEerr(ENGINE_F_DYNAMIC_SET_DATA_CTX, ERR_R_MALLOC_FAILURE);
166
0
        OPENSSL_free(c);
167
0
        return 0;
168
0
    }
169
0
    c->DYNAMIC_F1 = "v_check";
170
0
    c->DYNAMIC_F2 = "bind_engine";
171
0
    c->dir_load = 1;
172
0
    CRYPTO_THREAD_write_lock(global_engine_lock);
173
0
    if ((*ctx = (dynamic_data_ctx *)ENGINE_get_ex_data(e,
174
0
                                                       dynamic_ex_data_idx))
175
0
        == NULL) {
176
0
        /* Good, we're the first */
177
0
        ret = ENGINE_set_ex_data(e, dynamic_ex_data_idx, c);
178
0
        if (ret) {
179
0
            *ctx = c;
180
0
            c = NULL;
181
0
        }
182
0
    }
183
0
    CRYPTO_THREAD_unlock(global_engine_lock);
184
0
    /*
185
0
     * If we lost the race to set the context, c is non-NULL and *ctx is the
186
0
     * context of the thread that won.
187
0
     */
188
0
    if (c)
189
0
        sk_OPENSSL_STRING_free(c->dirs);
190
0
    OPENSSL_free(c);
191
0
    return ret;
192
0
}
193
194
/*
195
 * This function retrieves the context structure from an ENGINE's "ex_data",
196
 * or if it doesn't exist yet, sets it up.
197
 */
198
static dynamic_data_ctx *dynamic_get_data_ctx(ENGINE *e)
199
0
{
200
0
    dynamic_data_ctx *ctx;
201
0
    if (dynamic_ex_data_idx < 0) {
202
0
        /*
203
0
         * Create and register the ENGINE ex_data, and associate our "free"
204
0
         * function with it to ensure any allocated contexts get freed when
205
0
         * an ENGINE goes underground.
206
0
         */
207
0
        int new_idx = ENGINE_get_ex_new_index(0, NULL, NULL, NULL,
208
0
                                              dynamic_data_ctx_free_func);
209
0
        if (new_idx == -1) {
210
0
            ENGINEerr(ENGINE_F_DYNAMIC_GET_DATA_CTX, ENGINE_R_NO_INDEX);
211
0
            return NULL;
212
0
        }
213
0
        CRYPTO_THREAD_write_lock(global_engine_lock);
214
0
        /* Avoid a race by checking again inside this lock */
215
0
        if (dynamic_ex_data_idx < 0) {
216
0
            /* Good, someone didn't beat us to it */
217
0
            dynamic_ex_data_idx = new_idx;
218
0
            new_idx = -1;
219
0
        }
220
0
        CRYPTO_THREAD_unlock(global_engine_lock);
221
0
        /*
222
0
         * In theory we could "give back" the index here if (new_idx>-1), but
223
0
         * it's not possible and wouldn't gain us much if it were.
224
0
         */
225
0
    }
226
0
    ctx = (dynamic_data_ctx *)ENGINE_get_ex_data(e, dynamic_ex_data_idx);
227
0
    /* Check if the context needs to be created */
228
0
    if ((ctx == NULL) && !dynamic_set_data_ctx(e, &ctx))
229
0
        /* "set_data" will set errors if necessary */
230
0
        return NULL;
231
0
    return ctx;
232
0
}
233
234
static ENGINE *engine_dynamic(void)
235
0
{
236
0
    ENGINE *ret = ENGINE_new();
237
0
    if (ret == NULL)
238
0
        return NULL;
239
0
    if (!ENGINE_set_id(ret, engine_dynamic_id) ||
240
0
        !ENGINE_set_name(ret, engine_dynamic_name) ||
241
0
        !ENGINE_set_init_function(ret, dynamic_init) ||
242
0
        !ENGINE_set_finish_function(ret, dynamic_finish) ||
243
0
        !ENGINE_set_ctrl_function(ret, dynamic_ctrl) ||
244
0
        !ENGINE_set_flags(ret, ENGINE_FLAGS_BY_ID_COPY) ||
245
0
        !ENGINE_set_cmd_defns(ret, dynamic_cmd_defns)) {
246
0
        ENGINE_free(ret);
247
0
        return NULL;
248
0
    }
249
0
    return ret;
250
0
}
251
252
void engine_load_dynamic_int(void)
253
0
{
254
0
    ENGINE *toadd = engine_dynamic();
255
0
    if (!toadd)
256
0
        return;
257
0
    ENGINE_add(toadd);
258
0
    /*
259
0
     * If the "add" worked, it gets a structural reference. So either way, we
260
0
     * release our just-created reference.
261
0
     */
262
0
    ENGINE_free(toadd);
263
0
    /*
264
0
     * If the "add" didn't work, it was probably a conflict because it was
265
0
     * already added (eg. someone calling ENGINE_load_blah then calling
266
0
     * ENGINE_load_builtin_engines() perhaps).
267
0
     */
268
0
    ERR_clear_error();
269
0
}
270
271
static int dynamic_init(ENGINE *e)
272
0
{
273
0
    /*
274
0
     * We always return failure - the "dynamic" engine itself can't be used
275
0
     * for anything.
276
0
     */
277
0
    return 0;
278
0
}
279
280
static int dynamic_finish(ENGINE *e)
281
0
{
282
0
    /*
283
0
     * This should never be called on account of "dynamic_init" always
284
0
     * failing.
285
0
     */
286
0
    return 0;
287
0
}
288
289
static int dynamic_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
290
0
{
291
0
    dynamic_data_ctx *ctx = dynamic_get_data_ctx(e);
292
0
    int initialised;
293
0
294
0
    if (!ctx) {
295
0
        ENGINEerr(ENGINE_F_DYNAMIC_CTRL, ENGINE_R_NOT_LOADED);
296
0
        return 0;
297
0
    }
298
0
    initialised = ((ctx->dynamic_dso == NULL) ? 0 : 1);
299
0
    /* All our control commands require the ENGINE to be uninitialised */
300
0
    if (initialised) {
301
0
        ENGINEerr(ENGINE_F_DYNAMIC_CTRL, ENGINE_R_ALREADY_LOADED);
302
0
        return 0;
303
0
    }
304
0
    switch (cmd) {
305
0
    case DYNAMIC_CMD_SO_PATH:
306
0
        /* a NULL 'p' or a string of zero-length is the same thing */
307
0
        if (p && (strlen((const char *)p) < 1))
308
0
            p = NULL;
309
0
        OPENSSL_free(ctx->DYNAMIC_LIBNAME);
310
0
        if (p)
311
0
            ctx->DYNAMIC_LIBNAME = OPENSSL_strdup(p);
312
0
        else
313
0
            ctx->DYNAMIC_LIBNAME = NULL;
314
0
        return (ctx->DYNAMIC_LIBNAME ? 1 : 0);
315
0
    case DYNAMIC_CMD_NO_VCHECK:
316
0
        ctx->no_vcheck = ((i == 0) ? 0 : 1);
317
0
        return 1;
318
0
    case DYNAMIC_CMD_ID:
319
0
        /* a NULL 'p' or a string of zero-length is the same thing */
320
0
        if (p && (strlen((const char *)p) < 1))
321
0
            p = NULL;
322
0
        OPENSSL_free(ctx->engine_id);
323
0
        if (p)
324
0
            ctx->engine_id = OPENSSL_strdup(p);
325
0
        else
326
0
            ctx->engine_id = NULL;
327
0
        return (ctx->engine_id ? 1 : 0);
328
0
    case DYNAMIC_CMD_LIST_ADD:
329
0
        if ((i < 0) || (i > 2)) {
330
0
            ENGINEerr(ENGINE_F_DYNAMIC_CTRL, ENGINE_R_INVALID_ARGUMENT);
331
0
            return 0;
332
0
        }
333
0
        ctx->list_add_value = (int)i;
334
0
        return 1;
335
0
    case DYNAMIC_CMD_LOAD:
336
0
        return dynamic_load(e, ctx);
337
0
    case DYNAMIC_CMD_DIR_LOAD:
338
0
        if ((i < 0) || (i > 2)) {
339
0
            ENGINEerr(ENGINE_F_DYNAMIC_CTRL, ENGINE_R_INVALID_ARGUMENT);
340
0
            return 0;
341
0
        }
342
0
        ctx->dir_load = (int)i;
343
0
        return 1;
344
0
    case DYNAMIC_CMD_DIR_ADD:
345
0
        /* a NULL 'p' or a string of zero-length is the same thing */
346
0
        if (!p || (strlen((const char *)p) < 1)) {
347
0
            ENGINEerr(ENGINE_F_DYNAMIC_CTRL, ENGINE_R_INVALID_ARGUMENT);
348
0
            return 0;
349
0
        }
350
0
        {
351
0
            char *tmp_str = OPENSSL_strdup(p);
352
0
            if (tmp_str == NULL) {
353
0
                ENGINEerr(ENGINE_F_DYNAMIC_CTRL, ERR_R_MALLOC_FAILURE);
354
0
                return 0;
355
0
            }
356
0
            if (!sk_OPENSSL_STRING_push(ctx->dirs, tmp_str)) {
357
0
                OPENSSL_free(tmp_str);
358
0
                ENGINEerr(ENGINE_F_DYNAMIC_CTRL, ERR_R_MALLOC_FAILURE);
359
0
                return 0;
360
0
            }
361
0
        }
362
0
        return 1;
363
0
    default:
364
0
        break;
365
0
    }
366
0
    ENGINEerr(ENGINE_F_DYNAMIC_CTRL, ENGINE_R_CTRL_COMMAND_NOT_IMPLEMENTED);
367
0
    return 0;
368
0
}
369
370
static int int_load(dynamic_data_ctx *ctx)
371
0
{
372
0
    int num, loop;
373
0
    /* Unless told not to, try a direct load */
374
0
    if ((ctx->dir_load != 2) && (DSO_load(ctx->dynamic_dso,
375
0
                                          ctx->DYNAMIC_LIBNAME, NULL,
376
0
                                          0)) != NULL)
377
0
        return 1;
378
0
    /* If we're not allowed to use 'dirs' or we have none, fail */
379
0
    if (!ctx->dir_load || (num = sk_OPENSSL_STRING_num(ctx->dirs)) < 1)
380
0
        return 0;
381
0
    for (loop = 0; loop < num; loop++) {
382
0
        const char *s = sk_OPENSSL_STRING_value(ctx->dirs, loop);
383
0
        char *merge = DSO_merge(ctx->dynamic_dso, ctx->DYNAMIC_LIBNAME, s);
384
0
        if (!merge)
385
0
            return 0;
386
0
        if (DSO_load(ctx->dynamic_dso, merge, NULL, 0)) {
387
0
            /* Found what we're looking for */
388
0
            OPENSSL_free(merge);
389
0
            return 1;
390
0
        }
391
0
        OPENSSL_free(merge);
392
0
    }
393
0
    return 0;
394
0
}
395
396
static int dynamic_load(ENGINE *e, dynamic_data_ctx *ctx)
397
0
{
398
0
    ENGINE cpy;
399
0
    dynamic_fns fns;
400
0
401
0
    if (ctx->dynamic_dso == NULL)
402
0
        ctx->dynamic_dso = DSO_new();
403
0
    if (ctx->dynamic_dso == NULL)
404
0
        return 0;
405
0
    if (!ctx->DYNAMIC_LIBNAME) {
406
0
        if (!ctx->engine_id)
407
0
            return 0;
408
0
        DSO_ctrl(ctx->dynamic_dso, DSO_CTRL_SET_FLAGS,
409
0
                 DSO_FLAG_NAME_TRANSLATION_EXT_ONLY, NULL);
410
0
        ctx->DYNAMIC_LIBNAME =
411
0
            DSO_convert_filename(ctx->dynamic_dso, ctx->engine_id);
412
0
    }
413
0
    if (!int_load(ctx)) {
414
0
        ENGINEerr(ENGINE_F_DYNAMIC_LOAD, ENGINE_R_DSO_NOT_FOUND);
415
0
        DSO_free(ctx->dynamic_dso);
416
0
        ctx->dynamic_dso = NULL;
417
0
        return 0;
418
0
    }
419
0
    /* We have to find a bind function otherwise it'll always end badly */
420
0
    if (!
421
0
        (ctx->bind_engine =
422
0
         (dynamic_bind_engine) DSO_bind_func(ctx->dynamic_dso,
423
0
                                             ctx->DYNAMIC_F2))) {
424
0
        ctx->bind_engine = NULL;
425
0
        DSO_free(ctx->dynamic_dso);
426
0
        ctx->dynamic_dso = NULL;
427
0
        ENGINEerr(ENGINE_F_DYNAMIC_LOAD, ENGINE_R_DSO_FAILURE);
428
0
        return 0;
429
0
    }
430
0
    /* Do we perform version checking? */
431
0
    if (!ctx->no_vcheck) {
432
0
        unsigned long vcheck_res = 0;
433
0
        /*
434
0
         * Now we try to find a version checking function and decide how to
435
0
         * cope with failure if/when it fails.
436
0
         */
437
0
        ctx->v_check =
438
0
            (dynamic_v_check_fn) DSO_bind_func(ctx->dynamic_dso,
439
0
                                               ctx->DYNAMIC_F1);
440
0
        if (ctx->v_check)
441
0
            vcheck_res = ctx->v_check(OSSL_DYNAMIC_VERSION);
442
0
        /*
443
0
         * We fail if the version checker veto'd the load *or* if it is
444
0
         * deferring to us (by returning its version) and we think it is too
445
0
         * old.
446
0
         */
447
0
        if (vcheck_res < OSSL_DYNAMIC_OLDEST) {
448
0
            /* Fail */
449
0
            ctx->bind_engine = NULL;
450
0
            ctx->v_check = NULL;
451
0
            DSO_free(ctx->dynamic_dso);
452
0
            ctx->dynamic_dso = NULL;
453
0
            ENGINEerr(ENGINE_F_DYNAMIC_LOAD,
454
0
                      ENGINE_R_VERSION_INCOMPATIBILITY);
455
0
            return 0;
456
0
        }
457
0
    }
458
0
    /*
459
0
     * First binary copy the ENGINE structure so that we can roll back if the
460
0
     * hand-over fails
461
0
     */
462
0
    memcpy(&cpy, e, sizeof(ENGINE));
463
0
    /*
464
0
     * Provide the ERR, "ex_data", memory, and locking callbacks so the
465
0
     * loaded library uses our state rather than its own. FIXME: As noted in
466
0
     * engine.h, much of this would be simplified if each area of code
467
0
     * provided its own "summary" structure of all related callbacks. It
468
0
     * would also increase opaqueness.
469
0
     */
470
0
    fns.static_state = ENGINE_get_static_state();
471
0
    CRYPTO_get_mem_functions(&fns.mem_fns.malloc_fn, &fns.mem_fns.realloc_fn,
472
0
                             &fns.mem_fns.free_fn);
473
0
    /*
474
0
     * Now that we've loaded the dynamic engine, make sure no "dynamic"
475
0
     * ENGINE elements will show through.
476
0
     */
477
0
    engine_set_all_null(e);
478
0
479
0
    /* Try to bind the ENGINE onto our own ENGINE structure */
480
0
    if (!ctx->bind_engine(e, ctx->engine_id, &fns)) {
481
0
        ctx->bind_engine = NULL;
482
0
        ctx->v_check = NULL;
483
0
        DSO_free(ctx->dynamic_dso);
484
0
        ctx->dynamic_dso = NULL;
485
0
        ENGINEerr(ENGINE_F_DYNAMIC_LOAD, ENGINE_R_INIT_FAILED);
486
0
        /* Copy the original ENGINE structure back */
487
0
        memcpy(e, &cpy, sizeof(ENGINE));
488
0
        return 0;
489
0
    }
490
0
    /* Do we try to add this ENGINE to the internal list too? */
491
0
    if (ctx->list_add_value > 0) {
492
0
        if (!ENGINE_add(e)) {
493
0
            /* Do we tolerate this or fail? */
494
0
            if (ctx->list_add_value > 1) {
495
0
                /*
496
0
                 * Fail - NB: By this time, it's too late to rollback, and
497
0
                 * trying to do so allows the bind_engine() code to have
498
0
                 * created leaks. We just have to fail where we are, after
499
0
                 * the ENGINE has changed.
500
0
                 */
501
0
                ENGINEerr(ENGINE_F_DYNAMIC_LOAD,
502
0
                          ENGINE_R_CONFLICTING_ENGINE_ID);
503
0
                return 0;
504
0
            }
505
0
            /* Tolerate */
506
0
            ERR_clear_error();
507
0
        }
508
0
    }
509
0
    return 1;
510
0
}