Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp7039892ybi; Thu, 13 Jun 2019 08:33:54 -0700 (PDT) X-Google-Smtp-Source: APXvYqx6pzKJQPuxx5rEsfy97jGW04/4/kIc4Urq8+FdFftzFqaMm0mFlxBMUwh6bh+9aul7uwq5 X-Received: by 2002:a17:90a:62cb:: with SMTP id k11mr6024974pjs.26.1560440034257; Thu, 13 Jun 2019 08:33:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1560440034; cv=none; d=google.com; s=arc-20160816; b=oFEc2PHw14Itxz3jNsGTSLfCTVFuXcEHSS+614+SZRq0IEq+SCIIaK4dY+X1MMWwlN EXAHofbXbBch9VDxWQeZ8Oi7O1PsNRsg29F2XWtmhjqIUJNi1tg7tCrWkYz92JzNWM9y n2PWVprtlY6hVZC0+KqB2b8vqGIU4ysfWfxBjmjSj907D+SN9eG/zZY2Ul4CPH99BKvK VSBQPeA+z1Df1kLeFA4DkdAJpKJqNQYBmsQqLmi7jhndlBHv/FtJk1j/Wn2N2gb1I0// 6v6QaC1l1wp63/AsGAdv9J9bTbVS/a5rcD4jxc0QiJLwbqVRg6utDqOXXVJx3xtPZWCH ZPJg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=YR8hMv6qqSRLcHR5+nJRHlZJCSYGZ5XT+sBjsdKq79s=; b=MH++l1EXRxMwR5L6rzaeWr+9/BbpSXMQ8/0AnFwAgRSitlAyZxDSueCk498okH0J4z c46GilUMmbDOE2rauNwF1tceUMGebwvY0RKhW8q54xN8ST0jsawvJWAdVr7rFuU4WBqh JH2WRh89EiRVawj/Ia6cktGM7YQXj/f6Bkj/xezCy4FrQTzpcsdETjBUQ10rU01X+ozB RH7+kvxOf/++STFuMWKO7I7w9z5D88Q0ME68YTdpRMnadf2VFlk3LdXrJiHIQyvTqN2Q /HRWGk3PYFZjY+3C0YONF7F8IF7MVWjDYTFo2Va/My/X5LrJRfsl3eqEU3IWWsTitf8Z tJLA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j68si3698866pfb.268.2019.06.13.08.33.38; Thu, 13 Jun 2019 08:33:54 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726788AbfFMPcl (ORCPT + 99 others); Thu, 13 Jun 2019 11:32:41 -0400 Received: from mx2.suse.de ([195.135.220.15]:45242 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728458AbfFMKr0 (ORCPT ); Thu, 13 Jun 2019 06:47:26 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 8C775AE4B; Thu, 13 Jun 2019 10:47:25 +0000 (UTC) From: =?UTF-8?q?Michal=20Koutn=C3=BD?= To: mkoutny@suse.com Cc: gorcunov@gmail.com, ktkhai@virtuozzo.com, ldufour@linux.ibm.com, Matthew Wilcox , linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [RFC PATCH v2] binfmt_elf: Protect mm_struct access with mmap_sem Date: Thu, 13 Jun 2019 12:47:15 +0200 Message-Id: <20190613104715.22367-1-mkoutny@suse.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190612142811.24894-1-mkoutny@suse.com> References: <20190612142811.24894-1-mkoutny@suse.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 accommodate argv[] pointers happens without the lock. I was not able to cause an mm_struct corruption but an inserted BUG_ON(!rwsem_is_locked(&mm->mmap_sem)) in find_extend_vma could be triggered as # Cc: Matthew Wilcox Reviewed-by: Cyrill Gorcunov Signed-off-by: Michal Koutný --- 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); + if (!vma) return -EFAULT; @@ -1123,11 +1127,15 @@ static int load_elf_binary(struct linux_binprm *bprm) goto out; #endif /* ARCH_HAS_SETUP_ADDITIONAL_PAGES */ + /* + * Don't take mm->arg_lock. The concurrent change might happen only + * from prctl_set_mm but after de_thread we are certainly alone here. + */ retval = create_elf_tables(bprm, &loc->elf_ex, load_addr, interp_load_addr); if (retval < 0) goto out; - /* N.B. passed_fileno might not be initialized? */ + current->mm->end_code = end_code; current->mm->start_code = start_code; current->mm->start_data = start_data; diff --git a/fs/exec.c b/fs/exec.c index 89a500bb897a..d5b55c92019a 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -212,7 +212,8 @@ static struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos, /* * We are doing an exec(). 'current' is the process - * doing the exec and bprm->mm is the new process's mm. + * doing the exec and bprm->mm is the new process's mm that is not + * shared yet, so no synchronization on mmap_sem. */ ret = get_user_pages_remote(current, bprm->mm, pos, 1, gup_flags, &page, NULL, NULL); -- 2.21.0