[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20221009131830.395569-3-xukuohai@huaweicloud.com>
Date: Sun, 9 Oct 2022 09:18:27 -0400
From: Xu Kuohai <xukuohai@...weicloud.com>
To: bpf@...r.kernel.org, linux-kernel@...r.kernel.org,
linux-kselftest@...r.kernel.org, netdev@...r.kernel.org
Cc: Alexei Starovoitov <ast@...nel.org>,
Daniel Borkmann <daniel@...earbox.net>,
Andrii Nakryiko <andrii@...nel.org>,
Martin KaFai Lau <martin.lau@...ux.dev>,
Song Liu <song@...nel.org>, Yonghong Song <yhs@...com>,
John Fastabend <john.fastabend@...il.com>,
KP Singh <kpsingh@...nel.org>,
Stanislav Fomichev <sdf@...gle.com>,
Hao Luo <haoluo@...gle.com>, Jiri Olsa <jolsa@...nel.org>,
Mykola Lysenko <mykolal@...com>, Shuah Khan <shuah@...nel.org>,
"David S . Miller" <davem@...emloft.net>,
Jakub Kicinski <kuba@...nel.org>,
Jesper Dangaard Brouer <hawk@...nel.org>,
Hou Tao <houtao1@...wei.com>,
Dmitrii Dolgov <9erthalion6@...il.com>,
Alan Maguire <alan.maguire@...cle.com>,
Kui-Feng Lee <kuifeng@...com>,
Kumar Kartikeya Dwivedi <memxor@...il.com>
Subject: [PATCH bpf-next 2/5] libbpf: Fix memory leak in parse_usdt_arg()
From: Xu Kuohai <xukuohai@...wei.com>
In the arm64 version of parse_usdt_arg(), when sscanf returns 2, reg_name
is allocated but not freed. Fix it.
Signed-off-by: Xu Kuohai <xukuohai@...wei.com>
---
tools/lib/bpf/usdt.c | 59 +++++++++++++++++++++++++-------------------
1 file changed, 33 insertions(+), 26 deletions(-)
diff --git a/tools/lib/bpf/usdt.c b/tools/lib/bpf/usdt.c
index e83b497c2245..f3b5be7415b5 100644
--- a/tools/lib/bpf/usdt.c
+++ b/tools/lib/bpf/usdt.c
@@ -1351,8 +1351,10 @@ static int parse_usdt_arg(const char *arg_str, int arg_num, struct usdt_arg_spec
char *reg_name = NULL;
int arg_sz, len, reg_off;
long off;
+ int ret;
- if (sscanf(arg_str, " %d @ \[ %m[a-z0-9], %ld ] %n", &arg_sz, ®_name, &off, &len) == 3) {
+ ret = sscanf(arg_str, " %d @ \[ %m[a-z0-9], %ld ] %n", &arg_sz, ®_name, &off, &len);
+ if (ret == 3) {
/* Memory dereference case, e.g., -4@[sp, 96] */
arg->arg_type = USDT_ARG_REG_DEREF;
arg->val_off = off;
@@ -1361,32 +1363,37 @@ static int parse_usdt_arg(const char *arg_str, int arg_num, struct usdt_arg_spec
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, ®_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;
- } else if (sscanf(arg_str, " %d @ %ld %n", &arg_sz, &off, &len) == 2) {
- /* Constant value case, e.g., 4@5 */
- 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, ®_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;
} else {
- pr_warn("usdt: unrecognized arg #%d spec '%s'\n", arg_num, arg_str);
- return -EINVAL;
+ if (ret == 2)
+ free(reg_name);
+
+ if (sscanf(arg_str, " %d @ \[ %m[a-z0-9] ] %n", &arg_sz, ®_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;
+ } else if (sscanf(arg_str, " %d @ %ld %n", &arg_sz, &off, &len) == 2) {
+ /* Constant value case, e.g., 4@5 */
+ 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, ®_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;
+ } else {
+ pr_warn("usdt: unrecognized arg #%d spec '%s'\n", arg_num, arg_str);
+ return -EINVAL;
+ }
}
arg->arg_signed = arg_sz < 0;
--
2.25.1
Powered by blists - more mailing lists