Coverage Report

Created: 2025-06-13 07:22

/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
90
{
47
90
  static char *function     = "libfsext_attributes_block_read_header_data";
48
90
  uint32_t number_of_blocks = 0;
49
90
  uint32_t signature        = 0;
50
51
#if defined( HAVE_DEBUG_OUTPUT )
52
  uint32_t value_32bit      = 0;
53
#endif
54
55
90
  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
90
  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
90
  if( ( data_size < sizeof( fsext_attributes_header_ext2_t ) )
78
90
   || ( 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
90
  byte_stream_copy_to_uint32_little_endian(
103
90
   ( (fsext_attributes_header_ext2_t *) data )->signature,
104
90
   signature );
105
106
90
  byte_stream_copy_to_uint32_little_endian(
107
90
   ( (fsext_attributes_header_ext2_t *) data )->number_of_blocks,
108
90
   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
90
  if( signature != 0xea020000UL )
172
46
  {
173
46
    libcerror_error_set(
174
46
     error,
175
46
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
176
46
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
177
46
     "%s: invalid signature.",
178
46
     function );
179
180
46
    return( -1 );
181
46
  }
182
44
  if( number_of_blocks != 1 )
183
37
  {
184
37
    libcerror_error_set(
185
37
     error,
186
37
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
187
37
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
188
37
     "%s: invalid number of blocks value out of bounds.",
189
37
     function );
190
191
37
    return( -1 );
192
37
  }
193
7
  return( 1 );
194
44
}
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
249
{
206
249
  libfsext_attribute_values_t *attribute_values = NULL;
207
249
  static char *function                         = "libfsext_attributes_block_read_entries_data";
208
249
  size_t alignment_padding_size                 = 0;
209
249
  int attribute_index                           = 0;
210
249
  int entry_index                               = 0;
211
212
249
  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
249
  if( ( data_size < sizeof( fsext_attributes_entry_t ) )
224
249
   || ( data_size > (size_t) SSIZE_MAX ) )
225
5
  {
226
5
    libcerror_error_set(
227
5
     error,
228
5
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
229
5
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
230
5
     "%s: invalid data size value out of bounds.",
231
5
     function );
232
233
5
    return( -1 );
234
5
  }
235
244
  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.57k
  while( data_offset < data_size )
247
2.55k
  {
248
2.55k
    if( data_offset >= ( data_size - sizeof( fsext_attributes_entry_t ) ) )
249
13
    {
250
13
      libcerror_error_set(
251
13
       error,
252
13
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
253
13
       LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
254
13
       "%s: invalid data size value out of bounds.",
255
13
       function );
256
257
13
      goto on_error;
258
13
    }
259
    /* The list terminator consist of 8 0-byte values
260
     */
261
2.54k
    if( memory_compare(
262
2.54k
         &( data[ data_offset ] ),
263
2.54k
         "\x00\x00\x00\x00\x00\x00\x00\x00",
264
2.54k
         8 ) == 0 )
265
71
    {
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
71
      break;
281
71
    }
282
2.47k
    if( libfsext_attribute_values_initialize(
283
2.47k
         &attribute_values,
284
2.47k
         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.47k
    if( libfsext_attribute_values_read_data(
297
2.47k
         attribute_values,
298
2.47k
         &( data[ data_offset ] ),
299
2.47k
         data_size - data_offset,
300
2.47k
         error ) != 1 )
301
33
    {
302
33
      libcerror_error_set(
303
33
       error,
304
33
       LIBCERROR_ERROR_DOMAIN_IO,
305
33
       LIBCERROR_IO_ERROR_READ_FAILED,
306
33
       "%s: unable to read attribute: %d values.",
307
33
       function,
308
33
       attribute_index );
309
310
33
      goto on_error;
311
33
    }
312
2.44k
    data_offset += sizeof( fsext_attributes_entry_t ) + data[ data_offset ];
313
314
2.44k
    alignment_padding_size = data_offset % 4;
315
316
2.44k
    if( alignment_padding_size != 0 )
317
866
    {
318
866
      alignment_padding_size = 4 - alignment_padding_size;
319
866
    }
320
2.44k
    if( alignment_padding_size > 0 )
321
866
    {
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
866
      data_offset += alignment_padding_size;
336
866
    }
337
2.44k
    if( ( attribute_values->value_data_inode_number == 0 )
338
2.44k
     && ( attribute_values->value_data_size > 0 ) )
339
305
    {
340
305
      if( ( attribute_values->value_data_offset < sizeof( fsext_attributes_header_ext2_t ) )
341
305
       || ( attribute_values->value_data_offset >= data_size ) )
342
57
      {
343
57
        libcerror_error_set(
344
57
         error,
345
57
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
346
57
         LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
347
57
         "%s: invalid value data offset value out of bounds.",
348
57
         function );
349
350
57
        goto on_error;
351
57
      }
352
248
      if( attribute_values->value_data_size > ( data_size - attribute_values->value_data_offset ) )
353
50
      {
354
50
        libcerror_error_set(
355
50
         error,
356
50
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
357
50
         LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
358
50
         "%s: invalid value data size value out of bounds.",
359
50
         function );
360
361
50
        goto on_error;
362
50
      }
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
198
      attribute_values->value_data = (uint8_t *) memory_allocate(
378
198
                                                  sizeof( uint8_t ) * attribute_values->value_data_size );
379
380
198
      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
198
      if( memory_copy(
392
198
           attribute_values->value_data,
393
198
           &( data[ attribute_values->value_data_offset ] ),
394
198
           (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
198
    }
406
2.33k
    if( libcdata_array_append_entry(
407
2.33k
         extended_attributes,
408
2.33k
         &entry_index,
409
2.33k
         (intptr_t *) attribute_values,
410
2.33k
         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.33k
    attribute_values = NULL;
423
424
2.33k
    attribute_index++;
425
2.33k
  }
426
89
  return( 1 );
427
428
153
on_error:
429
153
  if( attribute_values != NULL )
430
140
  {
431
140
    libfsext_attribute_values_free(
432
140
     &attribute_values,
433
140
     NULL );
434
140
  }
435
153
  libcdata_array_empty(
436
153
   extended_attributes,
437
153
   (int (*)(intptr_t **, libcerror_error_t **)) &libfsext_attribute_values_free,
438
153
   NULL );
439
440
153
  return( -1 );
441
242
}
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
177
{
453
177
  uint8_t *data         = NULL;
454
177
  static char *function = "libfsext_attributes_block_read_file_io_handle";
455
177
  ssize_t read_count    = 0;
456
457
177
  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
177
  if( ( io_handle->block_size == 0 )
469
177
   || ( 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
177
  data = (uint8_t *) memory_allocate(
481
177
                      sizeof( uint8_t ) * io_handle->block_size );
482
483
177
  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
177
  read_count = libbfio_handle_read_buffer_at_offset(
505
177
                file_io_handle,
506
177
                data,
507
177
                (size_t) io_handle->block_size,
508
177
                file_offset,
509
177
                error );
510
511
177
  if( read_count != (ssize_t) io_handle->block_size )
512
87
  {
513
87
    libcerror_error_set(
514
87
     error,
515
87
     LIBCERROR_ERROR_DOMAIN_IO,
516
87
     LIBCERROR_IO_ERROR_READ_FAILED,
517
87
     "%s: unable to read extended attributes block data at offset: %" PRIi64 " (0x%08" PRIx64 ").",
518
87
     function,
519
87
     file_offset,
520
87
     file_offset );
521
522
87
    goto on_error;
523
87
  }
524
90
  if( libfsext_attributes_block_read_header_data(
525
90
       io_handle,
526
90
       data,
527
90
       (size_t) io_handle->block_size,
528
90
       error ) != 1 )
529
83
  {
530
83
    libcerror_error_set(
531
83
     error,
532
83
     LIBCERROR_ERROR_DOMAIN_IO,
533
83
     LIBCERROR_IO_ERROR_READ_FAILED,
534
83
     "%s: unable to read extended attributes block header.",
535
83
     function );
536
537
83
    goto on_error;
538
83
  }
539
7
  if( libfsext_attributes_block_read_entries_data(
540
7
       data,
541
7
       (size_t) io_handle->block_size,
542
7
       sizeof( fsext_attributes_header_ext2_t ),
543
7
       extended_attributes,
544
7
       error ) != 1 )
545
3
  {
546
3
    libcerror_error_set(
547
3
     error,
548
3
     LIBCERROR_ERROR_DOMAIN_IO,
549
3
     LIBCERROR_IO_ERROR_READ_FAILED,
550
3
     "%s: unable to read extended attributes block entries.",
551
3
     function );
552
553
3
    goto on_error;
554
3
  }
555
4
  memory_free(
556
4
   data );
557
558
4
  return( 1 );
559
560
173
on_error:
561
173
  if( data != NULL )
562
173
  {
563
173
    memory_free(
564
173
     data );
565
173
  }
566
173
  return( -1 );
567
7
}
568