2015-11-09 02:56:23 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2018-10-23 22:06:55 +02:00
|
|
|
#include "FsCrypt.h"
|
2015-04-29 00:14:15 +02:00
|
|
|
|
2016-01-21 21:26:12 +01:00
|
|
|
#include "KeyStorage.h"
|
2016-06-02 20:01:19 +02:00
|
|
|
#include "KeyUtil.h"
|
2015-11-09 02:56:23 +01:00
|
|
|
#include "Utils.h"
|
2017-10-06 23:29:33 +02:00
|
|
|
#include "VoldUtil.h"
|
|
|
|
|
2016-05-17 23:17:56 +02:00
|
|
|
#include <algorithm>
|
2015-04-29 00:14:15 +02:00
|
|
|
#include <map>
|
2019-11-01 06:59:34 +01:00
|
|
|
#include <optional>
|
2016-01-28 11:09:46 +01:00
|
|
|
#include <set>
|
2015-04-10 16:48:51 +02:00
|
|
|
#include <sstream>
|
2016-03-09 18:31:37 +01:00
|
|
|
#include <string>
|
2016-06-02 20:01:19 +02:00
|
|
|
#include <vector>
|
2015-04-29 00:14:15 +02:00
|
|
|
|
2015-05-06 16:04:43 +02:00
|
|
|
#include <dirent.h>
|
2016-03-09 18:31:37 +01:00
|
|
|
#include <errno.h>
|
2015-05-06 16:04:43 +02:00
|
|
|
#include <fcntl.h>
|
2016-05-17 23:17:56 +02:00
|
|
|
#include <limits.h>
|
2016-01-13 00:52:16 +01:00
|
|
|
#include <selinux/android.h>
|
2016-03-09 18:31:37 +01:00
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2018-09-18 22:30:21 +02:00
|
|
|
#include <unistd.h>
|
2015-04-29 00:14:15 +02:00
|
|
|
|
2015-08-24 15:53:28 +02:00
|
|
|
#include <private/android_filesystem_config.h>
|
|
|
|
|
2017-10-20 17:17:54 +02:00
|
|
|
#include "android/os/IVold.h"
|
|
|
|
|
2016-01-13 00:52:16 +01:00
|
|
|
#define EMULATED_USES_SELINUX 0
|
2016-04-15 04:45:16 +02:00
|
|
|
#define MANAGE_MISC_DIRS 0
|
2016-01-13 00:52:16 +01:00
|
|
|
|
2015-11-09 02:56:23 +01:00
|
|
|
#include <cutils/fs.h>
|
2016-06-02 20:01:19 +02:00
|
|
|
#include <cutils/properties.h>
|
|
|
|
|
2018-10-23 22:06:55 +02:00
|
|
|
#include <fscrypt/fscrypt.h>
|
2017-05-10 02:01:04 +02:00
|
|
|
#include <keyutils.h>
|
2015-11-09 02:56:23 +01:00
|
|
|
|
2015-12-05 00:50:53 +01:00
|
|
|
#include <android-base/file.h>
|
2015-12-05 02:55:33 +01:00
|
|
|
#include <android-base/logging.h>
|
2017-10-10 01:35:51 +02:00
|
|
|
#include <android-base/properties.h>
|
2015-12-05 00:50:53 +01:00
|
|
|
#include <android-base/stringprintf.h>
|
vold: support v2 encryption policies
Add support for setting v2 encryption policies when configured in the
fstab (for internal storage) or in system properties (for adoptable
storage), and for installing and evicting the keys for such policies.
v2 policies support the same encryption modes and flags as v1 policies,
but internally they use a more standard, secure, and flexible KDF. Due
to this, some future features will be supported by v2 policies only.
Bug: 140500999
Test: Configured a device to use v2 encryption policies (applied the
needed kernel patches and added
"fileencryption=aes-256-xts:aes-256-cts:v2" to fstab, and set the
corresponding system properties for adoptable storage). Wiped
userdata, booted device and checked logs to verify that v2
policies were being used.
Also enabled virtual SD card and formatted as adoptable storage;
verified it works and that v2 policies were being used on it.
Also created, started, and stopped a 2nd user and verified their
keys were evicted.
Also verified that the device comes up again after rebooting.
Also verified that a device using v1 encryption policies continues
to work, both with and without an updated kernel -- including
stopping a user so that their keys get evicted.
Change-Id: If64028d8580584b2c33c614cabd5d6b93657f608
2019-09-30 22:06:47 +02:00
|
|
|
#include <android-base/strings.h>
|
2018-11-12 08:26:02 +01:00
|
|
|
#include <android-base/unique_fd.h>
|
2015-04-29 00:14:15 +02:00
|
|
|
|
2015-11-09 02:56:23 +01:00
|
|
|
using android::base::StringPrintf;
|
2019-01-29 23:34:01 +01:00
|
|
|
using android::fs_mgr::GetEntryForMountPoint;
|
2020-01-24 00:29:30 +01:00
|
|
|
using android::vold::BuildDataPath;
|
2020-02-11 14:20:29 +01:00
|
|
|
using android::vold::IsFilesystemSupported;
|
2016-02-08 16:55:41 +01:00
|
|
|
using android::vold::kEmptyAuthentication;
|
2017-08-01 18:15:53 +02:00
|
|
|
using android::vold::KeyBuffer;
|
2020-02-12 20:04:05 +01:00
|
|
|
using android::vold::makeGen;
|
|
|
|
using android::vold::retrieveKey;
|
|
|
|
using android::vold::retrieveOrGenerateKey;
|
2020-02-11 14:20:29 +01:00
|
|
|
using android::vold::SetQuotaInherit;
|
|
|
|
using android::vold::SetQuotaProjectId;
|
2019-03-26 07:14:19 +01:00
|
|
|
using android::vold::writeStringToFile;
|
2019-10-24 23:55:17 +02:00
|
|
|
using namespace android::fscrypt;
|
2015-11-09 02:56:23 +01:00
|
|
|
|
2015-04-29 00:14:15 +02:00
|
|
|
namespace {
|
2016-10-31 11:28:25 +01:00
|
|
|
|
2018-10-23 22:06:55 +02:00
|
|
|
const std::string device_key_dir = std::string() + DATA_MNT_POINT + fscrypt_unencrypted_folder;
|
2016-03-09 18:31:37 +01:00
|
|
|
const std::string device_key_path = device_key_dir + "/key";
|
|
|
|
const std::string device_key_temp = device_key_dir + "/temp";
|
2016-02-03 22:39:13 +01:00
|
|
|
|
2016-03-09 18:31:37 +01:00
|
|
|
const std::string user_key_dir = std::string() + DATA_MNT_POINT + "/misc/vold/user_keys";
|
|
|
|
const std::string user_key_temp = user_key_dir + "/temp";
|
2017-10-20 17:17:54 +02:00
|
|
|
const std::string prepare_subdirs_path = "/system/bin/vold_prepare_subdirs";
|
2016-01-20 14:12:38 +01:00
|
|
|
|
2017-10-26 20:16:39 +02:00
|
|
|
const std::string systemwide_volume_key_dir =
|
|
|
|
std::string() + DATA_MNT_POINT + "/misc/vold/volume_keys";
|
|
|
|
|
2019-09-12 00:00:08 +02:00
|
|
|
bool s_systemwide_keys_initialized = false;
|
2016-02-02 20:14:59 +01:00
|
|
|
|
2016-03-09 18:31:37 +01:00
|
|
|
// Some users are ephemeral, don't try to wipe their keys from disk
|
|
|
|
std::set<userid_t> s_ephemeral_users;
|
2016-02-03 19:52:41 +01:00
|
|
|
|
2020-01-24 00:29:30 +01:00
|
|
|
// Map user ids to encryption policies
|
|
|
|
std::map<userid_t, EncryptionPolicy> s_de_policies;
|
|
|
|
std::map<userid_t, EncryptionPolicy> s_ce_policies;
|
2015-04-29 00:14:15 +02:00
|
|
|
|
2018-09-18 22:30:21 +02:00
|
|
|
} // namespace
|
2015-04-29 00:14:15 +02:00
|
|
|
|
2018-10-23 22:06:55 +02:00
|
|
|
static bool fscrypt_is_emulated() {
|
2016-02-09 10:50:32 +01:00
|
|
|
return property_get_bool("persist.sys.emulate_fbe", false);
|
|
|
|
}
|
|
|
|
|
2017-10-09 19:55:21 +02:00
|
|
|
static const char* escape_empty(const std::string& value) {
|
|
|
|
return value.empty() ? "null" : value.c_str();
|
2015-04-29 00:14:15 +02:00
|
|
|
}
|
|
|
|
|
2016-02-01 15:10:43 +01:00
|
|
|
static std::string get_de_key_path(userid_t user_id) {
|
|
|
|
return StringPrintf("%s/de/%d", user_key_dir.c_str(), user_id);
|
|
|
|
}
|
|
|
|
|
2016-05-17 23:17:56 +02:00
|
|
|
static std::string get_ce_key_directory_path(userid_t user_id) {
|
|
|
|
return StringPrintf("%s/ce/%d", user_key_dir.c_str(), user_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the keys newest first
|
|
|
|
static std::vector<std::string> get_ce_key_paths(const std::string& directory_path) {
|
|
|
|
auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(directory_path.c_str()), closedir);
|
|
|
|
if (!dirp) {
|
|
|
|
PLOG(ERROR) << "Unable to open ce key directory: " + directory_path;
|
|
|
|
return std::vector<std::string>();
|
|
|
|
}
|
|
|
|
std::vector<std::string> result;
|
|
|
|
for (;;) {
|
|
|
|
errno = 0;
|
|
|
|
auto const entry = readdir(dirp.get());
|
|
|
|
if (!entry) {
|
|
|
|
if (errno) {
|
|
|
|
PLOG(ERROR) << "Unable to read ce key directory: " + directory_path;
|
|
|
|
return std::vector<std::string>();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (entry->d_type != DT_DIR || entry->d_name[0] != 'c') {
|
|
|
|
LOG(DEBUG) << "Skipping non-key " << entry->d_name;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
result.emplace_back(directory_path + "/" + entry->d_name);
|
|
|
|
}
|
|
|
|
std::sort(result.begin(), result.end());
|
|
|
|
std::reverse(result.begin(), result.end());
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string get_ce_key_current_path(const std::string& directory_path) {
|
|
|
|
return directory_path + "/current";
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool get_ce_key_new_path(const std::string& directory_path,
|
2018-09-18 22:30:21 +02:00
|
|
|
const std::vector<std::string>& paths, std::string* ce_key_path) {
|
2016-05-17 23:17:56 +02:00
|
|
|
if (paths.empty()) {
|
|
|
|
*ce_key_path = get_ce_key_current_path(directory_path);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
for (unsigned int i = 0; i < UINT_MAX; i++) {
|
|
|
|
auto const candidate = StringPrintf("%s/cx%010u", directory_path.c_str(), i);
|
|
|
|
if (paths[0] < candidate) {
|
|
|
|
*ce_key_path = candidate;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Discard all keys but the named one; rename it to canonical name.
|
|
|
|
// No point in acting on errors in this; ignore them.
|
2018-09-18 22:30:21 +02:00
|
|
|
static void fixate_user_ce_key(const std::string& directory_path, const std::string& to_fix,
|
2016-05-17 23:17:56 +02:00
|
|
|
const std::vector<std::string>& paths) {
|
2018-09-18 22:30:21 +02:00
|
|
|
for (auto const other_path : paths) {
|
2016-05-17 23:17:56 +02:00
|
|
|
if (other_path != to_fix) {
|
|
|
|
android::vold::destroyKey(other_path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
auto const current_path = get_ce_key_current_path(directory_path);
|
|
|
|
if (to_fix != current_path) {
|
|
|
|
LOG(DEBUG) << "Renaming " << to_fix << " to " << current_path;
|
|
|
|
if (rename(to_fix.c_str(), current_path.c_str()) != 0) {
|
|
|
|
PLOG(WARNING) << "Unable to rename " << to_fix << " to " << current_path;
|
2018-11-12 08:26:02 +01:00
|
|
|
return;
|
2016-05-17 23:17:56 +02:00
|
|
|
}
|
|
|
|
}
|
2018-12-08 00:36:09 +01:00
|
|
|
android::vold::FsyncDirectory(directory_path);
|
2016-05-17 23:17:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool read_and_fixate_user_ce_key(userid_t user_id,
|
|
|
|
const android::vold::KeyAuthentication& auth,
|
2018-09-18 22:30:21 +02:00
|
|
|
KeyBuffer* ce_key) {
|
2016-05-17 23:17:56 +02:00
|
|
|
auto const directory_path = get_ce_key_directory_path(user_id);
|
|
|
|
auto const paths = get_ce_key_paths(directory_path);
|
2018-09-18 22:30:21 +02:00
|
|
|
for (auto const ce_key_path : paths) {
|
2016-05-17 23:17:56 +02:00
|
|
|
LOG(DEBUG) << "Trying user CE key " << ce_key_path;
|
2020-02-12 20:04:05 +01:00
|
|
|
if (retrieveKey(ce_key_path, auth, ce_key)) {
|
2016-05-17 23:17:56 +02:00
|
|
|
LOG(DEBUG) << "Successfully retrieved key";
|
|
|
|
fixate_user_ce_key(directory_path, ce_key_path, paths);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LOG(ERROR) << "Failed to find working ce key for user " << user_id;
|
|
|
|
return false;
|
2015-05-19 13:34:09 +02:00
|
|
|
}
|
|
|
|
|
vold: support v2 encryption policies
Add support for setting v2 encryption policies when configured in the
fstab (for internal storage) or in system properties (for adoptable
storage), and for installing and evicting the keys for such policies.
v2 policies support the same encryption modes and flags as v1 policies,
but internally they use a more standard, secure, and flexible KDF. Due
to this, some future features will be supported by v2 policies only.
Bug: 140500999
Test: Configured a device to use v2 encryption policies (applied the
needed kernel patches and added
"fileencryption=aes-256-xts:aes-256-cts:v2" to fstab, and set the
corresponding system properties for adoptable storage). Wiped
userdata, booted device and checked logs to verify that v2
policies were being used.
Also enabled virtual SD card and formatted as adoptable storage;
verified it works and that v2 policies were being used on it.
Also created, started, and stopped a 2nd user and verified their
keys were evicted.
Also verified that the device comes up again after rebooting.
Also verified that a device using v1 encryption policies continues
to work, both with and without an updated kernel -- including
stopping a user so that their keys get evicted.
Change-Id: If64028d8580584b2c33c614cabd5d6b93657f608
2019-09-30 22:06:47 +02:00
|
|
|
// Retrieve the options to use for encryption policies on the /data filesystem.
|
2020-01-24 00:29:30 +01:00
|
|
|
static bool get_data_file_encryption_options(EncryptionOptions* options) {
|
vold: support v2 encryption policies
Add support for setting v2 encryption policies when configured in the
fstab (for internal storage) or in system properties (for adoptable
storage), and for installing and evicting the keys for such policies.
v2 policies support the same encryption modes and flags as v1 policies,
but internally they use a more standard, secure, and flexible KDF. Due
to this, some future features will be supported by v2 policies only.
Bug: 140500999
Test: Configured a device to use v2 encryption policies (applied the
needed kernel patches and added
"fileencryption=aes-256-xts:aes-256-cts:v2" to fstab, and set the
corresponding system properties for adoptable storage). Wiped
userdata, booted device and checked logs to verify that v2
policies were being used.
Also enabled virtual SD card and formatted as adoptable storage;
verified it works and that v2 policies were being used on it.
Also created, started, and stopped a 2nd user and verified their
keys were evicted.
Also verified that the device comes up again after rebooting.
Also verified that a device using v1 encryption policies continues
to work, both with and without an updated kernel -- including
stopping a user so that their keys get evicted.
Change-Id: If64028d8580584b2c33c614cabd5d6b93657f608
2019-09-30 22:06:47 +02:00
|
|
|
auto entry = GetEntryForMountPoint(&fstab_default, DATA_MNT_POINT);
|
|
|
|
if (entry == nullptr) {
|
2020-01-24 00:29:30 +01:00
|
|
|
LOG(ERROR) << "No mount point entry for " << DATA_MNT_POINT;
|
|
|
|
return false;
|
vold: support v2 encryption policies
Add support for setting v2 encryption policies when configured in the
fstab (for internal storage) or in system properties (for adoptable
storage), and for installing and evicting the keys for such policies.
v2 policies support the same encryption modes and flags as v1 policies,
but internally they use a more standard, secure, and flexible KDF. Due
to this, some future features will be supported by v2 policies only.
Bug: 140500999
Test: Configured a device to use v2 encryption policies (applied the
needed kernel patches and added
"fileencryption=aes-256-xts:aes-256-cts:v2" to fstab, and set the
corresponding system properties for adoptable storage). Wiped
userdata, booted device and checked logs to verify that v2
policies were being used.
Also enabled virtual SD card and formatted as adoptable storage;
verified it works and that v2 policies were being used on it.
Also created, started, and stopped a 2nd user and verified their
keys were evicted.
Also verified that the device comes up again after rebooting.
Also verified that a device using v1 encryption policies continues
to work, both with and without an updated kernel -- including
stopping a user so that their keys get evicted.
Change-Id: If64028d8580584b2c33c614cabd5d6b93657f608
2019-09-30 22:06:47 +02:00
|
|
|
}
|
2019-10-25 08:21:44 +02:00
|
|
|
if (!ParseOptions(entry->encryption_options, options)) {
|
|
|
|
LOG(ERROR) << "Unable to parse encryption options for " << DATA_MNT_POINT ": "
|
|
|
|
<< entry->encryption_options;
|
2020-01-24 00:29:30 +01:00
|
|
|
return false;
|
2019-10-25 08:21:44 +02:00
|
|
|
}
|
2020-01-24 00:29:30 +01:00
|
|
|
return true;
|
vold: support v2 encryption policies
Add support for setting v2 encryption policies when configured in the
fstab (for internal storage) or in system properties (for adoptable
storage), and for installing and evicting the keys for such policies.
v2 policies support the same encryption modes and flags as v1 policies,
but internally they use a more standard, secure, and flexible KDF. Due
to this, some future features will be supported by v2 policies only.
Bug: 140500999
Test: Configured a device to use v2 encryption policies (applied the
needed kernel patches and added
"fileencryption=aes-256-xts:aes-256-cts:v2" to fstab, and set the
corresponding system properties for adoptable storage). Wiped
userdata, booted device and checked logs to verify that v2
policies were being used.
Also enabled virtual SD card and formatted as adoptable storage;
verified it works and that v2 policies were being used on it.
Also created, started, and stopped a 2nd user and verified their
keys were evicted.
Also verified that the device comes up again after rebooting.
Also verified that a device using v1 encryption policies continues
to work, both with and without an updated kernel -- including
stopping a user so that their keys get evicted.
Change-Id: If64028d8580584b2c33c614cabd5d6b93657f608
2019-09-30 22:06:47 +02:00
|
|
|
}
|
|
|
|
|
2020-02-03 22:06:45 +01:00
|
|
|
static bool install_storage_key(const std::string& mountpoint, const EncryptionOptions& options,
|
|
|
|
const KeyBuffer& key, EncryptionPolicy* policy) {
|
|
|
|
KeyBuffer ephemeral_wrapped_key;
|
|
|
|
if (options.use_hw_wrapped_key) {
|
|
|
|
if (!exportWrappedStorageKey(key, &ephemeral_wrapped_key)) {
|
|
|
|
LOG(ERROR) << "Failed to get ephemeral wrapped key";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return installKey(mountpoint, options, options.use_hw_wrapped_key ? ephemeral_wrapped_key : key,
|
|
|
|
policy);
|
|
|
|
}
|
|
|
|
|
vold: support v2 encryption policies
Add support for setting v2 encryption policies when configured in the
fstab (for internal storage) or in system properties (for adoptable
storage), and for installing and evicting the keys for such policies.
v2 policies support the same encryption modes and flags as v1 policies,
but internally they use a more standard, secure, and flexible KDF. Due
to this, some future features will be supported by v2 policies only.
Bug: 140500999
Test: Configured a device to use v2 encryption policies (applied the
needed kernel patches and added
"fileencryption=aes-256-xts:aes-256-cts:v2" to fstab, and set the
corresponding system properties for adoptable storage). Wiped
userdata, booted device and checked logs to verify that v2
policies were being used.
Also enabled virtual SD card and formatted as adoptable storage;
verified it works and that v2 policies were being used on it.
Also created, started, and stopped a 2nd user and verified their
keys were evicted.
Also verified that the device comes up again after rebooting.
Also verified that a device using v1 encryption policies continues
to work, both with and without an updated kernel -- including
stopping a user so that their keys get evicted.
Change-Id: If64028d8580584b2c33c614cabd5d6b93657f608
2019-09-30 22:06:47 +02:00
|
|
|
// Retrieve the options to use for encryption policies on adoptable storage.
|
2019-10-24 23:55:17 +02:00
|
|
|
static bool get_volume_file_encryption_options(EncryptionOptions* options) {
|
2019-10-25 07:52:02 +02:00
|
|
|
auto contents_mode =
|
|
|
|
android::base::GetProperty("ro.crypto.volume.contents_mode", "aes-256-xts");
|
|
|
|
auto filenames_mode =
|
|
|
|
android::base::GetProperty("ro.crypto.volume.filenames_mode", "aes-256-heh");
|
2020-01-24 00:29:30 +01:00
|
|
|
auto options_string = android::base::GetProperty("ro.crypto.volume.options",
|
|
|
|
contents_mode + ":" + filenames_mode + ":v1");
|
|
|
|
if (!ParseOptions(options_string, options)) {
|
|
|
|
LOG(ERROR) << "Unable to parse volume encryption options: " << options_string;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2019-09-30 22:05:58 +02:00
|
|
|
}
|
|
|
|
|
2016-03-09 18:31:37 +01:00
|
|
|
static bool read_and_install_user_ce_key(userid_t user_id,
|
|
|
|
const android::vold::KeyAuthentication& auth) {
|
2020-01-24 00:29:30 +01:00
|
|
|
if (s_ce_policies.count(user_id) != 0) return true;
|
|
|
|
EncryptionOptions options;
|
|
|
|
if (!get_data_file_encryption_options(&options)) return false;
|
2017-08-01 18:15:53 +02:00
|
|
|
KeyBuffer ce_key;
|
2016-05-17 23:17:56 +02:00
|
|
|
if (!read_and_fixate_user_ce_key(user_id, auth, &ce_key)) return false;
|
2020-01-24 00:29:30 +01:00
|
|
|
EncryptionPolicy ce_policy;
|
2020-02-03 22:06:45 +01:00
|
|
|
if (!install_storage_key(DATA_MNT_POINT, options, ce_key, &ce_policy)) return false;
|
2020-01-24 00:29:30 +01:00
|
|
|
s_ce_policies[user_id] = ce_policy;
|
2016-01-28 11:09:46 +01:00
|
|
|
LOG(DEBUG) << "Installed ce key for user " << user_id;
|
2016-01-21 21:26:12 +01:00
|
|
|
return true;
|
2016-01-20 14:12:38 +01:00
|
|
|
}
|
|
|
|
|
2016-03-09 18:31:37 +01:00
|
|
|
static bool prepare_dir(const std::string& dir, mode_t mode, uid_t uid, gid_t gid) {
|
2016-01-28 11:09:46 +01:00
|
|
|
LOG(DEBUG) << "Preparing: " << dir;
|
2016-01-27 15:30:22 +01:00
|
|
|
if (fs_prepare_dir(dir.c_str(), mode, uid, gid) != 0) {
|
|
|
|
PLOG(ERROR) << "Failed to prepare " << dir;
|
2016-01-20 14:12:38 +01:00
|
|
|
return false;
|
|
|
|
}
|
2016-01-27 15:30:22 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-04-15 04:45:16 +02:00
|
|
|
static bool destroy_dir(const std::string& dir) {
|
|
|
|
LOG(DEBUG) << "Destroying: " << dir;
|
|
|
|
if (rmdir(dir.c_str()) != 0 && errno != ENOENT) {
|
|
|
|
PLOG(ERROR) << "Failed to destroy " << dir;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-01-28 11:09:46 +01:00
|
|
|
// NB this assumes that there is only one thread listening for crypt commands, because
|
|
|
|
// it creates keys in a fixed location.
|
2016-02-01 15:10:43 +01:00
|
|
|
static bool create_and_install_user_keys(userid_t user_id, bool create_ephemeral) {
|
2020-01-24 00:29:30 +01:00
|
|
|
EncryptionOptions options;
|
|
|
|
if (!get_data_file_encryption_options(&options)) return false;
|
2017-08-01 18:15:53 +02:00
|
|
|
KeyBuffer de_key, ce_key;
|
2020-02-12 20:04:05 +01:00
|
|
|
if (!generateStorageKey(makeGen(options), &de_key)) return false;
|
|
|
|
if (!generateStorageKey(makeGen(options), &ce_key)) return false;
|
2015-11-25 10:13:03 +01:00
|
|
|
if (create_ephemeral) {
|
2016-01-28 11:09:46 +01:00
|
|
|
// If the key should be created as ephemeral, don't store it.
|
|
|
|
s_ephemeral_users.insert(user_id);
|
2016-01-27 15:30:22 +01:00
|
|
|
} else {
|
2016-05-17 23:17:56 +02:00
|
|
|
auto const directory_path = get_ce_key_directory_path(user_id);
|
|
|
|
if (!prepare_dir(directory_path, 0700, AID_ROOT, AID_ROOT)) return false;
|
|
|
|
auto const paths = get_ce_key_paths(directory_path);
|
|
|
|
std::string ce_key_path;
|
|
|
|
if (!get_ce_key_new_path(directory_path, paths, &ce_key_path)) return false;
|
2018-09-18 22:30:21 +02:00
|
|
|
if (!android::vold::storeKeyAtomically(ce_key_path, user_key_temp, kEmptyAuthentication,
|
|
|
|
ce_key))
|
|
|
|
return false;
|
2016-05-17 23:17:56 +02:00
|
|
|
fixate_user_ce_key(directory_path, ce_key_path, paths);
|
|
|
|
// Write DE key second; once this is written, all is good.
|
2016-06-02 20:01:19 +02:00
|
|
|
if (!android::vold::storeKeyAtomically(get_de_key_path(user_id), user_key_temp,
|
2018-09-18 22:30:21 +02:00
|
|
|
kEmptyAuthentication, de_key))
|
|
|
|
return false;
|
2015-05-06 16:04:43 +02:00
|
|
|
}
|
2020-01-24 00:29:30 +01:00
|
|
|
EncryptionPolicy de_policy;
|
2020-02-03 22:06:45 +01:00
|
|
|
if (!install_storage_key(DATA_MNT_POINT, options, de_key, &de_policy)) return false;
|
2020-01-24 00:29:30 +01:00
|
|
|
s_de_policies[user_id] = de_policy;
|
|
|
|
EncryptionPolicy ce_policy;
|
2020-02-03 22:06:45 +01:00
|
|
|
if (!install_storage_key(DATA_MNT_POINT, options, ce_key, &ce_policy)) return false;
|
2020-01-24 00:29:30 +01:00
|
|
|
s_ce_policies[user_id] = ce_policy;
|
2016-02-01 15:10:43 +01:00
|
|
|
LOG(DEBUG) << "Created keys for user " << user_id;
|
2016-01-20 14:12:38 +01:00
|
|
|
return true;
|
2015-05-06 16:04:43 +02:00
|
|
|
}
|
|
|
|
|
2020-01-24 00:29:30 +01:00
|
|
|
static bool lookup_policy(const std::map<userid_t, EncryptionPolicy>& key_map, userid_t user_id,
|
|
|
|
EncryptionPolicy* policy) {
|
2016-01-28 11:09:46 +01:00
|
|
|
auto refi = key_map.find(user_id);
|
|
|
|
if (refi == key_map.end()) {
|
2019-04-02 19:38:15 +02:00
|
|
|
LOG(DEBUG) << "Cannot find key for " << user_id;
|
2016-01-28 11:09:46 +01:00
|
|
|
return false;
|
|
|
|
}
|
2020-01-24 00:29:30 +01:00
|
|
|
*policy = refi->second;
|
2016-01-28 11:09:46 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-03-09 18:31:37 +01:00
|
|
|
static bool is_numeric(const char* name) {
|
|
|
|
for (const char* p = name; *p != '\0'; p++) {
|
|
|
|
if (!isdigit(*p)) return false;
|
2016-02-01 15:10:43 +01:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool load_all_de_keys() {
|
2020-01-24 00:29:30 +01:00
|
|
|
EncryptionOptions options;
|
|
|
|
if (!get_data_file_encryption_options(&options)) return false;
|
2016-02-01 15:10:43 +01:00
|
|
|
auto de_dir = user_key_dir + "/de";
|
2016-03-09 18:31:37 +01:00
|
|
|
auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(de_dir.c_str()), closedir);
|
2016-02-01 15:10:43 +01:00
|
|
|
if (!dirp) {
|
|
|
|
PLOG(ERROR) << "Unable to read de key directory";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (;;) {
|
|
|
|
errno = 0;
|
|
|
|
auto entry = readdir(dirp.get());
|
|
|
|
if (!entry) {
|
|
|
|
if (errno) {
|
|
|
|
PLOG(ERROR) << "Unable to read de key directory";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (entry->d_type != DT_DIR || !is_numeric(entry->d_name)) {
|
|
|
|
LOG(DEBUG) << "Skipping non-de-key " << entry->d_name;
|
|
|
|
continue;
|
|
|
|
}
|
2017-09-19 02:19:28 +02:00
|
|
|
userid_t user_id = std::stoi(entry->d_name);
|
2020-01-24 00:29:30 +01:00
|
|
|
if (s_de_policies.count(user_id) == 0) {
|
2016-02-08 16:55:41 +01:00
|
|
|
auto key_path = de_dir + "/" + entry->d_name;
|
2020-01-24 00:29:30 +01:00
|
|
|
KeyBuffer de_key;
|
2020-02-12 20:04:05 +01:00
|
|
|
if (!retrieveKey(key_path, kEmptyAuthentication, &de_key)) return false;
|
2020-01-24 00:29:30 +01:00
|
|
|
EncryptionPolicy de_policy;
|
2020-02-03 22:06:45 +01:00
|
|
|
if (!install_storage_key(DATA_MNT_POINT, options, de_key, &de_policy)) return false;
|
2020-01-24 00:29:30 +01:00
|
|
|
s_de_policies[user_id] = de_policy;
|
2016-02-01 15:10:43 +01:00
|
|
|
LOG(DEBUG) << "Installed de key for user " << user_id;
|
|
|
|
}
|
|
|
|
}
|
2018-10-23 22:06:55 +02:00
|
|
|
// fscrypt:TODO: go through all DE directories, ensure that all user dirs have the
|
2016-02-01 15:10:43 +01:00
|
|
|
// correct policy set on them, and that no rogue ones exist.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-09-12 00:00:08 +02:00
|
|
|
bool fscrypt_initialize_systemwide_keys() {
|
|
|
|
LOG(INFO) << "fscrypt_initialize_systemwide_keys";
|
2016-02-03 19:52:41 +01:00
|
|
|
|
2019-09-12 00:00:08 +02:00
|
|
|
if (s_systemwide_keys_initialized) {
|
2016-02-09 10:50:32 +01:00
|
|
|
LOG(INFO) << "Already initialized";
|
2016-02-09 11:04:39 +01:00
|
|
|
return true;
|
2016-02-03 19:52:41 +01:00
|
|
|
}
|
2020-01-24 00:29:30 +01:00
|
|
|
EncryptionOptions options;
|
|
|
|
if (!get_data_file_encryption_options(&options)) return false;
|
2016-02-03 19:52:41 +01:00
|
|
|
|
2020-01-24 00:29:30 +01:00
|
|
|
KeyBuffer device_key;
|
2020-02-12 20:04:05 +01:00
|
|
|
if (!retrieveOrGenerateKey(device_key_path, device_key_temp, kEmptyAuthentication,
|
|
|
|
makeGen(options), &device_key))
|
2020-01-24 00:29:30 +01:00
|
|
|
return false;
|
vold: support v2 encryption policies
Add support for setting v2 encryption policies when configured in the
fstab (for internal storage) or in system properties (for adoptable
storage), and for installing and evicting the keys for such policies.
v2 policies support the same encryption modes and flags as v1 policies,
but internally they use a more standard, secure, and flexible KDF. Due
to this, some future features will be supported by v2 policies only.
Bug: 140500999
Test: Configured a device to use v2 encryption policies (applied the
needed kernel patches and added
"fileencryption=aes-256-xts:aes-256-cts:v2" to fstab, and set the
corresponding system properties for adoptable storage). Wiped
userdata, booted device and checked logs to verify that v2
policies were being used.
Also enabled virtual SD card and formatted as adoptable storage;
verified it works and that v2 policies were being used on it.
Also created, started, and stopped a 2nd user and verified their
keys were evicted.
Also verified that the device comes up again after rebooting.
Also verified that a device using v1 encryption policies continues
to work, both with and without an updated kernel -- including
stopping a user so that their keys get evicted.
Change-Id: If64028d8580584b2c33c614cabd5d6b93657f608
2019-09-30 22:06:47 +02:00
|
|
|
|
2020-01-24 00:29:30 +01:00
|
|
|
EncryptionPolicy device_policy;
|
2020-02-03 22:06:45 +01:00
|
|
|
if (!install_storage_key(DATA_MNT_POINT, options, device_key, &device_policy)) return false;
|
2017-02-02 23:52:12 +01:00
|
|
|
|
2019-10-24 23:55:17 +02:00
|
|
|
std::string options_string;
|
|
|
|
if (!OptionsToString(device_policy.options, &options_string)) {
|
|
|
|
LOG(ERROR) << "Unable to serialize options";
|
|
|
|
return false;
|
|
|
|
}
|
2020-01-24 00:29:30 +01:00
|
|
|
std::string options_filename = std::string(DATA_MNT_POINT) + fscrypt_key_mode;
|
vold: support v2 encryption policies
Add support for setting v2 encryption policies when configured in the
fstab (for internal storage) or in system properties (for adoptable
storage), and for installing and evicting the keys for such policies.
v2 policies support the same encryption modes and flags as v1 policies,
but internally they use a more standard, secure, and flexible KDF. Due
to this, some future features will be supported by v2 policies only.
Bug: 140500999
Test: Configured a device to use v2 encryption policies (applied the
needed kernel patches and added
"fileencryption=aes-256-xts:aes-256-cts:v2" to fstab, and set the
corresponding system properties for adoptable storage). Wiped
userdata, booted device and checked logs to verify that v2
policies were being used.
Also enabled virtual SD card and formatted as adoptable storage;
verified it works and that v2 policies were being used on it.
Also created, started, and stopped a 2nd user and verified their
keys were evicted.
Also verified that the device comes up again after rebooting.
Also verified that a device using v1 encryption policies continues
to work, both with and without an updated kernel -- including
stopping a user so that their keys get evicted.
Change-Id: If64028d8580584b2c33c614cabd5d6b93657f608
2019-09-30 22:06:47 +02:00
|
|
|
if (!android::vold::writeStringToFile(options_string, options_filename)) return false;
|
2016-05-24 23:20:38 +02:00
|
|
|
|
2020-01-24 00:29:30 +01:00
|
|
|
std::string ref_filename = std::string(DATA_MNT_POINT) + fscrypt_key_ref;
|
2019-10-24 23:55:17 +02:00
|
|
|
if (!android::vold::writeStringToFile(device_policy.key_raw_ref, ref_filename)) return false;
|
2016-06-02 20:01:19 +02:00
|
|
|
LOG(INFO) << "Wrote system DE key reference to:" << ref_filename;
|
2016-02-03 19:52:41 +01:00
|
|
|
|
2019-09-12 00:00:08 +02:00
|
|
|
KeyBuffer per_boot_key;
|
2020-02-12 20:04:05 +01:00
|
|
|
if (!generateStorageKey(makeGen(options), &per_boot_key)) return false;
|
2020-01-24 00:29:30 +01:00
|
|
|
EncryptionPolicy per_boot_policy;
|
2020-02-03 22:06:45 +01:00
|
|
|
if (!install_storage_key(DATA_MNT_POINT, options, per_boot_key, &per_boot_policy)) return false;
|
2019-09-12 00:00:08 +02:00
|
|
|
std::string per_boot_ref_filename = std::string("/data") + fscrypt_key_per_boot_ref;
|
2020-01-24 00:29:30 +01:00
|
|
|
if (!android::vold::writeStringToFile(per_boot_policy.key_raw_ref, per_boot_ref_filename))
|
|
|
|
return false;
|
2019-09-12 00:00:08 +02:00
|
|
|
LOG(INFO) << "Wrote per boot key reference to:" << per_boot_ref_filename;
|
|
|
|
|
2019-03-26 07:14:19 +01:00
|
|
|
if (!android::vold::FsyncDirectory(device_key_dir)) return false;
|
2019-09-12 00:00:08 +02:00
|
|
|
s_systemwide_keys_initialized = true;
|
2016-02-09 11:04:39 +01:00
|
|
|
return true;
|
2016-02-03 19:52:41 +01:00
|
|
|
}
|
|
|
|
|
2018-10-23 22:06:55 +02:00
|
|
|
bool fscrypt_init_user0() {
|
|
|
|
LOG(DEBUG) << "fscrypt_init_user0";
|
|
|
|
if (fscrypt_is_native()) {
|
2016-02-09 11:04:39 +01:00
|
|
|
if (!prepare_dir(user_key_dir, 0700, AID_ROOT, AID_ROOT)) return false;
|
|
|
|
if (!prepare_dir(user_key_dir + "/ce", 0700, AID_ROOT, AID_ROOT)) return false;
|
|
|
|
if (!prepare_dir(user_key_dir + "/de", 0700, AID_ROOT, AID_ROOT)) return false;
|
2016-06-02 20:01:19 +02:00
|
|
|
if (!android::vold::pathExists(get_de_key_path(0))) {
|
2016-02-09 11:04:39 +01:00
|
|
|
if (!create_and_install_user_keys(0, false)) return false;
|
2016-02-01 15:28:12 +01:00
|
|
|
}
|
2016-02-02 01:02:29 +01:00
|
|
|
// TODO: switch to loading only DE_0 here once framework makes
|
|
|
|
// explicit calls to install DE keys for secondary users
|
2016-02-09 11:04:39 +01:00
|
|
|
if (!load_all_de_keys()) return false;
|
2016-02-01 15:28:12 +01:00
|
|
|
}
|
2016-02-02 01:02:29 +01:00
|
|
|
// We can only safely prepare DE storage here, since CE keys are probably
|
|
|
|
// entangled with user credentials. The framework will always prepare CE
|
|
|
|
// storage once CE keys are installed.
|
2018-10-23 22:06:55 +02:00
|
|
|
if (!fscrypt_prepare_user_storage("", 0, 0, android::os::IVold::STORAGE_FLAG_DE)) {
|
2016-02-02 01:02:29 +01:00
|
|
|
LOG(ERROR) << "Failed to prepare user 0 storage";
|
2016-02-09 11:04:39 +01:00
|
|
|
return false;
|
2016-02-02 01:02:29 +01:00
|
|
|
}
|
2016-02-08 20:21:42 +01:00
|
|
|
|
|
|
|
// If this is a non-FBE device that recently left an emulated mode,
|
|
|
|
// restore user data directories to known-good state.
|
2018-10-23 22:06:55 +02:00
|
|
|
if (!fscrypt_is_native() && !fscrypt_is_emulated()) {
|
|
|
|
fscrypt_unlock_user_key(0, 0, "!", "!");
|
2016-02-08 20:21:42 +01:00
|
|
|
}
|
|
|
|
|
2016-02-09 11:04:39 +01:00
|
|
|
return true;
|
2016-02-01 15:28:12 +01:00
|
|
|
}
|
|
|
|
|
2018-10-23 22:06:55 +02:00
|
|
|
bool fscrypt_vold_create_user_key(userid_t user_id, int serial, bool ephemeral) {
|
|
|
|
LOG(DEBUG) << "fscrypt_vold_create_user_key for " << user_id << " serial " << serial;
|
|
|
|
if (!fscrypt_is_native()) {
|
2016-02-09 11:04:39 +01:00
|
|
|
return true;
|
2016-01-28 13:23:53 +01:00
|
|
|
}
|
2016-01-28 11:09:46 +01:00
|
|
|
// FIXME test for existence of key that is not loaded yet
|
2020-01-24 00:29:30 +01:00
|
|
|
if (s_ce_policies.count(user_id) != 0) {
|
2018-10-23 22:06:55 +02:00
|
|
|
LOG(ERROR) << "Already exists, can't fscrypt_vold_create_user_key for " << user_id
|
2016-03-09 18:31:37 +01:00
|
|
|
<< " serial " << serial;
|
2016-01-20 14:12:38 +01:00
|
|
|
// FIXME should we fail the command?
|
2016-02-09 11:04:39 +01:00
|
|
|
return true;
|
2015-05-06 16:04:43 +02:00
|
|
|
}
|
2016-02-01 15:10:43 +01:00
|
|
|
if (!create_and_install_user_keys(user_id, ephemeral)) {
|
2016-02-09 11:04:39 +01:00
|
|
|
return false;
|
2015-11-09 02:56:23 +01:00
|
|
|
}
|
2016-02-09 11:04:39 +01:00
|
|
|
return true;
|
2015-11-09 02:56:23 +01:00
|
|
|
}
|
|
|
|
|
2019-04-04 00:44:06 +02:00
|
|
|
// "Lock" all encrypted directories whose key has been removed. This is needed
|
2019-09-30 22:05:58 +02:00
|
|
|
// in the case where the keys are being put in the session keyring (rather in
|
|
|
|
// the newer filesystem-level keyrings), because removing a key from the session
|
|
|
|
// keyring doesn't affect inodes in the kernel's inode cache whose per-file key
|
|
|
|
// was already set up. So to remove the per-file keys and make the files
|
|
|
|
// "appear encrypted", these inodes must be evicted.
|
2019-04-04 00:44:06 +02:00
|
|
|
//
|
|
|
|
// To do this, sync() to clean all dirty inodes, then drop all reclaimable slab
|
|
|
|
// objects systemwide. This is overkill, but it's the best available method
|
|
|
|
// currently. Don't use drop_caches mode "3" because that also evicts pagecache
|
|
|
|
// for in-use files; all files relevant here are already closed and sync'ed.
|
2019-09-30 22:05:58 +02:00
|
|
|
static void drop_caches_if_needed() {
|
|
|
|
if (android::vold::isFsKeyringSupported()) {
|
|
|
|
return;
|
|
|
|
}
|
2017-07-27 18:34:57 +02:00
|
|
|
sync();
|
2019-04-04 00:44:06 +02:00
|
|
|
if (!writeStringToFile("2", "/proc/sys/vm/drop_caches")) {
|
2017-07-27 18:34:57 +02:00
|
|
|
PLOG(ERROR) << "Failed to drop caches during key eviction";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-31 11:28:25 +01:00
|
|
|
static bool evict_ce_key(userid_t user_id) {
|
|
|
|
bool success = true;
|
2020-01-24 00:29:30 +01:00
|
|
|
EncryptionPolicy policy;
|
2016-10-31 11:28:25 +01:00
|
|
|
// If we haven't loaded the CE key, no need to evict it.
|
2020-01-24 00:29:30 +01:00
|
|
|
if (lookup_policy(s_ce_policies, user_id, &policy)) {
|
|
|
|
success &= android::vold::evictKey(DATA_MNT_POINT, policy);
|
2019-09-30 22:05:58 +02:00
|
|
|
drop_caches_if_needed();
|
2016-10-31 11:28:25 +01:00
|
|
|
}
|
2020-01-24 00:29:30 +01:00
|
|
|
s_ce_policies.erase(user_id);
|
2016-10-31 11:28:25 +01:00
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2018-10-23 22:06:55 +02:00
|
|
|
bool fscrypt_destroy_user_key(userid_t user_id) {
|
|
|
|
LOG(DEBUG) << "fscrypt_destroy_user_key(" << user_id << ")";
|
|
|
|
if (!fscrypt_is_native()) {
|
2016-02-09 11:04:39 +01:00
|
|
|
return true;
|
2016-01-28 13:23:53 +01:00
|
|
|
}
|
2016-01-28 11:09:46 +01:00
|
|
|
bool success = true;
|
2016-10-31 11:28:25 +01:00
|
|
|
success &= evict_ce_key(user_id);
|
2020-01-24 00:29:30 +01:00
|
|
|
EncryptionPolicy de_policy;
|
|
|
|
success &= lookup_policy(s_de_policies, user_id, &de_policy) &&
|
|
|
|
android::vold::evictKey(DATA_MNT_POINT, de_policy);
|
|
|
|
s_de_policies.erase(user_id);
|
2016-01-28 11:09:46 +01:00
|
|
|
auto it = s_ephemeral_users.find(user_id);
|
|
|
|
if (it != s_ephemeral_users.end()) {
|
|
|
|
s_ephemeral_users.erase(it);
|
2016-01-21 21:26:12 +01:00
|
|
|
} else {
|
2018-09-18 22:30:21 +02:00
|
|
|
for (auto const path : get_ce_key_paths(get_ce_key_directory_path(user_id))) {
|
2016-05-17 23:17:56 +02:00
|
|
|
success &= android::vold::destroyKey(path);
|
|
|
|
}
|
2016-07-20 00:29:53 +02:00
|
|
|
auto de_key_path = get_de_key_path(user_id);
|
2016-06-02 20:01:19 +02:00
|
|
|
if (android::vold::pathExists(de_key_path)) {
|
2016-07-20 00:29:53 +02:00
|
|
|
success &= android::vold::destroyKey(de_key_path);
|
|
|
|
} else {
|
|
|
|
LOG(INFO) << "Not present so not erasing: " << de_key_path;
|
|
|
|
}
|
2015-05-19 13:34:09 +02:00
|
|
|
}
|
2016-02-09 11:04:39 +01:00
|
|
|
return success;
|
2015-05-19 13:34:09 +02:00
|
|
|
}
|
2015-11-09 02:56:23 +01:00
|
|
|
|
2016-02-09 11:04:39 +01:00
|
|
|
static bool emulated_lock(const std::string& path) {
|
2016-01-13 00:52:16 +01:00
|
|
|
if (chmod(path.c_str(), 0000) != 0) {
|
|
|
|
PLOG(ERROR) << "Failed to chmod " << path;
|
2016-02-09 11:04:39 +01:00
|
|
|
return false;
|
2016-01-13 00:52:16 +01:00
|
|
|
}
|
|
|
|
#if EMULATED_USES_SELINUX
|
|
|
|
if (setfilecon(path.c_str(), "u:object_r:storage_stub_file:s0") != 0) {
|
|
|
|
PLOG(WARNING) << "Failed to setfilecon " << path;
|
2016-02-09 11:04:39 +01:00
|
|
|
return false;
|
2016-01-13 00:52:16 +01:00
|
|
|
}
|
|
|
|
#endif
|
2016-02-09 11:04:39 +01:00
|
|
|
return true;
|
2016-01-13 00:52:16 +01:00
|
|
|
}
|
|
|
|
|
2016-02-09 11:04:39 +01:00
|
|
|
static bool emulated_unlock(const std::string& path, mode_t mode) {
|
2016-01-13 00:52:16 +01:00
|
|
|
if (chmod(path.c_str(), mode) != 0) {
|
|
|
|
PLOG(ERROR) << "Failed to chmod " << path;
|
2016-01-21 18:24:49 +01:00
|
|
|
// FIXME temporary workaround for b/26713622
|
2018-10-23 22:06:55 +02:00
|
|
|
if (fscrypt_is_emulated()) return false;
|
2016-01-13 00:52:16 +01:00
|
|
|
}
|
|
|
|
#if EMULATED_USES_SELINUX
|
|
|
|
if (selinux_android_restorecon(path.c_str(), SELINUX_ANDROID_RESTORECON_FORCE) != 0) {
|
|
|
|
PLOG(WARNING) << "Failed to restorecon " << path;
|
2016-01-21 18:24:49 +01:00
|
|
|
// FIXME temporary workaround for b/26713622
|
2018-10-23 22:06:55 +02:00
|
|
|
if (fscrypt_is_emulated()) return false;
|
2016-01-13 00:52:16 +01:00
|
|
|
}
|
|
|
|
#endif
|
2016-02-09 11:04:39 +01:00
|
|
|
return true;
|
2016-01-13 00:52:16 +01:00
|
|
|
}
|
|
|
|
|
2017-10-09 19:55:21 +02:00
|
|
|
static bool parse_hex(const std::string& hex, std::string* result) {
|
|
|
|
if (hex == "!") {
|
2016-03-09 01:08:32 +01:00
|
|
|
*result = "";
|
2016-02-08 16:55:41 +01:00
|
|
|
return true;
|
|
|
|
}
|
2016-03-09 01:08:32 +01:00
|
|
|
if (android::vold::HexToStr(hex, *result) != 0) {
|
2016-03-09 18:31:37 +01:00
|
|
|
LOG(ERROR) << "Invalid FBE hex string"; // Don't log the string for security reasons
|
2016-02-08 16:55:41 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-11-01 06:59:34 +01:00
|
|
|
static std::optional<android::vold::KeyAuthentication> authentication_from_hex(
|
|
|
|
const std::string& token_hex, const std::string& secret_hex) {
|
|
|
|
std::string token, secret;
|
|
|
|
if (!parse_hex(token_hex, &token)) return std::optional<android::vold::KeyAuthentication>();
|
|
|
|
if (!parse_hex(secret_hex, &secret)) return std::optional<android::vold::KeyAuthentication>();
|
|
|
|
if (secret.empty()) {
|
|
|
|
return kEmptyAuthentication;
|
|
|
|
} else {
|
|
|
|
return android::vold::KeyAuthentication(token, secret);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-10 01:35:51 +02:00
|
|
|
static std::string volkey_path(const std::string& misc_path, const std::string& volume_uuid) {
|
|
|
|
return misc_path + "/vold/volume_keys/" + volume_uuid + "/default";
|
|
|
|
}
|
|
|
|
|
2017-10-26 20:16:39 +02:00
|
|
|
static std::string volume_secdiscardable_path(const std::string& volume_uuid) {
|
|
|
|
return systemwide_volume_key_dir + "/" + volume_uuid + "/secdiscardable";
|
|
|
|
}
|
|
|
|
|
2017-10-10 01:35:51 +02:00
|
|
|
static bool read_or_create_volkey(const std::string& misc_path, const std::string& volume_uuid,
|
2019-10-24 23:55:17 +02:00
|
|
|
EncryptionPolicy* policy) {
|
2017-10-26 20:16:39 +02:00
|
|
|
auto secdiscardable_path = volume_secdiscardable_path(volume_uuid);
|
|
|
|
std::string secdiscardable_hash;
|
|
|
|
if (android::vold::pathExists(secdiscardable_path)) {
|
|
|
|
if (!android::vold::readSecdiscardable(secdiscardable_path, &secdiscardable_hash))
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
if (fs_mkdirs(secdiscardable_path.c_str(), 0700) != 0) {
|
|
|
|
PLOG(ERROR) << "Creating directories for: " << secdiscardable_path;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!android::vold::createSecdiscardable(secdiscardable_path, &secdiscardable_hash))
|
|
|
|
return false;
|
|
|
|
}
|
2017-10-10 01:35:51 +02:00
|
|
|
auto key_path = volkey_path(misc_path, volume_uuid);
|
|
|
|
if (fs_mkdirs(key_path.c_str(), 0700) != 0) {
|
|
|
|
PLOG(ERROR) << "Creating directories for: " << key_path;
|
|
|
|
return false;
|
|
|
|
}
|
2017-10-26 20:16:39 +02:00
|
|
|
android::vold::KeyAuthentication auth("", secdiscardable_hash);
|
vold: support v2 encryption policies
Add support for setting v2 encryption policies when configured in the
fstab (for internal storage) or in system properties (for adoptable
storage), and for installing and evicting the keys for such policies.
v2 policies support the same encryption modes and flags as v1 policies,
but internally they use a more standard, secure, and flexible KDF. Due
to this, some future features will be supported by v2 policies only.
Bug: 140500999
Test: Configured a device to use v2 encryption policies (applied the
needed kernel patches and added
"fileencryption=aes-256-xts:aes-256-cts:v2" to fstab, and set the
corresponding system properties for adoptable storage). Wiped
userdata, booted device and checked logs to verify that v2
policies were being used.
Also enabled virtual SD card and formatted as adoptable storage;
verified it works and that v2 policies were being used on it.
Also created, started, and stopped a 2nd user and verified their
keys were evicted.
Also verified that the device comes up again after rebooting.
Also verified that a device using v1 encryption policies continues
to work, both with and without an updated kernel -- including
stopping a user so that their keys get evicted.
Change-Id: If64028d8580584b2c33c614cabd5d6b93657f608
2019-09-30 22:06:47 +02:00
|
|
|
|
2020-01-24 00:29:30 +01:00
|
|
|
EncryptionOptions options;
|
|
|
|
if (!get_volume_file_encryption_options(&options)) return false;
|
|
|
|
KeyBuffer key;
|
2020-02-12 20:04:05 +01:00
|
|
|
if (!retrieveOrGenerateKey(key_path, key_path + "_tmp", auth, makeGen(options), &key))
|
2020-02-03 22:06:45 +01:00
|
|
|
return false;
|
|
|
|
if (!install_storage_key(BuildDataPath(volume_uuid), options, key, policy)) return false;
|
2020-01-24 00:29:30 +01:00
|
|
|
return true;
|
2017-10-10 01:35:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool destroy_volkey(const std::string& misc_path, const std::string& volume_uuid) {
|
2017-10-24 23:54:43 +02:00
|
|
|
auto path = volkey_path(misc_path, volume_uuid);
|
|
|
|
if (!android::vold::pathExists(path)) return true;
|
|
|
|
return android::vold::destroyKey(path);
|
2017-10-10 01:35:51 +02:00
|
|
|
}
|
|
|
|
|
2019-11-01 06:59:34 +01:00
|
|
|
static bool fscrypt_rewrap_user_key(userid_t user_id, int serial,
|
|
|
|
const android::vold::KeyAuthentication& retrieve_auth,
|
|
|
|
const android::vold::KeyAuthentication& store_auth) {
|
2016-02-09 11:04:39 +01:00
|
|
|
if (s_ephemeral_users.count(user_id) != 0) return true;
|
2019-11-01 06:59:34 +01:00
|
|
|
auto const directory_path = get_ce_key_directory_path(user_id);
|
|
|
|
KeyBuffer ce_key;
|
|
|
|
std::string ce_key_current_path = get_ce_key_current_path(directory_path);
|
2020-02-12 20:04:05 +01:00
|
|
|
if (retrieveKey(ce_key_current_path, retrieve_auth, &ce_key)) {
|
2019-11-01 06:59:34 +01:00
|
|
|
LOG(DEBUG) << "Successfully retrieved key";
|
|
|
|
// TODO(147732812): Remove this once Locksettingservice is fixed.
|
|
|
|
// Currently it calls fscrypt_clear_user_key_auth with a secret when lockscreen is
|
|
|
|
// changed from swipe to none or vice-versa
|
2020-02-12 20:04:05 +01:00
|
|
|
} else if (retrieveKey(ce_key_current_path, kEmptyAuthentication, &ce_key)) {
|
2019-11-01 06:59:34 +01:00
|
|
|
LOG(DEBUG) << "Successfully retrieved key with empty auth";
|
|
|
|
} else {
|
|
|
|
LOG(ERROR) << "Failed to retrieve key for user " << user_id;
|
2016-02-09 11:04:39 +01:00
|
|
|
return false;
|
2016-02-08 16:55:41 +01:00
|
|
|
}
|
2016-05-17 23:17:56 +02:00
|
|
|
auto const paths = get_ce_key_paths(directory_path);
|
|
|
|
std::string ce_key_path;
|
|
|
|
if (!get_ce_key_new_path(directory_path, paths, &ce_key_path)) return false;
|
2019-11-01 06:59:34 +01:00
|
|
|
if (!android::vold::storeKeyAtomically(ce_key_path, user_key_temp, store_auth, ce_key))
|
|
|
|
return false;
|
2018-12-08 00:36:09 +01:00
|
|
|
if (!android::vold::FsyncDirectory(directory_path)) return false;
|
2016-05-17 23:17:56 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-11-01 06:59:34 +01:00
|
|
|
bool fscrypt_add_user_key_auth(userid_t user_id, int serial, const std::string& token_hex,
|
|
|
|
const std::string& secret_hex) {
|
|
|
|
LOG(DEBUG) << "fscrypt_add_user_key_auth " << user_id << " serial=" << serial
|
|
|
|
<< " token_present=" << (token_hex != "!");
|
|
|
|
if (!fscrypt_is_native()) return true;
|
|
|
|
auto auth = authentication_from_hex(token_hex, secret_hex);
|
|
|
|
if (!auth) return false;
|
|
|
|
return fscrypt_rewrap_user_key(user_id, serial, kEmptyAuthentication, *auth);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool fscrypt_clear_user_key_auth(userid_t user_id, int serial, const std::string& token_hex,
|
|
|
|
const std::string& secret_hex) {
|
|
|
|
LOG(DEBUG) << "fscrypt_clear_user_key_auth " << user_id << " serial=" << serial
|
|
|
|
<< " token_present=" << (token_hex != "!");
|
|
|
|
if (!fscrypt_is_native()) return true;
|
|
|
|
auto auth = authentication_from_hex(token_hex, secret_hex);
|
|
|
|
if (!auth) return false;
|
|
|
|
return fscrypt_rewrap_user_key(user_id, serial, *auth, kEmptyAuthentication);
|
|
|
|
}
|
|
|
|
|
2018-10-23 22:06:55 +02:00
|
|
|
bool fscrypt_fixate_newest_user_key_auth(userid_t user_id) {
|
|
|
|
LOG(DEBUG) << "fscrypt_fixate_newest_user_key_auth " << user_id;
|
|
|
|
if (!fscrypt_is_native()) return true;
|
2016-07-26 00:55:36 +02:00
|
|
|
if (s_ephemeral_users.count(user_id) != 0) return true;
|
2016-05-17 23:17:56 +02:00
|
|
|
auto const directory_path = get_ce_key_directory_path(user_id);
|
|
|
|
auto const paths = get_ce_key_paths(directory_path);
|
|
|
|
if (paths.empty()) {
|
|
|
|
LOG(ERROR) << "No ce keys present, cannot fixate for user " << user_id;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
fixate_user_ce_key(directory_path, paths[0], paths);
|
2016-02-09 11:04:39 +01:00
|
|
|
return true;
|
2016-02-08 16:55:41 +01:00
|
|
|
}
|
|
|
|
|
2016-02-02 01:02:29 +01:00
|
|
|
// TODO: rename to 'install' for consistency, and take flags to know which keys to install
|
2018-10-23 22:06:55 +02:00
|
|
|
bool fscrypt_unlock_user_key(userid_t user_id, int serial, const std::string& token_hex,
|
2017-10-09 19:55:21 +02:00
|
|
|
const std::string& secret_hex) {
|
2018-10-23 22:06:55 +02:00
|
|
|
LOG(DEBUG) << "fscrypt_unlock_user_key " << user_id << " serial=" << serial
|
2017-10-09 19:55:21 +02:00
|
|
|
<< " token_present=" << (token_hex != "!");
|
2018-10-23 22:06:55 +02:00
|
|
|
if (fscrypt_is_native()) {
|
2020-01-24 00:29:30 +01:00
|
|
|
if (s_ce_policies.count(user_id) != 0) {
|
2016-02-08 16:55:41 +01:00
|
|
|
LOG(WARNING) << "Tried to unlock already-unlocked key for user " << user_id;
|
2016-02-09 11:04:39 +01:00
|
|
|
return true;
|
2016-02-08 16:55:41 +01:00
|
|
|
}
|
2019-11-01 06:59:34 +01:00
|
|
|
auto auth = authentication_from_hex(token_hex, secret_hex);
|
|
|
|
if (!auth) return false;
|
|
|
|
if (!read_and_install_user_ce_key(user_id, *auth)) {
|
2016-02-01 15:28:12 +01:00
|
|
|
LOG(ERROR) << "Couldn't read key for " << user_id;
|
2016-02-09 11:04:39 +01:00
|
|
|
return false;
|
2015-11-09 02:56:23 +01:00
|
|
|
}
|
2015-12-08 01:27:01 +01:00
|
|
|
} else {
|
|
|
|
// When in emulation mode, we just use chmod. However, we also
|
|
|
|
// unlock directories when not in emulation mode, to bring devices
|
|
|
|
// back into a known-good state.
|
2016-02-09 11:04:39 +01:00
|
|
|
if (!emulated_unlock(android::vold::BuildDataSystemCePath(user_id), 0771) ||
|
2016-03-09 18:31:37 +01:00
|
|
|
!emulated_unlock(android::vold::BuildDataMiscCePath(user_id), 01771) ||
|
2017-10-09 19:55:21 +02:00
|
|
|
!emulated_unlock(android::vold::BuildDataMediaCePath("", user_id), 0770) ||
|
|
|
|
!emulated_unlock(android::vold::BuildDataUserCePath("", user_id), 0771)) {
|
2016-01-13 00:52:16 +01:00
|
|
|
LOG(ERROR) << "Failed to unlock user " << user_id;
|
2016-02-09 11:04:39 +01:00
|
|
|
return false;
|
2015-12-08 01:27:01 +01:00
|
|
|
}
|
2015-11-09 02:56:23 +01:00
|
|
|
}
|
2016-02-09 11:04:39 +01:00
|
|
|
return true;
|
2015-11-09 02:56:23 +01:00
|
|
|
}
|
|
|
|
|
2016-02-02 01:02:29 +01:00
|
|
|
// TODO: rename to 'evict' for consistency
|
2018-10-23 22:06:55 +02:00
|
|
|
bool fscrypt_lock_user_key(userid_t user_id) {
|
|
|
|
LOG(DEBUG) << "fscrypt_lock_user_key " << user_id;
|
|
|
|
if (fscrypt_is_native()) {
|
2016-10-31 11:28:25 +01:00
|
|
|
return evict_ce_key(user_id);
|
2018-10-23 22:06:55 +02:00
|
|
|
} else if (fscrypt_is_emulated()) {
|
2015-11-09 02:56:23 +01:00
|
|
|
// When in emulation mode, we just use chmod
|
2016-02-09 11:04:39 +01:00
|
|
|
if (!emulated_lock(android::vold::BuildDataSystemCePath(user_id)) ||
|
2016-03-09 18:31:37 +01:00
|
|
|
!emulated_lock(android::vold::BuildDataMiscCePath(user_id)) ||
|
2017-10-09 19:55:21 +02:00
|
|
|
!emulated_lock(android::vold::BuildDataMediaCePath("", user_id)) ||
|
|
|
|
!emulated_lock(android::vold::BuildDataUserCePath("", user_id))) {
|
2016-02-09 10:30:23 +01:00
|
|
|
LOG(ERROR) << "Failed to lock user " << user_id;
|
2016-02-09 11:04:39 +01:00
|
|
|
return false;
|
2015-11-09 02:56:23 +01:00
|
|
|
}
|
|
|
|
}
|
2015-12-08 01:27:01 +01:00
|
|
|
|
2016-02-09 11:04:39 +01:00
|
|
|
return true;
|
2015-11-09 02:56:23 +01:00
|
|
|
}
|
|
|
|
|
2017-10-20 17:17:54 +02:00
|
|
|
static bool prepare_subdirs(const std::string& action, const std::string& volume_uuid,
|
|
|
|
userid_t user_id, int flags) {
|
|
|
|
if (0 != android::vold::ForkExecvp(
|
|
|
|
std::vector<std::string>{prepare_subdirs_path, action, volume_uuid,
|
|
|
|
std::to_string(user_id), std::to_string(flags)})) {
|
|
|
|
LOG(ERROR) << "vold_prepare_subdirs failed";
|
2017-10-17 02:01:44 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-10-23 22:06:55 +02:00
|
|
|
bool fscrypt_prepare_user_storage(const std::string& volume_uuid, userid_t user_id, int serial,
|
2017-10-09 19:55:21 +02:00
|
|
|
int flags) {
|
2018-10-23 22:06:55 +02:00
|
|
|
LOG(DEBUG) << "fscrypt_prepare_user_storage for volume " << escape_empty(volume_uuid)
|
2016-03-09 18:31:37 +01:00
|
|
|
<< ", user " << user_id << ", serial " << serial << ", flags " << flags;
|
2016-02-02 01:02:29 +01:00
|
|
|
|
2017-10-20 17:17:54 +02:00
|
|
|
if (flags & android::os::IVold::STORAGE_FLAG_DE) {
|
2016-04-15 04:45:16 +02:00
|
|
|
// DE_sys key
|
|
|
|
auto system_legacy_path = android::vold::BuildDataSystemLegacyPath(user_id);
|
|
|
|
auto misc_legacy_path = android::vold::BuildDataMiscLegacyPath(user_id);
|
|
|
|
auto profiles_de_path = android::vold::BuildDataProfilesDePath(user_id);
|
|
|
|
|
|
|
|
// DE_n key
|
2016-02-02 01:02:29 +01:00
|
|
|
auto system_de_path = android::vold::BuildDataSystemDePath(user_id);
|
|
|
|
auto misc_de_path = android::vold::BuildDataMiscDePath(user_id);
|
2018-01-22 20:25:29 +01:00
|
|
|
auto vendor_de_path = android::vold::BuildDataVendorDePath(user_id);
|
2016-02-02 01:02:29 +01:00
|
|
|
auto user_de_path = android::vold::BuildDataUserDePath(volume_uuid, user_id);
|
|
|
|
|
2017-10-09 19:55:21 +02:00
|
|
|
if (volume_uuid.empty()) {
|
2017-10-05 23:07:09 +02:00
|
|
|
if (!prepare_dir(system_legacy_path, 0700, AID_SYSTEM, AID_SYSTEM)) return false;
|
2016-04-15 04:45:16 +02:00
|
|
|
#if MANAGE_MISC_DIRS
|
2017-10-05 23:07:09 +02:00
|
|
|
if (!prepare_dir(misc_legacy_path, 0750, multiuser_get_uid(user_id, AID_SYSTEM),
|
2018-09-18 22:30:21 +02:00
|
|
|
multiuser_get_uid(user_id, AID_EVERYBODY)))
|
|
|
|
return false;
|
2016-04-15 04:45:16 +02:00
|
|
|
#endif
|
2017-10-05 23:07:09 +02:00
|
|
|
if (!prepare_dir(profiles_de_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
|
2016-04-15 04:45:16 +02:00
|
|
|
|
2017-10-05 23:07:09 +02:00
|
|
|
if (!prepare_dir(system_de_path, 0770, AID_SYSTEM, AID_SYSTEM)) return false;
|
|
|
|
if (!prepare_dir(misc_de_path, 01771, AID_SYSTEM, AID_MISC)) return false;
|
2018-01-22 20:25:29 +01:00
|
|
|
if (!prepare_dir(vendor_de_path, 0771, AID_ROOT, AID_ROOT)) return false;
|
2017-10-05 23:07:09 +02:00
|
|
|
}
|
2016-02-09 11:04:39 +01:00
|
|
|
if (!prepare_dir(user_de_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
|
2016-03-02 19:36:50 +01:00
|
|
|
|
2018-10-23 22:06:55 +02:00
|
|
|
if (fscrypt_is_native()) {
|
2019-10-24 23:55:17 +02:00
|
|
|
EncryptionPolicy de_policy;
|
2017-10-09 19:55:21 +02:00
|
|
|
if (volume_uuid.empty()) {
|
2020-01-24 00:29:30 +01:00
|
|
|
if (!lookup_policy(s_de_policies, user_id, &de_policy)) return false;
|
2019-10-24 23:55:17 +02:00
|
|
|
if (!EnsurePolicy(de_policy, system_de_path)) return false;
|
|
|
|
if (!EnsurePolicy(de_policy, misc_de_path)) return false;
|
|
|
|
if (!EnsurePolicy(de_policy, vendor_de_path)) return false;
|
2017-10-10 01:35:51 +02:00
|
|
|
} else {
|
2019-10-24 23:55:17 +02:00
|
|
|
if (!read_or_create_volkey(misc_de_path, volume_uuid, &de_policy)) return false;
|
2017-10-05 23:07:09 +02:00
|
|
|
}
|
2019-10-24 23:55:17 +02:00
|
|
|
if (!EnsurePolicy(de_policy, user_de_path)) return false;
|
2016-02-02 01:02:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-20 17:17:54 +02:00
|
|
|
if (flags & android::os::IVold::STORAGE_FLAG_CE) {
|
2016-04-15 04:45:16 +02:00
|
|
|
// CE_n key
|
2016-02-02 01:02:29 +01:00
|
|
|
auto system_ce_path = android::vold::BuildDataSystemCePath(user_id);
|
|
|
|
auto misc_ce_path = android::vold::BuildDataMiscCePath(user_id);
|
2018-01-22 20:25:29 +01:00
|
|
|
auto vendor_ce_path = android::vold::BuildDataVendorCePath(user_id);
|
2016-04-15 04:45:16 +02:00
|
|
|
auto media_ce_path = android::vold::BuildDataMediaCePath(volume_uuid, user_id);
|
|
|
|
auto user_ce_path = android::vold::BuildDataUserCePath(volume_uuid, user_id);
|
2016-02-02 01:02:29 +01:00
|
|
|
|
2017-10-09 19:55:21 +02:00
|
|
|
if (volume_uuid.empty()) {
|
2017-10-05 23:07:09 +02:00
|
|
|
if (!prepare_dir(system_ce_path, 0770, AID_SYSTEM, AID_SYSTEM)) return false;
|
|
|
|
if (!prepare_dir(misc_ce_path, 01771, AID_SYSTEM, AID_MISC)) return false;
|
2018-01-22 20:25:29 +01:00
|
|
|
if (!prepare_dir(vendor_ce_path, 0771, AID_ROOT, AID_ROOT)) return false;
|
2017-10-05 23:07:09 +02:00
|
|
|
}
|
2016-02-09 11:04:39 +01:00
|
|
|
if (!prepare_dir(media_ce_path, 0770, AID_MEDIA_RW, AID_MEDIA_RW)) return false;
|
2020-02-11 14:20:29 +01:00
|
|
|
// Setup quota project ID and inheritance policy
|
|
|
|
if (!IsFilesystemSupported("sdcardfs")) {
|
|
|
|
if (SetQuotaInherit(media_ce_path) != 0) return false;
|
|
|
|
if (SetQuotaProjectId(media_ce_path, multiuser_get_uid(user_id, AID_MEDIA_RW)) != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-09 11:04:39 +01:00
|
|
|
if (!prepare_dir(user_ce_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
|
2016-02-02 01:02:29 +01:00
|
|
|
|
2018-10-23 22:06:55 +02:00
|
|
|
if (fscrypt_is_native()) {
|
2019-10-24 23:55:17 +02:00
|
|
|
EncryptionPolicy ce_policy;
|
2017-10-09 19:55:21 +02:00
|
|
|
if (volume_uuid.empty()) {
|
2020-01-24 00:29:30 +01:00
|
|
|
if (!lookup_policy(s_ce_policies, user_id, &ce_policy)) return false;
|
2019-10-24 23:55:17 +02:00
|
|
|
if (!EnsurePolicy(ce_policy, system_ce_path)) return false;
|
|
|
|
if (!EnsurePolicy(ce_policy, misc_ce_path)) return false;
|
|
|
|
if (!EnsurePolicy(ce_policy, vendor_ce_path)) return false;
|
2017-10-10 01:35:51 +02:00
|
|
|
} else {
|
2019-10-24 23:55:17 +02:00
|
|
|
if (!read_or_create_volkey(misc_ce_path, volume_uuid, &ce_policy)) return false;
|
2017-10-05 23:07:09 +02:00
|
|
|
}
|
2019-10-24 23:55:17 +02:00
|
|
|
if (!EnsurePolicy(ce_policy, media_ce_path)) return false;
|
|
|
|
if (!EnsurePolicy(ce_policy, user_ce_path)) return false;
|
2016-02-02 01:02:29 +01:00
|
|
|
}
|
2017-10-13 22:49:50 +02:00
|
|
|
|
|
|
|
if (volume_uuid.empty()) {
|
2017-10-17 02:01:44 +02:00
|
|
|
// Now that credentials have been installed, we can run restorecon
|
|
|
|
// over these paths
|
|
|
|
// NOTE: these paths need to be kept in sync with libselinux
|
|
|
|
android::vold::RestoreconRecursive(system_ce_path);
|
2019-05-14 18:30:29 +02:00
|
|
|
android::vold::RestoreconRecursive(vendor_ce_path);
|
2017-10-17 02:01:44 +02:00
|
|
|
android::vold::RestoreconRecursive(misc_ce_path);
|
2017-10-13 22:49:50 +02:00
|
|
|
}
|
2015-11-09 02:56:23 +01:00
|
|
|
}
|
2017-10-20 17:17:54 +02:00
|
|
|
if (!prepare_subdirs("prepare", volume_uuid, user_id, flags)) return false;
|
2015-11-09 02:56:23 +01:00
|
|
|
|
2016-02-09 11:04:39 +01:00
|
|
|
return true;
|
2015-11-09 02:56:23 +01:00
|
|
|
}
|
2016-04-15 04:45:16 +02:00
|
|
|
|
2018-10-23 22:06:55 +02:00
|
|
|
bool fscrypt_destroy_user_storage(const std::string& volume_uuid, userid_t user_id, int flags) {
|
|
|
|
LOG(DEBUG) << "fscrypt_destroy_user_storage for volume " << escape_empty(volume_uuid)
|
2016-04-15 04:45:16 +02:00
|
|
|
<< ", user " << user_id << ", flags " << flags;
|
|
|
|
bool res = true;
|
|
|
|
|
2017-10-20 17:17:54 +02:00
|
|
|
res &= prepare_subdirs("destroy", volume_uuid, user_id, flags);
|
|
|
|
|
|
|
|
if (flags & android::os::IVold::STORAGE_FLAG_CE) {
|
2017-10-17 02:01:44 +02:00
|
|
|
// CE_n key
|
|
|
|
auto system_ce_path = android::vold::BuildDataSystemCePath(user_id);
|
|
|
|
auto misc_ce_path = android::vold::BuildDataMiscCePath(user_id);
|
2018-01-22 20:25:29 +01:00
|
|
|
auto vendor_ce_path = android::vold::BuildDataVendorCePath(user_id);
|
2017-10-17 02:01:44 +02:00
|
|
|
auto media_ce_path = android::vold::BuildDataMediaCePath(volume_uuid, user_id);
|
|
|
|
auto user_ce_path = android::vold::BuildDataUserCePath(volume_uuid, user_id);
|
|
|
|
|
|
|
|
res &= destroy_dir(media_ce_path);
|
|
|
|
res &= destroy_dir(user_ce_path);
|
|
|
|
if (volume_uuid.empty()) {
|
|
|
|
res &= destroy_dir(system_ce_path);
|
|
|
|
res &= destroy_dir(misc_ce_path);
|
2018-01-22 20:25:29 +01:00
|
|
|
res &= destroy_dir(vendor_ce_path);
|
2017-10-10 01:35:51 +02:00
|
|
|
} else {
|
2018-10-23 22:06:55 +02:00
|
|
|
if (fscrypt_is_native()) {
|
2017-10-10 01:35:51 +02:00
|
|
|
res &= destroy_volkey(misc_ce_path, volume_uuid);
|
|
|
|
}
|
2017-10-17 02:01:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-20 17:17:54 +02:00
|
|
|
if (flags & android::os::IVold::STORAGE_FLAG_DE) {
|
2016-04-15 04:45:16 +02:00
|
|
|
// DE_sys key
|
|
|
|
auto system_legacy_path = android::vold::BuildDataSystemLegacyPath(user_id);
|
|
|
|
auto misc_legacy_path = android::vold::BuildDataMiscLegacyPath(user_id);
|
|
|
|
auto profiles_de_path = android::vold::BuildDataProfilesDePath(user_id);
|
|
|
|
|
|
|
|
// DE_n key
|
|
|
|
auto system_de_path = android::vold::BuildDataSystemDePath(user_id);
|
|
|
|
auto misc_de_path = android::vold::BuildDataMiscDePath(user_id);
|
2018-01-22 20:25:29 +01:00
|
|
|
auto vendor_de_path = android::vold::BuildDataVendorDePath(user_id);
|
2016-04-15 04:45:16 +02:00
|
|
|
auto user_de_path = android::vold::BuildDataUserDePath(volume_uuid, user_id);
|
|
|
|
|
2017-10-17 02:01:44 +02:00
|
|
|
res &= destroy_dir(user_de_path);
|
2017-10-09 19:55:21 +02:00
|
|
|
if (volume_uuid.empty()) {
|
2016-04-15 04:45:16 +02:00
|
|
|
res &= destroy_dir(system_legacy_path);
|
|
|
|
#if MANAGE_MISC_DIRS
|
|
|
|
res &= destroy_dir(misc_legacy_path);
|
|
|
|
#endif
|
|
|
|
res &= destroy_dir(profiles_de_path);
|
|
|
|
res &= destroy_dir(system_de_path);
|
|
|
|
res &= destroy_dir(misc_de_path);
|
2018-01-22 20:25:29 +01:00
|
|
|
res &= destroy_dir(vendor_de_path);
|
2017-10-10 01:35:51 +02:00
|
|
|
} else {
|
2018-10-23 22:06:55 +02:00
|
|
|
if (fscrypt_is_native()) {
|
2017-10-10 01:35:51 +02:00
|
|
|
res &= destroy_volkey(misc_de_path, volume_uuid);
|
|
|
|
}
|
2016-04-15 04:45:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
2017-04-27 21:43:10 +02:00
|
|
|
|
2017-10-24 23:54:43 +02:00
|
|
|
static bool destroy_volume_keys(const std::string& directory_path, const std::string& volume_uuid) {
|
|
|
|
auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(directory_path.c_str()), closedir);
|
|
|
|
if (!dirp) {
|
|
|
|
PLOG(ERROR) << "Unable to open directory: " + directory_path;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool res = true;
|
|
|
|
for (;;) {
|
|
|
|
errno = 0;
|
|
|
|
auto const entry = readdir(dirp.get());
|
|
|
|
if (!entry) {
|
|
|
|
if (errno) {
|
|
|
|
PLOG(ERROR) << "Unable to read directory: " + directory_path;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (entry->d_type != DT_DIR || entry->d_name[0] == '.') {
|
|
|
|
LOG(DEBUG) << "Skipping non-user " << entry->d_name;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
res &= destroy_volkey(directory_path + "/" + entry->d_name, volume_uuid);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2018-10-23 22:06:55 +02:00
|
|
|
bool fscrypt_destroy_volume_keys(const std::string& volume_uuid) {
|
2017-10-24 23:54:43 +02:00
|
|
|
bool res = true;
|
2018-10-23 22:06:55 +02:00
|
|
|
LOG(DEBUG) << "fscrypt_destroy_volume_keys for volume " << escape_empty(volume_uuid);
|
2017-10-26 20:16:39 +02:00
|
|
|
auto secdiscardable_path = volume_secdiscardable_path(volume_uuid);
|
|
|
|
res &= android::vold::runSecdiscardSingle(secdiscardable_path);
|
2017-10-24 23:54:43 +02:00
|
|
|
res &= destroy_volume_keys("/data/misc_ce", volume_uuid);
|
|
|
|
res &= destroy_volume_keys("/data/misc_de", volume_uuid);
|
|
|
|
return res;
|
|
|
|
}
|