aboutsummaryrefslogtreecommitdiff
path: root/lib/sqfs/read_inode.c
blob: 12bef482eb51e856ffc3469afa80fe84c45a5353 (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
/* SPDX-License-Identifier: LGPL-3.0-or-later */
/*
 * read_inode.c
 *
 * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
 */
#define SQFS_BUILDING_DLL
#include "config.h"

#include "sqfs/meta_reader.h"
#include "sqfs/error.h"
#include "sqfs/super.h"
#include "sqfs/inode.h"
#include "sqfs/dir.h"
#include "util/util.h"

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

#define SWAB16(x) x = le16toh(x)
#define SWAB32(x) x = le32toh(x)
#define SWAB64(x) x = le64toh(x)

static int set_mode(sqfs_inode_t *inode)
{
	inode->mode &= ~S_IFMT;

	switch (inode->type) {
	case SQFS_INODE_SOCKET:
	case SQFS_INODE_EXT_SOCKET:
		inode->mode |= S_IFSOCK;
		break;
	case SQFS_INODE_SLINK:
	case SQFS_INODE_EXT_SLINK:
		inode->mode |= S_IFLNK;
		break;
	case SQFS_INODE_FILE:
	case SQFS_INODE_EXT_FILE:
		inode->mode |= S_IFREG;
		break;
	case SQFS_INODE_BDEV:
	case SQFS_INODE_EXT_BDEV:
		inode->mode |= S_IFBLK;
		break;
	case SQFS_INODE_DIR:
	case SQFS_INODE_EXT_DIR:
		inode->mode |= S_IFDIR;
		break;
	case SQFS_INODE_CDEV:
	case SQFS_INODE_EXT_CDEV:
		inode->mode |= S_IFCHR;
		break;
	case SQFS_INODE_FIFO:
	case SQFS_INODE_EXT_FIFO:
		inode->mode |= S_IFIFO;
		break;
	default:
		return SQFS_ERROR_UNSUPPORTED;
	}

	return 0;
}

static sqfs_u64 get_block_count(sqfs_u64 size, sqfs_u64 block_size,
				sqfs_u32 frag_index, sqfs_u32 frag_offset)
{
	sqfs_u64 count = size / block_size;

	if ((size % block_size) != 0 &&
	    (frag_index == 0xFFFFFFFF || frag_offset == 0xFFFFFFFF)) {
		++count;
	}

	return count;
}

static int read_inode_file(sqfs_meta_reader_t *ir, sqfs_inode_t *base,
			   size_t block_size, sqfs_inode_generic_t **result)
{
	sqfs_inode_generic_t *out;
	sqfs_inode_file_t file;
	sqfs_u64 i, count;
	int err;

	err = sqfs_meta_reader_read(ir, &file, sizeof(file));
	if (err)
		return err;

	SWAB32(file.blocks_start);
	SWAB32(file.fragment_index);
	SWAB32(file.fragment_offset);
	SWAB32(file.file_size);

	count = get_block_count(file.file_size, block_size,
				file.fragment_index, file.fragment_offset);

	out = alloc_flex(sizeof(*out), sizeof(sqfs_u32), count);
	if (out == NULL)
		return SQFS_ERROR_ALLOC;

	out->base = *base;
	out->data.file = file;
	out->payload_bytes_available = count * sizeof(sqfs_u32);
	out->payload_bytes_used = count * sizeof(sqfs_u32);

	err = sqfs_meta_reader_read(ir, out->extra, count * sizeof(sqfs_u32));
	if (err) {
		free(out);
		return err;
	}

	for (i = 0; i < count; ++i)
		SWAB32(out->extra[i]);

	*result = out;
	return 0;
}

static int read_inode_file_ext(sqfs_meta_reader_t *ir, sqfs_inode_t *base,
			       size_t block_size, sqfs_inode_generic_t **result)
{
	sqfs_inode_file_ext_t file;
	sqfs_inode_generic_t *out;
	sqfs_u64 i, count;
	int err;

	err = sqfs_meta_reader_read(ir, &file, sizeof(file));
	if (err)
		return err;

	SWAB64(file.blocks_start);
	SWAB64(file.file_size);
	SWAB64(file.sparse);
	SWAB32(file.nlink);
	SWAB32(file.fragment_idx);
	SWAB32(file.fragment_offset);
	SWAB32(file.xattr_idx);

	count = get_block_count(file.file_size, block_size,
				file.fragment_idx, file.fragment_offset);

	out = alloc_flex(sizeof(*out), sizeof(sqfs_u32), count);
	if (out == NULL) {
		return errno == EOVERFLOW ? SQFS_ERROR_OVERFLOW :
			SQFS_ERROR_ALLOC;
	}

	out->base = *base;
	out->data.file_ext = file;
	out->payload_bytes_available = count * sizeof(sqfs_u32);
	out->payload_bytes_used = count * sizeof(sqfs_u32);

	err = sqfs_meta_reader_read(ir, out->extra, count * sizeof(sqfs_u32));
	if (err) {
		free(out);
		return err;
	}

	for (i = 0; i < count; ++i)
		SWAB32(out->extra[i]);

	*result = out;
	return 0;
}

static int read_inode_slink(sqfs_meta_reader_t *ir, sqfs_inode_t *base,
			    sqfs_inode_generic_t **result)
{
	sqfs_inode_generic_t *out;
	sqfs_inode_slink_t slink;
	size_t size;
	int err;

	err = sqfs_meta_reader_read(ir, &slink, sizeof(slink));
	if (err)
		return err;

	SWAB32(slink.nlink);
	SWAB32(slink.target_size);

	if (SZ_ADD_OV(slink.target_size, 1, &size) ||
	    SZ_ADD_OV(sizeof(*out), size, &size)) {
		return SQFS_ERROR_OVERFLOW;
	}

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

	out->payload_bytes_available = size - sizeof(*out);
	out->payload_bytes_used = size - sizeof(*out) - 1;
	out->base = *base;
	out->data.slink = slink;

	err = sqfs_meta_reader_read(ir, (void *)out->extra, slink.target_size);
	if (err) {
		free(out);
		return err;
	}

	*result = out;
	return 0;
}

static int read_inode_slink_ext(sqfs_meta_reader_t *ir, sqfs_inode_t *base,
				sqfs_inode_generic_t **result)
{
	sqfs_u32 xattr;
	int err;

	err = read_inode_slink(ir, base, result);
	if (err)
		return err;

	err = sqfs_meta_reader_read(ir, &xattr, sizeof(xattr));
	if (err) {
		free(*result);
		return err;
	}

	(*result)->data.slink_ext.xattr_idx = le32toh(xattr);
	return 0;
}

static int read_inode_dir_ext(sqfs_meta_reader_t *ir, sqfs_inode_t *base,
			      sqfs_inode_generic_t **result)
{
	size_t i, new_sz, index_max, index_used;
	sqfs_inode_generic_t *out, *new;
	sqfs_inode_dir_ext_t dir;
	sqfs_dir_index_t ent;
	int err;

	err = sqfs_meta_reader_read(ir, &dir, sizeof(dir));
	if (err)
		return err;

	SWAB32(dir.nlink);
	SWAB32(dir.size);
	SWAB32(dir.start_block);
	SWAB32(dir.parent_inode);
	SWAB16(dir.inodex_count);
	SWAB16(dir.offset);
	SWAB32(dir.xattr_idx);

	index_max = dir.size ? 128 : 0;
	index_used = 0;

	out = alloc_flex(sizeof(*out), 1, index_max);
	if (out == NULL)
		return SQFS_ERROR_ALLOC;

	out->base = *base;
	out->data.dir_ext = dir;

	if (dir.size == 0) {
		*result = out;
		return 0;
	}

	for (i = 0; i < dir.inodex_count; ++i) {
		err = sqfs_meta_reader_read(ir, &ent, sizeof(ent));
		if (err) {
			free(out);
			return err;
		}

		SWAB32(ent.start_block);
		SWAB32(ent.index);
		SWAB32(ent.size);

		new_sz = index_max;
		while (sizeof(ent) + ent.size + 1 > new_sz - index_used) {
			if (SZ_MUL_OV(new_sz, 2, &new_sz)) {
				free(out);
				return SQFS_ERROR_OVERFLOW;
			}
		}

		if (new_sz > index_max) {
			new = realloc(out, sizeof(*out) + new_sz);
			if (new == NULL) {
				free(out);
				return SQFS_ERROR_ALLOC;
			}
			out = new;
			index_max = new_sz;
		}

		memcpy((char *)out->extra + index_used, &ent, sizeof(ent));
		index_used += sizeof(ent);

		err = sqfs_meta_reader_read(ir, (char *)out->extra + index_used,
					    ent.size + 1);
		if (err) {
			free(out);
			return err;
		}

		index_used += ent.size + 1;
	}

	out->payload_bytes_used = index_used;
	out->payload_bytes_available = index_used;
	*result = out;
	return 0;
}

int sqfs_meta_reader_read_inode(sqfs_meta_reader_t *ir,
				const sqfs_super_t *super,
				sqfs_u64 block_start, size_t offset,
				sqfs_inode_generic_t **result)
{
	sqfs_inode_generic_t *out;
	sqfs_inode_t inode;
	int err;

	/* read base inode */
	block_start += super->inode_table_start;

	err = sqfs_meta_reader_seek(ir, block_start, offset);
	if (err)
		return err;

	err = sqfs_meta_reader_read(ir, &inode, sizeof(inode));
	if (err)
		return err;

	SWAB16(inode.type);
	SWAB16(inode.mode);
	SWAB16(inode.uid_idx);
	SWAB16(inode.gid_idx);
	SWAB32(inode.mod_time);
	SWAB32(inode.inode_number);

	err = set_mode(&inode);
	if (err)
		return err;

	/* inode types where the size is variable */
	switch (inode.type) {
	case SQFS_INODE_FILE:
		return read_inode_file(ir, &inode, super->block_size, result);
	case SQFS_INODE_SLINK:
		return read_inode_slink(ir, &inode, result);
	case SQFS_INODE_EXT_FILE:
		return read_inode_file_ext(ir, &inode, super->block_size,
					   result);
	case SQFS_INODE_EXT_SLINK:
		return read_inode_slink_ext(ir, &inode, result);
	case SQFS_INODE_EXT_DIR:
		return read_inode_dir_ext(ir, &inode, result);
	default:
		break;
	}

	/* everything else */
	out = calloc(1, sizeof(*out));
	if (out == NULL)
		return SQFS_ERROR_ALLOC;

	out->base = inode;

	switch (inode.type) {
	case SQFS_INODE_DIR:
		err = sqfs_meta_reader_read(ir, &out->data.dir,
					    sizeof(out->data.dir));
		if (err)
			goto fail_free;

		SWAB32(out->data.dir.start_block);
		SWAB32(out->data.dir.nlink);
		SWAB16(out->data.dir.size);
		SWAB16(out->data.dir.offset);
		SWAB32(out->data.dir.parent_inode);
		break;
	case SQFS_INODE_BDEV:
	case SQFS_INODE_CDEV:
		err = sqfs_meta_reader_read(ir, &out->data.dev,
					    sizeof(out->data.dev));
		if (err)
			goto fail_free;
		SWAB32(out->data.dev.nlink);
		SWAB32(out->data.dev.devno);
		break;
	case SQFS_INODE_FIFO:
	case SQFS_INODE_SOCKET:
		err = sqfs_meta_reader_read(ir, &out->data.ipc,
					    sizeof(out->data.ipc));
		if (err)
			goto fail_free;
		SWAB32(out->data.ipc.nlink);
		break;
	case SQFS_INODE_EXT_BDEV:
	case SQFS_INODE_EXT_CDEV:
		err = sqfs_meta_reader_read(ir, &out->data.dev_ext,
					    sizeof(out->data.dev_ext));
		if (err)
			goto fail_free;
		SWAB32(out->data.dev_ext.nlink);
		SWAB32(out->data.dev_ext.devno);
		SWAB32(out->data.dev_ext.xattr_idx);
		break;
	case SQFS_INODE_EXT_FIFO:
	case SQFS_INODE_EXT_SOCKET:
		err = sqfs_meta_reader_read(ir, &out->data.ipc_ext,
					    sizeof(out->data.ipc_ext));
		if (err)
			goto fail_free;
		SWAB32(out->data.ipc_ext.nlink);
		SWAB32(out->data.ipc_ext.xattr_idx);
		break;
	default:
		err = SQFS_ERROR_UNSUPPORTED;
		goto fail_free;
	}

	*result = out;
	return 0;
fail_free:
	free(out);
	return err;
}