Coverage Report

Created: 2026-10-31 08:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qemu/tests/qtest/fuzz/virtio_scsi_fuzz.c
Line
Count
Source
1
/*
2
 * virtio-serial Fuzzing Target
3
 *
4
 * Copyright Red Hat Inc., 2019
5
 *
6
 * Authors:
7
 *  Alexander Bulekov   <alxndr@bu.edu>
8
 *
9
 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10
 * See the COPYING file in the top-level directory.
11
 */
12
13
#include "qemu/osdep.h"
14
15
#include "tests/qtest/libqtest.h"
16
#include "tests/qtest/libqos/virtio-scsi.h"
17
#include "tests/qtest/libqos/virtio.h"
18
#include "tests/qtest/libqos/virtio-pci.h"
19
#include "standard-headers/linux/virtio_ids.h"
20
#include "standard-headers/linux/virtio_pci.h"
21
#include "standard-headers/linux/virtio_scsi.h"
22
#include "fuzz.h"
23
#include "qos_fuzz.h"
24
25
#define PCI_SLOT                0x02
26
#define PCI_FN                  0x00
27
#define QVIRTIO_SCSI_TIMEOUT_US (1 * 1000 * 1000)
28
29
0
#define MAX_NUM_QUEUES 64
30
31
/* Based on tests/virtio-scsi-test.c */
32
typedef struct {
33
    int num_queues;
34
    QVirtQueue *vq[MAX_NUM_QUEUES + 2];
35
} QVirtioSCSIQueues;
36
37
static QVirtioSCSIQueues *qvirtio_scsi_init(QVirtioDevice *dev, uint64_t mask)
38
0
{
39
0
    QVirtioSCSIQueues *vs;
40
0
    uint64_t feat;
41
0
    int i;
42
43
0
    vs = g_new0(QVirtioSCSIQueues, 1);
44
45
0
    feat = qvirtio_get_features(dev);
46
0
    if (mask) {
47
0
        feat &= ~QVIRTIO_F_BAD_FEATURE | mask;
48
0
    } else {
49
0
        feat &= ~(QVIRTIO_F_BAD_FEATURE | (1ull << VIRTIO_RING_F_EVENT_IDX));
50
0
    }
51
0
    qvirtio_set_features(dev, feat);
52
53
0
    vs->num_queues = qvirtio_config_readl(dev, 0);
54
55
0
    for (i = 0; i < vs->num_queues + 2; i++) {
56
0
        vs->vq[i] = qvirtqueue_setup(dev, fuzz_qos_alloc, i);
57
0
    }
58
59
0
    qvirtio_set_driver_ok(dev);
60
61
0
    return vs;
62
0
}
63
64
static void virtio_scsi_fuzz(QTestState *s, QVirtioSCSIQueues* queues,
65
        const unsigned char *Data, size_t Size)
