All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 1/2] powerpc: Avoid signed to unsigned conversion in set_thread_tidr()
@ 2017-11-24  8:33 Vaibhav Jain
  2017-11-24  8:33 ` [PATCH v2 2/2] powerpc: Do not assign thread.tidr if already assigned Vaibhav Jain
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Vaibhav Jain @ 2017-11-24  8:33 UTC (permalink / raw)
  To: linuxppc-dev, Sukadev Bhattiprolu, Christophe Lombard,
	Philippe Bergheaud, Michael Ellerman
  Cc: Vaibhav Jain, Andrew Donnellan, Alastair D'Silva, Frederic Barrat

There is an unsafe signed to unsigned conversion in set_thread_tidr()
that may cause an error value to be assigned to SPRN_TIDR register and
used as thread-id.

The issue happens as assign_thread_tidr() returns an int and
thread.tidr is an unsigned-long. So a negative error code returned
from assign_thread_tidr() will fail the error check and gets assigned
as tidr as a large positive value.

To fix this the patch assigns the return value of assign_thread_tidr()
to a temporary int and assigns it to thread.tidr iff its '> 0'.

The patch shouldn't impact the calling convention of set_thread_tidr()
i.e all -ve return-values are error codes, +ve return values are
assigned Thread-ids. The way function is implemented it should never
return a '0'.

Fixes: ec233ede4c86("powerpc: Add support for setting SPRN_TIDR")
Signed-off-by: Vaibhav Jain <vaibhav@linux.vnet.ibm.com>

---
Changelog:

v2  ->	* Update the patch description to document the calling
	convention of set_thread_tidr(). [Mpe]
	* Fix a tidr allocation leak.
---
 arch/powerpc/kernel/process.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/arch/powerpc/kernel/process.c b/arch/powerpc/kernel/process.c
index bfdd783e3916..5d8176c7c2d8 100644
--- a/arch/powerpc/kernel/process.c
+++ b/arch/powerpc/kernel/process.c
@@ -1569,19 +1569,21 @@ void arch_release_task_struct(struct task_struct *t)
  */
 int set_thread_tidr(struct task_struct *t)
 {
+	int rc;
+
 	if (!cpu_has_feature(CPU_FTR_ARCH_300))
 		return -EINVAL;
 
 	if (t != current)
 		return -EINVAL;
 
-	t->thread.tidr = assign_thread_tidr();
-	if (t->thread.tidr < 0)
-		return t->thread.tidr;
-
-	mtspr(SPRN_TIDR, t->thread.tidr);
+	rc = assign_thread_tidr();
+	if (rc > 0) {
+		t->thread.tidr = rc;
+		mtspr(SPRN_TIDR, t->thread.tidr);
+	}
 
-	return 0;
+	return rc;
 }
 
 #endif /* CONFIG_PPC64 */
-- 
2.14.3

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

* [PATCH v2 2/2] powerpc: Do not assign thread.tidr if already assigned
  2017-11-24  8:33 [PATCH v2 1/2] powerpc: Avoid signed to unsigned conversion in set_thread_tidr() Vaibhav Jain
@ 2017-11-24  8:33 ` Vaibhav Jain
  2017-11-27  1:06   ` Andrew Donnellan
  2017-11-29 12:06   ` [v2,2/2] " Michael Ellerman
  2017-11-27  1:00 ` [PATCH v2 1/2] powerpc: Avoid signed to unsigned conversion in set_thread_tidr() Andrew Donnellan
  2017-11-27  3:48 ` Michael Ellerman
  2 siblings, 2 replies; 7+ messages in thread
From: Vaibhav Jain @ 2017-11-24  8:33 UTC (permalink / raw)
  To: linuxppc-dev, Sukadev Bhattiprolu, Christophe Lombard,
	Philippe Bergheaud, Michael Ellerman
  Cc: Vaibhav Jain, Andrew Donnellan, Alastair D'Silva, Frederic Barrat

If set_thread_tidr() is called twice for same task_struct then it will
allocate a new tidr value to it leaving the previous value still
dangling in the vas_thread_ida table.

To fix this the patch changes set_thread_tidr() to check if a tidr
value is already assigned to the task_struct and if yes then returns
the existing value from function instead of allocating a new one.

Fixes: ec233ede4c86("powerpc: Add support for setting SPRN_TIDR")
Signed-off-by: Vaibhav Jain <vaibhav@linux.vnet.ibm.com>
---
v2 ->	Fix minor spell errors in patch description
---
 arch/powerpc/kernel/process.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/arch/powerpc/kernel/process.c b/arch/powerpc/kernel/process.c
index 5d8176c7c2d8..9a72282b022d 100644
--- a/arch/powerpc/kernel/process.c
+++ b/arch/powerpc/kernel/process.c
@@ -1577,6 +1577,9 @@ int set_thread_tidr(struct task_struct *t)
 	if (t != current)
 		return -EINVAL;
 
+	if (t->thread.tidr)
+		return t->thread.tidr;
+
 	rc = assign_thread_tidr();
 	if (rc > 0) {
 		t->thread.tidr = rc;
-- 
2.14.3

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

* Re: [PATCH v2 1/2] powerpc: Avoid signed to unsigned conversion in set_thread_tidr()
  2017-11-24  8:33 [PATCH v2 1/2] powerpc: Avoid signed to unsigned conversion in set_thread_tidr() Vaibhav Jain
  2017-11-24  8:33 ` [PATCH v2 2/2] powerpc: Do not assign thread.tidr if already assigned Vaibhav Jain
@ 2017-11-27  1:00 ` Andrew Donnellan
  2017-11-27  3:48 ` Michael Ellerman
  2 siblings, 0 replies; 7+ messages in thread
From: Andrew Donnellan @ 2017-11-27  1:00 UTC (permalink / raw)
  To: Vaibhav Jain, linuxppc-dev, Sukadev Bhattiprolu,
	Christophe Lombard, Philippe Bergheaud, Michael Ellerman
  Cc: Alastair D'Silva, Frederic Barrat

On 24/11/17 19:33, Vaibhav Jain wrote:
> There is an unsafe signed to unsigned conversion in set_thread_tidr()
> that may cause an error value to be assigned to SPRN_TIDR register and
> used as thread-id.
> 
> The issue happens as assign_thread_tidr() returns an int and
> thread.tidr is an unsigned-long. So a negative error code returned
> from assign_thread_tidr() will fail the error check and gets assigned
> as tidr as a large positive value.
> 
> To fix this the patch assigns the return value of assign_thread_tidr()
> to a temporary int and assigns it to thread.tidr iff its '> 0'.
> 
> The patch shouldn't impact the calling convention of set_thread_tidr()
> i.e all -ve return-values are error codes, +ve return values are
> assigned Thread-ids. The way function is implemented it should never
> return a '0'.
> 
> Fixes: ec233ede4c86("powerpc: Add support for setting SPRN_TIDR")
> Signed-off-by: Vaibhav Jain <vaibhav@linux.vnet.ibm.com>

Reviewed-by: Andrew Donnellan <andrew.donnellan@au1.ibm.com>

> 
> ---
> Changelog:
> 
> v2  ->	* Update the patch description to document the calling
> 	convention of set_thread_tidr(). [Mpe]
> 	* Fix a tidr allocation leak.
> ---
>   arch/powerpc/kernel/process.c | 14 ++++++++------
>   1 file changed, 8 insertions(+), 6 deletions(-)
> 
> diff --git a/arch/powerpc/kernel/process.c b/arch/powerpc/kernel/process.c
> index bfdd783e3916..5d8176c7c2d8 100644
> --- a/arch/powerpc/kernel/process.c
> +++ b/arch/powerpc/kernel/process.c
> @@ -1569,19 +1569,21 @@ void arch_release_task_struct(struct task_struct *t)
>    */
>   int set_thread_tidr(struct task_struct *t)
>   {
> +	int rc;
> +
>   	if (!cpu_has_feature(CPU_FTR_ARCH_300))
>   		return -EINVAL;
> 
>   	if (t != current)
>   		return -EINVAL;
> 
> -	t->thread.tidr = assign_thread_tidr();
> -	if (t->thread.tidr < 0)
> -		return t->thread.tidr;
> -
> -	mtspr(SPRN_TIDR, t->thread.tidr);
> +	rc = assign_thread_tidr();
> +	if (rc > 0) {
> +		t->thread.tidr = rc;
> +		mtspr(SPRN_TIDR, t->thread.tidr);
> +	}
> 
> -	return 0;
> +	return rc;
>   }
> 
>   #endif /* CONFIG_PPC64 */
> 

-- 
Andrew Donnellan              OzLabs, ADL Canberra
andrew.donnellan@au1.ibm.com  IBM Australia Limited

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

* Re: [PATCH v2 2/2] powerpc: Do not assign thread.tidr if already assigned
  2017-11-24  8:33 ` [PATCH v2 2/2] powerpc: Do not assign thread.tidr if already assigned Vaibhav Jain
@ 2017-11-27  1:06   ` Andrew Donnellan
  2017-11-29 12:06   ` [v2,2/2] " Michael Ellerman
  1 sibling, 0 replies; 7+ messages in thread
From: Andrew Donnellan @ 2017-11-27  1:06 UTC (permalink / raw)
  To: Vaibhav Jain, linuxppc-dev, Sukadev Bhattiprolu,
	Christophe Lombard, Philippe Bergheaud, Michael Ellerman
  Cc: Alastair D'Silva, Frederic Barrat



On 24/11/17 19:33, Vaibhav Jain wrote:
> If set_thread_tidr() is called twice for same task_struct then it will
> allocate a new tidr value to it leaving the previous value still
> dangling in the vas_thread_ida table.
> 
> To fix this the patch changes set_thread_tidr() to check if a tidr
> value is already assigned to the task_struct and if yes then returns
> the existing value from function instead of allocating a new one.
> 
> Fixes: ec233ede4c86("powerpc: Add support for setting SPRN_TIDR")
> Signed-off-by: Vaibhav Jain <vaibhav@linux.vnet.ibm.com>

Reviewed-by: Andrew Donnellan <andrew.donnellan@au1.ibm.com>

> ---
> v2 ->	Fix minor spell errors in patch description
> ---
>   arch/powerpc/kernel/process.c | 3 +++
>   1 file changed, 3 insertions(+)
> 
> diff --git a/arch/powerpc/kernel/process.c b/arch/powerpc/kernel/process.c
> index 5d8176c7c2d8..9a72282b022d 100644
> --- a/arch/powerpc/kernel/process.c
> +++ b/arch/powerpc/kernel/process.c
> @@ -1577,6 +1577,9 @@ int set_thread_tidr(struct task_struct *t)
>   	if (t != current)
>   		return -EINVAL;
> 
> +	if (t->thread.tidr)
> +		return t->thread.tidr;
> +
>   	rc = assign_thread_tidr();
>   	if (rc > 0) {
>   		t->thread.tidr = rc;
> 

-- 
Andrew Donnellan              OzLabs, ADL Canberra
andrew.donnellan@au1.ibm.com  IBM Australia Limited

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

* Re: [PATCH v2 1/2] powerpc: Avoid signed to unsigned conversion in set_thread_tidr()
  2017-11-24  8:33 [PATCH v2 1/2] powerpc: Avoid signed to unsigned conversion in set_thread_tidr() Vaibhav Jain
  2017-11-24  8:33 ` [PATCH v2 2/2] powerpc: Do not assign thread.tidr if already assigned Vaibhav Jain
  2017-11-27  1:00 ` [PATCH v2 1/2] powerpc: Avoid signed to unsigned conversion in set_thread_tidr() Andrew Donnellan
@ 2017-11-27  3:48 ` Michael Ellerman
  2017-11-27 17:24   ` Vaibhav Jain
  2 siblings, 1 reply; 7+ messages in thread
From: Michael Ellerman @ 2017-11-27  3:48 UTC (permalink / raw)
  To: Vaibhav Jain, linuxppc-dev, Sukadev Bhattiprolu,
	Christophe Lombard, Philippe Bergheaud
  Cc: Vaibhav Jain, Andrew Donnellan, Alastair D'Silva, Frederic Barrat

Vaibhav Jain <vaibhav@linux.vnet.ibm.com> writes:

> There is an unsafe signed to unsigned conversion in set_thread_tidr()
> that may cause an error value to be assigned to SPRN_TIDR register and
> used as thread-id.
>
> The issue happens as assign_thread_tidr() returns an int and
> thread.tidr is an unsigned-long. So a negative error code returned
> from assign_thread_tidr() will fail the error check and gets assigned
> as tidr as a large positive value.
>
> To fix this the patch assigns the return value of assign_thread_tidr()
> to a temporary int and assigns it to thread.tidr iff its '> 0'.
>
> The patch shouldn't impact the calling convention of set_thread_tidr()

But it does? I'm really confused.

Please fix and resend.

cheers

>  int set_thread_tidr(struct task_struct *t)
>  {
> +	int rc;
> +
>  	if (!cpu_has_feature(CPU_FTR_ARCH_300))
>  		return -EINVAL;
                ^
                -ve error

>  
>  	if (t != current)
>  		return -EINVAL;
                ^
                -ve error

>  
> -	t->thread.tidr = assign_thread_tidr();
> -	if (t->thread.tidr < 0)
> -		return t->thread.tidr;
                ^
                -ve error

Yes I know we never hit that case because of the bug, that is what this
patch should be fixing, and only that.

> -
> -	mtspr(SPRN_TIDR, t->thread.tidr);
> +	rc = assign_thread_tidr();
> +	if (rc > 0) {
> +		t->thread.tidr = rc;
> +		mtspr(SPRN_TIDR, t->thread.tidr);
> +	}
>  
> -	return 0;

Success == 0

> +	return rc;

Success == +ve TIDR

>  }

cheers

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

* Re: [PATCH v2 1/2] powerpc: Avoid signed to unsigned conversion in set_thread_tidr()
  2017-11-27  3:48 ` Michael Ellerman
@ 2017-11-27 17:24   ` Vaibhav Jain
  0 siblings, 0 replies; 7+ messages in thread
From: Vaibhav Jain @ 2017-11-27 17:24 UTC (permalink / raw)
  To: Michael Ellerman, linuxppc-dev, Sukadev Bhattiprolu,
	Christophe Lombard, Philippe Bergheaud
  Cc: Frederic Barrat, Alastair D'Silva, Andrew Donnellan

Michael Ellerman <mpe@ellerman.id.au> writes:

> Success == 0
>
>> +	return rc;
>
> Success == +ve TIDR
>
>>  }
>
> cheers
>

Thanks for reviewing the patch Mpe, I have updated the patch and sent a
v3 with the fix that shouldn't impact the calling convention.

-- 
Vaibhav Jain <vaibhav@linux.vnet.ibm.com>
Linux Technology Center, IBM India Pvt. Ltd.

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

* Re: [v2,2/2] powerpc: Do not assign thread.tidr if already assigned
  2017-11-24  8:33 ` [PATCH v2 2/2] powerpc: Do not assign thread.tidr if already assigned Vaibhav Jain
  2017-11-27  1:06   ` Andrew Donnellan
@ 2017-11-29 12:06   ` Michael Ellerman
  1 sibling, 0 replies; 7+ messages in thread
From: Michael Ellerman @ 2017-11-29 12:06 UTC (permalink / raw)
  To: Vaibhav Jain, linuxppc-dev, Sukadev Bhattiprolu,
	Christophe Lombard, Philippe Bergheaud
  Cc: Frederic Barrat, Alastair D'Silva, Vaibhav Jain, Andrew Donnellan

On Fri, 2017-11-24 at 08:33:38 UTC, Vaibhav Jain wrote:
> If set_thread_tidr() is called twice for same task_struct then it will
> allocate a new tidr value to it leaving the previous value still
> dangling in the vas_thread_ida table.
> 
> To fix this the patch changes set_thread_tidr() to check if a tidr
> value is already assigned to the task_struct and if yes then returns
> the existing value from function instead of allocating a new one.
> 
> Fixes: ec233ede4c86("powerpc: Add support for setting SPRN_TIDR")
> Signed-off-by: Vaibhav Jain <vaibhav@linux.vnet.ibm.com>
> Reviewed-by: Andrew Donnellan <andrew.donnellan@au1.ibm.com>

Applied to powerpc fixes, thanks.

https://git.kernel.org/powerpc/c/7e4d4233260be0611c7fbdb2730c12

cheers

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

end of thread, other threads:[~2017-11-29 12:06 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-11-24  8:33 [PATCH v2 1/2] powerpc: Avoid signed to unsigned conversion in set_thread_tidr() Vaibhav Jain
2017-11-24  8:33 ` [PATCH v2 2/2] powerpc: Do not assign thread.tidr if already assigned Vaibhav Jain
2017-11-27  1:06   ` Andrew Donnellan
2017-11-29 12:06   ` [v2,2/2] " Michael Ellerman
2017-11-27  1:00 ` [PATCH v2 1/2] powerpc: Avoid signed to unsigned conversion in set_thread_tidr() Andrew Donnellan
2017-11-27  3:48 ` Michael Ellerman
2017-11-27 17:24   ` Vaibhav Jain

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.