From nobody Thu Feb 17 16:12:43 2022 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 052DF19D035B; Thu, 17 Feb 2022 16:12:44 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4K00Gg6S3fz4TwF; Thu, 17 Feb 2022 16:12:43 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1645114363; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=igyrbUr4wfSyC7KNdIEW/63CXiKCx9YsOAnrgs2Rd6M=; b=xtUJXjpHRgdayylG8slldwDxktn9EiDa9X31YNi5+ETG61rl9Zx2xOutNqZXvdSXvqTgdU hPaplnPWNP2h5RbnTkHLkU0Rd0RhWkkFE2cH43RMTLc0sMPpE5F5L02NuD1Ntpge73k16z RW1P7lfI+J1KBpfPR6qplkIlMCpil3YHdSBiiVrIhPPJXmTMWtvS5c2ZiXMNVWS+IyDMrB E371NJ/XKJe7quiNx3gVwvwMD30BFhxxWuelLdHiTcXN75VdmRhf3MtDcepBO0Zqpv0D6f DhTjqWeL2blMvywfIGVBMFOh0U/LOe7+ZlLf8wAWeBSuRzwYgTDcmQDIkeuFLA== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id BD78B5081; Thu, 17 Feb 2022 16:12:43 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 21HGChiL049381; Thu, 17 Feb 2022 16:12:43 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 21HGChMQ049380; Thu, 17 Feb 2022 16:12:43 GMT (envelope-from git) Date: Thu, 17 Feb 2022 16:12:43 GMT Message-Id: <202202171612.21HGChMQ049380@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Eric van Gyzen Subject: git: 86e5e10daf54 - main - elfdump: handle small files more gracefully List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: vangyzen X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 86e5e10daf54da7df358a06033f3a3bd8c852a08 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1645114363; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=igyrbUr4wfSyC7KNdIEW/63CXiKCx9YsOAnrgs2Rd6M=; b=X1+2QXKmro9FST2NUoOsVDlJnFqzwVcIebcCgMDrpeybo4TSmcpP24jUeIVLrvYAzsx/FT p2khw8Cnc/lgI/rCKfzMEwJMFm1K4pKOIpSEIWP7mILf3E+1wo3osjXDabO72LDQUZyBjy y4NU9yI6YuokTbdEcqa1ScShsoEA9Hl+rfDqofDUjA3PWNXSx2J8wUZkMrkBR7Xuc1X3ib C5hePzzDLNzB89ISyD9UJfwztCDkwVi4jiblh9mk7xf4p/rblqemrJofj0dbP5oJmwsrep zfOoAzNFeh/zcnZ2Xy2LEg0ydQE1zN2zX3rPLArgULIS4Y9lcDC5+R+0SPXg0A== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1645114363; a=rsa-sha256; cv=none; b=HE9ZoG7bN56VUflrOQcRj0NamdhnhqFBQlhhc8QKVpS90dAJ8HlyLIlXlGkRXXPZ3B8mPL +ddSWVyLJAGzXlv1wt5kdAj9eZSfSr6NFBrGmTPN5sCIjYlo/IC7NMzVpx2QpIzHsIT1wn oO/l/b8LbzYMOONqH9h9JZX6ZnF4Cdr5fMyNAzgmNPxAuBHLPFrFcoPfgtvw9tsmjFlCXr dRJcGSCp037qtxv1qCSryvClD6PfrCxsTLkHP5VDzMXDgEARkshfx0+mcsX59+rzZE9JIo T7wWxXWF79J9inTjFdgb7gQsKt3B42SbJHMahxVZYwtW3HWG46VIQld7lHskVA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by vangyzen: URL: https://cgit.FreeBSD.org/src/commit/?id=86e5e10daf54da7df358a06033f3a3bd8c852a08 commit 86e5e10daf54da7df358a06033f3a3bd8c852a08 Author: Eric van Gyzen AuthorDate: 2022-02-17 15:53:48 +0000 Commit: Eric van Gyzen CommitDate: 2022-02-17 16:09:53 +0000 elfdump: handle small files more gracefully elfdump -E on an empty file would complain "Invalid argument" because it tried to mmap zero bytes. With the -E flag, elfdump should simply exit non-zero. For tiny files, the code would reference off the end of the mapped region. Ensure the file is large enough to contain an ELF header before mapping it. MFC after: 1 week Sponsored by: Dell EMC Isilon --- usr.bin/elfdump/elfdump.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/usr.bin/elfdump/elfdump.c b/usr.bin/elfdump/elfdump.c index 2bdf98830088..28b42a55a508 100644 --- a/usr.bin/elfdump/elfdump.c +++ b/usr.bin/elfdump/elfdump.c @@ -585,6 +585,11 @@ main(int ac, char **av) if ((fd = open(*av, O_RDONLY)) < 0 || fstat(fd, &sb) < 0) err(1, "%s", *av); + if ((size_t)sb.st_size < sizeof(Elf32_Ehdr)) { + if (flags & ED_IS_ELF) + exit(1); + errx(1, "not an elf file"); + } cap_rights_init(&rights, CAP_MMAP_R); if (caph_rights_limit(fd, &rights) < 0) err(1, "unable to limit rights for %s", *av); @@ -598,7 +603,7 @@ main(int ac, char **av) e = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, fd, 0); if (e == MAP_FAILED) err(1, NULL); - if (!IS_ELF(*(Elf32_Ehdr *)e)) { + if (!IS_ELF(*e)) { if (flags & ED_IS_ELF) exit(1); errx(1, "not an elf file");