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