From patchwork Mon Sep 21 02:24:53 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Bulekov X-Patchwork-Id: 273217 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.7 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 55A1EC43463 for ; Mon, 21 Sep 2020 02:30:50 +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 A8EE92067D for ; Mon, 21 Sep 2020 02:30:49 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=bushare.onmicrosoft.com header.i=@bushare.onmicrosoft.com header.b="iJtA1tka" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org A8EE92067D 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]:48986 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kKBbM-0008VQ-Gb for qemu-devel@archiver.kernel.org; Sun, 20 Sep 2020 22:30:48 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:41470) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kKBWT-000399-VQ for qemu-devel@nongnu.org; Sun, 20 Sep 2020 22:25:45 -0400 Received: from mail-dm6nam12on2117.outbound.protection.outlook.com ([40.107.243.117]:28256 helo=NAM12-DM6-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 1kKBWS-0000b7-2t for qemu-devel@nongnu.org; Sun, 20 Sep 2020 22:25:45 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Gh2WO/lK7kMR8v+8iXRkfeIJUsuuTHS0mBvRHkXqyx22jE8enrciuiZRW77bQqbcQ/r7dLytYGVy0dBJbg4D5hOxDPCd3YdHFUdH/RABJTUatCF9YGvS7eMdgTUqSQzlzurPOP7n+bLGb3dvb8P/f4GrLV+IWy5XFIkIetYEVv7l4hiRdKMUmJ4biXqYObDtoKwypT/QL9ALQGa3FUD+mf+ES0KMPpO7UyJ1nMm0DmOt2XW1qTg0I669D8SgeNh/IcNqCIkRzMRvp9XREAt+hRdpdEHjrl9ROWiWi4AUyXixjxFQwgyzuElnVvikMdQGxSmQq1P/tNluPyWUsy96uA== 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=sETfKWrK2/FdFUo0x/a5ED2AtHMkmhB2OCwpeRseRyo=; b=AwMLzjc/rCa8PIT1W0L+y8OaHlhx/03XcC8MVyv/FoE9mt6bsZZokf3CsT/v5FxrFVccnlAlGjbOJVCY/B1gX1YZMhOJNkFcvcMsq/X3+kcxFR+kSMdFbXTh3v++KFA3koakn23I2M3h+wWP/SI2XzFrQ9C+GHkw+7nRuJM395u9GhXZ75TBcJNeLDi52auZT60yweQMZccvtRmC/AMfwP59i5/DsqiLRMKsFn4AF+U3Oh23DN3ekk82KUThbTc2LDhSHJh7Nt/v0j4K1DTqr4J2q62lW5i4ndVstVdwx+zAqTgEnEaT0cwk3vBb4HQO+w/sOXh9eYQtbzJma7vx9g== 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=sETfKWrK2/FdFUo0x/a5ED2AtHMkmhB2OCwpeRseRyo=; b=iJtA1tkaLi0DC+sI7hI2vPwhir8hCe/aoAX8fUXE1b+vE0kvEu3tIwIFh12ffWfuujqYLrTQdG2rjVr4Jmd+lUXQd6JtkjQqFcAa4S8N6SFX3v3wIqyNKDbIiuZG0xbx8G6l2LcL1fvyxqwq5TNTY8GQWexh/WRhekOX7dnh8vc= 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 SN2PR03MB2237.namprd03.prod.outlook.com (2603:10b6:804:e::27) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3391.11; Mon, 21 Sep 2020 02:25:24 +0000 Received: from SN6PR03MB3871.namprd03.prod.outlook.com ([fe80::61ae:93a8:b26c:77b8]) by SN6PR03MB3871.namprd03.prod.outlook.com ([fe80::61ae:93a8:b26c:77b8%4]) with mapi id 15.20.3391.024; Mon, 21 Sep 2020 02:25:24 +0000 From: Alexander Bulekov To: qemu-devel@nongnu.org Subject: [PATCH v3 03/16] fuzz: Add PCI features to the general fuzzer Date: Sun, 20 Sep 2020 22:24:53 -0400 Message-Id: <20200921022506.873303-4-alxndr@bu.edu> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200921022506.873303-1-alxndr@bu.edu> References: <20200921022506.873303-1-alxndr@bu.edu> X-ClientProxiedBy: BL0PR1501CA0034.namprd15.prod.outlook.com (2603:10b6:207:17::47) 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 BL0PR1501CA0034.namprd15.prod.outlook.com (2603:10b6:207:17::47) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3391.14 via Frontend Transport; Mon, 21 Sep 2020 02:25:23 +0000 X-Mailer: git-send-email 2.28.0 X-Originating-IP: [72.93.72.163] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: f3552ee0-5f4a-426c-966f-08d85dd5984c X-MS-TrafficTypeDiagnostic: SN2PR03MB2237: 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: v5kNdLJqKpfQ9QXzQvemwKOIhCJBlzHZH7DP30oy0wAJ25ZAs9HRneJFOxzHWSqgbBfW4GkW+ZHqAs7g+biKI65Rh0WbT9aDFYn+X8nOS/axWlohT4AaJar5j9wVIL1QcUw3mY+fNNOpX+Q4RuyjHrxw06vcvlkQjccXPRJ8WraSaLpOj8jpnk7BIDeCNmwZFBOssKB3b7aCOKqOOlkNVVOde7DnfG9vOvEHc92QtER7FOjIrBBWU0HnCFF8x4Zm36Vddp/iWbB4cJUdUP8UA0IPkJze10+lgLeadqNqAueBY1a/b9M2ovptIjAMS52wkgp5C8d83PDrqymrRszYcjkiOcscM6NQ7nmh6syKok0D/oeJM85EnHExNofD4Gmm 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)(366004)(376002)(136003)(346002)(39860400002)(2616005)(956004)(4326008)(54906003)(83380400001)(26005)(478600001)(16526019)(6666004)(8936002)(316002)(6486002)(66476007)(1076003)(6512007)(186003)(6916009)(86362001)(66946007)(5660300002)(36756003)(75432002)(2906002)(6506007)(8676002)(66556008)(52116002)(786003); DIR:OUT; SFP:1102; X-MS-Exchange-AntiSpam-MessageData: x1sVVQuaodCgKwkCvdX39ialyFNt2LAgwcHmCTdFfgY8+KzjTSRCj4PvP/Dzzyx5DZu6YTLsVtsH7JPvt0G0BTbtNty81ZJQuOo98FaY67wD3KEncmPm2lkoe3msW8/7n8I9MkfyQqHtJghgMyFW3yQjOyEagjtRQWvaG2+XlJQhlPTkjx1rS1MGBV1c69UUaR0VMMIpCg3xmU7RnfM87CgQ7S8tjK3wNhIiea9VBnkma2KYvnCPp6PFgykDikAFoJ8HPxu5coQPdlDxpVaTxkfW99V16sxG+5FdcsPgLfZbDhoCtZAYqhD7D99VrxmlfYHHAXwg+RZ16Jjaga0Un0T+2hp/3Y3nBL8rJsyASpmqtCtWLdn4tSaoczhCw0nlBknZQfOsmTEcMOoDOvEgOnDLMVqzD1pbUHl3lhsWOwNAowSZOWNvVQdfcmNTMQwYfPQKbPaE3PUACmBn8uMaj2mYgHG0YdeIBFBvPA9k9UwCVyuGhB64VsN5frF4MRL4BOYeb65JY84+GfDm6dAW1fqe97hwU6Tmy3VIbXr7IU2KRQvjbEo9U5fkvyT58OpNKBMFIop9wPwtwwrUb9MFgaUlWKzZc0GqCPWBuuGX1aO54rASqJ3PEOf2njvwuL8RegiQ+L/ZWKOdtGdpM0s3Kg== X-OriginatorOrg: bu.edu X-MS-Exchange-CrossTenant-Network-Message-Id: f3552ee0-5f4a-426c-966f-08d85dd5984c X-MS-Exchange-CrossTenant-AuthSource: SN6PR03MB3871.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Sep 2020 02:25:24.2605 (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: 4ZvhtWVdMLeqC94sxiCDePKRt45ByXM6Z0hmJX83Os86kwHsk908cw0Mw7qskuVo X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN2PR03MB2237 Received-SPF: pass client-ip=40.107.243.117; envelope-from=alxndr@bu.edu; helo=NAM12-DM6-obe.outbound.protection.outlook.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/09/20 22:25:42 X-ACL-Warn: Detected OS = Windows NT kernel [generic] [fuzzy] X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, HK_RANDOM_ENVFROM=0.001, HK_RANDOM_FROM=0.001, MSGID_FROM_MTA_HEADER=0.001, RCVD_IN_DNSWL_NONE=-0.0001, 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 , darren.kenny@oracle.com, bsd@redhat.com, stefanha@redhat.com, Paolo Bonzini , philmd@redhat.com 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/general_fuzz.c | 81 +++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/tests/qtest/fuzz/general_fuzz.c b/tests/qtest/fuzz/general_fuzz.c index bf75b215ca..7c4c1398a7 100644 --- a/tests/qtest/fuzz/general_fuzz.c +++ b/tests/qtest/fuzz/general_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 100000000 +#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); @@ -324,6 +390,8 @@ static void general_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; @@ -415,6 +483,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))) { @@ -448,6 +528,7 @@ static void general_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++) {