【问题标题】:how to fix 'SYSCALL execve': ls complains about a NULL argv[0]如何修复“SYSCALL execve”:ls 抱怨 NULL argv[0]
【发布时间】:2019-08-27 21:29:08
【问题描述】:

我创建了我的系统调用但没有找到。 这是我在 asm 中的代码

section .text
global _start
_start:
xor eax,eax

push eax
push dword '//ls'
push dword '/bin'
mov ebx,esp
mov al,0xb
int 0x80
;/bin//ls

当从这里构建一个静态的 32 位可执行文件并运行它时,我得到:

A NULL argv[0] was passed through an exec system call.
Aborted (core dumped)

这里发生了什么以及如何解决它?

【问题讨论】:

  • 为什么//ls中有两个斜线?
  • execve 需要 3 个参数:程序的路径、指向以 null 结尾的参数字符串数组的指针和指向以 null 结尾的环境变量字符串数组的指针。
  • 您似乎只推送了 2 个参数。此外,argv 数组的第一个元素中应包含程序名称。
  • @Barmar 从技术上讲,这是一个参数(ebx 指向堆栈上的一个区域)。
  • @barmar 不管你放多少 // 你。谢谢

标签: linux debugging assembly x86 system-calls


【解决方案1】:

您的代码按您的预期工作,进行成功的execve("/bin//ls", NULL, NULL) 系统调用。

问题在于 GNU lssh 或其他一些程序不同,在以空 argv[] 运行时选择 abort()。通常的约定是将程序名称作为第一个参数 (argv[0]) 传递。

您的程序可以使用/bin//sh 而不是ls 正常工作


execve 的 argv 和 envp 参数来自离开 ECX 和 EDX = 0,因为 Linux 选择在进入用户空间之前设置它们。 (这避免了内核信息泄漏而不是让它们未初始化)。显然,这在 shellcode 中是行不通的,因为 shellcode 会使用包含未知值的寄存器来运行。

顺便说一句,Linux execve() does document 它支持传递 envp 和/或 argv=NULL 而不是 argv = (char*[]){NULL};(指向 NULL 的指针)。它不会为不指向有效内存的argv 返回-EFAULT,而是将NULL 特例化为空数组。


我可以使用 ls (GNU coreutils) 8.31 在 Arch Linux 上重现你的结果

$ nasm -felf32 exec.asm
$ ld -melf_i386 -o exec exec.o

$ ./exec 
A NULL argv[0] was passed through an exec system call.
Aborted (core dumped)

您可以使用strace 确定该消息不是来自您的进程。

$ strace ./exec 
execve("./exec", ["./exec"], 0x7ffd7090ed90 /* 54 vars */) = 0  <=== starting your program
strace: [ Process PID=21152 runs in 32 bit mode. ]

execve("/bin//ls", NULL, NULL)          = 0         <==== This is your system call
  # then ls starts up normally
strace: [ Process PID=21152 runs in 64 bit mode. ]
brk(NULL)                               = 0x55b5fbaff000
arch_prctl(0x3001 /* ARCH_??? */, 0x7ffe1d74c5b0) = -1 EINVAL (Invalid argument)
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3

 ... normal dynamically-linked executable startup stuff ...

 ... until it writes a message to stderr and raises SIGABRT ...
write(2, "A NULL argv[0] was passed throug"..., 55A NULL argv[0] was passed through an exec system call.
) = 55
rt_sigprocmask(SIG_UNBLOCK, [ABRT], NULL, 8) = 0
rt_sigprocmask(SIG_BLOCK, ~[RTMIN RT_1], [], 8) = 0
getpid()                                = 21152
gettid()                                = 21152
tgkill(21152, 21152, SIGABRT)           = 0
rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
--- SIGABRT {si_signo=SIGABRT, si_code=SI_TKILL, si_pid=21152, si_uid=1000} ---
+++ killed by SIGABRT (core dumped) +++
Aborted (core dumped)

如果您查看ls 的源代码,您可能会看到abort() 函数调用,尤其是当您搜索它打印的字符串时。

正如我所说,bash 和大多数其他 shell 不这样做。


如果您确实想修复它,您可能需要创建一个指针数组 {"/bin//ls", NULL} 并将指向它的指针作为第二个参数传递。

    ...
    mov  ebx, esp     ; path

    push eax          ; 0
    push ebx          ; pointer to the filename
    mov  ecx, esp     ; argv = pointer to that array

    cdq               ; envp = EDX = 0 (broadcast high bit of EAX into EDX)

    mov  al, 0xb
    int  0x80

我添加了 EDX 调零以进行良好的测量。

这对我有用:ls 运行成功。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-30
    • 1970-01-01
    • 2021-11-09
    • 2015-10-06
    • 2019-09-14
    • 2022-12-15
    • 1970-01-01
    • 2022-07-19
    相关资源
    最近更新 更多