From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 00D2BC433DF for ; Sat, 17 Oct 2020 23:14:18 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C258020E65 for ; Sat, 17 Oct 2020 23:14:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1602976457; bh=3kOwiTpnCK1Bv4+1XPwqTtAubtIlwLSRR5F/+KJVms8=; h=Date:From:To:Subject:In-Reply-To:Reply-To:List-ID:From; b=vRNGqgZVoSeP1ZtHt6gaXP1HN+nUe0HaJvH+vGl6gsk8NCYSSLHn/Tql2SbYPVdW7 vEu4lHm8P3NA3uhZQ8KQTcthULIkuJIxVsqGLlwkPlq4ikw5Nk+GvVaMMuKGxxfusF x4V6H4GjhV6RycmzR7PRVBJkf6wqvG3BkWimAuI4= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2439892AbgJQXOR (ORCPT ); Sat, 17 Oct 2020 19:14:17 -0400 Received: from mail.kernel.org ([198.145.29.99]:47986 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2439878AbgJQXOQ (ORCPT ); Sat, 17 Oct 2020 19:14:16 -0400 Received: from localhost.localdomain (c-73-231-172-41.hsd1.ca.comcast.net [73.231.172.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id E683D208B6; Sat, 17 Oct 2020 23:14:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1602976456; bh=3kOwiTpnCK1Bv4+1XPwqTtAubtIlwLSRR5F/+KJVms8=; h=Date:From:To:Subject:In-Reply-To:From; b=2J556YqShNJAJGEGS4R90SU++sGEP0FUvOaP09UqQ+RsiSz/LUKoo+v8AI8asZ3m1 2T5b8U2quZENka6NICO0hmXKOYWfqptB2eZSLbAZjpEcHjp5qG7khiPDJmRpN180N8 uIgCUqt8RNNstkmB4kkrpWR+xwPevDrCzacYxDcU= Date: Sat, 17 Oct 2020 16:14:15 -0700 From: Andrew Morton To: akpm@linux-foundation.org, ebiederm@xmission.com, jannh@google.com, jgg@nvidia.com, jhubbard@nvidia.com, linux-mm@kvack.org, mchehab@kernel.org, mm-commits@vger.kernel.org, sakari.ailus@linux.intel.com, torvalds@linux-foundation.org, walken@google.com Subject: [patch 13/40] binfmt_elf: take the mmap lock around find_extend_vma() Message-ID: <20201017231415.XtZWgOT7Q%akpm@linux-foundation.org> In-Reply-To: <20201017161314.88890b87fae7446ccc13c902@linux-foundation.org> User-Agent: s-nail v14.8.16 Precedence: bulk Reply-To: linux-kernel@vger.kernel.org List-ID: X-Mailing-List: mm-commits@vger.kernel.org From: Jann Horn Subject: binfmt_elf: take the mmap lock around find_extend_vma() create_elf_tables() runs after setup_new_exec(), so other tasks can already access our new mm and do things like process_madvise() on it. (At the time I'm writing this commit, process_madvise() is not in mainline yet, but has been in akpm's tree for some time.) While I believe that there are currently no APIs that would actually allow another process to mess up our VMA tree (process_madvise() is limited to MADV_COLD and MADV_PAGEOUT, and uring and userfaultfd cannot reach an mm under which no syscalls have been executed yet), this seems like an accident waiting to happen. Let's make sure that we always take the mmap lock around GUP paths as long as another process might be able to see the mm. (Yes, this diff looks suspicious because we drop the lock before doing anything with `vma`, but that's because we actually don't do anything with it apart from the NULL check.) Link: https://lkml.kernel.org/r/CAG48ez1-PBCdv3y8pn-Ty-b+FmBSLwDuVKFSt8h7wARLy0dF-Q@mail.gmail.com Signed-off-by: Jann Horn Acked-by: Michel Lespinasse Cc: "Eric W . Biederman" Cc: Jason Gunthorpe Cc: John Hubbard Cc: Mauro Carvalho Chehab Cc: Sakari Ailus Signed-off-by: Andrew Morton --- fs/binfmt_elf.c | 3 +++ 1 file changed, 3 insertions(+) --- a/fs/binfmt_elf.c~binfmt_elf-take-the-mmap-lock-around-find_extend_vma +++ a/fs/binfmt_elf.c @@ -310,7 +310,10 @@ create_elf_tables(struct linux_binprm *b * 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 (mmap_read_lock_killable(mm)) + return -EINTR; vma = find_extend_vma(mm, bprm->p); + mmap_read_unlock(mm); if (!vma) return -EFAULT; _