Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 4b042e4

Browse files
committedMar 6, 2025·
Handle ELF binary with no program segments
Do not return False for NX and No for RELRO when there is no program segments in the ELF binary file (e.g. kernel module). This is inspired from slimm609/checksec@29aea68 For RELRO, a new NA value is added For NX, True is returned to avoid changing NX type from boolean to string Signed-off-by: Fabrice Fontaine <fontaine.fabrice@gmail.com>
1 parent 265d45c commit 4b042e4

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed
 

‎checksec/binary.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@ def __init__(self, bin_path: Path):
1919

2020
@property
2121
def has_nx(self) -> bool:
22-
return self.bin.has_nx
22+
# Handle ELF binary with no program segments (e.g., Kernel modules)
23+
# In this case, return True
24+
if isinstance(self.bin, lief.ELF.Binary) and len(self.bin.segments) == 0:
25+
return True
26+
else:
27+
return self.bin.has_nx
2328

2429
@property
2530
def checksec_state(self) -> Union["ELFChecksecData", "PEChecksecData"]:

‎checksec/elf.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class RelroType(Enum):
7070
No = 1
7171
Partial = 2
7272
Full = 3
73+
NA = 4
7374

7475

7576
class PIEType(Enum):
@@ -117,6 +118,11 @@ def set_dyn_syms(self) -> FrozenSet[str]:
117118

118119
@property
119120
def relro(self) -> RelroType:
121+
# Handle binary with no program segments (e.g., Kernel modules)
122+
# In this case, return NA
123+
if len(self.bin.segments) == 0:
124+
return RelroType.NA
125+
120126
if self.bin.get(lief.ELF.Segment.TYPE.GNU_RELRO) is None:
121127
return RelroType.No
122128

‎tests/e2e/test_e2e_elf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def test_relro_full_df1():
3737

3838
@pytest.mark.parametrize("pie_type", list(PIEType))
3939
def test_pie(pie_type):
40-
"""Test that PIE is No/Partial/Full"""
40+
"""Test that PIE is No/Partial/Full/NA"""
4141
bin_path = ELF_BINARIES / f"pie_{pie_type.name.lower()}"
4242
chk_data = run_checksec(bin_path)
4343
assert chk_data[str(bin_path)]["pie"] == pie_type.name

0 commit comments

Comments
 (0)
Please sign in to comment.