kobject: Replace strlcpy with strscpy
Subsystems (2)
PATCH v2 1/1 kobject: Replace strlcpy with strscpy
strlcpy() reads the entire source buffer first. This read may exceed the destination size limit. This is both inefficient and can lead to linear read overflows if a source string is not NUL-terminated [1]. In an effort to remove strlcpy() completely [2], replace strlcpy() here with strscpy().
Direct replacement is safe here since return value of -errno is used to check for truncation instead of sizeof(dest).
[1] https://www.kernel.org/doc/html/latest/process/deprecated.html#strlcpy
[2] https://github.com/KSPP/linux/issues/89
---
v2:
* Updated error message to remove @len which is errno now.
v1:
* https://lore.kernel.org/all/[email protected]/
lib/kobject_uevent.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
lib/kobject_uevent.c +4 -4
@@ -254,10 +254,10 @@ static int init_uevent_argv(struct kobj_uevent_env *env, const char *subsystem)int buffer_size =sizeof (env->buf) - env->buflen;int len; - len =strlcpy (&env->buf[env->buflen], subsystem, buffer_size); -if (len >= buffer_size) { -pr_warn ("init_uevent_argv: buffer size of %d too small, needed %d\n" , - buffer_size, len); + len =strscpy (&env->buf[env->buflen], subsystem, buffer_size); +if (len <0 ) { +pr_warn ("init_uevent_argv: insufficient buffer space " +"(%u left) for %s\n" , buffer_size, subsystem);return -ENOMEM ; }
Signature · 1 line
2.42.0.rc2.253.gd59a3bf2b4-goog
lib/kobject_uevent.c static int init_uevent_argv(struct kobj_uevent_env *env, const char *subsystem) · line 260
Quoted 8 lines
lib/kobject_uevent.c +4
-4- len =strlcpy (&env->buf[env->buflen], subsystem, buffer_size); -if (len >= buffer_size) { -pr_warn ("init_uevent_argv: buffer size of %d too small, needed %d\n" , - buffer_size, len); + len =strscpy (&env->buf[env->buflen], subsystem, buffer_size); +if (len <0 ) { +pr_warn ("init_uevent_argv: insufficient buffer space " +"(%u left) for %s\n" , buffer_size, subsystem);
Please ignore this patch. There's some weird indentation that I noticed here that I'll fix in v3.