diff mbox series

[v2] vt: Replace strlcpy with strscpy

Message ID 20230919192156.121503-1-azeems@google.com (mailing list archive)
State Mainlined
Commit 95e8e7eebab58ec8f6c978242a6d143ba238a69e
Headers show
Series [v2] vt: Replace strlcpy with strscpy | expand

Commit Message

Azeem Shaikh Sept. 19, 2023, 7:21 p.m. UTC
strlcpy() reads the entire source buffer first and returns the size of
the source string, not the destination string, which can be accidentally
misused [1].

The copy_to_user() call uses @len returned from strlcpy() directly
without checking its value. This could potentially lead to read
overflow. There is no existing bug since @len is always guaranteed to be
greater than hardcoded strings in @func_table[kb_func]. But as written
it is very fragile and specifically uses a strlcpy() result without sanity
checking and using it to copy to userspace.

In an effort to remove strlcpy() completely [2], replace
strlcpy() here with strscpy().

[1] https://www.kernel.org/doc/html/latest/process/deprecated.html#strlcpy
[2] https://github.com/KSPP/linux/issues/89

Signed-off-by: Azeem Shaikh <azeems@google.com>
---
v2:
 * Return -ENOSPC instead of -EFAULT in case of truncation.
 * Update commit log to clarify that there is no exploitable bug but instead the code uses a fragile anti-pattern.

v1:
 * https://lore.kernel.org/all/20230830160410.3820390-1-azeemshaikh38@gmail.com/

 drivers/tty/vt/keyboard.c |    7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

--
2.42.0.459.ge4e396fd5e-goog

Comments

Justin Stitt Sept. 20, 2023, 3:21 a.m. UTC | #1
On Tue, Sep 19, 2023 at 12:22 PM Azeem Shaikh <azeems@google.com> wrote:
>
> strlcpy() reads the entire source buffer first and returns the size of
> the source string, not the destination string, which can be accidentally
> misused [1].
>
> The copy_to_user() call uses @len returned from strlcpy() directly
> without checking its value. This could potentially lead to read
> overflow. There is no existing bug since @len is always guaranteed to be
> greater than hardcoded strings in @func_table[kb_func]. But as written
> it is very fragile and specifically uses a strlcpy() result without sanity
> checking and using it to copy to userspace.
>
> In an effort to remove strlcpy() completely [2], replace
> strlcpy() here with strscpy().
>
> [1] https://www.kernel.org/doc/html/latest/process/deprecated.html#strlcpy
> [2] https://github.com/KSPP/linux/issues/89
>
> Signed-off-by: Azeem Shaikh <azeems@google.com>
Reviewed-by: Justin Stitt <justinstitt@google.com>

> ---
> v2:
>  * Return -ENOSPC instead of -EFAULT in case of truncation.
>  * Update commit log to clarify that there is no exploitable bug but instead the code uses a fragile anti-pattern.
>
> v1:
>  * https://lore.kernel.org/all/20230830160410.3820390-1-azeemshaikh38@gmail.com/
>
>  drivers/tty/vt/keyboard.c |    7 +++++--
>  1 file changed, 5 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/tty/vt/keyboard.c b/drivers/tty/vt/keyboard.c
> index 1fe6107b539b..12a192e1196b 100644
> --- a/drivers/tty/vt/keyboard.c
> +++ b/drivers/tty/vt/keyboard.c
> @@ -2079,12 +2079,15 @@ int vt_do_kdgkb_ioctl(int cmd, struct kbsentry __user *user_kdgkb, int perm)
>                         return -ENOMEM;
>
>                 spin_lock_irqsave(&func_buf_lock, flags);
> -               len = strlcpy(kbs, func_table[kb_func] ? : "", len);
> +               len = strscpy(kbs, func_table[kb_func] ? : "", len);
>                 spin_unlock_irqrestore(&func_buf_lock, flags);
>
> +               if (len < 0) {
> +                       ret = -ENOSPC;
> +                       break;
> +               }
>                 ret = copy_to_user(user_kdgkb->kb_string, kbs, len + 1) ?
>                         -EFAULT : 0;
> -
>                 break;
>         }
>         case KDSKBSENT:
> --
> 2.42.0.459.ge4e396fd5e-goog
>
>

whitespace nitpicks: A newline after the newly added if-statement's
ending curly brace as well as reinstating the removed newline in your
diff might make the code look "better".

Thanks
Justin
Kees Cook Sept. 20, 2023, 3:13 p.m. UTC | #2
On Tue, Sep 19, 2023 at 07:21:56PM +0000, Azeem Shaikh wrote:
> strlcpy() reads the entire source buffer first and returns the size of
> the source string, not the destination string, which can be accidentally
> misused [1].
> 
> The copy_to_user() call uses @len returned from strlcpy() directly
> without checking its value. This could potentially lead to read
> overflow. There is no existing bug since @len is always guaranteed to be
> greater than hardcoded strings in @func_table[kb_func]. But as written
> it is very fragile and specifically uses a strlcpy() result without sanity
> checking and using it to copy to userspace.
> 
> In an effort to remove strlcpy() completely [2], replace
> strlcpy() here with strscpy().
> 
> [1] https://www.kernel.org/doc/html/latest/process/deprecated.html#strlcpy
> [2] https://github.com/KSPP/linux/issues/89
> 
> Signed-off-by: Azeem Shaikh <azeems@google.com>
> ---
> v2:
>  * Return -ENOSPC instead of -EFAULT in case of truncation.
>  * Update commit log to clarify that there is no exploitable bug but instead the code uses a fragile anti-pattern.

Changes look good. Thanks!

Reviewed-by: Kees Cook <keescook@chromium.org>
diff mbox series

Patch

diff --git a/drivers/tty/vt/keyboard.c b/drivers/tty/vt/keyboard.c
index 1fe6107b539b..12a192e1196b 100644
--- a/drivers/tty/vt/keyboard.c
+++ b/drivers/tty/vt/keyboard.c
@@ -2079,12 +2079,15 @@  int vt_do_kdgkb_ioctl(int cmd, struct kbsentry __user *user_kdgkb, int perm)
 			return -ENOMEM;

 		spin_lock_irqsave(&func_buf_lock, flags);
-		len = strlcpy(kbs, func_table[kb_func] ? : "", len);
+		len = strscpy(kbs, func_table[kb_func] ? : "", len);
 		spin_unlock_irqrestore(&func_buf_lock, flags);

+		if (len < 0) {
+			ret = -ENOSPC;
+			break;
+		}
 		ret = copy_to_user(user_kdgkb->kb_string, kbs, len + 1) ?
 			-EFAULT : 0;
-
 		break;
 	}
 	case KDSKBSENT: