Skip to contentSkip to conversation

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

PATCH Superseded Peter Maydell 4 participants 6 messages Jun 2, 2023 – Jun 9, 2023

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

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
index 89b58b386b1..29fdfdf18e4 100644
@@ -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
Reviewed-by on v1 1/1
#1 lore raw

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.

---
  linux-user/syscall.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
linux-user/syscall.c +0 -0
index 89b58b386b1..29fdfdf18e4 100644

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) {
Reviewed-by on v1 1/1
#2 lore raw
In reply to Peter Maydell
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.

---
  linux-user/syscall.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Reviewed-by on v1 1/1
#3 lore raw
In reply to Peter Maydell
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.

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

#4 lore raw
In reply to Peter Maydell

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

#5 lore raw
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