From patchwork Mon Feb 25 09:03:32 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hou Tao X-Patchwork-Id: 10828359 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D26521399 for ; Mon, 25 Feb 2019 08:59:22 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C1D5229F97 for ; Mon, 25 Feb 2019 08:59:22 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B535729FA7; Mon, 25 Feb 2019 08:59:22 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 660D329F97 for ; Mon, 25 Feb 2019 08:59:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726417AbfBYI7U (ORCPT ); Mon, 25 Feb 2019 03:59:20 -0500 Received: from szxga07-in.huawei.com ([45.249.212.35]:32916 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1725863AbfBYI7U (ORCPT ); Mon, 25 Feb 2019 03:59:20 -0500 Received: from DGGEMS408-HUB.china.huawei.com (unknown [172.30.72.58]) by Forcepoint Email with ESMTP id 0317D85C2ED8357D4B6C; Mon, 25 Feb 2019 16:59:18 +0800 (CST) Received: from huawei.com (10.90.53.225) by DGGEMS408-HUB.china.huawei.com (10.3.19.208) with Microsoft SMTP Server id 14.3.408.0; Mon, 25 Feb 2019 16:59:13 +0800 From: Hou Tao To: , , Christoph Hellwig CC: Alexander Viro , Benjamin LaHaise Subject: [PATCH] aio: take an extra file reference before call vfs_poll() Date: Mon, 25 Feb 2019 17:03:32 +0800 Message-ID: <20190225090332.88640-1-houtao1@huawei.com> X-Mailer: git-send-email 2.16.2.dirty MIME-Version: 1.0 X-Originating-IP: [10.90.53.225] X-CFilter-Loop: Reflected Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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;