| File: | builds/wireshark/wireshark/epan/tvbuff_zlib.c |
| Warning: | line 297, column 15 Potential leak of memory pointed to by 'strmbuf' |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | /* tvbuff_zlib.c | |||
| 2 | * | |||
| 3 | * Copyright (c) 2000 by Gilbert Ramirez <[email protected]> | |||
| 4 | * | |||
| 5 | * Wireshark - Network traffic analyzer | |||
| 6 | * By Gerald Combs <[email protected]> | |||
| 7 | * Copyright 1998 Gerald Combs | |||
| 8 | * | |||
| 9 | * SPDX-License-Identifier: GPL-2.0-or-later | |||
| 10 | */ | |||
| 11 | ||||
| 12 | #include <config.h> | |||
| 13 | #define WS_LOG_DOMAIN"Epan" LOG_DOMAIN_EPAN"Epan" | |||
| 14 | ||||
| 15 | #include <glib.h> | |||
| 16 | ||||
| 17 | #include <string.h> | |||
| 18 | #include <wsutil/glib-compat.h> | |||
| 19 | #include <wsutil/zlib_compat.h> | |||
| 20 | ||||
| 21 | #include "tvbuff.h" | |||
| 22 | #include <wsutil/wslog.h> | |||
| 23 | ||||
| 24 | #ifdef USE_ZLIB_OR_ZLIBNG | |||
| 25 | /* | |||
| 26 | * Uncompresses a zlib compressed packet inside a message of tvb at offset with | |||
| 27 | * length comprlen. Returns an uncompressed tvbuffer if uncompression | |||
| 28 | * succeeded or NULL if uncompression failed. | |||
| 29 | */ | |||
| 30 | #define TVB_Z_MIN_BUFSIZ32768 32768 | |||
| 31 | #define TVB_Z_MAX_BUFSIZ1048576 * 10 1048576 * 10 | |||
| 32 | ||||
| 33 | tvbuff_t * | |||
| 34 | tvb_uncompress_zlib_max(tvbuff_t *tvb, const unsigned offset, unsigned comprlen, unsigned max_out) | |||
| 35 | { | |||
| 36 | int err; | |||
| 37 | /* bytes_out is an unsigned because tvb_new_real_data does not accept | |||
| 38 | * more than an unsigned. */ | |||
| 39 | unsigned bytes_out = 0; | |||
| 40 | uint8_t *compr; | |||
| 41 | tvbuff_t *uncompr_tvb = NULL((void*)0); | |||
| 42 | zlib_stream strm = {0}; | |||
| 43 | Bytef *strmbuf; | |||
| 44 | unsigned inits_done = 0; | |||
| 45 | int wbits = MAX_WBITS15; | |||
| 46 | uint8_t *next; | |||
| 47 | unsigned bufsiz; | |||
| 48 | unsigned inflate_passes = 0; | |||
| 49 | unsigned bytes_in = tvb_captured_length_remaining(tvb, offset); | |||
| 50 | ||||
| 51 | if (tvb == NULL((void*)0) || comprlen <= 0) { | |||
| ||||
| 52 | return NULL((void*)0); | |||
| 53 | } | |||
| 54 | ||||
| 55 | compr = (uint8_t *)tvb_memdup(NULL((void*)0), tvb, offset, comprlen); | |||
| 56 | if (compr == NULL((void*)0)) { | |||
| 57 | return NULL((void*)0); | |||
| 58 | } | |||
| 59 | ||||
| 60 | /* | |||
| 61 | * Assume that the uncompressed data is at least twice as big as | |||
| 62 | * the compressed size. | |||
| 63 | */ | |||
| 64 | if (ckd_mul(&bufsiz, bytes_in, 2)__builtin_mul_overflow((bytes_in), (2), (&bufsiz))) { | |||
| 65 | bufsiz = TVB_Z_MAX_BUFSIZ1048576 * 10; | |||
| 66 | } else { | |||
| 67 | bufsiz = CLAMP(bufsiz, TVB_Z_MIN_BUFSIZ, TVB_Z_MAX_BUFSIZ)(((bufsiz) > (1048576 * 10)) ? (1048576 * 10) : (((bufsiz) < (32768)) ? (32768) : (bufsiz))); | |||
| 68 | } | |||
| 69 | ||||
| 70 | ws_debug("bufsiz: %u bytes\n", bufsiz)do { if (1) { ws_log_full("Epan", LOG_LEVEL_DEBUG, "epan/tvbuff_zlib.c" , 70, __func__, "bufsiz: %u bytes\n", bufsiz); } } while (0); | |||
| 71 | ||||
| 72 | next = compr; | |||
| 73 | ||||
| 74 | strm.next_in = next; | |||
| 75 | strm.avail_in = comprlen; | |||
| 76 | ||||
| 77 | strmbuf = (Bytef *)g_malloc(bufsiz); | |||
| 78 | ||||
| 79 | err = ZLIB_PREFIX(inflateInit2)(&strm, wbits)inflateInit2_((&strm), (wbits), "1.3.1", (int)sizeof(z_stream )); | |||
| 80 | inits_done = 1; | |||
| 81 | if (err != Z_OK0) { | |||
| 82 | ZLIB_PREFIX(inflateEnd)inflateEnd(&strm); | |||
| 83 | goto exit_err; | |||
| 84 | } | |||
| 85 | ||||
| 86 | while (1) { | |||
| 87 | strm.next_out = &strmbuf[bytes_out]; | |||
| 88 | strm.avail_out = bufsiz; | |||
| 89 | if (max_out - bytes_out < bufsiz) { | |||
| 90 | /* One more byte than allowed, to detect the excess. */ | |||
| 91 | strm.avail_out = max_out - bytes_out + 1; | |||
| 92 | } | |||
| 93 | ||||
| 94 | err = ZLIB_PREFIX(inflate)inflate(&strm, Z_SYNC_FLUSH2); | |||
| 95 | ||||
| 96 | if (err == Z_OK0 || err == Z_STREAM_END1) { | |||
| 97 | unsigned bytes_pass; | |||
| 98 | ||||
| 99 | if (max_out - bytes_out < bufsiz) { | |||
| 100 | bytes_pass = max_out - bytes_out + 1 - strm.avail_out; | |||
| 101 | if (bytes_pass > max_out - bytes_out) { | |||
| 102 | /* Longer than the caller allows. */ | |||
| 103 | ZLIB_PREFIX(inflateEnd)inflateEnd(&strm); | |||
| 104 | goto exit_err; | |||
| 105 | } | |||
| 106 | } else { | |||
| 107 | bytes_pass = bufsiz - strm.avail_out; | |||
| 108 | } | |||
| 109 | ||||
| 110 | // This is an unsigned long, but won't overflow even | |||
| 111 | // on platforms where that is 32-bit, because bufsize | |||
| 112 | // is clamped, if we break when it reaches INT_MAX. | |||
| 113 | if (strm.total_out > INT_MAX2147483647) { | |||
| 114 | // Qt uses signed ints in various classes, so | |||
| 115 | // the GUI can't really handle anything | |||
| 116 | ws_debug("overflow, returning what we have")do { if (1) { ws_log_full("Epan", LOG_LEVEL_DEBUG, "epan/tvbuff_zlib.c" , 116, __func__, "overflow, returning what we have"); } } while (0); | |||
| 117 | ZLIB_PREFIX(inflateEnd)inflateEnd(&strm); | |||
| 118 | break; | |||
| 119 | } | |||
| 120 | ||||
| 121 | /* Use this for a workaround for bug #6480 | |||
| 122 | * (https://gitlab.com/wireshark/wireshark/-/issues/6480) | |||
| 123 | * When a zero length payload was compressed into 20 bytes | |||
| 124 | * bytes we want to return a tvb with 0 length instead | |||
| 125 | * of NULL. */ | |||
| 126 | ++inflate_passes; | |||
| 127 | ||||
| 128 | bytes_out += bytes_pass; | |||
| 129 | ws_assert(bytes_out == strm.total_out)do { if ((1) && !(bytes_out == strm.total_out)) ws_log_fatal_full ("Epan", LOG_LEVEL_ERROR, "epan/tvbuff_zlib.c", 129, __func__ , "assertion failed: %s", "bytes_out == strm.total_out"); } while (0); | |||
| 130 | ||||
| 131 | /* Grow the buffer even at max_out: the next pass | |||
| 132 | * asks for one more byte to detect the excess. */ | |||
| 133 | if (strm.avail_out == 0) { | |||
| 134 | strmbuf = (uint8_t*)g_realloc(strmbuf, bytes_out + bufsiz); | |||
| 135 | } | |||
| 136 | ||||
| 137 | if (err
| |||
| 138 | ZLIB_PREFIX(inflateEnd)inflateEnd(&strm); | |||
| 139 | break; | |||
| 140 | } | |||
| 141 | } else if (err == Z_BUF_ERROR(-5)) { | |||
| 142 | /* | |||
| 143 | * It's possible that not enough frames were captured | |||
| 144 | * to decompress this fully, so return what we've done | |||
| 145 | * so far, if any. | |||
| 146 | */ | |||
| 147 | ZLIB_PREFIX(inflateEnd)inflateEnd(&strm); | |||
| 148 | ||||
| 149 | if (inflate_passes) { | |||
| 150 | break; | |||
| 151 | } else { | |||
| 152 | goto exit_err; | |||
| 153 | } | |||
| 154 | ||||
| 155 | } else if (err == Z_DATA_ERROR(-3) && inits_done == 1 | |||
| 156 | && inflate_passes == 0 && comprlen >= 2 && | |||
| 157 | (*compr == 0x1f) && (*(compr + 1) == 0x8b)) { | |||
| 158 | /* | |||
| 159 | * inflate() is supposed to handle both gzip and deflate | |||
| 160 | * streams automatically, but in reality it doesn't | |||
| 161 | * seem to handle either (at least not within the | |||
| 162 | * context of an HTTP response.) We have to try | |||
| 163 | * several tweaks, depending on the type of data and | |||
| 164 | * version of the library installed. | |||
| 165 | */ | |||
| 166 | ||||
| 167 | /* | |||
| 168 | * Gzip file format. Skip past the header, since the | |||
| 169 | * fix to make it work (setting windowBits to 31) | |||
| 170 | * doesn't work with all versions of the library. | |||
| 171 | */ | |||
| 172 | Bytef *c = compr + 2; | |||
| 173 | Bytef flags = 0; | |||
| 174 | ||||
| 175 | /* we read two bytes already (0x1f, 0x8b) and | |||
| 176 | need at least Z_DEFLATED, 1 byte flags, 4 | |||
| 177 | bytes MTIME, 1 byte XFL, 1 byte OS */ | |||
| 178 | if (comprlen < 10 || *c != Z_DEFLATED8) { | |||
| 179 | ZLIB_PREFIX(inflateEnd)inflateEnd(&strm); | |||
| 180 | goto exit_err; | |||
| 181 | } | |||
| 182 | ||||
| 183 | c++; | |||
| 184 | flags = *c; | |||
| 185 | c++; | |||
| 186 | ||||
| 187 | /* Skip past the MTIME (4 bytes), | |||
| 188 | XFL, and OS fields (1 byte each). */ | |||
| 189 | c += 6; | |||
| 190 | ||||
| 191 | if (flags & (1 << 2)) { | |||
| 192 | /* An Extra field is present. It | |||
| 193 | consists of 2 bytes xsize and xsize | |||
| 194 | bytes of data. | |||
| 195 | Read byte-by-byte (least significant | |||
| 196 | byte first) to make sure we abort | |||
| 197 | cleanly when the xsize is truncated | |||
| 198 | after the first byte. */ | |||
| 199 | uint16_t xsize = 0; | |||
| 200 | ||||
| 201 | if ((unsigned)(c-compr) < comprlen) { | |||
| 202 | xsize += *c; | |||
| 203 | c++; | |||
| 204 | } | |||
| 205 | if ((unsigned)(c-compr) < comprlen) { | |||
| 206 | xsize += *c << 8; | |||
| 207 | c++; | |||
| 208 | } | |||
| 209 | ||||
| 210 | c += xsize; | |||
| 211 | } | |||
| 212 | ||||
| 213 | if (flags & (1 << 3)) { | |||
| 214 | /* A null terminated filename */ | |||
| 215 | ||||
| 216 | while ((unsigned)(c - compr) < comprlen && *c != '\0') { | |||
| 217 | c++; | |||
| 218 | } | |||
| 219 | ||||
| 220 | c++; | |||
| 221 | } | |||
| 222 | ||||
| 223 | if (flags & (1 << 4)) { | |||
| 224 | /* A null terminated comment */ | |||
| 225 | ||||
| 226 | while ((unsigned)(c - compr) < comprlen && *c != '\0') { | |||
| 227 | c++; | |||
| 228 | } | |||
| 229 | ||||
| 230 | c++; | |||
| 231 | } | |||
| 232 | ||||
| 233 | ||||
| 234 | if ((unsigned)(c - compr) > comprlen) { | |||
| 235 | ZLIB_PREFIX(inflateEnd)inflateEnd(&strm); | |||
| 236 | goto exit_err; | |||
| 237 | } | |||
| 238 | /* Drop gzip header */ | |||
| 239 | comprlen -= (unsigned)(c - compr); | |||
| 240 | next = c; | |||
| 241 | ||||
| 242 | ZLIB_PREFIX(inflateReset)inflateReset(&strm); | |||
| 243 | strm.next_in = next; | |||
| 244 | strm.avail_in = comprlen; | |||
| 245 | ||||
| 246 | ZLIB_PREFIX(inflateEnd)inflateEnd(&strm); | |||
| 247 | err = ZLIB_PREFIX(inflateInit2)(&strm, wbits)inflateInit2_((&strm), (wbits), "1.3.1", (int)sizeof(z_stream )); | |||
| 248 | inits_done++; | |||
| 249 | if (err != Z_OK0) { | |||
| 250 | goto exit_err; | |||
| 251 | } | |||
| 252 | } else if (err == Z_DATA_ERROR(-3) && inflate_passes == 0 && | |||
| 253 | inits_done <= 3) { | |||
| 254 | ||||
| 255 | /* | |||
| 256 | * Re-init the stream with a negative | |||
| 257 | * MAX_WBITS. This is necessary due to | |||
| 258 | * some servers (Apache) not sending | |||
| 259 | * the deflate header with the | |||
| 260 | * content-encoded response. | |||
| 261 | */ | |||
| 262 | wbits = -MAX_WBITS15; | |||
| 263 | ||||
| 264 | ZLIB_PREFIX(inflateReset)inflateReset(&strm); | |||
| 265 | ||||
| 266 | strm.next_in = next; | |||
| 267 | strm.avail_in = comprlen; | |||
| 268 | ||||
| 269 | ZLIB_PREFIX(inflateEnd)inflateEnd(&strm); | |||
| 270 | strm.next_out = strmbuf; | |||
| 271 | strm.avail_out = bufsiz; | |||
| 272 | ||||
| 273 | err = ZLIB_PREFIX(inflateInit2)(&strm, wbits)inflateInit2_((&strm), (wbits), "1.3.1", (int)sizeof(z_stream )); | |||
| 274 | ||||
| 275 | inits_done++; | |||
| 276 | ||||
| 277 | if (err != Z_OK0) { | |||
| 278 | goto exit_err; | |||
| 279 | } | |||
| 280 | } else { | |||
| 281 | ZLIB_PREFIX(inflateEnd)inflateEnd(&strm); | |||
| 282 | ||||
| 283 | if (!inflate_passes) { | |||
| 284 | goto exit_err; | |||
| 285 | } | |||
| 286 | ||||
| 287 | break; | |||
| 288 | } | |||
| 289 | } | |||
| 290 | ||||
| 291 | ws_debug("inflate() total passes: %u\n", inflate_passes)do { if (1) { ws_log_full("Epan", LOG_LEVEL_DEBUG, "epan/tvbuff_zlib.c" , 291, __func__, "inflate() total passes: %u\n", inflate_passes ); } } while (0); | |||
| 292 | ws_debug("bytes in: %u\nbytes out: %u\n\n", bytes_in, bytes_out)do { if (1) { ws_log_full("Epan", LOG_LEVEL_DEBUG, "epan/tvbuff_zlib.c" , 292, __func__, "bytes in: %u\nbytes out: %u\n\n", bytes_in , bytes_out); } } while (0); | |||
| 293 | ||||
| 294 | if (inflate_passes
| |||
| 295 | /* g_realloc(..., 0) is well-defined, returns NULL. */ | |||
| 296 | strmbuf = (uint8_t*)g_realloc(strmbuf, bytes_out); | |||
| 297 | uncompr_tvb = tvb_new_real_data(strmbuf, bytes_out, bytes_out); | |||
| ||||
| 298 | tvb_set_free_cb(uncompr_tvb, g_free); | |||
| 299 | wmem_free(NULL((void*)0), compr); | |||
| 300 | return uncompr_tvb; | |||
| 301 | } | |||
| 302 | ||||
| 303 | exit_err: | |||
| 304 | wmem_free(NULL((void*)0), compr); | |||
| 305 | g_free(strmbuf)(__builtin_object_size ((strmbuf), 0) != ((size_t) - 1)) ? g_free_sized (strmbuf, __builtin_object_size ((strmbuf), 0)) : (g_free) ( strmbuf); | |||
| 306 | return NULL((void*)0); | |||
| 307 | } | |||
| 308 | ||||
| 309 | tvbuff_t * | |||
| 310 | tvb_uncompress_zlib(tvbuff_t *tvb, const unsigned offset, unsigned comprlen) | |||
| 311 | { | |||
| 312 | return tvb_uncompress_zlib_max(tvb, offset, comprlen, UINT_MAX(2147483647 *2U +1U)); | |||
| 313 | } | |||
| 314 | #else /* USE_ZLIB_OR_ZLIBNG */ | |||
| 315 | tvbuff_t * | |||
| 316 | tvb_uncompress_zlib_max(tvbuff_t *tvb _U___attribute__((unused)), const unsigned offset _U___attribute__((unused)), unsigned comprlen _U___attribute__((unused)), unsigned max_out _U___attribute__((unused))) | |||
| 317 | { | |||
| 318 | return NULL((void*)0); | |||
| 319 | } | |||
| 320 | ||||
| 321 | tvbuff_t * | |||
| 322 | tvb_uncompress_zlib(tvbuff_t *tvb _U___attribute__((unused)), const unsigned offset _U___attribute__((unused)), unsigned comprlen _U___attribute__((unused))) | |||
| 323 | { | |||
| 324 | return NULL((void*)0); | |||
| 325 | } | |||
| 326 | #endif /* USE_ZLIB_OR_ZLIBNG */ | |||
| 327 | ||||
| 328 | tvbuff_t * | |||
| 329 | tvb_child_uncompress_zlib(tvbuff_t *parent, tvbuff_t *tvb, const unsigned offset, unsigned comprlen) | |||
| 330 | { | |||
| 331 | tvbuff_t *new_tvb = tvb_uncompress_zlib(tvb, offset, comprlen); | |||
| 332 | if (new_tvb) | |||
| 333 | tvb_set_child_real_data_tvbuff (parent, new_tvb); | |||
| 334 | return new_tvb; | |||
| 335 | } | |||
| 336 | ||||
| 337 | tvbuff_t * | |||
| 338 | tvb_child_uncompress_zlib_max(tvbuff_t *parent, tvbuff_t *tvb, const unsigned offset, unsigned comprlen, unsigned max_out) | |||
| 339 | { | |||
| 340 | tvbuff_t *new_tvb = tvb_uncompress_zlib_max(tvb, offset, comprlen, max_out); | |||
| 341 | if (new_tvb) | |||
| 342 | tvb_set_child_real_data_tvbuff (parent, new_tvb); | |||
| 343 | return new_tvb; | |||
| 344 | } | |||
| 345 | ||||
| 346 | /* | |||
| 347 | * Editor modelines - https://www.wireshark.org/tools/modelines.html | |||
| 348 | * | |||
| 349 | * Local variables: | |||
| 350 | * c-basic-offset: 8 | |||
| 351 | * tab-width: 8 | |||
| 352 | * indent-tabs-mode: t | |||
| 353 | * End: | |||
| 354 | * | |||
| 355 | * vi: set shiftwidth=8 tabstop=8 noexpandtab: | |||
| 356 | * :indentSize=8:tabSize=8:noTabs=false: | |||
| 357 | */ |