Skip to content

Commit 78d90c3

Browse files
committed
Adding section semantics to deal with semantically read-only sections inside of writable areas
1 parent 5e25409 commit 78d90c3

4 files changed

Lines changed: 83 additions & 25 deletions

File tree

binaryninjaapi.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,6 +1030,7 @@ namespace BinaryNinja
10301030
std::string linkedSection, infoSection;
10311031
uint64_t infoData;
10321032
uint64_t align, entrySize;
1033+
BNSectionSemantics semantics;
10331034
};
10341035

10351036
struct QualifiedNameAndType;
@@ -1159,6 +1160,8 @@ namespace BinaryNinja
11591160
bool IsOffsetWritable(uint64_t offset) const;
11601161
bool IsOffsetExecutable(uint64_t offset) const;
11611162
bool IsOffsetBackedByFile(uint64_t offset) const;
1163+
bool IsOffsetCodeSemantics(uint64_t offset) const;
1164+
bool IsOffsetWritableSemantics(uint64_t offset) const;
11621165
uint64_t GetNextValidOffset(uint64_t offset) const;
11631166

11641167
uint64_t GetStart() const;
@@ -1299,11 +1302,13 @@ namespace BinaryNinja
12991302
bool GetSegmentAt(uint64_t addr, Segment& result);
13001303
bool GetAddressForDataOffset(uint64_t offset, uint64_t& addr);
13011304

1302-
void AddAutoSection(const std::string& name, uint64_t start, uint64_t length, const std::string& type = "",
1305+
void AddAutoSection(const std::string& name, uint64_t start, uint64_t length,
1306+
BNSectionSemantics semantics = DefaultSectionSemantics, const std::string& type = "",
13031307
uint64_t align = 1, uint64_t entrySize = 0, const std::string& linkedSection = "",
13041308
const std::string& infoSection = "", uint64_t infoData = 0);
13051309
void RemoveAutoSection(const std::string& name);
1306-
void AddUserSection(const std::string& name, uint64_t start, uint64_t length, const std::string& type = "",
1310+
void AddUserSection(const std::string& name, uint64_t start, uint64_t length,
1311+
BNSectionSemantics semantics = DefaultSectionSemantics, const std::string& type = "",
13071312
uint64_t align = 1, uint64_t entrySize = 0, const std::string& linkedSection = "",
13081313
const std::string& infoSection = "", uint64_t infoData = 0);
13091314
void RemoveUserSection(const std::string& name);

binaryninjacore.h

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,6 +1504,14 @@ extern "C"
15041504
uint32_t flags;
15051505
};
15061506

1507+
enum BNSectionSemantics
1508+
{
1509+
DefaultSectionSemantics,
1510+
ReadOnlyCodeSectionSemantics,
1511+
ReadOnlyDataSectionSemantics,
1512+
ReadWriteDataSectionSemantics
1513+
};
1514+
15071515
struct BNSection
15081516
{
15091517
char* name;
@@ -1513,6 +1521,7 @@ extern "C"
15131521
char* infoSection;
15141522
uint64_t infoData;
15151523
uint64_t align, entrySize;
1524+
BNSectionSemantics semantics;
15161525
};
15171526

15181527
struct BNAddressRange
@@ -1727,6 +1736,8 @@ extern "C"
17271736
BINARYNINJACOREAPI bool BNIsOffsetWritable(BNBinaryView* view, uint64_t offset);
17281737
BINARYNINJACOREAPI bool BNIsOffsetExecutable(BNBinaryView* view, uint64_t offset);
17291738
BINARYNINJACOREAPI bool BNIsOffsetBackedByFile(BNBinaryView* view, uint64_t offset);
1739+
BINARYNINJACOREAPI bool BNIsOffsetCodeSemantics(BNBinaryView* view, uint64_t offset);
1740+
BINARYNINJACOREAPI bool BNIsOffsetWritableSemantics(BNBinaryView* view, uint64_t offset);
17301741
BINARYNINJACOREAPI uint64_t BNGetNextValidOffset(BNBinaryView* view, uint64_t offset);
17311742
BINARYNINJACOREAPI uint64_t BNGetStartOffset(BNBinaryView* view);
17321743
BINARYNINJACOREAPI uint64_t BNGetEndOffset(BNBinaryView* view);
@@ -1777,12 +1788,12 @@ extern "C"
17771788
BINARYNINJACOREAPI bool BNGetAddressForDataOffset(BNBinaryView* view, uint64_t offset, uint64_t* addr);
17781789

17791790
BINARYNINJACOREAPI void BNAddAutoSection(BNBinaryView* view, const char* name, uint64_t start, uint64_t length,
1780-
const char* type, uint64_t align, uint64_t entrySize, const char* linkedSection, const char* infoSection,
1781-
uint64_t infoData);
1791+
BNSectionSemantics semantics, const char* type, uint64_t align, uint64_t entrySize,
1792+
const char* linkedSection, const char* infoSection, uint64_t infoData);
17821793
BINARYNINJACOREAPI void BNRemoveAutoSection(BNBinaryView* view, const char* name);
17831794
BINARYNINJACOREAPI void BNAddUserSection(BNBinaryView* view, const char* name, uint64_t start, uint64_t length,
1784-
const char* type, uint64_t align, uint64_t entrySize, const char* linkedSection, const char* infoSection,
1785-
uint64_t infoData);
1795+
BNSectionSemantics semantics, const char* type, uint64_t align, uint64_t entrySize,
1796+
const char* linkedSection, const char* infoSection, uint64_t infoData);
17861797
BINARYNINJACOREAPI void BNRemoveUserSection(BNBinaryView* view, const char* name);
17871798
BINARYNINJACOREAPI BNSection* BNGetSections(BNBinaryView* view, size_t* count);
17881799
BINARYNINJACOREAPI BNSection* BNGetSectionsAt(BNBinaryView* view, uint64_t addr, size_t* count);

binaryview.cpp

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,18 @@ bool BinaryView::IsOffsetBackedByFile(uint64_t offset) const
758758
}
759759

760760

761+
bool BinaryView::IsOffsetCodeSemantics(uint64_t offset) const
762+
{
763+
return BNIsOffsetCodeSemantics(m_object, offset);
764+
}
765+
766+
767+
bool BinaryView::IsOffsetWritableSemantics(uint64_t offset) const
768+
{
769+
return BNIsOffsetWritableSemantics(m_object, offset);
770+
}
771+
772+
761773
uint64_t BinaryView::GetNextValidOffset(uint64_t offset) const
762774
{
763775
return BNGetNextValidOffset(m_object, offset);
@@ -1716,11 +1728,12 @@ bool BinaryView::GetAddressForDataOffset(uint64_t offset, uint64_t& addr)
17161728
}
17171729

17181730

1719-
void BinaryView::AddAutoSection(const string& name, uint64_t start, uint64_t length, const string& type,
1720-
uint64_t align, uint64_t entrySize, const string& linkedSection, const string& infoSection, uint64_t infoData)
1731+
void BinaryView::AddAutoSection(const string& name, uint64_t start, uint64_t length, BNSectionSemantics semantics,
1732+
const string& type, uint64_t align, uint64_t entrySize, const string& linkedSection,
1733+
const string& infoSection, uint64_t infoData)
17211734
{
1722-
BNAddAutoSection(m_object, name.c_str(), start, length, type.c_str(), align, entrySize, linkedSection.c_str(),
1723-
infoSection.c_str(), infoData);
1735+
BNAddAutoSection(m_object, name.c_str(), start, length, semantics, type.c_str(), align, entrySize,
1736+
linkedSection.c_str(), infoSection.c_str(), infoData);
17241737
}
17251738

17261739

