exec: do not sleep in TASK_TRACED under ->cred_guard_mutex
Tom Horsley reports that his debugger hangs when it tries to read /proc/pid_of_tracee/maps, this happens since "mm_for_maps: take ->cred_guard_mutex to fix the race with exec" 04b836cbf19e885f8366bccb2e4b0474346c02d commit in 2.6.31. But the root of the problem lies in the fact that do_execve() path calls tracehook_report_exec() which can stop if the tracer sets PT_TRACE_EXEC. The tracee must not sleep in TASK_TRACED holding this mutex. Even if we remove ->cred_guard_mutex from mm_for_maps() and proc_pid_attr_write(), another task doing PTRACE_ATTACH should not hang until it is killed or the tracee resumes. With this patch do_execve() does not use ->cred_guard_mutex directly and we do not hold it throughout, instead: - introduce prepare_bprm_creds() helper, it locks the mutex and calls prepare_exec_creds() to initialize bprm->cred. - install_exec_creds() drops the mutex after commit_creds(), and thus before tracehook_report_exec()->ptrace_stop(). or, if exec fails, free_bprm() drops this mutex when bprm->cred != NULL which indicates install_exec_creds() was not called. Reported-by: Tom Horsley <tom.horsley@att.net> Signed-off-by: Oleg Nesterov <oleg@redhat.com> Acked-by: David Howells <dhowells@redhat.com> Cc: Roland McGrath <roland@redhat.com> Cc: James Morris <jmorris@namei.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
This commit is contained in:
Родитель
dd5d241ea9
Коммит
a2a8474c3f
17
fs/compat.c
17
fs/compat.c
|
@ -1485,20 +1485,15 @@ int compat_do_execve(char * filename,
|
||||||
if (!bprm)
|
if (!bprm)
|
||||||
goto out_files;
|
goto out_files;
|
||||||
|
|
||||||
retval = -ERESTARTNOINTR;
|
retval = prepare_bprm_creds(bprm);
|
||||||
if (mutex_lock_interruptible(¤t->cred_guard_mutex))
|
if (retval)
|
||||||
goto out_free;
|
goto out_free;
|
||||||
current->in_execve = 1;
|
|
||||||
|
|
||||||
retval = -ENOMEM;
|
|
||||||
bprm->cred = prepare_exec_creds();
|
|
||||||
if (!bprm->cred)
|
|
||||||
goto out_unlock;
|
|
||||||
|
|
||||||
retval = check_unsafe_exec(bprm);
|
retval = check_unsafe_exec(bprm);
|
||||||
if (retval < 0)
|
if (retval < 0)
|
||||||
goto out_unlock;
|
goto out_free;
|
||||||
clear_in_exec = retval;
|
clear_in_exec = retval;
|
||||||
|
current->in_execve = 1;
|
||||||
|
|
||||||
file = open_exec(filename);
|
file = open_exec(filename);
|
||||||
retval = PTR_ERR(file);
|
retval = PTR_ERR(file);
|
||||||
|
@ -1547,7 +1542,6 @@ int compat_do_execve(char * filename,
|
||||||
/* execve succeeded */
|
/* execve succeeded */
|
||||||
current->fs->in_exec = 0;
|
current->fs->in_exec = 0;
|
||||||
current->in_execve = 0;
|
current->in_execve = 0;
|
||||||
mutex_unlock(¤t->cred_guard_mutex);
|
|
||||||
acct_update_integrals(current);
|
acct_update_integrals(current);
|
||||||
free_bprm(bprm);
|
free_bprm(bprm);
|
||||||
if (displaced)
|
if (displaced)
|
||||||
|
@ -1567,10 +1561,7 @@ out_file:
|
||||||
out_unmark:
|
out_unmark:
|
||||||
if (clear_in_exec)
|
if (clear_in_exec)
|
||||||
current->fs->in_exec = 0;
|
current->fs->in_exec = 0;
|
||||||
|
|
||||||
out_unlock:
|
|
||||||
current->in_execve = 0;
|
current->in_execve = 0;
|
||||||
mutex_unlock(¤t->cred_guard_mutex);
|
|
||||||
|
|
||||||
out_free:
|
out_free:
|
||||||
free_bprm(bprm);
|
free_bprm(bprm);
|
||||||
|
|
63
fs/exec.c
63
fs/exec.c
|
@ -1015,6 +1015,35 @@ out:
|
||||||
|
|
||||||
EXPORT_SYMBOL(flush_old_exec);
|
EXPORT_SYMBOL(flush_old_exec);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Prepare credentials and lock ->cred_guard_mutex.
|
||||||
|
* install_exec_creds() commits the new creds and drops the lock.
|
||||||
|
* Or, if exec fails before, free_bprm() should release ->cred and
|
||||||
|
* and unlock.
|
||||||
|
*/
|
||||||
|
int prepare_bprm_creds(struct linux_binprm *bprm)
|
||||||
|
{
|
||||||
|
if (mutex_lock_interruptible(¤t->cred_guard_mutex))
|
||||||
|
return -ERESTARTNOINTR;
|
||||||
|
|
||||||
|
bprm->cred = prepare_exec_creds();
|
||||||
|
if (likely(bprm->cred))
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
mutex_unlock(¤t->cred_guard_mutex);
|
||||||
|
return -ENOMEM;
|
||||||
|
}
|
||||||
|
|
||||||
|
void free_bprm(struct linux_binprm *bprm)
|
||||||
|
{
|
||||||
|
free_arg_pages(bprm);
|
||||||
|
if (bprm->cred) {
|
||||||
|
mutex_unlock(¤t->cred_guard_mutex);
|
||||||
|
abort_creds(bprm->cred);
|
||||||
|
}
|
||||||
|
kfree(bprm);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* install the new credentials for this executable
|
* install the new credentials for this executable
|
||||||
*/
|
*/
|
||||||
|
@ -1024,12 +1053,13 @@ void install_exec_creds(struct linux_binprm *bprm)
|
||||||
|
|
||||||
commit_creds(bprm->cred);
|
commit_creds(bprm->cred);
|
||||||
bprm->cred = NULL;
|
bprm->cred = NULL;
|
||||||
|
/*
|
||||||
/* cred_guard_mutex must be held at least to this point to prevent
|
* cred_guard_mutex must be held at least to this point to prevent
|
||||||
* ptrace_attach() from altering our determination of the task's
|
* ptrace_attach() from altering our determination of the task's
|
||||||
* credentials; any time after this it may be unlocked */
|
* credentials; any time after this it may be unlocked.
|
||||||
|
*/
|
||||||
security_bprm_committed_creds(bprm);
|
security_bprm_committed_creds(bprm);
|
||||||
|
mutex_unlock(¤t->cred_guard_mutex);
|
||||||
}
|
}
|
||||||
EXPORT_SYMBOL(install_exec_creds);
|
EXPORT_SYMBOL(install_exec_creds);
|
||||||
|
|
||||||
|
@ -1246,14 +1276,6 @@ int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
|
||||||
|
|
||||||
EXPORT_SYMBOL(search_binary_handler);
|
EXPORT_SYMBOL(search_binary_handler);
|
||||||
|
|
||||||
void free_bprm(struct linux_binprm *bprm)
|
|
||||||
{
|
|
||||||
free_arg_pages(bprm);
|
|
||||||
if (bprm->cred)
|
|
||||||
abort_creds(bprm->cred);
|
|
||||||
kfree(bprm);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* sys_execve() executes a new program.
|
* sys_execve() executes a new program.
|
||||||
*/
|
*/
|
||||||
|
@ -1277,20 +1299,15 @@ int do_execve(char * filename,
|
||||||
if (!bprm)
|
if (!bprm)
|
||||||
goto out_files;
|
goto out_files;
|
||||||
|
|
||||||
retval = -ERESTARTNOINTR;
|
retval = prepare_bprm_creds(bprm);
|
||||||
if (mutex_lock_interruptible(¤t->cred_guard_mutex))
|
if (retval)
|
||||||
goto out_free;
|
goto out_free;
|
||||||
current->in_execve = 1;
|
|
||||||
|
|
||||||
retval = -ENOMEM;
|
|
||||||
bprm->cred = prepare_exec_creds();
|
|
||||||
if (!bprm->cred)
|
|
||||||
goto out_unlock;
|
|
||||||
|
|
||||||
retval = check_unsafe_exec(bprm);
|
retval = check_unsafe_exec(bprm);
|
||||||
if (retval < 0)
|
if (retval < 0)
|
||||||
goto out_unlock;
|
goto out_free;
|
||||||
clear_in_exec = retval;
|
clear_in_exec = retval;
|
||||||
|
current->in_execve = 1;
|
||||||
|
|
||||||
file = open_exec(filename);
|
file = open_exec(filename);
|
||||||
retval = PTR_ERR(file);
|
retval = PTR_ERR(file);
|
||||||
|
@ -1340,7 +1357,6 @@ int do_execve(char * filename,
|
||||||
/* execve succeeded */
|
/* execve succeeded */
|
||||||
current->fs->in_exec = 0;
|
current->fs->in_exec = 0;
|
||||||
current->in_execve = 0;
|
current->in_execve = 0;
|
||||||
mutex_unlock(¤t->cred_guard_mutex);
|
|
||||||
acct_update_integrals(current);
|
acct_update_integrals(current);
|
||||||
free_bprm(bprm);
|
free_bprm(bprm);
|
||||||
if (displaced)
|
if (displaced)
|
||||||
|
@ -1360,10 +1376,7 @@ out_file:
|
||||||
out_unmark:
|
out_unmark:
|
||||||
if (clear_in_exec)
|
if (clear_in_exec)
|
||||||
current->fs->in_exec = 0;
|
current->fs->in_exec = 0;
|
||||||
|
|
||||||
out_unlock:
|
|
||||||
current->in_execve = 0;
|
current->in_execve = 0;
|
||||||
mutex_unlock(¤t->cred_guard_mutex);
|
|
||||||
|
|
||||||
out_free:
|
out_free:
|
||||||
free_bprm(bprm);
|
free_bprm(bprm);
|
||||||
|
|
|
@ -117,6 +117,7 @@ extern int setup_arg_pages(struct linux_binprm * bprm,
|
||||||
int executable_stack);
|
int executable_stack);
|
||||||
extern int bprm_mm_init(struct linux_binprm *bprm);
|
extern int bprm_mm_init(struct linux_binprm *bprm);
|
||||||
extern int copy_strings_kernel(int argc,char ** argv,struct linux_binprm *bprm);
|
extern int copy_strings_kernel(int argc,char ** argv,struct linux_binprm *bprm);
|
||||||
|
extern int prepare_bprm_creds(struct linux_binprm *bprm);
|
||||||
extern void install_exec_creds(struct linux_binprm *bprm);
|
extern void install_exec_creds(struct linux_binprm *bprm);
|
||||||
extern void do_coredump(long signr, int exit_code, struct pt_regs *regs);
|
extern void do_coredump(long signr, int exit_code, struct pt_regs *regs);
|
||||||
extern int set_binfmt(struct linux_binfmt *new);
|
extern int set_binfmt(struct linux_binfmt *new);
|
||||||
|
|
Загрузка…
Ссылка в новой задаче