Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp1352669pxm; Thu, 24 Feb 2022 01:10:06 -0800 (PST) X-Google-Smtp-Source: ABdhPJz/tkMTTy03JXucAMkO55hG9SW384/hh2tB2mdLSF7Pu8aH503XMO2/+eInWjoQjg2WEmmj X-Received: by 2002:a05:6402:280d:b0:410:be87:c59d with SMTP id h13-20020a056402280d00b00410be87c59dmr1388771ede.95.1645693806303; Thu, 24 Feb 2022 01:10:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645693806; cv=none; d=google.com; s=arc-20160816; b=Emv1jK20EYlrFdzfBlefepLwTWd8MpxVS74j0dsKYKvHrLr5JT0GqEaXYJNGm1mcPV CHVRmpjcDNhK8NeMLGuzGHEzhY+0IsiQbEHP+21ykrVcVrwx7E2DqVbCIjvut4x2WWIw bgE3QPIB7lQu6rjEbBcRmiW6Fev1CoRJL7/21LMD1BCsN5Xnid2WrAhzOI3Qfgv5eu0r WSsGeySbrjK74rDxxYB+VkPQkgKU71BDCcHI7THc4Cfwf3tMJj5P0qh33FVhFsM8m+pr Ni1si8khaSsLKC/6/5m+a1RSYSXSGdK6GW39KLz4lp60nNB0x+g1C4PIZhXfID0/t1+M ft4A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=m54wP8QDH9xsCu8tvqikfzHGy3YusnIQ1dTaWJQjT18=; b=OZEvaY33+9lQwbSE8ynMAulHjav39Dya9nI64jBJjM5kFk1foCuEQL4r+dwgkgIzxR hKZ64m5UBruL262iRnkSlqkTrfpJBM26Lq7QoqQlf8KNoTNw+ObesSUJPvD0VRX0OGMa yOzrCORsH5DUq1i9JTQ1+YAZ84LlTnv4UvJNhy8XVNuVs3+Vz53clalNYpu41RlRn40D 1XAT38qJtUNrn57Qn7F2iZ5R+ADUZtTU2/K553QZCD4Swnr+DXLtd0fO8unIhwmauFp6 wsd1NeURYuH6qEgm8+dPeZ/5u5CPRIJmM1aZF2KDjrW/NaZV0/XjMsR4+iRA33p9Fg5S cSTw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b=fG3w9T8o; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=suse.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id rk24si1152567ejb.821.2022.02.24.01.09.39; Thu, 24 Feb 2022 01:10:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b=fG3w9T8o; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=suse.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232112AbiBXIqP (ORCPT + 99 others); Thu, 24 Feb 2022 03:46:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56414 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232140AbiBXIqM (ORCPT ); Thu, 24 Feb 2022 03:46:12 -0500 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E6F9116040C for ; Thu, 24 Feb 2022 00:45:36 -0800 (PST) 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-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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