Merge "adb: Support 'adb enable/disable-verity' when using AVB."
This commit is contained in:
commit
8fc6cc8910
2 changed files with 86 additions and 24 deletions
|
|
@ -363,6 +363,7 @@ LOCAL_SANITIZE := $(adb_target_sanitize)
|
||||||
LOCAL_STRIP_MODULE := keep_symbols
|
LOCAL_STRIP_MODULE := keep_symbols
|
||||||
LOCAL_STATIC_LIBRARIES := \
|
LOCAL_STATIC_LIBRARIES := \
|
||||||
libadbd \
|
libadbd \
|
||||||
|
libavb_user \
|
||||||
libbase \
|
libbase \
|
||||||
libbootloader_message \
|
libbootloader_message \
|
||||||
libfs_mgr \
|
libfs_mgr \
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@
|
||||||
|
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
|
#include <libavb_user/libavb_user.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
|
@ -45,13 +46,12 @@ static const bool kAllowDisableVerity = false;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Turn verity on/off */
|
/* Turn verity on/off */
|
||||||
static int set_verity_enabled_state(int fd, const char *block_device,
|
static bool set_verity_enabled_state(int fd, const char* block_device, const char* mount_point,
|
||||||
const char* mount_point, bool enable)
|
bool enable) {
|
||||||
{
|
|
||||||
if (!make_block_device_writable(block_device)) {
|
if (!make_block_device_writable(block_device)) {
|
||||||
WriteFdFmt(fd, "Could not make block device %s writable (%s).\n",
|
WriteFdFmt(fd, "Could not make block device %s writable (%s).\n",
|
||||||
block_device, strerror(errno));
|
block_device, strerror(errno));
|
||||||
return -1;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
fec::io fh(block_device, O_RDWR);
|
fec::io fh(block_device, O_RDWR);
|
||||||
|
|
@ -59,39 +59,84 @@ static int set_verity_enabled_state(int fd, const char *block_device,
|
||||||
if (!fh) {
|
if (!fh) {
|
||||||
WriteFdFmt(fd, "Could not open block device %s (%s).\n", block_device, strerror(errno));
|
WriteFdFmt(fd, "Could not open block device %s (%s).\n", block_device, strerror(errno));
|
||||||
WriteFdFmt(fd, "Maybe run adb root?\n");
|
WriteFdFmt(fd, "Maybe run adb root?\n");
|
||||||
return -1;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
fec_verity_metadata metadata;
|
fec_verity_metadata metadata;
|
||||||
|
|
||||||
if (!fh.get_verity_metadata(metadata)) {
|
if (!fh.get_verity_metadata(metadata)) {
|
||||||
WriteFdFmt(fd, "Couldn't find verity metadata!\n");
|
WriteFdFmt(fd, "Couldn't find verity metadata!\n");
|
||||||
return -1;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!enable && metadata.disabled) {
|
if (!enable && metadata.disabled) {
|
||||||
WriteFdFmt(fd, "Verity already disabled on %s\n", mount_point);
|
WriteFdFmt(fd, "Verity already disabled on %s\n", mount_point);
|
||||||
return -1;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (enable && !metadata.disabled) {
|
if (enable && !metadata.disabled) {
|
||||||
WriteFdFmt(fd, "Verity already enabled on %s\n", mount_point);
|
WriteFdFmt(fd, "Verity already enabled on %s\n", mount_point);
|
||||||
return -1;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!fh.set_verity_status(enable)) {
|
if (!fh.set_verity_status(enable)) {
|
||||||
WriteFdFmt(fd, "Could not set verity %s flag on device %s with error %s\n",
|
WriteFdFmt(fd, "Could not set verity %s flag on device %s with error %s\n",
|
||||||
enable ? "enabled" : "disabled",
|
enable ? "enabled" : "disabled",
|
||||||
block_device, strerror(errno));
|
block_device, strerror(errno));
|
||||||
return -1;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
WriteFdFmt(fd, "Verity %s on %s\n", enable ? "enabled" : "disabled", mount_point);
|
WriteFdFmt(fd, "Verity %s on %s\n", enable ? "enabled" : "disabled", mount_point);
|
||||||
return 0;
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Helper function to get A/B suffix, if any. If the device isn't
|
||||||
|
* using A/B the empty string is returned. Otherwise either "_a",
|
||||||
|
* "_b", ... is returned.
|
||||||
|
*
|
||||||
|
* Note that since sometime in O androidboot.slot_suffix is deprecated
|
||||||
|
* and androidboot.slot should be used instead. Since bootloaders may
|
||||||
|
* be out of sync with the OS, we check both and for extra safety
|
||||||
|
* prepend a leading underscore if there isn't one already.
|
||||||
|
*/
|
||||||
|
static std::string get_ab_suffix() {
|
||||||
|
std::string ab_suffix = android::base::GetProperty("ro.boot.slot_suffix", "");
|
||||||
|
if (ab_suffix == "") {
|
||||||
|
ab_suffix = android::base::GetProperty("ro.boot.slot", "");
|
||||||
|
}
|
||||||
|
if (ab_suffix.size() > 0 && ab_suffix[0] != '_') {
|
||||||
|
ab_suffix = std::string("_") + ab_suffix;
|
||||||
|
}
|
||||||
|
return ab_suffix;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Use AVB to turn verity on/off */
|
||||||
|
static bool set_avb_verity_enabled_state(int fd, AvbOps* ops, bool enable_verity) {
|
||||||
|
std::string ab_suffix = get_ab_suffix();
|
||||||
|
|
||||||
|
bool verity_enabled;
|
||||||
|
if (!avb_user_verity_get(ops, ab_suffix.c_str(), &verity_enabled)) {
|
||||||
|
WriteFdFmt(fd, "Error getting verity state\n");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((verity_enabled && enable_verity) || (!verity_enabled && !enable_verity)) {
|
||||||
|
WriteFdFmt(fd, "verity is already %s\n", verity_enabled ? "enabled" : "disabled");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!avb_user_verity_set(ops, ab_suffix.c_str(), enable_verity)) {
|
||||||
|
WriteFdFmt(fd, "Error setting verity\n");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
WriteFdFmt(fd, "Successfully %s verity\n", enable_verity ? "enabled" : "disabled");
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_verity_enabled_state_service(int fd, void* cookie) {
|
void set_verity_enabled_state_service(int fd, void* cookie) {
|
||||||
unique_fd closer(fd);
|
unique_fd closer(fd);
|
||||||
|
bool any_changed = false;
|
||||||
|
|
||||||
bool enable = (cookie != NULL);
|
bool enable = (cookie != NULL);
|
||||||
if (!kAllowDisableVerity) {
|
if (!kAllowDisableVerity) {
|
||||||
|
|
@ -108,21 +153,37 @@ void set_verity_enabled_state_service(int fd, void* cookie) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// read all fstab entries at once from all sources
|
// Figure out if we're using VB1.0 or VB2.0 (aka AVB).
|
||||||
fstab = fs_mgr_read_fstab_default();
|
std::string vbmeta_hash = android::base::GetProperty("ro.boot.vbmeta.digest", "");
|
||||||
if (!fstab) {
|
if (vbmeta_hash != "") {
|
||||||
WriteFdFmt(fd, "Failed to read fstab\nMaybe run adb root?\n");
|
// Yep, the system is using AVB (by contract, androidboot.vbmeta.hash is
|
||||||
return;
|
// set by the bootloader when using AVB).
|
||||||
}
|
AvbOps* ops = avb_ops_user_new();
|
||||||
|
if (ops == nullptr) {
|
||||||
|
WriteFdFmt(fd, "Error getting AVB ops\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (set_avb_verity_enabled_state(fd, ops, enable)) {
|
||||||
|
any_changed = true;
|
||||||
|
}
|
||||||
|
avb_ops_user_free(ops);
|
||||||
|
} else {
|
||||||
|
// Not using AVB - assume VB1.0.
|
||||||
|
|
||||||
// Loop through entries looking for ones that vold manages.
|
// read all fstab entries at once from all sources
|
||||||
bool any_changed = false;
|
fstab = fs_mgr_read_fstab_default();
|
||||||
for (int i = 0; i < fstab->num_entries; i++) {
|
if (!fstab) {
|
||||||
if (fs_mgr_is_verified(&fstab->recs[i])) {
|
WriteFdFmt(fd, "Failed to read fstab\nMaybe run adb root?\n");
|
||||||
if (!set_verity_enabled_state(fd, fstab->recs[i].blk_device,
|
return;
|
||||||
fstab->recs[i].mount_point,
|
}
|
||||||
enable)) {
|
|
||||||
any_changed = true;
|
// Loop through entries looking for ones that vold manages.
|
||||||
|
for (int i = 0; i < fstab->num_entries; i++) {
|
||||||
|
if (fs_mgr_is_verified(&fstab->recs[i])) {
|
||||||
|
if (set_verity_enabled_state(fd, fstab->recs[i].blk_device,
|
||||||
|
fstab->recs[i].mount_point, enable)) {
|
||||||
|
any_changed = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue