/src/fuzz/ext2fs_image_read_write_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 | 571 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
26 | | |
27 | 571 | enum FuzzerType { |
28 | 571 | ext2fsImageBitmapRead, |
29 | 571 | ext2fsImageInodeRead, |
30 | 571 | ext2fsImageSuperRead, |
31 | 571 | ext2fsImageBitmapWrite, |
32 | 571 | ext2fsImageInodeWrite, |
33 | 571 | ext2fsImageSuperWrite, |
34 | 571 | kMaxValue = ext2fsImageSuperWrite |
35 | 571 | }; |
36 | | |
37 | 571 | FuzzedDataProvider stream(data, size); |
38 | 571 | const FuzzerType f = stream.ConsumeEnum<FuzzerType>(); |
39 | 571 | (void) stream.ConsumeIntegral<int>(); |
40 | | // Keep this here to not spoil the corpus or reproducers |
41 | | |
42 | 571 | static const char* fname = "ext2_test_file"; |
43 | | |
44 | | // Write our data to a temp file. |
45 | 571 | int fd = syscall(SYS_memfd_create, fname, 0); |
46 | 571 | std::vector<char> buffer = stream.ConsumeRemainingBytes<char>(); |
47 | 571 | write(fd, buffer.data(), buffer.size()); |
48 | | |
49 | 571 | std::string fspath("/proc/self/fd/" + std::to_string(fd)); |
50 | | |
51 | 571 | ext2_filsys fs; |
52 | 571 | errcode_t retval = ext2fs_open( |
53 | 571 | fspath.c_str(), |
54 | 571 | EXT2_FLAG_IGNORE_CSUM_ERRORS, 0, 0, |
55 | 571 | unix_io_manager, |
56 | 571 | &fs); |
57 | | |
58 | 571 | if (!retval) { |
59 | 430 | switch (f) { |
60 | 97 | case ext2fsImageBitmapRead: { |
61 | 97 | ext2fs_image_bitmap_read(fs, fd, 0); |
62 | 97 | break; |
63 | 0 | } |
64 | 13 | case ext2fsImageInodeRead: { |
65 | 13 | ext2fs_image_inode_read(fs, fd, 0); |
66 | 13 | break; |
67 | 0 | } |
68 | 3 | case ext2fsImageSuperRead: { |
69 | 3 | ext2fs_image_super_read(fs, fd, 0); |
70 | 3 | break; |
71 | 0 | } |
72 | 130 | case ext2fsImageBitmapWrite: { |
73 | 130 | ext2fs_image_bitmap_write(fs, fd, 0); |
74 | 130 | break; |
75 | 0 | } |
76 | 179 | case ext2fsImageInodeWrite: { |
77 | 179 | ext2fs_image_inode_write(fs, fd, 0); |
78 | 179 | break; |
79 | 0 | } |
80 | 8 | case ext2fsImageSuperWrite: { |
81 | 8 | ext2fs_image_super_write(fs, fd, 0); |
82 | 8 | break; |
83 | 0 | } |
84 | 0 | default: { |
85 | 0 | assert(false); |
86 | 0 | } |
87 | 430 | } |
88 | 430 | ext2fs_close(fs); |
89 | 430 | } |
90 | 571 | close(fd); |
91 | | |
92 | 571 | return 0; |
93 | 571 | } |