From 8a398788e40a780727709986dcdee3032d3dc813 Mon Sep 17 00:00:00 2001 From: Ashok Mutyala Date: Tue, 28 May 2024 09:31:44 +0000 Subject: Add length parameter to format /data f2fs filesystem With metadata encryption, if partition is wiped (or) cryptfs failed , it will do format without length which make partition size is grown large to occupy remaining space instead of restricting the Android defined partition size. Bug: 343159184 Test: Add length flag in fstab && fastboot erase userdata && fastboot reboot && df -h /data Change-Id: Iad041e960c9337ab1b9d51b115db3c5f3f1f75e2 Signed-off-by: Ashok Mutyala --- MetadataCrypt.cpp | 8 +++++--- MetadataCrypt.h | 2 +- VoldNativeService.cpp | 7 ++++--- VoldNativeService.h | 3 ++- binder/android/os/IVold.aidl | 2 +- fs/F2fs.cpp | 7 ++++++- fs/F2fs.h | 2 +- vdc.cpp | 16 +++++++++------- 8 files changed, 29 insertions(+), 18 deletions(-) diff --git a/MetadataCrypt.cpp b/MetadataCrypt.cpp index a1106fdd..38dd1126 100644 --- a/MetadataCrypt.cpp +++ b/MetadataCrypt.cpp @@ -247,10 +247,12 @@ static bool parse_options(const std::string& options_string, CryptoOptions* opti bool fscrypt_mount_metadata_encrypted(const std::string& blk_device, const std::string& mount_point, bool needs_encrypt, bool should_format, const std::string& fs_type, bool is_zoned, - const std::vector& user_devices) { + const std::vector& user_devices, + 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; + << fs_type << " block device: " << blk_device << " with zoned " << is_zoned + << " length: " << length; for (auto& device : user_devices) { LOG(DEBUG) << " - user devices: " << device; @@ -338,7 +340,7 @@ bool fscrypt_mount_metadata_encrypted(const std::string& blk_device, const std:: 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); + error = f2fs::Format(crypto_blkdev, is_zoned, crypto_user_blkdev, length); } else { LOG(ERROR) << "Unknown filesystem type: " << fs_type; return false; diff --git a/MetadataCrypt.h b/MetadataCrypt.h index 2c07a143..a0914433 100644 --- a/MetadataCrypt.h +++ b/MetadataCrypt.h @@ -29,7 +29,7 @@ void defaultkey_precreate_dm_device(); 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& user_devices); + const std::vector& user_devices, int64_t length); bool defaultkey_volume_keygen(KeyGeneration* gen); diff --git a/VoldNativeService.cpp b/VoldNativeService.cpp index a70639c6..98dec667 100644 --- a/VoldNativeService.cpp +++ b/VoldNativeService.cpp @@ -594,18 +594,19 @@ binder::Status VoldNativeService::mountFstab(const std::string& blkDevice, ACQUIRE_LOCK; return translateBool(fscrypt_mount_metadata_encrypted(blkDevice, mountPoint, false, false, - "null", isZoned, userDevices)); + "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& userDevices) { + const std::vector& userDevices, + int64_t length) { ENFORCE_SYSTEM_OR_ROOT; ACQUIRE_LOCK; return translateBool(fscrypt_mount_metadata_encrypted(blkDevice, mountPoint, true, shouldFormat, - fsType, isZoned, userDevices)); + fsType, isZoned, userDevices, length)); } binder::Status VoldNativeService::setStorageBindingSeed(const std::vector& seed) { diff --git a/VoldNativeService.h b/VoldNativeService.h index 619c7202..bd37ac76 100644 --- a/VoldNativeService.h +++ b/VoldNativeService.h @@ -107,9 +107,10 @@ class VoldNativeService : public BinderService, public os::Bn binder::Status initUser0(); binder::Status mountFstab(const std::string& blkDevice, const std::string& mountPoint, bool isZoned, const std::vector& userDevices); + binder::Status encryptFstab(const std::string& blkDevice, const std::string& mountPoint, bool shouldFormat, const std::string& fsType, bool isZoned, - const std::vector& userDevices); + const std::vector& userDevices, int64_t length); binder::Status setStorageBindingSeed(const std::vector& seed); diff --git a/binder/android/os/IVold.aidl b/binder/android/os/IVold.aidl index 919369b6..a8cce94a 100644 --- a/binder/android/os/IVold.aidl +++ b/binder/android/os/IVold.aidl @@ -84,7 +84,7 @@ interface IVold { 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); + void encryptFstab(@utf8InCpp String blkDevice, @utf8InCpp String mountPoint, boolean shouldFormat, @utf8InCpp String fsType, boolean isZoned, in @utf8InCpp String[] userDevices, long length); void setStorageBindingSeed(in byte[] seed); diff --git a/fs/F2fs.cpp b/fs/F2fs.cpp index 99afc32a..3cdf5740 100644 --- a/fs/F2fs.cpp +++ b/fs/F2fs.cpp @@ -72,8 +72,10 @@ status_t Mount(const std::string& source, const std::string& target) { } status_t Format(const std::string& source, bool is_zoned, - const std::vector& user_devices) { + const std::vector& user_devices, int64_t length) { std::vector cmd; + /* '-g android' parameter passed here which defaults the sector size to 4096 */ + static constexpr int kSectorSize = 4096; cmd.emplace_back(kMkfsPath); cmd.emplace_back("-f"); @@ -110,6 +112,9 @@ status_t Format(const std::string& source, bool is_zoned, cmd.emplace_back(source.c_str()); + if (length) { + cmd.emplace_back(std::to_string(length / kSectorSize).c_str()); + } return logwrap_fork_execvp(cmd.size(), cmd.data(), nullptr, false, LOG_KLOG, false, nullptr); } diff --git a/fs/F2fs.h b/fs/F2fs.h index a0218f26..73913102 100644 --- a/fs/F2fs.h +++ b/fs/F2fs.h @@ -31,7 +31,7 @@ bool IsSupported(); 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& user_devices); + const std::vector& user_devices, int64_t length = 0); } // namespace f2fs } // namespace vold diff --git a/vdc.cpp b/vdc.cpp index ee8cf9ee..9764b1af 100644 --- a/vdc.cpp +++ b/vdc.cpp @@ -109,13 +109,15 @@ static void encryptFstab(std::vector& args, if (isZoned == android::base::ParseBoolResult::kError) exit(EINVAL); std::vector userDevices = {}; - if (args[7] != "") { - userDevices = android::base::Split(args[7], " "); + int64_t length; + if (!android::base::ParseInt(args[7], &length)) exit(EINVAL); + if (args[8] != "") { + userDevices = android::base::Split(args[8], " "); } - checkStatus(args, - vold->encryptFstab(args[2], args[3], - shouldFormat == android::base::ParseBoolResult::kTrue, args[5], - isZoned == android::base::ParseBoolResult::kTrue, userDevices)); + checkStatus(args, vold->encryptFstab(args[2], args[3], + shouldFormat == android::base::ParseBoolResult::kTrue, + args[5], isZoned == android::base::ParseBoolResult::kTrue, + userDevices, length)); } int main(int argc, char** argv) { @@ -162,7 +164,7 @@ int main(int argc, char** argv) { 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() == 8) { + } else if (args[0] == "cryptfs" && args[1] == "encryptFstab" && args.size() == 9) { encryptFstab(args, vold); } else if (args[0] == "checkpoint" && args[1] == "supportsCheckpoint" && args.size() == 2) { bool supported = false; -- cgit v1.2.3