89bf901afb
linux-user getgroups(), setgroups(), getgroups32() and setgroups32()
used alloca() to allocate grouplist arrays, with unchecked gidsetsize
coming from the "guest". With NGROUPS_MAX being 65536 (linux, and it
is common for an application to allocate NGROUPS_MAX for getgroups()),
this means a typical allocation is half the megabyte on the stack.
Which just overflows stack, which leads to immediate SIGSEGV in actual
system getgroups() implementation.
An example of such issue is aptitude, eg
https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=811087#72
Cap gidsetsize to NGROUPS_MAX (return EINVAL if it is larger than that),
and use heap allocation for grouplist instead of alloca(). While at it,
fix coding style and make all 4 implementations identical.
Try to not impose random limits - for example, allow gidsetsize to be
negative for getgroups() - just do not allocate negative-sized grouplist
in this case but still do actual getgroups() call. But do not allow
negative gidsetsize for setgroups() since its argument is unsigned.
Capping by NGROUPS_MAX seems a bit arbitrary, - we can do more, it is
not an error if set size will be NGROUPS_MAX+1. But we should not allow
integer overflow for the array being allocated. Maybe it is enough to
just call g_try_new() and return ENOMEM if it fails.
Maybe there's also no need to convert setgroups() since this one is
usually smaller and known beforehand (KERN_NGROUPS_MAX is actually 63, -
this is apparently a kernel-imposed limit for runtime group set).
The patch fixes aptitude segfault mentioned above.
Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
Message-Id: <20230409105327.1273372-1-mjt@msgid.tls.msk.ru>
Signed-off-by: Laurent Vivier <laurent@vivier.eu>
(cherry picked from commit
|
||
---|---|---|
.. | ||
aarch64 | ||
alpha | ||
arm | ||
cris | ||
generic | ||
hexagon | ||
hppa | ||
i386 | ||
include | ||
loongarch64 | ||
m68k | ||
microblaze | ||
mips | ||
mips64 | ||
nios2 | ||
openrisc | ||
ppc | ||
riscv | ||
s390x | ||
sh4 | ||
sparc | ||
x86_64 | ||
xtensa | ||
cpu_loop-common.h | ||
elfload.c | ||
errnos.c.inc | ||
exit.c | ||
fd-trans.c | ||
fd-trans.h | ||
flat.h | ||
flatload.c | ||
ioctls.h | ||
linux_loop.h | ||
linuxload.c | ||
loader.h | ||
main.c | ||
meson.build | ||
mmap.c | ||
qemu.h | ||
semihost.c | ||
signal-common.h | ||
signal.c | ||
socket.h | ||
strace.c | ||
strace.h | ||
strace.list | ||
syscall_defs.h | ||
syscall_types.h | ||
syscall.c | ||
target_flat.h | ||
thunk.c | ||
trace-events | ||
trace.h | ||
uaccess.c | ||
uname.c | ||
uname.h | ||
user-internals.h | ||
user-mmap.h | ||
vm86.c |