Received: by 2002:a05:6358:111d:b0:dc:6189:e246 with SMTP id f29csp3648357rwi; Wed, 2 Nov 2022 01:13:30 -0700 (PDT) X-Google-Smtp-Source: AMsMyM54lO9Eir17wN62a0Bhd5aflLWOW2OJIVuRYJys5GCyhmYwnL9Z8KaGJgtjKDTVnGcxcUEW X-Received: by 2002:a05:6402:5024:b0:440:e4ad:f7b6 with SMTP id p36-20020a056402502400b00440e4adf7b6mr23183413eda.358.1667376810161; Wed, 02 Nov 2022 01:13:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667376810; cv=none; d=google.com; s=arc-20160816; b=a6uHG4RKU19mBOCM1L31tB+Jih0CtAlHay7dTZES+zL6sko2ZWd5OmdlO4OIQu97Mu d+jfY8hLyTIFcqhaTGDbXuSViFBe+FBWQ7ZbS6TAdR6T9AQ7toC9D3ZawMK0st1JHwbj BVw0S3CeRevunBMxvBc7Pl+dG7flmFcHXAGkEn47GXY3Ls19ebam4CTVt3oDlc0vP3Z7 W9zlNf1LCeOBMYBaJqCMTKrGvhSHAT/FHTe+uQlRMdhY4EMA8Nd78ENYskprbkdGgM2z 7Rfpr+a/TC7IP32b9cb+XVNR4IwQLbt6+Q6sjrSFSpAykM5JQQB+COpeCCPEvOVZLfw4 XtZA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:date:subject:cc:to:from; bh=znobtM6gi+dfLGTAt9sHLVQGIzF+0g9upgDSt7hdPcU=; b=xobf3xOdWJNB+3v3GsOFXjAUMsNondGyALP/qbWvFrABNq5x4RzkZogdcVqc97xS6h UBhVKMC5YbV8u+C6QlZXamFFH0pOct0Pe1qqBGG2TmRW6YeVE5Biq+PgcQqPkC0BQlpm dlNVjTtnlay9p6aIo5wGFLJJf1BFe03cYkftMvwOuCbxz+eQTUU6j33oaUFIcFDTXQHw f6hVGYQkwuVZks9Qu2mIdUfviXIiudxbREcGaLRpxj7zjFUkTVOQuI/uVUTO4RBWhlkZ yZlSpiWSe7Yfq3y7eoq4AvuRRf0f/e1ruotnPl0n5qt4k0yZINQL++Riubc2xPw5VGd0 Roqw== 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hh7-20020a170906a94700b007835897050esi11314731ejb.404.2022.11.02.01.13.05; Wed, 02 Nov 2022 01:13:30 -0700 (PDT) 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 S230373AbiKBHbI (ORCPT + 97 others); Wed, 2 Nov 2022 03:31:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53902 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229548AbiKBHbG (ORCPT ); Wed, 2 Nov 2022 03:31:06 -0400 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 54A2F23150 for ; Wed, 2 Nov 2022 00:31:05 -0700 (PDT) Received: from kwepemi500016.china.huawei.com (unknown [172.30.72.56]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4N2JTJ07JNzHvSF; Wed, 2 Nov 2022 15:30:43 +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; Wed, 2 Nov 2022 15:31:02 +0800 From: Shang XiaoJing To: , , , CC: Subject: [PATCH] tracing: kprobe: Fix memory leak in test_gen_kprobe/kretprobe_cmd() Date: Wed, 2 Nov 2022 15:29:54 +0800 Message-ID: <20221102072954.26555-1-shangxiaojing@huawei.com> X-Mailer: git-send-email 2.17.1 MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.175.100.227] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) 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 test_gen_kprobe_cmd() only free buf in fail path, hence buf will leak when there is no failure. Move kfree(buf) from fail path to common path to prevent the memleak. The same reason and solution in test_gen_kretprobe_cmd(). unreferenced object 0xffff888143b14000 (size 2048): comm "insmod", pid 52490, jiffies 4301890980 (age 40.553s) hex dump (first 32 bytes): 70 3a 6b 70 72 6f 62 65 73 2f 67 65 6e 5f 6b 70 p:kprobes/gen_kp 72 6f 62 65 5f 74 65 73 74 20 64 6f 5f 73 79 73 robe_test do_sys backtrace: [<000000006d7b836b>] kmalloc_trace+0x27/0xa0 [<0000000009528b5b>] 0xffffffffa059006f [<000000008408b580>] do_one_initcall+0x87/0x2a0 [<00000000c4980a7e>] do_init_module+0xdf/0x320 [<00000000d775aad0>] load_module+0x3006/0x3390 [<00000000e9a74b80>] __do_sys_finit_module+0x113/0x1b0 [<000000003726480d>] do_syscall_64+0x35/0x80 [<000000003441e93b>] entry_SYSCALL_64_after_hwframe+0x46/0xb0 Fixes: 64836248dda2 ("tracing: Add kprobe event command generation test module") Signed-off-by: Shang XiaoJing --- kernel/trace/kprobe_event_gen_test.c | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/kernel/trace/kprobe_event_gen_test.c b/kernel/trace/kprobe_event_gen_test.c index 80e04a1e1977..d81f7c51025c 100644 --- a/kernel/trace/kprobe_event_gen_test.c +++ b/kernel/trace/kprobe_event_gen_test.c @@ -100,20 +100,20 @@ static int __init test_gen_kprobe_cmd(void) KPROBE_GEN_TEST_FUNC, KPROBE_GEN_TEST_ARG0, KPROBE_GEN_TEST_ARG1); if (ret) - goto free; + goto out; /* Use kprobe_event_add_fields to add the rest of the fields */ ret = kprobe_event_add_fields(&cmd, KPROBE_GEN_TEST_ARG2, KPROBE_GEN_TEST_ARG3); if (ret) - goto free; + goto out; /* * This actually creates the event. */ ret = kprobe_event_gen_cmd_end(&cmd); if (ret) - goto free; + goto out; /* * Now get the gen_kprobe_test event file. We need to prevent @@ -136,13 +136,11 @@ static int __init test_gen_kprobe_cmd(void) goto delete; } out: + kfree(buf); return ret; delete: /* We got an error after creating the event, delete it */ ret = kprobe_event_delete("gen_kprobe_test"); - free: - kfree(buf); - goto out; } @@ -170,14 +168,14 @@ static int __init test_gen_kretprobe_cmd(void) KPROBE_GEN_TEST_FUNC, "$retval"); if (ret) - goto free; + goto out; /* * This actually creates the event. */ ret = kretprobe_event_gen_cmd_end(&cmd); if (ret) - goto free; + goto out; /* * Now get the gen_kretprobe_test event file. We need to @@ -201,13 +199,11 @@ static int __init test_gen_kretprobe_cmd(void) goto delete; } out: + kfree(buf); return ret; delete: /* We got an error after creating the event, delete it */ ret = kprobe_event_delete("gen_kretprobe_test"); - free: - kfree(buf); - goto out; } -- 2.17.1