Coverage Report

Created: 2026-01-13 07:09

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-2025, 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
73
{
47
73
  static char *function     = "libfsext_attributes_block_read_header_data";
48
73
  uint32_t number_of_blocks = 0;
49
73
  uint32_t signature        = 0;
50
51
#if defined( HAVE_DEBUG_OUTPUT )
52
  uint32_t value_32bit      = 0;
53
#endif
54
55
73
  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
73
  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
73
  if( ( data_size < sizeof( fsext_attributes_header_ext2_t ) )
78
73
   || ( 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
73
  byte_stream_copy_to_uint32_little_endian(
103
73
   ( (fsext_attributes_header_ext2_t *) data )->signature,
104
73
   signature );
105
106
73
  byte_stream_copy_to_uint32_little_endian(
107
73
   ( (fsext_attributes_header_ext2_t *) data )->number_of_blocks,
108
73
   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
73
  if( signature != 0xea020000UL )
172
43
  {
173
43
    libcerror_error_set(
174
43
     error,
175
43
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
176
43
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
177
43
     "%s: invalid signature.",
178
43
     function );
179
180
43
    return( -1 );
181
43
  }
182
30
  if( number_of_blocks != 1 )
183
24
  {
184
24
    libcerror_error_set(
185
24
     error,
186
24
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
187
24
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
188
24
     "%s: invalid number of blocks value out of bounds.",
189
24
     function );
190
191
24
    return( -1 );
192
24
  }
193
6
  return( 1 );
194
30
}
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
303
{
206
303
  libfsext_attribute_values_t *attribute_values = NULL;
207
303
  static char *function                         = "libfsext_attributes_block_read_entries_data";
208
303
  size_t alignment_padding_size                 = 0;
209
303
  int attribute_index                           = 0;
210
303
  int entry_index                               = 0;
211
212
303
  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
303
  if( ( data_size < sizeof( fsext_attributes_entry_t ) )
224
300
   || ( 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
300
  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.33k
  while( data_offset < data_size )
247
2.23k
  {
248
2.23k
    if( data_offset >= ( data_size - sizeof( fsext_attributes_entry_t ) ) )
249
10
    {
250
10
      libcerror_error_set(
251
10
       error,
252
10
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
253
10
       LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
254
10
       "%s: invalid data size value out of bounds.",
255
10
       function );
256
257
10
      goto on_error;
258
10
    }
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.22k
    if( memory_compare(
264
2.22k
         &( data[ data_offset ] ),
265
2.22k
         "\x00\x00\x00\x00",
266
2.22k
         4 ) == 0 )
267
61
    {
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
61
      break;
283
61
    }
284
2.15k
    if( libfsext_attribute_values_initialize(
285
2.15k
         &attribute_values,
286
2.15k
         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.15k
    if( libfsext_attribute_values_read_data(
299
2.15k
         attribute_values,
300
2.15k
         &( data[ data_offset ] ),
301
2.15k
         data_size - data_offset,
302
2.15k
         error ) != 1 )
303
25
    {
304
25
      libcerror_error_set(
305
25
       error,
306
25
       LIBCERROR_ERROR_DOMAIN_IO,
307
25
       LIBCERROR_IO_ERROR_READ_FAILED,
308
25
       "%s: unable to read attribute: %d values.",
309
25
       function,
310
25
       attribute_index );
311
312
25
      goto on_error;
313
25
    }
314
2.13k
    data_offset += sizeof( fsext_attributes_entry_t ) + data[ data_offset ];
315
316
2.13k
    alignment_padding_size = data_offset % 4;
317
318
2.13k
    if( alignment_padding_size != 0 )
319
900
    {
320
900
      alignment_padding_size = 4 - alignment_padding_size;
321
900
    }
322
2.13k
    if( alignment_padding_size > 0 )
323
900
    {
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
900
      data_offset += alignment_padding_size;
338
900
    }
339
2.13k
    if( ( attribute_values->value_data_inode_number == 0 )
340
435
     && ( attribute_values->value_data_size > 0 ) )
341
248
    {
342
248
      if( ( attribute_values->value_data_offset < sizeof( fsext_attributes_header_ext2_t ) )
343
234
       || ( attribute_values->value_data_offset >= data_size ) )
344
52
      {
345
52
        libcerror_error_set(
346
52
         error,
347
52
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
348
52
         LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
349
52
         "%s: invalid value data offset value out of bounds.",
350
52
         function );
351
352
52
        goto on_error;
353
52
      }
354
196
      if( attribute_values->value_data_size > ( data_size - attribute_values->value_data_offset ) )
355
45
      {
356
45
        libcerror_error_set(
357
45
         error,
358
45
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
359
45
         LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
360
45
         "%s: invalid value data size value out of bounds.",
361
45
         function );
362
363
45
        goto on_error;
364
45
      }
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
151
      attribute_values->value_data = (uint8_t *) memory_allocate(
380
151
                                                  sizeof( uint8_t ) * attribute_values->value_data_size );
381
382
151
      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
151
      if( memory_copy(
394
151
           attribute_values->value_data,
395
151
           &( data[ attribute_values->value_data_offset ] ),
396
151
           (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
151
    }
408
2.03k
    if( libcdata_array_append_entry(
409
2.03k
         extended_attributes,
410
2.03k
         &entry_index,
411
2.03k
         (intptr_t *) attribute_values,
412
2.03k
         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.03k
    attribute_values = NULL;
425
426
2.03k
    attribute_index++;
427
2.03k
  }
428
166
  return( 1 );
429
430
132
on_error:
431
132
  if( attribute_values != NULL )
432
122
  {
433
122
    libfsext_attribute_values_free(
434
122
     &attribute_values,
435
122
     NULL );
436
122
  }
437
132
  libcdata_array_empty(
438
132
   extended_attributes,
439
132
   (int (*)(intptr_t **, libcerror_error_t **)) &libfsext_attribute_values_free,
440
132
   NULL );
441
442
132
  return( -1 );
443
298
}
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
178
{
455
178
  uint8_t *data         = NULL;
456
178
  static char *function = "libfsext_attributes_block_read_file_io_handle";
457
178
  ssize_t read_count    = 0;
458
459
178
  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
178
  if( ( io_handle->block_size == 0 )
471
178
   || ( 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
178
  data = (uint8_t *) memory_allocate(
483
178
                      sizeof( uint8_t ) * io_handle->block_size );
484
485
178
  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
178
  read_count = libbfio_handle_read_buffer_at_offset(
507
178
                file_io_handle,
508
178
                data,
509
178
                (size_t) io_handle->block_size,
510
178
                file_offset,
511
178
                error );
512
513
178
  if( read_count != (ssize_t) io_handle->block_size )
514
105
  {
515
105
    libcerror_error_set(
516
105
     error,
517
105
     LIBCERROR_ERROR_DOMAIN_IO,
518
105
     LIBCERROR_IO_ERROR_READ_FAILED,
519
105
     "%s: unable to read extended attributes block data at offset: %" PRIi64 " (0x%08" PRIx64 ").",
520
105
     function,
521
105
     file_offset,
522
105
     file_offset );
523
524
105
    goto on_error;
525
105
  }
526
73
  if( libfsext_attributes_block_read_header_data(
527
73
       io_handle,
528
73
       data,
529
73
       (size_t) io_handle->block_size,
530
73
       error ) != 1 )
531
67
  {
532
67
    libcerror_error_set(
533
67
     error,
534
67
     LIBCERROR_ERROR_DOMAIN_IO,
535
67
     LIBCERROR_IO_ERROR_READ_FAILED,
536
67
     "%s: unable to read extended attributes block header.",
537
67
     function );
538
539
67
    goto on_error;
540
67
  }
541
6
  if( libfsext_attributes_block_read_entries_data(
542
6
       data,
543
6
       (size_t) io_handle->block_size,
544
6
       sizeof( fsext_attributes_header_ext2_t ),
545
6
       extended_attributes,
546
6
       error ) != 1 )
547
2
  {
548
2
    libcerror_error_set(
549
2
     error,
550
2
     LIBCERROR_ERROR_DOMAIN_IO,
551
2
     LIBCERROR_IO_ERROR_READ_FAILED,
552
2
     "%s: unable to read extended attributes block entries.",
553
2
     function );
554
555
2
    goto on_error;
556
2
  }
557
4
  memory_free(
558
4
   data );
559
560
4
  return( 1 );
561
562
174
on_error:
563
174
  if( data != NULL )
564
174
  {
565
174
    memory_free(
566
174
     data );
567
174
  }
568
174
  return( -1 );
569
6
}
570