Skip to content

Commit 63e46b9

Browse files
oleg-nesterovtorvalds
authored andcommitted
exec: move the final allow_write_access/fput into free_bprm()
Both success/failure paths cleanup bprm->file, we can move this code into free_bprm() to simlify and cleanup this logic. Signed-off-by: Oleg Nesterov <oleg@redhat.com> Acked-by: KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com> Cc: Al Viro <viro@zeniv.linux.org.uk> Acked-by: Kees Cook <keescook@chromium.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 9e00cdb commit 63e46b9

1 file changed

Lines changed: 5 additions & 15 deletions

File tree

‎fs/exec.c‎

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,9 +1138,7 @@ void setup_new_exec(struct linux_binprm * bprm)
11381138

11391139
/* An exec changes our domain. We are no longer part of the thread
11401140
group */
1141-
11421141
current->self_exec_id++;
1143-
11441142
flush_signal_handlers(current, 0);
11451143
do_close_on_exec(current->files);
11461144
}
@@ -1172,6 +1170,10 @@ void free_bprm(struct linux_binprm *bprm)
11721170
mutex_unlock(&current->signal->cred_guard_mutex);
11731171
abort_creds(bprm->cred);
11741172
}
1173+
if (bprm->file) {
1174+
allow_write_access(bprm->file);
1175+
fput(bprm->file);
1176+
}
11751177
/* If a binfmt changed the interp, free it. */
11761178
if (bprm->interp != bprm->filename)
11771179
kfree(bprm->interp);
@@ -1424,12 +1426,6 @@ static int exec_binprm(struct linux_binprm *bprm)
14241426
ptrace_event(PTRACE_EVENT_EXEC, old_vpid);
14251427
current->did_exec = 1;
14261428
proc_exec_connector(current);
1427-
1428-
if (bprm->file) {
1429-
allow_write_access(bprm->file);
1430-
fput(bprm->file);
1431-
bprm->file = NULL; /* to catch use-after-free */
1432-
}
14331429
}
14341430

14351431
return ret;
@@ -1492,7 +1488,7 @@ static int do_execve_common(const char *filename,
14921488

14931489
retval = bprm_mm_init(bprm);
14941490
if (retval)
1495-
goto out_file;
1491+
goto out_unmark;
14961492

14971493
bprm->argc = count(argv, MAX_ARG_STRINGS);
14981494
if ((retval = bprm->argc) < 0)
@@ -1539,12 +1535,6 @@ static int do_execve_common(const char *filename,
15391535
mmput(bprm->mm);
15401536
}
15411537

1542-
out_file:
1543-
if (bprm->file) {
1544-
allow_write_access(bprm->file);
1545-
fput(bprm->file);
1546-
}
1547-
15481538
out_unmark:
15491539
current->fs->in_exec = 0;
15501540
current->in_execve = 0;

0 commit comments

Comments
 (0)