Skip to contentSkip to conversation

s390/ipl: refactor deprecated strncpy

PATCH In review Justin Stitt 3 participants 4 messages Aug 11, 2023 – Aug 17, 2023 s390

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 s390/ipl: refactor deprecated strncpy

strncpy is deprecated for use on NUL-terminated destination strings [1].

Use strscpy_pad which has the same behavior as strncpy here with the extra safeguard of guaranteeing NUL-termination of destination strings. In it's current form, this may result in silent truncation if the src string has the same size as the destination string.

Link www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings[1]
---
 arch/s390/kernel/ipl.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/s390/kernel/ipl.c +1 -1
index 85a00d97a314..a6dcf6f28197 100644
@@ -266,7 +266,7 @@ static ssize_t sys_##_prefix##_##_name##_store(struct kobject *kobj,	\
 		struct kobj_attribute *attr,				\
 		const char *buf, size_t len)				\
 {									\
-	strncpy(_value, buf, sizeof(_value) - 1);			\
+	strscpy_pad(_value, buf, sizeof(_value));			\
 	strim(_value);							\
 	return len;							\
 }									\

---
base-commit: 52a93d39b17dc7eb98b6aa3edb93943248e03b2f
change-id: 20230811-arch-s390-kernel-968d0545c45a

Best regards,
--
Justin Stitt <[email protected]>

#1 lore raw
Quoted 20 lines · Justin Stitt

strncpy is deprecated for use on NUL-terminated destination strings [1].

Use strscpy_pad which has the same behavior as strncpy here with the extra safeguard of guaranteeing NUL-termination of destination strings. In it's current form, this may result in silent truncation if the src string has the same size as the destination string.

Link www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings[1]
---
 arch/s390/kernel/ipl.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/s390/kernel/ipl.c +0 -0
index 85a00d97a314..a6dcf6f28197 100644

arch/s390/kernel/ipl.c static ssize_t sys_##_prefix##_##_name##_store(struct kobject *kobj, \ · line 269

Quoted 5 lines
arch/s390/kernel/ipl.c +1 -1
@@ -266,7 +266,7 @@ static ssize_t sys_##_prefix##_##_name##_store(struct kobject *kobj,	\
 		struct kobj_attribute *attr,				\
 		const char *buf, size_t len)				\
 {									\
-	strncpy(_value, buf, sizeof(_value) - 1);			\
+	strscpy_pad(_value, buf, sizeof(_value));			\

Padding isn't needed here -- the string are consumed by __cpcmd(), which explicitly uses strlen() and a memcpy to pass them off.

arch/s390/kernel/ipl.c static ssize_t sys_##_prefix##_##_name##_store(struct kobject *kobj, \ · line 270

Quoted 1 line
arch/s390/kernel/ipl.c +0 -0
 	strim(_value);							\

This existing code line is buggy, though -- it will not trim leading whitespace in the buffer. (It _returns_ a string that has been forward-adjusted.)

I think this is an API mistake -- strim() should either do in-place changes for both ends or be defined with __much_check so the return value doesn't get lost. (But this is a separate issue.)

-Kees

Signature · 1 line
Kees Cook
maintainer
#2 lore raw
Quoted 15 lines · Kees Cook
Quoted 14 lines · Justin Stitt

strncpy is deprecated for use on NUL-terminated destination strings [1].

Use strscpy_pad which has the same behavior as strncpy here with the extra safeguard of guaranteeing NUL-termination of destination strings. In it's current form, this may result in silent truncation if the src string has the same size as the destination string.

Link www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings[1]
---
 arch/s390/kernel/ipl.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

...

Quoted 11 lines

arch/s390/kernel/ipl.c static ssize_t sys_##_prefix##_##_name##_store(struct kobject *kobj, \ · line 269

Quoted 2 lines
arch/s390/kernel/ipl.c +1 -1
-	strncpy(_value, buf, sizeof(_value) - 1);			\
+	strscpy_pad(_value, buf, sizeof(_value));			\

Padding isn't needed here -- the string are consumed by __cpcmd(), which explicitly uses strlen() and a memcpy to pass them off.

Quoted 1 line

strim(_value); \

This existing code line is buggy, though -- it will not trim leading whitespace in the buffer. (It _returns_ a string that has been forward-adjusted.)

I'm quite sure that was intentional, so only whitespace at the end of the string, in particular '\n', will be stripped.
But then again this code is 15 years old...

Quoted 3 lines

I think this is an API mistake -- strim() should either do in-place changes for both ends or be defined with __much_check so the return value doesn't get lost. (But this is a separate issue.)

For __must_check see strstrip() which was actively avoided with commit 1d802e24774c ("[S390] Use strim instead of strstrip to avoid false warnings.") many years ago.

For all converted usages of strstrip() by that commit it was indeed intended to remove only trailing whitespace, where the above code snipped is the only piece of code which is questionable, since it is user space provided input; however I don't think this should be changed now.

maintainer
#3 lore raw
In reply to Justin Stitt
Quoted 20 lines · Justin Stitt

strncpy is deprecated for use on NUL-terminated destination strings [1].

Use strscpy_pad which has the same behavior as strncpy here with the extra safeguard of guaranteeing NUL-termination of destination strings. In it's current form, this may result in silent truncation if the src string has the same size as the destination string.

Link www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings[1]
---
 arch/s390/kernel/ipl.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/s390/kernel/ipl.c +0 -0
index 85a00d97a314..a6dcf6f28197 100644

arch/s390/kernel/ipl.c static ssize_t sys_##_prefix##_##_name##_store(struct kobject *kobj, \ · line 272

Quoted 8 lines
arch/s390/kernel/ipl.c +1 -1
@@ -266,7 +266,7 @@ static ssize_t sys_##_prefix##_##_name##_store(struct kobject *kobj,	\
 		struct kobj_attribute *attr,				\
 		const char *buf, size_t len)				\
 {									\
-	strncpy(_value, buf, sizeof(_value) - 1);			\
+	strscpy_pad(_value, buf, sizeof(_value));			\
 	strim(_value);							\
 	return len;							\
 }									\

Patch applied, but replaced strscpy_pad() with strscpy() like it was suggested by Kees.

Thanks!