Skip to content

Commit a840b02

Browse files
committed
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 a840b02

File tree

4 files changed

+15
-4
lines changed

4 files changed

+15
-4
lines changed

checksec/binary.py

+6-1
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

+6
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/binaries

Submodule binaries updated 1 file

tests/e2e/test_e2e_elf.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def test_bool_prop(prop: str, is_enabled: bool):
2222

2323
@pytest.mark.parametrize("relro_type", list(RelroType))
2424
def test_relro(relro_type: RelroType):
25-
"""Test that relro type is No/Partial/Full"""
25+
"""Test that relro type is No/Partial/Full/NA"""
2626
bin_path = ELF_BINARIES / f"relro_{relro_type.name.lower()}"
2727
chk_data = run_checksec(bin_path)
2828
assert chk_data[str(bin_path)]["relro"] == relro_type.name
@@ -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)