* Copyright (c) 2015 Google, Inc.
*/
+#define TAG "ext4_utils"
+
#include <dirent.h>
#include <errno.h>
#include <string.h>
#include <sys/syscall.h>
#include <sys/stat.h>
-// ext4enc:TODO Use include paths
-#include "../../core/init/log.h"
+#include <cutils/klog.h>
#include "ext4_crypt.h"
static int is_path_valid(const char *path)
{
if (access(path, W_OK)) {
- ERROR("Can't access %s: %s\n",strerror(errno), path);
+ KLOG_ERROR(TAG, "Can't access %s: %s\n",strerror(errno), path);
return 0;
}
ssize_t ret;
if (!is_keyref_valid(policy)) {
- ERROR("Policy has invalid format.\n");
+ KLOG_ERROR(TAG, "Policy has invalid format.\n");
return -EINVAL;
}
stat(directory, &st);
if (!S_ISDIR(st.st_mode)) {
- ERROR("Can only set policy on a directory (%s)\n", directory);
+ KLOG_ERROR(TAG, "Can only set policy on a directory (%s)\n", directory);
return -EINVAL;
}
if (!is_dir_empty(directory)) {
- ERROR("Can only set policy on an empty directory (%s)\n", directory);
+ KLOG_ERROR(TAG, "Can only set policy on an empty directory (%s)\n", directory);
return -EINVAL;
}
strlen(policy), 0);
if (ret) {
- ERROR("Failed to set encryption policy for %s: %s\n",
- directory, strerror(errno));
+ KLOG_ERROR(TAG, "Failed to set encryption policy for %s: %s\n",
+ directory, strerror(errno));
return -EINVAL;
}
- INFO("Encryption policy for %s is set to %s\n", directory, policy);
+ KLOG_INFO(TAG, "Encryption policy for %s is set to %s\n", directory, policy);
return 0;
}
+#define TAG "ext4_utils"
+
#include "ext4_crypt.h"
#include <string>
#include <errno.h>
#include <sys/mount.h>
-#include <cutils/properties.h>
-// ext4enc:TODO Use include paths
-#include "../../core/init/log.h"
+#include <cutils/klog.h>
+#include <cutils/properties.h>
// ext4enc::TODO remove this duplicated const
static const std::string unencrypted_path = "/unencrypted";
{
auto full_path = std::string() + path + unencrypted_path;
if (!std::ifstream(full_path + "/key")) {
- INFO("No master key, so not ext4enc\n");
+ KLOG_INFO(TAG, "No master key, so not ext4enc\n");
return -1;
}
std::ifstream type(full_path + "/type");
if (!type) {
- INFO("No password type so default\n");
+ KLOG_INFO(TAG, "No password type so default\n");
return 1; // Default
}
int value = 0;
type >> value;
- INFO("Password type is %d\n", value);
+ KLOG_INFO(TAG, "Password type is %d\n", value);
return value;
}
int e4crypt_crypto_complete(const char* path)
{
- INFO("ext4 crypto complete called on %s\n", path);
+ KLOG_INFO(TAG, "ext4 crypto complete called on %s\n", path);
auto full_path = std::string() + path + unencrypted_path;
if (!std::ifstream(full_path + "/key")) {
- INFO("No master key, so not ext4enc\n");
+ KLOG_INFO(TAG, "No master key, so not ext4enc\n");
return -1;
}
{
auto full_path = std::string() + path + unencrypted_path;
if (!std::ifstream(full_path + "/key")) {
- INFO("No master key, so not ext4enc\n");
+ KLOG_INFO(TAG, "No master key, so not ext4enc\n");
return -1;
}
{
int rc = 0;
- INFO("ext4 restart called on %s\n", path);
+ KLOG_INFO(TAG, "ext4 restart called on %s\n", path);
property_set("vold.decrypt", "trigger_reset_main");
- INFO("Just asked init to shut down class main\n");
+ KLOG_INFO(TAG, "Just asked init to shut down class main\n");
sleep(2);
std::string tmp_path = std::string() + path + "/tmp_mnt";
// ext4enc:TODO add retry logic
rc = umount(tmp_path.c_str());
if (rc) {
- ERROR("umount %s failed with rc %d, msg %s\n",
- tmp_path.c_str(), rc, strerror(errno));
+ KLOG_ERROR(TAG, "umount %s failed with rc %d, msg %s\n",
+ tmp_path.c_str(), rc, strerror(errno));
return rc;
}
// ext4enc:TODO add retry logic
rc = umount(path);
if (rc) {
- ERROR("umount %s failed with rc %d, msg %s\n",
- path, rc, strerror(errno));
+ KLOG_ERROR(TAG, "umount %s failed with rc %d, msg %s\n",
+ path, rc, strerror(errno));
return rc;
}
+#define TAG "ext4_utils"
+
#include "ext4_crypt.h"
#include <string>
#include <iomanip>
#include <sstream>
-#include <sys/mount.h>
-
#include <errno.h>
+#include <sys/mount.h>
#include <sys/stat.h>
+
+#include <cutils/klog.h>
#include <cutils/properties.h>
#include <cutils/sockets.h>
-// ext4enc:TODO Use include paths
-#include "../../core/init/log.h"
#include "../../core/init/util.h"
#include "ext2fs/ext2_fs.h"
static std::string vold_command(std::string const& command)
{
- INFO("Running command %s\n", command.c_str());
+ KLOG_INFO(TAG, "Running command %s\n", command.c_str());
int sock = socket_local_client("vold",
ANDROID_SOCKET_NAMESPACE_RESERVED,
SOCK_STREAM);
if (sock < 0) {
- INFO("Cannot open vold, failing command\n");
+ KLOG_INFO(TAG, "Cannot open vold, failing command\n");
return "";
}
// framework is down, so this is (mostly) OK.
std::string actual_command = arbitrary_sequence_number + " " + command;
if (write(sock, actual_command.c_str(), actual_command.size() + 1) < 0) {
- ERROR("Cannot write command\n");
+ KLOG_ERROR(TAG, "Cannot write command\n");
return "";
}
int rc = select(sock + 1, &read_fds, NULL, NULL, &to);
if (rc < 0) {
- ERROR("Error in select %s\n", strerror(errno));
+ KLOG_ERROR(TAG, "Error in select %s\n", strerror(errno));
return "";
} else if (!rc) {
- ERROR("Timeout\n");
+ KLOG_ERROR(TAG, "Timeout\n");
return "";
} else if (FD_ISSET(sock, &read_fds)) {
char buffer[4096];
rc = read(sock, buffer, sizeof(buffer));
if (rc <= 0) {
if (rc == 0) {
- ERROR("Lost connection to Vold - did it crash?\n");
+ KLOG_ERROR(TAG, "Lost connection to Vold - did it crash?\n");
} else {
- ERROR("Error reading data (%s)\n", strerror(errno));
+ KLOG_ERROR(TAG, "Error reading data (%s)\n", strerror(errno));
}
return "";
}
int e4crypt_create_device_key(const char* dir)
{
// Make sure folder exists. Use make_dir to set selinux permissions.
- INFO("Creating test device key\n");
+ KLOG_INFO(TAG, "Creating test device key\n");
std::string path = std::string() + dir + unencrypted_path;
if (make_dir(path.c_str(), 0700) && errno != EEXIST) {
- ERROR("Failed to create %s with error %s\n",
- path.c_str(), strerror(errno));
+ KLOG_ERROR(TAG, "Failed to create %s with error %s\n",
+ path.c_str(), strerror(errno));
return -1;
}
// Create new key if it doesn't
std::ofstream new_key(key_path.c_str(), std::ofstream::binary);
if (!new_key) {
- ERROR("Failed to open %s\n", key_path.c_str());
+ KLOG_ERROR(TAG, "Failed to open %s\n", key_path.c_str());
return -1;
}
std::ifstream urandom("/dev/urandom", std::ifstream::binary);
if (!urandom) {
- ERROR("Failed to open /dev/urandom\n");
+ KLOG_ERROR(TAG, "Failed to open /dev/urandom\n");
return -1;
}
char key_material[32];
urandom.read(key_material, 32);
if (!urandom) {
- ERROR("Failed to read random bytes\n");
+ KLOG_ERROR(TAG, "Failed to read random bytes\n");
return -1;
}
new_key.write(key_material, 32);
if (!new_key) {
- ERROR("Failed to write key material");
+ KLOG_ERROR(TAG, "Failed to write key material");
return -1;
}
}
KEY_SPEC_SESSION_KEYRING);
if (device_keyring == -1) {
- ERROR("Failed to create keyring\n");
+ KLOG_ERROR(TAG, "Failed to create keyring\n");
return -1;
}
- INFO("Keyring created wth id %d in process %d\n", device_keyring, getpid());
+ KLOG_INFO(TAG, "Keyring created wth id %d in process %d\n", device_keyring, getpid());
// ext4enc:TODO set correct permissions
long result = keyctl_setperm(device_keyring, 0x3f3f3f3f);
if (result) {
- ERROR("KEYCTL_SETPERM failed with error %ld\n", result);
+ KLOG_ERROR(TAG, "KEYCTL_SETPERM failed with error %ld\n", result);
return -1;
}
std::string key_path = path + "/key";
std::ifstream key(key_path.c_str(), std::ifstream::binary);
if (!key.good()) {
- ERROR("Failed to open key %s\n", key_path.c_str());
+ KLOG_ERROR(TAG, "Failed to open key %s\n", key_path.c_str());
return -1;
}
key.read(keyblob, sizeof(keyblob));
std::streamsize keyblob_size = key.gcount();
if (keyblob_size <= 0) {
- ERROR("Failed to read key data\n");
+ KLOG_ERROR(TAG, "Failed to read key data\n");
return -1;
}
std::string bit;
i >> bit;
if (bit != "200") {
- ERROR("Expecting 200\n");
+ KLOG_ERROR(TAG, "Expecting 200\n");
return -1;
}
i >> bit;
if (bit != arbitrary_sequence_number) {
- ERROR("Expecting %s\n", arbitrary_sequence_number.c_str());
+ KLOG_ERROR(TAG, "Expecting %s\n", arbitrary_sequence_number.c_str());
return -1;
}
i >> bit;
if (bit != "{{sensitive}}") {
- INFO("Not encrypted\n");
+ KLOG_INFO(TAG, "Not encrypted\n");
return -1;
}
device_keyring);
if (key_id == -1) {
- ERROR("Failed to insert key into keyring with error %s\n",
- strerror(errno));
+ KLOG_ERROR(TAG, "Failed to insert key into keyring with error %s\n",
+ strerror(errno));
return -1;
}
- INFO("Added key %d to keyring %d in process %d\n",
- key_id, device_keyring, getpid());
+ KLOG_INFO(TAG, "Added key %d to keyring %d in process %d\n",
+ key_id, device_keyring, getpid());
// ext4enc:TODO set correct permissions
long result = keyctl_setperm(key_id, 0x3f3f3f3f);
if (result) {
- ERROR("KEYCTL_SETPERM failed with error %ld\n", result);
+ KLOG_ERROR(TAG, "KEYCTL_SETPERM failed with error %ld\n", result);
return -1;
}
std::ifstream ref_file("/data/unencrypted/ref");
if (!ref_file) {
- ERROR("Cannot open key reference file\n");
+ KLOG_ERROR(TAG, "Cannot open key reference file\n");
return -1;
}
std::string ref;
std::getline(ref_file, ref);
std::string policy = std::string() + keyring + "." + ref;
- INFO("Setting poliy %s\n", policy.c_str());
+ KLOG_INFO(TAG, "Setting policy %s\n", policy.c_str());
if (do_policy_set(dir, policy.c_str())) {
- ERROR("Setting policy on %s failed!", dir);
+ KLOG_ERROR(TAG, "Setting policy on %s failed!", dir);
return -1;
}