Questions tagged [mprotect]

88 questions
81
votes
6 answers

How to write a signal handler to catch SIGSEGV?

I want to write a signal handler to catch SIGSEGV. I protect a block of memory for read or write using char *buffer; char *p; char a; int pagesize = 4096; mprotect(buffer,pagesize,PROT_NONE) This protects pagesize bytes of memory starting at…
Adi
  • 1,589
  • 3
  • 19
  • 27
18
votes
3 answers

How can I call inlined machine code in Python on Linux?

I'm trying to call inlined machine code from pure Python code on Linux. To this end, I embed the code in a bytes literal code = b"\x55\x89\xe5\x5d\xc3" and then call mprotect() via ctypes to allow execution of the page containing the code. …
Walter
  • 183
  • 6
18
votes
2 answers

Is there a better way than parsing /proc/self/maps to figure out memory protection?

On Linux (or Solaris) is there a better way than hand parsing /proc/self/maps repeatedly to figure out whether or not you can read, write or execute whatever is stored at one or more addresses in memory? For instance, in Windows you have…
Edward Kmett
  • 29,632
  • 7
  • 85
  • 107
15
votes
3 answers

Segmentation fault when calling a function located in the heap

I'm trying to tweak the rules a little bit here, and malloc a buffer, then copy a function to the buffer. Calling the buffered function works, but the function throws a Segmentation fault when i'm trying to call another function within. Any thoughts…
Delights
  • 349
  • 4
  • 10
8
votes
1 answer

Using mprotect to make text segment writable on macOS

This is essentially what I'm trying to do, #include int zero() { return 0; } int main(int argc, const char *argv[]) { return mprotect((void *) &zero, 4096, PROT_READ | PROT_WRITE); } so I'm trying to make code writable,…
8
votes
2 answers

Does mmap allocate a page or part of a page?

I'm confused, does mmap allocate an entire page of memory (regardless of size specified), or does it just allocate the size you request? Really, I'm curious about what happens on subsequent calls to mmap -- would a second call allocate a new page…
sircodesalot
  • 11,231
  • 8
  • 50
  • 83
8
votes
2 answers

mprotect() like functionality within Linux kernel

I am in a Linux kernel module, and I allocate some memory with, say, vmalloc(). I want to make the memory have read, write, and execute permission. What is the clean and appropriate way of doing that? Basically, this is generally the equivalent…
Dave
  • 320
  • 3
  • 9
7
votes
2 answers

mprotect on a mmap-ed shared memory segment

When two processes share a segment of memory opened with shm_open and then it gets mmap-ed, does doing an mprotect on a portion of the shared memory in one process affects the permissions seen by the other process on this same portion? In other…
sunmat
  • 6,976
  • 3
  • 28
  • 44
7
votes
2 answers

Behaviour of PROT_READ and PROT_WRITE with mprotect

I've been trying to use mprotect against reading first, and then writing. Is here my code #include #include #include #include #include int main(void) { int pagesize =…
Aif
  • 11,015
  • 1
  • 30
  • 44
6
votes
1 answer

Behavior of mprotect with multiple threads

For the purpose of concurrent/parallel GC, I'm interested in what memory order guarantee is provided by the mprotect syscall (i.e. the behavior of mprotect with multiple threads or the memory model of mprotect). My questions are (assuming no…
6
votes
3 answers

Does mprotect flush the instruction cache on ARM Linux?

I am writing a JIT on ARM Linux that executes an instruction set that contains self-modifying code. The instruction set does not have any cache flush instructions (similar to x86 in that respect). If I write out some code to a page and then call…
Adam Goode
  • 7,380
  • 3
  • 29
  • 33
6
votes
1 answer

Why is my mprotect function called with 5 arguments?

According to the Linux man page for mprotect the function has 3 arguments: int mprotect(const void *addr, size_t len, int prot); but while running ltrace on a program that I'm analyzing I see that mprotect is called like this: mprotect(0x8049000,…
woolagaroo
  • 1,542
  • 2
  • 22
  • 31
5
votes
1 answer

mprotect entire program, to run dangerous code

I have a small program that mmaps potentially dangerous executable code (with PROT_EXEC), calls prctl(PR_SET_SECCOMP, 1) and then executes this mmap'd code. This is all well and good, and allows me to "save" the state of the evaluation by sync the…
Heptic
  • 3,076
  • 4
  • 30
  • 51
5
votes
2 answers

Can I write-protect every page in the address space of a Linux process?

I'm wondering if there's a way to write-protect every page in a Linux process' address space (from inside of the process itself, by way of mprotect()). By "every page", I really mean every page of the process's address space that might be written…
Lindsey Kuper
  • 984
  • 8
  • 21
5
votes
3 answers

Loading MachineCode From File Into Memory and Executing in C -- mprotect Failing

Hi I'm trying to load raw machine code into memory and run it from within a C program, right now when the program executes it breaks when trying to run mprotect on the memory to make it executable. I'm also not entirely sure that if the memory does…
1
2 3 4 5 6