diff mbox series

[bpf-next] libbpf: fix memory leak when emitting final btf_ext

Message ID 20210327042502.969745-1-andrii@kernel.org (mailing list archive)
State Accepted
Delegated to: BPF
Headers show
Series [bpf-next] libbpf: fix memory leak when emitting final btf_ext | expand

Checks

Context Check Description
netdev/cover_letter success Link
netdev/fixes_present success Link
netdev/patch_count success Link
netdev/tree_selection success Clearly marked for bpf-next
netdev/subject_prefix success Link
netdev/cc_maintainers fail 1 blamed authors not CCed: ast@kernel.org; 6 maintainers not CCed: yhs@fb.com kpsingh@kernel.org kafai@fb.com ast@kernel.org john.fastabend@gmail.com songliubraving@fb.com
netdev/source_inline success Was 0 now: 0
netdev/verify_signedoff success Link
netdev/module_param success Was 0 now: 0
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/verify_fixes success Link
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 48 lines checked
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/header_inline success Link

Commit Message

Andrii Nakryiko March 27, 2021, 4:25 a.m. UTC
Free temporary allocated memory used to construct finalized .BTF.ext data.
Found by Coverity static analysis on libbpf's Github repo.

Fixes: 8fd27bf69b86 ("libbpf: Add BPF static linker BTF and BTF.ext support")
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
---
 tools/lib/bpf/linker.c | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)

Comments

Song Liu March 29, 2021, 3:54 p.m. UTC | #1
On Fri, Mar 26, 2021 at 9:35 PM Andrii Nakryiko <andrii@kernel.org> wrote:
>
> Free temporary allocated memory used to construct finalized .BTF.ext data.
> Found by Coverity static analysis on libbpf's Github repo.
>
> Fixes: 8fd27bf69b86 ("libbpf: Add BPF static linker BTF and BTF.ext support")
> Signed-off-by: Andrii Nakryiko <andrii@kernel.org>

Acked-by: Song Liu <songliubraving@fb.com>


> ---
>  tools/lib/bpf/linker.c | 24 ++++++++++++++++--------
>  1 file changed, 16 insertions(+), 8 deletions(-)
>
> diff --git a/tools/lib/bpf/linker.c b/tools/lib/bpf/linker.c
> index a29d62ff8041..46b16cbdcda3 100644
> --- a/tools/lib/bpf/linker.c
> +++ b/tools/lib/bpf/linker.c
> @@ -1906,8 +1906,10 @@ static int finalize_btf_ext(struct bpf_linker *linker)
>                         struct dst_sec *sec = &linker->secs[i];
>
>                         sz = emit_btf_ext_data(linker, cur, sec->sec_name, &sec->func_info);
> -                       if (sz < 0)
> -                               return sz;
> +                       if (sz < 0) {
> +                               err = sz;
> +                               goto out;
> +                       }
>
>                         cur += sz;
>                 }
> @@ -1921,8 +1923,10 @@ static int finalize_btf_ext(struct bpf_linker *linker)
>                         struct dst_sec *sec = &linker->secs[i];
>
>                         sz = emit_btf_ext_data(linker, cur, sec->sec_name, &sec->line_info);
> -                       if (sz < 0)
> -                               return sz;
> +                       if (sz < 0) {
> +                               err = sz;
> +                               goto out;
> +                       }
>
>                         cur += sz;
>                 }
> @@ -1936,8 +1940,10 @@ static int finalize_btf_ext(struct bpf_linker *linker)
>                         struct dst_sec *sec = &linker->secs[i];
>
>                         sz = emit_btf_ext_data(linker, cur, sec->sec_name, &sec->core_relo_info);
> -                       if (sz < 0)
> -                               return sz;
> +                       if (sz < 0) {
> +                               err = sz;
> +                               goto out;
> +                       }
>
>                         cur += sz;
>                 }
> @@ -1948,8 +1954,10 @@ static int finalize_btf_ext(struct bpf_linker *linker)
>         if (err) {
>                 linker->btf_ext = NULL;
>                 pr_warn("failed to parse final .BTF.ext data: %d\n", err);
> -               return err;
> +               goto out;
>         }
>
> -       return 0;
> +out:
> +       free(data);
> +       return err;
>  }
> --
> 2.30.2
>
Alexei Starovoitov March 30, 2021, 4:32 p.m. UTC | #2
On 3/26/21 9:25 PM, Andrii Nakryiko wrote:
> Free temporary allocated memory used to construct finalized .BTF.ext data.
> Found by Coverity static analysis on libbpf's Github repo.
> 
> Fixes: 8fd27bf69b86 ("libbpf: Add BPF static linker BTF and BTF.ext support")
> Signed-off-by: Andrii Nakryiko<andrii@kernel.org>

Applied.
diff mbox series

Patch

diff --git a/tools/lib/bpf/linker.c b/tools/lib/bpf/linker.c
index a29d62ff8041..46b16cbdcda3 100644
--- a/tools/lib/bpf/linker.c
+++ b/tools/lib/bpf/linker.c
@@ -1906,8 +1906,10 @@  static int finalize_btf_ext(struct bpf_linker *linker)
 			struct dst_sec *sec = &linker->secs[i];
 
 			sz = emit_btf_ext_data(linker, cur, sec->sec_name, &sec->func_info);
-			if (sz < 0)
-				return sz;
+			if (sz < 0) {
+				err = sz;
+				goto out;
+			}
 
 			cur += sz;
 		}
@@ -1921,8 +1923,10 @@  static int finalize_btf_ext(struct bpf_linker *linker)
 			struct dst_sec *sec = &linker->secs[i];
 
 			sz = emit_btf_ext_data(linker, cur, sec->sec_name, &sec->line_info);
-			if (sz < 0)
-				return sz;
+			if (sz < 0) {
+				err = sz;
+				goto out;
+			}
 
 			cur += sz;
 		}
@@ -1936,8 +1940,10 @@  static int finalize_btf_ext(struct bpf_linker *linker)
 			struct dst_sec *sec = &linker->secs[i];
 
 			sz = emit_btf_ext_data(linker, cur, sec->sec_name, &sec->core_relo_info);
-			if (sz < 0)
-				return sz;
+			if (sz < 0) {
+				err = sz;
+				goto out;
+			}
 
 			cur += sz;
 		}
@@ -1948,8 +1954,10 @@  static int finalize_btf_ext(struct bpf_linker *linker)
 	if (err) {
 		linker->btf_ext = NULL;
 		pr_warn("failed to parse final .BTF.ext data: %d\n", err);
-		return err;
+		goto out;
 	}
 
-	return 0;
+out:
+	free(data);
+	return err;
 }