proc: prevent accessing /proc/<PID>/environ until it's ready
diff mbox series

Message ID 1461870258-17970-1-git-send-email-minipli@googlemail.com
State New, archived
Headers show
Series
  • proc: prevent accessing /proc/<PID>/environ until it's ready
Related show

Commit Message

Mathias Krause April 28, 2016, 7:04 p.m. UTC
If /proc/<PID>/environ gets read before the envp[] array is fully set
up in create_{aout,elf,elf_fdpic,flat}_tables(), we might end up trying
to read more bytes than are actually written, as env_start will already
be set but env_end will still be zero, making the range calculation
underflow, allowing to read beyond the end of what has been written.

Fix this as it is done for /proc/<PID>/cmdline by testing env_end for
zero. It is, apparently, intentionally set last in create_*_tables().

This bug was found by the PaX size_overflow plugin that detected the
arithmetic underflow of 'this_len = env_end - (env_start + src)' when
env_end is still zero.

Reported-at: https://forums.grsecurity.net/viewtopic.php?f=3&t=4363
Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=116461
Signed-off-by: Mathias Krause <minipli@googlemail.com>
Cc: Emese Revfy <re.emese@gmail.com>
Cc: Pax Team <pageexec@freemail.hu>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: Mateusz Guzik <mguzik@redhat.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Cyrill Gorcunov <gorcunov@openvz.org>
Cc: Jarod Wilson <jarod@redhat.com>
---
 fs/proc/base.c |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Comments

Mateusz Guzik April 28, 2016, 7:20 p.m. UTC | #1
On Thu, Apr 28, 2016 at 09:04:18PM +0200, Mathias Krause wrote:
> If /proc/<PID>/environ gets read before the envp[] array is fully set
> up in create_{aout,elf,elf_fdpic,flat}_tables(), we might end up trying
> to read more bytes than are actually written, as env_start will already
> be set but env_end will still be zero, making the range calculation
> underflow, allowing to read beyond the end of what has been written.
> 
> Fix this as it is done for /proc/<PID>/cmdline by testing env_end for
> zero. It is, apparently, intentionally set last in create_*_tables().
> 
> This bug was found by the PaX size_overflow plugin that detected the
> arithmetic underflow of 'this_len = env_end - (env_start + src)' when
> env_end is still zero.
> 
> Reported-at: https://forums.grsecurity.net/viewtopic.php?f=3&t=4363
> Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=116461
> ---
>  fs/proc/base.c |    3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/fs/proc/base.c b/fs/proc/base.c
> index 4f764c2ac1a5..45f2162e55b2 100644
> --- a/fs/proc/base.c
> +++ b/fs/proc/base.c
> @@ -955,7 +955,8 @@ static ssize_t environ_read(struct file *file, char __user *buf,
>  	struct mm_struct *mm = file->private_data;
>  	unsigned long env_start, env_end;
>  
> -	if (!mm)
> +	/* Ensure the process spawned far enough to have an environment. */
> +	if (!mm || !mm->env_end)
>  		return 0;
>  
>  	page = (char *)__get_free_page(GFP_TEMPORARY);

In this case get_cmdline in mm/util.c should also be patched for
completness. It tests for arg_end, but later accesses env_end.
Cyrill Gorcunov April 28, 2016, 7:28 p.m. UTC | #2
On Thu, Apr 28, 2016 at 09:04:18PM +0200, Mathias Krause wrote:
> If /proc/<PID>/environ gets read before the envp[] array is fully set
> up in create_{aout,elf,elf_fdpic,flat}_tables(), we might end up trying
> to read more bytes than are actually written, as env_start will already
> be set but env_end will still be zero, making the range calculation
> underflow, allowing to read beyond the end of what has been written.
> 
> Fix this as it is done for /proc/<PID>/cmdline by testing env_end for
> zero. It is, apparently, intentionally set last in create_*_tables().
> 
> This bug was found by the PaX size_overflow plugin that detected the
> arithmetic underflow of 'this_len = env_end - (env_start + src)' when
> env_end is still zero.
> 
> Reported-at: https://forums.grsecurity.net/viewtopic.php?f=3&t=4363
> Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=116461
> Signed-off-by: Mathias Krause <minipli@googlemail.com>
> Cc: Emese Revfy <re.emese@gmail.com>
> Cc: Pax Team <pageexec@freemail.hu>
> Cc: Al Viro <viro@zeniv.linux.org.uk>
> Cc: Mateusz Guzik <mguzik@redhat.com>
> Cc: Alexey Dobriyan <adobriyan@gmail.com>
> Cc: Cyrill Gorcunov <gorcunov@openvz.org>
> Cc: Jarod Wilson <jarod@redhat.com>
> ---
>  fs/proc/base.c |    3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/fs/proc/base.c b/fs/proc/base.c
> index 4f764c2ac1a5..45f2162e55b2 100644
> --- a/fs/proc/base.c
> +++ b/fs/proc/base.c
> @@ -955,7 +955,8 @@ static ssize_t environ_read(struct file *file, char __user *buf,
>  	struct mm_struct *mm = file->private_data;
>  	unsigned long env_start, env_end;
>  
> -	if (!mm)
> +	/* Ensure the process spawned far enough to have an environment. */
> +	if (!mm || !mm->env_end)
>  		return 0;

At least in proc_pid_cmdline_read such test is done.
Acked-by: Cyrill Gorcunov <gorcunov@openvz.org>
Mathias Krause April 28, 2016, 7:36 p.m. UTC | #3
On 28 April 2016 at 21:20, Mateusz Guzik <mguzik@redhat.com> wrote:
> In this case get_cmdline in mm/util.c should also be patched for
> completness. It tests for arg_end, but later accesses env_end.

But it'll do this only when argv[] was modified from what the kernel
initially wrote, which, in turn, either requires the process to have
started executing and messing with it's own argv[] or another process
poking at it via ptrace(). In the former case env_end will be non-zero
already and I don't know if the latter case is actually possible, i.e.
if one can already attach to a process this early. If one can, then
yes, that place needs to be modified, too.

Thanks,
Mathias
Andrew Morton April 28, 2016, 9:26 p.m. UTC | #4
On Thu, 28 Apr 2016 21:04:18 +0200 Mathias Krause <minipli@googlemail.com> wrote:

> If /proc/<PID>/environ gets read before the envp[] array is fully set
> up in create_{aout,elf,elf_fdpic,flat}_tables(), we might end up trying
> to read more bytes than are actually written, as env_start will already
> be set but env_end will still be zero, making the range calculation
> underflow, allowing to read beyond the end of what has been written.
> 
> Fix this as it is done for /proc/<PID>/cmdline by testing env_end for
> zero. It is, apparently, intentionally set last in create_*_tables().
> 
> This bug was found by the PaX size_overflow plugin that detected the
> arithmetic underflow of 'this_len = env_end - (env_start + src)' when
> env_end is still zero.

So what are the implications of this?  From my reading, a craftily
constructed application could occasionally read arbitrarily large
amounts of kernel memory?
Andrew Morton April 28, 2016, 9:30 p.m. UTC | #5
On Thu, 28 Apr 2016 21:04:18 +0200 Mathias Krause <minipli@googlemail.com> wrote:

> If /proc/<PID>/environ gets read before the envp[] array is fully set
> up in create_{aout,elf,elf_fdpic,flat}_tables(), we might end up trying
> to read more bytes than are actually written, as env_start will already
> be set but env_end will still be zero, making the range calculation
> underflow, allowing to read beyond the end of what has been written.
> 
> Fix this as it is done for /proc/<PID>/cmdline by testing env_end for
> zero. It is, apparently, intentionally set last in create_*_tables().

Also, if this is indeed our design then

a) the various create_*_tables() should have comments in there which
   explain this subtlety to the reader.  Or, better, they use a common
   helper function for this readiness-signaling operation because..

b) we'll need some barriers there to ensure that the environ_read()
   caller sees the create_*_tables() writes in the correct order.

