From mboxrd@z Thu Jan 1 00:00:00 1970 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 66A9FC7EE30 for ; Mon, 30 Jun 2025 00:12:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 87A8B6B0093; Sun, 29 Jun 2025 20:12:14 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 82B536B0096; Sun, 29 Jun 2025 20:12:14 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 741656B0098; Sun, 29 Jun 2025 20:12:14 -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 607B36B0093 for ; Sun, 29 Jun 2025 20:12:14 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id BE260B8075 for ; Mon, 30 Jun 2025 00:12:13 +0000 (UTC) X-FDA: 83610139746.26.F913904 Received: from out30-118.freemail.mail.aliyun.com (out30-118.freemail.mail.aliyun.com [115.124.30.118]) by imf10.hostedemail.com (Postfix) with ESMTP id 7E098C0005 for ; Mon, 30 Jun 2025 00:12:10 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=b4g4hmaQ; dmarc=pass (policy=none) header.from=linux.alibaba.com; spf=pass (imf10.hostedemail.com: domain of fengwei_yin@linux.alibaba.com designates 115.124.30.118 as permitted sender) smtp.mailfrom=fengwei_yin@linux.alibaba.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1751242331; a=rsa-sha256; cv=none; b=izJdUN+F0o30kQFvK0QAZU1QMKSj5h4wi5gvk0mEvFNnkzIWL/J7KqlKnwWE3YMjkbHtfz LkEqfBC24igRvtxtKo2U+9n835zZfuK3coInSy9LzAkHdptw8NdXYxHS6gEG2zDH5WRW5u b+VnAkAbxPcA4rJdCgqBPls5XSMFyzA= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=b4g4hmaQ; dmarc=pass (policy=none) header.from=linux.alibaba.com; spf=pass (imf10.hostedemail.com: domain of fengwei_yin@linux.alibaba.com designates 115.124.30.118 as permitted sender) smtp.mailfrom=fengwei_yin@linux.alibaba.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1751242331; 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-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Kp53yHyclnfuXyHbcdfZXdkRcD9GyJnaFra1xJlpF/8=; b=VjyVp3iVegybnq4kph05Hgd6obut7a8NxgMH472wtiZA7OZDs7ehPW+TRGGK4xe8lUxSP+ 1FpeszulDvR0tqtBtdC862xeVywcOu7EN5Hq9fXMJGfuqYYhavF1eGYYGAx0hkAw2nJdcp oVDyOBcX0rdPnldAn2Gob9j9xcwD6BQ= DKIM-Signature:v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1751242327; h=Date:From:To:Subject:Message-ID:MIME-Version:Content-Type; bh=Kp53yHyclnfuXyHbcdfZXdkRcD9GyJnaFra1xJlpF/8=; b=b4g4hmaQ2MgWM0Yw0wNRU1PQ+0ATiZLhFA3kor20ItW3Y5CmzYtd3Kw/rm5QO81M7NjnKs0KhpYeGIjN4XjHRF7hUepgdWPYSVB94LynJlQuPP//3iW83+rZZZqD2mTMj9mbk3G0PsPzWlYF6VTiZKaMW0DUulHBgu0TpsWVDck= Received: from U-V2QX163P-2032.local(mailfrom:fengwei_yin@linux.alibaba.com fp:SMTPD_---0Wg1K.Qo_1751242325 cluster:ay36) by smtp.aliyun-inc.com; Mon, 30 Jun 2025 08:12:05 +0800 Date: Mon, 30 Jun 2025 08:12:05 +0800 From: YinFengwei To: Kees Cook Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, zhourundong.zrd@linux.alibaba.com Subject: Re: [Question] About the elf program header size Message-ID: <4a3mhbhfvj2ctmovfknmdhskbmmhii3jcsdvba3e7xxytzkpc4@odtyzuhto7lx> References: <202506270854.A729825@keescook> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <202506270854.A729825@keescook> X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 7E098C0005 X-Stat-Signature: xp11cr6h8sxumfwxpr8u115eu4ehuy1o X-Rspam-User: X-HE-Tag: 1751242330-388752 X-HE-Meta: U2FsdGVkX1/aSbyg125tF+TiwjEabRgPWb4AR/3PvBEhzaeDxOSglZDOGzkppHytoZofuu1ueK5l86Oaqij2I9hXzsWrziJbf2MZNIzXm8iyLZL8cV52gg0QwbDD3iqORsV1a4ErtbqM5uOTsryvwMTNYbOIXsB7nVvZEeugjTI4ictyUIZIgKZ/MLkr6D1IF1GdqZdoGO6Pto0jo0nkp4jvf5CeHo0CXf6Dk41mD2YtMCbcNZsqaEHOtlJN1T9BdV0rb1xV/Jt9lSNZDSWchL2+rXaeJ/tGeAmcmttNkeloK/rYYf0XeM1VcasYY8lh1hBmlj/x7CPdE27EoVjOpM6zkcefRnIVz52ahwW5ZqIb8AlWytLN+Jgx64Efj1qkUTh7eDsSGo6aX1oX5LAbe+FwOQ1mhji4q70/dOHlLIC3MgCNi7bd79x0gQ1SoyMbZTOgd14/XByDE9jcLZB0tJAARuOh8Yza8zaJwb3vQlc5so3Y8P6KQaD3LbwKzztWXFDfKWSYjurv4d+7Y83VbSOI/KnT0eDitM9UFJyrts1EFgAo35e0HPwgmybhS16Va0iBLBj97tIhu+FGuDlaZE9kLXMSqzjXXQnExyKKRmRqTC8dV/pxUKbh+v5TQVIoOnptEOnhMJxZivFdF/WSLIN7UOsa5J0tUwP2CkftzT2uLfp4d4QLnrfYB7pjnPF6khhldW3VttbdSBz94exgBIiPlbDofwtQ0qjKDhNx7JoVozHLS2DC9G653LnsSHBmMe+aLlyybAn+/26OYzYSexaybx8dIPe63wBIRWP/x9/L/lfPwvim9mTpIYW2WjxnxEA1ehbaqCb5uauxTXBUJBkJ+0AiKCkuapLvoWlxPPFJVRvpMTj4VtrX92q6XgYtsDW0hUyxALFqHyljzB16t4czZ2AWVuH2rjDMlSbBIs1fP2PIn/mO7zfJ+bjMSs8x0BZvo26/BokgNd2p98c TkZAVMSE HaWI6H5LPq4Z6dEhi9eWsC3jT3uNtCBoo+HO8RZKADB+6+T+WZv+2x2Fc0JRSIpF3XsJROtWt1LNG3h+vo6Wh0Q16NrjyVu2JhIvYPbzuelDPZ+8BfZkzQcILDx4a/vq92KWGuhW0RoT5go3/ROjd/Hyxnu7lgcgSuvk6K6GsjfrxBzTrx73NVAA0BTUChEL8VEwCRDtcz+JDSFFOqXHekp3QlTuk1fOY5pcqyiUbC7IScO1FIzMHkdcTW3iROlCV1CTs8i8LKBl5FEyXlcG532pKtEe4izSCZTOK0/PcoAnU51F/687ZQwrkt0CKGY8D6kcYc4tKijEnYXPI9pVLBKEM5Q== X-Bogosity: Ham, tests=bogofilter, spamicity=0.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: On Fri, Jun 27, 2025 at 09:35:45AM +0800, Kees Cook wrote: > On Fri, Jun 27, 2025 at 09:04:11AM +0800, YinFengwei wrote: > > We had a script generated assembly code. built it with gcc and the > > output elf file had 78 program headers. > > Why so many? I don't know the detail. Just know it's a tool generates assembly code according to the trace data. From the objdump, it looks like there are many hole generated (I assume it just generates the code which is the code path just hit). > > > On an arm64 platform, if we have 64KB base page size, the elf can > > be started correctly. But if we have 4KB base page size, the elf > > can NOT be started with: > > cannot execute binary file: Exec format error > > > > Look at the function load_elf_phdrs(): > > if (size == 0 || size > 65536 || size > ELF_MIN_ALIGN) > > goto out; > > > > ELF_MIN_ALIGN is defined as PAGE_SIZE on arm64. Which can explain > > above inconsistent behaviors (from user perspetive). > > > > I didn't find the limitation definition in ELF spec(Maybe I missed > > some obvious info there). If I remove "size > ELF_MIN_ALIGN", the > > same elf can be started correctly even with 4KB page size. > > > > So my question is why we limit the who program headers total size > > to PAGE_SIZE? git history couldn't tell anything because the > > limitation was introduced when whole linux kernel tree was migrated > > to git. Is there a possible constrain on other architecture? Thanks. > > Looking through > https://git.kernel.org/pub/scm/linux/kernel/git/history/history.git > (which doesn't have linked history, so you have to examine explicit "pre > git" tags), I see: > > 4779b38bcb96 ("[PATCH] Linux-0.99.13 (September 19, 1993)") > Which says "ELF binary support it a notable change." Here, the PAGE_SIZE > check does not exist. When ELF interp support was added in > 9e11983a5a3e ("Import 0.99.15f"), we see the check appear, and I can > find no rationale. > > And with 6a8d38945cf4 ("binfmt_elf: Hoist ELF program header loading to > a function"), the PAGE_SIZE check is _added_ for non-interp loads. Thanks a lot for this information. I didn't know the pre-git history can be found here. > > It seems the 64K count limit is sufficient? (If the goal was to avoid > large memory allocations happening from userspace, we're way past > PAGE_SIZE these days between IPC, BPF, etc.) Does this work for you? Yes. It works good. Thanks. Regards Yin, Fengwei > > > diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c > index a43363d593e5..92de44b8765f 100644 > --- a/fs/binfmt_elf.c > +++ b/fs/binfmt_elf.c > @@ -519,7 +519,7 @@ static struct elf_phdr *load_elf_phdrs(const struct elfhdr *elf_ex, > /* Sanity check the number of program headers... */ > /* ...and their total size. */ > size = sizeof(struct elf_phdr) * elf_ex->e_phnum; > - if (size == 0 || size > 65536 || size > ELF_MIN_ALIGN) > + if (size == 0 || size > 65536) > goto out; > > elf_phdata = kmalloc(size, GFP_KERNEL); > > > -- > Kees Cook