From patchwork Wed Oct 21 21:09:09 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Bulekov X-Patchwork-Id: 270721 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.3 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS, HK_RANDOM_FROM, INCLUDES_PATCH, MAILING_LIST_MULTI, MSGID_FROM_MTA_HEADER, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B2791C4363A for ; Wed, 21 Oct 2020 21:15:58 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id E47C72419B for ; Wed, 21 Oct 2020 21:15:57 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=bushare.onmicrosoft.com header.i=@bushare.onmicrosoft.com header.b="wUtlc1vR" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org E47C72419B Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=bu.edu Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:50220 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kVLSe-0000Dg-NP for qemu-devel@archiver.kernel.org; Wed, 21 Oct 2020 17:15:56 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:42912) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kVLMn-0003UQ-Vj for qemu-devel@nongnu.org; Wed, 21 Oct 2020 17:09:54 -0400 Received: from mail-eopbgr690122.outbound.protection.outlook.com ([40.107.69.122]:45277 helo=NAM04-CO1-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kVLMm-00083h-74 for qemu-devel@nongnu.org; Wed, 21 Oct 2020 17:09:53 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=jJNp4bWQSUbj5yqTBFM3jQGafvOyb7Z3S1zM6dpzssNh1lIdGseMm0NoF3LUCFwBR3EqQL2vlT7FuCQACl1wXSq/4TI57VVANzyGT4PfS+Zjk1qv/0wsv1aQYcwpS2PCRXogWOBs6VEO+hLdhYawfwFMSzYp0sGD6jvvn2BxZeJ9euNfTvG3RSpN/hKMB1i+UXeNYHcFFSZiDhSdOK0NQu7vRViu/4aiFVVKixC73yRZpFdVkm8GfvMWN0pibtH7Ov0y0ZIuT4Ltmm3n80FbJgSWG7a/DBbQec7FuUILCMMnbb3eBejGtkKzeJSUGDXQ1xwr5n4wEYeOlFyJk6fLlA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=HKB7W+fzMDCxTdWVDO83cCtE99Mlpb1tnh2BC3fr58U=; b=LqN9ypFMrWj7odwklLbN+i7nh9YD2Ul8Gosppf5BFizjD7Ew6ckMx+cG37mTwJBWA2oIX1bKa1/6V31cx/S8QSGLu/mrLOO+f4oVlqH53HFmAXgi/WBzQGQpu4S3bIq8z7NS7M/36+7n1K7y41zJbz+gy4Y6GO3+QFQLKSh2GieTUpbl4GeM03VXPvgXhuGtWk+ePdema3b6Nl8b/lEDeI6cNTJVi8n4rmrE6Z1DggkbWq6OAPfTOrgl/mctb5Yuf5NcE2Ophuc4MQ/1M2EomrxGZtewLa1j9Odo7Z+5D/A1XLrp+a2Q4/JWEGSXtgl4Yt723llfgB7K+27euXAngA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=bu.edu; dmarc=pass action=none header.from=bu.edu; dkim=pass header.d=bu.edu; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bushare.onmicrosoft.com; s=selector2-bushare-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=HKB7W+fzMDCxTdWVDO83cCtE99Mlpb1tnh2BC3fr58U=; b=wUtlc1vRdSj26017lmotUk1Qf2SJ2AiXOT0iwoEI3T+aSxtvl8w3wJm5jn8712bSN89Fz4Mi0fYTB5GEVdC+rneugsBXt8H7Pyl9SuRgEr8UxUR5fBD0fyh/9CqEfa5HnM7z95kty5iuxgLFOaitfajgZ3ZAOTaz2BvW1TzJR/o= Authentication-Results: nongnu.org; dkim=none (message not signed) header.d=none; nongnu.org; dmarc=none action=none header.from=bu.edu; Received: from SN6PR03MB3871.namprd03.prod.outlook.com (2603:10b6:805:6d::32) by SN6PR03MB4045.namprd03.prod.outlook.com (2603:10b6:805:b8::30) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3477.21; Wed, 21 Oct 2020 21:09:39 +0000 Received: from SN6PR03MB3871.namprd03.prod.outlook.com ([fe80::d520:4c19:8ce6:7db2]) by SN6PR03MB3871.namprd03.prod.outlook.com ([fe80::d520:4c19:8ce6:7db2%2]) with mapi id 15.20.3477.028; Wed, 21 Oct 2020 21:09:38 +0000 From: Alexander Bulekov To: qemu-devel@nongnu.org Subject: [PATCH v6 03/16] fuzz: Add PCI features to the generic fuzzer Date: Wed, 21 Oct 2020 17:09:09 -0400 Message-Id: <20201021210922.572955-4-alxndr@bu.edu> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20201021210922.572955-1-alxndr@bu.edu> References: <20201021210922.572955-1-alxndr@bu.edu> X-Originating-IP: [72.93.72.163] X-ClientProxiedBy: BL0PR0102CA0064.prod.exchangelabs.com (2603:10b6:208:25::41) To SN6PR03MB3871.namprd03.prod.outlook.com (2603:10b6:805:6d::32) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from stormtrooper.vrmnet (72.93.72.163) by BL0PR0102CA0064.prod.exchangelabs.com (2603:10b6:208:25::41) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3499.18 via Frontend Transport; Wed, 21 Oct 2020 21:09:37 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: ec91ff00-a42d-40d8-5c32-08d876059ec7 X-MS-TrafficTypeDiagnostic: SN6PR03MB4045: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:268; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: cjil8uwkscKwp02Nb6a+2SlQHm0e4YE9Rac4UjHi8YY1EvYBhv9NpIX1KmBKmx4AQUWF1oONtGEaFIirYEnd+ghCvP3jICJaZp1zFcTyvGRHESx/MggN6MUU0XsJxlDd/PddTMMf85lT/IMUNJGF2rByhlSDGNTd/PBPt5gYZallM41aOyk409z6/St25Mn/4bzpcSd98eWt/6BgAywVSQzgaiNqAdDN1BbEmn6+z4D16wBj/pnhl0It7DE1BGAgWl4O/x+whlp/Bfy2itQ79tGTg5x6X93Wjf1iV9g9PZlu2u5Kk6BDsEm7y/dzYi5pxi/NhVReuPgSJS10dPHlDyY+4fQMASvXac6GleMkhaR7gfknPDDghsmVEXCKp23f X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:SN6PR03MB3871.namprd03.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(396003)(136003)(366004)(376002)(346002)(39860400002)(83380400001)(86362001)(6506007)(956004)(6666004)(2616005)(52116002)(5660300002)(316002)(75432002)(54906003)(786003)(8936002)(6916009)(6512007)(16526019)(36756003)(26005)(186003)(478600001)(66946007)(6486002)(2906002)(4326008)(66476007)(1076003)(8676002)(66556008)(41533002); DIR:OUT; SFP:1102; X-MS-Exchange-AntiSpam-MessageData: ZQF/uwvPGb9Xuvr3BpC98BHomvg/A5ORfZKiiq+MhWR6Pbz/buXv2y/6kbeh39cKubUff+hxQVZ+8iSfn4XtY+HDa8eUfNQ971hCcVZaodhGrOW4Rgv1Sajd2R6OGxIpj+pAePlNfnFesv9MAXQChf650+zqJJboDM0zuiThQJC3yw9M1p9sOkxuT9exvgDkIrgAQlco/fGKRh6gRVosK/Qa1CcY5JtTA5IFU5MqN+qphtXYgPJv3APY7Y7CNSGoLDs2qVxorcuyWiwoPjURXa/k3ycitDuAMQEBPaw0yb9iAPLIB2U/kxBZ/aS+C6bC806tDnBitRJLh74oZSsHCSB679OmjbFvPvdgMbaHbMH81igHYHUPyQAqAXKbE340DA2wSwDdVRZmBL9QyCvQOW5DTnvEjJyX1T9zMv86oMBH7hBh+SPKEcJSPpHLvO/AgkGFBbRrvN0eA9kgKKLITRC43hpUDGi4tnZILawIKuxL2niztVXZiNgZ4jJ+6QJ3RAXWTfZQBKuIksalZO+QYHb52FDVY+GSBmXEV/GS8AH5jUmA+HUhHyJWZJugZNYxhnsnSF2m6GcrDC9tCocgnMyEWLN1SILLw8pDImcJ/en8KUvj19cQSS7/XftIWiVkwOrWuJ5a7A77tYJEgGOu7Q== X-OriginatorOrg: bu.edu X-MS-Exchange-CrossTenant-Network-Message-Id: ec91ff00-a42d-40d8-5c32-08d876059ec7 X-MS-Exchange-CrossTenant-AuthSource: SN6PR03MB3871.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Oct 2020 21:09:38.9051 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: d57d32cc-c121-488f-b07b-dfe705680c71 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: a39nc+3pkpv/TrVQXRoGRNBowqeDcUMfuw1/JYoN4yBT+13wHsR4nWyb0pGjBDM3 X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN6PR03MB4045 Received-SPF: pass client-ip=40.107.69.122; envelope-from=alxndr@bu.edu; helo=NAM04-CO1-obe.outbound.protection.outlook.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/10/21 17:09:36 X-ACL-Warn: Detected OS = Windows NT kernel [generic] [fuzzy] X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, HK_RANDOM_ENVFROM=0.001, HK_RANDOM_FROM=0.454, MSGID_FROM_MTA_HEADER=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action 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: Laurent Vivier , Thomas Huth , Alexander Bulekov , f4bug@amsat.org, darren.kenny@oracle.com, bsd@redhat.com, stefanha@redhat.com, pbonzini@redhat.com, dimastep@yandex-team.ru Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" This patch compares TYPE_PCI_DEVICE objects against the user-provided matching pattern. If there is a match, we use some hacks and leverage QOS to map each possible BAR for that device. Now fuzzed inputs might be converted to pci_read/write commands which target specific. This means that we can fuzz a particular device's PCI configuration space, Signed-off-by: Alexander Bulekov Reviewed-by: Darren Kenny --- tests/qtest/fuzz/generic_fuzz.c | 81 +++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/tests/qtest/fuzz/generic_fuzz.c b/tests/qtest/fuzz/generic_fuzz.c index f69e9583ce..4ab28c2ec7 100644 --- a/tests/qtest/fuzz/generic_fuzz.c +++ b/tests/qtest/fuzz/generic_fuzz.c @@ -24,6 +24,7 @@ #include "exec/ramblock.h" #include "exec/address-spaces.h" #include "hw/qdev-core.h" +#include "hw/pci/pci.h" /* * SEPARATOR is used to separate "operations" in the fuzz input @@ -35,12 +36,17 @@ enum cmds { OP_OUT, OP_READ, OP_WRITE, + OP_PCI_READ, + OP_PCI_WRITE, OP_CLOCK_STEP, }; #define DEFAULT_TIMEOUT_US 100000 #define USEC_IN_SEC 1000000000 +#define PCI_HOST_BRIDGE_CFG 0xcf8 +#define PCI_HOST_BRIDGE_DATA 0xcfc + typedef struct { ram_addr_t addr; ram_addr_t size; /* The number of bytes until the end of the I/O region */ @@ -55,6 +61,7 @@ static bool qtest_log_enabled; * user for fuzzing. */ static GHashTable *fuzzable_memoryregions; +static GPtrArray *fuzzable_pci_devices; struct get_io_cb_info { int index; @@ -280,6 +287,65 @@ static void op_write(QTestState *s, const unsigned char * data, size_t len) break; } } +static void op_pci_read(QTestState *s, const unsigned char * data, size_t len) +{ + enum Sizes {Byte, Word, Long, end_sizes}; + struct { + uint8_t size; + uint8_t base; + uint8_t offset; + } a; + if (len < sizeof(a) || fuzzable_pci_devices->len == 0) { + return; + } + memcpy(&a, data, sizeof(a)); + PCIDevice *dev = g_ptr_array_index(fuzzable_pci_devices, + a.base % fuzzable_pci_devices->len); + int devfn = dev->devfn; + qtest_outl(s, PCI_HOST_BRIDGE_CFG, (1U << 31) | (devfn << 8) | a.offset); + switch (a.size %= end_sizes) { + case Byte: + qtest_inb(s, PCI_HOST_BRIDGE_DATA); + break; + case Word: + qtest_inw(s, PCI_HOST_BRIDGE_DATA); + break; + case Long: + qtest_inl(s, PCI_HOST_BRIDGE_DATA); + break; + } +} + +static void op_pci_write(QTestState *s, const unsigned char * data, size_t len) +{ + enum Sizes {Byte, Word, Long, end_sizes}; + struct { + uint8_t size; + uint8_t base; + uint8_t offset; + uint32_t value; + } a; + if (len < sizeof(a) || fuzzable_pci_devices->len == 0) { + return; + } + memcpy(&a, data, sizeof(a)); + PCIDevice *dev = g_ptr_array_index(fuzzable_pci_devices, + a.base % fuzzable_pci_devices->len); + int devfn = dev->devfn; + qtest_outl(s, PCI_HOST_BRIDGE_CFG, (1U << 31) | (devfn << 8) | a.offset); + switch (a.size %= end_sizes) { + case Byte: + qtest_outb(s, PCI_HOST_BRIDGE_DATA, a.value & 0xFF); + break; + case Word: + qtest_outw(s, PCI_HOST_BRIDGE_DATA, a.value & 0xFFFF); + break; + case Long: + qtest_outl(s, PCI_HOST_BRIDGE_DATA, a.value & 0xFFFFFFFF); + break; + } +} + static void op_clock_step(QTestState *s, const unsigned char *data, size_t len) { qtest_clock_step_next(s); @@ -338,6 +404,8 @@ static void generic_fuzz(QTestState *s, const unsigned char *Data, size_t Size) [OP_OUT] = op_out, [OP_READ] = op_read, [OP_WRITE] = op_write, + [OP_PCI_READ] = op_pci_read, + [OP_PCI_WRITE] = op_pci_write, [OP_CLOCK_STEP] = op_clock_step, }; const unsigned char *cmd = Data; @@ -428,6 +496,18 @@ static int locate_fuzz_objects(Object *child, void *opaque) /* Find and save ptrs to any child MemoryRegions */ object_child_foreach_recursive(child, locate_fuzz_memory_regions, NULL); + /* + * We matched an object. If its a PCI device, store a pointer to it so + * we can map BARs and fuzz its config space. + */ + if (object_dynamic_cast(OBJECT(child), TYPE_PCI_DEVICE)) { + /* + * Don't want duplicate pointers to the same PCIDevice, so remove + * copies of the pointer, before adding it. + */ + g_ptr_array_remove_fast(fuzzable_pci_devices, PCI_DEVICE(child)); + g_ptr_array_add(fuzzable_pci_devices, PCI_DEVICE(child)); + } } else if (object_dynamic_cast(OBJECT(child), TYPE_MEMORY_REGION)) { if (g_pattern_match_simple(pattern, object_get_canonical_path_component(child))) { @@ -460,6 +540,7 @@ static void generic_pre_fuzz(QTestState *s) } fuzzable_memoryregions = g_hash_table_new(NULL, NULL); + fuzzable_pci_devices = g_ptr_array_new(); result = g_strsplit(getenv("QEMU_FUZZ_OBJECTS"), " ", -1); for (int i = 0; result[i] != NULL; i++) {