blob: 139b0741009546ded1687f746b44f87d33e4a06a [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 Albert59bcbe22015-01-26 17:49:17 -080017#include <fcntl.h>
18#include <inttypes.h>
19#include <stdarg.h>
20#include <stdbool.h>
21#include <stdio.h>
22#include <sys/stat.h>
Paul Lawrence5f356482014-10-09 14:22:49 +000023
24#define TRACE_TAG TRACE_ADB
25#include "adb.h"
Paul Lawrence5f356482014-10-09 14:22:49 +000026#include "cutils/properties.h"
Dan Albert0ca996b2015-01-26 17:13:54 -080027#include "ext4_sb.h"
Dan Albert59bcbe22015-01-26 17:49:17 -080028#include "fs_mgr.h"
Elliott Hughesdedf7672015-03-16 21:58:32 +000029#include "remount_service.h"
Dan Albert59bcbe22015-01-26 17:49:17 -080030#include "sysdeps.h"
Paul Lawrence5f356482014-10-09 14:22:49 +000031
32#define FSTAB_PREFIX "/fstab."
33struct fstab *fstab;
34
Dan Albert59bcbe22015-01-26 17:49:17 -080035#ifdef ALLOW_ADBD_DISABLE_VERITY
36static const bool kAllowDisableVerity = true;
37#else
38static const bool kAllowDisableVerity = false;
39#endif
40
Paul Lawrence5f356482014-10-09 14:22:49 +000041__attribute__((__format__(printf, 2, 3))) __nonnull((2))
42static void write_console(int fd, const char* format, ...)
43{
44 char buffer[256];
45 va_list args;
46 va_start (args, format);
47 vsnprintf (buffer, sizeof(buffer), format, args);
48 va_end (args);
49
50 adb_write(fd, buffer, strnlen(buffer, sizeof(buffer)));
51}
52
53static int get_target_device_size(int fd, const char *blk_device,
54 uint64_t *device_size)
55{
56 int data_device;
57 struct ext4_super_block sb;
58 struct fs_info info;
59
60 info.len = 0; /* Only len is set to 0 to ask the device for real size. */
61
62 data_device = adb_open(blk_device, O_RDONLY | O_CLOEXEC);
63 if (data_device < 0) {
64 write_console(fd, "Error opening block device (%s)\n", strerror(errno));
65 return -1;
66 }
67
68 if (lseek64(data_device, 1024, SEEK_SET) < 0) {
69 write_console(fd, "Error seeking to superblock\n");
70 adb_close(data_device);
71 return -1;
72 }
73
74 if (adb_read(data_device, &sb, sizeof(sb)) != sizeof(sb)) {
75 write_console(fd, "Error reading superblock\n");
76 adb_close(data_device);
77 return -1;
78 }
79
80 ext4_parse_sb(&sb, &info);
81 *device_size = info.len;
82
83 adb_close(data_device);
84 return 0;
85}
86
Paul Lawrence8ba2b022014-12-03 15:31:57 -080087/* Turn verity on/off */
88static int set_verity_enabled_state(int fd, const char *block_device,
89 const char* mount_point, bool enable)
Paul Lawrence5f356482014-10-09 14:22:49 +000090{
91 uint32_t magic_number;
Paul Lawrence8ba2b022014-12-03 15:31:57 -080092 const uint32_t new_magic = enable ? VERITY_METADATA_MAGIC_NUMBER
93 : VERITY_METADATA_MAGIC_DISABLE;
Dan Albert4d8a1ab2015-03-09 18:29:07 -070094 uint64_t device_length = 0;
Sami Tolvanen5638a1a2015-01-02 13:30:50 +000095 int device = -1;
Paul Lawrence5f356482014-10-09 14:22:49 +000096 int retval = -1;
97
Sami Tolvanen5638a1a2015-01-02 13:30:50 +000098 if (make_block_device_writable(block_device)) {
99 write_console(fd, "Could not make block device %s writable (%s).\n",
100 block_device, strerror(errno));
101 goto errout;
102 }
103
Paul Lawrence5f356482014-10-09 14:22:49 +0000104 device = adb_open(block_device, O_RDWR | O_CLOEXEC);
105 if (device == -1) {
106 write_console(fd, "Could not open block device %s (%s).\n",
107 block_device, strerror(errno));
108 write_console(fd, "Maybe run adb remount?\n");
109 goto errout;
110 }
111
112 // find the start of the verity metadata
113 if (get_target_device_size(fd, (char*)block_device, &device_length) < 0) {
114 write_console(fd, "Could not get target device size.\n");
115 goto errout;
116 }
117
118 if (lseek64(device, device_length, SEEK_SET) < 0) {
119 write_console(fd,
120 "Could not seek to start of verity metadata block.\n");
121 goto errout;
122 }
123
124 // check the magic number
125 if (adb_read(device, &magic_number, sizeof(magic_number))
126 != sizeof(magic_number)) {
127 write_console(fd, "Couldn't read magic number!\n");
128 goto errout;
129 }
130
Paul Lawrence8ba2b022014-12-03 15:31:57 -0800131 if (!enable && magic_number == VERITY_METADATA_MAGIC_DISABLE) {
Paul Lawrence5f356482014-10-09 14:22:49 +0000132 write_console(fd, "Verity already disabled on %s\n", mount_point);
133 goto errout;
134 }
135
Paul Lawrence8ba2b022014-12-03 15:31:57 -0800136 if (enable && magic_number == VERITY_METADATA_MAGIC_NUMBER) {
137 write_console(fd, "Verity already enabled on %s\n", mount_point);
138 goto errout;
139 }
140
141 if (magic_number != VERITY_METADATA_MAGIC_NUMBER
142 && magic_number != VERITY_METADATA_MAGIC_DISABLE) {
Paul Lawrence5f356482014-10-09 14:22:49 +0000143 write_console(fd,
Dan Albertf30d73c2015-02-25 17:51:28 -0800144 "Couldn't find verity metadata at offset %" PRIu64 "!\n",
Paul Lawrence5f356482014-10-09 14:22:49 +0000145 device_length);
146 goto errout;
147 }
148
149 if (lseek64(device, device_length, SEEK_SET) < 0) {
150 write_console(fd,
151 "Could not seek to start of verity metadata block.\n");
152 goto errout;
153 }
154
Paul Lawrence8ba2b022014-12-03 15:31:57 -0800155 if (adb_write(device, &new_magic, sizeof(new_magic)) != sizeof(new_magic)) {
Dan Albert59bcbe22015-01-26 17:49:17 -0800156 write_console(
157 fd, "Could not set verity %s flag on device %s with error %s\n",
158 enable ? "enabled" : "disabled",
159 block_device, strerror(errno));
Paul Lawrence5f356482014-10-09 14:22:49 +0000160 goto errout;
161 }
162
Paul Lawrence8ba2b022014-12-03 15:31:57 -0800163 write_console(fd, "Verity %s on %s\n",
164 enable ? "enabled" : "disabled",
165 mount_point);
Paul Lawrence5f356482014-10-09 14:22:49 +0000166 retval = 0;
167errout:
168 if (device != -1)
169 adb_close(device);
170 return retval;
171}
172
Paul Lawrence8ba2b022014-12-03 15:31:57 -0800173void set_verity_enabled_state_service(int fd, void* cookie)
Paul Lawrence5f356482014-10-09 14:22:49 +0000174{
Paul Lawrence8ba2b022014-12-03 15:31:57 -0800175 bool enable = (cookie != NULL);
Dan Albert59bcbe22015-01-26 17:49:17 -0800176 if (kAllowDisableVerity) {
177 char fstab_filename[PROPERTY_VALUE_MAX + sizeof(FSTAB_PREFIX)];
178 char propbuf[PROPERTY_VALUE_MAX];
179 int i;
180 bool any_changed = false;
Paul Lawrence5f356482014-10-09 14:22:49 +0000181
Dan Albert59bcbe22015-01-26 17:49:17 -0800182 property_get("ro.secure", propbuf, "0");
183 if (strcmp(propbuf, "1")) {
184 write_console(fd, "verity not enabled - ENG build\n");
185 goto errout;
186 }
187
188 property_get("ro.debuggable", propbuf, "0");
189 if (strcmp(propbuf, "1")) {
190 write_console(
191 fd, "verity cannot be disabled/enabled - USER build\n");
192 goto errout;
193 }
194
195 property_get("ro.hardware", propbuf, "");
196 snprintf(fstab_filename, sizeof(fstab_filename), FSTAB_PREFIX"%s",
197 propbuf);
198
199 fstab = fs_mgr_read_fstab(fstab_filename);
200 if (!fstab) {
201 write_console(fd, "Failed to open %s\nMaybe run adb root?\n",
202 fstab_filename);
203 goto errout;
204 }
205
206 /* Loop through entries looking for ones that vold manages */
207 for (i = 0; i < fstab->num_entries; i++) {
208 if(fs_mgr_is_verified(&fstab->recs[i])) {
209 if (!set_verity_enabled_state(fd, fstab->recs[i].blk_device,
210 fstab->recs[i].mount_point,
211 enable)) {
212 any_changed = true;
213 }
214 }
215 }
216
217 if (any_changed) {
218 write_console(
219 fd, "Now reboot your device for settings to take effect\n");
220 }
221 } else {
222 write_console(fd, "%s-verity only works for userdebug builds\n",
223 enable ? "enable" : "disable");
Paul Lawrence5f356482014-10-09 14:22:49 +0000224 }
225
Bernhard Rosenkränzerf6b32542014-12-12 22:22:37 +0100226errout:
Paul Lawrence5f356482014-10-09 14:22:49 +0000227 adb_close(fd);
228}