From: Rosen Penev Date: Sat, 20 Jun 2026 20:07:38 +0000 (-0700) Subject: kernel: replace strncpy with safer alternatives X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=35ee4f8d31199095b9e62046adf52fd08e368ecb;p=thirdparty%2Fopenwrt.git kernel: replace strncpy with safer alternatives strncpy is deprecated. Replace with: - strscpy() for NUL-terminated destinations - strscpy_pad() for NUL-terminated destinations with zero-padding - memcpy() for fixed-length in-place overwrites (bootargs mangle/cmdline replacement) Assisted-by: Opencode:Big-Pickle Signed-off-by: Rosen Penev Link: https://github.com/openwrt/openwrt/pull/23892 Signed-off-by: Jonas Jelonek --- diff --git a/target/linux/generic/files/drivers/net/phy/rtl8306.c b/target/linux/generic/files/drivers/net/phy/rtl8306.c index 5ddb874bf85..b91c493689c 100644 --- a/target/linux/generic/files/drivers/net/phy/rtl8306.c +++ b/target/linux/generic/files/drivers/net/phy/rtl8306.c @@ -898,19 +898,19 @@ rtl8306_config_init(struct phy_device *pdev) switch(chiptype) { case 0: case 2: - strncpy(priv->hwname, RTL_NAME_S, sizeof(priv->hwname)); + strscpy(priv->hwname, RTL_NAME_S, sizeof(priv->hwname)); priv->type = RTL_TYPE_S; break; case 1: - strncpy(priv->hwname, RTL_NAME_SD, sizeof(priv->hwname)); + strscpy(priv->hwname, RTL_NAME_SD, sizeof(priv->hwname)); priv->type = RTL_TYPE_SD; break; case 3: - strncpy(priv->hwname, RTL_NAME_SDM, sizeof(priv->hwname)); + strscpy(priv->hwname, RTL_NAME_SDM, sizeof(priv->hwname)); priv->type = RTL_TYPE_SDM; break; default: - strncpy(priv->hwname, RTL_NAME_UNKNOWN, sizeof(priv->hwname)); + strscpy(priv->hwname, RTL_NAME_UNKNOWN, sizeof(priv->hwname)); break; } diff --git a/target/linux/generic/files/drivers/net/phy/swconfig_leds.c b/target/linux/generic/files/drivers/net/phy/swconfig_leds.c index 1fcd4432b54..b0970b00e8a 100644 --- a/target/linux/generic/files/drivers/net/phy/swconfig_leds.c +++ b/target/linux/generic/files/drivers/net/phy/swconfig_leds.c @@ -237,8 +237,7 @@ static ssize_t swconfig_trig_mode_store(struct device *dev, char *p, *token; /* take a copy since we don't want to trash the inbound buffer when using strsep */ - strncpy(copybuf, buf, sizeof(copybuf)); - copybuf[sizeof(copybuf) - 1] = 0; + strscpy(copybuf, buf, sizeof(copybuf)); p = copybuf; while ((token = strsep(&p, " \t\n")) != NULL) { diff --git a/target/linux/generic/pending-6.12/430-mtd-add-myloader-partition-parser.patch b/target/linux/generic/pending-6.12/430-mtd-add-myloader-partition-parser.patch index e1fe9a46bc3..8d308c989e3 100644 --- a/target/linux/generic/pending-6.12/430-mtd-add-myloader-partition-parser.patch +++ b/target/linux/generic/pending-6.12/430-mtd-add-myloader-partition-parser.patch @@ -160,7 +160,7 @@ Signed-off-by: Adrian Schmutzler + mtd_part = mtd_parts; + names = (char *)&mtd_parts[num_parts]; + -+ strncpy(names, "myloader", PART_NAME_LEN); ++ strscpy_pad(names, "myloader", PART_NAME_LEN); + mtd_part->name = names; + mtd_part->offset = 0; + mtd_part->size = offset; @@ -168,7 +168,7 @@ Signed-off-by: Adrian Schmutzler + mtd_part++; + names += PART_NAME_LEN; + -+ strncpy(names, "partition_table", PART_NAME_LEN); ++ strscpy_pad(names, "partition_table", PART_NAME_LEN); + mtd_part->name = names; + mtd_part->offset = offset; + mtd_part->size = blocklen; @@ -183,7 +183,7 @@ Signed-off-by: Adrian Schmutzler + continue; + + if ((buf->names[i][0]) && (buf->names[i][0] != '\xff')) -+ strncpy(names, buf->names[i], PART_NAME_LEN); ++ strscpy_pad(names, buf->names[i], PART_NAME_LEN); + else + snprintf(names, PART_NAME_LEN, "partition%d", i); + diff --git a/target/linux/generic/pending-6.12/920-mangle_bootargs.patch b/target/linux/generic/pending-6.12/920-mangle_bootargs.patch index c3b2f9dd50f..636bf8f90e4 100644 --- a/target/linux/generic/pending-6.12/920-mangle_bootargs.patch +++ b/target/linux/generic/pending-6.12/920-mangle_bootargs.patch @@ -44,12 +44,12 @@ Signed-off-by: Imre Kaloz + rootdev = strstr(command_line, "root=/dev/mtdblock"); + + if (rootdev) -+ strncpy(rootdev, "mangled_rootblock=", 18); ++ memcpy(rootdev, "mangled_rootblock=", 18); + + rootfs = strstr(command_line, "rootfstype"); + + if (rootfs) -+ strncpy(rootfs, "mangled_fs", 10); ++ memcpy(rootfs, "mangled_fs", 10); + +} +#else diff --git a/target/linux/generic/pending-6.18/430-mtd-add-myloader-partition-parser.patch b/target/linux/generic/pending-6.18/430-mtd-add-myloader-partition-parser.patch index e1fe9a46bc3..8d308c989e3 100644 --- a/target/linux/generic/pending-6.18/430-mtd-add-myloader-partition-parser.patch +++ b/target/linux/generic/pending-6.18/430-mtd-add-myloader-partition-parser.patch @@ -160,7 +160,7 @@ Signed-off-by: Adrian Schmutzler + mtd_part = mtd_parts; + names = (char *)&mtd_parts[num_parts]; + -+ strncpy(names, "myloader", PART_NAME_LEN); ++ strscpy_pad(names, "myloader", PART_NAME_LEN); + mtd_part->name = names; + mtd_part->offset = 0; + mtd_part->size = offset; @@ -168,7 +168,7 @@ Signed-off-by: Adrian Schmutzler + mtd_part++; + names += PART_NAME_LEN; + -+ strncpy(names, "partition_table", PART_NAME_LEN); ++ strscpy_pad(names, "partition_table", PART_NAME_LEN); + mtd_part->name = names; + mtd_part->offset = offset; + mtd_part->size = blocklen; @@ -183,7 +183,7 @@ Signed-off-by: Adrian Schmutzler + continue; + + if ((buf->names[i][0]) && (buf->names[i][0] != '\xff')) -+ strncpy(names, buf->names[i], PART_NAME_LEN); ++ strscpy_pad(names, buf->names[i], PART_NAME_LEN); + else + snprintf(names, PART_NAME_LEN, "partition%d", i); + diff --git a/target/linux/generic/pending-6.18/920-mangle_bootargs.patch b/target/linux/generic/pending-6.18/920-mangle_bootargs.patch index b91a9e10ac4..57942caced9 100644 --- a/target/linux/generic/pending-6.18/920-mangle_bootargs.patch +++ b/target/linux/generic/pending-6.18/920-mangle_bootargs.patch @@ -44,12 +44,12 @@ Signed-off-by: Imre Kaloz + rootdev = strstr(command_line, "root=/dev/mtdblock"); + + if (rootdev) -+ strncpy(rootdev, "mangled_rootblock=", 18); ++ memcpy(rootdev, "mangled_rootblock=", 18); + + rootfs = strstr(command_line, "rootfstype"); + + if (rootfs) -+ strncpy(rootfs, "mangled_fs", 10); ++ memcpy(rootfs, "mangled_fs", 10); + +} +#else diff --git a/target/linux/qualcommax/patches-6.12/0911-arm64-cmdline-replacement.patch b/target/linux/qualcommax/patches-6.12/0911-arm64-cmdline-replacement.patch index 8e72f181a65..b376265a5ce 100644 --- a/target/linux/qualcommax/patches-6.12/0911-arm64-cmdline-replacement.patch +++ b/target/linux/qualcommax/patches-6.12/0911-arm64-cmdline-replacement.patch @@ -91,7 +91,7 @@ Signed-off-by: Qiyuan Zhang + *(cur_ptr + offset) = *cur_ptr; + } + -+ strncpy(s_ptr, p, r_len - 1); ++ memcpy(s_ptr, p, r_len - 1); + + pr_info("Kernel command line after replacement: %s\n", cmdline); + } else {