4

我编写了这个程序,它应该以 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:使用 main 更改 _start 并在没有 -nostdlib 的情况下进行编译在 OpenBSD 和 NetBSD 上都可以正常工作

4

2 回答 2

4

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

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

于 2022-02-07T14:14:43.603 回答
3

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

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 <elf/common.h>文件以了解其他类型目标系统的类似注释部分的定义。

另见https://polprog.n​​et/blog/netbsdasmprog/

另请参阅https://github.com/robohack/experiments/blob/master/thello.s

于 2022-02-11T19:36:00.697 回答