public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] [gdb/tdep] Fix use-after-free in arm_exidx_fill_cache
@ 2024-02-05  5:54 Tom de Vries
  2024-02-05  8:34 ` Luis Machado
  0 siblings, 1 reply; 5+ messages in thread
From: Tom de Vries @ 2024-02-05  5:54 UTC (permalink / raw)
  To: gdb-patches

On arm-linux the linaro CI occasionally reports:
...
 (gdb) up 10
 #4  0x0001b864 in pthread_join ()
 (gdb) FAIL: gdb.threads/staticthreads.exp: up 10
...
while this is expected:
...
 (gdb) up 10
 #3  0x00010568 in main (argc=1, argv=0xfffeede4) at staticthreads.c:76
 76          pthread_join (thread, NULL);
 (gdb) PASS: gdb.threads/staticthreads.exp: up 10
...

Thiago investigated the problem, and using valgrind found an invalid read in
arm_exidx_fill_cache.

The problem happens as follows:
- an objfile and corresponding per_bfd are allocated
- some memory is allocated in arm_exidx_new_objfile using
  objfile->objfile_obstack, for the "exception table entry cache".
- a symbol reread is triggered, and the objfile, including the
  objfile_obstack, is destroyed
- a new objfile is allocated, using the same per_bfd
- again arm_exidx_new_objfile is called, but since the same per_bfd is used,
  it doesn't allocate any new memory for the "exception table entry cache".
- the "exception table entry cache" is accessed by arm_exidx_fill_cache,
  and we have a use-after-free.

This is a regression since commit a2726d4ff80 ("[ARM] Store exception handling
information per-bfd instead of per-objfile"), which changed the "exception
table entry cache" from per-objfile to per-bfd, but failed to update the
obstack_alloc.

Fix this by using objfile->per_bfd->storage_obstack instead of
objfile->objfile_obstack.

I couldn't reproduce the FAIL myself, but Thiago confirmed that the patch
fixes it.

Tested on arm-linux.

PR tdep/31254
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31254
---
 gdb/arm-tdep.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/gdb/arm-tdep.c b/gdb/arm-tdep.c
index 0d0431e0d1c..861d50a6a3b 100644
--- a/gdb/arm-tdep.c
+++ b/gdb/arm-tdep.c
@@ -2701,7 +2701,7 @@ arm_exidx_new_objfile (struct objfile *objfile)
       if (n_bytes || n_words)
 	{
 	  gdb_byte *p = entry
-	    = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack,
+	    = (gdb_byte *) obstack_alloc (&objfile->per_bfd->storage_obstack,
 					  n_bytes + n_words * 4 + 1);
 
 	  while (n_bytes--)

base-commit: 029e52bac7f3a6dd8b39f7f3d298b73174da806b
-- 
2.35.3


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

* Re: [PATCH] [gdb/tdep] Fix use-after-free in arm_exidx_fill_cache
  2024-02-05  5:54 [PATCH] [gdb/tdep] Fix use-after-free in arm_exidx_fill_cache Tom de Vries
@ 2024-02-05  8:34 ` Luis Machado
  2024-02-05 18:49   ` Thiago Jung Bauermann
  0 siblings, 1 reply; 5+ messages in thread
From: Luis Machado @ 2024-02-05  8:34 UTC (permalink / raw)
  To: Tom de Vries, gdb-patches

On 2/5/24 05:54, Tom de Vries wrote:
> On arm-linux the linaro CI occasionally reports:
> ...
>  (gdb) up 10
>  #4  0x0001b864 in pthread_join ()
>  (gdb) FAIL: gdb.threads/staticthreads.exp: up 10
> ...
> while this is expected:
> ...
>  (gdb) up 10
>  #3  0x00010568 in main (argc=1, argv=0xfffeede4) at staticthreads.c:76
>  76          pthread_join (thread, NULL);
>  (gdb) PASS: gdb.threads/staticthreads.exp: up 10
> ...
> 
> Thiago investigated the problem, and using valgrind found an invalid read in
> arm_exidx_fill_cache.
> 
> The problem happens as follows:
> - an objfile and corresponding per_bfd are allocated
> - some memory is allocated in arm_exidx_new_objfile using
>   objfile->objfile_obstack, for the "exception table entry cache".
> - a symbol reread is triggered, and the objfile, including the
>   objfile_obstack, is destroyed
> - a new objfile is allocated, using the same per_bfd
> - again arm_exidx_new_objfile is called, but since the same per_bfd is used,
>   it doesn't allocate any new memory for the "exception table entry cache".
> - the "exception table entry cache" is accessed by arm_exidx_fill_cache,
>   and we have a use-after-free.
> 
> This is a regression since commit a2726d4ff80 ("[ARM] Store exception handling
> information per-bfd instead of per-objfile"), which changed the "exception
> table entry cache" from per-objfile to per-bfd, but failed to update the
> obstack_alloc.
> 
> Fix this by using objfile->per_bfd->storage_obstack instead of
> objfile->objfile_obstack.
> 
> I couldn't reproduce the FAIL myself, but Thiago confirmed that the patch
> fixes it.
> 
> Tested on arm-linux.
> 
> PR tdep/31254
> Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31254
> ---
>  gdb/arm-tdep.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/gdb/arm-tdep.c b/gdb/arm-tdep.c
> index 0d0431e0d1c..861d50a6a3b 100644
> --- a/gdb/arm-tdep.c
> +++ b/gdb/arm-tdep.c
> @@ -2701,7 +2701,7 @@ arm_exidx_new_objfile (struct objfile *objfile)
>        if (n_bytes || n_words)
>  	{
>  	  gdb_byte *p = entry
> -	    = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack,
> +	    = (gdb_byte *) obstack_alloc (&objfile->per_bfd->storage_obstack,
>  					  n_bytes + n_words * 4 + 1);
>  
>  	  while (n_bytes--)
> 
> base-commit: 029e52bac7f3a6dd8b39f7f3d298b73174da806b

Looks like I missed a spot in the per-bfd conversion there.

Thanks Thiago and Tom for the investigation and fix. This is OK.

Approved-By: Luis Machado <luis.machado@arm.com>

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

* Re: [PATCH] [gdb/tdep] Fix use-after-free in arm_exidx_fill_cache
  2024-02-05  8:34 ` Luis Machado
