Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 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 | /* TO DO: |
| 18 | * 1. Perhaps keep several copies of the encrypted key, in case something |
| 19 | * goes horribly wrong? |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <sys/types.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <fcntl.h> |
| 26 | #include <unistd.h> |
| 27 | #include <stdio.h> |
| 28 | #include <sys/ioctl.h> |
| 29 | #include <linux/dm-ioctl.h> |
| 30 | #include <libgen.h> |
| 31 | #include <stdlib.h> |
| 32 | #include <sys/param.h> |
| 33 | #include <string.h> |
| 34 | #include <sys/mount.h> |
| 35 | #include <openssl/evp.h> |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 36 | #include <openssl/sha.h> |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 37 | #include <errno.h> |
Ken Sumrall | c290eaf | 2011-03-07 23:40:35 -0800 | [diff] [blame] | 38 | #include <cutils/android_reboot.h> |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 39 | #include <ext4.h> |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 40 | #include <linux/kdev_t.h> |
Ken Sumrall | e5032c4 | 2012-04-01 23:58:44 -0700 | [diff] [blame] | 41 | #include <fs_mgr.h> |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 42 | #include "cryptfs.h" |
| 43 | #define LOG_TAG "Cryptfs" |
Mike Lockwood | ee6d8c4 | 2012-02-15 13:43:28 -0800 | [diff] [blame] | 44 | #include "cutils/android_reboot.h" |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 45 | #include "cutils/log.h" |
| 46 | #include "cutils/properties.h" |
Ken Sumrall | 5d4c68e | 2011-01-30 19:06:03 -0800 | [diff] [blame] | 47 | #include "hardware_legacy/power.h" |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 48 | #include "VolumeManager.h" |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 49 | |
| 50 | #define DM_CRYPT_BUF_SIZE 4096 |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 51 | #define DATA_MNT_POINT "/data" |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 52 | |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 53 | #define HASH_COUNT 2000 |
| 54 | #define KEY_LEN_BYTES 16 |
| 55 | #define IV_LEN_BYTES 16 |
| 56 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 57 | #define KEY_IN_FOOTER "footer" |
| 58 | |
| 59 | #define EXT4_FS 1 |
| 60 | #define FAT_FS 2 |
| 61 | |
Ken Sumrall | e919efe | 2012-09-29 17:07:41 -0700 | [diff] [blame] | 62 | #define TABLE_LOAD_RETRIES 10 |
| 63 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 64 | char *me = "cryptfs"; |
| 65 | |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 66 | static unsigned char saved_master_key[KEY_LEN_BYTES]; |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 67 | static char *saved_data_blkdev; |
Ken Sumrall | 3ad9072 | 2011-10-04 20:38:29 -0700 | [diff] [blame] | 68 | static char *saved_mount_point; |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 69 | static int master_key_saved = 0; |
Ken Sumrall | e5032c4 | 2012-04-01 23:58:44 -0700 | [diff] [blame] | 70 | #define FSTAB_PREFIX "/fstab." |
| 71 | static char fstab_filename[PROPERTY_VALUE_MAX + sizeof(FSTAB_PREFIX)]; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 72 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 73 | static void ioctl_init(struct dm_ioctl *io, size_t dataSize, const char *name, unsigned flags) |
| 74 | { |
| 75 | memset(io, 0, dataSize); |
| 76 | io->data_size = dataSize; |
| 77 | io->data_start = sizeof(struct dm_ioctl); |
| 78 | io->version[0] = 4; |
| 79 | io->version[1] = 0; |
| 80 | io->version[2] = 0; |
| 81 | io->flags = flags; |
| 82 | if (name) { |
| 83 | strncpy(io->name, name, sizeof(io->name)); |
| 84 | } |
| 85 | } |
| 86 | |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 87 | static unsigned int get_fs_size(char *dev) |
| 88 | { |
| 89 | int fd, block_size; |
| 90 | struct ext4_super_block sb; |
| 91 | off64_t len; |
| 92 | |
| 93 | if ((fd = open(dev, O_RDONLY)) < 0) { |
| 94 | SLOGE("Cannot open device to get filesystem size "); |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | if (lseek64(fd, 1024, SEEK_SET) < 0) { |
| 99 | SLOGE("Cannot seek to superblock"); |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | if (read(fd, &sb, sizeof(sb)) != sizeof(sb)) { |
| 104 | SLOGE("Cannot read superblock"); |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | close(fd); |
| 109 | |
| 110 | block_size = 1024 << sb.s_log_block_size; |
| 111 | /* compute length in bytes */ |
| 112 | len = ( ((off64_t)sb.s_blocks_count_hi << 32) + sb.s_blocks_count_lo) * block_size; |
| 113 | |
| 114 | /* return length in sectors */ |
| 115 | return (unsigned int) (len / 512); |
| 116 | } |
| 117 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 118 | static unsigned int get_blkdev_size(int fd) |
| 119 | { |
| 120 | unsigned int nr_sec; |
| 121 | |
| 122 | if ( (ioctl(fd, BLKGETSIZE, &nr_sec)) == -1) { |
| 123 | nr_sec = 0; |
| 124 | } |
| 125 | |
| 126 | return nr_sec; |
| 127 | } |
| 128 | |
Ken Sumrall | e5032c4 | 2012-04-01 23:58:44 -0700 | [diff] [blame] | 129 | /* Get and cache the name of the fstab file so we don't |
| 130 | * keep talking over the socket to the property service. |
| 131 | */ |
| 132 | static char *get_fstab_filename(void) |
| 133 | { |
| 134 | if (fstab_filename[0] == 0) { |
| 135 | strcpy(fstab_filename, FSTAB_PREFIX); |
| 136 | property_get("ro.hardware", fstab_filename + sizeof(FSTAB_PREFIX) - 1, ""); |
| 137 | } |
| 138 | |
| 139 | return fstab_filename; |
| 140 | } |
| 141 | |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 142 | /* key or salt can be NULL, in which case just skip writing that value. Useful to |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 143 | * update the failed mount count but not change the key. |
| 144 | */ |
| 145 | static int put_crypt_ftr_and_key(char *real_blk_name, struct crypt_mnt_ftr *crypt_ftr, |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 146 | unsigned char *key, unsigned char *salt) |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 147 | { |
| 148 | int fd; |
| 149 | unsigned int nr_sec, cnt; |
| 150 | off64_t off; |
| 151 | int rc = -1; |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 152 | char *fname; |
| 153 | char key_loc[PROPERTY_VALUE_MAX]; |
Ken Sumrall | 3be890f | 2011-09-14 16:53:46 -0700 | [diff] [blame] | 154 | struct stat statbuf; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 155 | |
Ken Sumrall | e5032c4 | 2012-04-01 23:58:44 -0700 | [diff] [blame] | 156 | fs_mgr_get_crypt_info(get_fstab_filename(), key_loc, 0, sizeof(key_loc)); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 157 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 158 | if (!strcmp(key_loc, KEY_IN_FOOTER)) { |
| 159 | fname = real_blk_name; |
| 160 | if ( (fd = open(fname, O_RDWR)) < 0) { |
| 161 | SLOGE("Cannot open real block device %s\n", fname); |
| 162 | return -1; |
| 163 | } |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 164 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 165 | if ( (nr_sec = get_blkdev_size(fd)) == 0) { |
| 166 | SLOGE("Cannot get size of block device %s\n", fname); |
| 167 | goto errout; |
| 168 | } |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 169 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 170 | /* If it's an encrypted Android partition, the last 16 Kbytes contain the |
| 171 | * encryption info footer and key, and plenty of bytes to spare for future |
| 172 | * growth. |
| 173 | */ |
| 174 | off = ((off64_t)nr_sec * 512) - CRYPT_FOOTER_OFFSET; |
| 175 | |
| 176 | if (lseek64(fd, off, SEEK_SET) == -1) { |
| 177 | SLOGE("Cannot seek to real block device footer\n"); |
| 178 | goto errout; |
| 179 | } |
| 180 | } else if (key_loc[0] == '/') { |
| 181 | fname = key_loc; |
| 182 | if ( (fd = open(fname, O_RDWR | O_CREAT, 0600)) < 0) { |
| 183 | SLOGE("Cannot open footer file %s\n", fname); |
| 184 | return -1; |
| 185 | } |
| 186 | } else { |
Ken Sumrall | e5032c4 | 2012-04-01 23:58:44 -0700 | [diff] [blame] | 187 | SLOGE("Unexpected value for crypto key location\n"); |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 188 | return -1;; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | if ((cnt = write(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) { |
| 192 | SLOGE("Cannot write real block device footer\n"); |
| 193 | goto errout; |
| 194 | } |
| 195 | |
| 196 | if (key) { |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 197 | if (crypt_ftr->keysize != KEY_LEN_BYTES) { |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 198 | SLOGE("Keysize of %d bits not supported for real block device %s\n", |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 199 | crypt_ftr->keysize*8, fname); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 200 | goto errout; |
| 201 | } |
| 202 | |
| 203 | if ( (cnt = write(fd, key, crypt_ftr->keysize)) != crypt_ftr->keysize) { |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 204 | SLOGE("Cannot write key for real block device %s\n", fname); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 205 | goto errout; |
| 206 | } |
| 207 | } |
| 208 | |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 209 | if (salt) { |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 210 | /* Compute the offset from the last write to the salt */ |
| 211 | off = KEY_TO_SALT_PADDING; |
| 212 | if (! key) |
| 213 | off += crypt_ftr->keysize; |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 214 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 215 | if (lseek64(fd, off, SEEK_CUR) == -1) { |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 216 | SLOGE("Cannot seek to real block device salt \n"); |
| 217 | goto errout; |
| 218 | } |
| 219 | |
| 220 | if ( (cnt = write(fd, salt, SALT_LEN)) != SALT_LEN) { |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 221 | SLOGE("Cannot write salt for real block device %s\n", fname); |
| 222 | goto errout; |
| 223 | } |
| 224 | } |
| 225 | |
Ken Sumrall | 3be890f | 2011-09-14 16:53:46 -0700 | [diff] [blame] | 226 | fstat(fd, &statbuf); |
| 227 | /* If the keys are kept on a raw block device, do not try to truncate it. */ |
| 228 | if (S_ISREG(statbuf.st_mode) && (key_loc[0] == '/')) { |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 229 | if (ftruncate(fd, 0x4000)) { |
Ken Sumrall | 3be890f | 2011-09-14 16:53:46 -0700 | [diff] [blame] | 230 | SLOGE("Cannot set footer file size\n", fname); |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 231 | goto errout; |
| 232 | } |
| 233 | } |
| 234 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 235 | /* Success! */ |
| 236 | rc = 0; |
| 237 | |
| 238 | errout: |
| 239 | close(fd); |
| 240 | return rc; |
| 241 | |
| 242 | } |
| 243 | |
| 244 | static int get_crypt_ftr_and_key(char *real_blk_name, struct crypt_mnt_ftr *crypt_ftr, |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 245 | unsigned char *key, unsigned char *salt) |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 246 | { |
| 247 | int fd; |
| 248 | unsigned int nr_sec, cnt; |
| 249 | off64_t off; |
| 250 | int rc = -1; |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 251 | char key_loc[PROPERTY_VALUE_MAX]; |
| 252 | char *fname; |
| 253 | struct stat statbuf; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 254 | |
Ken Sumrall | e5032c4 | 2012-04-01 23:58:44 -0700 | [diff] [blame] | 255 | fs_mgr_get_crypt_info(get_fstab_filename(), key_loc, 0, sizeof(key_loc)); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 256 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 257 | if (!strcmp(key_loc, KEY_IN_FOOTER)) { |
| 258 | fname = real_blk_name; |
| 259 | if ( (fd = open(fname, O_RDONLY)) < 0) { |
| 260 | SLOGE("Cannot open real block device %s\n", fname); |
| 261 | return -1; |
| 262 | } |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 263 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 264 | if ( (nr_sec = get_blkdev_size(fd)) == 0) { |
| 265 | SLOGE("Cannot get size of block device %s\n", fname); |
| 266 | goto errout; |
| 267 | } |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 268 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 269 | /* If it's an encrypted Android partition, the last 16 Kbytes contain the |
| 270 | * encryption info footer and key, and plenty of bytes to spare for future |
| 271 | * growth. |
| 272 | */ |
| 273 | off = ((off64_t)nr_sec * 512) - CRYPT_FOOTER_OFFSET; |
| 274 | |
| 275 | if (lseek64(fd, off, SEEK_SET) == -1) { |
| 276 | SLOGE("Cannot seek to real block device footer\n"); |
| 277 | goto errout; |
| 278 | } |
| 279 | } else if (key_loc[0] == '/') { |
| 280 | fname = key_loc; |
| 281 | if ( (fd = open(fname, O_RDONLY)) < 0) { |
| 282 | SLOGE("Cannot open footer file %s\n", fname); |
| 283 | return -1; |
| 284 | } |
| 285 | |
| 286 | /* Make sure it's 16 Kbytes in length */ |
| 287 | fstat(fd, &statbuf); |
Ken Sumrall | 3be890f | 2011-09-14 16:53:46 -0700 | [diff] [blame] | 288 | if (S_ISREG(statbuf.st_mode) && (statbuf.st_size != 0x4000)) { |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 289 | SLOGE("footer file %s is not the expected size!\n", fname); |
| 290 | goto errout; |
| 291 | } |
| 292 | } else { |
Ken Sumrall | e5032c4 | 2012-04-01 23:58:44 -0700 | [diff] [blame] | 293 | SLOGE("Unexpected value for crypto key location\n"); |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 294 | return -1;; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | if ( (cnt = read(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) { |
| 298 | SLOGE("Cannot read real block device footer\n"); |
| 299 | goto errout; |
| 300 | } |
| 301 | |
| 302 | if (crypt_ftr->magic != CRYPT_MNT_MAGIC) { |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 303 | SLOGE("Bad magic for real block device %s\n", fname); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 304 | goto errout; |
| 305 | } |
| 306 | |
| 307 | if (crypt_ftr->major_version != 1) { |
| 308 | SLOGE("Cannot understand major version %d real block device footer\n", |
| 309 | crypt_ftr->major_version); |
| 310 | goto errout; |
| 311 | } |
| 312 | |
| 313 | if (crypt_ftr->minor_version != 0) { |
| 314 | SLOGW("Warning: crypto footer minor version %d, expected 0, continuing...\n", |
| 315 | crypt_ftr->minor_version); |
| 316 | } |
| 317 | |
| 318 | if (crypt_ftr->ftr_size > sizeof(struct crypt_mnt_ftr)) { |
| 319 | /* the footer size is bigger than we expected. |
| 320 | * Skip to it's stated end so we can read the key. |
| 321 | */ |
| 322 | if (lseek(fd, crypt_ftr->ftr_size - sizeof(struct crypt_mnt_ftr), SEEK_CUR) == -1) { |
| 323 | SLOGE("Cannot seek to start of key\n"); |
| 324 | goto errout; |
| 325 | } |
| 326 | } |
| 327 | |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 328 | if (crypt_ftr->keysize != KEY_LEN_BYTES) { |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 329 | SLOGE("Keysize of %d bits not supported for real block device %s\n", |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 330 | crypt_ftr->keysize * 8, fname); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 331 | goto errout; |
| 332 | } |
| 333 | |
| 334 | if ( (cnt = read(fd, key, crypt_ftr->keysize)) != crypt_ftr->keysize) { |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 335 | SLOGE("Cannot read key for real block device %s\n", fname); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 336 | goto errout; |
| 337 | } |
| 338 | |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 339 | if (lseek64(fd, KEY_TO_SALT_PADDING, SEEK_CUR) == -1) { |
| 340 | SLOGE("Cannot seek to real block device salt\n"); |
| 341 | goto errout; |
| 342 | } |
| 343 | |
| 344 | if ( (cnt = read(fd, salt, SALT_LEN)) != SALT_LEN) { |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 345 | SLOGE("Cannot read salt for real block device %s\n", fname); |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 346 | goto errout; |
| 347 | } |
| 348 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 349 | /* Success! */ |
| 350 | rc = 0; |
| 351 | |
| 352 | errout: |
| 353 | close(fd); |
| 354 | return rc; |
| 355 | } |
| 356 | |
| 357 | /* Convert a binary key of specified length into an ascii hex string equivalent, |
| 358 | * without the leading 0x and with null termination |
| 359 | */ |
| 360 | void convert_key_to_hex_ascii(unsigned char *master_key, unsigned int keysize, |
| 361 | char *master_key_ascii) |
| 362 | { |
| 363 | unsigned int i, a; |
| 364 | unsigned char nibble; |
| 365 | |
| 366 | for (i=0, a=0; i<keysize; i++, a+=2) { |
| 367 | /* For each byte, write out two ascii hex digits */ |
| 368 | nibble = (master_key[i] >> 4) & 0xf; |
| 369 | master_key_ascii[a] = nibble + (nibble > 9 ? 0x37 : 0x30); |
| 370 | |
| 371 | nibble = master_key[i] & 0xf; |
| 372 | master_key_ascii[a+1] = nibble + (nibble > 9 ? 0x37 : 0x30); |
| 373 | } |
| 374 | |
| 375 | /* Add the null termination */ |
| 376 | master_key_ascii[a] = '\0'; |
| 377 | |
| 378 | } |
| 379 | |
Ken Sumrall | db5e026 | 2013-02-05 17:39:48 -0800 | [diff] [blame^] | 380 | static int load_crypto_mapping_table(struct crypt_mnt_ftr *crypt_ftr, unsigned char *master_key, |
| 381 | char *real_blk_name, const char *name, int fd, |
| 382 | char *extra_params) |
| 383 | { |
| 384 | char buffer[DM_CRYPT_BUF_SIZE]; |
| 385 | struct dm_ioctl *io; |
| 386 | struct dm_target_spec *tgt; |
| 387 | char *crypt_params; |
| 388 | char master_key_ascii[129]; /* Large enough to hold 512 bit key and null */ |
| 389 | int i; |
| 390 | |
| 391 | io = (struct dm_ioctl *) buffer; |
| 392 | |
| 393 | /* Load the mapping table for this device */ |
| 394 | tgt = (struct dm_target_spec *) &buffer[sizeof(struct dm_ioctl)]; |
| 395 | |
| 396 | ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0); |
| 397 | io->target_count = 1; |
| 398 | tgt->status = 0; |
| 399 | tgt->sector_start = 0; |
| 400 | tgt->length = crypt_ftr->fs_size; |
| 401 | strcpy(tgt->target_type, "crypt"); |
| 402 | |
| 403 | crypt_params = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec); |
| 404 | convert_key_to_hex_ascii(master_key, crypt_ftr->keysize, master_key_ascii); |
| 405 | sprintf(crypt_params, "%s %s 0 %s 0 %s", crypt_ftr->crypto_type_name, |
| 406 | master_key_ascii, real_blk_name, extra_params); |
| 407 | crypt_params += strlen(crypt_params) + 1; |
| 408 | crypt_params = (char *) (((unsigned long)crypt_params + 7) & ~8); /* Align to an 8 byte boundary */ |
| 409 | tgt->next = crypt_params - buffer; |
| 410 | |
| 411 | for (i = 0; i < TABLE_LOAD_RETRIES; i++) { |
| 412 | if (! ioctl(fd, DM_TABLE_LOAD, io)) { |
| 413 | break; |
| 414 | } |
| 415 | usleep(500000); |
| 416 | } |
| 417 | |
| 418 | if (i == TABLE_LOAD_RETRIES) { |
| 419 | /* We failed to load the table, return an error */ |
| 420 | return -1; |
| 421 | } else { |
| 422 | return i + 1; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | |
| 427 | static int get_dm_crypt_version(int fd, const char *name, int *version) |
| 428 | { |
| 429 | char buffer[DM_CRYPT_BUF_SIZE]; |
| 430 | struct dm_ioctl *io; |
| 431 | struct dm_target_versions *v; |
| 432 | int i; |
| 433 | |
| 434 | io = (struct dm_ioctl *) buffer; |
| 435 | |
| 436 | ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0); |
| 437 | |
| 438 | if (ioctl(fd, DM_LIST_VERSIONS, io)) { |
| 439 | return -1; |
| 440 | } |
| 441 | |
| 442 | /* Iterate over the returned versions, looking for name of "crypt". |
| 443 | * When found, get and return the version. |
| 444 | */ |
| 445 | v = (struct dm_target_versions *) &buffer[sizeof(struct dm_ioctl)]; |
| 446 | while (v->next) { |
| 447 | if (! strcmp(v->name, "crypt")) { |
| 448 | /* We found the crypt driver, return the version, and get out */ |
| 449 | version[0] = v->version[0]; |
| 450 | version[1] = v->version[1]; |
| 451 | version[2] = v->version[2]; |
| 452 | return 0; |
| 453 | } |
| 454 | v = (struct dm_target_versions *)(((char *)v) + v->next); |
| 455 | } |
| 456 | |
| 457 | return -1; |
| 458 | } |
| 459 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 460 | static int create_crypto_blk_dev(struct crypt_mnt_ftr *crypt_ftr, unsigned char *master_key, |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 461 | char *real_blk_name, char *crypto_blk_name, const char *name) |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 462 | { |
| 463 | char buffer[DM_CRYPT_BUF_SIZE]; |
| 464 | char master_key_ascii[129]; /* Large enough to hold 512 bit key and null */ |
| 465 | char *crypt_params; |
| 466 | struct dm_ioctl *io; |
| 467 | struct dm_target_spec *tgt; |
| 468 | unsigned int minor; |
| 469 | int fd; |
Ken Sumrall | e919efe | 2012-09-29 17:07:41 -0700 | [diff] [blame] | 470 | int i; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 471 | int retval = -1; |
Ken Sumrall | db5e026 | 2013-02-05 17:39:48 -0800 | [diff] [blame^] | 472 | int version[3]; |
| 473 | char *extra_params; |
| 474 | int load_count; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 475 | |
| 476 | if ((fd = open("/dev/device-mapper", O_RDWR)) < 0 ) { |
| 477 | SLOGE("Cannot open device-mapper\n"); |
| 478 | goto errout; |
| 479 | } |
| 480 | |
| 481 | io = (struct dm_ioctl *) buffer; |
| 482 | |
| 483 | ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0); |
| 484 | if (ioctl(fd, DM_DEV_CREATE, io)) { |
| 485 | SLOGE("Cannot create dm-crypt device\n"); |
| 486 | goto errout; |
| 487 | } |
| 488 | |
| 489 | /* Get the device status, in particular, the name of it's device file */ |
| 490 | ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0); |
| 491 | if (ioctl(fd, DM_DEV_STATUS, io)) { |
| 492 | SLOGE("Cannot retrieve dm-crypt device status\n"); |
| 493 | goto errout; |
| 494 | } |
| 495 | minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00); |
| 496 | snprintf(crypto_blk_name, MAXPATHLEN, "/dev/block/dm-%u", minor); |
| 497 | |
Ken Sumrall | db5e026 | 2013-02-05 17:39:48 -0800 | [diff] [blame^] | 498 | extra_params = ""; |
| 499 | if (! get_dm_crypt_version(fd, name, version)) { |
| 500 | /* Support for allow_discards was added in version 1.11.0 */ |
| 501 | if ((version[0] >= 2) || |
| 502 | ((version[0] == 1) && (version[1] >= 11))) { |
| 503 | extra_params = "1 allow_discards"; |
| 504 | SLOGI("Enabling support for allow_discards in dmcrypt.\n"); |
| 505 | } |
Ken Sumrall | e919efe | 2012-09-29 17:07:41 -0700 | [diff] [blame] | 506 | } |
| 507 | |
Ken Sumrall | db5e026 | 2013-02-05 17:39:48 -0800 | [diff] [blame^] | 508 | load_count = load_crypto_mapping_table(crypt_ftr, master_key, real_blk_name, name, |
| 509 | fd, extra_params); |
| 510 | if (load_count < 0) { |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 511 | SLOGE("Cannot load dm-crypt mapping table.\n"); |
| 512 | goto errout; |
Ken Sumrall | db5e026 | 2013-02-05 17:39:48 -0800 | [diff] [blame^] | 513 | } else if (load_count > 1) { |
| 514 | SLOGI("Took %d tries to load dmcrypt table.\n", load_count); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | /* Resume this device to activate it */ |
Ken Sumrall | db5e026 | 2013-02-05 17:39:48 -0800 | [diff] [blame^] | 518 | ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 519 | |
| 520 | if (ioctl(fd, DM_DEV_SUSPEND, io)) { |
| 521 | SLOGE("Cannot resume the dm-crypt device\n"); |
| 522 | goto errout; |
| 523 | } |
| 524 | |
| 525 | /* We made it here with no errors. Woot! */ |
| 526 | retval = 0; |
| 527 | |
| 528 | errout: |
| 529 | close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */ |
| 530 | |
| 531 | return retval; |
| 532 | } |
| 533 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 534 | static int delete_crypto_blk_dev(char *name) |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 535 | { |
| 536 | int fd; |
| 537 | char buffer[DM_CRYPT_BUF_SIZE]; |
| 538 | struct dm_ioctl *io; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 539 | int retval = -1; |
| 540 | |
| 541 | if ((fd = open("/dev/device-mapper", O_RDWR)) < 0 ) { |
| 542 | SLOGE("Cannot open device-mapper\n"); |
| 543 | goto errout; |
| 544 | } |
| 545 | |
| 546 | io = (struct dm_ioctl *) buffer; |
| 547 | |
| 548 | ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0); |
| 549 | if (ioctl(fd, DM_DEV_REMOVE, io)) { |
| 550 | SLOGE("Cannot remove dm-crypt device\n"); |
| 551 | goto errout; |
| 552 | } |
| 553 | |
| 554 | /* We made it here with no errors. Woot! */ |
| 555 | retval = 0; |
| 556 | |
| 557 | errout: |
| 558 | close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */ |
| 559 | |
| 560 | return retval; |
| 561 | |
| 562 | } |
| 563 | |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 564 | static void pbkdf2(char *passwd, unsigned char *salt, unsigned char *ikey) |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 565 | { |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 566 | /* Turn the password into a key and IV that can decrypt the master key */ |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 567 | PKCS5_PBKDF2_HMAC_SHA1(passwd, strlen(passwd), salt, SALT_LEN, |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 568 | HASH_COUNT, KEY_LEN_BYTES+IV_LEN_BYTES, ikey); |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 569 | } |
| 570 | |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 571 | static int encrypt_master_key(char *passwd, unsigned char *salt, |
| 572 | unsigned char *decrypted_master_key, |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 573 | unsigned char *encrypted_master_key) |
| 574 | { |
| 575 | unsigned char ikey[32+32] = { 0 }; /* Big enough to hold a 256 bit key and 256 bit IV */ |
| 576 | EVP_CIPHER_CTX e_ctx; |
| 577 | int encrypted_len, final_len; |
| 578 | |
| 579 | /* Turn the password into a key and IV that can decrypt the master key */ |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 580 | pbkdf2(passwd, salt, ikey); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 581 | |
| 582 | /* Initialize the decryption engine */ |
| 583 | if (! EVP_EncryptInit(&e_ctx, EVP_aes_128_cbc(), ikey, ikey+KEY_LEN_BYTES)) { |
| 584 | SLOGE("EVP_EncryptInit failed\n"); |
| 585 | return -1; |
| 586 | } |
| 587 | EVP_CIPHER_CTX_set_padding(&e_ctx, 0); /* Turn off padding as our data is block aligned */ |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 588 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 589 | /* Encrypt the master key */ |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 590 | if (! EVP_EncryptUpdate(&e_ctx, encrypted_master_key, &encrypted_len, |
| 591 | decrypted_master_key, KEY_LEN_BYTES)) { |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 592 | SLOGE("EVP_EncryptUpdate failed\n"); |
| 593 | return -1; |
| 594 | } |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 595 | if (! EVP_EncryptFinal(&e_ctx, encrypted_master_key + encrypted_len, &final_len)) { |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 596 | SLOGE("EVP_EncryptFinal failed\n"); |
| 597 | return -1; |
| 598 | } |
| 599 | |
| 600 | if (encrypted_len + final_len != KEY_LEN_BYTES) { |
| 601 | SLOGE("EVP_Encryption length check failed with %d, %d bytes\n", encrypted_len, final_len); |
| 602 | return -1; |
| 603 | } else { |
| 604 | return 0; |
| 605 | } |
| 606 | } |
| 607 | |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 608 | static int decrypt_master_key(char *passwd, unsigned char *salt, |
| 609 | unsigned char *encrypted_master_key, |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 610 | unsigned char *decrypted_master_key) |
| 611 | { |
| 612 | unsigned char ikey[32+32] = { 0 }; /* Big enough to hold a 256 bit key and 256 bit IV */ |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 613 | EVP_CIPHER_CTX d_ctx; |
| 614 | int decrypted_len, final_len; |
| 615 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 616 | /* Turn the password into a key and IV that can decrypt the master key */ |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 617 | pbkdf2(passwd, salt, ikey); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 618 | |
| 619 | /* Initialize the decryption engine */ |
| 620 | if (! EVP_DecryptInit(&d_ctx, EVP_aes_128_cbc(), ikey, ikey+KEY_LEN_BYTES)) { |
| 621 | return -1; |
| 622 | } |
| 623 | EVP_CIPHER_CTX_set_padding(&d_ctx, 0); /* Turn off padding as our data is block aligned */ |
| 624 | /* Decrypt the master key */ |
| 625 | if (! EVP_DecryptUpdate(&d_ctx, decrypted_master_key, &decrypted_len, |
| 626 | encrypted_master_key, KEY_LEN_BYTES)) { |
| 627 | return -1; |
| 628 | } |
| 629 | if (! EVP_DecryptFinal(&d_ctx, decrypted_master_key + decrypted_len, &final_len)) { |
| 630 | return -1; |
| 631 | } |
| 632 | |
| 633 | if (decrypted_len + final_len != KEY_LEN_BYTES) { |
| 634 | return -1; |
| 635 | } else { |
| 636 | return 0; |
| 637 | } |
| 638 | } |
| 639 | |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 640 | static int create_encrypted_random_key(char *passwd, unsigned char *master_key, unsigned char *salt) |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 641 | { |
| 642 | int fd; |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 643 | unsigned char key_buf[KEY_LEN_BYTES]; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 644 | EVP_CIPHER_CTX e_ctx; |
| 645 | int encrypted_len, final_len; |
| 646 | |
| 647 | /* Get some random bits for a key */ |
| 648 | fd = open("/dev/urandom", O_RDONLY); |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 649 | read(fd, key_buf, sizeof(key_buf)); |
| 650 | read(fd, salt, SALT_LEN); |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 651 | close(fd); |
| 652 | |
| 653 | /* Now encrypt it with the password */ |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 654 | return encrypt_master_key(passwd, salt, key_buf, master_key); |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 655 | } |
| 656 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 657 | static int wait_and_unmount(char *mountpoint) |
| 658 | { |
| 659 | int i, rc; |
Ken Sumrall | 2eaf713 | 2011-01-14 12:45:48 -0800 | [diff] [blame] | 660 | #define WAIT_UNMOUNT_COUNT 20 |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 661 | |
| 662 | /* Now umount the tmpfs filesystem */ |
| 663 | for (i=0; i<WAIT_UNMOUNT_COUNT; i++) { |
| 664 | if (umount(mountpoint)) { |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 665 | if (errno == EINVAL) { |
| 666 | /* EINVAL is returned if the directory is not a mountpoint, |
| 667 | * i.e. there is no filesystem mounted there. So just get out. |
| 668 | */ |
| 669 | break; |
| 670 | } |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 671 | sleep(1); |
| 672 | i++; |
| 673 | } else { |
| 674 | break; |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | if (i < WAIT_UNMOUNT_COUNT) { |
| 679 | SLOGD("unmounting %s succeeded\n", mountpoint); |
| 680 | rc = 0; |
| 681 | } else { |
| 682 | SLOGE("unmounting %s failed\n", mountpoint); |
| 683 | rc = -1; |
| 684 | } |
| 685 | |
| 686 | return rc; |
| 687 | } |
| 688 | |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 689 | #define DATA_PREP_TIMEOUT 100 |
| 690 | static int prep_data_fs(void) |
| 691 | { |
| 692 | int i; |
| 693 | |
| 694 | /* Do the prep of the /data filesystem */ |
| 695 | property_set("vold.post_fs_data_done", "0"); |
| 696 | property_set("vold.decrypt", "trigger_post_fs_data"); |
| 697 | SLOGD("Just triggered post_fs_data\n"); |
| 698 | |
| 699 | /* Wait a max of 25 seconds, hopefully it takes much less */ |
| 700 | for (i=0; i<DATA_PREP_TIMEOUT; i++) { |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 701 | char p[PROPERTY_VALUE_MAX]; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 702 | |
| 703 | property_get("vold.post_fs_data_done", p, "0"); |
| 704 | if (*p == '1') { |
| 705 | break; |
| 706 | } else { |
| 707 | usleep(250000); |
| 708 | } |
| 709 | } |
| 710 | if (i == DATA_PREP_TIMEOUT) { |
| 711 | /* Ugh, we failed to prep /data in time. Bail. */ |
| 712 | return -1; |
| 713 | } else { |
| 714 | SLOGD("post_fs_data done\n"); |
| 715 | return 0; |
| 716 | } |
| 717 | } |
| 718 | |
Ken Sumrall | 6864b7e | 2011-01-14 15:20:02 -0800 | [diff] [blame] | 719 | int cryptfs_restart(void) |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 720 | { |
| 721 | char fs_type[32]; |
| 722 | char real_blkdev[MAXPATHLEN]; |
Ken Sumrall | 6864b7e | 2011-01-14 15:20:02 -0800 | [diff] [blame] | 723 | char crypto_blkdev[MAXPATHLEN]; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 724 | char fs_options[256]; |
| 725 | unsigned long mnt_flags; |
| 726 | struct stat statbuf; |
| 727 | int rc = -1, i; |
Ken Sumrall | 0cc1663 | 2011-01-18 20:32:26 -0800 | [diff] [blame] | 728 | static int restart_successful = 0; |
| 729 | |
| 730 | /* Validate that it's OK to call this routine */ |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 731 | if (! master_key_saved) { |
Ken Sumrall | 0cc1663 | 2011-01-18 20:32:26 -0800 | [diff] [blame] | 732 | SLOGE("Encrypted filesystem not validated, aborting"); |
| 733 | return -1; |
| 734 | } |
| 735 | |
| 736 | if (restart_successful) { |
| 737 | SLOGE("System already restarted with encrypted disk, aborting"); |
| 738 | return -1; |
| 739 | } |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 740 | |
| 741 | /* Here is where we shut down the framework. The init scripts |
| 742 | * start all services in one of three classes: core, main or late_start. |
| 743 | * On boot, we start core and main. Now, we stop main, but not core, |
| 744 | * as core includes vold and a few other really important things that |
| 745 | * we need to keep running. Once main has stopped, we should be able |
| 746 | * to umount the tmpfs /data, then mount the encrypted /data. |
| 747 | * We then restart the class main, and also the class late_start. |
| 748 | * At the moment, I've only put a few things in late_start that I know |
| 749 | * are not needed to bring up the framework, and that also cause problems |
| 750 | * with unmounting the tmpfs /data, but I hope to add add more services |
| 751 | * to the late_start class as we optimize this to decrease the delay |
| 752 | * till the user is asked for the password to the filesystem. |
| 753 | */ |
| 754 | |
| 755 | /* The init files are setup to stop the class main when vold.decrypt is |
| 756 | * set to trigger_reset_main. |
| 757 | */ |
| 758 | property_set("vold.decrypt", "trigger_reset_main"); |
| 759 | SLOGD("Just asked init to shut down class main\n"); |
| 760 | |
Ken Sumrall | 92736ef | 2012-10-17 20:57:14 -0700 | [diff] [blame] | 761 | /* Ugh, shutting down the framework is not synchronous, so until it |
| 762 | * can be fixed, this horrible hack will wait a moment for it all to |
| 763 | * shut down before proceeding. Without it, some devices cannot |
| 764 | * restart the graphics services. |
| 765 | */ |
| 766 | sleep(2); |
Ken Sumrall | 9dedfd4 | 2012-10-09 14:16:59 -0700 | [diff] [blame] | 767 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 768 | /* Now that the framework is shutdown, we should be able to umount() |
| 769 | * the tmpfs filesystem, and mount the real one. |
| 770 | */ |
| 771 | |
Ken Sumrall | 6864b7e | 2011-01-14 15:20:02 -0800 | [diff] [blame] | 772 | property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, ""); |
| 773 | if (strlen(crypto_blkdev) == 0) { |
| 774 | SLOGE("fs_crypto_blkdev not set\n"); |
| 775 | return -1; |
| 776 | } |
| 777 | |
Ken Sumrall | e5032c4 | 2012-04-01 23:58:44 -0700 | [diff] [blame] | 778 | if (! (rc = wait_and_unmount(DATA_MNT_POINT)) ) { |
| 779 | /* If that succeeded, then mount the decrypted filesystem */ |
| 780 | fs_mgr_do_mount(get_fstab_filename(), DATA_MNT_POINT, crypto_blkdev, 0); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 781 | |
Ken Sumrall | e5032c4 | 2012-04-01 23:58:44 -0700 | [diff] [blame] | 782 | property_set("vold.decrypt", "trigger_load_persist_props"); |
| 783 | /* Create necessary paths on /data */ |
| 784 | if (prep_data_fs()) { |
| 785 | return -1; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 786 | } |
Ken Sumrall | e5032c4 | 2012-04-01 23:58:44 -0700 | [diff] [blame] | 787 | |
| 788 | /* startup service classes main and late_start */ |
| 789 | property_set("vold.decrypt", "trigger_restart_framework"); |
| 790 | SLOGD("Just triggered restart_framework\n"); |
| 791 | |
| 792 | /* Give it a few moments to get started */ |
| 793 | sleep(1); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 794 | } |
| 795 | |
Ken Sumrall | 0cc1663 | 2011-01-18 20:32:26 -0800 | [diff] [blame] | 796 | if (rc == 0) { |
| 797 | restart_successful = 1; |
| 798 | } |
| 799 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 800 | return rc; |
| 801 | } |
| 802 | |
Ken Sumrall | 7f7dbaa | 2011-02-01 15:46:41 -0800 | [diff] [blame] | 803 | static int do_crypto_complete(char *mount_point) |
| 804 | { |
| 805 | struct crypt_mnt_ftr crypt_ftr; |
| 806 | unsigned char encrypted_master_key[32]; |
| 807 | unsigned char salt[SALT_LEN]; |
| 808 | char real_blkdev[MAXPATHLEN]; |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 809 | char encrypted_state[PROPERTY_VALUE_MAX]; |
Ken Sumrall | e1a4585 | 2011-12-14 21:24:27 -0800 | [diff] [blame] | 810 | char key_loc[PROPERTY_VALUE_MAX]; |
Ken Sumrall | 7f7dbaa | 2011-02-01 15:46:41 -0800 | [diff] [blame] | 811 | |
| 812 | property_get("ro.crypto.state", encrypted_state, ""); |
| 813 | if (strcmp(encrypted_state, "encrypted") ) { |
| 814 | SLOGE("not running with encryption, aborting"); |
| 815 | return 1; |
| 816 | } |
| 817 | |
Ken Sumrall | e5032c4 | 2012-04-01 23:58:44 -0700 | [diff] [blame] | 818 | fs_mgr_get_crypt_info(get_fstab_filename(), 0, real_blkdev, sizeof(real_blkdev)); |
Ken Sumrall | 7f7dbaa | 2011-02-01 15:46:41 -0800 | [diff] [blame] | 819 | |
| 820 | if (get_crypt_ftr_and_key(real_blkdev, &crypt_ftr, encrypted_master_key, salt)) { |
Ken Sumrall | e5032c4 | 2012-04-01 23:58:44 -0700 | [diff] [blame] | 821 | fs_mgr_get_crypt_info(get_fstab_filename(), key_loc, 0, sizeof(key_loc)); |
| 822 | |
Ken Sumrall | e1a4585 | 2011-12-14 21:24:27 -0800 | [diff] [blame] | 823 | /* |
| 824 | * Only report this error if key_loc is a file and it exists. |
| 825 | * If the device was never encrypted, and /data is not mountable for |
| 826 | * some reason, returning 1 should prevent the UI from presenting the |
| 827 | * a "enter password" screen, or worse, a "press button to wipe the |
| 828 | * device" screen. |
| 829 | */ |
| 830 | if ((key_loc[0] == '/') && (access("key_loc", F_OK) == -1)) { |
| 831 | SLOGE("master key file does not exist, aborting"); |
| 832 | return 1; |
| 833 | } else { |
| 834 | SLOGE("Error getting crypt footer and key\n"); |
| 835 | return -1; |
| 836 | } |
Ken Sumrall | 7f7dbaa | 2011-02-01 15:46:41 -0800 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | if (crypt_ftr.flags & CRYPT_ENCRYPTION_IN_PROGRESS) { |
| 840 | SLOGE("Encryption process didn't finish successfully\n"); |
| 841 | return -2; /* -2 is the clue to the UI that there is no usable data on the disk, |
| 842 | * and give the user an option to wipe the disk */ |
| 843 | } |
| 844 | |
| 845 | /* We passed the test! We shall diminish, and return to the west */ |
| 846 | return 0; |
| 847 | } |
| 848 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 849 | static int test_mount_encrypted_fs(char *passwd, char *mount_point, char *label) |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 850 | { |
| 851 | struct crypt_mnt_ftr crypt_ftr; |
| 852 | /* Allocate enough space for a 256 bit key, but we may use less */ |
| 853 | unsigned char encrypted_master_key[32], decrypted_master_key[32]; |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 854 | unsigned char salt[SALT_LEN]; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 855 | char crypto_blkdev[MAXPATHLEN]; |
| 856 | char real_blkdev[MAXPATHLEN]; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 857 | char tmp_mount_point[64]; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 858 | unsigned int orig_failed_decrypt_count; |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 859 | char encrypted_state[PROPERTY_VALUE_MAX]; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 860 | int rc; |
| 861 | |
Ken Sumrall | 0cc1663 | 2011-01-18 20:32:26 -0800 | [diff] [blame] | 862 | property_get("ro.crypto.state", encrypted_state, ""); |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 863 | if ( master_key_saved || strcmp(encrypted_state, "encrypted") ) { |
Ken Sumrall | 0cc1663 | 2011-01-18 20:32:26 -0800 | [diff] [blame] | 864 | SLOGE("encrypted fs already validated or not running with encryption, aborting"); |
| 865 | return -1; |
| 866 | } |
| 867 | |
Ken Sumrall | e5032c4 | 2012-04-01 23:58:44 -0700 | [diff] [blame] | 868 | fs_mgr_get_crypt_info(get_fstab_filename(), 0, real_blkdev, sizeof(real_blkdev)); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 869 | |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 870 | if (get_crypt_ftr_and_key(real_blkdev, &crypt_ftr, encrypted_master_key, salt)) { |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 871 | SLOGE("Error getting crypt footer and key\n"); |
| 872 | return -1; |
| 873 | } |
Ken Sumrall | d33d417 | 2011-02-01 00:49:13 -0800 | [diff] [blame] | 874 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 875 | SLOGD("crypt_ftr->fs_size = %lld\n", crypt_ftr.fs_size); |
| 876 | orig_failed_decrypt_count = crypt_ftr.failed_decrypt_count; |
| 877 | |
| 878 | if (! (crypt_ftr.flags & CRYPT_MNT_KEY_UNENCRYPTED) ) { |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 879 | decrypt_master_key(passwd, salt, encrypted_master_key, decrypted_master_key); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | if (create_crypto_blk_dev(&crypt_ftr, decrypted_master_key, |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 883 | real_blkdev, crypto_blkdev, label)) { |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 884 | SLOGE("Error creating decrypted block device\n"); |
| 885 | return -1; |
| 886 | } |
| 887 | |
| 888 | /* If init detects an encrypted filesystme, it writes a file for each such |
| 889 | * encrypted fs into the tmpfs /data filesystem, and then the framework finds those |
| 890 | * files and passes that data to me */ |
| 891 | /* Create a tmp mount point to try mounting the decryptd fs |
| 892 | * Since we're here, the mount_point should be a tmpfs filesystem, so make |
| 893 | * a directory in it to test mount the decrypted filesystem. |
| 894 | */ |
| 895 | sprintf(tmp_mount_point, "%s/tmp_mnt", mount_point); |
| 896 | mkdir(tmp_mount_point, 0755); |
Ken Sumrall | e5032c4 | 2012-04-01 23:58:44 -0700 | [diff] [blame] | 897 | if (fs_mgr_do_mount(get_fstab_filename(), DATA_MNT_POINT, crypto_blkdev, tmp_mount_point)) { |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 898 | SLOGE("Error temp mounting decrypted block device\n"); |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 899 | delete_crypto_blk_dev(label); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 900 | crypt_ftr.failed_decrypt_count++; |
| 901 | } else { |
| 902 | /* Success, so just umount and we'll mount it properly when we restart |
| 903 | * the framework. |
| 904 | */ |
| 905 | umount(tmp_mount_point); |
| 906 | crypt_ftr.failed_decrypt_count = 0; |
| 907 | } |
| 908 | |
| 909 | if (orig_failed_decrypt_count != crypt_ftr.failed_decrypt_count) { |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 910 | put_crypt_ftr_and_key(real_blkdev, &crypt_ftr, 0, 0); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | if (crypt_ftr.failed_decrypt_count) { |
| 914 | /* We failed to mount the device, so return an error */ |
| 915 | rc = crypt_ftr.failed_decrypt_count; |
| 916 | |
| 917 | } else { |
Ken Sumrall | 6864b7e | 2011-01-14 15:20:02 -0800 | [diff] [blame] | 918 | /* Woot! Success! Save the name of the crypto block device |
| 919 | * so we can mount it when restarting the framework. |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 920 | */ |
Ken Sumrall | 6864b7e | 2011-01-14 15:20:02 -0800 | [diff] [blame] | 921 | property_set("ro.crypto.fs_crypto_blkdev", crypto_blkdev); |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 922 | |
| 923 | /* Also save a the master key so we can reencrypted the key |
| 924 | * the key when we want to change the password on it. |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 925 | */ |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 926 | memcpy(saved_master_key, decrypted_master_key, KEY_LEN_BYTES); |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 927 | saved_data_blkdev = strdup(real_blkdev); |
Ken Sumrall | 3ad9072 | 2011-10-04 20:38:29 -0700 | [diff] [blame] | 928 | saved_mount_point = strdup(mount_point); |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 929 | master_key_saved = 1; |
Ken Sumrall | 6864b7e | 2011-01-14 15:20:02 -0800 | [diff] [blame] | 930 | rc = 0; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 931 | } |
| 932 | |
| 933 | return rc; |
| 934 | } |
| 935 | |
Ken Sumrall | 0b8b597 | 2011-08-31 16:14:23 -0700 | [diff] [blame] | 936 | /* Called by vold when it wants to undo the crypto mapping of a volume it |
| 937 | * manages. This is usually in response to a factory reset, when we want |
| 938 | * to undo the crypto mapping so the volume is formatted in the clear. |
| 939 | */ |
| 940 | int cryptfs_revert_volume(const char *label) |
| 941 | { |
| 942 | return delete_crypto_blk_dev((char *)label); |
| 943 | } |
| 944 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 945 | /* |
| 946 | * Called by vold when it's asked to mount an encrypted, nonremovable volume. |
| 947 | * Setup a dm-crypt mapping, use the saved master key from |
| 948 | * setting up the /data mapping, and return the new device path. |
| 949 | */ |
| 950 | int cryptfs_setup_volume(const char *label, int major, int minor, |
| 951 | char *crypto_sys_path, unsigned int max_path, |
| 952 | int *new_major, int *new_minor) |
| 953 | { |
| 954 | char real_blkdev[MAXPATHLEN], crypto_blkdev[MAXPATHLEN]; |
| 955 | struct crypt_mnt_ftr sd_crypt_ftr; |
| 956 | unsigned char key[32], salt[32]; |
| 957 | struct stat statbuf; |
| 958 | int nr_sec, fd; |
| 959 | |
| 960 | sprintf(real_blkdev, "/dev/block/vold/%d:%d", major, minor); |
| 961 | |
| 962 | /* Just want the footer, but gotta get it all */ |
| 963 | get_crypt_ftr_and_key(saved_data_blkdev, &sd_crypt_ftr, key, salt); |
| 964 | |
| 965 | /* Update the fs_size field to be the size of the volume */ |
| 966 | fd = open(real_blkdev, O_RDONLY); |
| 967 | nr_sec = get_blkdev_size(fd); |
| 968 | close(fd); |
| 969 | if (nr_sec == 0) { |
| 970 | SLOGE("Cannot get size of volume %s\n", real_blkdev); |
| 971 | return -1; |
| 972 | } |
| 973 | |
| 974 | sd_crypt_ftr.fs_size = nr_sec; |
| 975 | create_crypto_blk_dev(&sd_crypt_ftr, saved_master_key, real_blkdev, |
| 976 | crypto_blkdev, label); |
| 977 | |
| 978 | stat(crypto_blkdev, &statbuf); |
| 979 | *new_major = MAJOR(statbuf.st_rdev); |
| 980 | *new_minor = MINOR(statbuf.st_rdev); |
| 981 | |
| 982 | /* Create path to sys entry for this block device */ |
| 983 | snprintf(crypto_sys_path, max_path, "/devices/virtual/block/%s", strrchr(crypto_blkdev, '/')+1); |
| 984 | |
| 985 | return 0; |
| 986 | } |
| 987 | |
Ken Sumrall | 7f7dbaa | 2011-02-01 15:46:41 -0800 | [diff] [blame] | 988 | int cryptfs_crypto_complete(void) |
| 989 | { |
| 990 | return do_crypto_complete("/data"); |
| 991 | } |
| 992 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 993 | int cryptfs_check_passwd(char *passwd) |
| 994 | { |
| 995 | int rc = -1; |
| 996 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 997 | rc = test_mount_encrypted_fs(passwd, DATA_MNT_POINT, "userdata"); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 998 | |
| 999 | return rc; |
| 1000 | } |
| 1001 | |
Ken Sumrall | 3ad9072 | 2011-10-04 20:38:29 -0700 | [diff] [blame] | 1002 | int cryptfs_verify_passwd(char *passwd) |
| 1003 | { |
| 1004 | struct crypt_mnt_ftr crypt_ftr; |
| 1005 | /* Allocate enough space for a 256 bit key, but we may use less */ |
| 1006 | unsigned char encrypted_master_key[32], decrypted_master_key[32]; |
| 1007 | unsigned char salt[SALT_LEN]; |
| 1008 | char real_blkdev[MAXPATHLEN]; |
Ken Sumrall | 3ad9072 | 2011-10-04 20:38:29 -0700 | [diff] [blame] | 1009 | char encrypted_state[PROPERTY_VALUE_MAX]; |
| 1010 | int rc; |
| 1011 | |
| 1012 | property_get("ro.crypto.state", encrypted_state, ""); |
| 1013 | if (strcmp(encrypted_state, "encrypted") ) { |
| 1014 | SLOGE("device not encrypted, aborting"); |
| 1015 | return -2; |
| 1016 | } |
| 1017 | |
| 1018 | if (!master_key_saved) { |
| 1019 | SLOGE("encrypted fs not yet mounted, aborting"); |
| 1020 | return -1; |
| 1021 | } |
| 1022 | |
| 1023 | if (!saved_mount_point) { |
| 1024 | SLOGE("encrypted fs failed to save mount point, aborting"); |
| 1025 | return -1; |
| 1026 | } |
| 1027 | |
Ken Sumrall | e5032c4 | 2012-04-01 23:58:44 -0700 | [diff] [blame] | 1028 | fs_mgr_get_crypt_info(get_fstab_filename(), 0, real_blkdev, sizeof(real_blkdev)); |
Ken Sumrall | 3ad9072 | 2011-10-04 20:38:29 -0700 | [diff] [blame] | 1029 | |
| 1030 | if (get_crypt_ftr_and_key(real_blkdev, &crypt_ftr, encrypted_master_key, salt)) { |
| 1031 | SLOGE("Error getting crypt footer and key\n"); |
| 1032 | return -1; |
| 1033 | } |
| 1034 | |
| 1035 | if (crypt_ftr.flags & CRYPT_MNT_KEY_UNENCRYPTED) { |
| 1036 | /* If the device has no password, then just say the password is valid */ |
| 1037 | rc = 0; |
| 1038 | } else { |
| 1039 | decrypt_master_key(passwd, salt, encrypted_master_key, decrypted_master_key); |
| 1040 | if (!memcmp(decrypted_master_key, saved_master_key, crypt_ftr.keysize)) { |
| 1041 | /* They match, the password is correct */ |
| 1042 | rc = 0; |
| 1043 | } else { |
| 1044 | /* If incorrect, sleep for a bit to prevent dictionary attacks */ |
| 1045 | sleep(1); |
| 1046 | rc = 1; |
| 1047 | } |
| 1048 | } |
| 1049 | |
| 1050 | return rc; |
| 1051 | } |
| 1052 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1053 | /* Initialize a crypt_mnt_ftr structure. The keysize is |
| 1054 | * defaulted to 16 bytes, and the filesystem size to 0. |
| 1055 | * Presumably, at a minimum, the caller will update the |
| 1056 | * filesystem size and crypto_type_name after calling this function. |
| 1057 | */ |
| 1058 | static void cryptfs_init_crypt_mnt_ftr(struct crypt_mnt_ftr *ftr) |
| 1059 | { |
| 1060 | ftr->magic = CRYPT_MNT_MAGIC; |
| 1061 | ftr->major_version = 1; |
| 1062 | ftr->minor_version = 0; |
| 1063 | ftr->ftr_size = sizeof(struct crypt_mnt_ftr); |
| 1064 | ftr->flags = 0; |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 1065 | ftr->keysize = KEY_LEN_BYTES; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1066 | ftr->spare1 = 0; |
| 1067 | ftr->fs_size = 0; |
| 1068 | ftr->failed_decrypt_count = 0; |
| 1069 | ftr->crypto_type_name[0] = '\0'; |
| 1070 | } |
| 1071 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1072 | static int cryptfs_enable_wipe(char *crypto_blkdev, off64_t size, int type) |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1073 | { |
| 1074 | char cmdline[256]; |
| 1075 | int rc = -1; |
| 1076 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1077 | if (type == EXT4_FS) { |
| 1078 | snprintf(cmdline, sizeof(cmdline), "/system/bin/make_ext4fs -a /data -l %lld %s", |
| 1079 | size * 512, crypto_blkdev); |
| 1080 | SLOGI("Making empty filesystem with command %s\n", cmdline); |
| 1081 | } else if (type== FAT_FS) { |
| 1082 | snprintf(cmdline, sizeof(cmdline), "/system/bin/newfs_msdos -F 32 -O android -c 8 -s %lld %s", |
| 1083 | size, crypto_blkdev); |
| 1084 | SLOGI("Making empty filesystem with command %s\n", cmdline); |
| 1085 | } else { |
| 1086 | SLOGE("cryptfs_enable_wipe(): unknown filesystem type %d\n", type); |
| 1087 | return -1; |
| 1088 | } |
| 1089 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1090 | if (system(cmdline)) { |
| 1091 | SLOGE("Error creating empty filesystem on %s\n", crypto_blkdev); |
| 1092 | } else { |
| 1093 | SLOGD("Successfully created empty filesystem on %s\n", crypto_blkdev); |
| 1094 | rc = 0; |
| 1095 | } |
| 1096 | |
| 1097 | return rc; |
| 1098 | } |
| 1099 | |
| 1100 | static inline int unix_read(int fd, void* buff, int len) |
| 1101 | { |
| 1102 | int ret; |
| 1103 | do { ret = read(fd, buff, len); } while (ret < 0 && errno == EINTR); |
| 1104 | return ret; |
| 1105 | } |
| 1106 | |
| 1107 | static inline int unix_write(int fd, const void* buff, int len) |
| 1108 | { |
| 1109 | int ret; |
| 1110 | do { ret = write(fd, buff, len); } while (ret < 0 && errno == EINTR); |
| 1111 | return ret; |
| 1112 | } |
| 1113 | |
| 1114 | #define CRYPT_INPLACE_BUFSIZE 4096 |
| 1115 | #define CRYPT_SECTORS_PER_BUFSIZE (CRYPT_INPLACE_BUFSIZE / 512) |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1116 | static int cryptfs_enable_inplace(char *crypto_blkdev, char *real_blkdev, off64_t size, |
| 1117 | off64_t *size_already_done, off64_t tot_size) |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1118 | { |
| 1119 | int realfd, cryptofd; |
| 1120 | char *buf[CRYPT_INPLACE_BUFSIZE]; |
| 1121 | int rc = -1; |
| 1122 | off64_t numblocks, i, remainder; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1123 | off64_t one_pct, cur_pct, new_pct; |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1124 | off64_t blocks_already_done, tot_numblocks; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1125 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1126 | if ( (realfd = open(real_blkdev, O_RDONLY)) < 0) { |
| 1127 | SLOGE("Error opening real_blkdev %s for inplace encrypt\n", real_blkdev); |
| 1128 | return -1; |
| 1129 | } |
| 1130 | |
| 1131 | if ( (cryptofd = open(crypto_blkdev, O_WRONLY)) < 0) { |
| 1132 | SLOGE("Error opening crypto_blkdev %s for inplace encrypt\n", crypto_blkdev); |
| 1133 | close(realfd); |
| 1134 | return -1; |
| 1135 | } |
| 1136 | |
| 1137 | /* This is pretty much a simple loop of reading 4K, and writing 4K. |
| 1138 | * The size passed in is the number of 512 byte sectors in the filesystem. |
| 1139 | * So compute the number of whole 4K blocks we should read/write, |
| 1140 | * and the remainder. |
| 1141 | */ |
| 1142 | numblocks = size / CRYPT_SECTORS_PER_BUFSIZE; |
| 1143 | remainder = size % CRYPT_SECTORS_PER_BUFSIZE; |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1144 | tot_numblocks = tot_size / CRYPT_SECTORS_PER_BUFSIZE; |
| 1145 | blocks_already_done = *size_already_done / CRYPT_SECTORS_PER_BUFSIZE; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1146 | |
| 1147 | SLOGE("Encrypting filesystem in place..."); |
| 1148 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1149 | one_pct = tot_numblocks / 100; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1150 | cur_pct = 0; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1151 | /* process the majority of the filesystem in blocks */ |
| 1152 | for (i=0; i<numblocks; i++) { |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1153 | new_pct = (i + blocks_already_done) / one_pct; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1154 | if (new_pct > cur_pct) { |
| 1155 | char buf[8]; |
| 1156 | |
| 1157 | cur_pct = new_pct; |
| 1158 | snprintf(buf, sizeof(buf), "%lld", cur_pct); |
| 1159 | property_set("vold.encrypt_progress", buf); |
| 1160 | } |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1161 | if (unix_read(realfd, buf, CRYPT_INPLACE_BUFSIZE) <= 0) { |
| 1162 | SLOGE("Error reading real_blkdev %s for inplace encrypt\n", crypto_blkdev); |
| 1163 | goto errout; |
| 1164 | } |
| 1165 | if (unix_write(cryptofd, buf, CRYPT_INPLACE_BUFSIZE) <= 0) { |
| 1166 | SLOGE("Error writing crypto_blkdev %s for inplace encrypt\n", crypto_blkdev); |
| 1167 | goto errout; |
| 1168 | } |
| 1169 | } |
| 1170 | |
| 1171 | /* Do any remaining sectors */ |
| 1172 | for (i=0; i<remainder; i++) { |
| 1173 | if (unix_read(realfd, buf, 512) <= 0) { |
| 1174 | SLOGE("Error reading rival sectors from real_blkdev %s for inplace encrypt\n", crypto_blkdev); |
| 1175 | goto errout; |
| 1176 | } |
| 1177 | if (unix_write(cryptofd, buf, 512) <= 0) { |
| 1178 | SLOGE("Error writing final sectors to crypto_blkdev %s for inplace encrypt\n", crypto_blkdev); |
| 1179 | goto errout; |
| 1180 | } |
| 1181 | } |
| 1182 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1183 | *size_already_done += size; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1184 | rc = 0; |
| 1185 | |
| 1186 | errout: |
| 1187 | close(realfd); |
| 1188 | close(cryptofd); |
| 1189 | |
| 1190 | return rc; |
| 1191 | } |
| 1192 | |
| 1193 | #define CRYPTO_ENABLE_WIPE 1 |
| 1194 | #define CRYPTO_ENABLE_INPLACE 2 |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1195 | |
| 1196 | #define FRAMEWORK_BOOT_WAIT 60 |
| 1197 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1198 | static inline int should_encrypt(struct volume_info *volume) |
| 1199 | { |
| 1200 | return (volume->flags & (VOL_ENCRYPTABLE | VOL_NONREMOVABLE)) == |
| 1201 | (VOL_ENCRYPTABLE | VOL_NONREMOVABLE); |
| 1202 | } |
| 1203 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1204 | int cryptfs_enable(char *howarg, char *passwd) |
| 1205 | { |
| 1206 | int how = 0; |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1207 | char crypto_blkdev[MAXPATHLEN], real_blkdev[MAXPATHLEN], sd_crypto_blkdev[MAXPATHLEN]; |
Ken Sumrall | e5032c4 | 2012-04-01 23:58:44 -0700 | [diff] [blame] | 1208 | unsigned long nr_sec; |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 1209 | unsigned char master_key[KEY_LEN_BYTES], decrypted_master_key[KEY_LEN_BYTES]; |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 1210 | unsigned char salt[SALT_LEN]; |
Ken Sumrall | 319b104 | 2011-06-14 14:01:55 -0700 | [diff] [blame] | 1211 | int rc=-1, fd, i, ret; |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1212 | struct crypt_mnt_ftr crypt_ftr, sd_crypt_ftr;; |
| 1213 | char tmpfs_options[PROPERTY_VALUE_MAX]; |
| 1214 | char encrypted_state[PROPERTY_VALUE_MAX]; |
Ken Sumrall | 5d4c68e | 2011-01-30 19:06:03 -0800 | [diff] [blame] | 1215 | char lockid[32] = { 0 }; |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1216 | char key_loc[PROPERTY_VALUE_MAX]; |
| 1217 | char fuse_sdcard[PROPERTY_VALUE_MAX]; |
| 1218 | char *sd_mnt_point; |
| 1219 | char sd_blk_dev[256] = { 0 }; |
| 1220 | int num_vols; |
| 1221 | struct volume_info *vol_list = 0; |
| 1222 | off64_t cur_encryption_done=0, tot_encryption_size=0; |
Ken Sumrall | 0cc1663 | 2011-01-18 20:32:26 -0800 | [diff] [blame] | 1223 | |
| 1224 | property_get("ro.crypto.state", encrypted_state, ""); |
| 1225 | if (strcmp(encrypted_state, "unencrypted")) { |
| 1226 | SLOGE("Device is already running encrypted, aborting"); |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 1227 | goto error_unencrypted; |
Ken Sumrall | 0cc1663 | 2011-01-18 20:32:26 -0800 | [diff] [blame] | 1228 | } |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1229 | |
Ken Sumrall | e5032c4 | 2012-04-01 23:58:44 -0700 | [diff] [blame] | 1230 | fs_mgr_get_crypt_info(get_fstab_filename(), key_loc, 0, sizeof(key_loc)); |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1231 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1232 | if (!strcmp(howarg, "wipe")) { |
| 1233 | how = CRYPTO_ENABLE_WIPE; |
| 1234 | } else if (! strcmp(howarg, "inplace")) { |
| 1235 | how = CRYPTO_ENABLE_INPLACE; |
| 1236 | } else { |
| 1237 | /* Shouldn't happen, as CommandListener vets the args */ |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 1238 | goto error_unencrypted; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1239 | } |
| 1240 | |
Ken Sumrall | e5032c4 | 2012-04-01 23:58:44 -0700 | [diff] [blame] | 1241 | fs_mgr_get_crypt_info(get_fstab_filename(), 0, real_blkdev, sizeof(real_blkdev)); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1242 | |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 1243 | /* Get the size of the real block device */ |
| 1244 | fd = open(real_blkdev, O_RDONLY); |
| 1245 | if ( (nr_sec = get_blkdev_size(fd)) == 0) { |
| 1246 | SLOGE("Cannot get size of block device %s\n", real_blkdev); |
| 1247 | goto error_unencrypted; |
| 1248 | } |
| 1249 | close(fd); |
| 1250 | |
| 1251 | /* If doing inplace encryption, make sure the orig fs doesn't include the crypto footer */ |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1252 | if ((how == CRYPTO_ENABLE_INPLACE) && (!strcmp(key_loc, KEY_IN_FOOTER))) { |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 1253 | unsigned int fs_size_sec, max_fs_size_sec; |
| 1254 | |
| 1255 | fs_size_sec = get_fs_size(real_blkdev); |
| 1256 | max_fs_size_sec = nr_sec - (CRYPT_FOOTER_OFFSET / 512); |
| 1257 | |
| 1258 | if (fs_size_sec > max_fs_size_sec) { |
| 1259 | SLOGE("Orig filesystem overlaps crypto footer region. Cannot encrypt in place."); |
| 1260 | goto error_unencrypted; |
| 1261 | } |
| 1262 | } |
| 1263 | |
Ken Sumrall | 5d4c68e | 2011-01-30 19:06:03 -0800 | [diff] [blame] | 1264 | /* Get a wakelock as this may take a while, and we don't want the |
| 1265 | * device to sleep on us. We'll grab a partial wakelock, and if the UI |
| 1266 | * wants to keep the screen on, it can grab a full wakelock. |
| 1267 | */ |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1268 | snprintf(lockid, sizeof(lockid), "enablecrypto%d", (int) getpid()); |
Ken Sumrall | 5d4c68e | 2011-01-30 19:06:03 -0800 | [diff] [blame] | 1269 | acquire_wake_lock(PARTIAL_WAKE_LOCK, lockid); |
| 1270 | |
Jeff Sharkey | 7382f81 | 2012-08-23 14:08:59 -0700 | [diff] [blame] | 1271 | /* Get the sdcard mount point */ |
Jeff Sharkey | b77bc46 | 2012-10-01 14:36:26 -0700 | [diff] [blame] | 1272 | sd_mnt_point = getenv("EMULATED_STORAGE_SOURCE"); |
Jeff Sharkey | 7382f81 | 2012-08-23 14:08:59 -0700 | [diff] [blame] | 1273 | if (!sd_mnt_point) { |
| 1274 | sd_mnt_point = getenv("EXTERNAL_STORAGE"); |
| 1275 | } |
| 1276 | if (!sd_mnt_point) { |
| 1277 | sd_mnt_point = "/mnt/sdcard"; |
| 1278 | } |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1279 | |
| 1280 | num_vols=vold_getNumDirectVolumes(); |
| 1281 | vol_list = malloc(sizeof(struct volume_info) * num_vols); |
| 1282 | vold_getDirectVolumeList(vol_list); |
| 1283 | |
| 1284 | for (i=0; i<num_vols; i++) { |
| 1285 | if (should_encrypt(&vol_list[i])) { |
| 1286 | fd = open(vol_list[i].blk_dev, O_RDONLY); |
| 1287 | if ( (vol_list[i].size = get_blkdev_size(fd)) == 0) { |
| 1288 | SLOGE("Cannot get size of block device %s\n", vol_list[i].blk_dev); |
| 1289 | goto error_unencrypted; |
| 1290 | } |
| 1291 | close(fd); |
| 1292 | |
Ken Sumrall | 3b17005 | 2011-07-11 15:38:57 -0700 | [diff] [blame] | 1293 | ret=vold_disableVol(vol_list[i].label); |
Ken Sumrall | 319b104 | 2011-06-14 14:01:55 -0700 | [diff] [blame] | 1294 | if ((ret < 0) && (ret != UNMOUNT_NOT_MOUNTED_ERR)) { |
| 1295 | /* -2 is returned when the device exists but is not currently mounted. |
| 1296 | * ignore the error and continue. */ |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1297 | SLOGE("Failed to unmount volume %s\n", vol_list[i].label); |
| 1298 | goto error_unencrypted; |
| 1299 | } |
| 1300 | } |
| 1301 | } |
| 1302 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1303 | /* The init files are setup to stop the class main and late start when |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1304 | * vold sets trigger_shutdown_framework. |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1305 | */ |
| 1306 | property_set("vold.decrypt", "trigger_shutdown_framework"); |
| 1307 | SLOGD("Just asked init to shut down class main\n"); |
| 1308 | |
Ken Sumrall | 425524d | 2012-06-14 20:55:28 -0700 | [diff] [blame] | 1309 | if (vold_unmountAllAsecs()) { |
| 1310 | /* Just report the error. If any are left mounted, |
| 1311 | * umounting /data below will fail and handle the error. |
| 1312 | */ |
| 1313 | SLOGE("Error unmounting internal asecs"); |
| 1314 | } |
| 1315 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1316 | property_get("ro.crypto.fuse_sdcard", fuse_sdcard, ""); |
| 1317 | if (!strcmp(fuse_sdcard, "true")) { |
| 1318 | /* This is a device using the fuse layer to emulate the sdcard semantics |
| 1319 | * on top of the userdata partition. vold does not manage it, it is managed |
| 1320 | * by the sdcard service. The sdcard service was killed by the property trigger |
| 1321 | * above, so just unmount it now. We must do this _AFTER_ killing the framework, |
| 1322 | * unlike the case for vold managed devices above. |
| 1323 | */ |
| 1324 | if (wait_and_unmount(sd_mnt_point)) { |
| 1325 | goto error_shutting_down; |
| 1326 | } |
Ken Sumrall | 2eaf713 | 2011-01-14 12:45:48 -0800 | [diff] [blame] | 1327 | } |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1328 | |
| 1329 | /* Now unmount the /data partition. */ |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1330 | if (wait_and_unmount(DATA_MNT_POINT)) { |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 1331 | goto error_shutting_down; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1332 | } |
| 1333 | |
| 1334 | /* Do extra work for a better UX when doing the long inplace encryption */ |
| 1335 | if (how == CRYPTO_ENABLE_INPLACE) { |
| 1336 | /* Now that /data is unmounted, we need to mount a tmpfs |
| 1337 | * /data, set a property saying we're doing inplace encryption, |
| 1338 | * and restart the framework. |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1339 | */ |
Ken Sumrall | e5032c4 | 2012-04-01 23:58:44 -0700 | [diff] [blame] | 1340 | if (fs_mgr_do_tmpfs_mount(DATA_MNT_POINT)) { |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 1341 | goto error_shutting_down; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1342 | } |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1343 | /* Tells the framework that inplace encryption is starting */ |
Ken Sumrall | 7df8412 | 2011-01-18 14:04:08 -0800 | [diff] [blame] | 1344 | property_set("vold.encrypt_progress", "0"); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1345 | |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1346 | /* restart the framework. */ |
| 1347 | /* Create necessary paths on /data */ |
| 1348 | if (prep_data_fs()) { |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 1349 | goto error_shutting_down; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1350 | } |
| 1351 | |
Ken Sumrall | 92736ef | 2012-10-17 20:57:14 -0700 | [diff] [blame] | 1352 | /* Ugh, shutting down the framework is not synchronous, so until it |
| 1353 | * can be fixed, this horrible hack will wait a moment for it all to |
| 1354 | * shut down before proceeding. Without it, some devices cannot |
| 1355 | * restart the graphics services. |
| 1356 | */ |
| 1357 | sleep(2); |
| 1358 | |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1359 | /* startup service classes main and late_start */ |
| 1360 | property_set("vold.decrypt", "trigger_restart_min_framework"); |
| 1361 | SLOGD("Just triggered restart_min_framework\n"); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1362 | |
Ken Sumrall | 7df8412 | 2011-01-18 14:04:08 -0800 | [diff] [blame] | 1363 | /* OK, the framework is restarted and will soon be showing a |
| 1364 | * progress bar. Time to setup an encrypted mapping, and |
| 1365 | * either write a new filesystem, or encrypt in place updating |
| 1366 | * the progress bar as we work. |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1367 | */ |
| 1368 | } |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1369 | |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1370 | /* Start the actual work of making an encrypted filesystem */ |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1371 | /* Initialize a crypt_mnt_ftr for the partition */ |
| 1372 | cryptfs_init_crypt_mnt_ftr(&crypt_ftr); |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1373 | if (!strcmp(key_loc, KEY_IN_FOOTER)) { |
| 1374 | crypt_ftr.fs_size = nr_sec - (CRYPT_FOOTER_OFFSET / 512); |
| 1375 | } else { |
| 1376 | crypt_ftr.fs_size = nr_sec; |
| 1377 | } |
Ken Sumrall | d33d417 | 2011-02-01 00:49:13 -0800 | [diff] [blame] | 1378 | crypt_ftr.flags |= CRYPT_ENCRYPTION_IN_PROGRESS; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1379 | strcpy((char *)crypt_ftr.crypto_type_name, "aes-cbc-essiv:sha256"); |
| 1380 | |
| 1381 | /* Make an encrypted master key */ |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 1382 | if (create_encrypted_random_key(passwd, master_key, salt)) { |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1383 | SLOGE("Cannot create encrypted master key\n"); |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 1384 | goto error_unencrypted; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1385 | } |
| 1386 | |
| 1387 | /* Write the key to the end of the partition */ |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 1388 | put_crypt_ftr_and_key(real_blkdev, &crypt_ftr, master_key, salt); |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1389 | |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 1390 | decrypt_master_key(passwd, salt, master_key, decrypted_master_key); |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1391 | create_crypto_blk_dev(&crypt_ftr, decrypted_master_key, real_blkdev, crypto_blkdev, |
| 1392 | "userdata"); |
| 1393 | |
Ken Sumrall | 128626f | 2011-06-28 18:45:14 -0700 | [diff] [blame] | 1394 | /* The size of the userdata partition, and add in the vold volumes below */ |
| 1395 | tot_encryption_size = crypt_ftr.fs_size; |
| 1396 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1397 | /* setup crypto mapping for all encryptable volumes handled by vold */ |
| 1398 | for (i=0; i<num_vols; i++) { |
| 1399 | if (should_encrypt(&vol_list[i])) { |
| 1400 | vol_list[i].crypt_ftr = crypt_ftr; /* gotta love struct assign */ |
| 1401 | vol_list[i].crypt_ftr.fs_size = vol_list[i].size; |
| 1402 | create_crypto_blk_dev(&vol_list[i].crypt_ftr, decrypted_master_key, |
| 1403 | vol_list[i].blk_dev, vol_list[i].crypto_blkdev, |
| 1404 | vol_list[i].label); |
Ken Sumrall | 128626f | 2011-06-28 18:45:14 -0700 | [diff] [blame] | 1405 | tot_encryption_size += vol_list[i].size; |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1406 | } |
| 1407 | } |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1408 | |
| 1409 | if (how == CRYPTO_ENABLE_WIPE) { |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1410 | rc = cryptfs_enable_wipe(crypto_blkdev, crypt_ftr.fs_size, EXT4_FS); |
| 1411 | /* Encrypt all encryptable volumes handled by vold */ |
| 1412 | if (!rc) { |
| 1413 | for (i=0; i<num_vols; i++) { |
| 1414 | if (should_encrypt(&vol_list[i])) { |
| 1415 | rc = cryptfs_enable_wipe(vol_list[i].crypto_blkdev, |
| 1416 | vol_list[i].crypt_ftr.fs_size, FAT_FS); |
| 1417 | } |
| 1418 | } |
| 1419 | } |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1420 | } else if (how == CRYPTO_ENABLE_INPLACE) { |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1421 | rc = cryptfs_enable_inplace(crypto_blkdev, real_blkdev, crypt_ftr.fs_size, |
| 1422 | &cur_encryption_done, tot_encryption_size); |
| 1423 | /* Encrypt all encryptable volumes handled by vold */ |
| 1424 | if (!rc) { |
| 1425 | for (i=0; i<num_vols; i++) { |
| 1426 | if (should_encrypt(&vol_list[i])) { |
| 1427 | rc = cryptfs_enable_inplace(vol_list[i].crypto_blkdev, |
| 1428 | vol_list[i].blk_dev, |
| 1429 | vol_list[i].crypt_ftr.fs_size, |
| 1430 | &cur_encryption_done, tot_encryption_size); |
| 1431 | } |
| 1432 | } |
| 1433 | } |
| 1434 | if (!rc) { |
| 1435 | /* The inplace routine never actually sets the progress to 100% |
| 1436 | * due to the round down nature of integer division, so set it here */ |
| 1437 | property_set("vold.encrypt_progress", "100"); |
| 1438 | } |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1439 | } else { |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1440 | /* Shouldn't happen */ |
| 1441 | SLOGE("cryptfs_enable: internal error, unknown option\n"); |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 1442 | goto error_unencrypted; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1443 | } |
| 1444 | |
| 1445 | /* Undo the dm-crypt mapping whether we succeed or not */ |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1446 | delete_crypto_blk_dev("userdata"); |
| 1447 | for (i=0; i<num_vols; i++) { |
| 1448 | if (should_encrypt(&vol_list[i])) { |
| 1449 | delete_crypto_blk_dev(vol_list[i].label); |
| 1450 | } |
| 1451 | } |
| 1452 | |
| 1453 | free(vol_list); |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1454 | |
| 1455 | if (! rc) { |
| 1456 | /* Success */ |
Ken Sumrall | 7f7dbaa | 2011-02-01 15:46:41 -0800 | [diff] [blame] | 1457 | |
Ken Sumrall | d33d417 | 2011-02-01 00:49:13 -0800 | [diff] [blame] | 1458 | /* Clear the encryption in progres flag in the footer */ |
| 1459 | crypt_ftr.flags &= ~CRYPT_ENCRYPTION_IN_PROGRESS; |
| 1460 | put_crypt_ftr_and_key(real_blkdev, &crypt_ftr, 0, 0); |
| 1461 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1462 | sleep(2); /* Give the UI a chance to show 100% progress */ |
Ken Sumrall | c290eaf | 2011-03-07 23:40:35 -0800 | [diff] [blame] | 1463 | android_reboot(ANDROID_RB_RESTART, 0, 0); |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 1464 | } else { |
Mike Lockwood | ee6d8c4 | 2012-02-15 13:43:28 -0800 | [diff] [blame] | 1465 | char value[PROPERTY_VALUE_MAX]; |
| 1466 | |
Ken Sumrall | 319369a | 2012-06-27 16:30:18 -0700 | [diff] [blame] | 1467 | property_get("ro.vold.wipe_on_crypt_fail", value, "0"); |
Mike Lockwood | ee6d8c4 | 2012-02-15 13:43:28 -0800 | [diff] [blame] | 1468 | if (!strcmp(value, "1")) { |
| 1469 | /* wipe data if encryption failed */ |
| 1470 | SLOGE("encryption failed - rebooting into recovery to wipe data\n"); |
| 1471 | mkdir("/cache/recovery", 0700); |
Nick Kralevich | 4684e58 | 2012-06-26 15:07:03 -0700 | [diff] [blame] | 1472 | int fd = open("/cache/recovery/command", O_RDWR|O_CREAT|O_TRUNC, 0600); |
Mike Lockwood | ee6d8c4 | 2012-02-15 13:43:28 -0800 | [diff] [blame] | 1473 | if (fd >= 0) { |
| 1474 | write(fd, "--wipe_data", strlen("--wipe_data") + 1); |
| 1475 | close(fd); |
| 1476 | } else { |
| 1477 | SLOGE("could not open /cache/recovery/command\n"); |
| 1478 | } |
| 1479 | android_reboot(ANDROID_RB_RESTART2, 0, "recovery"); |
| 1480 | } else { |
| 1481 | /* set property to trigger dialog */ |
| 1482 | property_set("vold.encrypt_progress", "error_partially_encrypted"); |
| 1483 | release_wake_lock(lockid); |
| 1484 | } |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 1485 | return -1; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1486 | } |
| 1487 | |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 1488 | /* hrm, the encrypt step claims success, but the reboot failed. |
| 1489 | * This should not happen. |
| 1490 | * Set the property and return. Hope the framework can deal with it. |
| 1491 | */ |
| 1492 | property_set("vold.encrypt_progress", "error_reboot_failed"); |
Ken Sumrall | 5d4c68e | 2011-01-30 19:06:03 -0800 | [diff] [blame] | 1493 | release_wake_lock(lockid); |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1494 | return rc; |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 1495 | |
| 1496 | error_unencrypted: |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1497 | free(vol_list); |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 1498 | property_set("vold.encrypt_progress", "error_not_encrypted"); |
Ken Sumrall | 5d4c68e | 2011-01-30 19:06:03 -0800 | [diff] [blame] | 1499 | if (lockid[0]) { |
| 1500 | release_wake_lock(lockid); |
| 1501 | } |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 1502 | return -1; |
| 1503 | |
| 1504 | error_shutting_down: |
| 1505 | /* we failed, and have not encrypted anthing, so the users's data is still intact, |
| 1506 | * but the framework is stopped and not restarted to show the error, so it's up to |
| 1507 | * vold to restart the system. |
| 1508 | */ |
| 1509 | SLOGE("Error enabling encryption after framework is shutdown, no data changed, restarting system"); |
Ken Sumrall | c290eaf | 2011-03-07 23:40:35 -0800 | [diff] [blame] | 1510 | android_reboot(ANDROID_RB_RESTART, 0, 0); |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 1511 | |
| 1512 | /* shouldn't get here */ |
| 1513 | property_set("vold.encrypt_progress", "error_shutting_down"); |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1514 | free(vol_list); |
Ken Sumrall | 5d4c68e | 2011-01-30 19:06:03 -0800 | [diff] [blame] | 1515 | if (lockid[0]) { |
| 1516 | release_wake_lock(lockid); |
| 1517 | } |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 1518 | return -1; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1519 | } |
| 1520 | |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 1521 | int cryptfs_changepw(char *newpw) |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1522 | { |
| 1523 | struct crypt_mnt_ftr crypt_ftr; |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 1524 | unsigned char encrypted_master_key[KEY_LEN_BYTES], decrypted_master_key[KEY_LEN_BYTES]; |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 1525 | unsigned char salt[SALT_LEN]; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1526 | char real_blkdev[MAXPATHLEN]; |
| 1527 | |
| 1528 | /* This is only allowed after we've successfully decrypted the master key */ |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 1529 | if (! master_key_saved) { |
Ken Sumrall | 0cc1663 | 2011-01-18 20:32:26 -0800 | [diff] [blame] | 1530 | SLOGE("Key not saved, aborting"); |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1531 | return -1; |
| 1532 | } |
| 1533 | |
Ken Sumrall | e5032c4 | 2012-04-01 23:58:44 -0700 | [diff] [blame] | 1534 | fs_mgr_get_crypt_info(get_fstab_filename(), 0, real_blkdev, sizeof(real_blkdev)); |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1535 | if (strlen(real_blkdev) == 0) { |
Ken Sumrall | 57b63e6 | 2011-01-17 18:29:19 -0800 | [diff] [blame] | 1536 | SLOGE("Can't find real blkdev"); |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1537 | return -1; |
| 1538 | } |
| 1539 | |
| 1540 | /* get key */ |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 1541 | if (get_crypt_ftr_and_key(real_blkdev, &crypt_ftr, encrypted_master_key, salt)) { |
Ken Sumrall | 57b63e6 | 2011-01-17 18:29:19 -0800 | [diff] [blame] | 1542 | SLOGE("Error getting crypt footer and key"); |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1543 | return -1; |
| 1544 | } |
| 1545 | |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 1546 | encrypt_master_key(newpw, salt, saved_master_key, encrypted_master_key); |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1547 | |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 1548 | /* save the key */ |
| 1549 | put_crypt_ftr_and_key(real_blkdev, &crypt_ftr, encrypted_master_key, salt); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1550 | |
| 1551 | return 0; |
| 1552 | } |