Skip to contentSkip to conversation

linux-user: Return EINVAL for getgroups() with negative gidsetsize

PATCH In review Peter Maydell 2 participants 2 messages Jun 9, 2023

lore
Share & tools
lore
#0 lore raw

PATCH v2 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. It also brings the getgroups() code in to line with the setgroups() code.

Possibly Coverity may still complain about getgroups(0, NULL), but that would be a false positive.

---
v2: also change TARGET_NR_getgroups32 code

---
 linux-user/syscall.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
linux-user/syscall.c +2 -2
index 83685f0aa59..bb29444ca7e 100644
@@ -11676,7 +11676,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) {
@@ -12012,7 +12012,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
Reviewed-by on v2 1/1
#1 lore raw
Quoted 10 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. It also brings the getgroups() code in to line with the setgroups() code.

Quoted 6 lines

Possibly Coverity may still complain about getgroups(0, NULL), but that would be a false positive.

---
v2: also change TARGET_NR_getgroups32 code

This can be applied to -trivial just fine, I think. There's another change in there already in exactly this area.

Thank you Peter!

/mjt