Skip to contentSkip to conversation

vfs: Avoid creation of directory loops for corrupted filesystems

PATCH In review Jan Kara 1 participant 6 messages Jun 20, 2012 – Jun 28, 2012

Subsystems (3)
lore
Share & tools
lore

Jan Kara

author reviewer
#0 lore raw

PATCH v2 1/5 vfs: Avoid creation of directory loops for corrupted filesystems

When a directory hierarchy is corrupted (e. g. due to a bit flip on the media), it can happen that it contains loops of directories. That creates possibilities for deadlock when locking directories.

Fix the problem by checking in d_splice_alias() that when we splice a directory, it does not have any other connected alias.

---
 fs/dcache.c |    5 +++++
 1 files changed, 5 insertions(+), 0 deletions(-)

Sorry guys, I'm resending this again because I messed up list address
previously.

fs/dcache.c +5 -0
index 4046904..afe81fe 100644
@@ -1658,6 +1658,11 @@ struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
 			d_move(new, dentry);
 			iput(inode);
 		} else {
+			if (unlikely(!list_empty(&inode->i_dentry))) {
+				spin_unlock(&inode->i_lock);
+				iput(inode);
+				return ERR_PTR(-EIO);
+			}
 			/* already taking inode->i_lock, so d_add() by hand */
 			__d_instantiate(dentry, inode);
 			spin_unlock(&inode->i_lock);
Signature · 1 line
1.7.1
author reviewer
#1 lore raw

PATCH 2/5 ext2: Handle error from d_splice_alias()

When directory hiearchy is corrupted and contains cycles, d_splice_alias() can fail. Warn user and mark filesystem as with errors when that happens.

---
 fs/ext2/namei.c |    8 +++++++-
 1 files changed, 7 insertions(+), 1 deletions(-)
fs/ext2/namei.c +7 -1
index f663a67..b3f074b 100644
@@ -59,6 +59,7 @@ static struct dentry *ext2_lookup(struct inode * dir, struct dentry *dentry, str
 {
 	struct inode * inode;
 	ino_t ino;
+	struct dentry *ret;
 	
 	if (dentry->d_name.len > EXT2_NAME_LEN)
 		return ERR_PTR(-ENAMETOOLONG);
@@ -74,7 +75,12 @@ static struct dentry *ext2_lookup(struct inode * dir, struct dentry *dentry, str
 			return ERR_PTR(-EIO);
 		}
 	}
-	return d_splice_alias(inode, dentry);
+	ret = d_splice_alias(inode, dentry);
+	if (IS_ERR(ret)) {
+		ext2_error(dir->i_sb, __func__, "directory #%lu corrupted",
+			   dir->i_ino);
+	}
+	return ret;
 }
 
 struct dentry *ext2_get_parent(struct dentry *child)
Signature · 1 line
1.7.1
author reviewer
#2 lore raw

PATCH 3/5 ext3: Handle error from d_splice_alias()

In reply to Jan Kara

When directory hiearchy is corrupted and contains cycles, d_splice_alias() can fail. Warn user and mark filesystem as with errors when that happens.

---
 fs/ext3/namei.c |    8 +++++++-
 1 files changed, 7 insertions(+), 1 deletions(-)
fs/ext3/namei.c +7 -1
index eeb63df..febb9f1 100644
@@ -1016,6 +1016,7 @@ static struct dentry *ext3_lookup(struct inode * dir, struct dentry *dentry, str
 	struct inode * inode;
 	struct ext3_dir_entry_2 * de;
 	struct buffer_head * bh;
+	struct dentry *ret;
 
 	if (dentry->d_name.len > EXT3_NAME_LEN)
 		return ERR_PTR(-ENAMETOOLONG);
@@ -1038,7 +1039,12 @@ static struct dentry *ext3_lookup(struct inode * dir, struct dentry *dentry, str
 			return ERR_PTR(-EIO);
 		}
 	}
-	return d_splice_alias(inode, dentry);
+	ret = d_splice_alias(inode, dentry);
+	if (IS_ERR(ret)) {
+		ext3_error(dir->i_sb, __func__, "directory #%lu corrupted",
+			   dir->i_ino);
+	}
+	return ret;
 }
 
 
