execve() 无法启动 C 程序

发布于 2024-12-20 14:51:06 字数 366 浏览 4 评论 0原文

我正在尝试在 Linux 上使用 unistd.h 中的 execve() 生成一个新进程。我尝试向其传递以下参数 execve("/bin/ls", "/bin/ls", NULL); 但没有得到结果。我也没有收到错误,程序只是退出。发生这种情况有原因吗?我尝试以 root 和普通用户身份启动它。我需要使用 execve() 的原因是因为我试图让它在程序集调用中工作,就像这样

program: db "/bin/ls",0

mov eax, 0xb
mov ebx, program
mov ecx, program
mov edx, 0
int 0x80

谢谢!

I am trying to spawn a new process using execve() from unistd.h on Linux. I have tried passing it the following parameters execve("/bin/ls", "/bin/ls", NULL); but get no result. I do not get an error either, the program just exits. Is there a reason why this is happening? I have tried launching it as root and regular user. The reason I need to use execve() is because I am trying to get it to work in an assembly call like so

program: db "/bin/ls",0

mov eax, 0xb
mov ebx, program
mov ecx, program
mov edx, 0
int 0x80

Thank you!

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

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

发布评论

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

评论(3

居里长安 2024-12-27 14:51:06

您传递给 execve 的参数是错误的。第二个和第三个必须是具有 NULL 标记值的 char 指针数组,而不是单个指针。

换句话说,就像:

#include <unistd.h>
int main (void) {
    char * const argv[] = {"/bin/ls", NULL};
    char * const envp[] = {NULL};
    int rc = execve ("/bin/ls", argv, envp);
    return rc;
}

当我运行它时,我确实得到了当前目录中的文件列表。

The arguments that you're passing to execve are wrong. Both the second and third must be an array of char pointers with a NULL sentinel value, not a single pointer.

In other words, something like:

#include <unistd.h>
int main (void) {
    char * const argv[] = {"/bin/ls", NULL};
    char * const envp[] = {NULL};
    int rc = execve ("/bin/ls", argv, envp);
    return rc;
}

When I run that, I do indeed get a list of the files in the current directory.

梦与时光遇 2024-12-27 14:51:06

man 页面,

int execve(const char *filename, char *const argv[], char *const envp[]);

所以你的情况的问题是你没有通过第二个和第三个参数正确。

/* execve.c */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int
main(int argc, char *argv[])
{
    char *newargv[] = { NULL, "hello", "world", NULL };
    char *newenviron[] = { NULL };


newargv[0] = argv[1];

execve(argv[1], newargv, newenviron);


}
//This is a over-simplified version of the example in the man page

运行如下:

$ cc execve.c -o execve
$ ./execve ls

From the man pages,

int execve(const char *filename, char *const argv[], char *const envp[]);

So the problem in your case is that you haven't passed the 2nd and the 3rd argument correctly.

/* execve.c */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int
main(int argc, char *argv[])
{
    char *newargv[] = { NULL, "hello", "world", NULL };
    char *newenviron[] = { NULL };


newargv[0] = argv[1];

execve(argv[1], newargv, newenviron);


}
//This is a over-simplified version of the example in the man page

Run this as:

$ cc execve.c -o execve
$ ./execve ls
や莫失莫忘 2024-12-27 14:51:06

尝试再次阅读 man execve 。您向其传递了错误的参数。特别注意第二个参数应该是什么。

另外,在 strace 下运行您的程序可能会很有启发。

Try reading man execve again. You are passing the wrong arguments to it. Pay particular attention to what the second argument should be.

Also, running your program under strace could be illuminating.

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