LinuxQuestions.org
Review your favorite Linux distribution.
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 03-04-2015, 11:47 AM   #1
asif_bahrainwala
Member
 
Registered: Mar 2015
Posts: 47

Rep: Reputation: Disabled
mprotect


Hi,
I am trying to change the page protection using mprotect, it throws memory violation
cant seem to understand the problem.

below is the C code

#include <stdio.h>
#include <sys/mman.h>
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/mman.h>

int main()
{
char *str="my name";
int pagesize = sysconf(_SC_PAGE_SIZE);

int i=mprotect(pagesize*((int)str/pagesize),pagesize,PROT_WRITE); //calculate the page boundary

printf("%x %x \n",pagesize*((int)str/pagesize),str);
if(i==-1)
printf("%d\n",errno);

str[1]='b';

printf(str);
return 0;
}
 
Old 03-04-2015, 12:11 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,874
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Exactly what did you want to do with this?
At the very least, be a bit more clear:

Code:
intptr_t addr = (intptr_t)str;
intptr_t rounded_addr = ((addr + pagesize-1)/pagesize)*pagesize;

printf ("addr=p rounded_addr=%p\n", (void *)addr, (void *)rounded_addr);
Note: of course you cannot protect automatic variables via mprotect...

Last edited by NevemTeve; 03-04-2015 at 01:19 PM. Reason: Missing '' in '%p'
 
Old 03-04-2015, 12:19 PM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,786

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by asif_bahrainwala View Post
Hi,
I am trying to change the page protection using mprotect, it throws memory violation
cant seem to understand the problem.
Is the violation on read or write? Because you didn't pass PROT_READ although you try to read str later...


Quote:
Originally Posted by NevemTeve
Note: of course you cannot protect automatic variables via mprotect...
?
Quote:
http://linux.die.net/man/2/mprotect

On Linux it is always permissible to call mprotect() on any address in a process's address space (except for the kernel vsyscall area).
 
Old 03-04-2015, 12:57 PM   #4
asif_bahrainwala
Member
 
Registered: Mar 2015
Posts: 47

Original Poster
Rep: Reputation: Disabled
Hi,
I need to change hook certain functions by altering its opcode, (something similar to MS detour or mhook)

anyways from a learning point of view, why does the above attached code crash at mprotect

using Zorin OS 9.1 (Ubuntu)
 
Old 03-04-2015, 01:18 PM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,874
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
> > Note: of course you cannot protect automatic variables via mprotect...

> ?

Well, yes, I was wrong, actually he wants to 'unprotect' the string literal.

(What I thought he wanted was switching the stack (or a part of it) to read-only.
Of course that would end the program very quickly.

Last edited by NevemTeve; 03-04-2015 at 02:16 PM.
 
Old 03-04-2015, 01:30 PM   #6
asif_bahrainwala
Member
 
Registered: Mar 2015
Posts: 47

Original Poster
Rep: Reputation: Disabled
uhhh...any help
why is mprotect not working.......
can you get mprotect to work,
please post your code
 
Old 03-04-2015, 02:07 PM   #7
genss
Member
 
Registered: Nov 2013
Posts: 744

Rep: Reputation: Disabled
a quick way to get the aligned adress

(int)pointer & ~ 0xFFF
edit: 4k is x86 and amd64, other cpu architectures may use a diff alignment
(so use the getpagesize from above, or better yet if there is a header that declares it)

another, maybe faster, way would be to use two shifts



idk why it fails, not one for thinking tonight

Last edited by genss; 03-04-2015 at 02:10 PM.
 
Old 03-04-2015, 02:11 PM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,874
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Use gdb to find out where your program stops:

Code:
$ gcc -g -W -Wall -Wextra -Werror -o prog prog.c
$ gdb prog
(gdb) run
(gdb) bt
 
Old 03-04-2015, 02:49 PM   #9
asif_bahrainwala
Member
 
Registered: Mar 2015
Posts: 47

Original Poster
Rep: Reputation: Disabled
Hi,
I know where/when it stops...I am debugging using QT.

can you post a working code (fixing the code mentioned above), I don't want ideas on how to solve it since I would have tried them already, please copy paste the code to your editor, compile, run and figure out the reason for violation......
 
Old 03-04-2015, 02:54 PM   #10
genss
Member
 
Registered: Nov 2013
Posts: 744

Rep: Reputation: Disabled
Quote:
Originally Posted by asif_bahrainwala View Post
Hi,
I know where/when it stops...I am debugging using QT.

can you post a working code (fixing the code mentioned above), I don't want ideas on how to solve it since I would have tried them already, please copy paste the code to your editor, compile, run and figure out the reason for violation......
i understand the language barrier

people will be willing to help you if you try to do it yourself and post on what you got stuck

also remember to use the CODE ("#" button above) tags when pasting code on the forum
 
Old 03-04-2015, 02:57 PM   #11
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,786

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by asif_bahrainwala View Post
I don't want ideas on how to solve it since I would have tried them already,
Clearly you didn't try the idea that actually works, so there is at least one you haven't tried already...
 
1 members found this post helpful.
Old 03-04-2015, 03:46 PM   #12
asif_bahrainwala
Member
 
Registered: Mar 2015
Posts: 47

Original Poster
Rep: Reputation: Disabled
I am stuck at mprotect , it throws an access violation

please try the code and let me know your inputs on solving it
That is: try the solution on your own machine , then post it on this thread

---------- Post added 03-04-15 at 04:47 PM ----------

Quote:
Originally Posted by ntubski View Post
Clearly you didn't try the idea that actually works, so there is at least one you haven't tried already...
which is why I am on this forum.....think you can solve it ;-)
 
Old 03-04-2015, 04:12 PM   #13
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,153

Rep: Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265
You are trying to change the protection on a page containing a constant string, so it would be in your program's text segment. The value that you gave turns off read and execute access on the whole page, which is likely to instantly kill your program. Allocate a new page and mess with that, not parts of your program text.
 
1 members found this post helpful.
Old 03-04-2015, 04:41 PM   #14
asif_bahrainwala
Member
 
Registered: Mar 2015
Posts: 47

Original Poster
Rep: Reputation: Disabled
Thanks....
after a bit of probing the problem is as you described.

Last edited by asif_bahrainwala; 03-05-2015 at 01:06 AM.
 
  


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 asif_bahrainwala Linux - Newbie 3 03-06-2015 12:14 AM
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

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

All times are GMT -5. The time now is 11:54 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