Skip to content

Commit 9d6e309

Browse files
gekyCruz Monrreal II
authored andcommitted
Extended mount to check all metadata-pairs
The most common issue with using littlefs in mbed-os is when users change from littlefs->FAT->littlefs (or with MBR or similar). When this corrupts the superblock, littlefs tries to fall back to the backup superblock. However, at this point in the time the old superblock may be very out-of-date and pointing to an incorrect filesystem. There's no complete solution to a malicious modification of the filesystem (short of checking all metadata+data, a very expensive operation), but we can at least expand our validation to all of the metadata for the filesystem. This at least catches the common issues with changing between different filesystems.
1 parent 1a8844e commit 9d6e309

File tree

1 file changed

+13
-1
lines changed
  • features/storage/filesystem/littlefs/littlefs

1 file changed

+13
-1
lines changed

features/storage/filesystem/littlefs/littlefs/lfs.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2206,6 +2206,10 @@ int lfs_mount(lfs_t *lfs, const struct lfs_config *cfg) {
22062206

22072207
lfs->root[0] = superblock.d.root[0];
22082208
lfs->root[1] = superblock.d.root[1];
2209+
if (lfs_paircmp(lfs->root, dir.d.tail) != 0) {
2210+
err = LFS_ERR_CORRUPT;
2211+
goto cleanup;
2212+
}
22092213
}
22102214

22112215
if (err || memcmp(superblock.d.magic, "littlefs", 8) != 0) {
@@ -2223,10 +2227,18 @@ int lfs_mount(lfs_t *lfs, const struct lfs_config *cfg) {
22232227
goto cleanup;
22242228
}
22252229

2230+
// verify that no metadata pairs are corrupt
2231+
while (!lfs_pairisnull(dir.d.tail)) {
2232+
err = lfs_dir_fetch(lfs, &dir, dir.d.tail);
2233+
if (err) {
2234+
goto cleanup;
2235+
}
2236+
}
2237+
2238+
// succuessfully mounted
22262239
return 0;
22272240

22282241
cleanup:
2229-
22302242
lfs_deinit(lfs);
22312243
return err;
22322244
}

0 commit comments

Comments
 (0)