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 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id E3971C433F5 for ; Thu, 24 Feb 2022 08:45:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8A26B8D0003; Thu, 24 Feb 2022 03:45:37 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 851DA8D0001; Thu, 24 Feb 2022 03:45:37 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 741648D0003; Thu, 24 Feb 2022 03:45:37 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0022.hostedemail.com [216.40.44.22]) by kanga.kvack.org (Postfix) with ESMTP id 670388D0001 for ; Thu, 24 Feb 2022 03:45:37 -0500 (EST) Received: from smtpin25.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 1DC75181CCB05 for ; Thu, 24 Feb 2022 08:45:37 +0000 (UTC) X-FDA: 79177039914.25.88B0623 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by imf08.hostedemail.com (Postfix) with ESMTP id 8182B160004 for ; Thu, 24 Feb 2022 08:45:36 +0000 (UTC) Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id 9AE3721155; Thu, 24 Feb 2022 08:45:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1645692335; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=m54wP8QDH9xsCu8tvqikfzHGy3YusnIQ1dTaWJQjT18=; b=fG3w9T8oaJOm3aWcAp6tCn+DBj+sv9XOBoMBEW/P3HUkdeJNjvOc44CyTyffx1WBCV5vxZ fZrlrv97q4u6vBDfPifrp4qrNthvpUMRoaHnd8m8yvinraCHWd2JA4UuO10h1QcqtycHeS ybhxNb8Jp6EO6XPm6kgWhqCRETSYBCs= Received: from suse.cz (unknown [10.100.201.86]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id 33888A3B84; Thu, 24 Feb 2022 08:45:31 +0000 (UTC) Date: Thu, 24 Feb 2022 09:45:34 +0100 From: Michal Hocko To: Suren Baghdasaryan Cc: akpm@linux-foundation.org, ccross@google.com, sumit.semwal@linaro.org, dave.hansen@intel.com, keescook@chromium.org, willy@infradead.org, kirill.shutemov@linux.intel.com, vbabka@suse.cz, hannes@cmpxchg.org, ebiederm@xmission.com, brauner@kernel.org, legion@kernel.org, ran.xiaokai@zte.com.cn, sashal@kernel.org, chris.hyser@oracle.com, dave@stgolabs.net, pcc@google.com, caoxiaofeng@yulong.com, david@redhat.com, gorcunov@gmail.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel-team@android.com, syzbot+aa7b3d4b35f9dc46a366@syzkaller.appspotmail.com Subject: Re: [PATCH v5 3/3] mm: fix use-after-free when anon vma name is used after vma is freed Message-ID: References: <20220223153613.835563-1-surenb@google.com> <20220223153613.835563-3-surenb@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220223153613.835563-3-surenb@google.com> X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 8182B160004 X-Rspam-User: Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=suse.com header.s=susede1 header.b=fG3w9T8o; spf=pass (imf08.hostedemail.com: domain of mhocko@suse.com designates 195.135.220.28 as permitted sender) smtp.mailfrom=mhocko@suse.com; dmarc=pass (policy=quarantine) header.from=suse.com X-Stat-Signature: qhtxngxs978gacpbuw1i13jnc44rwnqh X-HE-Tag: 1645692336-528323 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Wed 23-02-22 07:36:13, Suren Baghdasaryan wrote: > When adjacent vmas are being merged it can result in the vma that was > originally passed to madvise_update_vma being destroyed. In the current > implementation, the name parameter passed to madvise_update_vma points > directly to vma->anon_name and it is used after the call to > vma_merge. In the cases when vma_merge merges the original vma and > destroys it, this will result in use-after-free bug as shown below: Not that I want to nit pick on the wording here because destruction of the original vma will not result in UAF automatically. For that it would need to hold anon_vma_name with the last reference. So I would reformulate: "In cases when vma_merge merges the original vma and destroys it, it might result in UAF. For that the original vma would have to hold the anon_vma_name with the last reference. The following vma would need to contain a different anon_vma_name object with the same string. > madvise_vma_behavior(vma) > madvise_update_vma(vma, ..., anon_name == vma->anon_name) > vma_merge(vma) > __vma_adjust(vma) <-- merges vma with adjacent one > vm_area_free(vma) <-- frees the original vma > replace_vma_anon_name(anon_name) <-- UAF of vma->anon_name > > Fix this by raising the name refcount and stabilizing it. > > Fixes: 9a10064f5625 ("mm: add a field to store names for private anonymous memory") > Signed-off-by: Suren Baghdasaryan > Reported-by: syzbot+aa7b3d4b35f9dc46a366@syzkaller.appspotmail.com Acked-by: Michal Hocko Thanks! > --- > changes in v5: > - Updated description, per Michal Hocko > > mm/madvise.c | 8 +++++++- > 1 file changed, 7 insertions(+), 1 deletion(-) > > diff --git a/mm/madvise.c b/mm/madvise.c > index 1f2693dccf7b..38d0f515d548 100644 > --- a/mm/madvise.c > +++ b/mm/madvise.c > @@ -131,6 +131,8 @@ static int replace_anon_vma_name(struct vm_area_struct *vma, > /* > * Update the vm_flags on region of a vma, splitting it or merging it as > * necessary. Must be called with mmap_sem held for writing; > + * Caller should ensure anon_name stability by raising its refcount even when > + * anon_name belongs to a valid vma because this function might free that vma. > */ > static int madvise_update_vma(struct vm_area_struct *vma, > struct vm_area_struct **prev, unsigned long start, > @@ -945,6 +947,7 @@ static int madvise_vma_behavior(struct vm_area_struct *vma, > unsigned long behavior) > { > int error; > + struct anon_vma_name *anon_name; > unsigned long new_flags = vma->vm_flags; > > switch (behavior) { > @@ -1010,8 +1013,11 @@ static int madvise_vma_behavior(struct vm_area_struct *vma, > break; > } > > + anon_name = anon_vma_name(vma); > + anon_vma_name_get(anon_name); > error = madvise_update_vma(vma, prev, start, end, new_flags, > - anon_vma_name(vma)); > + anon_name); > + anon_vma_name_put(anon_name); > > out: > /* > -- > 2.35.1.473.g83b2b277ed-goog -- Michal Hocko SUSE Labs