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