fs: clear file set[ug]id when writing via mmap
Subsystems (3)
PATCH 1/1 fs: clear file set[ug]id when writing via mmap
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() directly but not when writing to a shared mmap on the file. This could allow the file writer to gain privileges by changing the binary without losing the setuid/setgid bits.
--- mm/memory.c | 1 + 1 file changed, 1 insertion(+)
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); +file_remove_privs (vma->vm_file); }return VM_FAULT_WRITE ;
Signature · 6 lines
1.9.1 -- Kees Cook Chrome OS Security
Andrew Morton
maintainerQuoted 17 lines · On Thu, 19 Nov 2015 16:10:43 -0800 Kees Cook <[email protected]> wrote:
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()directly but not when writing to a shared mmap on the file. This could allow the file writer to gain privileges by changing the binary without losing the setuid/setgid bits.Signed-off-by Kees Cook <[email protected]>--- mm/memory.c | 1 + 1 file changed, 1 insertion(+)mm/memory.c +0
-0
mm/memory.c static inline int wp_page_reuse(struct mm_struct *mm, · line 2042
Quoted 7 lines
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); +file_remove_privs (vma->vm_file); }return VM_FAULT_WRITE ;
file_remove_privs() is depressingly heavyweight. You'd think there was some more lightweight way of caching the fact that we've already done this.
Dumb question: can we run file_remove_privs() once, when the file is opened writably, rather than for each and every write into each page?
Also, the proposed patch drops the file_remove_privs() return value on the floor and we just go ahead with the modification. How come?
--
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 25 lines · Andrew Morton
Quoted 23 lines · On Thu, 19 Nov 2015 16:10:43 -0800 Kees Cook <[email protected]> wrote:
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()directly but not when writing to a shared mmap on the file. This could allow the file writer to gain privileges by changing the binary without losing the setuid/setgid bits.Signed-off-by Kees Cook <[email protected]>--- mm/memory.c | 1 + 1 file changed, 1 insertion(+)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); +file_remove_privs (vma->vm_file); }
Quoted 5 lines
mm/memory.c static inline int wp_page_reuse(struct mm_struct *mm, · line 2042
Quoted 1 line
mm/memory.c +0
-0return VM_FAULT_WRITE ;
file_remove_privs()is depressingly heavyweight. You'd think there was some more lightweight way of caching the fact that we've already done this.
In theory, the IS_NOSEC(inode) should be fast. Perhaps track it in the vma or file struct?
Quoted 2 lines
Dumb question: can we run
file_remove_privs()once, when the file is opened writably, rather than for each and every write into each page?
This got discussed briefly, but I can't remember why it got shot down.
Quoted 2 lines
Also, the proposed patch drops the
file_remove_privs()return value on the floor and we just go ahead with the modification. How come?
Oh, excellent catch. If it can't drop it, it shouldn't be writable. I'm not sure what the right abort scenario is in wp_page_reuse. Maybe move this to start of wp_page_shared instead?
-Kees
Signature · 8 lines
Kees Cook Chrome OS 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>
Hi Kees,
Quoted 17 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()directly but not when writing to a shared mmap on the file. This could allow the file writer to gain privileges by changing the binary without losing the setuid/setgid bits.Signed-off-by Kees Cook <[email protected]>--- mm/memory.c | 1 + 1 file changed, 1 insertion(+)mm/memory.c +0
-0
mm/memory.c static inline int wp_page_reuse(struct mm_struct *mm, · line 2039
Quoted 4 lines
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); +file_remove_privs (vma->vm_file);
I thought you said in one of the early mails of this thread that it didn't work. Or maybe I misunderstood.
Also, don't you think we should move that into the if (!page_mkwrite) just like for the time update ?
Willy
Quoted 21 lines · Willy Tarreau
Hi Kees,
Quoted 18 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()directly but not when writing to a shared mmap on the file. This could allow the file writer to gain privileges by changing the binary without losing the setuid/setgid bits.Signed-off-by Kees Cook <[email protected]>--- mm/memory.c | 1 + 1 file changed, 1 insertion(+)mm/memory.c +0
-0@@ -2036,6 +2036,7 @@ static inline int wp_page_reuse(struct mm_struct *mm,
Quoted 6 lines
mm/memory.c static inline int wp_page_reuse(struct mm_struct *mm, · line 2039
Quoted 3 lines
mm/memory.c +1
-0if (!page_mkwrite)file_update_time (vma->vm_file); +file_remove_privs (vma->vm_file);I thought you said in one of the early mails of this thread that it didn't work. Or maybe I misunderstood.
I had a think-o in my earlier attempts. I understood the meaning of page_mkwrite incorrectly.
Quoted 2 lines
Also, don't you think we should move that into the if (!page_mkwrite) just like for the time update ?
Nope, page_mkwrite indicates if there was a vmops call to page_mkwrite. In this case, it means "I will update the file time if the filesystem driver didn't take care of it like it should". For file_remove_privs, we want to always do it, since we should not depend on filesystems to do it.
-Kees
Signature · 8 lines
Kees Cook Chrome OS 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 32 lines · Kees Cook
Quoted 21 lines · Willy Tarreau
Hi Kees,
Quoted 18 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()directly but not when writing to a shared mmap on the file. This could allow the file writer to gain privileges by changing the binary without losing the setuid/setgid bits.Signed-off-by Kees Cook <[email protected]>--- mm/memory.c | 1 + 1 file changed, 1 insertion(+)mm/memory.c +0
-0@@ -2036,6 +2036,7 @@ static inline int wp_page_reuse(struct mm_struct *mm,Quoted 6 lines
mm/memory.c static inline int wp_page_reuse(struct mm_struct *mm, · line 2039
Quoted 3 lines
mm/memory.c +1
-0if (!page_mkwrite)file_update_time (vma->vm_file); +file_remove_privs (vma->vm_file);I thought you said in one of the early mails of this thread that it didn't work. Or maybe I misunderstood.
I had a think-o in my earlier attempts. I understood the meaning of
page_mkwriteincorrectly.
Quoted 8 lines
Quoted 2 lines
Also, don't you think we should move that into the if (!page_mkwrite) just like for the time update ?
Nope,
page_mkwriteindicates if there was a vmops call topage_mkwrite. In this case, it means "I will update the file time if the filesystem driver didn't take care of it like it should". Forfile_remove_privs, we want to always do it, since we should not depend on filesystems to do it.
Ah OK, thanks for the explanation, I didn't understand it like this at all last time I read it.
Cheers,
Willy
Quoted 6 lines · Willy Tarreau
Quoted 6 lines
mm/memory.c +0
-0@@ -2036,6 +2036,7 @@ static inline int wp_page_reuse(struct mm_struct *mm,
Quoted 6 lines
mm/memory.c static inline int wp_page_reuse(struct mm_struct *mm, · line 2039
Quoted 3 lines
mm/memory.c +1
-0if (!page_mkwrite)file_update_time (vma->vm_file); +file_remove_privs (vma->vm_file);I thought you said in one of the early mails of this thread that it didn't work. Or maybe I misunderstood.
OK never mind for this one I just saw the other mail where you said the test is OK now. But I'm still worried about the performance so the other point below remains :
Quoted 2 lines
Also, don't you think we should move that into the if (!page_mkwrite) just like for the time update ?
Thanks!
Willy
Jan Kara
reviewerQuoted 8 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()directly but not when writing to a shared mmap on the file. This could allow the file writer to gain privileges by changing the binary without losing the setuid/setgid bits.Signed-off-by Kees Cook <[email protected]>
So I had another look at this and now I understand why we didn't do it from the start:
To call file_remove_privs() safely, we need to hold inode->i_mutex since that operations is going to modify file mode / extended attributes and i_mutex protects those. However we cannot get i_mutex in the page fault path as that ranks above mmap_sem which we hold during the whole page fault.
So calling file_remove_privs() when opening the file is probably as good as it can get. It doesn't catch the case when suid bits / IMA attrs are set while the file is already open but I don't see easy way around this.
BTW: This is another example where page fault locking is constraining us and life would be simpler for filesystems we they get called without mmap_sem held...
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 19 lines · Jan Kara
Quoted 8 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()directly but not when writing to a shared mmap on the file. This could allow the file writer to gain privileges by changing the binary without losing the setuid/setgid bits.Signed-off-by Kees Cook <[email protected]>So I had another look at this and now I understand why we didn't do it from the start:
To call
file_remove_privs()safely, we need to holdinode->i_mutexsince that operations is going to modify file mode / extended attributes andi_mutexprotects those. However we cannot geti_mutexin the page fault path as that ranks abovemmap_semwhich we hold during the whole page fault.
Quoted 3 lines
So calling
file_remove_privs()when opening the file is probably as good as it can get. It doesn't catch the case when suid bits / IMA attrs are set while the file is already open but I don't see easy way around this.
Could we perhaps do this on mmap MAP_WRITE instead of open, and simply deny adding these attributes if a file is mapped for write?
That would seem to be a little more compatible with what we already do, and guards against the races you mention as well.
Eric
--
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 10 lines · Jan Kara
Quoted 8 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()directly but not when writing to a shared mmap on the file. This could allow the file writer to gain privileges by changing the binary without losing the setuid/setgid bits.Signed-off-by Kees Cook <[email protected]>
Quoted 8 lines
So I had another look at this and now I understand why we didn't do it from the start:
To call
file_remove_privs()safely, we need to holdinode->i_mutexsince that operations is going to modify file mode / extended attributes andi_mutexprotects those. However we cannot geti_mutexin the page fault path as that ranks abovemmap_semwhich we hold during the whole page fault.
Ah, I see the notation in __generic_file_write_iter about i_mutex. Should file_remove_privs() get some debug annotation to catch callers that don't hold that mutex? (That would have alerted me much earlier.)
Quoted 3 lines
So calling
file_remove_privs()when opening the file is probably as good as it can get. It doesn't catch the case when suid bits / IMA attrs are set while the file is already open but I don't see easy way around this.
I agree with Eric: mmap time seems like the right place.
Quoted 8 lines
BTW: This is another example where page fault locking is constraining us and life would be simpler for filesystems we they get called without
mmap_semheld...Honza
--
Jan Kara <[email protected]>
SUSE Labs, CR
-Kees
Signature · 2 lines
Kees Cook Chrome OS & Brillo Security