| File: | builds/wireshark/wireshark/epan/tvbuff_zlib.c |
| Warning: | line 303, column 23 Attempt to release already released memory |
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(tvbuff_t *tvb, const unsigned offset, unsigned comprlen) | |||
| 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_streamp strm; | |||
| 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 = g_new0(zlib_stream, 1)((zlib_stream *) g_malloc0_n ((1), sizeof (zlib_stream))); | |||
| 75 | strm->next_in = next; | |||
| 76 | strm->avail_in = comprlen; | |||
| 77 | ||||
| 78 | strmbuf = (Bytef *)g_malloc(bufsiz); | |||
| 79 | ||||
| 80 | err = ZLIB_PREFIX(inflateInit2)(strm, wbits)inflateInit2_((strm), (wbits), "1.3.1", (int)sizeof(z_stream) ); | |||
| 81 | inits_done = 1; | |||
| 82 | if (err != Z_OK0) { | |||
| 83 | ZLIB_PREFIX(inflateEnd)inflateEnd(strm); | |||
| 84 | g_free(strm)(__builtin_object_size ((strm), 0) != ((size_t) - 1)) ? g_free_sized (strm, __builtin_object_size ((strm), 0)) : (g_free) (strm); | |||
| 85 | wmem_free(NULL((void*)0), compr); | |||
| 86 | g_free(strmbuf)(__builtin_object_size ((strmbuf), 0) != ((size_t) - 1)) ? g_free_sized (strmbuf, __builtin_object_size ((strmbuf), 0)) : (g_free) ( strmbuf); | |||
| 87 | return NULL((void*)0); | |||
| 88 | } | |||
| 89 | ||||
| 90 | while (1) { | |||
| 91 | strm->next_out = &strmbuf[bytes_out]; | |||
| 92 | strm->avail_out = bufsiz; | |||
| 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 = bufsiz - strm->avail_out; | |||
| 98 | ||||
| 99 | // This is an unsigned long, but won't overflow even | |||
| 100 | // on platforms where that is 32-bit, because bufsize | |||
| 101 | // is clamped, if we break when it reaches INT_MAX. | |||
| 102 | if (strm->total_out > INT_MAX2147483647) { | |||
| 103 | // Qt uses signed ints in various classes, so | |||
| 104 | // the GUI can't really handle anything | |||
| 105 | ws_debug("overflow, returning what we have")do { if (1) { ws_log_full("Epan", LOG_LEVEL_DEBUG, "epan/tvbuff_zlib.c" , 105, __func__, "overflow, returning what we have"); } } while (0); | |||
| 106 | ZLIB_PREFIX(inflateEnd)inflateEnd(strm); | |||
| 107 | g_free(strm)(__builtin_object_size ((strm), 0) != ((size_t) - 1)) ? g_free_sized (strm, __builtin_object_size ((strm), 0)) : (g_free) (strm); | |||
| 108 | break; | |||
| 109 | } | |||
| 110 | ||||
| 111 | /* Use this for a workaround for bug #6480 | |||
| 112 | * (https://gitlab.com/wireshark/wireshark/-/issues/6480) | |||
| 113 | * When a zero length payload was compressed into 20 bytes | |||
| 114 | * bytes we want to return a tvb with 0 length instead | |||
| 115 | * of NULL. */ | |||
| 116 | ++inflate_passes; | |||
| 117 | ||||
| 118 | bytes_out += bytes_pass; | |||
| 119 | 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", 119, __func__ , "assertion failed: %s", "bytes_out == strm->total_out"); } while (0); | |||
| 120 | ||||
| 121 | if (strm->avail_out == 0) { | |||
| 122 | strmbuf = (uint8_t*)g_realloc(strmbuf, bytes_out + bufsiz); | |||
| 123 | } | |||
| 124 | ||||
| 125 | if (err
| |||
| 126 | ZLIB_PREFIX(inflateEnd)inflateEnd(strm); | |||
| 127 | g_free(strm)(__builtin_object_size ((strm), 0) != ((size_t) - 1)) ? g_free_sized (strm, __builtin_object_size ((strm), 0)) : (g_free) (strm); | |||
| 128 | break; | |||
| 129 | } | |||
| 130 | } else if (err == Z_BUF_ERROR(-5)) { | |||
| 131 | /* | |||
| 132 | * It's possible that not enough frames were captured | |||
| 133 | * to decompress this fully, so return what we've done | |||
| 134 | * so far, if any. | |||
| 135 | */ | |||
| 136 | ZLIB_PREFIX(inflateEnd)inflateEnd(strm); | |||
| 137 | g_free(strm)(__builtin_object_size ((strm), 0) != ((size_t) - 1)) ? g_free_sized (strm, __builtin_object_size ((strm), 0)) : (g_free) (strm); | |||
| 138 | ||||
| 139 | if (inflate_passes) { | |||
| 140 | break; | |||
| 141 | } else { | |||
| 142 | wmem_free(NULL((void*)0), compr); | |||
| 143 | return NULL((void*)0); | |||
| 144 | } | |||
| 145 | ||||
| 146 | } else if (err == Z_DATA_ERROR(-3) && inits_done == 1 | |||
| 147 | && inflate_passes == 0 && comprlen >= 2 && | |||
| 148 | (*compr == 0x1f) && (*(compr + 1) == 0x8b)) { | |||
| 149 | /* | |||
| 150 | * inflate() is supposed to handle both gzip and deflate | |||
| 151 | * streams automatically, but in reality it doesn't | |||
| 152 | * seem to handle either (at least not within the | |||
| 153 | * context of an HTTP response.) We have to try | |||
| 154 | * several tweaks, depending on the type of data and | |||
| 155 | * version of the library installed. | |||
| 156 | */ | |||
| 157 | ||||
| 158 | /* | |||
| 159 | * Gzip file format. Skip past the header, since the | |||
| 160 | * fix to make it work (setting windowBits to 31) | |||
| 161 | * doesn't work with all versions of the library. | |||
| 162 | */ | |||
| 163 | Bytef *c = compr + 2; | |||
| 164 | Bytef flags = 0; | |||
| 165 | ||||
| 166 | /* we read two bytes already (0x1f, 0x8b) and | |||
| 167 | need at least Z_DEFLATED, 1 byte flags, 4 | |||
| 168 | bytes MTIME, 1 byte XFL, 1 byte OS */ | |||
| 169 | if (comprlen < 10 || *c != Z_DEFLATED8) { | |||
| 170 | ZLIB_PREFIX(inflateEnd)inflateEnd(strm); | |||
| 171 | g_free(strm)(__builtin_object_size ((strm), 0) != ((size_t) - 1)) ? g_free_sized (strm, __builtin_object_size ((strm), 0)) : (g_free) (strm); | |||
| 172 | wmem_free(NULL((void*)0), compr); | |||
| 173 | g_free(strmbuf)(__builtin_object_size ((strmbuf), 0) != ((size_t) - 1)) ? g_free_sized (strmbuf, __builtin_object_size ((strmbuf), 0)) : (g_free) ( strmbuf); | |||
| 174 | return NULL((void*)0); | |||
| 175 | } | |||
| 176 | ||||
| 177 | c++; | |||
| 178 | flags = *c; | |||
| 179 | c++; | |||
| 180 | ||||
| 181 | /* Skip past the MTIME (4 bytes), | |||
| 182 | XFL, and OS fields (1 byte each). */ | |||
| 183 | c += 6; | |||
| 184 | ||||
| 185 | if (flags & (1 << 2)) { | |||
| 186 | /* An Extra field is present. It | |||
| 187 | consists of 2 bytes xsize and xsize | |||
| 188 | bytes of data. | |||
| 189 | Read byte-by-byte (least significant | |||
| 190 | byte first) to make sure we abort | |||
| 191 | cleanly when the xsize is truncated | |||
| 192 | after the first byte. */ | |||
| 193 | uint16_t xsize = 0; | |||
| 194 | ||||
| 195 | if ((unsigned)(c-compr) < comprlen) { | |||
| 196 | xsize += *c; | |||
| 197 | c++; | |||
| 198 | } | |||
| 199 | if ((unsigned)(c-compr) < comprlen) { | |||
| 200 | xsize += *c << 8; | |||
| 201 | c++; | |||
| 202 | } | |||
| 203 | ||||
| 204 | c += xsize; | |||
| 205 | } | |||
| 206 | ||||
| 207 | if (flags & (1 << 3)) { | |||
| 208 | /* A null terminated filename */ | |||
| 209 | ||||
| 210 | while ((unsigned)(c - compr) < comprlen && *c != '\0') { | |||
| 211 | c++; | |||
| 212 | } | |||
| 213 | ||||
| 214 | c++; | |||
| 215 | } | |||
| 216 | ||||
| 217 | if (flags & (1 << 4)) { | |||
| 218 | /* A null terminated comment */ | |||
| 219 | ||||
| 220 | while ((unsigned)(c - compr) < comprlen && *c != '\0') { | |||
| 221 | c++; | |||
| 222 | } | |||
| 223 | ||||
| 224 | c++; | |||
| 225 | } | |||
| 226 | ||||
| 227 | ||||
| 228 | if ((unsigned)(c - compr) > comprlen) { | |||
| 229 | ZLIB_PREFIX(inflateEnd)inflateEnd(strm); | |||
| 230 | g_free(strm)(__builtin_object_size ((strm), 0) != ((size_t) - 1)) ? g_free_sized (strm, __builtin_object_size ((strm), 0)) : (g_free) (strm); | |||
| 231 | wmem_free(NULL((void*)0), compr); | |||
| 232 | g_free(strmbuf)(__builtin_object_size ((strmbuf), 0) != ((size_t) - 1)) ? g_free_sized (strmbuf, __builtin_object_size ((strmbuf), 0)) : (g_free) ( strmbuf); | |||
| 233 | return NULL((void*)0); | |||
| 234 | } | |||
| 235 | /* Drop gzip header */ | |||
| 236 | comprlen -= (unsigned)(c - compr); | |||
| 237 | next = c; | |||
| 238 | ||||
| 239 | ZLIB_PREFIX(inflateReset)inflateReset(strm); | |||
| 240 | strm->next_in = next; | |||
| 241 | strm->avail_in = comprlen; | |||
| 242 | ||||
| 243 | ZLIB_PREFIX(inflateEnd)inflateEnd(strm); | |||
| 244 | err = ZLIB_PREFIX(inflateInit2)(strm, wbits)inflateInit2_((strm), (wbits), "1.3.1", (int)sizeof(z_stream) ); | |||
| 245 | inits_done++; | |||
| 246 | if (err != Z_OK0) { | |||
| 247 | g_free(strm)(__builtin_object_size ((strm), 0) != ((size_t) - 1)) ? g_free_sized (strm, __builtin_object_size ((strm), 0)) : (g_free) (strm); | |||
| 248 | wmem_free(NULL((void*)0), compr); | |||
| 249 | g_free(strmbuf)(__builtin_object_size ((strmbuf), 0) != ((size_t) - 1)) ? g_free_sized (strmbuf, __builtin_object_size ((strmbuf), 0)) : (g_free) ( strmbuf); | |||
| 250 | return NULL((void*)0); | |||
| 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 | g_free(strm)(__builtin_object_size ((strm), 0) != ((size_t) - 1)) ? g_free_sized (strm, __builtin_object_size ((strm), 0)) : (g_free) (strm); | |||
| 279 | g_free(strmbuf)(__builtin_object_size ((strmbuf), 0) != ((size_t) - 1)) ? g_free_sized (strmbuf, __builtin_object_size ((strmbuf), 0)) : (g_free) ( strmbuf); | |||
| 280 | wmem_free(NULL((void*)0), compr); | |||
| 281 | ||||
| 282 | return NULL((void*)0); | |||
| 283 | } | |||
| 284 | } else { | |||
| 285 | ZLIB_PREFIX(inflateEnd)inflateEnd(strm); | |||
| 286 | g_free(strm)(__builtin_object_size ((strm), 0) != ((size_t) - 1)) ? g_free_sized (strm, __builtin_object_size ((strm), 0)) : (g_free) (strm); | |||
| 287 | g_free(strmbuf)(__builtin_object_size ((strmbuf), 0) != ((size_t) - 1)) ? g_free_sized (strmbuf, __builtin_object_size ((strmbuf), 0)) : (g_free) ( strmbuf); | |||
| 288 | ||||
| 289 | if (!inflate_passes
| |||
| 290 | wmem_free(NULL((void*)0), compr); | |||
| 291 | return NULL((void*)0); | |||
| 292 | } | |||
| 293 | ||||
| 294 | break; | |||
| 295 | } | |||
| 296 | } | |||
| 297 | ||||
| 298 | ws_debug("inflate() total passes: %u\n", inflate_passes)do { if (1) { ws_log_full("Epan", LOG_LEVEL_DEBUG, "epan/tvbuff_zlib.c" , 298, __func__, "inflate() total passes: %u\n", inflate_passes ); } } while (0); | |||
| 299 | 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" , 299, __func__, "bytes in: %u\nbytes out: %u\n\n", bytes_in , bytes_out); } } while (0); | |||
| 300 | ||||
| 301 | if (inflate_passes
| |||
| 302 | /* g_realloc(..., 0) is well-defined, returns NULL. */ | |||
| 303 | strmbuf = (uint8_t*)g_realloc(strmbuf, bytes_out); | |||
| ||||
| 304 | uncompr_tvb = tvb_new_real_data(strmbuf, bytes_out, bytes_out); | |||
| 305 | tvb_set_free_cb(uncompr_tvb, g_free); | |||
| 306 | } | |||
| 307 | wmem_free(NULL((void*)0), compr); | |||
| 308 | return uncompr_tvb; | |||
| 309 | } | |||
| 310 | #else /* USE_ZLIB_OR_ZLIBNG */ | |||
| 311 | tvbuff_t * | |||
| 312 | tvb_uncompress_zlib(tvbuff_t *tvb _U___attribute__((unused)), const unsigned offset _U___attribute__((unused)), unsigned comprlen _U___attribute__((unused))) | |||
| 313 | { | |||
| 314 | return NULL((void*)0); | |||
| 315 | } | |||
| 316 | #endif /* USE_ZLIB_OR_ZLIBNG */ | |||
| 317 | ||||
| 318 | tvbuff_t * | |||
| 319 | tvb_child_uncompress_zlib(tvbuff_t *parent, tvbuff_t *tvb, const unsigned offset, unsigned comprlen) | |||
| 320 | { | |||
| 321 | tvbuff_t *new_tvb = tvb_uncompress_zlib(tvb, offset, comprlen); | |||
| 322 | if (new_tvb) | |||
| 323 | tvb_set_child_real_data_tvbuff (parent, new_tvb); | |||
| 324 | return new_tvb; | |||
| 325 | } | |||
| 326 | ||||
| 327 | /* | |||
| 328 | * Editor modelines - https://www.wireshark.org/tools/modelines.html | |||
| 329 | * | |||
| 330 | * Local variables: | |||
| 331 | * c-basic-offset: 8 | |||
| 332 | * tab-width: 8 | |||
| 333 | * indent-tabs-mode: t | |||
| 334 | * End: | |||
| 335 | * | |||
| 336 | * vi: set shiftwidth=8 tabstop=8 noexpandtab: | |||
| 337 | * :indentSize=8:tabSize=8:noTabs=false: | |||
| 338 | */ |