Coverage Report

Created: 2026-12-01 07:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/liblnk/ossfuzz/file_fuzzer.cc
Line
Count
Source
1
/*
2
 * OSS-Fuzz target for liblnk file type
3
 *
4
 * Copyright (C) 2009-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
#include <stdlib.h>
25
26
/* Note that some of the OSS-Fuzz engines use C++
27
 */
28
extern "C" {
29
30
#include "ossfuzz_libbfio.h"
31
#include "ossfuzz_liblnk.h"
32
33
#if !defined( LIBLNK_HAVE_BFIO )
34
35
/* Opens a file using a Basic File IO (bfio) handle
36
 * Returns 1 if successful or -1 on error
37
 */
38
LIBLNK_EXTERN \
39
int liblnk_file_open_file_io_handle(
40
     liblnk_file_t *file,
41
     libbfio_handle_t *file_io_handle,
42
     int access_flags,
43
     liblnk_error_t **error );
44
45
/* Determines if a file contains a LNK file signature using a Basic File IO (bfio) handle
46
 * Returns 1 if true, 0 if not or -1 on error
47
 */
48
LIBLNK_EXTERN \
49
int liblnk_check_file_signature_file_io_handle(
50
     libbfio_handle_t *file_io_handle,
51
     liblnk_error_t **error );
52
53
#endif /* !defined( LIBLNK_HAVE_BFIO ) */
54
55
/* The deprecated declarations are ignored
56
 * so that the deprecated functions can be exercised
57
 */
58
#if defined( __clang__ )
59
#pragma clang diagnostic push
60
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
61
#elif defined( __GNUC__ )
62
#pragma GCC diagnostic push
63
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
64
#endif
65
66
/* Used to limit the amount of memory the fuzzer will allocate
67
 */
68
898k
#define MAXIMUM_ALLOCATION_SIZE ( 8 * 1024 * 1024 )
69
70
8.68k
#define NUMBER_OF_CODEPAGES 16
71
72
/* The codepages supported by liblnk_file_set_ascii_codepage
73
 * The value 0 represents an unsupported codepage
74
 * which is used to exercise the error path
75
 */
76
static const int codepages[ NUMBER_OF_CODEPAGES ] = {
77
  0,
78
  LIBLNK_CODEPAGE_WINDOWS_874,
79
  LIBLNK_CODEPAGE_WINDOWS_932,
80
  LIBLNK_CODEPAGE_WINDOWS_936,
81
  LIBLNK_CODEPAGE_WINDOWS_949,
82
  LIBLNK_CODEPAGE_WINDOWS_950,
83
  LIBLNK_CODEPAGE_WINDOWS_1250,
84
  LIBLNK_CODEPAGE_WINDOWS_1251,
85
  LIBLNK_CODEPAGE_WINDOWS_1252,
86
  LIBLNK_CODEPAGE_WINDOWS_1253,
87
  LIBLNK_CODEPAGE_WINDOWS_1254,
88
  LIBLNK_CODEPAGE_WINDOWS_1255,
89
  LIBLNK_CODEPAGE_WINDOWS_1256,
90
  LIBLNK_CODEPAGE_WINDOWS_1257,
91
  LIBLNK_CODEPAGE_WINDOWS_1258,
92
  LIBLNK_CODEPAGE_ASCII
93
};
94
95
/* Function pointer type definitions
96
 */
97
typedef int (*liblnk_file_utf8_string_size_getter_t)(
98
               liblnk_file_t *file,
99
               size_t *utf8_string_size,
100
               liblnk_error_t **error );
101
102
typedef int (*liblnk_file_utf8_string_getter_t)(
103
               liblnk_file_t *file,
104
               uint8_t *utf8_string,
105
               size_t utf8_string_size,
106
               liblnk_error_t **error );
107
108
typedef int (*liblnk_file_utf16_string_size_getter_t)(
109
               liblnk_file_t *file,
110
               size_t *utf16_string_size,
111
               liblnk_error_t **error );
112
113
typedef int (*liblnk_file_utf16_string_getter_t)(
114
               liblnk_file_t *file,
115
               uint16_t *utf16_string,
116
               size_t utf16_string_size,
117
               liblnk_error_t **error );
118
119
typedef int (*liblnk_data_block_utf8_string_size_getter_t)(
120
               liblnk_data_block_t *data_block,
121
               size_t *utf8_string_size,
122
               liblnk_error_t **error );
123
124
typedef int (*liblnk_data_block_utf8_string_getter_t)(
125
               liblnk_data_block_t *data_block,
126
               uint8_t *utf8_string,
127
               size_t utf8_string_size,
128
               liblnk_error_t **error );
129
130
typedef int (*liblnk_data_block_utf16_string_size_getter_t)(
131
               liblnk_data_block_t *data_block,
132
               size_t *utf16_string_size,
133
               liblnk_error_t **error );
134
135
typedef int (*liblnk_data_block_utf16_string_getter_t)(
136
               liblnk_data_block_t *data_block,
137
               uint16_t *utf16_string,
138
               size_t utf16_string_size,
139
               liblnk_error_t **error );
140
141
typedef int (*liblnk_file_guid_getter_t)(
142
               liblnk_file_t *file,
143
               uint8_t *guid_data,
144
               size_t guid_data_size,
145
               liblnk_error_t **error );
146
147
/* Retrieves the UTF-8 encoded string from a file
148
 * using a pair of getter functions
149
 */
150
void fuzzer_file_fetch_utf8_string(
151
      liblnk_file_utf8_string_size_getter_t get_utf8_string_size,
152
      liblnk_file_utf8_string_getter_t get_utf8_string,
153
      liblnk_file_t *file )
154
61.4k
{
155
61.4k
  uint8_t *utf8_string    = NULL;
156
61.4k
  size_t utf8_string_size = 0;
157
158
61.4k
  if( get_utf8_string_size(
159
61.4k
       file,
160
61.4k
       &utf8_string_size,
161
61.4k
       NULL ) != 1 )
162
57.9k
  {
163
57.9k
    return;
164
57.9k
  }
165
3.54k
  if( ( utf8_string_size == 0 )
166
3.50k
   || ( utf8_string_size > MAXIMUM_ALLOCATION_SIZE ) )
167
52
  {
168
52
    return;
169
52
  }
170
3.48k
  utf8_string = (uint8_t *) malloc( utf8_string_size );
171
172
3.48k
  if( utf8_string == NULL )
173
0
  {
174
0
    return;
175
0
  }
176
3.48k
  get_utf8_string(
177
3.48k
   file,
178
3.48k
   utf8_string,
179
3.48k
   utf8_string_size,
180
3.48k
   NULL );
181
182
3.48k
  free( utf8_string );
183
3.48k
}
184
185
/* Retrieves the UTF-16 encoded string from a file
186
 * using a pair of getter functions
187
 */
188
void fuzzer_file_fetch_utf16_string(
189
      liblnk_file_utf16_string_size_getter_t get_utf16_string_size,
190
      liblnk_file_utf16_string_getter_t get_utf16_string,
191
      liblnk_file_t *file )
192
61.4k
{
193
61.4k
  uint16_t *utf16_string   = NULL;
194
61.4k
  size_t utf16_string_size = 0;
195
196
61.4k
  if( get_utf16_string_size(
197
61.4k
       file,
198
61.4k
       &utf16_string_size,
199
61.4k
       NULL ) != 1 )
200
57.9k
  {
201
57.9k
    return;
202
57.9k
  }
203
3.54k
  if( ( utf16_string_size == 0 )
204
3.50k
   || ( utf16_string_size > ( MAXIMUM_ALLOCATION_SIZE / sizeof( uint16_t ) ) ) )
205
52
  {
206
52
    return;
207
52
  }
208
3.48k
  utf16_string = (uint16_t *) malloc( utf16_string_size * sizeof( uint16_t ) );
209
210
3.48k
  if( utf16_string == NULL )
211
0
  {
212
0
    return;
213
0
  }
214
3.48k
  get_utf16_string(
215
3.48k
   file,
216
3.48k
   utf16_string,
217
3.48k
   utf16_string_size,
218
3.48k
   NULL );
219
220
3.48k
  free( utf16_string );
221
3.48k
}
222
223
/* Retrieves the UTF-8 encoded string from a data block
224
 * using a pair of getter functions
225
 */
226
void fuzzer_data_block_fetch_utf8_string(
227
      liblnk_data_block_utf8_string_size_getter_t get_utf8_string_size,
228
      liblnk_data_block_utf8_string_getter_t get_utf8_string,
229
      liblnk_data_block_t *data_block )
230
9.30k
{
231
9.30k
  uint8_t *utf8_string    = NULL;
232
9.30k
  size_t utf8_string_size = 0;
233
234
9.30k
  if( get_utf8_string_size(
235
9.30k
       data_block,
236
9.30k
       &utf8_string_size,
237
9.30k
       NULL ) != 1 )
238
3.88k
  {
239
3.88k
    return;
240
3.88k
  }
241
5.41k
  if( ( utf8_string_size == 0 )
242
5.41k
   || ( utf8_string_size > MAXIMUM_ALLOCATION_SIZE ) )
243
0
  {
244
0
    return;
245
0
  }
246
5.41k
  utf8_string = (uint8_t *) malloc( utf8_string_size );
247
248
5.41k
  if( utf8_string == NULL )
249
0
  {
250
0
    return;
251
0
  }
252
5.41k
  get_utf8_string(
253
5.41k
   data_block,
254
5.41k
   utf8_string,
255
5.41k
   utf8_string_size,
256
5.41k
   NULL );
257
258
5.41k
  free( utf8_string );
259
5.41k
}
260
261
/* Retrieves the UTF-16 encoded string from a data block
262
 * using a pair of getter functions
263
 */
264
void fuzzer_data_block_fetch_utf16_string(
265
             liblnk_data_block_utf16_string_size_getter_t get_utf16_string_size,
266
             liblnk_data_block_utf16_string_getter_t get_utf16_string,
267
             liblnk_data_block_t *data_block )
268
9.30k
{
269
9.30k
  uint16_t *utf16_string   = NULL;
270
9.30k
  size_t utf16_string_size = 0;
271
272
9.30k
  if( get_utf16_string_size(
273
9.30k
       data_block,
274
9.30k
       &utf16_string_size,
275
9.30k
       NULL ) != 1 )
276
3.88k
  {
277
3.88k
    return;
278
3.88k
  }
279
5.41k
  if( ( utf16_string_size == 0 )
280
5.41k
   || ( utf16_string_size > ( MAXIMUM_ALLOCATION_SIZE / sizeof( uint16_t ) ) ) )
281
0
  {
282
0
    return;
283
0
  }
284
5.41k
  utf16_string = (uint16_t *) malloc( utf16_string_size * sizeof( uint16_t ) );
285
286
5.41k
  if( utf16_string == NULL )
287
0
  {
288
0
    return;
289
0
  }
290
5.41k
  get_utf16_string(
291
5.41k
   data_block,
292
5.41k
   utf16_string,
293
5.41k
   utf16_string_size,
294
5.41k
   NULL );
295
296
5.41k
  free( utf16_string );
297
5.41k
}
298
299
/* Retrieves the GUID (identifier) from a file
300
 * using a getter function
301
 */
302
void fuzzer_file_fetch_guid(
303
             liblnk_file_guid_getter_t get_guid,
304
             liblnk_file_t *file )
305
24.5k
{
306
24.5k
  uint8_t guid_data[ 16 ] = { 0 };
307
308
24.5k
  get_guid(
309
24.5k
   file,
310
24.5k
   guid_data,
311
24.5k
   sizeof( guid_data ),
312
24.5k
   NULL );
313
24.5k
}
314
315
/* Exercises the getter functions of a data block
316
 */
317
void fuzzer_data_block_exercise_getters(
318
             liblnk_data_block_t *data_block )
319
885k
{
320
885k
  uint8_t guid_data[ 16 ] = { 0 };
321
885k
  uint8_t *data           = NULL;
322
885k
  size_t data_size        = 0;
323
885k
  uint32_t signature      = 0;
324
885k
  int result              = 0;
325
326
885k
  if( liblnk_data_block_get_signature(
327
885k
       data_block,
328
885k
       &signature,
329
885k
       NULL ) != 1 )
330
0
  {
331
0
    return;
332
0
  }
333
885k
  switch( signature )
334
885k
  {
335
1.96k
    case LIBLNK_DATA_BLOCK_SIGNATURE_ENVIRONMENT_VARIABLES_LOCATION:
336
2.62k
    case LIBLNK_DATA_BLOCK_SIGNATURE_DARWIN_PROPERTIES:
337
4.14k
    case LIBLNK_DATA_BLOCK_SIGNATURE_ICON_LOCATION:
338
4.14k
      fuzzer_data_block_fetch_utf8_string(
339
4.14k
       liblnk_strings_data_block_get_utf8_string_size,
340
4.14k
       liblnk_strings_data_block_get_utf8_string,
341
4.14k
       data_block );
342
343
4.14k
      fuzzer_data_block_fetch_utf16_string(
344
4.14k
       liblnk_strings_data_block_get_utf16_string_size,
345
4.14k
       liblnk_strings_data_block_get_utf16_string,
346
4.14k
       data_block );
347
348
4.14k
      fuzzer_data_block_fetch_utf8_string(
349
4.14k
       liblnk_strings_data_block_get_utf8_path_string_size,
350
4.14k
       liblnk_strings_data_block_get_utf8_path_string,
351
4.14k
       data_block );
352
353
4.14k
      fuzzer_data_block_fetch_utf16_string(
354
4.14k
       liblnk_strings_data_block_get_utf16_path_string_size,
355
4.14k
       liblnk_strings_data_block_get_utf16_path_string,
356
4.14k
       data_block );
357
358
4.14k
      liblnk_distributed_link_tracking_data_block_get_utf8_machine_identifier_size(
359
4.14k
       data_block,
360
4.14k
       NULL,
361
4.14k
       NULL );
362
363
4.14k
      break;
364
365
1.01k
    case LIBLNK_DATA_BLOCK_SIGNATURE_DISTRIBUTED_LINK_TRACKER_PROPERTIES:
366
1.01k
      fuzzer_data_block_fetch_utf8_string(
367
1.01k
       liblnk_distributed_link_tracking_data_block_get_utf8_machine_identifier_size,
368
1.01k
       liblnk_distributed_link_tracking_data_block_get_utf8_machine_identifier,
369
1.01k
       data_block );
370
371
1.01k
      fuzzer_data_block_fetch_utf16_string(
372
1.01k
       liblnk_distributed_link_tracking_data_block_get_utf16_machine_identifier_size,
373
1.01k
       liblnk_distributed_link_tracking_data_block_get_utf16_machine_identifier,
374
1.01k
       data_block );
375
376
1.01k
      liblnk_distributed_link_tracking_data_block_get_droid_volume_identifier(
377
1.01k
       data_block,
378
1.01k
       guid_data,
379
1.01k
       sizeof( guid_data ),
380
1.01k
       NULL );
381
382
1.01k
      liblnk_distributed_link_tracking_data_block_get_droid_file_identifier(
383
1.01k
       data_block,
384
1.01k
       guid_data,
385
1.01k
       sizeof( guid_data ),
386
1.01k
       NULL );
387
388
1.01k
      liblnk_distributed_link_tracking_data_block_get_birth_droid_volume_identifier(
389
1.01k
       data_block,
390
1.01k
       guid_data,
391
1.01k
       sizeof( guid_data ),
392
1.01k
       NULL );
393
394
1.01k
      liblnk_distributed_link_tracking_data_block_get_birth_droid_file_identifier(
395
1.01k
       data_block,
396
1.01k
       guid_data,
397
1.01k
       sizeof( guid_data ),
398
1.01k
       NULL );
399
400
1.01k
      liblnk_strings_data_block_get_utf8_string_size(
401
1.01k
       data_block,
402
1.01k
       NULL,
403
1.01k
       NULL );
404
405
1.01k
      break;
406
407
880k
    default:
408
      /* Retrieves the raw data of the data block
409
       */
410
880k
      result = liblnk_data_block_get_data_size(
411
880k
                data_block,
412
880k
                &data_size,
413
880k
                NULL );
414
415
880k
      if( ( result == 1 )
416
880k
       && ( data_size <= MAXIMUM_ALLOCATION_SIZE ) )
417
880k
      {
418
880k
        data = (uint8_t *) malloc(
419
880k
                            data_size < 1 ? 1 : data_size );
420
421
880k
        if( data != NULL )
422
880k
        {
423
880k
          liblnk_data_block_copy_data(
424
880k
           data_block,
425
880k
           data,
426
880k
           data_size,
427
880k
           NULL );
428
880k
          free( data );
429
880k
        }
430
880k
      }
431
880k
      liblnk_strings_data_block_get_utf8_string_size(
432
880k
       data_block,
433
880k
       NULL,
434
880k
       NULL );
435
436
880k
      liblnk_distributed_link_tracking_data_block_get_utf8_machine_identifier_size(
437
880k
       data_block,
438
880k
       NULL,
439
880k
       NULL );
440
441
880k
      break;
442
885k
  }
443
885k
}
444
445
/* Exercises the getter functions of a file
446
 */
447
void fuzzer_file_exercise_getters(
448
             liblnk_file_t *file )
449
6.14k
{
450
6.14k
  liblnk_data_block_t *data_block = NULL;
451
6.14k
  uint8_t *data                   = NULL;
452
6.14k
  uint16_t hot_key_value          = 0;
453
6.14k
  uint32_t value32                = 0;
454
6.14k
  uint64_t value64                = 0;
455
6.14k
  size_t data_size                = 0;
456
6.14k
  int ascii_codepage              = 0;
457
6.14k
  int number_of_data_blocks       = 0;
458
6.14k
  int data_block_index            = 0;
459
6.14k
  int result                      = 0;
460
461
6.14k
  liblnk_file_get_ascii_codepage(
462
6.14k
   file,
463
6.14k
   &ascii_codepage,
464
6.14k
   NULL );
465
466
6.14k
  liblnk_file_get_data_flags(
467
6.14k
   file,
468
6.14k
   &( value32 ),
469
6.14k
   NULL );
470
471
6.14k
  liblnk_file_get_data_size(
472
6.14k
   file,
473
6.14k
   &( value64 ),
474
6.14k
   NULL );
475
476
6.14k
  liblnk_file_is_corrupted(
477
6.14k
   file,
478
6.14k
   NULL );
479
480
6.14k
  liblnk_file_link_refers_to_file(
481
6.14k
   file,
482
6.14k
   NULL );
483
484
6.14k
  liblnk_file_get_file_creation_time(
485
6.14k
   file,
486
6.14k
   &( value64 ),
487
6.14k
   NULL );
488
489
6.14k
  liblnk_file_get_file_modification_time(
490
6.14k
   file,
491
6.14k
   &( value64 ),
492
6.14k
   NULL );
493
494
6.14k
  liblnk_file_get_file_access_time(
495
6.14k
   file,
496
6.14k
   &( value64 ),
497
6.14k
   NULL );
498
499
6.14k
  liblnk_file_get_file_size(
500
6.14k
   file,
501
6.14k
   &( value32 ),
502
6.14k
   NULL );
503
504
6.14k
  liblnk_file_get_icon_index(
505
6.14k
   file,
506
6.14k
   &( value32 ),
507
6.14k
   NULL );
508
509
6.14k
  liblnk_file_get_show_window_value(
510
6.14k
   file,
511
6.14k
   &( value32 ),
512
6.14k
   NULL );
513
514
6.14k
  liblnk_file_get_hot_key_value(
515
6.14k
   file,
516
6.14k
   &( hot_key_value ),
517
6.14k
   NULL );
518
519
6.14k
  liblnk_file_get_file_attribute_flags(
520
6.14k
   file,
521
6.14k
   &( value32 ),
522
6.14k
   NULL );
523
524
6.14k
  liblnk_file_get_drive_type(
525
6.14k
   file,
526
6.14k
   &( value32 ),
527
6.14k
   NULL );
528
529
6.14k
  liblnk_file_get_drive_serial_number(
530
6.14k
   file,
531
6.14k
   &( value32 ),
532
6.14k
   NULL );
533
534
6.14k
  fuzzer_file_fetch_utf8_string(
535
6.14k
   liblnk_file_get_utf8_volume_label_size,
536
6.14k
   liblnk_file_get_utf8_volume_label,
537
6.14k
   file );
538
539
6.14k
  fuzzer_file_fetch_utf16_string(
540
6.14k
   liblnk_file_get_utf16_volume_label_size,
541
6.14k
   liblnk_file_get_utf16_volume_label,
542
6.14k
   file );
543
544
6.14k
  fuzzer_file_fetch_utf8_string(
545
6.14k
   liblnk_file_get_utf8_local_path_size,
546
6.14k
   liblnk_file_get_utf8_local_path,
547
6.14k
   file );
548
549
6.14k
  fuzzer_file_fetch_utf16_string(
550
6.14k
   liblnk_file_get_utf16_local_path_size,
551
6.14k
   liblnk_file_get_utf16_local_path,
552
6.14k
   file );
553
554
6.14k
  fuzzer_file_fetch_utf8_string(
555
6.14k
   liblnk_file_get_utf8_network_path_size,
556
6.14k
   liblnk_file_get_utf8_network_path,
557
6.14k
   file );
558
559
6.14k
  fuzzer_file_fetch_utf16_string(
560
6.14k
   liblnk_file_get_utf16_network_path_size,
561
6.14k
   liblnk_file_get_utf16_network_path,
562
6.14k
   file );
563
564
6.14k
  fuzzer_file_fetch_utf8_string(
565
6.14k
   liblnk_file_get_utf8_description_size,
566
6.14k
   liblnk_file_get_utf8_description,
567
6.14k
   file );
568
569
6.14k
  fuzzer_file_fetch_utf16_string(
570
6.14k
   liblnk_file_get_utf16_description_size,
571
6.14k
   liblnk_file_get_utf16_description,
572
6.14k
   file );
573
574
6.14k
  fuzzer_file_fetch_utf8_string(
575
6.14k
   liblnk_file_get_utf8_relative_path_size,
576
6.14k
   liblnk_file_get_utf8_relative_path,
577
6.14k
   file );
578
579
6.14k
  fuzzer_file_fetch_utf16_string(
580
6.14k
   liblnk_file_get_utf16_relative_path_size,
581
6.14k
   liblnk_file_get_utf16_relative_path,
582
6.14k
   file );
583
584
6.14k
  fuzzer_file_fetch_utf8_string(
585
6.14k
   liblnk_file_get_utf8_working_directory_size,
586
6.14k
   liblnk_file_get_utf8_working_directory,
587
6.14k
   file );
588
589
6.14k
  fuzzer_file_fetch_utf16_string(
590
6.14k
   liblnk_file_get_utf16_working_directory_size,
591
6.14k
   liblnk_file_get_utf16_working_directory,
592
6.14k
   file );
593
594
6.14k
  fuzzer_file_fetch_utf8_string(
595
6.14k
   liblnk_file_get_utf8_command_line_arguments_size,
596
6.14k
   liblnk_file_get_utf8_command_line_arguments,
597
6.14k
   file );
598
599
6.14k
  fuzzer_file_fetch_utf16_string(
600
6.14k
   liblnk_file_get_utf16_command_line_arguments_size,
601
6.14k
   liblnk_file_get_utf16_command_line_arguments,
602
6.14k
   file );
603
604
6.14k
  fuzzer_file_fetch_utf8_string(
605
6.14k
   liblnk_file_get_utf8_icon_location_size,
606
6.14k
   liblnk_file_get_utf8_icon_location,
607
6.14k
   file );
608
609
6.14k
  fuzzer_file_fetch_utf16_string(
610
6.14k
   liblnk_file_get_utf16_icon_location_size,
611
6.14k
   liblnk_file_get_utf16_icon_location,
612
6.14k
   file );
613
614
6.14k
  fuzzer_file_fetch_utf8_string(
615
6.14k
   liblnk_file_get_utf8_environment_variables_location_size,
616
6.14k
   liblnk_file_get_utf8_environment_variables_location,
617
6.14k
   file );
618
619
6.14k
  fuzzer_file_fetch_utf16_string(
620
6.14k
   liblnk_file_get_utf16_environment_variables_location_size,
621
6.14k
   liblnk_file_get_utf16_environment_variables_location,
622
6.14k
   file );
623
624
6.14k
  fuzzer_file_fetch_utf8_string(
625
6.14k
   liblnk_file_get_utf8_machine_identifier_size,
626
6.14k
   liblnk_file_get_utf8_machine_identifier,
627
6.14k
   file );
628
629
6.14k
  fuzzer_file_fetch_utf16_string(
630
6.14k
   liblnk_file_get_utf16_machine_identifier_size,
631
6.14k
   liblnk_file_get_utf16_machine_identifier,
632
6.14k
   file );
633
634
6.14k
  fuzzer_file_fetch_guid(
635
6.14k
   liblnk_file_get_droid_volume_identifier,
636
6.14k
   file );
637
638
6.14k
  fuzzer_file_fetch_guid(
639
6.14k
   liblnk_file_get_droid_file_identifier,
640
6.14k
   file );
641
642
6.14k
  fuzzer_file_fetch_guid(
643
6.14k
   liblnk_file_get_birth_droid_volume_identifier,
644
6.14k
   file );
645
646
6.14k
  fuzzer_file_fetch_guid(
647
6.14k
   liblnk_file_get_birth_droid_file_identifier,
648
6.14k
   file );
649
650
6.14k
  liblnk_file_has_distributed_link_tracking_data(
651
6.14k
   file,
652
6.14k
   NULL );
653
654
  /* Retrieves the link target identifier data
655
   */
656
6.14k
  data_size = 0;
657
658
6.14k
  result = liblnk_file_get_link_target_identifier_data_size(
659
6.14k
            file,
660
6.14k
            &data_size,
661
6.14k
            NULL );
662
663
6.14k
  if( ( result == 1 )
664
304
   && ( data_size <= MAXIMUM_ALLOCATION_SIZE ) )
665
304
  {
666
304
    data = (uint8_t *) malloc(
667
304
                        data_size < 1 ? 1 : data_size );
668
669
304
    if( data != NULL )
670
304
    {
671
304
      liblnk_file_copy_link_target_identifier_data(
672
304
       file,
673
304
       data,
674
304
       data_size,
675
304
       NULL );
676
304
      free( data );
677
304
    }
678
304
  }
679
6.14k
  result = liblnk_file_get_number_of_data_blocks(
680
6.14k
            file,
681
6.14k
            &( number_of_data_blocks ),
682
6.14k
            NULL );
683
684
6.14k
  if( ( result == 1 )
685
6.14k
   && ( number_of_data_blocks > 0 ) )
686
944
  {
687
886k
    while( data_block_index < number_of_data_blocks )
688
885k
    {
689
885k
      result = liblnk_file_get_data_block_by_index(
690
885k
          file,
691
885k
          data_block_index,
692
885k
          &data_block,
693
885k
          NULL );
694
695
885k
      if( ( result == 1 )
696
885k
       && ( data_block != NULL ) )
697
885k
      {
698
885k
        fuzzer_data_block_exercise_getters(
699
885k
         data_block );
700
885k
        liblnk_data_block_free(
701
885k
         &data_block,
702
885k
         NULL );
703
885k
      }
704
885k
      data_block_index += 1;
705
885k
    }
706
944
  }
707
6.14k
}
708
709
/* Exercises the invalid output getter functions of a file
710
 */
711
void fuzzer_file_exercise_invalid_output(
712
             liblnk_file_t *file )
713
6.14k
{
714
6.14k
  liblnk_file_utf8_string_size_getter_t get_utf8_string_size = NULL;
715
716
6.14k
  liblnk_file_get_ascii_codepage( file, NULL, NULL );
717
6.14k
  liblnk_file_get_data_flags( file, NULL, NULL );
718
6.14k
  liblnk_file_get_data_size( file, NULL, NULL );
719
6.14k
  liblnk_file_get_file_creation_time( file, NULL, NULL );
720
6.14k
  liblnk_file_get_file_modification_time( file, NULL, NULL );
721
6.14k
  liblnk_file_get_file_access_time( file, NULL, NULL );
722
6.14k
  liblnk_file_get_file_size( file, NULL, NULL );
723
6.14k
  liblnk_file_get_icon_index( file, NULL, NULL );
724
6.14k
  liblnk_file_get_show_window_value( file, NULL, NULL );
725
6.14k
  liblnk_file_get_hot_key_value( file, NULL, NULL );
726
6.14k
  liblnk_file_get_file_attribute_flags( file, NULL, NULL );
727
6.14k
  liblnk_file_get_drive_type( file, NULL, NULL );
728
6.14k
  liblnk_file_get_drive_serial_number( file, NULL, NULL );
729
6.14k
  liblnk_file_get_utf8_volume_label_size( file, NULL, NULL );
730
6.14k
  liblnk_file_get_utf16_local_path_size( file, NULL, NULL );
731
6.14k
  liblnk_file_get_utf8_description_size( file, NULL, NULL );
732
733
6.14k
  get_utf8_string_size = liblnk_file_get_utf8_environment_variables_location_size;
734
6.14k
  get_utf8_string_size(
735
6.14k
   file,
736
6.14k
   NULL,
737
6.14k
   NULL );
738
739
6.14k
  get_utf8_string_size = liblnk_file_get_utf8_machine_identifier_size;
740
741
6.14k
  get_utf8_string_size(
742
6.14k
   file,
743
6.14k
   NULL,
744
6.14k
   NULL );
745
746
6.14k
  liblnk_file_get_link_target_identifier_data_size( file, NULL, NULL );
747
748
6.14k
  liblnk_file_get_number_of_data_blocks( file, NULL, NULL );
749
750
6.14k
  liblnk_file_get_data_block_by_index( file, 0, NULL, NULL );
751
6.14k
}
752
753
int LLVMFuzzerTestOneInput(
754
     const uint8_t *data,
755
     size_t size )
756
4.35k
{
757
4.35k
  libbfio_handle_t *file_io_handle = NULL;
758
4.35k
  liblnk_file_t *file              = NULL;
759
4.35k
  int codepage                     = 0;
760
4.35k
  int global_codepage              = 0;
761
4.35k
  int result                       = 0;
762
763
  /* Exercises the library support functions
764
   */
765
4.35k
  liblnk_get_version();
766
4.35k
  liblnk_get_access_flags_read();
767
768
4.35k
  liblnk_get_codepage(
769
4.35k
   &global_codepage,
770
4.35k
   NULL );
771
4.35k
  liblnk_set_codepage(
772
4.35k
   1252,
773
4.35k
   NULL );
774
775
  /* Exercises the unsupported codepage error branch
776
   */
777
4.35k
  liblnk_set_codepage(
778
4.35k
   1234,
779
4.35k
   NULL );
780
781
4.35k
  if( libbfio_memory_range_initialize(
782
4.35k
       &file_io_handle,
783
4.35k
       NULL ) != 1 )
784
0
  {
785
0
    return( 0 );
786
0
  }
787
4.35k
  if( libbfio_memory_range_set(
788
4.35k
       file_io_handle,
789
4.35k
       (uint8_t *) data,
790
4.35k
       size,
791
4.35k
       NULL ) != 1 )
792
0
  {
793
0
    goto on_error_libbfio;
794
0
  }
795
4.35k
  if( liblnk_file_initialize(
796
4.35k
       &file,
797
4.35k
       NULL ) != 1 )
798
0
  {
799
0
    goto on_error_libbfio;
800
0
  }
801
4.35k
  liblnk_file_close(
802
4.35k
   file,
803
4.35k
   NULL );
804
805
4.35k
  liblnk_file_signal_abort(
806
4.35k
   file,
807
4.35k
   NULL );
808
809
4.35k
  liblnk_file_open_file_io_handle(
810
4.35k
   file,
811
4.35k
   NULL,
812
4.35k
   LIBLNK_OPEN_READ,
813
4.35k
   NULL );
814
815
4.35k
  liblnk_file_open_file_io_handle(
816
4.35k
   file,
817
4.35k
   file_io_handle,
818
4.35k
   0,
819
4.35k
   NULL );
820
821
4.35k
  liblnk_file_open_file_io_handle(
822
4.35k
   file,
823
4.35k
   file_io_handle,
824
4.35k
   LIBLNK_OPEN_READ_WRITE,
825
4.35k
   NULL );
826
827
4.35k
  liblnk_check_file_signature_file_io_handle(
828
4.35k
   file_io_handle,
829
4.35k
   NULL );
830
831
4.35k
  fuzzer_file_exercise_getters(
832
4.35k
   file );
833
834
4.35k
  fuzzer_file_exercise_invalid_output(
835
4.35k
   file );
836
837
4.35k
  if( size > 0 )
838
4.35k
  {
839
4.35k
    codepage = codepages[ (int) ( data[ 0 ] % NUMBER_OF_CODEPAGES ) ];
840
4.35k
  }
841
0
  else
842
0
  {
843
0
    codepage = codepages[ 1 ];
844
0
  }
845
4.35k
  if( liblnk_file_set_ascii_codepage(
846
4.35k
       file,
847
4.35k
       codepage,
848
4.35k
       NULL ) != 1 )
849
21
  {
850
21
    codepage = LIBLNK_CODEPAGE_WINDOWS_1252;
851
21
  }
852
4.35k
  result = liblnk_file_open_file_io_handle(
853
4.35k
            file,
854
4.35k
            file_io_handle,
855
4.35k
            LIBLNK_OPEN_READ,
856
4.35k
            NULL );
857
858
4.35k
  if( result == 1 )
859
2.03k
  {
860
2.03k
    liblnk_file_open_file_io_handle(
861
2.03k
     file,
862
2.03k
     file_io_handle,
863
2.03k
     LIBLNK_OPEN_READ,
864
2.03k
     NULL );
865
866
2.03k
    fuzzer_file_exercise_getters(
867
2.03k
     file );
868
869
2.03k
    fuzzer_file_exercise_invalid_output(
870
2.03k
     file );
871
872
2.03k
    liblnk_file_close(
873
2.03k
     file,
874
2.03k
     NULL );
875
2.03k
  }
876
4.35k
  if( size > 1 )
877
4.32k
  {
878
4.32k
    codepage = codepages[ (int) ( data[ 1 ] % NUMBER_OF_CODEPAGES ) ];
879
880
4.32k
    if( liblnk_file_set_ascii_codepage(
881
4.32k
         file,
882
4.32k
         codepage,
883
4.32k
         NULL ) != 1 )
884
4.20k
    {
885
4.20k
      codepage = LIBLNK_CODEPAGE_WINDOWS_1252;
886
4.20k
    }
887
4.32k
  }
888
4.35k
  result = liblnk_file_open_file_io_handle(
889
4.35k
            file,
890
4.35k
            file_io_handle,
891
4.35k
            LIBLNK_OPEN_READ,
892
4.35k
            NULL );
893
894
4.35k
  if( result == 1 )
895
2.03k
  {
896
2.03k
    fuzzer_file_exercise_getters(
897
2.03k
     file );
898
2.03k
    fuzzer_file_exercise_invalid_output(
899
2.03k
     file );
900
2.03k
  }
901
4.35k
  liblnk_file_free(
902
4.35k
   &file,
903
4.35k
   NULL );
904
905
4.35k
on_error_libbfio:
906
4.35k
  libbfio_handle_free(
907
4.35k
   &file_io_handle,
908
4.35k
   NULL );
909
910
4.35k
  return( 0 );
911
4.35k
}
912
913
#if defined( __clang__ )
914
#pragma clang diagnostic pop
915
#elif defined( __GNUC__ )
916
#pragma GCC diagnostic pop
917
#endif
918
919
} /* extern "C" */