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 A903FC433F5 for ; Tue, 22 Feb 2022 15:43:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E41DC8D0002; Tue, 22 Feb 2022 10:43:53 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id DF1118D0001; Tue, 22 Feb 2022 10:43:53 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C931D8D0002; Tue, 22 Feb 2022 10:43:53 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0239.hostedemail.com [216.40.44.239]) by kanga.kvack.org (Postfix) with ESMTP id BBAD08D0001 for ; Tue, 22 Feb 2022 10:43:53 -0500 (EST) Received: from smtpin20.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 72BB28249980 for ; Tue, 22 Feb 2022 15:43:53 +0000 (UTC) X-FDA: 79170836346.20.D8066E5 Received: from mail-yb1-f179.google.com (mail-yb1-f179.google.com [209.85.219.179]) by imf01.hostedemail.com (Postfix) with ESMTP id D906340007 for ; Tue, 22 Feb 2022 15:43:52 +0000 (UTC) Received: by mail-yb1-f179.google.com with SMTP id c6so42026615ybk.3 for ; Tue, 22 Feb 2022 07:43:52 -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=LqXD7typxRqGUARnaw7Pj7LwfkKcjOGLF1BV+v9Ov4A=; b=kMZdlDYteCJBNpP7EcodSyEFNNM33iTzouRYRZy4sNNvi8VL9QsFmswNh0yZFVFEka H1ljgT3YeKJwxPUeLB1K4HLbyoWYwaNtP99vdqN9PgWc95ewSWy2R3yWFAzO0+ryDjrc 6ymKyHOrQy7YsjgJN19Hw1v9ZEoMVeY2ebjkmIU0C+neAlBxoGJ6tM7VyfzeYaLwo/BZ cZivrUi0SL6vZ4mgk0DL4umpMzIcnQAxNnL0BqXqKFJuAeykbT2iv9XptRpRGy029lAt 8LCm6p606pKu3eaMboNMDBsb5Z0TQLMOwgCvg7hlimVYbONG9rLfsb/g8Y9KOuIHSq9P fH3A== 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=LqXD7typxRqGUARnaw7Pj7LwfkKcjOGLF1BV+v9Ov4A=; b=o7NW6MnmUA/mrMqOLYBuKLHRdWr6YbY3cjw5mKPyycION2xhP1ONQSPQX42JY2VdpM 8u6W7Ds89K3Mp4/EItbE86Wr5z/PV1JMcI1OyEt+SbM+0/WCq6p1YzQ5cROy25/MQAzl G8bk5AOywjHzYoPdacWJ9bnU8BtrKGkMvQpEnOtQU5cqxSMezhWMotFrgisGLYzLkT7B DfzCsJl92NP9iEFM3901aw/3Rfs4A4XUOEmW0kGeu55n4OGhVjKtV+rx5d4yuQC7KQWf bL/Jnn6LhcjHBpTtMH1fBhTGh3TsThMZ8kXdXIszg1gyXOKlgfeHqStYid2QQXIcXpB3 jeHw== X-Gm-Message-State: AOAM531i5mGCov+qsxMyjduRB2Bdk8sX4KIqXC6K1iWX4r0VcLwOHULf bIx1H3V1DX1MSQJ5dJ/5lfkkdalj/3BHPy1MDRNxtw== X-Google-Smtp-Source: ABdhPJy3N2oGtBnhBdp+zKpJljXWde4cxCC5OtRofJNJX4vcopfRezUgTy2sWe8nFNEYaQwaZhfaPsDeMZYnRh5bROg= X-Received: by 2002:a25:d986:0:b0:624:ddc:ff9 with SMTP id q128-20020a25d986000000b006240ddc0ff9mr23367040ybg.509.1645544631876; Tue, 22 Feb 2022 07:43:51 -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: Tue, 22 Feb 2022 07:43:40 -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-Server: rspam04 X-Rspamd-Queue-Id: D906340007 X-Stat-Signature: 9e7nhbrqgdm8cnzonuqgeudrhahs6b6w Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=kMZdlDYt; spf=pass (imf01.hostedemail.com: domain of surenb@google.com designates 209.85.219.179 as permitted sender) smtp.mailfrom=surenb@google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspam-User: X-HE-Tag: 1645544632-933572 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 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 replace_vma_anon_name(vma, >>anon_name<<) <-- UAF Does this make sense or did I miss something? Thanks, Suren. > > > 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