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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
|
# Fuzzing functions for qcow2 fields
#
# Copyright (C) 2014 Maria Kustova <maria.k@catit.be>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import random
UINT8 = 0xff
UINT32 = 0xffffffff
UINT64 = 0xffffffffffffffff
# Most significant bit orders
UINT32_M = 31
UINT64_M = 63
# Fuzz vectors
UINT8_V = [0, 0x10, UINT8/4, UINT8/2 - 1, UINT8/2, UINT8/2 + 1, UINT8 - 1,
UINT8]
UINT32_V = [0, 0x100, 0x1000, 0x10000, 0x100000, UINT32/4, UINT32/2 - 1,
UINT32/2, UINT32/2 + 1, UINT32 - 1, UINT32]
UINT64_V = UINT32_V + [0x1000000, 0x10000000, 0x100000000, UINT64/4,
UINT64/2 - 1, UINT64/2, UINT64/2 + 1, UINT64 - 1,
UINT64]
STRING_V = ['%s%p%x%d', '.1024d', '%.2049d', '%p%p%p%p', '%x%x%x%x',
'%d%d%d%d', '%s%s%s%s', '%99999999999s', '%08x', '%%20d', '%%20n',
'%%20x', '%%20s', '%s%s%s%s%s%s%s%s%s%s', '%p%p%p%p%p%p%p%p%p%p',
'%#0123456x%08x%x%s%p%d%n%o%u%c%h%l%q%j%z%Z%t%i%e%g%f%a%C%S%08x%%',
'%s x 129', '%x x 257']
def random_from_intervals(intervals):
"""Select a random integer number from the list of specified intervals.
Each interval is a tuple of lower and upper limits of the interval. The
limits are included. Intervals in a list should not overlap.
"""
total = reduce(lambda x, y: x + y[1] - y[0] + 1, intervals, 0)
r = random.randint(0, total - 1) + intervals[0][0]
for x in zip(intervals, intervals[1:]):
r = r + (r > x[0][1]) * (x[1][0] - x[0][1] - 1)
return r
def random_bits(bit_ranges):
"""Generate random binary mask with ones in the specified bit ranges.
Each bit_ranges is a list of tuples of lower and upper limits of bit
positions will be fuzzed. The limits are included. Random amount of bits
in range limits will be set to ones. The mask is returned in decimal
integer format.
"""
bit_numbers = []
# Select random amount of random positions in bit_ranges
for rng in bit_ranges:
bit_numbers += random.sample(range(rng[0], rng[1] + 1),
random.randint(0, rng[1] - rng[0] + 1))
val = 0
# Set bits on selected positions to ones
for bit in bit_numbers:
val |= 1 << bit
return val
def truncate_string(strings, length):
"""Return strings truncated to specified length."""
if type(strings) == list:
return [s[:length] for s in strings]
else:
return strings[:length]
def validator(current, pick, choices):
"""Return a value not equal to the current selected by the pick
function from choices.
"""
while True:
val = pick(choices)
if not val == current:
return val
def int_validator(current, intervals):
"""Return a random value from intervals not equal to the current.
This function is useful for selection from valid values except current one.
"""
return validator(current, random_from_intervals, intervals)
def bit_validator(current, bit_ranges):
"""Return a random bit mask not equal to the current.
This function is useful for selection from valid values except current one.
"""
return validator(current, random_bits, bit_ranges)
def string_validator(current, strings):
"""Return a random string value from the list not equal to the current.
This function is useful for selection from valid values except current one.
"""
return validator(current, random.choice, strings)
def selector(current, constraints, validate=int_validator):
"""Select one value from all defined by constraints.
Each constraint produces one random value satisfying to it. The function
randomly selects one value satisfying at least one constraint (depending on
constraints overlaps).
"""
def iter_validate(c):
"""Apply validate() only to constraints represented as lists.
This auxiliary function replaces short circuit conditions not supported
in Python 2.4
"""
if type(c) == list:
return validate(current, c)
else:
return c
fuzz_values = [iter_validate(c) for c in constraints]
# Remove current for cases it's implicitly specified in constraints
# Duplicate validator functionality to prevent decreasing of probability
# to get one of allowable values
# TODO: remove validators after implementation of intelligent selection
# of fields will be fuzzed
try:
fuzz_values.remove(current)
except ValueError:
pass
return random.choice(fuzz_values)
def magic(current):
"""Fuzz magic header field.
The function just returns the current magic value and provides uniformity
of calls for all fuzzing functions.
"""
return current
def version(current):
"""Fuzz version header field."""
constraints = UINT32_V + [
[(2, 3)], # correct values
[(0, 1), (4, UINT32)]
]
return selector(current, constraints)
def backing_file_offset(current):
"""Fuzz backing file offset header field."""
constraints = UINT64_V
return selector(current, constraints)
def backing_file_size(current):
"""Fuzz backing file size header field."""
constraints = UINT32_V
return selector(current, constraints)
def cluster_bits(current):
"""Fuzz cluster bits header field."""
constraints = UINT32_V + [
[(9, 20)], # correct values
[(0, 9), (20, UINT32)]
]
return selector(current, constraints)
def size(current):
"""Fuzz image size header field."""
constraints = UINT64_V
return selector(current, constraints)
def crypt_method(current):
"""Fuzz crypt method header field."""
constraints = UINT32_V + [
1,
[(2, UINT32)]
]
return selector(current, constraints)
def l1_size(current):
"""Fuzz L1 table size header field."""
constraints = UINT32_V
return selector(current, constraints)
def l1_table_offset(current):
"""Fuzz L1 table offset header field."""
constraints = UINT64_V
return selector(current, constraints)
def refcount_table_offset(current):
"""Fuzz refcount table offset header field."""
constraints = UINT64_V
return selector(current, constraints)
def refcount_table_clusters(current):
"""Fuzz refcount table clusters header field."""
constraints = UINT32_V
return selector(current, constraints)
def nb_snapshots(current):
"""Fuzz number of snapshots header field."""
constraints = UINT32_V
return selector(current, constraints)
def snapshots_offset(current):
"""Fuzz snapshots offset header field."""
constraints = UINT64_V
return selector(current, constraints)
def incompatible_features(current):
"""Fuzz incompatible features header field."""
constraints = [
[(0, 1)], # allowable values
[(0, UINT64_M)]
]
return selector(current, constraints, bit_validator)
def compatible_features(current):
"""Fuzz compatible features header field."""
constraints = [
[(0, UINT64_M)]
]
return selector(current, constraints, bit_validator)
def autoclear_features(current):
"""Fuzz autoclear features header field."""
constraints = [
[(0, UINT64_M)]
]
return selector(current, constraints, bit_validator)
def refcount_order(current):
"""Fuzz number of refcount order header field."""
constraints = UINT32_V
return selector(current, constraints)
def header_length(current):
"""Fuzz number of refcount order header field."""
constraints = UINT32_V + [
72,
104,
[(0, UINT32)]
]
return selector(current, constraints)
def bf_name(current):
"""Fuzz the backing file name."""
constraints = [
truncate_string(STRING_V, len(current))
]
return selector(current, constraints, string_validator)
def ext_magic(current):
"""Fuzz magic field of a header extension."""
constraints = UINT32_V
return selector(current, constraints)
def ext_length(current):
"""Fuzz length field of a header extension."""
constraints = UINT32_V
return selector(current, constraints)
def bf_format(current):
"""Fuzz backing file format in the corresponding header extension."""
constraints = [
truncate_string(STRING_V, len(current)),
truncate_string(STRING_V, (len(current) + 7) & ~7) # Fuzz padding
]
return selector(current, constraints, string_validator)
def feature_type(current):
"""Fuzz feature type field of a feature name table header extension."""
constraints = UINT8_V
return selector(current, constraints)
def feature_bit_number(current):
"""Fuzz bit number field of a feature name table header extension."""
constraints = UINT8_V
return selector(current, constraints)
def feature_name(current):
"""Fuzz feature name field of a feature name table header extension."""
constraints = [
truncate_string(STRING_V, len(current)),
truncate_string(STRING_V, 46) # Fuzz padding (field length = 46)
]
return selector(current, constraints, string_validator)
|