diff mbox series

[bpf,v3,2/6] libbpf: Fix memory leak in parse_usdt_arg()

Message ID 20221010142553.776550-3-xukuohai@huawei.com (mailing list archive)
State Changes Requested
Delegated to: BPF
Headers show
Series Fix bugs found by ASAN when running selftests | expand

Checks

Context Check Description
bpf/vmtest-bpf-PR fail PR summary
bpf/vmtest-bpf-VM_Test-4 success Logs for llvm-toolchain
bpf/vmtest-bpf-VM_Test-5 success Logs for set-matrix
bpf/vmtest-bpf-VM_Test-2 success Logs for build for x86_64 with gcc
bpf/vmtest-bpf-VM_Test-3 success Logs for build for x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-1 success Logs for build for s390x with gcc
bpf/vmtest-bpf-VM_Test-15 success Logs for test_verifier on s390x with gcc
bpf/vmtest-bpf-VM_Test-16 success Logs for test_verifier on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-17 success Logs for test_verifier on x86_64 with llvm-16
netdev/tree_selection success Clearly marked for bpf
netdev/fixes_present success Fixes tag present in non-next series
netdev/subject_prefix success Link
netdev/cover_letter success Series has a cover letter
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers success CCed 13 of 13 maintainers
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success Fixes tag looks correct
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/checkpatch warning WARNING: line length of 87 exceeds 80 columns WARNING: line length of 92 exceeds 80 columns WARNING: line length of 95 exceeds 80 columns WARNING: line length of 97 exceeds 80 columns WARNING: line length of 99 exceeds 80 columns
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-VM_Test-12 fail Logs for test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-VM_Test-13 success Logs for test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-14 success Logs for test_progs_no_alu32 on x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-6 success Logs for test_maps on s390x with gcc
bpf/vmtest-bpf-VM_Test-7 success Logs for test_maps on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-8 success Logs for test_maps on x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-10 success Logs for test_progs on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-11 success Logs for test_progs on x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-9 fail Logs for test_progs on s390x with gcc

Commit Message

Xu Kuohai Oct. 10, 2022, 2:25 p.m. UTC
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 | 59 +++++++++++++++++++++++++-------------------
 1 file changed, 33 insertions(+), 26 deletions(-)

Comments

Andrii Nakryiko Oct. 11, 2022, 1:34 a.m. UTC | #1
On Mon, Oct 10, 2022 at 7:08 AM Xu Kuohai <xukuohai@huawei.com> wrote:
>
> 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 | 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, &reg_name, &off, &len) == 3) {
> +       ret = sscanf(arg_str, " %d @ \[ %m[a-z0-9], %ld ] %n", &arg_sz, &reg_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, &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;
> -       } 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, &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;
>         } 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, &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;
> +               } 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, &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;
> +               } else {
> +                       pr_warn("usdt: unrecognized arg #%d spec '%s'\n", arg_num, arg_str);
> +                       return -EINVAL;
> +               }
>         }
>

I think all this is more complicated than it has to be. How big  can
register names be? Few characters? Let's get rid of %m[a-z0-9] and
instead use fixed-max-length strings, e.g., %5s. And read register
names into such local char buffers. It will simplify everything
tremendously. Let's use 16-byte buffers and use %15s to match it?
Would that be enough?

>         arg->arg_signed = arg_sz < 0;
> --
> 2.30.2
>
Xu Kuohai Oct. 11, 2022, 6:26 a.m. UTC | #2
On 10/11/2022 9:34 AM, Andrii Nakryiko wrote:
> On Mon, Oct 10, 2022 at 7:08 AM Xu Kuohai <xukuohai@huawei.com> wrote:
>>
>> 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 | 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, &reg_name, &off, &len) == 3) {
>> +       ret = sscanf(arg_str, " %d @ \[ %m[a-z0-9], %ld ] %n", &arg_sz, &reg_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, &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;
>> -       } 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, &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;
>>          } 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, &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;
>> +               } 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, &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;
>> +               } else {
>> +                       pr_warn("usdt: unrecognized arg #%d spec '%s'\n", arg_num, arg_str);
>> +                       return -EINVAL;
>> +               }
>>          }
>>
> 
> I think all this is more complicated than it has to be. How big  can
> register names be? Few characters? Let's get rid of %m[a-z0-9] and
> instead use fixed-max-length strings, e.g., %5s. And read register
> names into such local char buffers. It will simplify everything
> tremendously. Let's use 16-byte buffers and use %15s to match it?
> Would that be enough?
> 

The valid register names accepted by calc_pt_regs_off() are x0~x31 and sp, so
16-byte buffer is enough. Since %15s matches all non-space characters, will use
%15[a-z0-9] to match it.

>>          arg->arg_signed = arg_sz < 0;
>> --
>> 2.30.2
>>
> .
diff mbox series

Patch

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, &reg_name, &off, &len) == 3) {
+	ret = sscanf(arg_str, " %d @ \[ %m[a-z0-9], %ld ] %n", &arg_sz, &reg_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, &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;
-	} 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, &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;
 	} 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, &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;
+		} 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, &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;
+		} else {
+			pr_warn("usdt: unrecognized arg #%d spec '%s'\n", arg_num, arg_str);
+			return -EINVAL;
+		}
 	}
 
 	arg->arg_signed = arg_sz < 0;