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 3BD82C433EF for ; Thu, 10 Mar 2022 16:28:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8F7E78D0002; Thu, 10 Mar 2022 11:28:46 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8A70B8D0001; Thu, 10 Mar 2022 11:28:46 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 797288D0002; Thu, 10 Mar 2022 11:28:46 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0083.hostedemail.com [216.40.44.83]) by kanga.kvack.org (Postfix) with ESMTP id 6C0178D0001 for ; Thu, 10 Mar 2022 11:28:46 -0500 (EST) Received: from smtpin28.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 183939D294 for ; Thu, 10 Mar 2022 16:28:46 +0000 (UTC) X-FDA: 79229010252.28.8558F51 Received: from mail-yb1-f174.google.com (mail-yb1-f174.google.com [209.85.219.174]) by imf17.hostedemail.com (Postfix) with ESMTP id 790EE40025 for ; Thu, 10 Mar 2022 16:28:45 +0000 (UTC) Received: by mail-yb1-f174.google.com with SMTP id g26so11893554ybj.10 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=MYe538MopfNuVuAjm/14gmf+zv/Ay6bD/vB+jRQCul1+THBqnYw8ExEBceSqzTNLPG xna0eCZ1tkvy06chTVdyHrHaidVex+mkm1/+MnT2PUInfokpJKZBkrmO6UoIvwb+mUua y5CCv3/NVyHEV3LaGpSQ2N55gV9vXOwZizpl8SNVFNGUpYMIB+BNNTfKFtuEiWtESPu7 b37Vyg+y8KN03rfUhdcJ8+fluSq1uxn1dTTTcmJLbE8lfG69zVV3qaFWe/Q/feYyvLLY P4NdNLdWh3GhWZY0KbUqM1FAZnYOBTpxbNXFVCX3KD4WyRa1/7tJ2zb5BBKokDF1BzVY BshQ== X-Gm-Message-State: AOAM530ERU8vHp3wIYoICCqJduGhl2cQmQAPm7oPJRP8tKBXVlyHjaig bKoNiuR55kA093hCpWqFeh6WMXu9F2YNyPz95UTPxg== X-Google-Smtp-Source: ABdhPJxYOldag4Zl+3/XgyPjYH24IVzC25AgQCcRlkznI9lMRHpIjirNsoWgGfK/aipoEHd2eDaaUxuH68qS65mUOuo= 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-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 790EE40025 X-Stat-Signature: xkjqq4ijxz6afte1wuew8s4rem5dbse8 Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=NIrtWgeo; spf=pass (imf17.hostedemail.com: domain of surenb@google.com designates 209.85.219.174 as permitted sender) smtp.mailfrom=surenb@google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspam-User: X-HE-Tag: 1646929725-279275 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, 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. >