lib/string: avoid reading beyond src buffer in strscpy
Subsystems (2)
PATCH 1/1 lib/string: avoid reading beyond src buffer in strscpy
strscpy() tries to copy sizeof(unsigned long) bytes a time from src to dest when possible, and stops the loop when 'max' is less than sizeof(unsigned long). But it doesn't check if (src+res) goes beyond src buffer and does out-of-bound access to the underlying memory.
KASAN reported global-out-of-bound bug when reading seccomp actions_logged file in procfs:
cat /proc/sys/kernel/seccomp/actions_logged
Because seccomp_names_from_actions_logged() is copying short strings (less than sizeof(unsigned long)) to buffer 'names'. e.g.
ret = strscpy(names, " ", size);
Fixed by capping the 'max' value according to the src buffer size, to make sure we won't go beyond src buffer.
--- lib/string.c | 5 +++++ 1 file changed, 5 insertions(+)
lib/string.c +5 -0
@@ -179,6 +179,7 @@ ssize_t strscpy(char *dest, const char *src, size_t count) {const struct word_at_a_time constants =WORD_AT_A_TIME_CONSTANTS ;size_t max = count; +size_t src_sz =strlen (src) +1 ;long res =0 ;if (count ==0 )
@@ -200,6 +201,10 @@ ssize_t strscpy(char *dest, const char *src, size_t count) max =0 ;#endif +/* avoid reading beyond src buffer */ +if (max > src_sz) + max = src_sz; +while (max >=sizeof (unsigned long )) {unsigned long c, data;
Signature · 1 line
2.14.3
Quoted 8 lines · Eryu Guan
strscpy()tries to copy sizeof(unsigned long) bytes a time from src to dest when possible, and stops the loop when 'max' is less than sizeof(unsigned long). But it doesn't check if (src+res) goes beyond src buffer and does out-of-bound access to the underlying memory.KASAN reported global-out-of-bound bug when reading seccomp
actions_loggedfile in procfs:
Quoted 6 lines
cat
/proc/sys/kernel/seccomp/actions_loggedBecause
seccomp_names_from_actions_logged()is copying short strings (less than sizeof(unsigned long)) to buffer 'names'. e.g.ret = strscpy(names, " ", size);
This is a false positive:
https://marc.info/?l=linux-kernel&m=150768944030805&w=2
Given that we keep getting these reports (this is the third), I wonder if can adjust the seccomp code to work around the bug in KASAN...
Quoted 16 lines
Fixed by capping the 'max' value according to the src buffer size, to make sure we won't go beyond src buffer.
Cc Andrew Morton <[email protected]>Cc Chris Metcalf <[email protected]>Cc Kees Cook <[email protected]>Signed-off-by Eryu Guan <[email protected]>--- lib/string.c | 5 +++++ 1 file changed, 5 insertions(+)lib/string.c +0
-0
lib/string.c ssize_t strscpy(char *dest, const char *src, size_t count) · line 182
Quoted 4 lines
lib/string.c +1
-0@@ -179,6 +179,7 @@ ssize_t strscpy(char *dest, const char *src, size_t count) {const struct word_at_a_time constants =WORD_AT_A_TIME_CONSTANTS ;size_t max = count; +size_t src_sz =strlen (src) +1 ;
NAK. The whole point of strscpy is to avoid over-reading the source (see the comments above the function):
* Preferred to strlcpy() since the API doesn't require reading memory
* from the src string beyond the specified "count" bytes, and since
* the return value is easier to error-check than strlcpy()'s.
-Kees
Signature · 2 lines
Kees Cook Pixel Security
Quoted 14 lines · Kees Cook
Quoted 13 lines · Eryu Guan
strscpy()tries to copy sizeof(unsigned long) bytes a time from src to dest when possible, and stops the loop when 'max' is less than sizeof(unsigned long). But it doesn't check if (src+res) goes beyond src buffer and does out-of-bound access to the underlying memory.KASAN reported global-out-of-bound bug when reading seccomp
actions_loggedfile in procfs:cat
/proc/sys/kernel/seccomp/actions_loggedBecause
seccomp_names_from_actions_logged()is copying short strings (less than sizeof(unsigned long)) to buffer 'names'. e.g.
Quoted 8 lines
Quoted 1 line
ret = strscpy(names, " ", size);This is a false positive:
https://marc.info/?l=linux-kernel&m=150768944030805&w=2Given that we keep getting these reports (this is the third), I wonder if can adjust the seccomp code to work around the bug in KASAN...
We should fix this in strscpy() somehow. We just need to decide how to do this. Last time we haven't came to agreement.
So, possible solutions are:
1) Simply disable word-at-a-time optimization in strscpy(). I seriously doubt that this optimization have noticeable performance impact in real workloads.
2) Apply patch https://lkml.kernel.org/r/[email protected]
It's a simple, minimally intrusive way to fix the bug.
However, the patch changes semantics/implementation of the strscpy() which is bad idea.
It basically means that we use one implementation of the strscpy() but test with KASAN another implementation. For that reason patch wasn't liked by Linus.
3) Introduce read_once_partial_check() function and use it in strscpy(). read_once_partial_check() supposed to read one word, but KASAN will check only the first byte of the access. Dmitry didn't like this. I'm also not fan of this solution, because we may not notice some real bugs, e.g.:
char dst[8]; char *src; src = kmalloc(5, GFP_KERNEL); memset(src, 0xff, 5); strscpy(dst, src, 8);
stscpy() will copy from 6 up to 8 bytes (exact size depends on what is stored in src[5-7]) from non-null terminated src. With read_once_partial_check() used in strscpy() KASAN will not report such bug.
So, what it's gonna be? Let's finally decide something and deal with the problem. My vote belongs to 1) or 2).
Quoted 49 lines · Andrey Ryabinin
Quoted 14 lines · Kees Cook
Quoted 13 lines · Eryu Guan
strscpy()tries to copy sizeof(unsigned long) bytes a time from src to dest when possible, and stops the loop when 'max' is less than sizeof(unsigned long). But it doesn't check if (src+res) goes beyond src buffer and does out-of-bound access to the underlying memory.KASAN reported global-out-of-bound bug when reading seccomp
actions_loggedfile in procfs:cat
/proc/sys/kernel/seccomp/actions_loggedBecause
seccomp_names_from_actions_logged()is copying short strings (less than sizeof(unsigned long)) to buffer 'names'. e.g.Quoted 8 lines
Quoted 1 line
ret = strscpy(names, " ", size);This is a false positive:
https://marc.info/?l=linux-kernel&m=150768944030805&w=2Given that we keep getting these reports (this is the third), I wonder if can adjust the seccomp code to work around the bug in KASAN...
We should fix this in
strscpy()somehow. We just need to decide how to do this. Last time we haven't came to agreement.So, possible solutions are:
1) Simply disable word-at-a-time optimization in
strscpy(). I seriously doubt that this optimization have noticeable performance impact in real workloads.2) Apply patch https://lkml.kernel.org/r/[email protected]
It's a simple, minimally intrusive way to fix the bug.
However, the patch changes semantics/implementation of thestrscpy()which is bad idea.
It basically means that we use one implementation of thestrscpy()but test with KASAN another implementation. For that reason patch wasn't liked by Linus.3) Introduce
read_once_partial_check()function and use it instrscpy().read_once_partial_check()supposed to read one word, but KASAN will check only the first byte of the access. Dmitry didn't like this. I'm also not fan of this solution, because we may not notice some real bugs, e.g.:char dst[8]; char *src; src = kmalloc(5, GFP_KERNEL); memset(src, 0xff, 5); strscpy(dst, src, 8);
Quoted 7 lines
stscpy()will copy from 6 up to 8 bytes (exact size depends on what is stored in src[5-7]) from non-null terminated src. Withread_once_partial_check()used instrscpy()KASAN will not report such bug.So, what it's gonna be? Let's finally decide something and deal with the problem. My vote belongs to 1) or 2).
My vote is for 1) if we agree that the optimization is not worth it, otherwise for 2).
Quoted 55 lines · Dmitry Vyukov
Quoted 54 lines · Andrey Ryabinin
Quoted 14 lines · Kees Cook
Quoted 13 lines · Eryu Guan
strscpy()tries to copy sizeof(unsigned long) bytes a time from src to dest when possible, and stops the loop when 'max' is less than sizeof(unsigned long). But it doesn't check if (src+res) goes beyond src buffer and does out-of-bound access to the underlying memory.KASAN reported global-out-of-bound bug when reading seccomp
actions_loggedfile in procfs:cat
/proc/sys/kernel/seccomp/actions_loggedBecause
seccomp_names_from_actions_logged()is copying short strings (less than sizeof(unsigned long)) to buffer 'names'. e.g.Quoted 8 lines
Quoted 1 line
ret = strscpy(names, " ", size);This is a false positive:
https://marc.info/?l=linux-kernel&m=150768944030805&w=2Given that we keep getting these reports (this is the third), I wonder if can adjust the seccomp code to work around the bug in KASAN...
We should fix this in
strscpy()somehow. We just need to decide how to do this. Last time we haven't came to agreement.So, possible solutions are:
1) Simply disable word-at-a-time optimization in
strscpy(). I seriously doubt that this optimization have noticeable performance impact in real workloads.2) Apply patch https://lkml.kernel.org/r/[email protected]
It's a simple, minimally intrusive way to fix the bug.
However, the patch changes semantics/implementation of thestrscpy()which is bad idea.
It basically means that we use one implementation of thestrscpy()but test with KASAN another implementation. For that reason patch wasn't liked by Linus.3) Introduce
read_once_partial_check()function and use it instrscpy().read_once_partial_check()supposed to read one word, but KASAN will check only the first byte of the access. Dmitry didn't like this. I'm also not fan of this solution, because we may not notice some real bugs, e.g.:char dst[8]; char *src; src = kmalloc(5, GFP_KERNEL); memset(src, 0xff, 5); strscpy(dst, src, 8);
stscpy()will copy from 6 up to 8 bytes (exact size depends on what is stored in src[5-7]) from non-null terminated src. Withread_once_partial_check()used instrscpy()KASAN will not report such bug.
Quoted 6 lines
Quoted 2 lines
So, what it's gonna be? Let's finally decide something and deal with the problem. My vote belongs to 1) or 2).
My vote is for 1) if we agree that the optimization is not worth it, otherwise for 2).
Who would be the best person to measure the difference for 1)?
-Kees
Signature · 2 lines
Kees Cook Pixel Security
Quoted 30 lines · Kees Cook
Quoted 29 lines · Dmitry Vyukov
Quoted 28 lines · Andrey Ryabinin
So, possible solutions are:
1) Simply disable word-at-a-time optimization in
strscpy(). I seriously doubt that this optimization have noticeable performance impact in real workloads.2) Apply patch https://lkml.kernel.org/r/[email protected]
It's a simple, minimally intrusive way to fix the bug.
However, the patch changes semantics/implementation of thestrscpy()which is bad idea.
It basically means that we use one implementation of thestrscpy()but test with KASAN another implementation. For that reason patch wasn't liked by Linus.3) Introduce
read_once_partial_check()function and use it instrscpy().read_once_partial_check()supposed to read one word, but KASAN will check only the first byte of the access. Dmitry didn't like this. I'm also not fan of this solution, because we may not notice some real bugs, e.g.:char dst[8]; char *src; src = kmalloc(5, GFP_KERNEL); memset(src, 0xff, 5); strscpy(dst, src, 8);
stscpy()will copy from 6 up to 8 bytes (exact size depends on what is stored in src[5-7]) from non-null terminated src. Withread_once_partial_check()used instrscpy()KASAN will not report such bug.
Quoted 9 lines
Quoted 6 lines
Quoted 2 lines
So, what it's gonna be? Let's finally decide something and deal with the problem. My vote belongs to 1) or 2).
My vote is for 1) if we agree that the optimization is not worth it, otherwise for 2).
Who would be the best person to measure the difference for 1)?
I suppose that depends on which one strscpy() caller you'd want to test. Briefly looking at all current users, it doesn't look like they process huge amounts of data through strscpy(), thus we shouldn't suffer from a slight performance degradation of strscpy().
From: Andrey Ryabinin
Quoted 1 line
Sent: 11 December 2017 16:44
...
Quoted 4 lines
I suppose that depends on which one
strscpy()caller you'd want to test. Briefly looking at all current users, it doesn't look like they process huge amounts of data throughstrscpy(), thus we shouldn't suffer from a slight performance degradation ofstrscpy().
Don't most of the fast string functions use the same kind of optimisations.strlen() is very likely to do 64 bit reads and then shifts (etc) to determine whether any of the bytes are zero.
David
Quoted 8 lines · David Laight
From: Andrey Ryabinin
Quoted 1 line
Sent: 11 December 2017 16:44
...
Quoted 4 lines
I suppose that depends on which one
strscpy()caller you'd want to test. Briefly looking at all current users, it doesn't look like they process huge amounts of data throughstrscpy(), thus we shouldn't suffer from a slight performance degradation ofstrscpy().
Quoted 5 lines
Don't most of the fast string functions use the same kind of optimisations.
strlen()is very likely to do 64 bit reads and then shifts (etc) to determine whether any of the bytes are zero.
See for yourself, strscpy() is the only sting function doing this.
Quoted 2 lines
David
Quoted 2 lines · Andrey Ryabinin
See for yourself,
strscpy()is the only sting function doing this.
No, strnlen_user() definitely does too.
It's just that KASAN doesn't track user pointers.
And the important strlen() in the kernel is the pathname hashing code, which *definitely* accesses outside the source, but since it can actually traverse to another page we have that one annotated too (with load_unaligned_zeropad()).
So no, strscpy() isn't the only one doing it, it is just the only one that KASAN catches.
Linus