|
| 1 | +"""E2E tests for ELF""" |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from checksec.elf import PIEType, RelroType |
| 8 | +from tests.conftest import run_checksec |
| 9 | + |
| 10 | +ELF_BINARIES = Path(__file__).parent.parent / "binaries" / "elf" |
| 11 | + |
| 12 | + |
| 13 | +@pytest.mark.parametrize("is_enabled", [False, True]) |
| 14 | +@pytest.mark.parametrize("prop", ["nx", "canary", "rpath", "runpath", "symbols", "fortify_source"]) |
| 15 | +def test_bool_prop(prop: str, is_enabled: bool): |
| 16 | + """Test that boolean prop is disabled/enabled""" |
| 17 | + libc_path = ELF_BINARIES / "libc-2.27.so" |
| 18 | + bin_path = ELF_BINARIES / f"{prop}_{'enabled' if is_enabled else 'disabled'}" |
| 19 | + chk_data = run_checksec(bin_path, libc_path) |
| 20 | + assert chk_data[str(bin_path)][prop] == is_enabled |
| 21 | + |
| 22 | + |
| 23 | +@pytest.mark.parametrize("relro_type", list(RelroType)) |
| 24 | +def test_relro(relro_type: RelroType): |
| 25 | + """Test that relro type is No/Partial/Full""" |
| 26 | + bin_path = ELF_BINARIES / f"relro_{relro_type.name.lower()}" |
| 27 | + chk_data = run_checksec(bin_path) |
| 28 | + assert chk_data[str(bin_path)]["relro"] == relro_type.name |
| 29 | + |
| 30 | + |
| 31 | +@pytest.mark.parametrize("pie_type", list(PIEType)) |
| 32 | +def test_pie(pie_type): |
| 33 | + """Test that PIE is No/Partial/Full""" |
| 34 | + bin_path = ELF_BINARIES / f"pie_{pie_type.name.lower()}" |
| 35 | + chk_data = run_checksec(bin_path) |
| 36 | + assert chk_data[str(bin_path)]["pie"] == pie_type.name |
| 37 | + |
| 38 | + |
| 39 | +def test_fortified(): |
| 40 | + """Test the fortified functions""" |
| 41 | + libc_path = ELF_BINARIES / "libc-2.27.so" |
| 42 | + bin_path = ELF_BINARIES / "fortify_funcs" |
| 43 | + chk_data = run_checksec(bin_path, libc_path) |
| 44 | + fortified_funcs = ["__fprintf_chk@@GLIBC_2.3.4", "__printf_chk@@GLIBC_2.3.4"] |
| 45 | + assert chk_data[str(bin_path)]["fortified"] == len(fortified_funcs) |
| 46 | + |
| 47 | + |
| 48 | +def test_fortifiable(): |
| 49 | + """Test the fortifiable functions""" |
| 50 | + libc_path = ELF_BINARIES / "libc-2.27.so" |
| 51 | + bin_path = ELF_BINARIES / "fortify_funcs" |
| 52 | + chk_data = run_checksec(bin_path, libc_path) |
| 53 | + fortified_funcs = ["__fprintf_chk@@GLIBC_2.3.4", "__printf_chk@@GLIBC_2.3.4"] |
| 54 | + non_fortified_funcs = ["fgets"] |
| 55 | + assert chk_data[str(bin_path)]["fortify-able"] == len(fortified_funcs) + len(non_fortified_funcs) |
| 56 | + |
| 57 | + |
| 58 | +def test_fortify_score(): |
| 59 | + """Test the fortify score""" |
| 60 | + libc_path = ELF_BINARIES / "libc-2.27.so" |
| 61 | + bin_path = ELF_BINARIES / "fortify_funcs" |
| 62 | + chk_data = run_checksec(bin_path, libc_path) |
| 63 | + fortified_funcs = ["__fprintf_chk@@GLIBC_2.3.4", "__printf_chk@@GLIBC_2.3.4"] |
| 64 | + non_fortified_funcs = ["fgets"] |
| 65 | + total = len(fortified_funcs) + len(non_fortified_funcs) |
| 66 | + assert chk_data[str(bin_path)]["fortify_score"] == round((2 * 100) / total, 0) |
0 commit comments