Skip to contentSkip to conversation

ovl: skip checking lower file's write permisson on truncate

RFC PATCH In review Chengguang Xu 2 participants 9 messages Apr 24, 2021 – Jul 21, 2021 Overlay filesystem

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/2 ovl: skip checking lower file's write permisson on truncate

Lower files may be shared in overlayfs so strictly checking write perssmion on lower file will cause interferes between different overlayfs instances.

---
 fs/overlayfs/inode.c | 6 ------
 1 file changed, 6 deletions(-)
fs/overlayfs/inode.c +0 -6
index 28c71978eb2e..17d1add0af1a 100644
@@ -31,12 +31,6 @@ int ovl_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
 		goto out;
 
 	if (attr->ia_valid & ATTR_SIZE) {
-		struct inode *realinode = d_inode(ovl_dentry_real(dentry));
-
-		err = -ETXTBSY;
-		if (atomic_read(&realinode->i_writecount) < 0)
-			goto out_drop_write;
-
 		/* Truncate should trigger data copy up as well */
 		full_copy_up = true;
 	}
Signature · 1 line
2.27.0
#1 lore raw

RFC PATCH 2/2 ovl: enhance write permission check for writable open

Check upper file's write permission when open on writable mode.

NOTE: lower files may be shared between differnt overlayfs instances, so we skip the check of lower file to avoid introducing interferes.

---
 fs/overlayfs/file.c | 6 ++++++
 1 file changed, 6 insertions(+)
