/src/openssl/include/internal/mem_alloc_utils.h
Line  | Count  | Source  | 
1  |  | /*  | 
2  |  |  * Copyright 2025 The OpenSSL Project Authors. All Rights Reserved.  | 
3  |  |  *  | 
4  |  |  * Licensed under the Apache License 2.0 (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  |  | /*  | 
11  |  |  * Utility overflow checking and reporting functions  | 
12  |  |  */  | 
13  |  |  | 
14  |  | #ifndef OSSL_INTERNAL_CHECK_SIZE_OVERFLOW_H  | 
15  |  | # define OSSL_INTERNAL_CHECK_SIZE_OVERFLOW_H  | 
16  |  |  | 
17  |  | # include <limits.h>  | 
18  |  | # include <stdbool.h>  | 
19  |  | # include <stdint.h>  | 
20  |  |  | 
21  |  | # include "internal/common.h"  | 
22  |  | # include "internal/safe_math.h"  | 
23  |  |  | 
24  |  | # include <openssl/cryptoerr.h>  | 
25  |  | # include <openssl/err.h>  | 
26  |  |  | 
27  |  | OSSL_SAFE_MATH_UNSIGNED(size_t, size_t)  | 
28  |  |  | 
29  |  | /*  | 
30  |  |  * A helper open-coded aligned alloc implementation around CRYPTO_malloc(),  | 
31  |  |  * for use in cases where non-standard malloc implementation is (or may be,  | 
32  |  |  * as it is the case of the FIPS module) used, or posix_memalign  | 
33  |  |  * is not available.  | 
34  |  |  */  | 
35  |  | void *ossl_malloc_align(size_t num, size_t alignment, void **freeptr,  | 
36  |  |                         const char *file, int line);  | 
37  |  |  | 
38  |  | /*  | 
39  |  |  * A helper routine to report memory allocation errors.  | 
40  |  |  * Similar to the ERR_raise() macro, but accepts explicit file/line arguments,  | 
41  |  |  * pre-defines the library to ERR_LIB_CRYPTO, and avoids emitting an error  | 
42  |  |  * if both file set to NULL and line set to 0.  | 
43  |  |  */  | 
44  |  | static ossl_inline ossl_unused void  | 
45  |  | ossl_report_alloc_err_ex(const char * const file, const int line,  | 
46  |  |                          const int reason)  | 
47  | 0  | { | 
48  |  |     /*  | 
49  |  |      * ossl_err_get_state_int() in err.c uses CRYPTO_zalloc(num, NULL, 0) for  | 
50  |  |      * ERR_STATE allocation. Prevent mem alloc error loop while reporting error.  | 
51  |  |      */  | 
52  | 0  |     if (file != NULL || line != 0) { | 
53  | 0  |         ERR_new();  | 
54  | 0  |         ERR_set_debug(file, line, NULL);  | 
55  | 0  |         ERR_set_error(ERR_LIB_CRYPTO, reason, NULL);  | 
56  | 0  |     }  | 
57  | 0  | } Unexecuted instantiation: array_alloc.c:ossl_report_alloc_err_ex Unexecuted instantiation: mem.c:ossl_report_alloc_err_ex Unexecuted instantiation: params_dup.c:ossl_report_alloc_err_ex Unexecuted instantiation: aligned_alloc.c:ossl_report_alloc_err_ex  | 
58  |  |  | 
59  |  | /* Report a memory allocation failure. */  | 
60  |  | static ossl_inline ossl_unused void  | 
61  |  | ossl_report_alloc_err(const char * const file, const int line)  | 
62  | 0  | { | 
63  | 0  |     ossl_report_alloc_err_ex(file, line, ERR_R_MALLOC_FAILURE);  | 
64  | 0  | } Unexecuted instantiation: array_alloc.c:ossl_report_alloc_err Unexecuted instantiation: mem.c:ossl_report_alloc_err Unexecuted instantiation: params_dup.c:ossl_report_alloc_err Unexecuted instantiation: aligned_alloc.c:ossl_report_alloc_err  | 
65  |  |  | 
66  |  | /* Report an integer overflow during allocation size calculation. */  | 
67  |  | static ossl_inline ossl_unused void  | 
68  |  | ossl_report_alloc_err_of(const char * const file, const int line)  | 
69  | 0  | { | 
70  | 0  |     ossl_report_alloc_err_ex(file, line, CRYPTO_R_INTEGER_OVERFLOW);  | 
71  | 0  | } Unexecuted instantiation: array_alloc.c:ossl_report_alloc_err_of Unexecuted instantiation: mem.c:ossl_report_alloc_err_of Unexecuted instantiation: params_dup.c:ossl_report_alloc_err_of Unexecuted instantiation: aligned_alloc.c:ossl_report_alloc_err_of  | 
72  |  |  | 
73  |  | /* Report invalid memory allocation call arguments. */  | 
74  |  | static ossl_inline ossl_unused void  | 
75  |  | ossl_report_alloc_err_inv(const char * const file, const int line)  | 
76  | 0  | { | 
77  | 0  |     ossl_report_alloc_err_ex(file, line, ERR_R_PASSED_INVALID_ARGUMENT);  | 
78  | 0  | } Unexecuted instantiation: array_alloc.c:ossl_report_alloc_err_inv Unexecuted instantiation: mem.c:ossl_report_alloc_err_inv Unexecuted instantiation: params_dup.c:ossl_report_alloc_err_inv Unexecuted instantiation: aligned_alloc.c:ossl_report_alloc_err_inv  | 
79  |  |  | 
80  |  | /*  | 
81  |  |  * Check the result of num and size multiplication for overflow  | 
82  |  |  * and set error if it is the case;  return true if there was no overflow,  | 
83  |  |  * false if there was.  | 
84  |  |  */  | 
85  |  | static ossl_inline ossl_unused bool  | 
86  |  | ossl_size_mul(const size_t num, const size_t size, size_t *bytes,  | 
87  |  |               const char * const file, const int line)  | 
88  | 51.9k  | { | 
89  | 51.9k  |     int err = 0;  | 
90  | 51.9k  |     *bytes = safe_mul_size_t(num, size, &err);  | 
91  |  |  | 
92  | 51.9k  |     if (ossl_unlikely(err != 0)) { | 
93  | 0  |         ossl_report_alloc_err_of(file, line);  | 
94  |  | 
  | 
95  | 0  |         return false;  | 
96  | 0  |     }  | 
97  |  |  | 
98  | 51.9k  |     return true;  | 
99  | 51.9k  | } array_alloc.c:ossl_size_mul Line  | Count  | Source  |  88  | 51.9k  | { |  89  | 51.9k  |     int err = 0;  |  90  | 51.9k  |     *bytes = safe_mul_size_t(num, size, &err);  |  91  |  |  |  92  | 51.9k  |     if (ossl_unlikely(err != 0)) { |  93  | 0  |         ossl_report_alloc_err_of(file, line);  |  94  |  | 
  |  95  | 0  |         return false;  |  96  | 0  |     }  |  97  |  |  |  98  | 51.9k  |     return true;  |  99  | 51.9k  | }  |  
 Unexecuted instantiation: mem.c:ossl_size_mul Unexecuted instantiation: params_dup.c:ossl_size_mul Unexecuted instantiation: aligned_alloc.c:ossl_size_mul  | 
