Coverage Report

Created: 2026-04-04 07:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libfsext/libfsext/libfsext_attributes_block.c
Line
Count
Source
1
/*
2
 * Extended attributes block functions
3
 *
4
 * Copyright (C) 2010-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 <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
84
{
47
84
  static char *function     = "libfsext_attributes_block_read_header_data";
48
84
  uint32_t number_of_blocks = 0;
49
84
  uint32_t signature        = 0;
50
51
#if defined( HAVE_DEBUG_OUTPUT )
52
  uint32_t value_32bit      = 0;
53
#endif
54
55
84
  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
84
  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
84
  if( ( data_size < sizeof( fsext_attributes_header_ext2_t ) )
78
84
   || ( 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
84
  byte_stream_copy_to_uint32_little_endian(
103
84
   ( (fsext_attributes_header_ext2_t *) data )->signature,
104
84
   signature );
105
106
84
  byte_stream_copy_to_uint32_little_endian(
107
84
   ( (fsext_attributes_header_ext2_t *) data )->number_of_blocks,
108
84
   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
84
  if( signature != 0xea020000UL )
172
37
  {
173
37
    libcerror_error_set(
174
37
     error,
175
37
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
176
37
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
177
37
     "%s: invalid signature.",
178
37
     function );
179
180
37
    return( -1 );
181
37
  }
182
47
  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
7
  return( 1 );
194
47
}
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
313
{
206
313
  libfsext_attribute_values_t *attribute_values = NULL;
207
313
  static char *function                         = "libfsext_attributes_block_read_entries_data";
208
313
  size_t alignment_padding_size                 = 0;
209
313
  int attribute_index                           = 0;
210
313
  int entry_index                               = 0;
211
212
313
  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
313
  if( ( data_size < sizeof( fsext_attributes_entry_t ) )
224
310
   || ( data_size > (size_t) SSIZE_MAX ) )
225
3
  {
226
3
    libcerror_error_set(
227
3
     error,
228
3
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
229
3
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
230
3
     "%s: invalid data size value out of bounds.",
231
3
     function );
232
233
3
    return( -1 );
234
3
  }
235
310
  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.51k
  while( data_offset < data_size )
247
2.40k
  {
248
2.40k
    if( data_offset >= ( data_size - sizeof( fsext_attributes_entry_t ) ) )
249
14
    {
250
14
      libcerror_error_set(
251
14
       error,
252
14
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
253
14
       LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
254
14
       "%s: invalid data size value out of bounds.",
255
14
       function );
256
257
14
      goto on_error;
258
14
    }
259
    /* The list terminator has the first 4 values set to 0 (8 bytes).
260
     * Note that some implementations of older Android versions of ext appear to
261
     * only set the first 4 bytes to 0.
262
     */
263
2.39k
    if( memory_compare(
264
2.39k
         &( data[ data_offset ] ),
265
2.39k
         "\x00\x00\x00\x00",
266
2.39k
         4 ) == 0 )
