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 // #define USE_FLAGS
16 // #define DUMP_SUPER
17 // #define SAVE_FS_IMAGE
18
19 #include <stdio.h>
20 #include <stddef.h>
21 #include <stdint.h>
22 #include <unistd.h>
23 #include <assert.h>
24 #include <fcntl.h>
25 #include <sys/syscall.h>
26 #include <linux/memfd.h>
27 #include <fuzzer/FuzzedDataProvider.h>
28
29 #include "ext2fs/ext2fs.h"
30 extern "C" {
31 #include "e2p/e2p.h"
32 #include "support/print_fs_flags.h"
33 }
34
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)35 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
36
37 const char *progname = "ext2fs_read_bitmap_fuzzer";
38 add_error_table(&et_ext2_error_table);
39
40 enum FuzzerType {
41 ext2fsReadBlockBitmap,
42 ext2fsReadInodeBitmap,
43 kMaxValue = ext2fsReadInodeBitmap
44 };
45
46 FuzzedDataProvider stream(data, size);
47 const FuzzerType f = stream.ConsumeEnum<FuzzerType>();
48 int flags = stream.ConsumeIntegral<int>();
49 #ifndef USE_FLAGS
50 flags = 0;
51 #endif
52
53 static const char* fname = "/tmp/ext2_test_file";
54
55 // Write our data to a temp file.
56 #ifdef SAVE_FS_IMAGE
57 int fd = open(fname, O_CREAT|O_TRUNC|O_RDWR, 0644);
58 #else
59 int fd = syscall(SYS_memfd_create, fname, 0);
60 #endif
61 std::vector<char> buffer = stream.ConsumeRemainingBytes<char>();
62 write(fd, buffer.data(), buffer.size());
63
64 std::string fspath("/proc/self/fd/" + std::to_string(fd));
65
66 ext2_filsys fs;
67 #ifdef USE_FLAGS
68 printf("Flags: 0x%08x ", flags);
69 print_fs_flags(stdout, flags);
70 flags &= ~EXT2_FLAG_NOFREE_ON_ERROR;
71 #endif
72 errcode_t retval = ext2fs_open(
73 fspath.c_str(),
74 flags | EXT2_FLAG_IGNORE_CSUM_ERRORS, 0, 0,
75 unix_io_manager,
76 &fs);
77
78 if (retval) {
79 com_err(progname, retval, "while trying to open file system");
80 } else {
81 #ifdef DUMP_SUPER
82 list_super2(fs->super, stdout);
83 #endif
84 switch (f) {
85 case ext2fsReadBlockBitmap: {
86 retval = ext2fs_read_block_bitmap(fs);
87 if (retval)
88 com_err(progname, retval, "while trying to read block bitmap");
89 break;
90 }
91 case ext2fsReadInodeBitmap: {
92 retval = ext2fs_read_inode_bitmap(fs);
93 if (retval)
94 com_err(progname, retval, "while trying to read inode bitmap");
95 break;
96 }
97 default: {
98 assert(false);
99 }
100 }
101 retval = ext2fs_close(fs);
102 if (retval)
103 com_err(progname, retval, "while trying to close file system");
104 }
105 close(fd);
106
107 return 0;
108 }
109