100  |  |  | 
101  |  | /*  | 
102  |  |  * Check the result of size1 and size2 addition for overflow  | 
103  |  |  * and set error if it is the case;  returns true if there was no overflow,  | 
104  |  |  * false if there was.  | 
105  |  |  */  | 
106  |  | static ossl_inline ossl_unused bool  | 
107  |  | ossl_size_add(const size_t size1, const size_t size2, size_t *bytes,  | 
108  |  |               const char * const file, const int line)  | 
109  | 0  | { | 
110  | 0  |     int err = 0;  | 
111  | 0  |     *bytes = safe_add_size_t(size1, size2, &err);  | 
112  |  | 
  | 
113  | 0  |     if (ossl_unlikely(err != 0)) { | 
114  | 0  |         ossl_report_alloc_err_of(file, line);  | 
115  |  | 
  | 
116  | 0  |         return false;  | 
117  | 0  |     }  | 
118  |  |  | 
119  | 0  |     return true;  | 
120  | 0  | } Unexecuted instantiation: array_alloc.c:ossl_size_add Unexecuted instantiation: mem.c:ossl_size_add Unexecuted instantiation: params_dup.c:ossl_size_add Unexecuted instantiation: aligned_alloc.c:ossl_size_add  | 
121  |  |  | 
122  |  | #endif /* OSSL_INTERNAL_CHECK_SIZE_OVERFLOW_H */  |