Received: by 2002:a05:6a10:d5a5:0:0:0:0 with SMTP id gn37csp3729615pxb; Mon, 4 Oct 2021 08:23:51 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxkls9gXBo3E7fWUHMCJASJ3+DV2OyJA/HJXmKwCIvisBhcmuUMROiJOh5Ag1Ip7lO8p09f X-Received: by 2002:a17:90a:1f4a:: with SMTP id y10mr30693700pjy.225.1633361031277; Mon, 04 Oct 2021 08:23:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1633361031; cv=none; d=google.com; s=arc-20160816; b=Ec1SJtwHfHiRsyptQGXDctazdBl4DYKbd7tfDN/GC0zxspcmnW/g2N/n6yE0Mc4MOk sKGmLw/JAqF5rsFbMjmn69bFkhvdgmoAuZEgxrXqxko/PsfksBJ5a6kWUwJV8kePKt8r mS/WfexoVPEdluM7vuC9WdPA0GeETfKEMbxd0j+lGggF6hoTPWcxliHlXJOrksZPcaX9 uV/EUZw8zihuGw8bNWfp+aXdGiDacExek16kZckoEBHcVYcudN97ckSNT/O0JjkDYaKQ nq/bXpIJdPC7ntAeCpMUCMeFg33AHqhOFfbGsD3VbYe1f8cOg38xPfdGyFrFejw6OTok 7XMw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=7S9frGCh8WhR0BYKOiae/vkrLVyUlyzQAD77xgq6bFk=; b=Bcq9RDQZxSiE8ldlabg92oPEcp8b9+aax9NHv/48IEJveE8tj0ao4KIWPKmoe181hE 7Zue8Nh67Bzjk+dJQCQTSlrrJrBzDwGbvEWGJzeQ+Q3xlRoSypX3tCdGi4zI4ON0XZWF n3zfwaKRy9IC7qgLF670aGBRg8Ll1bxtZDWokf8hwYt12NA8n8qL7tEsaOpdfrQ6+DsU 7lrwWvYtUXgh22/WkKdw84BnjPliKEVq+K06rMuiumyQ/uLCe9v1+Lg1/9XYF7FbEcUY o88mfs+iSdWXeNoqYxwwz2fmFb7GU8rFbq80AFhotlnFndc07y0kE0M/48w3ZT8hkfo2 NUFA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=kRc5SHrI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id f9si19125384plo.229.2021.10.04.08.23.29; Mon, 04 Oct 2021 08:23:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=kRc5SHrI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235421AbhJDNKy (ORCPT + 99 others); Mon, 4 Oct 2021 09:10:54 -0400 Received: from mail.kernel.org ([198.145.29.99]:39466 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235596AbhJDNIk (ORCPT ); Mon, 4 Oct 2021 09:08:40 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id D4B6461B4B; Mon, 4 Oct 2021 13:02:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1633352553; bh=7qWnpFmAnzTW4l7vSTzlUKn3nHLDhw+0A705Ollhg0Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kRc5SHrIh78g8gigqdGdoqcrdPsmNAICnOOEmU9ptgiIykQVidhAX3SmNuaI/but1 gImX5HHqdMUUKan6bs4J5nwMZM3eUMKswm1uHPOn0JLXqmu2nTynFsPDbVZEc8chN5 7tirmMdx93deGsyBRsqsB9Zu3tV7EMU8wtJyMJOw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Zhihao Cheng , Jens Axboe , Sasha Levin Subject: [PATCH 4.19 32/95] blktrace: Fix uaf in blk_trace access after removing by sysfs Date: Mon, 4 Oct 2021 14:52:02 +0200 Message-Id: <20211004125034.614629743@linuxfoundation.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211004125033.572932188@linuxfoundation.org> References: <20211004125033.572932188@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Zhihao Cheng [ Upstream commit 5afedf670caf30a2b5a52da96eb7eac7dee6a9c9 ] There is an use-after-free problem triggered by following process: P1(sda) P2(sdb) echo 0 > /sys/block/sdb/trace/enable blk_trace_remove_queue synchronize_rcu blk_trace_free relay_close rcu_read_lock __blk_add_trace trace_note_tsk (Iterate running_trace_list) relay_close_buf relay_destroy_buf kfree(buf) trace_note(sdb's bt) relay_reserve buf->offset <- nullptr deference (use-after-free) !!! rcu_read_unlock [ 502.714379] BUG: kernel NULL pointer dereference, address: 0000000000000010 [ 502.715260] #PF: supervisor read access in kernel mode [ 502.715903] #PF: error_code(0x0000) - not-present page [ 502.716546] PGD 103984067 P4D 103984067 PUD 17592b067 PMD 0 [ 502.717252] Oops: 0000 [#1] SMP [ 502.720308] RIP: 0010:trace_note.isra.0+0x86/0x360 [ 502.732872] Call Trace: [ 502.733193] __blk_add_trace.cold+0x137/0x1a3 [ 502.733734] blk_add_trace_rq+0x7b/0xd0 [ 502.734207] blk_add_trace_rq_issue+0x54/0xa0 [ 502.734755] blk_mq_start_request+0xde/0x1b0 [ 502.735287] scsi_queue_rq+0x528/0x1140 ... [ 502.742704] sg_new_write.isra.0+0x16e/0x3e0 [ 502.747501] sg_ioctl+0x466/0x1100 Reproduce method: ioctl(/dev/sda, BLKTRACESETUP, blk_user_trace_setup[buf_size=127]) ioctl(/dev/sda, BLKTRACESTART) ioctl(/dev/sdb, BLKTRACESETUP, blk_user_trace_setup[buf_size=127]) ioctl(/dev/sdb, BLKTRACESTART) echo 0 > /sys/block/sdb/trace/enable & // Add delay(mdelay/msleep) before kernel enters blk_trace_free() ioctl$SG_IO(/dev/sda, SG_IO, ...) // Enters trace_note_tsk() after blk_trace_free() returned // Use mdelay in rcu region rather than msleep(which may schedule out) Remove blk_trace from running_list before calling blk_trace_free() by sysfs if blk_trace is at Blktrace_running state. Fixes: c71a896154119f ("blktrace: add ftrace plugin") Signed-off-by: Zhihao Cheng Link: https://lore.kernel.org/r/20210923134921.109194-1-chengzhihao1@huawei.com Signed-off-by: Jens Axboe Signed-off-by: Sasha Levin --- kernel/trace/blktrace.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/kernel/trace/blktrace.c b/kernel/trace/blktrace.c index 645048bb1e86..75ea1a5be31a 100644 --- a/kernel/trace/blktrace.c +++ b/kernel/trace/blktrace.c @@ -1661,6 +1661,14 @@ static int blk_trace_remove_queue(struct request_queue *q) if (bt == NULL) return -EINVAL; + if (bt->trace_state == Blktrace_running) { + bt->trace_state = Blktrace_stopped; + spin_lock_irq(&running_trace_lock); + list_del_init(&bt->running_list); + spin_unlock_irq(&running_trace_lock); + relay_flush(bt->rchan); + } + put_probe_ref(); synchronize_rcu(); blk_trace_free(bt); -- 2.33.0