/**** Kernel Problem Report & Test Case

Summary: mprotect() can't set a memory region as read-only

  In the course of our project development, we've noticed a major
flaw in the Linux 2.4.x kernels: in-process memory protection
just doesn't seem to work at all.

  I've tried the attached simple program on RedHat 2.4.9-17 and
the lastest 2.4.17 build from IBM, and both fail miserably:

On the s390 box, under both 2.4.x kernels, I get:

        [root@s390devel root]# ./foo
        Value of mem is 1234
        Protect 0x40018000 with 0x5
        Protect err=0 (0)
        Value of mem is 1235


The expected output (ie, on an x86 box) is the following:

        [root@x86devel root]# ./foo
        Value of mem is 1234
        Protect 0x40016000 with 0x5
        Protect err=0 (0)
        Segmentation fault (core dumped)

Note the "Segmentation fault" in the x86 case when I try to
write into the mprotect()ed memory...

*/
#include <stdio.h>
#include <errno.h>
#include <sys/mman.h>
#include <limits.h>
#ifndef PAGESIZE
#define PAGESIZE 4096
#endif

int *mem=NULL;

void doit(void)
{
  fprintf(stderr,"Value of mem is %x\n",*mem);
}

int main(int argc,char **argv)
{
  int err;

  mem=mmap(NULL,PAGESIZE,PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_ANONYMOUS,-1,0);
  *mem=0x1234;
  doit();

  fprintf(stderr,"Protect %p with 0x%x\n",mem,PROT_READ|PROT_EXEC);
  err=mprotect(mem,PAGESIZE, PROT_READ|PROT_EXEC);
  fprintf(stderr,"Protect err=%d (%d)\n",err,errno);
  *mem=0x1235;
  doit();

  return 0;
}

/**
Jason McMullan, Senior Linux Consultant
Linuxcare, Inc. 412.432.6457 tel, 412.656.3519 cell
[EMAIL PROTECTED], http://www.linuxcare.com/
Linuxcare. Putting open source to work. */

Reply via email to