kdb: Replace deprecated strcpy() with strscpy() in vkdb_printf()
PATCH 1/1 kdb: Replace deprecated strcpy() with strscpy() in vkdb_printf()
strcpy() is deprecated; use strscpy() instead.
Use the return value of strscpy() instead of calling strlen() again.
--- kernel/debug/kdb/kdb_io.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
kernel/debug/kdb/kdb_io.c +2 -4
@@ -732,8 +732,7 @@ int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap)* Shift the buffer left. */ *cphold = replaced_byte; -strcpy (kdb_buffer, cphold); - len =strlen (kdb_buffer); + len =strscpy (kdb_buffer, cphold); next_avail = kdb_buffer + len; size_avail =sizeof (kdb_buffer) - len; goto kdb_print_out;
@@ -872,8 +871,7 @@ int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap)*/ if (kdb_grepping_flag && !suspend_grep) { *cphold = replaced_byte; -strcpy (kdb_buffer, cphold); - len =strlen (kdb_buffer); + len =strscpy (kdb_buffer, cphold); next_avail = kdb_buffer + len; size_avail =sizeof (kdb_buffer) - len; }
Signature · 1 line
2.50.1
Doug Anderson
reviewerHi,
Quoted 16 lines · Thorsten Blum
strcpy()is deprecated; usestrscpy()instead.Use the return value of
strscpy()instead of callingstrlen()again.Signed-off-by Thorsten Blum <[email protected]>--- kernel/debug/kdb/kdb_io.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)kernel/debug/kdb/kdb_io.c +0
-0
kernel/debug/kdb/kdb_io.c int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap) · line 738
Quoted 9 lines
kernel/debug/kdb/kdb_io.c +1
-2@@ -732,8 +732,7 @@ int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap)* Shift the buffer left. */ *cphold = replaced_byte; -strcpy (kdb_buffer, cphold); - len =strlen (kdb_buffer); + len =strscpy (kdb_buffer, cphold); next_avail = kdb_buffer + len; size_avail =sizeof (kdb_buffer) - len; goto kdb_print_out;
It made me a little nervous that you're not checking for the fact that strscpy() could return an error code. Without the check you're just replacing one type of problem (buffer overflow) with another type (the code running with a negative length). IMO in cases like this either leave the strlen() in there or check the return value for errors.
...so I looked a little deeper here to see if the buffer overflow was actually possible to begin with. Looking, I _think_ this is true:
* cp is a pointer into kdb_buffer (location of first '\n')
* cphold and cp are equal at this point.
...so you're guaranteed not to overflow because the destination and source overlap. ...but that means we shouldn't have been using strcpy() either way. Both strcpy() and strscpy() say that their behaviors are undefined if the src/dest overlap. This means that really the right fix is to use memmove().
The above is based solely on code inspection w/ no testing. If I got it wrong, let me know.
kernel/debug/kdb/kdb_io.c int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap) · line 876
Quoted 9 lines
kernel/debug/kdb/kdb_io.c +1
-2@@ -872,8 +871,7 @@ int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap)*/ if (kdb_grepping_flag && !suspend_grep) { *cphold = replaced_byte; -strcpy (kdb_buffer, cphold); - len =strlen (kdb_buffer); + len =strscpy (kdb_buffer, cphold); next_avail = kdb_buffer + len; size_avail =sizeof (kdb_buffer) - len;
I believe the above case is similar.
-Doug
Hi Doug,
Quoted 10 lines · Doug Anderson
Quoted 8 lines · Thorsten Blum
strcpy()is deprecated; usestrscpy()instead.Use the return value of
strscpy()instead of callingstrlen()again.Signed-off-by Thorsten Blum <[email protected]>---
Quoted 5 lines
It made me a little nervous that you're not checking for the fact that
strscpy()could return an error code. Without the check you're just replacing one type of problem (buffer overflow) with another type (the code running with a negative length). IMO in cases like this either leave thestrlen()in there or check the return value for errors.
Yes, I should have checked the return value or left strlen() as is. This was actually a last minute "improvement" I should have skipped.
Quoted 11 lines
...so I looked a little deeper here to see if the buffer overflow was actually possible to begin with. Looking, I _think_ this is true:
*
cpis a pointer intokdb_buffer(location of first '\n')
*cpholdandcpare equal at this point....so you're guaranteed not to overflow because the destination and source overlap. ...but that means we shouldn't have been using
strcpy()either way. Bothstrcpy()andstrscpy()say that their behaviors are undefined if the src/dest overlap. This means that really the right fix is to usememmove().
Good catch. I read about the undefined behavior in the function comment, but never encountered it and haven't really been looking out for it.
Quoted 2 lines
The above is based solely on code inspection w/ no testing. If I got it wrong, let me know.
Yes, I just compile-tested it as I didn't expect src/dst to overlap. And then my last-minute change to strlen() made it even worse. Sorry about that.
Are you going to fix it using memmove() or should I submit a v2?
Thanks,
Thorsten
Doug Anderson
reviewerHi,
Quoted 16 lines · Thorsten Blum
Quoted 4 lines
...so I looked a little deeper here to see if the buffer overflow was actually possible to begin with. Looking, I _think_ this is true:
Quoted 8 lines
*
cpis a pointer intokdb_buffer(location of first '\n')
*cpholdandcpare equal at this point....so you're guaranteed not to overflow because the destination and source overlap. ...but that means we shouldn't have been using
strcpy()either way. Bothstrcpy()andstrscpy()say that their behaviors are undefined if the src/dest overlap. This means that really the right fix is to usememmove().Good catch. I read about the undefined behavior in the function comment, but never encountered it and haven't really been looking out for it.
Quoted 8 lines
Quoted 2 lines
The above is based solely on code inspection w/ no testing. If I got it wrong, let me know.
Yes, I just compile-tested it as I didn't expect src/dst to overlap. And then my last-minute change to
strlen()made it even worse. Sorry about that.Are you going to fix it using
memmove()or should I submit a v2?
Do you want to send a v2 that switches it to memmove()?
-Doug
Quoted 1 line · Doug Anderson
Do you want to send a v2 that switches it to
memmove()?
Yes, I might just need a day or two.
Thanks,
Thorsten