Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 Google, Inc. |
| 3 | * |
| 4 | * This software is licensed under the terms of the GNU General Public |
| 5 | * License version 2, as published by the Free Software Foundation, and |
| 6 | * may be copied, distributed, and modified under those terms. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | */ |
| 14 | |
| 15 | #include <linux/buffer_head.h> |
Badhri Jagan Sridharan | d0706bc | 2016-03-21 10:55:23 -0700 | [diff] [blame] | 16 | #include <linux/debugfs.h> |
Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 17 | #include <linux/delay.h> |
| 18 | #include <linux/device.h> |
| 19 | #include <linux/device-mapper.h> |
| 20 | #include <linux/errno.h> |
| 21 | #include <linux/fs.h> |
| 22 | #include <linux/fcntl.h> |
| 23 | #include <linux/init.h> |
| 24 | #include <linux/kernel.h> |
| 25 | #include <linux/key.h> |
| 26 | #include <linux/module.h> |
| 27 | #include <linux/mount.h> |
| 28 | #include <linux/namei.h> |
| 29 | #include <linux/of.h> |
| 30 | #include <linux/reboot.h> |
| 31 | #include <linux/string.h> |
| 32 | #include <linux/vmalloc.h> |
| 33 | |
| 34 | #include <asm/setup.h> |
| 35 | #include <crypto/hash.h> |
| 36 | #include <crypto/public_key.h> |
| 37 | #include <crypto/sha.h> |
| 38 | #include <keys/asymmetric-type.h> |
| 39 | #include <keys/system_keyring.h> |
| 40 | |
| 41 | #include "dm-verity.h" |
| 42 | #include "dm-android-verity.h" |
| 43 | |
| 44 | static char verifiedbootstate[VERITY_COMMANDLINE_PARAM_LENGTH]; |
| 45 | static char veritymode[VERITY_COMMANDLINE_PARAM_LENGTH]; |
Badhri Jagan Sridharan | adb82b9 | 2016-05-20 16:45:45 -0700 | [diff] [blame] | 46 | static char veritykeyid[VERITY_DEFAULT_KEY_ID_LENGTH]; |
Badhri Jagan Sridharan | c68a615 | 2016-06-17 18:54:35 -0700 | [diff] [blame] | 47 | static char buildvariant[BUILD_VARIANT]; |
Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 48 | |
Badhri Jagan Sridharan | d0706bc | 2016-03-21 10:55:23 -0700 | [diff] [blame] | 49 | static bool target_added; |
| 50 | static bool verity_enabled = true; |
| 51 | struct dentry *debug_dir; |
| 52 | static int android_verity_ctr(struct dm_target *ti, unsigned argc, char **argv); |
| 53 | |
| 54 | static struct target_type android_verity_target = { |
| 55 | .name = "android-verity", |
| 56 | .version = {1, 0, 0}, |
| 57 | .module = THIS_MODULE, |
| 58 | .ctr = android_verity_ctr, |
| 59 | .dtr = verity_dtr, |
| 60 | .map = verity_map, |
| 61 | .status = verity_status, |
Badhri Jagan Sridharan | 424861b | 2016-08-09 12:47:37 -0700 | [diff] [blame] | 62 | .prepare_ioctl = verity_prepare_ioctl, |
Badhri Jagan Sridharan | d0706bc | 2016-03-21 10:55:23 -0700 | [diff] [blame] | 63 | .iterate_devices = verity_iterate_devices, |
| 64 | .io_hints = verity_io_hints, |
| 65 | }; |
| 66 | |
Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 67 | static int __init verified_boot_state_param(char *line) |
| 68 | { |
| 69 | strlcpy(verifiedbootstate, line, sizeof(verifiedbootstate)); |
| 70 | return 1; |
| 71 | } |
| 72 | |
| 73 | __setup("androidboot.verifiedbootstate=", verified_boot_state_param); |
| 74 | |
| 75 | static int __init verity_mode_param(char *line) |
| 76 | { |
| 77 | strlcpy(veritymode, line, sizeof(veritymode)); |
| 78 | return 1; |
| 79 | } |
| 80 | |
| 81 | __setup("androidboot.veritymode=", verity_mode_param); |
| 82 | |
Badhri Jagan Sridharan | adb82b9 | 2016-05-20 16:45:45 -0700 | [diff] [blame] | 83 | static int __init verity_keyid_param(char *line) |
| 84 | { |
| 85 | strlcpy(veritykeyid, line, sizeof(veritykeyid)); |
| 86 | return 1; |
| 87 | } |
| 88 | |
| 89 | __setup("veritykeyid=", verity_keyid_param); |
| 90 | |
Badhri Jagan Sridharan | c68a615 | 2016-06-17 18:54:35 -0700 | [diff] [blame] | 91 | static int __init verity_buildvariant(char *line) |
| 92 | { |
| 93 | strlcpy(buildvariant, line, sizeof(buildvariant)); |
| 94 | return 1; |
| 95 | } |
| 96 | |
| 97 | __setup("buildvariant=", verity_buildvariant); |
| 98 | |
Badhri Jagan Sridharan | adb82b9 | 2016-05-20 16:45:45 -0700 | [diff] [blame] | 99 | static inline bool default_verity_key_id(void) |
| 100 | { |
| 101 | return veritykeyid[0] != '\0'; |
| 102 | } |
| 103 | |
Badhri Jagan Sridharan | c68a615 | 2016-06-17 18:54:35 -0700 | [diff] [blame] | 104 | static inline bool is_eng(void) |
| 105 | { |
| 106 | static const char typeeng[] = "eng"; |
| 107 | |
| 108 | return !strncmp(buildvariant, typeeng, sizeof(typeeng)); |
| 109 | } |
| 110 | |
Badhri Jagan Sridharan | d5943e0 | 2016-06-27 16:25:55 -0700 | [diff] [blame] | 111 | static inline bool is_userdebug(void) |
| 112 | { |
| 113 | static const char typeuserdebug[] = "userdebug"; |
| 114 | |
| 115 | return !strncmp(buildvariant, typeuserdebug, sizeof(typeuserdebug)); |
| 116 | } |
| 117 | |
| 118 | |
Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 119 | static int table_extract_mpi_array(struct public_key_signature *pks, |
| 120 | const void *data, size_t len) |
| 121 | { |
| 122 | MPI mpi = mpi_read_raw_data(data, len); |
| 123 | |
| 124 | if (!mpi) { |
| 125 | DMERR("Error while allocating mpi array"); |
| 126 | return -ENOMEM; |
| 127 | } |
| 128 | |
| 129 | pks->mpi[0] = mpi; |
| 130 | pks->nr_mpi = 1; |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | static struct public_key_signature *table_make_digest( |
Badhri Jagan Sridharan | 56f6a6b | 2016-02-08 16:28:43 -0800 | [diff] [blame] | 135 | enum hash_algo hash, |
Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 136 | const void *table, |
| 137 | unsigned long table_len) |
| 138 | { |
| 139 | struct public_key_signature *pks = NULL; |
| 140 | struct crypto_shash *tfm; |
| 141 | struct shash_desc *desc; |
| 142 | size_t digest_size, desc_size; |
| 143 | int ret; |
| 144 | |
| 145 | /* Allocate the hashing algorithm we're going to need and find out how |
| 146 | * big the hash operational data will be. |
| 147 | */ |
Badhri Jagan Sridharan | 56f6a6b | 2016-02-08 16:28:43 -0800 | [diff] [blame] | 148 | tfm = crypto_alloc_shash(hash_algo_name[hash], 0, 0); |
Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 149 | if (IS_ERR(tfm)) |
| 150 | return ERR_CAST(tfm); |
| 151 | |
| 152 | desc_size = crypto_shash_descsize(tfm) + sizeof(*desc); |
| 153 | digest_size = crypto_shash_digestsize(tfm); |
| 154 | |
| 155 | /* We allocate the hash operational data storage on the end of out |
| 156 | * context data and the digest output buffer on the end of that. |
| 157 | */ |
| 158 | ret = -ENOMEM; |
| 159 | pks = kzalloc(digest_size + sizeof(*pks) + desc_size, GFP_KERNEL); |
| 160 | if (!pks) |
| 161 | goto error; |
| 162 | |
| 163 | pks->pkey_hash_algo = hash; |
| 164 | pks->digest = (u8 *)pks + sizeof(*pks) + desc_size; |
| 165 | pks->digest_size = digest_size; |
| 166 | |
| 167 | desc = (struct shash_desc *)(pks + 1); |
| 168 | desc->tfm = tfm; |
| 169 | desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP; |
| 170 | |
| 171 | ret = crypto_shash_init(desc); |
| 172 | if (ret < 0) |
| 173 | goto error; |
| 174 | |
| 175 | ret = crypto_shash_finup(desc, table, table_len, pks->digest); |
| 176 | if (ret < 0) |
| 177 | goto error; |
| 178 | |
| 179 | crypto_free_shash(tfm); |
| 180 | return pks; |
| 181 | |
| 182 | error: |
| 183 | kfree(pks); |
| 184 | crypto_free_shash(tfm); |
| 185 | return ERR_PTR(ret); |
| 186 | } |
| 187 | |
| 188 | static int read_block_dev(struct bio_read *payload, struct block_device *bdev, |
| 189 | sector_t offset, int length) |
| 190 | { |
| 191 | struct bio *bio; |
| 192 | int err = 0, i; |
| 193 | |
| 194 | payload->number_of_pages = DIV_ROUND_UP(length, PAGE_SIZE); |
| 195 | |
| 196 | bio = bio_alloc(GFP_KERNEL, payload->number_of_pages); |
| 197 | if (!bio) { |
| 198 | DMERR("Error while allocating bio"); |
| 199 | return -ENOMEM; |
| 200 | } |
| 201 | |
| 202 | bio->bi_bdev = bdev; |
Badhri Jagan Sridharan | 56f6a6b | 2016-02-08 16:28:43 -0800 | [diff] [blame] | 203 | bio->bi_iter.bi_sector = offset; |
Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 204 | |
| 205 | payload->page_io = kzalloc(sizeof(struct page *) * |
| 206 | payload->number_of_pages, GFP_KERNEL); |
| 207 | if (!payload->page_io) { |
| 208 | DMERR("page_io array alloc failed"); |
| 209 | err = -ENOMEM; |
| 210 | goto free_bio; |
| 211 | } |
| 212 | |
| 213 | for (i = 0; i < payload->number_of_pages; i++) { |
| 214 | payload->page_io[i] = alloc_page(GFP_KERNEL); |
| 215 | if (!payload->page_io[i]) { |
| 216 | DMERR("alloc_page failed"); |
| 217 | err = -ENOMEM; |
| 218 | goto free_pages; |
| 219 | } |
| 220 | if (!bio_add_page(bio, payload->page_io[i], PAGE_SIZE, 0)) { |
| 221 | DMERR("bio_add_page error"); |
| 222 | err = -EIO; |
| 223 | goto free_pages; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | if (!submit_bio_wait(READ, bio)) |
| 228 | /* success */ |
| 229 | goto free_bio; |
| 230 | DMERR("bio read failed"); |
| 231 | err = -EIO; |
| 232 | |
| 233 | free_pages: |
| 234 | for (i = 0; i < payload->number_of_pages; i++) |
| 235 | if (payload->page_io[i]) |
| 236 | __free_page(payload->page_io[i]); |
| 237 | kfree(payload->page_io); |
| 238 | free_bio: |
| 239 | bio_put(bio); |
| 240 | return err; |
| 241 | } |
| 242 | |
| 243 | static inline u64 fec_div_round_up(u64 x, u64 y) |
| 244 | { |
| 245 | u64 remainder; |
| 246 | |
| 247 | return div64_u64_rem(x, y, &remainder) + |
| 248 | (remainder > 0 ? 1 : 0); |
| 249 | } |
| 250 | |
| 251 | static inline void populate_fec_metadata(struct fec_header *header, |
| 252 | struct fec_ecc_metadata *ecc) |
| 253 | { |
| 254 | ecc->blocks = fec_div_round_up(le64_to_cpu(header->inp_size), |
| 255 | FEC_BLOCK_SIZE); |
| 256 | ecc->roots = le32_to_cpu(header->roots); |
| 257 | ecc->start = le64_to_cpu(header->inp_size); |
| 258 | } |
| 259 | |
| 260 | static inline int validate_fec_header(struct fec_header *header, u64 offset) |
| 261 | { |
| 262 | /* move offset to make the sanity check work for backup header |
| 263 | * as well. */ |
| 264 | offset -= offset % FEC_BLOCK_SIZE; |
| 265 | if (le32_to_cpu(header->magic) != FEC_MAGIC || |
| 266 | le32_to_cpu(header->version) != FEC_VERSION || |
| 267 | le32_to_cpu(header->size) != sizeof(struct fec_header) || |
| 268 | le32_to_cpu(header->roots) == 0 || |
Badhri Jagan Sridharan | 07bec5d | 2016-09-27 13:48:29 -0700 | [diff] [blame^] | 269 | le32_to_cpu(header->roots) >= FEC_RSM) |
Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 270 | return -EINVAL; |
| 271 | |
| 272 | return 0; |
| 273 | } |
| 274 | |
| 275 | static int extract_fec_header(dev_t dev, struct fec_header *fec, |
| 276 | struct fec_ecc_metadata *ecc) |
| 277 | { |
| 278 | u64 device_size; |
| 279 | struct bio_read payload; |
| 280 | int i, err = 0; |
| 281 | struct block_device *bdev; |
| 282 | |
| 283 | bdev = blkdev_get_by_dev(dev, FMODE_READ, NULL); |
| 284 | |
Badhri Jagan Sridharan | c68a615 | 2016-06-17 18:54:35 -0700 | [diff] [blame] | 285 | if (IS_ERR_OR_NULL(bdev)) { |
Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 286 | DMERR("bdev get error"); |
| 287 | return PTR_ERR(bdev); |
| 288 | } |
| 289 | |
| 290 | device_size = i_size_read(bdev->bd_inode); |
| 291 | |
| 292 | /* fec metadata size is a power of 2 and PAGE_SIZE |
| 293 | * is a power of 2 as well. |
| 294 | */ |
| 295 | BUG_ON(FEC_BLOCK_SIZE > PAGE_SIZE); |
| 296 | /* 512 byte sector alignment */ |
| 297 | BUG_ON(((device_size - FEC_BLOCK_SIZE) % (1 << SECTOR_SHIFT)) != 0); |
| 298 | |
| 299 | err = read_block_dev(&payload, bdev, (device_size - |
| 300 | FEC_BLOCK_SIZE) / (1 << SECTOR_SHIFT), FEC_BLOCK_SIZE); |
| 301 | if (err) { |
| 302 | DMERR("Error while reading verity metadata"); |
| 303 | goto error; |
| 304 | } |
| 305 | |
| 306 | BUG_ON(sizeof(struct fec_header) > PAGE_SIZE); |
| 307 | memcpy(fec, page_address(payload.page_io[0]), |
| 308 | sizeof(*fec)); |
| 309 | |
| 310 | ecc->valid = true; |
| 311 | if (validate_fec_header(fec, device_size - FEC_BLOCK_SIZE)) { |
| 312 | /* Try the backup header */ |
| 313 | memcpy(fec, page_address(payload.page_io[0]) + FEC_BLOCK_SIZE |
| 314 | - sizeof(*fec) , |
| 315 | sizeof(*fec)); |
| 316 | if (validate_fec_header(fec, device_size - |
| 317 | sizeof(struct fec_header))) |
| 318 | ecc->valid = false; |
| 319 | } |
| 320 | |
| 321 | if (ecc->valid) |
| 322 | populate_fec_metadata(fec, ecc); |
| 323 | |
| 324 | for (i = 0; i < payload.number_of_pages; i++) |
| 325 | __free_page(payload.page_io[i]); |
| 326 | kfree(payload.page_io); |
| 327 | |
| 328 | error: |
| 329 | blkdev_put(bdev, FMODE_READ); |
| 330 | return err; |
| 331 | } |
| 332 | static void find_metadata_offset(struct fec_header *fec, |
| 333 | struct block_device *bdev, u64 *metadata_offset) |
| 334 | { |
| 335 | u64 device_size; |
| 336 | |
| 337 | device_size = i_size_read(bdev->bd_inode); |
| 338 | |
| 339 | if (le32_to_cpu(fec->magic) == FEC_MAGIC) |
| 340 | *metadata_offset = le64_to_cpu(fec->inp_size) - |
| 341 | VERITY_METADATA_SIZE; |
| 342 | else |
| 343 | *metadata_offset = device_size - VERITY_METADATA_SIZE; |
| 344 | } |
| 345 | |
Badhri Jagan Sridharan | c68a615 | 2016-06-17 18:54:35 -0700 | [diff] [blame] | 346 | static int find_size(dev_t dev, u64 *device_size) |
| 347 | { |
| 348 | struct block_device *bdev; |
| 349 | |
| 350 | bdev = blkdev_get_by_dev(dev, FMODE_READ, NULL); |
| 351 | if (IS_ERR_OR_NULL(bdev)) { |
| 352 | DMERR("blkdev_get_by_dev failed"); |
| 353 | return PTR_ERR(bdev); |
| 354 | } |
| 355 | |
| 356 | *device_size = i_size_read(bdev->bd_inode); |
| 357 | *device_size >>= SECTOR_SHIFT; |
| 358 | |
| 359 | DMINFO("blkdev size in sectors: %llu", *device_size); |
| 360 | blkdev_put(bdev, FMODE_READ); |
| 361 | return 0; |
| 362 | } |
| 363 | |
Badhri Jagan Sridharan | 7635064 | 2016-07-06 17:16:19 -0700 | [diff] [blame] | 364 | static int verify_header(struct android_metadata_header *header) |
| 365 | { |
| 366 | int retval = -EINVAL; |
| 367 | |
| 368 | if (is_userdebug() && le32_to_cpu(header->magic_number) == |
| 369 | VERITY_METADATA_MAGIC_DISABLE) |
| 370 | return VERITY_STATE_DISABLE; |
| 371 | |
| 372 | if (!(le32_to_cpu(header->magic_number) == |
| 373 | VERITY_METADATA_MAGIC_NUMBER) || |
| 374 | (le32_to_cpu(header->magic_number) == |
| 375 | VERITY_METADATA_MAGIC_DISABLE)) { |
| 376 | DMERR("Incorrect magic number"); |
| 377 | return retval; |
| 378 | } |
| 379 | |
| 380 | if (le32_to_cpu(header->protocol_version) != |
| 381 | VERITY_METADATA_VERSION) { |
| 382 | DMERR("Unsupported version %u", |
| 383 | le32_to_cpu(header->protocol_version)); |
| 384 | return retval; |
| 385 | } |
| 386 | |
| 387 | return 0; |
| 388 | } |
| 389 | |
| 390 | static int extract_metadata(dev_t dev, struct fec_header *fec, |
| 391 | struct android_metadata **metadata, |
| 392 | bool *verity_enabled) |
Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 393 | { |
| 394 | struct block_device *bdev; |
| 395 | struct android_metadata_header *header; |
Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 396 | int i; |
| 397 | u32 table_length, copy_length, offset; |
| 398 | u64 metadata_offset; |
| 399 | struct bio_read payload; |
| 400 | int err = 0; |
| 401 | |
| 402 | bdev = blkdev_get_by_dev(dev, FMODE_READ, NULL); |
| 403 | |
Badhri Jagan Sridharan | c68a615 | 2016-06-17 18:54:35 -0700 | [diff] [blame] | 404 | if (IS_ERR_OR_NULL(bdev)) { |
Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 405 | DMERR("blkdev_get_by_dev failed"); |
Badhri Jagan Sridharan | 7635064 | 2016-07-06 17:16:19 -0700 | [diff] [blame] | 406 | return -ENODEV; |
Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | find_metadata_offset(fec, bdev, &metadata_offset); |
| 410 | |
| 411 | /* Verity metadata size is a power of 2 and PAGE_SIZE |
| 412 | * is a power of 2 as well. |
| 413 | * PAGE_SIZE is also a multiple of 512 bytes. |
| 414 | */ |
| 415 | if (VERITY_METADATA_SIZE > PAGE_SIZE) |
| 416 | BUG_ON(VERITY_METADATA_SIZE % PAGE_SIZE != 0); |
| 417 | /* 512 byte sector alignment */ |
| 418 | BUG_ON(metadata_offset % (1 << SECTOR_SHIFT) != 0); |
| 419 | |
| 420 | err = read_block_dev(&payload, bdev, metadata_offset / |
| 421 | (1 << SECTOR_SHIFT), VERITY_METADATA_SIZE); |
| 422 | if (err) { |
| 423 | DMERR("Error while reading verity metadata"); |
Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 424 | goto blkdev_release; |
| 425 | } |
| 426 | |
| 427 | header = kzalloc(sizeof(*header), GFP_KERNEL); |
| 428 | if (!header) { |
| 429 | DMERR("kzalloc failed for header"); |
| 430 | err = -ENOMEM; |
| 431 | goto free_payload; |
| 432 | } |
| 433 | |
| 434 | memcpy(header, page_address(payload.page_io[0]), |
| 435 | sizeof(*header)); |
| 436 | |
| 437 | DMINFO("bio magic_number:%u protocol_version:%d table_length:%u", |
| 438 | le32_to_cpu(header->magic_number), |
| 439 | le32_to_cpu(header->protocol_version), |
| 440 | le32_to_cpu(header->table_length)); |
| 441 | |
Badhri Jagan Sridharan | 7635064 | 2016-07-06 17:16:19 -0700 | [diff] [blame] | 442 | err = verify_header(header); |
| 443 | |
| 444 | if (err == VERITY_STATE_DISABLE) { |
| 445 | DMERR("Mounting root with verity disabled"); |
| 446 | *verity_enabled = false; |
| 447 | /* we would still have to read the metadata to figure out |
| 448 | * the data blocks size. Or may be could map the entire |
| 449 | * partition similar to mounting the device. |
| 450 | * |
| 451 | * Reset error as well as the verity_enabled flag is changed. |
| 452 | */ |
| 453 | err = 0; |
| 454 | } else if (err) |
| 455 | goto free_header; |
| 456 | |
| 457 | *metadata = kzalloc(sizeof(**metadata), GFP_KERNEL); |
| 458 | if (!*metadata) { |
Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 459 | DMERR("kzalloc for metadata failed"); |
| 460 | err = -ENOMEM; |
| 461 | goto free_header; |
| 462 | } |
| 463 | |
Badhri Jagan Sridharan | 7635064 | 2016-07-06 17:16:19 -0700 | [diff] [blame] | 464 | (*metadata)->header = header; |
Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 465 | table_length = le32_to_cpu(header->table_length); |
| 466 | |
| 467 | if (table_length == 0 || |
| 468 | table_length > (VERITY_METADATA_SIZE - |
Badhri Jagan Sridharan | 7635064 | 2016-07-06 17:16:19 -0700 | [diff] [blame] | 469 | sizeof(struct android_metadata_header))) { |
| 470 | DMERR("table_length too long"); |
| 471 | err = -EINVAL; |
Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 472 | goto free_metadata; |
Badhri Jagan Sridharan | 7635064 | 2016-07-06 17:16:19 -0700 | [diff] [blame] | 473 | } |
Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 474 | |
Badhri Jagan Sridharan | 7635064 | 2016-07-06 17:16:19 -0700 | [diff] [blame] | 475 | (*metadata)->verity_table = kzalloc(table_length + 1, GFP_KERNEL); |
Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 476 | |
Badhri Jagan Sridharan | 7635064 | 2016-07-06 17:16:19 -0700 | [diff] [blame] | 477 | if (!(*metadata)->verity_table) { |
Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 478 | DMERR("kzalloc verity_table failed"); |
| 479 | err = -ENOMEM; |
| 480 | goto free_metadata; |
| 481 | } |
| 482 | |
| 483 | if (sizeof(struct android_metadata_header) + |
| 484 | table_length <= PAGE_SIZE) { |
Badhri Jagan Sridharan | 7635064 | 2016-07-06 17:16:19 -0700 | [diff] [blame] | 485 | memcpy((*metadata)->verity_table, |
| 486 | page_address(payload.page_io[0]) |
Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 487 | + sizeof(struct android_metadata_header), |
| 488 | table_length); |
| 489 | } else { |
| 490 | copy_length = PAGE_SIZE - |
| 491 | sizeof(struct android_metadata_header); |
Badhri Jagan Sridharan | 7635064 | 2016-07-06 17:16:19 -0700 | [diff] [blame] | 492 | memcpy((*metadata)->verity_table, |
| 493 | page_address(payload.page_io[0]) |
Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 494 | + sizeof(struct android_metadata_header), |
| 495 | copy_length); |
| 496 | table_length -= copy_length; |
| 497 | offset = copy_length; |
| 498 | i = 1; |
| 499 | while (table_length != 0) { |
| 500 | if (table_length > PAGE_SIZE) { |
Badhri Jagan Sridharan | 7635064 | 2016-07-06 17:16:19 -0700 | [diff] [blame] | 501 | memcpy((*metadata)->verity_table + offset, |
Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 502 | page_address(payload.page_io[i]), |
| 503 | PAGE_SIZE); |
| 504 | offset += PAGE_SIZE; |
| 505 | table_length -= PAGE_SIZE; |
| 506 | } else { |
Badhri Jagan Sridharan | 7635064 | 2016-07-06 17:16:19 -0700 | [diff] [blame] | 507 | memcpy((*metadata)->verity_table + offset, |
Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 508 | page_address(payload.page_io[i]), |
| 509 | table_length); |
| 510 | table_length = 0; |
| 511 | } |
| 512 | i++; |
| 513 | } |
| 514 | } |
Badhri Jagan Sridharan | 7635064 | 2016-07-06 17:16:19 -0700 | [diff] [blame] | 515 | (*metadata)->verity_table[table_length] = '\0'; |
Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 516 | |
Badhri Jagan Sridharan | 7635064 | 2016-07-06 17:16:19 -0700 | [diff] [blame] | 517 | DMINFO("verity_table: %s", (*metadata)->verity_table); |
Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 518 | goto free_payload; |
| 519 | |
| 520 | free_metadata: |
Badhri Jagan Sridharan | 7635064 | 2016-07-06 17:16:19 -0700 | [diff] [blame] | 521 | kfree(*metadata); |
Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 522 | free_header: |
| 523 | kfree(header); |
Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 524 | free_payload: |
| 525 | for (i = 0; i < payload.number_of_pages; i++) |
| 526 | if (payload.page_io[i]) |
| 527 | __free_page(payload.page_io[i]); |
| 528 | kfree(payload.page_io); |
Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 529 | blkdev_release: |
| 530 | blkdev_put(bdev, FMODE_READ); |
Badhri Jagan Sridharan | 7635064 | 2016-07-06 17:16:19 -0700 | [diff] [blame] | 531 | return err; |
Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | /* helper functions to extract properties from dts */ |
| 535 | const char *find_dt_value(const char *name) |
| 536 | { |
| 537 | struct device_node *firmware; |
| 538 | const char *value; |
| 539 | |
| 540 | firmware = of_find_node_by_path("/firmware/android"); |
| 541 | if (!firmware) |
| 542 | return NULL; |
| 543 | value = of_get_property(firmware, name, NULL); |
| 544 | of_node_put(firmware); |
| 545 | |
| 546 | return value; |
| 547 | } |
| 548 | |
Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 549 | static int verity_mode(void) |
| 550 | { |
| 551 | static const char enforcing[] = "enforcing"; |
| 552 | static const char verified_mode_prop[] = "veritymode"; |
| 553 | const char *value; |
| 554 | |
| 555 | value = find_dt_value(verified_mode_prop); |
| 556 | if (!value) |
| 557 | value = veritymode; |
| 558 | if (!strncmp(value, enforcing, sizeof(enforcing) - 1)) |
| 559 | return DM_VERITY_MODE_RESTART; |
| 560 | |
| 561 | return DM_VERITY_MODE_EIO; |
| 562 | } |
| 563 | |
Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 564 | static int verify_verity_signature(char *key_id, |
| 565 | struct android_metadata *metadata) |
| 566 | { |
| 567 | key_ref_t key_ref; |
| 568 | struct key *key; |
| 569 | struct public_key_signature *pks = NULL; |
| 570 | int retval = -EINVAL; |
| 571 | |
| 572 | key_ref = keyring_search(make_key_ref(system_trusted_keyring, 1), |
| 573 | &key_type_asymmetric, key_id); |
| 574 | |
| 575 | if (IS_ERR(key_ref)) { |
| 576 | DMERR("keyring: key not found"); |
| 577 | return -ENOKEY; |
| 578 | } |
| 579 | |
| 580 | key = key_ref_to_ptr(key_ref); |
| 581 | |
Badhri Jagan Sridharan | 56f6a6b | 2016-02-08 16:28:43 -0800 | [diff] [blame] | 582 | pks = table_make_digest(HASH_ALGO_SHA256, |
Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 583 | (const void *)metadata->verity_table, |
| 584 | le32_to_cpu(metadata->header->table_length)); |
| 585 | |
| 586 | if (IS_ERR(pks)) { |
| 587 | DMERR("hashing failed"); |
| 588 | goto error; |
| 589 | } |
| 590 | |
| 591 | retval = table_extract_mpi_array(pks, &metadata->header->signature[0], |
| 592 | RSANUMBYTES); |
| 593 | if (retval < 0) { |
| 594 | DMERR("Error extracting mpi %d", retval); |
| 595 | goto error; |
| 596 | } |
| 597 | |
| 598 | retval = verify_signature(key, pks); |
| 599 | mpi_free(pks->rsa.s); |
| 600 | error: |
| 601 | kfree(pks); |
| 602 | key_put(key); |
| 603 | |
| 604 | return retval; |
| 605 | } |
| 606 | |
| 607 | static void handle_error(void) |
| 608 | { |
| 609 | int mode = verity_mode(); |
| 610 | if (mode == DM_VERITY_MODE_RESTART) { |
| 611 | DMERR("triggering restart"); |
| 612 | kernel_restart("dm-verity device corrupted"); |
| 613 | } else { |
| 614 | DMERR("Mounting verity root failed"); |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | static inline bool test_mult_overflow(sector_t a, u32 b) |
| 619 | { |
| 620 | sector_t r = (sector_t)~0ULL; |
| 621 | |
| 622 | sector_div(r, b); |
| 623 | return a > r; |
| 624 | } |
| 625 | |
Badhri Jagan Sridharan | d0706bc | 2016-03-21 10:55:23 -0700 | [diff] [blame] | 626 | static int add_as_linear_device(struct dm_target *ti, char *dev) |
| 627 | { |
| 628 | /*Move to linear mapping defines*/ |
Badhri Jagan Sridharan | 7e70218 | 2016-03-28 14:41:21 -0700 | [diff] [blame] | 629 | char *linear_table_args[DM_LINEAR_ARGS] = {dev, |
| 630 | DM_LINEAR_TARGET_OFFSET}; |
Badhri Jagan Sridharan | d0706bc | 2016-03-21 10:55:23 -0700 | [diff] [blame] | 631 | int err = 0; |
| 632 | |
Badhri Jagan Sridharan | 67584ff | 2016-04-05 11:18:16 -0700 | [diff] [blame] | 633 | android_verity_target.dtr = dm_linear_dtr, |
| 634 | android_verity_target.map = dm_linear_map, |
| 635 | android_verity_target.status = dm_linear_status, |
Badhri Jagan Sridharan | 424861b | 2016-08-09 12:47:37 -0700 | [diff] [blame] | 636 | android_verity_target.prepare_ioctl = dm_linear_prepare_ioctl, |
Badhri Jagan Sridharan | 67584ff | 2016-04-05 11:18:16 -0700 | [diff] [blame] | 637 | android_verity_target.iterate_devices = dm_linear_iterate_devices, |
Badhri Jagan Sridharan | d0706bc | 2016-03-21 10:55:23 -0700 | [diff] [blame] | 638 | android_verity_target.io_hints = NULL; |
| 639 | |
Badhri Jagan Sridharan | 67584ff | 2016-04-05 11:18:16 -0700 | [diff] [blame] | 640 | err = dm_linear_ctr(ti, DM_LINEAR_ARGS, linear_table_args); |
Badhri Jagan Sridharan | d0706bc | 2016-03-21 10:55:23 -0700 | [diff] [blame] | 641 | |
| 642 | if (!err) { |
| 643 | DMINFO("Added android-verity as a linear target"); |
| 644 | target_added = true; |
| 645 | } else |
| 646 | DMERR("Failed to add android-verity as linear target"); |
| 647 | |
| 648 | return err; |
| 649 | } |
| 650 | |
Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 651 | /* |
| 652 | * Target parameters: |
| 653 | * <key id> Key id of the public key in the system keyring. |
| 654 | * Verity metadata's signature would be verified against |
| 655 | * this. If the key id contains spaces, replace them |
| 656 | * with '#'. |
| 657 | * <block device> The block device for which dm-verity is being setup. |
| 658 | */ |
| 659 | static int android_verity_ctr(struct dm_target *ti, unsigned argc, char **argv) |
| 660 | { |
| 661 | dev_t uninitialized_var(dev); |
Badhri Jagan Sridharan | 7635064 | 2016-07-06 17:16:19 -0700 | [diff] [blame] | 662 | struct android_metadata *metadata = NULL; |
Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 663 | int err = 0, i, mode; |
Badhri Jagan Sridharan | adb82b9 | 2016-05-20 16:45:45 -0700 | [diff] [blame] | 664 | char *key_id, *table_ptr, dummy, *target_device, |
Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 665 | *verity_table_args[VERITY_TABLE_ARGS + 2 + VERITY_TABLE_OPT_FEC_ARGS]; |
| 666 | /* One for specifying number of opt args and one for mode */ |
| 667 | sector_t data_sectors; |
| 668 | u32 data_block_size; |
Jeremy Compostella | 0b768a4 | 2016-04-15 13:32:54 +0200 | [diff] [blame] | 669 | unsigned int no_of_args = VERITY_TABLE_ARGS + 2 + VERITY_TABLE_OPT_FEC_ARGS; |
Badhri Jagan Sridharan | 56f6a6b | 2016-02-08 16:28:43 -0800 | [diff] [blame] | 670 | struct fec_header uninitialized_var(fec); |
Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 671 | struct fec_ecc_metadata uninitialized_var(ecc); |
| 672 | char buf[FEC_ARG_LENGTH], *buf_ptr; |
| 673 | unsigned long long tmpll; |
Badhri Jagan Sridharan | 424861b | 2016-08-09 12:47:37 -0700 | [diff] [blame] | 674 | u64 uninitialized_var(device_size); |
Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 675 | |
Badhri Jagan Sridharan | adb82b9 | 2016-05-20 16:45:45 -0700 | [diff] [blame] | 676 | if (argc == 1) { |
| 677 | /* Use the default keyid */ |
| 678 | if (default_verity_key_id()) |
| 679 | key_id = veritykeyid; |
Badhri Jagan Sridharan | c68a615 | 2016-06-17 18:54:35 -0700 | [diff] [blame] | 680 | else if (!is_eng()) { |
Badhri Jagan Sridharan | adb82b9 | 2016-05-20 16:45:45 -0700 | [diff] [blame] | 681 | DMERR("veritykeyid= is not set"); |
| 682 | handle_error(); |
| 683 | return -EINVAL; |
| 684 | } |
| 685 | } else if (argc == 2) |
| 686 | key_id = argv[1]; |
| 687 | else { |
Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 688 | DMERR("Incorrect number of arguments"); |
| 689 | handle_error(); |
| 690 | return -EINVAL; |
| 691 | } |
| 692 | |
Badhri Jagan Sridharan | adb82b9 | 2016-05-20 16:45:45 -0700 | [diff] [blame] | 693 | target_device = argv[0]; |
Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 694 | |
Badhri Jagan Sridharan | adb82b9 | 2016-05-20 16:45:45 -0700 | [diff] [blame] | 695 | dev = name_to_dev_t(target_device); |
Jeremy Compostella | 0b768a4 | 2016-04-15 13:32:54 +0200 | [diff] [blame] | 696 | if (!dev) { |
Badhri Jagan Sridharan | adb82b9 | 2016-05-20 16:45:45 -0700 | [diff] [blame] | 697 | DMERR("no dev found for %s", target_device); |
Jeremy Compostella | 0b768a4 | 2016-04-15 13:32:54 +0200 | [diff] [blame] | 698 | handle_error(); |
| 699 | return -EINVAL; |
Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 700 | } |
| 701 | |
Badhri Jagan Sridharan | c68a615 | 2016-06-17 18:54:35 -0700 | [diff] [blame] | 702 | if (is_eng()) { |
| 703 | err = find_size(dev, &device_size); |
| 704 | if (err) { |
| 705 | DMERR("error finding bdev size"); |
| 706 | handle_error(); |
| 707 | return err; |
| 708 | } |
| 709 | |
| 710 | ti->len = device_size; |
| 711 | err = add_as_linear_device(ti, target_device); |
| 712 | if (err) { |
| 713 | handle_error(); |
| 714 | return err; |
| 715 | } |
| 716 | verity_enabled = false; |
| 717 | return 0; |
| 718 | } |
| 719 | |
| 720 | strreplace(key_id, '#', ' '); |
| 721 | |
Badhri Jagan Sridharan | adb82b9 | 2016-05-20 16:45:45 -0700 | [diff] [blame] | 722 | DMINFO("key:%s dev:%s", key_id, target_device); |
Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 723 | |
| 724 | if (extract_fec_header(dev, &fec, &ecc)) { |
| 725 | DMERR("Error while extracting fec header"); |
| 726 | handle_error(); |
| 727 | return -EINVAL; |
| 728 | } |
| 729 | |
Badhri Jagan Sridharan | 7635064 | 2016-07-06 17:16:19 -0700 | [diff] [blame] | 730 | err = extract_metadata(dev, &fec, &metadata, &verity_enabled); |
Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 731 | |
Badhri Jagan Sridharan | 7635064 | 2016-07-06 17:16:19 -0700 | [diff] [blame] | 732 | if (err) { |
Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 733 | DMERR("Error while extracting metadata"); |
| 734 | handle_error(); |
Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 735 | goto free_metadata; |
| 736 | } |
| 737 | |
Badhri Jagan Sridharan | e96affa | 2016-05-20 16:44:19 -0700 | [diff] [blame] | 738 | if (verity_enabled) { |
Badhri Jagan Sridharan | d0706bc | 2016-03-21 10:55:23 -0700 | [diff] [blame] | 739 | err = verify_verity_signature(key_id, metadata); |
Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 740 | |
Badhri Jagan Sridharan | d0706bc | 2016-03-21 10:55:23 -0700 | [diff] [blame] | 741 | if (err) { |
| 742 | DMERR("Signature verification failed"); |
| 743 | handle_error(); |
| 744 | goto free_metadata; |
| 745 | } else |
| 746 | DMINFO("Signature verification success"); |
| 747 | } |
Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 748 | |
| 749 | table_ptr = metadata->verity_table; |
| 750 | |
| 751 | for (i = 0; i < VERITY_TABLE_ARGS; i++) { |
| 752 | verity_table_args[i] = strsep(&table_ptr, " "); |
| 753 | if (verity_table_args[i] == NULL) |
| 754 | break; |
| 755 | } |
| 756 | |
| 757 | if (i != VERITY_TABLE_ARGS) { |
| 758 | DMERR("Verity table not in the expected format"); |
| 759 | err = -EINVAL; |
| 760 | handle_error(); |
| 761 | goto free_metadata; |
| 762 | } |
| 763 | |
| 764 | if (sscanf(verity_table_args[5], "%llu%c", &tmpll, &dummy) |
| 765 | != 1) { |
| 766 | DMERR("Verity table not in the expected format"); |
| 767 | handle_error(); |
| 768 | err = -EINVAL; |
| 769 | goto free_metadata; |
| 770 | } |
| 771 | |
| 772 | if (tmpll > ULONG_MAX) { |
| 773 | DMERR("<num_data_blocks> too large. Forgot to turn on CONFIG_LBDAF?"); |
| 774 | handle_error(); |
| 775 | err = -EINVAL; |
| 776 | goto free_metadata; |
| 777 | } |
| 778 | |
| 779 | data_sectors = tmpll; |
| 780 | |
| 781 | if (sscanf(verity_table_args[3], "%u%c", &data_block_size, &dummy) |
| 782 | != 1) { |
| 783 | DMERR("Verity table not in the expected format"); |
| 784 | handle_error(); |
| 785 | err = -EINVAL; |
| 786 | goto free_metadata; |
| 787 | } |
| 788 | |
| 789 | if (test_mult_overflow(data_sectors, data_block_size >> |
| 790 | SECTOR_SHIFT)) { |
| 791 | DMERR("data_sectors too large"); |
| 792 | handle_error(); |
| 793 | err = -EOVERFLOW; |
| 794 | goto free_metadata; |
| 795 | } |
| 796 | |
| 797 | data_sectors *= data_block_size >> SECTOR_SHIFT; |
| 798 | DMINFO("Data sectors %llu", (unsigned long long)data_sectors); |
| 799 | |
| 800 | /* update target length */ |
| 801 | ti->len = data_sectors; |
| 802 | |
Badhri Jagan Sridharan | d0706bc | 2016-03-21 10:55:23 -0700 | [diff] [blame] | 803 | /* Setup linear target and free */ |
| 804 | if (!verity_enabled) { |
Badhri Jagan Sridharan | adb82b9 | 2016-05-20 16:45:45 -0700 | [diff] [blame] | 805 | err = add_as_linear_device(ti, target_device); |
Badhri Jagan Sridharan | d0706bc | 2016-03-21 10:55:23 -0700 | [diff] [blame] | 806 | goto free_metadata; |
| 807 | } |
| 808 | |
Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 809 | /*substitute data_dev and hash_dev*/ |
Badhri Jagan Sridharan | adb82b9 | 2016-05-20 16:45:45 -0700 | [diff] [blame] | 810 | verity_table_args[1] = target_device; |
| 811 | verity_table_args[2] = target_device; |
Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 812 | |
| 813 | mode = verity_mode(); |
| 814 | |
| 815 | if (ecc.valid && IS_BUILTIN(CONFIG_DM_VERITY_FEC)) { |
| 816 | if (mode) { |
| 817 | err = snprintf(buf, FEC_ARG_LENGTH, |
Badhri Jagan Sridharan | adb82b9 | 2016-05-20 16:45:45 -0700 | [diff] [blame] | 818 | "%u %s " VERITY_TABLE_OPT_FEC_FORMAT, |
| 819 | 1 + VERITY_TABLE_OPT_FEC_ARGS, |
| 820 | mode == DM_VERITY_MODE_RESTART ? |
| 821 | VERITY_TABLE_OPT_RESTART : |
| 822 | VERITY_TABLE_OPT_LOGGING, |
| 823 | target_device, |
| 824 | ecc.start / FEC_BLOCK_SIZE, ecc.blocks, |
| 825 | ecc.roots); |
Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 826 | } else { |
| 827 | err = snprintf(buf, FEC_ARG_LENGTH, |
Badhri Jagan Sridharan | adb82b9 | 2016-05-20 16:45:45 -0700 | [diff] [blame] | 828 | "%u " VERITY_TABLE_OPT_FEC_FORMAT, |
| 829 | VERITY_TABLE_OPT_FEC_ARGS, target_device, |
| 830 | ecc.start / FEC_BLOCK_SIZE, ecc.blocks, |
| 831 | ecc.roots); |
Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 832 | } |
| 833 | } else if (mode) { |
| 834 | err = snprintf(buf, FEC_ARG_LENGTH, |
| 835 | "2 " VERITY_TABLE_OPT_IGNZERO " %s", |
| 836 | mode == DM_VERITY_MODE_RESTART ? |
| 837 | VERITY_TABLE_OPT_RESTART : VERITY_TABLE_OPT_LOGGING); |
| 838 | } else { |
| 839 | err = snprintf(buf, FEC_ARG_LENGTH, "1 %s", |
| 840 | "ignore_zero_blocks"); |
| 841 | } |
| 842 | |
| 843 | if (err < 0 || err >= FEC_ARG_LENGTH) |
| 844 | goto free_metadata; |
| 845 | |
| 846 | buf_ptr = buf; |
| 847 | |
| 848 | for (i = VERITY_TABLE_ARGS; i < (VERITY_TABLE_ARGS + |
| 849 | VERITY_TABLE_OPT_FEC_ARGS + 2); i++) { |
| 850 | verity_table_args[i] = strsep(&buf_ptr, " "); |
| 851 | if (verity_table_args[i] == NULL) { |
| 852 | no_of_args = i; |
| 853 | break; |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | err = verity_ctr(ti, no_of_args, verity_table_args); |
| 858 | |
Badhri Jagan Sridharan | d0706bc | 2016-03-21 10:55:23 -0700 | [diff] [blame] | 859 | if (err) |
| 860 | DMERR("android-verity failed to mount as verity target"); |
| 861 | else { |
| 862 | target_added = true; |
| 863 | DMINFO("android-verity mounted as verity target"); |
| 864 | } |
| 865 | |
Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 866 | free_metadata: |
Badhri Jagan Sridharan | 7635064 | 2016-07-06 17:16:19 -0700 | [diff] [blame] | 867 | if (metadata) { |
| 868 | kfree(metadata->header); |
| 869 | kfree(metadata->verity_table); |
| 870 | } |
Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 871 | kfree(metadata); |
| 872 | return err; |
| 873 | } |
| 874 | |
Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 875 | static int __init dm_android_verity_init(void) |
| 876 | { |
| 877 | int r; |
Badhri Jagan Sridharan | d0706bc | 2016-03-21 10:55:23 -0700 | [diff] [blame] | 878 | struct dentry *file; |
Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 879 | |
| 880 | r = dm_register_target(&android_verity_target); |
| 881 | if (r < 0) |
| 882 | DMERR("register failed %d", r); |
| 883 | |
Badhri Jagan Sridharan | d0706bc | 2016-03-21 10:55:23 -0700 | [diff] [blame] | 884 | /* Tracks the status of the last added target */ |
| 885 | debug_dir = debugfs_create_dir("android_verity", NULL); |
| 886 | |
| 887 | if (IS_ERR_OR_NULL(debug_dir)) { |
| 888 | DMERR("Cannot create android_verity debugfs directory: %ld", |
| 889 | PTR_ERR(debug_dir)); |
| 890 | goto end; |
| 891 | } |
| 892 | |
| 893 | file = debugfs_create_bool("target_added", S_IRUGO, debug_dir, |
Badhri Jagan Sridharan | 424861b | 2016-08-09 12:47:37 -0700 | [diff] [blame] | 894 | &target_added); |
Badhri Jagan Sridharan | d0706bc | 2016-03-21 10:55:23 -0700 | [diff] [blame] | 895 | |
| 896 | if (IS_ERR_OR_NULL(file)) { |
| 897 | DMERR("Cannot create android_verity debugfs directory: %ld", |
| 898 | PTR_ERR(debug_dir)); |
| 899 | debugfs_remove_recursive(debug_dir); |
| 900 | goto end; |
| 901 | } |
| 902 | |
| 903 | file = debugfs_create_bool("verity_enabled", S_IRUGO, debug_dir, |
Badhri Jagan Sridharan | 424861b | 2016-08-09 12:47:37 -0700 | [diff] [blame] | 904 | &verity_enabled); |
Badhri Jagan Sridharan | d0706bc | 2016-03-21 10:55:23 -0700 | [diff] [blame] | 905 | |
| 906 | if (IS_ERR_OR_NULL(file)) { |
| 907 | DMERR("Cannot create android_verity debugfs directory: %ld", |
| 908 | PTR_ERR(debug_dir)); |
| 909 | debugfs_remove_recursive(debug_dir); |
| 910 | } |
| 911 | |
| 912 | end: |
Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 913 | return r; |
| 914 | } |
| 915 | |
| 916 | static void __exit dm_android_verity_exit(void) |
| 917 | { |
Badhri Jagan Sridharan | d0706bc | 2016-03-21 10:55:23 -0700 | [diff] [blame] | 918 | if (!IS_ERR_OR_NULL(debug_dir)) |
| 919 | debugfs_remove_recursive(debug_dir); |
| 920 | |
Badhri Jagan Sridharan | 8bb45a5 | 2015-12-14 20:09:39 -0800 | [diff] [blame] | 921 | dm_unregister_target(&android_verity_target); |
| 922 | } |
| 923 | |
| 924 | module_init(dm_android_verity_init); |
| 925 | module_exit(dm_android_verity_exit); |