From patchwork Wed Jul 24 22:52:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13741430 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id B547FC3DA61 for ; Wed, 24 Jul 2024 22:52:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D8DC56B0095; Wed, 24 Jul 2024 18:52:23 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D3EF36B0098; Wed, 24 Jul 2024 18:52:23 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B6BD46B009A; Wed, 24 Jul 2024 18:52:23 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 7882D6B0095 for ; Wed, 24 Jul 2024 18:52:23 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 3213D80C79 for ; Wed, 24 Jul 2024 22:52:23 +0000 (UTC) X-FDA: 82376146566.09.E38F147 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf20.hostedemail.com (Postfix) with ESMTP id A62BD1C0011 for ; Wed, 24 Jul 2024 22:52:21 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=TQ5vO9qh; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf20.hostedemail.com: domain of andrii@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=andrii@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1721861487; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=WOAQQVIDcPMn/JrdEI1DEueVlKQqj+vq02sOlQag+xE=; b=my3MmtNJFOW1RlV2+8Q9eHa2MuO4tjr2kyMDYK9cRokvNIb9/OCqMrh49T0WBtuEwHRylF W9MhPy0K1o3hwrZNOWts9ATDeFVsBZf5Jagb7otKlRq8OWoNcxiHybph7MCrfp5vrVlx3U lzEr/OiYSp1gpVXudbc5MeMrYq16PIk= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1721861487; a=rsa-sha256; cv=none; b=FHXwKC4Hm1tdkZqp4ogbnsAq4zSTjZp3R/M0fFAAQdwAdDDTS+UEQMXubcVgUJufqiuJM5 7vk5pJM5vLI81PxDSOblTGJbRhgyO01ld5B43QZVroDaqaglGq9/QwVNUlWK6N+6n2K8Y8 lbvmvQomHKaIKgTBxeMBLjl0sG8cCuY= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=TQ5vO9qh; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf20.hostedemail.com: domain of andrii@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=andrii@kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id B0134612CF; Wed, 24 Jul 2024 22:52:20 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3ABE4C32781; Wed, 24 Jul 2024 22:52:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1721861540; bh=C6pZlI4b95ZXtBjMHdytNmZo2a2DbuYqAFHnzJFYuwM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=TQ5vO9qhbM2GJE56vZsS/lOuShzvkYaxdZObXx7h0SBq5zPT+TFk3JKaQtNMmPm/b Zt/UnPeecmXrpRtskvooa1y4ovCwvyNHYlX4tohVHjDNJsRCuNZXQkXj8xjHj7BCCj Hb9RSTDC86TG4steWnHHflvUG2Ct63Fub0wiHe3Q+4kJmd7M/Jkm/+VoVXZG4ESrGj 2XJmGVlhE4wEnkf2FfU4pg+p3VnxQ4d3afOk4exQvPBXk0JfkNYXEUNz0Iqo/btYbJ bhAtrJ0zu3zaRayCKdmpE1OaU33VqG5DMp+b1mkKaVJ4yw/zuIMGHO6QHvuP/d+seT pQoXN6KfioxaA== From: Andrii Nakryiko To: bpf@vger.kernel.org Cc: linux-mm@kvack.org, akpm@linux-foundation.org, adobriyan@gmail.com, shakeel.butt@linux.dev, hannes@cmpxchg.org, ak@linux.intel.com, osandov@osandov.com, song@kernel.org, Andrii Nakryiko Subject: [PATCH v2 bpf-next 02/10] lib/buildid: take into account e_phoff when fetching program headers Date: Wed, 24 Jul 2024 15:52:02 -0700 Message-ID: <20240724225210.545423-3-andrii@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240724225210.545423-1-andrii@kernel.org> References: <20240724225210.545423-1-andrii@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: A62BD1C0011 X-Stat-Signature: oeqozcgese3h6dmxxdzgr5xe8d1u3h8u X-Rspam-User: X-HE-Tag: 1721861541-14646 X-HE-Meta: U2FsdGVkX1+gGRmvw3qjrWpBbS3woxm3aupqIQq06Aa+kodDG041rEd68DynvOA4/EJV4FwHc++Z4IU/CSV2ZEcXc0HEtLzJ3B6Jdc6vYa5DKtAC6/MRieZDfHcTEeUaMdh9YafGw1xm1hEsm+JWbzFJWkKayHvYO5ZMKbTtj8zIrbNa7iDjY4u+mcLqSLo6wMCIRxS+WCW+MvWv5NMe2NFfyFbXN02g+VOSrT4EJl3xkhkyUm4tR1ct0m3bySVloixnA/Q47EU1MA7aRWz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@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Current code assumption is that program (segment) headers are following ELF header immediately. This is a common case, but is not guaranteed. So take into account e_phoff field of the ELF header when accessing program headers. Reported-by: Alexey Dobriyan Signed-off-by: Andrii Nakryiko --- lib/buildid.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/buildid.c b/lib/buildid.c index 1442a2483a8b..ce48ffab4111 100644 --- a/lib/buildid.c +++ b/lib/buildid.c @@ -206,7 +206,7 @@ static int get_build_id_32(struct freader *r, unsigned char *build_id, __u32 *si { const Elf32_Ehdr *ehdr; const Elf32_Phdr *phdr; - __u32 phnum, i; + __u32 phnum, phoff, i; ehdr = freader_fetch(r, 0, sizeof(Elf32_Ehdr)); if (!ehdr) @@ -214,13 +214,14 @@ static int get_build_id_32(struct freader *r, unsigned char *build_id, __u32 *si /* subsequent freader_fetch() calls invalidate pointers, so remember locally */ phnum = ehdr->e_phnum; + phoff = READ_ONCE(ehdr->e_phoff); /* only supports phdr that fits in one page */ if (phnum > (PAGE_SIZE - sizeof(Elf32_Ehdr)) / sizeof(Elf32_Phdr)) return -EINVAL; for (i = 0; i < phnum; ++i) { - phdr = freader_fetch(r, i * sizeof(Elf32_Phdr), sizeof(Elf32_Phdr)); + phdr = freader_fetch(r, phoff + i * sizeof(Elf32_Phdr), sizeof(Elf32_Phdr)); if (!phdr) return r->err; @@ -237,6 +238,7 @@ static int get_build_id_64(struct freader *r, unsigned char *build_id, __u32 *si const Elf64_Ehdr *ehdr; const Elf64_Phdr *phdr; __u32 phnum, i; + __u64 phoff; ehdr = freader_fetch(r, 0, sizeof(Elf64_Ehdr)); if (!ehdr) @@ -244,13 +246,14 @@ static int get_build_id_64(struct freader *r, unsigned char *build_id, __u32 *si /* subsequent freader_fetch() calls invalidate pointers, so remember locally */ phnum = ehdr->e_phnum; + phoff = READ_ONCE(ehdr->e_phoff); /* only supports phdr that fits in one page */ if (phnum > (PAGE_SIZE - sizeof(Elf64_Ehdr)) / sizeof(Elf64_Phdr)) return -EINVAL; for (i = 0; i < phnum; ++i) { - phdr = freader_fetch(r, i * sizeof(Elf64_Phdr), sizeof(Elf64_Phdr)); + phdr = freader_fetch(r, phoff + i * sizeof(Elf64_Phdr), sizeof(Elf64_Phdr)); if (!phdr) return r->err;