aboutsummaryrefslogtreecommitdiff
path: root/lib/sqfs/src/xattr/xattr_reader.c
blob: 1c8ea374e868ca045517030b84b361062cd67a61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/* SPDX-License-Identifier: LGPL-3.0-or-later */
/*
 * xattr_reader.c
 *
 * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
 */
#define SQFS_BUILDING_DLL
#include "config.h"

#include "sqfs/xattr_reader.h"
#include "sqfs/meta_reader.h"
#include "sqfs/super.h"
#include "sqfs/xattr.h"
#include "sqfs/error.h"
#include "sqfs/block.h"
#include "sqfs/io.h"
#include "util/util.h"

#include <stdlib.h>
#include <string.h>
#include <errno.h>

struct sqfs_xattr_reader_t {
	sqfs_object_t base;

	sqfs_u64 xattr_start;
	sqfs_u64 xattr_end;

	size_t num_id_blocks;
	size_t num_ids;

	sqfs_u64 *id_block_starts;

	sqfs_meta_reader_t *idrd;
	sqfs_meta_reader_t *kvrd;
};

static sqfs_object_t *xattr_reader_copy(const sqfs_object_t *obj)
{
	const sqfs_xattr_reader_t *xr = (const sqfs_xattr_reader_t *)obj;
	sqfs_xattr_reader_t *copy = malloc(sizeof(*copy));

	if (copy == NULL)
		return NULL;

	memcpy(copy, xr, sizeof(*xr));

	if (xr->kvrd != NULL) {
		copy->kvrd = sqfs_copy(xr->kvrd);
		if (copy->kvrd == NULL)
			goto fail;
	}

	if (xr->idrd != NULL) {
		copy->idrd = sqfs_copy(xr->idrd);
		if (copy->idrd == NULL)
			goto fail;
	}

	if (xr->id_block_starts != NULL) {
		copy->id_block_starts = alloc_array(sizeof(sqfs_u64),
						    xr->num_id_blocks);
		if (copy->id_block_starts == NULL)
			goto fail;

		memcpy(copy->id_block_starts, xr->id_block_starts,
		       sizeof(sqfs_u64) * xr->num_id_blocks);
	}

	return (sqfs_object_t *)copy;
fail:
	sqfs_drop(copy->idrd);
	sqfs_drop(copy->kvrd);
	free(copy);
	return NULL;
}

static void xattr_reader_destroy(sqfs_object_t *obj)
{
	sqfs_xattr_reader_t *xr = (sqfs_xattr_reader_t *)obj;

	sqfs_drop(xr->kvrd);
	sqfs_drop(xr->idrd);
	free(xr->id_block_starts);
	free(xr);
}

int sqfs_xattr_reader_load(sqfs_xattr_reader_t *xr, const sqfs_super_t *super,
			   sqfs_file_t *file, sqfs_compressor_t *cmp)
{
	sqfs_xattr_id_table_t idtbl;
	size_t i;
	int err;

	/* sanity check */
	if (super->flags & SQFS_FLAG_NO_XATTRS)
		return 0;

	if (super->xattr_id_table_start == 0xFFFFFFFFFFFFFFFF)
		return 0;

	if (super->xattr_id_table_start >= super->bytes_used)
		return SQFS_ERROR_OUT_OF_BOUNDS;

	/* cleanup pre-existing data */
	xr->idrd = sqfs_drop(xr->idrd);
	xr->kvrd = sqfs_drop(xr->kvrd);

	free(xr->id_block_starts);
	xr->id_block_starts = NULL;

	/* read the locations table */
	err = file->read_at(file, super->xattr_id_table_start,
			    &idtbl, sizeof(idtbl));
	if (err)
		return err;

	xr->xattr_start = le64toh(idtbl.xattr_table_start);
	xr->num_ids = le32toh(idtbl.xattr_ids);
	xr->num_id_blocks =
		(xr->num_ids * sizeof(sqfs_xattr_id_t)) / SQFS_META_BLOCK_SIZE;

	if ((xr->num_ids * sizeof(sqfs_xattr_id_t)) % SQFS_META_BLOCK_SIZE)
		xr->num_id_blocks += 1;

	xr->id_block_starts = alloc_array(sizeof(sqfs_u64), xr->num_id_blocks);
	if (xr->id_block_starts == NULL) {
		if (errno == EOVERFLOW)
			return SQFS_ERROR_OVERFLOW;
		return SQFS_ERROR_ALLOC;
	}

	err = file->read_at(file, super->xattr_id_table_start + sizeof(idtbl),
			    xr->id_block_starts,
			    sizeof(sqfs_u64) * xr->num_id_blocks);
	if (err)
		goto fail_blocks;

	for (i = 0; i < xr->num_id_blocks; ++i) {
		xr->id_block_starts[i] = le64toh(xr->id_block_starts[i]);

		if (xr->id_block_starts[i] > super->bytes_used) {
			err = SQFS_ERROR_OUT_OF_BOUNDS;
			goto fail_blocks;
		}
	}

	/* create the meta data readers */
	xr->idrd = sqfs_meta_reader_create(file, cmp, super->id_table_start,
					   super->bytes_used);
	if (xr->idrd == NULL)
		goto fail_blocks;

	xr->kvrd = sqfs_meta_reader_create(file, cmp, super->id_table_start,
					   super->bytes_used);
	if (xr->kvrd == NULL)
		goto fail_idrd;

	xr->xattr_end = super->bytes_used;
	return 0;
fail_idrd:
	xr->idrd = sqfs_drop(xr->idrd);
fail_blocks:
	free(xr->id_block_starts);
	xr->id_block_starts = NULL;
	return err;
}

