Fix internal storage bugs with restoring U2F keys

This commit is contained in:
Willy-JL
2023-04-28 19:50:04 +01:00
parent 8a5d88fc9a
commit 7adb7e2ded

View File

@@ -193,42 +193,64 @@ static void storage_int_lfs_mount(LFSData* lfs_data, StorageData* storage) {
was_fingerprint_outdated; was_fingerprint_outdated;
if(need_format) { if(need_format) {
err = lfs_mount(lfs, &lfs_data->config);
if(err == 0) {
FURI_LOG_I(TAG, "Factory reset: Mounted for backup");
// Backup U2F keys // Backup U2F keys
lfs_file_t file; lfs_file_t file;
lfs_file_open(lfs, &file, ".cnt.u2f", LFS_O_RDWR | LFS_O_CREAT); uint8_t* cnt = NULL;
uint8_t cnt[file.ctz.size]; uint32_t cnt_size;
lfs_file_read(lfs, &file, cnt, sizeof(cnt)); uint8_t* key = NULL;
lfs_file_close(lfs, &file); uint32_t key_size;
lfs_file_open(lfs, &file, ".key.u2f", LFS_O_RDWR | LFS_O_CREAT); if(lfs_mount(lfs, &lfs_data->config) == 0) {
uint8_t key[file.ctz.size]; FURI_LOG_I(TAG, "Factory reset: Mounted for backup");
lfs_file_read(lfs, &file, key, sizeof(key));
lfs_file_close(lfs, &file);
err = lfs_unmount(lfs); if(lfs_file_open(lfs, &file, ".cnt.u2f", LFS_O_RDONLY) == 0) {
if(err == 0) { cnt_size = file.ctz.size;
cnt = malloc(cnt_size);
if(lfs_file_read(lfs, &file, cnt, cnt_size) != (int32_t)cnt_size) {
free(cnt);
cnt = NULL;
}
lfs_file_close(lfs, &file);
if(lfs_file_open(lfs, &file, ".key.u2f", LFS_O_RDONLY) == 0) {
key_size = file.ctz.size;
key = malloc(key_size);
if(lfs_file_read(lfs, &file, key, key_size) != (int32_t)key_size) {
free(key);
key = NULL;
}
lfs_file_close(lfs, &file);
}
}
if(lfs_unmount(lfs) == 0) {
FURI_LOG_E(TAG, "Factory reset: Unmounted after backup"); FURI_LOG_E(TAG, "Factory reset: Unmounted after backup");
} else {
FURI_LOG_E(TAG, "Factory reset: Unmount after backup failed");
}
} else {
FURI_LOG_E(TAG, "Factory reset: Mount for backup failed");
}
// Format storage // Format storage
err = lfs_format(lfs, &lfs_data->config); if(lfs_format(lfs, &lfs_data->config) == 0) {
if(err == 0) {
FURI_LOG_I(TAG, "Factory reset: Format successful, trying to mount"); FURI_LOG_I(TAG, "Factory reset: Format successful, trying to mount");
furi_hal_rtc_reset_flag(FuriHalRtcFlagFactoryReset); furi_hal_rtc_reset_flag(FuriHalRtcFlagFactoryReset);
err = lfs_mount(lfs, &lfs_data->config); if(lfs_mount(lfs, &lfs_data->config) == 0) {
if(err == 0) {
FURI_LOG_I(TAG, "Factory reset: Mounted"); FURI_LOG_I(TAG, "Factory reset: Mounted");
storage->status = StorageStatusOK;
// Restore U2F keys // Restore U2F keys
lfs_file_open(lfs, &file, ".cnt.u2f", LFS_O_RDWR | LFS_O_CREAT); if(cnt != NULL && key != NULL) {
lfs_file_write(lfs, &file, cnt, sizeof(cnt)); if(lfs_file_open(lfs, &file, ".cnt.u2f", LFS_O_WRONLY | LFS_O_CREAT) == 0) {
lfs_file_write(lfs, &file, cnt, cnt_size);
lfs_file_close(lfs, &file); lfs_file_close(lfs, &file);
lfs_file_open(lfs, &file, ".key.u2f", LFS_O_RDWR | LFS_O_CREAT); if(lfs_file_open(lfs, &file, ".key.u2f", LFS_O_WRONLY | LFS_O_CREAT) == 0) {
lfs_file_write(lfs, &file, key, sizeof(key)); lfs_file_write(lfs, &file, key, key_size);
lfs_file_close(lfs, &file); lfs_file_close(lfs, &file);
}
storage->status = StorageStatusOK; }
}
if(cnt != NULL) free(cnt);
if(key != NULL) free(key);
} else { } else {
FURI_LOG_E(TAG, "Factory reset: Mount after format failed"); FURI_LOG_E(TAG, "Factory reset: Mount after format failed");
storage->status = StorageStatusNotMounted; storage->status = StorageStatusNotMounted;
@@ -237,14 +259,6 @@ static void storage_int_lfs_mount(LFSData* lfs_data, StorageData* storage) {
FURI_LOG_E(TAG, "Factory reset: Format failed"); FURI_LOG_E(TAG, "Factory reset: Format failed");
storage->status = StorageStatusNoFS; storage->status = StorageStatusNoFS;
} }
} else {
FURI_LOG_E(TAG, "Factory reset: Unmount after backup failed");
storage->status = StorageStatusNotMounted;
}
} else {
FURI_LOG_E(TAG, "Factory reset: Mount for backup failed");
storage->status = StorageStatusNotMounted;
}
} else { } else {
// Normal // Normal
err = lfs_mount(lfs, &lfs_data->config); err = lfs_mount(lfs, &lfs_data->config);