Skip to contentSkip to conversation

xfs: Correctly lock inode when removing suid and security marks

PATCH In review Jan Kara 3 participants 4 messages Dec 2, 2014 XFS filesystem

lore
Share & tools
lore

Jan Kara

author
#0 lore raw

PATCH 1/1 xfs: Correctly lock inode when removing suid and security marks

Currently XFS calls file_remove_suid() without holding i_mutex. This is wrong because that function can end up messing with file permissions and security xattrs for which we need i_mutex held.

Fix the problem by grabbing iolock exclusively when we will need to change anything in permissions / xattrs.

---
 fs/xfs/xfs_file.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
fs/xfs/xfs_file.c +12 -0
index eb596b419942..ad6636ac4943 100644
@@ -521,6 +521,18 @@ restart:
 	if (error)
 		return error;
 
+	/* For changing security info in file_remove_suid() we need i_mutex */
+	if (!IS_NOSEC(inode) && *iolock == XFS_IOLOCK_SHARED) {
+		struct dentry *dentry = file->f_path.dentry;
+
+		if (should_remove_suid(dentry) ||
+		    security_inode_need_killpriv(dentry)) {
+			xfs_rw_iunlock(ip, *iolock);
+			*iolock = XFS_IOLOCK_EXCL;
+			xfs_rw_ilock(ip, *iolock);
+			goto restart;
+		}
+	}
 	/*
 	 * If the offset is beyond the size of the file, we need to zero any
 	 * blocks that fall between the existing EOF and the start of this
Signature · 6 lines
1.8.1.4

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
#1 lore raw
Quoted 9 lines · Jan Kara

Currently XFS calls file_remove_suid() without holding i_mutex. This is wrong because that function can end up messing with file permissions and security xattrs for which we need i_mutex held.

Fix the problem by grabbing iolock exclusively when we will need to change anything in permissions / xattrs.

---

Hi Jan,

This doesn't compile... it looks like we need to include the security.h header. FWIW, even then I get an undefined symbol error when compiling as a module (security_inode_need_killpriv() does not appear to be exported).

Brian

Quoted 33 lines
 fs/xfs/xfs_file.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
fs/xfs/xfs_file.c +12 -0
index eb596b419942..ad6636ac4943 100644
@@ -521,6 +521,18 @@ restart:
 	if (error)
 		return error;
 
+	/* For changing security info in file_remove_suid() we need i_mutex */
+	if (!IS_NOSEC(inode) && *iolock == XFS_IOLOCK_SHARED) {
+		struct dentry *dentry = file->f_path.dentry;
+
+		if (should_remove_suid(dentry) ||
+		    security_inode_need_killpriv(dentry)) {
+			xfs_rw_iunlock(ip, *iolock);
+			*iolock = XFS_IOLOCK_EXCL;
+			xfs_rw_ilock(ip, *iolock);
+			goto restart;
+		}
+	}
 	/*
 	 * If the offset is beyond the size of the file, we need to zero any
 	 * blocks that fall between the existing EOF and the start of this
Signature · 6 lines
1.8.1.4

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

author
#2 lore raw
Quoted 11 lines · Brian Foster
Quoted 9 lines · Jan Kara

Currently XFS calls file_remove_suid() without holding i_mutex. This is wrong because that function can end up messing with file permissions and security xattrs for which we need i_mutex held.

Fix the problem by grabbing iolock exclusively when we will need to change anything in permissions / xattrs.

---

Quoted 6 lines

Hi Jan,

This doesn't compile... it looks like we need to include the security.h header. FWIW, even then I get an undefined symbol error when compiling as a module (security_inode_need_killpriv() does not appear to be exported).

Sorry, forgot to amend the include in the commit. Regarding export of
security_inode_need_killpriv() - right, I had security XFS compiled in so I didn't notice. Before I go and fix this up in the obvious way, does anyone have better idea how to fix this than to second guess what file_remove_suid() does? Maybe a VFS helper like file_needs_remove_suid() will be cleaner than what I did?

Honza

Quoted 33 lines
Quoted 33 lines
 fs/xfs/xfs_file.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
fs/xfs/xfs_file.c +12 -0
index eb596b419942..ad6636ac4943 100644
@@ -521,6 +521,18 @@ restart:
 	if (error)
 		return error;
 
+	/* For changing security info in file_remove_suid() we need i_mutex */
+	if (!IS_NOSEC(inode) && *iolock == XFS_IOLOCK_SHARED) {
+		struct dentry *dentry = file->f_path.dentry;
+
+		if (should_remove_suid(dentry) ||
+		    security_inode_need_killpriv(dentry)) {
+			xfs_rw_iunlock(ip, *iolock);
+			*iolock = XFS_IOLOCK_EXCL;
+			xfs_rw_ilock(ip, *iolock);
+			goto restart;
+		}
+	}
 	/*
 	 * If the offset is beyond the size of the file, we need to zero any
 	 * blocks that fall between the existing EOF and the start of this
Signature · 6 lines
1.8.1.4

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
Signature · 7 lines
Jan Kara <[email protected]>
SUSE Labs, CR

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
#3 lore raw
Quoted 18 lines · Jan Kara
Quoted 11 lines · Brian Foster
Quoted 9 lines · Jan Kara

Currently XFS calls file_remove_suid() without holding i_mutex. This is wrong because that function can end up messing with file permissions and security xattrs for which we need i_mutex held.

Fix the problem by grabbing iolock exclusively when we will need to change anything in permissions / xattrs.

---

Quoted 6 lines

Hi Jan,

This doesn't compile... it looks like we need to include the security.h header. FWIW, even then I get an undefined symbol error when compiling as a module (security_inode_need_killpriv() does not appear to be exported).

Quoted 6 lines

Sorry, forgot to amend the include in the commit. Regarding export of
security_inode_need_killpriv() - right, I had security XFS compiled in so I didn't notice. Before I go and fix this up in the obvious way, does anyone have better idea how to fix this than to second guess what file_remove_suid() does? Maybe a VFS helper like file_needs_remove_suid() will be cleaner than what I did?

Helper seems like a sane idea - that way the filesystems can use them as a matched pair if need be...

Cheers,

Dave.

Signature · 7 lines
Dave Chinner
[email protected]

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs