Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp5937017ybi; Wed, 12 Jun 2019 11:01:36 -0700 (PDT) X-Google-Smtp-Source: APXvYqzmCoO8sRq1rv+pAT6p+fLz6JVeu0I9NcXYa4tXFu7mwOZtNUaM64wfO8k+j/gSuEObl9ZA X-Received: by 2002:a17:902:e105:: with SMTP id cc5mr63047507plb.320.1560362496269; Wed, 12 Jun 2019 11:01:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1560362496; cv=none; d=google.com; s=arc-20160816; b=TliQsy3he89tx+s7Gs/JRPgvWNgGBtJfHPsLRY0lIIxe8r9cHrm9edaH5iPyfkdg6K XaQ53OdS4bWkIb3Vd9C1cDcAsvy7CcJi6NTsQJmHvN31jh9LZdW6+QjXuLOT3TdMcSF4 6elR8DXLuHFpYGgdTyBz1Ym8iarWFFJVLIxebsabwwdjpnXo4E17m5ki2ZmThiLkYjl5 BKWTXeu8VgIMYU+eCMzkTxoQ8LmifdKl65vx7mK6aZV0SPP2bR/rDrSiObGidseLhSHs cmfuoDu1C0aCKr7UvC3r2jbT6Kc2kR6p7ADCelI63+3SfRbMIoISdawRkz6+Rm/5WjnQ JnXA== 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 :message-id:date:subject:cc:to:from; bh=wMikMlBNKS1Ut1DebH98gB1mwWJIVawaIVnza7rvA2U=; b=cq+HcvOrWyTac7epp/ShzED2EvailsG+Ug2JLFLjTXMhCLHwDihntouqtk/qhjXo9Z etbMgskgpSR6wlmA58G5zTZpO2uVQcLOQwey4OZtebXIYXTtiPtD/8KUhoR5S2Lf6uJQ yisemLraWZpw+wfOJ7JvlNr+Qkk9JDtdV+2B9cgIAa5fbWevmQYYsOunsDxOwfPNJIUu rjgSKwzrCOxc4DqffKA4MPpuZEYeUPz5zCFZffzD7Ubjz0TqPOoOAdcWYVFJyG3c9ffX IKf2VuaC23RU0zXcBJMz4gG4+jHdwToTSmV9MGu7E4bGm9dx8DbzVGmWI3L/fGDCNSS+ LFZQ== 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 z9si404833pgi.341.2019.06.12.11.01.21; Wed, 12 Jun 2019 11:01:36 -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 S2501867AbfFLO23 (ORCPT + 99 others); Wed, 12 Jun 2019 10:28:29 -0400 Received: from mx2.suse.de ([195.135.220.15]:43592 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S2437660AbfFLO21 (ORCPT ); Wed, 12 Jun 2019 10:28:27 -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 4D542B026; Wed, 12 Jun 2019 14:28:26 +0000 (UTC) From: =?UTF-8?q?Michal=20Koutn=C3=BD?= To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, gorcunov@gmail.com, Laurent Dufour , Kirill Tkhai Subject: [RFC PATCH] binfmt_elf: Protect mm_struct access with mmap_sem Date: Wed, 12 Jun 2019 16:28:11 +0200 Message-Id: <20190612142811.24894-1-mkoutny@suse.com> X-Mailer: git-send-email 2.21.0 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 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 # Signed-off-by: Michal Koutný --- 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); + 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