Skip to contentSkip to conversation

staging/fbtft: Remove all strcpy() uses

PATCH Superseded Len Baker 3 participants 5 messages Jul 18, 2021 – Jul 24, 2021

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Len Baker

author
#0 lore raw

PATCH 1/1 staging/fbtft: Remove all strcpy() uses

strcpy() performs no bounds checking on the destination buffer. This could result in linear overflows beyond the end of the buffer, leading to all kinds of misbehaviors. The safe replacement is strscpy() but in this case it is simpler to add NULL to the first position since we want to empty the string.

This is a previous step in the path to remove the strcpy() function.

---
 drivers/staging/fbtft/fbtft-core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/fbtft/fbtft-core.c +1 -1
index 3723269890d5..b8791806cb20 100644
@@ -1037,7 +1037,7 @@ int fbtft_init_display(struct fbtft_par *par)
 		case -1:
 			i++;
 			/* make debug message */
-			strcpy(msg, "");
+			msg[0] = 0;
 			j = i + 1;
 			while (par->init_sequence[j] >= 0) {
 				sprintf(str, "0x%02X ", par->init_sequence[j]);

--
2.25.1

#1 lore raw
Quoted 8 lines · Len Baker

strcpy() performs no bounds checking on the destination buffer. This could result in linear overflows beyond the end of the buffer, leading to all kinds of misbehaviors. The safe replacement is strscpy() but in this case it is simpler to add NULL to the first position since we want to empty the string.

This is a previous step in the path to remove the strcpy() function.

Any document behind this (something to read on the site(s) more or less affiliated with what is going to happen in the kernel) to read background?

...

drivers/staging/fbtft/fbtft-core.c int fbtft_init_display(struct fbtft_par *par) · line 1040

Quoted 5 lines
                case -1:
                        i++;
                        /* make debug message */
drivers/staging/fbtft/fbtft-core.c +1 -1
-                       strcpy(msg, "");
+                       msg[0] = 0;

Strictly speaking it should be '\0'.

Quoted 3 lines
                        j = i + 1;
                        while (par->init_sequence[j] >= 0) {
                                sprintf(str, "0x%02X ", par->init_sequence[j]);
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw
Quoted 5 lines · Andy Shevchenko
Quoted 4 lines · Len Baker

strcpy() performs no bounds checking on the destination buffer. This could result in linear overflows beyond the end of the buffer, leading to all kinds of misbehaviors. The safe replacement is strscpy() but in this case it is simpler to add NULL to the first position since we want

"NULL" is a pointer value, "NUL" is the character with value zero.

Quoted 14 lines
Quoted 3 lines

to empty the string.

This is a previous step in the path to remove the strcpy() function.

Any document behind this (something to read on the site(s) more or less affiliated with what is going to happen in the kernel) to read background?

...

Quoted 4 lines
                case -1:
                        i++;
                        /* make debug message */

- strcpy(msg, "");

While this strcpy() is provably safe at compile-time, and will probably be replaced by an assignment to zero by the compiler...

Quoted 7 lines
Quoted 1 line

+ msg[0] = 0;

Strictly speaking it should be '\0'.

Quoted 3 lines
                        j = i + 1;
                        while (par->init_sequence[j] >= 0) {
                                sprintf(str, "0x%02X ", par->init_sequence[j]);

... the real danger is the

        strcat(msg, str);

on the next line.
Fortunately this whole debug printing block (including the strcpy) can (and should) be rewritten to just use "%*ph".

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
author
#4 lore raw
In reply to Andy Shevchenko

Hi,

Quoted 8 lines · Andy Shevchenko
Quoted 6 lines · Len Baker

strcpy() performs no bounds checking on the destination buffer. This could result in linear overflows beyond the end of the buffer, leading to all kinds of misbehaviors. The safe replacement is strscpy() but in this case it is simpler to add NULL to the first position since we want to empty the string.

Quoted 5 lines
Quoted 1 line

This is a previous step in the path to remove the strcpy() function.

Any document behind this (something to read on the site(s) more or less affiliated with what is going to happen in the kernel) to read background?

This is a task of the KSPP (kernel self protection project) [1]

[1] https://github.com/KSPP/linux/issues/88

Quoted 10 lines

...

drivers/staging/fbtft/fbtft-core.c int fbtft_init_display(struct fbtft_par *par) · line 1040

Quoted 5 lines
                case -1:
                        i++;
                        /* make debug message */
drivers/staging/fbtft/fbtft-core.c +1 -1
-                       strcpy(msg, "");
+                       msg[0] = 0;

Strictly speaking it should be '\0'.

Ok, understood.

Quoted 9 lines
Quoted 4 lines
                        j = i + 1;
                        while (par->init_sequence[j] >= 0) {
                                sprintf(str, "0x%02X ", par->init_sequence[j]);

--
With Best Regards,
Andy Shevchenko

Thanks for the feedback,
Len

author
#3 lore raw
In reply to Geert Uytterhoeven
Quoted 9 lines · Geert Uytterhoeven
Quoted 5 lines · Andy Shevchenko
Quoted 4 lines · Len Baker

strcpy() performs no bounds checking on the destination buffer. This could result in linear overflows beyond the end of the buffer, leading to all kinds of misbehaviors. The safe replacement is strscpy() but in this case it is simpler to add NULL to the first position since we want

"NULL" is a pointer value, "NUL" is the character with value zero.

Ok, understood. Thanks.

Quoted 27 lines
Quoted 15 lines
Quoted 4 lines

to empty the string.

This is a previous step in the path to remove the strcpy() function.

Any document behind this (something to read on the site(s) more or less affiliated with what is going to happen in the kernel) to read background?

...

Quoted 4 lines
                case -1:
                        i++;
                        /* make debug message */

- strcpy(msg, "");

While this strcpy() is provably safe at compile-time, and will probably be replaced by an assignment to zero by the compiler...

Quoted 7 lines
Quoted 1 line

+ msg[0] = 0;

Strictly speaking it should be '\0'.

Quoted 3 lines
                        j = i + 1;
                        while (par->init_sequence[j] >= 0) {
                                sprintf(str, "0x%02X ", par->init_sequence[j]);
Quoted 7 lines

... the real danger is the

        strcat(msg, str);

on the next line.
Fortunately this whole debug printing block (including the strcpy) can (and should) be rewritten to just use "%*ph".

Ok, I will work on it and I will send a v2 for review. Thanks for the feedback.

Quoted 11 lines

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

Regards,
Len