• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * e4crypt.c - ext4 encryption management utility
3  *
4  * Copyright (c) 2014 Google, Inc.
5  *	SHA512 implementation from libtomcrypt.
6  *
7  * Authors: Michael Halcrow <mhalcrow@google.com>,
8  *	Ildar Muslukhov <ildarm@google.com>
9  */
10 
11 #ifndef _LARGEFILE_SOURCE
12 #define _LARGEFILE_SOURCE
13 #endif
14 
15 #ifndef _LARGEFILE64_SOURCE
16 #define _LARGEFILE64_SOURCE
17 #endif
18 
19 #ifndef _GNU_SOURCE
20 #define _GNU_SOURCE
21 #endif
22 
23 #include "config.h"
24 #include <assert.h>
25 #include <errno.h>
26 #include <getopt.h>
27 #include <dirent.h>
28 #include <errno.h>
29 #include <stdbool.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <mntent.h>
35 #include <sys/ioctl.h>
36 #include <sys/stat.h>
37 #include <sys/types.h>
38 #include <fcntl.h>
39 #include <termios.h>
40 #include <unistd.h>
41 #include <signal.h>
42 #if !defined(HAVE_ADD_KEY) || !defined(HAVE_KEYCTL)
43 #include <sys/syscall.h>
44 #endif
45 #ifdef HAVE_SYS_KEY_H
46 #include <sys/key.h>
47 #endif
48 
49 #include "ext2fs/ext2_fs.h"
50 #include "ext2fs/ext2fs.h"
51 #include "uuid/uuid.h"
52 
53 /* special process keyring shortcut IDs */
54 #define KEY_SPEC_THREAD_KEYRING		-1
55 #define KEY_SPEC_PROCESS_KEYRING	-2
56 #define KEY_SPEC_SESSION_KEYRING	-3
57 #define KEY_SPEC_USER_KEYRING		-4
58 #define KEY_SPEC_USER_SESSION_KEYRING	-5
59 #define KEY_SPEC_GROUP_KEYRING		-6
60 
61 #define KEYCTL_GET_KEYRING_ID		0
62 #define KEYCTL_JOIN_SESSION_KEYRING	1
63 #define KEYCTL_DESCRIBE			6
64 #define KEYCTL_SEARCH			10
65 #define KEYCTL_SESSION_TO_PARENT	18
66 
67 typedef __s32 key_serial_t;
68 
69 #define EXT4_KEY_REF_STR_BUF_SIZE ((EXT4_KEY_DESCRIPTOR_SIZE * 2) + 1)
70 
71 #ifndef EXT4_IOC_GET_ENCRYPTION_PWSALT
72 #define EXT4_IOC_GET_ENCRYPTION_PWSALT	_IOW('f', 20, __u8[16])
73 #endif
74 
75 #define OPT_VERBOSE	0x0001
76 #define OPT_QUIET	0x0002
77 
78 int options;
79 
80 #ifndef HAVE_KEYCTL
keyctl(int cmd,...)81 static long keyctl(int cmd, ...)
82 {
83 	va_list va;
84 	unsigned long arg2, arg3, arg4, arg5;
85 
86 	va_start(va, cmd);
87 	arg2 = va_arg(va, unsigned long);
88 	arg3 = va_arg(va, unsigned long);
89 	arg4 = va_arg(va, unsigned long);
90 	arg5 = va_arg(va, unsigned long);
91 	va_end(va);
92 	return syscall(__NR_keyctl, cmd, arg2, arg3, arg4, arg5);
93 }
94 #endif
95 
96 #ifndef HAVE_ADD_KEY
add_key(const char * type,const char * description,const void * payload,size_t plen,key_serial_t keyring)97 static key_serial_t add_key(const char *type, const char *description,
98 			    const void *payload, size_t plen,
99 			    key_serial_t keyring)
100 {
101 	return syscall(__NR_add_key, type, description, payload,
102 		       plen, keyring);
103 }
104 #endif
105 
106 static const unsigned char *hexchars = (const unsigned char *) "0123456789abcdef";
107 static const size_t hexchars_size = 16;
108 
109 #define SHA512_LENGTH 64
110 #define EXT2FS_KEY_TYPE_LOGON "logon"
111 #define EXT2FS_KEY_DESC_PREFIX "ext4:"
112 #define EXT2FS_KEY_DESC_PREFIX_SIZE 5
113 
114 #define EXT4_IOC_SET_ENCRYPTION_POLICY      _IOR('f', 19, struct ext4_encryption_policy)
115 #define EXT4_IOC_GET_ENCRYPTION_POLICY      _IOW('f', 21, struct ext4_encryption_policy)
116 
int_log2(int arg)117 static int int_log2(int arg)
118 {
119 	int     l = 0;
120 
121 	arg >>= 1;
122 	while (arg) {
123 		l++;
124 		arg >>= 1;
125 	}
126 	return l;
127 }
128 
validate_paths(int argc,char * argv[],int path_start_index)129 static void validate_paths(int argc, char *argv[], int path_start_index)
130 {
131 	int x;
132 	int valid = 1;
133 	struct stat st;
134 
135 	for (x = path_start_index; x < argc; x++) {
136 		int ret = access(argv[x], W_OK);
137 		if (ret) {
138 		invalid:
139 			perror(argv[x]);
140 			valid = 0;
141 			continue;
142 		}
143 		ret = stat(argv[x], &st);
144 		if (ret < 0)
145 			goto invalid;
146 		if (!S_ISDIR(st.st_mode)) {
147 			fprintf(stderr, "%s is not a directory\n", argv[x]);
148 			goto invalid;
149 		}
150 	}
151 	if (!valid)
152 		exit(1);
153 }
154 
hex2byte(const char * hex,size_t hex_size,unsigned char * bytes,size_t bytes_size)155 static int hex2byte(const char *hex, size_t hex_size, unsigned char *bytes,
156 		    size_t bytes_size)
157 {
158 	size_t x;
159 	unsigned char *h, *l;
160 
161 	if (hex_size % 2)
162 		return -EINVAL;
163 	for (x = 0; x < hex_size; x += 2) {
164 		h = memchr(hexchars, hex[x], hexchars_size);
165 		if (!h)
166 			return -EINVAL;
167 		l = memchr(hexchars, hex[x + 1], hexchars_size);
168 		if (!l)
169 			return -EINVAL;
170 		if ((x >> 1) >= bytes_size)
171 			return -EINVAL;
172 		bytes[x >> 1] = (((unsigned char)(h - hexchars) << 4) +
173 				 (unsigned char)(l - hexchars));
174 	}
175 	return 0;
176 }
177 
178 /*
179  * Salt handling
180  */
181 struct salt {
182 	unsigned char *salt;
183 	char key_ref_str[EXT4_KEY_REF_STR_BUF_SIZE];
184 	unsigned char key_desc[EXT4_KEY_DESCRIPTOR_SIZE];
185 	unsigned char key[EXT4_MAX_KEY_SIZE];
186 	size_t salt_len;
187 };
188 struct salt *salt_list;
189 unsigned num_salt;
190 unsigned max_salt;
191 char in_passphrase[EXT4_MAX_PASSPHRASE_SIZE];
192 
find_by_salt(unsigned char * salt,size_t salt_len)193 static struct salt *find_by_salt(unsigned char *salt, size_t salt_len)
194 {
195 	unsigned int i;
196 	struct salt *p;
197 
198 	for (i = 0, p = salt_list; i < num_salt; i++, p++)
199 		if ((p->salt_len == salt_len) &&
200 		    !memcmp(p->salt, salt, salt_len))
201 			return p;
202 	return NULL;
203 }
204 
add_salt(unsigned char * salt,size_t salt_len)205 static void add_salt(unsigned char *salt, size_t salt_len)
206 {
207 	if (find_by_salt(salt, salt_len))
208 		return;
209 	if (num_salt >= max_salt) {
210 		max_salt = num_salt + 10;
211 		salt_list = realloc(salt_list, max_salt * sizeof(struct salt));
212 		if (!salt_list) {
213 			fprintf(stderr, "Couldn't allocate salt list\n");
214 			exit(1);
215 		}
216 	}
217 	salt_list[num_salt].salt = salt;
218 	salt_list[num_salt].salt_len = salt_len;
219 	num_salt++;
220 }
221 
clear_secrets(void)222 static void clear_secrets(void)
223 {
224 	if (salt_list) {
225 		memset(salt_list, 0, sizeof(struct salt) * max_salt);
226 		free(salt_list);
227 		salt_list = NULL;
228 	}
229 	memset(in_passphrase, 0, sizeof(in_passphrase));
230 }
231 
die_signal_handler(int signum EXT2FS_ATTR ((unused)),siginfo_t * siginfo EXT2FS_ATTR ((unused)),void * context EXT2FS_ATTR ((unused)))232 static void die_signal_handler(int signum EXT2FS_ATTR((unused)),
233 			       siginfo_t *siginfo EXT2FS_ATTR((unused)),
234 			       void *context EXT2FS_ATTR((unused)))
235 {
236 	clear_secrets();
237 	exit(-1);
238 }
239 
sigcatcher_setup(void)240 static void sigcatcher_setup(void)
241 {
242 	struct sigaction	sa;
243 
244 	memset(&sa, 0, sizeof(struct sigaction));
245 	sa.sa_sigaction = die_signal_handler;
246 	sa.sa_flags = SA_SIGINFO;
247 
248 	sigaction(SIGHUP, &sa, 0);
249 	sigaction(SIGINT, &sa, 0);
250 	sigaction(SIGQUIT, &sa, 0);
251 	sigaction(SIGFPE, &sa, 0);
252 	sigaction(SIGILL, &sa, 0);
253 	sigaction(SIGBUS, &sa, 0);
254 	sigaction(SIGSEGV, &sa, 0);
255 	sigaction(SIGABRT, &sa, 0);
256 	sigaction(SIGPIPE, &sa, 0);
257 	sigaction(SIGALRM, &sa, 0);
258 	sigaction(SIGTERM, &sa, 0);
259 	sigaction(SIGUSR1, &sa, 0);
260 	sigaction(SIGUSR2, &sa, 0);
261 	sigaction(SIGPOLL, &sa, 0);
262 	sigaction(SIGPROF, &sa, 0);
263 	sigaction(SIGSYS, &sa, 0);
264 	sigaction(SIGTRAP, &sa, 0);
265 	sigaction(SIGVTALRM, &sa, 0);
266 	sigaction(SIGXCPU, &sa, 0);
267 	sigaction(SIGXFSZ, &sa, 0);
268 }
269 
270 
271 #define PARSE_FLAGS_NOTSUPP_OK	0x0001
272 #define PARSE_FLAGS_FORCE_FN	0x0002
273 
parse_salt(char * salt_str,int flags)274 static void parse_salt(char *salt_str, int flags)
275 {
276 	unsigned char buf[EXT4_MAX_SALT_SIZE];
277 	char *cp = salt_str;
278 	unsigned char *salt_buf;
279 	int fd, ret, salt_len = 0;
280 
281 	if (flags & PARSE_FLAGS_FORCE_FN)
282 		goto salt_from_filename;
283 	if (strncmp(cp, "s:", 2) == 0) {
284 		cp += 2;
285 		salt_len = strlen(cp);
286 		if (salt_len >= EXT4_MAX_SALT_SIZE)
287 			goto invalid_salt;
288 		strncpy((char *) buf, cp, sizeof(buf));
289 	} else if (cp[0] == '/') {
290 	salt_from_filename:
291 		fd = open(cp, O_RDONLY | O_DIRECTORY);
292 		if (fd == -1 && errno == ENOTDIR)
293 			fd = open(cp, O_RDONLY);
294 		if (fd == -1) {
295 			perror(cp);
296 			exit(1);
297 		}
298 		ret = ioctl(fd, EXT4_IOC_GET_ENCRYPTION_PWSALT, &buf);
299 		close(fd);
300 		if (ret < 0) {
301 			if (flags & PARSE_FLAGS_NOTSUPP_OK)
302 				return;
303 			perror("EXT4_IOC_GET_ENCRYPTION_PWSALT");
304 			exit(1);
305 		}
306 		if (options & OPT_VERBOSE) {
307 			char tmp[80];
308 			uuid_unparse(buf, tmp);
309 			printf("%s has pw salt %s\n", cp, tmp);
310 		}
311 		salt_len = 16;
312 	} else if (strncmp(cp, "f:", 2) == 0) {
313 		cp += 2;
314 		goto salt_from_filename;
315 	} else if (strncmp(cp, "0x", 2) == 0) {
316 		unsigned char *h, *l;
317 
318 		cp += 2;
319 		if (strlen(cp) & 1)
320 			goto invalid_salt;
321 		while (*cp) {
322 			if (salt_len >= EXT4_MAX_SALT_SIZE)
323 				goto invalid_salt;
324 			h = memchr(hexchars, *cp++, hexchars_size);
325 			l = memchr(hexchars, *cp++, hexchars_size);
326 			if (!h || !l)
327 				goto invalid_salt;
328 			buf[salt_len++] =
329 				(((unsigned char)(h - hexchars) << 4) +
330 				 (unsigned char)(l - hexchars));
331 		}
332 	} else if (uuid_parse(cp, buf) == 0) {
333 		salt_len = 16;
334 	} else {
335 	invalid_salt:
336 		fprintf(stderr, "Invalid salt: %s\n", salt_str);
337 		exit(1);
338 	}
339 	salt_buf = malloc(salt_len);
340 	if (!salt_buf) {
341 		fprintf(stderr, "Couldn't allocate salt\n");
342 		exit(1);
343 	}
344 	memcpy(salt_buf, buf, salt_len);
345 	add_salt(salt_buf, salt_len);
346 }
347 
set_policy(struct salt * set_salt,int pad,int argc,char * argv[],int path_start_index)348 static void set_policy(struct salt *set_salt, int pad,
349 		       int argc, char *argv[], int path_start_index)
350 {
351 	struct salt *salt;
352 	struct ext4_encryption_policy policy;
353 	uuid_t	uu;
354 	int fd;
355 	int x;
356 	int rc;
357 
358 	if ((pad != 4) && (pad != 8) &&
359 		 (pad != 16) && (pad != 32)) {
360 		fprintf(stderr, "Invalid padding %d\n", pad);
361 		exit(1);
362 	}
363 
364 	for (x = path_start_index; x < argc; x++) {
365 		fd = open(argv[x], O_DIRECTORY);
366 		if (fd == -1) {
367 			perror(argv[x]);
368 			exit(1);
369 		}
370 		if (set_salt)
371 			salt = set_salt;
372 		else {
373 			if (ioctl(fd, EXT4_IOC_GET_ENCRYPTION_PWSALT,
374 				  &uu) < 0) {
375 				perror("EXT4_IOC_GET_ENCRYPTION_PWSALT");
376 				exit(1);
377 			}
378 			salt = find_by_salt(uu, sizeof(uu));
379 			if (!salt) {
380 				fprintf(stderr, "Couldn't find salt!?!\n");
381 				exit(1);
382 			}
383 		}
384 		policy.version = 0;
385 		policy.contents_encryption_mode =
386 			EXT4_ENCRYPTION_MODE_AES_256_XTS;
387 		policy.filenames_encryption_mode =
388 			EXT4_ENCRYPTION_MODE_AES_256_CTS;
389 		policy.flags = int_log2(pad >> 2);
390 		memcpy(policy.master_key_descriptor, salt->key_desc,
391 		       EXT4_KEY_DESCRIPTOR_SIZE);
392 		rc = ioctl(fd, EXT4_IOC_SET_ENCRYPTION_POLICY, &policy);
393 		close(fd);
394 		if (rc) {
395 			printf("Error [%s] setting policy.\nThe key descriptor "
396 			       "[%s] may not match the existing encryption "
397 			       "context for directory [%s].\n",
398 			       strerror(errno), salt->key_ref_str, argv[x]);
399 			continue;
400 		}
401 		printf("Key with descriptor [%s] applied to %s.\n",
402 		       salt->key_ref_str, argv[x]);
403 	}
404 }
405 
pbkdf2_sha512(const char * passphrase,struct salt * salt,unsigned int count,unsigned char derived_key[EXT4_MAX_KEY_SIZE])406 static void pbkdf2_sha512(const char *passphrase, struct salt *salt,
407 			  unsigned int count,
408 			  unsigned char derived_key[EXT4_MAX_KEY_SIZE])
409 {
410 	size_t passphrase_size = strlen(passphrase);
411 	unsigned char buf[SHA512_LENGTH + EXT4_MAX_PASSPHRASE_SIZE] = {0};
412 	unsigned char tempbuf[SHA512_LENGTH] = {0};
413 	char final[SHA512_LENGTH] = {0};
414 	unsigned char saltbuf[EXT4_MAX_SALT_SIZE + EXT4_MAX_PASSPHRASE_SIZE] = {0};
415 	int actual_buf_len = SHA512_LENGTH + passphrase_size;
416 	int actual_saltbuf_len = EXT4_MAX_SALT_SIZE + passphrase_size;
417 	unsigned int x, y;
418 	__u32 *final_u32 = (__u32 *)final;
419 	__u32 *temp_u32 = (__u32 *)tempbuf;
420 
421 	if (passphrase_size > EXT4_MAX_PASSPHRASE_SIZE) {
422 		printf("Passphrase size is %zd; max is %d.\n", passphrase_size,
423 		       EXT4_MAX_PASSPHRASE_SIZE);
424 		exit(1);
425 	}
426 	if (salt->salt_len > EXT4_MAX_SALT_SIZE) {
427 		printf("Salt size is %zd; max is %d.\n", salt->salt_len,
428 		       EXT4_MAX_SALT_SIZE);
429 		exit(1);
430 	}
431 	assert(EXT4_MAX_KEY_SIZE <= SHA512_LENGTH);
432 
433 	memcpy(saltbuf, salt->salt, salt->salt_len);
434 	memcpy(&saltbuf[EXT4_MAX_SALT_SIZE], passphrase, passphrase_size);
435 
436 	memcpy(&buf[SHA512_LENGTH], passphrase, passphrase_size);
437 
438 	for (x = 0; x < count; ++x) {
439 		if (x == 0) {
440 			ext2fs_sha512(saltbuf, actual_saltbuf_len, tempbuf);
441 		} else {
442 			/*
443 			 * buf: [previous hash || passphrase]
444 			 */
445 			memcpy(buf, tempbuf, SHA512_LENGTH);
446 			ext2fs_sha512(buf, actual_buf_len, tempbuf);
447 		}
448 		for (y = 0; y < (sizeof(final) / sizeof(*final_u32)); ++y)
449 			final_u32[y] = final_u32[y] ^ temp_u32[y];
450 	}
451 	memcpy(derived_key, final, EXT4_MAX_KEY_SIZE);
452 }
453 
disable_echo(struct termios * saved_settings)454 static int disable_echo(struct termios *saved_settings)
455 {
456 	struct termios current_settings;
457 	int rc = 0;
458 
459 	rc = tcgetattr(0, &current_settings);
460 	if (rc)
461 		return rc;
462 	*saved_settings = current_settings;
463 	current_settings.c_lflag &= ~ECHO;
464 	rc = tcsetattr(0, TCSANOW, &current_settings);
465 
466 	return rc;
467 }
468 
get_passphrase(char * passphrase,int len)469 static void get_passphrase(char *passphrase, int len)
470 {
471 	char *p;
472 	struct termios current_settings;
473 
474 	assert(len > 0);
475 	disable_echo(&current_settings);
476 	p = fgets(passphrase, len, stdin);
477 	tcsetattr(0, TCSANOW, &current_settings);
478 	printf("\n");
479 	if (!p) {
480 		printf("Aborting.\n");
481 		exit(1);
482 	}
483 	p = strrchr(passphrase, '\n');
484 	if (!p)
485 		p = passphrase + len - 1;
486 	*p = '\0';
487 }
488 
489 struct keyring_map {
490 	char name[4];
491 	size_t name_len;
492 	int code;
493 };
494 
495 static const struct keyring_map keyrings[] = {
496 	{"@us", 3, KEY_SPEC_USER_SESSION_KEYRING},
497 	{"@u", 2, KEY_SPEC_USER_KEYRING},
498 	{"@s", 2, KEY_SPEC_SESSION_KEYRING},
499 	{"@g", 2, KEY_SPEC_GROUP_KEYRING},
500 	{"@p", 2, KEY_SPEC_PROCESS_KEYRING},
501 	{"@t", 2, KEY_SPEC_THREAD_KEYRING},
502 };
503 
get_keyring_id(const char * keyring)504 static int get_keyring_id(const char *keyring)
505 {
506 	unsigned int x;
507 	char *end;
508 
509 	/*
510 	 * If no keyring is specified, by default use either the user
511 	 * session keyring or the session keyring.  Fetching the
512 	 * session keyring will return the user session keyring if no
513 	 * session keyring has been set.
514 	 */
515 	if (keyring == NULL)
516 		return KEY_SPEC_SESSION_KEYRING;
517 	for (x = 0; x < (sizeof(keyrings) / sizeof(keyrings[0])); ++x) {
518 		if (strcmp(keyring, keyrings[x].name) == 0) {
519 			return keyrings[x].code;
520 		}
521 	}
522 	x = strtoul(keyring, &end, 10);
523 	if (*end == '\0') {
524 		if (keyctl(KEYCTL_DESCRIBE, x, NULL, 0) < 0)
525 			return 0;
526 		return x;
527 	}
528 	return 0;
529 }
530 
generate_key_ref_str(struct salt * salt)531 static void generate_key_ref_str(struct salt *salt)
532 {
533 	unsigned char key_ref1[SHA512_LENGTH];
534 	unsigned char key_ref2[SHA512_LENGTH];
535 	int x;
536 
537 	ext2fs_sha512(salt->key, EXT4_MAX_KEY_SIZE, key_ref1);
538 	ext2fs_sha512(key_ref1, SHA512_LENGTH, key_ref2);
539 	memcpy(salt->key_desc, key_ref2, EXT4_KEY_DESCRIPTOR_SIZE);
540 	for (x = 0; x < EXT4_KEY_DESCRIPTOR_SIZE; ++x) {
541 		sprintf(&salt->key_ref_str[x * 2], "%02x",
542 			salt->key_desc[x]);
543 	}
544 	salt->key_ref_str[EXT4_KEY_REF_STR_BUF_SIZE - 1] = '\0';
545 }
546 
insert_key_into_keyring(const char * keyring,struct salt * salt)547 static void insert_key_into_keyring(const char *keyring, struct salt *salt)
548 {
549 	int keyring_id = get_keyring_id(keyring);
550 	struct ext4_encryption_key key;
551 	char key_ref_full[EXT2FS_KEY_DESC_PREFIX_SIZE +
552 			  EXT4_KEY_REF_STR_BUF_SIZE];
553 	int rc;
554 
555 	if (keyring_id == 0) {
556 		printf("Invalid keyring [%s].\n", keyring);
557 		exit(1);
558 	}
559 	sprintf(key_ref_full, "%s%s", EXT2FS_KEY_DESC_PREFIX,
560 		salt->key_ref_str);
561 	rc = keyctl(KEYCTL_SEARCH, keyring_id, EXT2FS_KEY_TYPE_LOGON,
562 		    key_ref_full, 0);
563 	if (rc != -1) {
564 		if ((options & OPT_QUIET) == 0)
565 			printf("Key with descriptor [%s] already exists\n",
566 			       salt->key_ref_str);
567 		return;
568 	} else if ((rc == -1) && (errno != ENOKEY)) {
569 		printf("keyctl_search failed: %s\n", strerror(errno));
570 		if (errno == EINVAL)
571 			printf("Keyring [%s] is not available.\n", keyring);
572 		exit(1);
573 	}
574 	key.mode = EXT4_ENCRYPTION_MODE_AES_256_XTS;
575 	memcpy(key.raw, salt->key, EXT4_MAX_KEY_SIZE);
576 	key.size = EXT4_MAX_KEY_SIZE;
577 
578 	/*
579 	 * We need to do this instead of simply adding the key to
580 	 * KEY_SPEC_SESSION_KEYRING since trying to add a key to a
581 	 * session keyring that does not yet exist will cause the
582 	 * kernel to create a session keyring --- which will then get
583 	 * garbage collected as soon as e4crypt exits.
584 	 *
585 	 * The fact that the keyctl system call and the add_key system
586 	 * call treats KEY_SPEC_SESSION_KEYRING differently when a
587 	 * session keyring does not exist is very unfortunate and
588 	 * confusing, but so it goes...
589 	 */
590 	if (keyring_id == KEY_SPEC_SESSION_KEYRING) {
591 		keyring_id = keyctl(KEYCTL_GET_KEYRING_ID, keyring_id, 0);
592 		if (keyring_id < 0) {
593 			printf("Error getting session keyring ID: %s\n",
594 			       strerror(errno));
595 			exit(1);
596 		}
597 	}
598 	rc = add_key(EXT2FS_KEY_TYPE_LOGON, key_ref_full, (void *)&key,
599 		     sizeof(key), keyring_id);
600 	if (rc == -1) {
601 		if (errno == EDQUOT) {
602 			printf("Error adding key to keyring; quota exceeded\n");
603 		} else {
604 			printf("Error adding key with key descriptor [%s]: "
605 			       "%s\n", salt->key_ref_str, strerror(errno));
606 		}
607 		exit(1);
608 	} else {
609 		if ((options & OPT_QUIET) == 0)
610 			printf("Added key with descriptor [%s]\n",
611 			       salt->key_ref_str);
612 	}
613 }
614 
get_default_salts(void)615 static void get_default_salts(void)
616 {
617 	FILE	*f = setmntent("/etc/mtab", "r");
618 	struct mntent *mnt;
619 
620 	while (f && ((mnt = getmntent(f)) != NULL)) {
621 		if (strcmp(mnt->mnt_type, "ext4") ||
622 		    access(mnt->mnt_dir, R_OK))
623 			continue;
624 		parse_salt(mnt->mnt_dir, PARSE_FLAGS_NOTSUPP_OK);
625 	}
626 	endmntent(f);
627 }
628 
629 /* Functions which implement user commands */
630 
631 struct cmd_desc {
632 	const char *cmd_name;
633 	void (*cmd_func)(int, char **, const struct cmd_desc *);
634 	const char *cmd_desc;
635 	const char *cmd_help;
636 	int cmd_flags;
637 };
638 
639 #define CMD_HIDDEN 	0x0001
640 
641 static void do_help(int argc, char **argv, const struct cmd_desc *cmd);
642 
643 #define add_key_desc "adds a key to the user's keyring"
644 #define add_key_help \
645 "e4crypt add_key -S salt [ -k keyring ] [-v] [-q] [ -p pad ] [ path ... ]\n\n" \
646 "Prompts the user for a passphrase and inserts it into the specified\n" \
647 "keyring.  If no keyring is specified, e4crypt will use the session\n" \
648 "keyring if it exists or the user session keyring if it does not.\n\n" \
649 "If one or more directory paths are specified, e4crypt will try to\n" \
650 "set the policy of those directories to use the key just entered by\n" \
651 "the user.\n"
652 
do_add_key(int argc,char ** argv,const struct cmd_desc * cmd)653 static void do_add_key(int argc, char **argv, const struct cmd_desc *cmd)
654 {
655 	struct salt *salt;
656 	bool explicit_salt = false;
657 	char *keyring = NULL;
658 	int i, opt, pad = 4;
659 	unsigned j;
660 
661 	while ((opt = getopt(argc, argv, "k:S:p:vq")) != -1) {
662 		switch (opt) {
663 		case 'k':
664 			/* Specify a keyring. */
665 			keyring = optarg;
666 			break;
667 		case 'p':
668 			pad = atoi(optarg);
669 			break;
670 		case 'S':
671 			if (explicit_salt) {
672 				fputs("May only provide -S once\n", stderr);
673 				exit(1);
674 			}
675 			/* Salt value for passphrase. */
676 			parse_salt(optarg, 0);
677 			explicit_salt = true;
678 			break;
679 		case 'v':
680 			options |= OPT_VERBOSE;
681 			break;
682 		case 'q':
683 			options |= OPT_QUIET;
684 			break;
685 		default:
686 		case '?':
687 			if (opt != '?')
688 				fprintf(stderr, "Unrecognized option: %c\n",
689 					opt);
690 			fputs("USAGE:\n  ", stderr);
691 			fputs(cmd->cmd_help, stderr);
692 			exit(1);
693 		}
694 	}
695 	if (num_salt == 0)
696 		get_default_salts();
697 	if (num_salt == 0) {
698 		fprintf(stderr, "No salt values available\n");
699 		exit(1);
700 	}
701 	validate_paths(argc, argv, optind);
702 	if (!explicit_salt)
703 		for (i = optind; i < argc; i++)
704 			parse_salt(argv[i], PARSE_FLAGS_FORCE_FN);
705 	printf("Enter passphrase (echo disabled): ");
706 	get_passphrase(in_passphrase, sizeof(in_passphrase));
707 	for (j = 0, salt = salt_list; j < num_salt; j++, salt++) {
708 		pbkdf2_sha512(in_passphrase, salt,
709 			      EXT4_PBKDF2_ITERATIONS, salt->key);
710 		generate_key_ref_str(salt);
711 		insert_key_into_keyring(keyring, salt);
712 	}
713 	if (optind != argc) {
714 		salt = explicit_salt ? salt_list : NULL;
715 		set_policy(salt, pad, argc, argv, optind);
716 	}
717 	clear_secrets();
718 	exit(0);
719 }
720 
721 #define set_policy_desc "sets a policy for directories"
722 #define set_policy_help \
723 "e4crypt set_policy [ -p pad ] policy path ... \n\n" \
724 "Sets the policy for the directories specified on the command line.\n" \
725 "All directories must be empty to set the policy; if the directory\n" \
726 "already has a policy established, e4crypt will validate that it the\n" \
727 "policy matches what was specified.  A policy is an encryption key\n" \
728 "identifier consisting of 16 hexadecimal characters.\n"
729 
do_set_policy(int argc,char ** argv,const struct cmd_desc * cmd)730 static void do_set_policy(int argc, char **argv, const struct cmd_desc *cmd)
731 {
732 	struct salt saltbuf;
733 	int c, pad = 4;
734 
735 	while ((c = getopt (argc, argv, "p:")) != EOF) {
736 		switch (c) {
737 		case 'p':
738 			pad = atoi(optarg);
739 			break;
740 		}
741 	}
742 
743 	if (argc < optind + 2) {
744 		fprintf(stderr, "Missing required argument(s).\n\n");
745 		fputs("USAGE:\n  ", stderr);
746 		fputs(cmd->cmd_help, stderr);
747 		exit(1);
748 	}
749 
750 	if ((strlen(argv[optind]) != (EXT4_KEY_DESCRIPTOR_SIZE * 2)) ||
751 	    hex2byte(argv[optind], (EXT4_KEY_DESCRIPTOR_SIZE * 2),
752 		     saltbuf.key_desc, EXT4_KEY_DESCRIPTOR_SIZE)) {
753 		printf("Invalid key descriptor [%s]. Valid characters "
754 		       "are 0-9 and a-f, lower case.  "
755 		       "Length must be %d.\n",
756 		       argv[optind], (EXT4_KEY_DESCRIPTOR_SIZE * 2));
757 			exit(1);
758 	}
759 	validate_paths(argc, argv, optind+1);
760 	strcpy(saltbuf.key_ref_str, argv[optind]);
761 	set_policy(&saltbuf, pad, argc, argv, optind+1);
762 	exit(0);
763 }
764 
765 #define get_policy_desc "get the encryption for directories"
766 #define get_policy_help \
767 "e4crypt get_policy path ... \n\n" \
768 "Gets the policy for the directories specified on the command line.\n"
769 
do_get_policy(int argc,char ** argv,const struct cmd_desc * cmd)770 static void do_get_policy(int argc, char **argv, const struct cmd_desc *cmd)
771 {
772 	struct ext4_encryption_policy policy;
773 	int i, j, fd, rc;
774 
775 	if (argc < 2) {
776 		fprintf(stderr, "Missing required argument(s).\n\n");
777 		fputs("USAGE:\n  ", stderr);
778 		fputs(cmd->cmd_help, stderr);
779 		exit(1);
780 	}
781 
782 	for (i = 1; i < argc; i++) {
783 		fd = open(argv[i], O_RDONLY);
784 		if (fd == -1) {
785 			perror(argv[i]);
786 			exit(1);
787 		}
788 		rc = ioctl(fd, EXT4_IOC_GET_ENCRYPTION_POLICY, &policy);
789 		close(fd);
790 		if (rc) {
791 			printf("Error getting policy for %s: %s\n",
792 			       argv[i], strerror(errno));
793 			continue;
794 		}
795 		printf("%s: ", argv[i]);
796 		for (j = 0; j < EXT4_KEY_DESCRIPTOR_SIZE; j++) {
797 			printf("%02x", (unsigned char) policy.master_key_descriptor[j]);
798 		}
799 		fputc('\n', stdout);
800 	}
801 	exit(0);
802 }
803 
804 #define new_session_desc "give the invoking process a new session keyring"
805 #define new_session_help \
806 "e4crypt new_session\n\n" \
807 "Give the invoking process (typically a shell) a new session keyring,\n" \
808 "discarding its old session keyring.\n"
809 
do_new_session(int argc,char ** argv EXT2FS_ATTR ((unused)),const struct cmd_desc * cmd)810 static void do_new_session(int argc, char **argv EXT2FS_ATTR((unused)),
811 			   const struct cmd_desc *cmd)
812 {
813 	long keyid, ret;
814 
815 	if (argc > 1) {
816 		fputs("Excess arguments\n\n", stderr);
817 		fputs(cmd->cmd_help, stderr);
818 		exit(1);
819 	}
820 	keyid = keyctl(KEYCTL_JOIN_SESSION_KEYRING, NULL);
821 	if (keyid < 0) {
822 		perror("KEYCTL_JOIN_SESSION_KEYRING");
823 		exit(1);
824 	}
825 	ret = keyctl(KEYCTL_SESSION_TO_PARENT, NULL);
826 	if (ret < 0) {
827 		perror("KEYCTL_SESSION_TO_PARENT");
828 		exit(1);
829 	}
830 	printf("Switched invoking process to new session keyring %ld\n", keyid);
831 	exit(0);
832 }
833 
834 #define CMD(name) { #name, do_##name, name##_desc, name##_help, 0 }
835 #define _CMD(name) { #name, do_##name, NULL, NULL, CMD_HIDDEN }
836 
837 const struct cmd_desc cmd_list[] = {
838 	_CMD(help),
839 	CMD(add_key),
840 	CMD(get_policy),
841 	CMD(new_session),
842 	CMD(set_policy),
843 	{ NULL, NULL, NULL, NULL, 0 }
844 };
845 
do_help(int argc,char ** argv,const struct cmd_desc * cmd EXT2FS_ATTR ((unused)))846 static void do_help(int argc, char **argv,
847 		    const struct cmd_desc *cmd EXT2FS_ATTR((unused)))
848 {
849 	const struct cmd_desc *p;
850 
851 	if (argc > 1) {
852 		for (p = cmd_list; p->cmd_name; p++) {
853 			if (p->cmd_flags & CMD_HIDDEN)
854 				continue;
855 			if (strcmp(p->cmd_name, argv[1]) == 0) {
856 				putc('\n', stdout);
857 				fputs("USAGE:\n  ", stdout);
858 				fputs(p->cmd_help, stdout);
859 				exit(0);
860 			}
861 		}
862 		printf("Unknown command: %s\n\n", argv[1]);
863 	}
864 
865 	fputs("Available commands:\n", stdout);
866 	for (p = cmd_list; p->cmd_name; p++) {
867 		if (p->cmd_flags & CMD_HIDDEN)
868 			continue;
869 		printf("  %-20s %s\n", p->cmd_name, p->cmd_desc);
870 	}
871 	printf("\nTo get more information on a command, "
872 	       "type 'e4crypt help cmd'\n");
873 	exit(0);
874 }
875 
main(int argc,char * argv[])876 int main(int argc, char *argv[])
877 {
878 	const struct cmd_desc *cmd;
879 
880 	if (argc < 2)
881 		do_help(argc, argv, cmd_list);
882 
883 	sigcatcher_setup();
884 	for (cmd = cmd_list; cmd->cmd_name; cmd++) {
885 		if (strcmp(cmd->cmd_name, argv[1]) == 0) {
886 			cmd->cmd_func(argc-1, argv+1, cmd);
887 			exit(0);
888 		}
889 	}
890 	printf("Unknown command: %s\n\n", argv[1]);
891 	do_help(1, argv, cmd_list);
892 	return 0;
893 }
894