Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp5037944rdb; Tue, 12 Dec 2023 17:51:18 -0800 (PST) X-Google-Smtp-Source: AGHT+IH09DyvUTAj3AEne+YXgcnSXSEybtFdywvDSH5qrVIDuSDc2KQkgtezWFUCdEp4oAuw5FEr X-Received: by 2002:a17:90b:3b8f:b0:285:b08a:7817 with SMTP id pc15-20020a17090b3b8f00b00285b08a7817mr8230872pjb.13.1702432278005; Tue, 12 Dec 2023 17:51:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702432277; cv=none; d=google.com; s=arc-20160816; b=UIbd8vN2vhrCDeLW0tSLbUlbWNBpnj91hRrJdfVwNucgnS3P+x26IXugfewvEiUXeu 8i5f1sd4NlvGqQBY3Eo79zGTqxO4Lo1sjVySWoYnp3qL7ydHb4IjkSDmM81bHbiKy847 vVAKgIL/hW912NTy4xr6kW74/kitnlyqCA66Vr/+Q5FuHYY7B0Ps8dcTfLoWCqvTo/l4 AlWwcfi6yfvhLWRx/7qLhCqkibfOBTfN7DaFWRwgtWwmHmXi5yYsJgLPdrpDx3/rOwAH 1lgMvr5ZMb4JbVisfnfMBc7vX8YkePEdjSOT1KizMUNaSv/+RtBRU01qTe6Ke0/oJGnS /AQw== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=zMsfZg+OILrxqy88rHh4nhzVkJlQwfkTGHgDjMzWxJA=; fh=dYEuCgrvw3vNM8x6eBM3xFhuh+idOIQAte6V6ctjRm8=; b=PBn/SJizV742GkjStZB7dHJlGm66RWmjpRKzxkyhnydaNiCK9UzKhs7P9X6sMr7mqt /TJy/3whWVoSx0pBupB6MFYI0YHeqSRw8El6lYS0II5lxgfCbXKRe0UlTcnce68LbfkU qgV7ty3rqIS2KKWNkjjl/HCv1+Xq8w6RHZO1A5YNgT7QidRBYaNajxmiUXiILJ/uhbqm rd3TMMFniSdL1B9nU4IjEBxZgnE3kfa5iwgDvn4oOlNgplgkK3jR4b1sJ7n47cR1l7EZ aoIDQj2zhumKsscQZycbvAik7KLPIwANJrpn4EcRzzf4TTL+fvJzBcXXQeMq9k+XqrPw 6uNg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 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 pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id a9-20020a17090a854900b00286a2b05cb6si10046232pjw.145.2023.12.12.17.51.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 17:51:17 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 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 pete.vger.email (Postfix) with ESMTP id 0A77180A68B1; Tue, 12 Dec 2023 17:51:15 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1378226AbjLMBuw (ORCPT + 99 others); Tue, 12 Dec 2023 20:50:52 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56890 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1378199AbjLMBuv (ORCPT ); Tue, 12 Dec 2023 20:50:51 -0500 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7F881AD; Tue, 12 Dec 2023 17:50:56 -0800 (PST) Received: from mail.maildlp.com (unknown [172.19.163.48]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4Sqdjf4hdXzsSGS; Wed, 13 Dec 2023 09:50:46 +0800 (CST) Received: from dggpeml500012.china.huawei.com (unknown [7.185.36.15]) by mail.maildlp.com (Postfix) with ESMTPS id 0D2D6180032; Wed, 13 Dec 2023 09:50:54 +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; Wed, 13 Dec 2023 09:50:53 +0800 From: Zheng Yejian To: CC: , , , , , Subject: [PATCH v2] tracing: Fix uaf issue when open the hist or hist_debug file Date: Wed, 13 Dec 2023 09:51:38 +0800 Message-ID: <20231213015138.281888-1-zhengyejian1@huawei.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231212113546.6a51d359@gandalf.local.home> References: <20231212113546.6a51d359@gandalf.local.home> MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.67.175.61] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) 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 pete.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 (pete.vger.email [0.0.0.0]); Tue, 12 Dec 2023 17:51:15 -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, call tracing_{open,release}_file_tr() in file_operations callback to have the ref count and avoid 'trace_event_file' being freed. 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 Suggested-by: Steven Rostedt Signed-off-by: Zheng Yejian --- kernel/trace/trace_events_hist.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) Steve, thanks for your review! v2: - Introduce tracing_single_release_file_tr() to add the missing call for single_release() as suggested by Steve; Link: https://lore.kernel.org/all/20231212113546.6a51d359@gandalf.local.home/ - Slightly modify the commit message and comments. v1: Link: https://lore.kernel.org/all/20231212113317.4159890-1-zhengyejian1@huawei.com/ diff --git a/kernel/trace/trace_events_hist.c b/kernel/trace/trace_events_hist.c index 1abc07fba1b9..5296a08c0641 100644 --- a/kernel/trace/trace_events_hist.c +++ b/kernel/trace/trace_events_hist.c @@ -5619,14 +5619,22 @@ static int hist_show(struct seq_file *m, void *v) return ret; } +static int tracing_single_release_file_tr(struct inode *inode, struct file *filp) +{ + tracing_release_file_tr(inode, filp); + return single_release(inode, filp); +} + 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 +5642,7 @@ const struct file_operations event_hist_fops = { .open = event_hist_open, .read = seq_read, .llseek = seq_lseek, - .release = single_release, + .release = tracing_single_release_file_tr, }; #ifdef CONFIG_HIST_TRIGGERS_DEBUG @@ -5900,10 +5908,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 +5921,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_single_release_file_tr, }; #endif -- 2.25.1