Skip to contentSkip to conversation

s390/debug: reject zero-length input before trimming a newline

PATCH In review Pengpeng Hou 3 participants 3 messages Apr 17 s390

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 s390/debug: reject zero-length input before trimming a newline

debug_get_user_string() duplicates the userspace buffer with memdup_user_nul() and then unconditionally looks at buffer[user_len - 1] to strip a trailing newline.

A zero-length write reaches this helper unchanged, so the newline trim reads before the start of the allocated buffer.

Reject empty writes before accessing the last input byte.

Fixes 66a464dbc8e0 ("[PATCH] s390: debug feature changes")
---
 arch/s390/kernel/debug.c | 3 +++
 1 file changed, 3 insertions(+)
arch/s390/kernel/debug.c +3 -0
index 31430e9bcfdd..2612f634e826 100644
@@ -1414,6 +1414,9 @@ static inline char *debug_get_user_string(const char __user *user_buf,
 {
 	char *buffer;
 
+	if (!user_len)
+		return ERR_PTR(-EINVAL);
+
 	buffer = memdup_user_nul(user_buf, user_len);
 	if (IS_ERR(buffer))
 		return buffer;
Signature · 1 line
2.50.1 (Apple Git-155)
Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · Pengpeng Hou

debug_get_user_string() duplicates the userspace buffer with memdup_user_nul() and then unconditionally looks at buffer[user_len - 1] to strip a trailing newline.

A zero-length write reaches this helper unchanged, so the newline trim reads before the start of the allocated buffer.

Reject empty writes before accessing the last input byte.

Fixes 66a464dbc8e0 ("[PATCH] s390: debug feature changes")

There shouldn't be a blank line here.

Quoted 10 lines
---
 arch/s390/kernel/debug.c | 3 +++
 1 file changed, 3 insertions(+)
arch/s390/kernel/debug.c +0 -0
index 31430e9bcfdd..2612f634e826 100644

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

Quoted 9 lines
arch/s390/kernel/debug.c +3 -0
@@ -1414,6 +1414,9 @@ static inline char *debug_get_user_string(const char __user *user_buf,
 {
 	char *buffer;
 
+	if (!user_len)
+		return ERR_PTR(-EINVAL);
+
 	buffer = memdup_user_nul(user_buf, user_len);
 	if (IS_ERR(buffer))
 		return buffer;

Otherwise this looks good to me.

Signature · 4 lines
Best Regards, Benjamin Block        /        Linux on IBM Z Kernel Development
IBM Deutschland Research & Development GmbH    /   https://www.ibm.com/privacy
Vors. Aufs.-R.: Wolfgang Wendt         /        Geschäftsführung: David Faller
Sitz der Ges.: Ehningen     /     Registergericht: AmtsG Stuttgart, HRB 243294
maintainer Reviewed-by Tested-by on v1 1/1
#2 lore raw
In reply to Pengpeng Hou
Quoted 7 lines · Pengpeng Hou

debug_get_user_string() duplicates the userspace buffer with memdup_user_nul() and then unconditionally looks at buffer[user_len - 1] to strip a trailing newline.

A zero-length write reaches this helper unchanged, so the newline trim reads before the start of the allocated buffer.

Quoted 8 lines

Reject empty writes before accessing the last input byte.

Fixes 66a464dbc8e0 ("[PATCH] s390: debug feature changes")
---
 arch/s390/kernel/debug.c | 3 +++
 1 file changed, 3 insertions(+)

Added
Cc: [email protected]

And applied, thank you!

I've also addressed Sashiko's complaint [1] about debug_input_flush_fn() as a separate patch.

[1] https://sashiko.dev/#/patchset/20260417073530.96002-1-pengpeng%40iscas.ac.cn