>Synopsis: OpenBSD's readelf does not properly validate input.
>Category: system
>Environment:
System      : OpenBSD 6.0
Details     : OpenBSD 6.0 (GENERIC) #2148: Tue Jul 26 12:55:20 MDT 2016
dera...@amd64.openbsd.org:/usr/src/sys/arch/amd64/compile/GENERIC

Architecture: OpenBSD.amd64
Machine     : amd64
>Description:
The /usr/bin/readelf binary on OpenBSD 6.0 does not seem to properly
validate input.
You can for example cat a binary and take the first few "lines" of the file
and throw them at readelf,
which will cause a sig abort, as shown in the test case. This could
possibly be exploited,
but I havn't tried to manipulate memory any more than in the test case so
far.
The relevant code can be found in
https://github.com/openbsd/src/blob/5271000b44abe23907b73bbb3aa38ddf4a0bce08/gnu/usr.bin/binutils/binutils/readelf.c
and the directory it is in.
(Though I havn't compiled from that git tree specifically to see if it has
the same bug, 6.0 release does).
>How-To-Repeat:

bash-4.3# uname -mprsv
OpenBSD 6.0 GENERIC#2148 amd64 amd64
bash-4.3# file c4
c4: ELF 64-bit LSB MathCoPro/FPU/MAU Required
bash-4.3# sha1 /usr/bin/readelf
SHA1 (/usr/bin/readelf) = 577d6d481371bba63df7bab5e3669f30ef8720bc
bash-4.3# hexdump ./c4
0000000    457f    464c    0102    0001    0000    0000    0000    0000
0000010    4141    4242    4343    4343    cafe    beef    dead    0000
0000020    000a    003e    0001    0000    2450    0000    0000    0000
0000030    0040    0000    0000    7f0a    4c45    0246    0101    0000
0000040    0000    0000    0000    0300    3e00    0100    0000    5000
0000050    0024    0000    0000    4000    0000    0000    0000    e800
0000060    0081    0000    0000    0000    0000    4000    3800    0b00
0000070    4000    1800    1700    0600    0000    0500    0000    4000
0000080    0000    0000    0000    4000    0000    0000    0000    4000
0000090    0000    0000    0000    6800    0002    0000    0000    6800
00000a0    0002    0000    0000    0800    0000    0000    0000    0300
00000b0    0000    0400    0000    1800    0003    0000    0000    1800
00000c0    0003    0000    0000    1800    0003    0000    0000    1300
00000d0    0000    0000    0000    1300    0000    0000    0000    0100
00000e0    0000    0000    0000    0100    0000    0500    0000    0000
00000f0    0000    0000    0000    0000    0000    0000    0000    0000
0000100    0000    0000    0000    2e00    0056    0000    0000    2e00
0000110    0056    0000    0000    0000    1000    0000    0000    0100
0000120    0000    0400    0000    4000    0056    0000    0000    4000
0000130    1056    0000    0000    4000    1056    0000    0000    0400
0000140    001b    0000    0000    0400    001b    0000    0000    0000
0000150    1000    0000    0000    0100    0000    0600    0000    4800
0000160    0071    0000    0000    4800    2071    0000    0000    4800
0000170    2071    0000    0000    7000    0001    0000    0000    7000
0000180    0001    0000    0000    0000    1000    0000    0000    0100
0000190    0000    0600    0000    b800    0072    0000    0000    b800
00001a0    3072    0000    0000    b800    3072    0000    0000    4800
00001b0    0002    0000    0000    4800    0002    0000    0000    0000
00001c0    1000    0000    0000    0100    0000    0600    0000    0000
00001d0    0075    0000    0000    0000    4075    0000    0000    0000
00001e0    4075    0000    0000    2000    000c    0000    0000    a000
00001f0    000c    0000    0000    0000    1000    0000    0000    0200
0000200    0000    0600    0000    5800    0071    0000    0000    5800
0000210    2071    0000    0000    5800    2071    0000    0000    6000
0000220    0001    0000    0000    6000    0001    0000    0000    0800
0000230    0000    0000    0000    0400    0000    0400    0000    2c00
0000240    0003    0000    0000    2c00    0003    0000    0000    2c00
0000250    0003    0000    0000    1800    0000    0000    0000    1800
0000260    0000    0000    0000    0400    0000    0000    0000    5000
0000270    74e5    0464    0000    1c00    006e    0000    0000    1c00
0000280    106e    0000    0000    1c00    106e    0000    0000    8c00
0000290    0000    0000    0000    8c00    0000    0000    0000    0400
00002a0    0000    0000    0000    e600    a3db    0665    0000    4800
00002b0    0071    0000    0000    4800    2071    0000    0000    4800
00002c0    2071    0000    0000    0800    0000    0000    0000    0800
00002d0    0000    0000    0000    0800    0000    0000    0000    0000
00002e0    0000    0000    0000    0000    0000    0000    0000    0000
*
0000340    0000    0000    0000    0000    0000    0000    0000    2f00
0000350    7375    2f72    696c    6562    6578    2f63    646c    732e
0000360    006f    0800    0000    0400    0000    0100    0000    4f00
0000370    6570    426e    4453    0000    0000    0000    0000    4300
0000380    0000    5b00    0000    0000    0000    5800    0000    0000
0000390    0000    5100    0000    5400    0000    1500    0000    0000
00003a0    0000    3300    0000    3d00    0000    0000    0000    4b00
00003b0    0000    1f00    0000    0000    0000    1e00    0000    3b00
00003c0    0000    3c00    0000    0000    0000    2e00    0000    3600
00003d0    0000    0000    0000    0000    0000    3a00    0000    0000
00003e0    0000    5300    0000    0000    0000    5900    0000    5700
00003f0    0000    0000    0000    0000    0000    4c00    0000    4100
0000400    0000    0000    0000    3700    0000    4600    0000    0000
0000410    0000    2c00    0000    4300    0000    2700    0000    4800
0000420    0000    0000    0000    5000    0000    0000    0000    4000
0000430    0000    2f00    0000    0000    0000    5200    0000    0000
0000440    0000    2a00    0000    1700    0000    5500    0000    0000
0000450    0000    1600    0000    4400    0000    5600    0000    4a00
0000460    0000    0000    0000    4d00    0000    4500    0000    3200
0000470    0000    4e00    0000    3900    0000    5a00    0000    1c00
0000480    0000    0000    0000    3800    0000    3e00    0000    0000
0000490    0000    0000    0000    0000    0000    0000    0000    0000
*
00004e0    0000    0000    0000    0e00    0000    0000    0000    0000
00004f0    0000    0000    0000    1800    0000    1400    0000    0000
0000500    0000    0000    0000    0000    0000    1000    0000    0000
0000510    0000    0000    0000    0000    0000    0000    0000    0000
0000520    0000    0000    0000    0000    0000    1a00    0000    0000
0000530    0000    0000    0000    0000    0000    0000    0000    1900
0000540    0000    0000    0000    0000    0000    0000    0000    0000
0000550    0000    2300    0000    0000    0000    0000    0000    3000
0000560    0000    2600    0000    0000    0000    0000    0000    0000
0000570    0000    3100    0000    0000    0000    0f00    0000    0000
0000580    0000    2000    0000    2d00    0000    1b00    0000    0000
0000590    0000    0000    0000    0000    0000    3400    0000    0000
00005a0    0000    1100    0000    1200    0000    2200    0000    2b00
00005b0    0000    0000    0000    2900    0000    3f00    0000    0000
00005c0    0000    1300    0000    4200    0000    0000    0000    3500
00005d0    0000    2100    0000    0000    0000    1d00    0000    2800
00005e0    0000    4900    0000    4f00    0000    2500    0000    4700
00005f0    0000    0000    0000    0000    0000    2400    0000    0000
0000600    0000    0000    0000    0000    0000    0000    0000    0000
0000610    0000    0000    0000    0000    0000    0300    0100    1800
0000620    0003    0000    0000    0000    0000    0000    0000    0000
0000630    0000    0300    0800    8000    0020    0000    0000    0000
0000640    0000    0000    0000    0000    0000    0300    0a00    5000
0000650    0024    0000    0000    0000    0000    0000    0000    0000
0000660    0000    0300    0b00    2000    0056    0000    0000    0000
0000670    0000    0000    0000    0000    0000    0300    0c00    4000
0000680    1056    0000    0000    0000    0000    0000    0000    0000
0000690    0000    0300    0d00    1c00    106e    0000    0000    0000
00006a0    0000    0000    0000    0000    0000    0300    0e00    a800
00006b0    106e    0000    0000    0000    0000    0000    0000    0000
00006c0    0000    0300    0f00    4800    2071    0000    0000    0000
00006d0    0000    0000    0000    0000    0000    0300    1000    5000
00006e0    2071    0000    0000    0000    0000    0000    0000    0000
00006f0    0000    0300    1300    e000    3074    0000    0000    0000
0000700    0000    0000    0000    0000    0000    0300    1400    f000
0000710    3074    0000    0000    0000    0000    0000    0000    0000
0000720    0000    0300    1500    0000    4075    0000    0000    0000
0000730    0000    0000    0000    0000    0000    0300    1600    2000
0000740    4081    0000    0000    0000    0000    0000    0000    1100
0000750    0000    1200    0000    0000    0000    0000    0000    2700
0000760    0000    0000    0000    1800    0000    1200    0000    0000
0000770    0000    0000    0000    a700    0007    0000    0000    0a00
0000780
bash-4.3#
bash-4.3# readelf
Usage: readelf <option(s)> elf-file(s)
 Display information about the contents of ELF format files
 Options are:
  -a --all               Equivalent to: -h -l -S -s -r -d -V -A -I
  -h --file-header       Display the ELF file header
  -l --program-headers   Display the program headers
     --segments          An alias for --program-headers
  -S --section-headers   Display the sections' header
     --sections          An alias for --section-headers
  -g --section-groups    Display the section groups
  -t --section-details   Display the section details
  -e --headers           Equivalent to: -h -l -S
  -s --syms              Display the symbol table
      --symbols          An alias for --syms
  -n --notes             Display the core notes (if present)
  -r --relocs            Display the relocations (if present)
  -u --unwind            Display the unwind info (if present)
  -d --dynamic           Display the dynamic section (if present)
  -V --version-info      Display the version sections (if present)
  -A --arch-specific     Display architecture specific information (if
any).
  -D --use-dynamic       Use the dynamic section info when displaying
