Remove dead selinux code
[project/make_ext4fs.git] / contents.c
1 /*
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
17 #include <sys/stat.h>
18 #include <string.h>
19 #include <stdio.h>
20
21 #include <private/android_filesystem_capability.h>
22
23 #define XATTR_SELINUX_SUFFIX "selinux"
24 #define XATTR_CAPS_SUFFIX "capability"
25
26 #include "ext4_utils.h"
27 #include "make_ext4fs.h"
28 #include "allocate.h"
29 #include "contents.h"
30 #include "extent.h"
31 #include "indirect.h"
32
33 #ifdef USE_MINGW
34 #define S_IFLNK 0 /* used by make_link, not needed under mingw */
35 #endif
36
37 static struct block_allocation* saved_allocation_head = NULL;
38
39 struct block_allocation* get_saved_allocation_chain() {
40 return saved_allocation_head;
41 }
42
43 static u32 dentry_size(u32 entries, struct dentry *dentries)
44 {
45 u32 len = 24;
46 unsigned int i;
47 unsigned int dentry_len;
48
49 for (i = 0; i < entries; i++) {
50 dentry_len = 8 + EXT4_ALIGN(strlen(dentries[i].filename), 4);
51 if (len % info.block_size + dentry_len > info.block_size)
52 len += info.block_size - (len % info.block_size);
53 len += dentry_len;
54 }
55
56 return len;
57 }
58
59 static struct ext4_dir_entry_2 *add_dentry(u8 *data, u32 *offset,
60 struct ext4_dir_entry_2 *prev, u32 inode, const char *name,
61 u8 file_type)
62 {
63 u8 name_len = strlen(name);
64 u16 rec_len = 8 + EXT4_ALIGN(name_len, 4);
65 struct ext4_dir_entry_2 *dentry;
66
67 u32 start_block = *offset / info.block_size;
68 u32 end_block = (*offset + rec_len - 1) / info.block_size;
69 if (start_block != end_block) {
70 /* Adding this dentry will cross a block boundary, so pad the previous
71 dentry to the block boundary */
72 if (!prev)
73 critical_error("no prev");
74 prev->rec_len += end_block * info.block_size - *offset;
75 *offset = end_block * info.block_size;
76 }
77
78 dentry = (struct ext4_dir_entry_2 *)(data + *offset);
79 dentry->inode = inode;
80 dentry->rec_len = rec_len;
81 dentry->name_len = name_len;
82 dentry->file_type = file_type;
83 memcpy(dentry->name, name, name_len);
84
85 *offset += rec_len;
86 return dentry;
87 }
88
89 /* Creates a directory structure for an array of directory entries, dentries,
90 and stores the location of the structure in an inode. The new inode's
91 .. link is set to dir_inode_num. Stores the location of the inode number
92 of each directory entry into dentries[i].inode, to be filled in later
93 when the inode for the entry is allocated. Returns the inode number of the
94 new directory */
95 u32 make_directory(u32 dir_inode_num, u32 entries, struct dentry *dentries,
96 u32 dirs)
97 {
98 struct ext4_inode *inode;
99 u32 blocks;
100 u32 len;
101 u32 offset = 0;
102 u32 inode_num;
103 u8 *data;
104 unsigned int i;
105 struct ext4_dir_entry_2 *dentry;
106
107 blocks = DIV_ROUND_UP(dentry_size(entries, dentries), info.block_size);
108 len = blocks * info.block_size;
109
110 if (dir_inode_num) {
111 inode_num = allocate_inode(info);
112 } else {
113 dir_inode_num = EXT4_ROOT_INO;
114 inode_num = EXT4_ROOT_INO;
115 }
116
117 if (inode_num == EXT4_ALLOCATE_FAILED) {
118 error("failed to allocate inode\n");
119 return EXT4_ALLOCATE_FAILED;
120 }
121
122 add_directory(inode_num);
123
124 inode = get_inode(inode_num);
125 if (inode == NULL) {
126 error("failed to get inode %u", inode_num);
127 return EXT4_ALLOCATE_FAILED;
128 }
129
130 data = inode_allocate_data_extents(inode, len, len);
131 if (data == NULL) {
132 error("failed to allocate %u extents", len);
133 return EXT4_ALLOCATE_FAILED;
134 }
135
136 inode->i_mode = S_IFDIR;
137 inode->i_links_count = dirs + 2;
138 inode->i_flags |= aux_info.default_i_flags;
139
140 dentry = NULL;
141
142 dentry = add_dentry(data, &offset, NULL, inode_num, ".", EXT4_FT_DIR);
143 if (!dentry) {
144 error("failed to add . directory");
145 return EXT4_ALLOCATE_FAILED;
146 }
147
148 dentry = add_dentry(data, &offset, dentry, dir_inode_num, "..", EXT4_FT_DIR);
149 if (!dentry) {
150 error("failed to add .. directory");
151 return EXT4_ALLOCATE_FAILED;
152 }
153
154 for (i = 0; i < entries; i++) {
155 dentry = add_dentry(data, &offset, dentry, 0,
156 dentries[i].filename, dentries[i].file_type);
157 if (offset > len || (offset == len && i != entries - 1))
158 critical_error("internal error: dentry for %s ends at %d, past %d\n",
159 dentries[i].filename, offset, len);
160 dentries[i].inode = &dentry->inode;
161 if (!dentry) {
162 error("failed to add directory");
163 return EXT4_ALLOCATE_FAILED;
164 }
165 }
166
167 /* pad the last dentry out to the end of the block */
168 dentry->rec_len += len - offset;
169
170 return inode_num;
171 }
172
173 /* Creates a file on disk. Returns the inode number of the new file */
174 u32 make_file(const char *filename, u64 len)
175 {
176 struct ext4_inode *inode;
177 u32 inode_num;
178
179 inode_num = allocate_inode(info);
180 if (inode_num == EXT4_ALLOCATE_FAILED) {
181 error("failed to allocate inode\n");
182 return EXT4_ALLOCATE_FAILED;
183 }
184
185 inode = get_inode(inode_num);
186 if (inode == NULL) {
187 error("failed to get inode %u", inode_num);
188 return EXT4_ALLOCATE_FAILED;
189 }
190
191 if (len > 0) {
192 struct block_allocation* alloc = inode_allocate_file_extents(inode, len, filename);
193 if (alloc) {
194 alloc->filename = strdup(filename);
195 alloc->next = saved_allocation_head;
196 saved_allocation_head = alloc;
197 }
198 }
199
200 inode->i_mode = S_IFREG;
201 inode->i_links_count = 1;
202 inode->i_flags |= aux_info.default_i_flags;
203
204 return inode_num;
205 }
206
207 /* Creates a file on disk. Returns the inode number of the new file */
208 u32 make_link(const char *link)
209 {
210 struct ext4_inode *inode;
211 u32 inode_num;
212 u32 len = strlen(link);
213
214 inode_num = allocate_inode(info);
215 if (inode_num == EXT4_ALLOCATE_FAILED) {
216 error("failed to allocate inode\n");
217 return EXT4_ALLOCATE_FAILED;
218 }
219
220 inode = get_inode(inode_num);
221 if (inode == NULL) {
222 error("failed to get inode %u", inode_num);
223 return EXT4_ALLOCATE_FAILED;
224 }
225
226 inode->i_mode = S_IFLNK;
227 inode->i_links_count = 1;
228 inode->i_flags |= aux_info.default_i_flags;
229 inode->i_size_lo = len;
230
231 if (len + 1 <= sizeof(inode->i_block)) {
232 /* Fast symlink */
233 memcpy((char*)inode->i_block, link, len);
234 } else {
235 u8 *data = inode_allocate_data_indirect(inode, info.block_size, info.block_size);
236 memcpy(data, link, len);
237 inode->i_blocks_lo = info.block_size / 512;
238 }
239
240 return inode_num;
241 }
242
243 /* Creates a special file on disk. Returns the inode number of the new file */
244 u32 make_special(const char *path)
245 {
246 struct ext4_inode *inode;
247 struct stat s;
248 u32 inode_num;
249
250 if (stat(path, &s)) {
251 error("failed to stat file\n");
252 return EXT4_ALLOCATE_FAILED;
253 }
254
255 inode_num = allocate_inode(info);
256 if (inode_num == EXT4_ALLOCATE_FAILED) {
257 error("failed to allocate inode\n");
258 return EXT4_ALLOCATE_FAILED;
259 }
260
261 inode = get_inode(inode_num);
262 if (inode == NULL) {
263 error("failed to get inode %u", inode_num);
264 return EXT4_ALLOCATE_FAILED;
265 }
266
267 inode->i_mode = s.st_mode & S_IFMT;
268 inode->i_links_count = 1;
269 inode->i_flags |= aux_info.default_i_flags;
270
271 ((u8 *)inode->i_block)[0] = major(s.st_rdev);
272 ((u8 *)inode->i_block)[1] = minor(s.st_rdev);
273
274 return inode_num;
275 }
276
277 int inode_set_permissions(u32 inode_num, u16 mode, u16 uid, u16 gid, u32 mtime)
278 {
279 struct ext4_inode *inode = get_inode(inode_num);
280
281 if (!inode)
282 return -1;
283
284 inode->i_mode |= mode;
285 inode->i_uid = uid;
286 inode->i_gid = gid;
287 inode->i_mtime = mtime;
288 inode->i_atime = mtime;
289 inode->i_ctime = mtime;
290
291 return 0;
292 }
293
294 /*
295 * Returns the amount of free space available in the specified
296 * xattr region
297 */
298 static size_t xattr_free_space(struct ext4_xattr_entry *entry, char *end)
299 {
300 while(!IS_LAST_ENTRY(entry) && (((char *) entry) < end)) {
301 end -= EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size));
302 entry = EXT4_XATTR_NEXT(entry);
303 }
304
305 if (((char *) entry) > end) {
306 error("unexpected read beyond end of xattr space");
307 return 0;
308 }
309
310 return end - ((char *) entry);
311 }
312
313 /*
314 * Returns a pointer to the free space immediately after the
315 * last xattr element
316 */
317 static struct ext4_xattr_entry* xattr_get_last(struct ext4_xattr_entry *entry)
318 {
319 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
320 // skip entry
321 }
322 return entry;
323 }
324
325 /*
326 * assert that the elements in the ext4 xattr section are in sorted order
327 *
328 * The ext4 filesystem requires extended attributes to be sorted when
329 * they're not stored in the inode. The kernel ext4 code uses the following
330 * sorting algorithm:
331 *
332 * 1) First sort extended attributes by their name_index. For example,
333 * EXT4_XATTR_INDEX_USER (1) comes before EXT4_XATTR_INDEX_SECURITY (6).
334 * 2) If the name_indexes are equal, then sorting is based on the length
335 * of the name. For example, XATTR_SELINUX_SUFFIX ("selinux") comes before
336 * XATTR_CAPS_SUFFIX ("capability") because "selinux" is shorter than "capability"
337 * 3) If the name_index and name_length are equal, then memcmp() is used to determine
338 * which name comes first. For example, "selinux" would come before "yelinux".
339 *
340 * This method is intended to implement the sorting function defined in
341 * the Linux kernel file fs/ext4/xattr.c function ext4_xattr_find_entry().
342 */
343 static void xattr_assert_sane(struct ext4_xattr_entry *entry)
344 {
345 for( ; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
346 struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(entry);
347 if (IS_LAST_ENTRY(next)) {
348 return;
349 }
350
351 int cmp = next->e_name_index - entry->e_name_index;
352 if (cmp == 0)
353 cmp = next->e_name_len - entry->e_name_len;
354 if (cmp == 0)
355 cmp = memcmp(next->e_name, entry->e_name, next->e_name_len);
356 if (cmp < 0) {
357 error("BUG: extended attributes are not sorted\n");
358 return;
359 }
360 if (cmp == 0) {
361 error("BUG: duplicate extended attributes detected\n");
362 return;
363 }
364 }
365 }
366
367 #define NAME_HASH_SHIFT 5
368 #define VALUE_HASH_SHIFT 16
369
370 static void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
371 struct ext4_xattr_entry *entry)
372 {
373 u32 hash = 0;
374 char *name = entry->e_name;
375 int n;
376
377 for (n = 0; n < entry->e_name_len; n++) {
378 hash = (hash << NAME_HASH_SHIFT) ^
379 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
380 *name++;
381 }
382
383 if (entry->e_value_block == 0 && entry->e_value_size != 0) {
384 u32 *value = (u32 *)((char *)header +
385 le16_to_cpu(entry->e_value_offs));
386 for (n = (le32_to_cpu(entry->e_value_size) +
387 EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
388 hash = (hash << VALUE_HASH_SHIFT) ^
389 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
390 le32_to_cpu(*value++);
391 }
392 }
393 entry->e_hash = cpu_to_le32(hash);
394 }
395
396 #undef NAME_HASH_SHIFT
397 #undef VALUE_HASH_SHIFT
398
399 static struct ext4_xattr_entry* xattr_addto_range(
400 void *block_start,
401 void *block_end,
402 struct ext4_xattr_entry *first,
403 int name_index,
404 const char *name,
405 const void *value,
406 size_t value_len)
407 {
408 size_t name_len = strlen(name);
409 if (name_len > 255)
410 return NULL;
411
412 size_t available_size = xattr_free_space(first, block_end);
413 size_t needed_size = EXT4_XATTR_LEN(name_len) + EXT4_XATTR_SIZE(value_len);
414
415 if (needed_size > available_size)
416 return NULL;
417
418 struct ext4_xattr_entry *new_entry = xattr_get_last(first);
419 memset(new_entry, 0, EXT4_XATTR_LEN(name_len));
420
421 new_entry->e_name_len = name_len;
422 new_entry->e_name_index = name_index;
423 memcpy(new_entry->e_name, name, name_len);
424 new_entry->e_value_block = 0;
425 new_entry->e_value_size = cpu_to_le32(value_len);
426
427 char *val = (char *) new_entry + available_size - EXT4_XATTR_SIZE(value_len);
428 size_t e_value_offs = val - (char *) block_start;
429
430 new_entry->e_value_offs = cpu_to_le16(e_value_offs);
431 memset(val, 0, EXT4_XATTR_SIZE(value_len));
432 memcpy(val, value, value_len);
433
434 xattr_assert_sane(first);
435 return new_entry;
436 }
437
438 static int xattr_addto_inode(struct ext4_inode *inode, int name_index,
439 const char *name, const void *value, size_t value_len)
440 {
441 struct ext4_xattr_ibody_header *hdr = (struct ext4_xattr_ibody_header *) (inode + 1);
442 struct ext4_xattr_entry *first = (struct ext4_xattr_entry *) (hdr + 1);
443 char *block_end = ((char *) inode) + info.inode_size;
444
445 struct ext4_xattr_entry *result =
446 xattr_addto_range(first, block_end, first, name_index, name, value, value_len);
447
448 if (result == NULL)
449 return -1;
450
451 hdr->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
452 inode->i_extra_isize = cpu_to_le16(sizeof(struct ext4_inode) - EXT4_GOOD_OLD_INODE_SIZE);
453
454 return 0;
455 }
456
457 static int xattr_addto_block(struct ext4_inode *inode, int name_index,
458 const char *name, const void *value, size_t value_len)
459 {
460 struct ext4_xattr_header *header = get_xattr_block_for_inode(inode);
461 if (!header)
462 return -1;
463
464 struct ext4_xattr_entry *first = (struct ext4_xattr_entry *) (header + 1);
465 char *block_end = ((char *) header) + info.block_size;
466
467 struct ext4_xattr_entry *result =
468 xattr_addto_range(header, block_end, first, name_index, name, value, value_len);
469
470 if (result == NULL)
471 return -1;
472
473 ext4_xattr_hash_entry(header, result);
474 return 0;
475 }
476
477
478 static int xattr_add(u32 inode_num, int name_index, const char *name,
479 const void *value, size_t value_len)
480 {
481 if (!value)
482 return 0;
483
484 struct ext4_inode *inode = get_inode(inode_num);
485
486 if (!inode)
487 return -1;
488
489 int result = xattr_addto_inode(inode, name_index, name, value, value_len);
490 if (result != 0) {
491 result = xattr_addto_block(inode, name_index, name, value, value_len);
492 }
493 return result;
494 }
495
496 int inode_set_capabilities(u32 inode_num, uint64_t capabilities) {
497 if (capabilities == 0)
498 return 0;
499
500 struct vfs_cap_data cap_data;
501 memset(&cap_data, 0, sizeof(cap_data));
502
503 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
504 cap_data.data[0].permitted = (uint32_t) (capabilities & 0xffffffff);
505 cap_data.data[0].inheritable = 0;
506 cap_data.data[1].permitted = (uint32_t) (capabilities >> 32);
507 cap_data.data[1].inheritable = 0;
508
509 return xattr_add(inode_num, EXT4_XATTR_INDEX_SECURITY,
510 XATTR_CAPS_SUFFIX, &cap_data, sizeof(cap_data));
511 }