Note that there are some explanatory texts on larger screens.

plurals
  1. POCopying part of the stack and using mmap to map it to the current process
    primarykey
    data
    text
    <p>I want my program to do the following:</p> <ol> <li>Open a new file.</li> <li>Copy a (page-aligned) portion of the stack that includes the current frame pointer address to the file.</li> <li>Map the contents of the file back into the process's address space in the same range as that of the original portion of the stack, so that the process will use the file for that part of its stack rather than the region of memory the system had originally allocated to it for the stack.</li> </ol> <p>Below is my code. I am getting a segmentation fault on the call to mmap, specifically where mmap makes the system call with vsyscall. (I am working with gcc 4.4.3, glibc 2.11.1, under Ubuntu Server (x86-64). I have compiled and run both with 64-bit and 32-bit configurations, with the same results.</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;stdbool.h&gt; #include &lt;stdint.h&gt; #include &lt;string.h&gt; #include &lt;sys/mman.h&gt; #include &lt;assert.h&gt; #include &lt;unistd.h&gt; #include &lt;sys/mman.h&gt; #include &lt;sys/types.h&gt; #include &lt;sys/wait.h&gt; #define PAGE_SIZE 0x1000 #define FILENAME_LENGTH 0x10 #if defined ARCH &amp;&amp; ARCH == 32 #define PAGE_SIZE_COMPLEMENT 0xfffff000 #define UINT uint32_t #define INT int32_t #define BP "ebp" #define SP "esp" #define X_FORMAT "%x" #else #define PAGE_SIZE_COMPLEMENT 0xfffffffffffff000 #define UINT uint64_t #define INT int64_t #define BP "rbp" #define SP "rsp" #define X_FORMAT "%lx" #endif #define PAGE_ROUND_UP(v) (((v) + PAGE_SIZE - 1) &amp; PAGE_SIZE_COMPLEMENT) #define PAGE_ROUND_DOWN(v) ((v) &amp; PAGE_SIZE_COMPLEMENT) UINT stack_low, stack_high, stack_length; void find_stack_high(void) { UINT bp = 0; UINT raw_stack_high = 0; /* Set the global stack high to the best * approximation. */ asm volatile ("mov %%"BP", %0" : "=m"(bp)); while (bp) { raw_stack_high = bp; bp = *(UINT *)bp; } stack_high = PAGE_ROUND_UP(raw_stack_high); } int file_create(void) { int fd; char filename[FILENAME_LENGTH]; strcpy(filename, "tmp.XXXXXX"); fd = mkstemp(filename); if (fd == -1) { perror("file_create:mkstemp"); exit(EXIT_FAILURE); } unlink(filename); return fd; } int main(void) { int fd, bytes_written; UINT bp; off_t offset; printf("In main\n"); fd = file_create(); printf("fd %d\n", fd); find_stack_high(); // Get the current frame pointer. asm volatile ("mov %%"BP", %0" : "=m" (bp)); // Store page boundary below // frame pointer as end of potentially shared stack. stack_low = PAGE_ROUND_DOWN(bp); stack_length = stack_high - stack_low; printf("start "X_FORMAT" end "X_FORMAT" length "X_FORMAT"\n", stack_low, stack_high, stack_length); bytes_written = write(fd, (const void *)stack_low, PAGE_SIZE); if (bytes_written != PAGE_SIZE) { perror("main: write"); fprintf(stderr, "Num bytes: %x\n", bytes_written); exit(EXIT_FAILURE); } offset = 0; if (mmap((void *)stack_low, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED | MAP_GROWSDOWN, fd, offset) == MAP_FAILED) { perror("file_copy: mmap"); exit(EXIT_FAILURE); } close(fd); return EXIT_SUCCESS; } </code></pre> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload