ovl: check VM_DENYWRITE mappings in copy-up
Subsystems (3)
PATCH 1/1 ovl: check VM_DENYWRITE mappings in copy-up
In overlayfs copy-up, if open flag has O_TRUNC then upper file will truncate to zero size, in this case we should check VM_DENYWRITE mappings to keep compatibility with other filesystems.
--- fs/overlayfs/copy_up.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
fs/overlayfs/copy_up.c +4 -1
@@ -901,8 +901,11 @@ static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry, }/* maybe truncate regular file. this has no effect on dirs */ -if (flags &O_TRUNC ) +if (flags &O_TRUNC ) { +if (atomic_read (&d_inode (ovl_dentry_real (dentry))->i_writecount) <0 ) +return -ETXTBSY ; ctx.stat.size =0 ; + }if (S_ISLNK (ctx.stat.mode)) { ctx.link =vfs_get_link (ctx.lowerpath.dentry, &done);
Signature · 1 line
2.27.0
---- 在 星期四, 2021-04-08 19:20:42 Chengguang Xu <[email protected]> 撰写 ----
Quoted 14 lines
In overlayfs copy-up, if open flag has
O_TRUNCthen upper file will truncate to zero size, in this case we should checkVM_DENYWRITEmappings to keep compatibility with other filesystems.Signed-off-by Chengguang Xu <[email protected]>--- fs/overlayfs/copy_up.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)fs/overlayfs/copy_up.c +0
-0
fs/overlayfs/copy_up.c static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry, · line 908
Quoted 9 lines
fs/overlayfs/copy_up.c +4
-1@@ -901,8 +901,11 @@ static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry, }/* maybe truncate regular file. this has no effect on dirs */ -if (flags &O_TRUNC ) +if (flags &O_TRUNC ) { +if (atomic_read (&d_inode (ovl_dentry_real (dentry))->i_writecount) <0 ) +return -ETXTBSY ; ctx.stat.size =0 ; + }
Maybe we should check this for open(with writable flag) not only for truncate case, right?
Thanks,
Chengguang
Quoted 8 lines
fs/overlayfs/copy_up.c +0
-0if (S_ISLNK (ctx.stat.mode)) { ctx.link =vfs_get_link (ctx.lowerpath.dentry, &done);Signature · 1 line
2.27.0
Miklos Szeredi
maintainerQuoted 5 lines · Chengguang Xu
---- 在 星期四, 2021-04-08 19:20:42 Chengguang Xu <[email protected]> 撰写 ----
Quoted 3 lines
In overlayfs copy-up, if open flag has
O_TRUNCthen upper file will truncate to zero size, in this case we should checkVM_DENYWRITEmappings to keep compatibility with other filesystems.
Can you provide a test case for the bug that this is fixing?
Thanks,
Miklos
---- 在 星期四, 2021-04-08 19:29:55 Miklos Szeredi <[email protected]> 撰写 ----
Quoted 9 lines · Chengguang Xu
Quoted 5 lines
---- 在 星期四, 2021-04-08 19:20:42 Chengguang Xu <[email protected]> 撰写 ----
Quoted 3 lines
In overlayfs copy-up, if open flag has
O_TRUNCthen upper file will truncate to zero size, in this case we should checkVM_DENYWRITEmappings to keep compatibility with other filesystems.Can you provide a test case for the bug that this is fixing?
Execute binary file(keep running until open) in overlayfs which only has lower && open the binary file with flag O_RDWR|O_TRUNC
Expected result: open fail with -ETXTBSY
Actual result: open success
Thanks,
Chengguang
Miklos Szeredi
maintainerQuoted 9 lines · Chengguang Xu
---- 在 星期四, 2021-04-08 19:29:55 Miklos Szeredi <[email protected]> 撰写 ----
Quoted 7 lines · Chengguang Xu
Quoted 5 lines
---- 在 星期四, 2021-04-08 19:20:42 Chengguang Xu <[email protected]> 撰写 ----
Quoted 3 lines
In overlayfs copy-up, if open flag has
O_TRUNCthen upper file will truncate to zero size, in this case we should checkVM_DENYWRITEmappings to keep compatibility with other filesystems.
Quoted 8 lines
Quoted 2 lines
Can you provide a test case for the bug that this is fixing?
Execute binary file(keep running until open) in overlayfs which only has lower && open the binary file with flag O_RDWR|O_TRUNC
Expected result: open fail with
-ETXTBSYActual result: open success
Worse, it's possible to get a "Bus error" with just execute and write on an overlayfs file, which i_writecount is supposed to protect.
The reason is that the put_write_access() call in __vma_link_file() assumes an already negative writecount, but because of the vm_file
shuffle in ovl_mmap() that's not guaranteed. There's even a comment
about exactly this situation in mmap():
/* ->mmap() can change vma->vm_file, but must guarantee that
* vma_link() below can deny write-access if VM_DENYWRITE is set
* and map writably if VM_SHARED is set. This usually means the
* new file must not have been exposed to user-space, yet. */
The attached patch fixes this, but not your original bug.
That could be addressed by checking the writecount on *both* lower and upper for open for write/truncate. Note: this could be checked before copy-up, but that's not reliable alone, because the copy up could happen due to meta-data update, for example, and then the open/truncate wouldn't trigger the writecount check.
Something like the second attached patch?
Thanks,
Miklos
fs/overlayfs/file.c +17 -0
@@ -422,6 +422,7 @@ static int ovl_mmap(struct file *file, struct vm_area_struct *vma) {struct file *realfile = file->private_data;const struct cred *old_cred; +vm_flags_t vm_flags = vma->vm_flags;int ret;if (!realfile->f_op->mmap)
@@ -430,6 +431,15 @@ static int ovl_mmap(struct file *file, struct vm_area_struct *vma)if (WARN_ON (file != vma->vm_file))return -EIO ; +/* Get temporary denial counts on realfile */ +if (vm_flags &VM_DENYWRITE && + (ret =deny_write_access (realfile))) + goto out; + +if (vm_flags &VM_SHARED && + (ret =mapping_map_writable (file->f_mapping))) + goto allow_write; + vma->vm_file =get_file (realfile); old_cred =ovl_override_creds (file_inode (file)->i_sb);
@@ -446,6 +456,13 @@ static int ovl_mmap(struct file *file, struct vm_area_struct *vma)ovl_file_accessed (file); +/* Undo temporary denial counts */ +if (vm_flags &VM_SHARED ) +mapping_unmap_writable (realfile->f_mapping); +allow_write: +if (vm_flags &VM_DENYWRITE ) +allow_write_access (realfile); +out:return ret; }
mm/mmap.c +10 -3
@@ -659,11 +659,18 @@ static void __vma_link_file(struct vm_area_struct *vma) file = vma->vm_file;if (file) {struct address_space *mapping = file->f_mapping; +struct inode *inode =file_inode (file); -if (vma->vm_flags &VM_DENYWRITE ) -put_write_access (file_inode (file)); -if (vma->vm_flags &VM_SHARED ) +if (vma->vm_flags &VM_DENYWRITE ) { +/* This is an unconditional deny_write_access() */ +WARN_ON (atomic_read (&inode->i_writecount) >0 ); +put_write_access (inode); + } +if (vma->vm_flags &VM_SHARED ) { +/* This is an unconditional mapping_map_writable() */ +WARN_ON (atomic_read (&mapping->i_mmap_writable) <0 );mapping_allow_writable (mapping); + }flush_dcache_mmap_lock (mapping);vma_interval_tree_insert (vma, &mapping->i_mmap);
fs/overlayfs/file.c +9 -0
@@ -144,8 +144,17 @@ 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 *lowerinode, *upperinode;int err; + lowerinode =ovl_inode_lower (inode); + upperinode =ovl_inode_upper (inode); + +if (((file->f_mode &FMODE_WRITE ) || file->f_flags &O_TRUNC ) && + ((lowerinode &&atomic_read (&lowerinode->i_writecount) <0 ) || + (upperinode &&atomic_read (&upperinode->i_writecount) <0 ))) +return -ETXTBSY ; + err =ovl_maybe_copy_up (file_dentry (file), file->f_flags);if (err)return err;
---- 在 星期四, 2021-04-08 23:03:39 Miklos Szeredi <[email protected]> 撰写 ----
Quoted 35 lines · Chengguang Xu
Quoted 9 lines
---- 在 星期四, 2021-04-08 19:29:55 Miklos Szeredi <[email protected]> 撰写 ----
Quoted 7 lines · Chengguang Xu
Quoted 5 lines
---- 在 星期四, 2021-04-08 19:20:42 Chengguang Xu <[email protected]> 撰写 ----
Quoted 3 lines
In overlayfs copy-up, if open flag has
O_TRUNCthen upper file will truncate to zero size, in this case we should checkVM_DENYWRITEmappings to keep compatibility with other filesystems.Quoted 8 lines
Quoted 2 lines
Can you provide a test case for the bug that this is fixing?
Execute binary file(keep running until open) in overlayfs which only has lower && open the binary file with flag O_RDWR|O_TRUNC
Expected result: open fail with
-ETXTBSYActual result: open success
Worse, it's possible to get a "Bus error" with just execute and write on an overlayfs file, which
i_writecountis supposed to protect.The reason is that the
put_write_access()call in__vma_link_file()assumes an already negative writecount, but because of thevm_file
shuffle inovl_mmap()that's not guaranteed. There's even a comment
about exactly this situation inmmap():/* ->mmap() can change
vma->vm_file, but must guarantee that
*vma_link()below can deny write-access ifVM_DENYWRITEis set
* and map writably ifVM_SHAREDis set. This usually means the
* new file must not have been exposed to user-space, yet. */The attached patch fixes this, but not your original bug.
Quoted 8 lines
That could be addressed by checking the writecount on *both* lower and upper for open for write/truncate. Note: this could be checked before copy-up, but that's not reliable alone, because the copy up could happen due to meta-data update, for example, and then the open/truncate wouldn't trigger the writecount check.
Something like the second attached patch?
Yeah, I noticed that too just after posted my previous patch. However, rethink these two cases, in practice we share lower layers in most use cases especially in container use case. So if we check VM_DENYWRITE of lower file strictly, it may cause interferes between container instances. Maybe only checking upper file will be better option?
Thanks,
Chengguang
Miklos Szeredi
maintainerQuoted 46 lines · Chengguang Xu
---- 在 星期四, 2021-04-08 23:03:39 Miklos Szeredi <[email protected]> 撰写 ----
Quoted 35 lines · Chengguang Xu
Quoted 9 lines
---- 在 星期四, 2021-04-08 19:29:55 Miklos Szeredi <[email protected]> 撰写 ----
Quoted 7 lines · Chengguang Xu
Quoted 5 lines
---- 在 星期四, 2021-04-08 19:20:42 Chengguang Xu <[email protected]> 撰写 ----
Quoted 3 lines
In overlayfs copy-up, if open flag has
O_TRUNCthen upper file will truncate to zero size, in this case we should checkVM_DENYWRITEmappings to keep compatibility with other filesystems.Quoted 8 lines
Quoted 2 lines
Can you provide a test case for the bug that this is fixing?
Execute binary file(keep running until open) in overlayfs which only has lower && open the binary file with flag O_RDWR|O_TRUNC
Expected result: open fail with
-ETXTBSYActual result: open success
Worse, it's possible to get a "Bus error" with just execute and write on an overlayfs file, which
i_writecountis supposed to protect.The reason is that the
put_write_access()call in__vma_link_file()assumes an already negative writecount, but because of thevm_file
shuffle inovl_mmap()that's not guaranteed. There's even a comment
about exactly this situation inmmap():/* ->mmap() can change
vma->vm_file, but must guarantee that
*vma_link()below can deny write-access ifVM_DENYWRITEis set
* and map writably ifVM_SHAREDis set. This usually means the
* new file must not have been exposed to user-space, yet. */The attached patch fixes this, but not your original bug.
Quoted 8 lines
That could be addressed by checking the writecount on *both* lower and upper for open for write/truncate. Note: this could be checked before copy-up, but that's not reliable alone, because the copy up could happen due to meta-data update, for example, and then the open/truncate wouldn't trigger the writecount check.
Something like the second attached patch?
Quoted 6 lines
Yeah, I noticed that too just after posted my previous patch. However, rethink these two cases, in practice we share lower layers in most use cases especially in container use case. So if we check
VM_DENYWRITEof lower file strictly, it may cause interferes between container instances. Maybe only checking upper file will be better option?
Yes.
My patch to fix the SIGBUS is also incomplete as there's still a race window between releasing the temporary writecount and the __vma_link()
that acquires the final count. This requires major surgery to fix
properly :(
Thanks,
Miklos
---- 在 星期二, 2021-04-13 16:47:53 Miklos Szeredi <[email protected]> 撰写 ----
Quoted 54 lines · Chengguang Xu
Quoted 46 lines
---- 在 星期四, 2021-04-08 23:03:39 Miklos Szeredi <[email protected]> 撰写 ----
Quoted 35 lines · Chengguang Xu
Quoted 9 lines
---- 在 星期四, 2021-04-08 19:29:55 Miklos Szeredi <[email protected]> 撰写 ----
Quoted 7 lines · Chengguang Xu
Quoted 5 lines
---- 在 星期四, 2021-04-08 19:20:42 Chengguang Xu <[email protected]> 撰写 ----
Quoted 3 lines
In overlayfs copy-up, if open flag has
O_TRUNCthen upper file will truncate to zero size, in this case we should checkVM_DENYWRITEmappings to keep compatibility with other filesystems.Quoted 8 lines
Quoted 2 lines
Can you provide a test case for the bug that this is fixing?
Execute binary file(keep running until open) in overlayfs which only has lower && open the binary file with flag O_RDWR|O_TRUNC
Expected result: open fail with
-ETXTBSYActual result: open success
Worse, it's possible to get a "Bus error" with just execute and write on an overlayfs file, which
i_writecountis supposed to protect.The reason is that the
put_write_access()call in__vma_link_file()assumes an already negative writecount, but because of thevm_file
shuffle inovl_mmap()that's not guaranteed. There's even a comment
about exactly this situation inmmap():/* ->mmap() can change
vma->vm_file, but must guarantee that
*vma_link()below can deny write-access ifVM_DENYWRITEis set
* and map writably ifVM_SHAREDis set. This usually means the
* new file must not have been exposed to user-space, yet. */The attached patch fixes this, but not your original bug.
Quoted 8 lines
That could be addressed by checking the writecount on *both* lower and upper for open for write/truncate. Note: this could be checked before copy-up, but that's not reliable alone, because the copy up could happen due to meta-data update, for example, and then the open/truncate wouldn't trigger the writecount check.
Something like the second attached patch?
Quoted 6 lines
Yeah, I noticed that too just after posted my previous patch. However, rethink these two cases, in practice we share lower layers in most use cases especially in container use case. So if we check
VM_DENYWRITEof lower file strictly, it may cause interferes between container instances. Maybe only checking upper file will be better option?
Quoted 7 lines
Yes.
My patch to fix the SIGBUS is also incomplete as there's still a race window between releasing the temporary writecount and the
__vma_link()
that acquires the final count. This requires major surgery to fix
properly :(
Hi Miklos,
How about to fix something like attached patch?
Thanks,
Chengguang
From 2bf3c60d86dc1b86f5ae22cd4e1f5d39603143c6 Mon Sep 17 00:00:00 2001
From: Chengguang Xu <[email protected]>
Date: Tue, 20 Apr 2021 09:51:36 +0800
Subject: [PATCH] ovl: test
--- fs/overlayfs/file.c | 29 +++++++++++++++++++++++++++-- mm/mmap.c | 3 ++- 2 files changed, 29 insertions(+), 3 deletions(-)
fs/overlayfs/file.c +27 -2
@@ -422,6 +422,7 @@ static int ovl_mmap(struct file *file, struct vm_area_struct *vma) {struct file *realfile = file->private_data;const struct cred *old_cred; +vm_flags_t vm_flags = vma->vm_flags;int ret;if (!realfile->f_op->mmap)
@@ -430,22 +431,46 @@ static int ovl_mmap(struct file *file, struct vm_area_struct *vma)if (WARN_ON (file != vma->vm_file))return -EIO ; +/* Get temporary denial counts on realfile */ +if (vm_flags &VM_DENYWRITE && + (ret =deny_write_access (realfile))) + goto out; + +if (vm_flags &VM_SHARED && + (ret =mapping_map_writable (realfile->f_mapping))) + goto allow_write; + vma->vm_file =get_file (realfile); old_cred =ovl_override_creds (file_inode (file)->i_sb); ret =call_mmap (vma->vm_file, vma);revert_creds (old_cred); +ovl_file_accessed (file); +if (ret) {/* Drop reference count from new vm_file value */ fput (realfile); + vma->vm_file = file; + +/* Undo temporary denial counts */ +if (vm_flags &VM_SHARED ) +mapping_unmap_writable (realfile->f_mapping); +allow_write: +if (vm_flags &VM_DENYWRITE ) +allow_write_access (realfile); + }else { +if (vm_flags &VM_DENYWRITE ) +allow_write_access (file); +if (vm_flags &VM_SHARED ) +mapping_unmap_writable (file->f_mapping); +/* Drop reference count from previous vm_file value */ fput (file); } -ovl_file_accessed (file); - +out:return ret; }
mm/mmap.c +2 -1
@@ -1806,6 +1806,8 @@ unsigned long mmap_region(struct file *file, unsigned long addr,if (error) goto unmap_and_free_vma; + file = vma->vm_file; +/* Can addr have changed?? * * Answer: Yes, several device drivers can do it in their
@@ -1864,7 +1866,6 @@ unsigned long mmap_region(struct file *file, unsigned long addr,if (vm_flags &VM_DENYWRITE )allow_write_access (file); } - file = vma->vm_file; out:perf_event_mmap (vma);
Signature · 1 line
2.27.0