Bug 246960 - file(1) does not report PIE binaries
Summary: file(1) does not report PIE binaries
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: bin (show other bugs)
Version: CURRENT
Hardware: Any Any
: --- Affects Only Me
Assignee: freebsd-bugs (Nobody)
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-06-03 16:22 UTC by Ed Maste
Modified: 2020-06-29 15:02 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Ed Maste freebsd_committer 2020-06-03 16:22:50 UTC
Reproduction steps:
1. build a PIE (position independent executable) binary:

$ cc -fpie -pie -o hello ~/hello.c
$ ./hello
Hello, world

2. use file to determine the type:

$ file hello
hello: ELF 64-bit LSB shared object, x86-64, version 1 (FreeBSD), dynamically linked, interpreter /libexec/ld-elf.so.1, for FreeBSD 13.0 (1300087), FreeBSD-style, with debug_info, not stripped

As of very recently we have a DF_1_PIE flag to indicate that this is a PIE binary, not a DSO:

$ readelf -d hello

Dynamic section at offset 0xc20 contains 25 entries:
  Tag                Type                  Name/Value
 0x0000000000000001 NEEDED               Shared library: [libc.so.7]
 0x000000006ffffffb FLAGS_1              PIE

We should report something like "ELF 64-bit LSB position independent executable..." from file/libmagic.
Comment 1 Ed Maste freebsd_committer 2020-06-21 15:47:25 UTC
File 5.39 from ports works as expected:

 $ /usr/local/bin/file ~/hello
/home/emaste/hello: ELF 64-bit LSB pie executable, x86-64, version 1 (FreeBSD), dynamically linked, interpreter /libexec/ld-elf.so.1, for FreeBSD 13.0 (1300087), FreeBSD-style, with debug_info, not stripped
Comment 2 Ed Maste freebsd_committer 2020-06-21 16:02:18 UTC
File from ports gets both cases correct:
% cp /lib/libc.so.7 /tmp/
% chmod +x /tmp/libc.so.7

% /usr/local/bin/file /tmp/libc.so.7
/tmp/libc.so.7: ELF 64-bit LSB shared object, x86-64, version 1 (FreeBSD), dynamically linked, stripped

% /usr/local/bin/file ~/hello
/home/emaste/hello: ELF 64-bit LSB pie executable, x86-64, version 1 (FreeBSD), dynamically linked, interpreter /libexec/ld-elf.so.1, for FreeBSD 13.0 (1300087), FreeBSD-style, with debug_info, not stripped

while file in-tree incorrectly reports shared object for the PIE binary
Comment 3 commit-hook freebsd_committer 2020-06-29 13:31:54 UTC
A commit references this bug:

Author: emaste
Date: Mon Jun 29 13:30:48 UTC 2020
New revision: 362782
URL: https://svnweb.freebsd.org/changeset/base/362782

Log:
  Revert r362261, "Re-apply r333944 to unbreak ports"

  A file update in 2018 broke many ports as it misidentified shared
  libraries as PIE binaries.  r333944 reverted part of the change,
  restoring ports builds but misidentifying objects in the opposite
  direction.

  Earlier this month file 5.39 was imported, and then the change
  originally from r333944 was recommitted as r362261.  However, the
  issue was fixed upstream, so r362261 serves no purpose.

  PR:		246960, 247461 [exp-run]
  Sponsored by:	The FreeBSD Foundation

Changes:
  head/contrib/file/magic/Magdir/elf