linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* don't let mmap allocate down to zero
@ 2005-01-26 16:18 Rik van Riel
  2005-01-26 16:38 ` linux-os
  2005-01-26 17:25 ` William Lee Irwin III
  0 siblings, 2 replies; 40+ messages in thread
From: Rik van Riel @ 2005-01-26 16:18 UTC (permalink / raw)
  To: Andrew Morton; +Cc: linux-kernel, James Antill, Bryn Reeves

With some programs the 2.6 kernel can end up allocating memory
at address zero, for a non-MAP_FIXED mmap call!  This causes
problems with some programs and is generally rude to do. This
simple patch fixes the problem in my tests.

Make sure that we don't allocate memory all the way down to zero,
so the NULL pointer never gets covered up with anonymous memory
and we don't end up violating the C standard.

Signed-off-by: Rik van Riel <riel@redhat.com>

--- linux-2.6.9/mm/mmap.c.nullmmap	2005-01-25 18:00:26.000000000 -0500
+++ linux-2.6.9/mm/mmap.c	2005-01-26 08:48:03.438701673 -0500
@@ -1114,6 +1114,8 @@ void arch_unmap_area(struct vm_area_stru
  		area->vm_mm->free_area_cache = area->vm_start;
  }

+#define SHLIB_BASE             0x00111000
+
  /*
   * This mmap-allocator allocates new areas top-down from below the
   * stack's low limit (the base):
@@ -1162,6 +1164,13 @@ try_again:
  			return addr;

  		/*
+		 * Make sure we don't allocate all the way down to
+		 * zero, which would break NULL pointer detection.
+		 */
+		if (addr < SHLIB_BASE)
+			goto fail;
+
+		/*
  		 * new region fits between prev_vma->vm_end and
  		 * vma->vm_start, use it:
  		 */
@@ -1258,8 +1267,6 @@ get_unmapped_area_prot(struct file *file
  EXPORT_SYMBOL(get_unmapped_area_prot);


-#define SHLIB_BASE             0x00111000
-
  unsigned long arch_get_unmapped_exec_area(struct file *filp, unsigned long 
addr0,
  		unsigned long len0, unsigned long pgoff, unsigned long flags)
  {

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: don't let mmap allocate down to zero
  2005-01-26 16:18 don't let mmap allocate down to zero Rik van Riel
@ 2005-01-26 16:38 ` linux-os
  2005-01-26 17:05   ` Sytse Wielinga
                     ` (6 more replies)
  2005-01-26 17:25 ` William Lee Irwin III
  1 sibling, 7 replies; 40+ messages in thread
From: linux-os @ 2005-01-26 16:38 UTC (permalink / raw)
  To: Rik van Riel; +Cc: Andrew Morton, linux-kernel, James Antill, Bryn Reeves

On Wed, 26 Jan 2005, Rik van Riel wrote:

> With some programs the 2.6 kernel can end up allocating memory
> at address zero, for a non-MAP_FIXED mmap call!  This causes
> problems with some programs and is generally rude to do. This
> simple patch fixes the problem in my tests.

Does this mean that we can't mmap the screen regen buffer at
0x000b8000 anymore?

How do I look at the real-mode interrupt table starting at
offset 0? You know that the return value of mmap is to be
checked for MAP_FAILED, not for NULL, don't you?

What 'C' standard do you refer to? Seg-faults on null pointers
have nothing to do with the 'C' standard and everything to
do with the platform.

I don't think you should apply this patch. It puts "your"
policy in the kernel. Now, your policy might be "good" for
you, but the kernel is not supposed to have such policy
inside.

>
> Make sure that we don't allocate memory all the way down to zero,
> so the NULL pointer never gets covered up with anonymous memory
> and we don't end up violating the C standard.
>
> Signed-off-by: Rik van Riel <riel@redhat.com>
>
> --- linux-2.6.9/mm/mmap.c.nullmmap	2005-01-25 18:00:26.000000000 -0500
> +++ linux-2.6.9/mm/mmap.c	2005-01-26 08:48:03.438701673 -0500
> @@ -1114,6 +1114,8 @@ void arch_unmap_area(struct vm_area_stru
> 		area->vm_mm->free_area_cache = area->vm_start;
> }
>
> +#define SHLIB_BASE             0x00111000
> +
> /*
>  * This mmap-allocator allocates new areas top-down from below the
>  * stack's low limit (the base):
> @@ -1162,6 +1164,13 @@ try_again:
> 			return addr;
>
> 		/*
> +		 * Make sure we don't allocate all the way down to
> +		 * zero, which would break NULL pointer detection.
> +		 */
> +		if (addr < SHLIB_BASE)
> +			goto fail;
> +
> +		/*
> 		 * new region fits between prev_vma->vm_end and
> 		 * vma->vm_start, use it:
> 		 */
> @@ -1258,8 +1267,6 @@ get_unmapped_area_prot(struct file *file
> EXPORT_SYMBOL(get_unmapped_area_prot);
>
>
> -#define SHLIB_BASE             0x00111000
> -
> unsigned long arch_get_unmapped_exec_area(struct file *filp, unsigned long 
> addr0,
> 		unsigned long len0, unsigned long pgoff, unsigned long flags)
> {
> -
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at  http://www.tux.org/lkml/
>

Cheers,
Dick Johnson
Penguin : Linux version 2.6.10 on an i686 machine (5537.79 BogoMips).
  Notice : All mail here is now cached for review by Dictator Bush.
                  98.36% of all statistics are fiction.

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: don't let mmap allocate down to zero
  2005-01-26 16:38 ` linux-os
@ 2005-01-26 17:05   ` Sytse Wielinga
  2005-01-26 17:34   ` Chris Friesen
                     ` (5 subsequent siblings)
  6 siblings, 0 replies; 40+ messages in thread
From: Sytse Wielinga @ 2005-01-26 17:05 UTC (permalink / raw)
  To: linux-os
  Cc: Rik van Riel, Andrew Morton, linux-kernel, James Antill, Bryn Reeves

On Wed, Jan 26, 2005 at 11:38:15AM -0500, linux-os wrote:
> On Wed, 26 Jan 2005, Rik van Riel wrote:
> 
> >With some programs the 2.6 kernel can end up allocating memory
> >at address zero, for a non-MAP_FIXED mmap call!  This causes
> >problems with some programs and is generally rude to do. This
> >simple patch fixes the problem in my tests.
> 
> Does this mean that we can't mmap the screen regen buffer at
> 0x000b8000 anymore?

If you would have looked inside mmap.c, you would have seen that his check
is executed *after* trying for a specific address if it was given. Mmapping
0x000b8000 should still work. I don't know if this patch was very clean (it
probably isn't) but what it's supposed to do is only fail if no specific
address has been given to it.

> How do I look at the real-mode interrupt table starting at
> offset 0? You know that the return value of mmap is to be
> checked for MAP_FAILED, not for NULL, don't you?
> 
> What 'C' standard do you refer to? Seg-faults on null pointers
> have nothing to do with the 'C' standard and everything to
> do with the platform.

Oh come on. Every normal program checks whether a variable has been allocated
or not by comparing it to NULL. I have no knowledge of the internals of glibc
though, and wouldn't know whether this should be handled inside the kernel or
if having it checked in glibc and userspace programs that use mmap directly
should be enough, but AFAIK every C coder assumes that NULL pointers point to
nothing.

    Sytse

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: don't let mmap allocate down to zero
  2005-01-26 16:18 don't let mmap allocate down to zero Rik van Riel
  2005-01-26 16:38 ` linux-os
@ 2005-01-26 17:25 ` William Lee Irwin III
  2005-01-27  5:09   ` William Lee Irwin III
  1 sibling, 1 reply; 40+ messages in thread
From: William Lee Irwin III @ 2005-01-26 17:25 UTC (permalink / raw)
  To: Rik van Riel; +Cc: Andrew Morton, linux-kernel, James Antill, Bryn Reeves

On Wed, Jan 26, 2005 at 11:18:08AM -0500, Rik van Riel wrote:
> With some programs the 2.6 kernel can end up allocating memory
> at address zero, for a non-MAP_FIXED mmap call!  This causes
> problems with some programs and is generally rude to do. This
> simple patch fixes the problem in my tests.
> Make sure that we don't allocate memory all the way down to zero,
> so the NULL pointer never gets covered up with anonymous memory
> and we don't end up violating the C standard.
> Signed-off-by: Rik van Riel <riel@redhat.com>

SHLIB_BASE does not appear to be present in 2.6.9; perhaps something
else is going on.

I think we are better off:
	(a) checking for hitting zero explicitly as opposed to
		enforcing a randomly-chosen lower limit for addresses
	(b) enforcing vma allocation above FIRST_USER_PGD_NR*PGDIR_SIZE,
		to which SHLIB_BASE bears no relation.


-- wli

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: don't let mmap allocate down to zero
  2005-01-26 16:38 ` linux-os
  2005-01-26 17:05   ` Sytse Wielinga
@ 2005-01-26 17:34   ` Chris Friesen
  2005-01-26 17:57     ` Bryn Reeves
  2005-01-26 18:10   ` Olivier Galibert
                     ` (4 subsequent siblings)
  6 siblings, 1 reply; 40+ messages in thread
From: Chris Friesen @ 2005-01-26 17:34 UTC (permalink / raw)
  To: linux-os
  Cc: Rik van Riel, Andrew Morton, linux-kernel, James Antill, Bryn Reeves

linux-os wrote:

> Does this mean that we can't mmap the screen regen buffer at
> 0x000b8000 anymore?
> 
> How do I look at the real-mode interrupt table starting at
> offset 0? You know that the return value of mmap is to be
> checked for MAP_FAILED, not for NULL, don't you?

Can't you still map those physical addresses to other virtual addresses?

> What 'C' standard do you refer to? Seg-faults on null pointers
> have nothing to do with the 'C' standard and everything to
> do with the platform.

I believe the ISO/IEC 9899:1999 C Standard explicitly states that 
dereferencing a null pointer with the unary * operator results in 
undefined behavior.

Chris

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: don't let mmap allocate down to zero
  2005-01-26 17:34   ` Chris Friesen
@ 2005-01-26 17:57     ` Bryn Reeves
  2005-01-26 18:37       ` linux-os
  2005-01-26 19:08       ` Chris Friesen
  0 siblings, 2 replies; 40+ messages in thread
From: Bryn Reeves @ 2005-01-26 17:57 UTC (permalink / raw)
  To: Chris Friesen
  Cc: linux-os, Rik van Riel, Andrew Morton, linux-kernel, James Antill

On Wed, 2005-01-26 at 17:34, Chris Friesen wrote: 
> linux-os wrote:
> 
> > Does this mean that we can't mmap the screen regen buffer at
> > 0x000b8000 anymore?
> > 
> > How do I look at the real-mode interrupt table starting at
> > offset 0? You know that the return value of mmap is to be
> > checked for MAP_FAILED, not for NULL, don't you?
> 
> Can't you still map those physical addresses to other virtual addresses?
> 

I think that's the case. The 0 address as refered to here is only for
the user virtual address space. 

> > What 'C' standard do you refer to? Seg-faults on null pointers
> > have nothing to do with the 'C' standard and everything to
> > do with the platform.
> 
> I believe the ISO/IEC 9899:1999 C Standard explicitly states that 
> dereferencing a null pointer with the unary * operator results in 
> undefined behavior.

Exactly. Undefined. VAX/UNIX allowed assignment to null pointers. BUT
it's now such a commonly held assumption that a null pointer is not
valid that things will break if this is changed. Doesn't glibc malloc
use mmap for small allocations? From the man page:

RETURN VALUE
  For calloc() and malloc(), the value returned is a pointer to the
  allocated  memory,  which  is suitably aligned for any kind of
  variable, or NULL if the request fails.

This could get pretty confusing if NULL was a valid address...

Cheers,

Bryn.

The DBX manual contrasts this behaviour on different systems:
http://acs.ucsd.edu/info/dbx.debug.php



^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: don't let mmap allocate down to zero
  2005-01-26 16:38 ` linux-os
  2005-01-26 17:05   ` Sytse Wielinga
  2005-01-26 17:34   ` Chris Friesen
@ 2005-01-26 18:10   ` Olivier Galibert
  2005-01-26 18:20     ` linux-os
  2005-01-26 19:41   ` Arjan van de Ven
                     ` (3 subsequent siblings)
  6 siblings, 1 reply; 40+ messages in thread
From: Olivier Galibert @ 2005-01-26 18:10 UTC (permalink / raw)
  To: linux-os
  Cc: Rik van Riel, Andrew Morton, linux-kernel, James Antill, Bryn Reeves

On Wed, Jan 26, 2005 at 11:38:15AM -0500, linux-os wrote:
> On Wed, 26 Jan 2005, Rik van Riel wrote:
> 
> >With some programs the 2.6 kernel can end up allocating memory
> >at address zero, for a non-MAP_FIXED mmap call!  This causes
> >problems with some programs and is generally rude to do. This
> >simple patch fixes the problem in my tests.
> 
> Does this mean that we can't mmap the screen regen buffer at
> 0x000b8000 anymore?

No.  Missed the "non-MAP_FIXED" part?  You can always map at 0, you
just have to ask for it.


> What 'C' standard do you refer to?

Malloc uses mmap to get more memory.  Malloc returning 0 means no
memory, not "the memory happens to be at 0".  Not that easy to fix in
the glibc if you want to keep the "segfault on null pointer accesses"
debugging help too.

Given that the man page itself says that unless you're using MAP_FIXED
start is only a hint and you should use 0 if you don't care things can
get real annoying real fast.  Imagine if you want to mmap a <4K file
and mmap then returns 0, i.e. NULL, as the mapping address as you
asked.  It's illegal from the point of view of susv3[1] and it's real
annoying in a C/C++ program.

  OG.

[1]
  When MAP_FIXED is not set, the implementation uses addr in an
  implementation-defined manner to arrive at pa. The pa so chosen
  shall be an area of the address space that the implementation deems
  suitable for a mapping of len bytes to the file. All implementations
  interpret an addr value of 0 as granting the implementation complete
  freedom in selecting pa, subject to constraints described below. A
  non-zero value of addr is taken to be a suggestion of a process
  address near which the mapping should be placed. When the
  implementation selects a value for pa, it never places a mapping at
  address 0, nor does it replace any extant mapping.

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: don't let mmap allocate down to zero
  2005-01-26 18:10   ` Olivier Galibert
@ 2005-01-26 18:20     ` linux-os
  2005-01-26 18:31       ` Olivier Galibert
  0 siblings, 1 reply; 40+ messages in thread
From: linux-os @ 2005-01-26 18:20 UTC (permalink / raw)
  To: Olivier Galibert
  Cc: Rik van Riel, Andrew Morton, linux-kernel, James Antill, Bryn Reeves

On Wed, 26 Jan 2005, Olivier Galibert wrote:

> On Wed, Jan 26, 2005 at 11:38:15AM -0500, linux-os wrote:
>> On Wed, 26 Jan 2005, Rik van Riel wrote:
>>
>>> With some programs the 2.6 kernel can end up allocating memory
>>> at address zero, for a non-MAP_FIXED mmap call!  This causes
>>> problems with some programs and is generally rude to do. This
>>> simple patch fixes the problem in my tests.
>>
>> Does this mean that we can't mmap the screen regen buffer at
>> 0x000b8000 anymore?
>
> No.  Missed the "non-MAP_FIXED" part?  You can always map at 0, you
> just have to ask for it.
>

Okay.

>
>> What 'C' standard do you refer to?
>
> Malloc uses mmap to get more memory.  Malloc returning 0 means no
> memory, not "the memory happens to be at 0".  Not that easy to fix in
> the glibc if you want to keep the "segfault on null pointer accesses"
> debugging help too.
>

malloc is a runtime library. It has its own documented rules.

> Given that the man page itself says that unless you're using MAP_FIXED
> start is only a hint and you should use 0 if you don't care things can
> get real annoying real fast.  Imagine if you want to mmap a <4K file
> and mmap then returns 0, i.e. NULL, as the mapping address as you
> asked.  It's illegal from the point of view of susv3[1] and it's real
> annoying in a C/C++ program.

mmap() can (will) return 0 if you use 0 as the hint and use MAP_FIXED
at 0. That's the reason why one does NOT check for NULL with mmap() but
for MAP_FAILED (which on this system is (void *)-1.

>
>  OG.
>
> [1]
>  When MAP_FIXED is not set, the implementation uses addr in an
>  implementation-defined manner to arrive at pa. The pa so chosen
>  shall be an area of the address space that the implementation deems
>  suitable for a mapping of len bytes to the file. All implementations
>  interpret an addr value of 0 as granting the implementation complete
>  freedom in selecting pa, subject to constraints described below. A
>  non-zero value of addr is taken to be a suggestion of a process
>  address near which the mapping should be placed. When the
>  implementation selects a value for pa, it never places a mapping at
>  address 0, nor does it replace any extant mapping.
>



Cheers,
Dick Johnson
Penguin : Linux version 2.6.10 on an i686 machine (5537.79 BogoMips).
  Notice : All mail here is now cached for review by Dictator Bush.
                  98.36% of all statistics are fiction.

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: don't let mmap allocate down to zero
  2005-01-26 18:20     ` linux-os
@ 2005-01-26 18:31       ` Olivier Galibert
  2005-01-26 18:39         ` linux-os
  0 siblings, 1 reply; 40+ messages in thread
From: Olivier Galibert @ 2005-01-26 18:31 UTC (permalink / raw)
  To: linux-os
  Cc: Rik van Riel, Andrew Morton, linux-kernel, James Antill, Bryn Reeves

On Wed, Jan 26, 2005 at 01:20:53PM -0500, linux-os wrote:
> On Wed, 26 Jan 2005, Olivier Galibert wrote:
> >Given that the man page itself says that unless you're using MAP_FIXED
> >start is only a hint and you should use 0 if you don't care things can
> >get real annoying real fast.  Imagine if you want to mmap a <4K file
> >and mmap then returns 0, i.e. NULL, as the mapping address as you
> >asked.  It's illegal from the point of view of susv3[1] and it's real
> >annoying in a C/C++ program.
> 
> mmap() can (will) return 0 if you use 0 as the hint and use MAP_FIXED
> at 0. That's the reason why one does NOT check for NULL with mmap() but
> for MAP_FAILED (which on this system is (void *)-1.

All the paragraph was under an obvious "when you do not use MAP_FIXED"
precondition.  The patch is not supposed to change anything to the
MAP_FIXED case.  Malloc does not use MAP_FIXED.  Usual file mmaping
as an alternative to read() does not use MAP_FIXED.

  OG.

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: don't let mmap allocate down to zero
  2005-01-26 17:57     ` Bryn Reeves
@ 2005-01-26 18:37       ` linux-os
  2005-01-26 18:54         ` Rik van Riel
  2005-01-26 19:13         ` Chris Friesen
  2005-01-26 19:08       ` Chris Friesen
  1 sibling, 2 replies; 40+ messages in thread
From: linux-os @ 2005-01-26 18:37 UTC (permalink / raw)
  To: Bryn Reeves
  Cc: Chris Friesen, Rik van Riel, Andrew Morton, linux-kernel, James Antill

[-- Attachment #1: Type: TEXT/PLAIN, Size: 2436 bytes --]

On Wed, 26 Jan 2005, Bryn Reeves wrote:

> On Wed, 2005-01-26 at 17:34, Chris Friesen wrote:
>> linux-os wrote:
>>
>>> Does this mean that we can't mmap the screen regen buffer at
>>> 0x000b8000 anymore?
>>>
>>> How do I look at the real-mode interrupt table starting at
>>> offset 0? You know that the return value of mmap is to be
>>> checked for MAP_FAILED, not for NULL, don't you?
>>
>> Can't you still map those physical addresses to other virtual addresses?
>>
>
> I think that's the case. The 0 address as refered to here is only for
> the user virtual address space.
>
>>> What 'C' standard do you refer to? Seg-faults on null pointers
>>> have nothing to do with the 'C' standard and everything to
>>> do with the platform.
>>
>> I believe the ISO/IEC 9899:1999 C Standard explicitly states that
>> dereferencing a null pointer with the unary * operator results in
>> undefined behavior.
>
> Exactly. Undefined. VAX/UNIX allowed assignment to null pointers. BUT
> it's now such a commonly held assumption that a null pointer is not
> valid that things will break if this is changed. Doesn't glibc malloc
> use mmap for small allocations? From the man page:

Wrong! A returned value of 0 is perfectly correct for mmap()
when mapping a fixed address. The attached code shows it working
perfectly while returning NULL, that event being put on the
terminal.

Any kernel changes that break this code are wrong. There are
many 'C' runtime library functions that signal a problem (or
should signal a problem) by returning NULL. That is, however,
the documented behavior of those procedures. Even malloc()
which __should__ show a failure to allocate by returning NULL,
doesn't work that way because the allocation never fails
(even if you are out of memory) as long as the user address
space hasn't been corrupted by the user (overwriting buffers).

The seg-fault you get when you de-reference a pointer to NULL
is caused by the kernel. You are attempting to access memory
that has not been mapped into your address space. Once that
memory gets mmap()ed, you will no longer get a seg-fault.
Again, the seg-fault has nothing to do with 'C'. It's an
implementation behavior that can be changed with mmap().

[SNIPPED...]

Cheers,
Dick Johnson
Penguin : Linux version 2.6.10 on an i686 machine (5537.79 BogoMips).
  Notice : All mail here is now cached for review by Dictator Bush.
                  98.36% of all statistics are fiction.

[-- Attachment #2: Type: TEXT/PLAIN, Size: 2941 bytes --]



#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <fcntl.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/mman.h>

#define xxDEBUG

#if !defined(PAGE_SIZE)
#define PAGE_SIZE 0x1000
#endif
#if !defined(PAGE_MASK)
#define PAGE_MASK ~(PAGE_SIZE - 1)
#endif
#if !defined(MAP_FAILED)
#define MAP_FAILED (void *) -1
#endif
#define UL unsigned long

#define ERRORS \
    { fprintf(stderr, errmsg, __LINE__,\
      __FILE__, errno, strerror(errno));\
      exit(EXIT_FAILURE); }

#ifdef DEBUG
#define DEB(f) (f)
#else
#define DEB(f)
#endif

static const char errmsg[]="Error at line %d, file %s (%d) [%s]\n";
static const char mapfile[]="/dev/mem";
#define PROT (PROT_READ|PROT_WRITE)
#define FLAGS (MAP_FIXED|MAP_SHARED)
/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
/*
 *  This writes a 'debug-like' dump out the screen. It always writes
 *  16 lines of 16 characters. It returns the last address displayed.
 */
static off_t dump(off_t addr);

static off_t dump(off_t addr)
{
    size_t i, j;
    int fd;
    caddr_t mem;
    off_t next;
    unsigned char c, *buf;

    next = addr; 
    addr &= PAGE_MASK;
    if((fd = open(mapfile, O_RDWR)) < 0)
        ERRORS;

    if((mem = mmap((caddr_t)addr, PAGE_SIZE * 2, PROT, FLAGS, fd, addr)) == MAP_FAILED)
    {
        (void)close(fd);
        fprintf(stderr, "\tCan't map address %08lX\n", (UL) addr);
        return (off_t)mem;
    } 


    printf("mmap returned %p  (0x%08x)\n", mem, (size_t) mem);

    buf = (unsigned char *) next;
    DEB(fprintf(stderr, "Mapped address = 0x%lx\n", addr));
    DEB(fprintf(stderr, "Buffer address = %p\n", buf));

    for(i=0; i< 0x10; i++)
    {
        fprintf(stdout, "\n%08lX ", (UL) next);
        next += 0x10;
        for(j=0; j< 0x10; j++)
            fprintf(stdout, "%02X ", (unsigned int) *buf++);
        buf -= 0x10;
        for(j=0; j<0x10; j++)
        {
            c = *buf++;
            if((c < (unsigned char) ' ') || (c > (unsigned char) 'z'))
                c = (unsigned char) '.';
            fprintf(stdout, "%c", c);
        }
    }
    fprintf(stdout, "\n");
    (void)fflush(stdout);
    (void)close(fd);
    if(munmap(mem, PAGE_SIZE)< 0)
        ERRORS;
    return next;
}

void usage(char *cp)
{
        fprintf(stderr, "Usage\n%s <start address> [end address]\n", cp);
        exit(EXIT_FAILURE);
}

int main(int args, char *argv[])
{
    off_t addr;
    off_t end;

    if(args < 2)
        usage(argv[0]);
    end = 0;
    if(sscanf(argv[1], "%lx", &addr) != 1)
        usage(argv[0]);
    if(argv[2] != NULL)
        (void)sscanf(argv[2], "%lx", &end);
    do {    
        if((addr = dump(addr)) == 0xffffffff)
            break;
        } while (end > addr);
    return 0;
}


^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: don't let mmap allocate down to zero
  2005-01-26 18:31       ` Olivier Galibert
@ 2005-01-26 18:39         ` linux-os
  0 siblings, 0 replies; 40+ messages in thread
From: linux-os @ 2005-01-26 18:39 UTC (permalink / raw)
  To: Olivier Galibert
  Cc: Rik van Riel, Andrew Morton, linux-kernel, James Antill, Bryn Reeves

On Wed, 26 Jan 2005, Olivier Galibert wrote:

> On Wed, Jan 26, 2005 at 01:20:53PM -0500, linux-os wrote:
>> On Wed, 26 Jan 2005, Olivier Galibert wrote:
>>> Given that the man page itself says that unless you're using MAP_FIXED
>>> start is only a hint and you should use 0 if you don't care things can
>>> get real annoying real fast.  Imagine if you want to mmap a <4K file
>>> and mmap then returns 0, i.e. NULL, as the mapping address as you
>>> asked.  It's illegal from the point of view of susv3[1] and it's real
>>> annoying in a C/C++ program.
>>
>> mmap() can (will) return 0 if you use 0 as the hint and use MAP_FIXED
>> at 0. That's the reason why one does NOT check for NULL with mmap() but
>> for MAP_FAILED (which on this system is (void *)-1.
>
> All the paragraph was under an obvious "when you do not use MAP_FIXED"
> precondition.  The patch is not supposed to change anything to the
> MAP_FIXED case.  Malloc does not use MAP_FIXED.  Usual file mmaping
> as an alternative to read() does not use MAP_FIXED.
>
>  OG.

Okay. That's fine.

Cheers,
Dick Johnson
Penguin : Linux version 2.6.10 on an i686 machine (5537.79 BogoMips).
  Notice : All mail here is now cached for review by Dictator Bush.
                  98.36% of all statistics are fiction.

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: don't let mmap allocate down to zero
  2005-01-26 18:37       ` linux-os
@ 2005-01-26 18:54         ` Rik van Riel
  2005-01-26 19:09           ` linux-os
  2005-01-26 19:13         ` Chris Friesen
  1 sibling, 1 reply; 40+ messages in thread
From: Rik van Riel @ 2005-01-26 18:54 UTC (permalink / raw)
  To: linux-os
  Cc: Bryn Reeves, Chris Friesen, Andrew Morton, linux-kernel, James Antill

On Wed, 26 Jan 2005, linux-os wrote:

> Wrong! A returned value of 0 is perfectly correct for mmap()
> when mapping a fixed address. The attached code shows it working
   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The code that is patched is only run in case of a non-MAP_FIXED
mmap() call...

-- 
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it." - Brian W. Kernighan

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: don't let mmap allocate down to zero
  2005-01-26 17:57     ` Bryn Reeves
  2005-01-26 18:37       ` linux-os
@ 2005-01-26 19:08       ` Chris Friesen
  1 sibling, 0 replies; 40+ messages in thread
From: Chris Friesen @ 2005-01-26 19:08 UTC (permalink / raw)
  To: breeves; +Cc: linux-os, Rik van Riel, Andrew Morton, linux-kernel, James Antill

Bryn Reeves wrote:

> RETURN VALUE
>   For calloc() and malloc(), the value returned is a pointer to the
>   allocated  memory,  which  is suitably aligned for any kind of
>   variable, or NULL if the request fails.
> 
> This could get pretty confusing if NULL was a valid address...

Internally the library can use mmap().  Presumably they will map a 
MAP_FAILED return code from mmap() to a NULL return code in malloc().

Chris

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: don't let mmap allocate down to zero
  2005-01-26 18:54         ` Rik van Riel
@ 2005-01-26 19:09           ` linux-os
  0 siblings, 0 replies; 40+ messages in thread
From: linux-os @ 2005-01-26 19:09 UTC (permalink / raw)
  To: Rik van Riel
  Cc: Bryn Reeves, Chris Friesen, Andrew Morton, Linux kernel, James Antill

On Wed, 26 Jan 2005, Rik van Riel wrote:

> On Wed, 26 Jan 2005, linux-os wrote:
>
>> Wrong! A returned value of 0 is perfectly correct for mmap()
>> when mapping a fixed address. The attached code shows it working
>  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>
> The code that is patched is only run in case of a non-MAP_FIXED
> mmap() call...
>

That's good then. I needed to make sure. Lots of embedded stuff
peeks and pokes at ix86 low-memory physical addresses.

Cheers,
Dick Johnson
Penguin : Linux version 2.6.10 on an i686 machine (5537.79 BogoMips).
  Notice : All mail here is now cached for review by Dictator Bush.
                  98.36% of all statistics are fiction.

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: don't let mmap allocate down to zero
  2005-01-26 18:37       ` linux-os
  2005-01-26 18:54         ` Rik van Riel
@ 2005-01-26 19:13         ` Chris Friesen
  1 sibling, 0 replies; 40+ messages in thread
From: Chris Friesen @ 2005-01-26 19:13 UTC (permalink / raw)
  To: linux-os
  Cc: Bryn Reeves, Rik van Riel, Andrew Morton, linux-kernel, James Antill

linux-os wrote:

> The seg-fault you get when you de-reference a pointer to NULL
> is caused by the kernel. You are attempting to access memory
> that has not been mapped into your address space. Once that
> memory gets mmap()ed, you will no longer get a seg-fault.
> Again, the seg-fault has nothing to do with 'C'. It's an
> implementation behavior that can be changed with mmap().

The segfault *does* have something to do with C.  The standard says that 
the result of dereferencing a NULL pointer is *undefined*.  Not 
implementation-defined, but undefined.  Anything relying on 
dereferencing NULL pointers is not valid C code.

Chris

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: don't let mmap allocate down to zero
  2005-01-26 16:38 ` linux-os
                     ` (2 preceding siblings ...)
  2005-01-26 18:10   ` Olivier Galibert
@ 2005-01-26 19:41   ` Arjan van de Ven
  2005-01-26 20:26   ` Andy Isaacson
                     ` (2 subsequent siblings)
  6 siblings, 0 replies; 40+ messages in thread
From: Arjan van de Ven @ 2005-01-26 19:41 UTC (permalink / raw)
  To: linux-os
  Cc: Rik van Riel, Andrew Morton, linux-kernel, James Antill, Bryn Reeves

On Wed, 2005-01-26 at 11:38 -0500, linux-os wrote:
> On Wed, 26 Jan 2005, Rik van Riel wrote:
> 
> > With some programs the 2.6 kernel can end up allocating memory
> > at address zero, for a non-MAP_FIXED mmap call!  This causes
> > problems with some programs and is generally rude to do. This
> > simple patch fixes the problem in my tests.
> 
> Does this mean that we can't mmap the screen regen buffer at
> 0x000b8000 anymore?

you're confusing virtual and physical addresses




^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: don't let mmap allocate down to zero
  2005-01-26 16:38 ` linux-os
                     ` (3 preceding siblings ...)
  2005-01-26 19:41   ` Arjan van de Ven
@ 2005-01-26 20:26   ` Andy Isaacson
  2005-01-26 20:42     ` Rik van Riel
  2005-01-26 22:12   ` Kyle Moffett
  2005-01-26 23:31   ` Brian Gerst
  6 siblings, 1 reply; 40+ messages in thread
From: Andy Isaacson @ 2005-01-26 20:26 UTC (permalink / raw)
  To: linux-os
  Cc: Rik van Riel, Andrew Morton, linux-kernel, James Antill, Bryn Reeves

On Wed, Jan 26, 2005 at 11:38:15AM -0500, linux-os wrote:
> On Wed, 26 Jan 2005, Rik van Riel wrote:
> >With some programs the 2.6 kernel can end up allocating memory
> >at address zero, for a non-MAP_FIXED mmap call!  This causes
> >problems with some programs and is generally rude to do. This
> >simple patch fixes the problem in my tests.
> 
> Does this mean that we can't mmap the screen regen buffer at
> 0x000b8000 anymore?

That would be a MAP_FIXED call, so not affected by this change.

> How do I look at the real-mode interrupt table starting at
> offset 0? You know that the return value of mmap is to be
> checked for MAP_FAILED, not for NULL, don't you?

All MAP_FIXED, too.

> What 'C' standard do you refer to? Seg-faults on null pointers
> have nothing to do with the 'C' standard and everything to
> do with the platform.

Obviously having malloc() return NULL for a successful allocation would
be a bad thing, no?  That's precisely what could happen if an anonymous
allocation got mapped at 0x0.

> >+#define SHLIB_BASE             0x00111000

Any particular thoughts as to how large a window should be reserved?
SHLIB_BASE is a bit more than 1MB, which is fairly small in the grand
scheme of things, but I guess I don't see why you'd reserve more than
PAGE_SIZE (or maybe PAGE_SIZE*2, though I can't actually articulate why
that seems like a good idea).

FWIW, mmap(2) also will return NULL if length==0.  That sure did confuse
me the first time I noticed.

-andy

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: don't let mmap allocate down to zero
  2005-01-26 20:26   ` Andy Isaacson
@ 2005-01-26 20:42     ` Rik van Riel
  0 siblings, 0 replies; 40+ messages in thread
From: Rik van Riel @ 2005-01-26 20:42 UTC (permalink / raw)
  To: Andy Isaacson
  Cc: linux-os, Andrew Morton, linux-kernel, James Antill, Bryn Reeves

On Wed, 26 Jan 2005, Andy Isaacson wrote:

> Any particular thoughts as to how large a window should be reserved?
> SHLIB_BASE is a bit more than 1MB, which is fairly small in the grand
> scheme of things, but I guess I don't see why you'd reserve more than
> PAGE_SIZE (or maybe PAGE_SIZE*2, though I can't actually articulate why
> that seems like a good idea).

You also want to catch not-quite-NULL pointer dereferences,
where you dereference the member of a large struct or array,
but the array or struct pointer itself was NULL.

Eg. you try to fetch the 100,000th from an array, but you
got passed a NULL array pointer.  Getting a segfault is
much, much better than corrupting data.

-- 
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it." - Brian W. Kernighan

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: don't let mmap allocate down to zero
  2005-01-26 16:38 ` linux-os
                     ` (4 preceding siblings ...)
  2005-01-26 20:26   ` Andy Isaacson
@ 2005-01-26 22:12   ` Kyle Moffett
  2005-01-26 23:31   ` Brian Gerst
  6 siblings, 0 replies; 40+ messages in thread
From: Kyle Moffett @ 2005-01-26 22:12 UTC (permalink / raw)
  To: linux-os
  Cc: linux-kernel, Bryn Reeves, James Antill, Rik van Riel, Andrew Morton

On Jan 26, 2005, at 11:38, linux-os wrote:
> Does this mean that we can't mmap the screen regen buffer at
> 0x000b8000 anymore?

I believe the point of this is to ensure that *non*-MAP_FIXED
mmap calls won't use 0, IOW, it keeps things from accidentally
being mapped at 0 when the user didn't intend to, like shared
libs and such.

Cheers,
Kyle Moffett

-----BEGIN GEEK CODE BLOCK-----
Version: 3.12
GCM/CS/IT/U d- s++: a18 C++++>$ UB/L/X/*++++(+)>$ P+++(++++)>$
L++++(+++) E W++(+) N+++(++) o? K? w--- O? M++ V? PS+() PE+(-) Y+
PGP+++ t+(+++) 5 X R? tv-(--) b++++(++) DI+ D+ G e->++++$ h!*()>++$ r  
!y?(-)
------END GEEK CODE BLOCK------



^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: don't let mmap allocate down to zero
  2005-01-26 16:38 ` linux-os
                     ` (5 preceding siblings ...)
  2005-01-26 22:12   ` Kyle Moffett
@ 2005-01-26 23:31   ` Brian Gerst
  6 siblings, 0 replies; 40+ messages in thread
From: Brian Gerst @ 2005-01-26 23:31 UTC (permalink / raw)
  To: linux-os
  Cc: Rik van Riel, Andrew Morton, linux-kernel, James Antill, Bryn Reeves

linux-os wrote:
> On Wed, 26 Jan 2005, Rik van Riel wrote:
> 
>> With some programs the 2.6 kernel can end up allocating memory
>> at address zero, for a non-MAP_FIXED mmap call!  This causes
>> problems with some programs and is generally rude to do. This
>> simple patch fixes the problem in my tests.
> 
> 
> Does this mean that we can't mmap the screen regen buffer at
> 0x000b8000 anymore?
> 
> How do I look at the real-mode interrupt table starting at
> offset 0? You know that the return value of mmap is to be
> checked for MAP_FAILED, not for NULL, don't you?

This does not affect the case where the user requests a specific 
virrtual address (ie. vm86 stuff).

--
				Brian Gerst

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: don't let mmap allocate down to zero
  2005-01-26 17:25 ` William Lee Irwin III
@ 2005-01-27  5:09   ` William Lee Irwin III
  2005-01-27  5:18     ` Dave Jones
  2005-01-27  9:29     ` Mikael Pettersson
  0 siblings, 2 replies; 40+ messages in thread
From: William Lee Irwin III @ 2005-01-27  5:09 UTC (permalink / raw)
  To: Rik van Riel; +Cc: Andrew Morton, linux-kernel, James Antill, Bryn Reeves

On Wed, Jan 26, 2005 at 11:18:08AM -0500, Rik van Riel wrote:
>> With some programs the 2.6 kernel can end up allocating memory
>> at address zero, for a non-MAP_FIXED mmap call!  This causes
>> problems with some programs and is generally rude to do. This
>> simple patch fixes the problem in my tests.
>> Make sure that we don't allocate memory all the way down to zero,
>> so the NULL pointer never gets covered up with anonymous memory
>> and we don't end up violating the C standard.
>> Signed-off-by: Rik van Riel <riel@redhat.com>

On Wed, Jan 26, 2005 at 09:25:38AM -0800, William Lee Irwin III wrote:
> SHLIB_BASE does not appear to be present in 2.6.9; perhaps something
> else is going on.
> I think we are better off:
> 	(a) checking for hitting zero explicitly as opposed to
> 		enforcing a randomly-chosen lower limit for addresses
> 	(b) enforcing vma allocation above FIRST_USER_PGD_NR*PGDIR_SIZE,
> 		to which SHLIB_BASE bears no relation.

There's a long discussion here, in which no one appears to have noticed
that SHLIB_BASE does not exist in mainline. Is anyone else awake here?


-- wli

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: don't let mmap allocate down to zero
  2005-01-27  5:09   ` William Lee Irwin III
@ 2005-01-27  5:18     ` Dave Jones
  2005-01-27  5:28       ` William Lee Irwin III
  2005-01-27  9:29     ` Mikael Pettersson
  1 sibling, 1 reply; 40+ messages in thread
From: Dave Jones @ 2005-01-27  5:18 UTC (permalink / raw)
  To: William Lee Irwin III
  Cc: Rik van Riel, Andrew Morton, linux-kernel, James Antill, Bryn Reeves

On Wed, Jan 26, 2005 at 09:09:27PM -0800, William Lee Irwin III wrote:
 > On Wed, Jan 26, 2005 at 11:18:08AM -0500, Rik van Riel wrote:
 > >> With some programs the 2.6 kernel can end up allocating memory
 > >> at address zero, for a non-MAP_FIXED mmap call!  This causes
 > >> problems with some programs and is generally rude to do. This
 > >> simple patch fixes the problem in my tests.
 > >> Make sure that we don't allocate memory all the way down to zero,
 > >> so the NULL pointer never gets covered up with anonymous memory
 > >> and we don't end up violating the C standard.
 > >> Signed-off-by: Rik van Riel <riel@redhat.com>
 > 
 > On Wed, Jan 26, 2005 at 09:25:38AM -0800, William Lee Irwin III wrote:
 > > SHLIB_BASE does not appear to be present in 2.6.9; perhaps something
 > > else is going on.
 > > I think we are better off:
 > > 	(a) checking for hitting zero explicitly as opposed to
 > > 		enforcing a randomly-chosen lower limit for addresses
 > > 	(b) enforcing vma allocation above FIRST_USER_PGD_NR*PGDIR_SIZE,
 > > 		to which SHLIB_BASE bears no relation.
 > 
 > There's a long discussion here, in which no one appears to have noticed
 > that SHLIB_BASE does not exist in mainline. Is anyone else awake here?

It's an exec-shield'ism. Rik likely was working off a Red Hat/Fedora kernel tree.

		Dave


^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: don't let mmap allocate down to zero
  2005-01-27  5:18     ` Dave Jones
@ 2005-01-27  5:28       ` William Lee Irwin III
  0 siblings, 0 replies; 40+ messages in thread
From: William Lee Irwin III @ 2005-01-27  5:28 UTC (permalink / raw)
  To: Dave Jones
  Cc: Rik van Riel, Andrew Morton, linux-kernel, James Antill,
	Bryn Reeves, rmk

On Wed, Jan 26, 2005 at 09:09:27PM -0800, William Lee Irwin III wrote:
>> There's a long discussion here, in which no one appears to have noticed
>> that SHLIB_BASE does not exist in mainline. Is anyone else awake here?

On Thu, Jan 27, 2005 at 12:18:56AM -0500, Dave Jones wrote:
> It's an exec-shield'ism. Rik likely was working off a Red Hat/Fedora
> kernel tree.

It does change things in a substantial way. Namely, the lower address
space boundary doesn't exist in mainline at all, and so its enforcement
requires introduction. Furthermore, mainline must concern itself with
all Linux-supported architectures (not distro-supported architectures),
not just ia32/x86-64, so there is also FIRST_USER_PGD_NR to take into
account as opposed to pulling magic ia32/x86-64 -specific numbers out
of a hat and splattering them all over core code. Not that you had
anything to do with any of this.


-- wli

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: don't let mmap allocate down to zero
  2005-01-27  5:09   ` William Lee Irwin III
  2005-01-27  5:18     ` Dave Jones
@ 2005-01-27  9:29     ` Mikael Pettersson
  2005-01-27 12:52       ` William Lee Irwin III
  1 sibling, 1 reply; 40+ messages in thread
From: Mikael Pettersson @ 2005-01-27  9:29 UTC (permalink / raw)
  To: William Lee Irwin III
  Cc: Rik van Riel, Andrew Morton, linux-kernel, James Antill, Bryn Reeves

William Lee Irwin III writes:
 > On Wed, Jan 26, 2005 at 11:18:08AM -0500, Rik van Riel wrote:
 > >> With some programs the 2.6 kernel can end up allocating memory
 > >> at address zero, for a non-MAP_FIXED mmap call!  This causes
 > >> problems with some programs and is generally rude to do. This
 > >> simple patch fixes the problem in my tests.
 > >> Make sure that we don't allocate memory all the way down to zero,
 > >> so the NULL pointer never gets covered up with anonymous memory
 > >> and we don't end up violating the C standard.
 > >> Signed-off-by: Rik van Riel <riel@redhat.com>
 > 
 > On Wed, Jan 26, 2005 at 09:25:38AM -0800, William Lee Irwin III wrote:
 > > SHLIB_BASE does not appear to be present in 2.6.9; perhaps something
 > > else is going on.
 > > I think we are better off:
 > > 	(a) checking for hitting zero explicitly as opposed to
 > > 		enforcing a randomly-chosen lower limit for addresses
 > > 	(b) enforcing vma allocation above FIRST_USER_PGD_NR*PGDIR_SIZE,
 > > 		to which SHLIB_BASE bears no relation.
 > 
 > There's a long discussion here, in which no one appears to have noticed
 > that SHLIB_BASE does not exist in mainline. Is anyone else awake here?

About the only kernel-level enforcement I would feel comfortable with is
to have non-fixed mmap()s refuse to grab the _page_ at address 0. Any range
larger than this is policy, and hence needs a user-space override mechanism.

Also, if user-space wants to catch accesses in a larger region above 0 then
it can do that itself, by checking the result of mmap(), or by doing a fixed
mmap() at address 0 with suitable size and rwx protection disabled.

/Mikael

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: don't let mmap allocate down to zero
  2005-01-27  9:29     ` Mikael Pettersson
@ 2005-01-27 12:52       ` William Lee Irwin III
  2005-01-27 14:25         ` Russell King
  2005-01-27 14:26         ` Mikael Pettersson
  0 siblings, 2 replies; 40+ messages in thread
From: William Lee Irwin III @ 2005-01-27 12:52 UTC (permalink / raw)
  To: Mikael Pettersson
  Cc: Rik van Riel, Andrew Morton, linux-kernel, James Antill, Bryn Reeves

William Lee Irwin III writes:
>> There's a long discussion here, in which no one appears to have noticed
>> that SHLIB_BASE does not exist in mainline. Is anyone else awake here?

On Thu, Jan 27, 2005 at 10:29:12AM +0100, Mikael Pettersson wrote:
> About the only kernel-level enforcement I would feel comfortable with is
> to have non-fixed mmap()s refuse to grab the _page_ at address 0. Any range
> larger than this is policy, and hence needs a user-space override mechanism.
> Also, if user-space wants to catch accesses in a larger region above 0 then
> it can do that itself, by checking the result of mmap(), or by doing a fixed
> mmap() at address 0 with suitable size and rwx protection disabled.

FIRST_USER_PGD_NR is a matter of killing the entire box dead where it
exists, not any kind of process' preference. Userspace should be
prevented from setting up vmas below FIRST_USER_PGD_NR. It has zero to
do with what userspace's own concerns, but rather the kernel trying to
avoid system-critical data from being stomped on by userspace. It would
be like accidentally allowing userspace to use the IDT for malloc() on
x86, destroying one's ability to handle interrupts, page faults, etc.,
to allow userspace to go below FIRST_USER_PGD_NR on ARM.


-- wli

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: don't let mmap allocate down to zero
  2005-01-27 12:52       ` William Lee Irwin III
@ 2005-01-27 14:25         ` Russell King
  2005-01-27 15:12           ` William Lee Irwin III
  2005-01-27 14:26         ` Mikael Pettersson
  1 sibling, 1 reply; 40+ messages in thread
From: Russell King @ 2005-01-27 14:25 UTC (permalink / raw)
  To: William Lee Irwin III
  Cc: Mikael Pettersson, Rik van Riel, Andrew Morton, linux-kernel,
	James Antill, Bryn Reeves

On Thu, Jan 27, 2005 at 04:52:54AM -0800, William Lee Irwin III wrote:
> On Thu, Jan 27, 2005 at 10:29:12AM +0100, Mikael Pettersson wrote:
> > About the only kernel-level enforcement I would feel comfortable with is
> > to have non-fixed mmap()s refuse to grab the _page_ at address 0. Any range
> > larger than this is policy, and hence needs a user-space override mechanism.
> > Also, if user-space wants to catch accesses in a larger region above 0 then
> > it can do that itself, by checking the result of mmap(), or by doing a fixed
> > mmap() at address 0 with suitable size and rwx protection disabled.
> 
> FIRST_USER_PGD_NR is a matter of killing the entire box dead where it
> exists, not any kind of process' preference. Userspace should be
> prevented from setting up vmas below FIRST_USER_PGD_NR.

No it should not.  The PGD index is FAR to coarse to use - each PGD on
ARM maps 1MB of virtual address space.  Userspace text starts at 32K.

The protection against mmap() MAP_FIXED fiddling with the first page is
handled by the arch-specific mmap() wrappers, so generic code doesn't
have to worry about it.

What generic code _does_ have to worry about is:

(a) not removing the very first page.
(b) not removing the very first pointer to the 2nd level table in the
    1st level tables.

and that is all.  Maybe FIRST_USER_PGD_NR was a bad way of achieving
this, but in the instance of the VM upon which it was originally
implemented (somewhere between 2.2 and 2.4), it was deemed (by others
iirc) to be the best way of achieving it at the time.

-- 
Russell King
 Linux kernel    2.6 ARM Linux   - http://www.arm.linux.org.uk/
 maintainer of:  2.6 PCMCIA      - http://pcmcia.arm.linux.org.uk/
                 2.6 Serial core

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: don't let mmap allocate down to zero
  2005-01-27 12:52       ` William Lee Irwin III
  2005-01-27 14:25         ` Russell King
@ 2005-01-27 14:26         ` Mikael Pettersson
  1 sibling, 0 replies; 40+ messages in thread
From: Mikael Pettersson @ 2005-01-27 14:26 UTC (permalink / raw)
  To: William Lee Irwin III
  Cc: Mikael Pettersson, Rik van Riel, Andrew Morton, linux-kernel,
	James Antill, Bryn Reeves

William Lee Irwin III writes:
 > William Lee Irwin III writes:
 > >> There's a long discussion here, in which no one appears to have noticed
 > >> that SHLIB_BASE does not exist in mainline. Is anyone else awake here?
 > 
 > On Thu, Jan 27, 2005 at 10:29:12AM +0100, Mikael Pettersson wrote:
 > > About the only kernel-level enforcement I would feel comfortable with is
 > > to have non-fixed mmap()s refuse to grab the _page_ at address 0. Any range
 > > larger than this is policy, and hence needs a user-space override mechanism.
 > > Also, if user-space wants to catch accesses in a larger region above 0 then
 > > it can do that itself, by checking the result of mmap(), or by doing a fixed
 > > mmap() at address 0 with suitable size and rwx protection disabled.
 > 
 > FIRST_USER_PGD_NR is a matter of killing the entire box dead where it
 > exists, not any kind of process' preference. Userspace should be
 > prevented from setting up vmas below FIRST_USER_PGD_NR. It has zero to
 > do with what userspace's own concerns, but rather the kernel trying to
 > avoid system-critical data from being stomped on by userspace. It would
 > be like accidentally allowing userspace to use the IDT for malloc() on
 > x86, destroying one's ability to handle interrupts, page faults, etc.,
 > to allow userspace to go below FIRST_USER_PGD_NR on ARM.

My argument only applied to the "protect the user" discussion.

_If_ the kernel needs to reserve parts of the user's address
space for IDTs etc, then certainly mmap() mustn't map those.
But that's orthogonal (in spirit, if not in implementation)
from the "protect the user" discussion.

/Mkael

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: don't let mmap allocate down to zero
  2005-01-27 14:25         ` Russell King
@ 2005-01-27 15:12           ` William Lee Irwin III
  2005-01-27 19:22             ` Rik van Riel
  0 siblings, 1 reply; 40+ messages in thread
From: William Lee Irwin III @ 2005-01-27 15:12 UTC (permalink / raw)
  To: Russell King
  Cc: Mikael Pettersson, Rik van Riel, Andrew Morton, linux-kernel,
	James Antill, Bryn Reeves

On Thu, Jan 27, 2005 at 04:52:54AM -0800, William Lee Irwin III wrote:
>> FIRST_USER_PGD_NR is a matter of killing the entire box dead where it
>> exists, not any kind of process' preference. Userspace should be
>> prevented from setting up vmas below FIRST_USER_PGD_NR.

On Thu, Jan 27, 2005 at 02:25:00PM +0000, Russell King wrote:
> No it should not.  The PGD index is FAR to coarse to use - each PGD on
> ARM maps 1MB of virtual address space.  Userspace text starts at 32K.
> The protection against mmap() MAP_FIXED fiddling with the first page is
> handled by the arch-specific mmap() wrappers, so generic code doesn't
> have to worry about it.
> What generic code _does_ have to worry about is:
> 
> (a) not removing the very first page.
> (b) not removing the very first pointer to the 2nd level table in the
>     1st level tables.
> and that is all.  Maybe FIRST_USER_PGD_NR was a bad way of achieving
> this, but in the instance of the VM upon which it was originally
> implemented (somewhere between 2.2 and 2.4), it was deemed (by others
> iirc) to be the best way of achieving it at the time.

The only claim above is the effect of clobbering virtual page 0 and
referring to this phenomenon by the macro. I was rather careful not to
claim a specific lower boundary to the address space.


-- wli

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: don't let mmap allocate down to zero
  2005-01-27 15:12           ` William Lee Irwin III
@ 2005-01-27 19:22             ` Rik van Riel
  2005-01-27 20:44               ` William Lee Irwin III
  0 siblings, 1 reply; 40+ messages in thread
From: Rik van Riel @ 2005-01-27 19:22 UTC (permalink / raw)
  To: William Lee Irwin III
  Cc: Russell King, Mikael Pettersson, Andrew Morton, linux-kernel,
	James Antill, Bryn Reeves

[-- Attachment #1: Type: TEXT/PLAIN, Size: 909 bytes --]

On Thu, 27 Jan 2005, William Lee Irwin III wrote:

> The only claim above is the effect of clobbering virtual page 0 and
> referring to this phenomenon by the macro. I was rather careful not to
> claim a specific lower boundary to the address space.

OK, here is a patch that does compile against the current
2.6 kernel.  It it obvious we need a cutoff somewhere, so
I've chosen one that's sure to spark up a conversation
and I'll let others decide what that cutoff value is ;)))

===== mm/mmap.c 1.161 vs edited =====
--- 1.161/mm/mmap.c	Wed Jan 12 11:26:28 2005
+++ edited/mm/mmap.c	Thu Jan 27 14:19:21 2005
@@ -1259,6 +1259,13 @@
  			return addr;

  		/*
+		 * Make sure we don't allocate all the way down to
+		 * zero, which would break NULL pointer detection.
+		 */
+		if (addr < mm->brk)
+			goto fail;
+
+		/*
  		 * new region fits between prev_vma->vm_end and
  		 * vma->vm_start, use it:
  		 */

[-- Attachment #2: zero mmap test case --]
[-- Type: TEXT/PLAIN, Size: 805 bytes --]

/*
 * Test case to determine whether the kernel can end up mapping a
 * non-zero non-MAP_FIXED mmap area at address zero, which would
 * be bad.
 */

#include <stdio.h> 
#include <sys/time.h> 
#include <sys/mman.h> 
#include <sys/resource.h> 
#include <unistd.h> 
#include <fcntl.h> 
  
  
int main(int argc, char **argv) 
{ 
    int i; 
    void *p; 
    for(i = 0; ;i++) { 
	
        p = mmap(0, 4096, PROT_READ | PROT_WRITE, 
                   MAP_PRIVATE | MAP_ANON, -1, 0);
        if (p == MAP_FAILED) { 
            break; 
        } 
        if (p == 0) { 
            printf("ACK! Got NIL after %d mappings\n", i); 
 //	    perror("probably unhelpful: ");
            exit(1); 
        } 
	if ((i % 1000) == 0) printf("%d maps\n", i); 
    } 
    return 0; 
} 

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: don't let mmap allocate down to zero
  2005-01-27 19:22             ` Rik van Riel
@ 2005-01-27 20:44               ` William Lee Irwin III
  2005-01-27 20:58                 ` Rik van Riel
  0 siblings, 1 reply; 40+ messages in thread
From: William Lee Irwin III @ 2005-01-27 20:44 UTC (permalink / raw)
  To: Rik van Riel
  Cc: Russell King, Mikael Pettersson, Andrew Morton, linux-kernel,
	James Antill, Bryn Reeves

On Thu, 27 Jan 2005, William Lee Irwin III wrote:
>> The only claim above is the effect of clobbering virtual page 0 and
>> referring to this phenomenon by the macro. I was rather careful not to
>> claim a specific lower boundary to the address space.

On Thu, Jan 27, 2005 at 02:22:50PM -0500, Rik van Riel wrote:
> OK, here is a patch that does compile against the current
> 2.6 kernel.  It it obvious we need a cutoff somewhere, so
> I've chosen one that's sure to spark up a conversation
> and I'll let others decide what that cutoff value is ;)))

Okay, 2 comments:

(a) The most permissive scheme possible given architectural constraints
	I'm aware of (thanks, rmk) would only disallow below PAGE_SIZE,
	or basically !vma->vm_start. mm->brk affects executables with
	high load addresses and prevents the use of the lower 128MB on
	ia32, which I personally make extensive use of in order to move
	program stacks out of the way of larger mmap()'s and to conserve
	pagetable memory.
(b) sys_mremap() isn't covered.

Does the following give you any new ideas?


-- wli

Index: mm1-2.6.11-rc2/mm/mmap.c
===================================================================
--- mm1-2.6.11-rc2.orig/mm/mmap.c	2005-01-26 00:30:38.000000000 -0800
+++ mm1-2.6.11-rc2/mm/mmap.c	2005-01-27 12:33:34.000000000 -0800
@@ -1258,7 +1258,7 @@
 		 * return with success:
 		 */
 		vma = find_vma(mm, addr);
-		if (!vma || addr+len <= vma->vm_start)
+		if (addr && (!vma || addr+len <= vma->vm_start))
 			/* remember the address as a hint for next time */
 			return (mm->free_area_cache = addr);
 
Index: mm1-2.6.11-rc2/mm/mremap.c
===================================================================
--- mm1-2.6.11-rc2.orig/mm/mremap.c	2005-01-26 00:26:43.000000000 -0800
+++ mm1-2.6.11-rc2/mm/mremap.c	2005-01-27 12:34:34.000000000 -0800
@@ -297,6 +297,8 @@
 	if (flags & MREMAP_FIXED) {
 		if (new_addr & ~PAGE_MASK)
 			goto out;
+		if (!new_addr)
+			goto out;
 		if (!(flags & MREMAP_MAYMOVE))
 			goto out;
 

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: don't let mmap allocate down to zero
  2005-01-27 20:44               ` William Lee Irwin III
@ 2005-01-27 20:58                 ` Rik van Riel
  2005-01-27 21:13                   ` William Lee Irwin III
  0 siblings, 1 reply; 40+ messages in thread
From: Rik van Riel @ 2005-01-27 20:58 UTC (permalink / raw)
  To: William Lee Irwin III
  Cc: Russell King, Mikael Pettersson, Andrew Morton, linux-kernel,
	James Antill, Bryn Reeves

On Thu, 27 Jan 2005, William Lee Irwin III wrote:

> (b) sys_mremap() isn't covered.

AFAICS it is covered.

> --- mm1-2.6.11-rc2.orig/mm/mremap.c	2005-01-26 00:26:43.000000000 -0800
> +++ mm1-2.6.11-rc2/mm/mremap.c	2005-01-27 12:34:34.000000000 -0800
> @@ -297,6 +297,8 @@
> 	if (flags & MREMAP_FIXED) {
> 		if (new_addr & ~PAGE_MASK)
> 			goto out;
> +		if (!new_addr)
> +			goto out;

This looks broken, look at the MREMAP_FIXED part...

-- 
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it." - Brian W. Kernighan

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: don't let mmap allocate down to zero
  2005-01-27 20:58                 ` Rik van Riel
@ 2005-01-27 21:13                   ` William Lee Irwin III
  2005-01-27 21:28                     ` Rik van Riel
  2005-01-27 21:58                     ` linux-os
  0 siblings, 2 replies; 40+ messages in thread
From: William Lee Irwin III @ 2005-01-27 21:13 UTC (permalink / raw)
  To: Rik van Riel
  Cc: Russell King, Mikael Pettersson, Andrew Morton, linux-kernel,
	James Antill, Bryn Reeves

On Thu, 27 Jan 2005, William Lee Irwin III wrote:
>> (b) sys_mremap() isn't covered.

On Thu, Jan 27, 2005 at 03:58:12PM -0500, Rik van Riel wrote:
> AFAICS it is covered.
> >--- mm1-2.6.11-rc2.orig/mm/mremap.c	2005-01-26 00:26:43.000000000 -0800
> >+++ mm1-2.6.11-rc2/mm/mremap.c	2005-01-27 12:34:34.000000000 -0800
> >@@ -297,6 +297,8 @@
> >	if (flags & MREMAP_FIXED) {
> >		if (new_addr & ~PAGE_MASK)
> >			goto out;
> >+		if (!new_addr)
> >+			goto out;
> 
> This looks broken, look at the MREMAP_FIXED part...

The only way I can make sense of this is if you're trying to say that
because the user is trying to pass in a fixed address, that 0 should
then be permitted.

The intention was to disallow vmas starting at 0 categorically. i.e. it
is very intentional to deny the MREMAP_FIXED to 0 case of mremap().
It was also the intention to deny the MAP_FIXED to 0 case of mmap(),
though I didn't actually sweep that much (if at all).


-- wli

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: don't let mmap allocate down to zero
  2005-01-27 21:13                   ` William Lee Irwin III
@ 2005-01-27 21:28                     ` Rik van Riel
  2005-01-28  5:30                       ` William Lee Irwin III
  2005-01-27 21:58                     ` linux-os
  1 sibling, 1 reply; 40+ messages in thread
From: Rik van Riel @ 2005-01-27 21:28 UTC (permalink / raw)
  To: William Lee Irwin III
  Cc: Russell King, Mikael Pettersson, Andrew Morton, linux-kernel,
	James Antill, Bryn Reeves

On Thu, 27 Jan 2005, William Lee Irwin III wrote:

> The intention was to disallow vmas starting at 0 categorically. i.e. it
> is very intentional to deny the MREMAP_FIXED to 0 case of mremap().
> It was also the intention to deny the MAP_FIXED to 0 case of mmap(),
> though I didn't actually sweep that much (if at all).

We can't do that, look at line 944 of fs/binfmt_elf.c:

         if (current->personality & MMAP_PAGE_ZERO) {
                 /* Why this, you ask???  Well SVr4 maps page 0 as read-only,
                    and some applications "depend" upon this behavior.
                    Since we do not have the power to recompile these, we
                    emulate the SVr4 behavior.  Sigh.  */
                 down_write(&current->mm->mmap_sem);
                 error = do_mmap(NULL, 0, PAGE_SIZE, PROT_READ | PROT_EXEC,
                                 MAP_FIXED | MAP_PRIVATE, 0);
                 up_write(&current->mm->mmap_sem);
         }


-- 
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it." - Brian W. Kernighan

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: don't let mmap allocate down to zero
  2005-01-27 21:13                   ` William Lee Irwin III
  2005-01-27 21:28                     ` Rik van Riel
@ 2005-01-27 21:58                     ` linux-os
  1 sibling, 0 replies; 40+ messages in thread
From: linux-os @ 2005-01-27 21:58 UTC (permalink / raw)
  To: William Lee Irwin III
  Cc: Rik van Riel, Russell King, Mikael Pettersson, Andrew Morton,
	Linux kernel, James Antill, Bryn Reeves

On Thu, 27 Jan 2005, William Lee Irwin III wrote:

> On Thu, 27 Jan 2005, William Lee Irwin III wrote:
>>> (b) sys_mremap() isn't covered.
>
> On Thu, Jan 27, 2005 at 03:58:12PM -0500, Rik van Riel wrote:
>> AFAICS it is covered.
>>> --- mm1-2.6.11-rc2.orig/mm/mremap.c	2005-01-26 00:26:43.000000000 -0800
>>> +++ mm1-2.6.11-rc2/mm/mremap.c	2005-01-27 12:34:34.000000000 -0800
>>> @@ -297,6 +297,8 @@
>>> 	if (flags & MREMAP_FIXED) {
>>> 		if (new_addr & ~PAGE_MASK)
>>> 			goto out;
>>> +		if (!new_addr)
>>> +			goto out;
>>
>> This looks broken, look at the MREMAP_FIXED part...
>
> The only way I can make sense of this is if you're trying to say that
> because the user is trying to pass in a fixed address, that 0 should
> then be permitted.
>
> The intention was to disallow vmas starting at 0 categorically. i.e. it
> is very intentional to deny the MREMAP_FIXED to 0 case of mremap().
> It was also the intention to deny the MAP_FIXED to 0 case of mmap(),
> though I didn't actually sweep that much (if at all).
>
>
> -- wli

No! Then you can't make a tool that will be able to look at
the entire x86 address-space! You end up with an offset that will
eventually wrap to zero which you are denying. You must leave
MAP_FIXED alone. Ignore the 'C' pedants, a pointer is properly
initialized if it points to a mapped address. It would be absurd
to have to make the CPU calculate the address at run-time just
because you thew some rocks in the way.

Cheers,
Dick Johnson
Penguin : Linux version 2.6.10 on an i686 machine (5537.79 BogoMips).
  Notice : All mail here is now cached for review by Dictator Bush.
                  98.36% of all statistics are fiction.

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: don't let mmap allocate down to zero
  2005-01-27 21:28                     ` Rik van Riel
@ 2005-01-28  5:30                       ` William Lee Irwin III
  2005-01-28 13:01                         ` Rik van Riel
  0 siblings, 1 reply; 40+ messages in thread
From: William Lee Irwin III @ 2005-01-28  5:30 UTC (permalink / raw)
  To: Rik van Riel
  Cc: Russell King, Mikael Pettersson, Andrew Morton, linux-kernel,
	James Antill, Bryn Reeves

On Thu, 27 Jan 2005, William Lee Irwin III wrote:
>> The intention was to disallow vmas starting at 0 categorically. i.e. it
>> is very intentional to deny the MREMAP_FIXED to 0 case of mremap().
>> It was also the intention to deny the MAP_FIXED to 0 case of mmap(),
>> though I didn't actually sweep that much (if at all).

On Thu, Jan 27, 2005 at 04:28:19PM -0500, Rik van Riel wrote:
> We can't do that, look at line 944 of fs/binfmt_elf.c:
>         if (current->personality & MMAP_PAGE_ZERO) {
>                 /* Why this, you ask???  Well SVr4 maps page 0 as read-only,
>                    and some applications "depend" upon this behavior.
>                    Since we do not have the power to recompile these, we
>                    emulate the SVr4 behavior.  Sigh.  */
>                 down_write(&current->mm->mmap_sem);
>                 error = do_mmap(NULL, 0, PAGE_SIZE, PROT_READ | PROT_EXEC,
>                                 MAP_FIXED | MAP_PRIVATE, 0);
>                 up_write(&current->mm->mmap_sem);
>         }

You seem to be on about something else, e.g. only forbidding the vma
allocator to return a vma starting at 0 when not specifically requested.
In that case vma->vm_start < mm->brk and similar are all fine.


-- wli

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: don't let mmap allocate down to zero
  2005-01-28  5:30                       ` William Lee Irwin III
@ 2005-01-28 13:01                         ` Rik van Riel
  2005-01-28 14:14                           ` Hugh Dickins
  0 siblings, 1 reply; 40+ messages in thread
From: Rik van Riel @ 2005-01-28 13:01 UTC (permalink / raw)
  To: William Lee Irwin III
  Cc: Russell King, Mikael Pettersson, Andrew Morton, linux-kernel,
	James Antill, Bryn Reeves

On Thu, 27 Jan 2005, William Lee Irwin III wrote:

> You seem to be on about something else, e.g. only forbidding the vma
> allocator to return a vma starting at 0 when not specifically requested.
> In that case vma->vm_start < mm->brk and similar are all fine.

Yes.

-- 
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it." - Brian W. Kernighan

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: don't let mmap allocate down to zero
  2005-01-28 13:01                         ` Rik van Riel
@ 2005-01-28 14:14                           ` Hugh Dickins
  2005-01-28 14:26                             ` William Lee Irwin III
  2005-01-28 15:41                             ` Rik van Riel
  0 siblings, 2 replies; 40+ messages in thread
From: Hugh Dickins @ 2005-01-28 14:14 UTC (permalink / raw)
  To: Rik van Riel
  Cc: William Lee Irwin III, Russell King, Mikael Pettersson,
	Andrew Morton, linux-kernel, James Antill, Bryn Reeves

On Fri, 28 Jan 2005, Rik van Riel wrote:
> On Thu, 27 Jan 2005, William Lee Irwin III wrote:
> 
> > You seem to be on about something else, e.g. only forbidding the vma
> > allocator to return a vma starting at 0 when not specifically requested.
> > In that case vma->vm_start < mm->brk and similar are all fine.
> 
> Yes.

Prohibiting "addr < mm->brk" (unless FIXED) seems arbitrary policy to me,
and at variance with the comment "would break NULL pointer detection".
Why have you chosen to prohibit all that rather than just "!addr"?
(Other than to stoke up this controversy you expect ;)

But I have to admit that it's much less arbitrary policy than the
TASK_UNMAPPED_BASE used when going bottom up - I much prefer your
your mm->brk test to that.

I had imagined that top down (non-FIXED) would continue to make
more space available, the space below the text, just cutting off
at PAGE_SIZE.  There was a more serious lower limit on ARM under
discussion before, but ARM doesn't use top down so far as I can see.

Perhaps you're coming from experience of various buggy apps
that get into difficulties if mmaps are found below mm->brk?
I'm not sure that we should be cutting others' address space to
make life easier for those, they should be ADDR_COMPAT_LAYOUT.
Part (all?) of the point of topdown was to make more address
space available, wasn't it?

arch/ppc64/mm/hugetlbpage.c (odd place to find it) has its own
arch_get_unmapped_area_topdown, should be given a similar fix.

Hugh

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: don't let mmap allocate down to zero
  2005-01-28 14:14                           ` Hugh Dickins
@ 2005-01-28 14:26                             ` William Lee Irwin III
  2005-01-28 15:41                             ` Rik van Riel
  1 sibling, 0 replies; 40+ messages in thread
From: William Lee Irwin III @ 2005-01-28 14:26 UTC (permalink / raw)
  To: Hugh Dickins
  Cc: Rik van Riel, Russell King, Mikael Pettersson, Andrew Morton,
	linux-kernel, James Antill, Bryn Reeves

On Fri, Jan 28, 2005 at 02:14:36PM +0000, Hugh Dickins wrote:
> I had imagined that top down (non-FIXED) would continue to make
> more space available, the space below the text, just cutting off
> at PAGE_SIZE.  There was a more serious lower limit on ARM under
> discussion before, but ARM doesn't use top down so far as I can see.

rmk chimed in at one point and made it clear that ARM's actual lower
bounday was PAGE_SIZE (before that, all I knew was:
0 < x <= (FIRST_USER_PGD_NR << PGDIR_SHIFT)).


-- wli

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: don't let mmap allocate down to zero
  2005-01-28 14:14                           ` Hugh Dickins
  2005-01-28 14:26                             ` William Lee Irwin III
@ 2005-01-28 15:41                             ` Rik van Riel
  2005-01-28 15:53                               ` Hugh Dickins
  1 sibling, 1 reply; 40+ messages in thread
From: Rik van Riel @ 2005-01-28 15:41 UTC (permalink / raw)
  To: Hugh Dickins
  Cc: William Lee Irwin III, Russell King, Mikael Pettersson,
	Andrew Morton, linux-kernel, James Antill, Bryn Reeves

On Fri, 28 Jan 2005, Hugh Dickins wrote:

> Perhaps you're coming from experience of various buggy apps
> that get into difficulties if mmaps are found below mm->brk?
> I'm not sure that we should be cutting others' address space to
> make life easier for those, they should be ADDR_COMPAT_LAYOUT.

The main thing I would really like to preserve is the
space used for "near-NULL" pointer detection. That is,
detection of trying to access a large index in a NULL
pointer array, etc.

I'd be happy to have some arbitrary value for the lower
boundary...

> arch/ppc64/mm/hugetlbpage.c (odd place to find it) has its own
> arch_get_unmapped_area_topdown, should be given a similar fix.

Good point, though a 64 bit architecture is, umm, less
likely to run all the way down to zero within our lifetime.

-- 
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it." - Brian W. Kernighan

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: don't let mmap allocate down to zero
  2005-01-28 15:41                             ` Rik van Riel
@ 2005-01-28 15:53                               ` Hugh Dickins
  0 siblings, 0 replies; 40+ messages in thread
From: Hugh Dickins @ 2005-01-28 15:53 UTC (permalink / raw)
  To: Rik van Riel
  Cc: William Lee Irwin III, Russell King, Mikael Pettersson,
	Andrew Morton, linux-kernel, James Antill, Bryn Reeves

On Fri, 28 Jan 2005, Rik van Riel wrote:
> 
> The main thing I would really like to preserve is the
> space used for "near-NULL" pointer detection. That is,
> detection of trying to access a large index in a NULL
> pointer array, etc.
> 
> I'd be happy to have some arbitrary value for the lower
> boundary...

Almost everything will still have that not-so-near-NULL pointer
detection.  It only gets limited to a PAGE_SIZE detection extent
in the case when the app mmaps as much as it possibly can.
I think it should be allowed make that tradeoff.

> > arch/ppc64/mm/hugetlbpage.c (odd place to find it) has its own
> > arch_get_unmapped_area_topdown, should be given a similar fix.
> 
> Good point, though a 64 bit architecture is, umm, less
> likely to run all the way down to zero within our lifetime.

I hadn't looked at it that way!

Hugh

^ permalink raw reply	[flat|nested] 40+ messages in thread

end of thread, other threads:[~2005-01-28 15:56 UTC | newest]

Thread overview: 40+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-01-26 16:18 don't let mmap allocate down to zero Rik van Riel
2005-01-26 16:38 ` linux-os
2005-01-26 17:05   ` Sytse Wielinga
2005-01-26 17:34   ` Chris Friesen
2005-01-26 17:57     ` Bryn Reeves
2005-01-26 18:37       ` linux-os
2005-01-26 18:54         ` Rik van Riel
2005-01-26 19:09           ` linux-os
2005-01-26 19:13         ` Chris Friesen
2005-01-26 19:08       ` Chris Friesen
2005-01-26 18:10   ` Olivier Galibert
2005-01-26 18:20     ` linux-os
2005-01-26 18:31       ` Olivier Galibert
2005-01-26 18:39         ` linux-os
2005-01-26 19:41   ` Arjan van de Ven
2005-01-26 20:26   ` Andy Isaacson
2005-01-26 20:42     ` Rik van Riel
2005-01-26 22:12   ` Kyle Moffett
2005-01-26 23:31   ` Brian Gerst
2005-01-26 17:25 ` William Lee Irwin III
2005-01-27  5:09   ` William Lee Irwin III
2005-01-27  5:18     ` Dave Jones
2005-01-27  5:28       ` William Lee Irwin III
2005-01-27  9:29     ` Mikael Pettersson
2005-01-27 12:52       ` William Lee Irwin III
2005-01-27 14:25         ` Russell King
2005-01-27 15:12           ` William Lee Irwin III
2005-01-27 19:22             ` Rik van Riel
2005-01-27 20:44               ` William Lee Irwin III
2005-01-27 20:58                 ` Rik van Riel
2005-01-27 21:13                   ` William Lee Irwin III
2005-01-27 21:28                     ` Rik van Riel
2005-01-28  5:30                       ` William Lee Irwin III
2005-01-28 13:01                         ` Rik van Riel
2005-01-28 14:14                           ` Hugh Dickins
2005-01-28 14:26                             ` William Lee Irwin III
2005-01-28 15:41                             ` Rik van Riel
2005-01-28 15:53                               ` Hugh Dickins
2005-01-27 21:58                     ` linux-os
2005-01-27 14:26         ` Mikael Pettersson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).