Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp2087307rwi; Tue, 11 Oct 2022 04:48:48 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7YvqQgeUxbAnsUNgYjDAGmlb85m5l18Hp2BGrM5AevP+C2Ms9XIuY4zhvVJn+K5VoW0907 X-Received: by 2002:a17:90b:314b:b0:203:41c:2dbb with SMTP id ip11-20020a17090b314b00b00203041c2dbbmr26474076pjb.18.1665488927706; Tue, 11 Oct 2022 04:48:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665488927; cv=none; d=google.com; s=arc-20160816; b=QcNRBpJtubOXESWZZ5OunORBBGjMcmxIzsZjuc/a9yymkLqM1GFFTg66o8DATvN2zn ByHtdHqNT3O75cvKws7LDZJtdPKArHQcvAmuTX6kQwbHz6TsxfUBf0wB+6KRuX5/h4UU 5s4Ze5TqpFi39KLISP/erJ04zosLLufOanAQzcXreKwap+wl9iWPEhJN7ozTrnULRd0U 9FMV+QO0yWSKTMRDiDuatmjwgZlajD+8+N0/8sw1piFY1vgFhYlfQ07OCZxaTyZUKKPr u8HaLz/Lq4hh4K3EVSx6ZwzwqFMUTt6RUKKy1o2QLRP6/v0qRmJ7Kkf3yVPiBzFe/G/R xwVw== 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=Yw1JwCtRtaW9fK27IsXrZeesgfcsBNeuBauyFx18z0E=; b=z4xhhn1tiaZ/M65kUo1wWTsUu42iFy9gbvgnlQWeCCjrONkzXTHUjz1SjLH1tAhqi1 GTnFmI3r4FE9CaUEIHvFfxnnl5dsube3elZxKaJe994i3a1/tA5wNJ9lbdnSCnG9jy7r +mjj0E/MFm7M/uwrLaCelfRd0dcVkuz2alxpefCzxYWjCwOs9puXJSGeFsfx66YOExW6 NOqEDHNhUx0uOU60+fgplaA0nsGYIQ4i2ROOYKI1qBWbXPaNcjkcierjQrepX4im1RqW QMK68CTYxyjN5FTh31/5n/NslN+oJqCaRXMUz0eV8v1NBOp3EHGjna5pEuiQEIJYD5Wa micw== 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l16-20020a170902f69000b0016ce3d67e7csi16182496plg.387.2022.10.11.04.48.36; Tue, 11 Oct 2022 04:48:47 -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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229613AbiJKLn6 (ORCPT + 99 others); Tue, 11 Oct 2022 07:43:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60972 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229971AbiJKLnh (ORCPT ); Tue, 11 Oct 2022 07:43:37 -0400 Received: from dggsgout12.his.huawei.com (unknown [45.249.212.56]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3C1377CB6B; Tue, 11 Oct 2022 04:43:34 -0700 (PDT) Received: from mail02.huawei.com (unknown [172.30.67.143]) by dggsgout12.his.huawei.com (SkyGuard) with ESMTP id 4Mmv4Y2vPbz6Pmfc; Tue, 11 Oct 2022 19:41:17 +0800 (CST) Received: from k01.huawei.com (unknown [10.67.174.197]) by APP2 (Coremail) with SMTP id Syh0CgBX8NTXVkVj5HNpAA--.32480S6; Tue, 11 Oct 2022 19:43:32 +0800 (CST) From: Xu Kuohai To: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, netdev@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Mykola Lysenko , Shuah Khan , "David S . Miller" , Jakub Kicinski , Jesper Dangaard Brouer , Kumar Kartikeya Dwivedi , Alan Maguire , Delyan Kratunov , Lorenzo Bianconi Subject: [PATCH bpf-next v4 4/6] selftest/bpf: Fix memory leak in kprobe_multi_test Date: Tue, 11 Oct 2022 08:01:06 -0400 Message-Id: <20221011120108.782373-5-xukuohai@huaweicloud.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20221011120108.782373-1-xukuohai@huaweicloud.com> References: <20221011120108.782373-1-xukuohai@huaweicloud.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: Syh0CgBX8NTXVkVj5HNpAA--.32480S6 X-Coremail-Antispam: 1UD129KBjvJXoW7uFyfZw18AF1fXw43Cr13twb_yoW8ZFyDpa 4xCw4YkF1xAF1rWFn7Ga1kXry5ur4kZry8Cry5tw13uw1kAwn5JF4IkayfKas3GrWkX3Wr C3Z7Gr9rK3yDX3DanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUBSb4IE77IF4wAFF20E14v26rWj6s0DM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28IrcIa0xkI8VA2jI8067AKxVWUAV Cq3wA2048vs2IY020Ec7CjxVAFwI0_Xr0E3s1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0 rcxSw2x7M28EF7xvwVC0I7IYx2IY67AKxVW7JVWDJwA2z4x0Y4vE2Ix0cI8IcVCY1x0267 AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I0E 14v26rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7 xfMcIj6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Y z7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41lFIxGxcIEc7CjxVA2Y2ka0xkIwI1l42xK82IYc2 Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s02 6x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r4a6rW5MIIYrxkI7VAKI48JMIIF0x vE2Ix0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0I7IYx2IY6xkF7I0E14v26r4UJVWxJr1lIxAI cVCF04k26cxKx2IYs7xG6r1j6r1xMIIF0xvEx4A2jsIE14v26r4j6F4UMIIF0xvEx4A2js IEc7CjxVAFwI0_Gr1j6F4UJbIYCTnIWIevJa73UjIFyTuYvjxUFgAwUUUUU X-CM-SenderInfo: 50xn30hkdlqx5xdzvxpfor3voofrz/ X-CFilter-Loop: Reflected X-Spam-Status: No, score=-1.5 required=5.0 tests=BAYES_00,KHOP_HELO_FCRDNS, SPF_HELO_NONE,SPF_NONE autolearn=no 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 From: Xu Kuohai The get_syms() function in kprobe_multi_test.c does not free the string memory allocated by sscanf correctly. Fix it. Fixes: 5b6c7e5c4434 ("selftests/bpf: Add attach bench test") Signed-off-by: Xu Kuohai Acked-by: Jiri Olsa --- .../bpf/prog_tests/kprobe_multi_test.c | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c b/tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c index d457a55ff408..287b3ac40227 100644 --- a/tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c +++ b/tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c @@ -325,7 +325,7 @@ static bool symbol_equal(const void *key1, const void *key2, void *ctx __maybe_u static int get_syms(char ***symsp, size_t *cntp) { size_t cap = 0, cnt = 0, i; - char *name, **syms = NULL; + char *name = NULL, **syms = NULL; struct hashmap *map; char buf[256]; FILE *f; @@ -352,6 +352,8 @@ static int get_syms(char ***symsp, size_t *cntp) /* skip modules */ if (strchr(buf, '[')) continue; + + free(name); if (sscanf(buf, "%ms$*[^\n]\n", &name) != 1) continue; /* @@ -369,32 +371,32 @@ static int get_syms(char ***symsp, size_t *cntp) if (!strncmp(name, "__ftrace_invalid_address__", sizeof("__ftrace_invalid_address__") - 1)) continue; + err = hashmap__add(map, name, NULL); - if (err) { - free(name); - if (err == -EEXIST) - continue; + if (err == -EEXIST) + continue; + if (err) goto error; - } + err = libbpf_ensure_mem((void **) &syms, &cap, sizeof(*syms), cnt + 1); - if (err) { - free(name); + if (err) goto error; - } - syms[cnt] = name; - cnt++; + + syms[cnt++] = name; + name = NULL; } *symsp = syms; *cntp = cnt; error: + free(name); fclose(f); hashmap__free(map); if (err) { for (i = 0; i < cnt; i++) - free(syms[cnt]); + free(syms[i]); free(syms); } return err; -- 2.30.2