Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp776749pxp; Fri, 11 Mar 2022 14:52:01 -0800 (PST) X-Google-Smtp-Source: ABdhPJzcIqFZpKWoB464fRMmuA/VfX3s9Oq0WiWeUA1u7f744kAl9ZJ1STI8LhFvG/NuSnzXAbjD X-Received: by 2002:a65:610e:0:b0:378:d43b:9703 with SMTP id z14-20020a65610e000000b00378d43b9703mr10170347pgu.229.1647039121759; Fri, 11 Mar 2022 14:52:01 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1647039121; cv=none; d=google.com; s=arc-20160816; b=cxc6dZqtuQ30w1AeB4GbtrFO4g91fAJMZTJSkOehypnmEmgEDMQG3C7pvoF0NsZdXe C3gXgl15Ygy7IL73G1xbSiMF2euqg8fqVF3NQotMWQRznFoobwY13QBTFppRESnSPsff Rx3p0JJM/B6tQdVzKMDaaK2R7cTVW5ZDOnSPpk4pHCou1Nmf/S//2DA0xn02hPAYI6oO QCs0NOErlxYU+bXINXkwskBwMiexRkCHWaWLyyknFmeOKWeddcs72bBf7T0kR4cIWqfS w+Hoe1IIHGfa1MqIiXNvy1xbHu3J0b3/aJcLEFJ6ZuNDZEiIkDr4O78f769l4LGoyQ2D Ee6A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=KCmTmDIULOozSrwQB+RZx6LIMi9PHUhNmxXBcwVEqBI=; b=ojZ69IWf4HkueNA19Finy8ukQvH7FJ50vpAm2QhOgUbrmLWwsIUIrP8yOVnSVviqsh 37JeJ2s8j4Sg8ykhFIYIrxxd44YS1yuY+PDYXCUGMMKYuxYnILV3HwKeVjuv8HEWkKpX XKPRI3pQ7vJiYzMFenjHduPtWiLS3IpC1C+YCpvsuI+eLNUeA6XTH/3Pg5p3/SqfMN9F gwX1X/McAobdFQFTW5Z6T5gWn3mE85jL256rK/XFYZGmh1T4FqwiaVgSPjM+LdS+K+qj rdBl4iIDoB85/GPJSlQJskcWOdBGYlCo4wIBZ8NlYVHOs8BJnegP424Zg13OAJRc7uxA nyRg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=NIrtWgeo; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id j9-20020a056a00130900b004f777b51012si5839339pfu.306.2022.03.11.14.52.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Mar 2022 14:52:01 -0800 (PST) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=NIrtWgeo; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 81EB43534E5; Fri, 11 Mar 2022 13:49:55 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240024AbiCJQ3x (ORCPT + 99 others); Thu, 10 Mar 2022 11:29:53 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42604 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242554AbiCJQ3q (ORCPT ); Thu, 10 Mar 2022 11:29:46 -0500 Received: from mail-yb1-xb35.google.com (mail-yb1-xb35.google.com [IPv6:2607:f8b0:4864:20::b35]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8203F4CD49 for ; Thu, 10 Mar 2022 08:28:45 -0800 (PST) Received: by mail-yb1-xb35.google.com with SMTP id g1so11953309ybe.4 for ; Thu, 10 Mar 2022 08:28:45 -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=KCmTmDIULOozSrwQB+RZx6LIMi9PHUhNmxXBcwVEqBI=; b=NIrtWgeox+THnUtkq2c9KwIV7pR3KuQcyV1lIZ4k/4tK266hqCj0gxk5mcN7Zlo5RK kv+PTtNoRw14gWpZ4DfxWr1ToZYyMxMeG1dMp48HC7/cWFOapZVdBqa2i55qj1D7Dr96 Xzs4U2vXQ+ylEbXHPMresqa2vI2DuFaJwB7m40ef3LqoAuejH0Jd1oBCj0+1x9v3/MdB xQOF0KtlzO75qGRXIY/6AjMpKI783AUYvmN7PlgrvdXzUIwQuvv5zjRelJfZRiuTSl1U Yjqe0I8OseDSvpi7yxIF9tWfOMcjTGClVyB+pJDqoNInbeSf8485lHvRFsA+C/+NMyv5 A2sQ== 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=KCmTmDIULOozSrwQB+RZx6LIMi9PHUhNmxXBcwVEqBI=; b=d1M+BFE1CzvqQID74bjr0HqGnV9li9MpVlHCd49hFDZmJXkZ+9vMQBfmEKfksLKmji yuTTq6sBzy/ZuvrxsZ8TCZJthPhuaDdccAk2W/mNAbavQQKcQInMv6UCh1lagUZzuOgu gGo6OapJpglto9jyfcKKpHhLcz1AQmyTBaaShQsx1HdziofZ1WWQD8XCwGL6QxlEN+ap HHcm4ze2el7ZhZr+lRDlfI0vok9bcxe5UjZIYeMEg1lT1QS1ainxWDKe0twV6ZFZ788m 1zvhuE2fhnBBgoj4AuzaBsvwx2xqyLqbtCBS7ntyK33fOFmvOTYZyFHqFZTqeo6vvM8S Wttw== X-Gm-Message-State: AOAM5301JvAHz+aS9j9m9uTKit+80nKs+Z1lwziQn0NxrUQukmDu2WWf a/B6tuCa8ymQf1iD1x/ZJRrN3crvs5Gy7D5Zges+dw== X-Received: by 2002:a25:718b:0:b0:62c:16d8:d17f with SMTP id m133-20020a25718b000000b0062c16d8d17fmr4465877ybc.553.1646929724353; Thu, 10 Mar 2022 08:28:44 -0800 (PST) MIME-Version: 1.0 References: <20220215201922.1908156-1-surenb@google.com> <20220224201859.a38299b6c9d52cb51e6738ea@linux-foundation.org> <20220310155454.g6lt54yxel3ixnp3@revolver> In-Reply-To: <20220310155454.g6lt54yxel3ixnp3@revolver> From: Suren Baghdasaryan Date: Thu, 10 Mar 2022 08:28:33 -0800 Message-ID: Subject: Re: [PATCH 1/1] mm: fix use-after-free bug when mm->mmap is reused after being freed To: Liam Howlett Cc: Matthew Wilcox , Andrew Morton , "mhocko@kernel.org" , "mhocko@suse.com" , "shy828301@gmail.com" , "rientjes@google.com" , "hannes@cmpxchg.org" , "guro@fb.com" , "riel@surriel.com" , "minchan@kernel.org" , "kirill@shutemov.name" , "aarcange@redhat.com" , "brauner@kernel.org" , "christian@brauner.io" , "hch@infradead.org" , "oleg@redhat.com" , "david@redhat.com" , "jannh@google.com" , "shakeelb@google.com" , "luto@kernel.org" , "christian.brauner@ubuntu.com" , "fweimer@redhat.com" , "jengelh@inai.de" , "timmurray@google.com" , "linux-mm@kvack.org" , "linux-kernel@vger.kernel.org" , "kernel-team@android.com" , "syzbot+2ccf63a4bd07cf39cab0@syzkaller.appspotmail.com" Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.5 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=no 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 Thu, Mar 10, 2022 at 7:55 AM Liam Howlett wrote: > > * Suren Baghdasaryan [220225 00:51]: > > On Thu, Feb 24, 2022 at 8:23 PM Matthew Wilcox wrote: > > > > > > On Thu, Feb 24, 2022 at 08:18:59PM -0800, Andrew Morton wrote: > > > > On Tue, 15 Feb 2022 12:19:22 -0800 Suren Baghdasaryan wrote: > > > > > > > > > After exit_mmap frees all vmas in the mm, mm->mmap needs to be reset, > > > > > otherwise it points to a vma that was freed and when reused leads to > > > > > a use-after-free bug. > > > > > > > > > > ... > > > > > > > > > > --- a/mm/mmap.c > > > > > +++ b/mm/mmap.c > > > > > @@ -3186,6 +3186,7 @@ void exit_mmap(struct mm_struct *mm) > > > > > vma = remove_vma(vma); > > > > > cond_resched(); > > > > > } > > > > > + mm->mmap = NULL; > > > > > mmap_write_unlock(mm); > > > > > vm_unacct_memory(nr_accounted); > > > > > } > > > > > > > > After the Maple tree patches, mm_struct.mmap doesn't exist. So I'll > > > > revert this fix as part of merging the maple-tree parts of linux-next. > > > > I'll be sending this fix to Linus this week. > > > > > > > > All of which means that the thusly-resolved Maple tree patches might > > > > reintroduce this use-after-free bug. > > > > > > I don't think so? The problem is that VMAs are (currently) part of > > > two data structures -- the rbtree and the linked list. remove_vma() > > > only removes VMAs from the rbtree; it doesn't set mm->mmap to NULL. > > > > > > With maple tree, the linked list goes away. remove_vma() removes VMAs > > > from the maple tree. So anyone looking to iterate over all VMAs has to > > > go and look in the maple tree for them ... and there's nothing there. > > > > Yes, I think you are right. With maple trees we don't need this fix. > > > Yes, this is correct. The maple tree removes the entire linked list... > but since the mm is unstable in the exit_mmap(), I had added the > destruction of the maple tree there. Maybe this is the wrong place to > be destroying the tree tracking the VMAs (althought this patch partially > destroys the VMA tracking linked list), but it brought my attention to > the race that this patch solves and the process_mrelease() function. > Couldn't this be avoided by using mmget_not_zero() instead of mmgrab() > in process_mrelease()? That's what we were doing before [1]. That unfortunately has a problem of process_mrelease possibly calling the last mmput and being blocked on IO completion in exit_aio. The race between exit_mmap and process_mrelease is solved by using mmap_lock. I think by destroying the maple tree in exit_mmap before the mmap_write_unlock call, you keep things working and functionality intact. Is there any reason this can't be done? [1] ba535c1caf3ee78a ("mm/oom_kill: allow process_mrelease to run under mmap_lock protection") > That would ensure we aren't stepping on an > exit_mmap() and potentially the locking change in exit_mmap() wouldn't > be needed either? Logically, I view this as process_mrelease() having > issue with the fact that the mmaps are no longer stable in tear down > regardless of the data structure that is used. > > Thanks, > Liam > > -- > To unsubscribe from this group and stop receiving emails from it, send an email to kernel-team+unsubscribe@android.com. >