1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
/* tvbuff_composite.c
*
* Copyright (c) 2000 by Gilbert Ramirez <gram@alumni.rice.edu>
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <gerald@wireshark.org>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "config.h"
#include "tvbuff.h"
#include "tvbuff-int.h"
#include "proto.h" /* XXX - only used for DISSECTOR_ASSERT, probably a new header file? */
typedef struct {
GQueue *tvbs;
/* Used for quick testing to see if this
* is the tvbuff that a COMPOSITE is
* interested in. */
unsigned *start_offsets;
unsigned *end_offsets;
} tvb_comp_t;
struct tvb_composite {
struct tvbuff tvb;
tvb_comp_t composite;
};
static void
composite_free(tvbuff_t *tvb)
{
struct tvb_composite *composite_tvb = (struct tvb_composite *) tvb;
tvb_comp_t *composite = &composite_tvb->composite;
g_queue_free(composite->tvbs);
g_free(composite->start_offsets);
g_free(composite->end_offsets);
g_free((void *)tvb->real_data);
}
static unsigned
composite_offset(const tvbuff_t *tvb _U_, const unsigned counter)
{
return counter;
}
static const uint8_t*
composite_get_ptr(tvbuff_t *tvb, unsigned abs_offset, unsigned abs_length)
{
struct tvb_composite *composite_tvb = (struct tvb_composite *) tvb;
unsigned i;
tvb_comp_t *composite;
tvbuff_t *member_tvb = NULL;
unsigned member_offset;
/* DISSECTOR_ASSERT(tvb->ops == &tvb_composite_ops); */
/* Maybe the range specified by offset/length
* is contiguous inside one of the member tvbuffs */
composite = &composite_tvb->composite;
GList *item = (GList*)composite->tvbs->head;
for (i = 0; i < g_queue_get_length(composite->tvbs); i++, item=item->next) {
if (abs_offset <= composite->end_offsets[i]) {
member_tvb = (tvbuff_t *)item->data;
break;
}
}
/* special case */
if (!member_tvb) {
DISSECTOR_ASSERT(abs_offset == tvb->length && abs_length == 0);
return "";
}
member_offset = abs_offset - composite->start_offsets[i];
if (tvb_bytes_exist(member_tvb, member_offset, abs_length)) {
/*
* The range is, in fact, contiguous within member_tvb.
*/
DISSECTOR_ASSERT(!tvb->real_data);
return tvb_get_ptr(member_tvb, member_offset, abs_length);
}
else {
/* Use a temporary variable as tvb_memcpy is also checking tvb->real_data pointer */
void *real_data = g_malloc(tvb->length);
tvb_memcpy(tvb, real_data, 0, tvb->length);
tvb->real_data = (const uint8_t *)real_data;
return tvb->real_data + abs_offset;
}
DISSECTOR_ASSERT_NOT_REACHED();
}
static void *
composite_memcpy(tvbuff_t *tvb, void* _target, unsigned abs_offset, unsigned abs_length)
{
struct tvb_composite *composite_tvb = (struct tvb_composite *) tvb;
uint8_t *target = (uint8_t *) _target;
unsigned i;
tvb_comp_t *composite;
tvbuff_t *member_tvb = NULL;
unsigned member_offset, member_length;
/* DISSECTOR_ASSERT(tvb->ops == &tvb_composite_ops); */
/* Maybe the range specified by offset/length
* is contiguous inside one of the member tvbuffs */
composite = &composite_tvb->composite;
GList *item = (GList*)composite->tvbs->head;
for (i = 0; i < g_queue_get_length(composite->tvbs); i++, item=item->next) {
if (abs_offset <= composite->end_offsets[i]) {
member_tvb = (tvbuff_t *)item->data;
break;
}
}
/* special case */
if (!member_tvb) {
DISSECTOR_ASSERT(abs_offset == tvb->length && abs_length == 0);
return target;
}
member_offset = abs_offset - composite->start_offsets[i];
if (tvb_bytes_exist(member_tvb, member_offset, abs_length)) {
DISSECTOR_ASSERT(!tvb->real_data);
return tvb_memcpy(member_tvb, target, member_offset, abs_length);
}
else {
/* The requested data is non-contiguous inside
* the member tvb. We have to memcpy() the part that's in the member tvb,
* then iterate across the other member tvb's, copying their portions
* until we have copied all data.
*/
member_length = tvb_captured_length_remaining(member_tvb, member_offset);
/* composite_memcpy() can't handle a member_length of zero. */
DISSECTOR_ASSERT(member_length > 0);
tvb_memcpy(member_tvb, target, member_offset, member_length);
abs_offset += member_length;
abs_length -= member_length;
/* Recurse */
if (abs_length > 0) {
composite_memcpy(tvb, target + member_length, abs_offset, abs_length);
}
return target;
}
DISSECTOR_ASSERT_NOT_REACHED();
}
static const struct tvb_ops tvb_composite_ops = {
sizeof(struct tvb_composite), /* size */
composite_free, /* free */
composite_offset, /* offset */
composite_get_ptr, /* get_ptr */
composite_memcpy, /* memcpy */
NULL, /* find_uint8 XXX */
NULL, /* pbrk_uint8 XXX */
NULL, /* clone */
};
/*
* Composite tvb
*
* A composite TVB references the concatenation of one or more TVBs, each of
* them MUST be part of the same chain (the same memory "scope"). The
* caller of tvb_new_composite MUST immediately call tvb_composite_append or
* tvb_composite_prepend to ensure that the composite TVB is properly freed as
* needed.
*
* Failure to satisfy the same chain requirement can result in memory-safety
* issues such as use-after-free or double-free.
*/
tvbuff_t *
tvb_new_composite(void)
{
tvbuff_t *tvb = tvb_new(&tvb_composite_ops);
struct tvb_composite *composite_tvb = (struct tvb_composite *) tvb;
tvb_comp_t *composite = &composite_tvb->composite;
composite->tvbs = g_queue_new();
composite->start_offsets = NULL;
composite->end_offsets = NULL;
return tvb;
}
void
tvb_composite_append(tvbuff_t *tvb, tvbuff_t *member)
{
struct tvb_composite *composite_tvb = (struct tvb_composite *) tvb;
tvb_comp_t *composite;
DISSECTOR_ASSERT(tvb && !tvb->initialized);
DISSECTOR_ASSERT(tvb->ops == &tvb_composite_ops);
/* Don't allow zero-length TVBs: composite_memcpy() can't handle them
* and anyway it makes no sense.
*/
if (member && member->length) {
composite = &composite_tvb->composite;
g_queue_push_tail(composite->tvbs, member);
/* Attach the composite TVB to the first TVB only. */
if (g_queue_get_length(composite->tvbs) == 1) {
tvb_add_to_chain((tvbuff_t *)g_queue_peek_head(composite->tvbs), tvb);
}
}
}
void
tvb_composite_prepend(tvbuff_t *tvb, tvbuff_t *member)
{
struct tvb_composite *composite_tvb = (struct tvb_composite *) tvb;
tvb_comp_t *composite;
DISSECTOR_ASSERT(tvb && !tvb->initialized);
DISSECTOR_ASSERT(tvb->ops == &tvb_composite_ops);
/* Don't allow zero-length TVBs: composite_memcpy() can't handle them
* and anyway it makes no sense.
*/
if (member && member->length) {
composite = &composite_tvb->composite;
g_queue_push_head(composite->tvbs, member);
/* Attach the composite TVB to the first TVB only. */
if (g_queue_get_length(composite->tvbs) == 1) {
tvb_add_to_chain((tvbuff_t *)g_queue_peek_head(composite->tvbs), tvb);
}
}
}
void
tvb_composite_finalize(tvbuff_t *tvb)
{
struct tvb_composite *composite_tvb = (struct tvb_composite *) tvb;
unsigned num_members;
tvbuff_t *member_tvb;
tvb_comp_t *composite;
unsigned i;
DISSECTOR_ASSERT(tvb && !tvb->initialized);
DISSECTOR_ASSERT(tvb->ops == &tvb_composite_ops);
DISSECTOR_ASSERT(tvb->length == 0);
DISSECTOR_ASSERT(tvb->reported_length == 0);
DISSECTOR_ASSERT(tvb->contained_length == 0);
composite = &composite_tvb->composite;
num_members = g_queue_get_length(composite->tvbs);
/* Dissectors should not create composite TVBs if they're not going to
* put at least one TVB in them.
* (Without this check--or something similar--we'll seg-fault below.)
*/
DISSECTOR_ASSERT(num_members);
composite->start_offsets = g_new(unsigned, num_members);
composite->end_offsets = g_new(unsigned, num_members);
GList *item = (GList*)composite->tvbs->head;
for (i=0; i < num_members; i++, item=item->next) {
member_tvb = (tvbuff_t *)item->data;
composite->start_offsets[i] = tvb->length;
tvb->length += member_tvb->length;
tvb->reported_length += member_tvb->reported_length;
tvb->contained_length += member_tvb->contained_length;
composite->end_offsets[i] = tvb->length - 1;
}
tvb->initialized = true;
tvb->ds_tvb = tvb;
}
/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*
* Local variables:
* c-basic-offset: 8
* tab-width: 8
* indent-tabs-mode: t
* End:
*
* vi: set shiftwidth=8 tabstop=8 noexpandtab:
* :indentSize=8:tabSize=8:noTabs=false:
*/
|