【问题标题】:OpenBSD fails to execute a.outOpenBSD 无法执行 a.out
【发布时间】:2022-02-12 03:36:00
【问题描述】:

我编写了这个程序,应该以 exitcode 44 退出:

// prog.S
#include <sys/syscall.h>
        .text
        .globl _start
_start:
        subl $8, %esp
        pushl $44
        pushl $0
        movl $SYS_exit, %eax
        int $0x80

我编译用

$ cc prog.S -nostdlib -o a.out

然后运行

$./a.out

在 FreeBSD 13.0-RELEASE i386 (clang 11.0.1) 上这样做可以正常工作。事实上,可执行文件运行,程序的退出代码应该是 44。

但是,在 OpenBSD 7.0 GENERIC.MP#5 i386(clang 版本 11.1.0)和 NetBSD 9.2 i386(gcc 7.5.0)上执行相同操作,内核拒绝执行代码并将其传递给 shell ,当然失败了:

openbsd$ ./a.out
./a.out[1]: syntax error: `(' unexpected

奇怪的是那个文件说它是一个ELF二进制文件,因此应该由内核正常执行

openbsd$ file a.out

a.out: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), statically linked, not stripped

即使是 objdump 也会打印预期打印的内容

openbsd$ objdump -d a.out

a.out:     file format elf32-i386

Disassembly of section .text:

00001184 <_start>:
    1184:   83 ec 08                sub    $0x8,%esp
    1187:   6a 2c                   push   $0x2c
    1189:   b8 01 00 00 00          mov    $0x1,%eax
    118e:   6a 00                   push   $0x0
    1190:   cd 80                   int    $0x80


知道我做错了什么吗?

PS:在 OpenBSD 和 NetBSD 上使用 main 更改 _start 并在没有 -nostdlib 的情况下进行编译都可以正常工作

【问题讨论】:

  • 你确定它是正确的文件并且它的标记为可执行文件
  • @pm100 是的。它是 755
  • 它是正确的文件吗? file a.out 说什么
  • 在我放置的旧 OpenBSD 6.7 amd64 vm 上复制。我猜它在决定实际执行它之前对 ELF 可执行文件进行了一些额外的检查,如果这些检查失败,它决定将它作为一个 shell 脚本来尝试。我不知道这些检查可能是什么,以及为什么这个二进制文件失败了。
  • objdump 对此有何评论?

标签: assembly freebsd bsd openbsd netbsd


【解决方案1】:

我发现 OpenBSD 检查 .section ".note.openbsd.ident", "a"。如果它不存在,则不会执行该文件。如果我将 _start 替换为 main 并在没有 -nostdlib 的情况下链接,则可以在 libc 中找到 .note.openbsd.ident。 NetBSD 也是如此。

要了解更多信息,请访问https://www.exploit-db.com/papers/13219

【讨论】:

    【解决方案2】:

    在最新版本的 NetBSD 上,您还可以通过仅包含 /usr/lib/sysident.o 来链接以 _start 符号和 -nostdlib 开头的汇编语言程序,例如:

    as -o thello.o thello.s && ld -Bstatic -o thello /usr/lib/sysident.o thello.o
    

    在最近的 NetBSD 上,此对象还包括一个 .note.netbsd.pax 部分,用于控制 PaX 可执行安全功能,例如 Mprotect、ASLR 和 Segvguard。

    查看#include &lt;elf/common.h&gt; 文件,了解其他类型目标系统的类似注释部分的定义。

    另见https://polprog.net/blog/netbsdasmprog/

    另见https://github.com/robohack/experiments/blob/master/thello.s

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-18
      • 2017-02-08
      • 1970-01-01
      • 2014-05-26
      相关资源
      最近更新 更多