Coverage Report

Created: 2018-08-29 13:53

/src/openssl/crypto/ex_data.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2018 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 "internal/cryptlib_int.h"
11
#include "internal/thread_once.h"
12
13
/*
14
 * Each structure type (sometimes called a class), that supports
15
 * exdata has a stack of callbacks for each instance.
16
 */
17
struct ex_callback_st {
18
    long argl;                  /* Arbitrary long */
19
    void *argp;                 /* Arbitrary void * */
20
    CRYPTO_EX_new *new_func;
21
    CRYPTO_EX_free *free_func;
22
    CRYPTO_EX_dup *dup_func;
23
};
24
25
/*
26
 * The state for each class.  This could just be a typedef, but
27
 * a structure allows future changes.
28
 */
29
typedef struct ex_callbacks_st {
30
    STACK_OF(EX_CALLBACK) *meth;
31
} EX_CALLBACKS;
32
33
static EX_CALLBACKS ex_data[CRYPTO_EX_INDEX__COUNT];
34
35
static CRYPTO_RWLOCK *ex_data_lock = NULL;
36
static CRYPTO_ONCE ex_data_init = CRYPTO_ONCE_STATIC_INIT;
37
38
DEFINE_RUN_ONCE_STATIC(do_ex_data_init)
39
8
{
40
8
    if (!OPENSSL_init_crypto(0, NULL))
41
0
        return 0;
42
8
    ex_data_lock = CRYPTO_THREAD_lock_new();
43
8
    return ex_data_lock != NULL;
44
8
}
45
46
/*
47
 * Return the EX_CALLBACKS from the |ex_data| array that corresponds to
48
 * a given class.  On success, *holds the lock.*
49
 */
50
static EX_CALLBACKS *get_and_lock(int class_index)
51
2.80M
{
52
2.80M
    EX_CALLBACKS *ip;
53
2.80M
54
2.80M
    if (class_index < 0 || class_index >= CRYPTO_EX_INDEX__COUNT) {
55
0
        CRYPTOerr(CRYPTO_F_GET_AND_LOCK, ERR_R_PASSED_INVALID_ARGUMENT);
56
0
        return NULL;
57
0
    }
58
2.80M
59
2.80M
    if (!RUN_ONCE(&ex_data_init, do_ex_data_init)) {
60
0
        CRYPTOerr(CRYPTO_F_GET_AND_LOCK, ERR_R_MALLOC_FAILURE);
61
0
        return NULL;
62
0
    }
63
2.80M
64
2.80M
    if (ex_data_lock == NULL) {
65
0
        /*
66
0
         * This can happen in normal operation when using CRYPTO_mem_leaks().
67
0
         * The CRYPTO_mem_leaks() function calls OPENSSL_cleanup() which cleans
68
0
         * up the locks. Subsequently the BIO that CRYPTO_mem_leaks() uses gets
69
0
         * freed, which also attempts to free the ex_data. However
70
0
         * CRYPTO_mem_leaks() ensures that the ex_data is freed early (i.e.
71
0
         * before OPENSSL_cleanup() is called), so if we get here we can safely
72
0
         * ignore this operation. We just treat it as an error.
73
0
         */
74
0
         return NULL;
75
0
    }
76
2.80M
77
2.80M
    ip = &ex_data[class_index];
78
2.80M
    CRYPTO_THREAD_write_lock(ex_data_lock);
79
2.80M
    return ip;
80
2.80M
}
81
82
static void cleanup_cb(EX_CALLBACK *funcs)
83
0
{
84
0
    OPENSSL_free(funcs);
85
0
}
86
87
/*
88
 * Release all "ex_data" state to prevent memory leaks. This can't be made
89
 * thread-safe without overhauling a lot of stuff, and shouldn't really be
90
 * called under potential race-conditions anyway (it's for program shutdown
91
 * after all).
92
 */
