Skip to contentSkip to conversation

SUNRPC: Remove strlcpy

PATCH In review Azeem Shaikh 2 participants 3 messages Jun 14, 2023

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 SUNRPC: Remove strlcpy

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 sysfs_emit().

Direct replacement is safe here since the getter in kernel_params_ops handles -errno 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
index e6d4cec61e47..77326f163801 100644
@@ -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 sysfs_emit(buf, "auto\n");
 	case SVC_POOL_GLOBAL:
-		return strlcpy(buf, "global\n", 20);
+		return sysfs_emit(buf, "global\n");
 	case SVC_POOL_PERCPU:
-		return strlcpy(buf, "percpu\n", 20);
+		return sysfs_emit(buf, "percpu\n");
 	case SVC_POOL_PERNODE:
-		return strlcpy(buf, "pernode\n", 20);
+		return sysfs_emit(buf, "pernode\n");
 	default:
 		return sprintf(buf, "%d\n", *ip);
 	}
Signature · 1 line
2.41.0.162.gfafddb0af9-goog
#1 lore raw
Quoted 30 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 remove strlcpy() completely [2], replace strlcpy() here with sysfs_emit().

Direct replacement is safe here since the getter in kernel_params_ops handles -errno 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 +1 -1
index e6d4cec61e47..77326f163801 100644
@@ -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 sysfs_emit(buf, "auto\n");
 	case SVC_POOL_GLOBAL:

net/sunrpc/svc.c param_get_pool_mode(char *buf, const struct kernel_param *kp) · line 120

Quoted 10 lines
net/sunrpc/svc.c +3 -3
-		return strlcpy(buf, "global\n", 20);
+		return sysfs_emit(buf, "global\n");
 	case SVC_POOL_PERCPU:
-		return strlcpy(buf, "percpu\n", 20);
+		return sysfs_emit(buf, "percpu\n");
 	case SVC_POOL_PERNODE:
-		return strlcpy(buf, "pernode\n", 20);
+		return sysfs_emit(buf, "pernode\n");
 	default:
 		return sprintf(buf, "%d\n", *ip);

Please replace the sprintf too (and then the Subject could be "use sysfs_emit" or so).

-Kees

Signature · 1 line
Kees Cook
#2 lore raw
Quoted 36 lines · Kees Cook
Quoted 34 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 remove strlcpy() completely [2], replace strlcpy() here with sysfs_emit().

Direct replacement is safe here since the getter in kernel_params_ops handles -errno 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 +2 -3
index e6d4cec61e47..77326f163801 100644
@@ -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 sysfs_emit(buf, "auto\n");
      case SVC_POOL_GLOBAL:
-              return strlcpy(buf, "global\n", 20);
+              return sysfs_emit(buf, "global\n");
      case SVC_POOL_PERCPU:
-              return strlcpy(buf, "percpu\n", 20);
Quoted 8 lines

net/sunrpc/svc.c param_get_pool_mode(char *buf, const struct kernel_param *kp) · line 120

Quoted 6 lines
net/sunrpc/svc.c +2 -1
+              return sysfs_emit(buf, "percpu\n");
      case SVC_POOL_PERNODE:
-              return strlcpy(buf, "pernode\n", 20);
+              return sysfs_emit(buf, "pernode\n");
      default:
              return sprintf(buf, "%d\n", *ip);

Please replace the sprintf too (and then the Subject could be "use sysfs_emit" or so).

Ah sorry, I missed the "replace default sprintf too" part in the previous thread. Resending.

Quoted 7 lines

-Kees

--
Kees Cook