Skip to contentSkip to conversation

fat: Fix oops on corrupted vfat fs

PATCH In review OGAWA Hirofumi 2 participants 3 messages Nov 26, 2014 – Nov 27, 2014

Subsystems (2)
lore
Share & tools
lore

OGAWA Hirofumi

author maintainer
#0 lore raw

PATCH 1/1 fat: Fix oops on corrupted vfat fs

a) don't bother with ->d_time for positives - we only check it for negatives anyway.
b) make sure to set it at unlink and rmdir time - at *that* point soon-to-be negative dentry matches then-current directory contents
c) don't go into renaming of old alias in vfat_lookup() unless it has the same parent (which it will, unless we are seeing corrupted image)

Cc <[email protected]> # 3.17.x
[Make change minimum, don't call d_move() for dir]

---

 fs/fat/namei_vfat.c |   20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)
fs/fat/namei_vfat.c renamed from fs/fat/namei_vfat.c~fat-corrupt-fs-fix +11 -9
diff -puN fs/fat/namei_vfat.c~fat-corrupt-fs-fix fs/fat/namei_vfat.c
@@ -736,7 +736,12 @@ static struct dentry *vfat_lookup(struct
 	}
 
 	alias = d_find_alias(inode);
-	if (alias && !vfat_d_anon_disconn(alias)) {
+	/*
+	 * Checking "alias->d_parent == dentry->d_parent" to make sure
+	 * FS is not corrupted (especially double linked dir).
+	 */
+	if (alias && alias->d_parent == dentry->d_parent &&
+	    !vfat_d_anon_disconn(alias)) {
 		/*
 		 * This inode has non anonymous-DCACHE_DISCONNECTED
 		 * dentry. This means, the user did ->lookup() by an
@@ -755,12 +760,9 @@ static struct dentry *vfat_lookup(struct
 
 out:
 	mutex_unlock(&MSDOS_SB(sb)->s_lock);
-	dentry->d_time = dentry->d_parent->d_inode->i_version;
-	dentry = d_splice_alias(inode, dentry);
-	if (dentry)
-		dentry->d_time = dentry->d_parent->d_inode->i_version;
-	return dentry;
-
+	if (!inode)
+		dentry->d_time = dir->i_version;
+	return d_splice_alias(inode, dentry);
 error:
 	mutex_unlock(&MSDOS_SB(sb)->s_lock);
 	return ERR_PTR(err);
@@ -793,7 +795,6 @@ static int vfat_create(struct inode *dir
 	inode->i_mtime = inode->i_atime = inode->i_ctime = ts;
 	/* timestamp is already written, so mark_inode_dirty() is unneeded. */
 
-	dentry->d_time = dentry->d_parent->d_inode->i_version;
 	d_instantiate(dentry, inode);
 out:
 	mutex_unlock(&MSDOS_SB(sb)->s_lock);
@@ -824,6 +825,7 @@ static int vfat_rmdir(struct inode *dir,
 	clear_nlink(inode);
 	inode->i_mtime = inode->i_atime = CURRENT_TIME_SEC;
 	fat_detach(inode);
+	dentry->d_time = dir->i_version;
 out:
 	mutex_unlock(&MSDOS_SB(sb)->s_lock);
 
@@ -849,6 +851,7 @@ static int vfat_unlink(struct inode *dir
 	clear_nlink(inode);
 	inode->i_mtime = inode->i_atime = CURRENT_TIME_SEC;
 	fat_detach(inode);
+	dentry->d_time = dir->i_version;
 out:
 	mutex_unlock(&MSDOS_SB(sb)->s_lock);
 
@@ -889,7 +892,6 @@ static int vfat_mkdir(struct inode *dir,
 	inode->i_mtime = inode->i_atime = inode->i_ctime = ts;
 	/* timestamp is already written, so mark_inode_dirty() is unneeded. */
 
-	dentry->d_time = dentry->d_parent->d_inode->i_version;
 	d_instantiate(dentry, inode);
 
 	mutex_unlock(&MSDOS_SB(sb)->s_lock);

_

#1 lore raw
Quoted 11 lines · On Thu, 27 Nov 2014 00:31:28 +0900 OGAWA Hirofumi…

a) don't bother with ->d_time for positives - we only check it for negatives anyway.
b) make sure to set it at unlink and rmdir time - at *that* point soon-to-be negative dentry matches then-current directory contents
c) don't go into renaming of old alias in vfat_lookup() unless it has the same parent (which it will, unless we are seeing corrupted image)

Cc <[email protected]> # 3.17.x
[Make change minimum, don't call d_move() for dir]

It's unclear who did the "[Make change minimum.." alteration.

I do it this way:
[[email protected]: make change minimum, don't call d_move() for dir]

Also, who was the primary author of this patch? It *looks* like it was Al, unsure. If it was indeed Al then this can be communicated by putting his From: line at the very top of the changelog body.

author maintainer
#2 lore raw
Quoted 14 lines · Andrew Morton
Quoted 11 lines · On Thu, 27 Nov 2014 00:31:28 +0900 OGAWA Hirofumi…

a) don't bother with ->d_time for positives - we only check it for negatives anyway.
b) make sure to set it at unlink and rmdir time - at *that* point soon-to-be negative dentry matches then-current directory contents
c) don't go into renaming of old alias in vfat_lookup() unless it has the same parent (which it will, unless we are seeing corrupted image)

Cc <[email protected]> # 3.17.x
[Make change minimum, don't call d_move() for dir]
Quoted 4 lines

It's unclear who did the "[Make change minimum.." alteration.

I do it this way:
[[email protected]: make change minimum, don't call d_move() for dir]

I see. OK, I will also use your format next time.

Quoted 3 lines

Also, who was the primary author of this patch? It *looks* like it was Al, unsure. If it was indeed Al then this can be communicated by putting his From: line at the very top of the changelog body.

Ah, sorry. The patch is written by Al, and I was forgetting to add "From:".

Signature · 1 line
OGAWA Hirofumi <[email protected]>