From patchwork Wed Apr 14 10:58:38 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Vivier X-Patchwork-Id: 1466128 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=) 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 4FKzxX30Fzz9sPf for ; Wed, 14 Apr 2021 20:59:11 +1000 (AEST) Received: from localhost ([::1]:40792 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lWdEi-00022T-LI for incoming@patchwork.ozlabs.org; Wed, 14 Apr 2021 06:59:08 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33110) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lWdER-00022B-Sb for qemu-devel@nongnu.org; Wed, 14 Apr 2021 06:58:51 -0400 Received: from mout.kundenserver.de ([212.227.17.10]:39101) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lWdEP-0005U5-BF for qemu-devel@nongnu.org; Wed, 14 Apr 2021 06:58:51 -0400 Received: from localhost.localdomain ([82.142.18.94]) by mrelayeu.kundenserver.de (mreue108 [212.227.15.183]) with ESMTPSA (Nemesis) id 1Mysa4-1lj5Zz2SQc-00vyrf; Wed, 14 Apr 2021 12:58:41 +0200 From: Laurent Vivier To: qemu-devel@nongnu.org Subject: [PATCH] hw/elf_ops: clear uninitialized segment space Date: Wed, 14 Apr 2021 12:58:38 +0200 Message-Id: <20210414105838.205019-1-laurent@vivier.eu> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 X-Provags-ID: V03:K1:M3fe91IA2xSYigQOk49iE22uRYlE0CUTdrFsULb6rHCbzUoAaw0 SDrQUcqSEWw/HipjHr0owHrZksyh+LF0vjeJIqCVYOxurDUJ/HjkXAAJx8YYJQfcKik9sIZ T+HrkXKPEZum64cQqm0txeW8pj7/M9CpZ7GyEVgHFLuTdaPzvhCWPB7eMS1ZC7aiR5C6Nqp GGy6WDmfFkxlyL8udQjNA== X-UI-Out-Filterresults: notjunk:1;V03:K0:t20dXKRM/m4=:Ti/gGoXKHTMxeBPPuGtuL9 f5hbITFqY8OcMtz6D/cIZE7ZTPsTeRo2V3/qaS1uVguqTKygzL8k0aqVyEhbVRAcRqN25PGfd lz4FRjw1m7+JSfcZsn8dbwyUP3QHNYXTUtSqER1Wguo1DTcSKTO316UIo1D6ndBjGi8EhV70E d4XFlv1fBvFS4PA12tnW/+9WdYUDXqoT4Ej12xharQUSw4+zVSIHGqv9+uJ++BFjONkO/giuo x+SXfv4peP3kp/D3pkbD0FUQ4sC/bSFdmWhMSq8o2ctAl+ujUijiCWNH6Oq7lPG6BiknF5Z9A 07u4ghiVqD5oMJQHPdcYnh+wp08OYG/i30D2GB7SBb4N0MOD/o55zydBO9ylp7494QA/yLOwC qvTjaC2pYLir9cSMzA5557kFCjt3iSbfPWDZ1PY8cKjSMP9i2kM2OXRcty4SkiBmcExzayHvQ QLXY77iILnmk1tbnPiVE2KOYvE75sPMZqk7aoRorAdyqJ0wFMKHQXMv/EoDmP+KqLjiZw1bO8 zaavdAm4TzTaV/6V4OfSHY= Received-SPF: none client-ip=212.227.17.10; envelope-from=laurent@vivier.eu; helo=mout.kundenserver.de 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, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham 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: Peter Maydell , =?utf-8?q?Philippe_Mathieu-Dau?= =?utf-8?q?d=C3=A9?= , Laurent Vivier , Stefano Garzarella Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" When the mem_size of the segment is bigger than the file_size, and if this space doesn't overlap another segment, it needs to be cleared. This bug is very similar to the one we had for linux-user, 22d113b52f41 ("linux-user: Fix loading of BSS segments"), where .bss section is encoded as an extension of the the data one by setting the segment p_memsz > p_filesz. Signed-off-by: Laurent Vivier --- include/hw/elf_ops.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/include/hw/elf_ops.h b/include/hw/elf_ops.h index 6ee458e7bc3c..e3dcee3ee349 100644 --- a/include/hw/elf_ops.h +++ b/include/hw/elf_ops.h @@ -562,6 +562,23 @@ static int glue(load_elf, SZ)(const char *name, int fd, if (res != MEMTX_OK) { goto fail; } + /* + * We need to zero'ify the space that is not copied + * from file + */ + if (file_size < mem_size) { + static uint8_t zero[4096]; + uint64_t i; + for (i = file_size; i < mem_size; i += sizeof(zero)) { + res = address_space_write( + as ? as : &address_space_memory, + addr + i, MEMTXATTRS_UNSPECIFIED, + zero, MIN(sizeof(zero), mem_size - i)); + if (res != MEMTX_OK) { + goto fail; + } + } + } } }