This repository has been archived by the owner on Jun 29, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathxattr.c
325 lines (284 loc) · 8.66 KB
/
xattr.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
// SPDX-License-Identifier: GPL-2.0
/*
* linux/fs/apfs/xattr.c
*
* Copyright (C) 2018 Ernesto A. Fernández <[email protected]>
*/
#include <linux/buffer_head.h>
#include <linux/xattr.h>
#include "apfs.h"
#include "btree.h"
#include "extents.h"
#include "key.h"
#include "super.h"
#include "node.h"
#include "message.h"
#include "xattr.h"
/**
* apfs_xattr_from_query - Read the xattr record found by a successful query
* @query: the query that found the record
* @xattr: Return parameter. The xattr record found.
*
* Reads the xattr record into @xattr and performs some basic sanity checks
* as a protection against crafted filesystems. Returns 0 on success or
* -EFSCORRUPTED otherwise.
*
* The caller must not free @query while @xattr is in use, because @xattr->name
* and @xattr->xdata point to data on disk.
*/
static int apfs_xattr_from_query(struct apfs_query *query,
struct apfs_xattr *xattr)
{
struct apfs_xattr_val *xattr_val;
struct apfs_xattr_key *xattr_key;
char *raw = query->node->object.bh->b_data;
int datalen = query->len - sizeof(*xattr_val);
int namelen = query->key_len - sizeof(*xattr_key);
if (namelen < 1 || datalen < 0)
return -EFSCORRUPTED;
xattr_val = (struct apfs_xattr_val *)(raw + query->off);
xattr_key = (struct apfs_xattr_key *)(raw + query->key_off);
if (namelen != le16_to_cpu(xattr_key->name_len))
return -EFSCORRUPTED;
/* The xattr name must be NULL-terminated */
if (xattr_key->name[namelen - 1] != 0)
return -EFSCORRUPTED;
xattr->has_dstream = le16_to_cpu(xattr_val->flags) &
APFS_XATTR_DATA_STREAM;
if (xattr->has_dstream && datalen != sizeof(struct apfs_xattr_dstream))
return -EFSCORRUPTED;
if (!xattr->has_dstream && datalen != le16_to_cpu(xattr_val->xdata_len))
return -EFSCORRUPTED;
xattr->name = xattr_key->name;
xattr->name_len = namelen - 1; /* Don't count the NULL termination */
xattr->xdata = xattr_val->xdata;
xattr->xdata_len = datalen;
return 0;
}
/**
* apfs_xattr_extents_read - Read the value of a xattr from its extents
* @parent: inode the attribute belongs to
* @xattr: the xattr structure
* @buffer: where to copy the attribute value
* @size: size of @buffer
*
* Copies the value of @xattr to @buffer, if provided. If @buffer is NULL, just
* computes the size of the buffer required.
*
* Returns the number of bytes used/required, or a negative error code in case
* of failure.
*/
static int apfs_xattr_extents_read(struct inode *parent,
struct apfs_xattr *xattr,
void *buffer, size_t size)
{
struct super_block *sb = parent->i_sb;
struct apfs_sb_info *sbi = APFS_SB(sb);
struct apfs_key key;
struct apfs_query *query;
struct apfs_xattr_dstream *xdata;
u64 extent_id;
int length;
int ret;
int i;
xdata = (struct apfs_xattr_dstream *) xattr->xdata;
length = le64_to_cpu(xdata->dstream.size);
if (length < 0 || length < le64_to_cpu(xdata->dstream.size))
return -E2BIG;
if (!buffer) /* All we want is the length */
return length;
if (length > size) /* xattr won't fit in the buffer */
return -ERANGE;
extent_id = le64_to_cpu(xdata->xattr_obj_id);
/* We will read all the extents, starting with the last one */
apfs_init_file_extent_key(extent_id, 0 /* offset */, &key);
query = apfs_alloc_query(sbi->s_cat_root, NULL /* parent */);
if (!query)
return -ENOMEM;
query->key = &key;
query->flags = APFS_QUERY_CAT | APFS_QUERY_MULTIPLE | APFS_QUERY_EXACT;
/*
* The logic in this loop would allow a crafted filesystem with a large
* number of redundant extents to become stuck for a long time. We use
* the xattr length to put a limit on the number of iterations.
*/
ret = -EFSCORRUPTED;
for (i = 0; i < (length >> parent->i_blkbits) + 2; i++) {
struct apfs_file_extent ext;
u64 block_count, file_off;
int err;
int j;
err = apfs_btree_query(sb, &query);
if (err == -ENODATA) { /* No more records to search */
ret = length;
goto done;
}
if (err) {
ret = err;
goto done;
}
err = apfs_extent_from_query(query, &ext);
if (err) {
apfs_alert(sb, "bad extent for xattr in inode 0x%llx",
(unsigned long long) parent->i_ino);
ret = err;
goto done;
}
block_count = ext.len >> sb->s_blocksize_bits;
file_off = ext.logical_addr;
for (j = 0; j < block_count; ++j) {
struct buffer_head *bh;
int bytes;
if (length <= file_off) /* Read the whole extent */
break;
bytes = min(sb->s_blocksize,
(unsigned long)(length - file_off));
bh = sb_bread(sb, ext.phys_block_num + j);
if (!bh) {
ret = -EIO;
goto done;
}
memcpy(buffer + file_off, bh->b_data, bytes);
brelse(bh);
file_off = file_off + bytes;
}
}
done:
apfs_free_query(sb, query);
return ret;
}
/**
* apfs_xattr_inline_read - Read the value of an inline xattr
* @parent: inode the attribute belongs to
* @xattr: the xattr structure
* @buffer: where to copy the attribute value
* @size: size of @buffer
*
* Copies the inline value of @xattr to @buffer, if provided. If @buffer is
* NULL, just computes the size of the buffer required.
*
* Returns the number of bytes used/required, or a negative error code in case
* of failure.
*/
static int apfs_xattr_inline_read(struct inode *parent,
struct apfs_xattr *xattr,
void *buffer, size_t size)
{
int length = xattr->xdata_len;
if (!buffer) /* All we want is the length */
return length;
if (length > size) /* xattr won't fit in the buffer */
return -ERANGE;
memcpy(buffer, xattr->xdata, length);
return length;
}
/**
* apfs_xattr_get - Find and read a named attribute
* @inode: inode the attribute belongs to
* @name: name of the attribute
* @buffer: where to copy the attribute value
* @size: size of @buffer
*
* Finds an extended attribute and copies its value to @buffer, if provided. If
* @buffer is NULL, just computes the size of the buffer required.
*
* Returns the number of bytes used/required, or a negative error code in case
* of failure.
*/
int apfs_xattr_get(struct inode *inode, const char *name, void *buffer,
size_t size)
{
struct super_block *sb = inode->i_sb;
struct apfs_sb_info *sbi = APFS_SB(sb);
struct apfs_key key;
struct apfs_query *query;
struct apfs_xattr xattr;
u64 cnid = inode->i_ino;
int ret;
apfs_init_xattr_key(cnid, name, &key);
query = apfs_alloc_query(sbi->s_cat_root, NULL /* parent */);
if (!query)
return -ENOMEM;
query->key = &key;
query->flags |= APFS_QUERY_CAT | APFS_QUERY_EXACT;
ret = apfs_btree_query(sb, &query);
if (ret)
goto done;
ret = apfs_xattr_from_query(query, &xattr);
if (ret) {
apfs_alert(sb, "bad xattr record in inode 0x%llx", cnid);
goto done;
}
if (xattr.has_dstream)
ret = apfs_xattr_extents_read(inode, &xattr, buffer, size);
else
ret = apfs_xattr_inline_read(inode, &xattr, buffer, size);
done:
apfs_free_query(sb, query);
return ret;
}
static int apfs_xattr_osx_get(const struct xattr_handler *handler,
struct dentry *unused, struct inode *inode,
const char *name, void *buffer, size_t size)
{
/* Ignore the fake 'osx' prefix */
return apfs_xattr_get(inode, name, buffer, size);
}
static const struct xattr_handler apfs_xattr_osx_handler = {
.prefix = XATTR_MAC_OSX_PREFIX,
.get = apfs_xattr_osx_get,
};
/* On-disk xattrs have no namespace; use a fake 'osx' prefix in the kernel */
const struct xattr_handler *apfs_xattr_handlers[] = {
&apfs_xattr_osx_handler,
NULL
};
ssize_t apfs_listxattr(struct dentry *dentry, char *buffer, size_t size)
{
struct inode *inode = d_inode(dentry);
struct super_block *sb = inode->i_sb;
struct apfs_sb_info *sbi = APFS_SB(sb);
struct apfs_key key;
struct apfs_query *query;
u64 cnid = inode->i_ino;
size_t free = size;
ssize_t ret;
query = apfs_alloc_query(sbi->s_cat_root, NULL /* parent */);
if (!query)
return -ENOMEM;
/* We want all the xattrs for the cnid, regardless of the name */
apfs_init_xattr_key(cnid, NULL /* name */, &key);
query->key = &key;
query->flags = APFS_QUERY_CAT | APFS_QUERY_MULTIPLE | APFS_QUERY_EXACT;
while (1) {
struct apfs_xattr xattr;
ret = apfs_btree_query(sb, &query);
if (ret == -ENODATA) { /* Got all the xattrs */
ret = size - free;
break;
}
if (ret)
break;
ret = apfs_xattr_from_query(query, &xattr);
if (ret) {
apfs_alert(sb, "bad xattr key in inode %llx", cnid);
break;
}
if (buffer) {
/* Prepend the fake 'osx' prefix before listing */
if (xattr.name_len + XATTR_MAC_OSX_PREFIX_LEN + 1 >
free) {
ret = -ERANGE;
break;
}
memcpy(buffer, XATTR_MAC_OSX_PREFIX,
XATTR_MAC_OSX_PREFIX_LEN);
buffer += XATTR_MAC_OSX_PREFIX_LEN;
memcpy(buffer, xattr.name, xattr.name_len + 1);
buffer += xattr.name_len + 1;
}
free -= xattr.name_len + XATTR_MAC_OSX_PREFIX_LEN + 1;
}
apfs_free_query(sb, query);
return ret;
}