linuxppc-dev.lists.ozlabs.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v4] powerpc: Avoid signed to unsigned conversion in set_thread_tidr()
@ 2017-11-28  2:53 Vaibhav Jain
  2017-11-28  8:26 ` christophe lombard
  2017-11-29 12:06 ` [v4] " Michael Ellerman
  0 siblings, 2 replies; 3+ messages in thread
From: Vaibhav Jain @ 2017-11-28  2:53 UTC (permalink / raw)
  To: linuxppc-dev, Sukadev Bhattiprolu, Christophe Lombard,
	Philippe Bergheaud, Michael Ellerman
  Cc: Vaibhav Jain, Frederic Barrat, Andrew Donnellan,
	Alastair D'Silva

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 and a return value of '0'
indicates success.

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

---
Changelog:

v4  ->  Simplified the code flow [Sukadev]

v3  ->  Updated the patch to not impact the calling convention [Mpe, Christophe]

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 | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/arch/powerpc/kernel/process.c b/arch/powerpc/kernel/process.c
index bfdd783e3916..d205b52e3850 100644
--- a/arch/powerpc/kernel/process.c
+++ b/arch/powerpc/kernel/process.c
@@ -1569,16 +1569,19 @@ 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;
+	rc = assign_thread_tidr();
+	if (rc < 0)
+		return rc;
 
+	t->thread.tidr = rc;
 	mtspr(SPRN_TIDR, t->thread.tidr);
 
 	return 0;
-- 
2.14.3

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

* Re: [PATCH v4] powerpc: Avoid signed to unsigned conversion in set_thread_tidr()
  2017-11-28  2:53 [PATCH v4] powerpc: Avoid signed to unsigned conversion in set_thread_tidr() Vaibhav Jain
@ 2017-11-28  8:26 ` christophe lombard
  2017-11-29 12:06 ` [v4] " Michael Ellerman
  1 sibling, 0 replies; 3+ messages in thread
From: christophe lombard @ 2017-11-28  8:26 UTC (permalink / raw)
  To: linuxppc-dev

Le 28/11/2017 à 03:53, Vaibhav Jain a écrit :
> 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 and a return value of '0'
> indicates success.
> 
> Fixes: ec233ede4c86("powerpc: Add support for setting SPRN_TIDR")
> Signed-off-by: Vaibhav Jain <vaibhav@linux.vnet.ibm.com>
> 
> ---

sounds good for me
Thanks

Reviewed-by: Christophe Lombard clombard@linux.vnet.ibm.com

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

* Re: [v4] powerpc: Avoid signed to unsigned conversion in set_thread_tidr()
  2017-11-28  2:53 [PATCH v4] powerpc: Avoid signed to unsigned conversion in set_thread_tidr() Vaibhav Jain
  2017-11-28  8:26 ` christophe lombard
@ 2017-11-29 12:06 ` Michael Ellerman
  1 sibling, 0 replies; 3+ 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: Andrew Donnellan, Alastair D'Silva, Vaibhav Jain,
	Frederic Barrat

On Tue, 2017-11-28 at 02:53:04 UTC, 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 and a return value of '0'
> indicates success.
> 
> Fixes: ec233ede4c86("powerpc: Add support for setting SPRN_TIDR")
> Signed-off-by: Vaibhav Jain <vaibhav@linux.vnet.ibm.com>
> Reviewed-by: Christophe Lombard clombard@linux.vnet.ibm.com

Applied to powerpc fixes, thanks.

https://git.kernel.org/powerpc/c/aca7573fde95152378361cba734996

cheers

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

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

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-11-28  2:53 [PATCH v4] powerpc: Avoid signed to unsigned conversion in set_thread_tidr() Vaibhav Jain
2017-11-28  8:26 ` christophe lombard
2017-11-29 12:06 ` [v4] " Michael Ellerman

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).