public inbox for linux-s390@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] s390/debug: Replace kmalloc() + copy_from_user() with memdup_user_nul()
@ 2025-09-04 11:40 Thorsten Blum
  2025-09-04 12:14 ` Niklas Schnelle
  2025-09-08 11:50 ` Alexander Gordeev
  0 siblings, 2 replies; 3+ messages in thread
From: Thorsten Blum @ 2025-09-04 11:40 UTC (permalink / raw)
  To: Heiko Carstens, Vasily Gorbik, Alexander Gordeev,
	Christian Borntraeger, Sven Schnelle, Halil Pasic,
	Niklas Schnelle, Bill O'Donnell, Al Viro, Tigran Mkrtchyan,
	Joel Granados
  Cc: Thorsten Blum, Wei Liu, linux-s390, linux-kernel

Replace kmalloc() followed by copy_from_user() with memdup_user_nul() to
improve and simplify debug_get_user_string(). Remove the manual
NUL-termination.

No functional changes intended.

Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
---
 arch/s390/kernel/debug.c | 12 +++---------
 1 file changed, 3 insertions(+), 9 deletions(-)

diff --git a/arch/s390/kernel/debug.c b/arch/s390/kernel/debug.c
index c62100dc62c8..6a26f202441d 100644
--- a/arch/s390/kernel/debug.c
+++ b/arch/s390/kernel/debug.c
@@ -1416,18 +1416,12 @@ static inline char *debug_get_user_string(const char __user *user_buf,
 {
 	char *buffer;
 
-	buffer = kmalloc(user_len + 1, GFP_KERNEL);
-	if (!buffer)
-		return ERR_PTR(-ENOMEM);
-	if (copy_from_user(buffer, user_buf, user_len) != 0) {
-		kfree(buffer);
-		return ERR_PTR(-EFAULT);
-	}
+	buffer = memdup_user_nul(user_buf, user_len);
+	if (IS_ERR(buffer))
+		return buffer;
 	/* got the string, now strip linefeed. */
 	if (buffer[user_len - 1] == '\n')
 		buffer[user_len - 1] = 0;
-	else
-		buffer[user_len] = 0;
 	return buffer;
 }
 
-- 
2.51.0


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

* Re: [PATCH] s390/debug: Replace kmalloc() + copy_from_user() with memdup_user_nul()
  2025-09-04 11:40 [PATCH] s390/debug: Replace kmalloc() + copy_from_user() with memdup_user_nul() Thorsten Blum
@ 2025-09-04 12:14 ` Niklas Schnelle
  2025-09-08 11:50 ` Alexander Gordeev
  1 sibling, 0 replies; 3+ messages in thread
From: Niklas Schnelle @ 2025-09-04 12:14 UTC (permalink / raw)
  To: Thorsten Blum, Heiko Carstens, Vasily Gorbik, Alexander Gordeev,
	Christian Borntraeger, Sven Schnelle, Halil Pasic,
	Bill O'Donnell, Al Viro, Tigran Mkrtchyan, Joel Granados
  Cc: Wei Liu, linux-s390, linux-kernel

On Thu, 2025-09-04 at 13:40 +0200, Thorsten Blum wrote:
> Replace kmalloc() followed by copy_from_user() with memdup_user_nul() to
> improve and simplify debug_get_user_string(). Remove the manual
> NUL-termination.
> 
> No functional changes intended.
> 
> Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
> ---
>  arch/s390/kernel/debug.c | 12 +++---------
>  1 file changed, 3 insertions(+), 9 deletions(-)
> 
> diff --git a/arch/s390/kernel/debug.c b/arch/s390/kernel/debug.c
> index c62100dc62c8..6a26f202441d 100644
> --- a/arch/s390/kernel/debug.c
> +++ b/arch/s390/kernel/debug.c
> @@ -1416,18 +1416,12 @@ static inline char *debug_get_user_string(const char __user *user_buf,
>  {
>  	char *buffer;
>  
> -	buffer = kmalloc(user_len + 1, GFP_KERNEL);
> -	if (!buffer)
> -		return ERR_PTR(-ENOMEM);
> -	if (copy_from_user(buffer, user_buf, user_len) != 0) {
> -		kfree(buffer);
> -		return ERR_PTR(-EFAULT);
> -	}
> +	buffer = memdup_user_nul(user_buf, user_len);
> +	if (IS_ERR(buffer))
> +		return buffer;
>  	/* got the string, now strip linefeed. */
>  	if (buffer[user_len - 1] == '\n')
>  		buffer[user_len - 1] = 0;
> -	else
> -		buffer[user_len] = 0;
>  	return buffer;
>  }
>  

I like it, thanks!

Reviewed-by: Niklas Schnelle <schnelle@linux.ibm.com>

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

* Re: [PATCH] s390/debug: Replace kmalloc() + copy_from_user() with memdup_user_nul()
  2025-09-04 11:40 [PATCH] s390/debug: Replace kmalloc() + copy_from_user() with memdup_user_nul() Thorsten Blum
  2025-09-04 12:14 ` Niklas Schnelle
@ 2025-09-08 11:50 ` Alexander Gordeev
  1 sibling, 0 replies; 3+ messages in thread
From: Alexander Gordeev @ 2025-09-08 11:50 UTC (permalink / raw)
  To: Thorsten Blum
  Cc: Heiko Carstens, Vasily Gorbik, Christian Borntraeger,
	Sven Schnelle, Halil Pasic, Niklas Schnelle, Bill O'Donnell,
	Al Viro, Tigran Mkrtchyan, Joel Granados, Wei Liu, linux-s390,
	linux-kernel

On Thu, Sep 04, 2025 at 01:40:29PM +0200, Thorsten Blum wrote:
> Replace kmalloc() followed by copy_from_user() with memdup_user_nul() to
> improve and simplify debug_get_user_string(). Remove the manual
> NUL-termination.
> 
> No functional changes intended.
> 
> Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
> ---
>  arch/s390/kernel/debug.c | 12 +++---------
>  1 file changed, 3 insertions(+), 9 deletions(-)

Applied, thanks!

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

end of thread, other threads:[~2025-09-08 11:50 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-09-04 11:40 [PATCH] s390/debug: Replace kmalloc() + copy_from_user() with memdup_user_nul() Thorsten Blum
2025-09-04 12:14 ` Niklas Schnelle
2025-09-08 11:50 ` Alexander Gordeev

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox