Skip to contentSkip to conversation

s390/debug: Replace kmalloc() + copy_from_user() with memdup_user_nul()

PATCH Applied Thorsten Blum 3 participants 3 messages Sep 4, 2025 – Sep 8, 2025 s390

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 s390/debug: Replace kmalloc() + copy_from_user() with memdup_user_nul()

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.

---
 arch/s390/kernel/debug.c | 12 +++---------
 1 file changed, 3 insertions(+), 9 deletions(-)
arch/s390/kernel/debug.c +3 -9
index c62100dc62c8..6a26f202441d 100644
@@ -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;
 }
 
Signature · 1 line
2.51.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 27 lines · Thorsten Blum

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.

---
 arch/s390/kernel/debug.c | 12 +++---------
 1 file changed, 3 insertions(+), 9 deletions(-)
arch/s390/kernel/debug.c +1 -7
index c62100dc62c8..6a26f202441d 100644
@@ -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);

arch/s390/kernel/debug.c static inline char *debug_get_user_string(const char __user *user_buf, · line 1427

Quoted 10 lines
arch/s390/kernel/debug.c +2 -2
+	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!

maintainer Applied
#2 lore raw
In reply to Thorsten Blum
Quoted 10 lines · Thorsten Blum

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.

---
 arch/s390/kernel/debug.c | 12 +++---------
 1 file changed, 3 insertions(+), 9 deletions(-)

Applied, thanks!