Re: Marking a Page of Memory Executable



In article <DkH1k.3477$E41.2245@xxxxxxxxxxxxxxxxxxxxxxxxx>,
Bartc <bc@xxxxxxxxxx> wrote:

What I want to do is get an executable and writable page of memory, so
that I can (say) write machine code to it and then switch %eip (the
instruction pointer on x86) to that page so that it will execute that
code, or something similar.

This sounds like a problem peculiar to your system. Under WinXP I don't have
a problem executing code created in my data:

Just because something isn't the same as Windows XP doesn't mean it's
"peculiar to your system". For decades processors and operating
systems have distinguished between executable and non-executable
memory, just as between writable and read-only memory.

(It's one of the deficiencies of the x86 architecture that it has
not generally been possible to make the stack non-executable, making
possible most of the buffer-overflow exploits that are so popular.)

Most operating systems have functions for controlling this, assuming
the processor supports it. In unix mmap() allows permissions to
be set on allocated memory, and mprotect() allows them to be changed.

int testfn(int a,int b) {return a*b;}

int main(void){
int (*newfn)(int,int);
int i;

newfn=malloc(100); /* assumed to work */
memcpy(newfn,&testfn,100); /* assumes testfn has <=100 bytes */

And it works for this example although crashes if testfn is changed, maybe
because some x86 code is not relocatable.

There's also no guarantee that all the code for testfn immediately
follows the address of testfn.

-- Richard
--
In the selection of the two characters immediately succeeding the numeral 9,
consideration shall be given to their replacement by the graphics 10 and 11 to
facilitate the adoption of the code in the sterling monetary area. (X3.4-1963)
.



Relevant Pages

  • Re: Marking a Page of Memory Executable
    ... What I want to do is get an executable and writable page of memory, ... instruction pointer on x86) to that page so that it will execute that ... mmap, munmap - map or unmap files or devices into memory ... void * mmap(void *start, size_t length, int prot, int ...
    (comp.lang.c)
  • Re: [PATCH - resend] Memory-Hotplug: Fix the bug on interface /dev/mem for 64-bit kernel
    ... The generic resource based page_is_ramworks better with memory ... So switch the x86 e820map based code to it. ... to not add the above two ranges into iomem_resource. ... int checktype) ...
    (Linux-Kernel)
  • Re: Executing data
    ... int testfn ... data segment, and it did actually work, but this is not a good example ... But I thread established I think that being able to execute data does depend ... on the OS amongst other things; the above used Winxp on x86. ...
    (comp.lang.c)
  • Re: coding in asm
    ... or a simply ret... ... it will execute whatever random bytes happened to be in memory ... > after the int 10. ...
    (microsoft.public.vc.language)
  • [GIT PULL] x86/platform changes for v2.6.38
    ... x86, mrst: The shutdown for MRST requires the SCU IPC mechanism ... extern int pci_mrst_init; ... * it under the terms of version 2 of the GNU General Public License as ...
    (Linux-Kernel)