From patchwork Tue Jul 30 20:39:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13747866 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 A2DFFC3DA49 for ; Tue, 30 Jul 2024 20:39:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2F0426B0093; Tue, 30 Jul 2024 16:39:34 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2ABEA6B0095; Tue, 30 Jul 2024 16:39:34 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 140406B0096; Tue, 30 Jul 2024 16:39:34 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id EAB296B0093 for ; Tue, 30 Jul 2024 16:39:33 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 60D0840516 for ; Tue, 30 Jul 2024 20:39:33 +0000 (UTC) X-FDA: 82397584626.26.BBA06A3 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf21.hostedemail.com (Postfix) with ESMTP id 21E071C002B for ; Tue, 30 Jul 2024 20:39:30 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=JJHHNmm0; spf=pass (imf21.hostedemail.com: domain of andrii@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=andrii@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1722371916; 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=8NSSfvTz2Qd7z1KNZnvE+BPgYr6TWzFHXFjUXO0bWT4=; b=Z8h9FLF9dKD0jXQBMSu2PkfpazRIs3+fdvIqlmnvb+HOBHDAVmx5qn0GyW+SEFAUpTAb6f UH8VsW8y1j5XhTuw+B4Tpq4oj/2qn4y4CruGnY6xQhuqalWUjfcV/Prhdcx/quuZY4jerq cNbmTihv942VvqBiAMSbLzeAk6+nVDY= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722371916; a=rsa-sha256; cv=none; b=jjEfi17RzYCv8hgsn67uFpcgM2Eo+84YsPbfSmEWx/ANba6V7Hqavaxoakdjl3ebPOpyzl MQ7C9d2jdC36QPg90HKS+b0FzKfGCKcmoPYZd8VjxQnXCagFgKjUXX+Ugdfcwh+t5MVX7c czBgs/QfA5EW6LIDFLmQTjyG9cKf0co= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=JJHHNmm0; spf=pass (imf21.hostedemail.com: domain of andrii@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=andrii@kernel.org; dmarc=pass (policy=none) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 446F7CE0EBB; Tue, 30 Jul 2024 20:39:28 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 635DDC32782; Tue, 30 Jul 2024 20:39:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1722371967; bh=dgztIaI7wcUufYQC2M1f2CEzPpb4qtKFeC4HcS1F34s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JJHHNmm0RMR0lI68DTZjiB1Shtq5MZ1QfO8BeQ7hc3vZ0cTdbz1Q2AR5dSLhMOT+M cDHtRFjC59k8ZM/cy8Y15dooEsrwrZzCEHtkmS55Ph7HFcsfKaMHsc45mMhLpzJ//9 cyCsUBW1oEof2DPgCMNLwCBerVU3wAhvxwxi9uCcjylMui6/QUGx06sOMMeEEMp2zn lTA3faXrcDLFlWKdnkCV+S8opQPU55ht8uQx9kel3yiQszLvmpDVwgrqDoG457104d 3wg83ar9DdMT7ad3I3BUyAmad/M3b48csSLAYWbtjYb1367xyI9fEOdSKgSEoPObat w119lCn4clZzA== 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, jannh@google.com, Andrii Nakryiko Subject: [PATCH v3 bpf-next 03/10] lib/buildid: take into account e_phoff when fetching program headers Date: Tue, 30 Jul 2024 13:39:07 -0700 Message-ID: <20240730203914.1182569-4-andrii@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240730203914.1182569-1-andrii@kernel.org> References: <20240730203914.1182569-1-andrii@kernel.org> MIME-Version: 1.0 X-Stat-Signature: 1wmkjbbnstchqe9tjd9uepz3o41won8g X-Rspamd-Queue-Id: 21E071C002B X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1722371970-406506 X-HE-Meta: U2FsdGVkX1+HiJX7TdWEEb69gfhsV1wbHac0ZNlCm1tNyREi4apzxhMcZgO3n5+YUXztKW7URMnRH34jj8meMySbcyd6t6J04GPc/UIXivIJxBGSLoGUyx4sDGkoG2gisqCM5ucFMP4okqBZu62LdnWBBjFq6kHHnbWtg47YV4jDVmxL5I0B4HcAbkYqL4FRP0q0HXgYaEfZgRzhJugCrIY0jTVzCGdgc/+ASUJF5DzuMKogXbr65HLrBXxvhQAXPQlMNTmFaMvA13xn1Em9Jm0bZ1Hy5Q0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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 | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/lib/buildid.c b/lib/buildid.c index 522850f827a5..ba453a3784d1 100644 --- a/lib/buildid.c +++ b/lib/buildid.c @@ -207,27 +207,22 @@ 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; + __u64 phoff; ehdr = freader_fetch(r, 0, sizeof(Elf32_Ehdr)); if (!ehdr) return r->err; - /* - * FIXME - * Neither ELF spec nor ELF loader require that program headers - * start immediately after ELF header. - */ - if (ehdr->e_phoff != sizeof(Elf32_Ehdr)) - return -EINVAL; - /* subsequent freader_fetch() calls invalidate pointers, so remember locally */ phnum = READ_ONCE(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; @@ -245,27 +240,22 @@ 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) return r->err; - /* - * FIXME - * Neither ELF spec nor ELF loader require that program headers - * start immediately after ELF header. - */ - if (ehdr->e_phoff != sizeof(Elf64_Ehdr)) - return -EINVAL; - /* subsequent freader_fetch() calls invalidate pointers, so remember locally */ phnum = READ_ONCE(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;