linux-user/riscv: Fix handling of cpu mask in riscv_hwprobe syscall

The third argument of the syscall contains the size of the
cpu mask in bytes, not bits.  Nor is the size rounded up to
a multiple of sizeof(abi_ulong).

Cc: qemu-stable@nongnu.org
Reported-by: Andreas Schwab <schwab@suse.de>
Fixes: 9e1c7d982d ("linux-user/riscv: Add syscall riscv_hwprobe")
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
Message-ID: <20250308225902.1208237-3-richard.henderson@linaro.org>
Signed-off-by: Alistair Francis <alistair.francis@wdc.com>
This commit is contained in:
Richard Henderson 2025-03-08 14:58:40 -08:00 committed by Alistair Francis
parent 4e9e2478df
commit 1a010d22b7

View file

@ -9119,35 +9119,38 @@ static void risc_hwprobe_fill_pairs(CPURISCVState *env,
} }
} }
static int cpu_set_valid(abi_long arg3, abi_long arg4) /*
* If the cpumask_t of (target_cpus, cpusetsize) cannot be read: -EFAULT.
* If the cpumast_t has no bits set: -EINVAL.
* Otherwise the cpumask_t contains some bit set: 0.
* Unlike the kernel, we do not mask cpumask_t by the set of online cpus,
* nor bound the search by cpumask_size().
*/
static int nonempty_cpu_set(abi_ulong cpusetsize, abi_ptr target_cpus)
{ {
int ret, i, tmp; unsigned char *p = lock_user(VERIFY_READ, target_cpus, cpusetsize, 1);
size_t host_mask_size, target_mask_size; int ret = -TARGET_EFAULT;
unsigned long *host_mask;
/* if (p) {
* cpu_set_t represent CPU masks as bit masks of type unsigned long *. ret = -TARGET_EINVAL;
* arg3 contains the cpu count. /*
*/ * Since we only care about the empty/non-empty state of the cpumask_t
tmp = (8 * sizeof(abi_ulong)); * not the individual bits, we do not need to repartition the bits
target_mask_size = ((arg3 + tmp - 1) / tmp) * sizeof(abi_ulong); * from target abi_ulong to host unsigned long.
host_mask_size = (target_mask_size + (sizeof(*host_mask) - 1)) & *
~(sizeof(*host_mask) - 1); * Note that the kernel does not round up cpusetsize to a multiple of
* sizeof(abi_ulong). After bounding cpusetsize by cpumask_size(),
host_mask = alloca(host_mask_size); * it copies exactly cpusetsize bytes into a zeroed buffer.
*/
ret = target_to_host_cpu_mask(host_mask, host_mask_size, for (abi_ulong i = 0; i < cpusetsize; ++i) {
arg4, target_mask_size); if (p[i]) {
if (ret != 0) { ret = 0;
return ret; break;
} }
for (i = 0 ; i < host_mask_size / sizeof(*host_mask); i++) {
if (host_mask[i] != 0) {
return 0;
} }
unlock_user(p, target_cpus, 0);
} }
return -TARGET_EINVAL; return ret;
} }
static abi_long do_riscv_hwprobe(CPUArchState *cpu_env, abi_long arg1, static abi_long do_riscv_hwprobe(CPUArchState *cpu_env, abi_long arg1,
@ -9164,7 +9167,7 @@ static abi_long do_riscv_hwprobe(CPUArchState *cpu_env, abi_long arg1,
/* check cpu_set */ /* check cpu_set */
if (arg3 != 0) { if (arg3 != 0) {
ret = cpu_set_valid(arg3, arg4); ret = nonempty_cpu_set(arg3, arg4);
if (ret != 0) { if (ret != 0) {
return ret; return ret;
} }