summaryrefslogtreecommitdiff
path: root/tests/format/elf/oob_section_name.py
blob: 64d7dc3b75b02c10bc9dee92dc8b8f626d858b01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/python
# -*- coding: utf-8 -*-


# Une section peut avoir un index pour son nom démesurément grand (et invalide).
#
# Si la section des chaînes de caractères est toute aussi grande et invalide,
# l'index invalide reste suffisamment cohérent pour passer les premiers tests
# de extract_name_from_elf_string_section() et conduire ensuite à un plantage
# lors de l'accès concret, au moment de l'appel à strlen().


from chrysacase import ChrysalideTestCase
from pychrysalide.analysis.contents import FileContent
from pychrysalide.format.elf import ElfFormat
import os
import sys


class TestNonExistingBinary(ChrysalideTestCase):
    """TestCase for corrupted ELF binaries with wrong section names."""

    @classmethod
    def setUpClass(cls):

        super(TestNonExistingBinary, cls).setUpClass()

        cls.log('Compile binary "oob_section_name" if needed...')

        fullname = sys.modules[cls.__module__].__file__
        dirpath = os.path.dirname(fullname)

        os.system('make -C %s oob_section_name > /dev/null 2>&1' % dirpath)


    @classmethod
    def tearDownClass(cls):

        super(TestNonExistingBinary, cls).tearDownClass()

        cls.log('Delete built binaries...')

        fullname = sys.modules[cls.__module__].__file__
        dirpath = os.path.dirname(fullname)

        os.system('make -C %s clean > /dev/null 2>&1' % dirpath)


    def testOOBSectionName(self):
        """Avoid crashing when dealing with OutOfBound section names."""

        fullname = sys.modules[self.__class__.__module__].__file__
        filename = os.path.basename(fullname)

        baselen = len(fullname) - len(filename)

        cnt = FileContent(fullname[:baselen] + 'oob_section_name')
        self.assertIsNotNone(cnt)

        fmt = ElfFormat(cnt)
        self.assertIsInstance(fmt, ElfFormat)