Coverage Report

Created: 2025-07-04 07:01

/src/libfsext/libfsext/libfsext_attributes_block.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Extended attributes block functions
3
 *
4
 * Copyright (C) 2010-2024, 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 <common.h>
23
#include <byte_stream.h>
24
#include <memory.h>
25
#include <types.h>
26
27
#include "libfsext_attribute_values.h"
28
#include "libfsext_attributes_block.h"
29
#include "libfsext_debug.h"
30
#include "libfsext_io_handle.h"
31
#include "libfsext_libbfio.h"
32
#include "libfsext_libcdata.h"
33
#include "libfsext_libcerror.h"
34
#include "libfsext_libcnotify.h"
35
36
#include "fsext_attributes.h"
37
38
/* Reads the extended attributes block header data
39
 * Returns 1 if successful or -1 on error
40
 */
41
int libfsext_attributes_block_read_header_data(
42
     libfsext_io_handle_t *io_handle,
43
     const uint8_t *data,
44
     size_t data_size,
45
     libcerror_error_t **error )
46
91
{
47
91
  static char *function     = "libfsext_attributes_block_read_header_data";
48
91
  uint32_t number_of_blocks = 0;
49
91
  uint32_t signature        = 0;
50
51
#if defined( HAVE_DEBUG_OUTPUT )
52
  uint32_t value_32bit      = 0;
53
#endif
54
55
91
  if( io_handle == NULL )
56
0
  {
57
0
    libcerror_error_set(
58
0
     error,
59
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
60
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
61
0
     "%s: invalid IO handle.",
62
0
     function );
63
64
0
    return( -1 );
65
0
  }
66
91
  if( data == NULL )
67
0
  {
68
0
    libcerror_error_set(
69
0
     error,
70
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
71
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
72
0
     "%s: invalid data.",
73
0
     function );
74
75
0
    return( -1 );
76
0
  }
77
91
  if( ( data_size < sizeof( fsext_attributes_header_ext2_t ) )
78
91
   || ( data_size > (size_t) SSIZE_MAX ) )
79
0
  {
80
0
    libcerror_error_set(
81
0
     error,
82
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
83
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
84
0
     "%s: invalid data size value out of bounds.",
85
0
     function );
86
87
0
    return( -1 );
88
0
  }
89
#if defined( HAVE_DEBUG_OUTPUT )
90
  if( libcnotify_verbose != 0 )
91
  {
92
    libcnotify_printf(
93
     "%s: extended attributes block header data:\n",
94
     function );
95
    libcnotify_print_data(
96
     data,
97
     sizeof( fsext_attributes_header_ext2_t ),
98
     LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
99
  }
100
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
101
102
91
  byte_stream_copy_to_uint32_little_endian(
103
91
   ( (fsext_attributes_header_ext2_t *) data )->signature,
104
91
   signature );
105
106
91
  byte_stream_copy_to_uint32_little_endian(
107
91
   ( (fsext_attributes_header_ext2_t *) data )->number_of_blocks,
108
91
   number_of_blocks );
109
110
#if defined( HAVE_DEBUG_OUTPUT )
111
  if( libcnotify_verbose != 0 )
112
  {
113
    libcnotify_printf(
114
     "%s: signature\t\t\t: 0x%08" PRIx32 "\n",
115
     function,
116
     signature );
117
118
    byte_stream_copy_to_uint32_little_endian(
119
     ( (fsext_attributes_header_ext2_t *) data )->reference_count,
120
     value_32bit );
121
    libcnotify_printf(
122
     "%s: reference count\t\t: %" PRIu32 "\n",
123
     function,
124
     value_32bit );
125
126
    libcnotify_printf(
127
     "%s: number of blocks\t\t: %" PRIu32 "\n",
128
     function,
129
     number_of_blocks );
130
131
    byte_stream_copy_to_uint32_little_endian(
132
     ( (fsext_attributes_header_ext2_t *) data )->attributes_hash,
133
     value_32bit );
134
    libcnotify_printf(
135
     "%s: attributes hash\t\t: 0x%08" PRIx32 "\n",
136
     function,
137
     value_32bit );
138
139
    if( ( io_handle->format_version == 2 )
140
     || ( io_handle->format_version == 3 ) )
141
    {
142
      libcnotify_printf(
143
       "%s: unknown1:\n",
144
       function );
145
      libcnotify_print_data(
146
       ( (fsext_attributes_header_ext2_t *) data )->unknown1,
147
       16,
148
       0 );
149
    }
150
    else if( io_handle->format_version == 4 )
151
    {
152
      byte_stream_copy_to_uint32_little_endian(
153
       ( (fsext_attributes_header_ext4_t *) data )->checksum,
154
       value_32bit );
155
      libcnotify_printf(
156
       "%s: checksum\t\t\t: 0x%08" PRIx32 "\n",
157
       function,
158
       value_32bit );
159
160
      libcnotify_printf(
161
       "%s: unknown1:\n",
162
       function );
163
      libcnotify_print_data(
164
       ( (fsext_attributes_header_ext4_t *) data )->unknown1,
165
       12,
166
       0 );
167
    }
168
  }
169
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
170
171
91
  if( signature != 0xea020000UL )
172
45
  {
173
45
    libcerror_error_set(
174
45
     error,
175
45
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
176
45
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
177
45
     "%s: invalid signature.",
178
45
     function );
179
180
45
    return( -1 );
181
45
  }
182
46
  if( number_of_blocks != 1 )
183
40
  {
184
40
    libcerror_error_set(
185
40
     error,
186
40
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
187
40
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
188
40
     "%s: invalid number of blocks value out of bounds.",
189
40
     function );
190
191
40
    return( -1 );
192
40
  }
193
6
  return( 1 );
194
46
}
195
196
/* Reads the extended attributes block entries data
197
 * Returns 1 if successful or -1 on error
198
 */
199
int libfsext_attributes_block_read_entries_data(
200
     const uint8_t *data,
201
     size_t data_size,
202
     size_t data_offset,
203
     libcdata_array_t *extended_attributes,
204
     libcerror_error_t **error )
205
244
{
206
244
  libfsext_attribute_values_t *attribute_values = NULL;
207
244
  static char *function                         = "libfsext_attributes_block_read_entries_data";
208
244
  size_t alignment_padding_size                 = 0;
209
244
  int attribute_index                           = 0;
210
244
  int entry_index                               = 0;
211
212
244
  if( data == NULL )
213
0
  {
214
0
    libcerror_error_set(
215
0
     error,
216
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
217
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
218
0
     "%s: invalid data.",
219
0
     function );
220
221
0
    return( -1 );
222
0
  }
223
244
  if( ( data_size < sizeof( fsext_attributes_entry_t ) )
224
244
   || ( data_size > (size_t) SSIZE_MAX ) )
225
4
  {
226
4
    libcerror_error_set(
227
4
     error,
228
4
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
229
4
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
230
4
     "%s: invalid data size value out of bounds.",
231
4
     function );
232
233
4
    return( -1 );
234
4
  }
235
240
  if( data_offset >= ( data_size - sizeof( fsext_attributes_entry_t ) ) )
236
2
  {
237
2
    libcerror_error_set(
238
2
     error,
239
2
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
240
2
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
241
2
     "%s: invalid data offset value out of bounds.",
242
2
     function );
243
244
2
    return( -1 );
245
2
  }
246
2.50k
  while( data_offset < data_size )
247
2.49k
  {
248
2.49k
    if( data_offset >= ( data_size - sizeof( fsext_attributes_entry_t ) ) )
249
15
    {
250
15
      libcerror_error_set(
251
15
       error,
252
15
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
253
15
       LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
254
15
       "%s: invalid data size value out of bounds.",
255
15
       function );
256
257
15
      goto on_error;
258
15
    }
259
    /* The list terminator consist of 8 0-byte values
260
     */
261
2.47k
    if( memory_compare(
262
2.47k
         &( data[ data_offset ] ),
263
2.47k
         "\x00\x00\x00\x00\x00\x00\x00\x00",
264
2.47k
         8 ) == 0 )
265
67
    {
266
#if defined( HAVE_DEBUG_OUTPUT )
267
      if( libcnotify_verbose != 0 )
268
      {
269
        libcnotify_printf(
270
         "%s: extended attributes entry: %d data:\n",
271
         function,
272
         attribute_index );
273
        libcnotify_print_data(
274
         &( data[ data_offset ] ),
275
         sizeof( fsext_attributes_entry_t ),
276
         LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
277
      }
278
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
279
280
67
      break;
281
67
    }
282
2.40k
    if( libfsext_attribute_values_initialize(
283
2.40k
         &attribute_values,
284
2.40k
         error ) != 1 )
285
0
    {
286
0
      libcerror_error_set(
287
0
       error,
288
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
289
0
       LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
290
0
       "%s: unable to create attribute: %d values.",
291
0
       function,
292
0
       attribute_index );
293
294
0
      goto on_error;
295
0
    }
296
2.40k
    if( libfsext_attribute_values_read_data(
297
2.40k
         attribute_values,
298
2.40k
         &( data[ data_offset ] ),
299
2.40k
         data_size - data_offset,
300
2.40k
         error ) != 1 )
301
32
    {
302
32
      libcerror_error_set(
303
32
       error,
304
32
       LIBCERROR_ERROR_DOMAIN_IO,
305
32
       LIBCERROR_IO_ERROR_READ_FAILED,
306
32
       "%s: unable to read attribute: %d values.",
307
32
       function,
308
32
       attribute_index );
309
310
32
      goto on_error;
311
32
    }
312
2.37k
    data_offset += sizeof( fsext_attributes_entry_t ) + data[ data_offset ];
313
314
2.37k
    alignment_padding_size = data_offset % 4;
315
316
2.37k
    if( alignment_padding_size != 0 )
317
829
    {
318
829
      alignment_padding_size = 4 - alignment_padding_size;
319
829
    }
320
2.37k
    if( alignment_padding_size > 0 )
321
829
    {
322
#if defined( HAVE_DEBUG_OUTPUT )
323
      if( libcnotify_verbose != 0 )
324
      {
325
        libcnotify_printf(
326
         "%s: alignment padding:\n",
327
         function );
328
        libcnotify_print_data(
329
         &( data[ data_offset ] ),
330
         alignment_padding_size,
331
         0 );
332
      }
333
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
334
335
829
      data_offset += alignment_padding_size;
336
829
    }
337
2.37k
    if( ( attribute_values->value_data_inode_number == 0 )
338
2.37k
     && ( attribute_values->value_data_size > 0 ) )
339
310
    {
340
310
      if( ( attribute_values->value_data_offset < sizeof( fsext_attributes_header_ext2_t ) )
341
310
       || ( attribute_values->value_data_offset >= data_size ) )
342
55
      {
343
55
        libcerror_error_set(
344
55
         error,
345
55
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
346
55
         LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
347
55
         "%s: invalid value data offset value out of bounds.",
348
55
         function );
349
350
55
        goto on_error;
351
55
      }
352
255
      if( attribute_values->value_data_size > ( data_size - attribute_values->value_data_offset ) )
353
53
      {
354
53
        libcerror_error_set(
355
53
         error,
356
53
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
357
53
         LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
358
53
         "%s: invalid value data size value out of bounds.",
359
53
         function );
360
361
53
        goto on_error;
362
53
      }
363
#if defined( HAVE_DEBUG_OUTPUT )
364
      if( libcnotify_verbose != 0 )
365
      {
366
        libcnotify_printf(
367
         "%s: attribute: %d value data:\n",
368
         function,
369
         attribute_index );
370
        libcnotify_print_data(
371
         &( data[ attribute_values->value_data_offset ] ),
372
         attribute_values->value_data_size,
373
         0 );
374
      }
375
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
376
377
202
      attribute_values->value_data = (uint8_t *) memory_allocate(
378
202
                                                  sizeof( uint8_t ) * attribute_values->value_data_size );
379
380
202
      if( attribute_values->value_data == NULL )
381
0
      {
382
0
        libcerror_error_set(
383
0
         error,
384
0
         LIBCERROR_ERROR_DOMAIN_MEMORY,
385
0
         LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
386
0
         "%s: unable to create value data.",
387
0
         function );
388
389
0
        goto on_error;
390
0
      }
391
202
      if( memory_copy(
392
202
           attribute_values->value_data,
393
202
           &( data[ attribute_values->value_data_offset ] ),
394
202
           (size_t) attribute_values->value_data_size ) == NULL )
395
0
      {
396
0
        libcerror_error_set(
397
0
         error,
398
0
         LIBCERROR_ERROR_DOMAIN_MEMORY,
399
0
         LIBCERROR_MEMORY_ERROR_COPY_FAILED,
400
0
         "%s: unable to copy value data to attribute values.",
401
0
         function );
402
403
0
        goto on_error;
404
0
      }
405
202
    }
406
2.26k
    if( libcdata_array_append_entry(
407
2.26k
         extended_attributes,
408
2.26k
         &entry_index,
409
2.26k
         (intptr_t *) attribute_values,
410
2.26k
         error ) != 1 )
411
0
    {
412
0
      libcerror_error_set(
413
0
       error,
414
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
415
0
       LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
416
0
       "%s: unable to append attribute: %d values to array.",
417
0
       function,
418
0
       attribute_index );
419
420
0
      goto on_error;
421
0
    }
422
2.26k
    attribute_values = NULL;
423
424
2.26k
    attribute_index++;
425
2.26k
  }
426
83
  return( 1 );
427
428
155
on_error:
429
155
  if( attribute_values != NULL )
430
140
  {
431
140
    libfsext_attribute_values_free(
432
140
     &attribute_values,
433
140
     NULL );
434
140
  }
435
155
  libcdata_array_empty(
436
155
   extended_attributes,
437
155
   (int (*)(intptr_t **, libcerror_error_t **)) &libfsext_attribute_values_free,
438
155
   NULL );
439
440
155
  return( -1 );
441
238
}
442
443
/* Reads the extended attributes block from a Basic File IO (bfio) handle
444
 * Returns 1 if successful or -1 on error
445
 */
446
int libfsext_attributes_block_read_file_io_handle(
447
     libcdata_array_t *extended_attributes,
448
     libfsext_io_handle_t *io_handle,
449
     libbfio_handle_t *file_io_handle,
450
     off64_t file_offset,
451
     libcerror_error_t **error )
452
179
{
453
179
  uint8_t *data         = NULL;
454
179
  static char *function = "libfsext_attributes_block_read_file_io_handle";
455
179
  ssize_t read_count    = 0;
456
457
179
  if( io_handle == NULL )
458
0
  {
459
0
    libcerror_error_set(
460
0
     error,
461
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
462
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
463
0
     "%s: invalid IO handle.",
464
0
     function );
465
466
0
    return( -1 );
467
0
  }
468
179
  if( ( io_handle->block_size == 0 )
469
179
   || ( io_handle->block_size > (uint32_t) MEMORY_MAXIMUM_ALLOCATION_SIZE ) )
470
0
  {
471
0
    libcerror_error_set(
472
0
     error,
473
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
474
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
475
0
     "%s: invalid IO handle - block size value out of bounds.",
476
0
     function );
477
478
0
    return( -1 );
479
0
  }
480
179
  data = (uint8_t *) memory_allocate(
481
179
                      sizeof( uint8_t ) * io_handle->block_size );
482
483
179
  if( data == NULL )
484
0
  {
485
0
    libcerror_error_set(
486
0
     error,
487
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
488
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
489
0
     "%s: unable to create data.",
490
0
     function );
491
492
0
    goto on_error;
493
0
  }
494
#if defined( HAVE_DEBUG_OUTPUT )
495
  if( libcnotify_verbose != 0 )
496
  {
497
    libcnotify_printf(
498
     "%s: reading extended attributes block at offset: %" PRIi64 " (0x%08" PRIx64 ")\n",
499
     function,
500
     file_offset,
501
     file_offset );
502
  }
503
#endif
504
179
  read_count = libbfio_handle_read_buffer_at_offset(
505
179
                file_io_handle,
506
179
                data,
507
179
                (size_t) io_handle->block_size,
508
179
                file_offset,
509
179
                error );
510
511
179
  if( read_count != (ssize_t) io_handle->block_size )
512
88
  {
513
88
    libcerror_error_set(
514
88
     error,
515
88
     LIBCERROR_ERROR_DOMAIN_IO,
516
88
     LIBCERROR_IO_ERROR_READ_FAILED,
517
88
     "%s: unable to read extended attributes block data at offset: %" PRIi64 " (0x%08" PRIx64 ").",
518
88
     function,
519
88
     file_offset,
520
88
     file_offset );
521
522
88
    goto on_error;
523
88
  }
524
91
  if( libfsext_attributes_block_read_header_data(
525
91
       io_handle,
526
91
       data,
527
91
       (size_t) io_handle->block_size,
528
91
       error ) != 1 )
529
85
  {
530
85
    libcerror_error_set(
531
85
     error,
532
85
     LIBCERROR_ERROR_DOMAIN_IO,
533
85
     LIBCERROR_IO_ERROR_READ_FAILED,
534
85
     "%s: unable to read extended attributes block header.",
535
85
     function );
536
537
85
    goto on_error;
538
85
  }
539
6
  if( libfsext_attributes_block_read_entries_data(
540
6
       data,
541
6
       (size_t) io_handle->block_size,
542
6
       sizeof( fsext_attributes_header_ext2_t ),
543
6
       extended_attributes,
544
6
       error ) != 1 )
545
2
  {
546
2
    libcerror_error_set(
547
2
     error,
548
2
     LIBCERROR_ERROR_DOMAIN_IO,
549
2
     LIBCERROR_IO_ERROR_READ_FAILED,
550
2
     "%s: unable to read extended attributes block entries.",
551
2
     function );
552
553
2
    goto on_error;
554
2
  }
555
4
  memory_free(
556
4
   data );
557
558
4
  return( 1 );
559
560
175
on_error:
561
175
  if( data != NULL )
562
175
  {
563
175
    memory_free(
564
175
     data );
565
175
  }
566
175
  return( -1 );
567
6
}
568