> This bug was found by the PaX size_overflow plugin that detected the
> arithmetic underflow of 'this_len = env_end - (env_start + src)' when
> env_end is still zero.
Mathias Krause April 29, 2016, 5:59 a.m. UTC | #6
On 28 April 2016 at 23:26, Andrew Morton <akpm@linux-foundation.org> wrote:
> On Thu, 28 Apr 2016 21:04:18 +0200 Mathias Krause <minipli@googlemail.com> wrote:
>
>> If /proc/<PID>/environ gets read before the envp[] array is fully set
>> up in create_{aout,elf,elf_fdpic,flat}_tables(), we might end up trying
>> to read more bytes than are actually written, as env_start will already
>> be set but env_end will still be zero, making the range calculation
>> underflow, allowing to read beyond the end of what has been written.
>>
>> Fix this as it is done for /proc/<PID>/cmdline by testing env_end for
>> zero. It is, apparently, intentionally set last in create_*_tables().
>>
>> This bug was found by the PaX size_overflow plugin that detected the
>> arithmetic underflow of 'this_len = env_end - (env_start + src)' when
>> env_end is still zero.
>
> So what are the implications of this?  From my reading, a craftily
> constructed application could occasionally read arbitrarily large
> amounts of kernel memory?

I don't think access_remote_vm() is capable of that. So, the only
consequence is, userland trying to access /proc/<PID>/environ of a not
yet fully set up process may get inconsistent data as we're in the
middle of copying in the environment variables.

Regards,
Mathias
Mathias Krause April 29, 2016, 6:02 a.m. UTC | #7
On 28 April 2016 at 23:30, Andrew Morton <akpm@linux-foundation.org> wrote:
> On Thu, 28 Apr 2016 21:04:18 +0200 Mathias Krause <minipli@googlemail.com> wrote:
>
>> If /proc/<PID>/environ gets read before the envp[] array is fully set
>> up in create_{aout,elf,elf_fdpic,flat}_tables(), we might end up trying
>> to read more bytes than are actually written, as env_start will already
>> be set but env_end will still be zero, making the range calculation
>> underflow, allowing to read beyond the end of what has been written.
>>
>> Fix this as it is done for /proc/<PID>/cmdline by testing env_end for
>> zero. It is, apparently, intentionally set last in create_*_tables().
>
> Also, if this is indeed our design then
>
> a) the various create_*_tables() should have comments in there which
>    explain this subtlety to the reader.  Or, better, they use a common
>    helper function for this readiness-signaling operation because..
>
> b) we'll need some barriers there to ensure that the environ_read()
>    caller sees the create_*_tables() writes in the correct order.

