BPF List
 help / color / mirror / Atom feed
* [PATCH dwarves v2] btf_encoder: fix memory access bugs
@ 2024-12-16 18:31 Ihor Solodrai
  2025-01-28  0:03 ` ihor.solodrai
  2025-03-12 16:52 ` Alan Maguire
  0 siblings, 2 replies; 5+ messages in thread
From: Ihor Solodrai @ 2024-12-16 18:31 UTC (permalink / raw)
  To: dwarves; +Cc: acme, alan.maguire, eddyz87, andrii, mykolal, bpf

When compiled with address sanitizer, a couple of errors were reported
on pahole BTF encoding:
  * A memory leak of strdup(func->alias), due to unchecked
    reassignment.
  * A read of uninitialized memory in gobuffer__sort or bsearch in
    case btf_funcs gobuffer is empty.

Used compiler flags:
    -fsanitize=undefined,address
    -fsanitize-recover=address
    -fno-omit-frame-pointer

v1: https://lore.kernel.org/dwarves/20241213233205.633927-1-ihor.solodrai@pm.me/

Reviewed-by: Alan Maguire <alan.maguire@oracle.com>
Signed-off-by: Ihor Solodrai <ihor.solodrai@pm.me>
---
 btf_encoder.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/btf_encoder.c b/btf_encoder.c
index 3754884..fbc9509 100644
--- a/btf_encoder.c
+++ b/btf_encoder.c
@@ -1794,7 +1794,8 @@ static int btf_encoder__collect_btf_funcs(struct btf_encoder *encoder, struct go
 	}
 
 	/* Now that we've collected funcs, sort them by name */
-	gobuffer__sort(funcs, sizeof(struct btf_func), btf_func_cmp);
+	if (gobuffer__nr_entries(funcs) > 0)
+		gobuffer__sort(funcs, sizeof(struct btf_func), btf_func_cmp);
 
 	err = 0;
 out:
@@ -1954,6 +1955,11 @@ static int btf_encoder__tag_kfuncs(struct btf_encoder *encoder)
 		goto out;
 	}
 
