222
echo "== an unallocated cluster still reads as zeroes =="
223
{ $QEMU_IO -r -c "read -P 0x00 0 $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir
224
225
+# Clear image
226
+_make_test_img $SIZE
227
+
228
+echo "== TEST BAT ENTRY POINTING OUTSIDE IMAGE =="
229
+
230
+echo "== corrupt image: point first cluster far outside the file =="
231
+poke_file_le "$TEST_IMG" $BAT_OFFSET 4 1000000
232
+
233
+echo "== read-only read must return zeroes, not an I/O error =="
234
+{ $QEMU_IO -r -c "read -P 0x00 0 $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir
235
+
236
+echo "== write must allocate a fresh cluster instead of trusting the entry =="
237
+{ $QEMU_IO -c "write -P 0x77 0 $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir
238
+
239
+echo "== file did not grow anywhere near the bogus offset =="
240
+file_size=`stat --printf="%s" "$TEST_IMG"`
241
+if [ "$file_size" -lt $((16 * 1024 * 1024)) ]; then
242
+ echo "file size sane: yes"
243
+else
244
+ echo "file size sane: no ($file_size bytes)"
245
+fi
246
+
247
+echo "== data reads back correctly =="
248
+{ $QEMU_IO -r -c "read -P 0x77 0 $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir
249
+
250
+# Clear image, with a small cluster size so the BAT table itself spans
251
+# more than one cluster and there is room to point before data_off.
252
+_make_test_img -o cluster_size=512 65536
253
+
254
+SMALL_CLUSTER_SIZE=$(peek_file_le $TEST_IMG $CLUSTER_SIZE_OFFSET 4)
255
+SMALL_CLUSTER_SIZE=$((SMALL_CLUSTER_SIZE * 512))
256
+DATA_OFF=$(peek_file_le $TEST_IMG $DATA_OFF_OFFSET 4)
257
+echo "cluster size: $SMALL_CLUSTER_SIZE, data offset (sectors): $DATA_OFF"
258
+
259
+# Cluster index 1 starts at this byte offset, which must be < data_off
260
+# in sectors * 512 for this test to actually exercise the bug.
261
+VICTIM_OFFSET=$SMALL_CLUSTER_SIZE
262
+
263
+echo "== TEST BAT ENTRY POINTING BEFORE DATA AREA =="
264
+
265
+echo "== corrupt image: point first cluster into the BAT table itself =="
266
+poke_file_le "$TEST_IMG" $BAT_OFFSET 4 1
267
+
268
+echo "== qemu-img check detects it without repairing =="
269
+_check_test_img
270
+
271
+echo "== bytes at the victim offset before write =="
272
+echo "$(peek_file_le "$TEST_IMG" $VICTIM_OFFSET 4)"
273
+
274
+echo "== write must allocate a fresh cluster instead of clobbering the BAT =="
275
+{ $QEMU_IO -c "write -P 0x88 0 $SMALL_CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir
276
+
277
+echo "== bytes at the victim offset are unchanged =="
278
+echo "$(peek_file_le "$TEST_IMG" $VICTIM_OFFSET 4)"
279
+
280
+echo "== data reads back correctly =="
281
+{ $QEMU_IO -r -c "read -P 0x88 0 $SMALL_CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir
282
+
283
# success, all done
284
echo "*** done"
285
rm -f $seq.full