From nobody Fri Nov 14 15:21:03 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1588608584; cv=none; d=zohomail.com; s=zohoarc; b=nv+pXMGzLi6R3dO1nSEGO58c6o5BHC5JdSztlxgg+zrHLwLOosxUV/gAdWM3WRpOmWWz+T6JNsPLZeEgm0qRPKPrdKrElJ7h0v+v7X9HIL02CZvGw/5FHh2vxOlrRX3pYA/LemTYkE3GTzBXSYSYaJ3GuRVPBt5VTqvKv12b6O8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1588608584; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=i4PZBQWT1lWk1owaBXRR7ha4wIQxrw3n7urbluKvKBM=; b=OKp49Y624MRbUSeJaQRO7yfydQgAdNAP9rmFXRMg1YFoO3YjDbcrFRsLJyIVKjW8gk4bug4wLe51NFZO1AOP7j96ulrP8iwqiXuJ0VYJA5WpjwyCjNk4ZMM+5svYVrqmdeCyuPwYFborXlqtALV/HNliaPKn95LN4ObR75wE+TI= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1588608584549330.3440738953416; Mon, 4 May 2020 09:09:44 -0700 (PDT) Received: from localhost ([::1]:60034 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jVdf5-0001A3-7h for importer@patchew.org; Mon, 04 May 2020 12:09:43 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:38060) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jVdVy-0001PB-CE; Mon, 04 May 2020 12:00:18 -0400 Received: from mout.kundenserver.de ([212.227.126.135]:44609) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1jVdVw-0005nY-5U; Mon, 04 May 2020 12:00:17 -0400 Received: from localhost.localdomain ([82.252.135.106]) by mrelayeu.kundenserver.de (mreue010 [212.227.15.167]) with ESMTPSA (Nemesis) id 1MA7X0-1jPXum20HS-00Bccv; Mon, 04 May 2020 18:00:06 +0200 From: Laurent Vivier To: qemu-devel@nongnu.org Subject: [PULL v2 07/19] elf_ops: Don't try to g_mapped_file_unref(NULL) Date: Mon, 4 May 2020 17:59:44 +0200 Message-Id: <20200504155956.380695-8-laurent@vivier.eu> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200504155956.380695-1-laurent@vivier.eu> References: <20200504155956.380695-1-laurent@vivier.eu> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:f+vpvirZHuRjvIciBk9UcbBzY51RKB7hnElLVSfcmmYBp6mxs0J HxuKMLIMnjBJhT31m5V0SPS6c2U+ywNe1Y12Als2WuZl9cRnh5frwzmijdOwUzeUduhM6HW qnLDcKEX4p7BosWThczVeDqiBCjiCxuG3KWYCX3KT191c+B+JTCQsLc/NFrth278ORe088P mWQAqwPaLzlEvYeIfnNlA== X-UI-Out-Filterresults: notjunk:1;V03:K0:Zt1BNX0Nszg=:87pJDOrPwdPruObCNNdWMh 0FrqtSD0/+QZqvoGPyoPy/tcGcv1ErgfL5gj77iMStR3cM/q+Donm7I/RPGvszC8qwfcym6W1 O0CvlpIDAyF45xKkwFW9YWJPbbPWq57rtv4H6KpspF9IQI81Vba1FpisjCwtZYntEK+a1QalQ VTeIVA+60uE8JgTtAvf7cBtYb9Fmkm/fdYlwsF/NyxtLzlx3tqUa+I/lD7TTRA+fsyoSMnbTO xTmh6eZTcs4DSSXwPN3pf1CvhLS8JdOx4kDX6VBF9HJlJhTEWnkhJkIs9vGG9QZhDd1zU7GEK ucUVBqTMOGqmJaNDrBhyRiwtz7B0AHcyHsNqC/ISIdEP3aH8se9KKShG54LJ5y6lBVRGVX6fw ea7vDqy/cdZkm5Jy5EIIlXdRz78V/HoBGs4FtfKVUj6QjiK/+2sikKziQSU2W30pCHTUL0ONp lqpqUR8Xi4Zk36AAwGUpIdA6DbgDZu5AeGPljWWREFKU2Eju+DIxs5vDxgj35Y+mZDzBNK4Jo npKauCzU+3NgP6kI/GMCQtI2Fusn1Ehfw8zjCp/H7KS53PMUDH8jiUy+qX7FmLNtmiQq5BKjc Awx9ZTpCRRs3yclNDe2t1h6vZI3AJT14AsR2KQl9H3yZbMbVwDLvd4TLvvxngcDG4rK5UM935 by9Jk/Z0eFRZMlqJgQ4bzlAkNSXBlp4bwDI51nRIjCi7hogzAqp81FabWEYbkV+/a7g91XXR6 OxPPKTQlffT4e8MbZSMzx9fXK1I01C04tTO26oJA58eEpfkGXrM5Wt+DF4/7l2/u5TpQFnpPl OgA/iNujadxhb5F1oKzgLcckFRd6eaG+/mw8+L8Ln9WRiDgEUBllYrAM+QbSIZP7q8xLDsN Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: none client-ip=212.227.126.135; envelope-from=laurent@vivier.eu; helo=mout.kundenserver.de X-detected-operating-system: by eggs.gnu.org: First seen = 2020/05/04 11:59:59 X-ACL-Warn: Detected OS = Linux 3.11 and newer 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, URIBL_BLOCKED=0.001 autolearn=_AUTOLEARN 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 , qemu-trivial@nongnu.org, Michael Tokarev , Laurent Vivier , Randy Yates , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Stefano Garzarella Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" From: Peter Maydell Calling g_mapped_file_unref() on a NULL pointer is not valid, and glib will assert if you try it. $ qemu-system-arm -M virt -display none -device loader,file=3D/tmp/bad.elf qemu-system-arm: -device loader,file=3D/tmp/bad.elf: GLib: g_mapped_file_un= ref: assertion 'file !=3D NULL' failed (One way to produce an ELF file that fails like this is to copy just the first 16 bytes of a valid ELF file; this is sufficient to fool the code in load_elf_ram_sym() into thinking it's an ELF file and calling load_elf32() or load_elf64().) The failure-exit path in load_elf can be reached from various points in execution, and for some of those we haven't yet called g_mapped_file_new_from_fd(). Add a condition to the unref call so we only call it if we successfully created the GMappedFile to start with. This will fix the assertion; for the specific case of the generic loader it will then fall back from "guess this is an ELF file" to "maybe it's a uImage or a hex file" and eventually to "just load as a raw data file". Reported-by: Randy Yates Signed-off-by: Peter Maydell Reviewed-by: Philippe Mathieu-Daud=C3=A9 Reviewed-by: Stefano Garzarella Message-Id: <20200423202011.32686-1-peter.maydell@linaro.org> Signed-off-by: Laurent Vivier --- include/hw/elf_ops.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/include/hw/elf_ops.h b/include/hw/elf_ops.h index e0bb47bb678d..398a4a2c85bb 100644 --- a/include/hw/elf_ops.h +++ b/include/hw/elf_ops.h @@ -606,7 +606,9 @@ static int glue(load_elf, SZ)(const char *name, int fd, *highaddr =3D (uint64_t)(elf_sword)high; ret =3D total_size; fail: - g_mapped_file_unref(mapped_file); + if (mapped_file) { + g_mapped_file_unref(mapped_file); + } g_free(phdr); return ret; } --=20 2.26.2