Skip to contentSkip to conversation

riscv: hwprobe: Avoid uninitialized read in hwprobe_get_cpus()

PATCH Superseded Mark Harris 3 participants 5 messages Jun 12 – Jun 17 RISC-V

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 riscv: hwprobe: Avoid uninitialized read in hwprobe_get_cpus()

When cpusetsize < cpumask_size(), hwprobe_get_cpus() did not fully initialize its copy of the cpu mask, which could cause non-deterministic results from the riscv_hwprobe syscall on a system with more than 8 CPUs when the supplied cpu mask is empty. Address this by fully initializing the cpu mask.

---
 arch/riscv/kernel/sys_hwprobe.c | 1 +
 1 file changed, 1 insertion(+)
arch/riscv/kernel/sys_hwprobe.c +1 -0
index 1659d31fd288..caf6762427c8 100644
@@ -450,6 +450,7 @@ static int hwprobe_get_cpus(struct riscv_hwprobe __user *pairs,
 	if (cpusetsize > cpumask_size())
 		cpusetsize = cpumask_size();
 
+	cpumask_clear(&cpus);
 	ret = copy_from_user(&cpus, cpus_user, cpusetsize);
 	if (ret)
 		return -EFAULT;
Signature · 1 line
2.54.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Mark Harris

When cpusetsize < cpumask_size(), hwprobe_get_cpus() did not fully initialize its copy of the cpu mask, which could cause non-deterministic results from the riscv_hwprobe syscall on a system with more than 8 CPUs when the supplied cpu mask is empty. Address this by fully initializing the cpu mask.

Reviewed-by on v1 1/1
#2 lore raw
In reply to Mark Harris
Quoted 10 lines · Mark Harris

When cpusetsize < cpumask_size(), hwprobe_get_cpus() did not fully initialize its copy of the cpu mask, which could cause non-deterministic results from the riscv_hwprobe syscall on a system with more than 8 CPUs when the supplied cpu mask is empty. Address this by fully initializing the cpu mask.

---
  arch/riscv/kernel/sys_hwprobe.c | 1 +
  1 file changed, 1 insertion(+)

This should have a fixes tag, I think it's:

Fixes e178bf146e4b ("RISC-V: hwprobe: Introduce which-cpus flag")

arch/riscv/kernel/sys_hwprobe.c static int hwprobe_get_cpus(struct riscv_hwprobe __user *pairs, · line 456

Quoted 12 lines
arch/riscv/kernel/sys_hwprobe.c +1 -0
index 1659d31fd288..caf6762427c8 100644
@@ -450,6 +450,7 @@ static int hwprobe_get_cpus(struct riscv_hwprobe __user *pairs,
  	if (cpusetsize > cpumask_size())
  		cpusetsize = cpumask_size();
 
+	cpumask_clear(&cpus);
  	ret = copy_from_user(&cpus, cpus_user, cpusetsize);
  	if (ret)
  		return -EFAULT;

cpus is on the stack, and is copied back out at the end of the function, so this looks like it could be a stack info leak.

But the copy back is also bounded by cpusetsize, so in fact there is not any leak of uninitialised stack out to userspace:

        ret = copy_to_user(cpus_user, &cpus, cpusetsize);
        if (ret)
                return -EFAULT;

cheers

#3 lore raw
Quoted 8 lines · Michael Ellerman
Quoted 6 lines · Mark Harris

When cpusetsize < cpumask_size(), hwprobe_get_cpus() did not fully initialize its copy of the cpu mask, which could cause non-deterministic results from the riscv_hwprobe syscall on a system with more than 8 CPUs when the supplied cpu mask is empty. Address this by fully initializing the cpu mask.

Quoted 8 lines
Quoted 4 lines
---
  arch/riscv/kernel/sys_hwprobe.c | 1 +
  1 file changed, 1 insertion(+)

This should have a fixes tag, I think it's:

Fixes e178bf146e4b ("RISC-V: hwprobe: Introduce which-cpus flag")

Yes, that looks correct.

Quoted 17 lines

arch/riscv/kernel/sys_hwprobe.c static int hwprobe_get_cpus(struct riscv_hwprobe __user *pairs, · line 456

Quoted 13 lines
arch/riscv/kernel/sys_hwprobe.c +1 -0
index 1659d31fd288..caf6762427c8 100644
@@ -450,6 +450,7 @@ static int hwprobe_get_cpus(struct riscv_hwprobe __user *pairs,
      if (cpusetsize > cpumask_size())
              cpusetsize = cpumask_size();
 
+     cpumask_clear(&cpus);
      ret = copy_from_user(&cpus, cpus_user, cpusetsize);
      if (ret)
              return -EFAULT;

cpus is on the stack, and is copied back out at the end of the function, so this looks like it could be a stack info leak.

Quoted 6 lines

But the copy back is also bounded by cpusetsize, so in fact there is not any leak of uninitialised stack out to userspace:

         ret = copy_to_user(cpus_user, &cpus, cpusetsize);
         if (ret)
                 return -EFAULT;

It can leak 1 bit of information from the kernel stack. For example with 16 CPUs, all online, if a 1-byte 0x00 mask is supplied, the caller can determine whether the second (uninitialized) mask byte is zero or non-zero due to the cpumask_empty(&cpus) check.

- Mark

Quoted 4 lines

cheers

#4 lore raw
Quoted 40 lines · Mark Harris
Quoted 8 lines · Michael Ellerman
Quoted 6 lines · Mark Harris

When cpusetsize < cpumask_size(), hwprobe_get_cpus() did not fully initialize its copy of the cpu mask, which could cause non-deterministic results from the riscv_hwprobe syscall on a system with more than 8 CPUs when the supplied cpu mask is empty. Address this by fully initializing the cpu mask.

Quoted 8 lines
Quoted 4 lines
---
   arch/riscv/kernel/sys_hwprobe.c | 1 +
   1 file changed, 1 insertion(+)

This should have a fixes tag, I think it's:

Fixes e178bf146e4b ("RISC-V: hwprobe: Introduce which-cpus flag")

Yes, that looks correct.

Quoted 20 lines

arch/riscv/kernel/sys_hwprobe.c static int hwprobe_get_cpus(struct riscv_hwprobe __user *pairs, · line 456

Quoted 13 lines
arch/riscv/kernel/sys_hwprobe.c +1 -0
index 1659d31fd288..caf6762427c8 100644
@@ -450,6 +450,7 @@ static int hwprobe_get_cpus(struct riscv_hwprobe __user *pairs,
       if (cpusetsize > cpumask_size())
               cpusetsize = cpumask_size();
 
+     cpumask_clear(&cpus);
       ret = copy_from_user(&cpus, cpus_user, cpusetsize);
       if (ret)
               return -EFAULT;

cpus is on the stack, and is copied back out at the end of the function, so this looks like it could be a stack info leak.

But the copy back is also bounded by cpusetsize, so in fact there is not any leak of uninitialised stack out to userspace:

Quoted 8 lines
Quoted 3 lines
          ret = copy_to_user(cpus_user, &cpus, cpusetsize);
          if (ret)
                  return -EFAULT;

It can leak 1 bit of information from the kernel stack. For example with 16 CPUs, all online, if a 1-byte 0x00 mask is supplied, the caller can determine whether the second (uninitialized) mask byte is zero or non-zero due to the cpumask_empty(&cpus) check.

Oh yep, because the caller can observe if cpus was/wasn't overwritten with the cpus_online_mask.

I guess that counts as an info leak, it doesn't let an attacker reconstruct actual values from the stack, but a zero/non-zero check could be useful in theory.

cheers