Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp4579568rdb; Tue, 12 Dec 2023 03:52:33 -0800 (PST) X-Google-Smtp-Source: AGHT+IGZAzLoW8QZznt3VH9oXepRRc7nVrrWyNn7C+OV6R4Z5wADNSBiJscU2Jz54ITpKb26Hw5O X-Received: by 2002:a05:6808:1b11:b0:3b9:d9d6:16ba with SMTP id bx17-20020a0568081b1100b003b9d9d616bamr8073911oib.74.1702381953247; Tue, 12 Dec 2023 03:52:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702381953; cv=none; d=google.com; s=arc-20160816; b=TpsYHxjslhASnedqpxsweBnT2OdXX/9lAZDX7a+UmIKyQzpFu+6kcJqKc1amunJSL7 VQXoc8V/3e0dNgWWljBv3d8sLRI8vM3LY3vpFJhfFumtIT9Mn+s6pzIBExu1JQwg1qPR fNenKqK/oV4FAor/Td2qNRjMRKh72DiGeHZH+Bh//sBkcqg2PCJjakX8xubhKdA5AQuY bay2wZObmoiD35tVjW3xwD3UsVStrsBjwtWr75OqMxdZo2oiAO2UUf2hrTf2vIkZraD7 /6qUx+uqO8oUh3Wxo9HZ6pZnCzuEAWJC5/fdCbbcuoz68mK6JJfEuoYZzywmzddjHMkz tUCA== 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 :message-id:date:subject:cc:to:from; bh=U+nmFrMtOkm1CpUJamNGYZH0uti0rfslizHDnaBMr8w=; fh=RexdSvDW4HeRq92dwXSWEXabUVyVwcRjSHcR3Gke+Ac=; b=JWdWkTnrx0h9karjgy+doV2DxuDtMzMPdMQTKEIJnWrek/NHuegWCleAFJQw2UBxsC GXIxuah9hmgZVCGFRCeI45Cm3a3cGZp7vhR0o3+RomO7cjEROCMYb7tcxWW65hTuKwxJ hmQnjqYLnnHRvtxynHpuAfI+rJNGkNJV79Epb8iSUHsa84vhWuEFrcw6fLnX15c+lLcF Jfrf1OWMFjFfkRpaA3ejQEOk97ulmEILd5ay11SAQpzfFaKOEg8jB7NWODLmxOL8oBf0 /ApZQSyJU4/6/J3UMYhyCwW56hzKRFuUoALjk1l7qj21EJmRAnz5wTvmmNv/w/dD4mdg b1Lw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id g13-20020a056a00078d00b006cb901a8775si7368727pfu.396.2023.12.12.03.52.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 03:52:33 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 2CF83805C711; Tue, 12 Dec 2023 03:52:30 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232412AbjLLLwQ (ORCPT + 99 others); Tue, 12 Dec 2023 06:52:16 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37772 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232388AbjLLLwP (ORCPT ); Tue, 12 Dec 2023 06:52:15 -0500 X-Greylist: delayed 1181 seconds by postgrey-1.37 at lindbergh.monkeyblade.net; Tue, 12 Dec 2023 03:52:18 PST Received: from szxga05-in.huawei.com (szxga05-in.huawei.com [45.249.212.191]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 277F7109; Tue, 12 Dec 2023 03:52:17 -0800 (PST) Received: from mail.maildlp.com (unknown [172.19.88.163]) by szxga05-in.huawei.com (SkyGuard) with ESMTP id 4SqGf74fmNz1kv72; Tue, 12 Dec 2023 19:31:27 +0800 (CST) Received: from dggpeml500012.china.huawei.com (unknown [7.185.36.15]) by mail.maildlp.com (Postfix) with ESMTPS id 1D5A81800C8; Tue, 12 Dec 2023 19:32:34 +0800 (CST) Received: from localhost.localdomain (10.67.175.61) by dggpeml500012.china.huawei.com (7.185.36.15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.35; Tue, 12 Dec 2023 19:32:33 +0800 From: Zheng Yejian To: , CC: , , , , Subject: [PATCH] tracing: Fix uaf issue when open the hist or hist_debug file Date: Tue, 12 Dec 2023 19:33:17 +0800 Message-ID: <20231212113317.4159890-1-zhengyejian1@huawei.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.67.175.61] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To dggpeml500012.china.huawei.com (7.185.36.15) X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Tue, 12 Dec 2023 03:52:30 -0800 (PST) KASAN report following issue. The root cause is when opening 'hist' file of an instance and accessing 'trace_event_file' in hist_show(), but 'trace_event_file' has been freed due to the instance being removed. 'hist_debug' file has the same problem. To fix it, use tracing_{open, release}_file_tr() as instead when open these two files. BUG: KASAN: slab-use-after-free in hist_show+0x11e0/0x1278 Read of size 8 at addr ffff242541e336b8 by task head/190 CPU: 4 PID: 190 Comm: head Not tainted 6.7.0-rc5-g26aff849438c #133 Hardware name: linux,dummy-virt (DT) Call trace: dump_backtrace+0x98/0xf8 show_stack+0x1c/0x30 dump_stack_lvl+0x44/0x58 print_report+0xf0/0x5a0 kasan_report+0x80/0xc0 __asan_report_load8_noabort+0x1c/0x28 hist_show+0x11e0/0x1278 seq_read_iter+0x344/0xd78 seq_read+0x128/0x1c0 vfs_read+0x198/0x6c8 ksys_read+0xf4/0x1e0 __arm64_sys_read+0x70/0xa8 invoke_syscall+0x70/0x260 el0_svc_common.constprop.0+0xb0/0x280 do_el0_svc+0x44/0x60 el0_svc+0x34/0x68 el0t_64_sync_handler+0xb8/0xc0 el0t_64_sync+0x168/0x170 Allocated by task 188: kasan_save_stack+0x28/0x50 kasan_set_track+0x28/0x38 kasan_save_alloc_info+0x20/0x30 __kasan_slab_alloc+0x6c/0x80 kmem_cache_alloc+0x15c/0x4a8 trace_create_new_event+0x84/0x348 __trace_add_new_event+0x18/0x88 event_trace_add_tracer+0xc4/0x1a0 trace_array_create_dir+0x6c/0x100 trace_array_create+0x2e8/0x568 instance_mkdir+0x48/0x80 tracefs_syscall_mkdir+0x90/0xe8 vfs_mkdir+0x3c4/0x610 do_mkdirat+0x144/0x200 __arm64_sys_mkdirat+0x8c/0xc0 invoke_syscall+0x70/0x260 el0_svc_common.constprop.0+0xb0/0x280 do_el0_svc+0x44/0x60 el0_svc+0x34/0x68 el0t_64_sync_handler+0xb8/0xc0 el0t_64_sync+0x168/0x170 Freed by task 191: kasan_save_stack+0x28/0x50 kasan_set_track+0x28/0x38 kasan_save_free_info+0x34/0x58 __kasan_slab_free+0xe4/0x158 kmem_cache_free+0x19c/0x508 event_file_put+0xa0/0x120 remove_event_file_dir+0x180/0x320 event_trace_del_tracer+0xb0/0x180 __remove_instance+0x224/0x508 instance_rmdir+0x44/0x78 tracefs_syscall_rmdir+0xbc/0x140 vfs_rmdir+0x1cc/0x4c8 do_rmdir+0x220/0x2b8 __arm64_sys_unlinkat+0xc0/0x100 invoke_syscall+0x70/0x260 el0_svc_common.constprop.0+0xb0/0x280 do_el0_svc+0x44/0x60 el0_svc+0x34/0x68 el0t_64_sync_handler+0xb8/0xc0 el0t_64_sync+0x168/0x170 Signed-off-by: Zheng Yejian --- kernel/trace/trace_events_hist.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/kernel/trace/trace_events_hist.c b/kernel/trace/trace_events_hist.c index 1abc07fba1b9..00447ea7dabd 100644 --- a/kernel/trace/trace_events_hist.c +++ b/kernel/trace/trace_events_hist.c @@ -5623,10 +5623,12 @@ static int event_hist_open(struct inode *inode, struct file *file) { int ret; - ret = security_locked_down(LOCKDOWN_TRACEFS); + ret = tracing_open_file_tr(inode, file); if (ret) return ret; + /* Clear private_data to avoid warning in single_open */ + file->private_data = NULL; return single_open(file, hist_show, file); } @@ -5634,7 +5636,7 @@ const struct file_operations event_hist_fops = { .open = event_hist_open, .read = seq_read, .llseek = seq_lseek, - .release = single_release, + .release = tracing_release_file_tr, }; #ifdef CONFIG_HIST_TRIGGERS_DEBUG @@ -5900,10 +5902,12 @@ static int event_hist_debug_open(struct inode *inode, struct file *file) { int ret; - ret = security_locked_down(LOCKDOWN_TRACEFS); + ret = tracing_open_file_tr(inode, file); if (ret) return ret; + /* Clear private_data to avoid warning in single_open */ + file->private_data = NULL; return single_open(file, hist_debug_show, file); } @@ -5911,7 +5915,7 @@ const struct file_operations event_hist_debug_fops = { .open = event_hist_debug_open, .read = seq_read, .llseek = seq_lseek, - .release = single_release, + .release = tracing_release_file_tr, }; #endif -- 2.25.1