/src/fuzz/ext2fs_read_bitmap_fuzzer.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 2020 Google LLC |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #include <stddef.h> |
16 | | #include <stdint.h> |
17 | | #include <unistd.h> |
18 | | #include <assert.h> |
19 | | #include <sys/syscall.h> |
20 | | #include <linux/memfd.h> |
21 | | #include <fuzzer/FuzzedDataProvider.h> |
22 | | |
23 | | #include "ext2fs/ext2fs.h" |
24 | | |
25 | 313 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
26 | | |
27 | 313 | enum FuzzerType { |
28 | 313 | ext2fsReadBlockBitmap, |
29 | 313 | ext2fsReadInodeBitmap, |
30 | 313 | kMaxValue = ext2fsReadInodeBitmap |
31 | 313 | }; |
32 | | |
33 | 313 | FuzzedDataProvider stream(data, size); |
34 | 313 | const FuzzerType f = stream.ConsumeEnum<FuzzerType>(); |
35 | 313 | (void) stream.ConsumeIntegral<int>(); |
36 | | |
37 | 313 | static const char* fname = "ext2_test_file"; |
38 | | |
39 | | // Write our data to a temp file. |
40 | 313 | int fd = syscall(SYS_memfd_create, fname, 0); |
41 | 313 | std::vector<char> buffer = stream.ConsumeRemainingBytes<char>(); |
42 | 313 | write(fd, buffer.data(), buffer.size()); |
43 | | |
44 | 313 | std::string fspath("/proc/self/fd/" + std::to_string(fd)); |
45 | | |
46 | 313 | ext2_filsys fs; |
47 | 313 | errcode_t retval = ext2fs_open( |
48 | 313 | fspath.c_str(), |
49 | 313 | EXT2_FLAG_IGNORE_CSUM_ERRORS, 0, 0, |
50 | 313 | unix_io_manager, |
51 | 313 | &fs); |
52 | | |
53 | 313 | if (!retval) { |
54 | 260 | switch (f) { |
55 | 217 | case ext2fsReadBlockBitmap: { |
56 | 217 | ext2fs_read_block_bitmap(fs); |
57 | 217 | break; |
58 | 0 | } |
59 | 43 | case ext2fsReadInodeBitmap: { |
60 | 43 | ext2fs_read_inode_bitmap(fs); |
61 | 43 | break; |
62 | 0 | } |
63 | 0 | default: { |
64 | 0 | assert(false); |
65 | 0 | } |
66 | 260 | } |
67 | 260 | ext2fs_close(fs); |
68 | 260 | } |
69 | 313 | close(fd); |
70 | | |
71 | 313 | return 0; |
72 | 313 | } |