93
void crypto_cleanup_all_ex_data_int(void)
94
8
{
95
8
    int i;
96
8
97
136
    for (i = 0; i < CRYPTO_EX_INDEX__COUNT; ++i) {
98
128
        EX_CALLBACKS *ip = &ex_data[i];
99
128
100
128
        sk_EX_CALLBACK_pop_free(ip->meth, cleanup_cb);
101
128
        ip->meth = NULL;
102
128
    }
103
8
104
8
    CRYPTO_THREAD_lock_free(ex_data_lock);
105
8
    ex_data_lock = NULL;
106
8
}
107
108
109
/*
110
 * Unregister a new index by replacing the callbacks with no-ops.
111
 * Any in-use instances are leaked.
112
 */
113
static void dummy_new(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx,
114
                     long argl, void *argp)
115
0
{
116
0
}
117
118
static void dummy_free(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx,
119
                       long argl, void *argp)
120
0
{
121
0
}
122
123
static int dummy_dup(CRYPTO_EX_DATA *to, const CRYPTO_EX_DATA *from,
124
                     void *from_d, int idx,
125
                     long argl, void *argp)
126
0
{
127
0
    return 1;
128
0
}
129
130
int CRYPTO_free_ex_index(int class_index, int idx)
131
0
{
132
0
    EX_CALLBACKS *ip = get_and_lock(class_index);
133
0
    EX_CALLBACK *a;
134
0
    int toret = 0;
135
0
136
0
    if (ip == NULL)
137
0
        return 0;
138
0
    if (idx < 0 || idx >= sk_EX_CALLBACK_num(ip->meth))
139
0
        goto err;
140
0
    a = sk_EX_CALLBACK_value(ip->meth, idx);
141
0
    if (a == NULL)
142
0
        goto err;
143
0
    a->new_func = dummy_new;
144
0
    a->dup_func = dummy_dup;
145
0
    a->free_func = dummy_free;
146
0
    toret = 1;
147
0
err:
148
0
    CRYPTO_THREAD_unlock(ex_data_lock);
149
0
    return toret;
150
0
}
151
152
/*
153
 * Register a new index.
154
 */
155
int CRYPTO_get_ex_new_index(int class_index, long argl, void *argp,
156
                            CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
157
                            CRYPTO_EX_free *free_func)
158
0
{
159
0
    int toret = -1;
160
0
    EX_CALLBACK *a;
161
0
    EX_CALLBACKS *ip = get_and_lock(class_index);
162
0
163
0
    if (ip == NULL)
164
0
        return -1;
165
0
166
0
    if (ip->meth == NULL) {
167
0
        ip->meth = sk_EX_CALLBACK_new_null();
168
0
        /* We push an initial value on the stack because the SSL
169
0
         * "app_data" routines use ex_data index zero.  See RT 3710. */
170
0
        if (ip->meth == NULL
171
0
            || !sk_EX_CALLBACK_push(ip->meth, NULL)) {
172
0
            CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX, ERR_R_MALLOC_FAILURE);
173
0
            goto err;
174
0
        }
175
0
    }
176
0
177
0
    a = (EX_CALLBACK *)OPENSSL_malloc(sizeof(*a));
178
0
    if (a == NULL) {
179
0
        CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX, ERR_R_MALLOC_FAILURE);
180
0
        goto err;
181
0
    }
182
0
    a->argl = argl;
183
0
    a->argp = argp;
184
0
    a->new_func = new_func;
185
0
    a->dup_func = dup_func;
186
0
    a->free_func = free_func;
187
0
188
0
    if (!sk_EX_CALLBACK_push(ip->meth, NULL)) {
189
0
        CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX, ERR_R_MALLOC_FAILURE);
190
0
        OPENSSL_free(a);
191
0
        goto err;
192
0
    }
193
0
    toret = sk_EX_CALLBACK_num(ip->meth) - 1;
194
0
    (void)sk_EX_CALLBACK_set(ip->meth, toret, a);
195
0
196
0
 err:
197
0
    CRYPTO_THREAD_unlock(ex_data_lock);
198
0
    return toret;
199
0
}
200
201
/*
202
 * Initialise a new CRYPTO_EX_DATA for use in a particular class - including
203
 * calling new() callbacks for each index in the class used by this variable
204
 * Thread-safe by copying a class's array of "EX_CALLBACK" entries
205
 * in the lock, then using them outside the lock. Note this only applies
206
 * to the global "ex_data" state (ie. class definitions), not 'ad' itself.
207
 */
