Skip to content

Commit

Permalink
Refactor of use of fstab in advance of fix.
Browse files Browse the repository at this point in the history
Test: Ensure device still boots.
Bug: 65737446
Change-Id: Ie466db9f5d8c77656cc525c0d49fe6a3cce154f1
  • Loading branch information
ciphergoth committed Sep 26, 2017
1 parent 7782336 commit e2ee152
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ common_src_files := \
Devmapper.cpp \
CheckBattery.cpp \
Ext4Crypt.cpp \
VoldUtil.c \
VoldUtil.cpp \
cryptfs.cpp \
model/Disk.cpp \
model/VolumeBase.cpp \
Expand Down
9 changes: 4 additions & 5 deletions MetadataCrypt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
#include "Utils.h"
#include "VoldUtil.h"

extern struct fstab *fstab;
#define DM_CRYPT_BUF_SIZE 4096
#define TABLE_LOAD_RETRIES 10
#define DEFAULT_KEY_TARGET_TYPE "default-key"
Expand All @@ -58,7 +57,7 @@ static bool mount_via_fs_mgr(const char* mount_point, const char* blk_device) {
PLOG(ERROR) << "Failed to setexeccon";
return false;
}
auto mount_rc = fs_mgr_do_mount(fstab, const_cast<char*>(mount_point),
auto mount_rc = fs_mgr_do_mount(fstab_default, const_cast<char*>(mount_point),
const_cast<char*>(blk_device), nullptr);
if (setexeccon(nullptr)) {
PLOG(ERROR) << "Failed to clear setexeccon";
Expand All @@ -73,7 +72,7 @@ static bool mount_via_fs_mgr(const char* mount_point, const char* blk_device) {
}

static bool read_key(bool create_if_absent, KeyBuffer* key) {
auto data_rec = fs_mgr_get_crypt_entry(fstab);
auto data_rec = fs_mgr_get_crypt_entry(fstab_default);
if (!data_rec) {
LOG(ERROR) << "Failed to get data_rec";
return false;
Expand Down Expand Up @@ -253,7 +252,7 @@ bool e4crypt_mount_metadata_encrypted() {
LOG(DEBUG) << "e4crypt_mount_default_encrypted";
KeyBuffer key;
if (!read_key(false, &key)) return false;
auto data_rec = fs_mgr_get_crypt_entry(fstab);
auto data_rec = fs_mgr_get_crypt_entry(fstab_default);
if (!data_rec) {
LOG(ERROR) << "Failed to get data_rec";
return false;
Expand Down Expand Up @@ -283,7 +282,7 @@ bool e4crypt_enable_crypto() {
KeyBuffer key_ref;
if (!read_key(true, &key_ref)) return false;

auto data_rec = fs_mgr_get_crypt_entry(fstab);
auto data_rec = fs_mgr_get_crypt_entry(fstab_default);
if (!data_rec) {
LOG(ERROR) << "Failed to get data_rec";
return false;
Expand Down
2 changes: 2 additions & 0 deletions VoldUtil.c → VoldUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#include <sys/ioctl.h>
#include <linux/fs.h>

struct fstab *fstab_default;

void get_blkdev_size(int fd, unsigned long* nr_sec) {
if ((ioctl(fd, BLKGETSIZE, nr_sec)) == -1) {
*nr_sec = 0;
Expand Down
5 changes: 3 additions & 2 deletions VoldUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@
#ifndef _VOLDUTIL_H
#define _VOLDUTIL_H

#include <fstab/fstab.h>
#include <sys/cdefs.h>

extern struct fstab *fstab_default;

#define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a)))

__BEGIN_DECLS
void get_blkdev_size(int fd, unsigned long* nr_sec);
__END_DECLS

#endif
24 changes: 11 additions & 13 deletions cryptfs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,6 @@ static char* password = 0;
static int password_expiry_time = 0;
static const int password_max_age_seconds = 60;

extern struct fstab *fstab;

enum class RebootType {reboot, recovery, shutdown};
static void cryptfs_reboot(RebootType rt)
{
Expand Down Expand Up @@ -295,7 +293,7 @@ static int get_crypt_ftr_info(char **metadata_fname, off64_t *off)
int rc = -1;

if (!cached_data) {
fs_mgr_get_crypt_info(fstab, key_loc, real_blkdev, sizeof(key_loc));
fs_mgr_get_crypt_info(fstab_default, key_loc, real_blkdev, sizeof(key_loc));

if (!strcmp(key_loc, KEY_IN_FOOTER)) {
if ( (fd = open(real_blkdev, O_RDWR|O_CLOEXEC)) < 0) {
Expand Down Expand Up @@ -1420,7 +1418,7 @@ static int cryptfs_restart_internal(int restart_main)
char ro_prop[PROPERTY_VALUE_MAX];
property_get("ro.crypto.readonly", ro_prop, "");
if (strlen(ro_prop) > 0 && std::stoi(ro_prop)) {
struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab, DATA_MNT_POINT);
struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab_default, DATA_MNT_POINT);
rec->flags |= MS_RDONLY;
}

Expand All @@ -1436,7 +1434,7 @@ static int cryptfs_restart_internal(int restart_main)
SLOGE("Failed to setexeccon");
return -1;
}
while ((mount_rc = fs_mgr_do_mount(fstab, DATA_MNT_POINT,
while ((mount_rc = fs_mgr_do_mount(fstab_default, DATA_MNT_POINT,
crypto_blkdev, 0))
!= 0) {
if (mount_rc == FS_MGR_DOMNT_BUSY) {
Expand Down Expand Up @@ -1517,7 +1515,7 @@ static int do_crypto_complete(const char *mount_point)
}

if (get_crypt_ftr_and_key(&crypt_ftr)) {
fs_mgr_get_crypt_info(fstab, key_loc, 0, sizeof(key_loc));
fs_mgr_get_crypt_info(fstab_default, key_loc, 0, sizeof(key_loc));

/*
* Only report this error if key_loc is a file and it exists.
Expand Down Expand Up @@ -1585,7 +1583,7 @@ static int test_mount_encrypted_fs(struct crypt_mnt_ftr* crypt_ftr,
}
}

fs_mgr_get_crypt_info(fstab, 0, real_blkdev, sizeof(real_blkdev));
fs_mgr_get_crypt_info(fstab_default, 0, real_blkdev, sizeof(real_blkdev));

// Create crypto block device - all (non fatal) code paths
// need it
Expand Down Expand Up @@ -1617,7 +1615,7 @@ static int test_mount_encrypted_fs(struct crypt_mnt_ftr* crypt_ftr,
snprintf(tmp_mount_point, sizeof(tmp_mount_point), "%s/tmp_mnt",
mount_point);
mkdir(tmp_mount_point, 0755);
if (fs_mgr_do_mount(fstab, DATA_MNT_POINT, crypto_blkdev, tmp_mount_point)) {
if (fs_mgr_do_mount(fstab_default, DATA_MNT_POINT, crypto_blkdev, tmp_mount_point)) {
SLOGE("Error temp mounting decrypted block device\n");
delete_crypto_blk_dev(label);

Expand Down Expand Up @@ -2021,7 +2019,7 @@ static int cryptfs_enable_all_volumes(struct crypt_mnt_ftr *crypt_ftr, int how,
tot_encryption_size = crypt_ftr->fs_size;

if (how == CRYPTO_ENABLE_WIPE) {
struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab, DATA_MNT_POINT);
struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab_default, DATA_MNT_POINT);
int fs_type = get_fs_type(rec);
if (fs_type < 0) {
SLOGE("cryptfs_enable: unsupported fs type %s\n", rec->fs_type);
Expand Down Expand Up @@ -2121,8 +2119,8 @@ int cryptfs_enable_internal(const char *howarg, int crypt_type, const char *pass
}

// TODO refactor fs_mgr_get_crypt_info to get both in one call
fs_mgr_get_crypt_info(fstab, key_loc, 0, sizeof(key_loc));
fs_mgr_get_crypt_info(fstab, 0, real_blkdev, sizeof(real_blkdev));
fs_mgr_get_crypt_info(fstab_default, key_loc, 0, sizeof(key_loc));
fs_mgr_get_crypt_info(fstab_default, 0, real_blkdev, sizeof(real_blkdev));

/* Get the size of the real block device */
fd = open(real_blkdev, O_RDONLY|O_CLOEXEC);
Expand Down Expand Up @@ -2870,7 +2868,7 @@ void cryptfs_clear_password()

int cryptfs_isConvertibleToFBE()
{
struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab, DATA_MNT_POINT);
struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab_default, DATA_MNT_POINT);
return fs_mgr_is_convertible_to_fbe(rec) ? 1 : 0;
}

Expand Down Expand Up @@ -2943,6 +2941,6 @@ int cryptfs_set_password(struct crypt_mnt_ftr* ftr, const char* password,
void cryptfs_get_file_encryption_modes(const char **contents_mode_ret,
const char **filenames_mode_ret)
{
struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab, DATA_MNT_POINT);
struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab_default, DATA_MNT_POINT);
fs_mgr_get_file_encryption_modes(rec, contents_mode_ret, filenames_mode_ret);
}
24 changes: 12 additions & 12 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "VolumeManager.h"
#include "NetlinkManager.h"
#include "VoldNativeService.h"
#include "VoldUtil.h"
#include "cryptfs.h"
#include "sehandle.h"

Expand All @@ -44,8 +45,6 @@ static int process_config(VolumeManager *vm, bool* has_adoptable, bool* has_quot
static void coldboot(const char *path);
static void parse_args(int argc, char** argv);

struct fstab *fstab;

struct selabel_handle *sehandle;

using android::base::StringPrintf;
Expand Down Expand Up @@ -211,35 +210,36 @@ static void coldboot(const char *path) {
static int process_config(VolumeManager *vm, bool* has_adoptable, bool* has_quota) {
ATRACE_NAME("process_config");

fstab = fs_mgr_read_fstab_default();
if (!fstab) {
fstab_default = fs_mgr_read_fstab_default();
if (!fstab_default) {
PLOG(ERROR) << "Failed to open default fstab";
return -1;
}

/* Loop through entries looking for ones that vold manages */
*has_adoptable = false;
*has_quota = false;
for (int i = 0; i < fstab->num_entries; i++) {
if (fs_mgr_is_quota(&fstab->recs[i])) {
for (int i = 0; i < fstab_default->num_entries; i++) {
auto rec = &fstab_default->recs[i];
if (fs_mgr_is_quota(rec)) {
*has_quota = true;
}

if (fs_mgr_is_voldmanaged(&fstab->recs[i])) {
if (fs_mgr_is_nonremovable(&fstab->recs[i])) {
if (fs_mgr_is_voldmanaged(rec)) {
if (fs_mgr_is_nonremovable(rec)) {
LOG(WARNING) << "nonremovable no longer supported; ignoring volume";
continue;
}

std::string sysPattern(fstab->recs[i].blk_device);
std::string nickname(fstab->recs[i].label);
std::string sysPattern(rec->blk_device);
std::string nickname(rec->label);
int flags = 0;

if (fs_mgr_is_encryptable(&fstab->recs[i])) {
if (fs_mgr_is_encryptable(rec)) {
flags |= android::vold::Disk::Flags::kAdoptable;
*has_adoptable = true;
}
if (fs_mgr_is_noemulatedsd(&fstab->recs[i])
if (fs_mgr_is_noemulatedsd(rec)
|| property_get_bool("vold.debug.default_primary", false)) {
flags |= android::vold::Disk::Flags::kDefaultPrimary;
}
Expand Down

0 comments on commit e2ee152

Please sign in to comment.