267
58
    {
268
#if defined( HAVE_DEBUG_OUTPUT )
269
      if( libcnotify_verbose != 0 )
270
      {
271
        libcnotify_printf(
272
         "%s: extended attributes entry: %d data:\n",
273
         function,
274
         attribute_index );
275
        libcnotify_print_data(
276
         &( data[ data_offset ] ),
277
         sizeof( fsext_attributes_entry_t ),
278
         LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
279
      }
280
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
281
282
58
      break;
283
58
    }
284
2.33k
    if( libfsext_attribute_values_initialize(
285
2.33k
         &attribute_values,
286
2.33k
         error ) != 1 )
287
0
    {
288
0
      libcerror_error_set(
289
0
       error,
290
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
291
0
       LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
292
0
       "%s: unable to create attribute: %d values.",
293
0
       function,
294
0
       attribute_index );
295
296
0
      goto on_error;
297
0
    }
298
2.33k
    if( libfsext_attribute_values_read_data(
299
2.33k
         attribute_values,
300
2.33k
         &( data[ data_offset ] ),
301
2.33k
         data_size - data_offset,
302
2.33k
         error ) != 1 )
303
30
    {
304
30
      libcerror_error_set(
305
30
       error,
306
30
       LIBCERROR_ERROR_DOMAIN_IO,
307
30
       LIBCERROR_IO_ERROR_READ_FAILED,
308
30
       "%s: unable to read attribute: %d values.",
309
30
       function,
310
30
       attribute_index );
311
312
30
      goto on_error;
313
30
    }
314
2.30k
    data_offset += sizeof( fsext_attributes_entry_t ) + data[ data_offset ];
315
316
2.30k
    alignment_padding_size = data_offset % 4;
317
318
2.30k
    if( alignment_padding_size != 0 )
319
1.00k
    {
320
1.00k
      alignment_padding_size = 4 - alignment_padding_size;
321
1.00k
    }
322
2.30k
    if( alignment_padding_size > 0 )
323
1.00k
    {
324
#if defined( HAVE_DEBUG_OUTPUT )
325
      if( libcnotify_verbose != 0 )
326
      {
327
        libcnotify_printf(
328
         "%s: alignment padding:\n",
329
         function );
330
        libcnotify_print_data(
331
         &( data[ data_offset ] ),
332
         alignment_padding_size,
333
         0 );
334
      }
335
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
336
337
1.00k
      data_offset += alignment_padding_size;
338
1.00k
    }
339
2.30k
    if( ( attribute_values->value_data_inode_number == 0 )
340
492
     && ( attribute_values->value_data_size > 0 ) )
341
310
    {
342
310
      if( ( attribute_values->value_data_offset < sizeof( fsext_attributes_header_ext2_t ) )
343
298
       || ( attribute_values->value_data_offset >= data_size ) )
344
50
      {
345
50
        libcerror_error_set(
346
50
         error,
347
50
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
348
50
         LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
349
50
         "%s: invalid value data offset value out of bounds.",
350
50
         function );
351
352
50
        goto on_error;
353
50
      }
354
260
      if( attribute_values->value_data_size > ( data_size - attribute_values->value_data_offset ) )
355
53
      {
356
53
        libcerror_error_set(
357
53
         error,
358
53
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
359
53
         LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
360
53
         "%s: invalid value data size value out of bounds.",
361
53
         function );
362
363
53
        goto on_error;
364
53
      }
365
#if defined( HAVE_DEBUG_OUTPUT )
366
      if( libcnotify_verbose != 0 )
367
      {
368
        libcnotify_printf(
369
         "%s: attribute: %d value data:\n",
370
         function,
371
         attribute_index );
372
        libcnotify_print_data(
373
         &( data[ attribute_values->value_data_offset ] ),
374
         attribute_values->value_data_size,
375
         0 );
376
      }
377
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
378
379
207
      attribute_values->value_data = (uint8_t *) memory_allocate(
380
207
                                                  sizeof( uint8_t ) * attribute_values->value_data_size );
381
382
207
      if( attribute_values->value_data == NULL )
383
0
      {
384
0
        libcerror_error_set(
385
0
         error,
386
0
         LIBCERROR_ERROR_DOMAIN_MEMORY,
387
0
         LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
388
0
         "%s: unable to create value data.",
389
0
         function );
390
391
0
        goto on_error;
392
0
      }
393
207
      if( memory_copy(
394
207
           attribute_values->value_data,
395
207
           &( data[ attribute_values->value_data_offset ] ),
396
207
           (size_t) attribute_values->value_data_size ) == NULL )
397
0
      {
398
0
        libcerror_error_set(
399
0
         error,
400
0
         LIBCERROR_ERROR_DOMAIN_MEMORY,
401
0
         LIBCERROR_MEMORY_ERROR_COPY_FAILED,
402
0
         "%s: unable to copy value data to attribute values.",
403
0
         function );
404
405
0
        goto on_error;
406
0
      }
407
207
    }
408
2.20k
    if( libcdata_array_append_entry(
409
2.20k
         extended_attributes,
410
2.20k
         &entry_index,
411
2.20k
         (intptr_t *) attribute_values,
412
2.20k
         error ) != 1 )
413
0
    {
414
0
      libcerror_error_set(
415
0
       error,
416
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
417
0
       LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
418
0
       "%s: unable to append attribute: %d values to array.",
419
0
       function,
420
0
       attribute_index );
421
422
0
      goto on_error;
423
0
    }
424
2.20k
    attribute_values = NULL;
425
426
2.20k
    attribute_index++;
427
2.20k
  }
428
161
  return( 1 );
429
430
147
on_error:
431
147
  if( attribute_values != NULL )
432
133
  {
433
133
    libfsext_attribute_values_free(
434
133
     &attribute_values,
435
133
     NULL );
436
133
  }
437
147
  libcdata_array_empty(
438
147
   extended_attributes,
439
147
   (int (*)(intptr_t **, libcerror_error_t **)) &libfsext_attribute_values_free,
440
147
   NULL );
