linux-user: Return EINVAL for getgroups() with negative gidsetsize
PATCH 1/1 linux-user: Return EINVAL for getgroups() with negative gidsetsize
Coverity doesn't like the way we might end up calling getgroups() with a NULL grouplist pointer. This is fine for the special case of gidsetsize == 0, but we will also do it if the guest passes us a negative gidsetsize. (CID 1512465)
Explicitly fail the negative gidsetsize with EINVAL, as the kernel does. This means we definitely only call the libc getgroups() with valid parameters.
Possibly Coverity may still complain about getgroups(0, NULL), but that would be a false positive.
--- linux-user/syscall.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
linux-user/syscall.c +1 -1
@@ -11574,7 +11574,7 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1, g_autofree gid_t *grouplist = NULL;int i; -if (gidsetsize >NGROUPS_MAX ) { +if (gidsetsize >NGROUPS_MAX || gidsetsize <0 ) {return -TARGET_EINVAL ; }if (gidsetsize >0 ) {
Signature · 1 line
2.34.1
Le 02/06/2023 à 19:48, Peter Maydell a écrit :
Quoted 22 lines
Coverity doesn't like the way we might end up calling
getgroups()with a NULL grouplist pointer. This is fine for the special case of gidsetsize == 0, but we will also do it if the guest passes us a negative gidsetsize. (CID 1512465)Explicitly fail the negative gidsetsize with EINVAL, as the kernel does. This means we definitely only call the libc
getgroups()with valid parameters.Possibly Coverity may still complain about
getgroups(0, NULL), but that would be a false positive.Signed-off-by Peter Maydell <[email protected]>--- linux-user/syscall.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)linux-user/syscall.c +0
-0
linux-user/syscall.c static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1, · line 11580
Quoted 8 lines
linux-user/syscall.c +1
-1@@ -11574,7 +11574,7 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1, g_autofree gid_t *grouplist = NULL;int i; -if (gidsetsize >NGROUPS_MAX ) { +if (gidsetsize >NGROUPS_MAX || gidsetsize <0 ) {return -TARGET_EINVAL ; }if (gidsetsize >0 ) {
Quoted 9 lines · Peter Maydell
Coverity doesn't like the way we might end up calling
getgroups()with a NULL grouplist pointer. This is fine for the special case of gidsetsize == 0, but we will also do it if the guest passes us a negative gidsetsize. (CID 1512465)Explicitly fail the negative gidsetsize with EINVAL, as the kernel does. This means we definitely only call the libc
getgroups()with valid parameters.
Quoted 7 lines
Possibly Coverity may still complain about
getgroups(0, NULL), but that would be a false positive.Signed-off-by Peter Maydell <[email protected]>--- linux-user/syscall.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 5 lines · Peter Maydell
Coverity doesn't like the way we might end up calling
getgroups()with a NULL grouplist pointer. This is fine for the special case of gidsetsize == 0, but we will also do it if the guest passes us a negative gidsetsize. (CID 1512465)
Quoted 8 lines
Explicitly fail the negative gidsetsize with EINVAL, as the kernel does. This means we definitely only call the libc
getgroups()with valid parameters.Possibly Coverity may still complain about
getgroups(0, NULL), but that would be a false positive.Signed-off-by Peter Maydell <[email protected]>
I especially kept the negative case like this when initially writing this code, thinking to return whatever error the kernel will return. But I guess it doesn't really matter here, and the less fragile corner cases, the better.
Thanks,
/mjt
linux-user/syscall.c static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1, · line 11580
Quoted 9 lines · Peter Maydell
linux-user/syscall.c +1
-1@@ -11574,7 +11574,7 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1, g_autofree gid_t *grouplist = NULL;int i; -if (gidsetsize >NGROUPS_MAX ) { +if (gidsetsize >NGROUPS_MAX || gidsetsize <0 ) {return -TARGET_EINVAL ; }if (gidsetsize >0 ) {
FWIW, there's another piece of code exactly like this one, for TARGET_NR_getgroups32. The same change is needed there too.
Thanks,
/mjt
Quoted 13 lines · Michael Tokarev
linux-user/syscall.c static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1, · line 11579
Quoted 8 lines · Peter Maydell
linux-user/syscall.c +1
-1@@ -11574,7 +11574,7 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1, g_autofree gid_t *grouplist = NULL; int i; - if (gidsetsize > NGROUPS_MAX) { + if (gidsetsize > NGROUPS_MAX || gidsetsize < 0) { return -TARGET_EINVAL; } if (gidsetsize > 0) {FWIW, there's another piece of code exactly like this one, for TARGET_NR_getgroups32. The same change is needed there too.
Peter, will you respin this (to include getgroups32 case), or should I ? (The change is trivial enough to carry though -trivial@).
Thanks,
/mjt