blob: 6692ab757f932946ba7b3deacadb6f40c245f7ef [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
47static int get_target_device_size(int fd, const char *blk_device,
48 uint64_t *device_size)
49{
50 int data_device;
51 struct ext4_super_block sb;
52 struct fs_info info;
53
54 info.len = 0; /* Only len is set to 0 to ask the device for real size. */
55
56 data_device = adb_open(blk_device, O_RDONLY | O_CLOEXEC);
57 if (data_device < 0) {
58 write_console(fd, "Error opening block device (%s)\n", strerror(errno));
59 return -1;
60 }
61
62 if (lseek64(data_device, 1024, SEEK_SET) < 0) {
63 write_console(fd, "Error seeking to superblock\n");
64 adb_close(data_device);
65 return -1;
66 }
67
68 if (adb_read(data_device, &sb, sizeof(sb)) != sizeof(sb)) {
69 write_console(fd, "Error reading superblock\n");
70 adb_close(data_device);
71 return -1;
72 }
73
74 ext4_parse_sb(&sb, &info);
75 *device_size = info.len;
76
77 adb_close(data_device);
78 return 0;
79}
80
Paul Lawrence8ba2b022014-12-03 15:31:57 -080081/* Turn verity on/off */
82static int set_verity_enabled_state(int fd, const char *block_device,
83 const char* mount_point, bool enable)
Paul Lawrence5f356482014-10-09 14:22:49 +000084{
85 uint32_t magic_number;
Paul Lawrence8ba2b022014-12-03 15:31:57 -080086 const uint32_t new_magic = enable ? VERITY_METADATA_MAGIC_NUMBER
87 : VERITY_METADATA_MAGIC_DISABLE;
Paul Lawrence5f356482014-10-09 14:22:49 +000088 uint64_t device_length;
89 int device;
90 int retval = -1;
91
92 device = adb_open(block_device, O_RDWR | O_CLOEXEC);
93 if (device == -1) {
94 write_console(fd, "Could not open block device %s (%s).\n",
95 block_device, strerror(errno));
96 write_console(fd, "Maybe run adb remount?\n");
97 goto errout;
98 }
99
100 // find the start of the verity metadata
101 if (get_target_device_size(fd, (char*)block_device, &device_length) < 0) {
102 write_console(fd, "Could not get target device size.\n");
103 goto errout;
104 }
105
106 if (lseek64(device, device_length, SEEK_SET) < 0) {
107 write_console(fd,
108 "Could not seek to start of verity metadata block.\n");
109 goto errout;
110 }
111
112 // check the magic number
113 if (adb_read(device, &magic_number, sizeof(magic_number))
114 != sizeof(magic_number)) {
115 write_console(fd, "Couldn't read magic number!\n");
116 goto errout;
117 }
118
Paul Lawrence8ba2b022014-12-03 15:31:57 -0800119 if (!enable && magic_number == VERITY_METADATA_MAGIC_DISABLE) {
Paul Lawrence5f356482014-10-09 14:22:49 +0000120 write_console(fd, "Verity already disabled on %s\n", mount_point);
121 goto errout;
122 }
123
Paul Lawrence8ba2b022014-12-03 15:31:57 -0800124 if (enable && magic_number == VERITY_METADATA_MAGIC_NUMBER) {
125 write_console(fd, "Verity already enabled on %s\n", mount_point);
126 goto errout;
127 }
128
129 if (magic_number != VERITY_METADATA_MAGIC_NUMBER
130 && magic_number != VERITY_METADATA_MAGIC_DISABLE) {
Paul Lawrence5f356482014-10-09 14:22:49 +0000131 write_console(fd,
132 "Couldn't find verity metadata at offset %"PRIu64"!\n",
133 device_length);
134 goto errout;
135 }
136
137 if (lseek64(device, device_length, SEEK_SET) < 0) {
138 write_console(fd,
139 "Could not seek to start of verity metadata block.\n");
140 goto errout;
141 }
142
Paul Lawrence8ba2b022014-12-03 15:31:57 -0800143 if (adb_write(device, &new_magic, sizeof(new_magic)) != sizeof(new_magic)) {
144 write_console(fd, "Could not set verity %s flag on device %s with error %s\n",
145 enable ? "enabled" : "disabled",
146 block_device,
147 strerror(errno));
Paul Lawrence5f356482014-10-09 14:22:49 +0000148 goto errout;
149 }
150
Paul Lawrence8ba2b022014-12-03 15:31:57 -0800151 write_console(fd, "Verity %s on %s\n",
152 enable ? "enabled" : "disabled",
153 mount_point);
Paul Lawrence5f356482014-10-09 14:22:49 +0000154 retval = 0;
155errout:
156 if (device != -1)
157 adb_close(device);
158 return retval;
159}
160
Paul Lawrence8ba2b022014-12-03 15:31:57 -0800161void set_verity_enabled_state_service(int fd, void* cookie)
Paul Lawrence5f356482014-10-09 14:22:49 +0000162{
Paul Lawrence8ba2b022014-12-03 15:31:57 -0800163 bool enable = (cookie != NULL);
Paul Lawrence5f356482014-10-09 14:22:49 +0000164#ifdef ALLOW_ADBD_DISABLE_VERITY
165 char fstab_filename[PROPERTY_VALUE_MAX + sizeof(FSTAB_PREFIX)];
166 char propbuf[PROPERTY_VALUE_MAX];
167 int i;
Paul Lawrence8ba2b022014-12-03 15:31:57 -0800168 bool any_changed = false;
Paul Lawrence5f356482014-10-09 14:22:49 +0000169
170 property_get("ro.secure", propbuf, "0");
171 if (strcmp(propbuf, "1")) {
172 write_console(fd, "verity not enabled - ENG build\n");
173 goto errout;
174 }
175
176 property_get("ro.debuggable", propbuf, "0");
177 if (strcmp(propbuf, "1")) {
Paul Lawrence8ba2b022014-12-03 15:31:57 -0800178 write_console(fd, "verity cannot be disabled/enabled - USER build\n");
Paul Lawrence5f356482014-10-09 14:22:49 +0000179 goto errout;
180 }
181
182 property_get("ro.hardware", propbuf, "");
183 snprintf(fstab_filename, sizeof(fstab_filename), FSTAB_PREFIX"%s", propbuf);
184
185 fstab = fs_mgr_read_fstab(fstab_filename);
186 if (!fstab) {
187 write_console(fd, "Failed to open %s\nMaybe run adb root?\n",
188 fstab_filename);
189 goto errout;
190 }
191
Paul Lawrence8ba2b022014-12-03 15:31:57 -0800192 if (enable && make_system_and_vendor_block_devices_writable(fd)) {
193 goto errout;
194 }
195
Paul Lawrence5f356482014-10-09 14:22:49 +0000196 /* Loop through entries looking for ones that vold manages */
197 for (i = 0; i < fstab->num_entries; i++) {
198 if(fs_mgr_is_verified(&fstab->recs[i])) {
Paul Lawrence8ba2b022014-12-03 15:31:57 -0800199 if (!set_verity_enabled_state(fd, fstab->recs[i].blk_device,
200 fstab->recs[i].mount_point, enable)) {
201 any_changed = true;
Paul Lawrence5f356482014-10-09 14:22:49 +0000202 }
203 }
204 }
205
Paul Lawrence8ba2b022014-12-03 15:31:57 -0800206 if (any_changed) {
Paul Lawrence5f356482014-10-09 14:22:49 +0000207 write_console(fd,
208 "Now reboot your device for settings to take effect\n");
209 }
210#else
Paul Lawrence8ba2b022014-12-03 15:31:57 -0800211 write_console(fd, "%s-verity only works for userdebug builds\n",
212 disabling ? "disable" : "enable");
Paul Lawrence5f356482014-10-09 14:22:49 +0000213#endif
214
215errout:
216 adb_close(fd);
217}