diff mbox series

[v2] SUNRPC: Remove strlcpy

Message ID 20230614001246.538643-1-azeemshaikh38@gmail.com (mailing list archive)
State Not Applicable
Delegated to: Netdev Maintainers
Headers show
Series [v2] SUNRPC: Remove strlcpy | expand

Checks

Context Check Description
netdev/series_format warning Single patches do not need cover letters; Target tree name not specified in the subject
netdev/tree_selection success Guessed tree name to be net-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 10 this patch: 10
netdev/cc_maintainers success CCed 10 of 10 maintainers
netdev/build_clang success Errors and warnings before: 10 this patch: 10
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 10 this patch: 10
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 17 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Azeem Shaikh June 14, 2023, 12:12 a.m. UTC
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

Signed-off-by: Azeem Shaikh <azeemshaikh38@gmail.com>
---
 net/sunrpc/svc.c |    8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

Comments

Kees Cook June 14, 2023, 1:04 a.m. UTC | #1
On June 13, 2023 5:12:46 PM PDT, Azeem Shaikh <azeemshaikh38@gmail.com> wrote:
>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
>
>Signed-off-by: Azeem Shaikh <azeemshaikh38@gmail.com>
>---
> net/sunrpc/svc.c |    8 ++++----
> 1 file changed, 4 insertions(+), 4 deletions(-)
>
>diff --git a/net/sunrpc/svc.c b/net/sunrpc/svc.c
>index e6d4cec61e47..77326f163801 100644
>--- a/net/sunrpc/svc.c
>+++ b/net/sunrpc/svc.c
>@@ -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);

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

-Kees
Azeem Shaikh June 14, 2023, 1:10 a.m. UTC | #2
On Tue, Jun 13, 2023 at 9:04 PM Kees Cook <kees@kernel.org> wrote:
>
> On June 13, 2023 5:12:46 PM PDT, Azeem Shaikh <azeemshaikh38@gmail.com> wrote:
> >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
> >
> >Signed-off-by: Azeem Shaikh <azeemshaikh38@gmail.com>
> >---
> > net/sunrpc/svc.c |    8 ++++----
> > 1 file changed, 4 insertions(+), 4 deletions(-)
> >
> >diff --git a/net/sunrpc/svc.c b/net/sunrpc/svc.c
> >index e6d4cec61e47..77326f163801 100644
> >--- a/net/sunrpc/svc.c
> >+++ b/net/sunrpc/svc.c
> >@@ -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);
>
> 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.

>
> -Kees
>
>
>
> --
> Kees Cook
diff mbox series

Patch

diff --git a/net/sunrpc/svc.c b/net/sunrpc/svc.c
index e6d4cec61e47..77326f163801 100644
--- a/net/sunrpc/svc.c
+++ b/net/sunrpc/svc.c
@@ -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);
 	}