Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp1548556rwi; Mon, 10 Oct 2022 18:45:45 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5OsjuS9iyNzZlg/FsAfNV+c1loy2yUWTup3+kzF9P9/stQ6p77hfOc635sIrHGrUDHfVZ5 X-Received: by 2002:a50:ed86:0:b0:458:e1da:af2 with SMTP id h6-20020a50ed86000000b00458e1da0af2mr20124143edr.364.1665452745164; Mon, 10 Oct 2022 18:45:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665452745; cv=none; d=google.com; s=arc-20160816; b=yo9hqFI7vWSFyqmQciKer4pohbn9FYPvdrZelry0wDgPJaHD5E4s6I5yInqXSptQeV 80SOvVEfC4sUPBCUjK5PGiNXJX9loNuu2109m3PfFuCjkeKMUB+KMYkXz0lPeztFdVzD /c71/dLU+umnKST+OKEB4+h+J2ferOpToRNsadFjiFkKqp7HeLxzU8vgigOsd7JEmEtT pSrbTViBKar6zXN9wYVZCc3BT3fe/max4QvPZsVB772C93gYkdCdk1nIGGyzTxm1SCqd s5YmAL5Gdcj//U6oI83R0WrCpkFrw0KheQC2bjvMWS5VAL95naCw0Ug3NWA1Shy1Kp4g vXIg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=I5pt7FCzRV0b8PQeZR2wM7TKOPnSu9bqmN2Ckrkc5X4=; b=gdhyd3HMmY+N8GS31og7BfCji+5Jf//Td3qkyidNQnFfGkhhWqLRy9M5TDFGqp9FzF 4SpPC+grkegml0ZquyMdxiHjAXUncxC//vegttvFTX3pHk7oGAwqEZEWRDCwqB7tbIOy W8U9A+oHkcAjuZH6GXdgjwbu6uo3zDStWZ32wwG3ZrA4nx9o+r6n0x4mO65iKowS5hWj qJJz8mCP7/A1WlxM/31OBuVAF5Y4/MY1D7aObtwb9qu+s4K1d/z7YCAMHoQ5vP5Nb4HJ VdFRVKihpmxEuxQ/IvsCsEKtywjDBwQYybUYRIGrblO59W+WbW+9SUNpss6NUSXMz7AH jzzg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=TLjqhcNr; 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=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id qb18-20020a1709077e9200b0078dcdbb3e87si2045940ejc.530.2022.10.10.18.45.20; Mon, 10 Oct 2022 18:45:45 -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; dkim=pass header.i=@gmail.com header.s=20210112 header.b=TLjqhcNr; 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=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229981AbiJKBfm (ORCPT + 99 others); Mon, 10 Oct 2022 21:35:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48708 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229943AbiJKBfY (ORCPT ); Mon, 10 Oct 2022 21:35:24 -0400 Received: from mail-ed1-x536.google.com (mail-ed1-x536.google.com [IPv6:2a00:1450:4864:20::536]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 50E404D4FA; Mon, 10 Oct 2022 18:35:12 -0700 (PDT) Received: by mail-ed1-x536.google.com with SMTP id m15so18042783edb.13; Mon, 10 Oct 2022 18:35:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=I5pt7FCzRV0b8PQeZR2wM7TKOPnSu9bqmN2Ckrkc5X4=; b=TLjqhcNrPe2HVvu/RCTsgi2UDo/CwFiUw6t71gtgrmg5VVHEsnlVrs4e13cWpMbvN+ mrMjxn9LnclWra6JWlaraRhbrs6Z+5MMIYpIZh/9Wyk6H9J9GOyxFMI6e/fh6+7SRGL+ 8BLNjT9ovgq3TqRo+/h/E6EYS0BBjb2TTJyYMThdfjjP8StsCuYXvMKtLXaK9VsJHUjv KNe7MQCZmP3uNbvceThRorYTBHvoc7JPOd83eYCACINsE0C/KisM9CjddSyzGE6Xqvxm ogwQgAalRNcSA3P7A8KQN6eTXkQUmbDF/47b5owUv5NMBzo0ERXHp44f5z6AKfoYjvyb G3zQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=I5pt7FCzRV0b8PQeZR2wM7TKOPnSu9bqmN2Ckrkc5X4=; b=q+NU5oFNgbf+VEZbNkIVDowX2eY+aJZ5tiGJAZ6YYzJAKSYLh4T/eEPh0tJPqqyYNL kyKkL1f0MeGQ3gg4hHd3NlHfgbwmmiBhIkuEa73Aa6sWtmPgBQ/XIfrJJsE1Ul+hWsLI vwuh8JPyLiHYGr0xFxJWFBXpjAnLRi9Q7f8SOLDnPCjrwxzJZ8cKbQmnY9CoThps9xL/ TRr2/XOEIRukU1ORiKbZq1IB82JEDgZ0HNIvDHAE/C7nMXwUZlRo6sdSHogb27iIUk5X a7+a/5Nb/4YBP0uoK85uZnTLTAF/TnU8Bi+PJnQ3ws1XzkU471lngMlBOUDLFF5+6brM CxNQ== X-Gm-Message-State: ACrzQf3UHVNH60FpLpbCGOZTZNq9Gdt7WGmyWcKx6NI6a9A84FTcY6wY YsOrpW2EGjJm7Wo5F5/c3t0wV/wKtbnKcYBlMrI= X-Received: by 2002:a50:eb05:0:b0:457:c6f5:5f65 with SMTP id y5-20020a50eb05000000b00457c6f55f65mr19992113edp.311.1665452110881; Mon, 10 Oct 2022 18:35:10 -0700 (PDT) MIME-Version: 1.0 References: <20221010142553.776550-1-xukuohai@huawei.com> <20221010142553.776550-5-xukuohai@huawei.com> In-Reply-To: <20221010142553.776550-5-xukuohai@huawei.com> From: Andrii Nakryiko Date: Mon, 10 Oct 2022 18:34:58 -0700 Message-ID: Subject: Re: [PATCH bpf v3 4/6] selftest/bpf: Fix memory leak in kprobe_multi_test To: Xu Kuohai Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, netdev@vger.kernel.org, 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 Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,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 On Mon, Oct 10, 2022 at 7:08 AM Xu Kuohai wrote: > > 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 | 17 ++++++++--------- > 1 file changed, 8 insertions(+), 9 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..07dd2c5b7f98 100644 > --- a/tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c > +++ b/tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c > @@ -360,15 +360,14 @@ static int get_syms(char ***symsp, size_t *cntp) > * to them. Filter out the current culprits - arch_cpu_idle > * and rcu_* functions. > */ > - if (!strcmp(name, "arch_cpu_idle")) > - continue; > - if (!strncmp(name, "rcu_", 4)) > - continue; > - if (!strcmp(name, "bpf_dispatcher_xdp_func")) > - continue; > - if (!strncmp(name, "__ftrace_invalid_address__", > - sizeof("__ftrace_invalid_address__") - 1)) > + if (!strcmp(name, "arch_cpu_idle") || > + !strncmp(name, "rcu_", 4) || > + !strcmp(name, "bpf_dispatcher_xdp_func") || > + !strncmp(name, "__ftrace_invalid_address__", > + sizeof("__ftrace_invalid_address__") - 1)) { > + free(name); > continue; > + } it seems cleaner if we add if (name) free(name) under error: goto label. And in the success case when we assign name to syms[cnt] we can reset name to NULL to avoid double-free. WDYT? > err = hashmap__add(map, name, NULL); > if (err) { > free(name); > @@ -394,7 +393,7 @@ static int get_syms(char ***symsp, size_t *cntp) > hashmap__free(map); > if (err) { > for (i = 0; i < cnt; i++) > - free(syms[cnt]); > + free(syms[i]); > free(syms); > } > return err; > -- > 2.30.2 >