This file is indexed.

/usr/share/fusil/fuzzers/fusil-libc-printf is in fusil 1.4-1.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
Generate valid printf format to test GNU libc implementation

Written using manual page to get all options.
"""

FORMAT_TO_SIZE = {
    's': 'str',
    'S': 'wide str',
    'c': 'char',
    'C': 'wide char',
    'p': 'pointer',
    'm': None,
    '%': None,
    'n': 'write',

    'a': 'double',
    'A': 'double',
    'e': 'double',
    'E': 'double',
    'f': 'double',
    'F': 'double',
    'g': 'double',
    'G': 'double',

    'i': 'int',
    'u': 'int',
    'd': 'int',
    'x': 'int',
    'X': 'int',
    'o': 'int',
    'O': 'int',
}

from fusil.application import Application
from optparse import OptionGroup
from random import choice, randint
from fusil.c_tools import encodeUTF32, quoteString, CodeC, CompilerError
from fusil.process.create import CreateProcess
from fusil.process.watch import WatchProcess
from fusil.project_agent import ProjectAgent
from ptrace.compatibility import any

HELLO_UTF32 = encodeUTF32(u"Héllô")+"\0"*4

class Fuzzer(Application):
    NAME = "printf"

    def createFuzzerOptions(self, parser):
        options = OptionGroup(parser, "printf fuzzer")
        options.add_option("--asprintf", help="Don't use asprintf()",
            action="store_true")
        return options

    def setupProject(self):
        project = self.project
        printf = GeneratePrintfProgram(project, self.options)
        printf.max_nb_arg = 10

        # AVOID printf("%*d", 10000000, 42) crash
        #printf.max_width = 10*1000

        # AVOID "%.10000000s" crash
        #printf.max_precision = 10*1000

        # AVOID "%10000000hc" crash
        del printf.modifiers['char']['h']

        # AVOID "%qp" and "%llC" crashes
        for size in ('char', 'wide char', 'pointer'):
            for key in ('ll', 'q', 'j'):
                del printf.modifiers[size][key]

        process = PrintfProcess(project, name="printf", stdout='null')
        WatchProcess(process)

class PrintfProcess(CreateProcess):
    def on_printf_program(self, program):
        self.cmdline.arguments = [program]
        self.createProcess()

class GeneratePrintfArguments:
    def __init__(self, printf):
        self.printf = printf

    def genFormat(self, argument_index):
        # choose type
        prefix = None
        type = choice(self.printf.types)
        size = FORMAT_TO_SIZE[type]
        format = ['%']

        # add attribute
        format.append(choice(self.printf.format_attr))

        # add width
        rnd = randint(0, 2)
        if rnd == 1:
            format.append(str(randint(0, self.printf.max_width)))
        elif rnd == 2:
            width = randint(self.printf.min_width, self.printf.max_width)
            prefix = '/* width of x%s */ %s' % (argument_index, width)
            format.append('*')
            #if randint(0, 1) == 1:
            #    format.append('*')
            #else:
            #    format.append('%s$*%s$' % (
            #        argument_index+2, argument_index+1))

        # add precision
        if randint(0, 1) == 1:
            format.append('.%s' % randint(0, self.printf.max_precision))

        # add modifier
        if size in self.printf.modifiers:
            modifiers = self.printf.modifiers[size]
            keys = modifiers.keys()+[None]
            modifier = choice(keys)
            if modifier:
                format.append(modifier)
                size = modifiers[modifier]

        # add type
        format.append(type)
        return format, prefix, size

    def generate(self, nb_arg):
        text = []
        arguments = []
        for index in xrange(nb_arg):
            if text:
                text.append(' -- ')

            # Generate format and value
            format, prefix, size = self.genFormat(index)
            if size:
                value = self.printf.values[size]
            else:
                value = None

            # Append format and value
            format = ''.join(format)
            text.append('x%s' % index + '=' + format)
            if prefix:
                arguments.append(prefix)
            if value is not None:
                if value == "&written":
                    arguments.append('/* written bytes at x%s */ ' % index + value)
                else:
                    arguments.append('/* x%s value */ ' % index + value)
        text.append('\n')
        return [quoteString(''.join(text))]+arguments

class GeneratePrintfProgram(ProjectAgent):
    def __init__(self, project, options):
        ProjectAgent.__init__(self, project, "gen printf")
        self.has_asprintf = (not options.asprintf)

        # --- printf options ---
        self.min_nb_arg = 1
        self.max_nb_arg = 6
        self.types = 'aAcCdeEfFgGimnopuxXsS%'
        self.format_attr = ('#', '0', '-', ' ', '+', "'", 'I', '')
        self.min_width = 0
        self.max_width = 10*1000*1000
        self.max_precision = 10*1000*1000
        self.values = {
            'str': quoteString('Hello'),
            'wide str': quoteString(HELLO_UTF32),
            'char': "'A'",
            'wide char': "(wchar_t)322", # 'ł'
            'double': "(double)3.14",
            'short': "(short)7",
            'pointer': "(void *)0xDEADBEEF",
            'int': "(int)42",
            'intmax': "(intmax_t)232",
            'size_t': "(size_t)-1",
            'long': "(long)1234567890",
            'long long': "(long long)10101010",
            'ptrdiff_t': "(ptrdiff_t)100",
            'write': "&written",
            'long double': '(long double)5.92',
        }
        int_modifiers = {
            'hh': 'char',
            'h': 'short',
            'l': 'long',
            'll': 'long long',
            'q': 'long long',
            'j': 'intmax',
            'z': 'size_t',
            't': 'ptrdiff_t',
        }
        self.modifiers = {
            'int': dict(int_modifiers),
            'char': dict(int_modifiers),
            'wide char': dict(int_modifiers),
            'pointer': dict(int_modifiers),
            'str': {'l': 'wide str'},
            'wide str': {'l': 'wide str'},
            'double': {'L': 'long double'},
        }

    def on_session_start(self):
        self.use_locale = (randint(0, 1) == 0)
        if self.has_asprintf:
            self.use_asprintf = (randint(0, 1) == 0)
        else:
            self.use_asprintf = False

        # Generate printf() arguments
        nb_arg = randint(self.min_nb_arg, self.max_nb_arg)
        arguments = GeneratePrintfArguments(self).generate(nb_arg)
        self.info("Arguments: %s" % repr(arguments[1:]))
        self.info("Format: %s" % repr(arguments[0]))

        # Write C code to reproduce the bug
        code = CodeC()
        self.writeC(code, arguments)

        session = self.session()
        self.c_filename = session.createFilename("printf.c")
        self.program_filename = session.createFilename("printf")

        try:
            code.compile(self, self.c_filename, self.program_filename, options="-Wno-format")
        except CompilerError, err:
            self.error("Compiler error: %s" % err)
            self.send('project_stop')
            return
        self.send('printf_program', self.program_filename)

    def writeC(self, code, arguments):
        if self.use_asprintf:
            code.gnu_source = True
        code.includes = [
            '<stddef.h>',   # for ptrdiff_t
            '<stdint.h>',   # for intmax_t
            '<stdio.h>',    # for printf()
        ]
        if self.use_locale:
            code.includes.append('<locale.h>')  # for setlocale()
        main = code.addMain()

        if any( "&written" in text for text in arguments):
            main.variables.append('int written')

        if self.use_locale:
            main.callFunction('setlocale', ['LC_ALL', '""'])

        if self.use_asprintf:
            main.variables.append("char *text = NULL")
            arguments.insert(0, "&text")
            name = "asprintf"
        else:
            name = "printf"
        main.callFunction(name, arguments)

if __name__ == "__main__":
    Fuzzer().main()