static int read_key_hdr(sqfs_xattr_reader_t *xr, sqfs_xattr_entry_t *key,
			const char **prefix)
{
	int ret;

	ret = sqfs_meta_reader_read(xr->kvrd, key, sizeof(*key));
	if (ret)
		return ret;

	key->type = le16toh(key->type);
	key->size = le16toh(key->size);

	*prefix = sqfs_get_xattr_prefix(key->type & SQFS_XATTR_PREFIX_MASK);
	if ((*prefix) == NULL)
		return SQFS_ERROR_UNSUPPORTED;

	return 0;
}

static int read_value_hdr(sqfs_xattr_reader_t *xr,
			  const sqfs_xattr_entry_t *key, sqfs_u64 *start,
			  size_t *offset, sqfs_xattr_value_t *value)
{
	sqfs_u64 ref, new_start;
	size_t new_offset;
	int ret;

	ret = sqfs_meta_reader_read(xr->kvrd, value, sizeof(*value));
	if (ret)
		return ret;

	if (key->type & SQFS_XATTR_FLAG_OOL) {
		ret = sqfs_meta_reader_read(xr->kvrd, &ref, sizeof(ref));
		if (ret)
			return ret;

		ref = le64toh(ref);
		new_start = xr->xattr_start + (ref >> 16);
		new_offset = ref & 0xFFFF;

		if (new_start >= xr->xattr_end ||
		    new_offset >= SQFS_META_BLOCK_SIZE) {
			return SQFS_ERROR_OUT_OF_BOUNDS;
		}

		sqfs_meta_reader_get_position(xr->kvrd, start, offset);

		ret = sqfs_meta_reader_seek(xr->kvrd, new_start, new_offset);
		if (ret)
			return ret;

		ret = sqfs_meta_reader_read(xr->kvrd, value, sizeof(*value));
		if (ret)
			return ret;
	}

	value->size = le32toh(value->size);
	return 0;
}

int sqfs_xattr_reader_read_key(sqfs_xattr_reader_t *xr,
			       sqfs_xattr_entry_t **key_out)
{
	sqfs_xattr_entry_t key, *out;
	const char *prefix;
	size_t plen, total;
	int ret;

	ret = read_key_hdr(xr, &key, &prefix);
	if (ret)
		return ret;

	plen = strlen(prefix);

	if (SZ_ADD_OV(plen, key.size, &total) || SZ_ADD_OV(total, 1, &total) ||
	    SZ_ADD_OV(sizeof(*out), total, &total)) {
		return SQFS_ERROR_OVERFLOW;
	}

	out = calloc(1, total);
	if (out == NULL)
		return SQFS_ERROR_ALLOC;

	*out = key;
	memcpy(out->key, prefix, plen);

	ret = sqfs_meta_reader_read(xr->kvrd, out->key + plen, key.size);
	if (ret) {
		free(out);
		return ret;
	}

	*key_out = out;
	return 0;
}

int sqfs_xattr_reader_read_value(sqfs_xattr_reader_t *xr,
				 const sqfs_xattr_entry_t *key,
				 sqfs_xattr_value_t **val_out)
{
	sqfs_xattr_value_t value, *out = NULL;
	size_t size, offset = 0;
	sqfs_u64 start = 0;
	int ret;

	ret = read_value_hdr(xr, key, &start, &offset, &value);
	if (ret)
		return ret;

	size = sizeof(*out) + 1;
	if (SZ_ADD_OV(size, value.size, &size))
		return SQFS_ERROR_OVERFLOW;

	out = calloc(1, size);
	if (out == NULL)
		return SQFS_ERROR_ALLOC;

	*out = value;

	ret = sqfs_meta_reader_read(xr->kvrd, out->value, value.size);
	if (ret)
		goto fail;

	if (key->type & SQFS_XATTR_FLAG_OOL) {
		ret = sqfs_meta_reader_seek(xr->kvrd, start, offset);
		if (ret)
			goto fail;
	}

	*val_out = out;
	return 0;
fail:
	free(out);
	return ret;
}

