Paul Lawrence | bc2eb8c | 2015-04-28 22:13:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 Google, Inc. |
| 3 | */ |
| 4 | |
Elliott Hughes | ee29686 | 2015-03-28 10:39:46 -0700 | [diff] [blame] | 5 | #define TAG "ext4_utils" |
| 6 | |
Paul Lawrence | bc2eb8c | 2015-04-28 22:13:04 +0000 | [diff] [blame] | 7 | #include "ext4_crypt_init_extensions.h" |
Paul Lawrence | 92da49d | 2015-02-25 15:11:13 -0800 | [diff] [blame] | 8 | |
Paul Lawrence | bc2eb8c | 2015-04-28 22:13:04 +0000 | [diff] [blame] | 9 | #include <dirent.h> |
Paul Lawrence | 92da49d | 2015-02-25 15:11:13 -0800 | [diff] [blame] | 10 | #include <errno.h> |
Paul Lawrence | bc2eb8c | 2015-04-28 22:13:04 +0000 | [diff] [blame] | 11 | #include <string.h> |
| 12 | #include <unistd.h> |
| 13 | |
Paul Lawrence | db1d49c | 2015-04-10 07:47:30 -0700 | [diff] [blame] | 14 | #include <fcntl.h> |
| 15 | #include <asm/ioctl.h> |
Paul Lawrence | bc2eb8c | 2015-04-28 22:13:04 +0000 | [diff] [blame] | 16 | #include <sys/syscall.h> |
| 17 | #include <sys/stat.h> |
Paul Lawrence | db1d49c | 2015-04-10 07:47:30 -0700 | [diff] [blame] | 18 | #include <sys/types.h> |
Paul Lawrence | 92da49d | 2015-02-25 15:11:13 -0800 | [diff] [blame] | 19 | |
Elliott Hughes | ee29686 | 2015-03-28 10:39:46 -0700 | [diff] [blame] | 20 | #include <cutils/klog.h> |
Paul Lawrence | 92da49d | 2015-02-25 15:11:13 -0800 | [diff] [blame] | 21 | |
Paul Lawrence | 6198026 | 2015-03-16 15:35:55 -0700 | [diff] [blame] | 22 | #include "unencrypted_properties.h" |
Paul Lawrence | 92da49d | 2015-02-25 15:11:13 -0800 | [diff] [blame] | 23 | |
Paul Lawrence | bc2eb8c | 2015-04-28 22:13:04 +0000 | [diff] [blame] | 24 | #define XATTR_NAME_ENCRYPTION_POLICY "encryption.policy" |
| 25 | #define EXT4_KEYREF_DELIMITER ((char)'.') |
| 26 | |
| 27 | // ext4enc:TODO Include structure from somewhere sensible |
| 28 | // MUST be in sync with ext4_crypto.c in kernel |
Paul Lawrence | db1d49c | 2015-04-10 07:47:30 -0700 | [diff] [blame] | 29 | #define EXT4_KEY_DESCRIPTOR_SIZE 8 |
| 30 | struct ext4_encryption_policy { |
| 31 | char version; |
| 32 | char contents_encryption_mode; |
| 33 | char filenames_encryption_mode; |
Paul Lawrence | af02e8a | 2015-05-01 05:24:04 -0700 | [diff] [blame] | 34 | char flags; |
Paul Lawrence | db1d49c | 2015-04-10 07:47:30 -0700 | [diff] [blame] | 35 | char master_key_descriptor[EXT4_KEY_DESCRIPTOR_SIZE]; |
| 36 | } __attribute__((__packed__)); |
| 37 | |
| 38 | #define EXT4_ENCRYPTION_MODE_AES_256_XTS 1 |
| 39 | #define EXT4_ENCRYPTION_MODE_AES_256_CTS 4 |
| 40 | |
| 41 | // ext4enc:TODO Get value from somewhere sensible |
| 42 | #define EXT4_IOC_SET_ENCRYPTION_POLICY \ |
| 43 | _IOR('f', 19, struct ext4_encryption_policy) |
Paul Lawrence | bc2eb8c | 2015-04-28 22:13:04 +0000 | [diff] [blame] | 44 | |
| 45 | /* Validate that all path items are available and accessible. */ |
| 46 | static int is_path_valid(const char *path) |
| 47 | { |
| 48 | if (access(path, W_OK)) { |
| 49 | KLOG_ERROR(TAG, "Can't access %s: %s\n",strerror(errno), path); |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | return 1; |
| 54 | } |
| 55 | |
Paul Lawrence | bc2eb8c | 2015-04-28 22:13:04 +0000 | [diff] [blame] | 56 | static int is_dir_empty(const char *dirname) |
| 57 | { |
| 58 | int n = 0; |
| 59 | struct dirent *d; |
| 60 | DIR *dir; |
| 61 | |
| 62 | dir = opendir(dirname); |
| 63 | while ((d = readdir(dir)) != NULL) { |
| 64 | if (strcmp(d->d_name, "lost+found") == 0) { |
| 65 | // Skip lost+found directory |
| 66 | } else if (++n > 2) { |
| 67 | break; |
| 68 | } |
| 69 | } |
| 70 | closedir(dir); |
| 71 | return n <= 2; |
| 72 | } |
| 73 | |
Paul Lawrence | db1d49c | 2015-04-10 07:47:30 -0700 | [diff] [blame] | 74 | int do_policy_set(const char *directory, const char *policy, int policy_length) |
Paul Lawrence | bc2eb8c | 2015-04-28 22:13:04 +0000 | [diff] [blame] | 75 | { |
| 76 | struct stat st; |
| 77 | ssize_t ret; |
| 78 | |
Paul Lawrence | db1d49c | 2015-04-10 07:47:30 -0700 | [diff] [blame] | 79 | if (policy_length != EXT4_KEY_DESCRIPTOR_SIZE) { |
| 80 | KLOG_ERROR("Policy wrong length\n"); |
Paul Lawrence | bc2eb8c | 2015-04-28 22:13:04 +0000 | [diff] [blame] | 81 | return -EINVAL; |
| 82 | } |
| 83 | |
| 84 | if (!is_path_valid(directory)) { |
| 85 | return -EINVAL; |
| 86 | } |
| 87 | |
| 88 | stat(directory, &st); |
| 89 | if (!S_ISDIR(st.st_mode)) { |
| 90 | KLOG_ERROR(TAG, "Can only set policy on a directory (%s)\n", directory); |
| 91 | return -EINVAL; |
| 92 | } |
| 93 | |
| 94 | if (!is_dir_empty(directory)) { |
| 95 | KLOG_ERROR(TAG, "Can only set policy on an empty directory (%s)\n", |
| 96 | directory); |
| 97 | return -EINVAL; |
| 98 | } |
| 99 | |
Paul Lawrence | db1d49c | 2015-04-10 07:47:30 -0700 | [diff] [blame] | 100 | int fd = open(directory, O_DIRECTORY); |
| 101 | if (fd == -1) { |
| 102 | KLOG_ERROR(TAG, "Failed to open directory (%s)\n", directory); |
| 103 | return -EINVAL; |
| 104 | } |
| 105 | |
| 106 | ext4_encryption_policy eep; |
| 107 | eep.version = 0; |
| 108 | eep.contents_encryption_mode = EXT4_ENCRYPTION_MODE_AES_256_XTS; |
| 109 | eep.filenames_encryption_mode = EXT4_ENCRYPTION_MODE_AES_256_CTS; |
Paul Lawrence | af02e8a | 2015-05-01 05:24:04 -0700 | [diff] [blame] | 110 | eep.flags = 0; |
Paul Lawrence | db1d49c | 2015-04-10 07:47:30 -0700 | [diff] [blame] | 111 | memcpy(eep.master_key_descriptor, policy, EXT4_KEY_DESCRIPTOR_SIZE); |
| 112 | ret = ioctl(fd, EXT4_IOC_SET_ENCRYPTION_POLICY, &eep); |
| 113 | auto preserve_errno = errno; |
| 114 | close(fd); |
Paul Lawrence | bc2eb8c | 2015-04-28 22:13:04 +0000 | [diff] [blame] | 115 | |
| 116 | if (ret) { |
| 117 | KLOG_ERROR(TAG, "Failed to set encryption policy for %s: %s\n", |
Paul Lawrence | db1d49c | 2015-04-10 07:47:30 -0700 | [diff] [blame] | 118 | directory, strerror(preserve_errno)); |
Paul Lawrence | bc2eb8c | 2015-04-28 22:13:04 +0000 | [diff] [blame] | 119 | return -EINVAL; |
| 120 | } |
| 121 | |
Paul Lawrence | af02e8a | 2015-05-01 05:24:04 -0700 | [diff] [blame] | 122 | KLOG_INFO(TAG, "Encryption policy for %s is set to %02x%02x%02x%02x\n", |
| 123 | directory, policy[0], policy[1], policy[2], policy[3]); |
Paul Lawrence | bc2eb8c | 2015-04-28 22:13:04 +0000 | [diff] [blame] | 124 | return 0; |
Paul Lawrence | 6198026 | 2015-03-16 15:35:55 -0700 | [diff] [blame] | 125 | } |
Paul Lawrence | 92da49d | 2015-02-25 15:11:13 -0800 | [diff] [blame] | 126 | |
| 127 | bool e4crypt_non_default_key(const char* dir) |
| 128 | { |
Paul Lawrence | bc2eb8c | 2015-04-28 22:13:04 +0000 | [diff] [blame] | 129 | UnencryptedProperties props(dir); |
| 130 | return props.Get<int>(properties::is_default, 1) != 1; |
Paul Lawrence | 92da49d | 2015-02-25 15:11:13 -0800 | [diff] [blame] | 131 | } |