LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-19-2008, 07:44 PM   #1
raghu2383
LQ Newbie
 
Registered: Jun 2008
Posts: 20

Rep: Reputation: 0
How to align memory for mprotect()


I am trying to write on the code section of a running process. The code is below:
Code:
#include <sys/mman.h>
#include <limits.h>
#ifndef PAGESIZE
#define PAGESIZE 4096
#endif
unsigned char buffer[1024];
int blank();

int main()
{
 unsigned  long *p;
 unsigned char c;
 unsigned long *d;
 int i;
 p = &blank;
 
 d  = (unsigned long *)(((int) p + PAGESIZE-1) & ~(PAGESIZE-1));

 if (mprotect(d,4096,PROT_WRITE))
  {
   perror("Could not mprotect");
   exit(errno);
  }
  printf("\naddress contained in p %x add cotained in d %x address of blank %x", p, d, blank);
 printf("\n address of d[0] %x, address of d[1] %x",d, (d+1)); 
 printf("\n address of d[0] %x, address of d [-1] %x d[-2] %x",d, d-1, (d-2)); 
p[1] = 0x89;
  return 0;
 
}
int blank()
{
  int index=0;
  int i;
  unsigned char *p;
  p = &main;
  printf("here"); 
 for (i=0;i<=203;i++)
  buffer[index++] = p[i];
  buffer[index]='\0';
  return 0;
}
The program prints
p = 8048536, d = 8049000, blank = 8048536, d[0] =8049000, d[1] = 8049004

So what is happening is that 'd' (the write enabled page) is getting aligned to 1 page after 'p'. So when I try to do a write on p[1], it segment faults. Can someone help me on how to align d such that 'p' becomes write enabled?
any help is appreciated
Thanks
 
Old 06-19-2008, 10:47 PM   #2
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Don't you want to mask off PAGESIZE-1 from p (the address of blank), leaving you with the base page in which blank() resides?
 
Old 06-20-2008, 12:52 AM   #3
raghu2383
LQ Newbie
 
Registered: Jun 2008
Posts: 20

Original Poster
Rep: Reputation: 0
Yes, so what I tried doing was
Code:
 d  = (unsigned long *)(((int) p + PAGESIZE-1) & ~(PAGESIZE-1));
 d= d-4096; // should move pointer back one page
This one gives an error : Could not allocate memory on the mprotect() call.
Thanks in advance
 
Old 06-20-2008, 01:09 AM   #4
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
I can't tell from your description what the errno is.
 
Old 06-20-2008, 09:29 AM   #5
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
Code:
#include <unistd.h>
long sz = sysconf(_SC_PAGESIZE);\
Is this 4096 for your system?
 
Old 06-20-2008, 03:29 PM   #6
raghu2383
LQ Newbie
 
Registered: Jun 2008
Posts: 20

Original Poster
Rep: Reputation: 0
Thank you for responding, I got it working, I had to do
d = (unsigned long *) ((int) p &~(PAGESIZE-1));
if (mprotect(d, 1024, PROT_WRITE|PROT_EXEC))
{
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
mprotect() return codes tim_l Programming 2 05-09-2005 07:38 AM
mprotect and SIGSEGV tim_l Programming 1 05-04-2005 08:12 AM
Call to mprotect() tim_l Linux - General 1 05-03-2005 09:44 AM
mprotect Damaged Soul Programming 0 11-28-2004 05:18 AM
align cartridges nielchiano Linux - Hardware 3 02-03-2004 04:57 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration