RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1480431
Accepted
eri
eri
Asked:2022-12-23 16:26:16 +0000 UTC2022-12-23 16:26:16 +0000 UTC 2022-12-23 16:26:16 +0000 UTC

在不写入磁盘的情况下运行 elf 应用程序

  • 772

有一个go编译的二进制可执行文件。我正在制作一个启动它的主管程序(现在是 fork+execv)。

但是是否可以在不写入磁盘的情况下从字节数组运行它?

如果不是,是否可以在没有名称的情况下启动文件描述符?对 tmpfile 的调用使 readlink 说该文件已被删除。是否可以通过打开描述符或 inode 编号运行?这里的问题就出现了,文件是打开写的,关闭的时候打不开,因为会被彻底删除。

对于这种情况,如果您使用 mkstemp 打开文件,一切正常,但使用 tmpfile

#include "bin.h"
#include <unistd.h>
#include <stdio.h>
// #include <fcntl.h>
#include <sys/stat.h>
#include <stdlib.h>

#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>

int main(int argc, char *argv[], char *envp[])
{
    int r;
    FILE *tmp = tmpfile();
    int fd = fileno(tmp);
    write(fd, __bin, __bin_len);
    fsync(fd);
    fchmod(fd, S_IRWXU);
    FILE *tmp2 = fdopen(fd, "r");
    fclose(tmp);
    fd = fileno(tmp2);
    pid_t pid;
    switch (pid = fork())
    {
    case -1:
        perror("fork");
        exit(1);        
    case 0:
        getppid();
        //fclose(tmp2);
        return 0;
    default:
        return fexecve(fd, argv, envp);
    }
    return 1;
}

这样的流浪

.....
openat(AT_FDCWD, "/tmp", O_RDWR|O_EXCL|O_TMPFILE, 0600) = 3
fcntl(3, F_GETFL)                       = 0x418002 (flags O_RDWR|O_LARGEFILE|O_TMPFILE)
getrandom("\xc1\x83\x35\x01\xbe\xec\x96\x5a", 8, GRND_NONBLOCK) = 8
brk(NULL)                               = 0x55c875ae0000
brk(0x55c875b01000)                     = 0x55c875b01000
write(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\360d\0\0\0\0\0\0"..., 352808) = 352808
fsync(3)                                = 0
fchmod(3, 0700)                         = 0
fcntl(3, F_GETFL)                       = 0x418002 (flags O_RDWR|O_LARGEFILE|O_TMPFILE)
close(3)                                = 0
clone(child_stack=NULL, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7f6e46e76910) = 348850
execveat(3, "", ["./nano"], 0x7ffe01dcd068 /* 60 vars */, AT_EMPTY_PATH) = -1 EBADF (Неправильный дескриптор файла)
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=348850, si_uid=1000, si_status=0, si_utime=0, si_stime=0} ---
exit_group(-1)                          = ?
+++ exited with 255 +++

附言 “bin.h”通过xxd -i /bin/nano > ./bin.h

linux
  • 1 1 个回答
  • 40 Views

1 个回答

  • Voted
  1. Best Answer
    eri
    2022-12-26T17:24:23Z2022-12-26T17:24:23Z
    #include "bin.h"
    #include <unistd.h>
    #include <stdio.h>
    #include <sys/stat.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    #include <sys/mman.h>
    
    
    int supervisor(){
        int ppid = getppid();
        return 0;
    };
    
    int main(int argc, char *argv[], char *envp[])
    {
        int r;
        int fd = memfd_create(argv[0], 0);
        write(fd, __bin, __bin_len);
    
        fchmod(fd, S_IRWXU);
        pid_t pid;
        switch (pid = fork())
        {
        case -1:
            perror("cant fork");
            exit(1);
        case 0:
            return supervisor();
        default:
            return fexecve(fd, argv, envp);
        }
        return 1;
    }
    
    • 1

相关问题

  • 如果 fuser -k number/tcp 没有帮助,如何在 Debian 中释放端口?

  • Ubuntu。startx 不起作用。黑屏

  • --syn 在 iptables 中有什么作用?

  • 为什么需要iso格式?

  • C程序中没有密码的sudo

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5