复制部分堆栈并使用 mmap 将其映射到当前进程

发布于 2024-10-06 23:31:02 字数 2918 浏览 0 评论 0原文

我希望我的程序执行以下操作:

  1. 打开一个新文件。
  2. 将包含当前帧指针地址的堆栈(页对齐)部分复制到文件。
  3. 将文件的内容映射回进程的地址空间,其范围与堆栈的原始部分相同,以便进程将文件用于其堆栈的该部分,而不是系统最初拥有的内存区域分配给它的堆栈。

下面是我的代码。我在调用 mmap 时遇到分段错误,特别是 mmap 使用 vsyscall 进行系统调用的地方。 (我正在 Ubuntu Server (x86-64) 下使用 gcc 4.4.3、glibc 2.11.1。我已使用 64 位和 32 位配置编译并运行这两个版本,结果相同。

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <sys/mman.h>
#include <assert.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/wait.h>

#define PAGE_SIZE 0x1000
#define FILENAME_LENGTH 0x10
#if defined ARCH && 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) & PAGE_SIZE_COMPLEMENT)
#define PAGE_ROUND_DOWN(v) ((v) & 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;
}

谢谢!

I want my program to do the following:

  1. Open a new file.
  2. Copy a (page-aligned) portion of the stack that includes the current frame pointer address to the file.
  3. 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.

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.

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <sys/mman.h>
#include <assert.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/wait.h>

#define PAGE_SIZE 0x1000
#define FILENAME_LENGTH 0x10
#if defined ARCH && 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) & PAGE_SIZE_COMPLEMENT)
#define PAGE_ROUND_DOWN(v) ((v) & 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;
}

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心不设防 2024-10-13 23:31:03

复制后堆栈会发生变化(例如 mmap 调用的返回地址)。我可以想到两种可能的方法来解决这个问题:

  1. 编写不需要堆栈来执行新映射的汇编语言。
  2. 调用具有大量本地数据的函数,以便工作堆栈位于与您映射的页面不同的页面上。然后,一旦该函数返回,您就可以通过第二次调用 mmap 来映射较低的地址。

无论你做什么,这都是一个可怕的黑客行为,而且可能是一个坏主意。

The stack changes (e.g. the return address for the mmap call) after you copied it. I can think of 2 possible ways around this:

  1. Write asm that doesn't need the stack to perform the new mapping.
  2. Call into a function with some huge local data so that the working stack is on a different page from the pages you're mapping over. Then, you could map over the lower addresses with a second call to mmap once this function returns.

Whatever you do, this is a horrible hack and probably a bad idea..

固执像三岁 2024-10-13 23:31:03

尝试打开执行权限?无论如何,症状表明您已成功映射到堆栈顶部,从而破坏了返回指针。

Tried turning on execute permission? In any case, the symptom suggests that you've managed to map in over the top of the stack, destroying the return pointer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文