Coverage Report

Created: 2026-12-01 07:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libpff/ossfuzz/message_fuzzer.cc
Line
Count
Source
1
/*
2
 * OSS-Fuzz target for libpff message type
3
 *
4
 * Copyright (C) 2008-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
#include <stdlib.h>
26
27
/* Note that some of the OSS-Fuzz engines use C++
28
 */
29
extern "C" {
30
31
#include "ossfuzz_libbfio.h"
32
#include "ossfuzz_libpff.h"
33
34
#if !defined( LIBPFF_HAVE_BFIO )
35
36
/* Opens a file using a Basic File IO (bfio) handle
37
 * Returns 1 if successful or -1 on error
38
 */
39
LIBPFF_EXTERN \
40
int libpff_file_open_file_io_handle(
41
      libpff_file_t *file,
42
      libbfio_handle_t *file_io_handle,
43
      int access_flags,
44
      libpff_error_t **error );
45
46
#endif /* !defined( LIBPFF_HAVE_BFIO ) */
47
48
/* Limits the number of sub items to walk
49
 * to bound the time a single input takes
50
 */
51
17.8k
#define FUZZER_SUB_ITEM_LIMIT 8
52
53
/* Limits the depth of the sub item search
54
 * to bound the time a single input takes
55
 */
56
2.12k
#define FUZZER_SEARCH_DEPTH 2
57
58
/* Limits the size of the message body buffer
59
 */
60
145
#define FUZZER_MESSAGE_BODY_SIZE_LIMIT  ( (size_t) 0x4000 )
61
62
/* Determines if the specified item type is a message type
63
 * Returns 1 if a message type or 0 if not a message type
64
 */
65
static int message_fuzzer_is_message_type(
66
               uint8_t item_type )
67
15.7k
{
68
15.7k
  switch( item_type )
69
15.7k
  {
70
7.99k
    case LIBPFF_ITEM_TYPE_UNDEFINED:
71
7.99k
    case LIBPFF_ITEM_TYPE_ATTACHMENT:
72
7.99k
    case LIBPFF_ITEM_TYPE_ATTACHMENTS:
73
7.99k
    case LIBPFF_ITEM_TYPE_COMMON:
74
7.99k
    case LIBPFF_ITEM_TYPE_CONFIGURATION:
75
15.7k
    case LIBPFF_ITEM_TYPE_FOLDER:
76
15.7k
    case LIBPFF_ITEM_TYPE_RECIPIENTS:
77
15.7k
    case LIBPFF_ITEM_TYPE_SUB_ASSOCIATED_CONTENTS:
78
15.7k
    case LIBPFF_ITEM_TYPE_SUB_FOLDERS:
79
15.7k
    case LIBPFF_ITEM_TYPE_SUB_MESSAGES:
80
15.7k
    case LIBPFF_ITEM_TYPE_UNKNOWN:
81
15.7k
      return( 0 );
82
83
29
    default:
84
29
      return( 1 );
85
15.7k
  }
86
15.7k
}
87
88
/* Searches for a message item within the parent item and its sub items
89
 * Returns 1 if successful, 0 if no message item was found or -1 on error
90
 */
91
static int message_fuzzer_find_message(
92
               libpff_item_t *parent_item,
93
               int search_depth,
94
               libpff_item_t **message )
95
19.1k
{
96
19.1k
  libpff_item_t *sub_item             = NULL;
97
19.1k
  uint8_t item_type                  = 0;
98
19.1k
  int number_of_sub_items           = 0;
99
19.1k
  int number_of_sub_messages        = 0;
100
19.1k
  int sub_item_index                = 0;
101
102
19.1k
  if( parent_item == NULL
103
19.1k
   || search_depth < 0
104
19.1k
   || message == NULL )
105
0
  {
106
0
    return( -1 );
107
0
  }
108
  /* A folder may contain sub messages directly
109
   */
110
19.1k
  number_of_sub_messages = 0;
111
112
19.1k
  if( libpff_folder_get_number_of_sub_messages(
113
19.1k
       parent_item,
114
19.1k
       &number_of_sub_messages,
115
19.1k
       NULL ) == 1
116
3.67k
   && number_of_sub_messages > 0 )
117
751
  {
118
751
    if( libpff_folder_get_sub_message(
119
751
         parent_item,
120
751
         0,
121
751
         message,
122
751
         NULL ) == 1
123
629
     && ( *message != NULL ) )
124
629
    {
125
629
      return( 1 );
126
629
    }
127
751
  }
128
18.5k
  if( search_depth == 0 )
129
11.0k
  {
130
11.0k
    return( 0 );
131
11.0k
  }
132
7.48k
  number_of_sub_items = 0;
133
134
7.48k
  if( libpff_item_get_number_of_sub_items(
135
7.48k
       parent_item,
136
7.48k
       &number_of_sub_items,
137
7.48k
       NULL ) != 1
138
7.48k
   || number_of_sub_items <= 0 )
139
3.68k
  {
140
3.68k
    return( 0 );
141
3.68k
  }
142
3.80k
  for( sub_item_index = 0;
143
19.5k
       sub_item_index < number_of_sub_items;
144
15.7k
       sub_item_index++ )
145
17.8k
  {
146
17.8k
    if( sub_item_index >= FUZZER_SUB_ITEM_LIMIT )
147
809
    {
148
809
      break;
149
809
    }
150
17.0k
    sub_item = NULL;
151
152
17.0k
    if( libpff_item_get_sub_item(
153
17.0k
         parent_item,
154
17.0k
         sub_item_index,
155
17.0k
         &sub_item,
156
17.0k
         NULL ) != 1 )
157
0
    {
158
0
      continue;
159
0
    }
160
17.0k
    if( message_fuzzer_find_message(
161
17.0k
         sub_item,
162
17.0k
         search_depth - 1,
163
17.0k
         message ) == 1 )
164
1.28k
    {
165
1.28k
      libpff_item_free(
166
1.28k
       &sub_item,
167
1.28k
       NULL );
168
1.28k
      return( 1 );
169
1.28k
    }
170
15.7k
    item_type = 0;
171
172
15.7k
    libpff_item_get_type(
173
15.7k
     sub_item,
174
15.7k
     &item_type,
175
15.7k
     NULL );
176
    /* The sub item may itself be a message,
177
     * e.g. a message stored directly in a folder
178
     */
179
15.7k
    if( message_fuzzer_is_message_type(
180
15.7k
         item_type ) )
181
29
    {
182
29
      *message = sub_item;
183
29
      return( 1 );
184
29
    }
185
15.7k
    libpff_item_free(
186
15.7k
     &sub_item,
187
15.7k
     NULL );
188
15.7k
  }
189
2.49k
  return( 0 );
190
3.80k
}
191
192
int LLVMFuzzerTestOneInput(
193
      const uint8_t *data,
194
      size_t size )
195
3.26k
{
196
3.26k
  uint8_t *message_body        = NULL;
197
198
3.26k
  libbfio_handle_t *file_io_handle = NULL;
199
3.26k
  libpff_file_t *file              = NULL;
200
3.26k
  libpff_item_t *message           = NULL;
201
3.26k
  libpff_item_t *recipients        = NULL;
202
3.26k
  libpff_item_t *root_folder       = NULL;
203
3.26k
  size_t message_body_size         = 0;
204
3.26k
  int number_of_attachments        = 0;
205
206
3.26k
  if( libbfio_memory_range_initialize(
207
3.26k
       &file_io_handle,
208
3.26k
       NULL ) != 1 )
209
0
  {
210
0
    return( 0 );
211
0
  }
212
3.26k
  if( libbfio_memory_range_set(
213
3.26k
       file_io_handle,
214
3.26k
       (uint8_t *) data,
215
3.26k
       size,
216
3.26k
       NULL ) != 1 )
217
0
  {
218
0
    goto on_error_libbfio;
219
0
  }
220
3.26k
  if( libpff_file_initialize(
221
3.26k
       &file,
222
3.26k
       NULL ) != 1 )
223
0
  {
224
0
    goto on_error_libbfio;
225
0
  }
226
3.26k
  if( libpff_file_open_file_io_handle(
227
3.26k
       file,
228
3.26k
       file_io_handle,
229
3.26k
       LIBPFF_OPEN_READ,
230
3.26k
       NULL ) != 1 )
231
964
  {
232
964
    goto on_error_libpff_file;
233
964
  }
234
2.30k
  if( libpff_file_get_root_folder(
235
2.30k
       file,
236
2.30k
       &root_folder,
237
2.30k
       NULL ) == 1 )
238
2.12k
  {
239
2.12k
    message = NULL;
240
241
2.12k
    if( message_fuzzer_find_message(
242
2.12k
         root_folder,
243
2.12k
         FUZZER_SEARCH_DEPTH,
244
2.12k
         &message ) == 1
245
658
     && message != NULL )
246
658
    {
247
658
      if( libpff_message_get_number_of_attachments(
248
658
           message,
249
658
           &number_of_attachments,
250
658
           NULL ) != 1 )
251
349
      {
252
349
        goto on_error_libpff_message;
253
349
      }
254
309
      recipients = NULL;
255
256
309
      if( libpff_message_get_recipients(
257
309
           message,
258
309
           &recipients,
259
309
           NULL ) == 1
260
104
       && recipients != NULL )
261
104
      {
262
104
        libpff_item_free(
263
104
         &recipients,
264
104
         NULL );
265
104
      }
266
309
      message_body_size = 0;
267
268
309
      if( libpff_message_get_plain_text_body_size(
269
309
           message,
270
309
           &message_body_size,
271
309
           NULL ) == 1
272
233
       && message_body_size > 0 )
273
133
      {
274
133
        if( message_body_size > FUZZER_MESSAGE_BODY_SIZE_LIMIT )
275
12
        {
276
12
          message_body_size = FUZZER_MESSAGE_BODY_SIZE_LIMIT;
277
12
        }
278
133
        message_body = (uint8_t *) malloc( message_body_size );
279
280
133
        if( message_body != NULL
281
133
         && libpff_message_get_plain_text_body(
282
133
                  message,
283
133
                  message_body,
284
133
                  message_body_size,
285
133
                  NULL ) == 1 )
286
121
        {
287
          /* Use the body as a sink to keep it live
288
           */
289
121
          message_body[ message_body_size - 1 ] = 0;
290
121
        }
291
133
        free( message_body );
292
133
        message_body = NULL;
293
133
      }
294
658
 on_error_libpff_message:
295
658
      libpff_item_free(
296
658
       &message,
297
658
       NULL );
298
658
    }
299
2.12k
 on_error_libpff_root_folder:
300
2.12k
    libpff_item_free(
301
2.12k
     &root_folder,
302
2.12k
     NULL );
303
2.12k
  }
304
2.30k
  libpff_file_close(
305
2.30k
   file,
306
2.30k
   NULL );
307
308
3.26k
on_error_libpff_file:
309
3.26k
  libpff_file_free(
310
3.26k
   &file,
311
3.26k
   NULL );
312
313
3.26k
on_error_libbfio:
314
3.26k
  libbfio_handle_free(
315
3.26k
   &file_io_handle,
316
3.26k
   NULL );
317
318
3.26k
  return( 0 );
319
3.26k
}
320
321
} /* extern "C" */