From nobody Tue Nov 26 04:43:08 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=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1710184977; cv=none; d=zohomail.com; s=zohoarc; b=LJh9ci+mM5dhQicXix2pqO5lPwKcOKci/winHolzfuyvO2OBJcDQORXW6uZ5vapk5nZs35m/wDXJctrvdq+9ne04P6FIDowXSbRGza8Q9Lz+Igq4F+6x25fhzs+bQy0GfKA04aS0iQnMVX7ofnGKVGDLM52+8c9xPCayUwWVJ0U= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1710184977; h=Content-Transfer-Encoding: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:Cc; bh=sD6QWErgcpioeXWdrsT2adMymrvCNHI9GMAxmhteL7o=; b=Sga2oZB3qb/2ZaTUqO8BiGy7h+3NgSBlD13nLEQAnRcXWQeAScqTrSdqZq64h2RK2Z0fEDznCF5tEzEXfAlj0IG7aC/BuTntppECGnD0u6VuFbn5G1helJCZBFDZqhYkt01TV82BHVmxl107t/IVxcAjDBy2DuRzhkW/EKxfbqo= 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=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1710184977840527.4392485718447; Mon, 11 Mar 2024 12:22:57 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rjl6d-0004f1-DH; Mon, 11 Mar 2024 15:14:40 -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 1rjl53-0003PG-1d for qemu-devel@nongnu.org; Mon, 11 Mar 2024 15:13:02 -0400 Received: from mail-wm1-x32b.google.com ([2a00:1450:4864:20::32b]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rjl4w-0000Nj-Bg for qemu-devel@nongnu.org; Mon, 11 Mar 2024 15:12:57 -0400 Received: by mail-wm1-x32b.google.com with SMTP id 5b1f17b1804b1-4132cbe26a9so6186415e9.0 for ; Mon, 11 Mar 2024 12:12:53 -0700 (PDT) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [2001:8b0:1d0::2]) by smtp.gmail.com with ESMTPSA id t10-20020a05600c450a00b004131388d948sm14425266wmo.0.2024.03.11.12.12.50 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Mar 2024 12:12:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1710184371; x=1710789171; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=sD6QWErgcpioeXWdrsT2adMymrvCNHI9GMAxmhteL7o=; b=y79PSPf6d6Ez0nbCDXw6So2eXrzGAZmyCc3NodLZ9Xwg9lS/Px/vvqqwdweWoOmKnh lXdCqtDRzNMWd9rscFmH2zO/bNU5qqaF2PObA7n9v8IUd6RypqHj9rJRaLfIbJbjY5h8 E7kDb2sfDic2m373CqxLd0836IJgp/r/L/cMo6SFaCRMIzR6nEFpUZVNJp7eSA7nnVNl fEdAJ8Gdcwn9MQ9T6PVyHDZwK90B97WmKpVd9HV7WfxTPgMipwWq1ynDr4CuxXUW1Owo KpdSlQRF2vxYc1ucFsE6vYbOBya9SdcfLBLdyrxIPmjMb546yJJ1SL+aU4x/VPnBHsY+ 14iA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710184371; x=1710789171; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=sD6QWErgcpioeXWdrsT2adMymrvCNHI9GMAxmhteL7o=; b=KXgDvH11N31sSniYlR6fMGXDBgKFvhyYcDJwyQxBGVNL3KJF7P73zBuqyDgWdR5NP/ 1lMJ3kAPmzQdiCUz5O+dUtyY41ciZWBtxvDiwN8Ph+PmDLWwd/cOrkd0VE/qmCD7Mb5w JIZT/YbCtQcvkf1QIZwXAKbjXyDOm6tu9NT3GAhDLK3BY1JrBk2gEuS3iGjA4+Gnc4y7 RS8ie778sYuQAU5TOArQfCn55ouk/3Wu+cUE6hgtu6V3+WCjTCkbVR+qyX5p01yC6OZ0 POKOBID6mwUQc7PeUGU58lIl19nGvXYbPXYYAM9O/6KLgtwhhNQvgnuZK/8DYTAGN06S de+w== X-Gm-Message-State: AOJu0YzvQeleEle1osYDI6lioc4WbPlzZeuFCWD2oNk7N/VETHLdUddE PqotU4YU7ArBN3gKQKRG5m9qULwNk/mMfbiAABs/OvdXuxhqg9lMOWbtJieZd6IV4222dF8vdv0 D X-Google-Smtp-Source: AGHT+IHdfrOsmDDP5mb1eGF62DCy38bW3TXaKO5KsYJEs3IFuL6WKN1CpkyGY1kKTKYuC2h1Y4AViw== X-Received: by 2002:a05:600c:3492:b0:413:2bcf:df81 with SMTP id a18-20020a05600c349200b004132bcfdf81mr2008103wmq.1.1710184371097; Mon, 11 Mar 2024 12:12:51 -0700 (PDT) From: Peter Maydell To: qemu-devel@nongnu.org Subject: [PULL 18/20] contrib/elf2dmp: Clamp QEMU note to file size Date: Mon, 11 Mar 2024 19:12:39 +0000 Message-Id: <20240311191241.4177990-19-peter.maydell@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240311191241.4177990-1-peter.maydell@linaro.org> References: <20240311191241.4177990-1-peter.maydell@linaro.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=2a00:1450:4864:20::32b; envelope-from=peter.maydell@linaro.org; helo=mail-wm1-x32b.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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 @linaro.org) X-ZM-MESSAGEID: 1710184978436100001 Content-Type: text/plain; charset="utf-8" From: Akihiko Odaki This fixes crashes with truncated dumps. Signed-off-by: Akihiko Odaki Reviewed-by: Peter Maydell Tested-by: Viktor Prutyanov Message-id: 20240307-elf2dmp-v4-18-4f324ad4d99d@daynix.com Signed-off-by: Peter Maydell --- contrib/elf2dmp/qemu_elf.c | 87 ++++++++++++++++++++++++-------------- 1 file changed, 55 insertions(+), 32 deletions(-) diff --git a/contrib/elf2dmp/qemu_elf.c b/contrib/elf2dmp/qemu_elf.c index 7d896cac5b1..8d750adf904 100644 --- a/contrib/elf2dmp/qemu_elf.c +++ b/contrib/elf2dmp/qemu_elf.c @@ -6,6 +6,7 @@ */ =20 #include "qemu/osdep.h" +#include "qemu/host-utils.h" #include "err.h" #include "qemu_elf.h" =20 @@ -15,36 +16,11 @@ #define ROUND_UP(n, d) (((n) + (d) - 1) & -(0 ? (n) : (d))) #endif =20 -#ifndef DIV_ROUND_UP -#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d)) -#endif - -#define ELF_NOTE_SIZE(hdr_size, name_size, desc_size) \ - ((DIV_ROUND_UP((hdr_size), 4) + \ - DIV_ROUND_UP((name_size), 4) + \ - DIV_ROUND_UP((desc_size), 4)) * 4) - int is_system(QEMUCPUState *s) { return s->gs.base >> 63; } =20 -static char *nhdr_get_name(Elf64_Nhdr *nhdr) -{ - return (char *)nhdr + ROUND_UP(sizeof(*nhdr), 4); -} - -static void *nhdr_get_desc(Elf64_Nhdr *nhdr) -{ - return nhdr_get_name(nhdr) + ROUND_UP(nhdr->n_namesz, 4); -} - -static Elf64_Nhdr *nhdr_get_next(Elf64_Nhdr *nhdr) -{ - return (void *)((uint8_t *)nhdr + ELF_NOTE_SIZE(sizeof(*nhdr), - nhdr->n_namesz, nhdr->n_descsz)); -} - Elf64_Phdr *elf64_getphdr(void *map) { Elf64_Ehdr *ehdr =3D map; @@ -60,13 +36,35 @@ Elf64_Half elf_getphdrnum(void *map) return ehdr->e_phnum; } =20 +static bool advance_note_offset(uint64_t *offsetp, uint64_t size, uint64_t= end) +{ + uint64_t offset =3D *offsetp; + + if (uadd64_overflow(offset, size, &offset) || offset > UINT64_MAX - 3)= { + return false; + } + + offset =3D ROUND_UP(offset, 4); + + if (offset > end) { + return false; + } + + *offsetp =3D offset; + + return true; +} + static bool init_states(QEMU_Elf *qe) { Elf64_Phdr *phdr =3D elf64_getphdr(qe->map); - Elf64_Nhdr *start =3D (void *)((uint8_t *)qe->map + phdr[0].p_offset); - Elf64_Nhdr *end =3D (void *)((uint8_t *)start + phdr[0].p_memsz); Elf64_Nhdr *nhdr; GPtrArray *states; + QEMUCPUState *state; + uint32_t state_size; + uint64_t offset; + uint64_t end_offset; + char *name; =20 if (phdr[0].p_type !=3D PT_NOTE) { eprintf("Failed to find PT_NOTE\n"); @@ -74,15 +72,40 @@ static bool init_states(QEMU_Elf *qe) } =20 qe->has_kernel_gs_base =3D 1; + offset =3D phdr[0].p_offset; states =3D g_ptr_array_new(); =20 - for (nhdr =3D start; nhdr < end; nhdr =3D nhdr_get_next(nhdr)) { - if (!strcmp(nhdr_get_name(nhdr), QEMU_NOTE_NAME)) { - QEMUCPUState *state =3D nhdr_get_desc(nhdr); + if (uadd64_overflow(offset, phdr[0].p_memsz, &end_offset) || + end_offset > qe->size) { + end_offset =3D qe->size; + } =20 - if (state->size < sizeof(*state)) { + while (offset < end_offset) { + nhdr =3D (void *)((uint8_t *)qe->map + offset); + + if (!advance_note_offset(&offset, sizeof(*nhdr), end_offset)) { + break; + } + + name =3D (char *)qe->map + offset; + + if (!advance_note_offset(&offset, nhdr->n_namesz, end_offset)) { + break; + } + + state =3D (void *)((uint8_t *)qe->map + offset); + + if (!advance_note_offset(&offset, nhdr->n_descsz, end_offset)) { + break; + } + + if (!strcmp(name, QEMU_NOTE_NAME) && + nhdr->n_descsz >=3D offsetof(QEMUCPUState, kernel_gs_base)) { + state_size =3D MIN(state->size, nhdr->n_descsz); + + if (state_size < sizeof(*state)) { eprintf("CPU #%u: QEMU CPU state size %u doesn't match\n", - states->len, state->size); + states->len, state_size); /* * We assume either every QEMU CPU state has KERNEL_GS_BAS= E or * no one has. --=20 2.34.1