blob: d4dd25636387c358ec6ec9ce35eafe6cb3c697a0 [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
Yabin Cui19bec5b2015-09-22 15:52:57 -070017#define TRACE_TAG ADB
Dan Albertdb6fe642015-03-19 15:21:08 -070018
19#include "sysdeps.h"
20
Dan Albert59bcbe22015-01-26 17:49:17 -080021#include <fcntl.h>
22#include <inttypes.h>
23#include <stdarg.h>
Dan Albert59bcbe22015-01-26 17:49:17 -080024#include <stdio.h>
25#include <sys/stat.h>
Paul Lawrence5f356482014-10-09 14:22:49 +000026
Elliott Hughes8b249d22016-09-23 15:40:03 -070027#include "android-base/properties.h"
28#include "android-base/stringprintf.h"
Steven Morelandb087d302017-04-13 23:48:57 -070029#include <log/log_properties.h>
Dan Albertdb6fe642015-03-19 15:21:08 -070030
31#include "adb.h"
Elliott Hughesfb596842015-05-01 17:04:38 -070032#include "adb_io.h"
Elliott Hughes8b249d22016-09-23 15:40:03 -070033#include "adb_unique_fd.h"
Dan Albert59bcbe22015-01-26 17:49:17 -080034#include "fs_mgr.h"
Elliott Hughesdedf7672015-03-16 21:58:32 +000035#include "remount_service.h"
Paul Lawrence5f356482014-10-09 14:22:49 +000036
Sami Tolvanen778f5002015-10-20 13:24:24 +010037#include "fec/io.h"
38
Paul Lawrence5f356482014-10-09 14:22:49 +000039struct fstab *fstab;
40
Dan Albert59bcbe22015-01-26 17:49:17 -080041#ifdef ALLOW_ADBD_DISABLE_VERITY
42static const bool kAllowDisableVerity = true;
43#else
44static const bool kAllowDisableVerity = false;
45#endif
46
Paul Lawrence8ba2b022014-12-03 15:31:57 -080047/* Turn verity on/off */
48static int set_verity_enabled_state(int fd, const char *block_device,
49 const char* mount_point, bool enable)
Paul Lawrence5f356482014-10-09 14:22:49 +000050{
Elliott Hughes689011f2015-05-11 11:55:25 -070051 if (!make_block_device_writable(block_device)) {
Elliott Hughesfb596842015-05-01 17:04:38 -070052 WriteFdFmt(fd, "Could not make block device %s writable (%s).\n",
53 block_device, strerror(errno));
Sami Tolvanen778f5002015-10-20 13:24:24 +010054 return -1;
Sami Tolvanen5638a1a2015-01-02 13:30:50 +000055 }
56
Sami Tolvanen778f5002015-10-20 13:24:24 +010057 fec::io fh(block_device, O_RDWR);
58
59 if (!fh) {
Elliott Hughesfb596842015-05-01 17:04:38 -070060 WriteFdFmt(fd, "Could not open block device %s (%s).\n", block_device, strerror(errno));
Sami Tolvanen778f5002015-10-20 13:24:24 +010061 WriteFdFmt(fd, "Maybe run adb root?\n");
62 return -1;
Paul Lawrence5f356482014-10-09 14:22:49 +000063 }
64
Sami Tolvanen778f5002015-10-20 13:24:24 +010065 fec_verity_metadata metadata;
66
67 if (!fh.get_verity_metadata(metadata)) {
68 WriteFdFmt(fd, "Couldn't find verity metadata!\n");
69 return -1;
Paul Lawrence5f356482014-10-09 14:22:49 +000070 }
71
Sami Tolvanen778f5002015-10-20 13:24:24 +010072 if (!enable && metadata.disabled) {
Elliott Hughesfb596842015-05-01 17:04:38 -070073 WriteFdFmt(fd, "Verity already disabled on %s\n", mount_point);
Sami Tolvanen778f5002015-10-20 13:24:24 +010074 return -1;
Paul Lawrence5f356482014-10-09 14:22:49 +000075 }
76
Sami Tolvanen778f5002015-10-20 13:24:24 +010077 if (enable && !metadata.disabled) {
Elliott Hughesfb596842015-05-01 17:04:38 -070078 WriteFdFmt(fd, "Verity already enabled on %s\n", mount_point);
Sami Tolvanen778f5002015-10-20 13:24:24 +010079 return -1;
Paul Lawrence8ba2b022014-12-03 15:31:57 -080080 }
81
Sami Tolvanen778f5002015-10-20 13:24:24 +010082 if (!fh.set_verity_status(enable)) {
Elliott Hughesfb596842015-05-01 17:04:38 -070083 WriteFdFmt(fd, "Could not set verity %s flag on device %s with error %s\n",
84 enable ? "enabled" : "disabled",
85 block_device, strerror(errno));
Sami Tolvanen778f5002015-10-20 13:24:24 +010086 return -1;
Paul Lawrence5f356482014-10-09 14:22:49 +000087 }
88
Elliott Hughesfb596842015-05-01 17:04:38 -070089 WriteFdFmt(fd, "Verity %s on %s\n", enable ? "enabled" : "disabled", mount_point);
Sami Tolvanen778f5002015-10-20 13:24:24 +010090 return 0;
Paul Lawrence5f356482014-10-09 14:22:49 +000091}
92
Elliott Hughes8b249d22016-09-23 15:40:03 -070093void set_verity_enabled_state_service(int fd, void* cookie) {
94 unique_fd closer(fd);
95
Paul Lawrence8ba2b022014-12-03 15:31:57 -080096 bool enable = (cookie != NULL);
Elliott Hughes8b249d22016-09-23 15:40:03 -070097 if (!kAllowDisableVerity) {
Elliott Hughesfb596842015-05-01 17:04:38 -070098 WriteFdFmt(fd, "%s-verity only works for userdebug builds\n",
99 enable ? "enable" : "disable");
Paul Lawrence5f356482014-10-09 14:22:49 +0000100 }
101
Elliott Hughes8b249d22016-09-23 15:40:03 -0700102 if (!android::base::GetBoolProperty("ro.secure", false)) {
103 WriteFdFmt(fd, "verity not enabled - ENG build\n");
104 return;
105 }
Mark Salyzync75f65f2016-03-28 15:52:13 -0700106 if (!__android_log_is_debuggable()) {
Elliott Hughes8b249d22016-09-23 15:40:03 -0700107 WriteFdFmt(fd, "verity cannot be disabled/enabled - USER build\n");
108 return;
109 }
110
Sandeep Patilbed28182017-02-23 16:26:21 -0800111 // read all fstab entries at once from all sources
112 fstab = fs_mgr_read_fstab_default();
Elliott Hughes8b249d22016-09-23 15:40:03 -0700113 if (!fstab) {
Sandeep Patilbed28182017-02-23 16:26:21 -0800114 WriteFdFmt(fd, "Failed to read fstab\nMaybe run adb root?\n");
Elliott Hughes8b249d22016-09-23 15:40:03 -0700115 return;
116 }
117
118 // Loop through entries looking for ones that vold manages.
119 bool any_changed = false;
120 for (int i = 0; i < fstab->num_entries; i++) {
121 if (fs_mgr_is_verified(&fstab->recs[i])) {
122 if (!set_verity_enabled_state(fd, fstab->recs[i].blk_device,
123 fstab->recs[i].mount_point,
124 enable)) {
125 any_changed = true;
126 }
127 }
128 }
129
130 if (any_changed) {
131 WriteFdFmt(fd, "Now reboot your device for settings to take effect\n");
132 }
Paul Lawrence5f356482014-10-09 14:22:49 +0000133}