Skip to contentSkip to conversation

ovl: fix IS_APPEND() checks

PATCH Superseded Amir Goldstein 2 participants 11 messages Apr 7, 2017 – Apr 8, 2017

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Amir Goldstein

author maintainer
#0 lore raw

v2 cover ovl: fix IS_APPEND() checks

Miklos,

This series fixes the append-only violations found by the new xfstest overlay/030.

I got rid of the wrong patch to convert sb_start_write() to file_start_write() and moved the open flag checks into ovl_d_real().

Amir.

v2:
- move open check to ovl_d_real()
- discard wrong patch to file_start_write() in ftruncate()

v1:
- fix checks in [f]truncate and vfs_open()

Amir Goldstein (3):
vfs: ftruncate check IS_APPEND() on real inode
vfs: truncate check IS_APPEND() on real inode
ovl: check IS_APPEND() on real upper inode

 fs/open.c            | 20 ++++++++++----------
 fs/overlayfs/super.c | 30 +++++++++++++++++++++++++++---
 2 files changed, 37 insertions(+), 13 deletions(-)
Signature · 1 line
2.7.4
author maintainer
#1 lore raw

PATCH v2 1/3 vfs: ftruncate check IS_APPEND() on real inode

ftruncate an overlayfs inode was checking IS_APPEND() on overlay inode, but overlay inode does not have the S_APPEND flag.

Set 'inode' var to file_inode() so all checks are performed on the real inode and use the overlay inode/sb explicitly for freeze protection and file lock checks.

---
 fs/open.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
fs/open.c +4 -4
index 949cef2..b7d5ab1 100644
@@ -182,7 +182,7 @@ static long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
 		small = 0;
 
 	dentry = f.file->f_path.dentry;
-	inode = dentry->d_inode;
+	inode = file_inode(f.file);
 	error = -EINVAL;
 	if (!S_ISREG(inode->i_mode) || !(f.file->f_mode & FMODE_WRITE))
 		goto out_putf;
@@ -196,13 +196,13 @@ static long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
 	if (IS_APPEND(inode))
 		goto out_putf;
 
-	sb_start_write(inode->i_sb);
-	error = locks_verify_truncate(inode, f.file, length);
+	sb_start_write(dentry->d_sb);
+	error = locks_verify_truncate(d_inode(dentry), f.file, length);
 	if (!error)
 		error = security_path_truncate(&f.file->f_path);
 	if (!error)
 		error = do_truncate(dentry, length, ATTR_MTIME|ATTR_CTIME, f.file);
-	sb_end_write(inode->i_sb);
+	sb_end_write(dentry->d_sb);
 out_putf:
 	fdput(f);
 out:
Signature · 1 line
2.7.4
maintainer
#2 lore raw

Re: [PATCH v2 1/3] vfs: ftruncate check IS_APPEND() on real inode

Quoted 6 lines · Amir Goldstein

ftruncate an overlayfs inode was checking IS_APPEND() on overlay inode, but overlay inode does not have the S_APPEND flag.

Set 'inode' var to file_inode() so all checks are performed on the real inode and use the overlay inode/sb explicitly for freeze protection and file lock checks.

If you'd just add the file_inode() to the IS_APPEND check then the patch would look a lot less complicated and the effect should be exactly the same. Am I missing something?

Thanks,
Miklos

Quoted 39 lines
---
 fs/open.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
fs/open.c +4 -5
index 949cef2..b7d5ab1 100644
@@ -182,7 +182,7 @@ static long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
                small = 0;
 
        dentry = f.file->f_path.dentry;
-       inode = dentry->d_inode;
+       inode = file_inode(f.file);
        error = -EINVAL;
        if (!S_ISREG(inode->i_mode) || !(f.file->f_mode & FMODE_WRITE))
                goto out_putf;
@@ -196,13 +196,13 @@ static long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
        if (IS_APPEND(inode))
                goto out_putf;
 
-       sb_start_write(inode->i_sb);
-       error = locks_verify_truncate(inode, f.file, length);
+       sb_start_write(dentry->d_sb);
+       error = locks_verify_truncate(d_inode(dentry), f.file, length);
        if (!error)
                error = security_path_truncate(&f.file->f_path);
        if (!error)
                error = do_truncate(dentry, length, ATTR_MTIME|ATTR_CTIME, f.file);
-       sb_end_write(inode->i_sb);
+       sb_end_write(dentry->d_sb);
 out_putf:
        fdput(f);
 out:
--

2.7.4

