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 11-28-2004, 05:18 AM   #1
Damaged Soul
LQ Newbie
 
Registered: Nov 2004
Posts: 8

Rep: Reputation: 0
mprotect


I'm trying to use mprotect to change memory protections of the .text and .rodata segments of an already loaded shared library.

Code:
/* Wrapper for mprotect and VirtualProtect */
int MemoryProtect(void *addr, size_t len, unsigned long newProt, unsigned long *oldProt, char memType) {
	int retVal;

	#ifdef __linux__
		maddress alignAddr = ((maddress)addr + pageSize - 1) & ~(pageSize - 1);
		retVal = mprotect((void*)alignAddr, pageSize, newProt);
		switch (memType) {
			case MEMTYPE_CODE:
				*oldProt = MPROT_CODE;
				break;
			case MEMTYPE_RODATA:
				*oldProt = MPROT_RODATA;
				break;
			default:
				*oldProt = MPROT_CODE;
				break;
		}
	#else
		retVal = VirtualProtect(addr, len, newProt, oldProt);
		if (retVal == 0) {
			retVal = -1;
		} else {
			retVal = 0;
		}
	#endif

	MF_Log("MemoryProtect called. Return value is %d", retVal);

	return retVal;
}
I'm trying to get this working on both Linux and Windows, hence the #ifdef. The Windows code works perfectly fine. But it's the Linux part that doesn't seem to be working.

So I need to take an arbitrary memory address which is supposed to be in the .text or .rodata segments, align it to a page, and then use mprotect to change permissions. maddress is a type I created using typedef. It's either a uint32 or unit64 depending on whether the machine is x86 or AMD64.

pageSize is assigned a value earlier using sysconf(_SC_PAGESIZE). So when I call my MemoryProtect function and then proceed to write to the memory address afterward I will get a segfault. Now mprotect is returning 0, meaning there are supposedly no errors in changing the protection I guess. But it's still not working. I guess the way I tried aligning the address is incorrect? I did take the alignment code from a man page somewhere on the Internet, but I did have to modify it some. Maybe that is what it wrong... And my shared library, the one I'm writing is already attached to the process, so I should have sufficient access I think... But I really don't know.

But on the other hand, does the Linux kernel have special protections against changing the protection of the .text or .rodata segments? Maybe that is the problem instead. I'm not sure about it though.

Thanks in advance.

Last edited by Damaged Soul; 11-28-2004 at 05:34 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() 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

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

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