From nobody Sun Nov 24 00:34:24 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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; dmarc=pass(p=quarantine dis=none) header.from=kernel.org ARC-Seal: i=1; a=rsa-sha256; t=1727633303; cv=none; d=zohomail.com; s=zohoarc; b=Ksm9HWhgM+SieK+7g2Lfkf+5AQT2Wu7Yt1v0Yx+UeNJ7YxKsv7hcNCjDPOddZ0AwCut5T8UQMfdbXnTcW239K4kSEK9z9aWl5/WjUtkLI7yl152T0OCNHHPDc6slA8//1XBC3dTBZmIB8hNkgfKl/eY9o9acu7j72i7uI5kQX6A= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1727633303; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=BoHkup0+dbt0qsuzAEALLpd3vKJCIh/d62/Nf/78Nos=; b=GRiaRc0uOYLKsEDznakFP/AJKX01QU1TCdmjJsq7R4wsvXRADIeHA0uVFGew4aPpjy/y62A19cTHfd6H+N35g7eabJLHwcy3dBZb/pmS5bnO0AjvgmgH8WSDCigia7Bgmwuj67PdJ+BqhVXfnbdwLqgGZ10qgd30GJ/AjkD4vlc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1727633303878398.7429661753555; Sun, 29 Sep 2024 11:08:23 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1suyKf-0003Yz-Rz; Sun, 29 Sep 2024 14:07:46 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1suyKV-0003WM-K1 for qemu-devel@nongnu.org; Sun, 29 Sep 2024 14:07:37 -0400 Received: from dfw.source.kernel.org ([2604:1380:4641:c500::1]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1suyKT-0008UN-FI for qemu-devel@nongnu.org; Sun, 29 Sep 2024 14:07:34 -0400 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 171295C3A55; Sun, 29 Sep 2024 18:07:28 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D77D4C4CEC6; Sun, 29 Sep 2024 18:07:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1727633251; bh=gaTEkzSDeytW6SwTF1R7+8EhibjOouFicNyg1NxbWWo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jJtk6rSCHFMzfVhPopq9giLbhUOa9I0ItG/GJLOfbpfmK7dGzJrO+x1KWhMz0NzNa v8OUCrMQNUlAO5smFpUjF5CurkoYND5q6MlJXe2we1GECByDfWtduPGi1W5lyMyUuF xDrjd5ca2BKhPApIx4aMnhCpqvMv9fX7bM8HuCJ2G/tGKczUfbhLr3ANAw7SwXA1fd AbAhZzVDw8+7UasUSlfasDh4OkLt1otwcg5gZCZYcdFuxTEa2Tjza5QJNjqYCLn9xZ fCB9DqE2DaJgJJYPcY1nSL9SzjVPJ5Oj+EehAYWYxdb/fm8CtAHyGMD45bKuftNEoo +oAQBp7mCaEIA== From: Ard Biesheuvel To: qemu-devel@nongnu.org Cc: pbonzini@redhat.com, Ard Biesheuvel Subject: [PATCH v2 1/2] hw/elf_ops: Implement missing endian swabbing for ELF notes Date: Sun, 29 Sep 2024 20:06:57 +0200 Message-Id: <20240929180659.3598-2-ardb@kernel.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20240929180659.3598-1-ardb@kernel.org> References: <20240929180659.3598-1-ardb@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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: pass client-ip=2604:1380:4641:c500::1; envelope-from=ardb@kernel.org; helo=dfw.source.kernel.org X-Spam_score_int: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.095, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @kernel.org) X-ZM-MESSAGEID: 1727633305440116600 Content-Type: text/plain; charset="utf-8" ELF notes have type and size fields in the elf_note header that need to be swabbed before use if the host endianness differs from the endianness of the ELF binary. Signed-off-by: Ard Biesheuvel --- include/hw/elf_ops.h.inc | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/include/hw/elf_ops.h.inc b/include/hw/elf_ops.h.inc index 9c35d1b9da..8b563db2a3 100644 --- a/include/hw/elf_ops.h.inc +++ b/include/hw/elf_ops.h.inc @@ -41,6 +41,13 @@ static void glue(bswap_shdr, SZ)(struct elf_shdr *shdr) bswapSZs(&shdr->sh_entsize); } =20 +static void glue(bswap_nhdr, SZ)(struct elf_note *nhdr) +{ + bswap32s(&nhdr->n_namesz); + bswap32s(&nhdr->n_descsz); + bswap32s(&nhdr->n_type); +} + static void glue(bswap_sym, SZ)(struct elf_sym *sym) { bswap32s(&sym->st_name); @@ -275,7 +282,8 @@ fail: static struct elf_note *glue(get_elf_note_type, SZ)(struct elf_note *nhdr, elf_word note_size, elf_word phdr_align, - elf_word elf_note_type) + elf_word elf_note_type, + int must_swab) { elf_word nhdr_size =3D sizeof(struct elf_note); elf_word elf_note_entry_offset =3D 0; @@ -287,6 +295,9 @@ static struct elf_note *glue(get_elf_note_type, SZ)(str= uct elf_note *nhdr, return NULL; } =20 + if (must_swab) { + glue(bswap_nhdr, SZ)(nhdr); + } note_type =3D nhdr->n_type; while (note_type !=3D elf_note_type) { nhdr_namesz =3D nhdr->n_namesz; @@ -306,6 +317,9 @@ static struct elf_note *glue(get_elf_note_type, SZ)(str= uct elf_note *nhdr, =20 /* skip to the next ELF Note entry */ nhdr =3D (void *)nhdr + elf_note_entry_offset; + if (must_swab) { + glue(bswap_nhdr, SZ)(nhdr); + } note_type =3D nhdr->n_type; } =20 @@ -603,7 +617,8 @@ static ssize_t glue(load_elf, SZ)(const char *name, int= fd, nhdr =3D (struct elf_note *)data; assert(translate_opaque !=3D NULL); nhdr =3D glue(get_elf_note_type, SZ)(nhdr, file_size, ph->p_al= ign, - *(uint64_t *)translate_opaq= ue); + *(uint64_t *)translate_opaq= ue, + must_swab); if (nhdr !=3D NULL) { elf_note_fn((void *)nhdr, (void *)&ph->p_align, SZ =3D=3D = 64); } --=20 2.39.5 From nobody Sun Nov 24 00:34:24 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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; dmarc=pass(p=quarantine dis=none) header.from=kernel.org ARC-Seal: i=1; a=rsa-sha256; t=1727633328; cv=none; d=zohomail.com; s=zohoarc; b=QP6D+0G7+QtH6+lJjwYJfU2N0ugJ/we2GVDmcNOxvqYTqfpVqhE9Lflk5WJLWpAWbevGFSDhdAdettbfl/blKOp7X092T1KR2p9/Pp9j0M1yjE90O5biF/5kzwDsc1GXeDgGjb9LO10a+u4/UGl1OUOpRNqohL6ckOIaCqXvuP8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1727633328; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=wrM2oxNewyMOHa0AWMa0svl8RqnzGJnSUxAxHZkJRJ0=; b=LQubY/H1RQ6bnvlDH0pApya8HYgLFV7SanoO56AlF4H1kRnWO57ibBCsrTPsh2LwKXZtxzEy03Tu3p4RxrQ7CuyW+NfbExiqTl4wVuVogOtlUqiYUT1f3pptpV5zLRDMLEqeMTgJzsmPzjwoPn45nJXTPHww40XmjljUflWaKSs= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1727633327970636.5163070425194; Sun, 29 Sep 2024 11:08:47 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1suyKl-0003k3-39; Sun, 29 Sep 2024 14:07:51 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1suyKa-0003Xc-Nb for qemu-devel@nongnu.org; Sun, 29 Sep 2024 14:07:41 -0400 Received: from dfw.source.kernel.org ([2604:1380:4641:c500::1]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1suyKZ-0008Ul-1a for qemu-devel@nongnu.org; Sun, 29 Sep 2024 14:07:40 -0400 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 48B7E5C35DE; Sun, 29 Sep 2024 18:07:34 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1307CC4CEC5; Sun, 29 Sep 2024 18:07:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1727633258; bh=zugxR724dlw5HRjvz+NLeFunLq6E0krl84kUd9AxbSk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=o/6tOhddQLXy22KKCtRvIH1q4jyjRlkiwLODq8IrHvnGVPEEf75Jk/gDgIJhMQ++g DzgT03CFq8MKsujusipgk/qihC6mGhGw2H5wwnGtW9bek4di5nwsM8D6LsjVfl0Ooi uWmofnICXJAghxSHXMiJfBcJuO3TC0njWXreq/cZr3TyYRBEt97/lzZYbnp4jPgBid kpwcCDl3l1Kgt9dsKmnitjcdhy1kUn/MR6kQZZCWZK08wKTEjfUgFXjQhMOmg62Ih+ qcVrvW9L/BHoie8s6R/DvbFolTb6d+dgrPcEqLupVF6+G6xgy9B672rMKkULJg/IRl aHAvjHC7RQ6Vw== From: Ard Biesheuvel To: qemu-devel@nongnu.org Cc: pbonzini@redhat.com, Ard Biesheuvel Subject: [PATCH v2 2/2] hw/x86: Always treat the PVH entrypoint as a 32-bit LE field Date: Sun, 29 Sep 2024 20:06:58 +0200 Message-Id: <20240929180659.3598-3-ardb@kernel.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20240929180659.3598-1-ardb@kernel.org> References: <20240929180659.3598-1-ardb@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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: pass client-ip=2604:1380:4641:c500::1; envelope-from=ardb@kernel.org; helo=dfw.source.kernel.org X-Spam_score_int: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.095, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @kernel.org) X-ZM-MESSAGEID: 1727633329494116600 Content-Type: text/plain; charset="utf-8" The PVH entrypoint is entered in 32-bit mode, and is documented as being a 32-bit field. Linux happens to widen the field in the ELF note to 64 bits so treating it as a 64-bit field works for booting the kernel. However, Xen documents the ELF note with the following example ELFNOTE(Xen, XEN_ELFNOTE_PHYS32_ENTRY, .long, xen_start32) and uses .long in the code as well, and so reading more than 32 bits here is risky. And dereferencing a size_t* in portable code is just bizarre, so let's use a uint32_t specifically in all cases here. While at it, read the field as little-endian explicitly, so things work as expected on big endian hosts too. Signed-off-by: Ard Biesheuvel --- hw/i386/x86-common.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/hw/i386/x86-common.c b/hw/i386/x86-common.c index 992ea1f25e..44e5c365f1 100644 --- a/hw/i386/x86-common.c +++ b/hw/i386/x86-common.c @@ -539,7 +539,7 @@ DeviceState *ioapic_init_secondary(GSIState *gsi_state) */ static uint64_t read_pvh_start_addr(void *arg1, void *arg2, bool is64) { - size_t *elf_note_data_addr; + void *elf_note_data_addr; =20 /* Check if ELF Note header passed in is valid */ if (arg1 =3D=3D NULL) { @@ -555,8 +555,6 @@ static uint64_t read_pvh_start_addr(void *arg1, void *a= rg2, bool is64) elf_note_data_addr =3D ((void *)nhdr64) + nhdr_size64 + QEMU_ALIGN_UP(nhdr_namesz, phdr_align); - - pvh_start_addr =3D *elf_note_data_addr; } else { struct elf32_note *nhdr32 =3D (struct elf32_note *)arg1; uint32_t nhdr_size32 =3D sizeof(struct elf32_note); @@ -566,10 +564,9 @@ static uint64_t read_pvh_start_addr(void *arg1, void *= arg2, bool is64) elf_note_data_addr =3D ((void *)nhdr32) + nhdr_size32 + QEMU_ALIGN_UP(nhdr_namesz, phdr_align); - - pvh_start_addr =3D *(uint32_t *)elf_note_data_addr; } =20 + pvh_start_addr =3D ldl_le_p(elf_note_data_addr); return pvh_start_addr; } =20 --=20 2.39.5