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