+	if (gobuffer__nr_entries(&btf_funcs) == 0) {
+		err = 0;
+		goto out;
+	}
+
 	/* First collect all kfunc set ranges.
 	 *
 	 * Note we choose not to sort these ranges and accept a linear
@@ -2607,7 +2613,8 @@ int btf_encoder__encode_cu(struct btf_encoder *encoder, struct cu *cu, struct co
 						       ", has optimized-out parameters" :
 						       fn->proto.unexpected_reg ? ", has unexpected register use by params" :
 						       "");
-					func->alias = strdup(name);
+					if (!func->alias)
+						func->alias = strdup(name);
 				}
 			}
 		} else {
-- 
2.47.1



^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH dwarves v2] btf_encoder: fix memory access bugs
  2024-12-16 18:31 [PATCH dwarves v2] btf_encoder: fix memory access bugs Ihor Solodrai
@ 2025-01-28  0:03 ` ihor.solodrai
  2025-01-31 13:14   ` Alan Maguire
  2025-03-12 16:52 ` Alan Maguire
  1 sibling, 1 reply; 5+ messages in thread
From: ihor.solodrai @ 2025-01-28  0:03 UTC (permalink / raw)
  To: alan.maguire, acme; +Cc: eddyz87, andrii, mykolal, bpf, dwarves

December 16, 2024 at 10:31 AM, "Ihor Solodrai" <ihor.solodrai@pm.me> wrote:

> 
> When compiled with address sanitizer, a couple of errors were reported
> 
> on pahole BTF encoding:
> 
>  * A memory leak of strdup(func->alias), due to unchecked
> 
>  reassignment.
> 
>  * A read of uninitialized memory in gobuffer__sort or bsearch in
> 
>  case btf_funcs gobuffer is empty.
> 
> Used compiler flags:
> 
>  -fsanitize=undefined,address
> 
>  -fsanitize-recover=address
> 
>  -fno-omit-frame-pointer
> 
> v1: https://lore.kernel.org/dwarves/20241213233205.633927-1-ihor.solodrai@pm.me/
> 
> Reviewed-by: Alan Maguire <alan.maguire@oracle.com>
> 
> Signed-off-by: Ihor Solodrai <ihor.solodrai@pm.me>
> 
> ---
> 
>  btf_encoder.c | 11 +++++++++--
> 
>  1 file changed, 9 insertions(+), 2 deletions(-)
> 

Alan, Arnaldo,

This patch hasn't been applied.
Just a reminder in case it fell off the radar.

Thanks.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH dwarves v2] btf_encoder: fix memory access bugs
  2025-01-28  0:03 ` ihor.solodrai
@ 2025-01-31 13:14   ` Alan Maguire
  2025-01-31 22:41     ` Ihor Solodrai
  0 siblings, 1 reply; 5+ messages in thread
From: Alan Maguire @ 2025-01-31 13:14 UTC (permalink / raw)
  To: ihor.solodrai, acme; +Cc: eddyz87, andrii, mykolal, bpf, dwarves

On 28/01/2025 00:03, ihor.solodrai@linux.dev wrote:
> December 16, 2024 at 10:31 AM, "Ihor Solodrai" <ihor.solodrai@pm.me> wrote:
> 
>>
>> When compiled with address sanitizer, a couple of errors were reported
>>
>> on pahole BTF encoding:
>>
>>  * A memory leak of strdup(func->alias), due to unchecked
>>
>>  reassignment.
>>
>>  * A read of uninitialized memory in gobuffer__sort or bsearch in
>>
>>  case btf_funcs gobuffer is empty.
>>
>> Used compiler flags:
>>
>>  -fsanitize=undefined,address
>>
>>  -fsanitize-recover=address
>>
>>  -fno-omit-frame-pointer
>>
>> v1: https://lore.kernel.org/dwarves/20241213233205.633927-1-ihor.solodrai@pm.me/
>>
>> Reviewed-by: Alan Maguire <alan.maguire@oracle.com>
>>
>> Signed-off-by: Ihor Solodrai <ihor.solodrai@pm.me>
>>
>> ---
>>
>>  btf_encoder.c | 11 +++++++++--
>>
>>  1 file changed, 9 insertions(+), 2 deletions(-)
>>
> 
> Alan, Arnaldo,
> 
> This patch hasn't been applied.
> Just a reminder in case it fell off the radar.
>

Thanks for the reminder; we'll make sure this one gets applied shortly.
I wonder if we should add the -fsanitize flags to CFLAGS for RELEASE mode?


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH dwarves v2] btf_encoder: fix memory access bugs
  2025-01-31 13:14   ` Alan Maguire
@ 2025-01-31 22:41     ` Ihor Solodrai
  0 siblings, 0 replies; 5+ messages in thread
From: Ihor Solodrai @ 2025-01-31 22:41 UTC (permalink / raw)
  To: Alan Maguire, acme; +Cc: eddyz87, andrii, mykolal, bpf, dwarves

On 1/31/25 5:14 AM, Alan Maguire wrote:
 >> [...]
 >
 > Thanks for the reminder; we'll make sure this one gets applied shortly.
 > I wonder if we should add the -fsanitize flags to CFLAGS for RELEASE 
mode?
 >

The issue with these flags is that they significantly reduce
performance. We might consider adding them for debug builds, but
certainly not for the release.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH dwarves v2] btf_encoder: fix memory access bugs
  2024-12-16 18:31 [PATCH dwarves v2] btf_encoder: fix memory access bugs Ihor Solodrai
  2025-01-28  0:03 ` ihor.solodrai
@ 2025-03-12 16:52 ` Alan Maguire
  1 sibling, 0 replies; 5+ messages in thread
From: Alan Maguire @ 2025-03-12 16:52 UTC (permalink / raw)
  To: Ihor Solodrai, dwarves; +Cc: acme, eddyz87, andrii, mykolal, bpf

On 16/12/2024 18:31, Ihor Solodrai wrote:
> When compiled with address sanitizer, a couple of errors were reported
> on pahole BTF encoding:
>   * A memory leak of strdup(func->alias), due to unchecked
>     reassignment.
>   * A read of uninitialized memory in gobuffer__sort or bsearch in
>     case btf_funcs gobuffer is empty.
> 
> Used compiler flags:
>     -fsanitize=undefined,address
>     -fsanitize-recover=address
>     -fno-omit-frame-pointer
> 
> v1: https://lore.kernel.org/dwarves/20241213233205.633927-1-ihor.solodrai@pm.me/
> 
> Reviewed-by: Alan Maguire <alan.maguire@oracle.com>
> Signed-off-by: Ihor Solodrai <ihor.solodrai@pm.me>

applied to the next branch of

https://git.kernel.org/pub/scm/devel/pahole/pahole.git/

thanks!

> ---
>  btf_encoder.c | 11 +++++++++--
>  1 file changed, 9 insertions(+), 2 deletions(-)
> 
> diff --git a/btf_encoder.c b/btf_encoder.c
> index 3754884..fbc9509 100644
> --- a/btf_encoder.c
> +++ b/btf_encoder.c
> @@ -1794,7 +1794,8 @@ static int btf_encoder__collect_btf_funcs(struct btf_encoder *encoder, struct go
>  	}
>  
>  	/* Now that we've collected funcs, sort them by name */
> -	gobuffer__sort(funcs, sizeof(struct btf_func), btf_func_cmp);
> +	if (gobuffer__nr_entries(funcs) > 0)
> +		gobuffer__sort(funcs, sizeof(struct btf_func), btf_func_cmp);
>  
>  	err = 0;
>  out:
> @@ -1954,6 +1955,11 @@ static int btf_encoder__tag_kfuncs(struct btf_encoder *encoder)
>  		goto out;
>  	}
>  
> +	if (gobuffer__nr_entries(&btf_funcs) == 0) {
> +		err = 0;
> +		goto out;
> +	}
> +
>  	/* First collect all kfunc set ranges.
>  	 *
>  	 * Note we choose not to sort these ranges and accept a linear
> @@ -2607,7 +2613,8 @@ int btf_encoder__encode_cu(struct btf_encoder *encoder, struct cu *cu, struct co
>  						       ", has optimized-out parameters" :
>  						       fn->proto.unexpected_reg ? ", has unexpected register use by params" :
>  						       "");
> -					func->alias = strdup(name);
> +					if (!func->alias)
> +						func->alias = strdup(name);
>  				}
>  			}
>  		} else {


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2025-03-12 16:53 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-12-16 18:31 [PATCH dwarves v2] btf_encoder: fix memory access bugs Ihor Solodrai
2025-01-28  0:03 ` ihor.solodrai
2025-01-31 13:14   ` Alan Maguire
2025-01-31 22:41     ` Ihor Solodrai
2025-03-12 16:52 ` Alan Maguire

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox