Files
qemu-reims-vgpu/tests/tcg/multiarch/linux/linux-null-buffer.c
T
TAKANO Mitsuhiro cdd9951340 linux-user: allow a zero-length getrandom with a NULL buffer
The kernel never looks at the buffer of a zero-length request, so
getrandom(NULL, 0, 0) returns 0.  qemu-user hands the request to
lock_user() unconditionally, and a NULL or otherwise unusable address
fails there with EFAULT even though nothing is copied.

read(), write(), pread64() and pwrite64() already special-case the
zero-length request; do the same here.  The address plays no part in it:
with a zero length it is never dereferenced, whatever value the caller
passes.

Fixes: f894efd199 ("linux-user: add getrandom() syscall")
Signed-off-by: TAKANO Mitsuhiro <takano32@gmail.com>
Signed-off-by: Helge Deller <deller@gmx.de>
2026-11-05 23:43:50 +01:00

20 lines
388 B
C

/*
* A zero-length getrandom() needs no valid buffer: the kernel never looks at
* the pointer, so getrandom(NULL, 0, 0) succeeds.
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#define _GNU_SOURCE
#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/random.h>
int main(void)
{
errno = 0;
assert(getrandom(NULL, 0, 0) == 0);
return EXIT_SUCCESS;
}