I totally agree that this kind of "synchronization" is rather fragile.
Adding comments won't help much, I fear. Rather a dedicated flag,
signaling "process ready for inspection" may be needed. So far, that's
what env_end is (ab-)used for.

Regards,
Mathias
Alexey Dobriyan April 29, 2016, 10:11 a.m. UTC | #8
On Thu, Apr 28, 2016 at 10:20 PM, Mateusz Guzik <mguzik@redhat.com> wrote:
> On Thu, Apr 28, 2016 at 09:04:18PM +0200, Mathias Krause wrote:
>> If /proc/<PID>/environ gets read before the envp[] array is fully set
>> up in create_{aout,elf,elf_fdpic,flat}_tables(), we might end up trying
>> to read more bytes than are actually written, as env_start will already
>> be set but env_end will still be zero, making the range calculation
>> underflow, allowing to read beyond the end of what has been written.
>>
>> Fix this as it is done for /proc/<PID>/cmdline by testing env_end for
>> zero. It is, apparently, intentionally set last in create_*_tables().
>>
>> This bug was found by the PaX size_overflow plugin that detected the
>> arithmetic underflow of 'this_len = env_end - (env_start + src)' when
>> env_end is still zero.
>>
>> Reported-at: https://forums.grsecurity.net/viewtopic.php?f=3&t=4363
>> Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=116461
>> ---
>>  fs/proc/base.c |    3 ++-
>>  1 file changed, 2 insertions(+), 1 deletion(-)
>>
>> diff --git a/fs/proc/base.c b/fs/proc/base.c
>> index 4f764c2ac1a5..45f2162e55b2 100644
>> --- a/fs/proc/base.c
>> +++ b/fs/proc/base.c
>> @@ -955,7 +955,8 @@ static ssize_t environ_read(struct file *file, char __user *buf,
>>       struct mm_struct *mm = file->private_data;
>>       unsigned long env_start, env_end;
>>
>> -     if (!mm)
>> +     /* Ensure the process spawned far enough to have an environment. */
>> +     if (!mm || !mm->env_end)
>>               return 0;
>>
>>       page = (char *)__get_free_page(GFP_TEMPORARY);
>
> In this case get_cmdline in mm/util.c should also be patched for
> completness. It tests for arg_end, but later accesses env_end.

Sort of. get_cmdline() is only really used in audit code applied
to an exiting process which has cmdline setup long ago.

Should have rewrote /proc/*/environ as well... :-(

    Alexey
Alexey Dobriyan April 29, 2016, 10:25 a.m. UTC | #9
On Fri, Apr 29, 2016 at 9:02 AM, Mathias Krause <minipli@googlemail.com> wrote:
> On 28 April 2016 at 23:30, Andrew Morton <akpm@linux-foundation.org> wrote:
>> On Thu, 28 Apr 2016 21:04:18 +0200 Mathias Krause <minipli@googlemail.com> wrote:
>>
>>> If /proc/<PID>/environ gets read before the envp[] array is fully set
>>> up in create_{aout,elf,elf_fdpic,flat}_tables(), we might end up trying
>>> to read more bytes than are actually written, as env_start will already
>>> be set but env_end will still be zero, making the range calculation
>>> underflow, allowing to read beyond the end of what has been written.
>>>
>>> Fix this as it is done for /proc/<PID>/cmdline by testing env_end for
>>> zero. It is, apparently, intentionally set last in create_*_tables().
>>
>> Also, if this is indeed our design then
>>
>> a) the various create_*_tables() should have comments in there which
>>    explain this subtlety to the reader.  Or, better, they use a common
>>    helper function for this readiness-signaling operation because..
>>
>> b) we'll need some barriers there to ensure that the environ_read()
>>    caller sees the create_*_tables() writes in the correct order.
>
> I totally agree that this kind of "synchronization" is rather fragile.
> Adding comments won't help much, I fear. Rather a dedicated flag,
> signaling "process ready for inspection" may be needed. So far, that's
> what env_end is (ab-)used for.

If MM Cabal is OK with MMF_LOAD_BINARY_OK flag
applied at search_binary_handler(), it should work for /proc .

    Alexey

Patch
diff mbox series

diff --git a/fs/proc/base.c b/fs/proc/base.c
index 4f764c2ac1a5..45f2162e55b2 100644
--- a/fs/proc/base.c
+++ b/fs/proc/base.c
@@ -955,7 +955,8 @@  static ssize_t environ_read(struct file *file, char __user *buf,
 	struct mm_struct *mm = file->private_data;
 	unsigned long env_start, env_end;
 
-	if (!mm)
+	/* Ensure the process spawned far enough to have an environment. */
+	if (!mm || !mm->env_end)
 		return 0;
 
 	page = (char *)__get_free_page(GFP_TEMPORARY);