Vector35 / binaryninja-api

Public API, examples, documentation and issues for Binary Ninja
https://binary.ninja/
MIT License
900 stars 203 forks source link

Lifting LLIL->MLIL shift value lifted incorrectly #3081

Closed fabianfreyer closed 2 years ago

fabianfreyer commented 2 years ago

Version and Platform (required):

Bug Description: The shift value is lifted incorrectly from LLIL to MLIL for some instructions, depending on how the corresponding variable is loaded.

Steps To Reproduce:

  1. Create the following binary:

    $objdump -d bug.bin
    
    bug.bin:        file format elf64-littleaarch64
    
    Disassembly of section .text:
    
    000000000021015c <_start>:
      21015c: 88 00 00 90   adrp    x8, 0x220000 <_start+0x40>
      210160: 0a c1 40 f9   ldr     x10, [x8, #384]
      210164: 48 01 40 39   ldrb    w8, [x10]
      210168: 49 01 40 39   ldrb    w9, [x10]
      21016c: 08 f1 7d d3   lsl     x8, x8, #3
      210170: 28 1d 75 b3   bfi     x8, x9, #11, #8
      210174: e0 03 08 aa   mov     x0, x8
      210178: c0 03 5f d6   ret
    $ xxd -r > bug.bin << EOF
    00000000: 7f45 4c46 0201 0100 0000 0000 0000 0000  .ELF............
    00000010: 0200 b700 0100 0000 5c01 2100 0000 0000  ........\.!.....
    00000020: 4000 0000 0000 0000 f002 0000 0000 0000  @...............
    00000030: 0000 0000 4000 3800 0500 4000 0800 0600  ....@.8...@.....
    00000040: 0600 0000 0400 0000 4000 0000 0000 0000  ........@.......
    00000050: 4000 2000 0000 0000 4000 2000 0000 0000  @. .....@. .....
    00000060: 1801 0000 0000 0000 1801 0000 0000 0000  ................
    00000070: 0800 0000 0000 0000 0100 0000 0400 0000  ................
    00000080: 0000 0000 0000 0000 0000 2000 0000 0000  .......... .....
    00000090: 0000 2000 0000 0000 5b01 0000 0000 0000  .. .....[.......
    000000a0: 5b01 0000 0000 0000 0000 0100 0000 0000  [...............
    000000b0: 0100 0000 0500 0000 5c01 0000 0000 0000  ........\.......
    000000c0: 5c01 2100 0000 0000 5c01 2100 0000 0000  \.!.....\.!.....
    000000d0: 2000 0000 0000 0000 2000 0000 0000 0000   ....... .......
    000000e0: 0000 0100 0000 0000 0100 0000 0600 0000  ................
    000000f0: 8001 0000 0000 0000 8001 2200 0000 0000  ..........".....
    00000100: 8001 2200 0000 0000 0800 0000 0000 0000  ..".............
    00000110: 0800 0000 0000 0000 0000 0100 0000 0000  ................
    00000120: 51e5 7464 0600 0000 0000 0000 0000 0000  Q.td............
    00000130: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00000140: 0000 0000 0000 0000 0000 0001 0000 0000  ................
    00000150: 0000 0000 0000 0000 1337 0000 8800 0090  .........7......
    00000160: 0ac1 40f9 4801 4039 4901 4039 08f1 7dd3  ..@.H.@9I.@9..}.
    00000170: 281d 75b3 e003 08aa c003 5fd6 0000 0000  (.u......._.....
    00000180: 5801 2000 0000 0000 0048 6f6d 6562 7265  X. ......Homebre
    00000190: 7720 636c 616e 6720 7665 7273 696f 6e20  w clang version
    000001a0: 3133 2e30 2e30 004c 696e 6b65 723a 2048  13.0.0.Linker: H
    000001b0: 6f6d 6562 7265 7720 4c4c 4420 3133 2e30  omebrew LLD 13.0
    000001c0: 2e30 0000 0000 0000 0000 0000 0000 0000  .0..............
    000001d0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    000001e0: 0100 0000 0400 f1ff 0000 0000 0000 0000  ................
    000001f0: 0000 0000 0000 0000 0a00 0000 0000 0200  ................
    00000200: 5c01 2100 0000 0000 0000 0000 0000 0000  \.!.............
    00000210: 0f00 0000 0000 0100 5801 2000 0000 0000  ........X. .....
    00000220: 0000 0000 0000 0000 1400 0000 0000 0300  ................
    00000230: 8001 2200 0000 0000 0000 0000 0000 0000  ..".............
    00000240: 1900 0000 0000 0400 0000 0000 0000 0000  ................
    00000250: 0000 0000 0000 0000 1e00 0000 1200 0200  ................
    00000260: 5c01 2100 0000 0000 2000 0000 0000 0000  \.!..... .......
    00000270: 2500 0000 1100 0300 8001 2200 0000 0000  %.........".....
    00000280: 0800 0000 0000 0000 002e 726f 6461 7461  ..........rodata
    00000290: 002e 7465 7874 002e 6461 7461 002e 636f  ..text..data..co
    000002a0: 6d6d 656e 7400 2e73 796d 7461 6200 2e73  mment..symtab..s
    000002b0: 6873 7472 7461 6200 2e73 7472 7461 6200  hstrtab..strtab.
    000002c0: 0062 6f72 6b65 6e2e 6300 2478 2e30 0024  .borken.c.$x.0.$
    000002d0: 642e 3100 2464 2e32 0024 642e 3300 5f73  d.1.$d.2.$d.3._s
    000002e0: 7461 7274 0064 6174 6100 0000 0000 0000  tart.data.......
    000002f0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00000300: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00000310: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00000320: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00000330: 0100 0000 0100 0000 3200 0000 0000 0000  ........2.......
    00000340: 5801 2000 0000 0000 5801 0000 0000 0000  X. .....X.......
    00000350: 0300 0000 0000 0000 0000 0000 0000 0000  ................
    00000360: 0100 0000 0000 0000 0100 0000 0000 0000  ................
    00000370: 0900 0000 0100 0000 0600 0000 0000 0000  ................
    00000380: 5c01 2100 0000 0000 5c01 0000 0000 0000  \.!.....\.......
    00000390: 2000 0000 0000 0000 0000 0000 0000 0000   ...............
    000003a0: 0400 0000 0000 0000 0000 0000 0000 0000  ................
    000003b0: 0f00 0000 0100 0000 0300 0000 0000 0000  ................
    000003c0: 8001 2200 0000 0000 8001 0000 0000 0000  ..".............
    000003d0: 0800 0000 0000 0000 0000 0000 0000 0000  ................
    000003e0: 0800 0000 0000 0000 0000 0000 0000 0000  ................
    000003f0: 1500 0000 0100 0000 3000 0000 0000 0000  ........0.......
    00000400: 0000 0000 0000 0000 8801 0000 0000 0000  ................
    00000410: 3b00 0000 0000 0000 0000 0000 0000 0000  ;...............
    00000420: 0100 0000 0000 0000 0100 0000 0000 0000  ................
    00000430: 1e00 0000 0200 0000 0000 0000 0000 0000  ................
    00000440: 0000 0000 0000 0000 c801 0000 0000 0000  ................
    00000450: c000 0000 0000 0000 0700 0000 0600 0000  ................
    00000460: 0800 0000 0000 0000 1800 0000 0000 0000  ................
    00000470: 2600 0000 0300 0000 0000 0000 0000 0000  &...............
    00000480: 0000 0000 0000 0000 8802 0000 0000 0000  ................
    00000490: 3800 0000 0000 0000 0000 0000 0000 0000  8...............
    000004a0: 0100 0000 0000 0000 0000 0000 0000 0000  ................
    000004b0: 3000 0000 0300 0000 0000 0000 0000 0000  0...............
    000004c0: 0000 0000 0000 0000 c002 0000 0000 0000  ................
    000004d0: 2a00 0000 0000 0000 0000 0000 0000 0000  *...............
    000004e0: 0100 0000 0000 0000 0000 0000 0000 0000  ................
    EOF
    $ sha256sum bug.bin
    d9f5ca00b060b0621b73bd4f5b32c669b0b36d22d50610e1daaf0e8ac4834784  bug.bin
  2. Look at _start in LLIL. Consider the LLIL for the bfi instruction. Observe that x9 is left-shifted by 0xb. This is correct.

    image
  3. Look at _start in MLIL. Consider the MLIL for the same instruction. Observe that the MLIL variable x9 is left-shifted by 1, which is displayed as true. This is incorrect.

    image

Expected Behavior: The bfi instruction is lifted as x8_2 = (-0x7f801 & x8_1) | zx.q(x9) << 0xb

Additional Information: This behavior depends on how the registers used in the bfi instruction are loaded. If they are loaded using a mov instead of using ldrb, the instruction is lifted correctly through constant propagation.

The following file can be used to reproduce this correct behavior.

$ objdump -d nobug.bin 

nobug.bin:      file format elf64-littleaarch64

Disassembly of section .text:

0000000000210120 <_start>:
  210120: 68 02 80 52   mov     w8, #19
  210124: e9 06 80 52   mov     w9, #55
  210128: 08 f1 7d d3   lsl     x8, x8, #3
  21012c: 28 1d 75 b3   bfi     x8, x9, #11, #8
  210130: e0 03 08 aa   mov     x0, x8
  210134: c0 03 5f d6   ret
$ xxd -r > nobug.bin << EOF
00000000: 7f45 4c46 0201 0100 0000 0000 0000 0000  .ELF............
00000010: 0200 b700 0100 0000 2001 2100 0000 0000  ........ .!.....
00000020: 4000 0000 0000 0000 3802 0000 0000 0000  @.......8.......
00000030: 0000 0000 4000 3800 0400 4000 0600 0400  ....@.8...@.....
00000040: 0600 0000 0400 0000 4000 0000 0000 0000  ........@.......
00000050: 4000 2000 0000 0000 4000 2000 0000 0000  @. .....@. .....
00000060: e000 0000 0000 0000 e000 0000 0000 0000  ................
00000070: 0800 0000 0000 0000 0100 0000 0400 0000  ................
00000080: 0000 0000 0000 0000 0000 2000 0000 0000  .......... .....
00000090: 0000 2000 0000 0000 2001 0000 0000 0000  .. ..... .......
000000a0: 2001 0000 0000 0000 0000 0100 0000 0000   ...............
000000b0: 0100 0000 0500 0000 2001 0000 0000 0000  ........ .......
000000c0: 2001 2100 0000 0000 2001 2100 0000 0000   .!..... .!.....
000000d0: 1800 0000 0000 0000 1800 0000 0000 0000  ................
000000e0: 0000 0100 0000 0000 51e5 7464 0600 0000  ........Q.td....
000000f0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000100: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000110: 0000 0001 0000 0000 0000 0000 0000 0000  ................
00000120: 6802 8052 e906 8052 08f1 7dd3 281d 75b3  h..R...R..}.(.u.
00000130: e003 08aa c003 5fd6 0048 6f6d 6562 7265  ......_..Homebre
00000140: 7720 636c 616e 6720 7665 7273 696f 6e20  w clang version 
00000150: 3133 2e30 2e30 004c 696e 6b65 723a 2048  13.0.0.Linker: H
00000160: 6f6d 6562 7265 7720 4c4c 4420 3133 2e30  omebrew LLD 13.0
00000170: 2e30 0000 0000 0000 0000 0000 0000 0000  .0..............
00000180: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000190: 0100 0000 0400 f1ff 0000 0000 0000 0000  ................
000001a0: 0000 0000 0000 0000 0800 0000 0000 0100  ................
000001b0: 2001 2100 0000 0000 0000 0000 0000 0000   .!.............
000001c0: 0d00 0000 0000 0200 0000 0000 0000 0000  ................
000001d0: 0000 0000 0000 0000 1200 0000 1200 0100  ................
000001e0: 2001 2100 0000 0000 1800 0000 0000 0000   .!.............
000001f0: 002e 7465 7874 002e 636f 6d6d 656e 7400  ..text..comment.
00000200: 2e73 796d 7461 6200 2e73 6873 7472 7461  .symtab..shstrta
00000210: 6200 2e73 7472 7461 6200 006d 6169 6e2e  b..strtab..main.
00000220: 6300 2478 2e30 0024 642e 3100 5f73 7461  c.$x.0.$d.1._sta
00000230: 7274 0000 0000 0000 0000 0000 0000 0000  rt..............
00000240: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000250: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000260: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000270: 0000 0000 0000 0000 0100 0000 0100 0000  ................
00000280: 0600 0000 0000 0000 2001 2100 0000 0000  ........ .!.....
00000290: 2001 0000 0000 0000 1800 0000 0000 0000   ...............
000002a0: 0000 0000 0000 0000 0400 0000 0000 0000  ................
000002b0: 0000 0000 0000 0000 0700 0000 0100 0000  ................
000002c0: 3000 0000 0000 0000 0000 0000 0000 0000  0...............
000002d0: 3801 0000 0000 0000 3b00 0000 0000 0000  8.......;.......
000002e0: 0000 0000 0000 0000 0100 0000 0000 0000  ................
000002f0: 0100 0000 0000 0000 1000 0000 0200 0000  ................
00000300: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000310: 7801 0000 0000 0000 7800 0000 0000 0000  x.......x.......
00000320: 0500 0000 0400 0000 0800 0000 0000 0000  ................
00000330: 1800 0000 0000 0000 1800 0000 0300 0000  ................
00000340: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000350: f001 0000 0000 0000 2a00 0000 0000 0000  ........*.......
00000360: 0000 0000 0000 0000 0100 0000 0000 0000  ................
00000370: 0000 0000 0000 0000 2200 0000 0300 0000  ........".......
00000380: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000390: 1a02 0000 0000 0000 1900 0000 0000 0000  ................
000003a0: 0000 0000 0000 0000 0100 0000 0000 0000  ................
000003b0: 0000 0000 0000 0000                      ........
EOF
$ sha256sum nobug.bin
764e07d6c252f437cffbc0aba95673c077b229a5a7067861202f7ecd4838e4aa  nobug.bin
bpotchik commented 2 years ago

This impacted bfi, and several other instructions with similar shift logic. It was caused lifting a width field with a zero size.

bpotchik commented 2 years ago

Fixed in 3.0.3368-dev.

fabianfreyer commented 2 years ago

Thanks!