From patchwork Fri Jul 1 07:39:35 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Omar Sandoval X-Patchwork-Id: 9209377 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 10EFF60221 for ; Fri, 1 Jul 2016 07:39:53 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0514E284FA for ; Fri, 1 Jul 2016 07:39:53 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id EDC1028533; Fri, 1 Jul 2016 07:39:52 +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=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID 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 66C06284FA for ; Fri, 1 Jul 2016 07:39:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752226AbcGAHjv (ORCPT ); Fri, 1 Jul 2016 03:39:51 -0400 Received: from mail-pf0-f180.google.com ([209.85.192.180]:36767 "EHLO mail-pf0-f180.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752003AbcGAHju (ORCPT ); Fri, 1 Jul 2016 03:39:50 -0400 Received: by mail-pf0-f180.google.com with SMTP id t190so37938750pfb.3 for ; Fri, 01 Jul 2016 00:39:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=osandov-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id; bh=HdLAWbT6S5rUU1QgLRwAb1I5ZIZP1ynCBwT/uaQlR6U=; b=SemBr6l02ku+1PyUbPQFEq6wLkDZmXKxnI9hnEzFgerFuFEl0VI71agAVfL8RMujVP qn3ysYUkb9SVNCeumkYhWSJW+2e81i6FDAZl4YgtqcxW4ssQTRtTZINB7yt2SJ1pDLsA ZkndXcQDT1gdPmUPrd+epZlrX1EEME4CDTm36q5AUC9uAyRlbA2mcCUlV95isgnXOHIv x7XF5JS1nAZkrAVa12XLoS43yG3LXR06UxbdoI2y8QZjItWVwDMUCACZXwKpmrcUZhPm 8VPhT2NNXKOyyj58xepDlz35ph5Dp4hV7O43ke7fL08kHjr9W2tk6uUDFxrMxd3SVWvN L6rQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=HdLAWbT6S5rUU1QgLRwAb1I5ZIZP1ynCBwT/uaQlR6U=; b=Z/i+WS/qdfw7jQVrDVQlyDrjTKbhAjiLVNHnZ07UDu55084Vr9rvru+h4vaOdFXlN6 sem3liATo+VZEs/JxRtLk46yqZDBzHHXg26cMBI9ugA6Qn8asM7jMvjZDqs97r5anqsa M+drpWOl+MqjP7H1b5YljmHyzrFXuMetV0S/zuuBzJlRocdc6Dja2C3Dh5eG6p76PR1M z8wva5/jFYdLgWTh+pm4TQM11PdlycYC4hbzMjNaUcwArpK0fvBGiaasTYskbXoY+ram VL8ElgeL/k/yNEGjJCQD11tMts2SrXf9dGRDEDXKSDd8PZB2fHmg8rGw2exoEiOdPrS7 SspQ== X-Gm-Message-State: ALyK8tLEPUNrFueXade1Cz7iFOFgNPkpsQdY3ucueWteUkjebjOmicG2Lux0VHQQqRKwMXaM X-Received: by 10.98.32.65 with SMTP id g62mr29164095pfg.12.1467358789799; Fri, 01 Jul 2016 00:39:49 -0700 (PDT) Received: from vader.thefacebook.com ([2620:10d:c090:180::1:ca2]) by smtp.gmail.com with ESMTPSA id t128sm3061375pfb.14.2016.07.01.00.39.48 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 01 Jul 2016 00:39:49 -0700 (PDT) From: Omar Sandoval To: Jens Axboe Cc: Dmitry Vyukov , linux-block@vger.kernel.org, kernel-team@fb.com, stable@vger.kernel.org Subject: [PATCH] block: fix use-after-free in sys_ioprio_get() Date: Fri, 1 Jul 2016 00:39:35 -0700 Message-Id: X-Mailer: git-send-email 2.9.0 Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Omar Sandoval get_task_ioprio() accesses the task->io_context without holding the task lock and thus can race with exit_io_context(), leading to a use-after-free. The reproducer below hits this within a few seconds on my 4-core QEMU VM: #define _GNU_SOURCE #include #include #include #include int main(int argc, char **argv) { pid_t pid, child; long nproc, i; /* ioprio_set(IOPRIO_WHO_PROCESS, 0, IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0)); */ syscall(SYS_ioprio_set, 1, 0, 0x6000); nproc = sysconf(_SC_NPROCESSORS_ONLN); for (i = 0; i < nproc; i++) { pid = fork(); assert(pid != -1); if (pid == 0) { for (;;) { pid = fork(); assert(pid != -1); if (pid == 0) { _exit(0); } else { child = wait(NULL); assert(child == pid); } } } pid = fork(); assert(pid != -1); if (pid == 0) { for (;;) { /* ioprio_get(IOPRIO_WHO_PGRP, 0); */ syscall(SYS_ioprio_get, 2, 0); } } } for (;;) { /* ioprio_get(IOPRIO_WHO_PGRP, 0); */ syscall(SYS_ioprio_get, 2, 0); } return 0; } This gets us KASAN dumps like this: [ 35.526914] ================================================================== [ 35.530009] BUG: KASAN: out-of-bounds in get_task_ioprio+0x7b/0x90 at addr ffff880066f34e6c [ 35.530009] Read of size 2 by task ioprio-gpf/363 [ 35.530009] ============================================================================= [ 35.530009] BUG blkdev_ioc (Not tainted): kasan: bad access detected [ 35.530009] ----------------------------------------------------------------------------- [ 35.530009] Disabling lock debugging due to kernel taint [ 35.530009] INFO: Allocated in create_task_io_context+0x2b/0x370 age=0 cpu=0 pid=360 [ 35.530009] ___slab_alloc+0x55d/0x5a0 [ 35.530009] __slab_alloc.isra.20+0x2b/0x40 [ 35.530009] kmem_cache_alloc_node+0x84/0x200 [ 35.530009] create_task_io_context+0x2b/0x370 [ 35.530009] get_task_io_context+0x92/0xb0 [ 35.530009] copy_process.part.8+0x5029/0x5660 [ 35.530009] _do_fork+0x155/0x7e0 [ 35.530009] SyS_clone+0x19/0x20 [ 35.530009] do_syscall_64+0x195/0x3a0 [ 35.530009] return_from_SYSCALL_64+0x0/0x6a [ 35.530009] INFO: Freed in put_io_context+0xe7/0x120 age=0 cpu=0 pid=1060 [ 35.530009] __slab_free+0x27b/0x3d0 [ 35.530009] kmem_cache_free+0x1fb/0x220 [ 35.530009] put_io_context+0xe7/0x120 [ 35.530009] put_io_context_active+0x238/0x380 [ 35.530009] exit_io_context+0x66/0x80 [ 35.530009] do_exit+0x158e/0x2b90 [ 35.530009] do_group_exit+0xe5/0x2b0 [ 35.530009] SyS_exit_group+0x1d/0x20 [ 35.530009] entry_SYSCALL_64_fastpath+0x1a/0xa4 [ 35.530009] INFO: Slab 0xffffea00019bcd00 objects=20 used=4 fp=0xffff880066f34ff0 flags=0x1fffe0000004080 [ 35.530009] INFO: Object 0xffff880066f34e58 @offset=3672 fp=0x0000000000000001 [ 35.530009] ================================================================== Fix it by grabbing the task lock while we poke at the io_context. Cc: stable@vger.kernel.org Reported-by: Dmitry Vyukov Signed-off-by: Omar Sandoval --- Applies to v4.7-rc5. block/ioprio.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/block/ioprio.c b/block/ioprio.c index cc7800e9eb44..01b8116298a1 100644 --- a/block/ioprio.c +++ b/block/ioprio.c @@ -150,8 +150,10 @@ static int get_task_ioprio(struct task_struct *p) if (ret) goto out; ret = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, IOPRIO_NORM); + task_lock(p); if (p->io_context) ret = p->io_context->ioprio; + task_unlock(p); out: return ret; }