| File: | builds/wireshark/wireshark/epan/tvbuff_lz77.c |
| Warning: | line 158, column 7 Potential leak of memory pointed to by 'p' |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | /* | |||
| 2 | * Decompression code for Plain LZ77. This encoding is used by | |||
| 3 | * Microsoft in various file formats and protocols including SMB3. | |||
| 4 | * | |||
| 5 | * See MS-XCA. | |||
| 6 | * | |||
| 7 | * Copyright (C) 2019 Aurélien Aptel | |||
| 8 | * | |||
| 9 | * SPDX-License-Identifier: GPL-2.0-or-later | |||
| 10 | */ | |||
| 11 | ||||
| 12 | #include <glib.h> | |||
| 13 | #include <epan/exceptions.h> | |||
| 14 | #include <epan/tvbuff.h> | |||
| 15 | #include <epan/wmem_scopes.h> | |||
| 16 | ||||
| 17 | #define MAX_INPUT_SIZE(16*1024*1024) (16*1024*1024) /* 16MB */ | |||
| 18 | #define MAX_OUTPUT_SIZE(16*1024*1024) (16*1024*1024) /* 16MB */ | |||
| 19 | ||||
| 20 | static bool_Bool do_uncompress(tvbuff_t *tvb, int offset, int in_size, | |||
| 21 | wmem_array_t *obuf) | |||
| 22 | { | |||
| 23 | unsigned buf_flags = 0, buf_flag_count = 0; | |||
| 24 | int in_off = 0; | |||
| 25 | int last_length_half_byte = 0; | |||
| 26 | unsigned match_bytes, match_len, match_off; | |||
| 27 | unsigned i; | |||
| 28 | ||||
| 29 | while (1) { | |||
| 30 | if (buf_flag_count == 0) { | |||
| 31 | buf_flags = tvb_get_letohl(tvb, offset+in_off); | |||
| 32 | in_off += 4; | |||
| 33 | buf_flag_count = 32; | |||
| 34 | } | |||
| 35 | buf_flag_count--; | |||
| 36 | if ((buf_flags & (1u << buf_flag_count)) == 0) { | |||
| 37 | /* A literal must be inside the compressed data. */ | |||
| 38 | if (in_off >= in_size) | |||
| 39 | return false0; | |||
| 40 | if (wmem_array_get_count(obuf) >= MAX_OUTPUT_SIZE(16*1024*1024)) | |||
| 41 | return false0; | |||
| 42 | uint8_t v = tvb_get_uint8(tvb, offset+in_off); | |||
| 43 | wmem_array_append_one(obuf, v)wmem_array_append((obuf), &(v), 1); | |||
| 44 | in_off++; | |||
| 45 | } else { | |||
| 46 | if (in_off == in_size) | |||
| 47 | return true1; | |||
| 48 | match_bytes = tvb_get_letohs(tvb, offset+in_off); | |||
| 49 | in_off += 2; | |||
| 50 | match_len = match_bytes % 8; | |||
| 51 | match_off = (match_bytes/8) + 1; | |||
| 52 | if (match_len == 7) { | |||
| 53 | if (last_length_half_byte == 0) { | |||
| 54 | match_len = tvb_get_uint8(tvb, offset+in_off); | |||
| 55 | match_len = match_len % 16; | |||
| 56 | last_length_half_byte = in_off; | |||
| 57 | in_off++; | |||
| 58 | } else { | |||
| 59 | match_len = tvb_get_uint8(tvb, offset+last_length_half_byte); | |||
| 60 | match_len = match_len / 16; | |||
| 61 | last_length_half_byte = 0; | |||
| 62 | } | |||
| 63 | if (match_len == 15) { | |||
| 64 | match_len = tvb_get_uint8(tvb, offset+in_off); | |||
| 65 | in_off++; | |||
| 66 | if (match_len == 255) { | |||
| 67 | match_len = tvb_get_letohs(tvb, offset+in_off); | |||
| 68 | in_off += 2; | |||
| 69 | if (match_len == 0) { | |||
| 70 | match_len = tvb_get_letohl(tvb, offset+in_off); | |||
| 71 | in_off += 4; | |||
| 72 | } | |||
| 73 | if (match_len < 15+7) | |||
| 74 | return false0; | |||
| 75 | match_len -= (15 + 7); | |||
| 76 | } | |||
| 77 | match_len += 15; | |||
| 78 | } | |||
| 79 | match_len += 7; | |||
| 80 | } | |||
| 81 | /* Each match can be up to 4 GB long, so limit the | |||
| 82 | * whole output, not just one match: a few octets of | |||
| 83 | * matches could otherwise expand to gigabytes. | |||
| 84 | * XXX - We could instead return what we could | |||
| 85 | * decompress in a tvb with reported length greater | |||
| 86 | * than captured length. */ | |||
| 87 | if (match_len > MAX_OUTPUT_SIZE(16*1024*1024)) | |||
| 88 | return false0; | |||
| 89 | match_len += 3; | |||
| 90 | if (match_len > MAX_OUTPUT_SIZE(16*1024*1024) - wmem_array_get_count(obuf)) | |||
| 91 | return false0; | |||
| 92 | /* We can copy up to match_off bytes at a time. | |||
| 93 | * (The overlap handling is *not* like memmove, | |||
| 94 | * see [MS-XCA] 2.4.4.) */ | |||
| 95 | /* wmem_array_get_count only increases, so we only need | |||
| 96 | * test this once. */ | |||
| 97 | if (match_off > wmem_array_get_count(obuf)) | |||
| 98 | return false0; | |||
| 99 | uint8_t *src; | |||
| 100 | ws_assert(match_off != 0)do { if ((1) && !(match_off != 0)) ws_log_fatal_full( "", LOG_LEVEL_ERROR, "epan/tvbuff_lz77.c", 100, __func__, "assertion failed: %s" , "match_off != 0"); } while (0); // Guaranteed by line 45 | |||
| 101 | /* Must call grow first, to realloc the array first | |||
| 102 | * if needed and avoid invalidating pointers returned | |||
| 103 | * from wmem_array_index when appending. */ | |||
| 104 | if (!wmem_array_grow(obuf, match_len)) | |||
| 105 | return false0; | |||
| 106 | for (i = 0; i < match_len / match_off; i++) { | |||
| 107 | src = wmem_array_index(obuf, wmem_array_get_count(obuf) - match_off); | |||
| 108 | wmem_array_append(obuf, src, match_off); | |||
| 109 | } | |||
| 110 | for (i *= match_off; i < match_len; i++) { | |||
| 111 | src = wmem_array_index(obuf, wmem_array_get_count(obuf) - match_off); | |||
| 112 | wmem_array_append(obuf, src, 1); | |||
| 113 | } | |||
| 114 | } | |||
| 115 | } | |||
| 116 | ||||
| 117 | return true1; | |||
| 118 | } | |||
| 119 | ||||
| 120 | tvbuff_t * | |||
| 121 | tvb_uncompress_lz77(tvbuff_t *tvb, const unsigned offset, unsigned in_size) | |||
| 122 | { | |||
| 123 | volatile bool_Bool ok = false0; | |||
| 124 | wmem_allocator_t *pool; | |||
| 125 | wmem_array_t *obuf; | |||
| 126 | tvbuff_t *out; | |||
| 127 | ||||
| 128 | if (!tvb) | |||
| 129 | return NULL((void*)0); | |||
| 130 | ||||
| 131 | if (!in_size || in_size > MAX_INPUT_SIZE(16*1024*1024)) | |||
| 132 | return NULL((void*)0); | |||
| 133 | ||||
| 134 | pool = wmem_allocator_new(WMEM_ALLOCATOR_SIMPLE); | |||
| 135 | obuf = wmem_array_sized_new(pool, 1, in_size*2); | |||
| 136 | ||||
| 137 | TRY{ except_t *volatile exc; volatile int except_state = 0; static const except_id_t catch_spec[] = { { 1, 0 } }; { struct except_stacknode except_sn; struct except_catch except_ch; except_setup_try(& except_sn, &except_ch, catch_spec, 1); if (_setjmp (except_ch .except_jmp)) *(&exc) = &except_ch.except_obj; else * (&exc) = 0; if(except_state & 1) except_state |= 2; except_state &= ~1; if (except_state == 0 && exc == 0) { | |||
| 138 | ok = do_uncompress(tvb, offset, in_size, obuf); | |||
| 139 | } CATCH_ALLif (except_state == 0 && exc != 0 && (except_state |=1)) { | |||
| 140 | ok = false0; | |||
| 141 | } | |||
| 142 | ENDTRYif(!(except_state&1) && exc != 0) except_rethrow( exc); except_free(except_ch.except_obj.except_dyndata); except_pop (); };}; | |||
| 143 | ||||
| 144 | if (ok) { | |||
| 145 | /* | |||
| 146 | * Cannot pass a tvb free callback that frees the wmem | |||
| 147 | * pool, so we make an extra copy that uses bare | |||
| 148 | * pointers. This could be optimized if tvb API had a | |||
| 149 | * free pool callback of some sort. | |||
| 150 | * | |||
| 151 | * XXX - Maybe a tvb_set_free_cb_with_data or similar | |||
| 152 | * that takes functions that take a void* userdata | |||
| 153 | * parameter? | |||
| 154 | */ | |||
| 155 | unsigned size = wmem_array_get_count(obuf); | |||
| 156 | uint8_t *p = (uint8_t *)g_malloc(size); | |||
| 157 | memcpy(p, wmem_array_get_raw(obuf), size); | |||
| 158 | out = tvb_new_real_data(p, size, size); | |||
| ||||
| 159 | tvb_set_free_cb(out, g_free); | |||
| 160 | } else { | |||
| 161 | out = NULL((void*)0); | |||
| 162 | } | |||
| 163 | ||||
| 164 | wmem_destroy_allocator(pool); | |||
| 165 | ||||
| 166 | return out; | |||
| 167 | } | |||
| 168 | ||||
| 169 | tvbuff_t * | |||
| 170 | tvb_child_uncompress_lz77(tvbuff_t *parent, tvbuff_t *tvb, const unsigned offset, unsigned in_size) | |||
| 171 | { | |||
| 172 | tvbuff_t *new_tvb = tvb_uncompress_lz77(tvb, offset, in_size); | |||
| ||||
| 173 | if (new_tvb) | |||
| 174 | tvb_set_child_real_data_tvbuff(parent, new_tvb); | |||
| 175 | return new_tvb; | |||
| 176 | } | |||
| 177 | ||||
| 178 | ||||
| 179 | /* | |||
| 180 | * Editor modelines - https://www.wireshark.org/tools/modelines.html | |||
| 181 | * | |||
| 182 | * Local variables: | |||
| 183 | * c-basic-offset: 8 | |||
| 184 | * tab-width: 8 | |||
| 185 | * indent-tabs-mode: t | |||
| 186 | * End: | |||
| 187 | * | |||
| 188 | * vi: set shiftwidth=8 tabstop=8 noexpandtab: | |||
| 189 | * :indentSize=8:tabSize=8:noTabs=false: | |||
| 190 | */ |