@@ -1730,11 +1743,12 @@ void BinaryView::RemoveAutoSection(const string& name)
17301743
}
17311744

17321745

1733-
void BinaryView::AddUserSection(const string& name, uint64_t start, uint64_t length, const string& type,
1734-
uint64_t align, uint64_t entrySize, const string& linkedSection, const string& infoSection, uint64_t infoData)
1746+
void BinaryView::AddUserSection(const string& name, uint64_t start, uint64_t length, BNSectionSemantics semantics,
1747+
const string& type, uint64_t align, uint64_t entrySize, const string& linkedSection,
1748+
const string& infoSection, uint64_t infoData)
17351749
{
1736-
BNAddUserSection(m_object, name.c_str(), start, length, type.c_str(), align, entrySize, linkedSection.c_str(),
1737-
infoSection.c_str(), infoData);
1750+
BNAddUserSection(m_object, name.c_str(), start, length, semantics, type.c_str(), align, entrySize,
1751+
linkedSection.c_str(), infoSection.c_str(), infoData);
17381752
}
17391753

17401754

@@ -1762,6 +1776,7 @@ vector<Section> BinaryView::GetSections()
17621776
section.infoData = sections[i].infoData;
17631777
section.align = sections[i].align;
17641778
section.entrySize = sections[i].entrySize;
1779+
section.semantics = sections[i].semantics;
17651780
result.push_back(section);
17661781
}
17671782

@@ -1788,6 +1803,7 @@ vector<Section> BinaryView::GetSectionsAt(uint64_t addr)
17881803
section.infoData = sections[i].infoData;
17891804
section.align = sections[i].align;
17901805
section.entrySize = sections[i].entrySize;
1806+
section.semantics = sections[i].semantics;
17911807
result.push_back(section);
17921808
}
17931809

@@ -1811,6 +1827,7 @@ bool BinaryView::GetSectionByName(const string& name, Section& result)
18111827
result.infoData = section.infoData;
18121828
result.align = section.align;
18131829
result.entrySize = section.entrySize;
1830+
result.semantics = section.semantics;
18141831

18151832
BNFreeSection(&section);
18161833
return true;

python/binaryview.py

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
# Binary Ninja components
2828
import _binaryninjacore as core
2929
from enums import (AnalysisState, SymbolType, InstructionTextTokenType,
30-
Endianness, ModificationStatus, StringType, SegmentFlag)
30+
Endianness, ModificationStatus, StringType, SegmentFlag, SectionSemantics)
3131
import function
3232
import startup
3333
import architecture
@@ -422,7 +422,7 @@ def __repr__(self):
422422

423423

424424
class Section(object):
425-
def __init__(self, name, section_type, start, length, linked_section, info_section, info_data, align, entry_size):
425+
def __init__(self, name, section_type, start, length, linked_section, info_section, info_data, align, entry_size, semantics):
426426
self.name = name
427427
self.type = section_type
428428
self.start = start
@@ -432,6 +432,7 @@ def __init__(self, name, section_type, start, length, linked_section, info_secti
432432
self.info_data = info_data
433433
self.align = align
434434
self.entry_size = entry_size
435+
self.semantics = SectionSemantics(semantics)
435436

436437
@property
437438
def end(self):
@@ -899,7 +900,8 @@ def sections(self):
899900
for i in xrange(0, count.value):
900901
result[section_list[i].name] = Section(section_list[i].name, section_list[i].type, section_list[i].start,
901902
section_list[i].length, section_list[i].linkedSection, section_list[i].infoSection,
902-
section_list[i].infoData, section_list[i].align, section_list[i].entrySize)
903+
section_list[i].infoData, section_list[i].align, section_list[i].entrySize,
904+
section_list[i].semantics)
903905
core.BNFreeSectionList(section_list, count.value)
904906
return result
905907

