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