linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] objtool: Fix infinite loop in for_offset_range()
@ 2020-04-25 19:19 Josh Poimboeuf
  2020-04-25 20:04 ` Randy Dunlap
  2020-04-26  7:33 ` [tip: x86/urgent] " tip-bot2 for Josh Poimboeuf
  0 siblings, 2 replies; 4+ messages in thread
From: Josh Poimboeuf @ 2020-04-25 19:19 UTC (permalink / raw)
  To: x86
  Cc: linux-kernel, Peter Zijlstra, Miroslav Benes, Julien Thierry,
	Randy Dunlap

Randy reported that objtool got stuck in an infinite loop when
processing drivers/i2c/busses/i2c-parport.o.  It was caused by the
following code:

  00000000000001fd <line_set>:
   1fd:	48 b8 00 00 00 00 00 	movabs $0x0,%rax
   204:	00 00 00
  			1ff: R_X86_64_64	.rodata-0x8
   207:	41 55                	push   %r13
   209:	41 89 f5             	mov    %esi,%r13d
   20c:	41 54                	push   %r12
   20e:	49 89 fc             	mov    %rdi,%r12
   211:	55                   	push   %rbp
   212:	48 89 d5             	mov    %rdx,%rbp
   215:	53                   	push   %rbx
   216:	0f b6 5a 01          	movzbl 0x1(%rdx),%ebx
   21a:	48 8d 34 dd 00 00 00 	lea    0x0(,%rbx,8),%rsi
   221:	00
  			21e: R_X86_64_32S	.rodata
   222:	48 89 f1             	mov    %rsi,%rcx
   225:	48 29 c1             	sub    %rax,%rcx

