Elevate .text.syscall to .privileged

It turns out adding OpenBSD msyscall() origin verification broke the
--ftrace flag. The executable needs to issue raw syscalls while it's
rewriting itself. So they need to be in the same section, and that's
just plain simpler too.
This commit is contained in:
Justine Tunney 2021-02-06 03:17:41 -08:00
parent d7733579d3
commit 9f149e1de3
19 changed files with 30 additions and 38 deletions

View file

@ -283,9 +283,6 @@ SECTIONS {
KEEP(*(.textwindowsepilogue)) KEEP(*(.textwindowsepilogue))
*(SORT_BY_ALIGNMENT(.text.modernity)) *(SORT_BY_ALIGNMENT(.text.modernity))
*(SORT_BY_ALIGNMENT(.text.modernity.*)) *(SORT_BY_ALIGNMENT(.text.modernity.*))
HIDDEN(__text_syscall_start = .);
*(.text.syscall .text.syscall.*)
HIDDEN(__text_syscall_end = .);
*(SORT_BY_ALIGNMENT(.text.hot)) *(SORT_BY_ALIGNMENT(.text.hot))
*(SORT_BY_ALIGNMENT(.text.hot.*)) *(SORT_BY_ALIGNMENT(.text.hot.*))
KEEP(*(.keep.text)) KEEP(*(.keep.text))
@ -309,6 +306,7 @@ SECTIONS {
HIDDEN(__test_end = .); HIDDEN(__test_end = .);
. += 1; . += 1;
*(.privileged) *(.privileged)
HIDDEN(__privileged_end = .);
/*BEGIN: Read Only Data */ /*BEGIN: Read Only Data */
@ -487,9 +485,9 @@ SHSTUB2(.Lape.macho.dd.skip, RVA(ape.macho) / 8);
SHSTUB2(.Lape.macho.dd.count, (.Lape.macho.end - ape.macho) / 8); SHSTUB2(.Lape.macho.dd.count, (.Lape.macho.end - ape.macho) / 8);
PFSTUB4(.Lape.pe.offset, ape.pe - ape.mz); PFSTUB4(.Lape.pe.offset, ape.pe - ape.mz);
HIDDEN(__text_syscall_addr = ROUNDDOWN(__text_syscall_start, PAGESIZE)); HIDDEN(__privileged_addr = ROUNDDOWN(__privileged_start, PAGESIZE));
HIDDEN(__text_syscall_size = (ROUNDUP(__text_syscall_end, PAGESIZE) - HIDDEN(__privileged_size = (ROUNDUP(__privileged_end, PAGESIZE) -
ROUNDDOWN(__text_syscall_start, PAGESIZE))); ROUNDDOWN(__privileged_start, PAGESIZE)));
HIDDEN(.Lape.pe.optsz = .Lape.pe.sections - (ape.pe + 24)); HIDDEN(.Lape.pe.optsz = .Lape.pe.sections - (ape.pe + 24));
HIDDEN(.Lape.pe.shnum = (.Lape.pe.sections_end - .Lape.pe.sections) / 40); HIDDEN(.Lape.pe.shnum = (.Lape.pe.sections_end - .Lape.pe.sections) / 40);

View file

@ -17,7 +17,7 @@
PERFORMANCE OF THIS SOFTWARE. PERFORMANCE OF THIS SOFTWARE.
*/ */
#include "libc/macros.h" #include "libc/macros.h"
.text.syscall .privileged
/ Tiny Raw Linux Binary Tutorial / Tiny Raw Linux Binary Tutorial
/ /

View file

@ -34,7 +34,7 @@ extern __msabi typeof(VirtualProtect) *const __imp_VirtualProtect;
* @return 0 on success, or -1 w/ errno * @return 0 on success, or -1 w/ errno
* @see mmap() * @see mmap()
*/ */
textsyscall int mprotect(void *addr, uint64_t len, int prot) { privileged int mprotect(void *addr, uint64_t len, int prot) {
bool cf; bool cf;
int64_t rc; int64_t rc;
uint32_t oldprot; uint32_t oldprot;

View file

@ -17,7 +17,7 @@
PERFORMANCE OF THIS SOFTWARE. PERFORMANCE OF THIS SOFTWARE.
*/ */
#include "libc/macros.h" #include "libc/macros.h"
.text.syscall .privileged
__restore_rt_netbsd: __restore_rt_netbsd:
mov %r15,%rdi mov %r15,%rdi

View file

@ -67,9 +67,6 @@ SECTIONS {
.text : { .text : {
*(SORT_BY_ALIGNMENT(.text.hot)) *(SORT_BY_ALIGNMENT(.text.hot))
KEEP(*(.keep.text)) KEEP(*(.keep.text))
HIDDEN(__text_syscall_start = .);
*(.text.syscall .text.syscall.*)
HIDDEN(__text_syscall_end = .);
*(.text .text.*) *(.text .text.*)
KEEP(*(SORT_BY_NAME(.sort.text.*))) KEEP(*(SORT_BY_NAME(.sort.text.*)))
} }
@ -84,6 +81,7 @@ SECTIONS {
. = ALIGN(DEFINED(ftrace_init) ? PAGESIZE : 1); . = ALIGN(DEFINED(ftrace_init) ? PAGESIZE : 1);
HIDDEN(__privileged_start = .); HIDDEN(__privileged_start = .);
*(.privileged) *(.privileged)
HIDDEN(__privileged_end = .);
} }
.rodata : { .rodata : {
@ -212,6 +210,6 @@ SECTIONS {
} }
} }
HIDDEN(__text_syscall_addr = ROUNDDOWN(__text_syscall_start, PAGESIZE)); HIDDEN(__privileged_addr = ROUNDDOWN(__privileged_start, PAGESIZE));
HIDDEN(__text_syscall_size = (ROUNDUP(__text_syscall_end, PAGESIZE) - HIDDEN(__privileged_size = (ROUNDUP(__privileged_end, PAGESIZE) -
ROUNDDOWN(__text_syscall_start, PAGESIZE))); ROUNDDOWN(__privileged_start, PAGESIZE)));

View file

@ -620,7 +620,6 @@ typedef uint64_t uintmax_t;
#define textexit _Section(".text.exit") noinstrument #define textexit _Section(".text.exit") noinstrument
#define textreal _Section(".text.real") #define textreal _Section(".text.real")
#define textwindows _Section(".text.windows") #define textwindows _Section(".text.windows")
#define textsyscall _Section(".text.syscall") noinline
#define antiquity _Section(".text.antiquity") #define antiquity _Section(".text.antiquity")
#else #else
#define testonly #define testonly

View file

@ -411,7 +411,7 @@ static const char *__asan_describe_access_poison(char *p) {
} }
} }
static textsyscall wontreturn void __asan_exit(int rc) { static privileged noinline wontreturn void __asan_exit(int rc) {
if (!IsWindows()) { if (!IsWindows()) {
asm volatile("syscall" asm volatile("syscall"
: /* no outputs */ : /* no outputs */
@ -423,7 +423,7 @@ static textsyscall wontreturn void __asan_exit(int rc) {
} }
} }
static textsyscall ssize_t __asan_write(const void *data, size_t size) { static privileged noinline ssize_t __asan_write(const void *data, size_t size) {
ssize_t rc; ssize_t rc;
uint32_t wrote; uint32_t wrote;
if (!IsWindows()) { if (!IsWindows()) {

View file

@ -33,9 +33,6 @@
.macro .text.exit .macro .text.exit
.section .text.exit,"ax",@progbits .section .text.exit,"ax",@progbits
.endm .endm
.macro .text.syscall
.section .text.syscall,"ax",@progbits
.endm
.macro .firstclass .macro .firstclass
.section .text.hot,"ax",@progbits .section .text.hot,"ax",@progbits
.endm .endm

View file

@ -21,7 +21,7 @@
#include "libc/sysv/consts/sig.h" #include "libc/sysv/consts/sig.h"
#include "libc/sysv/consts/nr.h" #include "libc/sysv/consts/nr.h"
#include "libc/macros.h" #include "libc/macros.h"
.text.syscall .privileged
.source __FILE__ .source __FILE__
/ Terminates program abnormally. / Terminates program abnormally.

View file

@ -111,7 +111,7 @@ static int arch_prctl_freebsd(int code, int64_t addr) {
} }
} }
static textsyscall int arch_prctl_xnu(int code, int64_t addr) { static privileged noinline int arch_prctl_xnu(int code, int64_t addr) {
int ax; int ax;
switch (code) { switch (code) {
case ARCH_SET_GS: case ARCH_SET_GS:
@ -129,7 +129,7 @@ static textsyscall int arch_prctl_xnu(int code, int64_t addr) {
} }
} }
static textsyscall int arch_prctl_openbsd(int code, int64_t addr) { static privileged noinline int arch_prctl_openbsd(int code, int64_t addr) {
int64_t rax; int64_t rax;
switch (code) { switch (code) {
case ARCH_GET_FS: case ARCH_GET_FS:

View file

@ -41,7 +41,7 @@ static noasan char *__assert_stpcpy(char *d, const char *s) {
} }
} }
static textsyscall noasan wontreturn void __assert_exit(int rc) { static privileged noinline noasan wontreturn void __assert_exit(int rc) {
if (!IsWindows()) { if (!IsWindows()) {
asm volatile("syscall" asm volatile("syscall"
: /* no outputs */ : /* no outputs */
@ -53,8 +53,8 @@ static textsyscall noasan wontreturn void __assert_exit(int rc) {
} }
} }
static textsyscall noasan ssize_t __assert_write(const void *data, static privileged noinline noasan ssize_t __assert_write(const void *data,
size_t size) { size_t size) {
ssize_t rc; ssize_t rc;
uint32_t wrote; uint32_t wrote;
if (!IsWindows()) { if (!IsWindows()) {

View file

@ -19,7 +19,7 @@
#include "libc/dce.h" #include "libc/dce.h"
#include "libc/runtime/internal.h" #include "libc/runtime/internal.h"
#include "libc/macros.h" #include "libc/macros.h"
.text.syscall .privileged
.source __FILE__ .source __FILE__
/ Terminates process, ignoring destructors and atexit() handlers. / Terminates process, ignoring destructors and atexit() handlers.

View file

@ -48,7 +48,7 @@ static void __sys_print_nt(const void *data, size_t len) {
* @param len can be computed w/ tinystrlen() * @param len can be computed w/ tinystrlen()
* @clob nothing except flags * @clob nothing except flags
*/ */
textsyscall void __print(const void *data, size_t len) { privileged noinline void __print(const void *data, size_t len) {
int64_t ax, ordinal; int64_t ax, ordinal;
if (WasImported(__imp_WriteFile)) { if (WasImported(__imp_WriteFile)) {
__sys_print_nt(data, len); __sys_print_nt(data, len);

View file

@ -28,7 +28,7 @@
/** /**
* Aborts program under enemy fire to avoid being taken alive. * Aborts program under enemy fire to avoid being taken alive.
*/ */
textsyscall noasan void __stack_chk_fail(void) { privileged noasan void __stack_chk_fail(void) {
size_t len; size_t len;
const char *msg; const char *msg;
int64_t ax, cx, si; int64_t ax, cx, si;

View file

@ -18,7 +18,7 @@
*/ */
#include "libc/dce.h" #include "libc/dce.h"
#include "libc/macros.h" #include "libc/macros.h"
.text.syscall .privileged
/ Forks process without copying page tables. / Forks process without copying page tables.
/ /

View file

@ -17,7 +17,7 @@
PERFORMANCE OF THIS SOFTWARE. PERFORMANCE OF THIS SOFTWARE.
*/ */
#include "libc/macros.h" #include "libc/macros.h"
.text.syscall .privileged
.source __FILE__ .source __FILE__
/ Linux Signal Trampoline (HOLY CODE) / Linux Signal Trampoline (HOLY CODE)

View file

@ -103,7 +103,7 @@ __systemfive:
.endobj __systemfive,globl,hidden .endobj __systemfive,globl,hidden
.previous .previous
.text.syscall .privileged
.Lanchorpoint: .Lanchorpoint:
systemfive.linux: systemfive.linux:
and $0xfff,%eax and $0xfff,%eax
@ -332,10 +332,10 @@ systemfive.init.syscall:
js systemfive.init.done js systemfive.init.done
push %rdi push %rdi
push %rsi push %rsi
.weak __text_syscall_addr .weak __privileged_addr
.weak __text_syscall_size .weak __privileged_size
mov $__text_syscall_addr,%edi mov $__privileged_addr,%edi
mov $__text_syscall_size,%esi mov $__privileged_size,%esi
syscall syscall
pop %rsi pop %rsi
pop %rdi pop %rdi

View file

@ -1,3 +1,4 @@
#!/bin/sh
#-*-mode:sh;indent-tabs-mode:nil;tab-width:2;coding:utf-8-*-┐ #-*-mode:sh;indent-tabs-mode:nil;tab-width:2;coding:utf-8-*-┐
#───vi: set net ft=sh ts=2 sts=2 fenc=utf-8 :vi─────────────┘ #───vi: set net ft=sh ts=2 sts=2 fenc=utf-8 :vi─────────────┘
mkdir -p o/third_party mkdir -p o/third_party

View file

@ -176,7 +176,6 @@
"interruptfn" "interruptfn"
"nocallback" "nocallback"
"textstartup" "textstartup"
"textsyscall"
"warnifused" "warnifused"
"attributeallocsize" "attributeallocsize"
"attributeallocalign" "attributeallocalign"