Skip to contentSkip to conversation

linux-user: limit number of arguments to execve

PATCH Superseded P J P 4 participants 9 messages Mar 3, 2017 – Mar 6, 2017

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

P J P

author
#0 lore raw

PATCH 1/1 linux-user: limit number of arguments to execve

From: Prasad J Pandit <[email protected]>

Limit the number of arguments passed to execve(2) call from a user program, as large number of them could lead to a bad guest address error.

---
 linux-user/syscall.c | 7 +++++++
 1 file changed, 7 insertions(+)
linux-user/syscall.c +7 -0
index 9be8e95..c545c12 100644
@@ -7766,6 +7766,7 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
 #endif
     case TARGET_NR_execve:
         {
+#define ARG_MAX 65535
             char **argp, **envp;
             int argc, envc;
             abi_ulong gp;
@@ -7794,6 +7795,12 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
                 envc++;
             }
 
+            if (argc > ARG_MAX || envc > ARG_MAX) {
+                fprintf(stderr,
+                        "argc(%d), envc(%d) exceed %d\n", argc, envc, ARG_MAX);
+                ret = -TARGET_EFAULT;
+                break;
+            }
             argp = alloca((argc + 1) * sizeof(void *));
             envp = alloca((envc + 1) * sizeof(void *));
 
Signature · 1 line
2.9.3
#1 lore raw
Quoted 5 lines · P J P

From: Prasad J Pandit <[email protected]>

Limit the number of arguments passed to execve(2) call from a user program, as large number of them could lead to a bad guest address error.

Depending on how the kernel was compiled, you may have a limited maximum size for the combined storage used by argc, environ, and all pointers. Older kernels had a hard ceiling on storage used by argc and environ as small as 128k (not just the bytes pointed to, but also the storage occupied by the pointers) - it was possible to trigger E2BIG failures with a large number of arguments in the array, but also with a single argument that was too long. Modern kernels have increased the limits somewhat (now it is as large as 1/4 available stack size), but still has maximum sizes for a single argument (larger than before, but still finite). Any one of these E2BIG failures is something we need to handle gracefully, especially if the guest is expecting to be able to use a larger limit than the host is supporting. So I'm not sure that limiting the number of arguments is sufficient - you may also have to limit the size of the arguments.

I typed the above without looking at your patch. Now that I've done that, I see that you are tackling yet another type of corruption...

Quoted 20 lines
---
 linux-user/syscall.c | 7 +++++++
 1 file changed, 7 insertions(+)
