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 889A1C433EF for ; Thu, 10 Feb 2022 16:00:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C024B6B0071; Thu, 10 Feb 2022 11:00:28 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id BB1206B0073; Thu, 10 Feb 2022 11:00:28 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A79E96B0075; Thu, 10 Feb 2022 11:00:28 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0087.hostedemail.com [216.40.44.87]) by kanga.kvack.org (Postfix) with ESMTP id 9A01F6B0071 for ; Thu, 10 Feb 2022 11:00:28 -0500 (EST) Received: from smtpin11.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 4DB4C8B755 for ; Thu, 10 Feb 2022 16:00:28 +0000 (UTC) X-FDA: 79127332536.11.C841EE4 Received: from mail-yb1-f180.google.com (mail-yb1-f180.google.com [209.85.219.180]) by imf09.hostedemail.com (Postfix) with ESMTP id DC277140009 for ; Thu, 10 Feb 2022 16:00:27 +0000 (UTC) Received: by mail-yb1-f180.google.com with SMTP id y129so16768767ybe.7 for ; Thu, 10 Feb 2022 08:00:27 -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=8XCqHQHpWHW92nEkn4B+1VbtafR8Zb17zrKGkCgMBOY=; b=faunBAocEyTU3CGL7gl//+yF9YyCQoMEbj2ZN8NRFm7J2vTf4HCC646inj2AKDJ/Li T+L57QEnJDZMU/x5GvmE1TNoUgI6KdnyW3BJjjsX9Ktj53KEWlWav5AGH3mFjHB/krwD DwKURXCZ+PeogMZdeSdYdXkS9BI63xaS9p7lST+vD23tLVjVQqoY3v+oCv2POgiGsVUC 9eEAWA8Vn4Wcjzl2qibliqTyiuxXOWEyrIltryEJ1vCwYCfU+r75T8MMcofrmi6aaQ/E oIZytQHO2X/pmANbIcx4+WBIDHJFfOMRT/t/dE8qVwVBRxhh8Mrp+WqP1zmnFZu+wiZ7 7mQw== 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=8XCqHQHpWHW92nEkn4B+1VbtafR8Zb17zrKGkCgMBOY=; b=uaOwZwE4s0foNXUpf94+YKKOHZGuZuM/VOEo7rjsRqTaQDZ1otH5AbwYSKvr5mr4R7 kYc8hy78RwlxRvSVTmQNiPnQg3Uu7/oWizWSRz3z83pmKEKMBDoZrlMzzBS2MvTr0rmB i213Vjvb62RXKKjL7uhaZjqSrtHCh5CJwnyp1DgkeDXifq+ZfLSVihKL/miT0g+Fcf0o 1tKBtRGGAjEauAYaGcTZdOrYoOlYL32GISoKFezW7IHisuGaAcnfiw1TX1iWqjJPMriX 8V21lCCt23Lg81CgEimIEUXowPDEnzs723DOaPcmIWYC14/V2IUdbolxY/9ZIOzYRQbE Bn/w== X-Gm-Message-State: AOAM531Q1wJE3Mj6tZxQE7jbBf3KAyPQRvBL+v7XjULC180cRPqHCpL4 1WO9RyeqTf4KhVkPwcasMkEFt3VVlu/7Hiq/Ae1oLQ== X-Google-Smtp-Source: ABdhPJyK0yeU3tCgFGaq/lWLUswz250+pcrtPVEN8SUTLC8FJkGgxsfg2XRHKVPKKE59pgalYQZ9QccYTUPgTw8lEL0= X-Received: by 2002:a81:7e4f:: with SMTP id p15mr7799834ywn.180.1644508825921; Thu, 10 Feb 2022 08:00:25 -0800 (PST) MIME-Version: 1.0 References: <20220210043215.42794-1-surenb@google.com> In-Reply-To: From: Suren Baghdasaryan Date: Thu, 10 Feb 2022 08:00:15 -0800 Message-ID: Subject: Re: [PATCH v2 1/1] mm: fix use-after-free when anon vma name is used after vma is freed To: Matthew Wilcox Cc: Michal Hocko , Andrew Morton , Colin Cross , Sumit Semwal , Dave Hansen , Kees Cook , "Kirill A . Shutemov" , Vlastimil Babka , Johannes Weiner , "Eric W. Biederman" , brauner@kernel.org, legion@kernel.org, ran.xiaokai@zte.com.cn, sashal@kernel.org, Chris Hyser , Davidlohr Bueso , Peter Collingbourne , caoxiaofeng@yulong.com, David Hildenbrand , Cyrill Gorcunov , linux-mm , LKML , kernel-team , syzbot+aa7b3d4b35f9dc46a366@syzkaller.appspotmail.com Content-Type: text/plain; charset="UTF-8" X-Rspamd-Queue-Id: DC277140009 X-Rspam-User: Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=faunBAoc; spf=pass (imf09.hostedemail.com: domain of surenb@google.com designates 209.85.219.180 as permitted sender) smtp.mailfrom=surenb@google.com; dmarc=pass (policy=reject) header.from=google.com X-Stat-Signature: sap7zanewno54nxp7mi5jreupmg9e9zj X-Rspamd-Server: rspam04 X-HE-Tag: 1644508827-2864 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 Thu, Feb 10, 2022 at 7:27 AM Matthew Wilcox wrote: > > On Thu, Feb 10, 2022 at 07:18:24AM -0800, Suren Baghdasaryan wrote: > > On Thu, Feb 10, 2022 at 4:40 AM 'Michal Hocko' via kernel-team > > wrote: > > > > > > On Wed 09-02-22 20:32:15, 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 > > > > > > > > Fix this by raising the name refcount and stabilizing it. Introduce > > > > vma_anon_name_{get/put} API for this purpose. > > > > > > What is the reason that madvise_update_vma uses the naked name rather > > > than the encapsulated anon_vma_name? This really just begs for problems. > > > > The reason for that is the second place it's being used from the prctl syscall: > > > > prctl_set_vma > > madvise_set_anon_name > > madvise_vma_anon_name > > madvise_update_vma > > > > In that case the name parameter is not part of any anon_vma_name > > struct and therefore is stable. I can add a comment to > > madvise_update_vma indicating that the name parameter has to be stable > > if that helps. > > Seems to me it'd simplify things if replace_vma_anon_name() and > madvise_vma_anon_name() took a struct anon_vma_name instead of > a bare char *. You could construct it in madvise_set_anon_name(). Ok, this can be done. However I don't think changing replace_vma_anon_name() to accept a struct anon_vma_name would be a good idea. Reader might think that the object being passed will become the vma->anon_name of the vma, while in reality that's not the case. Keeping it a char* makes it obvious that the function will construct a new anon_vma_name struct. I'll post a v3 shortly implementing these suggestions. Thanks for the review folks!