From nobody Fri Dec 19 04:38:22 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1582362179; cv=none; d=zohomail.com; s=zohoarc; b=eXc/QzfkLnYI01RAwMdFTODgZzuJPcZA9eTYe4kQeAjmcjMiu4VSfweUpo7hQzECqPeNVlQHdZdNZfd4FZkwGRrAdrmwUoM9gZEGNCm3IsZ6Wn5R/kCNrixZhw40ZF5FP/KPDbMiCSdm3XtfSIkytwwwE2kUgEk3+NOx9Dyy9cw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1582362179; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=RP6Gk/V7LgK+F6cCoicw+Xsk6TLvYX3+9o3w/LID7mA=; b=TSimga5eZKFHJOXBUMJ2x1xy7IKeshMjMw45o/NNStf9sH677eUrPeBx+BU2fw9DF+GWtFI5c+tMsH+4/5ovw15eEDXl6FzhD039JL0M9UrtfMsQJ1JByOTYs5MjnHhvRDZhcQlS9Oim6BJS6s//JqJqmcugs2uejw66x3McGCM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1582362179215699.9028956292462; Sat, 22 Feb 2020 01:02:59 -0800 (PST) Received: from localhost ([::1]:40602 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j5Qgc-0001af-0x for importer@patchew.org; Sat, 22 Feb 2020 04:02:58 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:39731) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j5QaM-0006sl-Lv for qemu-devel@nongnu.org; Sat, 22 Feb 2020 03:56:32 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j5QaI-0004Pp-FF for qemu-devel@nongnu.org; Sat, 22 Feb 2020 03:56:30 -0500 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:50531 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1j5QaI-0004Pd-Be for qemu-devel@nongnu.org; Sat, 22 Feb 2020 03:56:26 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-117-Ro1WpIBQP2SkGIAmnm2Vgw-1; Sat, 22 Feb 2020 03:56:23 -0500 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 49C9C100550E; Sat, 22 Feb 2020 08:56:22 +0000 (UTC) Received: from localhost (ovpn-116-74.ams2.redhat.com [10.36.116.74]) by smtp.corp.redhat.com (Postfix) with ESMTP id 045B060BE0; Sat, 22 Feb 2020 08:56:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582361786; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=RP6Gk/V7LgK+F6cCoicw+Xsk6TLvYX3+9o3w/LID7mA=; b=AsCn5cTvzuyVSsCwxN3OPZJ3kxtA8gUD8eZkGveBz/5FBtIJ8g23sVQkehut9ypFC6u3vl mFQpAHFYmB0lmD+ZmwiNwomeSMbYdVW+o6c2zFPLBN801Uh2zZPBoB33aUd4EpYKF9sHU2 zdxupP3oM9QxuuD8LayBZDVPiTXc4h8= X-MC-Unique: Ro1WpIBQP2SkGIAmnm2Vgw-1 From: Stefan Hajnoczi To: qemu-devel@nongnu.org Subject: [PULL 30/31] fuzz: add virtio-scsi fuzz target Date: Sat, 22 Feb 2020 08:50:29 +0000 Message-Id: <20200222085030.1760640-31-stefanha@redhat.com> In-Reply-To: <20200222085030.1760640-1-stefanha@redhat.com> References: <20200222085030.1760640-1-stefanha@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 207.211.31.120 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Peter Maydell , Thomas Huth , Eduardo Habkost , qemu-block@nongnu.org, "Michael S. Tsirkin" , Laurent Vivier , Max Reitz , Alexander Bulekov , Bandan Das , Stefan Hajnoczi , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Paolo Bonzini , Fam Zheng , Darren Kenny , Richard Henderson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: Alexander Bulekov The virtio-scsi fuzz target sets up and fuzzes the available virtio-scsi queues. After an element is placed on a queue, the fuzzer can select whether to perform a kick, or continue adding elements. Signed-off-by: Alexander Bulekov Reviewed-by: Darren Kenny Message-id: 20200220041118.23264-22-alxndr@bu.edu Signed-off-by: Stefan Hajnoczi --- tests/qtest/fuzz/Makefile.include | 1 + tests/qtest/fuzz/virtio_scsi_fuzz.c | 213 ++++++++++++++++++++++++++++ 2 files changed, 214 insertions(+) create mode 100644 tests/qtest/fuzz/virtio_scsi_fuzz.c diff --git a/tests/qtest/fuzz/Makefile.include b/tests/qtest/fuzz/Makefile.= include index 77385777ef..cde3e9636c 100644 --- a/tests/qtest/fuzz/Makefile.include +++ b/tests/qtest/fuzz/Makefile.include @@ -9,6 +9,7 @@ fuzz-obj-y +=3D tests/qtest/fuzz/qos_fuzz.o # Targets fuzz-obj-y +=3D tests/qtest/fuzz/i440fx_fuzz.o fuzz-obj-y +=3D tests/qtest/fuzz/virtio_net_fuzz.o +fuzz-obj-y +=3D tests/qtest/fuzz/virtio_scsi_fuzz.o =20 FUZZ_CFLAGS +=3D -I$(SRC_PATH)/tests -I$(SRC_PATH)/tests/qtest =20 diff --git a/tests/qtest/fuzz/virtio_scsi_fuzz.c b/tests/qtest/fuzz/virtio_= scsi_fuzz.c new file mode 100644 index 0000000000..3b95247f12 --- /dev/null +++ b/tests/qtest/fuzz/virtio_scsi_fuzz.c @@ -0,0 +1,213 @@ +/* + * virtio-serial Fuzzing Target + * + * Copyright Red Hat Inc., 2019 + * + * Authors: + * Alexander Bulekov + * + * This work is licensed under the terms of the GNU GPL, version 2 or late= r. + * See the COPYING file in the top-level directory. + */ + +#include "qemu/osdep.h" + +#include "tests/qtest/libqtest.h" +#include "libqos/virtio-scsi.h" +#include "libqos/virtio.h" +#include "libqos/virtio-pci.h" +#include "standard-headers/linux/virtio_ids.h" +#include "standard-headers/linux/virtio_pci.h" +#include "standard-headers/linux/virtio_scsi.h" +#include "fuzz.h" +#include "fork_fuzz.h" +#include "qos_fuzz.h" + +#define PCI_SLOT 0x02 +#define PCI_FN 0x00 +#define QVIRTIO_SCSI_TIMEOUT_US (1 * 1000 * 1000) + +#define MAX_NUM_QUEUES 64 + +/* Based on tests/virtio-scsi-test.c */ +typedef struct { + int num_queues; + QVirtQueue *vq[MAX_NUM_QUEUES + 2]; +} QVirtioSCSIQueues; + +static QVirtioSCSIQueues *qvirtio_scsi_init(QVirtioDevice *dev, uint64_t m= ask) +{ + QVirtioSCSIQueues *vs; + uint64_t feat; + int i; + + vs =3D g_new0(QVirtioSCSIQueues, 1); + + feat =3D qvirtio_get_features(dev); + if (mask) { + feat &=3D ~QVIRTIO_F_BAD_FEATURE | mask; + } else { + feat &=3D ~(QVIRTIO_F_BAD_FEATURE | (1ull << VIRTIO_RING_F_EVENT_I= DX)); + } + qvirtio_set_features(dev, feat); + + vs->num_queues =3D qvirtio_config_readl(dev, 0); + + for (i =3D 0; i < vs->num_queues + 2; i++) { + vs->vq[i] =3D qvirtqueue_setup(dev, fuzz_qos_alloc, i); + } + + qvirtio_set_driver_ok(dev); + + return vs; +} + +static void virtio_scsi_fuzz(QTestState *s, QVirtioSCSIQueues* queues, + const unsigned char *Data, size_t Size) +{ + /* + * Data is a sequence of random bytes. We split them up into "actions", + * followed by data: + * [vqa][dddddddd][vqa][dddd][vqa][dddddddddddd] ... + * The length of the data is specified by the preceding vqa.length + */ + typedef struct vq_action { + uint8_t queue; + uint8_t length; + uint8_t write; + uint8_t next; + uint8_t kick; + } vq_action; + + /* Keep track of the free head for each queue we interact with */ + bool vq_touched[MAX_NUM_QUEUES + 2] =3D {0}; + uint32_t free_head[MAX_NUM_QUEUES + 2]; + + QGuestAllocator *t_alloc =3D fuzz_qos_alloc; + + QVirtioSCSI *scsi =3D fuzz_qos_obj; + QVirtioDevice *dev =3D scsi->vdev; + QVirtQueue *q; + vq_action vqa; + while (Size >=3D sizeof(vqa)) { + /* Copy the action, so we can normalize length, queue and flags */ + memcpy(&vqa, Data, sizeof(vqa)); + + Data +=3D sizeof(vqa); + Size -=3D sizeof(vqa); + + vqa.queue =3D vqa.queue % queues->num_queues; + /* Cap length at the number of remaining bytes in data */ + vqa.length =3D vqa.length >=3D Size ? Size : vqa.length; + vqa.write =3D vqa.write & 1; + vqa.next =3D vqa.next & 1; + vqa.kick =3D vqa.kick & 1; + + + q =3D queues->vq[vqa.queue]; + + /* Copy the data into ram, and place it on the virtqueue */ + uint64_t req_addr =3D guest_alloc(t_alloc, vqa.length); + qtest_memwrite(s, req_addr, Data, vqa.length); + if (vq_touched[vqa.queue] =3D=3D 0) { + vq_touched[vqa.queue] =3D 1; + free_head[vqa.queue] =3D qvirtqueue_add(s, q, req_addr, vqa.le= ngth, + vqa.write, vqa.next); + } else { + qvirtqueue_add(s, q, req_addr, vqa.length, vqa.write , vqa.nex= t); + } + + if (vqa.kick) { + qvirtqueue_kick(s, dev, q, free_head[vqa.queue]); + free_head[vqa.queue] =3D 0; + } + Data +=3D vqa.length; + Size -=3D vqa.length; + } + /* In the end, kick each queue we interacted with */ + for (int i =3D 0; i < MAX_NUM_QUEUES + 2; i++) { + if (vq_touched[i]) { + qvirtqueue_kick(s, dev, queues->vq[i], free_head[i]); + } + } +} + +static void virtio_scsi_fork_fuzz(QTestState *s, + const unsigned char *Data, size_t Size) +{ + QVirtioSCSI *scsi =3D fuzz_qos_obj; + static QVirtioSCSIQueues *queues; + if (!queues) { + queues =3D qvirtio_scsi_init(scsi->vdev, 0); + } + if (fork() =3D=3D 0) { + virtio_scsi_fuzz(s, queues, Data, Size); + flush_events(s); + _Exit(0); + } else { + wait(NULL); + } +} + +static void virtio_scsi_with_flag_fuzz(QTestState *s, + const unsigned char *Data, size_t Size) +{ + QVirtioSCSI *scsi =3D fuzz_qos_obj; + static QVirtioSCSIQueues *queues; + + if (fork() =3D=3D 0) { + if (Size >=3D sizeof(uint64_t)) { + queues =3D qvirtio_scsi_init(scsi->vdev, *(uint64_t *)Data); + virtio_scsi_fuzz(s, queues, + Data + sizeof(uint64_t), Size - sizeof(uint64= _t)); + flush_events(s); + } + _Exit(0); + } else { + wait(NULL); + } +} + +static void virtio_scsi_pre_fuzz(QTestState *s) +{ + qos_init_path(s); + counter_shm_init(); +} + +static void *virtio_scsi_test_setup(GString *cmd_line, void *arg) +{ + g_string_append(cmd_line, + " -drive file=3Dblkdebug::null-co://," + "file.image.read-zeroes=3Don," + "if=3Dnone,id=3Ddr1,format=3Draw,file.align=3D4k " + "-device scsi-hd,drive=3Ddr1,lun=3D0,scsi-id=3D1"); + return arg; +} + + +static void register_virtio_scsi_fuzz_targets(void) +{ + fuzz_add_qos_target(&(FuzzTarget){ + .name =3D "virtio-scsi-fuzz", + .description =3D "Fuzz the virtio-scsi virtual queues, for= king" + "for each fuzz run", + .pre_vm_init =3D &counter_shm_init, + .pre_fuzz =3D &virtio_scsi_pre_fuzz, + .fuzz =3D virtio_scsi_fork_fuzz,}, + "virtio-scsi", + &(QOSGraphTestOptions){.before =3D virtio_scsi_test_setup} + ); + + fuzz_add_qos_target(&(FuzzTarget){ + .name =3D "virtio-scsi-flags-fuzz", + .description =3D "Fuzz the virtio-scsi virtual queues, for= king" + "for each fuzz run (also fuzzes the virtio flags)", + .pre_vm_init =3D &counter_shm_init, + .pre_fuzz =3D &virtio_scsi_pre_fuzz, + .fuzz =3D virtio_scsi_with_flag_fuzz,}, + "virtio-scsi", + &(QOSGraphTestOptions){.before =3D virtio_scsi_test_setup} + ); +} + +fuzz_target_init(register_virtio_scsi_fuzz_targets); --=20 2.24.1