symbols
  -x --hex-dump=<number> Dump the contents of section <number>
  -w[liaprmfFsoR] or

--debug-dump[=line,=info,=abbrev,=pubnames,=aranges,=macro,=frames,=str,=loc,=Ranges]
                         Display the contents of DWARF2 debug sections
  -I --histogram         Display histogram of bucket list lengths
  -W --wide              Allow output width to exceed 80 characters
  @<file>                Read options from <file>
  -H --help              Display this information
  -v --version           Display the version number of readelf
Report bugs to <URL:http://www.sourceware.org/bugzilla/>
bash-4.3#
bash-4.3# readelf ./c4 -a
readelf: Error: Unable to read in 0x246 bytes of section headers
ELF Header:
  Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
  Class:                             ELF64
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              <unknown>: 4141
  Machine:                           <unknown>: 4242
  Version:                           0x43434343
  Entry point address:               0xdeadbeefcafe
  Start of program headers:          4299030538 (bytes into file)
  Start of section headers:          9296 (bytes into file)
  Flags:                             0x40
  Size of this header:               0 (bytes)
  Size of program headers:           32522 (bytes)
  Number of program headers:         19525
  Size of section headers:           582 (bytes)
  Number of section headers:         257
  Section header string table index: 0
readelf: Error: Unable to read in 0x24846 bytes of section headers
readelf: Error: Section headers are not available!
Abort trap (core dumped)
bash-4.3#
bash-4.3# gdb -c readelf.core
GNU gdb 6.3
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you
are
welcome to change it and/or distribute copies of it under certain
conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for
details.
This GDB was configured as "amd64-unknown-openbsd6.0".
Core was generated by `readelf'.
Program terminated with signal 6, Aborted.
#0  0x000010e524d9459a in ?? ()
(gdb) bt
#0  0x000010e524d9459a in ?? ()
#1  0x000010e524d9a0f9 in ?? ()
#2  0x000010e524dc339a in ?? ()
#3  0x0000000000000000 in ?? ()
(gdb)
>Fix:
I havn't tried to make a patch yet.


Regards,
Marshall Whittaker

Reply via email to