Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753804AbdLOIBD (ORCPT ); Fri, 15 Dec 2017 03:01:03 -0500 Received: from mail-pg0-f67.google.com ([74.125.83.67]:36517 "EHLO mail-pg0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753123AbdLOIBA (ORCPT ); Fri, 15 Dec 2017 03:01:00 -0500 X-Google-Smtp-Source: ACJfBot6xvwYoIUQPxjGFindvYhok78bn2o/vXgoKlGSVShDBxlZxkKOzT/s3vvaGWy642QMRzn5J7/Bd/JVw4AkHJ4= MIME-Version: 1.0 In-Reply-To: References: <20171214201757.5393-1-xiyou.wangcong@gmail.com> <87efnwcwd3.fsf@xmission.com> From: Dmitry Vyukov Date: Fri, 15 Dec 2017 09:00:38 +0100 Message-ID: Subject: Re: [PATCH] exit: move exit_task_namespaces() after exit_task_work() To: "Eric W. Biederman" Cc: Cong Wang , LKML , Ingo Molnar , Al Viro , Andrew Morton , Linus Torvalds , stable Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2462 Lines: 68 On Fri, Dec 15, 2017 at 8:35 AM, Dmitry Vyukov wrote: > On Fri, Dec 15, 2017 at 7:56 AM, Eric W. Biederman > wrote: >> Cong Wang writes: >> >>> syzbot reported we have a use-after-free when mqueue_evict_inode() >>> is called on __cleanup_mnt() path, where the ipc ns is already >>> freed by the previous exit_task_namespaces(). We can just move >>> it after after exit_task_work() to avoid this use-after-free. >> >> How does that possibly work. (I haven't seen this syzbot report). >> >> Looking at the code we have get_ns_from_inode. Which takes the mq_lock, >> sees if the pointer is NULL and takes a reference if it is non-NULL. >> >> Meanwhile put_ipc_ns calls mq_clear_sbinfo(ns) with the mq_lock held >> when the count drops to zero. >> >> Where is the race in that? >> >> The rest of mqueue_evict_inode uses the returned pointer and >> tests that the pointer is non-NULL before user it. >> >> So either szbot is giving you a bad report or there is a subtle race >> there I am not seeing. The change below is not at all the proper way to >> fix a subtle race. >> >> Eric > > Cong, what was that report? Searching by > "exit_task_work|exit_task_namespaces" there are too many of them: > https://groups.google.com/forum/#!searchin/syzkaller-bugs/%22exit_task_work$7Cexit_task_namespaces%22%7Csort:date > > I can only say that syzbot does not make up reports. That's something > that actually happened and was provoked by userspace. Ah, found that bug: https://groups.google.com/d/msg/syzkaller-bugs/1XBaqnPSXzs/VF-eCSPuCQAJ >>> Reported-by: syzbot >>> Cc: Ingo Molnar >>> Cc: Al Viro >>> Cc: Andrew Morton >>> Cc: Linus Torvalds >>> Cc: stable@vger.kernel.org >>> Signed-off-by: Cong Wang >>> --- >>> kernel/exit.c | 2 +- >>> 1 file changed, 1 insertion(+), 1 deletion(-) >>> >>> diff --git a/kernel/exit.c b/kernel/exit.c >>> index 6b4298a41167..909e43c45158 100644 >>> --- a/kernel/exit.c >>> +++ b/kernel/exit.c >>> @@ -861,8 +861,8 @@ void __noreturn do_exit(long code) >>> exit_fs(tsk); >>> if (group_dead) >>> disassociate_ctty(1); >>> - exit_task_namespaces(tsk); >>> exit_task_work(tsk); >>> + exit_task_namespaces(tsk); >>> exit_thread(tsk); >>> >>> /*