mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Xu Kuohai <xukuohai@huawei.com>
To: Andrii Nakryiko <andrii.nakryiko@gmail.com>,
	Xu Kuohai <xukuohai@huaweicloud.com>
Cc: <bpf@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
	<linux-kselftest@vger.kernel.org>, <netdev@vger.kernel.org>,
	Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>,
	Martin KaFai Lau <martin.lau@linux.dev>,
	Song Liu <song@kernel.org>, Yonghong Song <yhs@fb.com>,
	John Fastabend <john.fastabend@gmail.com>,
	KP Singh <kpsingh@kernel.org>,
	Stanislav Fomichev <sdf@google.com>, Hao Luo <haoluo@google.com>,
	Jiri Olsa <jolsa@kernel.org>, Mykola Lysenko <mykolal@fb.com>,
	Shuah Khan <shuah@kernel.org>,
	"David S . Miller" <davem@davemloft.net>,
	Jakub Kicinski <kuba@kernel.org>,
	Jesper Dangaard Brouer <hawk@kernel.org>,
	Kumar Kartikeya Dwivedi <memxor@gmail.com>,
	Alan Maguire <alan.maguire@oracle.com>,
	Delyan Kratunov <delyank@fb.com>,
	Lorenzo Bianconi <lorenzo@kernel.org>
Subject: Re: [PATCH bpf-next v4 2/6] libbpf: Fix memory leak in parse_usdt_arg()
Date: Fri, 14 Oct 2022 09:53:23 +0800	[thread overview]
Message-ID: <86c88c01-22eb-b7f8-9c65-0faf97b4096b@huawei.com> (raw)
In-Reply-To: <CAEf4BzZVYO42kDcmNqorLfwJcMcN7fyTLdp2GWbGfV5akP12GQ@mail.gmail.com>

On 10/13/2022 11:47 PM, Andrii Nakryiko wrote:
> On Tue, Oct 11, 2022 at 4:43 AM Xu Kuohai <xukuohai@huaweicloud.com> wrote:
>>
>> From: Xu Kuohai <xukuohai@huawei.com>
>>
>> In the arm64 version of parse_usdt_arg(), when sscanf returns 2, reg_name
>> is allocated but not freed. Fix it.
>>
>> Fixes: 0f8619929c57 ("libbpf: Usdt aarch64 arg parsing support")
>> Signed-off-by: Xu Kuohai <xukuohai@huawei.com>
>> ---
>>   tools/lib/bpf/usdt.c | 11 ++++-------
>>   1 file changed, 4 insertions(+), 7 deletions(-)
>>
>> diff --git a/tools/lib/bpf/usdt.c b/tools/lib/bpf/usdt.c
>> index e83b497c2245..49f3c3b7f609 100644
>> --- a/tools/lib/bpf/usdt.c
>> +++ b/tools/lib/bpf/usdt.c
>> @@ -1348,25 +1348,23 @@ static int calc_pt_regs_off(const char *reg_name)
>>
>>   static int parse_usdt_arg(const char *arg_str, int arg_num, struct usdt_arg_spec *arg)
>>   {
>> -       char *reg_name = NULL;
>> +       char reg_name[16];
>>          int arg_sz, len, reg_off;
>>          long off;
>>
>> -       if (sscanf(arg_str, " %d @ \[ %m[a-z0-9], %ld ] %n", &arg_sz, &reg_name, &off, &len) == 3) {
>> +       if (sscanf(arg_str, " %d @ \[ %15[a-z0-9], %ld ] %n", &arg_sz, reg_name, &off, &len) == 3) {
> 
> It would be nice to do the same change for other architectures where
> it makes sense and avoid having to deal with unnecessary memory
> allocations. Please send follow up patches with similar changes for
> other implementations of parse_usdt_arg. Thanks.
>

ok, will do

> 
>>                  /* Memory dereference case, e.g., -4@[sp, 96] */
>>                  arg->arg_type = USDT_ARG_REG_DEREF;
>>                  arg->val_off = off;
>>                  reg_off = calc_pt_regs_off(reg_name);
>> -               free(reg_name);
>>                  if (reg_off < 0)
>>                          return reg_off;
>>                  arg->reg_off = reg_off;
>> -       } else if (sscanf(arg_str, " %d @ \[ %m[a-z0-9] ] %n", &arg_sz, &reg_name, &len) == 2) {
>> +       } else if (sscanf(arg_str, " %d @ \[ %15[a-z0-9] ] %n", &arg_sz, reg_name, &len) == 2) {
>>                  /* Memory dereference case, e.g., -4@[sp] */
>>                  arg->arg_type = USDT_ARG_REG_DEREF;
>>                  arg->val_off = 0;
>>                  reg_off = calc_pt_regs_off(reg_name);
>> -               free(reg_name);
>>                  if (reg_off < 0)
>>                          return reg_off;
>>                  arg->reg_off = reg_off;
>> @@ -1375,12 +1373,11 @@ static int parse_usdt_arg(const char *arg_str, int arg_num, struct usdt_arg_spec
>>                  arg->arg_type = USDT_ARG_CONST;
>>                  arg->val_off = off;
>>                  arg->reg_off = 0;
>> -       } else if (sscanf(arg_str, " %d @ %m[a-z0-9] %n", &arg_sz, &reg_name, &len) == 2) {
>> +       } else if (sscanf(arg_str, " %d @ %15[a-z0-9] %n", &arg_sz, reg_name, &len) == 2) {
>>                  /* Register read case, e.g., -8@x4 */
>>                  arg->arg_type = USDT_ARG_REG;
>>                  arg->val_off = 0;
>>                  reg_off = calc_pt_regs_off(reg_name);
>> -               free(reg_name);
>>                  if (reg_off < 0)
>>                          return reg_off;
>>                  arg->reg_off = reg_off;
>> --
>> 2.30.2
>>
> .


  reply	other threads:[~2022-10-14  1:53 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-11 12:01 [PATCH bpf-next v4 0/6] Fix bugs found by ASAN when running selftests Xu Kuohai
2022-10-11 12:01 ` [PATCH bpf-next v4 1/6] libbpf: Fix use-after-free in btf_dump_name_dups Xu Kuohai
2022-10-11 12:01 ` [PATCH bpf-next v4 2/6] libbpf: Fix memory leak in parse_usdt_arg() Xu Kuohai
2022-10-13 15:47   ` Andrii Nakryiko
2022-10-14  1:53     ` Xu Kuohai [this message]
2022-10-11 12:01 ` [PATCH bpf-next v4 3/6] selftests/bpf: Fix memory leak caused by not destroying skeleton Xu Kuohai
2022-10-11 12:01 ` [PATCH bpf-next v4 4/6] selftest/bpf: Fix memory leak in kprobe_multi_test Xu Kuohai
2022-10-11 12:01 ` [PATCH bpf-next v4 5/6] selftests/bpf: Fix error failure of case test_xdp_adjust_tail_grow Xu Kuohai
2022-10-12 23:17   ` Martin KaFai Lau
2022-10-13  1:45     ` Xu Kuohai
2022-10-11 12:01 ` [PATCH bpf-next v4 6/6] selftest/bpf: Fix error usage of ASSERT_OK in xdp_adjust_tail.c Xu Kuohai
2022-10-12 23:26   ` Martin KaFai Lau
2022-10-13  1:36     ` Xu Kuohai
2022-10-12 23:41 ` [PATCH bpf-next v4 0/6] Fix bugs found by ASAN when running selftests Martin KaFai Lau
2022-10-13 15:50 ` patchwork-bot+netdevbpf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=86c88c01-22eb-b7f8-9c65-0faf97b4096b@huawei.com \
    --to=xukuohai@huawei.com \
    --cc=alan.maguire@oracle.com \
    --cc=andrii.nakryiko@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=delyank@fb.com \
    --cc=haoluo@google.com \
    --cc=hawk@kernel.org \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kpsingh@kernel.org \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=lorenzo@kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=memxor@gmail.com \
    --cc=mykolal@fb.com \
    --cc=netdev@vger.kernel.org \
    --cc=sdf@google.com \
    --cc=shuah@kernel.org \
    --cc=song@kernel.org \
    --cc=xukuohai@huaweicloud.com \
    --cc=yhs@fb.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®