/src/libheif/libheif/security_limits.cc
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * HEIF codec. |
3 | | * Copyright (c) 2024 Dirk Farin <dirk.farin@gmail.com> |
4 | | * |
5 | | * This file is part of libheif. |
6 | | * |
7 | | * libheif is free software: you can redistribute it and/or modify |
8 | | * it under the terms of the GNU Lesser General Public License as |
9 | | * published by the Free Software Foundation, either version 3 of |
10 | | * the License, or (at your option) any later version. |
11 | | * |
12 | | * libheif is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | | * GNU Lesser General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU Lesser General Public License |
18 | | * along with libheif. If not, see <http://www.gnu.org/licenses/>. |
19 | | */ |
20 | | |
21 | | #include "security_limits.h" |
22 | | #include <limits> |
23 | | #include <map> |
24 | | #include <mutex> |
25 | | |
26 | | |
27 | | struct heif_security_limits global_security_limits{ |
28 | | .version = 2, |
29 | | |
30 | | // --- version 1 |
31 | | |
32 | | // Artificial limit to avoid allocating too much memory. |
33 | | // 32768^2 = 1.5 GB as YUV-4:2:0 or 4 GB as RGB32 |
34 | | .max_image_size_pixels = 32768 * 32768, |
35 | | .max_number_of_tiles = 4096 * 4096, |
36 | | .max_bayer_pattern_pixels = 16 * 16, |
37 | | .max_items = 1000, |
38 | | |
39 | | .max_color_profile_size = 100 * 1024 * 1024, // 100 MB |
40 | | .max_memory_block_size = UINT64_C(4) * 1024 * 1024 * 1024, // 4 GB |
41 | | |
42 | | .max_components = 256, |
43 | | .max_iloc_extents_per_item = 32, |
44 | | .max_size_entity_group = 64, |
45 | | |
46 | | .max_children_per_box = 100, |
47 | | |
48 | | // --- version 2 |
49 | | |
50 | | .max_total_memory = UINT64_C(4) * 1024 * 1024 * 1024, // 4 GB |
51 | | .max_sample_description_box_entries = 1024, |
52 | | .max_sample_group_description_box_entries = 1024 |
53 | | }; |
54 | | |
55 | | |
56 | | struct heif_security_limits disabled_security_limits{ |
57 | | .version = 2 |
58 | | }; |
59 | | |
60 | | |
61 | | Error check_for_valid_image_size(const heif_security_limits* limits, uint32_t width, uint32_t height) |
62 | 15.1k | { |
63 | 15.1k | uint64_t maximum_image_size_limit = limits->max_image_size_pixels; |
64 | | |
65 | | // --- check whether the image size is "too large" |
66 | | |
67 | 15.1k | if (maximum_image_size_limit > 0) { |
68 | 15.1k | auto max_width_height = static_cast<uint32_t>(std::numeric_limits<int>::max()); |
69 | 15.1k | if ((width > max_width_height || height > max_width_height) || |
70 | 15.1k | (height != 0 && width > maximum_image_size_limit / height)) { |
71 | 70 | std::stringstream sstr; |
72 | 70 | sstr << "Image size " << width << "x" << height << " exceeds the maximum image size " |
73 | 70 | << maximum_image_size_limit << "\n"; |
74 | | |
75 | 70 | return {heif_error_Memory_allocation_error, |
76 | 70 | heif_suberror_Security_limit_exceeded, |
77 | 70 | sstr.str()}; |
78 | 70 | } |
79 | 15.1k | } |
80 | | |
81 | 15.0k | if (width == 0 || height == 0) { |
82 | 3 | return {heif_error_Memory_allocation_error, |
83 | 3 | heif_suberror_Invalid_image_size, |
84 | 3 | "zero width or height"}; |
85 | 3 | } |
86 | | |
87 | 15.0k | return Error::Ok; |
88 | 15.0k | } |
89 | | |
90 | | |
91 | | struct memory_stats { |
92 | | size_t total_memory_usage = 0; |
93 | | size_t max_memory_usage = 0; |
94 | | }; |
95 | | |
96 | | std::mutex& get_memory_usage_mutex() |
97 | 69.7k | { |
98 | 69.7k | static std::mutex sMutex; |
99 | 69.7k | return sMutex; |
100 | 69.7k | } |
101 | | |
102 | | static std::map<const heif_security_limits*, memory_stats> sMemoryUsage; |
103 | | |
104 | | TotalMemoryTracker::TotalMemoryTracker(const heif_security_limits* limits) |
105 | 19.0k | { |
106 | 19.0k | std::lock_guard<std::mutex> lock(get_memory_usage_mutex()); |
107 | | |
108 | 19.0k | sMemoryUsage[limits] = {}; |
109 | 19.0k | m_limits_context = limits; |
110 | 19.0k | } |
111 | | |
112 | | TotalMemoryTracker::~TotalMemoryTracker() |
113 | 19.0k | { |
114 | 19.0k | std::lock_guard<std::mutex> lock(get_memory_usage_mutex()); |
115 | 19.0k | sMemoryUsage.erase(m_limits_context); |
116 | 19.0k | } |
117 | | |
118 | | |
119 | | size_t TotalMemoryTracker::get_max_total_memory_used() const |
120 | 0 | { |
121 | 0 | std::lock_guard<std::mutex> lock(get_memory_usage_mutex()); |
122 | |
|
123 | 0 | auto it = sMemoryUsage.find(m_limits_context); |
124 | 0 | if (it != sMemoryUsage.end()) { |
125 | 0 | return it->second.max_memory_usage; |
126 | 0 | } |
127 | 0 | else { |
128 | 0 | assert(false); |
129 | 0 | return 0; |
130 | 0 | } |
131 | 0 | } |
132 | | |
133 | | |
134 | | Error MemoryHandle::alloc(size_t memory_amount, const heif_security_limits* limits_context, |
135 | | const char* reason_description) |
136 | 21.7k | { |
137 | | // we allow several allocations on the same handle, but they have to be for the same context |
138 | 21.7k | if (m_limits_context) { |
139 | 11.9k | assert(m_limits_context == limits_context); |
140 | 11.9k | } |
141 | | |
142 | | |
143 | | // --- check whether limits are exceeded |
144 | | |
145 | 21.7k | if (!limits_context) { |
146 | 0 | return Error::Ok; |
147 | 0 | } |
148 | | |
149 | | // check against maximum memory block size |
150 | | |
151 | 21.7k | if (limits_context->max_memory_block_size != 0 && |
152 | 21.7k | memory_amount > limits_context->max_memory_block_size) { |
153 | 18 | std::stringstream sstr; |
154 | | |
155 | 18 | if (reason_description) { |
156 | 18 | sstr << "Allocating " << memory_amount << " bytes for " << reason_description <<" exceeds the security limit of " |
157 | 18 | << limits_context->max_memory_block_size << " bytes"; |
158 | 18 | } |
159 | 0 | else { |
160 | 0 | sstr << "Allocating " << memory_amount << " bytes exceeds the security limit of " |
161 | 0 | << limits_context->max_memory_block_size << " bytes"; |
162 | 0 | } |
163 | | |
164 | 18 | return {heif_error_Memory_allocation_error, |
165 | 18 | heif_suberror_Security_limit_exceeded, |
166 | 18 | sstr.str()}; |
167 | 18 | } |
168 | | |
169 | 21.6k | if (limits_context == &global_security_limits || |
170 | 21.6k | limits_context == &disabled_security_limits) { |
171 | 0 | return Error::Ok; |
172 | 0 | } |
173 | | |
174 | 21.6k | std::lock_guard<std::mutex> lock(get_memory_usage_mutex()); |
175 | 21.6k | auto it = sMemoryUsage.find(limits_context); |
176 | 21.6k | if (it == sMemoryUsage.end()) { |
177 | 0 | assert(false); |
178 | 0 | return Error::Ok; |
179 | 0 | } |
180 | | |
181 | | // check against maximum total memory usage |
182 | | |
183 | 21.6k | if (limits_context->max_total_memory != 0 && |
184 | 21.6k | it->second.total_memory_usage + memory_amount > limits_context->max_total_memory) { |
185 | 0 | std::stringstream sstr; |
186 | |
|
187 | 0 | if (reason_description) { |
188 | 0 | sstr << "Memory usage of " << it->second.total_memory_usage + memory_amount |
189 | 0 | << " bytes for " << reason_description << " exceeds the security limit of " |
190 | 0 | << limits_context->max_total_memory << " bytes of total memory usage"; |
191 | 0 | } |
192 | 0 | else { |
193 | 0 | sstr << "Memory usage of " << it->second.total_memory_usage + memory_amount |
194 | 0 | << " bytes exceeds the security limit of " |
195 | 0 | << limits_context->max_total_memory << " bytes of total memory usage"; |
196 | 0 | } |
197 | |
|
198 | 0 | return {heif_error_Memory_allocation_error, |
199 | 0 | heif_suberror_Security_limit_exceeded, |
200 | 0 | sstr.str()}; |
201 | 0 | } |
202 | | |
203 | | |
204 | | // --- register memory usage |
205 | | |
206 | 21.6k | m_limits_context = limits_context; |
207 | 21.6k | m_memory_amount += memory_amount; |
208 | | |
209 | 21.6k | it->second.total_memory_usage += memory_amount; |
210 | | |
211 | | // remember maximum memory usage (for informational purpose) |
212 | 21.6k | if (it->second.total_memory_usage > it->second.max_memory_usage) { |
213 | 20.5k | it->second.max_memory_usage = it->second.total_memory_usage; |
214 | 20.5k | } |
215 | | |
216 | 21.6k | return Error::Ok; |
217 | 21.6k | } |
218 | | |
219 | | |
220 | | void MemoryHandle::free() |
221 | 10.0k | { |
222 | 10.0k | if (m_limits_context) { |
223 | 9.79k | std::lock_guard<std::mutex> lock(get_memory_usage_mutex()); |
224 | | |
225 | 9.79k | auto it = sMemoryUsage.find(m_limits_context); |
226 | 9.79k | if (it != sMemoryUsage.end()) { |
227 | 9.77k | it->second.total_memory_usage -= m_memory_amount; |
228 | 9.77k | } |
229 | | |
230 | 9.79k | m_limits_context = nullptr; |
231 | 9.79k | m_memory_amount = 0; |
232 | 9.79k | } |
233 | 10.0k | } |
234 | | |
235 | | |
236 | | void MemoryHandle::free(size_t memory_amount) |
237 | 110 | { |
238 | 110 | if (m_limits_context) { |
239 | 110 | std::lock_guard<std::mutex> lock(get_memory_usage_mutex()); |
240 | | |
241 | 110 | auto it = sMemoryUsage.find(m_limits_context); |
242 | 110 | if (it != sMemoryUsage.end()) { |
243 | 110 | it->second.total_memory_usage -= memory_amount; |
244 | 110 | } |
245 | | |
246 | 110 | m_memory_amount -= memory_amount; |
247 | 110 | } |
248 | 110 | } |
249 | | |