1*4c89978fSchristos /* $NetBSD: magic_fuzzer.c,v 1.1.1.1 2022/09/24 20:07:53 christos Exp $ */
2*4c89978fSchristos
3*4c89978fSchristos /*
4*4c89978fSchristos * Redistribution and use in source and binary forms, with or without
5*4c89978fSchristos * modification, are permitted provided that the following conditions
6*4c89978fSchristos * are met:
7*4c89978fSchristos * 1. Redistributions of source code must retain the above copyright
8*4c89978fSchristos * notice immediately at the beginning of the file, without modification,
9*4c89978fSchristos * this list of conditions, and the following disclaimer.
10*4c89978fSchristos * 2. Redistributions in binary form must reproduce the above copyright
11*4c89978fSchristos * notice, this list of conditions and the following disclaimer in the
12*4c89978fSchristos * documentation and/or other materials provided with the distribution.
13*4c89978fSchristos *
14*4c89978fSchristos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*4c89978fSchristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*4c89978fSchristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*4c89978fSchristos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
18*4c89978fSchristos * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*4c89978fSchristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*4c89978fSchristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*4c89978fSchristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*4c89978fSchristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*4c89978fSchristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*4c89978fSchristos * SUCH DAMAGE.
25*4c89978fSchristos */
26*4c89978fSchristos /*
27*4c89978fSchristos * LLVM fuzzing integration.
28*4c89978fSchristos */
29*4c89978fSchristos
30*4c89978fSchristos #include "file.h"
31*4c89978fSchristos
32*4c89978fSchristos #ifndef lint
33*4c89978fSchristos #if 0
34*4c89978fSchristos FILE_RCSID("@(#)$File: magic_fuzzer.c,v 1.1 2017/04/24 19:41:34 christos Exp $")
35*4c89978fSchristos #else
36*4c89978fSchristos __RCSID("$NetBSD: magic_fuzzer.c,v 1.1.1.1 2022/09/24 20:07:53 christos Exp $");
37*4c89978fSchristos #endif
38*4c89978fSchristos #endif /* lint */
39*4c89978fSchristos
40*4c89978fSchristos #include "magic.h"
41*4c89978fSchristos #include <libgen.h>
42*4c89978fSchristos #include <stdlib.h>
43*4c89978fSchristos #include <err.h>
44*4c89978fSchristos
45*4c89978fSchristos int LLVMFuzzerInitialize(int *, char ***);
46*4c89978fSchristos int LLVMFuzzerTestOneInput(const uint8_t *, size_t);
47*4c89978fSchristos
48*4c89978fSchristos static magic_t magic;
49*4c89978fSchristos
50*4c89978fSchristos int
LLVMFuzzerInitialize(int * argc,char *** argv)51*4c89978fSchristos LLVMFuzzerInitialize(int *argc, char ***argv)
52*4c89978fSchristos {
53*4c89978fSchristos char dfile[MAXPATHLEN], mfile[MAXPATHLEN];
54*4c89978fSchristos
55*4c89978fSchristos magic = magic_open(MAGIC_NONE);
56*4c89978fSchristos if (magic == NULL) {
57*4c89978fSchristos warn("magic_open");
58*4c89978fSchristos return -1;
59*4c89978fSchristos }
60*4c89978fSchristos
61*4c89978fSchristos // Poor man's strlcpy(3), to avoid potentially destructive dirname(3)
62*4c89978fSchristos snprintf(dfile, sizeof(dfile), "%s", (*argv)[0]);
63*4c89978fSchristos snprintf(mfile, sizeof(mfile), "%s/magic", dirname(dfile));
64*4c89978fSchristos
65*4c89978fSchristos if (magic_load(magic, mfile) == -1) {
66*4c89978fSchristos warnx("magic_load: %s", magic_error(magic));
67*4c89978fSchristos return -1;
68*4c89978fSchristos }
69*4c89978fSchristos
70*4c89978fSchristos return 0;
71*4c89978fSchristos }
72*4c89978fSchristos
73*4c89978fSchristos int
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)74*4c89978fSchristos LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
75*4c89978fSchristos {
76*4c89978fSchristos if (size == 0)
77*4c89978fSchristos return 0;
78*4c89978fSchristos
79*4c89978fSchristos magic_buffer(magic, data, size);
80*4c89978fSchristos return 0;
81*4c89978fSchristos }
82