2 * virtio-blk Fuzzing Target
4 * Copyright Red Hat Inc., 2020
6 * Based on virtio-scsi-fuzz target.
8 * This work is licensed under the terms of the GNU GPL, version 2 or later.
9 * See the COPYING file in the top-level directory.
12 #include "qemu/osdep.h"
14 #include "tests/qtest/libqtest.h"
15 #include "tests/qtest/libqos/virtio-blk.h"
16 #include "tests/qtest/libqos/virtio.h"
17 #include "tests/qtest/libqos/virtio-pci.h"
18 #include "standard-headers/linux/virtio_ids.h"
19 #include "standard-headers/linux/virtio_pci.h"
20 #include "standard-headers/linux/virtio_blk.h"
24 #define TEST_IMAGE_SIZE (64 * 1024 * 1024)
28 #define MAX_NUM_QUEUES 64
30 /* Based on tests/qtest/virtio-blk-test.c. */
33 QVirtQueue
*vq
[MAX_NUM_QUEUES
+ 2];
36 static QVirtioBlkQueues
*qvirtio_blk_init(QVirtioDevice
*dev
, uint64_t mask
)
41 vs
= g_new0(QVirtioBlkQueues
, 1);
43 features
= qvirtio_get_features(dev
);
45 mask
= ~((1u << VIRTIO_RING_F_INDIRECT_DESC
) |
46 (1u << VIRTIO_RING_F_EVENT_IDX
) |
47 (1u << VIRTIO_BLK_F_SCSI
));
49 mask
|= ~QVIRTIO_F_BAD_FEATURE
;
51 qvirtio_set_features(dev
, features
);
54 vs
->vq
[0] = qvirtqueue_setup(dev
, fuzz_qos_alloc
, 0);
56 qvirtio_set_driver_ok(dev
);
61 static void virtio_blk_fuzz(QTestState
*s
, QVirtioBlkQueues
* queues
,
62 const unsigned char *Data
, size_t Size
)
65 * Data is a sequence of random bytes. We split them up into "actions",
67 * [vqa][dddddddd][vqa][dddd][vqa][dddddddddddd] ...
68 * The length of the data is specified by the preceding vqa.length
70 typedef struct vq_action
{
78 /* Keep track of the free head for each queue we interact with */
79 bool vq_touched
[MAX_NUM_QUEUES
+ 2] = {0};
80 uint32_t free_head
[MAX_NUM_QUEUES
+ 2];
82 QGuestAllocator
*t_alloc
= fuzz_qos_alloc
;
84 QVirtioBlk
*blk
= fuzz_qos_obj
;
85 QVirtioDevice
*dev
= blk
->vdev
;
88 while (Size
>= sizeof(vqa
)) {
89 /* Copy the action, so we can normalize length, queue and flags */
90 memcpy(&vqa
, Data
, sizeof(vqa
));
95 vqa
.queue
= vqa
.queue
% queues
->num_queues
;
96 /* Cap length at the number of remaining bytes in data */
97 vqa
.length
= vqa
.length
>= Size
? Size
: vqa
.length
;
98 vqa
.write
= vqa
.write
& 1;
99 vqa
.next
= vqa
.next
& 1;
100 vqa
.kick
= vqa
.kick
& 1;
102 q
= queues
->vq
[vqa
.queue
];
104 /* Copy the data into ram, and place it on the virtqueue */
105 uint64_t req_addr
= guest_alloc(t_alloc
, vqa
.length
);
106 qtest_memwrite(s
, req_addr
, Data
, vqa
.length
);
107 if (vq_touched
[vqa
.queue
] == 0) {
108 vq_touched
[vqa
.queue
] = 1;
109 free_head
[vqa
.queue
] = qvirtqueue_add(s
, q
, req_addr
, vqa
.length
,
110 vqa
.write
, vqa
.next
);
112 qvirtqueue_add(s
, q
, req_addr
, vqa
.length
, vqa
.write
, vqa
.next
);
116 qvirtqueue_kick(s
, dev
, q
, free_head
[vqa
.queue
]);
117 free_head
[vqa
.queue
] = 0;
122 /* In the end, kick each queue we interacted with */
123 for (int i
= 0; i
< MAX_NUM_QUEUES
+ 2; i
++) {
125 qvirtqueue_kick(s
, dev
, queues
->vq
[i
], free_head
[i
]);
130 static void virtio_blk_with_flag_fuzz(QTestState
*s
,
131 const unsigned char *Data
, size_t Size
)
133 QVirtioBlk
*blk
= fuzz_qos_obj
;
134 static QVirtioBlkQueues
*queues
;
136 if (Size
>= sizeof(uint64_t)) {
137 queues
= qvirtio_blk_init(blk
->vdev
, *(uint64_t *)Data
);
138 virtio_blk_fuzz(s
, queues
,
139 Data
+ sizeof(uint64_t), Size
- sizeof(uint64_t));
145 static void virtio_blk_pre_fuzz(QTestState
*s
)
150 static void drive_destroy(void *path
)
156 static char *drive_create(void)
161 /* Create a temporary raw image */
162 fd
= g_file_open_tmp("qtest.XXXXXX", &t_path
, NULL
);
163 g_assert_cmpint(fd
, >=, 0);
164 ret
= ftruncate(fd
, TEST_IMAGE_SIZE
);
165 g_assert_cmpint(ret
, ==, 0);
168 g_test_queue_destroy(drive_destroy
, t_path
);
172 static void *virtio_blk_test_setup(GString
*cmd_line
, void *arg
)
174 char *tmp_path
= drive_create();
176 g_string_append_printf(cmd_line
,
177 " -drive if=none,id=drive0,file=%s,"
178 "format=raw,auto-read-only=off ",
184 static void register_virtio_blk_fuzz_targets(void)
186 fuzz_add_qos_target(&(FuzzTarget
){
187 .name
= "virtio-blk-flags-fuzz",
188 .description
= "Fuzz the virtio-blk virtual queues. "
189 "Also fuzzes the virtio flags)",
190 .pre_fuzz
= &virtio_blk_pre_fuzz
,
191 .fuzz
= virtio_blk_with_flag_fuzz
,},
193 &(QOSGraphTestOptions
){.before
= virtio_blk_test_setup
}
197 fuzz_target_init(register_virtio_blk_fuzz_targets
);