Limit and protect execve arguments
v2 cover Limit and protect execve arguments
From: Prasad J Pandit <[email protected]>
Hello,
A user program could pass large number of 'argv','env' arguments to an execve(2) call. It could lead to bad behaviour as the TARGET_NR_execve:
allocates stack memory(via alloca) for these arguments. alloca(3) is better for allocations of upto one page(4KB) of stack memory. As anything more could smash stack protectors in place.
This patch(v2) set attempts to fix these issues.
-> https://lists.gnu.org/archive/html/qemu-devel/2017-03/msg00750.html
Thank you.
--
Prasad J Pandit (2):
linux-user: limit number of arguments to execve
linux-user: allocate heap memory for execve arguments
linux-user/syscall.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)
--
2.9.3
PATCH v2 1/2 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 | 6 ++++++ 1 file changed, 6 insertions(+)
Update per: use gemu_log() to report error
-> https://lists.gnu.org/archive/html/qemu-devel/2017-03/msg00750.html
linux-user/syscall.c +6 -0
@@ -7766,6 +7766,7 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,#endif case TARGET_NR_execve: { +#define ARG_MAX 65535char **argp, **envp;int argc, envc;abi_ulong gp;
@@ -7794,6 +7795,11 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1, envc++; } +if (argc >ARG_MAX || envc >ARG_MAX ) { +gemu_log ("argc(%d), envc(%d) exceed %d\n" , argc, envc,ARG_MAX ); + ret = -TARGET_E2BIG ; +break ; + } argp =alloca ((argc +1 ) *sizeof (void *)); envp =alloca ((envc +1 ) *sizeof (void *));
Signature · 1 line
2.9.3
PATCH v2 2/2 linux-user: allocate heap memory for execve arguments
In reply to P J PFrom: Prasad J Pandit <[email protected]>
Arguments passed to execve(2) call from user program could be large, allocating stack memory for them via alloca(3) call would lead to bad behaviour. Use 'g_malloc0' to allocate memory for such arguments.
--- linux-user/syscall.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
Update per: replace alloca() with g_malloc0()
-> https://lists.gnu.org/archive/html/qemu-devel/2017-03/msg00750.html
linux-user/syscall.c +5 -2
@@ -7800,8 +7800,8 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1, ret = -TARGET_E2BIG ;break ; } - argp =alloca ((argc +1 ) *sizeof (void *)); - envp =alloca ((envc +1 ) *sizeof (void *)); + argp =g_malloc0 ((argc +1 ) *sizeof (void *)); + envp =g_malloc0 ((envc +1 ) *sizeof (void *));for (gp = guest_argp, q = argp; gp; gp +=sizeof (abi_ulong), q++) {
@@ -7862,6 +7862,9 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,break ;unlock_user (*q, addr,0 ); } + +g_free (argp); +g_free (envp); }break ;case TARGET_NR_chdir:
Signature · 1 line
2.9.3
Re: [Qemu-devel] [PATCH v2 1/2] linux-user: limit number of arguments to execve
In reply to P J PQuoted 28 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.
Reported-by Jann Horn <[email protected]>Signed-off-by Prasad J Pandit <[email protected]>--- linux-user/syscall.c | 6 ++++++ 1 file changed, 6 insertions(+)Update per: use
gemu_log()to report error
-> https://lists.gnu.org/archive/html/qemu-devel/2017-03/msg00750.htmllinux-user/syscall.c +1
-0@@ -7766,6 +7766,7 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,#endif case TARGET_NR_execve: { +#define ARG_MAX 65535char **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 7804
Quoted 10 lines
linux-user/syscall.c +5
-0@@ -7794,6 +7795,11 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1, envc++; } +if (argc >ARG_MAX || envc >ARG_MAX ) { +gemu_log ("argc(%d), envc(%d) exceed %d\n" , argc, envc,ARG_MAX ); + ret = -TARGET_E2BIG ; +break ; + } argp =alloca ((argc +1 ) *sizeof (void *)); envp =alloca ((envc +1 ) *sizeof (void *));
We need to fix this by not using alloca(), not by imposing an arbitrary limit that's still rather over-large for an alloca allocation, as Eric suggested.
thanks
-- PMM
Re: [Qemu-devel] [PATCH v2 2/2] linux-user: allocate heap memory for execve arguments
In reply to P J PQuoted 11 lines · P J P
From: Prasad J Pandit <[email protected]>
Arguments passed to execve(2) call from user program could be large, allocating stack memory for them via alloca(3) call would lead to bad behaviour. Use '
g_malloc0' to allocate memory for such arguments.Signed-off-by Prasad J Pandit <[email protected]>--- linux-user/syscall.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
Is this patch alone (without 1/2) sufficient to solve the problem? If so, then drop 1/2.
linux-user/syscall.c abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
Quoted 9 lines
Update per: replace
alloca()withg_malloc0()
-> https://lists.gnu.org/archive/html/qemu-devel/2017-03/msg00750.htmllinux-user/syscall.c +0
-0@@ -7800,8 +7800,8 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
What version of qemu are you patching? Line 7800 of current master is nowhere near 'case TARGET_NR_execve:' (line 7899)
linux-user/syscall.c abi_long do_syscall(void *cpu_env, int num, abi_long arg1, · line 7802
Quoted 3 lines
linux-user/syscall.c +0
-0ret = -TARGET_E2BIG ;break ; }
and current master has 'goto efault' rather than directly setting ret at this point.
linux-user/syscall.c abi_long do_syscall(void *cpu_env, int num, abi_long arg1, · line 7804
Quoted 4 lines
linux-user/syscall.c +2
-2- argp =alloca ((argc +1 ) *sizeof (void *)); - envp =alloca ((envc +1 ) *sizeof (void *)); + argp =g_malloc0 ((argc +1 ) *sizeof (void *)); + envp =g_malloc0 ((envc +1 ) *sizeof (void *));
Subject to a potential multiplication overflow. I'd prefer:
g_new0(void *, argc + 1);
as that is guaranteed to not overflow.
Quoted 14 lines
linux-user/syscall.c +3
-0for (gp = guest_argp, q = argp; gp; gp +=sizeof (abi_ulong), q++) {@@ -7862,6 +7862,9 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,break ;unlock_user (*q, addr,0 ); } + +g_free (argp); +g_free (envp); }break ;case TARGET_NR_chdir:
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH v2 1/2] linux-user: limit number of arguments to execve
In reply to Peter MaydellQuoted 23 lines · Peter Maydell
Quoted 22 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.
Reported-by Jann Horn <[email protected]>Signed-off-by Prasad J Pandit <[email protected]>--- linux-user/syscall.c | 6 ++++++ 1 file changed, 6 insertions(+){linux-user/syscall.c +1
-0+#define ARG_MAX 65535char **argp, **envp;int argc, envc;abi_ulong gp;@@ -7794,6 +7795,11 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1, envc++; }
Quoted 12 lines
linux-user/syscall.c abi_long do_syscall(void *cpu_env, int num, abi_long arg1, · line 7804
Quoted 7 lines
linux-user/syscall.c +5
-0+if (argc >ARG_MAX || envc >ARG_MAX ) { +gemu_log ("argc(%d), envc(%d) exceed %d\n" , argc, envc,ARG_MAX ); + ret = -TARGET_E2BIG ; +break ; + } argp =alloca ((argc +1 ) *sizeof (void *)); envp =alloca ((envc +1 ) *sizeof (void *));We need to fix this by not using
alloca(), not by imposing an arbitrary limit that's still rather over-large for an alloca allocation, as Eric suggested.
And patch 2/2 does that. Does that patch in isolation fix the problem? In which case, we don't want this patch.
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH v2 2/2] linux-user: allocate heap memory for execve arguments
In reply to P J PQuoted 31 lines · P J P
From: Prasad J Pandit <[email protected]>
Arguments passed to execve(2) call from user program could be large, allocating stack memory for them via alloca(3) call would lead to bad behaviour. Use '
g_malloc0' to allocate memory for such arguments.Signed-off-by Prasad J Pandit <[email protected]>--- linux-user/syscall.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)Update per: replace
alloca()withg_malloc0()
-> https://lists.gnu.org/archive/html/qemu-devel/2017-03/msg00750.htmllinux-user/syscall.c +2
-2@@ -7800,8 +7800,8 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1, ret = -TARGET_E2BIG ;break ; } - argp =alloca ((argc +1 ) *sizeof (void *)); - envp =alloca ((envc +1 ) *sizeof (void *)); + argp =g_malloc0 ((argc +1 ) *sizeof (void *)); + envp =g_malloc0 ((envc +1 ) *sizeof (void *));for (gp = guest_argp, q = argp; gp; gp +=sizeof (abi_ulong), q++) {
linux-user/syscall.c abi_long do_syscall(void *cpu_env, int num, abi_long arg1, · line 7870
Quoted 9 lines
linux-user/syscall.c +3
-0@@ -7862,6 +7862,9 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,break ;unlock_user (*q, addr,0 ); } + +g_free (argp); +g_free (envp); }break ;case TARGET_NR_chdir:
Better to use the _try_ glib allocation functions and fail the syscall on allocation failure, maybe?
thanks
-- PMM
Re: [Qemu-devel] [PATCH v2 2/2] linux-user: allocate heap memory for execve arguments
In reply to Eric BlakeQuoted 29 lines · Eric Blake
Quoted 11 lines · P J P
From: Prasad J Pandit <[email protected]>
Arguments passed to execve(2) call from user program could be large, allocating stack memory for them via alloca(3) call would lead to bad behaviour. Use '
g_malloc0' to allocate memory for such arguments.Signed-off-by Prasad J Pandit <[email protected]>--- linux-user/syscall.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)Is this patch alone (without 1/2) sufficient to solve the problem? If so, then drop 1/2.
linux-user/syscall.c abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
Quoted 9 lines
Update per: replace
alloca()withg_malloc0()
-> https://lists.gnu.org/archive/html/qemu-devel/2017-03/msg00750.htmllinux-user/syscall.c +0
-0@@ -7800,8 +7800,8 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,What version of qemu are you patching? Line 7800 of current master is nowhere near 'case
TARGET_NR_execve:' (line 7899)
Quoted 6 lines
linux-user/syscall.c abi_long do_syscall(void *cpu_env, int num, abi_long arg1, · line 7802
Quoted 3 lines
linux-user/syscall.c +0
-0ret = -TARGET_E2BIG ;break ; }and current master has 'goto efault' rather than directly setting ret at this point.
Okay, I see that this context came from patch 1/2. Sorry for the noise (I was trying to review this patch in isolation, since I've already argued that 1/2 is probably not necessary).
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH v2 2/2] linux-user: allocate heap memory for execve arguments
In reply to Eric Blake+-- On Mon, 6 Mar 2017, Eric Blake wrote --+ | On 03/06/2017 01:17 AM, P J P wrote: | > Arguments passed to execve(2) call from user program could | > be large, allocating stack memory for them via alloca(3) call | > would lead to bad behaviour. Use 'g_malloc0' to allocate memory | > for such arguments. | > | > Signed-off-by: Prasad J Pandit <[email protected]> | > --- | > linux-user/syscall.c | 7 +++++-- | > 1 file changed, 5 insertions(+), 2 deletions(-) | | Is this patch alone (without 1/2) sufficient to solve the problem? If | so, then drop 1/2.
Yes, it seems to fix the issue. Still I think having ARG_MAX limit would be
good, as system exec(3) routines too impose _SC_ARG_MAX limit. I'll send a revised patch with 'g_try_new' call instead of g_malloc0.
Thank you.
--
Prasad J Pandit / Red Hat Product Security Team
47AF CE69 3A90 54AA 9045 1053 DD13 3D32 FE5B 041F
Re: [Qemu-devel] [PATCH v2 2/2] linux-user: allocate heap memory for execve arguments
Quoted 15 lines · P J P
+-- On Mon, 6 Mar 2017, Eric Blake wrote --+ | On 03/06/2017 01:17 AM, P J P wrote: | > Arguments passed to execve(2) call from user program could | > be large, allocating stack memory for them via alloca(3) call | > would lead to bad behaviour. Use 'g_malloc0' to allocate memory | > for such arguments. | > | > Signed-off-by: Prasad J Pandit <[email protected]> | > --- | > linux-user/syscall.c | 7 +++++-- | > 1 file changed, 5 insertions(+), 2 deletions(-) | | Is this patch alone (without 1/2) sufficient to solve the problem? If | so, then drop 1/2.
Quoted 3 lines
Yes, it seems to fix the issue. Still I think having
ARG_MAXlimit would be
good, as system exec(3) routines too impose_SC_ARG_MAXlimit. I'll send a revised patch with 'g_try_new' call instead ofg_malloc0.
If you impose any limit smaller than _SC_ARG_MAX, you are needlessly limiting things. Furthermore, _SC_ARG_MAX may not always be the same value, depending on how the kernel was compiled. So it's probably asiest to just let execve() impose its own limits (and correctly report errors to the caller when execve() fails), rather than trying to impose limits yourself.
In short, the bug that you are fixing is not caused by the guest requesting something beyond execve() limits, but caused by poor use of alloca() leading to a stack overrun. You only need to fix the bug (by switching alloca() to heap allocation), not introduce additional ones (by imposing arbitrary, and probably wrong, limits).
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH v2 2/2] linux-user: allocate heap memory for execve arguments
+-- On Mon, 6 Mar 2017, Eric Blake wrote --+ | If you impose any limit smaller than _SC_ARG_MAX, you are needlessly | limiting things. Furthermore, _SC_ARG_MAX may not always be the same | value, depending on how the kernel was compiled. So it's probably | asiest to just let execve() impose its own limits (and correctly report | errors to the caller when execve() fails), rather than trying to impose | limits yourself.
Okay.
| In short, the bug that you are fixing is not caused by the guest | requesting something beyond execve() limits, but caused by poor use of | alloca() leading to a stack overrun.
Yes, true.
| You only need to fix the bug (by switching alloca() to heap allocation),
Okay, I'll send just one revised patch.
Thank you.
--
Prasad J Pandit / Red Hat Product Security Team
47AF CE69 3A90 54AA 9045 1053 DD13 3D32 FE5B 041F