473,468 Members | 1,531 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

mprotect

Hi,
I'm having some trouble using mprotect. A short code snippet to give
you an idea of where this is heading:
char *p;

p = malloc(1024+PAGESIZE-1);
if (!p)
exit(errno);
p = (char *)(((int) p + PAGESIZE-1) & ~(PAGESIZE-1));
memset( (void*)p, 0xc3, 1024 ); /* set everything to "ret" */

/* here we set p to contain some binary code */

if (mprotect(p, 1024, PROT_EXEC|PROT_WRITE|PROT_READ)) {
exit(errno);
}
__asm__ __volatile__ (
"call *%0 \n"
:
: "m"(p)
);
exit(0);

Now the problem is that the call always causes a segfault. I tried
setting the memory to just PROT_EXEC with no luck... so I'm wondering:
Is there something else I need to do in order for Linux to allow me
execution of a certain page? I'm really out of ideas here... :/
When I pass the last asm segment a pointer to a function that I
declared in c++ it works fine. So it has to be the memory protection
not doing what I expect it to...

Jan 21 '07 #1
4 3467
Jan Althaus wrote:
Hi,
I'm having some trouble using mprotect. A short code snippet to give
you an idea of where this is heading:
<snip>

This is a Linux related question try posting to one of the groups under
the comp.os.linux hierarchy. Also try comp.unix.programmer.

Jan 21 '07 #2
"Jan Althaus" <he*******@gmail.comwrites:
I'm having some trouble using mprotect.
mprotect() is not a standard C function; try comp.unix.programmer.
But I'll add a few C-related comments as well.
A short code snippet to give
you an idea of where this is heading:
char *p;

p = malloc(1024+PAGESIZE-1);
if (!p)
exit(errno);
errno values and arguments to exit() are not necessarily related
(beyond the fact that 0 means "no error" for both). In standard C,
the only portable arguments to exit() are 0, EXIT_SUCCESS, and
EXIT_FAILURE. If you're not concerned with portability to all C
implementations (and since you're using mprotect(), presumably you're
not), you can use other values, but errno values aren't necessarily
sensible.
p = (char *)(((int) p + PAGESIZE-1) & ~(PAGESIZE-1));
memset( (void*)p, 0xc3, 1024 ); /* set everything to "ret" */
There's no need to cast the first argument to memset(); char*, or any
other pointer-to-object type, is implicitly converted to void*.
/* here we set p to contain some binary code */

if (mprotect(p, 1024, PROT_EXEC|PROT_WRITE|PROT_READ)) {
exit(errno);
}
__asm__ __volatile__ (
"call *%0 \n"
:
: "m"(p)
);
Obviously this is extremely non-portable; it's probably specific to
whatever compiler you're using (gcc?).
exit(0);

Now the problem is that the call always causes a segfault. I tried
setting the memory to just PROT_EXEC with no luck... so I'm wondering:
Is there something else I need to do in order for Linux to allow me
execution of a certain page? I'm really out of ideas here... :/
When I pass the last asm segment a pointer to a function that I
declared in c++ it works fine. So it has to be the memory protection
not doing what I expect it to...
Declared in C++? If your code is C++ rather than C (which may also
affect implicit conversions to void*), you should have wrongly posted
to comp.lang.c++ before being redirected to comp.unix.programmer.
8-)}

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jan 21 '07 #3
Sorry, last one was a typo. It's C code of course.
Thanks for the feedback! I'll keep it in mind.
Regarding portability: Seeing as I'd only like to get the basic
principle working I'm not really cocerned with that right now. :)
Keith Thompson wrote:
"Jan Althaus" <he*******@gmail.comwrites:
I'm having some trouble using mprotect.

mprotect() is not a standard C function; try comp.unix.programmer.
But I'll add a few C-related comments as well.
A short code snippet to give
you an idea of where this is heading:
char *p;

p = malloc(1024+PAGESIZE-1);
if (!p)
exit(errno);

