SUNRPC: Replace strlcpy with strscpy
Subsystems (4)
PATCH 1/1 SUNRPC: 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 the getter in kernel_params_ops handles -errorno return [3].
[1] https://www.kernel.org/doc/html/latest/process/deprecated.html#strlcpy
[2] https://github.com/KSPP/linux/issues/89
[3] https://elixir.bootlin.com/linux/v6.4-rc6/source/include/linux/moduleparam.h#L52
--- net/sunrpc/svc.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
net/sunrpc/svc.c +4 -4
@@ -109,13 +109,13 @@ param_get_pool_mode(char *buf, const struct kernel_param *kp)switch (*ip) {case SVC_POOL_AUTO : -return strlcpy (buf,"auto\n" ,20 ); +return strscpy (buf,"auto\n" ,20 );case SVC_POOL_GLOBAL : -return strlcpy (buf,"global\n" ,20 ); +return strscpy (buf,"global\n" ,20 );case SVC_POOL_PERCPU : -return strlcpy (buf,"percpu\n" ,20 ); +return strscpy (buf,"percpu\n" ,20 );case SVC_POOL_PERNODE : -return strlcpy (buf,"pernode\n" ,20 ); +return strscpy (buf,"pernode\n" ,20 );default :return sprintf (buf,"%d\n" , *ip); }
Signature · 1 line
2.41.0.162.gfafddb0af9-goog
Quoted 10 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().
Using sprintf() seems cleaner to me: it would get rid of the undocumented naked integer. Would that work for you?
Quoted 2 lines
Direct replacement is safe here since the getter in
kernel_params_opshandles -errorno return [3].
s/errorno/errno/
Quoted 23 lines
[1] https://www.kernel.org/doc/html/latest/process/deprecated.html#strlcpy
[2] https://github.com/KSPP/linux/issues/89
[3] https://elixir.bootlin.com/linux/v6.4-rc6/source/include/linux/moduleparam.h#L52Signed-off-by Azeem Shaikh <[email protected]>---
net/sunrpc/svc.c| 8 ++++----1 file changed, 4 insertions(+), 4 deletions(-)net/sunrpc/svc.c +0
-0@@ -109,13 +109,13 @@ param_get_pool_mode(char *buf, const struct kernel_param *kp)switch (*ip) {case
SVC_POOL_AUTO:
- returnstrlcpy(buf, "auto\n", 20);
+ returnstrscpy(buf, "auto\n", 20);
caseSVC_POOL_GLOBAL:
- returnstrlcpy(buf, "global\n", 20);
+ returnstrscpy(buf, "global\n", 20);
caseSVC_POOL_PERCPU:
Quoted 12 lines
- return
strlcpy(buf, "percpu\n", 20);
+ returnstrscpy(buf, "percpu\n", 20);
caseSVC_POOL_PERNODE:
- returnstrlcpy(buf, "pernode\n", 20);
+ returnstrscpy(buf, "pernode\n", 20);
default:
returnsprintf(buf, "%d\n", *ip);
}Signature · 1 line
2.41.0.162.gfafddb0af9-goog
--
Chuck Lever
Quoted 11 lines · Chuck Lever III
Quoted 10 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().
Quoted 2 lines
Using
sprintf()seems cleaner to me: it would get rid of the undocumented naked integer. Would that work for you?
This is changing the "get" routine for reporting module parameters out of /sys. I think the right choice here is sysfs_emit(), as it performs the size tracking correctly. (Even the "default" sprintf() call should be replaced too, IMO.)
Quoted 17 lines
Quoted 4 lines
Direct replacement is safe here since the getter in
kernel_params_opshandles -errorno return [3].s/errorno/errno/
Quoted 9 lines
[1] https://www.kernel.org/doc/html/latest/process/deprecated.html#strlcpy
[2] https://github.com/KSPP/linux/issues/89
[3] https://elixir.bootlin.com/linux/v6.4-rc6/source/include/linux/moduleparam.h#L52Signed-off-by Azeem Shaikh <[email protected]>---
net/sunrpc/svc.c| 8 ++++----1 file changed, 4 insertions(+), 4 deletions(-)
Quoted 10 lines
Quoted 10 lines
net/sunrpc/svc.c +0
-0@@ -109,13 +109,13 @@ param_get_pool_mode(char *buf, const struct kernel_param *kp)switch (*ip) {case
SVC_POOL_AUTO:
- returnstrlcpy(buf, "auto\n", 20);
+ returnstrscpy(buf, "auto\n", 20);
e.g.
return sysfs_emit(buf, "auto\n");
...
Quoted 11 lines
Quoted 11 lines
case
SVC_POOL_GLOBAL:
- returnstrlcpy(buf, "global\n", 20);
+ returnstrscpy(buf, "global\n", 20);
caseSVC_POOL_PERCPU:
- returnstrlcpy(buf, "percpu\n", 20);
+ returnstrscpy(buf, "percpu\n", 20);
caseSVC_POOL_PERNODE:
- returnstrlcpy(buf, "pernode\n", 20);
+ returnstrscpy(buf, "pernode\n", 20);
default:
returnsprintf(buf, "%d\n", *ip);
and:
return sysfs_emit(buf, "%d\n", *ip);
-Kees
Signature · 1 line
Kees Cook
Quoted 16 lines · Kees Cook
Quoted 11 lines · Chuck Lever III
Quoted 10 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().
Quoted 7 lines
Quoted 2 lines
Using
sprintf()seems cleaner to me: it would get rid of the undocumented naked integer. Would that work for you?This is changing the "get" routine for reporting module parameters out of /sys. I think the right choice here is
sysfs_emit(), as it performs the size tracking correctly. (Even the "default"sprintf()call should be replaced too, IMO.)
Agreed, that's even better.
Quoted 42 lines
Quoted 15 lines
Quoted 2 lines
Direct replacement is safe here since the getter in
kernel_params_opshandles -errorno return [3].s/errorno/errno/
Quoted 9 lines
[1] https://www.kernel.org/doc/html/latest/process/deprecated.html#strlcpy
[2] https://github.com/KSPP/linux/issues/89
[3] https://elixir.bootlin.com/linux/v6.4-rc6/source/include/linux/moduleparam.h#L52Signed-off-by Azeem Shaikh <[email protected]>---
net/sunrpc/svc.c| 8 ++++----1 file changed, 4 insertions(+), 4 deletions(-)Quoted 10 lines
Quoted 10 lines
net/sunrpc/svc.c +0
-0@@ -109,13 +109,13 @@ param_get_pool_mode(char *buf, const struct kernel_param *kp)switch (*ip) {case
SVC_POOL_AUTO:
- returnstrlcpy(buf, "auto\n", 20);
+ returnstrscpy(buf, "auto\n", 20);e.g.
returnsysfs_emit(buf, "auto\n");
...Quoted 11 lines
Quoted 11 lines
case
SVC_POOL_GLOBAL:
- returnstrlcpy(buf, "global\n", 20);
+ returnstrscpy(buf, "global\n", 20);
caseSVC_POOL_PERCPU:
- returnstrlcpy(buf, "percpu\n", 20);
+ returnstrscpy(buf, "percpu\n", 20);
caseSVC_POOL_PERNODE:
- returnstrlcpy(buf, "pernode\n", 20);
+ returnstrscpy(buf, "pernode\n", 20);
default:
returnsprintf(buf, "%d\n", *ip);
Quoted 9 lines
and:
return
sysfs_emit(buf, "%d\n", *ip);-Kees
Signature · 1 line
Kees Cook
--
Chuck Lever
Quoted 18 lines · Chuck Lever III
Quoted 18 lines · Kees Cook
Quoted 13 lines · Chuck Lever III
Quoted 10 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().Using
sprintf()seems cleaner to me: it would get rid of the undocumented naked integer. Would that work for you?
Quoted 7 lines
Quoted 4 lines
This is changing the "get" routine for reporting module parameters out of /sys. I think the right choice here is
sysfs_emit(), as it performs the size tracking correctly. (Even the "default"sprintf()call should be replaced too, IMO.)Agreed, that's even better.
Thanks folks. Will send over a v2 which replaces strlcpy with sysfs_emit.