Skip to content

Commit f18ac55

Browse files
committed
exec: Move bprm_mm_init into alloc_bprm
Currently it is necessary for the usermode helper code and the code that launches init to use set_fs so that pages coming from the kernel look like they are coming from userspace. To allow that usage of set_fs to be removed cleanly the argument copying from userspace needs to happen earlier. Move the allocation and initialization of bprm->mm into alloc_bprm so that the bprm->mm is available early to store the new user stack into. This is a prerequisite for copying argv and envp into the new user stack early before ther rest of exec. To keep the things consistent the cleanup of bprm->mm is moved into free_bprm. So that bprm->mm will be cleaned up whenever bprm->mm is allocated and free_bprm are called. Moving bprm_mm_init earlier is safe as it does not depend on any files, current->in_execve, current->fs->in_exec, bprm->unsafe, or the if the file table is shared. (AKA bprm_mm_init does not depend on any of the code that happens between alloc_bprm and where it was previously called.) This moves bprm->mm cleanup after current->fs->in_exec is set to 0. This is safe because current->fs->in_exec is only used to preventy taking an additional reference on the fs_struct. This moves bprm->mm cleanup after current->in_execve is set to 0. This is safe because current->in_execve is only used by the lsms (apparmor and tomoyou) and always for LSM specific functions, never for anything to do with the mm. This adds bprm->mm cleanup into the successful return path. This is safe because being on the successful return path implies that begin_new_exec succeeded and set brpm->mm to NULL. As bprm->mm is NULL bprm cleanup I am moving into free_bprm will do nothing. Reviewed-by: Kees Cook <[email protected]> Reviewed-by: Christoph Hellwig <[email protected]> Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: "Eric W. Biederman" <[email protected]>
1 parent 60d9ad1 commit f18ac55

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

fs/exec.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1543,6 +1543,10 @@ static int prepare_bprm_creds(struct linux_binprm *bprm)
15431543

15441544
static void free_bprm(struct linux_binprm *bprm)
15451545
{
1546+
if (bprm->mm) {
1547+
acct_arg_size(bprm, 0);
1548+
mmput(bprm->mm);
1549+
}
15461550
free_arg_pages(bprm);
15471551
if (bprm->cred) {
15481552
mutex_unlock(&current->signal->cred_guard_mutex);
@@ -1582,6 +1586,10 @@ static struct linux_binprm *alloc_bprm(int fd, struct filename *filename)
15821586
bprm->filename = bprm->fdpath;
15831587
}
15841588
bprm->interp = bprm->filename;
1589+
1590+
retval = bprm_mm_init(bprm);
1591+
if (retval)
1592+
goto out_free;
15851593
return bprm;
15861594

15871595
out_free:
@@ -1911,10 +1919,6 @@ static int do_execveat_common(int fd, struct filename *filename,
19111919
close_on_exec(fd, rcu_dereference_raw(current->files->fdt)))
19121920
bprm->interp_flags |= BINPRM_FLAGS_PATH_INACCESSIBLE;
19131921

1914-
retval = bprm_mm_init(bprm);
1915-
if (retval)
1916-
goto out_unmark;
1917-
19181922
retval = prepare_arg_pages(bprm, argv, envp);
19191923
if (retval < 0)
19201924
goto out;
@@ -1962,10 +1966,6 @@ static int do_execveat_common(int fd, struct filename *filename,
19621966
*/
19631967
if (bprm->point_of_no_return && !fatal_signal_pending(current))
19641968
force_sigsegv(SIGSEGV);
1965-
if (bprm->mm) {
1966-
acct_arg_size(bprm, 0);
1967-
mmput(bprm->mm);
1968-
}
19691969

19701970
out_unmark:
19711971
current->fs->in_exec = 0;

0 commit comments

Comments
 (0)