441
442
147
  return( -1 );
443
308
}
444
445
/* Reads the extended attributes block from a Basic File IO (bfio) handle
446
 * Returns 1 if successful or -1 on error
447
 */
448
int libfsext_attributes_block_read_file_io_handle(
449
     libcdata_array_t *extended_attributes,
450
     libfsext_io_handle_t *io_handle,
451
     libbfio_handle_t *file_io_handle,
452
     off64_t file_offset,
453
     libcerror_error_t **error )
454
199
{
455
199
  uint8_t *data         = NULL;
456
199
  static char *function = "libfsext_attributes_block_read_file_io_handle";
457
199
  ssize_t read_count    = 0;
458
459
199
  if( io_handle == NULL )
460
0
  {
461
0
    libcerror_error_set(
462
0
     error,
463
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
464
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
465
0
     "%s: invalid IO handle.",
466
0
     function );
467
468
0
    return( -1 );
469
0
  }
470
199
  if( ( io_handle->block_size == 0 )
471
199
   || ( io_handle->block_size > (uint32_t) MEMORY_MAXIMUM_ALLOCATION_SIZE ) )
472
0
  {
473
0
    libcerror_error_set(
474
0
     error,
475
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
476
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
477
0
     "%s: invalid IO handle - block size value out of bounds.",
478
0
     function );
479
480
0
    return( -1 );
481
0
  }
482
199
  data = (uint8_t *) memory_allocate(
483
199
                      sizeof( uint8_t ) * io_handle->block_size );
484
485
199
  if( data == NULL )
486
0
  {
487
0
    libcerror_error_set(
488
0
     error,
489
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
490
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
491
0
     "%s: unable to create data.",
492
0
     function );
493
494
0
    goto on_error;
495
0
  }
496
#if defined( HAVE_DEBUG_OUTPUT )
497
  if( libcnotify_verbose != 0 )
498
  {
499
    libcnotify_printf(
500
     "%s: reading extended attributes block at offset: %" PRIi64 " (0x%08" PRIx64 ")\n",
501
     function,
502
     file_offset,
503
     file_offset );
504
  }
505
#endif
506
199
  read_count = libbfio_handle_read_buffer_at_offset(
507
199
                file_io_handle,
508
199
                data,
509
199
                (size_t) io_handle->block_size,
510
199
                file_offset,
511
199
                error );
512
513
199
  if( read_count != (ssize_t) io_handle->block_size )
514
115
  {
515
115
    libcerror_error_set(
516
115
     error,
517
115
     LIBCERROR_ERROR_DOMAIN_IO,
518
115
     LIBCERROR_IO_ERROR_READ_FAILED,
519
115
     "%s: unable to read extended attributes block data at offset: %" PRIi64 " (0x%08" PRIx64 ").",
520
115
     function,
521
115
     file_offset,
522
115
     file_offset );
523
524
115
    goto on_error;
525
115
  }
526
84
  if( libfsext_attributes_block_read_header_data(
527
84
       io_handle,
528
84
       data,
529
84
       (size_t) io_handle->block_size,
530
84
       error ) != 1 )
531
77
  {
532
77
    libcerror_error_set(
533
77
     error,
534
77
     LIBCERROR_ERROR_DOMAIN_IO,
535
77
     LIBCERROR_IO_ERROR_READ_FAILED,
536
77
     "%s: unable to read extended attributes block header.",
537
77
     function );
538
539
77
    goto on_error;
540
77
  }
541
7
  if( libfsext_attributes_block_read_entries_data(
542
7
       data,
543
7
       (size_t) io_handle->block_size,
544
7
       sizeof( fsext_attributes_header_ext2_t ),
545
7
       extended_attributes,
546
7
       error ) != 1 )
547
4
  {
548
4
    libcerror_error_set(
549
4
     error,
550
4
     LIBCERROR_ERROR_DOMAIN_IO,
551
4
     LIBCERROR_IO_ERROR_READ_FAILED,
552
4
     "%s: unable to read extended attributes block entries.",
553
4
     function );
554
555
4
    goto on_error;
556
4
  }
557
3
  memory_free(
558
3
   data );
559
560
3
  return( 1 );
561
562
196
on_error:
563
196
  if( data != NULL )
564
196
  {
565
196
    memory_free(
566
196
     data );
567
196
  }
568
196
  return( -1 );
569
7
}
570