LoongArch: Avoid in-place string operation on FDT content
authorYao Zi <ziyao@disroot.org>
Sun, 3 Aug 2025 14:49:47 +0000 (22:49 +0800)
committerHuacai Chen <chenhuacai@loongson.cn>
Sun, 3 Aug 2025 14:49:47 +0000 (22:49 +0800)
In init_cpu_fullname(), a constant pointer to "model" property is
retrieved. It's later modified by the strsep() function, which is
illegal and corrupts kernel's FDT copy. This is shown by dmesg,

OF: fdt: not creating '/sys/firmware/fdt': CRC check failed

Create a mutable copy of the model property and do in-place operations
on the mutable copy instead. loongson_sysconf.cpuname lives across the
kernel lifetime, thus manually releasing isn't necessary.

Also move the of_node_put() call for the root node after the usage of
its property, since of_node_put() decreases the reference counter thus
usage after the call is unsafe.

Cc: stable@vger.kernel.org
Fixes: 44a01f1f726a ("LoongArch: Parsing CPU-related information from DTS")
Reviewed-by: Jiaxun Yang <jiaxun.yang@flygoat.com>
Signed-off-by: Yao Zi <ziyao@disroot.org>
Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
arch/loongarch/kernel/env.c

index 27144de5c5fe4f6159aa430f476c3d249af2aa2e..c0a5dc9aeae2871223b6e5efc88a9137c11378d9 100644 (file)
@@ -39,16 +39,19 @@ void __init init_environ(void)
 
 static int __init init_cpu_fullname(void)
 {
-       struct device_node *root;
        int cpu, ret;
-       char *model;
+       char *cpuname;
+       const char *model;
+       struct device_node *root;
 
        /* Parsing cpuname from DTS model property */
        root = of_find_node_by_path("/");
-       ret = of_property_read_string(root, "model", (const char **)&model);
+       ret = of_property_read_string(root, "model", &model);
+       if (ret == 0) {
+               cpuname = kstrdup(model, GFP_KERNEL);
+               loongson_sysconf.cpuname = strsep(&cpuname, " ");
+       }
        of_node_put(root);
-       if (ret == 0)
-               loongson_sysconf.cpuname = strsep(&model, " ");
 
        if (loongson_sysconf.cpuname && !strncmp(loongson_sysconf.cpuname, "Loongson", 8)) {
                for (cpu = 0; cpu < NR_CPUS; cpu++)