summaryrefslogtreecommitdiffstats
path: root/libblkid/src/fuzz.c
blob: 772340b6a1f5d3a6387775357aac81ad9652a6ac (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
#include "blkidP.h"
#include "fuzz.h"

#include <stdlib.h>
#include <unistd.h>

static int process_file(const char *name)
{
    int rc = -1;
    blkid_probe pr = blkid_new_probe_from_filename(name);
    if (pr != NULL) {
        blkid_probe_enable_partitions(pr, TRUE);
        blkid_probe_set_partitions_flags(pr, FALSE);
        blkid_probe_enable_superblocks(pr, TRUE);
        blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_DEFAULT | BLKID_SUBLKS_FSINFO | BLKID_SUBLKS_MAGIC | BLKID_SUBLKS_VERSION | BLKID_SUBLKS_BADCSUM);
        rc = blkid_do_safeprobe(pr) == -1 ? -1 : 0;
    }
    blkid_free_probe(pr);
    return rc;
}

int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
    int fd;
    char name[] = "/tmp/test-script-fuzz.XXXXXX";

    fd = mkostemp(name, O_RDWR|O_CREAT|O_EXCL|O_CLOEXEC);
    if (fd == -1)
        err(EXIT_FAILURE, "mkostemp() failed");

    if (write(fd, data, size) != (ssize_t)size)
        goto out;

    process_file(name);
out:
    close(fd);
    unlink(name);
    return 0;
}

#ifndef FUZZ_TARGET
int main(int argc, char **argv)
{
    for (int i = 1; i < argc; i++) {
        printf("%s ", argv[i]);
        if (process_file(argv[i]) == 0)
            printf(" OK\n");
        else
            printf(" FAILED\n");

    }
}
#endif