@ 2024-02-05 18:49   ` Thiago Jung Bauermann
  2024-02-05 20:24     ` Tom Tromey
  0 siblings, 1 reply; 5+ messages in thread
From: Thiago Jung Bauermann @ 2024-02-05 18:49 UTC (permalink / raw)
  To: Luis Machado; +Cc: Tom de Vries, gdb-patches


Luis Machado <luis.machado@arm.com> writes:

> On 2/5/24 05:54, Tom de Vries wrote:
>> On arm-linux the linaro CI occasionally reports:
>> ...
>>  (gdb) up 10
>>  #4  0x0001b864 in pthread_join ()
>>  (gdb) FAIL: gdb.threads/staticthreads.exp: up 10
>> ...
>> while this is expected:
>> ...
>>  (gdb) up 10
>>  #3  0x00010568 in main (argc=1, argv=0xfffeede4) at staticthreads.c:76
>>  76          pthread_join (thread, NULL);
>>  (gdb) PASS: gdb.threads/staticthreads.exp: up 10
>> ...
>> 
>> Thiago investigated the problem, and using valgrind found an invalid read in
>> arm_exidx_fill_cache.
>> 
>> The problem happens as follows:
>> - an objfile and corresponding per_bfd are allocated
>> - some memory is allocated in arm_exidx_new_objfile using
>>   objfile->objfile_obstack, for the "exception table entry cache".
>> - a symbol reread is triggered, and the objfile, including the
>>   objfile_obstack, is destroyed
>> - a new objfile is allocated, using the same per_bfd
>> - again arm_exidx_new_objfile is called, but since the same per_bfd is used,
>>   it doesn't allocate any new memory for the "exception table entry cache".
>> - the "exception table entry cache" is accessed by arm_exidx_fill_cache,
>>   and we have a use-after-free.
>> 
>> This is a regression since commit a2726d4ff80 ("[ARM] Store exception handling
>> information per-bfd instead of per-objfile"), which changed the "exception
>> table entry cache" from per-objfile to per-bfd, but failed to update the
>> obstack_alloc.
>> 
>> Fix this by using objfile->per_bfd->storage_obstack instead of
>> objfile->objfile_obstack.
>> 
>> I couldn't reproduce the FAIL myself, but Thiago confirmed that the patch
>> fixes it.
>> 
>> Tested on arm-linux.
>> 
>> PR tdep/31254
>> Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31254
>> ---
>>  gdb/arm-tdep.c | 2 +-
>>  1 file changed, 1 insertion(+), 1 deletion(-)
>> 
>> diff --git a/gdb/arm-tdep.c b/gdb/arm-tdep.c
>> index 0d0431e0d1c..861d50a6a3b 100644
>> --- a/gdb/arm-tdep.c
>> +++ b/gdb/arm-tdep.c
>> @@ -2701,7 +2701,7 @@ arm_exidx_new_objfile (struct objfile *objfile)
>>        if (n_bytes || n_words)
>>  	{
>>  	  gdb_byte *p = entry
>> -	    = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack,
>> +	    = (gdb_byte *) obstack_alloc (&objfile->per_bfd->storage_obstack,
>>  					  n_bytes + n_words * 4 + 1);
>>  
>>  	  while (n_bytes--)
>> 
>> base-commit: 029e52bac7f3a6dd8b39f7f3d298b73174da806b
>
> Looks like I missed a spot in the per-bfd conversion there.
>
> Thanks Thiago and Tom for the investigation and fix. This is OK.
>
> Approved-By: Luis Machado <luis.machado@arm.com>

Could this patch also be committed to the GDB 14 branch?

-- 
Thiago

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

* Re: [PATCH] [gdb/tdep] Fix use-after-free in arm_exidx_fill_cache
  2024-02-05 18:49   ` Thiago Jung Bauermann
@ 2024-02-05 20:24     ` Tom Tromey
  2024-02-06 21:35       ` Thiago Jung Bauermann
  0 siblings, 1 reply; 5+ messages in thread
From: Tom Tromey @ 2024-02-05 20:24 UTC (permalink / raw)
  To: Thiago Jung Bauermann; +Cc: Luis Machado, Tom de Vries, gdb-patches

>>>>> "Thiago" == Thiago Jung Bauermann <thiago.bauermann@linaro.org> writes:

>> Looks like I missed a spot in the per-bfd conversion there.
>> 
>> Thanks Thiago and Tom for the investigation and fix. This is OK.
>> 
>> Approved-By: Luis Machado <luis.machado@arm.com>

Thiago> Could this patch also be committed to the GDB 14 branch?

I think it's fine.

Tom

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

* Re: [PATCH] [gdb/tdep] Fix use-after-free in arm_exidx_fill_cache
  2024-02-05 20:24     ` Tom Tromey
@ 2024-02-06 21:35       ` Thiago Jung Bauermann
  0 siblings, 0 replies; 5+ messages in thread
From: Thiago Jung Bauermann @ 2024-02-06 21:35 UTC (permalink / raw)
  To: Tom Tromey; +Cc: Luis Machado, Tom de Vries, gdb-patches


Tom Tromey <tom@tromey.com> writes:

>>>>>> "Thiago" == Thiago Jung Bauermann <thiago.bauermann@linaro.org> writes:
>
>>> Looks like I missed a spot in the per-bfd conversion there.
>>> 
>>> Thanks Thiago and Tom for the investigation and fix. This is OK.
>>> 
>>> Approved-By: Luis Machado <luis.machado@arm.com>
>
> Thiago> Could this patch also be committed to the GDB 14 branch?
>
> I think it's fine.

Thanks! I just pushed it to the branch as commit 7c709b26ba13.

-- 
Thiago

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

end of thread, other threads:[~2024-02-06 21:35 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-05  5:54 [PATCH] [gdb/tdep] Fix use-after-free in arm_exidx_fill_cache Tom de Vries
2024-02-05  8:34 ` Luis Machado
2024-02-05 18:49   ` Thiago Jung Bauermann
2024-02-05 20:24     ` Tom Tromey
2024-02-06 21:35       ` Thiago Jung Bauermann

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).