Skip to content

Commit

Permalink
prctl: PR_SET_MM -- factor out mmap_sem when updating mm::exe_file
Browse files Browse the repository at this point in the history
Instead of taking mm->mmap_sem inside prctl_set_mm_exe_file() move it out
and rename the helper to prctl_set_mm_exe_file_locked().  This will allow
to reuse this function in a next patch.

Signed-off-by: Cyrill Gorcunov <[email protected]>
Cc: Kees Cook <[email protected]>
Cc: Tejun Heo <[email protected]>
Cc: Andrew Vagin <[email protected]>
Cc: Eric W. Biederman <[email protected]>
Cc: H. Peter Anvin <[email protected]>
Acked-by: Serge Hallyn <[email protected]>
Cc: Pavel Emelyanov <[email protected]>
Cc: Vasiliy Kulikov <[email protected]>
Cc: KAMEZAWA Hiroyuki <[email protected]>
Cc: Michael Kerrisk <[email protected]>
Cc: Julien Tinnes <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
Cyrill Gorcunov authored and torvalds committed Oct 10, 2014
1 parent 8764b33 commit 71fe97e
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions kernel/sys.c
Original file line number Diff line number Diff line change
Expand Up @@ -1628,12 +1628,14 @@ SYSCALL_DEFINE1(umask, int, mask)
return mask;
}

static int prctl_set_mm_exe_file(struct mm_struct *mm, unsigned int fd)
static int prctl_set_mm_exe_file_locked(struct mm_struct *mm, unsigned int fd)
{
struct fd exe;
struct inode *inode;
int err;

VM_BUG_ON(!rwsem_is_locked(&mm->mmap_sem));

exe = fdget(fd);
if (!exe.file)
return -EBADF;
Expand All @@ -1654,8 +1656,6 @@ static int prctl_set_mm_exe_file(struct mm_struct *mm, unsigned int fd)
if (err)
goto exit;

down_write(&mm->mmap_sem);

/*
* Forbid mm->exe_file change if old file still mapped.
*/
Expand All @@ -1667,7 +1667,7 @@ static int prctl_set_mm_exe_file(struct mm_struct *mm, unsigned int fd)
if (vma->vm_file &&
path_equal(&vma->vm_file->f_path,
&mm->exe_file->f_path))
goto exit_unlock;
goto exit;
}

/*
Expand All @@ -1678,13 +1678,10 @@ static int prctl_set_mm_exe_file(struct mm_struct *mm, unsigned int fd)
*/
err = -EPERM;
if (test_and_set_bit(MMF_EXE_FILE_CHANGED, &mm->flags))
goto exit_unlock;
goto exit;

err = 0;
set_mm_exe_file(mm, exe.file); /* this grabs a reference to exe.file */
exit_unlock:
up_write(&mm->mmap_sem);

exit:
fdput(exe);
return err;
Expand All @@ -1703,8 +1700,12 @@ static int prctl_set_mm(int opt, unsigned long addr,
if (!capable(CAP_SYS_RESOURCE))
return -EPERM;

if (opt == PR_SET_MM_EXE_FILE)
return prctl_set_mm_exe_file(mm, (unsigned int)addr);
if (opt == PR_SET_MM_EXE_FILE) {
down_write(&mm->mmap_sem);
error = prctl_set_mm_exe_file_locked(mm, (unsigned int)addr);
up_write(&mm->mmap_sem);
return error;
}

if (addr >= TASK_SIZE || addr < mmap_min_addr)
return -EINVAL;
Expand Down

0 comments on commit 71fe97e

Please sign in to comment.