linux-user/syscall.c +1 -0
index 9be8e95..c545c12 100644
@@ -7766,6 +7766,7 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
 #endif
     case TARGET_NR_execve:
         {
+#define ARG_MAX 65535
             char **argp, **envp;
             int argc, envc;
             abi_ulong gp;

linux-user/syscall.c abi_long do_syscall(void *cpu_env, int num, abi_long arg1, · line 7800

Quoted 6 lines
linux-user/syscall.c +3 -0
@@ -7794,6 +7795,12 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
                 envc++;
             }
 
+            if (argc > ARG_MAX || envc > ARG_MAX) {
+                fprintf(stderr,
+                        "argc(%d), envc(%d) exceed %d\n", argc, envc, ARG_MAX);

Isn't there something better than fprintf() for reporting errors?

linux-user/syscall.c abi_long do_syscall(void *cpu_env, int num, abi_long arg1, · line 7805

Quoted 5 lines
linux-user/syscall.c +3 -0
+                ret = -TARGET_EFAULT;
+                break;
+            }
             argp = alloca((argc + 1) * sizeof(void *));
             envp = alloca((envc + 1) * sizeof(void *));

...Uggh. You're using alloca() but allowing an allocation of way more than 4k. That means a guest can cause corruption of the stack (or, with large enough arguments, even escape out of the stack) before you even get to the execve() call to even worry about E2BIG issues. Even though your new limit of 64k argc/envc limits the damage compared to what it used to be, you are still prone to an allocation that walks beyond the guard page and causes bad behavior. Either you need the limits to be much smaller, or you should consider using the heap instead of the stack (alloca should never be used for more than about 4k). And there's still the possibility that even with your cap, that you are not handling E2BIG correctly. You'll need to respin this patch.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#2 lore raw
Quoted 10 lines · Eric Blake

linux-user/syscall.c abi_long do_syscall(void *cpu_env, int num, abi_long arg1, · line 7805

Quoted 5 lines
linux-user/syscall.c +3 -0
+                ret = -TARGET_EFAULT;
+                break;
+            }
             argp = alloca((argc + 1) * sizeof(void *));
             envp = alloca((envc + 1) * sizeof(void *));

...Uggh. You're using alloca() but allowing an allocation of way more than 4k. That means a guest can cause corruption of the stack (or, with large enough arguments, even escape out of the stack) before you even get to the execve() call to even worry about E2BIG issues.

Yeah, linux-user is shot through with that kind of alloca() usage.

(It's not great, but it's not a security hole because we already give the guest binary complete control to do anything it likes. Worth fixing bugs if we run into them, though.)

thanks
-- PMM

#3 lore raw
Quoted 12 lines · Peter Maydell
Quoted 10 lines · Eric Blake

linux-user/syscall.c abi_long do_syscall(void *cpu_env, int num, abi_long arg1, · line 7805

Quoted 5 lines
linux-user/syscall.c +3 -0
+                ret = -TARGET_EFAULT;
+                break;
+            }
             argp = alloca((argc + 1) * sizeof(void *));
             envp = alloca((envc + 1) * sizeof(void *));

...Uggh. You're using alloca() but allowing an allocation of way more than 4k. That means a guest can cause corruption of the stack (or, with large enough arguments, even escape out of the stack) before you even get to the execve() call to even worry about E2BIG issues.

Quoted 5 lines

Yeah, linux-user is shot through with that kind of alloca() usage.

(It's not great, but it's not a security hole because we already give the guest binary complete control to do anything it likes. Worth fixing bugs if we run into them, though.)

It could be a security hole if a benign guest userspace process decides to allow a remote client to specify a bunch of environment variables or so. E.g. HTTP servers with CGI support pass HTTP headers on as environment variables whose names start with "HTTP_"; however, since HTTP servers usually limit the request size, this isn't actually exploitable in that case. But there could theoretically be similar scenarios.

author
#4 lore raw
In reply to Eric Blake

Hello Eric,

+-- On Fri, 3 Mar 2017, Eric Blake wrote --+
| much smaller, or you should consider using the heap instead of the stack
| (alloca should never be used for more than about 4k).  And there's still
| the possibility that even with your cap, that you are not handling E2BIG
| correctly.  You'll need to respin this patch.

Yes, sent a revised patch set v2.

Thank you.
--
Prasad J Pandit / Red Hat Product Security Team
47AF CE69 3A90 54AA 9045 1053 DD13 3D32 FE5B 041F

#5 lore raw
In reply to P J P
Quoted 26 lines · P J P

From: Prasad J Pandit <[email protected]>

Limit the number of arguments passed to execve(2) call from a user program, as large number of them could lead to a bad guest address error.

---
 linux-user/syscall.c | 7 +++++++
 1 file changed, 7 insertions(+)
linux-user/syscall.c +1 -0
index 9be8e95..c545c12 100644
@@ -7766,6 +7766,7 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
 #endif
     case TARGET_NR_execve:
         {
+#define ARG_MAX 65535
             char **argp, **envp;
             int argc, envc;
             abi_ulong gp;
@@ -7794,6 +7795,12 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
                 envc++;

linux-user/syscall.c abi_long do_syscall(void *cpu_env, int num, abi_long arg1, · line 7805

Quoted 10 lines
linux-user/syscall.c +6 -0
             }
 
+            if (argc > ARG_MAX || envc > ARG_MAX) {
+                fprintf(stderr,
+                        "argc(%d), envc(%d) exceed %d\n", argc, envc, ARG_MAX);
+                ret = -TARGET_EFAULT;
+                break;
+            }
             argp = alloca((argc + 1) * sizeof(void *));
             envp = alloca((envc + 1) * sizeof(void *));

This code is already supposed to handle "argument string too big", see commit a6f79cc9a5e.

What's the actual bug case we're trying to handle here?

EFAULT looks like a decidedly odd error to return here, too.

thanks
-- PMM

#6 lore raw
Quoted 41 lines · Peter Maydell
Quoted 26 lines · P J P

From: Prasad J Pandit <[email protected]>

Limit the number of arguments passed to execve(2) call from a user program, as large number of them could lead to a bad guest address error.

---
 linux-user/syscall.c | 7 +++++++
 1 file changed, 7 insertions(+)
linux-user/syscall.c +1 -0
index 9be8e95..c545c12 100644
@@ -7766,6 +7766,7 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
 #endif
     case TARGET_NR_execve:
         {
+#define ARG_MAX 65535
             char **argp, **envp;
             int argc, envc;
             abi_ulong gp;
@@ -7794,6 +7795,12 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
                 envc++;

linux-user/syscall.c abi_long do_syscall(void *cpu_env, int num, abi_long arg1, · line 7805

Quoted 10 lines
linux-user/syscall.c +6 -0
             }
 
+            if (argc > ARG_MAX || envc > ARG_MAX) {
+                fprintf(stderr,
+                        "argc(%d), envc(%d) exceed %d\n", argc, envc, ARG_MAX);
+                ret = -TARGET_EFAULT;
+                break;
+            }
             argp = alloca((argc + 1) * sizeof(void *));
             envp = alloca((envc + 1) * sizeof(void *));

This code is already supposed to handle "argument string too big", see commit a6f79cc9a5e.

Quoted 1 line

What's the actual bug case we're trying to handle here?

commit a6f79cc9a5e doesn't help here, it only bails out after the two alloca() calls

#7 lore raw
Quoted 8 lines · Jann Horn
Quoted 4 lines · Peter Maydell

This code is already supposed to handle "argument string too big", see commit a6f79cc9a5e.

What's the actual bug case we're trying to handle here?

commit a6f79cc9a5e doesn't help here, it only bails out after the two alloca() calls

That's why I asked "what's the actual bug case we're trying to handle?"...

thanks
-- PMM

author
#8 lore raw
In reply to Jann Horn
+-- On Fri, 3 Mar 2017, Jann Horn wrote --+
| On Fri, Mar 3, 2017 at 4:55 PM, Peter Maydell <[email protected]> wrote:
| >> +            if (argc > ARG_MAX || envc > ARG_MAX) {
| >> +                fprintf(stderr,
| >> +                        "argc(%d), envc(%d) exceed %d\n", argc, envc, ARG_MAX);
| >> +                ret = -TARGET_EFAULT;
| >> +                break;
| >> +            }
| >>              argp = alloca((argc + 1) * sizeof(void *));
| >>              envp = alloca((envc + 1) * sizeof(void *));
| >
| > This code is already supposed to handle "argument string too big",
| > see commit a6f79cc9a5e.
| >
| > What's the actual bug case we're trying to handle here?

Not argument string, but argument count too big leads to a bad address in get_user_ual(...), resulting in segfault.

Thank you.
--
Prasad J Pandit / Red Hat Product Security Team
47AF CE69 3A90 54AA 9045 1053 DD13 3D32 FE5B 041F