blob: bae38cf3353ba1e539f9346468f1b5928c1feb95 [file] [log] [blame]
Paul Lawrence5f356482014-10-09 14:22:49 +00001/*
2 * Copyright (C) 2014 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
Dan Albertdb6fe642015-03-19 15:21:08 -070017#define TRACE_TAG TRACE_ADB
18
19#include "sysdeps.h"
20
Dan Albert59bcbe22015-01-26 17:49:17 -080021#include <fcntl.h>
22#include <inttypes.h>
23#include <stdarg.h>
Dan Albert59bcbe22015-01-26 17:49:17 -080024#include <stdio.h>
25#include <sys/stat.h>
Paul Lawrence5f356482014-10-09 14:22:49 +000026
Paul Lawrence5f356482014-10-09 14:22:49 +000027#include "cutils/properties.h"
Dan Albertdb6fe642015-03-19 15:21:08 -070028
29#include "adb.h"
Elliott Hughesfb596842015-05-01 17:04:38 -070030#include "adb_io.h"
Dan Albert0ca996b2015-01-26 17:13:54 -080031#include "ext4_sb.h"
Dan Albert59bcbe22015-01-26 17:49:17 -080032#include "fs_mgr.h"
Elliott Hughesdedf7672015-03-16 21:58:32 +000033#include "remount_service.h"
Paul Lawrence5f356482014-10-09 14:22:49 +000034
35#define FSTAB_PREFIX "/fstab."
36struct fstab *fstab;
37
Dan Albert59bcbe22015-01-26 17:49:17 -080038#ifdef ALLOW_ADBD_DISABLE_VERITY
39static const bool kAllowDisableVerity = true;
40#else
41static const bool kAllowDisableVerity = false;
42#endif
43
Paul Lawrence5f356482014-10-09 14:22:49 +000044static int get_target_device_size(int fd, const char *blk_device,
45 uint64_t *device_size)
46{
47 int data_device;
48 struct ext4_super_block sb;
49 struct fs_info info;
50
51 info.len = 0; /* Only len is set to 0 to ask the device for real size. */
52
53 data_device = adb_open(blk_device, O_RDONLY | O_CLOEXEC);
54 if (data_device < 0) {
Elliott Hughesfb596842015-05-01 17:04:38 -070055 WriteFdFmt(fd, "Error opening block device (%s)\n", strerror(errno));
Paul Lawrence5f356482014-10-09 14:22:49 +000056 return -1;
57 }
58
59 if (lseek64(data_device, 1024, SEEK_SET) < 0) {
Elliott Hughesfb596842015-05-01 17:04:38 -070060 WriteFdFmt(fd, "Error seeking to superblock\n");
Paul Lawrence5f356482014-10-09 14:22:49 +000061 adb_close(data_device);
62 return -1;
63 }
64
65 if (adb_read(data_device, &sb, sizeof(sb)) != sizeof(sb)) {
Elliott Hughesfb596842015-05-01 17:04:38 -070066 WriteFdFmt(fd, "Error reading superblock\n");
Paul Lawrence5f356482014-10-09 14:22:49 +000067 adb_close(data_device);
68 return -1;
69 }
70
71 ext4_parse_sb(&sb, &info);
72 *device_size = info.len;
73
74 adb_close(data_device);
75 return 0;
76}
77
Paul Lawrence8ba2b022014-12-03 15:31:57 -080078/* Turn verity on/off */
79static int set_verity_enabled_state(int fd, const char *block_device,
80 const char* mount_point, bool enable)
Paul Lawrence5f356482014-10-09 14:22:49 +000081{
82 uint32_t magic_number;
Paul Lawrence8ba2b022014-12-03 15:31:57 -080083 const uint32_t new_magic = enable ? VERITY_METADATA_MAGIC_NUMBER
84 : VERITY_METADATA_MAGIC_DISABLE;
Dan Albert4d8a1ab2015-03-09 18:29:07 -070085 uint64_t device_length = 0;
Sami Tolvanen5638a1a2015-01-02 13:30:50 +000086 int device = -1;
Paul Lawrence5f356482014-10-09 14:22:49 +000087 int retval = -1;
88
Elliott Hughes689011f2015-05-11 11:55:25 -070089 if (!make_block_device_writable(block_device)) {
Elliott Hughesfb596842015-05-01 17:04:38 -070090 WriteFdFmt(fd, "Could not make block device %s writable (%s).\n",
91 block_device, strerror(errno));
Sami Tolvanen5638a1a2015-01-02 13:30:50 +000092 goto errout;
93 }
94
Paul Lawrence5f356482014-10-09 14:22:49 +000095 device = adb_open(block_device, O_RDWR | O_CLOEXEC);
96 if (device == -1) {
Elliott Hughesfb596842015-05-01 17:04:38 -070097 WriteFdFmt(fd, "Could not open block device %s (%s).\n", block_device, strerror(errno));
98 WriteFdFmt(fd, "Maybe run adb remount?\n");
Paul Lawrence5f356482014-10-09 14:22:49 +000099 goto errout;
100 }
101
102 // find the start of the verity metadata
103 if (get_target_device_size(fd, (char*)block_device, &device_length) < 0) {
Elliott Hughesfb596842015-05-01 17:04:38 -0700104 WriteFdFmt(fd, "Could not get target device size.\n");
Paul Lawrence5f356482014-10-09 14:22:49 +0000105 goto errout;
106 }
107
108 if (lseek64(device, device_length, SEEK_SET) < 0) {
Elliott Hughesfb596842015-05-01 17:04:38 -0700109 WriteFdFmt(fd, "Could not seek to start of verity metadata block.\n");
Paul Lawrence5f356482014-10-09 14:22:49 +0000110 goto errout;
111 }
112
113 // check the magic number
Elliott Hughesfb596842015-05-01 17:04:38 -0700114 if (adb_read(device, &magic_number, sizeof(magic_number)) != sizeof(magic_number)) {
115 WriteFdFmt(fd, "Couldn't read magic number!\n");
Paul Lawrence5f356482014-10-09 14:22:49 +0000116 goto errout;
117 }
118
Paul Lawrence8ba2b022014-12-03 15:31:57 -0800119 if (!enable && magic_number == VERITY_METADATA_MAGIC_DISABLE) {
Elliott Hughesfb596842015-05-01 17:04:38 -0700120 WriteFdFmt(fd, "Verity already disabled on %s\n", mount_point);
Paul Lawrence5f356482014-10-09 14:22:49 +0000121 goto errout;
122 }
123
Paul Lawrence8ba2b022014-12-03 15:31:57 -0800124 if (enable && magic_number == VERITY_METADATA_MAGIC_NUMBER) {
Elliott Hughesfb596842015-05-01 17:04:38 -0700125 WriteFdFmt(fd, "Verity already enabled on %s\n", mount_point);
Paul Lawrence8ba2b022014-12-03 15:31:57 -0800126 goto errout;
127 }
128
129 if (magic_number != VERITY_METADATA_MAGIC_NUMBER
130 && magic_number != VERITY_METADATA_MAGIC_DISABLE) {
Elliott Hughesfb596842015-05-01 17:04:38 -0700131 WriteFdFmt(fd, "Couldn't find verity metadata at offset %" PRIu64 "!\n", device_length);
Paul Lawrence5f356482014-10-09 14:22:49 +0000132 goto errout;
133 }
134
135 if (lseek64(device, device_length, SEEK_SET) < 0) {
Elliott Hughesfb596842015-05-01 17:04:38 -0700136 WriteFdFmt(fd, "Could not seek to start of verity metadata block.\n");
Paul Lawrence5f356482014-10-09 14:22:49 +0000137 goto errout;
138 }
139
Paul Lawrence8ba2b022014-12-03 15:31:57 -0800140 if (adb_write(device, &new_magic, sizeof(new_magic)) != sizeof(new_magic)) {
Elliott Hughesfb596842015-05-01 17:04:38 -0700141 WriteFdFmt(fd, "Could not set verity %s flag on device %s with error %s\n",
142 enable ? "enabled" : "disabled",
143 block_device, strerror(errno));
Paul Lawrence5f356482014-10-09 14:22:49 +0000144 goto errout;
145 }
146
Elliott Hughesfb596842015-05-01 17:04:38 -0700147 WriteFdFmt(fd, "Verity %s on %s\n", enable ? "enabled" : "disabled", mount_point);
Paul Lawrence5f356482014-10-09 14:22:49 +0000148 retval = 0;
149errout:
150 if (device != -1)
151 adb_close(device);
152 return retval;
153}
154
Paul Lawrence8ba2b022014-12-03 15:31:57 -0800155void set_verity_enabled_state_service(int fd, void* cookie)
Paul Lawrence5f356482014-10-09 14:22:49 +0000156{
Paul Lawrence8ba2b022014-12-03 15:31:57 -0800157 bool enable = (cookie != NULL);
Dan Albert59bcbe22015-01-26 17:49:17 -0800158 if (kAllowDisableVerity) {
159 char fstab_filename[PROPERTY_VALUE_MAX + sizeof(FSTAB_PREFIX)];
160 char propbuf[PROPERTY_VALUE_MAX];
161 int i;
162 bool any_changed = false;
Paul Lawrence5f356482014-10-09 14:22:49 +0000163
Dan Albert59bcbe22015-01-26 17:49:17 -0800164 property_get("ro.secure", propbuf, "0");
165 if (strcmp(propbuf, "1")) {
Elliott Hughesfb596842015-05-01 17:04:38 -0700166 WriteFdFmt(fd, "verity not enabled - ENG build\n");
Dan Albert59bcbe22015-01-26 17:49:17 -0800167 goto errout;
168 }
169
170 property_get("ro.debuggable", propbuf, "0");
171 if (strcmp(propbuf, "1")) {
Elliott Hughesfb596842015-05-01 17:04:38 -0700172 WriteFdFmt(fd, "verity cannot be disabled/enabled - USER build\n");
Dan Albert59bcbe22015-01-26 17:49:17 -0800173 goto errout;
174 }
175
176 property_get("ro.hardware", propbuf, "");
177 snprintf(fstab_filename, sizeof(fstab_filename), FSTAB_PREFIX"%s",
178 propbuf);
179
180 fstab = fs_mgr_read_fstab(fstab_filename);
181 if (!fstab) {
Elliott Hughesfb596842015-05-01 17:04:38 -0700182 WriteFdFmt(fd, "Failed to open %s\nMaybe run adb root?\n", fstab_filename);
Dan Albert59bcbe22015-01-26 17:49:17 -0800183 goto errout;
184 }
185
186 /* Loop through entries looking for ones that vold manages */
187 for (i = 0; i < fstab->num_entries; i++) {
188 if(fs_mgr_is_verified(&fstab->recs[i])) {
189 if (!set_verity_enabled_state(fd, fstab->recs[i].blk_device,
190 fstab->recs[i].mount_point,
191 enable)) {
192 any_changed = true;
193 }
194 }
195 }
196
197 if (any_changed) {
Elliott Hughesfb596842015-05-01 17:04:38 -0700198 WriteFdFmt(fd, "Now reboot your device for settings to take effect\n");
Dan Albert59bcbe22015-01-26 17:49:17 -0800199 }
200 } else {
Elliott Hughesfb596842015-05-01 17:04:38 -0700201 WriteFdFmt(fd, "%s-verity only works for userdebug builds\n",
202 enable ? "enable" : "disable");
Paul Lawrence5f356482014-10-09 14:22:49 +0000203 }
204
Bernhard Rosenkränzerf6b32542014-12-12 22:22:37 +0100205errout:
Paul Lawrence5f356482014-10-09 14:22:49 +0000206 adb_close(fd);
207}