aboutsummaryrefslogtreecommitdiff
path: root/lib/sqfs/read_inode.c
blob: 56ea6a42872ed30088d8e333152a34d0560c2e0e (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
/* SPDX-License-Identifier: GPL-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 "util.h"

#include <sys/stat.h>
#include <stdlib.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 uint64_t get_block_count(uint64_t size, uint64_t block_size,
				uint32_t frag_index, uint32_t frag_offset)
{
	uint64_t 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;
	uint64_t 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(uint32_t), count);
	if (out == NULL)
		return SQFS_ERROR_ALLOC;

	out->base = *base;
	out->data.file = file;
	out->block_sizes = (uint32_t *)out->extra;
	out->num_file_blocks = count;

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

	for (i = 0; i < count; ++i)
		SWAB32(out->block_sizes[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;
	uint64_t 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(uint32_t), count);
	if (out == NULL) {
		return errno == EOVERFLOW ? SQFS_ERROR_OVERFLOW :
			SQFS_ERROR_ALLOC;
	}

	out->base = *base;
	out->data.file_ext = file;
	out->block_sizes = (uint32_t *)out->extra;
	out->num_file_blocks = count;

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

	for (i = 0; i < count; ++i)
		SWAB32(out->block_sizes[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->slink_target = (char *)out->extra;
	out->base = *base;
	out->data.slink = slink;

	err = sqfs_meta_reader_read(ir, out->slink_target, 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)
{
	uint32_t 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;
}

int sqfs_meta_reader_read_inode(sqfs_meta_reader_t *ir, sqfs_super_t *super,
				uint64_t 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);
	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_DIR:
		err = sqfs_meta_reader_read(ir, &out->data.dir_ext,
					    sizeof(out->data.dir_ext));
		if (err)
			goto fail_free;
		SWAB32(out->data.dir_ext.nlink);
		SWAB32(out->data.dir_ext.size);
		SWAB32(out->data.dir_ext.start_block);
		SWAB32(out->data.dir_ext.parent_inode);
		SWAB16(out->data.dir_ext.inodex_count);
		SWAB16(out->data.dir_ext.offset);
		SWAB32(out->data.dir_ext.xattr_idx);
		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:
		return SQFS_ERROR_UNSUPPORTED;
	}

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