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 12D85C433F5 for ; Wed, 23 Feb 2022 15:39:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 95CC08D001E; Wed, 23 Feb 2022 10:39:07 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8E5008D0011; Wed, 23 Feb 2022 10:39:07 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 739F98D001E; Wed, 23 Feb 2022 10:39:07 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0042.hostedemail.com [216.40.44.42]) by kanga.kvack.org (Postfix) with ESMTP id 475B88D0011 for ; Wed, 23 Feb 2022 10:39:07 -0500 (EST) Received: from smtpin11.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id CAB5F18147594 for ; Wed, 23 Feb 2022 15:39:06 +0000 (UTC) X-FDA: 79174453092.11.44C7261 Received: from mail-yb1-f173.google.com (mail-yb1-f173.google.com [209.85.219.173]) by imf25.hostedemail.com (Postfix) with ESMTP id 5FAC3A000B for ; Wed, 23 Feb 2022 15:39:06 +0000 (UTC) Received: by mail-yb1-f173.google.com with SMTP id y189so27200103ybe.4 for ; Wed, 23 Feb 2022 07:39: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=ZtJfBRX+21hIC4X5ueYxlOe21t3bqp0p9D1sl8QaQt8=; b=UBEdQRitCS6H4LdP/Noq8ewRXieRTmDfy1+EbGy3VRDXImdOcQD0fd1sZaNiuoXU7C YyHdTmA5OYonjmBs2S15mD1DORekJseTIg8JWYwX1LC0gr6qr1J+X/iWi9TByaWSfiQL ygA0rpJw8qAtTnJt4n986UpOrSi/KnXdSBUPQc2hNZZgXTEFpFZJaWKoXDtRRR+r7qES uk+5JOM4V84Q1h6vDAGlD0TLm/Ut+mIL3nO/3ArT/vIu1c/mkEo7ZC+TNSEEBvVdMECZ 9vZyXttWZVIuVudxP37OyISefa2ez0tdOf9JNxhqM8f5UboEfn20IY3d5VCenTwMHmwt 5m1w== 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=ZtJfBRX+21hIC4X5ueYxlOe21t3bqp0p9D1sl8QaQt8=; b=LRdNivjJLpcApZ6xjAAFxQoDOAlRW3z2cpR9RDpLLDCQle/82ALpJnyntSLqps08c7 sd4+oWkGEKWPlEd7usclHIOneMMjLL2Mgw/E3JWrsuPc0HglKU3PyEH0RUAMQIaVUXNE oXVR9c2IO6LlOo44ORhwjrIX+43H1uSRIc3DHncqMpg0Loxx5jeHxvl36VlBiezerjxc eWlOl5rwTO1g5HomSsiUv60lOj2lxaTOVSkbnhPKUSW3teaviQPzsevzdl6CHHfG4FnD k9tPaJVvG4k0w5QNteikKlvOMALzWy3u8zCfMMSqoJte99/mjLYQSDhs8Sa3WOInD9Jh 6WzQ== X-Gm-Message-State: AOAM532TxYssynwmukzyW8MWmmGoG0yqPEmdeUGkaO5QGtMFijodgISi Dt6zHqJ+1xqaZgpCpfOsegyMRnvv2iK586dM8qJttQ== X-Google-Smtp-Source: ABdhPJzaoTP2kqxhFgM++hTOOSkKPMNmKMYubdjBie1xtzxCyizuc9FjXlDPvkE4TRYJJ4sZBRAkxtOnJJl+MkY9O8U= X-Received: by 2002:a25:2693:0:b0:624:50a8:fee9 with SMTP id m141-20020a252693000000b0062450a8fee9mr277030ybm.348.1645630745172; Wed, 23 Feb 2022 07:39: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 07:38: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-Server: rspam01 X-Rspamd-Queue-Id: 5FAC3A000B X-Rspam-User: Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=UBEdQRit; spf=pass (imf25.hostedemail.com: domain of surenb@google.com designates 209.85.219.173 as permitted sender) smtp.mailfrom=surenb@google.com; dmarc=pass (policy=reject) header.from=google.com X-Stat-Signature: ose5tcgkejm7opxqtik6437f33q3b533 X-HE-Tag: 1645630746-438972 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 7:29 AM Michal Hocko wrote: > > On Wed 23-02-22 06:10:54, Suren Baghdasaryan wrote: > > 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 > > OK, I see the problem now. Essentially if we have two adjacent vmas > which have the same name but wrapped by two anon_vma_name instances > and we merge them together then the first one can be freed along with > its anon_vma_name which is still used by replace_anon_vma_name. > > Thanks for the patience. Thanks for reviewing! I posted the next version of the patchset at https://lore.kernel.org/linux-mm/20220223153613.835563-1-surenb@google.com/ > -- > Michal Hocko > SUSE Labs