Skip to content

Commit 70a2365

Browse files
ziyao233chenhuacai
authored andcommitted
LoongArch: Avoid in-place string operation on FDT content
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: 44a01f1 ("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>
1 parent 243f8de commit 70a2365

File tree

1 file changed

+8
-5
lines changed
  • arch/loongarch/kernel

1 file changed

+8
-5
lines changed

arch/loongarch/kernel/env.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,19 @@ void __init init_environ(void)
3939

4040
static int __init init_cpu_fullname(void)
4141
{
42-
struct device_node *root;
4342
int cpu, ret;
44-
char *model;
43+
char *cpuname;
44+
const char *model;
45+
struct device_node *root;
4546

4647
/* Parsing cpuname from DTS model property */
4748
root = of_find_node_by_path("/");
48-
ret = of_property_read_string(root, "model", (const char **)&model);
49+
ret = of_property_read_string(root, "model", &model);
50+
if (ret == 0) {
51+
cpuname = kstrdup(model, GFP_KERNEL);
52+
loongson_sysconf.cpuname = strsep(&cpuname, " ");
53+
}
4954
of_node_put(root);
50-
if (ret == 0)
51-
loongson_sysconf.cpuname = strsep(&model, " ");
5255

5356
if (loongson_sysconf.cpuname && !strncmp(loongson_sysconf.cpuname, "Loongson", 8)) {
5457
for (cpu = 0; cpu < NR_CPUS; cpu++)

0 commit comments

Comments
 (0)