All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Vincent W. Freeh" <vin@csc.ncsu.edu>
To: linux-kernel@vger.kernel.org
Subject: Re: Understanding Linux addr space, malloc, and heap
Date: Fri, 21 Oct 2005 12:04:46 -0400	[thread overview]
Message-ID: <4359119E.6050407@csc.ncsu.edu> (raw)
In-Reply-To: <1129909719.2786.27.camel@laptopd505.fenrus.org>

Clearly, it was a mistake to post that code.  I had no idea so many 
people would point out the bleeding obvious.

Here is a more elaborate version--that does the same thing, but more 
lines of code.  In it malloc'd memory is mprotect'd.  The program 
generates a SIGSEGV, a page fault.

----------------
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/mman.h>

#include <limits.h>    /* for PAGESIZE */
#ifndef PAGESIZE
#define PAGESIZE 4096
#endif

int
main(void)
{
   char *p;
   char c;

   /* Allocate a buffer; it will have the default
      protection of PROT_READ|PROT_WRITE. */
   p = malloc(1024+PAGESIZE-1);
   if (!p) {
     perror("Couldn’t malloc(1024)");
     exit(errno);
   }

   /* Align to a multiple of PAGESIZE, assumed to be a power of two */
   p = (char *)(((int) p + PAGESIZE-1) & ~(PAGESIZE-1));

   c = p[666];         /* Read; ok */
   p[666] = 42;        /* Write; ok */

   /* Mark the buffer read-only. */
   if (mprotect(p, 1024, PROT_READ)) {
     perror("Couldn’t mprotect");
     exit(errno);
   }

   c = p[666];         /* Read; ok */
   p[666] = 42;        /* Write; program dies on SIGSEGV */

   exit(0);
}


Arjan van de Ven wrote:
>>But I can't mprotect the 66th page I malloc.  And mprotect fails SILENTLY!
> 
> 
> I'm not convinced it does that.. not until the bugs are out of the
> code.... since right now it mprotects the wrong stuff, which sometimes
> overlaps with what you malloced, sometimes not.
> 
> 

  reply	other threads:[~2005-10-21 16:04 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-10-21 13:45 Understanding Linux addr space, malloc, and heap Vincent W. Freeh
2005-10-21 14:03 ` Arjan van de Ven
2005-10-21 15:11   ` Vincent W. Freeh
2005-10-21 15:20     ` Anton Altaparmakov
2005-10-21 15:21     ` Paulo Marques
2005-10-21 15:22     ` Arjan van de Ven
2005-10-21 15:37       ` Vincent W. Freeh
2005-10-21 15:48         ` Arjan van de Ven
2005-10-21 16:04           ` Vincent W. Freeh [this message]
2005-10-21 16:23             ` Arjan van de Ven
2005-10-21 15:52         ` Kyle Moffett
2005-10-21 16:10           ` Vincent W. Freeh
2005-10-21 16:19             ` Theodore Ts'o
2005-10-21 16:26             ` Paulo Marques
2005-10-21 16:14         ` Andreas Schwab
2005-10-21 16:24           ` Vincent W. Freeh
2005-10-22 19:27             ` Kyle Moffett
2005-10-21 15:37       ` Alex Bligh - linux-kernel
2005-10-21 15:47         ` Arjan van de Ven
2005-10-21 15:58           ` Paulo Marques
     [not found] <505ru-8qi-1@gated-at.bofh.it>
     [not found] ` <505Lp-B4-81@gated-at.bofh.it>
     [not found]   ` <506QZ-2cH-3@gated-at.bofh.it>
     [not found]     ` <5070Y-2qP-23@gated-at.bofh.it>
     [not found]       ` <507ac-2Cm-25@gated-at.bofh.it>
     [not found]         ` <507NL-3Em-29@gated-at.bofh.it>
     [not found]           ` <507Xd-3QT-19@gated-at.bofh.it>
     [not found]             ` <50xnU-7s2-37@gated-at.bofh.it>
2005-10-23 10:41               ` Bodo Eggert
2005-10-23 10:44                 ` Arjan van de Ven
2005-10-23 21:29                   ` Kyle Moffett
  -- strict thread matches above, loose matches on Subject: below --
2005-10-21 12:46 Vincent W. Freeh
2005-10-21 13:00 ` Arjan van de Ven

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=4359119E.6050407@csc.ncsu.edu \
    --to=vin@csc.ncsu.edu \
    --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.