errno values and arguments to exit() are not necessarily related
(beyond the fact that 0 means "no error" for both). In standard C,
the only portable arguments to exit() are 0, EXIT_SUCCESS, and
EXIT_FAILURE. If you're not concerned with portability to all C
implementations (and since you're using mprotect(), presumably you're
not), you can use other values, but errno values aren't necessarily
sensible.
p = (char *)(((int) p + PAGESIZE-1) & ~(PAGESIZE-1));
memset( (void*)p, 0xc3, 1024 ); /* set everything to "ret" */

There's no need to cast the first argument to memset(); char*, or any
other pointer-to-object type, is implicitly converted to void*.
/* here we set p to contain some binary code */

if (mprotect(p, 1024, PROT_EXEC|PROT_WRITE|PROT_READ)) {
exit(errno);
}
__asm__ __volatile__ (
"call *%0 \n"
:
: "m"(p)
);

Obviously this is extremely non-portable; it's probably specific to
whatever compiler you're using (gcc?).
exit(0);

Now the problem is that the call always causes a segfault. I tried
setting the memory to just PROT_EXEC with no luck... so I'm wondering:
Is there something else I need to do in order for Linux to allow me
execution of a certain page? I'm really out of ideas here... :/
When I pass the last asm segment a pointer to a function that I
declared in c++ it works fine. So it has to be the memory protection
not doing what I expect it to...

Declared in C++? If your code is C++ rather than C (which may also
affect implicit conversions to void*), you should have wrongly posted
to comp.lang.c++ before being redirected to comp.unix.programmer.
8-)}

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jan 22 '07 #4
In article <11**********************@a75g2000cwd.googlegroups .com>,
Jan Althaus <he*******@gmail.comwrote:
>Sorry, last one was a typo. It's C code of course.
In the theology of this ng, it is *not* C code.

You may (quite sensibly) think otherwise.
>Thanks for the feedback! I'll keep it in mind.
Regarding portability: Seeing as I'd only like to get the basic
principle working I'm not really cocerned with that right now. :)
That is no more permissible a view here than would be the view that
abortion is permissible when addressing a KofC audience.

Jan 22 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jeremy C. Reed | last post by:
Configuring gramps (genealogy software) says: checking Python bindings for gtk... ok checking Python bindings for GNOME... ok checking Python bindings for gconf... Traceback (most recent call...
3
by: subnet | last post by:
I'm trying to write a very simple program that uses a signal-based synchronization between two processes. Here's it: ----------------------------------------------- /* The world's simplest...
5
by: Sontu | last post by:
Hi all, Consider the following code: #include<signal.h> #include<stdio.h> #include<sys/mman.h> void handler(int sig) {
10
by: Markus.Elfring | last post by:
Some APIs/SDKs are available to modify settings like "readable", "writeable" or "executeable". Examples: - http://msdn.microsoft.com/library/en-us/memory/base/memory_protection_constants.asp -...
0
by: Kirt Loki Dankmyer | last post by:
So, I download the latest "stable" tar for perl (5.8.7) and try to compile it on the Solaris 8 (SPARC) box that I administrate. I try all sorts of different switches, but I can't get it to compile....
14
by: spamtrap | last post by:
Mostly for testing reasons I'd like to see if it makes sense to chose the following approach for just-in-time compilation of shaders for a renderer: Seeing as the shaders themsefs consist mostly...
1
by: neojia | last post by:
hi, I am not sure if this is the proper list to ask this question. If not, I would appreciate if you can point me to the right place. I am going to use pread64 open file as /proc/(pid)/mem,...
3
by: Javier | last post by:
Hello all, I need to know the protection of a memory region in UNIX (protected with mprotect) system call. Is there a way to do it? Thanks.
21
by: omkar pangarkar | last post by:
Hi all, I have two simple hello world programs one using printf() and other using write() --prog 1-- #include<stdio.h> #include<stdlib.h> int main() { printf("Hello"); /* up to here...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.