Signature · 1 line
1.7.1
author reviewer
#3 lore raw

PATCH 4/5 ext4: Handle error from d_splice_alias()

In reply to Jan Kara

When directory hiearchy is corrupted and contains cycles, d_splice_alias() can fail. Warn user and mark filesystem as with errors when that happens.

---
 fs/ext4/namei.c |    6 +++++-
 1 files changed, 5 insertions(+), 1 deletions(-)
fs/ext4/namei.c +5 -1
index 5845cd9..0cb1a94 100644
@@ -1317,6 +1317,7 @@ static struct dentry *ext4_lookup(struct inode *dir, struct dentry *dentry, stru
 	struct inode *inode;
 	struct ext4_dir_entry_2 *de;
 	struct buffer_head *bh;
+	struct dentry *ret;
 
 	if (dentry->d_name.len > EXT4_NAME_LEN)
 		return ERR_PTR(-ENAMETOOLONG);
@@ -1344,7 +1345,10 @@ static struct dentry *ext4_lookup(struct inode *dir, struct dentry *dentry, stru
 			return ERR_PTR(-EIO);
 		}
 	}
-	return d_splice_alias(inode, dentry);
+	ret = d_splice_alias(inode, dentry);
+	if (IS_ERR(ret))
+		EXT4_ERROR_INODE(dir, "directory corrupted");
+	return ret;
 }
 
 
Signature · 1 line
1.7.1
author reviewer
#4 lore raw

PATCH 5/5 exofs: Handle error from d_splice_alias()

In reply to Jan Kara

From: Boaz Harrosh <[email protected]>

When directory hierarchy is corrupted and contains cycles, d_splice_alias() can fail. Warn user if that happens.

---
 fs/exofs/namei.c |    6 +++++-
 1 files changed, 5 insertions(+), 1 deletions(-)
fs/exofs/namei.c +5 -1
index fc7161d..6f44d3c 100644
@@ -50,13 +50,17 @@ static struct dentry *exofs_lookup(struct inode *dir, struct dentry *dentry,
 {
 	struct inode *inode;
 	ino_t ino;
+	struct dentry *ret;
 
 	if (dentry->d_name.len > EXOFS_NAME_LEN)
 		return ERR_PTR(-ENAMETOOLONG);
 
 	ino = exofs_inode_by_name(dir, dentry);
 	inode = ino ? exofs_iget(dir->i_sb, ino) : NULL;
-	return d_splice_alias(inode, dentry);
+	ret = d_splice_alias(inode, dentry);
+	if (IS_ERR(ret))
+		EXOFS_ERR("directory #%lu corrupted", dir->i_ino);
+	return ret;
 }
 
 static int exofs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Signature · 1 line
1.7.1
author reviewer
#5 lore raw
In reply to Jan Kara
Quoted 6 lines · Jan Kara

When a directory hierarchy is corrupted (e. g. due to a bit flip on the media), it can happen that it contains loops of directories. That creates possibilities for deadlock when locking directories.

Fix the problem by checking in d_splice_alias() that when we splice a directory, it does not have any other connected alias.

Al, any opinion on this patch series?

Honza

Quoted 29 lines
---
 fs/dcache.c |    5 +++++
 1 files changed, 5 insertions(+), 0 deletions(-)

Sorry guys, I'm resending this again because I messed up list address
previously.

fs/dcache.c +5 -0
index 4046904..afe81fe 100644
@@ -1658,6 +1658,11 @@ struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
 			d_move(new, dentry);
 			iput(inode);
 		} else {
+			if (unlikely(!list_empty(&inode->i_dentry))) {
+				spin_unlock(&inode->i_lock);
+				iput(inode);
+				return ERR_PTR(-EIO);
+			}
 			/* already taking inode->i_lock, so d_add() by hand */
 			__d_instantiate(dentry, inode);
 			spin_unlock(&inode->i_lock);
Signature · 1 line
1.7.1
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR