Merge "[USB] Change default volume label for ExFat format mounts while formatting." into main
diff --git a/FsCrypt.cpp b/FsCrypt.cpp
index 3eb4599..4f42dd8 100644
--- a/FsCrypt.cpp
+++ b/FsCrypt.cpp
@@ -37,6 +37,7 @@
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/types.h>
+#include <sys/utsname.h>
#include <unistd.h>
#include <private/android_filesystem_config.h>
@@ -233,7 +234,20 @@
return false;
}
-static bool MightBeEmmcStorage(const std::string& blk_device) {
+// Checks whether the kernel definitely supports the sysfs files that describe the storage
+// hardware's inline encryption capabilities. They are supported in upstream 5.18 and later, and in
+// android14-5.15 and later (but not android13-5.15). For simplicity we just check for 5.18.
+static bool DoesKernelSupportBlkCryptoSysfsFiles() {
+ struct utsname uts;
+ unsigned int major = 0, minor = 0;
+ if (uname(&uts) != 0 || sscanf(uts.release, "%u.%u", &major, &minor) != 2) {
+ return true; // This should never happen; assume new rather than old.
+ }
+ return major > 5 || (major == 5 && minor >= 18);
+}
+
+// Checks whether the storage hardware might support only 32-bit data unit numbers.
+static bool DoesHardwareSupportOnly32DunBits(const std::string& blk_device) {
// Handle symlinks.
std::string real_path;
if (!Realpath(blk_device, &real_path)) {
@@ -249,15 +263,53 @@
}
// Now we should have the "real" block device.
- LOG(DEBUG) << "MightBeEmmcStorage(): blk_device = " << blk_device
- << ", real_path=" << real_path;
std::string name = Basename(real_path);
- return StartsWith(name, "mmcblk") ||
- // virtio devices may provide inline encryption support that is
- // backed by eMMC inline encryption on the host, thus inheriting the
- // DUN size limitation. So virtio devices must be allowed here too.
- // TODO(b/207390665): check the maximum DUN size directly instead.
- StartsWith(name, "vd");
+
+ // If possible, do the check precisely via sysfs.
+ // Exclude older devices, just in case they are broken by doing the check correctly...
+ if (GetFirstApiLevel() >= __ANDROID_API_V__) {
+ std::string sysfs_path = "/sys/class/block/" + name + "/queue/crypto/max_dun_bits";
+ if (!android::vold::pathExists(sysfs_path)) {
+ // For a partition, "queue" is in the parent directory which represents the disk.
+ sysfs_path = "/sys/class/block/" + name + "/../queue/crypto/max_dun_bits";
+ }
+ if (android::vold::pathExists(sysfs_path)) {
+ std::string max_dun_bits;
+ if (!android::base::ReadFileToString(sysfs_path, &max_dun_bits)) {
+ PLOG(ERROR) << "Error reading " << sysfs_path;
+ return false;
+ }
+ max_dun_bits = android::base::Trim(max_dun_bits);
+ if (max_dun_bits != "32") {
+ LOG(ERROR) << sysfs_path << " = " << max_dun_bits;
+ // In this case, using emmc_optimized is not appropriate because the hardware
+ // supports inline encryption but does not have the 32-bit DUN limit.
+ return false;
+ }
+ LOG(DEBUG) << sysfs_path << " = " << max_dun_bits;
+ return true;
+ }
+ if (DoesKernelSupportBlkCryptoSysfsFiles()) {
+ // In this case, using emmc_optimized is not appropriate because the hardware does not
+ // support inline encryption.
+ LOG(ERROR) << sysfs_path << " does not exist";
+ return false;
+ }
+ // In this case, the kernel might be too old to support the sysfs files.
+ }
+
+ // Fallback method for older kernels that don't have the crypto capabilities in sysfs. The
+ // 32-bit DUN limit is only known to exist on eMMC storage, and also on virtio storage that
+ // inherits the limit from eMMC on the host. So allow either of those storage types. Note that
+ // this can be overly lenient compared to actually checking max_dun_bits.
+ if (StartsWith(name, "mmcblk") || StartsWith(name, "vd")) {
+ LOG(DEBUG) << __func__ << "(): << blk_device = " << blk_device
+ << ", real_path = " << real_path;
+ return true;
+ }
+ // Log at ERROR level here so that it shows up in the kernel log.
+ LOG(ERROR) << __func__ << "(): << blk_device = " << blk_device << ", real_path = " << real_path;
+ return false;
}
// Sets s_data_options to the file encryption options for the /data filesystem.
@@ -273,9 +325,10 @@
return false;
}
if ((s_data_options.flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32) &&
- !MightBeEmmcStorage(entry->blk_device)) {
- LOG(ERROR) << "The emmc_optimized encryption flag is only allowed on eMMC storage. Remove "
- "this flag from the device's fstab";
+ !DoesHardwareSupportOnly32DunBits(entry->blk_device)) {
+ // This would unnecessarily reduce security and not be compliant with the CDD.
+ LOG(ERROR) << "The emmc_optimized encryption flag is only allowed on hardware limited to "
+ "32-bit DUNs. Remove this flag from the device's fstab";
return false;
}
return true;
diff --git a/Keystore.cpp b/Keystore.cpp
index 6040f2d..fd8a887 100644
--- a/Keystore.cpp
+++ b/Keystore.cpp
@@ -130,8 +130,8 @@
bool Keystore::generateKey(const km::AuthorizationSet& inParams, std::string* key) {
ks2::KeyDescriptor in_key = {
.domain = ks2::Domain::BLOB,
- .alias = std::nullopt,
.nspace = VOLD_NAMESPACE,
+ .alias = std::nullopt,
.blob = std::nullopt,
};
ks2::KeyMetadata keyMetadata;
@@ -154,8 +154,8 @@
bool ret = false;
ks2::KeyDescriptor storageKey = {
.domain = ks2::Domain::BLOB,
- .alias = std::nullopt,
.nspace = VOLD_NAMESPACE,
+ .alias = std::nullopt,
};
storageKey.blob = std::make_optional<std::vector<uint8_t>>(ksKey.begin(), ksKey.end());
ks2::EphemeralStorageKeyResponse ephemeral_key_response;
@@ -184,8 +184,8 @@
bool Keystore::deleteKey(const std::string& key) {
ks2::KeyDescriptor keyDesc = {
.domain = ks2::Domain::BLOB,
- .alias = std::nullopt,
.nspace = VOLD_NAMESPACE,
+ .alias = std::nullopt,
};
keyDesc.blob =
std::optional<std::vector<uint8_t>>(std::vector<uint8_t>(key.begin(), key.end()));
@@ -198,8 +198,8 @@
km::AuthorizationSet* outParams) {
ks2::KeyDescriptor keyDesc = {
.domain = ks2::Domain::BLOB,
- .alias = std::nullopt,
.nspace = VOLD_NAMESPACE,
+ .alias = std::nullopt,
};
keyDesc.blob =
std::optional<std::vector<uint8_t>>(std::vector<uint8_t>(key.begin(), key.end()));
@@ -224,7 +224,7 @@
}
void Keystore::earlyBootEnded() {
- ::ndk::SpAIBinder binder(AServiceManager_getService(maintenance_service_name));
+ ::ndk::SpAIBinder binder(AServiceManager_waitForService(maintenance_service_name));
auto maint_service = ks2_maint::IKeystoreMaintenance::fromBinder(binder);
if (!maint_service) {
@@ -237,7 +237,7 @@
}
void Keystore::deleteAllKeys() {
- ::ndk::SpAIBinder binder(AServiceManager_getService(maintenance_service_name));
+ ::ndk::SpAIBinder binder(AServiceManager_waitForService(maintenance_service_name));
auto maint_service = ks2_maint::IKeystoreMaintenance::fromBinder(binder);
if (!maint_service) {
diff --git a/MetadataCrypt.cpp b/MetadataCrypt.cpp
index 8d83541..2fa0cff 100644
--- a/MetadataCrypt.cpp
+++ b/MetadataCrypt.cpp
@@ -291,7 +291,7 @@
bool needs_encrypt, bool should_format,
const std::string& fs_type, bool is_zoned,
const std::vector<std::string>& user_devices,
- int64_t length) {
+ const std::vector<bool>& device_aliased, int64_t length) {
LOG(DEBUG) << "fscrypt_mount_metadata_encrypted: " << mount_point
<< " encrypt: " << needs_encrypt << " format: " << should_format << " with "
<< fs_type << " block device: " << blk_device << " with zoned " << is_zoned
@@ -385,7 +385,8 @@
if (fs_type == "ext4") {
error = ext4::Format(crypto_blkdev, 0, mount_point);
} else if (fs_type == "f2fs") {
- error = f2fs::Format(crypto_blkdev, is_zoned, crypto_user_blkdev, length);
+ error = f2fs::Format(crypto_blkdev, is_zoned, crypto_user_blkdev, device_aliased,
+ length);
} else {
LOG(ERROR) << "Unknown filesystem type: " << fs_type;
return false;
diff --git a/MetadataCrypt.h b/MetadataCrypt.h
index a091443..6c46237 100644
--- a/MetadataCrypt.h
+++ b/MetadataCrypt.h
@@ -29,7 +29,8 @@
bool fscrypt_mount_metadata_encrypted(const std::string& block_device,
const std::string& mount_point, bool needs_encrypt,
bool should_format, const std::string& fs_type, bool is_zoned,
- const std::vector<std::string>& user_devices, int64_t length);
+ const std::vector<std::string>& user_devices,
+ const std::vector<bool>& device_aliased, int64_t length);
bool defaultkey_volume_keygen(KeyGeneration* gen);
diff --git a/VoldNativeService.cpp b/VoldNativeService.cpp
index 98dec66..3784487 100644
--- a/VoldNativeService.cpp
+++ b/VoldNativeService.cpp
@@ -594,19 +594,21 @@
ACQUIRE_LOCK;
return translateBool(fscrypt_mount_metadata_encrypted(blkDevice, mountPoint, false, false,
- "null", isZoned, userDevices, 0));
+ "null", isZoned, userDevices, {}, 0));
}
binder::Status VoldNativeService::encryptFstab(const std::string& blkDevice,
const std::string& mountPoint, bool shouldFormat,
const std::string& fsType, bool isZoned,
const std::vector<std::string>& userDevices,
+ const std::vector<bool>& deviceAliased,
int64_t length) {
ENFORCE_SYSTEM_OR_ROOT;
ACQUIRE_LOCK;
return translateBool(fscrypt_mount_metadata_encrypted(blkDevice, mountPoint, true, shouldFormat,
- fsType, isZoned, userDevices, length));
+ fsType, isZoned, userDevices,
+ deviceAliased, length));
}
binder::Status VoldNativeService::setStorageBindingSeed(const std::vector<uint8_t>& seed) {
diff --git a/VoldNativeService.h b/VoldNativeService.h
index bd37ac7..a5253c0 100644
--- a/VoldNativeService.h
+++ b/VoldNativeService.h
@@ -110,7 +110,8 @@
binder::Status encryptFstab(const std::string& blkDevice, const std::string& mountPoint,
bool shouldFormat, const std::string& fsType, bool isZoned,
- const std::vector<std::string>& userDevices, int64_t length);
+ const std::vector<std::string>& userDevices,
+ const std::vector<bool>& deviceAliased, int64_t length);
binder::Status setStorageBindingSeed(const std::vector<uint8_t>& seed);
diff --git a/VolumeManager.cpp b/VolumeManager.cpp
index 7c2b2f2..a7c87a3 100644
--- a/VolumeManager.cpp
+++ b/VolumeManager.cpp
@@ -1036,7 +1036,8 @@
!StartsWith(test, "/mnt/scratch") &&
#endif
!StartsWith(test, "/mnt/vendor") && !StartsWith(test, "/mnt/product") &&
- !StartsWith(test, "/mnt/installer") && !StartsWith(test, "/mnt/androidwritable")) ||
+ !StartsWith(test, "/mnt/installer") && !StartsWith(test, "/mnt/androidwritable") &&
+ !StartsWith(test, "/mnt/vm")) ||
StartsWith(test, "/storage/")) {
toUnmount.push_front(test);
}
diff --git a/binder/android/os/IVold.aidl b/binder/android/os/IVold.aidl
index a8cce94..810fdad 100644
--- a/binder/android/os/IVold.aidl
+++ b/binder/android/os/IVold.aidl
@@ -84,7 +84,7 @@
void initUser0();
void mountFstab(@utf8InCpp String blkDevice, @utf8InCpp String mountPoint, boolean isZoned, in @utf8InCpp String[] userDevices);
- void encryptFstab(@utf8InCpp String blkDevice, @utf8InCpp String mountPoint, boolean shouldFormat, @utf8InCpp String fsType, boolean isZoned, in @utf8InCpp String[] userDevices, long length);
+ void encryptFstab(@utf8InCpp String blkDevice, @utf8InCpp String mountPoint, boolean shouldFormat, @utf8InCpp String fsType, boolean isZoned, in @utf8InCpp String[] userDevices, in boolean[] deviceAliased, long length);
void setStorageBindingSeed(in byte[] seed);
diff --git a/fs/F2fs.cpp b/fs/F2fs.cpp
index 3cdf574..4cdddec 100644
--- a/fs/F2fs.cpp
+++ b/fs/F2fs.cpp
@@ -27,6 +27,7 @@
#include <vector>
#include <sys/mount.h>
+#include <filesystem>
using android::base::StringPrintf;
@@ -72,7 +73,8 @@
}
status_t Format(const std::string& source, bool is_zoned,
- const std::vector<std::string>& user_devices, int64_t length) {
+ const std::vector<std::string>& user_devices,
+ const std::vector<bool>& device_aliased, int64_t length) {
std::vector<char const*> cmd;
/* '-g android' parameter passed here which defaults the sector size to 4096 */
static constexpr int kSectorSize = 4096;
@@ -102,9 +104,15 @@
if (is_zoned) {
cmd.emplace_back("-m");
}
- for (auto& device : user_devices) {
+ for (size_t i = 0; i < user_devices.size(); i++) {
+ std::string device_name = user_devices[i];
+
cmd.emplace_back("-c");
- cmd.emplace_back(device.c_str());
+ if (device_aliased[i]) {
+ std::filesystem::path path = device_name;
+ device_name += "@" + path.filename().string();
+ }
+ cmd.emplace_back(device_name.c_str());
}
std::string block_size = std::to_string(getpagesize());
cmd.emplace_back("-b");
diff --git a/fs/F2fs.h b/fs/F2fs.h
index 7391310..4193c87 100644
--- a/fs/F2fs.h
+++ b/fs/F2fs.h
@@ -31,7 +31,8 @@
status_t Check(const std::string& source);
status_t Mount(const std::string& source, const std::string& target);
status_t Format(const std::string& source, const bool is_zoned,
- const std::vector<std::string>& user_devices, int64_t length = 0);
+ const std::vector<std::string>& user_devices,
+ const std::vector<bool>& device_aliased, int64_t length = 0);
} // namespace f2fs
} // namespace vold
diff --git a/model/PrivateVolume.cpp b/model/PrivateVolume.cpp
index bb52647..b2128a8 100644
--- a/model/PrivateVolume.cpp
+++ b/model/PrivateVolume.cpp
@@ -234,7 +234,7 @@
return -EIO;
}
} else if (resolvedFsType == "f2fs") {
- if (f2fs::Format(mDmDevPath, false, {})) {
+ if (f2fs::Format(mDmDevPath, false, {}, {})) {
PLOG(ERROR) << getId() << " failed to format";
return -EIO;
}
diff --git a/vdc.cpp b/vdc.cpp
index 9764b1a..400bc5d 100644
--- a/vdc.cpp
+++ b/vdc.cpp
@@ -114,10 +114,22 @@
if (args[8] != "") {
userDevices = android::base::Split(args[8], " ");
}
+ std::vector<std::string> deviceAliasedStr = {};
+ std::vector<bool> deviceAliased = {};
+ if (args[9] != "") {
+ deviceAliasedStr = android::base::Split(args[9], " ");
+ for (auto aliased : deviceAliasedStr) {
+ if (aliased == "0") {
+ deviceAliased.push_back(false);
+ } else {
+ deviceAliased.push_back(true);
+ }
+ }
+ }
checkStatus(args, vold->encryptFstab(args[2], args[3],
shouldFormat == android::base::ParseBoolResult::kTrue,
args[5], isZoned == android::base::ParseBoolResult::kTrue,
- userDevices, length));
+ userDevices, deviceAliased, length));
}
int main(int argc, char** argv) {
@@ -164,7 +176,7 @@
bindkeys(args, vold);
} else if (args[0] == "cryptfs" && args[1] == "mountFstab" && args.size() == 6) {
mountFstab(args, vold);
- } else if (args[0] == "cryptfs" && args[1] == "encryptFstab" && args.size() == 9) {
+ } else if (args[0] == "cryptfs" && args[1] == "encryptFstab" && args.size() == 10) {
encryptFstab(args, vold);
} else if (args[0] == "checkpoint" && args[1] == "supportsCheckpoint" && args.size() == 2) {
bool supported = false;