/src/libfsapfs/ossfuzz/container_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * OSS-Fuzz target for libfsapfs container type |
3 | | * |
4 | | * Copyright (C) 2018-2026, Joachim Metz <joachim.metz@gmail.com> |
5 | | * |
6 | | * Refer to AUTHORS for acknowledgements. |
7 | | * |
8 | | * This program is free software: you can redistribute it and/or modify |
9 | | * it under the terms of the GNU Lesser General Public License as published by |
10 | | * the Free Software Foundation, either version 3 of the License, or |
11 | | * (at your option) any later version. |
12 | | * |
13 | | * This program is distributed in the hope that it will be useful, |
14 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | | * GNU General Public License for more details. |
17 | | * |
18 | | * You should have received a copy of the GNU Lesser General Public License |
19 | | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
20 | | */ |
21 | | |
22 | | #include <stddef.h> |
23 | | #include <stdint.h> |
24 | | |
25 | | /* Note that some of the OSS-Fuzz engines use C++ |
26 | | */ |
27 | | extern "C" { |
28 | | |
29 | | #include "ossfuzz_libbfio.h" |
30 | | #include "ossfuzz_libfsapfs.h" |
31 | | |
32 | | #if !defined( LIBFSAPFS_HAVE_BFIO ) |
33 | | |
34 | | /* Determines if a file contains an APFS container signature using a Basic File IO (bfio) handle |
35 | | * Returns 1 if true, 0 if not or -1 on error |
36 | | */ |
37 | | LIBFSAPFS_EXTERN \ |
38 | | int libfsapfs_check_container_signature_file_io_handle( |
39 | | libbfio_handle_t *file_io_handle, |
40 | | libfsapfs_error_t **error ); |
41 | | |
42 | | /* Determines if a file contains an APFS volume signature using a Basic File IO (bfio) handle |
43 | | * Returns 1 if true, 0 if not or -1 on error |
44 | | */ |
45 | | LIBFSAPFS_EXTERN \ |
46 | | int libfsapfs_check_volume_signature_file_io_handle( |
47 | | libbfio_handle_t *file_io_handle, |
48 | | libfsapfs_error_t **error ); |
49 | | |
50 | | /* Opens a container using a Basic File IO (bfio) handle |
51 | | * Returns 1 if successful or -1 on error |
52 | | */ |
53 | | LIBFSAPFS_EXTERN \ |
54 | | int libfsapfs_container_open_file_io_handle( |
55 | | libfsapfs_container_t *container, |
56 | | libbfio_handle_t *file_io_handle, |
57 | | int access_flags, |
58 | | libfsapfs_error_t **error ); |
59 | | |
60 | | #endif /* !defined( LIBFSAPFS_HAVE_BFIO ) */ |
61 | | |
62 | | int LLVMFuzzerTestOneInput( |
63 | | const uint8_t *data, |
64 | | size_t size ) |
65 | 1.44k | { |
66 | 1.44k | uint8_t container_identifier[ 16 ]; |
67 | | |
68 | 1.44k | libbfio_handle_t *file_io_handle = NULL; |
69 | 1.44k | libfsapfs_container_t *container = NULL; |
70 | 1.44k | size64_t container_size = 0; |
71 | 1.44k | int is_locked = 0; |
72 | 1.44k | int number_of_volumes = 0; |
73 | | |
74 | 1.44k | if( libbfio_memory_range_initialize( |
75 | 1.44k | &file_io_handle, |
76 | 1.44k | NULL ) != 1 ) |
77 | 0 | { |
78 | 0 | return( 0 ); |
79 | 0 | } |
80 | 1.44k | if( libbfio_memory_range_set( |
81 | 1.44k | file_io_handle, |
82 | 1.44k | (uint8_t *) data, |
83 | 1.44k | size, |
84 | 1.44k | NULL ) != 1 ) |
85 | 0 | { |
86 | 0 | goto on_error_libbfio; |
87 | 0 | } |
88 | 1.44k | if( libfsapfs_check_container_signature_file_io_handle( |
89 | 1.44k | file_io_handle, |
90 | 1.44k | NULL ) == -1 ) |
91 | 13 | { |
92 | 13 | goto on_error_libbfio; |
93 | 13 | } |
94 | 1.43k | if( libfsapfs_check_volume_signature_file_io_handle( |
95 | 1.43k | file_io_handle, |
96 | 1.43k | NULL ) == -1 ) |
97 | 0 | { |
98 | 0 | goto on_error_libbfio; |
99 | 0 | } |
100 | 1.43k | if( libfsapfs_container_initialize( |
101 | 1.43k | &container, |
102 | 1.43k | NULL ) != 1 ) |
103 | 0 | { |
104 | 0 | goto on_error_libbfio; |
105 | 0 | } |
106 | 1.43k | if( libfsapfs_container_open_file_io_handle( |
107 | 1.43k | container, |
108 | 1.43k | file_io_handle, |
109 | 1.43k | LIBFSAPFS_OPEN_READ, |
110 | 1.43k | NULL ) != 1 ) |
111 | 1.38k | { |
112 | 1.38k | goto on_error_libfsapfs; |
113 | 1.38k | } |
114 | 50 | if( libfsapfs_container_get_size( |
115 | 50 | container, |
116 | 50 | &container_size, |
117 | 50 | NULL ) != 1 ) |
118 | 0 | { |
119 | 0 | goto on_error_libfsapfs; |
120 | 0 | } |
121 | 50 | if( libfsapfs_container_get_identifier( |
122 | 50 | container, |
123 | 50 | container_identifier, |
124 | 50 | 16, |
125 | 50 | NULL ) != 1 ) |
126 | 0 | { |
127 | 0 | goto on_error_libfsapfs; |
128 | 0 | } |
129 | 50 | is_locked = libfsapfs_container_is_locked( |
130 | 50 | container, |
131 | 50 | NULL ); |
132 | | |
133 | 50 | if( is_locked == -1 ) |
134 | 0 | { |
135 | 0 | goto on_error_libfsapfs; |
136 | 0 | } |
137 | 50 | if( libfsapfs_container_get_number_of_volumes( |
138 | 50 | container, |
139 | 50 | &number_of_volumes, |
140 | 50 | NULL ) != 1 ) |
141 | 0 | { |
142 | 0 | goto on_error_libfsapfs; |
143 | 0 | } |
144 | 50 | libfsapfs_container_close( |
145 | 50 | container, |
146 | 50 | NULL ); |
147 | | |
148 | 1.43k | on_error_libfsapfs: |
149 | 1.43k | libfsapfs_container_free( |
150 | 1.43k | &container, |
151 | 1.43k | NULL ); |
152 | | |
153 | 1.44k | on_error_libbfio: |
154 | 1.44k | libbfio_handle_free( |
155 | 1.44k | &file_io_handle, |
156 | 1.44k | NULL ); |
157 | | |
158 | 1.44k | return( 0 ); |
159 | 1.43k | } |
160 | | |
161 | | } /* extern "C" */ |