• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #define _GNU_SOURCE
2 
3 #include "config.h"
4 #include <stdio.h>
5 #include <getopt.h>
6 #include <string.h>
7 #include <unistd.h>
8 #include <limits.h>
9 
10 #include "perms.h"
11 #include "base_fs.h"
12 #include "block_list.h"
13 #include "basefs_allocator.h"
14 #include "create_inode.h"
15 
16 #ifndef UID_GID_MAP_MAX_EXTENTS
17 /*
18  * The value is defined in linux/user_namspace.h.
19  * The value is (arbitrarily) 5 in 4.14 and earlier, or 340 in 4.15 and later.
20  * Here, the bigger value is taken. See also man user_namespace(7).
21  */
22 #define UID_GID_MAP_MAX_EXTENTS 340
23 #endif
24 
25 static char *prog_name = "e2fsdroid";
26 static char *in_file;
27 static char *block_list;
28 static char *basefs_out;
29 static char *basefs_in;
30 static char *mountpoint = "";
31 static time_t fixed_time = -1;
32 static char *fs_config_file;
33 static struct selinux_opt seopt_file[8];
34 static int max_nr_opt = (int)sizeof(seopt_file) / sizeof(seopt_file[0]);
35 static char *product_out;
36 static char *src_dir;
37 static int android_configure;
38 static int android_sparse_file = 1;
39 
usage(int ret)40 static void usage(int ret)
41 {
42 	fprintf(stderr, "%s [-B block_list] [-D basefs_out] [-T timestamp]\n"
43 			"\t[-C fs_config] [-S file_contexts] [-p product_out]\n"
44 			"\t[-a mountpoint] [-d basefs_in] [-f src_dir] [-e] [-s]\n"
45 			"\t[-u uid-mapping] [-g gid-mapping] image\n",
46                 prog_name);
47 	exit(ret);
48 }
49 
absolute_path(const char * file)50 static char *absolute_path(const char *file)
51 {
52 	char *ret;
53 	char cwd[PATH_MAX];
54 
55 	if (file[0] != '/') {
56 		if (getcwd(cwd, PATH_MAX) == NULL) {
57 			fprintf(stderr, "Failed to getcwd\n");
58 			exit(EXIT_FAILURE);
59 		}
60 		ret = malloc(strlen(cwd) + 1 + strlen(file) + 1);
61 		if (ret)
62 			sprintf(ret, "%s/%s", cwd, file);
63 	} else
64 		ret = strdup(file);
65 	return ret;
66 }
67 
parse_ugid_map_entry(char * line,struct ugid_map_entry * result)68 static int parse_ugid_map_entry(char* line, struct ugid_map_entry* result)
69 {
70 	char *token, *token_saveptr;
71 	size_t num_tokens;
72 	unsigned int *parsed[] = {&result->child_id,
73 				  &result->parent_id,
74 				  &result->length};
75 	for (token = strtok_r(line, " ", &token_saveptr), num_tokens = 0;
76 	     token && num_tokens < 3;
77 	     token = strtok_r(NULL, " ", &token_saveptr), ++num_tokens) {
78 		char* endptr = NULL;
79 		unsigned long t = strtoul(token, &endptr, 10);
80 		if ((t == ULONG_MAX && errno) || (t > UINT_MAX) || *endptr) {
81 			fprintf(stderr, "Malformed u/gid mapping line\n");
82 			return 0;
83 		}
84 		*parsed[num_tokens] = (unsigned int) t;
85 	}
86 	if (num_tokens < 3 || strtok_r(NULL, " ", &token_saveptr) != NULL) {
87 		fprintf(stderr, "Malformed u/gid mapping line\n");
88 		return 0;
89 	}
90 	if (result->child_id + result->length < result->child_id ||
91 	    result->parent_id + result->length < result->parent_id) {
92 		fprintf(stderr, "u/gid mapping overflow\n");
93 		return 0;
94 	}
95 	return 1;
96 }
97 
98 /*
99  * Returns 1 if [begin1, begin1+length1) and [begin2, begin2+length2) have
100  * overlapping range. Otherwise 0.
101  */
is_overlapping(unsigned int begin1,unsigned int length1,unsigned int begin2,unsigned int length2)102 static int is_overlapping(unsigned int begin1, unsigned int length1,
103 			  unsigned int begin2, unsigned int length2)
104 {
105 	unsigned int end1 = begin1 + length1;
106 	unsigned int end2 = begin2 + length2;
107 	return !(end1 <= begin2 || end2 <= begin1);
108 }
109 
110 /*
111  * Verifies if the given mapping works.
112  * - Checks if the number of entries is less than or equals to
113  *   UID_GID_MAP_MAX_EXTENTS.
114  * - Checks if there is no overlapped ranges.
115  * Returns 1 if valid, otherwise 0.
116  */
is_valid_ugid_map(const struct ugid_map * mapping)117 static int is_valid_ugid_map(const struct ugid_map* mapping)
118 {
119 	size_t i, j;
120 
121 	if (mapping->size > UID_GID_MAP_MAX_EXTENTS) {
122 		fprintf(stderr, "too many u/gid mapping entries\n");
123 		return 0;
124 	}
125 
126 	for (i = 0; i < mapping->size; ++i) {
127 		const struct ugid_map_entry *entry1 = &mapping->entries[i];
128 		for (j = i + 1; j < mapping->size; ++j) {
129 			const struct ugid_map_entry *entry2 =
130 				&mapping->entries[j];
131 			if (is_overlapping(entry1->child_id, entry1->length,
132 					   entry2->child_id, entry2->length)) {
133 				fprintf(stderr,
134 					"Overlapping child u/gid: [%d %d %d],"
135 					" [%d %d %d]\n",
136 					entry1->child_id, entry1->parent_id,
137 					entry1->length, entry2->child_id,
138 					entry2->parent_id, entry2->length);
139 				return 0;
140 			}
141 			if (is_overlapping(entry1->parent_id, entry1->length,
142 					   entry2->parent_id, entry2->length)) {
143 				fprintf(stderr,
144 					"Overlapping parent u/gid: [%d %d %d],"
145 					" [%d %d %d]\n",
146 					entry1->child_id, entry1->parent_id,
147 					entry1->length, entry2->child_id,
148 					entry2->parent_id, entry2->length);
149 				return 0;
150 			}
151 		}
152 	}
153 	return 1;
154 }
155 
156 /*
157  * Parses the UID/GID mapping argument. The argument could be a multi-line
158  * string (separated by '\n', no trailing '\n' is allowed). Each line must
159  * contain exact three integer tokens; the first token is |child_id|,
160  * the second is |parent_id|, and the last is |length| of the mapping range.
161  * See also user_namespace(7) man page.
162  * On success, the parsed entries are stored in |result|, and it returns 1.
163  * Otherwise, returns 0.
164  */
parse_ugid_map(char * arg,struct ugid_map * result)165 static int parse_ugid_map(char* arg, struct ugid_map* result)
166 {
167 	int i;
168 	char *line, *line_saveptr;
169 	size_t current_index;
170 
171 	/* Count the number of lines. */
172 	result->size = 1;
173 	for (i = 0; arg[i]; ++i) {
174 		if (arg[i] == '\n')
175 			++result->size;
176 	}
177 
178 	/* Allocate memory for entries. */
179 	result->entries = malloc(sizeof(struct ugid_map_entry) * result->size);
180 	if (!result->entries) {
181 		result->size = 0;
182 		return 0;
183 	}
184 
185 	/* Parse each line */
186 	for (line = strtok_r(arg, "\n", &line_saveptr), current_index = 0;
187 	     line;
188 	     line = strtok_r(NULL, "\n", &line_saveptr), ++current_index) {
189 		if (!parse_ugid_map_entry(
190 			line, &result->entries[current_index])) {
191 			return 0;
192 		}
193 	}
194 
195 	return is_valid_ugid_map(result);
196 }
197 
main(int argc,char * argv[])198 int main(int argc, char *argv[])
199 {
200 	int c;
201 	char *p;
202 	int flags = EXT2_FLAG_RW;
203 	errcode_t retval;
204 	io_manager io_mgr;
205 	ext2_filsys fs = NULL;
206 	struct fs_ops_callbacks fs_callbacks = { NULL, NULL };
207 	char *token;
208 	int nr_opt = 0;
209 	ext2_ino_t inodes_count;
210 	ext2_ino_t free_inodes_count;
211 	blk64_t blocks_count;
212 	blk64_t free_blocks_count;
213 	struct ugid_map uid_map = { 0, NULL }, gid_map = { 0, NULL };
214 
215 	add_error_table(&et_ext2_error_table);
216 
217 	while ((c = getopt (argc, argv, "T:C:S:p:a:D:d:B:f:esu:g:")) != EOF) {
218 		switch (c) {
219 		case 'T':
220 			fixed_time = strtoul(optarg, &p, 0);
221 			android_configure = 1;
222 			break;
223 		case 'C':
224 			fs_config_file = absolute_path(optarg);
225 			android_configure = 1;
226 			break;
227 		case 'S':
228 			token = strtok(optarg, ",");
229 			while (token) {
230 				if (nr_opt == max_nr_opt) {
231 					fprintf(stderr, "Expected at most %d selinux opts\n",
232 						max_nr_opt);
233 					exit(EXIT_FAILURE);
234 				}
235 				seopt_file[nr_opt].type = SELABEL_OPT_PATH;
236 				seopt_file[nr_opt].value = absolute_path(token);
237 				nr_opt++;
238 				token = strtok(NULL, ",");
239 			}
240 			android_configure = 1;
241 			break;
242 		case 'p':
243 			product_out = absolute_path(optarg);
244 			android_configure = 1;
245 			break;
246 		case 'a':
247 			mountpoint = strdup(optarg);
248 			break;
249 		case 'D':
250 			basefs_out = absolute_path(optarg);
251 			break;
252 		case 'd':
253 			basefs_in = absolute_path(optarg);
254 			break;
255 		case 'B':
256 			block_list = absolute_path(optarg);
257 			break;
258 		case 'f':
259 			src_dir = absolute_path(optarg);
260 			break;
261 		case 'e':
262 			android_sparse_file = 0;
263 			break;
264 		case 's':
265 			flags |= EXT2_FLAG_SHARE_DUP;
266 			break;
267 		case 'u':
268 			if (!parse_ugid_map(optarg, &uid_map))
269 				exit(EXIT_FAILURE);
270 			android_configure = 1;
271 			break;
272 		case 'g':
273 			if (!parse_ugid_map(optarg, &gid_map))
274 				exit(EXIT_FAILURE);
275 			android_configure = 1;
276 			break;
277 		default:
278 			usage(EXIT_FAILURE);
279 		}
280 	}
281 	if (optind >= argc) {
282 		fprintf(stderr, "Expected filename after options\n");
283 		exit(EXIT_FAILURE);
284 	}
285 
286 	if (android_sparse_file) {
287 		io_mgr = sparse_io_manager;
288 		if (asprintf(&in_file, "(%s)", argv[optind]) == -1) {
289 			fprintf(stderr, "Failed to allocate file name\n");
290 			exit(EXIT_FAILURE);
291 		}
292 	} else {
293 		io_mgr = unix_io_manager;
294 		in_file = strdup(argv[optind]);
295 	}
296 	retval = ext2fs_open(in_file, flags, 0, 0, io_mgr, &fs);
297 	if (retval) {
298 		com_err(prog_name, retval, "while opening file %s\n", in_file);
299 		return retval;
300 	}
301 
302 	if (src_dir) {
303 		ext2fs_read_bitmaps(fs);
304 		if (basefs_in) {
305 			retval = base_fs_alloc_load(fs, basefs_in, mountpoint,
306 				src_dir);
307 			if (retval) {
308 				com_err(prog_name, retval, "%s",
309 				"while reading base_fs file");
310 			    exit(1);
311 			}
312 			fs_callbacks.create_new_inode =
313 				base_fs_alloc_set_target;
314 			fs_callbacks.end_create_new_inode =
315 				base_fs_alloc_unset_target;
316 		}
317 		retval = populate_fs2(fs, EXT2_ROOT_INO, src_dir,
318 				      EXT2_ROOT_INO, &fs_callbacks);
319 		if (retval) {
320 			com_err(prog_name, retval, "%s",
321 			"while populating file system");
322 		    exit(1);
323 		}
324 		if (basefs_in)
325 			base_fs_alloc_cleanup(fs);
326 	}
327 
328 	if (android_configure) {
329 		retval = android_configure_fs(
330 			fs, src_dir, product_out, mountpoint, seopt_file,
331 			nr_opt, fs_config_file, fixed_time, &uid_map, &gid_map);
332 		if (retval) {
333 			com_err(prog_name, retval, "%s",
334 				"while configuring the file system");
335 			exit(1);
336 		}
337 	}
338 
339 	if (block_list) {
340 		retval = fsmap_iter_filsys(fs, &block_list_format, block_list,
341 					   mountpoint);
342 		if (retval) {
343 			com_err(prog_name, retval, "%s",
344 				"while creating the block_list");
345 			exit(1);
346 		}
347 	}
348 
349 	if (basefs_out) {
350 		retval = fsmap_iter_filsys(fs, &base_fs_format,
351 					   basefs_out, mountpoint);
352 		if (retval) {
353 			com_err(prog_name, retval, "%s",
354 				"while creating the basefs file");
355 			exit(1);
356 		}
357 	}
358 
359 	inodes_count = fs->super->s_inodes_count;
360 	free_inodes_count = fs->super->s_free_inodes_count;
361 	blocks_count = ext2fs_blocks_count(fs->super);
362 	free_blocks_count = ext2fs_free_blocks_count(fs->super);
363 
364 	retval = ext2fs_close_free(&fs);
365 	if (retval) {
366 		com_err(prog_name, retval, "%s",
367 				"while writing superblocks");
368 		exit(1);
369 	}
370 
371 	printf("Created filesystem with %u/%u inodes and %llu/%llu blocks\n",
372 			inodes_count - free_inodes_count, inodes_count,
373 			blocks_count - free_blocks_count, blocks_count);
374 
375 	remove_error_table(&et_ext2_error_table);
376 	return 0;
377 }
378