fs/overlayfs/file.c +6 -0
index 6e454a294046..1c3c24d07d01 100644
@@ -144,12 +144,18 @@ static int ovl_real_fdget(const struct file *file, struct fd *real)
 static int ovl_open(struct inode *inode, struct file *file)
 {
 	struct file *realfile;
+	struct inode *upperinode;
 	int err;
 
 	err = ovl_maybe_copy_up(file_dentry(file), file->f_flags);
 	if (err)
 		return err;
 
+	upperinode = ovl_inode_upper(inode);
+	if (((file->f_mode & FMODE_WRITE) || file->f_flags & O_TRUNC) &&
+	    (upperinode && atomic_read(&upperinode->i_writecount) < 0))
+		return -ETXTBSY;
+
 	/* No longer need these flags, so don't pass them on to underlying fs */
 	file->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
 
Signature · 1 line
2.27.0
maintainer
#2 lore raw

Re: [RFC PATCH 2/2] ovl: enhance write permission check for writable open

Quoted 2 lines · Chengguang Xu

Check upper file's write permission when open on writable mode.

This should already be done in ovl_open() -> ovl_open_realfile() -> open_with_fake_path() -> do_dentry_open().

Do you have a test case indicating that the writecount test is not working?

Thanks,
Miklos

Quoted 36 lines

NOTE: lower files may be shared between differnt overlayfs instances, so we skip the check of lower file to avoid introducing interferes.

---
 fs/overlayfs/file.c | 6 ++++++
 1 file changed, 6 insertions(+)
fs/overlayfs/file.c +6 -1
index 6e454a294046..1c3c24d07d01 100644
@@ -144,12 +144,18 @@ static int ovl_real_fdget(const struct file *file, struct fd *real)
 static int ovl_open(struct inode *inode, struct file *file)
 {
        struct file *realfile;
+       struct inode *upperinode;
        int err;
 
        err = ovl_maybe_copy_up(file_dentry(file), file->f_flags);
        if (err)
                return err;
 
+       upperinode = ovl_inode_upper(inode);
+       if (((file->f_mode & FMODE_WRITE) || file->f_flags & O_TRUNC) &&
+           (upperinode && atomic_read(&upperinode->i_writecount) < 0))
+               return -ETXTBSY;
+
        /* No longer need these flags, so don't pass them on to underlying fs */
        file->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
 
--

2.27.0

#3 lore raw
In reply to Chengguang Xu

---- 在 星期六, 2021-04-24 22:03:15 Chengguang Xu <[email protected]> 撰写 ----

Quoted 3 lines

Lower files may be shared in overlayfs so strictly checking write perssmion on lower file will cause interferes between different overlayfs instances.

Any comment for this?

Thanks,
Chengguang

Quoted 28 lines
---
 fs/overlayfs/inode.c | 6 ------
 1 file changed, 6 deletions(-)
fs/overlayfs/inode.c +0 -6
index 28c71978eb2e..17d1add0af1a 100644
@@ -31,12 +31,6 @@ int ovl_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
         goto out;
 
     if (attr->ia_valid & ATTR_SIZE) {
-        struct inode *realinode = d_inode(ovl_dentry_real(dentry));
-
-        err = -ETXTBSY;
-        if (atomic_read(&realinode->i_writecount) < 0)
-            goto out_drop_write;
-
         /* Truncate should trigger data copy up as well */
         full_copy_up = true;
     }
Signature · 1 line
2.27.0
maintainer
#4 lore raw
In reply to Chengguang Xu
Quoted 4 lines · Chengguang Xu

Lower files may be shared in overlayfs so strictly checking write perssmion on lower file will cause interferes between different overlayfs instances.

How so?

i_writecount on lower inode is not modified by overlayfs (at least not in this codepath). Which means that there should be no interference between overlayfs instances sharing a lower directory tree.

Thanks,
Miklos

Quoted 27 lines
---
 fs/overlayfs/inode.c | 6 ------
 1 file changed, 6 deletions(-)
fs/overlayfs/inode.c +0 -7
index 28c71978eb2e..17d1add0af1a 100644
@@ -31,12 +31,6 @@ int ovl_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
                goto out;
 
        if (attr->ia_valid & ATTR_SIZE) {
-               struct inode *realinode = d_inode(ovl_dentry_real(dentry));
-
-               err = -ETXTBSY;
-               if (atomic_read(&realinode->i_writecount) < 0)
-                       goto out_drop_write;
-
                /* Truncate should trigger data copy up as well */
                full_copy_up = true;
        }
--

2.27.0

maintainer
#5 lore raw
Quoted 7 lines · Miklos Szeredi
Quoted 4 lines · Chengguang Xu

Lower files may be shared in overlayfs so strictly checking write perssmion on lower file will cause interferes between different overlayfs instances.

Quoted 5 lines

How so?

i_writecount on lower inode is not modified by overlayfs (at least not in this codepath). Which means that there should be no interference between overlayfs instances sharing a lower directory tree.

I'm beginning to see what you are worrying about.

So on one instance a file on lower gets executed and on another instance sharing the lower layer the file is truncated. The truncate is currently denied due to the negative i_writecount on the lower file. Also behavior is inconsistent between open(path, O_TRUNC) and truncate(path) even though the two should be equivalent.

Applied with the following description:

It is possible that a directory tree is shared between multiple overlay instances as a lower layer. In this case when one instance executes a file residing on the lower layer, the other instance denies a truncate(2) call on this file.

This only happens for truncate(2) and not for open(2) with the O_TRUNC flag.

Fix this interference and inconsistency by removing the preliminary i_writecount check before copy-up.

This means that unlike on normal filesystems truncate(argv[0]) will now succeed. If this ever causes a regression in a real world use case this needs to be revisited.

One way to fix this properly would be to keep a correct i_writecount in the overlay inode, but that is difficult due to memory mapping code only dealing with the real file/inode.

Thanks,
Miklos

#6 lore raw

---- 在 星期二, 2021-07-20 23:19:16 Miklos Szeredi <[email protected]> 撰写 ----

Quoted 14 lines · Miklos Szeredi
Quoted 7 lines · Chengguang Xu
Quoted 4 lines

Lower files may be shared in overlayfs so strictly checking write perssmion on lower file will cause interferes between different overlayfs instances.

Quoted 5 lines

How so?

i_writecount on lower inode is not modified by overlayfs (at least not in this codepath). Which means that there should be no interference between overlayfs instances sharing a lower directory tree.

Quoted 7 lines

I'm beginning to see what you are worrying about.

So on one instance a file on lower gets executed and on another instance sharing the lower layer the file is truncated. The truncate is currently denied due to the negative i_writecount on the lower file. Also behavior is inconsistent between open(path, O_TRUNC) and truncate(path) even though the two should be equivalent.

Yeah, that's it.
Thanks for applying the patch and supplementary description.

Thanks,
Chengguang

maintainer
#7 lore raw
In reply to Miklos Szeredi
Quoted 8 lines · Miklos Szeredi

So on one instance a file on lower gets executed and on another instance sharing the lower layer the file is truncated. The truncate is currently denied due to the negative i_writecount on the lower file. Also behavior is inconsistent between open(path, O_TRUNC) and truncate(path) even though the two should be equivalent.

Applied with the following description:

[...]

Also adding the following documentation in the "Non-standard behavior" section:

c) If a file residing on a lower layer is being executed, then opening that file for write or truncating the file will not be denied with ETXTBSY.

Looked at the POSIX standard and it only documents ETXTBUSY for O_RDWR and O_WRONLY and not for truncate(2) or O_TRUNC. So strictly speaking this patch doesn't even change the POSIX correctness.

Thanks,
Miklos

#8 lore raw

---- 在 星期三, 2021-07-21 00:01:11 Miklos Szeredi <[email protected]> 撰写 ----

Quoted 12 lines · Miklos Szeredi
Quoted 9 lines

So on one instance a file on lower gets executed and on another instance sharing the lower layer the file is truncated. The truncate is currently denied due to the negative i_writecount on the lower file. Also behavior is inconsistent between open(path, O_TRUNC) and truncate(path) even though the two should be equivalent.

Applied with the following description:

[...]

Quoted 9 lines

Also adding the following documentation in the "Non-standard behavior" section:

c) If a file residing on a lower layer is being executed, then opening that file for write or truncating the file will not be denied with ETXTBSY.

Looked at the POSIX standard and it only documents ETXTBUSY for O_RDWR and O_WRONLY and not for truncate(2) or O_TRUNC. So strictly speaking this patch doesn't even change the POSIX correctness.

Hi Miklos,

Thanks for doing this too.

Thanks,
Chengguang