From nobody Wed Mar 02 21:57:29 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 44BEE19E0B43; Wed, 2 Mar 2022 21:57:32 +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 4K87JV74Jwz3KY4; Wed, 2 Mar 2022 21:57:30 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1646258251; 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=UDkw0HEBAC/WIrNS7UqKfnwjgOyloKoTOm0Qc3BbxSg=; b=rcsgnHzOs0gRv9Q/KznqujfwWviJrurTjzr+4q4YW5kQe4PegEvzV4gvebYOQlLi24He7t ADP1T6kMqb9vwmdDILkF25b8Nl7pKIdsh7Q5Akxi1ZW04Ba3exsOY820aEA5DL2Ta3/a3Z 85QnWsCq4JZFdBEJfWBA+8iYNMHGOY77DS71761ZksNdeOhMz1CA0HmYVZw5gK6e+leMx/ meWMNl9KY9d7Wj61JuCUDT64q+E8BWYMG+uq7BkDrj447RPzfV345dxFeEFyiZznsWvcvF TGq+TkKC6QXmcfm4Nn623Zjh1mSebCib2oe/pkMzR2Gw4yZgZP2/cMl0N0/+jg== 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 84B773469; Wed, 2 Mar 2022 21:57:29 +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 222LvTS3065294; Wed, 2 Mar 2022 21:57:29 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 222LvT3D065293; Wed, 2 Mar 2022 21:57:29 GMT (envelope-from git) Date: Wed, 2 Mar 2022 21:57:29 GMT Message-Id: <202203022157.222LvT3D065293@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Eric van Gyzen Subject: git: 5be8c23a343f - stable/13 - 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/stable/13 X-Git-Reftype: branch X-Git-Commit: 5be8c23a343f1c4dc60a4b48e3e3832e146aaacb Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1646258251; 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=UDkw0HEBAC/WIrNS7UqKfnwjgOyloKoTOm0Qc3BbxSg=; b=arj3ZgZ1bgacJYu95VX+syexoL+vpxjZqDMVUAPnHjvWbN7kjF43ru8wOCFIFmnMrqJXEL kDkX4FISupx3dNg7HdaRQ6Tusz3trf5JFSahMJ5sWUBbB95NQ4irb9aHhpfOcD7RNuBmem 3uNYcOOcuwEc3HRg3Fg2JofuG4qsJf4bdAwAO972m5+JbwJbYofwvEu3smABEyjxaddfsp KMbTCi2UwMxDMYfVJoSM91VDrgSOWDMVKC0Z/gdfZfivFojDhm2xCCZfQBBYohV5hB6m4X Srd+tHkJYqJ4RXPfXQolL0ghTykhlzXiYUTWC5b7iDUqQOXlunA0izZxIZ6STw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1646258251; a=rsa-sha256; cv=none; b=jgabbmneVjpltLh7p+VXxAsuMA7Ig7YAm1Bp4hEovbIPf+VuyEnfOX7JHHI+oIrSPleCZV X1Rbmhi1g+u8CvVW2xdI7BkN6WT80ceY7YrpcyaCA/1Vvtj2Bf+4gvcL2XmyH9xt6d6aPv jOAJjav3pSyBttLABMScktcMizsUNAcdUqsAHv7wLOambSeEDgTzebL0JJ4xmYRr9Th8Vo AmcpdUo7+l/tmjVhgMvLlMP2bUp19xRMx7ZU5UJah1rE1DD6wb5Kj/Y8+v+L9MeOCf9Y5L kW3BzXN1UQ8WxttCiRQ/KIbrPOvJPS6EgcdnAzQMmserEGN+vvSdXbhwCAiAuw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by vangyzen: URL: https://cgit.FreeBSD.org/src/commit/?id=5be8c23a343f1c4dc60a4b48e3e3832e146aaacb commit 5be8c23a343f1c4dc60a4b48e3e3832e146aaacb Author: Eric van Gyzen AuthorDate: 2022-02-17 15:53:48 +0000 Commit: Eric van Gyzen CommitDate: 2022-03-02 21:56:31 +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 (cherry picked from commit 86e5e10daf54da7df358a06033f3a3bd8c852a08) --- 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");