Received: by 2002:a05:6a10:9e8c:0:0:0:0 with SMTP id y12csp872431pxx; Tue, 27 Oct 2020 02:35:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzjo8a64lh3BuYqwmMoy+htUi2KE6YtB/puZVw2e7rd+Jf1+tFVJRH5IUj00cSzZ0M3ApCd X-Received: by 2002:a17:906:2850:: with SMTP id s16mr1590253ejc.276.1603791310341; Tue, 27 Oct 2020 02:35:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1603791310; cv=none; d=google.com; s=arc-20160816; b=OYxT+jZd7mDJYU6V6isig0WDw7WEWflu2T8aUl+I/EzKWj424wqR32705RubNJzq2s vZ4/DRcR3nQrImRMatt5fTMI5VjlXuyYiB03utOtywlRuI13kNyNGOKqQs9n6OBBx2Wj a3YBWViyyVMrRfdujPUp6OFqW/BVyvSXAJPhaFK6Pn+9ibSaRF0uZjey+027zCKnSkgF jdTDBPtcOoOIpBhP+By1haqZ/ZKvlegI+doE/ZFpKFtSzH1KIIdbKdicOJ6/n+n3d9wB 3MLhZXOWHZhA0eg8WQ81PIop4TABVgDRfz3CFsf0iRa5PgvQAxn1EfNbzJx+VR5XM0Cd BFHQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=BHGzcRAUViBXShEkUuOEq0tPyoWEg/KzJ1xMK8KVx0c=; b=CrAzVkRbLiWGWoXoTSZNyryx1XZnJZGvDdEMf6BVPlz+J4LDcGVnrC2lhTHk8Hs1Lx yotVALwssQZ+a0I4l+qBieISqk63WGjkbPEVszX/yWzIqgLJk/Wa+DZb2X5Nm0qKKofi k/I23P36VM3iqXnWMUEVQwUffptt2n5CC7Xy4l6TnZnH4iEU9VGrHh0KvOfYPgxxycMy 9eKO4kqNayZWs0HS95boPfW5EzDaZHd6n0vZq3T5HtlKS/12+sqt9tirKBeBLB/YAf90 CCQl0W7fbBlybzanYXa+u05ncFztTcY18CBbrCXANp/hfkhJ6PvT8FxR9DgYxjxDT6/W qhYQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=ILWenKOd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id g30si462214edf.324.2020.10.27.02.34.48; Tue, 27 Oct 2020 02:35:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=ILWenKOd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2409492AbgJZXvk (ORCPT + 99 others); Mon, 26 Oct 2020 19:51:40 -0400 Received: from mail.kernel.org ([198.145.29.99]:51880 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2409370AbgJZXvS (ORCPT ); Mon, 26 Oct 2020 19:51:18 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 3A2822075B; Mon, 26 Oct 2020 23:51:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1603756277; bh=s6BpDc92vwIIqyKivdzAUxKkYDhM+Pq/e4ELnmq5AmI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ILWenKOdC+HQyKbTWoiYka+lmZImFhdZY4wdLbWAbJff+AUC3glUJpxwcNHVTiYnX kZSeOuHnp7ySXT6CMv4SnFCoSifPjpqLJlB9r8rNsDtHa8h834obF0KgJWLNC+ysJ3 8irpSU4bKjuxCf+y7d+ktrWlcy8a+81IS+8SVBd8= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Jann Horn , Andrew Morton , Michel Lespinasse , "Eric W . Biederman" , Jason Gunthorpe , John Hubbard , Mauro Carvalho Chehab , Sakari Ailus , Linus Torvalds , Sasha Levin , linux-fsdevel@vger.kernel.org Subject: [PATCH AUTOSEL 5.9 108/147] binfmt_elf: take the mmap lock around find_extend_vma() Date: Mon, 26 Oct 2020 19:48:26 -0400 Message-Id: <20201026234905.1022767-108-sashal@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201026234905.1022767-1-sashal@kernel.org> References: <20201026234905.1022767-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jann Horn [ Upstream commit b2767d97f5ff758250cf28684aaa48bbfd34145f ] 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.) Signed-off-by: Jann Horn Signed-off-by: Andrew Morton Acked-by: Michel Lespinasse Cc: "Eric W . Biederman" Cc: Jason Gunthorpe Cc: John Hubbard Cc: Mauro Carvalho Chehab Cc: Sakari Ailus Link: https://lkml.kernel.org/r/CAG48ez1-PBCdv3y8pn-Ty-b+FmBSLwDuVKFSt8h7wARLy0dF-Q@mail.gmail.com Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin --- fs/binfmt_elf.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c index 13d053982dd73..02ad1a1e3781f 100644 --- a/fs/binfmt_elf.c +++ b/fs/binfmt_elf.c @@ -309,7 +309,10 @@ create_elf_tables(struct linux_binprm *bprm, const 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 (mmap_read_lock_killable(mm)) + return -EINTR; vma = find_extend_vma(mm, bprm->p); + mmap_read_unlock(mm); if (!vma) return -EFAULT; -- 2.25.1