blob: a2cd3b88cc15b5396e8c8811decc58a6bd6e916a [file] [log] [blame]
Colin Crossec0a2e82010-06-11 14:21:37 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Tao Bao06ca8112016-10-05 12:44:18 -070017#include "contents.h"
18
Colin Crossec0a2e82010-06-11 14:21:37 -070019#include <sys/stat.h>
20#include <string.h>
21#include <stdio.h>
Nick Kralevich3ebdcb82013-02-22 19:39:21 -080022
Tao Bao06ca8112016-10-05 12:44:18 -070023#include "allocate.h"
24#include "ext4_utils/ext4_utils.h"
25#include "ext4_utils/make_ext4fs.h"
26#include "extent.h"
27#include "indirect.h"
28
Elliott Hughescf27ba92015-07-31 08:49:29 -070029#ifdef __ANDROID__
Nick Kralevich8a632322013-03-01 13:24:44 -080030#include <linux/capability.h>
31#else
32#include <private/android_filesystem_capability.h>
33#endif
34
Nick Kralevich3ebdcb82013-02-22 19:39:21 -080035#define XATTR_SELINUX_SUFFIX "selinux"
36#define XATTR_CAPS_SUFFIX "capability"
Colin Crossec0a2e82010-06-11 14:21:37 -070037
Elliott Hughes1eb2f542016-10-05 09:44:48 -070038#ifdef _WIN32
Raphael Moll4605b3f2012-02-03 23:02:33 -080039#define S_IFLNK 0 /* used by make_link, not needed under mingw */
40#endif
41
Doug Zongkerbec598e2014-08-12 11:35:37 -070042static struct block_allocation* saved_allocation_head = NULL;
43
44struct block_allocation* get_saved_allocation_chain() {
45 return saved_allocation_head;
46}
47
Colin Crossec0a2e82010-06-11 14:21:37 -070048static u32 dentry_size(u32 entries, struct dentry *dentries)
49{
50 u32 len = 24;
51 unsigned int i;
Colin Cross6bd2b5d2010-08-05 11:56:38 -070052 unsigned int dentry_len;
Colin Crossec0a2e82010-06-11 14:21:37 -070053
54 for (i = 0; i < entries; i++) {
Paul Lawrence40ce87a2014-03-03 10:51:58 -080055 dentry_len = 8 + EXT4_ALIGN(strlen(dentries[i].filename), 4);
Colin Crossec0a2e82010-06-11 14:21:37 -070056 if (len % info.block_size + dentry_len > info.block_size)
57 len += info.block_size - (len % info.block_size);
58 len += dentry_len;
59 }
60
61 return len;
62}
63
Colin Crossec0a2e82010-06-11 14:21:37 -070064static struct ext4_dir_entry_2 *add_dentry(u8 *data, u32 *offset,
65 struct ext4_dir_entry_2 *prev, u32 inode, const char *name,
66 u8 file_type)
67{
68 u8 name_len = strlen(name);
Paul Lawrence40ce87a2014-03-03 10:51:58 -080069 u16 rec_len = 8 + EXT4_ALIGN(name_len, 4);
Colin Crossec0a2e82010-06-11 14:21:37 -070070 struct ext4_dir_entry_2 *dentry;
71
72 u32 start_block = *offset / info.block_size;
Colin Cross2ff1c5b2011-01-25 21:44:37 -080073 u32 end_block = (*offset + rec_len - 1) / info.block_size;
Colin Crossec0a2e82010-06-11 14:21:37 -070074 if (start_block != end_block) {
75 /* Adding this dentry will cross a block boundary, so pad the previous
76 dentry to the block boundary */
77 if (!prev)
78 critical_error("no prev");
79 prev->rec_len += end_block * info.block_size - *offset;
80 *offset = end_block * info.block_size;
81 }
82
83 dentry = (struct ext4_dir_entry_2 *)(data + *offset);
84 dentry->inode = inode;
85 dentry->rec_len = rec_len;
86 dentry->name_len = name_len;
87 dentry->file_type = file_type;
88 memcpy(dentry->name, name, name_len);
89
90 *offset += rec_len;
91 return dentry;
92}
93
94/* Creates a directory structure for an array of directory entries, dentries,
95 and stores the location of the structure in an inode. The new inode's
96 .. link is set to dir_inode_num. Stores the location of the inode number
97 of each directory entry into dentries[i].inode, to be filled in later
98 when the inode for the entry is allocated. Returns the inode number of the
99 new directory */
100u32 make_directory(u32 dir_inode_num, u32 entries, struct dentry *dentries,
Colin Cross8aef66d2010-06-20 23:22:12 -0700101 u32 dirs)
Colin Crossec0a2e82010-06-11 14:21:37 -0700102{
103 struct ext4_inode *inode;
Colin Cross6bd2b5d2010-08-05 11:56:38 -0700104 u32 blocks;
105 u32 len;
Colin Crossec0a2e82010-06-11 14:21:37 -0700106 u32 offset = 0;
107 u32 inode_num;
108 u8 *data;
109 unsigned int i;
110 struct ext4_dir_entry_2 *dentry;
111
Colin Cross6bd2b5d2010-08-05 11:56:38 -0700112 blocks = DIV_ROUND_UP(dentry_size(entries, dentries), info.block_size);
113 len = blocks * info.block_size;
114
Colin Crossec0a2e82010-06-11 14:21:37 -0700115 if (dir_inode_num) {
116 inode_num = allocate_inode(info);
117 } else {
118 dir_inode_num = EXT4_ROOT_INO;
119 inode_num = EXT4_ROOT_INO;
120 }
121
122 if (inode_num == EXT4_ALLOCATE_FAILED) {
123 error("failed to allocate inode\n");
124 return EXT4_ALLOCATE_FAILED;
125 }
126
127 add_directory(inode_num);
128
129 inode = get_inode(inode_num);
130 if (inode == NULL) {
131 error("failed to get inode %u", inode_num);
132 return EXT4_ALLOCATE_FAILED;
133 }
134
135 data = inode_allocate_data_extents(inode, len, len);
136 if (data == NULL) {
Colin Cross2e905e52010-12-29 14:20:53 -0800137 error("failed to allocate %u extents", len);
Colin Crossec0a2e82010-06-11 14:21:37 -0700138 return EXT4_ALLOCATE_FAILED;
139 }
140
141 inode->i_mode = S_IFDIR;
142 inode->i_links_count = dirs + 2;
143 inode->i_flags |= aux_info.default_i_flags;
144
145 dentry = NULL;
146
147 dentry = add_dentry(data, &offset, NULL, inode_num, ".", EXT4_FT_DIR);
148 if (!dentry) {
149 error("failed to add . directory");
150 return EXT4_ALLOCATE_FAILED;
151 }
152
153 dentry = add_dentry(data, &offset, dentry, dir_inode_num, "..", EXT4_FT_DIR);
154 if (!dentry) {
155 error("failed to add .. directory");
156 return EXT4_ALLOCATE_FAILED;
157 }
158
159 for (i = 0; i < entries; i++) {
Colin Cross6bd2b5d2010-08-05 11:56:38 -0700160 dentry = add_dentry(data, &offset, dentry, 0,
161 dentries[i].filename, dentries[i].file_type);
162 if (offset > len || (offset == len && i != entries - 1))
163 critical_error("internal error: dentry for %s ends at %d, past %d\n",
164 dentries[i].filename, offset, len);
Colin Crossec0a2e82010-06-11 14:21:37 -0700165 dentries[i].inode = &dentry->inode;
166 if (!dentry) {
167 error("failed to add directory");
168 return EXT4_ALLOCATE_FAILED;
169 }
170 }
171
Colin Cross87489382012-11-20 14:07:39 -0800172 /* pad the last dentry out to the end of the block */
173 dentry->rec_len += len - offset;
Colin Crossec0a2e82010-06-11 14:21:37 -0700174
175 return inode_num;
176}
177
178/* Creates a file on disk. Returns the inode number of the new file */
179u32 make_file(const char *filename, u64 len)
180{
181 struct ext4_inode *inode;
182 u32 inode_num;
183
184 inode_num = allocate_inode(info);
185 if (inode_num == EXT4_ALLOCATE_FAILED) {
186 error("failed to allocate inode\n");
187 return EXT4_ALLOCATE_FAILED;
188 }
189
190 inode = get_inode(inode_num);
191 if (inode == NULL) {
192 error("failed to get inode %u", inode_num);
193 return EXT4_ALLOCATE_FAILED;
194 }
195
Doug Zongkerbec598e2014-08-12 11:35:37 -0700196 if (len > 0) {
197 struct block_allocation* alloc = inode_allocate_file_extents(inode, len, filename);
Doug Zongker99221352014-08-12 16:55:56 -0700198 if (alloc) {
199 alloc->filename = strdup(filename);
200 alloc->next = saved_allocation_head;
201 saved_allocation_head = alloc;
202 }
Doug Zongkerbec598e2014-08-12 11:35:37 -0700203 }
Colin Crossec0a2e82010-06-11 14:21:37 -0700204
205 inode->i_mode = S_IFREG;
206 inode->i_links_count = 1;
207 inode->i_flags |= aux_info.default_i_flags;
208
209 return inode_num;
210}
211
212/* Creates a file on disk. Returns the inode number of the new file */
Nick Kralevich5446bde2013-02-19 19:05:47 -0800213u32 make_link(const char *link)
Colin Crossec0a2e82010-06-11 14:21:37 -0700214{
215 struct ext4_inode *inode;
216 u32 inode_num;
217 u32 len = strlen(link);
218
219 inode_num = allocate_inode(info);
220 if (inode_num == EXT4_ALLOCATE_FAILED) {
221 error("failed to allocate inode\n");
222 return EXT4_ALLOCATE_FAILED;
223 }
224
225 inode = get_inode(inode_num);
226 if (inode == NULL) {
227 error("failed to get inode %u", inode_num);
228 return EXT4_ALLOCATE_FAILED;
229 }
230
231 inode->i_mode = S_IFLNK;
232 inode->i_links_count = 1;
233 inode->i_flags |= aux_info.default_i_flags;
234 inode->i_size_lo = len;
235
236 if (len + 1 <= sizeof(inode->i_block)) {
237 /* Fast symlink */
238 memcpy((char*)inode->i_block, link, len);
239 } else {
240 u8 *data = inode_allocate_data_indirect(inode, info.block_size, info.block_size);
241 memcpy(data, link, len);
242 inode->i_blocks_lo = info.block_size / 512;
243 }
244
245 return inode_num;
246}
247
Colin Crossde61f982010-08-04 15:06:09 -0700248int inode_set_permissions(u32 inode_num, u16 mode, u16 uid, u16 gid, u32 mtime)
Colin Crossec0a2e82010-06-11 14:21:37 -0700249{
250 struct ext4_inode *inode = get_inode(inode_num);
251
252 if (!inode)
253 return -1;
254
255 inode->i_mode |= mode;
256 inode->i_uid = uid;
257 inode->i_gid = gid;
Colin Crossde61f982010-08-04 15:06:09 -0700258 inode->i_mtime = mtime;
259 inode->i_atime = mtime;
260 inode->i_ctime = mtime;
Colin Crossec0a2e82010-06-11 14:21:37 -0700261
262 return 0;
263}
Stephen Smalleyb4eca4b2012-01-13 09:00:56 -0500264
Nick Kralevich4df62f32013-02-07 14:21:34 -0800265/*
266 * Returns the amount of free space available in the specified
267 * xattr region
268 */
269static size_t xattr_free_space(struct ext4_xattr_entry *entry, char *end)
Stephen Smalleyb4eca4b2012-01-13 09:00:56 -0500270{
Mark Salyzyn9036d552015-04-06 12:37:28 -0700271 end -= sizeof(uint32_t); /* Required four null bytes */
Nick Kralevich4df62f32013-02-07 14:21:34 -0800272 while(!IS_LAST_ENTRY(entry) && (((char *) entry) < end)) {
273 end -= EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size));
274 entry = EXT4_XATTR_NEXT(entry);
275 }
Stephen Smalleyb4eca4b2012-01-13 09:00:56 -0500276
Nick Kralevich4df62f32013-02-07 14:21:34 -0800277 if (((char *) entry) > end) {
278 error("unexpected read beyond end of xattr space");
Stephen Smalleyb4eca4b2012-01-13 09:00:56 -0500279 return 0;
Nick Kralevich4df62f32013-02-07 14:21:34 -0800280 }
Stephen Smalleyb4eca4b2012-01-13 09:00:56 -0500281
Mark Salyzyn9036d552015-04-06 12:37:28 -0700282 return end - ((char *) entry);
Nick Kralevich4df62f32013-02-07 14:21:34 -0800283}
284
285/*
286 * Returns a pointer to the free space immediately after the
287 * last xattr element
288 */
289static struct ext4_xattr_entry* xattr_get_last(struct ext4_xattr_entry *entry)
290{
291 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
292 // skip entry
293 }
294 return entry;
295}
296
297/*
298 * assert that the elements in the ext4 xattr section are in sorted order
299 *
300 * The ext4 filesystem requires extended attributes to be sorted when
301 * they're not stored in the inode. The kernel ext4 code uses the following
302 * sorting algorithm:
303 *
304 * 1) First sort extended attributes by their name_index. For example,
305 * EXT4_XATTR_INDEX_USER (1) comes before EXT4_XATTR_INDEX_SECURITY (6).
306 * 2) If the name_indexes are equal, then sorting is based on the length
307 * of the name. For example, XATTR_SELINUX_SUFFIX ("selinux") comes before
308 * XATTR_CAPS_SUFFIX ("capability") because "selinux" is shorter than "capability"
309 * 3) If the name_index and name_length are equal, then memcmp() is used to determine
310 * which name comes first. For example, "selinux" would come before "yelinux".
311 *
312 * This method is intended to implement the sorting function defined in
313 * the Linux kernel file fs/ext4/xattr.c function ext4_xattr_find_entry().
314 */
315static void xattr_assert_sane(struct ext4_xattr_entry *entry)
316{
317 for( ; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
318 struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(entry);
319 if (IS_LAST_ENTRY(next)) {
320 return;
321 }
322
323 int cmp = next->e_name_index - entry->e_name_index;
324 if (cmp == 0)
325 cmp = next->e_name_len - entry->e_name_len;
326 if (cmp == 0)
327 cmp = memcmp(next->e_name, entry->e_name, next->e_name_len);
328 if (cmp < 0) {
329 error("BUG: extended attributes are not sorted\n");
330 return;
331 }
332 if (cmp == 0) {
333 error("BUG: duplicate extended attributes detected\n");
334 return;
335 }
336 }
337}
338
339#define NAME_HASH_SHIFT 5
340#define VALUE_HASH_SHIFT 16
341
342static void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
343 struct ext4_xattr_entry *entry)
344{
Colin Cross7900c772014-01-23 12:53:30 -0800345 u32 hash = 0;
Nick Kralevich4df62f32013-02-07 14:21:34 -0800346 char *name = entry->e_name;
347 int n;
348
349 for (n = 0; n < entry->e_name_len; n++) {
350 hash = (hash << NAME_HASH_SHIFT) ^
351 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
352 *name++;
353 }
354
355 if (entry->e_value_block == 0 && entry->e_value_size != 0) {
Colin Cross7900c772014-01-23 12:53:30 -0800356 u32 *value = (u32 *)((char *)header +
Nick Kralevich4df62f32013-02-07 14:21:34 -0800357 le16_to_cpu(entry->e_value_offs));
358 for (n = (le32_to_cpu(entry->e_value_size) +
359 EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
360 hash = (hash << VALUE_HASH_SHIFT) ^
361 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
362 le32_to_cpu(*value++);
363 }
364 }
365 entry->e_hash = cpu_to_le32(hash);
366}
367
368#undef NAME_HASH_SHIFT
369#undef VALUE_HASH_SHIFT
370
371static struct ext4_xattr_entry* xattr_addto_range(
372 void *block_start,
373 void *block_end,
374 struct ext4_xattr_entry *first,
375 int name_index,
376 const char *name,
377 const void *value,
378 size_t value_len)
379{
380 size_t name_len = strlen(name);
381 if (name_len > 255)
382 return NULL;
383
384 size_t available_size = xattr_free_space(first, block_end);
385 size_t needed_size = EXT4_XATTR_LEN(name_len) + EXT4_XATTR_SIZE(value_len);
386
387 if (needed_size > available_size)
388 return NULL;
389
390 struct ext4_xattr_entry *new_entry = xattr_get_last(first);
391 memset(new_entry, 0, EXT4_XATTR_LEN(name_len));
392
393 new_entry->e_name_len = name_len;
394 new_entry->e_name_index = name_index;
395 memcpy(new_entry->e_name, name, name_len);
396 new_entry->e_value_block = 0;
397 new_entry->e_value_size = cpu_to_le32(value_len);
398
399 char *val = (char *) new_entry + available_size - EXT4_XATTR_SIZE(value_len);
400 size_t e_value_offs = val - (char *) block_start;
401
402 new_entry->e_value_offs = cpu_to_le16(e_value_offs);
403 memset(val, 0, EXT4_XATTR_SIZE(value_len));
404 memcpy(val, value, value_len);
405
406 xattr_assert_sane(first);
407 return new_entry;
408}
409
410static int xattr_addto_inode(struct ext4_inode *inode, int name_index,
411 const char *name, const void *value, size_t value_len)
412{
413 struct ext4_xattr_ibody_header *hdr = (struct ext4_xattr_ibody_header *) (inode + 1);
414 struct ext4_xattr_entry *first = (struct ext4_xattr_entry *) (hdr + 1);
415 char *block_end = ((char *) inode) + info.inode_size;
416
417 struct ext4_xattr_entry *result =
418 xattr_addto_range(first, block_end, first, name_index, name, value, value_len);
419
420 if (result == NULL)
Stephen Smalleyb4eca4b2012-01-13 09:00:56 -0500421 return -1;
422
Nick Kralevich4df62f32013-02-07 14:21:34 -0800423 hdr->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
Stephen Smalleyb4eca4b2012-01-13 09:00:56 -0500424 inode->i_extra_isize = cpu_to_le16(sizeof(struct ext4_inode) - EXT4_GOOD_OLD_INODE_SIZE);
425
426 return 0;
427}
Nick Kralevich4df62f32013-02-07 14:21:34 -0800428
429static int xattr_addto_block(struct ext4_inode *inode, int name_index,
430 const char *name, const void *value, size_t value_len)
431{
432 struct ext4_xattr_header *header = get_xattr_block_for_inode(inode);
433 if (!header)
434 return -1;
435
436 struct ext4_xattr_entry *first = (struct ext4_xattr_entry *) (header + 1);
437 char *block_end = ((char *) header) + info.block_size;
438
439 struct ext4_xattr_entry *result =
440 xattr_addto_range(header, block_end, first, name_index, name, value, value_len);
441
442 if (result == NULL)
443 return -1;
444
445 ext4_xattr_hash_entry(header, result);
446 return 0;
447}
448
449
450static int xattr_add(u32 inode_num, int name_index, const char *name,
451 const void *value, size_t value_len)
452{
453 if (!value)
454 return 0;
455
456 struct ext4_inode *inode = get_inode(inode_num);
457
458 if (!inode)
459 return -1;
460
461 int result = xattr_addto_inode(inode, name_index, name, value, value_len);
462 if (result != 0) {
463 result = xattr_addto_block(inode, name_index, name, value, value_len);
464 }
465 return result;
466}
467
468int inode_set_selinux(u32 inode_num, const char *secon)
469{
Sungmin Choi192238d2013-02-25 15:37:51 +0900470 if (!secon)
471 return 0;
472
Nick Kralevich4df62f32013-02-07 14:21:34 -0800473 return xattr_add(inode_num, EXT4_XATTR_INDEX_SECURITY,
474 XATTR_SELINUX_SUFFIX, secon, strlen(secon) + 1);
475}
476
477int inode_set_capabilities(u32 inode_num, uint64_t capabilities) {
478 if (capabilities == 0)
479 return 0;
480
481 struct vfs_cap_data cap_data;
482 memset(&cap_data, 0, sizeof(cap_data));
483
484 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
485 cap_data.data[0].permitted = (uint32_t) (capabilities & 0xffffffff);
486 cap_data.data[0].inheritable = 0;
487 cap_data.data[1].permitted = (uint32_t) (capabilities >> 32);
488 cap_data.data[1].inheritable = 0;
489
490 return xattr_add(inode_num, EXT4_XATTR_INDEX_SECURITY,
491 XATTR_CAPS_SUFFIX, &cap_data, sizeof(cap_data));
492}