66
0
{
67
    /*
68
     * Data is a sequence of random bytes. We split them up into "actions",
69
     * followed by data:
70
     * [vqa][dddddddd][vqa][dddd][vqa][dddddddddddd] ...
71
     * The length of the data is specified by the preceding vqa.length
72
     */
73
0
    typedef struct vq_action {
74
0
        uint8_t queue;
75
0
        uint8_t length;
76
0
        uint8_t write;
77
0
        uint8_t next;
78
0
        uint8_t kick;
79
0
    } vq_action;
80
81
    /* Keep track of the free head for each queue we interact with */
82
0
    bool vq_touched[MAX_NUM_QUEUES + 2] = {0};
83
0
    uint32_t free_head[MAX_NUM_QUEUES + 2];
84
85
0
    QGuestAllocator *t_alloc = fuzz_qos_alloc;
86
87
0
    QVirtioSCSI *scsi = fuzz_qos_obj;
88
0
    QVirtioDevice *dev = scsi->vdev;
89
0
    QVirtQueue *q;
90
0
    vq_action vqa;
91
0
    while (Size >= sizeof(vqa)) {
92
        /* Copy the action, so we can normalize length, queue and flags */
93
0
        memcpy(&vqa, Data, sizeof(vqa));
94
95
0
        Data += sizeof(vqa);
96
0
        Size -= sizeof(vqa);
97
98
0
        vqa.queue = vqa.queue % queues->num_queues;
99
        /* Cap length at the number of remaining bytes in data */
100
0
        vqa.length = vqa.length >= Size ? Size : vqa.length;
101
0
        vqa.write = vqa.write & 1;
102
0
        vqa.next = vqa.next & 1;
103
0
        vqa.kick = vqa.kick & 1;
104
105
106
0
        q = queues->vq[vqa.queue];
107
108
        /* Copy the data into ram, and place it on the virtqueue */
109
0
        uint64_t req_addr = guest_alloc(t_alloc, vqa.length);
110
0
        qtest_memwrite(s, req_addr, Data, vqa.length);
111
0
        if (vq_touched[vqa.queue] == 0) {
112
0
            vq_touched[vqa.queue] = 1;
113
0
            free_head[vqa.queue] = qvirtqueue_add(s, q, req_addr, vqa.length,
114
0
                    vqa.write, vqa.next);
115
0
        } else {
116
0
            qvirtqueue_add(s, q, req_addr, vqa.length, vqa.write , vqa.next);
117
0
        }
118
119
0
        if (vqa.kick) {
120
0
            qvirtqueue_kick(s, dev, q, free_head[vqa.queue]);
121
0
            free_head[vqa.queue] = 0;
122
0
        }
123
0
        Data += vqa.length;
124
0
        Size -= vqa.length;
125
0
    }
126
    /* In the end, kick each queue we interacted with */
127
0
    for (int i = 0; i < MAX_NUM_QUEUES + 2; i++) {
128
0
        if (vq_touched[i]) {
129
0
            qvirtqueue_kick(s, dev, queues->vq[i], free_head[i]);
130
0
        }
131
0
    }
132
0
}
133
134
static void virtio_scsi_with_flag_fuzz(QTestState *s,
135
        const unsigned char *Data, size_t Size)
136
0
{
137
0
    QVirtioSCSI *scsi = fuzz_qos_obj;
138
0
    static QVirtioSCSIQueues *queues;
139
140
0
    if (Size >= sizeof(uint64_t)) {
141
0
        queues = qvirtio_scsi_init(scsi->vdev, *(uint64_t *)Data);
142
0
        virtio_scsi_fuzz(s, queues,
143
0
                Data + sizeof(uint64_t), Size - sizeof(uint64_t));
144
0
        flush_events(s);
145
0
    }
146
0
    fuzz_reset(s);
147
0
}
148
149
static void virtio_scsi_pre_fuzz(QTestState *s)
150
0
{
151
0
    qos_init_path(s);
152
0
}
153
154
static void *virtio_scsi_test_setup(GString *cmd_line, void *arg)
155
0
{
156
0
    g_string_append(cmd_line,
157
0
                    " -drive file=blkdebug::null-co://,"
158
0
                    "file.image.read-zeroes=on,"
159
0
                    "if=none,id=dr1,format=raw,file.align=4k "
160
0
                    "-device scsi-hd,drive=dr1,lun=0,scsi-id=1");
161
0
    return arg;
162
0
}
163
164
165
static void register_virtio_scsi_fuzz_targets(void)
166
61
{
167
61
    fuzz_add_qos_target(&(FuzzTarget){
168
61
                .name = "virtio-scsi-flags-fuzz",
169
61
                .description = "Fuzz the virtio-scsi virtual queues. "
170
61
                "Also fuzzes the virtio flags",
171
61
                .pre_fuzz = &virtio_scsi_pre_fuzz,
172
61
                .fuzz = virtio_scsi_with_flag_fuzz,},
173
61
                "virtio-scsi",
174
61
                &(QOSGraphTestOptions){.before = virtio_scsi_test_setup}
175
61
                );
176
61
}
177
178
fuzz_target_init(register_virtio_scsi_fuzz_targets);