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 1B6D1C433F5 for ; Wed, 23 Feb 2022 14:11:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 612988D0007; Wed, 23 Feb 2022 09:11:08 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 59A9F8D0001; Wed, 23 Feb 2022 09:11:08 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 414D98D0007; Wed, 23 Feb 2022 09:11:08 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0214.hostedemail.com [216.40.44.214]) by kanga.kvack.org (Postfix) with ESMTP id 13F5E8D0001 for ; Wed, 23 Feb 2022 09:11:08 -0500 (EST) Received: from smtpin12.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 8F1EE9F5D5 for ; Wed, 23 Feb 2022 14:11:07 +0000 (UTC) X-FDA: 79174231374.12.B5C7D8B Received: from mail-yb1-f177.google.com (mail-yb1-f177.google.com [209.85.219.177]) by imf01.hostedemail.com (Postfix) with ESMTP id 1EEF840005 for ; Wed, 23 Feb 2022 14:11:06 +0000 (UTC) Received: by mail-yb1-f177.google.com with SMTP id u12so34266989ybd.7 for ; Wed, 23 Feb 2022 06:11:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=phABIdj2nervTu7AKA4uorje6NMaTJX6SO98p2WDatg=; b=FLAcdNcu3lRYnXTtjQo0vbyH9BDZt7GVCVkkTL7wCR1Z4/DsS08KFSY1glY1Gm7Km/ gcUFXoB/L7c6U1th++WkDi++GQDwuWXCb+xtzi3OHr4VxfCx7YfWRCDIi9oww3KEGdlf eIg/BmLbZedkBaLmmXYtdMJu3EYAg6+R7Qcvu4hJUZ1qgGFAoowi70GpAvC/KvBnJ0oU tnN+CTLsFKPuevSEq/7bYfilakJZGmdhG2Nk3PIM7eECYZq6az9F5aRAmKzd5K0A8stq Od1ZGYOHki0sbhjiaroJxhB0Qb/o4mmJtGSwU5M6tpOZawrt88IaNyOlLXG7C2aWVSGU eGOg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=phABIdj2nervTu7AKA4uorje6NMaTJX6SO98p2WDatg=; b=nRXPd1BvvpE9ADi4nEgM2AzyALsiU61IbrkV7z7XysAbbt9wwgODAG42xyaVv6fukA l+RVGrqXYD0Rr1ZervBQDZCkkpz5f5yj5/ixxbkqCj5xRpMOdeUg+Hm93yncp3xhIJ/w aZd/2HeSmtMWY4ugnwHaRIm2C7KP/rVQ2f8Iy8MJknOAdrQukTQJJleXn/1+IWKEboQf 9oL3JFlZaAqo0PIbCAMAj6eNrwa8la4Q5Gn2Wbhgj8BsnNQSqfI70SSkvkQCp3Qq28tX 1lNf900aop/QTirM8ZeR3uQdmePC/UOudvmhv9cYRL8x5sopMN2AWHHSI7tyingGLYDx e7pA== X-Gm-Message-State: AOAM532nXvG4oUGZhJHXwiIF95L7yqO+c2oAKh11Nh78rKVWBNO6QmdQ 1xHWedr3vGk/+H/xCR7l0pjtsOxE9zhdaHNb7/zHAg== X-Google-Smtp-Source: ABdhPJwAE//TeIHCNAZd6BTSXuk8mA8WkjX54Q3b/grjIh0CSztjHeGcURhId1sRso3Hl74i70Fu2PWN9ILT5DO0Xp4= X-Received: by 2002:a25:2693:0:b0:624:50a8:fee9 with SMTP id m141-20020a252693000000b0062450a8fee9mr21007552ybm.348.1645625465753; Wed, 23 Feb 2022 06:11:05 -0800 (PST) MIME-Version: 1.0 References: <20220222054025.3412898-1-surenb@google.com> <20220222054025.3412898-3-surenb@google.com> In-Reply-To: From: Suren Baghdasaryan Date: Wed, 23 Feb 2022 06:10:54 -0800 Message-ID: Subject: Re: [PATCH v4 3/3] mm: fix use-after-free when anon vma name is used after vma is freed To: Michal Hocko 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 Content-Type: text/plain; charset="UTF-8" X-Rspamd-Queue-Id: 1EEF840005 X-Stat-Signature: mnunca5fgn67uio6d7f14pk6rmaxkf6k Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=FLAcdNcu; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf01.hostedemail.com: domain of surenb@google.com designates 209.85.219.177 as permitted sender) smtp.mailfrom=surenb@google.com X-Rspam-User: X-Rspamd-Server: rspam11 X-HE-Tag: 1645625466-858170 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, Feb 23, 2022 at 12:55 AM Michal Hocko wrote: > > On Tue 22-02-22 07:43:40, Suren Baghdasaryan wrote: > > On Tue, Feb 22, 2022 at 12:06 AM Michal Hocko wrote: > > > > > > 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? > > > > Yes, sorry, I need to update the part of the description talking about > > passing vma->anon_name->name directly. > > I think UAF is still there, it's just harder to reproduce (admittedly > > I could not reproduce it with the previous reproducer). The scenario > > would be when a vma with vma->anon_name->kref == 1 is being merged > > with another one and freed in the process: > > > > madvise_vma_behavior > > anon_name = vma_anon_name(vma) <-- does not increase refcount > > madvise_update_vma(anon_name) > > *prev = vma_merge <-- returns another vma > > __vma_adjust > > vm_area_free(vma) > > free_vma_anon_name > > anon_vma_name_put > > vma_anon_name_free <-- frees the vma->anon_name > > vma = *prev <-- original vma was freed > > How come this is not a UAF in the first place? Sorry, I got you confused. The original vma that was passed as a parameter to vma_merge(vma) was freed and vma_merge() returns the area it was merged with: *prev = vma_merge(vma_a) <-- vma_a is merged with adjacent vma_b, vma_a is freed and vma_b is returned vma = *prev <-- "vma" now points to vma_b > > > replace_vma_anon_name(vma, >>anon_name<<) <-- UAF > > > > Does this make sense or did I miss something? > > Sorry for being dense but I still do not see it. If *prev has been freed > then we already have a different UAF. Admittedly, I am not really fluent > at vma_merge code path so I am not really sure your chain above is > really possible. I will try to double check later. > -- > Michal Hocko > SUSE Labs