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 61F15C433FE for ; Sat, 26 Feb 2022 03:11:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 04A758D0007; Fri, 25 Feb 2022 22:11:10 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id F3D2E8D0001; Fri, 25 Feb 2022 22:11:09 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E2B7F8D0007; Fri, 25 Feb 2022 22:11:09 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0172.hostedemail.com [216.40.44.172]) by kanga.kvack.org (Postfix) with ESMTP id D56B08D0001 for ; Fri, 25 Feb 2022 22:11:09 -0500 (EST) Received: from smtpin16.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 9E325181D02B0 for ; Sat, 26 Feb 2022 03:11:09 +0000 (UTC) X-FDA: 79183454658.16.7792599 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf31.hostedemail.com (Postfix) with ESMTP id 162B020003 for ; Sat, 26 Feb 2022 03:11:08 +0000 (UTC) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id E4303B8340B; Sat, 26 Feb 2022 03:11:07 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7583FC340EF; Sat, 26 Feb 2022 03:11:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1645845066; bh=vfJjeD6UaeMWHVng8jjExvaUWg885puAEYUfr8nUkAw=; h=Date:To:From:In-Reply-To:Subject:From; b=EJII1OXapn7RuHvP6jdzLMqnTq9Uyh+fZ4x+upCZqM/t0IZ1BskkZKcTOLeDCTJQt uLnVVxkIVmz1T9DYBZtdUjXahnxdkjEYSeRb5VJ33BnMI7TFFB/4l7R26wIbl2uP7V 94DMbhuYtZrJMnUC+M7LyxpdespMYcbwutmJgdrw= Date: Fri, 25 Feb 2022 19:11:05 -0800 To: willy@infradead.org,timmurray@google.com,shy828301@gmail.com,shakeelb@google.com,roman.gushchin@linux.dev,rientjes@google.com,riel@surriel.com,oleg@redhat.com,minchan@kernel.org,mhocko@suse.com,luto@kernel.org,kirill@shutemov.name,jengelh@inai.de,jannh@google.com,hch@infradead.org,hannes@cmpxchg.org,fweimer@redhat.com,david@redhat.com,christian.brauner@ubuntu.com,brauner@kernel.org,aarcange@redhat.com,surenb@google.com,akpm@linux-foundation.org,patches@lists.linux.dev,linux-mm@kvack.org,mm-commits@vger.kernel.org,torvalds@linux-foundation.org,akpm@linux-foundation.org From: Andrew Morton In-Reply-To: <20220225191021.f71538a3f43dc448110e88b6@linux-foundation.org> Subject: [patch 05/12] mm: fix use-after-free bug when mm->mmap is reused after being freed Message-Id: <20220226031106.7583FC340EF@smtp.kernel.org> X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 162B020003 X-Rspam-User: Authentication-Results: imf31.hostedemail.com; dkim=pass header.d=linux-foundation.org header.s=korg header.b=EJII1OXa; spf=pass (imf31.hostedemail.com: domain of akpm@linux-foundation.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=akpm@linux-foundation.org; dmarc=none X-Stat-Signature: 6d5453s8i48cqka95844bz9pf4ywpumy X-HE-Tag: 1645845068-42653 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: From: Suren Baghdasaryan Subject: mm: fix use-after-free bug when mm->mmap is reused after being freed oom reaping (__oom_reap_task_mm) relies on a 2 way synchronization with exit_mmap. First it relies on the mmap_lock to exclude from unlock path[1], page tables tear down (free_pgtables) and vma destruction. This alone is not sufficient because mm->mmap is never reset. For historical reasons[2] the lock is taken there is also MMF_OOM_SKIP set for oom victims before. The oom reaper only ever looks at oom victims so the whole scheme works properly but process_mrelease can opearate on any task (with fatal signals pending) which doesn't really imply oom victims. That means that the MMF_OOM_SKIP part of the synchronization doesn't work and it can see a task after the whole address space has been demolished and traverse an already released mm->mmap list. This leads to use after free as properly caught up by KASAN report. Fix the issue by reseting mm->mmap so that MMF_OOM_SKIP synchronization is not needed anymore. The MMF_OOM_SKIP is not removed from exit_mmap yet but it acts mostly as an optimization now. [1] 27ae357fa82b ("mm, oom: fix concurrent munlock and oom reaper unmap, v3") [2] 212925802454 ("mm: oom: let oom_reap_task and exit_mmap run concurrently") [mhocko@suse.com: changelog rewrite] Link: https://lore.kernel.org/all/00000000000072ef2c05d7f81950@google.com/ Link: https://lkml.kernel.org/r/20220215201922.1908156-1-surenb@google.com Fixes: 64591e8605d6 ("mm: protect free_pgtables with mmap_lock write lock in exit_mmap") Signed-off-by: Suren Baghdasaryan Reported-by: syzbot+2ccf63a4bd07cf39cab0@syzkaller.appspotmail.com Suggested-by: Michal Hocko Reviewed-by: Rik van Riel Reviewed-by: Yang Shi Acked-by: Michal Hocko Cc: David Rientjes Cc: Matthew Wilcox Cc: Johannes Weiner Cc: Roman Gushchin Cc: Rik van Riel Cc: Minchan Kim Cc: Kirill A. Shutemov Cc: Andrea Arcangeli Cc: Christian Brauner Cc: Christoph Hellwig Cc: Oleg Nesterov Cc: David Hildenbrand Cc: Jann Horn Cc: Shakeel Butt Cc: Andy Lutomirski Cc: Christian Brauner Cc: Florian Weimer Cc: Jan Engelhardt Cc: Tim Murray Signed-off-by: Andrew Morton --- mm/mmap.c | 1 + 1 file changed, 1 insertion(+) --- a/mm/mmap.c~mm-fix-use-after-free-bug-when-mm-mmap-is-reused-after-being-freed +++ a/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); } _