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 E3145C433EF for ; Tue, 22 Feb 2022 08:07:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2E1C58D0003; Tue, 22 Feb 2022 03:07:11 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 26ABC8D0001; Tue, 22 Feb 2022 03:07:11 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0E58B8D0003; Tue, 22 Feb 2022 03:07:11 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0205.hostedemail.com [216.40.44.205]) by kanga.kvack.org (Postfix) with ESMTP id F01888D0001 for ; Tue, 22 Feb 2022 03:07:10 -0500 (EST) Received: from smtpin12.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id ACF378249980 for ; Tue, 22 Feb 2022 08:07:10 +0000 (UTC) X-FDA: 79169685420.12.0F840DD Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by imf05.hostedemail.com (Postfix) with ESMTP id 138E4100005 for ; Tue, 22 Feb 2022 08:06:33 +0000 (UTC) Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id B2E2D1F397; Tue, 22 Feb 2022 08:06:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1645517192; 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=ehW6wfM086KZA+sOLiBbTPlkvEAXOt9X6KB2AN8WGwg=; b=mJ2iEOIeamqg8KzeTaG9JbEdw321KXbt+VC0mifrBnbxGVOsWGnGTmbijRuYVU7RPp6/Y4 wzlKQSPSuYi3FgssR2Zwb68VEJ9j1psm0wZ74s3QRtHoUso7aeCSywpjHkwGARftb+W97K gSKvXn04/zhnA8aEheC/O6e3CIYdnK0= 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 CD582A3B87; Tue, 22 Feb 2022 08:06:31 +0000 (UTC) Date: Tue, 22 Feb 2022 09:06:31 +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 v4 3/3] mm: fix use-after-free when anon vma name is used after vma is freed Message-ID: References: <20220222054025.3412898-1-surenb@google.com> <20220222054025.3412898-3-surenb@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220222054025.3412898-3-surenb@google.com> X-Rspamd-Queue-Id: 138E4100005 X-Stat-Signature: emrnbhu8ps8padegdx6nkrij8rbsjgj8 X-Rspam-User: Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=suse.com header.s=susede1 header.b=mJ2iEOIe; spf=pass (imf05.hostedemail.com: domain of mhocko@suse.com designates 195.135.220.29 as permitted sender) smtp.mailfrom=mhocko@suse.com; dmarc=pass (policy=quarantine) header.from=suse.com X-Rspamd-Server: rspam05 X-HE-Tag: 1645517193-226139 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 Mon 21-02-22 21:40:25, 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->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: > > madvise_vma_behavior << passes vma->anon_name->name as name param > madvise_update_vma(name) > vma_merge > __vma_adjust > vm_area_free <-- frees the vma > replace_vma_anon_name(name) <-- UAF This seems to be stale because bare const char pointer is not passed in the call chain. In fact I am not even sure there is any actual UAF here after the rework. Could you be more specific in describing the scenario? > 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 > --- > changes in v3: > - Reapplied the fix after code refactoring, per Michal Hocko > > mm/madvise.c | 4 ++++ > 1 file changed, 4 insertions(+) > > diff --git a/mm/madvise.c b/mm/madvise.c > index a395884aeecb..00e8105430e9 100644 > --- a/mm/madvise.c > +++ b/mm/madvise.c > @@ -140,6 +140,8 @@ static int replace_vma_anon_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, > @@ -1021,8 +1023,10 @@ static int madvise_vma_behavior(struct vm_area_struct *vma, > } > > anon_name = vma_anon_name(vma); > + anon_vma_name_get(anon_name); > error = madvise_update_vma(vma, prev, start, end, new_flags, > anon_name); > + anon_vma_name_put(anon_name); > > out: > /* > -- > 2.35.1.473.g83b2b277ed-goog -- Michal Hocko SUSE Labs