/src/yara/tests/oss-fuzz/filemap_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | Copyright (c) 2026. The YARA Authors. All Rights Reserved. |
3 | | |
4 | | Redistribution and use in source and binary forms, with or without modification, |
5 | | are permitted provided that the following conditions are met: |
6 | | |
7 | | 1. Redistributions of source code must retain the above copyright notice, this |
8 | | list of conditions and the following disclaimer. |
9 | | |
10 | | 2. Redistributions in binary form must reproduce the above copyright notice, |
11 | | this list of conditions and the following disclaimer in the documentation and/or |
12 | | other materials provided with the distribution. |
13 | | |
14 | | 3. Neither the name of the copyright holder nor the names of its contributors |
15 | | may be used to endorse or promote products derived from this software without |
16 | | specific prior written permission. |
17 | | |
18 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
19 | | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
20 | | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
21 | | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
22 | | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
23 | | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
24 | | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
25 | | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
26 | | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
27 | | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | | */ |
29 | | |
30 | | #include <stddef.h> |
31 | | #include <stdint.h> |
32 | | #include <stdio.h> |
33 | | #include <stdlib.h> |
34 | | #include <unistd.h> |
35 | | #include <sys/mman.h> |
36 | | #include <fcntl.h> |
37 | | |
38 | | extern "C" { |
39 | | #include <yara.h> |
40 | | #include <yara/filemap.h> |
41 | | } |
42 | | |
43 | | #include <fuzzer/FuzzedDataProvider.h> |
44 | | |
45 | | extern "C" int LLVMFuzzerInitialize(int* argc, char*** argv) |
46 | 2 | { |
47 | 2 | yr_initialize(); |
48 | 2 | return 0; |
49 | 2 | } |
50 | | |
51 | | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) |
52 | 86 | { |
53 | 86 | char temp_file[] = "/tmp/yara-filemap-fuzz-XXXXXX"; |
54 | 86 | int fd = mkstemp(temp_file); |
55 | 86 | if (fd < 0) return 0; |
56 | | |
57 | 86 | if (write(fd, data, size) != (ssize_t) size) |
58 | 0 | { |
59 | 0 | close(fd); |
60 | 0 | unlink(temp_file); |
61 | 0 | return 0; |
62 | 0 | } |
63 | | |
64 | 86 | YR_MAPPED_FILE mapped_file; |
65 | 86 | if (yr_filemap_map_fd(fd, 0, size, &mapped_file) == ERROR_SUCCESS) |
66 | 86 | { |
67 | 86 | yr_filemap_unmap_fd(&mapped_file); |
68 | 86 | } |
69 | | |
70 | | // Also try with some offsets and different sizes |
71 | 86 | FuzzedDataProvider fdp(data, size); |
72 | 86 | if (size > 0) |
73 | 86 | { |
74 | 86 | size_t offset = fdp.ConsumeIntegralInRange<size_t>(0, size - 1); |
75 | 86 | size_t map_size = fdp.ConsumeIntegralInRange<size_t>(0, size - offset); |
76 | 86 | if (yr_filemap_map_fd(fd, offset, map_size, &mapped_file) == ERROR_SUCCESS) |
77 | 52 | { |
78 | 52 | yr_filemap_unmap_fd(&mapped_file); |
79 | 52 | } |
80 | 86 | } |
81 | | |
82 | 86 | close(fd); |
83 | 86 | unlink(temp_file); |
84 | | |
85 | 86 | return 0; |
86 | 86 | } |