Vector35 / binaryninja-api

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

Segments signedness issue #2881

Closed fabianfreyer closed 5 days ago

fabianfreyer commented 2 years ago

Version and Platform (required):

Bug Description: Segment addresses and lengths seem to have a signedness issue.

Steps To Reproduce:

  1. Create a small test binary:
    truncate -s 4 empty
  2. open with binja, select any arch.
  3. Run the following commands in the python shell:

    # can change these lengths arbitrarily within size
    len1 = 1
    len2 = 2
    bv.add_user_segment(0, len1, 0, len1, 5)
    bv.add_user_segment(len1, len2, len1, len2, 7)

    Segments now look like this:

    Type: Mapped
    Platform: x86_64
    Architecture: x86_64
    
    Segments:
    r-x  0x00000000-0x00000001 
    rwx  0x00000001-0x00000003 
    r-x  0x00000003-0x00000004 
  4. Save bndb
  5. Open bndb Segments now look like this:

    Type: Mapped
    Platform: x86_64
    Architecture: x86_64
    
    Segments:
    r-x  0x00000000-0x00000004 
    r-x  0xfffffffffffffffd-0xfffffffffffffffe 
    rwx  0xfffffffffffffffe-0x00000000 

Expected Behavior: Segments should be the same before and after saving.

Screenshots: Before saving: image

After saving: image

fabianfreyer commented 2 years ago

Closing in favor of #2882

psifertex commented 2 years ago

2882 has been renamed to describe a more broad feature that would include this fix as a super set but has been pushed out further to 3.2 so we're opening this to track just the existing bug and potentially a quicker temporary fix in the core during creation of user segments when no auto-segments exist such that this issue no longer occurs.

psifertex commented 2 years ago

Also, note that we believe that using the open-with-options JSON specification for segments is a temporary work-around in the meantime.

bpotchik commented 5 days ago

Previously, normalized offsets were based on bv.start. Now normalized offsets are based on bv.image_base. This allows us to support adding 'user' segments and/or memory regions before the image base. Now, rebasing and save/load operations preserve the memory map correctly.

Fixed in 4.2.5758.