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