Skip to contentSkip to conversation

fs: ext4: Prevent infinite loop in ext4fs_iterate_dir

PATCH In review Thomas Fitzsimmons 3 participants 3 messages Nov 18, 2015 – Nov 23, 2015 ext4 file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fs: ext4: Prevent infinite loop in ext4fs_iterate_dir

If the ext3 journal gets out of sync with what is written on disk, for example because of an unexpected power cut, ext4fs_read_file can return an all-zero directory entry. In that case, ext4fs_iterate_dir would infinite loop.

This patch detects when a directory entry's direntlen member is 0 and returns a failure status, which breaks out of the infinite loop. As a result, U-Boot will not find files that may subsequently be recovered when the journal is replayed.

This is better behaviour than hanging in an infinite loop, but as a further improvement maybe U-Boot could interpret the ext3 journal and actually find the unsynced entries.

---
 fs/ext4/ext4_common.c | 5 +++++
 1 file changed, 5 insertions(+)
fs/ext4/ext4_common.c +5 -0
index 727a2f7..e73223a 100644
@@ -1920,6 +1920,11 @@ int ext4fs_iterate_dir(struct ext2fs_node *dir, char *name,
 		if (status < 0)
 			return 0;
 
+		if (dirent.direntlen == 0) {
+			printf("Failed to iterate over directory %s\n", name);
+			return 0;
+		}
+
 		if (dirent.namelen != 0) {
 			char filename[dirent.namelen + 1];
 			struct ext2fs_node *fdiro;
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH 1/1 fs: ext4: Prevent infinite loop in ext4fs_iterate_dir

Hi Thomas,

Quoted 26 lines · Thomas Fitzsimmons

If the ext3 journal gets out of sync with what is written on disk, for example because of an unexpected power cut, ext4fs_read_file can return an all-zero directory entry. In that case, ext4fs_iterate_dir would infinite loop.

This patch detects when a directory entry's direntlen member is 0 and returns a failure status, which breaks out of the infinite loop. As a result, U-Boot will not find files that may subsequently be recovered when the journal is replayed.

This is better behaviour than hanging in an infinite loop, but as a further improvement maybe U-Boot could interpret the ext3 journal and actually find the unsynced entries.

---
  fs/ext4/ext4_common.c | 5 +++++
  1 file changed, 5 insertions(+)
fs/ext4/ext4_common.c +0 -0
index 727a2f7..e73223a 100644
@@ -1920,6 +1920,11 @@ int ext4fs_iterate_dir(struct ext2fs_node *dir, char *name,
  		if (status < 0)

fs/ext4/ext4_common.c int ext4fs_iterate_dir(struct ext2fs_node *dir, char *name, · line 1930

Quoted 10 lines
fs/ext4/ext4_common.c +5 -0
  			return 0;
 
+		if (dirent.direntlen == 0) {
+			printf("Failed to iterate over directory %s\n", name);
+			return 0;
+		}
+
  		if (dirent.namelen != 0) {
  			char filename[dirent.namelen + 1];
  			struct ext2fs_node *fdiro;

Thanks,
Stefan

#2 lore raw
In reply to Thomas Fitzsimmons
Quoted 10 lines · Thomas Fitzsimmons

If the ext3 journal gets out of sync with what is written on disk, for example because of an unexpected power cut, ext4fs_read_file can return an all-zero directory entry. In that case, ext4fs_iterate_dir would infinite loop.

This patch detects when a directory entry's direntlen member is 0 and returns a failure status, which breaks out of the infinite loop. As a result, U-Boot will not find files that may subsequently be recovered when the journal is replayed.

Quoted 6 lines

This is better behaviour than hanging in an infinite loop, but as a further improvement maybe U-Boot could interpret the ext3 journal and actually find the unsynced entries.

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20151123/396abff4/attachment.sig>