All of lore.kernel.org
 help / color / mirror / Atom feed
From: Eric Smith <eric@brouhaha.com>
To: linux-kernel@vger.kernel.org
Subject: mmap to address zero with MAP_FIXED returns ENOPERM for non-root users?
Date: Sat, 04 Dec 2010 22:40:26 -0800	[thread overview]
Message-ID: <4CFB33DA.1070306@brouhaha.com> (raw)

I'm doing some work with binary translation of an executable from a 
non-x86 microcontroller to run in an x86 process, and need to have part 
of the memory map match that of the microcontroller.  This includes 
having some memory at virtual address zero.  I know why this isn't 
usually a good idea, and why mmap() won't give that out without MAP_FIXED.

However, when I try an anonymous mmap() to virtual address zero with 
MAP_FIXED, I get ENOPERM unless running as superuser.

Is this deliberate?  I really don't want to have to run my translated 
executable as superuser.

strace shows the system call, so I don't think glibc is the culprit.

I'm running Fedora 14 with kernel 2.6.35.6-48.fc14.x86_64.  I'm 
compiling with gcc 4.5.1 with the -m32 option to get a 32-bit 
executable, which I'm then running on the 64-bit kernel.  However, I 
seem to get the same behavior if I build a 64-bit executable.  My 
trivial test program is below.

Thanks!
Eric




#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>

int main (int argc, char *argv [])
{
   void *p;
   int fd = -1;
   int offset = 0;

   uint32_t addr;
   uint32_t length;

   if (argc != 3)
     {
       fprintf (stderr, "usage: %s <addr> <length>\n", argv [0]);
       return 1;
     }

   addr = strtoul (argv [1], NULL, 0);
   length = strtoul (argv [2], NULL, 0);

   printf ("attempting to create anonymous mapping at addr 0x%08x, 
length 0x%08x\n", addr, length);

   p = mmap ((void *) addr,
         length,
         PROT_READ | PROT_WRITE,
         MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED,
         fd,
         offset);

   if (p == MAP_FAILED)
     {
       perror ("mapping failed");
       return 2;
     }

   printf ("mapped at address 0x%08x\n", (uint32_t) p);
   return 0;
}


             reply	other threads:[~2010-12-05  6:50 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-12-05  6:40 Eric Smith [this message]
2010-12-05  7:24 ` mmap to address zero with MAP_FIXED returns ENOPERM for non-root users? Kyle Moffett
2010-12-05  7:47   ` Eric Smith
2010-12-05  8:08     ` Samuel Thibault

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4CFB33DA.1070306@brouhaha.com \
    --to=eric@brouhaha.com \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.