@@ -1678,6 +1680,28 @@ def is_offset_executable(self, addr):
16781680
"""
16791681
return core.BNIsOffsetExecutable(self.handle, addr)
16801682

1683+
def is_offset_code_semantics(self, addr):
1684+
"""
1685+
``is_offset_code_semantics`` checks if an virtual address ``addr`` is semantically valid for code.
1686+
1687+
:param int addr: a virtual address to be checked
1688+
:return: true if the virtual address is valid for writing, false if the virtual address is invalid or error
1689+
:rtype: bool
1690+
"""
1691+
return core.BNIsOffsetCodeSemantics(self.handle, addr)
1692+
1693+
def is_offset_writable_semantics(self, addr):
1694+
"""
1695+
``is_offset_writable_semantics`` checks if an virtual address ``addr`` is semantically writable. Some sections
1696+
may have writable permissions for linking purposes but can be treated as read-only for the purposes of
1697+
analysis.
1698+
1699+
:param int addr: a virtual address to be checked
1700+
:return: true if the virtual address is valid for writing, false if the virtual address is invalid or error
1701+
:rtype: bool
1702+
"""
1703+
return core.BNIsOffsetWritableSemantics(self.handle, addr)
1704+
16811705
def save(self, dest):
16821706
"""
16831707
``save`` saves the original binary file to the provided destination ``dest`` along with any modifications.
@@ -3218,17 +3242,17 @@ def get_address_for_data_offset(self, offset):
32183242
return None
32193243
return address.value
32203244

3221-
def add_auto_section(self, name, start, length, type = "", align = 1, entry_size = 1, linked_section = "",
3222-
info_section = "", info_data = 0):
3223-
core.BNAddAutoSection(self.handle, name, start, length, type, align, entry_size, linked_section,
3245+
def add_auto_section(self, name, start, length, semantics = SectionSemantics.DefaultSectionSemantics,
3246+
type = "", align = 1, entry_size = 1, linked_section = "", info_section = "", info_data = 0):
3247+
core.BNAddAutoSection(self.handle, name, start, length, semantics, type, align, entry_size, linked_section,
32243248
info_section, info_data)
32253249

32263250
def remove_auto_section(self, name):
32273251
core.BNRemoveAutoSection(self.handle, name)
32283252

3229-
def add_user_section(self, name, start, length, type = "", align = 1, entry_size = 1, linked_section = "",
3230-
info_section = "", info_data = 0):
3231-
core.BNAddUserSection(self.handle, name, start, length, type, align, entry_size, linked_section,
3253+
def add_user_section(self, name, start, length, semantics = SectionSemantics.DefaultSectionSemantics,
3254+
type = "", align = 1, entry_size = 1, linked_section = "", info_section = "", info_data = 0):
3255+
core.BNAddUserSection(self.handle, name, start, length, semantics, type, align, entry_size, linked_section,
32323256
info_section, info_data)
32333257

32343258
def remove_user_section(self, name):
@@ -3241,7 +3265,8 @@ def get_sections_at(self, addr):
32413265
for i in xrange(0, count.value):
32423266
result.append(Section(section_list[i].name, section_list[i].type, section_list[i].start,
32433267
section_list[i].length, section_list[i].linkedSection, section_list[i].infoSection,
3244-
section_list[i].infoData, section_list[i].align, section_list[i].entrySize))
3268+
section_list[i].infoData, section_list[i].align, section_list[i].entrySize,
3269+
section_list[i].semantics))
32453270
core.BNFreeSectionList(section_list, count.value)
32463271
return result
32473272

@@ -3250,7 +3275,7 @@ def get_section_by_name(self, name):
32503275
if not core.BNGetSectionByName(self.handle, name, section):
32513276
return None
32523277
result = Section(section.name, section.type, section.start, section.length, section.linkedSection,
3253-
section.infoSection, section.infoData, section.align, section.entrySize)
3278+
section.infoSection, section.infoData, section.align, section.entrySize, section.semantics)
32543279
core.BNFreeSection(section)
32553280
return result
32563281

0 commit comments

Comments
 (0)