Home | History | Annotate | Download | only in fs_mgr
      1 /*
      2  * Copyright (C) 2013 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 #include <inttypes.h>
     18 #include <stdio.h>
     19 #include <stdlib.h>
     20 #include <string.h>
     21 #include <unistd.h>
     22 #include <fcntl.h>
     23 #include <ctype.h>
     24 #include <sys/mount.h>
     25 #include <sys/stat.h>
     26 #include <errno.h>
     27 #include <sys/types.h>
     28 #include <sys/wait.h>
     29 #include <libgen.h>
     30 #include <time.h>
     31 
     32 #include <private/android_filesystem_config.h>
     33 #include <cutils/properties.h>
     34 #include <logwrap/logwrap.h>
     35 
     36 #include "mincrypt/rsa.h"
     37 #include "mincrypt/sha.h"
     38 #include "mincrypt/sha256.h"
     39 
     40 #include "ext4_sb.h"
     41 
     42 #include "fs_mgr_priv.h"
     43 #include "fs_mgr_priv_verity.h"
     44 
     45 #define VERITY_METADATA_SIZE 32768
     46 #define VERITY_METADATA_MAGIC_NUMBER 0xb001b001
     47 #define VERITY_TABLE_RSA_KEY "/verity_key"
     48 
     49 extern struct fs_info info;
     50 
     51 static RSAPublicKey *load_key(char *path)
     52 {
     53     FILE *f;
     54     RSAPublicKey *key;
     55 
     56     key = malloc(sizeof(RSAPublicKey));
     57     if (!key) {
     58         ERROR("Can't malloc key\n");
     59         return NULL;
     60     }
     61 
     62     f = fopen(path, "r");
     63     if (!f) {
     64         ERROR("Can't open '%s'\n", path);
     65         free(key);
     66         return NULL;
     67     }
     68 
     69     if (!fread(key, sizeof(*key), 1, f)) {
     70         ERROR("Could not read key!");
     71         fclose(f);
     72         free(key);
     73         return NULL;
     74     }
     75 
     76     if (key->len != RSANUMWORDS) {
     77         ERROR("Invalid key length %d\n", key->len);
     78         fclose(f);
     79         free(key);
     80         return NULL;
     81     }
     82 
     83     fclose(f);
     84     return key;
     85 }
     86 
     87 static int verify_table(char *signature, char *table, int table_length)
     88 {
     89     RSAPublicKey *key;
     90     uint8_t hash_buf[SHA_DIGEST_SIZE];
     91     int retval = -1;
     92 
     93     // Hash the table
     94     SHA_hash((uint8_t*)table, table_length, hash_buf);
     95 
     96     // Now get the public key from the keyfile
     97     key = load_key(VERITY_TABLE_RSA_KEY);
     98     if (!key) {
     99         ERROR("Couldn't load verity keys");
    100         goto out;
    101     }
    102 
    103     // verify the result
    104     if (!RSA_verify(key,
    105                     (uint8_t*) signature,
    106                     RSANUMBYTES,
    107                     (uint8_t*) hash_buf,
    108                     SHA_DIGEST_SIZE)) {
    109         ERROR("Couldn't verify table.");
    110         goto out;
    111     }
    112 
    113     retval = 0;
    114 
    115 out:
    116     free(key);
    117     return retval;
    118 }
    119 
    120 static int get_target_device_size(char *blk_device, uint64_t *device_size)
    121 {
    122     int data_device;
    123     struct ext4_super_block sb;
    124     struct fs_info info = {0};
    125 
    126     data_device = open(blk_device, O_RDONLY);
    127     if (data_device < 0) {
    128         ERROR("Error opening block device (%s)", strerror(errno));
    129         return -1;
    130     }
    131 
    132     if (lseek64(data_device, 1024, SEEK_SET) < 0) {
    133         ERROR("Error seeking to superblock");
    134         close(data_device);
    135         return -1;
    136     }
    137 
    138     if (read(data_device, &sb, sizeof(sb)) != sizeof(sb)) {
    139         ERROR("Error reading superblock");
    140         close(data_device);
    141         return -1;
    142     }
    143 
    144     ext4_parse_sb(&sb, &info);
    145     *device_size = info.len;
    146 
    147     close(data_device);
    148     return 0;
    149 }
    150 
    151 static int read_verity_metadata(char *block_device, char **signature, char **table)
    152 {
    153     unsigned magic_number;
    154     unsigned table_length;
    155     uint64_t device_length;
    156     int protocol_version;
    157     FILE *device;
    158     int retval = -1;
    159 
    160     device = fopen(block_device, "r");
    161     if (!device) {
    162         ERROR("Could not open block device %s (%s).\n", block_device, strerror(errno));
    163         goto out;
    164     }
    165 
    166     // find the start of the verity metadata
    167     if (get_target_device_size(block_device, &device_length) < 0) {
    168         ERROR("Could not get target device size.\n");
    169         goto out;
    170     }
    171     if (fseek(device, device_length, SEEK_SET) < 0) {
    172         ERROR("Could not seek to start of verity metadata block.\n");
    173         goto out;
    174     }
    175 
    176     // check the magic number
    177     if (!fread(&magic_number, sizeof(int), 1, device)) {
    178         ERROR("Couldn't read magic number!\n");
    179         goto out;
    180     }
    181     if (magic_number != VERITY_METADATA_MAGIC_NUMBER) {
    182         ERROR("Couldn't find verity metadata at offset %"PRIu64"!\n", device_length);
    183         goto out;
    184     }
    185 
    186     // check the protocol version
    187     if (!fread(&protocol_version, sizeof(int), 1, device)) {
    188         ERROR("Couldn't read verity metadata protocol version!\n");
    189         goto out;
    190     }
    191     if (protocol_version != 0) {
    192         ERROR("Got unknown verity metadata protocol version %d!\n", protocol_version);
    193         goto out;
    194     }
    195 
    196     // get the signature
    197     *signature = (char*) malloc(RSANUMBYTES * sizeof(char));
    198     if (!*signature) {
    199         ERROR("Couldn't allocate memory for signature!\n");
    200         goto out;
    201     }
    202     if (!fread(*signature, RSANUMBYTES, 1, device)) {
    203         ERROR("Couldn't read signature from verity metadata!\n");
    204         free(*signature);
    205         goto out;
    206     }
    207 
    208     // get the size of the table
    209     if (!fread(&table_length, sizeof(int), 1, device)) {
    210         ERROR("Couldn't get the size of the verity table from metadata!\n");
    211         free(*signature);
    212         goto out;
    213     }
    214 
    215     // get the table + null terminator
    216     table_length += 1;
    217     *table = malloc(table_length);
    218     if(!*table) {
    219         ERROR("Couldn't allocate memory for verity table!\n");
    220         goto out;
    221     }
    222     if (!fgets(*table, table_length, device)) {
    223         ERROR("Couldn't read the verity table from metadata!\n");
    224         free(*table);
    225         free(*signature);
    226         goto out;
    227     }
    228 
    229     retval = 0;
    230 
    231 out:
    232     if (device)
    233         fclose(device);
    234     return retval;
    235 }
    236 
    237 static void verity_ioctl_init(struct dm_ioctl *io, char *name, unsigned flags)
    238 {
    239     memset(io, 0, DM_BUF_SIZE);
    240     io->data_size = DM_BUF_SIZE;
    241     io->data_start = sizeof(struct dm_ioctl);
    242     io->version[0] = 4;
    243     io->version[1] = 0;
    244     io->version[2] = 0;
    245     io->flags = flags | DM_READONLY_FLAG;
    246     if (name) {
    247         strlcpy(io->name, name, sizeof(io->name));
    248     }
    249 }
    250 
    251 static int create_verity_device(struct dm_ioctl *io, char *name, int fd)
    252 {
    253     verity_ioctl_init(io, name, 1);
    254     if (ioctl(fd, DM_DEV_CREATE, io)) {
    255         ERROR("Error creating device mapping (%s)", strerror(errno));
    256         return -1;
    257     }
    258     return 0;
    259 }
    260 
    261 static int get_verity_device_name(struct dm_ioctl *io, char *name, int fd, char **dev_name)
    262 {
    263     verity_ioctl_init(io, name, 0);
    264     if (ioctl(fd, DM_DEV_STATUS, io)) {
    265         ERROR("Error fetching verity device number (%s)", strerror(errno));
    266         return -1;
    267     }
    268     int dev_num = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00);
    269     if (asprintf(dev_name, "/dev/block/dm-%u", dev_num) < 0) {
    270         ERROR("Error getting verity block device name (%s)", strerror(errno));
    271         return -1;
    272     }
    273     return 0;
    274 }
    275 
    276 static int load_verity_table(struct dm_ioctl *io, char *name, char *blockdev, int fd, char *table)
    277 {
    278     char *verity_params;
    279     char *buffer = (char*) io;
    280     uint64_t device_size = 0;
    281 
    282     if (get_target_device_size(blockdev, &device_size) < 0) {
    283         return -1;
    284     }
    285 
    286     verity_ioctl_init(io, name, DM_STATUS_TABLE_FLAG);
    287 
    288     struct dm_target_spec *tgt = (struct dm_target_spec *) &buffer[sizeof(struct dm_ioctl)];
    289 
    290     // set tgt arguments here
    291     io->target_count = 1;
    292     tgt->status=0;
    293     tgt->sector_start=0;
    294     tgt->length=device_size/512;
    295     strcpy(tgt->target_type, "verity");
    296 
    297     // build the verity params here
    298     verity_params = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec);
    299     if (sprintf(verity_params, "%s", table) < 0) {
    300         return -1;
    301     }
    302 
    303     // set next target boundary
    304     verity_params += strlen(verity_params) + 1;
    305     verity_params = (char*) (((unsigned long)verity_params + 7) & ~8);
    306     tgt->next = verity_params - buffer;
    307 
    308     // send the ioctl to load the verity table
    309     if (ioctl(fd, DM_TABLE_LOAD, io)) {
    310         ERROR("Error loading verity table (%s)", strerror(errno));
    311         return -1;
    312     }
    313 
    314     return 0;
    315 }
    316 
    317 static int resume_verity_table(struct dm_ioctl *io, char *name, int fd)
    318 {
    319     verity_ioctl_init(io, name, 0);
    320     if (ioctl(fd, DM_DEV_SUSPEND, io)) {
    321         ERROR("Error activating verity device (%s)", strerror(errno));
    322         return -1;
    323     }
    324     return 0;
    325 }
    326 
    327 static int test_access(char *device) {
    328     int tries = 25;
    329     while (tries--) {
    330         if (!access(device, F_OK) || errno != ENOENT) {
    331             return 0;
    332         }
    333         usleep(40 * 1000);
    334     }
    335     return -1;
    336 }
    337 
    338 static int set_verified_property(char *name) {
    339     int ret;
    340     char *key;
    341     ret = asprintf(&key, "partition.%s.verified", name);
    342     if (ret < 0) {
    343         ERROR("Error formatting verified property");
    344         return ret;
    345     }
    346     ret = PROP_NAME_MAX - strlen(key);
    347     if (ret < 0) {
    348         ERROR("Verified property name is too long");
    349         return -1;
    350     }
    351     ret = property_set(key, "1");
    352     if (ret < 0)
    353         ERROR("Error setting verified property %s: %d", key, ret);
    354     free(key);
    355     return ret;
    356 }
    357 
    358 int fs_mgr_setup_verity(struct fstab_rec *fstab) {
    359 
    360     int retval = -1;
    361 
    362     char *verity_blk_name;
    363     char *verity_table;
    364     char *verity_table_signature;
    365 
    366     char buffer[DM_BUF_SIZE];
    367     struct dm_ioctl *io = (struct dm_ioctl *) buffer;
    368     char *mount_point = basename(fstab->mount_point);
    369 
    370     // set the dm_ioctl flags
    371     io->flags |= 1;
    372     io->target_count = 1;
    373 
    374     // check to ensure that the verity device is ext4
    375     // TODO: support non-ext4 filesystems
    376     if (strcmp(fstab->fs_type, "ext4")) {
    377         ERROR("Cannot verify non-ext4 device (%s)", fstab->fs_type);
    378         return retval;
    379     }
    380 
    381     // get the device mapper fd
    382     int fd;
    383     if ((fd = open("/dev/device-mapper", O_RDWR)) < 0) {
    384         ERROR("Error opening device mapper (%s)", strerror(errno));
    385         return retval;
    386     }
    387 
    388     // create the device
    389     if (create_verity_device(io, mount_point, fd) < 0) {
    390         ERROR("Couldn't create verity device!");
    391         goto out;
    392     }
    393 
    394     // get the name of the device file
    395     if (get_verity_device_name(io, mount_point, fd, &verity_blk_name) < 0) {
    396         ERROR("Couldn't get verity device number!");
    397         goto out;
    398     }
    399 
    400     // read the verity block at the end of the block device
    401     if (read_verity_metadata(fstab->blk_device,
    402                                     &verity_table_signature,
    403                                     &verity_table) < 0) {
    404         goto out;
    405     }
    406 
    407     // verify the signature on the table
    408     if (verify_table(verity_table_signature,
    409                             verity_table,
    410                             strlen(verity_table)) < 0) {
    411         goto out;
    412     }
    413 
    414     // load the verity mapping table
    415     if (load_verity_table(io, mount_point, fstab->blk_device, fd, verity_table) < 0) {
    416         goto out;
    417     }
    418 
    419     // activate the device
    420     if (resume_verity_table(io, mount_point, fd) < 0) {
    421         goto out;
    422     }
    423 
    424     // assign the new verity block device as the block device
    425     free(fstab->blk_device);
    426     fstab->blk_device = verity_blk_name;
    427 
    428     // make sure we've set everything up properly
    429     if (test_access(fstab->blk_device) < 0) {
    430         goto out;
    431     }
    432 
    433     // set the property indicating that the partition is verified
    434     retval = set_verified_property(mount_point);
    435 
    436 out:
    437     close(fd);
    438     return retval;
    439 }
    440