forked from devttys0/sasquatch
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathread_xattrs.c
390 lines (321 loc) · 10.1 KB
/
read_xattrs.c
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
/*
* Read a squashfs filesystem. This is a highly compressed read only
* filesystem.
*
* Copyright (c) 2010, 2012, 2013
* Phillip Lougher <phillip@squashfs.org.uk>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2,
* or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* read_xattrs.c
*/
/*
* Common xattr read code shared between mksquashfs and unsquashfs
*/
#define TRUE 1
#define FALSE 0
#include <stdio.h>
#include <string.h>
#ifndef linux
#define __BYTE_ORDER BYTE_ORDER
#define __BIG_ENDIAN BIG_ENDIAN
#define __LITTLE_ENDIAN LITTLE_ENDIAN
#else
#include <endian.h>
#endif
#include "squashfs_fs.h"
#include "squashfs_swap.h"
#include "xattr.h"
#include "error.h"
#include <stdlib.h>
extern int read_fs_bytes(int, long long, int, void *);
extern int read_block(int, long long, long long *, int, void *);
static struct hash_entry {
long long start;
unsigned int offset;
struct hash_entry *next;
} *hash_table[65536];
static struct squashfs_xattr_id *xattr_ids;
static void *xattrs = NULL;
static long long xattr_table_start;
/*
* Prefix lookup table, storing mapping to/from prefix string and prefix id
*/
struct prefix prefix_table[] = {
{ "user.", SQUASHFS_XATTR_USER },
{ "trusted.", SQUASHFS_XATTR_TRUSTED },
{ "security.", SQUASHFS_XATTR_SECURITY },
{ "", -1 }
};
/*
* store mapping from location of compressed block in fs ->
* location of uncompressed block in memory
*/
static void save_xattr_block(long long start, int offset)
{
struct hash_entry *hash_entry = malloc(sizeof(*hash_entry));
int hash = start & 0xffff;
TRACE("save_xattr_block: start %lld, offset %d\n", start, offset);
if(hash_entry == NULL)
MEM_ERROR();
hash_entry->start = start;
hash_entry->offset = offset;
hash_entry->next = hash_table[hash];
hash_table[hash] = hash_entry;
}
/*
* map from location of compressed block in fs ->
* location of uncompressed block in memory
*/
static int get_xattr_block(long long start)
{
int hash = start & 0xffff;
struct hash_entry *hash_entry = hash_table[hash];
for(; hash_entry; hash_entry = hash_entry->next)
if(hash_entry->start == start)
break;
TRACE("get_xattr_block: start %lld, offset %d\n", start,
hash_entry ? hash_entry->offset : -1);
return hash_entry ? hash_entry->offset : -1;
}
/*
* construct the xattr_list entry from the fs xattr, including
* mapping name and prefix into a full name
*/
static int read_xattr_entry(struct xattr_list *xattr,
struct squashfs_xattr_entry *entry, void *name)
{
int i, len, type = entry->type & XATTR_PREFIX_MASK;
for(i = 0; prefix_table[i].type != -1; i++)
if(prefix_table[i].type == type)
break;
if(prefix_table[i].type == -1) {
ERROR("Unrecognised type in read_xattr_entry\n");
return 0;
}
len = strlen(prefix_table[i].prefix);
xattr->full_name = malloc(len + entry->size + 1);
if(xattr->full_name == NULL)
MEM_ERROR();
memcpy(xattr->full_name, prefix_table[i].prefix, len);
memcpy(xattr->full_name + len, name, entry->size);
xattr->full_name[len + entry->size] = '\0';
xattr->name = xattr->full_name + len;
xattr->size = entry->size;
xattr->type = type;
return 1;
}
/*
* Read and decompress the xattr id table and the xattr metadata.
* This is cached in memory for later use by get_xattr()
*/
int read_xattrs_from_disk(int fd, struct squashfs_super_block *sBlk)
{
int res, bytes, i, indexes, index_bytes, ids;
long long *index, start, end;
struct squashfs_xattr_table id_table;
TRACE("read_xattrs_from_disk\n");
if(sBlk->xattr_id_table_start == SQUASHFS_INVALID_BLK)
return SQUASHFS_INVALID_BLK;
/*
* Read xattr id table, containing start of xattr metadata and the
* number of xattrs in the file system
*/
res = read_fs_bytes(fd, sBlk->xattr_id_table_start, sizeof(id_table),
&id_table);
if(res == 0)
return 0;
SQUASHFS_INSWAP_XATTR_TABLE(&id_table);
/*
* Allocate and read the index to the xattr id table metadata
* blocks
*/
ids = id_table.xattr_ids;
xattr_table_start = id_table.xattr_table_start;
index_bytes = SQUASHFS_XATTR_BLOCK_BYTES(ids);
indexes = SQUASHFS_XATTR_BLOCKS(ids);
index = malloc(index_bytes);
if(index == NULL)
MEM_ERROR();
res = read_fs_bytes(fd, sBlk->xattr_id_table_start + sizeof(id_table),
index_bytes, index);
if(res ==0)
goto failed1;
SQUASHFS_INSWAP_LONG_LONGS(index, indexes);
/*
* Allocate enough space for the uncompressed xattr id table, and
* read and decompress it
*/
bytes = SQUASHFS_XATTR_BYTES(ids);
xattr_ids = malloc(bytes);
if(xattr_ids == NULL)
MEM_ERROR();
for(i = 0; i < indexes; i++) {
int expected = (i + 1) != indexes ? SQUASHFS_METADATA_SIZE :
bytes & (SQUASHFS_METADATA_SIZE - 1);
int length = read_block(fd, index[i], NULL, expected,
((unsigned char *) xattr_ids) +
(i * SQUASHFS_METADATA_SIZE));
TRACE("Read xattr id table block %d, from 0x%llx, length "
"%d\n", i, index[i], length);
if(length == 0) {
ERROR("Failed to read xattr id table block %d, "
"from 0x%llx, length %d\n", i, index[i],
length);
goto failed2;
}
}
/*
* Read and decompress the xattr metadata
*
* Note the first xattr id table metadata block is immediately after
* the last xattr metadata block, so we can use index[0] to work out
* the end of the xattr metadata
*/
start = xattr_table_start;
end = index[0];
for(i = 0; start < end; i++) {
int length;
xattrs = realloc(xattrs, (i + 1) * SQUASHFS_METADATA_SIZE);
if(xattrs == NULL)
MEM_ERROR();
/* store mapping from location of compressed block in fs ->
* location of uncompressed block in memory */
save_xattr_block(start, i * SQUASHFS_METADATA_SIZE);
length = read_block(fd, start, &start, 0,
((unsigned char *) xattrs) +
(i * SQUASHFS_METADATA_SIZE));
TRACE("Read xattr block %d, length %d\n", i, length);
if(length == 0) {
ERROR("Failed to read xattr block %d\n", i);
goto failed3;
}
/*
* If this is not the last metadata block in the xattr metadata
* then it should be SQUASHFS_METADATA_SIZE in size.
* Note, we can't use expected in read_block() above for this
* because we don't know if this is the last block until
* after reading.
*/
if(start != end && length != SQUASHFS_METADATA_SIZE) {
ERROR("Xattr block %d should be %d bytes in length, "
"it is %d bytes\n", i, SQUASHFS_METADATA_SIZE,
length);
goto failed3;
}
}
/* swap if necessary the xattr id entries */
for(i = 0; i < ids; i++)
SQUASHFS_INSWAP_XATTR_ID(&xattr_ids[i]);
free(index);
return ids;
failed3:
free(xattrs);
failed2:
free(xattr_ids);
failed1:
free(index);
return 0;
}
void free_xattr(struct xattr_list *xattr_list, int count)
{
int i;
for(i = 0; i < count; i++)
free(xattr_list[i].full_name);
free(xattr_list);
}
/*
* Construct and return the list of xattr name:value pairs for the passed xattr
* id
*
* There are two users for get_xattr(), Mksquashfs uses it to read the
* xattrs from the filesystem on appending, and Unsquashfs uses it
* to retrieve the xattrs for writing to disk.
*
* Unfortunately, the two users disagree on what to do with unknown
* xattr prefixes, Mksquashfs wants to treat this as fatal otherwise
* this will cause xattrs to be be lost on appending. Unsquashfs
* on the otherhand wants to retrieve the xattrs which are known and
* to ignore the rest, this allows Unsquashfs to cope more gracefully
* with future versions which may have unknown xattrs, as long as the
* general xattr structure is adhered to, Unsquashfs should be able
* to safely ignore unknown xattrs, and to write the ones it knows about,
* this is better than completely refusing to retrieve all the xattrs.
*
* If ignore is TRUE then don't treat unknown xattr prefixes as
* a failure to read the xattr.
*/
struct xattr_list *get_xattr(int i, unsigned int *count, int ignore)
{
long long start;
struct xattr_list *xattr_list = NULL;
unsigned int offset;
void *xptr;
int j = 0, res = 1;
TRACE("get_xattr\n");
*count = xattr_ids[i].count;
start = SQUASHFS_XATTR_BLK(xattr_ids[i].xattr) + xattr_table_start;
offset = SQUASHFS_XATTR_OFFSET(xattr_ids[i].xattr);
xptr = xattrs + get_xattr_block(start) + offset;
TRACE("get_xattr: xattr_id %d, count %d, start %lld, offset %d\n", i,
*count, start, offset);
while(j < *count) {
struct squashfs_xattr_entry entry;
struct squashfs_xattr_val val;
if(res != 0) {
xattr_list = realloc(xattr_list, (j + 1) *
sizeof(struct xattr_list));
if(xattr_list == NULL)
MEM_ERROR();
}
SQUASHFS_SWAP_XATTR_ENTRY(xptr, &entry);
xptr += sizeof(entry);
res = read_xattr_entry(&xattr_list[j], &entry, xptr);
if(ignore && res == 0) {
/* unknown prefix, but ignore flag is set */
(*count) --;
continue;
}
if(res != 1)
goto failed;
xptr += entry.size;
TRACE("get_xattr: xattr %d, type %d, size %d, name %s\n", j,
entry.type, entry.size, xattr_list[j].full_name);
if(entry.type & SQUASHFS_XATTR_VALUE_OOL) {
long long xattr;
void *ool_xptr;
xptr += sizeof(val);
SQUASHFS_SWAP_LONG_LONGS(xptr, &xattr, 1);
xptr += sizeof(xattr);
start = SQUASHFS_XATTR_BLK(xattr) + xattr_table_start;
offset = SQUASHFS_XATTR_OFFSET(xattr);
ool_xptr = xattrs + get_xattr_block(start) + offset;
SQUASHFS_SWAP_XATTR_VAL(ool_xptr, &val);
xattr_list[j].value = ool_xptr + sizeof(val);
} else {
SQUASHFS_SWAP_XATTR_VAL(xptr, &val);
xattr_list[j].value = xptr + sizeof(val);
xptr += sizeof(val) + val.vsize;
}
TRACE("get_xattr: xattr %d, vsize %d\n", j, val.vsize);
xattr_list[j ++].vsize = val.vsize;
}
if(*count == 0)
goto failed;
return xattr_list;
failed:
free_xattr(xattr_list, j);
return NULL;
}