tags:

views:

109

answers:

4

is it possible but I guess that memory is read only. I tried something like that

void (*b)();

b = *test;

char * z = (char * )b;

and when I print contents of z, it prints constants from test function.

but that memory is read only and I get segfaults when I edit them and call test. I tried this.

char x[100];

for(i=0; i<100; i++) x[i] = z[i];

b = (void(*)())x;

b();

and segfault again.

I know this is kinda stupid, but is there a way to do this? I think there is a way to change code memory to read/write but what I really need is being able to send functions over network and make a remote machine to run them.

edit: this is not for malicious use. what I trying to achieve creating a network which can share code parts between them and run a program parallelly on different part of same input

+1  A: 

The ability to modify executable code (during execution) depends on the operating system and all operating systems differ at this level. Some require high levels of protection (*nix), while others may have code in ROM (embedded systems).

Executing code on a remote computer is not standard either. You'll have to investigate the operating system and figure out how to do this. I know the Unix operating system allows execution of programs on remote Unix machines.

Post more details about what you are trying to accomplish, so we may better assist you.

Thomas Matthews
@Thomas I think it's kinda obvious.
ruslik
it is for a distributed operating system course projects, I am planning to create a computer network which can run parallel programs easily using my library.for example there is a function and an input. and more than 1 computer can work on different part of that input using same function. so I have to send contents of function, input data and which part of inputs they will work on etc to other computers and they will run that code on that part of input.is it possible? I think I can send files directly to other machine and they can run them as a different process etc
Engin Mercan
@Engin Study then. Just knowing the trick won't be enough.
ruslik
yeah, for now all I am trying to learn if it is possible to do it before choosing my project
Engin Mercan
@Engin: your teachers should be in a much better shape to advise you on which project to chose than we are, here on SO. This is their job.
Jens Gustedt
+1  A: 

It's possible to write code into memory and then execute it. JIT compilers for Java and .NET do this regularly.

Wikipedia has some background information here.

As far as sending the code and having it execute remotely, you need the assistance of the remote machine (it should be designed to receive code, put it into memory, then run it, protected by appropriate security of course).

Ben Voigt
.. or have a convenient buffer overflow bug for you
pm100
thanks. is it possible to change nx bit for a memory on heap on ubuntu (or another linux distro)? I think I might even alter the kernel and add a system call for that. it does not look like that hard
Engin Mercan
@Engin: try "man mprotect"
Ben Voigt
@pm100: There are legitimate uses to executing a memory buffer as code. Mostly JIT compilation. Buffer overflows OTOH are definitely into the realm of illegitimate.
Ben Voigt
thanks guys! exactly what I need
Engin Mercan
A: 

Normally, functions are stored inside read-only memory sections (on ELF systems, that's the .text section inside the ELF file; almost always gets mmap'ed read-only). And as long as the function isn't PIC (Position Independent Code, as in UNIX shared libraries) you can't copy and execute it (or if you can, then by pure chance).

DarkDust
A: 

You're looking for the ability to write a loader.

The target architecture/host is going to determine the ease of use for that.

E.g., if you are sending Lisp, it's just strings, then a command to eval them. OTOH, if you are doing this with Windows 7 and managed C# code, this could be a real grief.

Technically, yes, this is very doable with a von Neumann architecture. It's also sort of a security hole.

Paul Nathan
yeah, I am planning to learn more about lisp if I fail to do it on c
Engin Mercan