// SPDX-License-Identifier: GPL-2.0-only /* * fsgsbase_restore.c, test ptrace vs fsgsbase * Copyright (c) 2020 Andy Lutomirski * * This test case simulates a tracer redirecting tracee execution to * a function and then restoring tracee state using PTRACE_GETREGS and * PTRACE_SETREGS. This is similar to what gdb does when doing * 'p func()'. The catch is that this test has the called function * modify a segment register. This makes sure that ptrace correctly * restores segment state when using PTRACE_SETREGS. * * This is not part of fsgsbase.c, because that test is 64-bit only.
*/
#ifdef __x86_64__ # define SEG "%gs" #else # define SEG "%fs" #endif
/* * Defined in clang_helpers_[32|64].S, because unlike gcc, clang inline asm does * not support segmentation prefixes.
*/ unsignedint dereference_seg_base(void);
if (ret != 0) {
printf("[NOTE]\tcould not create a segment -- can't test anything\n"); exit(0);
}
printf("\tusing GDT slot %d\n", desc.entry_number);
unsignedshort sel = (unsignedshort)((desc.entry_number << 3) | 0x3); asmvolatile ("mov %0, %" SEG :: "rm" (sel));
}
}
staticvoid tracee_zap_segment(void)
{ /* * The tracer will redirect execution here. This is meant to * work like gdb's 'p func()' feature. The tricky bit is that * we modify a segment register in order to make sure that ptrace * can correctly restore segment registers.
*/
printf("\tTracee: in tracee_zap_segment()\n");
/* * Write a nonzero selector with base zero to the segment register. * Using a null selector would defeat the test on AMD pre-Zen2 * CPUs, as such CPUs don't clear the base when loading a null * selector.
*/ unsignedshort sel; asmvolatile ("mov %%ss, %0\n\t" "mov %0, %" SEG
: "=rm" (sel));
pid_t pid = getpid(), tid = syscall(SYS_gettid);
printf("\tTracee is going back to sleep\n");
syscall(SYS_tgkill, pid, tid, SIGSTOP);
/* Should not get here. */ while (true) {
printf("[FAIL]\tTracee hit unreachable code\n");
pause();
}
}
int main()
{
printf("\tSetting up a segment\n");
init_seg();
unsignedint val = dereference_seg_base(); if (val != EXPECTED_VALUE) {
printf("[FAIL]\tseg[0] == %x; should be %x\n", val, EXPECTED_VALUE); return 1;
}
printf("[OK]\tThe segment points to the right place.\n");
pid_t chld = fork(); if (chld < 0)
err(1, "fork");
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.