1 /*
2 * Copyright 1995-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 <openssl/ex_data.h>
11
12 #include <assert.h>
13 #include <limits.h>
14 #include <stdlib.h>
15 #include <string.h>
16
17 #include <openssl/crypto.h>
18 #include <openssl/err.h>
19 #include <openssl/mem.h>
20 #include <openssl/thread.h>
21
22 #include "internal.h"
23
24
25 DEFINE_STACK_OF(CRYPTO_EX_DATA_FUNCS)
26
27 struct crypto_ex_data_func_st {
28 long argl; // Arbitary long
29 void *argp; // Arbitary void pointer
30 CRYPTO_EX_free *free_func;
31 // next points to the next |CRYPTO_EX_DATA_FUNCS| or NULL if this is the last
32 // one. It may only be read if synchronized with a read from |num_funcs|.
33 CRYPTO_EX_DATA_FUNCS *next;
34 };
35
CRYPTO_get_ex_new_index_ex(CRYPTO_EX_DATA_CLASS * ex_data_class,long argl,void * argp,CRYPTO_EX_free * free_func)36 int CRYPTO_get_ex_new_index_ex(CRYPTO_EX_DATA_CLASS *ex_data_class, long argl,
37 void *argp, CRYPTO_EX_free *free_func) {
38 CRYPTO_EX_DATA_FUNCS *funcs = reinterpret_cast<CRYPTO_EX_DATA_FUNCS *>(
39 OPENSSL_malloc(sizeof(CRYPTO_EX_DATA_FUNCS)));
40 if (funcs == NULL) {
41 return -1;
42 }
43
44 funcs->argl = argl;
45 funcs->argp = argp;
46 funcs->free_func = free_func;
47 funcs->next = NULL;
48
49 CRYPTO_MUTEX_lock_write(&ex_data_class->lock);
50
51 uint32_t num_funcs = CRYPTO_atomic_load_u32(&ex_data_class->num_funcs);
52 // The index must fit in |int|.
53 if (num_funcs > (size_t)(INT_MAX - ex_data_class->num_reserved)) {
54 OPENSSL_PUT_ERROR(CRYPTO, ERR_R_OVERFLOW);
55 CRYPTO_MUTEX_unlock_write(&ex_data_class->lock);
56 return -1;
57 }
58
59 // Append |funcs| to the linked list.
60 if (ex_data_class->last == NULL) {
61 assert(num_funcs == 0);
62 ex_data_class->funcs = funcs;
63 ex_data_class->last = funcs;
64 } else {
65 ex_data_class->last->next = funcs;
66 ex_data_class->last = funcs;
67 }
68
69 CRYPTO_atomic_store_u32(&ex_data_class->num_funcs, num_funcs + 1);
70 CRYPTO_MUTEX_unlock_write(&ex_data_class->lock);
71 return (int)num_funcs + ex_data_class->num_reserved;
72 }
73
CRYPTO_set_ex_data(CRYPTO_EX_DATA * ad,int index,void * val)74 int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int index, void *val) {
75 if (index < 0) {
76 // A caller that can accidentally pass in an invalid index into this
77 // function will hit an memory error if |index| happened to be valid, and
78 // expected |val| to be of a different type.
79 abort();
80 }
81
82 if (ad->sk == NULL) {
83 ad->sk = sk_void_new_null();
84 if (ad->sk == NULL) {
85 return 0;
86 }
87 }
88
89 // Add NULL values until the stack is long enough.
90 for (size_t i = sk_void_num(ad->sk); i <= (size_t)index; i++) {
91 if (!sk_void_push(ad->sk, NULL)) {
92 return 0;
93 }
94 }
95
96 sk_void_set(ad->sk, (size_t)index, val);
97 return 1;
98 }
99
CRYPTO_get_ex_data(const CRYPTO_EX_DATA * ad,int idx)100 void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int idx) {
101 if (ad->sk == NULL || idx < 0 || (size_t)idx >= sk_void_num(ad->sk)) {
102 return NULL;
103 }
104 return sk_void_value(ad->sk, idx);
105 }
106
CRYPTO_new_ex_data(CRYPTO_EX_DATA * ad)107 void CRYPTO_new_ex_data(CRYPTO_EX_DATA *ad) { ad->sk = NULL; }
108
CRYPTO_free_ex_data(CRYPTO_EX_DATA_CLASS * ex_data_class,void * obj,CRYPTO_EX_DATA * ad)109 void CRYPTO_free_ex_data(CRYPTO_EX_DATA_CLASS *ex_data_class, void *obj,
110 CRYPTO_EX_DATA *ad) {
111 if (ad->sk == NULL) {
112 // Nothing to do.
113 return;
114 }
115
116 uint32_t num_funcs = CRYPTO_atomic_load_u32(&ex_data_class->num_funcs);
117 // |CRYPTO_get_ex_new_index_ex| will not allocate indices beyond |INT_MAX|.
118 assert(num_funcs <= (size_t)(INT_MAX - ex_data_class->num_reserved));
119
120 // Defer dereferencing |ex_data_class->funcs| and |funcs->next|. It must come
121 // after the |num_funcs| comparison to be correctly synchronized.
122 CRYPTO_EX_DATA_FUNCS *const *funcs = &ex_data_class->funcs;
123 for (uint32_t i = 0; i < num_funcs; i++) {
124 if ((*funcs)->free_func != NULL) {
125 int index = (int)i + ex_data_class->num_reserved;
126 void *ptr = CRYPTO_get_ex_data(ad, index);
127 (*funcs)->free_func(obj, ptr, ad, index, (*funcs)->argl, (*funcs)->argp);
128 }
129 funcs = &(*funcs)->next;
130 }
131
132 sk_void_free(ad->sk);
133 ad->sk = NULL;
134 }
135
CRYPTO_cleanup_all_ex_data(void)136 void CRYPTO_cleanup_all_ex_data(void) {}
137