From nobody Mon Feb 9 13:00:55 2026 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 682A8328B56 for ; Sat, 17 Jan 2026 05:29:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768627764; cv=none; b=lTmuc05VnjsvfAn0x3CSMgw9p/jUgefCYKyuyOsfWadbg0TUAtdCdSiZzdvWzpL9k8YIXJ4EAV6LVYqC9N+WdahVHE8maa0nW7GZo90PCHjlhZzXBqmoZfCJzOXgZDXQ5sOM/kFp0nb9a8Juk3yGCuhWnKO0yUcvWbtq41iq1KI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768627764; c=relaxed/simple; bh=72XsDyD4QhqC99XxL2NKKPs+ap/5CJLt+GafEN9+hzM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Content-Type; b=MYyhSpbG2EpjPkUB16V8/04b0R/RlYMDgXYsy3P7xWnxKFtfz18+1uEheAsbH8jY4/KeMWGYMRCT/ZJiH2oayAG7Sf8ZvC+neguLWd830uS8Ok8yi2iQsU/vBtsDqBcwZsgX6LDAxWVmQgVrcblnRhLlQGW+FctJ4bhk6QIt0kI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=NrP1sPFD; arc=none smtp.client-ip=209.85.214.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="NrP1sPFD" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-29f1f69eec6so26790535ad.1 for ; Fri, 16 Jan 2026 21:29:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1768627763; x=1769232563; darn=vger.kernel.org; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=kK4WpTbjYTQ30LMox0FYXGUlgDkXshdRvemGVnSiQqs=; b=NrP1sPFDNLf8KZljOW9peOaBCL26dkr3UexLZGDlM5v/SCNryFX0Ln6RwyanHUGseZ FVBqBlH7JhuEOcx3iWbJC/PjfcrcOhtWU90ySb9TX9VbRenu+Wdk/Cx/yK9iIAHkWa66 9viRN/QQn7yUIg1mJYoPkTtzVW6EHNWxjYmg2ORJFPPr6VW8iXndD3h9hZTNEzsjKHTt k3OInXqXQx5Tbt8J0UenspACFM32zTMgn300I6i+GKZPTNowK+iKDgeX4bK42fGgeGqK FGqE0qoBzMu5pifiaJB8hckFTs1/ZmhY/pzBan5duZGRr7Wd5YSjbTggtfXRxKnQQ8IJ PYxw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768627763; x=1769232563; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=kK4WpTbjYTQ30LMox0FYXGUlgDkXshdRvemGVnSiQqs=; b=t/S3W0OtZZ7ig4cMF1IvU5LjuXATQakhvmJtWNOTGIAc5l0gjatdoiTXR47vaKreMK PJ1WmPRCHcMOXm5fMbBkngq2WYEHwXnqF3SoweENvcGLnBaAKxU4QQbY5t2nxseSKZ7/ app2rRIDmwKglnqUnAZqL6IkqBguFxs+dDmwXmjUju3zUsOqBrRgxExYnrYwRdHLTPTN vITAq8a7joI405+drOizYZkgNazE80j501NqZ4iSCuTjpTNFTAHkKK+S9zsVBIvutsJQ y9l02hzbnJOKywEa3o3Lfg5rXpXeC7CCK+TpUSBlb61p/ZTwFOADtdjXQmo2UBfn3yCv Wj5A== X-Forwarded-Encrypted: i=1; AJvYcCWYdpfnd0yRO0ekkOnqhjf1ABolEI+hxK3aOwuO+Pq7aCv9PGeVAsV4L1bkp8VMxcZQd8RTTPBvtjtkl4E=@vger.kernel.org X-Gm-Message-State: AOJu0YyDzYSFnT43Y8oaenMQzEftr0ZS/ED0pMhIkItOKybS9OCtP5S4 nqWASCdFZnwInetCdGrLMZ+ADP7Fgw1N4K07CDyqkAkNT6d13fX1h8izMmzn17lpOmM4m6GpRfg fj0EjDKEt4Q== X-Received: from plov5.prod.google.com ([2002:a17:902:8d85:b0:29d:7778:2d65]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:2f8a:b0:2a2:f0cb:dfa2 with SMTP id d9443c01a7336-2a71754509fmr50340175ad.13.1768627762755; Fri, 16 Jan 2026 21:29:22 -0800 (PST) Date: Fri, 16 Jan 2026 21:28:28 -0800 In-Reply-To: <20260117052849.2205545-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260117052849.2205545-1-irogers@google.com> X-Mailer: git-send-email 2.52.0.457.g6b5491de43-goog Message-ID: <20260117052849.2205545-3-irogers@google.com> Subject: [PATCH v1 02/23] perf dso: Extra validity checks that e_machine is valid From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Jiri Olsa , Ian Rogers , Adrian Hunter , James Clark , John Garry , Will Deacon , Leo Yan , Guo Ren , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti , Shimin Guo , Athira Rajeev , Stephen Brennan , Howard Chu , Thomas Falcon , Andi Kleen , "Dr. David Alan Gilbert" , Dmitry Vyukov , "=?UTF-8?q?Krzysztof=20=C5=81opatowski?=" , Chun-Tse Shao , Aditya Bodkhe , Haibo Xu , Sergei Trofimovich , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-csky@vger.kernel.org, linux-riscv@lists.infradead.org, Mark Wielaard Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Better ensure a read e_machine is valid by checking the file appears like an ELF file and the read e_machine value is less than EM_NUM. This better avoids spurious e_machine values when looking for an e_machine in say a thread. Signed-off-by: Ian Rogers --- tools/perf/util/dso.c | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c index 18e656712f5a..143720d1ecb1 100644 --- a/tools/perf/util/dso.c +++ b/tools/perf/util/dso.c @@ -1236,17 +1236,28 @@ uint16_t dso__e_machine(struct dso *dso, struct mac= hine *machine) try_to_open_dso(dso, machine); fd =3D dso__data(dso)->fd; if (fd >=3D 0) { - _Static_assert(offsetof(Elf32_Ehdr, e_machine) =3D=3D 18, "Unexpected of= fset"); - _Static_assert(offsetof(Elf64_Ehdr, e_machine) =3D=3D 18, "Unexpected of= fset"); - if (dso__needs_swap(dso) =3D=3D DSO_SWAP__UNSET) { - unsigned char eidata; - - if (pread(fd, &eidata, sizeof(eidata), EI_DATA) =3D=3D sizeof(eidata)) - dso__swap_init(dso, eidata); + unsigned char e_ident[EI_NIDENT]; + + _Static_assert(offsetof(Elf32_Ehdr, e_ident) =3D=3D 0, "Unexpected offse= t"); + _Static_assert(offsetof(Elf64_Ehdr, e_ident) =3D=3D 0, "Unexpected offse= t"); + if (pread(fd, &e_ident, sizeof(e_ident), 0) =3D=3D sizeof(e_ident) && + memcmp(e_ident, ELFMAG, SELFMAG) =3D=3D 0 && + e_ident[EI_CLASS] > ELFCLASSNONE && e_ident[EI_CLASS] < ELFCLASSNUM = && + e_ident[EI_DATA] > ELFDATANONE && e_ident[EI_DATA] < ELFDATANUM && + e_ident[EI_VERSION] =3D=3D EV_CURRENT) { + _Static_assert(offsetof(Elf32_Ehdr, e_machine) =3D=3D 18, "Unexpected o= ffset"); + _Static_assert(offsetof(Elf64_Ehdr, e_machine) =3D=3D 18, "Unexpected o= ffset"); + + if (dso__needs_swap(dso) =3D=3D DSO_SWAP__UNSET) + dso__swap_init(dso, e_ident[EI_DATA]); + + if (dso__needs_swap(dso) !=3D DSO_SWAP__UNSET && + pread(fd, &e_machine, sizeof(e_machine), 18) =3D=3D sizeof(e_machin= e) && + e_machine < EM_NUM) + e_machine =3D DSO__SWAP(dso, uint16_t, e_machine); + else + e_machine =3D EM_NONE; } - if (dso__needs_swap(dso) !=3D DSO_SWAP__UNSET && - pread(fd, &e_machine, sizeof(e_machine), 18) =3D=3D sizeof(e_machine= )) - e_machine =3D DSO__SWAP(dso, uint16_t, e_machine); } mutex_unlock(dso__data_open_lock()); return e_machine; --=20 2.52.0.457.g6b5491de43-goog