From patchwork Mon Jul 8 13:22:35 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 168642 Delivered-To: patch@linaro.org Received: by 2002:ac9:6410:0:0:0:0:0 with SMTP id r16csp6548386ock; Mon, 8 Jul 2019 06:25:38 -0700 (PDT) X-Google-Smtp-Source: APXvYqxEU29+CTd3KwHlnmSQkvvywlsvXWEVpLavVmYsDk4TL1D1y0DdYNRD5rAtoGkj+08BAAA1 X-Received: by 2002:a17:906:43c9:: with SMTP id j9mr16173471ejn.248.1562592338193; Mon, 08 Jul 2019 06:25:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1562592338; cv=none; d=google.com; s=arc-20160816; b=C8sWLFFjdg8hSKjUK6dRmKo40jmGrRj8WRKZWbk4nEHOh8wQrvNx7eCayBgx+EZqiA IAQRuYOBX1AcRKo1XnGCmgv3Ux1kAb1SLd5zwVBOYb4FwZ+qbH6hcHeMot16YGyp/IQ4 ntRYu5z1sTGQc/JKt2KNJD/CDYtCd6BAafqvJQjp9AhQ/+sC2xb5fpuMeKHXupHKUdfh soJJ+hm+H/df4qRYpXXWpDobc8nC4PLVR0Y802+HU2m82jkrhVJ/eCSJJfKzxNKGya/L n+H5ZfVHRR36peQ555iub451XOMUrBmAnwIYjG7QTPwZKCNvW95W2ou9Cf9WWNoXY4xW K/bA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:subject :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:to:from:dkim-signature; bh=g9eAQagE0QheHxHPruslRXBJov60L65Bz5mc5X82Q1s=; b=tXtkoWnCxaPgF8z4mhdMzR3YtYPpswJdOuvcVbgYNFPmwfkyBqM+OxZCRW31IqYCCu /AaGyHMA4t5wEDp2/buyvdzetRDSx2PIaBsOwbTAbpNe4vzf2edPC8fGXVvKKH5401bX +J9livSKKo21jFHZ49rqfqzoNoYQK74rOq54U0oSfJ4Mm8x9GcTBI5uky5k78dk6e/DW 7ikPaF7mTtxWEtRoZeUojw+mRnDmhH3ou/Vv09WIOUHsTlUL3CJyVScqOIRGKkzK97Gm zK5b/l2EMexk1/IAFAyOP7RoqYuGEMYBHRk7/S+RPte68wWsFsXFxMWkNmPn93U6ra2Q Iknw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=J7wnSgeA; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id q54si13845205edd.370.2019.07.08.06.25.37 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 08 Jul 2019 06:25:38 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=J7wnSgeA; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:41718 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hkTeD-0000MQ-7X for patch@linaro.org; Mon, 08 Jul 2019 09:25:37 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:52276) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hkTbQ-000619-47 for qemu-devel@nongnu.org; Mon, 08 Jul 2019 09:22:45 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hkTbO-0003xt-Eb for qemu-devel@nongnu.org; Mon, 08 Jul 2019 09:22:44 -0400 Received: from mail-wm1-x344.google.com ([2a00:1450:4864:20::344]:51867) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hkTbO-0003x3-73 for qemu-devel@nongnu.org; Mon, 08 Jul 2019 09:22:42 -0400 Received: by mail-wm1-x344.google.com with SMTP id 207so15798055wma.1 for ; Mon, 08 Jul 2019 06:22:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=g9eAQagE0QheHxHPruslRXBJov60L65Bz5mc5X82Q1s=; b=J7wnSgeATIQ2AvmHjvU8KPSc540FeoJA9oP0Jv73H3zKYojxl/5ZNRQxJR369N6/0S Qzhtrsi+AzpaNVsbhvG/xd9wv87Fi6HqA4mw85nfzsm368cZa+QgGtG8PWum2efvUnxz CG/EF1Q738uwONJ7ZjBCYL7PFliYoiciXve9aMJMe6DybpPnf+L+5xVgTEUOEjQeQ0p2 cGEM9CQSNq3t1oVmKcBWZF+UwgKrw985/U1rJHHFIgmyIUuGNDPomL9BsmwfYzQWCeaJ l+hMH1iU5DufV6n5AZGHjVj6KpVraLsbHeUunJhdPgk+t9pl//WlMMNyPNP0xRK6BGXH CYUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=g9eAQagE0QheHxHPruslRXBJov60L65Bz5mc5X82Q1s=; b=hTjeKkhoNHG6iwnD5xREOneGUn6i/CzENKiLYrTxb+IEhLWYdVX7oYSIkUUTV+J7Tj PSktOK5G09pYTKEz/EY9jbipOdHKYKU9x0UzxSj/X9++AOt9xtJdFBy+/4v5vmHPCwQj 5HSsOKZBV6ifJsGkthYYjWtYQFlJjPKcc5vIGbe7JxRJ7HPGAyqOG5HvSmVJ3a6wKpTQ 3Tkadubbtc7GQYS8oBHTmqKUXsa1QouiylHCMejP/Hem8cLA+fWbEo6BYmlemGbWlAAq aGcQIZCdCkktQtdv8NXh/kVpcmmRY6baBGGhsHClscfemzjmCn0d7BDuvI/NTpTEpskH t6zw== X-Gm-Message-State: APjAAAUMkuV4KIGftfDoX/x+6sDOjseihkrl97YAckABHghIn6RT81X+ pzeLp/E95bSLVbYwpMAfYhJn14zmCSkE6A== X-Received: by 2002:a05:600c:303:: with SMTP id q3mr17365420wmd.130.1562592160921; Mon, 08 Jul 2019 06:22:40 -0700 (PDT) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [81.2.115.148]) by smtp.gmail.com with ESMTPSA id v15sm13773744wrt.25.2019.07.08.06.22.40 for (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Mon, 08 Jul 2019 06:22:40 -0700 (PDT) From: Peter Maydell To: qemu-devel@nongnu.org Date: Mon, 8 Jul 2019 14:22:35 +0100 Message-Id: <20190708132237.7911-3-peter.maydell@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190708132237.7911-1-peter.maydell@linaro.org> References: <20190708132237.7911-1-peter.maydell@linaro.org> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::344 Subject: [Qemu-devel] [PULL 2/4] tests/migration-test: Fix read off end of aarch64_kernel array 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: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" The test aarch64 kernel is in an array defined with unsigned char aarch64_kernel[] = { [...] } which means it could be any size; currently it's quite small. However we write it to a file using init_bootfile(), which writes exactly 512 bytes to the file. This will break if we ever end up with a kernel larger than that, and will read garbage off the end of the array in the current setup where the kernel is smaller. Make init_bootfile() take an argument giving the length of the data to write. This allows us to use it for all architectures (previously s390 had a special-purpose init_bootfile_s390x which hardcoded the file to write so it could write the correct length). We assert that the x86 bootfile really is exactly 512 bytes as it should be (and as we were previously just assuming it was). This was detected by the clang-7 asan: ==15607==ERROR: AddressSanitizer: global-buffer-overflow on address 0x55a796f51d20 at pc 0x55a796b89c2f bp 0x7ffc58e89160 sp 0x7ffc58e88908 READ of size 512 at 0x55a796f51d20 thread T0 #0 0x55a796b89c2e in fwrite (/home/petmay01/linaro/qemu-from-laptop/qemu/build/sanitizers/tests/migration-test+0xb0c2e) #1 0x55a796c46492 in init_bootfile /home/petmay01/linaro/qemu-from-laptop/qemu/tests/migration-test.c:99:5 #2 0x55a796c46492 in test_migrate_start /home/petmay01/linaro/qemu-from-laptop/qemu/tests/migration-test.c:593 #3 0x55a796c44101 in test_baddest /home/petmay01/linaro/qemu-from-laptop/qemu/tests/migration-test.c:854:9 #4 0x7f906ffd3cc9 (/usr/lib/x86_64-linux-gnu/libglib-2.0.so.0+0x72cc9) #5 0x7f906ffd3bfa (/usr/lib/x86_64-linux-gnu/libglib-2.0.so.0+0x72bfa) #6 0x7f906ffd3bfa (/usr/lib/x86_64-linux-gnu/libglib-2.0.so.0+0x72bfa) #7 0x7f906ffd3ea1 in g_test_run_suite (/usr/lib/x86_64-linux-gnu/libglib-2.0.so.0+0x72ea1) #8 0x7f906ffd3ec0 in g_test_run (/usr/lib/x86_64-linux-gnu/libglib-2.0.so.0+0x72ec0) #9 0x55a796c43707 in main /home/petmay01/linaro/qemu-from-laptop/qemu/tests/migration-test.c:1187:11 #10 0x7f906e9abb96 in __libc_start_main /build/glibc-OTsEL5/glibc-2.27/csu/../csu/libc-start.c:310 #11 0x55a796b6c2d9 in _start (/home/petmay01/linaro/qemu-from-laptop/qemu/build/sanitizers/tests/migration-test+0x932d9) Signed-off-by: Peter Maydell Reviewed-by: Laurent Vivier Reviewed-by: Philippe Mathieu-Daudé Message-id: 20190702150311.20467-1-peter.maydell@linaro.org --- tests/migration-test.c | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) -- 2.20.1 diff --git a/tests/migration-test.c b/tests/migration-test.c index 0cd014dbe51..b6434628e1c 100644 --- a/tests/migration-test.c +++ b/tests/migration-test.c @@ -91,23 +91,13 @@ static const char *tmpfs; */ #include "tests/migration/i386/a-b-bootblock.h" #include "tests/migration/aarch64/a-b-kernel.h" - -static void init_bootfile(const char *bootpath, void *content) -{ - FILE *bootfile = fopen(bootpath, "wb"); - - g_assert_cmpint(fwrite(content, 512, 1, bootfile), ==, 1); - fclose(bootfile); -} - #include "tests/migration/s390x/a-b-bios.h" -static void init_bootfile_s390x(const char *bootpath) +static void init_bootfile(const char *bootpath, void *content, size_t len) { FILE *bootfile = fopen(bootpath, "wb"); - size_t len = sizeof(s390x_elf); - g_assert_cmpint(fwrite(s390x_elf, len, 1, bootfile), ==, 1); + g_assert_cmpint(fwrite(content, len, 1, bootfile), ==, 1); fclose(bootfile); } @@ -537,7 +527,9 @@ static int test_migrate_start(QTestState **from, QTestState **to, got_stop = false; bootpath = g_strdup_printf("%s/bootsect", tmpfs); if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) { - init_bootfile(bootpath, x86_bootsect); + /* the assembled x86 boot sector should be exactly one sector large */ + assert(sizeof(x86_bootsect) == 512); + init_bootfile(bootpath, x86_bootsect, sizeof(x86_bootsect)); extra_opts = use_shmem ? get_shmem_opts("150M", shmem_path) : NULL; cmd_src = g_strdup_printf("-machine accel=%s -m 150M" " -name source,debug-threads=on" @@ -555,7 +547,7 @@ static int test_migrate_start(QTestState **from, QTestState **to, start_address = X86_TEST_MEM_START; end_address = X86_TEST_MEM_END; } else if (g_str_equal(arch, "s390x")) { - init_bootfile_s390x(bootpath); + init_bootfile(bootpath, s390x_elf, sizeof(s390x_elf)); extra_opts = use_shmem ? get_shmem_opts("128M", shmem_path) : NULL; cmd_src = g_strdup_printf("-machine accel=%s -m 128M" " -name source,debug-threads=on" @@ -590,7 +582,7 @@ static int test_migrate_start(QTestState **from, QTestState **to, start_address = PPC_TEST_MEM_START; end_address = PPC_TEST_MEM_END; } else if (strcmp(arch, "aarch64") == 0) { - init_bootfile(bootpath, aarch64_kernel); + init_bootfile(bootpath, aarch64_kernel, sizeof(aarch64_kernel)); extra_opts = use_shmem ? get_shmem_opts("150M", shmem_path) : NULL; cmd_src = g_strdup_printf("-machine virt,accel=%s,gic-version=max " "-name vmsource,debug-threads=on -cpu max "