int sqfs_xattr_reader_read(sqfs_xattr_reader_t *xr, sqfs_xattr_t **out)
{
	sqfs_xattr_t *kv = NULL, *new = NULL;
	size_t plen, total = 0, offset = 0;
	sqfs_xattr_value_t value;
	sqfs_xattr_entry_t key;
	const char *prefix;
	sqfs_u64 start = 0;
	int ret;

	/* read and decode the key */
	ret = read_key_hdr(xr, &key, &prefix);
	if (ret)
		return ret;

	plen = strlen(prefix);
	total = sizeof(*kv) + plen + 1;

	if (SZ_ADD_OV(total, key.size, &total))
		return SQFS_ERROR_OVERFLOW;

	kv = calloc(1, total);
	if (kv == NULL)
		return SQFS_ERROR_ALLOC;

	memcpy(kv->data, prefix, plen);
	ret = sqfs_meta_reader_read(xr->kvrd, kv->data + plen, key.size);
	if (ret)
		goto fail;

	/* read and decode the value */
	ret = read_value_hdr(xr, &key, &start, &offset, &value);
	if (ret)
		goto fail;

	ret = SQFS_ERROR_OVERFLOW;
	if (SZ_ADD_OV(total, value.size, &total) || SZ_ADD_OV(total, 1, &total))
		goto fail;

	ret = SQFS_ERROR_ALLOC;
	new = realloc(kv, total);
	if (new == NULL)
		goto fail;
	kv = new;

	ret = sqfs_meta_reader_read(xr->kvrd, kv->data + plen + key.size + 1,
				    value.size);
	if (ret)
		goto fail;

	if (key.type & SQFS_XATTR_FLAG_OOL) {
		ret = sqfs_meta_reader_seek(xr->kvrd, start, offset);
		if (ret)
			goto fail;
	}

	/* prefix with the kv struct */
	kv->value_len = value.size;
	kv->key = (const char *)kv->data;
	kv->value = kv->data + plen + key.size + 1;
	kv->data[plen + key.size + 1 + value.size] = '\0';
	*out = kv;
	return 0;
fail:
	free(kv);
	return ret;
}

int sqfs_xattr_reader_seek_kv(sqfs_xattr_reader_t *xr,
			      const sqfs_xattr_id_t *desc)
{
	sqfs_u32 offset = desc->xattr & 0xFFFF;
	sqfs_u64 block = xr->xattr_start + (desc->xattr >> 16);

	return sqfs_meta_reader_seek(xr->kvrd, block, offset);
}

int sqfs_xattr_reader_get_desc(sqfs_xattr_reader_t *xr, sqfs_u32 idx,
			       sqfs_xattr_id_t *desc)
{
	size_t block, offset;
	int ret;

	memset(desc, 0, sizeof(*desc));

	if (idx == 0xFFFFFFFF)
		return 0;

	if (xr->kvrd == NULL || xr->idrd == NULL)
		return idx == 0 ? 0 : SQFS_ERROR_OUT_OF_BOUNDS;

	if (idx >= xr->num_ids)
		return SQFS_ERROR_OUT_OF_BOUNDS;

	offset = (idx * sizeof(*desc)) % SQFS_META_BLOCK_SIZE;
	block = (idx * sizeof(*desc)) / SQFS_META_BLOCK_SIZE;

	ret = sqfs_meta_reader_seek(xr->idrd, xr->id_block_starts[block],
				    offset);
	if (ret)
		return ret;

	ret = sqfs_meta_reader_read(xr->idrd, desc, sizeof(*desc));
	if (ret)
		return ret;

	desc->xattr = le64toh(desc->xattr);
	desc->count = le32toh(desc->count);
	desc->size = le32toh(desc->size);
	return 0;
}

int sqfs_xattr_reader_read_all(sqfs_xattr_reader_t *xr, sqfs_u32 idx,
			       sqfs_xattr_t **out)
{
	sqfs_xattr_t *head = NULL, *tail = NULL;
	sqfs_xattr_id_t desc;
	int ret;

	*out = NULL;
	if (idx == 0xFFFFFFFF)
		return 0;

	ret = sqfs_xattr_reader_get_desc(xr, idx, &desc);
	if (ret)
		return ret;

	ret = sqfs_xattr_reader_seek_kv(xr, &desc);
	if (ret)
		return ret;

	for (size_t i = 0; i < desc.count; ++i) {
		sqfs_xattr_t *ent;

		ret = sqfs_xattr_reader_read(xr, &ent);
		if (ret)
			goto fail;

		if (tail == NULL) {
			head = ent;
			tail = ent;
		} else {
			tail->next = ent;
			tail = ent;
		}
	}

	*out = head;
	return 0;
fail:
	sqfs_xattr_list_free(head);
	return ret;
}

sqfs_xattr_reader_t *sqfs_xattr_reader_create(sqfs_u32 flags)
{
	sqfs_xattr_reader_t *xr;

	if (flags != 0)
		return NULL;

	xr = calloc(1, sizeof(*xr));
	if (xr == NULL)
		return NULL;

	sqfs_object_init(xr, xattr_reader_destroy, xattr_reader_copy);
	return xr;
}