208
int CRYPTO_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
209
1.40M
{
210
1.40M
    int mx, i;
211
1.40M
    void *ptr;
212
1.40M
    EX_CALLBACK **storage = NULL;
213
1.40M
    EX_CALLBACK *stack[10];
214
1.40M
    EX_CALLBACKS *ip = get_and_lock(class_index);
215
1.40M
216
1.40M
    if (ip == NULL)
217
1.40M
        return 0;
218
1.40M
219
1.40M
    ad->sk = NULL;
220
1.40M
221
1.40M
    mx = sk_EX_CALLBACK_num(ip->meth);
222
1.40M
    if (mx > 0) {
223
0
        if (mx < (int)OSSL_NELEM(stack))
224
0
            storage = stack;
225
0
        else
226
0
            storage = OPENSSL_malloc(sizeof(*storage) * mx);
227
0
        if (storage != NULL)
228
0
            for (i = 0; i < mx; i++)
229
0
                storage[i] = sk_EX_CALLBACK_value(ip->meth, i);
230
0
    }
231
1.40M
    CRYPTO_THREAD_unlock(ex_data_lock);
232
1.40M
233
1.40M
    if (mx > 0 && storage == NULL) {
234
0
        CRYPTOerr(CRYPTO_F_CRYPTO_NEW_EX_DATA, ERR_R_MALLOC_FAILURE);
235
0
        return 0;
236
0
    }
237
1.40M
    for (i = 0; i < mx; i++) {
238
0
        if (storage[i] && storage[i]->new_func) {
239
0
            ptr = CRYPTO_get_ex_data(ad, i);
240
0
            storage[i]->new_func(obj, ptr, ad, i,
241
0
                                 storage[i]->argl, storage[i]->argp);
242
0
        }
243
0
    }
244
1.40M
    if (storage != stack)
245
1.40M
        OPENSSL_free(storage);
246
1.40M
    return 1;
247
1.40M
}
248
249
/*
250
 * Duplicate a CRYPTO_EX_DATA variable - including calling dup() callbacks
251
 * for each index in the class used by this variable
252
 */
253
int CRYPTO_dup_ex_data(int class_index, CRYPTO_EX_DATA *to,
254
                       const CRYPTO_EX_DATA *from)
255
0
{
256
0
    int mx, j, i;
257
0
    void *ptr;
258
0
    EX_CALLBACK *stack[10];
259
0
    EX_CALLBACK **storage = NULL;
260
0
    EX_CALLBACKS *ip;
261
0
    int toret = 0;
262
0
263
0
    if (from->sk == NULL)
264
0
        /* Nothing to copy over */
265
0
        return 1;
266
0
    if ((ip = get_and_lock(class_index)) == NULL)
267
0
        return 0;
268
0
269
0
    mx = sk_EX_CALLBACK_num(ip->meth);
270
0
    j = sk_void_num(from->sk);
271
0
    if (j < mx)
272
0
        mx = j;
273
0
    if (mx > 0) {
274
0
        if (mx < (int)OSSL_NELEM(stack))
275
0
            storage = stack;
276
0
        else
277
0
            storage = OPENSSL_malloc(sizeof(*storage) * mx);
278
0
        if (storage != NULL)
279
0
            for (i = 0; i < mx; i++)
280
0
                storage[i] = sk_EX_CALLBACK_value(ip->meth, i);
281
0
    }
282
0
    CRYPTO_THREAD_unlock(ex_data_lock);
283
0
284
0
    if (mx == 0)
285
0
        return 1;
286
0
    if (storage == NULL) {
287
0
        CRYPTOerr(CRYPTO_F_CRYPTO_DUP_EX_DATA, ERR_R_MALLOC_FAILURE);
288
0
        return 0;
289
0
    }
290
0
    /*
291
0
     * Make sure the ex_data stack is at least |mx| elements long to avoid
292
0
     * issues in the for loop that follows; so go get the |mx|'th element
293
0
     * (if it does not exist CRYPTO_get_ex_data() returns NULL), and assign
294
0
     * to itself. This is normally a no-op; but ensures the stack is the
295
0
     * proper size
296
0
     */
297
0
    if (!CRYPTO_set_ex_data(to, mx - 1, CRYPTO_get_ex_data(to, mx - 1)))
298
0
        goto err;
299
0
300
0
    for (i = 0; i < mx; i++) {
301
0
        ptr = CRYPTO_get_ex_data(from, i);
302
0
        if (storage[i] && storage[i]->dup_func)
303
0
            if (!storage[i]->dup_func(to, from, &ptr, i,
304
0
                                      storage[i]->argl, storage[i]->argp))
305
0
                goto err;
306
0
        CRYPTO_set_ex_data(to, i, ptr);
307
0
    }
308
0
    toret = 1;
309
0
 err:
310
0
    if (storage != stack)
311
0
        OPENSSL_free(storage);
312
0
    return toret;
313
0
}
314
315
316
/*
317
 * Cleanup a CRYPTO_EX_DATA variable - including calling free() callbacks for
318
 * each index in the class used by this variable
319
 */
