blob: 09e2eb907701b7d6ee3be006d1cf56a913b7ce36 [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
17#include "sysdeps.h"
18
19#define TRACE_TAG TRACE_ADB
20#include "adb.h"
21
22#include <stdio.h>
23#include <stdarg.h>
24#include <sys/stat.h>
25#include <fcntl.h>
26#include <inttypes.h>
27
28#include "cutils/properties.h"
29#include "ext4_sb.h"
30#include <fs_mgr.h>
31
32#define FSTAB_PREFIX "/fstab."
33struct fstab *fstab;
34
35__attribute__((__format__(printf, 2, 3))) __nonnull((2))
36static void write_console(int fd, const char* format, ...)
37{
38 char buffer[256];
39 va_list args;
40 va_start (args, format);
41 vsnprintf (buffer, sizeof(buffer), format, args);
42 va_end (args);
43
44 adb_write(fd, buffer, strnlen(buffer, sizeof(buffer)));
45}
46
Bernhard Rosenkränzerf6b32542014-12-12 22:22:37 +010047#ifdef ALLOW_ADBD_DISABLE_VERITY
Paul Lawrence5f356482014-10-09 14:22:49 +000048static int get_target_device_size(int fd, const char *blk_device,
49 uint64_t *device_size)
50{
51 int data_device;
52 struct ext4_super_block sb;
53 struct fs_info info;
54
55 info.len = 0; /* Only len is set to 0 to ask the device for real size. */
56
57 data_device = adb_open(blk_device, O_RDONLY | O_CLOEXEC);
58 if (data_device < 0) {
59 write_console(fd, "Error opening block device (%s)\n", strerror(errno));
60 return -1;
61 }
62
63 if (lseek64(data_device, 1024, SEEK_SET) < 0) {
64 write_console(fd, "Error seeking to superblock\n");
65 adb_close(data_device);
66 return -1;
67 }
68
69 if (adb_read(data_device, &sb, sizeof(sb)) != sizeof(sb)) {
70 write_console(fd, "Error reading superblock\n");
71 adb_close(data_device);
72 return -1;
73 }
74
75 ext4_parse_sb(&sb, &info);
76 *device_size = info.len;
77
78 adb_close(data_device);
79 return 0;
80}
81
Paul Lawrence8ba2b022014-12-03 15:31:57 -080082/* Turn verity on/off */
83static int set_verity_enabled_state(int fd, const char *block_device,
84 const char* mount_point, bool enable)
Paul Lawrence5f356482014-10-09 14:22:49 +000085{
86 uint32_t magic_number;
Paul Lawrence8ba2b022014-12-03 15:31:57 -080087 const uint32_t new_magic = enable ? VERITY_METADATA_MAGIC_NUMBER
88 : VERITY_METADATA_MAGIC_DISABLE;
Paul Lawrence5f356482014-10-09 14:22:49 +000089 uint64_t device_length;
90 int device;
91 int retval = -1;
92
93 device = adb_open(block_device, O_RDWR | O_CLOEXEC);
94 if (device == -1) {
95 write_console(fd, "Could not open block device %s (%s).\n",
96 block_device, strerror(errno));
97 write_console(fd, "Maybe run adb remount?\n");
98 goto errout;
99 }
100
101 // find the start of the verity metadata
102 if (get_target_device_size(fd, (char*)block_device, &device_length) < 0) {
103 write_console(fd, "Could not get target device size.\n");
104 goto errout;
105 }
106
107 if (lseek64(device, device_length, SEEK_SET) < 0) {
108 write_console(fd,
109 "Could not seek to start of verity metadata block.\n");
110 goto errout;
111 }
112
113 // check the magic number
114 if (adb_read(device, &magic_number, sizeof(magic_number))
115 != sizeof(magic_number)) {
116 write_console(fd, "Couldn't read magic number!\n");
117 goto errout;
118 }
119
Paul Lawrence8ba2b022014-12-03 15:31:57 -0800120 if (!enable && magic_number == VERITY_METADATA_MAGIC_DISABLE) {
Paul Lawrence5f356482014-10-09 14:22:49 +0000121 write_console(fd, "Verity already disabled on %s\n", mount_point);
122 goto errout;
123 }
124
Paul Lawrence8ba2b022014-12-03 15:31:57 -0800125 if (enable && magic_number == VERITY_METADATA_MAGIC_NUMBER) {
126 write_console(fd, "Verity already enabled on %s\n", mount_point);
127 goto errout;
128 }
129
130 if (magic_number != VERITY_METADATA_MAGIC_NUMBER
131 && magic_number != VERITY_METADATA_MAGIC_DISABLE) {
Paul Lawrence5f356482014-10-09 14:22:49 +0000132 write_console(fd,
133 "Couldn't find verity metadata at offset %"PRIu64"!\n",
134 device_length);
135 goto errout;
136 }
137
138 if (lseek64(device, device_length, SEEK_SET) < 0) {
139 write_console(fd,
140 "Could not seek to start of verity metadata block.\n");
141 goto errout;
142 }
143
Paul Lawrence8ba2b022014-12-03 15:31:57 -0800144 if (adb_write(device, &new_magic, sizeof(new_magic)) != sizeof(new_magic)) {
145 write_console(fd, "Could not set verity %s flag on device %s with error %s\n",
146 enable ? "enabled" : "disabled",
147 block_device,
148 strerror(errno));
Paul Lawrence5f356482014-10-09 14:22:49 +0000149 goto errout;
150 }
151
Paul Lawrence8ba2b022014-12-03 15:31:57 -0800152 write_console(fd, "Verity %s on %s\n",
153 enable ? "enabled" : "disabled",
154 mount_point);
Paul Lawrence5f356482014-10-09 14:22:49 +0000155 retval = 0;
156errout:
157 if (device != -1)
158 adb_close(device);
159 return retval;
160}
Bernhard Rosenkränzerf6b32542014-12-12 22:22:37 +0100161#endif
Paul Lawrence5f356482014-10-09 14:22:49 +0000162
Paul Lawrence8ba2b022014-12-03 15:31:57 -0800163void set_verity_enabled_state_service(int fd, void* cookie)
Paul Lawrence5f356482014-10-09 14:22:49 +0000164{
Paul Lawrence8ba2b022014-12-03 15:31:57 -0800165 bool enable = (cookie != NULL);
Paul Lawrence5f356482014-10-09 14:22:49 +0000166#ifdef ALLOW_ADBD_DISABLE_VERITY
167 char fstab_filename[PROPERTY_VALUE_MAX + sizeof(FSTAB_PREFIX)];
168 char propbuf[PROPERTY_VALUE_MAX];
169 int i;
Paul Lawrence8ba2b022014-12-03 15:31:57 -0800170 bool any_changed = false;
Paul Lawrence5f356482014-10-09 14:22:49 +0000171
172 property_get("ro.secure", propbuf, "0");
173 if (strcmp(propbuf, "1")) {
174 write_console(fd, "verity not enabled - ENG build\n");
175 goto errout;
176 }
177
178 property_get("ro.debuggable", propbuf, "0");
179 if (strcmp(propbuf, "1")) {
Paul Lawrence8ba2b022014-12-03 15:31:57 -0800180 write_console(fd, "verity cannot be disabled/enabled - USER build\n");
Paul Lawrence5f356482014-10-09 14:22:49 +0000181 goto errout;
182 }
183
184 property_get("ro.hardware", propbuf, "");
185 snprintf(fstab_filename, sizeof(fstab_filename), FSTAB_PREFIX"%s", propbuf);
186
187 fstab = fs_mgr_read_fstab(fstab_filename);
188 if (!fstab) {
189 write_console(fd, "Failed to open %s\nMaybe run adb root?\n",
190 fstab_filename);
191 goto errout;
192 }
193
Paul Lawrence8ba2b022014-12-03 15:31:57 -0800194 if (enable && make_system_and_vendor_block_devices_writable(fd)) {
195 goto errout;
196 }
197
Paul Lawrence5f356482014-10-09 14:22:49 +0000198 /* Loop through entries looking for ones that vold manages */
199 for (i = 0; i < fstab->num_entries; i++) {
200 if(fs_mgr_is_verified(&fstab->recs[i])) {
Paul Lawrence8ba2b022014-12-03 15:31:57 -0800201 if (!set_verity_enabled_state(fd, fstab->recs[i].blk_device,
202 fstab->recs[i].mount_point, enable)) {
203 any_changed = true;
Paul Lawrence5f356482014-10-09 14:22:49 +0000204 }
205 }
206 }
207
Paul Lawrence8ba2b022014-12-03 15:31:57 -0800208 if (any_changed) {
Paul Lawrence5f356482014-10-09 14:22:49 +0000209 write_console(fd,
210 "Now reboot your device for settings to take effect\n");
211 }
Bernhard Rosenkränzerf6b32542014-12-12 22:22:37 +0100212errout:
Paul Lawrence5f356482014-10-09 14:22:49 +0000213#else
Paul Lawrence8ba2b022014-12-03 15:31:57 -0800214 write_console(fd, "%s-verity only works for userdebug builds\n",
Paul Lawrence345a1a92014-12-04 14:54:18 -0800215 enable ? "enable" : "disable");
Paul Lawrence5f356482014-10-09 14:22:49 +0000216#endif
217
Paul Lawrence5f356482014-10-09 14:22:49 +0000218 adb_close(fd);
219}