| File: | builds/wireshark/wireshark/epan/tvbuff_lz77.c |
| Warning: | line 136, 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 | ||||
| 19 | static bool_Bool do_uncompress(tvbuff_t *tvb, int offset, int in_size, | |||
| 20 | wmem_array_t *obuf) | |||
| 21 | { | |||
| 22 | unsigned buf_flags = 0, buf_flag_count = 0; | |||
| 23 | int in_off = 0; | |||
| 24 | int last_length_half_byte = 0; | |||
| 25 | unsigned match_bytes, match_len, match_off; | |||
| 26 | unsigned i; | |||
| 27 | ||||
| 28 | while (1) { | |||
| 29 | if (buf_flag_count == 0) { | |||
| 30 | buf_flags = tvb_get_letohl(tvb, offset+in_off); | |||
| 31 | in_off += 4; | |||
| 32 | buf_flag_count = 32; | |||
| 33 | } | |||
| 34 | buf_flag_count--; | |||
| 35 | if ((buf_flags & (1u << buf_flag_count)) == 0) { | |||
| 36 | uint8_t v = tvb_get_uint8(tvb, offset+in_off); | |||
| 37 | wmem_array_append_one(obuf, v)wmem_array_append((obuf), &(v), 1); | |||
| 38 | in_off++; | |||
| 39 | } else { | |||
| 40 | if (in_off == in_size) | |||
| 41 | return true1; | |||
| 42 | match_bytes = tvb_get_letohs(tvb, offset+in_off); | |||
| 43 | in_off += 2; | |||
| 44 | match_len = match_bytes % 8; | |||
| 45 | match_off = (match_bytes/8) + 1; | |||
| 46 | if (match_len == 7) { | |||
| 47 | if (last_length_half_byte == 0) { | |||
| 48 | match_len = tvb_get_uint8(tvb, offset+in_off); | |||
| 49 | match_len = match_len % 16; | |||
| 50 | last_length_half_byte = in_off; | |||
| 51 | in_off++; | |||
| 52 | } else { | |||
| 53 | match_len = tvb_get_uint8(tvb, offset+last_length_half_byte); | |||
| 54 | match_len = match_len / 16; | |||
| 55 | last_length_half_byte = 0; | |||
| 56 | } | |||
| 57 | if (match_len == 15) { | |||
| 58 | match_len = tvb_get_uint8(tvb, offset+in_off); | |||
| 59 | in_off++; | |||
| 60 | if (match_len == 255) { | |||
| 61 | match_len = tvb_get_letohs(tvb, offset+in_off); | |||
| 62 | in_off += 2; | |||
| 63 | if (match_len == 0) { | |||
| 64 | match_len = tvb_get_letohl(tvb, offset+in_off); | |||
| 65 | in_off += 4; | |||
| 66 | } | |||
| 67 | if (match_len < 15+7) | |||
| 68 | return false0; | |||
| 69 | match_len -= (15 + 7); | |||
| 70 | } | |||
| 71 | match_len += 15; | |||
| 72 | } | |||
| 73 | match_len += 7; | |||
| 74 | } | |||
| 75 | if (match_len > MAX_INPUT_SIZE(16*1024*1024)) | |||
| 76 | return false0; | |||
| 77 | match_len += 3; | |||
| 78 | /* It is tempting to use memmove and/or check if | |||
| 79 | * match_len > match_off and fail, but it is allowed | |||
| 80 | * and is why the 1 byte at a time loop is used. | |||
| 81 | * See [MS-XCA] 2.4.4. [There still are some likely | |||
| 82 | * possible optimizations to copy several bytes at | |||
| 83 | * a time.] */ | |||
| 84 | for (i = 0; i < match_len; i++) { | |||
| 85 | uint8_t byte; | |||
| 86 | if (match_off > wmem_array_get_count(obuf)) | |||
| 87 | return false0; | |||
| 88 | if (wmem_array_try_index(obuf, wmem_array_get_count(obuf)-match_off, &byte)) | |||
| 89 | return false0; | |||
| 90 | wmem_array_append_one(obuf, byte)wmem_array_append((obuf), &(byte), 1); | |||
| 91 | } | |||
| 92 | } | |||
| 93 | } | |||
| 94 | ||||
| 95 | return true1; | |||
| 96 | } | |||
| 97 | ||||
| 98 | tvbuff_t * | |||
| 99 | tvb_uncompress_lz77(tvbuff_t *tvb, const unsigned offset, unsigned in_size) | |||
| 100 | { | |||
| 101 | volatile bool_Bool ok = false0; | |||
| 102 | wmem_allocator_t *pool; | |||
| 103 | wmem_array_t *obuf; | |||
| 104 | tvbuff_t *out; | |||
| 105 | ||||
| 106 | if (!tvb) | |||
| 107 | return NULL((void*)0); | |||
| 108 | ||||
| 109 | if (!in_size || in_size > MAX_INPUT_SIZE(16*1024*1024)) | |||
| 110 | return NULL((void*)0); | |||
| 111 | ||||
| 112 | pool = wmem_allocator_new(WMEM_ALLOCATOR_SIMPLE); | |||
| 113 | obuf = wmem_array_sized_new(pool, 1, in_size*2); | |||
| 114 | ||||
| 115 | 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) { | |||
| 116 | ok = do_uncompress(tvb, offset, in_size, obuf); | |||
| 117 | } CATCH_ALLif (except_state == 0 && exc != 0 && (except_state |=1)) { | |||
| 118 | ok = false0; | |||
| 119 | } | |||
| 120 | ENDTRYif(!(except_state&1) && exc != 0) except_rethrow( exc); except_free(except_ch.except_obj.except_dyndata); except_pop (); };}; | |||
| 121 | ||||
| 122 | if (ok) { | |||
| 123 | /* | |||
| 124 | * Cannot pass a tvb free callback that frees the wmem | |||
| 125 | * pool, so we make an extra copy that uses bare | |||
| 126 | * pointers. This could be optimized if tvb API had a | |||
| 127 | * free pool callback of some sort. | |||
| 128 | * | |||
| 129 | * XXX - Maybe a tvb_set_free_cb_with_data or similar | |||
| 130 | * that takes functions that take a void* userdata | |||
| 131 | * parameter? | |||
| 132 | */ | |||
| 133 | unsigned size = wmem_array_get_count(obuf); | |||
| 134 | uint8_t *p = (uint8_t *)g_malloc(size); | |||
| 135 | memcpy(p, wmem_array_get_raw(obuf), size); | |||
| 136 | out = tvb_new_real_data(p, size, size); | |||
| ||||
| 137 | tvb_set_free_cb(out, g_free); | |||
| 138 | } else { | |||
| 139 | out = NULL((void*)0); | |||
| 140 | } | |||
| 141 | ||||
| 142 | wmem_destroy_allocator(pool); | |||
| 143 | ||||
| 144 | return out; | |||
| 145 | } | |||
| 146 | ||||
| 147 | tvbuff_t * | |||
| 148 | tvb_child_uncompress_lz77(tvbuff_t *parent, tvbuff_t *tvb, const unsigned offset, unsigned in_size) | |||
| 149 | { | |||
| 150 | tvbuff_t *new_tvb = tvb_uncompress_lz77(tvb, offset, in_size); | |||
| ||||
| 151 | if (new_tvb) | |||
| 152 | tvb_set_child_real_data_tvbuff(parent, new_tvb); | |||
| 153 | return new_tvb; | |||
| 154 | } | |||
| 155 | ||||
| 156 | ||||
| 157 | /* | |||
| 158 | * Editor modelines - https://www.wireshark.org/tools/modelines.html | |||
| 159 | * | |||
| 160 | * Local variables: | |||
| 161 | * c-basic-offset: 8 | |||
| 162 | * tab-width: 8 | |||
| 163 | * indent-tabs-mode: t | |||
| 164 | * End: | |||
| 165 | * | |||
| 166 | * vi: set shiftwidth=8 tabstop=8 noexpandtab: | |||
| 167 | * :indentSize=8:tabSize=8:noTabs=false: | |||
| 168 | */ |