320
void CRYPTO_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
321
1.40M
{
322
1.40M
    int mx, i;
323
1.40M
    EX_CALLBACKS *ip;
324
1.40M
    void *ptr;
325
1.40M
    EX_CALLBACK *f;
326
1.40M
    EX_CALLBACK *stack[10];
327
1.40M
    EX_CALLBACK **storage = NULL;
328
1.40M
329
1.40M
    if ((ip = get_and_lock(class_index)) == NULL)
330
1.40M
        goto err;
331
1.40M
332
1.40M
    mx = sk_EX_CALLBACK_num(ip->meth);
333
1.40M
    if (mx > 0) {
334
0
        if (mx < (int)OSSL_NELEM(stack))
335
0
            storage = stack;
336
0
        else
337
0
            storage = OPENSSL_malloc(sizeof(*storage) * mx);
338
0
        if (storage != NULL)
339
0
            for (i = 0; i < mx; i++)
340
0
                storage[i] = sk_EX_CALLBACK_value(ip->meth, i);
341
0
    }
342
1.40M
    CRYPTO_THREAD_unlock(ex_data_lock);
343
1.40M
344
1.40M
    for (i = 0; i < mx; i++) {
345
0
        if (storage != NULL)
346
0
            f = storage[i];
347
0
        else {
348
0
            CRYPTO_THREAD_write_lock(ex_data_lock);
349
0
            f = sk_EX_CALLBACK_value(ip->meth, i);
350
0
            CRYPTO_THREAD_unlock(ex_data_lock);
351
0
        }
352
0
        if (f != NULL && f->free_func != NULL) {
353
0
            ptr = CRYPTO_get_ex_data(ad, i);
354
0
            f->free_func(obj, ptr, ad, i, f->argl, f->argp);
355
0
        }
356
0
    }
357
1.40M
358
1.40M
    if (storage != stack)
359
1.40M
        OPENSSL_free(storage);
360
1.40M
 err:
361
1.40M
    sk_void_free(ad->sk);
362
1.40M
    ad->sk = NULL;
363
1.40M
}
364
365
/*
366
 * For a given CRYPTO_EX_DATA variable, set the value corresponding to a
367
 * particular index in the class used by this variable
368
 */
369
int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int idx, void *val)
370
0
{
371
0
    int i;
372
0
373
0
    if (ad->sk == NULL) {
374
0
        if ((ad->sk = sk_void_new_null()) == NULL) {
375
0
            CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA, ERR_R_MALLOC_FAILURE);
376
0
            return 0;
377
0
        }
378
0
    }
379
0
380
0
    for (i = sk_void_num(ad->sk); i <= idx; ++i) {
381
0
        if (!sk_void_push(ad->sk, NULL)) {
382
0
            CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA, ERR_R_MALLOC_FAILURE);
383
0
            return 0;
384
0
        }
385
0
    }
386
0
    sk_void_set(ad->sk, idx, val);
387
0
    return 1;
388
0
}
389
390
/*
391
 * For a given CRYPTO_EX_DATA_ variable, get the value corresponding to a
392
 * particular index in the class used by this variable
393
 */
394
void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int idx)
395
0
{
396
0
    if (ad->sk == NULL || idx >= sk_void_num(ad->sk))
397
0
        return NULL;
398
0
    return sk_void_value(ad->sk, idx);
399
0
}