From patchwork Tue Aug 13 00:29:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13761168 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 4B1E3C3DA7F for ; Tue, 13 Aug 2024 00:29:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7CE876B009F; Mon, 12 Aug 2024 20:29:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6E3F16B00A2; Mon, 12 Aug 2024 20:29:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4E3386B00A0; Mon, 12 Aug 2024 20:29:52 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 225CB6B00A0 for ; Mon, 12 Aug 2024 20:29:52 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id CB042C035D for ; Tue, 13 Aug 2024 00:29:51 +0000 (UTC) X-FDA: 82445339382.16.A06D041 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf30.hostedemail.com (Postfix) with ESMTP id 8AAC280021 for ; Tue, 13 Aug 2024 00:29:49 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=aFz3Oa0J; spf=pass (imf30.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=1723508921; 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=vaDrr2Mt3NhLIcfB0RKKtZlVExveRPkXlfvOE6WUJgY=; b=gHTCs0/2iCAmuPj2CCVhfhxMlcIT3hq1/Smn99Lpd08dIL40f6jkruA4HcVI9kvN8SWzmf zWYuRjL1gzWZga1nLGrkIqQUleMnR+a86SvECsxN24gZcjTlrW8/wYTQS8sPciB60iyExu 1Ca1PKHKBbk4m+/sKJzMBuMloqyJxLA= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723508921; a=rsa-sha256; cv=none; b=1SDOL4EYqlupbPA6LSjYqKMeuu9WhBcf6AQ0DbcH8CH9JlzCTTbivN/MpGvsfsyn5ekm5l uQhh3j8nNU38DT6Ps6OkmP90Y98jgxiRmLQBZ/uEnUoPE7/a39BYwle9PEtdgsmoAkTKGA a4iyIM80Y2ESMIg5eNR2kHF8MXI+Vb4= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=aFz3Oa0J; spf=pass (imf30.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 69BB8CE10FC; Tue, 13 Aug 2024 00:29:46 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7F10EC4AF12; Tue, 13 Aug 2024 00:29:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1723508985; bh=qaMr9V+HCCVlztizCv4KeksSMYe9fzp24XbALik8ZKw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=aFz3Oa0JPzBYydr1B1rTYWL12SMjsvjDU39bYLcGFfOiGOoxokLHaRnxa8hbl70nM l+2MC47iNIGLaO9kz9+WPs4VLDTb9opO4u8c/slc41g4vXqB+vDJL17siRX3wUdJcn F8NISui0ctqh8H5M44FfyVc+G1fsNRQYudQ8F5oqLgCBI6JKdBFrtWQ7Tkrm2ioUhG 7TjX5RTf/e8Q5Bie/ACJAaNF7hUPgrtCzgoBtAvl8A9ycOqkR+fEHse3vtrkMbqraB HwKWKxHQzKXaGGEer9lNGbY1biagmt06xQ8wJb0sqIDgojpUm8hpWC2rW01MirJlNO bY0JdWZT2LOkw== 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, linux-fsdevel@vger.kernel.org, willy@infradead.org, Andrii Nakryiko Subject: [PATCH v5 bpf-next 03/10] lib/buildid: take into account e_phoff when fetching program headers Date: Mon, 12 Aug 2024 17:29:25 -0700 Message-ID: <20240813002932.3373935-4-andrii@kernel.org> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20240813002932.3373935-1-andrii@kernel.org> References: <20240813002932.3373935-1-andrii@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 8AAC280021 X-Stat-Signature: ck8xscyt7836puob7zueuhku6mxo8h6a X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1723508989-265788 X-HE-Meta: U2FsdGVkX18JIP1b2OCLGQIGyvP/i1IbpLusoHDapItS4v3F4WdRGVxq+L8TSCQFK9MfQjNszvjNQz7Slt513LCAwm3TD+Va7PHLj9iLtwWu/AdovtPJ6q0EsQ0G92+Tn2XBZJizHjwp4tUf8aAhrBlGd8t8EsdnzadpamaBYyHYS1xHzvUB36nk3NlbVpG/T07JxTpOZcG4dR15Ji156jXTQTLZmL26ivNX/sSbosPu1+m2hbT2GWPvyhH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.000000, version=1.2.4 Sender: owner-linux-mm@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 | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/lib/buildid.c b/lib/buildid.c index 2b1050e99576..abf5d1c7eb47 100644 --- a/lib/buildid.c +++ b/lib/buildid.c @@ -213,28 +213,26 @@ 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) 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; + /* check that phoff is not large enough to cause an overflow */ + if (phoff + phnum * sizeof(Elf32_Phdr) < phoff) + 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; @@ -252,27 +250,26 @@ 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; + /* check that phoff is not large enough to cause an overflow */ + if (phoff + phnum * sizeof(Elf64_Phdr) < phoff) + 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;