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
|
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
* xattr_writer.c
*
* Copyright (C) 2021 David Oberhollenzer <goliath@infraroot.at>
*/
#include "config.h"
#include "compat.h"
#include "util/test.h"
#include "sqfs/xattr_writer.h"
#include "sqfs/compressor.h"
#include "sqfs/xattr.h"
#include "sqfs/error.h"
#include "sqfs/super.h"
#include "sqfs/io.h"
static sqfs_u8 file_data[1024];
static size_t file_used = 0;
static int dummy_write_at(sqfs_file_t *file, sqfs_u64 offset,
const void *buffer, size_t size)
{
(void)file;
if (offset >= sizeof(file_data))
return SQFS_ERROR_OUT_OF_BOUNDS;
if (size > (sizeof(file_data) - offset))
return SQFS_ERROR_OUT_OF_BOUNDS;
if (offset > file_used)
memset(file_data + file_used, 0, offset - file_used);
if ((offset + size) > file_used)
file_used = offset + size;
memcpy(file_data + offset, buffer, size);
return 0;
}
static sqfs_u64 dummy_get_size(const sqfs_file_t *file)
{
(void)file;
return file_used;
}
static sqfs_s32 dummy_compress(sqfs_compressor_t *cmp, const sqfs_u8 *in,
sqfs_u32 size, sqfs_u8 *out, sqfs_u32 outsize)
{
(void)cmp;
memcpy(out, in, outsize < size ? outsize : size);
return 0;
}
static sqfs_file_t dummy_file = {
{ NULL, NULL },
NULL,
dummy_write_at,
dummy_get_size,
NULL,
};
static sqfs_compressor_t dummy_compressor = {
{ NULL, NULL },
NULL,
NULL,
NULL,
dummy_compress,
};
/*****************************************************************************/
int main(int argc, char **argv)
{
size_t offset, ool_value_offset, id_offset;
sqfs_xattr_id_table_t idtbl;
sqfs_xattr_writer_t *xwr;
sqfs_xattr_value_t value;
sqfs_xattr_entry_t key;
sqfs_xattr_id_t desc;
sqfs_super_t super;
char strbuf[32];
sqfs_u16 hdr;
sqfs_u64 ref;
sqfs_u32 id;
int ret;
(void)argc; (void)argv;
/* setup */
xwr = sqfs_xattr_writer_create(0);
TEST_NOT_NULL(xwr);
/* record a block of key/value pairs */
ret = sqfs_xattr_writer_begin(xwr, 0);
TEST_EQUAL_I(ret, 0);
ret = sqfs_xattr_writer_add(xwr, "user.foobar", "test", 4);
TEST_EQUAL_I(ret, 0);
ret = sqfs_xattr_writer_add(xwr, "security.selinux", "Xwhatever", 9);
TEST_EQUAL_I(ret, 0);
ret = sqfs_xattr_writer_end(xwr, &id);
TEST_EQUAL_I(ret, 0);
TEST_EQUAL_UI(id, 0);
/* record a second, different block */
ret = sqfs_xattr_writer_begin(xwr, 0);
TEST_EQUAL_I(ret, 0);
ret = sqfs_xattr_writer_add(xwr, "user.foobar", "bla", 3);
TEST_EQUAL_I(ret, 0);
ret = sqfs_xattr_writer_add(xwr, "security.selinux", "blub", 4);
TEST_EQUAL_I(ret, 0);
ret = sqfs_xattr_writer_end(xwr, &id);
TEST_EQUAL_I(ret, 0);
TEST_EQUAL_UI(id, 1);
/* same as first block after sorting and gets the same ID */
ret = sqfs_xattr_writer_begin(xwr, 0);
TEST_EQUAL_I(ret, 0);
ret = sqfs_xattr_writer_add(xwr, "security.selinux", "Xwhatever", 9);
TEST_EQUAL_I(ret, 0);
ret = sqfs_xattr_writer_add(xwr, "user.foobar", "test", 4);
TEST_EQUAL_I(ret, 0);
ret = sqfs_xattr_writer_end(xwr, &id);
TEST_EQUAL_I(ret, 0);
TEST_EQUAL_UI(id, 0);
/* the third assignment overwrites the first, making
the block identical to the second one */
ret = sqfs_xattr_writer_begin(xwr, 0);
TEST_EQUAL_I(ret, 0);
ret = sqfs_xattr_writer_add(xwr, "user.foobar", "mimimi", 6);
TEST_EQUAL_I(ret, 0);
ret = sqfs_xattr_writer_add(xwr, "security.selinux", "blub", 4);
TEST_EQUAL_I(ret, 0);
ret = sqfs_xattr_writer_add(xwr, "user.foobar", "bla", 3);
TEST_EQUAL_I(ret, 0);
ret = sqfs_xattr_writer_end(xwr, &id);
TEST_EQUAL_I(ret, 0);
TEST_EQUAL_UI(id, 1);
/* add another block with the same value, so it gets stored OOL */
ret = sqfs_xattr_writer_begin(xwr, 0);
TEST_EQUAL_I(ret, 0);
ret = sqfs_xattr_writer_add(xwr, "security.selinux", "Xwhatever", 9);
TEST_EQUAL_I(ret, 0);
ret = sqfs_xattr_writer_end(xwr, &id);
TEST_EQUAL_I(ret, 0);
TEST_EQUAL_UI(id, 2);
/* serialize */
sqfs_super_init(&super, 131072, 0, SQFS_COMP_GZIP);
ret = sqfs_xattr_writer_flush(xwr, &dummy_file, &super,
&dummy_compressor);
TEST_EQUAL_I(ret, 0);
TEST_EQUAL_UI(file_used, 177);
/* meta data block holding the key-value-pairs */
memcpy(&hdr, file_data, sizeof(hdr));
hdr = le16toh(hdr);
TEST_EQUAL_UI(hdr, (0x8000 | 101));
offset = 2;
memcpy(&key, file_data + offset, sizeof(key));
key.type = le16toh(key.type);
key.size = le16toh(key.size);
offset += sizeof(key);
TEST_EQUAL_UI(key.type, SQFS_XATTR_USER);
TEST_EQUAL_UI(key.size, 6);
memset(strbuf, '\0', sizeof(strbuf));
memcpy(strbuf, file_data + offset, key.size);
TEST_STR_EQUAL(strbuf, "foobar");
offset += key.size;
memcpy(&value, file_data + offset, sizeof(value));
value.size = le32toh(value.size);
offset += sizeof(value);
TEST_EQUAL_UI(value.size, 4);
memset(strbuf, '\0', sizeof(strbuf));
memcpy(strbuf, file_data + offset, value.size);
TEST_STR_EQUAL(strbuf, "test");
offset += value.size;
memcpy(&key, file_data + offset, sizeof(key));
key.type = le16toh(key.type);
key.size = le16toh(key.size);
offset += sizeof(key);
TEST_EQUAL_UI(key.type, SQFS_XATTR_SECURITY);
TEST_EQUAL_UI(key.size, 7);
memset(strbuf, '\0', sizeof(strbuf));
memcpy(strbuf, file_data + offset, key.size);
TEST_STR_EQUAL(strbuf, "selinux");
offset += key.size;
ool_value_offset = offset;
memcpy(&value, file_data + offset, sizeof(value));
value.size = le32toh(value.size);
offset += sizeof(value);
TEST_EQUAL_UI(value.size, 9);
memset(strbuf, '\0', sizeof(strbuf));
memcpy(strbuf, file_data + offset, value.size);
TEST_STR_EQUAL(strbuf, "Xwhatever");
offset += value.size;
memcpy(&key, file_data + offset, sizeof(key));
key.type = le16toh(key.type);
key.size = le16toh(key.size);
offset += sizeof(key);
TEST_EQUAL_UI(key.type, SQFS_XATTR_USER);
TEST_EQUAL_UI(key.size, 6);
memset(strbuf, '\0', sizeof(strbuf));
memcpy(strbuf, file_data + offset, key.size);
TEST_STR_EQUAL(strbuf, "foobar");
offset += key.size;
memcpy(&value, file_data + offset, sizeof(value));
value.size = le32toh(value.size);
offset += sizeof(value);
TEST_EQUAL_UI(value.size, 3);
memset(strbuf, '\0', sizeof(strbuf));
memcpy(strbuf, file_data + offset, value.size);
TEST_STR_EQUAL(strbuf, "bla");
offset += value.size;
memcpy(&key, file_data + offset, sizeof(key));
key.type = le16toh(key.type);
key.size = le16toh(key.size);
offset += sizeof(key);
TEST_EQUAL_UI(key.type, SQFS_XATTR_SECURITY);
TEST_EQUAL_UI(key.size, 7);
memset(strbuf, '\0', sizeof(strbuf));
memcpy(strbuf, file_data + offset, key.size);
TEST_STR_EQUAL(strbuf, "selinux");
offset += key.size;
memcpy(&value, file_data + offset, sizeof(value));
value.size = le32toh(value.size);
offset += sizeof(value);
TEST_EQUAL_UI(value.size, 4);
memset(strbuf, '\0', sizeof(strbuf));
memcpy(strbuf, file_data + offset, value.size);
TEST_STR_EQUAL(strbuf, "blub");
offset += value.size;
memcpy(&key, file_data + offset, sizeof(key));
key.type = le16toh(key.type);
key.size = le16toh(key.size);
offset += sizeof(key);
TEST_EQUAL_UI(key.type, (SQFS_XATTR_SECURITY | SQFS_XATTR_FLAG_OOL));
TEST_EQUAL_UI(key.size, 7);
memset(strbuf, '\0', sizeof(strbuf));
memcpy(strbuf, file_data + offset, key.size);
TEST_STR_EQUAL(strbuf, "selinux");
offset += key.size;
memcpy(&value, file_data + offset, sizeof(value));
value.size = le32toh(value.size);
offset += sizeof(value);
TEST_EQUAL_UI(value.size, 8);
memcpy(&ref, file_data + offset, sizeof(ref));
ref = le64toh(ref);
TEST_EQUAL_UI(ref, (ool_value_offset - 2));
offset += value.size;
/* meta data block holding the ID descriptions */
id_offset = offset;
memcpy(&hdr, file_data + offset, sizeof(hdr));
TEST_EQUAL_UI(le16toh(hdr), (0x8000 | (16 * 3)));
offset += sizeof(hdr);
memcpy(&desc, file_data + offset, sizeof(desc));
TEST_EQUAL_UI(le64toh(desc.xattr), 0);
TEST_EQUAL_UI(le32toh(desc.count), 2);
TEST_EQUAL_UI(le32toh(desc.size), 42);
offset += sizeof(desc);
memcpy(&desc, file_data + offset, sizeof(desc));
TEST_EQUAL_UI(le64toh(desc.xattr), 42);
TEST_EQUAL_UI(le32toh(desc.count), 2);
TEST_EQUAL_UI(le32toh(desc.size), 36);
offset += sizeof(desc);
memcpy(&desc, file_data + offset, sizeof(desc));
TEST_EQUAL_UI(le64toh(desc.xattr), 78);
TEST_EQUAL_UI(le32toh(desc.count), 1);
TEST_EQUAL_UI(le32toh(desc.size), 23);
offset += sizeof(desc);
/* the xattr table itself */
TEST_EQUAL_UI(super.xattr_id_table_start, offset);
memcpy(&idtbl, file_data + offset, sizeof(idtbl));
TEST_EQUAL_UI(le64toh(idtbl.xattr_table_start), 0);
TEST_EQUAL_UI(le32toh(idtbl.xattr_ids), 3);
offset += sizeof(idtbl);
memcpy(&ref, file_data + offset, sizeof(ref));
TEST_EQUAL_UI(le64toh(ref), id_offset);
offset += sizeof(ref);
TEST_EQUAL_UI(offset, file_used);
/* cleanup */
sqfs_destroy(xwr);
return EXIT_SUCCESS;
}
|