From: Cyrill Gorcunov <gorcunov@gmail.com>
To: "Michal Koutný" <mkoutny@suse.com>
Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org,
Laurent Dufour <ldufour@linux.ibm.com>,
Kirill Tkhai <ktkhai@virtuozzo.com>
Subject: Re: [RFC PATCH] binfmt_elf: Protect mm_struct access with mmap_sem
Date: Wed, 12 Jun 2019 21:02:29 +0300 [thread overview]
Message-ID: <20190612180229.GD23535@uranus.lan> (raw)
In-Reply-To: <20190612142811.24894-1-mkoutny@suse.com>
On Wed, Jun 12, 2019 at 04:28:11PM +0200, Michal Koutný wrote:
> find_extend_vma assumes the caller holds mmap_sem as a reader (explained
> in expand_downwards()). The path when we are extending the stack VMA to
> accomodate argv[] pointers happens without the lock.
>
> I was not able to cause an mm_struct corruption but
> BUG_ON(!rwsem_is_locked(&mm->mmap_sem)) in find_extend_vma could be
> triggered as
>
> # <bigfile xargs echo
> xargs: echo: terminated by signal 11
>
> (bigfile needs to have more than RLIMIT_STACK / sizeof(char *) rows)
>
> Other accesses to mm_struct in exec path are protected by mmap_sem, so
> conservatively, protect also this one. Besides that, explain why we omit
> mm_struct.arg_lock in the exec(2) path.
>
> Cc: Cyrill Gorcunov <gorcunov@gmail.com>
> Signed-off-by: Michal Koutný <mkoutny@suse.com>
> ---
>
> When I was attempting to reduce usage of mmap_sem I came across this
> unprotected access and increased number of its holders :-/
>
> I'm not sure whether there is a real concurrent writer at this early
> stages (I considered khugepaged especially as setup_arg_pages invokes
> khugepaged_enter_vma_merge but we're lucky because khugepaged skips it
> because of VM_STACK_INCOMPLETE_SETUP).
>
> A nicer approach would perhaps be to do all this exec setup when the
> mm_struct is still not exposed via current->mm (and hence no need to
> synchronize via mmap_sem). But I didn't look enough into binfmt specific
> whether it is even doable and worth it.
>
> So I'm sending this for a discussion.
>
> fs/binfmt_elf.c | 10 +++++++++-
> fs/exec.c | 3 ++-
> 2 files changed, 11 insertions(+), 2 deletions(-)
>
> diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c
> index 8264b468f283..48e169760a9c 100644
> --- a/fs/binfmt_elf.c
> +++ b/fs/binfmt_elf.c
> @@ -299,7 +299,11 @@ create_elf_tables(struct linux_binprm *bprm, struct elfhdr *exec,
> * Grow the stack manually; some architectures have a limit on how
> * far ahead a user-space access may be in order to grow the stack.
> */
> + if (down_read_killable(¤t->mm->mmap_sem))
> + return -EINTR;
> vma = find_extend_vma(current->mm, bprm->p);
> + up_read(¤t->mm->mmap_sem);
> +
Good catch, Michal! Actually the loader code is heavy on its own so
I think having readlock taken here should not cause any perf problems
but worth having for consistency.
Reviewed-by: Cyrill Gorcunov <gorcunov@gmail.com>
next prev parent reply other threads:[~2019-06-12 18:02 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-06-12 14:28 Michal Koutný
2019-06-12 17:00 ` Matthew Wilcox
2019-06-12 17:29 ` Michal Koutný
2019-06-12 17:51 ` Matthew Wilcox
2019-06-12 18:04 ` Cyrill Gorcunov
2019-06-12 18:02 ` Cyrill Gorcunov [this message]
2019-06-13 10:47 ` [RFC PATCH v2] " Michal Koutný
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20190612180229.GD23535@uranus.lan \
--to=gorcunov@gmail.com \
--cc=ktkhai@virtuozzo.com \
--cc=ldufour@linux.ibm.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mkoutny@suse.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox