diff mbox series

[v2,1/2] powerpc: Avoid signed to unsigned conversion in set_thread_tidr()

Message ID 20171124083338.15283-1-vaibhav@linux.vnet.ibm.com (mailing list archive)
State Superseded
Headers show
Series [v2,1/2] powerpc: Avoid signed to unsigned conversion in set_thread_tidr() | expand

Commit Message

Vaibhav Jain Nov. 24, 2017, 8:33 a.m. UTC
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(-)

Comments

Andrew Donnellan Nov. 27, 2017, 1 a.m. UTC | #1
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 */
>
Michael Ellerman Nov. 27, 2017, 3:48 a.m. UTC | #2
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
Vaibhav Jain Nov. 27, 2017, 5:24 p.m. UTC | #3
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.
diff mbox series

Patch

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 */