find_jump_table() saw the .rodata reference and tried to find a jump
table associated with it (though there wasn't one).  The -0x8 rela
addend is unusual.  It caused find_jump_table() to send a negative
table_offset (unsigned 0xfffffffffffffff8) to find_rela_by_dest().

The negative offset should have been harmless, but it actually threw
for_offset_range() for a loop... literally.  When the mask value got
incremented past the end value, it also wrapped to zero, causing the
loop exit condition to remain true forever.

Prevent this scenario from happening by ensuring the incremented value
is always >= the starting value.

Fixes: 74b873e49d92 ("objtool: Optimize find_rela_by_dest_range()")
Reported-by: Randy Dunlap <rdunlap@infradead.org>
Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
---
 tools/objtool/elf.h | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/tools/objtool/elf.h b/tools/objtool/elf.h
index 5e76ac38cf99..f753148f5dac 100644
--- a/tools/objtool/elf.h
+++ b/tools/objtool/elf.h
@@ -89,9 +89,10 @@ struct elf {
 #define OFFSET_STRIDE		(1UL << OFFSET_STRIDE_BITS)
 #define OFFSET_STRIDE_MASK	(~(OFFSET_STRIDE - 1))
 
-#define for_offset_range(_offset, _start, _end)		\
-	for (_offset = ((_start) & OFFSET_STRIDE_MASK);	\
-	     _offset <= ((_end) & OFFSET_STRIDE_MASK);	\
+#define for_offset_range(_offset, _start, _end)			\
+	for (_offset = ((_start) & OFFSET_STRIDE_MASK);		\
+	     _offset >= ((_start) & OFFSET_STRIDE_MASK) &&	\
+	     _offset <= ((_end) & OFFSET_STRIDE_MASK);		\
 	     _offset += OFFSET_STRIDE)
 
 static inline u32 sec_offset_hash(struct section *sec, unsigned long offset)
-- 
2.21.1


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

* Re: [PATCH] objtool: Fix infinite loop in for_offset_range()
  2020-04-25 19:19 [PATCH] objtool: Fix infinite loop in for_offset_range() Josh Poimboeuf
@ 2020-04-25 20:04 ` Randy Dunlap
  2020-04-25 20:07   ` Randy Dunlap
  2020-04-26  7:33 ` [tip: x86/urgent] " tip-bot2 for Josh Poimboeuf
  1 sibling, 1 reply; 4+ messages in thread
From: Randy Dunlap @ 2020-04-25 20:04 UTC (permalink / raw)
  To: Josh Poimboeuf, x86
  Cc: linux-kernel, Peter Zijlstra, Miroslav Benes, Julien Thierry

On 4/25/20 12:19 PM, Josh Poimboeuf wrote:
> Randy reported that objtool got stuck in an infinite loop when
> processing drivers/i2c/busses/i2c-parport.o.  It was caused by the
> following code:
> 
>   00000000000001fd <line_set>:
>    1fd:	48 b8 00 00 00 00 00 	movabs $0x0,%rax
>    204:	00 00 00
>   			1ff: R_X86_64_64	.rodata-0x8
>    207:	41 55                	push   %r13
>    209:	41 89 f5             	mov    %esi,%r13d
>    20c:	41 54                	push   %r12
>    20e:	49 89 fc             	mov    %rdi,%r12
>    211:	55                   	push   %rbp
>    212:	48 89 d5             	mov    %rdx,%rbp
>    215:	53                   	push   %rbx
>    216:	0f b6 5a 01          	movzbl 0x1(%rdx),%ebx
>    21a:	48 8d 34 dd 00 00 00 	lea    0x0(,%rbx,8),%rsi
>    221:	00
>   			21e: R_X86_64_32S	.rodata
>    222:	48 89 f1             	mov    %rsi,%rcx
>    225:	48 29 c1             	sub    %rax,%rcx
> 
> find_jump_table() saw the .rodata reference and tried to find a jump
> table associated with it (though there wasn't one).  The -0x8 rela
> addend is unusual.  It caused find_jump_table() to send a negative
> table_offset (unsigned 0xfffffffffffffff8) to find_rela_by_dest().
> 
> The negative offset should have been harmless, but it actually threw
> for_offset_range() for a loop... literally.  When the mask value got
> incremented past the end value, it also wrapped to zero, causing the
> loop exit condition to remain true forever.
> 
> Prevent this scenario from happening by ensuring the incremented value
> is always >= the starting value.
> 
> Fixes: 74b873e49d92 ("objtool: Optimize find_rela_by_dest_range()")
> Reported-by: Randy Dunlap <rdunlap@infradead.org>
> Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>

Hi Josh,

When applied to linux-next 20200414 (where it was reported) and using
config-r2092, objtool still loops (I killed it after 6 minutes of CPU time).

When applied to linux-next 20200424 and using the same config-r2092 file,
objtool appears to terminate normally and the entire build does also.

Acked-by: Randy Dunlap <rdunlap@infradead.org>
Tested-by: Randy Dunlap <rdunlap@infradead.org>

thanks.

> ---
>  tools/objtool/elf.h | 7 ++++---
>  1 file changed, 4 insertions(+), 3 deletions(-)
> 
> diff --git a/tools/objtool/elf.h b/tools/objtool/elf.h
> index 5e76ac38cf99..f753148f5dac 100644
> --- a/tools/objtool/elf.h
> +++ b/tools/objtool/elf.h
> @@ -89,9 +89,10 @@ struct elf {
>  #define OFFSET_STRIDE		(1UL << OFFSET_STRIDE_BITS)
>  #define OFFSET_STRIDE_MASK	(~(OFFSET_STRIDE - 1))
>  
> -#define for_offset_range(_offset, _start, _end)		\
> -	for (_offset = ((_start) & OFFSET_STRIDE_MASK);	\
> -	     _offset <= ((_end) & OFFSET_STRIDE_MASK);	\
> +#define for_offset_range(_offset, _start, _end)			\
> +	for (_offset = ((_start) & OFFSET_STRIDE_MASK);		\
> +	     _offset >= ((_start) & OFFSET_STRIDE_MASK) &&	\
> +	     _offset <= ((_end) & OFFSET_STRIDE_MASK);		\
>  	     _offset += OFFSET_STRIDE)
>  
>  static inline u32 sec_offset_hash(struct section *sec, unsigned long offset)
> 


-- 
~Randy

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

* Re: [PATCH] objtool: Fix infinite loop in for_offset_range()
  2020-04-25 20:04 ` Randy Dunlap
@ 2020-04-25 20:07   ` Randy Dunlap
  0 siblings, 0 replies; 4+ messages in thread
From: Randy Dunlap @ 2020-04-25 20:07 UTC (permalink / raw)
  To: Josh Poimboeuf, x86
  Cc: linux-kernel, Peter Zijlstra, Miroslav Benes, Julien Thierry

On 4/25/20 1:04 PM, Randy Dunlap wrote:
> On 4/25/20 12:19 PM, Josh Poimboeuf wrote:
>> Randy reported that objtool got stuck in an infinite loop when
>> processing drivers/i2c/busses/i2c-parport.o.  It was caused by the
>> following code:
>>
>>   00000000000001fd <line_set>:
>>    1fd:	48 b8 00 00 00 00 00 	movabs $0x0,%rax
>>    204:	00 00 00
>>   			1ff: R_X86_64_64	.rodata-0x8
>>    207:	41 55                	push   %r13
>>    209:	41 89 f5             	mov    %esi,%r13d
>>    20c:	41 54                	push   %r12
>>    20e:	49 89 fc             	mov    %rdi,%r12
>>    211:	55                   	push   %rbp
>>    212:	48 89 d5             	mov    %rdx,%rbp
>>    215:	53                   	push   %rbx
>>    216:	0f b6 5a 01          	movzbl 0x1(%rdx),%ebx
>>    21a:	48 8d 34 dd 00 00 00 	lea    0x0(,%rbx,8),%rsi
>>    221:	00
>>   			21e: R_X86_64_32S	.rodata
>>    222:	48 89 f1             	mov    %rsi,%rcx
>>    225:	48 29 c1             	sub    %rax,%rcx
>>
>> find_jump_table() saw the .rodata reference and tried to find a jump
>> table associated with it (though there wasn't one).  The -0x8 rela
>> addend is unusual.  It caused find_jump_table() to send a negative
>> table_offset (unsigned 0xfffffffffffffff8) to find_rela_by_dest().
>>
>> The negative offset should have been harmless, but it actually threw
>> for_offset_range() for a loop... literally.  When the mask value got
>> incremented past the end value, it also wrapped to zero, causing the
>> loop exit condition to remain true forever.
>>
>> Prevent this scenario from happening by ensuring the incremented value
>> is always >= the starting value.
>>
>> Fixes: 74b873e49d92 ("objtool: Optimize find_rela_by_dest_range()")
>> Reported-by: Randy Dunlap <rdunlap@infradead.org>
>> Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
> 
> Hi Josh,
> 
> When applied to linux-next 20200414 (where it was reported) and using
> config-r2092, objtool still loops (I killed it after 6 minutes of CPU time).

Nope, scratch that. Operator error. Sorry.

> When applied to linux-next 20200424 and using the same config-r2092 file,
> objtool appears to terminate normally and the entire build does also.
> 
> Acked-by: Randy Dunlap <rdunlap@infradead.org>
> Tested-by: Randy Dunlap <rdunlap@infradead.org>
> 
> thanks.
> 
>> ---


-- 
~Randy


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

* [tip: x86/urgent] objtool: Fix infinite loop in for_offset_range()
  2020-04-25 19:19 [PATCH] objtool: Fix infinite loop in for_offset_range() Josh Poimboeuf
  2020-04-25 20:04 ` Randy Dunlap
@ 2020-04-26  7:33 ` tip-bot2 for Josh Poimboeuf
  1 sibling, 0 replies; 4+ messages in thread
From: tip-bot2 for Josh Poimboeuf @ 2020-04-26  7:33 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: Randy Dunlap, Josh Poimboeuf, Ingo Molnar, Julien Thierry,
	Miroslav Benes, Peter Zijlstra, x86, LKML

The following commit has been merged into the x86/urgent branch of tip:

Commit-ID:     53fb6e990d782ded62d7c76d566e107c03393b74
Gitweb:        https://git.kernel.org/tip/53fb6e990d782ded62d7c76d566e107c03393b74
Author:        Josh Poimboeuf <jpoimboe@redhat.com>
AuthorDate:    Sat, 25 Apr 2020 14:19:01 -05:00
Committer:     Ingo Molnar <mingo@kernel.org>
CommitterDate: Sun, 26 Apr 2020 09:28:14 +02:00

objtool: Fix infinite loop in for_offset_range()

Randy reported that objtool got stuck in an infinite loop when
processing drivers/i2c/busses/i2c-parport.o.  It was caused by the
following code:

  00000000000001fd <line_set>:
   1fd:	48 b8 00 00 00 00 00	movabs $0x0,%rax
   204:	00 00 00
			1ff: R_X86_64_64	.rodata-0x8
   207:	41 55                	push   %r13
   209:	41 89 f5             	mov    %esi,%r13d
   20c:	41 54                	push   %r12
   20e:	49 89 fc             	mov    %rdi,%r12
   211:	55                   	push   %rbp
   212:	48 89 d5             	mov    %rdx,%rbp
   215:	53                   	push   %rbx
   216:	0f b6 5a 01          	movzbl 0x1(%rdx),%ebx
   21a:	48 8d 34 dd 00 00 00 	lea    0x0(,%rbx,8),%rsi
   221:	00
			21e: R_X86_64_32S	.rodata
   222:	48 89 f1             	mov    %rsi,%rcx
   225:	48 29 c1             	sub    %rax,%rcx

find_jump_table() saw the .rodata reference and tried to find a jump
table associated with it (though there wasn't one).  The -0x8 rela
addend is unusual.  It caused find_jump_table() to send a negative
table_offset (unsigned 0xfffffffffffffff8) to find_rela_by_dest().

The negative offset should have been harmless, but it actually threw
for_offset_range() for a loop... literally.  When the mask value got
incremented past the end value, it also wrapped to zero, causing the
loop exit condition to remain true forever.

Prevent this scenario from happening by ensuring the incremented value
is always >= the starting value.

Fixes: 74b873e49d92 ("objtool: Optimize find_rela_by_dest_range()")
Reported-by: Randy Dunlap <rdunlap@infradead.org>
Tested-by: Randy Dunlap <rdunlap@infradead.org>
Acked-by: Randy Dunlap <rdunlap@infradead.org>
Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Cc: Julien Thierry <jthierry@redhat.com>
Cc: Miroslav Benes <mbenes@suse.cz>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: https://lore.kernel.org/r/02b719674b031800b61e33c30b2e823183627c19.1587842122.git.jpoimboe@redhat.com
---
 tools/objtool/elf.h | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/tools/objtool/elf.h b/tools/objtool/elf.h
index ebbb10c..c227a2e 100644
--- a/tools/objtool/elf.h
+++ b/tools/objtool/elf.h
@@ -87,9 +87,10 @@ struct elf {
 #define OFFSET_STRIDE		(1UL << OFFSET_STRIDE_BITS)
 #define OFFSET_STRIDE_MASK	(~(OFFSET_STRIDE - 1))
 
-#define for_offset_range(_offset, _start, _end)		\
-	for (_offset = ((_start) & OFFSET_STRIDE_MASK);	\
-	     _offset <= ((_end) & OFFSET_STRIDE_MASK);	\
+#define for_offset_range(_offset, _start, _end)			\
+	for (_offset = ((_start) & OFFSET_STRIDE_MASK);		\
+	     _offset >= ((_start) & OFFSET_STRIDE_MASK) &&	\
+	     _offset <= ((_end) & OFFSET_STRIDE_MASK);		\
 	     _offset += OFFSET_STRIDE)
 
 static inline u32 sec_offset_hash(struct section *sec, unsigned long offset)

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

end of thread, other threads:[~2020-04-26  7:34 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-25 19:19 [PATCH] objtool: Fix infinite loop in for_offset_range() Josh Poimboeuf
2020-04-25 20:04 ` Randy Dunlap
2020-04-25 20:07   ` Randy Dunlap
2020-04-26  7:33 ` [tip: x86/urgent] " tip-bot2 for Josh Poimboeuf

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 NNTP newsgroup(s).