]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
riscv: hwprobe: avoid uninitialized variable use in hwprobe_arch_id()
authorPaul Walmsley <pjw@kernel.org>
Sat, 18 Oct 2025 15:32:12 +0000 (09:32 -0600)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 29 Oct 2025 13:08:59 +0000 (14:08 +0100)
[ Upstream commit b7776a802f2f80139f96530a489dd00fd7089eda ]

Resolve this smatch warning:

  arch/riscv/kernel/sys_hwprobe.c:50 hwprobe_arch_id() error: uninitialized symbol 'cpu_id'.

This could happen if hwprobe_arch_id() was called with a key ID of
something other than MVENDORID, MIMPID, and MARCHID.  This does not
happen in the current codebase.  The only caller of hwprobe_arch_id()
is a function that only passes one of those three key IDs.

For the sake of reducing static analyzer warning noise, and in the
unlikely event that hwprobe_arch_id() is someday called with some
other key ID, validate hwprobe_arch_id()'s input to ensure that
'cpu_id' is always initialized before use.

Fixes: ea3de9ce8aa280 ("RISC-V: Add a syscall for HW probing")
Cc: Evan Green <evan@rivosinc.com>
Signed-off-by: Paul Walmsley <pjw@kernel.org>
Link: https://lore.kernel.org/r/cf5a13ec-19d0-9862-059b-943f36107bf3@kernel.org
Signed-off-by: Sasha Levin <sashal@kernel.org>
arch/riscv/kernel/sys_hwprobe.c

index cea0ca2bf2a25ecc671e31b141e84c6d1977da25..fc62548888c587885c1c389de293075eb3fb8b04 100644 (file)
@@ -25,6 +25,11 @@ static void hwprobe_arch_id(struct riscv_hwprobe *pair,
        bool first = true;
        int cpu;
 
+       if (pair->key != RISCV_HWPROBE_KEY_MVENDORID &&
+           pair->key != RISCV_HWPROBE_KEY_MIMPID &&
+           pair->key != RISCV_HWPROBE_KEY_MARCHID)
+               goto out;
+
        for_each_cpu(cpu, cpus) {
                u64 cpu_id;
 
@@ -55,6 +60,7 @@ static void hwprobe_arch_id(struct riscv_hwprobe *pair,
                }
        }
 
+out:
        pair->value = id;
 }