vt: Fix potential read overflow of kernel memory
PATCH 1/1 vt: Fix potential read overflow of kernel memory
strlcpy() reads the entire source buffer first. This read may exceed the destination size limit if a source string is not NUL-terminated [1].
The copy_to_user() call uses @len returned from strlcpy() directly without checking its value. This could potentially lead to read overflow.
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
--- drivers/tty/vt/keyboard.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/tty/vt/keyboard.c +5 -2
@@ -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 = -EFAULT ; +break ; + } ret =copy_to_user (user_kdgkb->kb_string, kbs, len +1 ) ? -EFAULT :0 ; -break ; }case KDSKBSENT :
--
2.42.0.rc2.253.gd59a3bf2b4-goog
Greg Kroah-Hartman
maintainerQuoted 3 lines · Azeem Shaikh
strlcpy()reads the entire source buffer first. This read may exceed the destination size limit if a source string is not NUL-terminated [1].
But that's not the case here, right? So your "potential read overflow" isn't relevant here.
Quoted 3 lines
The
copy_to_user()call uses @len returned fromstrlcpy()directly without checking its value. This could potentially lead to read overflow.
But can it? How?
Those are all hard-coded strings, in the kernel source, there is no potential overflow here.
And you know the buffer size is correct as well.
So why even check?
Quoted 17 lines
In an effort to remove
strlcpy()completely [2], replacestrlcpy()here withstrscpy().[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]>--- drivers/tty/vt/keyboard.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)drivers/tty/vt/keyboard.c +0
-0@@ -2079,12 +2079,15 @@ int vt_do_kdgkb_ioctl(int cmd, struct kbsentry __user *user_kdgkb, int perm)return -ENOMEM ;
drivers/tty/vt/keyboard.c int vt_do_kdgkb_ioctl(int cmd, struct kbsentry __user *user_kdgkb, int perm) · line 2088
Quoted 10 lines
drivers/tty/vt/keyboard.c +5
-1spin_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 = -EFAULT ; +break ; + }
Don't check for impossible things please.
thanks,
greg k-h
Quoted 6 lines · Greg Kroah-Hartman
Quoted 3 lines · Azeem Shaikh
strlcpy()reads the entire source buffer first. This read may exceed the destination size limit if a source string is not NUL-terminated [1].
Quoted 9 lines
But that's not the case here, right? So your "potential read overflow" isn't relevant here.
Quoted 3 lines
The
copy_to_user()call uses @len returned fromstrlcpy()directly without checking its value. This could potentially lead to read overflow.But can it? How?
The case I was considering is when the null-terminated hardcoded string @func_table[kb_func] has length @new_len > @len. In this case, strlcpy() will assign @len = @new_len and copy_to_user() would read @new_len from the kmalloc-ed memory of @len. This is the potential read overflow I was referring to. Let me know if I'm mistaken.
Quoted 15 lines · Azeem Shaikh
Quoted 13 lines · Greg Kroah-Hartman
Quoted 3 lines · Azeem Shaikh
strlcpy()reads the entire source buffer first. This read may exceed the destination size limit if a source string is not NUL-terminated [1].But that's not the case here, right? So your "potential read overflow" isn't relevant here.
Quoted 3 lines
The
copy_to_user()call uses @len returned fromstrlcpy()directly without checking its value. This could potentially lead to read overflow.
Quoted 8 lines
Quoted 2 lines
But can it? How?
The case I was considering is when the null-terminated hardcoded string @func_table[kb_func] has length @new_len > @len. In this case,
strlcpy()will assign @len = @new_len andcopy_to_user()would read @new_len from the kmalloc-ed memory of @len. This is the potential read overflow I was referring to. Let me know if I'm mistaken.
First there is:
ssize_t len = sizeof(user_kdgkb->kb_string);
"struct user_kdgkb" is UAPI (therefore unlikely to change), and kb_string is 512:
struct kbsentry {
unsigned char kb_func;
unsigned char kb_string[512];
};Then we do:
len = strlcpy(kbs, func_table[kb_func] ? : "", len);
This is the anti-pattern (take the length of the _source_) we need to remove. However, func_table[] is made up of either %NUL-terminated strings:
char func_buf[] = {
'\033', '[', '[', 'A', 0,
'\033', '[', '[', 'B', 0,
...
char *func_table[MAX_NR_FUNC] = {
func_buf + 0,
func_buf + 5,...
Or a NULL address itself. The ?: operator handles the NULL case, so "len" can only ever be 0 through the longest string in func_buf. So it's what I'd call "accidentally correct". i.e. it's using a fragile anti-pattern, but in this case everything is hard-coded and less than 512.
Regardless, we need to swap for a sane pattern, which you've done. But the commit log is misleading, so it needs some more detail. :)
-Kees
Signature · 1 line
Kees Cook
Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
The
copy_to_user()call uses @len returned fromstrlcpy()directly without checking its value. This could potentially lead to read overflow.Quoted 8 lines
Quoted 2 lines
But can it? How?
The case I was considering is when the null-terminated hardcoded string @func_table[kb_func] has length @new_len > @len. In this case,
strlcpy()will assign @len = @new_len andcopy_to_user()would read @new_len from the kmalloc-ed memory of @len. This is the potential read overflow I was referring to. Let me know if I'm mistaken.
Quoted 7 lines
..it's what I'd call "accidentally correct". i.e. it's using a fragile> anti-pattern, but in this case everything is hard-coded and less than 512.
Regardless, we need to swap for a sane pattern, which you've done. But the commit log is misleading, so it needs some more detail. :)
-Kees
In my opinion strlcpy() is being used correctly here as a defensive precaution. If the source string is larger than the destination buffer it will truncate rather than corrupt kernel memory. However the return value of strlcpy() is being misused. If truncation occurred the copy_to_user() call will corrupt user memory instead.
I also agree that this is not currently a bug. It is fragile and it could break if someone added a very large string to the table.
Why not fix this by avoiding the redundant string copy? How about something like this:
ptr = func_table[kb_func] ? : "";
len = strlen(ptr);
if (len >= sizeof(user_kdgkb->kb_string))
return -ENOSPC;
if (copy_to_user(user_kdgkb->kb_string, ptr, len + 1))
return -EFAULT;
Quoted 12 lines · Dan Raymond
In my opinion
strlcpy()is being used correctly here as a defensive precaution. If the source string is larger than the destination buffer it will truncate rather than corrupt kernel memory. However the return value ofstrlcpy()is being misused. If truncation occurred thecopy_to_user()call will corrupt user memory instead.I also agree that this is not currently a bug. It is fragile and it could break if someone added a very large string to the table.
Why not fix this by avoiding the redundant string copy? How about something like this:
Quoted 8 lines
ptr = func_table[kb_func] ? : "";
len = strlen(ptr);if (len >=
sizeof(user_kdgkb->kb_string))return -ENOSPC;if (
copy_to_user(user_kdgkb->kb_string, ptr, len + 1))return -EFAULT;
This would work if not for func_buf_lock. The bounce buffer is used to avoid needing to hold the spin lock across copy_to_user.
Signature · 1 line
Kees Cook
Quoted 19 lines · Kees Cook
Warning: This email is from an unusual correspondent.
Warning: Make sure this is someone you trust.Quoted 15 lines · Dan Raymond
In my opinion
strlcpy()is being used correctly here as a defensive precaution. If the source string is larger than the destination buffer it will truncate rather than corrupt kernel memory. However the return value ofstrlcpy()is being misused. If truncation occurred thecopy_to_user()call will corrupt user memory instead.I also agree that this is not currently a bug. It is fragile and it could break if someone added a very large string to the table.
Why not fix this by avoiding the redundant string copy? How about something like this:
ptr = func_table[kb_func] ? : "";
len = strlen(ptr);
Quoted 9 lines
Quoted 5 lines
if (len >=
sizeof(user_kdgkb->kb_string))return -ENOSPC;if (
copy_to_user(user_kdgkb->kb_string, ptr, len + 1))return -EFAULT;This would work if not for
func_buf_lock. The bounce buffer is used to avoid needing to hold the spin lock acrosscopy_to_user.
Ah you're right. Thanks for setting me straight. Now I realize that my entire assessment was wrong. The original author was not using strlcpy() as a defensive measure to prevent a buffer overflow. He was using it so that he could create a copy of the string and measure its length using only one pass. This minimizes the time spent holding the spinlock.
The surrounding code was written such that a buffer overflow is impossible. No additional checks are needed. The proposed patch is unnecessary. But at least it preserves the spirit of the original author's code by performing only one pass of the source string while holding the spinlock.
Quoted 37 lines · Dan Raymond
Quoted 19 lines · Kees Cook
Warning: This email is from an unusual correspondent.
Warning: Make sure this is someone you trust.Quoted 15 lines · Dan Raymond
In my opinion
strlcpy()is being used correctly here as a defensive precaution. If the source string is larger than the destination buffer it will truncate rather than corrupt kernel memory. However the return value ofstrlcpy()is being misused. If truncation occurred thecopy_to_user()call will corrupt user memory instead.I also agree that this is not currently a bug. It is fragile and it could break if someone added a very large string to the table.
Why not fix this by avoiding the redundant string copy? How about something like this:
ptr = func_table[kb_func] ? : "";
len = strlen(ptr);Quoted 9 lines
Quoted 5 lines
if (len >=
sizeof(user_kdgkb->kb_string))return -ENOSPC;if (
copy_to_user(user_kdgkb->kb_string, ptr, len + 1))return -EFAULT;This would work if not for
func_buf_lock. The bounce buffer is used to avoid needing to hold the spin lock acrosscopy_to_user.Ah you're right. Thanks for setting me straight. Now I realize that my entire assessment was wrong. The original author was not using
strlcpy()as a defensive measure to prevent a buffer overflow. He was using it so that he could create a copy of the string and measure its length using only one pass. This minimizes the time spent holding the spinlock.
Quoted 5 lines
The surrounding code was written such that a buffer overflow is impossible. No additional checks are needed. The proposed patch is unnecessary. But at least it preserves the spirit of the original author's code by performing only one pass of the source string while holding the spinlock.
Are folks ok with me sending out a v2 for this with a better commit log that explains the issue?
Quoted 2 lines · Azeem Shaikh
Are folks ok with me sending out a v2 for this with a better commit log that explains the issue?
Yes, please do. It should clear up the questions from this thread. :)
Thanks!
-Kees
Signature · 1 line
Kees Cook
Jiri Slaby
maintainerQuoted 37 lines · Kees Cook
Quoted 15 lines · Azeem Shaikh
Quoted 13 lines · Greg Kroah-Hartman
Quoted 3 lines · Azeem Shaikh
strlcpy()reads the entire source buffer first. This read may exceed the destination size limit if a source string is not NUL-terminated [1].But that's not the case here, right? So your "potential read overflow" isn't relevant here.
Quoted 3 lines
The
copy_to_user()call uses @len returned fromstrlcpy()directly without checking its value. This could potentially lead to read overflow.Quoted 8 lines
Quoted 2 lines
But can it? How?
The case I was considering is when the null-terminated hardcoded string @func_table[kb_func] has length @new_len > @len. In this case,
strlcpy()will assign @len = @new_len andcopy_to_user()would read @new_len from the kmalloc-ed memory of @len. This is the potential read overflow I was referring to. Let me know if I'm mistaken.First there is:
ssize_tlen =sizeof(user_kdgkb->kb_string);"struct
user_kdgkb" is UAPI (therefore unlikely to change), andkb_stringis 512:struct kbsentry { unsigned char kb_func; unsigned char kb_string[512]; };
Quoted 6 lines
Then we do:
len = strlcpy(kbs, func_table[kb_func] ? : "", len);This is the anti-pattern (take the length of the _source_) we need to remove.
But len is the length of kbs, i.e. the destination. Or what am I missing?
kbs = kmalloc(len, GFP_KERNEL);
len = strlcpy(kbs, func_table[kb_func] ? : "", len);Quoted 12 lines
However, func_table[] is made up of either %NUL-terminated strings:
char func_buf[] = { '\033', '[', '[', 'A', 0, '\033', '[', '[', 'B', 0, ... char *func_table[MAX_NR_FUNC] = { func_buf + 0, func_buf + 5,...
Quoted 8 lines
Or a NULL address itself. The ?: operator handles the NULL case, so "len" can only ever be 0 through the longest string in
func_buf. So it's what I'd call "accidentally correct". i.e. it's using a fragile anti-pattern, but in this case everything is hard-coded and less than 512.Regardless, we need to swap for a sane pattern, which you've done. But the commit log is misleading, so it needs some more detail. :)
I am still missing what is wrong in the above code with strlcpy(). The dest is deliberately made as long as the source, so the returned len is never less then the passed len. No checking needed IMO. Perhaps, we might switch to strcpy()?
FWIW I introduced this in commit 82e61c3909db5. So if it needs fixing, the patch deserves a Fixes: 82e61c3909db5 tag.
thanks,
Signature · 2 lines
js suse labs
Quoted 72 lines · Jiri Slaby
Quoted 37 lines · Kees Cook
Quoted 15 lines · Azeem Shaikh
Quoted 13 lines · Greg Kroah-Hartman
Quoted 3 lines · Azeem Shaikh
strlcpy()reads the entire source buffer first. This read may exceed the destination size limit if a source string is not NUL-terminated [1].But that's not the case here, right? So your "potential read overflow" isn't relevant here.
Quoted 3 lines
The
copy_to_user()call uses @len returned fromstrlcpy()directly without checking its value. This could potentially lead to read overflow.Quoted 8 lines
Quoted 2 lines
But can it? How?
The case I was considering is when the null-terminated hardcoded string @func_table[kb_func] has length @new_len > @len. In this case,
strlcpy()will assign @len = @new_len andcopy_to_user()would read @new_len from the kmalloc-ed memory of @len. This is the potential read overflow I was referring to. Let me know if I'm mistaken.First there is:
ssize_tlen =sizeof(user_kdgkb->kb_string);"struct
user_kdgkb" is UAPI (therefore unlikely to change), andkb_stringis 512:struct kbsentry { unsigned char kb_func; unsigned char kb_string[512]; };Quoted 6 lines
Then we do:
len = strlcpy(kbs, func_table[kb_func] ? : "", len);This is the anti-pattern (take the length of the _source_) we need to remove.
But len is the length of kbs, i.e. the destination. Or what am I missing?
kbs = kmalloc(len, GFP_KERNEL); len = strlcpy(kbs, func_table[kb_func] ? : "", len);Quoted 12 lines
However, func_table[] is made up of either %NUL-terminated strings:
char func_buf[] = { '\033', '[', '[', 'A', 0, '\033', '[', '[', 'B', 0, ... char *func_table[MAX_NR_FUNC] = { func_buf + 0, func_buf + 5,...
Quoted 8 lines
Or a NULL address itself. The ?: operator handles the NULL case, so "len" can only ever be 0 through the longest string in
func_buf. So it's what I'd call "accidentally correct". i.e. it's using a fragile anti-pattern, but in this case everything is hard-coded and less than 512.Regardless, we need to swap for a sane pattern, which you've done. But the commit log is misleading, so it needs some more detail. :)
Quoted 8 lines
I am still missing what is wrong in the above code with
strlcpy(). The dest is deliberately made as long as the source, so the returned len is never less then the passed len. No checking needed IMO. Perhaps, we might switch tostrcpy()?FWIW I introduced this in commit
82e61c3909db5. So if it needs fixing, the patch deserves a Fixes:82e61c3909db5tag.
As explained by Kees in previous comments, this is not actually a bug, just a fragile anti-pattern. So we shouldn't need the Fixes: tag on this patch.
Quoted 5 lines
thanks,
--
js
suse labs
Quoted 7 lines · Jiri Slaby
Quoted 4 lines · Kees Cook
len = strlcpy(kbs, func_table[kb_func] ? : "", len);This is the anti-pattern (take the length of the _source_) we need to remove.
But len is the length of kbs, i.e. the destination. Or what am I missing?
strlcpy() returns the length of the _source_ string (i.e. it could be greater than the input argument len). But there is no current flaw here (since all sources are very short). We're just trying to remove strlcpy() since it leads to unexpected results.
-Kees
Signature · 1 line
Kees Cook
Quoted 1 line · Azeem Shaikh
strlcpy()reads the entire source buffer first.
Perhaps add:
"... and returns the size of the source string, not the destination string, which can be accidentally misused."
Quoted 6 lines
This read may exceed the destination size limit if a source string is not NUL-terminated [1].
The
copy_to_user()call uses @len returned fromstrlcpy()directly without checking its value. This could potentially lead to read overflow.
Since the code as written today is "accidentally correct", it's worth clarifying this: there is no existing bug, but as written it is very fragile and specifically uses a strlcpy() result without sanity checking and using it to copy to userspace. (This is the exact anti-pattern for strlcpy(), and only because the source strings are known good is this safe.)
Quoted 16 lines
In an effort to remove
strlcpy()completely [2], replacestrlcpy()here withstrscpy().[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]>--- drivers/tty/vt/keyboard.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)drivers/tty/vt/keyboard.c +0
-0
drivers/tty/vt/keyboard.c int vt_do_kdgkb_ioctl(int cmd, struct kbsentry __user *user_kdgkb, int perm) · line 2086
Quoted 9 lines
drivers/tty/vt/keyboard.c +3
-1@@ -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 = -EFAULT ;
I think this should be -ENOSPC as EFAULT implies an actual memory fault.
Quoted 9 lines
drivers/tty/vt/keyboard.c +2
-2+break ; + } ret =copy_to_user (user_kdgkb->kb_string, kbs, len +1 ) ? -EFAULT :0 ; -break ; }case KDSKBSENT : --
Quoted 3 lines
2.42.0.rc2.253.gd59a3bf2b4-goog
Thanks for sticking with these refactorings; we're almost free from strlcpy. :)
-Kees
Signature · 1 line
Kees Cook