blob: f5188e94f05a098cc45834f012ae1b1cdc87d9bd [file] [log] [blame]
Paul Lawrenceec900bb2014-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
Yabin Cuiaed3c612015-09-22 15:52:57 -070017#define TRACE_TAG ADB
Dan Albert33134262015-03-19 15:21:08 -070018
19#include "sysdeps.h"
20
Dan Albert1f09bda2015-01-26 17:49:17 -080021#include <fcntl.h>
22#include <inttypes.h>
23#include <stdarg.h>
Dan Albert1f09bda2015-01-26 17:49:17 -080024#include <stdio.h>
25#include <sys/stat.h>
Paul Lawrenceec900bb2014-10-09 14:22:49 +000026
Paul Lawrenceec900bb2014-10-09 14:22:49 +000027#include "cutils/properties.h"
Dan Albert33134262015-03-19 15:21:08 -070028
29#include "adb.h"
Elliott Hughesab52c182015-05-01 17:04:38 -070030#include "adb_io.h"
Dan Albert1f09bda2015-01-26 17:49:17 -080031#include "fs_mgr.h"
Elliott Hughesec7a6672015-03-16 21:58:32 +000032#include "remount_service.h"
Paul Lawrenceec900bb2014-10-09 14:22:49 +000033
Sami Tolvanen8ad80762015-10-20 13:24:24 +010034#include "fec/io.h"
35
Paul Lawrenceec900bb2014-10-09 14:22:49 +000036#define FSTAB_PREFIX "/fstab."
37struct fstab *fstab;
38
Dan Albert1f09bda2015-01-26 17:49:17 -080039#ifdef ALLOW_ADBD_DISABLE_VERITY
40static const bool kAllowDisableVerity = true;
41#else
42static const bool kAllowDisableVerity = false;
43#endif
44
Paul Lawrence982089d2014-12-03 15:31:57 -080045/* Turn verity on/off */
46static int set_verity_enabled_state(int fd, const char *block_device,
47 const char* mount_point, bool enable)
Paul Lawrenceec900bb2014-10-09 14:22:49 +000048{
Elliott Hughes9aa4fda2015-05-11 11:55:25 -070049 if (!make_block_device_writable(block_device)) {
Elliott Hughesab52c182015-05-01 17:04:38 -070050 WriteFdFmt(fd, "Could not make block device %s writable (%s).\n",
51 block_device, strerror(errno));
Sami Tolvanen8ad80762015-10-20 13:24:24 +010052 return -1;
Sami Tolvanen13449cd2015-01-02 13:30:50 +000053 }
54
Sami Tolvanen8ad80762015-10-20 13:24:24 +010055 fec::io fh(block_device, O_RDWR);
56
57 if (!fh) {
Elliott Hughesab52c182015-05-01 17:04:38 -070058 WriteFdFmt(fd, "Could not open block device %s (%s).\n", block_device, strerror(errno));
Sami Tolvanen8ad80762015-10-20 13:24:24 +010059 WriteFdFmt(fd, "Maybe run adb root?\n");
60 return -1;
Paul Lawrenceec900bb2014-10-09 14:22:49 +000061 }
62
Sami Tolvanen8ad80762015-10-20 13:24:24 +010063 fec_verity_metadata metadata;
64
65 if (!fh.get_verity_metadata(metadata)) {
66 WriteFdFmt(fd, "Couldn't find verity metadata!\n");
67 return -1;
Paul Lawrenceec900bb2014-10-09 14:22:49 +000068 }
69
Sami Tolvanen8ad80762015-10-20 13:24:24 +010070 if (!enable && metadata.disabled) {
Elliott Hughesab52c182015-05-01 17:04:38 -070071 WriteFdFmt(fd, "Verity already disabled on %s\n", mount_point);
Sami Tolvanen8ad80762015-10-20 13:24:24 +010072 return -1;
Paul Lawrenceec900bb2014-10-09 14:22:49 +000073 }
74
Sami Tolvanen8ad80762015-10-20 13:24:24 +010075 if (enable && !metadata.disabled) {
Elliott Hughesab52c182015-05-01 17:04:38 -070076 WriteFdFmt(fd, "Verity already enabled on %s\n", mount_point);
Sami Tolvanen8ad80762015-10-20 13:24:24 +010077 return -1;
Paul Lawrence982089d2014-12-03 15:31:57 -080078 }
79
Sami Tolvanen8ad80762015-10-20 13:24:24 +010080 if (!fh.set_verity_status(enable)) {
Elliott Hughesab52c182015-05-01 17:04:38 -070081 WriteFdFmt(fd, "Could not set verity %s flag on device %s with error %s\n",
82 enable ? "enabled" : "disabled",
83 block_device, strerror(errno));
Sami Tolvanen8ad80762015-10-20 13:24:24 +010084 return -1;
Paul Lawrenceec900bb2014-10-09 14:22:49 +000085 }
86
Elliott Hughesab52c182015-05-01 17:04:38 -070087 WriteFdFmt(fd, "Verity %s on %s\n", enable ? "enabled" : "disabled", mount_point);
Sami Tolvanen8ad80762015-10-20 13:24:24 +010088 return 0;
Paul Lawrenceec900bb2014-10-09 14:22:49 +000089}
90
Paul Lawrence982089d2014-12-03 15:31:57 -080091void set_verity_enabled_state_service(int fd, void* cookie)
Paul Lawrenceec900bb2014-10-09 14:22:49 +000092{
Paul Lawrence982089d2014-12-03 15:31:57 -080093 bool enable = (cookie != NULL);
Dan Albert1f09bda2015-01-26 17:49:17 -080094 if (kAllowDisableVerity) {
95 char fstab_filename[PROPERTY_VALUE_MAX + sizeof(FSTAB_PREFIX)];
96 char propbuf[PROPERTY_VALUE_MAX];
97 int i;
98 bool any_changed = false;
Paul Lawrenceec900bb2014-10-09 14:22:49 +000099
Dan Albert1f09bda2015-01-26 17:49:17 -0800100 property_get("ro.secure", propbuf, "0");
101 if (strcmp(propbuf, "1")) {
Elliott Hughesab52c182015-05-01 17:04:38 -0700102 WriteFdFmt(fd, "verity not enabled - ENG build\n");
Dan Albert1f09bda2015-01-26 17:49:17 -0800103 goto errout;
104 }
105
106 property_get("ro.debuggable", propbuf, "0");
107 if (strcmp(propbuf, "1")) {
Elliott Hughesab52c182015-05-01 17:04:38 -0700108 WriteFdFmt(fd, "verity cannot be disabled/enabled - USER build\n");
Dan Albert1f09bda2015-01-26 17:49:17 -0800109 goto errout;
110 }
111
112 property_get("ro.hardware", propbuf, "");
113 snprintf(fstab_filename, sizeof(fstab_filename), FSTAB_PREFIX"%s",
114 propbuf);
115
116 fstab = fs_mgr_read_fstab(fstab_filename);
117 if (!fstab) {
Elliott Hughesab52c182015-05-01 17:04:38 -0700118 WriteFdFmt(fd, "Failed to open %s\nMaybe run adb root?\n", fstab_filename);
Dan Albert1f09bda2015-01-26 17:49:17 -0800119 goto errout;
120 }
121
122 /* Loop through entries looking for ones that vold manages */
123 for (i = 0; i < fstab->num_entries; i++) {
124 if(fs_mgr_is_verified(&fstab->recs[i])) {
125 if (!set_verity_enabled_state(fd, fstab->recs[i].blk_device,
126 fstab->recs[i].mount_point,
127 enable)) {
128 any_changed = true;
129 }
130 }
131 }
132
133 if (any_changed) {
Elliott Hughesab52c182015-05-01 17:04:38 -0700134 WriteFdFmt(fd, "Now reboot your device for settings to take effect\n");
Dan Albert1f09bda2015-01-26 17:49:17 -0800135 }
136 } else {
Elliott Hughesab52c182015-05-01 17:04:38 -0700137 WriteFdFmt(fd, "%s-verity only works for userdebug builds\n",
138 enable ? "enable" : "disable");
Paul Lawrenceec900bb2014-10-09 14:22:49 +0000139 }
140
Bernhard Rosenkränzer2bd41242014-12-12 22:22:37 +0100141errout:
Paul Lawrenceec900bb2014-10-09 14:22:49 +0000142 adb_close(fd);
143}