From patchwork Thu Nov 17 01:23:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shang XiaoJing X-Patchwork-Id: 21369 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp158052wrr; Wed, 16 Nov 2022 17:31:02 -0800 (PST) X-Google-Smtp-Source: AA0mqf4GVR9Gb1HTHvWXxLaeBbTsWspvbJeUz6sv4dOIzpPnyPB7ipaKZwPfACe0UvEQQSePJ1k4 X-Received: by 2002:a17:906:ae93:b0:78a:3479:ec7d with SMTP id md19-20020a170906ae9300b0078a3479ec7dmr329310ejb.671.1668648662807; Wed, 16 Nov 2022 17:31:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668648662; cv=none; d=google.com; s=arc-20160816; b=wrJBhjCczgKPi4EG1dyHgFszNMxogr8V+nnNZm2fLwdIvg+ikFpz2fnBbjCuDbo/Yd UiUZJhJrGc0RbUHZMOTBrJT5J2l692pFb9KSSEGs1UXVdzpVWJ3q4qtPvbgftNfm8Jd4 QXzJg8KKp+NcKUcURd/OSYEGAs18Ob1SxX/ejkQG4sOKzMM2PpJ64uVLTj/AMx4f8Rxz SpospIvrAiT6yBgw52M8UVKUPyLQHdjduvXU09fTqHgz2vlQ9lJxXd77gAB7WgLl4RP1 O2nGiTydZs9TgLqAb8smYOVdKZmHGs1nobLa+NLYAQ1qJb3sMFWSaYUHYNQQcQ5TYv9B 5joQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:in-reply-to:message-id :date:subject:cc:to:from; bh=NZ5EOH/YzsRN9tEafrH9F5KNurYHSpQbQZ7H2geBXeM=; b=JqGzGclYAS0gNOOyNbWzakKE39s9NdMGMn8bu+yK3JGe4lQA3l4cT1sn3SJ292qfI0 W95xDVDQq/8bX0lzd2sYpBfGAAUVZT8gRpmQgS0CGpMiJVQ1P046poVtkKHZAfkVyufU Z/3JDENeBvbzUDWW/2n4pKZlnZTf9n++RqGHdI54ppT/jQLW/h8Fu38h+GDsfwKi/ATo /26RPzbeK6eKMK6jEbRB6dvAnXjG4Bqihe1WqCaboscYi6azfaWJ+x1hctbEZkRIkK41 ey8ZTVK+i0pkGIW2b9rybDILZSLRTKn48YHqAXn7y7F/pbp575Y6i98LT6PfhEKzBtxu KMBA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id cy24-20020a0564021c9800b00458cdba7a90si14925736edb.471.2022.11.16.17.30.35; Wed, 16 Nov 2022 17:31:02 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233803AbiKQBZ0 (ORCPT + 99 others); Wed, 16 Nov 2022 20:25:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33610 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233023AbiKQBZY (ORCPT ); Wed, 16 Nov 2022 20:25:24 -0500 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8A0D92CDDE for ; Wed, 16 Nov 2022 17:25:22 -0800 (PST) Received: from kwepemi500016.china.huawei.com (unknown [172.30.72.57]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4NCMfK51ypzmVsy; Thu, 17 Nov 2022 09:24:57 +0800 (CST) Received: from huawei.com (10.175.100.227) by kwepemi500016.china.huawei.com (7.221.188.220) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Thu, 17 Nov 2022 09:25:20 +0800 From: Shang XiaoJing To: , , , , CC: Subject: [PATCH v3 1/2] tracing: Fix memory leak in test_gen_synth_cmd() and test_empty_synth_event() Date: Thu, 17 Nov 2022 09:23:45 +0800 Message-ID: <20221117012346.22647-2-shangxiaojing@huawei.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20221117012346.22647-1-shangxiaojing@huawei.com> References: <20221117012346.22647-1-shangxiaojing@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.100.227] X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To kwepemi500016.china.huawei.com (7.221.188.220) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749704940074448166?= X-GMAIL-MSGID: =?utf-8?q?1749704940074448166?= test_gen_synth_cmd() only free buf in fail path, hence buf will leak when there is no failure. Add kfree(buf) to prevent the memleak. The same reason and solution in test_empty_synth_event(). unreferenced object 0xffff8881127de000 (size 2048): comm "modprobe", pid 247, jiffies 4294972316 (age 78.756s) hex dump (first 32 bytes): 20 67 65 6e 5f 73 79 6e 74 68 5f 74 65 73 74 20 gen_synth_test 20 70 69 64 5f 74 20 6e 65 78 74 5f 70 69 64 5f pid_t next_pid_ backtrace: [<000000004254801a>] kmalloc_trace+0x26/0x100 [<0000000039eb1cf5>] 0xffffffffa00083cd [<000000000e8c3bc8>] 0xffffffffa00086ba [<00000000c293d1ea>] do_one_initcall+0xdb/0x480 [<00000000aa189e6d>] do_init_module+0x1cf/0x680 [<00000000d513222b>] load_module+0x6a50/0x70a0 [<000000001fd4d529>] __do_sys_finit_module+0x12f/0x1c0 [<00000000b36c4c0f>] do_syscall_64+0x3f/0x90 [<00000000bbf20cf3>] entry_SYSCALL_64_after_hwframe+0x63/0xcd unreferenced object 0xffff8881127df000 (size 2048): comm "modprobe", pid 247, jiffies 4294972324 (age 78.728s) hex dump (first 32 bytes): 20 65 6d 70 74 79 5f 73 79 6e 74 68 5f 74 65 73 empty_synth_tes 74 20 20 70 69 64 5f 74 20 6e 65 78 74 5f 70 69 t pid_t next_pi backtrace: [<000000004254801a>] kmalloc_trace+0x26/0x100 [<00000000d4db9a3d>] 0xffffffffa0008071 [<00000000c31354a5>] 0xffffffffa00086ce [<00000000c293d1ea>] do_one_initcall+0xdb/0x480 [<00000000aa189e6d>] do_init_module+0x1cf/0x680 [<00000000d513222b>] load_module+0x6a50/0x70a0 [<000000001fd4d529>] __do_sys_finit_module+0x12f/0x1c0 [<00000000b36c4c0f>] do_syscall_64+0x3f/0x90 [<00000000bbf20cf3>] entry_SYSCALL_64_after_hwframe+0x63/0xcd Fixes: 9fe41efaca08 ("tracing: Add synth event generation test module") Signed-off-by: Shang XiaoJing --- changes in v2: - adjust code for clean. --- kernel/trace/synth_event_gen_test.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/kernel/trace/synth_event_gen_test.c b/kernel/trace/synth_event_gen_test.c index 0b15e975d2c2..8d77526892f4 100644 --- a/kernel/trace/synth_event_gen_test.c +++ b/kernel/trace/synth_event_gen_test.c @@ -120,15 +120,13 @@ static int __init test_gen_synth_cmd(void) /* Now generate a gen_synth_test event */ ret = synth_event_trace_array(gen_synth_test, vals, ARRAY_SIZE(vals)); - out: + free: + kfree(buf); return ret; delete: /* We got an error after creating the event, delete it */ synth_event_delete("gen_synth_test"); - free: - kfree(buf); - - goto out; + goto free; } /* @@ -227,15 +225,13 @@ static int __init test_empty_synth_event(void) /* Now trace an empty_synth_test event */ ret = synth_event_trace_array(empty_synth_test, vals, ARRAY_SIZE(vals)); - out: + free: + kfree(buf); return ret; delete: /* We got an error after creating the event, delete it */ synth_event_delete("empty_synth_test"); - free: - kfree(buf); - - goto out; + goto free; } static struct synth_field_desc create_synth_test_fields[] = { From patchwork Thu Nov 17 01:23:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shang XiaoJing X-Patchwork-Id: 21370 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp158729wrr; Wed, 16 Nov 2022 17:33:08 -0800 (PST) X-Google-Smtp-Source: AA0mqf4Ai50nZVo3d8eZPXLWhqxDb/E+LCtMRPcFAMbL7iQjPFRCgwn+0KXnAh0SMjLlLkxOaUMk X-Received: by 2002:a17:906:36cb:b0:7ad:c9b4:8faf with SMTP id b11-20020a17090636cb00b007adc9b48fafmr354861ejc.507.1668648788337; Wed, 16 Nov 2022 17:33:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668648788; cv=none; d=google.com; s=arc-20160816; b=1JePIs4r/drsEXVsdlsC3vqR3QEuumHXeOO010nT7L0HFIchedo/UEV6hHcmWn0S/H 5ncmLbqK2ZUcJ8wRhkn2wGma5Yv+NN1gxMfllQiJ0LlKv8lOhoDM874dtHCBmbBdtWor z+C5sFYLyChXn5BlUT8DRkYMcXwQx9lBwMcyCtWmO8grg3WgX4bVUGzHCsgY2vBeixar wr/ewcRx5dwpx5vPr+5FhINzxXzSN3Keq3PCVRH0ZN3k8YFeQ0/wl+w1FA7qHU3R+HVu VoZJeoh6vwiKBbg3Y+noZbYmiHanQyo/pW+oag7E9XUp3Q6kofs6XFxpSV4NMqs9HWiP PeIw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:in-reply-to:message-id :date:subject:cc:to:from; bh=0JLlm1pWNL1YmFdXkLdwxvlw7jWaF2WtU3iA/rSEwt4=; b=saprrRsCLD4X84ZdVqx/bTtZofG02Yeo3v22QYQA638t+J8Sm4r9GNCHoCg9CY3ucc WbAyHHoQtRJeeo8bLo1H0tpkucmMonsU8owBXzpJ3vAVQNAsfIaroKqyLI9/4D0dvTbY rGFmR+JF6YKA7CR7CEPCYSBL4JSXAKwbQsXEEtgf4iUUJMl5/0jUfqaAUXxUoLJ/yfAJ 9vJVzaDCCDyXbn6tmTQbgKkCe8UxFoLpiFEKcBgYhhp5BBcVtUe/RwsRZduUBHx7bMu2 Vdu0d1Os8+C3c0RdcdsVikxtZgsXiUAXAH0Ah9uvDOUfCYM5PQ0PDOiBgZDDl0ujrzRw TFjA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hc18-20020a170907169200b00782ff2649a7si16355941ejc.346.2022.11.16.17.32.45; Wed, 16 Nov 2022 17:33:08 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234544AbiKQBZd (ORCPT + 99 others); Wed, 16 Nov 2022 20:25:33 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33614 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233772AbiKQBZ0 (ORCPT ); Wed, 16 Nov 2022 20:25:26 -0500 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 772D73134D for ; Wed, 16 Nov 2022 17:25:23 -0800 (PST) Received: from kwepemi500016.china.huawei.com (unknown [172.30.72.55]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4NCMfN6GS2zRpNW; Thu, 17 Nov 2022 09:25:00 +0800 (CST) Received: from huawei.com (10.175.100.227) by kwepemi500016.china.huawei.com (7.221.188.220) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Thu, 17 Nov 2022 09:25:20 +0800 From: Shang XiaoJing To: , , , , CC: Subject: [PATCH v3 2/2] tracing: Fix wild-memory-access in register_synth_event() Date: Thu, 17 Nov 2022 09:23:46 +0800 Message-ID: <20221117012346.22647-3-shangxiaojing@huawei.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20221117012346.22647-1-shangxiaojing@huawei.com> References: <20221117012346.22647-1-shangxiaojing@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.100.227] X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To kwepemi500016.china.huawei.com (7.221.188.220) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749705071961255840?= X-GMAIL-MSGID: =?utf-8?q?1749705071961255840?= In register_synth_event(), if set_synth_event_print_fmt() failed, then both trace_remove_event_call() and unregister_trace_event() will be called, which means the trace_event_call will call __unregister_trace_event() twice. As the result, the second unregister will causes the wild-memory-access. register_synth_event set_synth_event_print_fmt failed trace_remove_event_call event_remove if call->event.funcs then __unregister_trace_event (first call) unregister_trace_event __unregister_trace_event (second call) Fix the bug by avoiding to call the second __unregister_trace_event() by checking if the first one is called. general protection fault, probably for non-canonical address 0xfbd59c0000000024: 0000 [#1] SMP KASAN PTI KASAN: maybe wild-memory-access in range [0xdead000000000120-0xdead000000000127] CPU: 0 PID: 3807 Comm: modprobe Not tainted 6.1.0-rc1-00186-g76f33a7eedb4 #299 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.15.0-0-g2dd4b9b3f840-prebuilt.qemu.org 04/01/2014 RIP: 0010:unregister_trace_event+0x6e/0x280 Code: 00 fc ff df 4c 89 ea 48 c1 ea 03 80 3c 02 00 0f 85 0e 02 00 00 48 b8 00 00 00 00 00 fc ff df 4c 8b 63 08 4c 89 e2 48 c1 ea 03 <80> 3c 02 00 0f 85 e2 01 00 00 49 89 2c 24 48 85 ed 74 28 e8 7a 9b RSP: 0018:ffff88810413f370 EFLAGS: 00010a06 RAX: dffffc0000000000 RBX: ffff888105d050b0 RCX: 0000000000000000 RDX: 1bd5a00000000024 RSI: ffff888119e276e0 RDI: ffffffff835a8b20 RBP: dead000000000100 R08: 0000000000000000 R09: fffffbfff0913481 R10: ffffffff8489a407 R11: fffffbfff0913480 R12: dead000000000122 R13: ffff888105d050b8 R14: 0000000000000000 R15: ffff888105d05028 FS: 00007f7823e8d540(0000) GS:ffff888119e00000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 00007f7823e7ebec CR3: 000000010a058002 CR4: 0000000000330ef0 DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400 Call Trace: __create_synth_event+0x1e37/0x1eb0 create_or_delete_synth_event+0x110/0x250 synth_event_run_command+0x2f/0x110 test_gen_synth_cmd+0x170/0x2eb [synth_event_gen_test] synth_event_gen_test_init+0x76/0x9bc [synth_event_gen_test] do_one_initcall+0xdb/0x480 do_init_module+0x1cf/0x680 load_module+0x6a50/0x70a0 __do_sys_finit_module+0x12f/0x1c0 do_syscall_64+0x3f/0x90 entry_SYSCALL_64_after_hwframe+0x63/0xcd Fixes: 4b147936fa50 ("tracing: Add support for 'synthetic' events") Signed-off-by: Shang XiaoJing Cc: stable@vger.kernel.org --- changes in v2: - remove the redundant if statement. changes in v3: - remove the redundant return statment. --- kernel/trace/trace_events_synth.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/kernel/trace/trace_events_synth.c b/kernel/trace/trace_events_synth.c index e310052dc83c..29fbfb27c2b2 100644 --- a/kernel/trace/trace_events_synth.c +++ b/kernel/trace/trace_events_synth.c @@ -828,10 +828,9 @@ static int register_synth_event(struct synth_event *event) } ret = set_synth_event_print_fmt(call); - if (ret < 0) { + /* unregister_trace_event() will be called inside */ + if (ret < 0) trace_remove_event_call(call); - goto err; - } out: return ret; err: