kobject: Replace strlcpy with strscpy
Subsystems (2)
PATCH v3 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
---
v3:
* Fixed weird indentation.
v2:
* Updated error message to remove @len which is errno now.
* https://lore.kernel.org/all/[email protected]/
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 ; }
--
2.42.0.rc2.253.gd59a3bf2b4-goog
Quoted 36 lines · Azeem Shaikh
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 removestrlcpy()completely [2], replacestrlcpy()here withstrscpy().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/89Signed-off-by Azeem Shaikh <[email protected]>---
v3:
* Fixed weird indentation.v2:
* Updated error message to remove @len which is errno now.
* https://lore.kernel.org/all/[email protected]/v1:
* https://lore.kernel.org/all/[email protected]/lib/kobject_uevent.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)lib/kobject_uevent.c +0
-0@@ -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;
lib/kobject_uevent.c static int init_uevent_argv(struct kobj_uevent_env *env, const char *subsystem) · line 262
Quoted 10 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);return -ENOMEM ; }
I thought scripts/checkpatch.pl would warn about this, but kernel coding-style (as it is) says to not split strings. I'd expect this to be:
+ pr_warn("init_uevent_argv: insufficient buffer space (%u left) for %s\n",
+ buffer_size, subsystem);
Signature · 1 line
Kees Cook