fs: clear file privilege bits when mmap writing
Subsystems (3)
PATCH v4 1/1 fs: clear file privilege bits when mmap writing
Normally, when a user can modify a file that has setuid or setgid bits, those bits are cleared when they are not the file owner or a member of the group. This is enforced when using write and truncate but not when writing to a shared mmap on the file. This could allow the file writer to gain privileges by changing a binary without losing the setuid/setgid/caps bits.
Changing the bits requires holding inode->i_mutex, so it cannot be done during the page fault (due to mmap_sem being held during the fault). We could do this during vm_mmap_pgoff, but that would need coverage in mprotect as well, but to check for MAP_SHARED, we'd need to hold mmap_sem again.
Instead, detect the need to clear the bits during the page fault, and actually remove the bits during final fput. Since the file was open for writing, it wouldn't have been possible to execute it yet.
---
Here's another way? I wonder which of these will actually work. I wish we could reject writes if file_remove_privs() fails.
v4:
- delay removal instead of still needing mmap_sem for mprotect, yalin v3:
- move outside of mmap_sem for real now, fengguang
- check return code of file_remove_privs, akpm
v2:
- move to mmap from fault handler, jack
--- fs/file_table.c | 8 ++++++++ include/linux/fs.h | 1 + mm/memory.c | 1 + 3 files changed, 10 insertions(+)
fs/file_table.c +8 -0
@@ -191,6 +191,14 @@ static void __fput(struct file *file)might_sleep (); +/* +* XXX: While avoiding mmap_sem, we've already been written to. +* We must ignore the return value, since we can't reject the +* write. +*/ +if (unlikely (file->f_remove_privs)) +file_remove_privs (file); +fsnotify_close (file);/* * The function eventpoll_release() should be the first called
include/linux/fs.h +1 -0
@@ -872,6 +872,7 @@ struct file {struct list_head f_tfile_llink;#endif /* #ifdef CONFIG_EPOLL */ struct address_space *f_mapping; +bool f_remove_privs; } __attribute__((aligned(4))); /* lest something weird decides that 2 is OK */struct file_handle {
mm/memory.c +1 -0
@@ -2036,6 +2036,7 @@ static inline int wp_page_reuse(struct mm_struct *mm,if (!page_mkwrite)file_update_time (vma->vm_file); + vma->vm_file->f_remove_privs = true; }return VM_FAULT_WRITE ;
Signature · 12 lines
1.9.1 -- Kees Cook Chrome OS & Brillo Security -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 9 lines · Kees Cook
Changing the bits requires holding
inode->i_mutex, so it cannot be done during the page fault (due tommap_sembeing held during the fault). We could do this duringvm_mmap_pgoff, but that would need coverage in mprotect as well, but to check forMAP_SHARED, we'd need to holdmmap_semagain.Instead, detect the need to clear the bits during the page fault, and actually remove the bits during final fput. Since the file was open for writing, it wouldn't have been possible to execute it yet.
Did you check that inode->i_mutex is held when final fput() is called?
Did you check a case where the file is copied between mmap() and final fput() (i.e. open() for write, mmap() for write, sleep forever waiting for the file owner to copy the content and attributes of the modified file)?
Jan Kara
reviewerQuoted 13 lines · Kees Cook
Normally, when a user can modify a file that has setuid or setgid bits, those bits are cleared when they are not the file owner or a member of the group. This is enforced when using write and truncate but not when writing to a shared mmap on the file. This could allow the file writer to gain privileges by changing a binary without losing the setuid/setgid/caps bits.
Changing the bits requires holding
inode->i_mutex, so it cannot be done during the page fault (due tommap_sembeing held during the fault). We could do this duringvm_mmap_pgoff, but that would need coverage in mprotect as well, but to check forMAP_SHARED, we'd need to holdmmap_semagain.
Quoted 8 lines
Instead, detect the need to clear the bits during the page fault, and actually remove the bits during final fput. Since the file was open for writing, it wouldn't have been possible to execute it yet.
Signed-off-by Kees Cook <[email protected]>---
Here's another way? I wonder which of these will actually work. I wish we could reject writes iffile_remove_privs()fails.
Yeah, the fact that we cannot do anything with file_remove_privs() failure is rather unfortunate. So open for writing may be the best choice for file_remove_privs() in the end? It's not perfect but it looks like the least problematic solution.
Frankly writeable files that have SUID / SGID bits set are IMHO problems on its own, with IMA attrs which are handled by file_remove_privs() as well things may be somewhat different.
Quoted 6 lines
fs/file_table.c +0
-0@@ -191,6 +191,14 @@ static void __fput(struct file *file)
fs/file_table.c static void __fput(struct file *file) · line 201
Quoted 10 lines
fs/file_table.c +8
-0might_sleep (); +/* +* XXX: While avoiding mmap_sem, we've already been written to. +* We must ignore the return value, since we can't reject the +* write. +*/ +if (unlikely (file->f_remove_privs)) +file_remove_privs (file); +
You're missing i_mutex locking again ;).
Honza
Signature · 8 lines
Jan Kara <[email protected]> SUSE Labs, CR -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 23 lines · Jan Kara
Quoted 13 lines · Kees Cook
Normally, when a user can modify a file that has setuid or setgid bits, those bits are cleared when they are not the file owner or a member of the group. This is enforced when using write and truncate but not when writing to a shared mmap on the file. This could allow the file writer to gain privileges by changing a binary without losing the setuid/setgid/caps bits.
Changing the bits requires holding
inode->i_mutex, so it cannot be done during the page fault (due tommap_sembeing held during the fault). We could do this duringvm_mmap_pgoff, but that would need coverage in mprotect as well, but to check forMAP_SHARED, we'd need to holdmmap_semagain.Quoted 8 lines
Instead, detect the need to clear the bits during the page fault, and actually remove the bits during final fput. Since the file was open for writing, it wouldn't have been possible to execute it yet.
Signed-off-by Kees Cook <[email protected]>---
Here's another way? I wonder which of these will actually work. I wish we could reject writes iffile_remove_privs()fails.
Quoted 4 lines
Yeah, the fact that we cannot do anything with
file_remove_privs()failure is rather unfortunate. So open for writing may be the best choice forfile_remove_privs()in the end? It's not perfect but it looks like the least problematic solution.
Yeah, back to just the open itself. I can't even delay this to the mmap. :(
I will do a v5. :)
-Kees
Quoted 28 lines
Frankly writeable files that have SUID / SGID bits set are IMHO problems on its own, with IMA attrs which are handled by
file_remove_privs()as well things may be somewhat different.Quoted 6 lines
fs/file_table.c +0
-0@@ -191,6 +191,14 @@ static void __fput(struct file *file)fs/file_table.c static void __fput(struct file *file) · line 201
Quoted 10 lines
fs/file_table.c +8
-0might_sleep (); +/* +* XXX: While avoiding mmap_sem, we've already been written to. +* We must ignore the return value, since we can't reject the +* write. +*/ +if (unlikely (file->f_remove_privs)) +file_remove_privs (file); +You're missing
i_mutexlocking again ;).Honza
--
Jan Kara <[email protected]>
SUSE Labs, CR
Signature · 8 lines
Kees Cook Chrome OS & Brillo Security -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>