From patchwork Mon Jan 20 05:55:00 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Bulekov X-Patchwork-Id: 1225648 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=bu.edu Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=bushare.onmicrosoft.com header.i=@bushare.onmicrosoft.com header.a=rsa-sha256 header.s=selector2-bushare-onmicrosoft-com header.b=00w2MYKH; dkim-atps=neutral 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 ozlabs.org (Postfix) with ESMTPS id 481Lqd5qBHz9sRd for ; Mon, 20 Jan 2020 17:10:05 +1100 (AEDT) Received: from localhost ([::1]:59066 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1itQGA-0006MW-TA for incoming@patchwork.ozlabs.org; Mon, 20 Jan 2020 01:10:02 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:35347) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1itQ1f-0007FY-Le for qemu-devel@nongnu.org; Mon, 20 Jan 2020 00:55:05 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1itQ1e-0006SP-2f for qemu-devel@nongnu.org; Mon, 20 Jan 2020 00:55:03 -0500 Received: from mail-bn7nam10on2098.outbound.protection.outlook.com ([40.107.92.98]:14528 helo=NAM10-BN7-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1itQ1d-0006S7-Tz for qemu-devel@nongnu.org; Mon, 20 Jan 2020 00:55:02 -0500 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=SN3VMyTSkjWFTs68tRoHYD//KAwpa/OMel2xVU4DcjocEQPjsvh1k6WPP7+7PmUJhfWy6/g/cLKqqSUQHTRrC2Pt8gv3n0E62l0q+B9K50rA0/ykjgas7gcBvI/3FYiLH4nr0EzOyjUa2cZa4jaUf21fmYvuVUxp7InHSDMRN9UNunjt0Z5f075Aljmtb8rF9DJm1ubb24CrT94b/vVd3WDLSanU4BVn5nQxqcsv4Eglsq15Tckgux79eCcvQLq9Xbx34n1/pOWqoH6/Tb0bEWr14/ympm1u6r2k2ffpOHpSZ+Ib22R1RaG7KJ7eG4W8dL0H7b9rPFCfRwzSRk+zkg== 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=fryQK8xhDZQ/FTsWZyYBSV50LhpNP2y8Q2iAmhkAd6c=; b=CrrEP5HCNirtM8MltOaujByKKQ9r+CB+xu2x5BPuE6ceyDRALtFUJmcf3G7IDsIVERT+cvIs5DBut5PzWrfMyo0VhJV/l8TLNbxoIjebuTLQERZOHSidRyPM5ieyLsKPCIoeJPDFKCneUTD8Rk0oi34DC3zqlW3ZLiksgtEIMbw+z6NC14erhtR/96mupFRv/lbFptfr4XYvczfMGXl8jpB20uAICgjCIxyoc5Oi7ylG4IoIp2Ncw9TgiJkgaZr7St0S0H9HmKs6obK9e4HmjVqLzueshAu6qzMg6WFSltbfmjaUhSPjbFLcv3tyswo+PGNBM5FzdWMkG6HlESIEMQ== 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=fryQK8xhDZQ/FTsWZyYBSV50LhpNP2y8Q2iAmhkAd6c=; b=00w2MYKHrwQd/xnZNpjIPoQ2EgEF8CMicSJPX96UZdKNpjQnvIY8rjRd0opSwZByazLDbZStcVONSO+9n3i26627KHFjFQrcH8ZXMbQ67FweiAYzMhPYhbKru0QlWF/JRXA/NKZLcBs/a07ZsMjnq07+gxixpHeg942LCEERBP8= Received: from SN6PR03MB3871.namprd03.prod.outlook.com (52.135.102.32) by SN6PR03MB4399.namprd03.prod.outlook.com (20.178.6.20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2644.23; Mon, 20 Jan 2020 05:55:00 +0000 Received: from SN6PR03MB3871.namprd03.prod.outlook.com ([fe80::9c11:10cd:6e97:bbe8]) by SN6PR03MB3871.namprd03.prod.outlook.com ([fe80::9c11:10cd:6e97:bbe8%7]) with mapi id 15.20.2644.024; Mon, 20 Jan 2020 05:55:00 +0000 Received: from mozz.bu.edu (128.197.127.33) by MN2PR20CA0032.namprd20.prod.outlook.com (2603:10b6:208:e8::45) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2644.20 via Frontend Transport; Mon, 20 Jan 2020 05:54:59 +0000 From: "Bulekov, Alexander" To: "qemu-devel@nongnu.org" Subject: [PATCH v7 13/20] fuzz: support for fork-based fuzzing. Thread-Topic: [PATCH v7 13/20] fuzz: support for fork-based fuzzing. Thread-Index: AQHVz1YnOBDAY/WqDEGgvxlgfET9KA== Date: Mon, 20 Jan 2020 05:55:00 +0000 Message-ID: <20200120055410.22322-19-alxndr@bu.edu> References: <20200120055410.22322-1-alxndr@bu.edu> In-Reply-To: <20200120055410.22322-1-alxndr@bu.edu> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-mailer: git-send-email 2.23.0 x-originating-ip: [128.197.127.33] x-clientproxiedby: MN2PR20CA0032.namprd20.prod.outlook.com (2603:10b6:208:e8::45) To SN6PR03MB3871.namprd03.prod.outlook.com (2603:10b6:805:6d::32) authentication-results: spf=none (sender IP is ) smtp.mailfrom=alxndr@bu.edu; x-ms-exchange-messagesentrepresentingtype: 1 x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: e55500ca-a1a9-488e-513c-08d79d6d4941 x-ms-traffictypediagnostic: SN6PR03MB4399: x-ms-exchange-transport-forked: True x-microsoft-antispam-prvs: x-ms-oob-tlc-oobclassifiers: OLM:9508; x-forefront-prvs: 0288CD37D9 x-forefront-antispam-report: SFV:NSPM; SFS:(10019020)(4636009)(136003)(366004)(39860400002)(346002)(376002)(396003)(189003)(199004)(5660300002)(956004)(2616005)(8936002)(86362001)(6916009)(6486002)(7696005)(1076003)(52116002)(316002)(71200400001)(16526019)(186003)(786003)(478600001)(2906002)(6666004)(26005)(81166006)(66946007)(66476007)(64756008)(36756003)(8676002)(75432002)(81156014)(66446008)(4326008)(66556008)(54906003); DIR:OUT; SFP:1102; SCL:1; SRVR:SN6PR03MB4399; H:SN6PR03MB3871.namprd03.prod.outlook.com; FPR:; SPF:None; LANG:en; PTR:InfoNoRecords; MX:1; A:1; received-spf: None (protection.outlook.com: bu.edu does not designate permitted sender hosts) x-ms-exchange-senderadcheck: 1 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: p30TOs4AjVOJvcv/BP5UAFQw6Wj/T7jbD+X9ut+LMSRrX0fPBBC4HUH5H0SbFMjwLGhWNEovK+4Fqfb4zgRLNH2evBHQWAqal1zOMnBaf1siHIPsWR/AUHx2DjxZQIT1dx+h/JERLC/oU23fHDTLjxkoa4WmCd5ad2SXXDBDdzH8vzYKti2mOHHht5xpu+MRtdpYozwhTZKRDWAJTL4J+w/PwgE7Kt0wt/OS5KXc7F5kdr9oq9Qccq8MD/wrZUaHARma+/PPEoH/+UBZX2w1O90UzmMAQRFosWguibs2RaSbiPLCszGTaMYoihxYgGrmiNcL+f2X9ubUxIUGuDcZWqe2ubZ2nP2tkQYCetiDr7g+wQjwIE0Dada1NXwd6keqJZnDhU1/q+ugHO5zFST2ENUZGffz0Dv/GjEzBV3wOHy0jiZ4dAFOqN99zqb6BmvS MIME-Version: 1.0 X-OriginatorOrg: bu.edu X-MS-Exchange-CrossTenant-Network-Message-Id: e55500ca-a1a9-488e-513c-08d79d6d4941 X-MS-Exchange-CrossTenant-originalarrivaltime: 20 Jan 2020 05:55:00.7295 (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: guR2jZCAUe8giJmb1PkahofWhPXt8i6kjz0sTWAMigVcZg7arQdsKCBooA+SfQsn X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN6PR03MB4399 X-detected-operating-system: by eggs.gnu.org: Windows 7 or 8 [fuzzy] X-Received-From: 40.107.92.98 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 , "Bulekov, Alexander" , "bsd@redhat.com" , "stefanha@redhat.com" , "pbonzini@redhat.com" Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" fork() is a simple way to ensure that state does not leak in between fuzzing runs. Unfortunately, the fuzzer mutation engine relies on bitmaps which contain coverage information for each fuzzing run, and these bitmaps should be copied from the child to the parent(where the mutation occurs). These bitmaps are created through compile-time instrumentation and they are not shared with fork()-ed processes, by default. To address this, we create a shared memory region, adjust its size and map it _over_ the counter region. Furthermore, libfuzzer doesn't generally expose the globals that specify the location of the counters/coverage bitmap. As a workaround, we rely on a custom linker script which forces all of the bitmaps we care about to be placed in a contiguous region, which is easy to locate and mmap over. Signed-off-by: Alexander Bulekov Reviewed-by: Stefan Hajnoczi --- tests/qtest/fuzz/Makefile.include | 5 +++ tests/qtest/fuzz/fork_fuzz.c | 55 +++++++++++++++++++++++++++++++ tests/qtest/fuzz/fork_fuzz.h | 23 +++++++++++++ tests/qtest/fuzz/fork_fuzz.ld | 37 +++++++++++++++++++++ 4 files changed, 120 insertions(+) create mode 100644 tests/qtest/fuzz/fork_fuzz.c create mode 100644 tests/qtest/fuzz/fork_fuzz.h create mode 100644 tests/qtest/fuzz/fork_fuzz.ld diff --git a/tests/qtest/fuzz/Makefile.include b/tests/qtest/fuzz/Makefile.include index 8632bb89f4..a90915d56d 100644 --- a/tests/qtest/fuzz/Makefile.include +++ b/tests/qtest/fuzz/Makefile.include @@ -2,5 +2,10 @@ QEMU_PROG_FUZZ=qemu-fuzz-$(TARGET_NAME)$(EXESUF) fuzz-obj-y += tests/qtest/libqtest.o fuzz-obj-y += tests/qtest/fuzz/fuzz.o # Fuzzer skeleton +fuzz-obj-y += tests/qtest/fuzz/fork_fuzz.o FUZZ_CFLAGS += -I$(SRC_PATH)/tests -I$(SRC_PATH)/tests/qtest + +# Linker Script to force coverage-counters into known regions which we can mark +# shared +FUZZ_LDFLAGS += -Xlinker -T$(SRC_PATH)/tests/qtest/fuzz/fork_fuzz.ld diff --git a/tests/qtest/fuzz/fork_fuzz.c b/tests/qtest/fuzz/fork_fuzz.c new file mode 100644 index 0000000000..2bd0851903 --- /dev/null +++ b/tests/qtest/fuzz/fork_fuzz.c @@ -0,0 +1,55 @@ +/* + * Fork-based fuzzing helpers + * + * Copyright Red Hat Inc., 2019 + * + * Authors: + * Alexander Bulekov + * + * This work is licensed under the terms of the GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + * + */ + +#include "qemu/osdep.h" +#include "fork_fuzz.h" + + +void counter_shm_init(void) +{ + char *shm_path = g_strdup_printf("/qemu-fuzz-cntrs.%d", getpid()); + int fd = shm_open(shm_path, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR); + g_free(shm_path); + + if (fd == -1) { + perror("Error: "); + exit(1); + } + if (ftruncate(fd, &__FUZZ_COUNTERS_END - &__FUZZ_COUNTERS_START) == -1) { + perror("Error: "); + exit(1); + } + /* Copy what's in the counter region to the shm.. */ + void *rptr = mmap(NULL , + &__FUZZ_COUNTERS_END - &__FUZZ_COUNTERS_START, + PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + memcpy(rptr, + &__FUZZ_COUNTERS_START, + &__FUZZ_COUNTERS_END - &__FUZZ_COUNTERS_START); + + munmap(rptr, &__FUZZ_COUNTERS_END - &__FUZZ_COUNTERS_START); + + /* And map the shm over the counter region */ + rptr = mmap(&__FUZZ_COUNTERS_START, + &__FUZZ_COUNTERS_END - &__FUZZ_COUNTERS_START, + PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, fd, 0); + + close(fd); + + if (!rptr) { + perror("Error: "); + exit(1); + } +} + + diff --git a/tests/qtest/fuzz/fork_fuzz.h b/tests/qtest/fuzz/fork_fuzz.h new file mode 100644 index 0000000000..9ecb8b58ef --- /dev/null +++ b/tests/qtest/fuzz/fork_fuzz.h @@ -0,0 +1,23 @@ +/* + * Fork-based fuzzing helpers + * + * Copyright Red Hat Inc., 2019 + * + * Authors: + * Alexander Bulekov + * + * This work is licensed under the terms of the GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + * + */ + +#ifndef FORK_FUZZ_H +#define FORK_FUZZ_H + +extern uint8_t __FUZZ_COUNTERS_START; +extern uint8_t __FUZZ_COUNTERS_END; + +void counter_shm_init(void); + +#endif + diff --git a/tests/qtest/fuzz/fork_fuzz.ld b/tests/qtest/fuzz/fork_fuzz.ld new file mode 100644 index 0000000000..b23a59f194 --- /dev/null +++ b/tests/qtest/fuzz/fork_fuzz.ld @@ -0,0 +1,37 @@ +/* We adjust linker script modification to place all of the stuff that needs to + * persist across fuzzing runs into a contiguous seciton of memory. Then, it is + * easy to re-map the counter-related memory as shared. +*/ + +SECTIONS +{ + .data.fuzz_start : ALIGN(4K) + { + __FUZZ_COUNTERS_START = .; + __start___sancov_cntrs = .; + *(_*sancov_cntrs); + __stop___sancov_cntrs = .; + + /* Lowest stack counter */ + *(__sancov_lowest_stack); + } + .data.fuzz_ordered : + { + /* Coverage counters. They're not necessary for fuzzing, but are useful + * for analyzing the fuzzing performance + */ + __start___llvm_prf_cnts = .; + *(*llvm_prf_cnts); + __stop___llvm_prf_cnts = .; + + /* Internal Libfuzzer TracePC object which contains the ValueProfileMap */ + FuzzerTracePC*(.bss*); + } + .data.fuzz_end : ALIGN(4K) + { + __FUZZ_COUNTERS_END = .; + } +} +/* Dont overwrite the SECTIONS in the default linker script. Instead insert the + * above into the default script */ +INSERT AFTER .data;