blob: b75ed4cdf29580702d887468aaa18f117288dc42 [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>
24#include <stdbool.h>
25#include <stdio.h>
26#include <sys/stat.h>
Paul Lawrence5f356482014-10-09 14:22:49 +000027
Paul Lawrence5f356482014-10-09 14:22:49 +000028#include "cutils/properties.h"
Dan Albertdb6fe642015-03-19 15:21:08 -070029
30#include "adb.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 +000044__attribute__((__format__(printf, 2, 3))) __nonnull((2))
45static void write_console(int fd, const char* format, ...)
46{
47 char buffer[256];
48 va_list args;
49 va_start (args, format);
50 vsnprintf (buffer, sizeof(buffer), format, args);
51 va_end (args);
52
53 adb_write(fd, buffer, strnlen(buffer, sizeof(buffer)));
54}
55
56static int get_target_device_size(int fd, const char *blk_device,
57 uint64_t *device_size)
58{
59 int data_device;
60 struct ext4_super_block sb;
61 struct fs_info info;
62
63 info.len = 0; /* Only len is set to 0 to ask the device for real size. */
64
65 data_device = adb_open(blk_device, O_RDONLY | O_CLOEXEC);
66 if (data_device < 0) {
67 write_console(fd, "Error opening block device (%s)\n", strerror(errno));
68 return -1;
69 }
70
71 if (lseek64(data_device, 1024, SEEK_SET) < 0) {
72 write_console(fd, "Error seeking to superblock\n");
73 adb_close(data_device);
74 return -1;
75 }
76
77 if (adb_read(data_device, &sb, sizeof(sb)) != sizeof(sb)) {
78 write_console(fd, "Error reading superblock\n");
79 adb_close(data_device);
80 return -1;
81 }
82
83 ext4_parse_sb(&sb, &info);
84 *device_size = info.len;
85
86 adb_close(data_device);
87 return 0;
88}
89
Paul Lawrence8ba2b022014-12-03 15:31:57 -080090/* Turn verity on/off */
91static int set_verity_enabled_state(int fd, const char *block_device,
92 const char* mount_point, bool enable)
Paul Lawrence5f356482014-10-09 14:22:49 +000093{
94 uint32_t magic_number;
Paul Lawrence8ba2b022014-12-03 15:31:57 -080095 const uint32_t new_magic = enable ? VERITY_METADATA_MAGIC_NUMBER
96 : VERITY_METADATA_MAGIC_DISABLE;
Dan Albert4d8a1ab2015-03-09 18:29:07 -070097 uint64_t device_length = 0;
Sami Tolvanen5638a1a2015-01-02 13:30:50 +000098 int device = -1;
Paul Lawrence5f356482014-10-09 14:22:49 +000099 int retval = -1;
100
Sami Tolvanen5638a1a2015-01-02 13:30:50 +0000101 if (make_block_device_writable(block_device)) {
102 write_console(fd, "Could not make block device %s writable (%s).\n",
103 block_device, strerror(errno));
104 goto errout;
105 }
106
Paul Lawrence5f356482014-10-09 14:22:49 +0000107 device = adb_open(block_device, O_RDWR | O_CLOEXEC);
108 if (device == -1) {
109 write_console(fd, "Could not open block device %s (%s).\n",
110 block_device, strerror(errno));
111 write_console(fd, "Maybe run adb remount?\n");
112 goto errout;
113 }
114
115 // find the start of the verity metadata
116 if (get_target_device_size(fd, (char*)block_device, &device_length) < 0) {
117 write_console(fd, "Could not get target device size.\n");
118 goto errout;
119 }
120
121 if (lseek64(device, device_length, SEEK_SET) < 0) {
122 write_console(fd,
123 "Could not seek to start of verity metadata block.\n");
124 goto errout;
125 }
126
127 // check the magic number
128 if (adb_read(device, &magic_number, sizeof(magic_number))
129 != sizeof(magic_number)) {
130 write_console(fd, "Couldn't read magic number!\n");
131 goto errout;
132 }
133
Paul Lawrence8ba2b022014-12-03 15:31:57 -0800134 if (!enable && magic_number == VERITY_METADATA_MAGIC_DISABLE) {
Paul Lawrence5f356482014-10-09 14:22:49 +0000135 write_console(fd, "Verity already disabled on %s\n", mount_point);
136 goto errout;
137 }
138
Paul Lawrence8ba2b022014-12-03 15:31:57 -0800139 if (enable && magic_number == VERITY_METADATA_MAGIC_NUMBER) {
140 write_console(fd, "Verity already enabled on %s\n", mount_point);
141 goto errout;
142 }
143
144 if (magic_number != VERITY_METADATA_MAGIC_NUMBER
145 && magic_number != VERITY_METADATA_MAGIC_DISABLE) {
Paul Lawrence5f356482014-10-09 14:22:49 +0000146 write_console(fd,
Dan Albertf30d73c2015-02-25 17:51:28 -0800147 "Couldn't find verity metadata at offset %" PRIu64 "!\n",
Paul Lawrence5f356482014-10-09 14:22:49 +0000148 device_length);
149 goto errout;
150 }
151
152 if (lseek64(device, device_length, SEEK_SET) < 0) {
153 write_console(fd,
154 "Could not seek to start of verity metadata block.\n");
155 goto errout;
156 }
157
Paul Lawrence8ba2b022014-12-03 15:31:57 -0800158 if (adb_write(device, &new_magic, sizeof(new_magic)) != sizeof(new_magic)) {
Dan Albert59bcbe22015-01-26 17:49:17 -0800159 write_console(
160 fd, "Could not set verity %s flag on device %s with error %s\n",
161 enable ? "enabled" : "disabled",
162 block_device, strerror(errno));
Paul Lawrence5f356482014-10-09 14:22:49 +0000163 goto errout;
164 }
165
Paul Lawrence8ba2b022014-12-03 15:31:57 -0800166 write_console(fd, "Verity %s on %s\n",
167 enable ? "enabled" : "disabled",
168 mount_point);
Paul Lawrence5f356482014-10-09 14:22:49 +0000169 retval = 0;
170errout:
171 if (device != -1)
172 adb_close(device);
173 return retval;
174}
175
Paul Lawrence8ba2b022014-12-03 15:31:57 -0800176void set_verity_enabled_state_service(int fd, void* cookie)
Paul Lawrence5f356482014-10-09 14:22:49 +0000177{
Paul Lawrence8ba2b022014-12-03 15:31:57 -0800178 bool enable = (cookie != NULL);
Dan Albert59bcbe22015-01-26 17:49:17 -0800179 if (kAllowDisableVerity) {
180 char fstab_filename[PROPERTY_VALUE_MAX + sizeof(FSTAB_PREFIX)];
181 char propbuf[PROPERTY_VALUE_MAX];
182 int i;
183 bool any_changed = false;
Paul Lawrence5f356482014-10-09 14:22:49 +0000184
Dan Albert59bcbe22015-01-26 17:49:17 -0800185 property_get("ro.secure", propbuf, "0");
186 if (strcmp(propbuf, "1")) {
187 write_console(fd, "verity not enabled - ENG build\n");
188 goto errout;
189 }
190
191 property_get("ro.debuggable", propbuf, "0");
192 if (strcmp(propbuf, "1")) {
193 write_console(
194 fd, "verity cannot be disabled/enabled - USER build\n");
195 goto errout;
196 }
197
198 property_get("ro.hardware", propbuf, "");
199 snprintf(fstab_filename, sizeof(fstab_filename), FSTAB_PREFIX"%s",
200 propbuf);
201
202 fstab = fs_mgr_read_fstab(fstab_filename);
203 if (!fstab) {
204 write_console(fd, "Failed to open %s\nMaybe run adb root?\n",
205 fstab_filename);
206 goto errout;
207 }
208
209 /* Loop through entries looking for ones that vold manages */
210 for (i = 0; i < fstab->num_entries; i++) {
211 if(fs_mgr_is_verified(&fstab->recs[i])) {
212 if (!set_verity_enabled_state(fd, fstab->recs[i].blk_device,
213 fstab->recs[i].mount_point,
214 enable)) {
215 any_changed = true;
216 }
217 }
218 }
219
220 if (any_changed) {
221 write_console(
222 fd, "Now reboot your device for settings to take effect\n");
223 }
224 } else {
225 write_console(fd, "%s-verity only works for userdebug builds\n",
226 enable ? "enable" : "disable");
Paul Lawrence5f356482014-10-09 14:22:49 +0000227 }
228
Bernhard Rosenkränzerf6b32542014-12-12 22:22:37 +0100229errout:
Paul Lawrence5f356482014-10-09 14:22:49 +0000230 adb_close(fd);
231}