diff options
author | Oleg Nesterov <oleg@redhat.com> | 2009-04-23 19:01:56 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2009-04-24 10:39:45 -0400 |
commit | 8c652f96d3852b97a49c331cd0bb02d22f3cb31b (patch) | |
tree | 9752969f2ae7312026bb4037ecd950dafd403d9b /fs/compat.c | |
parent | 091069740304c979f957ceacec39c461d0192158 (diff) |
do_execve() must not clear fs->in_exec if it was set by another thread
If do_execve() fails after check_unsafe_exec(), it clears fs->in_exec
unconditionally. This is wrong if we race with our sub-thread which
also does do_execve:
Two threads T1 and T2 and another process P, all share the same
->fs.
T1 starts do_execve(BAD_FILE). It calls check_unsafe_exec(), since
->fs is shared, we set LSM_UNSAFE but not ->in_exec.
P exits and decrements fs->users.
T2 starts do_execve(), calls check_unsafe_exec(), now ->fs is not
shared, we set fs->in_exec.
T1 continues, open_exec(BAD_FILE) fails, we clear ->in_exec and
return to the user-space.
T1 does clone(CLONE_FS /* without CLONE_THREAD */).
T2 continues without LSM_UNSAFE_SHARE while ->fs is shared with
another process.
Change check_unsafe_exec() to return res = 1 if we set ->in_exec, and change
do_execve() to clear ->in_exec depending on res.
When do_execve() suceeds, it is safe to clear ->in_exec unconditionally.
It can be set only if we don't share ->fs with another process, and since
we already killed all sub-threads either ->in_exec == 0 or we are the
only user of this ->fs.
Also, we do not need fs->lock to clear fs->in_exec.
Signed-off-by: Oleg Nesterov <oleg@redhat.com>
Acked-by: Roland McGrath <roland@redhat.com>
Acked-by: Hugh Dickins <hugh@veritas.com>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'fs/compat.c')
-rw-r--r-- | fs/compat.c | 11 |
1 files changed, 5 insertions, 6 deletions
diff --git a/fs/compat.c b/fs/compat.c index 379a399bf5c3..681ed81e6be0 100644 --- a/fs/compat.c +++ b/fs/compat.c | |||
@@ -1476,6 +1476,7 @@ int compat_do_execve(char * filename, | |||
1476 | struct linux_binprm *bprm; | 1476 | struct linux_binprm *bprm; |
1477 | struct file *file; | 1477 | struct file *file; |
1478 | struct files_struct *displaced; | 1478 | struct files_struct *displaced; |
1479 | bool clear_in_exec; | ||
1479 | int retval; | 1480 | int retval; |
1480 | 1481 | ||
1481 | retval = unshare_files(&displaced); | 1482 | retval = unshare_files(&displaced); |
@@ -1498,8 +1499,9 @@ int compat_do_execve(char * filename, | |||
1498 | goto out_unlock; | 1499 | goto out_unlock; |
1499 | 1500 | ||
1500 | retval = check_unsafe_exec(bprm); | 1501 | retval = check_unsafe_exec(bprm); |
1501 | if (retval) | 1502 | if (retval < 0) |
1502 | goto out_unlock; | 1503 | goto out_unlock; |
1504 | clear_in_exec = retval; | ||
1503 | 1505 | ||
1504 | file = open_exec(filename); | 1506 | file = open_exec(filename); |
1505 | retval = PTR_ERR(file); | 1507 | retval = PTR_ERR(file); |
@@ -1546,9 +1548,7 @@ int compat_do_execve(char * filename, | |||
1546 | goto out; | 1548 | goto out; |
1547 | 1549 | ||
1548 | /* execve succeeded */ | 1550 | /* execve succeeded */ |
1549 | write_lock(¤t->fs->lock); | ||
1550 | current->fs->in_exec = 0; | 1551 | current->fs->in_exec = 0; |
1551 | write_unlock(¤t->fs->lock); | ||
1552 | current->in_execve = 0; | 1552 | current->in_execve = 0; |
1553 | mutex_unlock(¤t->cred_exec_mutex); | 1553 | mutex_unlock(¤t->cred_exec_mutex); |
1554 | acct_update_integrals(current); | 1554 | acct_update_integrals(current); |
@@ -1568,9 +1568,8 @@ out_file: | |||
1568 | } | 1568 | } |
1569 | 1569 | ||
1570 | out_unmark: | 1570 | out_unmark: |
1571 | write_lock(¤t->fs->lock); | 1571 | if (clear_in_exec) |
1572 | current->fs->in_exec = 0; | 1572 | current->fs->in_exec = 0; |
1573 | write_unlock(¤t->fs->lock); | ||
1574 | 1573 | ||
1575 | out_unlock: | 1574 | out_unlock: |
1576 | current->in_execve = 0; | 1575 | current->in_execve = 0; |