Skip to contentSkip to conversation

Some fixes for overlayfs remove privs

PATCH New Amir Goldstein 1 participant 3 messages Oct 3, 2022

Subsystems (2)
lore
Share & tools
lore

Amir Goldstein

author maintainer
#0 lore raw

cover Some fixes for overlayfs remove privs

Miklos,

While running latest fstests on overlayfs-next, I noticed these failures:
generic/673 generic/683 generic/684 generic/685 generic/686 generic/687

Christian has also reported those failures earlier.

Those are not regressions, those are 5 new tests added to fstests and one test whose expected result was modified by fstests commit b3a59bb6 ("generic/673: fix golden output to reflect vfs setgid behavior").

The following two patches aim to fix those test failures, but they are incomplete - without those patches, the tests fail miserably in all test cases, because no privs are stripped.

With those two patches, only two test cases are failing, which are the two test cases whose expectation was changed by fstests commit b3a59bb6. The reason was explained in [1] and the issue was fixed for xfs by kernel commit e014f37db1a2 ("xfs: use setattr_copy to set vfs inode attributes").

Trying to figure out how to fix this hurts my brain, so I'll need suggestions how to proceed.

Thanks,
Amir.

[1] https://lore.kernel.org/linux-xfs/CAL3q7H47iNQ=Wmk83WcGB-KBJVOEtR9+qGczzCeXJ9Y2KCV25Q@mail.gmail.com/

Amir Goldstein (2):
ovl: remove privs in ovl_copyfile()
ovl: remove privs in ovl_fallocate()

 fs/overlayfs/file.c | 28 +++++++++++++++++++++++++---
 1 file changed, 25 insertions(+), 3 deletions(-)
Signature · 1 line
2.25.1
author maintainer
#1 lore raw

PATCH 1/2 ovl: remove privs in ovl_copyfile()

Underlying fs doesn't remove privs because copy_range/remap_range are called with privileged mounter credentials.

This fixes some failures in fstest generic/673.

Fixes 8ede205541ff ("ovl: add reflink/copyfile/dedup support")
---
 fs/overlayfs/file.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
fs/overlayfs/file.c +14 -2
index d17faeb014e5..c8308da8909a 100644
@@ -567,14 +567,23 @@ static loff_t ovl_copyfile(struct file *file_in, loff_t pos_in,
 	const struct cred *old_cred;
 	loff_t ret;
 
+	inode_lock(inode_out);
+	if (op != OVL_DEDUPE) {
+		/* Update mode */
+		ovl_copyattr(inode_out);
+		ret = file_remove_privs(file_out);
+		if (ret)
+			goto out_unlock;
+	}
+
 	ret = ovl_real_fdget(file_out, &real_out);
 	if (ret)
-		return ret;
+		goto out_unlock;
 
 	ret = ovl_real_fdget(file_in, &real_in);
 	if (ret) {
 		fdput(real_out);
-		return ret;
+		goto out_unlock;
 	}
 
 	old_cred = ovl_override_creds(file_inode(file_out)->i_sb);
@@ -603,6 +612,9 @@ static loff_t ovl_copyfile(struct file *file_in, loff_t pos_in,
 	fdput(real_in);
 	fdput(real_out);
 
+out_unlock:
+	inode_unlock(inode_out);
+
 	return ret;
 }
 
Signature · 1 line
2.25.1
author maintainer
#2 lore raw

PATCH 2/2 ovl: remove privs in ovl_fallocate()

In reply to Amir Goldstein

Underlying fs doesn't remove privs because fallocate is called with privileged mounter credentials.

This fixes some failure in fstests generic/683..687.

Fixes aab8848cee5e ("ovl: add ovl_fallocate()")
---
 fs/overlayfs/file.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
fs/overlayfs/file.c +11 -1
index c8308da8909a..e90ac5376456 100644
@@ -517,9 +517,16 @@ static long ovl_fallocate(struct file *file, int mode, loff_t offset, loff_t len
 	const struct cred *old_cred;
 	int ret;
 
+	inode_lock(inode);
+	/* Update mode */
+	ovl_copyattr(inode);
+	ret = file_remove_privs(file);
+	if (ret)
+		goto out_unlock;
+
 	ret = ovl_real_fdget(file, &real);
 	if (ret)
-		return ret;
+		goto out_unlock;
 
 	old_cred = ovl_override_creds(file_inode(file)->i_sb);
 	ret = vfs_fallocate(real.file, mode, offset, len);
@@ -530,6 +537,9 @@ static long ovl_fallocate(struct file *file, int mode, loff_t offset, loff_t len
 
 	fdput(real);
 
+out_unlock:
+	inode_unlock(inode);
+
 	return ret;
 }
 
Signature · 1 line
2.25.1