author maintainer
#3 lore raw

Re: [PATCH v2 1/3] vfs: ftruncate check IS_APPEND() on real inode

Quoted 4 lines · Miklos Szeredi
Quoted 3 lines · Amir Goldstein

ftruncate an overlayfs inode was checking IS_APPEND() on overlay inode, but overlay inode does not have the S_APPEND flag.

Quoted 8 lines
Quoted 3 lines

Set 'inode' var to file_inode() so all checks are performed on the real inode and use the overlay inode/sb explicitly for freeze protection and file lock checks.

If you'd just add the file_inode() to the IS_APPEND check then the patch would look a lot less complicated and the effect should be exactly the same. Am I missing something?

Technically you are right.
Social engineering wise, its quite hard to look at a random vfs function and say what 'inode' stands.

I surveyed some functions and found that in most cases where file->f_inode is set a local var named inode stands for file_inode() and locks_inode() is the exception, so this patch brings do_sys_ftruncate() into compliance.

Quoted 42 lines

Thanks,
Miklos

Quoted 39 lines
---
 fs/open.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
fs/open.c +4 -5
index 949cef2..b7d5ab1 100644
@@ -182,7 +182,7 @@ static long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
                small = 0;
 
        dentry = f.file->f_path.dentry;
-       inode = dentry->d_inode;
+       inode = file_inode(f.file);
        error = -EINVAL;
        if (!S_ISREG(inode->i_mode) || !(f.file->f_mode & FMODE_WRITE))
                goto out_putf;
@@ -196,13 +196,13 @@ static long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
        if (IS_APPEND(inode))
                goto out_putf;
 
-       sb_start_write(inode->i_sb);
-       error = locks_verify_truncate(inode, f.file, length);
+       sb_start_write(dentry->d_sb);
+       error = locks_verify_truncate(d_inode(dentry), f.file, length);
        if (!error)
                error = security_path_truncate(&f.file->f_path);
        if (!error)
                error = do_truncate(dentry, length, ATTR_MTIME|ATTR_CTIME, f.file);
-       sb_end_write(inode->i_sb);
+       sb_end_write(dentry->d_sb);
 out_putf:
        fdput(f);
 out:
--

2.7.4

author maintainer
#4 lore raw

PATCH v2 2/3 vfs: truncate check IS_APPEND() on real inode

In reply to Amir Goldstein

truncate an overlayfs inode was checking IS_APPEND() on overlay inode, but overlay inode does not have the S_APPEND flag.

Move the IS_APPEND() check to after we have the upperdentry and pass it the real upper inode.

---
 fs/open.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
fs/open.c +6 -6
index b7d5ab1..425db52 100644
@@ -87,10 +87,6 @@ long vfs_truncate(const struct path *path, loff_t length)
 	if (error)
 		goto mnt_drop_write_and_out;
 
-	error = -EPERM;
-	if (IS_APPEND(inode))
-		goto mnt_drop_write_and_out;
-
 	/*
 	 * If this is an overlayfs then do as if opening the file so we get
 	 * write access on the upper inode, not on the overlay inode.  For
@@ -101,7 +97,11 @@ long vfs_truncate(const struct path *path, loff_t length)
 	if (IS_ERR(upperdentry))
 		goto mnt_drop_write_and_out;
 
-	error = get_write_access(upperdentry->d_inode);
+	error = -EPERM;
+	if (IS_APPEND(d_inode(upperdentry)))
+		goto mnt_drop_write_and_out;
+
+	error = get_write_access(d_inode(upperdentry));
 	if (error)
 		goto mnt_drop_write_and_out;
 
@@ -120,7 +120,7 @@ long vfs_truncate(const struct path *path, loff_t length)
 		error = do_truncate(path->dentry, length, 0, NULL);
 
 put_write_and_out:
-	put_write_access(upperdentry->d_inode);
+	put_write_access(d_inode(upperdentry));
 mnt_drop_write_and_out:
 	mnt_drop_write(path->mnt);
 out:
Signature · 1 line
2.7.4
maintainer
#5 lore raw

Re: [PATCH v2 2/3] vfs: truncate check IS_APPEND() on real inode

Quoted 5 lines · Amir Goldstein

truncate an overlayfs inode was checking IS_APPEND() on overlay inode, but overlay inode does not have the S_APPEND flag.

Move the IS_APPEND() check to after we have the upperdentry and pass it the real upper inode.

Not sure the reordering is worth it. Just use d_real_inode() in the IS_APPEND() check. OTOH it shouldn't matter (well, except whether the file is copied up or not in the error caae ). But mainly I just feel when there's a choice of a simpler way we should use that.

Also it's usually better not to mix fixes with cleanups (the d_inode() thing).

Thanks,
Miklos

Quoted 46 lines
---
 fs/open.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
fs/open.c +6 -7
index b7d5ab1..425db52 100644
@@ -87,10 +87,6 @@ long vfs_truncate(const struct path *path, loff_t length)
        if (error)
                goto mnt_drop_write_and_out;
 
-       error = -EPERM;
-       if (IS_APPEND(inode))
-               goto mnt_drop_write_and_out;
-
        /*
         * If this is an overlayfs then do as if opening the file so we get
         * write access on the upper inode, not on the overlay inode.  For
@@ -101,7 +97,11 @@ long vfs_truncate(const struct path *path, loff_t length)
        if (IS_ERR(upperdentry))
                goto mnt_drop_write_and_out;
 
-       error = get_write_access(upperdentry->d_inode);
+       error = -EPERM;
+       if (IS_APPEND(d_inode(upperdentry)))
+               goto mnt_drop_write_and_out;
+
+       error = get_write_access(d_inode(upperdentry));
        if (error)
                goto mnt_drop_write_and_out;
 
@@ -120,7 +120,7 @@ long vfs_truncate(const struct path *path, loff_t length)
                error = do_truncate(path->dentry, length, 0, NULL);
 
 put_write_and_out:
-       put_write_access(upperdentry->d_inode);
+       put_write_access(d_inode(upperdentry));
 mnt_drop_write_and_out:
        mnt_drop_write(path->mnt);
 out:
--

2.7.4

author maintainer
#6 lore raw

Re: [PATCH v2 2/3] vfs: truncate check IS_APPEND() on real inode

Quoted 8 lines · Miklos Szeredi
Quoted 5 lines · Amir Goldstein

truncate an overlayfs inode was checking IS_APPEND() on overlay inode, but overlay inode does not have the S_APPEND flag.

Move the IS_APPEND() check to after we have the upperdentry and pass it the real upper inode.

Not sure the reordering is worth it. Just use d_real_inode() in the

Hmm, I meant move after d_real() which actually does a copy up. If we use d_real_inode() before d_real() then we prevent truncate of a lower with chattr +a.
That is not consistent at all with open(O_TRUNC) and ftruncate and with checks for IS_IMMUTABLE() which only test upper (and I think it is better this way).

Quoted 6 lines

IS_APPEND() check. OTOH it shouldn't matter (well, except whether the file is copied up or not in the error caae ). But mainly I just feel when there's a choice of a simpler way we should use that.

Also it's usually better not to mix fixes with cleanups (the d_inode() thing).

Sure, I can split that or drop the cleanup altogether.

Quoted 49 lines

Thanks,
Miklos

Quoted 46 lines
---
 fs/open.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
fs/open.c +6 -7
index b7d5ab1..425db52 100644
@@ -87,10 +87,6 @@ long vfs_truncate(const struct path *path, loff_t length)
        if (error)
                goto mnt_drop_write_and_out;
 
-       error = -EPERM;
-       if (IS_APPEND(inode))
-               goto mnt_drop_write_and_out;
-
        /*
         * If this is an overlayfs then do as if opening the file so we get
         * write access on the upper inode, not on the overlay inode.  For
@@ -101,7 +97,11 @@ long vfs_truncate(const struct path *path, loff_t length)
        if (IS_ERR(upperdentry))
                goto mnt_drop_write_and_out;
 
-       error = get_write_access(upperdentry->d_inode);
+       error = -EPERM;
+       if (IS_APPEND(d_inode(upperdentry)))
+               goto mnt_drop_write_and_out;
+
+       error = get_write_access(d_inode(upperdentry));
        if (error)
                goto mnt_drop_write_and_out;
 
@@ -120,7 +120,7 @@ long vfs_truncate(const struct path *path, loff_t length)
                error = do_truncate(path->dentry, length, 0, NULL);
 
 put_write_and_out:
-       put_write_access(upperdentry->d_inode);
+       put_write_access(d_inode(upperdentry));
 mnt_drop_write_and_out:
        mnt_drop_write(path->mnt);
 out:
--

2.7.4

author maintainer
#7 lore raw

Re: [PATCH v2 2/3] vfs: truncate check IS_APPEND() on real inode

Quoted 17 lines · Amir Goldstein
Quoted 8 lines · Miklos Szeredi
Quoted 5 lines · Amir Goldstein

truncate an overlayfs inode was checking IS_APPEND() on overlay inode, but overlay inode does not have the S_APPEND flag.

Move the IS_APPEND() check to after we have the upperdentry and pass it the real upper inode.

Not sure the reordering is worth it. Just use d_real_inode() in the

Hmm, I meant move after d_real() which actually does a copy up. If we use d_real_inode() before d_real() then we prevent truncate of a lower with chattr +a.
That is not consistent at all with open(O_TRUNC) and ftruncate and with checks for IS_IMMUTABLE() which only test upper (and I think it is better this way).

Quoted 9 lines
Quoted 6 lines

IS_APPEND() check. OTOH it shouldn't matter (well, except whether the file is copied up or not in the error caae ). But mainly I just feel when there's a choice of a simpler way we should use that.

Also it's usually better not to mix fixes with cleanups (the d_inode() thing).

Sure, I can split that or drop the cleanup altogether.

Or drop the patch altogether... d_real() already takes care of IS_APPEND() now :)

author maintainer
#8 lore raw

PATCH v2 3/3 ovl: check IS_APPEND() on real upper inode

In reply to Amir Goldstein

For overlay file open, check IS_APPEND() on the real upper inode inside d_real(), because the overlay inode does not have the S_APPEND flag and IS_APPEND() can only be checked at open time.

Note that because overlayfs does not copy up the chattr inode flags (i.e. S_APPEND, S_IMMUTABLE), the IS_APPEND() check is only relevant for upper inodes that were set with chattr +a and not to lower inodes that had chattr +a before copy up.

---
 fs/overlayfs/super.c | 30 +++++++++++++++++++++++++++---
 1 file changed, 27 insertions(+), 3 deletions(-)
fs/overlayfs/super.c +27 -3
index c9e70d3..13806b8 100644
@@ -49,11 +49,30 @@ static void ovl_dentry_release(struct dentry *dentry)
 	}
 }
 
+static int ovl_may_open(struct dentry *dentry, int flag)
+{
+	struct inode *inode = dentry->d_inode;
+
+	/*
+	 * This test was moot in vfs may_open() because overlay inode does
+	 * not have the S_APPEND flag
+	 */
+	if (IS_APPEND(inode)) {
+		if  ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
+			return -EPERM;
+		if (flag & O_TRUNC)
+			return -EPERM;
+	}
+
+	return 0;
+}
+
 static struct dentry *ovl_d_real(struct dentry *dentry,
 				 const struct inode *inode,
 				 unsigned int open_flags)
 {
 	struct dentry *real;
+	int err;
 
 	if (!d_is_reg(dentry)) {
 		if (!inode || inode == d_inode(dentry))
@@ -65,15 +84,20 @@ static struct dentry *ovl_d_real(struct dentry *dentry,
 		return dentry;
 
 	if (open_flags) {
-		int err = ovl_open_maybe_copy_up(dentry, open_flags);
-
+		err = ovl_open_maybe_copy_up(dentry, open_flags);
 		if (err)
 			return ERR_PTR(err);
 	}
 
 	real = ovl_dentry_upper(dentry);
-	if (real && (!inode || inode == d_inode(real)))
+	if (real && (!inode || inode == d_inode(real))) {
+		if (!inode) {
+			err = ovl_may_open(real, open_flags);
+			if (err)
+				return ERR_PTR(err);
+		}
 		return real;
+	}
 
 	real = ovl_dentry_lower(dentry);
 	if (!real)
Signature · 1 line
2.7.4
maintainer
#9 lore raw

Re: [PATCH v2 3/3] ovl: check IS_APPEND() on real upper inode

Quoted 8 lines · Amir Goldstein

For overlay file open, check IS_APPEND() on the real upper inode inside d_real(), because the overlay inode does not have the S_APPEND flag and IS_APPEND() can only be checked at open time.

Note that because overlayfs does not copy up the chattr inode flags (i.e. S_APPEND, S_IMMUTABLE), the IS_APPEND() check is only relevant for upper inodes that were set with chattr +a and not to lower inodes that had chattr +a before copy up.

Okay. Maybe rename ovl_may_open() to ovl_check_append_only(),
because that what it does.

Thanks,
Miklos

Quoted 68 lines
---
 fs/overlayfs/super.c | 30 +++++++++++++++++++++++++++---
 1 file changed, 27 insertions(+), 3 deletions(-)
fs/overlayfs/super.c +27 -4
index c9e70d3..13806b8 100644
@@ -49,11 +49,30 @@ static void ovl_dentry_release(struct dentry *dentry)
        }
 }
 
+static int ovl_may_open(struct dentry *dentry, int flag)
+{
+       struct inode *inode = dentry->d_inode;
+
+       /*
+        * This test was moot in vfs may_open() because overlay inode does
+        * not have the S_APPEND flag
+        */
+       if (IS_APPEND(inode)) {
+               if  ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
+                       return -EPERM;
+               if (flag & O_TRUNC)
+                       return -EPERM;
+       }
+
+       return 0;
+}
+
 static struct dentry *ovl_d_real(struct dentry *dentry,
                                 const struct inode *inode,
                                 unsigned int open_flags)
 {
        struct dentry *real;
+       int err;
 
        if (!d_is_reg(dentry)) {
                if (!inode || inode == d_inode(dentry))
@@ -65,15 +84,20 @@ static struct dentry *ovl_d_real(struct dentry *dentry,
                return dentry;
 
        if (open_flags) {
-               int err = ovl_open_maybe_copy_up(dentry, open_flags);
-
+               err = ovl_open_maybe_copy_up(dentry, open_flags);
                if (err)
                        return ERR_PTR(err);
        }
 
        real = ovl_dentry_upper(dentry);
-       if (real && (!inode || inode == d_inode(real)))
+       if (real && (!inode || inode == d_inode(real))) {
+               if (!inode) {
+                       err = ovl_may_open(real, open_flags);
+                       if (err)
+                               return ERR_PTR(err);
+               }
                return real;
+       }
 
        real = ovl_dentry_lower(dentry);
        if (!real)
--

2.7.4

author maintainer
#10 lore raw

Re: [PATCH v2 3/3] ovl: check IS_APPEND() on real upper inode

Quoted 5 lines · Miklos Szeredi
Quoted 4 lines · Amir Goldstein

For overlay file open, check IS_APPEND() on the real upper inode inside d_real(), because the overlay inode does not have the S_APPEND flag and IS_APPEND() can only be checked at open time.

Quoted 7 lines
Quoted 4 lines

Note that because overlayfs does not copy up the chattr inode flags (i.e. S_APPEND, S_IMMUTABLE), the IS_APPEND() check is only relevant for upper inodes that were set with chattr +a and not to lower inodes that had chattr +a before copy up.

Okay. Maybe rename ovl_may_open() to ovl_check_append_only(),
because that what it does.

Agree. Also I can pass the inode instead of dentry.

Quoted 72 lines

Thanks,
Miklos

Quoted 68 lines
---
 fs/overlayfs/super.c | 30 +++++++++++++++++++++++++++---
 1 file changed, 27 insertions(+), 3 deletions(-)
fs/overlayfs/super.c +27 -4
index c9e70d3..13806b8 100644
@@ -49,11 +49,30 @@ static void ovl_dentry_release(struct dentry *dentry)
        }
 }
 
+static int ovl_may_open(struct dentry *dentry, int flag)
+{
+       struct inode *inode = dentry->d_inode;
+
+       /*
+        * This test was moot in vfs may_open() because overlay inode does
+        * not have the S_APPEND flag
+        */
+       if (IS_APPEND(inode)) {
+               if  ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
+                       return -EPERM;
+               if (flag & O_TRUNC)
+                       return -EPERM;
+       }
+
+       return 0;
+}
+
 static struct dentry *ovl_d_real(struct dentry *dentry,
                                 const struct inode *inode,
                                 unsigned int open_flags)
 {
        struct dentry *real;
+       int err;
 
        if (!d_is_reg(dentry)) {
                if (!inode || inode == d_inode(dentry))
@@ -65,15 +84,20 @@ static struct dentry *ovl_d_real(struct dentry *dentry,
                return dentry;
 
        if (open_flags) {
-               int err = ovl_open_maybe_copy_up(dentry, open_flags);
-
+               err = ovl_open_maybe_copy_up(dentry, open_flags);
                if (err)
                        return ERR_PTR(err);
        }
 
        real = ovl_dentry_upper(dentry);
-       if (real && (!inode || inode == d_inode(real)))
+       if (real && (!inode || inode == d_inode(real))) {
+               if (!inode) {
+                       err = ovl_may_open(real, open_flags);
+                       if (err)
+                               return ERR_PTR(err);
+               }
                return real;
+       }
 
        real = ovl_dentry_lower(dentry);
        if (!real)
--

2.7.4