1 /*
2 * ext_attr.c --- extended attribute blocks
3 *
4 * Copyright (C) 2001 Andreas Gruenbacher, <a.gruenbacher@computer.org>
5 *
6 * Copyright (C) 2002 Theodore Ts'o.
7 *
8 * %Begin-Header%
9 * This file may be redistributed under the terms of the GNU Library
10 * General Public License, version 2.
11 * %End-Header%
12 */
13
14 #include "config.h"
15 #include <stdio.h>
16 #if HAVE_UNISTD_H
17 #include <unistd.h>
18 #endif
19 #include <string.h>
20 #include <time.h>
21
22 #include "ext2_fs.h"
23 #include "ext2_ext_attr.h"
24 #include "ext4_acl.h"
25
26 #include "ext2fs.h"
27
read_ea_inode_hash(ext2_filsys fs,ext2_ino_t ino,__u32 * hash)28 static errcode_t read_ea_inode_hash(ext2_filsys fs, ext2_ino_t ino, __u32 *hash)
29 {
30 struct ext2_inode inode;
31 errcode_t retval;
32
33 retval = ext2fs_read_inode(fs, ino, &inode);
34 if (retval)
35 return retval;
36 *hash = ext2fs_get_ea_inode_hash(&inode);
37 return 0;
38 }
39
40 #define NAME_HASH_SHIFT 5
41 #define VALUE_HASH_SHIFT 16
42
43 /*
44 * ext2_xattr_hash_entry()
45 *
46 * Compute the hash of an extended attribute.
47 */
ext2fs_ext_attr_hash_entry(struct ext2_ext_attr_entry * entry,void * data)48 __u32 ext2fs_ext_attr_hash_entry(struct ext2_ext_attr_entry *entry, void *data)
49 {
50 __u32 hash = 0;
51 unsigned char *name = (((unsigned char *) entry) +
52 sizeof(struct ext2_ext_attr_entry));
53 int n;
54
55 for (n = 0; n < entry->e_name_len; n++) {
56 hash = (hash << NAME_HASH_SHIFT) ^
57 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
58 *name++;
59 }
60
61 /* The hash needs to be calculated on the data in little-endian. */
62 if (entry->e_value_inum == 0 && entry->e_value_size != 0) {
63 __u32 *value = (__u32 *)data;
64 for (n = (entry->e_value_size + EXT2_EXT_ATTR_ROUND) >>
65 EXT2_EXT_ATTR_PAD_BITS; n; n--) {
66 hash = (hash << VALUE_HASH_SHIFT) ^
67 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
68 ext2fs_le32_to_cpu(*value++);
69 }
70 }
71
72 return hash;
73 }
74
ext2fs_ext_attr_hash_entry_signed(struct ext2_ext_attr_entry * entry,void * data)75 __u32 ext2fs_ext_attr_hash_entry_signed(struct ext2_ext_attr_entry *entry,
76 void *data)
77 {
78 __u32 hash = 0;
79 signed char *name = (((signed char *) entry) +
80 sizeof(struct ext2_ext_attr_entry));
81 int n;
82
83 for (n = 0; n < entry->e_name_len; n++) {
84 hash = (hash << NAME_HASH_SHIFT) ^
85 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
86 *name++;
87 }
88
89 /* The hash needs to be calculated on the data in little-endian. */
90 if (entry->e_value_inum == 0 && entry->e_value_size != 0) {
91 __u32 *value = (__u32 *)data;
92 for (n = (entry->e_value_size + EXT2_EXT_ATTR_ROUND) >>
93 EXT2_EXT_ATTR_PAD_BITS; n; n--) {
94 hash = (hash << VALUE_HASH_SHIFT) ^
95 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
96 ext2fs_le32_to_cpu(*value++);
97 }
98 }
99
100 return hash;
101 }
102
103
104 /*
105 * ext2fs_ext_attr_hash_entry3()
106 *
107 * Compute the hash of an extended attribute. This version of the
108 * function supports hashing entries that reference external inodes
109 * (ea_inode feature) as well as calculating the old legacy signed
110 * hash variant.
111 */
ext2fs_ext_attr_hash_entry3(ext2_filsys fs,struct ext2_ext_attr_entry * entry,void * data,__u32 * hash,__u32 * signed_hash)112 errcode_t ext2fs_ext_attr_hash_entry3(ext2_filsys fs,
113 struct ext2_ext_attr_entry *entry,
114 void *data, __u32 *hash,
115 __u32 *signed_hash)
116 {
117 *hash = ext2fs_ext_attr_hash_entry(entry, data);
118 if (signed_hash)
119 *signed_hash = ext2fs_ext_attr_hash_entry_signed(entry, data);
120
121 if (entry->e_value_inum) {
122 __u32 ea_inode_hash;
123 errcode_t retval;
124
125 retval = read_ea_inode_hash(fs, entry->e_value_inum,
126 &ea_inode_hash);
127 if (retval)
128 return retval;
129
130 *hash = (*hash << VALUE_HASH_SHIFT) ^
131 (*hash >> (8*sizeof(*hash) - VALUE_HASH_SHIFT)) ^
132 ea_inode_hash;
133 if (signed_hash)
134 *signed_hash = (*signed_hash << VALUE_HASH_SHIFT) ^
135 (*signed_hash >> (8*sizeof(*hash) -
136 VALUE_HASH_SHIFT)) ^
137 ea_inode_hash;
138 }
139 return 0;
140 }
141
142 /*
143 * ext2fs_ext_attr_hash_entry2()
144 *
145 * Compute the hash of an extended attribute.
146 * This version of the function supports hashing entries that reference
147 * external inodes (ea_inode feature).
148 */
ext2fs_ext_attr_hash_entry2(ext2_filsys fs,struct ext2_ext_attr_entry * entry,void * data,__u32 * hash)149 errcode_t ext2fs_ext_attr_hash_entry2(ext2_filsys fs,
150 struct ext2_ext_attr_entry *entry,
151 void *data, __u32 *hash)
152 {
153 return ext2fs_ext_attr_hash_entry3(fs, entry, data, hash, NULL);
154 }
155
156 #undef NAME_HASH_SHIFT
157 #undef VALUE_HASH_SHIFT
158
159 #define BLOCK_HASH_SHIFT 16
160
161 /* Mirrors ext4_xattr_rehash() implementation in kernel. */
ext2fs_ext_attr_block_rehash(struct ext2_ext_attr_header * header,struct ext2_ext_attr_entry * end)162 void ext2fs_ext_attr_block_rehash(struct ext2_ext_attr_header *header,
163 struct ext2_ext_attr_entry *end)
164 {
165 struct ext2_ext_attr_entry *here;
166 __u32 hash = 0;
167
168 here = (struct ext2_ext_attr_entry *)(header+1);
169 while (here < end && !EXT2_EXT_IS_LAST_ENTRY(here)) {
170 if (!here->e_hash) {
171 /* Block is not shared if an entry's hash value == 0 */
172 hash = 0;
173 break;
174 }
175 hash = (hash << BLOCK_HASH_SHIFT) ^
176 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
177 here->e_hash;
178 here = EXT2_EXT_ATTR_NEXT(here);
179 }
180 header->h_hash = hash;
181 }
182
183 #undef BLOCK_HASH_SHIFT
184
ext2fs_get_ea_inode_hash(struct ext2_inode * inode)185 __u32 ext2fs_get_ea_inode_hash(struct ext2_inode *inode)
186 {
187 return inode->i_atime;
188 }
189
ext2fs_set_ea_inode_hash(struct ext2_inode * inode,__u32 hash)190 void ext2fs_set_ea_inode_hash(struct ext2_inode *inode, __u32 hash)
191 {
192 inode->i_atime = hash;
193 }
194
ext2fs_get_ea_inode_ref(struct ext2_inode * inode)195 __u64 ext2fs_get_ea_inode_ref(struct ext2_inode *inode)
196 {
197 return ((__u64)inode->i_ctime << 32) | inode->osd1.linux1.l_i_version;
198 }
199
ext2fs_set_ea_inode_ref(struct ext2_inode * inode,__u64 ref_count)200 void ext2fs_set_ea_inode_ref(struct ext2_inode *inode, __u64 ref_count)
201 {
202 inode->i_ctime = (__u32)(ref_count >> 32);
203 inode->osd1.linux1.l_i_version = (__u32)ref_count;
204 }
205
check_ext_attr_header(struct ext2_ext_attr_header * header)206 static errcode_t check_ext_attr_header(struct ext2_ext_attr_header *header)
207 {
208 if ((header->h_magic != EXT2_EXT_ATTR_MAGIC_v1 &&
209 header->h_magic != EXT2_EXT_ATTR_MAGIC) ||
210 header->h_blocks != 1)
211 return EXT2_ET_BAD_EA_HEADER;
212
213 return 0;
214 }
215
ext2fs_read_ext_attr3(ext2_filsys fs,blk64_t block,void * buf,ext2_ino_t inum)216 errcode_t ext2fs_read_ext_attr3(ext2_filsys fs, blk64_t block, void *buf,
217 ext2_ino_t inum)
218 {
219 int csum_failed = 0;
220 errcode_t retval;
221
222 retval = io_channel_read_blk64(fs->io, block, 1, buf);
223 if (retval)
224 return retval;
225
226 if (!(fs->flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) &&
227 !ext2fs_ext_attr_block_csum_verify(fs, inum, block, buf))
228 csum_failed = 1;
229
230 #ifdef WORDS_BIGENDIAN
231 ext2fs_swap_ext_attr(buf, buf, fs->blocksize, 1);
232 #endif
233
234 retval = check_ext_attr_header(buf);
235 if (retval == 0 && csum_failed)
236 retval = EXT2_ET_EXT_ATTR_CSUM_INVALID;
237
238 return retval;
239 }
240
ext2fs_read_ext_attr2(ext2_filsys fs,blk64_t block,void * buf)241 errcode_t ext2fs_read_ext_attr2(ext2_filsys fs, blk64_t block, void *buf)
242 {
243 return ext2fs_read_ext_attr3(fs, block, buf, 0);
244 }
245
ext2fs_read_ext_attr(ext2_filsys fs,blk_t block,void * buf)246 errcode_t ext2fs_read_ext_attr(ext2_filsys fs, blk_t block, void *buf)
247 {
248 return ext2fs_read_ext_attr2(fs, block, buf);
249 }
250
ext2fs_write_ext_attr3(ext2_filsys fs,blk64_t block,void * inbuf,ext2_ino_t inum)251 errcode_t ext2fs_write_ext_attr3(ext2_filsys fs, blk64_t block, void *inbuf,
252 ext2_ino_t inum)
253 {
254 errcode_t retval;
255 char *write_buf;
256
257 #ifdef WORDS_BIGENDIAN
258 retval = ext2fs_get_mem(fs->blocksize, &write_buf);
259 if (retval)
260 return retval;
261 ext2fs_swap_ext_attr(write_buf, inbuf, fs->blocksize, 1);
262 #else
263 write_buf = (char *) inbuf;
264 #endif
265
266 retval = ext2fs_ext_attr_block_csum_set(fs, inum, block,
267 (struct ext2_ext_attr_header *)write_buf);
268 if (retval)
269 return retval;
270
271 retval = io_channel_write_blk64(fs->io, block, 1, write_buf);
272 #ifdef WORDS_BIGENDIAN
273 ext2fs_free_mem(&write_buf);
274 #endif
275 if (!retval)
276 ext2fs_mark_changed(fs);
277 return retval;
278 }
279
ext2fs_write_ext_attr2(ext2_filsys fs,blk64_t block,void * inbuf)280 errcode_t ext2fs_write_ext_attr2(ext2_filsys fs, blk64_t block, void *inbuf)
281 {
282 return ext2fs_write_ext_attr3(fs, block, inbuf, 0);
283 }
284
ext2fs_write_ext_attr(ext2_filsys fs,blk_t block,void * inbuf)285 errcode_t ext2fs_write_ext_attr(ext2_filsys fs, blk_t block, void *inbuf)
286 {
287 return ext2fs_write_ext_attr2(fs, block, inbuf);
288 }
289
290 /*
291 * This function adjusts the reference count of the EA block.
292 */
ext2fs_adjust_ea_refcount3(ext2_filsys fs,blk64_t blk,char * block_buf,int adjust,__u32 * newcount,ext2_ino_t inum)293 errcode_t ext2fs_adjust_ea_refcount3(ext2_filsys fs, blk64_t blk,
294 char *block_buf, int adjust,
295 __u32 *newcount, ext2_ino_t inum)
296 {
297 errcode_t retval;
298 struct ext2_ext_attr_header *header;
299 char *buf = 0;
300
301 if ((blk >= ext2fs_blocks_count(fs->super)) ||
302 (blk < fs->super->s_first_data_block))
303 return EXT2_ET_BAD_EA_BLOCK_NUM;
304
305 if (!block_buf) {
306 retval = ext2fs_get_mem(fs->blocksize, &buf);
307 if (retval)
308 return retval;
309 block_buf = buf;
310 }
311
312 retval = ext2fs_read_ext_attr3(fs, blk, block_buf, inum);
313 if (retval)
314 goto errout;
315
316 header = (struct ext2_ext_attr_header *) block_buf;
317 header->h_refcount += adjust;
318 if (newcount)
319 *newcount = header->h_refcount;
320
321 retval = ext2fs_write_ext_attr3(fs, blk, block_buf, inum);
322 if (retval)
323 goto errout;
324
325 errout:
326 if (buf)
327 ext2fs_free_mem(&buf);
328 return retval;
329 }
330
ext2fs_adjust_ea_refcount2(ext2_filsys fs,blk64_t blk,char * block_buf,int adjust,__u32 * newcount)331 errcode_t ext2fs_adjust_ea_refcount2(ext2_filsys fs, blk64_t blk,
332 char *block_buf, int adjust,
333 __u32 *newcount)
334 {
335 return ext2fs_adjust_ea_refcount3(fs, blk, block_buf, adjust,
336 newcount, 0);
337 }
338
ext2fs_adjust_ea_refcount(ext2_filsys fs,blk_t blk,char * block_buf,int adjust,__u32 * newcount)339 errcode_t ext2fs_adjust_ea_refcount(ext2_filsys fs, blk_t blk,
340 char *block_buf, int adjust,
341 __u32 *newcount)
342 {
343 return ext2fs_adjust_ea_refcount2(fs, blk, block_buf, adjust,
344 newcount);
345 }
346
347 /* Manipulate the contents of extended attribute regions */
348 struct ext2_xattr {
349 int name_index;
350 char *name;
351 char *short_name;
352 void *value;
353 unsigned int value_len;
354 ext2_ino_t ea_ino;
355 };
356
357 struct ext2_xattr_handle {
358 errcode_t magic;
359 ext2_filsys fs;
360 struct ext2_xattr *attrs;
361 int capacity;
362 int count;
363 int ibody_count;
364 ext2_ino_t ino;
365 unsigned int flags;
366 };
367
ext2fs_xattrs_expand(struct ext2_xattr_handle * h,unsigned int expandby)368 static errcode_t ext2fs_xattrs_expand(struct ext2_xattr_handle *h,
369 unsigned int expandby)
370 {
371 struct ext2_xattr *new_attrs;
372 errcode_t err;
373
374 err = ext2fs_get_arrayzero(h->capacity + expandby,
375 sizeof(struct ext2_xattr), &new_attrs);
376 if (err)
377 return err;
378
379 memcpy(new_attrs, h->attrs, h->capacity * sizeof(struct ext2_xattr));
380 ext2fs_free_mem(&h->attrs);
381 h->capacity += expandby;
382 h->attrs = new_attrs;
383
384 return 0;
385 }
386
387 struct ea_name_index {
388 int index;
389 const char *name;
390 };
391
392 /* Keep these names sorted in order of decreasing specificity. */
393 static struct ea_name_index ea_names[] = {
394 {10, "gnu."},
395 {3, "system.posix_acl_default"},
396 {2, "system.posix_acl_access"},
397 {8, "system.richacl"},
398 {6, "security."},
399 {4, "trusted."},
400 {7, "system."},
401 {1, "user."},
402 {0, NULL},
403 };
404
find_ea_prefix(int index)405 static const char *find_ea_prefix(int index)
406 {
407 struct ea_name_index *e;
408
409 for (e = ea_names; e->name; e++)
410 if (e->index == index)
411 return e->name;
412
413 return NULL;
414 }
415
find_ea_index(const char * fullname,const char ** name,int * index)416 static int find_ea_index(const char *fullname, const char **name, int *index)
417 {
418 struct ea_name_index *e;
419
420 for (e = ea_names; e->name; e++) {
421 if (strncmp(fullname, e->name, strlen(e->name)) == 0) {
422 *name = fullname + strlen(e->name);
423 *index = e->index;
424 return 1;
425 }
426 }
427 return 0;
428 }
429
ext2fs_free_ext_attr(ext2_filsys fs,ext2_ino_t ino,struct ext2_inode_large * inode)430 errcode_t ext2fs_free_ext_attr(ext2_filsys fs, ext2_ino_t ino,
431 struct ext2_inode_large *inode)
432 {
433 struct ext2_ext_attr_header *header;
434 void *block_buf = NULL;
435 blk64_t blk;
436 errcode_t err;
437 struct ext2_inode_large i;
438
439 /* Read inode? */
440 if (inode == NULL) {
441 err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&i,
442 sizeof(struct ext2_inode_large));
443 if (err)
444 return err;
445 inode = &i;
446 }
447
448 /* Do we already have an EA block? */
449 blk = ext2fs_file_acl_block(fs, (struct ext2_inode *)inode);
450 if (blk == 0)
451 return 0;
452
453 /* Find block, zero it, write back */
454 if ((blk < fs->super->s_first_data_block) ||
455 (blk >= ext2fs_blocks_count(fs->super))) {
456 err = EXT2_ET_BAD_EA_BLOCK_NUM;
457 goto out;
458 }
459
460 err = ext2fs_get_mem(fs->blocksize, &block_buf);
461 if (err)
462 goto out;
463
464 err = ext2fs_read_ext_attr3(fs, blk, block_buf, ino);
465 if (err)
466 goto out2;
467
468 /* We only know how to deal with v2 EA blocks */
469 header = (struct ext2_ext_attr_header *) block_buf;
470 if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
471 err = EXT2_ET_BAD_EA_HEADER;
472 goto out2;
473 }
474
475 header->h_refcount--;
476 err = ext2fs_write_ext_attr3(fs, blk, block_buf, ino);
477 if (err)
478 goto out2;
479
480 /* Erase link to block */
481 ext2fs_file_acl_block_set(fs, (struct ext2_inode *)inode, 0);
482 if (header->h_refcount == 0)
483 ext2fs_block_alloc_stats2(fs, blk, -1);
484 err = ext2fs_iblk_sub_blocks(fs, (struct ext2_inode *)inode, 1);
485 if (err)
486 goto out2;
487
488 /* Write inode? */
489 if (inode == &i) {
490 err = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&i,
491 sizeof(struct ext2_inode_large));
492 if (err)
493 goto out2;
494 }
495
496 out2:
497 ext2fs_free_mem(&block_buf);
498 out:
499 return err;
500 }
501
prep_ea_block_for_write(ext2_filsys fs,ext2_ino_t ino,struct ext2_inode_large * inode)502 static errcode_t prep_ea_block_for_write(ext2_filsys fs, ext2_ino_t ino,
503 struct ext2_inode_large *inode)
504 {
505 struct ext2_ext_attr_header *header;
506 void *block_buf = NULL;
507 blk64_t blk, goal;
508 errcode_t err;
509
510 /* Do we already have an EA block? */
511 blk = ext2fs_file_acl_block(fs, (struct ext2_inode *)inode);
512 if (blk != 0) {
513 if ((blk < fs->super->s_first_data_block) ||
514 (blk >= ext2fs_blocks_count(fs->super))) {
515 err = EXT2_ET_BAD_EA_BLOCK_NUM;
516 goto out;
517 }
518
519 err = ext2fs_get_mem(fs->blocksize, &block_buf);
520 if (err)
521 goto out;
522
523 err = ext2fs_read_ext_attr3(fs, blk, block_buf, ino);
524 if (err)
525 goto out2;
526
527 /* We only know how to deal with v2 EA blocks */
528 header = (struct ext2_ext_attr_header *) block_buf;
529 if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
530 err = EXT2_ET_BAD_EA_HEADER;
531 goto out2;
532 }
533
534 /* Single-user block. We're done here. */
535 if (header->h_refcount == 1)
536 goto out2;
537
538 /* We need to CoW the block. */
539 header->h_refcount--;
540 err = ext2fs_write_ext_attr3(fs, blk, block_buf, ino);
541 if (err)
542 goto out2;
543 } else {
544 /* No block, we must increment i_blocks */
545 err = ext2fs_iblk_add_blocks(fs, (struct ext2_inode *)inode,
546 1);
547 if (err)
548 goto out;
549 }
550
551 /* Allocate a block */
552 goal = ext2fs_find_inode_goal(fs, ino, (struct ext2_inode *)inode, 0);
553 err = ext2fs_alloc_block2(fs, goal, NULL, &blk);
554 if (err)
555 goto out2;
556 ext2fs_file_acl_block_set(fs, (struct ext2_inode *)inode, blk);
557 out2:
558 if (block_buf)
559 ext2fs_free_mem(&block_buf);
560 out:
561 return err;
562 }
563
564
565 static inline int
posix_acl_xattr_count(size_t size)566 posix_acl_xattr_count(size_t size)
567 {
568 if (size < sizeof(posix_acl_xattr_header))
569 return -1;
570 size -= sizeof(posix_acl_xattr_header);
571 if (size % sizeof(posix_acl_xattr_entry))
572 return -1;
573 return size / sizeof(posix_acl_xattr_entry);
574 }
575
576 /*
577 * The lgetxattr function returns data formatted in the POSIX extended
578 * attribute format. The on-disk format uses a more compact encoding.
579 * See the ext4_acl_to_disk in fs/ext4/acl.c.
580 */
convert_posix_acl_to_disk_buffer(const void * value,size_t size,void * out_buf,size_t * size_out)581 static errcode_t convert_posix_acl_to_disk_buffer(const void *value, size_t size,
582 void *out_buf, size_t *size_out)
583 {
584 const posix_acl_xattr_header *header =
585 (const posix_acl_xattr_header*) value;
586 const posix_acl_xattr_entry *end, *entry =
587 (const posix_acl_xattr_entry *)(header+1);
588 ext4_acl_header *ext_acl;
589 size_t s;
590 char *e;
591
592 int count;
593
594 if (!value)
595 return EINVAL;
596 if (size < sizeof(posix_acl_xattr_header))
597 return ENOMEM;
598 if (header->a_version != ext2fs_cpu_to_le32(POSIX_ACL_XATTR_VERSION))
599 return EINVAL;
600
601 count = posix_acl_xattr_count(size);
602 ext_acl = out_buf;
603 ext_acl->a_version = ext2fs_cpu_to_le32(EXT4_ACL_VERSION);
604
605 if (count <= 0)
606 return EINVAL;
607
608 e = (char *) out_buf + sizeof(ext4_acl_header);
609 s = sizeof(ext4_acl_header);
610 for (end = entry + count; entry != end;entry++) {
611 ext4_acl_entry *disk_entry = (ext4_acl_entry*) e;
612 disk_entry->e_tag = entry->e_tag;
613 disk_entry->e_perm = entry->e_perm;
614
615 switch(ext2fs_le16_to_cpu(entry->e_tag)) {
616 case ACL_USER_OBJ:
617 case ACL_GROUP_OBJ:
618 case ACL_MASK:
619 case ACL_OTHER:
620 e += sizeof(ext4_acl_entry_short);
621 s += sizeof(ext4_acl_entry_short);
622 break;
623 case ACL_USER:
624 case ACL_GROUP:
625 disk_entry->e_id = entry->e_id;
626 e += sizeof(ext4_acl_entry);
627 s += sizeof(ext4_acl_entry);
628 break;
629 default:
630 return EINVAL;
631 }
632 }
633 *size_out = s;
634 return 0;
635 }
636
convert_disk_buffer_to_posix_acl(const void * value,size_t size,void ** out_buf,size_t * size_out)637 static errcode_t convert_disk_buffer_to_posix_acl(const void *value, size_t size,
638 void **out_buf, size_t *size_out)
639 {
640 posix_acl_xattr_header *header;
641 posix_acl_xattr_entry *entry;
642 const ext4_acl_header *ext_acl = (const ext4_acl_header *) value;
643 errcode_t err;
644 const char *cp;
645 char *out;
646
647 if ((!value) ||
648 (size < sizeof(ext4_acl_header)) ||
649 (ext_acl->a_version != ext2fs_cpu_to_le32(EXT4_ACL_VERSION)))
650 return EINVAL;
651
652 err = ext2fs_get_mem(size * 2, &out);
653 if (err)
654 return err;
655
656 header = (posix_acl_xattr_header *) out;
657 header->a_version = ext2fs_cpu_to_le32(POSIX_ACL_XATTR_VERSION);
658 entry = (posix_acl_xattr_entry *) (out + sizeof(posix_acl_xattr_header));
659
660 cp = (const char *) value + sizeof(ext4_acl_header);
661 size -= sizeof(ext4_acl_header);
662
663 while (size > 0) {
664 const ext4_acl_entry *disk_entry = (const ext4_acl_entry *) cp;
665
666 entry->e_tag = disk_entry->e_tag;
667 entry->e_perm = disk_entry->e_perm;
668
669 switch(ext2fs_le16_to_cpu(entry->e_tag)) {
670 case ACL_USER_OBJ:
671 case ACL_GROUP_OBJ:
672 case ACL_MASK:
673 case ACL_OTHER:
674 entry->e_id = 0;
675 cp += sizeof(ext4_acl_entry_short);
676 size -= sizeof(ext4_acl_entry_short);
677 break;
678 case ACL_USER:
679 case ACL_GROUP:
680 entry->e_id = disk_entry->e_id;
681 cp += sizeof(ext4_acl_entry);
682 size -= sizeof(ext4_acl_entry);
683 break;
684 default:
685 ext2fs_free_mem(&out);
686 return EINVAL;
687 }
688 entry++;
689 }
690 *out_buf = out;
691 *size_out = ((char *) entry - out);
692 return 0;
693 }
694
695 static errcode_t
write_xattrs_to_buffer(ext2_filsys fs,struct ext2_xattr * attrs,int count,void * entries_start,unsigned int storage_size,unsigned int value_offset_correction,int write_hash)696 write_xattrs_to_buffer(ext2_filsys fs, struct ext2_xattr *attrs, int count,
697 void *entries_start, unsigned int storage_size,
698 unsigned int value_offset_correction, int write_hash)
699 {
700 struct ext2_xattr *x;
701 struct ext2_ext_attr_entry *e = entries_start;
702 char *end = (char *) entries_start + storage_size;
703 unsigned int value_size;
704 errcode_t err;
705
706 memset(entries_start, 0, storage_size);
707 for (x = attrs; x < attrs + count; x++) {
708 value_size = ((x->value_len + EXT2_EXT_ATTR_PAD - 1) /
709 EXT2_EXT_ATTR_PAD) * EXT2_EXT_ATTR_PAD;
710
711 /* Fill out e appropriately */
712 e->e_name_len = strlen(x->short_name);
713 e->e_name_index = x->name_index;
714
715 e->e_value_size = x->value_len;
716 e->e_value_inum = x->ea_ino;
717
718 /* Store name */
719 memcpy((char *)e + sizeof(*e), x->short_name, e->e_name_len);
720 if (x->ea_ino) {
721 e->e_value_offs = 0;
722 } else {
723 end -= value_size;
724 e->e_value_offs = end - (char *) entries_start +
725 value_offset_correction;
726 memcpy(end, x->value, e->e_value_size);
727 }
728
729 if (write_hash || x->ea_ino) {
730 err = ext2fs_ext_attr_hash_entry2(fs, e,
731 x->ea_ino ? 0 : end,
732 &e->e_hash);
733 if (err)
734 return err;
735 } else
736 e->e_hash = 0;
737
738 e = EXT2_EXT_ATTR_NEXT(e);
739 *(__u32 *)e = 0;
740 }
741 return 0;
742 }
743
ext2fs_xattrs_write(struct ext2_xattr_handle * handle)744 errcode_t ext2fs_xattrs_write(struct ext2_xattr_handle *handle)
745 {
746 ext2_filsys fs = handle->fs;
747 const unsigned int inode_size = EXT2_INODE_SIZE(fs->super);
748 struct ext2_inode_large *inode;
749 char *start, *block_buf = NULL;
750 struct ext2_ext_attr_header *header;
751 __u32 ea_inode_magic;
752 blk64_t blk;
753 unsigned int storage_size;
754 unsigned int i;
755 errcode_t err;
756
757 EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EA_HANDLE);
758 i = inode_size;
759 if (i < sizeof(*inode))
760 i = sizeof(*inode);
761 err = ext2fs_get_memzero(i, &inode);
762 if (err)
763 return err;
764
765 err = ext2fs_read_inode_full(fs, handle->ino, EXT2_INODE(inode),
766 inode_size);
767 if (err)
768 goto out;
769
770 /* If extra_isize isn't set, we need to set it now */
771 if (inode->i_extra_isize == 0 &&
772 inode_size > EXT2_GOOD_OLD_INODE_SIZE) {
773 char *p = (char *)inode;
774 size_t extra = fs->super->s_want_extra_isize;
775
776 if (extra == 0)
777 extra = sizeof(__u32);
778 memset(p + EXT2_GOOD_OLD_INODE_SIZE, 0, extra);
779 inode->i_extra_isize = extra;
780 }
781 if (inode->i_extra_isize & 3) {
782 err = EXT2_ET_INODE_CORRUPTED;
783 goto out;
784 }
785
786 /* Does the inode have space for EA? */
787 if (inode->i_extra_isize < sizeof(inode->i_extra_isize) ||
788 inode_size <= EXT2_GOOD_OLD_INODE_SIZE + inode->i_extra_isize +
789 sizeof(__u32))
790 goto write_ea_block;
791
792 /* Write the inode EA */
793 ea_inode_magic = EXT2_EXT_ATTR_MAGIC;
794 memcpy(((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
795 inode->i_extra_isize, &ea_inode_magic, sizeof(__u32));
796 storage_size = inode_size - EXT2_GOOD_OLD_INODE_SIZE -
797 inode->i_extra_isize - sizeof(__u32);
798 start = ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
799 inode->i_extra_isize + sizeof(__u32);
800
801 err = write_xattrs_to_buffer(fs, handle->attrs, handle->ibody_count,
802 start, storage_size, 0, 0);
803 if (err)
804 goto out;
805 write_ea_block:
806 /* Are we done? */
807 if (handle->ibody_count == handle->count &&
808 !ext2fs_file_acl_block(fs, EXT2_INODE(inode)))
809 goto skip_ea_block;
810
811 /* Write the EA block */
812 err = ext2fs_get_memzero(fs->blocksize, &block_buf);
813 if (err)
814 goto out;
815
816 storage_size = fs->blocksize - sizeof(struct ext2_ext_attr_header);
817 start = block_buf + sizeof(struct ext2_ext_attr_header);
818
819 err = write_xattrs_to_buffer(fs, handle->attrs + handle->ibody_count,
820 handle->count - handle->ibody_count, start,
821 storage_size, start - block_buf, 1);
822 if (err)
823 goto out2;
824
825 /* Write a header on the EA block */
826 header = (struct ext2_ext_attr_header *) block_buf;
827 header->h_magic = EXT2_EXT_ATTR_MAGIC;
828 header->h_refcount = 1;
829 header->h_blocks = 1;
830
831 /* Get a new block for writing */
832 err = prep_ea_block_for_write(fs, handle->ino, inode);
833 if (err)
834 goto out2;
835
836 /* Finally, write the new EA block */
837 blk = ext2fs_file_acl_block(fs, EXT2_INODE(inode));
838 err = ext2fs_write_ext_attr3(fs, blk, block_buf, handle->ino);
839 if (err)
840 goto out2;
841
842 skip_ea_block:
843 blk = ext2fs_file_acl_block(fs, (struct ext2_inode *)inode);
844 if (!block_buf && blk) {
845 /* xattrs shrunk, free the block */
846 err = ext2fs_free_ext_attr(fs, handle->ino, inode);
847 if (err)
848 goto out;
849 }
850
851 /* Write the inode */
852 err = ext2fs_write_inode_full(fs, handle->ino, EXT2_INODE(inode),
853 inode_size);
854 if (err)
855 goto out2;
856
857 out2:
858 ext2fs_free_mem(&block_buf);
859 out:
860 ext2fs_free_mem(&inode);
861 return err;
862 }
863
read_xattrs_from_buffer(struct ext2_xattr_handle * handle,struct ext2_inode_large * inode,struct ext2_ext_attr_entry * entries,unsigned int storage_size,char * value_start)864 static errcode_t read_xattrs_from_buffer(struct ext2_xattr_handle *handle,
865 struct ext2_inode_large *inode,
866 struct ext2_ext_attr_entry *entries,
867 unsigned int storage_size,
868 char *value_start)
869 {
870 struct ext2_xattr *x;
871 struct ext2_ext_attr_entry *entry, *end;
872 const char *prefix;
873 unsigned int remain, prefix_len;
874 errcode_t err;
875 unsigned int values_size = storage_size +
876 ((char *)entries - value_start);
877
878 /* find the end */
879 end = entries;
880 remain = storage_size;
881 while (remain >= sizeof(struct ext2_ext_attr_entry) &&
882 !EXT2_EXT_IS_LAST_ENTRY(end)) {
883
884 /* header eats this space */
885 remain -= sizeof(struct ext2_ext_attr_entry);
886
887 /* is attribute name valid? */
888 if (EXT2_EXT_ATTR_SIZE(end->e_name_len) > remain)
889 return EXT2_ET_EA_BAD_NAME_LEN;
890
891 /* attribute len eats this space */
892 remain -= EXT2_EXT_ATTR_SIZE(end->e_name_len);
893 end = EXT2_EXT_ATTR_NEXT(end);
894 }
895
896 entry = entries;
897 remain = storage_size;
898 while (remain >= sizeof(struct ext2_ext_attr_entry) &&
899 !EXT2_EXT_IS_LAST_ENTRY(entry)) {
900
901 /* Allocate space for more attrs? */
902 if (handle->count == handle->capacity) {
903 err = ext2fs_xattrs_expand(handle, 4);
904 if (err)
905 return err;
906 }
907
908 x = handle->attrs + handle->count;
909
910 /* header eats this space */
911 remain -= sizeof(struct ext2_ext_attr_entry);
912
913 /* attribute len eats this space */
914 remain -= EXT2_EXT_ATTR_SIZE(entry->e_name_len);
915
916 /* Extract name */
917 prefix = find_ea_prefix(entry->e_name_index);
918 prefix_len = (prefix ? strlen(prefix) : 0);
919 err = ext2fs_get_memzero(entry->e_name_len + prefix_len + 1,
920 &x->name);
921 if (err)
922 return err;
923 if (prefix)
924 memcpy(x->name, prefix, prefix_len);
925 if (entry->e_name_len)
926 memcpy(x->name + prefix_len,
927 (char *)entry + sizeof(*entry),
928 entry->e_name_len);
929 x->short_name = x->name + prefix_len;
930 x->name_index = entry->e_name_index;
931
932 /* Check & copy value */
933 if (!ext2fs_has_feature_ea_inode(handle->fs->super) &&
934 entry->e_value_inum != 0)
935 return EXT2_ET_BAD_EA_BLOCK_NUM;
936
937 if (entry->e_value_inum == 0) {
938 if (entry->e_value_size > remain)
939 return EXT2_ET_EA_BAD_VALUE_SIZE;
940
941 if (entry->e_value_offs + entry->e_value_size > values_size)
942 return EXT2_ET_EA_BAD_VALUE_OFFSET;
943
944 if (entry->e_value_size > 0 &&
945 value_start + entry->e_value_offs <
946 (char *)end + sizeof(__u32))
947 return EXT2_ET_EA_BAD_VALUE_OFFSET;
948
949 remain -= entry->e_value_size;
950
951 err = ext2fs_get_mem(entry->e_value_size, &x->value);
952 if (err)
953 return err;
954 memcpy(x->value, value_start + entry->e_value_offs,
955 entry->e_value_size);
956 } else {
957 struct ext2_inode *ea_inode;
958 ext2_file_t ea_file;
959
960 if (entry->e_value_offs != 0)
961 return EXT2_ET_EA_BAD_VALUE_OFFSET;
962
963 if (entry->e_value_size > (64 * 1024))
964 return EXT2_ET_EA_BAD_VALUE_SIZE;
965
966 err = ext2fs_get_mem(entry->e_value_size, &x->value);
967 if (err)
968 return err;
969
970 err = ext2fs_file_open(handle->fs, entry->e_value_inum,
971 0, &ea_file);
972 if (err)
973 return err;
974
975 ea_inode = ext2fs_file_get_inode(ea_file);
976 if ((ea_inode->i_flags & EXT4_INLINE_DATA_FL) ||
977 !(ea_inode->i_flags & EXT4_EA_INODE_FL) ||
978 ea_inode->i_links_count == 0)
979 err = EXT2_ET_EA_INODE_CORRUPTED;
980 else if ((__u64) ext2fs_file_get_size(ea_file) !=
981 entry->e_value_size)
982 err = EXT2_ET_EA_BAD_VALUE_SIZE;
983 else
984 err = ext2fs_file_read(ea_file, x->value,
985 entry->e_value_size, 0);
986 ext2fs_file_close(ea_file);
987 if (err)
988 return err;
989 }
990
991 x->ea_ino = entry->e_value_inum;
992 x->value_len = entry->e_value_size;
993
994 /* e_hash may be 0 in older inode's ea */
995 if (entry->e_hash != 0) {
996 __u32 hash, signed_hash;
997
998 void *data = (entry->e_value_inum != 0) ?
999 0 : value_start + entry->e_value_offs;
1000
1001 err = ext2fs_ext_attr_hash_entry3(handle->fs, entry,
1002 data, &hash,
1003 &signed_hash);
1004 if (err)
1005 return err;
1006 if ((entry->e_hash != hash) &&
1007 (entry->e_hash != signed_hash)) {
1008 struct ext2_inode child;
1009
1010 /* Check whether this is an old Lustre-style
1011 * ea_inode reference.
1012 */
1013 err = ext2fs_read_inode(handle->fs,
1014 entry->e_value_inum,
1015 &child);
1016 if (err)
1017 return err;
1018 if (child.i_mtime != handle->ino ||
1019 child.i_generation != inode->i_generation)
1020 return EXT2_ET_BAD_EA_HASH;
1021 }
1022 }
1023
1024 handle->count++;
1025 entry = EXT2_EXT_ATTR_NEXT(entry);
1026 }
1027
1028 return 0;
1029 }
1030
xattrs_free_keys(struct ext2_xattr_handle * h)1031 static void xattrs_free_keys(struct ext2_xattr_handle *h)
1032 {
1033 struct ext2_xattr *a = h->attrs;
1034 int i;
1035
1036 for (i = 0; i < h->capacity; i++) {
1037 if (a[i].name)
1038 ext2fs_free_mem(&a[i].name);
1039 if (a[i].value)
1040 ext2fs_free_mem(&a[i].value);
1041 }
1042 h->count = 0;
1043 h->ibody_count = 0;
1044 }
1045
ext2fs_xattrs_read(struct ext2_xattr_handle * handle)1046 errcode_t ext2fs_xattrs_read(struct ext2_xattr_handle *handle)
1047 {
1048 struct ext2_inode_large *inode;
1049 struct ext2_ext_attr_header *header;
1050 __u32 ea_inode_magic;
1051 unsigned int storage_size;
1052 char *start, *block_buf = NULL;
1053 blk64_t blk;
1054 size_t i;
1055 errcode_t err;
1056
1057 EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EA_HANDLE);
1058 i = EXT2_INODE_SIZE(handle->fs->super);
1059 if (i < sizeof(*inode))
1060 i = sizeof(*inode);
1061 err = ext2fs_get_memzero(i, &inode);
1062 if (err)
1063 return err;
1064
1065 err = ext2fs_read_inode_full(handle->fs, handle->ino,
1066 (struct ext2_inode *)inode,
1067 EXT2_INODE_SIZE(handle->fs->super));
1068 if (err)
1069 goto out;
1070
1071 xattrs_free_keys(handle);
1072
1073 /* Does the inode have space for EA? */
1074 if (inode->i_extra_isize < sizeof(inode->i_extra_isize) ||
1075 EXT2_INODE_SIZE(handle->fs->super) <= EXT2_GOOD_OLD_INODE_SIZE +
1076 inode->i_extra_isize +
1077 sizeof(__u32))
1078 goto read_ea_block;
1079 if (inode->i_extra_isize & 3) {
1080 err = EXT2_ET_INODE_CORRUPTED;
1081 goto out;
1082 }
1083
1084 /* Look for EA in the inode */
1085 memcpy(&ea_inode_magic, ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
1086 inode->i_extra_isize, sizeof(__u32));
1087 if (ea_inode_magic == EXT2_EXT_ATTR_MAGIC) {
1088 storage_size = EXT2_INODE_SIZE(handle->fs->super) -
1089 EXT2_GOOD_OLD_INODE_SIZE - inode->i_extra_isize -
1090 sizeof(__u32);
1091 start = ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
1092 inode->i_extra_isize + sizeof(__u32);
1093
1094 err = read_xattrs_from_buffer(handle, inode,
1095 (struct ext2_ext_attr_entry *) start,
1096 storage_size, start);
1097 if (err)
1098 goto out;
1099
1100 handle->ibody_count = handle->count;
1101 }
1102
1103 read_ea_block:
1104 /* Look for EA in a separate EA block */
1105 blk = ext2fs_file_acl_block(handle->fs, (struct ext2_inode *)inode);
1106 if (blk != 0) {
1107 if ((blk < handle->fs->super->s_first_data_block) ||
1108 (blk >= ext2fs_blocks_count(handle->fs->super))) {
1109 err = EXT2_ET_BAD_EA_BLOCK_NUM;
1110 goto out;
1111 }
1112
1113 err = ext2fs_get_mem(handle->fs->blocksize, &block_buf);
1114 if (err)
1115 goto out;
1116
1117 err = ext2fs_read_ext_attr3(handle->fs, blk, block_buf,
1118 handle->ino);
1119 if (err)
1120 goto out3;
1121
1122 /* We only know how to deal with v2 EA blocks */
1123 header = (struct ext2_ext_attr_header *) block_buf;
1124 if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
1125 err = EXT2_ET_BAD_EA_HEADER;
1126 goto out3;
1127 }
1128
1129 /* Read EAs */
1130 storage_size = handle->fs->blocksize -
1131 sizeof(struct ext2_ext_attr_header);
1132 start = block_buf + sizeof(struct ext2_ext_attr_header);
1133 err = read_xattrs_from_buffer(handle, inode,
1134 (struct ext2_ext_attr_entry *) start,
1135 storage_size, block_buf);
1136 if (err)
1137 goto out3;
1138
1139 ext2fs_free_mem(&block_buf);
1140 }
1141
1142 ext2fs_free_mem(&block_buf);
1143 ext2fs_free_mem(&inode);
1144 return 0;
1145
1146 out3:
1147 ext2fs_free_mem(&block_buf);
1148 out:
1149 ext2fs_free_mem(&inode);
1150 return err;
1151 }
1152
ext2fs_xattrs_iterate(struct ext2_xattr_handle * h,int (* func)(char * name,char * value,size_t value_len,void * data),void * data)1153 errcode_t ext2fs_xattrs_iterate(struct ext2_xattr_handle *h,
1154 int (*func)(char *name, char *value,
1155 size_t value_len, void *data),
1156 void *data)
1157 {
1158 struct ext2_xattr *x;
1159 int dirty = 0;
1160 int ret;
1161
1162 EXT2_CHECK_MAGIC(h, EXT2_ET_MAGIC_EA_HANDLE);
1163 for (x = h->attrs; x < h->attrs + h->count; x++) {
1164 ret = func(x->name, x->value, x->value_len, data);
1165 if (ret & XATTR_CHANGED)
1166 dirty = 1;
1167 if (ret & XATTR_ABORT)
1168 break;
1169 }
1170
1171 if (dirty)
1172 return ext2fs_xattrs_write(h);
1173 return 0;
1174 }
1175
ext2fs_xattr_get(struct ext2_xattr_handle * h,const char * key,void ** value,size_t * value_len)1176 errcode_t ext2fs_xattr_get(struct ext2_xattr_handle *h, const char *key,
1177 void **value, size_t *value_len)
1178 {
1179 struct ext2_xattr *x;
1180 char *val;
1181 errcode_t err;
1182
1183 EXT2_CHECK_MAGIC(h, EXT2_ET_MAGIC_EA_HANDLE);
1184 for (x = h->attrs; x < h->attrs + h->count; x++) {
1185 if (strcmp(x->name, key))
1186 continue;
1187
1188 if (!(h->flags & XATTR_HANDLE_FLAG_RAW) &&
1189 ((strcmp(key, "system.posix_acl_default") == 0) ||
1190 (strcmp(key, "system.posix_acl_access") == 0))) {
1191 err = convert_disk_buffer_to_posix_acl(x->value, x->value_len,
1192 value, value_len);
1193 return err;
1194 } else {
1195 err = ext2fs_get_mem(x->value_len, &val);
1196 if (err)
1197 return err;
1198 memcpy(val, x->value, x->value_len);
1199 *value = val;
1200 *value_len = x->value_len;
1201 return 0;
1202 }
1203 }
1204
1205 return EXT2_ET_EA_KEY_NOT_FOUND;
1206 }
1207
ext2fs_xattr_inode_max_size(ext2_filsys fs,ext2_ino_t ino,size_t * size)1208 errcode_t ext2fs_xattr_inode_max_size(ext2_filsys fs, ext2_ino_t ino,
1209 size_t *size)
1210 {
1211 struct ext2_ext_attr_entry *entry;
1212 struct ext2_inode_large *inode;
1213 __u32 ea_inode_magic;
1214 unsigned int minoff;
1215 char *start;
1216 size_t i;
1217 errcode_t err;
1218
1219 i = EXT2_INODE_SIZE(fs->super);
1220 if (i < sizeof(*inode))
1221 i = sizeof(*inode);
1222 err = ext2fs_get_memzero(i, &inode);
1223 if (err)
1224 return err;
1225
1226 err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)inode,
1227 EXT2_INODE_SIZE(fs->super));
1228 if (err)
1229 goto out;
1230
1231 /* Does the inode have size for EA? */
1232 if (EXT2_INODE_SIZE(fs->super) <= EXT2_GOOD_OLD_INODE_SIZE +
1233 inode->i_extra_isize +
1234 sizeof(__u32)) {
1235 err = EXT2_ET_INLINE_DATA_NO_SPACE;
1236 goto out;
1237 }
1238
1239 minoff = EXT2_INODE_SIZE(fs->super) - sizeof(*inode) - sizeof(__u32);
1240 memcpy(&ea_inode_magic, ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
1241 inode->i_extra_isize, sizeof(__u32));
1242 if (ea_inode_magic == EXT2_EXT_ATTR_MAGIC) {
1243 /* has xattrs. calculate the size */
1244 start= ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
1245 inode->i_extra_isize + sizeof(__u32);
1246 entry = (struct ext2_ext_attr_entry *) start;
1247 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
1248 if (!entry->e_value_inum && entry->e_value_size) {
1249 unsigned int offs = entry->e_value_offs;
1250 if (offs < minoff)
1251 minoff = offs;
1252 }
1253 entry = EXT2_EXT_ATTR_NEXT(entry);
1254 }
1255 *size = minoff - ((char *)entry - (char *)start) - sizeof(__u32);
1256 } else {
1257 /* no xattr. return a maximum size */
1258 *size = EXT2_EXT_ATTR_SIZE(minoff -
1259 EXT2_EXT_ATTR_LEN(strlen("data")) -
1260 EXT2_EXT_ATTR_ROUND - sizeof(__u32));
1261 }
1262
1263 out:
1264 ext2fs_free_mem(&inode);
1265 return err;
1266 }
1267
xattr_create_ea_inode(ext2_filsys fs,const void * value,size_t value_len,ext2_ino_t * ea_ino)1268 static errcode_t xattr_create_ea_inode(ext2_filsys fs, const void *value,
1269 size_t value_len, ext2_ino_t *ea_ino)
1270 {
1271 struct ext2_inode inode;
1272 ext2_ino_t ino;
1273 ext2_file_t file;
1274 __u32 hash;
1275 errcode_t ret;
1276
1277 ret = ext2fs_new_inode(fs, 0, 0, 0, &ino);
1278 if (ret)
1279 return ret;
1280
1281 memset(&inode, 0, sizeof(inode));
1282 inode.i_flags |= EXT4_EA_INODE_FL;
1283 if (ext2fs_has_feature_extents(fs->super))
1284 inode.i_flags |= EXT4_EXTENTS_FL;
1285 inode.i_size = 0;
1286 inode.i_mode = LINUX_S_IFREG | 0600;
1287 inode.i_links_count = 1;
1288 ret = ext2fs_write_new_inode(fs, ino, &inode);
1289 if (ret)
1290 return ret;
1291 /*
1292 * ref_count and hash utilize inode's i_*time fields.
1293 * ext2fs_write_new_inode() call above initializes these fields with
1294 * current time. That's why ref count and hash updates are done
1295 * separately below.
1296 */
1297 ext2fs_set_ea_inode_ref(&inode, 1);
1298 hash = ext2fs_crc32c_le(fs->csum_seed, value, value_len);
1299 ext2fs_set_ea_inode_hash(&inode, hash);
1300
1301 ret = ext2fs_write_inode(fs, ino, &inode);
1302 if (ret)
1303 return ret;
1304
1305 ret = ext2fs_file_open(fs, ino, EXT2_FILE_WRITE, &file);
1306 if (ret)
1307 return ret;
1308 ret = ext2fs_file_write(file, value, value_len, NULL);
1309 ext2fs_file_close(file);
1310 if (ret)
1311 return ret;
1312
1313 ext2fs_inode_alloc_stats2(fs, ino, 1 /* inuse */, 0 /* isdir */);
1314
1315 *ea_ino = ino;
1316 return 0;
1317 }
1318
xattr_inode_dec_ref(ext2_filsys fs,ext2_ino_t ino)1319 static errcode_t xattr_inode_dec_ref(ext2_filsys fs, ext2_ino_t ino)
1320 {
1321 struct ext2_inode_large inode;
1322 __u64 ref_count;
1323 errcode_t ret;
1324
1325 ret = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
1326 sizeof(inode));
1327 if (ret)
1328 goto out;
1329
1330 ref_count = ext2fs_get_ea_inode_ref(EXT2_INODE(&inode));
1331 ref_count--;
1332 ext2fs_set_ea_inode_ref(EXT2_INODE(&inode), ref_count);
1333
1334 if (ref_count)
1335 goto write_out;
1336
1337 inode.i_links_count = 0;
1338 inode.i_dtime = fs->now ? fs->now : time(0);
1339
1340 ret = ext2fs_free_ext_attr(fs, ino, &inode);
1341 if (ret)
1342 goto write_out;
1343
1344 if (ext2fs_inode_has_valid_blocks2(fs, (struct ext2_inode *)&inode)) {
1345 ret = ext2fs_punch(fs, ino, (struct ext2_inode *)&inode, NULL,
1346 0, ~0ULL);
1347 if (ret)
1348 goto out;
1349 }
1350
1351 ext2fs_inode_alloc_stats2(fs, ino, -1 /* inuse */, 0 /* is_dir */);
1352
1353 write_out:
1354 ret = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode,
1355 sizeof(inode));
1356 out:
1357 return ret;
1358 }
1359
xattr_update_entry(ext2_filsys fs,struct ext2_xattr * x,const char * name,const char * short_name,int index,const void * value,size_t value_len,int in_inode)1360 static errcode_t xattr_update_entry(ext2_filsys fs, struct ext2_xattr *x,
1361 const char *name, const char *short_name,
1362 int index, const void *value,
1363 size_t value_len, int in_inode)
1364 {
1365 ext2_ino_t ea_ino = 0;
1366 void *new_value = NULL;
1367 char *new_name = NULL;
1368 int name_len;
1369 errcode_t ret;
1370
1371 if (!x->name) {
1372 name_len = strlen(name);
1373 ret = ext2fs_get_mem(name_len + 1, &new_name);
1374 if (ret)
1375 goto fail;
1376 memcpy(new_name, name, name_len + 1);
1377 }
1378
1379 ret = ext2fs_get_mem(value_len, &new_value);
1380 if (ret)
1381 goto fail;
1382 memcpy(new_value, value, value_len);
1383
1384 if (in_inode) {
1385 ret = xattr_create_ea_inode(fs, value, value_len, &ea_ino);
1386 if (ret)
1387 goto fail;
1388 }
1389
1390 if (x->ea_ino) {
1391 ret = xattr_inode_dec_ref(fs, x->ea_ino);
1392 if (ret)
1393 goto fail;
1394 }
1395
1396 if (!x->name) {
1397 x->name = new_name;
1398 x->short_name = new_name + (short_name - name);
1399 }
1400 x->name_index = index;
1401
1402 if (x->value)
1403 ext2fs_free_mem(&x->value);
1404 x->value = new_value;
1405 x->value_len = value_len;
1406 x->ea_ino = ea_ino;
1407 return 0;
1408 fail:
1409 if (new_name)
1410 ext2fs_free_mem(&new_name);
1411 if (new_value)
1412 ext2fs_free_mem(&new_value);
1413 if (ea_ino)
1414 xattr_inode_dec_ref(fs, ea_ino);
1415 return ret;
1416 }
1417
xattr_find_position(struct ext2_xattr * attrs,int count,const char * shortname,int name_idx)1418 static int xattr_find_position(struct ext2_xattr *attrs, int count,
1419 const char *shortname, int name_idx)
1420 {
1421 struct ext2_xattr *x;
1422 int i;
1423 int shortname_len, x_shortname_len;
1424
1425 shortname_len = strlen(shortname);
1426
1427 for (i = 0, x = attrs; i < count; i++, x++) {
1428 if (name_idx < x->name_index)
1429 break;
1430 if (name_idx > x->name_index)
1431 continue;
1432
1433 x_shortname_len = strlen(x->short_name);
1434 if (shortname_len < x_shortname_len)
1435 break;
1436 if (shortname_len > x_shortname_len)
1437 continue;
1438
1439 if (memcmp(shortname, x->short_name, shortname_len) <= 0)
1440 break;
1441 }
1442 return i;
1443 }
1444
xattr_array_update(struct ext2_xattr_handle * h,const char * name,const void * value,size_t value_len,int ibody_free,int block_free,int old_idx,int in_inode)1445 static errcode_t xattr_array_update(struct ext2_xattr_handle *h,
1446 const char *name,
1447 const void *value, size_t value_len,
1448 int ibody_free, int block_free,
1449 int old_idx, int in_inode)
1450 {
1451 struct ext2_xattr tmp;
1452 int add_to_ibody;
1453 int needed;
1454 int name_len, name_idx = 0;
1455 const char *shortname = name;
1456 int new_idx;
1457 int ret;
1458
1459 find_ea_index(name, &shortname, &name_idx);
1460 name_len = strlen(shortname);
1461
1462 needed = EXT2_EXT_ATTR_LEN(name_len);
1463 if (!in_inode)
1464 needed += EXT2_EXT_ATTR_SIZE(value_len);
1465
1466 if (old_idx >= 0 && old_idx < h->ibody_count) {
1467 ibody_free += EXT2_EXT_ATTR_LEN(name_len);
1468 if (!h->attrs[old_idx].ea_ino)
1469 ibody_free += EXT2_EXT_ATTR_SIZE(
1470 h->attrs[old_idx].value_len);
1471 }
1472
1473 if (needed <= ibody_free) {
1474 if (old_idx < 0) {
1475 new_idx = h->ibody_count;
1476 add_to_ibody = 1;
1477 goto add_new;
1478 }
1479
1480 /* Update the existing entry. */
1481 ret = xattr_update_entry(h->fs, &h->attrs[old_idx], name,
1482 shortname, name_idx, value,
1483 value_len, in_inode);
1484 if (ret)
1485 return ret;
1486 if (h->ibody_count <= old_idx) {
1487 /* Move entry from block to the end of ibody. */
1488 tmp = h->attrs[old_idx];
1489 memmove(h->attrs + h->ibody_count + 1,
1490 h->attrs + h->ibody_count,
1491 (old_idx - h->ibody_count) * sizeof(*h->attrs));
1492 h->attrs[h->ibody_count] = tmp;
1493 h->ibody_count++;
1494 }
1495 return 0;
1496 }
1497
1498 if (h->ibody_count <= old_idx) {
1499 block_free += EXT2_EXT_ATTR_LEN(name_len);
1500 if (!h->attrs[old_idx].ea_ino)
1501 block_free +=
1502 EXT2_EXT_ATTR_SIZE(h->attrs[old_idx].value_len);
1503 }
1504
1505 if (needed > block_free)
1506 return EXT2_ET_EA_NO_SPACE;
1507
1508 if (old_idx >= 0) {
1509 /* Update the existing entry. */
1510 ret = xattr_update_entry(h->fs, &h->attrs[old_idx], name,
1511 shortname, name_idx, value,
1512 value_len, in_inode);
1513 if (ret)
1514 return ret;
1515 if (old_idx < h->ibody_count) {
1516 /*
1517 * Move entry from ibody to the block. Note that
1518 * entries in the block are sorted.
1519 */
1520 new_idx = xattr_find_position(h->attrs + h->ibody_count,
1521 h->count - h->ibody_count,
1522 shortname, name_idx);
1523 new_idx += h->ibody_count - 1;
1524 tmp = h->attrs[old_idx];
1525 memmove(h->attrs + old_idx, h->attrs + old_idx + 1,
1526 (new_idx - old_idx) * sizeof(*h->attrs));
1527 h->attrs[new_idx] = tmp;
1528 h->ibody_count--;
1529 }
1530 return 0;
1531 }
1532
1533 new_idx = xattr_find_position(h->attrs + h->ibody_count,
1534 h->count - h->ibody_count,
1535 shortname, name_idx);
1536 new_idx += h->ibody_count;
1537 add_to_ibody = 0;
1538
1539 add_new:
1540 if (h->count == h->capacity) {
1541 ret = ext2fs_xattrs_expand(h, 4);
1542 if (ret)
1543 return ret;
1544 }
1545
1546 ret = xattr_update_entry(h->fs, &h->attrs[h->count], name, shortname,
1547 name_idx, value, value_len, in_inode);
1548 if (ret)
1549 return ret;
1550
1551 tmp = h->attrs[h->count];
1552 memmove(h->attrs + new_idx + 1, h->attrs + new_idx,
1553 (h->count - new_idx)*sizeof(*h->attrs));
1554 h->attrs[new_idx] = tmp;
1555 if (add_to_ibody)
1556 h->ibody_count++;
1557 h->count++;
1558 return 0;
1559 }
1560
space_used(struct ext2_xattr * attrs,int count)1561 static int space_used(struct ext2_xattr *attrs, int count)
1562 {
1563 int total = 0;
1564 struct ext2_xattr *x;
1565 int i, len;
1566
1567 for (i = 0, x = attrs; i < count; i++, x++) {
1568 len = strlen(x->short_name);
1569 total += EXT2_EXT_ATTR_LEN(len);
1570 if (!x->ea_ino)
1571 total += EXT2_EXT_ATTR_SIZE(x->value_len);
1572 }
1573 return total;
1574 }
1575
1576 /*
1577 * The minimum size of EA value when you start storing it in an external inode
1578 * size of block - size of header - size of 1 entry - 4 null bytes
1579 */
1580 #define EXT4_XATTR_MIN_LARGE_EA_SIZE(b) \
1581 ((b) - EXT2_EXT_ATTR_LEN(3) - sizeof(struct ext2_ext_attr_header) - 4)
1582
ext2fs_xattr_set(struct ext2_xattr_handle * h,const char * name,const void * value,size_t value_len)1583 errcode_t ext2fs_xattr_set(struct ext2_xattr_handle *h,
1584 const char *name,
1585 const void *value,
1586 size_t value_len)
1587 {
1588 ext2_filsys fs = h->fs;
1589 const int inode_size = EXT2_INODE_SIZE(fs->super);
1590 struct ext2_inode_large *inode = NULL;
1591 struct ext2_xattr *x;
1592 char *new_value;
1593 int ibody_free, block_free;
1594 int in_inode = 0;
1595 int old_idx = -1;
1596 int extra_isize;
1597 errcode_t ret;
1598
1599 EXT2_CHECK_MAGIC(h, EXT2_ET_MAGIC_EA_HANDLE);
1600
1601 ret = ext2fs_get_mem(value_len, &new_value);
1602 if (ret)
1603 return ret;
1604 if (!(h->flags & XATTR_HANDLE_FLAG_RAW) &&
1605 ((strcmp(name, "system.posix_acl_default") == 0) ||
1606 (strcmp(name, "system.posix_acl_access") == 0))) {
1607 ret = convert_posix_acl_to_disk_buffer(value, value_len,
1608 new_value, &value_len);
1609 if (ret)
1610 goto out;
1611 } else if (value_len)
1612 memcpy(new_value, value, value_len);
1613
1614 /* Imitate kernel behavior by skipping update if value is the same. */
1615 for (x = h->attrs; x < h->attrs + h->count; x++) {
1616 if (!strcmp(x->name, name)) {
1617 if (!x->ea_ino && x->value_len == value_len &&
1618 (!value_len ||
1619 !memcmp(x->value, new_value, value_len))) {
1620 ret = 0;
1621 goto out;
1622 }
1623 old_idx = x - h->attrs;
1624 break;
1625 }
1626 }
1627
1628 ret = ext2fs_get_memzero(inode_size, &inode);
1629 if (ret)
1630 goto out;
1631 ret = ext2fs_read_inode_full(fs, h->ino,
1632 (struct ext2_inode *)inode,
1633 inode_size);
1634 if (ret)
1635 goto out;
1636 if (inode_size > EXT2_GOOD_OLD_INODE_SIZE) {
1637 extra_isize = inode->i_extra_isize;
1638 if (extra_isize == 0) {
1639 extra_isize = fs->super->s_want_extra_isize;
1640 if (extra_isize == 0)
1641 extra_isize = sizeof(__u32);
1642 }
1643 ibody_free = inode_size - EXT2_GOOD_OLD_INODE_SIZE;
1644 ibody_free -= extra_isize;
1645 /* Extended attribute magic and final null entry. */
1646 ibody_free -= sizeof(__u32) * 2;
1647 ibody_free -= space_used(h->attrs, h->ibody_count);
1648 } else
1649 ibody_free = 0;
1650
1651 /* Inline data can only go to ibody. */
1652 if (strcmp(name, "system.data") == 0) {
1653 if (h->ibody_count <= old_idx) {
1654 ret = EXT2_ET_FILESYSTEM_CORRUPTED;
1655 goto out;
1656 }
1657 ret = xattr_array_update(h, name, new_value, value_len,
1658 ibody_free,
1659 0 /* block_free */, old_idx,
1660 0 /* in_inode */);
1661 if (ret)
1662 goto out;
1663 goto write_out;
1664 }
1665
1666 block_free = fs->blocksize;
1667 block_free -= sizeof(struct ext2_ext_attr_header);
1668 /* Final null entry. */
1669 block_free -= sizeof(__u32);
1670 block_free -= space_used(h->attrs + h->ibody_count,
1671 h->count - h->ibody_count);
1672
1673 if (ext2fs_has_feature_ea_inode(fs->super) &&
1674 value_len > EXT4_XATTR_MIN_LARGE_EA_SIZE(fs->blocksize))
1675 in_inode = 1;
1676
1677 ret = xattr_array_update(h, name, new_value, value_len, ibody_free,
1678 block_free, old_idx, in_inode);
1679 if (ret == EXT2_ET_EA_NO_SPACE && !in_inode &&
1680 ext2fs_has_feature_ea_inode(fs->super))
1681 ret = xattr_array_update(h, name, new_value, value_len,
1682 ibody_free, block_free, old_idx, 1 /* in_inode */);
1683 if (ret)
1684 goto out;
1685
1686 write_out:
1687 ret = ext2fs_xattrs_write(h);
1688 out:
1689 if (inode)
1690 ext2fs_free_mem(&inode);
1691 ext2fs_free_mem(&new_value);
1692 return ret;
1693 }
1694
ext2fs_xattr_remove(struct ext2_xattr_handle * handle,const char * key)1695 errcode_t ext2fs_xattr_remove(struct ext2_xattr_handle *handle,
1696 const char *key)
1697 {
1698 struct ext2_xattr *x;
1699 struct ext2_xattr *end = handle->attrs + handle->count;
1700
1701 EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EA_HANDLE);
1702 for (x = handle->attrs; x < end; x++) {
1703 if (strcmp(x->name, key) == 0) {
1704 ext2fs_free_mem(&x->name);
1705 ext2fs_free_mem(&x->value);
1706 if (x->ea_ino)
1707 xattr_inode_dec_ref(handle->fs, x->ea_ino);
1708 memmove(x, x + 1, (end - x - 1)*sizeof(*x));
1709 memset(end - 1, 0, sizeof(*end));
1710 if (x < handle->attrs + handle->ibody_count)
1711 handle->ibody_count--;
1712 handle->count--;
1713 return ext2fs_xattrs_write(handle);
1714 }
1715 }
1716
1717 /* no key found, success! */
1718 return 0;
1719 }
1720
ext2fs_xattrs_open(ext2_filsys fs,ext2_ino_t ino,struct ext2_xattr_handle ** handle)1721 errcode_t ext2fs_xattrs_open(ext2_filsys fs, ext2_ino_t ino,
1722 struct ext2_xattr_handle **handle)
1723 {
1724 struct ext2_xattr_handle *h;
1725 errcode_t err;
1726
1727 if (!ext2fs_has_feature_xattr(fs->super) &&
1728 !ext2fs_has_feature_inline_data(fs->super))
1729 return EXT2_ET_MISSING_EA_FEATURE;
1730
1731 err = ext2fs_get_memzero(sizeof(*h), &h);
1732 if (err)
1733 return err;
1734
1735 h->magic = EXT2_ET_MAGIC_EA_HANDLE;
1736 h->capacity = 4;
1737 err = ext2fs_get_arrayzero(h->capacity, sizeof(struct ext2_xattr),
1738 &h->attrs);
1739 if (err) {
1740 ext2fs_free_mem(&h);
1741 return err;
1742 }
1743 h->count = 0;
1744 h->ino = ino;
1745 h->fs = fs;
1746 *handle = h;
1747 return 0;
1748 }
1749
ext2fs_xattrs_close(struct ext2_xattr_handle ** handle)1750 errcode_t ext2fs_xattrs_close(struct ext2_xattr_handle **handle)
1751 {
1752 struct ext2_xattr_handle *h = *handle;
1753
1754 EXT2_CHECK_MAGIC(h, EXT2_ET_MAGIC_EA_HANDLE);
1755 xattrs_free_keys(h);
1756 ext2fs_free_mem(&h->attrs);
1757 ext2fs_free_mem(handle);
1758 return 0;
1759 }
1760
ext2fs_xattrs_count(struct ext2_xattr_handle * handle,size_t * count)1761 errcode_t ext2fs_xattrs_count(struct ext2_xattr_handle *handle, size_t *count)
1762 {
1763 EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EA_HANDLE);
1764 *count = handle->count;
1765 return 0;
1766 }
1767
ext2fs_xattrs_flags(struct ext2_xattr_handle * handle,unsigned int * new_flags,unsigned int * old_flags)1768 errcode_t ext2fs_xattrs_flags(struct ext2_xattr_handle *handle,
1769 unsigned int *new_flags, unsigned int *old_flags)
1770 {
1771 EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EA_HANDLE);
1772 if (old_flags)
1773 *old_flags = handle->flags;
1774 if (new_flags)
1775 handle->flags = *new_flags;
1776 return 0;
1777 }
1778