MIPS: Loongson: Fix potentially wrong string handling [Linux 2.6.38]

This Linux kernel change "MIPS: Loongson: Fix potentially wrong string handling" is included in the Linux 2.6.38 release. This change is authored by Stefan Weil <weil [at] mail.berlios.de> on Sun Jan 30 21:41:44 2011 +0100. The commit for this change in Linux stable tree is 994fed2 (patch).

MIPS: Loongson: Fix potentially wrong string handling

This error was reported by cppcheck:
arch/mips/loongson/common/machtype.c:56: error: Dangerous usage of 'str' (strncpy doesn't always 0-terminate it)

If strncpy copied MACHTYPE_LEN bytes, the destination string str
was not terminated.

The patch adds one more byte to str and makes sure that this byte is
always 0.

Signed-off-by: Stefan Weil <[email protected]>
Cc: Wu Zhangjin <[email protected]>
Cc: Arnaud Patard <[email protected]>
Cc: [email protected]
Cc: [email protected]
Patchwork: https://patchwork.linux-mips.org/patch/2053/
Signed-off-by: Ralf Baechle <[email protected]>

There are 3 lines of Linux source code added/deleted in this change. Code changes to Linux kernel are as follows.

 arch/mips/loongson/common/machtype.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/arch/mips/loongson/common/machtype.c b/arch/mips/loongson/common/machtype.c
index 81fbe6b..2efd5d9 100644
--- a/arch/mips/loongson/common/machtype.c
+++ b/arch/mips/loongson/common/machtype.c
@@ -41,7 +41,7 @@ void __weak __init mach_prom_init_machtype(void)

 void __init prom_init_machtype(void)
 {
-   char *p, str[MACHTYPE_LEN];
+   char *p, str[MACHTYPE_LEN + 1];
    int machtype = MACH_LEMOTE_FL2E;

    mips_machtype = LOONGSON_MACHTYPE;
@@ -53,6 +53,7 @@ void __init prom_init_machtype(void)
    }
    p += strlen("machtype=");
    strncpy(str, p, MACHTYPE_LEN);
+   str[MACHTYPE_LEN] = '';
    p = strstr(str, " ");
    if (p)
        *p = '';

Leave a Reply

Your email address will not be published. Required fields are marked *