Coverage Report

Created: 2025-06-11 06:41

/src/boringssl/crypto/ex_data.cc
Line
Count
Source (jump to first uncovered line)
1
// Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     https://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#include <openssl/ex_data.h>
16
17
#include <assert.h>
18
#include <limits.h>
19
#include <stdlib.h>
20
#include <string.h>
21
22
#include <openssl/crypto.h>
23
#include <openssl/err.h>
24
#include <openssl/mem.h>
25
26
#include "internal.h"
27
28
29
struct crypto_ex_data_func_st {
30
  long argl;   // Arbitary long
31
  void *argp;  // Arbitary void pointer
32
  CRYPTO_EX_free *free_func;
33
  // next points to the next |CRYPTO_EX_DATA_FUNCS| or NULL if this is the last
34
  // one. It may only be read if synchronized with a read from |num_funcs|.
35
  CRYPTO_EX_DATA_FUNCS *next;
36
};
37
38
int CRYPTO_get_ex_new_index_ex(CRYPTO_EX_DATA_CLASS *ex_data_class, long argl,
39
0
                               void *argp, CRYPTO_EX_free *free_func) {
40
0
  CRYPTO_EX_DATA_FUNCS *funcs = reinterpret_cast<CRYPTO_EX_DATA_FUNCS *>(
41
0
      OPENSSL_malloc(sizeof(CRYPTO_EX_DATA_FUNCS)));
42
0
  if (funcs == NULL) {
43
0
    return -1;
44
0
  }
45
46
0
  funcs->argl = argl;
47
0
  funcs->argp = argp;
48
0
  funcs->free_func = free_func;
49
0
  funcs->next = NULL;
50
51
0
  CRYPTO_MUTEX_lock_write(&ex_data_class->lock);
52
53
0
  uint32_t num_funcs = CRYPTO_atomic_load_u32(&ex_data_class->num_funcs);
54
  // The index must fit in |int|.
55
0
  if (num_funcs > (size_t)(INT_MAX - ex_data_class->num_reserved)) {
56
0
    OPENSSL_PUT_ERROR(CRYPTO, ERR_R_OVERFLOW);
57
0
    CRYPTO_MUTEX_unlock_write(&ex_data_class->lock);
58
0
    return -1;
59
0
  }
60
61
  // Append |funcs| to the linked list.
62
0
  if (ex_data_class->last == NULL) {
63
0
    assert(num_funcs == 0);
64
0
    ex_data_class->funcs = funcs;
65
0
    ex_data_class->last = funcs;
66
0
  } else {
67
0
    ex_data_class->last->next = funcs;
68
0
    ex_data_class->last = funcs;
69
0
  }
70
71
0
  CRYPTO_atomic_store_u32(&ex_data_class->num_funcs, num_funcs + 1);
72
0
  CRYPTO_MUTEX_unlock_write(&ex_data_class->lock);
73
0
  return (int)num_funcs + ex_data_class->num_reserved;
74
0
}
75
76
8.62k
int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int index, void *val) {
77
8.62k
  if (index < 0) {
78
    // A caller that can accidentally pass in an invalid index into this
79
    // function will hit an memory error if |index| happened to be valid, and
80
    // expected |val| to be of a different type.
81
0
    abort();
82
0
  }
83
84
8.62k
  if (ad->sk == NULL) {
85
8.62k
    ad->sk = sk_void_new_null();
86
8.62k
    if (ad->sk == NULL) {
87
0
      return 0;
88
0
    }
89
8.62k
  }
90
91
  // Add NULL values until the stack is long enough.
92
17.2k
  for (size_t i = sk_void_num(ad->sk); i <= (size_t)index; i++) {
93
8.62k
    if (!sk_void_push(ad->sk, NULL)) {
94
0
      return 0;
95
0
    }
96
8.62k
  }
97
98
8.62k
  sk_void_set(ad->sk, (size_t)index, val);
99
8.62k
  return 1;
100
8.62k
}
101
102
0
void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int idx) {
103
0
  if (ad->sk == NULL || idx < 0 || (size_t)idx >= sk_void_num(ad->sk)) {
104
0
    return NULL;
105
0
  }
106
0
  return sk_void_value(ad->sk, idx);
107
0
}
108
109
1.19M
void CRYPTO_new_ex_data(CRYPTO_EX_DATA *ad) { ad->sk = NULL; }
110
111
void CRYPTO_free_ex_data(CRYPTO_EX_DATA_CLASS *ex_data_class,
112
1.20M
                         CRYPTO_EX_DATA *ad) {
113
1.20M
  if (ad->sk == NULL) {
114
    // Nothing to do.
115
1.19M
    return;
116
1.19M
  }
117
118
8.62k
  uint32_t num_funcs = CRYPTO_atomic_load_u32(&ex_data_class->num_funcs);
119
  // |CRYPTO_get_ex_new_index_ex| will not allocate indices beyond |INT_MAX|.
120
8.62k
  assert(num_funcs <= (size_t)(INT_MAX - ex_data_class->num_reserved));
121
122
  // Defer dereferencing |ex_data_class->funcs| and |funcs->next|. It must come
123
  // after the |num_funcs| comparison to be correctly synchronized.
124
8.62k
  CRYPTO_EX_DATA_FUNCS *const *funcs = &ex_data_class->funcs;
125
8.62k
  for (uint32_t i = 0; i < num_funcs; i++) {
126
0
    if ((*funcs)->free_func != NULL) {
127
0
      int index = (int)i + ex_data_class->num_reserved;
128
0
      void *ptr = CRYPTO_get_ex_data(ad, index);
129
0
      (*funcs)->free_func(/*parent=*/nullptr, ptr, /*ad*/ nullptr, index,
130
0
                          (*funcs)->argl, (*funcs)->argp);
131
0
    }
132
0
    funcs = &(*funcs)->next;
133
0
  }
134
135
8.62k
  sk_void_free(ad->sk);
136
8.62k
  ad->sk = NULL;
137
8.62k
}
138
139
0
void CRYPTO_cleanup_all_ex_data(void) {}