Received: by 2002:ac0:aed5:0:0:0:0:0 with SMTP id t21csp2886908imb; Mon, 4 Mar 2019 17:18:21 -0800 (PST) X-Google-Smtp-Source: APXvYqyLy3UNRCi1URVY1x+j8Dj+HyZigQ6MrJrNIaz0mtoE2iAnXcFeHBqfk5zBRqfSivuMGJr5 X-Received: by 2002:a17:902:2c01:: with SMTP id m1mr6911932plb.314.1551748701754; Mon, 04 Mar 2019 17:18:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1551748701; cv=none; d=google.com; s=arc-20160816; b=EiHAYFnh3tD8r4a48e/KJj2/Iro3kdzmNxgMh9jf3/LWp4MqcQZSiLBIdh3CXzhxrp vd8dDdx3jIWI04M+KBD373j+Y2IgsJNmFBNHFGgjXc0B5zkgaVCdccjnhOh4xOm+oiJJ lqyHA9c4FgMi7NXkK/5Gq9mqFM6yBJB6RFqDgz2byVTGQRAs2w5nLe1Uchw3FMeCd8a7 1RwcUJGZ0UopBXj4G/+z4nS/B9DK7i/izJiq0tySuBC6Zyqr/tSnJ+w7wqea8UBmCka6 wf1joQc8JyVAmAD9NtEs0eHoAsDPFyI8oesuqBPkS6jHZhs5Q3gmjJrhDfY21UcoEsXZ ES4A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:references:cc:to:from:subject; bh=NBIRxjPAU1R12zgV1FLLCZahZoCfJp7zZWtHvEkfxJA=; b=asSvwJLiSCnmjjGNL3LSa+nUkcwFoNI1QvpP9d0jI0DvnMspXD9S5psLbiET5z0MQ2 i8368TckN3qv71qeN4TCkBsEK8jRxubfwdS5sPtuAe3TJRsliuBepoiAyExrfwHScpjp O4VREyCX+8FGO5kUl1X0NEyVryHiEdMvHd7cZzILSxbW9KIpuNgidQCo1NwPtfir9DOE Su1ovDEylX0VUIJfP2BPpwrAhaCr3C04gFWrf+Viyi7/Vj+aPgUgfFXl8U4YZc9qKRpK ZhQvbqHxfDOw7UIoNPRfPmtKBy+odEy9N1BGebOWQfVqHV7qLtmB9h1BQLufkNen86+f S3hg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 134si6847101pfz.274.2019.03.04.17.18.06; Mon, 04 Mar 2019 17:18:21 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726925AbfCEBRM (ORCPT + 99 others); Mon, 4 Mar 2019 20:17:12 -0500 Received: from szxga05-in.huawei.com ([45.249.212.191]:4768 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726066AbfCEBRM (ORCPT ); Mon, 4 Mar 2019 20:17:12 -0500 Received: from DGGEMS408-HUB.china.huawei.com (unknown [172.30.72.60]) by Forcepoint Email with ESMTP id F165B8DD6C8E5A19A2CA; Tue, 5 Mar 2019 09:17:09 +0800 (CST) Received: from [127.0.0.1] (10.177.31.14) by DGGEMS408-HUB.china.huawei.com (10.3.19.208) with Microsoft SMTP Server id 14.3.408.0; Tue, 5 Mar 2019 09:17:08 +0800 Subject: Re: [PATCH] aio: take an extra file reference before call vfs_poll() From: Hou Tao To: , , Christoph Hellwig CC: Alexander Viro , Benjamin LaHaise , LKML References: <20190225090332.88640-1-houtao1@huawei.com> Message-ID: <978dc206-d28e-fb5d-e857-8d16e160a87c@huawei.com> Date: Tue, 5 Mar 2019 09:17:07 +0800 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.8.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset="utf-8" Content-Language: en-US Content-Transfer-Encoding: 7bit X-Originating-IP: [10.177.31.14] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org ping ? On 2019/3/1 18:09, Hou Tao wrote: > ping ? > > On 2019/2/25 17:03, Hou Tao wrote: >> Taking an extra file reference before call vfs_poll(), else >> the file may be released by aio_poll_wake() if an expected >> event is triggered immediately (e.g., by the close of a >> pair of pipes) after the return of vfs_poll(), and we may >> hit a use-after-free splat as shown below: >> >> BUG: KASAN: use-after-free in perf_trace_lock_acquire+0x3ab/0x570 >> Read of size 8 at addr ffff888379bfd4b0 by task syz-executor.1/4953 >> >> CPU: 0 PID: 4953 Comm: syz-executor.1 Not tainted 4.19.24 >> Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.10.2-1ubuntu1 >> Call Trace: >> __dump_stack lib/dump_stack.c:77 [inline] >> dump_stack+0xca/0x13e lib/dump_stack.c:113 >> print_address_description+0x79/0x330 mm/kasan/report.c:256 >> kasan_report_error mm/kasan/report.c:354 [inline] >> kasan_report+0x18a/0x2e0 mm/kasan/report.c:412 >> trace_event_get_offsets_lock_acquire include/trace/events/lock.h:13 [inline] >> perf_trace_lock_acquire+0x3ab/0x570 include/trace/events/lock.h:13 >> trace_lock_acquire include/trace/events/lock.h:13 [inline] >> lock_acquire+0x202/0x310 kernel/locking/lockdep.c:3899 >> __raw_spin_lock include/linux/spinlock_api_smp.h:142 [inline] >> _raw_spin_lock+0x2c/0x40 kernel/locking/spinlock.c:144 >> spin_lock include/linux/spinlock.h:329 [inline] >> aio_poll fs/aio.c:1750 [inline] >> io_submit_one+0xb90/0x1b30 fs/aio.c:1853 >> __do_sys_io_submit fs/aio.c:1919 [inline] >> __se_sys_io_submit fs/aio.c:1890 [inline] >> __x64_sys_io_submit+0x19b/0x500 fs/aio.c:1890 >> do_syscall_64+0xc8/0x580 arch/x86/entry/common.c:290 >> entry_SYSCALL_64_after_hwframe+0x49/0xbe >> ...... >> Allocated by task 4953: >> set_track mm/kasan/kasan.c:460 [inline] >> kasan_kmalloc+0xa0/0xd0 mm/kasan/kasan.c:553 >> kmem_cache_alloc_trace+0x12f/0x2d0 mm/slub.c:2733 >> kmalloc include/linux/slab.h:513 [inline] >> kzalloc include/linux/slab.h:707 [inline] >> alloc_pipe_info+0xdf/0x410 fs/pipe.c:633 >> get_pipe_inode fs/pipe.c:712 [inline] >> create_pipe_files+0x98/0x780 fs/pipe.c:744 >> __do_pipe_flags+0x35/0x230 fs/pipe.c:781 >> do_pipe2+0x87/0x150 fs/pipe.c:829 >> __do_sys_pipe2 fs/pipe.c:847 [inline] >> __se_sys_pipe2 fs/pipe.c:845 [inline] >> __x64_sys_pipe2+0x55/0x80 fs/pipe.c:845 >> do_syscall_64+0xc8/0x580 arch/x86/entry/common.c:290 >> entry_SYSCALL_64_after_hwframe+0x49/0xbe >> >> Freed by task 4952: >> set_track mm/kasan/kasan.c:460 [inline] >> __kasan_slab_free+0x12e/0x180 mm/kasan/kasan.c:521 >> slab_free_hook mm/slub.c:1371 [inline] >> slab_free_freelist_hook mm/slub.c:1398 [inline] >> slab_free mm/slub.c:2953 [inline] >> kfree+0xeb/0x2f0 mm/slub.c:3906 >> put_pipe_info+0xb0/0xd0 fs/pipe.c:556 >> pipe_release+0x1ab/0x240 fs/pipe.c:577 >> __fput+0x27f/0x7f0 fs/file_table.c:278 >> task_work_run+0x136/0x1b0 kernel/task_work.c:113 >> tracehook_notify_resume include/linux/tracehook.h:193 [inline] >> exit_to_usermode_loop+0x1a7/0x1d0 arch/x86/entry/common.c:166 >> prepare_exit_to_usermode arch/x86/entry/common.c:197 [inline] >> syscall_return_slowpath arch/x86/entry/common.c:268 [inline] >> do_syscall_64+0x461/0x580 arch/x86/entry/common.c:293 >> entry_SYSCALL_64_after_hwframe+0x49/0xbe >> >> Fixes: bfe4037e722e ("aio: implement IOCB_CMD_POLL") >> Cc: stable@vger.kernel.org [4.19+] >> Signed-off-by: Hou Tao >> --- >> fs/aio.c | 8 ++++++++ >> 1 file changed, 8 insertions(+) >> >> diff --git a/fs/aio.c b/fs/aio.c >> index aaaaf4d12c73..ea2f5de4feac 100644 >> --- a/fs/aio.c >> +++ b/fs/aio.c >> @@ -1763,6 +1763,12 @@ static ssize_t aio_poll(struct aio_kiocb *aiocb, const struct iocb *iocb) >> /* one for removal from waitqueue, one for this function */ >> refcount_set(&aiocb->ki_refcnt, 2); >> >> + /* >> + * file may be released by aio_poll_wake() if an expected event >> + * is triggered immediately after the return of vfs_poll(), so >> + * an extra reference is needed here to prevent use-after-free. >> + */ >> + get_file(req->file); >> mask = vfs_poll(req->file, &apt.pt) & req->events; >> if (unlikely(!req->head)) { >> /* we did not manage to set up a waitqueue, done */ >> @@ -1788,6 +1794,8 @@ static ssize_t aio_poll(struct aio_kiocb *aiocb, const struct iocb *iocb) >> spin_unlock_irq(&ctx->ctx_lock); >> >> out: >> + /* release the extra reference for vfs_poll() */ >> + fput(req->file); >> if (unlikely(apt.error)) { >> fput(req->file); >> return apt.error; >> > > > . >